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