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