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