]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/client.c
add group transients when setting up the group
[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     /* this makes sure that these windows appear on all desktops */
522     if (self->type == Type_Desktop)
523         self->desktop = DESKTOP_ALL;
524
525     /* set the desktop hint, to make sure that it always exists, and to
526        reflect any changes we've made here */
527     PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
528
529     client_change_state(self);
530 }
531
532 static void client_get_area(Client *self)
533 {
534     XWindowAttributes wattrib;
535     Status ret;
536   
537     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
538     g_assert(ret != BadWindow);
539
540     RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
541     self->border_width = wattrib.border_width;
542 }
543
544 static void client_get_desktop(Client *self)
545 {
546     guint32 d;
547
548     if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
549         if (d >= screen_num_desktops && d != DESKTOP_ALL)
550             d = screen_num_desktops - 1;
551         self->desktop = d;
552     } else {
553         /* defaults to the current desktop */
554         self->desktop = screen_desktop;
555     }
556 }
557
558 static void client_get_state(Client *self)
559 {
560     guint32 *state;
561     guint num;
562   
563     self->modal = self->shaded = self->max_horz = self->max_vert =
564         self->fullscreen = self->above = self->below = self->iconic =
565         self->skip_taskbar = self->skip_pager = FALSE;
566
567     if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
568         gulong i;
569         for (i = 0; i < num; ++i) {
570             if (state[i] == prop_atoms.net_wm_state_modal)
571                 self->modal = TRUE;
572             else if (state[i] == prop_atoms.net_wm_state_shaded)
573                 self->shaded = TRUE;
574             else if (state[i] == prop_atoms.net_wm_state_hidden)
575                 self->iconic = TRUE;
576             else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
577                 self->skip_taskbar = TRUE;
578             else if (state[i] == prop_atoms.net_wm_state_skip_pager)
579                 self->skip_pager = TRUE;
580             else if (state[i] == prop_atoms.net_wm_state_fullscreen)
581                 self->fullscreen = TRUE;
582             else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
583                 self->max_vert = TRUE;
584             else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
585                 self->max_horz = TRUE;
586             else if (state[i] == prop_atoms.net_wm_state_above)
587                 self->above = TRUE;
588             else if (state[i] == prop_atoms.net_wm_state_below)
589                 self->below = TRUE;
590         }
591
592         g_free(state);
593     }
594 }
595
596 static void client_get_shaped(Client *self)
597 {
598     self->shaped = FALSE;
599 #ifdef   SHAPE
600     if (extensions_shape) {
601         int foo;
602         guint ufoo;
603         int s;
604
605         XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
606
607         XShapeQueryExtents(ob_display, self->window, &s, &foo,
608                            &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
609                            &ufoo);
610         self->shaped = (s != 0);
611     }
612 #endif
613 }
614
615 void client_update_transient_for(Client *self)
616 {
617     Window t = None;
618     Client *c = NULL;
619
620     if (XGetTransientForHint(ob_display, self->window, &t) &&
621         t != self->window) { /* cant be transient to itself! */
622         self->transient = TRUE;
623         c = g_hash_table_lookup(client_map, &t);
624         g_assert(c != self);/* if this happens then we need to check for it*/
625
626         if (!c && self->group) {
627             /* not transient to a client, see if it is transient for a
628                group */
629             if (t == self->group->leader ||
630                 t == None ||
631                 t == ob_root) {
632                 /* window is a transient for its group! */
633                 c = TRAN_GROUP;
634             }
635         }
636     } else
637         self->transient = FALSE;
638
639     /* if anything has changed... */
640     if (c != self->transient_for) {
641         if (self->transient_for == TRAN_GROUP) { /* transient of group */
642             GSList *it;
643
644             /* remove from old parents */
645             for (it = self->group->members; it; it = it->next)
646                 if (it->data != self)
647                     ((Client*)it->data)->transients =
648                         g_slist_remove(((Client*)it->data)->transients, self);
649         } else if (self->transient_for != NULL) { /* transient of window */
650             /* remove from old parent */
651             self->transient_for->transients =
652                 g_slist_remove(self->transient_for->transients, self);
653         }
654         self->transient_for = c;
655         if (self->transient_for == TRAN_GROUP) { /* transient of group */
656             GSList *it;
657
658             /* add to new parents */
659             for (it = self->group->members; it; it = it->next)
660                 if (it->data != self)
661                     ((Client*)it->data)->transients =
662                         g_slist_append(((Client*)it->data)->transients, self);
663         } else if (self->transient_for != NULL) { /* transient of window */
664             /* add to new parent */
665             self->transient_for->transients =
666                 g_slist_append(self->transient_for->transients, self);
667         }
668     }
669 }
670
671 static void client_get_mwm_hints(Client *self)
672 {
673     guint num;
674     guint32 *hints;
675
676     self->mwmhints.flags = 0; /* default to none */
677
678     if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
679                     &hints, &num)) {
680         if (num >= MWM_ELEMENTS) {
681             self->mwmhints.flags = hints[0];
682             self->mwmhints.functions = hints[1];
683             self->mwmhints.decorations = hints[2];
684         }
685         g_free(hints);
686     }
687 }
688
689 void client_get_type(Client *self)
690 {
691     guint num, i;
692     guint32 *val;
693
694     self->type = -1;
695   
696     if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
697         /* use the first value that we know about in the array */
698         for (i = 0; i < num; ++i) {
699             if (val[i] == prop_atoms.net_wm_window_type_desktop)
700                 self->type = Type_Desktop;
701             else if (val[i] == prop_atoms.net_wm_window_type_dock)
702                 self->type = Type_Dock;
703             else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
704                 self->type = Type_Toolbar;
705             else if (val[i] == prop_atoms.net_wm_window_type_menu)
706                 self->type = Type_Menu;
707             else if (val[i] == prop_atoms.net_wm_window_type_utility)
708                 self->type = Type_Utility;
709             else if (val[i] == prop_atoms.net_wm_window_type_splash)
710                 self->type = Type_Splash;
711             else if (val[i] == prop_atoms.net_wm_window_type_dialog)
712                 self->type = Type_Dialog;
713             else if (val[i] == prop_atoms.net_wm_window_type_normal)
714                 self->type = Type_Normal;
715             else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
716                 /* prevent this window from getting any decor or
717                    functionality */
718                 self->mwmhints.flags &= (MwmFlag_Functions |
719                                          MwmFlag_Decorations);
720                 self->mwmhints.decorations = 0;
721                 self->mwmhints.functions = 0;
722             }
723             if (self->type != (WindowType) -1)
724                 break; /* grab the first legit type */
725         }
726         g_free(val);
727     }
728     
729     if (self->type == (WindowType) -1) {
730         /*the window type hint was not set, which means we either classify
731           ourself as a normal window or a dialog, depending on if we are a
732           transient. */
733         if (self->transient)
734             self->type = Type_Dialog;
735         else
736             self->type = Type_Normal;
737     }
738 }
739
740 void client_update_protocols(Client *self)
741 {
742     guint32 *proto;
743     guint num_return, i;
744
745     self->focus_notify = FALSE;
746     self->delete_window = FALSE;
747
748     if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
749         for (i = 0; i < num_return; ++i) {
750             if (proto[i] == prop_atoms.wm_delete_window) {
751                 /* this means we can request the window to close */
752                 self->delete_window = TRUE;
753             } else if (proto[i] == prop_atoms.wm_take_focus)
754                 /* if this protocol is requested, then the window will be
755                    notified whenever we want it to receive focus */
756                 self->focus_notify = TRUE;
757         }
758         g_free(proto);
759     }
760 }
761
762 static void client_get_gravity(Client *self)
763 {
764     XWindowAttributes wattrib;
765     Status ret;
766
767     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
768     g_assert(ret != BadWindow);
769     self->gravity = wattrib.win_gravity;
770 }
771
772 void client_update_normal_hints(Client *self)
773 {
774     XSizeHints size;
775     long ret;
776     int oldgravity = self->gravity;
777
778     /* defaults */
779     self->min_ratio = 0.0f;
780     self->max_ratio = 0.0f;
781     SIZE_SET(self->size_inc, 1, 1);
782     SIZE_SET(self->base_size, 0, 0);
783     SIZE_SET(self->min_size, 0, 0);
784     SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
785
786     /* get the hints from the window */
787     if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
788         self->positioned = !!(size.flags & (PPosition|USPosition));
789
790         if (size.flags & PWinGravity) {
791             self->gravity = size.win_gravity;
792       
793             /* if the client has a frame, i.e. has already been mapped and
794                is changing its gravity */
795             if (self->frame && self->gravity != oldgravity) {
796                 /* move our idea of the client's position based on its new
797                    gravity */
798                 self->area.x = self->frame->area.x;
799                 self->area.y = self->frame->area.y;
800                 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
801             }
802         }
803
804         if (size.flags & PAspect) {
805             if (size.min_aspect.y)
806                 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
807             if (size.max_aspect.y)
808                 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
809         }
810
811         if (size.flags & PMinSize)
812             SIZE_SET(self->min_size, size.min_width, size.min_height);
813     
814         if (size.flags & PMaxSize)
815             SIZE_SET(self->max_size, size.max_width, size.max_height);
816     
817         if (size.flags & PBaseSize)
818             SIZE_SET(self->base_size, size.base_width, size.base_height);
819     
820         if (size.flags & PResizeInc)
821             SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
822     }
823 }
824
825 void client_setup_decor_and_functions(Client *self)
826 {
827     /* start with everything (cept fullscreen) */
828     self->decorations = Decor_Titlebar | Decor_Handle | Decor_Border |
829         Decor_Icon | Decor_AllDesktops | Decor_Iconify | Decor_Maximize |
830         Decor_Shade;
831     self->functions = Func_Resize | Func_Move | Func_Iconify | Func_Maximize |
832         Func_Shade;
833     if (self->delete_window) {
834         self->decorations |= Decor_Close;
835         self->functions |= Func_Close;
836     }
837
838     if (!(self->min_size.width < self->max_size.width ||
839           self->min_size.height < self->max_size.height)) {
840         self->decorations &= ~(Decor_Maximize | Decor_Handle);
841         self->functions &= ~(Func_Resize | Func_Maximize);
842     }
843
844     switch (self->type) {
845     case Type_Normal:
846         /* normal windows retain all of the possible decorations and
847            functionality, and are the only windows that you can fullscreen */
848         self->functions |= Func_Fullscreen;
849         break;
850
851     case Type_Dialog:
852     case Type_Utility:
853         /* these windows cannot be maximized */
854         self->decorations &= ~Decor_Maximize;
855         self->functions &= ~Func_Maximize;
856         break;
857
858     case Type_Menu:
859     case Type_Toolbar:
860         /* these windows get less functionality */
861         self->decorations &= ~(Decor_Iconify | Decor_Handle);
862         self->functions &= ~(Func_Iconify | Func_Resize);
863         break;
864
865     case Type_Desktop:
866     case Type_Dock:
867     case Type_Splash:
868         /* none of these windows are manipulated by the window manager */
869         self->decorations = 0;
870         self->functions = 0;
871         break;
872     }
873
874     /* Mwm Hints are applied subtractively to what has already been chosen for
875        decor and functionality */
876     if (self->mwmhints.flags & MwmFlag_Decorations) {
877         if (! (self->mwmhints.decorations & MwmDecor_All)) {
878             if (! (self->mwmhints.decorations & MwmDecor_Border))
879                 self->decorations &= ~Decor_Border;
880             if (! (self->mwmhints.decorations & MwmDecor_Handle))
881                 self->decorations &= ~Decor_Handle;
882             if (! (self->mwmhints.decorations & MwmDecor_Title))
883                 self->decorations &= ~Decor_Titlebar;
884             if (! (self->mwmhints.decorations & MwmDecor_Iconify))
885                 self->decorations &= ~Decor_Iconify;
886             if (! (self->mwmhints.decorations & MwmDecor_Maximize))
887                 self->decorations &= ~Decor_Maximize;
888         }
889     }
890
891     if (self->mwmhints.flags & MwmFlag_Functions) {
892         if (! (self->mwmhints.functions & MwmFunc_All)) {
893             if (! (self->mwmhints.functions & MwmFunc_Resize))
894                 self->functions &= ~Func_Resize;
895             if (! (self->mwmhints.functions & MwmFunc_Move))
896                 self->functions &= ~Func_Move;
897             if (! (self->mwmhints.functions & MwmFunc_Iconify))
898                 self->functions &= ~Func_Iconify;
899             if (! (self->mwmhints.functions & MwmFunc_Maximize))
900                 self->functions &= ~Func_Maximize;
901             /* dont let mwm hints kill the close button
902                if (! (self->mwmhints.functions & MwmFunc_Close))
903                self->functions &= ~Func_Close; */
904         }
905     }
906
907     /* can't maximize without moving/resizing */
908     if (!((self->functions & Func_Move) && (self->functions & Func_Resize)))
909         self->functions &= ~(Func_Maximize | Func_Fullscreen);
910
911     /* finally, user specified disabled decorations are applied to subtract
912        decorations */
913     if (self->disabled_decorations & Decor_Titlebar)
914         self->decorations &= ~Decor_Titlebar;
915     if (self->disabled_decorations & Decor_Handle)
916         self->decorations &= ~Decor_Handle;
917     if (self->disabled_decorations & Decor_Border)
918         self->decorations &= ~Decor_Border;
919     if (self->disabled_decorations & Decor_Iconify)
920         self->decorations &= ~Decor_Iconify;
921     if (self->disabled_decorations & Decor_Maximize)
922         self->decorations &= ~Decor_Maximize;
923     if (self->disabled_decorations & Decor_AllDesktops)
924         self->decorations &= ~Decor_AllDesktops;
925     if (self->disabled_decorations & Decor_Shade)
926         self->decorations &= ~Decor_Shade;
927     if (self->disabled_decorations & Decor_Close)
928         self->decorations &= ~Decor_Close;
929
930     /* if we don't have a titlebar, then we cannot shade! */
931     if (!(self->decorations & Decor_Titlebar))
932         self->functions &= ~Func_Shade;
933
934     /* now we need to check against rules for the client's current state */
935     if (self->fullscreen) {
936         self->functions &= (Func_Close | Func_Fullscreen | Func_Iconify);
937         self->decorations = 0;
938     }
939
940     client_change_allowed_actions(self);
941
942     if (self->frame) {
943         /* change the decors on the frame, and with more/less decorations,
944            we may also need to be repositioned */
945         frame_adjust_area(self->frame, TRUE, TRUE);
946         /* with new decor, the window's maximized size may change */
947         client_remaximize(self);
948     }
949 }
950
951 static void client_change_allowed_actions(Client *self)
952 {
953     guint32 actions[9];
954     int num = 0;
955
956     actions[num++] = prop_atoms.net_wm_action_change_desktop;
957
958     if (self->functions & Func_Shade)
959         actions[num++] = prop_atoms.net_wm_action_shade;
960     if (self->functions & Func_Close)
961         actions[num++] = prop_atoms.net_wm_action_close;
962     if (self->functions & Func_Move)
963         actions[num++] = prop_atoms.net_wm_action_move;
964     if (self->functions & Func_Iconify)
965         actions[num++] = prop_atoms.net_wm_action_minimize;
966     if (self->functions & Func_Resize)
967         actions[num++] = prop_atoms.net_wm_action_resize;
968     if (self->functions & Func_Fullscreen)
969         actions[num++] = prop_atoms.net_wm_action_fullscreen;
970     if (self->functions & Func_Maximize) {
971         actions[num++] = prop_atoms.net_wm_action_maximize_horz;
972         actions[num++] = prop_atoms.net_wm_action_maximize_vert;
973     }
974
975     PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
976
977     /* make sure the window isn't breaking any rules now */
978
979     if (!(self->functions & Func_Shade) && self->shaded) {
980         if (self->frame) client_shade(self, FALSE);
981         else self->shaded = FALSE;
982     }
983     if (!(self->functions & Func_Iconify) && self->iconic) {
984         g_message("UNSETTING ICONIC");
985         if (self->frame) client_iconify(self, FALSE, TRUE);
986         else self->iconic = FALSE;
987     }
988     if (!(self->functions & Func_Fullscreen) && self->fullscreen) {
989         if (self->frame) client_fullscreen(self, FALSE, TRUE);
990         else self->fullscreen = FALSE;
991     }
992     if (!(self->functions & Func_Maximize) && (self->max_horz ||
993                                                self->max_vert)) {
994         if (self->frame) client_maximize(self, FALSE, 0, TRUE);
995         else self->max_vert = self->max_horz = FALSE;
996     }
997 }
998
999 void client_remaximize(Client *self)
1000 {
1001     int dir;
1002     if (self->max_horz && self->max_vert)
1003         dir = 0;
1004     else if (self->max_horz)
1005         dir = 1;
1006     else if (self->max_vert)
1007         dir = 2;
1008     else
1009         return; /* not maximized */
1010     self->max_horz = self->max_vert = FALSE;
1011     client_maximize(self, TRUE, dir, FALSE);
1012 }
1013
1014 void client_update_wmhints(Client *self)
1015 {
1016     XWMHints *hints;
1017     gboolean ur = FALSE;
1018     GSList *it;
1019
1020     /* assume a window takes input if it doesnt specify */
1021     self->can_focus = TRUE;
1022   
1023     if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1024         if (hints->flags & InputHint)
1025             self->can_focus = hints->input;
1026
1027         /* only do this when first managing the window *AND* when we aren't
1028            starting up! */
1029         if (ob_state != State_Starting && self->frame == NULL)
1030             if (hints->flags & StateHint)
1031                 self->iconic = hints->initial_state == IconicState;
1032
1033         if (hints->flags & XUrgencyHint)
1034             ur = TRUE;
1035
1036         if (!(hints->flags & WindowGroupHint))
1037             hints->window_group = None; /* no group */
1038         /* did the group state change? */
1039         if (hints->window_group != (self->group ? self->group->leader : None)){
1040             /* remove from the old group if there was one */
1041             if (self->group != NULL)
1042                 group_remove(self->group, self);
1043             if (hints->window_group != None)
1044                 self->group = group_add(hints->window_group, self);
1045
1046             /* add other transients of the group that are already set up */
1047             for (it = self->group->members; it; it = it->next)
1048                 if (it->data != self &&
1049                     ((Client*)it->data)->transient_for == TRAN_GROUP)
1050                     self->transients = g_slist_append(self->transients,
1051                                                       it->data);
1052
1053             /* because the self->transient flag wont change from this call,
1054                we don't need to update the window's type and such, only its
1055                transient_for, and the transients lists of other windows in the
1056                group may be affected */
1057             client_update_transient_for(self);
1058         }
1059
1060         if (hints->flags & IconPixmapHint) {
1061             client_update_kwm_icon(self);
1062             /* try get the kwm icon first, this is a fallback only */
1063             if (self->pixmap_icon == None) {
1064                 self->pixmap_icon = hints->icon_pixmap;
1065                 if (hints->flags & IconMaskHint)
1066                     self->pixmap_icon_mask = hints->icon_mask;
1067                 else
1068                     self->pixmap_icon_mask = None;
1069
1070                 if (self->frame)
1071                     frame_adjust_icon(self->frame);
1072             }
1073         }
1074
1075         XFree(hints);
1076     }
1077
1078     if (ur != self->urgent) {
1079         self->urgent = ur;
1080         g_message("Urgent Hint for 0x%lx: %s", self->window,
1081                   ur ? "ON" : "OFF");
1082         /* fire the urgent callback if we're mapped, otherwise, wait until
1083            after we're mapped */
1084         if (self->frame)
1085             dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1086     }
1087 }
1088
1089 void client_update_title(Client *self)
1090 {
1091     char *data = NULL;
1092
1093     g_free(self->title);
1094      
1095     /* try netwm */
1096     if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1097         /* try old x stuff */
1098         if (!PROP_GETS(self->window, wm_name, locale, &data))
1099             data = g_strdup("Unnamed Window");
1100
1101     /* look for duplicates and append a number */
1102
1103     PROP_SETS(self->window, net_wm_visible_name, data);
1104
1105     self->title = data;
1106
1107     if (self->frame)
1108         frame_adjust_title(self->frame);
1109 }
1110
1111 void client_update_icon_title(Client *self)
1112 {
1113     char *data = NULL;
1114
1115     g_free(self->icon_title);
1116      
1117     /* try netwm */
1118     if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1119         /* try old x stuff */
1120         if (!PROP_GETS(self->window, wm_icon_name, locale, &data))
1121             data = g_strdup("Unnamed Window");
1122
1123     PROP_SETS(self->window, net_wm_visible_icon_name, data);
1124
1125     self->icon_title = data;
1126 }
1127
1128 void client_update_class(Client *self)
1129 {
1130     char **data;
1131     char *s;
1132
1133     if (self->name) g_free(self->name);
1134     if (self->class) g_free(self->class);
1135     if (self->role) g_free(self->role);
1136
1137     self->name = self->class = self->role = NULL;
1138
1139     if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1140         if (data[0]) {
1141             self->name = g_strdup(data[0]);
1142             if (data[1])
1143                 self->class = g_strdup(data[1]);
1144         }
1145         g_strfreev(data);     
1146     }
1147
1148     if (PROP_GETS(self->window, wm_window_role, locale, &s))
1149         self->role = g_strdup(s);
1150
1151     if (self->name == NULL) self->name = g_strdup("");
1152     if (self->class == NULL) self->class = g_strdup("");
1153     if (self->role == NULL) self->role = g_strdup("");
1154 }
1155
1156 void client_update_strut(Client *self)
1157 {
1158     guint num;
1159     guint32 *data;
1160
1161     if (!PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1162         STRUT_SET(self->strut, 0, 0, 0, 0);
1163     } else {
1164         if (num == 4)
1165             STRUT_SET(self->strut, data[0], data[2], data[1], data[3]);
1166         else
1167             STRUT_SET(self->strut, 0, 0, 0, 0);
1168         g_free(data);
1169     }
1170
1171     /* updating here is pointless while we're being mapped cuz we're not in
1172        the client list yet */
1173     if (self->frame)
1174         screen_update_struts();
1175 }
1176
1177 void client_update_icons(Client *self)
1178 {
1179     guint num;
1180     guint32 *data;
1181     guint w, h, i;
1182     int j;
1183
1184     for (j = 0; j < self->nicons; ++j)
1185         g_free(self->icons[j].data);
1186     if (self->nicons > 0)
1187         g_free(self->icons);
1188     self->nicons = 0;
1189
1190     if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1191         /* figure out how many valid icons are in here */
1192         i = 0;
1193         while (num - i > 2) {
1194             w = data[i++];
1195             h = data[i++];
1196             i += w * h;
1197             if (i > num) break;
1198             ++self->nicons;
1199         }
1200
1201         self->icons = g_new(Icon, self->nicons);
1202     
1203         /* store the icons */
1204         i = 0;
1205         for (j = 0; j < self->nicons; ++j) {
1206             w = self->icons[j].width = data[i++];
1207             h = self->icons[j].height = data[i++];
1208             self->icons[j].data =
1209                 g_memdup(&data[i], w * h * sizeof(gulong));
1210             i += w * h;
1211             g_assert(i <= num);
1212         }
1213
1214         g_free(data);
1215     }
1216
1217     if (self->frame)
1218         frame_adjust_icon(self->frame);
1219 }
1220
1221 void client_update_kwm_icon(Client *self)
1222 {
1223     guint num;
1224     guint32 *data;
1225
1226     if (!PROP_GETA32(self->window, kwm_win_icon, kwm_win_icon, &data, &num)) {
1227         self->pixmap_icon = self->pixmap_icon_mask = None;
1228     } else {
1229         if (num == 2) {
1230             self->pixmap_icon = data[0];
1231             self->pixmap_icon_mask = data[1];
1232         } else
1233             self->pixmap_icon = self->pixmap_icon_mask = None;
1234         g_free(data);
1235     }
1236     if (self->frame)
1237         frame_adjust_icon(self->frame);
1238 }
1239
1240 static void client_change_state(Client *self)
1241 {
1242     guint32 state[2];
1243     guint32 netstate[10];
1244     guint num;
1245
1246     state[0] = self->wmstate;
1247     state[1] = None;
1248     PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1249
1250     num = 0;
1251     if (self->modal)
1252         netstate[num++] = prop_atoms.net_wm_state_modal;
1253     if (self->shaded)
1254         netstate[num++] = prop_atoms.net_wm_state_shaded;
1255     if (self->iconic)
1256         netstate[num++] = prop_atoms.net_wm_state_hidden;
1257     if (self->skip_taskbar)
1258         netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1259     if (self->skip_pager)
1260         netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1261     if (self->fullscreen)
1262         netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1263     if (self->max_vert)
1264         netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1265     if (self->max_horz)
1266         netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1267     if (self->above)
1268         netstate[num++] = prop_atoms.net_wm_state_above;
1269     if (self->below)
1270         netstate[num++] = prop_atoms.net_wm_state_below;
1271     PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1272
1273     client_calc_layer(self);
1274
1275     if (self->frame)
1276         frame_adjust_state(self->frame);
1277 }
1278
1279 static Client *search_focus_tree(Client *node, Client *skip)
1280 {
1281     GSList *it;
1282     Client *ret;
1283
1284     for (it = node->transients; it != NULL; it = it->next) {
1285         Client *c = it->data;
1286         if (c == skip) continue; /* circular? */
1287         if ((ret = search_focus_tree(c, skip))) return ret;
1288         if (client_focused(c)) return c;
1289     }
1290     return NULL;
1291 }
1292
1293 static void calc_recursive(Client *self, StackLayer l, gboolean raised)
1294 {
1295     StackLayer old;
1296     GSList *it;
1297
1298     old = self->layer;
1299     self->layer = l;
1300
1301     for (it = self->transients; it; it = it->next)
1302         calc_recursive(it->data, l, raised ? raised : l != old);
1303
1304     if (!raised && l != old)
1305         if (self->frame)
1306             stacking_raise(self);
1307 }
1308
1309 void client_calc_layer(Client *self)
1310 {
1311     StackLayer l;
1312     gboolean f;
1313
1314     /* transients take on the layer of their parents */
1315     if (self->transient_for) {
1316         if (self->transient_for != TRAN_GROUP) {
1317             self = self->transient_for;
1318         } else {
1319             GSList *it;
1320
1321             for (it = self->group->members; it; it = it->next)
1322                 if (it->data != self &&
1323                     ((Client*)it->data)->transient_for != TRAN_GROUP) {
1324                     self = it->data;
1325                     break;
1326                 }
1327         }
1328     }
1329
1330     /* is us or one of our transients focused? */
1331     if (client_focused(self))
1332         f = TRUE;
1333     else if (search_focus_tree(self, self))
1334         f = TRUE;
1335     else
1336         f = FALSE;
1337
1338     if (self->iconic) l = Layer_Icon;
1339     /* fullscreen windows are only in the fullscreen layer while focused */
1340     else if (self->fullscreen && f) l = Layer_Fullscreen;
1341     else if (self->type == Type_Desktop) l = Layer_Desktop;
1342     else if (self->type == Type_Dock) {
1343         if (!self->below) l = Layer_Top;
1344         else l = Layer_Normal;
1345     }
1346     else if (self->above) l = Layer_Above;
1347     else if (self->below) l = Layer_Below;
1348     else l = Layer_Normal;
1349
1350     calc_recursive(self, l, FALSE);
1351 }
1352
1353 gboolean client_should_show(Client *self)
1354 {
1355     if (self->iconic) return FALSE;
1356     else if (!(self->desktop == screen_desktop ||
1357                self->desktop == DESKTOP_ALL)) return FALSE;
1358     else if (client_normal(self) && screen_showing_desktop) return FALSE;
1359     
1360     return TRUE;
1361 }
1362
1363 static void client_showhide(Client *self)
1364 {
1365
1366     if (client_should_show(self))
1367         frame_show(self->frame);
1368     else
1369         frame_hide(self->frame);
1370 }
1371
1372 gboolean client_normal(Client *self) {
1373     return ! (self->type == Type_Desktop || self->type == Type_Dock ||
1374               self->type == Type_Splash);
1375 }
1376
1377 static void client_apply_startup_state(Client *self)
1378 {
1379     /* these are in a carefully crafted order.. */
1380
1381     if (self->iconic) {
1382         self->iconic = FALSE;
1383         client_iconify(self, TRUE, FALSE);
1384     }
1385     if (self->fullscreen) {
1386         self->fullscreen = FALSE;
1387         client_fullscreen(self, TRUE, FALSE);
1388     }
1389     if (self->shaded) {
1390         self->shaded = FALSE;
1391         client_shade(self, TRUE);
1392     }
1393     if (self->urgent)
1394         dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1395   
1396     if (self->max_vert && self->max_horz) {
1397         self->max_vert = self->max_horz = FALSE;
1398         client_maximize(self, TRUE, 0, FALSE);
1399     } else if (self->max_vert) {
1400         self->max_vert = FALSE;
1401         client_maximize(self, TRUE, 2, FALSE);
1402     } else if (self->max_horz) {
1403         self->max_horz = FALSE;
1404         client_maximize(self, TRUE, 1, FALSE);
1405     }
1406
1407     /* nothing to do for the other states:
1408        skip_taskbar
1409        skip_pager
1410        modal
1411        above
1412        below
1413     */
1414 }
1415
1416 void client_configure(Client *self, Corner anchor, int x, int y, int w, int h,
1417                       gboolean user, gboolean final)
1418 {
1419     gboolean moved = FALSE, resized = FALSE;
1420
1421     /* gets the frame's position */
1422     frame_client_gravity(self->frame, &x, &y);
1423
1424     /* these positions are frame positions, not client positions */
1425
1426     /* set the size and position if fullscreen */
1427     if (self->fullscreen) {
1428         x = 0;
1429         y = 0;
1430         w = screen_physical_size.width;
1431         h = screen_physical_size.height;
1432         user = FALSE; /* ignore that increment etc shit when in fullscreen */
1433     } else {
1434         /* set the size and position if maximized */
1435         if (self->max_horz) {
1436             x = screen_area(self->desktop)->x - self->frame->size.left;
1437             w = screen_area(self->desktop)->width;
1438         }
1439         if (self->max_vert) {
1440             y = screen_area(self->desktop)->y;
1441             h = screen_area(self->desktop)->height -
1442                 self->frame->size.top - self->frame->size.bottom;
1443         }
1444     }
1445
1446     /* gets the client's position */
1447     frame_frame_gravity(self->frame, &x, &y);
1448
1449     /* these override the above states! if you cant move you can't move! */
1450     if (user) {
1451         if (!(self->functions & Func_Move)) {
1452             x = self->area.x;
1453             y = self->area.y;
1454         }
1455         if (!(self->functions & Func_Resize)) {
1456             w = self->area.width;
1457             h = self->area.height;
1458         }
1459     }
1460
1461     if (!(w == self->area.width && h == self->area.height)) {
1462         w -= self->base_size.width;
1463         h -= self->base_size.height;
1464
1465         if (user) {
1466             /* for interactive resizing. have to move half an increment in each
1467                direction. */
1468
1469             /* how far we are towards the next size inc */
1470             int mw = w % self->size_inc.width; 
1471             int mh = h % self->size_inc.height;
1472             /* amount to add */
1473             int aw = self->size_inc.width / 2;
1474             int ah = self->size_inc.height / 2;
1475             /* don't let us move into a new size increment */
1476             if (mw + aw >= self->size_inc.width)
1477                 aw = self->size_inc.width - mw - 1;
1478             if (mh + ah >= self->size_inc.height)
1479                 ah = self->size_inc.height - mh - 1;
1480             w += aw;
1481             h += ah;
1482     
1483             /* if this is a user-requested resize, then check against min/max
1484                sizes and aspect ratios */
1485
1486             /* smaller than min size or bigger than max size? */
1487             if (w > self->max_size.width) w = self->max_size.width;
1488             if (w < self->min_size.width) w = self->min_size.width;
1489             if (h > self->max_size.height) h = self->max_size.height;
1490             if (h < self->min_size.height) h = self->min_size.height;
1491
1492             /* adjust the height ot match the width for the aspect ratios */
1493             if (self->min_ratio)
1494                 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1495             if (self->max_ratio)
1496                 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1497         }
1498
1499         /* keep to the increments */
1500         w /= self->size_inc.width;
1501         h /= self->size_inc.height;
1502
1503         /* you cannot resize to nothing */
1504         if (w < 1) w = 1;
1505         if (h < 1) h = 1;
1506   
1507         /* store the logical size */
1508         SIZE_SET(self->logical_size, w, h);
1509
1510         w *= self->size_inc.width;
1511         h *= self->size_inc.height;
1512
1513         w += self->base_size.width;
1514         h += self->base_size.height;
1515     }
1516
1517     switch (anchor) {
1518     case Corner_TopLeft:
1519         break;
1520     case Corner_TopRight:
1521         x -= w - self->area.width;
1522         break;
1523     case Corner_BottomLeft:
1524         y -= h - self->area.height;
1525         break;
1526     case Corner_BottomRight:
1527         x -= w - self->area.width;
1528         y -= h - self->area.height;
1529         break;
1530     }
1531
1532     moved = x != self->area.x || y != self->area.y;
1533     resized = w != self->area.width || h != self->area.height;
1534
1535     RECT_SET(self->area, x, y, w, h);
1536
1537     if (resized)
1538         XResizeWindow(ob_display, self->window, w, h);
1539
1540     /* move/resize the frame to match the request */
1541     if (self->frame) {
1542         if (moved || resized)
1543             frame_adjust_area(self->frame, moved, resized);
1544
1545         if (!user || final) {
1546             XEvent event;
1547             event.type = ConfigureNotify;
1548             event.xconfigure.display = ob_display;
1549             event.xconfigure.event = self->window;
1550             event.xconfigure.window = self->window;
1551     
1552             /* root window coords with border in mind */
1553             event.xconfigure.x = x - self->border_width +
1554                 self->frame->size.left;
1555             event.xconfigure.y = y - self->border_width +
1556                 self->frame->size.top;
1557     
1558             event.xconfigure.width = self->area.width;
1559             event.xconfigure.height = self->area.height;
1560             event.xconfigure.border_width = self->border_width;
1561             event.xconfigure.above = self->frame->plate;
1562             event.xconfigure.override_redirect = FALSE;
1563             XSendEvent(event.xconfigure.display, event.xconfigure.window,
1564                        FALSE, StructureNotifyMask, &event);
1565         }
1566     }
1567 }
1568
1569 void client_fullscreen(Client *self, gboolean fs, gboolean savearea)
1570 {
1571     int x, y, w, h;
1572
1573     if (!(self->functions & Func_Fullscreen) || /* can't */
1574         self->fullscreen == fs) return;         /* already done */
1575
1576     self->fullscreen = fs;
1577     client_change_state(self); /* change the state hints on the client,
1578                                   and adjust out layer/stacking */
1579
1580     if (fs) {
1581         if (savearea) {
1582             guint32 dimensions[4];
1583             dimensions[0] = self->area.x;
1584             dimensions[1] = self->area.y;
1585             dimensions[2] = self->area.width;
1586             dimensions[3] = self->area.height;
1587   
1588             PROP_SETA32(self->window, openbox_premax, cardinal,
1589                         dimensions, 4);
1590         }
1591
1592         /* these are not actually used cuz client_configure will set them
1593            as appropriate when the window is fullscreened */
1594         x = y = w = h = 0;
1595     } else {
1596         guint num;
1597         gint32 *dimensions;
1598
1599         /* pick some fallbacks... */
1600         x = screen_area(self->desktop)->x +
1601             screen_area(self->desktop)->width / 4;
1602         y = screen_area(self->desktop)->y +
1603             screen_area(self->desktop)->height / 4;
1604         w = screen_area(self->desktop)->width / 2;
1605         h = screen_area(self->desktop)->height / 2;
1606
1607         if (PROP_GETA32(self->window, openbox_premax, cardinal,
1608                         (guint32**)&dimensions, &num)) {
1609             if (num == 4) {
1610                 x = dimensions[0];
1611                 y = dimensions[1];
1612                 w = dimensions[2];
1613                 h = dimensions[3];
1614             }
1615             g_free(dimensions);
1616         }
1617     }
1618
1619     client_setup_decor_and_functions(self);
1620
1621     client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1622
1623     /* try focus us when we go into fullscreen mode */
1624     client_focus(self);
1625 }
1626
1627 void client_iconify(Client *self, gboolean iconic, gboolean curdesk)
1628 {
1629     GSList *it;
1630
1631     /* move up the transient chain as far as possible first if deiconifying */
1632     if (!iconic)
1633         while (self->transient_for) {
1634             if (self->transient_for != TRAN_GROUP) {
1635                 if (self->transient_for->iconic == iconic)
1636                     break;
1637                 self = self->transient_for;
1638             } else {
1639                 GSList *it;
1640
1641                 /* the check for TRAN_GROUP is to prevent an infinate loop with
1642                    2 transients of the same group at the head of the group's
1643                    members list */
1644                 for (it = self->group->members; it; it = it->next) {
1645                     Client *c = it->data;
1646
1647                     if (c != self && c->transient_for->iconic != iconic &&
1648                         c->transient_for != TRAN_GROUP) {
1649                         self = it->data;
1650                         break;
1651                     }
1652                 }
1653                 if (it == NULL) break;
1654             }
1655         }
1656
1657     if (self->iconic == iconic) return; /* nothing to do */
1658
1659     g_message("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
1660               self->window);
1661
1662     self->iconic = iconic;
1663
1664     if (iconic) {
1665         self->wmstate = IconicState;
1666         self->ignore_unmaps++;
1667         /* we unmap the client itself so that we can get MapRequest events,
1668            and because the ICCCM tells us to! */
1669         XUnmapWindow(ob_display, self->window);
1670     } else {
1671         if (curdesk)
1672             client_set_desktop(self, screen_desktop, FALSE);
1673         self->wmstate = self->shaded ? IconicState : NormalState;
1674         XMapWindow(ob_display, self->window);
1675     }
1676     client_change_state(self);
1677     client_showhide(self);
1678     screen_update_struts();
1679
1680     dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
1681                     self, 0, 0);
1682
1683     /* iconify all transients */
1684     for (it = self->transients; it != NULL; it = it->next)
1685         if (it->data != self) client_iconify(it->data, iconic, curdesk);
1686 }
1687
1688 void client_maximize(Client *self, gboolean max, int dir, gboolean savearea)
1689 {
1690     int x, y, w, h;
1691      
1692     g_assert(dir == 0 || dir == 1 || dir == 2);
1693     if (!(self->functions & Func_Maximize)) return; /* can't */
1694
1695     /* check if already done */
1696     if (max) {
1697         if (dir == 0 && self->max_horz && self->max_vert) return;
1698         if (dir == 1 && self->max_horz) return;
1699         if (dir == 2 && self->max_vert) return;
1700     } else {
1701         if (dir == 0 && !self->max_horz && !self->max_vert) return;
1702         if (dir == 1 && !self->max_horz) return;
1703         if (dir == 2 && !self->max_vert) return;
1704     }
1705
1706     /* work with the frame's coords */
1707     x = self->frame->area.x;
1708     y = self->frame->area.y;
1709     w = self->area.width;
1710     h = self->area.height;
1711
1712     if (max) {
1713         if (savearea) {
1714             gint32 dimensions[4];
1715             gint32 *readdim;
1716             guint num;
1717
1718             dimensions[0] = x;
1719             dimensions[1] = y;
1720             dimensions[2] = w;
1721             dimensions[3] = h;
1722
1723             /* get the property off the window and use it for the dimensions
1724                we are already maxed on */
1725             if (PROP_GETA32(self->window, openbox_premax, cardinal,
1726                             (guint32**)&readdim, &num)) {
1727                 if (num == 4) {
1728                     if (self->max_horz) {
1729                         dimensions[0] = readdim[0];
1730                         dimensions[2] = readdim[2];
1731                     }
1732                     if (self->max_vert) {
1733                         dimensions[1] = readdim[1];
1734                         dimensions[3] = readdim[3];
1735                     }
1736                 }
1737                 g_free(readdim);
1738             }
1739
1740             PROP_SETA32(self->window, openbox_premax, cardinal,
1741                         (guint32*)dimensions, 4);
1742         }
1743     } else {
1744         guint num;
1745         gint32 *dimensions;
1746
1747         /* pick some fallbacks... */
1748         if (dir == 0 || dir == 1) { /* horz */
1749             x = screen_area(self->desktop)->x +
1750                 screen_area(self->desktop)->width / 4;
1751             w = screen_area(self->desktop)->width / 2;
1752         }
1753         if (dir == 0 || dir == 2) { /* vert */
1754             y = screen_area(self->desktop)->y +
1755                 screen_area(self->desktop)->height / 4;
1756             h = screen_area(self->desktop)->height / 2;
1757         }
1758
1759         if (PROP_GETA32(self->window, openbox_premax, cardinal,
1760                         (guint32**)&dimensions, &num)) {
1761             if (num == 4) {
1762                 if (dir == 0 || dir == 1) { /* horz */
1763                     x = dimensions[0];
1764                     w = dimensions[2];
1765                 }
1766                 if (dir == 0 || dir == 2) { /* vert */
1767                     y = dimensions[1];
1768                     h = dimensions[3];
1769                 }
1770             }
1771             g_free(dimensions);
1772         }
1773     }
1774
1775     if (dir == 0 || dir == 1) /* horz */
1776         self->max_horz = max;
1777     if (dir == 0 || dir == 2) /* vert */
1778         self->max_vert = max;
1779
1780     if (!self->max_horz && !self->max_vert)
1781         PROP_ERASE(self->window, openbox_premax);
1782
1783     client_change_state(self); /* change the state hints on the client */
1784
1785     /* figure out where the client should be going */
1786     frame_frame_gravity(self->frame, &x, &y);
1787     client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1788 }
1789
1790 void client_shade(Client *self, gboolean shade)
1791 {
1792     if ((!(self->functions & Func_Shade) && shade) || /* can't shade */
1793         self->shaded == shade) return;     /* already done */
1794
1795     /* when we're iconic, don't change the wmstate */
1796     if (!self->iconic)
1797         self->wmstate = shade ? IconicState : NormalState;
1798     self->shaded = shade;
1799     client_change_state(self);
1800     /* resize the frame to just the titlebar */
1801     frame_adjust_area(self->frame, FALSE, FALSE);
1802 }
1803
1804 void client_close(Client *self)
1805 {
1806     XEvent ce;
1807
1808     if (!(self->functions & Func_Close)) return;
1809
1810     /*
1811       XXX: itd be cool to do timeouts and shit here for killing the client's
1812       process off
1813       like... if the window is around after 5 seconds, then the close button
1814       turns a nice red, and if this function is called again, the client is
1815       explicitly killed.
1816     */
1817
1818     ce.xclient.type = ClientMessage;
1819     ce.xclient.message_type =  prop_atoms.wm_protocols;
1820     ce.xclient.display = ob_display;
1821     ce.xclient.window = self->window;
1822     ce.xclient.format = 32;
1823     ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
1824     ce.xclient.data.l[1] = event_lasttime;
1825     ce.xclient.data.l[2] = 0l;
1826     ce.xclient.data.l[3] = 0l;
1827     ce.xclient.data.l[4] = 0l;
1828     XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
1829 }
1830
1831 void client_kill(Client *self)
1832 {
1833     XKillClient(ob_display, self->window);
1834 }
1835
1836 void client_set_desktop(Client *self, guint target, gboolean donthide)
1837 {
1838     guint old, i;
1839
1840     if (target == self->desktop) return;
1841   
1842     g_message("Setting desktop %u", target);
1843
1844     g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
1845
1846     old = self->desktop;
1847     self->desktop = target;
1848     PROP_SET32(self->window, net_wm_desktop, cardinal, target);
1849     /* the frame can display the current desktop state */
1850     frame_adjust_state(self->frame);
1851     /* 'move' the window to the new desktop */
1852     if (!donthide)
1853         client_showhide(self);
1854     /* raise if it was not already on the desktop */
1855     if (old != DESKTOP_ALL)
1856         stacking_raise(self);
1857     screen_update_struts();
1858
1859     /* update the focus lists */
1860     if (old == DESKTOP_ALL) {
1861         for (i = 0; i < screen_num_desktops; ++i)
1862             focus_order[i] = g_list_remove(focus_order[i], self);
1863     } else
1864         focus_order[old] = g_list_remove(focus_order[old], self);
1865     if (target == DESKTOP_ALL) {
1866         for (i = 0; i < screen_num_desktops; ++i) {
1867             if (config_focus_new)
1868                 focus_order[i] = g_list_prepend(focus_order[i], self);
1869             else
1870                 focus_order[i] = g_list_append(focus_order[i], self);
1871         }
1872     } else {
1873         if (config_focus_new)
1874             focus_order[target] = g_list_prepend(focus_order[target], self);
1875         else
1876             focus_order[target] = g_list_append(focus_order[target], self);
1877     }
1878
1879     dispatch_client(Event_Client_Desktop, self, target, old);
1880 }
1881
1882 static Client *search_modal_tree(Client *node, Client *skip)
1883 {
1884     GSList *it;
1885     Client *ret;
1886   
1887     for (it = node->transients; it != NULL; it = it->next) {
1888         Client *c = it->data;
1889         if (c == skip) continue; /* circular? */
1890         if ((ret = search_modal_tree(c, skip))) return ret;
1891         if (c->modal) return c;
1892     }
1893     return NULL;
1894 }
1895
1896 Client *client_find_modal_child(Client *self)
1897 {
1898     return search_modal_tree(self, self);
1899 }
1900
1901 gboolean client_validate(Client *self)
1902 {
1903     XEvent e; 
1904
1905     XSync(ob_display, FALSE); /* get all events on the server */
1906
1907     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
1908         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
1909         XPutBackEvent(ob_display, &e);
1910         return FALSE;
1911     }
1912
1913     return TRUE;
1914 }
1915
1916 void client_set_wm_state(Client *self, long state)
1917 {
1918     if (state == self->wmstate) return; /* no change */
1919   
1920     switch (state) {
1921     case IconicState:
1922         client_iconify(self, TRUE, TRUE);
1923         break;
1924     case NormalState:
1925         client_iconify(self, FALSE, TRUE);
1926         break;
1927     }
1928 }
1929
1930 void client_set_state(Client *self, Atom action, long data1, long data2)
1931 {
1932     gboolean shaded = self->shaded;
1933     gboolean fullscreen = self->fullscreen;
1934     gboolean max_horz = self->max_horz;
1935     gboolean max_vert = self->max_vert;
1936     int i;
1937
1938     if (!(action == prop_atoms.net_wm_state_add ||
1939           action == prop_atoms.net_wm_state_remove ||
1940           action == prop_atoms.net_wm_state_toggle))
1941         /* an invalid action was passed to the client message, ignore it */
1942         return; 
1943
1944     for (i = 0; i < 2; ++i) {
1945         Atom state = i == 0 ? data1 : data2;
1946     
1947         if (!state) continue;
1948
1949         /* if toggling, then pick whether we're adding or removing */
1950         if (action == prop_atoms.net_wm_state_toggle) {
1951             if (state == prop_atoms.net_wm_state_modal)
1952                 action = self->modal ? prop_atoms.net_wm_state_remove :
1953                     prop_atoms.net_wm_state_add;
1954             else if (state == prop_atoms.net_wm_state_maximized_vert)
1955                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
1956                     prop_atoms.net_wm_state_add;
1957             else if (state == prop_atoms.net_wm_state_maximized_horz)
1958                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
1959                     prop_atoms.net_wm_state_add;
1960             else if (state == prop_atoms.net_wm_state_shaded)
1961                 action = self->shaded ? prop_atoms.net_wm_state_remove :
1962                     prop_atoms.net_wm_state_add;
1963             else if (state == prop_atoms.net_wm_state_skip_taskbar)
1964                 action = self->skip_taskbar ?
1965                     prop_atoms.net_wm_state_remove :
1966                     prop_atoms.net_wm_state_add;
1967             else if (state == prop_atoms.net_wm_state_skip_pager)
1968                 action = self->skip_pager ?
1969                     prop_atoms.net_wm_state_remove :
1970                     prop_atoms.net_wm_state_add;
1971             else if (state == prop_atoms.net_wm_state_fullscreen)
1972                 action = self->fullscreen ?
1973                     prop_atoms.net_wm_state_remove :
1974                     prop_atoms.net_wm_state_add;
1975             else if (state == prop_atoms.net_wm_state_above)
1976                 action = self->above ? prop_atoms.net_wm_state_remove :
1977                     prop_atoms.net_wm_state_add;
1978             else if (state == prop_atoms.net_wm_state_below)
1979                 action = self->below ? prop_atoms.net_wm_state_remove :
1980                     prop_atoms.net_wm_state_add;
1981         }
1982     
1983         if (action == prop_atoms.net_wm_state_add) {
1984             if (state == prop_atoms.net_wm_state_modal) {
1985                 /* XXX raise here or something? */
1986                 self->modal = TRUE;
1987             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
1988                 max_vert = TRUE;
1989             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
1990                 max_horz = TRUE;
1991             } else if (state == prop_atoms.net_wm_state_shaded) {
1992                 shaded = TRUE;
1993             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
1994                 self->skip_taskbar = TRUE;
1995             } else if (state == prop_atoms.net_wm_state_skip_pager) {
1996                 self->skip_pager = TRUE;
1997             } else if (state == prop_atoms.net_wm_state_fullscreen) {
1998                 fullscreen = TRUE;
1999             } else if (state == prop_atoms.net_wm_state_above) {
2000                 self->above = TRUE;
2001             } else if (state == prop_atoms.net_wm_state_below) {
2002                 self->below = TRUE;
2003             }
2004
2005         } else { /* action == prop_atoms.net_wm_state_remove */
2006             if (state == prop_atoms.net_wm_state_modal) {
2007                 self->modal = FALSE;
2008             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2009                 max_vert = FALSE;
2010             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2011                 max_horz = FALSE;
2012             } else if (state == prop_atoms.net_wm_state_shaded) {
2013                 shaded = FALSE;
2014             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2015                 self->skip_taskbar = FALSE;
2016             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2017                 self->skip_pager = FALSE;
2018             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2019                 fullscreen = FALSE;
2020             } else if (state == prop_atoms.net_wm_state_above) {
2021                 self->above = FALSE;
2022             } else if (state == prop_atoms.net_wm_state_below) {
2023                 self->below = FALSE;
2024             }
2025         }
2026     }
2027     if (max_horz != self->max_horz || max_vert != self->max_vert) {
2028         if (max_horz != self->max_horz && max_vert != self->max_vert) {
2029             /* toggling both */
2030             if (max_horz == max_vert) { /* both going the same way */
2031                 client_maximize(self, max_horz, 0, TRUE);
2032             } else {
2033                 client_maximize(self, max_horz, 1, TRUE);
2034                 client_maximize(self, max_vert, 2, TRUE);
2035             }
2036         } else {
2037             /* toggling one */
2038             if (max_horz != self->max_horz)
2039                 client_maximize(self, max_horz, 1, TRUE);
2040             else
2041                 client_maximize(self, max_vert, 2, TRUE);
2042         }
2043     }
2044     /* change fullscreen state before shading, as it will affect if the window
2045        can shade or not */
2046     if (fullscreen != self->fullscreen)
2047         client_fullscreen(self, fullscreen, TRUE);
2048     if (shaded != self->shaded)
2049         client_shade(self, shaded);
2050     client_calc_layer(self);
2051     client_change_state(self); /* change the hint to relect these changes */
2052 }
2053
2054 Client *client_focus_target(Client *self)
2055 {
2056     Client *child;
2057      
2058     /* if we have a modal child, then focus it, not us */
2059     child = client_find_modal_child(self);
2060     if (child) return child;
2061     return self;
2062 }
2063
2064 gboolean client_focusable(Client *self)
2065 {
2066     /* won't try focus if the client doesn't want it, or if the window isn't
2067        visible on the screen */
2068     return self->frame->visible &&
2069         (self->can_focus || self->focus_notify);
2070 }
2071
2072 gboolean client_focus(Client *self)
2073 {
2074     XEvent ev;
2075
2076     /* choose the correct target */
2077     self = client_focus_target(self);
2078
2079     if (!client_focusable(self))
2080         return FALSE;
2081
2082     /* do a check to see if the window has already been unmapped or destroyed
2083        do this intelligently while watching out for unmaps we've generated
2084        (ignore_unmaps > 0) */
2085     if (XCheckTypedWindowEvent(ob_display, self->window,
2086                                DestroyNotify, &ev)) {
2087         XPutBackEvent(ob_display, &ev);
2088         return FALSE;
2089     }
2090     while (XCheckTypedWindowEvent(ob_display, self->window,
2091                                   UnmapNotify, &ev)) {
2092         if (self->ignore_unmaps) {
2093             self->ignore_unmaps--;
2094         } else {
2095             XPutBackEvent(ob_display, &ev);
2096             return FALSE;
2097         }
2098     }
2099
2100     if (self->can_focus)
2101         /* RevertToPointerRoot causes much more headache than TevertToNone, so
2102            I choose to use it always, hopefully to find errors quicker, if any
2103            are left. (I hate X. I hate focus events.) */
2104         XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2105                        event_lasttime);
2106
2107     if (self->focus_notify) {
2108         XEvent ce;
2109         ce.xclient.type = ClientMessage;
2110         ce.xclient.message_type = prop_atoms.wm_protocols;
2111         ce.xclient.display = ob_display;
2112         ce.xclient.window = self->window;
2113         ce.xclient.format = 32;
2114         ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2115         ce.xclient.data.l[1] = event_lasttime;
2116         ce.xclient.data.l[2] = 0l;
2117         ce.xclient.data.l[3] = 0l;
2118         ce.xclient.data.l[4] = 0l;
2119         XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2120     }
2121
2122 #ifdef DEBUG_FOCUS
2123     g_message("focusing %lx", self->window);
2124 #endif
2125
2126     /* Cause the FocusIn to come back to us. Important for desktop switches,
2127        since otherwise we'll have no FocusIn on the queue and send it off to
2128        the focus_backup. */
2129     XSync(ob_display, FALSE);
2130     return TRUE;
2131 }
2132
2133 void client_unfocus(Client *self)
2134 {
2135     g_assert(focus_client == self);
2136 #ifdef DEBUG_FOCUS
2137     g_message("client_unfocus");
2138 #endif
2139     focus_fallback(Fallback_Unfocusing);
2140 }
2141
2142 gboolean client_focused(Client *self)
2143 {
2144     return self == focus_client;
2145 }
2146
2147 Icon *client_icon(Client *self, int w, int h)
2148 {
2149     int i;
2150     /* si is the smallest image >= req */
2151     /* li is the largest image < req */
2152     unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2153
2154     if (!self->nicons) return NULL;
2155
2156     for (i = 0; i < self->nicons; ++i) {
2157         size = self->icons[i].width * self->icons[i].height;
2158         if (size < smallest && size >= (unsigned)(w * h)) {
2159             smallest = size;
2160             si = i;
2161         }
2162         if (size > largest && size <= (unsigned)(w * h)) {
2163             largest = size;
2164             li = i;
2165         }
2166     }
2167     if (largest == 0) /* didnt find one smaller than the requested size */
2168         return &self->icons[si];
2169     return &self->icons[li];
2170 }