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