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