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