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