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