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