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