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