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