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