]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/client.c
Set a prop on locked windows, so panels can see it, and so it persists over restarts.
[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             else if (state[i] == OBT_PROP_ATOM(OB_WM_STATE_LOCKED))
1377                 self->locked = TRUE;
1378         }
1379
1380         g_free(state);
1381     }
1382 }
1383
1384 static void client_get_shaped(ObClient *self)
1385 {
1386     self->shaped = FALSE;
1387 #ifdef SHAPE
1388     if (obt_display_extension_shape) {
1389         gint foo;
1390         guint ufoo;
1391         gint s;
1392
1393         XShapeSelectInput(obt_display, self->window, ShapeNotifyMask);
1394
1395         XShapeQueryExtents(obt_display, self->window, &s, &foo,
1396                            &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
1397                            &ufoo);
1398         self->shaped = !!s;
1399     }
1400 #endif
1401 }
1402
1403 void client_update_transient_for(ObClient *self)
1404 {
1405     Window t = None;
1406     ObClient *target = NULL;
1407     gboolean trangroup = FALSE;
1408
1409     if (XGetTransientForHint(obt_display, self->window, &t)) {
1410         if (t != self->window) { /* can't be transient to itself! */
1411             ObWindow *tw = window_find(t);
1412             /* if this happens then we need to check for it */
1413             g_assert(tw != CLIENT_AS_WINDOW(self));
1414             if (tw && WINDOW_IS_CLIENT(tw)) {
1415                 /* watch out for windows with a parent that is something
1416                    different, like a dockapp for example */
1417                 target = WINDOW_AS_CLIENT(tw);
1418             }
1419         }
1420
1421         /* Setting the transient_for to Root is actually illegal, however
1422            applications from time have done this to specify transient for
1423            their group */
1424         if (!target && self->group && t == obt_root(ob_screen))
1425             trangroup = TRUE;
1426     } else if (self->group && self->transient)
1427         trangroup = TRUE;
1428
1429     client_update_transient_tree(self, self->group, self->group,
1430                                  self->transient_for_group, trangroup,
1431                                  client_direct_parent(self), target);
1432     self->transient_for_group = trangroup;
1433
1434 }
1435
1436 static void client_update_transient_tree(ObClient *self,
1437                                          ObGroup *oldgroup, ObGroup *newgroup,
1438                                          gboolean oldgtran, gboolean newgtran,
1439                                          ObClient* oldparent,
1440                                          ObClient *newparent)
1441 {
1442     GSList *it, *next;
1443     ObClient *c;
1444
1445     g_assert(!oldgtran || oldgroup);
1446     g_assert(!newgtran || newgroup);
1447     g_assert((!oldgtran && !oldparent) ||
1448              (oldgtran && !oldparent) ||
1449              (!oldgtran && oldparent));
1450     g_assert((!newgtran && !newparent) ||
1451              (newgtran && !newparent) ||
1452              (!newgtran && newparent));
1453
1454     /* * *
1455       Group transient windows are not allowed to have other group
1456       transient windows as their children.
1457       * * */
1458
1459     /* No change has occured */
1460     if (oldgroup == newgroup &&
1461         oldgtran == newgtran &&
1462         oldparent == newparent) return;
1463
1464     /** Remove the client from the transient tree **/
1465
1466     for (it = self->transients; it; it = next) {
1467         next = g_slist_next(it);
1468         c = it->data;
1469         self->transients = g_slist_delete_link(self->transients, it);
1470         c->parents = g_slist_remove(c->parents, self);
1471     }
1472     for (it = self->parents; it; it = next) {
1473         next = g_slist_next(it);
1474         c = it->data;
1475         self->parents = g_slist_delete_link(self->parents, it);
1476         c->transients = g_slist_remove(c->transients, self);
1477     }
1478
1479     /** Re-add the client to the transient tree **/
1480
1481     /* If we're transient for a group then we need to add ourselves to all our
1482        parents */
1483     if (newgtran) {
1484         for (it = newgroup->members; it; it = g_slist_next(it)) {
1485             c = it->data;
1486             if (c != self &&
1487                 !client_search_top_direct_parent(c)->transient_for_group &&
1488                 client_normal(c))
1489             {
1490                 c->transients = g_slist_prepend(c->transients, self);
1491                 self->parents = g_slist_prepend(self->parents, c);
1492             }
1493         }
1494     }
1495
1496     /* If we are now transient for a single window we need to add ourselves to
1497        its children
1498
1499        WARNING: Cyclical transient-ness is possible if two windows are
1500        transient for eachother.
1501     */
1502     else if (newparent &&
1503              /* don't make ourself its child if it is already our child */
1504              !client_is_direct_child(self, newparent) &&
1505              client_normal(newparent))
1506     {
1507         newparent->transients = g_slist_prepend(newparent->transients, self);
1508         self->parents = g_slist_prepend(self->parents, newparent);
1509     }
1510
1511     /* Add any group transient windows to our children. But if we're transient
1512        for the group, then other group transients are not our children.
1513
1514        WARNING: Cyclical transient-ness is possible. For e.g. if:
1515        A is transient for the group
1516        B is transient for A
1517        C is transient for B
1518        A can't be transient for C or we have a cycle
1519     */
1520     if (!newgtran && newgroup &&
1521         (!newparent ||
1522          !client_search_top_direct_parent(newparent)->transient_for_group) &&
1523         client_normal(self))
1524     {
1525         for (it = newgroup->members; it; it = g_slist_next(it)) {
1526             c = it->data;
1527             if (c != self && c->transient_for_group &&
1528                 /* Don't make it our child if it is already our parent */
1529                 !client_is_direct_child(c, self))
1530             {
1531                 self->transients = g_slist_prepend(self->transients, c);
1532                 c->parents = g_slist_prepend(c->parents, self);
1533             }
1534         }
1535     }
1536
1537     /** If we change our group transient-ness, our children change their
1538         effective group transient-ness, which affects how they relate to other
1539         group windows **/
1540
1541     for (it = self->transients; it; it = g_slist_next(it)) {
1542         c = it->data;
1543         if (!c->transient_for_group)
1544             client_update_transient_tree(c, c->group, c->group,
1545                                          c->transient_for_group,
1546                                          c->transient_for_group,
1547                                          client_direct_parent(c),
1548                                          client_direct_parent(c));
1549     }
1550 }
1551
1552 void client_get_mwm_hints(ObClient *self)
1553 {
1554     guint num;
1555     guint32 *hints;
1556
1557     self->mwmhints.flags = 0; /* default to none */
1558
1559     if (OBT_PROP_GETA32(self->window, MOTIF_WM_HINTS, MOTIF_WM_HINTS,
1560                         &hints, &num)) {
1561         if (num >= OB_MWM_ELEMENTS) {
1562             self->mwmhints.flags = hints[0];
1563             self->mwmhints.functions = hints[1];
1564             self->mwmhints.decorations = hints[2];
1565         }
1566         g_free(hints);
1567     }
1568 }
1569
1570 void client_get_type_and_transientness(ObClient *self)
1571 {
1572     guint num, i;
1573     guint32 *val;
1574     Window t;
1575
1576     self->type = -1;
1577     self->transient = FALSE;
1578
1579     if (OBT_PROP_GETA32(self->window, NET_WM_WINDOW_TYPE, ATOM, &val, &num)) {
1580         /* use the first value that we know about in the array */
1581         for (i = 0; i < num; ++i) {
1582             if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DESKTOP))
1583                 self->type = OB_CLIENT_TYPE_DESKTOP;
1584             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DOCK))
1585                 self->type = OB_CLIENT_TYPE_DOCK;
1586             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_TOOLBAR))
1587                 self->type = OB_CLIENT_TYPE_TOOLBAR;
1588             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_MENU))
1589                 self->type = OB_CLIENT_TYPE_MENU;
1590             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_UTILITY))
1591                 self->type = OB_CLIENT_TYPE_UTILITY;
1592             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_SPLASH))
1593                 self->type = OB_CLIENT_TYPE_SPLASH;
1594             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DIALOG))
1595                 self->type = OB_CLIENT_TYPE_DIALOG;
1596             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_NORMAL))
1597                 self->type = OB_CLIENT_TYPE_NORMAL;
1598             else if (val[i] == OBT_PROP_ATOM(KDE_NET_WM_WINDOW_TYPE_OVERRIDE))
1599             {
1600                 /* prevent this window from getting any decor or
1601                    functionality */
1602                 self->mwmhints.flags &= (OB_MWM_FLAG_FUNCTIONS |
1603                                          OB_MWM_FLAG_DECORATIONS);
1604                 self->mwmhints.decorations = 0;
1605                 self->mwmhints.functions = 0;
1606             }
1607             if (self->type != (ObClientType) -1)
1608                 break; /* grab the first legit type */
1609         }
1610         g_free(val);
1611     }
1612
1613     if (XGetTransientForHint(obt_display, self->window, &t))
1614         self->transient = TRUE;
1615
1616     if (self->type == (ObClientType) -1) {
1617         /*the window type hint was not set, which means we either classify
1618           ourself as a normal window or a dialog, depending on if we are a
1619           transient. */
1620         if (self->transient)
1621             self->type = OB_CLIENT_TYPE_DIALOG;
1622         else
1623             self->type = OB_CLIENT_TYPE_NORMAL;
1624     }
1625
1626     /* then, based on our type, we can update our transientness.. */
1627     if (self->type == OB_CLIENT_TYPE_DIALOG ||
1628         self->type == OB_CLIENT_TYPE_TOOLBAR ||
1629         self->type == OB_CLIENT_TYPE_MENU ||
1630         self->type == OB_CLIENT_TYPE_UTILITY)
1631     {
1632         self->transient = TRUE;
1633     }
1634 }
1635
1636 void client_update_protocols(ObClient *self)
1637 {
1638     guint32 *proto;
1639     guint num_ret, i;
1640
1641     self->focus_notify = FALSE;
1642     self->delete_window = FALSE;
1643
1644     if (OBT_PROP_GETA32(self->window, WM_PROTOCOLS, ATOM, &proto, &num_ret)) {
1645         for (i = 0; i < num_ret; ++i) {
1646             if (proto[i] == OBT_PROP_ATOM(WM_DELETE_WINDOW))
1647                 /* this means we can request the window to close */
1648                 self->delete_window = TRUE;
1649             else if (proto[i] == OBT_PROP_ATOM(WM_TAKE_FOCUS))
1650                 /* if this protocol is requested, then the window will be
1651                    notified whenever we want it to receive focus */
1652                 self->focus_notify = TRUE;
1653             else if (proto[i] == OBT_PROP_ATOM(NET_WM_PING))
1654                 /* if this protocol is requested, then the window will allow
1655                    pings to determine if it is still alive */
1656                 self->ping = TRUE;
1657 #ifdef SYNC
1658             else if (proto[i] == OBT_PROP_ATOM(NET_WM_SYNC_REQUEST))
1659                 /* if this protocol is requested, then resizing the
1660                    window will be synchronized between the frame and the
1661                    client */
1662                 self->sync_request = TRUE;
1663 #endif
1664         }
1665         g_free(proto);
1666     }
1667 }
1668
1669 #ifdef SYNC
1670 void client_update_sync_request_counter(ObClient *self)
1671 {
1672     guint32 i;
1673
1674     if (OBT_PROP_GET32(self->window, NET_WM_SYNC_REQUEST_COUNTER, CARDINAL,&i))
1675     {
1676         XSyncValue val;
1677
1678         self->sync_counter = i;
1679
1680         /* this must be set when managing a new window according to EWMH */
1681         XSyncIntToValue(&val, 0);
1682         XSyncSetCounter(obt_display, self->sync_counter, val);
1683     } else
1684         self->sync_counter = None;
1685 }
1686 #endif
1687
1688 static void client_get_colormap(ObClient *self)
1689 {
1690     XWindowAttributes wa;
1691
1692     if (XGetWindowAttributes(obt_display, self->window, &wa))
1693         client_update_colormap(self, wa.colormap);
1694 }
1695
1696 void client_update_colormap(ObClient *self, Colormap colormap)
1697 {
1698     if (colormap == self->colormap) return;
1699
1700     ob_debug("Setting client %s colormap: 0x%x", self->title, colormap);
1701
1702     if (client_focused(self)) {
1703         screen_install_colormap(self, FALSE); /* uninstall old one */
1704         self->colormap = colormap;
1705         screen_install_colormap(self, TRUE); /* install new one */
1706     } else
1707         self->colormap = colormap;
1708 }
1709
1710 void client_update_opacity(ObClient *self)
1711 {
1712     guint32 o;
1713
1714     if (OBT_PROP_GET32(self->window, NET_WM_WINDOW_OPACITY, CARDINAL, &o))
1715         OBT_PROP_SET32(self->frame->window, NET_WM_WINDOW_OPACITY, CARDINAL, o);
1716     else
1717         OBT_PROP_ERASE(self->frame->window, NET_WM_WINDOW_OPACITY);
1718 }
1719
1720 void client_update_normal_hints(ObClient *self)
1721 {
1722     XSizeHints size;
1723     glong ret;
1724
1725     /* defaults */
1726     self->min_ratio = 0.0f;
1727     self->max_ratio = 0.0f;
1728     SIZE_SET(self->size_inc, 1, 1);
1729     SIZE_SET(self->base_size, -1, -1);
1730     SIZE_SET(self->min_size, 0, 0);
1731     SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
1732
1733     /* get the hints from the window */
1734     if (XGetWMNormalHints(obt_display, self->window, &size, &ret)) {
1735         /* normal windows can't request placement! har har
1736         if (!client_normal(self))
1737         */
1738         self->positioned = (size.flags & (PPosition|USPosition));
1739         self->sized = (size.flags & (PSize|USSize));
1740
1741         if (size.flags & PWinGravity)
1742             self->gravity = size.win_gravity;
1743
1744         if (size.flags & PAspect) {
1745             if (size.min_aspect.y)
1746                 self->min_ratio =
1747                     (gfloat) size.min_aspect.x / size.min_aspect.y;
1748             if (size.max_aspect.y)
1749                 self->max_ratio =
1750                     (gfloat) size.max_aspect.x / size.max_aspect.y;
1751         }
1752
1753         if (size.flags & PMinSize)
1754             SIZE_SET(self->min_size, size.min_width, size.min_height);
1755
1756         if (size.flags & PMaxSize)
1757             SIZE_SET(self->max_size, size.max_width, size.max_height);
1758
1759         if (size.flags & PBaseSize)
1760             SIZE_SET(self->base_size, size.base_width, size.base_height);
1761
1762         if (size.flags & PResizeInc && size.width_inc && size.height_inc)
1763             SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
1764
1765         ob_debug("Normal hints: min size (%d %d) max size (%d %d)",
1766                  self->min_size.width, self->min_size.height,
1767                  self->max_size.width, self->max_size.height);
1768         ob_debug("size inc (%d %d) base size (%d %d)",
1769                  self->size_inc.width, self->size_inc.height,
1770                  self->base_size.width, self->base_size.height);
1771     }
1772     else
1773         ob_debug("Normal hints: not set");
1774 }
1775
1776 static void client_setup_default_decor_and_functions(ObClient *self)
1777 {
1778     /* start with everything (cept fullscreen) */
1779     self->decorations =
1780         (OB_FRAME_DECOR_TITLEBAR |
1781          OB_FRAME_DECOR_HANDLE |
1782          OB_FRAME_DECOR_GRIPS |
1783          OB_FRAME_DECOR_BORDER |
1784          OB_FRAME_DECOR_ICON |
1785          OB_FRAME_DECOR_ALLDESKTOPS |
1786          OB_FRAME_DECOR_ICONIFY |
1787          OB_FRAME_DECOR_MAXIMIZE |
1788          OB_FRAME_DECOR_SHADE |
1789          OB_FRAME_DECOR_CLOSE);
1790     self->functions =
1791         (OB_CLIENT_FUNC_RESIZE |
1792          OB_CLIENT_FUNC_MOVE |
1793          OB_CLIENT_FUNC_ICONIFY |
1794          OB_CLIENT_FUNC_MAXIMIZE |
1795          OB_CLIENT_FUNC_SHADE |
1796          OB_CLIENT_FUNC_CLOSE |
1797          OB_CLIENT_FUNC_BELOW |
1798          OB_CLIENT_FUNC_ABOVE |
1799          OB_CLIENT_FUNC_UNDECORATE);
1800
1801     if (!(self->min_size.width < self->max_size.width ||
1802           self->min_size.height < self->max_size.height))
1803         self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1804
1805     switch (self->type) {
1806     case OB_CLIENT_TYPE_NORMAL:
1807         /* normal windows retain all of the possible decorations and
1808            functionality, and can be fullscreen */
1809         self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1810         break;
1811
1812     case OB_CLIENT_TYPE_DIALOG:
1813         /* sometimes apps make dialog windows fullscreen for some reason (for
1814            e.g. kpdf does this..) */
1815         self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1816         break;
1817
1818     case OB_CLIENT_TYPE_UTILITY:
1819         /* these windows don't have anything added or removed by default */
1820         break;
1821
1822     case OB_CLIENT_TYPE_MENU:
1823     case OB_CLIENT_TYPE_TOOLBAR:
1824         /* these windows can't iconify or maximize */
1825         self->decorations &= ~(OB_FRAME_DECOR_ICONIFY |
1826                                OB_FRAME_DECOR_MAXIMIZE);
1827         self->functions &= ~(OB_CLIENT_FUNC_ICONIFY |
1828                              OB_CLIENT_FUNC_MAXIMIZE);
1829         break;
1830
1831     case OB_CLIENT_TYPE_SPLASH:
1832         /* these don't get get any decorations, and the only thing you can
1833            do with them is move them */
1834         self->decorations = 0;
1835         self->functions = OB_CLIENT_FUNC_MOVE;
1836         break;
1837
1838     case OB_CLIENT_TYPE_DESKTOP:
1839         /* these windows are not manipulated by the window manager */
1840         self->decorations = 0;
1841         self->functions = 0;
1842         break;
1843
1844     case OB_CLIENT_TYPE_DOCK:
1845         /* these windows are not manipulated by the window manager, but they
1846            can set below layer which has a special meaning */
1847         self->decorations = 0;
1848         self->functions = OB_CLIENT_FUNC_BELOW;
1849         break;
1850     }
1851
1852     /* If the client has no decor from its type (which never changes) then
1853        don't allow the user to "undecorate" the window.  Otherwise, allow them
1854        to, even if there are motif hints removing the decor, because those
1855        may change these days (e.g. chromium) */
1856     if (self->decorations == 0)
1857         self->functions &= ~OB_CLIENT_FUNC_UNDECORATE;
1858
1859     /* Mwm Hints are applied subtractively to what has already been chosen for
1860        decor and functionality */
1861     if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
1862         if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
1863             if (! ((self->mwmhints.decorations & OB_MWM_DECOR_HANDLE) ||
1864                    (self->mwmhints.decorations & OB_MWM_DECOR_TITLE)))
1865             {
1866                 /* if the mwm hints request no handle or title, then all
1867                    decorations are disabled, but keep the border if that's
1868                    specified */
1869                 if (self->mwmhints.decorations & OB_MWM_DECOR_BORDER)
1870                     self->decorations = OB_FRAME_DECOR_BORDER;
1871                 else
1872                     self->decorations = 0;
1873             }
1874         }
1875     }
1876
1877     if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
1878         if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
1879             if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
1880                 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1881             if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
1882                 self->functions &= ~OB_CLIENT_FUNC_MOVE;
1883             /* dont let mwm hints kill any buttons
1884                if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
1885                self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
1886                if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
1887                self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1888             */
1889             /* dont let mwm hints kill the close button
1890                if (! (self->mwmhints.functions & MwmFunc_Close))
1891                self->functions &= ~OB_CLIENT_FUNC_CLOSE; */
1892         }
1893     }
1894
1895     if (!(self->functions & OB_CLIENT_FUNC_SHADE))
1896         self->decorations &= ~OB_FRAME_DECOR_SHADE;
1897     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY))
1898         self->decorations &= ~OB_FRAME_DECOR_ICONIFY;
1899     if (!(self->functions & OB_CLIENT_FUNC_RESIZE))
1900         self->decorations &= ~(OB_FRAME_DECOR_GRIPS | OB_FRAME_DECOR_HANDLE);
1901
1902     /* can't maximize without moving/resizing */
1903     if (!((self->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
1904           (self->functions & OB_CLIENT_FUNC_MOVE) &&
1905           (self->functions & OB_CLIENT_FUNC_RESIZE))) {
1906         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1907         self->decorations &= ~OB_FRAME_DECOR_MAXIMIZE;
1908     }
1909 }
1910
1911 /*! Set up decor for a client based on its undecorated state. */
1912 static void client_setup_decor_undecorated(ObClient *self)
1913 {
1914     /* If the user requested no decorations, then remove all the decorations,
1915        except the border.  But don't add a border if there wasn't one. */
1916     if (self->undecorated)
1917         self->decorations &= (config_theme_keepborder ?
1918                               OB_FRAME_DECOR_BORDER : 0);
1919 }
1920
1921 void client_setup_decor_and_functions(ObClient *self, gboolean reconfig)
1922 {
1923     client_setup_default_decor_and_functions(self);
1924
1925     client_setup_decor_undecorated(self);
1926
1927     if (self->max_horz && self->max_vert) {
1928         /* once upon a time you couldn't resize maximized windows, that is not
1929            the case any more though !
1930
1931            but do kill the handle on fully maxed windows */
1932         self->decorations &= ~(OB_FRAME_DECOR_HANDLE | OB_FRAME_DECOR_GRIPS);
1933     }
1934
1935     /* if we don't have a titlebar, then we cannot shade! */
1936     if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1937         self->functions &= ~OB_CLIENT_FUNC_SHADE;
1938
1939     /* now we need to check against rules for the client's current state */
1940     if (self->fullscreen) {
1941         self->functions &= (OB_CLIENT_FUNC_CLOSE |
1942                             OB_CLIENT_FUNC_FULLSCREEN |
1943                             OB_CLIENT_FUNC_ICONIFY);
1944         self->decorations = 0;
1945     }
1946
1947     client_change_allowed_actions(self);
1948
1949     if (reconfig)
1950         /* reconfigure to make sure decorations are updated */
1951         client_reconfigure(self, FALSE);
1952 }
1953
1954 static void client_change_allowed_actions(ObClient *self)
1955 {
1956     gulong actions[12];
1957     gint num = 0;
1958
1959     /* desktop windows are kept on all desktops */
1960     if (self->type != OB_CLIENT_TYPE_DESKTOP)
1961         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_CHANGE_DESKTOP);
1962
1963     if (self->functions & OB_CLIENT_FUNC_SHADE)
1964         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_SHADE);
1965     if (self->functions & OB_CLIENT_FUNC_CLOSE)
1966         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_CLOSE);
1967     if (self->functions & OB_CLIENT_FUNC_MOVE)
1968         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_MOVE);
1969     if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1970         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_MINIMIZE);
1971     if (self->functions & OB_CLIENT_FUNC_RESIZE)
1972         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_RESIZE);
1973     if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1974         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_FULLSCREEN);
1975     if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1976         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_MAXIMIZE_HORZ);
1977         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_MAXIMIZE_VERT);
1978     }
1979     if (self->functions & OB_CLIENT_FUNC_ABOVE)
1980         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_ABOVE);
1981     if (self->functions & OB_CLIENT_FUNC_BELOW)
1982         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_BELOW);
1983     if (self->functions & OB_CLIENT_FUNC_UNDECORATE)
1984         actions[num++] = OBT_PROP_ATOM(OB_WM_ACTION_UNDECORATE);
1985
1986     OBT_PROP_SETA32(self->window, NET_WM_ALLOWED_ACTIONS, ATOM, actions, num);
1987
1988     /* make sure the window isn't breaking any rules now
1989
1990        don't check ICONIFY here.  just cuz a window can't iconify doesnt mean
1991        it can't be iconified with its parent
1992     */
1993
1994     if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1995         if (self->frame) client_shade(self, FALSE);
1996         else self->shaded = FALSE;
1997     }
1998     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1999         if (self->frame) client_fullscreen(self, FALSE);
2000         else self->fullscreen = FALSE;
2001     }
2002     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
2003                                                          self->max_vert)) {
2004         if (self->frame) client_maximize(self, FALSE, 0);
2005         else self->max_vert = self->max_horz = FALSE;
2006     }
2007 }
2008
2009 void client_update_wmhints(ObClient *self)
2010 {
2011     XWMHints *hints;
2012
2013     /* assume a window takes input if it doesn't specify */
2014     self->can_focus = TRUE;
2015
2016     if ((hints = XGetWMHints(obt_display, self->window)) != NULL) {
2017         gboolean ur;
2018
2019         if (hints->flags & InputHint)
2020             self->can_focus = hints->input;
2021
2022         /* only do this when first managing the window *AND* when we aren't
2023            starting up! */
2024         if (ob_state() != OB_STATE_STARTING && self->frame == NULL)
2025             if (hints->flags & StateHint)
2026                 self->iconic = hints->initial_state == IconicState;
2027
2028         ur = self->urgent;
2029         self->urgent = (hints->flags & XUrgencyHint);
2030         if (self->urgent && !ur)
2031             client_hilite(self, TRUE);
2032         else if (!self->urgent && ur && self->demands_attention)
2033             client_hilite(self, FALSE);
2034
2035         if (!(hints->flags & WindowGroupHint))
2036             hints->window_group = None;
2037
2038         /* did the group state change? */
2039         if (hints->window_group !=
2040             (self->group ? self->group->leader : None))
2041         {
2042             ObGroup *oldgroup = self->group;
2043
2044             /* remove from the old group if there was one */
2045             if (self->group) {
2046                 group_remove(self->group, self);
2047                 self->group = NULL;
2048             }
2049
2050             /* add ourself to the group if we have one */
2051             if (hints->window_group != None) {
2052                 self->group = group_add(hints->window_group, self);
2053             }
2054
2055             /* Put ourselves into the new group's transient tree, and remove
2056                ourselves from the old group's */
2057             client_update_transient_tree(self, oldgroup, self->group,
2058                                          self->transient_for_group,
2059                                          self->transient_for_group,
2060                                          client_direct_parent(self),
2061                                          client_direct_parent(self));
2062
2063             /* Lastly, being in a group, or not, can change if the window is
2064                transient for anything.
2065
2066                The logic for this is:
2067                self->transient = TRUE always if the window wants to be
2068                transient for something, even if transient_for was NULL because
2069                it wasn't in a group before.
2070
2071                If parents was NULL and oldgroup was NULL we can assume
2072                that when we add the new group, it will become transient for
2073                something.
2074
2075                If transient_for_group is TRUE, then it must have already
2076                had a group. If it is getting a new group, the above call to
2077                client_update_transient_tree has already taken care of
2078                everything ! If it is losing all group status then it will
2079                no longer be transient for anything and that needs to be
2080                updated.
2081             */
2082             if (self->transient &&
2083                 ((self->parents == NULL && oldgroup == NULL) ||
2084                  (self->transient_for_group && !self->group)))
2085                 client_update_transient_for(self);
2086         }
2087
2088         /* the WM_HINTS can contain an icon */
2089         if (hints->flags & IconPixmapHint)
2090             client_update_icons(self);
2091
2092         XFree(hints);
2093     }
2094
2095     focus_cycle_addremove(self, TRUE);
2096 }
2097
2098 void client_update_title(ObClient *self)
2099 {
2100     gchar *data = NULL;
2101     gchar *visible = NULL;
2102
2103     g_free(self->title);
2104     g_free(self->original_title);
2105
2106     /* try netwm */
2107     if (!OBT_PROP_GETS_UTF8(self->window, NET_WM_NAME, &data)) {
2108         /* try old x stuff */
2109         if (!OBT_PROP_GETS(self->window, WM_NAME, &data)) {
2110             if (self->transient) {
2111    /*
2112    GNOME alert windows are not given titles:
2113    http://developer.gnome.org/projects/gup/hig/draft_hig_new/windows-alert.html
2114    */
2115                 data = g_strdup("");
2116             } else
2117                 data = g_strdup(_("Unnamed Window"));
2118         }
2119     }
2120     self->original_title = g_strdup(data);
2121
2122     if (self->client_machine) {
2123         visible = g_strdup_printf("%s (%s)", data, self->client_machine);
2124         g_free(data);
2125     } else
2126         visible = data;
2127
2128     if (self->not_responding) {
2129         data = visible;
2130         if (self->kill_level > 0)
2131             visible = g_strdup_printf("%s - [%s]", data, _("Killing..."));
2132         else
2133             visible = g_strdup_printf("%s - [%s]", data, _("Not Responding"));
2134         g_free(data);
2135     }
2136
2137     OBT_PROP_SETS(self->window, NET_WM_VISIBLE_NAME, visible);
2138     self->title = visible;
2139
2140     if (self->frame)
2141         frame_adjust_title(self->frame);
2142
2143     /* update the icon title */
2144     data = NULL;
2145     g_free(self->icon_title);
2146
2147     /* try netwm */
2148     if (!OBT_PROP_GETS_UTF8(self->window, NET_WM_ICON_NAME, &data))
2149         /* try old x stuff */
2150         if (!OBT_PROP_GETS(self->window, WM_ICON_NAME, &data))
2151             data = g_strdup(self->title);
2152
2153     if (self->client_machine) {
2154         visible = g_strdup_printf("%s (%s)", data, self->client_machine);
2155         g_free(data);
2156     } else
2157         visible = data;
2158
2159     if (self->not_responding) {
2160         data = visible;
2161         if (self->kill_level > 0)
2162             visible = g_strdup_printf("%s - [%s]", data, _("Killing..."));
2163         else
2164             visible = g_strdup_printf("%s - [%s]", data, _("Not Responding"));
2165         g_free(data);
2166     }
2167
2168     OBT_PROP_SETS(self->window, NET_WM_VISIBLE_ICON_NAME, visible);
2169     self->icon_title = visible;
2170 }
2171
2172 void client_update_strut(ObClient *self)
2173 {
2174     guint num;
2175     guint32 *data;
2176     gboolean got = FALSE;
2177     StrutPartial strut;
2178
2179     if (OBT_PROP_GETA32(self->window, NET_WM_STRUT_PARTIAL, CARDINAL,
2180                         &data, &num))
2181     {
2182         if (num == 12) {
2183             got = TRUE;
2184             STRUT_PARTIAL_SET(strut,
2185                               data[0], data[2], data[1], data[3],
2186                               data[4], data[5], data[8], data[9],
2187                               data[6], data[7], data[10], data[11]);
2188         }
2189         g_free(data);
2190     }
2191
2192     if (!got &&
2193         OBT_PROP_GETA32(self->window, NET_WM_STRUT, CARDINAL, &data, &num)) {
2194         if (num == 4) {
2195             const Rect *a;
2196
2197             got = TRUE;
2198
2199             /* use the screen's width/height */
2200             a = screen_physical_area_all_monitors();
2201
2202             STRUT_PARTIAL_SET(strut,
2203                               data[0], data[2], data[1], data[3],
2204                               a->y, a->y + a->height - 1,
2205                               a->x, a->x + a->width - 1,
2206                               a->y, a->y + a->height - 1,
2207                               a->x, a->x + a->width - 1);
2208         }
2209         g_free(data);
2210     }
2211
2212     if (!got)
2213         STRUT_PARTIAL_SET(strut, 0, 0, 0, 0,
2214                           0, 0, 0, 0, 0, 0, 0, 0);
2215
2216     if (!PARTIAL_STRUT_EQUAL(strut, self->strut)) {
2217         self->strut = strut;
2218
2219         /* updating here is pointless while we're being mapped cuz we're not in
2220            the client list yet */
2221         if (self->frame)
2222             screen_update_areas();
2223     }
2224 }
2225
2226 void client_update_icons(ObClient *self)
2227 {
2228     guint num;
2229     guint32 *data;
2230     guint w, h, i, j;
2231     RrImage *img;
2232
2233     img = NULL;
2234
2235     /* grab the server, because we might be setting the window's icon and
2236        we don't want them to set it in between and we overwrite their own
2237        icon */
2238     grab_server(TRUE);
2239
2240     if (OBT_PROP_GETA32(self->window, NET_WM_ICON, CARDINAL, &data, &num)) {
2241         /* figure out how many valid icons are in here */
2242         i = 0;
2243         while (i + 2 < num) { /* +2 is to make sure there is a w and h */
2244             w = data[i++];
2245             h = data[i++];
2246             /* watch for the data being too small for the specified size,
2247                or for zero sized icons. */
2248             if (i + w*h > num || w == 0 || h == 0) {
2249                 i += w*h;
2250                 continue;
2251             }
2252
2253             /* convert it to the right bit order for ObRender */
2254             for (j = 0; j < w*h; ++j)
2255                 data[i+j] =
2256                     (((data[i+j] >> 24) & 0xff) << RrDefaultAlphaOffset) +
2257                     (((data[i+j] >> 16) & 0xff) << RrDefaultRedOffset)   +
2258                     (((data[i+j] >>  8) & 0xff) << RrDefaultGreenOffset) +
2259                     (((data[i+j] >>  0) & 0xff) << RrDefaultBlueOffset);
2260
2261             /* add it to the image cache as an original */
2262             if (!img)
2263                 img = RrImageNewFromData(ob_rr_icons, &data[i], w, h);
2264             else
2265                 RrImageAddFromData(img, &data[i], w, h);
2266
2267             i += w*h;
2268         }
2269
2270         g_free(data);
2271     }
2272
2273     /* if we didn't find an image from the NET_WM_ICON stuff, then try the
2274        legacy X hints */
2275     if (!img) {
2276         XWMHints *hints;
2277
2278         if ((hints = XGetWMHints(obt_display, self->window))) {
2279             if (hints->flags & IconPixmapHint) {
2280                 gboolean xicon;
2281                 obt_display_ignore_errors(TRUE);
2282                 xicon = RrPixmapToRGBA(ob_rr_inst,
2283                                        hints->icon_pixmap,
2284                                        (hints->flags & IconMaskHint ?
2285                                         hints->icon_mask : None),
2286                                        (gint*)&w, (gint*)&h, &data);
2287                 obt_display_ignore_errors(FALSE);
2288
2289                 if (xicon) {
2290                     if (w > 0 && h > 0) {
2291                         if (!img)
2292                             img = RrImageNewFromData(ob_rr_icons, data, w, h);
2293                         else
2294                             RrImageAddFromData(img, data, w, h);
2295                     }
2296
2297                     g_free(data);
2298                 }
2299             }
2300             XFree(hints);
2301         }
2302     }
2303
2304     /* set the client's icons to be whatever we found */
2305     RrImageUnref(self->icon_set);
2306     self->icon_set = img;
2307
2308     /* if the client has no icon at all, then we set a default icon onto it.
2309        but, if it has parents, then one of them will have an icon already
2310     */
2311     if (!self->icon_set && !self->parents) {
2312         RrPixel32 *icon = ob_rr_theme->def_win_icon;
2313         gulong *ldata; /* use a long here to satisfy OBT_PROP_SETA32 */
2314
2315         w = ob_rr_theme->def_win_icon_w;
2316         h = ob_rr_theme->def_win_icon_h;
2317         ldata = g_new(gulong, w*h+2);
2318         ldata[0] = w;
2319         ldata[1] = h;
2320         for (i = 0; i < w*h; ++i)
2321             ldata[i+2] = (((icon[i] >> RrDefaultAlphaOffset) & 0xff) << 24) +
2322                 (((icon[i] >> RrDefaultRedOffset) & 0xff) << 16) +
2323                 (((icon[i] >> RrDefaultGreenOffset) & 0xff) << 8) +
2324                 (((icon[i] >> RrDefaultBlueOffset) & 0xff) << 0);
2325         OBT_PROP_SETA32(self->window, NET_WM_ICON, CARDINAL, ldata, w*h+2);
2326         g_free(ldata);
2327     } else if (self->frame)
2328         /* don't draw the icon empty if we're just setting one now anyways,
2329            we'll get the property change any second */
2330         frame_adjust_icon(self->frame);
2331
2332     grab_server(FALSE);
2333 }
2334
2335 void client_update_icon_geometry(ObClient *self)
2336 {
2337     guint num;
2338     guint32 *data;
2339
2340     RECT_SET(self->icon_geometry, 0, 0, 0, 0);
2341
2342     if (OBT_PROP_GETA32(self->window, NET_WM_ICON_GEOMETRY, CARDINAL,
2343                         &data, &num))
2344     {
2345         if (num == 4)
2346             /* don't let them set it with an area < 0 */
2347             RECT_SET(self->icon_geometry, data[0], data[1],
2348                      MAX(data[2],0), MAX(data[3],0));
2349         g_free(data);
2350     }
2351 }
2352
2353 static void client_get_session_ids(ObClient *self)
2354 {
2355     guint32 leader;
2356     gboolean got;
2357     gchar *s;
2358     gchar **ss;
2359
2360     if (!OBT_PROP_GET32(self->window, WM_CLIENT_LEADER, WINDOW, &leader))
2361         leader = None;
2362
2363     /* get the SM_CLIENT_ID */
2364     if (leader && leader != self->window)
2365         OBT_PROP_GETS_XPCS(leader, SM_CLIENT_ID, &self->sm_client_id);
2366     else
2367         OBT_PROP_GETS_XPCS(self->window, SM_CLIENT_ID, &self->sm_client_id);
2368
2369     /* get the WM_CLASS (name and class). make them "" if they are not
2370        provided */
2371     got = OBT_PROP_GETSS_TYPE(self->window, WM_CLASS, STRING_NO_CC, &ss);
2372
2373     if (got) {
2374         if (ss[0]) {
2375             self->name = g_strdup(ss[0]);
2376             if (ss[1])
2377                 self->class = g_strdup(ss[1]);
2378         }
2379         g_strfreev(ss);
2380     }
2381
2382     if (self->name == NULL) self->name = g_strdup("");
2383     if (self->class == NULL) self->class = g_strdup("");
2384
2385     /* get the WM_CLASS (name and class) from the group leader. make them "" if
2386        they are not provided */
2387     if (leader)
2388         got = OBT_PROP_GETSS_TYPE(leader, WM_CLASS, STRING_NO_CC, &ss);
2389     else
2390         got = FALSE;
2391
2392     if (got) {
2393         if (ss[0]) {
2394             self->group_name = g_strdup(ss[0]);
2395             if (ss[1])
2396                 self->group_class = g_strdup(ss[1]);
2397         }
2398         g_strfreev(ss);
2399     }
2400
2401     if (self->group_name == NULL) self->group_name = g_strdup("");
2402     if (self->group_class == NULL) self->group_class = g_strdup("");
2403
2404     /* get the WM_WINDOW_ROLE. make it "" if it is not provided */
2405     got = OBT_PROP_GETS_XPCS(self->window, WM_WINDOW_ROLE, &s);
2406
2407     if (got)
2408         self->role = s;
2409     else
2410         self->role = g_strdup("");
2411
2412     /* get the WM_COMMAND */
2413     got = FALSE;
2414
2415     if (leader)
2416         got = OBT_PROP_GETSS(leader, WM_COMMAND, &ss);
2417     if (!got)
2418         got = OBT_PROP_GETSS(self->window, WM_COMMAND, &ss);
2419
2420     if (got) {
2421         /* merge/mash them all together */
2422         gchar *merge = NULL;
2423         gint i;
2424
2425         for (i = 0; ss[i]; ++i) {
2426             gchar *tmp = merge;
2427             if (merge)
2428                 merge = g_strconcat(merge, ss[i], NULL);
2429             else
2430                 merge = g_strconcat(ss[i], NULL);
2431             g_free(tmp);
2432         }
2433         g_strfreev(ss);
2434
2435         self->wm_command = merge;
2436     }
2437
2438     /* get the WM_CLIENT_MACHINE */
2439     got = FALSE;
2440     if (leader)
2441         got = OBT_PROP_GETS(leader, WM_CLIENT_MACHINE, &s);
2442     if (!got)
2443         got = OBT_PROP_GETS(self->window, WM_CLIENT_MACHINE, &s);
2444
2445     if (got) {
2446         gchar localhost[128];
2447         guint32 pid;
2448
2449         gethostname(localhost, 127);
2450         localhost[127] = '\0';
2451         if (strcmp(localhost, s) != 0)
2452             self->client_machine = s;
2453         else
2454             g_free(s);
2455
2456         /* see if it has the PID set too (the PID requires that the
2457            WM_CLIENT_MACHINE be set) */
2458         if (OBT_PROP_GET32(self->window, NET_WM_PID, CARDINAL, &pid))
2459             self->pid = pid;
2460     }
2461 }
2462
2463 const gchar *client_type_to_string(ObClient *self)
2464 {
2465     const gchar *type;
2466
2467     switch (self->type) {
2468     case OB_CLIENT_TYPE_NORMAL:
2469         type = "normal"; break;
2470     case OB_CLIENT_TYPE_DIALOG:
2471         type = "dialog"; break;
2472     case OB_CLIENT_TYPE_UTILITY:
2473         type = "utility"; break;
2474     case OB_CLIENT_TYPE_MENU:
2475         type = "menu"; break;
2476     case OB_CLIENT_TYPE_TOOLBAR:
2477         type = "toolbar"; break;
2478     case OB_CLIENT_TYPE_SPLASH:
2479         type = "splash"; break;
2480     case OB_CLIENT_TYPE_DESKTOP:
2481         type = "desktop"; break;
2482     case OB_CLIENT_TYPE_DOCK:
2483         type = "dock"; break;
2484     }
2485
2486     return type;
2487 }
2488
2489 /*! Save the properties used for app matching rules, as seen by Openbox when
2490   the window mapped, so that users can still access them later if the app
2491   changes them */
2492 static void client_save_app_rule_values(ObClient *self)
2493 {
2494     OBT_PROP_SETS(self->window, OB_APP_ROLE, self->role);
2495     OBT_PROP_SETS(self->window, OB_APP_NAME, self->name);
2496     OBT_PROP_SETS(self->window, OB_APP_CLASS, self->class);
2497     OBT_PROP_SETS(self->window, OB_APP_GROUP_NAME, self->group_name);
2498     OBT_PROP_SETS(self->window, OB_APP_GROUP_CLASS, self->group_class);
2499     OBT_PROP_SETS(self->window, OB_APP_TITLE, self->original_title);
2500
2501     OBT_PROP_SETS(self->window, OB_APP_TYPE, client_type_to_string(self));
2502 }
2503
2504 static void client_change_wm_state(ObClient *self)
2505 {
2506     gulong state[2];
2507     glong old;
2508
2509     old = self->wmstate;
2510
2511     if (self->shaded || self->iconic ||
2512         (self->desktop != DESKTOP_ALL && self->desktop != screen_desktop))
2513     {
2514         self->wmstate = IconicState;
2515     } else
2516         self->wmstate = NormalState;
2517
2518     if (old != self->wmstate) {
2519         OBT_PROP_MSG(ob_screen, self->window, KDE_WM_CHANGE_STATE,
2520                      self->wmstate, 1, 0, 0, 0);
2521
2522         state[0] = self->wmstate;
2523         state[1] = None;
2524         OBT_PROP_SETA32(self->window, WM_STATE, WM_STATE, state, 2);
2525     }
2526 }
2527
2528 static void client_change_state(ObClient *self)
2529 {
2530     gulong netstate[13];
2531     guint num;
2532
2533     num = 0;
2534     if (self->modal)
2535         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_MODAL);
2536     if (self->shaded)
2537         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_SHADED);
2538     if (self->iconic)
2539         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_HIDDEN);
2540     if (self->skip_taskbar)
2541         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_SKIP_TASKBAR);
2542     if (self->skip_pager)
2543         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_SKIP_PAGER);
2544     if (self->fullscreen)
2545         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_FULLSCREEN);
2546     if (self->max_vert)
2547         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_VERT);
2548     if (self->max_horz)
2549         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_HORZ);
2550     if (self->above)
2551         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_ABOVE);
2552     if (self->below)
2553         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_BELOW);
2554     if (self->demands_attention)
2555         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_DEMANDS_ATTENTION);
2556     if (self->undecorated)
2557         netstate[num++] = OBT_PROP_ATOM(OB_WM_STATE_UNDECORATED);
2558     if (self->locked)
2559         netstate[num++] = OBT_PROP_ATOM(OB_WM_STATE_LOCKED);
2560     OBT_PROP_SETA32(self->window, NET_WM_STATE, ATOM, netstate, num);
2561
2562     if (self->frame)
2563         frame_adjust_state(self->frame);
2564 }
2565
2566 ObClient *client_search_focus_tree(ObClient *self)
2567 {
2568     GSList *it;
2569     ObClient *ret;
2570
2571     for (it = self->transients; it; it = g_slist_next(it)) {
2572         if (client_focused(it->data)) return it->data;
2573         if ((ret = client_search_focus_tree(it->data))) return ret;
2574     }
2575     return NULL;
2576 }
2577
2578 ObClient *client_search_focus_tree_full(ObClient *self)
2579 {
2580     if (self->parents) {
2581         GSList *it;
2582
2583         for (it = self->parents; it; it = g_slist_next(it)) {
2584             ObClient *c = it->data;
2585             if ((c = client_search_focus_tree_full(c))) return c;
2586         }
2587
2588         return NULL;
2589     }
2590     else {
2591         /* this function checks the whole tree, the client_search_focus_tree
2592            does not, so we need to check this window */
2593         if (client_focused(self))
2594             return self;
2595         return client_search_focus_tree(self);
2596     }
2597 }
2598
2599 ObClient *client_search_focus_group_full(ObClient *self)
2600 {
2601     GSList *it;
2602
2603     if (self->group) {
2604         for (it = self->group->members; it; it = g_slist_next(it)) {
2605             ObClient *c = it->data;
2606
2607             if (client_focused(c)) return c;
2608             if ((c = client_search_focus_tree(it->data))) return c;
2609         }
2610     } else
2611         if (client_focused(self)) return self;
2612     return NULL;
2613 }
2614
2615 gboolean client_has_parent(ObClient *self)
2616 {
2617     return self->parents != NULL;
2618 }
2619
2620 gboolean client_has_children(ObClient *self)
2621 {
2622     return self->transients != NULL;
2623 }
2624
2625 gboolean client_is_oldfullscreen(const ObClient *self,
2626                                  const Rect *area)
2627 {
2628     const Rect *monitor, *allmonitors;
2629
2630     /* No decorations and fills the monitor = oldskool fullscreen.
2631        But not for maximized windows.
2632     */
2633
2634     if (self->decorations || self->max_horz || self->max_vert) return FALSE;
2635
2636     monitor = screen_physical_area_monitor(screen_find_monitor(area));
2637     allmonitors = screen_physical_area_all_monitors();
2638
2639     return (RECT_EQUAL(*area, *monitor) ||
2640             RECT_EQUAL(*area, *allmonitors));
2641 }
2642
2643 static ObStackingLayer calc_layer(ObClient *self)
2644 {
2645     ObStackingLayer l;
2646
2647     if (self->type == OB_CLIENT_TYPE_DESKTOP)
2648         l = OB_STACKING_LAYER_DESKTOP;
2649     else if (self->type == OB_CLIENT_TYPE_DOCK) {
2650         if (self->below) l = OB_STACKING_LAYER_NORMAL;
2651         else l = OB_STACKING_LAYER_ABOVE;
2652     }
2653     else if ((self->fullscreen ||
2654               client_is_oldfullscreen(self, &self->area)) &&
2655              /* you are fullscreen while you or your children are focused.. */
2656              (client_focused(self) || client_search_focus_tree(self) ||
2657               /* you can be fullscreen if you're on another desktop */
2658               (self->desktop != screen_desktop &&
2659                self->desktop != DESKTOP_ALL) ||
2660               /* and you can also be fullscreen if the focused client is on
2661                  another monitor, or nothing else is focused */
2662               (!focus_client ||
2663                client_monitor(focus_client) != client_monitor(self))))
2664         l = OB_STACKING_LAYER_FULLSCREEN;
2665     else if (self->above) l = OB_STACKING_LAYER_ABOVE;
2666     else if (self->below) l = OB_STACKING_LAYER_BELOW;
2667     else l = OB_STACKING_LAYER_NORMAL;
2668
2669     return l;
2670 }
2671
2672 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
2673                                         ObStackingLayer min)
2674 {
2675     ObStackingLayer old, own;
2676     GSList *it;
2677
2678     old = self->layer;
2679     own = calc_layer(self);
2680     self->layer = MAX(own, min);
2681
2682     if (self->layer != old) {
2683         stacking_remove(CLIENT_AS_WINDOW(self));
2684         stacking_add_nonintrusive(CLIENT_AS_WINDOW(self));
2685     }
2686
2687     /* we've been restacked */
2688     self->visited = TRUE;
2689
2690     for (it = self->transients; it; it = g_slist_next(it))
2691         client_calc_layer_recursive(it->data, orig,
2692                                     self->layer);
2693 }
2694
2695 static void client_calc_layer_internal(ObClient *self)
2696 {
2697     GSList *sit;
2698
2699     /* transients take on the layer of their parents */
2700     sit = client_search_all_top_parents(self);
2701
2702     for (; sit; sit = g_slist_next(sit))
2703         client_calc_layer_recursive(sit->data, self, 0);
2704 }
2705
2706 void client_calc_layer(ObClient *self)
2707 {
2708     GList *it;
2709
2710     /* skip over stuff above fullscreen layer */
2711     for (it = stacking_list; it; it = g_list_next(it))
2712         if (window_layer(it->data) <= OB_STACKING_LAYER_FULLSCREEN) break;
2713
2714     /* find the windows in the fullscreen layer, and mark them not-visited */
2715     for (; it; it = g_list_next(it)) {
2716         if (window_layer(it->data) < OB_STACKING_LAYER_FULLSCREEN) break;
2717         else if (WINDOW_IS_CLIENT(it->data))
2718             WINDOW_AS_CLIENT(it->data)->visited = FALSE;
2719     }
2720
2721     client_calc_layer_internal(self);
2722
2723     /* skip over stuff above fullscreen layer */
2724     for (it = stacking_list; it; it = g_list_next(it))
2725         if (window_layer(it->data) <= OB_STACKING_LAYER_FULLSCREEN) break;
2726
2727     /* now recalc any windows in the fullscreen layer which have not
2728        had their layer recalced already */
2729     for (; it; it = g_list_next(it)) {
2730         if (window_layer(it->data) < OB_STACKING_LAYER_FULLSCREEN) break;
2731         else if (WINDOW_IS_CLIENT(it->data) &&
2732                  !WINDOW_AS_CLIENT(it->data)->visited)
2733             client_calc_layer_internal(it->data);
2734     }
2735 }
2736
2737 gboolean client_should_show(ObClient *self)
2738 {
2739     if (self->iconic)
2740         return FALSE;
2741     if (client_normal(self) && screen_showing_desktop())
2742         return FALSE;
2743     if (self->desktop == screen_desktop || self->desktop == DESKTOP_ALL)
2744         return TRUE;
2745
2746     return FALSE;
2747 }
2748
2749 gboolean client_show(ObClient *self)
2750 {
2751     gboolean show = FALSE;
2752
2753     if (client_should_show(self)) {
2754         /* replay pending pointer event before showing the window, in case it
2755            should be going to something under the window */
2756         mouse_replay_pointer();
2757
2758         frame_show(self->frame);
2759         show = TRUE;
2760
2761         /* According to the ICCCM (sec 4.1.3.1) when a window is not visible,
2762            it needs to be in IconicState. This includes when it is on another
2763            desktop!
2764         */
2765         client_change_wm_state(self);
2766     }
2767     return show;
2768 }
2769
2770 gboolean client_hide(ObClient *self)
2771 {
2772     gboolean hide = FALSE;
2773
2774     if (!client_should_show(self)) {
2775         /* We don't need to ignore enter events here.
2776            The window can hide/iconify in 3 different ways:
2777            1 - through an x message. in this case we ignore all enter events
2778                caused by responding to the x message (unless underMouse)
2779            2 - by a keyboard action. in this case we ignore all enter events
2780                caused by the action
2781            3 - by a mouse action. in this case they are doing stuff with the
2782                mouse and focus _should_ move.
2783
2784            Also in action_end, we simulate an enter event that can't be ignored
2785            so trying to ignore them is futile in case 3 anyways
2786         */
2787
2788         /* replay pending pointer event before hiding the window, in case it
2789            should be going to the window */
2790         mouse_replay_pointer();
2791
2792         frame_hide(self->frame);
2793         hide = TRUE;
2794
2795         /* According to the ICCCM (sec 4.1.3.1) when a window is not visible,
2796            it needs to be in IconicState. This includes when it is on another
2797            desktop!
2798         */
2799         client_change_wm_state(self);
2800     }
2801     return hide;
2802 }
2803
2804 void client_showhide(ObClient *self)
2805 {
2806     if (!client_show(self))
2807         client_hide(self);
2808 }
2809
2810 gboolean client_normal(ObClient *self) {
2811     return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
2812               self->type == OB_CLIENT_TYPE_DOCK ||
2813               self->type == OB_CLIENT_TYPE_SPLASH);
2814 }
2815
2816 gboolean client_helper(ObClient *self)
2817 {
2818     return (self->type == OB_CLIENT_TYPE_UTILITY ||
2819             self->type == OB_CLIENT_TYPE_MENU ||
2820             self->type == OB_CLIENT_TYPE_TOOLBAR);
2821 }
2822
2823 gboolean client_occupies_space(ObClient *self)
2824 {
2825     return !(self->type == OB_CLIENT_TYPE_DESKTOP ||
2826              self->type == OB_CLIENT_TYPE_SPLASH);
2827 }
2828
2829 gboolean client_mouse_focusable(ObClient *self)
2830 {
2831     return !(self->type == OB_CLIENT_TYPE_MENU ||
2832              self->type == OB_CLIENT_TYPE_TOOLBAR ||
2833              self->type == OB_CLIENT_TYPE_SPLASH ||
2834              self->type == OB_CLIENT_TYPE_DOCK);
2835 }
2836
2837 gboolean client_enter_focusable(ObClient *self)
2838 {
2839     /* you can focus desktops but it shouldn't on enter */
2840     return (client_mouse_focusable(self) &&
2841             self->type != OB_CLIENT_TYPE_DESKTOP);
2842 }
2843
2844 static void client_apply_startup_state(ObClient *self,
2845                                        gint x, gint y, gint w, gint h)
2846 {
2847     /* save the states that we are going to apply */
2848     gboolean iconic = self->iconic;
2849     gboolean fullscreen = self->fullscreen;
2850     gboolean undecorated = self->undecorated;
2851     gboolean shaded = self->shaded;
2852     gboolean demands_attention = self->demands_attention;
2853     gboolean max_horz = self->max_horz;
2854     gboolean max_vert = self->max_vert;
2855     Rect oldarea;
2856     gint l;
2857
2858     /* turn them all off in the client, so they won't affect the window
2859        being placed */
2860     self->iconic = self->fullscreen = self->undecorated = self->shaded =
2861         self->demands_attention = self->max_horz = self->max_vert = FALSE;
2862
2863     /* move the client to its placed position, or it it's already there,
2864        generate a ConfigureNotify telling the client where it is.
2865
2866        do this after adjusting the frame. otherwise it gets all weird and
2867        clients don't work right
2868
2869        do this before applying the states so they have the correct
2870        pre-max/pre-fullscreen values
2871     */
2872     client_try_configure(self, &x, &y, &w, &h, &l, &l, FALSE);
2873     ob_debug("placed window 0x%x at %d, %d with size %d x %d",
2874              self->window, x, y, w, h);
2875     /* save the area, and make it where it should be for the premax stuff */
2876     oldarea = self->area;
2877     RECT_SET(self->area, x, y, w, h);
2878
2879     /* apply the states. these are in a carefully crafted order.. */
2880
2881     if (iconic)
2882         client_iconify(self, TRUE, FALSE, TRUE);
2883     if (undecorated)
2884         client_set_undecorated(self, TRUE);
2885     if (shaded)
2886         client_shade(self, TRUE);
2887     if (demands_attention)
2888         client_hilite(self, TRUE);
2889
2890     if (max_vert && max_horz)
2891         client_maximize(self, TRUE, 0);
2892     else if (max_vert)
2893         client_maximize(self, TRUE, 2);
2894     else if (max_horz)
2895         client_maximize(self, TRUE, 1);
2896
2897     /* fullscreen removes the ability to apply other states */
2898     if (fullscreen)
2899         client_fullscreen(self, TRUE);
2900
2901     /* make sure client_setup_decor_and_functions() is called at least once */
2902     client_setup_decor_and_functions(self, FALSE);
2903
2904     /* if the window hasn't been configured yet, then do so now, in fact the
2905        x,y,w,h may _not_ be the same as the area rect, which can end up
2906        meaning that the client isn't properly moved/resized by the fullscreen
2907        function
2908        pho can cause this because it maps at size of the screen but not 0,0
2909        so openbox moves it on screen to 0,0 (thus x,y=0,0 and area.x,y don't).
2910        then fullscreen'ing makes it go to 0,0 which it thinks it already is at
2911        cuz thats where the pre-fullscreen will be. however the actual area is
2912        not, so this needs to be called even if we have fullscreened/maxed
2913     */
2914     self->area = oldarea;
2915     client_configure(self, x, y, w, h, FALSE, TRUE, FALSE);
2916
2917     /* nothing to do for the other states:
2918        skip_taskbar
2919        skip_pager
2920        modal
2921        above
2922        below
2923     */
2924 }
2925
2926 void client_gravity_resize_w(ObClient *self, gint *x, gint oldw, gint neww)
2927 {
2928     /* these should be the current values. this is for when you're not moving,
2929        just resizing */
2930     g_assert(*x == self->area.x);
2931     g_assert(oldw == self->area.width);
2932
2933     /* horizontal */
2934     switch (self->gravity) {
2935     default:
2936     case NorthWestGravity:
2937     case WestGravity:
2938     case SouthWestGravity:
2939     case StaticGravity:
2940     case ForgetGravity:
2941         break;
2942     case NorthGravity:
2943     case CenterGravity:
2944     case SouthGravity:
2945         *x -= (neww - oldw) / 2;
2946         break;
2947     case NorthEastGravity:
2948     case EastGravity:
2949     case SouthEastGravity:
2950         *x -= neww - oldw;
2951         break;
2952     }
2953 }
2954
2955 void client_gravity_resize_h(ObClient *self, gint *y, gint oldh, gint newh)
2956 {
2957     /* these should be the current values. this is for when you're not moving,
2958        just resizing */
2959     g_assert(*y == self->area.y);
2960     g_assert(oldh == self->area.height);
2961
2962     /* vertical */
2963     switch (self->gravity) {
2964     default:
2965     case NorthWestGravity:
2966     case NorthGravity:
2967     case NorthEastGravity:
2968     case StaticGravity:
2969     case ForgetGravity:
2970         break;
2971     case WestGravity:
2972     case CenterGravity:
2973     case EastGravity:
2974         *y -= (newh - oldh) / 2;
2975         break;
2976     case SouthWestGravity:
2977     case SouthGravity:
2978     case SouthEastGravity:
2979         *y -= newh - oldh;
2980         break;
2981     }
2982 }
2983
2984 void client_try_configure(ObClient *self, gint *x, gint *y, gint *w, gint *h,
2985                           gint *logicalw, gint *logicalh,
2986                           gboolean user)
2987 {
2988     Rect desired = {*x, *y, *w, *h};
2989     frame_rect_to_frame(self->frame, &desired);
2990
2991     /* make the frame recalculate its dimensions n shit without changing
2992        anything visible for real, this way the constraints below can work with
2993        the updated frame dimensions. */
2994     frame_adjust_area(self->frame, FALSE, TRUE, TRUE);
2995
2996     /* cap any X windows at the size of an unsigned short */
2997     *w = MIN(*w,
2998              (gint)G_MAXUSHORT
2999              - self->frame->size.left - self->frame->size.right);
3000     *h = MIN(*h,
3001              (gint)G_MAXUSHORT
3002              - self->frame->size.top - self->frame->size.bottom);
3003
3004     /* gets the frame's position */
3005     frame_client_gravity(self->frame, x, y);
3006
3007     /* these positions are frame positions, not client positions */
3008
3009     /* set the size and position if fullscreen */
3010     if (self->fullscreen) {
3011         const Rect *a;
3012         guint i;
3013
3014         i = screen_find_monitor(&desired);
3015         a = screen_physical_area_monitor(i);
3016
3017         *x = a->x;
3018         *y = a->y;
3019         *w = a->width;
3020         *h = a->height;
3021
3022         user = FALSE; /* ignore if the client can't be moved/resized when it
3023                          is fullscreening */
3024     } else if (self->max_horz || self->max_vert) {
3025         Rect *a;
3026         guint i;
3027
3028         /* use all possible struts when maximizing to the full screen */
3029         i = screen_find_monitor(&desired);
3030         a = screen_area(self->desktop, i,
3031                         (self->max_horz && self->max_vert ? NULL : &desired));
3032
3033         /* set the size and position if maximized */
3034         if (self->max_horz) {
3035             *x = a->x;
3036             *w = a->width - self->frame->size.left - self->frame->size.right;
3037         }
3038         if (self->max_vert) {
3039             *y = a->y;
3040             *h = a->height - self->frame->size.top - self->frame->size.bottom;
3041         }
3042
3043         user = FALSE; /* ignore if the client can't be moved/resized when it
3044                          is maximizing */
3045
3046         g_slice_free(Rect, a);
3047     }
3048
3049     /* gets the client's position */
3050     frame_frame_gravity(self->frame, x, y);
3051
3052     /* work within the preferred sizes given by the window, these may have
3053        changed rather than it's requested width and height, so always run
3054        through this code */
3055     {
3056         gint basew, baseh, minw, minh;
3057         gint incw, inch, maxw, maxh;
3058         gfloat minratio, maxratio;
3059
3060         incw = self->size_inc.width;
3061         inch = self->size_inc.height;
3062         minratio = self->fullscreen || (self->max_horz && self->max_vert) ?
3063             0 : self->min_ratio;
3064         maxratio = self->fullscreen || (self->max_horz && self->max_vert) ?
3065             0 : self->max_ratio;
3066
3067         /* base size is substituted with min size if not specified */
3068         if (self->base_size.width >= 0 || self->base_size.height >= 0) {
3069             basew = self->base_size.width;
3070             baseh = self->base_size.height;
3071         } else {
3072             basew = self->min_size.width;
3073             baseh = self->min_size.height;
3074         }
3075         /* min size is substituted with base size if not specified */
3076         if (self->min_size.width || self->min_size.height) {
3077             minw = self->min_size.width;
3078             minh = self->min_size.height;
3079         } else {
3080             minw = self->base_size.width;
3081             minh = self->base_size.height;
3082         }
3083
3084         /* This comment is no longer true */
3085         /* if this is a user-requested resize, then check against min/max
3086            sizes */
3087
3088         /* smaller than min size or bigger than max size? */
3089         if (*w > self->max_size.width) *w = self->max_size.width;
3090         if (*w < minw) *w = minw;
3091         if (*h > self->max_size.height) *h = self->max_size.height;
3092         if (*h < minh) *h = minh;
3093
3094         *w -= basew;
3095         *h -= baseh;
3096
3097         /* the sizes to used for maximized */
3098         maxw = *w;
3099         maxh = *h;
3100
3101         /* keep to the increments */
3102         *w /= incw;
3103         *h /= inch;
3104
3105         /* you cannot resize to nothing */
3106         if (basew + *w < 1) *w = 1 - basew;
3107         if (baseh + *h < 1) *h = 1 - baseh;
3108
3109         /* save the logical size */
3110         *logicalw = incw > 1 ? *w : *w + basew;
3111         *logicalh = inch > 1 ? *h : *h + baseh;
3112
3113         *w *= incw;
3114         *h *= inch;
3115
3116         /* if maximized/fs then don't use the size increments */
3117         if (self->fullscreen || self->max_horz) *w = maxw;
3118         if (self->fullscreen || self->max_vert) *h = maxh;
3119
3120         *w += basew;
3121         *h += baseh;
3122
3123         /* adjust the height to match the width for the aspect ratios.
3124            for this, min size is not substituted for base size ever. */
3125         *w -= self->base_size.width;
3126         *h -= self->base_size.height;
3127
3128         if (minratio)
3129             if (*h * minratio > *w) {
3130                 *h = (gint)(*w / minratio);
3131
3132                 /* you cannot resize to nothing */
3133                 if (*h < 1) {
3134                     *h = 1;
3135                     *w = (gint)(*h * minratio);
3136                 }
3137             }
3138         if (maxratio)
3139             if (*h * maxratio < *w) {
3140                 *h = (gint)(*w / maxratio);
3141
3142                 /* you cannot resize to nothing */
3143                 if (*h < 1) {
3144                     *h = 1;
3145                     *w = (gint)(*h * minratio);
3146                 }
3147             }
3148
3149         *w += self->base_size.width;
3150         *h += self->base_size.height;
3151     }
3152
3153     /* these override the above states! if you cant move you can't move! */
3154     if (user) {
3155         if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
3156             *x = self->area.x;
3157             *y = self->area.y;
3158         }
3159         if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
3160             *w = self->area.width;
3161             *h = self->area.height;
3162         }
3163     }
3164
3165     g_assert(*w > 0);
3166     g_assert(*h > 0);
3167 }
3168
3169 void client_configure(ObClient *self, gint x, gint y, gint w, gint h,
3170                       gboolean user, gboolean final, gboolean force_reply)
3171 {
3172     Rect oldframe, oldclient;
3173     gboolean send_resize_client;
3174     gboolean moved = FALSE, resized = FALSE, rootmoved = FALSE;
3175     gboolean fmoved, fresized;
3176     guint fdecor = self->frame->decorations;
3177     gboolean fhorz = self->frame->max_horz;
3178     gboolean fvert = self->frame->max_vert;
3179     gint logicalw, logicalh;
3180
3181     /* find the new x, y, width, and height (and logical size) */
3182     client_try_configure(self, &x, &y, &w, &h, &logicalw, &logicalh, user);
3183
3184     /* set the logical size if things changed */
3185     if (!(w == self->area.width && h == self->area.height))
3186         SIZE_SET(self->logical_size, logicalw, logicalh);
3187
3188     /* figure out if we moved or resized or what */
3189     moved = (x != self->area.x || y != self->area.y);
3190     resized = (w != self->area.width || h != self->area.height);
3191
3192     oldframe = self->frame->area;
3193     oldclient = self->area;
3194     RECT_SET(self->area, x, y, w, h);
3195
3196     /* for app-requested resizes, always resize if 'resized' is true.
3197        for user-requested ones, only resize if final is true, or when
3198        resizing in redraw mode */
3199     send_resize_client = ((!user && resized) ||
3200                           (user && (final ||
3201                                     (resized && config_resize_redraw))));
3202
3203     /* if the client is enlarging, then resize the client before the frame */
3204     if (send_resize_client && (w > oldclient.width || h > oldclient.height)) {
3205         XMoveResizeWindow(obt_display, self->window,
3206                           self->frame->size.left, self->frame->size.top,
3207                           MAX(w, oldclient.width), MAX(h, oldclient.height));
3208         frame_adjust_client_area(self->frame);
3209     }
3210
3211     /* find the frame's dimensions and move/resize it */
3212     fmoved = moved;
3213     fresized = resized;
3214
3215     /* if decorations changed, then readjust everything for the frame */
3216     if (self->decorations != fdecor ||
3217         self->max_horz != fhorz || self->max_vert != fvert)
3218     {
3219         fmoved = fresized = TRUE;
3220     }
3221
3222     /* adjust the frame */
3223     if (fmoved || fresized) {
3224         gulong ignore_start;
3225         if (!user)
3226             ignore_start = event_start_ignore_all_enters();
3227
3228         /* replay pending pointer event before move the window, in case it
3229            would change what window gets the event */
3230         mouse_replay_pointer();
3231
3232         frame_adjust_area(self->frame, fmoved, fresized, FALSE);
3233
3234         if (!user)
3235             event_end_ignore_all_enters(ignore_start);
3236     }
3237
3238     if (!user || final) {
3239         gint oldrx = self->root_pos.x;
3240         gint oldry = self->root_pos.y;
3241         /* we have reset the client to 0 border width, so don't include
3242            it in these coords */
3243         POINT_SET(self->root_pos,
3244                   self->frame->area.x + self->frame->size.left -
3245                   self->border_width,
3246                   self->frame->area.y + self->frame->size.top -
3247                   self->border_width);
3248         if (self->root_pos.x != oldrx || self->root_pos.y != oldry)
3249             rootmoved = TRUE;
3250     }
3251
3252     /* This is kinda tricky and should not be changed.. let me explain!
3253
3254        When user = FALSE, then the request is coming from the application
3255        itself, and we are more strict about when to send a synthetic
3256        ConfigureNotify.  We strictly follow the rules of the ICCCM sec 4.1.5
3257        in this case (or send one if force_reply is true)
3258
3259        When user = TRUE, then the request is coming from "us", like when we
3260        maximize a window or something.  In this case we are more lenient.  We
3261        used to follow the same rules as above, but _Java_ Swing can't handle
3262        this. So just to appease Swing, when user = TRUE, we always send
3263        a synthetic ConfigureNotify to give the window its root coordinates.
3264        Lastly, if force_reply is TRUE, we always send a
3265        ConfigureNotify, which is needed during a resize with XSYNCronization.
3266     */
3267     if ((!user && !resized && (rootmoved || force_reply)) ||
3268         (user && ((!resized && force_reply) || (final && rootmoved))))
3269     {
3270         XEvent event;
3271
3272         event.type = ConfigureNotify;
3273         event.xconfigure.display = obt_display;
3274         event.xconfigure.event = self->window;
3275         event.xconfigure.window = self->window;
3276
3277         ob_debug("Sending ConfigureNotify to %s for %d,%d %dx%d",
3278                  self->title, self->root_pos.x, self->root_pos.y, w, h);
3279
3280         /* root window real coords */
3281         event.xconfigure.x = self->root_pos.x;
3282         event.xconfigure.y = self->root_pos.y;
3283         event.xconfigure.width = w;
3284         event.xconfigure.height = h;
3285         event.xconfigure.border_width = self->border_width;
3286         event.xconfigure.above = None;
3287         event.xconfigure.override_redirect = FALSE;
3288         XSendEvent(event.xconfigure.display, event.xconfigure.window,
3289                    FALSE, StructureNotifyMask, &event);
3290     }
3291
3292     /* if the client is shrinking, then resize the frame before the client.
3293
3294        both of these resize sections may run, because the top one only resizes
3295        in the direction that is growing
3296      */
3297     if (send_resize_client && (w <= oldclient.width || h <= oldclient.height))
3298     {
3299         frame_adjust_client_area(self->frame);
3300         XMoveResizeWindow(obt_display, self->window,
3301                           self->frame->size.left, self->frame->size.top, w, h);
3302     }
3303
3304     XFlush(obt_display);
3305
3306     /* if it moved between monitors, then this can affect the stacking
3307        layer of this window or others - for fullscreen windows.
3308        also if it changed to/from oldschool fullscreen then its layer may
3309        change
3310
3311        watch out tho, don't try change stacking stuff if the window is no
3312        longer being managed !
3313     */
3314     if (self->managed &&
3315         (screen_find_monitor(&self->frame->area) !=
3316          screen_find_monitor(&oldframe) ||
3317          (final && (client_is_oldfullscreen(self, &oldclient) !=
3318                     client_is_oldfullscreen(self, &self->area)))))
3319     {
3320         client_calc_layer(self);
3321     }
3322 }
3323
3324 void client_fullscreen(ObClient *self, gboolean fs)
3325 {
3326     gint x, y, w, h;
3327
3328     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
3329         self->fullscreen == fs) return;                   /* already done */
3330
3331     self->fullscreen = fs;
3332     client_change_state(self); /* change the state hints on the client */
3333
3334     if (fs) {
3335         self->pre_fullscreen_area = self->area;
3336         self->pre_fullscreen_max_horz = self->max_horz;
3337         self->pre_fullscreen_max_vert = self->max_vert;
3338
3339         /* if the window is maximized, its area isn't all that meaningful.
3340            save its premax area instead. */
3341         if (self->max_horz) {
3342             self->pre_fullscreen_area.x = self->pre_max_area.x;
3343             self->pre_fullscreen_area.width = self->pre_max_area.width;
3344         }
3345         if (self->max_vert) {
3346             self->pre_fullscreen_area.y = self->pre_max_area.y;
3347             self->pre_fullscreen_area.height = self->pre_max_area.height;
3348         }
3349
3350         /* these will help configure_full figure out where to fullscreen
3351            the window */
3352         x = self->area.x;
3353         y = self->area.y;
3354         w = self->area.width;
3355         h = self->area.height;
3356     } else {
3357         g_assert(self->pre_fullscreen_area.width > 0 &&
3358                  self->pre_fullscreen_area.height > 0);
3359
3360         self->max_horz = self->pre_fullscreen_max_horz;
3361         self->max_vert = self->pre_fullscreen_max_vert;
3362         if (self->max_horz) {
3363             self->pre_max_area.x = self->pre_fullscreen_area.x;
3364             self->pre_max_area.width = self->pre_fullscreen_area.width;
3365         }
3366         if (self->max_vert) {
3367             self->pre_max_area.y = self->pre_fullscreen_area.y;
3368             self->pre_max_area.height = self->pre_fullscreen_area.height;
3369         }
3370
3371         x = self->pre_fullscreen_area.x;
3372         y = self->pre_fullscreen_area.y;
3373         w = self->pre_fullscreen_area.width;
3374         h = self->pre_fullscreen_area.height;
3375         RECT_SET(self->pre_fullscreen_area, 0, 0, 0, 0);
3376     }
3377
3378     ob_debug("Window %s going fullscreen (%d)",
3379              self->title, self->fullscreen);
3380
3381     if (fs) {
3382         /* make sure the window is on some monitor */
3383         client_find_onscreen(self, &x, &y, w, h, FALSE);
3384     }
3385
3386     client_setup_decor_and_functions(self, FALSE);
3387     client_move_resize(self, x, y, w, h);
3388
3389     /* and adjust our layer/stacking. do this after resizing the window,
3390        and applying decorations, because windows which fill the screen are
3391        considered "fullscreen" and it affects their layer */
3392     client_calc_layer(self);
3393
3394     if (fs) {
3395         /* try focus us when we go into fullscreen mode */
3396         client_focus(self);
3397     }
3398 }
3399
3400 static void client_iconify_recursive(ObClient *self,
3401                                      gboolean iconic, gboolean curdesk,
3402                                      gboolean hide_animation)
3403 {
3404     GSList *it;
3405     gboolean changed = FALSE;
3406
3407     if (self->iconic != iconic) {
3408         ob_debug("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
3409                  self->window);
3410
3411         if (iconic) {
3412             /* don't let non-normal windows iconify along with their parents
3413                or whatever */
3414             if (client_normal(self)) {
3415                 self->iconic = iconic;
3416
3417                 /* update the focus lists.. iconic windows go to the bottom of
3418                    the list. this will also call focus_cycle_addremove(). */
3419                 focus_order_to_bottom(self);
3420
3421                 changed = TRUE;
3422             }
3423         } else {
3424             self->iconic = iconic;
3425
3426             if (curdesk && self->desktop != screen_desktop &&
3427                 self->desktop != DESKTOP_ALL)
3428                 client_set_desktop(self, screen_desktop, FALSE, FALSE);
3429
3430             /* this puts it after the current focused window, this will
3431                also cause focus_cycle_addremove() to be called for the
3432                client */
3433             focus_order_like_new(self);
3434
3435             changed = TRUE;
3436         }
3437     }
3438
3439     if (changed) {
3440         client_change_state(self);
3441         if (config_animate_iconify && !hide_animation)
3442             frame_begin_iconify_animation(self->frame, iconic);
3443         /* do this after starting the animation so it doesn't flash */
3444         client_showhide(self);
3445     }
3446
3447     /* iconify all direct transients, and deiconify all transients
3448        (non-direct too) */
3449     for (it = self->transients; it; it = g_slist_next(it))
3450         if (it->data != self)
3451             if (client_is_direct_child(self, it->data) || !iconic)
3452                 client_iconify_recursive(it->data, iconic, curdesk,
3453                                          hide_animation);
3454 }
3455
3456 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk,
3457                     gboolean hide_animation)
3458 {
3459     if (self->functions & OB_CLIENT_FUNC_ICONIFY || !iconic) {
3460         /* move up the transient chain as far as possible first */
3461         self = client_search_top_direct_parent(self);
3462         client_iconify_recursive(self, iconic, curdesk, hide_animation);
3463     }
3464 }
3465
3466 void client_maximize(ObClient *self, gboolean max, gint dir)
3467 {
3468     gint x, y, w, h;
3469
3470     g_assert(dir == 0 || dir == 1 || dir == 2);
3471     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && max) return;/* can't */
3472
3473     /* check if already done */
3474     if (max) {
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     } else {
3479         if (dir == 0 && !self->max_horz && !self->max_vert) return;
3480         if (dir == 1 && !self->max_horz) return;
3481         if (dir == 2 && !self->max_vert) return;
3482     }
3483
3484     /* these will help configure_full figure out which screen to fill with
3485        the window */
3486     x = self->area.x;
3487     y = self->area.y;
3488     w = self->area.width;
3489     h = self->area.height;
3490
3491     if (max) {
3492         if ((dir == 0 || dir == 1) && !self->max_horz) { /* horz */
3493             RECT_SET(self->pre_max_area,
3494                      self->area.x, self->pre_max_area.y,
3495                      self->area.width, self->pre_max_area.height);
3496         }
3497         if ((dir == 0 || dir == 2) && !self->max_vert) { /* vert */
3498             RECT_SET(self->pre_max_area,
3499                      self->pre_max_area.x, self->area.y,
3500                      self->pre_max_area.width, self->area.height);
3501         }
3502     } else {
3503         if ((dir == 0 || dir == 1) && self->max_horz) { /* horz */
3504             g_assert(self->pre_max_area.width > 0);
3505
3506             x = self->pre_max_area.x;
3507             w = self->pre_max_area.width;
3508
3509             RECT_SET(self->pre_max_area, 0, self->pre_max_area.y,
3510                      0, self->pre_max_area.height);
3511         }
3512         if ((dir == 0 || dir == 2) && self->max_vert) { /* vert */
3513             g_assert(self->pre_max_area.height > 0);
3514
3515             y = self->pre_max_area.y;
3516             h = self->pre_max_area.height;
3517
3518             RECT_SET(self->pre_max_area, self->pre_max_area.x, 0,
3519                      self->pre_max_area.width, 0);
3520         }
3521     }
3522
3523     if (dir == 0 || dir == 1) /* horz */
3524         self->max_horz = max;
3525     if (dir == 0 || dir == 2) /* vert */
3526         self->max_vert = max;
3527
3528     if (max) {
3529         /* make sure the window is on some monitor */
3530         client_find_onscreen(self, &x, &y, w, h, FALSE);
3531     }
3532
3533     client_change_state(self); /* change the state hints on the client */
3534
3535     client_setup_decor_and_functions(self, FALSE);
3536     client_move_resize(self, x, y, w, h);
3537 }
3538
3539 void client_shade(ObClient *self, gboolean shade)
3540 {
3541     if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
3542          shade) ||                         /* can't shade */
3543         self->shaded == shade) return;     /* already done */
3544
3545     self->shaded = shade;
3546     client_change_state(self);
3547     client_change_wm_state(self); /* the window is being hidden/shown */
3548     /* resize the frame to just the titlebar */
3549     frame_adjust_area(self->frame, FALSE, TRUE, FALSE);
3550 }
3551
3552 static void client_ping_event(ObClient *self, gboolean dead)
3553 {
3554     if (self->not_responding != dead) {
3555         self->not_responding = dead;
3556         client_update_title(self);
3557
3558         if (dead)
3559             /* the client isn't responding, so ask to kill it */
3560             client_prompt_kill(self);
3561         else {
3562             /* it came back to life ! */
3563
3564             if (self->kill_prompt) {
3565                 prompt_unref(self->kill_prompt);
3566                 self->kill_prompt = NULL;
3567             }
3568
3569             self->kill_level = 0;
3570         }
3571     }
3572 }
3573
3574 void client_close(ObClient *self)
3575 {
3576     if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
3577
3578     /* if closing an internal obprompt, that is just cancelling it */
3579     if (self->prompt) {
3580         prompt_cancel(self->prompt);
3581         return;
3582     }
3583
3584     /* in the case that the client provides no means to requesting that it
3585        close, we just kill it */
3586     if (!self->delete_window)
3587         /* don't use client_kill(), we should only kill based on PID in
3588            response to a lack of PING replies */
3589         XKillClient(obt_display, self->window);
3590     else {
3591         /* request the client to close with WM_DELETE_WINDOW */
3592         OBT_PROP_MSG_TO(self->window, self->window, WM_PROTOCOLS,
3593                         OBT_PROP_ATOM(WM_DELETE_WINDOW), event_time(),
3594                         0, 0, 0, NoEventMask);
3595
3596         /* we're trying to close the window, so see if it is responding. if it
3597            is not, then we will let them kill the window */
3598         if (self->ping)
3599             ping_start(self, client_ping_event);
3600
3601         /* if we already know the window isn't responding (maybe they clicked
3602            no in the kill dialog but it hasn't come back to life), then show
3603            the kill dialog */
3604         if (self->not_responding)
3605             client_prompt_kill(self);
3606     }
3607 }
3608
3609 #define OB_KILL_RESULT_NO 0
3610 #define OB_KILL_RESULT_YES 1
3611
3612 static gboolean client_kill_requested(ObPrompt *p, gint result, gpointer data)
3613 {
3614     ObClient *self = data;
3615
3616     if (result == OB_KILL_RESULT_YES)
3617         client_kill(self);
3618     return TRUE; /* call the cleanup func */
3619 }
3620
3621 static void client_kill_cleanup(ObPrompt *p, gpointer data)
3622 {
3623     ObClient *self = data;
3624
3625     g_assert(p == self->kill_prompt);
3626
3627     prompt_unref(self->kill_prompt);
3628     self->kill_prompt = NULL;
3629 }
3630
3631 static void client_prompt_kill(ObClient *self)
3632 {
3633     /* check if we're already prompting */
3634     if (!self->kill_prompt) {
3635         ObPromptAnswer answers[] = {
3636             { 0, OB_KILL_RESULT_NO },
3637             { 0, OB_KILL_RESULT_YES }
3638         };
3639         gchar *m;
3640         const gchar *y, *title;
3641
3642         title = self->original_title;
3643         if (title[0] == '\0') {
3644             /* empty string, so use its parent */
3645             ObClient *p = client_search_top_direct_parent(self);
3646             if (p) title = p->original_title;
3647         }
3648
3649         if (client_on_localhost(self)) {
3650             const gchar *sig;
3651
3652             if (self->kill_level == 0)
3653                 sig = "terminate";
3654             else
3655                 sig = "kill";
3656
3657             m = g_strdup_printf
3658                 (_("The window \"%s\" does not seem to be responding.  Do you want to force it to exit by sending the %s signal?"),
3659                  title, sig);
3660             y = _("End Process");
3661         }
3662         else {
3663             m = g_strdup_printf
3664                 (_("The window \"%s\" does not seem to be responding.  Do you want to disconnect it from the X server?"),
3665                  title);
3666             y = _("Disconnect");
3667         }
3668         /* set the dialog buttons' text */
3669         answers[0].text = _("Cancel");  /* "no" */
3670         answers[1].text = y;            /* "yes" */
3671
3672         self->kill_prompt = prompt_new(m, NULL, answers,
3673                                        sizeof(answers)/sizeof(answers[0]),
3674                                        OB_KILL_RESULT_NO, /* default = no */
3675                                        OB_KILL_RESULT_NO, /* cancel = no */
3676                                        client_kill_requested,
3677                                        client_kill_cleanup,
3678                                        self);
3679         g_free(m);
3680     }
3681
3682     prompt_show(self->kill_prompt, self, TRUE);
3683 }
3684
3685 void client_kill(ObClient *self)
3686 {
3687     /* don't kill our own windows */
3688     if (self->prompt) return;
3689
3690     if (client_on_localhost(self) && self->pid) {
3691         /* running on the local host */
3692         if (self->kill_level == 0) {
3693             ob_debug("killing window 0x%x with pid %lu, with SIGTERM",
3694                      self->window, self->pid);
3695             kill(self->pid, SIGTERM);
3696             ++self->kill_level;
3697
3698             /* show that we're trying to kill it */
3699             client_update_title(self);
3700         }
3701         else {
3702             ob_debug("killing window 0x%x with pid %lu, with SIGKILL",
3703                      self->window, self->pid);
3704             kill(self->pid, SIGKILL); /* kill -9 */
3705         }
3706     }
3707     else {
3708         /* running on a remote host */
3709         XKillClient(obt_display, self->window);
3710     }
3711 }
3712
3713 void client_hilite(ObClient *self, gboolean hilite)
3714 {
3715     if (self->demands_attention == hilite)
3716         return; /* no change */
3717
3718     /* don't allow focused windows to hilite */
3719     self->demands_attention = hilite && !client_focused(self);
3720     if (self->frame != NULL) { /* if we're mapping, just set the state */
3721         if (self->demands_attention) {
3722             frame_flash_start(self->frame);
3723
3724             /* if the window is on another desktop then raise it and make it
3725                the most recently used window */
3726             if (self->desktop != screen_desktop &&
3727                 self->desktop != DESKTOP_ALL)
3728             {
3729                 stacking_raise(CLIENT_AS_WINDOW(self));
3730                 focus_order_to_top(self);
3731             }
3732         }
3733         else
3734             frame_flash_stop(self->frame);
3735         client_change_state(self);
3736     }
3737 }
3738
3739 static void client_set_desktop_recursive(ObClient *self,
3740                                          guint target,
3741                                          gboolean donthide,
3742                                          gboolean dontraise)
3743 {
3744     guint old;
3745     GSList *it;
3746
3747     if (target != self->desktop && self->type != OB_CLIENT_TYPE_DESKTOP) {
3748
3749         ob_debug("Setting desktop %u", target+1);
3750
3751         g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
3752
3753         old = self->desktop;
3754         self->desktop = target;
3755         OBT_PROP_SET32(self->window, NET_WM_DESKTOP, CARDINAL, target);
3756         /* the frame can display the current desktop state */
3757         frame_adjust_state(self->frame);
3758         /* 'move' the window to the new desktop */
3759         if (!donthide)
3760             client_hide(self);
3761         client_show(self);
3762         /* raise if it was not already on the desktop */
3763         if (old != DESKTOP_ALL && !dontraise)
3764             stacking_raise(CLIENT_AS_WINDOW(self));
3765         if (STRUT_EXISTS(self->strut))
3766             screen_update_areas();
3767         else
3768             /* the new desktop's geometry may be different, so we may need to
3769                resize, for example if we are maximized */
3770             client_reconfigure(self, FALSE);
3771
3772         focus_cycle_addremove(self, FALSE);
3773     }
3774
3775     /* move all transients */
3776     for (it = self->transients; it; it = g_slist_next(it))
3777         if (it->data != self)
3778             if (client_is_direct_child(self, it->data))
3779                 client_set_desktop_recursive(it->data, target,
3780                                              donthide, dontraise);
3781 }
3782
3783 void client_set_desktop(ObClient *self, guint target,
3784                         gboolean donthide, gboolean dontraise)
3785 {
3786     self = client_search_top_direct_parent(self);
3787     client_set_desktop_recursive(self, target, donthide, dontraise);
3788
3789     focus_cycle_addremove(NULL, TRUE);
3790 }
3791
3792 gboolean client_is_direct_child(ObClient *parent, ObClient *child)
3793 {
3794     while (child != parent && (child = client_direct_parent(child)));
3795     return child == parent;
3796 }
3797
3798 ObClient *client_search_modal_child(ObClient *self)
3799 {
3800     GSList *it;
3801     ObClient *ret;
3802
3803     for (it = self->transients; it; it = g_slist_next(it)) {
3804         ObClient *c = it->data;
3805         if ((ret = client_search_modal_child(c))) return ret;
3806         if (c->modal) return c;
3807     }
3808     return NULL;
3809 }
3810
3811 struct ObClientFindDestroyUnmap {
3812     Window window;
3813     gint ignore_unmaps;
3814 };
3815
3816 static gboolean find_destroy_unmap(XEvent *e, gpointer data)
3817 {
3818     struct ObClientFindDestroyUnmap *find = data;
3819     if (e->type == DestroyNotify)
3820         return e->xdestroywindow.window == find->window;
3821     if (e->type == UnmapNotify && e->xunmap.window == find->window)
3822         /* ignore the first $find->ignore_unmaps$ many unmap events */
3823         return --find->ignore_unmaps < 0;
3824     return FALSE;
3825 }
3826
3827 gboolean client_validate(ObClient *self)
3828 {
3829     struct ObClientFindDestroyUnmap find;
3830
3831     XSync(obt_display, FALSE); /* get all events on the server */
3832
3833     find.window = self->window;
3834     find.ignore_unmaps = self->ignore_unmaps;
3835     if (xqueue_exists_local(find_destroy_unmap, &find))
3836         return FALSE;
3837
3838     return TRUE;
3839 }
3840
3841 void client_set_wm_state(ObClient *self, glong state)
3842 {
3843     if (state == self->wmstate) return; /* no change */
3844
3845     switch (state) {
3846     case IconicState:
3847         client_iconify(self, TRUE, TRUE, FALSE);
3848         break;
3849     case NormalState:
3850         client_iconify(self, FALSE, TRUE, FALSE);
3851         break;
3852     }
3853 }
3854
3855 void client_set_state(ObClient *self, Atom action, glong data1, glong data2)
3856 {
3857     gboolean shaded = self->shaded;
3858     gboolean fullscreen = self->fullscreen;
3859     gboolean undecorated = self->undecorated;
3860     gboolean locked = self->locked;
3861     gboolean max_horz = self->max_horz;
3862     gboolean max_vert = self->max_vert;
3863     gboolean modal = self->modal;
3864     gboolean iconic = self->iconic;
3865     gboolean demands_attention = self->demands_attention;
3866     gboolean above = self->above;
3867     gboolean below = self->below;
3868     gint i;
3869     gboolean value;
3870
3871     if (!(action == OBT_PROP_ATOM(NET_WM_STATE_ADD) ||
3872           action == OBT_PROP_ATOM(NET_WM_STATE_REMOVE) ||
3873           action == OBT_PROP_ATOM(NET_WM_STATE_TOGGLE)))
3874         /* an invalid action was passed to the client message, ignore it */
3875         return;
3876
3877     for (i = 0; i < 2; ++i) {
3878         Atom state = i == 0 ? data1 : data2;
3879
3880         if (!state) continue;
3881
3882         /* if toggling, then pick whether we're adding or removing */
3883         if (action == OBT_PROP_ATOM(NET_WM_STATE_TOGGLE)) {
3884             if (state == OBT_PROP_ATOM(NET_WM_STATE_MODAL))
3885                 value = modal;
3886             else if (state == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_VERT))
3887                 value = self->max_vert;
3888             else if (state == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_HORZ))
3889                 value = self->max_horz;
3890             else if (state == OBT_PROP_ATOM(NET_WM_STATE_SHADED))
3891                 value = shaded;
3892             else if (state == OBT_PROP_ATOM(NET_WM_STATE_SKIP_TASKBAR))
3893                 value = self->skip_taskbar;
3894             else if (state == OBT_PROP_ATOM(NET_WM_STATE_SKIP_PAGER))
3895                 value = self->skip_pager;
3896             else if (state == OBT_PROP_ATOM(NET_WM_STATE_HIDDEN))
3897                 value = self->iconic;
3898             else if (state == OBT_PROP_ATOM(NET_WM_STATE_FULLSCREEN))
3899                 value = fullscreen;
3900             else if (state == OBT_PROP_ATOM(NET_WM_STATE_ABOVE))
3901                 value = self->above;
3902             else if (state == OBT_PROP_ATOM(NET_WM_STATE_BELOW))
3903                 value = self->below;
3904             else if (state == OBT_PROP_ATOM(NET_WM_STATE_DEMANDS_ATTENTION))
3905                 value = self->demands_attention;
3906             else if (state == OBT_PROP_ATOM(OB_WM_STATE_UNDECORATED))
3907                 value = undecorated;
3908             else if (state == OBT_PROP_ATOM(OB_WM_STATE_LOCKED))
3909                 value = locked;
3910             else
3911                 g_assert_not_reached();
3912             action = value ? OBT_PROP_ATOM(NET_WM_STATE_REMOVE) :
3913                              OBT_PROP_ATOM(NET_WM_STATE_ADD);
3914         }
3915
3916         value = action == OBT_PROP_ATOM(NET_WM_STATE_ADD);
3917         if (state == OBT_PROP_ATOM(NET_WM_STATE_MODAL)) {
3918             modal = value;
3919         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_VERT)) {
3920             max_vert = value;
3921         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_HORZ)) {
3922             max_horz = value;
3923         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_SHADED)) {
3924             shaded = value;
3925         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_SKIP_TASKBAR)) {
3926             self->skip_taskbar = value;
3927         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_SKIP_PAGER)) {
3928             self->skip_pager = value;
3929         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_HIDDEN)) {
3930             iconic = value;
3931         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_FULLSCREEN)) {
3932             fullscreen = value;
3933         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_ABOVE)) {
3934             above = value;
3935             /* only unset below when setting above, otherwise you can't get to
3936                the normal layer */
3937             if (value)
3938                 below = FALSE;
3939         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_BELOW)) {
3940             /* and vice versa */
3941             if (value)
3942                 above = FALSE;
3943             below = value;
3944         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_DEMANDS_ATTENTION)){
3945             demands_attention = value;
3946         } else if (state == OBT_PROP_ATOM(OB_WM_STATE_UNDECORATED)) {
3947             undecorated = value;
3948         } else if (state == OBT_PROP_ATOM(OB_WM_STATE_LOCKED)) {
3949             locked = value;
3950         }
3951     }
3952
3953     if (max_horz != self->max_horz || max_vert != self->max_vert) {
3954         if (max_horz != self->max_horz && max_vert != self->max_vert) {
3955             /* toggling both */
3956             if (max_horz == max_vert) { /* both going the same way */
3957                 client_maximize(self, max_horz, 0);
3958             } else {
3959                 client_maximize(self, max_horz, 1);
3960                 client_maximize(self, max_vert, 2);
3961             }
3962         } else {
3963             /* toggling one */
3964             if (max_horz != self->max_horz)
3965                 client_maximize(self, max_horz, 1);
3966             else
3967                 client_maximize(self, max_vert, 2);
3968         }
3969     }
3970     /* change fullscreen state before shading, as it will affect if the window
3971        can shade or not */
3972     if (fullscreen != self->fullscreen)
3973         client_fullscreen(self, fullscreen);
3974     if (shaded != self->shaded)
3975         client_shade(self, shaded);
3976     if (undecorated != self->undecorated)
3977         client_set_undecorated(self, undecorated);
3978     if (locked != self->locked)
3979         client_set_locked(self, locked);
3980     if (above != self->above || below != self->below) {
3981         self->above = above;
3982         self->below = below;
3983         client_calc_layer(self);
3984     }
3985
3986     if (modal != self->modal) {
3987         self->modal = modal;
3988         /* when a window changes modality, then its stacking order with its
3989            transients needs to change */
3990         stacking_raise(CLIENT_AS_WINDOW(self));
3991
3992         /* it also may get focused. if something is focused that shouldn't
3993            be focused anymore, then move the focus */
3994         if (focus_client && client_focus_target(focus_client) != focus_client)
3995             client_focus(focus_client);
3996     }
3997
3998     if (iconic != self->iconic)
3999         client_iconify(self, iconic, FALSE, FALSE);
4000
4001     if (demands_attention != self->demands_attention)
4002         client_hilite(self, demands_attention);
4003
4004     client_change_state(self); /* change the hint to reflect these changes */
4005
4006     focus_cycle_addremove(self, TRUE);
4007 }
4008
4009 ObClient *client_focus_target(ObClient *self)
4010 {
4011     ObClient *child = NULL;
4012
4013     child = client_search_modal_child(self);
4014     if (child) return child;
4015     return self;
4016 }
4017
4018 gboolean client_can_focus(ObClient *self)
4019 {
4020     /* choose the correct target */
4021     self = client_focus_target(self);
4022
4023     if (!self->frame->visible)
4024         return FALSE;
4025
4026     if (!(self->can_focus || self->focus_notify))
4027         return FALSE;
4028
4029     return TRUE;
4030 }
4031
4032 gboolean client_focus(ObClient *self)
4033 {
4034     if (!client_validate(self)) return FALSE;
4035
4036     /* we might not focus this window, so if we have modal children which would
4037        be focused instead, bring them to this desktop */
4038     client_bring_modal_windows(self);
4039
4040     /* choose the correct target */
4041     self = client_focus_target(self);
4042
4043     if (!client_can_focus(self)) {
4044         ob_debug_type(OB_DEBUG_FOCUS,
4045                       "Client %s can't be focused", self->title);
4046         return FALSE;
4047     }
4048
4049     /* if we have helper windows they should be there with the window */
4050     client_bring_helper_windows(self);
4051
4052     ob_debug_type(OB_DEBUG_FOCUS,
4053                   "Focusing client \"%s\" (0x%x) at time %u",
4054                   self->title, self->window, event_time());
4055
4056     /* if using focus_delay, stop the timer now so that focus doesn't
4057        go moving on us */
4058     event_halt_focus_delay();
4059
4060     obt_display_ignore_errors(TRUE);
4061
4062     if (self->can_focus) {
4063         /* This can cause a BadMatch error with CurrentTime, or if an app
4064            passed in a bad time for _NET_WM_ACTIVE_WINDOW. */
4065         XSetInputFocus(obt_display, self->window, RevertToPointerRoot,
4066                        event_time());
4067     }
4068
4069     if (self->focus_notify) {
4070         XEvent ce;
4071         ce.xclient.type = ClientMessage;
4072         ce.xclient.message_type = OBT_PROP_ATOM(WM_PROTOCOLS);
4073         ce.xclient.display = obt_display;
4074         ce.xclient.window = self->window;
4075         ce.xclient.format = 32;
4076         ce.xclient.data.l[0] = OBT_PROP_ATOM(WM_TAKE_FOCUS);
4077         ce.xclient.data.l[1] = event_time();
4078         ce.xclient.data.l[2] = 0l;
4079         ce.xclient.data.l[3] = 0l;
4080         ce.xclient.data.l[4] = 0l;
4081         XSendEvent(obt_display, self->window, FALSE, NoEventMask, &ce);
4082     }
4083
4084     obt_display_ignore_errors(FALSE);
4085
4086     ob_debug_type(OB_DEBUG_FOCUS, "Error focusing? %d",
4087                   obt_display_error_occured);
4088     return !obt_display_error_occured;
4089 }
4090
4091 static void client_present(ObClient *self, gboolean here, gboolean raise,
4092                            gboolean unshade)
4093 {
4094     if (client_normal(self) && screen_showing_desktop())
4095         screen_show_desktop(FALSE, self);
4096     if (self->iconic)
4097         client_iconify(self, FALSE, here, FALSE);
4098     if (self->desktop != DESKTOP_ALL &&
4099         self->desktop != screen_desktop)
4100     {
4101         if (here)
4102             client_set_desktop(self, screen_desktop, FALSE, TRUE);
4103         else
4104             screen_set_desktop(self->desktop, FALSE);
4105     } else if (!self->frame->visible)
4106         /* if its not visible for other reasons, then don't mess
4107            with it */
4108         return;
4109     if (self->shaded && unshade)
4110         client_shade(self, FALSE);
4111     if (raise)
4112         stacking_raise(CLIENT_AS_WINDOW(self));
4113
4114     client_focus(self);
4115 }
4116
4117 /* this function exists to map to the net_active_window message in the ewmh */
4118 void client_activate(ObClient *self, gboolean desktop,
4119                      gboolean here, gboolean raise,
4120                      gboolean unshade, gboolean user)
4121 {
4122     self = client_focus_target(self);
4123
4124     if (client_can_steal_focus(self, desktop, user, event_time(), CurrentTime))
4125         client_present(self, here, raise, unshade);
4126     else
4127         client_hilite(self, TRUE);
4128 }
4129
4130 static void client_bring_windows_recursive(ObClient *self,
4131                                            guint desktop,
4132                                            gboolean helpers,
4133                                            gboolean modals,
4134                                            gboolean iconic)
4135 {
4136     GSList *it;
4137
4138     for (it = self->transients; it; it = g_slist_next(it))
4139         client_bring_windows_recursive(it->data, desktop,
4140                                        helpers, modals, iconic);
4141
4142     if (((helpers && client_helper(self)) ||
4143          (modals && self->modal)) &&
4144         (!screen_compare_desktops(self->desktop, desktop) ||
4145          (iconic && self->iconic)))
4146     {
4147         if (iconic && self->iconic)
4148             client_iconify(self, FALSE, TRUE, FALSE);
4149         else
4150             client_set_desktop(self, desktop, FALSE, FALSE);
4151     }
4152 }
4153
4154 void client_bring_helper_windows(ObClient *self)
4155 {
4156     client_bring_windows_recursive(self, self->desktop, TRUE, FALSE, FALSE);
4157 }
4158
4159 void client_bring_modal_windows(ObClient *self)
4160 {
4161     client_bring_windows_recursive(self, self->desktop, FALSE, TRUE, TRUE);
4162 }
4163
4164 gboolean client_focused(ObClient *self)
4165 {
4166     return self == focus_client;
4167 }
4168
4169 RrImage* client_icon(ObClient *self)
4170 {
4171     RrImage *ret = NULL;
4172
4173     if (self->icon_set)
4174         ret = self->icon_set;
4175     else if (self->parents) {
4176         GSList *it;
4177         for (it = self->parents; it && !ret; it = g_slist_next(it))
4178             ret = client_icon(it->data);
4179     }
4180     if (!ret)
4181         ret = client_default_icon;
4182     return ret;
4183 }
4184
4185 void client_set_layer(ObClient *self, gint layer)
4186 {
4187     if (layer < 0) {
4188         self->below = TRUE;
4189         self->above = FALSE;
4190     } else if (layer == 0) {
4191         self->below = self->above = FALSE;
4192     } else {
4193         self->below = FALSE;
4194         self->above = TRUE;
4195     }
4196     client_calc_layer(self);
4197     client_change_state(self); /* reflect this in the state hints */
4198 }
4199
4200 void client_set_locked(ObClient *self, gboolean locked)
4201 {
4202     if (self->locked != locked) {
4203         self->locked = locked;
4204         client_change_state(self);
4205     }
4206 }
4207
4208 void client_set_undecorated(ObClient *self, gboolean undecorated)
4209 {
4210     if (self->undecorated != undecorated &&
4211         /* don't let it undecorate if the function is missing, but let
4212            it redecorate */
4213         (self->functions & OB_CLIENT_FUNC_UNDECORATE || !undecorated))
4214     {
4215         self->undecorated = undecorated;
4216         client_setup_decor_and_functions(self, TRUE);
4217         client_change_state(self); /* reflect this in the state hints */
4218     }
4219 }
4220
4221 guint client_monitor(ObClient *self)
4222 {
4223     return screen_find_monitor(&self->frame->area);
4224 }
4225
4226 ObClient *client_direct_parent(ObClient *self)
4227 {
4228     if (!self->parents) return NULL;
4229     if (self->transient_for_group) return NULL;
4230     return self->parents->data;
4231 }
4232
4233 ObClient *client_search_top_direct_parent(ObClient *self)
4234 {
4235     ObClient *p;
4236     while ((p = client_direct_parent(self))) self = p;
4237     return self;
4238 }
4239
4240 static GSList *client_search_all_top_parents_internal(ObClient *self,
4241                                                       gboolean bylayer,
4242                                                       ObStackingLayer layer)
4243 {
4244     GSList *ret;
4245     ObClient *p;
4246
4247     /* move up the direct transient chain as far as possible */
4248     while ((p = client_direct_parent(self)) &&
4249            (!bylayer || p->layer == layer))
4250         self = p;
4251
4252     if (!self->parents)
4253         ret = g_slist_prepend(NULL, self);
4254     else
4255         ret = g_slist_copy(self->parents);
4256
4257     return ret;
4258 }
4259
4260 GSList *client_search_all_top_parents(ObClient *self)
4261 {
4262     return client_search_all_top_parents_internal(self, FALSE, 0);
4263 }
4264
4265 GSList *client_search_all_top_parents_layer(ObClient *self)
4266 {
4267     return client_search_all_top_parents_internal(self, TRUE, self->layer);
4268 }
4269
4270 ObClient *client_search_focus_parent(ObClient *self)
4271 {
4272     GSList *it;
4273
4274     for (it = self->parents; it; it = g_slist_next(it))
4275         if (client_focused(it->data)) return it->data;
4276
4277     return NULL;
4278 }
4279
4280 ObClient *client_search_focus_parent_full(ObClient *self)
4281 {
4282     GSList *it;
4283     ObClient *ret = NULL;
4284
4285     for (it = self->parents; it; it = g_slist_next(it)) {
4286         if (client_focused(it->data))
4287             ret = it->data;
4288         else
4289             ret = client_search_focus_parent_full(it->data);
4290         if (ret) break;
4291     }
4292     return ret;
4293 }
4294
4295 ObClient *client_search_parent(ObClient *self, ObClient *search)
4296 {
4297     GSList *it;
4298
4299     for (it = self->parents; it; it = g_slist_next(it))
4300         if (it->data == search) return search;
4301
4302     return NULL;
4303 }
4304
4305 ObClient *client_search_transient(ObClient *self, ObClient *search)
4306 {
4307     GSList *sit;
4308
4309     for (sit = self->transients; sit; sit = g_slist_next(sit)) {
4310         if (sit->data == search)
4311             return search;
4312         if (client_search_transient(sit->data, search))
4313             return search;
4314     }
4315     return NULL;
4316 }
4317
4318 static void detect_edge(Rect area, ObDirection dir,
4319                         gint my_head, gint my_size,
4320                         gint my_edge_start, gint my_edge_size,
4321                         gint *dest, gboolean *near_edge)
4322 {
4323     gint edge_start, edge_size, head, tail;
4324     gboolean skip_head = FALSE, skip_tail = FALSE;
4325
4326     switch (dir) {
4327         case OB_DIRECTION_NORTH:
4328         case OB_DIRECTION_SOUTH:
4329             edge_start = area.x;
4330             edge_size = area.width;
4331             break;
4332         case OB_DIRECTION_EAST:
4333         case OB_DIRECTION_WEST:
4334             edge_start = area.y;
4335             edge_size = area.height;
4336             break;
4337         default:
4338             g_assert_not_reached();
4339     }
4340
4341     /* do we collide with this window? */
4342     if (!RANGES_INTERSECT(my_edge_start, my_edge_size,
4343                 edge_start, edge_size))
4344         return;
4345
4346     switch (dir) {
4347         case OB_DIRECTION_NORTH:
4348             head = RECT_BOTTOM(area);
4349             tail = RECT_TOP(area);
4350             break;
4351         case OB_DIRECTION_SOUTH:
4352             head = RECT_TOP(area);
4353             tail = RECT_BOTTOM(area);
4354             break;
4355         case OB_DIRECTION_WEST:
4356             head = RECT_RIGHT(area);
4357             tail = RECT_LEFT(area);
4358             break;
4359         case OB_DIRECTION_EAST:
4360             head = RECT_LEFT(area);
4361             tail = RECT_RIGHT(area);
4362             break;
4363         default:
4364             g_assert_not_reached();
4365     }
4366     switch (dir) {
4367         case OB_DIRECTION_NORTH:
4368         case OB_DIRECTION_WEST:
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         case OB_DIRECTION_SOUTH:
4387         case OB_DIRECTION_EAST:
4388             /* check if our window is past the head of this window */
4389             if (my_head >= head - 1)
4390                 skip_head = TRUE;
4391             /* check if our window's tail is past the tail of this window */
4392             if (my_head - my_size + 1 >= tail)
4393                 skip_tail = TRUE;
4394             /* check if the head of this window is closer than the previously
4395                chosen edge (take into account that the previously chosen
4396                edge might have been a tail, not a head) */
4397             if (head - (*near_edge ? 0 : my_size) >= *dest)
4398                 skip_head = TRUE;
4399             /* check if the tail of this window is closer than the previously
4400                chosen edge (take into account that the previously chosen
4401                edge might have been a head, not a tail) */
4402             if (tail + (!*near_edge ? 0 : my_size) >= *dest)
4403                 skip_tail = TRUE;
4404             break;
4405         default:
4406             g_assert_not_reached();
4407     }
4408
4409     ob_debug("my head %d size %d", my_head, my_size);
4410     ob_debug("head %d tail %d dest %d", head, tail, *dest);
4411     if (!skip_head) {
4412         ob_debug("using near edge %d", head);
4413         *dest = head;
4414         *near_edge = TRUE;
4415     }
4416     else if (!skip_tail) {
4417         ob_debug("using far edge %d", tail);
4418         *dest = tail;
4419         *near_edge = FALSE;
4420     }
4421 }
4422
4423 void client_find_edge_directional(ObClient *self, ObDirection dir,
4424                                   gint my_head, gint my_size,
4425                                   gint my_edge_start, gint my_edge_size,
4426                                   gint *dest, gboolean *near_edge)
4427 {
4428     GList *it;
4429     Rect *a;
4430     Rect dock_area;
4431     gint edge;
4432     guint i;
4433
4434     a = screen_area(self->desktop, SCREEN_AREA_ALL_MONITORS,
4435                     &self->frame->area);
4436
4437     switch (dir) {
4438     case OB_DIRECTION_NORTH:
4439         edge = RECT_TOP(*a) - 1;
4440         break;
4441     case OB_DIRECTION_SOUTH:
4442         edge = RECT_BOTTOM(*a) + 1;
4443         break;
4444     case OB_DIRECTION_EAST:
4445         edge = RECT_RIGHT(*a) + 1;
4446         break;
4447     case OB_DIRECTION_WEST:
4448         edge = RECT_LEFT(*a) - 1;
4449         break;
4450     default:
4451         g_assert_not_reached();
4452     }
4453     /* default to the far edge, then narrow it down */
4454     *dest = edge;
4455     *near_edge = TRUE;
4456
4457     /* search for edges of monitors */
4458     for (i = 0; i < screen_num_monitors; ++i) {
4459         Rect *area = screen_area(self->desktop, i, NULL);
4460         detect_edge(*area, dir, my_head, my_size, my_edge_start,
4461                     my_edge_size, dest, near_edge);
4462         g_slice_free(Rect, area);
4463     }
4464
4465     /* search for edges of clients */
4466     for (it = client_list; it; it = g_list_next(it)) {
4467         ObClient *cur = it->data;
4468
4469         /* skip windows to not bump into */
4470         if (cur == self)
4471             continue;
4472         if (cur->iconic)
4473             continue;
4474         if (self->desktop != cur->desktop && cur->desktop != DESKTOP_ALL &&
4475             cur->desktop != screen_desktop)
4476             continue;
4477
4478         ob_debug("trying window %s", cur->title);
4479
4480         detect_edge(cur->frame->area, dir, my_head, my_size, my_edge_start,
4481                     my_edge_size, dest, near_edge);
4482     }
4483     dock_get_area(&dock_area);
4484     detect_edge(dock_area, dir, my_head, my_size, my_edge_start,
4485                 my_edge_size, dest, near_edge);
4486
4487     g_slice_free(Rect, a);
4488 }
4489
4490 void client_find_move_directional(ObClient *self, ObDirection dir,
4491                                   gint *x, gint *y)
4492 {
4493     gint head, size;
4494     gint e, e_start, e_size;
4495     gboolean near;
4496
4497     switch (dir) {
4498     case OB_DIRECTION_EAST:
4499         head = RECT_RIGHT(self->frame->area);
4500         size = self->frame->area.width;
4501         e_start = RECT_TOP(self->frame->area);
4502         e_size = self->frame->area.height;
4503         break;
4504     case OB_DIRECTION_WEST:
4505         head = RECT_LEFT(self->frame->area);
4506         size = self->frame->area.width;
4507         e_start = RECT_TOP(self->frame->area);
4508         e_size = self->frame->area.height;
4509         break;
4510     case OB_DIRECTION_NORTH:
4511         head = RECT_TOP(self->frame->area);
4512         size = self->frame->area.height;
4513         e_start = RECT_LEFT(self->frame->area);
4514         e_size = self->frame->area.width;
4515         break;
4516     case OB_DIRECTION_SOUTH:
4517         head = RECT_BOTTOM(self->frame->area);
4518         size = self->frame->area.height;
4519         e_start = RECT_LEFT(self->frame->area);
4520         e_size = self->frame->area.width;
4521         break;
4522     default:
4523         g_assert_not_reached();
4524     }
4525
4526     client_find_edge_directional(self, dir, head, size,
4527                                  e_start, e_size, &e, &near);
4528     *x = self->frame->area.x;
4529     *y = self->frame->area.y;
4530     switch (dir) {
4531     case OB_DIRECTION_EAST:
4532         if (near) e -= self->frame->area.width;
4533         else      e++;
4534         *x = e;
4535         break;
4536     case OB_DIRECTION_WEST:
4537         if (near) e++;
4538         else      e -= self->frame->area.width;
4539         *x = e;
4540         break;
4541     case OB_DIRECTION_NORTH:
4542         if (near) e++;
4543         else      e -= self->frame->area.height;
4544         *y = e;
4545         break;
4546     case OB_DIRECTION_SOUTH:
4547         if (near) e -= self->frame->area.height;
4548         else      e++;
4549         *y = e;
4550         break;
4551     default:
4552         g_assert_not_reached();
4553     }
4554     frame_frame_gravity(self->frame, x, y);
4555 }
4556
4557 void client_find_resize_directional(ObClient *self,
4558                                     ObDirection side,
4559                                     ObClientDirectionalResizeType resize_type,
4560                                     gint *x, gint *y, gint *w, gint *h)
4561 {
4562     gint head;
4563     gint e, e_start, e_size, delta;
4564     gboolean near;
4565     ObDirection dir;
4566
4567     gboolean grow;
4568     switch (resize_type) {
4569     case CLIENT_RESIZE_GROW:
4570         grow = TRUE;
4571         break;
4572     case CLIENT_RESIZE_GROW_IF_NOT_ON_EDGE:
4573         grow = TRUE;
4574         break;
4575     case CLIENT_RESIZE_SHRINK:
4576         grow = FALSE;
4577         break;
4578     }
4579
4580     switch (side) {
4581     case OB_DIRECTION_EAST:
4582         head = RECT_RIGHT(self->frame->area);
4583         switch (resize_type) {
4584         case CLIENT_RESIZE_GROW:
4585             head += self->size_inc.width - 1;
4586             break;
4587         case CLIENT_RESIZE_GROW_IF_NOT_ON_EDGE:
4588             head -= 1;
4589             break;
4590         case CLIENT_RESIZE_SHRINK:
4591             break;
4592         }
4593
4594         e_start = RECT_TOP(self->frame->area);
4595         e_size = self->frame->area.height;
4596         dir = grow ? OB_DIRECTION_EAST : OB_DIRECTION_WEST;
4597         break;
4598     case OB_DIRECTION_WEST:
4599         head = RECT_LEFT(self->frame->area);
4600         switch (resize_type) {
4601         case CLIENT_RESIZE_GROW:
4602             head -= self->size_inc.width - 1;
4603             break;
4604         case CLIENT_RESIZE_GROW_IF_NOT_ON_EDGE:
4605             head += 1;
4606             break;
4607         case CLIENT_RESIZE_SHRINK:
4608             break;
4609         }
4610
4611         e_start = RECT_TOP(self->frame->area);
4612         e_size = self->frame->area.height;
4613         dir = grow ? OB_DIRECTION_WEST : OB_DIRECTION_EAST;
4614         break;
4615     case OB_DIRECTION_NORTH:
4616         head = RECT_TOP(self->frame->area);
4617         switch (resize_type) {
4618         case CLIENT_RESIZE_GROW:
4619             head -= self->size_inc.height - 1;
4620             break;
4621         case CLIENT_RESIZE_GROW_IF_NOT_ON_EDGE:
4622             head += 1;
4623             break;
4624         case CLIENT_RESIZE_SHRINK:
4625             break;
4626         }
4627
4628         e_start = RECT_LEFT(self->frame->area);
4629         e_size = self->frame->area.width;
4630         dir = grow ? OB_DIRECTION_NORTH : OB_DIRECTION_SOUTH;
4631         break;
4632     case OB_DIRECTION_SOUTH:
4633         head = RECT_BOTTOM(self->frame->area);
4634         switch (resize_type) {
4635         case CLIENT_RESIZE_GROW:
4636             head += self->size_inc.height - 1;
4637             break;
4638         case CLIENT_RESIZE_GROW_IF_NOT_ON_EDGE:
4639             head -= 1;
4640             break;
4641         case CLIENT_RESIZE_SHRINK:
4642             break;
4643         }
4644
4645         e_start = RECT_LEFT(self->frame->area);
4646         e_size = self->frame->area.width;
4647         dir = grow ? OB_DIRECTION_SOUTH : OB_DIRECTION_NORTH;
4648         break;
4649     default:
4650         g_assert_not_reached();
4651     }
4652
4653     ob_debug("head %d dir %d", head, dir);
4654     client_find_edge_directional(self, dir, head, 1,
4655                                  e_start, e_size, &e, &near);
4656     ob_debug("edge %d", e);
4657     *x = self->frame->area.x;
4658     *y = self->frame->area.y;
4659     *w = self->frame->area.width;
4660     *h = self->frame->area.height;
4661     switch (side) {
4662     case OB_DIRECTION_EAST:
4663         if (grow == near) --e;
4664         delta = e - RECT_RIGHT(self->frame->area);
4665         *w += delta;
4666         break;
4667     case OB_DIRECTION_WEST:
4668         if (grow == near) ++e;
4669         delta = RECT_LEFT(self->frame->area) - e;
4670         *x -= delta;
4671         *w += delta;
4672         break;
4673     case OB_DIRECTION_NORTH:
4674         if (grow == near) ++e;
4675         delta = RECT_TOP(self->frame->area) - e;
4676         *y -= delta;
4677         *h += delta;
4678         break;
4679     case OB_DIRECTION_SOUTH:
4680         if (grow == near) --e;
4681         delta = e - RECT_BOTTOM(self->frame->area);
4682         *h += delta;
4683        break;
4684     default:
4685         g_assert_not_reached();
4686     }
4687     frame_frame_gravity(self->frame, x, y);
4688     *w -= self->frame->size.left + self->frame->size.right;
4689     *h -= self->frame->size.top + self->frame->size.bottom;
4690 }
4691
4692 ObClient* client_under_pointer(void)
4693 {
4694     gint x, y;
4695     GList *it;
4696     ObClient *ret = NULL;
4697
4698     if (screen_pointer_pos(&x, &y)) {
4699         for (it = stacking_list; it; it = g_list_next(it)) {
4700             if (WINDOW_IS_CLIENT(it->data)) {
4701                 ObClient *c = WINDOW_AS_CLIENT(it->data);
4702                 if (c->frame->visible &&
4703                     /* check the desktop, this is done during desktop
4704                        switching and windows are shown/hidden status is not
4705                        reliable */
4706                     (c->desktop == screen_desktop ||
4707                      c->desktop == DESKTOP_ALL) &&
4708                     /* ignore all animating windows */
4709                     !frame_iconify_animating(c->frame) &&
4710                     RECT_CONTAINS(c->frame->area, x, y))
4711                 {
4712                     ret = c;
4713                     break;
4714                 }
4715             }
4716         }
4717     }
4718     return ret;
4719 }
4720
4721 gboolean client_has_group_siblings(ObClient *self)
4722 {
4723     return self->group && self->group->members->next;
4724 }
4725
4726 gboolean client_has_relative(ObClient *self)
4727 {
4728     return client_has_parent(self) ||
4729         client_has_group_siblings(self) ||
4730         client_has_children(self);
4731 }
4732
4733 /*! Returns TRUE if the client is running on the same machine as Openbox */
4734 gboolean client_on_localhost(ObClient *self)
4735 {
4736     return self->client_machine == NULL;
4737 }