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