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