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