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