]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/client.c
adjust what gets raised on map a bit, a little less selective.
[mikachu/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             self->icons[j].data = g_new(pixel32, w * h);
1340             for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1341                 if (x >= w) {
1342                     x = 0;
1343                     ++y;
1344                 }
1345                 self->icons[j].data[t] =
1346                     (((data[i] >> 24) & 0xff) << default_alpha_offset) +
1347                     (((data[i] >> 16) & 0xff) << default_red_offset) +
1348                     (((data[i] >> 8) & 0xff) << default_green_offset) +
1349                     (((data[i] >> 0) & 0xff) << default_blue_offset);
1350             }
1351             g_assert(i <= num);
1352         }
1353
1354         g_free(data);
1355     } else if (PROP_GETA32(self->window, kwm_win_icon,
1356                            kwm_win_icon, &data, &num)) {
1357         if (num == 2) {
1358             self->nicons++;
1359             self->icons = g_new(Icon, self->nicons);
1360             if (!render_pixmap_to_rgba(data[0], data[1],
1361                                        &self->icons[self->nicons-1].width,
1362                                        &self->icons[self->nicons-1].height,
1363                                        &self->icons[self->nicons-1].data)) {
1364                 g_free(&self->icons[self->nicons-1]);
1365                 self->nicons--;
1366             }
1367         }
1368         g_free(data);
1369     } else {
1370         XWMHints *hints;
1371
1372         if ((hints = XGetWMHints(ob_display, self->window))) {
1373             if (hints->flags & IconPixmapHint) {
1374                 self->nicons++;
1375                 self->icons = g_new(Icon, self->nicons);
1376                 if (!render_pixmap_to_rgba(hints->icon_pixmap,
1377                                            (hints->flags & IconMaskHint ?
1378                                             hints->icon_mask : None),
1379                                            &self->icons[self->nicons-1].width,
1380                                            &self->icons[self->nicons-1].height,
1381                                            &self->icons[self->nicons-1].data)){
1382                     g_free(&self->icons[self->nicons-1]);
1383                     self->nicons--;
1384                 }
1385             }
1386             XFree(hints);
1387         }
1388     }
1389
1390     if (self->frame)
1391         frame_adjust_icon(self->frame);
1392 }
1393
1394 static void client_change_state(Client *self)
1395 {
1396     guint32 state[2];
1397     guint32 netstate[10];
1398     guint num;
1399
1400     state[0] = self->wmstate;
1401     state[1] = None;
1402     PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1403
1404     num = 0;
1405     if (self->modal)
1406         netstate[num++] = prop_atoms.net_wm_state_modal;
1407     if (self->shaded)
1408         netstate[num++] = prop_atoms.net_wm_state_shaded;
1409     if (self->iconic)
1410         netstate[num++] = prop_atoms.net_wm_state_hidden;
1411     if (self->skip_taskbar)
1412         netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1413     if (self->skip_pager)
1414         netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1415     if (self->fullscreen)
1416         netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1417     if (self->max_vert)
1418         netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1419     if (self->max_horz)
1420         netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1421     if (self->above)
1422         netstate[num++] = prop_atoms.net_wm_state_above;
1423     if (self->below)
1424         netstate[num++] = prop_atoms.net_wm_state_below;
1425     PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1426
1427     client_calc_layer(self);
1428
1429     if (self->frame)
1430         frame_adjust_state(self->frame);
1431 }
1432
1433 Client *client_search_focus_tree(Client *self)
1434 {
1435     GSList *it;
1436     Client *ret;
1437
1438     for (it = self->transients; it != NULL; it = it->next) {
1439         if (client_focused(it->data)) return it->data;
1440         if ((ret = client_search_focus_tree(it->data))) return ret;
1441     }
1442     return NULL;
1443 }
1444
1445 Client *client_search_focus_tree_full(Client *self)
1446 {
1447     if (self->transient_for) {
1448         if (self->transient_for != TRAN_GROUP) {
1449             return client_search_focus_tree_full(self->transient_for);
1450         } else {
1451             GSList *it;
1452             gboolean recursed = FALSE;
1453         
1454             for (it = self->group->members; it; it = it->next)
1455                 if (!((Client*)it->data)->transient_for) {
1456                     Client *c;
1457                     if ((c = client_search_focus_tree_full(it->data)))
1458                         return c;
1459                     recursed = TRUE;
1460                 }
1461             if (recursed)
1462               return NULL;
1463         }
1464     }
1465
1466     /* this function checks the whole tree, the client_search_focus_tree~
1467        does not, so we need to check this window */
1468     if (client_focused(self))
1469       return self;
1470     return client_search_focus_tree(self);
1471 }
1472
1473 static StackLayer calc_layer(Client *self)
1474 {
1475     StackLayer l;
1476
1477     if (self->fullscreen) l = Layer_Fullscreen;
1478     else if (self->type == Type_Desktop) l = Layer_Desktop;
1479     else if (self->type == Type_Dock) {
1480         if (!self->below) l = Layer_Top;
1481         else l = Layer_Normal;
1482     }
1483     else if (self->above) l = Layer_Above;
1484     else if (self->below) l = Layer_Below;
1485     else l = Layer_Normal;
1486
1487     return l;
1488 }
1489
1490 static void calc_recursive(Client *self, Client *orig, StackLayer l,
1491                            gboolean raised)
1492 {
1493     StackLayer old, own;
1494     GSList *it;
1495
1496     old = self->layer;
1497     own = calc_layer(self);
1498     self->layer = l > own ? l : own;
1499
1500     for (it = self->transients; it; it = it->next)
1501         calc_recursive(it->data, orig, l, raised ? raised : l != old);
1502
1503     if (!raised && l != old)
1504         if (orig->frame) /* only restack if the original window is managed */
1505             stacking_raise(CLIENT_AS_WINDOW(self));
1506 }
1507
1508 void client_calc_layer(Client *self)
1509 {
1510     StackLayer l;
1511     Client *orig;
1512
1513     orig = self;
1514
1515     /* transients take on the layer of their parents */
1516     if (self->transient_for) {
1517         if (self->transient_for != TRAN_GROUP) {
1518             self = self->transient_for;
1519         } else {
1520             GSList *it;
1521
1522             for (it = self->group->members; it; it = it->next)
1523                 if (it->data != self &&
1524                     !((Client*)it->data)->transient_for) {
1525                     self = it->data;
1526                     break;
1527                 }
1528         }
1529     }
1530
1531     l = calc_layer(self);
1532
1533     calc_recursive(self, orig, l, FALSE);
1534 }
1535
1536 gboolean client_should_show(Client *self)
1537 {
1538     if (self->iconic) return FALSE;
1539     else if (!(self->desktop == screen_desktop ||
1540                self->desktop == DESKTOP_ALL)) return FALSE;
1541     else if (client_normal(self) && screen_showing_desktop) return FALSE;
1542     
1543     return TRUE;
1544 }
1545
1546 static void client_showhide(Client *self)
1547 {
1548
1549     if (client_should_show(self))
1550         frame_show(self->frame);
1551     else
1552         frame_hide(self->frame);
1553 }
1554
1555 gboolean client_normal(Client *self) {
1556     return ! (self->type == Type_Desktop || self->type == Type_Dock ||
1557               self->type == Type_Splash);
1558 }
1559
1560 static void client_apply_startup_state(Client *self)
1561 {
1562     /* these are in a carefully crafted order.. */
1563
1564     if (self->iconic) {
1565         self->iconic = FALSE;
1566         client_iconify(self, TRUE, FALSE);
1567     }
1568     if (self->fullscreen) {
1569         self->fullscreen = FALSE;
1570         client_fullscreen(self, TRUE, FALSE);
1571     }
1572     if (self->shaded) {
1573         self->shaded = FALSE;
1574         client_shade(self, TRUE);
1575     }
1576     if (self->urgent)
1577         dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1578   
1579     if (self->max_vert && self->max_horz) {
1580         self->max_vert = self->max_horz = FALSE;
1581         client_maximize(self, TRUE, 0, FALSE);
1582     } else if (self->max_vert) {
1583         self->max_vert = FALSE;
1584         client_maximize(self, TRUE, 2, FALSE);
1585     } else if (self->max_horz) {
1586         self->max_horz = FALSE;
1587         client_maximize(self, TRUE, 1, FALSE);
1588     }
1589
1590     /* nothing to do for the other states:
1591        skip_taskbar
1592        skip_pager
1593        modal
1594        above
1595        below
1596     */
1597 }
1598
1599 void client_configure(Client *self, Corner anchor, int x, int y, int w, int h,
1600                       gboolean user, gboolean final)
1601 {
1602     gboolean moved = FALSE, resized = FALSE;
1603
1604     /* gets the frame's position */
1605     frame_client_gravity(self->frame, &x, &y);
1606
1607     /* these positions are frame positions, not client positions */
1608
1609     /* set the size and position if fullscreen */
1610     if (self->fullscreen) {
1611         x = 0;
1612         y = 0;
1613         w = screen_physical_size.width;
1614         h = screen_physical_size.height;
1615         user = FALSE; /* ignore that increment etc shit when in fullscreen */
1616     } else {
1617         /* set the size and position if maximized */
1618         if (self->max_horz) {
1619             x = screen_area(self->desktop)->x - self->frame->size.left;
1620             w = screen_area(self->desktop)->width;
1621         }
1622         if (self->max_vert) {
1623             y = screen_area(self->desktop)->y;
1624             h = screen_area(self->desktop)->height -
1625                 self->frame->size.top - self->frame->size.bottom;
1626         }
1627     }
1628
1629     /* gets the client's position */
1630     frame_frame_gravity(self->frame, &x, &y);
1631
1632     /* these override the above states! if you cant move you can't move! */
1633     if (user) {
1634         if (!(self->functions & Func_Move)) {
1635             x = self->area.x;
1636             y = self->area.y;
1637         }
1638         if (!(self->functions & Func_Resize)) {
1639             w = self->area.width;
1640             h = self->area.height;
1641         }
1642     }
1643
1644     if (!(w == self->area.width && h == self->area.height)) {
1645         w -= self->base_size.width;
1646         h -= self->base_size.height;
1647
1648         if (user) {
1649             /* for interactive resizing. have to move half an increment in each
1650                direction. */
1651
1652             /* how far we are towards the next size inc */
1653             int mw = w % self->size_inc.width; 
1654             int mh = h % self->size_inc.height;
1655             /* amount to add */
1656             int aw = self->size_inc.width / 2;
1657             int ah = self->size_inc.height / 2;
1658             /* don't let us move into a new size increment */
1659             if (mw + aw >= self->size_inc.width)
1660                 aw = self->size_inc.width - mw - 1;
1661             if (mh + ah >= self->size_inc.height)
1662                 ah = self->size_inc.height - mh - 1;
1663             w += aw;
1664             h += ah;
1665     
1666             /* if this is a user-requested resize, then check against min/max
1667                sizes and aspect ratios */
1668
1669             /* smaller than min size or bigger than max size? */
1670             if (w > self->max_size.width) w = self->max_size.width;
1671             if (w < self->min_size.width) w = self->min_size.width;
1672             if (h > self->max_size.height) h = self->max_size.height;
1673             if (h < self->min_size.height) h = self->min_size.height;
1674
1675             /* adjust the height ot match the width for the aspect ratios */
1676             if (self->min_ratio)
1677                 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1678             if (self->max_ratio)
1679                 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1680         }
1681
1682         /* keep to the increments */
1683         w /= self->size_inc.width;
1684         h /= self->size_inc.height;
1685
1686         /* you cannot resize to nothing */
1687         if (w < 1) w = 1;
1688         if (h < 1) h = 1;
1689   
1690         /* store the logical size */
1691         SIZE_SET(self->logical_size, w, h);
1692
1693         w *= self->size_inc.width;
1694         h *= self->size_inc.height;
1695
1696         w += self->base_size.width;
1697         h += self->base_size.height;
1698     }
1699
1700     switch (anchor) {
1701     case Corner_TopLeft:
1702         break;
1703     case Corner_TopRight:
1704         x -= w - self->area.width;
1705         break;
1706     case Corner_BottomLeft:
1707         y -= h - self->area.height;
1708         break;
1709     case Corner_BottomRight:
1710         x -= w - self->area.width;
1711         y -= h - self->area.height;
1712         break;
1713     }
1714
1715     moved = x != self->area.x || y != self->area.y;
1716     resized = w != self->area.width || h != self->area.height;
1717
1718     RECT_SET(self->area, x, y, w, h);
1719
1720     if (resized)
1721         XResizeWindow(ob_display, self->window, w, h);
1722
1723     /* move/resize the frame to match the request */
1724     if (self->frame) {
1725         if (moved || resized)
1726             frame_adjust_area(self->frame, moved, resized);
1727
1728         if (!user || final) {
1729             XEvent event;
1730             event.type = ConfigureNotify;
1731             event.xconfigure.display = ob_display;
1732             event.xconfigure.event = self->window;
1733             event.xconfigure.window = self->window;
1734     
1735             /* root window real coords */
1736             event.xconfigure.x = self->frame->area.x + self->frame->size.left;
1737             event.xconfigure.y = self->frame->area.y + self->frame->size.top;
1738     
1739             event.xconfigure.width = w;
1740             event.xconfigure.height = h;
1741             event.xconfigure.border_width = 0;
1742             event.xconfigure.above = self->frame->plate;
1743             event.xconfigure.override_redirect = FALSE;
1744             XSendEvent(event.xconfigure.display, event.xconfigure.window,
1745                        FALSE, StructureNotifyMask, &event);
1746         }
1747     }
1748 }
1749
1750 void client_fullscreen(Client *self, gboolean fs, gboolean savearea)
1751 {
1752     int x, y, w, h;
1753
1754     if (!(self->functions & Func_Fullscreen) || /* can't */
1755         self->fullscreen == fs) return;         /* already done */
1756
1757     self->fullscreen = fs;
1758     client_change_state(self); /* change the state hints on the client,
1759                                   and adjust out layer/stacking */
1760
1761     if (fs) {
1762         if (savearea) {
1763             guint32 dimensions[4];
1764             dimensions[0] = self->area.x;
1765             dimensions[1] = self->area.y;
1766             dimensions[2] = self->area.width;
1767             dimensions[3] = self->area.height;
1768   
1769             PROP_SETA32(self->window, openbox_premax, cardinal,
1770                         dimensions, 4);
1771         }
1772
1773         /* these are not actually used cuz client_configure will set them
1774            as appropriate when the window is fullscreened */
1775         x = y = w = h = 0;
1776     } else {
1777         guint num;
1778         gint32 *dimensions;
1779
1780         /* pick some fallbacks... */
1781         x = screen_area(self->desktop)->x +
1782             screen_area(self->desktop)->width / 4;
1783         y = screen_area(self->desktop)->y +
1784             screen_area(self->desktop)->height / 4;
1785         w = screen_area(self->desktop)->width / 2;
1786         h = screen_area(self->desktop)->height / 2;
1787
1788         if (PROP_GETA32(self->window, openbox_premax, cardinal,
1789                         (guint32**)&dimensions, &num)) {
1790             if (num == 4) {
1791                 x = dimensions[0];
1792                 y = dimensions[1];
1793                 w = dimensions[2];
1794                 h = dimensions[3];
1795             }
1796             g_free(dimensions);
1797         }
1798     }
1799
1800     client_setup_decor_and_functions(self);
1801
1802     client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1803
1804     /* try focus us when we go into fullscreen mode */
1805     client_focus(self);
1806 }
1807
1808 void client_iconify(Client *self, gboolean iconic, gboolean curdesk)
1809 {
1810     GSList *it;
1811
1812     /* move up the transient chain as far as possible first */
1813     if (self->transient_for) {
1814         if (self->transient_for != TRAN_GROUP) {
1815             if (self->transient_for->iconic != iconic) {
1816                 client_iconify(self->transient_for, iconic, curdesk);
1817                 return;
1818             }
1819         } else {
1820             GSList *it;
1821
1822             for (it = self->group->members; it; it = it->next) {
1823                 Client *c = it->data;
1824                 if (c != self && c->iconic != iconic &&
1825                     !c->transient_for) {
1826                     client_iconify(it->data, iconic, curdesk);
1827                     break;
1828                 }
1829             }
1830             if (it != NULL) return;
1831         }
1832     }
1833
1834     if (self->iconic == iconic) return; /* nothing to do */
1835
1836     g_message("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
1837               self->window);
1838
1839     self->iconic = iconic;
1840
1841     if (iconic) {
1842         self->wmstate = IconicState;
1843         self->ignore_unmaps++;
1844         /* we unmap the client itself so that we can get MapRequest events,
1845            and because the ICCCM tells us to! */
1846         XUnmapWindow(ob_display, self->window);
1847
1848         /* update the focus lists.. iconic windows go to the bottom of the
1849          list, put the new iconic window at the 'top of the bottom'. */
1850         focus_order_to_top(self);
1851     } else {
1852         if (curdesk)
1853             client_set_desktop(self, screen_desktop, FALSE);
1854         self->wmstate = self->shaded ? IconicState : NormalState;
1855         XMapWindow(ob_display, self->window);
1856
1857         /* this puts it after the current focused window */
1858         focus_order_remove(self);
1859         focus_order_add_new(self);
1860     }
1861     client_change_state(self);
1862     client_showhide(self);
1863     screen_update_struts();
1864
1865     dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
1866                     self, 0, 0);
1867
1868     /* iconify all transients */
1869     for (it = self->transients; it != NULL; it = it->next)
1870         if (it->data != self) client_iconify(it->data, iconic, curdesk);
1871 }
1872
1873 void client_maximize(Client *self, gboolean max, int dir, gboolean savearea)
1874 {
1875     int x, y, w, h;
1876      
1877     g_assert(dir == 0 || dir == 1 || dir == 2);
1878     if (!(self->functions & Func_Maximize)) return; /* can't */
1879
1880     /* check if already done */
1881     if (max) {
1882         if (dir == 0 && self->max_horz && self->max_vert) return;
1883         if (dir == 1 && self->max_horz) return;
1884         if (dir == 2 && self->max_vert) return;
1885     } else {
1886         if (dir == 0 && !self->max_horz && !self->max_vert) return;
1887         if (dir == 1 && !self->max_horz) return;
1888         if (dir == 2 && !self->max_vert) return;
1889     }
1890
1891     /* work with the frame's coords */
1892     x = self->frame->area.x;
1893     y = self->frame->area.y;
1894     w = self->area.width;
1895     h = self->area.height;
1896
1897     if (max) {
1898         if (savearea) {
1899             gint32 dimensions[4];
1900             gint32 *readdim;
1901             guint num;
1902
1903             dimensions[0] = x;
1904             dimensions[1] = y;
1905             dimensions[2] = w;
1906             dimensions[3] = h;
1907
1908             /* get the property off the window and use it for the dimensions
1909                we are already maxed on */
1910             if (PROP_GETA32(self->window, openbox_premax, cardinal,
1911                             (guint32**)&readdim, &num)) {
1912                 if (num == 4) {
1913                     if (self->max_horz) {
1914                         dimensions[0] = readdim[0];
1915                         dimensions[2] = readdim[2];
1916                     }
1917                     if (self->max_vert) {
1918                         dimensions[1] = readdim[1];
1919                         dimensions[3] = readdim[3];
1920                     }
1921                 }
1922                 g_free(readdim);
1923             }
1924
1925             PROP_SETA32(self->window, openbox_premax, cardinal,
1926                         (guint32*)dimensions, 4);
1927         }
1928     } else {
1929         guint num;
1930         gint32 *dimensions;
1931
1932         /* pick some fallbacks... */
1933         if (dir == 0 || dir == 1) { /* horz */
1934             x = screen_area(self->desktop)->x +
1935                 screen_area(self->desktop)->width / 4;
1936             w = screen_area(self->desktop)->width / 2;
1937         }
1938         if (dir == 0 || dir == 2) { /* vert */
1939             y = screen_area(self->desktop)->y +
1940                 screen_area(self->desktop)->height / 4;
1941             h = screen_area(self->desktop)->height / 2;
1942         }
1943
1944         if (PROP_GETA32(self->window, openbox_premax, cardinal,
1945                         (guint32**)&dimensions, &num)) {
1946             if (num == 4) {
1947                 if (dir == 0 || dir == 1) { /* horz */
1948                     x = dimensions[0];
1949                     w = dimensions[2];
1950                 }
1951                 if (dir == 0 || dir == 2) { /* vert */
1952                     y = dimensions[1];
1953                     h = dimensions[3];
1954                 }
1955             }
1956             g_free(dimensions);
1957         }
1958     }
1959
1960     if (dir == 0 || dir == 1) /* horz */
1961         self->max_horz = max;
1962     if (dir == 0 || dir == 2) /* vert */
1963         self->max_vert = max;
1964
1965     if (!self->max_horz && !self->max_vert)
1966         PROP_ERASE(self->window, openbox_premax);
1967
1968     client_change_state(self); /* change the state hints on the client */
1969
1970     /* figure out where the client should be going */
1971     frame_frame_gravity(self->frame, &x, &y);
1972     client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1973 }
1974
1975 void client_shade(Client *self, gboolean shade)
1976 {
1977     if ((!(self->functions & Func_Shade) && shade) || /* can't shade */
1978         self->shaded == shade) return;     /* already done */
1979
1980     /* when we're iconic, don't change the wmstate */
1981     if (!self->iconic)
1982         self->wmstate = shade ? IconicState : NormalState;
1983     self->shaded = shade;
1984     client_change_state(self);
1985     /* resize the frame to just the titlebar */
1986     frame_adjust_area(self->frame, FALSE, FALSE);
1987 }
1988
1989 void client_close(Client *self)
1990 {
1991     XEvent ce;
1992
1993     if (!(self->functions & Func_Close)) return;
1994
1995     /*
1996       XXX: itd be cool to do timeouts and shit here for killing the client's
1997       process off
1998       like... if the window is around after 5 seconds, then the close button
1999       turns a nice red, and if this function is called again, the client is
2000       explicitly killed.
2001     */
2002
2003     ce.xclient.type = ClientMessage;
2004     ce.xclient.message_type =  prop_atoms.wm_protocols;
2005     ce.xclient.display = ob_display;
2006     ce.xclient.window = self->window;
2007     ce.xclient.format = 32;
2008     ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
2009     ce.xclient.data.l[1] = event_lasttime;
2010     ce.xclient.data.l[2] = 0l;
2011     ce.xclient.data.l[3] = 0l;
2012     ce.xclient.data.l[4] = 0l;
2013     XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2014 }
2015
2016 void client_kill(Client *self)
2017 {
2018     XKillClient(ob_display, self->window);
2019 }
2020
2021 void client_set_desktop(Client *self, guint target, gboolean donthide)
2022 {
2023     guint old;
2024
2025     if (target == self->desktop) return;
2026   
2027     g_message("Setting desktop %u", target+1);
2028
2029     g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2030
2031     /* remove from the old desktop(s) */
2032     focus_order_remove(self);
2033
2034     old = self->desktop;
2035     self->desktop = target;
2036     PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2037     /* the frame can display the current desktop state */
2038     frame_adjust_state(self->frame);
2039     /* 'move' the window to the new desktop */
2040     if (!donthide)
2041         client_showhide(self);
2042     /* raise if it was not already on the desktop */
2043     if (old != DESKTOP_ALL)
2044         stacking_raise(CLIENT_AS_WINDOW(self));
2045     screen_update_struts();
2046
2047     /* add to the new desktop(s) */
2048     if (config_focus_new)
2049         focus_order_to_top(self);
2050     else
2051         focus_order_to_bottom(self);
2052
2053     dispatch_client(Event_Client_Desktop, self, target, old);
2054 }
2055
2056 Client *client_search_modal_child(Client *self)
2057 {
2058     GSList *it;
2059     Client *ret;
2060   
2061     for (it = self->transients; it != NULL; it = it->next) {
2062         Client *c = it->data;
2063         if ((ret = client_search_modal_child(c))) return ret;
2064         if (c->modal) return c;
2065     }
2066     return NULL;
2067 }
2068
2069 gboolean client_validate(Client *self)
2070 {
2071     XEvent e; 
2072
2073     XSync(ob_display, FALSE); /* get all events on the server */
2074
2075     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2076         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2077         XPutBackEvent(ob_display, &e);
2078         return FALSE;
2079     }
2080
2081     return TRUE;
2082 }
2083
2084 void client_set_wm_state(Client *self, long state)
2085 {
2086     if (state == self->wmstate) return; /* no change */
2087   
2088     switch (state) {
2089     case IconicState:
2090         client_iconify(self, TRUE, TRUE);
2091         break;
2092     case NormalState:
2093         client_iconify(self, FALSE, TRUE);
2094         break;
2095     }
2096 }
2097
2098 void client_set_state(Client *self, Atom action, long data1, long data2)
2099 {
2100     gboolean shaded = self->shaded;
2101     gboolean fullscreen = self->fullscreen;
2102     gboolean max_horz = self->max_horz;
2103     gboolean max_vert = self->max_vert;
2104     int i;
2105
2106     if (!(action == prop_atoms.net_wm_state_add ||
2107           action == prop_atoms.net_wm_state_remove ||
2108           action == prop_atoms.net_wm_state_toggle))
2109         /* an invalid action was passed to the client message, ignore it */
2110         return; 
2111
2112     for (i = 0; i < 2; ++i) {
2113         Atom state = i == 0 ? data1 : data2;
2114     
2115         if (!state) continue;
2116
2117         /* if toggling, then pick whether we're adding or removing */
2118         if (action == prop_atoms.net_wm_state_toggle) {
2119             if (state == prop_atoms.net_wm_state_modal)
2120                 action = self->modal ? prop_atoms.net_wm_state_remove :
2121                     prop_atoms.net_wm_state_add;
2122             else if (state == prop_atoms.net_wm_state_maximized_vert)
2123                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2124                     prop_atoms.net_wm_state_add;
2125             else if (state == prop_atoms.net_wm_state_maximized_horz)
2126                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2127                     prop_atoms.net_wm_state_add;
2128             else if (state == prop_atoms.net_wm_state_shaded)
2129                 action = self->shaded ? prop_atoms.net_wm_state_remove :
2130                     prop_atoms.net_wm_state_add;
2131             else if (state == prop_atoms.net_wm_state_skip_taskbar)
2132                 action = self->skip_taskbar ?
2133                     prop_atoms.net_wm_state_remove :
2134                     prop_atoms.net_wm_state_add;
2135             else if (state == prop_atoms.net_wm_state_skip_pager)
2136                 action = self->skip_pager ?
2137                     prop_atoms.net_wm_state_remove :
2138                     prop_atoms.net_wm_state_add;
2139             else if (state == prop_atoms.net_wm_state_fullscreen)
2140                 action = self->fullscreen ?
2141                     prop_atoms.net_wm_state_remove :
2142                     prop_atoms.net_wm_state_add;
2143             else if (state == prop_atoms.net_wm_state_above)
2144                 action = self->above ? prop_atoms.net_wm_state_remove :
2145                     prop_atoms.net_wm_state_add;
2146             else if (state == prop_atoms.net_wm_state_below)
2147                 action = self->below ? prop_atoms.net_wm_state_remove :
2148                     prop_atoms.net_wm_state_add;
2149         }
2150     
2151         if (action == prop_atoms.net_wm_state_add) {
2152             if (state == prop_atoms.net_wm_state_modal) {
2153                 /* XXX raise here or something? */
2154                 self->modal = TRUE;
2155             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2156                 max_vert = TRUE;
2157             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2158                 max_horz = TRUE;
2159             } else if (state == prop_atoms.net_wm_state_shaded) {
2160                 shaded = TRUE;
2161             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2162                 self->skip_taskbar = TRUE;
2163             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2164                 self->skip_pager = TRUE;
2165             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2166                 fullscreen = TRUE;
2167             } else if (state == prop_atoms.net_wm_state_above) {
2168                 self->above = TRUE;
2169             } else if (state == prop_atoms.net_wm_state_below) {
2170                 self->below = TRUE;
2171             }
2172
2173         } else { /* action == prop_atoms.net_wm_state_remove */
2174             if (state == prop_atoms.net_wm_state_modal) {
2175                 self->modal = FALSE;
2176             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2177                 max_vert = FALSE;
2178             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2179                 max_horz = FALSE;
2180             } else if (state == prop_atoms.net_wm_state_shaded) {
2181                 shaded = FALSE;
2182             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2183                 self->skip_taskbar = FALSE;
2184             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2185                 self->skip_pager = FALSE;
2186             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2187                 fullscreen = FALSE;
2188             } else if (state == prop_atoms.net_wm_state_above) {
2189                 self->above = FALSE;
2190             } else if (state == prop_atoms.net_wm_state_below) {
2191                 self->below = FALSE;
2192             }
2193         }
2194     }
2195     if (max_horz != self->max_horz || max_vert != self->max_vert) {
2196         if (max_horz != self->max_horz && max_vert != self->max_vert) {
2197             /* toggling both */
2198             if (max_horz == max_vert) { /* both going the same way */
2199                 client_maximize(self, max_horz, 0, TRUE);
2200             } else {
2201                 client_maximize(self, max_horz, 1, TRUE);
2202                 client_maximize(self, max_vert, 2, TRUE);
2203             }
2204         } else {
2205             /* toggling one */
2206             if (max_horz != self->max_horz)
2207                 client_maximize(self, max_horz, 1, TRUE);
2208             else
2209                 client_maximize(self, max_vert, 2, TRUE);
2210         }
2211     }
2212     /* change fullscreen state before shading, as it will affect if the window
2213        can shade or not */
2214     if (fullscreen != self->fullscreen)
2215         client_fullscreen(self, fullscreen, TRUE);
2216     if (shaded != self->shaded)
2217         client_shade(self, shaded);
2218     client_calc_layer(self);
2219     client_change_state(self); /* change the hint to reflect these changes */
2220 }
2221
2222 Client *client_focus_target(Client *self)
2223 {
2224     Client *child;
2225      
2226     /* if we have a modal child, then focus it, not us */
2227     child = client_search_modal_child(self);
2228     if (child) return child;
2229     return self;
2230 }
2231
2232 gboolean client_focus(Client *self)
2233 {
2234     XEvent ev;
2235
2236     /* choose the correct target */
2237     self = client_focus_target(self);
2238
2239     if (!self->frame->visible) {
2240         /* update the focus lists */
2241         focus_order_to_top(self);
2242         return FALSE;
2243     }
2244
2245     if (!((self->can_focus || self->focus_notify) &&
2246           (self->desktop == screen_desktop ||
2247            self->desktop == DESKTOP_ALL) &&
2248           !self->iconic))
2249         return FALSE;
2250
2251     /* do a check to see if the window has already been unmapped or destroyed
2252        do this intelligently while watching out for unmaps we've generated
2253        (ignore_unmaps > 0) */
2254     if (XCheckTypedWindowEvent(ob_display, self->window,
2255                                DestroyNotify, &ev)) {
2256         XPutBackEvent(ob_display, &ev);
2257         return FALSE;
2258     }
2259     while (XCheckTypedWindowEvent(ob_display, self->window,
2260                                   UnmapNotify, &ev)) {
2261         if (self->ignore_unmaps) {
2262             self->ignore_unmaps--;
2263         } else {
2264             XPutBackEvent(ob_display, &ev);
2265             return FALSE;
2266         }
2267     }
2268
2269     if (self->can_focus)
2270         /* RevertToPointerRoot causes much more headache than RevertToNone, so
2271            I choose to use it always, hopefully to find errors quicker, if any
2272            are left. (I hate X. I hate focus events.) */
2273         XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2274                        event_lasttime);
2275
2276     if (self->focus_notify) {
2277         XEvent ce;
2278         ce.xclient.type = ClientMessage;
2279         ce.xclient.message_type = prop_atoms.wm_protocols;
2280         ce.xclient.display = ob_display;
2281         ce.xclient.window = self->window;
2282         ce.xclient.format = 32;
2283         ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2284         ce.xclient.data.l[1] = event_lasttime;
2285         ce.xclient.data.l[2] = 0l;
2286         ce.xclient.data.l[3] = 0l;
2287         ce.xclient.data.l[4] = 0l;
2288         XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2289     }
2290
2291 #ifdef DEBUG_FOCUS
2292     g_message("%sively focusing %lx at %d", (self->can_focus ? "act" : "pass"),
2293               self->window, (int)
2294               event_lasttime);
2295 #endif
2296
2297     /* Cause the FocusIn to come back to us. Important for desktop switches,
2298        since otherwise we'll have no FocusIn on the queue and send it off to
2299        the focus_backup. */
2300     XSync(ob_display, FALSE);
2301     return TRUE;
2302 }
2303
2304 void client_unfocus(Client *self)
2305 {
2306     g_assert(focus_client == self);
2307 #ifdef DEBUG_FOCUS
2308     g_message("client_unfocus for %lx", self->window);
2309 #endif
2310     focus_fallback(Fallback_Unfocusing);
2311 }
2312
2313 void client_activate(Client *self)
2314 {
2315     if (client_normal(self) && screen_showing_desktop)
2316         screen_show_desktop(FALSE);
2317     if (self->iconic)
2318         client_iconify(self, FALSE, TRUE);
2319     else if (!self->frame->visible)
2320         /* if its not visible for other reasons, then don't mess
2321            with it */
2322         return;
2323     if (self->shaded)
2324         client_shade(self, FALSE);
2325     client_focus(self);
2326     stacking_raise(CLIENT_AS_WINDOW(self));
2327 }
2328
2329 gboolean client_focused(Client *self)
2330 {
2331     return self == focus_client;
2332 }
2333
2334 Icon *client_icon(Client *self, int w, int h)
2335 {
2336     int i;
2337     /* si is the smallest image >= req */
2338     /* li is the largest image < req */
2339     unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2340
2341     if (!self->nicons) return NULL;
2342
2343     for (i = 0; i < self->nicons; ++i) {
2344         size = self->icons[i].width * self->icons[i].height;
2345         if (size < smallest && size >= (unsigned)(w * h)) {
2346             smallest = size;
2347             si = i;
2348         }
2349         if (size > largest && size <= (unsigned)(w * h)) {
2350             largest = size;
2351             li = i;
2352         }
2353     }
2354     if (largest == 0) /* didnt find one smaller than the requested size */
2355         return &self->icons[si];
2356     return &self->icons[li];
2357 }
2358
2359 /* this be mostly ripped from fvwm */
2360 Client *client_find_directional(Client *c, Direction dir) 
2361 {
2362     int my_cx, my_cy, his_cx, his_cy;
2363     int offset = 0;
2364     int distance = 0;
2365     int score, best_score;
2366     Client *best_client, *cur;
2367     GList *it;
2368
2369     if(!client_list)
2370         return NULL;
2371
2372     /* first, find the centre coords of the currently focused window */
2373     my_cx = c->frame->area.x + c->frame->area.width / 2;
2374     my_cy = c->frame->area.y + c->frame->area.height / 2;
2375
2376     best_score = -1;
2377     best_client = NULL;
2378
2379     for(it = g_list_first(client_list); it; it = it->next) {
2380         cur = it->data;
2381
2382         /* the currently selected window isn't interesting */
2383         if(cur == c)
2384             continue;
2385         if (!client_normal(cur))
2386             continue;
2387         if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2388             continue;
2389         if(cur->iconic)
2390             continue;
2391         if(client_focus_target(cur) == cur &&
2392            !(cur->can_focus || cur->focus_notify))
2393             continue;
2394
2395         /* find the centre coords of this window, from the
2396          * currently focused window's point of view */
2397         his_cx = (cur->frame->area.x - my_cx)
2398             + cur->frame->area.width / 2;
2399         his_cy = (cur->frame->area.y - my_cy)
2400             + cur->frame->area.height / 2;
2401
2402         if(dir > 3) { 
2403             int tx;
2404             /* Rotate the diagonals 45 degrees counterclockwise.
2405              * To do this, multiply the matrix /+h +h\ with the
2406              * vector (x y).                   \-h +h/
2407              * h = sqrt(0.5). We can set h := 1 since absolute
2408              * distance doesn't matter here. */
2409             tx = his_cx + his_cy;
2410             his_cy = -his_cx + his_cy;
2411             his_cx = tx;
2412         }
2413
2414         switch(dir) {
2415         case Direction_North :
2416         case Direction_South :
2417         case Direction_NorthEast :
2418         case Direction_SouthWest :
2419             offset = (his_cx < 0) ? -his_cx : his_cx;
2420             distance = (dir == Direction_North || dir == Direction_NorthEast) ?
2421                 -his_cy : his_cy;
2422             break;
2423         case Direction_East :
2424         case Direction_West :
2425         case Direction_SouthEast :
2426         case Direction_NorthWest :
2427             offset = (his_cy < 0) ? -his_cy : his_cy;
2428             distance = (dir == Direction_West || dir == Direction_NorthWest) ?
2429                 -his_cx : his_cx;
2430             break;
2431         }
2432
2433         /* the target must be in the requested direction */
2434         if(distance <= 0)
2435             continue;
2436
2437         /* Calculate score for this window.  The smaller the better. */
2438         score = distance + offset;
2439
2440         /* windows more than 45 degrees off the direction are
2441          * heavily penalized and will only be chosen if nothing
2442          * else within a million pixels */
2443         if(offset > distance)
2444             score += 1000000;
2445
2446         if(best_score == -1 || score < best_score)
2447             best_client = cur,
2448                 best_score = score;
2449     }
2450
2451     return best_client;
2452 }
2453
2454 void client_set_layer(Client *self, int layer)
2455 {
2456     if (layer < 0) {
2457         self->below = TRUE;
2458         self->above = FALSE;
2459     } else if (layer == 0) {
2460         self->below = self->above = FALSE;
2461     } else {
2462         self->below = FALSE;
2463         self->above = TRUE;
2464     }
2465     client_calc_layer(self);
2466     client_change_state(self); /* reflect this in the state hints */
2467 }