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