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