]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/client.c
set the max settings properly, was setting max_vert for both.
[mikachu/openbox.git] / openbox / client.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2    
3    client.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "client.h"
21 #include "debug.h"
22 #include "startupnotify.h"
23 #include "dock.h"
24 #include "xerror.h"
25 #include "screen.h"
26 #include "moveresize.h"
27 #include "place.h"
28 #include "prop.h"
29 #include "extensions.h"
30 #include "frame.h"
31 #include "session.h"
32 #include "event.h"
33 #include "grab.h"
34 #include "focus.h"
35 #include "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_horz = !!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 }
1023
1024 static void client_get_layer(ObClient *self)
1025 {
1026     if (!(self->above || self->below)) {
1027         if (self->group) {
1028             /* apply stuff from the group */
1029             GSList *it;
1030             gint layer = -2;
1031
1032             for (it = self->group->members; it; it = g_slist_next(it)) {
1033                 ObClient *c = it->data;
1034                 if (c != self && !client_search_transient(self, c) &&
1035                     client_normal(self) && client_normal(c))
1036                 {
1037                     layer = MAX(layer,
1038                                 (c->above ? 1 : (c->below ? -1 : 0)));
1039                 }
1040             }
1041             switch (layer) {
1042             case -1:
1043                 self->below = TRUE;
1044                 break;
1045             case -2:
1046             case 0:
1047                 break;
1048             case 1:
1049                 self->above = TRUE;
1050                 break;
1051             default:
1052                 g_assert_not_reached();
1053                 break;
1054             }
1055         }
1056     }
1057 }
1058
1059 static void client_get_state(ObClient *self)
1060 {
1061     guint32 *state;
1062     guint num;
1063   
1064     if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
1065         gulong i;
1066         for (i = 0; i < num; ++i) {
1067             if (state[i] == prop_atoms.net_wm_state_modal)
1068                 self->modal = TRUE;
1069             else if (state[i] == prop_atoms.net_wm_state_shaded)
1070                 self->shaded = TRUE;
1071             else if (state[i] == prop_atoms.net_wm_state_hidden)
1072                 self->iconic = TRUE;
1073             else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
1074                 self->skip_taskbar = TRUE;
1075             else if (state[i] == prop_atoms.net_wm_state_skip_pager)
1076                 self->skip_pager = TRUE;
1077             else if (state[i] == prop_atoms.net_wm_state_fullscreen)
1078                 self->fullscreen = TRUE;
1079             else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
1080                 self->max_vert = TRUE;
1081             else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
1082                 self->max_horz = TRUE;
1083             else if (state[i] == prop_atoms.net_wm_state_above)
1084                 self->above = TRUE;
1085             else if (state[i] == prop_atoms.net_wm_state_below)
1086                 self->below = TRUE;
1087             else if (state[i] == prop_atoms.net_wm_state_demands_attention)
1088                 self->demands_attention = TRUE;
1089             else if (state[i] == prop_atoms.ob_wm_state_undecorated)
1090                 self->undecorated = TRUE;
1091         }
1092
1093         g_free(state);
1094     }
1095 }
1096
1097 static void client_get_shaped(ObClient *self)
1098 {
1099     self->shaped = FALSE;
1100 #ifdef   SHAPE
1101     if (extensions_shape) {
1102         gint foo;
1103         guint ufoo;
1104         gint s;
1105
1106         XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
1107
1108         XShapeQueryExtents(ob_display, self->window, &s, &foo,
1109                            &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
1110                            &ufoo);
1111         self->shaped = (s != 0);
1112     }
1113 #endif
1114 }
1115
1116 void client_update_transient_for(ObClient *self)
1117 {
1118     Window t = None;
1119     ObClient *target = NULL;
1120
1121     if (XGetTransientForHint(ob_display, self->window, &t)) {
1122         self->transient = TRUE;
1123         if (t != self->window) { /* cant be transient to itself! */
1124             target = g_hash_table_lookup(window_map, &t);
1125             /* if this happens then we need to check for it*/
1126             g_assert(target != self);
1127             if (target && !WINDOW_IS_CLIENT(target)) {
1128                 /* this can happen when a dialog is a child of
1129                    a dockapp, for example */
1130                 target = NULL;
1131             }
1132
1133             /* THIS IS SO ANNOYING ! ! ! ! Let me explain.... have a seat..
1134
1135                Setting the transient_for to Root is actually illegal, however
1136                applications from time have done this to specify transient for
1137                their group.
1138
1139                Now you can do that by being a TYPE_DIALOG and not setting
1140                the transient_for hint at all on your window. But people still
1141                use Root, and Kwin is very strange in this regard.
1142
1143                KWin 3.0 will not consider windows with transient_for set to
1144                Root as transient for their group *UNLESS* they are also modal.
1145                In that case, it will make them transient for the group. This
1146                leads to all sorts of weird behavior from KDE apps which are
1147                only tested in KWin. I'd like to follow their behavior just to
1148                make this work right with KDE stuff, but that seems wrong.
1149             */
1150             if (!target && self->group) {
1151                 /* not transient to a client, see if it is transient for a
1152                    group */
1153                 if (t == RootWindow(ob_display, ob_screen)) {
1154                     /* window is a transient for its group! */
1155                     target = OB_TRAN_GROUP;
1156                 }
1157             }
1158         }
1159     } else if (self->group) {
1160         if (self->type == OB_CLIENT_TYPE_DIALOG ||
1161             self->type == OB_CLIENT_TYPE_TOOLBAR ||
1162             self->type == OB_CLIENT_TYPE_MENU ||
1163             self->type == OB_CLIENT_TYPE_UTILITY)
1164         {
1165             self->transient = TRUE;
1166             target = OB_TRAN_GROUP;
1167         }
1168     } else
1169         self->transient = FALSE;
1170
1171     /* if anything has changed... */
1172     if (target != self->transient_for) {
1173         if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
1174             GSList *it;
1175
1176             /* remove from old parents */
1177             for (it = self->group->members; it; it = g_slist_next(it)) {
1178                 ObClient *c = it->data;
1179                 if (c != self && !c->transient_for)
1180                     c->transients = g_slist_remove(c->transients, self);
1181             }
1182         } else if (self->transient_for != NULL) { /* transient of window */
1183             /* remove from old parent */
1184             self->transient_for->transients =
1185                 g_slist_remove(self->transient_for->transients, self);
1186         }
1187         self->transient_for = target;
1188         if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
1189             GSList *it;
1190
1191             /* add to new parents */
1192             for (it = self->group->members; it; it = g_slist_next(it)) {
1193                 ObClient *c = it->data;
1194                 if (c != self && !c->transient_for)
1195                     c->transients = g_slist_append(c->transients, self);
1196             }
1197
1198             /* remove all transients which are in the group, that causes
1199                circlular pointer hell of doom */
1200             for (it = self->group->members; it; it = g_slist_next(it)) {
1201                 GSList *sit, *next;
1202                 for (sit = self->transients; sit; sit = next) {
1203                     next = g_slist_next(sit);
1204                     if (sit->data == it->data)
1205                         self->transients =
1206                             g_slist_delete_link(self->transients, sit);
1207                 }
1208             }
1209         } else if (self->transient_for != NULL) { /* transient of window */
1210             /* add to new parent */
1211             self->transient_for->transients =
1212                 g_slist_append(self->transient_for->transients, self);
1213         }
1214     }
1215 }
1216
1217 static void client_get_mwm_hints(ObClient *self)
1218 {
1219     guint num;
1220     guint32 *hints;
1221
1222     self->mwmhints.flags = 0; /* default to none */
1223
1224     if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
1225                     &hints, &num)) {
1226         if (num >= OB_MWM_ELEMENTS) {
1227             self->mwmhints.flags = hints[0];
1228             self->mwmhints.functions = hints[1];
1229             self->mwmhints.decorations = hints[2];
1230         }
1231         g_free(hints);
1232     }
1233 }
1234
1235 void client_get_type(ObClient *self)
1236 {
1237     guint num, i;
1238     guint32 *val;
1239
1240     self->type = -1;
1241   
1242     if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
1243         /* use the first value that we know about in the array */
1244         for (i = 0; i < num; ++i) {
1245             if (val[i] == prop_atoms.net_wm_window_type_desktop)
1246                 self->type = OB_CLIENT_TYPE_DESKTOP;
1247             else if (val[i] == prop_atoms.net_wm_window_type_dock)
1248                 self->type = OB_CLIENT_TYPE_DOCK;
1249             else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
1250                 self->type = OB_CLIENT_TYPE_TOOLBAR;
1251             else if (val[i] == prop_atoms.net_wm_window_type_menu)
1252                 self->type = OB_CLIENT_TYPE_MENU;
1253             else if (val[i] == prop_atoms.net_wm_window_type_utility)
1254                 self->type = OB_CLIENT_TYPE_UTILITY;
1255             else if (val[i] == prop_atoms.net_wm_window_type_splash)
1256                 self->type = OB_CLIENT_TYPE_SPLASH;
1257             else if (val[i] == prop_atoms.net_wm_window_type_dialog)
1258                 self->type = OB_CLIENT_TYPE_DIALOG;
1259             else if (val[i] == prop_atoms.net_wm_window_type_normal)
1260                 self->type = OB_CLIENT_TYPE_NORMAL;
1261             else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
1262                 /* prevent this window from getting any decor or
1263                    functionality */
1264                 self->mwmhints.flags &= (OB_MWM_FLAG_FUNCTIONS |
1265                                          OB_MWM_FLAG_DECORATIONS);
1266                 self->mwmhints.decorations = 0;
1267                 self->mwmhints.functions = 0;
1268             }
1269             if (self->type != (ObClientType) -1)
1270                 break; /* grab the first legit type */
1271         }
1272         g_free(val);
1273     }
1274     
1275     if (self->type == (ObClientType) -1) {
1276         /*the window type hint was not set, which means we either classify
1277           ourself as a normal window or a dialog, depending on if we are a
1278           transient. */
1279         if (self->transient)
1280             self->type = OB_CLIENT_TYPE_DIALOG;
1281         else
1282             self->type = OB_CLIENT_TYPE_NORMAL;
1283     }
1284 }
1285
1286 void client_update_protocols(ObClient *self)
1287 {
1288     guint32 *proto;
1289     guint num_return, i;
1290
1291     self->focus_notify = FALSE;
1292     self->delete_window = FALSE;
1293
1294     if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
1295         for (i = 0; i < num_return; ++i) {
1296             if (proto[i] == prop_atoms.wm_delete_window)
1297                 /* this means we can request the window to close */
1298                 self->delete_window = TRUE;
1299             else if (proto[i] == prop_atoms.wm_take_focus)
1300                 /* if this protocol is requested, then the window will be
1301                    notified whenever we want it to receive focus */
1302                 self->focus_notify = TRUE;
1303 #ifdef SYNC
1304             else if (proto[i] == prop_atoms.net_wm_sync_request) 
1305                 /* if this protocol is requested, then resizing the
1306                    window will be synchronized between the frame and the
1307                    client */
1308                 self->sync_request = TRUE;
1309 #endif
1310         }
1311         g_free(proto);
1312     }
1313 }
1314
1315 #ifdef SYNC
1316 void client_update_sync_request_counter(ObClient *self)
1317 {
1318     guint32 i;
1319
1320     if (PROP_GET32(self->window, net_wm_sync_request_counter, cardinal, &i)) {
1321         self->sync_counter = i;
1322     } else
1323         self->sync_counter = None;
1324 }
1325 #endif
1326
1327 static void client_get_gravity(ObClient *self)
1328 {
1329     XWindowAttributes wattrib;
1330     Status ret;
1331
1332     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
1333     g_assert(ret != BadWindow);
1334     self->gravity = wattrib.win_gravity;
1335 }
1336
1337 void client_get_colormap(ObClient *self)
1338 {
1339     XWindowAttributes wa;
1340
1341     if (XGetWindowAttributes(ob_display, self->window, &wa))
1342         client_update_colormap(self, wa.colormap);
1343 }
1344
1345 void client_update_colormap(ObClient *self, Colormap colormap)
1346 {
1347     self->colormap = colormap;
1348 }
1349
1350 void client_update_normal_hints(ObClient *self)
1351 {
1352     XSizeHints size;
1353     glong ret;
1354     gint oldgravity = self->gravity;
1355
1356     /* defaults */
1357     self->min_ratio = 0.0f;
1358     self->max_ratio = 0.0f;
1359     SIZE_SET(self->size_inc, 1, 1);
1360     SIZE_SET(self->base_size, 0, 0);
1361     SIZE_SET(self->min_size, 0, 0);
1362     SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
1363
1364     /* get the hints from the window */
1365     if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
1366         /* normal windows can't request placement! har har
1367         if (!client_normal(self))
1368         */
1369         self->positioned = (size.flags & (PPosition|USPosition));
1370
1371         if (size.flags & PWinGravity) {
1372             self->gravity = size.win_gravity;
1373       
1374             /* if the client has a frame, i.e. has already been mapped and
1375                is changing its gravity */
1376             if (self->frame && self->gravity != oldgravity) {
1377                 /* move our idea of the client's position based on its new
1378                    gravity */
1379                 self->area.x = self->frame->area.x;
1380                 self->area.y = self->frame->area.y;
1381                 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
1382             }
1383         }
1384
1385         if (size.flags & PAspect) {
1386             if (size.min_aspect.y)
1387                 self->min_ratio =
1388                     (gfloat) size.min_aspect.x / size.min_aspect.y;
1389             if (size.max_aspect.y)
1390                 self->max_ratio =
1391                     (gfloat) size.max_aspect.x / size.max_aspect.y;
1392         }
1393
1394         if (size.flags & PMinSize)
1395             SIZE_SET(self->min_size, size.min_width, size.min_height);
1396     
1397         if (size.flags & PMaxSize)
1398             SIZE_SET(self->max_size, size.max_width, size.max_height);
1399     
1400         if (size.flags & PBaseSize)
1401             SIZE_SET(self->base_size, size.base_width, size.base_height);
1402     
1403         if (size.flags & PResizeInc && size.width_inc && size.height_inc)
1404             SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
1405     }
1406 }
1407
1408 void client_setup_decor_and_functions(ObClient *self)
1409 {
1410     /* start with everything (cept fullscreen) */
1411     self->decorations =
1412         (OB_FRAME_DECOR_TITLEBAR |
1413          OB_FRAME_DECOR_HANDLE |
1414          OB_FRAME_DECOR_GRIPS |
1415          OB_FRAME_DECOR_BORDER |
1416          OB_FRAME_DECOR_ICON |
1417          OB_FRAME_DECOR_ALLDESKTOPS |
1418          OB_FRAME_DECOR_ICONIFY |
1419          OB_FRAME_DECOR_MAXIMIZE |
1420          OB_FRAME_DECOR_SHADE |
1421          OB_FRAME_DECOR_CLOSE);
1422     self->functions =
1423         (OB_CLIENT_FUNC_RESIZE |
1424          OB_CLIENT_FUNC_MOVE |
1425          OB_CLIENT_FUNC_ICONIFY |
1426          OB_CLIENT_FUNC_MAXIMIZE |
1427          OB_CLIENT_FUNC_SHADE |
1428          OB_CLIENT_FUNC_CLOSE);
1429
1430     if (!(self->min_size.width < self->max_size.width ||
1431           self->min_size.height < self->max_size.height))
1432         self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1433
1434     switch (self->type) {
1435     case OB_CLIENT_TYPE_NORMAL:
1436         /* normal windows retain all of the possible decorations and
1437            functionality, and are the only windows that you can fullscreen */
1438         self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1439         break;
1440
1441     case OB_CLIENT_TYPE_DIALOG:
1442     case OB_CLIENT_TYPE_UTILITY:
1443         /* these windows cannot be maximized */
1444         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1445         break;
1446
1447     case OB_CLIENT_TYPE_MENU:
1448     case OB_CLIENT_TYPE_TOOLBAR:
1449         /* these windows get less functionality */
1450         self->functions &= ~(OB_CLIENT_FUNC_ICONIFY | OB_CLIENT_FUNC_RESIZE);
1451         break;
1452
1453     case OB_CLIENT_TYPE_DESKTOP:
1454     case OB_CLIENT_TYPE_DOCK:
1455     case OB_CLIENT_TYPE_SPLASH:
1456         /* none of these windows are manipulated by the window manager */
1457         self->decorations = 0;
1458         self->functions = 0;
1459         break;
1460     }
1461
1462     /* Mwm Hints are applied subtractively to what has already been chosen for
1463        decor and functionality */
1464     if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
1465         if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
1466             if (! ((self->mwmhints.decorations & OB_MWM_DECOR_HANDLE) ||
1467                    (self->mwmhints.decorations & OB_MWM_DECOR_TITLE)))
1468             {
1469                 /* if the mwm hints request no handle or title, then all
1470                    decorations are disabled, but keep the border if that's
1471                    specified */
1472                 if (self->mwmhints.decorations & OB_MWM_DECOR_BORDER)
1473                     self->decorations = OB_FRAME_DECOR_BORDER;
1474                 else
1475                     self->decorations = 0;
1476             }
1477         }
1478     }
1479
1480     if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
1481         if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
1482             if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
1483                 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1484             if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
1485                 self->functions &= ~OB_CLIENT_FUNC_MOVE;
1486             /* dont let mwm hints kill any buttons
1487                if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
1488                self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
1489                if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
1490                self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1491             */
1492             /* dont let mwm hints kill the close button
1493                if (! (self->mwmhints.functions & MwmFunc_Close))
1494                self->functions &= ~OB_CLIENT_FUNC_CLOSE; */
1495         }
1496     }
1497
1498     if (!(self->functions & OB_CLIENT_FUNC_SHADE))
1499         self->decorations &= ~OB_FRAME_DECOR_SHADE;
1500     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY))
1501         self->decorations &= ~OB_FRAME_DECOR_ICONIFY;
1502     if (!(self->functions & OB_CLIENT_FUNC_RESIZE))
1503         self->decorations &= ~OB_FRAME_DECOR_GRIPS;
1504
1505     /* can't maximize without moving/resizing */
1506     if (!((self->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
1507           (self->functions & OB_CLIENT_FUNC_MOVE) &&
1508           (self->functions & OB_CLIENT_FUNC_RESIZE))) {
1509         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1510         self->decorations &= ~OB_FRAME_DECOR_MAXIMIZE;
1511     }
1512
1513     /* kill the handle on fully maxed windows */
1514     if (self->max_vert && self->max_horz)
1515         self->decorations &= ~OB_FRAME_DECOR_HANDLE;
1516
1517     /* finally, the user can have requested no decorations, which overrides
1518        everything (but doesnt give it a border if it doesnt have one) */
1519     if (self->undecorated) {
1520         if (config_theme_keepborder)
1521             self->decorations &= OB_FRAME_DECOR_BORDER;
1522         else
1523             self->decorations = 0;
1524     }
1525
1526     /* if we don't have a titlebar, then we cannot shade! */
1527     if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1528         self->functions &= ~OB_CLIENT_FUNC_SHADE;
1529
1530     /* now we need to check against rules for the client's current state */
1531     if (self->fullscreen) {
1532         self->functions &= (OB_CLIENT_FUNC_CLOSE |
1533                             OB_CLIENT_FUNC_FULLSCREEN |
1534                             OB_CLIENT_FUNC_ICONIFY);
1535         self->decorations = 0;
1536     }
1537
1538     client_change_allowed_actions(self);
1539
1540     if (self->frame) {
1541         /* adjust the client's decorations, etc. */
1542         client_reconfigure(self);
1543     }
1544 }
1545
1546 static void client_change_allowed_actions(ObClient *self)
1547 {
1548     gulong actions[9];
1549     gint num = 0;
1550
1551     /* desktop windows are kept on all desktops */
1552     if (self->type != OB_CLIENT_TYPE_DESKTOP)
1553         actions[num++] = prop_atoms.net_wm_action_change_desktop;
1554
1555     if (self->functions & OB_CLIENT_FUNC_SHADE)
1556         actions[num++] = prop_atoms.net_wm_action_shade;
1557     if (self->functions & OB_CLIENT_FUNC_CLOSE)
1558         actions[num++] = prop_atoms.net_wm_action_close;
1559     if (self->functions & OB_CLIENT_FUNC_MOVE)
1560         actions[num++] = prop_atoms.net_wm_action_move;
1561     if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1562         actions[num++] = prop_atoms.net_wm_action_minimize;
1563     if (self->functions & OB_CLIENT_FUNC_RESIZE)
1564         actions[num++] = prop_atoms.net_wm_action_resize;
1565     if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1566         actions[num++] = prop_atoms.net_wm_action_fullscreen;
1567     if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1568         actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1569         actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1570     }
1571
1572     PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1573
1574     /* make sure the window isn't breaking any rules now */
1575
1576     if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1577         if (self->frame) client_shade(self, FALSE);
1578         else self->shaded = FALSE;
1579     }
1580     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY) && self->iconic) {
1581         if (self->frame) client_iconify(self, FALSE, TRUE);
1582         else self->iconic = FALSE;
1583     }
1584     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1585         if (self->frame) client_fullscreen(self, FALSE);
1586         else self->fullscreen = FALSE;
1587     }
1588     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
1589                                                          self->max_vert)) {
1590         if (self->frame) client_maximize(self, FALSE, 0);
1591         else self->max_vert = self->max_horz = FALSE;
1592     }
1593 }
1594
1595 void client_reconfigure(ObClient *self)
1596 {
1597     /* by making this pass FALSE for user, we avoid the emacs event storm where
1598        every configurenotify causes an update in its normal hints, i think this
1599        is generally what we want anyways... */
1600     client_configure(self, OB_CORNER_TOPLEFT, self->area.x, self->area.y,
1601                      self->area.width, self->area.height, FALSE, TRUE);
1602 }
1603
1604 void client_update_wmhints(ObClient *self)
1605 {
1606     XWMHints *hints;
1607     GSList *it;
1608
1609     /* assume a window takes input if it doesnt specify */
1610     self->can_focus = TRUE;
1611   
1612     if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1613         if (hints->flags & InputHint)
1614             self->can_focus = hints->input;
1615
1616         /* only do this when first managing the window *AND* when we aren't
1617            starting up! */
1618         if (ob_state() != OB_STATE_STARTING && self->frame == NULL)
1619             if (hints->flags & StateHint)
1620                 self->iconic = hints->initial_state == IconicState;
1621
1622         if (!(hints->flags & WindowGroupHint))
1623             hints->window_group = None;
1624
1625         /* did the group state change? */
1626         if (hints->window_group !=
1627             (self->group ? self->group->leader : None)) {
1628             /* remove from the old group if there was one */
1629             if (self->group != NULL) {
1630                 /* remove transients of the group */
1631                 for (it = self->group->members; it; it = g_slist_next(it))
1632                     self->transients = g_slist_remove(self->transients,
1633                                                       it->data);
1634
1635                 /* remove myself from parents in the group */
1636                 if (self->transient_for == OB_TRAN_GROUP) {
1637                     for (it = self->group->members; it;
1638                          it = g_slist_next(it))
1639                     {
1640                         ObClient *c = it->data;
1641
1642                         if (c != self && !c->transient_for)
1643                             c->transients = g_slist_remove(c->transients,
1644                                                            self);
1645                     }
1646                 }
1647
1648                 group_remove(self->group, self);
1649                 self->group = NULL;
1650             }
1651             if (hints->window_group != None) {
1652                 self->group = group_add(hints->window_group, self);
1653
1654                 /* i can only have transients from the group if i am not
1655                    transient myself */
1656                 if (!self->transient_for) {
1657                     /* add other transients of the group that are already
1658                        set up */
1659                     for (it = self->group->members; it;
1660                          it = g_slist_next(it))
1661                     {
1662                         ObClient *c = it->data;
1663                         if (c != self && c->transient_for == OB_TRAN_GROUP)
1664                             self->transients =
1665                                 g_slist_append(self->transients, c);
1666                     }
1667                 }
1668             }
1669
1670             /* because the self->transient flag wont change from this call,
1671                we don't need to update the window's type and such, only its
1672                transient_for, and the transients lists of other windows in
1673                the group may be affected */
1674             client_update_transient_for(self);
1675         }
1676
1677         /* the WM_HINTS can contain an icon */
1678         client_update_icons(self);
1679
1680         XFree(hints);
1681     }
1682 }
1683
1684 void client_update_title(ObClient *self)
1685 {
1686     gchar *data = NULL;
1687     gchar *visible = NULL;
1688
1689     g_free(self->title);
1690      
1691     /* try netwm */
1692     if (!PROP_GETS(self->window, net_wm_name, utf8, &data)) {
1693         /* try old x stuff */
1694         if (!(PROP_GETS(self->window, wm_name, locale, &data)
1695               || PROP_GETS(self->window, wm_name, utf8, &data))) {
1696             if (self->transient) {
1697                 /*
1698                   GNOME alert windows are not given titles:
1699                   http://developer.gnome.org/projects/gup/hig/draft_hig_new/windows-alert.html
1700                 */
1701                 data = g_strdup("");
1702             } else
1703                 data = g_strdup("Unnamed Window");
1704         }
1705     }
1706
1707     if (self->client_machine) {
1708         visible = g_strdup_printf("%s (%s)", data, self->client_machine);
1709         g_free(data);
1710     } else
1711         visible = data;
1712
1713     PROP_SETS(self->window, net_wm_visible_name, visible);
1714     self->title = visible;
1715
1716     if (self->frame)
1717         frame_adjust_title(self->frame);
1718
1719     /* update the icon title */
1720     data = NULL;
1721     g_free(self->icon_title);
1722
1723     /* try netwm */
1724     if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1725         /* try old x stuff */
1726         if (!(PROP_GETS(self->window, wm_icon_name, locale, &data) ||
1727               PROP_GETS(self->window, wm_icon_name, utf8, &data)))
1728             data = g_strdup(self->title);
1729
1730     PROP_SETS(self->window, net_wm_visible_icon_name, data);
1731     self->icon_title = data;
1732 }
1733
1734 void client_update_class(ObClient *self)
1735 {
1736     gchar **data;
1737     gchar *s;
1738
1739     if (self->name) g_free(self->name);
1740     if (self->class) g_free(self->class);
1741     if (self->role) g_free(self->role);
1742
1743     self->name = self->class = self->role = NULL;
1744
1745     if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1746         if (data[0]) {
1747             self->name = g_strdup(data[0]);
1748             if (data[1])
1749                 self->class = g_strdup(data[1]);
1750         }
1751         g_strfreev(data);     
1752     }
1753
1754     if (PROP_GETS(self->window, wm_window_role, locale, &s))
1755         self->role = s;
1756
1757     if (self->name == NULL) self->name = g_strdup("");
1758     if (self->class == NULL) self->class = g_strdup("");
1759     if (self->role == NULL) self->role = g_strdup("");
1760 }
1761
1762 void client_update_strut(ObClient *self)
1763 {
1764     guint num;
1765     guint32 *data;
1766     gboolean got = FALSE;
1767     StrutPartial strut;
1768
1769     if (PROP_GETA32(self->window, net_wm_strut_partial, cardinal,
1770                     &data, &num)) {
1771         if (num == 12) {
1772             got = TRUE;
1773             STRUT_PARTIAL_SET(strut,
1774                               data[0], data[2], data[1], data[3],
1775                               data[4], data[5], data[8], data[9],
1776                               data[6], data[7], data[10], data[11]);
1777         }
1778         g_free(data);
1779     }
1780
1781     if (!got &&
1782         PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1783         if (num == 4) {
1784             const Rect *a;
1785
1786             got = TRUE;
1787
1788             /* use the screen's width/height */
1789             a = screen_physical_area();
1790
1791             STRUT_PARTIAL_SET(strut,
1792                               data[0], data[2], data[1], data[3],
1793                               a->y, a->y + a->height - 1,
1794                               a->x, a->x + a->width - 1,
1795                               a->y, a->y + a->height - 1,
1796                               a->x, a->x + a->width - 1);
1797         }
1798         g_free(data);
1799     }
1800
1801     if (!got)
1802         STRUT_PARTIAL_SET(strut, 0, 0, 0, 0,
1803                           0, 0, 0, 0, 0, 0, 0, 0);
1804
1805     if (!STRUT_EQUAL(strut, self->strut)) {
1806         self->strut = strut;
1807
1808         /* updating here is pointless while we're being mapped cuz we're not in
1809            the client list yet */
1810         if (self->frame)
1811             screen_update_areas();
1812     }
1813 }
1814
1815 void client_update_icons(ObClient *self)
1816 {
1817     guint num;
1818     guint32 *data;
1819     guint w, h, i, j;
1820
1821     for (i = 0; i < self->nicons; ++i)
1822         g_free(self->icons[i].data);
1823     if (self->nicons > 0)
1824         g_free(self->icons);
1825     self->nicons = 0;
1826
1827     if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1828         /* figure out how many valid icons are in here */
1829         i = 0;
1830         while (num - i > 2) {
1831             w = data[i++];
1832             h = data[i++];
1833             i += w * h;
1834             if (i > num || w*h == 0) break;
1835             ++self->nicons;
1836         }
1837
1838         self->icons = g_new(ObClientIcon, self->nicons);
1839     
1840         /* store the icons */
1841         i = 0;
1842         for (j = 0; j < self->nicons; ++j) {
1843             guint x, y, t;
1844
1845             w = self->icons[j].width = data[i++];
1846             h = self->icons[j].height = data[i++];
1847
1848             if (w*h == 0) continue;
1849
1850             self->icons[j].data = g_new(RrPixel32, w * h);
1851             for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1852                 if (x >= w) {
1853                     x = 0;
1854                     ++y;
1855                 }
1856                 self->icons[j].data[t] =
1857                     (((data[i] >> 24) & 0xff) << RrDefaultAlphaOffset) +
1858                     (((data[i] >> 16) & 0xff) << RrDefaultRedOffset) +
1859                     (((data[i] >> 8) & 0xff) << RrDefaultGreenOffset) +
1860                     (((data[i] >> 0) & 0xff) << RrDefaultBlueOffset);
1861             }
1862             g_assert(i <= num);
1863         }
1864
1865         g_free(data);
1866     } else {
1867         XWMHints *hints;
1868
1869         if ((hints = XGetWMHints(ob_display, self->window))) {
1870             if (hints->flags & IconPixmapHint) {
1871                 self->nicons++;
1872                 self->icons = g_new(ObClientIcon, self->nicons);
1873                 xerror_set_ignore(TRUE);
1874                 if (!RrPixmapToRGBA(ob_rr_inst,
1875                                     hints->icon_pixmap,
1876                                     (hints->flags & IconMaskHint ?
1877                                      hints->icon_mask : None),
1878                                     &self->icons[self->nicons-1].width,
1879                                     &self->icons[self->nicons-1].height,
1880                                     &self->icons[self->nicons-1].data)){
1881                     g_free(&self->icons[self->nicons-1]);
1882                     self->nicons--;
1883                 }
1884                 xerror_set_ignore(FALSE);
1885             }
1886             XFree(hints);
1887         }
1888     }
1889
1890     if (self->frame)
1891         frame_adjust_icon(self->frame);
1892 }
1893
1894 void client_update_user_time(ObClient *self)
1895 {
1896     guint32 time;
1897
1898     if (PROP_GET32(self->window, net_wm_user_time, cardinal, &time)) {
1899         /* we set this every time, not just when it grows, because in practice
1900            sometimes time goes backwards! (ntpdate.. yay....) so.. if it goes
1901            backward we don't want all windows to stop focusing. we'll just
1902            assume noone is setting times older than the last one, cuz that
1903            would be pretty stupid anyways
1904         */
1905         self->user_time = time;
1906
1907         /*
1908         ob_debug("window %s user time %u\n", self->title, time);
1909         */
1910     }
1911 }
1912
1913 static void client_get_client_machine(ObClient *self)
1914 {
1915     gchar *data = NULL;
1916     gchar localhost[128];
1917
1918     g_free(self->client_machine);
1919
1920     if (PROP_GETS(self->window, wm_client_machine, locale, &data)) {
1921         gethostname(localhost, 127);
1922         localhost[127] = '\0';
1923         if (strcmp(localhost, data))
1924             self->client_machine = data;
1925     }
1926 }
1927
1928 static void client_change_wm_state(ObClient *self)
1929 {
1930     gulong state[2];
1931     glong old;
1932
1933     old = self->wmstate;
1934
1935     if (self->shaded || self->iconic || !self->frame->visible)
1936         self->wmstate = IconicState;
1937     else
1938         self->wmstate = NormalState;
1939
1940     if (old != self->wmstate) {
1941         PROP_MSG(self->window, kde_wm_change_state,
1942                  self->wmstate, 1, 0, 0);
1943
1944         state[0] = self->wmstate;
1945         state[1] = None;
1946         PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1947     }
1948 }
1949
1950 static void client_change_state(ObClient *self)
1951 {
1952     gulong netstate[11];
1953     guint num;
1954
1955     num = 0;
1956     if (self->modal)
1957         netstate[num++] = prop_atoms.net_wm_state_modal;
1958     if (self->shaded)
1959         netstate[num++] = prop_atoms.net_wm_state_shaded;
1960     if (self->iconic)
1961         netstate[num++] = prop_atoms.net_wm_state_hidden;
1962     if (self->skip_taskbar)
1963         netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1964     if (self->skip_pager)
1965         netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1966     if (self->fullscreen)
1967         netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1968     if (self->max_vert)
1969         netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1970     if (self->max_horz)
1971         netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1972     if (self->above)
1973         netstate[num++] = prop_atoms.net_wm_state_above;
1974     if (self->below)
1975         netstate[num++] = prop_atoms.net_wm_state_below;
1976     if (self->demands_attention)
1977         netstate[num++] = prop_atoms.net_wm_state_demands_attention;
1978     if (self->undecorated)
1979         netstate[num++] = prop_atoms.ob_wm_state_undecorated;
1980     PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1981
1982     if (self->frame)
1983         frame_adjust_state(self->frame);
1984 }
1985
1986 ObClient *client_search_focus_tree(ObClient *self)
1987 {
1988     GSList *it;
1989     ObClient *ret;
1990
1991     for (it = self->transients; it; it = g_slist_next(it)) {
1992         if (client_focused(it->data)) return it->data;
1993         if ((ret = client_search_focus_tree(it->data))) return ret;
1994     }
1995     return NULL;
1996 }
1997
1998 ObClient *client_search_focus_tree_full(ObClient *self)
1999 {
2000     if (self->transient_for) {
2001         if (self->transient_for != OB_TRAN_GROUP) {
2002             return client_search_focus_tree_full(self->transient_for);
2003         } else {
2004             GSList *it;
2005             gboolean recursed = FALSE;
2006         
2007             for (it = self->group->members; it; it = g_slist_next(it))
2008                 if (!((ObClient*)it->data)->transient_for) {
2009                     ObClient *c;
2010                     if ((c = client_search_focus_tree_full(it->data)))
2011                         return c;
2012                     recursed = TRUE;
2013                 }
2014             if (recursed)
2015                 return NULL;
2016         }
2017     }
2018
2019     /* this function checks the whole tree, the client_search_focus_tree~
2020        does not, so we need to check this window */
2021     if (client_focused(self))
2022         return self;
2023     return client_search_focus_tree(self);
2024 }
2025
2026 static ObStackingLayer calc_layer(ObClient *self)
2027 {
2028     ObStackingLayer l;
2029
2030     if (self->fullscreen &&
2031         (client_focused(self) || client_search_focus_tree(self)))
2032         l = OB_STACKING_LAYER_FULLSCREEN;
2033     else if (self->type == OB_CLIENT_TYPE_DESKTOP)
2034         l = OB_STACKING_LAYER_DESKTOP;
2035     else if (self->type == OB_CLIENT_TYPE_DOCK) {
2036         if (self->below) l = OB_STACKING_LAYER_NORMAL;
2037         else l = OB_STACKING_LAYER_ABOVE;
2038     }
2039     else if (self->above) l = OB_STACKING_LAYER_ABOVE;
2040     else if (self->below) l = OB_STACKING_LAYER_BELOW;
2041     else l = OB_STACKING_LAYER_NORMAL;
2042
2043     return l;
2044 }
2045
2046 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
2047                                         ObStackingLayer min, gboolean raised)
2048 {
2049     ObStackingLayer old, own;
2050     GSList *it;
2051
2052     old = self->layer;
2053     own = calc_layer(self);
2054     self->layer = MAX(own, min);
2055
2056     for (it = self->transients; it; it = g_slist_next(it))
2057         client_calc_layer_recursive(it->data, orig,
2058                                     self->layer,
2059                                     raised ? raised : self->layer != old);
2060
2061     if (!raised && self->layer != old)
2062         if (orig->frame) { /* only restack if the original window is managed */
2063             stacking_remove(CLIENT_AS_WINDOW(self));
2064             stacking_add(CLIENT_AS_WINDOW(self));
2065         }
2066 }
2067
2068 void client_calc_layer(ObClient *self)
2069 {
2070     ObClient *orig;
2071     GSList *it;
2072
2073     orig = self;
2074
2075     /* transients take on the layer of their parents */
2076     it = client_search_all_top_parents(self);
2077
2078     for (; it; it = g_slist_next(it))
2079         client_calc_layer_recursive(it->data, orig, 0, FALSE);
2080 }
2081
2082 gboolean client_should_show(ObClient *self)
2083 {
2084     if (self->iconic)
2085         return FALSE;
2086     if (client_normal(self) && screen_showing_desktop)
2087         return FALSE;
2088     /*
2089     if (self->transient_for) {
2090         if (self->transient_for != OB_TRAN_GROUP)
2091             return client_should_show(self->transient_for);
2092         else {
2093             GSList *it;
2094
2095             for (it = self->group->members; it; it = g_slist_next(it)) {
2096                 ObClient *c = it->data;
2097                 if (c != self && !c->transient_for) {
2098                     if (client_should_show(c))
2099                         return TRUE;
2100                 }
2101             }
2102         }
2103     }
2104     */
2105     if (self->desktop == screen_desktop || self->desktop == DESKTOP_ALL)
2106         return TRUE;
2107     
2108     return FALSE;
2109 }
2110
2111 void client_show(ObClient *self)
2112 {
2113
2114     if (client_should_show(self)) {
2115         frame_show(self->frame);
2116     }
2117
2118     /* According to the ICCCM (sec 4.1.3.1) when a window is not visible, it
2119        needs to be in IconicState. This includes when it is on another
2120        desktop!
2121     */
2122     client_change_wm_state(self);
2123 }
2124
2125 void client_hide(ObClient *self)
2126 {
2127     if (!client_should_show(self)) {
2128         frame_hide(self->frame);
2129     }
2130
2131     /* According to the ICCCM (sec 4.1.3.1) when a window is not visible, it
2132        needs to be in IconicState. This includes when it is on another
2133        desktop!
2134     */
2135     client_change_wm_state(self);
2136 }
2137
2138 void client_showhide(ObClient *self)
2139 {
2140
2141     if (client_should_show(self)) {
2142         frame_show(self->frame);
2143     }
2144     else {
2145         frame_hide(self->frame);
2146     }
2147
2148     /* According to the ICCCM (sec 4.1.3.1) when a window is not visible, it
2149        needs to be in IconicState. This includes when it is on another
2150        desktop!
2151     */
2152     client_change_wm_state(self);
2153 }
2154
2155 gboolean client_normal(ObClient *self) {
2156     return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
2157               self->type == OB_CLIENT_TYPE_DOCK ||
2158               self->type == OB_CLIENT_TYPE_SPLASH);
2159 }
2160
2161 static void client_apply_startup_state(ObClient *self, gint x, gint y)
2162 {
2163     gboolean pos = FALSE; /* has the window's position been configured? */
2164     gint ox, oy;
2165
2166     /* save the position, and set self->area for these to use */
2167     ox = self->area.x;
2168     oy = self->area.y;
2169     self->area.x = x;
2170     self->area.y = y;
2171
2172     /* set the desktop hint, to make sure that it always exists */
2173     PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
2174
2175     /* these are in a carefully crafted order.. */
2176
2177     if (self->iconic) {
2178         self->iconic = FALSE;
2179         client_iconify(self, TRUE, FALSE);
2180     }
2181     if (self->fullscreen) {
2182         self->fullscreen = FALSE;
2183         client_fullscreen(self, TRUE);
2184         pos = TRUE;
2185     }
2186     if (self->undecorated) {
2187         self->undecorated = FALSE;
2188         client_set_undecorated(self, TRUE);
2189     }
2190     if (self->shaded) {
2191         self->shaded = FALSE;
2192         client_shade(self, TRUE);
2193     }
2194     if (self->demands_attention) {
2195         self->demands_attention = FALSE;
2196         client_hilite(self, TRUE);
2197     }
2198   
2199     if (self->max_vert && self->max_horz) {
2200         self->max_vert = self->max_horz = FALSE;
2201         client_maximize(self, TRUE, 0);
2202         pos = TRUE;
2203     } else if (self->max_vert) {
2204         self->max_vert = FALSE;
2205         client_maximize(self, TRUE, 2);
2206         pos = TRUE;
2207     } else if (self->max_horz) {
2208         self->max_horz = FALSE;
2209         client_maximize(self, TRUE, 1);
2210         pos = TRUE;
2211     }
2212
2213     /* if the client didn't get positioned yet, then do so now
2214        call client_move even if the window is not being moved anywhere, because
2215        when we reparent it and decorate it, it is getting moved and we need to
2216        be telling it so with a ConfigureNotify event.
2217     */
2218     if (!pos) {
2219         /* use the saved position */
2220         self->area.x = ox;
2221         self->area.y = oy;
2222         client_move(self, x, y);
2223     }
2224
2225     /* nothing to do for the other states:
2226        skip_taskbar
2227        skip_pager
2228        modal
2229        above
2230        below
2231     */
2232 }
2233
2234 void client_try_configure(ObClient *self, ObCorner anchor,
2235                           gint *x, gint *y, gint *w, gint *h,
2236                           gint *logicalw, gint *logicalh,
2237                           gboolean user)
2238 {
2239     Rect desired_area = {*x, *y, *w, *h};
2240
2241     /* make the frame recalculate its dimentions n shit without changing
2242        anything visible for real, this way the constraints below can work with
2243        the updated frame dimensions. */
2244     frame_adjust_area(self->frame, TRUE, TRUE, TRUE);
2245
2246     /* work within the prefered sizes given by the window */
2247     if (!(*w == self->area.width && *h == self->area.height)) {
2248         gint basew, baseh, minw, minh;
2249
2250         /* base size is substituted with min size if not specified */
2251         if (self->base_size.width || self->base_size.height) {
2252             basew = self->base_size.width;
2253             baseh = self->base_size.height;
2254         } else {
2255             basew = self->min_size.width;
2256             baseh = self->min_size.height;
2257         }
2258         /* min size is substituted with base size if not specified */
2259         if (self->min_size.width || self->min_size.height) {
2260             minw = self->min_size.width;
2261             minh = self->min_size.height;
2262         } else {
2263             minw = self->base_size.width;
2264             minh = self->base_size.height;
2265         }
2266
2267         /* if this is a user-requested resize, then check against min/max
2268            sizes */
2269
2270         /* smaller than min size or bigger than max size? */
2271         if (*w > self->max_size.width) *w = self->max_size.width;
2272         if (*w < minw) *w = minw;
2273         if (*h > self->max_size.height) *h = self->max_size.height;
2274         if (*h < minh) *h = minh;
2275
2276         *w -= basew;
2277         *h -= baseh;
2278
2279         /* keep to the increments */
2280         *w /= self->size_inc.width;
2281         *h /= self->size_inc.height;
2282
2283         /* you cannot resize to nothing */
2284         if (basew + *w < 1) *w = 1 - basew;
2285         if (baseh + *h < 1) *h = 1 - baseh;
2286   
2287         /* save the logical size */
2288         *logicalw = self->size_inc.width > 1 ? *w : *w + basew;
2289         *logicalh = self->size_inc.height > 1 ? *h : *h + baseh;
2290
2291         *w *= self->size_inc.width;
2292         *h *= self->size_inc.height;
2293
2294         *w += basew;
2295         *h += baseh;
2296
2297         /* adjust the height to match the width for the aspect ratios.
2298            for this, min size is not substituted for base size ever. */
2299         *w -= self->base_size.width;
2300         *h -= self->base_size.height;
2301
2302         if (!self->fullscreen) {
2303             if (self->min_ratio)
2304                 if (*h * self->min_ratio > *w) {
2305                     *h = (gint)(*w / self->min_ratio);
2306
2307                     /* you cannot resize to nothing */
2308                     if (*h < 1) {
2309                         *h = 1;
2310                         *w = (gint)(*h * self->min_ratio);
2311                     }
2312                 }
2313             if (self->max_ratio)
2314                 if (*h * self->max_ratio < *w) {
2315                     *h = (gint)(*w / self->max_ratio);
2316
2317                     /* you cannot resize to nothing */
2318                     if (*h < 1) {
2319                         *h = 1;
2320                         *w = (gint)(*h * self->min_ratio);
2321                     }
2322                 }
2323         }
2324
2325         *w += self->base_size.width;
2326         *h += self->base_size.height;
2327     }
2328
2329     /* gets the frame's position */
2330     frame_client_gravity(self->frame, x, y);
2331
2332     /* these positions are frame positions, not client positions */
2333
2334     /* set the size and position if fullscreen */
2335     if (self->fullscreen) {
2336         Rect *a;
2337         guint i;
2338
2339         i = screen_find_monitor(&desired_area);
2340         a = screen_physical_area_monitor(i);
2341
2342         *x = a->x;
2343         *y = a->y;
2344         *w = a->width;
2345         *h = a->height;
2346
2347         user = FALSE; /* ignore if the client can't be moved/resized when it
2348                          is entering fullscreen */
2349     } else if (self->max_horz || self->max_vert) {
2350         Rect *a;
2351         guint i;
2352
2353         i = screen_find_monitor(&desired_area);
2354         a = screen_area_monitor(self->desktop, i);
2355
2356         /* set the size and position if maximized */
2357         if (self->max_horz) {
2358             *x = a->x;
2359             *w = a->width - self->frame->size.left - self->frame->size.right;
2360         }
2361         if (self->max_vert) {
2362             *y = a->y;
2363             *h = a->height - self->frame->size.top - self->frame->size.bottom;
2364         }
2365
2366         /* maximizing is not allowed if the user can't move+resize the window
2367          */
2368     }
2369
2370     /* gets the client's position */
2371     frame_frame_gravity(self->frame, x, y);
2372
2373     /* these override the above states! if you cant move you can't move! */
2374     if (user) {
2375         if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
2376             *x = self->area.x;
2377             *y = self->area.y;
2378         }
2379         if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
2380             *w = self->area.width;
2381             *h = self->area.height;
2382         }
2383     }
2384
2385     g_assert(*w > 0);
2386     g_assert(*h > 0);
2387
2388     switch (anchor) {
2389     case OB_CORNER_TOPLEFT:
2390         break;
2391     case OB_CORNER_TOPRIGHT:
2392         *x -= *w - self->area.width;
2393         break;
2394     case OB_CORNER_BOTTOMLEFT:
2395         *y -= *h - self->area.height;
2396         break;
2397     case OB_CORNER_BOTTOMRIGHT:
2398         *x -= *w - self->area.width;
2399         *y -= *h - self->area.height;
2400         break;
2401     }
2402 }
2403
2404
2405 void client_configure_full(ObClient *self, ObCorner anchor,
2406                            gint x, gint y, gint w, gint h,
2407                            gboolean user, gboolean final,
2408                            gboolean force_reply)
2409 {
2410     gint oldw, oldh, oldrx, oldry;
2411     gboolean send_resize_client;
2412     gboolean moved = FALSE, resized = FALSE, rootmoved = FALSE;
2413     guint fdecor = self->frame->decorations;
2414     gboolean fhorz = self->frame->max_horz;
2415     gint logicalw, logicalh;
2416
2417     /* find the new x, y, width, and height (and logical size) */
2418     client_try_configure(self, anchor, &x, &y, &w, &h,
2419                          &logicalw, &logicalh, user);
2420
2421     /* set the logical size if things changed */
2422     if (!(w == self->area.width && h == self->area.height))
2423         SIZE_SET(self->logical_size, logicalw, logicalh);
2424
2425     /* figure out if we moved or resized or what */
2426     moved = x != self->area.x || y != self->area.y;
2427     resized = w != self->area.width || h != self->area.height;
2428
2429     oldw = self->area.width;
2430     oldh = self->area.height;
2431     RECT_SET(self->area, x, y, w, h);
2432
2433     /* for app-requested resizes, always resize if 'resized' is true.
2434        for user-requested ones, only resize if final is true, or when
2435        resizing in redraw mode */
2436     send_resize_client = ((!user && resized) ||
2437                           (user && (final ||
2438                                     (resized && config_resize_redraw))));
2439
2440     /* if the client is enlarging, then resize the client before the frame */
2441     if (send_resize_client && user && (w > oldw || h > oldh)) {
2442         XResizeWindow(ob_display, self->window, MAX(w, oldw), MAX(h, oldh));
2443         frame_adjust_client_area(self->frame);
2444     }
2445
2446     /* find the frame's dimensions and move/resize it */
2447     if (self->decorations != fdecor || self->max_horz != fhorz)
2448         moved = resized = TRUE;
2449     if (moved || resized)
2450         frame_adjust_area(self->frame, moved, resized, FALSE);
2451
2452     /* find the client's position relative to the root window */
2453     oldrx = self->root_pos.x;
2454     oldry = self->root_pos.y;
2455     rootmoved = (oldrx != (signed)(self->frame->area.x +
2456                                    self->frame->size.left -
2457                                    self->border_width) ||
2458                  oldry != (signed)(self->frame->area.y +
2459                                    self->frame->size.top -
2460                                    self->border_width));
2461
2462     if (force_reply || ((!user || (user && final)) && rootmoved))
2463     {
2464         XEvent event;
2465
2466         POINT_SET(self->root_pos,
2467                   self->frame->area.x + self->frame->size.left -
2468                   self->border_width,
2469                   self->frame->area.y + self->frame->size.top -
2470                   self->border_width);
2471
2472         event.type = ConfigureNotify;
2473         event.xconfigure.display = ob_display;
2474         event.xconfigure.event = self->window;
2475         event.xconfigure.window = self->window;
2476
2477         /* root window real coords */
2478         event.xconfigure.x = self->root_pos.x;
2479         event.xconfigure.y = self->root_pos.y;
2480         event.xconfigure.width = w;
2481         event.xconfigure.height = h;
2482         event.xconfigure.border_width = 0;
2483         event.xconfigure.above = self->frame->plate;
2484         event.xconfigure.override_redirect = FALSE;
2485         XSendEvent(event.xconfigure.display, event.xconfigure.window,
2486                    FALSE, StructureNotifyMask, &event);
2487     }
2488
2489     /* if the client is shrinking, then resize the frame before the client */
2490     if (send_resize_client && (!user || (w <= oldw || h <= oldh))) {
2491         frame_adjust_client_area(self->frame);
2492         XResizeWindow(ob_display, self->window, w, h);
2493     }
2494
2495     XFlush(ob_display);
2496 }
2497
2498 void client_fullscreen(ObClient *self, gboolean fs)
2499 {
2500     gint x, y, w, h;
2501
2502     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
2503         self->fullscreen == fs) return;                   /* already done */
2504
2505     self->fullscreen = fs;
2506     client_change_state(self); /* change the state hints on the client */
2507     client_calc_layer(self);   /* and adjust out layer/stacking */
2508
2509     if (fs) {
2510         self->pre_fullscreen_area = self->area;
2511         /* if the window is maximized, its area isn't all that meaningful.
2512            save it's premax area instead. */
2513         if (self->max_horz) {
2514             self->pre_fullscreen_area.x = self->pre_max_area.x;
2515             self->pre_fullscreen_area.width = self->pre_max_area.width;
2516         }
2517         if (self->max_vert) {
2518             self->pre_fullscreen_area.y = self->pre_max_area.y;
2519             self->pre_fullscreen_area.height = self->pre_max_area.height;
2520         }
2521
2522         /* these are not actually used cuz client_configure will set them
2523            as appropriate when the window is fullscreened */
2524         x = y = w = h = 0;
2525     } else {
2526         Rect *a;
2527
2528         if (self->pre_fullscreen_area.width > 0 &&
2529             self->pre_fullscreen_area.height > 0)
2530         {
2531             x = self->pre_fullscreen_area.x;
2532             y = self->pre_fullscreen_area.y;
2533             w = self->pre_fullscreen_area.width;
2534             h = self->pre_fullscreen_area.height;
2535             RECT_SET(self->pre_fullscreen_area, 0, 0, 0, 0);
2536         } else {
2537             /* pick some fallbacks... */
2538             a = screen_area_monitor(self->desktop, 0);
2539             x = a->x + a->width / 4;
2540             y = a->y + a->height / 4;
2541             w = a->width / 2;
2542             h = a->height / 2;
2543         }
2544     }
2545
2546     client_setup_decor_and_functions(self);
2547
2548     client_move_resize(self, x, y, w, h);
2549
2550     /* try focus us when we go into fullscreen mode */
2551     client_focus(self);
2552 }
2553
2554 static void client_iconify_recursive(ObClient *self,
2555                                      gboolean iconic, gboolean curdesk)
2556 {
2557     GSList *it;
2558     gboolean changed = FALSE;
2559
2560
2561     if (self->iconic != iconic) {
2562         ob_debug("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"),
2563                  self->window);
2564
2565         if (iconic) {
2566             if (self->functions & OB_CLIENT_FUNC_ICONIFY) {
2567                 self->iconic = iconic;
2568
2569                 /* update the focus lists.. iconic windows go to the bottom of
2570                    the list, put the new iconic window at the 'top of the
2571                    bottom'. */
2572                 focus_order_to_top(self);
2573
2574                 changed = TRUE;
2575             }
2576         } else {
2577             self->iconic = iconic;
2578
2579             if (curdesk)
2580                 client_set_desktop(self, screen_desktop, FALSE);
2581
2582             /* this puts it after the current focused window */
2583             focus_order_remove(self);
2584             focus_order_add_new(self);
2585
2586             changed = TRUE;
2587         }
2588     }
2589
2590     if (changed) {
2591         client_change_state(self);
2592         client_showhide(self);
2593         if (STRUT_EXISTS(self->strut))
2594             screen_update_areas();
2595     }
2596
2597     /* iconify all direct transients */
2598     for (it = self->transients; it; it = g_slist_next(it))
2599         if (it->data != self)
2600             if (client_is_direct_child(self, it->data))
2601                 client_iconify_recursive(it->data, iconic, curdesk);
2602 }
2603
2604 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk)
2605 {
2606     /* move up the transient chain as far as possible first */
2607     self = client_search_top_parent(self);
2608     client_iconify_recursive(self, iconic, curdesk);
2609 }
2610
2611 void client_maximize(ObClient *self, gboolean max, gint dir)
2612 {
2613     gint x, y, w, h;
2614      
2615     g_assert(dir == 0 || dir == 1 || dir == 2);
2616     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE)) return; /* can't */
2617
2618     /* check if already done */
2619     if (max) {
2620         if (dir == 0 && self->max_horz && self->max_vert) return;
2621         if (dir == 1 && self->max_horz) return;
2622         if (dir == 2 && self->max_vert) return;
2623     } else {
2624         if (dir == 0 && !self->max_horz && !self->max_vert) return;
2625         if (dir == 1 && !self->max_horz) return;
2626         if (dir == 2 && !self->max_vert) return;
2627     }
2628
2629     /* we just tell it to configure in the same place and client_configure
2630        worries about filling the screen with the window */
2631     x = self->area.x;
2632     y = self->area.y;
2633     w = self->area.width;
2634     h = self->area.height;
2635
2636     if (max) {
2637         if ((dir == 0 || dir == 1) && !self->max_horz) { /* horz */
2638             RECT_SET(self->pre_max_area,
2639                      self->area.x, self->pre_max_area.y,
2640                      self->area.width, self->pre_max_area.height);
2641         }
2642         if ((dir == 0 || dir == 2) && !self->max_vert) { /* vert */
2643             RECT_SET(self->pre_max_area,
2644                      self->pre_max_area.x, self->area.y,
2645                      self->pre_max_area.width, self->area.height);
2646         }
2647     } else {
2648         Rect *a;
2649
2650         a = screen_area_monitor(self->desktop, 0);
2651         if ((dir == 0 || dir == 1) && self->max_horz) { /* horz */
2652             if (self->pre_max_area.width > 0) {
2653                 x = self->pre_max_area.x;
2654                 w = self->pre_max_area.width;
2655
2656                 RECT_SET(self->pre_max_area, 0, self->pre_max_area.y,
2657                          0, self->pre_max_area.height);
2658             } else {
2659                 /* pick some fallbacks... */
2660                 x = a->x + a->width / 4;
2661                 w = a->width / 2;
2662             }
2663         }
2664         if ((dir == 0 || dir == 2) && self->max_vert) { /* vert */
2665             if (self->pre_max_area.height > 0) {
2666                 y = self->pre_max_area.y;
2667                 h = self->pre_max_area.height;
2668
2669                 RECT_SET(self->pre_max_area, self->pre_max_area.x, 0,
2670                          self->pre_max_area.width, 0);
2671             } else {
2672                 /* pick some fallbacks... */
2673                 y = a->y + a->height / 4;
2674                 h = a->height / 2;
2675             }
2676         }
2677     }
2678
2679     if (dir == 0 || dir == 1) /* horz */
2680         self->max_horz = max;
2681     if (dir == 0 || dir == 2) /* vert */
2682         self->max_vert = max;
2683
2684     client_change_state(self); /* change the state hints on the client */
2685
2686     client_setup_decor_and_functions(self);
2687
2688     client_move_resize(self, x, y, w, h);
2689 }
2690
2691 void client_shade(ObClient *self, gboolean shade)
2692 {
2693     if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
2694          shade) ||                         /* can't shade */
2695         self->shaded == shade) return;     /* already done */
2696
2697     self->shaded = shade;
2698     client_change_state(self);
2699     client_change_wm_state(self); /* the window is being hidden/shown */
2700     /* resize the frame to just the titlebar */
2701     frame_adjust_area(self->frame, FALSE, FALSE, FALSE);
2702 }
2703
2704 void client_close(ObClient *self)
2705 {
2706     XEvent ce;
2707
2708     if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
2709
2710     /* in the case that the client provides no means to requesting that it
2711        close, we just kill it */
2712     if (!self->delete_window)
2713         client_kill(self);
2714     
2715     /*
2716       XXX: itd be cool to do timeouts and shit here for killing the client's
2717       process off
2718       like... if the window is around after 5 seconds, then the close button
2719       turns a nice red, and if this function is called again, the client is
2720       explicitly killed.
2721     */
2722
2723     ce.xclient.type = ClientMessage;
2724     ce.xclient.message_type =  prop_atoms.wm_protocols;
2725     ce.xclient.display = ob_display;
2726     ce.xclient.window = self->window;
2727     ce.xclient.format = 32;
2728     ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
2729     ce.xclient.data.l[1] = event_curtime;
2730     ce.xclient.data.l[2] = 0l;
2731     ce.xclient.data.l[3] = 0l;
2732     ce.xclient.data.l[4] = 0l;
2733     XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2734 }
2735
2736 void client_kill(ObClient *self)
2737 {
2738     XKillClient(ob_display, self->window);
2739 }
2740
2741 void client_hilite(ObClient *self, gboolean hilite)
2742 {
2743     if (self->demands_attention == hilite)
2744         return; /* no change */
2745
2746     /* don't allow focused windows to hilite */
2747     self->demands_attention = hilite && !client_focused(self);
2748     if (self->demands_attention)
2749         frame_flash_start(self->frame);
2750     else
2751         frame_flash_stop(self->frame);
2752     client_change_state(self);
2753 }
2754
2755 void client_set_desktop_recursive(ObClient *self,
2756                                   guint target, gboolean donthide)
2757 {
2758     guint old;
2759     GSList *it;
2760
2761     if (target != self->desktop) {
2762
2763         ob_debug("Setting desktop %u\n", target+1);
2764
2765         g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2766
2767         /* remove from the old desktop(s) */
2768         focus_order_remove(self);
2769
2770         old = self->desktop;
2771         self->desktop = target;
2772         PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2773         /* the frame can display the current desktop state */
2774         frame_adjust_state(self->frame);
2775         /* 'move' the window to the new desktop */
2776         if (!donthide)
2777             client_showhide(self);
2778         /* raise if it was not already on the desktop */
2779         if (old != DESKTOP_ALL)
2780             client_raise(self);
2781         if (STRUT_EXISTS(self->strut))
2782             screen_update_areas();
2783
2784         /* add to the new desktop(s) */
2785         if (config_focus_new)
2786             focus_order_to_top(self);
2787         else
2788             focus_order_to_bottom(self);
2789     }
2790
2791     /* move all transients */
2792     for (it = self->transients; it; it = g_slist_next(it))
2793         if (it->data != self)
2794             if (client_is_direct_child(self, it->data))
2795                 client_set_desktop_recursive(it->data, target, donthide);
2796 }
2797
2798 void client_set_desktop(ObClient *self, guint target, gboolean donthide)
2799 {
2800     self = client_search_top_parent(self);
2801     client_set_desktop_recursive(self, target, donthide);
2802 }
2803
2804 gboolean client_is_direct_child(ObClient *parent, ObClient *child)
2805 {
2806     while (child != parent &&
2807            child->transient_for && child->transient_for != OB_TRAN_GROUP)
2808         child = child->transient_for;
2809     return child == parent;
2810 }
2811
2812 ObClient *client_search_modal_child(ObClient *self)
2813 {
2814     GSList *it;
2815     ObClient *ret;
2816   
2817     for (it = self->transients; it; it = g_slist_next(it)) {
2818         ObClient *c = it->data;
2819         if ((ret = client_search_modal_child(c))) return ret;
2820         if (c->modal) return c;
2821     }
2822     return NULL;
2823 }
2824
2825 gboolean client_validate(ObClient *self)
2826 {
2827     XEvent e; 
2828
2829     XSync(ob_display, FALSE); /* get all events on the server */
2830
2831     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2832         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2833         XPutBackEvent(ob_display, &e);
2834         return FALSE;
2835     }
2836
2837     return TRUE;
2838 }
2839
2840 void client_set_wm_state(ObClient *self, glong state)
2841 {
2842     if (state == self->wmstate) return; /* no change */
2843   
2844     switch (state) {
2845     case IconicState:
2846         client_iconify(self, TRUE, TRUE);
2847         break;
2848     case NormalState:
2849         client_iconify(self, FALSE, TRUE);
2850         break;
2851     }
2852 }
2853
2854 void client_set_state(ObClient *self, Atom action, glong data1, glong data2)
2855 {
2856     gboolean shaded = self->shaded;
2857     gboolean fullscreen = self->fullscreen;
2858     gboolean undecorated = self->undecorated;
2859     gboolean max_horz = self->max_horz;
2860     gboolean max_vert = self->max_vert;
2861     gboolean modal = self->modal;
2862     gboolean iconic = self->iconic;
2863     gboolean demands_attention = self->demands_attention;
2864     gint i;
2865
2866     if (!(action == prop_atoms.net_wm_state_add ||
2867           action == prop_atoms.net_wm_state_remove ||
2868           action == prop_atoms.net_wm_state_toggle))
2869         /* an invalid action was passed to the client message, ignore it */
2870         return; 
2871
2872     for (i = 0; i < 2; ++i) {
2873         Atom state = i == 0 ? data1 : data2;
2874     
2875         if (!state) continue;
2876
2877         /* if toggling, then pick whether we're adding or removing */
2878         if (action == prop_atoms.net_wm_state_toggle) {
2879             if (state == prop_atoms.net_wm_state_modal)
2880                 action = modal ? prop_atoms.net_wm_state_remove :
2881                     prop_atoms.net_wm_state_add;
2882             else if (state == prop_atoms.net_wm_state_maximized_vert)
2883                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2884                     prop_atoms.net_wm_state_add;
2885             else if (state == prop_atoms.net_wm_state_maximized_horz)
2886                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2887                     prop_atoms.net_wm_state_add;
2888             else if (state == prop_atoms.net_wm_state_shaded)
2889                 action = shaded ? prop_atoms.net_wm_state_remove :
2890                     prop_atoms.net_wm_state_add;
2891             else if (state == prop_atoms.net_wm_state_skip_taskbar)
2892                 action = self->skip_taskbar ?
2893                     prop_atoms.net_wm_state_remove :
2894                     prop_atoms.net_wm_state_add;
2895             else if (state == prop_atoms.net_wm_state_skip_pager)
2896                 action = self->skip_pager ?
2897                     prop_atoms.net_wm_state_remove :
2898                     prop_atoms.net_wm_state_add;
2899             else if (state == prop_atoms.net_wm_state_hidden)
2900                 action = self->iconic ?
2901                     prop_atoms.net_wm_state_remove :
2902                     prop_atoms.net_wm_state_add;
2903             else if (state == prop_atoms.net_wm_state_fullscreen)
2904                 action = fullscreen ?
2905                     prop_atoms.net_wm_state_remove :
2906                     prop_atoms.net_wm_state_add;
2907             else if (state == prop_atoms.net_wm_state_above)
2908                 action = self->above ? prop_atoms.net_wm_state_remove :
2909                     prop_atoms.net_wm_state_add;
2910             else if (state == prop_atoms.net_wm_state_below)
2911                 action = self->below ? prop_atoms.net_wm_state_remove :
2912                     prop_atoms.net_wm_state_add;
2913             else if (state == prop_atoms.net_wm_state_demands_attention)
2914                 action = self->demands_attention ?
2915                     prop_atoms.net_wm_state_remove :
2916                     prop_atoms.net_wm_state_add;
2917             else if (state == prop_atoms.ob_wm_state_undecorated)
2918                 action = undecorated ? prop_atoms.net_wm_state_remove :
2919                     prop_atoms.net_wm_state_add;
2920         }
2921     
2922         if (action == prop_atoms.net_wm_state_add) {
2923             if (state == prop_atoms.net_wm_state_modal) {
2924                 modal = TRUE;
2925             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2926                 max_vert = TRUE;
2927             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2928                 max_horz = TRUE;
2929             } else if (state == prop_atoms.net_wm_state_shaded) {
2930                 shaded = TRUE;
2931             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2932                 self->skip_taskbar = TRUE;
2933             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2934                 self->skip_pager = TRUE;
2935             } else if (state == prop_atoms.net_wm_state_hidden) {
2936                 iconic = TRUE;
2937             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2938                 fullscreen = TRUE;
2939             } else if (state == prop_atoms.net_wm_state_above) {
2940                 self->above = TRUE;
2941                 self->below = FALSE;
2942             } else if (state == prop_atoms.net_wm_state_below) {
2943                 self->above = FALSE;
2944                 self->below = TRUE;
2945             } else if (state == prop_atoms.net_wm_state_demands_attention) {
2946                 demands_attention = TRUE;
2947             } else if (state == prop_atoms.ob_wm_state_undecorated) {
2948                 undecorated = TRUE;
2949             }
2950
2951         } else { /* action == prop_atoms.net_wm_state_remove */
2952             if (state == prop_atoms.net_wm_state_modal) {
2953                 modal = FALSE;
2954             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2955                 max_vert = FALSE;
2956             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2957                 max_horz = FALSE;
2958             } else if (state == prop_atoms.net_wm_state_shaded) {
2959                 shaded = FALSE;
2960             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2961                 self->skip_taskbar = FALSE;
2962             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2963                 self->skip_pager = FALSE;
2964             } else if (state == prop_atoms.net_wm_state_hidden) {
2965                 iconic = FALSE;
2966             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2967                 fullscreen = FALSE;
2968             } else if (state == prop_atoms.net_wm_state_above) {
2969                 self->above = FALSE;
2970             } else if (state == prop_atoms.net_wm_state_below) {
2971                 self->below = FALSE;
2972             } else if (state == prop_atoms.net_wm_state_demands_attention) {
2973                 demands_attention = FALSE;
2974             } else if (state == prop_atoms.ob_wm_state_undecorated) {
2975                 undecorated = FALSE;
2976             }
2977         }
2978     }
2979     if (max_horz != self->max_horz || max_vert != self->max_vert) {
2980         if (max_horz != self->max_horz && max_vert != self->max_vert) {
2981             /* toggling both */
2982             if (max_horz == max_vert) { /* both going the same way */
2983                 client_maximize(self, max_horz, 0);
2984             } else {
2985                 client_maximize(self, max_horz, 1);
2986                 client_maximize(self, max_vert, 2);
2987             }
2988         } else {
2989             /* toggling one */
2990             if (max_horz != self->max_horz)
2991                 client_maximize(self, max_horz, 1);
2992             else
2993                 client_maximize(self, max_vert, 2);
2994         }
2995     }
2996     /* change fullscreen state before shading, as it will affect if the window
2997        can shade or not */
2998     if (fullscreen != self->fullscreen)
2999         client_fullscreen(self, fullscreen);
3000     if (shaded != self->shaded)
3001         client_shade(self, shaded);
3002     if (undecorated != self->undecorated)
3003         client_set_undecorated(self, undecorated);
3004     if (modal != self->modal) {
3005         self->modal = modal;
3006         /* when a window changes modality, then its stacking order with its
3007            transients needs to change */
3008         client_raise(self);
3009     }
3010     if (iconic != self->iconic)
3011         client_iconify(self, iconic, FALSE);
3012
3013     if (demands_attention != self->demands_attention)
3014         client_hilite(self, demands_attention);
3015
3016     client_change_state(self); /* change the hint to reflect these changes */
3017 }
3018
3019 ObClient *client_focus_target(ObClient *self)
3020 {
3021     ObClient *child = NULL;
3022
3023     child = client_search_modal_child(self);
3024     if (child) return child;
3025     return self;
3026 }
3027
3028 gboolean client_can_focus(ObClient *self)
3029 {
3030     XEvent ev;
3031
3032     /* choose the correct target */
3033     self = client_focus_target(self);
3034
3035     if (!self->frame->visible)
3036         return FALSE;
3037
3038     if (!(self->can_focus || self->focus_notify))
3039         return FALSE;
3040
3041     /* do a check to see if the window has already been unmapped or destroyed
3042        do this intelligently while watching out for unmaps we've generated
3043        (ignore_unmaps > 0) */
3044     if (XCheckTypedWindowEvent(ob_display, self->window,
3045                                DestroyNotify, &ev)) {
3046         XPutBackEvent(ob_display, &ev);
3047         return FALSE;
3048     }
3049     while (XCheckTypedWindowEvent(ob_display, self->window,
3050                                   UnmapNotify, &ev)) {
3051         if (self->ignore_unmaps) {
3052             self->ignore_unmaps--;
3053         } else {
3054             XPutBackEvent(ob_display, &ev);
3055             return FALSE;
3056         }
3057     }
3058
3059     return TRUE;
3060 }
3061
3062 gboolean client_focus(ObClient *self)
3063 {
3064     /* choose the correct target */
3065     self = client_focus_target(self);
3066
3067     if (!client_can_focus(self)) {
3068         if (!self->frame->visible) {
3069             /* update the focus lists */
3070             focus_order_to_top(self);
3071         }
3072         return FALSE;
3073     }
3074
3075     ob_debug_type(OB_DEBUG_FOCUS,
3076                   "Focusing client \"%s\" at time %u\n",
3077                   self->title, event_curtime);
3078
3079     if (self->can_focus) {
3080         /* This can cause a BadMatch error with CurrentTime, or if an app
3081            passed in a bad time for _NET_WM_ACTIVE_WINDOW. */
3082         xerror_set_ignore(TRUE);
3083         XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
3084                        event_curtime);
3085         xerror_set_ignore(FALSE);
3086     }
3087
3088     if (self->focus_notify) {
3089         XEvent ce;
3090         ce.xclient.type = ClientMessage;
3091         ce.xclient.message_type = prop_atoms.wm_protocols;
3092         ce.xclient.display = ob_display;
3093         ce.xclient.window = self->window;
3094         ce.xclient.format = 32;
3095         ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
3096         ce.xclient.data.l[1] = event_curtime;
3097         ce.xclient.data.l[2] = 0l;
3098         ce.xclient.data.l[3] = 0l;
3099         ce.xclient.data.l[4] = 0l;
3100         XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
3101     }
3102
3103 #ifdef DEBUG_FOCUS
3104     ob_debug("%sively focusing %lx at %d\n",
3105              (self->can_focus ? "act" : "pass"),
3106              self->window, (gint) event_curtime);
3107 #endif
3108
3109     /* Cause the FocusIn to come back to us. Important for desktop switches,
3110        since otherwise we'll have no FocusIn on the queue and send it off to
3111        the focus_backup. */
3112     XSync(ob_display, FALSE);
3113     return TRUE;
3114 }
3115
3116 void client_activate(ObClient *self, gboolean here, gboolean user)
3117 {
3118     guint32 last_time = focus_client ? focus_client->user_time : CurrentTime;
3119
3120     /* XXX do some stuff here if user is false to determine if we really want
3121        to activate it or not (a parent or group member is currently
3122        active)?
3123     */
3124     ob_debug_type(OB_DEBUG_FOCUS,
3125                   "Want to activate window 0x%x with time %u (last time %u), "
3126                   "source=%s\n",
3127                   self->window, event_curtime, last_time,
3128                   (user ? "user" : "application"));
3129
3130     if (!user && event_curtime && last_time &&
3131         !event_time_after(event_curtime, last_time))
3132     {
3133         client_hilite(self, TRUE);
3134     } else {
3135         if (client_normal(self) && screen_showing_desktop)
3136             screen_show_desktop(FALSE);
3137         if (self->iconic)
3138             client_iconify(self, FALSE, here);
3139         if (self->desktop != DESKTOP_ALL &&
3140             self->desktop != screen_desktop) {
3141             if (here)
3142                 client_set_desktop(self, screen_desktop, FALSE);
3143             else
3144                 screen_set_desktop(self->desktop);
3145         } else if (!self->frame->visible)
3146             /* if its not visible for other reasons, then don't mess
3147                with it */
3148             return;
3149         if (self->shaded)
3150             client_shade(self, FALSE);
3151
3152         client_focus(self);
3153
3154         /* we do this an action here. this is rather important. this is because
3155            we want the results from the focus change to take place BEFORE we go
3156            about raising the window. when a fullscreen window loses focus, we
3157            need this or else the raise wont be able to raise above the
3158            to-lose-focus fullscreen window. */
3159         client_raise(self);
3160     }
3161 }
3162
3163 void client_raise(ObClient *self)
3164 {
3165     action_run_string("Raise", self, CurrentTime);
3166 }
3167
3168 void client_lower(ObClient *self)
3169 {
3170     action_run_string("Lower", self, CurrentTime);
3171 }
3172
3173 gboolean client_focused(ObClient *self)
3174 {
3175     return self == focus_client;
3176 }
3177
3178 static ObClientIcon* client_icon_recursive(ObClient *self, gint w, gint h)
3179 {
3180     guint i;
3181     /* si is the smallest image >= req */
3182     /* li is the largest image < req */
3183     gulong size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
3184
3185     if (!self->nicons) {
3186         ObClientIcon *parent = NULL;
3187
3188         if (self->transient_for) {
3189             if (self->transient_for != OB_TRAN_GROUP)
3190                 parent = client_icon_recursive(self->transient_for, w, h);
3191             else {
3192                 GSList *it;
3193                 for (it = self->group->members; it; it = g_slist_next(it)) {
3194                     ObClient *c = it->data;
3195                     if (c != self && !c->transient_for) {
3196                         if ((parent = client_icon_recursive(c, w, h)))
3197                             break;
3198                     }
3199                 }
3200             }
3201         }
3202         
3203         return parent;
3204     }
3205
3206     for (i = 0; i < self->nicons; ++i) {
3207         size = self->icons[i].width * self->icons[i].height;
3208         if (size < smallest && size >= (unsigned)(w * h)) {
3209             smallest = size;
3210             si = i;
3211         }
3212         if (size > largest && size <= (unsigned)(w * h)) {
3213             largest = size;
3214             li = i;
3215         }
3216     }
3217     if (largest == 0) /* didnt find one smaller than the requested size */
3218         return &self->icons[si];
3219     return &self->icons[li];
3220 }
3221
3222 const ObClientIcon* client_icon(ObClient *self, gint w, gint h)
3223 {
3224     ObClientIcon *ret;
3225     static ObClientIcon deficon;
3226
3227     if (!(ret = client_icon_recursive(self, w, h))) {
3228         deficon.width = deficon.height = 48;
3229         deficon.data = ob_rr_theme->def_win_icon;
3230         ret = &deficon;
3231     }
3232     return ret;
3233 }
3234
3235 void client_set_layer(ObClient *self, gint layer)
3236 {
3237     if (layer < 0) {
3238         self->below = TRUE;
3239         self->above = FALSE;
3240     } else if (layer == 0) {
3241         self->below = self->above = FALSE;
3242     } else {
3243         self->below = FALSE;
3244         self->above = TRUE;
3245     }
3246     client_calc_layer(self);
3247     client_change_state(self); /* reflect this in the state hints */
3248 }
3249
3250 void client_set_undecorated(ObClient *self, gboolean undecorated)
3251 {
3252     if (self->undecorated != undecorated) {
3253         self->undecorated = undecorated;
3254         client_setup_decor_and_functions(self);
3255         /* Make sure the client knows it might have moved. Maybe there is a
3256          * better way of doing this so only one client_configure is sent, but
3257          * since 125 of these are sent per second when moving the window (with
3258          * user = FALSE) i doubt it matters much.
3259          */
3260         client_configure(self, OB_CORNER_TOPLEFT, self->area.x, self->area.y,
3261                          self->area.width, self->area.height, TRUE, TRUE);
3262         client_change_state(self); /* reflect this in the state hints */
3263     }
3264 }
3265
3266 guint client_monitor(ObClient *self)
3267 {
3268     return screen_find_monitor(&self->frame->area);
3269 }
3270
3271 ObClient *client_search_top_parent(ObClient *self)
3272 {
3273     while (self->transient_for && self->transient_for != OB_TRAN_GROUP &&
3274            client_normal(self))
3275         self = self->transient_for;
3276     return self;
3277 }
3278
3279 GSList *client_search_all_top_parents(ObClient *self)
3280 {
3281     GSList *ret = NULL;
3282
3283     /* move up the direct transient chain as far as possible */
3284     while (self->transient_for && self->transient_for != OB_TRAN_GROUP)
3285         self = self->transient_for;
3286
3287     if (!self->transient_for)
3288         ret = g_slist_prepend(ret, self);
3289     else {
3290             GSList *it;
3291
3292             g_assert(self->group);
3293
3294             for (it = self->group->members; it; it = g_slist_next(it)) {
3295                 ObClient *c = it->data;
3296
3297                 if (!c->transient_for && client_normal(c))
3298                     ret = g_slist_prepend(ret, c);
3299             }
3300
3301             if (ret == NULL) /* no group parents */
3302                 ret = g_slist_prepend(ret, self);
3303     }
3304
3305     return ret;
3306 }
3307
3308 ObClient *client_search_focus_parent(ObClient *self)
3309 {
3310     if (self->transient_for) {
3311         if (self->transient_for != OB_TRAN_GROUP) {
3312             if (client_focused(self->transient_for))
3313                 return self->transient_for;
3314         } else {
3315             GSList *it;
3316
3317             for (it = self->group->members; it; it = g_slist_next(it)) {
3318                 ObClient *c = it->data;
3319
3320                 /* checking transient_for prevents infinate loops! */
3321                 if (c != self && !c->transient_for)
3322                     if (client_focused(c))
3323                         return c;
3324             }
3325         }
3326     }
3327
3328     return NULL;
3329 }
3330
3331 ObClient *client_search_parent(ObClient *self, ObClient *search)
3332 {
3333     if (self->transient_for) {
3334         if (self->transient_for != OB_TRAN_GROUP) {
3335             if (self->transient_for == search)
3336                 return search;
3337         } else {
3338             GSList *it;
3339
3340             for (it = self->group->members; it; it = g_slist_next(it)) {
3341                 ObClient *c = it->data;
3342
3343                 /* checking transient_for prevents infinate loops! */
3344                 if (c != self && !c->transient_for)
3345                     if (c == search)
3346                         return search;
3347             }
3348         }
3349     }
3350
3351     return NULL;
3352 }
3353
3354 ObClient *client_search_transient(ObClient *self, ObClient *search)
3355 {
3356     GSList *sit;
3357
3358     for (sit = self->transients; sit; sit = g_slist_next(sit)) {
3359         if (sit->data == search)
3360             return search;
3361         if (client_search_transient(sit->data, search))
3362             return search;
3363     }
3364     return NULL;
3365 }
3366
3367 void client_update_sm_client_id(ObClient *self)
3368 {
3369     g_free(self->sm_client_id);
3370     self->sm_client_id = NULL;
3371
3372     if (!PROP_GETS(self->window, sm_client_id, locale, &self->sm_client_id) &&
3373         self->group)
3374         PROP_GETS(self->group->leader, sm_client_id, locale,
3375                   &self->sm_client_id);
3376 }
3377
3378 #define WANT_EDGE(cur, c) \
3379             if(cur == c)                                                      \
3380                 continue;                                                     \
3381             if(!client_normal(cur))                                   \
3382                 continue;                                                     \
3383             if(screen_desktop != cur->desktop && cur->desktop != DESKTOP_ALL) \
3384                 continue;                                                     \
3385             if(cur->iconic)                                                   \
3386                 continue;                                                     \
3387             if(cur->layer < c->layer && !config_resist_layers_below)          \
3388                 continue;
3389
3390 #define HIT_EDGE(my_edge_start, my_edge_end, his_edge_start, his_edge_end) \
3391             if ((his_edge_start >= my_edge_start && \
3392                  his_edge_start <= my_edge_end) ||  \
3393                 (my_edge_start >= his_edge_start && \
3394                  my_edge_start <= his_edge_end))    \
3395                 dest = his_offset;
3396
3397 /* finds the nearest edge in the given direction from the current client
3398  * note to self: the edge is the -frame- edge (the actual one), not the
3399  * client edge.
3400  */
3401 gint client_directional_edge_search(ObClient *c, ObDirection dir, gboolean hang)
3402 {
3403     gint dest, monitor_dest;
3404     gint my_edge_start, my_edge_end, my_offset;
3405     GList *it;
3406     Rect *a, *monitor;
3407     
3408     if(!client_list)
3409         return -1;
3410
3411     a = screen_area(c->desktop);
3412     monitor = screen_area_monitor(c->desktop, client_monitor(c));
3413
3414     switch(dir) {
3415     case OB_DIRECTION_NORTH:
3416         my_edge_start = c->frame->area.x;
3417         my_edge_end = c->frame->area.x + c->frame->area.width;
3418         my_offset = c->frame->area.y + (hang ? c->frame->area.height : 0);
3419         
3420         /* default: top of screen */
3421         dest = a->y + (hang ? c->frame->area.height : 0);
3422         monitor_dest = monitor->y + (hang ? c->frame->area.height : 0);
3423         /* if the monitor edge comes before the screen edge, */
3424         /* use that as the destination instead. (For xinerama) */
3425         if (monitor_dest != dest && my_offset > monitor_dest)
3426             dest = monitor_dest; 
3427
3428         for(it = client_list; it && my_offset != dest; it = g_list_next(it)) {
3429             gint his_edge_start, his_edge_end, his_offset;
3430             ObClient *cur = it->data;
3431
3432             WANT_EDGE(cur, c)
3433
3434             his_edge_start = cur->frame->area.x;
3435             his_edge_end = cur->frame->area.x + cur->frame->area.width;
3436             his_offset = cur->frame->area.y + 
3437                          (hang ? 0 : cur->frame->area.height);
3438
3439             if(his_offset + 1 > my_offset)
3440                 continue;
3441
3442             if(his_offset < dest)
3443                 continue;
3444
3445             HIT_EDGE(my_edge_start, my_edge_end, his_edge_start, his_edge_end)
3446         }
3447         break;
3448     case OB_DIRECTION_SOUTH:
3449         my_edge_start = c->frame->area.x;
3450         my_edge_end = c->frame->area.x + c->frame->area.width;
3451         my_offset = c->frame->area.y + (hang ? 0 : c->frame->area.height);
3452
3453         /* default: bottom of screen */
3454         dest = a->y + a->height - (hang ? c->frame->area.height : 0);
3455         monitor_dest = monitor->y + monitor->height -
3456                        (hang ? c->frame->area.height : 0);
3457         /* if the monitor edge comes before the screen edge, */
3458         /* use that as the destination instead. (For xinerama) */
3459         if (monitor_dest != dest && my_offset < monitor_dest)
3460             dest = monitor_dest; 
3461
3462         for(it = client_list; it && my_offset != dest; it = g_list_next(it)) {
3463             gint his_edge_start, his_edge_end, his_offset;
3464             ObClient *cur = it->data;
3465
3466             WANT_EDGE(cur, c)
3467
3468             his_edge_start = cur->frame->area.x;
3469             his_edge_end = cur->frame->area.x + cur->frame->area.width;
3470             his_offset = cur->frame->area.y +
3471                          (hang ? cur->frame->area.height : 0);
3472
3473
3474             if(his_offset - 1 < my_offset)
3475                 continue;
3476             
3477             if(his_offset > dest)
3478                 continue;
3479
3480             HIT_EDGE(my_edge_start, my_edge_end, his_edge_start, his_edge_end)
3481         }
3482         break;
3483     case OB_DIRECTION_WEST:
3484         my_edge_start = c->frame->area.y;
3485         my_edge_end = c->frame->area.y + c->frame->area.height;
3486         my_offset = c->frame->area.x + (hang ? c->frame->area.width : 0);
3487
3488         /* default: leftmost egde of screen */
3489         dest = a->x + (hang ? c->frame->area.width : 0);
3490         monitor_dest = monitor->x + (hang ? c->frame->area.width : 0);
3491         /* if the monitor edge comes before the screen edge, */
3492         /* use that as the destination instead. (For xinerama) */
3493         if (monitor_dest != dest && my_offset > monitor_dest)
3494             dest = monitor_dest;            
3495
3496         for(it = client_list; it && my_offset != dest; it = g_list_next(it)) {
3497             gint his_edge_start, his_edge_end, his_offset;
3498             ObClient *cur = it->data;
3499
3500             WANT_EDGE(cur, c)
3501
3502             his_edge_start = cur->frame->area.y;
3503             his_edge_end = cur->frame->area.y + cur->frame->area.height;
3504             his_offset = cur->frame->area.x +
3505                          (hang ? 0 : cur->frame->area.width);
3506
3507             if(his_offset + 1 > my_offset)
3508                 continue;
3509
3510             if(his_offset < dest)
3511                 continue;
3512
3513             HIT_EDGE(my_edge_start, my_edge_end, his_edge_start, his_edge_end)
3514         }
3515        break;
3516     case OB_DIRECTION_EAST:
3517         my_edge_start = c->frame->area.y;
3518         my_edge_end = c->frame->area.y + c->frame->area.height;
3519         my_offset = c->frame->area.x + (hang ? 0 : c->frame->area.width);
3520         
3521         /* default: rightmost edge of screen */
3522         dest = a->x + a->width - (hang ? c->frame->area.width : 0);
3523         monitor_dest = monitor->x + monitor->width -
3524                        (hang ? c->frame->area.width : 0);
3525         /* if the monitor edge comes before the screen edge, */
3526         /* use that as the destination instead. (For xinerama) */
3527         if (monitor_dest != dest && my_offset < monitor_dest)
3528             dest = monitor_dest;            
3529
3530         for(it = client_list; it && my_offset != dest; it = g_list_next(it)) {
3531             gint his_edge_start, his_edge_end, his_offset;
3532             ObClient *cur = it->data;
3533
3534             WANT_EDGE(cur, c)
3535
3536             his_edge_start = cur->frame->area.y;
3537             his_edge_end = cur->frame->area.y + cur->frame->area.height;
3538             his_offset = cur->frame->area.x +
3539                          (hang ? cur->frame->area.width : 0);
3540
3541             if(his_offset - 1 < my_offset)
3542                 continue;
3543             
3544             if(his_offset > dest)
3545                 continue;
3546
3547             HIT_EDGE(my_edge_start, my_edge_end, his_edge_start, his_edge_end)
3548         }
3549         break;
3550     case OB_DIRECTION_NORTHEAST:
3551     case OB_DIRECTION_SOUTHEAST:
3552     case OB_DIRECTION_NORTHWEST:
3553     case OB_DIRECTION_SOUTHWEST:
3554         /* not implemented */
3555     default:
3556         g_assert_not_reached();
3557         dest = 0; /* suppress warning */
3558     }
3559     return dest;
3560 }
3561
3562 ObClient* client_under_pointer()
3563 {
3564     gint x, y;
3565     GList *it;
3566     ObClient *ret = NULL;
3567
3568     if (screen_pointer_pos(&x, &y)) {
3569         for (it = stacking_list; it; it = g_list_next(it)) {
3570             if (WINDOW_IS_CLIENT(it->data)) {
3571                 ObClient *c = WINDOW_AS_CLIENT(it->data);
3572                 if (c->frame->visible &&
3573                     RECT_CONTAINS(c->frame->area, x, y)) {
3574                     ret = c;
3575                     break;
3576                 }
3577             }
3578         }
3579     }
3580     return ret;
3581 }
3582
3583 gboolean client_has_group_siblings(ObClient *self)
3584 {
3585     return self->group && self->group->members->next;
3586 }