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