save the dock's class/name for future evil purposes!
[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
18 #include <glib.h>
19 #include <X11/Xutil.h>
20
21 /*! The event mask to grab on client windows */
22 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
23                           StructureNotifyMask)
24
25 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
26                                 ButtonMotionMask)
27
28 GList      *client_list      = NULL;
29
30 static void client_get_all(Client *self);
31 static void client_toggle_border(Client *self, gboolean show);
32 static void client_get_area(Client *self);
33 static void client_get_desktop(Client *self);
34 static void client_get_state(Client *self);
35 static void client_get_shaped(Client *self);
36 static void client_get_mwm_hints(Client *self);
37 static void client_get_gravity(Client *self);
38 static void client_showhide(Client *self);
39 static void client_change_allowed_actions(Client *self);
40 static void client_change_state(Client *self);
41 static void client_apply_startup_state(Client *self);
42
43 void client_startup()
44 {
45     client_set_list();
46 }
47
48 void client_shutdown()
49 {
50 }
51
52 void client_set_list()
53 {
54     Window *windows, *win_it;
55     GList *it;
56     guint size = g_list_length(client_list);
57
58     /* create an array of the window ids */
59     if (size > 0) {
60         windows = g_new(Window, size);
61         win_it = windows;
62         for (it = client_list; it != NULL; it = it->next, ++win_it)
63             *win_it = ((Client*)it->data)->window;
64     } else
65         windows = NULL;
66
67     PROP_SETA32(ob_root, net_client_list, window, (guint32*)windows, size);
68
69     if (windows)
70         g_free(windows);
71
72     stacking_set_list();
73 }
74
75 /*
76 void client_foreach_transient(Client *self, ClientForeachFunc func, void *data)
77 {
78     GSList *it;
79
80     for (it = self->transients; it; it = it->next) {
81         if (!func(it->data, data)) return;
82         client_foreach_transient(it->data, func, data);
83     }
84 }
85
86 void client_foreach_ancestor(Client *self, ClientForeachFunc func, void *data)
87 {
88     if (self->transient_for) {
89         if (self->transient_for != TRAN_GROUP) {
90             if (!func(self->transient_for, data)) return;
91             client_foreach_ancestor(self->transient_for, func, data);
92         } else {
93             GSList *it;
94
95             for (it = self->group->members; it; it = it->next)
96                 if (it->data != self &&
97                     ((Client*)it->data)->transient_for != TRAN_GROUP) {
98                     if (!func(it->data, data)) return;
99                     client_foreach_ancestor(it->data, func, data);
100                 }
101         }
102     }
103 }
104 */
105
106 void client_manage_all()
107 {
108     unsigned int i, j, nchild;
109     Window w, *children;
110     XWMHints *wmhints;
111     XWindowAttributes attrib;
112
113     XQueryTree(ob_display, ob_root, &w, &w, &children, &nchild);
114
115     /* remove all icon windows from the list */
116     for (i = 0; i < nchild; i++) {
117         if (children[i] == None) continue;
118         wmhints = XGetWMHints(ob_display, children[i]);
119         if (wmhints) {
120             if ((wmhints->flags & IconWindowHint) &&
121                 (wmhints->icon_window != children[i]))
122                 for (j = 0; j < nchild; j++)
123                     if (children[j] == wmhints->icon_window) {
124                         children[j] = None;
125                         break;
126                     }
127             XFree(wmhints);
128         }
129     }
130
131     for (i = 0; i < nchild; ++i) {
132         if (children[i] == None)
133             continue;
134         if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
135             if (attrib.override_redirect) continue;
136
137             if (attrib.map_state != IsUnmapped)
138                 client_manage(children[i]);
139         }
140     }
141     XFree(children);
142
143     /* stack them as they were on startup!
144        why with stacking_lower? Why, because then windows who aren't in the
145        stacking list are on the top where you can see them instead of buried
146        at the bottom! */
147     for (i = startup_stack_size; i > 0; --i) {
148         ObWindow *obw;
149
150         w = startup_stack_order[i-1];
151         obw = g_hash_table_lookup(window_map, &w);
152         if (obw) {
153             g_assert(WINDOW_IS_CLIENT(obw));
154             stacking_lower(CLIENT_AS_WINDOW(obw));
155         }
156     }
157     g_free(startup_stack_order);
158     startup_stack_order = NULL;
159     startup_stack_size = 0;
160
161     if (config_focus_new) {
162         ObWindow *active;
163
164         active = g_hash_table_lookup(window_map, &startup_active);
165         if (active) {
166             g_assert(WINDOW_IS_CLIENT(active));
167             if (!client_focus(WINDOW_AS_CLIENT(active)))
168                 focus_fallback(Fallback_NoFocus);
169         } else
170             focus_fallback(Fallback_NoFocus);
171     }
172 }
173
174 void client_manage(Window window)
175 {
176     Client *self;
177     XEvent e;
178     XWindowAttributes attrib;
179     XSetWindowAttributes attrib_set;
180     XWMHints *wmhint;
181     gboolean activate = FALSE;
182
183     grab_server(TRUE);
184
185     /* check if it has already been unmapped by the time we started mapping
186        the grab does a sync so we don't have to here */
187     if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
188         XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
189         XPutBackEvent(ob_display, &e);
190
191         grab_server(FALSE);
192         return; /* don't manage it */
193     }
194
195     /* make sure it isn't an override-redirect window */
196     if (!XGetWindowAttributes(ob_display, window, &attrib) ||
197         attrib.override_redirect) {
198         grab_server(FALSE);
199         return; /* don't manage it */
200     }
201   
202     /* is the window a docking app */
203     if ((wmhint = XGetWMHints(ob_display, window))) {
204         if ((wmhint->flags & StateHint) &&
205             wmhint->initial_state == WithdrawnState) {
206             dock_add(window, wmhint);
207             grab_server(FALSE);
208             XFree(wmhint);
209             return;
210         }
211         XFree(wmhint);
212     }
213
214     g_message("Managing window: %lx", window);
215
216     /* choose the events we want to receive on the CLIENT window */
217     attrib_set.event_mask = CLIENT_EVENTMASK;
218     attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
219     XChangeWindowAttributes(ob_display, window,
220                             CWEventMask|CWDontPropagate, &attrib_set);
221
222
223     /* create the Client struct, and populate it from the hints on the
224        window */
225     self = g_new(Client, 1);
226     self->obwin.type = Window_Client;
227     self->window = window;
228     client_get_all(self);
229
230     /* remove the client's border (and adjust re gravity) */
231     client_toggle_border(self, FALSE);
232      
233     /* specify that if we exit, the window should not be destroyed and should
234        be reparented back to root automatically */
235     XChangeSaveSet(ob_display, window, SetModeInsert);
236
237     /* create the decoration frame for the client window */
238     self->frame = frame_new();
239
240     frame_grab_client(self->frame, self);
241
242     client_apply_startup_state(self);
243
244     grab_server(FALSE);
245
246     /* add to client list/map */
247     client_list = g_list_append(client_list, self);
248     g_hash_table_insert(window_map, &self->window, self);
249
250     /* update the focus lists */
251     focus_order_add_new(self);
252
253     /* focus the new window? */
254     if (ob_state != State_Starting && config_focus_new) {
255         gboolean group_foc = FALSE;
256         
257         if (self->group) {
258             GSList *it;
259
260             for (it = self->group->members; it; it = it->next)
261                 if (client_focused(it->data)) {
262                     group_foc = TRUE;
263                     break;
264                 }
265         }
266         /* note the check against Type_Normal/Dialog, not client_normal(self),
267            which would also include other types. in this case we want more
268            strict rules for focus */
269         if (((self->type == Type_Normal ||
270               (self->type == Type_Dialog &&
271                (group_foc ||
272                 (!self->transient_for && (!self->group ||
273                                           !self->group->members->next)))))) ||
274             client_search_focus_tree_full(self) ||
275             !focus_client ||
276             !client_normal(focus_client)) {
277             /* activate the window */
278             stacking_add(CLIENT_AS_WINDOW(self));
279             activate = TRUE;
280         } else {
281             /* try to not get in the way */
282             stacking_add_nonintrusive(CLIENT_AS_WINDOW(self));
283         }
284     } else {
285         stacking_add(CLIENT_AS_WINDOW(self));
286     }
287
288     screen_update_struts();
289
290     /* make sure the window is visible */
291     client_move_onscreen(self);
292
293     dispatch_client(Event_Client_New, self, 0, 0);
294
295     client_showhide(self);
296
297     if (activate) client_activate(self);
298
299     /* update the list hints */
300     client_set_list();
301
302     dispatch_client(Event_Client_Mapped, self, 0, 0);
303
304     g_message("Managed window 0x%lx (%s)", window, self->name);
305 }
306
307 void client_unmanage_all()
308 {
309     while (client_list != NULL)
310         client_unmanage(client_list->data);
311 }
312
313 void client_unmanage(Client *self)
314 {
315     int j;
316     GSList *it;
317
318     g_message("Unmanaging window: %lx (%s)", self->window, self->name);
319
320     dispatch_client(Event_Client_Destroy, self, 0, 0);
321     g_assert(self != NULL);
322
323     /* remove the window from our save set */
324     XChangeSaveSet(ob_display, self->window, SetModeDelete);
325
326     /* we dont want events no more */
327     XSelectInput(ob_display, self->window, NoEventMask);
328
329     frame_hide(self->frame);
330
331     client_list = g_list_remove(client_list, self);
332     stacking_remove(self);
333     g_hash_table_remove(window_map, &self->window);
334
335     /* update the focus lists */
336     focus_order_remove(self);
337
338     /* once the client is out of the list, update the struts to remove it's
339        influence */
340     screen_update_struts();
341
342     /* tell our parent(s) that we're gone */
343     if (self->transient_for == TRAN_GROUP) { /* transient of group */
344         GSList *it;
345
346         for (it = self->group->members; it; it = it->next)
347             if (it->data != self)
348                 ((Client*)it->data)->transients =
349                     g_slist_remove(((Client*)it->data)->transients, self);
350     } else if (self->transient_for) {        /* transient of window */
351         self->transient_for->transients =
352             g_slist_remove(self->transient_for->transients, self);
353     }
354
355     /* tell our transients that we're gone */
356     for (it = self->transients; it != NULL; it = it->next) {
357         if (((Client*)it->data)->transient_for != TRAN_GROUP) {
358             ((Client*)it->data)->transient_for = NULL;
359             client_calc_layer(it->data);
360         }
361     }
362
363     if (moveresize_client == self)
364         moveresize_end(TRUE);
365
366     if (focus_client == self) {
367         XEvent e;
368
369         /* focus the last focused window on the desktop, and ignore enter
370            events from the unmap so it doesnt mess with the focus */
371         while (XCheckTypedEvent(ob_display, EnterNotify, &e));
372         client_unfocus(self);
373     }
374
375     /* remove from its group */
376     if (self->group) {
377         group_remove(self->group, self);
378         self->group = NULL;
379     }
380
381     /* dispatch the unmapped event */
382     dispatch_client(Event_Client_Unmapped, self, 0, 0);
383     g_assert(self != NULL);
384
385     /* give the client its border back */
386     client_toggle_border(self, TRUE);
387
388     /* reparent the window out of the frame, and free the frame */
389     frame_release_client(self->frame, self);
390     self->frame = NULL;
391      
392     if (ob_state != State_Exiting) {
393         /* these values should not be persisted across a window
394            unmapping/mapping */
395         prop_erase(self->window, prop_atoms.net_wm_desktop);
396         prop_erase(self->window, prop_atoms.net_wm_state);
397     } else {
398         /* if we're left in an iconic state, the client wont be mapped. this is
399            bad, since we will no longer be managing the window on restart */
400         if (self->iconic)
401             XMapWindow(ob_display, self->window);
402     }
403
404
405     g_message("Unmanaged window 0x%lx", self->window);
406
407     /* free all data allocated in the client struct */
408     g_slist_free(self->transients);
409     for (j = 0; j < self->nicons; ++j)
410         g_free(self->icons[j].data);
411     if (self->nicons > 0)
412         g_free(self->icons);
413     g_free(self->title);
414     g_free(self->icon_title);
415     g_free(self->name);
416     g_free(self->class);
417     g_free(self->role);
418     g_free(self);
419      
420     /* update the list hints */
421     client_set_list();
422 }
423
424 void client_move_onscreen(Client *self)
425 {
426     Rect *a;
427     int x = self->frame->area.x, y = self->frame->area.y;
428
429     a = screen_area(self->desktop);
430     if (x >= a->x + a->width - 1)
431         x = a->x + a->width - self->frame->area.width;
432     if (y >= a->y + a->height - 1)
433         y = a->y + a->height - self->frame->area.height;
434     if (x + self->frame->area.width - 1 < a->x)
435         x = a->x;
436     if (y + self->frame->area.height - 1< a->y)
437         y = a->y;
438
439     frame_frame_gravity(self->frame, &x, &y); /* get where the client
440                                                  should be */
441     client_configure(self , Corner_TopLeft, x, y,
442                      self->area.width, self->area.height,
443                      TRUE, TRUE);
444 }
445
446 static void client_toggle_border(Client *self, gboolean show)
447 {
448     /* adjust our idea of where the client is, based on its border. When the
449        border is removed, the client should now be considered to be in a
450        different position.
451        when re-adding the border to the client, the same operation needs to be
452        reversed. */
453     int oldx = self->area.x, oldy = self->area.y;
454     int x = oldx, y = oldy;
455     switch(self->gravity) {
456     default:
457     case NorthWestGravity:
458     case WestGravity:
459     case SouthWestGravity:
460         break;
461     case NorthEastGravity:
462     case EastGravity:
463     case SouthEastGravity:
464         if (show) x -= self->border_width * 2;
465         else      x += self->border_width * 2;
466         break;
467     case NorthGravity:
468     case SouthGravity:
469     case CenterGravity:
470     case ForgetGravity:
471     case StaticGravity:
472         if (show) x -= self->border_width;
473         else      x += self->border_width;
474         break;
475     }
476     switch(self->gravity) {
477     default:
478     case NorthWestGravity:
479     case NorthGravity:
480     case NorthEastGravity:
481         break;
482     case SouthWestGravity:
483     case SouthGravity:
484     case SouthEastGravity:
485         if (show) y -= self->border_width * 2;
486         else      y += self->border_width * 2;
487         break;
488     case WestGravity:
489     case EastGravity:
490     case CenterGravity:
491     case ForgetGravity:
492     case StaticGravity:
493         if (show) y -= self->border_width;
494         else      y += self->border_width;
495         break;
496     }
497     self->area.x = x;
498     self->area.y = y;
499
500     if (show) {
501         XSetWindowBorderWidth(ob_display, self->window, self->border_width);
502
503         /* move the client so it is back it the right spot _with_ its
504            border! */
505         if (x != oldx || y != oldy)
506             XMoveWindow(ob_display, self->window, x, y);
507     } else
508         XSetWindowBorderWidth(ob_display, self->window, 0);
509 }
510
511
512 static void client_get_all(Client *self)
513 {
514     /* update EVERYTHING!! */
515
516     self->ignore_unmaps = 0;
517   
518     /* defaults */
519     self->frame = NULL;
520     self->title = self->icon_title = NULL;
521     self->title_count = 1;
522     self->name = self->class = self->role = NULL;
523     self->wmstate = NormalState;
524     self->transient = FALSE;
525     self->transients = NULL;
526     self->transient_for = NULL;
527     self->layer = -1;
528     self->urgent = FALSE;
529     self->positioned = FALSE;
530     self->disabled_decorations = 0;
531     self->group = NULL;
532     self->nicons = 0;
533
534     client_get_area(self);
535     client_update_transient_for(self);
536     client_update_wmhints(self);
537     client_get_desktop(self);
538     client_get_state(self);
539     client_get_shaped(self);
540
541     client_get_mwm_hints(self);
542     client_get_type(self);/* this can change the mwmhints for special cases */
543
544     client_update_protocols(self);
545
546     client_get_gravity(self); /* get the attribute gravity */
547     client_update_normal_hints(self); /* this may override the attribute
548                                          gravity */
549
550     /* got the type, the mwmhints, the protocols, and the normal hints
551        (min/max sizes), so we're ready to set up the decorations/functions */
552     client_setup_decor_and_functions(self);
553   
554     client_update_title(self);
555     client_update_class(self);
556     client_update_strut(self);
557     client_update_icons(self);
558     client_update_kwm_icon(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 != TRAN_GROUP) {
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 != TRAN_GROUP))
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 != TRAN_GROUP))
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                     if (it->data != self &&
1127                         ((Client*)it->data)->transient_for == TRAN_GROUP) {
1128                         self->transients = g_slist_remove(self->transients,
1129                                                           it->data);
1130                     }
1131                 group_remove(self->group, self);
1132                 self->group = NULL;
1133             }
1134             if (hints->window_group != None) {
1135                 self->group = group_add(hints->window_group, self);
1136
1137                 /* add other transients of the group that are already
1138                    set up */
1139                 for (it = self->group->members; it; it = it->next)
1140                     if (it->data != self &&
1141                         ((Client*)it->data)->transient_for == TRAN_GROUP)
1142                         self->transients = g_slist_append(self->transients,
1143                                                           it->data);
1144             }
1145
1146             /* because the self->transient flag wont change from this call,
1147                we don't need to update the window's type and such, only its
1148                transient_for, and the transients lists of other windows in
1149                the group may be affected */
1150             client_update_transient_for(self);
1151         }
1152
1153         client_update_kwm_icon(self);
1154         /* try get the kwm icon first, this is a fallback only */
1155         if (self->pixmap_icon == None) {
1156             if (hints->flags & IconPixmapHint) {
1157                 if (self->pixmap_icon == None) {
1158                     self->pixmap_icon = hints->icon_pixmap;
1159                     if (hints->flags & IconMaskHint)
1160                         self->pixmap_icon_mask = hints->icon_mask;
1161                     else
1162                         self->pixmap_icon_mask = None;
1163
1164                     if (self->frame)
1165                         frame_adjust_icon(self->frame);
1166                 }
1167             }
1168         }
1169
1170         XFree(hints);
1171     }
1172
1173     if (ur != self->urgent) {
1174         self->urgent = ur;
1175         g_message("Urgent Hint for 0x%lx: %s", self->window,
1176                   ur ? "ON" : "OFF");
1177         /* fire the urgent callback if we're mapped, otherwise, wait until
1178            after we're mapped */
1179         if (self->frame)
1180             dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1181     }
1182 }
1183
1184 void client_update_title(Client *self)
1185 {
1186     GList *it;
1187     guint32 nums;
1188     guint i;
1189     char *data = NULL;
1190     gboolean read_title;
1191
1192     g_free(self->title);
1193      
1194     /* try netwm */
1195     if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1196         /* try old x stuff */
1197         if (!PROP_GETS(self->window, wm_name, locale, &data))
1198             data = g_strdup("Unnamed Window");
1199
1200     /* look for duplicates and append a number */
1201     nums = 0;
1202     for (it = client_list; it; it = it->next)
1203         if (it->data != self) {
1204             Client *c = it->data;
1205             if (0 == strncmp(c->title, data, strlen(data)))
1206                 nums |= 1 << c->title_count;
1207         }
1208     /* find first free number */
1209     for (i = 1; i <= 32; ++i)
1210         if (!(nums & (1 << i))) {
1211             if (self->title_count == 1 || i == 1)
1212                 self->title_count = i;
1213             break;
1214         }
1215     /* dont display the number for the first window */
1216     if (self->title_count > 1) {
1217         char *vdata, *ndata;
1218         ndata = g_strdup_printf(" - [%u]", self->title_count);
1219         vdata = g_strconcat(data, ndata, NULL);
1220         g_free(ndata);
1221         g_free(data);
1222         data = vdata;
1223     }
1224
1225     PROP_SETS(self->window, net_wm_visible_name, data);
1226
1227     self->title = data;
1228
1229     if (self->frame)
1230         frame_adjust_title(self->frame);
1231
1232     /* update the icon title */
1233     data = NULL;
1234     g_free(self->icon_title);
1235
1236     read_title = TRUE;
1237     /* try netwm */
1238     if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1239         /* try old x stuff */
1240         if (!PROP_GETS(self->window, wm_icon_name, locale, &data)) {
1241             data = g_strdup(self->title);
1242             read_title = FALSE;
1243         }
1244
1245     /* append the title count, dont display the number for the first window */
1246     if (read_title && self->title_count > 1) {
1247         char *vdata, *ndata;
1248         ndata = g_strdup_printf(" - [%u]", self->title_count);
1249         vdata = g_strconcat(data, ndata, NULL);
1250         g_free(ndata);
1251         g_free(data);
1252         data = vdata;
1253     }
1254
1255     PROP_SETS(self->window, net_wm_visible_icon_name, data);
1256
1257     self->icon_title = data;
1258 }
1259
1260 void client_update_class(Client *self)
1261 {
1262     char **data;
1263     char *s;
1264
1265     if (self->name) g_free(self->name);
1266     if (self->class) g_free(self->class);
1267     if (self->role) g_free(self->role);
1268
1269     self->name = self->class = self->role = NULL;
1270
1271     if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1272         if (data[0]) {
1273             self->name = g_strdup(data[0]);
1274             if (data[1])
1275                 self->class = g_strdup(data[1]);
1276         }
1277         g_strfreev(data);     
1278     }
1279
1280     if (PROP_GETS(self->window, wm_window_role, locale, &s))
1281         self->role = g_strdup(s);
1282
1283     if (self->name == NULL) self->name = g_strdup("");
1284     if (self->class == NULL) self->class = g_strdup("");
1285     if (self->role == NULL) self->role = g_strdup("");
1286 }
1287
1288 void client_update_strut(Client *self)
1289 {
1290     guint num;
1291     guint32 *data;
1292
1293     if (!PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1294         STRUT_SET(self->strut, 0, 0, 0, 0);
1295     } else {
1296         if (num == 4)
1297             STRUT_SET(self->strut, data[0], data[2], data[1], data[3]);
1298         else
1299             STRUT_SET(self->strut, 0, 0, 0, 0);
1300         g_free(data);
1301     }
1302
1303     /* updating here is pointless while we're being mapped cuz we're not in
1304        the client list yet */
1305     if (self->frame)
1306         screen_update_struts();
1307 }
1308
1309 void client_update_icons(Client *self)
1310 {
1311     guint num;
1312     guint32 *data;
1313     guint w, h, i;
1314     int j;
1315
1316     for (j = 0; j < self->nicons; ++j)
1317         g_free(self->icons[j].data);
1318     if (self->nicons > 0)
1319         g_free(self->icons);
1320     self->nicons = 0;
1321
1322     if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1323         /* figure out how many valid icons are in here */
1324         i = 0;
1325         while (num - i > 2) {
1326             w = data[i++];
1327             h = data[i++];
1328             i += w * h;
1329             if (i > num) break;
1330             ++self->nicons;
1331         }
1332
1333         self->icons = g_new(Icon, self->nicons);
1334     
1335         /* store the icons */
1336         i = 0;
1337         for (j = 0; j < self->nicons; ++j) {
1338             w = self->icons[j].width = data[i++];
1339             h = self->icons[j].height = data[i++];
1340             self->icons[j].data =
1341                 g_memdup(&data[i], w * h * sizeof(gulong));
1342             i += w * h;
1343             g_assert(i <= num);
1344         }
1345
1346         g_free(data);
1347     }
1348
1349     if (self->frame)
1350         frame_adjust_icon(self->frame);
1351 }
1352
1353 void client_update_kwm_icon(Client *self)
1354 {
1355     guint num;
1356     guint32 *data;
1357
1358     if (!PROP_GETA32(self->window, kwm_win_icon, kwm_win_icon, &data, &num)) {
1359         self->pixmap_icon = self->pixmap_icon_mask = None;
1360     } else {
1361         if (num == 2) {
1362             self->pixmap_icon = data[0];
1363             self->pixmap_icon_mask = data[1];
1364         } else
1365             self->pixmap_icon = self->pixmap_icon_mask = None;
1366         g_free(data);
1367     }
1368     if (self->frame)
1369         frame_adjust_icon(self->frame);
1370 }
1371
1372 static void client_change_state(Client *self)
1373 {
1374     guint32 state[2];
1375     guint32 netstate[10];
1376     guint num;
1377
1378     state[0] = self->wmstate;
1379     state[1] = None;
1380     PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1381
1382     num = 0;
1383     if (self->modal)
1384         netstate[num++] = prop_atoms.net_wm_state_modal;
1385     if (self->shaded)
1386         netstate[num++] = prop_atoms.net_wm_state_shaded;
1387     if (self->iconic)
1388         netstate[num++] = prop_atoms.net_wm_state_hidden;
1389     if (self->skip_taskbar)
1390         netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1391     if (self->skip_pager)
1392         netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1393     if (self->fullscreen)
1394         netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1395     if (self->max_vert)
1396         netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1397     if (self->max_horz)
1398         netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1399     if (self->above)
1400         netstate[num++] = prop_atoms.net_wm_state_above;
1401     if (self->below)
1402         netstate[num++] = prop_atoms.net_wm_state_below;
1403     PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1404
1405     client_calc_layer(self);
1406
1407     if (self->frame)
1408         frame_adjust_state(self->frame);
1409 }
1410
1411 Client *client_search_focus_tree(Client *self)
1412 {
1413     GSList *it;
1414     Client *ret;
1415
1416     for (it = self->transients; it != NULL; it = it->next) {
1417         if (client_focused(it->data)) return it->data;
1418         if ((ret = client_search_focus_tree(it->data))) return ret;
1419     }
1420     return NULL;
1421 }
1422
1423 Client *client_search_focus_tree_full(Client *self)
1424 {
1425     if (self->transient_for) {
1426         if (self->transient_for != TRAN_GROUP) {
1427             return client_search_focus_tree_full(self->transient_for);
1428         } else {
1429             GSList *it;
1430         
1431             for (it = self->group->members; it; it = it->next)
1432                 if (((Client*)it->data)->transient_for != TRAN_GROUP) {
1433                     Client *c;
1434                     if ((c = client_search_focus_tree_full(it->data)))
1435                         return c;
1436                 }
1437             return NULL;
1438         }
1439     } else {
1440         /* this function checks the whole tree, the client_search_focus_tree
1441            does not, so we need to check this window */
1442         if (client_focused(self))
1443             return self;
1444         return client_search_focus_tree(self);
1445     }
1446 }
1447
1448 static StackLayer calc_layer(Client *self)
1449 {
1450     StackLayer l;
1451
1452     if (self->fullscreen) l = Layer_Fullscreen;
1453     else if (self->type == Type_Desktop) l = Layer_Desktop;
1454     else if (self->type == Type_Dock) {
1455         if (!self->below) l = Layer_Top;
1456         else l = Layer_Normal;
1457     }
1458     else if (self->above) l = Layer_Above;
1459     else if (self->below) l = Layer_Below;
1460     else l = Layer_Normal;
1461
1462     return l;
1463 }
1464
1465 static void calc_recursive(Client *self, Client *orig, StackLayer l,
1466                            gboolean raised)
1467 {
1468     StackLayer old, own;
1469     GSList *it;
1470
1471     old = self->layer;
1472     own = calc_layer(self);
1473     self->layer = l > own ? l : own;
1474
1475     for (it = self->transients; it; it = it->next)
1476         calc_recursive(it->data, orig, l, raised ? raised : l != old);
1477
1478     if (!raised && l != old)
1479         if (orig->frame) /* only restack if the original window is managed */
1480             stacking_raise(CLIENT_AS_WINDOW(self));
1481 }
1482
1483 void client_calc_layer(Client *self)
1484 {
1485     StackLayer l;
1486     Client *orig;
1487
1488     orig = self;
1489
1490     /* transients take on the layer of their parents */
1491     if (self->transient_for) {
1492         if (self->transient_for != TRAN_GROUP) {
1493             self = self->transient_for;
1494         } else {
1495             GSList *it;
1496
1497             for (it = self->group->members; it; it = it->next)
1498                 if (it->data != self &&
1499                     ((Client*)it->data)->transient_for != TRAN_GROUP) {
1500                     self = it->data;
1501                     break;
1502                 }
1503         }
1504     }
1505
1506     l = calc_layer(self);
1507
1508     calc_recursive(self, orig, l, FALSE);
1509 }
1510
1511 gboolean client_should_show(Client *self)
1512 {
1513     if (self->iconic) return FALSE;
1514     else if (!(self->desktop == screen_desktop ||
1515                self->desktop == DESKTOP_ALL)) return FALSE;
1516     else if (client_normal(self) && screen_showing_desktop) return FALSE;
1517     
1518     return TRUE;
1519 }
1520
1521 static void client_showhide(Client *self)
1522 {
1523
1524     if (client_should_show(self))
1525         frame_show(self->frame);
1526     else
1527         frame_hide(self->frame);
1528 }
1529
1530 gboolean client_normal(Client *self) {
1531     return ! (self->type == Type_Desktop || self->type == Type_Dock ||
1532               self->type == Type_Splash);
1533 }
1534
1535 static void client_apply_startup_state(Client *self)
1536 {
1537     /* these are in a carefully crafted order.. */
1538
1539     if (self->iconic) {
1540         self->iconic = FALSE;
1541         client_iconify(self, TRUE, FALSE);
1542     }
1543     if (self->fullscreen) {
1544         self->fullscreen = FALSE;
1545         client_fullscreen(self, TRUE, FALSE);
1546     }
1547     if (self->shaded) {
1548         self->shaded = FALSE;
1549         client_shade(self, TRUE);
1550     }
1551     if (self->urgent)
1552         dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1553   
1554     if (self->max_vert && self->max_horz) {
1555         self->max_vert = self->max_horz = FALSE;
1556         client_maximize(self, TRUE, 0, FALSE);
1557     } else if (self->max_vert) {
1558         self->max_vert = FALSE;
1559         client_maximize(self, TRUE, 2, FALSE);
1560     } else if (self->max_horz) {
1561         self->max_horz = FALSE;
1562         client_maximize(self, TRUE, 1, FALSE);
1563     }
1564
1565     /* nothing to do for the other states:
1566        skip_taskbar
1567        skip_pager
1568        modal
1569        above
1570        below
1571     */
1572 }
1573
1574 void client_configure(Client *self, Corner anchor, int x, int y, int w, int h,
1575                       gboolean user, gboolean final)
1576 {
1577     gboolean moved = FALSE, resized = FALSE;
1578
1579     /* gets the frame's position */
1580     frame_client_gravity(self->frame, &x, &y);
1581
1582     /* these positions are frame positions, not client positions */
1583
1584     /* set the size and position if fullscreen */
1585     if (self->fullscreen) {
1586         x = 0;
1587         y = 0;
1588         w = screen_physical_size.width;
1589         h = screen_physical_size.height;
1590         user = FALSE; /* ignore that increment etc shit when in fullscreen */
1591     } else {
1592         /* set the size and position if maximized */
1593         if (self->max_horz) {
1594             x = screen_area(self->desktop)->x - self->frame->size.left;
1595             w = screen_area(self->desktop)->width;
1596         }
1597         if (self->max_vert) {
1598             y = screen_area(self->desktop)->y;
1599             h = screen_area(self->desktop)->height -
1600                 self->frame->size.top - self->frame->size.bottom;
1601         }
1602     }
1603
1604     /* gets the client's position */
1605     frame_frame_gravity(self->frame, &x, &y);
1606
1607     /* these override the above states! if you cant move you can't move! */
1608     if (user) {
1609         if (!(self->functions & Func_Move)) {
1610             x = self->area.x;
1611             y = self->area.y;
1612         }
1613         if (!(self->functions & Func_Resize)) {
1614             w = self->area.width;
1615             h = self->area.height;
1616         }
1617     }
1618
1619     if (!(w == self->area.width && h == self->area.height)) {
1620         w -= self->base_size.width;
1621         h -= self->base_size.height;
1622
1623         if (user) {
1624             /* for interactive resizing. have to move half an increment in each
1625                direction. */
1626
1627             /* how far we are towards the next size inc */
1628             int mw = w % self->size_inc.width; 
1629             int mh = h % self->size_inc.height;
1630             /* amount to add */
1631             int aw = self->size_inc.width / 2;
1632             int ah = self->size_inc.height / 2;
1633             /* don't let us move into a new size increment */
1634             if (mw + aw >= self->size_inc.width)
1635                 aw = self->size_inc.width - mw - 1;
1636             if (mh + ah >= self->size_inc.height)
1637                 ah = self->size_inc.height - mh - 1;
1638             w += aw;
1639             h += ah;
1640     
1641             /* if this is a user-requested resize, then check against min/max
1642                sizes and aspect ratios */
1643
1644             /* smaller than min size or bigger than max size? */
1645             if (w > self->max_size.width) w = self->max_size.width;
1646             if (w < self->min_size.width) w = self->min_size.width;
1647             if (h > self->max_size.height) h = self->max_size.height;
1648             if (h < self->min_size.height) h = self->min_size.height;
1649
1650             /* adjust the height ot match the width for the aspect ratios */
1651             if (self->min_ratio)
1652                 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1653             if (self->max_ratio)
1654                 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1655         }
1656
1657         /* keep to the increments */
1658         w /= self->size_inc.width;
1659         h /= self->size_inc.height;
1660
1661         /* you cannot resize to nothing */
1662         if (w < 1) w = 1;
1663         if (h < 1) h = 1;
1664   
1665         /* store the logical size */
1666         SIZE_SET(self->logical_size, w, h);
1667
1668         w *= self->size_inc.width;
1669         h *= self->size_inc.height;
1670
1671         w += self->base_size.width;
1672         h += self->base_size.height;
1673     }
1674
1675     switch (anchor) {
1676     case Corner_TopLeft:
1677         break;
1678     case Corner_TopRight:
1679         x -= w - self->area.width;
1680         break;
1681     case Corner_BottomLeft:
1682         y -= h - self->area.height;
1683         break;
1684     case Corner_BottomRight:
1685         x -= w - self->area.width;
1686         y -= h - self->area.height;
1687         break;
1688     }
1689
1690     moved = x != self->area.x || y != self->area.y;
1691     resized = w != self->area.width || h != self->area.height;
1692
1693     RECT_SET(self->area, x, y, w, h);
1694
1695     if (resized)
1696         XResizeWindow(ob_display, self->window, w, h);
1697
1698     /* move/resize the frame to match the request */
1699     if (self->frame) {
1700         if (moved || resized)
1701             frame_adjust_area(self->frame, moved, resized);
1702
1703         if (!user || final) {
1704             XEvent event;
1705             event.type = ConfigureNotify;
1706             event.xconfigure.display = ob_display;
1707             event.xconfigure.event = self->window;
1708             event.xconfigure.window = self->window;
1709     
1710             /* root window real coords */
1711             event.xconfigure.x = self->frame->area.x + self->frame->size.left;
1712             event.xconfigure.y = self->frame->area.y + self->frame->size.top;
1713     
1714             event.xconfigure.width = w;
1715             event.xconfigure.height = h;
1716             event.xconfigure.border_width = 0;
1717             event.xconfigure.above = self->frame->plate;
1718             event.xconfigure.override_redirect = FALSE;
1719             XSendEvent(event.xconfigure.display, event.xconfigure.window,
1720                        FALSE, StructureNotifyMask, &event);
1721         }
1722     }
1723 }
1724
1725 void client_fullscreen(Client *self, gboolean fs, gboolean savearea)
1726 {
1727     int x, y, w, h;
1728
1729     if (!(self->functions & Func_Fullscreen) || /* can't */
1730         self->fullscreen == fs) return;         /* already done */
1731
1732     self->fullscreen = fs;
1733     client_change_state(self); /* change the state hints on the client,
1734                                   and adjust out layer/stacking */
1735
1736     if (fs) {
1737         if (savearea) {
1738             guint32 dimensions[4];
1739             dimensions[0] = self->area.x;
1740             dimensions[1] = self->area.y;
1741             dimensions[2] = self->area.width;
1742             dimensions[3] = self->area.height;
1743   
1744             PROP_SETA32(self->window, openbox_premax, cardinal,
1745                         dimensions, 4);
1746         }
1747
1748         /* these are not actually used cuz client_configure will set them
1749            as appropriate when the window is fullscreened */
1750         x = y = w = h = 0;
1751     } else {
1752         guint num;
1753         gint32 *dimensions;
1754
1755         /* pick some fallbacks... */
1756         x = screen_area(self->desktop)->x +
1757             screen_area(self->desktop)->width / 4;
1758         y = screen_area(self->desktop)->y +
1759             screen_area(self->desktop)->height / 4;
1760         w = screen_area(self->desktop)->width / 2;
1761         h = screen_area(self->desktop)->height / 2;
1762
1763         if (PROP_GETA32(self->window, openbox_premax, cardinal,
1764                         (guint32**)&dimensions, &num)) {
1765             if (num == 4) {
1766                 x = dimensions[0];
1767                 y = dimensions[1];
1768                 w = dimensions[2];
1769                 h = dimensions[3];
1770             }
1771             g_free(dimensions);
1772         }
1773     }
1774
1775     client_setup_decor_and_functions(self);
1776
1777     client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1778
1779     /* try focus us when we go into fullscreen mode */
1780     client_focus(self);
1781 }
1782
1783 void client_iconify(Client *self, gboolean iconic, gboolean curdesk)
1784 {
1785     GSList *it;
1786
1787     /* move up the transient chain as far as possible first */
1788     if (self->transient_for) {
1789         if (self->transient_for != TRAN_GROUP) {
1790             if (self->transient_for->iconic != iconic) {
1791                 client_iconify(self->transient_for, iconic, curdesk);
1792                 return;
1793             }
1794         } else {
1795             GSList *it;
1796
1797             /* the check for TRAN_GROUP is to prevent an infinate loop with
1798                2 transients of the same group at the head of the group's
1799                members list */
1800             for (it = self->group->members; it; it = it->next) {
1801                 Client *c = it->data;
1802                 if (c != self && c->iconic != iconic &&
1803                     c->transient_for != TRAN_GROUP) {
1804                     client_iconify(it->data, iconic, curdesk);
1805                     break;
1806                 }
1807             }
1808             if (it != NULL) return;
1809         }
1810     }
1811
1812     if (self->iconic == iconic) return; /* nothing to do */
1813
1814     g_message("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
1815               self->window);
1816
1817     self->iconic = iconic;
1818
1819     if (iconic) {
1820         self->wmstate = IconicState;
1821         self->ignore_unmaps++;
1822         /* we unmap the client itself so that we can get MapRequest events,
1823            and because the ICCCM tells us to! */
1824         XUnmapWindow(ob_display, self->window);
1825
1826         /* update the focus lists.. iconic windows go to the bottom of the
1827          list, put the new iconic window at the 'top of the bottom'. */
1828         focus_order_to_top(self);
1829     } else {
1830         if (curdesk)
1831             client_set_desktop(self, screen_desktop, FALSE);
1832         self->wmstate = self->shaded ? IconicState : NormalState;
1833         XMapWindow(ob_display, self->window);
1834
1835         /* this puts it after the current focused window */
1836         focus_order_remove(self);
1837         focus_order_add_new(self);
1838     }
1839     client_change_state(self);
1840     client_showhide(self);
1841     screen_update_struts();
1842
1843     dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
1844                     self, 0, 0);
1845
1846     /* iconify all transients */
1847     for (it = self->transients; it != NULL; it = it->next)
1848         if (it->data != self) client_iconify(it->data, iconic, curdesk);
1849 }
1850
1851 void client_maximize(Client *self, gboolean max, int dir, gboolean savearea)
1852 {
1853     int x, y, w, h;
1854      
1855     g_assert(dir == 0 || dir == 1 || dir == 2);
1856     if (!(self->functions & Func_Maximize)) return; /* can't */
1857
1858     /* check if already done */
1859     if (max) {
1860         if (dir == 0 && self->max_horz && self->max_vert) return;
1861         if (dir == 1 && self->max_horz) return;
1862         if (dir == 2 && self->max_vert) return;
1863     } else {
1864         if (dir == 0 && !self->max_horz && !self->max_vert) return;
1865         if (dir == 1 && !self->max_horz) return;
1866         if (dir == 2 && !self->max_vert) return;
1867     }
1868
1869     /* work with the frame's coords */
1870     x = self->frame->area.x;
1871     y = self->frame->area.y;
1872     w = self->area.width;
1873     h = self->area.height;
1874
1875     if (max) {
1876         if (savearea) {
1877             gint32 dimensions[4];
1878             gint32 *readdim;
1879             guint num;
1880
1881             dimensions[0] = x;
1882             dimensions[1] = y;
1883             dimensions[2] = w;
1884             dimensions[3] = h;
1885
1886             /* get the property off the window and use it for the dimensions
1887                we are already maxed on */
1888             if (PROP_GETA32(self->window, openbox_premax, cardinal,
1889                             (guint32**)&readdim, &num)) {
1890                 if (num == 4) {
1891                     if (self->max_horz) {
1892                         dimensions[0] = readdim[0];
1893                         dimensions[2] = readdim[2];
1894                     }
1895                     if (self->max_vert) {
1896                         dimensions[1] = readdim[1];
1897                         dimensions[3] = readdim[3];
1898                     }
1899                 }
1900                 g_free(readdim);
1901             }
1902
1903             PROP_SETA32(self->window, openbox_premax, cardinal,
1904                         (guint32*)dimensions, 4);
1905         }
1906     } else {
1907         guint num;
1908         gint32 *dimensions;
1909
1910         /* pick some fallbacks... */
1911         if (dir == 0 || dir == 1) { /* horz */
1912             x = screen_area(self->desktop)->x +
1913                 screen_area(self->desktop)->width / 4;
1914             w = screen_area(self->desktop)->width / 2;
1915         }
1916         if (dir == 0 || dir == 2) { /* vert */
1917             y = screen_area(self->desktop)->y +
1918                 screen_area(self->desktop)->height / 4;
1919             h = screen_area(self->desktop)->height / 2;
1920         }
1921
1922         if (PROP_GETA32(self->window, openbox_premax, cardinal,
1923                         (guint32**)&dimensions, &num)) {
1924             if (num == 4) {
1925                 if (dir == 0 || dir == 1) { /* horz */
1926                     x = dimensions[0];
1927                     w = dimensions[2];
1928                 }
1929                 if (dir == 0 || dir == 2) { /* vert */
1930                     y = dimensions[1];
1931                     h = dimensions[3];
1932                 }
1933             }
1934             g_free(dimensions);
1935         }
1936     }
1937
1938     if (dir == 0 || dir == 1) /* horz */
1939         self->max_horz = max;
1940     if (dir == 0 || dir == 2) /* vert */
1941         self->max_vert = max;
1942
1943     if (!self->max_horz && !self->max_vert)
1944         PROP_ERASE(self->window, openbox_premax);
1945
1946     client_change_state(self); /* change the state hints on the client */
1947
1948     /* figure out where the client should be going */
1949     frame_frame_gravity(self->frame, &x, &y);
1950     client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1951 }
1952
1953 void client_shade(Client *self, gboolean shade)
1954 {
1955     if ((!(self->functions & Func_Shade) && shade) || /* can't shade */
1956         self->shaded == shade) return;     /* already done */
1957
1958     /* when we're iconic, don't change the wmstate */
1959     if (!self->iconic)
1960         self->wmstate = shade ? IconicState : NormalState;
1961     self->shaded = shade;
1962     client_change_state(self);
1963     /* resize the frame to just the titlebar */
1964     frame_adjust_area(self->frame, FALSE, FALSE);
1965 }
1966
1967 void client_close(Client *self)
1968 {
1969     XEvent ce;
1970
1971     if (!(self->functions & Func_Close)) return;
1972
1973     /*
1974       XXX: itd be cool to do timeouts and shit here for killing the client's
1975       process off
1976       like... if the window is around after 5 seconds, then the close button
1977       turns a nice red, and if this function is called again, the client is
1978       explicitly killed.
1979     */
1980
1981     ce.xclient.type = ClientMessage;
1982     ce.xclient.message_type =  prop_atoms.wm_protocols;
1983     ce.xclient.display = ob_display;
1984     ce.xclient.window = self->window;
1985     ce.xclient.format = 32;
1986     ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
1987     ce.xclient.data.l[1] = event_lasttime;
1988     ce.xclient.data.l[2] = 0l;
1989     ce.xclient.data.l[3] = 0l;
1990     ce.xclient.data.l[4] = 0l;
1991     XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
1992 }
1993
1994 void client_kill(Client *self)
1995 {
1996     XKillClient(ob_display, self->window);
1997 }
1998
1999 void client_set_desktop(Client *self, guint target, gboolean donthide)
2000 {
2001     guint old;
2002
2003     if (target == self->desktop) return;
2004   
2005     g_message("Setting desktop %u", target);
2006
2007     g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2008
2009     /* remove from the old desktop(s) */
2010     focus_order_remove(self);
2011
2012     old = self->desktop;
2013     self->desktop = target;
2014     PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2015     /* the frame can display the current desktop state */
2016     frame_adjust_state(self->frame);
2017     /* 'move' the window to the new desktop */
2018     if (!donthide)
2019         client_showhide(self);
2020     /* raise if it was not already on the desktop */
2021     if (old != DESKTOP_ALL)
2022         stacking_raise(CLIENT_AS_WINDOW(self));
2023     screen_update_struts();
2024
2025     /* add to the new desktop(s) */
2026     if (config_focus_new)
2027         focus_order_to_top(self);
2028     else
2029         focus_order_to_bottom(self);
2030
2031     dispatch_client(Event_Client_Desktop, self, target, old);
2032 }
2033
2034 Client *client_search_modal_child(Client *self)
2035 {
2036     GSList *it;
2037     Client *ret;
2038   
2039     for (it = self->transients; it != NULL; it = it->next) {
2040         Client *c = it->data;
2041         if ((ret = client_search_modal_child(c))) return ret;
2042         if (c->modal) return c;
2043     }
2044     return NULL;
2045 }
2046
2047 gboolean client_validate(Client *self)
2048 {
2049     XEvent e; 
2050
2051     XSync(ob_display, FALSE); /* get all events on the server */
2052
2053     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2054         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2055         XPutBackEvent(ob_display, &e);
2056         return FALSE;
2057     }
2058
2059     return TRUE;
2060 }
2061
2062 void client_set_wm_state(Client *self, long state)
2063 {
2064     if (state == self->wmstate) return; /* no change */
2065   
2066     switch (state) {
2067     case IconicState:
2068         client_iconify(self, TRUE, TRUE);
2069         break;
2070     case NormalState:
2071         client_iconify(self, FALSE, TRUE);
2072         break;
2073     }
2074 }
2075
2076 void client_set_state(Client *self, Atom action, long data1, long data2)
2077 {
2078     gboolean shaded = self->shaded;
2079     gboolean fullscreen = self->fullscreen;
2080     gboolean max_horz = self->max_horz;
2081     gboolean max_vert = self->max_vert;
2082     int i;
2083
2084     if (!(action == prop_atoms.net_wm_state_add ||
2085           action == prop_atoms.net_wm_state_remove ||
2086           action == prop_atoms.net_wm_state_toggle))
2087         /* an invalid action was passed to the client message, ignore it */
2088         return; 
2089
2090     for (i = 0; i < 2; ++i) {
2091         Atom state = i == 0 ? data1 : data2;
2092     
2093         if (!state) continue;
2094
2095         /* if toggling, then pick whether we're adding or removing */
2096         if (action == prop_atoms.net_wm_state_toggle) {
2097             if (state == prop_atoms.net_wm_state_modal)
2098                 action = self->modal ? prop_atoms.net_wm_state_remove :
2099                     prop_atoms.net_wm_state_add;
2100             else if (state == prop_atoms.net_wm_state_maximized_vert)
2101                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2102                     prop_atoms.net_wm_state_add;
2103             else if (state == prop_atoms.net_wm_state_maximized_horz)
2104                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2105                     prop_atoms.net_wm_state_add;
2106             else if (state == prop_atoms.net_wm_state_shaded)
2107                 action = self->shaded ? prop_atoms.net_wm_state_remove :
2108                     prop_atoms.net_wm_state_add;
2109             else if (state == prop_atoms.net_wm_state_skip_taskbar)
2110                 action = self->skip_taskbar ?
2111                     prop_atoms.net_wm_state_remove :
2112                     prop_atoms.net_wm_state_add;
2113             else if (state == prop_atoms.net_wm_state_skip_pager)
2114                 action = self->skip_pager ?
2115                     prop_atoms.net_wm_state_remove :
2116                     prop_atoms.net_wm_state_add;
2117             else if (state == prop_atoms.net_wm_state_fullscreen)
2118                 action = self->fullscreen ?
2119                     prop_atoms.net_wm_state_remove :
2120                     prop_atoms.net_wm_state_add;
2121             else if (state == prop_atoms.net_wm_state_above)
2122                 action = self->above ? prop_atoms.net_wm_state_remove :
2123                     prop_atoms.net_wm_state_add;
2124             else if (state == prop_atoms.net_wm_state_below)
2125                 action = self->below ? prop_atoms.net_wm_state_remove :
2126                     prop_atoms.net_wm_state_add;
2127         }
2128     
2129         if (action == prop_atoms.net_wm_state_add) {
2130             if (state == prop_atoms.net_wm_state_modal) {
2131                 /* XXX raise here or something? */
2132                 self->modal = TRUE;
2133             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2134                 max_vert = TRUE;
2135             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2136                 max_horz = TRUE;
2137             } else if (state == prop_atoms.net_wm_state_shaded) {
2138                 shaded = TRUE;
2139             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2140                 self->skip_taskbar = TRUE;
2141             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2142                 self->skip_pager = TRUE;
2143             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2144                 fullscreen = TRUE;
2145             } else if (state == prop_atoms.net_wm_state_above) {
2146                 self->above = TRUE;
2147             } else if (state == prop_atoms.net_wm_state_below) {
2148                 self->below = TRUE;
2149             }
2150
2151         } else { /* action == prop_atoms.net_wm_state_remove */
2152             if (state == prop_atoms.net_wm_state_modal) {
2153                 self->modal = FALSE;
2154             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2155                 max_vert = FALSE;
2156             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2157                 max_horz = FALSE;
2158             } else if (state == prop_atoms.net_wm_state_shaded) {
2159                 shaded = FALSE;
2160             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2161                 self->skip_taskbar = FALSE;
2162             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2163                 self->skip_pager = FALSE;
2164             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2165                 fullscreen = FALSE;
2166             } else if (state == prop_atoms.net_wm_state_above) {
2167                 self->above = FALSE;
2168             } else if (state == prop_atoms.net_wm_state_below) {
2169                 self->below = FALSE;
2170             }
2171         }
2172     }
2173     if (max_horz != self->max_horz || max_vert != self->max_vert) {
2174         if (max_horz != self->max_horz && max_vert != self->max_vert) {
2175             /* toggling both */
2176             if (max_horz == max_vert) { /* both going the same way */
2177                 client_maximize(self, max_horz, 0, TRUE);
2178             } else {
2179                 client_maximize(self, max_horz, 1, TRUE);
2180                 client_maximize(self, max_vert, 2, TRUE);
2181             }
2182         } else {
2183             /* toggling one */
2184             if (max_horz != self->max_horz)
2185                 client_maximize(self, max_horz, 1, TRUE);
2186             else
2187                 client_maximize(self, max_vert, 2, TRUE);
2188         }
2189     }
2190     /* change fullscreen state before shading, as it will affect if the window
2191        can shade or not */
2192     if (fullscreen != self->fullscreen)
2193         client_fullscreen(self, fullscreen, TRUE);
2194     if (shaded != self->shaded)
2195         client_shade(self, shaded);
2196     client_calc_layer(self);
2197     client_change_state(self); /* change the hint to relect these changes */
2198 }
2199
2200 Client *client_focus_target(Client *self)
2201 {
2202     Client *child;
2203      
2204     /* if we have a modal child, then focus it, not us */
2205     child = client_search_modal_child(self);
2206     if (child) return child;
2207     return self;
2208 }
2209
2210 gboolean client_focus(Client *self)
2211 {
2212     XEvent ev;
2213
2214     /* choose the correct target */
2215     self = client_focus_target(self);
2216
2217     if (self->desktop != DESKTOP_ALL && self->desktop != screen_desktop) {
2218         /* update the focus lists */
2219         focus_order_to_top(self);
2220         return FALSE;
2221     }
2222
2223     if (!((self->can_focus || self->focus_notify) &&
2224           (self->desktop == screen_desktop ||
2225            self->desktop == DESKTOP_ALL) &&
2226           !self->iconic))
2227         return FALSE;
2228
2229     /* do a check to see if the window has already been unmapped or destroyed
2230        do this intelligently while watching out for unmaps we've generated
2231        (ignore_unmaps > 0) */
2232     if (XCheckTypedWindowEvent(ob_display, self->window,
2233                                DestroyNotify, &ev)) {
2234         XPutBackEvent(ob_display, &ev);
2235         return FALSE;
2236     }
2237     while (XCheckTypedWindowEvent(ob_display, self->window,
2238                                   UnmapNotify, &ev)) {
2239         if (self->ignore_unmaps) {
2240             self->ignore_unmaps--;
2241         } else {
2242             XPutBackEvent(ob_display, &ev);
2243             return FALSE;
2244         }
2245     }
2246
2247     if (self->can_focus)
2248         /* RevertToPointerRoot causes much more headache than RevertToNone, so
2249            I choose to use it always, hopefully to find errors quicker, if any
2250            are left. (I hate X. I hate focus events.) */
2251         XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2252                        event_lasttime);
2253
2254     if (self->focus_notify) {
2255         XEvent ce;
2256         ce.xclient.type = ClientMessage;
2257         ce.xclient.message_type = prop_atoms.wm_protocols;
2258         ce.xclient.display = ob_display;
2259         ce.xclient.window = self->window;
2260         ce.xclient.format = 32;
2261         ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2262         ce.xclient.data.l[1] = event_lasttime;
2263         ce.xclient.data.l[2] = 0l;
2264         ce.xclient.data.l[3] = 0l;
2265         ce.xclient.data.l[4] = 0l;
2266         XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2267     }
2268
2269 #ifdef DEBUG_FOCUS
2270     g_message("%sively focusing %lx at %d", (self->can_focus ? "act" : "pass"),
2271               self->window, (int)
2272               event_lasttime);
2273 #endif
2274
2275     /* Cause the FocusIn to come back to us. Important for desktop switches,
2276        since otherwise we'll have no FocusIn on the queue and send it off to
2277        the focus_backup. */
2278     XSync(ob_display, FALSE);
2279     return TRUE;
2280 }
2281
2282 void client_unfocus(Client *self)
2283 {
2284     g_assert(focus_client == self);
2285 #ifdef DEBUG_FOCUS
2286     g_message("client_unfocus for %lx", self->window);
2287 #endif
2288     focus_fallback(Fallback_Unfocusing);
2289 }
2290
2291 void client_activate(Client *self)
2292 {
2293     if (client_normal(self) && screen_showing_desktop)
2294         screen_show_desktop(FALSE);
2295     if (self->iconic)
2296         client_iconify(self, FALSE, TRUE);
2297     else if (!self->frame->visible)
2298         /* if its not visible for other reasons, then don't mess
2299            with it */
2300         return;
2301     if (self->shaded)
2302         client_shade(self, FALSE);
2303     client_focus(self);
2304     stacking_raise(CLIENT_AS_WINDOW(self));
2305 }
2306
2307 gboolean client_focused(Client *self)
2308 {
2309     return self == focus_client;
2310 }
2311
2312 Icon *client_icon(Client *self, int w, int h)
2313 {
2314     int i;
2315     /* si is the smallest image >= req */
2316     /* li is the largest image < req */
2317     unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2318
2319     if (!self->nicons) return NULL;
2320
2321     for (i = 0; i < self->nicons; ++i) {
2322         size = self->icons[i].width * self->icons[i].height;
2323         if (size < smallest && size >= (unsigned)(w * h)) {
2324             smallest = size;
2325             si = i;
2326         }
2327         if (size > largest && size <= (unsigned)(w * h)) {
2328             largest = size;
2329             li = i;
2330         }
2331     }
2332     if (largest == 0) /* didnt find one smaller than the requested size */
2333         return &self->icons[si];
2334     return &self->icons[li];
2335 }