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