]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/client.c
prefixing and capitalizing the StackLayer -> ObStackingLayer enum.
[mikachu/openbox.git] / openbox / client.c
1 #include "client.h"
2 #include "dock.h"
3 #include "xerror.h"
4 #include "startup.h"
5 #include "screen.h"
6 #include "moveresize.h"
7 #include "prop.h"
8 #include "extensions.h"
9 #include "frame.h"
10 #include "event.h"
11 #include "grab.h"
12 #include "focus.h"
13 #include "stacking.h"
14 #include "dispatch.h"
15 #include "openbox.h"
16 #include "group.h"
17 #include "config.h"
18 #include "menu.h"
19 #include "render/render.h"
20
21 #include <glib.h>
22 #include <X11/Xutil.h>
23
24 /*! The event mask to grab on client windows */
25 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
26                           StructureNotifyMask)
27
28 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
29                                 ButtonMotionMask)
30
31 GList      *client_list      = NULL;
32
33 static void client_get_all(ObClient *self);
34 static void client_toggle_border(ObClient *self, gboolean show);
35 static void client_get_area(ObClient *self);
36 static void client_get_desktop(ObClient *self);
37 static void client_get_state(ObClient *self);
38 static void client_get_shaped(ObClient *self);
39 static void client_get_mwm_hints(ObClient *self);
40 static void client_get_gravity(ObClient *self);
41 static void client_showhide(ObClient *self);
42 static void client_change_allowed_actions(ObClient *self);
43 static void client_change_state(ObClient *self);
44 static void client_apply_startup_state(ObClient *self);
45
46 void client_startup()
47 {
48     client_set_list();
49 }
50
51 void client_shutdown()
52 {
53 }
54
55 void client_set_list()
56 {
57     Window *windows, *win_it;
58     GList *it;
59     guint size = g_list_length(client_list);
60
61     /* create an array of the window ids */
62     if (size > 0) {
63         windows = g_new(Window, size);
64         win_it = windows;
65         for (it = client_list; it != NULL; it = it->next, ++win_it)
66             *win_it = ((ObClient*)it->data)->window;
67     } else
68         windows = NULL;
69
70     PROP_SETA32(ob_root, net_client_list, window, (guint32*)windows, size);
71
72     if (windows)
73         g_free(windows);
74
75     stacking_set_list();
76 }
77
78 /*
79 void client_foreach_transient(ObClient *self, ObClientForeachFunc func, void *data)
80 {
81     GSList *it;
82
83     for (it = self->transients; it; it = it->next) {
84         if (!func(it->data, data)) return;
85         client_foreach_transient(it->data, func, data);
86     }
87 }
88
89 void client_foreach_ancestor(ObClient *self, ObClientForeachFunc func, void *data)
90 {
91     if (self->transient_for) {
92         if (self->transient_for != OB_TRAN_GROUP) {
93             if (!func(self->transient_for, data)) return;
94             client_foreach_ancestor(self->transient_for, func, data);
95         } else {
96             GSList *it;
97
98             for (it = self->group->members; it; it = it->next)
99                 if (it->data != self &&
100                     !((ObClient*)it->data)->transient_for) {
101                     if (!func(it->data, data)) return;
102                     client_foreach_ancestor(it->data, func, data);
103                 }
104         }
105     }
106 }
107 */
108
109 void client_manage_all()
110 {
111     unsigned int i, j, nchild;
112     Window w, *children;
113     XWMHints *wmhints;
114     XWindowAttributes attrib;
115
116     XQueryTree(ob_display, ob_root, &w, &w, &children, &nchild);
117
118     /* remove all icon windows from the list */
119     for (i = 0; i < nchild; i++) {
120         if (children[i] == None) continue;
121         wmhints = XGetWMHints(ob_display, children[i]);
122         if (wmhints) {
123             if ((wmhints->flags & IconWindowHint) &&
124                 (wmhints->icon_window != children[i]))
125                 for (j = 0; j < nchild; j++)
126                     if (children[j] == wmhints->icon_window) {
127                         children[j] = None;
128                         break;
129                     }
130             XFree(wmhints);
131         }
132     }
133
134     for (i = 0; i < nchild; ++i) {
135         if (children[i] == None)
136             continue;
137         if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
138             if (attrib.override_redirect) continue;
139
140             if (attrib.map_state != IsUnmapped)
141                 client_manage(children[i]);
142         }
143     }
144     XFree(children);
145
146     /* stack them as they were on startup!
147        why with stacking_lower? Why, because then windows who aren't in the
148        stacking list are on the top where you can see them instead of buried
149        at the bottom! */
150     for (i = startup_stack_size; i > 0; --i) {
151         ObWindow *obw;
152
153         w = startup_stack_order[i-1];
154         obw = g_hash_table_lookup(window_map, &w);
155         if (obw) {
156             g_assert(WINDOW_IS_CLIENT(obw));
157             stacking_lower(CLIENT_AS_WINDOW(obw));
158         }
159     }
160     g_free(startup_stack_order);
161     startup_stack_order = NULL;
162     startup_stack_size = 0;
163
164     if (config_focus_new) {
165         ObWindow *active;
166
167         active = g_hash_table_lookup(window_map, &startup_active);
168         if (active) {
169             g_assert(WINDOW_IS_CLIENT(active));
170             if (!client_focus(WINDOW_AS_CLIENT(active)))
171                 focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS);
172         } else
173             focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS);
174     }
175 }
176
177 void client_manage(Window window)
178 {
179     ObClient *self;
180     XEvent e;
181     XWindowAttributes attrib;
182     XSetWindowAttributes attrib_set;
183     XWMHints *wmhint;
184     gboolean activate = FALSE;
185
186     grab_server(TRUE);
187
188     /* check if it has already been unmapped by the time we started mapping
189        the grab does a sync so we don't have to here */
190     if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
191         XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
192         XPutBackEvent(ob_display, &e);
193
194         grab_server(FALSE);
195         return; /* don't manage it */
196     }
197
198     /* make sure it isn't an override-redirect window */
199     if (!XGetWindowAttributes(ob_display, window, &attrib) ||
200         attrib.override_redirect) {
201         grab_server(FALSE);
202         return; /* don't manage it */
203     }
204   
205     /* is the window a docking app */
206     if ((wmhint = XGetWMHints(ob_display, window))) {
207         if ((wmhint->flags & StateHint) &&
208             wmhint->initial_state == WithdrawnState) {
209             dock_add(window, wmhint);
210             grab_server(FALSE);
211             XFree(wmhint);
212             return;
213         }
214         XFree(wmhint);
215     }
216
217     g_message("Managing window: %lx", window);
218
219     /* choose the events we want to receive on the CLIENT window */
220     attrib_set.event_mask = CLIENT_EVENTMASK;
221     attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
222     XChangeWindowAttributes(ob_display, window,
223                             CWEventMask|CWDontPropagate, &attrib_set);
224
225
226     /* create the ObClient struct, and populate it from the hints on the
227        window */
228     self = g_new(ObClient, 1);
229     self->obwin.type = Window_Client;
230     self->window = window;
231     client_get_all(self);
232
233     /* remove the client's border (and adjust re gravity) */
234     client_toggle_border(self, FALSE);
235      
236     /* specify that if we exit, the window should not be destroyed and should
237        be reparented back to root automatically */
238     XChangeSaveSet(ob_display, window, SetModeInsert);
239
240     /* create the decoration frame for the client window */
241     self->frame = frame_new();
242
243     frame_grab_client(self->frame, self);
244
245     client_apply_startup_state(self);
246
247     grab_server(FALSE);
248
249     /* add to client list/map */
250     client_list = g_list_append(client_list, self);
251     g_hash_table_insert(window_map, &self->window, self);
252
253     /* update the focus lists */
254     focus_order_add_new(self);
255
256     /* focus the new window? */
257     if (ob_state != OB_STATE_STARTING && config_focus_new &&
258         (self->type == OB_CLIENT_TYPE_NORMAL || self->type == OB_CLIENT_TYPE_DIALOG)) {
259         gboolean group_foc = FALSE;
260         
261         if (self->group) {
262             GSList *it;
263
264             for (it = self->group->members; it; it = it->next) {
265                 if (client_focused(it->data)) {
266                     group_foc = TRUE;
267                     break;
268                 }
269             }
270         }
271         /* note the check against Type_Normal/Dialog, not client_normal(self),
272            which would also include other types. in this case we want more
273            strict rules for focus */
274         if ((group_foc ||
275              (!self->transient_for && (!self->group ||
276                                        !self->group->members->next))) ||
277             client_search_focus_tree_full(self) ||
278             !focus_client ||
279             !client_normal(focus_client)) {
280             /* activate the window */
281             stacking_add(CLIENT_AS_WINDOW(self));
282             activate = TRUE;
283         } else {
284             /* try to not get in the way */
285             stacking_add_nonintrusive(CLIENT_AS_WINDOW(self));
286         }
287     } else {
288         stacking_add(CLIENT_AS_WINDOW(self));
289     }
290
291     dispatch_client(Event_Client_New, self, 0, 0);
292
293     /* make sure the window is visible */
294     client_move_onscreen(self);
295
296     screen_update_areas();
297
298     client_showhide(self);
299
300     if (activate) client_activate(self);
301
302     /* update the list hints */
303     client_set_list();
304
305     dispatch_client(Event_Client_Mapped, self, 0, 0);
306
307     g_message("Managed window 0x%lx (%s)", window, self->class);
308 }
309
310 void client_unmanage_all()
311 {
312     while (client_list != NULL)
313         client_unmanage(client_list->data);
314 }
315
316 /* called by client_unmanage() to close any menus referencing this client */
317 void client_close_menus(gpointer key, gpointer value, gpointer self)
318 {
319     if (((Menu *)value)->client == (ObClient *)self)
320         menu_hide((Menu *)value);
321 }
322
323 void client_unmanage(ObClient *self)
324 {
325     guint j;
326     GSList *it;
327
328     g_message("Unmanaging window: %lx (%s)", self->window, self->class);
329
330     dispatch_client(Event_Client_Destroy, self, 0, 0);
331     g_assert(self != NULL);
332
333     /* remove the window from our save set */
334     XChangeSaveSet(ob_display, self->window, SetModeDelete);
335
336     /* we dont want events no more */
337     XSelectInput(ob_display, self->window, NoEventMask);
338
339     frame_hide(self->frame);
340
341     client_list = g_list_remove(client_list, self);
342     stacking_remove(self);
343     g_hash_table_remove(window_map, &self->window);
344
345     /* update the focus lists */
346     focus_order_remove(self);
347
348     /* once the client is out of the list, update the struts to remove it's
349        influence */
350     screen_update_areas();
351
352     /* tell our parent(s) that we're gone */
353     if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
354         GSList *it;
355
356         for (it = self->group->members; it; it = it->next)
357             if (it->data != self)
358                 ((ObClient*)it->data)->transients =
359                     g_slist_remove(((ObClient*)it->data)->transients, self);
360     } else if (self->transient_for) {        /* transient of window */
361         self->transient_for->transients =
362             g_slist_remove(self->transient_for->transients, self);
363     }
364
365     /* tell our transients that we're gone */
366     for (it = self->transients; it != NULL; it = it->next) {
367         if (((ObClient*)it->data)->transient_for != OB_TRAN_GROUP) {
368             ((ObClient*)it->data)->transient_for = NULL;
369             client_calc_layer(it->data);
370         }
371     }
372
373     if (moveresize_client == self)
374         moveresize_end(TRUE);
375
376     /* close any windows that are attached to this window */
377     g_hash_table_foreach(menu_hash, client_close_menus, self);
378
379     
380     if (focus_client == self) {
381         XEvent e;
382
383         /* focus the last focused window on the desktop, and ignore enter
384            events from the unmap so it doesnt mess with the focus */
385         while (XCheckTypedEvent(ob_display, EnterNotify, &e));
386         client_unfocus(self);
387     }
388
389     /* remove from its group */
390     if (self->group) {
391         group_remove(self->group, self);
392         self->group = NULL;
393     }
394
395     /* dispatch the unmapped event */
396     dispatch_client(Event_Client_Unmapped, self, 0, 0);
397     g_assert(self != NULL);
398
399     /* give the client its border back */
400     client_toggle_border(self, TRUE);
401
402     /* reparent the window out of the frame, and free the frame */
403     frame_release_client(self->frame, self);
404     self->frame = NULL;
405      
406     if (ob_state != OB_STATE_EXITING) {
407         /* these values should not be persisted across a window
408            unmapping/mapping */
409         prop_erase(self->window, prop_atoms.net_wm_desktop);
410         prop_erase(self->window, prop_atoms.net_wm_state);
411     } else {
412         /* if we're left in an iconic state, the client wont be mapped. this is
413            bad, since we will no longer be managing the window on restart */
414         if (self->iconic)
415             XMapWindow(ob_display, self->window);
416     }
417
418
419     g_message("Unmanaged window 0x%lx", self->window);
420
421     /* free all data allocated in the client struct */
422     g_slist_free(self->transients);
423     for (j = 0; j < self->nicons; ++j)
424         g_free(self->icons[j].data);
425     if (self->nicons > 0)
426         g_free(self->icons);
427     g_free(self->title);
428     g_free(self->icon_title);
429     g_free(self->name);
430     g_free(self->class);
431     g_free(self->role);
432     g_free(self);
433      
434     /* update the list hints */
435     client_set_list();
436 }
437
438 void client_move_onscreen(ObClient *self)
439 {
440     Rect *a;
441     int x = self->frame->area.x, y = self->frame->area.y;
442
443     /* XXX watch for xinerama dead areas */
444     a = screen_area(self->desktop);
445     if (x >= a->x + a->width - 1)
446         x = a->x + a->width - self->frame->area.width;
447     if (y >= a->y + a->height - 1)
448         y = a->y + a->height - self->frame->area.height;
449     if (x + self->frame->area.width - 1 < a->x)
450         x = a->x;
451     if (y + self->frame->area.height - 1< a->y)
452         y = a->y;
453
454     frame_frame_gravity(self->frame, &x, &y); /* get where the client
455                                                  should be */
456     client_configure(self, OB_CORNER_TOPLEFT, x, y,
457                      self->area.width, self->area.height,
458                      TRUE, TRUE);
459 }
460
461 static void client_toggle_border(ObClient *self, gboolean show)
462 {
463     /* adjust our idea of where the client is, based on its border. When the
464        border is removed, the client should now be considered to be in a
465        different position.
466        when re-adding the border to the client, the same operation needs to be
467        reversed. */
468     int oldx = self->area.x, oldy = self->area.y;
469     int x = oldx, y = oldy;
470     switch(self->gravity) {
471     default:
472     case NorthWestGravity:
473     case WestGravity:
474     case SouthWestGravity:
475         break;
476     case NorthEastGravity:
477     case EastGravity:
478     case SouthEastGravity:
479         if (show) x -= self->border_width * 2;
480         else      x += self->border_width * 2;
481         break;
482     case NorthGravity:
483     case SouthGravity:
484     case CenterGravity:
485     case ForgetGravity:
486     case StaticGravity:
487         if (show) x -= self->border_width;
488         else      x += self->border_width;
489         break;
490     }
491     switch(self->gravity) {
492     default:
493     case NorthWestGravity:
494     case NorthGravity:
495     case NorthEastGravity:
496         break;
497     case SouthWestGravity:
498     case SouthGravity:
499     case SouthEastGravity:
500         if (show) y -= self->border_width * 2;
501         else      y += self->border_width * 2;
502         break;
503     case WestGravity:
504     case EastGravity:
505     case CenterGravity:
506     case ForgetGravity:
507     case StaticGravity:
508         if (show) y -= self->border_width;
509         else      y += self->border_width;
510         break;
511     }
512     self->area.x = x;
513     self->area.y = y;
514
515     if (show) {
516         XSetWindowBorderWidth(ob_display, self->window, self->border_width);
517
518         /* move the client so it is back it the right spot _with_ its
519            border! */
520         if (x != oldx || y != oldy)
521             XMoveWindow(ob_display, self->window, x, y);
522     } else
523         XSetWindowBorderWidth(ob_display, self->window, 0);
524 }
525
526
527 static void client_get_all(ObClient *self)
528 {
529     /* update EVERYTHING!! */
530
531     self->ignore_unmaps = 0;
532   
533     /* defaults */
534     self->frame = NULL;
535     self->title = self->icon_title = NULL;
536     self->title_count = 1;
537     self->name = self->class = self->role = NULL;
538     self->wmstate = NormalState;
539     self->transient = FALSE;
540     self->transients = NULL;
541     self->transient_for = NULL;
542     self->layer = -1;
543     self->urgent = FALSE;
544     self->positioned = FALSE;
545     self->disabled_decorations = 0;
546     self->group = NULL;
547     self->nicons = 0;
548
549     client_get_area(self);
550     client_update_transient_for(self);
551     client_update_wmhints(self);
552     client_get_desktop(self);
553     client_get_state(self);
554     client_get_shaped(self);
555
556     client_get_mwm_hints(self);
557     client_get_type(self);/* this can change the mwmhints for special cases */
558
559     client_update_protocols(self);
560
561     client_get_gravity(self); /* get the attribute gravity */
562     client_update_normal_hints(self); /* this may override the attribute
563                                          gravity */
564
565     /* got the type, the mwmhints, the protocols, and the normal hints
566        (min/max sizes), so we're ready to set up the decorations/functions */
567     client_setup_decor_and_functions(self);
568   
569     client_update_title(self);
570     client_update_class(self);
571     client_update_strut(self);
572     client_update_icons(self);
573
574     client_change_state(self);
575 }
576
577 static void client_get_area(ObClient *self)
578 {
579     XWindowAttributes wattrib;
580     Status ret;
581   
582     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
583     g_assert(ret != BadWindow);
584
585     RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
586     self->border_width = wattrib.border_width;
587 }
588
589 static void client_get_desktop(ObClient *self)
590 {
591     guint32 d = screen_num_desktops; /* an always-invalid value */
592
593     if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
594         if (d >= screen_num_desktops && d != DESKTOP_ALL)
595             self->desktop = screen_num_desktops - 1;
596         else
597             self->desktop = d;
598     } else {
599         gboolean trdesk = FALSE;
600
601        if (self->transient_for) {
602            if (self->transient_for != OB_TRAN_GROUP) {
603                 self->desktop = self->transient_for->desktop;
604                 trdesk = TRUE;
605             } else {
606                 GSList *it;
607
608                 for (it = self->group->members; it; it = it->next)
609                     if (it->data != self &&
610                         !((ObClient*)it->data)->transient_for) {
611                         self->desktop = ((ObClient*)it->data)->desktop;
612                         trdesk = TRUE;
613                         break;
614                     }
615             }
616        }
617        if (!trdesk)
618            /* defaults to the current desktop */
619            self->desktop = screen_desktop;
620
621     }
622     if (self->desktop != d) {
623         /* set the desktop hint, to make sure that it always exists */
624         PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
625     }
626 }
627
628 static void client_get_state(ObClient *self)
629 {
630     guint32 *state;
631     guint num;
632   
633     self->modal = self->shaded = self->max_horz = self->max_vert =
634         self->fullscreen = self->above = self->below = self->iconic =
635         self->skip_taskbar = self->skip_pager = FALSE;
636
637     if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
638         gulong i;
639         for (i = 0; i < num; ++i) {
640             if (state[i] == prop_atoms.net_wm_state_modal)
641                 self->modal = TRUE;
642             else if (state[i] == prop_atoms.net_wm_state_shaded)
643                 self->shaded = TRUE;
644             else if (state[i] == prop_atoms.net_wm_state_hidden)
645                 self->iconic = TRUE;
646             else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
647                 self->skip_taskbar = TRUE;
648             else if (state[i] == prop_atoms.net_wm_state_skip_pager)
649                 self->skip_pager = TRUE;
650             else if (state[i] == prop_atoms.net_wm_state_fullscreen)
651                 self->fullscreen = TRUE;
652             else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
653                 self->max_vert = TRUE;
654             else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
655                 self->max_horz = TRUE;
656             else if (state[i] == prop_atoms.net_wm_state_above)
657                 self->above = TRUE;
658             else if (state[i] == prop_atoms.net_wm_state_below)
659                 self->below = TRUE;
660         }
661
662         g_free(state);
663     }
664 }
665
666 static void client_get_shaped(ObClient *self)
667 {
668     self->shaped = FALSE;
669 #ifdef   SHAPE
670     if (extensions_shape) {
671         int foo;
672         guint ufoo;
673         int s;
674
675         XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
676
677         XShapeQueryExtents(ob_display, self->window, &s, &foo,
678                            &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
679                            &ufoo);
680         self->shaped = (s != 0);
681     }
682 #endif
683 }
684
685 void client_update_transient_for(ObClient *self)
686 {
687     Window t = None;
688     ObClient *c = NULL;
689
690     if (XGetTransientForHint(ob_display, self->window, &t)) {
691         self->transient = TRUE;
692         if (t != self->window) { /* cant be transient to itself! */
693             c = g_hash_table_lookup(window_map, &t);
694             /* if this happens then we need to check for it*/
695             g_assert(c != self);
696             g_assert(!c || WINDOW_IS_CLIENT(c));
697             
698             if (!c && self->group) {
699                 /* not transient to a client, see if it is transient for a
700                    group */
701                 if (t == self->group->leader ||
702                     t == None ||
703                     t == ob_root) {
704                     /* window is a transient for its group! */
705                     c = OB_TRAN_GROUP;
706                 }
707             }
708         }
709     } else
710         self->transient = FALSE;
711
712     /* if anything has changed... */
713     if (c != self->transient_for) {
714         if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
715             GSList *it;
716
717             /* remove from old parents */
718             for (it = self->group->members; it; it = it->next)
719                 if (it->data != self &&
720                     !((ObClient*)it->data)->transient_for)
721                     ((ObClient*)it->data)->transients =
722                         g_slist_remove(((ObClient*)it->data)->transients, self);
723         } else if (self->transient_for != NULL) { /* transient of window */
724             /* remove from old parent */
725             self->transient_for->transients =
726                 g_slist_remove(self->transient_for->transients, self);
727         }
728         self->transient_for = c;
729         if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
730             GSList *it;
731
732             /* add to new parents */
733             for (it = self->group->members; it; it = it->next)
734                 if (it->data != self &&
735                     !((ObClient*)it->data)->transient_for)
736                     ((ObClient*)it->data)->transients =
737                         g_slist_append(((ObClient*)it->data)->transients, self);
738
739             /* remove all transients which are in the group, that causes
740                circlular pointer hell of doom */
741             for (it = self->group->members; it; it = it->next) {
742                 GSList *sit, *next;
743                 for (sit = self->transients; sit; sit = next) {
744                     next = sit->next;
745                     if (sit->data == it->data)
746                         self->transients = g_slist_remove(self->transients,
747                                                           sit->data);
748                 }
749             }
750         } else if (self->transient_for != NULL) { /* transient of window */
751             /* add to new parent */
752             self->transient_for->transients =
753                 g_slist_append(self->transient_for->transients, self);
754         }
755     }
756 }
757
758 static void client_get_mwm_hints(ObClient *self)
759 {
760     guint num;
761     guint32 *hints;
762
763     self->mwmhints.flags = 0; /* default to none */
764
765     if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
766                     &hints, &num)) {
767         if (num >= OB_MWM_ELEMENTS) {
768             self->mwmhints.flags = hints[0];
769             self->mwmhints.functions = hints[1];
770             self->mwmhints.decorations = hints[2];
771         }
772         g_free(hints);
773     }
774 }
775
776 void client_get_type(ObClient *self)
777 {
778     guint num, i;
779     guint32 *val;
780
781     self->type = -1;
782   
783     if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
784         /* use the first value that we know about in the array */
785         for (i = 0; i < num; ++i) {
786             if (val[i] == prop_atoms.net_wm_window_type_desktop)
787                 self->type = OB_CLIENT_TYPE_DESKTOP;
788             else if (val[i] == prop_atoms.net_wm_window_type_dock)
789                 self->type = OB_CLIENT_TYPE_DOCK;
790             else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
791                 self->type = OB_CLIENT_TYPE_TOOLBAR;
792             else if (val[i] == prop_atoms.net_wm_window_type_menu)
793                 self->type = OB_CLIENT_TYPE_MENU;
794             else if (val[i] == prop_atoms.net_wm_window_type_utility)
795                 self->type = OB_CLIENT_TYPE_UTILITY;
796             else if (val[i] == prop_atoms.net_wm_window_type_splash)
797                 self->type = OB_CLIENT_TYPE_SPLASH;
798             else if (val[i] == prop_atoms.net_wm_window_type_dialog)
799                 self->type = OB_CLIENT_TYPE_DIALOG;
800             else if (val[i] == prop_atoms.net_wm_window_type_normal)
801                 self->type = OB_CLIENT_TYPE_NORMAL;
802             else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
803                 /* prevent this window from getting any decor or
804                    functionality */
805                 self->mwmhints.flags &= (OB_MWM_FLAG_FUNCTIONS |
806                                          OB_MWM_FLAG_DECORATIONS);
807                 self->mwmhints.decorations = 0;
808                 self->mwmhints.functions = 0;
809             }
810             if (self->type != (ObClientType) -1)
811                 break; /* grab the first legit type */
812         }
813         g_free(val);
814     }
815     
816     if (self->type == (ObClientType) -1) {
817         /*the window type hint was not set, which means we either classify
818           ourself as a normal window or a dialog, depending on if we are a
819           transient. */
820         if (self->transient)
821             self->type = OB_CLIENT_TYPE_DIALOG;
822         else
823             self->type = OB_CLIENT_TYPE_NORMAL;
824     }
825 }
826
827 void client_update_protocols(ObClient *self)
828 {
829     guint32 *proto;
830     guint num_return, i;
831
832     self->focus_notify = FALSE;
833     self->delete_window = FALSE;
834
835     if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
836         for (i = 0; i < num_return; ++i) {
837             if (proto[i] == prop_atoms.wm_delete_window) {
838                 /* this means we can request the window to close */
839                 self->delete_window = TRUE;
840             } else if (proto[i] == prop_atoms.wm_take_focus)
841                 /* if this protocol is requested, then the window will be
842                    notified whenever we want it to receive focus */
843                 self->focus_notify = TRUE;
844         }
845         g_free(proto);
846     }
847 }
848
849 static void client_get_gravity(ObClient *self)
850 {
851     XWindowAttributes wattrib;
852     Status ret;
853
854     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
855     g_assert(ret != BadWindow);
856     self->gravity = wattrib.win_gravity;
857 }
858
859 void client_update_normal_hints(ObClient *self)
860 {
861     XSizeHints size;
862     long ret;
863     int oldgravity = self->gravity;
864
865     /* defaults */
866     self->min_ratio = 0.0f;
867     self->max_ratio = 0.0f;
868     SIZE_SET(self->size_inc, 1, 1);
869     SIZE_SET(self->base_size, 0, 0);
870     SIZE_SET(self->min_size, 0, 0);
871     SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
872
873     /* get the hints from the window */
874     if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
875         self->positioned = !!(size.flags & (PPosition|USPosition));
876
877         if (size.flags & PWinGravity) {
878             self->gravity = size.win_gravity;
879       
880             /* if the client has a frame, i.e. has already been mapped and
881                is changing its gravity */
882             if (self->frame && self->gravity != oldgravity) {
883                 /* move our idea of the client's position based on its new
884                    gravity */
885                 self->area.x = self->frame->area.x;
886                 self->area.y = self->frame->area.y;
887                 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
888             }
889         }
890
891         if (size.flags & PAspect) {
892             if (size.min_aspect.y)
893                 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
894             if (size.max_aspect.y)
895                 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
896         }
897
898         if (size.flags & PMinSize)
899             SIZE_SET(self->min_size, size.min_width, size.min_height);
900     
901         if (size.flags & PMaxSize)
902             SIZE_SET(self->max_size, size.max_width, size.max_height);
903     
904         if (size.flags & PBaseSize)
905             SIZE_SET(self->base_size, size.base_width, size.base_height);
906     
907         if (size.flags & PResizeInc)
908             SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
909     }
910 }
911
912 void client_setup_decor_and_functions(ObClient *self)
913 {
914     /* start with everything (cept fullscreen) */
915     self->decorations = Decor_Titlebar | Decor_Handle | Decor_Border |
916         Decor_Icon | Decor_AllDesktops | Decor_Iconify | Decor_Maximize |
917         Decor_Shade;
918     self->functions = OB_CLIENT_FUNC_RESIZE | OB_CLIENT_FUNC_MOVE |
919         OB_CLIENT_FUNC_ICONIFY | OB_CLIENT_FUNC_MAXIMIZE |
920         OB_CLIENT_FUNC_SHADE;
921     if (self->delete_window) {
922         self->decorations |= Decor_Close;
923         self->functions |= OB_CLIENT_FUNC_CLOSE;
924     }
925
926     if (!(self->min_size.width < self->max_size.width ||
927           self->min_size.height < self->max_size.height)) {
928         self->decorations &= ~(Decor_Maximize | Decor_Handle);
929         self->functions &= ~OB_CLIENT_FUNC_RESIZE;
930     }
931
932     switch (self->type) {
933     case OB_CLIENT_TYPE_NORMAL:
934         /* normal windows retain all of the possible decorations and
935            functionality, and are the only windows that you can fullscreen */
936         self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
937         break;
938
939     case OB_CLIENT_TYPE_DIALOG:
940     case OB_CLIENT_TYPE_UTILITY:
941         /* these windows cannot be maximized */
942         self->decorations &= ~Decor_Maximize;
943         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
944         break;
945
946     case OB_CLIENT_TYPE_MENU:
947     case OB_CLIENT_TYPE_TOOLBAR:
948         /* these windows get less functionality */
949         self->decorations &= ~(Decor_Iconify | Decor_Handle);
950         self->functions &= ~(OB_CLIENT_FUNC_ICONIFY | OB_CLIENT_FUNC_RESIZE);
951         break;
952
953     case OB_CLIENT_TYPE_DESKTOP:
954     case OB_CLIENT_TYPE_DOCK:
955     case OB_CLIENT_TYPE_SPLASH:
956         /* none of these windows are manipulated by the window manager */
957         self->decorations = 0;
958         self->functions = 0;
959         break;
960     }
961
962     /* Mwm Hints are applied subtractively to what has already been chosen for
963        decor and functionality */
964     if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
965         if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
966             if (! (self->mwmhints.decorations & OB_MWM_DECOR_BORDER))
967                 self->decorations &= ~Decor_Border;
968             if (! (self->mwmhints.decorations & OB_MWM_DECOR_HANDLE))
969                 self->decorations &= ~Decor_Handle;
970             if (! (self->mwmhints.decorations & OB_MWM_DECOR_TITLE))
971                 self->decorations &= ~Decor_Titlebar;
972             if (! (self->mwmhints.decorations & OB_MWM_DECOR_ICONIFY))
973                 self->decorations &= ~Decor_Iconify;
974             if (! (self->mwmhints.decorations & OB_MWM_DECOR_MAXIMIZE))
975                 self->decorations &= ~Decor_Maximize;
976         }
977     }
978
979     if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
980         if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
981             if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
982                 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
983             if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
984                 self->functions &= ~OB_CLIENT_FUNC_MOVE;
985             if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
986                 self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
987             if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
988                 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
989             /* dont let mwm hints kill the close button
990                if (! (self->mwmhints.functions & MwmFunc_Close))
991                self->functions &= ~Func_Close; */
992         }
993     }
994
995     /* can't maximize without moving/resizing */
996     if (!((self->functions & OB_CLIENT_FUNC_MOVE) &&
997           (self->functions & OB_CLIENT_FUNC_RESIZE)))
998         self->functions &= ~(OB_CLIENT_FUNC_MAXIMIZE |
999                              OB_CLIENT_FUNC_FULLSCREEN);
1000
1001     /* finally, user specified disabled decorations are applied to subtract
1002        decorations */
1003     if (self->disabled_decorations & Decor_Titlebar)
1004         self->decorations &= ~Decor_Titlebar;
1005     if (self->disabled_decorations & Decor_Handle)
1006         self->decorations &= ~Decor_Handle;
1007     if (self->disabled_decorations & Decor_Border)
1008         self->decorations &= ~Decor_Border;
1009     if (self->disabled_decorations & Decor_Iconify)
1010         self->decorations &= ~Decor_Iconify;
1011     if (self->disabled_decorations & Decor_Maximize)
1012         self->decorations &= ~Decor_Maximize;
1013     if (self->disabled_decorations & Decor_AllDesktops)
1014         self->decorations &= ~Decor_AllDesktops;
1015     if (self->disabled_decorations & Decor_Shade)
1016         self->decorations &= ~Decor_Shade;
1017     if (self->disabled_decorations & Decor_Close)
1018         self->decorations &= ~Decor_Close;
1019
1020     /* if we don't have a titlebar, then we cannot shade! */
1021     if (!(self->decorations & Decor_Titlebar))
1022         self->functions &= ~OB_CLIENT_FUNC_SHADE;
1023
1024     /* now we need to check against rules for the client's current state */
1025     if (self->fullscreen) {
1026         self->functions &= (OB_CLIENT_FUNC_CLOSE |
1027                             OB_CLIENT_FUNC_FULLSCREEN |
1028                             OB_CLIENT_FUNC_ICONIFY);
1029         self->decorations = 0;
1030     }
1031
1032     client_change_allowed_actions(self);
1033
1034     if (self->frame) {
1035         /* this makes sure that these windows appear on all desktops */
1036         if (self->type == OB_CLIENT_TYPE_DESKTOP &&
1037             self->desktop != DESKTOP_ALL)
1038             client_set_desktop(self, DESKTOP_ALL, FALSE);
1039
1040         /* adjust the client's decorations, etc. */
1041         client_reconfigure(self);
1042     } else {
1043         /* this makes sure that these windows appear on all desktops */
1044         if (self->type == OB_CLIENT_TYPE_DESKTOP &&
1045             self->desktop != DESKTOP_ALL)
1046         {
1047             self->desktop = DESKTOP_ALL;
1048         }
1049     }
1050 }
1051
1052 static void client_change_allowed_actions(ObClient *self)
1053 {
1054     guint32 actions[9];
1055     int num = 0;
1056
1057     /* desktop windows are kept on all desktops */
1058     if (self->type != OB_CLIENT_TYPE_DESKTOP)
1059         actions[num++] = prop_atoms.net_wm_action_change_desktop;
1060
1061     if (self->functions & OB_CLIENT_FUNC_SHADE)
1062         actions[num++] = prop_atoms.net_wm_action_shade;
1063     if (self->functions & OB_CLIENT_FUNC_CLOSE)
1064         actions[num++] = prop_atoms.net_wm_action_close;
1065     if (self->functions & OB_CLIENT_FUNC_MOVE)
1066         actions[num++] = prop_atoms.net_wm_action_move;
1067     if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1068         actions[num++] = prop_atoms.net_wm_action_minimize;
1069     if (self->functions & OB_CLIENT_FUNC_RESIZE)
1070         actions[num++] = prop_atoms.net_wm_action_resize;
1071     if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1072         actions[num++] = prop_atoms.net_wm_action_fullscreen;
1073     if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1074         actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1075         actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1076     }
1077
1078     PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1079
1080     /* make sure the window isn't breaking any rules now */
1081
1082     if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1083         if (self->frame) client_shade(self, FALSE);
1084         else self->shaded = FALSE;
1085     }
1086     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY) && self->iconic) {
1087         g_message("UNSETTING ICONIC");
1088         if (self->frame) client_iconify(self, FALSE, TRUE);
1089         else self->iconic = FALSE;
1090     }
1091     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1092         if (self->frame) client_fullscreen(self, FALSE, TRUE);
1093         else self->fullscreen = FALSE;
1094     }
1095     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
1096                                                          self->max_vert)) {
1097         if (self->frame) client_maximize(self, FALSE, 0, TRUE);
1098         else self->max_vert = self->max_horz = FALSE;
1099     }
1100 }
1101
1102 void client_reconfigure(ObClient *self)
1103 {
1104     /* by making this pass FALSE for user, we avoid the emacs event storm where
1105        every configurenotify causes an update in its normal hints, i think this
1106        is generally what we want anyways... */
1107     client_configure(self, OB_CORNER_TOPLEFT, self->area.x, self->area.y,
1108                      self->area.width, self->area.height, FALSE, TRUE);
1109 }
1110
1111 void client_update_wmhints(ObClient *self)
1112 {
1113     XWMHints *hints;
1114     gboolean ur = FALSE;
1115     GSList *it;
1116
1117     /* assume a window takes input if it doesnt specify */
1118     self->can_focus = TRUE;
1119   
1120     if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1121         if (hints->flags & InputHint)
1122             self->can_focus = hints->input;
1123
1124         /* only do this when first managing the window *AND* when we aren't
1125            starting up! */
1126         if (ob_state != OB_STATE_STARTING && self->frame == NULL)
1127             if (hints->flags & StateHint)
1128                 self->iconic = hints->initial_state == IconicState;
1129
1130         if (hints->flags & XUrgencyHint)
1131             ur = TRUE;
1132
1133         if (!(hints->flags & WindowGroupHint))
1134             hints->window_group = None;
1135
1136         /* did the group state change? */
1137         if (hints->window_group !=
1138             (self->group ? self->group->leader : None)) {
1139             /* remove from the old group if there was one */
1140             if (self->group != NULL) {
1141                 /* remove transients of the group */
1142                 for (it = self->group->members; it; it = it->next)
1143                     self->transients = g_slist_remove(self->transients,
1144                                                       it->data);
1145                 group_remove(self->group, self);
1146                 self->group = NULL;
1147             }
1148             if (hints->window_group != None) {
1149                 self->group = group_add(hints->window_group, self);
1150
1151                 /* i can only have transients from the group if i am not
1152                    transient myself */
1153                 if (!self->transient_for) {
1154                     /* add other transients of the group that are already
1155                        set up */
1156                     for (it = self->group->members; it; it = it->next)
1157                         if (it->data != self &&
1158                             ((ObClient*)it->data)->transient_for == OB_TRAN_GROUP)
1159                             self->transients = g_slist_append(self->transients,
1160                                                               it->data);
1161                 }
1162             }
1163
1164             /* the WM_HINTS can contain an icon */
1165             client_update_icons(self);
1166
1167             /* because the self->transient flag wont change from this call,
1168                we don't need to update the window's type and such, only its
1169                transient_for, and the transients lists of other windows in
1170                the group may be affected */
1171             client_update_transient_for(self);
1172         }
1173
1174         XFree(hints);
1175     }
1176
1177     if (ur != self->urgent) {
1178         self->urgent = ur;
1179         g_message("Urgent Hint for 0x%lx: %s", self->window,
1180                   ur ? "ON" : "OFF");
1181         /* fire the urgent callback if we're mapped, otherwise, wait until
1182            after we're mapped */
1183         if (self->frame)
1184             dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1185     }
1186 }
1187
1188 void client_update_title(ObClient *self)
1189 {
1190     GList *it;
1191     guint32 nums;
1192     guint i;
1193     char *data = NULL;
1194     gboolean read_title;
1195
1196     g_free(self->title);
1197      
1198     /* try netwm */
1199     if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1200         /* try old x stuff */
1201         if (!PROP_GETS(self->window, wm_name, locale, &data))
1202             data = g_strdup("Unnamed Window");
1203
1204     /* look for duplicates and append a number */
1205     nums = 0;
1206     for (it = client_list; it; it = it->next)
1207         if (it->data != self) {
1208             ObClient *c = it->data;
1209             if (0 == strncmp(c->title, data, strlen(data)))
1210                 nums |= 1 << c->title_count;
1211         }
1212     /* find first free number */
1213     for (i = 1; i <= 32; ++i)
1214         if (!(nums & (1 << i))) {
1215             if (self->title_count == 1 || i == 1)
1216                 self->title_count = i;
1217             break;
1218         }
1219     /* dont display the number for the first window */
1220     if (self->title_count > 1) {
1221         char *vdata, *ndata;
1222         ndata = g_strdup_printf(" - [%u]", self->title_count);
1223         vdata = g_strconcat(data, ndata, NULL);
1224         g_free(ndata);
1225         g_free(data);
1226         data = vdata;
1227     }
1228
1229     PROP_SETS(self->window, net_wm_visible_name, data);
1230
1231     self->title = data;
1232
1233     if (self->frame)
1234         frame_adjust_title(self->frame);
1235
1236     /* update the icon title */
1237     data = NULL;
1238     g_free(self->icon_title);
1239
1240     read_title = TRUE;
1241     /* try netwm */
1242     if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1243         /* try old x stuff */
1244         if (!PROP_GETS(self->window, wm_icon_name, locale, &data)) {
1245             data = g_strdup(self->title);
1246             read_title = FALSE;
1247         }
1248
1249     /* append the title count, dont display the number for the first window */
1250     if (read_title && self->title_count > 1) {
1251         char *vdata, *ndata;
1252         ndata = g_strdup_printf(" - [%u]", self->title_count);
1253         vdata = g_strconcat(data, ndata, NULL);
1254         g_free(ndata);
1255         g_free(data);
1256         data = vdata;
1257     }
1258
1259     PROP_SETS(self->window, net_wm_visible_icon_name, data);
1260
1261     self->icon_title = data;
1262 }
1263
1264 void client_update_class(ObClient *self)
1265 {
1266     char **data;
1267     char *s;
1268
1269     if (self->name) g_free(self->name);
1270     if (self->class) g_free(self->class);
1271     if (self->role) g_free(self->role);
1272
1273     self->name = self->class = self->role = NULL;
1274
1275     if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1276         if (data[0]) {
1277             self->name = g_strdup(data[0]);
1278             if (data[1])
1279                 self->class = g_strdup(data[1]);
1280         }
1281         g_strfreev(data);     
1282     }
1283
1284     if (PROP_GETS(self->window, wm_window_role, locale, &s))
1285         self->role = g_strdup(s);
1286
1287     if (self->name == NULL) self->name = g_strdup("");
1288     if (self->class == NULL) self->class = g_strdup("");
1289     if (self->role == NULL) self->role = g_strdup("");
1290 }
1291
1292 void client_update_strut(ObClient *self)
1293 {
1294     guint num;
1295     guint32 *data;
1296
1297     if (!PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1298         STRUT_SET(self->strut, 0, 0, 0, 0);
1299     } else {
1300         if (num == 4)
1301             STRUT_SET(self->strut, data[0], data[2], data[1], data[3]);
1302         else
1303             STRUT_SET(self->strut, 0, 0, 0, 0);
1304         g_free(data);
1305     }
1306
1307     /* updating here is pointless while we're being mapped cuz we're not in
1308        the client list yet */
1309     if (self->frame)
1310         screen_update_areas();
1311 }
1312
1313 void client_update_icons(ObClient *self)
1314 {
1315     guint num;
1316     guint32 *data;
1317     guint w, h, i, j;
1318
1319     for (i = 0; i < self->nicons; ++i)
1320         g_free(self->icons[i].data);
1321     if (self->nicons > 0)
1322         g_free(self->icons);
1323     self->nicons = 0;
1324
1325     if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1326         /* figure out how many valid icons are in here */
1327         i = 0;
1328         while (num - i > 2) {
1329             w = data[i++];
1330             h = data[i++];
1331             i += w * h;
1332             if (i > num || w*h == 0) break;
1333             ++self->nicons;
1334         }
1335
1336         self->icons = g_new(ObClientIcon, self->nicons);
1337     
1338         /* store the icons */
1339         i = 0;
1340         for (j = 0; j < self->nicons; ++j) {
1341             guint x, y, t;
1342
1343             w = self->icons[j].width = data[i++];
1344             h = self->icons[j].height = data[i++];
1345
1346             if (w*h == 0) continue;
1347
1348             self->icons[j].data = g_new(RrPixel32, w * h);
1349             for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1350                 if (x >= w) {
1351                     x = 0;
1352                     ++y;
1353                 }
1354                 self->icons[j].data[t] =
1355                     (((data[i] >> 24) & 0xff) << RrDefaultAlphaOffset) +
1356                     (((data[i] >> 16) & 0xff) << RrDefaultRedOffset) +
1357                     (((data[i] >> 8) & 0xff) << RrDefaultGreenOffset) +
1358                     (((data[i] >> 0) & 0xff) << RrDefaultBlueOffset);
1359             }
1360             g_assert(i <= num);
1361         }
1362
1363         g_free(data);
1364     } else if (PROP_GETA32(self->window, kwm_win_icon,
1365                            kwm_win_icon, &data, &num)) {
1366         if (num == 2) {
1367             self->nicons++;
1368             self->icons = g_new(ObClientIcon, self->nicons);
1369             xerror_set_ignore(TRUE);
1370             if (!RrPixmapToRGBA(ob_rr_inst,
1371                                 data[0], data[1],
1372                                 &self->icons[self->nicons-1].width,
1373                                 &self->icons[self->nicons-1].height,
1374                                 &self->icons[self->nicons-1].data)) {
1375                 g_free(&self->icons[self->nicons-1]);
1376                 self->nicons--;
1377             }
1378             xerror_set_ignore(FALSE);
1379         }
1380         g_free(data);
1381     } else {
1382         XWMHints *hints;
1383
1384         if ((hints = XGetWMHints(ob_display, self->window))) {
1385             if (hints->flags & IconPixmapHint) {
1386                 self->nicons++;
1387                 self->icons = g_new(ObClientIcon, self->nicons);
1388                 xerror_set_ignore(TRUE);
1389                 if (!RrPixmapToRGBA(ob_rr_inst,
1390                                     hints->icon_pixmap,
1391                                     (hints->flags & IconMaskHint ?
1392                                      hints->icon_mask : None),
1393                                     &self->icons[self->nicons-1].width,
1394                                     &self->icons[self->nicons-1].height,
1395                                     &self->icons[self->nicons-1].data)){
1396                     g_free(&self->icons[self->nicons-1]);
1397                     self->nicons--;
1398                 }
1399                 xerror_set_ignore(FALSE);
1400             }
1401             XFree(hints);
1402         }
1403     }
1404
1405     if (self->frame)
1406         frame_adjust_icon(self->frame);
1407 }
1408
1409 static void client_change_state(ObClient *self)
1410 {
1411     guint32 state[2];
1412     guint32 netstate[10];
1413     guint num;
1414
1415     state[0] = self->wmstate;
1416     state[1] = None;
1417     PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1418
1419     num = 0;
1420     if (self->modal)
1421         netstate[num++] = prop_atoms.net_wm_state_modal;
1422     if (self->shaded)
1423         netstate[num++] = prop_atoms.net_wm_state_shaded;
1424     if (self->iconic)
1425         netstate[num++] = prop_atoms.net_wm_state_hidden;
1426     if (self->skip_taskbar)
1427         netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1428     if (self->skip_pager)
1429         netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1430     if (self->fullscreen)
1431         netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1432     if (self->max_vert)
1433         netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1434     if (self->max_horz)
1435         netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1436     if (self->above)
1437         netstate[num++] = prop_atoms.net_wm_state_above;
1438     if (self->below)
1439         netstate[num++] = prop_atoms.net_wm_state_below;
1440     PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1441
1442     client_calc_layer(self);
1443
1444     if (self->frame)
1445         frame_adjust_state(self->frame);
1446 }
1447
1448 ObClient *client_search_focus_tree(ObClient *self)
1449 {
1450     GSList *it;
1451     ObClient *ret;
1452
1453     for (it = self->transients; it != NULL; it = it->next) {
1454         if (client_focused(it->data)) return it->data;
1455         if ((ret = client_search_focus_tree(it->data))) return ret;
1456     }
1457     return NULL;
1458 }
1459
1460 ObClient *client_search_focus_tree_full(ObClient *self)
1461 {
1462     if (self->transient_for) {
1463         if (self->transient_for != OB_TRAN_GROUP) {
1464             return client_search_focus_tree_full(self->transient_for);
1465         } else {
1466             GSList *it;
1467             gboolean recursed = FALSE;
1468         
1469             for (it = self->group->members; it; it = it->next)
1470                 if (!((ObClient*)it->data)->transient_for) {
1471                     ObClient *c;
1472                     if ((c = client_search_focus_tree_full(it->data)))
1473                         return c;
1474                     recursed = TRUE;
1475                 }
1476             if (recursed)
1477               return NULL;
1478         }
1479     }
1480
1481     /* this function checks the whole tree, the client_search_focus_tree~
1482        does not, so we need to check this window */
1483     if (client_focused(self))
1484       return self;
1485     return client_search_focus_tree(self);
1486 }
1487
1488 static ObStackingLayer calc_layer(ObClient *self)
1489 {
1490     ObStackingLayer l;
1491
1492     if (self->fullscreen) l = OB_STACKING_LAYER_FULLSCREEN;
1493     else if (self->type == OB_CLIENT_TYPE_DESKTOP)
1494         l = OB_STACKING_LAYER_DESKTOP;
1495     else if (self->type == OB_CLIENT_TYPE_DOCK) {
1496         if (!self->below) l = OB_STACKING_LAYER_TOP;
1497         else l = OB_STACKING_LAYER_NORMAL;
1498     }
1499     else if (self->above) l = OB_STACKING_LAYER_ABOVE;
1500     else if (self->below) l = OB_STACKING_LAYER_BELOW;
1501     else l = OB_STACKING_LAYER_NORMAL;
1502
1503     return l;
1504 }
1505
1506 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
1507                                         ObStackingLayer l, gboolean raised)
1508 {
1509     ObStackingLayer old, own;
1510     GSList *it;
1511
1512     old = self->layer;
1513     own = calc_layer(self);
1514     self->layer = l > own ? l : own;
1515
1516     for (it = self->transients; it; it = it->next)
1517         client_calc_layer_recursive(it->data, orig,
1518                                     l, raised ? raised : l != old);
1519
1520     if (!raised && l != old)
1521         if (orig->frame) { /* only restack if the original window is managed */
1522             /* XXX add_non_intrusive ever? */
1523             stacking_remove(CLIENT_AS_WINDOW(self));
1524             stacking_add(CLIENT_AS_WINDOW(self));
1525         }
1526 }
1527
1528 void client_calc_layer(ObClient *self)
1529 {
1530     ObStackingLayer l;
1531     ObClient *orig;
1532
1533     orig = self;
1534
1535     /* transients take on the layer of their parents */
1536     self = client_search_top_transient(self);
1537
1538     l = calc_layer(self);
1539
1540     client_calc_layer_recursive(self, orig, l, FALSE);
1541 }
1542
1543 gboolean client_should_show(ObClient *self)
1544 {
1545     if (self->iconic) return FALSE;
1546     else if (!(self->desktop == screen_desktop ||
1547                self->desktop == DESKTOP_ALL)) return FALSE;
1548     else if (client_normal(self) && screen_showing_desktop) return FALSE;
1549     
1550     return TRUE;
1551 }
1552
1553 static void client_showhide(ObClient *self)
1554 {
1555
1556     if (client_should_show(self))
1557         frame_show(self->frame);
1558     else
1559         frame_hide(self->frame);
1560 }
1561
1562 gboolean client_normal(ObClient *self) {
1563     return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
1564               self->type == OB_CLIENT_TYPE_DOCK ||
1565               self->type == OB_CLIENT_TYPE_SPLASH);
1566 }
1567
1568 static void client_apply_startup_state(ObClient *self)
1569 {
1570     /* these are in a carefully crafted order.. */
1571
1572     if (self->iconic) {
1573         self->iconic = FALSE;
1574         client_iconify(self, TRUE, FALSE);
1575     }
1576     if (self->fullscreen) {
1577         self->fullscreen = FALSE;
1578         client_fullscreen(self, TRUE, FALSE);
1579     }
1580     if (self->shaded) {
1581         self->shaded = FALSE;
1582         client_shade(self, TRUE);
1583     }
1584     if (self->urgent)
1585         dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1586   
1587     if (self->max_vert && self->max_horz) {
1588         self->max_vert = self->max_horz = FALSE;
1589         client_maximize(self, TRUE, 0, FALSE);
1590     } else if (self->max_vert) {
1591         self->max_vert = FALSE;
1592         client_maximize(self, TRUE, 2, FALSE);
1593     } else if (self->max_horz) {
1594         self->max_horz = FALSE;
1595         client_maximize(self, TRUE, 1, FALSE);
1596     }
1597
1598     /* nothing to do for the other states:
1599        skip_taskbar
1600        skip_pager
1601        modal
1602        above
1603        below
1604     */
1605 }
1606
1607 void client_configure(ObClient *self, ObCorner anchor,
1608                       int x, int y, int w, int h,
1609                       gboolean user, gboolean final)
1610 {
1611     gboolean moved = FALSE, resized = FALSE;
1612
1613     /* gets the frame's position */
1614     frame_client_gravity(self->frame, &x, &y);
1615
1616     /* these positions are frame positions, not client positions */
1617
1618     /* set the size and position if fullscreen */
1619     if (self->fullscreen) {
1620 #ifdef VIDMODE
1621         int dot;
1622         XF86VidModeModeLine mode;
1623 #endif
1624         Rect *a;
1625         guint i;
1626
1627         i = client_monitor(self);
1628         a = screen_physical_area_monitor(i);
1629
1630 #ifdef VIDMODE
1631         if (i == 0 && /* primary head */
1632             extensions_vidmode &&
1633             XF86VidModeGetViewPort(ob_display, ob_screen, &x, &y) &&
1634             /* get the mode last so the mode.privsize isnt freed incorrectly */
1635             XF86VidModeGetModeLine(ob_display, ob_screen, &dot, &mode)) {
1636             x += a->x;
1637             y += a->y;
1638             w = mode.hdisplay;
1639             h = mode.vdisplay;
1640             if (mode.privsize) XFree(mode.private);
1641         } else
1642 #endif
1643         {
1644             x = a->x;
1645             y = a->y;
1646             w = a->width;
1647             h = a->height;
1648         }
1649
1650         user = FALSE; /* ignore that increment etc shit when in fullscreen */
1651     } else {
1652         Rect *a;
1653
1654         a = screen_area_monitor(self->desktop, client_monitor(self));
1655
1656         /* set the size and position if maximized */
1657         if (self->max_horz) {
1658             x = a->x - self->frame->size.left;
1659             w = a->width;
1660         }
1661         if (self->max_vert) {
1662             y = a->y;
1663             h = a->height - self->frame->size.top - self->frame->size.bottom;
1664         }
1665     }
1666
1667     /* gets the client's position */
1668     frame_frame_gravity(self->frame, &x, &y);
1669
1670     /* these override the above states! if you cant move you can't move! */
1671     if (user) {
1672         if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
1673             x = self->area.x;
1674             y = self->area.y;
1675         }
1676         if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
1677             w = self->area.width;
1678             h = self->area.height;
1679         }
1680     }
1681
1682     if (!(w == self->area.width && h == self->area.height)) {
1683         int basew, baseh, minw, minh;
1684
1685         /* base size is substituted with min size if not specified */
1686         if (self->base_size.width || self->base_size.height) {
1687             basew = self->base_size.width;
1688             baseh = self->base_size.height;
1689         } else {
1690             basew = self->min_size.width;
1691             baseh = self->min_size.height;
1692         }
1693         /* min size is substituted with base size if not specified */
1694         if (self->min_size.width || self->min_size.height) {
1695             minw = self->min_size.width;
1696             minh = self->min_size.height;
1697         } else {
1698             minw = self->base_size.width;
1699             minh = self->base_size.height;
1700         }
1701
1702         if (user) {
1703             /* for interactive resizing. have to move half an increment in each
1704                direction. */
1705
1706             /* how far we are towards the next size inc */
1707             int mw = (w - basew) % self->size_inc.width; 
1708             int mh = (h - baseh) % self->size_inc.height;
1709             /* amount to add */
1710             int aw = self->size_inc.width / 2;
1711             int ah = self->size_inc.height / 2;
1712             /* don't let us move into a new size increment */
1713             if (mw + aw >= self->size_inc.width)
1714                 aw = self->size_inc.width - mw - 1;
1715             if (mh + ah >= self->size_inc.height)
1716                 ah = self->size_inc.height - mh - 1;
1717             w += aw;
1718             h += ah;
1719     
1720             /* if this is a user-requested resize, then check against min/max
1721                sizes */
1722
1723             /* smaller than min size or bigger than max size? */
1724             if (w > self->max_size.width) w = self->max_size.width;
1725             if (w < minw) w = minw;
1726             if (h > self->max_size.height) h = self->max_size.height;
1727             if (h < minh) h = minh;
1728         }
1729
1730         w -= basew;
1731         h -= baseh;
1732
1733         /* keep to the increments */
1734         w /= self->size_inc.width;
1735         h /= self->size_inc.height;
1736
1737         /* you cannot resize to nothing */
1738         if (w < 1) w = 1;
1739         if (h < 1) h = 1;
1740   
1741         /* store the logical size */
1742         SIZE_SET(self->logical_size, w, h);
1743
1744         w *= self->size_inc.width;
1745         h *= self->size_inc.height;
1746
1747         w += basew;
1748         h += baseh;
1749
1750         if (user) {
1751             /* adjust the height to match the width for the aspect ratios.
1752              for this, min size is not substituted for base size ever. */
1753             w -= self->base_size.width;
1754             h -= self->base_size.height;
1755
1756             if (self->min_ratio)
1757                 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1758             if (self->max_ratio)
1759                 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1760
1761             w += self->base_size.width;
1762             h += self->base_size.height;
1763         }
1764     }
1765
1766     switch (anchor) {
1767     case OB_CORNER_TOPLEFT:
1768         break;
1769     case OB_CORNER_TOPRIGHT:
1770         x -= w - self->area.width;
1771         break;
1772     case OB_CORNER_BOTTOMLEFT:
1773         y -= h - self->area.height;
1774         break;
1775     case OB_CORNER_BOTTOMRIGHT:
1776         x -= w - self->area.width;
1777         y -= h - self->area.height;
1778         break;
1779     }
1780
1781     moved = x != self->area.x || y != self->area.y;
1782     resized = w != self->area.width || h != self->area.height;
1783
1784     RECT_SET(self->area, x, y, w, h);
1785
1786     /* for app-requested resizes, always resize if 'resized' is true.
1787        for user-requested ones, only resize if final is true, or when
1788        resizing in opaque mode */
1789     if ((!user && resized) ||
1790         (user && (final || (resized && config_opaque_resize))))
1791         XResizeWindow(ob_display, self->window, w, h);
1792
1793     /* move/resize the frame to match the request */
1794     if (self->frame) {
1795         if (self->decorations != self->frame->decorations)
1796             moved = resized = TRUE;
1797
1798         if (moved || resized)
1799             frame_adjust_area(self->frame, moved, resized);
1800
1801         /* If you send this and the client hasn't changed you end up with buggy
1802            clients (emacs) freaking out, cuz they send back a configure every
1803            time they receive this event, which resends them this event... etc.
1804         */
1805         if ((!user && moved) || (user && final)) {
1806             XEvent event;
1807             event.type = ConfigureNotify;
1808             event.xconfigure.display = ob_display;
1809             event.xconfigure.event = self->window;
1810             event.xconfigure.window = self->window;
1811     
1812             /* root window real coords */
1813             event.xconfigure.x = self->frame->area.x + self->frame->size.left;
1814             event.xconfigure.y = self->frame->area.y + self->frame->size.top;
1815     
1816             event.xconfigure.width = w;
1817             event.xconfigure.height = h;
1818             event.xconfigure.border_width = 0;
1819             event.xconfigure.above = self->frame->plate;
1820             event.xconfigure.override_redirect = FALSE;
1821             XSendEvent(event.xconfigure.display, event.xconfigure.window,
1822                        FALSE, StructureNotifyMask, &event);
1823         }
1824     }
1825 }
1826
1827 void client_fullscreen(ObClient *self, gboolean fs, gboolean savearea)
1828 {
1829     int x, y, w, h;
1830
1831     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
1832         self->fullscreen == fs) return;                   /* already done */
1833
1834     self->fullscreen = fs;
1835     client_change_state(self); /* change the state hints on the client,
1836                                   and adjust out layer/stacking */
1837
1838     if (fs) {
1839         if (savearea) {
1840             guint32 dimensions[4];
1841             dimensions[0] = self->area.x;
1842             dimensions[1] = self->area.y;
1843             dimensions[2] = self->area.width;
1844             dimensions[3] = self->area.height;
1845   
1846             PROP_SETA32(self->window, openbox_premax, cardinal,
1847                         dimensions, 4);
1848         }
1849
1850         /* these are not actually used cuz client_configure will set them
1851            as appropriate when the window is fullscreened */
1852         x = y = w = h = 0;
1853     } else {
1854         guint num;
1855         gint32 *dimensions;
1856         Rect *a;
1857
1858         /* pick some fallbacks... */
1859         a = screen_area_monitor(self->desktop, 0);
1860         x = a->x + a->width / 4;
1861         y = a->y + a->height / 4;
1862         w = a->width / 2;
1863         h = a->height / 2;
1864
1865         if (PROP_GETA32(self->window, openbox_premax, cardinal,
1866                         (guint32**)&dimensions, &num)) {
1867             if (num == 4) {
1868                 x = dimensions[0];
1869                 y = dimensions[1];
1870                 w = dimensions[2];
1871                 h = dimensions[3];
1872             }
1873             g_free(dimensions);
1874         }
1875     }
1876
1877     client_setup_decor_and_functions(self);
1878
1879     client_configure(self, OB_CORNER_TOPLEFT, x, y, w, h, TRUE, TRUE);
1880
1881     /* try focus us when we go into fullscreen mode */
1882     client_focus(self);
1883 }
1884
1885 static void client_iconify_recursive(ObClient *self,
1886                                      gboolean iconic, gboolean curdesk)
1887 {
1888     GSList *it;
1889     gboolean changed = FALSE;
1890
1891
1892     if (self->iconic != iconic) {
1893         g_message("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
1894                   self->window);
1895
1896         self->iconic = iconic;
1897
1898         if (iconic) {
1899             if (self->functions & OB_CLIENT_FUNC_ICONIFY) {
1900                 self->wmstate = IconicState;
1901                 self->ignore_unmaps++;
1902                 /* we unmap the client itself so that we can get MapRequest
1903                    events, and because the ICCCM tells us to! */
1904                 XUnmapWindow(ob_display, self->window);
1905
1906                 /* update the focus lists.. iconic windows go to the bottom of
1907                    the list, put the new iconic window at the 'top of the
1908                    bottom'. */
1909                 focus_order_to_top(self);
1910
1911                 changed = TRUE;
1912             }
1913         } else {
1914             if (curdesk)
1915                 client_set_desktop(self, screen_desktop, FALSE);
1916             self->wmstate = self->shaded ? IconicState : NormalState;
1917             XMapWindow(ob_display, self->window);
1918
1919             /* this puts it after the current focused window */
1920             focus_order_remove(self);
1921             focus_order_add_new(self);
1922
1923             /* this is here cuz with the VIDMODE extension, the viewport can
1924                change while a fullscreen window is iconic, and when it
1925                uniconifies, it would be nice if it did so to the new position
1926                of the viewport */
1927             client_reconfigure(self);
1928
1929             changed = TRUE;
1930         }
1931     }
1932
1933     if (changed) {
1934         client_change_state(self);
1935         client_showhide(self);
1936         screen_update_areas();
1937
1938         dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
1939                         self, 0, 0);
1940     }
1941
1942     /* iconify all transients */
1943     for (it = self->transients; it != NULL; it = it->next)
1944         if (it->data != self) client_iconify_recursive(it->data,
1945                                                        iconic, curdesk);
1946 }
1947
1948 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk)
1949 {
1950     /* move up the transient chain as far as possible first */
1951     self = client_search_top_transient(self);
1952
1953     client_iconify_recursive(client_search_top_transient(self),
1954                              iconic, curdesk);
1955 }
1956
1957 void client_maximize(ObClient *self, gboolean max, int dir, gboolean savearea)
1958 {
1959     int x, y, w, h;
1960      
1961     g_assert(dir == 0 || dir == 1 || dir == 2);
1962     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE)) return; /* can't */
1963
1964     /* check if already done */
1965     if (max) {
1966         if (dir == 0 && self->max_horz && self->max_vert) return;
1967         if (dir == 1 && self->max_horz) return;
1968         if (dir == 2 && self->max_vert) return;
1969     } else {
1970         if (dir == 0 && !self->max_horz && !self->max_vert) return;
1971         if (dir == 1 && !self->max_horz) return;
1972         if (dir == 2 && !self->max_vert) return;
1973     }
1974
1975     /* work with the frame's coords */
1976     x = self->frame->area.x;
1977     y = self->frame->area.y;
1978     w = self->area.width;
1979     h = self->area.height;
1980
1981     if (max) {
1982         if (savearea) {
1983             gint32 dimensions[4];
1984             gint32 *readdim;
1985             guint num;
1986
1987             dimensions[0] = x;
1988             dimensions[1] = y;
1989             dimensions[2] = w;
1990             dimensions[3] = h;
1991
1992             /* get the property off the window and use it for the dimensions
1993                we are already maxed on */
1994             if (PROP_GETA32(self->window, openbox_premax, cardinal,
1995                             (guint32**)&readdim, &num)) {
1996                 if (num == 4) {
1997                     if (self->max_horz) {
1998                         dimensions[0] = readdim[0];
1999                         dimensions[2] = readdim[2];
2000                     }
2001                     if (self->max_vert) {
2002                         dimensions[1] = readdim[1];
2003                         dimensions[3] = readdim[3];
2004                     }
2005                 }
2006                 g_free(readdim);
2007             }
2008
2009             PROP_SETA32(self->window, openbox_premax, cardinal,
2010                         (guint32*)dimensions, 4);
2011         }
2012     } else {
2013         guint num;
2014         gint32 *dimensions;
2015         Rect *a;
2016
2017         /* pick some fallbacks... */
2018         a = screen_area_monitor(self->desktop, 0);
2019         if (dir == 0 || dir == 1) { /* horz */
2020             x = a->x + a->width / 4;
2021             w = a->width / 2;
2022         }
2023         if (dir == 0 || dir == 2) { /* vert */
2024             y = a->y + a->height / 4;
2025             h = a->height / 2;
2026         }
2027
2028         if (PROP_GETA32(self->window, openbox_premax, cardinal,
2029                         (guint32**)&dimensions, &num)) {
2030             if (num == 4) {
2031                 if (dir == 0 || dir == 1) { /* horz */
2032                     x = dimensions[0];
2033                     w = dimensions[2];
2034                 }
2035                 if (dir == 0 || dir == 2) { /* vert */
2036                     y = dimensions[1];
2037                     h = dimensions[3];
2038                 }
2039             }
2040             g_free(dimensions);
2041         }
2042     }
2043
2044     if (dir == 0 || dir == 1) /* horz */
2045         self->max_horz = max;
2046     if (dir == 0 || dir == 2) /* vert */
2047         self->max_vert = max;
2048
2049     if (!self->max_horz && !self->max_vert)
2050         PROP_ERASE(self->window, openbox_premax);
2051
2052     client_change_state(self); /* change the state hints on the client */
2053
2054     /* figure out where the client should be going */
2055     frame_frame_gravity(self->frame, &x, &y);
2056     client_configure(self, OB_CORNER_TOPLEFT, x, y, w, h, TRUE, TRUE);
2057 }
2058
2059 void client_shade(ObClient *self, gboolean shade)
2060 {
2061     if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
2062          shade) ||                         /* can't shade */
2063         self->shaded == shade) return;     /* already done */
2064
2065     /* when we're iconic, don't change the wmstate */
2066     if (!self->iconic)
2067         self->wmstate = shade ? IconicState : NormalState;
2068     self->shaded = shade;
2069     client_change_state(self);
2070     /* resize the frame to just the titlebar */
2071     frame_adjust_area(self->frame, FALSE, FALSE);
2072 }
2073
2074 void client_close(ObClient *self)
2075 {
2076     XEvent ce;
2077
2078     if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
2079
2080     /*
2081       XXX: itd be cool to do timeouts and shit here for killing the client's
2082       process off
2083       like... if the window is around after 5 seconds, then the close button
2084       turns a nice red, and if this function is called again, the client is
2085       explicitly killed.
2086     */
2087
2088     ce.xclient.type = ClientMessage;
2089     ce.xclient.message_type =  prop_atoms.wm_protocols;
2090     ce.xclient.display = ob_display;
2091     ce.xclient.window = self->window;
2092     ce.xclient.format = 32;
2093     ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
2094     ce.xclient.data.l[1] = event_lasttime;
2095     ce.xclient.data.l[2] = 0l;
2096     ce.xclient.data.l[3] = 0l;
2097     ce.xclient.data.l[4] = 0l;
2098     XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2099 }
2100
2101 void client_kill(ObClient *self)
2102 {
2103     XKillClient(ob_display, self->window);
2104 }
2105
2106 void client_set_desktop_recursive(ObClient *self,
2107                                   guint target, gboolean donthide)
2108 {
2109     guint old;
2110     GSList *it;
2111
2112     if (target != self->desktop) {
2113
2114         g_message("Setting desktop %u", target+1);
2115
2116         g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2117
2118         /* remove from the old desktop(s) */
2119         focus_order_remove(self);
2120
2121         old = self->desktop;
2122         self->desktop = target;
2123         PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2124         /* the frame can display the current desktop state */
2125         frame_adjust_state(self->frame);
2126         /* 'move' the window to the new desktop */
2127         if (!donthide)
2128             client_showhide(self);
2129         /* raise if it was not already on the desktop */
2130         if (old != DESKTOP_ALL)
2131             stacking_raise(CLIENT_AS_WINDOW(self));
2132         screen_update_areas();
2133
2134         /* add to the new desktop(s) */
2135         if (config_focus_new)
2136             focus_order_to_top(self);
2137         else
2138             focus_order_to_bottom(self);
2139
2140         dispatch_client(Event_Client_Desktop, self, target, old);
2141     }
2142
2143     /* move all transients */
2144     for (it = self->transients; it != NULL; it = it->next)
2145         if (it->data != self) client_set_desktop_recursive(it->data,
2146                                                            target, donthide);
2147 }
2148
2149 void client_set_desktop(ObClient *self, guint target, gboolean donthide)
2150 {
2151     client_set_desktop_recursive(client_search_top_transient(self),
2152                                  target, donthide);
2153 }
2154
2155 ObClient *client_search_modal_child(ObClient *self)
2156 {
2157     GSList *it;
2158     ObClient *ret;
2159   
2160     for (it = self->transients; it != NULL; it = it->next) {
2161         ObClient *c = it->data;
2162         if ((ret = client_search_modal_child(c))) return ret;
2163         if (c->modal) return c;
2164     }
2165     return NULL;
2166 }
2167
2168 gboolean client_validate(ObClient *self)
2169 {
2170     XEvent e; 
2171
2172     XSync(ob_display, FALSE); /* get all events on the server */
2173
2174     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2175         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2176         XPutBackEvent(ob_display, &e);
2177         return FALSE;
2178     }
2179
2180     return TRUE;
2181 }
2182
2183 void client_set_wm_state(ObClient *self, long state)
2184 {
2185     if (state == self->wmstate) return; /* no change */
2186   
2187     switch (state) {
2188     case IconicState:
2189         client_iconify(self, TRUE, TRUE);
2190         break;
2191     case NormalState:
2192         client_iconify(self, FALSE, TRUE);
2193         break;
2194     }
2195 }
2196
2197 void client_set_state(ObClient *self, Atom action, long data1, long data2)
2198 {
2199     gboolean shaded = self->shaded;
2200     gboolean fullscreen = self->fullscreen;
2201     gboolean max_horz = self->max_horz;
2202     gboolean max_vert = self->max_vert;
2203     int i;
2204
2205     if (!(action == prop_atoms.net_wm_state_add ||
2206           action == prop_atoms.net_wm_state_remove ||
2207           action == prop_atoms.net_wm_state_toggle))
2208         /* an invalid action was passed to the client message, ignore it */
2209         return; 
2210
2211     for (i = 0; i < 2; ++i) {
2212         Atom state = i == 0 ? data1 : data2;
2213     
2214         if (!state) continue;
2215
2216         /* if toggling, then pick whether we're adding or removing */
2217         if (action == prop_atoms.net_wm_state_toggle) {
2218             if (state == prop_atoms.net_wm_state_modal)
2219                 action = self->modal ? prop_atoms.net_wm_state_remove :
2220                     prop_atoms.net_wm_state_add;
2221             else if (state == prop_atoms.net_wm_state_maximized_vert)
2222                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2223                     prop_atoms.net_wm_state_add;
2224             else if (state == prop_atoms.net_wm_state_maximized_horz)
2225                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2226                     prop_atoms.net_wm_state_add;
2227             else if (state == prop_atoms.net_wm_state_shaded)
2228                 action = self->shaded ? prop_atoms.net_wm_state_remove :
2229                     prop_atoms.net_wm_state_add;
2230             else if (state == prop_atoms.net_wm_state_skip_taskbar)
2231                 action = self->skip_taskbar ?
2232                     prop_atoms.net_wm_state_remove :
2233                     prop_atoms.net_wm_state_add;
2234             else if (state == prop_atoms.net_wm_state_skip_pager)
2235                 action = self->skip_pager ?
2236                     prop_atoms.net_wm_state_remove :
2237                     prop_atoms.net_wm_state_add;
2238             else if (state == prop_atoms.net_wm_state_fullscreen)
2239                 action = self->fullscreen ?
2240                     prop_atoms.net_wm_state_remove :
2241                     prop_atoms.net_wm_state_add;
2242             else if (state == prop_atoms.net_wm_state_above)
2243                 action = self->above ? prop_atoms.net_wm_state_remove :
2244                     prop_atoms.net_wm_state_add;
2245             else if (state == prop_atoms.net_wm_state_below)
2246                 action = self->below ? prop_atoms.net_wm_state_remove :
2247                     prop_atoms.net_wm_state_add;
2248         }
2249     
2250         if (action == prop_atoms.net_wm_state_add) {
2251             if (state == prop_atoms.net_wm_state_modal) {
2252                 /* XXX raise here or something? */
2253                 self->modal = TRUE;
2254             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2255                 max_vert = TRUE;
2256             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2257                 max_horz = TRUE;
2258             } else if (state == prop_atoms.net_wm_state_shaded) {
2259                 shaded = TRUE;
2260             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2261                 self->skip_taskbar = TRUE;
2262             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2263                 self->skip_pager = TRUE;
2264             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2265                 fullscreen = TRUE;
2266             } else if (state == prop_atoms.net_wm_state_above) {
2267                 self->above = TRUE;
2268             } else if (state == prop_atoms.net_wm_state_below) {
2269                 self->below = TRUE;
2270             }
2271
2272         } else { /* action == prop_atoms.net_wm_state_remove */
2273             if (state == prop_atoms.net_wm_state_modal) {
2274                 self->modal = FALSE;
2275             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2276                 max_vert = FALSE;
2277             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2278                 max_horz = FALSE;
2279             } else if (state == prop_atoms.net_wm_state_shaded) {
2280                 shaded = FALSE;
2281             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2282                 self->skip_taskbar = FALSE;
2283             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2284                 self->skip_pager = FALSE;
2285             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2286                 fullscreen = FALSE;
2287             } else if (state == prop_atoms.net_wm_state_above) {
2288                 self->above = FALSE;
2289             } else if (state == prop_atoms.net_wm_state_below) {
2290                 self->below = FALSE;
2291             }
2292         }
2293     }
2294     if (max_horz != self->max_horz || max_vert != self->max_vert) {
2295         if (max_horz != self->max_horz && max_vert != self->max_vert) {
2296             /* toggling both */
2297             if (max_horz == max_vert) { /* both going the same way */
2298                 client_maximize(self, max_horz, 0, TRUE);
2299             } else {
2300                 client_maximize(self, max_horz, 1, TRUE);
2301                 client_maximize(self, max_vert, 2, TRUE);
2302             }
2303         } else {
2304             /* toggling one */
2305             if (max_horz != self->max_horz)
2306                 client_maximize(self, max_horz, 1, TRUE);
2307             else
2308                 client_maximize(self, max_vert, 2, TRUE);
2309         }
2310     }
2311     /* change fullscreen state before shading, as it will affect if the window
2312        can shade or not */
2313     if (fullscreen != self->fullscreen)
2314         client_fullscreen(self, fullscreen, TRUE);
2315     if (shaded != self->shaded)
2316         client_shade(self, shaded);
2317     client_calc_layer(self);
2318     client_change_state(self); /* change the hint to reflect these changes */
2319 }
2320
2321 ObClient *client_focus_target(ObClient *self)
2322 {
2323     ObClient *child;
2324      
2325     /* if we have a modal child, then focus it, not us */
2326     child = client_search_modal_child(self);
2327     if (child) return child;
2328     return self;
2329 }
2330
2331 gboolean client_can_focus(ObClient *self)
2332 {
2333     XEvent ev;
2334
2335     /* choose the correct target */
2336     self = client_focus_target(self);
2337
2338     if (!self->frame->visible)
2339         return FALSE;
2340
2341     if (!((self->can_focus || self->focus_notify) &&
2342           (self->desktop == screen_desktop ||
2343            self->desktop == DESKTOP_ALL) &&
2344           !self->iconic))
2345         return FALSE;
2346
2347     /* do a check to see if the window has already been unmapped or destroyed
2348        do this intelligently while watching out for unmaps we've generated
2349        (ignore_unmaps > 0) */
2350     if (XCheckTypedWindowEvent(ob_display, self->window,
2351                                DestroyNotify, &ev)) {
2352         XPutBackEvent(ob_display, &ev);
2353         return FALSE;
2354     }
2355     while (XCheckTypedWindowEvent(ob_display, self->window,
2356                                   UnmapNotify, &ev)) {
2357         if (self->ignore_unmaps) {
2358             self->ignore_unmaps--;
2359         } else {
2360             XPutBackEvent(ob_display, &ev);
2361             return FALSE;
2362         }
2363     }
2364
2365     return TRUE;
2366 }
2367
2368 gboolean client_focus(ObClient *self)
2369 {
2370     /* choose the correct target */
2371     self = client_focus_target(self);
2372
2373     if (!client_can_focus(self)) {
2374         if (!self->frame->visible) {
2375             /* update the focus lists */
2376             focus_order_to_top(self);
2377         }
2378         return FALSE;
2379     }
2380
2381     if (self->can_focus)
2382         /* RevertToPointerRoot causes much more headache than RevertToNone, so
2383            I choose to use it always, hopefully to find errors quicker, if any
2384            are left. (I hate X. I hate focus events.) */
2385         XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2386                        event_lasttime);
2387
2388     if (self->focus_notify) {
2389         XEvent ce;
2390         ce.xclient.type = ClientMessage;
2391         ce.xclient.message_type = prop_atoms.wm_protocols;
2392         ce.xclient.display = ob_display;
2393         ce.xclient.window = self->window;
2394         ce.xclient.format = 32;
2395         ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2396         ce.xclient.data.l[1] = event_lasttime;
2397         ce.xclient.data.l[2] = 0l;
2398         ce.xclient.data.l[3] = 0l;
2399         ce.xclient.data.l[4] = 0l;
2400         XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2401     }
2402
2403 #ifdef DEBUG_FOCUS
2404     g_message("%sively focusing %lx at %d", (self->can_focus ? "act" : "pass"),
2405               self->window, (int)
2406               event_lasttime);
2407 #endif
2408
2409     /* Cause the FocusIn to come back to us. Important for desktop switches,
2410        since otherwise we'll have no FocusIn on the queue and send it off to
2411        the focus_backup. */
2412     XSync(ob_display, FALSE);
2413     return TRUE;
2414 }
2415
2416 void client_unfocus(ObClient *self)
2417 {
2418     g_assert(focus_client == self);
2419 #ifdef DEBUG_FOCUS
2420     g_message("client_unfocus for %lx", self->window);
2421 #endif
2422     focus_fallback(OB_FOCUS_FALLBACK_UNFOCUSING);
2423 }
2424
2425 void client_activate(ObClient *self)
2426 {
2427     if (client_normal(self) && screen_showing_desktop)
2428         screen_show_desktop(FALSE);
2429     if (self->iconic)
2430         client_iconify(self, FALSE, TRUE);
2431     else if (!self->frame->visible)
2432         /* if its not visible for other reasons, then don't mess
2433            with it */
2434         return;
2435     if (self->shaded)
2436         client_shade(self, FALSE);
2437     client_focus(self);
2438     stacking_raise(CLIENT_AS_WINDOW(self));
2439 }
2440
2441 gboolean client_focused(ObClient *self)
2442 {
2443     return self == focus_client;
2444 }
2445
2446 ObClientIcon *client_icon(ObClient *self, int w, int h)
2447 {
2448     guint i;
2449     /* si is the smallest image >= req */
2450     /* li is the largest image < req */
2451     unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2452
2453     if (!self->nicons) return NULL;
2454
2455     for (i = 0; i < self->nicons; ++i) {
2456         size = self->icons[i].width * self->icons[i].height;
2457         if (size < smallest && size >= (unsigned)(w * h)) {
2458             smallest = size;
2459             si = i;
2460         }
2461         if (size > largest && size <= (unsigned)(w * h)) {
2462             largest = size;
2463             li = i;
2464         }
2465     }
2466     if (largest == 0) /* didnt find one smaller than the requested size */
2467         return &self->icons[si];
2468     return &self->icons[li];
2469 }
2470
2471 /* this be mostly ripped from fvwm */
2472 ObClient *client_find_directional(ObClient *c, ObDirection dir) 
2473 {
2474     int my_cx, my_cy, his_cx, his_cy;
2475     int offset = 0;
2476     int distance = 0;
2477     int score, best_score;
2478     ObClient *best_client, *cur;
2479     GList *it;
2480
2481     if(!client_list)
2482         return NULL;
2483
2484     /* first, find the centre coords of the currently focused window */
2485     my_cx = c->frame->area.x + c->frame->area.width / 2;
2486     my_cy = c->frame->area.y + c->frame->area.height / 2;
2487
2488     best_score = -1;
2489     best_client = NULL;
2490
2491     for(it = g_list_first(client_list); it; it = it->next) {
2492         cur = it->data;
2493
2494         /* the currently selected window isn't interesting */
2495         if(cur == c)
2496             continue;
2497         if (!client_normal(cur))
2498             continue;
2499         if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2500             continue;
2501         if(cur->iconic)
2502             continue;
2503         if(client_focus_target(cur) == cur &&
2504            !(cur->can_focus || cur->focus_notify))
2505             continue;
2506
2507         /* find the centre coords of this window, from the
2508          * currently focused window's point of view */
2509         his_cx = (cur->frame->area.x - my_cx)
2510             + cur->frame->area.width / 2;
2511         his_cy = (cur->frame->area.y - my_cy)
2512             + cur->frame->area.height / 2;
2513
2514         if(dir > 3) { 
2515             int tx;
2516             /* Rotate the diagonals 45 degrees counterclockwise.
2517              * To do this, multiply the matrix /+h +h\ with the
2518              * vector (x y).                   \-h +h/
2519              * h = sqrt(0.5). We can set h := 1 since absolute
2520              * distance doesn't matter here. */
2521             tx = his_cx + his_cy;
2522             his_cy = -his_cx + his_cy;
2523             his_cx = tx;
2524         }
2525
2526         switch(dir) {
2527         case OB_DIRECTION_NORTH:
2528         case OB_DIRECTION_SOUTH:
2529         case OB_DIRECTION_NORTHEAST:
2530         case OB_DIRECTION_SOUTHWEST:
2531             offset = (his_cx < 0) ? -his_cx : his_cx;
2532             distance = ((dir == OB_DIRECTION_NORTH ||
2533                         dir == OB_DIRECTION_NORTHEAST) ?
2534                         -his_cy : his_cy);
2535             break;
2536         case OB_DIRECTION_EAST:
2537         case OB_DIRECTION_WEST:
2538         case OB_DIRECTION_SOUTHEAST:
2539         case OB_DIRECTION_NORTHWEST:
2540             offset = (his_cy < 0) ? -his_cy : his_cy;
2541             distance = ((dir == OB_DIRECTION_WEST ||
2542                         dir == OB_DIRECTION_NORTHWEST) ?
2543                         -his_cx : his_cx);
2544             break;
2545         }
2546
2547         /* the target must be in the requested direction */
2548         if(distance <= 0)
2549             continue;
2550
2551         /* Calculate score for this window.  The smaller the better. */
2552         score = distance + offset;
2553
2554         /* windows more than 45 degrees off the direction are
2555          * heavily penalized and will only be chosen if nothing
2556          * else within a million pixels */
2557         if(offset > distance)
2558             score += 1000000;
2559
2560         if(best_score == -1 || score < best_score)
2561             best_client = cur,
2562                 best_score = score;
2563     }
2564
2565     return best_client;
2566 }
2567
2568 void client_set_layer(ObClient *self, int layer)
2569 {
2570     if (layer < 0) {
2571         self->below = TRUE;
2572         self->above = FALSE;
2573     } else if (layer == 0) {
2574         self->below = self->above = FALSE;
2575     } else {
2576         self->below = FALSE;
2577         self->above = TRUE;
2578     }
2579     client_calc_layer(self);
2580     client_change_state(self); /* reflect this in the state hints */
2581 }
2582
2583 guint client_monitor(ObClient *self)
2584 {
2585     guint i;
2586
2587     for (i = 0; i < screen_num_monitors; ++i) {
2588         Rect *area = screen_physical_area_monitor(i);
2589         if (RECT_INTERSECTS_RECT(*area, self->frame->area))
2590             break;
2591     }
2592     if (i == screen_num_monitors) i = 0;
2593     g_assert(i < screen_num_monitors);
2594     return i;
2595 }
2596
2597 ObClient *client_search_top_transient(ObClient *self)
2598 {
2599     /* move up the transient chain as far as possible */
2600     if (self->transient_for) {
2601         if (self->transient_for != OB_TRAN_GROUP) {
2602             return client_search_top_transient(self->transient_for);
2603         } else {
2604             GSList *it;
2605
2606             for (it = self->group->members; it; it = it->next) {
2607                 ObClient *c = it->data;
2608
2609                 /* checking transient_for prevents infinate loops! */
2610                 if (c != self && !c->transient_for)
2611                     break;
2612             }
2613             if (it)
2614                 return it->data;
2615         }
2616     }
2617
2618     return self;
2619 }