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