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