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