]> icculus.org git repositories - dana/openbox.git/blob - openbox/client.c
only focus on startup if focus_new is true
[dana/openbox.git] / openbox / client.c
1 #include "client.h"
2 #include "startup.h"
3 #include "screen.h"
4 #include "moveresize.h"
5 #include "prop.h"
6 #include "extensions.h"
7 #include "frame.h"
8 #include "event.h"
9 #include "grab.h"
10 #include "focus.h"
11 #include "stacking.h"
12 #include "dispatch.h"
13 #include "openbox.h"
14 #include "group.h"
15 #include "config.h"
16
17 #include <glib.h>
18 #include <X11/Xutil.h>
19
20 /*! The event mask to grab on client windows */
21 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
22                           StructureNotifyMask)
23
24 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
25                                 ButtonMotionMask)
26
27 GList      *client_list      = NULL;
28 GHashTable *client_map       = NULL;
29
30 static void client_get_all(Client *self);
31 static void client_toggle_border(Client *self, gboolean show);
32 static void client_get_area(Client *self);
33 static void client_get_desktop(Client *self);
34 static void client_get_state(Client *self);
35 static void client_get_shaped(Client *self);
36 static void client_get_mwm_hints(Client *self);
37 static void client_get_gravity(Client *self);
38 static void client_showhide(Client *self);
39 static void client_change_allowed_actions(Client *self);
40 static void client_change_state(Client *self);
41 static void client_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, StackLayer l, gboolean raised)
1360 {
1361     StackLayer old;
1362     GSList *it;
1363
1364     old = self->layer;
1365     self->layer = l;
1366
1367     for (it = self->transients; it; it = it->next)
1368         calc_recursive(it->data, l, raised ? raised : l != old);
1369
1370     if (!raised && l != old)
1371         if (self->frame)
1372             stacking_raise(self);
1373 }
1374
1375 void client_calc_layer(Client *self)
1376 {
1377     StackLayer l;
1378     gboolean f;
1379
1380     /* transients take on the layer of their parents */
1381     if (self->transient_for) {
1382         if (self->transient_for != TRAN_GROUP) {
1383             self = self->transient_for;
1384         } else {
1385             GSList *it;
1386
1387             for (it = self->group->members; it; it = it->next)
1388                 if (it->data != self &&
1389                     ((Client*)it->data)->transient_for != TRAN_GROUP) {
1390                     self = it->data;
1391                     break;
1392                 }
1393         }
1394     }
1395
1396     /* is us or one of our transients focused? */
1397     if (client_focused(self))
1398         f = TRUE;
1399     else if (search_focus_tree(self, self))
1400         f = TRUE;
1401     else
1402         f = FALSE;
1403
1404     if (self->iconic) l = Layer_Icon;
1405     /* fullscreen windows are only in the fullscreen layer while focused */
1406     else if (self->fullscreen && f) l = Layer_Fullscreen;
1407     else if (self->type == Type_Desktop) l = Layer_Desktop;
1408     else if (self->type == Type_Dock) {
1409         if (!self->below) l = Layer_Top;
1410         else l = Layer_Normal;
1411     }
1412     else if (self->above) l = Layer_Above;
1413     else if (self->below) l = Layer_Below;
1414     else l = Layer_Normal;
1415
1416     calc_recursive(self, l, FALSE);
1417 }
1418
1419 gboolean client_should_show(Client *self)
1420 {
1421     if (self->iconic) return FALSE;
1422     else if (!(self->desktop == screen_desktop ||
1423                self->desktop == DESKTOP_ALL)) return FALSE;
1424     else if (client_normal(self) && screen_showing_desktop) return FALSE;
1425     
1426     return TRUE;
1427 }
1428
1429 static void client_showhide(Client *self)
1430 {
1431
1432     if (client_should_show(self))
1433         frame_show(self->frame);
1434     else
1435         frame_hide(self->frame);
1436 }
1437
1438 gboolean client_normal(Client *self) {
1439     return ! (self->type == Type_Desktop || self->type == Type_Dock ||
1440               self->type == Type_Splash);
1441 }
1442
1443 static void client_apply_startup_state(Client *self)
1444 {
1445     /* these are in a carefully crafted order.. */
1446
1447     if (self->iconic) {
1448         self->iconic = FALSE;
1449         client_iconify(self, TRUE, FALSE);
1450     }
1451     if (self->fullscreen) {
1452         self->fullscreen = FALSE;
1453         client_fullscreen(self, TRUE, FALSE);
1454     }
1455     if (self->shaded) {
1456         self->shaded = FALSE;
1457         client_shade(self, TRUE);
1458     }
1459     if (self->urgent)
1460         dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1461   
1462     if (self->max_vert && self->max_horz) {
1463         self->max_vert = self->max_horz = FALSE;
1464         client_maximize(self, TRUE, 0, FALSE);
1465     } else if (self->max_vert) {
1466         self->max_vert = FALSE;
1467         client_maximize(self, TRUE, 2, FALSE);
1468     } else if (self->max_horz) {
1469         self->max_horz = FALSE;
1470         client_maximize(self, TRUE, 1, FALSE);
1471     }
1472
1473     /* nothing to do for the other states:
1474        skip_taskbar
1475        skip_pager
1476        modal
1477        above
1478        below
1479     */
1480 }
1481
1482 void client_configure(Client *self, Corner anchor, int x, int y, int w, int h,
1483                       gboolean user, gboolean final)
1484 {
1485     gboolean moved = FALSE, resized = FALSE;
1486
1487     /* gets the frame's position */
1488     frame_client_gravity(self->frame, &x, &y);
1489
1490     /* these positions are frame positions, not client positions */
1491
1492     /* set the size and position if fullscreen */
1493     if (self->fullscreen) {
1494         x = 0;
1495         y = 0;
1496         w = screen_physical_size.width;
1497         h = screen_physical_size.height;
1498         user = FALSE; /* ignore that increment etc shit when in fullscreen */
1499     } else {
1500         /* set the size and position if maximized */
1501         if (self->max_horz) {
1502             x = screen_area(self->desktop)->x - self->frame->size.left;
1503             w = screen_area(self->desktop)->width;
1504         }
1505         if (self->max_vert) {
1506             y = screen_area(self->desktop)->y;
1507             h = screen_area(self->desktop)->height -
1508                 self->frame->size.top - self->frame->size.bottom;
1509         }
1510     }
1511
1512     /* gets the client's position */
1513     frame_frame_gravity(self->frame, &x, &y);
1514
1515     /* these override the above states! if you cant move you can't move! */
1516     if (user) {
1517         if (!(self->functions & Func_Move)) {
1518             x = self->area.x;
1519             y = self->area.y;
1520         }
1521         if (!(self->functions & Func_Resize)) {
1522             w = self->area.width;
1523             h = self->area.height;
1524         }
1525     }
1526
1527     if (!(w == self->area.width && h == self->area.height)) {
1528         w -= self->base_size.width;
1529         h -= self->base_size.height;
1530
1531         if (user) {
1532             /* for interactive resizing. have to move half an increment in each
1533                direction. */
1534
1535             /* how far we are towards the next size inc */
1536             int mw = w % self->size_inc.width; 
1537             int mh = h % self->size_inc.height;
1538             /* amount to add */
1539             int aw = self->size_inc.width / 2;
1540             int ah = self->size_inc.height / 2;
1541             /* don't let us move into a new size increment */
1542             if (mw + aw >= self->size_inc.width)
1543                 aw = self->size_inc.width - mw - 1;
1544             if (mh + ah >= self->size_inc.height)
1545                 ah = self->size_inc.height - mh - 1;
1546             w += aw;
1547             h += ah;
1548     
1549             /* if this is a user-requested resize, then check against min/max
1550                sizes and aspect ratios */
1551
1552             /* smaller than min size or bigger than max size? */
1553             if (w > self->max_size.width) w = self->max_size.width;
1554             if (w < self->min_size.width) w = self->min_size.width;
1555             if (h > self->max_size.height) h = self->max_size.height;
1556             if (h < self->min_size.height) h = self->min_size.height;
1557
1558             /* adjust the height ot match the width for the aspect ratios */
1559             if (self->min_ratio)
1560                 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1561             if (self->max_ratio)
1562                 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1563         }
1564
1565         /* keep to the increments */
1566         w /= self->size_inc.width;
1567         h /= self->size_inc.height;
1568
1569         /* you cannot resize to nothing */
1570         if (w < 1) w = 1;
1571         if (h < 1) h = 1;
1572   
1573         /* store the logical size */
1574         SIZE_SET(self->logical_size, w, h);
1575
1576         w *= self->size_inc.width;
1577         h *= self->size_inc.height;
1578
1579         w += self->base_size.width;
1580         h += self->base_size.height;
1581     }
1582
1583     switch (anchor) {
1584     case Corner_TopLeft:
1585         break;
1586     case Corner_TopRight:
1587         x -= w - self->area.width;
1588         break;
1589     case Corner_BottomLeft:
1590         y -= h - self->area.height;
1591         break;
1592     case Corner_BottomRight:
1593         x -= w - self->area.width;
1594         y -= h - self->area.height;
1595         break;
1596     }
1597
1598     moved = x != self->area.x || y != self->area.y;
1599     resized = w != self->area.width || h != self->area.height;
1600
1601     RECT_SET(self->area, x, y, w, h);
1602
1603     if (resized)
1604         XResizeWindow(ob_display, self->window, w, h);
1605
1606     /* move/resize the frame to match the request */
1607     if (self->frame) {
1608         if (moved || resized)
1609             frame_adjust_area(self->frame, moved, resized);
1610
1611         if (!user || final) {
1612             XEvent event;
1613             event.type = ConfigureNotify;
1614             event.xconfigure.display = ob_display;
1615             event.xconfigure.event = self->window;
1616             event.xconfigure.window = self->window;
1617     
1618             /* root window coords with border in mind */
1619             event.xconfigure.x = x - self->border_width +
1620                 self->frame->size.left;
1621             event.xconfigure.y = y - self->border_width +
1622                 self->frame->size.top;
1623     
1624             event.xconfigure.width = self->area.width;
1625             event.xconfigure.height = self->area.height;
1626             event.xconfigure.border_width = self->border_width;
1627             event.xconfigure.above = self->frame->plate;
1628             event.xconfigure.override_redirect = FALSE;
1629             XSendEvent(event.xconfigure.display, event.xconfigure.window,
1630                        FALSE, StructureNotifyMask, &event);
1631         }
1632     }
1633 }
1634
1635 void client_fullscreen(Client *self, gboolean fs, gboolean savearea)
1636 {
1637     int x, y, w, h;
1638
1639     if (!(self->functions & Func_Fullscreen) || /* can't */
1640         self->fullscreen == fs) return;         /* already done */
1641
1642     self->fullscreen = fs;
1643     client_change_state(self); /* change the state hints on the client,
1644                                   and adjust out layer/stacking */
1645
1646     if (fs) {
1647         if (savearea) {
1648             guint32 dimensions[4];
1649             dimensions[0] = self->area.x;
1650             dimensions[1] = self->area.y;
1651             dimensions[2] = self->area.width;
1652             dimensions[3] = self->area.height;
1653   
1654             PROP_SETA32(self->window, openbox_premax, cardinal,
1655                         dimensions, 4);
1656         }
1657
1658         /* these are not actually used cuz client_configure will set them
1659            as appropriate when the window is fullscreened */
1660         x = y = w = h = 0;
1661     } else {
1662         guint num;
1663         gint32 *dimensions;
1664
1665         /* pick some fallbacks... */
1666         x = screen_area(self->desktop)->x +
1667             screen_area(self->desktop)->width / 4;
1668         y = screen_area(self->desktop)->y +
1669             screen_area(self->desktop)->height / 4;
1670         w = screen_area(self->desktop)->width / 2;
1671         h = screen_area(self->desktop)->height / 2;
1672
1673         if (PROP_GETA32(self->window, openbox_premax, cardinal,
1674                         (guint32**)&dimensions, &num)) {
1675             if (num == 4) {
1676                 x = dimensions[0];
1677                 y = dimensions[1];
1678                 w = dimensions[2];
1679                 h = dimensions[3];
1680             }
1681             g_free(dimensions);
1682         }
1683     }
1684
1685     client_setup_decor_and_functions(self);
1686
1687     client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1688
1689     /* try focus us when we go into fullscreen mode */
1690     client_focus(self);
1691 }
1692
1693 void client_iconify(Client *self, gboolean iconic, gboolean curdesk)
1694 {
1695     GSList *it;
1696
1697     /* move up the transient chain as far as possible first if deiconifying */
1698     if (!iconic)
1699         while (self->transient_for) {
1700             if (self->transient_for != TRAN_GROUP) {
1701                 if (self->transient_for->iconic == iconic)
1702                     break;
1703                 self = self->transient_for;
1704             } else {
1705                 GSList *it;
1706
1707                 /* the check for TRAN_GROUP is to prevent an infinate loop with
1708                    2 transients of the same group at the head of the group's
1709                    members list */
1710                 for (it = self->group->members; it; it = it->next) {
1711                     Client *c = it->data;
1712
1713                     if (c != self && c->transient_for->iconic != iconic &&
1714                         c->transient_for != TRAN_GROUP) {
1715                         self = it->data;
1716                         break;
1717                     }
1718                 }
1719                 if (it == NULL) break;
1720             }
1721         }
1722
1723     if (self->iconic == iconic) return; /* nothing to do */
1724
1725     g_message("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
1726               self->window);
1727
1728     self->iconic = iconic;
1729
1730     if (iconic) {
1731         self->wmstate = IconicState;
1732         self->ignore_unmaps++;
1733         /* we unmap the client itself so that we can get MapRequest events,
1734            and because the ICCCM tells us to! */
1735         XUnmapWindow(ob_display, self->window);
1736
1737         /* update the focus lists.. iconic windows go to the bottom */
1738         focus_order_to_bottom(self);
1739     } else {
1740         if (curdesk)
1741             client_set_desktop(self, screen_desktop, FALSE);
1742         self->wmstate = self->shaded ? IconicState : NormalState;
1743         XMapWindow(ob_display, self->window);
1744     }
1745     client_change_state(self);
1746     client_showhide(self);
1747     screen_update_struts();
1748
1749     dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
1750                     self, 0, 0);
1751
1752     /* iconify all transients */
1753     for (it = self->transients; it != NULL; it = it->next)
1754         if (it->data != self) client_iconify(it->data, iconic, curdesk);
1755 }
1756
1757 void client_maximize(Client *self, gboolean max, int dir, gboolean savearea)
1758 {
1759     int x, y, w, h;
1760      
1761     g_assert(dir == 0 || dir == 1 || dir == 2);
1762     if (!(self->functions & Func_Maximize)) return; /* can't */
1763
1764     /* check if already done */
1765     if (max) {
1766         if (dir == 0 && self->max_horz && self->max_vert) return;
1767         if (dir == 1 && self->max_horz) return;
1768         if (dir == 2 && self->max_vert) return;
1769     } else {
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     }
1774
1775     /* work with the frame's coords */
1776     x = self->frame->area.x;
1777     y = self->frame->area.y;
1778     w = self->area.width;
1779     h = self->area.height;
1780
1781     if (max) {
1782         if (savearea) {
1783             gint32 dimensions[4];
1784             gint32 *readdim;
1785             guint num;
1786
1787             dimensions[0] = x;
1788             dimensions[1] = y;
1789             dimensions[2] = w;
1790             dimensions[3] = h;
1791
1792             /* get the property off the window and use it for the dimensions
1793                we are already maxed on */
1794             if (PROP_GETA32(self->window, openbox_premax, cardinal,
1795                             (guint32**)&readdim, &num)) {
1796                 if (num == 4) {
1797                     if (self->max_horz) {
1798                         dimensions[0] = readdim[0];
1799                         dimensions[2] = readdim[2];
1800                     }
1801                     if (self->max_vert) {
1802                         dimensions[1] = readdim[1];
1803                         dimensions[3] = readdim[3];
1804                     }
1805                 }
1806                 g_free(readdim);
1807             }
1808
1809             PROP_SETA32(self->window, openbox_premax, cardinal,
1810                         (guint32*)dimensions, 4);
1811         }
1812     } else {
1813         guint num;
1814         gint32 *dimensions;
1815
1816         /* pick some fallbacks... */
1817         if (dir == 0 || dir == 1) { /* horz */
1818             x = screen_area(self->desktop)->x +
1819                 screen_area(self->desktop)->width / 4;
1820             w = screen_area(self->desktop)->width / 2;
1821         }
1822         if (dir == 0 || dir == 2) { /* vert */
1823             y = screen_area(self->desktop)->y +
1824                 screen_area(self->desktop)->height / 4;
1825             h = screen_area(self->desktop)->height / 2;
1826         }
1827
1828         if (PROP_GETA32(self->window, openbox_premax, cardinal,
1829                         (guint32**)&dimensions, &num)) {
1830             if (num == 4) {
1831                 if (dir == 0 || dir == 1) { /* horz */
1832                     x = dimensions[0];
1833                     w = dimensions[2];
1834                 }
1835                 if (dir == 0 || dir == 2) { /* vert */
1836                     y = dimensions[1];
1837                     h = dimensions[3];
1838                 }
1839             }
1840             g_free(dimensions);
1841         }
1842     }
1843
1844     if (dir == 0 || dir == 1) /* horz */
1845         self->max_horz = max;
1846     if (dir == 0 || dir == 2) /* vert */
1847         self->max_vert = max;
1848
1849     if (!self->max_horz && !self->max_vert)
1850         PROP_ERASE(self->window, openbox_premax);
1851
1852     client_change_state(self); /* change the state hints on the client */
1853
1854     /* figure out where the client should be going */
1855     frame_frame_gravity(self->frame, &x, &y);
1856     client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1857 }
1858
1859 void client_shade(Client *self, gboolean shade)
1860 {
1861     if ((!(self->functions & Func_Shade) && shade) || /* can't shade */
1862         self->shaded == shade) return;     /* already done */
1863
1864     /* when we're iconic, don't change the wmstate */
1865     if (!self->iconic)
1866         self->wmstate = shade ? IconicState : NormalState;
1867     self->shaded = shade;
1868     client_change_state(self);
1869     /* resize the frame to just the titlebar */
1870     frame_adjust_area(self->frame, FALSE, FALSE);
1871 }
1872
1873 void client_close(Client *self)
1874 {
1875     XEvent ce;
1876
1877     if (!(self->functions & Func_Close)) return;
1878
1879     /*
1880       XXX: itd be cool to do timeouts and shit here for killing the client's
1881       process off
1882       like... if the window is around after 5 seconds, then the close button
1883       turns a nice red, and if this function is called again, the client is
1884       explicitly killed.
1885     */
1886
1887     ce.xclient.type = ClientMessage;
1888     ce.xclient.message_type =  prop_atoms.wm_protocols;
1889     ce.xclient.display = ob_display;
1890     ce.xclient.window = self->window;
1891     ce.xclient.format = 32;
1892     ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
1893     ce.xclient.data.l[1] = event_lasttime;
1894     ce.xclient.data.l[2] = 0l;
1895     ce.xclient.data.l[3] = 0l;
1896     ce.xclient.data.l[4] = 0l;
1897     XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
1898 }
1899
1900 void client_kill(Client *self)
1901 {
1902     XKillClient(ob_display, self->window);
1903 }
1904
1905 void client_set_desktop(Client *self, guint target, gboolean donthide)
1906 {
1907     guint old;
1908
1909     if (target == self->desktop) return;
1910   
1911     g_message("Setting desktop %u", target);
1912
1913     g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
1914
1915     /* remove from the old desktop(s) */
1916     focus_order_remove(self);
1917
1918     old = self->desktop;
1919     self->desktop = target;
1920     PROP_SET32(self->window, net_wm_desktop, cardinal, target);
1921     /* the frame can display the current desktop state */
1922     frame_adjust_state(self->frame);
1923     /* 'move' the window to the new desktop */
1924     if (!donthide)
1925         client_showhide(self);
1926     /* raise if it was not already on the desktop */
1927     if (old != DESKTOP_ALL)
1928         stacking_raise(self);
1929     screen_update_struts();
1930
1931     /* add to the new desktop(s) */
1932     if (config_focus_new)
1933         focus_order_to_top(self);
1934     else
1935         focus_order_to_bottom(self);
1936
1937     dispatch_client(Event_Client_Desktop, self, target, old);
1938 }
1939
1940 static Client *search_modal_tree(Client *node, Client *skip)
1941 {
1942     GSList *it;
1943     Client *ret;
1944   
1945     for (it = node->transients; it != NULL; it = it->next) {
1946         Client *c = it->data;
1947         if (c == skip) continue; /* circular? */
1948         if ((ret = search_modal_tree(c, skip))) return ret;
1949         if (c->modal) return c;
1950     }
1951     return NULL;
1952 }
1953
1954 Client *client_find_modal_child(Client *self)
1955 {
1956     return search_modal_tree(self, self);
1957 }
1958
1959 gboolean client_validate(Client *self)
1960 {
1961     XEvent e; 
1962
1963     XSync(ob_display, FALSE); /* get all events on the server */
1964
1965     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
1966         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
1967         XPutBackEvent(ob_display, &e);
1968         return FALSE;
1969     }
1970
1971     return TRUE;
1972 }
1973
1974 void client_set_wm_state(Client *self, long state)
1975 {
1976     if (state == self->wmstate) return; /* no change */
1977   
1978     switch (state) {
1979     case IconicState:
1980         client_iconify(self, TRUE, TRUE);
1981         break;
1982     case NormalState:
1983         client_iconify(self, FALSE, TRUE);
1984         break;
1985     }
1986 }
1987
1988 void client_set_state(Client *self, Atom action, long data1, long data2)
1989 {
1990     gboolean shaded = self->shaded;
1991     gboolean fullscreen = self->fullscreen;
1992     gboolean max_horz = self->max_horz;
1993     gboolean max_vert = self->max_vert;
1994     int i;
1995
1996     if (!(action == prop_atoms.net_wm_state_add ||
1997           action == prop_atoms.net_wm_state_remove ||
1998           action == prop_atoms.net_wm_state_toggle))
1999         /* an invalid action was passed to the client message, ignore it */
2000         return; 
2001
2002     for (i = 0; i < 2; ++i) {
2003         Atom state = i == 0 ? data1 : data2;
2004     
2005         if (!state) continue;
2006
2007         /* if toggling, then pick whether we're adding or removing */
2008         if (action == prop_atoms.net_wm_state_toggle) {
2009             if (state == prop_atoms.net_wm_state_modal)
2010                 action = self->modal ? prop_atoms.net_wm_state_remove :
2011                     prop_atoms.net_wm_state_add;
2012             else if (state == prop_atoms.net_wm_state_maximized_vert)
2013                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2014                     prop_atoms.net_wm_state_add;
2015             else if (state == prop_atoms.net_wm_state_maximized_horz)
2016                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2017                     prop_atoms.net_wm_state_add;
2018             else if (state == prop_atoms.net_wm_state_shaded)
2019                 action = self->shaded ? prop_atoms.net_wm_state_remove :
2020                     prop_atoms.net_wm_state_add;
2021             else if (state == prop_atoms.net_wm_state_skip_taskbar)
2022                 action = self->skip_taskbar ?
2023                     prop_atoms.net_wm_state_remove :
2024                     prop_atoms.net_wm_state_add;
2025             else if (state == prop_atoms.net_wm_state_skip_pager)
2026                 action = self->skip_pager ?
2027                     prop_atoms.net_wm_state_remove :
2028                     prop_atoms.net_wm_state_add;
2029             else if (state == prop_atoms.net_wm_state_fullscreen)
2030                 action = self->fullscreen ?
2031                     prop_atoms.net_wm_state_remove :
2032                     prop_atoms.net_wm_state_add;
2033             else if (state == prop_atoms.net_wm_state_above)
2034                 action = self->above ? prop_atoms.net_wm_state_remove :
2035                     prop_atoms.net_wm_state_add;
2036             else if (state == prop_atoms.net_wm_state_below)
2037                 action = self->below ? prop_atoms.net_wm_state_remove :
2038                     prop_atoms.net_wm_state_add;
2039         }
2040     
2041         if (action == prop_atoms.net_wm_state_add) {
2042             if (state == prop_atoms.net_wm_state_modal) {
2043                 /* XXX raise here or something? */
2044                 self->modal = TRUE;
2045             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2046                 max_vert = TRUE;
2047             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2048                 max_horz = TRUE;
2049             } else if (state == prop_atoms.net_wm_state_shaded) {
2050                 shaded = TRUE;
2051             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2052                 self->skip_taskbar = TRUE;
2053             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2054                 self->skip_pager = TRUE;
2055             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2056                 fullscreen = TRUE;
2057             } else if (state == prop_atoms.net_wm_state_above) {
2058                 self->above = TRUE;
2059             } else if (state == prop_atoms.net_wm_state_below) {
2060                 self->below = TRUE;
2061             }
2062
2063         } else { /* action == prop_atoms.net_wm_state_remove */
2064             if (state == prop_atoms.net_wm_state_modal) {
2065                 self->modal = FALSE;
2066             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2067                 max_vert = FALSE;
2068             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2069                 max_horz = FALSE;
2070             } else if (state == prop_atoms.net_wm_state_shaded) {
2071                 shaded = FALSE;
2072             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2073                 self->skip_taskbar = FALSE;
2074             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2075                 self->skip_pager = FALSE;
2076             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2077                 fullscreen = FALSE;
2078             } else if (state == prop_atoms.net_wm_state_above) {
2079                 self->above = FALSE;
2080             } else if (state == prop_atoms.net_wm_state_below) {
2081                 self->below = FALSE;
2082             }
2083         }
2084     }
2085     if (max_horz != self->max_horz || max_vert != self->max_vert) {
2086         if (max_horz != self->max_horz && max_vert != self->max_vert) {
2087             /* toggling both */
2088             if (max_horz == max_vert) { /* both going the same way */
2089                 client_maximize(self, max_horz, 0, TRUE);
2090             } else {
2091                 client_maximize(self, max_horz, 1, TRUE);
2092                 client_maximize(self, max_vert, 2, TRUE);
2093             }
2094         } else {
2095             /* toggling one */
2096             if (max_horz != self->max_horz)
2097                 client_maximize(self, max_horz, 1, TRUE);
2098             else
2099                 client_maximize(self, max_vert, 2, TRUE);
2100         }
2101     }
2102     /* change fullscreen state before shading, as it will affect if the window
2103        can shade or not */
2104     if (fullscreen != self->fullscreen)
2105         client_fullscreen(self, fullscreen, TRUE);
2106     if (shaded != self->shaded)
2107         client_shade(self, shaded);
2108     client_calc_layer(self);
2109     client_change_state(self); /* change the hint to relect these changes */
2110 }
2111
2112 Client *client_focus_target(Client *self)
2113 {
2114     Client *child;
2115      
2116     /* if we have a modal child, then focus it, not us */
2117     child = client_find_modal_child(self);
2118     if (child) return child;
2119     return self;
2120 }
2121
2122 gboolean client_focus(Client *self)
2123 {
2124     XEvent ev;
2125
2126     /* choose the correct target */
2127     self = client_focus_target(self);
2128
2129     if (self->desktop != DESKTOP_ALL && self->desktop != screen_desktop) {
2130         /* update the focus lists */
2131         focus_order_to_top(self);
2132         return FALSE;
2133     }
2134
2135     if (!((self->can_focus || self->focus_notify) &&
2136           (self->desktop == screen_desktop ||
2137            self->desktop == DESKTOP_ALL) &&
2138           !self->iconic))
2139         return FALSE;
2140
2141     /* do a check to see if the window has already been unmapped or destroyed
2142        do this intelligently while watching out for unmaps we've generated
2143        (ignore_unmaps > 0) */
2144     if (XCheckTypedWindowEvent(ob_display, self->window,
2145                                DestroyNotify, &ev)) {
2146         XPutBackEvent(ob_display, &ev);
2147         return FALSE;
2148     }
2149     while (XCheckTypedWindowEvent(ob_display, self->window,
2150                                   UnmapNotify, &ev)) {
2151         if (self->ignore_unmaps) {
2152             self->ignore_unmaps--;
2153         } else {
2154             XPutBackEvent(ob_display, &ev);
2155             return FALSE;
2156         }
2157     }
2158
2159     if (self->can_focus)
2160         /* RevertToPointerRoot causes much more headache than RevertToNone, so
2161            I choose to use it always, hopefully to find errors quicker, if any
2162            are left. (I hate X. I hate focus events.) */
2163         XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2164                        event_lasttime);
2165
2166     if (self->focus_notify) {
2167         XEvent ce;
2168         ce.xclient.type = ClientMessage;
2169         ce.xclient.message_type = prop_atoms.wm_protocols;
2170         ce.xclient.display = ob_display;
2171         ce.xclient.window = self->window;
2172         ce.xclient.format = 32;
2173         ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2174         ce.xclient.data.l[1] = event_lasttime;
2175         ce.xclient.data.l[2] = 0l;
2176         ce.xclient.data.l[3] = 0l;
2177         ce.xclient.data.l[4] = 0l;
2178         XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2179     }
2180
2181 #ifdef DEBUG_FOCUS
2182     g_message("%sively focusing %lx at %d", (self->can_focus ? "act" : "pass"),
2183               self->window, (int)
2184               event_lasttime);
2185 #endif
2186
2187     /* Cause the FocusIn to come back to us. Important for desktop switches,
2188        since otherwise we'll have no FocusIn on the queue and send it off to
2189        the focus_backup. */
2190     XSync(ob_display, FALSE);
2191     return TRUE;
2192 }
2193
2194 void client_unfocus(Client *self)
2195 {
2196     g_assert(focus_client == self);
2197 #ifdef DEBUG_FOCUS
2198     g_message("client_unfocus for %lx", self->window);
2199 #endif
2200     focus_fallback(Fallback_Unfocusing);
2201 }
2202
2203 gboolean client_focused(Client *self)
2204 {
2205     return self == focus_client;
2206 }
2207
2208 Icon *client_icon(Client *self, int w, int h)
2209 {
2210     int i;
2211     /* si is the smallest image >= req */
2212     /* li is the largest image < req */
2213     unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2214
2215     if (!self->nicons) return NULL;
2216
2217     for (i = 0; i < self->nicons; ++i) {
2218         size = self->icons[i].width * self->icons[i].height;
2219         if (size < smallest && size >= (unsigned)(w * h)) {
2220             smallest = size;
2221             si = i;
2222         }
2223         if (size > largest && size <= (unsigned)(w * h)) {
2224             largest = size;
2225             li = i;
2226         }
2227     }
2228     if (largest == 0) /* didnt find one smaller than the requested size */
2229         return &self->icons[si];
2230     return &self->icons[li];
2231 }