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