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