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