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