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