]> icculus.org git repositories - dana/openbox.git/blob - openbox/client.c
dont put windows below by default
[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     if (!(self->above || self->below)) {
854         if (self->group) {
855             /* apply stuff from the group */
856             GSList *it;
857             gint layer = -2;
858
859             for (it = self->group->members; it; it = g_slist_next(it)) {
860                 ObClient *c = it->data;
861                 if (c != self && !client_search_transient(self, c))
862                 {
863                     layer = MAX(layer,
864                                 (c->above ? 1 : (c->below ? -1 : 0)));
865                 }
866             }
867             switch (layer) {
868             case -1:
869                 self->below = TRUE;
870                 break;
871             case -2:
872             case 0:
873                 break;
874             case 1:
875                 self->above = TRUE;
876                 break;
877             default:
878                 g_assert_not_reached();
879                 break;
880             }
881         }
882     }
883 }
884
885 static void client_get_shaped(ObClient *self)
886 {
887     self->shaped = FALSE;
888 #ifdef   SHAPE
889     if (extensions_shape) {
890         int foo;
891         guint ufoo;
892         int s;
893
894         XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
895
896         XShapeQueryExtents(ob_display, self->window, &s, &foo,
897                            &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
898                            &ufoo);
899         self->shaped = (s != 0);
900     }
901 #endif
902 }
903
904 void client_update_transient_for(ObClient *self)
905 {
906     Window t = None;
907     ObClient *target = NULL;
908
909     if (XGetTransientForHint(ob_display, self->window, &t)) {
910         self->transient = TRUE;
911         if (t != self->window) { /* cant be transient to itself! */
912             target = g_hash_table_lookup(window_map, &t);
913             /* if this happens then we need to check for it*/
914             g_assert(target != self);
915             if (target && !WINDOW_IS_CLIENT(target)) {
916                 /* this can happen when a dialog is a child of
917                    a dockapp, for example */
918                 target = NULL;
919             }
920             
921             if (!target && self->group) {
922                 /* not transient to a client, see if it is transient for a
923                    group */
924                 if (t == self->group->leader ||
925                     t == None ||
926                     t == RootWindow(ob_display, ob_screen))
927                 {
928                     /* window is a transient for its group! */
929                     target = OB_TRAN_GROUP;
930                 }
931             }
932         }
933     } else
934         self->transient = FALSE;
935
936     /* if anything has changed... */
937     if (target != self->transient_for) {
938         if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
939             GSList *it;
940
941             /* remove from old parents */
942             for (it = self->group->members; it; it = g_slist_next(it)) {
943                 ObClient *c = it->data;
944                 if (c != self && !c->transient_for)
945                     c->transients = g_slist_remove(c->transients, self);
946             }
947         } else if (self->transient_for != NULL) { /* transient of window */
948             /* remove from old parent */
949             self->transient_for->transients =
950                 g_slist_remove(self->transient_for->transients, self);
951         }
952         self->transient_for = target;
953         if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
954             GSList *it;
955
956             /* add to new parents */
957             for (it = self->group->members; it; it = g_slist_next(it)) {
958                 ObClient *c = it->data;
959                 if (c != self && !c->transient_for)
960                     c->transients = g_slist_append(c->transients, self);
961             }
962
963             /* remove all transients which are in the group, that causes
964                circlular pointer hell of doom */
965             for (it = self->group->members; it; it = g_slist_next(it)) {
966                 GSList *sit, *next;
967                 for (sit = self->transients; sit; sit = next) {
968                     next = g_slist_next(sit);
969                     if (sit->data == it->data)
970                         self->transients =
971                             g_slist_delete_link(self->transients, sit);
972                 }
973             }
974         } else if (self->transient_for != NULL) { /* transient of window */
975             /* add to new parent */
976             self->transient_for->transients =
977                 g_slist_append(self->transient_for->transients, self);
978         }
979     }
980 }
981
982 static void client_get_mwm_hints(ObClient *self)
983 {
984     guint num;
985     guint32 *hints;
986
987     self->mwmhints.flags = 0; /* default to none */
988
989     if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
990                     &hints, &num)) {
991         if (num >= OB_MWM_ELEMENTS) {
992             self->mwmhints.flags = hints[0];
993             self->mwmhints.functions = hints[1];
994             self->mwmhints.decorations = hints[2];
995         }
996         g_free(hints);
997     }
998 }
999
1000 void client_get_type(ObClient *self)
1001 {
1002     guint num, i;
1003     guint32 *val;
1004
1005     self->type = -1;
1006   
1007     if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
1008         /* use the first value that we know about in the array */
1009         for (i = 0; i < num; ++i) {
1010             if (val[i] == prop_atoms.net_wm_window_type_desktop)
1011                 self->type = OB_CLIENT_TYPE_DESKTOP;
1012             else if (val[i] == prop_atoms.net_wm_window_type_dock)
1013                 self->type = OB_CLIENT_TYPE_DOCK;
1014             else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
1015                 self->type = OB_CLIENT_TYPE_TOOLBAR;
1016             else if (val[i] == prop_atoms.net_wm_window_type_menu)
1017                 self->type = OB_CLIENT_TYPE_MENU;
1018             else if (val[i] == prop_atoms.net_wm_window_type_utility)
1019                 self->type = OB_CLIENT_TYPE_UTILITY;
1020             else if (val[i] == prop_atoms.net_wm_window_type_splash)
1021                 self->type = OB_CLIENT_TYPE_SPLASH;
1022             else if (val[i] == prop_atoms.net_wm_window_type_dialog)
1023                 self->type = OB_CLIENT_TYPE_DIALOG;
1024             else if (val[i] == prop_atoms.net_wm_window_type_normal)
1025                 self->type = OB_CLIENT_TYPE_NORMAL;
1026             else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
1027                 /* prevent this window from getting any decor or
1028                    functionality */
1029                 self->mwmhints.flags &= (OB_MWM_FLAG_FUNCTIONS |
1030                                          OB_MWM_FLAG_DECORATIONS);
1031                 self->mwmhints.decorations = 0;
1032                 self->mwmhints.functions = 0;
1033             }
1034             if (self->type != (ObClientType) -1)
1035                 break; /* grab the first legit type */
1036         }
1037         g_free(val);
1038     }
1039     
1040     if (self->type == (ObClientType) -1) {
1041         /*the window type hint was not set, which means we either classify
1042           ourself as a normal window or a dialog, depending on if we are a
1043           transient. */
1044         if (self->transient)
1045             self->type = OB_CLIENT_TYPE_DIALOG;
1046         else
1047             self->type = OB_CLIENT_TYPE_NORMAL;
1048     }
1049 }
1050
1051 void client_update_protocols(ObClient *self)
1052 {
1053     guint32 *proto;
1054     guint num_return, i;
1055
1056     self->focus_notify = FALSE;
1057     self->delete_window = FALSE;
1058
1059     if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
1060         for (i = 0; i < num_return; ++i) {
1061             if (proto[i] == prop_atoms.wm_delete_window) {
1062                 /* this means we can request the window to close */
1063                 self->delete_window = TRUE;
1064             } else if (proto[i] == prop_atoms.wm_take_focus)
1065                 /* if this protocol is requested, then the window will be
1066                    notified whenever we want it to receive focus */
1067                 self->focus_notify = TRUE;
1068         }
1069         g_free(proto);
1070     }
1071 }
1072
1073 static void client_get_gravity(ObClient *self)
1074 {
1075     XWindowAttributes wattrib;
1076     Status ret;
1077
1078     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
1079     g_assert(ret != BadWindow);
1080     self->gravity = wattrib.win_gravity;
1081 }
1082
1083 void client_update_normal_hints(ObClient *self)
1084 {
1085     XSizeHints size;
1086     long ret;
1087     int oldgravity = self->gravity;
1088
1089     /* defaults */
1090     self->min_ratio = 0.0f;
1091     self->max_ratio = 0.0f;
1092     SIZE_SET(self->size_inc, 1, 1);
1093     SIZE_SET(self->base_size, 0, 0);
1094     SIZE_SET(self->min_size, 0, 0);
1095     SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
1096
1097     /* get the hints from the window */
1098     if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
1099         /* normal windows can't request placement! har har
1100         if (!client_normal(self))
1101         */
1102         self->positioned = !!(size.flags & (PPosition|USPosition));
1103
1104         if (size.flags & PWinGravity) {
1105             self->gravity = size.win_gravity;
1106       
1107             /* if the client has a frame, i.e. has already been mapped and
1108                is changing its gravity */
1109             if (self->frame && self->gravity != oldgravity) {
1110                 /* move our idea of the client's position based on its new
1111                    gravity */
1112                 self->area.x = self->frame->area.x;
1113                 self->area.y = self->frame->area.y;
1114                 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
1115             }
1116         }
1117
1118         if (size.flags & PAspect) {
1119             if (size.min_aspect.y)
1120                 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
1121             if (size.max_aspect.y)
1122                 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
1123         }
1124
1125         if (size.flags & PMinSize)
1126             SIZE_SET(self->min_size, size.min_width, size.min_height);
1127     
1128         if (size.flags & PMaxSize)
1129             SIZE_SET(self->max_size, size.max_width, size.max_height);
1130     
1131         if (size.flags & PBaseSize)
1132             SIZE_SET(self->base_size, size.base_width, size.base_height);
1133     
1134         if (size.flags & PResizeInc)
1135             SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
1136     }
1137 }
1138
1139 void client_setup_decor_and_functions(ObClient *self)
1140 {
1141     /* start with everything (cept fullscreen) */
1142     self->decorations =
1143         (OB_FRAME_DECOR_TITLEBAR |
1144          (ob_rr_theme->show_handle ? OB_FRAME_DECOR_HANDLE : 0) |
1145          OB_FRAME_DECOR_GRIPS |
1146          OB_FRAME_DECOR_BORDER |
1147          OB_FRAME_DECOR_ICON |
1148          OB_FRAME_DECOR_ALLDESKTOPS |
1149          OB_FRAME_DECOR_ICONIFY |
1150          OB_FRAME_DECOR_MAXIMIZE |
1151          OB_FRAME_DECOR_SHADE |
1152          OB_FRAME_DECOR_CLOSE);
1153     self->functions =
1154         (OB_CLIENT_FUNC_RESIZE |
1155          OB_CLIENT_FUNC_MOVE |
1156          OB_CLIENT_FUNC_ICONIFY |
1157          OB_CLIENT_FUNC_MAXIMIZE |
1158          OB_CLIENT_FUNC_SHADE |
1159          OB_CLIENT_FUNC_CLOSE);
1160
1161     if (!(self->min_size.width < self->max_size.width ||
1162           self->min_size.height < self->max_size.height))
1163         self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1164
1165     switch (self->type) {
1166     case OB_CLIENT_TYPE_NORMAL:
1167         /* normal windows retain all of the possible decorations and
1168            functionality, and are the only windows that you can fullscreen */
1169         self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1170         break;
1171
1172     case OB_CLIENT_TYPE_DIALOG:
1173     case OB_CLIENT_TYPE_UTILITY:
1174         /* these windows cannot be maximized */
1175         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1176         break;
1177
1178     case OB_CLIENT_TYPE_MENU:
1179     case OB_CLIENT_TYPE_TOOLBAR:
1180         /* these windows get less functionality */
1181         self->functions &= ~(OB_CLIENT_FUNC_ICONIFY | OB_CLIENT_FUNC_RESIZE);
1182         break;
1183
1184     case OB_CLIENT_TYPE_DESKTOP:
1185     case OB_CLIENT_TYPE_DOCK:
1186     case OB_CLIENT_TYPE_SPLASH:
1187         /* none of these windows are manipulated by the window manager */
1188         self->decorations = 0;
1189         self->functions = 0;
1190         break;
1191     }
1192
1193     /* Mwm Hints are applied subtractively to what has already been chosen for
1194        decor and functionality */
1195     if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
1196         if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
1197             if (! ((self->mwmhints.decorations & OB_MWM_DECOR_HANDLE) ||
1198                    (self->mwmhints.decorations & OB_MWM_DECOR_TITLE)))
1199                 /* if the mwm hints request no handle or title, then all
1200                    decorations are disabled */
1201                 self->decorations = 0;
1202         }
1203     }
1204
1205     if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
1206         if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
1207             if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
1208                 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1209             if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
1210                 self->functions &= ~OB_CLIENT_FUNC_MOVE;
1211             /* dont let mwm hints kill any buttons
1212                if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
1213                self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
1214                if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
1215                self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1216             */
1217             /* dont let mwm hints kill the close button
1218                if (! (self->mwmhints.functions & MwmFunc_Close))
1219                self->functions &= ~OB_CLIENT_FUNC_CLOSE; */
1220             }
1221     }
1222
1223     if (!(self->functions & OB_CLIENT_FUNC_SHADE))
1224         self->decorations &= ~OB_FRAME_DECOR_SHADE;
1225     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY))
1226         self->decorations &= ~OB_FRAME_DECOR_ICONIFY;
1227     if (!(self->functions & OB_CLIENT_FUNC_RESIZE))
1228         self->decorations &= ~OB_FRAME_DECOR_GRIPS;
1229
1230     /* can't maximize without moving/resizing */
1231     if (!((self->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
1232           (self->functions & OB_CLIENT_FUNC_MOVE) &&
1233           (self->functions & OB_CLIENT_FUNC_RESIZE))) {
1234         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1235         self->decorations &= ~OB_FRAME_DECOR_MAXIMIZE;
1236     }
1237
1238     /* kill the handle on fully maxed windows */
1239     if (self->max_vert && self->max_horz)
1240         self->decorations &= ~OB_FRAME_DECOR_HANDLE;
1241
1242     /* finally, the user can have requested no decorations, which overrides
1243        everything (but doesnt give it a border if it doesnt have one) */
1244     if (self->undecorated)
1245         self->decorations &= OB_FRAME_DECOR_BORDER;
1246
1247     /* if we don't have a titlebar, then we cannot shade! */
1248     if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1249         self->functions &= ~OB_CLIENT_FUNC_SHADE;
1250
1251     /* now we need to check against rules for the client's current state */
1252     if (self->fullscreen) {
1253         self->functions &= (OB_CLIENT_FUNC_CLOSE |
1254                             OB_CLIENT_FUNC_FULLSCREEN |
1255                             OB_CLIENT_FUNC_ICONIFY);
1256         self->decorations = 0;
1257     }
1258
1259     client_change_allowed_actions(self);
1260
1261     if (self->frame) {
1262         /* adjust the client's decorations, etc. */
1263         client_reconfigure(self);
1264     }
1265 }
1266
1267 static void client_change_allowed_actions(ObClient *self)
1268 {
1269     guint32 actions[9];
1270     int num = 0;
1271
1272     /* desktop windows are kept on all desktops */
1273     if (self->type != OB_CLIENT_TYPE_DESKTOP)
1274         actions[num++] = prop_atoms.net_wm_action_change_desktop;
1275
1276     if (self->functions & OB_CLIENT_FUNC_SHADE)
1277         actions[num++] = prop_atoms.net_wm_action_shade;
1278     if (self->functions & OB_CLIENT_FUNC_CLOSE)
1279         actions[num++] = prop_atoms.net_wm_action_close;
1280     if (self->functions & OB_CLIENT_FUNC_MOVE)
1281         actions[num++] = prop_atoms.net_wm_action_move;
1282     if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1283         actions[num++] = prop_atoms.net_wm_action_minimize;
1284     if (self->functions & OB_CLIENT_FUNC_RESIZE)
1285         actions[num++] = prop_atoms.net_wm_action_resize;
1286     if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1287         actions[num++] = prop_atoms.net_wm_action_fullscreen;
1288     if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1289         actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1290         actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1291     }
1292
1293     PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1294
1295     /* make sure the window isn't breaking any rules now */
1296
1297     if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1298         if (self->frame) client_shade(self, FALSE);
1299         else self->shaded = FALSE;
1300     }
1301     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY) && self->iconic) {
1302         if (self->frame) client_iconify(self, FALSE, TRUE);
1303         else self->iconic = FALSE;
1304     }
1305     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1306         if (self->frame) client_fullscreen(self, FALSE, TRUE);
1307         else self->fullscreen = FALSE;
1308     }
1309     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
1310                                                          self->max_vert)) {
1311         if (self->frame) client_maximize(self, FALSE, 0, TRUE);
1312         else self->max_vert = self->max_horz = FALSE;
1313     }
1314 }
1315
1316 void client_reconfigure(ObClient *self)
1317 {
1318     /* by making this pass FALSE for user, we avoid the emacs event storm where
1319        every configurenotify causes an update in its normal hints, i think this
1320        is generally what we want anyways... */
1321     client_configure(self, OB_CORNER_TOPLEFT, self->area.x, self->area.y,
1322                      self->area.width, self->area.height, FALSE, TRUE);
1323 }
1324
1325 void client_update_wmhints(ObClient *self)
1326 {
1327     XWMHints *hints;
1328     gboolean ur = FALSE;
1329     GSList *it;
1330
1331     /* assume a window takes input if it doesnt specify */
1332     self->can_focus = TRUE;
1333   
1334     if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1335         if (hints->flags & InputHint)
1336             self->can_focus = hints->input;
1337
1338         /* only do this when first managing the window *AND* when we aren't
1339            starting up! */
1340         if (ob_state() != OB_STATE_STARTING && self->frame == NULL)
1341             if (hints->flags & StateHint)
1342                 self->iconic = hints->initial_state == IconicState;
1343
1344         if (hints->flags & XUrgencyHint)
1345             ur = TRUE;
1346
1347         if (!(hints->flags & WindowGroupHint))
1348             hints->window_group = None;
1349
1350         /* did the group state change? */
1351         if (hints->window_group !=
1352             (self->group ? self->group->leader : None)) {
1353             /* remove from the old group if there was one */
1354             if (self->group != NULL) {
1355                 /* remove transients of the group */
1356                 for (it = self->group->members; it; it = it->next)
1357                     self->transients = g_slist_remove(self->transients,
1358                                                       it->data);
1359
1360                 /* remove myself from parents in the group */
1361                 if (self->transient_for == OB_TRAN_GROUP) {
1362                     for (it = self->group->members; it; it = it->next) {
1363                         ObClient *c = it->data;
1364
1365                         if (c != self && !c->transient_for)
1366                             c->transients = g_slist_remove(c->transients,
1367                                                            self);
1368                     }
1369                 }
1370
1371                 group_remove(self->group, self);
1372                 self->group = NULL;
1373             }
1374             if (hints->window_group != None) {
1375                 self->group = group_add(hints->window_group, self);
1376
1377                 /* i can only have transients from the group if i am not
1378                    transient myself */
1379                 if (!self->transient_for) {
1380                     /* add other transients of the group that are already
1381                        set up */
1382                     for (it = self->group->members; it; it = it->next) {
1383                         ObClient *c = it->data;
1384                         if (c != self && c->transient_for == OB_TRAN_GROUP)
1385                             self->transients =
1386                                 g_slist_append(self->transients, c);
1387                     }
1388                 }
1389             }
1390
1391             /* because the self->transient flag wont change from this call,
1392                we don't need to update the window's type and such, only its
1393                transient_for, and the transients lists of other windows in
1394                the group may be affected */
1395             client_update_transient_for(self);
1396         }
1397
1398         /* the WM_HINTS can contain an icon */
1399         client_update_icons(self);
1400
1401         XFree(hints);
1402     }
1403
1404     if (ur != self->urgent) {
1405         self->urgent = ur;
1406         /* fire the urgent callback if we're mapped, otherwise, wait until
1407            after we're mapped */
1408         if (self->frame)
1409             client_urgent_notify(self);
1410     }
1411 }
1412
1413 void client_update_title(ObClient *self)
1414 {
1415     GList *it;
1416     guint32 nums;
1417     guint i;
1418     gchar *data = NULL;
1419     gboolean read_title;
1420     gchar *old_title;
1421
1422     old_title = self->title;
1423      
1424     /* try netwm */
1425     if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1426         /* try old x stuff */
1427         if (!PROP_GETS(self->window, wm_name, locale, &data))
1428             data = g_strdup("Unnamed Window");
1429
1430     /* did the title change? then reset the title_count */
1431     if (old_title && 0 != strncmp(old_title, data, strlen(data)))
1432         self->title_count = 1;
1433
1434     /* look for duplicates and append a number */
1435     nums = 0;
1436     for (it = client_list; it; it = it->next)
1437         if (it->data != self) {
1438             ObClient *c = it->data;
1439             if (0 == strncmp(c->title, data, strlen(data)))
1440                 nums |= 1 << c->title_count;
1441         }
1442     /* find first free number */
1443     for (i = 1; i <= 32; ++i)
1444         if (!(nums & (1 << i))) {
1445             if (self->title_count == 1 || i == 1)
1446                 self->title_count = i;
1447             break;
1448         }
1449     /* dont display the number for the first window */
1450     if (self->title_count > 1) {
1451         char *ndata;
1452         ndata = g_strdup_printf("%s - [%u]", data, self->title_count);
1453         g_free(data);
1454         data = ndata;
1455     }
1456
1457     PROP_SETS(self->window, net_wm_visible_name, data);
1458
1459     self->title = data;
1460
1461     if (self->frame)
1462         frame_adjust_title(self->frame);
1463
1464     g_free(old_title);
1465
1466     /* update the icon title */
1467     data = NULL;
1468     g_free(self->icon_title);
1469
1470     read_title = TRUE;
1471     /* try netwm */
1472     if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1473         /* try old x stuff */
1474         if (!PROP_GETS(self->window, wm_icon_name, locale, &data)) {
1475             data = g_strdup(self->title);
1476             read_title = FALSE;
1477         }
1478
1479     /* append the title count, dont display the number for the first window */
1480     if (read_title && self->title_count > 1) {
1481         char *vdata, *ndata;
1482         ndata = g_strdup_printf(" - [%u]", self->title_count);
1483         vdata = g_strconcat(data, ndata, NULL);
1484         g_free(ndata);
1485         g_free(data);
1486         data = vdata;
1487     }
1488
1489     PROP_SETS(self->window, net_wm_visible_icon_name, data);
1490
1491     self->icon_title = data;
1492 }
1493
1494 void client_update_class(ObClient *self)
1495 {
1496     char **data;
1497     char *s;
1498
1499     if (self->name) g_free(self->name);
1500     if (self->class) g_free(self->class);
1501     if (self->role) g_free(self->role);
1502
1503     self->name = self->class = self->role = NULL;
1504
1505     if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1506         if (data[0]) {
1507             self->name = g_strdup(data[0]);
1508             if (data[1])
1509                 self->class = g_strdup(data[1]);
1510         }
1511         g_strfreev(data);     
1512     }
1513
1514     if (PROP_GETS(self->window, wm_window_role, locale, &s))
1515         self->role = s;
1516
1517     if (self->name == NULL) self->name = g_strdup("");
1518     if (self->class == NULL) self->class = g_strdup("");
1519     if (self->role == NULL) self->role = g_strdup("");
1520 }
1521
1522 void client_update_strut(ObClient *self)
1523 {
1524     guint num;
1525     guint32 *data;
1526     gboolean got = FALSE;
1527     StrutPartial strut;
1528
1529     if (PROP_GETA32(self->window, net_wm_strut_partial, cardinal,
1530                     &data, &num)) {
1531         if (num == 12) {
1532             got = TRUE;
1533             STRUT_PARTIAL_SET(strut,
1534                               data[0], data[2], data[1], data[3],
1535                               data[4], data[5], data[8], data[9],
1536                               data[6], data[7], data[10], data[11]);
1537         }
1538         g_free(data);
1539     }
1540
1541     if (!got &&
1542         PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1543         if (num == 4) {
1544             got = TRUE;
1545             STRUT_PARTIAL_SET(strut,
1546                               data[0], data[2], data[1], data[3],
1547                               0, 0, 0, 0, 0, 0, 0, 0);
1548         }
1549         g_free(data);
1550     }
1551
1552     if (!got)
1553         STRUT_PARTIAL_SET(strut, 0, 0, 0, 0,
1554                           0, 0, 0, 0, 0, 0, 0, 0);
1555
1556     if (!STRUT_EQUAL(strut, self->strut)) {
1557         self->strut = strut;
1558
1559         /* updating here is pointless while we're being mapped cuz we're not in
1560            the client list yet */
1561         if (self->frame)
1562             screen_update_areas();
1563     }
1564 }
1565
1566 void client_update_icons(ObClient *self)
1567 {
1568     guint num;
1569     guint32 *data;
1570     guint w, h, i, j;
1571
1572     for (i = 0; i < self->nicons; ++i)
1573         g_free(self->icons[i].data);
1574     if (self->nicons > 0)
1575         g_free(self->icons);
1576     self->nicons = 0;
1577
1578     if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1579         /* figure out how many valid icons are in here */
1580         i = 0;
1581         while (num - i > 2) {
1582             w = data[i++];
1583             h = data[i++];
1584             i += w * h;
1585             if (i > num || w*h == 0) break;
1586             ++self->nicons;
1587         }
1588
1589         self->icons = g_new(ObClientIcon, self->nicons);
1590     
1591         /* store the icons */
1592         i = 0;
1593         for (j = 0; j < self->nicons; ++j) {
1594             guint x, y, t;
1595
1596             w = self->icons[j].width = data[i++];
1597             h = self->icons[j].height = data[i++];
1598
1599             if (w*h == 0) continue;
1600
1601             self->icons[j].data = g_new(RrPixel32, w * h);
1602             for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1603                 if (x >= w) {
1604                     x = 0;
1605                     ++y;
1606                 }
1607                 self->icons[j].data[t] =
1608                     (((data[i] >> 24) & 0xff) << RrDefaultAlphaOffset) +
1609                     (((data[i] >> 16) & 0xff) << RrDefaultRedOffset) +
1610                     (((data[i] >> 8) & 0xff) << RrDefaultGreenOffset) +
1611                     (((data[i] >> 0) & 0xff) << RrDefaultBlueOffset);
1612             }
1613             g_assert(i <= num);
1614         }
1615
1616         g_free(data);
1617     } else if (PROP_GETA32(self->window, kwm_win_icon,
1618                            kwm_win_icon, &data, &num)) {
1619         if (num == 2) {
1620             self->nicons++;
1621             self->icons = g_new(ObClientIcon, self->nicons);
1622             xerror_set_ignore(TRUE);
1623             if (!RrPixmapToRGBA(ob_rr_inst,
1624                                 data[0], data[1],
1625                                 &self->icons[self->nicons-1].width,
1626                                 &self->icons[self->nicons-1].height,
1627                                 &self->icons[self->nicons-1].data)) {
1628                 g_free(&self->icons[self->nicons-1]);
1629                 self->nicons--;
1630             }
1631             xerror_set_ignore(FALSE);
1632         }
1633         g_free(data);
1634     } else {
1635         XWMHints *hints;
1636
1637         if ((hints = XGetWMHints(ob_display, self->window))) {
1638             if (hints->flags & IconPixmapHint) {
1639                 self->nicons++;
1640                 self->icons = g_new(ObClientIcon, self->nicons);
1641                 xerror_set_ignore(TRUE);
1642                 if (!RrPixmapToRGBA(ob_rr_inst,
1643                                     hints->icon_pixmap,
1644                                     (hints->flags & IconMaskHint ?
1645                                      hints->icon_mask : None),
1646                                     &self->icons[self->nicons-1].width,
1647                                     &self->icons[self->nicons-1].height,
1648                                     &self->icons[self->nicons-1].data)){
1649                     g_free(&self->icons[self->nicons-1]);
1650                     self->nicons--;
1651                 }
1652                 xerror_set_ignore(FALSE);
1653             }
1654             XFree(hints);
1655         }
1656     }
1657
1658     if (self->frame)
1659         frame_adjust_icon(self->frame);
1660 }
1661
1662 static void client_change_state(ObClient *self)
1663 {
1664     guint32 state[2];
1665     guint32 netstate[11];
1666     guint num;
1667
1668     state[0] = self->wmstate;
1669     state[1] = None;
1670     PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1671
1672     num = 0;
1673     if (self->modal)
1674         netstate[num++] = prop_atoms.net_wm_state_modal;
1675     if (self->shaded)
1676         netstate[num++] = prop_atoms.net_wm_state_shaded;
1677     if (self->iconic)
1678         netstate[num++] = prop_atoms.net_wm_state_hidden;
1679     if (self->skip_taskbar)
1680         netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1681     if (self->skip_pager)
1682         netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1683     if (self->fullscreen)
1684         netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1685     if (self->max_vert)
1686         netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1687     if (self->max_horz)
1688         netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1689     if (self->above)
1690         netstate[num++] = prop_atoms.net_wm_state_above;
1691     if (self->below)
1692         netstate[num++] = prop_atoms.net_wm_state_below;
1693     if (self->undecorated)
1694         netstate[num++] = prop_atoms.ob_wm_state_undecorated;
1695     PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1696
1697     client_calc_layer(self);
1698
1699     if (self->frame)
1700         frame_adjust_state(self->frame);
1701 }
1702
1703 ObClient *client_search_focus_tree(ObClient *self)
1704 {
1705     GSList *it;
1706     ObClient *ret;
1707
1708     for (it = self->transients; it != NULL; it = it->next) {
1709         if (client_focused(it->data)) return it->data;
1710         if ((ret = client_search_focus_tree(it->data))) return ret;
1711     }
1712     return NULL;
1713 }
1714
1715 ObClient *client_search_focus_tree_full(ObClient *self)
1716 {
1717     if (self->transient_for) {
1718         if (self->transient_for != OB_TRAN_GROUP) {
1719             return client_search_focus_tree_full(self->transient_for);
1720         } else {
1721             GSList *it;
1722             gboolean recursed = FALSE;
1723         
1724             for (it = self->group->members; it; it = it->next)
1725                 if (!((ObClient*)it->data)->transient_for) {
1726                     ObClient *c;
1727                     if ((c = client_search_focus_tree_full(it->data)))
1728                         return c;
1729                     recursed = TRUE;
1730                 }
1731             if (recursed)
1732                 return NULL;
1733         }
1734     }
1735
1736     /* this function checks the whole tree, the client_search_focus_tree~
1737        does not, so we need to check this window */
1738     if (client_focused(self))
1739         return self;
1740     return client_search_focus_tree(self);
1741 }
1742
1743 static ObStackingLayer calc_layer(ObClient *self)
1744 {
1745     ObStackingLayer l;
1746
1747     if (self->fullscreen &&
1748         (client_focused(self) || client_search_focus_tree(self)))
1749         l = OB_STACKING_LAYER_FULLSCREEN;
1750     else if (self->type == OB_CLIENT_TYPE_DESKTOP)
1751         l = OB_STACKING_LAYER_DESKTOP;
1752     else if (self->type == OB_CLIENT_TYPE_DOCK) {
1753         if (self->above) l = OB_STACKING_LAYER_DOCK_ABOVE;
1754         else if (self->below) l = OB_STACKING_LAYER_DOCK_BELOW;
1755         else l = OB_STACKING_LAYER_NORMAL;
1756     }
1757     else if (self->above) l = OB_STACKING_LAYER_ABOVE;
1758     else if (self->below) l = OB_STACKING_LAYER_BELOW;
1759     else l = OB_STACKING_LAYER_NORMAL;
1760
1761     return l;
1762 }
1763
1764 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
1765                                         ObStackingLayer l, gboolean raised)
1766 {
1767     ObStackingLayer old, own;
1768     GSList *it;
1769
1770     old = self->layer;
1771     own = calc_layer(self);
1772     self->layer = l > own ? l : own;
1773
1774     for (it = self->transients; it; it = it->next)
1775         client_calc_layer_recursive(it->data, orig,
1776                                     l, raised ? raised : l != old);
1777
1778     if (!raised && l != old)
1779         if (orig->frame) { /* only restack if the original window is managed */
1780             stacking_remove(CLIENT_AS_WINDOW(self));
1781             stacking_add(CLIENT_AS_WINDOW(self));
1782         }
1783 }
1784
1785 void client_calc_layer(ObClient *self)
1786 {
1787     ObStackingLayer l;
1788     ObClient *orig;
1789
1790     orig = self;
1791
1792     /* transients take on the layer of their parents */
1793     self = client_search_top_transient(self);
1794
1795     l = calc_layer(self);
1796
1797     client_calc_layer_recursive(self, orig, l, FALSE);
1798 }
1799
1800 gboolean client_should_show(ObClient *self)
1801 {
1802     if (self->iconic)
1803         return FALSE;
1804     if (client_normal(self) && screen_showing_desktop)
1805         return FALSE;
1806     /*
1807     if (self->transient_for) {
1808         if (self->transient_for != OB_TRAN_GROUP)
1809             return client_should_show(self->transient_for);
1810         else {
1811             GSList *it;
1812
1813             for (it = self->group->members; it; it = g_slist_next(it)) {
1814                 ObClient *c = it->data;
1815                 if (c != self && !c->transient_for) {
1816                     if (client_should_show(c))
1817                         return TRUE;
1818                 }
1819             }
1820         }
1821     }
1822     */
1823     if (self->desktop == screen_desktop || self->desktop == DESKTOP_ALL)
1824         return TRUE;
1825     
1826     return FALSE;
1827 }
1828
1829 static void client_showhide(ObClient *self)
1830 {
1831
1832     if (client_should_show(self))
1833         frame_show(self->frame);
1834     else
1835         frame_hide(self->frame);
1836 }
1837
1838 gboolean client_normal(ObClient *self) {
1839     return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
1840               self->type == OB_CLIENT_TYPE_DOCK ||
1841               self->type == OB_CLIENT_TYPE_SPLASH);
1842 }
1843
1844 static void client_apply_startup_state(ObClient *self)
1845 {
1846     /* these are in a carefully crafted order.. */
1847
1848     if (self->iconic) {
1849         self->iconic = FALSE;
1850         client_iconify(self, TRUE, FALSE);
1851     }
1852     if (self->fullscreen) {
1853         self->fullscreen = FALSE;
1854         client_fullscreen(self, TRUE, FALSE);
1855     }
1856     if (self->undecorated) {
1857         self->undecorated = FALSE;
1858         client_set_undecorated(self, TRUE);
1859     }
1860     if (self->shaded) {
1861         self->shaded = FALSE;
1862         client_shade(self, TRUE);
1863     }
1864     if (self->urgent)
1865         client_urgent_notify(self);
1866   
1867     if (self->max_vert && self->max_horz) {
1868         self->max_vert = self->max_horz = FALSE;
1869         client_maximize(self, TRUE, 0, FALSE);
1870     } else if (self->max_vert) {
1871         self->max_vert = FALSE;
1872         client_maximize(self, TRUE, 2, FALSE);
1873     } else if (self->max_horz) {
1874         self->max_horz = FALSE;
1875         client_maximize(self, TRUE, 1, FALSE);
1876     }
1877
1878     /* nothing to do for the other states:
1879        skip_taskbar
1880        skip_pager
1881        modal
1882        above
1883        below
1884     */
1885 }
1886
1887 void client_configure_full(ObClient *self, ObCorner anchor,
1888                            int x, int y, int w, int h,
1889                            gboolean user, gboolean final,
1890                            gboolean force_reply)
1891 {
1892     gint oldw, oldh;
1893     gboolean send_resize_client;
1894     gboolean moved = FALSE, resized = FALSE;
1895     guint fdecor = self->frame->decorations;
1896     gboolean fhorz = self->frame->max_horz;
1897
1898     /* make the frame recalculate its dimentions n shit without changing
1899        anything visible for real, this way the constraints below can work with
1900        the updated frame dimensions. */
1901     frame_adjust_area(self->frame, TRUE, TRUE, TRUE);
1902
1903     /* gets the frame's position */
1904     frame_client_gravity(self->frame, &x, &y);
1905
1906     /* these positions are frame positions, not client positions */
1907
1908     /* set the size and position if fullscreen */
1909     if (self->fullscreen) {
1910 #ifdef VIDMODE
1911         int dot;
1912         XF86VidModeModeLine mode;
1913 #endif
1914         Rect *a;
1915         guint i;
1916
1917         i = client_monitor(self);
1918         a = screen_physical_area_monitor(i);
1919
1920 #ifdef VIDMODE
1921         if (i == 0 && /* primary head */
1922             extensions_vidmode &&
1923             XF86VidModeGetViewPort(ob_display, ob_screen, &x, &y) &&
1924             /* get the mode last so the mode.privsize isnt freed incorrectly */
1925             XF86VidModeGetModeLine(ob_display, ob_screen, &dot, &mode)) {
1926             x += a->x;
1927             y += a->y;
1928             w = mode.hdisplay;
1929             h = mode.vdisplay;
1930             if (mode.privsize) XFree(mode.private);
1931         } else
1932 #endif
1933         {
1934             x = a->x;
1935             y = a->y;
1936             w = a->width;
1937             h = a->height;
1938         }
1939
1940         user = FALSE; /* ignore that increment etc shit when in fullscreen */
1941     } else {
1942         Rect *a;
1943
1944         a = screen_area_monitor(self->desktop, client_monitor(self));
1945
1946         /* set the size and position if maximized */
1947         if (self->max_horz) {
1948             x = a->x;
1949             w = a->width - self->frame->size.left - self->frame->size.right;
1950         }
1951         if (self->max_vert) {
1952             y = a->y;
1953             h = a->height - self->frame->size.top - self->frame->size.bottom;
1954         }
1955     }
1956
1957     /* gets the client's position */
1958     frame_frame_gravity(self->frame, &x, &y);
1959
1960     /* these override the above states! if you cant move you can't move! */
1961     if (user) {
1962         if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
1963             x = self->area.x;
1964             y = self->area.y;
1965         }
1966         if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
1967             w = self->area.width;
1968             h = self->area.height;
1969         }
1970     }
1971
1972     if (!(w == self->area.width && h == self->area.height)) {
1973         int basew, baseh, minw, minh;
1974
1975         /* base size is substituted with min size if not specified */
1976         if (self->base_size.width || self->base_size.height) {
1977             basew = self->base_size.width;
1978             baseh = self->base_size.height;
1979         } else {
1980             basew = self->min_size.width;
1981             baseh = self->min_size.height;
1982         }
1983         /* min size is substituted with base size if not specified */
1984         if (self->min_size.width || self->min_size.height) {
1985             minw = self->min_size.width;
1986             minh = self->min_size.height;
1987         } else {
1988             minw = self->base_size.width;
1989             minh = self->base_size.height;
1990         }
1991
1992         /* if this is a user-requested resize, then check against min/max
1993            sizes */
1994
1995         /* smaller than min size or bigger than max size? */
1996         if (w > self->max_size.width) w = self->max_size.width;
1997         if (w < minw) w = minw;
1998         if (h > self->max_size.height) h = self->max_size.height;
1999         if (h < minh) h = minh;
2000
2001         w -= basew;
2002         h -= baseh;
2003
2004         /* keep to the increments */
2005         w /= self->size_inc.width;
2006         h /= self->size_inc.height;
2007
2008         /* you cannot resize to nothing */
2009         if (basew + w < 1) w = 1 - basew;
2010         if (baseh + h < 1) h = 1 - baseh;
2011   
2012         /* store the logical size */
2013         SIZE_SET(self->logical_size,
2014                  self->size_inc.width > 1 ? w : w + basew,
2015                  self->size_inc.height > 1 ? h : h + baseh);
2016
2017         w *= self->size_inc.width;
2018         h *= self->size_inc.height;
2019
2020         w += basew;
2021         h += baseh;
2022
2023         /* adjust the height to match the width for the aspect ratios.
2024            for this, min size is not substituted for base size ever. */
2025         w -= self->base_size.width;
2026         h -= self->base_size.height;
2027
2028         if (self->min_ratio)
2029             if (h * self->min_ratio > w) {
2030                 h = (int)(w / self->min_ratio);
2031
2032                 /* you cannot resize to nothing */
2033                 if (h < 1) {
2034                     h = 1;
2035                     w = (int)(h * self->min_ratio);
2036                 }
2037             }
2038         if (self->max_ratio)
2039             if (h * self->max_ratio < w) {
2040                 h = (int)(w / self->max_ratio);
2041
2042                 /* you cannot resize to nothing */
2043                 if (h < 1) {
2044                     h = 1;
2045                     w = (int)(h * self->min_ratio);
2046                 }
2047             }
2048
2049         w += self->base_size.width;
2050         h += self->base_size.height;
2051     }
2052
2053     g_assert(w > 0);
2054     g_assert(h > 0);
2055
2056     switch (anchor) {
2057     case OB_CORNER_TOPLEFT:
2058         break;
2059     case OB_CORNER_TOPRIGHT:
2060         x -= w - self->area.width;
2061         break;
2062     case OB_CORNER_BOTTOMLEFT:
2063         y -= h - self->area.height;
2064         break;
2065     case OB_CORNER_BOTTOMRIGHT:
2066         x -= w - self->area.width;
2067         y -= h - self->area.height;
2068         break;
2069     }
2070
2071     moved = x != self->area.x || y != self->area.y;
2072     resized = w != self->area.width || h != self->area.height;
2073
2074     oldw = self->area.width;
2075     oldh = self->area.height;
2076     RECT_SET(self->area, x, y, w, h);
2077
2078     /* for app-requested resizes, always resize if 'resized' is true.
2079        for user-requested ones, only resize if final is true, or when
2080        resizing in redraw mode */
2081     send_resize_client = ((!user && resized) ||
2082                           (user && (final ||
2083                                     (resized && config_redraw_resize))));
2084
2085     /* if the client is enlarging, the resize the client before the frame */
2086     if (send_resize_client && user && (w > oldw || h > oldh))
2087         XResizeWindow(ob_display, self->window, MAX(w, oldw), MAX(h, oldh));
2088
2089     /* move/resize the frame to match the request */
2090     if (self->frame) {
2091         if (self->decorations != fdecor || self->max_horz != fhorz)
2092             moved = resized = TRUE;
2093
2094         if (moved || resized)
2095             frame_adjust_area(self->frame, moved, resized, FALSE);
2096
2097         if (!resized && (force_reply || ((!user && moved) || (user && final))))
2098         {
2099             XEvent event;
2100             event.type = ConfigureNotify;
2101             event.xconfigure.display = ob_display;
2102             event.xconfigure.event = self->window;
2103             event.xconfigure.window = self->window;
2104
2105             /* root window real coords */
2106             event.xconfigure.x = self->frame->area.x + self->frame->size.left -
2107                 self->border_width;
2108             event.xconfigure.y = self->frame->area.y + self->frame->size.top -
2109                 self->border_width;
2110             event.xconfigure.width = w;
2111             event.xconfigure.height = h;
2112             event.xconfigure.border_width = 0;
2113             event.xconfigure.above = self->frame->plate;
2114             event.xconfigure.override_redirect = FALSE;
2115             XSendEvent(event.xconfigure.display, event.xconfigure.window,
2116                        FALSE, StructureNotifyMask, &event);
2117         }
2118     }
2119
2120     /* if the client is shrinking, then resize the frame before the client */
2121     if (send_resize_client && (!user || (w <= oldw || h <= oldh)))
2122         XResizeWindow(ob_display, self->window, w, h);
2123
2124     XFlush(ob_display);
2125 }
2126
2127 void client_fullscreen(ObClient *self, gboolean fs, gboolean savearea)
2128 {
2129     int x, y, w, h;
2130
2131     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
2132         self->fullscreen == fs) return;                   /* already done */
2133
2134     self->fullscreen = fs;
2135     client_change_state(self); /* change the state hints on the client,
2136                                   and adjust out layer/stacking */
2137
2138     if (fs) {
2139         if (savearea)
2140             self->pre_fullscreen_area = self->area;
2141
2142         /* these are not actually used cuz client_configure will set them
2143            as appropriate when the window is fullscreened */
2144         x = y = w = h = 0;
2145     } else {
2146         Rect *a;
2147
2148         if (self->pre_fullscreen_area.width > 0 &&
2149             self->pre_fullscreen_area.height > 0)
2150         {
2151             x = self->pre_fullscreen_area.x;
2152             y = self->pre_fullscreen_area.y;
2153             w = self->pre_fullscreen_area.width;
2154             h = self->pre_fullscreen_area.height;
2155             RECT_SET(self->pre_fullscreen_area, 0, 0, 0, 0);
2156         } else {
2157             /* pick some fallbacks... */
2158             a = screen_area_monitor(self->desktop, 0);
2159             x = a->x + a->width / 4;
2160             y = a->y + a->height / 4;
2161             w = a->width / 2;
2162             h = a->height / 2;
2163         }
2164     }
2165
2166     client_setup_decor_and_functions(self);
2167
2168     client_move_resize(self, x, y, w, h);
2169
2170     /* try focus us when we go into fullscreen mode */
2171     client_focus(self);
2172 }
2173
2174 static void client_iconify_recursive(ObClient *self,
2175                                      gboolean iconic, gboolean curdesk)
2176 {
2177     GSList *it;
2178     gboolean changed = FALSE;
2179
2180
2181     if (self->iconic != iconic) {
2182         ob_debug("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"),
2183                  self->window);
2184
2185         self->iconic = iconic;
2186
2187         if (iconic) {
2188             if (self->functions & OB_CLIENT_FUNC_ICONIFY) {
2189                 long old;
2190
2191                 old = self->wmstate;
2192                 self->wmstate = IconicState;
2193                 if (old != self->wmstate)
2194                     PROP_MSG(self->window, kde_wm_change_state,
2195                              self->wmstate, 1, 0, 0);
2196
2197                 self->ignore_unmaps++;
2198                 /* we unmap the client itself so that we can get MapRequest
2199                    events, and because the ICCCM tells us to! */
2200                 XUnmapWindow(ob_display, self->window);
2201
2202                 /* update the focus lists.. iconic windows go to the bottom of
2203                    the list, put the new iconic window at the 'top of the
2204                    bottom'. */
2205                 focus_order_to_top(self);
2206
2207                 changed = TRUE;
2208             }
2209         } else {
2210             long old;
2211
2212             if (curdesk)
2213                 client_set_desktop(self, screen_desktop, FALSE);
2214
2215             old = self->wmstate;
2216             self->wmstate = self->shaded ? IconicState : NormalState;
2217             if (old != self->wmstate)
2218                 PROP_MSG(self->window, kde_wm_change_state,
2219                          self->wmstate, 1, 0, 0);
2220
2221             XMapWindow(ob_display, self->window);
2222
2223             /* this puts it after the current focused window */
2224             focus_order_remove(self);
2225             focus_order_add_new(self);
2226
2227             /* this is here cuz with the VIDMODE extension, the viewport can
2228                change while a fullscreen window is iconic, and when it
2229                uniconifies, it would be nice if it did so to the new position
2230                of the viewport */
2231             client_reconfigure(self);
2232
2233             changed = TRUE;
2234         }
2235     }
2236
2237     if (changed) {
2238         client_change_state(self);
2239         client_showhide(self);
2240         screen_update_areas();
2241     }
2242
2243     /* iconify all transients */
2244     for (it = self->transients; it != NULL; it = it->next)
2245         if (it->data != self) client_iconify_recursive(it->data,
2246                                                        iconic, curdesk);
2247 }
2248
2249 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk)
2250 {
2251     /* move up the transient chain as far as possible first */
2252     self = client_search_top_transient(self);
2253
2254     client_iconify_recursive(client_search_top_transient(self),
2255                              iconic, curdesk);
2256 }
2257
2258 void client_maximize(ObClient *self, gboolean max, int dir, gboolean savearea)
2259 {
2260     int x, y, w, h;
2261      
2262     g_assert(dir == 0 || dir == 1 || dir == 2);
2263     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE)) return; /* can't */
2264
2265     /* check if already done */
2266     if (max) {
2267         if (dir == 0 && self->max_horz && self->max_vert) return;
2268         if (dir == 1 && self->max_horz) return;
2269         if (dir == 2 && self->max_vert) return;
2270     } else {
2271         if (dir == 0 && !self->max_horz && !self->max_vert) return;
2272         if (dir == 1 && !self->max_horz) return;
2273         if (dir == 2 && !self->max_vert) return;
2274     }
2275
2276     /* we just tell it to configure in the same place and client_configure
2277        worries about filling the screen with the window */
2278     x = self->area.x;
2279     y = self->area.y;
2280     w = self->area.width;
2281     h = self->area.height;
2282
2283     if (max) {
2284         if (savearea) {
2285             if ((dir == 0 || dir == 1) && !self->max_horz) { /* horz */
2286                 RECT_SET(self->pre_max_area,
2287                          self->area.x, self->pre_max_area.y,
2288                          self->area.width, self->pre_max_area.height);
2289             }
2290             if ((dir == 0 || dir == 2) && !self->max_vert) { /* vert */
2291                 RECT_SET(self->pre_max_area,
2292                          self->pre_max_area.x, self->area.y,
2293                          self->pre_max_area.width, self->area.height);
2294             }
2295         }
2296     } else {
2297         Rect *a;
2298
2299         a = screen_area_monitor(self->desktop, 0);
2300         if ((dir == 0 || dir == 1) && self->max_horz) { /* horz */
2301             if (self->pre_max_area.width > 0) {
2302                 x = self->pre_max_area.x;
2303                 w = self->pre_max_area.width;
2304
2305                 RECT_SET(self->pre_max_area, 0, self->pre_max_area.y,
2306                          0, self->pre_max_area.height);
2307             } else {
2308                 /* pick some fallbacks... */
2309                 x = a->x + a->width / 4;
2310                 w = a->width / 2;
2311             }
2312         }
2313         if ((dir == 0 || dir == 2) && self->max_vert) { /* vert */
2314             if (self->pre_max_area.height > 0) {
2315                 y = self->pre_max_area.y;
2316                 h = self->pre_max_area.height;
2317
2318                 RECT_SET(self->pre_max_area, self->pre_max_area.x, 0,
2319                          self->pre_max_area.width, 0);
2320             } else {
2321                 /* pick some fallbacks... */
2322                 y = a->y + a->height / 4;
2323                 h = a->height / 2;
2324             }
2325         }
2326     }
2327
2328     if (dir == 0 || dir == 1) /* horz */
2329         self->max_horz = max;
2330     if (dir == 0 || dir == 2) /* vert */
2331         self->max_vert = max;
2332
2333     client_change_state(self); /* change the state hints on the client */
2334
2335     client_setup_decor_and_functions(self);
2336
2337     client_move_resize(self, x, y, w, h);
2338 }
2339
2340 void client_shade(ObClient *self, gboolean shade)
2341 {
2342     if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
2343          shade) ||                         /* can't shade */
2344         self->shaded == shade) return;     /* already done */
2345
2346     /* when we're iconic, don't change the wmstate */
2347     if (!self->iconic) {
2348         long old;
2349
2350         old = self->wmstate;
2351         self->wmstate = shade ? IconicState : NormalState;
2352         if (old != self->wmstate)
2353             PROP_MSG(self->window, kde_wm_change_state,
2354                      self->wmstate, 1, 0, 0);
2355     }
2356
2357     self->shaded = shade;
2358     client_change_state(self);
2359     /* resize the frame to just the titlebar */
2360     frame_adjust_area(self->frame, FALSE, FALSE, FALSE);
2361 }
2362
2363 void client_close(ObClient *self)
2364 {
2365     XEvent ce;
2366
2367     if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
2368
2369     /* in the case that the client provides no means to requesting that it
2370        close, we just kill it */
2371     if (!self->delete_window)
2372         client_kill(self);
2373     
2374     /*
2375       XXX: itd be cool to do timeouts and shit here for killing the client's
2376       process off
2377       like... if the window is around after 5 seconds, then the close button
2378       turns a nice red, and if this function is called again, the client is
2379       explicitly killed.
2380     */
2381
2382     ce.xclient.type = ClientMessage;
2383     ce.xclient.message_type =  prop_atoms.wm_protocols;
2384     ce.xclient.display = ob_display;
2385     ce.xclient.window = self->window;
2386     ce.xclient.format = 32;
2387     ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
2388     ce.xclient.data.l[1] = event_lasttime;
2389     ce.xclient.data.l[2] = 0l;
2390     ce.xclient.data.l[3] = 0l;
2391     ce.xclient.data.l[4] = 0l;
2392     XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2393 }
2394
2395 void client_kill(ObClient *self)
2396 {
2397     XKillClient(ob_display, self->window);
2398 }
2399
2400 void client_set_desktop_recursive(ObClient *self,
2401                                   guint target, gboolean donthide)
2402 {
2403     guint old;
2404     GSList *it;
2405
2406     if (target != self->desktop) {
2407
2408         ob_debug("Setting desktop %u\n", target+1);
2409
2410         g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2411
2412         /* remove from the old desktop(s) */
2413         focus_order_remove(self);
2414
2415         old = self->desktop;
2416         self->desktop = target;
2417         PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2418         /* the frame can display the current desktop state */
2419         frame_adjust_state(self->frame);
2420         /* 'move' the window to the new desktop */
2421         if (!donthide)
2422             client_showhide(self);
2423         /* raise if it was not already on the desktop */
2424         if (old != DESKTOP_ALL)
2425             client_raise(self);
2426         screen_update_areas();
2427
2428         /* add to the new desktop(s) */
2429         if (config_focus_new)
2430             focus_order_to_top(self);
2431         else
2432             focus_order_to_bottom(self);
2433     }
2434
2435     /* move all transients */
2436     for (it = self->transients; it != NULL; it = it->next)
2437         if (it->data != self) client_set_desktop_recursive(it->data,
2438                                                            target, donthide);
2439 }
2440
2441 void client_set_desktop(ObClient *self, guint target, gboolean donthide)
2442 {
2443     client_set_desktop_recursive(client_search_top_transient(self),
2444                                  target, donthide);
2445 }
2446
2447 ObClient *client_search_modal_child(ObClient *self)
2448 {
2449     GSList *it;
2450     ObClient *ret;
2451   
2452     for (it = self->transients; it != NULL; it = it->next) {
2453         ObClient *c = it->data;
2454         if ((ret = client_search_modal_child(c))) return ret;
2455         if (c->modal) return c;
2456     }
2457     return NULL;
2458 }
2459
2460 gboolean client_validate(ObClient *self)
2461 {
2462     XEvent e; 
2463
2464     XSync(ob_display, FALSE); /* get all events on the server */
2465
2466     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2467         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2468         XPutBackEvent(ob_display, &e);
2469         return FALSE;
2470     }
2471
2472     return TRUE;
2473 }
2474
2475 void client_set_wm_state(ObClient *self, long state)
2476 {
2477     if (state == self->wmstate) return; /* no change */
2478   
2479     switch (state) {
2480     case IconicState:
2481         client_iconify(self, TRUE, TRUE);
2482         break;
2483     case NormalState:
2484         client_iconify(self, FALSE, TRUE);
2485         break;
2486     }
2487 }
2488
2489 void client_set_state(ObClient *self, Atom action, long data1, long data2)
2490 {
2491     gboolean shaded = self->shaded;
2492     gboolean fullscreen = self->fullscreen;
2493     gboolean undecorated = self->undecorated;
2494     gboolean max_horz = self->max_horz;
2495     gboolean max_vert = self->max_vert;
2496     gboolean modal = self->modal;
2497     int i;
2498
2499     if (!(action == prop_atoms.net_wm_state_add ||
2500           action == prop_atoms.net_wm_state_remove ||
2501           action == prop_atoms.net_wm_state_toggle))
2502         /* an invalid action was passed to the client message, ignore it */
2503         return; 
2504
2505     for (i = 0; i < 2; ++i) {
2506         Atom state = i == 0 ? data1 : data2;
2507     
2508         if (!state) continue;
2509
2510         /* if toggling, then pick whether we're adding or removing */
2511         if (action == prop_atoms.net_wm_state_toggle) {
2512             if (state == prop_atoms.net_wm_state_modal)
2513                 action = modal ? prop_atoms.net_wm_state_remove :
2514                     prop_atoms.net_wm_state_add;
2515             else if (state == prop_atoms.net_wm_state_maximized_vert)
2516                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2517                     prop_atoms.net_wm_state_add;
2518             else if (state == prop_atoms.net_wm_state_maximized_horz)
2519                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2520                     prop_atoms.net_wm_state_add;
2521             else if (state == prop_atoms.net_wm_state_shaded)
2522                 action = shaded ? prop_atoms.net_wm_state_remove :
2523                     prop_atoms.net_wm_state_add;
2524             else if (state == prop_atoms.net_wm_state_skip_taskbar)
2525                 action = self->skip_taskbar ?
2526                     prop_atoms.net_wm_state_remove :
2527                     prop_atoms.net_wm_state_add;
2528             else if (state == prop_atoms.net_wm_state_skip_pager)
2529                 action = self->skip_pager ?
2530                     prop_atoms.net_wm_state_remove :
2531                     prop_atoms.net_wm_state_add;
2532             else if (state == prop_atoms.net_wm_state_fullscreen)
2533                 action = fullscreen ?
2534                     prop_atoms.net_wm_state_remove :
2535                     prop_atoms.net_wm_state_add;
2536             else if (state == prop_atoms.net_wm_state_above)
2537                 action = self->above ? prop_atoms.net_wm_state_remove :
2538                     prop_atoms.net_wm_state_add;
2539             else if (state == prop_atoms.net_wm_state_below)
2540                 action = self->below ? prop_atoms.net_wm_state_remove :
2541                     prop_atoms.net_wm_state_add;
2542             else if (state == prop_atoms.ob_wm_state_undecorated)
2543                 action = undecorated ? prop_atoms.net_wm_state_remove :
2544                     prop_atoms.net_wm_state_add;
2545         }
2546     
2547         if (action == prop_atoms.net_wm_state_add) {
2548             if (state == prop_atoms.net_wm_state_modal) {
2549                 modal = TRUE;
2550             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2551                 max_vert = TRUE;
2552             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2553                 max_horz = TRUE;
2554             } else if (state == prop_atoms.net_wm_state_shaded) {
2555                 shaded = TRUE;
2556             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2557                 self->skip_taskbar = TRUE;
2558             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2559                 self->skip_pager = TRUE;
2560             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2561                 fullscreen = TRUE;
2562             } else if (state == prop_atoms.net_wm_state_above) {
2563                 self->above = TRUE;
2564             } else if (state == prop_atoms.net_wm_state_below) {
2565                 self->below = TRUE;
2566             } else if (state == prop_atoms.ob_wm_state_undecorated) {
2567                 undecorated = TRUE;
2568             }
2569
2570         } else { /* action == prop_atoms.net_wm_state_remove */
2571             if (state == prop_atoms.net_wm_state_modal) {
2572                 modal = FALSE;
2573             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2574                 max_vert = FALSE;
2575             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2576                 max_horz = FALSE;
2577             } else if (state == prop_atoms.net_wm_state_shaded) {
2578                 shaded = FALSE;
2579             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2580                 self->skip_taskbar = FALSE;
2581             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2582                 self->skip_pager = FALSE;
2583             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2584                 fullscreen = FALSE;
2585             } else if (state == prop_atoms.net_wm_state_above) {
2586                 self->above = FALSE;
2587             } else if (state == prop_atoms.net_wm_state_below) {
2588                 self->below = FALSE;
2589             } else if (state == prop_atoms.ob_wm_state_undecorated) {
2590                 undecorated = FALSE;
2591             }
2592         }
2593     }
2594     if (max_horz != self->max_horz || max_vert != self->max_vert) {
2595         if (max_horz != self->max_horz && max_vert != self->max_vert) {
2596             /* toggling both */
2597             if (max_horz == max_vert) { /* both going the same way */
2598                 client_maximize(self, max_horz, 0, TRUE);
2599             } else {
2600                 client_maximize(self, max_horz, 1, TRUE);
2601                 client_maximize(self, max_vert, 2, TRUE);
2602             }
2603         } else {
2604             /* toggling one */
2605             if (max_horz != self->max_horz)
2606                 client_maximize(self, max_horz, 1, TRUE);
2607             else
2608                 client_maximize(self, max_vert, 2, TRUE);
2609         }
2610     }
2611     /* change fullscreen state before shading, as it will affect if the window
2612        can shade or not */
2613     if (fullscreen != self->fullscreen)
2614         client_fullscreen(self, fullscreen, TRUE);
2615     if (shaded != self->shaded)
2616         client_shade(self, shaded);
2617     if (undecorated != self->undecorated)
2618         client_set_undecorated(self, undecorated);
2619     if (modal != self->modal) {
2620         self->modal = modal;
2621         /* when a window changes modality, then its stacking order with its
2622            transients needs to change */
2623         client_raise(self);
2624     }
2625     client_calc_layer(self);
2626     client_change_state(self); /* change the hint to reflect these changes */
2627 }
2628
2629 ObClient *client_focus_target(ObClient *self)
2630 {
2631     ObClient *child;
2632      
2633     /* if we have a modal child, then focus it, not us */
2634     child = client_search_modal_child(client_search_top_transient(self));
2635     if (child) return child;
2636     return self;
2637 }
2638
2639 gboolean client_can_focus(ObClient *self)
2640 {
2641     XEvent ev;
2642
2643     /* choose the correct target */
2644     self = client_focus_target(self);
2645
2646     if (!self->frame->visible)
2647         return FALSE;
2648
2649     if (!(self->can_focus || self->focus_notify))
2650         return FALSE;
2651
2652     /* do a check to see if the window has already been unmapped or destroyed
2653        do this intelligently while watching out for unmaps we've generated
2654        (ignore_unmaps > 0) */
2655     if (XCheckTypedWindowEvent(ob_display, self->window,
2656                                DestroyNotify, &ev)) {
2657         XPutBackEvent(ob_display, &ev);
2658         return FALSE;
2659     }
2660     while (XCheckTypedWindowEvent(ob_display, self->window,
2661                                   UnmapNotify, &ev)) {
2662         if (self->ignore_unmaps) {
2663             self->ignore_unmaps--;
2664         } else {
2665             XPutBackEvent(ob_display, &ev);
2666             return FALSE;
2667         }
2668     }
2669
2670     return TRUE;
2671 }
2672
2673 gboolean client_focus(ObClient *self)
2674 {
2675     /* choose the correct target */
2676     self = client_focus_target(self);
2677
2678     if (!client_can_focus(self)) {
2679         if (!self->frame->visible) {
2680             /* update the focus lists */
2681             focus_order_to_top(self);
2682         }
2683         return FALSE;
2684     }
2685
2686     if (self->can_focus) {
2687         /* RevertToPointerRoot causes much more headache than RevertToNone, so
2688            I choose to use it always, hopefully to find errors quicker, if any
2689            are left. (I hate X. I hate focus events.)
2690            
2691            Update: Changing this to RevertToNone fixed a bug with mozilla (bug
2692            #799. So now it is RevertToNone again.
2693         */
2694         XSetInputFocus(ob_display, self->window, RevertToNone,
2695                        event_lasttime);
2696     }
2697
2698     if (self->focus_notify) {
2699         XEvent ce;
2700         ce.xclient.type = ClientMessage;
2701         ce.xclient.message_type = prop_atoms.wm_protocols;
2702         ce.xclient.display = ob_display;
2703         ce.xclient.window = self->window;
2704         ce.xclient.format = 32;
2705         ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2706         ce.xclient.data.l[1] = event_lasttime;
2707         ce.xclient.data.l[2] = 0l;
2708         ce.xclient.data.l[3] = 0l;
2709         ce.xclient.data.l[4] = 0l;
2710         XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2711     }
2712
2713 #ifdef DEBUG_FOCUS
2714     ob_debug("%sively focusing %lx at %d\n",
2715              (self->can_focus ? "act" : "pass"),
2716              self->window, (int) event_lasttime);
2717 #endif
2718
2719     /* Cause the FocusIn to come back to us. Important for desktop switches,
2720        since otherwise we'll have no FocusIn on the queue and send it off to
2721        the focus_backup. */
2722     XSync(ob_display, FALSE);
2723     return TRUE;
2724 }
2725
2726 void client_unfocus(ObClient *self)
2727 {
2728     if (focus_client == self) {
2729 #ifdef DEBUG_FOCUS
2730         ob_debug("client_unfocus for %lx\n", self->window);
2731 #endif
2732         focus_fallback(OB_FOCUS_FALLBACK_UNFOCUSING);
2733     }
2734 }
2735
2736 void client_activate(ObClient *self, gboolean here)
2737 {
2738     if (client_normal(self) && screen_showing_desktop)
2739         screen_show_desktop(FALSE);
2740     if (self->iconic)
2741         client_iconify(self, FALSE, here);
2742     if (self->desktop != DESKTOP_ALL &&
2743         self->desktop != screen_desktop) {
2744         if (here)
2745             client_set_desktop(self, screen_desktop, FALSE);
2746         else
2747             screen_set_desktop(self->desktop);
2748     } else if (!self->frame->visible)
2749         /* if its not visible for other reasons, then don't mess
2750            with it */
2751         return;
2752     if (self->shaded)
2753         client_shade(self, FALSE);
2754
2755     client_focus(self);
2756
2757     /* we do this an action here. this is rather important. this is because
2758        we want the results from the focus change to take place BEFORE we go
2759        about raising the window. when a fullscreen window loses focus, we need
2760        this or else the raise wont be able to raise above the to-lose-focus
2761        fullscreen window. */
2762     client_raise(self);
2763 }
2764
2765 void client_raise(ObClient *self)
2766 {
2767     action_run_string("Raise", self);
2768 }
2769
2770 void client_lower(ObClient *self)
2771 {
2772     action_run_string("Raise", self);
2773 }
2774
2775 gboolean client_focused(ObClient *self)
2776 {
2777     return self == focus_client;
2778 }
2779
2780 static ObClientIcon* client_icon_recursive(ObClient *self, int w, int h)
2781 {
2782     guint i;
2783     /* si is the smallest image >= req */
2784     /* li is the largest image < req */
2785     unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2786
2787     if (!self->nicons) {
2788         ObClientIcon *parent = NULL;
2789
2790         if (self->transient_for) {
2791             if (self->transient_for != OB_TRAN_GROUP)
2792                 parent = client_icon_recursive(self->transient_for, w, h);
2793             else {
2794                 GSList *it;
2795                 for (it = self->group->members; it; it = g_slist_next(it)) {
2796                     ObClient *c = it->data;
2797                     if (c != self && !c->transient_for) {
2798                         if ((parent = client_icon_recursive(c, w, h)))
2799                             break;
2800                     }
2801                 }
2802             }
2803         }
2804         
2805         return parent;
2806     }
2807
2808     for (i = 0; i < self->nicons; ++i) {
2809         size = self->icons[i].width * self->icons[i].height;
2810         if (size < smallest && size >= (unsigned)(w * h)) {
2811             smallest = size;
2812             si = i;
2813         }
2814         if (size > largest && size <= (unsigned)(w * h)) {
2815             largest = size;
2816             li = i;
2817         }
2818     }
2819     if (largest == 0) /* didnt find one smaller than the requested size */
2820         return &self->icons[si];
2821     return &self->icons[li];
2822 }
2823
2824 const ObClientIcon* client_icon(ObClient *self, int w, int h)
2825 {
2826     ObClientIcon *ret;
2827     static ObClientIcon deficon;
2828
2829     if (!(ret = client_icon_recursive(self, w, h))) {
2830         deficon.width = deficon.height = 48;
2831         deficon.data = ob_rr_theme->def_win_icon;
2832         ret = &deficon;
2833     }
2834     return ret;
2835 }
2836
2837 /* this be mostly ripped from fvwm */
2838 ObClient *client_find_directional(ObClient *c, ObDirection dir) 
2839 {
2840     int my_cx, my_cy, his_cx, his_cy;
2841     int offset = 0;
2842     int distance = 0;
2843     int score, best_score;
2844     ObClient *best_client, *cur;
2845     GList *it;
2846
2847     if(!client_list)
2848         return NULL;
2849
2850     /* first, find the centre coords of the currently focused window */
2851     my_cx = c->frame->area.x + c->frame->area.width / 2;
2852     my_cy = c->frame->area.y + c->frame->area.height / 2;
2853
2854     best_score = -1;
2855     best_client = NULL;
2856
2857     for(it = g_list_first(client_list); it; it = it->next) {
2858         cur = it->data;
2859
2860         /* the currently selected window isn't interesting */
2861         if(cur == c)
2862             continue;
2863         if (!client_normal(cur))
2864             continue;
2865         if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2866             continue;
2867         if(cur->iconic)
2868             continue;
2869         if(client_focus_target(cur) == cur &&
2870            !(cur->can_focus || cur->focus_notify))
2871             continue;
2872
2873         /* find the centre coords of this window, from the
2874          * currently focused window's point of view */
2875         his_cx = (cur->frame->area.x - my_cx)
2876             + cur->frame->area.width / 2;
2877         his_cy = (cur->frame->area.y - my_cy)
2878             + cur->frame->area.height / 2;
2879
2880         if(dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
2881            dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST) {
2882             int tx;
2883             /* Rotate the diagonals 45 degrees counterclockwise.
2884              * To do this, multiply the matrix /+h +h\ with the
2885              * vector (x y).                   \-h +h/
2886              * h = sqrt(0.5). We can set h := 1 since absolute
2887              * distance doesn't matter here. */
2888             tx = his_cx + his_cy;
2889             his_cy = -his_cx + his_cy;
2890             his_cx = tx;
2891         }
2892
2893         switch(dir) {
2894         case OB_DIRECTION_NORTH:
2895         case OB_DIRECTION_SOUTH:
2896         case OB_DIRECTION_NORTHEAST:
2897         case OB_DIRECTION_SOUTHWEST:
2898             offset = (his_cx < 0) ? -his_cx : his_cx;
2899             distance = ((dir == OB_DIRECTION_NORTH ||
2900                          dir == OB_DIRECTION_NORTHEAST) ?
2901                         -his_cy : his_cy);
2902             break;
2903         case OB_DIRECTION_EAST:
2904         case OB_DIRECTION_WEST:
2905         case OB_DIRECTION_SOUTHEAST:
2906         case OB_DIRECTION_NORTHWEST:
2907             offset = (his_cy < 0) ? -his_cy : his_cy;
2908             distance = ((dir == OB_DIRECTION_WEST ||
2909                          dir == OB_DIRECTION_NORTHWEST) ?
2910                         -his_cx : his_cx);
2911             break;
2912         }
2913
2914         /* the target must be in the requested direction */
2915         if(distance <= 0)
2916             continue;
2917
2918         /* Calculate score for this window.  The smaller the better. */
2919         score = distance + offset;
2920
2921         /* windows more than 45 degrees off the direction are
2922          * heavily penalized and will only be chosen if nothing
2923          * else within a million pixels */
2924         if(offset > distance)
2925             score += 1000000;
2926
2927         if(best_score == -1 || score < best_score)
2928             best_client = cur,
2929                 best_score = score;
2930     }
2931
2932     return best_client;
2933 }
2934
2935 void client_set_layer(ObClient *self, int layer)
2936 {
2937     if (layer < 0) {
2938         self->below = TRUE;
2939         self->above = FALSE;
2940     } else if (layer == 0) {
2941         self->below = self->above = FALSE;
2942     } else {
2943         self->below = FALSE;
2944         self->above = TRUE;
2945     }
2946     client_calc_layer(self);
2947     client_change_state(self); /* reflect this in the state hints */
2948 }
2949
2950 void client_set_undecorated(ObClient *self, gboolean undecorated)
2951 {
2952     if (self->undecorated != undecorated) {
2953         self->undecorated = undecorated;
2954         client_setup_decor_and_functions(self);
2955         client_change_state(self); /* reflect this in the state hints */
2956     }
2957 }
2958
2959 guint client_monitor(ObClient *self)
2960 {
2961     guint i;
2962     guint most = 0;
2963     guint mostv = 0;
2964
2965     for (i = 0; i < screen_num_monitors; ++i) {
2966         Rect *area = screen_physical_area_monitor(i);
2967         if (RECT_INTERSECTS_RECT(*area, self->frame->area)) {
2968             Rect r;
2969             guint v;
2970
2971             RECT_SET_INTERSECTION(r, *area, self->frame->area);
2972             v = r.width * r.height;
2973
2974             if (v > mostv) {
2975                 mostv = v;
2976                 most = i;
2977             }
2978         }
2979     }
2980     return most;
2981 }
2982
2983 ObClient *client_search_top_transient(ObClient *self)
2984 {
2985     /* move up the transient chain as far as possible */
2986     if (self->transient_for) {
2987         if (self->transient_for != OB_TRAN_GROUP) {
2988             return client_search_top_transient(self->transient_for);
2989         } else {
2990             GSList *it;
2991
2992             g_assert(self->group);
2993
2994             for (it = self->group->members; it; it = it->next) {
2995                 ObClient *c = it->data;
2996
2997                 /* checking transient_for prevents infinate loops! */
2998                 if (c != self && !c->transient_for)
2999                     break;
3000             }
3001             if (it)
3002                 return it->data;
3003         }
3004     }
3005
3006     return self;
3007 }
3008
3009 ObClient *client_search_focus_parent(ObClient *self)
3010 {
3011     if (self->transient_for) {
3012         if (self->transient_for != OB_TRAN_GROUP) {
3013             if (client_focused(self->transient_for))
3014                 return self->transient_for;
3015         } else {
3016             GSList *it;
3017
3018             for (it = self->group->members; it; it = it->next) {
3019                 ObClient *c = it->data;
3020
3021                 /* checking transient_for prevents infinate loops! */
3022                 if (c != self && !c->transient_for)
3023                     if (client_focused(c))
3024                         return c;
3025             }
3026         }
3027     }
3028
3029     return NULL;
3030 }
3031
3032 ObClient *client_search_parent(ObClient *self, ObClient *search)
3033 {
3034     if (self->transient_for) {
3035         if (self->transient_for != OB_TRAN_GROUP) {
3036             if (self->transient_for == search)
3037                 return search;
3038         } else {
3039             GSList *it;
3040
3041             for (it = self->group->members; it; it = it->next) {
3042                 ObClient *c = it->data;
3043
3044                 /* checking transient_for prevents infinate loops! */
3045                 if (c != self && !c->transient_for)
3046                     if (c == search)
3047                         return search;
3048             }
3049         }
3050     }
3051
3052     return NULL;
3053 }
3054
3055 ObClient *client_search_transient(ObClient *self, ObClient *search)
3056 {
3057     GSList *sit;
3058
3059     for (sit = self->transients; sit; sit = g_slist_next(sit)) {
3060         if (sit->data == search)
3061             return search;
3062         if (client_search_transient(sit->data, search))
3063             return search;
3064     }
3065     return NULL;
3066 }
3067
3068 void client_update_sm_client_id(ObClient *self)
3069 {
3070     g_free(self->sm_client_id);
3071     self->sm_client_id = NULL;
3072
3073     if (!PROP_GETS(self->window, sm_client_id, locale, &self->sm_client_id) &&
3074         self->group)
3075         PROP_GETS(self->group->leader, sm_client_id, locale,
3076                   &self->sm_client_id);
3077 }
3078
3079 /* finds the nearest edge in the given direction from the current client
3080  * note to self: the edge is the -frame- edge (the actual one), not the
3081  * client edge.
3082  */
3083 int client_directional_edge_search(ObClient *c, ObDirection dir)
3084 {
3085     int dest;
3086     int my_edge_start, my_edge_end, my_offset;
3087     GList *it;
3088     Rect *a;
3089     
3090     if(!client_list)
3091         return -1;
3092
3093     a = screen_area(c->desktop);
3094
3095     switch(dir) {
3096     case OB_DIRECTION_NORTH:
3097         my_edge_start = c->frame->area.x;
3098         my_edge_end = c->frame->area.x + c->frame->area.width;
3099         my_offset = c->frame->area.y;
3100         
3101         /* default: top of screen */
3102         dest = a->y;
3103
3104         for(it = g_list_first(client_list); it; it = it->next) {
3105             int his_edge_start, his_edge_end, his_offset;
3106             ObClient *cur = it->data;
3107
3108             if(cur == c)
3109                 continue;
3110             if(!client_normal(cur))
3111                 continue;
3112             if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
3113                 continue;
3114             if(cur->iconic)
3115                 continue;
3116
3117             his_edge_start = cur->frame->area.x;
3118             his_edge_end = cur->frame->area.x + cur->frame->area.width;
3119             his_offset = cur->frame->area.y + cur->frame->area.height;
3120
3121             if(his_offset + 1 > my_offset)
3122                 continue;
3123
3124             if(his_offset < dest)
3125                 continue;
3126             
3127             if(his_edge_start >= my_edge_start &&
3128                his_edge_start <= my_edge_end)
3129                 dest = his_offset;
3130
3131             if(my_edge_start >= his_edge_start &&
3132                my_edge_start <= his_edge_end)
3133                 dest = his_offset;
3134
3135         }
3136         break;
3137     case OB_DIRECTION_SOUTH:
3138         my_edge_start = c->frame->area.x;
3139         my_edge_end = c->frame->area.x + c->frame->area.width;
3140         my_offset = c->frame->area.y + c->frame->area.height;
3141
3142         /* default: bottom of screen */
3143         dest = a->y + a->height;
3144
3145         for(it = g_list_first(client_list); it; it = it->next) {
3146             int his_edge_start, his_edge_end, his_offset;
3147             ObClient *cur = it->data;
3148
3149             if(cur == c)
3150                 continue;
3151             if(!client_normal(cur))
3152                 continue;
3153             if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
3154                 continue;
3155             if(cur->iconic)
3156                 continue;
3157
3158             his_edge_start = cur->frame->area.x;
3159             his_edge_end = cur->frame->area.x + cur->frame->area.width;
3160             his_offset = cur->frame->area.y;
3161
3162
3163             if(his_offset - 1 < my_offset)
3164                 continue;
3165             
3166             if(his_offset > dest)
3167                 continue;
3168             
3169             if(his_edge_start >= my_edge_start &&
3170                his_edge_start <= my_edge_end)
3171                 dest = his_offset;
3172
3173             if(my_edge_start >= his_edge_start &&
3174                my_edge_start <= his_edge_end)
3175                 dest = his_offset;
3176
3177         }
3178         break;
3179     case OB_DIRECTION_WEST:
3180         my_edge_start = c->frame->area.y;
3181         my_edge_end = c->frame->area.y + c->frame->area.height;
3182         my_offset = c->frame->area.x;
3183
3184         /* default: leftmost egde of screen */
3185         dest = a->x;
3186
3187         for(it = g_list_first(client_list); it; it = it->next) {
3188             int his_edge_start, his_edge_end, his_offset;
3189             ObClient *cur = it->data;
3190
3191             if(cur == c)
3192                 continue;
3193             if(!client_normal(cur))
3194                 continue;
3195             if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
3196                 continue;
3197             if(cur->iconic)
3198                 continue;
3199
3200             his_edge_start = cur->frame->area.y;
3201             his_edge_end = cur->frame->area.y + cur->frame->area.height;
3202             his_offset = cur->frame->area.x + cur->frame->area.width;
3203
3204             if(his_offset + 1 > my_offset)
3205                 continue;
3206             
3207             if(his_offset < dest)
3208                 continue;
3209             
3210             if(his_edge_start >= my_edge_start &&
3211                his_edge_start <= my_edge_end)
3212                 dest = his_offset;
3213
3214             if(my_edge_start >= his_edge_start &&
3215                my_edge_start <= his_edge_end)
3216                 dest = his_offset;
3217                 
3218
3219         }
3220         break;
3221     case OB_DIRECTION_EAST:
3222         my_edge_start = c->frame->area.y;
3223         my_edge_end = c->frame->area.y + c->frame->area.height;
3224         my_offset = c->frame->area.x + c->frame->area.width;
3225         
3226         /* default: rightmost edge of screen */
3227         dest = a->x + a->width;
3228
3229         for(it = g_list_first(client_list); it; it = it->next) {
3230             int his_edge_start, his_edge_end, his_offset;
3231             ObClient *cur = it->data;
3232
3233             if(cur == c)
3234                 continue;
3235             if(!client_normal(cur))
3236                 continue;
3237             if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
3238                 continue;
3239             if(cur->iconic)
3240                 continue;
3241
3242             his_edge_start = cur->frame->area.y;
3243             his_edge_end = cur->frame->area.y + cur->frame->area.height;
3244             his_offset = cur->frame->area.x;
3245
3246             if(his_offset - 1 < my_offset)
3247                 continue;
3248             
3249             if(his_offset > dest)
3250                 continue;
3251             
3252             if(his_edge_start >= my_edge_start &&
3253                his_edge_start <= my_edge_end)
3254                 dest = his_offset;
3255
3256             if(my_edge_start >= his_edge_start &&
3257                my_edge_start <= his_edge_end)
3258                 dest = his_offset;
3259
3260         }
3261         break;
3262     case OB_DIRECTION_NORTHEAST:
3263     case OB_DIRECTION_SOUTHEAST:
3264     case OB_DIRECTION_NORTHWEST:
3265     case OB_DIRECTION_SOUTHWEST:
3266         /* not implemented */
3267     default:
3268         g_assert_not_reached();
3269     }
3270     return dest;
3271 }
3272
3273 ObClient* client_under_pointer()
3274 {
3275     int x, y;
3276     GList *it;
3277     ObClient *ret = NULL;
3278
3279     if (screen_pointer_pos(&x, &y)) {
3280         for (it = stacking_list; it != NULL; it = it->next) {
3281             if (WINDOW_IS_CLIENT(it->data)) {
3282                 ObClient *c = WINDOW_AS_CLIENT(it->data);
3283                 if (c->frame->visible &&
3284                     RECT_CONTAINS(c->frame->area, x, y)) {
3285                     ret = c;
3286                     break;
3287                 }
3288             }
3289         }
3290     }
3291     return ret;
3292 }
3293
3294 gboolean client_has_group_siblings(ObClient *self)
3295 {
3296     return self->group && self->group->members->next;
3297 }