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