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