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