]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/client.c
Implement plugin engine to draw frame (Follow Master patch)
[mikachu/openbox.git] / openbox / client.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    client.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "client.h"
21 #include "debug.h"
22 #include "startupnotify.h"
23 #include "dock.h"
24 #include "screen.h"
25 #include "moveresize.h"
26 #include "ping.h"
27 #include "place.h"
28 #include "engine_interface.h"
29 #include "session.h"
30 #include "event.h"
31 #include "grab.h"
32 #include "prompt.h"
33 #include "focus.h"
34 #include "stacking.h"
35 #include "openbox.h"
36 #include "group.h"
37 #include "config.h"
38 #include "menuframe.h"
39 #include "keyboard.h"
40 #include "mouse.h"
41 #include "render/render.h"
42 #include "gettext.h"
43 #include "obt/display.h"
44 #include "obt/prop.h"
45 #include "window.h"
46
47 #ifdef HAVE_UNISTD_H
48 #  include <unistd.h>
49 #endif
50
51 #ifdef HAVE_SIGNAL_H
52 #  include <signal.h> /* for kill() */
53 #endif
54
55 #include <glib.h>
56 #include <X11/Xutil.h>
57
58 /*! The event mask to grab on client windows */
59 #define CLIENT_EVENTMASK (PropertyChangeMask | StructureNotifyMask | \
60                           ColormapChangeMask)
61
62 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
63                                 ButtonMotionMask)
64
65 typedef struct
66 {
67     ObClientCallback func;
68     gpointer data;
69 } ClientCallback;
70
71 GList            *client_list          = NULL;
72
73 static GSList *client_destroy_notifies = NULL;
74
75 static void client_get_all(ObClient *self, gboolean real);
76 static void client_get_startup_id(ObClient *self);
77 static void client_get_session_ids(ObClient *self);
78 static void client_get_area(ObClient *self);
79 static void client_get_desktop(ObClient *self);
80 static void client_get_state(ObClient *self);
81 static void client_get_shaped(ObClient *self);
82 static void client_get_mwm_hints(ObClient *self);
83 static void client_get_colormap(ObClient *self);
84 static void client_set_desktop_recursive(ObClient *self,
85                                          guint target,
86                                          gboolean donthide,
87                                          gboolean dontraise);
88 static void client_change_allowed_actions(ObClient *self);
89 static void client_change_state(ObClient *self);
90 static void client_change_wm_state(ObClient *self);
91 static void client_apply_startup_state(ObClient *self,
92                                        gint x, gint y, gint w, gint h);
93 static void client_restore_session_state(ObClient *self);
94 static gboolean client_restore_session_stacking(ObClient *self);
95 static ObAppSettings *client_get_settings_state(ObClient *self);
96 static void client_update_transient_tree(ObClient *self,
97                                          ObGroup *oldgroup, ObGroup *newgroup,
98                                          gboolean oldgtran, gboolean newgtran,
99                                          ObClient* oldparent,
100                                          ObClient *newparent);
101 static void client_present(ObClient *self, gboolean here, gboolean raise,
102                            gboolean unshade);
103 static GSList *client_search_all_top_parents_internal(ObClient *self,
104                                                       gboolean bylayer,
105                                                       ObStackingLayer layer);
106 static void client_call_notifies(ObClient *self, GSList *list);
107 static void client_ping_event(ObClient *self, gboolean dead);
108 static void client_prompt_kill(ObClient *self);
109
110
111 void client_startup(gboolean reconfig)
112 {
113     if (reconfig) return;
114
115     client_set_list();
116 }
117
118 void client_shutdown(gboolean reconfig)
119 {
120     if (reconfig) return;
121 }
122
123 static void client_call_notifies(ObClient *self, GSList *list)
124 {
125     GSList *it;
126
127     for (it = list; it; it = g_slist_next(it)) {
128         ClientCallback *d = it->data;
129         d->func(self, d->data);
130     }
131 }
132
133 void client_add_destroy_notify(ObClientCallback func, gpointer data)
134 {
135     ClientCallback *d = g_new(ClientCallback, 1);
136     d->func = func;
137     d->data = data;
138     client_destroy_notifies = g_slist_prepend(client_destroy_notifies, d);
139 }
140
141 void client_remove_destroy_notify(ObClientCallback func)
142 {
143     GSList *it;
144
145     for (it = client_destroy_notifies; it; it = g_slist_next(it)) {
146         ClientCallback *d = it->data;
147         if (d->func == func) {
148             g_free(d);
149             client_destroy_notifies =
150                 g_slist_delete_link(client_destroy_notifies, it);
151             break;
152         }
153     }
154 }
155
156 void client_set_list(void)
157 {
158     Window *windows, *win_it;
159     GList *it;
160     guint size = g_list_length(client_list);
161
162     /* create an array of the window ids */
163     if (size > 0) {
164         windows = g_new(Window, size);
165         win_it = windows;
166         for (it = client_list; it; it = g_list_next(it), ++win_it)
167             *win_it = ((ObClient*)it->data)->window;
168     } else
169         windows = NULL;
170
171     OBT_PROP_SETA32(obt_root(ob_screen), NET_CLIENT_LIST, WINDOW,
172                     (gulong*)windows, size);
173
174     if (windows)
175         g_free(windows);
176
177     stacking_set_list();
178 }
179
180 void client_manage(Window window, ObPrompt *prompt)
181 {
182     ObClient *self;
183     XSetWindowAttributes attrib_set;
184     gboolean activate = FALSE;
185     ObAppSettings *settings;
186     gboolean transient = FALSE;
187     Rect place, *monitor;
188     Time launch_time, map_time;
189
190     ob_debug("Managing window: 0x%lx", window);
191
192     map_time = event_get_server_time();
193
194     /* choose the events we want to receive on the CLIENT window
195        (ObPrompt windows can request events too) */
196     attrib_set.event_mask = CLIENT_EVENTMASK |
197         (prompt ? prompt->event_mask : 0);
198     attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
199     XChangeWindowAttributes(obt_display, window,
200                             CWEventMask|CWDontPropagate, &attrib_set);
201
202     /* create the ObClient struct, and populate it from the hints on the
203        window */
204     self = g_new0(ObClient, 1);
205     self->obwin.type = OB_WINDOW_CLASS_CLIENT;
206     self->window = window;
207     self->prompt = prompt;
208
209     /* non-zero defaults */
210     self->wmstate = WithdrawnState; /* make sure it gets updated first time */
211     self->gravity = NorthWestGravity;
212     self->desktop = screen_num_desktops; /* always an invalid value */
213
214     /* get all the stuff off the window */
215     client_get_all(self, TRUE);
216
217     ob_debug("Window type: %d", self->type);
218     ob_debug("Window group: 0x%x", self->group?self->group->leader:0);
219
220     /* now we have all of the window's information so we can set this up.
221        do this before creating the frame, so it can tell that we are still
222        mapping and doesn't go applying things right away */
223     client_setup_decor_and_functions(self, FALSE);
224
225     /* specify that if we exit, the window should not be destroyed and
226        should be reparented back to root automatically, unless we are managing
227        an internal ObPrompt window  */
228     if (!self->prompt)
229         XChangeSaveSet(obt_display, window, SetModeInsert);
230
231     /* create the decoration frame for the client window */
232     self->frame = render_plugin->frame_new(self);
233     render_plugin->frame_grab(self->frame, window_map);
234
235     /* we've grabbed everything and set everything that we need to at mapping
236        time now */
237     grab_server(FALSE);
238
239     /* per-app settings override stuff from client_get_all, and return the
240        settings for other uses too. the returned settings is a shallow copy,
241        that needs to be freed with g_free(). */
242     settings = client_get_settings_state(self);
243     /* the session should get the last say though */
244     client_restore_session_state(self);
245
246     /* tell startup notification that this app started */
247     launch_time = sn_app_started(self->startup_id, self->class, self->name);
248
249     /* do this after we have a frame.. it uses the frame to help determine the
250        WM_STATE to apply. */
251     client_change_state(self);
252
253     /* add ourselves to the focus order */
254     focus_order_add_new(self);
255
256     /* do this to add ourselves to the stacking list in a non-intrusive way */
257     client_calc_layer(self);
258
259     /* focus the new window? */
260     if (ob_state() != OB_STATE_STARTING &&
261         (!self->session || self->session->focused) &&
262         /* this means focus=true for window is same as config_focus_new=true */
263         ((config_focus_new || (settings && settings->focus == 1)) ||
264          client_search_focus_tree_full(self)) &&
265         /* this checks for focus=false for the window */
266         (!settings || settings->focus != 0) &&
267         focus_valid_target(self, FALSE, FALSE, TRUE, FALSE, FALSE))
268     {
269         activate = TRUE;
270     }
271
272     /* remove the client's border */
273     XSetWindowBorderWidth(obt_display, self->window, 0);
274
275     /* adjust the frame to the client's size before showing or placing
276        the window */
277     render_plugin->frame_set_client_area (self->frame, self->area);
278     render_plugin->frame_set_hover_flag (self->frame, OB_BUTTON_NONE);
279     render_plugin->frame_set_press_flag (self->frame, OB_BUTTON_NONE);
280     render_plugin->frame_set_is_focus (self->frame, FALSE);
281     render_plugin->frame_set_decorations (self->frame, self->decorations);
282     render_plugin->frame_update_layout (self->frame, FALSE, TRUE);
283     render_plugin->frame_update_skin (self->frame);
284
285     /* where the frame was placed is where the window was originally */
286     place = self->area;
287     monitor = screen_physical_area_monitor(screen_find_monitor(&place));
288
289     /* figure out placement for the window if the window is new */
290     if (ob_state() == OB_STATE_RUNNING) {
291         ob_debug("Positioned: %s @ %d %d",
292                  (!self->positioned ? "no" :
293                   (self->positioned == PPosition ? "program specified" :
294                    (self->positioned == USPosition ? "user specified" :
295                     (self->positioned == (PPosition | USPosition) ?
296                      "program + user specified" :
297                      "BADNESS !?")))), place.x, place.y);
298
299         ob_debug("Sized: %s @ %d %d",
300                  (!self->sized ? "no" :
301                   (self->sized == PSize ? "program specified" :
302                    (self->sized == USSize ? "user specified" :
303                     (self->sized == (PSize | USSize) ?
304                      "program + user specified" :
305                      "BADNESS !?")))), place.width, place.height);
306
307         /* splash screens are also returned as TRUE for transient,
308            and so will be forced on screen below */
309         transient = place_client(self, &place.x, &place.y, settings);
310
311         /* make sure the window is visible. */
312         client_find_onscreen(self, &place.x, &place.y,
313                              place.width, place.height,
314                              /* non-normal clients has less rules, and
315                                 windows that are being restored from a
316                                 session do also. we can assume you want
317                                 it back where you saved it. Clients saying
318                                 they placed themselves are subjected to
319                                 harder rules, ones that are placed by
320                                 place.c or by the user are allowed partially
321                                 off-screen and on xinerama divides (ie,
322                                 it is up to the placement routines to avoid
323                                 the xinerama divides)
324
325                                 splash screens get "transient" set to TRUE by
326                                 the place_client call
327                              */
328                              ob_state() == OB_STATE_RUNNING &&
329                              (transient ||
330                               (!((self->positioned & USPosition) ||
331                                  (settings && settings->pos_given)) &&
332                                client_normal(self) &&
333                                !self->session &&
334                                /* don't move oldschool fullscreen windows to
335                                   fit inside the struts (fixes Acroread, which
336                                   makes its fullscreen window fit the screen
337                                   but it is not USSize'd or USPosition'd) */
338                                !(self->decorations == 0 &&
339                                  RECT_EQUAL(place, *monitor)))));
340     }
341
342     /* if the window isn't user-sized, then make it fit inside
343        the visible screen area on its monitor. Use basically the same rules
344        for forcing the window on screen in the client_find_onscreen call.
345
346        do this after place_client, it chooses the monitor!
347
348        splash screens get "transient" set to TRUE by
349        the place_client call
350     */
351     if (ob_state() == OB_STATE_RUNNING &&
352         (transient ||
353          (!(self->sized & USSize || self->positioned & USPosition) &&
354           client_normal(self) &&
355           !self->session &&
356           /* don't shrink oldschool fullscreen windows to fit inside the
357              struts (fixes Acroread, which makes its fullscreen window
358              fit the screen but it is not USSize'd or USPosition'd) */
359           !(self->decorations == 0 && RECT_EQUAL(place, *monitor)))))
360     {
361         Rect *a = screen_area(self->desktop, SCREEN_AREA_ONE_MONITOR, &place);
362
363         Strut size = render_plugin->frame_get_size(self->frame);
364         /* get the size of the frame */
365         place.width += size.left + size.right;
366         place.height += size.top + size.bottom;
367
368         /* fit the window inside the area */
369         place.width = MIN(place.width, a->width);
370         place.height = MIN(place.height, a->height);
371
372         ob_debug("setting window size to %dx%d", place.width, place.height);
373
374         /* get the size of the client back */
375         place.width -= size.left + size.right;
376         place.height -= size.top + size.bottom;
377
378         g_free(a);
379     }
380
381     ob_debug("placing window 0x%x at %d, %d with size %d x %d. "
382              "some restrictions may apply",
383              self->window, place.x, place.y, place.width, place.height);
384     if (self->session)
385         ob_debug("  but session requested %d, %d  %d x %d instead, "
386                  "overriding",
387                  self->session->x, self->session->y,
388                  self->session->w, self->session->h);
389
390     /* do this after the window is placed, so the premax/prefullscreen numbers
391        won't be all wacko!!
392
393        this also places the window
394     */
395     client_apply_startup_state(self, place.x, place.y,
396                                place.width, place.height);
397
398     g_free(monitor);
399     monitor = NULL;
400
401     ob_debug_type(OB_DEBUG_FOCUS, "Going to try activate new window? %s",
402                   activate ? "yes" : "no");
403     if (activate) {
404         gboolean raise = FALSE;
405
406         /* This is focus stealing prevention */
407         ob_debug_type(OB_DEBUG_FOCUS,
408                       "Want to focus new window 0x%x at time %u "
409                       "launched at %u (last user interaction time %u)",
410                       self->window, map_time, launch_time,
411                       event_last_user_time);
412
413         if (menu_frame_visible || render_plugin->moveresize_in_progress) {
414             activate = FALSE;
415             raise = TRUE;
416             ob_debug_type(OB_DEBUG_FOCUS,
417                           "Not focusing the window because the user is inside "
418                           "an Openbox menu or is move/resizing a window and "
419                           "we don't want to interrupt them");
420         }
421
422         /* if it's on another desktop */
423         else if (!(self->desktop == screen_desktop ||
424                    self->desktop == DESKTOP_ALL) &&
425                  /* the timestamp is from before you changed desktops */
426                  launch_time && screen_desktop_user_time &&
427                  !event_time_after(launch_time, screen_desktop_user_time))
428         {
429             activate = FALSE;
430             raise = TRUE;
431             ob_debug_type(OB_DEBUG_FOCUS,
432                           "Not focusing the window because its on another "
433                           "desktop");
434         }
435         /* If something is focused, and it's not our relative... */
436         else if (focus_client && client_search_focus_tree_full(self) == NULL &&
437                  client_search_focus_group_full(self) == NULL)
438         {
439             /* If the user is working in another window right now, then don't
440                steal focus */
441             if (event_last_user_time && launch_time &&
442                 event_time_after(event_last_user_time, launch_time) &&
443                 event_last_user_time != launch_time &&
444                 event_time_after(event_last_user_time,
445                                  map_time - OB_EVENT_USER_TIME_DELAY))
446             {
447                 activate = FALSE;
448                 ob_debug_type(OB_DEBUG_FOCUS,
449                               "Not focusing the window because the user is "
450                               "working in another window");
451             }
452             /* If its a transient (and its parents aren't focused) */
453             else if (client_has_parent(self)) {
454                 activate = FALSE;
455                 ob_debug_type(OB_DEBUG_FOCUS,
456                               "Not focusing the window because it is a "
457                               "transient, and its relatives aren't focused");
458             }
459             /* Don't steal focus from globally active clients.
460                I stole this idea from KWin. It seems nice.
461              */
462             else if (!(focus_client->can_focus ||
463                        focus_client->focus_notify))
464             {
465                 activate = FALSE;
466                 ob_debug_type(OB_DEBUG_FOCUS,
467                               "Not focusing the window because a globally "
468                               "active client has focus");
469             }
470             /* Don't move focus if it's not going to go to this window
471                anyway */
472             else if (client_focus_target(self) != self) {
473                 activate = FALSE;
474                 raise = TRUE;
475                 ob_debug_type(OB_DEBUG_FOCUS,
476                               "Not focusing the window because another window "
477                               "would get the focus anyway");
478             }
479             else if (!(self->desktop == screen_desktop ||
480                        self->desktop == DESKTOP_ALL))
481             {
482                 activate = FALSE;
483                 raise = TRUE;
484                 ob_debug_type(OB_DEBUG_FOCUS,
485                               "Not focusing the window because it is on "
486                               "another desktop and no relatives are focused ");
487             }
488         }
489
490         if (!activate) {
491             ob_debug_type(OB_DEBUG_FOCUS,
492                           "Focus stealing prevention activated for %s at "
493                           "time %u (last user interactioon time %u)",
494                           self->title, map_time, event_last_user_time);
495             /* if the client isn't focused, then hilite it so the user
496                knows it is there */
497             client_hilite(self, TRUE);
498             /* we may want to raise it even tho we're not activating it */
499             if (raise && !client_restore_session_stacking(self))
500                 stacking_raise(CLIENT_AS_WINDOW(self));
501         }
502     }
503     else {
504         /* This may look rather odd. Well it's because new windows are added
505            to the stacking order non-intrusively. If we're not going to focus
506            the new window or hilite it, then we raise it to the top. This will
507            take affect for things that don't get focused like splash screens.
508            Also if you don't have focus_new enabled, then it's going to get
509            raised to the top. Legacy begets legacy I guess?
510         */
511         if (!client_restore_session_stacking(self))
512             stacking_raise(CLIENT_AS_WINDOW(self));
513     }
514
515     mouse_grab_for_client(self, TRUE);
516
517     /* this has to happen before we try focus the window, but we want it to
518        happen after the client's stacking has been determined or it looks bad
519     */
520     {
521         gulong ignore_start;
522         if (!config_focus_under_mouse)
523             ignore_start = event_start_ignore_all_enters();
524
525         client_show(self);
526
527         if (!config_focus_under_mouse)
528             event_end_ignore_all_enters(ignore_start);
529     }
530
531     if (activate) {
532         gboolean stacked = client_restore_session_stacking(self);
533         client_present(self, FALSE, !stacked, TRUE);
534     }
535
536     /* add to client list/map */
537     client_list = g_list_append(client_list, self);
538     window_add(&self->window, CLIENT_AS_WINDOW(self));
539
540     /* this has to happen after we're in the client_list */
541     if (STRUT_EXISTS(self->strut))
542         screen_update_areas();
543
544     /* update the list hints */
545     client_set_list();
546
547     /* watch for when the application stops responding.  only do this for
548        normal windows, i.e. windows which have titlebars and close buttons 
549        and things like that.
550        we don't need to stop pinging on unmanage, because it will be handled
551        automatically by the destroy callback!
552     */
553     if (self->ping && client_normal(self))
554         ping_start(self, client_ping_event);
555
556     /* free the ObAppSettings shallow copy */
557     g_free(settings);
558
559     ob_debug("Managed window 0x%lx plate 0x%x (%s)",
560              window, render_plugin->frame_get_window(self->frame), self->class);
561 }
562
563
564 ObClient *client_fake_manage(Window window)
565 {
566     ObClient *self;
567     ObAppSettings *settings;
568
569     ob_debug("Pretend-managing window: %lx", window);
570
571     /* do this minimal stuff to figure out the client's decorations */
572
573     self = g_new0(ObClient, 1);
574     self->window = window;
575
576     client_get_all(self, FALSE);
577     /* per-app settings override stuff, and return the settings for other
578        uses too. this returns a shallow copy that needs to be freed */
579     settings = client_get_settings_state(self);
580
581     client_setup_decor_and_functions(self, FALSE);
582
583     /* create the decoration frame for the client window and adjust its size */
584     self->frame = render_plugin->frame_new(self);
585     render_plugin->frame_set_decorations (self->frame, self->decorations);
586     render_plugin->frame_update_layout (self->frame, FALSE, FALSE);
587
588     ob_debug("gave extents left %d right %d top %d bottom %d",
589              render_plugin->frame_get_size(self->frame).left, render_plugin->frame_get_size(self->frame).right,
590              render_plugin->frame_get_size(self->frame).top, render_plugin->frame_get_size(self->frame).bottom);
591
592     /* free the ObAppSettings shallow copy */
593     g_free(settings);
594
595     return self;
596 }
597
598 void client_unmanage_all(void)
599 {
600     while (client_list != NULL)
601         client_unmanage(client_list->data);
602 }
603
604 void client_unmanage(ObClient *self)
605 {
606     guint j;
607     GSList *it;
608     gulong ignore_start;
609
610     ob_debug("Unmanaging window: 0x%x plate 0x%x (%s) (%s)",
611              self->window, render_plugin->frame_get_window(self->frame),
612              self->class, self->title ? self->title : "");
613
614     g_assert(self != NULL);
615
616     /* we dont want events no more. do this before hiding the frame so we
617        don't generate more events */
618     XSelectInput(obt_display, self->window, NoEventMask);
619
620     /* ignore enter events from the unmap so it doesnt mess with the focus */
621     if (!config_focus_under_mouse)
622         ignore_start = event_start_ignore_all_enters();
623
624     self->ignore_unmaps += render_plugin->frame_hide(self->frame);
625     /* flush to send the hide to the server quickly */
626     XFlush(obt_display);
627
628     if (!config_focus_under_mouse)
629         event_end_ignore_all_enters(ignore_start);
630
631     mouse_grab_for_client(self, FALSE);
632
633     /* remove the window from our save set, unless we are managing an internal
634        ObPrompt window */
635     if (!self->prompt)
636         XChangeSaveSet(obt_display, self->window, SetModeDelete);
637
638     /* update the focus lists */
639     focus_order_remove(self);
640     if (client_focused(self)) {
641         /* don't leave an invalid focus_client */
642         focus_client = NULL;
643     }
644
645     /* if we're prompting to kill the client, close that */
646     prompt_unref(self->kill_prompt);
647     self->kill_prompt = NULL;
648
649     client_list = g_list_remove(client_list, self);
650     stacking_remove(self);
651     window_remove(self->window);
652
653     /* once the client is out of the list, update the struts to remove its
654        influence */
655     if (STRUT_EXISTS(self->strut))
656         screen_update_areas();
657
658     client_call_notifies(self, client_destroy_notifies);
659
660     /* tell our parent(s) that we're gone */
661     for (it = self->parents; it; it = g_slist_next(it))
662         ((ObClient*)it->data)->transients =
663             g_slist_remove(((ObClient*)it->data)->transients,self);
664
665     /* tell our transients that we're gone */
666     for (it = self->transients; it; it = g_slist_next(it)) {
667         ((ObClient*)it->data)->parents =
668             g_slist_remove(((ObClient*)it->data)->parents, self);
669         /* we could be keeping our children in a higher layer */
670         client_calc_layer(it->data);
671     }
672
673     /* remove from its group */
674     if (self->group) {
675         group_remove(self->group, self);
676         self->group = NULL;
677     }
678
679     /* restore the window's original geometry so it is not lost */
680     {
681         Rect a;
682
683         a = self->area;
684
685         if (self->fullscreen)
686             a = self->pre_fullscreen_area;
687         else if (self->max_horz || self->max_vert) {
688             if (self->max_horz) {
689                 a.x = self->pre_max_area.x;
690                 a.width = self->pre_max_area.width;
691             }
692             if (self->max_vert) {
693                 a.y = self->pre_max_area.y;
694                 a.height = self->pre_max_area.height;
695             }
696         }
697
698         self->fullscreen = self->max_horz = self->max_vert = FALSE;
699         /* let it be moved and resized no matter what */
700         self->functions = OB_CLIENT_FUNC_MOVE | OB_CLIENT_FUNC_RESIZE;
701         self->decorations = 0; /* unmanaged windows have no decor */
702
703         /* give the client its border back */
704         XSetWindowBorderWidth(obt_display, self->window, self->border_width);
705
706         client_move_resize(self, a.x, a.y, a.width, a.height);
707     }
708
709     /* reparent the window out of the frame, and free the frame */
710     render_plugin->frame_ungrab(self->frame, window_map);
711     render_plugin->frame_free(self->frame);
712     self->frame = NULL;
713
714     if (ob_state() != OB_STATE_EXITING) {
715         /* these values should not be persisted across a window
716            unmapping/mapping */
717         OBT_PROP_ERASE(self->window, NET_WM_DESKTOP);
718         OBT_PROP_ERASE(self->window, NET_WM_STATE);
719         OBT_PROP_ERASE(self->window, WM_STATE);
720     } else {
721         /* if we're left in an unmapped state, the client wont be mapped.
722            this is bad, since we will no longer be managing the window on
723            restart */
724         XMapWindow(obt_display, self->window);
725     }
726
727     /* these should not be left on the window ever.  other window managers
728        don't necessarily use them and it will mess them up (like compiz) */
729     OBT_PROP_ERASE(self->window, NET_WM_VISIBLE_NAME);
730     OBT_PROP_ERASE(self->window, NET_WM_VISIBLE_ICON_NAME);
731
732     /* update the list hints */
733     client_set_list();
734
735     ob_debug("Unmanaged window 0x%lx", self->window);
736
737     /* free all data allocated in the client struct */
738     g_slist_free(self->transients);
739     for (j = 0; j < self->nicons; ++j)
740         g_free(self->icons[j].data);
741     if (self->nicons > 0)
742         g_free(self->icons);
743     g_free(self->startup_id);
744     g_free(self->wm_command);
745     g_free(self->title);
746     g_free(self->icon_title);
747     g_free(self->original_title);
748     g_free(self->name);
749     g_free(self->class);
750     g_free(self->role);
751     g_free(self->client_machine);
752     g_free(self->sm_client_id);
753     g_free(self);
754 }
755
756 void client_fake_unmanage(ObClient *self)
757 {
758     /* this is all that got allocated to get the decorations */
759
760     render_plugin->frame_free(self->frame);
761     g_free(self);
762 }
763
764 /*! Returns a new structure containing the per-app settings for this client.
765   The returned structure needs to be freed with g_free. */
766 static ObAppSettings *client_get_settings_state(ObClient *self)
767 {
768     ObAppSettings *settings;
769     GSList *it;
770
771     settings = config_create_app_settings();
772
773     for (it = config_per_app_settings; it; it = g_slist_next(it)) {
774         ObAppSettings *app = it->data;
775         gboolean match = TRUE;
776
777         g_assert(app->name != NULL || app->class != NULL);
778
779         /* we know that either name or class is not NULL so it will have to
780            match to use the rule */
781         if (app->name &&
782             !g_pattern_match(app->name, strlen(self->name), self->name, NULL))
783             match = FALSE;
784         else if (app->class &&
785                 !g_pattern_match(app->class,
786                                  strlen(self->class), self->class, NULL))
787             match = FALSE;
788         else if (app->role &&
789                  !g_pattern_match(app->role,
790                                   strlen(self->role), self->role, NULL))
791             match = FALSE;
792
793         if (match) {
794             ob_debug("Window matching: %s", app->name);
795
796             /* copy the settings to our struct, overriding the existing
797                settings if they are not defaults */
798             config_app_settings_copy_non_defaults(app, settings);
799         }
800     }
801
802     if (settings->shade != -1)
803         self->shaded = !!settings->shade;
804     if (settings->decor != -1)
805         self->undecorated = !settings->decor;
806     if (settings->iconic != -1)
807         self->iconic = !!settings->iconic;
808     if (settings->skip_pager != -1)
809         self->skip_pager = !!settings->skip_pager;
810     if (settings->skip_taskbar != -1)
811         self->skip_taskbar = !!settings->skip_taskbar;
812
813     if (settings->max_vert != -1)
814         self->max_vert = !!settings->max_vert;
815     if (settings->max_horz != -1)
816         self->max_horz = !!settings->max_horz;
817
818     if (settings->fullscreen != -1)
819         self->fullscreen = !!settings->fullscreen;
820
821     if (settings->desktop) {
822         if (settings->desktop == DESKTOP_ALL)
823             self->desktop = settings->desktop;
824         else if (settings->desktop > 0 &&
825                  settings->desktop <= screen_num_desktops)
826             self->desktop = settings->desktop - 1;
827     }
828
829     if (settings->layer == -1) {
830         self->below = TRUE;
831         self->above = FALSE;
832     }
833     else if (settings->layer == 0) {
834         self->below = FALSE;
835         self->above = FALSE;
836     }
837     else if (settings->layer == 1) {
838         self->below = FALSE;
839         self->above = TRUE;
840     }
841     return settings;
842 }
843
844 static void client_restore_session_state(ObClient *self)
845 {
846     GList *it;
847
848     ob_debug_type(OB_DEBUG_SM,
849                   "Restore session for client %s", self->title);
850
851     if (!(it = session_state_find(self))) {
852         ob_debug_type(OB_DEBUG_SM,
853                       "Session data not found for client %s", self->title);
854         return;
855     }
856
857     self->session = it->data;
858
859     ob_debug_type(OB_DEBUG_SM, "Session data loaded for client %s",
860                   self->title);
861
862     RECT_SET_POINT(self->area, self->session->x, self->session->y);
863     self->positioned = USPosition;
864     self->sized = USSize;
865     if (self->session->w > 0)
866         self->area.width = self->session->w;
867     if (self->session->h > 0)
868         self->area.height = self->session->h;
869     XResizeWindow(obt_display, self->window,
870                   self->area.width, self->area.height);
871
872     self->desktop = (self->session->desktop == DESKTOP_ALL ?
873                      self->session->desktop :
874                      MIN(screen_num_desktops - 1, self->session->desktop));
875     OBT_PROP_SET32(self->window, NET_WM_DESKTOP, CARDINAL, self->desktop);
876
877     self->shaded = self->session->shaded;
878     self->iconic = self->session->iconic;
879     self->skip_pager = self->session->skip_pager;
880     self->skip_taskbar = self->session->skip_taskbar;
881     self->fullscreen = self->session->fullscreen;
882     self->above = self->session->above;
883     self->below = self->session->below;
884     self->max_horz = self->session->max_horz;
885     self->max_vert = self->session->max_vert;
886     self->undecorated = self->session->undecorated;
887
888     render_plugin->frame_set_is_max_horz (self->frame, self->max_horz);
889     render_plugin->frame_set_is_max_vert (self->frame, self->max_vert);
890 }
891
892 static gboolean client_restore_session_stacking(ObClient *self)
893 {
894     GList *it, *mypos;
895
896     if (!self->session) return FALSE;
897
898     mypos = g_list_find(session_saved_state, self->session);
899     if (!mypos) return FALSE;
900
901     /* start above me and look for the first client */
902     for (it = g_list_previous(mypos); it; it = g_list_previous(it)) {
903         GList *cit;
904
905         for (cit = client_list; cit; cit = g_list_next(cit)) {
906             ObClient *c = cit->data;
907             /* found a client that was in the session, so go below it */
908             if (c->session == it->data) {
909                 stacking_below(CLIENT_AS_WINDOW(self),
910                                CLIENT_AS_WINDOW(cit->data));
911                 return TRUE;
912             }
913         }
914     }
915     return FALSE;
916 }
917
918 void client_move_onscreen(ObClient *self, gboolean rude)
919 {
920     gint x = self->area.x;
921     gint y = self->area.y;
922     if (client_find_onscreen(self, &x, &y,
923                              self->area.width,
924                              self->area.height, rude)) {
925         client_move(self, x, y);
926     }
927 }
928
929 gboolean client_find_onscreen(ObClient *self, gint *x, gint *y, gint w, gint h,
930                               gboolean rude)
931 {
932     gint ox = *x, oy = *y;
933     gboolean rudel = rude, ruder = rude, rudet = rude, rudeb = rude;
934     gint fw, fh;
935     Rect desired;
936     guint i;
937     gboolean found_mon;
938
939     RECT_SET(desired, *x, *y, w, h);
940     frame_rect_to_frame(self, &desired);
941
942     /* get where the frame would be */
943     frame_client_gravity(self, x, y);
944
945     Strut size = render_plugin->frame_get_size(self->frame);
946
947     /* get the requested size of the window with decorations */
948     fw = size.left + w + size.right;
949     fh = size.top + h + size.bottom;
950
951     /* If rudeness wasn't requested, then still be rude in a given direction
952        if the client is not moving, only resizing in that direction */
953     if (!rude) {
954         Point oldtl, oldtr, oldbl, oldbr;
955         Point newtl, newtr, newbl, newbr;
956         gboolean stationary_l, stationary_r, stationary_t, stationary_b;
957
958         Rect area = render_plugin->frame_get_window_area(self->frame);
959         POINT_SET(oldtl, area.x, area.y);
960         POINT_SET(oldbr, area.x + area.width - 1,
961                   area.y + area.height - 1);
962         POINT_SET(oldtr, oldbr.x, oldtl.y);
963         POINT_SET(oldbl, oldtl.x, oldbr.y);
964
965         POINT_SET(newtl, *x, *y);
966         POINT_SET(newbr, *x + fw - 1, *y + fh - 1);
967         POINT_SET(newtr, newbr.x, newtl.y);
968         POINT_SET(newbl, newtl.x, newbr.y);
969
970         /* is it moving or just resizing from some corner? */
971         stationary_l = oldtl.x == newtl.x;
972         stationary_r = oldtr.x == newtr.x;
973         stationary_t = oldtl.y == newtl.y;
974         stationary_b = oldbl.y == newbl.y;
975
976         /* if left edge is growing and didnt move right edge */
977         if (stationary_r && newtl.x < oldtl.x)
978             rudel = TRUE;
979         /* if right edge is growing and didnt move left edge */
980         if (stationary_l && newtr.x > oldtr.x)
981             ruder = TRUE;
982         /* if top edge is growing and didnt move bottom edge */
983         if (stationary_b && newtl.y < oldtl.y)
984             rudet = TRUE;
985         /* if bottom edge is growing and didnt move top edge */
986         if (stationary_t && newbl.y > oldbl.y)
987             rudeb = TRUE;
988     }
989
990     /* we iterate through every monitor that the window is at least partially
991        on, to make sure it is obeying the rules on them all
992
993        if the window does not appear on any monitors, then use the first one
994     */
995     found_mon = FALSE;
996     for (i = 0; i < screen_num_monitors; ++i) {
997         Rect *a;
998
999         if (!screen_physical_area_monitor_contains(i, &desired)) {
1000             if (i < screen_num_monitors - 1 || found_mon)
1001                 continue;
1002
1003             /* the window is not inside any monitor! so just use the first
1004                one */
1005             a = screen_area(self->desktop, 0, NULL);
1006         } else {
1007             found_mon = TRUE;
1008             a = screen_area(self->desktop, SCREEN_AREA_ONE_MONITOR, &desired);
1009         }
1010
1011         /* This makes sure windows aren't entirely outside of the screen so you
1012            can't see them at all.
1013            It makes sure 10% of the window is on the screen at least. At don't
1014            let it move itself off the top of the screen, which would hide the
1015            titlebar on you. (The user can still do this if they want too, it's
1016            only limiting the application.
1017         */
1018         if (client_normal(self)) {
1019             if (!self->strut.right && *x + fw/10 >= a->x + a->width - 1)
1020                 *x = a->x + a->width - fw/10;
1021             if (!self->strut.bottom && *y + fh/10 >= a->y + a->height - 1)
1022                 *y = a->y + a->height - fh/10;
1023             if (!self->strut.left && *x + fw*9/10 - 1 < a->x)
1024                 *x = a->x - fw*9/10;
1025             if (!self->strut.top && *y + fh*9/10 - 1 < a->y)
1026                 *y = a->y - fh*9/10;
1027         }
1028
1029         /* This here doesn't let windows even a pixel outside the
1030            struts/screen. When called from client_manage, programs placing
1031            themselves are forced completely onscreen, while things like
1032            xterm -geometry resolution-width/2 will work fine. Trying to
1033            place it completely offscreen will be handled in the above code.
1034            Sorry for this confused comment, i am tired. */
1035         if (rudel && !self->strut.left && *x < a->x) *x = a->x;
1036         if (ruder && !self->strut.right && *x + fw > a->x + a->width)
1037             *x = a->x + MAX(0, a->width - fw);
1038
1039         if (rudet && !self->strut.top && *y < a->y) *y = a->y;
1040         if (rudeb && !self->strut.bottom && *y + fh > a->y + a->height)
1041             *y = a->y + MAX(0, a->height - fh);
1042
1043         g_free(a);
1044     }
1045
1046     /* get where the client should be */
1047     frame_frame_gravity(self, x, y);
1048
1049     return ox != *x || oy != *y;
1050 }
1051
1052 static void client_get_all(ObClient *self, gboolean real)
1053 {
1054     /* this is needed for the frame to set itself up */
1055     client_get_area(self);
1056
1057     /* these things can change the decor and functions of the window */
1058
1059     client_get_mwm_hints(self);
1060     /* this can change the mwmhints for special cases */
1061     client_get_type_and_transientness(self);
1062     client_get_state(self);
1063     client_update_normal_hints(self);
1064
1065     /* get the session related properties, these can change decorations
1066        from per-app settings */
1067     client_get_session_ids(self);
1068
1069     /* now we got everything that can affect the decorations */
1070     if (!real)
1071         return;
1072
1073     /* get this early so we have it for debugging */
1074     client_update_title(self);
1075
1076     client_update_protocols(self);
1077
1078     client_update_wmhints(self);
1079     /* this may have already been called from client_update_wmhints */
1080     if (!self->parents && !self->transient_for_group)
1081         client_update_transient_for(self);
1082
1083     client_get_startup_id(self);
1084     client_get_desktop(self);/* uses transient data/group/startup id if a
1085                                 desktop is not specified */
1086     client_get_shaped(self);
1087
1088     {
1089         /* a couple type-based defaults for new windows */
1090
1091         /* this makes sure that these windows appear on all desktops */
1092         if (self->type == OB_CLIENT_TYPE_DESKTOP)
1093             self->desktop = DESKTOP_ALL;
1094     }
1095
1096 #ifdef SYNC
1097     client_update_sync_request_counter(self);
1098 #endif
1099
1100     client_get_colormap(self);
1101     client_update_strut(self);
1102     client_update_icons(self);
1103     client_update_icon_geometry(self);
1104 }
1105
1106 static void client_get_startup_id(ObClient *self)
1107 {
1108     if (!(OBT_PROP_GETS(self->window, NET_STARTUP_ID, utf8,
1109                         &self->startup_id)))
1110         if (self->group)
1111             OBT_PROP_GETS(self->group->leader,
1112                           NET_STARTUP_ID, utf8, &self->startup_id);
1113 }
1114
1115 static void client_get_area(ObClient *self)
1116 {
1117     XWindowAttributes wattrib;
1118     Status ret;
1119
1120     ret = XGetWindowAttributes(obt_display, self->window, &wattrib);
1121     g_assert(ret != BadWindow);
1122
1123     RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
1124     POINT_SET(self->root_pos, wattrib.x, wattrib.y);
1125     self->border_width = wattrib.border_width;
1126
1127     ob_debug("client area: %d %d  %d %d  bw %d", wattrib.x, wattrib.y,
1128              wattrib.width, wattrib.height, wattrib.border_width);
1129 }
1130
1131 static void client_get_desktop(ObClient *self)
1132 {
1133     guint32 d = screen_num_desktops; /* an always-invalid value */
1134
1135     if (OBT_PROP_GET32(self->window, NET_WM_DESKTOP, CARDINAL, &d)) {
1136         if (d >= screen_num_desktops && d != DESKTOP_ALL)
1137             self->desktop = screen_num_desktops - 1;
1138         else
1139             self->desktop = d;
1140         ob_debug("client requested desktop 0x%x", self->desktop);
1141     } else {
1142         GSList *it;
1143         gboolean first = TRUE;
1144         guint all = screen_num_desktops; /* not a valid value */
1145
1146         /* if they are all on one desktop, then open it on the
1147            same desktop */
1148         for (it = self->parents; it; it = g_slist_next(it)) {
1149             ObClient *c = it->data;
1150
1151             if (c->desktop == DESKTOP_ALL) continue;
1152
1153             if (first) {
1154                 all = c->desktop;
1155                 first = FALSE;
1156             }
1157             else if (all != c->desktop)
1158                 all = screen_num_desktops; /* make it invalid */
1159         }
1160         if (all != screen_num_desktops) {
1161             self->desktop = all;
1162
1163             ob_debug("client desktop set from parents: 0x%x",
1164                      self->desktop);
1165         }
1166         /* try get from the startup-notification protocol */
1167         else if (sn_get_desktop(self->startup_id, &self->desktop)) {
1168             if (self->desktop >= screen_num_desktops &&
1169                 self->desktop != DESKTOP_ALL)
1170                 self->desktop = screen_num_desktops - 1;
1171             ob_debug("client desktop set from startup-notification: 0x%x",
1172                      self->desktop);
1173         }
1174         /* defaults to the current desktop */
1175         else {
1176             self->desktop = screen_desktop;
1177             ob_debug("client desktop set to the current desktop: %d",
1178                      self->desktop);
1179         }
1180     }
1181 }
1182
1183 static void client_get_state(ObClient *self)
1184 {
1185     guint32 *state;
1186     guint num;
1187
1188     if (OBT_PROP_GETA32(self->window, NET_WM_STATE, ATOM, &state, &num)) {
1189         gulong i;
1190         for (i = 0; i < num; ++i) {
1191             if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_MODAL))
1192                 self->modal = TRUE;
1193             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_SHADED))
1194                 self->shaded = TRUE;
1195             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_HIDDEN))
1196                 self->iconic = TRUE;
1197             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_SKIP_TASKBAR))
1198                 self->skip_taskbar = TRUE;
1199             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_SKIP_PAGER))
1200                 self->skip_pager = TRUE;
1201             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_FULLSCREEN))
1202                 self->fullscreen = TRUE;
1203             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_VERT))
1204                 self->max_vert = TRUE;
1205             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_HORZ))
1206                 self->max_horz = TRUE;
1207             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_ABOVE))
1208                 self->above = TRUE;
1209             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_BELOW))
1210                 self->below = TRUE;
1211             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_DEMANDS_ATTENTION))
1212                 self->demands_attention = TRUE;
1213             else if (state[i] == OBT_PROP_ATOM(OB_WM_STATE_UNDECORATED))
1214                 self->undecorated = TRUE;
1215         }
1216
1217         g_free(state);
1218     }
1219 }
1220
1221 static void client_get_shaped(ObClient *self)
1222 {
1223     self->shaped = FALSE;
1224 #ifdef   SHAPE
1225     if (obt_display_extension_shape) {
1226         gint foo;
1227         guint ufoo;
1228         gint s;
1229
1230         XShapeSelectInput(obt_display, self->window, ShapeNotifyMask);
1231
1232         XShapeQueryExtents(obt_display, self->window, &s, &foo,
1233                            &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
1234                            &ufoo);
1235         self->shaped = (s != 0);
1236     }
1237 #endif
1238 }
1239
1240 void client_update_transient_for(ObClient *self)
1241 {
1242     Window t = None;
1243     ObClient *target = NULL;
1244     gboolean trangroup = FALSE;
1245
1246     if (XGetTransientForHint(obt_display, self->window, &t)) {
1247         if (t != self->window) { /* cant be transient to itself! */
1248             ObWindow *tw = window_find(t);
1249             /* if this happens then we need to check for it*/
1250             g_assert(tw != CLIENT_AS_WINDOW(self));
1251             if (tw && WINDOW_IS_CLIENT(tw)) {
1252                 /* watch out for windows with a parent that is something
1253                    different, like a dockapp for example */
1254                 target = WINDOW_AS_CLIENT(tw);
1255             }
1256         }
1257
1258         /* Setting the transient_for to Root is actually illegal, however
1259            applications from time have done this to specify transient for
1260            their group */
1261         if (!target && self->group && t == obt_root(ob_screen))
1262             trangroup = TRUE;
1263     } else if (self->group && self->transient)
1264         trangroup = TRUE;
1265
1266     client_update_transient_tree(self, self->group, self->group,
1267                                  self->transient_for_group, trangroup,
1268                                  client_direct_parent(self), target);
1269     self->transient_for_group = trangroup;
1270
1271 }
1272
1273 static void client_update_transient_tree(ObClient *self,
1274                                          ObGroup *oldgroup, ObGroup *newgroup,
1275                                          gboolean oldgtran, gboolean newgtran,
1276                                          ObClient* oldparent,
1277                                          ObClient *newparent)
1278 {
1279     GSList *it, *next;
1280     ObClient *c;
1281
1282     g_assert(!oldgtran || oldgroup);
1283     g_assert(!newgtran || newgroup);
1284     g_assert((!oldgtran && !oldparent) ||
1285              (oldgtran && !oldparent) ||
1286              (!oldgtran && oldparent));
1287     g_assert((!newgtran && !newparent) ||
1288              (newgtran && !newparent) ||
1289              (!newgtran && newparent));
1290
1291     /* * *
1292       Group transient windows are not allowed to have other group
1293       transient windows as their children.
1294       * * */
1295
1296
1297     /* No change has occured */
1298     if (oldgroup == newgroup &&
1299         oldgtran == newgtran &&
1300         oldparent == newparent) return;
1301
1302     /** Remove the client from the transient tree **/
1303
1304     for (it = self->transients; it; it = next) {
1305         next = g_slist_next(it);
1306         c = it->data;
1307         self->transients = g_slist_delete_link(self->transients, it);
1308         c->parents = g_slist_remove(c->parents, self);
1309     }
1310     for (it = self->parents; it; it = next) {
1311         next = g_slist_next(it);
1312         c = it->data;
1313         self->parents = g_slist_delete_link(self->parents, it);
1314         c->transients = g_slist_remove(c->transients, self);
1315     }
1316
1317     /** Re-add the client to the transient tree **/
1318
1319     /* If we're transient for a group then we need to add ourselves to all our
1320        parents */
1321     if (newgtran) {
1322         for (it = newgroup->members; it; it = g_slist_next(it)) {
1323             c = it->data;
1324             if (c != self &&
1325                 !client_search_top_direct_parent(c)->transient_for_group &&
1326                 client_normal(c))
1327             {
1328                 c->transients = g_slist_prepend(c->transients, self);
1329                 self->parents = g_slist_prepend(self->parents, c);
1330             }
1331         }
1332     }
1333
1334     /* If we are now transient for a single window we need to add ourselves to
1335        its children
1336
1337        WARNING: Cyclical transient ness is possible if two windows are
1338        transient for eachother.
1339     */
1340     else if (newparent &&
1341              /* don't make ourself its child if it is already our child */
1342              !client_is_direct_child(self, newparent) &&
1343              client_normal(newparent))
1344     {
1345         newparent->transients = g_slist_prepend(newparent->transients, self);
1346         self->parents = g_slist_prepend(self->parents, newparent);
1347     }
1348
1349     /* Add any group transient windows to our children. But if we're transient
1350        for the group, then other group transients are not our children.
1351
1352        WARNING: Cyclical transient-ness is possible. For e.g. if:
1353        A is transient for the group
1354        B is transient for A
1355        C is transient for B
1356        A can't be transient for C or we have a cycle
1357     */
1358     if (!newgtran && newgroup &&
1359         (!newparent ||
1360          !client_search_top_direct_parent(newparent)->transient_for_group) &&
1361         client_normal(self))
1362     {
1363         for (it = newgroup->members; it; it = g_slist_next(it)) {
1364             c = it->data;
1365             if (c != self && c->transient_for_group &&
1366                 /* Don't make it our child if it is already our parent */
1367                 !client_is_direct_child(c, self))
1368             {
1369                 self->transients = g_slist_prepend(self->transients, c);
1370                 c->parents = g_slist_prepend(c->parents, self);
1371             }
1372         }
1373     }
1374
1375     /** If we change our group transient-ness, our children change their
1376         effect group transient-ness, which affects how they relate to other
1377         group windows **/
1378
1379     for (it = self->transients; it; it = g_slist_next(it)) {
1380         c = it->data;
1381         if (!c->transient_for_group)
1382             client_update_transient_tree(c, c->group, c->group,
1383                                          c->transient_for_group,
1384                                          c->transient_for_group,
1385                                          client_direct_parent(c),
1386                                          client_direct_parent(c));
1387     }
1388 }
1389
1390 static void client_get_mwm_hints(ObClient *self)
1391 {
1392     guint num;
1393     guint32 *hints;
1394
1395     self->mwmhints.flags = 0; /* default to none */
1396
1397     if (OBT_PROP_GETA32(self->window, MOTIF_WM_HINTS, MOTIF_WM_HINTS,
1398                         &hints, &num)) {
1399         if (num >= OB_MWM_ELEMENTS) {
1400             self->mwmhints.flags = hints[0];
1401             self->mwmhints.functions = hints[1];
1402             self->mwmhints.decorations = hints[2];
1403         }
1404         g_free(hints);
1405     }
1406 }
1407
1408 void client_get_type_and_transientness(ObClient *self)
1409 {
1410     guint num, i;
1411     guint32 *val;
1412     Window t;
1413
1414     self->type = -1;
1415     self->transient = FALSE;
1416
1417     if (OBT_PROP_GETA32(self->window, NET_WM_WINDOW_TYPE, ATOM, &val, &num)) {
1418         /* use the first value that we know about in the array */
1419         for (i = 0; i < num; ++i) {
1420             if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DESKTOP))
1421                 self->type = OB_CLIENT_TYPE_DESKTOP;
1422             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DOCK))
1423                 self->type = OB_CLIENT_TYPE_DOCK;
1424             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_TOOLBAR))
1425                 self->type = OB_CLIENT_TYPE_TOOLBAR;
1426             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_MENU))
1427                 self->type = OB_CLIENT_TYPE_MENU;
1428             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_UTILITY))
1429                 self->type = OB_CLIENT_TYPE_UTILITY;
1430             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_SPLASH))
1431                 self->type = OB_CLIENT_TYPE_SPLASH;
1432             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DIALOG))
1433                 self->type = OB_CLIENT_TYPE_DIALOG;
1434             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_NORMAL))
1435                 self->type = OB_CLIENT_TYPE_NORMAL;
1436             else if (val[i] == OBT_PROP_ATOM(KDE_NET_WM_WINDOW_TYPE_OVERRIDE))
1437             {
1438                 /* prevent this window from getting any decor or
1439                    functionality */
1440                 self->mwmhints.flags &= (OB_MWM_FLAG_FUNCTIONS |
1441                                          OB_MWM_FLAG_DECORATIONS);
1442                 self->mwmhints.decorations = 0;
1443                 self->mwmhints.functions = 0;
1444             }
1445             if (self->type != (ObClientType) -1)
1446                 break; /* grab the first legit type */
1447         }
1448         g_free(val);
1449     }
1450
1451     if (XGetTransientForHint(obt_display, self->window, &t))
1452         self->transient = TRUE;
1453
1454     if (self->type == (ObClientType) -1) {
1455         /*the window type hint was not set, which means we either classify
1456           ourself as a normal window or a dialog, depending on if we are a
1457           transient. */
1458         if (self->transient)
1459             self->type = OB_CLIENT_TYPE_DIALOG;
1460         else
1461             self->type = OB_CLIENT_TYPE_NORMAL;
1462     }
1463
1464     /* then, based on our type, we can update our transientness.. */
1465     if (self->type == OB_CLIENT_TYPE_DIALOG ||
1466         self->type == OB_CLIENT_TYPE_TOOLBAR ||
1467         self->type == OB_CLIENT_TYPE_MENU ||
1468         self->type == OB_CLIENT_TYPE_UTILITY)
1469     {
1470         self->transient = TRUE;
1471     }
1472 }
1473
1474 void client_update_protocols(ObClient *self)
1475 {
1476     guint32 *proto;
1477     guint num_ret, i;
1478
1479     self->focus_notify = FALSE;
1480     self->delete_window = FALSE;
1481
1482     if (OBT_PROP_GETA32(self->window, WM_PROTOCOLS, ATOM, &proto, &num_ret)) {
1483         for (i = 0; i < num_ret; ++i) {
1484             if (proto[i] == OBT_PROP_ATOM(WM_DELETE_WINDOW))
1485                 /* this means we can request the window to close */
1486                 self->delete_window = TRUE;
1487             else if (proto[i] == OBT_PROP_ATOM(WM_TAKE_FOCUS))
1488                 /* if this protocol is requested, then the window will be
1489                    notified whenever we want it to receive focus */
1490                 self->focus_notify = TRUE;
1491             else if (proto[i] == OBT_PROP_ATOM(NET_WM_PING))
1492                 /* if this protocol is requested, then the window will allow
1493                    pings to determine if it is still alive */
1494                 self->ping = TRUE;
1495 #ifdef SYNC
1496             else if (proto[i] == OBT_PROP_ATOM(NET_WM_SYNC_REQUEST))
1497                 /* if this protocol is requested, then resizing the
1498                    window will be synchronized between the frame and the
1499                    client */
1500                 self->sync_request = TRUE;
1501 #endif
1502         }
1503         g_free(proto);
1504     }
1505 }
1506
1507 #ifdef SYNC
1508 void client_update_sync_request_counter(ObClient *self)
1509 {
1510     guint32 i;
1511
1512     if (OBT_PROP_GET32(self->window, NET_WM_SYNC_REQUEST_COUNTER, CARDINAL,&i))
1513     {
1514         self->sync_counter = i;
1515     } else
1516         self->sync_counter = None;
1517 }
1518 #endif
1519
1520 static void client_get_colormap(ObClient *self)
1521 {
1522     XWindowAttributes wa;
1523
1524     if (XGetWindowAttributes(obt_display, self->window, &wa))
1525         client_update_colormap(self, wa.colormap);
1526 }
1527
1528 void client_update_colormap(ObClient *self, Colormap colormap)
1529 {
1530     if (colormap == self->colormap) return;
1531
1532     ob_debug("Setting client %s colormap: 0x%x", self->title, colormap);
1533
1534     if (client_focused(self)) {
1535         screen_install_colormap(self, FALSE); /* uninstall old one */
1536         self->colormap = colormap;
1537         screen_install_colormap(self, FALSE); /* install new one */
1538     } else
1539         self->colormap = colormap;
1540 }
1541
1542 void client_update_normal_hints(ObClient *self)
1543 {
1544     XSizeHints size;
1545     glong ret;
1546
1547     /* defaults */
1548     self->min_ratio = 0.0f;
1549     self->max_ratio = 0.0f;
1550     SIZE_SET(self->size_inc, 1, 1);
1551     SIZE_SET(self->base_size, 0, 0);
1552     SIZE_SET(self->min_size, 0, 0);
1553     SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
1554
1555     /* get the hints from the window */
1556     if (XGetWMNormalHints(obt_display, self->window, &size, &ret)) {
1557         /* normal windows can't request placement! har har
1558         if (!client_normal(self))
1559         */
1560         self->positioned = (size.flags & (PPosition|USPosition));
1561         self->sized = (size.flags & (PSize|USSize));
1562
1563         if (size.flags & PWinGravity)
1564             self->gravity = size.win_gravity;
1565
1566         if (size.flags & PAspect) {
1567             if (size.min_aspect.y)
1568                 self->min_ratio =
1569                     (gfloat) size.min_aspect.x / size.min_aspect.y;
1570             if (size.max_aspect.y)
1571                 self->max_ratio =
1572                     (gfloat) size.max_aspect.x / size.max_aspect.y;
1573         }
1574
1575         if (size.flags & PMinSize)
1576             SIZE_SET(self->min_size, size.min_width, size.min_height);
1577
1578         if (size.flags & PMaxSize)
1579             SIZE_SET(self->max_size, size.max_width, size.max_height);
1580
1581         if (size.flags & PBaseSize)
1582             SIZE_SET(self->base_size, size.base_width, size.base_height);
1583
1584         if (size.flags & PResizeInc && size.width_inc && size.height_inc)
1585             SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
1586
1587         ob_debug("Normal hints: min size (%d %d) max size (%d %d)",
1588                  self->min_size.width, self->min_size.height,
1589                  self->max_size.width, self->max_size.height);
1590         ob_debug("size inc (%d %d) base size (%d %d)",
1591                  self->size_inc.width, self->size_inc.height,
1592                  self->base_size.width, self->base_size.height);
1593     }
1594     else
1595         ob_debug("Normal hints: not set");
1596 }
1597
1598 void client_setup_decor_and_functions(ObClient *self, gboolean reconfig)
1599 {
1600     /* start with everything (cept fullscreen) */
1601     self->decorations =
1602         (OB_FRAME_DECOR_TITLEBAR |
1603          OB_FRAME_DECOR_HANDLE |
1604          OB_FRAME_DECOR_GRIPS |
1605          OB_FRAME_DECOR_BORDER |
1606          OB_FRAME_DECOR_ICON |
1607          OB_FRAME_DECOR_ALLDESKTOPS |
1608          OB_FRAME_DECOR_ICONIFY |
1609          OB_FRAME_DECOR_MAXIMIZE |
1610          OB_FRAME_DECOR_SHADE |
1611          OB_FRAME_DECOR_CLOSE);
1612     self->functions =
1613         (OB_CLIENT_FUNC_RESIZE |
1614          OB_CLIENT_FUNC_MOVE |
1615          OB_CLIENT_FUNC_ICONIFY |
1616          OB_CLIENT_FUNC_MAXIMIZE |
1617          OB_CLIENT_FUNC_SHADE |
1618          OB_CLIENT_FUNC_CLOSE |
1619          OB_CLIENT_FUNC_BELOW |
1620          OB_CLIENT_FUNC_ABOVE |
1621          OB_CLIENT_FUNC_UNDECORATE);
1622
1623     if (!(self->min_size.width < self->max_size.width ||
1624           self->min_size.height < self->max_size.height))
1625         self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1626
1627     switch (self->type) {
1628     case OB_CLIENT_TYPE_NORMAL:
1629         /* normal windows retain all of the possible decorations and
1630            functionality, and can be fullscreen */
1631         self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1632         break;
1633
1634     case OB_CLIENT_TYPE_DIALOG:
1635         /* sometimes apps make dialog windows fullscreen for some reason (for
1636            e.g. kpdf does this..) */
1637         self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1638         break;
1639
1640     case OB_CLIENT_TYPE_UTILITY:
1641         /* these windows don't have anything added or removed by default */
1642         break;
1643
1644     case OB_CLIENT_TYPE_MENU:
1645     case OB_CLIENT_TYPE_TOOLBAR:
1646         /* these windows can't iconify or maximize */
1647         self->decorations &= ~(OB_FRAME_DECOR_ICONIFY |
1648                                OB_FRAME_DECOR_MAXIMIZE);
1649         self->functions &= ~(OB_CLIENT_FUNC_ICONIFY |
1650                              OB_CLIENT_FUNC_MAXIMIZE);
1651         break;
1652
1653     case OB_CLIENT_TYPE_SPLASH:
1654         /* these don't get get any decorations, and the only thing you can
1655            do with them is move them */
1656         self->decorations = 0;
1657         self->functions = OB_CLIENT_FUNC_MOVE;
1658         break;
1659
1660     case OB_CLIENT_TYPE_DESKTOP:
1661         /* these windows are not manipulated by the window manager */
1662         self->decorations = 0;
1663         self->functions = 0;
1664         break;
1665
1666     case OB_CLIENT_TYPE_DOCK:
1667         /* these windows are not manipulated by the window manager, but they
1668            can set below layer which has a special meaning */
1669         self->decorations = 0;
1670         self->functions = OB_CLIENT_FUNC_BELOW;
1671         break;
1672     }
1673
1674     /* Mwm Hints are applied subtractively to what has already been chosen for
1675        decor and functionality */
1676     if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
1677         if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
1678             if (! ((self->mwmhints.decorations & OB_MWM_DECOR_HANDLE) ||
1679                    (self->mwmhints.decorations & OB_MWM_DECOR_TITLE)))
1680             {
1681                 /* if the mwm hints request no handle or title, then all
1682                    decorations are disabled, but keep the border if that's
1683                    specified */
1684                 if (self->mwmhints.decorations & OB_MWM_DECOR_BORDER)
1685                     self->decorations = OB_FRAME_DECOR_BORDER;
1686                 else
1687                     self->decorations = 0;
1688             }
1689         }
1690     }
1691
1692     if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
1693         if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
1694             if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
1695                 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1696             if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
1697                 self->functions &= ~OB_CLIENT_FUNC_MOVE;
1698             /* dont let mwm hints kill any buttons
1699                if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
1700                self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
1701                if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
1702                self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1703             */
1704             /* dont let mwm hints kill the close button
1705                if (! (self->mwmhints.functions & MwmFunc_Close))
1706                self->functions &= ~OB_CLIENT_FUNC_CLOSE; */
1707         }
1708     }
1709
1710     if (!(self->functions & OB_CLIENT_FUNC_SHADE))
1711         self->decorations &= ~OB_FRAME_DECOR_SHADE;
1712     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY))
1713         self->decorations &= ~OB_FRAME_DECOR_ICONIFY;
1714     if (!(self->functions & OB_CLIENT_FUNC_RESIZE))
1715         self->decorations &= ~(OB_FRAME_DECOR_GRIPS | OB_FRAME_DECOR_HANDLE);
1716
1717     /* can't maximize without moving/resizing */
1718     if (!((self->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
1719           (self->functions & OB_CLIENT_FUNC_MOVE) &&
1720           (self->functions & OB_CLIENT_FUNC_RESIZE))) {
1721         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1722         self->decorations &= ~OB_FRAME_DECOR_MAXIMIZE;
1723     }
1724
1725     if (self->max_horz && self->max_vert) {
1726         /* you can't resize fully maximized windows */
1727         self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1728         /* kill the handle on fully maxed windows */
1729         self->decorations &= ~(OB_FRAME_DECOR_HANDLE | OB_FRAME_DECOR_GRIPS);
1730     }
1731
1732     /* If there are no decorations to remove, don't allow the user to try
1733        toggle the state */
1734     if (self->decorations == 0)
1735         self->functions &= ~OB_CLIENT_FUNC_UNDECORATE;
1736
1737     /* finally, the user can have requested no decorations, which overrides
1738        everything (but doesnt give it a border if it doesnt have one) */
1739     if (self->undecorated)
1740         self->decorations = 0;
1741
1742     /* if we don't have a titlebar, then we cannot shade! */
1743     if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1744         self->functions &= ~OB_CLIENT_FUNC_SHADE;
1745
1746     /* now we need to check against rules for the client's current state */
1747     if (self->fullscreen) {
1748         self->functions &= (OB_CLIENT_FUNC_CLOSE |
1749                             OB_CLIENT_FUNC_FULLSCREEN |
1750                             OB_CLIENT_FUNC_ICONIFY);
1751         self->decorations = 0;
1752     }
1753
1754     client_change_allowed_actions(self);
1755
1756     if (reconfig)
1757         /* force reconfigure to make sure decorations are updated */
1758         client_reconfigure(self, TRUE);
1759 }
1760
1761 static void client_change_allowed_actions(ObClient *self)
1762 {
1763     gulong actions[12];
1764     gint num = 0;
1765
1766     /* desktop windows are kept on all desktops */
1767     if (self->type != OB_CLIENT_TYPE_DESKTOP)
1768         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_CHANGE_DESKTOP);
1769
1770     if (self->functions & OB_CLIENT_FUNC_SHADE)
1771         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_SHADE);
1772     if (self->functions & OB_CLIENT_FUNC_CLOSE)
1773         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_CLOSE);
1774     if (self->functions & OB_CLIENT_FUNC_MOVE)
1775         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_MOVE);
1776     if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1777         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_MINIMIZE);
1778     if (self->functions & OB_CLIENT_FUNC_RESIZE)
1779         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_RESIZE);
1780     if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1781         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_FULLSCREEN);
1782     if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1783         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_MAXIMIZE_HORZ);
1784         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_MAXIMIZE_VERT);
1785     }
1786     if (self->functions & OB_CLIENT_FUNC_ABOVE)
1787         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_ABOVE);
1788     if (self->functions & OB_CLIENT_FUNC_BELOW)
1789         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_BELOW);
1790     if (self->functions & OB_CLIENT_FUNC_UNDECORATE)
1791         actions[num++] = OBT_PROP_ATOM(OB_WM_ACTION_UNDECORATE);
1792
1793     OBT_PROP_SETA32(self->window, NET_WM_ALLOWED_ACTIONS, ATOM, actions, num);
1794
1795     /* make sure the window isn't breaking any rules now
1796
1797        don't check ICONIFY here.  just cuz a window can't iconify doesnt mean
1798        it can't be iconified with its parent
1799     */
1800
1801     if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1802         if (self->frame) client_shade(self, FALSE);
1803         else self->shaded = FALSE;
1804     }
1805     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1806         if (self->frame) client_fullscreen(self, FALSE);
1807         else self->fullscreen = FALSE;
1808     }
1809     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
1810                                                          self->max_vert)) {
1811         if (self->frame) client_maximize(self, FALSE, 0);
1812         else self->max_vert = self->max_horz = FALSE;
1813         render_plugin->frame_set_is_max_horz (self->frame, self->max_horz);
1814         render_plugin->frame_set_is_max_vert (self->frame, self->max_vert);
1815     }
1816 }
1817
1818 void client_update_wmhints(ObClient *self)
1819 {
1820     XWMHints *hints;
1821
1822     /* assume a window takes input if it doesnt specify */
1823     self->can_focus = TRUE;
1824
1825     if ((hints = XGetWMHints(obt_display, self->window)) != NULL) {
1826         gboolean ur;
1827
1828         if (hints->flags & InputHint)
1829             self->can_focus = hints->input;
1830
1831         /* only do this when first managing the window *AND* when we aren't
1832            starting up! */
1833         if (ob_state() != OB_STATE_STARTING && self->frame == NULL)
1834             if (hints->flags & StateHint)
1835                 self->iconic = hints->initial_state == IconicState;
1836
1837         ur = self->urgent;
1838         self->urgent = (hints->flags & XUrgencyHint);
1839         if (self->urgent && !ur)
1840             client_hilite(self, TRUE);
1841         else if (!self->urgent && ur && self->demands_attention)
1842             client_hilite(self, FALSE);
1843
1844         if (!(hints->flags & WindowGroupHint))
1845             hints->window_group = None;
1846
1847         /* did the group state change? */
1848         if (hints->window_group !=
1849             (self->group ? self->group->leader : None))
1850         {
1851             ObGroup *oldgroup = self->group;
1852
1853             /* remove from the old group if there was one */
1854             if (self->group != NULL) {
1855                 group_remove(self->group, self);
1856                 self->group = NULL;
1857             }
1858
1859             /* add ourself to the group if we have one */
1860             if (hints->window_group != None) {
1861                 self->group = group_add(hints->window_group, self);
1862             }
1863
1864             /* Put ourselves into the new group's transient tree, and remove
1865                ourselves from the old group's */
1866             client_update_transient_tree(self, oldgroup, self->group,
1867                                          self->transient_for_group,
1868                                          self->transient_for_group,
1869                                          client_direct_parent(self),
1870                                          client_direct_parent(self));
1871
1872             /* Lastly, being in a group, or not, can change if the window is
1873                transient for anything.
1874
1875                The logic for this is:
1876                self->transient = TRUE always if the window wants to be
1877                transient for something, even if transient_for was NULL because
1878                it wasn't in a group before.
1879
1880                If parents was NULL and oldgroup was NULL we can assume
1881                that when we add the new group, it will become transient for
1882                something.
1883
1884                If transient_for_group is TRUE, then it must have already
1885                had a group. If it is getting a new group, the above call to
1886                client_update_transient_tree has already taken care of
1887                everything ! If it is losing all group status then it will
1888                no longer be transient for anything and that needs to be
1889                updated.
1890             */
1891             if (self->transient &&
1892                 ((self->parents == NULL && oldgroup == NULL) ||
1893                  (self->transient_for_group && !self->group)))
1894                 client_update_transient_for(self);
1895         }
1896
1897         /* the WM_HINTS can contain an icon */
1898         if (hints->flags & IconPixmapHint)
1899             client_update_icons(self);
1900
1901         XFree(hints);
1902     }
1903 }
1904
1905 void client_update_title(ObClient *self)
1906 {
1907     gchar *data = NULL;
1908     gchar *visible = NULL;
1909
1910     g_free(self->title);
1911     g_free(self->original_title);
1912
1913     /* try netwm */
1914     if (!OBT_PROP_GETS(self->window, NET_WM_NAME, utf8, &data)) {
1915         /* try old x stuff */
1916         if (!(OBT_PROP_GETS(self->window, WM_NAME, locale, &data)
1917               || OBT_PROP_GETS(self->window, WM_NAME, utf8, &data))) {
1918             if (self->transient) {
1919     /*
1920     GNOME alert windows are not given titles:
1921     http://developer.gnome.org/projects/gup/hig/draft_hig_new/windows-alert.html
1922     */
1923                 data = g_strdup("");
1924             } else
1925                 data = g_strdup("Unnamed Window");
1926         }
1927     }
1928     self->original_title = g_strdup(data);
1929
1930     if (self->client_machine) {
1931         visible = g_strdup_printf("%s (%s)", data, self->client_machine);
1932         g_free(data);
1933     } else
1934         visible = data;
1935
1936     if (self->not_responding) {
1937         data = visible;
1938         if (self->kill_level > 0)
1939             visible = g_strdup_printf("%s - [%s]", data, _("Killing..."));
1940         else
1941             visible = g_strdup_printf("%s - [%s]", data, _("Not Responding"));
1942         g_free(data);
1943     }
1944
1945     OBT_PROP_SETS(self->window, NET_WM_VISIBLE_NAME, utf8, visible);
1946     self->title = visible;
1947
1948     if (self->frame)
1949     render_plugin->frame_update_layout (self->frame, FALSE, FALSE);
1950
1951     /* update the icon title */
1952     data = NULL;
1953     g_free(self->icon_title);
1954
1955     /* try netwm */
1956     if (!OBT_PROP_GETS(self->window, NET_WM_ICON_NAME, utf8, &data))
1957         /* try old x stuff */
1958         if (!(OBT_PROP_GETS(self->window, WM_ICON_NAME, locale, &data) ||
1959               OBT_PROP_GETS(self->window, WM_ICON_NAME, utf8, &data)))
1960             data = g_strdup(self->title);
1961
1962     if (self->client_machine) {
1963         visible = g_strdup_printf("%s (%s)", data, self->client_machine);
1964         g_free(data);
1965     } else
1966         visible = data;
1967
1968     if (self->not_responding) {
1969         data = visible;
1970         if (self->kill_level > 0)
1971             visible = g_strdup_printf("%s - [%s]", data, _("Killing..."));
1972         else
1973             visible = g_strdup_printf("%s - [%s]", data, _("Not Responding"));
1974         g_free(data);
1975     }
1976
1977     OBT_PROP_SETS(self->window, NET_WM_VISIBLE_ICON_NAME, utf8, visible);
1978     self->icon_title = visible;
1979 }
1980
1981 void client_update_strut(ObClient *self)
1982 {
1983     guint num;
1984     guint32 *data;
1985     gboolean got = FALSE;
1986     StrutPartial strut;
1987
1988     if (OBT_PROP_GETA32(self->window, NET_WM_STRUT_PARTIAL, CARDINAL,
1989                         &data, &num))
1990     {
1991         if (num == 12) {
1992             got = TRUE;
1993             STRUT_PARTIAL_SET(strut,
1994                               data[0], data[2], data[1], data[3],
1995                               data[4], data[5], data[8], data[9],
1996                               data[6], data[7], data[10], data[11]);
1997         }
1998         g_free(data);
1999     }
2000
2001     if (!got &&
2002         OBT_PROP_GETA32(self->window, NET_WM_STRUT, CARDINAL, &data, &num)) {
2003         if (num == 4) {
2004             Rect *a;
2005
2006             got = TRUE;
2007
2008             /* use the screen's width/height */
2009             a = screen_physical_area_all_monitors();
2010
2011             STRUT_PARTIAL_SET(strut,
2012                               data[0], data[2], data[1], data[3],
2013                               a->y, a->y + a->height - 1,
2014                               a->x, a->x + a->width - 1,
2015                               a->y, a->y + a->height - 1,
2016                               a->x, a->x + a->width - 1);
2017             g_free(a);
2018         }
2019         g_free(data);
2020     }
2021
2022     if (!got)
2023         STRUT_PARTIAL_SET(strut, 0, 0, 0, 0,
2024                           0, 0, 0, 0, 0, 0, 0, 0);
2025
2026     if (!STRUT_EQUAL(strut, self->strut)) {
2027         self->strut = strut;
2028
2029         /* updating here is pointless while we're being mapped cuz we're not in
2030            the client list yet */
2031         if (self->frame)
2032             screen_update_areas();
2033     }
2034 }
2035
2036 /* Avoid storing icons above this size if possible */
2037 #define AVOID_ABOVE 64
2038
2039 void client_update_icons(ObClient *self)
2040 {
2041     guint num;
2042     guint32 *data;
2043     guint w, h, i, j;
2044     guint num_seen;  /* number of icons present */
2045     guint num_small_seen;  /* number of icons small enough present */
2046     guint smallest, smallest_area;
2047
2048     for (i = 0; i < self->nicons; ++i)
2049         g_free(self->icons[i].data);
2050     if (self->nicons > 0)
2051         g_free(self->icons);
2052     self->nicons = 0;
2053
2054     if (OBT_PROP_GETA32(self->window, NET_WM_ICON, CARDINAL, &data, &num)) {
2055         /* figure out how many valid icons are in here */
2056         i = 0;
2057         num_seen = num_small_seen = 0;
2058         smallest = smallest_area = 0;
2059         if (num > 2)
2060             while (i < num) {
2061                 w = data[i++];
2062                 h = data[i++];
2063                 i += w * h;
2064                 /* watch for it being too small for the specified size, or for
2065                    zero sized icons. */
2066                 if (i > num || w == 0 || h == 0) break;
2067
2068                 if (!smallest_area || w*h < smallest_area) {
2069                     smallest = num_seen;
2070                     smallest_area = w*h;
2071                 }
2072                 ++num_seen;
2073                 if (w <= AVOID_ABOVE && h <= AVOID_ABOVE)
2074                     ++num_small_seen;
2075             }
2076         if (num_small_seen > 0)
2077             self->nicons = num_small_seen;
2078         else if (num_seen)
2079             self->nicons = 1;
2080
2081         self->icons = g_new(ObClientIcon, self->nicons);
2082
2083         /* store the icons */
2084         i = 0;
2085         for (j = 0; j < self->nicons;) {
2086             guint x, y, t;
2087
2088             w = self->icons[j].width = data[i++];
2089             h = self->icons[j].height = data[i++];
2090
2091             /* if there are some icons smaller than the threshold, we're
2092                skipping all the ones above */
2093             if (num_small_seen > 0) {
2094                 if (w > AVOID_ABOVE || h > AVOID_ABOVE) {
2095                     i += w*h;
2096                     continue;
2097                 }
2098             }
2099             /* if there were no icons smaller than the threshold, then we are
2100                only taking the smallest available one we saw */
2101             else if (j != smallest) {
2102                 i += w*h;
2103                 continue;
2104             }
2105
2106             self->icons[j].data = g_new(RrPixel32, w * h);
2107             for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
2108                 if (x >= w) {
2109                     x = 0;
2110                     ++y;
2111                 }
2112                 self->icons[j].data[t] =
2113                     (((data[i] >> 24) & 0xff) << RrDefaultAlphaOffset) +
2114                     (((data[i] >> 16) & 0xff) << RrDefaultRedOffset) +
2115                     (((data[i] >> 8) & 0xff) << RrDefaultGreenOffset) +
2116                     (((data[i] >> 0) & 0xff) << RrDefaultBlueOffset);
2117             }
2118             g_assert(i <= num);
2119
2120             ++j;
2121         }
2122
2123         g_free(data);
2124     } else {
2125         XWMHints *hints;
2126
2127         if ((hints = XGetWMHints(obt_display, self->window))) {
2128             if (hints->flags & IconPixmapHint) {
2129                 self->nicons = 1;
2130                 self->icons = g_new(ObClientIcon, self->nicons);
2131                 obt_display_ignore_errors(TRUE);
2132                 if (!RrPixmapToRGBA(ob_rr_inst,
2133                                     hints->icon_pixmap,
2134                                     (hints->flags & IconMaskHint ?
2135                                      hints->icon_mask : None),
2136                                     &self->icons[0].width,
2137                                     &self->icons[0].height,
2138                                     &self->icons[0].data))
2139                 {
2140                     g_free(self->icons);
2141                     self->nicons = 0;
2142                 }
2143                 obt_display_ignore_errors(FALSE);
2144             }
2145             XFree(hints);
2146         }
2147     }
2148
2149     /* set the default icon onto the window
2150        in theory, this could be a race, but if a window doesn't set an icon
2151        or removes it entirely, it's not very likely it is going to set one
2152        right away afterwards
2153
2154        if it has parents, then one of them will have an icon already
2155     */
2156     if (self->nicons == 0 && !self->parents) {
2157         RrPixel32 *icon = ob_rr_theme->def_win_icon;
2158         gulong *data;
2159
2160         data = g_new(gulong, 48*48+2);
2161         data[0] = data[1] =  48;
2162         for (i = 0; i < 48*48; ++i)
2163             data[i+2] = (((icon[i] >> RrDefaultAlphaOffset) & 0xff) << 24) +
2164                 (((icon[i] >> RrDefaultRedOffset) & 0xff) << 16) +
2165                 (((icon[i] >> RrDefaultGreenOffset) & 0xff) << 8) +
2166                 (((icon[i] >> RrDefaultBlueOffset) & 0xff) << 0);
2167         OBT_PROP_SETA32(self->window, NET_WM_ICON, CARDINAL, data, 48*48+2);
2168         g_free(data);
2169     } else if (self->frame)
2170         render_plugin->frame_update_skin (self->frame);
2171         /* don't draw the icon empty if we're just setting one now anyways,
2172            we'll get the property change any second */
2173         //frame_adjust_icon(self->frame);
2174 }
2175
2176 void client_update_icon_geometry(ObClient *self)
2177 {
2178     guint num;
2179     guint32 *data;
2180
2181     RECT_SET(self->icon_geometry, 0, 0, 0, 0);
2182
2183     if (OBT_PROP_GETA32(self->window, NET_WM_ICON_GEOMETRY, CARDINAL,
2184                         &data, &num))
2185     {
2186         if (num == 4)
2187             /* don't let them set it with an area < 0 */
2188             RECT_SET(self->icon_geometry, data[0], data[1],
2189                      MAX(data[2],0), MAX(data[3],0));
2190         g_free(data);
2191     }
2192 }
2193
2194 static void client_get_session_ids(ObClient *self)
2195 {
2196     guint32 leader;
2197     gboolean got;
2198     gchar *s;
2199     gchar **ss;
2200
2201     if (!OBT_PROP_GET32(self->window, WM_CLIENT_LEADER, WINDOW, &leader))
2202         leader = None;
2203
2204     /* get the SM_CLIENT_ID */
2205     got = FALSE;
2206     if (leader)
2207         got = OBT_PROP_GETS(leader, SM_CLIENT_ID, locale, &self->sm_client_id);
2208     if (!got)
2209         OBT_PROP_GETS(self->window, SM_CLIENT_ID, locale, &self->sm_client_id);
2210
2211     /* get the WM_CLASS (name and class). make them "" if they are not
2212        provided */
2213     got = FALSE;
2214     if (leader)
2215         got = OBT_PROP_GETSS(leader, WM_CLASS, locale, &ss);
2216     if (!got)
2217         got = OBT_PROP_GETSS(self->window, WM_CLASS, locale, &ss);
2218
2219     if (got) {
2220         if (ss[0]) {
2221             self->name = g_strdup(ss[0]);
2222             if (ss[1])
2223                 self->class = g_strdup(ss[1]);
2224         }
2225         g_strfreev(ss);
2226     }
2227
2228     if (self->name == NULL) self->name = g_strdup("");
2229     if (self->class == NULL) self->class = g_strdup("");
2230
2231     /* get the WM_WINDOW_ROLE. make it "" if it is not provided */
2232     got = FALSE;
2233     if (leader)
2234         got = OBT_PROP_GETS(leader, WM_WINDOW_ROLE, locale, &s);
2235     if (!got)
2236         got = OBT_PROP_GETS(self->window, WM_WINDOW_ROLE, locale, &s);
2237
2238     if (got)
2239         self->role = s;
2240     else
2241         self->role = g_strdup("");
2242
2243     /* get the WM_COMMAND */
2244     got = FALSE;
2245
2246     if (leader)
2247         got = OBT_PROP_GETSS(leader, WM_COMMAND, locale, &ss);
2248     if (!got)
2249         got = OBT_PROP_GETSS(self->window, WM_COMMAND, locale, &ss);
2250
2251     if (got) {
2252         /* merge/mash them all together */
2253         gchar *merge = NULL;
2254         gint i;
2255
2256         for (i = 0; ss[i]; ++i) {
2257             gchar *tmp = merge;
2258             if (merge)
2259                 merge = g_strconcat(merge, ss[i], NULL);
2260             else
2261                 merge = g_strconcat(ss[i], NULL);
2262             g_free(tmp);
2263         }
2264         g_strfreev(ss);
2265
2266         self->wm_command = merge;
2267     }
2268
2269     /* get the WM_CLIENT_MACHINE */
2270     got = FALSE;
2271     if (leader)
2272         got = OBT_PROP_GETS(leader, WM_CLIENT_MACHINE, locale, &s);
2273     if (!got)
2274         got = OBT_PROP_GETS(self->window, WM_CLIENT_MACHINE, locale, &s);
2275
2276     if (got) {
2277         gchar localhost[128];
2278         guint32 pid;
2279
2280         gethostname(localhost, 127);
2281         localhost[127] = '\0';
2282         if (strcmp(localhost, s) != 0)
2283             self->client_machine = s;
2284         else
2285             g_free(s);
2286
2287         /* see if it has the PID set too (the PID requires that the
2288            WM_CLIENT_MACHINE be set) */
2289         if (OBT_PROP_GET32(self->window, NET_WM_PID, CARDINAL, &pid))
2290             self->pid = pid;
2291     }
2292 }
2293
2294 static void client_change_wm_state(ObClient *self)
2295 {
2296     gulong state[2];
2297     glong old;
2298
2299     old = self->wmstate;
2300
2301     if (self->shaded || self->iconic ||
2302         (self->desktop != DESKTOP_ALL && self->desktop != screen_desktop))
2303     {
2304         self->wmstate = IconicState;
2305     } else
2306         self->wmstate = NormalState;
2307
2308     if (old != self->wmstate) {
2309         OBT_PROP_MSG(ob_screen, self->window, KDE_WM_CHANGE_STATE,
2310                      self->wmstate, 1, 0, 0, 0);
2311
2312         state[0] = self->wmstate;
2313         state[1] = None;
2314         OBT_PROP_SETA32(self->window, WM_STATE, WM_STATE, state, 2);
2315     }
2316 }
2317
2318 static void client_change_state(ObClient *self)
2319 {
2320     gulong netstate[12];
2321     guint num;
2322
2323     num = 0;
2324     if (self->modal)
2325         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_MODAL);
2326     if (self->shaded)
2327         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_SHADED);
2328     if (self->iconic)
2329         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_HIDDEN);
2330     if (self->skip_taskbar)
2331         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_SKIP_TASKBAR);
2332     if (self->skip_pager)
2333         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_SKIP_PAGER);
2334     if (self->fullscreen)
2335         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_FULLSCREEN);
2336     if (self->max_vert)
2337         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_VERT);
2338     if (self->max_horz)
2339         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_HORZ);
2340     if (self->above)
2341         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_ABOVE);
2342     if (self->below)
2343         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_BELOW);
2344     if (self->demands_attention)
2345         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_DEMANDS_ATTENTION);
2346     if (self->undecorated)
2347         netstate[num++] = OBT_PROP_ATOM(OB_WM_STATE_UNDECORATED);
2348     OBT_PROP_SETA32(self->window, NET_WM_STATE, ATOM, netstate, num);
2349
2350     if (self->frame)
2351     render_plugin->frame_update_layout (self->frame, FALSE, FALSE);
2352 }
2353
2354 ObClient *client_search_focus_tree(ObClient *self)
2355 {
2356     GSList *it;
2357     ObClient *ret;
2358
2359     for (it = self->transients; it; it = g_slist_next(it)) {
2360         if (client_focused(it->data)) return it->data;
2361         if ((ret = client_search_focus_tree(it->data))) return ret;
2362     }
2363     return NULL;
2364 }
2365
2366 ObClient *client_search_focus_tree_full(ObClient *self)
2367 {
2368     if (self->parents) {
2369         GSList *it;
2370
2371         for (it = self->parents; it; it = g_slist_next(it)) {
2372             ObClient *c = it->data;
2373             if ((c = client_search_focus_tree_full(it->data))) return c;
2374         }
2375
2376         return NULL;
2377     }
2378     else {
2379         /* this function checks the whole tree, the client_search_focus_tree
2380            does not, so we need to check this window */
2381         if (client_focused(self))
2382             return self;
2383         return client_search_focus_tree(self);
2384     }
2385 }
2386
2387 ObClient *client_search_focus_group_full(ObClient *self)
2388 {
2389     GSList *it;
2390
2391     if (self->group) {
2392         for (it = self->group->members; it; it = g_slist_next(it)) {
2393             ObClient *c = it->data;
2394
2395             if (client_focused(c)) return c;
2396             if ((c = client_search_focus_tree(it->data))) return c;
2397         }
2398     } else
2399         if (client_focused(self)) return self;
2400     return NULL;
2401 }
2402
2403 gboolean client_has_parent(ObClient *self)
2404 {
2405     return self->parents != NULL;
2406 }
2407
2408 static ObStackingLayer calc_layer(ObClient *self)
2409 {
2410     ObStackingLayer l;
2411     Rect *monitor;
2412
2413     monitor = screen_physical_area_monitor(client_monitor(self));
2414
2415     if (self->type == OB_CLIENT_TYPE_DESKTOP)
2416         l = OB_STACKING_LAYER_DESKTOP;
2417     else if (self->type == OB_CLIENT_TYPE_DOCK) {
2418         if (self->below) l = OB_STACKING_LAYER_NORMAL;
2419         else l = OB_STACKING_LAYER_ABOVE;
2420     }
2421     else if ((self->fullscreen ||
2422               /* No decorations and fills the monitor = oldskool fullscreen.
2423                  But not for maximized windows.
2424               */
2425               (self->decorations == 0 &&
2426                !(self->max_horz && self->max_vert) &&
2427                RECT_EQUAL(self->area, *monitor))) &&
2428              /* you are fullscreen while you or your children are focused.. */
2429              (client_focused(self) || client_search_focus_tree(self) ||
2430               /* you can be fullscreen if you're on another desktop */
2431               (self->desktop != screen_desktop &&
2432                self->desktop != DESKTOP_ALL) ||
2433               /* and you can also be fullscreen if the focused client is on
2434                  another monitor, or nothing else is focused */
2435               (!focus_client ||
2436                client_monitor(focus_client) != client_monitor(self))))
2437         l = OB_STACKING_LAYER_FULLSCREEN;
2438     else if (self->above) l = OB_STACKING_LAYER_ABOVE;
2439     else if (self->below) l = OB_STACKING_LAYER_BELOW;
2440     else l = OB_STACKING_LAYER_NORMAL;
2441
2442     g_free(monitor);
2443
2444     return l;
2445 }
2446
2447 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
2448                                         ObStackingLayer min)
2449 {
2450     ObStackingLayer old, own;
2451     GSList *it;
2452
2453     old = self->layer;
2454     own = calc_layer(self);
2455     self->layer = MAX(own, min);
2456
2457     if (self->layer != old) {
2458         stacking_remove(CLIENT_AS_WINDOW(self));
2459         stacking_add_nonintrusive(CLIENT_AS_WINDOW(self));
2460     }
2461
2462     /* we've been restacked */
2463     self->visited = TRUE;
2464
2465     for (it = self->transients; it; it = g_slist_next(it))
2466         client_calc_layer_recursive(it->data, orig,
2467                                     self->layer);
2468 }
2469
2470 static void client_calc_layer_internal(ObClient *self)
2471 {
2472     GSList *sit;
2473
2474     /* transients take on the layer of their parents */
2475     sit = client_search_all_top_parents(self);
2476
2477     for (; sit; sit = g_slist_next(sit))
2478         client_calc_layer_recursive(sit->data, self, 0);
2479 }
2480
2481 void client_calc_layer(ObClient *self)
2482 {
2483     GList *it;
2484
2485     /* skip over stuff above fullscreen layer */
2486     for (it = stacking_list; it; it = g_list_next(it))
2487         if (window_layer(it->data) <= OB_STACKING_LAYER_FULLSCREEN) break;
2488
2489     /* find the windows in the fullscreen layer, and mark them not-visited */
2490     for (; it; it = g_list_next(it)) {
2491         if (window_layer(it->data) < OB_STACKING_LAYER_FULLSCREEN) break;
2492         else if (WINDOW_IS_CLIENT(it->data))
2493             WINDOW_AS_CLIENT(it->data)->visited = FALSE;
2494     }
2495
2496     client_calc_layer_internal(self);
2497
2498     /* skip over stuff above fullscreen layer */
2499     for (it = stacking_list; it; it = g_list_next(it))
2500         if (window_layer(it->data) <= OB_STACKING_LAYER_FULLSCREEN) break;
2501
2502     /* now recalc any windows in the fullscreen layer which have not
2503        had their layer recalced already */
2504     for (; it; it = g_list_next(it)) {
2505         if (window_layer(it->data) < OB_STACKING_LAYER_FULLSCREEN) break;
2506         else if (WINDOW_IS_CLIENT(it->data) &&
2507                  !WINDOW_AS_CLIENT(it->data)->visited)
2508             client_calc_layer_internal(it->data);
2509     }
2510 }
2511
2512 gboolean client_should_show(ObClient *self)
2513 {
2514     if (self->iconic)
2515         return FALSE;
2516     if (client_normal(self) && screen_showing_desktop)
2517         return FALSE;
2518     if (self->desktop == screen_desktop || self->desktop == DESKTOP_ALL)
2519         return TRUE;
2520
2521     return FALSE;
2522 }
2523
2524 gboolean client_show(ObClient *self)
2525 {
2526     gboolean show = FALSE;
2527
2528     if (client_should_show(self)) {
2529         /* replay pending pointer event before showing the window, in case it
2530            should be going to something under the window */
2531         mouse_replay_pointer();
2532
2533         render_plugin->frame_show(self->frame);
2534         show = TRUE;
2535
2536         /* According to the ICCCM (sec 4.1.3.1) when a window is not visible,
2537            it needs to be in IconicState. This includes when it is on another
2538            desktop!
2539         */
2540         client_change_wm_state(self);
2541     }
2542     return show;
2543 }
2544
2545 gboolean client_hide(ObClient *self)
2546 {
2547     gboolean hide = FALSE;
2548
2549     if (!client_should_show(self)) {
2550         if (self == focus_client) {
2551             /* if there is a grab going on, then we need to cancel it. if we
2552                move focus during the grab, applications will get
2553                NotifyWhileGrabbed events and ignore them !
2554
2555                actions should not rely on being able to move focus during an
2556                interactive grab.
2557             */
2558             event_cancel_all_key_grabs();
2559         }
2560
2561         /* We don't need to ignore enter events here.
2562            The window can hide/iconify in 3 different ways:
2563            1 - through an x message. in this case we ignore all enter events
2564                caused by responding to the x message (unless underMouse)
2565            2 - by a keyboard action. in this case we ignore all enter events
2566                caused by the action
2567            3 - by a mouse action. in this case they are doing stuff with the
2568                mouse and focus _should_ move.
2569
2570            Also in action_end, we simulate an enter event that can't be ignored
2571            so trying to ignore them is futile in case 3 anyways
2572         */
2573
2574         /* replay pending pointer event before hiding the window, in case it
2575            should be going to the window */
2576         mouse_replay_pointer();
2577
2578         self->ignore_unmaps += render_plugin->frame_hide(self->frame);
2579         hide = TRUE;
2580
2581         /* According to the ICCCM (sec 4.1.3.1) when a window is not visible,
2582            it needs to be in IconicState. This includes when it is on another
2583            desktop!
2584         */
2585         client_change_wm_state(self);
2586     }
2587     return hide;
2588 }
2589
2590 void client_showhide(ObClient *self)
2591 {
2592     if (!client_show(self))
2593         client_hide(self);
2594 }
2595
2596 gboolean client_normal(ObClient *self) {
2597     return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
2598               self->type == OB_CLIENT_TYPE_DOCK ||
2599               self->type == OB_CLIENT_TYPE_SPLASH);
2600 }
2601
2602 gboolean client_helper(ObClient *self)
2603 {
2604     return (self->type == OB_CLIENT_TYPE_UTILITY ||
2605             self->type == OB_CLIENT_TYPE_MENU ||
2606             self->type == OB_CLIENT_TYPE_TOOLBAR);
2607 }
2608
2609 gboolean client_mouse_focusable(ObClient *self)
2610 {
2611     return !(self->type == OB_CLIENT_TYPE_MENU ||
2612              self->type == OB_CLIENT_TYPE_TOOLBAR ||
2613              self->type == OB_CLIENT_TYPE_SPLASH ||
2614              self->type == OB_CLIENT_TYPE_DOCK);
2615 }
2616
2617 gboolean client_enter_focusable(ObClient *self)
2618 {
2619     /* you can focus desktops but it shouldn't on enter */
2620     return (client_mouse_focusable(self) &&
2621             self->type != OB_CLIENT_TYPE_DESKTOP);
2622 }
2623
2624
2625 static void client_apply_startup_state(ObClient *self,
2626                                        gint x, gint y, gint w, gint h)
2627 {
2628     /* save the states that we are going to apply */
2629     gboolean iconic = self->iconic;
2630     gboolean fullscreen = self->fullscreen;
2631     gboolean undecorated = self->undecorated;
2632     gboolean shaded = self->shaded;
2633     gboolean demands_attention = self->demands_attention;
2634     gboolean max_horz = self->max_horz;
2635     gboolean max_vert = self->max_vert;
2636     Rect oldarea;
2637     gint l;
2638
2639     /* turn them all off in the client, so they won't affect the window
2640        being placed */
2641     self->iconic = self->fullscreen = self->undecorated = self->shaded =
2642         self->demands_attention = self->max_horz = self->max_vert = FALSE;
2643
2644     /* move the client to its placed position, or it it's already there,
2645        generate a ConfigureNotify telling the client where it is.
2646
2647        do this after adjusting the frame. otherwise it gets all weird and
2648        clients don't work right
2649
2650        do this before applying the states so they have the correct
2651        pre-max/pre-fullscreen values
2652     */
2653     client_try_configure(self, &x, &y, &w, &h, &l, &l, FALSE);
2654     ob_debug("placed window 0x%x at %d, %d with size %d x %d",
2655              self->window, x, y, w, h);
2656     /* save the area, and make it where it should be for the premax stuff */
2657     oldarea = self->area;
2658     RECT_SET(self->area, x, y, w, h);
2659     render_plugin->frame_set_client_area (self->frame, self->area);
2660
2661     /* apply the states. these are in a carefully crafted order.. */
2662
2663     if (iconic)
2664         client_iconify(self, TRUE, FALSE, TRUE);
2665     if (fullscreen)
2666         client_fullscreen(self, TRUE);
2667     if (undecorated)
2668         client_set_undecorated(self, TRUE);
2669     if (shaded)
2670         client_shade(self, TRUE);
2671     if (demands_attention)
2672         client_hilite(self, TRUE);
2673
2674     if (max_vert && max_horz)
2675         client_maximize(self, TRUE, 0);
2676     else if (max_vert)
2677         client_maximize(self, TRUE, 2);
2678     else if (max_horz)
2679         client_maximize(self, TRUE, 1);
2680
2681     /* if the window hasn't been configured yet, then do so now, in fact the
2682        x,y,w,h may _not_ be the same as the area rect, which can end up
2683        meaning that the client isn't properly moved/resized by the fullscreen
2684        function
2685        pho can cause this because it maps at size of the screen but not 0,0
2686        so openbox moves it on screen to 0,0 (thus x,y=0,0 and area.x,y don't).
2687        then fullscreen'ing makes it go to 0,0 which it thinks it already is at
2688        cuz thats where the pre-fullscreen will be. however the actual area is
2689        not, so this needs to be called even if we have fullscreened/maxed
2690     */
2691     self->area = oldarea;
2692     render_plugin->frame_set_client_area (self->frame, self->area);
2693     render_plugin->frame_set_decorations (self->frame, self->decorations);
2694     render_plugin->frame_update_layout (self->frame, FALSE, FALSE);
2695     render_plugin->frame_set_is_max_horz (self->frame, self->max_horz);
2696     render_plugin->frame_set_is_max_vert (self->frame, self->max_vert);
2697     client_configure(self, x, y, w, h, FALSE, TRUE, FALSE);
2698
2699     /* set the desktop hint, to make sure that it always exists */
2700     OBT_PROP_SET32(self->window, NET_WM_DESKTOP, CARDINAL, self->desktop);
2701
2702     /* nothing to do for the other states:
2703        skip_taskbar
2704        skip_pager
2705        modal
2706        above
2707        below
2708     */
2709 }
2710
2711 void client_gravity_resize_w(ObClient *self, gint *x, gint oldw, gint neww)
2712 {
2713     /* these should be the current values. this is for when you're not moving,
2714        just resizing */
2715     g_assert(*x == self->area.x);
2716     g_assert(oldw == self->area.width);
2717
2718     /* horizontal */
2719     switch (self->gravity) {
2720     default:
2721     case NorthWestGravity:
2722     case WestGravity:
2723     case SouthWestGravity:
2724     case StaticGravity:
2725     case ForgetGravity:
2726         break;
2727     case NorthGravity:
2728     case CenterGravity:
2729     case SouthGravity:
2730         *x -= (neww - oldw) / 2;
2731         break;
2732     case NorthEastGravity:
2733     case EastGravity:
2734     case SouthEastGravity:
2735         *x -= neww - oldw;
2736         break;
2737     }
2738 }
2739
2740 void client_gravity_resize_h(ObClient *self, gint *y, gint oldh, gint newh)
2741 {
2742     /* these should be the current values. this is for when you're not moving,
2743        just resizing */
2744     g_assert(*y == self->area.y);
2745     g_assert(oldh == self->area.height);
2746
2747     /* vertical */
2748     switch (self->gravity) {
2749     default:
2750     case NorthWestGravity:
2751     case NorthGravity:
2752     case NorthEastGravity:
2753     case StaticGravity:
2754     case ForgetGravity:
2755         break;
2756     case WestGravity:
2757     case CenterGravity:
2758     case EastGravity:
2759         *y -= (newh - oldh) / 2;
2760         break;
2761     case SouthWestGravity:
2762     case SouthGravity:
2763     case SouthEastGravity:
2764         *y -= newh - oldh;
2765         break;
2766     }
2767 }
2768
2769 void client_try_configure(ObClient *self, gint *x, gint *y, gint *w, gint *h,
2770                           gint *logicalw, gint *logicalh,
2771                           gboolean user)
2772 {
2773     Rect desired = {*x, *y, *w, *h};
2774     frame_rect_to_frame(self, &desired);
2775
2776     /* make the frame recalculate its dimentions n shit without changing
2777        anything visible for real, this way the constraints below can work with
2778        the updated frame dimensions. */
2779     render_plugin->frame_set_decorations (self->frame, self->decorations);
2780     render_plugin->frame_update_layout (self->frame, FALSE, TRUE);
2781
2782     /* gets the frame's position */
2783     frame_client_gravity(self, x, y);
2784
2785     /* these positions are frame positions, not client positions */
2786
2787     /* set the size and position if fullscreen */
2788     if (self->fullscreen) {
2789         Rect *a;
2790         guint i;
2791
2792         i = screen_find_monitor(&desired);
2793         a = screen_physical_area_monitor(i);
2794
2795         *x = a->x;
2796         *y = a->y;
2797         *w = a->width;
2798         *h = a->height;
2799
2800         user = FALSE; /* ignore if the client can't be moved/resized when it
2801                          is fullscreening */
2802
2803         g_free(a);
2804     } else if (self->max_horz || self->max_vert) {
2805         Rect *a;
2806         guint i;
2807
2808         /* use all possible struts when maximizing to the full screen */
2809         i = screen_find_monitor(&desired);
2810         a = screen_area(self->desktop, i,
2811                         (self->max_horz && self->max_vert ? NULL : &desired));
2812
2813         Strut size = render_plugin->frame_get_size(self->frame);
2814         /* set the size and position if maximized */
2815         if (self->max_horz) {
2816             *x = a->x;
2817             *w = a->width - size.left - size.right;
2818         }
2819         if (self->max_vert) {
2820             *y = a->y;
2821             *h = a->height - size.top - size.bottom;
2822         }
2823
2824         user = FALSE; /* ignore if the client can't be moved/resized when it
2825                          is maximizing */
2826
2827         g_free(a);
2828     }
2829
2830     /* gets the client's position */
2831     frame_frame_gravity(self, x, y);
2832
2833     /* work within the prefered sizes given by the window */
2834     if (!(*w == self->area.width && *h == self->area.height)) {
2835         gint basew, baseh, minw, minh;
2836         gint incw, inch;
2837         gfloat minratio, maxratio;
2838
2839         incw = self->fullscreen || self->max_horz ? 1 : self->size_inc.width;
2840         inch = self->fullscreen || self->max_vert ? 1 : self->size_inc.height;
2841         minratio = self->fullscreen || (self->max_horz && self->max_vert) ?
2842             0 : self->min_ratio;
2843         maxratio = self->fullscreen || (self->max_horz && self->max_vert) ?
2844             0 : self->max_ratio;
2845
2846         /* base size is substituted with min size if not specified */
2847         if (self->base_size.width || self->base_size.height) {
2848             basew = self->base_size.width;
2849             baseh = self->base_size.height;
2850         } else {
2851             basew = self->min_size.width;
2852             baseh = self->min_size.height;
2853         }
2854         /* min size is substituted with base size if not specified */
2855         if (self->min_size.width || self->min_size.height) {
2856             minw = self->min_size.width;
2857             minh = self->min_size.height;
2858         } else {
2859             minw = self->base_size.width;
2860             minh = self->base_size.height;
2861         }
2862
2863         /* if this is a user-requested resize, then check against min/max
2864            sizes */
2865
2866         /* smaller than min size or bigger than max size? */
2867         if (*w > self->max_size.width) *w = self->max_size.width;
2868         if (*w < minw) *w = minw;
2869         if (*h > self->max_size.height) *h = self->max_size.height;
2870         if (*h < minh) *h = minh;
2871
2872         *w -= basew;
2873         *h -= baseh;
2874
2875         /* keep to the increments */
2876         *w /= incw;
2877         *h /= inch;
2878
2879         /* you cannot resize to nothing */
2880         if (basew + *w < 1) *w = 1 - basew;
2881         if (baseh + *h < 1) *h = 1 - baseh;
2882
2883         /* save the logical size */
2884         *logicalw = incw > 1 ? *w : *w + basew;
2885         *logicalh = inch > 1 ? *h : *h + baseh;
2886
2887         *w *= incw;
2888         *h *= inch;
2889
2890         *w += basew;
2891         *h += baseh;
2892
2893         /* adjust the height to match the width for the aspect ratios.
2894            for this, min size is not substituted for base size ever. */
2895         *w -= self->base_size.width;
2896         *h -= self->base_size.height;
2897
2898         if (minratio)
2899             if (*h * minratio > *w) {
2900                 *h = (gint)(*w / minratio);
2901
2902                 /* you cannot resize to nothing */
2903                 if (*h < 1) {
2904                     *h = 1;
2905                     *w = (gint)(*h * minratio);
2906                 }
2907             }
2908         if (maxratio)
2909             if (*h * maxratio < *w) {
2910                 *h = (gint)(*w / maxratio);
2911
2912                 /* you cannot resize to nothing */
2913                 if (*h < 1) {
2914                     *h = 1;
2915                     *w = (gint)(*h * minratio);
2916                 }
2917             }
2918
2919         *w += self->base_size.width;
2920         *h += self->base_size.height;
2921     }
2922
2923     /* these override the above states! if you cant move you can't move! */
2924     if (user) {
2925         if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
2926             *x = self->area.x;
2927             *y = self->area.y;
2928         }
2929         if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
2930             *w = self->area.width;
2931             *h = self->area.height;
2932         }
2933     }
2934
2935     g_assert(*w > 0);
2936     g_assert(*h > 0);
2937 }
2938
2939
2940 void client_configure(ObClient *self, gint x, gint y, gint w, gint h,
2941                       gboolean user, gboolean final, gboolean force_reply)
2942 {
2943     Rect oldframe;
2944     gint oldw, oldh;
2945     gboolean send_resize_client;
2946     gboolean moved = FALSE, resized = FALSE, rootmoved = FALSE;
2947     gboolean fmoved, fresized;
2948     guint fdecor = render_plugin->frame_get_decorations(self->frame);
2949     gboolean fhorz = render_plugin->frame_is_max_horz(self->frame);
2950     gboolean fvert = render_plugin->frame_is_max_vert(self->frame);
2951     gint logicalw, logicalh;
2952
2953     Strut size = render_plugin->frame_get_size(self->frame);
2954     Rect area = render_plugin->frame_get_window_area(self->frame);
2955
2956     /* find the new x, y, width, and height (and logical size) */
2957     client_try_configure(self, &x, &y, &w, &h, &logicalw, &logicalh, user);
2958
2959     /* set the logical size if things changed */
2960     if (!(w == self->area.width && h == self->area.height))
2961         SIZE_SET(self->logical_size, logicalw, logicalh);
2962
2963     /* figure out if we moved or resized or what */
2964     moved = (x != self->area.x || y != self->area.y);
2965     resized = (w != self->area.width || h != self->area.height);
2966
2967     oldw = self->area.width;
2968     oldh = self->area.height;
2969     oldframe = render_plugin->frame_get_window_area(self->frame);
2970     RECT_SET(self->area, x, y, w, h);
2971
2972     render_plugin->frame_set_client_area (self->frame, self->area);
2973
2974     /* for app-requested resizes, always resize if 'resized' is true.
2975        for user-requested ones, only resize if final is true, or when
2976        resizing in redraw mode */
2977     send_resize_client = ((!user && resized) ||
2978                           (user && (final ||
2979                                     (resized && config_resize_redraw))));
2980
2981     /* if the client is enlarging, then resize the client before the frame */
2982     if (send_resize_client && (w > oldw || h > oldh)) {
2983         render_plugin->frame_set_decorations (self->frame, self->decorations);
2984         render_plugin->frame_update_layout (self->frame, FALSE, FALSE);
2985     }
2986
2987     /* find the frame's dimensions and move/resize it */
2988     fmoved = moved;
2989     fresized = resized;
2990
2991     /* if decorations changed, then readjust everything for the frame */
2992     if (self->decorations != fdecor ||
2993         self->max_horz != fhorz || self->max_vert != fvert)
2994     {
2995         fmoved = fresized = TRUE;
2996     }
2997
2998     /* adjust the frame */
2999     if (fmoved || fresized) {
3000         gulong ignore_start;
3001         if (!user)
3002             ignore_start = event_start_ignore_all_enters();
3003
3004         /* replay pending pointer event before move the window, in case it
3005            would change what window gets the event */
3006         mouse_replay_pointer();
3007
3008         render_plugin->frame_set_decorations (self->frame, self->decorations);
3009         render_plugin->frame_update_layout (self->frame, TRUE, FALSE);
3010
3011         if (!user)
3012             event_end_ignore_all_enters(ignore_start);
3013     }
3014
3015     if (!user || final) {
3016         gint oldrx = self->root_pos.x;
3017         gint oldry = self->root_pos.y;
3018         /* we have reset the client to 0 border width, so don't include
3019            it in these coords */
3020         POINT_SET(self->root_pos,
3021                   area.x + size.left -
3022                   self->border_width,
3023                   area.y + size.top -
3024                   self->border_width);
3025         if (self->root_pos.x != oldrx || self->root_pos.y != oldry)
3026             rootmoved = TRUE;
3027     }
3028
3029     /* This is kinda tricky and should not be changed.. let me explain!
3030
3031        When user = FALSE, then the request is coming from the application
3032        itself, and we are more strict about when to send a synthetic
3033        ConfigureNotify.  We strictly follow the rules of the ICCCM sec 4.1.5
3034        in this case (if force_reply is true)
3035
3036        When user = TRUE, then the request is coming from "us", like when we
3037        maximize a window or something.  In this case we are more lenient.  We
3038        used to follow the same rules as above, but _Java_ Swing can't handle
3039        this. So just to appease Swing, when user = TRUE, we always send
3040        a synthetic ConfigureNotify to give the window its root coordinates.
3041     */
3042     if ((!user && !resized && (rootmoved || force_reply)) ||
3043         (user && final && rootmoved))
3044     {
3045         XEvent event;
3046
3047         event.type = ConfigureNotify;
3048         event.xconfigure.display = obt_display;
3049         event.xconfigure.event = self->window;
3050         event.xconfigure.window = self->window;
3051
3052         ob_debug("Sending ConfigureNotify to %s for %d,%d %dx%d",
3053                  self->title, self->root_pos.x, self->root_pos.y, w, h);
3054
3055         /* root window real coords */
3056         event.xconfigure.x = self->root_pos.x;
3057         event.xconfigure.y = self->root_pos.y;
3058         event.xconfigure.width = w;
3059         event.xconfigure.height = h;
3060         event.xconfigure.border_width = self->border_width;
3061         event.xconfigure.above = None;
3062         event.xconfigure.override_redirect = FALSE;
3063         XSendEvent(event.xconfigure.display, event.xconfigure.window,
3064                    FALSE, StructureNotifyMask, &event);
3065     }
3066
3067     /* if the client is shrinking, then resize the frame before the client.
3068
3069        both of these resize sections may run, because the top one only resizes
3070        in the direction that is growing
3071      */
3072     if (send_resize_client && (w <= oldw || h <= oldh)) {
3073       render_plugin->frame_set_decorations (self->frame, self->decorations);
3074       render_plugin->frame_update_layout (self->frame, FALSE, FALSE);
3075     }
3076
3077     XFlush(obt_display);
3078
3079     /* if it moved between monitors, then this can affect the stacking
3080        layer of this window or others - for fullscreen windows */
3081     Rect current_frame = render_plugin->frame_get_window_area(self->frame);
3082     if (screen_find_monitor(&current_frame) !=
3083         screen_find_monitor(&oldframe))
3084     {
3085         client_calc_layer(self);
3086     }
3087 }
3088
3089 void client_fullscreen(ObClient *self, gboolean fs)
3090 {
3091     gint x, y, w, h;
3092
3093     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
3094         self->fullscreen == fs) return;                   /* already done */
3095
3096     self->fullscreen = fs;
3097     client_change_state(self); /* change the state hints on the client */
3098
3099     if (fs) {
3100         self->pre_fullscreen_area = self->area;
3101         /* if the window is maximized, its area isn't all that meaningful.
3102            save it's premax area instead. */
3103         if (self->max_horz) {
3104             self->pre_fullscreen_area.x = self->pre_max_area.x;
3105             self->pre_fullscreen_area.width = self->pre_max_area.width;
3106         }
3107         if (self->max_vert) {
3108             self->pre_fullscreen_area.y = self->pre_max_area.y;
3109             self->pre_fullscreen_area.height = self->pre_max_area.height;
3110         }
3111
3112         /* these will help configure_full figure out where to fullscreen
3113            the window */
3114         x = self->area.x;
3115         y = self->area.y;
3116         w = self->area.width;
3117         h = self->area.height;
3118     } else {
3119         g_assert(self->pre_fullscreen_area.width > 0 &&
3120                  self->pre_fullscreen_area.height > 0);
3121
3122         x = self->pre_fullscreen_area.x;
3123         y = self->pre_fullscreen_area.y;
3124         w = self->pre_fullscreen_area.width;
3125         h = self->pre_fullscreen_area.height;
3126         RECT_SET(self->pre_fullscreen_area, 0, 0, 0, 0);
3127     }
3128
3129     ob_debug("Window %s going fullscreen (%d)",
3130              self->title, self->fullscreen);
3131
3132     client_setup_decor_and_functions(self, FALSE);
3133     client_move_resize(self, x, y, w, h);
3134
3135     /* and adjust our layer/stacking. do this after resizing the window,
3136        and applying decorations, because windows which fill the screen are
3137        considered "fullscreen" and it affects their layer */
3138     client_calc_layer(self);
3139
3140     if (fs) {
3141         /* try focus us when we go into fullscreen mode */
3142         client_focus(self);
3143     }
3144 }
3145
3146 static void client_iconify_recursive(ObClient *self,
3147                                      gboolean iconic, gboolean curdesk,
3148                                      gboolean hide_animation)
3149 {
3150     GSList *it;
3151     gboolean changed = FALSE;
3152
3153
3154     if (self->iconic != iconic) {
3155         ob_debug("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
3156                  self->window);
3157
3158         if (iconic) {
3159             /* don't let non-normal windows iconify along with their parents
3160                or whatever */
3161             if (client_normal(self)) {
3162                 self->iconic = iconic;
3163
3164                 /* update the focus lists.. iconic windows go to the bottom of
3165                    the list */
3166                 focus_order_to_bottom(self);
3167
3168                 changed = TRUE;
3169             }
3170         } else {
3171             self->iconic = iconic;
3172
3173             if (curdesk && self->desktop != screen_desktop &&
3174                 self->desktop != DESKTOP_ALL)
3175                 client_set_desktop(self, screen_desktop, FALSE, FALSE);
3176
3177             /* this puts it after the current focused window */
3178             focus_order_remove(self);
3179             focus_order_add_new(self);
3180
3181             changed = TRUE;
3182         }
3183     }
3184
3185     if (changed) {
3186         client_change_state(self);
3187         if (config_animate_iconify && !hide_animation)
3188         render_plugin->frame_begin_iconify_animation(self->frame, iconic);
3189         /* do this after starting the animation so it doesn't flash */
3190         client_showhide(self);
3191     }
3192
3193     /* iconify all direct transients, and deiconify all transients
3194        (non-direct too) */
3195     for (it = self->transients; it; it = g_slist_next(it))
3196         if (it->data != self)
3197             if (client_is_direct_child(self, it->data) || !iconic)
3198                 client_iconify_recursive(it->data, iconic, curdesk,
3199                                          hide_animation);
3200 }
3201
3202 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk,
3203                     gboolean hide_animation)
3204 {
3205     if (self->functions & OB_CLIENT_FUNC_ICONIFY || !iconic) {
3206         /* move up the transient chain as far as possible first */
3207         self = client_search_top_direct_parent(self);
3208         client_iconify_recursive(self, iconic, curdesk, hide_animation);
3209     }
3210 }
3211
3212 void client_maximize(ObClient *self, gboolean max, gint dir)
3213 {
3214     gint x, y, w, h;
3215
3216     g_assert(dir == 0 || dir == 1 || dir == 2);
3217     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE)) return; /* can't */
3218
3219     /* check if already done */
3220     if (max) {
3221         if (dir == 0 && self->max_horz && self->max_vert) return;
3222         if (dir == 1 && self->max_horz) return;
3223         if (dir == 2 && self->max_vert) return;
3224     } else {
3225         if (dir == 0 && !self->max_horz && !self->max_vert) return;
3226         if (dir == 1 && !self->max_horz) return;
3227         if (dir == 2 && !self->max_vert) return;
3228     }
3229
3230     /* these will help configure_full figure out which screen to fill with
3231        the window */
3232     x = self->area.x;
3233     y = self->area.y;
3234     w = self->area.width;
3235     h = self->area.height;
3236
3237     if (max) {
3238         if ((dir == 0 || dir == 1) && !self->max_horz) { /* horz */
3239             RECT_SET(self->pre_max_area,
3240                      self->area.x, self->pre_max_area.y,
3241                      self->area.width, self->pre_max_area.height);
3242         }
3243         if ((dir == 0 || dir == 2) && !self->max_vert) { /* vert */
3244             RECT_SET(self->pre_max_area,
3245                      self->pre_max_area.x, self->area.y,
3246                      self->pre_max_area.width, self->area.height);
3247         }
3248     } else {
3249         if ((dir == 0 || dir == 1) && self->max_horz) { /* horz */
3250             g_assert(self->pre_max_area.width > 0);
3251
3252             x = self->pre_max_area.x;
3253             w = self->pre_max_area.width;
3254
3255             RECT_SET(self->pre_max_area, 0, self->pre_max_area.y,
3256                      0, self->pre_max_area.height);
3257         }
3258         if ((dir == 0 || dir == 2) && self->max_vert) { /* vert */
3259             g_assert(self->pre_max_area.height > 0);
3260
3261             y = self->pre_max_area.y;
3262             h = self->pre_max_area.height;
3263
3264             RECT_SET(self->pre_max_area, self->pre_max_area.x, 0,
3265                      self->pre_max_area.width, 0);
3266         }
3267     }
3268
3269     if (dir == 0 || dir == 1) /* horz */
3270         self->max_horz = max;
3271     if (dir == 0 || dir == 2) /* vert */
3272         self->max_vert = max;
3273
3274     render_plugin->frame_set_is_max_horz (self->frame, self->max_horz);
3275     render_plugin->frame_set_is_max_vert (self->frame, self->max_vert);
3276
3277     client_change_state(self); /* change the state hints on the client */
3278
3279     client_setup_decor_and_functions(self, FALSE);
3280     client_move_resize(self, x, y, w, h);
3281 }
3282
3283 void client_shade(ObClient *self, gboolean shade)
3284 {
3285     if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
3286          shade) ||                         /* can't shade */
3287         self->shaded == shade) return;     /* already done */
3288
3289     self->shaded = shade;
3290     client_change_state(self);
3291     client_change_wm_state(self); /* the window is being hidden/shown */
3292     /* resize the frame to just the titlebar */
3293     render_plugin->frame_set_is_shaded (self->frame, self->shaded);
3294     render_plugin->frame_update_layout(self->frame, FALSE, FALSE);
3295 }
3296
3297 static void client_ping_event(ObClient *self, gboolean dead)
3298 {
3299     self->not_responding = dead;
3300     client_update_title(self);
3301
3302     if (!dead) {
3303         /* it came back to life ! */
3304
3305         if (self->kill_prompt) {
3306             prompt_unref(self->kill_prompt);
3307             self->kill_prompt = NULL;
3308         }
3309
3310         self->kill_level = 0;
3311     }
3312 }
3313
3314 void client_close(ObClient *self)
3315 {
3316     if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
3317
3318     if (self->prompt) {
3319         prompt_cancel(self->prompt);
3320         return;
3321     }
3322
3323     /* in the case that the client provides no means to requesting that it
3324        close, we just kill it */
3325     if (!self->delete_window)
3326         /* don't use client_kill(), we should only kill based on PID in
3327            response to a lack of PING replies */
3328         XKillClient(obt_display, self->window);
3329     else {
3330         /* request the client to close with WM_DELETE_WINDOW */
3331         OBT_PROP_MSG_TO(self->window, self->window, WM_PROTOCOLS,
3332                         OBT_PROP_ATOM(WM_DELETE_WINDOW), event_curtime,
3333                         0, 0, 0, NoEventMask);
3334
3335         if (self->not_responding)
3336             client_prompt_kill(self);
3337     }
3338 }
3339
3340 #define OB_KILL_RESULT_NO 0
3341 #define OB_KILL_RESULT_YES 1
3342
3343 static void client_kill_requested(ObPrompt *p, gint result, gpointer data)
3344 {
3345     ObClient *self = data;
3346
3347     if (result == OB_KILL_RESULT_YES)
3348         client_kill(self);
3349
3350     prompt_unref(self->kill_prompt);
3351     self->kill_prompt = NULL;
3352 }
3353
3354 static void client_prompt_kill(ObClient *self)
3355 {
3356     /* check if we're already prompting */
3357     if (!self->kill_prompt) {
3358         ObPromptAnswer answers[] = {
3359             { _("No"), OB_KILL_RESULT_NO },
3360             { _("Yes"), OB_KILL_RESULT_YES }
3361         };
3362         gchar *m;
3363         const gchar *sig;
3364
3365         if (self->kill_level == 0)
3366             sig = "terminate";
3367         else
3368             sig = "kill";
3369
3370         m = g_strdup_printf
3371             (_("The window \"%s\" does not seem to be responding.  Do you want to force it to exit by sending the %s signal?"), self->original_title, sig);
3372
3373         self->kill_prompt = prompt_new(m, answers,
3374                                        sizeof(answers)/sizeof(answers[0]),
3375                                        OB_KILL_RESULT_NO, /* default = no */
3376                                        OB_KILL_RESULT_NO, /* cancel = no */
3377                                        client_kill_requested, self);
3378         g_free(m);
3379     }
3380
3381     prompt_show(self->kill_prompt, self);
3382 }
3383
3384 void client_kill(ObClient *self)
3385 {
3386     /* don't kill our own windows */
3387     if (self->prompt) return;
3388
3389     if (!self->client_machine && self->pid) {
3390         /* running on the local host */
3391         if (self->kill_level == 0) {
3392             ob_debug("killing window 0x%x with pid %lu, with SIGTERM",
3393                      self->window, self->pid);
3394             kill(self->pid, SIGTERM);
3395             ++self->kill_level;
3396
3397             /* show that we're trying to kill it */
3398             client_update_title(self);
3399         }
3400         else {
3401             ob_debug("killing window 0x%x with pid %lu, with SIGKILL",
3402                      self->window, self->pid);
3403             kill(self->pid, SIGKILL); /* kill -9 */
3404         }
3405     }
3406     else {
3407         /* running on a remote host */
3408         XKillClient(obt_display, self->window);
3409     }
3410 }
3411
3412 void client_hilite(ObClient *self, gboolean hilite)
3413 {
3414     if (self->demands_attention == hilite)
3415         return; /* no change */
3416
3417     /* don't allow focused windows to hilite */
3418     self->demands_attention = hilite && !client_focused(self);
3419     if (self->frame != NULL) { /* if we're mapping, just set the state */
3420         if (self->demands_attention)
3421         render_plugin->frame_flash_start(self->frame);
3422         else
3423         render_plugin->frame_flash_stop(self->frame);
3424         client_change_state(self);
3425     }
3426 }
3427
3428 static void client_set_desktop_recursive(ObClient *self,
3429                                          guint target,
3430                                          gboolean donthide,
3431                                          gboolean dontraise)
3432 {
3433     guint old;
3434     GSList *it;
3435
3436     if (target != self->desktop && self->type != OB_CLIENT_TYPE_DESKTOP) {
3437
3438         ob_debug("Setting desktop %u", target+1);
3439
3440         g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
3441
3442         old = self->desktop;
3443         self->desktop = target;
3444         OBT_PROP_SET32(self->window, NET_WM_DESKTOP, CARDINAL, target);
3445         /* the frame can display the current desktop state */
3446         render_plugin->frame_set_decorations (self->frame, self->decorations);
3447         render_plugin->frame_update_layout(self->frame, FALSE, FALSE);
3448         /* 'move' the window to the new desktop */
3449         if (!donthide)
3450             client_hide(self);
3451         client_show(self);
3452         /* raise if it was not already on the desktop */
3453         if (old != DESKTOP_ALL && !dontraise)
3454             stacking_raise(CLIENT_AS_WINDOW(self));
3455         if (STRUT_EXISTS(self->strut))
3456             screen_update_areas();
3457         else
3458             /* the new desktop's geometry may be different, so we may need to
3459                resize, for example if we are maximized */
3460             client_reconfigure(self, FALSE);
3461     }
3462
3463     /* move all transients */
3464     for (it = self->transients; it; it = g_slist_next(it))
3465         if (it->data != self)
3466             if (client_is_direct_child(self, it->data))
3467                 client_set_desktop_recursive(it->data, target,
3468                                              donthide, dontraise);
3469 }
3470
3471 void client_set_desktop(ObClient *self, guint target,
3472                         gboolean donthide, gboolean dontraise)
3473 {
3474     self = client_search_top_direct_parent(self);
3475     client_set_desktop_recursive(self, target, donthide, dontraise);
3476 }
3477
3478 gboolean client_is_direct_child(ObClient *parent, ObClient *child)
3479 {
3480     while (child != parent && (child = client_direct_parent(child)));
3481     return child == parent;
3482 }
3483
3484 ObClient *client_search_modal_child(ObClient *self)
3485 {
3486     GSList *it;
3487     ObClient *ret;
3488
3489     for (it = self->transients; it; it = g_slist_next(it)) {
3490         ObClient *c = it->data;
3491         if ((ret = client_search_modal_child(c))) return ret;
3492         if (c->modal) return c;
3493     }
3494     return NULL;
3495 }
3496
3497 gboolean client_validate(ObClient *self)
3498 {
3499     XEvent e;
3500
3501     XSync(obt_display, FALSE); /* get all events on the server */
3502
3503     if (XCheckTypedWindowEvent(obt_display, self->window, DestroyNotify, &e) ||
3504         XCheckTypedWindowEvent(obt_display, self->window, UnmapNotify, &e))
3505     {
3506         XPutBackEvent(obt_display, &e);
3507         return FALSE;
3508     }
3509
3510     return TRUE;
3511 }
3512
3513 void client_set_wm_state(ObClient *self, glong state)
3514 {
3515     if (state == self->wmstate) return; /* no change */
3516
3517     switch (state) {
3518     case IconicState:
3519         client_iconify(self, TRUE, TRUE, FALSE);
3520         break;
3521     case NormalState:
3522         client_iconify(self, FALSE, TRUE, FALSE);
3523         break;
3524     }
3525 }
3526
3527 void client_set_state(ObClient *self, Atom action, glong data1, glong data2)
3528 {
3529     gboolean shaded = self->shaded;
3530     gboolean fullscreen = self->fullscreen;
3531     gboolean undecorated = self->undecorated;
3532     gboolean max_horz = self->max_horz;
3533     gboolean max_vert = self->max_vert;
3534     gboolean modal = self->modal;
3535     gboolean iconic = self->iconic;
3536     gboolean demands_attention = self->demands_attention;
3537     gboolean above = self->above;
3538     gboolean below = self->below;
3539     gint i;
3540
3541     if (!(action == OBT_PROP_ATOM(NET_WM_STATE_ADD) ||
3542           action == OBT_PROP_ATOM(NET_WM_STATE_REMOVE) ||
3543           action == OBT_PROP_ATOM(NET_WM_STATE_TOGGLE)))
3544         /* an invalid action was passed to the client message, ignore it */
3545         return;
3546
3547     for (i = 0; i < 2; ++i) {
3548         Atom state = i == 0 ? data1 : data2;
3549
3550         if (!state) continue;
3551
3552         /* if toggling, then pick whether we're adding or removing */
3553         if (action == OBT_PROP_ATOM(NET_WM_STATE_TOGGLE)) {
3554             if (state == OBT_PROP_ATOM(NET_WM_STATE_MODAL))
3555                 action = modal ? OBT_PROP_ATOM(NET_WM_STATE_REMOVE) :
3556                     OBT_PROP_ATOM(NET_WM_STATE_ADD);
3557             else if (state == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_VERT))
3558                 action = self->max_vert ? OBT_PROP_ATOM(NET_WM_STATE_REMOVE) :
3559                     OBT_PROP_ATOM(NET_WM_STATE_ADD);
3560             else if (state == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_HORZ))
3561                 action = self->max_horz ? OBT_PROP_ATOM(NET_WM_STATE_REMOVE) :
3562                     OBT_PROP_ATOM(NET_WM_STATE_ADD);
3563             else if (state == OBT_PROP_ATOM(NET_WM_STATE_SHADED))
3564                 action = shaded ? OBT_PROP_ATOM(NET_WM_STATE_REMOVE) :
3565                     OBT_PROP_ATOM(NET_WM_STATE_ADD);
3566             else if (state == OBT_PROP_ATOM(NET_WM_STATE_SKIP_TASKBAR))
3567                 action = self->skip_taskbar ?
3568                     OBT_PROP_ATOM(NET_WM_STATE_REMOVE) :
3569                     OBT_PROP_ATOM(NET_WM_STATE_ADD);
3570             else if (state == OBT_PROP_ATOM(NET_WM_STATE_SKIP_PAGER))
3571                 action = self->skip_pager ?
3572                     OBT_PROP_ATOM(NET_WM_STATE_REMOVE) :
3573                     OBT_PROP_ATOM(NET_WM_STATE_ADD);
3574             else if (state == OBT_PROP_ATOM(NET_WM_STATE_HIDDEN))
3575                 action = self->iconic ?
3576                     OBT_PROP_ATOM(NET_WM_STATE_REMOVE) :
3577                     OBT_PROP_ATOM(NET_WM_STATE_ADD);
3578             else if (state == OBT_PROP_ATOM(NET_WM_STATE_FULLSCREEN))
3579                 action = fullscreen ?
3580                     OBT_PROP_ATOM(NET_WM_STATE_REMOVE) :
3581                     OBT_PROP_ATOM(NET_WM_STATE_ADD);
3582             else if (state == OBT_PROP_ATOM(NET_WM_STATE_ABOVE))
3583                 action = self->above ? OBT_PROP_ATOM(NET_WM_STATE_REMOVE) :
3584                     OBT_PROP_ATOM(NET_WM_STATE_ADD);
3585             else if (state == OBT_PROP_ATOM(NET_WM_STATE_BELOW))
3586                 action = self->below ? OBT_PROP_ATOM(NET_WM_STATE_REMOVE) :
3587                     OBT_PROP_ATOM(NET_WM_STATE_ADD);
3588             else if (state == OBT_PROP_ATOM(NET_WM_STATE_DEMANDS_ATTENTION))
3589                 action = self->demands_attention ?
3590                     OBT_PROP_ATOM(NET_WM_STATE_REMOVE) :
3591                     OBT_PROP_ATOM(NET_WM_STATE_ADD);
3592             else if (state == OBT_PROP_ATOM(OB_WM_STATE_UNDECORATED))
3593                 action = undecorated ? OBT_PROP_ATOM(NET_WM_STATE_REMOVE) :
3594                     OBT_PROP_ATOM(NET_WM_STATE_ADD);
3595         }
3596
3597         if (action == OBT_PROP_ATOM(NET_WM_STATE_ADD)) {
3598             if (state == OBT_PROP_ATOM(NET_WM_STATE_MODAL)) {
3599                 modal = TRUE;
3600             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_VERT)) {
3601                 max_vert = TRUE;
3602             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_HORZ)) {
3603                 max_horz = TRUE;
3604             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_SHADED)) {
3605                 shaded = TRUE;
3606             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_SKIP_TASKBAR)) {
3607                 self->skip_taskbar = TRUE;
3608             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_SKIP_PAGER)) {
3609                 self->skip_pager = TRUE;
3610             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_HIDDEN)) {
3611                 iconic = TRUE;
3612             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_FULLSCREEN)) {
3613                 fullscreen = TRUE;
3614             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_ABOVE)) {
3615                 above = TRUE;
3616                 below = FALSE;
3617             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_BELOW)) {
3618                 above = FALSE;
3619                 below = TRUE;
3620             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_DEMANDS_ATTENTION)){
3621                 demands_attention = TRUE;
3622             } else if (state == OBT_PROP_ATOM(OB_WM_STATE_UNDECORATED)) {
3623                 undecorated = TRUE;
3624             }
3625
3626         } else { /* action == OBT_PROP_ATOM(NET_WM_STATE_REMOVE) */
3627             if (state == OBT_PROP_ATOM(NET_WM_STATE_MODAL)) {
3628                 modal = FALSE;
3629             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_VERT)) {
3630                 max_vert = FALSE;
3631             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_HORZ)) {
3632                 max_horz = FALSE;
3633             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_SHADED)) {
3634                 shaded = FALSE;
3635             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_SKIP_TASKBAR)) {
3636                 self->skip_taskbar = FALSE;
3637             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_SKIP_PAGER)) {
3638                 self->skip_pager = FALSE;
3639             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_HIDDEN)) {
3640                 iconic = FALSE;
3641             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_FULLSCREEN)) {
3642                 fullscreen = FALSE;
3643             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_ABOVE)) {
3644                 above = FALSE;
3645             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_BELOW)) {
3646                 below = FALSE;
3647             } else if (state == OBT_PROP_ATOM(NET_WM_STATE_DEMANDS_ATTENTION)){
3648                 demands_attention = FALSE;
3649             } else if (state == OBT_PROP_ATOM(OB_WM_STATE_UNDECORATED)) {
3650                 undecorated = FALSE;
3651             }
3652         }
3653     }
3654
3655     if (max_horz != self->max_horz || max_vert != self->max_vert) {
3656         if (max_horz != self->max_horz && max_vert != self->max_vert) {
3657             /* toggling both */
3658             if (max_horz == max_vert) { /* both going the same way */
3659                 client_maximize(self, max_horz, 0);
3660             } else {
3661                 client_maximize(self, max_horz, 1);
3662                 client_maximize(self, max_vert, 2);
3663             }
3664         } else {
3665             /* toggling one */
3666             if (max_horz != self->max_horz)
3667                 client_maximize(self, max_horz, 1);
3668             else
3669                 client_maximize(self, max_vert, 2);
3670         }
3671     }
3672     /* change fullscreen state before shading, as it will affect if the window
3673        can shade or not */
3674     if (fullscreen != self->fullscreen)
3675         client_fullscreen(self, fullscreen);
3676     if (shaded != self->shaded)
3677         client_shade(self, shaded);
3678     if (undecorated != self->undecorated)
3679         client_set_undecorated(self, undecorated);
3680     if (above != self->above || below != self->below) {
3681         self->above = above;
3682         self->below = below;
3683         client_calc_layer(self);
3684     }
3685
3686     if (modal != self->modal) {
3687         self->modal = modal;
3688         /* when a window changes modality, then its stacking order with its
3689            transients needs to change */
3690         stacking_raise(CLIENT_AS_WINDOW(self));
3691
3692         /* it also may get focused. if something is focused that shouldn't
3693            be focused anymore, then move the focus */
3694         if (focus_client && client_focus_target(focus_client) != focus_client)
3695             client_focus(focus_client);
3696     }
3697
3698     if (iconic != self->iconic)
3699         client_iconify(self, iconic, FALSE, FALSE);
3700
3701     if (demands_attention != self->demands_attention)
3702         client_hilite(self, demands_attention);
3703
3704     client_change_state(self); /* change the hint to reflect these changes */
3705 }
3706
3707 ObClient *client_focus_target(ObClient *self)
3708 {
3709     ObClient *child = NULL;
3710
3711     child = client_search_modal_child(self);
3712     if (child) return child;
3713     return self;
3714 }
3715
3716 gboolean client_can_focus(ObClient *self)
3717 {
3718     /* choose the correct target */
3719     self = client_focus_target(self);
3720
3721     if (!render_plugin->frame_is_visible(self->frame))
3722         return FALSE;
3723
3724     if (!(self->can_focus || self->focus_notify))
3725         return FALSE;
3726
3727     return TRUE;
3728 }
3729
3730 gboolean client_focus(ObClient *self)
3731 {
3732     /* we might not focus this window, so if we have modal children which would
3733        be focused instead, bring them to this desktop */
3734     client_bring_modal_windows(self);
3735
3736     /* choose the correct target */
3737     self = client_focus_target(self);
3738
3739     if (!client_can_focus(self)) {
3740         ob_debug_type(OB_DEBUG_FOCUS,
3741                       "Client %s can't be focused", self->title);
3742         return FALSE;
3743     }
3744
3745     ob_debug_type(OB_DEBUG_FOCUS,
3746                   "Focusing client \"%s\" (0x%x) at time %u",
3747                   self->title, self->window, event_curtime);
3748
3749     /* if using focus_delay, stop the timer now so that focus doesn't
3750        go moving on us */
3751     event_halt_focus_delay();
3752
3753     /* if there is a grab going on, then we need to cancel it. if we move
3754        focus during the grab, applications will get NotifyWhileGrabbed events
3755        and ignore them !
3756
3757        actions should not rely on being able to move focus during an
3758        interactive grab.
3759     */
3760     event_cancel_all_key_grabs();
3761
3762     obt_display_ignore_errors(TRUE);
3763
3764     if (self->can_focus) {
3765         /* This can cause a BadMatch error with CurrentTime, or if an app
3766            passed in a bad time for _NET_WM_ACTIVE_WINDOW. */
3767         XSetInputFocus(obt_display, self->window, RevertToPointerRoot,
3768                        event_curtime);
3769     }
3770
3771     if (self->focus_notify) {
3772         XEvent ce;
3773         ce.xclient.type = ClientMessage;
3774         ce.xclient.message_type = OBT_PROP_ATOM(WM_PROTOCOLS);
3775         ce.xclient.display = obt_display;
3776         ce.xclient.window = self->window;
3777         ce.xclient.format = 32;
3778         ce.xclient.data.l[0] = OBT_PROP_ATOM(WM_TAKE_FOCUS);
3779         ce.xclient.data.l[1] = event_curtime;
3780         ce.xclient.data.l[2] = 0l;
3781         ce.xclient.data.l[3] = 0l;
3782         ce.xclient.data.l[4] = 0l;
3783         XSendEvent(obt_display, self->window, FALSE, NoEventMask, &ce);
3784     }
3785
3786     obt_display_ignore_errors(FALSE);
3787
3788     ob_debug_type(OB_DEBUG_FOCUS, "Error focusing? %d",
3789                   obt_display_error_occured);
3790     return !obt_display_error_occured;
3791 }
3792
3793 static void client_present(ObClient *self, gboolean here, gboolean raise,
3794                            gboolean unshade)
3795 {
3796     if (client_normal(self) && screen_showing_desktop)
3797         screen_show_desktop(FALSE, self);
3798     if (self->iconic)
3799         client_iconify(self, FALSE, here, FALSE);
3800     if (self->desktop != DESKTOP_ALL &&
3801         self->desktop != screen_desktop)
3802     {
3803         if (here)
3804             client_set_desktop(self, screen_desktop, FALSE, TRUE);
3805         else
3806             screen_set_desktop(self->desktop, FALSE);
3807     } else if (!render_plugin->frame_is_visible(self->frame))
3808         /* if its not visible for other reasons, then don't mess
3809            with it */
3810         return;
3811     if (self->shaded && unshade)
3812         client_shade(self, FALSE);
3813     if (raise)
3814         stacking_raise(CLIENT_AS_WINDOW(self));
3815
3816     client_focus(self);
3817 }
3818
3819 void client_activate(ObClient *self, gboolean here, gboolean raise,
3820                      gboolean unshade, gboolean user)
3821 {
3822     client_present(self, here, raise, unshade);
3823 }
3824
3825 static void client_bring_windows_recursive(ObClient *self,
3826                                            guint desktop,
3827                                            gboolean helpers,
3828                                            gboolean modals,
3829                                            gboolean iconic)
3830 {
3831     GSList *it;
3832
3833     for (it = self->transients; it; it = g_slist_next(it))
3834         client_bring_windows_recursive(it->data, desktop,
3835                                        helpers, modals, iconic);
3836
3837     if (((helpers && client_helper(self)) ||
3838          (modals && self->modal)) &&
3839         ((self->desktop != desktop && self->desktop != DESKTOP_ALL) ||
3840          (iconic && self->iconic)))
3841     {
3842         if (iconic && self->iconic)
3843             client_iconify(self, FALSE, TRUE, FALSE);
3844         else
3845             client_set_desktop(self, desktop, FALSE, FALSE);
3846     }
3847 }
3848
3849 void client_bring_helper_windows(ObClient *self)
3850 {
3851     client_bring_windows_recursive(self, self->desktop, TRUE, FALSE, FALSE);
3852 }
3853
3854 void client_bring_modal_windows(ObClient *self)
3855 {
3856     client_bring_windows_recursive(self, self->desktop, FALSE, TRUE, TRUE);
3857 }
3858
3859 gboolean client_focused(ObClient *self)
3860 {
3861     return self == focus_client;
3862 }
3863
3864 static ObClientIcon* client_icon_recursive(ObClient *self, gint w, gint h)
3865 {
3866     guint i;
3867     gulong min_diff, min_i;
3868
3869     if (!self->nicons) {
3870         ObClientIcon *parent = NULL;
3871         GSList *it;
3872
3873         for (it = self->parents; it; it = g_slist_next(it)) {
3874             ObClient *c = it->data;
3875             if ((parent = client_icon_recursive(c, w, h)))
3876                 break;
3877         }
3878
3879         return parent;
3880     }
3881
3882     /* some kind of crappy approximation to find the icon closest in size to
3883        what we requested, but icons are generally all the same ratio as
3884        eachother so it's good enough. */
3885
3886     min_diff = ABS(self->icons[0].width - w) + ABS(self->icons[0].height - h);
3887     min_i = 0;
3888
3889     for (i = 1; i < self->nicons; ++i) {
3890         gulong diff;
3891
3892         diff = ABS(self->icons[i].width - w) + ABS(self->icons[i].height - h);
3893         if (diff < min_diff) {
3894             min_diff = diff;
3895             min_i = i;
3896         }
3897     }
3898     return &self->icons[min_i];
3899 }
3900
3901 const ObClientIcon* client_icon(ObClient *self, gint w, gint h)
3902 {
3903     ObClientIcon *ret;
3904     static ObClientIcon deficon;
3905
3906     if (!(ret = client_icon_recursive(self, w, h))) {
3907         deficon.width = deficon.height = 48;
3908         deficon.data = ob_rr_theme->def_win_icon;
3909         ret = &deficon;
3910     }
3911     return ret;
3912 }
3913
3914 void client_set_layer(ObClient *self, gint layer)
3915 {
3916     if (layer < 0) {
3917         self->below = TRUE;
3918         self->above = FALSE;
3919     } else if (layer == 0) {
3920         self->below = self->above = FALSE;
3921     } else {
3922         self->below = FALSE;
3923         self->above = TRUE;
3924     }
3925     client_calc_layer(self);
3926     client_change_state(self); /* reflect this in the state hints */
3927 }
3928
3929 void client_set_undecorated(ObClient *self, gboolean undecorated)
3930 {
3931     if (self->undecorated != undecorated &&
3932         /* don't let it undecorate if the function is missing, but let
3933            it redecorate */
3934         (self->functions & OB_CLIENT_FUNC_UNDECORATE || !undecorated))
3935     {
3936         self->undecorated = undecorated;
3937         client_setup_decor_and_functions(self, TRUE);
3938         client_change_state(self); /* reflect this in the state hints */
3939     }
3940 }
3941
3942 guint client_monitor(ObClient *self)
3943 {
3944     Rect area = render_plugin->frame_get_window_area(self->frame);
3945     return screen_find_monitor(&area);
3946 }
3947
3948 ObClient *client_direct_parent(ObClient *self)
3949 {
3950     if (!self->parents) return NULL;
3951     if (self->transient_for_group) return NULL;
3952     return self->parents->data;
3953 }
3954
3955 ObClient *client_search_top_direct_parent(ObClient *self)
3956 {
3957     ObClient *p;
3958     while ((p = client_direct_parent(self))) self = p;
3959     return self;
3960 }
3961
3962 static GSList *client_search_all_top_parents_internal(ObClient *self,
3963                                                       gboolean bylayer,
3964                                                       ObStackingLayer layer)
3965 {
3966     GSList *ret;
3967     ObClient *p;
3968
3969     /* move up the direct transient chain as far as possible */
3970     while ((p = client_direct_parent(self)) &&
3971            (!bylayer || p->layer == layer))
3972         self = p;
3973
3974     if (!self->parents)
3975         ret = g_slist_prepend(NULL, self);
3976     else
3977         ret = g_slist_copy(self->parents);
3978
3979     return ret;
3980 }
3981
3982 GSList *client_search_all_top_parents(ObClient *self)
3983 {
3984     return client_search_all_top_parents_internal(self, FALSE, 0);
3985 }
3986
3987 GSList *client_search_all_top_parents_layer(ObClient *self)
3988 {
3989     return client_search_all_top_parents_internal(self, TRUE, self->layer);
3990 }
3991
3992 ObClient *client_search_focus_parent(ObClient *self)
3993 {
3994     GSList *it;
3995
3996     for (it = self->parents; it; it = g_slist_next(it))
3997         if (client_focused(it->data)) return it->data;
3998
3999     return NULL;
4000 }
4001
4002 ObClient *client_search_parent(ObClient *self, ObClient *search)
4003 {
4004     GSList *it;
4005
4006     for (it = self->parents; it; it = g_slist_next(it))
4007         if (it->data == search) return search;
4008
4009     return NULL;
4010 }
4011
4012 ObClient *client_search_transient(ObClient *self, ObClient *search)
4013 {
4014     GSList *sit;
4015
4016     for (sit = self->transients; sit; sit = g_slist_next(sit)) {
4017         if (sit->data == search)
4018             return search;
4019         if (client_search_transient(sit->data, search))
4020             return search;
4021     }
4022     return NULL;
4023 }
4024
4025 static void detect_edge(Rect area, ObDirection dir,
4026                         gint my_head, gint my_size,
4027                         gint my_edge_start, gint my_edge_size,
4028                         gint *dest, gboolean *near_edge)
4029 {
4030     gint edge_start, edge_size, head, tail;
4031     gboolean skip_head = FALSE, skip_tail = FALSE;
4032
4033     switch (dir) {
4034         case OB_DIRECTION_NORTH:
4035         case OB_DIRECTION_SOUTH:
4036             edge_start = area.x;
4037             edge_size = area.width;
4038             break;
4039         case OB_DIRECTION_EAST:
4040         case OB_DIRECTION_WEST:
4041             edge_start = area.y;
4042             edge_size = area.height;
4043             break;
4044         default:
4045             g_assert_not_reached();
4046     }
4047
4048     /* do we collide with this window? */
4049     if (!RANGES_INTERSECT(my_edge_start, my_edge_size,
4050                 edge_start, edge_size))
4051         return;
4052
4053     switch (dir) {
4054         case OB_DIRECTION_NORTH:
4055             head = RECT_BOTTOM(area);
4056             tail = RECT_TOP(area);
4057             break;
4058         case OB_DIRECTION_SOUTH:
4059             head = RECT_TOP(area);
4060             tail = RECT_BOTTOM(area);
4061             break;
4062         case OB_DIRECTION_WEST:
4063             head = RECT_RIGHT(area);
4064             tail = RECT_LEFT(area);
4065             break;
4066         case OB_DIRECTION_EAST:
4067             head = RECT_LEFT(area);
4068             tail = RECT_RIGHT(area);
4069             break;
4070         default:
4071             g_assert_not_reached();
4072     }
4073     switch (dir) {
4074         case OB_DIRECTION_NORTH:
4075         case OB_DIRECTION_WEST:
4076             /* check if our window is past the head of this window */
4077             if (my_head <= head + 1)
4078                 skip_head = TRUE;
4079             /* check if our window's tail is past the tail of this window */
4080             if (my_head + my_size - 1 <= tail)
4081                 skip_tail = TRUE;
4082             /* check if the head of this window is closer than the previously
4083                chosen edge (take into account that the previously chosen
4084                edge might have been a tail, not a head) */
4085             if (head + (*near_edge ? 0 : my_size) < *dest)
4086                 skip_head = TRUE;
4087             /* check if the tail of this window is closer than the previously
4088                chosen edge (take into account that the previously chosen
4089                edge might have been a head, not a tail) */
4090             if (tail - (!*near_edge ? 0 : my_size) < *dest)
4091                 skip_tail = TRUE;
4092             break;
4093         case OB_DIRECTION_SOUTH:
4094         case OB_DIRECTION_EAST:
4095             /* check if our window is past the head of this window */
4096             if (my_head >= head - 1)
4097                 skip_head = TRUE;
4098             /* check if our window's tail is past the tail of this window */
4099             if (my_head - my_size + 1 >= tail)
4100                 skip_tail = TRUE;
4101             /* check if the head of this window is closer than the previously
4102                chosen edge (take into account that the previously chosen
4103                edge might have been a tail, not a head) */
4104             if (head - (*near_edge ? 0 : my_size) > *dest)
4105                 skip_head = TRUE;
4106             /* check if the tail of this window is closer than the previously
4107                chosen edge (take into account that the previously chosen
4108                edge might have been a head, not a tail) */
4109             if (tail + (!*near_edge ? 0 : my_size) > *dest)
4110                 skip_tail = TRUE;
4111             break;
4112         default:
4113             g_assert_not_reached();
4114     }
4115
4116     ob_debug("my head %d size %d", my_head, my_size);
4117     ob_debug("head %d tail %d deest %d", head, tail, *dest);
4118     if (!skip_head) {
4119         ob_debug("using near edge %d", head);
4120         *dest = head;
4121         *near_edge = TRUE;
4122     }
4123     else if (!skip_tail) {
4124         ob_debug("using far edge %d", tail);
4125         *dest = tail;
4126         *near_edge = FALSE;
4127     }
4128 }
4129
4130 void client_find_edge_directional(ObClient *self, ObDirection dir,
4131                                   gint my_head, gint my_size,
4132                                   gint my_edge_start, gint my_edge_size,
4133                                   gint *dest, gboolean *near_edge)
4134 {
4135     GList *it;
4136     Rect *a, *mon;
4137     Rect dock_area;
4138     gint edge;
4139
4140     Strut size = render_plugin->frame_get_size(self->frame);
4141     Rect area = render_plugin->frame_get_window_area(self->frame);
4142
4143     a = screen_area(self->desktop, SCREEN_AREA_ALL_MONITORS,
4144                     &area);
4145     mon = screen_area(self->desktop, SCREEN_AREA_ONE_MONITOR,
4146                       &area);
4147
4148     switch (dir) {
4149     case OB_DIRECTION_NORTH:
4150         if (my_head >= RECT_TOP(*mon) + 1)
4151             edge = RECT_TOP(*mon) - 1;
4152         else
4153             edge = RECT_TOP(*a) - 1;
4154         break;
4155     case OB_DIRECTION_SOUTH:
4156         if (my_head <= RECT_BOTTOM(*mon) - 1)
4157             edge = RECT_BOTTOM(*mon) + 1;
4158         else
4159             edge = RECT_BOTTOM(*a) + 1;
4160         break;
4161     case OB_DIRECTION_EAST:
4162         if (my_head <= RECT_RIGHT(*mon) - 1)
4163             edge = RECT_RIGHT(*mon) + 1;
4164         else
4165             edge = RECT_RIGHT(*a) + 1;
4166         break;
4167     case OB_DIRECTION_WEST:
4168         if (my_head >= RECT_LEFT(*mon) + 1)
4169             edge = RECT_LEFT(*mon) - 1;
4170         else
4171             edge = RECT_LEFT(*a) - 1;
4172         break;
4173     default:
4174         g_assert_not_reached();
4175     }
4176     /* default to the far edge, then narrow it down */
4177     *dest = edge;
4178     *near_edge = TRUE;
4179
4180     for (it = client_list; it; it = g_list_next(it)) {
4181         ObClient *cur = it->data;
4182
4183         /* skip windows to not bump into */
4184         if (cur == self)
4185             continue;
4186         if (cur->iconic)
4187             continue;
4188         if (self->desktop != cur->desktop && cur->desktop != DESKTOP_ALL &&
4189             cur->desktop != screen_desktop)
4190             continue;
4191
4192         ob_debug("trying window %s", cur->title);
4193
4194         detect_edge(render_plugin->frame_get_window_area(cur->frame), dir, my_head, my_size, my_edge_start,
4195                     my_edge_size, dest, near_edge);
4196     }
4197     dock_get_area(&dock_area);
4198     detect_edge(dock_area, dir, my_head, my_size, my_edge_start,
4199                 my_edge_size, dest, near_edge);
4200     g_free(a);
4201     g_free(mon);
4202 }
4203
4204 void client_find_move_directional(ObClient *self, ObDirection dir,
4205                                   gint *x, gint *y)
4206 {
4207     gint head, size;
4208     gint e, e_start, e_size;
4209     gboolean near;
4210
4211
4212     Rect area = render_plugin->frame_get_window_area(self->frame);
4213
4214     switch (dir) {
4215     case OB_DIRECTION_EAST:
4216         head = RECT_RIGHT(area);
4217         size = area.width;
4218         e_start = RECT_TOP(area);
4219         e_size = area.height;
4220         break;
4221     case OB_DIRECTION_WEST:
4222         head = RECT_LEFT(area);
4223         size = area.width;
4224         e_start = RECT_TOP(area);
4225         e_size = area.height;
4226         break;
4227     case OB_DIRECTION_NORTH:
4228         head = RECT_TOP(area);
4229         size = area.height;
4230         e_start = RECT_LEFT(area);
4231         e_size = area.width;
4232         break;
4233     case OB_DIRECTION_SOUTH:
4234         head = RECT_BOTTOM(area);
4235         size = area.height;
4236         e_start = RECT_LEFT(area);
4237         e_size = area.width;
4238         break;
4239     default:
4240         g_assert_not_reached();
4241     }
4242
4243     client_find_edge_directional(self, dir, head, size,
4244                                  e_start, e_size, &e, &near);
4245     *x = area.x;
4246     *y = area.y;
4247     switch (dir) {
4248     case OB_DIRECTION_EAST:
4249         if (near) e -= area.width;
4250         else      e++;
4251         *x = e;
4252         break;
4253     case OB_DIRECTION_WEST:
4254         if (near) e++;
4255         else      e -= area.width;
4256         *x = e;
4257         break;
4258     case OB_DIRECTION_NORTH:
4259         if (near) e++;
4260         else      e -= area.height;
4261         *y = e;
4262         break;
4263     case OB_DIRECTION_SOUTH:
4264         if (near) e -= area.height;
4265         else      e++;
4266         *y = e;
4267         break;
4268     default:
4269         g_assert_not_reached();
4270     }
4271     frame_frame_gravity(self, x, y);
4272 }
4273
4274 void client_find_resize_directional(ObClient *self, ObDirection side,
4275                                     gboolean grow,
4276                                     gint *x, gint *y, gint *w, gint *h)
4277 {
4278     gint head;
4279     gint e, e_start, e_size, delta;
4280     gboolean near;
4281     ObDirection dir;
4282
4283     Rect area = render_plugin->frame_get_window_area(self->frame);
4284     Strut size = render_plugin->frame_get_size(self->frame);
4285
4286     switch (side) {
4287     case OB_DIRECTION_EAST:
4288         head = RECT_RIGHT(area) +
4289             (self->size_inc.width - 1) * (grow ? 1 : -1);
4290         e_start = RECT_TOP(area);
4291         e_size = area.height;
4292         dir = grow ? OB_DIRECTION_EAST : OB_DIRECTION_WEST;
4293         break;
4294     case OB_DIRECTION_WEST:
4295         head = RECT_LEFT(area) -
4296             (self->size_inc.width - 1) * (grow ? 1 : -1);
4297         e_start = RECT_TOP(area);
4298         e_size = area.height;
4299         dir = grow ? OB_DIRECTION_WEST : OB_DIRECTION_EAST;
4300         break;
4301     case OB_DIRECTION_NORTH:
4302         head = RECT_TOP(area) -
4303             (self->size_inc.height - 1) * (grow ? 1 : -1);
4304         e_start = RECT_LEFT(area);
4305         e_size = area.width;
4306         dir = grow ? OB_DIRECTION_NORTH : OB_DIRECTION_SOUTH;
4307         break;
4308     case OB_DIRECTION_SOUTH:
4309         head = RECT_BOTTOM(area) +
4310             (self->size_inc.height - 1) * (grow ? 1 : -1);
4311         e_start = RECT_LEFT(area);
4312         e_size = area.width;
4313         dir = grow ? OB_DIRECTION_SOUTH : OB_DIRECTION_NORTH;
4314         break;
4315     default:
4316         g_assert_not_reached();
4317     }
4318
4319     ob_debug("head %d dir %d", head, dir);
4320     client_find_edge_directional(self, dir, head, 1,
4321                                  e_start, e_size, &e, &near);
4322     ob_debug("edge %d", e);
4323     *x = area.x;
4324     *y = area.y;
4325     *w = area.width;
4326     *h = area.height;
4327     switch (side) {
4328     case OB_DIRECTION_EAST:
4329         if (grow == near) --e;
4330         delta = e - RECT_RIGHT(area);
4331         *w += delta;
4332         break;
4333     case OB_DIRECTION_WEST:
4334         if (grow == near) ++e;
4335         delta = RECT_LEFT(area) - e;
4336         *x -= delta;
4337         *w += delta;
4338         break;
4339     case OB_DIRECTION_NORTH:
4340         if (grow == near) ++e;
4341         delta = RECT_TOP(area) - e;
4342         *y -= delta;
4343         *h += delta;
4344         break;
4345     case OB_DIRECTION_SOUTH:
4346         if (grow == near) --e;
4347         delta = e - RECT_BOTTOM(area);
4348         *h += delta;
4349         break;
4350     default:
4351         g_assert_not_reached();
4352     }
4353     frame_frame_gravity(self, x, y);
4354     *w -= size.left + size.right;
4355     *h -= size.top + size.bottom;
4356 }
4357
4358 ObClient* client_under_pointer(void)
4359 {
4360     gint x, y;
4361     GList *it;
4362     ObClient *ret = NULL;
4363
4364     if (screen_pointer_pos(&x, &y)) {
4365         for (it = stacking_list; it; it = g_list_next(it)) {
4366             if (WINDOW_IS_CLIENT(it->data)) {
4367                 ObClient *c = WINDOW_AS_CLIENT(it->data);
4368                 if (render_plugin->frame_is_visible(c->frame) &&
4369                     /* check the desktop, this is done during desktop
4370                        switching and windows are shown/hidden status is not
4371                        reliable */
4372                     (c->desktop == screen_desktop ||
4373                      c->desktop == DESKTOP_ALL) &&
4374                     /* ignore all animating windows */
4375                     !(render_plugin->frame_iconify_animating(c->frame)) &&
4376                     RECT_CONTAINS(render_plugin->frame_get_window_area(c->frame), x, y))
4377                 {
4378                     ret = c;
4379                     break;
4380                 }
4381             }
4382         }
4383     }
4384     return ret;
4385 }
4386
4387 gboolean client_has_group_siblings(ObClient *self)
4388 {
4389     return self->group && self->group->members->next;
4390 }