]> icculus.org git repositories - dana/openbox.git/blob - openbox/client.c
set the desktop hint if the desktop is changed by the session code
[dana/openbox.git] / openbox / client.c
1 #include "debug.h"
2 #include "client.h"
3 #include "dock.h"
4 #include "xerror.h"
5 #include "startup.h"
6 #include "screen.h"
7 #include "moveresize.h"
8 #include "prop.h"
9 #include "extensions.h"
10 #include "frame.h"
11 #include "session.h"
12 #include "event.h"
13 #include "grab.h"
14 #include "focus.h"
15 #include "stacking.h"
16 #include "dispatch.h"
17 #include "openbox.h"
18 #include "group.h"
19 #include "config.h"
20 #include "menu.h"
21 #include "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         /* don't let apps tell me where to put transient windows, but only if
961            they have a valid parent */
962         self->positioned = !!(size.flags & (PPosition|USPosition)) &&
963             !self->transient_for;
964
965         if (size.flags & PWinGravity) {
966             self->gravity = size.win_gravity;
967       
968             /* if the client has a frame, i.e. has already been mapped and
969                is changing its gravity */
970             if (self->frame && self->gravity != oldgravity) {
971                 /* move our idea of the client's position based on its new
972                    gravity */
973                 self->area.x = self->frame->area.x;
974                 self->area.y = self->frame->area.y;
975                 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
976             }
977         }
978
979         if (size.flags & PAspect) {
980             if (size.min_aspect.y)
981                 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
982             if (size.max_aspect.y)
983                 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
984         }
985
986         if (size.flags & PMinSize)
987             SIZE_SET(self->min_size, size.min_width, size.min_height);
988     
989         if (size.flags & PMaxSize)
990             SIZE_SET(self->max_size, size.max_width, size.max_height);
991     
992         if (size.flags & PBaseSize)
993             SIZE_SET(self->base_size, size.base_width, size.base_height);
994     
995         if (size.flags & PResizeInc)
996             SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
997     }
998 }
999
1000 void client_setup_decor_and_functions(ObClient *self)
1001 {
1002     /* start with everything (cept fullscreen) */
1003     self->decorations = (OB_FRAME_DECOR_TITLEBAR |
1004                          OB_FRAME_DECOR_HANDLE |
1005                          OB_FRAME_DECOR_GRIPS |
1006                          OB_FRAME_DECOR_BORDER |
1007                          OB_FRAME_DECOR_ICON |
1008                          OB_FRAME_DECOR_ALLDESKTOPS |
1009                          OB_FRAME_DECOR_ICONIFY |
1010                          OB_FRAME_DECOR_MAXIMIZE |
1011                          OB_FRAME_DECOR_SHADE);
1012     self->functions = (OB_CLIENT_FUNC_RESIZE |
1013                        OB_CLIENT_FUNC_MOVE |
1014                        OB_CLIENT_FUNC_ICONIFY |
1015                        OB_CLIENT_FUNC_MAXIMIZE |
1016                        OB_CLIENT_FUNC_SHADE);
1017     if (self->delete_window) {
1018         self->functions |= OB_CLIENT_FUNC_CLOSE;
1019         self->decorations |= OB_FRAME_DECOR_CLOSE;
1020     }
1021
1022     if (!(self->min_size.width < self->max_size.width ||
1023           self->min_size.height < self->max_size.height))
1024         self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1025
1026     switch (self->type) {
1027     case OB_CLIENT_TYPE_NORMAL:
1028         /* normal windows retain all of the possible decorations and
1029            functionality, and are the only windows that you can fullscreen */
1030         self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1031         break;
1032
1033     case OB_CLIENT_TYPE_DIALOG:
1034     case OB_CLIENT_TYPE_UTILITY:
1035         /* these windows cannot be maximized */
1036         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1037         break;
1038
1039     case OB_CLIENT_TYPE_MENU:
1040     case OB_CLIENT_TYPE_TOOLBAR:
1041         /* these windows get less functionality */
1042         self->functions &= ~(OB_CLIENT_FUNC_ICONIFY | OB_CLIENT_FUNC_RESIZE);
1043         break;
1044
1045     case OB_CLIENT_TYPE_DESKTOP:
1046     case OB_CLIENT_TYPE_DOCK:
1047     case OB_CLIENT_TYPE_SPLASH:
1048         /* none of these windows are manipulated by the window manager */
1049         self->decorations = 0;
1050         self->functions = 0;
1051         break;
1052     }
1053
1054     /* Mwm Hints are applied subtractively to what has already been chosen for
1055        decor and functionality */
1056     if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
1057         if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
1058             if (! ((self->mwmhints.decorations & OB_MWM_DECOR_HANDLE) ||
1059                    (self->mwmhints.decorations & OB_MWM_DECOR_TITLE)))
1060                 /* if the mwm hints request no handle or title, then all
1061                    decorations are disabled */
1062                 self->decorations = 0;
1063         }
1064     }
1065
1066     if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
1067         if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
1068             if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
1069                 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1070             if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
1071                 self->functions &= ~OB_CLIENT_FUNC_MOVE;
1072             /* dont let mwm hints kill any buttons
1073             if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
1074                 self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
1075             if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
1076                 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1077             */
1078             /* dont let mwm hints kill the close button
1079                if (! (self->mwmhints.functions & MwmFunc_Close))
1080                self->functions &= ~OB_CLIENT_FUNC_CLOSE; */
1081         }
1082     }
1083
1084     if (!(self->functions & OB_CLIENT_FUNC_SHADE))
1085         self->decorations &= ~OB_FRAME_DECOR_SHADE;
1086     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY))
1087         self->decorations &= ~OB_FRAME_DECOR_ICONIFY;
1088     if (!(self->functions & OB_CLIENT_FUNC_RESIZE))
1089         self->decorations &= ~OB_FRAME_DECOR_GRIPS;
1090
1091     /* can't maximize without moving/resizing */
1092     if (!((self->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
1093           (self->functions & OB_CLIENT_FUNC_MOVE) &&
1094           (self->functions & OB_CLIENT_FUNC_RESIZE))) {
1095         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1096         self->decorations &= ~OB_FRAME_DECOR_MAXIMIZE;
1097     }
1098
1099     /* finally, the user can have requested no decorations, which overrides
1100        everything */
1101     if (!self->decorate)
1102         self->decorations = 0;
1103
1104     /* if we don't have a titlebar, then we cannot shade! */
1105     if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1106         self->functions &= ~OB_CLIENT_FUNC_SHADE;
1107
1108     /* now we need to check against rules for the client's current state */
1109     if (self->fullscreen) {
1110         self->functions &= (OB_CLIENT_FUNC_CLOSE |
1111                             OB_CLIENT_FUNC_FULLSCREEN |
1112                             OB_CLIENT_FUNC_ICONIFY);
1113         self->decorations = 0;
1114     }
1115
1116     client_change_allowed_actions(self);
1117
1118     if (self->frame) {
1119         /* this makes sure that these windows appear on all desktops */
1120         if (self->type == OB_CLIENT_TYPE_DESKTOP &&
1121             self->desktop != DESKTOP_ALL)
1122             client_set_desktop(self, DESKTOP_ALL, FALSE);
1123
1124         /* adjust the client's decorations, etc. */
1125         client_reconfigure(self);
1126     } else {
1127         /* this makes sure that these windows appear on all desktops */
1128         if (self->type == OB_CLIENT_TYPE_DESKTOP &&
1129             self->desktop != DESKTOP_ALL)
1130         {
1131             self->desktop = DESKTOP_ALL;
1132         }
1133     }
1134 }
1135
1136 static void client_change_allowed_actions(ObClient *self)
1137 {
1138     guint32 actions[9];
1139     int num = 0;
1140
1141     /* desktop windows are kept on all desktops */
1142     if (self->type != OB_CLIENT_TYPE_DESKTOP)
1143         actions[num++] = prop_atoms.net_wm_action_change_desktop;
1144
1145     if (self->functions & OB_CLIENT_FUNC_SHADE)
1146         actions[num++] = prop_atoms.net_wm_action_shade;
1147     if (self->functions & OB_CLIENT_FUNC_CLOSE)
1148         actions[num++] = prop_atoms.net_wm_action_close;
1149     if (self->functions & OB_CLIENT_FUNC_MOVE)
1150         actions[num++] = prop_atoms.net_wm_action_move;
1151     if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1152         actions[num++] = prop_atoms.net_wm_action_minimize;
1153     if (self->functions & OB_CLIENT_FUNC_RESIZE)
1154         actions[num++] = prop_atoms.net_wm_action_resize;
1155     if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1156         actions[num++] = prop_atoms.net_wm_action_fullscreen;
1157     if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1158         actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1159         actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1160     }
1161
1162     PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1163
1164     /* make sure the window isn't breaking any rules now */
1165
1166     if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1167         if (self->frame) client_shade(self, FALSE);
1168         else self->shaded = FALSE;
1169     }
1170     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY) && self->iconic) {
1171         if (self->frame) client_iconify(self, FALSE, TRUE);
1172         else self->iconic = FALSE;
1173     }
1174     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1175         if (self->frame) client_fullscreen(self, FALSE, TRUE);
1176         else self->fullscreen = FALSE;
1177     }
1178     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
1179                                                          self->max_vert)) {
1180         if (self->frame) client_maximize(self, FALSE, 0, TRUE);
1181         else self->max_vert = self->max_horz = FALSE;
1182     }
1183 }
1184
1185 void client_reconfigure(ObClient *self)
1186 {
1187     /* by making this pass FALSE for user, we avoid the emacs event storm where
1188        every configurenotify causes an update in its normal hints, i think this
1189        is generally what we want anyways... */
1190     client_configure(self, OB_CORNER_TOPLEFT, self->area.x, self->area.y,
1191                      self->area.width, self->area.height, FALSE, TRUE);
1192 }
1193
1194 void client_update_wmhints(ObClient *self)
1195 {
1196     XWMHints *hints;
1197     gboolean ur = FALSE;
1198     GSList *it;
1199
1200     /* assume a window takes input if it doesnt specify */
1201     self->can_focus = TRUE;
1202   
1203     if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1204         if (hints->flags & InputHint)
1205             self->can_focus = hints->input;
1206
1207         /* only do this when first managing the window *AND* when we aren't
1208            starting up! */
1209         if (ob_state() != OB_STATE_STARTING && self->frame == NULL)
1210             if (hints->flags & StateHint)
1211                 self->iconic = hints->initial_state == IconicState;
1212
1213         if (hints->flags & XUrgencyHint)
1214             ur = TRUE;
1215
1216         if (!(hints->flags & WindowGroupHint))
1217             hints->window_group = None;
1218
1219         /* did the group state change? */
1220         if (hints->window_group !=
1221             (self->group ? self->group->leader : None)) {
1222             /* remove from the old group if there was one */
1223             if (self->group != NULL) {
1224                 /* remove transients of the group */
1225                 for (it = self->group->members; it; it = it->next)
1226                     self->transients = g_slist_remove(self->transients,
1227                                                       it->data);
1228                 group_remove(self->group, self);
1229                 self->group = NULL;
1230             }
1231             if (hints->window_group != None) {
1232                 self->group = group_add(hints->window_group, self);
1233
1234                 /* i can only have transients from the group if i am not
1235                    transient myself */
1236                 if (!self->transient_for) {
1237                     /* add other transients of the group that are already
1238                        set up */
1239                     for (it = self->group->members; it; it = it->next) {
1240                         ObClient *c = it->data;
1241                         if (c != self && c->transient_for == OB_TRAN_GROUP)
1242                             self->transients =
1243                                 g_slist_append(self->transients, c);
1244                     }
1245                 }
1246             }
1247
1248             /* the WM_HINTS can contain an icon */
1249             client_update_icons(self);
1250
1251             /* because the self->transient flag wont change from this call,
1252                we don't need to update the window's type and such, only its
1253                transient_for, and the transients lists of other windows in
1254                the group may be affected */
1255             client_update_transient_for(self);
1256         }
1257
1258         XFree(hints);
1259     }
1260
1261     if (ur != self->urgent) {
1262         self->urgent = ur;
1263         ob_debug("Urgent Hint for 0x%lx: %s\n", self->window,
1264                  ur ? "ON" : "OFF");
1265         /* fire the urgent callback if we're mapped, otherwise, wait until
1266            after we're mapped */
1267         if (self->frame)
1268             dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1269     }
1270 }
1271
1272 void client_update_title(ObClient *self)
1273 {
1274     GList *it;
1275     guint32 nums;
1276     guint i;
1277     char *data = NULL;
1278     gboolean read_title;
1279
1280     g_free(self->title);
1281      
1282     /* try netwm */
1283     if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1284         /* try old x stuff */
1285         if (!PROP_GETS(self->window, wm_name, locale, &data))
1286             data = g_strdup("Unnamed Window");
1287
1288     /* look for duplicates and append a number */
1289     nums = 0;
1290     for (it = client_list; it; it = it->next)
1291         if (it->data != self) {
1292             ObClient *c = it->data;
1293             if (0 == strncmp(c->title, data, strlen(data)))
1294                 nums |= 1 << c->title_count;
1295         }
1296     /* find first free number */
1297     for (i = 1; i <= 32; ++i)
1298         if (!(nums & (1 << i))) {
1299             if (self->title_count == 1 || i == 1)
1300                 self->title_count = i;
1301             break;
1302         }
1303     /* dont display the number for the first window */
1304     if (self->title_count > 1) {
1305         char *vdata, *ndata;
1306         ndata = g_strdup_printf(" - [%u]", self->title_count);
1307         vdata = g_strconcat(data, ndata, NULL);
1308         g_free(ndata);
1309         g_free(data);
1310         data = vdata;
1311     }
1312
1313     PROP_SETS(self->window, net_wm_visible_name, data);
1314
1315     self->title = data;
1316
1317     if (self->frame)
1318         frame_adjust_title(self->frame);
1319
1320     /* update the icon title */
1321     data = NULL;
1322     g_free(self->icon_title);
1323
1324     read_title = TRUE;
1325     /* try netwm */
1326     if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1327         /* try old x stuff */
1328         if (!PROP_GETS(self->window, wm_icon_name, locale, &data)) {
1329             data = g_strdup(self->title);
1330             read_title = FALSE;
1331         }
1332
1333     /* append the title count, dont display the number for the first window */
1334     if (read_title && self->title_count > 1) {
1335         char *vdata, *ndata;
1336         ndata = g_strdup_printf(" - [%u]", self->title_count);
1337         vdata = g_strconcat(data, ndata, NULL);
1338         g_free(ndata);
1339         g_free(data);
1340         data = vdata;
1341     }
1342
1343     PROP_SETS(self->window, net_wm_visible_icon_name, data);
1344
1345     self->icon_title = data;
1346 }
1347
1348 void client_update_class(ObClient *self)
1349 {
1350     char **data;
1351     char *s;
1352
1353     if (self->name) g_free(self->name);
1354     if (self->class) g_free(self->class);
1355     if (self->role) g_free(self->role);
1356
1357     self->name = self->class = self->role = NULL;
1358
1359     if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1360         if (data[0]) {
1361             self->name = g_strdup(data[0]);
1362             if (data[1])
1363                 self->class = g_strdup(data[1]);
1364         }
1365         g_strfreev(data);     
1366     }
1367
1368     if (PROP_GETS(self->window, wm_window_role, locale, &s))
1369         self->role = g_strdup(s);
1370
1371     if (self->name == NULL) self->name = g_strdup("");
1372     if (self->class == NULL) self->class = g_strdup("");
1373     if (self->role == NULL) self->role = g_strdup("");
1374 }
1375
1376 void client_update_strut(ObClient *self)
1377 {
1378     guint num;
1379     guint32 *data;
1380
1381     if (!PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1382         STRUT_SET(self->strut, 0, 0, 0, 0);
1383     } else {
1384         if (num == 4)
1385             STRUT_SET(self->strut, data[0], data[2], data[1], data[3]);
1386         else
1387             STRUT_SET(self->strut, 0, 0, 0, 0);
1388         g_free(data);
1389     }
1390
1391     /* updating here is pointless while we're being mapped cuz we're not in
1392        the client list yet */
1393     if (self->frame)
1394         screen_update_areas();
1395 }
1396
1397 void client_update_icons(ObClient *self)
1398 {
1399     guint num;
1400     guint32 *data;
1401     guint w, h, i, j;
1402
1403     for (i = 0; i < self->nicons; ++i)
1404         g_free(self->icons[i].data);
1405     if (self->nicons > 0)
1406         g_free(self->icons);
1407     self->nicons = 0;
1408
1409     if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1410         /* figure out how many valid icons are in here */
1411         i = 0;
1412         while (num - i > 2) {
1413             w = data[i++];
1414             h = data[i++];
1415             i += w * h;
1416             if (i > num || w*h == 0) break;
1417             ++self->nicons;
1418         }
1419
1420         self->icons = g_new(ObClientIcon, self->nicons);
1421     
1422         /* store the icons */
1423         i = 0;
1424         for (j = 0; j < self->nicons; ++j) {
1425             guint x, y, t;
1426
1427             w = self->icons[j].width = data[i++];
1428             h = self->icons[j].height = data[i++];
1429
1430             if (w*h == 0) continue;
1431
1432             self->icons[j].data = g_new(RrPixel32, w * h);
1433             for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1434                 if (x >= w) {
1435                     x = 0;
1436                     ++y;
1437                 }
1438                 self->icons[j].data[t] =
1439                     (((data[i] >> 24) & 0xff) << RrDefaultAlphaOffset) +
1440                     (((data[i] >> 16) & 0xff) << RrDefaultRedOffset) +
1441                     (((data[i] >> 8) & 0xff) << RrDefaultGreenOffset) +
1442                     (((data[i] >> 0) & 0xff) << RrDefaultBlueOffset);
1443             }
1444             g_assert(i <= num);
1445         }
1446
1447         g_free(data);
1448     } else if (PROP_GETA32(self->window, kwm_win_icon,
1449                            kwm_win_icon, &data, &num)) {
1450         if (num == 2) {
1451             self->nicons++;
1452             self->icons = g_new(ObClientIcon, self->nicons);
1453             xerror_set_ignore(TRUE);
1454             if (!RrPixmapToRGBA(ob_rr_inst,
1455                                 data[0], data[1],
1456                                 &self->icons[self->nicons-1].width,
1457                                 &self->icons[self->nicons-1].height,
1458                                 &self->icons[self->nicons-1].data)) {
1459                 g_free(&self->icons[self->nicons-1]);
1460                 self->nicons--;
1461             }
1462             xerror_set_ignore(FALSE);
1463         }
1464         g_free(data);
1465     } else {
1466         XWMHints *hints;
1467
1468         if ((hints = XGetWMHints(ob_display, self->window))) {
1469             if (hints->flags & IconPixmapHint) {
1470                 self->nicons++;
1471                 self->icons = g_new(ObClientIcon, self->nicons);
1472                 xerror_set_ignore(TRUE);
1473                 if (!RrPixmapToRGBA(ob_rr_inst,
1474                                     hints->icon_pixmap,
1475                                     (hints->flags & IconMaskHint ?
1476                                      hints->icon_mask : None),
1477                                     &self->icons[self->nicons-1].width,
1478                                     &self->icons[self->nicons-1].height,
1479                                     &self->icons[self->nicons-1].data)){
1480                     g_free(&self->icons[self->nicons-1]);
1481                     self->nicons--;
1482                 }
1483                 xerror_set_ignore(FALSE);
1484             }
1485             XFree(hints);
1486         }
1487     }
1488
1489     if (self->frame)
1490         frame_adjust_icon(self->frame);
1491 }
1492
1493 static void client_change_state(ObClient *self)
1494 {
1495     guint32 state[2];
1496     guint32 netstate[10];
1497     guint num;
1498
1499     state[0] = self->wmstate;
1500     state[1] = None;
1501     PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1502
1503     num = 0;
1504     if (self->modal)
1505         netstate[num++] = prop_atoms.net_wm_state_modal;
1506     if (self->shaded)
1507         netstate[num++] = prop_atoms.net_wm_state_shaded;
1508     if (self->iconic)
1509         netstate[num++] = prop_atoms.net_wm_state_hidden;
1510     if (self->skip_taskbar)
1511         netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1512     if (self->skip_pager)
1513         netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1514     if (self->fullscreen)
1515         netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1516     if (self->max_vert)
1517         netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1518     if (self->max_horz)
1519         netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1520     if (self->above)
1521         netstate[num++] = prop_atoms.net_wm_state_above;
1522     if (self->below)
1523         netstate[num++] = prop_atoms.net_wm_state_below;
1524     PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1525
1526     client_calc_layer(self);
1527
1528     if (self->frame)
1529         frame_adjust_state(self->frame);
1530 }
1531
1532 ObClient *client_search_focus_tree(ObClient *self)
1533 {
1534     GSList *it;
1535     ObClient *ret;
1536
1537     for (it = self->transients; it != NULL; it = it->next) {
1538         if (client_focused(it->data)) return it->data;
1539         if ((ret = client_search_focus_tree(it->data))) return ret;
1540     }
1541     return NULL;
1542 }
1543
1544 ObClient *client_search_focus_tree_full(ObClient *self)
1545 {
1546     if (self->transient_for) {
1547         if (self->transient_for != OB_TRAN_GROUP) {
1548             return client_search_focus_tree_full(self->transient_for);
1549         } else {
1550             GSList *it;
1551             gboolean recursed = FALSE;
1552         
1553             for (it = self->group->members; it; it = it->next)
1554                 if (!((ObClient*)it->data)->transient_for) {
1555                     ObClient *c;
1556                     if ((c = client_search_focus_tree_full(it->data)))
1557                         return c;
1558                     recursed = TRUE;
1559                 }
1560             if (recursed)
1561               return NULL;
1562         }
1563     }
1564
1565     /* this function checks the whole tree, the client_search_focus_tree~
1566        does not, so we need to check this window */
1567     if (client_focused(self))
1568       return self;
1569     return client_search_focus_tree(self);
1570 }
1571
1572 static ObStackingLayer calc_layer(ObClient *self)
1573 {
1574     ObStackingLayer l;
1575
1576     if (self->fullscreen) l = OB_STACKING_LAYER_FULLSCREEN;
1577     else if (self->type == OB_CLIENT_TYPE_DESKTOP)
1578         l = OB_STACKING_LAYER_DESKTOP;
1579     else if (self->type == OB_CLIENT_TYPE_DOCK) {
1580         if (!self->below) l = OB_STACKING_LAYER_TOP;
1581         else l = OB_STACKING_LAYER_NORMAL;
1582     }
1583     else if (self->above) l = OB_STACKING_LAYER_ABOVE;
1584     else if (self->below) l = OB_STACKING_LAYER_BELOW;
1585     else l = OB_STACKING_LAYER_NORMAL;
1586
1587     return l;
1588 }
1589
1590 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
1591                                         ObStackingLayer l, gboolean raised)
1592 {
1593     ObStackingLayer old, own;
1594     GSList *it;
1595
1596     old = self->layer;
1597     own = calc_layer(self);
1598     self->layer = l > own ? l : own;
1599
1600     for (it = self->transients; it; it = it->next)
1601         client_calc_layer_recursive(it->data, orig,
1602                                     l, raised ? raised : l != old);
1603
1604     if (!raised && l != old)
1605         if (orig->frame) { /* only restack if the original window is managed */
1606             /* XXX add_non_intrusive ever? */
1607             stacking_remove(CLIENT_AS_WINDOW(self));
1608             stacking_add(CLIENT_AS_WINDOW(self));
1609         }
1610 }
1611
1612 void client_calc_layer(ObClient *self)
1613 {
1614     ObStackingLayer l;
1615     ObClient *orig;
1616
1617     orig = self;
1618
1619     /* transients take on the layer of their parents */
1620     self = client_search_top_transient(self);
1621
1622     l = calc_layer(self);
1623
1624     client_calc_layer_recursive(self, orig, l, FALSE);
1625 }
1626
1627 gboolean client_should_show(ObClient *self)
1628 {
1629     if (self->iconic) return FALSE;
1630     else if (!(self->desktop == screen_desktop ||
1631                self->desktop == DESKTOP_ALL)) return FALSE;
1632     else if (client_normal(self) && screen_showing_desktop) return FALSE;
1633     
1634     return TRUE;
1635 }
1636
1637 static void client_showhide(ObClient *self)
1638 {
1639
1640     if (client_should_show(self))
1641         frame_show(self->frame);
1642     else
1643         frame_hide(self->frame);
1644 }
1645
1646 gboolean client_normal(ObClient *self) {
1647     return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
1648               self->type == OB_CLIENT_TYPE_DOCK ||
1649               self->type == OB_CLIENT_TYPE_SPLASH);
1650 }
1651
1652 static void client_apply_startup_state(ObClient *self)
1653 {
1654     /* these are in a carefully crafted order.. */
1655
1656     if (self->iconic) {
1657         self->iconic = FALSE;
1658         client_iconify(self, TRUE, FALSE);
1659     }
1660     if (self->fullscreen) {
1661         self->fullscreen = FALSE;
1662         client_fullscreen(self, TRUE, FALSE);
1663     }
1664     if (self->shaded) {
1665         self->shaded = FALSE;
1666         client_shade(self, TRUE);
1667     }
1668     if (self->urgent)
1669         dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1670   
1671     if (self->max_vert && self->max_horz) {
1672         self->max_vert = self->max_horz = FALSE;
1673         client_maximize(self, TRUE, 0, FALSE);
1674     } else if (self->max_vert) {
1675         self->max_vert = FALSE;
1676         client_maximize(self, TRUE, 2, FALSE);
1677     } else if (self->max_horz) {
1678         self->max_horz = FALSE;
1679         client_maximize(self, TRUE, 1, FALSE);
1680     }
1681
1682     /* nothing to do for the other states:
1683        skip_taskbar
1684        skip_pager
1685        modal
1686        above
1687        below
1688     */
1689 }
1690
1691 void client_configure_full(ObClient *self, ObCorner anchor,
1692                            int x, int y, int w, int h,
1693                            gboolean user, gboolean final,
1694                            gboolean force_reply)
1695 {
1696     gboolean moved = FALSE, resized = FALSE;
1697
1698     /* gets the frame's position */
1699     frame_client_gravity(self->frame, &x, &y);
1700
1701     /* these positions are frame positions, not client positions */
1702
1703     /* set the size and position if fullscreen */
1704     if (self->fullscreen) {
1705 #ifdef VIDMODE
1706         int dot;
1707         XF86VidModeModeLine mode;
1708 #endif
1709         Rect *a;
1710         guint i;
1711
1712         i = client_monitor(self);
1713         a = screen_physical_area_monitor(i);
1714
1715 #ifdef VIDMODE
1716         if (i == 0 && /* primary head */
1717             extensions_vidmode &&
1718             XF86VidModeGetViewPort(ob_display, ob_screen, &x, &y) &&
1719             /* get the mode last so the mode.privsize isnt freed incorrectly */
1720             XF86VidModeGetModeLine(ob_display, ob_screen, &dot, &mode)) {
1721             x += a->x;
1722             y += a->y;
1723             w = mode.hdisplay;
1724             h = mode.vdisplay;
1725             if (mode.privsize) XFree(mode.private);
1726         } else
1727 #endif
1728         {
1729             x = a->x;
1730             y = a->y;
1731             w = a->width;
1732             h = a->height;
1733         }
1734
1735         user = FALSE; /* ignore that increment etc shit when in fullscreen */
1736     } else {
1737         Rect *a;
1738
1739         a = screen_area_monitor(self->desktop, client_monitor(self));
1740
1741         /* set the size and position if maximized */
1742         if (self->max_horz) {
1743             x = a->x - self->frame->size.left;
1744             w = a->width;
1745         }
1746         if (self->max_vert) {
1747             y = a->y;
1748             h = a->height - self->frame->size.top - self->frame->size.bottom;
1749         }
1750     }
1751
1752     /* gets the client's position */
1753     frame_frame_gravity(self->frame, &x, &y);
1754
1755     /* these override the above states! if you cant move you can't move! */
1756     if (user) {
1757         if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
1758             x = self->area.x;
1759             y = self->area.y;
1760         }
1761         if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
1762             w = self->area.width;
1763             h = self->area.height;
1764         }
1765     }
1766
1767     if (!(w == self->area.width && h == self->area.height)) {
1768         int basew, baseh, minw, minh;
1769
1770         /* base size is substituted with min size if not specified */
1771         if (self->base_size.width || self->base_size.height) {
1772             basew = self->base_size.width;
1773             baseh = self->base_size.height;
1774         } else {
1775             basew = self->min_size.width;
1776             baseh = self->min_size.height;
1777         }
1778         /* min size is substituted with base size if not specified */
1779         if (self->min_size.width || self->min_size.height) {
1780             minw = self->min_size.width;
1781             minh = self->min_size.height;
1782         } else {
1783             minw = self->base_size.width;
1784             minh = self->base_size.height;
1785         }
1786
1787         if (user) {
1788             /* for interactive resizing. have to move half an increment in each
1789                direction. */
1790
1791             /* how far we are towards the next size inc */
1792             int mw = (w - basew) % self->size_inc.width; 
1793             int mh = (h - baseh) % self->size_inc.height;
1794             /* amount to add */
1795             int aw = self->size_inc.width / 2;
1796             int ah = self->size_inc.height / 2;
1797             /* don't let us move into a new size increment */
1798             if (mw + aw >= self->size_inc.width)
1799                 aw = self->size_inc.width - mw - 1;
1800             if (mh + ah >= self->size_inc.height)
1801                 ah = self->size_inc.height - mh - 1;
1802             w += aw;
1803             h += ah;
1804     
1805             /* if this is a user-requested resize, then check against min/max
1806                sizes */
1807
1808             /* smaller than min size or bigger than max size? */
1809             if (w > self->max_size.width) w = self->max_size.width;
1810             if (w < minw) w = minw;
1811             if (h > self->max_size.height) h = self->max_size.height;
1812             if (h < minh) h = minh;
1813         }
1814
1815         w -= basew;
1816         h -= baseh;
1817
1818         /* keep to the increments */
1819         w /= self->size_inc.width;
1820         h /= self->size_inc.height;
1821
1822         /* you cannot resize to nothing */
1823         if (basew + w < 1) w = 1 - basew;
1824         if (baseh + h < 1) h = 1 - baseh;
1825   
1826         /* store the logical size */
1827         SIZE_SET(self->logical_size,
1828                  self->size_inc.width > 1 ? w : w + basew,
1829                  self->size_inc.height > 1 ? h : h + baseh);
1830
1831         w *= self->size_inc.width;
1832         h *= self->size_inc.height;
1833
1834         w += basew;
1835         h += baseh;
1836
1837         if (user) {
1838             /* adjust the height to match the width for the aspect ratios.
1839              for this, min size is not substituted for base size ever. */
1840             w -= self->base_size.width;
1841             h -= self->base_size.height;
1842
1843             if (self->min_ratio)
1844                 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1845             if (self->max_ratio)
1846                 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1847
1848             w += self->base_size.width;
1849             h += self->base_size.height;
1850         }
1851     }
1852
1853     switch (anchor) {
1854     case OB_CORNER_TOPLEFT:
1855         break;
1856     case OB_CORNER_TOPRIGHT:
1857         x -= w - self->area.width;
1858         break;
1859     case OB_CORNER_BOTTOMLEFT:
1860         y -= h - self->area.height;
1861         break;
1862     case OB_CORNER_BOTTOMRIGHT:
1863         x -= w - self->area.width;
1864         y -= h - self->area.height;
1865         break;
1866     }
1867
1868     moved = x != self->area.x || y != self->area.y;
1869     resized = w != self->area.width || h != self->area.height;
1870
1871     RECT_SET(self->area, x, y, w, h);
1872
1873     /* for app-requested resizes, always resize if 'resized' is true.
1874        for user-requested ones, only resize if final is true, or when
1875        resizing in opaque mode */
1876     if ((!user && resized) ||
1877         (user && (final || (resized && config_opaque_resize))))
1878         XResizeWindow(ob_display, self->window, w, h);
1879
1880     /* move/resize the frame to match the request */
1881     if (self->frame) {
1882         if (self->decorations != self->frame->decorations)
1883             moved = resized = TRUE;
1884
1885         if (moved || resized)
1886             frame_adjust_area(self->frame, moved, resized);
1887
1888         if (!resized && (force_reply || ((!user && moved) || (user && final))))
1889         {
1890             XEvent event;
1891             event.type = ConfigureNotify;
1892             event.xconfigure.display = ob_display;
1893             event.xconfigure.event = self->window;
1894             event.xconfigure.window = self->window;
1895
1896             /* root window real coords */
1897             event.xconfigure.x = self->frame->area.x + self->frame->size.left -
1898                 self->border_width;
1899             event.xconfigure.y = self->frame->area.y + self->frame->size.top -
1900                 self->border_width;
1901             event.xconfigure.width = w;
1902             event.xconfigure.height = h;
1903             event.xconfigure.border_width = 0;
1904             event.xconfigure.above = self->frame->plate;
1905             event.xconfigure.override_redirect = FALSE;
1906             XSendEvent(event.xconfigure.display, event.xconfigure.window,
1907                        FALSE, StructureNotifyMask, &event);
1908         }
1909     }
1910 }
1911
1912 void client_fullscreen(ObClient *self, gboolean fs, gboolean savearea)
1913 {
1914     int x, y, w, h;
1915
1916     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
1917         self->fullscreen == fs) return;                   /* already done */
1918
1919     self->fullscreen = fs;
1920     client_change_state(self); /* change the state hints on the client,
1921                                   and adjust out layer/stacking */
1922
1923     if (fs) {
1924         if (savearea) {
1925             guint32 dimensions[4];
1926             dimensions[0] = self->area.x;
1927             dimensions[1] = self->area.y;
1928             dimensions[2] = self->area.width;
1929             dimensions[3] = self->area.height;
1930   
1931             PROP_SETA32(self->window, openbox_premax, cardinal,
1932                         dimensions, 4);
1933         }
1934
1935         /* these are not actually used cuz client_configure will set them
1936            as appropriate when the window is fullscreened */
1937         x = y = w = h = 0;
1938     } else {
1939         guint num;
1940         gint32 *dimensions;
1941         Rect *a;
1942
1943         /* pick some fallbacks... */
1944         a = screen_area_monitor(self->desktop, 0);
1945         x = a->x + a->width / 4;
1946         y = a->y + a->height / 4;
1947         w = a->width / 2;
1948         h = a->height / 2;
1949
1950         if (PROP_GETA32(self->window, openbox_premax, cardinal,
1951                         (guint32**)&dimensions, &num)) {
1952             if (num == 4) {
1953                 x = dimensions[0];
1954                 y = dimensions[1];
1955                 w = dimensions[2];
1956                 h = dimensions[3];
1957             }
1958             g_free(dimensions);
1959         }
1960     }
1961
1962     client_setup_decor_and_functions(self);
1963
1964     client_configure(self, OB_CORNER_TOPLEFT, x, y, w, h, TRUE, TRUE);
1965
1966     /* try focus us when we go into fullscreen mode */
1967     client_focus(self);
1968 }
1969
1970 static void client_iconify_recursive(ObClient *self,
1971                                      gboolean iconic, gboolean curdesk)
1972 {
1973     GSList *it;
1974     gboolean changed = FALSE;
1975
1976
1977     if (self->iconic != iconic) {
1978         ob_debug("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"),
1979                  self->window);
1980
1981         self->iconic = iconic;
1982
1983         if (iconic) {
1984             if (self->functions & OB_CLIENT_FUNC_ICONIFY) {
1985                 self->wmstate = IconicState;
1986                 self->ignore_unmaps++;
1987                 /* we unmap the client itself so that we can get MapRequest
1988                    events, and because the ICCCM tells us to! */
1989                 XUnmapWindow(ob_display, self->window);
1990
1991                 /* update the focus lists.. iconic windows go to the bottom of
1992                    the list, put the new iconic window at the 'top of the
1993                    bottom'. */
1994                 focus_order_to_top(self);
1995
1996                 changed = TRUE;
1997             }
1998         } else {
1999             if (curdesk)
2000                 client_set_desktop(self, screen_desktop, FALSE);
2001             self->wmstate = self->shaded ? IconicState : NormalState;
2002             XMapWindow(ob_display, self->window);
2003
2004             /* this puts it after the current focused window */
2005             focus_order_remove(self);
2006             focus_order_add_new(self);
2007
2008             /* this is here cuz with the VIDMODE extension, the viewport can
2009                change while a fullscreen window is iconic, and when it
2010                uniconifies, it would be nice if it did so to the new position
2011                of the viewport */
2012             client_reconfigure(self);
2013
2014             changed = TRUE;
2015         }
2016     }
2017
2018     if (changed) {
2019         client_change_state(self);
2020         client_showhide(self);
2021         screen_update_areas();
2022
2023         dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
2024                         self, 0, 0);
2025     }
2026
2027     /* iconify all transients */
2028     for (it = self->transients; it != NULL; it = it->next)
2029         if (it->data != self) client_iconify_recursive(it->data,
2030                                                        iconic, curdesk);
2031 }
2032
2033 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk)
2034 {
2035     /* move up the transient chain as far as possible first */
2036     self = client_search_top_transient(self);
2037
2038     client_iconify_recursive(client_search_top_transient(self),
2039                              iconic, curdesk);
2040 }
2041
2042 void client_maximize(ObClient *self, gboolean max, int dir, gboolean savearea)
2043 {
2044     int x, y, w, h;
2045      
2046     g_assert(dir == 0 || dir == 1 || dir == 2);
2047     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE)) return; /* can't */
2048
2049     /* check if already done */
2050     if (max) {
2051         if (dir == 0 && self->max_horz && self->max_vert) return;
2052         if (dir == 1 && self->max_horz) return;
2053         if (dir == 2 && self->max_vert) return;
2054     } else {
2055         if (dir == 0 && !self->max_horz && !self->max_vert) return;
2056         if (dir == 1 && !self->max_horz) return;
2057         if (dir == 2 && !self->max_vert) return;
2058     }
2059
2060     /* work with the frame's coords */
2061     x = self->frame->area.x;
2062     y = self->frame->area.y;
2063     w = self->area.width;
2064     h = self->area.height;
2065
2066     if (max) {
2067         if (savearea) {
2068             gint32 dimensions[4];
2069             gint32 *readdim;
2070             guint num;
2071
2072             dimensions[0] = x;
2073             dimensions[1] = y;
2074             dimensions[2] = w;
2075             dimensions[3] = h;
2076
2077             /* get the property off the window and use it for the dimensions
2078                we are already maxed on */
2079             if (PROP_GETA32(self->window, openbox_premax, cardinal,
2080                             (guint32**)&readdim, &num)) {
2081                 if (num == 4) {
2082                     if (self->max_horz) {
2083                         dimensions[0] = readdim[0];
2084                         dimensions[2] = readdim[2];
2085                     }
2086                     if (self->max_vert) {
2087                         dimensions[1] = readdim[1];
2088                         dimensions[3] = readdim[3];
2089                     }
2090                 }
2091                 g_free(readdim);
2092             }
2093
2094             PROP_SETA32(self->window, openbox_premax, cardinal,
2095                         (guint32*)dimensions, 4);
2096         }
2097     } else {
2098         guint num;
2099         gint32 *dimensions;
2100         Rect *a;
2101
2102         /* pick some fallbacks... */
2103         a = screen_area_monitor(self->desktop, 0);
2104         if (dir == 0 || dir == 1) { /* horz */
2105             x = a->x + a->width / 4;
2106             w = a->width / 2;
2107         }
2108         if (dir == 0 || dir == 2) { /* vert */
2109             y = a->y + a->height / 4;
2110             h = a->height / 2;
2111         }
2112
2113         if (PROP_GETA32(self->window, openbox_premax, cardinal,
2114                         (guint32**)&dimensions, &num)) {
2115             if (num == 4) {
2116                 if (dir == 0 || dir == 1) { /* horz */
2117                     x = dimensions[0];
2118                     w = dimensions[2];
2119                 }
2120                 if (dir == 0 || dir == 2) { /* vert */
2121                     y = dimensions[1];
2122                     h = dimensions[3];
2123                 }
2124             }
2125             g_free(dimensions);
2126         }
2127     }
2128
2129     if (dir == 0 || dir == 1) /* horz */
2130         self->max_horz = max;
2131     if (dir == 0 || dir == 2) /* vert */
2132         self->max_vert = max;
2133
2134     if (!self->max_horz && !self->max_vert)
2135         PROP_ERASE(self->window, openbox_premax);
2136
2137     client_change_state(self); /* change the state hints on the client */
2138
2139     /* figure out where the client should be going */
2140     frame_frame_gravity(self->frame, &x, &y);
2141     client_configure(self, OB_CORNER_TOPLEFT, x, y, w, h, TRUE, TRUE);
2142 }
2143
2144 void client_shade(ObClient *self, gboolean shade)
2145 {
2146     if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
2147          shade) ||                         /* can't shade */
2148         self->shaded == shade) return;     /* already done */
2149
2150     /* when we're iconic, don't change the wmstate */
2151     if (!self->iconic)
2152         self->wmstate = shade ? IconicState : NormalState;
2153     self->shaded = shade;
2154     client_change_state(self);
2155     /* resize the frame to just the titlebar */
2156     frame_adjust_area(self->frame, FALSE, FALSE);
2157 }
2158
2159 void client_close(ObClient *self)
2160 {
2161     XEvent ce;
2162
2163     if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
2164
2165     /*
2166       XXX: itd be cool to do timeouts and shit here for killing the client's
2167       process off
2168       like... if the window is around after 5 seconds, then the close button
2169       turns a nice red, and if this function is called again, the client is
2170       explicitly killed.
2171     */
2172
2173     ce.xclient.type = ClientMessage;
2174     ce.xclient.message_type =  prop_atoms.wm_protocols;
2175     ce.xclient.display = ob_display;
2176     ce.xclient.window = self->window;
2177     ce.xclient.format = 32;
2178     ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
2179     ce.xclient.data.l[1] = event_lasttime;
2180     ce.xclient.data.l[2] = 0l;
2181     ce.xclient.data.l[3] = 0l;
2182     ce.xclient.data.l[4] = 0l;
2183     XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2184 }
2185
2186 void client_kill(ObClient *self)
2187 {
2188     XKillClient(ob_display, self->window);
2189 }
2190
2191 void client_set_desktop_recursive(ObClient *self,
2192                                   guint target, gboolean donthide)
2193 {
2194     guint old;
2195     GSList *it;
2196
2197     if (target != self->desktop) {
2198
2199         ob_debug("Setting desktop %u\n", target+1);
2200
2201         g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2202
2203         /* remove from the old desktop(s) */
2204         focus_order_remove(self);
2205
2206         old = self->desktop;
2207         self->desktop = target;
2208         PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2209         /* the frame can display the current desktop state */
2210         frame_adjust_state(self->frame);
2211         /* 'move' the window to the new desktop */
2212         if (!donthide)
2213             client_showhide(self);
2214         /* raise if it was not already on the desktop */
2215         if (old != DESKTOP_ALL)
2216             stacking_raise(CLIENT_AS_WINDOW(self));
2217         screen_update_areas();
2218
2219         /* add to the new desktop(s) */
2220         if (config_focus_new)
2221             focus_order_to_top(self);
2222         else
2223             focus_order_to_bottom(self);
2224
2225         dispatch_client(Event_Client_Desktop, self, target, old);
2226     }
2227
2228     /* move all transients */
2229     for (it = self->transients; it != NULL; it = it->next)
2230         if (it->data != self) client_set_desktop_recursive(it->data,
2231                                                            target, donthide);
2232 }
2233
2234 void client_set_desktop(ObClient *self, guint target, gboolean donthide)
2235 {
2236     client_set_desktop_recursive(client_search_top_transient(self),
2237                                  target, donthide);
2238 }
2239
2240 ObClient *client_search_modal_child(ObClient *self)
2241 {
2242     GSList *it;
2243     ObClient *ret;
2244   
2245     for (it = self->transients; it != NULL; it = it->next) {
2246         ObClient *c = it->data;
2247         if ((ret = client_search_modal_child(c))) return ret;
2248         if (c->modal) return c;
2249     }
2250     return NULL;
2251 }
2252
2253 gboolean client_validate(ObClient *self)
2254 {
2255     XEvent e; 
2256
2257     XSync(ob_display, FALSE); /* get all events on the server */
2258
2259     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2260         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2261         XPutBackEvent(ob_display, &e);
2262         return FALSE;
2263     }
2264
2265     return TRUE;
2266 }
2267
2268 void client_set_wm_state(ObClient *self, long state)
2269 {
2270     if (state == self->wmstate) return; /* no change */
2271   
2272     switch (state) {
2273     case IconicState:
2274         client_iconify(self, TRUE, TRUE);
2275         break;
2276     case NormalState:
2277         client_iconify(self, FALSE, TRUE);
2278         break;
2279     }
2280 }
2281
2282 void client_set_state(ObClient *self, Atom action, long data1, long data2)
2283 {
2284     gboolean shaded = self->shaded;
2285     gboolean fullscreen = self->fullscreen;
2286     gboolean max_horz = self->max_horz;
2287     gboolean max_vert = self->max_vert;
2288     int i;
2289
2290     if (!(action == prop_atoms.net_wm_state_add ||
2291           action == prop_atoms.net_wm_state_remove ||
2292           action == prop_atoms.net_wm_state_toggle))
2293         /* an invalid action was passed to the client message, ignore it */
2294         return; 
2295
2296     for (i = 0; i < 2; ++i) {
2297         Atom state = i == 0 ? data1 : data2;
2298     
2299         if (!state) continue;
2300
2301         /* if toggling, then pick whether we're adding or removing */
2302         if (action == prop_atoms.net_wm_state_toggle) {
2303             if (state == prop_atoms.net_wm_state_modal)
2304                 action = self->modal ? prop_atoms.net_wm_state_remove :
2305                     prop_atoms.net_wm_state_add;
2306             else if (state == prop_atoms.net_wm_state_maximized_vert)
2307                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2308                     prop_atoms.net_wm_state_add;
2309             else if (state == prop_atoms.net_wm_state_maximized_horz)
2310                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2311                     prop_atoms.net_wm_state_add;
2312             else if (state == prop_atoms.net_wm_state_shaded)
2313                 action = self->shaded ? prop_atoms.net_wm_state_remove :
2314                     prop_atoms.net_wm_state_add;
2315             else if (state == prop_atoms.net_wm_state_skip_taskbar)
2316                 action = self->skip_taskbar ?
2317                     prop_atoms.net_wm_state_remove :
2318                     prop_atoms.net_wm_state_add;
2319             else if (state == prop_atoms.net_wm_state_skip_pager)
2320                 action = self->skip_pager ?
2321                     prop_atoms.net_wm_state_remove :
2322                     prop_atoms.net_wm_state_add;
2323             else if (state == prop_atoms.net_wm_state_fullscreen)
2324                 action = self->fullscreen ?
2325                     prop_atoms.net_wm_state_remove :
2326                     prop_atoms.net_wm_state_add;
2327             else if (state == prop_atoms.net_wm_state_above)
2328                 action = self->above ? prop_atoms.net_wm_state_remove :
2329                     prop_atoms.net_wm_state_add;
2330             else if (state == prop_atoms.net_wm_state_below)
2331                 action = self->below ? prop_atoms.net_wm_state_remove :
2332                     prop_atoms.net_wm_state_add;
2333         }
2334     
2335         if (action == prop_atoms.net_wm_state_add) {
2336             if (state == prop_atoms.net_wm_state_modal) {
2337                 /* XXX raise here or something? */
2338                 self->modal = TRUE;
2339             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2340                 max_vert = TRUE;
2341             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2342                 max_horz = TRUE;
2343             } else if (state == prop_atoms.net_wm_state_shaded) {
2344                 shaded = TRUE;
2345             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2346                 self->skip_taskbar = TRUE;
2347             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2348                 self->skip_pager = TRUE;
2349             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2350                 fullscreen = TRUE;
2351             } else if (state == prop_atoms.net_wm_state_above) {
2352                 self->above = TRUE;
2353             } else if (state == prop_atoms.net_wm_state_below) {
2354                 self->below = TRUE;
2355             }
2356
2357         } else { /* action == prop_atoms.net_wm_state_remove */
2358             if (state == prop_atoms.net_wm_state_modal) {
2359                 self->modal = FALSE;
2360             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2361                 max_vert = FALSE;
2362             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2363                 max_horz = FALSE;
2364             } else if (state == prop_atoms.net_wm_state_shaded) {
2365                 shaded = FALSE;
2366             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2367                 self->skip_taskbar = FALSE;
2368             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2369                 self->skip_pager = FALSE;
2370             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2371                 fullscreen = FALSE;
2372             } else if (state == prop_atoms.net_wm_state_above) {
2373                 self->above = FALSE;
2374             } else if (state == prop_atoms.net_wm_state_below) {
2375                 self->below = FALSE;
2376             }
2377         }
2378     }
2379     if (max_horz != self->max_horz || max_vert != self->max_vert) {
2380         if (max_horz != self->max_horz && max_vert != self->max_vert) {
2381             /* toggling both */
2382             if (max_horz == max_vert) { /* both going the same way */
2383                 client_maximize(self, max_horz, 0, TRUE);
2384             } else {
2385                 client_maximize(self, max_horz, 1, TRUE);
2386                 client_maximize(self, max_vert, 2, TRUE);
2387             }
2388         } else {
2389             /* toggling one */
2390             if (max_horz != self->max_horz)
2391                 client_maximize(self, max_horz, 1, TRUE);
2392             else
2393                 client_maximize(self, max_vert, 2, TRUE);
2394         }
2395     }
2396     /* change fullscreen state before shading, as it will affect if the window
2397        can shade or not */
2398     if (fullscreen != self->fullscreen)
2399         client_fullscreen(self, fullscreen, TRUE);
2400     if (shaded != self->shaded)
2401         client_shade(self, shaded);
2402     client_calc_layer(self);
2403     client_change_state(self); /* change the hint to reflect these changes */
2404 }
2405
2406 ObClient *client_focus_target(ObClient *self)
2407 {
2408     ObClient *child;
2409      
2410     /* if we have a modal child, then focus it, not us */
2411     child = client_search_modal_child(self);
2412     if (child) return child;
2413     return self;
2414 }
2415
2416 gboolean client_can_focus(ObClient *self)
2417 {
2418     XEvent ev;
2419
2420     /* choose the correct target */
2421     self = client_focus_target(self);
2422
2423     if (!self->frame->visible)
2424         return FALSE;
2425
2426     if (!((self->can_focus || self->focus_notify) &&
2427           (self->desktop == screen_desktop ||
2428            self->desktop == DESKTOP_ALL) &&
2429           !self->iconic))
2430         return FALSE;
2431
2432     /* do a check to see if the window has already been unmapped or destroyed
2433        do this intelligently while watching out for unmaps we've generated
2434        (ignore_unmaps > 0) */
2435     if (XCheckTypedWindowEvent(ob_display, self->window,
2436                                DestroyNotify, &ev)) {
2437         XPutBackEvent(ob_display, &ev);
2438         return FALSE;
2439     }
2440     while (XCheckTypedWindowEvent(ob_display, self->window,
2441                                   UnmapNotify, &ev)) {
2442         if (self->ignore_unmaps) {
2443             self->ignore_unmaps--;
2444         } else {
2445             XPutBackEvent(ob_display, &ev);
2446             return FALSE;
2447         }
2448     }
2449
2450     return TRUE;
2451 }
2452
2453 gboolean client_focus(ObClient *self)
2454 {
2455     /* choose the correct target */
2456     self = client_focus_target(self);
2457
2458     if (!client_can_focus(self)) {
2459         if (!self->frame->visible) {
2460             /* update the focus lists */
2461             focus_order_to_top(self);
2462         }
2463         return FALSE;
2464     }
2465
2466     if (self->can_focus)
2467         /* RevertToPointerRoot causes much more headache than RevertToNone, so
2468            I choose to use it always, hopefully to find errors quicker, if any
2469            are left. (I hate X. I hate focus events.) */
2470         XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2471                        event_lasttime);
2472
2473     if (self->focus_notify) {
2474         XEvent ce;
2475         ce.xclient.type = ClientMessage;
2476         ce.xclient.message_type = prop_atoms.wm_protocols;
2477         ce.xclient.display = ob_display;
2478         ce.xclient.window = self->window;
2479         ce.xclient.format = 32;
2480         ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2481         ce.xclient.data.l[1] = event_lasttime;
2482         ce.xclient.data.l[2] = 0l;
2483         ce.xclient.data.l[3] = 0l;
2484         ce.xclient.data.l[4] = 0l;
2485         XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2486     }
2487
2488 #ifdef DEBUG_FOCUS
2489     ob_debug("%sively focusing %lx at %d\n",
2490              (self->can_focus ? "act" : "pass"),
2491              self->window, (int) event_lasttime);
2492 #endif
2493
2494     /* Cause the FocusIn to come back to us. Important for desktop switches,
2495        since otherwise we'll have no FocusIn on the queue and send it off to
2496        the focus_backup. */
2497     XSync(ob_display, FALSE);
2498     return TRUE;
2499 }
2500
2501 void client_unfocus(ObClient *self)
2502 {
2503     g_assert(focus_client == self);
2504 #ifdef DEBUG_FOCUS
2505     ob_debug("client_unfocus for %lx\n", self->window);
2506 #endif
2507     focus_fallback(OB_FOCUS_FALLBACK_UNFOCUSING);
2508 }
2509
2510 void client_activate(ObClient *self)
2511 {
2512     if (client_normal(self) && screen_showing_desktop)
2513         screen_show_desktop(FALSE);
2514     if (self->iconic)
2515         client_iconify(self, FALSE, FALSE);
2516     if (self->desktop != DESKTOP_ALL &&
2517         self->desktop != screen_desktop)
2518         screen_set_desktop(self->desktop);
2519     else if (!self->frame->visible)
2520         /* if its not visible for other reasons, then don't mess
2521            with it */
2522         return;
2523     if (self->shaded)
2524         client_shade(self, FALSE);
2525     client_focus(self);
2526     stacking_raise(CLIENT_AS_WINDOW(self));
2527 }
2528
2529 gboolean client_focused(ObClient *self)
2530 {
2531     return self == focus_client;
2532 }
2533
2534 ObClientIcon *client_icon(ObClient *self, int w, int h)
2535 {
2536     guint i;
2537     /* si is the smallest image >= req */
2538     /* li is the largest image < req */
2539     unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2540
2541     if (!self->nicons) return NULL;
2542
2543     for (i = 0; i < self->nicons; ++i) {
2544         size = self->icons[i].width * self->icons[i].height;
2545         if (size < smallest && size >= (unsigned)(w * h)) {
2546             smallest = size;
2547             si = i;
2548         }
2549         if (size > largest && size <= (unsigned)(w * h)) {
2550             largest = size;
2551             li = i;
2552         }
2553     }
2554     if (largest == 0) /* didnt find one smaller than the requested size */
2555         return &self->icons[si];
2556     return &self->icons[li];
2557 }
2558
2559 /* this be mostly ripped from fvwm */
2560 ObClient *client_find_directional(ObClient *c, ObDirection dir) 
2561 {
2562     int my_cx, my_cy, his_cx, his_cy;
2563     int offset = 0;
2564     int distance = 0;
2565     int score, best_score;
2566     ObClient *best_client, *cur;
2567     GList *it;
2568
2569     if(!client_list)
2570         return NULL;
2571
2572     /* first, find the centre coords of the currently focused window */
2573     my_cx = c->frame->area.x + c->frame->area.width / 2;
2574     my_cy = c->frame->area.y + c->frame->area.height / 2;
2575
2576     best_score = -1;
2577     best_client = NULL;
2578
2579     for(it = g_list_first(client_list); it; it = it->next) {
2580         cur = it->data;
2581
2582         /* the currently selected window isn't interesting */
2583         if(cur == c)
2584             continue;
2585         if (!client_normal(cur))
2586             continue;
2587         if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2588             continue;
2589         if(cur->iconic)
2590             continue;
2591         if(client_focus_target(cur) == cur &&
2592            !(cur->can_focus || cur->focus_notify))
2593             continue;
2594
2595         /* find the centre coords of this window, from the
2596          * currently focused window's point of view */
2597         his_cx = (cur->frame->area.x - my_cx)
2598             + cur->frame->area.width / 2;
2599         his_cy = (cur->frame->area.y - my_cy)
2600             + cur->frame->area.height / 2;
2601
2602         if(dir > 3) { 
2603             int tx;
2604             /* Rotate the diagonals 45 degrees counterclockwise.
2605              * To do this, multiply the matrix /+h +h\ with the
2606              * vector (x y).                   \-h +h/
2607              * h = sqrt(0.5). We can set h := 1 since absolute
2608              * distance doesn't matter here. */
2609             tx = his_cx + his_cy;
2610             his_cy = -his_cx + his_cy;
2611             his_cx = tx;
2612         }
2613
2614         switch(dir) {
2615         case OB_DIRECTION_NORTH:
2616         case OB_DIRECTION_SOUTH:
2617         case OB_DIRECTION_NORTHEAST:
2618         case OB_DIRECTION_SOUTHWEST:
2619             offset = (his_cx < 0) ? -his_cx : his_cx;
2620             distance = ((dir == OB_DIRECTION_NORTH ||
2621                         dir == OB_DIRECTION_NORTHEAST) ?
2622                         -his_cy : his_cy);
2623             break;
2624         case OB_DIRECTION_EAST:
2625         case OB_DIRECTION_WEST:
2626         case OB_DIRECTION_SOUTHEAST:
2627         case OB_DIRECTION_NORTHWEST:
2628             offset = (his_cy < 0) ? -his_cy : his_cy;
2629             distance = ((dir == OB_DIRECTION_WEST ||
2630                         dir == OB_DIRECTION_NORTHWEST) ?
2631                         -his_cx : his_cx);
2632             break;
2633         }
2634
2635         /* the target must be in the requested direction */
2636         if(distance <= 0)
2637             continue;
2638
2639         /* Calculate score for this window.  The smaller the better. */
2640         score = distance + offset;
2641
2642         /* windows more than 45 degrees off the direction are
2643          * heavily penalized and will only be chosen if nothing
2644          * else within a million pixels */
2645         if(offset > distance)
2646             score += 1000000;
2647
2648         if(best_score == -1 || score < best_score)
2649             best_client = cur,
2650                 best_score = score;
2651     }
2652
2653     return best_client;
2654 }
2655
2656 void client_set_layer(ObClient *self, int layer)
2657 {
2658     if (layer < 0) {
2659         self->below = TRUE;
2660         self->above = FALSE;
2661     } else if (layer == 0) {
2662         self->below = self->above = FALSE;
2663     } else {
2664         self->below = FALSE;
2665         self->above = TRUE;
2666     }
2667     client_calc_layer(self);
2668     client_change_state(self); /* reflect this in the state hints */
2669 }
2670
2671 guint client_monitor(ObClient *self)
2672 {
2673     guint i;
2674
2675     for (i = 0; i < screen_num_monitors; ++i) {
2676         Rect *area = screen_physical_area_monitor(i);
2677         if (RECT_INTERSECTS_RECT(*area, self->frame->area))
2678             break;
2679     }
2680     if (i == screen_num_monitors) i = 0;
2681     g_assert(i < screen_num_monitors);
2682     return i;
2683 }
2684
2685 ObClient *client_search_top_transient(ObClient *self)
2686 {
2687     /* move up the transient chain as far as possible */
2688     if (self->transient_for) {
2689         if (self->transient_for != OB_TRAN_GROUP) {
2690             return client_search_top_transient(self->transient_for);
2691         } else {
2692             GSList *it;
2693
2694             for (it = self->group->members; it; it = it->next) {
2695                 ObClient *c = it->data;
2696
2697                 /* checking transient_for prevents infinate loops! */
2698                 if (c != self && !c->transient_for)
2699                     break;
2700             }
2701             if (it)
2702                 return it->data;
2703         }
2704     }
2705
2706     return self;
2707 }
2708
2709 ObClient *client_search_transient(ObClient *self, ObClient *search)
2710 {
2711     GSList *sit;
2712
2713     for (sit = self->transients; sit; sit = g_slist_next(sit)) {
2714         if (sit->data == search)
2715             return search;
2716         if (client_search_transient(sit->data, search))
2717             return search;
2718     }
2719     return NULL;
2720 }
2721
2722 gchar* client_get_sm_client_id(ObClient *self)
2723 {
2724     gchar *id = NULL;
2725
2726     if (!PROP_GETS(self->window, sm_client_id, locale, &id) && self->group)
2727         PROP_GETS(self->group->leader, sm_client_id, locale, &id);
2728     return id;
2729 }