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