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