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