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