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