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