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