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