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