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