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