]> icculus.org git repositories - mikachu/openbox.git/blob - c/client.c
merge the C branch into HEAD
[mikachu/openbox.git] / c / client.c
1 #include "client.h"
2 #include "screen.h"
3 #include "prop.h"
4 #include "extensions.h"
5 #include "frame.h"
6 #include "event.h"
7 #include "focus.h"
8 #include "clientwrap.h"
9 #include "stacking.h"
10 #include "hooks.h"
11 #include "mbind.h"
12 #include <X11/Xutil.h>
13
14 /*! The event mask to grab on client windows */
15 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
16                           StructureNotifyMask)
17
18 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
19                                 ButtonMotionMask)
20
21 GSList     *client_list      = NULL;
22 GHashTable *client_map       = NULL;
23
24 static void client_get_all(Client *self);
25 static void client_toggle_border(Client *self, gboolean show);
26 static void client_get_area(Client *self);
27 static void client_get_desktop(Client *self);
28 static void client_get_state(Client *self);
29 static void client_get_shaped(Client *self);
30 static void client_get_mwm_hints(Client *self);
31 static void client_get_gravity(Client *self);
32 static void client_change_allowed_actions(Client *self);
33 static void client_change_state(Client *self);
34 static Client *search_focus_tree(Client *node, Client *skip);
35 static void client_apply_startup_state(Client *self);
36 static Client *search_modal_tree(Client *node, Client *skip);
37
38 static guint map_hash(Window w) { return w; }
39 static gboolean map_key_comp(Window w1, Window w2) { return w1 == w2; }
40
41 void client_startup()
42 {
43     client_map = g_hash_table_new((GHashFunc)map_hash,
44                                   (GEqualFunc)map_key_comp);
45     client_set_list();
46 }
47
48 void client_shutdown()
49 {
50     g_hash_table_destroy(client_map);
51 }
52
53 void client_set_list()
54 {
55     Window *windows, *win_it;
56     GSList *it;
57     guint size = g_slist_length(client_list);
58
59     /* create an array of the window ids */
60     if (size > 0) {
61         windows = g_new(Window, size);
62         win_it = windows;
63         for (it = client_list; it != NULL; it = it->next, ++win_it)
64             *win_it = ((Client*)it->data)->window;
65     } else
66         windows = NULL;
67
68     PROP_SET32A(ob_root, net_client_list, window, windows, size);
69
70     if (windows)
71         g_free(windows);
72
73     stacking_set_list();
74 }
75
76 void client_manage_all()
77 {
78     unsigned int i, j, nchild;
79     Window w, *children;
80     XWMHints *wmhints;
81     XWindowAttributes attrib;
82
83     XQueryTree(ob_display, ob_root, &w, &w, &children, &nchild);
84
85     /* remove all icon windows from the list */
86     for (i = 0; i < nchild; i++) {
87         if (children[i] == None) continue;
88         wmhints = XGetWMHints(ob_display, children[i]);
89         if (wmhints) {
90             if ((wmhints->flags & IconWindowHint) &&
91                 (wmhints->icon_window != children[i]))
92                 for (j = 0; j < nchild; j++)
93                     if (children[j] == wmhints->icon_window) {
94                         children[j] = None;
95                         break;
96                     }
97             XFree(wmhints);
98         }
99     }
100
101     for (i = 0; i < nchild; ++i) {
102         if (children[i] == None)
103             continue;
104         if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
105             if (attrib.override_redirect) continue;
106
107             if (attrib.map_state != IsUnmapped)
108                 client_manage(children[i]);
109         }
110     }
111     XFree(children);
112 }
113
114 void client_manage(Window window)
115 {
116     Client *client;
117     XEvent e;
118     XWindowAttributes attrib;
119     XSetWindowAttributes attrib_set;
120     XWMHints *wmhint;
121      
122     XGrabServer(ob_display);
123     XSync(ob_display, FALSE);
124
125     /* check if it has already been unmapped by the time we started mapping
126        the grab does a sync so we don't have to here */
127     if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
128         XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
129         XPutBackEvent(ob_display, &e);
130     
131         XUngrabServer(ob_display);
132         XFlush(ob_display);
133         return; /* don't manage it */
134     }
135
136     /* make sure it isn't an override-redirect window */
137     if (!XGetWindowAttributes(ob_display, window, &attrib) ||
138         attrib.override_redirect) {
139         XUngrabServer(ob_display);
140         XFlush(ob_display);
141         return; /* don't manage it */
142     }
143   
144     /* is the window a docking app */
145     if ((wmhint = XGetWMHints(ob_display, window))) {
146         if ((wmhint->flags & StateHint) &&
147             wmhint->initial_state == WithdrawnState) {
148             /* XXX: make dock apps work! */
149             XUngrabServer(ob_display);
150             XFlush(ob_display);
151             XFree(wmhint);
152             return;
153         }
154         XFree(wmhint);
155     }
156
157     /* choose the events we want to receive on the CLIENT window */
158     attrib_set.event_mask = CLIENT_EVENTMASK;
159     attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
160     XChangeWindowAttributes(ob_display, window,
161                             CWEventMask|CWDontPropagate, &attrib_set);
162
163
164     /* create the Client struct, and populate it from the hints on the
165        window */
166     client = g_new(Client, 1);
167     client->window = window;
168     client_get_all(client);
169
170     /* remove the client's border (and adjust re gravity) */
171     client_toggle_border(client, FALSE);
172      
173     /* specify that if we exit, the window should not be destroyed and should
174        be reparented back to root automatically */
175     XChangeSaveSet(ob_display, window, SetModeInsert);
176
177     /* create the decoration frame for the client window */
178     client->frame = frame_new(client);
179
180     frame_grab_client(client->frame);
181
182     client_apply_startup_state(client);
183
184     XUngrabServer(ob_display);
185     XFlush(ob_display);
186      
187     client_list = g_slist_append(client_list, client);
188     stacking_list = g_list_append(stacking_list, client);
189     g_hash_table_insert(client_map, (gpointer)window, client);
190
191     stacking_raise(client);
192
193     screen_update_struts();
194
195     LOGICALHOOK(NewWindow, g_quark_try_string("client"), client);
196
197     client_showhide(client);
198
199     /* grab all mouse bindings */
200     mbind_grab_all(client, TRUE);
201
202     /* update the list hints */
203     client_set_list();
204
205     g_message("Managed window 0x%lx frame 0x%lx", window,
206               client->frame->window);
207 }
208
209 void client_unmanage_all()
210 {
211     while (client_list != NULL) {
212         client_unmanage(client_list->data);
213     }
214 }
215
216 void client_unmanage(Client *client)
217 {
218     int j;
219     GSList *it;
220
221     g_message("Unmanaging window: %lx", client->window);
222
223     LOGICALHOOK(CloseWindow, g_quark_try_string("client"), client);
224
225     /* remove the window from our save set */
226     XChangeSaveSet(ob_display, client->window, SetModeDelete);
227
228     /* we dont want events no more */
229     XSelectInput(ob_display, client->window, NoEventMask);
230
231     /* ungrab any mouse bindings */
232     mbind_grab_all(client, FALSE);
233      
234     frame_hide(client->frame);
235
236     /* give the client its border back */
237     client_toggle_border(client, TRUE);
238
239     /* reparent the window out of the frame */
240     frame_release_client(client->frame);
241      
242     client_list = g_slist_remove(client_list, client);
243     stacking_list = g_list_remove(stacking_list, client);
244     g_hash_table_remove(client_map, (gpointer)client->window);
245
246     /* once the client is out of the list, update the struts to remove it's
247        influence */
248     screen_update_struts();
249
250     frame_free(client->frame);
251
252     /* notify the wrapper that its useless now */
253     if (client->wrap != NULL)
254         client->wrap->client = NULL;
255
256     /* tell our parent that we're gone */
257     if (client->transient_for != NULL)
258         client->transient_for->transients =
259             g_slist_remove(client->transient_for->transients, client);
260
261     /* tell our transients that we're gone */
262     for (it = client->transients; it != NULL; it = it->next) {
263         Client *t = ((Client*)it->data)->transient_for;
264         ((Client*)it->data)->transient_for = NULL;
265         if (t != NULL)
266             client_calc_layer(it->data);
267     }
268
269     /* unfocus the client (calls the focus callbacks) (we're out of the
270      transient lists already, so being modal doesn't matter) */
271     if (client->focused)
272         client_unfocus(client);
273     
274     if (ob_state != State_Exiting) {
275         /* these values should not be persisted across a window
276            unmapping/mapping */
277         prop_erase(client->window, prop_atoms.net_wm_desktop);
278         prop_erase(client->window, prop_atoms.net_wm_state);
279     } else {
280         /* if we're left in an iconic state, the client wont be mapped. this is
281            bad, since we will no longer be managing the window on restart */
282         if (client->iconic)
283             XMapWindow(ob_display, client->window);
284     }
285
286     /* free all data allocated in the client struct */
287     g_slist_free(client->transients);
288     for (j = 0; j < client->nicons; ++j)
289         g_free(client->icons[j].data);
290     if (client->nicons > 0)
291         g_free(client->icons);
292     g_free(client->title);
293     g_free(client->icon_title);
294     g_free(client->res_name);
295     g_free(client->res_class);
296     g_free(client->role);
297     g_free(client);
298      
299     /* update the list hints */
300     client_set_list();
301 }
302
303 static void client_toggle_border(Client *self, gboolean show)
304 {
305     /* adjust our idea of where the client is, based on its border. When the
306        border is removed, the client should now be considered to be in a
307        different position.
308        when re-adding the border to the client, the same operation needs to be
309        reversed. */
310     int oldx = self->area.x, oldy = self->area.y;
311     int x = oldx, y = oldy;
312     switch(self->gravity) {
313     default:
314     case NorthWestGravity:
315     case WestGravity:
316     case SouthWestGravity:
317         break;
318     case NorthEastGravity:
319     case EastGravity:
320     case SouthEastGravity:
321         if (show) x -= self->border_width * 2;
322         else      x += self->border_width * 2;
323         break;
324     case NorthGravity:
325     case SouthGravity:
326     case CenterGravity:
327     case ForgetGravity:
328     case StaticGravity:
329         if (show) x -= self->border_width;
330         else      x += self->border_width;
331         break;
332     }
333     switch(self->gravity) {
334     default:
335     case NorthWestGravity:
336     case NorthGravity:
337     case NorthEastGravity:
338         break;
339     case SouthWestGravity:
340     case SouthGravity:
341     case SouthEastGravity:
342         if (show) y -= self->border_width * 2;
343         else      y += self->border_width * 2;
344         break;
345     case WestGravity:
346     case EastGravity:
347     case CenterGravity:
348     case ForgetGravity:
349     case StaticGravity:
350         if (show) y -= self->border_width;
351         else      y += self->border_width;
352         break;
353     }
354     self->area.x = x;
355     self->area.y = y;
356
357     if (show) {
358         XSetWindowBorderWidth(ob_display, self->window, self->border_width);
359
360         /* move the client so it is back it the right spot _with_ its
361            border! */
362         if (x != oldx || y != oldy)
363             XMoveWindow(ob_display, self->window, x, y);
364     } else
365         XSetWindowBorderWidth(ob_display, self->window, 0);
366 }
367
368
369 static void client_get_all(Client *self)
370 {
371     /* update EVERYTHING!! */
372
373     self->ignore_unmaps = 0;
374   
375     /* defaults */
376     self->frame = NULL;
377     self->title = self->icon_title = NULL;
378     self->res_name = self->res_class = self->role = NULL;
379     self->wmstate = NormalState;
380     self->focused = FALSE;
381     self->transient = FALSE;
382     self->transients = NULL;
383     self->transient_for = NULL;
384     self->layer = -1;
385     self->urgent = FALSE;
386     self->positioned = FALSE;
387     self->disabled_decorations = 0;
388     self->group = None;
389     self->nicons = 0;
390     self->wrap = NULL;
391
392     client_get_area(self);
393     client_get_desktop(self);
394     client_get_state(self);
395     client_get_shaped(self);
396
397     client_update_transient_for(self);
398     client_get_mwm_hints(self);
399     client_get_type(self);/* this can change the mwmhints for special cases */
400
401     client_update_protocols(self);
402
403     client_get_gravity(self); /* get the attribute gravity */
404     client_update_normal_hints(self); /* this may override the attribute
405                                          gravity */
406
407     /* got the type, the mwmhints, the protocols, and the normal hints
408        (min/max sizes), so we're ready to set up the decorations/functions */
409     client_setup_decor_and_functions(self);
410   
411     client_update_wmhints(self);
412     client_update_title(self);
413     client_update_icon_title(self);
414     client_update_class(self);
415     client_update_strut(self);
416     client_update_icons(self);
417     client_update_kwm_icon(self);
418
419     /* this makes sure that these windows appear on all desktops */
420     if (self->type == Type_Desktop)
421         self->desktop = DESKTOP_ALL;
422
423     /* set the desktop hint, to make sure that it always exists, and to
424        reflect any changes we've made here */
425     PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
426
427     client_change_state(self);
428 }
429
430 static void client_get_area(Client *self)
431 {
432     XWindowAttributes wattrib;
433     Status ret;
434   
435     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
436     g_assert(ret != BadWindow);
437
438     RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
439     self->border_width = wattrib.border_width;
440 }
441
442 static void client_get_desktop(Client *self)
443 {
444     unsigned int d;
445
446     if (PROP_GET32(self->window, net_wm_desktop, cardinal, d)) {
447         if (d >= screen_desktop && d != DESKTOP_ALL)
448             d = screen_desktop - 1;
449         self->desktop = d;
450     } else {
451         /* defaults to the current desktop */
452         self->desktop = screen_desktop;
453     }
454 }
455
456 static void client_get_state(Client *self)
457 {
458     gulong *state;
459     gulong num;
460   
461     self->modal = self->shaded = self->max_horz = self->max_vert =
462         self->fullscreen = self->above = self->below = self->iconic =
463         self->skip_taskbar = self->skip_pager = FALSE;
464
465     if (PROP_GET32U(self->window, net_wm_state, atom, state, num)) {
466         gulong i;
467         for (i = 0; i < num; ++i) {
468             if (state[i] == prop_atoms.net_wm_state_modal)
469                 self->modal = TRUE;
470             else if (state[i] == prop_atoms.net_wm_state_shaded)
471                 self->shaded = TRUE;
472             else if (state[i] == prop_atoms.net_wm_state_hidden)
473                 self->iconic = TRUE;
474             else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
475                 self->skip_taskbar = TRUE;
476             else if (state[i] == prop_atoms.net_wm_state_skip_pager)
477                 self->skip_pager = TRUE;
478             else if (state[i] == prop_atoms.net_wm_state_fullscreen)
479                 self->fullscreen = TRUE;
480             else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
481                 self->max_vert = TRUE;
482             else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
483                 self->max_horz = TRUE;
484             else if (state[i] == prop_atoms.net_wm_state_above)
485                 self->above = TRUE;
486             else if (state[i] == prop_atoms.net_wm_state_below)
487                 self->below = TRUE;
488         }
489
490         g_free(state);
491     }
492 }
493
494 static void client_get_shaped(Client *self)
495 {
496     self->shaped = FALSE;
497 #ifdef   SHAPE
498     if (extensions_shape) {
499         int foo;
500         guint ufoo;
501         int s;
502
503         XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
504
505         XShapeQueryExtents(ob_display, self->window, &s, &foo,
506                            &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
507                            &ufoo);
508         self->shaped = (s != 0);
509     }
510 #endif
511 }
512
513 void client_update_transient_for(Client *self)
514 {
515     Window t = None;
516     Client *c = NULL;
517
518     if (XGetTransientForHint(ob_display, self->window, &t) &&
519         t != self->window) { /* cant be transient to itself! */
520         self->transient = TRUE;
521         c = g_hash_table_lookup(client_map, (gpointer)t);
522         g_assert(c != self);/* if this happens then we need to check for it*/
523
524         if (!c /*XXX: && _group*/) {
525             /* not transient to a client, see if it is transient for a
526                group */
527             if (/*t == _group->leader() || */
528                 t == None ||
529                 t == ob_root) {
530                 /* window is a transient for its group! */
531                 /* XXX: for now this is treated as non-transient.
532                    this needs to be fixed! */
533             }
534         }
535     } else
536         self->transient = FALSE;
537
538     /* if anything has changed... */
539     if (c != self->transient_for) {
540         if (self->transient_for)
541             /* remove from old parent */
542             g_slist_remove(self->transient_for->transients, self);
543         self->transient_for = c;
544         if (self->transient_for)
545             /* add to new parent */
546             g_slist_append(self->transient_for->transients, self);
547     }
548 }
549
550 static void client_get_mwm_hints(Client *self)
551 {
552     unsigned long num;
553     unsigned long *hints;
554
555     self->mwmhints.flags = 0; /* default to none */
556
557     if (PROP_GET32U(self->window, motif_wm_hints, motif_wm_hints, hints, num)) {
558         if (num >= MWM_ELEMENTS) {
559             self->mwmhints.flags = hints[0];
560             self->mwmhints.functions = hints[1];
561             self->mwmhints.decorations = hints[2];
562         }
563         g_free(hints);
564     }
565 }
566
567 void client_get_type(Client *self)
568 {
569     gulong *val, num, i;
570
571     self->type = -1;
572   
573     if (PROP_GET32U(self->window, net_wm_window_type, atom, val, num)) {
574         /* use the first value that we know about in the array */
575         for (i = 0; i < num; ++i) {
576             if (val[i] == prop_atoms.net_wm_window_type_desktop)
577                 self->type = Type_Desktop;
578             else if (val[i] == prop_atoms.net_wm_window_type_dock)
579                 self->type = Type_Dock;
580             else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
581                 self->type = Type_Toolbar;
582             else if (val[i] == prop_atoms.net_wm_window_type_menu)
583                 self->type = Type_Menu;
584             else if (val[i] == prop_atoms.net_wm_window_type_utility)
585                 self->type = Type_Utility;
586             else if (val[i] == prop_atoms.net_wm_window_type_splash)
587                 self->type = Type_Splash;
588             else if (val[i] == prop_atoms.net_wm_window_type_dialog)
589                 self->type = Type_Dialog;
590             else if (val[i] == prop_atoms.net_wm_window_type_normal)
591                 self->type = Type_Normal;
592             else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
593                 /* prevent this window from getting any decor or
594                    functionality */
595                 self->mwmhints.flags &= (MwmFlag_Functions |
596                                          MwmFlag_Decorations);
597                 self->mwmhints.decorations = 0;
598                 self->mwmhints.functions = 0;
599             }
600             if (self->type != (WindowType) -1)
601                 break; /* grab the first legit type */
602         }
603         g_free(val);
604     }
605     
606     if (self->type == (WindowType) -1) {
607         /*the window type hint was not set, which means we either classify
608           ourself as a normal window or a dialog, depending on if we are a
609           transient. */
610         if (self->transient)
611             self->type = Type_Dialog;
612         else
613             self->type = Type_Normal;
614     }
615 }
616
617 void client_update_protocols(Client *self)
618 {
619     Atom *proto;
620     int num_return = 0, i;
621
622     self->focus_notify = FALSE;
623     self->delete_window = FALSE;
624
625     if (XGetWMProtocols(ob_display, self->window, &proto, &num_return)) {
626         for (i = 0; i < num_return; ++i) {
627             if (proto[i] == prop_atoms.wm_delete_window) {
628                 /* this means we can request the window to close */
629                 self->delete_window = TRUE;
630             } else if (proto[i] == prop_atoms.wm_take_focus)
631                 /* if this protocol is requested, then the window will be
632                    notified whenever we want it to receive focus */
633                 self->focus_notify = TRUE;
634         }
635         XFree(proto);
636     }
637 }
638
639 static void client_get_gravity(Client *self)
640 {
641     XWindowAttributes wattrib;
642     Status ret;
643
644     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
645     g_assert(ret != BadWindow);
646     self->gravity = wattrib.win_gravity;
647 }
648
649 void client_update_normal_hints(Client *self)
650 {
651     XSizeHints size;
652     long ret;
653     int oldgravity = self->gravity;
654
655     /* defaults */
656     self->min_ratio = 0.0f;
657     self->max_ratio = 0.0f;
658     SIZE_SET(self->size_inc, 1, 1);
659     SIZE_SET(self->base_size, 0, 0);
660     SIZE_SET(self->min_size, 0, 0);
661     SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
662
663     /* get the hints from the window */
664     if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
665         self->positioned = (size.flags & (PPosition|USPosition));
666
667         if (size.flags & PWinGravity) {
668             self->gravity = size.win_gravity;
669       
670             /* if the client has a frame, i.e. has already been mapped and
671                is changing its gravity */
672             if (self->frame && self->gravity != oldgravity) {
673                 /* move our idea of the client's position based on its new
674                    gravity */
675                 self->area.x = self->frame->area.x;
676                 self->area.y = self->frame->area.y;
677                 frame_frame_gravity(self->frame,
678                                     &self->area.x, &self->area.y);
679             }
680         }
681
682         if (size.flags & PAspect) {
683             if (size.min_aspect.y)
684                 self->min_ratio = size.min_aspect.x / size.min_aspect.y;
685             if (size.max_aspect.y)
686                 self->max_ratio = size.max_aspect.x / size.max_aspect.y;
687         }
688
689         if (size.flags & PMinSize)
690             SIZE_SET(self->min_size, size.min_width, size.min_height);
691     
692         if (size.flags & PMaxSize)
693             SIZE_SET(self->max_size, size.max_width, size.max_height);
694     
695         if (size.flags & PBaseSize)
696             SIZE_SET(self->base_size, size.base_width, size.base_height);
697     
698         if (size.flags & PResizeInc)
699             SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
700     }
701 }
702
703 void client_setup_decor_and_functions(Client *self)
704 {
705     /* start with everything (cept fullscreen) */
706     self->decorations = Decor_Titlebar | Decor_Handle | Decor_Border |
707         Decor_Icon | Decor_AllDesktops | Decor_Iconify | Decor_Maximize;
708     self->functions = Func_Resize | Func_Move | Func_Iconify | Func_Maximize |
709         Func_Shade;
710     if (self->delete_window) {
711         self->decorations |= Decor_Close;
712         self->functions |= Func_Close;
713     }
714
715     if (!(self->min_size.width < self->max_size.width ||
716           self->min_size.height < self->max_size.height)) {
717         self->decorations &= ~(Decor_Maximize | Decor_Handle);
718         self->functions &= ~(Func_Resize | Func_Maximize);
719     }
720
721     switch (self->type) {
722     case Type_Normal:
723         /* normal windows retain all of the possible decorations and
724            functionality, and are the only windows that you can fullscreen */
725         self->functions |= Func_Fullscreen;
726         break;
727
728     case Type_Dialog:
729         /* dialogs cannot be maximized */
730         self->decorations &= ~Decor_Maximize;
731         self->functions &= ~Func_Maximize;
732         break;
733
734     case Type_Menu:
735     case Type_Toolbar:
736     case Type_Utility:
737         /* these windows get less functionality */
738         self->decorations &= ~(Decor_Iconify | Decor_Handle);
739         self->functions &= ~(Func_Iconify | Func_Resize);
740         break;
741
742     case Type_Desktop:
743     case Type_Dock:
744     case Type_Splash:
745         /* none of these windows are manipulated by the window manager */
746         self->decorations = 0;
747         self->functions = 0;
748         break;
749     }
750
751     /* Mwm Hints are applied subtractively to what has already been chosen for
752        decor and functionality */
753     if (self->mwmhints.flags & MwmFlag_Decorations) {
754         if (! (self->mwmhints.decorations & MwmDecor_All)) {
755             if (! (self->mwmhints.decorations & MwmDecor_Border))
756                 self->decorations &= ~Decor_Border;
757             if (! (self->mwmhints.decorations & MwmDecor_Handle))
758                 self->decorations &= ~Decor_Handle;
759             if (! (self->mwmhints.decorations & MwmDecor_Title))
760                 self->decorations &= ~Decor_Titlebar;
761             if (! (self->mwmhints.decorations & MwmDecor_Iconify))
762                 self->decorations &= ~Decor_Iconify;
763             if (! (self->mwmhints.decorations & MwmDecor_Maximize))
764                 self->decorations &= ~Decor_Maximize;
765         }
766     }
767
768     if (self->mwmhints.flags & MwmFlag_Functions) {
769         if (! (self->mwmhints.functions & MwmFunc_All)) {
770             if (! (self->mwmhints.functions & MwmFunc_Resize))
771                 self->functions &= ~Func_Resize;
772             if (! (self->mwmhints.functions & MwmFunc_Move))
773                 self->functions &= ~Func_Move;
774             if (! (self->mwmhints.functions & MwmFunc_Iconify))
775                 self->functions &= ~Func_Iconify;
776             if (! (self->mwmhints.functions & MwmFunc_Maximize))
777                 self->functions &= ~Func_Maximize;
778             /* dont let mwm hints kill the close button
779                if (! (self->mwmhints.functions & MwmFunc_Close))
780                self->functions &= ~Func_Close; */
781         }
782     }
783
784     /* can't maximize without moving/resizing */
785     if (!((self->functions & Func_Move) && (self->functions & Func_Resize)))
786         self->functions &= ~Func_Maximize;
787
788     /* finally, user specified disabled decorations are applied to subtract
789        decorations */
790     if (self->disabled_decorations & Decor_Titlebar)
791         self->decorations &= ~Decor_Titlebar;
792     if (self->disabled_decorations & Decor_Handle)
793         self->decorations &= ~Decor_Handle;
794     if (self->disabled_decorations & Decor_Border)
795         self->decorations &= ~Decor_Border;
796     if (self->disabled_decorations & Decor_Iconify)
797         self->decorations &= ~Decor_Iconify;
798     if (self->disabled_decorations & Decor_Maximize)
799         self->decorations &= ~Decor_Maximize;
800     if (self->disabled_decorations & Decor_AllDesktops)
801         self->decorations &= ~Decor_AllDesktops;
802     if (self->disabled_decorations & Decor_Close)
803         self->decorations &= ~Decor_Close;
804
805     /* if we don't have a titlebar, then we cannot shade! */
806     if (!(self->decorations & Decor_Titlebar))
807         self->functions &= ~Func_Shade;
808
809     client_change_allowed_actions(self);
810
811     if (self->frame) {
812         /* change the decors on the frame */
813         frame_adjust_size(self->frame);
814         /* with more/less decorations, we may need to be repositioned */
815         frame_adjust_position(self->frame);
816         /* with new decor, the window's maximized size may change */
817         client_remaximize(self);
818     }
819 }
820
821 static void client_change_allowed_actions(Client *self)
822 {
823     Atom actions[9];
824     int num = 0;
825
826     actions[num++] = prop_atoms.net_wm_action_change_desktop;
827
828     if (self->functions & Func_Shade)
829         actions[num++] = prop_atoms.net_wm_action_shade;
830     if (self->functions & Func_Close)
831         actions[num++] = prop_atoms.net_wm_action_close;
832     if (self->functions & Func_Move)
833         actions[num++] = prop_atoms.net_wm_action_move;
834     if (self->functions & Func_Iconify)
835         actions[num++] = prop_atoms.net_wm_action_minimize;
836     if (self->functions & Func_Resize)
837         actions[num++] = prop_atoms.net_wm_action_resize;
838     if (self->functions & Func_Fullscreen)
839         actions[num++] = prop_atoms.net_wm_action_fullscreen;
840     if (self->functions & Func_Maximize) {
841         actions[num++] = prop_atoms.net_wm_action_maximize_horz;
842         actions[num++] = prop_atoms.net_wm_action_maximize_vert;
843     }
844
845     PROP_SET32A(self->window, net_wm_allowed_actions, atom, actions, num);
846
847     /* make sure the window isn't breaking any rules now */
848
849     if (!(self->functions & Func_Shade) && self->shaded) {
850         if (self->frame) client_shade(self, FALSE);
851         else self->shaded = FALSE;
852     }
853     if (!(self->functions & Func_Iconify) && self->iconic) {
854         if (self->frame) client_iconify(self, FALSE, TRUE);
855         else self->iconic = FALSE;
856     }
857     if (!(self->functions & Func_Fullscreen) && self->fullscreen) {
858         if (self->frame) client_fullscreen(self, FALSE, TRUE);
859         else self->fullscreen = FALSE;
860     }
861     if (!(self->functions & Func_Maximize) && (self->max_horz ||
862                                                self->max_vert)) {
863         if (self->frame) client_maximize(self, FALSE, 0, TRUE);
864         else self->max_vert = self->max_horz = FALSE;
865     }
866 }
867
868 void client_remaximize(Client *self)
869 {
870     int dir;
871     if (self->max_horz && self->max_vert)
872         dir = 0;
873     else if (self->max_horz)
874         dir = 1;
875     else if (self->max_vert)
876         dir = 2;
877     else
878         return; /* not maximized */
879     self->max_horz = self->max_vert = FALSE;
880     client_maximize(self, TRUE, dir, FALSE);
881 }
882
883 void client_update_wmhints(Client *self)
884 {
885     XWMHints *hints;
886     gboolean ur = FALSE;
887
888     /* assume a window takes input if it doesnt specify */
889     self->can_focus = TRUE;
890   
891     if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
892         if (hints->flags & InputHint)
893             self->can_focus = hints->input;
894
895         /* only do this when starting! */
896         if (ob_state == State_Starting && (hints->flags & StateHint))
897             self->iconic = hints->initial_state == IconicState;
898
899         if (hints->flags & XUrgencyHint)
900             ur = TRUE;
901
902         if (hints->flags & WindowGroupHint) {
903             if (hints->window_group != self->group) {
904                 /* XXX: remove from the old group if there was one */
905                 self->group = hints->window_group;
906                 /* XXX: do stuff with the group */
907             }
908         } else /* no group! */
909             self->group = None;
910
911         if (hints->flags & IconPixmapHint) {
912             client_update_kwm_icon(self);
913             /* try get the kwm icon first, this is a fallback only */
914             if (self->pixmap_icon == None) {
915                 self->pixmap_icon = hints->icon_pixmap;
916                 if (hints->flags & IconMaskHint)
917                     self->pixmap_icon_mask = hints->icon_mask;
918                 else
919                     self->pixmap_icon_mask = None;
920
921                 if (self->frame)
922                     frame_adjust_icon(self->frame);
923             }
924         }
925
926         XFree(hints);
927     }
928
929     if (ur != self->urgent) {
930         self->urgent = ur;
931         g_message("Urgent Hint for 0x%lx: %s\n", self->window,
932                   ur ? "ON" : "OFF");
933         /* fire the urgent callback if we're mapped, otherwise, wait until
934            after we're mapped */
935         if (self->frame)
936             LOGICALHOOK(UrgentWindow, g_quark_try_string("client"), self);
937     }
938 }
939
940 void client_update_title(Client *self)
941 {
942     gchar *data = NULL;
943
944     if (self->title != NULL)
945         g_free(self->title);
946      
947     /* try netwm */
948     if (!PROP_GETS(self->window, net_wm_name, utf8, data)) {
949         /* try old x stuff */
950         if (PROP_GETS(self->window, wm_name, string, data)) {
951             /* convert it to UTF-8 */
952             gsize r, w;
953             gchar *u;
954
955             u = g_locale_to_utf8(data, -1, &r, &w, NULL);
956             if (u == NULL) {
957                 g_warning("Unable to convert string to UTF-8");
958             } else {
959                 g_free(data);
960                 data = u;
961             }
962         }
963     }
964
965     if (data == NULL)
966         data = g_strdup("Unnamed Window");
967
968     self->title = data;
969
970     if (self->frame)
971         frame_adjust_title(self->frame);
972 }
973
974 void client_update_icon_title(Client *self)
975 {
976     gchar *data = NULL;
977
978     if (self->icon_title != NULL)
979         g_free(self->icon_title);
980      
981     /* try netwm */
982     if (!PROP_GETS(self->window, net_wm_icon_name, utf8, data)) {
983         /* try old x stuff */
984         if (PROP_GETS(self->window, wm_icon_name, string, data)) {
985             /* convert it to UTF-8 */
986             gsize r, w;
987             gchar *u;
988
989             u = g_locale_to_utf8(data, -1, &r, &w, NULL);
990             if (u == NULL) {
991                 g_warning("Unable to convert string to UTF-8");
992             } else {
993                 g_free(data);
994                 data = u;
995             }
996         }
997     }
998
999     if (data == NULL)
1000         data = g_strdup(self->title);
1001
1002     self->icon_title = data;
1003 }
1004
1005 void client_update_class(Client *self)
1006 {
1007     GPtrArray *data;
1008     gchar *s;
1009     guint i;
1010
1011     if (self->res_name) g_free(self->res_name);
1012     if (self->res_class) g_free(self->res_class);
1013     if (self->role) g_free(self->role);
1014
1015     self->res_name = self->res_class = self->role = NULL;
1016
1017     data = g_ptr_array_new();
1018      
1019     if (PROP_GETSA(self->window, wm_class, string, data)) {
1020         if (data->len > 0)
1021             self->res_name = g_strdup(g_ptr_array_index(data, 0));
1022         if (data->len > 1)
1023             self->res_class = g_strdup(g_ptr_array_index(data, 1));
1024     }
1025      
1026     for (i = 0; i < data->len; ++i)
1027         g_free(g_ptr_array_index(data, i));
1028     g_ptr_array_free(data, TRUE);
1029
1030     if (PROP_GETS(self->window, wm_window_role, string, s))
1031         self->role = g_strdup(s);
1032
1033     if (self->res_name == NULL) self->res_name = g_strdup("");
1034     if (self->res_class == NULL) self->res_class = g_strdup("");
1035     if (self->role == NULL) self->role = g_strdup("");
1036 }
1037
1038 void client_update_strut(Client *self)
1039 {
1040     gulong num = 4;
1041     gulong *data;
1042
1043     if (PROP_GET32A(self->window, net_wm_strut, cardinal, data, num)) {
1044         STRUT_SET(self->strut, data[0], data[1], data[2], data[3]);
1045         g_free(data);
1046     } else
1047         STRUT_SET(self->strut, 0, 0, 0, 0);
1048
1049     /* updating here is pointless while we're being mapped cuz we're not in
1050        the client list yet */
1051     if (self->frame)
1052         screen_update_struts();
1053 }
1054
1055 void client_update_icons(Client *self)
1056 {
1057     unsigned long num;
1058     unsigned long *data;
1059     unsigned long w, h, i;
1060     int j;
1061
1062     for (j = 0; j < self->nicons; ++j)
1063         g_free(self->icons[j].data);
1064     if (self->nicons > 0)
1065         g_free(self->icons);
1066     self->nicons = 0;
1067
1068     if (PROP_GET32U(self->window, net_wm_icon, cardinal, data, num)) {
1069         /* figure out how many valid icons are in here */
1070         i = 0;
1071         while (num - i > 2) {
1072             w = data[i++];
1073             h = data[i++];
1074             i += w * h;
1075             if (i > num) break;
1076             ++self->nicons;
1077         }
1078
1079         self->icons = g_new(Icon, self->nicons);
1080     
1081         /* store the icons */
1082         i = 0;
1083         for (j = 0; j < self->nicons; ++j) {
1084             w = self->icons[j].w = data[i++];
1085             h = self->icons[j].h = data[i++];
1086             self->icons[j].data =
1087                 g_memdup(&data[i], w * h * sizeof(gulong));
1088             i += w * h;
1089             g_assert(i <= num);
1090         }
1091
1092         g_free(data);
1093     }
1094
1095     if (self->nicons <= 0) {
1096         self->nicons = 1;
1097         self->icons = g_new0(Icon, 1);
1098     }
1099
1100     if (self->frame)
1101         frame_adjust_icon(self->frame);
1102 }
1103
1104 void client_update_kwm_icon(Client *self)
1105 {
1106     Pixmap *data;
1107
1108     if (PROP_GET32A(self->window, kwm_win_icon, kwm_win_icon, data, 2)) {
1109         self->pixmap_icon = data[0];
1110         self->pixmap_icon_mask = data[1];
1111         g_free(data);
1112     } else {
1113         self->pixmap_icon = self->pixmap_icon_mask = None;
1114     }
1115     if (self->frame)
1116         frame_adjust_icon(self->frame);
1117 }
1118
1119 static void client_change_state(Client *self)
1120 {
1121     unsigned long state[2];
1122     Atom netstate[10];
1123     int num;
1124
1125     state[0] = self->wmstate;
1126     state[1] = None;
1127     PROP_SET32A(self->window, wm_state, wm_state, state, 2);
1128
1129     num = 0;
1130     if (self->modal)
1131         netstate[num++] = prop_atoms.net_wm_state_modal;
1132     if (self->shaded)
1133         netstate[num++] = prop_atoms.net_wm_state_shaded;
1134     if (self->iconic)
1135         netstate[num++] = prop_atoms.net_wm_state_hidden;
1136     if (self->skip_taskbar)
1137         netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1138     if (self->skip_pager)
1139         netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1140     if (self->fullscreen)
1141         netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1142     if (self->max_vert)
1143         netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1144     if (self->max_horz)
1145         netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1146     if (self->above)
1147         netstate[num++] = prop_atoms.net_wm_state_above;
1148     if (self->below)
1149         netstate[num++] = prop_atoms.net_wm_state_below;
1150     PROP_SET32A(self->window, net_wm_state, atom, netstate, num);
1151
1152     client_calc_layer(self);
1153
1154     if (self->frame)
1155         frame_adjust_state(self->frame);
1156 }
1157
1158 static Client *search_focus_tree(Client *node, Client *skip)
1159 {
1160     GSList *it;
1161     Client *ret;
1162
1163     for (it = node->transients; it != NULL; it = g_slist_next(it)) {
1164         Client *c = it->data;
1165         if (c == skip) continue; /* circular? */
1166         if ((ret = search_focus_tree(c, skip))) return ret;
1167         if (c->focused) return c;
1168     }
1169     return NULL;
1170 }
1171
1172 void client_calc_layer(Client *self)
1173 {
1174     StackLayer l;
1175     gboolean fs;
1176     Client *c;
1177
1178     /* are we fullscreen, or do we have a fullscreen transient parent? */
1179     c = self;
1180     fs = FALSE;
1181     while (c) {
1182         if (c->fullscreen) {
1183             fs = TRUE;
1184             break;
1185         }
1186         c = c->transient_for;
1187     }
1188     if (!fs && self->fullscreen) {
1189         /* is one of our transients focused? */
1190         c = search_focus_tree(self, self);
1191         if (c != NULL) fs = TRUE;
1192     }
1193   
1194     if (self->iconic) l = Layer_Icon;
1195     else if (fs) l = Layer_Fullscreen;
1196     else if (self->type == Type_Desktop) l = Layer_Desktop;
1197     else if (self->type == Type_Dock) {
1198         if (!self->below) l = Layer_Top;
1199         else l = Layer_Normal;
1200     }
1201     else if (self->above) l = Layer_Above;
1202     else if (self->below) l = Layer_Below;
1203     else l = Layer_Normal;
1204      
1205     if (l != self->layer) {
1206         self->layer = l;
1207         if (self->frame)
1208             stacking_raise(self);
1209     }
1210 }
1211
1212 void client_showhide(Client *self)
1213 {
1214     gboolean show;
1215
1216     if (self->iconic) show = FALSE;
1217     else if (!(self->desktop == screen_desktop ||
1218                self->desktop == DESKTOP_ALL)) show = FALSE;
1219     else if (client_normal(self) && screen_showing_desktop) show = FALSE;
1220     else show = TRUE;
1221
1222     if (show) frame_show(self->frame);
1223     else      frame_hide(self->frame);
1224 }
1225
1226 gboolean client_normal(Client *self) {
1227     return ! (self->type == Type_Desktop || self->type == Type_Dock ||
1228               self->type == Type_Splash);
1229 }
1230
1231 static void client_apply_startup_state(Client *self)
1232 {
1233     /* these are in a carefully crafted order.. */
1234
1235     if (self->iconic) {
1236         self->iconic = FALSE;
1237         client_iconify(self, TRUE, FALSE);
1238     }
1239     if (self->fullscreen) {
1240         self->fullscreen = FALSE;
1241         client_fullscreen(self, TRUE, FALSE);
1242     }
1243     if (self->shaded) {
1244         self->shaded = FALSE;
1245         client_shade(self, TRUE);
1246     }
1247     if (self->urgent)
1248         LOGICALHOOK(UrgentWindow, g_quark_try_string("client"), self);
1249   
1250     if (self->max_vert && self->max_horz) {
1251         self->max_vert = self->max_horz = FALSE;
1252         client_maximize(self, TRUE, 0, FALSE);
1253     } else if (self->max_vert) {
1254         self->max_vert = FALSE;
1255         client_maximize(self, TRUE, 2, FALSE);
1256     } else if (self->max_horz) {
1257         self->max_horz = FALSE;
1258         client_maximize(self, TRUE, 1, FALSE);
1259     }
1260
1261     /* nothing to do for the other states:
1262        skip_taskbar
1263        skip_pager
1264        modal
1265        above
1266        below
1267     */
1268 }
1269
1270 void client_configure(Client *self, Corner anchor, int x, int y, int w, int h,
1271                       gboolean user, gboolean final)
1272 {
1273     w -= self->base_size.width;
1274     h -= self->base_size.height;
1275
1276     if (user) {
1277         /* for interactive resizing. have to move half an increment in each
1278            direction. */
1279
1280         /* how far we are towards the next size inc */
1281         int mw = w % self->size_inc.width; 
1282         int mh = h % self->size_inc.height;
1283         /* amount to add */
1284         int aw = self->size_inc.width / 2;
1285         int ah = self->size_inc.height / 2;
1286         /* don't let us move into a new size increment */
1287         if (mw + aw >= self->size_inc.width)
1288             aw = self->size_inc.width - mw - 1;
1289         if (mh + ah >= self->size_inc.height)
1290             ah = self->size_inc.height - mh - 1;
1291         w += aw;
1292         h += ah;
1293     
1294         /* if this is a user-requested resize, then check against min/max
1295            sizes and aspect ratios */
1296
1297         /* smaller than min size or bigger than max size? */
1298         if (w > self->max_size.width) w = self->max_size.width;
1299         if (w < self->min_size.width) w = self->min_size.width;
1300         if (h > self->max_size.height) h = self->max_size.height;
1301         if (h < self->min_size.height) h = self->min_size.height;
1302
1303         /* adjust the height ot match the width for the aspect ratios */
1304         if (self->min_ratio)
1305             if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1306         if (self->max_ratio)
1307             if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1308     }
1309
1310     /* keep to the increments */
1311     w /= self->size_inc.width;
1312     h /= self->size_inc.height;
1313
1314     /* you cannot resize to nothing */
1315     if (w < 1) w = 1;
1316     if (h < 1) h = 1;
1317   
1318     /* store the logical size */
1319     SIZE_SET(self->logical_size, w, h);
1320
1321     w *= self->size_inc.width;
1322     h *= self->size_inc.height;
1323
1324     w += self->base_size.width;
1325     h += self->base_size.height;
1326
1327     switch (anchor) {
1328     case Corner_TopLeft:
1329         break;
1330     case Corner_TopRight:
1331         x -= w - self->area.width;
1332         break;
1333     case Corner_BottomLeft:
1334         y -= h - self->area.height;
1335         break;
1336     case Corner_BottomRight:
1337         x -= w - self->area.width;
1338         y -= h - self->area.height;
1339         break;
1340     }
1341
1342     RECT_SET(self->area, x, y, w, h);
1343
1344     XResizeWindow(ob_display, self->window, w, h);
1345
1346     /* move/resize the frame to match the request */
1347     if (self->frame) {
1348         /* Adjust the size and then the position, as required by the EWMH */
1349         frame_adjust_size(self->frame);
1350         frame_adjust_position(self->frame);
1351
1352         if (!user || final) {
1353             XEvent event;
1354             event.type = ConfigureNotify;
1355             event.xconfigure.display = ob_display;
1356             event.xconfigure.event = self->window;
1357             event.xconfigure.window = self->window;
1358     
1359             /* root window coords with border in mind */
1360             event.xconfigure.x = x - self->border_width +
1361                 self->frame->size.left;
1362             event.xconfigure.y = y - self->border_width +
1363                 self->frame->size.top;
1364     
1365             event.xconfigure.width = self->area.width;
1366             event.xconfigure.height = self->area.height;
1367             event.xconfigure.border_width = self->border_width;
1368             event.xconfigure.above = self->frame->plate;
1369             event.xconfigure.override_redirect = FALSE;
1370             XSendEvent(event.xconfigure.display, event.xconfigure.window,
1371                        FALSE, StructureNotifyMask, &event);
1372         }
1373     }
1374 }
1375
1376 void client_fullscreen(Client *self, gboolean fs, gboolean savearea)
1377 {
1378     static int saved_func, saved_decor;
1379     int x, y, w, h;
1380
1381     if (!(self->functions & Func_Fullscreen) || /* can't */
1382         self->fullscreen == fs) return;         /* already done */
1383
1384     self->fullscreen = fs;
1385     client_change_state(self); /* change the state hints on the client */
1386
1387     if (fs) {
1388         /* save the functions and remove them */
1389         saved_func = self->functions;
1390         self->functions &= (Func_Close | Func_Fullscreen |
1391                             Func_Iconify);
1392         /* save the decorations and remove them */
1393         saved_decor = self->decorations;
1394         self->decorations = 0;
1395         if (savearea) {
1396             long dimensions[4];
1397             dimensions[0] = self->area.x;
1398             dimensions[1] = self->area.y;
1399             dimensions[2] = self->area.width;
1400             dimensions[3] = self->area.height;
1401   
1402             PROP_SET32A(self->window, openbox_premax, cardinal,
1403                         dimensions, 4);
1404         }
1405         x = 0;
1406         y = 0;
1407         w = screen_physical_size.width;
1408         h = screen_physical_size.height;
1409     } else {
1410         long *dimensions;
1411
1412         self->functions = saved_func;
1413         self->decorations = saved_decor;
1414           
1415         if (PROP_GET32A(self->window, openbox_premax, cardinal,
1416                         dimensions, 4)) {
1417             x = dimensions[0];
1418             y = dimensions[1];
1419             w = dimensions[2];
1420             h = dimensions[3];
1421             g_free(dimensions);
1422         } else {
1423             /* pick some fallbacks... */
1424             x = screen_area(self->desktop)->x +
1425                 screen_area(self->desktop)->width / 4;
1426             y = screen_area(self->desktop)->y +
1427                 screen_area(self->desktop)->height / 4;
1428             w = screen_area(self->desktop)->width / 2;
1429             h = screen_area(self->desktop)->height / 2;
1430         }
1431     }
1432
1433     client_change_allowed_actions(self); /* based on the new _functions */
1434
1435     /* when fullscreening, don't obey things like increments, fill the
1436        screen */
1437     client_configure(self, Corner_TopLeft, x, y, w, h, !fs, TRUE);
1438
1439     /* raise (back) into our stacking layer */
1440     stacking_raise(self);
1441
1442     /* try focus us when we go into fullscreen mode */
1443     client_focus(self);
1444 }
1445
1446 void client_iconify(Client *self, gboolean iconic, gboolean curdesk)
1447 {
1448     if (self->iconic == iconic) return; /* nothing to do */
1449
1450     g_message("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"),
1451               self->window);
1452
1453     self->iconic = iconic;
1454
1455     if (iconic) {
1456         self->wmstate = IconicState;
1457         self->ignore_unmaps++;
1458         /* we unmap the client itself so that we can get MapRequest events,
1459            and because the ICCCM tells us to! */
1460         XUnmapWindow(ob_display, self->window);
1461     } else {
1462         if (curdesk)
1463             client_set_desktop(self, screen_desktop);
1464         self->wmstate = self->shaded ? IconicState : NormalState;
1465         XMapWindow(ob_display, self->window);
1466     }
1467     client_change_state(self);
1468     client_showhide(self);
1469     screen_update_struts();
1470 }
1471
1472 void client_maximize(Client *self, gboolean max, int dir, gboolean savearea)
1473 {
1474     int x, y, w, h;
1475      
1476     g_assert(dir == 0 || dir == 1 || dir == 2);
1477     if (!(self->functions & Func_Maximize)) return; /* can't */
1478
1479     /* check if already done */
1480     if (max) {
1481         if (dir == 0 && self->max_horz && self->max_vert) return;
1482         if (dir == 1 && self->max_horz) return;
1483         if (dir == 2 && self->max_vert) return;
1484     } else {
1485         if (dir == 0 && !self->max_horz && !self->max_vert) return;
1486         if (dir == 1 && !self->max_horz) return;
1487         if (dir == 2 && !self->max_vert) return;
1488     }
1489
1490     x = self->frame->area.x;
1491     y = self->frame->area.y;
1492     w = self->frame->area.width;
1493     h = self->frame->area.height;
1494   
1495     if (max) {
1496         if (savearea) {
1497             long dimensions[4];
1498             long *readdim;
1499
1500             dimensions[0] = x;
1501             dimensions[1] = y;
1502             dimensions[2] = w;
1503             dimensions[3] = h;
1504
1505             /* get the property off the window and use it for the dimensions
1506                we are already maxed on */
1507             if (PROP_GET32A(self->window, openbox_premax, cardinal,
1508                             readdim, 4)) {
1509                 if (self->max_horz) {
1510                     dimensions[0] = readdim[0];
1511                     dimensions[2] = readdim[2];
1512                 }
1513                 if (self->max_vert) {
1514                     dimensions[1] = readdim[1];
1515                     dimensions[3] = readdim[3];
1516                 }
1517                 g_free(readdim);
1518             }
1519
1520             PROP_SET32A(self->window, openbox_premax, cardinal,
1521                         dimensions, 4);
1522         }
1523         if (dir == 0 || dir == 1) { /* horz */
1524             x = screen_area(self->desktop)->x;
1525             w = screen_area(self->desktop)->x +
1526                 screen_area(self->desktop)->width;
1527         }
1528         if (dir == 0 || dir == 2) { /* vert */
1529             y = screen_area(self->desktop)->y;
1530             h = screen_area(self->desktop)->y +
1531                 screen_area(self->desktop)->height -
1532                 self->frame->size.top - self->frame->size.bottom;
1533         }
1534     } else {
1535         long *dimensions;
1536
1537         if (PROP_GET32A(self->window, openbox_premax, cardinal,
1538                         dimensions, 4)) {
1539             if (dir == 0 || dir == 1) { /* horz */
1540                 x = dimensions[0];
1541                 w = dimensions[2];
1542             }
1543             if (dir == 0 || dir == 2) { /* vert */
1544                 y = dimensions[1];
1545                 h = dimensions[3];
1546             }
1547             g_free(dimensions);
1548         } else {
1549             /* pick some fallbacks... */
1550             if (dir == 0 || dir == 1) { /* horz */
1551                 x = screen_area(self->desktop)->x +
1552                     screen_area(self->desktop)->width / 4;
1553                 w = screen_area(self->desktop)->width / 2;
1554             }
1555             if (dir == 0 || dir == 2) { /* vert */
1556                 y = screen_area(self->desktop)->y +
1557                     screen_area(self->desktop)->height / 4;
1558                 h = screen_area(self->desktop)->height / 2;
1559             }
1560         }
1561     }
1562
1563     if (dir == 0 || dir == 1) /* horz */
1564         self->max_horz = max;
1565     if (dir == 0 || dir == 2) /* vert */
1566         self->max_vert = max;
1567
1568     if (!self->max_horz && !self->max_vert)
1569         PROP_ERASE(self->window, openbox_premax);
1570
1571     client_change_state(self); /* change the state hints on the client */
1572
1573     /* figure out where the client should be going */
1574     frame_frame_gravity(self->frame, &x, &y);
1575     client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1576 }
1577
1578 void client_shade(Client *self, gboolean shade)
1579 {
1580     if (!(self->functions & Func_Shade) || /* can't */
1581         self->shaded == shade) return;     /* already done */
1582
1583     /* when we're iconic, don't change the wmstate */
1584     if (!self->iconic)
1585         self->wmstate = shade ? IconicState : NormalState;
1586     self->shaded = shade;
1587     client_change_state(self);
1588     frame_adjust_size(self->frame);
1589 }
1590
1591 void client_close(Client *self)
1592 {
1593     XEvent ce;
1594
1595     if (!(self->functions & Func_Close)) return;
1596
1597     /*
1598       XXX: itd be cool to do timeouts and shit here for killing the client's
1599       process off
1600       like... if the window is around after 5 seconds, then the close button
1601       turns a nice red, and if this function is called again, the client is
1602       explicitly killed.
1603     */
1604
1605     ce.xclient.type = ClientMessage;
1606     ce.xclient.message_type =  prop_atoms.wm_protocols;
1607     ce.xclient.display = ob_display;
1608     ce.xclient.window = self->window;
1609     ce.xclient.format = 32;
1610     ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
1611     ce.xclient.data.l[1] = CurrentTime;
1612     ce.xclient.data.l[2] = 0l;
1613     ce.xclient.data.l[3] = 0l;
1614     ce.xclient.data.l[4] = 0l;
1615     XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
1616 }
1617
1618 void client_set_desktop(Client *self, unsigned int target)
1619 {
1620     if (target == self->desktop) return;
1621   
1622     g_message("Setting desktop %u\n", target);
1623
1624     if (!(target < screen_num_desktops ||
1625           target == DESKTOP_ALL))
1626         return;
1627
1628     self->desktop = target;
1629     PROP_SET32(self->window, net_wm_desktop, cardinal, target);
1630     /* the frame can display the current desktop state */
1631     frame_adjust_state(self->frame);
1632     /* 'move' the window to the new desktop */
1633     client_showhide(self);
1634     screen_update_struts();
1635 }
1636
1637 static Client *search_modal_tree(Client *node, Client *skip)
1638 {
1639     GSList *it;
1640     Client *ret;
1641   
1642     for (it = node->transients; it != NULL; it = it->next) {
1643         Client *c = it->data;
1644         if (c == skip) continue; /* circular? */
1645         if ((ret = search_modal_tree(c, skip))) return ret;
1646         if (c->modal) return c;
1647     }
1648     return NULL;
1649 }
1650
1651 Client *client_find_modal_child(Client *self)
1652 {
1653     return search_modal_tree(self, self);
1654 }
1655
1656 gboolean client_validate(Client *self)
1657 {
1658     XEvent e; 
1659
1660     XSync(ob_display, FALSE); /* get all events on the server */
1661
1662     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
1663         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
1664         XPutBackEvent(ob_display, &e);
1665         return FALSE;
1666     }
1667
1668     return FALSE;
1669 }
1670
1671 void client_set_wm_state(Client *self, long state)
1672 {
1673     if (state == self->wmstate) return; /* no change */
1674   
1675     switch (state) {
1676     case IconicState:
1677         client_iconify(self, TRUE, TRUE);
1678         break;
1679     case NormalState:
1680         client_iconify(self, FALSE, TRUE);
1681         break;
1682     }
1683 }
1684
1685 void client_set_state(Client *self, Atom action, long data1, long data2)
1686 {
1687     gboolean shaded = self->shaded;
1688     gboolean fullscreen = self->fullscreen;
1689     gboolean max_horz = self->max_horz;
1690     gboolean max_vert = self->max_vert;
1691     int i;
1692
1693     if (!(action == prop_atoms.net_wm_state_add ||
1694           action == prop_atoms.net_wm_state_remove ||
1695           action == prop_atoms.net_wm_state_toggle))
1696         /* an invalid action was passed to the client message, ignore it */
1697         return; 
1698
1699     for (i = 0; i < 2; ++i) {
1700         Atom state = i == 0 ? data1 : data2;
1701     
1702         if (!state) continue;
1703
1704         /* if toggling, then pick whether we're adding or removing */
1705         if (action == prop_atoms.net_wm_state_toggle) {
1706             if (state == prop_atoms.net_wm_state_modal)
1707                 action = self->modal ? prop_atoms.net_wm_state_remove :
1708                     prop_atoms.net_wm_state_add;
1709             else if (state == prop_atoms.net_wm_state_maximized_vert)
1710                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
1711                     prop_atoms.net_wm_state_add;
1712             else if (state == prop_atoms.net_wm_state_maximized_horz)
1713                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
1714                     prop_atoms.net_wm_state_add;
1715             else if (state == prop_atoms.net_wm_state_shaded)
1716                 action = self->shaded ? prop_atoms.net_wm_state_remove :
1717                     prop_atoms.net_wm_state_add;
1718             else if (state == prop_atoms.net_wm_state_skip_taskbar)
1719                 action = self->skip_taskbar ?
1720                     prop_atoms.net_wm_state_remove :
1721                     prop_atoms.net_wm_state_add;
1722             else if (state == prop_atoms.net_wm_state_skip_pager)
1723                 action = self->skip_pager ?
1724                     prop_atoms.net_wm_state_remove :
1725                     prop_atoms.net_wm_state_add;
1726             else if (state == prop_atoms.net_wm_state_fullscreen)
1727                 action = self->fullscreen ?
1728                     prop_atoms.net_wm_state_remove :
1729                     prop_atoms.net_wm_state_add;
1730             else if (state == prop_atoms.net_wm_state_above)
1731                 action = self->above ? prop_atoms.net_wm_state_remove :
1732                     prop_atoms.net_wm_state_add;
1733             else if (state == prop_atoms.net_wm_state_below)
1734                 action = self->below ? prop_atoms.net_wm_state_remove :
1735                     prop_atoms.net_wm_state_add;
1736         }
1737     
1738         if (action == prop_atoms.net_wm_state_add) {
1739             if (state == prop_atoms.net_wm_state_modal) {
1740                 /* XXX raise here or something? */
1741                 self->modal = TRUE;
1742             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
1743                 max_vert = TRUE;
1744             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
1745                 max_horz = TRUE;
1746             } else if (state == prop_atoms.net_wm_state_shaded) {
1747                 shaded = TRUE;
1748             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
1749                 self->skip_taskbar = TRUE;
1750             } else if (state == prop_atoms.net_wm_state_skip_pager) {
1751                 self->skip_pager = TRUE;
1752             } else if (state == prop_atoms.net_wm_state_fullscreen) {
1753                 fullscreen = TRUE;
1754             } else if (state == prop_atoms.net_wm_state_above) {
1755                 self->above = TRUE;
1756             } else if (state == prop_atoms.net_wm_state_below) {
1757                 self->below = TRUE;
1758             }
1759
1760         } else { /* action == prop_atoms.net_wm_state_remove */
1761             if (state == prop_atoms.net_wm_state_modal) {
1762                 self->modal = FALSE;
1763             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
1764                 max_vert = FALSE;
1765             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
1766                 max_horz = FALSE;
1767             } else if (state == prop_atoms.net_wm_state_shaded) {
1768                 shaded = FALSE;
1769             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
1770                 self->skip_taskbar = FALSE;
1771             } else if (state == prop_atoms.net_wm_state_skip_pager) {
1772                 self->skip_pager = FALSE;
1773             } else if (state == prop_atoms.net_wm_state_fullscreen) {
1774                 fullscreen = FALSE;
1775             } else if (state == prop_atoms.net_wm_state_above) {
1776                 self->above = FALSE;
1777             } else if (state == prop_atoms.net_wm_state_below) {
1778                 self->below = FALSE;
1779             }
1780         }
1781     }
1782     if (max_horz != self->max_horz || max_vert != self->max_vert) {
1783         if (max_horz != self->max_horz && max_vert != self->max_vert) {
1784             /* toggling both */
1785             if (max_horz == max_vert) { /* both going the same way */
1786                 client_maximize(self, max_horz, 0, TRUE);
1787             } else {
1788                 client_maximize(self, max_horz, 1, TRUE);
1789                 client_maximize(self, max_vert, 2, TRUE);
1790             }
1791         } else {
1792             /* toggling one */
1793             if (max_horz != self->max_horz)
1794                 client_maximize(self, max_horz, 1, TRUE);
1795             else
1796                 client_maximize(self, max_vert, 2, TRUE);
1797         }
1798     }
1799     /* change fullscreen state before shading, as it will affect if the window
1800        can shade or not */
1801     if (fullscreen != self->fullscreen)
1802         client_fullscreen(self, fullscreen, TRUE);
1803     if (shaded != self->shaded)
1804         client_shade(self, shaded);
1805     client_calc_layer(self);
1806     client_change_state(self); /* change the hint to relect these changes */
1807 }
1808
1809 gboolean client_focus(Client *self)
1810 {
1811     XEvent ev;
1812     Client *child;
1813      
1814     /* if we have a modal child, then focus it, not us */
1815     child = client_find_modal_child(self);
1816     if (child)
1817         return client_focus(child);
1818
1819     /* won't try focus if the client doesn't want it, or if the window isn't
1820        visible on the screen */
1821     if (!(self->frame->visible &&
1822           (self->can_focus || self->focus_notify)))
1823         return FALSE;
1824
1825     /* do a check to see if the window has already been unmapped or destroyed
1826        do this intelligently while watching out for unmaps we've generated
1827        (ignore_unmaps > 0) */
1828     if (XCheckTypedWindowEvent(ob_display, self->window,
1829                                DestroyNotify, &ev)) {
1830         XPutBackEvent(ob_display, &ev);
1831         return FALSE;
1832     }
1833     while (XCheckTypedWindowEvent(ob_display, self->window,
1834                                   UnmapNotify, &ev)) {
1835         if (self->ignore_unmaps) {
1836             self->ignore_unmaps--;
1837         } else {
1838             XPutBackEvent(ob_display, &ev);
1839             return FALSE;
1840         }
1841     }
1842      
1843     if (self->can_focus)
1844         XSetInputFocus(ob_display, self->window, RevertToNone, CurrentTime);
1845
1846     if (self->focus_notify) {
1847         XEvent ce;
1848         ce.xclient.type = ClientMessage;
1849         ce.xclient.message_type = prop_atoms.wm_protocols;
1850         ce.xclient.display = ob_display;
1851         ce.xclient.window = self->window;
1852         ce.xclient.format = 32;
1853         ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
1854         ce.xclient.data.l[1] = event_lasttime;
1855         ce.xclient.data.l[2] = 0l;
1856         ce.xclient.data.l[3] = 0l;
1857         ce.xclient.data.l[4] = 0l;
1858         XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
1859     }
1860
1861     /*XSync(ob_display, FALSE); XXX Why sync? */
1862     return TRUE;
1863 }
1864
1865 void client_unfocus(Client *self)
1866 {
1867     g_assert(focus_client == self);
1868     focus_set_client(NULL);
1869 }