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