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