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