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