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