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