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