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