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