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