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