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