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