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