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