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