]> icculus.org git repositories - dana/openbox.git/blob - openbox/client.c
make those apps stay/map on screen!! fuck stupid apps. thats right im talking to...
[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 (*x >= a->x + a->width - 1)
482         *x = a->x + a->width - self->frame->area.width;
483     if (*y >= a->y + a->height - 1)
484         *y = a->y + a->height - self->frame->area.height;
485     if (*x + self->frame->area.width - 1 < a->x)
486         *x = a->x;
487     if (*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 (*x < a->x) *x = a->x;
498             if (*x + w > a->x + a->width)
499                 *x = a->x + a->width - w;
500         }
501         if (h <= a->height) {
502             if (*y < a->y) *y = a->y;
503             if (*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             STRUT_SET(self->strut, data[0], data[2], data[1], data[3]);
1351         else
1352             STRUT_SET(self->strut, 0, 0, 0, 0);
1353         g_free(data);
1354     }
1355
1356     /* updating here is pointless while we're being mapped cuz we're not in
1357        the client list yet */
1358     if (self->frame)
1359         screen_update_areas();
1360 }
1361
1362 void client_update_icons(ObClient *self)
1363 {
1364     guint num;
1365     guint32 *data;
1366     guint w, h, i, j;
1367
1368     for (i = 0; i < self->nicons; ++i)
1369         g_free(self->icons[i].data);
1370     if (self->nicons > 0)
1371         g_free(self->icons);
1372     self->nicons = 0;
1373
1374     if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1375         /* figure out how many valid icons are in here */
1376         i = 0;
1377         while (num - i > 2) {
1378             w = data[i++];
1379             h = data[i++];
1380             i += w * h;
1381             if (i > num || w*h == 0) break;
1382             ++self->nicons;
1383         }
1384
1385         self->icons = g_new(ObClientIcon, self->nicons);
1386     
1387         /* store the icons */
1388         i = 0;
1389         for (j = 0; j < self->nicons; ++j) {
1390             guint x, y, t;
1391
1392             w = self->icons[j].width = data[i++];
1393             h = self->icons[j].height = data[i++];
1394
1395             if (w*h == 0) continue;
1396
1397             self->icons[j].data = g_new(RrPixel32, w * h);
1398             for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1399                 if (x >= w) {
1400                     x = 0;
1401                     ++y;
1402                 }
1403                 self->icons[j].data[t] =
1404                     (((data[i] >> 24) & 0xff) << RrDefaultAlphaOffset) +
1405                     (((data[i] >> 16) & 0xff) << RrDefaultRedOffset) +
1406                     (((data[i] >> 8) & 0xff) << RrDefaultGreenOffset) +
1407                     (((data[i] >> 0) & 0xff) << RrDefaultBlueOffset);
1408             }
1409             g_assert(i <= num);
1410         }
1411
1412         g_free(data);
1413     } else if (PROP_GETA32(self->window, kwm_win_icon,
1414                            kwm_win_icon, &data, &num)) {
1415         if (num == 2) {
1416             self->nicons++;
1417             self->icons = g_new(ObClientIcon, self->nicons);
1418             xerror_set_ignore(TRUE);
1419             if (!RrPixmapToRGBA(ob_rr_inst,
1420                                 data[0], data[1],
1421                                 &self->icons[self->nicons-1].width,
1422                                 &self->icons[self->nicons-1].height,
1423                                 &self->icons[self->nicons-1].data)) {
1424                 g_free(&self->icons[self->nicons-1]);
1425                 self->nicons--;
1426             }
1427             xerror_set_ignore(FALSE);
1428         }
1429         g_free(data);
1430     } else {
1431         XWMHints *hints;
1432
1433         if ((hints = XGetWMHints(ob_display, self->window))) {
1434             if (hints->flags & IconPixmapHint) {
1435                 self->nicons++;
1436                 self->icons = g_new(ObClientIcon, self->nicons);
1437                 xerror_set_ignore(TRUE);
1438                 if (!RrPixmapToRGBA(ob_rr_inst,
1439                                     hints->icon_pixmap,
1440                                     (hints->flags & IconMaskHint ?
1441                                      hints->icon_mask : None),
1442                                     &self->icons[self->nicons-1].width,
1443                                     &self->icons[self->nicons-1].height,
1444                                     &self->icons[self->nicons-1].data)){
1445                     g_free(&self->icons[self->nicons-1]);
1446                     self->nicons--;
1447                 }
1448                 xerror_set_ignore(FALSE);
1449             }
1450             XFree(hints);
1451         }
1452     }
1453
1454     if (self->frame)
1455         frame_adjust_icon(self->frame);
1456 }
1457
1458 static void client_change_state(ObClient *self)
1459 {
1460     guint32 state[2];
1461     guint32 netstate[10];
1462     guint num;
1463
1464     state[0] = self->wmstate;
1465     state[1] = None;
1466     PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1467
1468     num = 0;
1469     if (self->modal)
1470         netstate[num++] = prop_atoms.net_wm_state_modal;
1471     if (self->shaded)
1472         netstate[num++] = prop_atoms.net_wm_state_shaded;
1473     if (self->iconic)
1474         netstate[num++] = prop_atoms.net_wm_state_hidden;
1475     if (self->skip_taskbar)
1476         netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1477     if (self->skip_pager)
1478         netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1479     if (self->fullscreen)
1480         netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1481     if (self->max_vert)
1482         netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1483     if (self->max_horz)
1484         netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1485     if (self->above)
1486         netstate[num++] = prop_atoms.net_wm_state_above;
1487     if (self->below)
1488         netstate[num++] = prop_atoms.net_wm_state_below;
1489     PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1490
1491     client_calc_layer(self);
1492
1493     if (self->frame)
1494         frame_adjust_state(self->frame);
1495 }
1496
1497 ObClient *client_search_focus_tree(ObClient *self)
1498 {
1499     GSList *it;
1500     ObClient *ret;
1501
1502     for (it = self->transients; it != NULL; it = it->next) {
1503         if (client_focused(it->data)) return it->data;
1504         if ((ret = client_search_focus_tree(it->data))) return ret;
1505     }
1506     return NULL;
1507 }
1508
1509 ObClient *client_search_focus_tree_full(ObClient *self)
1510 {
1511     if (self->transient_for) {
1512         if (self->transient_for != OB_TRAN_GROUP) {
1513             return client_search_focus_tree_full(self->transient_for);
1514         } else {
1515             GSList *it;
1516             gboolean recursed = FALSE;
1517         
1518             for (it = self->group->members; it; it = it->next)
1519                 if (!((ObClient*)it->data)->transient_for) {
1520                     ObClient *c;
1521                     if ((c = client_search_focus_tree_full(it->data)))
1522                         return c;
1523                     recursed = TRUE;
1524                 }
1525             if (recursed)
1526               return NULL;
1527         }
1528     }
1529
1530     /* this function checks the whole tree, the client_search_focus_tree~
1531        does not, so we need to check this window */
1532     if (client_focused(self))
1533       return self;
1534     return client_search_focus_tree(self);
1535 }
1536
1537 static ObStackingLayer calc_layer(ObClient *self)
1538 {
1539     ObStackingLayer l;
1540
1541     if (self->fullscreen) l = OB_STACKING_LAYER_FULLSCREEN;
1542     else if (self->type == OB_CLIENT_TYPE_DESKTOP)
1543         l = OB_STACKING_LAYER_DESKTOP;
1544     else if (self->type == OB_CLIENT_TYPE_DOCK) {
1545         if (!self->below) l = OB_STACKING_LAYER_TOP;
1546         else l = OB_STACKING_LAYER_NORMAL;
1547     }
1548     else if (self->above) l = OB_STACKING_LAYER_ABOVE;
1549     else if (self->below) l = OB_STACKING_LAYER_BELOW;
1550     else l = OB_STACKING_LAYER_NORMAL;
1551
1552     return l;
1553 }
1554
1555 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
1556                                         ObStackingLayer l, gboolean raised)
1557 {
1558     ObStackingLayer old, own;
1559     GSList *it;
1560
1561     old = self->layer;
1562     own = calc_layer(self);
1563     self->layer = l > own ? l : own;
1564
1565     for (it = self->transients; it; it = it->next)
1566         client_calc_layer_recursive(it->data, orig,
1567                                     l, raised ? raised : l != old);
1568
1569     if (!raised && l != old)
1570         if (orig->frame) { /* only restack if the original window is managed */
1571             /* XXX add_non_intrusive ever? */
1572             stacking_remove(CLIENT_AS_WINDOW(self));
1573             stacking_add(CLIENT_AS_WINDOW(self));
1574         }
1575 }
1576
1577 void client_calc_layer(ObClient *self)
1578 {
1579     ObStackingLayer l;
1580     ObClient *orig;
1581
1582     orig = self;
1583
1584     /* transients take on the layer of their parents */
1585     self = client_search_top_transient(self);
1586
1587     l = calc_layer(self);
1588
1589     client_calc_layer_recursive(self, orig, l, FALSE);
1590 }
1591
1592 gboolean client_should_show(ObClient *self)
1593 {
1594     if (self->iconic) return FALSE;
1595     else if (!(self->desktop == screen_desktop ||
1596                self->desktop == DESKTOP_ALL)) return FALSE;
1597     else if (client_normal(self) && screen_showing_desktop) return FALSE;
1598     
1599     return TRUE;
1600 }
1601
1602 static void client_showhide(ObClient *self)
1603 {
1604
1605     if (client_should_show(self))
1606         frame_show(self->frame);
1607     else
1608         frame_hide(self->frame);
1609 }
1610
1611 gboolean client_normal(ObClient *self) {
1612     return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
1613               self->type == OB_CLIENT_TYPE_DOCK ||
1614               self->type == OB_CLIENT_TYPE_SPLASH);
1615 }
1616
1617 static void client_apply_startup_state(ObClient *self)
1618 {
1619     /* these are in a carefully crafted order.. */
1620
1621     if (self->iconic) {
1622         self->iconic = FALSE;
1623         client_iconify(self, TRUE, FALSE);
1624     }
1625     if (self->fullscreen) {
1626         self->fullscreen = FALSE;
1627         client_fullscreen(self, TRUE, FALSE);
1628     }
1629     if (self->shaded) {
1630         self->shaded = FALSE;
1631         client_shade(self, TRUE);
1632     }
1633     if (self->urgent)
1634         dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1635   
1636     if (self->max_vert && self->max_horz) {
1637         self->max_vert = self->max_horz = FALSE;
1638         client_maximize(self, TRUE, 0, FALSE);
1639     } else if (self->max_vert) {
1640         self->max_vert = FALSE;
1641         client_maximize(self, TRUE, 2, FALSE);
1642     } else if (self->max_horz) {
1643         self->max_horz = FALSE;
1644         client_maximize(self, TRUE, 1, FALSE);
1645     }
1646
1647     /* nothing to do for the other states:
1648        skip_taskbar
1649        skip_pager
1650        modal
1651        above
1652        below
1653     */
1654 }
1655
1656 void client_configure(ObClient *self, ObCorner anchor,
1657                       int x, int y, int w, int h,
1658                       gboolean user, gboolean final)
1659 {
1660     gboolean moved = FALSE, resized = FALSE;
1661
1662     /* gets the frame's position */
1663     frame_client_gravity(self->frame, &x, &y);
1664
1665     /* these positions are frame positions, not client positions */
1666
1667     /* set the size and position if fullscreen */
1668     if (self->fullscreen) {
1669 #ifdef VIDMODE
1670         int dot;
1671         XF86VidModeModeLine mode;
1672 #endif
1673         Rect *a;
1674         guint i;
1675
1676         i = client_monitor(self);
1677         a = screen_physical_area_monitor(i);
1678
1679 #ifdef VIDMODE
1680         if (i == 0 && /* primary head */
1681             extensions_vidmode &&
1682             XF86VidModeGetViewPort(ob_display, ob_screen, &x, &y) &&
1683             /* get the mode last so the mode.privsize isnt freed incorrectly */
1684             XF86VidModeGetModeLine(ob_display, ob_screen, &dot, &mode)) {
1685             x += a->x;
1686             y += a->y;
1687             w = mode.hdisplay;
1688             h = mode.vdisplay;
1689             if (mode.privsize) XFree(mode.private);
1690         } else
1691 #endif
1692         {
1693             x = a->x;
1694             y = a->y;
1695             w = a->width;
1696             h = a->height;
1697         }
1698
1699         user = FALSE; /* ignore that increment etc shit when in fullscreen */
1700     } else {
1701         Rect *a;
1702
1703         a = screen_area_monitor(self->desktop, client_monitor(self));
1704
1705         /* set the size and position if maximized */
1706         if (self->max_horz) {
1707             x = a->x - self->frame->size.left;
1708             w = a->width;
1709         }
1710         if (self->max_vert) {
1711             y = a->y;
1712             h = a->height - self->frame->size.top - self->frame->size.bottom;
1713         }
1714     }
1715
1716     /* gets the client's position */
1717     frame_frame_gravity(self->frame, &x, &y);
1718
1719     /* these override the above states! if you cant move you can't move! */
1720     if (user) {
1721         if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
1722             x = self->area.x;
1723             y = self->area.y;
1724         }
1725         if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
1726             w = self->area.width;
1727             h = self->area.height;
1728         }
1729     }
1730
1731     if (!(w == self->area.width && h == self->area.height)) {
1732         int basew, baseh, minw, minh;
1733
1734         /* base size is substituted with min size if not specified */
1735         if (self->base_size.width || self->base_size.height) {
1736             basew = self->base_size.width;
1737             baseh = self->base_size.height;
1738         } else {
1739             basew = self->min_size.width;
1740             baseh = self->min_size.height;
1741         }
1742         /* min size is substituted with base size if not specified */
1743         if (self->min_size.width || self->min_size.height) {
1744             minw = self->min_size.width;
1745             minh = self->min_size.height;
1746         } else {
1747             minw = self->base_size.width;
1748             minh = self->base_size.height;
1749         }
1750
1751         if (user) {
1752             /* for interactive resizing. have to move half an increment in each
1753                direction. */
1754
1755             /* how far we are towards the next size inc */
1756             int mw = (w - basew) % self->size_inc.width; 
1757             int mh = (h - baseh) % self->size_inc.height;
1758             /* amount to add */
1759             int aw = self->size_inc.width / 2;
1760             int ah = self->size_inc.height / 2;
1761             /* don't let us move into a new size increment */
1762             if (mw + aw >= self->size_inc.width)
1763                 aw = self->size_inc.width - mw - 1;
1764             if (mh + ah >= self->size_inc.height)
1765                 ah = self->size_inc.height - mh - 1;
1766             w += aw;
1767             h += ah;
1768     
1769             /* if this is a user-requested resize, then check against min/max
1770                sizes */
1771
1772             /* smaller than min size or bigger than max size? */
1773             if (w > self->max_size.width) w = self->max_size.width;
1774             if (w < minw) w = minw;
1775             if (h > self->max_size.height) h = self->max_size.height;
1776             if (h < minh) h = minh;
1777         }
1778
1779         w -= basew;
1780         h -= baseh;
1781
1782         /* keep to the increments */
1783         w /= self->size_inc.width;
1784         h /= self->size_inc.height;
1785
1786         /* you cannot resize to nothing */
1787         if (w < 1) w = 1;
1788         if (h < 1) h = 1;
1789   
1790         /* store the logical size */
1791         SIZE_SET(self->logical_size, w, h);
1792
1793         w *= self->size_inc.width;
1794         h *= self->size_inc.height;
1795
1796         w += basew;
1797         h += baseh;
1798
1799         if (user) {
1800             /* adjust the height to match the width for the aspect ratios.
1801              for this, min size is not substituted for base size ever. */
1802             w -= self->base_size.width;
1803             h -= self->base_size.height;
1804
1805             if (self->min_ratio)
1806                 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1807             if (self->max_ratio)
1808                 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1809
1810             w += self->base_size.width;
1811             h += self->base_size.height;
1812         }
1813     }
1814
1815     switch (anchor) {
1816     case OB_CORNER_TOPLEFT:
1817         break;
1818     case OB_CORNER_TOPRIGHT:
1819         x -= w - self->area.width;
1820         break;
1821     case OB_CORNER_BOTTOMLEFT:
1822         y -= h - self->area.height;
1823         break;
1824     case OB_CORNER_BOTTOMRIGHT:
1825         x -= w - self->area.width;
1826         y -= h - self->area.height;
1827         break;
1828     }
1829
1830     moved = x != self->area.x || y != self->area.y;
1831     resized = w != self->area.width || h != self->area.height;
1832
1833     RECT_SET(self->area, x, y, w, h);
1834
1835     /* for app-requested resizes, always resize if 'resized' is true.
1836        for user-requested ones, only resize if final is true, or when
1837        resizing in opaque mode */
1838     if ((!user && resized) ||
1839         (user && (final || (resized && config_opaque_resize))))
1840         XResizeWindow(ob_display, self->window, w, h);
1841
1842     /* move/resize the frame to match the request */
1843     if (self->frame) {
1844         if (self->decorations != self->frame->decorations)
1845             moved = resized = TRUE;
1846
1847         if (moved || resized)
1848             frame_adjust_area(self->frame, moved, resized);
1849
1850         /* If you send this and the client hasn't changed you end up with buggy
1851            clients (emacs) freaking out, cuz they send back a configure every
1852            time they receive this event, which resends them this event... etc.
1853         */
1854         if ((!user && moved) || (user && final)) {
1855             XEvent event;
1856             event.type = ConfigureNotify;
1857             event.xconfigure.display = ob_display;
1858             event.xconfigure.event = self->window;
1859             event.xconfigure.window = self->window;
1860     
1861             /* root window real coords */
1862             event.xconfigure.x = self->frame->area.x + self->frame->size.left;
1863             event.xconfigure.y = self->frame->area.y + self->frame->size.top;
1864     
1865             event.xconfigure.width = w;
1866             event.xconfigure.height = h;
1867             event.xconfigure.border_width = 0;
1868             event.xconfigure.above = self->frame->plate;
1869             event.xconfigure.override_redirect = FALSE;
1870             XSendEvent(event.xconfigure.display, event.xconfigure.window,
1871                        FALSE, StructureNotifyMask, &event);
1872         }
1873     }
1874 }
1875
1876 void client_fullscreen(ObClient *self, gboolean fs, gboolean savearea)
1877 {
1878     int x, y, w, h;
1879
1880     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
1881         self->fullscreen == fs) return;                   /* already done */
1882
1883     self->fullscreen = fs;
1884     client_change_state(self); /* change the state hints on the client,
1885                                   and adjust out layer/stacking */
1886
1887     if (fs) {
1888         if (savearea) {
1889             guint32 dimensions[4];
1890             dimensions[0] = self->area.x;
1891             dimensions[1] = self->area.y;
1892             dimensions[2] = self->area.width;
1893             dimensions[3] = self->area.height;
1894   
1895             PROP_SETA32(self->window, openbox_premax, cardinal,
1896                         dimensions, 4);
1897         }
1898
1899         /* these are not actually used cuz client_configure will set them
1900            as appropriate when the window is fullscreened */
1901         x = y = w = h = 0;
1902     } else {
1903         guint num;
1904         gint32 *dimensions;
1905         Rect *a;
1906
1907         /* pick some fallbacks... */
1908         a = screen_area_monitor(self->desktop, 0);
1909         x = a->x + a->width / 4;
1910         y = a->y + a->height / 4;
1911         w = a->width / 2;
1912         h = a->height / 2;
1913
1914         if (PROP_GETA32(self->window, openbox_premax, cardinal,
1915                         (guint32**)&dimensions, &num)) {
1916             if (num == 4) {
1917                 x = dimensions[0];
1918                 y = dimensions[1];
1919                 w = dimensions[2];
1920                 h = dimensions[3];
1921             }
1922             g_free(dimensions);
1923         }
1924     }
1925
1926     client_setup_decor_and_functions(self);
1927
1928     client_configure(self, OB_CORNER_TOPLEFT, x, y, w, h, TRUE, TRUE);
1929
1930     /* try focus us when we go into fullscreen mode */
1931     client_focus(self);
1932 }
1933
1934 static void client_iconify_recursive(ObClient *self,
1935                                      gboolean iconic, gboolean curdesk)
1936 {
1937     GSList *it;
1938     gboolean changed = FALSE;
1939
1940
1941     if (self->iconic != iconic) {
1942         ob_debug("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"),
1943                  self->window);
1944
1945         self->iconic = iconic;
1946
1947         if (iconic) {
1948             if (self->functions & OB_CLIENT_FUNC_ICONIFY) {
1949                 self->wmstate = IconicState;
1950                 self->ignore_unmaps++;
1951                 /* we unmap the client itself so that we can get MapRequest
1952                    events, and because the ICCCM tells us to! */
1953                 XUnmapWindow(ob_display, self->window);
1954
1955                 /* update the focus lists.. iconic windows go to the bottom of
1956                    the list, put the new iconic window at the 'top of the
1957                    bottom'. */
1958                 focus_order_to_top(self);
1959
1960                 changed = TRUE;
1961             }
1962         } else {
1963             if (curdesk)
1964                 client_set_desktop(self, screen_desktop, FALSE);
1965             self->wmstate = self->shaded ? IconicState : NormalState;
1966             XMapWindow(ob_display, self->window);
1967
1968             /* this puts it after the current focused window */
1969             focus_order_remove(self);
1970             focus_order_add_new(self);
1971
1972             /* this is here cuz with the VIDMODE extension, the viewport can
1973                change while a fullscreen window is iconic, and when it
1974                uniconifies, it would be nice if it did so to the new position
1975                of the viewport */
1976             client_reconfigure(self);
1977
1978             changed = TRUE;
1979         }
1980     }
1981
1982     if (changed) {
1983         client_change_state(self);
1984         client_showhide(self);
1985         screen_update_areas();
1986
1987         dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
1988                         self, 0, 0);
1989     }
1990
1991     /* iconify all transients */
1992     for (it = self->transients; it != NULL; it = it->next)
1993         if (it->data != self) client_iconify_recursive(it->data,
1994                                                        iconic, curdesk);
1995 }
1996
1997 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk)
1998 {
1999     /* move up the transient chain as far as possible first */
2000     self = client_search_top_transient(self);
2001
2002     client_iconify_recursive(client_search_top_transient(self),
2003                              iconic, curdesk);
2004 }
2005
2006 void client_maximize(ObClient *self, gboolean max, int dir, gboolean savearea)
2007 {
2008     int x, y, w, h;
2009      
2010     g_assert(dir == 0 || dir == 1 || dir == 2);
2011     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE)) return; /* can't */
2012
2013     /* check if already done */
2014     if (max) {
2015         if (dir == 0 && self->max_horz && self->max_vert) return;
2016         if (dir == 1 && self->max_horz) return;
2017         if (dir == 2 && self->max_vert) return;
2018     } else {
2019         if (dir == 0 && !self->max_horz && !self->max_vert) return;
2020         if (dir == 1 && !self->max_horz) return;
2021         if (dir == 2 && !self->max_vert) return;
2022     }
2023
2024     /* work with the frame's coords */
2025     x = self->frame->area.x;
2026     y = self->frame->area.y;
2027     w = self->area.width;
2028     h = self->area.height;
2029
2030     if (max) {
2031         if (savearea) {
2032             gint32 dimensions[4];
2033             gint32 *readdim;
2034             guint num;
2035
2036             dimensions[0] = x;
2037             dimensions[1] = y;
2038             dimensions[2] = w;
2039             dimensions[3] = h;
2040
2041             /* get the property off the window and use it for the dimensions
2042                we are already maxed on */
2043             if (PROP_GETA32(self->window, openbox_premax, cardinal,
2044                             (guint32**)&readdim, &num)) {
2045                 if (num == 4) {
2046                     if (self->max_horz) {
2047                         dimensions[0] = readdim[0];
2048                         dimensions[2] = readdim[2];
2049                     }
2050                     if (self->max_vert) {
2051                         dimensions[1] = readdim[1];
2052                         dimensions[3] = readdim[3];
2053                     }
2054                 }
2055                 g_free(readdim);
2056             }
2057
2058             PROP_SETA32(self->window, openbox_premax, cardinal,
2059                         (guint32*)dimensions, 4);
2060         }
2061     } else {
2062         guint num;
2063         gint32 *dimensions;
2064         Rect *a;
2065
2066         /* pick some fallbacks... */
2067         a = screen_area_monitor(self->desktop, 0);
2068         if (dir == 0 || dir == 1) { /* horz */
2069             x = a->x + a->width / 4;
2070             w = a->width / 2;
2071         }
2072         if (dir == 0 || dir == 2) { /* vert */
2073             y = a->y + a->height / 4;
2074             h = a->height / 2;
2075         }
2076
2077         if (PROP_GETA32(self->window, openbox_premax, cardinal,
2078                         (guint32**)&dimensions, &num)) {
2079             if (num == 4) {
2080                 if (dir == 0 || dir == 1) { /* horz */
2081                     x = dimensions[0];
2082                     w = dimensions[2];
2083                 }
2084                 if (dir == 0 || dir == 2) { /* vert */
2085                     y = dimensions[1];
2086                     h = dimensions[3];
2087                 }
2088             }
2089             g_free(dimensions);
2090         }
2091     }
2092
2093     if (dir == 0 || dir == 1) /* horz */
2094         self->max_horz = max;
2095     if (dir == 0 || dir == 2) /* vert */
2096         self->max_vert = max;
2097
2098     if (!self->max_horz && !self->max_vert)
2099         PROP_ERASE(self->window, openbox_premax);
2100
2101     client_change_state(self); /* change the state hints on the client */
2102
2103     /* figure out where the client should be going */
2104     frame_frame_gravity(self->frame, &x, &y);
2105     client_configure(self, OB_CORNER_TOPLEFT, x, y, w, h, TRUE, TRUE);
2106 }
2107
2108 void client_shade(ObClient *self, gboolean shade)
2109 {
2110     if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
2111          shade) ||                         /* can't shade */
2112         self->shaded == shade) return;     /* already done */
2113
2114     /* when we're iconic, don't change the wmstate */
2115     if (!self->iconic)
2116         self->wmstate = shade ? IconicState : NormalState;
2117     self->shaded = shade;
2118     client_change_state(self);
2119     /* resize the frame to just the titlebar */
2120     frame_adjust_area(self->frame, FALSE, FALSE);
2121 }
2122
2123 void client_close(ObClient *self)
2124 {
2125     XEvent ce;
2126
2127     if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
2128
2129     /*
2130       XXX: itd be cool to do timeouts and shit here for killing the client's
2131       process off
2132       like... if the window is around after 5 seconds, then the close button
2133       turns a nice red, and if this function is called again, the client is
2134       explicitly killed.
2135     */
2136
2137     ce.xclient.type = ClientMessage;
2138     ce.xclient.message_type =  prop_atoms.wm_protocols;
2139     ce.xclient.display = ob_display;
2140     ce.xclient.window = self->window;
2141     ce.xclient.format = 32;
2142     ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
2143     ce.xclient.data.l[1] = event_lasttime;
2144     ce.xclient.data.l[2] = 0l;
2145     ce.xclient.data.l[3] = 0l;
2146     ce.xclient.data.l[4] = 0l;
2147     XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2148 }
2149
2150 void client_kill(ObClient *self)
2151 {
2152     XKillClient(ob_display, self->window);
2153 }
2154
2155 void client_set_desktop_recursive(ObClient *self,
2156                                   guint target, gboolean donthide)
2157 {
2158     guint old;
2159     GSList *it;
2160
2161     if (target != self->desktop) {
2162
2163         ob_debug("Setting desktop %u\n", target+1);
2164
2165         g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2166
2167         /* remove from the old desktop(s) */
2168         focus_order_remove(self);
2169
2170         old = self->desktop;
2171         self->desktop = target;
2172         PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2173         /* the frame can display the current desktop state */
2174         frame_adjust_state(self->frame);
2175         /* 'move' the window to the new desktop */
2176         if (!donthide)
2177             client_showhide(self);
2178         /* raise if it was not already on the desktop */
2179         if (old != DESKTOP_ALL)
2180             stacking_raise(CLIENT_AS_WINDOW(self));
2181         screen_update_areas();
2182
2183         /* add to the new desktop(s) */
2184         if (config_focus_new)
2185             focus_order_to_top(self);
2186         else
2187             focus_order_to_bottom(self);
2188
2189         dispatch_client(Event_Client_Desktop, self, target, old);
2190     }
2191
2192     /* move all transients */
2193     for (it = self->transients; it != NULL; it = it->next)
2194         if (it->data != self) client_set_desktop_recursive(it->data,
2195                                                            target, donthide);
2196 }
2197
2198 void client_set_desktop(ObClient *self, guint target, gboolean donthide)
2199 {
2200     client_set_desktop_recursive(client_search_top_transient(self),
2201                                  target, donthide);
2202 }
2203
2204 ObClient *client_search_modal_child(ObClient *self)
2205 {
2206     GSList *it;
2207     ObClient *ret;
2208   
2209     for (it = self->transients; it != NULL; it = it->next) {
2210         ObClient *c = it->data;
2211         if ((ret = client_search_modal_child(c))) return ret;
2212         if (c->modal) return c;
2213     }
2214     return NULL;
2215 }
2216
2217 gboolean client_validate(ObClient *self)
2218 {
2219     XEvent e; 
2220
2221     XSync(ob_display, FALSE); /* get all events on the server */
2222
2223     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2224         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2225         XPutBackEvent(ob_display, &e);
2226         return FALSE;
2227     }
2228
2229     return TRUE;
2230 }
2231
2232 void client_set_wm_state(ObClient *self, long state)
2233 {
2234     if (state == self->wmstate) return; /* no change */
2235   
2236     switch (state) {
2237     case IconicState:
2238         client_iconify(self, TRUE, TRUE);
2239         break;
2240     case NormalState:
2241         client_iconify(self, FALSE, TRUE);
2242         break;
2243     }
2244 }
2245
2246 void client_set_state(ObClient *self, Atom action, long data1, long data2)
2247 {
2248     gboolean shaded = self->shaded;
2249     gboolean fullscreen = self->fullscreen;
2250     gboolean max_horz = self->max_horz;
2251     gboolean max_vert = self->max_vert;
2252     int i;
2253
2254     if (!(action == prop_atoms.net_wm_state_add ||
2255           action == prop_atoms.net_wm_state_remove ||
2256           action == prop_atoms.net_wm_state_toggle))
2257         /* an invalid action was passed to the client message, ignore it */
2258         return; 
2259
2260     for (i = 0; i < 2; ++i) {
2261         Atom state = i == 0 ? data1 : data2;
2262     
2263         if (!state) continue;
2264
2265         /* if toggling, then pick whether we're adding or removing */
2266         if (action == prop_atoms.net_wm_state_toggle) {
2267             if (state == prop_atoms.net_wm_state_modal)
2268                 action = self->modal ? prop_atoms.net_wm_state_remove :
2269                     prop_atoms.net_wm_state_add;
2270             else if (state == prop_atoms.net_wm_state_maximized_vert)
2271                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2272                     prop_atoms.net_wm_state_add;
2273             else if (state == prop_atoms.net_wm_state_maximized_horz)
2274                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2275                     prop_atoms.net_wm_state_add;
2276             else if (state == prop_atoms.net_wm_state_shaded)
2277                 action = self->shaded ? prop_atoms.net_wm_state_remove :
2278                     prop_atoms.net_wm_state_add;
2279             else if (state == prop_atoms.net_wm_state_skip_taskbar)
2280                 action = self->skip_taskbar ?
2281                     prop_atoms.net_wm_state_remove :
2282                     prop_atoms.net_wm_state_add;
2283             else if (state == prop_atoms.net_wm_state_skip_pager)
2284                 action = self->skip_pager ?
2285                     prop_atoms.net_wm_state_remove :
2286                     prop_atoms.net_wm_state_add;
2287             else if (state == prop_atoms.net_wm_state_fullscreen)
2288                 action = self->fullscreen ?
2289                     prop_atoms.net_wm_state_remove :
2290                     prop_atoms.net_wm_state_add;
2291             else if (state == prop_atoms.net_wm_state_above)
2292                 action = self->above ? prop_atoms.net_wm_state_remove :
2293                     prop_atoms.net_wm_state_add;
2294             else if (state == prop_atoms.net_wm_state_below)
2295                 action = self->below ? prop_atoms.net_wm_state_remove :
2296                     prop_atoms.net_wm_state_add;
2297         }
2298     
2299         if (action == prop_atoms.net_wm_state_add) {
2300             if (state == prop_atoms.net_wm_state_modal) {
2301                 /* XXX raise here or something? */
2302                 self->modal = TRUE;
2303             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2304                 max_vert = TRUE;
2305             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2306                 max_horz = TRUE;
2307             } else if (state == prop_atoms.net_wm_state_shaded) {
2308                 shaded = TRUE;
2309             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2310                 self->skip_taskbar = TRUE;
2311             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2312                 self->skip_pager = TRUE;
2313             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2314                 fullscreen = TRUE;
2315             } else if (state == prop_atoms.net_wm_state_above) {
2316                 self->above = TRUE;
2317             } else if (state == prop_atoms.net_wm_state_below) {
2318                 self->below = TRUE;
2319             }
2320
2321         } else { /* action == prop_atoms.net_wm_state_remove */
2322             if (state == prop_atoms.net_wm_state_modal) {
2323                 self->modal = FALSE;
2324             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2325                 max_vert = FALSE;
2326             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2327                 max_horz = FALSE;
2328             } else if (state == prop_atoms.net_wm_state_shaded) {
2329                 shaded = FALSE;
2330             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2331                 self->skip_taskbar = FALSE;
2332             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2333                 self->skip_pager = FALSE;
2334             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2335                 fullscreen = FALSE;
2336             } else if (state == prop_atoms.net_wm_state_above) {
2337                 self->above = FALSE;
2338             } else if (state == prop_atoms.net_wm_state_below) {
2339                 self->below = FALSE;
2340             }
2341         }
2342     }
2343     if (max_horz != self->max_horz || max_vert != self->max_vert) {
2344         if (max_horz != self->max_horz && max_vert != self->max_vert) {
2345             /* toggling both */
2346             if (max_horz == max_vert) { /* both going the same way */
2347                 client_maximize(self, max_horz, 0, TRUE);
2348             } else {
2349                 client_maximize(self, max_horz, 1, TRUE);
2350                 client_maximize(self, max_vert, 2, TRUE);
2351             }
2352         } else {
2353             /* toggling one */
2354             if (max_horz != self->max_horz)
2355                 client_maximize(self, max_horz, 1, TRUE);
2356             else
2357                 client_maximize(self, max_vert, 2, TRUE);
2358         }
2359     }
2360     /* change fullscreen state before shading, as it will affect if the window
2361        can shade or not */
2362     if (fullscreen != self->fullscreen)
2363         client_fullscreen(self, fullscreen, TRUE);
2364     if (shaded != self->shaded)
2365         client_shade(self, shaded);
2366     client_calc_layer(self);
2367     client_change_state(self); /* change the hint to reflect these changes */
2368 }
2369
2370 ObClient *client_focus_target(ObClient *self)
2371 {
2372     ObClient *child;
2373      
2374     /* if we have a modal child, then focus it, not us */
2375     child = client_search_modal_child(self);
2376     if (child) return child;
2377     return self;
2378 }
2379
2380 gboolean client_can_focus(ObClient *self)
2381 {
2382     XEvent ev;
2383
2384     /* choose the correct target */
2385     self = client_focus_target(self);
2386
2387     if (!self->frame->visible)
2388         return FALSE;
2389
2390     if (!((self->can_focus || self->focus_notify) &&
2391           (self->desktop == screen_desktop ||
2392            self->desktop == DESKTOP_ALL) &&
2393           !self->iconic))
2394         return FALSE;
2395
2396     /* do a check to see if the window has already been unmapped or destroyed
2397        do this intelligently while watching out for unmaps we've generated
2398        (ignore_unmaps > 0) */
2399     if (XCheckTypedWindowEvent(ob_display, self->window,
2400                                DestroyNotify, &ev)) {
2401         XPutBackEvent(ob_display, &ev);
2402         return FALSE;
2403     }
2404     while (XCheckTypedWindowEvent(ob_display, self->window,
2405                                   UnmapNotify, &ev)) {
2406         if (self->ignore_unmaps) {
2407             self->ignore_unmaps--;
2408         } else {
2409             XPutBackEvent(ob_display, &ev);
2410             return FALSE;
2411         }
2412     }
2413
2414     return TRUE;
2415 }
2416
2417 gboolean client_focus(ObClient *self)
2418 {
2419     /* choose the correct target */
2420     self = client_focus_target(self);
2421
2422     if (!client_can_focus(self)) {
2423         if (!self->frame->visible) {
2424             /* update the focus lists */
2425             focus_order_to_top(self);
2426         }
2427         return FALSE;
2428     }
2429
2430     if (self->can_focus)
2431         /* RevertToPointerRoot causes much more headache than RevertToNone, so
2432            I choose to use it always, hopefully to find errors quicker, if any
2433            are left. (I hate X. I hate focus events.) */
2434         XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2435                        event_lasttime);
2436
2437     if (self->focus_notify) {
2438         XEvent ce;
2439         ce.xclient.type = ClientMessage;
2440         ce.xclient.message_type = prop_atoms.wm_protocols;
2441         ce.xclient.display = ob_display;
2442         ce.xclient.window = self->window;
2443         ce.xclient.format = 32;
2444         ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2445         ce.xclient.data.l[1] = event_lasttime;
2446         ce.xclient.data.l[2] = 0l;
2447         ce.xclient.data.l[3] = 0l;
2448         ce.xclient.data.l[4] = 0l;
2449         XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2450     }
2451
2452 #ifdef DEBUG_FOCUS
2453     ob_debug("%sively focusing %lx at %d\n",
2454              (self->can_focus ? "act" : "pass"),
2455              self->window, (int) event_lasttime);
2456 #endif
2457
2458     /* Cause the FocusIn to come back to us. Important for desktop switches,
2459        since otherwise we'll have no FocusIn on the queue and send it off to
2460        the focus_backup. */
2461     XSync(ob_display, FALSE);
2462     return TRUE;
2463 }
2464
2465 void client_unfocus(ObClient *self)
2466 {
2467     g_assert(focus_client == self);
2468 #ifdef DEBUG_FOCUS
2469     ob_debug("client_unfocus for %lx\n", self->window);
2470 #endif
2471     focus_fallback(OB_FOCUS_FALLBACK_UNFOCUSING);
2472 }
2473
2474 void client_activate(ObClient *self)
2475 {
2476     if (client_normal(self) && screen_showing_desktop)
2477         screen_show_desktop(FALSE);
2478     if (self->iconic)
2479         client_iconify(self, FALSE, FALSE);
2480     if (self->desktop != DESKTOP_ALL &&
2481         self->desktop != screen_desktop)
2482         screen_set_desktop(self->desktop);
2483     else if (!self->frame->visible)
2484         /* if its not visible for other reasons, then don't mess
2485            with it */
2486         return;
2487     if (self->shaded)
2488         client_shade(self, FALSE);
2489     client_focus(self);
2490     stacking_raise(CLIENT_AS_WINDOW(self));
2491 }
2492
2493 gboolean client_focused(ObClient *self)
2494 {
2495     return self == focus_client;
2496 }
2497
2498 ObClientIcon *client_icon(ObClient *self, int w, int h)
2499 {
2500     guint i;
2501     /* si is the smallest image >= req */
2502     /* li is the largest image < req */
2503     unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2504
2505     if (!self->nicons) return NULL;
2506
2507     for (i = 0; i < self->nicons; ++i) {
2508         size = self->icons[i].width * self->icons[i].height;
2509         if (size < smallest && size >= (unsigned)(w * h)) {
2510             smallest = size;
2511             si = i;
2512         }
2513         if (size > largest && size <= (unsigned)(w * h)) {
2514             largest = size;
2515             li = i;
2516         }
2517     }
2518     if (largest == 0) /* didnt find one smaller than the requested size */
2519         return &self->icons[si];
2520     return &self->icons[li];
2521 }
2522
2523 /* this be mostly ripped from fvwm */
2524 ObClient *client_find_directional(ObClient *c, ObDirection dir) 
2525 {
2526     int my_cx, my_cy, his_cx, his_cy;
2527     int offset = 0;
2528     int distance = 0;
2529     int score, best_score;
2530     ObClient *best_client, *cur;
2531     GList *it;
2532
2533     if(!client_list)
2534         return NULL;
2535
2536     /* first, find the centre coords of the currently focused window */
2537     my_cx = c->frame->area.x + c->frame->area.width / 2;
2538     my_cy = c->frame->area.y + c->frame->area.height / 2;
2539
2540     best_score = -1;
2541     best_client = NULL;
2542
2543     for(it = g_list_first(client_list); it; it = it->next) {
2544         cur = it->data;
2545
2546         /* the currently selected window isn't interesting */
2547         if(cur == c)
2548             continue;
2549         if (!client_normal(cur))
2550             continue;
2551         if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2552             continue;
2553         if(cur->iconic)
2554             continue;
2555         if(client_focus_target(cur) == cur &&
2556            !(cur->can_focus || cur->focus_notify))
2557             continue;
2558
2559         /* find the centre coords of this window, from the
2560          * currently focused window's point of view */
2561         his_cx = (cur->frame->area.x - my_cx)
2562             + cur->frame->area.width / 2;
2563         his_cy = (cur->frame->area.y - my_cy)
2564             + cur->frame->area.height / 2;
2565
2566         if(dir > 3) { 
2567             int tx;
2568             /* Rotate the diagonals 45 degrees counterclockwise.
2569              * To do this, multiply the matrix /+h +h\ with the
2570              * vector (x y).                   \-h +h/
2571              * h = sqrt(0.5). We can set h := 1 since absolute
2572              * distance doesn't matter here. */
2573             tx = his_cx + his_cy;
2574             his_cy = -his_cx + his_cy;
2575             his_cx = tx;
2576         }
2577
2578         switch(dir) {
2579         case OB_DIRECTION_NORTH:
2580         case OB_DIRECTION_SOUTH:
2581         case OB_DIRECTION_NORTHEAST:
2582         case OB_DIRECTION_SOUTHWEST:
2583             offset = (his_cx < 0) ? -his_cx : his_cx;
2584             distance = ((dir == OB_DIRECTION_NORTH ||
2585                         dir == OB_DIRECTION_NORTHEAST) ?
2586                         -his_cy : his_cy);
2587             break;
2588         case OB_DIRECTION_EAST:
2589         case OB_DIRECTION_WEST:
2590         case OB_DIRECTION_SOUTHEAST:
2591         case OB_DIRECTION_NORTHWEST:
2592             offset = (his_cy < 0) ? -his_cy : his_cy;
2593             distance = ((dir == OB_DIRECTION_WEST ||
2594                         dir == OB_DIRECTION_NORTHWEST) ?
2595                         -his_cx : his_cx);
2596             break;
2597         }
2598
2599         /* the target must be in the requested direction */
2600         if(distance <= 0)
2601             continue;
2602
2603         /* Calculate score for this window.  The smaller the better. */
2604         score = distance + offset;
2605
2606         /* windows more than 45 degrees off the direction are
2607          * heavily penalized and will only be chosen if nothing
2608          * else within a million pixels */
2609         if(offset > distance)
2610             score += 1000000;
2611
2612         if(best_score == -1 || score < best_score)
2613             best_client = cur,
2614                 best_score = score;
2615     }
2616
2617     return best_client;
2618 }
2619
2620 void client_set_layer(ObClient *self, int layer)
2621 {
2622     if (layer < 0) {
2623         self->below = TRUE;
2624         self->above = FALSE;
2625     } else if (layer == 0) {
2626         self->below = self->above = FALSE;
2627     } else {
2628         self->below = FALSE;
2629         self->above = TRUE;
2630     }
2631     client_calc_layer(self);
2632     client_change_state(self); /* reflect this in the state hints */
2633 }
2634
2635 guint client_monitor(ObClient *self)
2636 {
2637     guint i;
2638
2639     for (i = 0; i < screen_num_monitors; ++i) {
2640         Rect *area = screen_physical_area_monitor(i);
2641         if (RECT_INTERSECTS_RECT(*area, self->frame->area))
2642             break;
2643     }
2644     if (i == screen_num_monitors) i = 0;
2645     g_assert(i < screen_num_monitors);
2646     return i;
2647 }
2648
2649 ObClient *client_search_top_transient(ObClient *self)
2650 {
2651     /* move up the transient chain as far as possible */
2652     if (self->transient_for) {
2653         if (self->transient_for != OB_TRAN_GROUP) {
2654             return client_search_top_transient(self->transient_for);
2655         } else {
2656             GSList *it;
2657
2658             for (it = self->group->members; it; it = it->next) {
2659                 ObClient *c = it->data;
2660
2661                 /* checking transient_for prevents infinate loops! */
2662                 if (c != self && !c->transient_for)
2663                     break;
2664             }
2665             if (it)
2666                 return it->data;
2667         }
2668     }
2669
2670     return self;
2671 }
2672
2673 ObClient *client_search_transient(ObClient *self, ObClient *search)
2674 {
2675     GSList *sit;
2676
2677     for (sit = self->transients; sit; sit = g_slist_next(sit)) {
2678         if (sit->data == search)
2679             return search;
2680         if (client_search_transient(sit->data, search))
2681             return search;
2682     }
2683     return NULL;
2684 }