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