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