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