]> icculus.org git repositories - manmower/openbox.git/blob - openbox/client.c
missed some stuff?
[manmower/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             /* Match if no role was specified in the per app setting, or if the
643              * string matches the beginning of the role, since apps like to set
644              * the role to things like browser-window-23c4b2f */
645             if (!app->role
646                 || !strncmp(app->role, self->role, strlen(app->role)))
647             {
648                 ob_debug("Window matching: %s\n", app->name);
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) {
677             if (settings->desktop == DESKTOP_ALL)
678                 self->desktop = settings->desktop;
679             else if (settings->desktop > 0 &&
680                      settings->desktop <= screen_num_desktops)
681                 self->desktop = settings->desktop - 1;
682         }
683
684         if (settings->layer == -1) {
685             self->below = TRUE;
686             self->above = FALSE;
687         }
688         else if (settings->layer == 0) {
689             self->below = FALSE;
690             self->above = FALSE;
691         }
692         else if (settings->layer == 1) {
693             self->below = FALSE;
694             self->above = TRUE;
695         }
696     }
697     return settings;
698 }
699
700 static void client_restore_session_state(ObClient *self)
701 {
702     GList *it;
703
704     ob_debug_type(OB_DEBUG_SM,
705                   "Restore session for client %s\n", self->title);
706
707     if (!(it = session_state_find(self))) {
708         ob_debug_type(OB_DEBUG_SM,
709                       "Session data not found for client %s\n", self->title);
710         return;
711     }
712
713     self->session = it->data;
714
715     ob_debug_type(OB_DEBUG_SM, "Session data loaded for client %s\n",
716                   self->title);
717
718     RECT_SET_POINT(self->area, self->session->x, self->session->y);
719     self->positioned = USPosition;
720     if (self->session->w > 0)
721         self->area.width = self->session->w;
722     if (self->session->h > 0)
723         self->area.height = self->session->h;
724     XResizeWindow(ob_display, self->window,
725                   self->area.width, self->area.height);
726
727     self->desktop = (self->session->desktop == DESKTOP_ALL ?
728                      self->session->desktop :
729                      MIN(screen_num_desktops - 1, self->session->desktop));
730     PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
731
732     self->shaded = self->session->shaded;
733     self->iconic = self->session->iconic;
734     self->skip_pager = self->session->skip_pager;
735     self->skip_taskbar = self->session->skip_taskbar;
736     self->fullscreen = self->session->fullscreen;
737     self->above = self->session->above;
738     self->below = self->session->below;
739     self->max_horz = self->session->max_horz;
740     self->max_vert = self->session->max_vert;
741     self->undecorated = self->session->undecorated;
742 }
743
744 static gboolean client_restore_session_stacking(ObClient *self)
745 {
746     GList *it, *mypos;
747
748     if (!self->session) return FALSE;
749
750     mypos = g_list_find(session_saved_state, self->session);
751     if (!mypos) return FALSE;
752
753     /* start above me and look for the first client */
754     for (it = g_list_previous(mypos); it; it = g_list_previous(it)) {
755         GList *cit;
756
757         for (cit = client_list; cit; cit = g_list_next(cit)) {
758             ObClient *c = cit->data;
759             /* found a client that was in the session, so go below it */
760             if (c->session == it->data) {
761                 stacking_below(CLIENT_AS_WINDOW(self),
762                                CLIENT_AS_WINDOW(cit->data));
763                 return TRUE;
764             }
765         }
766     }
767     return FALSE;
768 }
769
770 void client_move_onscreen(ObClient *self, gboolean rude)
771 {
772     gint x = self->area.x;
773     gint y = self->area.y;
774     if (client_find_onscreen(self, &x, &y,
775                              self->area.width,
776                              self->area.height, rude)) {
777         client_move(self, x, y);
778     }
779 }
780
781 gboolean client_find_onscreen(ObClient *self, gint *x, gint *y, gint w, gint h,
782                               gboolean rude)
783 {
784     Rect *mon_a, *all_a;
785     gint ox = *x, oy = *y;
786     gboolean rudel = rude, ruder = rude, rudet = rude, rudeb = rude;
787     gint fw, fh;
788
789     all_a = screen_area(self->desktop);
790     mon_a = screen_area_monitor(self->desktop, client_monitor(self));
791
792     /* get where the frame would be */
793     frame_client_gravity(self->frame, x, y, w, h);
794
795     /* get the requested size of the window with decorations */
796     fw = self->frame->size.left + w + self->frame->size.right;
797     fh = self->frame->size.top + h + self->frame->size.bottom;
798
799     /* This makes sure windows aren't entirely outside of the screen so you
800        can't see them at all.
801        It makes sure 10% of the window is on the screen at least. At don't let
802        it move itself off the top of the screen, which would hide the titlebar
803        on you. (The user can still do this if they want too, it's only limiting
804        the application.
805
806        XXX watch for xinerama dead areas...
807     */
808     if (client_normal(self)) {
809         if (!self->strut.right && *x + fw/10 >= all_a->x + all_a->width - 1)
810             *x = all_a->x + all_a->width - fw/10;
811         if (!self->strut.bottom && *y + fh/10 >= all_a->y + all_a->height - 1)
812             *y = all_a->y + all_a->height - fh/10;
813         if (!self->strut.left && *x + fw*9/10 - 1 < all_a->x)
814             *x = all_a->x - fw*9/10;
815         if (!self->strut.top && *y + fh*9/10 - 1 < all_a->y)
816             *y = all_a->y - fw*9/10;
817     }
818
819     /* If rudeness wasn't requested, then figure out of the client is currently
820        entirely on the screen. If it is, and the position isn't changing by
821        request, and it is enlarging, then be rude even though it wasn't
822        requested */
823     if (!rude) {
824         Point oldtl, oldtr, oldbl, oldbr;
825         Point newtl, newtr, newbl, newbr;
826         gboolean stationary_l, stationary_r, stationary_t, stationary_b;
827
828         POINT_SET(oldtl, self->frame->area.x, self->frame->area.y);
829         POINT_SET(oldbr, self->frame->area.x + self->frame->area.width - 1,
830                   self->frame->area.y + self->frame->area.height - 1);
831         POINT_SET(oldtr, oldbr.x, oldtl.y);
832         POINT_SET(oldbl, oldtl.x, oldbr.y);
833
834         POINT_SET(newtl, *x, *y);
835         POINT_SET(newbr, *x + fw - 1, *y + fh - 1);
836         POINT_SET(newtr, newbr.x, newtl.y);
837         POINT_SET(newbl, newtl.x, newbr.y);
838
839         /* is it moving or just resizing from some corner? */
840         stationary_l = oldtl.x == oldtl.x;
841         stationary_r = oldtr.x == oldtr.x;
842         stationary_t = oldtl.y == oldtl.y;
843         stationary_b = oldbl.y == oldbl.y;
844
845         /* if left edge is growing and didnt move right edge */
846         if (stationary_r && newtl.x < oldtl.x)
847             rudel = TRUE;
848         /* if right edge is growing and didnt move left edge */
849         if (stationary_l && newtr.x > oldtr.x)
850             ruder = TRUE;
851         /* if top edge is growing and didnt move bottom edge */
852         if (stationary_b && newtl.y < oldtl.y)
853             rudet = TRUE;
854         /* if bottom edge is growing and didnt move top edge */
855         if (stationary_t && newbl.y > oldbl.y)
856             rudeb = TRUE;
857     }
858
859     /* This here doesn't let windows even a pixel outside the struts/screen.
860      * When called from client_manage, programs placing themselves are
861      * forced completely onscreen, while things like
862      * xterm -geometry resolution-width/2 will work fine. Trying to
863      * place it completely offscreen will be handled in the above code.
864      * Sorry for this confused comment, i am tired. */
865     if (fw <= mon_a->width) {
866         if (rudel && !self->strut.left && *x < mon_a->x) *x = mon_a->x;
867         if (ruder && !self->strut.right && *x + fw > mon_a->x + mon_a->width)
868             *x = mon_a->x + mon_a->width - fw;
869     }
870     if (fh <= mon_a->height) {
871         if (rudet && !self->strut.top && *y < mon_a->y) *y = mon_a->y;
872         if (rudeb && !self->strut.bottom && *y + fh > mon_a->y + mon_a->height)
873             *y = mon_a->y + mon_a->height - fh;
874     }
875
876     /* get where the client should be */
877     frame_frame_gravity(self->frame, x, y, w, h);
878
879     return ox != *x || oy != *y;
880 }
881
882 static void client_toggle_border(ObClient *self, gboolean show)
883 {
884     /* adjust our idea of where the client is, based on its border. When the
885        border is removed, the client should now be considered to be in a
886        different position.
887        when re-adding the border to the client, the same operation needs to be
888        reversed. */
889     gint oldx = self->area.x, oldy = self->area.y;
890     gint x = oldx, y = oldy;
891     switch(self->gravity) {
892     default:
893     case NorthWestGravity:
894     case WestGravity:
895     case SouthWestGravity:
896         break;
897     case NorthEastGravity:
898     case EastGravity:
899     case SouthEastGravity:
900         if (show) x -= self->border_width * 2;
901         else      x += self->border_width * 2;
902         break;
903     case NorthGravity:
904     case SouthGravity:
905     case CenterGravity:
906     case ForgetGravity:
907     case StaticGravity:
908         if (show) x -= self->border_width;
909         else      x += self->border_width;
910         break;
911     }
912     switch(self->gravity) {
913     default:
914     case NorthWestGravity:
915     case NorthGravity:
916     case NorthEastGravity:
917         break;
918     case SouthWestGravity:
919     case SouthGravity:
920     case SouthEastGravity:
921         if (show) y -= self->border_width * 2;
922         else      y += self->border_width * 2;
923         break;
924     case WestGravity:
925     case EastGravity:
926     case CenterGravity:
927     case ForgetGravity:
928     case StaticGravity:
929         if (show) y -= self->border_width;
930         else      y += self->border_width;
931         break;
932     }
933     self->area.x = x;
934     self->area.y = y;
935
936     if (show) {
937         XSetWindowBorderWidth(ob_display, self->window, self->border_width);
938
939         /* set border_width to 0 because there is no border to add into
940            calculations anymore */
941         self->border_width = 0;
942     } else
943         XSetWindowBorderWidth(ob_display, self->window, 0);
944 }
945
946
947 static void client_get_all(ObClient *self)
948 {
949     client_get_area(self);
950     client_get_mwm_hints(self);
951
952     /* The transient-ness of a window is used to pick a type, but the type can
953        also affect transiency.
954
955        Dialogs are always made transients for their group if they have one.
956
957        I also have made non-application type windows be transients for their
958        group (eg utility windows).
959     */
960     client_get_transientness(self);
961     client_get_type(self);/* this can change the mwmhints for special cases */
962     client_get_state(self);
963
964     client_update_wmhints(self);
965     /* this may have already been called from client_update_wmhints */
966     if (self->transient_for == NULL)
967         client_update_transient_for(self);
968     client_get_startup_id(self);
969     client_get_desktop(self);/* uses transient data/group/startup id if a
970                                 desktop is not specified */
971     client_get_shaped(self);
972
973     client_get_layer(self); /* if layer hasn't been specified, get it from
974                                other sources if possible */
975
976     {
977         /* a couple type-based defaults for new windows */
978
979         /* this makes sure that these windows appear on all desktops */
980         if (self->type == OB_CLIENT_TYPE_DESKTOP)
981             self->desktop = DESKTOP_ALL;
982     }
983
984     client_update_protocols(self);
985
986     client_get_gravity(self); /* get the attribute gravity */
987     client_update_normal_hints(self); /* this may override the attribute
988                                          gravity */
989
990     /* got the type, the mwmhints, the protocols, and the normal hints
991        (min/max sizes), so we're ready to set up the decorations/functions */
992     client_setup_decor_and_functions(self);
993   
994 #ifdef SYNC
995     client_update_sync_request_counter(self);
996 #endif
997     client_get_client_machine(self);
998     client_get_colormap(self);
999     client_update_title(self);
1000     client_update_class(self);
1001     client_update_sm_client_id(self);
1002     client_update_command(self);
1003     client_update_strut(self);
1004     client_update_icons(self);
1005     client_update_user_time(self);
1006     client_update_icon_geometry(self);
1007 }
1008
1009 static void client_get_startup_id(ObClient *self)
1010 {
1011     if (!(PROP_GETS(self->window, net_startup_id, utf8, &self->startup_id)))
1012         if (self->group)
1013             PROP_GETS(self->group->leader,
1014                       net_startup_id, utf8, &self->startup_id);
1015 }
1016
1017 static void client_get_area(ObClient *self)
1018 {
1019     XWindowAttributes wattrib;
1020     Status ret;
1021   
1022     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
1023     g_assert(ret != BadWindow);
1024
1025     RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
1026     POINT_SET(self->root_pos, wattrib.x, wattrib.y);
1027     self->border_width = wattrib.border_width;
1028
1029     ob_debug("client area: %d %d  %d %d\n", wattrib.x, wattrib.y,
1030              wattrib.width, wattrib.height);
1031 }
1032
1033 static void client_get_desktop(ObClient *self)
1034 {
1035     guint32 d = screen_num_desktops; /* an always-invalid value */
1036
1037     if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
1038         if (d >= screen_num_desktops && d != DESKTOP_ALL)
1039             self->desktop = screen_num_desktops - 1;
1040         else
1041             self->desktop = d;
1042     } else {
1043         gboolean trdesk = FALSE;
1044
1045         if (self->transient_for) {
1046             if (self->transient_for != OB_TRAN_GROUP) {
1047                 self->desktop = self->transient_for->desktop;
1048                 trdesk = TRUE;
1049             } else {
1050                 GSList *it;
1051
1052                 for (it = self->group->members; it; it = g_slist_next(it))
1053                     if (it->data != self &&
1054                         !((ObClient*)it->data)->transient_for) {
1055                         self->desktop = ((ObClient*)it->data)->desktop;
1056                         trdesk = TRUE;
1057                         break;
1058                     }
1059             }
1060         }
1061         if (!trdesk) {
1062             /* try get from the startup-notification protocol */
1063             if (sn_get_desktop(self->startup_id, &self->desktop)) {
1064                 if (self->desktop >= screen_num_desktops &&
1065                     self->desktop != DESKTOP_ALL)
1066                     self->desktop = screen_num_desktops - 1;
1067             } else
1068                 /* defaults to the current desktop */
1069                 self->desktop = screen_desktop;
1070         }
1071     }
1072 }
1073
1074 static void client_get_layer(ObClient *self)
1075 {
1076     if (!(self->above || self->below)) {
1077         if (self->group) {
1078             /* apply stuff from the group */
1079             GSList *it;
1080             gint layer = -2;
1081
1082             for (it = self->group->members; it; it = g_slist_next(it)) {
1083                 ObClient *c = it->data;
1084                 if (c != self && !client_search_transient(self, c) &&
1085                     client_normal(self) && client_normal(c))
1086                 {
1087                     layer = MAX(layer,
1088                                 (c->above ? 1 : (c->below ? -1 : 0)));
1089                 }
1090             }
1091             switch (layer) {
1092             case -1:
1093                 self->below = TRUE;
1094                 break;
1095             case -2:
1096             case 0:
1097                 break;
1098             case 1:
1099                 self->above = TRUE;
1100                 break;
1101             default:
1102                 g_assert_not_reached();
1103                 break;
1104             }
1105         }
1106     }
1107 }
1108
1109 static void client_get_state(ObClient *self)
1110 {
1111     guint32 *state;
1112     guint num;
1113   
1114     if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
1115         gulong i;
1116         for (i = 0; i < num; ++i) {
1117             if (state[i] == prop_atoms.net_wm_state_modal)
1118                 self->modal = TRUE;
1119             else if (state[i] == prop_atoms.net_wm_state_shaded)
1120                 self->shaded = TRUE;
1121             else if (state[i] == prop_atoms.net_wm_state_hidden)
1122                 self->iconic = TRUE;
1123             else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
1124                 self->skip_taskbar = TRUE;
1125             else if (state[i] == prop_atoms.net_wm_state_skip_pager)
1126                 self->skip_pager = TRUE;
1127             else if (state[i] == prop_atoms.net_wm_state_fullscreen)
1128                 self->fullscreen = TRUE;
1129             else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
1130                 self->max_vert = TRUE;
1131             else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
1132                 self->max_horz = TRUE;
1133             else if (state[i] == prop_atoms.net_wm_state_above)
1134                 self->above = TRUE;
1135             else if (state[i] == prop_atoms.net_wm_state_below)
1136                 self->below = TRUE;
1137             else if (state[i] == prop_atoms.net_wm_state_demands_attention)
1138                 self->demands_attention = TRUE;
1139             else if (state[i] == prop_atoms.openbox_wm_state_undecorated)
1140                 self->undecorated = TRUE;
1141         }
1142
1143         g_free(state);
1144     }
1145 }
1146
1147 static void client_get_shaped(ObClient *self)
1148 {
1149     self->shaped = FALSE;
1150 #ifdef   SHAPE
1151     if (extensions_shape) {
1152         gint foo;
1153         guint ufoo;
1154         gint s;
1155
1156         XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
1157
1158         XShapeQueryExtents(ob_display, self->window, &s, &foo,
1159                            &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
1160                            &ufoo);
1161         self->shaped = (s != 0);
1162     }
1163 #endif
1164 }
1165
1166 void client_get_transientness(ObClient *self)
1167 {
1168     Window t;
1169     if (XGetTransientForHint(ob_display, self->window, &t))
1170         self->transient = TRUE;
1171 }
1172
1173 void client_update_transient_for(ObClient *self)
1174 {
1175     Window t = None;
1176     ObClient *target = NULL;
1177
1178     if (XGetTransientForHint(ob_display, self->window, &t)) {
1179         self->transient = TRUE;
1180         if (t != self->window) { /* cant be transient to itself! */
1181             target = g_hash_table_lookup(window_map, &t);
1182             /* if this happens then we need to check for it*/
1183             g_assert(target != self);
1184             if (target && !WINDOW_IS_CLIENT(target)) {
1185                 /* this can happen when a dialog is a child of
1186                    a dockapp, for example */
1187                 target = NULL;
1188             }
1189
1190             /* THIS IS SO ANNOYING ! ! ! ! Let me explain.... have a seat..
1191
1192                Setting the transient_for to Root is actually illegal, however
1193                applications from time have done this to specify transient for
1194                their group.
1195
1196                Now you can do that by being a TYPE_DIALOG and not setting
1197                the transient_for hint at all on your window. But people still
1198                use Root, and Kwin is very strange in this regard.
1199
1200                KWin 3.0 will not consider windows with transient_for set to
1201                Root as transient for their group *UNLESS* they are also modal.
1202                In that case, it will make them transient for the group. This
1203                leads to all sorts of weird behavior from KDE apps which are
1204                only tested in KWin. I'd like to follow their behavior just to
1205                make this work right with KDE stuff, but that seems wrong.
1206             */
1207             if (!target && self->group) {
1208                 /* not transient to a client, see if it is transient for a
1209                    group */
1210                 if (t == RootWindow(ob_display, ob_screen)) {
1211                     /* window is a transient for its group! */
1212                     target = OB_TRAN_GROUP;
1213                 }
1214             }
1215         }
1216     } else if (self->type == OB_CLIENT_TYPE_DIALOG ||
1217                self->type == OB_CLIENT_TYPE_TOOLBAR ||
1218                self->type == OB_CLIENT_TYPE_MENU ||
1219                self->type == OB_CLIENT_TYPE_UTILITY)
1220     {
1221         self->transient = TRUE;
1222         if (self->group)
1223             target = OB_TRAN_GROUP;
1224     } else
1225         self->transient = FALSE;
1226
1227     client_update_transient_tree(self, self->group, self->group,
1228                                  self->transient_for, target);
1229     self->transient_for = target;
1230                           
1231 }
1232
1233 static void client_update_transient_tree(ObClient *self,
1234                                          ObGroup *oldgroup, ObGroup *newgroup,
1235                                          ObClient* oldparent,
1236                                          ObClient *newparent)
1237 {
1238     GSList *it, *next;
1239     ObClient *c;
1240
1241     /* No change has occured */
1242     if (oldgroup == newgroup && oldparent == newparent) return;
1243
1244     /** Remove the client from the transient tree wherever it has changed **/
1245
1246     /* If the window is becoming a direct transient for a window in its group
1247        then that window can't be a child of this window anymore */
1248     if (oldparent != newparent &&
1249         newparent != NULL && newparent != OB_TRAN_GROUP &&
1250         newparent->transient_for == OB_TRAN_GROUP &&
1251         newgroup != NULL && newgroup == oldgroup)
1252     {
1253         self->transients = g_slist_remove(self->transients, newparent);
1254     }
1255             
1256
1257     /* If the group changed then we need to remove any old group transient
1258        windows from our children. But if we're transient for the group, then
1259        other group transients are not our children. */
1260     if (oldgroup != newgroup && oldgroup != NULL &&
1261         oldparent != OB_TRAN_GROUP)
1262     {
1263         for (it = self->transients; it; it = next) {
1264             next = g_slist_next(it);
1265             c = it->data;
1266             if (c->group == oldgroup)
1267                 self->transients = g_slist_delete_link(self->transients, it);
1268         }
1269     }
1270
1271     /* If we used to be transient for a group and now we are not, or we're
1272        transient for a new group, then we need to remove ourselves from all
1273        our ex-parents */
1274     if (oldparent == OB_TRAN_GROUP && (oldgroup != newgroup ||
1275                                        oldparent != newparent))
1276     {
1277         for (it = oldgroup->members; it; it = g_slist_next(it)) {
1278             c = it->data;
1279             if (c != self && (!c->transient_for ||
1280                               c->transient_for != OB_TRAN_GROUP))
1281                 c->transients = g_slist_remove(c->transients, self);
1282         }
1283     }
1284     /* If we used to be transient for a single window and we are no longer
1285        transient for it, then we need to remove ourself from its children */
1286     else if (oldparent != NULL && oldparent != OB_TRAN_GROUP &&
1287              oldparent != newparent)
1288         oldparent->transients = g_slist_remove(oldparent->transients, self);
1289
1290
1291     /** Re-add the client to the transient tree wherever it has changed **/
1292
1293     /* If we're now transient for a group and we weren't transient for it
1294        before then we need to add ourselves to all our new parents */
1295     if (newparent == OB_TRAN_GROUP && (oldgroup != newgroup ||
1296                                        oldparent != newparent))
1297     {
1298         for (it = oldgroup->members; it; it = g_slist_next(it)) {
1299             c = it->data;
1300             if (c != self && (!c->transient_for ||
1301                               c->transient_for != OB_TRAN_GROUP))
1302                 c->transients = g_slist_prepend(c->transients, self);
1303         }
1304     }
1305     /* If we are now transient for a single window which we weren't before,
1306        we need to add ourselves to its children
1307
1308        WARNING: Cyclical transient ness is possible if two windows are
1309        transient for eachother.
1310     */
1311     else if (newparent != NULL && newparent != OB_TRAN_GROUP &&
1312              newparent != oldparent &&
1313              /* don't make ourself its child if it is already our child */
1314              !client_is_direct_child(self, newparent))
1315         newparent->transients = g_slist_prepend(newparent->transients, self);
1316
1317     /* If the group changed then we need to add any new group transient
1318        windows to our children. But if we're transient for the group, then
1319        other group transients are not our children.
1320
1321        WARNING: Cyclical transient-ness is possible. For e.g. if:
1322        A is transient for the group
1323        B is a member of the group and transient for A
1324     */
1325     if (oldgroup != newgroup && newgroup != NULL &&
1326         newparent != OB_TRAN_GROUP)
1327     {
1328         for (it = newgroup->members; it; it = g_slist_next(it)) {
1329             c = it->data;
1330             if (c != self && c->transient_for == OB_TRAN_GROUP &&
1331                 /* Don't make it our child if it is already our parent */
1332                 !client_is_direct_child(c, self))
1333             {
1334                 self->transients = g_slist_prepend(self->transients, c);
1335             }
1336         }
1337     }
1338 }
1339
1340 static void client_get_mwm_hints(ObClient *self)
1341 {
1342     guint num;
1343     guint32 *hints;
1344
1345     self->mwmhints.flags = 0; /* default to none */
1346
1347     if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
1348                     &hints, &num)) {
1349         if (num >= OB_MWM_ELEMENTS) {
1350             self->mwmhints.flags = hints[0];
1351             self->mwmhints.functions = hints[1];
1352             self->mwmhints.decorations = hints[2];
1353         }
1354         g_free(hints);
1355     }
1356 }
1357
1358 void client_get_type(ObClient *self)
1359 {
1360     guint num, i;
1361     guint32 *val;
1362
1363     self->type = -1;
1364   
1365     if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
1366         /* use the first value that we know about in the array */
1367         for (i = 0; i < num; ++i) {
1368             if (val[i] == prop_atoms.net_wm_window_type_desktop)
1369                 self->type = OB_CLIENT_TYPE_DESKTOP;
1370             else if (val[i] == prop_atoms.net_wm_window_type_dock)
1371                 self->type = OB_CLIENT_TYPE_DOCK;
1372             else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
1373                 self->type = OB_CLIENT_TYPE_TOOLBAR;
1374             else if (val[i] == prop_atoms.net_wm_window_type_menu)
1375                 self->type = OB_CLIENT_TYPE_MENU;
1376             else if (val[i] == prop_atoms.net_wm_window_type_utility)
1377                 self->type = OB_CLIENT_TYPE_UTILITY;
1378             else if (val[i] == prop_atoms.net_wm_window_type_splash)
1379                 self->type = OB_CLIENT_TYPE_SPLASH;
1380             else if (val[i] == prop_atoms.net_wm_window_type_dialog)
1381                 self->type = OB_CLIENT_TYPE_DIALOG;
1382             else if (val[i] == prop_atoms.net_wm_window_type_normal)
1383                 self->type = OB_CLIENT_TYPE_NORMAL;
1384             else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
1385                 /* prevent this window from getting any decor or
1386                    functionality */
1387                 self->mwmhints.flags &= (OB_MWM_FLAG_FUNCTIONS |
1388                                          OB_MWM_FLAG_DECORATIONS);
1389                 self->mwmhints.decorations = 0;
1390                 self->mwmhints.functions = 0;
1391             }
1392             if (self->type != (ObClientType) -1)
1393                 break; /* grab the first legit type */
1394         }
1395         g_free(val);
1396     }
1397     
1398     if (self->type == (ObClientType) -1) {
1399         /*the window type hint was not set, which means we either classify
1400           ourself as a normal window or a dialog, depending on if we are a
1401           transient. */
1402         if (self->transient)
1403             self->type = OB_CLIENT_TYPE_DIALOG;
1404         else
1405             self->type = OB_CLIENT_TYPE_NORMAL;
1406     }
1407 }
1408
1409 void client_update_protocols(ObClient *self)
1410 {
1411     guint32 *proto;
1412     guint num_return, i;
1413
1414     self->focus_notify = FALSE;
1415     self->delete_window = FALSE;
1416
1417     if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
1418         for (i = 0; i < num_return; ++i) {
1419             if (proto[i] == prop_atoms.wm_delete_window)
1420                 /* this means we can request the window to close */
1421                 self->delete_window = TRUE;
1422             else if (proto[i] == prop_atoms.wm_take_focus)
1423                 /* if this protocol is requested, then the window will be
1424                    notified whenever we want it to receive focus */
1425                 self->focus_notify = TRUE;
1426 #ifdef SYNC
1427             else if (proto[i] == prop_atoms.net_wm_sync_request) 
1428                 /* if this protocol is requested, then resizing the
1429                    window will be synchronized between the frame and the
1430                    client */
1431                 self->sync_request = TRUE;
1432 #endif
1433         }
1434         g_free(proto);
1435     }
1436 }
1437
1438 #ifdef SYNC
1439 void client_update_sync_request_counter(ObClient *self)
1440 {
1441     guint32 i;
1442
1443     if (PROP_GET32(self->window, net_wm_sync_request_counter, cardinal, &i)) {
1444         self->sync_counter = i;
1445     } else
1446         self->sync_counter = None;
1447 }
1448 #endif
1449
1450 static void client_get_gravity(ObClient *self)
1451 {
1452     XWindowAttributes wattrib;
1453     Status ret;
1454
1455     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
1456     g_assert(ret != BadWindow);
1457     self->gravity = wattrib.win_gravity;
1458 }
1459
1460 void client_get_colormap(ObClient *self)
1461 {
1462     XWindowAttributes wa;
1463
1464     if (XGetWindowAttributes(ob_display, self->window, &wa))
1465         client_update_colormap(self, wa.colormap);
1466 }
1467
1468 void client_update_colormap(ObClient *self, Colormap colormap)
1469 {
1470     self->colormap = colormap;
1471 }
1472
1473 void client_update_normal_hints(ObClient *self)
1474 {
1475     XSizeHints size;
1476     glong ret;
1477     gint oldgravity = self->gravity;
1478
1479     /* defaults */
1480     self->min_ratio = 0.0f;
1481     self->max_ratio = 0.0f;
1482     SIZE_SET(self->size_inc, 1, 1);
1483     SIZE_SET(self->base_size, 0, 0);
1484     SIZE_SET(self->min_size, 0, 0);
1485     SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
1486
1487     /* get the hints from the window */
1488     if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
1489         /* normal windows can't request placement! har har
1490         if (!client_normal(self))
1491         */
1492         self->positioned = (size.flags & (PPosition|USPosition));
1493
1494         if (size.flags & PWinGravity) {
1495             self->gravity = size.win_gravity;
1496       
1497             /* if the client has a frame, i.e. has already been mapped and
1498                is changing its gravity */
1499             if (self->frame && self->gravity != oldgravity) {
1500                 /* move our idea of the client's position based on its new
1501                    gravity */
1502                 client_convert_gravity(self, oldgravity,
1503                                        &self->area.x, &self->area.y,
1504                                        self->area.width, self->area.height);
1505             }
1506         }
1507
1508         if (size.flags & PAspect) {
1509             if (size.min_aspect.y)
1510                 self->min_ratio =
1511                     (gfloat) size.min_aspect.x / size.min_aspect.y;
1512             if (size.max_aspect.y)
1513                 self->max_ratio =
1514                     (gfloat) size.max_aspect.x / size.max_aspect.y;
1515         }
1516
1517         if (size.flags & PMinSize)
1518             SIZE_SET(self->min_size, size.min_width, size.min_height);
1519     
1520         if (size.flags & PMaxSize)
1521             SIZE_SET(self->max_size, size.max_width, size.max_height);
1522     
1523         if (size.flags & PBaseSize)
1524             SIZE_SET(self->base_size, size.base_width, size.base_height);
1525     
1526         if (size.flags & PResizeInc && size.width_inc && size.height_inc)
1527             SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
1528     }
1529 }
1530
1531 void client_setup_decor_and_functions(ObClient *self)
1532 {
1533     /* start with everything (cept fullscreen) */
1534     self->decorations =
1535         (OB_FRAME_DECOR_TITLEBAR |
1536          OB_FRAME_DECOR_HANDLE |
1537          OB_FRAME_DECOR_GRIPS |
1538          OB_FRAME_DECOR_BORDER |
1539          OB_FRAME_DECOR_ICON |
1540          OB_FRAME_DECOR_ALLDESKTOPS |
1541          OB_FRAME_DECOR_ICONIFY |
1542          OB_FRAME_DECOR_MAXIMIZE |
1543          OB_FRAME_DECOR_SHADE |
1544          OB_FRAME_DECOR_CLOSE);
1545     self->functions =
1546         (OB_CLIENT_FUNC_RESIZE |
1547          OB_CLIENT_FUNC_MOVE |
1548          OB_CLIENT_FUNC_ICONIFY |
1549          OB_CLIENT_FUNC_MAXIMIZE |
1550          OB_CLIENT_FUNC_SHADE |
1551          OB_CLIENT_FUNC_CLOSE);
1552
1553     if (!(self->min_size.width < self->max_size.width ||
1554           self->min_size.height < self->max_size.height))
1555         self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1556
1557     switch (self->type) {
1558     case OB_CLIENT_TYPE_NORMAL:
1559         /* normal windows retain all of the possible decorations and
1560            functionality, and are the only windows that you can fullscreen */
1561         self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1562         break;
1563
1564     case OB_CLIENT_TYPE_DIALOG:
1565     case OB_CLIENT_TYPE_UTILITY:
1566         /* these windows cannot be maximized */
1567         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1568         break;
1569
1570     case OB_CLIENT_TYPE_MENU:
1571     case OB_CLIENT_TYPE_TOOLBAR:
1572         /* these windows get less functionality */
1573         self->functions &= ~(OB_CLIENT_FUNC_ICONIFY | OB_CLIENT_FUNC_RESIZE);
1574         break;
1575
1576     case OB_CLIENT_TYPE_DESKTOP:
1577     case OB_CLIENT_TYPE_DOCK:
1578     case OB_CLIENT_TYPE_SPLASH:
1579         /* none of these windows are manipulated by the window manager */
1580         self->decorations = 0;
1581         self->functions = 0;
1582         break;
1583     }
1584
1585     /* Mwm Hints are applied subtractively to what has already been chosen for
1586        decor and functionality */
1587     if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
1588         if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
1589             if (! ((self->mwmhints.decorations & OB_MWM_DECOR_HANDLE) ||
1590                    (self->mwmhints.decorations & OB_MWM_DECOR_TITLE)))
1591             {
1592                 /* if the mwm hints request no handle or title, then all
1593                    decorations are disabled, but keep the border if that's
1594                    specified */
1595                 if (self->mwmhints.decorations & OB_MWM_DECOR_BORDER)
1596                     self->decorations = OB_FRAME_DECOR_BORDER;
1597                 else
1598                     self->decorations = 0;
1599             }
1600         }
1601     }
1602
1603     if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
1604         if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
1605             if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
1606                 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1607             if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
1608                 self->functions &= ~OB_CLIENT_FUNC_MOVE;
1609             /* dont let mwm hints kill any buttons
1610                if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
1611                self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
1612                if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
1613                self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1614             */
1615             /* dont let mwm hints kill the close button
1616                if (! (self->mwmhints.functions & MwmFunc_Close))
1617                self->functions &= ~OB_CLIENT_FUNC_CLOSE; */
1618         }
1619     }
1620
1621     if (!(self->functions & OB_CLIENT_FUNC_SHADE))
1622         self->decorations &= ~OB_FRAME_DECOR_SHADE;
1623     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY))
1624         self->decorations &= ~OB_FRAME_DECOR_ICONIFY;
1625     if (!(self->functions & OB_CLIENT_FUNC_RESIZE))
1626         self->decorations &= ~OB_FRAME_DECOR_GRIPS;
1627
1628     /* can't maximize without moving/resizing */
1629     if (!((self->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
1630           (self->functions & OB_CLIENT_FUNC_MOVE) &&
1631           (self->functions & OB_CLIENT_FUNC_RESIZE))) {
1632         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1633         self->decorations &= ~OB_FRAME_DECOR_MAXIMIZE;
1634     }
1635
1636     /* kill the handle on fully maxed windows */
1637     if (self->max_vert && self->max_horz)
1638         self->decorations &= ~OB_FRAME_DECOR_HANDLE;
1639
1640     /* finally, the user can have requested no decorations, which overrides
1641        everything (but doesnt give it a border if it doesnt have one) */
1642     if (self->undecorated) {
1643         if (config_theme_keepborder)
1644             self->decorations &= OB_FRAME_DECOR_BORDER;
1645         else
1646             self->decorations = 0;
1647     }
1648
1649     /* if we don't have a titlebar, then we cannot shade! */
1650     if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1651         self->functions &= ~OB_CLIENT_FUNC_SHADE;
1652
1653     /* now we need to check against rules for the client's current state */
1654     if (self->fullscreen) {
1655         self->functions &= (OB_CLIENT_FUNC_CLOSE |
1656                             OB_CLIENT_FUNC_FULLSCREEN |
1657                             OB_CLIENT_FUNC_ICONIFY);
1658         self->decorations = 0;
1659     }
1660
1661     client_change_allowed_actions(self);
1662
1663     if (self->frame) {
1664         /* adjust the client's decorations, etc. */
1665         client_reconfigure(self);
1666     }
1667 }
1668
1669 static void client_change_allowed_actions(ObClient *self)
1670 {
1671     gulong actions[9];
1672     gint num = 0;
1673
1674     /* desktop windows are kept on all desktops */
1675     if (self->type != OB_CLIENT_TYPE_DESKTOP)
1676         actions[num++] = prop_atoms.net_wm_action_change_desktop;
1677
1678     if (self->functions & OB_CLIENT_FUNC_SHADE)
1679         actions[num++] = prop_atoms.net_wm_action_shade;
1680     if (self->functions & OB_CLIENT_FUNC_CLOSE)
1681         actions[num++] = prop_atoms.net_wm_action_close;
1682     if (self->functions & OB_CLIENT_FUNC_MOVE)
1683         actions[num++] = prop_atoms.net_wm_action_move;
1684     if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1685         actions[num++] = prop_atoms.net_wm_action_minimize;
1686     if (self->functions & OB_CLIENT_FUNC_RESIZE)
1687         actions[num++] = prop_atoms.net_wm_action_resize;
1688     if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1689         actions[num++] = prop_atoms.net_wm_action_fullscreen;
1690     if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1691         actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1692         actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1693     }
1694
1695     PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1696
1697     /* make sure the window isn't breaking any rules now */
1698
1699     if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1700         if (self->frame) client_shade(self, FALSE);
1701         else self->shaded = FALSE;
1702     }
1703     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY) && self->iconic) {
1704         if (self->frame) client_iconify(self, FALSE, TRUE);
1705         else self->iconic = FALSE;
1706     }
1707     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1708         if (self->frame) client_fullscreen(self, FALSE);
1709         else self->fullscreen = FALSE;
1710     }
1711     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
1712                                                          self->max_vert)) {
1713         if (self->frame) client_maximize(self, FALSE, 0);
1714         else self->max_vert = self->max_horz = FALSE;
1715     }
1716 }
1717
1718 void client_reconfigure(ObClient *self)
1719 {
1720     /* by making this pass FALSE for user, we avoid the emacs event storm where
1721        every configurenotify causes an update in its normal hints, i think this
1722        is generally what we want anyways... */
1723     client_configure(self, self->area.x, self->area.y,
1724                      self->area.width, self->area.height, FALSE, TRUE);
1725 }
1726
1727 void client_update_wmhints(ObClient *self)
1728 {
1729     XWMHints *hints;
1730
1731     /* assume a window takes input if it doesnt specify */
1732     self->can_focus = TRUE;
1733   
1734     if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1735         gboolean ur;
1736
1737         if (hints->flags & InputHint)
1738             self->can_focus = hints->input;
1739
1740         /* only do this when first managing the window *AND* when we aren't
1741            starting up! */
1742         if (ob_state() != OB_STATE_STARTING && self->frame == NULL)
1743             if (hints->flags & StateHint)
1744                 self->iconic = hints->initial_state == IconicState;
1745
1746         ur = self->urgent;
1747         self->urgent = (hints->flags & XUrgencyHint);
1748         if (self->urgent && !ur)
1749             client_hilite(self, TRUE);
1750         else if (!self->urgent && ur && self->demands_attention)
1751             client_hilite(self, FALSE);
1752
1753         if (!(hints->flags & WindowGroupHint))
1754             hints->window_group = None;
1755
1756         /* did the group state change? */
1757         if (hints->window_group !=
1758             (self->group ? self->group->leader : None))
1759         {
1760             ObGroup *oldgroup = self->group;
1761
1762             /* remove from the old group if there was one */
1763             if (self->group != NULL) {
1764                 group_remove(self->group, self);
1765                 self->group = NULL;
1766             }
1767
1768             /* add ourself to the group if we have one */
1769             if (hints->window_group != None) {
1770                 self->group = group_add(hints->window_group, self);
1771             }
1772
1773             /* Put ourselves into the new group's transient tree, and remove
1774                ourselves from the old group's */
1775             client_update_transient_tree(self, oldgroup, self->group,
1776                                          self->transient_for,
1777                                          self->transient_for);
1778
1779             /* Lastly, being in a group, or not, can change if the window is
1780                transient for anything.
1781
1782                The logic for this is:
1783                self->transient = TRUE always if the window wants to be
1784                transient for something, even if transient_for was NULL because
1785                it wasn't in a group before.
1786
1787                If transient_for was NULL and oldgroup was NULL we can assume
1788                that when we add the new group, it will become transient for
1789                something.
1790
1791                If transient_for was OB_TRAN_GROUP, then it must have already
1792                had a group. If it is getting a new group, the above call to
1793                client_update_transient_tree has already taken care of
1794                everything ! If it is losing all group status then it will
1795                no longer be transient for anything and that needs to be
1796                updated.
1797             */
1798             if (self->transient &&
1799                 ((self->transient_for == NULL && oldgroup == NULL) ||
1800                  (self->transient_for == OB_TRAN_GROUP && !self->group)))
1801                 client_update_transient_for(self);
1802         }
1803
1804         /* the WM_HINTS can contain an icon */
1805         client_update_icons(self);
1806
1807         XFree(hints);
1808     }
1809 }
1810
1811 void client_update_title(ObClient *self)
1812 {
1813     gchar *data = NULL;
1814     gchar *visible = NULL;
1815
1816     g_free(self->title);
1817      
1818     /* try netwm */
1819     if (!PROP_GETS(self->window, net_wm_name, utf8, &data)) {
1820         /* try old x stuff */
1821         if (!(PROP_GETS(self->window, wm_name, locale, &data)
1822               || PROP_GETS(self->window, wm_name, utf8, &data))) {
1823             if (self->transient) {
1824                 /*
1825                   GNOME alert windows are not given titles:
1826                   http://developer.gnome.org/projects/gup/hig/draft_hig_new/windows-alert.html
1827                 */
1828                 data = g_strdup("");
1829             } else
1830                 data = g_strdup("Unnamed Window");
1831         }
1832     }
1833
1834     if (self->client_machine) {
1835         visible = g_strdup_printf("%s (%s)", data, self->client_machine);
1836         g_free(data);
1837     } else
1838         visible = data;
1839
1840     PROP_SETS(self->window, net_wm_visible_name, visible);
1841     self->title = visible;
1842
1843     if (self->frame)
1844         frame_adjust_title(self->frame);
1845
1846     /* update the icon title */
1847     data = NULL;
1848     g_free(self->icon_title);
1849
1850     /* try netwm */
1851     if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1852         /* try old x stuff */
1853         if (!(PROP_GETS(self->window, wm_icon_name, locale, &data) ||
1854               PROP_GETS(self->window, wm_icon_name, utf8, &data)))
1855             data = g_strdup(self->title);
1856
1857     PROP_SETS(self->window, net_wm_visible_icon_name, data);
1858     self->icon_title = data;
1859 }
1860
1861 void client_update_class(ObClient *self)
1862 {
1863     gchar **data;
1864     gchar *s;
1865
1866     if (self->name) g_free(self->name);
1867     if (self->class) g_free(self->class);
1868     if (self->role) g_free(self->role);
1869
1870     self->name = self->class = self->role = NULL;
1871
1872     if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1873         if (data[0]) {
1874             self->name = g_strdup(data[0]);
1875             if (data[1])
1876                 self->class = g_strdup(data[1]);
1877         }
1878         g_strfreev(data);     
1879     }
1880
1881     if (PROP_GETS(self->window, wm_window_role, locale, &s))
1882         self->role = s;
1883
1884     if (self->name == NULL) self->name = g_strdup("");
1885     if (self->class == NULL) self->class = g_strdup("");
1886     if (self->role == NULL) self->role = g_strdup("");
1887 }
1888
1889 void client_update_strut(ObClient *self)
1890 {
1891     guint num;
1892     guint32 *data;
1893     gboolean got = FALSE;
1894     StrutPartial strut;
1895
1896     if (PROP_GETA32(self->window, net_wm_strut_partial, cardinal,
1897                     &data, &num)) {
1898         if (num == 12) {
1899             got = TRUE;
1900             STRUT_PARTIAL_SET(strut,
1901                               data[0], data[2], data[1], data[3],
1902                               data[4], data[5], data[8], data[9],
1903                               data[6], data[7], data[10], data[11]);
1904         }
1905         g_free(data);
1906     }
1907
1908     if (!got &&
1909         PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1910         if (num == 4) {
1911             const Rect *a;
1912
1913             got = TRUE;
1914
1915             /* use the screen's width/height */
1916             a = screen_physical_area();
1917
1918             STRUT_PARTIAL_SET(strut,
1919                               data[0], data[2], data[1], data[3],
1920                               a->y, a->y + a->height - 1,
1921                               a->x, a->x + a->width - 1,
1922                               a->y, a->y + a->height - 1,
1923                               a->x, a->x + a->width - 1);
1924         }
1925         g_free(data);
1926     }
1927
1928     if (!got)
1929         STRUT_PARTIAL_SET(strut, 0, 0, 0, 0,
1930                           0, 0, 0, 0, 0, 0, 0, 0);
1931
1932     if (!STRUT_EQUAL(strut, self->strut)) {
1933         self->strut = strut;
1934
1935         /* updating here is pointless while we're being mapped cuz we're not in
1936            the client list yet */
1937         if (self->frame)
1938             screen_update_areas();
1939     }
1940 }
1941
1942 void client_update_icons(ObClient *self)
1943 {
1944     guint num;
1945     guint32 *data;
1946     guint w, h, i, j;
1947
1948     for (i = 0; i < self->nicons; ++i)
1949         g_free(self->icons[i].data);
1950     if (self->nicons > 0)
1951         g_free(self->icons);
1952     self->nicons = 0;
1953
1954     if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1955         /* figure out how many valid icons are in here */
1956         i = 0;
1957         while (num - i > 2) {
1958             w = data[i++];
1959             h = data[i++];
1960             i += w * h;
1961             if (i > num || w*h == 0) break;
1962             ++self->nicons;
1963         }
1964
1965         self->icons = g_new(ObClientIcon, self->nicons);
1966     
1967         /* store the icons */
1968         i = 0;
1969         for (j = 0; j < self->nicons; ++j) {
1970             guint x, y, t;
1971
1972             w = self->icons[j].width = data[i++];
1973             h = self->icons[j].height = data[i++];
1974
1975             if (w*h == 0) continue;
1976
1977             self->icons[j].data = g_new(RrPixel32, w * h);
1978             for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1979                 if (x >= w) {
1980                     x = 0;
1981                     ++y;
1982                 }
1983                 self->icons[j].data[t] =
1984                     (((data[i] >> 24) & 0xff) << RrDefaultAlphaOffset) +
1985                     (((data[i] >> 16) & 0xff) << RrDefaultRedOffset) +
1986                     (((data[i] >> 8) & 0xff) << RrDefaultGreenOffset) +
1987                     (((data[i] >> 0) & 0xff) << RrDefaultBlueOffset);
1988             }
1989             g_assert(i <= num);
1990         }
1991
1992         g_free(data);
1993     } else {
1994         XWMHints *hints;
1995
1996         if ((hints = XGetWMHints(ob_display, self->window))) {
1997             if (hints->flags & IconPixmapHint) {
1998                 self->nicons++;
1999                 self->icons = g_new(ObClientIcon, self->nicons);
2000                 xerror_set_ignore(TRUE);
2001                 if (!RrPixmapToRGBA(ob_rr_inst,
2002                                     hints->icon_pixmap,
2003                                     (hints->flags & IconMaskHint ?
2004                                      hints->icon_mask : None),
2005                                     &self->icons[self->nicons-1].width,
2006                                     &self->icons[self->nicons-1].height,
2007                                     &self->icons[self->nicons-1].data)){
2008                     g_free(&self->icons[self->nicons-1]);
2009                     self->nicons--;
2010                 }
2011                 xerror_set_ignore(FALSE);
2012             }
2013             XFree(hints);
2014         }
2015     }
2016
2017     /* set the default icon onto the window
2018        in theory, this could be a race, but if a window doesn't set an icon
2019        or removes it entirely, it's not very likely it is going to set one
2020        right away afterwards */
2021     if (self->nicons == 0) {
2022         RrPixel32 *icon = ob_rr_theme->def_win_icon;
2023         gulong *data;
2024
2025         data = g_new(gulong, 48*48+2);
2026         data[0] = data[1] =  48;
2027         for (i = 0; i < 48*48; ++i)
2028             data[i+2] = (((icon[i] >> RrDefaultAlphaOffset) & 0xff) << 24) +
2029                 (((icon[i] >> RrDefaultRedOffset) & 0xff) << 16) +
2030                 (((icon[i] >> RrDefaultGreenOffset) & 0xff) << 8) +
2031                 (((icon[i] >> RrDefaultBlueOffset) & 0xff) << 0);
2032         PROP_SETA32(self->window, net_wm_icon, cardinal, data, 48*48+2);
2033         g_free(data);
2034     } else if (self->frame)
2035         /* don't draw the icon empty if we're just setting one now anyways,
2036            we'll get the property change any second */
2037         frame_adjust_icon(self->frame);
2038 }
2039
2040 void client_update_user_time(ObClient *self)
2041 {
2042     guint32 time;
2043
2044     if (PROP_GET32(self->window, net_wm_user_time, cardinal, &time)) {
2045         /* we set this every time, not just when it grows, because in practice
2046            sometimes time goes backwards! (ntpdate.. yay....) so.. if it goes
2047            backward we don't want all windows to stop focusing. we'll just
2048            assume noone is setting times older than the last one, cuz that
2049            would be pretty stupid anyways
2050         */
2051         self->user_time = time;
2052
2053         /*
2054         ob_debug("window %s user time %u\n", self->title, time);
2055         */
2056     }
2057 }
2058
2059 void client_update_icon_geometry(ObClient *self)
2060 {
2061     guint num;
2062     guint32 *data;
2063
2064     RECT_SET(self->icon_geometry, 0, 0, 0, 0);
2065
2066     if (PROP_GETA32(self->window, net_wm_icon_geometry, cardinal, &data, &num)
2067         && num == 4)
2068     {
2069         /* don't let them set it with an area < 0 */
2070         RECT_SET(self->icon_geometry, data[0], data[1],
2071                  MAX(data[2],0), MAX(data[3],0));
2072     }
2073 }
2074
2075 static void client_get_client_machine(ObClient *self)
2076 {
2077     gchar *data = NULL;
2078     gchar localhost[128];
2079
2080     g_free(self->client_machine);
2081
2082     if (PROP_GETS(self->window, wm_client_machine, locale, &data)) {
2083         gethostname(localhost, 127);
2084         localhost[127] = '\0';
2085         if (strcmp(localhost, data))
2086             self->client_machine = data;
2087     }
2088 }
2089
2090 static void client_change_wm_state(ObClient *self)
2091 {
2092     gulong state[2];
2093     glong old;
2094
2095     old = self->wmstate;
2096
2097     if (self->shaded || !self->frame->visible)
2098         self->wmstate = IconicState;
2099     else
2100         self->wmstate = NormalState;
2101
2102     if (old != self->wmstate) {
2103         PROP_MSG(self->window, kde_wm_change_state,
2104                  self->wmstate, 1, 0, 0);
2105
2106         state[0] = self->wmstate;
2107         state[1] = None;
2108         PROP_SETA32(self->window, wm_state, wm_state, state, 2);
2109     }
2110 }
2111
2112 static void client_change_state(ObClient *self)
2113 {
2114     gulong netstate[11];
2115     guint num;
2116
2117     num = 0;
2118     if (self->modal)
2119         netstate[num++] = prop_atoms.net_wm_state_modal;
2120     if (self->shaded)
2121         netstate[num++] = prop_atoms.net_wm_state_shaded;
2122     if (self->iconic)
2123         netstate[num++] = prop_atoms.net_wm_state_hidden;
2124     if (self->skip_taskbar)
2125         netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
2126     if (self->skip_pager)
2127         netstate[num++] = prop_atoms.net_wm_state_skip_pager;
2128     if (self->fullscreen)
2129         netstate[num++] = prop_atoms.net_wm_state_fullscreen;
2130     if (self->max_vert)
2131         netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
2132     if (self->max_horz)
2133         netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
2134     if (self->above)
2135         netstate[num++] = prop_atoms.net_wm_state_above;
2136     if (self->below)
2137         netstate[num++] = prop_atoms.net_wm_state_below;
2138     if (self->demands_attention)
2139         netstate[num++] = prop_atoms.net_wm_state_demands_attention;
2140     if (self->undecorated)
2141         netstate[num++] = prop_atoms.openbox_wm_state_undecorated;
2142     PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
2143
2144     if (self->frame)
2145         frame_adjust_state(self->frame);
2146 }
2147
2148 ObClient *client_search_focus_tree(ObClient *self)
2149 {
2150     GSList *it;
2151     ObClient *ret;
2152
2153     for (it = self->transients; it; it = g_slist_next(it)) {
2154         if (client_focused(it->data)) return it->data;
2155         if ((ret = client_search_focus_tree(it->data))) return ret;
2156     }
2157     return NULL;
2158 }
2159
2160 ObClient *client_search_focus_tree_full(ObClient *self)
2161 {
2162     if (self->transient_for) {
2163         if (self->transient_for != OB_TRAN_GROUP) {
2164             return client_search_focus_tree_full(self->transient_for);
2165         } else {
2166             GSList *it;
2167             gboolean recursed = FALSE;
2168         
2169             for (it = self->group->members; it; it = g_slist_next(it))
2170                 if (!((ObClient*)it->data)->transient_for) {
2171                     ObClient *c;
2172                     if ((c = client_search_focus_tree_full(it->data)))
2173                         return c;
2174                     recursed = TRUE;
2175                 }
2176             if (recursed)
2177                 return NULL;
2178         }
2179     }
2180
2181     /* this function checks the whole tree, the client_search_focus_tree~
2182        does not, so we need to check this window */
2183     if (client_focused(self))
2184         return self;
2185     return client_search_focus_tree(self);
2186 }
2187
2188 static ObStackingLayer calc_layer(ObClient *self)
2189 {
2190     ObStackingLayer l;
2191
2192     if (self->type == OB_CLIENT_TYPE_DESKTOP)
2193         l = OB_STACKING_LAYER_DESKTOP;
2194     else if (self->type == OB_CLIENT_TYPE_DOCK) {
2195         if (self->below) l = OB_STACKING_LAYER_NORMAL;
2196         else l = OB_STACKING_LAYER_ABOVE;
2197     }
2198     else if ((self->fullscreen ||
2199               /* no decorations and fills the monitor = oldskool fullscreen */
2200               (self->frame != NULL &&
2201                (self->frame->size.right == 0 && self->frame->size.left == 0 &&
2202                 self->frame->size.bottom == 0 && self->frame->size.top == 0 &&
2203                 RECT_EQUAL(self->area,
2204                            *screen_physical_area_monitor
2205                            (client_monitor(self)))))) &&
2206              (client_focused(self) || client_search_focus_tree(self)))
2207         l = OB_STACKING_LAYER_FULLSCREEN;
2208     else if (self->above) l = OB_STACKING_LAYER_ABOVE;
2209     else if (self->below) l = OB_STACKING_LAYER_BELOW;
2210     else l = OB_STACKING_LAYER_NORMAL;
2211
2212     return l;
2213 }
2214
2215 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
2216                                         ObStackingLayer min, gboolean raised)
2217 {
2218     ObStackingLayer old, own;
2219     GSList *it;
2220
2221     old = self->layer;
2222     own = calc_layer(self);
2223     self->layer = MAX(own, min);
2224
2225     for (it = self->transients; it; it = g_slist_next(it))
2226         client_calc_layer_recursive(it->data, orig,
2227                                     self->layer,
2228                                     raised ? raised : self->layer != old);
2229
2230     if (!raised && self->layer != old)
2231         if (orig->frame) { /* only restack if the original window is managed */
2232             stacking_remove(CLIENT_AS_WINDOW(self));
2233             stacking_add(CLIENT_AS_WINDOW(self));
2234         }
2235 }
2236
2237 void client_calc_layer(ObClient *self)
2238 {
2239     ObClient *orig;
2240     GSList *it;
2241
2242     orig = self;
2243
2244     /* transients take on the layer of their parents */
2245     it = client_search_all_top_parents(self);
2246
2247     for (; it; it = g_slist_next(it))
2248         client_calc_layer_recursive(it->data, orig, 0, FALSE);
2249 }
2250
2251 gboolean client_should_show(ObClient *self)
2252 {
2253     if (self->iconic)
2254         return FALSE;
2255     if (client_normal(self) && screen_showing_desktop)
2256         return FALSE;
2257     /*
2258     if (self->transient_for) {
2259         if (self->transient_for != OB_TRAN_GROUP)
2260             return client_should_show(self->transient_for);
2261         else {
2262             GSList *it;
2263
2264             for (it = self->group->members; it; it = g_slist_next(it)) {
2265                 ObClient *c = it->data;
2266                 if (c != self && !c->transient_for) {
2267                     if (client_should_show(c))
2268                         return TRUE;
2269                 }
2270             }
2271         }
2272     }
2273     */
2274     if (self->desktop == screen_desktop || self->desktop == DESKTOP_ALL)
2275         return TRUE;
2276     
2277     return FALSE;
2278 }
2279
2280 void client_show(ObClient *self)
2281 {
2282
2283     if (client_should_show(self)) {
2284         frame_show(self->frame);
2285     }
2286
2287     /* According to the ICCCM (sec 4.1.3.1) when a window is not visible, it
2288        needs to be in IconicState. This includes when it is on another
2289        desktop!
2290     */
2291     client_change_wm_state(self);
2292 }
2293
2294 void client_hide(ObClient *self)
2295 {
2296     if (!client_should_show(self)) {
2297         frame_hide(self->frame);
2298     }
2299
2300     /* According to the ICCCM (sec 4.1.3.1) when a window is not visible, it
2301        needs to be in IconicState. This includes when it is on another
2302        desktop!
2303     */
2304     client_change_wm_state(self);
2305 }
2306
2307 void client_showhide(ObClient *self)
2308 {
2309
2310     if (client_should_show(self)) {
2311         frame_show(self->frame);
2312     }
2313     else {
2314         frame_hide(self->frame);
2315     }
2316
2317     /* According to the ICCCM (sec 4.1.3.1) when a window is not visible, it
2318        needs to be in IconicState. This includes when it is on another
2319        desktop!
2320     */
2321     client_change_wm_state(self);
2322 }
2323
2324 gboolean client_normal(ObClient *self) {
2325     return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
2326               self->type == OB_CLIENT_TYPE_DOCK ||
2327               self->type == OB_CLIENT_TYPE_SPLASH);
2328 }
2329
2330 gboolean client_application(ObClient *self)
2331 {
2332     return (self->type == OB_CLIENT_TYPE_NORMAL ||
2333             self->type == OB_CLIENT_TYPE_DIALOG);
2334 }
2335
2336 static void client_apply_startup_state(ObClient *self, gint x, gint y)
2337 {
2338     gboolean pos = FALSE; /* has the window's position been configured? */
2339     gint ox, oy;
2340
2341     /* save the position, and set self->area for these to use */
2342     ox = self->area.x;
2343     oy = self->area.y;
2344     self->area.x = x;
2345     self->area.y = y;
2346
2347     /* set the desktop hint, to make sure that it always exists */
2348     PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
2349
2350     /* these are in a carefully crafted order.. */
2351
2352     if (self->iconic) {
2353         self->iconic = FALSE;
2354         client_iconify(self, TRUE, FALSE);
2355     }
2356     if (self->fullscreen) {
2357         self->fullscreen = FALSE;
2358         client_fullscreen(self, TRUE);
2359         pos = TRUE;
2360     }
2361     if (self->undecorated) {
2362         self->undecorated = FALSE;
2363         client_set_undecorated(self, TRUE);
2364     }
2365     if (self->shaded) {
2366         self->shaded = FALSE;
2367         client_shade(self, TRUE);
2368     }
2369     if (self->demands_attention) {
2370         self->demands_attention = FALSE;
2371         client_hilite(self, TRUE);
2372     }
2373   
2374     if (self->max_vert && self->max_horz) {
2375         self->max_vert = self->max_horz = FALSE;
2376         client_maximize(self, TRUE, 0);
2377         pos = TRUE;
2378     } else if (self->max_vert) {
2379         self->max_vert = FALSE;
2380         client_maximize(self, TRUE, 2);
2381         pos = TRUE;
2382     } else if (self->max_horz) {
2383         self->max_horz = FALSE;
2384         client_maximize(self, TRUE, 1);
2385         pos = TRUE;
2386     }
2387
2388     /* if the client didn't get positioned yet, then do so now
2389        call client_move even if the window is not being moved anywhere, because
2390        when we reparent it and decorate it, it is getting moved and we need to
2391        be telling it so with a ConfigureNotify event.
2392     */
2393     if (!pos) {
2394         /* use the saved position */
2395         self->area.x = ox;
2396         self->area.y = oy;
2397         client_move(self, x, y);
2398     }
2399
2400     /* nothing to do for the other states:
2401        skip_taskbar
2402        skip_pager
2403        modal
2404        above
2405        below
2406     */
2407 }
2408
2409 void client_convert_gravity(ObClient *self, gint gravity, gint *x, gint *y,
2410                             gint w, gint h)
2411 {
2412     gint oldg = self->gravity;
2413
2414     /* get the frame's position from the requested stuff */
2415     self->gravity = gravity;
2416     frame_client_gravity(self->frame, x, y, w, h);
2417     self->gravity = oldg;
2418
2419     /* get the client's position in its true gravity from that */
2420     frame_frame_gravity(self->frame, x, y, w, h);
2421 }
2422
2423 void client_try_configure(ObClient *self, gint *x, gint *y, gint *w, gint *h,
2424                           gint *logicalw, gint *logicalh,
2425                           gboolean user)
2426 {
2427     Rect desired_area = {*x, *y, *w, *h};
2428
2429     /* make the frame recalculate its dimentions n shit without changing
2430        anything visible for real, this way the constraints below can work with
2431        the updated frame dimensions. */
2432     frame_adjust_area(self->frame, TRUE, TRUE, TRUE);
2433
2434     /* work within the prefered sizes given by the window */
2435     if (!(*w == self->area.width && *h == self->area.height)) {
2436         gint basew, baseh, minw, minh;
2437
2438         /* base size is substituted with min size if not specified */
2439         if (self->base_size.width || self->base_size.height) {
2440             basew = self->base_size.width;
2441             baseh = self->base_size.height;
2442         } else {
2443             basew = self->min_size.width;
2444             baseh = self->min_size.height;
2445         }
2446         /* min size is substituted with base size if not specified */
2447         if (self->min_size.width || self->min_size.height) {
2448             minw = self->min_size.width;
2449             minh = self->min_size.height;
2450         } else {
2451             minw = self->base_size.width;
2452             minh = self->base_size.height;
2453         }
2454
2455         /* if this is a user-requested resize, then check against min/max
2456            sizes */
2457
2458         /* smaller than min size or bigger than max size? */
2459         if (*w > self->max_size.width) *w = self->max_size.width;
2460         if (*w < minw) *w = minw;
2461         if (*h > self->max_size.height) *h = self->max_size.height;
2462         if (*h < minh) *h = minh;
2463
2464         *w -= basew;
2465         *h -= baseh;
2466
2467         /* keep to the increments */
2468         *w /= self->size_inc.width;
2469         *h /= self->size_inc.height;
2470
2471         /* you cannot resize to nothing */
2472         if (basew + *w < 1) *w = 1 - basew;
2473         if (baseh + *h < 1) *h = 1 - baseh;
2474   
2475         /* save the logical size */
2476         *logicalw = self->size_inc.width > 1 ? *w : *w + basew;
2477         *logicalh = self->size_inc.height > 1 ? *h : *h + baseh;
2478
2479         *w *= self->size_inc.width;
2480         *h *= self->size_inc.height;
2481
2482         *w += basew;
2483         *h += baseh;
2484
2485         /* adjust the height to match the width for the aspect ratios.
2486            for this, min size is not substituted for base size ever. */
2487         *w -= self->base_size.width;
2488         *h -= self->base_size.height;
2489
2490         if (!self->fullscreen) {
2491             if (self->min_ratio)
2492                 if (*h * self->min_ratio > *w) {
2493                     *h = (gint)(*w / self->min_ratio);
2494
2495                     /* you cannot resize to nothing */
2496                     if (*h < 1) {
2497                         *h = 1;
2498                         *w = (gint)(*h * self->min_ratio);
2499                     }
2500                 }
2501             if (self->max_ratio)
2502                 if (*h * self->max_ratio < *w) {
2503                     *h = (gint)(*w / self->max_ratio);
2504
2505                     /* you cannot resize to nothing */
2506                     if (*h < 1) {
2507                         *h = 1;
2508                         *w = (gint)(*h * self->min_ratio);
2509                     }
2510                 }
2511         }
2512
2513         *w += self->base_size.width;
2514         *h += self->base_size.height;
2515     }
2516
2517     /* gets the frame's position */
2518     frame_client_gravity(self->frame, x, y, *w, *h);
2519
2520     /* these positions are frame positions, not client positions */
2521
2522     /* set the size and position if fullscreen */
2523     if (self->fullscreen) {
2524         Rect *a;
2525         guint i;
2526
2527         i = screen_find_monitor(&desired_area);
2528         a = screen_physical_area_monitor(i);
2529
2530         *x = a->x;
2531         *y = a->y;
2532         *w = a->width;
2533         *h = a->height;
2534
2535         user = FALSE; /* ignore if the client can't be moved/resized when it
2536                          is entering fullscreen */
2537     } else if (self->max_horz || self->max_vert) {
2538         Rect *a;
2539         guint i;
2540
2541         i = screen_find_monitor(&desired_area);
2542         a = screen_area_monitor(self->desktop, i);
2543
2544         /* set the size and position if maximized */
2545         if (self->max_horz) {
2546             *x = a->x;
2547             *w = a->width - self->frame->size.left - self->frame->size.right;
2548         }
2549         if (self->max_vert) {
2550             *y = a->y;
2551             *h = a->height - self->frame->size.top - self->frame->size.bottom;
2552         }
2553
2554         /* maximizing is not allowed if the user can't move+resize the window
2555          */
2556     }
2557
2558     /* gets the client's position */
2559     frame_frame_gravity(self->frame, x, y, *w, *h);
2560
2561     /* these override the above states! if you cant move you can't move! */
2562     if (user) {
2563         if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
2564             *x = self->area.x;
2565             *y = self->area.y;
2566         }
2567         if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
2568             *w = self->area.width;
2569             *h = self->area.height;
2570         }
2571     }
2572
2573     g_assert(*w > 0);
2574     g_assert(*h > 0);
2575 }
2576
2577
2578 void client_configure_full(ObClient *self, gint x, gint y, gint w, gint h,
2579                            gboolean user, gboolean final,
2580                            gboolean force_reply)
2581 {
2582     gint oldw, oldh, oldrx, oldry;
2583     gboolean send_resize_client;
2584     gboolean moved = FALSE, resized = FALSE, rootmoved = FALSE;
2585     guint fdecor = self->frame->decorations;
2586     gboolean fhorz = self->frame->max_horz;
2587     gint logicalw, logicalh;
2588
2589     /* find the new x, y, width, and height (and logical size) */
2590     client_try_configure(self, &x, &y, &w, &h, &logicalw, &logicalh, user);
2591
2592     /* set the logical size if things changed */
2593     if (!(w == self->area.width && h == self->area.height))
2594         SIZE_SET(self->logical_size, logicalw, logicalh);
2595
2596     /* figure out if we moved or resized or what */
2597     moved = x != self->area.x || y != self->area.y;
2598     resized = w != self->area.width || h != self->area.height;
2599
2600     oldw = self->area.width;
2601     oldh = self->area.height;
2602     RECT_SET(self->area, x, y, w, h);
2603
2604     /* for app-requested resizes, always resize if 'resized' is true.
2605        for user-requested ones, only resize if final is true, or when
2606        resizing in redraw mode */
2607     send_resize_client = ((!user && resized) ||
2608                           (user && (final ||
2609                                     (resized && config_resize_redraw))));
2610
2611     /* if the client is enlarging, then resize the client before the frame */
2612     if (send_resize_client && user && (w > oldw || h > oldh)) {
2613         XResizeWindow(ob_display, self->window, MAX(w, oldw), MAX(h, oldh));
2614         /* resize the plate to show the client padding color underneath */
2615         frame_adjust_client_area(self->frame);
2616     }
2617
2618     /* find the frame's dimensions and move/resize it */
2619     if (self->decorations != fdecor || self->max_horz != fhorz)
2620         moved = resized = TRUE;
2621     if (moved || resized)
2622         frame_adjust_area(self->frame, moved, resized, FALSE);
2623
2624     /* find the client's position relative to the root window */
2625     oldrx = self->root_pos.x;
2626     oldry = self->root_pos.y;
2627     rootmoved = (oldrx != (signed)(self->frame->area.x +
2628                                    self->frame->size.left -
2629                                    self->border_width) ||
2630                  oldry != (signed)(self->frame->area.y +
2631                                    self->frame->size.top -
2632                                    self->border_width));
2633
2634     if (force_reply || ((!user || (user && final)) && rootmoved))
2635     {
2636         XEvent event;
2637
2638         POINT_SET(self->root_pos,
2639                   self->frame->area.x + self->frame->size.left -
2640                   self->border_width,
2641                   self->frame->area.y + self->frame->size.top -
2642                   self->border_width);
2643
2644         event.type = ConfigureNotify;
2645         event.xconfigure.display = ob_display;
2646         event.xconfigure.event = self->window;
2647         event.xconfigure.window = self->window;
2648
2649         /* root window real coords */
2650         event.xconfigure.x = self->root_pos.x;
2651         event.xconfigure.y = self->root_pos.y;
2652         event.xconfigure.width = w;
2653         event.xconfigure.height = h;
2654         event.xconfigure.border_width = 0;
2655         event.xconfigure.above = self->frame->plate;
2656         event.xconfigure.override_redirect = FALSE;
2657         XSendEvent(event.xconfigure.display, event.xconfigure.window,
2658                    FALSE, StructureNotifyMask, &event);
2659     }
2660
2661     /* if the client is shrinking, then resize the frame before the client */
2662     if (send_resize_client && (!user || (w <= oldw || h <= oldh))) {
2663         /* resize the plate to show the client padding color underneath */
2664         frame_adjust_client_area(self->frame);
2665
2666         XResizeWindow(ob_display, self->window, w, h);
2667     }
2668
2669     XFlush(ob_display);
2670 }
2671
2672 void client_fullscreen(ObClient *self, gboolean fs)
2673 {
2674     gint x, y, w, h;
2675
2676     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
2677         self->fullscreen == fs) return;                   /* already done */
2678
2679     self->fullscreen = fs;
2680     client_change_state(self); /* change the state hints on the client */
2681     client_calc_layer(self);   /* and adjust out layer/stacking */
2682
2683     if (fs) {
2684         self->pre_fullscreen_area = self->area;
2685         /* if the window is maximized, its area isn't all that meaningful.
2686            save it's premax area instead. */
2687         if (self->max_horz) {
2688             self->pre_fullscreen_area.x = self->pre_max_area.x;
2689             self->pre_fullscreen_area.width = self->pre_max_area.width;
2690         }
2691         if (self->max_vert) {
2692             self->pre_fullscreen_area.y = self->pre_max_area.y;
2693             self->pre_fullscreen_area.height = self->pre_max_area.height;
2694         }
2695
2696         /* these are not actually used cuz client_configure will set them
2697            as appropriate when the window is fullscreened */
2698         x = y = w = h = 0;
2699     } else {
2700         Rect *a;
2701
2702         if (self->pre_fullscreen_area.width > 0 &&
2703             self->pre_fullscreen_area.height > 0)
2704         {
2705             x = self->pre_fullscreen_area.x;
2706             y = self->pre_fullscreen_area.y;
2707             w = self->pre_fullscreen_area.width;
2708             h = self->pre_fullscreen_area.height;
2709             RECT_SET(self->pre_fullscreen_area, 0, 0, 0, 0);
2710         } else {
2711             /* pick some fallbacks... */
2712             a = screen_area_monitor(self->desktop, 0);
2713             x = a->x + a->width / 4;
2714             y = a->y + a->height / 4;
2715             w = a->width / 2;
2716             h = a->height / 2;
2717         }
2718     }
2719
2720     client_setup_decor_and_functions(self);
2721
2722     client_move_resize(self, x, y, w, h);
2723
2724     /* try focus us when we go into fullscreen mode */
2725     client_focus(self);
2726 }
2727
2728 static void client_iconify_recursive(ObClient *self,
2729                                      gboolean iconic, gboolean curdesk)
2730 {
2731     GSList *it;
2732     gboolean changed = FALSE;
2733
2734
2735     if (self->iconic != iconic) {
2736         ob_debug("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"),
2737                  self->window);
2738
2739         if (iconic) {
2740             if (self->functions & OB_CLIENT_FUNC_ICONIFY) {
2741                 self->iconic = iconic;
2742
2743                 /* update the focus lists.. iconic windows go to the bottom of
2744                    the list, put the new iconic window at the 'top of the
2745                    bottom'. */
2746                 focus_order_to_top(self);
2747
2748                 changed = TRUE;
2749             }
2750         } else {
2751             self->iconic = iconic;
2752
2753             if (curdesk && self->desktop != screen_desktop &&
2754                 self->desktop != DESKTOP_ALL)
2755                 client_set_desktop(self, screen_desktop, FALSE);
2756
2757             /* this puts it after the current focused window */
2758             focus_order_remove(self);
2759             focus_order_add_new(self);
2760
2761             changed = TRUE;
2762         }
2763     }
2764
2765     if (changed) {
2766         client_change_state(self);
2767         if (ob_state() != OB_STATE_STARTING && config_animate_iconify)
2768             frame_begin_iconify_animation(self->frame, iconic);
2769         /* do this after starting the animation so it doesn't flash */
2770         client_showhide(self);
2771     }
2772
2773     /* iconify all direct transients, and deiconify all transients
2774        (non-direct too) */
2775     for (it = self->transients; it; it = g_slist_next(it))
2776         if (it->data != self)
2777             if (client_is_direct_child(self, it->data) || !iconic)
2778                 client_iconify_recursive(it->data, iconic, curdesk);
2779 }
2780
2781 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk)
2782 {
2783     /* move up the transient chain as far as possible first */
2784     self = client_search_top_normal_parent(self);
2785     client_iconify_recursive(self, iconic, curdesk);
2786 }
2787
2788 void client_maximize(ObClient *self, gboolean max, gint dir)
2789 {
2790     gint x, y, w, h;
2791      
2792     g_assert(dir == 0 || dir == 1 || dir == 2);
2793     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE)) return; /* can't */
2794
2795     /* check if already done */
2796     if (max) {
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     } else {
2801         if (dir == 0 && !self->max_horz && !self->max_vert) return;
2802         if (dir == 1 && !self->max_horz) return;
2803         if (dir == 2 && !self->max_vert) return;
2804     }
2805
2806     /* we just tell it to configure in the same place and client_configure
2807        worries about filling the screen with the window */
2808     x = self->area.x;
2809     y = self->area.y;
2810     w = self->area.width;
2811     h = self->area.height;
2812
2813     if (max) {
2814         if ((dir == 0 || dir == 1) && !self->max_horz) { /* horz */
2815             RECT_SET(self->pre_max_area,
2816                      self->area.x, self->pre_max_area.y,
2817                      self->area.width, self->pre_max_area.height);
2818         }
2819         if ((dir == 0 || dir == 2) && !self->max_vert) { /* vert */
2820             RECT_SET(self->pre_max_area,
2821                      self->pre_max_area.x, self->area.y,
2822                      self->pre_max_area.width, self->area.height);
2823         }
2824     } else {
2825         Rect *a;
2826
2827         a = screen_area_monitor(self->desktop, 0);
2828         if ((dir == 0 || dir == 1) && self->max_horz) { /* horz */
2829             if (self->pre_max_area.width > 0) {
2830                 x = self->pre_max_area.x;
2831                 w = self->pre_max_area.width;
2832
2833                 RECT_SET(self->pre_max_area, 0, self->pre_max_area.y,
2834                          0, self->pre_max_area.height);
2835             } else {
2836                 /* pick some fallbacks... */
2837                 x = a->x + a->width / 4;
2838                 w = a->width / 2;
2839             }
2840         }
2841         if ((dir == 0 || dir == 2) && self->max_vert) { /* vert */
2842             if (self->pre_max_area.height > 0) {
2843                 y = self->pre_max_area.y;
2844                 h = self->pre_max_area.height;
2845
2846                 RECT_SET(self->pre_max_area, self->pre_max_area.x, 0,
2847                          self->pre_max_area.width, 0);
2848             } else {
2849                 /* pick some fallbacks... */
2850                 y = a->y + a->height / 4;
2851                 h = a->height / 2;
2852             }
2853         }
2854     }
2855
2856     if (dir == 0 || dir == 1) /* horz */
2857         self->max_horz = max;
2858     if (dir == 0 || dir == 2) /* vert */
2859         self->max_vert = max;
2860
2861     client_change_state(self); /* change the state hints on the client */
2862
2863     client_setup_decor_and_functions(self);
2864
2865     client_move_resize(self, x, y, w, h);
2866 }
2867
2868 void client_shade(ObClient *self, gboolean shade)
2869 {
2870     if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
2871          shade) ||                         /* can't shade */
2872         self->shaded == shade) return;     /* already done */
2873
2874     self->shaded = shade;
2875     client_change_state(self);
2876     client_change_wm_state(self); /* the window is being hidden/shown */
2877     /* resize the frame to just the titlebar */
2878     frame_adjust_area(self->frame, FALSE, FALSE, FALSE);
2879 }
2880
2881 void client_close(ObClient *self)
2882 {
2883     XEvent ce;
2884
2885     if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
2886
2887     /* in the case that the client provides no means to requesting that it
2888        close, we just kill it */
2889     if (!self->delete_window)
2890         client_kill(self);
2891     
2892     /*
2893       XXX: itd be cool to do timeouts and shit here for killing the client's
2894       process off
2895       like... if the window is around after 5 seconds, then the close button
2896       turns a nice red, and if this function is called again, the client is
2897       explicitly killed.
2898     */
2899
2900     ce.xclient.type = ClientMessage;
2901     ce.xclient.message_type =  prop_atoms.wm_protocols;
2902     ce.xclient.display = ob_display;
2903     ce.xclient.window = self->window;
2904     ce.xclient.format = 32;
2905     ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
2906     ce.xclient.data.l[1] = event_curtime;
2907     ce.xclient.data.l[2] = 0l;
2908     ce.xclient.data.l[3] = 0l;
2909     ce.xclient.data.l[4] = 0l;
2910     XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2911 }
2912
2913 void client_kill(ObClient *self)
2914 {
2915     XKillClient(ob_display, self->window);
2916 }
2917
2918 void client_hilite(ObClient *self, gboolean hilite)
2919 {
2920     if (self->demands_attention == hilite)
2921         return; /* no change */
2922
2923     /* don't allow focused windows to hilite */
2924     self->demands_attention = hilite && !client_focused(self);
2925     if (self->frame != NULL) { /* if we're mapping, just set the state */
2926         if (self->demands_attention)
2927             frame_flash_start(self->frame);
2928         else
2929             frame_flash_stop(self->frame);
2930         client_change_state(self);
2931     }
2932 }
2933
2934 void client_set_desktop_recursive(ObClient *self,
2935                                   guint target, gboolean donthide)
2936 {
2937     guint old;
2938     GSList *it;
2939
2940     if (target != self->desktop) {
2941
2942         ob_debug("Setting desktop %u\n", target+1);
2943
2944         g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2945
2946         /* remove from the old desktop(s) */
2947         focus_order_remove(self);
2948
2949         old = self->desktop;
2950         self->desktop = target;
2951         PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2952         /* the frame can display the current desktop state */
2953         frame_adjust_state(self->frame);
2954         /* 'move' the window to the new desktop */
2955         if (!donthide)
2956             client_showhide(self);
2957         /* raise if it was not already on the desktop */
2958         if (old != DESKTOP_ALL)
2959             client_raise(self);
2960         if (STRUT_EXISTS(self->strut))
2961             screen_update_areas();
2962
2963         /* add to the new desktop(s) */
2964         if (config_focus_new)
2965             focus_order_to_top(self);
2966         else
2967             focus_order_to_bottom(self);
2968     }
2969
2970     /* move all transients */
2971     for (it = self->transients; it; it = g_slist_next(it))
2972         if (it->data != self)
2973             if (client_is_direct_child(self, it->data))
2974                 client_set_desktop_recursive(it->data, target, donthide);
2975 }
2976
2977 void client_set_desktop(ObClient *self, guint target, gboolean donthide)
2978 {
2979     self = client_search_top_normal_parent(self);
2980     client_set_desktop_recursive(self, target, donthide);
2981 }
2982
2983 gboolean client_is_direct_child(ObClient *parent, ObClient *child)
2984 {
2985     while (child != parent &&
2986            child->transient_for && child->transient_for != OB_TRAN_GROUP)
2987         child = child->transient_for;
2988     return child == parent;
2989 }
2990
2991 ObClient *client_search_modal_child(ObClient *self)
2992 {
2993     GSList *it;
2994     ObClient *ret;
2995   
2996     for (it = self->transients; it; it = g_slist_next(it)) {
2997         ObClient *c = it->data;
2998         if ((ret = client_search_modal_child(c))) return ret;
2999         if (c->modal) return c;
3000     }
3001     return NULL;
3002 }
3003
3004 gboolean client_validate(ObClient *self)
3005 {
3006     XEvent e; 
3007
3008     XSync(ob_display, FALSE); /* get all events on the server */
3009
3010     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
3011         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
3012         XPutBackEvent(ob_display, &e);
3013         return FALSE;
3014     }
3015
3016     return TRUE;
3017 }
3018
3019 void client_set_wm_state(ObClient *self, glong state)
3020 {
3021     if (state == self->wmstate) return; /* no change */
3022   
3023     switch (state) {
3024     case IconicState:
3025         client_iconify(self, TRUE, TRUE);
3026         break;
3027     case NormalState:
3028         client_iconify(self, FALSE, TRUE);
3029         break;
3030     }
3031 }
3032
3033 void client_set_state(ObClient *self, Atom action, glong data1, glong data2)
3034 {
3035     gboolean shaded = self->shaded;
3036     gboolean fullscreen = self->fullscreen;
3037     gboolean undecorated = self->undecorated;
3038     gboolean max_horz = self->max_horz;
3039     gboolean max_vert = self->max_vert;
3040     gboolean modal = self->modal;
3041     gboolean iconic = self->iconic;
3042     gboolean demands_attention = self->demands_attention;
3043     gint i;
3044
3045     if (!(action == prop_atoms.net_wm_state_add ||
3046           action == prop_atoms.net_wm_state_remove ||
3047           action == prop_atoms.net_wm_state_toggle))
3048         /* an invalid action was passed to the client message, ignore it */
3049         return; 
3050
3051     for (i = 0; i < 2; ++i) {
3052         Atom state = i == 0 ? data1 : data2;
3053     
3054         if (!state) continue;
3055
3056         /* if toggling, then pick whether we're adding or removing */
3057         if (action == prop_atoms.net_wm_state_toggle) {
3058             if (state == prop_atoms.net_wm_state_modal)
3059                 action = modal ? prop_atoms.net_wm_state_remove :
3060                     prop_atoms.net_wm_state_add;
3061             else if (state == prop_atoms.net_wm_state_maximized_vert)
3062                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
3063                     prop_atoms.net_wm_state_add;
3064             else if (state == prop_atoms.net_wm_state_maximized_horz)
3065                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
3066                     prop_atoms.net_wm_state_add;
3067             else if (state == prop_atoms.net_wm_state_shaded)
3068                 action = shaded ? prop_atoms.net_wm_state_remove :
3069                     prop_atoms.net_wm_state_add;
3070             else if (state == prop_atoms.net_wm_state_skip_taskbar)
3071                 action = self->skip_taskbar ?
3072                     prop_atoms.net_wm_state_remove :
3073                     prop_atoms.net_wm_state_add;
3074             else if (state == prop_atoms.net_wm_state_skip_pager)
3075                 action = self->skip_pager ?
3076                     prop_atoms.net_wm_state_remove :
3077                     prop_atoms.net_wm_state_add;
3078             else if (state == prop_atoms.net_wm_state_hidden)
3079                 action = self->iconic ?
3080                     prop_atoms.net_wm_state_remove :
3081                     prop_atoms.net_wm_state_add;
3082             else if (state == prop_atoms.net_wm_state_fullscreen)
3083                 action = fullscreen ?
3084                     prop_atoms.net_wm_state_remove :
3085                     prop_atoms.net_wm_state_add;
3086             else if (state == prop_atoms.net_wm_state_above)
3087                 action = self->above ? prop_atoms.net_wm_state_remove :
3088                     prop_atoms.net_wm_state_add;
3089             else if (state == prop_atoms.net_wm_state_below)
3090                 action = self->below ? prop_atoms.net_wm_state_remove :
3091                     prop_atoms.net_wm_state_add;
3092             else if (state == prop_atoms.net_wm_state_demands_attention)
3093                 action = self->demands_attention ?
3094                     prop_atoms.net_wm_state_remove :
3095                     prop_atoms.net_wm_state_add;
3096             else if (state == prop_atoms.openbox_wm_state_undecorated)
3097                 action = undecorated ? prop_atoms.net_wm_state_remove :
3098                     prop_atoms.net_wm_state_add;
3099         }
3100     
3101         if (action == prop_atoms.net_wm_state_add) {
3102             if (state == prop_atoms.net_wm_state_modal) {
3103                 modal = TRUE;
3104             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
3105                 max_vert = TRUE;
3106             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
3107                 max_horz = TRUE;
3108             } else if (state == prop_atoms.net_wm_state_shaded) {
3109                 shaded = TRUE;
3110             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
3111                 self->skip_taskbar = TRUE;
3112             } else if (state == prop_atoms.net_wm_state_skip_pager) {
3113                 self->skip_pager = TRUE;
3114             } else if (state == prop_atoms.net_wm_state_hidden) {
3115                 iconic = TRUE;
3116             } else if (state == prop_atoms.net_wm_state_fullscreen) {
3117                 fullscreen = TRUE;
3118             } else if (state == prop_atoms.net_wm_state_above) {
3119                 self->above = TRUE;
3120                 self->below = FALSE;
3121             } else if (state == prop_atoms.net_wm_state_below) {
3122                 self->above = FALSE;
3123                 self->below = TRUE;
3124             } else if (state == prop_atoms.net_wm_state_demands_attention) {
3125                 demands_attention = TRUE;
3126             } else if (state == prop_atoms.openbox_wm_state_undecorated) {
3127                 undecorated = TRUE;
3128             }
3129
3130         } else { /* action == prop_atoms.net_wm_state_remove */
3131             if (state == prop_atoms.net_wm_state_modal) {
3132                 modal = FALSE;
3133             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
3134                 max_vert = FALSE;
3135             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
3136                 max_horz = FALSE;
3137             } else if (state == prop_atoms.net_wm_state_shaded) {
3138                 shaded = FALSE;
3139             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
3140                 self->skip_taskbar = FALSE;
3141             } else if (state == prop_atoms.net_wm_state_skip_pager) {
3142                 self->skip_pager = FALSE;
3143             } else if (state == prop_atoms.net_wm_state_hidden) {
3144                 iconic = FALSE;
3145             } else if (state == prop_atoms.net_wm_state_fullscreen) {
3146                 fullscreen = FALSE;
3147             } else if (state == prop_atoms.net_wm_state_above) {
3148                 self->above = FALSE;
3149             } else if (state == prop_atoms.net_wm_state_below) {
3150                 self->below = FALSE;
3151             } else if (state == prop_atoms.net_wm_state_demands_attention) {
3152                 demands_attention = FALSE;
3153             } else if (state == prop_atoms.openbox_wm_state_undecorated) {
3154                 undecorated = FALSE;
3155             }
3156         }
3157     }
3158     if (max_horz != self->max_horz || max_vert != self->max_vert) {
3159         if (max_horz != self->max_horz && max_vert != self->max_vert) {
3160             /* toggling both */
3161             if (max_horz == max_vert) { /* both going the same way */
3162                 client_maximize(self, max_horz, 0);
3163             } else {
3164                 client_maximize(self, max_horz, 1);
3165                 client_maximize(self, max_vert, 2);
3166             }
3167         } else {
3168             /* toggling one */
3169             if (max_horz != self->max_horz)
3170                 client_maximize(self, max_horz, 1);
3171             else
3172                 client_maximize(self, max_vert, 2);
3173         }
3174     }
3175     /* change fullscreen state before shading, as it will affect if the window
3176        can shade or not */
3177     if (fullscreen != self->fullscreen)
3178         client_fullscreen(self, fullscreen);
3179     if (shaded != self->shaded)
3180         client_shade(self, shaded);
3181     if (undecorated != self->undecorated)
3182         client_set_undecorated(self, undecorated);
3183     if (modal != self->modal) {
3184         self->modal = modal;
3185         /* when a window changes modality, then its stacking order with its
3186            transients needs to change */
3187         client_raise(self);
3188     }
3189     if (iconic != self->iconic)
3190         client_iconify(self, iconic, FALSE);
3191
3192     if (demands_attention != self->demands_attention)
3193         client_hilite(self, demands_attention);
3194
3195     client_change_state(self); /* change the hint to reflect these changes */
3196 }
3197
3198 ObClient *client_focus_target(ObClient *self)
3199 {
3200     ObClient *child = NULL;
3201
3202     child = client_search_modal_child(self);
3203     if (child) return child;
3204     return self;
3205 }
3206
3207 gboolean client_can_focus(ObClient *self)
3208 {
3209     XEvent ev;
3210
3211     /* choose the correct target */
3212     self = client_focus_target(self);
3213
3214     if (!self->frame->visible)
3215         return FALSE;
3216
3217     if (!(self->can_focus || self->focus_notify))
3218         return FALSE;
3219
3220     /* do a check to see if the window has already been unmapped or destroyed
3221        do this intelligently while watching out for unmaps we've generated
3222        (ignore_unmaps > 0) */
3223     if (XCheckTypedWindowEvent(ob_display, self->window,
3224                                DestroyNotify, &ev)) {
3225         XPutBackEvent(ob_display, &ev);
3226         return FALSE;
3227     }
3228     while (XCheckTypedWindowEvent(ob_display, self->window,
3229                                   UnmapNotify, &ev)) {
3230         if (self->ignore_unmaps) {
3231             self->ignore_unmaps--;
3232         } else {
3233             XPutBackEvent(ob_display, &ev);
3234             return FALSE;
3235         }
3236     }
3237
3238     return TRUE;
3239 }
3240
3241 gboolean client_focus(ObClient *self)
3242 {
3243     /* choose the correct target */
3244     self = client_focus_target(self);
3245
3246     if (!client_can_focus(self)) {
3247         if (!self->frame->visible) {
3248             /* update the focus lists */
3249             focus_order_to_top(self);
3250         }
3251         return FALSE;
3252     }
3253
3254     ob_debug_type(OB_DEBUG_FOCUS,
3255                   "Focusing client \"%s\" at time %u\n",
3256                   self->title, event_curtime);
3257
3258     if (self->can_focus) {
3259         /* This can cause a BadMatch error with CurrentTime, or if an app
3260            passed in a bad time for _NET_WM_ACTIVE_WINDOW. */
3261         xerror_set_ignore(TRUE);
3262         XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
3263                        event_curtime);
3264         xerror_set_ignore(FALSE);
3265     }
3266
3267     if (self->focus_notify) {
3268         XEvent ce;
3269         ce.xclient.type = ClientMessage;
3270         ce.xclient.message_type = prop_atoms.wm_protocols;
3271         ce.xclient.display = ob_display;
3272         ce.xclient.window = self->window;
3273         ce.xclient.format = 32;
3274         ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
3275         ce.xclient.data.l[1] = event_curtime;
3276         ce.xclient.data.l[2] = 0l;
3277         ce.xclient.data.l[3] = 0l;
3278         ce.xclient.data.l[4] = 0l;
3279         XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
3280     }
3281
3282 #ifdef DEBUG_FOCUS
3283     ob_debug("%sively focusing %lx at %d\n",
3284              (self->can_focus ? "act" : "pass"),
3285              self->window, (gint) event_curtime);
3286 #endif
3287
3288     /* Cause the FocusIn to come back to us. Important for desktop switches,
3289        since otherwise we'll have no FocusIn on the queue and send it off to
3290        the focus_backup. */
3291     XSync(ob_display, FALSE);
3292     return TRUE;
3293 }
3294
3295 /*! Present the client to the user.
3296   @param raise If the client should be raised or not. You should only set
3297                raise to false if you don't care if the window is completely
3298                hidden.
3299 */
3300 static void client_present(ObClient *self, gboolean here, gboolean raise)
3301 {
3302     /* if using focus_delay, stop the timer now so that focus doesn't
3303        go moving on us */
3304     event_halt_focus_delay();
3305
3306     if (client_normal(self) && screen_showing_desktop)
3307         screen_show_desktop(FALSE, FALSE);
3308     if (self->iconic)
3309         client_iconify(self, FALSE, here);
3310     if (self->desktop != DESKTOP_ALL &&
3311         self->desktop != screen_desktop)
3312     {
3313         if (here)
3314             client_set_desktop(self, screen_desktop, FALSE);
3315         else
3316             screen_set_desktop(self->desktop, FALSE);
3317     } else if (!self->frame->visible)
3318         /* if its not visible for other reasons, then don't mess
3319            with it */
3320         return;
3321     if (self->shaded)
3322         client_shade(self, FALSE);
3323
3324     client_focus(self);
3325
3326     if (raise) {
3327         /* we do this as an action here. this is rather important. this is
3328            because we want the results from the focus change to take place 
3329            BEFORE we go about raising the window. when a fullscreen window 
3330            loses focus, we need this or else the raise wont be able to raise 
3331            above the to-lose-focus fullscreen window. */
3332         client_raise(self);
3333     }
3334 }
3335
3336 void client_activate(ObClient *self, gboolean here, gboolean user)
3337 {
3338     guint32 last_time = focus_client ? focus_client->user_time : CurrentTime;
3339
3340     /* XXX do some stuff here if user is false to determine if we really want
3341        to activate it or not (a parent or group member is currently
3342        active)?
3343     */
3344     ob_debug_type(OB_DEBUG_FOCUS,
3345                   "Want to activate window 0x%x with time %u (last time %u), "
3346                   "source=%s\n",
3347                   self->window, event_curtime, last_time,
3348                   (user ? "user" : "application"));
3349
3350     if (!user && event_curtime && last_time &&
3351         !event_time_after(event_curtime, last_time))
3352     {
3353         client_hilite(self, TRUE);
3354     } else {
3355         if (event_curtime != CurrentTime)
3356             self->user_time = event_curtime;
3357
3358         client_present(self, here, TRUE);
3359     }
3360 }
3361
3362 void client_raise(ObClient *self)
3363 {
3364     action_run_string("Raise", self, CurrentTime);
3365 }
3366
3367 void client_lower(ObClient *self)
3368 {
3369     action_run_string("Lower", self, CurrentTime);
3370 }
3371
3372 gboolean client_focused(ObClient *self)
3373 {
3374     return self == focus_client;
3375 }
3376
3377 static ObClientIcon* client_icon_recursive(ObClient *self, gint w, gint h)
3378 {
3379     guint i;
3380     /* si is the smallest image >= req */
3381     /* li is the largest image < req */
3382     gulong size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
3383
3384     if (!self->nicons) {
3385         ObClientIcon *parent = NULL;
3386
3387         if (self->transient_for) {
3388             if (self->transient_for != OB_TRAN_GROUP)
3389                 parent = client_icon_recursive(self->transient_for, w, h);
3390             else {
3391                 GSList *it;
3392                 for (it = self->group->members; it; it = g_slist_next(it)) {
3393                     ObClient *c = it->data;
3394                     if (c != self && !c->transient_for) {
3395                         if ((parent = client_icon_recursive(c, w, h)))
3396                             break;
3397                     }
3398                 }
3399             }
3400         }
3401         
3402         return parent;
3403     }
3404
3405     for (i = 0; i < self->nicons; ++i) {
3406         size = self->icons[i].width * self->icons[i].height;
3407         if (size < smallest && size >= (unsigned)(w * h)) {
3408             smallest = size;
3409             si = i;
3410         }
3411         if (size > largest && size <= (unsigned)(w * h)) {
3412             largest = size;
3413             li = i;
3414         }
3415     }
3416     if (largest == 0) /* didnt find one smaller than the requested size */
3417         return &self->icons[si];
3418     return &self->icons[li];
3419 }
3420
3421 const ObClientIcon* client_icon(ObClient *self, gint w, gint h)
3422 {
3423     ObClientIcon *ret;
3424     static ObClientIcon deficon;
3425
3426     if (!(ret = client_icon_recursive(self, w, h))) {
3427         deficon.width = deficon.height = 48;
3428         deficon.data = ob_rr_theme->def_win_icon;
3429         ret = &deficon;
3430     }
3431     return ret;
3432 }
3433
3434 void client_set_layer(ObClient *self, gint layer)
3435 {
3436     if (layer < 0) {
3437         self->below = TRUE;
3438         self->above = FALSE;
3439     } else if (layer == 0) {
3440         self->below = self->above = FALSE;
3441     } else {
3442         self->below = FALSE;
3443         self->above = TRUE;
3444     }
3445     client_calc_layer(self);
3446     client_change_state(self); /* reflect this in the state hints */
3447 }
3448
3449 void client_set_undecorated(ObClient *self, gboolean undecorated)
3450 {
3451     if (self->undecorated != undecorated) {
3452         self->undecorated = undecorated;
3453         client_setup_decor_and_functions(self);
3454         /* Make sure the client knows it might have moved. Maybe there is a
3455          * better way of doing this so only one client_configure is sent, but
3456          * since 125 of these are sent per second when moving the window (with
3457          * user = FALSE) i doubt it matters much.
3458          */
3459         client_configure(self, self->area.x, self->area.y,
3460                          self->area.width, self->area.height, TRUE, TRUE);
3461         client_change_state(self); /* reflect this in the state hints */
3462     }
3463 }
3464
3465 guint client_monitor(ObClient *self)
3466 {
3467     return screen_find_monitor(&self->frame->area);
3468 }
3469
3470 ObClient *client_search_top_normal_parent(ObClient *self)
3471 {
3472     while (self->transient_for && self->transient_for != OB_TRAN_GROUP &&
3473            client_normal(self->transient_for))
3474         self = self->transient_for;
3475     return self;
3476 }
3477
3478 static GSList *client_search_all_top_parents_internal(ObClient *self,
3479                                                       gboolean bylayer,
3480                                                       ObStackingLayer layer)
3481 {
3482     GSList *ret = NULL;
3483     
3484     /* move up the direct transient chain as far as possible */
3485     while (self->transient_for && self->transient_for != OB_TRAN_GROUP &&
3486            (!bylayer || self->transient_for->layer == layer) &&
3487            client_normal(self->transient_for))
3488         self = self->transient_for;
3489
3490     if (!self->transient_for)
3491         ret = g_slist_prepend(ret, self);
3492     else {
3493             GSList *it;
3494
3495             g_assert(self->group);
3496
3497             for (it = self->group->members; it; it = g_slist_next(it)) {
3498                 ObClient *c = it->data;
3499
3500                 if (!c->transient_for && client_normal(c) &&
3501                     (!bylayer || c->layer == layer))
3502                 {
3503                     ret = g_slist_prepend(ret, c);
3504                 }
3505             }
3506
3507             if (ret == NULL) /* no group parents */
3508                 ret = g_slist_prepend(ret, self);
3509     }
3510
3511     return ret;
3512 }
3513
3514 GSList *client_search_all_top_parents(ObClient *self)
3515 {
3516     return client_search_all_top_parents_internal(self, FALSE, 0);
3517 }
3518
3519 GSList *client_search_all_top_parents_layer(ObClient *self)
3520 {
3521     return client_search_all_top_parents_internal(self, TRUE, self->layer);
3522 }
3523
3524 ObClient *client_search_focus_parent(ObClient *self)
3525 {
3526     if (self->transient_for) {
3527         if (self->transient_for != OB_TRAN_GROUP) {
3528             if (client_focused(self->transient_for))
3529                 return self->transient_for;
3530         } else {
3531             GSList *it;
3532
3533             for (it = self->group->members; it; it = g_slist_next(it)) {
3534                 ObClient *c = it->data;
3535
3536                 /* checking transient_for prevents infinate loops! */
3537                 if (c != self && !c->transient_for)
3538                     if (client_focused(c))
3539                         return c;
3540             }
3541         }
3542     }
3543
3544     return NULL;
3545 }
3546
3547 ObClient *client_search_parent(ObClient *self, ObClient *search)
3548 {
3549     if (self->transient_for) {
3550         if (self->transient_for != OB_TRAN_GROUP) {
3551             if (self->transient_for == search)
3552                 return search;
3553         } else {
3554             GSList *it;
3555
3556             for (it = self->group->members; it; it = g_slist_next(it)) {
3557                 ObClient *c = it->data;
3558
3559                 /* checking transient_for prevents infinate loops! */
3560                 if (c != self && !c->transient_for)
3561                     if (c == search)
3562                         return search;
3563             }
3564         }
3565     }
3566
3567     return NULL;
3568 }
3569
3570 ObClient *client_search_transient(ObClient *self, ObClient *search)
3571 {
3572     GSList *sit;
3573
3574     for (sit = self->transients; sit; sit = g_slist_next(sit)) {
3575         if (sit->data == search)
3576             return search;
3577         if (client_search_transient(sit->data, search))
3578             return search;
3579     }
3580     return NULL;
3581 }
3582
3583 void client_update_sm_client_id(ObClient *self)
3584 {
3585     g_free(self->sm_client_id);
3586     self->sm_client_id = NULL;
3587
3588     if (!PROP_GETS(self->window, sm_client_id, locale, &self->sm_client_id))
3589         if (self->group)
3590             PROP_GETS(self->group->leader, sm_client_id, locale,
3591                       &self->sm_client_id);
3592 }
3593
3594 void client_update_command(ObClient *self)
3595 {
3596     gchar **data;
3597
3598     g_free(self->wm_command);
3599     self->wm_command = NULL;
3600
3601     if (PROP_GETSS(self->window, wm_command, locale, &data)) {
3602         /* merge/mash them all together */
3603         gchar *merge = NULL;
3604         gint i;
3605
3606         for (i = 0; data[i]; ++i) {
3607             gchar *tmp = merge;
3608             if (merge)
3609                 merge = g_strconcat(merge, data[i], NULL);
3610             else
3611                 merge = g_strconcat(data[i], NULL);
3612             g_free(tmp);
3613         }
3614         g_strfreev(data);
3615
3616         self->wm_command = merge;
3617     }
3618 }
3619
3620 #define WANT_EDGE(cur, c) \
3621             if(cur == c)                                                      \
3622                 continue;                                                     \
3623             if(!client_normal(cur))                                           \
3624                 continue;                                                     \
3625             if(screen_desktop != cur->desktop && cur->desktop != DESKTOP_ALL) \
3626                 continue;                                                     \
3627             if(cur->iconic)                                                   \
3628                 continue;                                                     \
3629             if(cur->layer < c->layer && !config_resist_layers_below)          \
3630                 continue;
3631
3632 #define HIT_EDGE(my_edge_start, my_edge_end, his_edge_start, his_edge_end) \
3633             if ((his_edge_start >= my_edge_start && \
3634                  his_edge_start <= my_edge_end) ||  \
3635                 (my_edge_start >= his_edge_start && \
3636                  my_edge_start <= his_edge_end))    \
3637                 dest = his_offset;
3638
3639 /* finds the nearest edge in the given direction from the current client
3640  * note to self: the edge is the -frame- edge (the actual one), not the
3641  * client edge.
3642  */
3643 gint client_directional_edge_search(ObClient *c, ObDirection dir, gboolean hang)
3644 {
3645     gint dest, monitor_dest;
3646     gint my_edge_start, my_edge_end, my_offset;
3647     GList *it;
3648     Rect *a, *monitor;
3649     
3650     if(!client_list)
3651         return -1;
3652
3653     a = screen_area(c->desktop);
3654     monitor = screen_area_monitor(c->desktop, client_monitor(c));
3655
3656     switch(dir) {
3657     case OB_DIRECTION_NORTH:
3658         my_edge_start = c->frame->area.x;
3659         my_edge_end = c->frame->area.x + c->frame->area.width;
3660         my_offset = c->frame->area.y + (hang ? c->frame->area.height : 0);
3661         
3662         /* default: top of screen */
3663         dest = a->y + (hang ? c->frame->area.height : 0);
3664         monitor_dest = monitor->y + (hang ? c->frame->area.height : 0);
3665         /* if the monitor edge comes before the screen edge, */
3666         /* use that as the destination instead. (For xinerama) */
3667         if (monitor_dest != dest && my_offset > monitor_dest)
3668             dest = monitor_dest; 
3669
3670         for(it = client_list; it && my_offset != dest; it = g_list_next(it)) {
3671             gint his_edge_start, his_edge_end, his_offset;
3672             ObClient *cur = it->data;
3673
3674             WANT_EDGE(cur, c)
3675
3676             his_edge_start = cur->frame->area.x;
3677             his_edge_end = cur->frame->area.x + cur->frame->area.width;
3678             his_offset = cur->frame->area.y + 
3679                          (hang ? 0 : cur->frame->area.height);
3680
3681             if(his_offset + 1 > my_offset)
3682                 continue;
3683
3684             if(his_offset < dest)
3685                 continue;
3686
3687             HIT_EDGE(my_edge_start, my_edge_end, his_edge_start, his_edge_end)
3688         }
3689         break;
3690     case OB_DIRECTION_SOUTH:
3691         my_edge_start = c->frame->area.x;
3692         my_edge_end = c->frame->area.x + c->frame->area.width;
3693         my_offset = c->frame->area.y + (hang ? 0 : c->frame->area.height);
3694
3695         /* default: bottom of screen */
3696         dest = a->y + a->height - (hang ? c->frame->area.height : 0);
3697         monitor_dest = monitor->y + monitor->height -
3698                        (hang ? c->frame->area.height : 0);
3699         /* if the monitor edge comes before the screen edge, */
3700         /* use that as the destination instead. (For xinerama) */
3701         if (monitor_dest != dest && my_offset < monitor_dest)
3702             dest = monitor_dest; 
3703
3704         for(it = client_list; it && my_offset != dest; it = g_list_next(it)) {
3705             gint his_edge_start, his_edge_end, his_offset;
3706             ObClient *cur = it->data;
3707
3708             WANT_EDGE(cur, c)
3709
3710             his_edge_start = cur->frame->area.x;
3711             his_edge_end = cur->frame->area.x + cur->frame->area.width;
3712             his_offset = cur->frame->area.y +
3713                          (hang ? cur->frame->area.height : 0);
3714
3715
3716             if(his_offset - 1 < my_offset)
3717                 continue;
3718             
3719             if(his_offset > dest)
3720                 continue;
3721
3722             HIT_EDGE(my_edge_start, my_edge_end, his_edge_start, his_edge_end)
3723         }
3724         break;
3725     case OB_DIRECTION_WEST:
3726         my_edge_start = c->frame->area.y;
3727         my_edge_end = c->frame->area.y + c->frame->area.height;
3728         my_offset = c->frame->area.x + (hang ? c->frame->area.width : 0);
3729
3730         /* default: leftmost egde of screen */
3731         dest = a->x + (hang ? c->frame->area.width : 0);
3732         monitor_dest = monitor->x + (hang ? c->frame->area.width : 0);
3733         /* if the monitor edge comes before the screen edge, */
3734         /* use that as the destination instead. (For xinerama) */
3735         if (monitor_dest != dest && my_offset > monitor_dest)
3736             dest = monitor_dest;            
3737
3738         for(it = client_list; it && my_offset != dest; it = g_list_next(it)) {
3739             gint his_edge_start, his_edge_end, his_offset;
3740             ObClient *cur = it->data;
3741
3742             WANT_EDGE(cur, c)
3743
3744             his_edge_start = cur->frame->area.y;
3745             his_edge_end = cur->frame->area.y + cur->frame->area.height;
3746             his_offset = cur->frame->area.x +
3747                          (hang ? 0 : cur->frame->area.width);
3748
3749             if(his_offset + 1 > my_offset)
3750                 continue;
3751
3752             if(his_offset < dest)
3753                 continue;
3754
3755             HIT_EDGE(my_edge_start, my_edge_end, his_edge_start, his_edge_end)
3756         }
3757        break;
3758     case OB_DIRECTION_EAST:
3759         my_edge_start = c->frame->area.y;
3760         my_edge_end = c->frame->area.y + c->frame->area.height;
3761         my_offset = c->frame->area.x + (hang ? 0 : c->frame->area.width);
3762         
3763         /* default: rightmost edge of screen */
3764         dest = a->x + a->width - (hang ? c->frame->area.width : 0);
3765         monitor_dest = monitor->x + monitor->width -
3766                        (hang ? c->frame->area.width : 0);
3767         /* if the monitor edge comes before the screen edge, */
3768         /* use that as the destination instead. (For xinerama) */
3769         if (monitor_dest != dest && my_offset < monitor_dest)
3770             dest = monitor_dest;            
3771
3772         for(it = client_list; it && my_offset != dest; it = g_list_next(it)) {
3773             gint his_edge_start, his_edge_end, his_offset;
3774             ObClient *cur = it->data;
3775
3776             WANT_EDGE(cur, c)
3777
3778             his_edge_start = cur->frame->area.y;
3779             his_edge_end = cur->frame->area.y + cur->frame->area.height;
3780             his_offset = cur->frame->area.x +
3781                          (hang ? cur->frame->area.width : 0);
3782
3783             if(his_offset - 1 < my_offset)
3784                 continue;
3785             
3786             if(his_offset > dest)
3787                 continue;
3788
3789             HIT_EDGE(my_edge_start, my_edge_end, his_edge_start, his_edge_end)
3790         }
3791         break;
3792     case OB_DIRECTION_NORTHEAST:
3793     case OB_DIRECTION_SOUTHEAST:
3794     case OB_DIRECTION_NORTHWEST:
3795     case OB_DIRECTION_SOUTHWEST:
3796         /* not implemented */
3797     default:
3798         g_assert_not_reached();
3799         dest = 0; /* suppress warning */
3800     }
3801     return dest;
3802 }
3803
3804 ObClient* client_under_pointer()
3805 {
3806     gint x, y;
3807     GList *it;
3808     ObClient *ret = NULL;
3809
3810     if (screen_pointer_pos(&x, &y)) {
3811         for (it = stacking_list; it; it = g_list_next(it)) {
3812             if (WINDOW_IS_CLIENT(it->data)) {
3813                 ObClient *c = WINDOW_AS_CLIENT(it->data);
3814                 if (c->frame->visible &&
3815                     /* ignore all animating windows */
3816                     !frame_iconify_animating(c->frame) &&
3817                     RECT_CONTAINS(c->frame->area, x, y))
3818                 {
3819                     ret = c;
3820                     break;
3821                 }
3822             }
3823         }
3824     }
3825     return ret;
3826 }
3827
3828 gboolean client_has_group_siblings(ObClient *self)
3829 {
3830     return self->group && self->group->members->next;
3831 }
3832
3833 gboolean client_has_application_group_siblings(ObClient *self)
3834 {
3835     GSList *it;
3836
3837     if (!self->group) return FALSE;
3838
3839     for (it = self->group->members; it; it = g_slist_next(it)) {
3840         ObClient *c = it->data;
3841         if (c != self && client_application(c))
3842             return TRUE;
3843     }
3844     return FALSE;
3845 }