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