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