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