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