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