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