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