check them startupnotify-provided wmclass against both parts of a window's wm_class...
[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 "xerror.h"
25 #include "screen.h"
26 #include "moveresize.h"
27 #include "ping.h"
28 #include "place.h"
29 #include "prop.h"
30 #include "extensions.h"
31 #include "frame.h"
32 #include "session.h"
33 #include "event.h"
34 #include "grab.h"
35 #include "focus.h"
36 #include "stacking.h"
37 #include "openbox.h"
38 #include "group.h"
39 #include "config.h"
40 #include "menuframe.h"
41 #include "keyboard.h"
42 #include "mouse.h"
43 #include "render/render.h"
44 #include "gettext.h"
45
46 #ifdef HAVE_UNISTD_H
47 #  include <unistd.h>
48 #endif
49
50 #ifdef HAVE_SIGNAL_H
51 #  include <signal.h> /* for kill() */
52 #endif
53
54 #include <glib.h>
55 #include <X11/Xutil.h>
56
57 /*! The event mask to grab on client windows */
58 #define CLIENT_EVENTMASK (PropertyChangeMask | StructureNotifyMask | \
59                           ColormapChangeMask)
60
61 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
62                                 ButtonMotionMask)
63
64 typedef struct
65 {
66     ObClientCallback func;
67     gpointer data;
68 } ClientCallback;
69
70 GList            *client_list          = NULL;
71
72 static GSList *client_destroy_notifies = NULL;
73
74 static void client_get_all(ObClient *self, gboolean real);
75 static void client_get_startup_id(ObClient *self);
76 static void client_get_session_ids(ObClient *self);
77 static void client_get_area(ObClient *self);
78 static void client_get_desktop(ObClient *self);
79 static void client_get_state(ObClient *self);
80 static void client_get_shaped(ObClient *self);
81 static void client_get_mwm_hints(ObClient *self);
82 static void client_get_colormap(ObClient *self);
83 static void client_set_desktop_recursive(ObClient *self,
84                                          guint target,
85                                          gboolean donthide,
86                                          gboolean dontraise);
87 static void client_change_allowed_actions(ObClient *self);
88 static void client_change_state(ObClient *self);
89 static void client_change_wm_state(ObClient *self);
90 static void client_apply_startup_state(ObClient *self,
91                                        gint x, gint y, gint w, gint h);
92 static void client_restore_session_state(ObClient *self);
93 static gboolean client_restore_session_stacking(ObClient *self);
94 static ObAppSettings *client_get_settings_state(ObClient *self);
95 static void client_update_transient_tree(ObClient *self,
96                                          ObGroup *oldgroup, ObGroup *newgroup,
97                                          gboolean oldgtran, gboolean newgtran,
98                                          ObClient* oldparent,
99                                          ObClient *newparent);
100 static void client_present(ObClient *self, gboolean here, gboolean raise,
101                            gboolean unshade);
102 static GSList *client_search_all_top_parents_internal(ObClient *self,
103                                                       gboolean bylayer,
104                                                       ObStackingLayer layer);
105 static void client_call_notifies(ObClient *self, GSList *list);
106 static void client_ping_event(ObClient *self, gboolean dead);
107
108
109 void client_startup(gboolean reconfig)
110 {
111     if (reconfig) return;
112
113     client_set_list();
114 }
115
116 void client_shutdown(gboolean reconfig)
117 {
118     if (reconfig) return;
119 }
120
121 static void client_call_notifies(ObClient *self, GSList *list)
122 {
123     GSList *it;
124
125     for (it = list; it; it = g_slist_next(it)) {
126         ClientCallback *d = it->data;
127         d->func(self, d->data);
128     }
129 }
130
131 void client_add_destroy_notify(ObClientCallback func, gpointer data)
132 {
133     ClientCallback *d = g_new(ClientCallback, 1);
134     d->func = func;
135     d->data = data;
136     client_destroy_notifies = g_slist_prepend(client_destroy_notifies, d);
137 }
138
139 void client_remove_destroy_notify(ObClientCallback func)
140 {
141     GSList *it;
142
143     for (it = client_destroy_notifies; it; it = g_slist_next(it)) {
144         ClientCallback *d = it->data;
145         if (d->func == func) {
146             g_free(d);
147             client_destroy_notifies =
148                 g_slist_delete_link(client_destroy_notifies, it);
149             break;
150         }
151     }
152 }
153
154 void client_set_list(void)
155 {
156     Window *windows, *win_it;
157     GList *it;
158     guint size = g_list_length(client_list);
159
160     /* create an array of the window ids */
161     if (size > 0) {
162         windows = g_new(Window, size);
163         win_it = windows;
164         for (it = client_list; it; it = g_list_next(it), ++win_it)
165             *win_it = ((ObClient*)it->data)->window;
166     } else
167         windows = NULL;
168
169     PROP_SETA32(RootWindow(ob_display, ob_screen),
170                 net_client_list, window, (gulong*)windows, size);
171
172     if (windows)
173         g_free(windows);
174
175     stacking_set_list();
176 }
177
178 void client_manage_all(void)
179 {
180     guint i, j, nchild;
181     Window w, *children;
182     XWMHints *wmhints;
183     XWindowAttributes attrib;
184
185     XQueryTree(ob_display, RootWindow(ob_display, ob_screen),
186                &w, &w, &children, &nchild);
187
188     /* remove all icon windows from the list */
189     for (i = 0; i < nchild; i++) {
190         if (children[i] == None) continue;
191         wmhints = XGetWMHints(ob_display, children[i]);
192         if (wmhints) {
193             if ((wmhints->flags & IconWindowHint) &&
194                 (wmhints->icon_window != children[i]))
195                 for (j = 0; j < nchild; j++)
196                     if (children[j] == wmhints->icon_window) {
197                         children[j] = None;
198                         break;
199                     }
200             XFree(wmhints);
201         }
202     }
203
204     for (i = 0; i < nchild; ++i) {
205         if (children[i] == None)
206             continue;
207         if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
208             if (attrib.override_redirect) continue;
209
210             if (attrib.map_state != IsUnmapped)
211                 client_manage(children[i]);
212         }
213     }
214     XFree(children);
215 }
216
217 void client_manage(Window window)
218 {
219     ObClient *self;
220     XEvent e;
221     XWindowAttributes attrib;
222     XSetWindowAttributes attrib_set;
223     XWMHints *wmhint;
224     gboolean activate = FALSE;
225     ObAppSettings *settings;
226     gboolean transient = FALSE;
227     Rect place, *monitor;
228     Time launch_time, map_time;
229
230     grab_server(TRUE);
231
232     /* check if it has already been unmapped by the time we started
233        mapping. the grab does a sync so we don't have to here */
234     if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
235         XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e))
236     {
237         XPutBackEvent(ob_display, &e);
238
239         ob_debug("Trying to manage unmapped window. Aborting that.\n");
240         grab_server(FALSE);
241         return; /* don't manage it */
242     }
243
244     /* make sure it isn't an override-redirect window */
245     if (!XGetWindowAttributes(ob_display, window, &attrib) ||
246         attrib.override_redirect)
247     {
248         grab_server(FALSE);
249         return; /* don't manage it */
250     }
251
252     /* is the window a docking app */
253     if ((wmhint = XGetWMHints(ob_display, window))) {
254         if ((wmhint->flags & StateHint) &&
255             wmhint->initial_state == WithdrawnState)
256         {
257             dock_add(window, wmhint);
258             grab_server(FALSE);
259             XFree(wmhint);
260             return;
261         }
262         XFree(wmhint);
263     }
264
265     ob_debug("Managing window: 0x%lx\n", window);
266
267     map_time = event_get_server_time();
268
269     /* choose the events we want to receive on the CLIENT window */
270     attrib_set.event_mask = CLIENT_EVENTMASK;
271     attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
272     XChangeWindowAttributes(ob_display, window,
273                             CWEventMask|CWDontPropagate, &attrib_set);
274
275     /* create the ObClient struct, and populate it from the hints on the
276        window */
277     self = g_new0(ObClient, 1);
278     self->obwin.type = Window_Client;
279     self->window = window;
280
281     /* non-zero defaults */
282     self->wmstate = WithdrawnState; /* make sure it gets updated first time */
283     self->gravity = NorthWestGravity;
284     self->desktop = screen_num_desktops; /* always an invalid value */
285
286     /* get all the stuff off the window */
287     client_get_all(self, TRUE);
288
289     ob_debug("Window type: %d\n", self->type);
290     ob_debug("Window group: 0x%x\n", self->group?self->group->leader:0);
291
292     /* specify that if we exit, the window should not be destroyed and
293        should be reparented back to root automatically */
294     XChangeSaveSet(ob_display, window, SetModeInsert);
295
296     /* create the decoration frame for the client window */
297     self->frame = frame_new(self);
298
299     frame_grab_client(self->frame);
300
301     /* we've grabbed everything and set everything that we need to at mapping
302        time now */
303     grab_server(FALSE);
304
305     /* per-app settings override stuff from client_get_all, and return the
306        settings for other uses too. the returned settings is a shallow copy,
307        that needs to be freed with g_free(). */
308     settings = client_get_settings_state(self);
309     /* the session should get the last say though */
310     client_restore_session_state(self);
311
312     /* now we have all of the window's information so we can set this up */
313     client_setup_decor_and_functions(self, FALSE);
314
315     /* tell startup notification that this app started */
316     launch_time = sn_app_started(self->startup_id, self->class, self->name);
317
318     /* do this after we have a frame.. it uses the frame to help determine the
319        WM_STATE to apply. */
320     client_change_state(self);
321
322     /* add ourselves to the focus order */
323     focus_order_add_new(self);
324
325     /* do this to add ourselves to the stacking list in a non-intrusive way */
326     client_calc_layer(self);
327
328     /* focus the new window? */
329     if (ob_state() != OB_STATE_STARTING &&
330         (!self->session || self->session->focused) &&
331         /* this means focus=true for window is same as config_focus_new=true */
332         ((config_focus_new || (settings && settings->focus == 1)) ||
333          client_search_focus_tree_full(self)) &&
334         /* this checks for focus=false for the window */
335         (!settings || settings->focus != 0) &&
336         focus_valid_target(self, FALSE, FALSE, TRUE, FALSE, FALSE))
337     {
338         activate = TRUE;
339     }
340
341     /* remove the client's border */
342     XSetWindowBorderWidth(ob_display, self->window, 0);
343
344     /* adjust the frame to the client's size before showing or placing
345        the window */
346     frame_adjust_area(self->frame, FALSE, TRUE, FALSE);
347     frame_adjust_client_area(self->frame);
348
349     /* where the frame was placed is where the window was originally */
350     place = self->area;
351     monitor = screen_physical_area_monitor(screen_find_monitor(&place));
352
353     /* figure out placement for the window if the window is new */
354     if (ob_state() == OB_STATE_RUNNING) {
355         ob_debug("Positioned: %s @ %d %d\n",
356                  (!self->positioned ? "no" :
357                   (self->positioned == PPosition ? "program specified" :
358                    (self->positioned == USPosition ? "user specified" :
359                     (self->positioned == (PPosition | USPosition) ?
360                      "program + user specified" :
361                      "BADNESS !?")))), place.x, place.y);
362
363         ob_debug("Sized: %s @ %d %d\n",
364                  (!self->sized ? "no" :
365                   (self->sized == PSize ? "program specified" :
366                    (self->sized == USSize ? "user specified" :
367                     (self->sized == (PSize | USSize) ?
368                      "program + user specified" :
369                      "BADNESS !?")))), place.width, place.height);
370
371         /* splash screens are also returned as TRUE for transient,
372            and so will be forced on screen below */
373         transient = place_client(self, &place.x, &place.y, settings);
374
375         /* make sure the window is visible. */
376         client_find_onscreen(self, &place.x, &place.y,
377                              place.width, place.height,
378                              /* non-normal clients has less rules, and
379                                 windows that are being restored from a
380                                 session do also. we can assume you want
381                                 it back where you saved it. Clients saying
382                                 they placed themselves are subjected to
383                                 harder rules, ones that are placed by
384                                 place.c or by the user are allowed partially
385                                 off-screen and on xinerama divides (ie,
386                                 it is up to the placement routines to avoid
387                                 the xinerama divides)
388
389                                 splash screens get "transient" set to TRUE by
390                                 the place_client call
391                              */
392                              ob_state() == OB_STATE_RUNNING &&
393                              (transient ||
394                               (!((self->positioned & USPosition) ||
395                                  (settings && settings->pos_given)) &&
396                                client_normal(self) &&
397                                !self->session &&
398                                /* don't move oldschool fullscreen windows to
399                                   fit inside the struts (fixes Acroread, which
400                                   makes its fullscreen window fit the screen
401                                   but it is not USSize'd or USPosition'd) */
402                                !(self->decorations == 0 &&
403                                  RECT_EQUAL(place, *monitor)))));
404     }
405
406     /* if the window isn't user-sized, then make it fit inside
407        the visible screen area on its monitor. Use basically the same rules
408        for forcing the window on screen in the client_find_onscreen call.
409
410        do this after place_client, it chooses the monitor!
411
412        splash screens get "transient" set to TRUE by
413        the place_client call
414     */
415     if (ob_state() == OB_STATE_RUNNING &&
416         (transient ||
417          (!(self->sized & USSize || self->positioned & USPosition) &&
418           client_normal(self) &&
419           !self->session &&
420           /* don't shrink oldschool fullscreen windows to fit inside the
421              struts (fixes Acroread, which makes its fullscreen window
422              fit the screen but it is not USSize'd or USPosition'd) */
423           !(self->decorations == 0 && RECT_EQUAL(place, *monitor)))))
424     {
425         Rect *a = screen_area(self->desktop, SCREEN_AREA_ONE_MONITOR, &place);
426
427         /* get the size of the frame */
428         place.width += self->frame->size.left + self->frame->size.right;
429         place.height += self->frame->size.top + self->frame->size.bottom;
430
431         /* fit the window inside the area */
432         place.width = MIN(place.width, a->width);
433         place.height = MIN(place.height, a->height);
434
435         ob_debug("setting window size to %dx%d\n", place.width, place.height);
436
437         /* get the size of the client back */
438         place.width -= self->frame->size.left + self->frame->size.right;
439         place.height -= self->frame->size.top + self->frame->size.bottom;
440
441         g_free(a);
442     }
443
444     ob_debug("placing window 0x%x at %d, %d with size %d x %d. "
445              "some restrictions may apply\n",
446              self->window, place.x, place.y, place.width, place.height);
447     if (self->session)
448         ob_debug("  but session requested %d, %d  %d x %d instead, "
449                  "overriding\n",
450                  self->session->x, self->session->y,
451                  self->session->w, self->session->h);
452
453     /* do this after the window is placed, so the premax/prefullscreen numbers
454        won't be all wacko!!
455
456        this also places the window
457     */
458     client_apply_startup_state(self, place.x, place.y,
459                                place.width, place.height);
460
461     g_free(monitor);
462     monitor = NULL;
463
464     ob_debug_type(OB_DEBUG_FOCUS, "Going to try activate new window? %s\n",
465                   activate ? "yes" : "no");
466     if (activate) {
467         gboolean raise = FALSE;
468
469         /* This is focus stealing prevention */
470         ob_debug_type(OB_DEBUG_FOCUS,
471                       "Want to focus new window 0x%x at time %u "
472                       "launched at %u (last user interaction time %u)\n",
473                       self->window, map_time, launch_time,
474                       event_last_user_time);
475
476         if (menu_frame_visible || moveresize_in_progress) {
477             activate = FALSE;
478             raise = TRUE;
479             ob_debug_type(OB_DEBUG_FOCUS,
480                           "Not focusing the window because the user is inside "
481                           "an Openbox menu or is move/resizing a window and "
482                           "we don't want to interrupt them\n");
483         }
484
485         /* if it's on another desktop */
486         else if (!(self->desktop == screen_desktop ||
487                    self->desktop == DESKTOP_ALL) &&
488                  /* the timestamp is from before you changed desktops */
489                  launch_time && screen_desktop_user_time &&
490                  !event_time_after(launch_time, screen_desktop_user_time))
491         {
492             activate = FALSE;
493             raise = TRUE;
494             ob_debug_type(OB_DEBUG_FOCUS,
495                           "Not focusing the window because its on another "
496                           "desktop\n");
497         }
498         /* If something is focused, and it's not our relative... */
499         else if (focus_client && client_search_focus_tree_full(self) == NULL &&
500                  client_search_focus_group_full(self) == NULL)
501         {
502             /* If the user is working in another window right now, then don't
503                steal focus */
504             if (event_last_user_time && launch_time &&
505                 event_time_after(event_last_user_time, launch_time) &&
506                 event_last_user_time != launch_time &&
507                 event_time_after(event_last_user_time,
508                                  map_time - OB_EVENT_USER_TIME_DELAY))
509             {
510                 activate = FALSE;
511                 ob_debug_type(OB_DEBUG_FOCUS,
512                               "Not focusing the window because the user is "
513                               "working in another window\n");
514             }
515             /* If its a transient (and its parents aren't focused) */
516             else if (client_has_parent(self)) {
517                 activate = FALSE;
518                 ob_debug_type(OB_DEBUG_FOCUS,
519                               "Not focusing the window because it is a "
520                               "transient, and its relatives aren't focused\n");
521             }
522             /* Don't steal focus from globally active clients.
523                I stole this idea from KWin. It seems nice.
524              */
525             else if (!(focus_client->can_focus ||
526                        focus_client->focus_notify))
527             {
528                 activate = FALSE;
529                 ob_debug_type(OB_DEBUG_FOCUS,
530                               "Not focusing the window because a globally "
531                               "active client has focus\n");
532             }
533             /* Don't move focus if it's not going to go to this window
534                anyway */
535             else if (client_focus_target(self) != self) {
536                 activate = FALSE;
537                 raise = TRUE;
538                 ob_debug_type(OB_DEBUG_FOCUS,
539                               "Not focusing the window because another window "
540                               "would get the focus anyway\n");
541             }
542             else if (!(self->desktop == screen_desktop ||
543                        self->desktop == DESKTOP_ALL))
544             {
545                 activate = FALSE;
546                 raise = TRUE;
547                 ob_debug_type(OB_DEBUG_FOCUS,
548                               "Not focusing the window because it is on "
549                               "another desktop and no relatives are focused ");
550             }
551         }
552
553         if (!activate) {
554             ob_debug_type(OB_DEBUG_FOCUS,
555                           "Focus stealing prevention activated for %s at "
556                           "time %u (last user interactioon time %u)\n",
557                           self->title, map_time, event_last_user_time);
558             /* if the client isn't focused, then hilite it so the user
559                knows it is there */
560             client_hilite(self, TRUE);
561             /* we may want to raise it even tho we're not activating it */
562             if (raise && !client_restore_session_stacking(self))
563                 stacking_raise(CLIENT_AS_WINDOW(self));
564         }
565     }
566     else {
567         /* This may look rather odd. Well it's because new windows are added
568            to the stacking order non-intrusively. If we're not going to focus
569            the new window or hilite it, then we raise it to the top. This will
570            take affect for things that don't get focused like splash screens.
571            Also if you don't have focus_new enabled, then it's going to get
572            raised to the top. Legacy begets legacy I guess?
573         */
574         if (!client_restore_session_stacking(self))
575             stacking_raise(CLIENT_AS_WINDOW(self));
576     }
577
578     mouse_grab_for_client(self, TRUE);
579
580     /* this has to happen before we try focus the window, but we want it to
581        happen after the client's stacking has been determined or it looks bad
582     */
583     {
584         gulong ignore_start;
585         if (!config_focus_under_mouse)
586             ignore_start = event_start_ignore_all_enters();
587
588         client_show(self);
589
590         if (!config_focus_under_mouse)
591             event_end_ignore_all_enters(ignore_start);
592     }
593
594     if (activate) {
595         gboolean stacked = client_restore_session_stacking(self);
596         client_present(self, FALSE, !stacked, TRUE);
597     }
598
599     /* add to client list/map */
600     client_list = g_list_append(client_list, self);
601     g_hash_table_insert(window_map, &self->window, self);
602
603     /* this has to happen after we're in the client_list */
604     if (STRUT_EXISTS(self->strut))
605         screen_update_areas();
606
607     /* update the list hints */
608     client_set_list();
609
610     /* watch for when the application stops responding.  only do this for
611        normal windows, i.e. windows which have titlebars and close buttons 
612        and things like that.
613        we don't need to stop pinging on unmanage, because it will be handled
614        automatically by the destroy callback!
615     */
616     if (self->ping && client_normal(self))
617         ping_start(self, client_ping_event);
618
619     /* free the ObAppSettings shallow copy */
620     g_free(settings);
621
622     ob_debug("Managed window 0x%lx plate 0x%x (%s)\n",
623              window, self->frame->window, self->class);
624
625     return;
626 }
627
628
629 ObClient *client_fake_manage(Window window)
630 {
631     ObClient *self;
632     ObAppSettings *settings;
633
634     ob_debug("Pretend-managing window: %lx\n", window);
635
636     /* do this minimal stuff to figure out the client's decorations */
637
638     self = g_new0(ObClient, 1);
639     self->window = window;
640
641     client_get_all(self, FALSE);
642     /* per-app settings override stuff, and return the settings for other
643        uses too. this returns a shallow copy that needs to be freed */
644     settings = client_get_settings_state(self);
645
646     client_setup_decor_and_functions(self, FALSE);
647
648     /* create the decoration frame for the client window and adjust its size */
649     self->frame = frame_new(self);
650     frame_adjust_area(self->frame, FALSE, TRUE, TRUE);
651
652     ob_debug("gave extents left %d right %d top %d bottom %d\n",
653              self->frame->size.left, self->frame->size.right,
654              self->frame->size.top, self->frame->size.bottom);
655
656     /* free the ObAppSettings shallow copy */
657     g_free(settings);
658
659     return self;
660 }
661
662 void client_unmanage_all(void)
663 {
664     while (client_list != NULL)
665         client_unmanage(client_list->data);
666 }
667
668 void client_unmanage(ObClient *self)
669 {
670     guint j;
671     GSList *it;
672     gulong ignore_start;
673
674     ob_debug("Unmanaging window: 0x%x plate 0x%x (%s) (%s)\n",
675              self->window, self->frame->window,
676              self->class, self->title ? self->title : "");
677
678     g_assert(self != NULL);
679
680     /* we dont want events no more. do this before hiding the frame so we
681        don't generate more events */
682     XSelectInput(ob_display, self->window, NoEventMask);
683
684     /* ignore enter events from the unmap so it doesnt mess with the focus */
685     if (!config_focus_under_mouse)
686         ignore_start = event_start_ignore_all_enters();
687
688     frame_hide(self->frame);
689     /* flush to send the hide to the server quickly */
690     XFlush(ob_display);
691
692     if (!config_focus_under_mouse)
693         event_end_ignore_all_enters(ignore_start);
694
695     mouse_grab_for_client(self, FALSE);
696
697     /* remove the window from our save set */
698     XChangeSaveSet(ob_display, self->window, SetModeDelete);
699
700     /* update the focus lists */
701     focus_order_remove(self);
702     if (client_focused(self)) {
703         /* don't leave an invalid focus_client */
704         focus_client = NULL;
705     }
706
707     client_list = g_list_remove(client_list, self);
708     stacking_remove(self);
709     g_hash_table_remove(window_map, &self->window);
710
711     /* once the client is out of the list, update the struts to remove its
712        influence */
713     if (STRUT_EXISTS(self->strut))
714         screen_update_areas();
715
716     client_call_notifies(self, client_destroy_notifies);
717
718     /* tell our parent(s) that we're gone */
719     for (it = self->parents; it; it = g_slist_next(it))
720         ((ObClient*)it->data)->transients =
721             g_slist_remove(((ObClient*)it->data)->transients,self);
722
723     /* tell our transients that we're gone */
724     for (it = self->transients; it; it = g_slist_next(it)) {
725         ((ObClient*)it->data)->parents =
726             g_slist_remove(((ObClient*)it->data)->parents, self);
727         /* we could be keeping our children in a higher layer */
728         client_calc_layer(it->data);
729     }
730
731     /* remove from its group */
732     if (self->group) {
733         group_remove(self->group, self);
734         self->group = NULL;
735     }
736
737     /* restore the window's original geometry so it is not lost */
738     {
739         Rect a;
740
741         a = self->area;
742
743         if (self->fullscreen)
744             a = self->pre_fullscreen_area;
745         else if (self->max_horz || self->max_vert) {
746             if (self->max_horz) {
747                 a.x = self->pre_max_area.x;
748                 a.width = self->pre_max_area.width;
749             }
750             if (self->max_vert) {
751                 a.y = self->pre_max_area.y;
752                 a.height = self->pre_max_area.height;
753             }
754         }
755
756         self->fullscreen = self->max_horz = self->max_vert = FALSE;
757         /* let it be moved and resized no matter what */
758         self->functions = OB_CLIENT_FUNC_MOVE | OB_CLIENT_FUNC_RESIZE;
759         self->decorations = 0; /* unmanaged windows have no decor */
760
761         /* give the client its border back */
762         XSetWindowBorderWidth(ob_display, self->window, self->border_width);
763
764         client_move_resize(self, a.x, a.y, a.width, a.height);
765     }
766
767     /* reparent the window out of the frame, and free the frame */
768     frame_release_client(self->frame);
769     frame_free(self->frame);
770     self->frame = NULL;
771
772     if (ob_state() != OB_STATE_EXITING) {
773         /* these values should not be persisted across a window
774            unmapping/mapping */
775         PROP_ERASE(self->window, net_wm_desktop);
776         PROP_ERASE(self->window, net_wm_state);
777         PROP_ERASE(self->window, wm_state);
778     } else {
779         /* if we're left in an unmapped state, the client wont be mapped.
780            this is bad, since we will no longer be managing the window on
781            restart */
782         XMapWindow(ob_display, self->window);
783     }
784
785     /* these should not be left on the window ever.  other window managers
786        don't necessarily use them and it will mess them up (like compiz) */
787     PROP_ERASE(self->window, net_wm_visible_name);
788     PROP_ERASE(self->window, net_wm_visible_icon_name);
789
790     /* update the list hints */
791     client_set_list();
792
793     ob_debug("Unmanaged window 0x%lx\n", self->window);
794
795     /* free all data allocated in the client struct */
796     g_slist_free(self->transients);
797     for (j = 0; j < self->nicons; ++j)
798         g_free(self->icons[j].data);
799     if (self->nicons > 0)
800         g_free(self->icons);
801     g_free(self->wm_command);
802     g_free(self->title);
803     g_free(self->icon_title);
804     g_free(self->name);
805     g_free(self->class);
806     g_free(self->role);
807     g_free(self->client_machine);
808     g_free(self->sm_client_id);
809     g_free(self);
810 }
811
812 void client_fake_unmanage(ObClient *self)
813 {
814     /* this is all that got allocated to get the decorations */
815
816     frame_free(self->frame);
817     g_free(self);
818 }
819
820 /*! Returns a new structure containing the per-app settings for this client.
821   The returned structure needs to be freed with g_free. */
822 static ObAppSettings *client_get_settings_state(ObClient *self)
823 {
824     ObAppSettings *settings;
825     GSList *it;
826
827     settings = config_create_app_settings();
828
829     for (it = config_per_app_settings; it; it = g_slist_next(it)) {
830         ObAppSettings *app = it->data;
831         gboolean match = TRUE;
832
833         g_assert(app->name != NULL || app->class != NULL);
834
835         /* we know that either name or class is not NULL so it will have to
836            match to use the rule */
837         if (app->name &&
838             !g_pattern_match(app->name, strlen(self->name), self->name, NULL))
839             match = FALSE;
840         else if (app->class &&
841                 !g_pattern_match(app->class,
842                                  strlen(self->class), self->class, NULL))
843             match = FALSE;
844         else if (app->role &&
845                  !g_pattern_match(app->role,
846                                   strlen(self->role), self->role, NULL))
847             match = FALSE;
848
849         if (match) {
850             ob_debug("Window matching: %s\n", app->name);
851
852             /* copy the settings to our struct, overriding the existing
853                settings if they are not defaults */
854             config_app_settings_copy_non_defaults(app, settings);
855         }
856     }
857
858     if (settings->shade != -1)
859         self->shaded = !!settings->shade;
860     if (settings->decor != -1)
861         self->undecorated = !settings->decor;
862     if (settings->iconic != -1)
863         self->iconic = !!settings->iconic;
864     if (settings->skip_pager != -1)
865         self->skip_pager = !!settings->skip_pager;
866     if (settings->skip_taskbar != -1)
867         self->skip_taskbar = !!settings->skip_taskbar;
868
869     if (settings->max_vert != -1)
870         self->max_vert = !!settings->max_vert;
871     if (settings->max_horz != -1)
872         self->max_horz = !!settings->max_horz;
873
874     if (settings->fullscreen != -1)
875         self->fullscreen = !!settings->fullscreen;
876
877     if (settings->desktop) {
878         if (settings->desktop == DESKTOP_ALL)
879             self->desktop = settings->desktop;
880         else if (settings->desktop > 0 &&
881                  settings->desktop <= screen_num_desktops)
882             self->desktop = settings->desktop - 1;
883     }
884
885     if (settings->layer == -1) {
886         self->below = TRUE;
887         self->above = FALSE;
888     }
889     else if (settings->layer == 0) {
890         self->below = FALSE;
891         self->above = FALSE;
892     }
893     else if (settings->layer == 1) {
894         self->below = FALSE;
895         self->above = TRUE;
896     }
897     return settings;
898 }
899
900 static void client_restore_session_state(ObClient *self)
901 {
902     GList *it;
903
904     ob_debug_type(OB_DEBUG_SM,
905                   "Restore session for client %s\n", self->title);
906
907     if (!(it = session_state_find(self))) {
908         ob_debug_type(OB_DEBUG_SM,
909                       "Session data not found for client %s\n", self->title);
910         return;
911     }
912
913     self->session = it->data;
914
915     ob_debug_type(OB_DEBUG_SM, "Session data loaded for client %s\n",
916                   self->title);
917
918     RECT_SET_POINT(self->area, self->session->x, self->session->y);
919     self->positioned = USPosition;
920     self->sized = USSize;
921     if (self->session->w > 0)
922         self->area.width = self->session->w;
923     if (self->session->h > 0)
924         self->area.height = self->session->h;
925     XResizeWindow(ob_display, self->window,
926                   self->area.width, self->area.height);
927
928     self->desktop = (self->session->desktop == DESKTOP_ALL ?
929                      self->session->desktop :
930                      MIN(screen_num_desktops - 1, self->session->desktop));
931     PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
932
933     self->shaded = self->session->shaded;
934     self->iconic = self->session->iconic;
935     self->skip_pager = self->session->skip_pager;
936     self->skip_taskbar = self->session->skip_taskbar;
937     self->fullscreen = self->session->fullscreen;
938     self->above = self->session->above;
939     self->below = self->session->below;
940     self->max_horz = self->session->max_horz;
941     self->max_vert = self->session->max_vert;
942     self->undecorated = self->session->undecorated;
943 }
944
945 static gboolean client_restore_session_stacking(ObClient *self)
946 {
947     GList *it, *mypos;
948
949     if (!self->session) return FALSE;
950
951     mypos = g_list_find(session_saved_state, self->session);
952     if (!mypos) return FALSE;
953
954     /* start above me and look for the first client */
955     for (it = g_list_previous(mypos); it; it = g_list_previous(it)) {
956         GList *cit;
957
958         for (cit = client_list; cit; cit = g_list_next(cit)) {
959             ObClient *c = cit->data;
960             /* found a client that was in the session, so go below it */
961             if (c->session == it->data) {
962                 stacking_below(CLIENT_AS_WINDOW(self),
963                                CLIENT_AS_WINDOW(cit->data));
964                 return TRUE;
965             }
966         }
967     }
968     return FALSE;
969 }
970
971 void client_move_onscreen(ObClient *self, gboolean rude)
972 {
973     gint x = self->area.x;
974     gint y = self->area.y;
975     if (client_find_onscreen(self, &x, &y,
976                              self->area.width,
977                              self->area.height, rude)) {
978         client_move(self, x, y);
979     }
980 }
981
982 gboolean client_find_onscreen(ObClient *self, gint *x, gint *y, gint w, gint h,
983                               gboolean rude)
984 {
985     gint ox = *x, oy = *y;
986     gboolean rudel = rude, ruder = rude, rudet = rude, rudeb = rude;
987     gint fw, fh;
988     Rect desired;
989     guint i;
990
991     RECT_SET(desired, *x, *y, w, h);
992     frame_rect_to_frame(self->frame, &desired);
993
994     /* get where the frame would be */
995     frame_client_gravity(self->frame, x, y);
996
997     /* get the requested size of the window with decorations */
998     fw = self->frame->size.left + w + self->frame->size.right;
999     fh = self->frame->size.top + h + self->frame->size.bottom;
1000
1001     /* If rudeness wasn't requested, then still be rude in a given direction
1002        if the client is not moving, only resizing in that direction */
1003     if (!rude) {
1004         Point oldtl, oldtr, oldbl, oldbr;
1005         Point newtl, newtr, newbl, newbr;
1006         gboolean stationary_l, stationary_r, stationary_t, stationary_b;
1007
1008         POINT_SET(oldtl, self->frame->area.x, self->frame->area.y);
1009         POINT_SET(oldbr, self->frame->area.x + self->frame->area.width - 1,
1010                   self->frame->area.y + self->frame->area.height - 1);
1011         POINT_SET(oldtr, oldbr.x, oldtl.y);
1012         POINT_SET(oldbl, oldtl.x, oldbr.y);
1013
1014         POINT_SET(newtl, *x, *y);
1015         POINT_SET(newbr, *x + fw - 1, *y + fh - 1);
1016         POINT_SET(newtr, newbr.x, newtl.y);
1017         POINT_SET(newbl, newtl.x, newbr.y);
1018
1019         /* is it moving or just resizing from some corner? */
1020         stationary_l = oldtl.x == newtl.x;
1021         stationary_r = oldtr.x == newtr.x;
1022         stationary_t = oldtl.y == newtl.y;
1023         stationary_b = oldbl.y == newbl.y;
1024
1025         /* if left edge is growing and didnt move right edge */
1026         if (stationary_r && newtl.x < oldtl.x)
1027             rudel = TRUE;
1028         /* if right edge is growing and didnt move left edge */
1029         if (stationary_l && newtr.x > oldtr.x)
1030             ruder = TRUE;
1031         /* if top edge is growing and didnt move bottom edge */
1032         if (stationary_b && newtl.y < oldtl.y)
1033             rudet = TRUE;
1034         /* if bottom edge is growing and didnt move top edge */
1035         if (stationary_t && newbl.y > oldbl.y)
1036             rudeb = TRUE;
1037     }
1038
1039     for (i = 0; i < screen_num_monitors; ++i) {
1040         Rect *a;
1041
1042         if (!screen_physical_area_monitor_contains(i, &desired)) {
1043             if (i < screen_num_monitors - 1)
1044                 continue;
1045
1046             /* the window is not inside any monitor! so just use the first
1047                one */
1048             a = screen_area(self->desktop, 0, NULL);
1049         } else
1050             a = screen_area(self->desktop, SCREEN_AREA_ONE_MONITOR, &desired);
1051
1052         /* This makes sure windows aren't entirely outside of the screen so you
1053            can't see them at all.
1054            It makes sure 10% of the window is on the screen at least. At don't
1055            let it move itself off the top of the screen, which would hide the
1056            titlebar on you. (The user can still do this if they want too, it's
1057            only limiting the application.
1058         */
1059         if (client_normal(self)) {
1060             if (!self->strut.right && *x + fw/10 >= a->x + a->width - 1)
1061                 *x = a->x + a->width - fw/10;
1062             if (!self->strut.bottom && *y + fh/10 >= a->y + a->height - 1)
1063                 *y = a->y + a->height - fh/10;
1064             if (!self->strut.left && *x + fw*9/10 - 1 < a->x)
1065                 *x = a->x - fw*9/10;
1066             if (!self->strut.top && *y + fh*9/10 - 1 < a->y)
1067                 *y = a->y - fh*9/10;
1068         }
1069
1070         /* This here doesn't let windows even a pixel outside the
1071            struts/screen. When called from client_manage, programs placing
1072            themselves are forced completely onscreen, while things like
1073            xterm -geometry resolution-width/2 will work fine. Trying to
1074            place it completely offscreen will be handled in the above code.
1075            Sorry for this confused comment, i am tired. */
1076         if (rudel && !self->strut.left && *x < a->x) *x = a->x;
1077         if (ruder && !self->strut.right && *x + fw > a->x + a->width)
1078             *x = a->x + MAX(0, a->width - fw);
1079
1080         if (rudet && !self->strut.top && *y < a->y) *y = a->y;
1081         if (rudeb && !self->strut.bottom && *y + fh > a->y + a->height)
1082             *y = a->y + MAX(0, a->height - fh);
1083
1084         g_free(a);
1085     }
1086
1087     /* get where the client should be */
1088     frame_frame_gravity(self->frame, x, y);
1089
1090     return ox != *x || oy != *y;
1091 }
1092
1093 static void client_get_all(ObClient *self, gboolean real)
1094 {
1095     /* this is needed for the frame to set itself up */
1096     client_get_area(self);
1097
1098     /* these things can change the decor and functions of the window */
1099
1100     client_get_mwm_hints(self);
1101     /* this can change the mwmhints for special cases */
1102     client_get_type_and_transientness(self);
1103     client_get_state(self);
1104     client_update_normal_hints(self);
1105
1106     /* get the session related properties, these can change decorations
1107        from per-app settings */
1108     client_get_session_ids(self);
1109
1110     /* now we got everything that can affect the decorations */
1111     if (!real)
1112         return;
1113
1114     /* get this early so we have it for debugging */
1115     client_update_title(self);
1116
1117     client_update_protocols(self);
1118
1119     client_update_wmhints(self);
1120     /* this may have already been called from client_update_wmhints */
1121     if (!self->parents && !self->transient_for_group)
1122         client_update_transient_for(self);
1123
1124     client_get_startup_id(self);
1125     client_get_desktop(self);/* uses transient data/group/startup id if a
1126                                 desktop is not specified */
1127     client_get_shaped(self);
1128
1129     {
1130         /* a couple type-based defaults for new windows */
1131
1132         /* this makes sure that these windows appear on all desktops */
1133         if (self->type == OB_CLIENT_TYPE_DESKTOP)
1134             self->desktop = DESKTOP_ALL;
1135     }
1136
1137 #ifdef SYNC
1138     client_update_sync_request_counter(self);
1139 #endif
1140
1141     client_get_colormap(self);
1142     client_update_strut(self);
1143     client_update_icons(self);
1144     client_update_icon_geometry(self);
1145 }
1146
1147 static void client_get_startup_id(ObClient *self)
1148 {
1149     if (!(PROP_GETS(self->window, net_startup_id, utf8, &self->startup_id)))
1150         if (self->group)
1151             PROP_GETS(self->group->leader,
1152                       net_startup_id, utf8, &self->startup_id);
1153 }
1154
1155 static void client_get_area(ObClient *self)
1156 {
1157     XWindowAttributes wattrib;
1158     Status ret;
1159
1160     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
1161     g_assert(ret != BadWindow);
1162
1163     RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
1164     POINT_SET(self->root_pos, wattrib.x, wattrib.y);
1165     self->border_width = wattrib.border_width;
1166
1167     ob_debug("client area: %d %d  %d %d  bw %d\n", wattrib.x, wattrib.y,
1168              wattrib.width, wattrib.height, wattrib.border_width);
1169 }
1170
1171 static void client_get_desktop(ObClient *self)
1172 {
1173     guint32 d = screen_num_desktops; /* an always-invalid value */
1174
1175     if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
1176         if (d >= screen_num_desktops && d != DESKTOP_ALL)
1177             self->desktop = screen_num_desktops - 1;
1178         else
1179             self->desktop = d;
1180         ob_debug("client requested desktop 0x%x\n", self->desktop);
1181     } else {
1182         GSList *it;
1183         gboolean first = TRUE;
1184         guint all = screen_num_desktops; /* not a valid value */
1185
1186         /* if they are all on one desktop, then open it on the
1187            same desktop */
1188         for (it = self->parents; it; it = g_slist_next(it)) {
1189             ObClient *c = it->data;
1190
1191             if (c->desktop == DESKTOP_ALL) continue;
1192
1193             if (first) {
1194                 all = c->desktop;
1195                 first = FALSE;
1196             }
1197             else if (all != c->desktop)
1198                 all = screen_num_desktops; /* make it invalid */
1199         }
1200         if (all != screen_num_desktops) {
1201             self->desktop = all;
1202
1203             ob_debug("client desktop set from parents: 0x%x\n",
1204                      self->desktop);
1205         }
1206         /* try get from the startup-notification protocol */
1207         else if (sn_get_desktop(self->startup_id, &self->desktop)) {
1208             if (self->desktop >= screen_num_desktops &&
1209                 self->desktop != DESKTOP_ALL)
1210                 self->desktop = screen_num_desktops - 1;
1211             ob_debug("client desktop set from startup-notification: 0x%x\n",
1212                      self->desktop);
1213         }
1214         /* defaults to the current desktop */
1215         else {
1216             self->desktop = screen_desktop;
1217             ob_debug("client desktop set to the current desktop: %d\n",
1218                      self->desktop);
1219         }
1220     }
1221 }
1222
1223 static void client_get_state(ObClient *self)
1224 {
1225     guint32 *state;
1226     guint num;
1227
1228     if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
1229         gulong i;
1230         for (i = 0; i < num; ++i) {
1231             if (state[i] == prop_atoms.net_wm_state_modal)
1232                 self->modal = TRUE;
1233             else if (state[i] == prop_atoms.net_wm_state_shaded)
1234                 self->shaded = TRUE;
1235             else if (state[i] == prop_atoms.net_wm_state_hidden)
1236                 self->iconic = TRUE;
1237             else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
1238                 self->skip_taskbar = TRUE;
1239             else if (state[i] == prop_atoms.net_wm_state_skip_pager)
1240                 self->skip_pager = TRUE;
1241             else if (state[i] == prop_atoms.net_wm_state_fullscreen)
1242                 self->fullscreen = TRUE;
1243             else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
1244                 self->max_vert = TRUE;
1245             else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
1246                 self->max_horz = TRUE;
1247             else if (state[i] == prop_atoms.net_wm_state_above)
1248                 self->above = TRUE;
1249             else if (state[i] == prop_atoms.net_wm_state_below)
1250                 self->below = TRUE;
1251             else if (state[i] == prop_atoms.net_wm_state_demands_attention)
1252                 self->demands_attention = TRUE;
1253             else if (state[i] == prop_atoms.ob_wm_state_undecorated)
1254                 self->undecorated = TRUE;
1255         }
1256
1257         g_free(state);
1258     }
1259 }
1260
1261 static void client_get_shaped(ObClient *self)
1262 {
1263     self->shaped = FALSE;
1264 #ifdef   SHAPE
1265     if (extensions_shape) {
1266         gint foo;
1267         guint ufoo;
1268         gint s;
1269
1270         XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
1271
1272         XShapeQueryExtents(ob_display, self->window, &s, &foo,
1273                            &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
1274                            &ufoo);
1275         self->shaped = (s != 0);
1276     }
1277 #endif
1278 }
1279
1280 void client_update_transient_for(ObClient *self)
1281 {
1282     Window t = None;
1283     ObClient *target = NULL;
1284     gboolean trangroup = FALSE;
1285
1286     if (XGetTransientForHint(ob_display, self->window, &t)) {
1287         if (t != self->window) { /* cant be transient to itself! */
1288             target = g_hash_table_lookup(window_map, &t);
1289             /* if this happens then we need to check for it*/
1290             g_assert(target != self);
1291             if (target && !WINDOW_IS_CLIENT(target)) {
1292                 /* this can happen when a dialog is a child of
1293                    a dockapp, for example */
1294                 target = NULL;
1295             }
1296         }
1297
1298         /* Setting the transient_for to Root is actually illegal, however
1299            applications from time have done this to specify transient for
1300            their group */
1301         if (!target && self->group && t == RootWindow(ob_display, ob_screen))
1302             trangroup = TRUE;
1303     } else if (self->group && self->transient)
1304         trangroup = TRUE;
1305
1306     client_update_transient_tree(self, self->group, self->group,
1307                                  self->transient_for_group, trangroup,
1308                                  client_direct_parent(self), target);
1309     self->transient_for_group = trangroup;
1310
1311 }
1312
1313 static void client_update_transient_tree(ObClient *self,
1314                                          ObGroup *oldgroup, ObGroup *newgroup,
1315                                          gboolean oldgtran, gboolean newgtran,
1316                                          ObClient* oldparent,
1317                                          ObClient *newparent)
1318 {
1319     GSList *it, *next;
1320     ObClient *c;
1321
1322     g_assert(!oldgtran || oldgroup);
1323     g_assert(!newgtran || newgroup);
1324     g_assert((!oldgtran && !oldparent) ||
1325              (oldgtran && !oldparent) ||
1326              (!oldgtran && oldparent));
1327     g_assert((!newgtran && !newparent) ||
1328              (newgtran && !newparent) ||
1329              (!newgtran && newparent));
1330
1331     /* * *
1332       Group transient windows are not allowed to have other group
1333       transient windows as their children.
1334       * * */
1335
1336
1337     /* No change has occured */
1338     if (oldgroup == newgroup &&
1339         oldgtran == newgtran &&
1340         oldparent == newparent) return;
1341
1342     /** Remove the client from the transient tree **/
1343
1344     for (it = self->transients; it; it = next) {
1345         next = g_slist_next(it);
1346         c = it->data;
1347         self->transients = g_slist_delete_link(self->transients, it);
1348         c->parents = g_slist_remove(c->parents, self);
1349     }
1350     for (it = self->parents; it; it = next) {
1351         next = g_slist_next(it);
1352         c = it->data;
1353         self->parents = g_slist_delete_link(self->parents, it);
1354         c->transients = g_slist_remove(c->transients, self);
1355     }
1356
1357     /** Re-add the client to the transient tree **/
1358
1359     /* If we're transient for a group then we need to add ourselves to all our
1360        parents */
1361     if (newgtran) {
1362         for (it = newgroup->members; it; it = g_slist_next(it)) {
1363             c = it->data;
1364             if (c != self &&
1365                 !client_search_top_direct_parent(c)->transient_for_group &&
1366                 client_normal(c))
1367             {
1368                 c->transients = g_slist_prepend(c->transients, self);
1369                 self->parents = g_slist_prepend(self->parents, c);
1370             }
1371         }
1372     }
1373
1374     /* If we are now transient for a single window we need to add ourselves to
1375        its children
1376
1377        WARNING: Cyclical transient ness is possible if two windows are
1378        transient for eachother.
1379     */
1380     else if (newparent &&
1381              /* don't make ourself its child if it is already our child */
1382              !client_is_direct_child(self, newparent) &&
1383              client_normal(newparent))
1384     {
1385         newparent->transients = g_slist_prepend(newparent->transients, self);
1386         self->parents = g_slist_prepend(self->parents, newparent);
1387     }
1388
1389     /* Add any group transient windows to our children. But if we're transient
1390        for the group, then other group transients are not our children.
1391
1392        WARNING: Cyclical transient-ness is possible. For e.g. if:
1393        A is transient for the group
1394        B is transient for A
1395        C is transient for B
1396        A can't be transient for C or we have a cycle
1397     */
1398     if (!newgtran && newgroup &&
1399         (!newparent ||
1400          !client_search_top_direct_parent(newparent)->transient_for_group) &&
1401         client_normal(self))
1402     {
1403         for (it = newgroup->members; it; it = g_slist_next(it)) {
1404             c = it->data;
1405             if (c != self && c->transient_for_group &&
1406                 /* Don't make it our child if it is already our parent */
1407                 !client_is_direct_child(c, self))
1408             {
1409                 self->transients = g_slist_prepend(self->transients, c);
1410                 c->parents = g_slist_prepend(c->parents, self);
1411             }
1412         }
1413     }
1414
1415     /** If we change our group transient-ness, our children change their
1416         effect group transient-ness, which affects how they relate to other
1417         group windows **/
1418
1419     for (it = self->transients; it; it = g_slist_next(it)) {
1420         c = it->data;
1421         if (!c->transient_for_group)
1422             client_update_transient_tree(c, c->group, c->group,
1423                                          c->transient_for_group,
1424                                          c->transient_for_group,
1425                                          client_direct_parent(c),
1426                                          client_direct_parent(c));
1427     }
1428 }
1429
1430 static void client_get_mwm_hints(ObClient *self)
1431 {
1432     guint num;
1433     guint32 *hints;
1434
1435     self->mwmhints.flags = 0; /* default to none */
1436
1437     if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
1438                     &hints, &num)) {
1439         if (num >= OB_MWM_ELEMENTS) {
1440             self->mwmhints.flags = hints[0];
1441             self->mwmhints.functions = hints[1];
1442             self->mwmhints.decorations = hints[2];
1443         }
1444         g_free(hints);
1445     }
1446 }
1447
1448 void client_get_type_and_transientness(ObClient *self)
1449 {
1450     guint num, i;
1451     guint32 *val;
1452     Window t;
1453
1454     self->type = -1;
1455     self->transient = FALSE;
1456
1457     if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
1458         /* use the first value that we know about in the array */
1459         for (i = 0; i < num; ++i) {
1460             if (val[i] == prop_atoms.net_wm_window_type_desktop)
1461                 self->type = OB_CLIENT_TYPE_DESKTOP;
1462             else if (val[i] == prop_atoms.net_wm_window_type_dock)
1463                 self->type = OB_CLIENT_TYPE_DOCK;
1464             else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
1465                 self->type = OB_CLIENT_TYPE_TOOLBAR;
1466             else if (val[i] == prop_atoms.net_wm_window_type_menu)
1467                 self->type = OB_CLIENT_TYPE_MENU;
1468             else if (val[i] == prop_atoms.net_wm_window_type_utility)
1469                 self->type = OB_CLIENT_TYPE_UTILITY;
1470             else if (val[i] == prop_atoms.net_wm_window_type_splash)
1471                 self->type = OB_CLIENT_TYPE_SPLASH;
1472             else if (val[i] == prop_atoms.net_wm_window_type_dialog)
1473                 self->type = OB_CLIENT_TYPE_DIALOG;
1474             else if (val[i] == prop_atoms.net_wm_window_type_normal)
1475                 self->type = OB_CLIENT_TYPE_NORMAL;
1476             else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
1477                 /* prevent this window from getting any decor or
1478                    functionality */
1479                 self->mwmhints.flags &= (OB_MWM_FLAG_FUNCTIONS |
1480                                          OB_MWM_FLAG_DECORATIONS);
1481                 self->mwmhints.decorations = 0;
1482                 self->mwmhints.functions = 0;
1483             }
1484             if (self->type != (ObClientType) -1)
1485                 break; /* grab the first legit type */
1486         }
1487         g_free(val);
1488     }
1489
1490     if (XGetTransientForHint(ob_display, self->window, &t))
1491         self->transient = TRUE;
1492
1493     if (self->type == (ObClientType) -1) {
1494         /*the window type hint was not set, which means we either classify
1495           ourself as a normal window or a dialog, depending on if we are a
1496           transient. */
1497         if (self->transient)
1498             self->type = OB_CLIENT_TYPE_DIALOG;
1499         else
1500             self->type = OB_CLIENT_TYPE_NORMAL;
1501     }
1502
1503     /* then, based on our type, we can update our transientness.. */
1504     if (self->type == OB_CLIENT_TYPE_DIALOG ||
1505         self->type == OB_CLIENT_TYPE_TOOLBAR ||
1506         self->type == OB_CLIENT_TYPE_MENU ||
1507         self->type == OB_CLIENT_TYPE_UTILITY)
1508     {
1509         self->transient = TRUE;
1510     }
1511 }
1512
1513 void client_update_protocols(ObClient *self)
1514 {
1515     guint32 *proto;
1516     guint num_return, i;
1517
1518     self->focus_notify = FALSE;
1519     self->delete_window = FALSE;
1520
1521     if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
1522         for (i = 0; i < num_return; ++i) {
1523             if (proto[i] == prop_atoms.wm_delete_window)
1524                 /* this means we can request the window to close */
1525                 self->delete_window = TRUE;
1526             else if (proto[i] == prop_atoms.wm_take_focus)
1527                 /* if this protocol is requested, then the window will be
1528                    notified whenever we want it to receive focus */
1529                 self->focus_notify = TRUE;
1530             else if (proto[i] == prop_atoms.net_wm_ping)
1531                 /* if this protocol is requested, then the window will allow
1532                    pings to determine if it is still alive */
1533                 self->ping = TRUE;
1534 #ifdef SYNC
1535             else if (proto[i] == prop_atoms.net_wm_sync_request)
1536                 /* if this protocol is requested, then resizing the
1537                    window will be synchronized between the frame and the
1538                    client */
1539                 self->sync_request = TRUE;
1540 #endif
1541         }
1542         g_free(proto);
1543     }
1544 }
1545
1546 #ifdef SYNC
1547 void client_update_sync_request_counter(ObClient *self)
1548 {
1549     guint32 i;
1550
1551     if (PROP_GET32(self->window, net_wm_sync_request_counter, cardinal, &i)) {
1552         self->sync_counter = i;
1553     } else
1554         self->sync_counter = None;
1555 }
1556 #endif
1557
1558 static void client_get_colormap(ObClient *self)
1559 {
1560     XWindowAttributes wa;
1561
1562     if (XGetWindowAttributes(ob_display, self->window, &wa))
1563         client_update_colormap(self, wa.colormap);
1564 }
1565
1566 void client_update_colormap(ObClient *self, Colormap colormap)
1567 {
1568     if (colormap == self->colormap) return;
1569
1570     ob_debug("Setting client %s colormap: 0x%x\n", self->title, colormap);
1571
1572     if (client_focused(self)) {
1573         screen_install_colormap(self, FALSE); /* uninstall old one */
1574         self->colormap = colormap;
1575         screen_install_colormap(self, FALSE); /* install new one */
1576     } else
1577         self->colormap = colormap;
1578 }
1579
1580 void client_update_normal_hints(ObClient *self)
1581 {
1582     XSizeHints size;
1583     glong ret;
1584
1585     /* defaults */
1586     self->min_ratio = 0.0f;
1587     self->max_ratio = 0.0f;
1588     SIZE_SET(self->size_inc, 1, 1);
1589     SIZE_SET(self->base_size, 0, 0);
1590     SIZE_SET(self->min_size, 0, 0);
1591     SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
1592
1593     /* get the hints from the window */
1594     if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
1595         /* normal windows can't request placement! har har
1596         if (!client_normal(self))
1597         */
1598         self->positioned = (size.flags & (PPosition|USPosition));
1599         self->sized = (size.flags & (PSize|USSize));
1600
1601         if (size.flags & PWinGravity)
1602             self->gravity = size.win_gravity;
1603
1604         if (size.flags & PAspect) {
1605             if (size.min_aspect.y)
1606                 self->min_ratio =
1607                     (gfloat) size.min_aspect.x / size.min_aspect.y;
1608             if (size.max_aspect.y)
1609                 self->max_ratio =
1610                     (gfloat) size.max_aspect.x / size.max_aspect.y;
1611         }
1612
1613         if (size.flags & PMinSize)
1614             SIZE_SET(self->min_size, size.min_width, size.min_height);
1615
1616         if (size.flags & PMaxSize)
1617             SIZE_SET(self->max_size, size.max_width, size.max_height);
1618
1619         if (size.flags & PBaseSize)
1620             SIZE_SET(self->base_size, size.base_width, size.base_height);
1621
1622         if (size.flags & PResizeInc && size.width_inc && size.height_inc)
1623             SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
1624
1625         ob_debug("Normal hints: min size (%d %d) max size (%d %d)\n   "
1626                  "size inc (%d %d) base size (%d %d)\n",
1627                  self->min_size.width, self->min_size.height,
1628                  self->max_size.width, self->max_size.height,
1629                  self->size_inc.width, self->size_inc.height,
1630                  self->base_size.width, self->base_size.height);
1631     }
1632     else
1633         ob_debug("Normal hints: not set\n");
1634 }
1635
1636 void client_setup_decor_and_functions(ObClient *self, gboolean reconfig)
1637 {
1638     /* start with everything (cept fullscreen) */
1639     self->decorations =
1640         (OB_FRAME_DECOR_TITLEBAR |
1641          OB_FRAME_DECOR_HANDLE |
1642          OB_FRAME_DECOR_GRIPS |
1643          OB_FRAME_DECOR_BORDER |
1644          OB_FRAME_DECOR_ICON |
1645          OB_FRAME_DECOR_ALLDESKTOPS |
1646          OB_FRAME_DECOR_ICONIFY |
1647          OB_FRAME_DECOR_MAXIMIZE |
1648          OB_FRAME_DECOR_SHADE |
1649          OB_FRAME_DECOR_CLOSE);
1650     self->functions =
1651         (OB_CLIENT_FUNC_RESIZE |
1652          OB_CLIENT_FUNC_MOVE |
1653          OB_CLIENT_FUNC_ICONIFY |
1654          OB_CLIENT_FUNC_MAXIMIZE |
1655          OB_CLIENT_FUNC_SHADE |
1656          OB_CLIENT_FUNC_CLOSE |
1657          OB_CLIENT_FUNC_BELOW |
1658          OB_CLIENT_FUNC_ABOVE |
1659          OB_CLIENT_FUNC_UNDECORATE);
1660
1661     if (!(self->min_size.width < self->max_size.width ||
1662           self->min_size.height < self->max_size.height))
1663         self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1664
1665     switch (self->type) {
1666     case OB_CLIENT_TYPE_NORMAL:
1667         /* normal windows retain all of the possible decorations and
1668            functionality, and can be fullscreen */
1669         self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1670         break;
1671
1672     case OB_CLIENT_TYPE_DIALOG:
1673         /* sometimes apps make dialog windows fullscreen for some reason (for
1674            e.g. kpdf does this..) */
1675         self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1676         break;
1677
1678     case OB_CLIENT_TYPE_UTILITY:
1679         /* these windows don't have anything added or removed by default */
1680         break;
1681
1682     case OB_CLIENT_TYPE_MENU:
1683     case OB_CLIENT_TYPE_TOOLBAR:
1684         /* these windows can't iconify or maximize */
1685         self->decorations &= ~(OB_FRAME_DECOR_ICONIFY |
1686                                OB_FRAME_DECOR_MAXIMIZE);
1687         self->functions &= ~(OB_CLIENT_FUNC_ICONIFY |
1688                              OB_CLIENT_FUNC_MAXIMIZE);
1689         break;
1690
1691     case OB_CLIENT_TYPE_SPLASH:
1692         /* these don't get get any decorations, and the only thing you can
1693            do with them is move them */
1694         self->decorations = 0;
1695         self->functions = OB_CLIENT_FUNC_MOVE;
1696         break;
1697
1698     case OB_CLIENT_TYPE_DESKTOP:
1699         /* these windows are not manipulated by the window manager */
1700         self->decorations = 0;
1701         self->functions = 0;
1702         break;
1703
1704     case OB_CLIENT_TYPE_DOCK:
1705         /* these windows are not manipulated by the window manager, but they
1706            can set below layer which has a special meaning */
1707         self->decorations = 0;
1708         self->functions = OB_CLIENT_FUNC_BELOW;
1709         break;
1710     }
1711
1712     /* Mwm Hints are applied subtractively to what has already been chosen for
1713        decor and functionality */
1714     if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
1715         if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
1716             if (! ((self->mwmhints.decorations & OB_MWM_DECOR_HANDLE) ||
1717                    (self->mwmhints.decorations & OB_MWM_DECOR_TITLE)))
1718             {
1719                 /* if the mwm hints request no handle or title, then all
1720                    decorations are disabled, but keep the border if that's
1721                    specified */
1722                 if (self->mwmhints.decorations & OB_MWM_DECOR_BORDER)
1723                     self->decorations = OB_FRAME_DECOR_BORDER;
1724                 else
1725                     self->decorations = 0;
1726             }
1727         }
1728     }
1729
1730     if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
1731         if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
1732             if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
1733                 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1734             if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
1735                 self->functions &= ~OB_CLIENT_FUNC_MOVE;
1736             /* dont let mwm hints kill any buttons
1737                if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
1738                self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
1739                if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
1740                self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1741             */
1742             /* dont let mwm hints kill the close button
1743                if (! (self->mwmhints.functions & MwmFunc_Close))
1744                self->functions &= ~OB_CLIENT_FUNC_CLOSE; */
1745         }
1746     }
1747
1748     if (!(self->functions & OB_CLIENT_FUNC_SHADE))
1749         self->decorations &= ~OB_FRAME_DECOR_SHADE;
1750     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY))
1751         self->decorations &= ~OB_FRAME_DECOR_ICONIFY;
1752     if (!(self->functions & OB_CLIENT_FUNC_RESIZE))
1753         self->decorations &= ~(OB_FRAME_DECOR_GRIPS | OB_FRAME_DECOR_HANDLE);
1754
1755     /* can't maximize without moving/resizing */
1756     if (!((self->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
1757           (self->functions & OB_CLIENT_FUNC_MOVE) &&
1758           (self->functions & OB_CLIENT_FUNC_RESIZE))) {
1759         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1760         self->decorations &= ~OB_FRAME_DECOR_MAXIMIZE;
1761     }
1762
1763     if (self->max_horz && self->max_vert) {
1764         /* you can't resize fully maximized windows */
1765         self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1766         /* kill the handle on fully maxed windows */
1767         self->decorations &= ~(OB_FRAME_DECOR_HANDLE | OB_FRAME_DECOR_GRIPS);
1768     }
1769
1770     /* If there are no decorations to remove, don't allow the user to try
1771        toggle the state */
1772     if (self->decorations == 0)
1773         self->functions &= ~OB_CLIENT_FUNC_UNDECORATE;
1774
1775     /* finally, the user can have requested no decorations, which overrides
1776        everything (but doesnt give it a border if it doesnt have one) */
1777     if (self->undecorated)
1778         self->decorations = 0;
1779
1780     /* if we don't have a titlebar, then we cannot shade! */
1781     if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1782         self->functions &= ~OB_CLIENT_FUNC_SHADE;
1783
1784     /* now we need to check against rules for the client's current state */
1785     if (self->fullscreen) {
1786         self->functions &= (OB_CLIENT_FUNC_CLOSE |
1787                             OB_CLIENT_FUNC_FULLSCREEN |
1788                             OB_CLIENT_FUNC_ICONIFY);
1789         self->decorations = 0;
1790     }
1791
1792     client_change_allowed_actions(self);
1793
1794     if (reconfig)
1795         /* force reconfigure to make sure decorations are updated */
1796         client_reconfigure(self, TRUE);
1797 }
1798
1799 static void client_change_allowed_actions(ObClient *self)
1800 {
1801     gulong actions[12];
1802     gint num = 0;
1803
1804     /* desktop windows are kept on all desktops */
1805     if (self->type != OB_CLIENT_TYPE_DESKTOP)
1806         actions[num++] = prop_atoms.net_wm_action_change_desktop;
1807
1808     if (self->functions & OB_CLIENT_FUNC_SHADE)
1809         actions[num++] = prop_atoms.net_wm_action_shade;
1810     if (self->functions & OB_CLIENT_FUNC_CLOSE)
1811         actions[num++] = prop_atoms.net_wm_action_close;
1812     if (self->functions & OB_CLIENT_FUNC_MOVE)
1813         actions[num++] = prop_atoms.net_wm_action_move;
1814     if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1815         actions[num++] = prop_atoms.net_wm_action_minimize;
1816     if (self->functions & OB_CLIENT_FUNC_RESIZE)
1817         actions[num++] = prop_atoms.net_wm_action_resize;
1818     if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1819         actions[num++] = prop_atoms.net_wm_action_fullscreen;
1820     if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1821         actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1822         actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1823     }
1824     if (self->functions & OB_CLIENT_FUNC_ABOVE)
1825         actions[num++] = prop_atoms.net_wm_action_above;
1826     if (self->functions & OB_CLIENT_FUNC_BELOW)
1827         actions[num++] = prop_atoms.net_wm_action_below;
1828     if (self->functions & OB_CLIENT_FUNC_UNDECORATE)
1829         actions[num++] = prop_atoms.ob_wm_action_undecorate;
1830
1831     PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1832
1833     /* make sure the window isn't breaking any rules now */
1834
1835     if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1836         if (self->frame) client_shade(self, FALSE);
1837         else self->shaded = FALSE;
1838     }
1839     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY) && self->iconic) {
1840         if (self->frame) client_iconify(self, FALSE, TRUE, FALSE);
1841         else self->iconic = FALSE;
1842     }
1843     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1844         if (self->frame) client_fullscreen(self, FALSE);
1845         else self->fullscreen = FALSE;
1846     }
1847     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
1848                                                          self->max_vert)) {
1849         if (self->frame) client_maximize(self, FALSE, 0);
1850         else self->max_vert = self->max_horz = FALSE;
1851     }
1852 }
1853
1854 void client_update_wmhints(ObClient *self)
1855 {
1856     XWMHints *hints;
1857
1858     /* assume a window takes input if it doesnt specify */
1859     self->can_focus = TRUE;
1860
1861     if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1862         gboolean ur;
1863
1864         if (hints->flags & InputHint)
1865             self->can_focus = hints->input;
1866
1867         /* only do this when first managing the window *AND* when we aren't
1868            starting up! */
1869         if (ob_state() != OB_STATE_STARTING && self->frame == NULL)
1870             if (hints->flags & StateHint)
1871                 self->iconic = hints->initial_state == IconicState;
1872
1873         ur = self->urgent;
1874         self->urgent = (hints->flags & XUrgencyHint);
1875         if (self->urgent && !ur)
1876             client_hilite(self, TRUE);
1877         else if (!self->urgent && ur && self->demands_attention)
1878             client_hilite(self, FALSE);
1879
1880         if (!(hints->flags & WindowGroupHint))
1881             hints->window_group = None;
1882
1883         /* did the group state change? */
1884         if (hints->window_group !=
1885             (self->group ? self->group->leader : None))
1886         {
1887             ObGroup *oldgroup = self->group;
1888
1889             /* remove from the old group if there was one */
1890             if (self->group != NULL) {
1891                 group_remove(self->group, self);
1892                 self->group = NULL;
1893             }
1894
1895             /* add ourself to the group if we have one */
1896             if (hints->window_group != None) {
1897                 self->group = group_add(hints->window_group, self);
1898             }
1899
1900             /* Put ourselves into the new group's transient tree, and remove
1901                ourselves from the old group's */
1902             client_update_transient_tree(self, oldgroup, self->group,
1903                                          self->transient_for_group,
1904                                          self->transient_for_group,
1905                                          client_direct_parent(self),
1906                                          client_direct_parent(self));
1907
1908             /* Lastly, being in a group, or not, can change if the window is
1909                transient for anything.
1910
1911                The logic for this is:
1912                self->transient = TRUE always if the window wants to be
1913                transient for something, even if transient_for was NULL because
1914                it wasn't in a group before.
1915
1916                If parents was NULL and oldgroup was NULL we can assume
1917                that when we add the new group, it will become transient for
1918                something.
1919
1920                If transient_for_group is TRUE, then it must have already
1921                had a group. If it is getting a new group, the above call to
1922                client_update_transient_tree has already taken care of
1923                everything ! If it is losing all group status then it will
1924                no longer be transient for anything and that needs to be
1925                updated.
1926             */
1927             if (self->transient &&
1928                 ((self->parents == NULL && oldgroup == NULL) ||
1929                  (self->transient_for_group && !self->group)))
1930                 client_update_transient_for(self);
1931         }
1932
1933         /* the WM_HINTS can contain an icon */
1934         if (hints->flags & IconPixmapHint)
1935             client_update_icons(self);
1936
1937         XFree(hints);
1938     }
1939 }
1940
1941 void client_update_title(ObClient *self)
1942 {
1943     gchar *data = NULL;
1944     gchar *visible = NULL;
1945
1946     g_free(self->title);
1947
1948     /* try netwm */
1949     if (!PROP_GETS(self->window, net_wm_name, utf8, &data)) {
1950         /* try old x stuff */
1951         if (!(PROP_GETS(self->window, wm_name, locale, &data)
1952               || PROP_GETS(self->window, wm_name, utf8, &data))) {
1953             if (self->transient) {
1954     /*
1955     GNOME alert windows are not given titles:
1956     http://developer.gnome.org/projects/gup/hig/draft_hig_new/windows-alert.html
1957     */
1958                 data = g_strdup("");
1959             } else
1960                 data = g_strdup("Unnamed Window");
1961         }
1962     }
1963
1964     if (self->client_machine) {
1965         visible = g_strdup_printf("%s (%s)", data, self->client_machine);
1966         g_free(data);
1967     } else
1968         visible = data;
1969
1970     if (self->not_responding) {
1971         data = visible;
1972         if (self->close_tried_term)
1973             visible = g_strdup_printf("%s - [%s]", data, _("Killing..."));
1974         else
1975             visible = g_strdup_printf("%s - [%s]", data, _("Not Responding"));
1976         g_free(data);
1977     }
1978
1979     PROP_SETS(self->window, net_wm_visible_name, visible);
1980     self->title = visible;
1981
1982     if (self->frame)
1983         frame_adjust_title(self->frame);
1984
1985     /* update the icon title */
1986     data = NULL;
1987     g_free(self->icon_title);
1988
1989     /* try netwm */
1990     if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1991         /* try old x stuff */
1992         if (!(PROP_GETS(self->window, wm_icon_name, locale, &data) ||
1993               PROP_GETS(self->window, wm_icon_name, utf8, &data)))
1994             data = g_strdup(self->title);
1995
1996     if (self->client_machine) {
1997         visible = g_strdup_printf("%s (%s)", data, self->client_machine);
1998         g_free(data);
1999     } else
2000         visible = data;
2001
2002     if (self->not_responding) {
2003         data = visible;
2004         if (self->close_tried_term)
2005             visible = g_strdup_printf("%s - [%s]", data, _("Killing..."));
2006         else
2007             visible = g_strdup_printf("%s - [%s]", data, _("Not Responding"));
2008         g_free(data);
2009     }
2010
2011     PROP_SETS(self->window, net_wm_visible_icon_name, visible);
2012     self->icon_title = visible;
2013 }
2014
2015 void client_update_strut(ObClient *self)
2016 {
2017     guint num;
2018     guint32 *data;
2019     gboolean got = FALSE;
2020     StrutPartial strut;
2021
2022     if (PROP_GETA32(self->window, net_wm_strut_partial, cardinal,
2023                     &data, &num)) {
2024         if (num == 12) {
2025             got = TRUE;
2026             STRUT_PARTIAL_SET(strut,
2027                               data[0], data[2], data[1], data[3],
2028                               data[4], data[5], data[8], data[9],
2029                               data[6], data[7], data[10], data[11]);
2030         }
2031         g_free(data);
2032     }
2033
2034     if (!got &&
2035         PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
2036         if (num == 4) {
2037             Rect *a;
2038
2039             got = TRUE;
2040
2041             /* use the screen's width/height */
2042             a = screen_physical_area_all_monitors();
2043
2044             STRUT_PARTIAL_SET(strut,
2045                               data[0], data[2], data[1], data[3],
2046                               a->y, a->y + a->height - 1,
2047                               a->x, a->x + a->width - 1,
2048                               a->y, a->y + a->height - 1,
2049                               a->x, a->x + a->width - 1);
2050             g_free(a);
2051         }
2052         g_free(data);
2053     }
2054
2055     if (!got)
2056         STRUT_PARTIAL_SET(strut, 0, 0, 0, 0,
2057                           0, 0, 0, 0, 0, 0, 0, 0);
2058
2059     if (!STRUT_EQUAL(strut, self->strut)) {
2060         self->strut = strut;
2061
2062         /* updating here is pointless while we're being mapped cuz we're not in
2063            the client list yet */
2064         if (self->frame)
2065             screen_update_areas();
2066     }
2067 }
2068
2069 void client_update_icons(ObClient *self)
2070 {
2071     guint num;
2072     guint32 *data;
2073     guint w, h, i, j;
2074
2075     for (i = 0; i < self->nicons; ++i)
2076         g_free(self->icons[i].data);
2077     if (self->nicons > 0)
2078         g_free(self->icons);
2079     self->nicons = 0;
2080
2081     if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
2082         /* figure out how many valid icons are in here */
2083         i = 0;
2084         while (num - i > 2) {
2085             w = data[i++];
2086             h = data[i++];
2087             i += w * h;
2088             if (i > num || w*h == 0) break;
2089             ++self->nicons;
2090         }
2091
2092         self->icons = g_new(ObClientIcon, self->nicons);
2093
2094         /* store the icons */
2095         i = 0;
2096         for (j = 0; j < self->nicons; ++j) {
2097             guint x, y, t;
2098
2099             w = self->icons[j].width = data[i++];
2100             h = self->icons[j].height = data[i++];
2101
2102             if (w*h == 0) continue;
2103
2104             self->icons[j].data = g_new(RrPixel32, w * h);
2105             for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
2106                 if (x >= w) {
2107                     x = 0;
2108                     ++y;
2109                 }
2110                 self->icons[j].data[t] =
2111                     (((data[i] >> 24) & 0xff) << RrDefaultAlphaOffset) +
2112                     (((data[i] >> 16) & 0xff) << RrDefaultRedOffset) +
2113                     (((data[i] >> 8) & 0xff) << RrDefaultGreenOffset) +
2114                     (((data[i] >> 0) & 0xff) << RrDefaultBlueOffset);
2115             }
2116             g_assert(i <= num);
2117         }
2118
2119         g_free(data);
2120     } else {
2121         XWMHints *hints;
2122
2123         if ((hints = XGetWMHints(ob_display, self->window))) {
2124             if (hints->flags & IconPixmapHint) {
2125                 self->nicons = 1;
2126                 self->icons = g_new(ObClientIcon, self->nicons);
2127                 xerror_set_ignore(TRUE);
2128                 if (!RrPixmapToRGBA(ob_rr_inst,
2129                                     hints->icon_pixmap,
2130                                     (hints->flags & IconMaskHint ?
2131                                      hints->icon_mask : None),
2132                                     &self->icons[0].width,
2133                                     &self->icons[0].height,
2134                                     &self->icons[0].data))
2135                 {
2136                     g_free(self->icons);
2137                     self->nicons = 0;
2138                 }
2139                 xerror_set_ignore(FALSE);
2140             }
2141             XFree(hints);
2142         }
2143     }
2144
2145     /* set the default icon onto the window
2146        in theory, this could be a race, but if a window doesn't set an icon
2147        or removes it entirely, it's not very likely it is going to set one
2148        right away afterwards
2149
2150        if it has parents, then one of them will have an icon already
2151     */
2152     if (self->nicons == 0 && !self->parents) {
2153         RrPixel32 *icon = ob_rr_theme->def_win_icon;
2154         gulong *data;
2155
2156         data = g_new(gulong, 48*48+2);
2157         data[0] = data[1] =  48;
2158         for (i = 0; i < 48*48; ++i)
2159             data[i+2] = (((icon[i] >> RrDefaultAlphaOffset) & 0xff) << 24) +
2160                 (((icon[i] >> RrDefaultRedOffset) & 0xff) << 16) +
2161                 (((icon[i] >> RrDefaultGreenOffset) & 0xff) << 8) +
2162                 (((icon[i] >> RrDefaultBlueOffset) & 0xff) << 0);
2163         PROP_SETA32(self->window, net_wm_icon, cardinal, data, 48*48+2);
2164         g_free(data);
2165     } else if (self->frame)
2166         /* don't draw the icon empty if we're just setting one now anyways,
2167            we'll get the property change any second */
2168         frame_adjust_icon(self->frame);
2169 }
2170
2171 void client_update_icon_geometry(ObClient *self)
2172 {
2173     guint num;
2174     guint32 *data;
2175
2176     RECT_SET(self->icon_geometry, 0, 0, 0, 0);
2177
2178     if (PROP_GETA32(self->window, net_wm_icon_geometry, cardinal, &data, &num)
2179         && num == 4)
2180     {
2181         /* don't let them set it with an area < 0 */
2182         RECT_SET(self->icon_geometry, data[0], data[1],
2183                  MAX(data[2],0), MAX(data[3],0));
2184     }
2185 }
2186
2187 static void client_get_session_ids(ObClient *self)
2188 {
2189     guint32 leader;
2190     gboolean got;
2191     gchar *s;
2192     gchar **ss;
2193
2194     if (!PROP_GET32(self->window, wm_client_leader, window, &leader))
2195         leader = None;
2196
2197     /* get the SM_CLIENT_ID */
2198     got = FALSE;
2199     if (leader)
2200         got = PROP_GETS(leader, sm_client_id, locale, &self->sm_client_id);
2201     if (!got)
2202         PROP_GETS(self->window, sm_client_id, locale, &self->sm_client_id);
2203
2204     /* get the WM_CLASS (name and class). make them "" if they are not
2205        provided */
2206     got = FALSE;
2207     if (leader)
2208         got = PROP_GETSS(leader, wm_class, locale, &ss);
2209     if (!got)
2210         got = PROP_GETSS(self->window, wm_class, locale, &ss);
2211
2212     if (got) {
2213         if (ss[0]) {
2214             self->name = g_strdup(ss[0]);
2215             if (ss[1])
2216                 self->class = g_strdup(ss[1]);
2217         }
2218         g_strfreev(ss);
2219     }
2220
2221     if (self->name == NULL) self->name = g_strdup("");
2222     if (self->class == NULL) self->class = g_strdup("");
2223
2224     /* get the WM_WINDOW_ROLE. make it "" if it is not provided */
2225     got = FALSE;
2226     if (leader)
2227         got = PROP_GETS(leader, wm_window_role, locale, &s);
2228     if (!got)
2229         got = PROP_GETS(self->window, wm_window_role, locale, &s);
2230
2231     if (got)
2232         self->role = s;
2233     else
2234         self->role = g_strdup("");
2235
2236     /* get the WM_COMMAND */
2237     got = FALSE;
2238
2239     if (leader)
2240         got = PROP_GETSS(leader, wm_command, locale, &ss);
2241     if (!got)
2242         got = PROP_GETSS(self->window, wm_command, locale, &ss);
2243
2244     if (got) {
2245         /* merge/mash them all together */
2246         gchar *merge = NULL;
2247         gint i;
2248
2249         for (i = 0; ss[i]; ++i) {
2250             gchar *tmp = merge;
2251             if (merge)
2252                 merge = g_strconcat(merge, ss[i], NULL);
2253             else
2254                 merge = g_strconcat(ss[i], NULL);
2255             g_free(tmp);
2256         }
2257         g_strfreev(ss);
2258
2259         self->wm_command = merge;
2260     }
2261
2262     /* get the WM_CLIENT_MACHINE */
2263     got = FALSE;
2264     if (leader)
2265         got = PROP_GETS(leader, wm_client_machine, locale, &s);
2266     if (!got)
2267         got = PROP_GETS(self->window, wm_client_machine, locale, &s);
2268
2269     if (got) {
2270         gchar localhost[128];
2271         guint32 pid;
2272
2273         gethostname(localhost, 127);
2274         localhost[127] = '\0';
2275         if (strcmp(localhost, s) != 0)
2276             self->client_machine = s;
2277         else
2278             g_free(s);
2279
2280         /* see if it has the PID set too (the PID requires that the
2281            WM_CLIENT_MACHINE be set) */
2282         if (PROP_GET32(self->window, net_wm_pid, cardinal, &pid))
2283             self->pid = pid;
2284     }
2285 }
2286
2287 static void client_change_wm_state(ObClient *self)
2288 {
2289     gulong state[2];
2290     glong old;
2291
2292     old = self->wmstate;
2293
2294     if (self->shaded || self->iconic ||
2295         (self->desktop != DESKTOP_ALL && self->desktop != screen_desktop))
2296     {
2297         self->wmstate = IconicState;
2298     } else
2299         self->wmstate = NormalState;
2300
2301     if (old != self->wmstate) {
2302         PROP_MSG(self->window, kde_wm_change_state,
2303                  self->wmstate, 1, 0, 0);
2304
2305         state[0] = self->wmstate;
2306         state[1] = None;
2307         PROP_SETA32(self->window, wm_state, wm_state, state, 2);
2308     }
2309 }
2310
2311 static void client_change_state(ObClient *self)
2312 {
2313     gulong netstate[12];
2314     guint num;
2315
2316     num = 0;
2317     if (self->modal)
2318         netstate[num++] = prop_atoms.net_wm_state_modal;
2319     if (self->shaded)
2320         netstate[num++] = prop_atoms.net_wm_state_shaded;
2321     if (self->iconic)
2322         netstate[num++] = prop_atoms.net_wm_state_hidden;
2323     if (self->skip_taskbar)
2324         netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
2325     if (self->skip_pager)
2326         netstate[num++] = prop_atoms.net_wm_state_skip_pager;
2327     if (self->fullscreen)
2328         netstate[num++] = prop_atoms.net_wm_state_fullscreen;
2329     if (self->max_vert)
2330         netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
2331     if (self->max_horz)
2332         netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
2333     if (self->above)
2334         netstate[num++] = prop_atoms.net_wm_state_above;
2335     if (self->below)
2336         netstate[num++] = prop_atoms.net_wm_state_below;
2337     if (self->demands_attention)
2338         netstate[num++] = prop_atoms.net_wm_state_demands_attention;
2339     if (self->undecorated)
2340         netstate[num++] = prop_atoms.ob_wm_state_undecorated;
2341     PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
2342
2343     if (self->frame)
2344         frame_adjust_state(self->frame);
2345 }
2346
2347 ObClient *client_search_focus_tree(ObClient *self)
2348 {
2349     GSList *it;
2350     ObClient *ret;
2351
2352     for (it = self->transients; it; it = g_slist_next(it)) {
2353         if (client_focused(it->data)) return it->data;
2354         if ((ret = client_search_focus_tree(it->data))) return ret;
2355     }
2356     return NULL;
2357 }
2358
2359 ObClient *client_search_focus_tree_full(ObClient *self)
2360 {
2361     if (self->parents) {
2362         GSList *it;
2363
2364         for (it = self->parents; it; it = g_slist_next(it)) {
2365             ObClient *c = it->data;
2366             if ((c = client_search_focus_tree_full(it->data))) return c;
2367         }
2368
2369         return NULL;
2370     }
2371     else {
2372         /* this function checks the whole tree, the client_search_focus_tree
2373            does not, so we need to check this window */
2374         if (client_focused(self))
2375             return self;
2376         return client_search_focus_tree(self);
2377     }
2378 }
2379
2380 ObClient *client_search_focus_group_full(ObClient *self)
2381 {
2382     GSList *it;
2383
2384     if (self->group) {
2385         for (it = self->group->members; it; it = g_slist_next(it)) {
2386             ObClient *c = it->data;
2387
2388             if (client_focused(c)) return c;
2389             if ((c = client_search_focus_tree(it->data))) return c;
2390         }
2391     } else
2392         if (client_focused(self)) return self;
2393     return NULL;
2394 }
2395
2396 gboolean client_has_parent(ObClient *self)
2397 {
2398     return self->parents != NULL;
2399 }
2400
2401 static ObStackingLayer calc_layer(ObClient *self)
2402 {
2403     ObStackingLayer l;
2404     Rect *monitor;
2405
2406     monitor = screen_physical_area_monitor(client_monitor(self));
2407
2408     if (self->type == OB_CLIENT_TYPE_DESKTOP)
2409         l = OB_STACKING_LAYER_DESKTOP;
2410     else if (self->type == OB_CLIENT_TYPE_DOCK) {
2411         if (self->below) l = OB_STACKING_LAYER_NORMAL;
2412         else l = OB_STACKING_LAYER_ABOVE;
2413     }
2414     else if ((self->fullscreen ||
2415               /* No decorations and fills the monitor = oldskool fullscreen.
2416                  But not for maximized windows.
2417               */
2418               (self->decorations == 0 &&
2419                !(self->max_horz && self->max_vert) &&
2420                RECT_EQUAL(self->area, *monitor))) &&
2421              (client_focused(self) || client_search_focus_tree(self)))
2422         l = OB_STACKING_LAYER_FULLSCREEN;
2423     else if (self->above) l = OB_STACKING_LAYER_ABOVE;
2424     else if (self->below) l = OB_STACKING_LAYER_BELOW;
2425     else l = OB_STACKING_LAYER_NORMAL;
2426
2427     g_free(monitor);
2428
2429     return l;
2430 }
2431
2432 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
2433                                         ObStackingLayer min)
2434 {
2435     ObStackingLayer old, own;
2436     GSList *it;
2437
2438     old = self->layer;
2439     own = calc_layer(self);
2440     self->layer = MAX(own, min);
2441
2442     if (self->layer != old) {
2443         stacking_remove(CLIENT_AS_WINDOW(self));
2444         stacking_add_nonintrusive(CLIENT_AS_WINDOW(self));
2445     }
2446
2447     for (it = self->transients; it; it = g_slist_next(it))
2448         client_calc_layer_recursive(it->data, orig,
2449                                     self->layer);
2450 }
2451
2452 void client_calc_layer(ObClient *self)
2453 {
2454     ObClient *orig;
2455     GSList *it;
2456
2457     orig = self;
2458
2459     /* transients take on the layer of their parents */
2460     it = client_search_all_top_parents(self);
2461
2462     for (; it; it = g_slist_next(it))
2463         client_calc_layer_recursive(it->data, orig, 0);
2464 }
2465
2466 gboolean client_should_show(ObClient *self)
2467 {
2468     if (self->iconic)
2469         return FALSE;
2470     if (client_normal(self) && screen_showing_desktop)
2471         return FALSE;
2472     if (self->desktop == screen_desktop || self->desktop == DESKTOP_ALL)
2473         return TRUE;
2474
2475     return FALSE;
2476 }
2477
2478 gboolean client_show(ObClient *self)
2479 {
2480     gboolean show = FALSE;
2481
2482     if (client_should_show(self)) {
2483         frame_show(self->frame);
2484         show = TRUE;
2485
2486         /* According to the ICCCM (sec 4.1.3.1) when a window is not visible,
2487            it needs to be in IconicState. This includes when it is on another
2488            desktop!
2489         */
2490         client_change_wm_state(self);
2491     }
2492     return show;
2493 }
2494
2495 gboolean client_hide(ObClient *self)
2496 {
2497     gboolean hide = FALSE;
2498
2499     if (!client_should_show(self)) {
2500         if (self == focus_client) {
2501             /* if there is a grab going on, then we need to cancel it. if we
2502                move focus during the grab, applications will get
2503                NotifyWhileGrabbed events and ignore them !
2504
2505                actions should not rely on being able to move focus during an
2506                interactive grab.
2507             */
2508             event_cancel_all_key_grabs();
2509         }
2510
2511         /* We don't need to ignore enter events here.
2512            The window can hide/iconify in 3 different ways:
2513            1 - through an x message. in this case we ignore all enter events
2514                caused by responding to the x message (unless underMouse)
2515            2 - by a keyboard action. in this case we ignore all enter events
2516                caused by the action
2517            3 - by a mouse action. in this case they are doing stuff with the
2518                mouse and focus _should_ move.
2519
2520            Also in action_end, we simulate an enter event that can't be ignored
2521            so trying to ignore them is futile in case 3 anyways
2522         */
2523
2524         frame_hide(self->frame);
2525         hide = TRUE;
2526
2527         /* According to the ICCCM (sec 4.1.3.1) when a window is not visible,
2528            it needs to be in IconicState. This includes when it is on another
2529            desktop!
2530         */
2531         client_change_wm_state(self);
2532     }
2533     return hide;
2534 }
2535
2536 void client_showhide(ObClient *self)
2537 {
2538     if (!client_show(self))
2539         client_hide(self);
2540 }
2541
2542 gboolean client_normal(ObClient *self) {
2543     return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
2544               self->type == OB_CLIENT_TYPE_DOCK ||
2545               self->type == OB_CLIENT_TYPE_SPLASH);
2546 }
2547
2548 gboolean client_helper(ObClient *self)
2549 {
2550     return (self->type == OB_CLIENT_TYPE_UTILITY ||
2551             self->type == OB_CLIENT_TYPE_MENU ||
2552             self->type == OB_CLIENT_TYPE_TOOLBAR);
2553 }
2554
2555 gboolean client_mouse_focusable(ObClient *self)
2556 {
2557     return !(self->type == OB_CLIENT_TYPE_MENU ||
2558              self->type == OB_CLIENT_TYPE_TOOLBAR ||
2559              self->type == OB_CLIENT_TYPE_SPLASH ||
2560              self->type == OB_CLIENT_TYPE_DOCK);
2561 }
2562
2563 gboolean client_enter_focusable(ObClient *self)
2564 {
2565     /* you can focus desktops but it shouldn't on enter */
2566     return (client_mouse_focusable(self) &&
2567             self->type != OB_CLIENT_TYPE_DESKTOP);
2568 }
2569
2570
2571 static void client_apply_startup_state(ObClient *self,
2572                                        gint x, gint y, gint w, gint h)
2573 {
2574     /* save the states that we are going to apply */
2575     gboolean iconic = self->iconic;
2576     gboolean fullscreen = self->fullscreen;
2577     gboolean undecorated = self->undecorated;
2578     gboolean shaded = self->shaded;
2579     gboolean demands_attention = self->demands_attention;
2580     gboolean max_horz = self->max_horz;
2581     gboolean max_vert = self->max_vert;
2582     Rect oldarea;
2583     gint l;
2584
2585     /* turn them all off in the client, so they won't affect the window
2586        being placed */
2587     self->iconic = self->fullscreen = self->undecorated = self->shaded =
2588         self->demands_attention = self->max_horz = self->max_vert = FALSE;
2589
2590     /* move the client to its placed position, or it it's already there,
2591        generate a ConfigureNotify telling the client where it is.
2592
2593        do this after adjusting the frame. otherwise it gets all weird and
2594        clients don't work right
2595
2596        do this before applying the states so they have the correct
2597        pre-max/pre-fullscreen values
2598     */
2599     client_try_configure(self, &x, &y, &w, &h, &l, &l, FALSE);
2600     ob_debug("placed window 0x%x at %d, %d with size %d x %d\n",
2601              self->window, x, y, w, h);
2602     /* save the area, and make it where it should be for the premax stuff */
2603     oldarea = self->area;
2604     RECT_SET(self->area, x, y, w, h);
2605
2606     /* apply the states. these are in a carefully crafted order.. */
2607
2608     if (iconic)
2609         client_iconify(self, TRUE, FALSE, TRUE);
2610     if (fullscreen)
2611         client_fullscreen(self, TRUE);
2612     if (undecorated)
2613         client_set_undecorated(self, TRUE);
2614     if (shaded)
2615         client_shade(self, TRUE);
2616     if (demands_attention)
2617         client_hilite(self, TRUE);
2618
2619     if (max_vert && max_horz)
2620         client_maximize(self, TRUE, 0);
2621     else if (max_vert)
2622         client_maximize(self, TRUE, 2);
2623     else if (max_horz)
2624         client_maximize(self, TRUE, 1);
2625
2626     /* if the window hasn't been configured yet, then do so now, in fact the
2627        x,y,w,h may _not_ be the same as the area rect, which can end up
2628        meaning that the client isn't properly moved/resized by the fullscreen
2629        function
2630        pho can cause this because it maps at size of the screen but not 0,0
2631        so openbox moves it on screen to 0,0 (thus x,y=0,0 and area.x,y don't).
2632        then fullscreen'ing makes it go to 0,0 which it thinks it already is at
2633        cuz thats where the pre-fullscreen will be. however the actual area is
2634        not, so this needs to be called even if we have fullscreened/maxed
2635     */
2636     self->area = oldarea;
2637     client_configure(self, x, y, w, h, FALSE, TRUE, FALSE);
2638
2639     /* set the desktop hint, to make sure that it always exists */
2640     PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
2641
2642     /* nothing to do for the other states:
2643        skip_taskbar
2644        skip_pager
2645        modal
2646        above
2647        below
2648     */
2649 }
2650
2651 void client_gravity_resize_w(ObClient *self, gint *x, gint oldw, gint neww)
2652 {
2653     /* these should be the current values. this is for when you're not moving,
2654        just resizing */
2655     g_assert(*x == self->area.x);
2656     g_assert(oldw == self->area.width);
2657
2658     /* horizontal */
2659     switch (self->gravity) {
2660     default:
2661     case NorthWestGravity:
2662     case WestGravity:
2663     case SouthWestGravity:
2664     case StaticGravity:
2665     case ForgetGravity:
2666         break;
2667     case NorthGravity:
2668     case CenterGravity:
2669     case SouthGravity:
2670         *x -= (neww - oldw) / 2;
2671         break;
2672     case NorthEastGravity:
2673     case EastGravity:
2674     case SouthEastGravity:
2675         *x -= neww - oldw;
2676         break;
2677     }
2678 }
2679
2680 void client_gravity_resize_h(ObClient *self, gint *y, gint oldh, gint newh)
2681 {
2682     /* these should be the current values. this is for when you're not moving,
2683        just resizing */
2684     g_assert(*y == self->area.y);
2685     g_assert(oldh == self->area.height);
2686
2687     /* vertical */
2688     switch (self->gravity) {
2689     default:
2690     case NorthWestGravity:
2691     case NorthGravity:
2692     case NorthEastGravity:
2693     case StaticGravity:
2694     case ForgetGravity:
2695         break;
2696     case WestGravity:
2697     case CenterGravity:
2698     case EastGravity:
2699         *y -= (newh - oldh) / 2;
2700         break;
2701     case SouthWestGravity:
2702     case SouthGravity:
2703     case SouthEastGravity:
2704         *y -= newh - oldh;
2705         break;
2706     }
2707 }
2708
2709 void client_try_configure(ObClient *self, gint *x, gint *y, gint *w, gint *h,
2710                           gint *logicalw, gint *logicalh,
2711                           gboolean user)
2712 {
2713     Rect desired = {*x, *y, *w, *h};
2714     frame_rect_to_frame(self->frame, &desired);
2715
2716     /* make the frame recalculate its dimentions n shit without changing
2717        anything visible for real, this way the constraints below can work with
2718        the updated frame dimensions. */
2719     frame_adjust_area(self->frame, FALSE, TRUE, TRUE);
2720
2721     /* gets the frame's position */
2722     frame_client_gravity(self->frame, x, y);
2723
2724     /* these positions are frame positions, not client positions */
2725
2726     /* set the size and position if fullscreen */
2727     if (self->fullscreen) {
2728         Rect *a;
2729         guint i;
2730
2731         i = screen_find_monitor(&desired);
2732         a = screen_physical_area_monitor(i);
2733
2734         *x = a->x;
2735         *y = a->y;
2736         *w = a->width;
2737         *h = a->height;
2738
2739         user = FALSE; /* ignore if the client can't be moved/resized when it
2740                          is fullscreening */
2741
2742         g_free(a);
2743     } else if (self->max_horz || self->max_vert) {
2744         Rect *a;
2745         guint i;
2746
2747         /* use all possible struts when maximizing to the full screen */
2748         i = screen_find_monitor(&desired);
2749         a = screen_area(self->desktop, i,
2750                         (self->max_horz && self->max_vert ? NULL : &desired));
2751
2752         /* set the size and position if maximized */
2753         if (self->max_horz) {
2754             *x = a->x;
2755             *w = a->width - self->frame->size.left - self->frame->size.right;
2756         }
2757         if (self->max_vert) {
2758             *y = a->y;
2759             *h = a->height - self->frame->size.top - self->frame->size.bottom;
2760         }
2761
2762         user = FALSE; /* ignore if the client can't be moved/resized when it
2763                          is maximizing */
2764
2765         g_free(a);
2766     }
2767
2768     /* gets the client's position */
2769     frame_frame_gravity(self->frame, x, y);
2770
2771     /* work within the prefered sizes given by the window */
2772     if (!(*w == self->area.width && *h == self->area.height)) {
2773         gint basew, baseh, minw, minh;
2774         gint incw, inch;
2775         gfloat minratio, maxratio;
2776
2777         incw = self->fullscreen || self->max_horz ? 1 : self->size_inc.width;
2778         inch = self->fullscreen || self->max_vert ? 1 : self->size_inc.height;
2779         minratio = self->fullscreen || (self->max_horz && self->max_vert) ?
2780             0 : self->min_ratio;
2781         maxratio = self->fullscreen || (self->max_horz && self->max_vert) ?
2782             0 : self->max_ratio;
2783
2784         /* base size is substituted with min size if not specified */
2785         if (self->base_size.width || self->base_size.height) {
2786             basew = self->base_size.width;
2787             baseh = self->base_size.height;
2788         } else {
2789             basew = self->min_size.width;
2790             baseh = self->min_size.height;
2791         }
2792         /* min size is substituted with base size if not specified */
2793         if (self->min_size.width || self->min_size.height) {
2794             minw = self->min_size.width;
2795             minh = self->min_size.height;
2796         } else {
2797             minw = self->base_size.width;
2798             minh = self->base_size.height;
2799         }
2800
2801         /* if this is a user-requested resize, then check against min/max
2802            sizes */
2803
2804         /* smaller than min size or bigger than max size? */
2805         if (*w > self->max_size.width) *w = self->max_size.width;
2806         if (*w < minw) *w = minw;
2807         if (*h > self->max_size.height) *h = self->max_size.height;
2808         if (*h < minh) *h = minh;
2809
2810         *w -= basew;
2811         *h -= baseh;
2812
2813         /* keep to the increments */
2814         *w /= incw;
2815         *h /= inch;
2816
2817         /* you cannot resize to nothing */
2818         if (basew + *w < 1) *w = 1 - basew;
2819         if (baseh + *h < 1) *h = 1 - baseh;
2820
2821         /* save the logical size */
2822         *logicalw = incw > 1 ? *w : *w + basew;
2823         *logicalh = inch > 1 ? *h : *h + baseh;
2824
2825         *w *= incw;
2826         *h *= inch;
2827
2828         *w += basew;
2829         *h += baseh;
2830
2831         /* adjust the height to match the width for the aspect ratios.
2832            for this, min size is not substituted for base size ever. */
2833         *w -= self->base_size.width;
2834         *h -= self->base_size.height;
2835
2836         if (minratio)
2837             if (*h * minratio > *w) {
2838                 *h = (gint)(*w / minratio);
2839
2840                 /* you cannot resize to nothing */
2841                 if (*h < 1) {
2842                     *h = 1;
2843                     *w = (gint)(*h * minratio);
2844                 }
2845             }
2846         if (maxratio)
2847             if (*h * maxratio < *w) {
2848                 *h = (gint)(*w / maxratio);
2849
2850                 /* you cannot resize to nothing */
2851                 if (*h < 1) {
2852                     *h = 1;
2853                     *w = (gint)(*h * minratio);
2854                 }
2855             }
2856
2857         *w += self->base_size.width;
2858         *h += self->base_size.height;
2859     }
2860
2861     /* these override the above states! if you cant move you can't move! */
2862     if (user) {
2863         if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
2864             *x = self->area.x;
2865             *y = self->area.y;
2866         }
2867         if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
2868             *w = self->area.width;
2869             *h = self->area.height;
2870         }
2871     }
2872
2873     g_assert(*w > 0);
2874     g_assert(*h > 0);
2875 }
2876
2877
2878 void client_configure(ObClient *self, gint x, gint y, gint w, gint h,
2879                       gboolean user, gboolean final, gboolean force_reply)
2880 {
2881     gint oldw, oldh;
2882     gboolean send_resize_client;
2883     gboolean moved = FALSE, resized = FALSE, rootmoved = FALSE;
2884     gboolean fmoved, fresized;
2885     guint fdecor = self->frame->decorations;
2886     gboolean fhorz = self->frame->max_horz;
2887     gboolean fvert = self->frame->max_vert;
2888     gint logicalw, logicalh;
2889
2890     /* find the new x, y, width, and height (and logical size) */
2891     client_try_configure(self, &x, &y, &w, &h, &logicalw, &logicalh, user);
2892
2893     /* set the logical size if things changed */
2894     if (!(w == self->area.width && h == self->area.height))
2895         SIZE_SET(self->logical_size, logicalw, logicalh);
2896
2897     /* figure out if we moved or resized or what */
2898     moved = (x != self->area.x || y != self->area.y);
2899     resized = (w != self->area.width || h != self->area.height);
2900
2901     oldw = self->area.width;
2902     oldh = self->area.height;
2903     RECT_SET(self->area, x, y, w, h);
2904
2905     /* for app-requested resizes, always resize if 'resized' is true.
2906        for user-requested ones, only resize if final is true, or when
2907        resizing in redraw mode */
2908     send_resize_client = ((!user && resized) ||
2909                           (user && (final ||
2910                                     (resized && config_resize_redraw))));
2911
2912     /* if the client is enlarging, then resize the client before the frame */
2913     if (send_resize_client && (w > oldw || h > oldh)) {
2914         XMoveResizeWindow(ob_display, self->window,
2915                           self->frame->size.left, self->frame->size.top,
2916                           MAX(w, oldw), MAX(h, oldh));
2917         frame_adjust_client_area(self->frame);
2918     }
2919
2920     /* find the frame's dimensions and move/resize it */
2921     fmoved = moved;
2922     fresized = resized;
2923
2924     /* if decorations changed, then readjust everything for the frame */
2925     if (self->decorations != fdecor ||
2926         self->max_horz != fhorz || self->max_vert != fvert)
2927     {
2928         fmoved = fresized = TRUE;
2929     }
2930
2931     /* adjust the frame */
2932     if (fmoved || fresized) {
2933         gulong ignore_start;
2934         if (!user)
2935             ignore_start = event_start_ignore_all_enters();
2936
2937         frame_adjust_area(self->frame, fmoved, fresized, FALSE);
2938
2939         if (!user)
2940             event_end_ignore_all_enters(ignore_start);
2941     }
2942
2943     if (!user || final) {
2944         gint oldrx = self->root_pos.x;
2945         gint oldry = self->root_pos.y;
2946         /* we have reset the client to 0 border width, so don't include
2947            it in these coords */
2948         POINT_SET(self->root_pos,
2949                   self->frame->area.x + self->frame->size.left -
2950                   self->border_width,
2951                   self->frame->area.y + self->frame->size.top -
2952                   self->border_width);
2953         if (self->root_pos.x != oldrx || self->root_pos.y != oldry)
2954             rootmoved = TRUE;
2955     }
2956
2957     /* This is kinda tricky and should not be changed.. let me explain!
2958
2959        When user = FALSE, then the request is coming from the application
2960        itself, and we are more strict about when to send a synthetic
2961        ConfigureNotify.  We strictly follow the rules of the ICCCM sec 4.1.5
2962        in this case (if force_reply is true)
2963
2964        When user = TRUE, then the request is coming from "us", like when we
2965        maximize a window or something.  In this case we are more lenient.  We
2966        used to follow the same rules as above, but _Java_ Swing can't handle
2967        this. So just to appease Swing, when user = TRUE, we always send
2968        a synthetic ConfigureNotify to give the window its root coordinates.
2969     */
2970     if ((!user && !resized && (rootmoved || force_reply)) ||
2971         (user && final && rootmoved))
2972     {
2973         XEvent event;
2974
2975         event.type = ConfigureNotify;
2976         event.xconfigure.display = ob_display;
2977         event.xconfigure.event = self->window;
2978         event.xconfigure.window = self->window;
2979
2980         ob_debug("Sending ConfigureNotify to %s for %d,%d %dx%d\n",
2981                  self->title, self->root_pos.x, self->root_pos.y, w, h);
2982
2983         /* root window real coords */
2984         event.xconfigure.x = self->root_pos.x;
2985         event.xconfigure.y = self->root_pos.y;
2986         event.xconfigure.width = w;
2987         event.xconfigure.height = h;
2988         event.xconfigure.border_width = self->border_width;
2989         event.xconfigure.above = None;
2990         event.xconfigure.override_redirect = FALSE;
2991         XSendEvent(event.xconfigure.display, event.xconfigure.window,
2992                    FALSE, StructureNotifyMask, &event);
2993     }
2994
2995     /* if the client is shrinking, then resize the frame before the client.
2996
2997        both of these resize sections may run, because the top one only resizes
2998        in the direction that is growing
2999      */
3000     if (send_resize_client && (w <= oldw || h <= oldh)) {
3001         frame_adjust_client_area(self->frame);
3002         XMoveResizeWindow(ob_display, self->window,
3003                           self->frame->size.left, self->frame->size.top, w, h);
3004     }
3005
3006     XFlush(ob_display);
3007 }
3008
3009 void client_fullscreen(ObClient *self, gboolean fs)
3010 {
3011     gint x, y, w, h;
3012
3013     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
3014         self->fullscreen == fs) return;                   /* already done */
3015
3016     self->fullscreen = fs;
3017     client_change_state(self); /* change the state hints on the client */
3018
3019     if (fs) {
3020         self->pre_fullscreen_area = self->area;
3021         /* if the window is maximized, its area isn't all that meaningful.
3022            save it's premax area instead. */
3023         if (self->max_horz) {
3024             self->pre_fullscreen_area.x = self->pre_max_area.x;
3025             self->pre_fullscreen_area.width = self->pre_max_area.width;
3026         }
3027         if (self->max_vert) {
3028             self->pre_fullscreen_area.y = self->pre_max_area.y;
3029             self->pre_fullscreen_area.height = self->pre_max_area.height;
3030         }
3031
3032         /* these will help configure_full figure out where to fullscreen
3033            the window */
3034         x = self->area.x;
3035         y = self->area.y;
3036         w = self->area.width;
3037         h = self->area.height;
3038     } else {
3039         g_assert(self->pre_fullscreen_area.width > 0 &&
3040                  self->pre_fullscreen_area.height > 0);
3041
3042         x = self->pre_fullscreen_area.x;
3043         y = self->pre_fullscreen_area.y;
3044         w = self->pre_fullscreen_area.width;
3045         h = self->pre_fullscreen_area.height;
3046         RECT_SET(self->pre_fullscreen_area, 0, 0, 0, 0);
3047     }
3048
3049     ob_debug("Window %s going fullscreen (%d)\n",
3050              self->title, self->fullscreen);
3051
3052     client_setup_decor_and_functions(self, FALSE);
3053     client_move_resize(self, x, y, w, h);
3054
3055     /* and adjust our layer/stacking. do this after resizing the window,
3056        and applying decorations, because windows which fill the screen are
3057        considered "fullscreen" and it affects their layer */
3058     client_calc_layer(self);
3059
3060     if (fs) {
3061         /* try focus us when we go into fullscreen mode */
3062         client_focus(self);
3063     }
3064 }
3065
3066 static void client_iconify_recursive(ObClient *self,
3067                                      gboolean iconic, gboolean curdesk,
3068                                      gboolean hide_animation)
3069 {
3070     GSList *it;
3071     gboolean changed = FALSE;
3072
3073
3074     if (self->iconic != iconic) {
3075         ob_debug("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"),
3076                  self->window);
3077
3078         if (iconic) {
3079             /* don't let non-normal windows iconify along with their parents
3080                or whatever */
3081             if (client_normal(self)) {
3082                 self->iconic = iconic;
3083
3084                 /* update the focus lists.. iconic windows go to the bottom of
3085                    the list */
3086                 focus_order_to_bottom(self);
3087
3088                 changed = TRUE;
3089             }
3090         } else {
3091             self->iconic = iconic;
3092
3093             if (curdesk && self->desktop != screen_desktop &&
3094                 self->desktop != DESKTOP_ALL)
3095                 client_set_desktop(self, screen_desktop, FALSE, FALSE);
3096
3097             /* this puts it after the current focused window */
3098             focus_order_remove(self);
3099             focus_order_add_new(self);
3100
3101             changed = TRUE;
3102         }
3103     }
3104
3105     if (changed) {
3106         client_change_state(self);
3107         if (config_animate_iconify && !hide_animation)
3108             frame_begin_iconify_animation(self->frame, iconic);
3109         /* do this after starting the animation so it doesn't flash */
3110         client_showhide(self);
3111     }
3112
3113     /* iconify all direct transients, and deiconify all transients
3114        (non-direct too) */
3115     for (it = self->transients; it; it = g_slist_next(it))
3116         if (it->data != self)
3117             if (client_is_direct_child(self, it->data) || !iconic)
3118                 client_iconify_recursive(it->data, iconic, curdesk,
3119                                          hide_animation);
3120 }
3121
3122 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk,
3123                     gboolean hide_animation)
3124 {
3125     if (self->functions & OB_CLIENT_FUNC_ICONIFY || !iconic) {
3126         /* move up the transient chain as far as possible first */
3127         self = client_search_top_direct_parent(self);
3128         client_iconify_recursive(self, iconic, curdesk, hide_animation);
3129     }
3130 }
3131
3132 void client_maximize(ObClient *self, gboolean max, gint dir)
3133 {
3134     gint x, y, w, h;
3135
3136     g_assert(dir == 0 || dir == 1 || dir == 2);
3137     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE)) return; /* can't */
3138
3139     /* check if already done */
3140     if (max) {
3141         if (dir == 0 && self->max_horz && self->max_vert) return;
3142         if (dir == 1 && self->max_horz) return;
3143         if (dir == 2 && self->max_vert) return;
3144     } else {
3145         if (dir == 0 && !self->max_horz && !self->max_vert) return;
3146         if (dir == 1 && !self->max_horz) return;
3147         if (dir == 2 && !self->max_vert) return;
3148     }
3149
3150     /* these will help configure_full figure out which screen to fill with
3151        the window */
3152     x = self->area.x;
3153     y = self->area.y;
3154     w = self->area.width;
3155     h = self->area.height;
3156
3157     if (max) {
3158         if ((dir == 0 || dir == 1) && !self->max_horz) { /* horz */
3159             RECT_SET(self->pre_max_area,
3160                      self->area.x, self->pre_max_area.y,
3161                      self->area.width, self->pre_max_area.height);
3162         }
3163         if ((dir == 0 || dir == 2) && !self->max_vert) { /* vert */
3164             RECT_SET(self->pre_max_area,
3165                      self->pre_max_area.x, self->area.y,
3166                      self->pre_max_area.width, self->area.height);
3167         }
3168     } else {
3169         if ((dir == 0 || dir == 1) && self->max_horz) { /* horz */
3170             g_assert(self->pre_max_area.width > 0);
3171
3172             x = self->pre_max_area.x;
3173             w = self->pre_max_area.width;
3174
3175             RECT_SET(self->pre_max_area, 0, self->pre_max_area.y,
3176                      0, self->pre_max_area.height);
3177         }
3178         if ((dir == 0 || dir == 2) && self->max_vert) { /* vert */
3179             g_assert(self->pre_max_area.height > 0);
3180
3181             y = self->pre_max_area.y;
3182             h = self->pre_max_area.height;
3183
3184             RECT_SET(self->pre_max_area, self->pre_max_area.x, 0,
3185                      self->pre_max_area.width, 0);
3186         }
3187     }
3188
3189     if (dir == 0 || dir == 1) /* horz */
3190         self->max_horz = max;
3191     if (dir == 0 || dir == 2) /* vert */
3192         self->max_vert = max;
3193
3194     client_change_state(self); /* change the state hints on the client */
3195
3196     client_setup_decor_and_functions(self, FALSE);
3197     client_move_resize(self, x, y, w, h);
3198 }
3199
3200 void client_shade(ObClient *self, gboolean shade)
3201 {
3202     if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
3203          shade) ||                         /* can't shade */
3204         self->shaded == shade) return;     /* already done */
3205
3206     self->shaded = shade;
3207     client_change_state(self);
3208     client_change_wm_state(self); /* the window is being hidden/shown */
3209     /* resize the frame to just the titlebar */
3210     frame_adjust_area(self->frame, FALSE, TRUE, FALSE);
3211 }
3212
3213 static void client_ping_event(ObClient *self, gboolean dead)
3214 {
3215     self->not_responding = dead;
3216     client_update_title(self);
3217
3218     if (!dead) {
3219         /* try kill it nicely the first time again, if it started responding
3220            at some point */
3221         self->close_tried_term = FALSE;
3222     }
3223 }
3224
3225 void client_close(ObClient *self)
3226 {
3227     if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
3228
3229     /* in the case that the client provides no means to requesting that it
3230        close, we just kill it */
3231     if (!self->delete_window)
3232         /* don't use client_kill(), we should only kill based on PID in
3233            response to a lack of PING replies */
3234         XKillClient(ob_display, self->window);
3235     else if (self->not_responding)
3236         client_kill(self);
3237     else
3238         /* request the client to close with WM_DELETE_WINDOW */
3239         PROP_MSG_TO(self->window, self->window, wm_protocols,
3240                     prop_atoms.wm_delete_window, event_curtime, 0, 0, 0,
3241                     NoEventMask);
3242 }
3243
3244 void client_kill(ObClient *self)
3245 {
3246     if (!self->client_machine && self->pid) {
3247         /* running on the local host */
3248         if (!self->close_tried_term) {
3249             ob_debug("killing window 0x%x with pid %lu, with SIGTERM\n",
3250                      self->window, self->pid);
3251             kill(self->pid, SIGTERM);
3252             self->close_tried_term = TRUE;
3253
3254             /* show that we're trying to kill it */
3255             client_update_title(self);
3256         }
3257         else {
3258             ob_debug("killing window 0x%x with pid %lu, with SIGKILL\n",
3259                      self->window, self->pid);
3260             kill(self->pid, SIGKILL); /* kill -9 */
3261         }
3262     }
3263     else
3264         XKillClient(ob_display, self->window);
3265 }
3266
3267 void client_hilite(ObClient *self, gboolean hilite)
3268 {
3269     if (self->demands_attention == hilite)
3270         return; /* no change */
3271
3272     /* don't allow focused windows to hilite */
3273     self->demands_attention = hilite && !client_focused(self);
3274     if (self->frame != NULL) { /* if we're mapping, just set the state */
3275         if (self->demands_attention)
3276             frame_flash_start(self->frame);
3277         else
3278             frame_flash_stop(self->frame);
3279         client_change_state(self);
3280     }
3281 }
3282
3283 static void client_set_desktop_recursive(ObClient *self,
3284                                          guint target,
3285                                          gboolean donthide,
3286                                          gboolean dontraise)
3287 {
3288     guint old;
3289     GSList *it;
3290
3291     if (target != self->desktop && self->type != OB_CLIENT_TYPE_DESKTOP) {
3292
3293         ob_debug("Setting desktop %u\n", target+1);
3294
3295         g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
3296
3297         old = self->desktop;
3298         self->desktop = target;
3299         PROP_SET32(self->window, net_wm_desktop, cardinal, target);
3300         /* the frame can display the current desktop state */
3301         frame_adjust_state(self->frame);
3302         /* 'move' the window to the new desktop */
3303         if (!donthide)
3304             client_hide(self);
3305         client_show(self);
3306         /* raise if it was not already on the desktop */
3307         if (old != DESKTOP_ALL && !dontraise)
3308             stacking_raise(CLIENT_AS_WINDOW(self));
3309         if (STRUT_EXISTS(self->strut))
3310             screen_update_areas();
3311         else
3312             /* the new desktop's geometry may be different, so we may need to
3313                resize, for example if we are maximized */
3314             client_reconfigure(self, FALSE);
3315     }
3316
3317     /* move all transients */
3318     for (it = self->transients; it; it = g_slist_next(it))
3319         if (it->data != self)
3320             if (client_is_direct_child(self, it->data))
3321                 client_set_desktop_recursive(it->data, target,
3322                                              donthide, dontraise);
3323 }
3324
3325 void client_set_desktop(ObClient *self, guint target,
3326                         gboolean donthide, gboolean dontraise)
3327 {
3328     self = client_search_top_direct_parent(self);
3329     client_set_desktop_recursive(self, target, donthide, dontraise);
3330 }
3331
3332 gboolean client_is_direct_child(ObClient *parent, ObClient *child)
3333 {
3334     while (child != parent && (child = client_direct_parent(child)));
3335     return child == parent;
3336 }
3337
3338 ObClient *client_search_modal_child(ObClient *self)
3339 {
3340     GSList *it;
3341     ObClient *ret;
3342
3343     for (it = self->transients; it; it = g_slist_next(it)) {
3344         ObClient *c = it->data;
3345         if ((ret = client_search_modal_child(c))) return ret;
3346         if (c->modal) return c;
3347     }
3348     return NULL;
3349 }
3350
3351 gboolean client_validate(ObClient *self)
3352 {
3353     XEvent e;
3354
3355     XSync(ob_display, FALSE); /* get all events on the server */
3356
3357     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
3358         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
3359         XPutBackEvent(ob_display, &e);
3360         return FALSE;
3361     }
3362
3363     return TRUE;
3364 }
3365
3366 void client_set_wm_state(ObClient *self, glong state)
3367 {
3368     if (state == self->wmstate) return; /* no change */
3369
3370     switch (state) {
3371     case IconicState:
3372         client_iconify(self, TRUE, TRUE, FALSE);
3373         break;
3374     case NormalState:
3375         client_iconify(self, FALSE, TRUE, FALSE);
3376         break;
3377     }
3378 }
3379
3380 void client_set_state(ObClient *self, Atom action, glong data1, glong data2)
3381 {
3382     gboolean shaded = self->shaded;
3383     gboolean fullscreen = self->fullscreen;
3384     gboolean undecorated = self->undecorated;
3385     gboolean max_horz = self->max_horz;
3386     gboolean max_vert = self->max_vert;
3387     gboolean modal = self->modal;
3388     gboolean iconic = self->iconic;
3389     gboolean demands_attention = self->demands_attention;
3390     gboolean above = self->above;
3391     gboolean below = self->below;
3392     gint i;
3393
3394     if (!(action == prop_atoms.net_wm_state_add ||
3395           action == prop_atoms.net_wm_state_remove ||
3396           action == prop_atoms.net_wm_state_toggle))
3397         /* an invalid action was passed to the client message, ignore it */
3398         return;
3399
3400     for (i = 0; i < 2; ++i) {
3401         Atom state = i == 0 ? data1 : data2;
3402
3403         if (!state) continue;
3404
3405         /* if toggling, then pick whether we're adding or removing */
3406         if (action == prop_atoms.net_wm_state_toggle) {
3407             if (state == prop_atoms.net_wm_state_modal)
3408                 action = modal ? prop_atoms.net_wm_state_remove :
3409                     prop_atoms.net_wm_state_add;
3410             else if (state == prop_atoms.net_wm_state_maximized_vert)
3411                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
3412                     prop_atoms.net_wm_state_add;
3413             else if (state == prop_atoms.net_wm_state_maximized_horz)
3414                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
3415                     prop_atoms.net_wm_state_add;
3416             else if (state == prop_atoms.net_wm_state_shaded)
3417                 action = shaded ? prop_atoms.net_wm_state_remove :
3418                     prop_atoms.net_wm_state_add;
3419             else if (state == prop_atoms.net_wm_state_skip_taskbar)
3420                 action = self->skip_taskbar ?
3421                     prop_atoms.net_wm_state_remove :
3422                     prop_atoms.net_wm_state_add;
3423             else if (state == prop_atoms.net_wm_state_skip_pager)
3424                 action = self->skip_pager ?
3425                     prop_atoms.net_wm_state_remove :
3426                     prop_atoms.net_wm_state_add;
3427             else if (state == prop_atoms.net_wm_state_hidden)
3428                 action = self->iconic ?
3429                     prop_atoms.net_wm_state_remove :
3430                     prop_atoms.net_wm_state_add;
3431             else if (state == prop_atoms.net_wm_state_fullscreen)
3432                 action = fullscreen ?
3433                     prop_atoms.net_wm_state_remove :
3434                     prop_atoms.net_wm_state_add;
3435             else if (state == prop_atoms.net_wm_state_above)
3436                 action = self->above ? prop_atoms.net_wm_state_remove :
3437                     prop_atoms.net_wm_state_add;
3438             else if (state == prop_atoms.net_wm_state_below)
3439                 action = self->below ? prop_atoms.net_wm_state_remove :
3440                     prop_atoms.net_wm_state_add;
3441             else if (state == prop_atoms.net_wm_state_demands_attention)
3442                 action = self->demands_attention ?
3443                     prop_atoms.net_wm_state_remove :
3444                     prop_atoms.net_wm_state_add;
3445             else if (state == prop_atoms.ob_wm_state_undecorated)
3446                 action = undecorated ? prop_atoms.net_wm_state_remove :
3447                     prop_atoms.net_wm_state_add;
3448         }
3449
3450         if (action == prop_atoms.net_wm_state_add) {
3451             if (state == prop_atoms.net_wm_state_modal) {
3452                 modal = TRUE;
3453             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
3454                 max_vert = TRUE;
3455             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
3456                 max_horz = TRUE;
3457             } else if (state == prop_atoms.net_wm_state_shaded) {
3458                 shaded = TRUE;
3459             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
3460                 self->skip_taskbar = TRUE;
3461             } else if (state == prop_atoms.net_wm_state_skip_pager) {
3462                 self->skip_pager = TRUE;
3463             } else if (state == prop_atoms.net_wm_state_hidden) {
3464                 iconic = TRUE;
3465             } else if (state == prop_atoms.net_wm_state_fullscreen) {
3466                 fullscreen = TRUE;
3467             } else if (state == prop_atoms.net_wm_state_above) {
3468                 above = TRUE;
3469                 below = FALSE;
3470             } else if (state == prop_atoms.net_wm_state_below) {
3471                 above = FALSE;
3472                 below = TRUE;
3473             } else if (state == prop_atoms.net_wm_state_demands_attention) {
3474                 demands_attention = TRUE;
3475             } else if (state == prop_atoms.ob_wm_state_undecorated) {
3476                 undecorated = TRUE;
3477             }
3478
3479         } else { /* action == prop_atoms.net_wm_state_remove */
3480             if (state == prop_atoms.net_wm_state_modal) {
3481                 modal = FALSE;
3482             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
3483                 max_vert = FALSE;
3484             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
3485                 max_horz = FALSE;
3486             } else if (state == prop_atoms.net_wm_state_shaded) {
3487                 shaded = FALSE;
3488             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
3489                 self->skip_taskbar = FALSE;
3490             } else if (state == prop_atoms.net_wm_state_skip_pager) {
3491                 self->skip_pager = FALSE;
3492             } else if (state == prop_atoms.net_wm_state_hidden) {
3493                 iconic = FALSE;
3494             } else if (state == prop_atoms.net_wm_state_fullscreen) {
3495                 fullscreen = FALSE;
3496             } else if (state == prop_atoms.net_wm_state_above) {
3497                 above = FALSE;
3498             } else if (state == prop_atoms.net_wm_state_below) {
3499                 below = FALSE;
3500             } else if (state == prop_atoms.net_wm_state_demands_attention) {
3501                 demands_attention = FALSE;
3502             } else if (state == prop_atoms.ob_wm_state_undecorated) {
3503                 undecorated = FALSE;
3504             }
3505         }
3506     }
3507
3508     if (max_horz != self->max_horz || max_vert != self->max_vert) {
3509         if (max_horz != self->max_horz && max_vert != self->max_vert) {
3510             /* toggling both */
3511             if (max_horz == max_vert) { /* both going the same way */
3512                 client_maximize(self, max_horz, 0);
3513             } else {
3514                 client_maximize(self, max_horz, 1);
3515                 client_maximize(self, max_vert, 2);
3516             }
3517         } else {
3518             /* toggling one */
3519             if (max_horz != self->max_horz)
3520                 client_maximize(self, max_horz, 1);
3521             else
3522                 client_maximize(self, max_vert, 2);
3523         }
3524     }
3525     /* change fullscreen state before shading, as it will affect if the window
3526        can shade or not */
3527     if (fullscreen != self->fullscreen)
3528         client_fullscreen(self, fullscreen);
3529     if (shaded != self->shaded)
3530         client_shade(self, shaded);
3531     if (undecorated != self->undecorated)
3532         client_set_undecorated(self, undecorated);
3533     if (above != self->above || below != self->below) {
3534         self->above = above;
3535         self->below = below;
3536         client_calc_layer(self);
3537     }
3538
3539     if (modal != self->modal) {
3540         self->modal = modal;
3541         /* when a window changes modality, then its stacking order with its
3542            transients needs to change */
3543         stacking_raise(CLIENT_AS_WINDOW(self));
3544
3545         /* it also may get focused. if something is focused that shouldn't
3546            be focused anymore, then move the focus */
3547         if (focus_client && client_focus_target(focus_client) != focus_client)
3548             client_focus(focus_client);
3549     }
3550
3551     if (iconic != self->iconic)
3552         client_iconify(self, iconic, FALSE, FALSE);
3553
3554     if (demands_attention != self->demands_attention)
3555         client_hilite(self, demands_attention);