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