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