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