]> icculus.org git repositories - dana/openbox.git/blob - openbox/client.c
2 in 1 again..
[dana/openbox.git] / openbox / client.c
1 #include "client.h"
2 #include "dock.h"
3 #include "startup.h"
4 #include "screen.h"
5 #include "moveresize.h"
6 #include "prop.h"
7 #include "extensions.h"
8 #include "frame.h"
9 #include "event.h"
10 #include "grab.h"
11 #include "focus.h"
12 #include "stacking.h"
13 #include "dispatch.h"
14 #include "openbox.h"
15 #include "group.h"
16 #include "config.h"
17 #include "render/render.h"
18
19 #include <glib.h>
20 #include <X11/Xutil.h>
21
22 /*! The event mask to grab on client windows */
23 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
24                           StructureNotifyMask)
25
26 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
27                                 ButtonMotionMask)
28
29 GList      *client_list      = NULL;
30
31 static void client_get_all(Client *self);
32 static void client_toggle_border(Client *self, gboolean show);
33 static void client_get_area(Client *self);
34 static void client_get_desktop(Client *self);
35 static void client_get_state(Client *self);
36 static void client_get_shaped(Client *self);
37 static void client_get_mwm_hints(Client *self);
38 static void client_get_gravity(Client *self);
39 static void client_showhide(Client *self);
40 static void client_change_allowed_actions(Client *self);
41 static void client_change_state(Client *self);
42 static void client_apply_startup_state(Client *self);
43
44 void client_startup()
45 {
46     client_set_list();
47 }
48
49 void client_shutdown()
50 {
51 }
52
53 void client_set_list()
54 {
55     Window *windows, *win_it;
56     GList *it;
57     guint size = g_list_length(client_list);
58
59     /* create an array of the window ids */
60     if (size > 0) {
61         windows = g_new(Window, size);
62         win_it = windows;
63         for (it = client_list; it != NULL; it = it->next, ++win_it)
64             *win_it = ((Client*)it->data)->window;
65     } else
66         windows = NULL;
67
68     PROP_SETA32(ob_root, net_client_list, window, (guint32*)windows, size);
69
70     if (windows)
71         g_free(windows);
72
73     stacking_set_list();
74 }
75
76 /*
77 void client_foreach_transient(Client *self, ClientForeachFunc func, void *data)
78 {
79     GSList *it;
80
81     for (it = self->transients; it; it = it->next) {
82         if (!func(it->data, data)) return;
83         client_foreach_transient(it->data, func, data);
84     }
85 }
86
87 void client_foreach_ancestor(Client *self, ClientForeachFunc func, void *data)
88 {
89     if (self->transient_for) {
90         if (self->transient_for != TRAN_GROUP) {
91             if (!func(self->transient_for, data)) return;
92             client_foreach_ancestor(self->transient_for, func, data);
93         } else {
94             GSList *it;
95
96             for (it = self->group->members; it; it = it->next)
97                 if (it->data != self &&
98                     !((Client*)it->data)->transient_for) {
99                     if (!func(it->data, data)) return;
100                     client_foreach_ancestor(it->data, func, data);
101                 }
102         }
103     }
104 }
105 */
106
107 void client_manage_all()
108 {
109     unsigned int i, j, nchild;
110     Window w, *children;
111     XWMHints *wmhints;
112     XWindowAttributes attrib;
113
114     XQueryTree(ob_display, ob_root, &w, &w, &children, &nchild);
115
116     /* remove all icon windows from the list */
117     for (i = 0; i < nchild; i++) {
118         if (children[i] == None) continue;
119         wmhints = XGetWMHints(ob_display, children[i]);
120         if (wmhints) {
121             if ((wmhints->flags & IconWindowHint) &&
122                 (wmhints->icon_window != children[i]))
123                 for (j = 0; j < nchild; j++)
124                     if (children[j] == wmhints->icon_window) {
125                         children[j] = None;
126                         break;
127                     }
128             XFree(wmhints);
129         }
130     }
131
132     for (i = 0; i < nchild; ++i) {
133         if (children[i] == None)
134             continue;
135         if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
136             if (attrib.override_redirect) continue;
137
138             if (attrib.map_state != IsUnmapped)
139                 client_manage(children[i]);
140         }
141     }
142     XFree(children);
143
144     /* stack them as they were on startup!
145        why with stacking_lower? Why, because then windows who aren't in the
146        stacking list are on the top where you can see them instead of buried
147        at the bottom! */
148     for (i = startup_stack_size; i > 0; --i) {
149         ObWindow *obw;
150
151         w = startup_stack_order[i-1];
152         obw = g_hash_table_lookup(window_map, &w);
153         if (obw) {
154             g_assert(WINDOW_IS_CLIENT(obw));
155             stacking_lower(CLIENT_AS_WINDOW(obw));
156         }
157     }
158     g_free(startup_stack_order);
159     startup_stack_order = NULL;
160     startup_stack_size = 0;
161
162     if (config_focus_new) {
163         ObWindow *active;
164
165         active = g_hash_table_lookup(window_map, &startup_active);
166         if (active) {
167             g_assert(WINDOW_IS_CLIENT(active));
168             if (!client_focus(WINDOW_AS_CLIENT(active)))
169                 focus_fallback(Fallback_NoFocus);
170         } else
171             focus_fallback(Fallback_NoFocus);
172     }
173 }
174
175 void client_manage(Window window)
176 {
177     Client *self;
178     XEvent e;
179     XWindowAttributes attrib;
180     XSetWindowAttributes attrib_set;
181     XWMHints *wmhint;
182     gboolean activate = FALSE;
183
184     grab_server(TRUE);
185
186     /* check if it has already been unmapped by the time we started mapping
187        the grab does a sync so we don't have to here */
188     if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
189         XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
190         XPutBackEvent(ob_display, &e);
191
192         grab_server(FALSE);
193         return; /* don't manage it */
194     }
195
196     /* make sure it isn't an override-redirect window */
197     if (!XGetWindowAttributes(ob_display, window, &attrib) ||
198         attrib.override_redirect) {
199         grab_server(FALSE);
200         return; /* don't manage it */
201     }
202   
203     /* is the window a docking app */
204     if ((wmhint = XGetWMHints(ob_display, window))) {
205         if ((wmhint->flags & StateHint) &&
206             wmhint->initial_state == WithdrawnState) {
207             dock_add(window, wmhint);
208             grab_server(FALSE);
209             XFree(wmhint);
210             return;
211         }
212         XFree(wmhint);
213     }
214
215     g_message("Managing window: %lx", window);
216
217     /* choose the events we want to receive on the CLIENT window */
218     attrib_set.event_mask = CLIENT_EVENTMASK;
219     attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
220     XChangeWindowAttributes(ob_display, window,
221                             CWEventMask|CWDontPropagate, &attrib_set);
222
223
224     /* create the Client struct, and populate it from the hints on the
225        window */
226     self = g_new(Client, 1);
227     self->obwin.type = Window_Client;
228     self->window = window;
229     client_get_all(self);
230
231     /* remove the client's border (and adjust re gravity) */
232     client_toggle_border(self, FALSE);
233      
234     /* specify that if we exit, the window should not be destroyed and should
235        be reparented back to root automatically */
236     XChangeSaveSet(ob_display, window, SetModeInsert);
237
238     /* create the decoration frame for the client window */
239     self->frame = frame_new();
240
241     frame_grab_client(self->frame, self);
242
243     client_apply_startup_state(self);
244
245     grab_server(FALSE);
246
247     /* add to client list/map */
248     client_list = g_list_append(client_list, self);
249     g_hash_table_insert(window_map, &self->window, self);
250
251     /* update the focus lists */
252     focus_order_add_new(self);
253
254     /* focus the new window? */
255     if (ob_state != State_Starting && config_focus_new) {
256         gboolean group_foc = FALSE;
257         
258         if (self->group) {
259             GSList *it;
260
261             for (it = self->group->members; it; it = it->next)
262                 if (client_focused(it->data)) {
263                     group_foc = TRUE;
264                     break;
265                 }
266         }
267         /* note the check against Type_Normal/Dialog, not client_normal(self),
268            which would also include other types. in this case we want more
269            strict rules for focus */
270         if (((self->type == Type_Normal ||
271               (self->type == Type_Dialog &&
272                (group_foc ||
273                 (!self->transient_for && (!self->group ||
274                                           !self->group->members->next)))))) ||
275             client_search_focus_tree_full(self) ||
276             !focus_client ||
277             !client_normal(focus_client)) {
278             /* activate the window */
279             stacking_add(CLIENT_AS_WINDOW(self));
280             activate = TRUE;
281         } else {
282             /* try to not get in the way */
283             stacking_add_nonintrusive(CLIENT_AS_WINDOW(self));
284         }
285     } else {
286         stacking_add(CLIENT_AS_WINDOW(self));
287     }
288
289     screen_update_struts();
290
291     /* make sure the window is visible */
292     client_move_onscreen(self);
293
294     dispatch_client(Event_Client_New, self, 0, 0);
295
296     client_showhide(self);
297
298     if (activate) client_activate(self);
299
300     /* update the list hints */
301     client_set_list();
302
303     dispatch_client(Event_Client_Mapped, self, 0, 0);
304
305     g_message("Managed window 0x%lx (%s)", window, self->class);
306 }
307
308 void client_unmanage_all()
309 {
310     while (client_list != NULL)
311         client_unmanage(client_list->data);
312 }
313
314 void client_unmanage(Client *self)
315 {
316     int j;
317     GSList *it;
318
319     g_message("Unmanaging window: %lx (%s)", self->window, self->class);
320
321     dispatch_client(Event_Client_Destroy, self, 0, 0);
322     g_assert(self != NULL);
323
324     /* remove the window from our save set */
325     XChangeSaveSet(ob_display, self->window, SetModeDelete);
326
327     /* we dont want events no more */
328     XSelectInput(ob_display, self->window, NoEventMask);
329
330     frame_hide(self->frame);
331
332     client_list = g_list_remove(client_list, self);
333     stacking_remove(self);
334     g_hash_table_remove(window_map, &self->window);
335
336     /* update the focus lists */
337     focus_order_remove(self);
338
339     /* once the client is out of the list, update the struts to remove it's
340        influence */
341     screen_update_struts();
342
343     /* tell our parent(s) that we're gone */
344     if (self->transient_for == TRAN_GROUP) { /* transient of group */
345         GSList *it;
346
347         for (it = self->group->members; it; it = it->next)
348             if (it->data != self)
349                 ((Client*)it->data)->transients =
350                     g_slist_remove(((Client*)it->data)->transients, self);
351     } else if (self->transient_for) {        /* transient of window */
352         self->transient_for->transients =
353             g_slist_remove(self->transient_for->transients, self);
354     }
355
356     /* tell our transients that we're gone */
357     for (it = self->transients; it != NULL; it = it->next) {
358         if (((Client*)it->data)->transient_for != TRAN_GROUP) {
359             ((Client*)it->data)->transient_for = NULL;
360             client_calc_layer(it->data);
361         }
362     }
363
364     if (moveresize_client == self)
365         moveresize_end(TRUE);
366
367     if (focus_client == self) {
368         XEvent e;
369
370         /* focus the last focused window on the desktop, and ignore enter
371            events from the unmap so it doesnt mess with the focus */
372         while (XCheckTypedEvent(ob_display, EnterNotify, &e));
373         client_unfocus(self);
374     }
375
376     /* remove from its group */
377     if (self->group) {
378         group_remove(self->group, self);
379         self->group = NULL;
380     }
381
382     /* dispatch the unmapped event */
383     dispatch_client(Event_Client_Unmapped, self, 0, 0);
384     g_assert(self != NULL);
385
386     /* give the client its border back */
387     client_toggle_border(self, TRUE);
388
389     /* reparent the window out of the frame, and free the frame */
390     frame_release_client(self->frame, self);
391     self->frame = NULL;
392      
393     if (ob_state != State_Exiting) {
394         /* these values should not be persisted across a window
395            unmapping/mapping */
396         prop_erase(self->window, prop_atoms.net_wm_desktop);
397         prop_erase(self->window, prop_atoms.net_wm_state);
398     } else {
399         /* if we're left in an iconic state, the client wont be mapped. this is
400            bad, since we will no longer be managing the window on restart */
401         if (self->iconic)
402             XMapWindow(ob_display, self->window);
403     }
404
405
406     g_message("Unmanaged window 0x%lx", self->window);
407
408     /* free all data allocated in the client struct */
409     g_slist_free(self->transients);
410     for (j = 0; j < self->nicons; ++j)
411         g_free(self->icons[j].data);
412     if (self->nicons > 0)
413         g_free(self->icons);
414     g_free(self->title);
415     g_free(self->icon_title);
416     g_free(self->name);
417     g_free(self->class);
418     g_free(self->role);
419     g_free(self);
420      
421     /* update the list hints */
422     client_set_list();
423 }
424
425 void client_move_onscreen(Client *self)
426 {
427     Rect *a;
428     int x = self->frame->area.x, y = self->frame->area.y;
429
430     a = screen_area(self->desktop);
431     if (x >= a->x + a->width - 1)
432         x = a->x + a->width - self->frame->area.width;
433     if (y >= a->y + a->height - 1)
434         y = a->y + a->height - self->frame->area.height;
435     if (x + self->frame->area.width - 1 < a->x)
436         x = a->x;
437     if (y + self->frame->area.height - 1< a->y)
438         y = a->y;
439
440     frame_frame_gravity(self->frame, &x, &y); /* get where the client
441                                                  should be */
442     client_configure(self , Corner_TopLeft, x, y,
443                      self->area.width, self->area.height,
444                      TRUE, TRUE);
445 }
446
447 static void client_toggle_border(Client *self, gboolean show)
448 {
449     /* adjust our idea of where the client is, based on its border. When the
450        border is removed, the client should now be considered to be in a
451        different position.
452        when re-adding the border to the client, the same operation needs to be
453        reversed. */
454     int oldx = self->area.x, oldy = self->area.y;
455     int x = oldx, y = oldy;
456     switch(self->gravity) {
457     default:
458     case NorthWestGravity:
459     case WestGravity:
460     case SouthWestGravity:
461         break;
462     case NorthEastGravity:
463     case EastGravity:
464     case SouthEastGravity:
465         if (show) x -= self->border_width * 2;
466         else      x += self->border_width * 2;
467         break;
468     case NorthGravity:
469     case SouthGravity:
470     case CenterGravity:
471     case ForgetGravity:
472     case StaticGravity:
473         if (show) x -= self->border_width;
474         else      x += self->border_width;
475         break;
476     }
477     switch(self->gravity) {
478     default:
479     case NorthWestGravity:
480     case NorthGravity:
481     case NorthEastGravity:
482         break;
483     case SouthWestGravity:
484     case SouthGravity:
485     case SouthEastGravity:
486         if (show) y -= self->border_width * 2;
487         else      y += self->border_width * 2;
488         break;
489     case WestGravity:
490     case EastGravity:
491     case CenterGravity:
492     case ForgetGravity:
493     case StaticGravity:
494         if (show) y -= self->border_width;
495         else      y += self->border_width;
496         break;
497     }
498     self->area.x = x;
499     self->area.y = y;
500
501     if (show) {
502         XSetWindowBorderWidth(ob_display, self->window, self->border_width);
503
504         /* move the client so it is back it the right spot _with_ its
505            border! */
506         if (x != oldx || y != oldy)
507             XMoveWindow(ob_display, self->window, x, y);
508     } else
509         XSetWindowBorderWidth(ob_display, self->window, 0);
510 }
511
512
513 static void client_get_all(Client *self)
514 {
515     /* update EVERYTHING!! */
516
517     self->ignore_unmaps = 0;
518   
519     /* defaults */
520     self->frame = NULL;
521     self->title = self->icon_title = NULL;
522     self->title_count = 1;
523     self->name = self->class = self->role = NULL;
524     self->wmstate = NormalState;
525     self->transient = FALSE;
526     self->transients = NULL;
527     self->transient_for = NULL;
528     self->layer = -1;
529     self->urgent = FALSE;
530     self->positioned = FALSE;
531     self->disabled_decorations = 0;
532     self->group = NULL;
533     self->nicons = 0;
534
535     client_get_area(self);
536     client_update_transient_for(self);
537     client_update_wmhints(self);
538     client_get_desktop(self);
539     client_get_state(self);
540     client_get_shaped(self);
541
542     client_get_mwm_hints(self);
543     client_get_type(self);/* this can change the mwmhints for special cases */
544
545     client_update_protocols(self);
546
547     client_get_gravity(self); /* get the attribute gravity */
548     client_update_normal_hints(self); /* this may override the attribute
549                                          gravity */
550
551     /* got the type, the mwmhints, the protocols, and the normal hints
552        (min/max sizes), so we're ready to set up the decorations/functions */
553     client_setup_decor_and_functions(self);
554   
555     client_update_title(self);
556     client_update_class(self);
557     client_update_strut(self);
558     client_update_icons(self);
559
560     client_change_state(self);
561 }
562
563 static void client_get_area(Client *self)
564 {
565     XWindowAttributes wattrib;
566     Status ret;
567   
568     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
569     g_assert(ret != BadWindow);
570
571     RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
572     self->border_width = wattrib.border_width;
573 }
574
575 static void client_get_desktop(Client *self)
576 {
577     guint32 d;
578
579     if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
580         if (d >= screen_num_desktops && d != DESKTOP_ALL)
581             d = screen_num_desktops - 1;
582         self->desktop = d;
583     } else { 
584         gboolean trdesk = FALSE;
585
586        if (self->transient_for) {
587            if (self->transient_for != TRAN_GROUP) {
588                 self->desktop = self->transient_for->desktop;
589                 trdesk = TRUE;
590             } else {
591                 GSList *it;
592
593                 for (it = self->group->members; it; it = it->next)
594                     if (it->data != self &&
595                         !((Client*)it->data)->transient_for) {
596                         self->desktop = ((Client*)it->data)->desktop;
597                         trdesk = TRUE;
598                         break;
599                     }
600             }
601        }
602        if (!trdesk)
603            /* defaults to the current desktop */
604            self->desktop = screen_desktop;
605
606         /* set the desktop hint, to make sure that it always exists */
607         PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
608     }
609 }
610
611 static void client_get_state(Client *self)
612 {
613     guint32 *state;
614     guint num;
615   
616     self->modal = self->shaded = self->max_horz = self->max_vert =
617         self->fullscreen = self->above = self->below = self->iconic =
618         self->skip_taskbar = self->skip_pager = FALSE;
619
620     if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
621         gulong i;
622         for (i = 0; i < num; ++i) {
623             if (state[i] == prop_atoms.net_wm_state_modal)
624                 self->modal = TRUE;
625             else if (state[i] == prop_atoms.net_wm_state_shaded)
626                 self->shaded = TRUE;
627             else if (state[i] == prop_atoms.net_wm_state_hidden)
628                 self->iconic = TRUE;
629             else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
630                 self->skip_taskbar = TRUE;
631             else if (state[i] == prop_atoms.net_wm_state_skip_pager)
632                 self->skip_pager = TRUE;
633             else if (state[i] == prop_atoms.net_wm_state_fullscreen)
634                 self->fullscreen = TRUE;
635             else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
636                 self->max_vert = TRUE;
637             else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
638                 self->max_horz = TRUE;
639             else if (state[i] == prop_atoms.net_wm_state_above)
640                 self->above = TRUE;
641             else if (state[i] == prop_atoms.net_wm_state_below)
642                 self->below = TRUE;
643         }
644
645         g_free(state);
646     }
647 }
648
649 static void client_get_shaped(Client *self)
650 {
651     self->shaped = FALSE;
652 #ifdef   SHAPE
653     if (extensions_shape) {
654         int foo;
655         guint ufoo;
656         int s;
657
658         XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
659
660         XShapeQueryExtents(ob_display, self->window, &s, &foo,
661                            &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
662                            &ufoo);
663         self->shaped = (s != 0);
664     }
665 #endif
666 }
667
668 void client_update_transient_for(Client *self)
669 {
670     Window t = None;
671     Client *c = NULL;
672
673     if (XGetTransientForHint(ob_display, self->window, &t)) {
674         self->transient = TRUE;
675         if (t != self->window) { /* cant be transient to itself! */
676             c = g_hash_table_lookup(window_map, &t);
677             /* if this happens then we need to check for it*/
678             g_assert(c != self);
679             g_assert(!c || WINDOW_IS_CLIENT(c));
680             
681             if (!c && self->group) {
682                 /* not transient to a client, see if it is transient for a
683                    group */
684                 if (t == self->group->leader ||
685                     t == None ||
686                     t == ob_root) {
687                     /* window is a transient for its group! */
688                     c = TRAN_GROUP;
689                 }
690             }
691         }
692     } else
693         self->transient = FALSE;
694
695     /* if anything has changed... */
696     if (c != self->transient_for) {
697         if (self->transient_for == TRAN_GROUP) { /* transient of group */
698             GSList *it;
699
700             /* remove from old parents */
701             for (it = self->group->members; it; it = it->next)
702                 if (it->data != self &&
703                     !((Client*)it->data)->transient_for)
704                     ((Client*)it->data)->transients =
705                         g_slist_remove(((Client*)it->data)->transients, self);
706         } else if (self->transient_for != NULL) { /* transient of window */
707             /* remove from old parent */
708             self->transient_for->transients =
709                 g_slist_remove(self->transient_for->transients, self);
710         }
711         self->transient_for = c;
712         if (self->transient_for == TRAN_GROUP) { /* transient of group */
713             GSList *it;
714
715             /* add to new parents */
716             for (it = self->group->members; it; it = it->next)
717                 if (it->data != self &&
718                     !((Client*)it->data)->transient_for)
719                     ((Client*)it->data)->transients =
720                         g_slist_append(((Client*)it->data)->transients, self);
721
722             /* remove all transients which are in the group, that causes
723                circlular pointer hell of doom */
724             for (it = self->group->members; it; it = it->next) {
725                 GSList *sit, *next;
726                 for (sit = self->transients; sit; sit = next) {
727                     next = sit->next;
728                     if (sit->data == it->data)
729                         self->transients = g_slist_remove(self->transients,
730                                                           sit->data);
731                 }
732             }
733         } else if (self->transient_for != NULL) { /* transient of window */
734             /* add to new parent */
735             self->transient_for->transients =
736                 g_slist_append(self->transient_for->transients, self);
737         }
738     }
739 }
740
741 static void client_get_mwm_hints(Client *self)
742 {
743     guint num;
744     guint32 *hints;
745
746     self->mwmhints.flags = 0; /* default to none */
747
748     if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
749                     &hints, &num)) {
750         if (num >= MWM_ELEMENTS) {
751             self->mwmhints.flags = hints[0];
752             self->mwmhints.functions = hints[1];
753             self->mwmhints.decorations = hints[2];
754         }
755         g_free(hints);
756     }
757 }
758
759 void client_get_type(Client *self)
760 {
761     guint num, i;
762     guint32 *val;
763
764     self->type = -1;
765   
766     if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
767         /* use the first value that we know about in the array */
768         for (i = 0; i < num; ++i) {
769             if (val[i] == prop_atoms.net_wm_window_type_desktop)
770                 self->type = Type_Desktop;
771             else if (val[i] == prop_atoms.net_wm_window_type_dock)
772                 self->type = Type_Dock;
773             else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
774                 self->type = Type_Toolbar;
775             else if (val[i] == prop_atoms.net_wm_window_type_menu)
776                 self->type = Type_Menu;
777             else if (val[i] == prop_atoms.net_wm_window_type_utility)
778                 self->type = Type_Utility;
779             else if (val[i] == prop_atoms.net_wm_window_type_splash)
780                 self->type = Type_Splash;
781             else if (val[i] == prop_atoms.net_wm_window_type_dialog)
782                 self->type = Type_Dialog;
783             else if (val[i] == prop_atoms.net_wm_window_type_normal)
784                 self->type = Type_Normal;
785             else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
786                 /* prevent this window from getting any decor or
787                    functionality */
788                 self->mwmhints.flags &= (MwmFlag_Functions |
789                                          MwmFlag_Decorations);
790                 self->mwmhints.decorations = 0;
791                 self->mwmhints.functions = 0;
792             }
793             if (self->type != (WindowType) -1)
794                 break; /* grab the first legit type */
795         }
796         g_free(val);
797     }
798     
799     if (self->type == (WindowType) -1) {
800         /*the window type hint was not set, which means we either classify
801           ourself as a normal window or a dialog, depending on if we are a
802           transient. */
803         if (self->transient)
804             self->type = Type_Dialog;
805         else
806             self->type = Type_Normal;
807     }
808 }
809
810 void client_update_protocols(Client *self)
811 {
812     guint32 *proto;
813     guint num_return, i;
814
815     self->focus_notify = FALSE;
816     self->delete_window = FALSE;
817
818     if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
819         for (i = 0; i < num_return; ++i) {
820             if (proto[i] == prop_atoms.wm_delete_window) {
821                 /* this means we can request the window to close */
822                 self->delete_window = TRUE;
823             } else if (proto[i] == prop_atoms.wm_take_focus)
824                 /* if this protocol is requested, then the window will be
825                    notified whenever we want it to receive focus */
826                 self->focus_notify = TRUE;
827         }
828         g_free(proto);
829     }
830 }
831
832 static void client_get_gravity(Client *self)
833 {
834     XWindowAttributes wattrib;
835     Status ret;
836
837     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
838     g_assert(ret != BadWindow);
839     self->gravity = wattrib.win_gravity;
840 }
841
842 void client_update_normal_hints(Client *self)
843 {
844     XSizeHints size;
845     long ret;
846     int oldgravity = self->gravity;
847
848     /* defaults */
849     self->min_ratio = 0.0f;
850     self->max_ratio = 0.0f;
851     SIZE_SET(self->size_inc, 1, 1);
852     SIZE_SET(self->base_size, 0, 0);
853     SIZE_SET(self->min_size, 0, 0);
854     SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
855
856     /* get the hints from the window */
857     if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
858         self->positioned = !!(size.flags & (PPosition|USPosition));
859
860         if (size.flags & PWinGravity) {
861             self->gravity = size.win_gravity;
862       
863             /* if the client has a frame, i.e. has already been mapped and
864                is changing its gravity */
865             if (self->frame && self->gravity != oldgravity) {
866                 /* move our idea of the client's position based on its new
867                    gravity */
868                 self->area.x = self->frame->area.x;
869                 self->area.y = self->frame->area.y;
870                 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
871             }
872         }
873
874         if (size.flags & PAspect) {
875             if (size.min_aspect.y)
876                 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
877             if (size.max_aspect.y)
878                 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
879         }
880
881         if (size.flags & PMinSize)
882             SIZE_SET(self->min_size, size.min_width, size.min_height);
883     
884         if (size.flags & PMaxSize)
885             SIZE_SET(self->max_size, size.max_width, size.max_height);
886     
887         if (size.flags & PBaseSize)
888             SIZE_SET(self->base_size, size.base_width, size.base_height);
889     
890         if (size.flags & PResizeInc)
891             SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
892     }
893 }
894
895 void client_setup_decor_and_functions(Client *self)
896 {
897     /* start with everything (cept fullscreen) */
898     self->decorations = Decor_Titlebar | Decor_Handle | Decor_Border |
899         Decor_Icon | Decor_AllDesktops | Decor_Iconify | Decor_Maximize |
900         Decor_Shade;
901     self->functions = Func_Resize | Func_Move | Func_Iconify | Func_Maximize |
902         Func_Shade;
903     if (self->delete_window) {
904         self->decorations |= Decor_Close;
905         self->functions |= Func_Close;
906     }
907
908     if (!(self->min_size.width < self->max_size.width ||
909           self->min_size.height < self->max_size.height)) {
910         self->decorations &= ~(Decor_Maximize | Decor_Handle);
911         self->functions &= ~(Func_Resize | Func_Maximize);
912     }
913
914     switch (self->type) {
915     case Type_Normal:
916         /* normal windows retain all of the possible decorations and
917            functionality, and are the only windows that you can fullscreen */
918         self->functions |= Func_Fullscreen;
919         break;
920
921     case Type_Dialog:
922     case Type_Utility:
923         /* these windows cannot be maximized */
924         self->decorations &= ~Decor_Maximize;
925         self->functions &= ~Func_Maximize;
926         break;
927
928     case Type_Menu:
929     case Type_Toolbar:
930         /* these windows get less functionality */
931         self->decorations &= ~(Decor_Iconify | Decor_Handle);
932         self->functions &= ~(Func_Iconify | Func_Resize);
933         break;
934
935     case Type_Desktop:
936     case Type_Dock:
937     case Type_Splash:
938         /* none of these windows are manipulated by the window manager */
939         self->decorations = 0;
940         self->functions = 0;
941         break;
942     }
943
944     /* Mwm Hints are applied subtractively to what has already been chosen for
945        decor and functionality */
946     if (self->mwmhints.flags & MwmFlag_Decorations) {
947         if (! (self->mwmhints.decorations & MwmDecor_All)) {
948             if (! (self->mwmhints.decorations & MwmDecor_Border))
949                 self->decorations &= ~Decor_Border;
950             if (! (self->mwmhints.decorations & MwmDecor_Handle))
951                 self->decorations &= ~Decor_Handle;
952             if (! (self->mwmhints.decorations & MwmDecor_Title))
953                 self->decorations &= ~Decor_Titlebar;
954             if (! (self->mwmhints.decorations & MwmDecor_Iconify))
955                 self->decorations &= ~Decor_Iconify;
956             if (! (self->mwmhints.decorations & MwmDecor_Maximize))
957                 self->decorations &= ~Decor_Maximize;
958         }
959     }
960
961     if (self->mwmhints.flags & MwmFlag_Functions) {
962         if (! (self->mwmhints.functions & MwmFunc_All)) {
963             if (! (self->mwmhints.functions & MwmFunc_Resize))
964                 self->functions &= ~Func_Resize;
965             if (! (self->mwmhints.functions & MwmFunc_Move))
966                 self->functions &= ~Func_Move;
967             if (! (self->mwmhints.functions & MwmFunc_Iconify))
968                 self->functions &= ~Func_Iconify;
969             if (! (self->mwmhints.functions & MwmFunc_Maximize))
970                 self->functions &= ~Func_Maximize;
971             /* dont let mwm hints kill the close button
972                if (! (self->mwmhints.functions & MwmFunc_Close))
973                self->functions &= ~Func_Close; */
974         }
975     }
976
977     /* can't maximize without moving/resizing */
978     if (!((self->functions & Func_Move) && (self->functions & Func_Resize)))
979         self->functions &= ~(Func_Maximize | Func_Fullscreen);
980
981     /* finally, user specified disabled decorations are applied to subtract
982        decorations */
983     if (self->disabled_decorations & Decor_Titlebar)
984         self->decorations &= ~Decor_Titlebar;
985     if (self->disabled_decorations & Decor_Handle)
986         self->decorations &= ~Decor_Handle;
987     if (self->disabled_decorations & Decor_Border)
988         self->decorations &= ~Decor_Border;
989     if (self->disabled_decorations & Decor_Iconify)
990         self->decorations &= ~Decor_Iconify;
991     if (self->disabled_decorations & Decor_Maximize)
992         self->decorations &= ~Decor_Maximize;
993     if (self->disabled_decorations & Decor_AllDesktops)
994         self->decorations &= ~Decor_AllDesktops;
995     if (self->disabled_decorations & Decor_Shade)
996         self->decorations &= ~Decor_Shade;
997     if (self->disabled_decorations & Decor_Close)
998         self->decorations &= ~Decor_Close;
999
1000     /* if we don't have a titlebar, then we cannot shade! */
1001     if (!(self->decorations & Decor_Titlebar))
1002         self->functions &= ~Func_Shade;
1003
1004     /* now we need to check against rules for the client's current state */
1005     if (self->fullscreen) {
1006         self->functions &= (Func_Close | Func_Fullscreen | Func_Iconify);
1007         self->decorations = 0;
1008     }
1009
1010     client_change_allowed_actions(self);
1011
1012     if (self->frame) {
1013         /* this makes sure that these windows appear on all desktops */
1014         if (self->type == Type_Desktop && self->desktop != DESKTOP_ALL)
1015             client_set_desktop(self, DESKTOP_ALL, FALSE);
1016
1017         /* change the decors on the frame, and with more/less decorations,
1018            we may also need to be repositioned */
1019         frame_adjust_area(self->frame, TRUE, TRUE);
1020         /* with new decor, the window's maximized size may change */
1021         client_remaximize(self);
1022     } else {
1023         /* this makes sure that these windows appear on all desktops */
1024         if (self->type == Type_Desktop && self->desktop != DESKTOP_ALL)
1025             self->desktop = DESKTOP_ALL;
1026     }
1027 }
1028
1029 static void client_change_allowed_actions(Client *self)
1030 {
1031     guint32 actions[9];
1032     int num = 0;
1033
1034     /* desktop windows are kept on all desktops */
1035     if (self->type != Type_Desktop)
1036         actions[num++] = prop_atoms.net_wm_action_change_desktop;
1037
1038     if (self->functions & Func_Shade)
1039         actions[num++] = prop_atoms.net_wm_action_shade;
1040     if (self->functions & Func_Close)
1041         actions[num++] = prop_atoms.net_wm_action_close;
1042     if (self->functions & Func_Move)
1043         actions[num++] = prop_atoms.net_wm_action_move;
1044     if (self->functions & Func_Iconify)
1045         actions[num++] = prop_atoms.net_wm_action_minimize;
1046     if (self->functions & Func_Resize)
1047         actions[num++] = prop_atoms.net_wm_action_resize;
1048     if (self->functions & Func_Fullscreen)
1049         actions[num++] = prop_atoms.net_wm_action_fullscreen;
1050     if (self->functions & Func_Maximize) {
1051         actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1052         actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1053     }
1054
1055     PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1056
1057     /* make sure the window isn't breaking any rules now */
1058
1059     if (!(self->functions & Func_Shade) && self->shaded) {
1060         if (self->frame) client_shade(self, FALSE);
1061         else self->shaded = FALSE;
1062     }
1063     if (!(self->functions & Func_Iconify) && self->iconic) {
1064         g_message("UNSETTING ICONIC");
1065         if (self->frame) client_iconify(self, FALSE, TRUE);
1066         else self->iconic = FALSE;
1067     }
1068     if (!(self->functions & Func_Fullscreen) && self->fullscreen) {
1069         if (self->frame) client_fullscreen(self, FALSE, TRUE);
1070         else self->fullscreen = FALSE;
1071     }
1072     if (!(self->functions & Func_Maximize) && (self->max_horz ||
1073                                                self->max_vert)) {
1074         if (self->frame) client_maximize(self, FALSE, 0, TRUE);
1075         else self->max_vert = self->max_horz = FALSE;
1076     }
1077 }
1078
1079 void client_remaximize(Client *self)
1080 {
1081     int dir;
1082     if (self->max_horz && self->max_vert)
1083         dir = 0;
1084     else if (self->max_horz)
1085         dir = 1;
1086     else if (self->max_vert)
1087         dir = 2;
1088     else
1089         return; /* not maximized */
1090     self->max_horz = self->max_vert = FALSE;
1091     client_maximize(self, TRUE, dir, FALSE);
1092 }
1093
1094 void client_update_wmhints(Client *self)
1095 {
1096     XWMHints *hints;
1097     gboolean ur = FALSE;
1098     GSList *it;
1099
1100     /* assume a window takes input if it doesnt specify */
1101     self->can_focus = TRUE;
1102   
1103     if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1104         if (hints->flags & InputHint)
1105             self->can_focus = hints->input;
1106
1107         /* only do this when first managing the window *AND* when we aren't
1108            starting up! */
1109         if (ob_state != State_Starting && self->frame == NULL)
1110             if (hints->flags & StateHint)
1111                 self->iconic = hints->initial_state == IconicState;
1112
1113         if (hints->flags & XUrgencyHint)
1114             ur = TRUE;
1115
1116         if (!(hints->flags & WindowGroupHint))
1117             hints->window_group = None;
1118
1119         /* did the group state change? */
1120         if (hints->window_group !=
1121             (self->group ? self->group->leader : None)) {
1122             /* remove from the old group if there was one */
1123             if (self->group != NULL) {
1124                 /* remove transients of the group */
1125                 for (it = self->group->members; it; it = it->next)
1126                     self->transients = g_slist_remove(self->transients,
1127                                                       it->data);
1128                 group_remove(self->group, self);
1129                 self->group = NULL;
1130             }
1131             /* i can only have transients from the group if i am not transient
1132                myself */
1133             if (hints->window_group != None && !self->transient_for) {
1134                 self->group = group_add(hints->window_group, self);
1135
1136                 /* add other transients of the group that are already
1137                    set up */
1138                 for (it = self->group->members; it; it = it->next)
1139                     if (it->data != self &&
1140                         ((Client*)it->data)->transient_for == TRAN_GROUP)
1141                         self->transients = g_slist_append(self->transients,
1142                                                           it->data);
1143             }
1144
1145             /* the WM_HINTS can contain an icon */
1146             client_update_icons(self);
1147
1148             /* because the self->transient flag wont change from this call,
1149                we don't need to update the window's type and such, only its
1150                transient_for, and the transients lists of other windows in
1151                the group may be affected */
1152             client_update_transient_for(self);
1153         }
1154
1155         XFree(hints);
1156     }
1157
1158     if (ur != self->urgent) {
1159         self->urgent = ur;
1160         g_message("Urgent Hint for 0x%lx: %s", self->window,
1161                   ur ? "ON" : "OFF");
1162         /* fire the urgent callback if we're mapped, otherwise, wait until
1163            after we're mapped */
1164         if (self->frame)
1165             dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1166     }
1167 }
1168
1169 void client_update_title(Client *self)
1170 {
1171     GList *it;
1172     guint32 nums;
1173     guint i;
1174     char *data = NULL;
1175     gboolean read_title;
1176
1177     g_free(self->title);
1178      
1179     /* try netwm */
1180     if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1181         /* try old x stuff */
1182         if (!PROP_GETS(self->window, wm_name, locale, &data))
1183             data = g_strdup("Unnamed Window");
1184
1185     /* look for duplicates and append a number */
1186     nums = 0;
1187     for (it = client_list; it; it = it->next)
1188         if (it->data != self) {
1189             Client *c = it->data;
1190             if (0 == strncmp(c->title, data, strlen(data)))
1191                 nums |= 1 << c->title_count;
1192         }
1193     /* find first free number */
1194     for (i = 1; i <= 32; ++i)
1195         if (!(nums & (1 << i))) {
1196             if (self->title_count == 1 || i == 1)
1197                 self->title_count = i;
1198             break;
1199         }
1200     /* dont display the number for the first window */
1201     if (self->title_count > 1) {
1202         char *vdata, *ndata;
1203         ndata = g_strdup_printf(" - [%u]", self->title_count);
1204         vdata = g_strconcat(data, ndata, NULL);
1205         g_free(ndata);
1206         g_free(data);
1207         data = vdata;
1208     }
1209
1210     PROP_SETS(self->window, net_wm_visible_name, data);
1211
1212     self->title = data;
1213
1214     if (self->frame)
1215         frame_adjust_title(self->frame);
1216
1217     /* update the icon title */
1218     data = NULL;
1219     g_free(self->icon_title);
1220
1221     read_title = TRUE;
1222     /* try netwm */
1223     if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1224         /* try old x stuff */
1225         if (!PROP_GETS(self->window, wm_icon_name, locale, &data)) {
1226             data = g_strdup(self->title);
1227             read_title = FALSE;
1228         }
1229
1230     /* append the title count, dont display the number for the first window */
1231     if (read_title && self->title_count > 1) {
1232         char *vdata, *ndata;
1233         ndata = g_strdup_printf(" - [%u]", self->title_count);
1234         vdata = g_strconcat(data, ndata, NULL);
1235         g_free(ndata);
1236         g_free(data);
1237         data = vdata;
1238     }
1239
1240     PROP_SETS(self->window, net_wm_visible_icon_name, data);
1241
1242     self->icon_title = data;
1243 }
1244
1245 void client_update_class(Client *self)
1246 {
1247     char **data;
1248     char *s;
1249
1250     if (self->name) g_free(self->name);
1251     if (self->class) g_free(self->class);
1252     if (self->role) g_free(self->role);
1253
1254     self->name = self->class = self->role = NULL;
1255
1256     if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1257         if (data[0]) {
1258             self->name = g_strdup(data[0]);
1259             if (data[1])
1260                 self->class = g_strdup(data[1]);
1261         }
1262         g_strfreev(data);     
1263     }
1264
1265     if (PROP_GETS(self->window, wm_window_role, locale, &s))
1266         self->role = g_strdup(s);
1267
1268     if (self->name == NULL) self->name = g_strdup("");
1269     if (self->class == NULL) self->class = g_strdup("");
1270     if (self->role == NULL) self->role = g_strdup("");
1271 }
1272
1273 void client_update_strut(Client *self)
1274 {
1275     guint num;
1276     guint32 *data;
1277
1278     if (!PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1279         STRUT_SET(self->strut, 0, 0, 0, 0);
1280     } else {
1281         if (num == 4)
1282             STRUT_SET(self->strut, data[0], data[2], data[1], data[3]);
1283         else
1284             STRUT_SET(self->strut, 0, 0, 0, 0);
1285         g_free(data);
1286     }
1287
1288     /* updating here is pointless while we're being mapped cuz we're not in
1289        the client list yet */
1290     if (self->frame)
1291         screen_update_struts();
1292 }
1293
1294 void client_update_icons(Client *self)
1295 {
1296     guint num;
1297     guint32 *data;
1298     guint w, h, i;
1299     int j;
1300
1301     for (j = 0; j < self->nicons; ++j)
1302         g_free(self->icons[j].data);
1303     if (self->nicons > 0)
1304         g_free(self->icons);
1305     self->nicons = 0;
1306
1307     if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1308         /* figure out how many valid icons are in here */
1309         i = 0;
1310         while (num - i > 2) {
1311             w = data[i++];
1312             h = data[i++];
1313             i += w * h;
1314             if (i > num) break;
1315             ++self->nicons;
1316         }
1317
1318         self->icons = g_new(Icon, self->nicons);
1319     
1320         /* store the icons */
1321         i = 0;
1322         for (j = 0; j < self->nicons; ++j) {
1323             guint x, y, t;
1324
1325             w = self->icons[j].width = data[i++];
1326             h = self->icons[j].height = data[i++];
1327
1328             self->icons[j].data = g_new(pixel32, w * h);
1329             for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1330                 if (x >= w) {
1331                     x = 0;
1332                     ++y;
1333                 }
1334                 self->icons[j].data[t] =
1335                     (((data[i] >> 24) & 0xff) << default_alpha_offset) +
1336                     (((data[i] >> 16) & 0xff) << default_red_offset) +
1337                     (((data[i] >> 8) & 0xff) << default_green_offset) +
1338                     (((data[i] >> 0) & 0xff) << default_blue_offset);
1339             }
1340             g_assert(i <= num);
1341         }
1342
1343         g_free(data);
1344     } else if (PROP_GETA32(self->window, kwm_win_icon,
1345                            kwm_win_icon, &data, &num)) {
1346         if (num == 2) {
1347             self->nicons++;
1348             self->icons = g_new(Icon, self->nicons);
1349             if (!render_pixmap_to_rgba(data[0], data[1],
1350                                        &self->icons[self->nicons-1].width,
1351                                        &self->icons[self->nicons-1].height,
1352                                        &self->icons[self->nicons-1].data)) {
1353                 g_free(&self->icons[self->nicons-1]);
1354                 self->nicons--;
1355             }
1356         }
1357         g_free(data);
1358     } else {
1359         XWMHints *hints;
1360
1361         if ((hints = XGetWMHints(ob_display, self->window))) {
1362             if (hints->flags & IconPixmapHint) {
1363                 self->nicons++;
1364                 self->icons = g_new(Icon, self->nicons);
1365                 if (!render_pixmap_to_rgba(hints->icon_pixmap,
1366                                            (hints->flags & IconMaskHint ?
1367                                             hints->icon_mask : None),
1368                                            &self->icons[self->nicons-1].width,
1369                                            &self->icons[self->nicons-1].height,
1370                                            &self->icons[self->nicons-1].data)){
1371                     g_free(&self->icons[self->nicons-1]);
1372                     self->nicons--;
1373                 }
1374             }
1375             XFree(hints);
1376         }
1377     }
1378
1379     if (self->frame)
1380         frame_adjust_icon(self->frame);
1381 }
1382
1383 static void client_change_state(Client *self)
1384 {
1385     guint32 state[2];
1386     guint32 netstate[10];
1387     guint num;
1388
1389     state[0] = self->wmstate;
1390     state[1] = None;
1391     PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1392
1393     num = 0;
1394     if (self->modal)
1395         netstate[num++] = prop_atoms.net_wm_state_modal;
1396     if (self->shaded)
1397         netstate[num++] = prop_atoms.net_wm_state_shaded;
1398     if (self->iconic)
1399         netstate[num++] = prop_atoms.net_wm_state_hidden;
1400     if (self->skip_taskbar)
1401         netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1402     if (self->skip_pager)
1403         netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1404     if (self->fullscreen)
1405         netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1406     if (self->max_vert)
1407         netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1408     if (self->max_horz)
1409         netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1410     if (self->above)
1411         netstate[num++] = prop_atoms.net_wm_state_above;
1412     if (self->below)
1413         netstate[num++] = prop_atoms.net_wm_state_below;
1414     PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1415
1416     client_calc_layer(self);
1417
1418     if (self->frame)
1419         frame_adjust_state(self->frame);
1420 }
1421
1422 Client *client_search_focus_tree(Client *self)
1423 {
1424     GSList *it;
1425     Client *ret;
1426
1427     for (it = self->transients; it != NULL; it = it->next) {
1428         if (client_focused(it->data)) return it->data;
1429         if ((ret = client_search_focus_tree(it->data))) return ret;
1430     }
1431     return NULL;
1432 }
1433
1434 Client *client_search_focus_tree_full(Client *self)
1435 {
1436     if (self->transient_for) {
1437         if (self->transient_for != TRAN_GROUP) {
1438             return client_search_focus_tree_full(self->transient_for);
1439         } else {
1440             GSList *it;
1441             gboolean recursed = FALSE;
1442         
1443             for (it = self->group->members; it; it = it->next)
1444                 if (!((Client*)it->data)->transient_for) {
1445                     Client *c;
1446                     if ((c = client_search_focus_tree_full(it->data)))
1447                         return c;
1448                     recursed = TRUE;
1449                 }
1450             if (recursed)
1451               return NULL;
1452         }
1453     }
1454
1455     /* this function checks the whole tree, the client_search_focus_tree~
1456        does not, so we need to check this window */
1457     if (client_focused(self))
1458       return self;
1459     return client_search_focus_tree(self);
1460 }
1461
1462 static StackLayer calc_layer(Client *self)
1463 {
1464     StackLayer l;
1465
1466     if (self->fullscreen) l = Layer_Fullscreen;
1467     else if (self->type == Type_Desktop) l = Layer_Desktop;
1468     else if (self->type == Type_Dock) {
1469         if (!self->below) l = Layer_Top;
1470         else l = Layer_Normal;
1471     }
1472     else if (self->above) l = Layer_Above;
1473     else if (self->below) l = Layer_Below;
1474     else l = Layer_Normal;
1475
1476     return l;
1477 }
1478
1479 static void calc_recursive(Client *self, Client *orig, StackLayer l,
1480                            gboolean raised)
1481 {
1482     StackLayer old, own;
1483     GSList *it;
1484
1485     old = self->layer;
1486     own = calc_layer(self);
1487     self->layer = l > own ? l : own;
1488
1489     for (it = self->transients; it; it = it->next)
1490         calc_recursive(it->data, orig, l, raised ? raised : l != old);
1491
1492     if (!raised && l != old)
1493         if (orig->frame) /* only restack if the original window is managed */
1494             stacking_raise(CLIENT_AS_WINDOW(self));
1495 }
1496
1497 void client_calc_layer(Client *self)
1498 {
1499     StackLayer l;
1500     Client *orig;
1501
1502     orig = self;
1503
1504     /* transients take on the layer of their parents */
1505     if (self->transient_for) {
1506         if (self->transient_for != TRAN_GROUP) {
1507             self = self->transient_for;
1508         } else {
1509             GSList *it;
1510
1511             for (it = self->group->members; it; it = it->next)
1512                 if (it->data != self &&
1513                     !((Client*)it->data)->transient_for) {
1514                     self = it->data;
1515                     break;
1516                 }
1517         }
1518     }
1519
1520     l = calc_layer(self);
1521
1522     calc_recursive(self, orig, l, FALSE);
1523 }
1524
1525 gboolean client_should_show(Client *self)
1526 {
1527     if (self->iconic) return FALSE;
1528     else if (!(self->desktop == screen_desktop ||
1529                self->desktop == DESKTOP_ALL)) return FALSE;
1530     else if (client_normal(self) && screen_showing_desktop) return FALSE;
1531     
1532     return TRUE;
1533 }
1534
1535 static void client_showhide(Client *self)
1536 {
1537
1538     if (client_should_show(self))
1539         frame_show(self->frame);
1540     else
1541         frame_hide(self->frame);
1542 }
1543
1544 gboolean client_normal(Client *self) {
1545     return ! (self->type == Type_Desktop || self->type == Type_Dock ||
1546               self->type == Type_Splash);
1547 }
1548
1549 static void client_apply_startup_state(Client *self)
1550 {
1551     /* these are in a carefully crafted order.. */
1552
1553     if (self->iconic) {
1554         self->iconic = FALSE;
1555         client_iconify(self, TRUE, FALSE);
1556     }
1557     if (self->fullscreen) {
1558         self->fullscreen = FALSE;
1559         client_fullscreen(self, TRUE, FALSE);
1560     }
1561     if (self->shaded) {
1562         self->shaded = FALSE;
1563         client_shade(self, TRUE);
1564     }
1565     if (self->urgent)
1566         dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1567   
1568     if (self->max_vert && self->max_horz) {
1569         self->max_vert = self->max_horz = FALSE;
1570         client_maximize(self, TRUE, 0, FALSE);
1571     } else if (self->max_vert) {
1572         self->max_vert = FALSE;
1573         client_maximize(self, TRUE, 2, FALSE);
1574     } else if (self->max_horz) {
1575         self->max_horz = FALSE;
1576         client_maximize(self, TRUE, 1, FALSE);
1577     }
1578
1579     /* nothing to do for the other states:
1580        skip_taskbar
1581        skip_pager
1582        modal
1583        above
1584        below
1585     */
1586 }
1587
1588 void client_configure(Client *self, Corner anchor, int x, int y, int w, int h,
1589                       gboolean user, gboolean final)
1590 {
1591     gboolean moved = FALSE, resized = FALSE;
1592
1593     /* gets the frame's position */
1594     frame_client_gravity(self->frame, &x, &y);
1595
1596     /* these positions are frame positions, not client positions */
1597
1598     /* set the size and position if fullscreen */
1599     if (self->fullscreen) {
1600         x = 0;
1601         y = 0;
1602         w = screen_physical_size.width;
1603         h = screen_physical_size.height;
1604         user = FALSE; /* ignore that increment etc shit when in fullscreen */
1605     } else {
1606         /* set the size and position if maximized */
1607         if (self->max_horz) {
1608             x = screen_area(self->desktop)->x - self->frame->size.left;
1609             w = screen_area(self->desktop)->width;
1610         }
1611         if (self->max_vert) {
1612             y = screen_area(self->desktop)->y;
1613             h = screen_area(self->desktop)->height -
1614                 self->frame->size.top - self->frame->size.bottom;
1615         }
1616     }
1617
1618     /* gets the client's position */
1619     frame_frame_gravity(self->frame, &x, &y);
1620
1621     /* these override the above states! if you cant move you can't move! */
1622     if (user) {
1623         if (!(self->functions & Func_Move)) {
1624             x = self->area.x;
1625             y = self->area.y;
1626         }
1627         if (!(self->functions & Func_Resize)) {
1628             w = self->area.width;
1629             h = self->area.height;
1630         }
1631     }
1632
1633     if (!(w == self->area.width && h == self->area.height)) {
1634         w -= self->base_size.width;
1635         h -= self->base_size.height;
1636
1637         if (user) {
1638             /* for interactive resizing. have to move half an increment in each
1639                direction. */
1640
1641             /* how far we are towards the next size inc */
1642             int mw = w % self->size_inc.width; 
1643             int mh = h % self->size_inc.height;
1644             /* amount to add */
1645             int aw = self->size_inc.width / 2;
1646             int ah = self->size_inc.height / 2;
1647             /* don't let us move into a new size increment */
1648             if (mw + aw >= self->size_inc.width)
1649                 aw = self->size_inc.width - mw - 1;
1650             if (mh + ah >= self->size_inc.height)
1651                 ah = self->size_inc.height - mh - 1;
1652             w += aw;
1653             h += ah;
1654     
1655             /* if this is a user-requested resize, then check against min/max
1656                sizes and aspect ratios */
1657
1658             /* smaller than min size or bigger than max size? */
1659             if (w > self->max_size.width) w = self->max_size.width;
1660             if (w < self->min_size.width) w = self->min_size.width;
1661             if (h > self->max_size.height) h = self->max_size.height;
1662             if (h < self->min_size.height) h = self->min_size.height;
1663
1664             /* adjust the height ot match the width for the aspect ratios */
1665             if (self->min_ratio)
1666                 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1667             if (self->max_ratio)
1668                 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1669         }
1670
1671         /* keep to the increments */
1672         w /= self->size_inc.width;
1673         h /= self->size_inc.height;
1674
1675         /* you cannot resize to nothing */
1676         if (w < 1) w = 1;
1677         if (h < 1) h = 1;
1678   
1679         /* store the logical size */
1680         SIZE_SET(self->logical_size, w, h);
1681
1682         w *= self->size_inc.width;
1683         h *= self->size_inc.height;
1684
1685         w += self->base_size.width;
1686         h += self->base_size.height;
1687     }
1688
1689     switch (anchor) {
1690     case Corner_TopLeft:
1691         break;
1692     case Corner_TopRight:
1693         x -= w - self->area.width;
1694         break;
1695     case Corner_BottomLeft:
1696         y -= h - self->area.height;
1697         break;
1698     case Corner_BottomRight:
1699         x -= w - self->area.width;
1700         y -= h - self->area.height;
1701         break;
1702     }
1703
1704     moved = x != self->area.x || y != self->area.y;
1705     resized = w != self->area.width || h != self->area.height;
1706
1707     RECT_SET(self->area, x, y, w, h);
1708
1709     if (resized)
1710         XResizeWindow(ob_display, self->window, w, h);
1711
1712     /* move/resize the frame to match the request */
1713     if (self->frame) {
1714         if (moved || resized)
1715             frame_adjust_area(self->frame, moved, resized);
1716
1717         if (!user || final) {
1718             XEvent event;
1719             event.type = ConfigureNotify;
1720             event.xconfigure.display = ob_display;
1721             event.xconfigure.event = self->window;
1722             event.xconfigure.window = self->window;
1723     
1724             /* root window real coords */
1725             event.xconfigure.x = self->frame->area.x + self->frame->size.left;
1726             event.xconfigure.y = self->frame->area.y + self->frame->size.top;
1727     
1728             event.xconfigure.width = w;
1729             event.xconfigure.height = h;
1730             event.xconfigure.border_width = 0;
1731             event.xconfigure.above = self->frame->plate;
1732             event.xconfigure.override_redirect = FALSE;
1733             XSendEvent(event.xconfigure.display, event.xconfigure.window,
1734                        FALSE, StructureNotifyMask, &event);
1735         }
1736     }
1737 }
1738
1739 void client_fullscreen(Client *self, gboolean fs, gboolean savearea)
1740 {
1741     int x, y, w, h;
1742
1743     if (!(self->functions & Func_Fullscreen) || /* can't */
1744         self->fullscreen == fs) return;         /* already done */
1745
1746     self->fullscreen = fs;
1747     client_change_state(self); /* change the state hints on the client,
1748                                   and adjust out layer/stacking */
1749
1750     if (fs) {
1751         if (savearea) {
1752             guint32 dimensions[4];
1753             dimensions[0] = self->area.x;
1754             dimensions[1] = self->area.y;
1755             dimensions[2] = self->area.width;
1756             dimensions[3] = self->area.height;
1757   
1758             PROP_SETA32(self->window, openbox_premax, cardinal,
1759                         dimensions, 4);
1760         }
1761
1762         /* these are not actually used cuz client_configure will set them
1763            as appropriate when the window is fullscreened */
1764         x = y = w = h = 0;
1765     } else {
1766         guint num;
1767         gint32 *dimensions;
1768
1769         /* pick some fallbacks... */
1770         x = screen_area(self->desktop)->x +
1771             screen_area(self->desktop)->width / 4;
1772         y = screen_area(self->desktop)->y +
1773             screen_area(self->desktop)->height / 4;
1774         w = screen_area(self->desktop)->width / 2;
1775         h = screen_area(self->desktop)->height / 2;
1776
1777         if (PROP_GETA32(self->window, openbox_premax, cardinal,
1778                         (guint32**)&dimensions, &num)) {
1779             if (num == 4) {
1780                 x = dimensions[0];
1781                 y = dimensions[1];
1782                 w = dimensions[2];
1783                 h = dimensions[3];
1784             }
1785             g_free(dimensions);
1786         }
1787     }
1788
1789     client_setup_decor_and_functions(self);
1790
1791     client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1792
1793     /* try focus us when we go into fullscreen mode */
1794     client_focus(self);
1795 }
1796
1797 void client_iconify(Client *self, gboolean iconic, gboolean curdesk)
1798 {
1799     GSList *it;
1800
1801     /* move up the transient chain as far as possible first */
1802     if (self->transient_for) {
1803         if (self->transient_for != TRAN_GROUP) {
1804             if (self->transient_for->iconic != iconic) {
1805                 client_iconify(self->transient_for, iconic, curdesk);
1806                 return;
1807             }
1808         } else {
1809             GSList *it;
1810
1811             for (it = self->group->members; it; it = it->next) {
1812                 Client *c = it->data;
1813                 if (c != self && c->iconic != iconic &&
1814                     !c->transient_for) {
1815                     client_iconify(it->data, iconic, curdesk);
1816                     break;
1817                 }
1818             }
1819             if (it != NULL) return;
1820         }
1821     }
1822
1823     if (self->iconic == iconic) return; /* nothing to do */
1824
1825     g_message("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
1826               self->window);
1827
1828     self->iconic = iconic;
1829
1830     if (iconic) {
1831         self->wmstate = IconicState;
1832         self->ignore_unmaps++;
1833         /* we unmap the client itself so that we can get MapRequest events,
1834            and because the ICCCM tells us to! */
1835         XUnmapWindow(ob_display, self->window);
1836
1837         /* update the focus lists.. iconic windows go to the bottom of the
1838          list, put the new iconic window at the 'top of the bottom'. */
1839         focus_order_to_top(self);
1840     } else {
1841         if (curdesk)
1842             client_set_desktop(self, screen_desktop, FALSE);
1843         self->wmstate = self->shaded ? IconicState : NormalState;
1844         XMapWindow(ob_display, self->window);
1845
1846         /* this puts it after the current focused window */
1847         focus_order_remove(self);
1848         focus_order_add_new(self);
1849     }
1850     client_change_state(self);
1851     client_showhide(self);
1852     screen_update_struts();
1853
1854     dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
1855                     self, 0, 0);
1856
1857     /* iconify all transients */
1858     for (it = self->transients; it != NULL; it = it->next)
1859         if (it->data != self) client_iconify(it->data, iconic, curdesk);
1860 }
1861
1862 void client_maximize(Client *self, gboolean max, int dir, gboolean savearea)
1863 {
1864     int x, y, w, h;
1865      
1866     g_assert(dir == 0 || dir == 1 || dir == 2);
1867     if (!(self->functions & Func_Maximize)) return; /* can't */
1868
1869     /* check if already done */
1870     if (max) {
1871         if (dir == 0 && self->max_horz && self->max_vert) return;
1872         if (dir == 1 && self->max_horz) return;
1873         if (dir == 2 && self->max_vert) return;
1874     } else {
1875         if (dir == 0 && !self->max_horz && !self->max_vert) return;
1876         if (dir == 1 && !self->max_horz) return;
1877         if (dir == 2 && !self->max_vert) return;
1878     }
1879
1880     /* work with the frame's coords */
1881     x = self->frame->area.x;
1882     y = self->frame->area.y;
1883     w = self->area.width;
1884     h = self->area.height;
1885
1886     if (max) {
1887         if (savearea) {
1888             gint32 dimensions[4];
1889             gint32 *readdim;
1890             guint num;
1891
1892             dimensions[0] = x;
1893             dimensions[1] = y;
1894             dimensions[2] = w;
1895             dimensions[3] = h;
1896
1897             /* get the property off the window and use it for the dimensions
1898                we are already maxed on */
1899             if (PROP_GETA32(self->window, openbox_premax, cardinal,
1900                             (guint32**)&readdim, &num)) {
1901                 if (num == 4) {
1902                     if (self->max_horz) {
1903                         dimensions[0] = readdim[0];
1904                         dimensions[2] = readdim[2];
1905                     }
1906                     if (self->max_vert) {
1907                         dimensions[1] = readdim[1];
1908                         dimensions[3] = readdim[3];
1909                     }
1910                 }
1911                 g_free(readdim);
1912             }
1913
1914             PROP_SETA32(self->window, openbox_premax, cardinal,
1915                         (guint32*)dimensions, 4);
1916         }
1917     } else {
1918         guint num;
1919         gint32 *dimensions;
1920
1921         /* pick some fallbacks... */
1922         if (dir == 0 || dir == 1) { /* horz */
1923             x = screen_area(self->desktop)->x +
1924                 screen_area(self->desktop)->width / 4;
1925             w = screen_area(self->desktop)->width / 2;
1926         }
1927         if (dir == 0 || dir == 2) { /* vert */
1928             y = screen_area(self->desktop)->y +
1929                 screen_area(self->desktop)->height / 4;
1930             h = screen_area(self->desktop)->height / 2;
1931         }
1932
1933         if (PROP_GETA32(self->window, openbox_premax, cardinal,
1934                         (guint32**)&dimensions, &num)) {
1935             if (num == 4) {
1936                 if (dir == 0 || dir == 1) { /* horz */
1937                     x = dimensions[0];
1938                     w = dimensions[2];
1939                 }
1940                 if (dir == 0 || dir == 2) { /* vert */
1941                     y = dimensions[1];
1942                     h = dimensions[3];
1943                 }
1944             }
1945             g_free(dimensions);
1946         }
1947     }
1948
1949     if (dir == 0 || dir == 1) /* horz */
1950         self->max_horz = max;
1951     if (dir == 0 || dir == 2) /* vert */
1952         self->max_vert = max;
1953
1954     if (!self->max_horz && !self->max_vert)
1955         PROP_ERASE(self->window, openbox_premax);
1956
1957     client_change_state(self); /* change the state hints on the client */
1958
1959     /* figure out where the client should be going */
1960     frame_frame_gravity(self->frame, &x, &y);
1961     client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1962 }
1963
1964 void client_shade(Client *self, gboolean shade)
1965 {
1966     if ((!(self->functions & Func_Shade) && shade) || /* can't shade */
1967         self->shaded == shade) return;     /* already done */
1968
1969     /* when we're iconic, don't change the wmstate */
1970     if (!self->iconic)
1971         self->wmstate = shade ? IconicState : NormalState;
1972     self->shaded = shade;
1973     client_change_state(self);
1974     /* resize the frame to just the titlebar */
1975     frame_adjust_area(self->frame, FALSE, FALSE);
1976 }
1977
1978 void client_close(Client *self)
1979 {
1980     XEvent ce;
1981
1982     if (!(self->functions & Func_Close)) return;
1983
1984     /*
1985       XXX: itd be cool to do timeouts and shit here for killing the client's
1986       process off
1987       like... if the window is around after 5 seconds, then the close button
1988       turns a nice red, and if this function is called again, the client is
1989       explicitly killed.
1990     */
1991
1992     ce.xclient.type = ClientMessage;
1993     ce.xclient.message_type =  prop_atoms.wm_protocols;
1994     ce.xclient.display = ob_display;
1995     ce.xclient.window = self->window;
1996     ce.xclient.format = 32;
1997     ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
1998     ce.xclient.data.l[1] = event_lasttime;
1999     ce.xclient.data.l[2] = 0l;
2000     ce.xclient.data.l[3] = 0l;
2001     ce.xclient.data.l[4] = 0l;
2002     XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2003 }
2004
2005 void client_kill(Client *self)
2006 {
2007     XKillClient(ob_display, self->window);
2008 }
2009
2010 void client_set_desktop(Client *self, guint target, gboolean donthide)
2011 {
2012     guint old;
2013
2014     if (target == self->desktop) return;
2015   
2016     g_message("Setting desktop %u", target);
2017
2018     g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2019
2020     /* remove from the old desktop(s) */
2021     focus_order_remove(self);
2022
2023     old = self->desktop;
2024     self->desktop = target;
2025     PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2026     /* the frame can display the current desktop state */
2027     frame_adjust_state(self->frame);
2028     /* 'move' the window to the new desktop */
2029     if (!donthide)
2030         client_showhide(self);
2031     /* raise if it was not already on the desktop */
2032     if (old != DESKTOP_ALL)
2033         stacking_raise(CLIENT_AS_WINDOW(self));
2034     screen_update_struts();
2035
2036     /* add to the new desktop(s) */
2037     if (config_focus_new)
2038         focus_order_to_top(self);
2039     else
2040         focus_order_to_bottom(self);
2041
2042     dispatch_client(Event_Client_Desktop, self, target, old);
2043 }
2044
2045 Client *client_search_modal_child(Client *self)
2046 {
2047     GSList *it;
2048     Client *ret;
2049   
2050     for (it = self->transients; it != NULL; it = it->next) {
2051         Client *c = it->data;
2052         if ((ret = client_search_modal_child(c))) return ret;
2053         if (c->modal) return c;
2054     }
2055     return NULL;
2056 }
2057
2058 gboolean client_validate(Client *self)
2059 {
2060     XEvent e; 
2061
2062     XSync(ob_display, FALSE); /* get all events on the server */
2063
2064     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2065         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2066         XPutBackEvent(ob_display, &e);
2067         return FALSE;
2068     }
2069
2070     return TRUE;
2071 }
2072
2073 void client_set_wm_state(Client *self, long state)
2074 {
2075     if (state == self->wmstate) return; /* no change */
2076   
2077     switch (state) {
2078     case IconicState:
2079         client_iconify(self, TRUE, TRUE);
2080         break;
2081     case NormalState:
2082         client_iconify(self, FALSE, TRUE);
2083         break;
2084     }
2085 }
2086
2087 void client_set_state(Client *self, Atom action, long data1, long data2)
2088 {
2089     gboolean shaded = self->shaded;
2090     gboolean fullscreen = self->fullscreen;
2091     gboolean max_horz = self->max_horz;
2092     gboolean max_vert = self->max_vert;
2093     int i;
2094
2095     if (!(action == prop_atoms.net_wm_state_add ||
2096           action == prop_atoms.net_wm_state_remove ||
2097           action == prop_atoms.net_wm_state_toggle))
2098         /* an invalid action was passed to the client message, ignore it */
2099         return; 
2100
2101     for (i = 0; i < 2; ++i) {
2102         Atom state = i == 0 ? data1 : data2;
2103     
2104         if (!state) continue;
2105
2106         /* if toggling, then pick whether we're adding or removing */
2107         if (action == prop_atoms.net_wm_state_toggle) {
2108             if (state == prop_atoms.net_wm_state_modal)
2109                 action = self->modal ? prop_atoms.net_wm_state_remove :
2110                     prop_atoms.net_wm_state_add;
2111             else if (state == prop_atoms.net_wm_state_maximized_vert)
2112                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2113                     prop_atoms.net_wm_state_add;
2114             else if (state == prop_atoms.net_wm_state_maximized_horz)
2115                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2116                     prop_atoms.net_wm_state_add;
2117             else if (state == prop_atoms.net_wm_state_shaded)
2118                 action = self->shaded ? prop_atoms.net_wm_state_remove :
2119                     prop_atoms.net_wm_state_add;
2120             else if (state == prop_atoms.net_wm_state_skip_taskbar)
2121                 action = self->skip_taskbar ?
2122                     prop_atoms.net_wm_state_remove :
2123                     prop_atoms.net_wm_state_add;
2124             else if (state == prop_atoms.net_wm_state_skip_pager)
2125                 action = self->skip_pager ?
2126                     prop_atoms.net_wm_state_remove :
2127                     prop_atoms.net_wm_state_add;
2128             else if (state == prop_atoms.net_wm_state_fullscreen)
2129                 action = self->fullscreen ?
2130                     prop_atoms.net_wm_state_remove :
2131                     prop_atoms.net_wm_state_add;
2132             else if (state == prop_atoms.net_wm_state_above)
2133                 action = self->above ? prop_atoms.net_wm_state_remove :
2134                     prop_atoms.net_wm_state_add;
2135             else if (state == prop_atoms.net_wm_state_below)
2136                 action = self->below ? prop_atoms.net_wm_state_remove :
2137                     prop_atoms.net_wm_state_add;
2138         }
2139     
2140         if (action == prop_atoms.net_wm_state_add) {
2141             if (state == prop_atoms.net_wm_state_modal) {
2142                 /* XXX raise here or something? */
2143                 self->modal = TRUE;
2144             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2145                 max_vert = TRUE;
2146             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2147                 max_horz = TRUE;
2148             } else if (state == prop_atoms.net_wm_state_shaded) {
2149                 shaded = TRUE;
2150             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2151                 self->skip_taskbar = TRUE;
2152             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2153                 self->skip_pager = TRUE;
2154             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2155                 fullscreen = TRUE;
2156             } else if (state == prop_atoms.net_wm_state_above) {
2157                 self->above = TRUE;
2158             } else if (state == prop_atoms.net_wm_state_below) {
2159                 self->below = TRUE;
2160             }
2161
2162         } else { /* action == prop_atoms.net_wm_state_remove */
2163             if (state == prop_atoms.net_wm_state_modal) {
2164                 self->modal = FALSE;
2165             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2166                 max_vert = FALSE;
2167             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2168                 max_horz = FALSE;
2169             } else if (state == prop_atoms.net_wm_state_shaded) {
2170                 shaded = FALSE;
2171             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2172                 self->skip_taskbar = FALSE;
2173             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2174                 self->skip_pager = FALSE;
2175             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2176                 fullscreen = FALSE;
2177             } else if (state == prop_atoms.net_wm_state_above) {
2178                 self->above = FALSE;
2179             } else if (state == prop_atoms.net_wm_state_below) {
2180                 self->below = FALSE;
2181             }
2182         }
2183     }
2184     if (max_horz != self->max_horz || max_vert != self->max_vert) {
2185         if (max_horz != self->max_horz && max_vert != self->max_vert) {
2186             /* toggling both */
2187             if (max_horz == max_vert) { /* both going the same way */
2188                 client_maximize(self, max_horz, 0, TRUE);
2189             } else {
2190                 client_maximize(self, max_horz, 1, TRUE);
2191                 client_maximize(self, max_vert, 2, TRUE);
2192             }
2193         } else {
2194             /* toggling one */
2195             if (max_horz != self->max_horz)
2196                 client_maximize(self, max_horz, 1, TRUE);
2197             else
2198                 client_maximize(self, max_vert, 2, TRUE);
2199         }
2200     }
2201     /* change fullscreen state before shading, as it will affect if the window
2202        can shade or not */
2203     if (fullscreen != self->fullscreen)
2204         client_fullscreen(self, fullscreen, TRUE);
2205     if (shaded != self->shaded)
2206         client_shade(self, shaded);
2207     client_calc_layer(self);
2208     client_change_state(self); /* change the hint to relect these changes */
2209 }
2210
2211 Client *client_focus_target(Client *self)
2212 {
2213     Client *child;
2214      
2215     /* if we have a modal child, then focus it, not us */
2216     child = client_search_modal_child(self);
2217     if (child) return child;
2218     return self;
2219 }
2220
2221 gboolean client_focus(Client *self)
2222 {
2223     XEvent ev;
2224
2225     /* choose the correct target */
2226     self = client_focus_target(self);
2227
2228     if (self->desktop != DESKTOP_ALL && self->desktop != screen_desktop) {
2229         /* update the focus lists */
2230         focus_order_to_top(self);
2231         return FALSE;
2232     }
2233
2234     if (!((self->can_focus || self->focus_notify) &&
2235           (self->desktop == screen_desktop ||
2236            self->desktop == DESKTOP_ALL) &&
2237           !self->iconic))
2238         return FALSE;
2239
2240     /* do a check to see if the window has already been unmapped or destroyed
2241        do this intelligently while watching out for unmaps we've generated
2242        (ignore_unmaps > 0) */
2243     if (XCheckTypedWindowEvent(ob_display, self->window,
2244                                DestroyNotify, &ev)) {
2245         XPutBackEvent(ob_display, &ev);
2246         return FALSE;
2247     }
2248     while (XCheckTypedWindowEvent(ob_display, self->window,
2249                                   UnmapNotify, &ev)) {
2250         if (self->ignore_unmaps) {
2251             self->ignore_unmaps--;
2252         } else {
2253             XPutBackEvent(ob_display, &ev);
2254             return FALSE;
2255         }
2256     }
2257
2258     if (self->can_focus)
2259         /* RevertToPointerRoot causes much more headache than RevertToNone, so
2260            I choose to use it always, hopefully to find errors quicker, if any
2261            are left. (I hate X. I hate focus events.) */
2262         XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2263                        event_lasttime);
2264
2265     if (self->focus_notify) {
2266         XEvent ce;
2267         ce.xclient.type = ClientMessage;
2268         ce.xclient.message_type = prop_atoms.wm_protocols;
2269         ce.xclient.display = ob_display;
2270         ce.xclient.window = self->window;
2271         ce.xclient.format = 32;
2272         ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2273         ce.xclient.data.l[1] = event_lasttime;
2274         ce.xclient.data.l[2] = 0l;
2275         ce.xclient.data.l[3] = 0l;
2276         ce.xclient.data.l[4] = 0l;
2277         XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2278     }
2279
2280 #ifdef DEBUG_FOCUS
2281     g_message("%sively focusing %lx at %d", (self->can_focus ? "act" : "pass"),
2282               self->window, (int)
2283               event_lasttime);
2284 #endif
2285
2286     /* Cause the FocusIn to come back to us. Important for desktop switches,
2287        since otherwise we'll have no FocusIn on the queue and send it off to
2288        the focus_backup. */
2289     XSync(ob_display, FALSE);
2290     return TRUE;
2291 }
2292
2293 void client_unfocus(Client *self)
2294 {
2295     g_assert(focus_client == self);
2296 #ifdef DEBUG_FOCUS
2297     g_message("client_unfocus for %lx", self->window);
2298 #endif
2299     focus_fallback(Fallback_Unfocusing);
2300 }
2301
2302 void client_activate(Client *self)
2303 {
2304     if (client_normal(self) && screen_showing_desktop)
2305         screen_show_desktop(FALSE);
2306     if (self->iconic)
2307         client_iconify(self, FALSE, TRUE);
2308     else if (!self->frame->visible)
2309         /* if its not visible for other reasons, then don't mess
2310            with it */
2311         return;
2312     if (self->shaded)
2313         client_shade(self, FALSE);
2314     client_focus(self);
2315     stacking_raise(CLIENT_AS_WINDOW(self));
2316 }
2317
2318 gboolean client_focused(Client *self)
2319 {
2320     return self == focus_client;
2321 }
2322
2323 Icon *client_icon(Client *self, int w, int h)
2324 {
2325     int i;
2326     /* si is the smallest image >= req */
2327     /* li is the largest image < req */
2328     unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2329
2330     if (!self->nicons) return NULL;
2331
2332     for (i = 0; i < self->nicons; ++i) {
2333         size = self->icons[i].width * self->icons[i].height;
2334         if (size < smallest && size >= (unsigned)(w * h)) {
2335             smallest = size;
2336             si = i;
2337         }
2338         if (size > largest && size <= (unsigned)(w * h)) {
2339             largest = size;
2340             li = i;
2341         }
2342     }
2343     if (largest == 0) /* didnt find one smaller than the requested size */
2344         return &self->icons[si];
2345     return &self->icons[li];
2346 }
2347
2348 /* this be mostly ripped from fvwm */
2349 Client *client_find_directional(Client *c, Direction dir) 
2350 {
2351     int my_cx, my_cy, his_cx, his_cy;
2352     int offset = 0;
2353     int distance = 0;
2354     int score, best_score;
2355     Client *best_client, *cur;
2356     GList *it;
2357
2358     if(!client_list)
2359         return NULL;
2360
2361     /* first, find the centre coords of the currently focused window */
2362     my_cx = c->frame->area.x + c->frame->area.width / 2;
2363     my_cy = c->frame->area.y + c->frame->area.height / 2;
2364
2365     best_score = -1;
2366     best_client = NULL;
2367
2368     for(it = g_list_first(client_list); it; it = it->next) {
2369         cur = it->data;
2370
2371         /* the currently selected window isn't interesting */
2372         if(cur == c)
2373             continue;
2374         if (!client_normal(cur))
2375             continue;
2376         if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2377             continue;
2378         if(cur->iconic)
2379             continue;
2380         if(client_focus_target(cur) == cur &&
2381            !(cur->can_focus || cur->focus_notify))
2382             continue;
2383
2384         /* find the centre coords of this window, from the
2385          * currently focused window's point of view */
2386         his_cx = (cur->frame->area.x - my_cx)
2387             + cur->frame->area.width / 2;
2388         his_cy = (cur->frame->area.y - my_cy)
2389             + cur->frame->area.height / 2;
2390
2391         if(dir > 3) { 
2392             int tx;
2393             /* Rotate the diagonals 45 degrees counterclockwise.
2394              * To do this, multiply the matrix /+h +h\ with the
2395              * vector (x y).                   \-h +h/
2396              * h = sqrt(0.5). We can set h := 1 since absolute
2397              * distance doesn't matter here. */
2398             tx = his_cx + his_cy;
2399             his_cy = -his_cx + his_cy;
2400             his_cx = tx;
2401         }
2402
2403         switch(dir) {
2404         case Direction_North :
2405         case Direction_South :
2406         case Direction_NorthEast :
2407         case Direction_SouthWest :
2408             offset = (his_cx < 0) ? -his_cx : his_cx;
2409             distance = (dir == Direction_North || dir == Direction_NorthEast) ?
2410                 -his_cy : his_cy;
2411             break;
2412         case Direction_East :
2413         case Direction_West :
2414         case Direction_SouthEast :
2415         case Direction_NorthWest :
2416             offset = (his_cy < 0) ? -his_cy : his_cy;
2417             distance = (dir == Direction_West || dir == Direction_NorthWest) ?
2418                 -his_cx : his_cx;
2419             break;
2420         }
2421
2422         /* the target must be in the requested direction */
2423         if(distance <= 0)
2424             continue;
2425
2426         /* Calculate score for this window.  The smaller the better. */
2427         score = distance + offset;
2428
2429         /* windows more than 45 degrees off the direction are
2430          * heavily penalized and will only be chosen if nothing
2431          * else within a million pixels */
2432         if(offset > distance)
2433             score += 1000000;
2434
2435         if(best_score == -1 || score < best_score)
2436             best_client = cur,
2437                 best_score = score;
2438     }
2439
2440     return best_client;
2441 }