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