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