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