]> icculus.org git repositories - dana/openbox.git/blob - openbox/client.c
make sure windows which cant be moved or resized also can't be fullscreened
[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     client_list = g_slist_remove(client_list, client);
243     stacking_list = g_list_remove(stacking_list, client);
244     g_hash_table_remove(client_map, (gpointer)client->window);
245
246     /* update the focus lists */
247     if (client->desktop == DESKTOP_ALL) {
248         for (i = 0; i < screen_num_desktops; ++i)
249             focus_order[i] = g_list_remove(focus_order[i], client);
250     } else {
251         i = client->desktop;
252         focus_order[i] = g_list_remove(focus_order[i], client);
253     }
254
255     /* once the client is out of the list, update the struts to remove it's
256        influence */
257     screen_update_struts();
258
259     /* tell our parent that we're gone */
260     if (client->transient_for != NULL)
261         client->transient_for->transients =
262             g_slist_remove(client->transient_for->transients, client);
263
264     /* tell our transients that we're gone */
265     for (it = client->transients; it != NULL; it = it->next) {
266         ((Client*)it->data)->transient_for = NULL;
267         client_calc_layer(it->data);
268     }
269
270     /* dispatch the unmapped event */
271     dispatch_client(Event_Client_Unmapped, client, 0, 0);
272     g_assert(client != NULL);
273
274     /* unfocus the client (dispatchs the focus event) (we're out of the
275      transient lists already, so being modal doesn't matter) */
276     if (client_focused(client))
277         client_unfocus(client);
278
279     /* give the client its border back */
280     client_toggle_border(client, TRUE);
281
282     /* reparent the window out of the frame, and free the frame */
283     engine_frame_release_client(client->frame, client);
284     client->frame = NULL;
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 | Func_Fullscreen);
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, TRUE, TRUE);
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", 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     /* these override the above states! if you cant move you can't move! */
1311     if (user) {
1312         if (!(self->functions & Func_Move)) {
1313             x = self->area.x;
1314             y = self->area.y;
1315         }
1316         if (!(self->functions & Func_Resize)) {
1317             w = self->area.width;
1318             h = self->area.height;
1319         }
1320     }
1321
1322     if (x == self->area.x && y == self->area.y && w == self->area.width &&
1323         h == self->area.height)
1324         return; /* no change */
1325
1326     w -= self->base_size.width;
1327     h -= self->base_size.height;
1328
1329     if (user) {
1330         /* for interactive resizing. have to move half an increment in each
1331            direction. */
1332
1333         /* how far we are towards the next size inc */
1334         int mw = w % self->size_inc.width; 
1335         int mh = h % self->size_inc.height;
1336         /* amount to add */
1337         int aw = self->size_inc.width / 2;
1338         int ah = self->size_inc.height / 2;
1339         /* don't let us move into a new size increment */
1340         if (mw + aw >= self->size_inc.width)
1341             aw = self->size_inc.width - mw - 1;
1342         if (mh + ah >= self->size_inc.height)
1343             ah = self->size_inc.height - mh - 1;
1344         w += aw;
1345         h += ah;
1346     
1347         /* if this is a user-requested resize, then check against min/max
1348            sizes and aspect ratios */
1349
1350         /* smaller than min size or bigger than max size? */
1351         if (w > self->max_size.width) w = self->max_size.width;
1352         if (w < self->min_size.width) w = self->min_size.width;
1353         if (h > self->max_size.height) h = self->max_size.height;
1354         if (h < self->min_size.height) h = self->min_size.height;
1355
1356         /* adjust the height ot match the width for the aspect ratios */
1357         if (self->min_ratio)
1358             if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1359         if (self->max_ratio)
1360             if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1361     }
1362
1363     /* keep to the increments */
1364     w /= self->size_inc.width;
1365     h /= self->size_inc.height;
1366
1367     /* you cannot resize to nothing */
1368     if (w < 1) w = 1;
1369     if (h < 1) h = 1;
1370   
1371     /* store the logical size */
1372     SIZE_SET(self->logical_size, w, h);
1373
1374     w *= self->size_inc.width;
1375     h *= self->size_inc.height;
1376
1377     w += self->base_size.width;
1378     h += self->base_size.height;
1379
1380     switch (anchor) {
1381     case Corner_TopLeft:
1382         break;
1383     case Corner_TopRight:
1384         x -= w - self->area.width;
1385         break;
1386     case Corner_BottomLeft:
1387         y -= h - self->area.height;
1388         break;
1389     case Corner_BottomRight:
1390         x -= w - self->area.width;
1391         y -= h - self->area.height;
1392         break;
1393     }
1394
1395     moved = x != self->area.x || y != self->area.y;
1396     resized = w != self->area.width || h != self->area.height;
1397
1398     RECT_SET(self->area, x, y, w, h);
1399
1400     if (resized)
1401         XResizeWindow(ob_display, self->window, w, h);
1402
1403     /* move/resize the frame to match the request */
1404     if (self->frame) {
1405         if (moved || resized)
1406             engine_frame_adjust_area(self->frame, moved, resized);
1407
1408         if (moved) {
1409             if (!user || final) {
1410                 XEvent event;
1411                 event.type = ConfigureNotify;
1412                 event.xconfigure.display = ob_display;
1413                 event.xconfigure.event = self->window;
1414                 event.xconfigure.window = self->window;
1415     
1416                 /* root window coords with border in mind */
1417                 event.xconfigure.x = x - self->border_width +
1418                     self->frame->size.left;
1419                 event.xconfigure.y = y - self->border_width +
1420                     self->frame->size.top;
1421     
1422                 event.xconfigure.width = self->area.width;
1423                 event.xconfigure.height = self->area.height;
1424                 event.xconfigure.border_width = self->border_width;
1425                 event.xconfigure.above = self->frame->plate;
1426                 event.xconfigure.override_redirect = FALSE;
1427                 XSendEvent(event.xconfigure.display, event.xconfigure.window,
1428                            FALSE, StructureNotifyMask, &event);
1429             }
1430         }
1431     }
1432 }
1433
1434 void client_fullscreen(Client *self, gboolean fs, gboolean savearea)
1435 {
1436     int x, y, w, h;
1437
1438     if (!(self->functions & Func_Fullscreen) || /* can't */
1439         self->fullscreen == fs) return;         /* already done */
1440
1441     self->fullscreen = fs;
1442     client_change_state(self); /* change the state hints on the client */
1443
1444     if (fs) {
1445         /* save the functions and remove them */
1446         self->pre_fs_func = self->functions;
1447         self->functions &= (Func_Close | Func_Fullscreen |
1448                             Func_Iconify);
1449         /* save the decorations and remove them */
1450         self->pre_fs_decor = self->decorations;
1451         self->decorations = 0;
1452         if (savearea) {
1453             long dimensions[4];
1454             dimensions[0] = self->area.x;
1455             dimensions[1] = self->area.y;
1456             dimensions[2] = self->area.width;
1457             dimensions[3] = self->area.height;
1458   
1459             PROP_SET32A(self->window, openbox_premax, cardinal,
1460                         dimensions, 4);
1461         }
1462
1463         /* these are not actually used cuz client_configure will set them
1464            as appropriate when the window is fullscreened */
1465         x = y = w = h = 0;
1466     } else {
1467         long *dimensions;
1468
1469         self->functions = self->pre_fs_func;
1470         self->decorations = self->pre_fs_decor;
1471           
1472         if (PROP_GET32A(self->window, openbox_premax, cardinal,
1473                         dimensions, 4)) {
1474             x = dimensions[0];
1475             y = dimensions[1];
1476             w = dimensions[2];
1477             h = dimensions[3];
1478             g_free(dimensions);
1479         } else {
1480             /* pick some fallbacks... */
1481             x = screen_area(self->desktop)->x +
1482                 screen_area(self->desktop)->width / 4;
1483             y = screen_area(self->desktop)->y +
1484                 screen_area(self->desktop)->height / 4;
1485             w = screen_area(self->desktop)->width / 2;
1486             h = screen_area(self->desktop)->height / 2;
1487         }
1488     }
1489
1490     client_change_allowed_actions(self); /* based on the new _functions */
1491
1492     /* when fullscreening, don't obey things like increments, fill the
1493        screen */
1494     client_configure(self, Corner_TopLeft, x, y, w, h, !fs, TRUE);
1495
1496     /* raise (back) into our stacking layer */
1497     stacking_raise(self);
1498
1499     /* try focus us when we go into fullscreen mode */
1500     client_focus(self);
1501 }
1502
1503 void client_iconify(Client *self, gboolean iconic, gboolean curdesk)
1504 {
1505     if (self->iconic == iconic) return; /* nothing to do */
1506
1507     g_message("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
1508               self->window);
1509
1510     self->iconic = iconic;
1511
1512     if (iconic) {
1513         self->wmstate = IconicState;
1514         self->ignore_unmaps++;
1515         /* we unmap the client itself so that we can get MapRequest events,
1516            and because the ICCCM tells us to! */
1517         XUnmapWindow(ob_display, self->window);
1518     } else {
1519         if (curdesk)
1520             client_set_desktop(self, screen_desktop);
1521         self->wmstate = self->shaded ? IconicState : NormalState;
1522         XMapWindow(ob_display, self->window);
1523     }
1524     client_change_state(self);
1525     client_showhide(self);
1526     screen_update_struts();
1527
1528     dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
1529                     self, 0, 0);
1530 }
1531
1532 void client_maximize(Client *self, gboolean max, int dir, gboolean savearea)
1533 {
1534     int x, y, w, h;
1535      
1536     g_assert(dir == 0 || dir == 1 || dir == 2);
1537     if (!(self->functions & Func_Maximize)) return; /* can't */
1538
1539     /* check if already done */
1540     if (max) {
1541         if (dir == 0 && self->max_horz && self->max_vert) return;
1542         if (dir == 1 && self->max_horz) return;
1543         if (dir == 2 && self->max_vert) return;
1544     } else {
1545         if (dir == 0 && !self->max_horz && !self->max_vert) return;
1546         if (dir == 1 && !self->max_horz) return;
1547         if (dir == 2 && !self->max_vert) return;
1548     }
1549
1550     /* work with the frame's coords */
1551     x = self->frame->area.x;
1552     y = self->frame->area.y;
1553     w = self->area.width;
1554     h = self->area.height;
1555
1556     if (max) {
1557         if (savearea) {
1558             long dimensions[4];
1559             long *readdim;
1560
1561             dimensions[0] = x;
1562             dimensions[1] = y;
1563             dimensions[2] = w;
1564             dimensions[3] = h;
1565
1566             /* get the property off the window and use it for the dimensions
1567                we are already maxed on */
1568             if (PROP_GET32A(self->window, openbox_premax, cardinal,
1569                             readdim, 4)) {
1570                 if (self->max_horz) {
1571                     dimensions[0] = readdim[0];
1572                     dimensions[2] = readdim[2];
1573                 }
1574                 if (self->max_vert) {
1575                     dimensions[1] = readdim[1];
1576                     dimensions[3] = readdim[3];
1577                 }
1578                 g_free(readdim);
1579             }
1580
1581             PROP_SET32A(self->window, openbox_premax, cardinal,
1582                         dimensions, 4);
1583         }
1584
1585         /* pass the client's current position info. the client_configure
1586            will move/size stuff as appropriate for a maximized window */
1587         x = self->area.x;
1588         y = self->area.y;
1589         w = self->area.width;
1590         h = self->area.height;
1591     } else {
1592         long *dimensions;
1593
1594         if (PROP_GET32A(self->window, openbox_premax, cardinal,
1595                         dimensions, 4)) {
1596             if (dir == 0 || dir == 1) { /* horz */
1597                 x = dimensions[0];
1598                 w = dimensions[2];
1599             }
1600             if (dir == 0 || dir == 2) { /* vert */
1601                 y = dimensions[1];
1602                 h = dimensions[3];
1603             }
1604             g_free(dimensions);
1605         } else {
1606             /* pick some fallbacks... */
1607             if (dir == 0 || dir == 1) { /* horz */
1608                 x = screen_area(self->desktop)->x +
1609                     screen_area(self->desktop)->width / 4;
1610                 w = screen_area(self->desktop)->width / 2;
1611             }
1612             if (dir == 0 || dir == 2) { /* vert */
1613                 y = screen_area(self->desktop)->y +
1614                     screen_area(self->desktop)->height / 4;
1615                 h = screen_area(self->desktop)->height / 2;
1616             }
1617         }
1618     }
1619
1620     if (dir == 0 || dir == 1) /* horz */
1621         self->max_horz = max;
1622     if (dir == 0 || dir == 2) /* vert */
1623         self->max_vert = max;
1624
1625     if (!self->max_horz && !self->max_vert)
1626         PROP_ERASE(self->window, openbox_premax);
1627
1628     client_change_state(self); /* change the state hints on the client */
1629
1630     /* figure out where the client should be going */
1631     frame_frame_gravity(self->frame, &x, &y);
1632     client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1633 }
1634
1635 void client_shade(Client *self, gboolean shade)
1636 {
1637     if (!(self->functions & Func_Shade) || /* can't */
1638         self->shaded == shade) return;     /* already done */
1639
1640     /* when we're iconic, don't change the wmstate */
1641     if (!self->iconic)
1642         self->wmstate = shade ? IconicState : NormalState;
1643     self->shaded = shade;
1644     client_change_state(self);
1645     /* resize the frame to just the titlebar */
1646     engine_frame_adjust_area(self->frame, FALSE, FALSE);
1647 }
1648
1649 void client_close(Client *self)
1650 {
1651     XEvent ce;
1652
1653     if (!(self->functions & Func_Close)) return;
1654
1655     /*
1656       XXX: itd be cool to do timeouts and shit here for killing the client's
1657       process off
1658       like... if the window is around after 5 seconds, then the close button
1659       turns a nice red, and if this function is called again, the client is
1660       explicitly killed.
1661     */
1662
1663     ce.xclient.type = ClientMessage;
1664     ce.xclient.message_type =  prop_atoms.wm_protocols;
1665     ce.xclient.display = ob_display;
1666     ce.xclient.window = self->window;
1667     ce.xclient.format = 32;
1668     ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
1669     ce.xclient.data.l[1] = event_lasttime;
1670     ce.xclient.data.l[2] = 0l;
1671     ce.xclient.data.l[3] = 0l;
1672     ce.xclient.data.l[4] = 0l;
1673     XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
1674 }
1675
1676 void client_kill(Client *self)
1677 {
1678     XKillClient(ob_display, self->window);
1679 }
1680
1681 void client_set_desktop(Client *self, guint target)
1682 {
1683     guint old, i;
1684
1685     if (target == self->desktop) return;
1686   
1687     g_message("Setting desktop %u\n", target);
1688
1689     g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
1690
1691     old = self->desktop;
1692     self->desktop = target;
1693     PROP_SET32(self->window, net_wm_desktop, cardinal, target);
1694     /* the frame can display the current desktop state */
1695     engine_frame_adjust_state(self->frame);
1696     /* 'move' the window to the new desktop */
1697     client_showhide(self);
1698     screen_update_struts();
1699
1700     /* update the focus lists */
1701     if (old == DESKTOP_ALL) {
1702         for (i = 0; i < screen_num_desktops; ++i)
1703             focus_order[i] = g_list_remove(focus_order[i], self);
1704         focus_order[target] = g_list_prepend(focus_order[target], self);
1705     } else {
1706         focus_order[old] = g_list_remove(focus_order[old], self);
1707         if (target == DESKTOP_ALL)
1708             for (i = 0; i < screen_num_desktops; ++i)
1709                 focus_order[i] = g_list_prepend(focus_order[i], self);
1710     }
1711
1712     dispatch_client(Event_Client_Desktop, self, target, old);
1713 }
1714
1715 static Client *search_modal_tree(Client *node, Client *skip)
1716 {
1717     GSList *it;
1718     Client *ret;
1719   
1720     for (it = node->transients; it != NULL; it = it->next) {
1721         Client *c = it->data;
1722         if (c == skip) continue; /* circular? */
1723         if ((ret = search_modal_tree(c, skip))) return ret;
1724         if (c->modal) return c;
1725     }
1726     return NULL;
1727 }
1728
1729 Client *client_find_modal_child(Client *self)
1730 {
1731     return search_modal_tree(self, self);
1732 }
1733
1734 gboolean client_validate(Client *self)
1735 {
1736     XEvent e; 
1737
1738     XSync(ob_display, FALSE); /* get all events on the server */
1739
1740     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
1741         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
1742         XPutBackEvent(ob_display, &e);
1743         return FALSE;
1744     }
1745
1746     return TRUE;
1747 }
1748
1749 void client_set_wm_state(Client *self, long state)
1750 {
1751     if (state == self->wmstate) return; /* no change */
1752   
1753     switch (state) {
1754     case IconicState:
1755         client_iconify(self, TRUE, TRUE);
1756         break;
1757     case NormalState:
1758         client_iconify(self, FALSE, TRUE);
1759         break;
1760     }
1761 }
1762
1763 void client_set_state(Client *self, Atom action, long data1, long data2)
1764 {
1765     gboolean shaded = self->shaded;
1766     gboolean fullscreen = self->fullscreen;
1767     gboolean max_horz = self->max_horz;
1768     gboolean max_vert = self->max_vert;
1769     int i;
1770
1771     if (!(action == prop_atoms.net_wm_state_add ||
1772           action == prop_atoms.net_wm_state_remove ||
1773           action == prop_atoms.net_wm_state_toggle))
1774         /* an invalid action was passed to the client message, ignore it */
1775         return; 
1776
1777     for (i = 0; i < 2; ++i) {
1778         Atom state = i == 0 ? data1 : data2;
1779     
1780         if (!state) continue;
1781
1782         /* if toggling, then pick whether we're adding or removing */
1783         if (action == prop_atoms.net_wm_state_toggle) {
1784             if (state == prop_atoms.net_wm_state_modal)
1785                 action = self->modal ? prop_atoms.net_wm_state_remove :
1786                     prop_atoms.net_wm_state_add;
1787             else if (state == prop_atoms.net_wm_state_maximized_vert)
1788                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
1789                     prop_atoms.net_wm_state_add;
1790             else if (state == prop_atoms.net_wm_state_maximized_horz)
1791                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
1792                     prop_atoms.net_wm_state_add;
1793             else if (state == prop_atoms.net_wm_state_shaded)
1794                 action = self->shaded ? prop_atoms.net_wm_state_remove :
1795                     prop_atoms.net_wm_state_add;
1796             else if (state == prop_atoms.net_wm_state_skip_taskbar)
1797                 action = self->skip_taskbar ?
1798                     prop_atoms.net_wm_state_remove :
1799                     prop_atoms.net_wm_state_add;
1800             else if (state == prop_atoms.net_wm_state_skip_pager)
1801                 action = self->skip_pager ?
1802                     prop_atoms.net_wm_state_remove :
1803                     prop_atoms.net_wm_state_add;
1804             else if (state == prop_atoms.net_wm_state_fullscreen)
1805                 action = self->fullscreen ?
1806                     prop_atoms.net_wm_state_remove :
1807                     prop_atoms.net_wm_state_add;
1808             else if (state == prop_atoms.net_wm_state_above)
1809                 action = self->above ? prop_atoms.net_wm_state_remove :
1810                     prop_atoms.net_wm_state_add;
1811             else if (state == prop_atoms.net_wm_state_below)
1812                 action = self->below ? prop_atoms.net_wm_state_remove :
1813                     prop_atoms.net_wm_state_add;
1814         }
1815     
1816         if (action == prop_atoms.net_wm_state_add) {
1817             if (state == prop_atoms.net_wm_state_modal) {
1818                 /* XXX raise here or something? */
1819                 self->modal = TRUE;
1820             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
1821                 max_vert = TRUE;
1822             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
1823                 max_horz = TRUE;
1824             } else if (state == prop_atoms.net_wm_state_shaded) {
1825                 shaded = TRUE;
1826             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
1827                 self->skip_taskbar = TRUE;
1828             } else if (state == prop_atoms.net_wm_state_skip_pager) {
1829                 self->skip_pager = TRUE;
1830             } else if (state == prop_atoms.net_wm_state_fullscreen) {
1831                 fullscreen = TRUE;
1832             } else if (state == prop_atoms.net_wm_state_above) {
1833                 self->above = TRUE;
1834             } else if (state == prop_atoms.net_wm_state_below) {
1835                 self->below = TRUE;
1836             }
1837
1838         } else { /* action == prop_atoms.net_wm_state_remove */
1839             if (state == prop_atoms.net_wm_state_modal) {
1840                 self->modal = FALSE;
1841             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
1842                 max_vert = FALSE;
1843             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
1844                 max_horz = FALSE;
1845             } else if (state == prop_atoms.net_wm_state_shaded) {
1846                 shaded = FALSE;
1847             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
1848                 self->skip_taskbar = FALSE;
1849             } else if (state == prop_atoms.net_wm_state_skip_pager) {
1850                 self->skip_pager = FALSE;
1851             } else if (state == prop_atoms.net_wm_state_fullscreen) {
1852                 fullscreen = FALSE;
1853             } else if (state == prop_atoms.net_wm_state_above) {
1854                 self->above = FALSE;
1855             } else if (state == prop_atoms.net_wm_state_below) {
1856                 self->below = FALSE;
1857             }
1858         }
1859     }
1860     if (max_horz != self->max_horz || max_vert != self->max_vert) {
1861         if (max_horz != self->max_horz && max_vert != self->max_vert) {
1862             /* toggling both */
1863             if (max_horz == max_vert) { /* both going the same way */
1864                 client_maximize(self, max_horz, 0, TRUE);
1865             } else {
1866                 client_maximize(self, max_horz, 1, TRUE);
1867                 client_maximize(self, max_vert, 2, TRUE);
1868             }
1869         } else {
1870             /* toggling one */
1871             if (max_horz != self->max_horz)
1872                 client_maximize(self, max_horz, 1, TRUE);
1873             else
1874                 client_maximize(self, max_vert, 2, TRUE);
1875         }
1876     }
1877     /* change fullscreen state before shading, as it will affect if the window
1878        can shade or not */
1879     if (fullscreen != self->fullscreen)
1880         client_fullscreen(self, fullscreen, TRUE);
1881     if (shaded != self->shaded)
1882         client_shade(self, shaded);
1883     client_calc_layer(self);
1884     client_change_state(self); /* change the hint to relect these changes */
1885 }
1886
1887 gboolean client_focus(Client *self)
1888 {
1889     XEvent ev;
1890     Client *child;
1891      
1892     /* if we have a modal child, then focus it, not us */
1893     child = client_find_modal_child(self);
1894     if (child)
1895         return client_focus(child);
1896
1897     /* won't try focus if the client doesn't want it, or if the window isn't
1898        visible on the screen */
1899     if (!(self->frame->visible &&
1900           (self->can_focus || self->focus_notify)))
1901         return FALSE;
1902
1903     /* do a check to see if the window has already been unmapped or destroyed
1904        do this intelligently while watching out for unmaps we've generated
1905        (ignore_unmaps > 0) */
1906     if (XCheckTypedWindowEvent(ob_display, self->window,
1907                                DestroyNotify, &ev)) {
1908         XPutBackEvent(ob_display, &ev);
1909         return FALSE;
1910     }
1911     while (XCheckTypedWindowEvent(ob_display, self->window,
1912                                   UnmapNotify, &ev)) {
1913         if (self->ignore_unmaps) {
1914             self->ignore_unmaps--;
1915         } else {
1916             XPutBackEvent(ob_display, &ev);
1917             return FALSE;
1918         }
1919     }
1920
1921     if (self->can_focus)
1922         XSetInputFocus(ob_display, self->window, RevertToNone,
1923                        CurrentTime);
1924
1925     if (self->focus_notify) {
1926         XEvent ce;
1927         ce.xclient.type = ClientMessage;
1928         ce.xclient.message_type = prop_atoms.wm_protocols;
1929         ce.xclient.display = ob_display;
1930         ce.xclient.window = self->window;
1931         ce.xclient.format = 32;
1932         ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
1933         ce.xclient.data.l[1] = CurrentTime;
1934         ce.xclient.data.l[2] = 0l;
1935         ce.xclient.data.l[3] = 0l;
1936         ce.xclient.data.l[4] = 0l;
1937         XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
1938     }
1939
1940     /* XSync(ob_display, FALSE); XXX Why sync? */
1941     return TRUE;
1942 }
1943
1944 void client_unfocus(Client *self)
1945 {
1946     g_assert(focus_client == self);
1947     client_set_focused(self, FALSE);
1948 }
1949
1950 gboolean client_focused(Client *self)
1951 {
1952     return self == focus_client;
1953 }
1954
1955 void client_set_focused(Client *self, gboolean focused)
1956 {
1957     if (focused) {
1958         if (focus_client != self)
1959             focus_set_client(self);
1960     } else {
1961         if (focus_client == self)
1962             focus_set_client(NULL);
1963     }
1964
1965     /* focus state can affect the stacking layer */
1966     client_calc_layer(self);
1967
1968     engine_frame_adjust_focus(self->frame);
1969 }