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