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