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