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