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