use const Rect* not Rect const*
[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, map_time;
207     guint32 user_time;
208     gboolean obplaced;
209
210     ob_debug("Managing window: 0x%lx", window);
211
212     map_time = event_get_server_time();
213
214     /* choose the events we want to receive on the CLIENT window
215        (ObPrompt windows can request events too) */
216     attrib_set.event_mask = CLIENT_EVENTMASK |
217         (prompt ? prompt->event_mask : 0);
218     attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
219     XChangeWindowAttributes(obt_display, window,
220                             CWEventMask|CWDontPropagate, &attrib_set);
221
222     /* create the ObClient struct, and populate it from the hints on the
223        window */
224     self = g_slice_new0(ObClient);
225     self->obwin.type = OB_WINDOW_CLASS_CLIENT;
226     self->window = window;
227     self->prompt = prompt;
228     self->managed = TRUE;
229
230     /* non-zero defaults */
231     self->wmstate = WithdrawnState; /* make sure it gets updated first time */
232     self->gravity = NorthWestGravity;
233     self->desktop = screen_num_desktops; /* always an invalid value */
234
235     /* get all the stuff off the window */
236     client_get_all(self, TRUE);
237
238     ob_debug("Window type: %d", self->type);
239     ob_debug("Window group: 0x%x", self->group?self->group->leader:0);
240     ob_debug("Window name: %s class: %s role: %s title: %s",
241              self->name, self->class, self->role, self->title);
242
243     /* per-app settings override stuff from client_get_all, and return the
244        settings for other uses too. the returned settings is a shallow copy,
245        that needs to be freed with g_free(). */
246     settings = client_get_settings_state(self);
247
248     /* now we have all of the window's information so we can set this up.
249        do this before creating the frame, so it can tell that we are still
250        mapping and doesn't go applying things right away */
251     client_setup_decor_and_functions(self, FALSE);
252
253     /* specify that if we exit, the window should not be destroyed and
254        should be reparented back to root automatically, unless we are managing
255        an internal ObPrompt window  */
256     if (!self->prompt)
257         XChangeSaveSet(obt_display, window, SetModeInsert);
258
259     /* create the decoration frame for the client window */
260     self->frame = frame_new(self);
261
262     frame_grab_client(self->frame);
263
264     /* we've grabbed everything and set everything that we need to at mapping
265        time now */
266     grab_server(FALSE);
267
268     /* the session should get the last say though */
269     client_restore_session_state(self);
270
271     /* tell startup notification that this app started */
272     launch_time = sn_app_started(self->startup_id, self->class, self->name);
273
274     if (!OBT_PROP_GET32(self->window, NET_WM_USER_TIME, CARDINAL, &user_time))
275         user_time = map_time;
276
277     /* do this after we have a frame.. it uses the frame to help determine the
278        WM_STATE to apply. */
279     client_change_state(self);
280
281     /* add ourselves to the focus order */
282     focus_order_add_new(self);
283
284     /* do this to add ourselves to the stacking list in a non-intrusive way */
285     client_calc_layer(self);
286
287     /* focus the new window? */
288     if (ob_state() != OB_STATE_STARTING &&
289         (!self->session || self->session->focused) &&
290         /* this means focus=true for window is same as config_focus_new=true */
291         ((config_focus_new || (settings && settings->focus == 1)) ||
292          client_search_focus_tree_full(self)) &&
293         /* NET_WM_USER_TIME 0 when mapping means don't focus */
294         (user_time != 0) &&
295         /* this checks for focus=false for the window */
296         (!settings || settings->focus != 0) &&
297         focus_valid_target(self, self->desktop,
298                            FALSE, FALSE, TRUE, FALSE, FALSE,
299                            settings->focus == 1))
300     {
301         activate = TRUE;
302     }
303
304     /* remove the client's border */
305     XSetWindowBorderWidth(obt_display, self->window, 0);
306
307     /* adjust the frame to the client's size before showing or placing
308        the window */
309     frame_adjust_area(self->frame, FALSE, TRUE, FALSE);
310     frame_adjust_client_area(self->frame);
311
312     /* where the frame was placed is where the window was originally */
313     place = self->area;
314
315     /* figure out placement for the window if the window is new */
316     if (ob_state() == OB_STATE_RUNNING) {
317         ob_debug("Positioned: %s @ %d %d",
318                  (!self->positioned ? "no" :
319                   (self->positioned == PPosition ? "program specified" :
320                    (self->positioned == USPosition ? "user specified" :
321                     (self->positioned == (PPosition | USPosition) ?
322                      "program + user specified" :
323                      "BADNESS !?")))), place.x, place.y);
324
325         ob_debug("Sized: %s @ %d %d",
326                  (!self->sized ? "no" :
327                   (self->sized == PSize ? "program specified" :
328                    (self->sized == USSize ? "user specified" :
329                     (self->sized == (PSize | USSize) ?
330                      "program + user specified" :
331                      "BADNESS !?")))), place.width, place.height);
332
333         obplaced = place_client(self, &place.x, &place.y, settings);
334
335         /* watch for buggy apps that ask to be placed at (0,0) when there is
336            a strut there */
337         if (!obplaced && place.x == 0 && place.y == 0 &&
338             /* non-normal windows are allowed */
339             client_normal(self) &&
340             /* oldschool fullscreen windows are allowed */
341             !client_is_oldfullscreen(self, &place))
342         {
343             Rect *r;
344
345             r = screen_area(self->desktop, SCREEN_AREA_ALL_MONITORS, NULL);
346             if (r->x || r->y) {
347                 place.x = r->x;
348                 place.y = r->y;
349                 ob_debug("Moving buggy app from (0,0) to (%d,%d)", r->x, r->y);
350             }
351             g_slice_free(Rect, r);
352         }
353
354         /* make sure the window is visible. */
355         client_find_onscreen(self, &place.x, &place.y,
356                              place.width, place.height,
357                              /* non-normal clients has less rules, and
358                                 windows that are being restored from a
359                                 session do also. we can assume you want
360                                 it back where you saved it. Clients saying
361                                 they placed themselves are subjected to
362                                 harder rules, ones that are placed by
363                                 place.c or by the user are allowed partially
364                                 off-screen and on xinerama divides (ie,
365                                 it is up to the placement routines to avoid
366                                 the xinerama divides)
367
368                                 children and splash screens are forced on
369                                 screen, but i don't remember why i decided to
370                                 do that.
371                              */
372                              ob_state() == OB_STATE_RUNNING &&
373                              (self->type == OB_CLIENT_TYPE_DIALOG ||
374                               self->type == OB_CLIENT_TYPE_SPLASH ||
375                               (!((self->positioned & USPosition) ||
376                                  (settings && settings->pos_given)) &&
377                                client_normal(self) &&
378                                !self->session &&
379                                /* don't move oldschool fullscreen windows to
380                                   fit inside the struts (fixes Acroread, which
381                                   makes its fullscreen window fit the screen
382                                   but it is not USSize'd or USPosition'd) */
383                                !client_is_oldfullscreen(self, &place))));
384     }
385
386     /* if the window isn't user-sized, then make it fit inside
387        the visible screen area on its monitor. Use basically the same rules
388        for forcing the window on screen in the client_find_onscreen call.
389
390        do this after place_client, it chooses the monitor!
391
392        splash screens get "transient" set to TRUE by
393        the place_client call
394     */
395     if (ob_state() == OB_STATE_RUNNING &&
396         (transient ||
397          (!(self->sized & USSize || self->positioned & USPosition) &&
398           client_normal(self) &&
399           !self->session &&
400           /* don't shrink oldschool fullscreen windows to fit inside the
401              struts (fixes Acroread, which makes its fullscreen window
402              fit the screen but it is not USSize'd or USPosition'd) */
403           !client_is_oldfullscreen(self, &place))))
404     {
405         Rect *a = screen_area(self->desktop, SCREEN_AREA_ONE_MONITOR, &place);
406
407         /* get the size of the frame */
408         place.width += self->frame->size.left + self->frame->size.right;
409         place.height += self->frame->size.top + self->frame->size.bottom;
410
411         /* fit the window inside the area */
412         place.width = MIN(place.width, a->width);
413         place.height = MIN(place.height, a->height);
414
415         ob_debug("setting window size to %dx%d", place.width, place.height);
416
417         /* get the size of the client back */
418         place.width -= self->frame->size.left + self->frame->size.right;
419         place.height -= self->frame->size.top + self->frame->size.bottom;
420
421         g_slice_free(Rect, a);
422     }
423
424     ob_debug("placing window 0x%x at %d, %d with size %d x %d. "
425              "some restrictions may apply",
426              self->window, place.x, place.y, place.width, place.height);
427     if (self->session)
428         ob_debug("  but session requested %d, %d  %d x %d instead, "
429                  "overriding",
430                  self->session->x, self->session->y,
431                  self->session->w, self->session->h);
432
433     /* do this after the window is placed, so the premax/prefullscreen numbers
434        won't be all wacko!!
435
436        this also places the window
437     */
438     client_apply_startup_state(self, place.x, place.y,
439                                place.width, place.height);
440
441     ob_debug_type(OB_DEBUG_FOCUS, "Going to try activate new window? %s",
442                   activate ? "yes" : "no");
443     if (activate) {
444         activate = client_can_steal_focus(self, map_time, launch_time);
445
446         if (!activate) {
447             /* if the client isn't stealing focus, then hilite it so the user
448                knows it is there, but don't do this if we're restoring from a
449                session */
450             if (!client_restore_session_stacking(self))
451                 client_hilite(self, TRUE);
452         }
453     }
454     else {
455         /* This may look rather odd. Well it's because new windows are added
456            to the stacking order non-intrusively. If we're not going to focus
457            the new window or hilite it, then we raise it to the top. This will
458            take affect for things that don't get focused like splash screens.
459            Also if you don't have focus_new enabled, then it's going to get
460            raised to the top. Legacy begets legacy I guess?
461         */
462         if (!client_restore_session_stacking(self))
463             stacking_raise(CLIENT_AS_WINDOW(self));
464     }
465
466     mouse_grab_for_client(self, TRUE);
467
468     /* this has to happen before we try focus the window, but we want it to
469        happen after the client's stacking has been determined or it looks bad
470     */
471     {
472         gulong ignore_start;
473         if (!config_focus_under_mouse)
474             ignore_start = event_start_ignore_all_enters();
475
476         client_show(self);
477
478         if (!config_focus_under_mouse)
479             event_end_ignore_all_enters(ignore_start);
480     }
481
482     if (activate) {
483         gboolean stacked = client_restore_session_stacking(self);
484         client_present(self, FALSE, !stacked, TRUE);
485     }
486
487     /* add to client list/map */
488     client_list = g_list_append(client_list, self);
489     window_add(&self->window, CLIENT_AS_WINDOW(self));
490
491     /* this has to happen after we're in the client_list */
492     if (STRUT_EXISTS(self->strut))
493         screen_update_areas();
494
495     /* update the list hints */
496     client_set_list();
497
498     /* free the ObAppSettings shallow copy */
499     g_slice_free(ObAppSettings, settings);
500
501     ob_debug("Managed window 0x%lx plate 0x%x (%s)",
502              window, self->frame->window, self->class);
503 }
504
505 ObClient *client_fake_manage(Window window)
506 {
507     ObClient *self;
508     ObAppSettings *settings;
509
510     ob_debug("Pretend-managing window: %lx", window);
511
512     /* do this minimal stuff to figure out the client's decorations */
513
514     self = g_slice_new0(ObClient);
515     self->window = window;
516
517     client_get_all(self, FALSE);
518     /* per-app settings override stuff, and return the settings for other
519        uses too. this returns a shallow copy that needs to be freed */
520     settings = client_get_settings_state(self);
521
522     client_setup_decor_and_functions(self, FALSE);
523
524     /* create the decoration frame for the client window and adjust its size */
525     self->frame = frame_new(self);
526     frame_adjust_area(self->frame, FALSE, TRUE, TRUE);
527
528     ob_debug("gave extents left %d right %d top %d bottom %d",
529              self->frame->size.left, self->frame->size.right,
530              self->frame->size.top, self->frame->size.bottom);
531
532     /* free the ObAppSettings shallow copy */
533     g_slice_free(ObAppSettings, settings);
534
535     return self;
536 }
537
538 void client_unmanage_all(void)
539 {
540     while (client_list)
541         client_unmanage(client_list->data);
542 }
543
544 void client_unmanage(ObClient *self)
545 {
546     GSList *it;
547     gulong ignore_start;
548
549     ob_debug("Unmanaging window: 0x%x plate 0x%x (%s) (%s)",
550              self->window, self->frame->window,
551              self->class, self->title ? self->title : "");
552
553     g_assert(self != NULL);
554
555     /* we dont want events no more. do this before hiding the frame so we
556        don't generate more events */
557     XSelectInput(obt_display, self->window, NoEventMask);
558
559     /* ignore enter events from the unmap so it doesnt mess with the focus */
560     if (!config_focus_under_mouse)
561         ignore_start = event_start_ignore_all_enters();
562
563     frame_hide(self->frame);
564     /* flush to send the hide to the server quickly */
565     XFlush(obt_display);
566
567     if (!config_focus_under_mouse)
568         event_end_ignore_all_enters(ignore_start);
569
570     mouse_grab_for_client(self, FALSE);
571
572     self->managed = FALSE;
573
574     /* remove the window from our save set, unless we are managing an internal
575        ObPrompt window */
576     if (!self->prompt)
577         XChangeSaveSet(obt_display, self->window, SetModeDelete);
578
579     /* update the focus lists */
580     focus_order_remove(self);
581     if (client_focused(self)) {
582         /* don't leave an invalid focus_client */
583         focus_client = NULL;
584     }
585
586     /* if we're prompting to kill the client, close that */
587     prompt_unref(self->kill_prompt);
588     self->kill_prompt = NULL;
589
590     client_list = g_list_remove(client_list, self);
591     stacking_remove(self);
592     window_remove(self->window);
593
594     /* once the client is out of the list, update the struts to remove its
595        influence */
596     if (STRUT_EXISTS(self->strut))
597         screen_update_areas();
598
599     client_call_notifies(self, client_destroy_notifies);
600
601     /* tell our parent(s) that we're gone */
602     for (it = self->parents; it; it = g_slist_next(it))
603         ((ObClient*)it->data)->transients =
604             g_slist_remove(((ObClient*)it->data)->transients,self);
605
606     /* tell our transients that we're gone */
607     for (it = self->transients; it; it = g_slist_next(it)) {
608         ((ObClient*)it->data)->parents =
609             g_slist_remove(((ObClient*)it->data)->parents, self);
610         /* we could be keeping our children in a higher layer */
611         client_calc_layer(it->data);
612     }
613
614     /* remove from its group */
615     if (self->group) {
616         group_remove(self->group, self);
617         self->group = NULL;
618     }
619
620     /* restore the window's original geometry so it is not lost */
621     {
622         Rect a;
623
624         a = self->area;
625
626         if (self->fullscreen)
627             a = self->pre_fullscreen_area;
628         else if (self->max_horz || self->max_vert) {
629             if (self->max_horz) {
630                 a.x = self->pre_max_area.x;
631                 a.width = self->pre_max_area.width;
632             }
633             if (self->max_vert) {
634                 a.y = self->pre_max_area.y;
635                 a.height = self->pre_max_area.height;
636             }
637         }
638
639         self->fullscreen = self->max_horz = self->max_vert = FALSE;
640         /* let it be moved and resized no matter what */
641         self->functions = OB_CLIENT_FUNC_MOVE | OB_CLIENT_FUNC_RESIZE;
642         self->decorations = 0; /* unmanaged windows have no decor */
643
644         /* give the client its border back */
645         XSetWindowBorderWidth(obt_display, self->window, self->border_width);
646
647         client_move_resize(self, a.x, a.y, a.width, a.height);
648     }
649
650     /* reparent the window out of the frame, and free the frame */
651     frame_release_client(self->frame);
652     frame_free(self->frame);
653     self->frame = NULL;
654
655     if (ob_state() != OB_STATE_EXITING) {
656         /* these values should not be persisted across a window
657            unmapping/mapping */
658         OBT_PROP_ERASE(self->window, NET_WM_DESKTOP);
659         OBT_PROP_ERASE(self->window, NET_WM_STATE);
660         OBT_PROP_ERASE(self->window, WM_STATE);
661     } else {
662         /* if we're left in an unmapped state, the client wont be mapped.
663            this is bad, since we will no longer be managing the window on
664            restart */
665         XMapWindow(obt_display, self->window);
666     }
667
668     /* these should not be left on the window ever.  other window managers
669        don't necessarily use them and it will mess them up (like compiz) */
670     OBT_PROP_ERASE(self->window, NET_WM_VISIBLE_NAME);
671     OBT_PROP_ERASE(self->window, NET_WM_VISIBLE_ICON_NAME);
672
673     /* update the list hints */
674     client_set_list();
675
676     ob_debug("Unmanaged window 0x%lx", self->window);
677
678     /* free all data allocated in the client struct */
679     RrImageUnref(self->icon_set);
680     g_slist_free(self->transients);
681     g_free(self->startup_id);
682     g_free(self->wm_command);
683     g_free(self->title);
684     g_free(self->icon_title);
685     g_free(self->original_title);
686     g_free(self->name);
687     g_free(self->class);
688     g_free(self->role);
689     g_free(self->client_machine);
690     g_free(self->sm_client_id);
691     g_slice_free(ObClient, self);
692 }
693
694 void client_fake_unmanage(ObClient *self)
695 {
696     /* this is all that got allocated to get the decorations */
697
698     frame_free(self->frame);
699     g_slice_free(ObClient, self);
700 }
701
702 static gboolean client_can_steal_focus(ObClient *self, Time steal_time,
703                                        Time launch_time)
704 {
705     gboolean steal;
706     gboolean relative_focused;
707     gboolean parent_focused;
708
709     steal = TRUE;
710
711     parent_focused = (focus_client != NULL &&
712                       client_search_focus_parent(self));
713     relative_focused = (focus_client != NULL &&
714                         (client_search_focus_tree_full(self) != NULL ||
715                          client_search_focus_group_full(self) != NULL));
716
717     /* This is focus stealing prevention */
718     ob_debug_type(OB_DEBUG_FOCUS,
719                   "Want to focus window 0x%x at time %u "
720                   "launched at %u (last user interaction time %u)",
721                   self->window, steal_time, launch_time,
722                   event_last_user_time);
723
724     /* if it's on another desktop */
725     if (!(self->desktop == screen_desktop ||
726           self->desktop == DESKTOP_ALL) &&
727         /* the timestamp is from before you changed desktops */
728         (!launch_time ||
729          (screen_desktop_user_time &&
730           !event_time_after(launch_time, screen_desktop_user_time))))
731     {
732         steal = FALSE;
733         ob_debug_type(OB_DEBUG_FOCUS,
734                       "Not focusing the window because its on another "
735                       "desktop\n");
736     }
737     /* If something is focused... */
738     else if (focus_client) {
739         /* If the user is working in another window right now, then don't
740            steal focus */
741         if (!parent_focused &&
742             event_last_user_time && launch_time &&
743             event_time_after(event_last_user_time, launch_time) &&
744             event_last_user_time != launch_time &&
745             event_time_after(event_last_user_time,
746                              steal_time - OB_EVENT_USER_TIME_DELAY))
747         {
748             steal = FALSE;
749             ob_debug_type(OB_DEBUG_FOCUS,
750                           "Not focusing the window because the user is "
751                           "working in another window that is not "
752                           "its parent");
753         }
754         /* If the new window is a transient (and its relatives aren't
755            focused) */
756         else if (client_has_parent(self) && !relative_focused) {
757             steal = FALSE;
758             ob_debug_type(OB_DEBUG_FOCUS,
759                           "Not focusing the window because it is a "
760                           "transient, and its relatives aren't focused");
761         }
762         /* Don't steal focus from globally active clients.
763            I stole this idea from KWin. It seems nice.
764         */
765         else if (!(focus_client->can_focus ||
766                    focus_client->focus_notify))
767         {
768             steal = FALSE;
769             ob_debug_type(OB_DEBUG_FOCUS,
770                           "Not focusing the window because a globally "
771                           "active client has focus");
772         }
773         /* Don't move focus if it's not going to go to this window
774            anyway */
775         else if (client_focus_target(self) != self) {
776             steal = FALSE;
777             ob_debug_type(OB_DEBUG_FOCUS,
778                           "Not focusing the window because another window "
779                           "would get the focus anyway");
780         }
781         /* Don't move focus if the window is not visible on the current
782            desktop and none of its relatives are focused */
783         else if (!(self->desktop == screen_desktop ||
784                    self->desktop == DESKTOP_ALL) &&
785                  !relative_focused)
786         {
787             steal = FALSE;
788             ob_debug_type(OB_DEBUG_FOCUS,
789                           "Not focusing the window because it is on "
790                           "another desktop and no relatives are focused ");
791         }
792     }
793
794     if (!steal)
795         ob_debug_type(OB_DEBUG_FOCUS,
796                       "Focus stealing prevention activated for %s at "
797                       "time %u (last user interaction time %u)",
798                       self->title, steal_time, event_last_user_time);
799     return steal;
800 }
801
802 /*! Returns a new structure containing the per-app settings for this client.
803   The returned structure needs to be freed with g_free. */
804 static ObAppSettings *client_get_settings_state(ObClient *self)
805 {
806     ObAppSettings *settings;
807     GSList *it;
808
809     settings = config_create_app_settings();
810
811     for (it = config_per_app_settings; it; it = g_slist_next(it)) {
812         ObAppSettings *app = it->data;
813         gboolean match = TRUE;
814
815         g_assert(app->name != NULL || app->class != NULL ||
816                  app->role != NULL || app->title != NULL ||
817                  (signed)app->type >= 0);
818
819         if (app->name &&
820             !g_pattern_match(app->name, strlen(self->name), self->name, NULL))
821             match = FALSE;
822         else if (app->class &&
823                  !g_pattern_match(app->class,
824                                   strlen(self->class), self->class, NULL))
825             match = FALSE;
826         else if (app->role &&
827                  !g_pattern_match(app->role,
828                                   strlen(self->role), self->role, NULL))
829             match = FALSE;
830         else if (app->title &&
831                  !g_pattern_match(app->title,
832                                   strlen(self->title), self->title, NULL))
833             match = FALSE;
834         else if ((signed)app->type >= 0 && app->type != self->type) {
835             match = FALSE;
836         }
837
838         if (match) {
839             ob_debug("Window matching: %s", app->name);
840
841             /* copy the settings to our struct, overriding the existing
842                settings if they are not defaults */
843             config_app_settings_copy_non_defaults(app, settings);
844         }
845     }
846
847     if (settings->shade != -1)
848         self->shaded = !!settings->shade;
849     if (settings->decor != -1)
850         self->undecorated = !settings->decor;
851     if (settings->iconic != -1)
852         self->iconic = !!settings->iconic;
853     if (settings->skip_pager != -1)
854         self->skip_pager = !!settings->skip_pager;
855     if (settings->skip_taskbar != -1)
856         self->skip_taskbar = !!settings->skip_taskbar;
857
858     if (settings->max_vert != -1)
859         self->max_vert = !!settings->max_vert;
860     if (settings->max_horz != -1)
861         self->max_horz = !!settings->max_horz;
862
863     if (settings->fullscreen != -1)
864         self->fullscreen = !!settings->fullscreen;
865
866     if (settings->desktop) {
867         if (settings->desktop == DESKTOP_ALL)
868             self->desktop = settings->desktop;
869         else if (settings->desktop > 0 &&
870                  settings->desktop <= screen_num_desktops)
871             self->desktop = settings->desktop - 1;
872     }
873
874     if (settings->layer == -1) {
875         self->below = TRUE;
876         self->above = FALSE;
877     }
878     else if (settings->layer == 0) {
879         self->below = FALSE;
880         self->above = FALSE;
881     }
882     else if (settings->layer == 1) {
883         self->below = FALSE;
884         self->above = TRUE;
885     }
886     return settings;
887 }
888
889 static void client_restore_session_state(ObClient *self)
890 {
891     GList *it;
892
893     ob_debug_type(OB_DEBUG_SM,
894                   "Restore session for client %s", self->title);
895
896     if (!(it = session_state_find(self))) {
897         ob_debug_type(OB_DEBUG_SM,
898                       "Session data not found for client %s", self->title);
899         return;
900     }
901
902     self->session = it->data;
903
904     ob_debug_type(OB_DEBUG_SM, "Session data loaded for client %s",
905                   self->title);
906
907     RECT_SET_POINT(self->area, self->session->x, self->session->y);
908     self->positioned = USPosition;
909     self->sized = USSize;
910     if (self->session->w > 0)
911         self->area.width = self->session->w;
912     if (self->session->h > 0)
913         self->area.height = self->session->h;
914     XResizeWindow(obt_display, self->window,
915                   self->area.width, self->area.height);
916
917     self->desktop = (self->session->desktop == DESKTOP_ALL ?
918                      self->session->desktop :
919                      MIN(screen_num_desktops - 1, self->session->desktop));
920     OBT_PROP_SET32(self->window, NET_WM_DESKTOP, CARDINAL, self->desktop);
921
922     self->shaded = self->session->shaded;
923     self->iconic = self->session->iconic;
924     self->skip_pager = self->session->skip_pager;
925     self->skip_taskbar = self->session->skip_taskbar;
926     self->fullscreen = self->session->fullscreen;
927     self->above = self->session->above;
928     self->below = self->session->below;
929     self->max_horz = self->session->max_horz;
930     self->max_vert = self->session->max_vert;
931     self->undecorated = self->session->undecorated;
932 }
933
934 static gboolean client_restore_session_stacking(ObClient *self)
935 {
936     GList *it, *mypos;
937
938     if (!self->session) return FALSE;
939
940     mypos = g_list_find(session_saved_state, self->session);
941     if (!mypos) return FALSE;
942
943     /* start above me and look for the first client */
944     for (it = g_list_previous(mypos); it; it = g_list_previous(it)) {
945         GList *cit;
946
947         for (cit = client_list; cit; cit = g_list_next(cit)) {
948             ObClient *c = cit->data;
949             /* found a client that was in the session, so go below it */
950             if (c->session == it->data) {
951                 stacking_below(CLIENT_AS_WINDOW(self),
952                                CLIENT_AS_WINDOW(cit->data));
953                 return TRUE;
954             }
955         }
956     }
957     return FALSE;
958 }
959
960 void client_move_onscreen(ObClient *self, gboolean rude)
961 {
962     gint x = self->area.x;
963     gint y = self->area.y;
964     if (client_find_onscreen(self, &x, &y,
965                              self->area.width,
966                              self->area.height, rude)) {
967         client_move(self, x, y);
968     }
969 }
970
971 gboolean client_find_onscreen(ObClient *self, gint *x, gint *y, gint w, gint h,
972                               gboolean rude)
973 {
974     gint ox = *x, oy = *y;
975     gboolean rudel = rude, ruder = rude, rudet = rude, rudeb = rude;
976     gint fw, fh;
977     Rect desired;
978     guint i;
979     gboolean found_mon;
980
981     RECT_SET(desired, *x, *y, w, h);
982     frame_rect_to_frame(self->frame, &desired);
983
984     /* get where the frame would be */
985     frame_client_gravity(self->frame, x, y);
986
987     /* get the requested size of the window with decorations */
988     fw = self->frame->size.left + w + self->frame->size.right;
989     fh = self->frame->size.top + h + self->frame->size.bottom;
990
991     /* If rudeness wasn't requested, then still be rude in a given direction
992        if the client is not moving, only resizing in that direction */
993     if (!rude) {
994         Point oldtl, oldtr, oldbl, oldbr;
995         Point newtl, newtr, newbl, newbr;
996         gboolean stationary_l, stationary_r, stationary_t, stationary_b;
997
998         POINT_SET(oldtl, self->frame->area.x, self->frame->area.y);
999         POINT_SET(oldbr, self->frame->area.x + self->frame->area.width - 1,
1000                   self->frame->area.y + self->frame->area.height - 1);
1001         POINT_SET(oldtr, oldbr.x, oldtl.y);
1002         POINT_SET(oldbl, oldtl.x, oldbr.y);
1003
1004         POINT_SET(newtl, *x, *y);
1005         POINT_SET(newbr, *x + fw - 1, *y + fh - 1);
1006         POINT_SET(newtr, newbr.x, newtl.y);
1007         POINT_SET(newbl, newtl.x, newbr.y);
1008
1009         /* is it moving or just resizing from some corner? */
1010         stationary_l = oldtl.x == newtl.x;
1011         stationary_r = oldtr.x == newtr.x;
1012         stationary_t = oldtl.y == newtl.y;
1013         stationary_b = oldbl.y == newbl.y;
1014
1015         /* if left edge is growing and didnt move right edge */
1016         if (stationary_r && newtl.x < oldtl.x)
1017             rudel = TRUE;
1018         /* if right edge is growing and didnt move left edge */
1019         if (stationary_l && newtr.x > oldtr.x)
1020             ruder = TRUE;
1021         /* if top edge is growing and didnt move bottom edge */
1022         if (stationary_b && newtl.y < oldtl.y)
1023             rudet = TRUE;
1024         /* if bottom edge is growing and didnt move top edge */
1025         if (stationary_t && newbl.y > oldbl.y)
1026             rudeb = TRUE;
1027     }
1028
1029     /* we iterate through every monitor that the window is at least partially
1030        on, to make sure it is obeying the rules on them all
1031
1032        if the window does not appear on any monitors, then use the first one
1033     */
1034     found_mon = FALSE;
1035     for (i = 0; i < screen_num_monitors; ++i) {
1036         Rect *a;
1037
1038         if (!screen_physical_area_monitor_contains(i, &desired)) {
1039             if (i < screen_num_monitors - 1 || found_mon)
1040                 continue;
1041
1042             /* the window is not inside any monitor! so just use the first
1043                one */
1044             a = screen_area(self->desktop, 0, NULL);
1045         } else {
1046             found_mon = TRUE;
1047             a = screen_area(self->desktop, SCREEN_AREA_ONE_MONITOR, &desired);
1048         }
1049
1050         /* This makes sure windows aren't entirely outside of the screen so you
1051            can't see them at all.
1052            It makes sure 10% of the window is on the screen at least. And don't
1053            let it move itself off the top of the screen, which would hide the
1054            titlebar on you. (The user can still do this if they want too, it's
1055            only limiting the application.
1056         */
1057         if (client_normal(self)) {
1058             if (!self->strut.right && *x + fw/10 >= a->x + a->width - 1)
1059                 *x = a->x + a->width - fw/10;
1060             if (!self->strut.bottom && *y + fh/10 >= a->y + a->height - 1)
1061                 *y = a->y + a->height - fh/10;
1062             if (!self->strut.left && *x + fw*9/10 - 1 < a->x)
1063                 *x = a->x - fw*9/10;
1064             if (!self->strut.top && *y + fh*9/10 - 1 < a->y)
1065                 *y = a->y - fh*9/10;
1066         }
1067
1068         /* This here doesn't let windows even a pixel outside the
1069            struts/screen. When called from client_manage, programs placing
1070            themselves are forced completely onscreen, while things like
1071            xterm -geometry resolution-width/2 will work fine. Trying to
1072            place it completely offscreen will be handled in the above code.
1073            Sorry for this confused comment, i am tired. */
1074         if (rudel && !self->strut.left && *x < a->x) *x = a->x;
1075         if (ruder && !self->strut.right && *x + fw > a->x + a->width)
1076             *x = a->x + MAX(0, a->width - fw);
1077
1078         if (rudet && !self->strut.top && *y < a->y) *y = a->y;
1079         if (rudeb && !self->strut.bottom && *y + fh > a->y + a->height)
1080             *y = a->y + MAX(0, a->height - fh);
1081
1082         g_slice_free(Rect, a);
1083     }
1084
1085     /* get where the client should be */
1086     frame_frame_gravity(self->frame, x, y);
1087
1088     return ox != *x || oy != *y;
1089 }
1090
1091 static void client_get_all(ObClient *self, gboolean real)
1092 {
1093     /* this is needed for the frame to set itself up */
1094     client_get_area(self);
1095
1096     /* these things can change the decor and functions of the window */
1097
1098     client_get_mwm_hints(self);
1099     /* this can change the mwmhints for special cases */
1100     client_get_type_and_transientness(self);
1101     client_get_state(self);
1102     client_update_normal_hints(self);
1103
1104     /* get the session related properties, these can change decorations
1105        from per-app settings */
1106     client_get_session_ids(self);
1107
1108     /* now we got everything that can affect the decorations */
1109     if (!real)
1110         return;
1111
1112     /* get this early so we have it for debugging */
1113     client_update_title(self);
1114
1115     /* save the values of the variables used for app rule matching */
1116     client_save_app_rule_values(self);
1117
1118     client_update_protocols(self);
1119
1120     client_update_wmhints(self);
1121     /* this may have already been called from client_update_wmhints */
1122     if (!self->parents && !self->transient_for_group)
1123         client_update_transient_for(self);
1124
1125     client_get_startup_id(self);
1126     client_get_desktop(self);/* uses transient data/group/startup id if a
1127                                 desktop is not specified */
1128     client_get_shaped(self);
1129
1130     {
1131         /* a couple type-based defaults for new windows */
1132
1133         /* this makes sure that these windows appear on all desktops */
1134         if (self->type == OB_CLIENT_TYPE_DESKTOP)
1135             self->desktop = DESKTOP_ALL;
1136     }
1137
1138 #ifdef SYNC
1139     client_update_sync_request_counter(self);
1140 #endif
1141
1142     client_get_colormap(self);
1143     client_update_strut(self);
1144     client_update_icons(self);
1145     client_update_icon_geometry(self);
1146 }
1147
1148 static void client_get_startup_id(ObClient *self)
1149 {
1150     if (!(OBT_PROP_GETS(self->window, NET_STARTUP_ID, utf8,
1151                         &self->startup_id)))
1152         if (self->group)
1153             OBT_PROP_GETS(self->group->leader,
1154                           NET_STARTUP_ID, utf8, &self->startup_id);
1155 }
1156
1157 static void client_get_area(ObClient *self)
1158 {
1159     XWindowAttributes wattrib;
1160     Status ret;
1161
1162     ret = XGetWindowAttributes(obt_display, self->window, &wattrib);
1163     g_assert(ret != BadWindow);
1164
1165     RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
1166     POINT_SET(self->root_pos, wattrib.x, wattrib.y);
1167     self->border_width = wattrib.border_width;
1168
1169     ob_debug("client area: %d %d  %d %d  bw %d", wattrib.x, wattrib.y,
1170              wattrib.width, wattrib.height, wattrib.border_width);
1171 }
1172
1173 static void client_get_desktop(ObClient *self)
1174 {
1175     guint32 d = screen_num_desktops; /* an always-invalid value */
1176
1177     if (OBT_PROP_GET32(self->window, NET_WM_DESKTOP, CARDINAL, &d)) {
1178         if (d >= screen_num_desktops && d != DESKTOP_ALL)
1179             self->desktop = screen_num_desktops - 1;
1180         else
1181             self->desktop = d;
1182         ob_debug("client requested desktop 0x%x", self->desktop);
1183     } else {
1184         GSList *it;
1185         gboolean first = TRUE;
1186         guint all = screen_num_desktops; /* not a valid value */
1187
1188         /* if they are all on one desktop, then open it on the
1189            same desktop */
1190         for (it = self->parents; it; it = g_slist_next(it)) {
1191             ObClient *c = it->data;
1192
1193             if (c->desktop == DESKTOP_ALL) continue;
1194
1195             if (first) {
1196                 all = c->desktop;
1197                 first = FALSE;
1198             }
1199             else if (all != c->desktop)
1200                 all = screen_num_desktops; /* make it invalid */
1201         }
1202         if (all != screen_num_desktops) {
1203             self->desktop = all;
1204
1205             ob_debug("client desktop set from parents: 0x%x",
1206                      self->desktop);
1207         }
1208         /* try get from the startup-notification protocol */
1209         else if (sn_get_desktop(self->startup_id, &self->desktop)) {
1210             if (self->desktop >= screen_num_desktops &&
1211                 self->desktop != DESKTOP_ALL)
1212                 self->desktop = screen_num_desktops - 1;
1213             ob_debug("client desktop set from startup-notification: 0x%x",
1214                      self->desktop);
1215         }
1216         /* defaults to the current desktop */
1217         else {
1218             self->desktop = screen_desktop;
1219             ob_debug("client desktop set to the current desktop: %d",
1220                      self->desktop);
1221         }
1222     }
1223 }
1224
1225 static void client_get_state(ObClient *self)
1226 {
1227     guint32 *state;
1228     guint num;
1229
1230     if (OBT_PROP_GETA32(self->window, NET_WM_STATE, ATOM, &state, &num)) {
1231         gulong i;
1232         for (i = 0; i < num; ++i) {
1233             if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_MODAL))
1234                 self->modal = TRUE;
1235             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_SHADED))
1236                 self->shaded = TRUE;
1237             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_HIDDEN))
1238                 self->iconic = TRUE;
1239             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_SKIP_TASKBAR))
1240                 self->skip_taskbar = TRUE;
1241             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_SKIP_PAGER))
1242                 self->skip_pager = TRUE;
1243             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_FULLSCREEN))
1244                 self->fullscreen = TRUE;
1245             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_VERT))
1246                 self->max_vert = TRUE;
1247             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_HORZ))
1248                 self->max_horz = TRUE;
1249             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_ABOVE))
1250                 self->above = TRUE;
1251             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_BELOW))
1252                 self->below = TRUE;
1253             else if (state[i] == OBT_PROP_ATOM(NET_WM_STATE_DEMANDS_ATTENTION))
1254                 self->demands_attention = TRUE;
1255             else if (state[i] == OBT_PROP_ATOM(OB_WM_STATE_UNDECORATED))
1256                 self->undecorated = TRUE;
1257         }
1258
1259         g_free(state);
1260     }
1261 }
1262
1263 static void client_get_shaped(ObClient *self)
1264 {
1265     self->shaped = FALSE;
1266 #ifdef SHAPE
1267     if (obt_display_extension_shape) {
1268         gint foo;
1269         guint ufoo;
1270         gint s;
1271
1272         XShapeSelectInput(obt_display, self->window, ShapeNotifyMask);
1273
1274         XShapeQueryExtents(obt_display, self->window, &s, &foo,
1275                            &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
1276                            &ufoo);
1277         self->shaped = !!s;
1278     }
1279 #endif
1280 }
1281
1282 void client_update_transient_for(ObClient *self)
1283 {
1284     Window t = None;
1285     ObClient *target = NULL;
1286     gboolean trangroup = FALSE;
1287
1288     if (XGetTransientForHint(obt_display, self->window, &t)) {
1289         if (t != self->window) { /* can't be transient to itself! */
1290             ObWindow *tw = window_find(t);
1291             /* if this happens then we need to check for it */
1292             g_assert(tw != CLIENT_AS_WINDOW(self));
1293             if (tw && WINDOW_IS_CLIENT(tw)) {
1294                 /* watch out for windows with a parent that is something
1295                    different, like a dockapp for example */
1296                 target = WINDOW_AS_CLIENT(tw);
1297             }
1298         }
1299
1300         /* Setting the transient_for to Root is actually illegal, however
1301            applications from time have done this to specify transient for
1302            their group */
1303         if (!target && self->group && t == obt_root(ob_screen))
1304             trangroup = TRUE;
1305     } else if (self->group && self->transient)
1306         trangroup = TRUE;
1307
1308     client_update_transient_tree(self, self->group, self->group,
1309                                  self->transient_for_group, trangroup,
1310                                  client_direct_parent(self), target);
1311     self->transient_for_group = trangroup;
1312
1313 }
1314
1315 static void client_update_transient_tree(ObClient *self,
1316                                          ObGroup *oldgroup, ObGroup *newgroup,
1317                                          gboolean oldgtran, gboolean newgtran,
1318                                          ObClient* oldparent,
1319                                          ObClient *newparent)
1320 {
1321     GSList *it, *next;
1322     ObClient *c;
1323
1324     g_assert(!oldgtran || oldgroup);
1325     g_assert(!newgtran || newgroup);
1326     g_assert((!oldgtran && !oldparent) ||
1327              (oldgtran && !oldparent) ||
1328              (!oldgtran && oldparent));
1329     g_assert((!newgtran && !newparent) ||
1330              (newgtran && !newparent) ||
1331              (!newgtran && newparent));
1332
1333     /* * *
1334       Group transient windows are not allowed to have other group
1335       transient windows as their children.
1336       * * */
1337
1338     /* No change has occured */
1339     if (oldgroup == newgroup &&
1340         oldgtran == newgtran &&
1341         oldparent == newparent) return;
1342
1343     /** Remove the client from the transient tree **/
1344
1345     for (it = self->transients; it; it = next) {
1346         next = g_slist_next(it);
1347         c = it->data;
1348         self->transients = g_slist_delete_link(self->transients, it);
1349         c->parents = g_slist_remove(c->parents, self);
1350     }
1351     for (it = self->parents; it; it = next) {
1352         next = g_slist_next(it);
1353         c = it->data;
1354         self->parents = g_slist_delete_link(self->parents, it);
1355         c->transients = g_slist_remove(c->transients, self);
1356     }
1357
1358     /** Re-add the client to the transient tree **/
1359
1360     /* If we're transient for a group then we need to add ourselves to all our
1361        parents */
1362     if (newgtran) {
1363         for (it = newgroup->members; it; it = g_slist_next(it)) {
1364             c = it->data;
1365             if (c != self &&
1366                 !client_search_top_direct_parent(c)->transient_for_group &&
1367                 client_normal(c))
1368             {
1369                 c->transients = g_slist_prepend(c->transients, self);
1370                 self->parents = g_slist_prepend(self->parents, c);
1371             }
1372         }
1373     }
1374
1375     /* If we are now transient for a single window we need to add ourselves to
1376        its children
1377
1378        WARNING: Cyclical transient-ness is possible if two windows are
1379        transient for eachother.
1380     */
1381     else if (newparent &&
1382              /* don't make ourself its child if it is already our child */
1383              !client_is_direct_child(self, newparent) &&
1384              client_normal(newparent))
1385     {
1386         newparent->transients = g_slist_prepend(newparent->transients, self);
1387         self->parents = g_slist_prepend(self->parents, newparent);
1388     }
1389
1390     /* Add any group transient windows to our children. But if we're transient
1391        for the group, then other group transients are not our children.
1392
1393        WARNING: Cyclical transient-ness is possible. For e.g. if:
1394        A is transient for the group
1395        B is transient for A
1396        C is transient for B
1397        A can't be transient for C or we have a cycle
1398     */
1399     if (!newgtran && newgroup &&
1400         (!newparent ||
1401          !client_search_top_direct_parent(newparent)->transient_for_group) &&
1402         client_normal(self))
1403     {
1404         for (it = newgroup->members; it; it = g_slist_next(it)) {
1405             c = it->data;
1406             if (c != self && c->transient_for_group &&
1407                 /* Don't make it our child if it is already our parent */
1408                 !client_is_direct_child(c, self))
1409             {
1410                 self->transients = g_slist_prepend(self->transients, c);
1411                 c->parents = g_slist_prepend(c->parents, self);
1412             }
1413         }
1414     }
1415
1416     /** If we change our group transient-ness, our children change their
1417         effective group transient-ness, which affects how they relate to other
1418         group windows **/
1419
1420     for (it = self->transients; it; it = g_slist_next(it)) {
1421         c = it->data;
1422         if (!c->transient_for_group)
1423             client_update_transient_tree(c, c->group, c->group,
1424                                          c->transient_for_group,
1425                                          c->transient_for_group,
1426                                          client_direct_parent(c),
1427                                          client_direct_parent(c));
1428     }
1429 }
1430
1431 void client_get_mwm_hints(ObClient *self)
1432 {
1433     guint num;
1434     guint32 *hints;
1435
1436     self->mwmhints.flags = 0; /* default to none */
1437
1438     if (OBT_PROP_GETA32(self->window, MOTIF_WM_HINTS, MOTIF_WM_HINTS,
1439                         &hints, &num)) {
1440         if (num >= OB_MWM_ELEMENTS) {
1441             self->mwmhints.flags = hints[0];
1442             self->mwmhints.functions = hints[1];
1443             self->mwmhints.decorations = hints[2];
1444         }
1445         g_free(hints);
1446     }
1447 }
1448
1449 void client_get_type_and_transientness(ObClient *self)
1450 {
1451     guint num, i;
1452     guint32 *val;
1453     Window t;
1454
1455     self->type = -1;
1456     self->transient = FALSE;
1457
1458     if (OBT_PROP_GETA32(self->window, NET_WM_WINDOW_TYPE, ATOM, &val, &num)) {
1459         /* use the first value that we know about in the array */
1460         for (i = 0; i < num; ++i) {
1461             if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DESKTOP))
1462                 self->type = OB_CLIENT_TYPE_DESKTOP;
1463             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DOCK))
1464                 self->type = OB_CLIENT_TYPE_DOCK;
1465             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_TOOLBAR))
1466                 self->type = OB_CLIENT_TYPE_TOOLBAR;
1467             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_MENU))
1468                 self->type = OB_CLIENT_TYPE_MENU;
1469             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_UTILITY))
1470                 self->type = OB_CLIENT_TYPE_UTILITY;
1471             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_SPLASH))
1472                 self->type = OB_CLIENT_TYPE_SPLASH;
1473             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DIALOG))
1474                 self->type = OB_CLIENT_TYPE_DIALOG;
1475             else if (val[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_NORMAL))
1476                 self->type = OB_CLIENT_TYPE_NORMAL;
1477             else if (val[i] == OBT_PROP_ATOM(KDE_NET_WM_WINDOW_TYPE_OVERRIDE))
1478             {
1479                 /* prevent this window from getting any decor or
1480                    functionality */
1481                 self->mwmhints.flags &= (OB_MWM_FLAG_FUNCTIONS |
1482                                          OB_MWM_FLAG_DECORATIONS);
1483                 self->mwmhints.decorations = 0;
1484                 self->mwmhints.functions = 0;
1485             }
1486             if (self->type != (ObClientType) -1)
1487                 break; /* grab the first legit type */
1488         }
1489         g_free(val);
1490     }
1491
1492     if (XGetTransientForHint(obt_display, self->window, &t))
1493         self->transient = TRUE;
1494
1495     if (self->type == (ObClientType) -1) {
1496         /*the window type hint was not set, which means we either classify
1497           ourself as a normal window or a dialog, depending on if we are a
1498           transient. */
1499         if (self->transient)
1500             self->type = OB_CLIENT_TYPE_DIALOG;
1501         else
1502             self->type = OB_CLIENT_TYPE_NORMAL;
1503     }
1504
1505     /* then, based on our type, we can update our transientness.. */
1506     if (self->type == OB_CLIENT_TYPE_DIALOG ||
1507         self->type == OB_CLIENT_TYPE_TOOLBAR ||
1508         self->type == OB_CLIENT_TYPE_MENU ||
1509         self->type == OB_CLIENT_TYPE_UTILITY)
1510     {
1511         self->transient = TRUE;
1512     }
1513 }
1514
1515 void client_update_protocols(ObClient *self)
1516 {
1517     guint32 *proto;
1518     guint num_ret, i;
1519
1520     self->focus_notify = FALSE;
1521     self->delete_window = FALSE;
1522
1523     if (OBT_PROP_GETA32(self->window, WM_PROTOCOLS, ATOM, &proto, &num_ret)) {
1524         for (i = 0; i < num_ret; ++i) {
1525             if (proto[i] == OBT_PROP_ATOM(WM_DELETE_WINDOW))
1526                 /* this means we can request the window to close */
1527                 self->delete_window = TRUE;
1528             else if (proto[i] == OBT_PROP_ATOM(WM_TAKE_FOCUS))
1529                 /* if this protocol is requested, then the window will be
1530                    notified whenever we want it to receive focus */
1531                 self->focus_notify = TRUE;
1532             else if (proto[i] == OBT_PROP_ATOM(NET_WM_PING))
1533                 /* if this protocol is requested, then the window will allow
1534                    pings to determine if it is still alive */
1535                 self->ping = TRUE;
1536 #ifdef SYNC
1537             else if (proto[i] == OBT_PROP_ATOM(NET_WM_SYNC_REQUEST))
1538                 /* if this protocol is requested, then resizing the
1539                    window will be synchronized between the frame and the
1540                    client */
1541                 self->sync_request = TRUE;
1542 #endif
1543         }
1544         g_free(proto);
1545     }
1546 }
1547
1548 #ifdef SYNC
1549 void client_update_sync_request_counter(ObClient *self)
1550 {
1551     guint32 i;
1552
1553     if (OBT_PROP_GET32(self->window, NET_WM_SYNC_REQUEST_COUNTER, CARDINAL,&i))
1554     {
1555         self->sync_counter = i;
1556     } else
1557         self->sync_counter = None;
1558 }
1559 #endif
1560
1561 static void client_get_colormap(ObClient *self)
1562 {
1563     XWindowAttributes wa;
1564
1565     if (XGetWindowAttributes(obt_display, self->window, &wa))
1566         client_update_colormap(self, wa.colormap);
1567 }
1568
1569 void client_update_colormap(ObClient *self, Colormap colormap)
1570 {
1571     if (colormap == self->colormap) return;
1572
1573     ob_debug("Setting client %s colormap: 0x%x", self->title, colormap);
1574
1575     if (client_focused(self)) {
1576         screen_install_colormap(self, FALSE); /* uninstall old one */
1577         self->colormap = colormap;
1578         screen_install_colormap(self, TRUE); /* install new one */
1579     } else
1580         self->colormap = colormap;
1581 }
1582
1583 void client_update_normal_hints(ObClient *self)
1584 {
1585     XSizeHints size;
1586     glong ret;
1587
1588     /* defaults */
1589     self->min_ratio = 0.0f;
1590     self->max_ratio = 0.0f;
1591     SIZE_SET(self->size_inc, 1, 1);
1592     SIZE_SET(self->base_size, -1, -1);
1593     SIZE_SET(self->min_size, 0, 0);
1594     SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
1595
1596     /* get the hints from the window */
1597     if (XGetWMNormalHints(obt_display, self->window, &size, &ret)) {
1598         /* normal windows can't request placement! har har
1599         if (!client_normal(self))
1600         */
1601         self->positioned = (size.flags & (PPosition|USPosition));
1602         self->sized = (size.flags & (PSize|USSize));
1603
1604         if (size.flags & PWinGravity)
1605             self->gravity = size.win_gravity;
1606
1607         if (size.flags & PAspect) {
1608             if (size.min_aspect.y)
1609                 self->min_ratio =
1610                     (gfloat) size.min_aspect.x / size.min_aspect.y;
1611             if (size.max_aspect.y)
1612                 self->max_ratio =
1613                     (gfloat) size.max_aspect.x / size.max_aspect.y;
1614         }
1615
1616         if (size.flags & PMinSize)
1617             SIZE_SET(self->min_size, size.min_width, size.min_height);
1618
1619         if (size.flags & PMaxSize)
1620             SIZE_SET(self->max_size, size.max_width, size.max_height);
1621
1622         if (size.flags & PBaseSize)
1623             SIZE_SET(self->base_size, size.base_width, size.base_height);
1624
1625         if (size.flags & PResizeInc && size.width_inc && size.height_inc)
1626             SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
1627
1628         ob_debug("Normal hints: min size (%d %d) max size (%d %d)",
1629                  self->min_size.width, self->min_size.height,
1630                  self->max_size.width, self->max_size.height);
1631         ob_debug("size inc (%d %d) base size (%d %d)",
1632                  self->size_inc.width, self->size_inc.height,
1633                  self->base_size.width, self->base_size.height);
1634     }
1635     else
1636         ob_debug("Normal hints: not set");
1637 }
1638
1639 void client_setup_decor_and_functions(ObClient *self, gboolean reconfig)
1640 {
1641     /* start with everything (cept fullscreen) */
1642     self->decorations =
1643         (OB_FRAME_DECOR_TITLEBAR |
1644          OB_FRAME_DECOR_HANDLE |
1645          OB_FRAME_DECOR_GRIPS |
1646          OB_FRAME_DECOR_BORDER |
1647          OB_FRAME_DECOR_ICON |
1648          OB_FRAME_DECOR_ALLDESKTOPS |
1649          OB_FRAME_DECOR_ICONIFY |
1650          OB_FRAME_DECOR_MAXIMIZE |
1651          OB_FRAME_DECOR_SHADE |
1652          OB_FRAME_DECOR_CLOSE);
1653     self->functions =
1654         (OB_CLIENT_FUNC_RESIZE |
1655          OB_CLIENT_FUNC_MOVE |
1656          OB_CLIENT_FUNC_ICONIFY |
1657          OB_CLIENT_FUNC_MAXIMIZE |
1658          OB_CLIENT_FUNC_SHADE |
1659          OB_CLIENT_FUNC_CLOSE |
1660          OB_CLIENT_FUNC_BELOW |
1661          OB_CLIENT_FUNC_ABOVE |
1662          OB_CLIENT_FUNC_UNDECORATE);
1663
1664     if (!(self->min_size.width < self->max_size.width ||
1665           self->min_size.height < self->max_size.height))
1666         self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1667
1668     switch (self->type) {
1669     case OB_CLIENT_TYPE_NORMAL:
1670         /* normal windows retain all of the possible decorations and
1671            functionality, and can be fullscreen */
1672         self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1673         break;
1674
1675     case OB_CLIENT_TYPE_DIALOG:
1676         /* sometimes apps make dialog windows fullscreen for some reason (for
1677            e.g. kpdf does this..) */
1678         self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1679         break;
1680
1681     case OB_CLIENT_TYPE_UTILITY:
1682         /* these windows don't have anything added or removed by default */
1683         break;
1684
1685     case OB_CLIENT_TYPE_MENU:
1686     case OB_CLIENT_TYPE_TOOLBAR:
1687         /* these windows can't iconify or maximize */
1688         self->decorations &= ~(OB_FRAME_DECOR_ICONIFY |
1689                                OB_FRAME_DECOR_MAXIMIZE);
1690         self->functions &= ~(OB_CLIENT_FUNC_ICONIFY |
1691                              OB_CLIENT_FUNC_MAXIMIZE);
1692         break;
1693
1694     case OB_CLIENT_TYPE_SPLASH:
1695         /* these don't get get any decorations, and the only thing you can
1696            do with them is move them */
1697         self->decorations = 0;
1698         self->functions = OB_CLIENT_FUNC_MOVE;
1699         break;
1700
1701     case OB_CLIENT_TYPE_DESKTOP:
1702         /* these windows are not manipulated by the window manager */
1703         self->decorations = 0;
1704         self->functions = 0;
1705         break;
1706
1707     case OB_CLIENT_TYPE_DOCK:
1708         /* these windows are not manipulated by the window manager, but they
1709            can set below layer which has a special meaning */
1710         self->decorations = 0;
1711         self->functions = OB_CLIENT_FUNC_BELOW;
1712         break;
1713     }
1714
1715     /* Mwm Hints are applied subtractively to what has already been chosen for
1716        decor and functionality */
1717     if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
1718         if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
1719             if (! ((self->mwmhints.decorations & OB_MWM_DECOR_HANDLE) ||
1720                    (self->mwmhints.decorations & OB_MWM_DECOR_TITLE)))
1721             {
1722                 /* if the mwm hints request no handle or title, then all
1723                    decorations are disabled, but keep the border if that's
1724                    specified */
1725                 if (self->mwmhints.decorations & OB_MWM_DECOR_BORDER)
1726                     self->decorations = OB_FRAME_DECOR_BORDER;
1727                 else
1728                     self->decorations = 0;
1729             }
1730         }
1731     }
1732
1733     if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
1734         if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
1735             if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
1736                 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1737             if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
1738                 self->functions &= ~OB_CLIENT_FUNC_MOVE;
1739             /* dont let mwm hints kill any buttons
1740                if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
1741                self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
1742                if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
1743                self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1744             */
1745             /* dont let mwm hints kill the close button
1746                if (! (self->mwmhints.functions & MwmFunc_Close))
1747                self->functions &= ~OB_CLIENT_FUNC_CLOSE; */
1748         }
1749     }
1750
1751     if (!(self->functions & OB_CLIENT_FUNC_SHADE))
1752         self->decorations &= ~OB_FRAME_DECOR_SHADE;
1753     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY))
1754         self->decorations &= ~OB_FRAME_DECOR_ICONIFY;
1755     if (!(self->functions & OB_CLIENT_FUNC_RESIZE))
1756         self->decorations &= ~(OB_FRAME_DECOR_GRIPS | OB_FRAME_DECOR_HANDLE);
1757
1758     /* can't maximize without moving/resizing */
1759     if (!((self->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
1760           (self->functions & OB_CLIENT_FUNC_MOVE) &&
1761           (self->functions & OB_CLIENT_FUNC_RESIZE))) {
1762         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1763         self->decorations &= ~OB_FRAME_DECOR_MAXIMIZE;
1764     }
1765
1766     if (self->max_horz && self->max_vert) {
1767         /* you can't resize fully maximized windows */
1768         self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1769         /* kill the handle on fully maxed windows */
1770         self->decorations &= ~(OB_FRAME_DECOR_HANDLE | OB_FRAME_DECOR_GRIPS);
1771     }
1772
1773     /* If there are no decorations to remove, don't allow the user to try
1774        toggle the state */
1775     if (self->decorations == 0)
1776         self->functions &= ~OB_CLIENT_FUNC_UNDECORATE;
1777
1778     /* finally, the user can have requested no decorations, which overrides
1779        everything (but doesnt give it a border if it doesnt have one) */
1780     if (self->undecorated)
1781         self->decorations &= (config_theme_keepborder ?
1782                               OB_FRAME_DECOR_BORDER : 0);
1783
1784     /* if we don't have a titlebar, then we cannot shade! */
1785     if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1786         self->functions &= ~OB_CLIENT_FUNC_SHADE;
1787
1788     /* now we need to check against rules for the client's current state */
1789     if (self->fullscreen) {
1790         self->functions &= (OB_CLIENT_FUNC_CLOSE |
1791                             OB_CLIENT_FUNC_FULLSCREEN |
1792                             OB_CLIENT_FUNC_ICONIFY);
1793         self->decorations = 0;
1794     }
1795
1796     client_change_allowed_actions(self);
1797
1798     if (reconfig)
1799         /* force reconfigure to make sure decorations are updated */
1800         client_reconfigure(self, TRUE);
1801 }
1802
1803 static void client_change_allowed_actions(ObClient *self)
1804 {
1805     gulong actions[12];
1806     gint num = 0;
1807
1808     /* desktop windows are kept on all desktops */
1809     if (self->type != OB_CLIENT_TYPE_DESKTOP)
1810         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_CHANGE_DESKTOP);
1811
1812     if (self->functions & OB_CLIENT_FUNC_SHADE)
1813         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_SHADE);
1814     if (self->functions & OB_CLIENT_FUNC_CLOSE)
1815         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_CLOSE);
1816     if (self->functions & OB_CLIENT_FUNC_MOVE)
1817         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_MOVE);
1818     if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1819         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_MINIMIZE);
1820     if (self->functions & OB_CLIENT_FUNC_RESIZE)
1821         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_RESIZE);
1822     if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1823         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_FULLSCREEN);
1824     if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1825         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_MAXIMIZE_HORZ);
1826         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_MAXIMIZE_VERT);
1827     }
1828     if (self->functions & OB_CLIENT_FUNC_ABOVE)
1829         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_ABOVE);
1830     if (self->functions & OB_CLIENT_FUNC_BELOW)
1831         actions[num++] = OBT_PROP_ATOM(NET_WM_ACTION_BELOW);
1832     if (self->functions & OB_CLIENT_FUNC_UNDECORATE)
1833         actions[num++] = OBT_PROP_ATOM(OB_WM_ACTION_UNDECORATE);
1834
1835     OBT_PROP_SETA32(self->window, NET_WM_ALLOWED_ACTIONS, ATOM, actions, num);
1836
1837     /* make sure the window isn't breaking any rules now
1838
1839        don't check ICONIFY here.  just cuz a window can't iconify doesnt mean
1840        it can't be iconified with its parent
1841     */
1842
1843     if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1844         if (self->frame) client_shade(self, FALSE);
1845         else self->shaded = FALSE;
1846     }
1847     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1848         if (self->frame) client_fullscreen(self, FALSE);
1849         else self->fullscreen = FALSE;
1850     }
1851     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
1852                                                          self->max_vert)) {
1853         if (self->frame) client_maximize(self, FALSE, 0);
1854         else self->max_vert = self->max_horz = FALSE;
1855     }
1856 }
1857
1858 void client_update_wmhints(ObClient *self)
1859 {
1860     XWMHints *hints;
1861
1862     /* assume a window takes input if it doesn't specify */
1863     self->can_focus = TRUE;
1864
1865     if ((hints = XGetWMHints(obt_display, self->window)) != NULL) {
1866         gboolean ur;
1867
1868         if (hints->flags & InputHint)
1869             self->can_focus = hints->input;
1870
1871         /* only do this when first managing the window *AND* when we aren't
1872            starting up! */
1873         if (ob_state() != OB_STATE_STARTING && self->frame == NULL)
1874             if (hints->flags & StateHint)
1875                 self->iconic = hints->initial_state == IconicState;
1876
1877         ur = self->urgent;
1878         self->urgent = (hints->flags & XUrgencyHint);
1879         if (self->urgent && !ur)
1880             client_hilite(self, TRUE);
1881         else if (!self->urgent && ur && self->demands_attention)
1882             client_hilite(self, FALSE);
1883
1884         if (!(hints->flags & WindowGroupHint))
1885             hints->window_group = None;
1886
1887         /* did the group state change? */
1888         if (hints->window_group !=
1889             (self->group ? self->group->leader : None))
1890         {
1891             ObGroup *oldgroup = self->group;
1892
1893             /* remove from the old group if there was one */
1894             if (self->group) {
1895                 group_remove(self->group, self);
1896                 self->group = NULL;
1897             }
1898
1899             /* add ourself to the group if we have one */
1900             if (hints->window_group != None) {
1901                 self->group = group_add(hints->window_group, self);
1902             }
1903
1904             /* Put ourselves into the new group's transient tree, and remove
1905                ourselves from the old group's */
1906             client_update_transient_tree(self, oldgroup, self->group,
1907                                          self->transient_for_group,
1908                                          self->transient_for_group,
1909                                          client_direct_parent(self),
1910                                          client_direct_parent(self));
1911
1912             /* Lastly, being in a group, or not, can change if the window is
1913                transient for anything.
1914
1915                The logic for this is:
1916                self->transient = TRUE always if the window wants to be
1917                transient for something, even if transient_for was NULL because
1918                it wasn't in a group before.
1919
1920                If parents was NULL and oldgroup was NULL we can assume
1921                that when we add the new group, it will become transient for
1922                something.
1923
1924                If transient_for_group is TRUE, then it must have already
1925                had a group. If it is getting a new group, the above call to
1926                client_update_transient_tree has already taken care of
1927                everything ! If it is losing all group status then it will
1928                no longer be transient for anything and that needs to be
1929                updated.
1930             */
1931             if (self->transient &&
1932                 ((self->parents == NULL && oldgroup == NULL) ||
1933                  (self->transient_for_group && !self->group)))
1934                 client_update_transient_for(self);
1935         }
1936
1937         /* the WM_HINTS can contain an icon */
1938         if (hints->flags & IconPixmapHint)
1939             client_update_icons(self);
1940
1941         XFree(hints);
1942     }
1943
1944     focus_cycle_addremove(self, TRUE);
1945 }
1946
1947 void client_update_title(ObClient *self)
1948 {
1949     gchar *data = NULL;
1950     gchar *visible = NULL;
1951
1952     g_free(self->title);
1953     g_free(self->original_title);
1954
1955     /* try netwm */
1956     if (!OBT_PROP_GETS(self->window, NET_WM_NAME, utf8, &data)) {
1957         /* try old x stuff */
1958         if (!(OBT_PROP_GETS(self->window, WM_NAME, locale, &data)
1959               || OBT_PROP_GETS(self->window, WM_NAME, utf8, &data))) {
1960             if (self->transient) {
1961     /*
1962     GNOME alert windows are not given titles:
1963     http://developer.gnome.org/projects/gup/hig/draft_hig_new/windows-alert.html
1964     */
1965                 data = g_strdup("");
1966             } else
1967                 data = g_strdup(_("Unnamed Window"));
1968         }
1969     }
1970     self->original_title = g_strdup(data);
1971
1972     if (self->client_machine) {
1973         visible = g_strdup_printf("%s (%s)", data, self->client_machine);
1974         g_free(data);
1975     } else
1976         visible = data;
1977
1978     if (self->not_responding) {
1979         data = visible;
1980         if (self->kill_level > 0)
1981             visible = g_strdup_printf("%s - [%s]", data, _("Killing..."));
1982         else
1983             visible = g_strdup_printf("%s - [%s]", data, _("Not Responding"));
1984         g_free(data);
1985     }
1986
1987     OBT_PROP_SETS(self->window, NET_WM_VISIBLE_NAME, utf8, visible);
1988     self->title = visible;
1989
1990     if (self->frame)
1991         frame_adjust_title(self->frame);
1992
1993     /* update the icon title */
1994     data = NULL;
1995     g_free(self->icon_title);
1996
1997     /* try netwm */
1998     if (!OBT_PROP_GETS(self->window, NET_WM_ICON_NAME, utf8, &data))
1999         /* try old x stuff */
2000         if (!(OBT_PROP_GETS(self->window, WM_ICON_NAME, locale, &data) ||
2001               OBT_PROP_GETS(self->window, WM_ICON_NAME, utf8, &data)))
2002             data = g_strdup(self->title);
2003
2004     if (self->client_machine) {
2005         visible = g_strdup_printf("%s (%s)", data, self->client_machine);
2006         g_free(data);
2007     } else
2008         visible = data;
2009
2010     if (self->not_responding) {
2011         data = visible;
2012         if (self->kill_level > 0)
2013             visible = g_strdup_printf("%s - [%s]", data, _("Killing..."));
2014         else
2015             visible = g_strdup_printf("%s - [%s]", data, _("Not Responding"));
2016         g_free(data);
2017     }
2018
2019     OBT_PROP_SETS(self->window, NET_WM_VISIBLE_ICON_NAME, utf8, visible);
2020     self->icon_title = visible;
2021 }
2022
2023 void client_update_strut(ObClient *self)
2024 {
2025     guint num;
2026     guint32 *data;
2027     gboolean got = FALSE;
2028     StrutPartial strut;
2029
2030     if (OBT_PROP_GETA32(self->window, NET_WM_STRUT_PARTIAL, CARDINAL,
2031                         &data, &num))
2032     {
2033         if (num == 12) {
2034             got = TRUE;
2035             STRUT_PARTIAL_SET(strut,
2036                               data[0], data[2], data[1], data[3],
2037                               data[4], data[5], data[8], data[9],
2038                               data[6], data[7], data[10], data[11]);
2039         }
2040         g_free(data);
2041     }
2042
2043     if (!got &&
2044         OBT_PROP_GETA32(self->window, NET_WM_STRUT, CARDINAL, &data, &num)) {
2045         if (num == 4) {
2046             const Rect *a;
2047
2048             got = TRUE;
2049
2050             /* use the screen's width/height */
2051             a = screen_physical_area_all_monitors();
2052
2053             STRUT_PARTIAL_SET(strut,
2054                               data[0], data[2], data[1], data[3],
2055                               a->y, a->y + a->height - 1,
2056                               a->x, a->x + a->width - 1,
2057                               a->y, a->y + a->height - 1,
2058                               a->x, a->x + a->width - 1);
2059         }
2060         g_free(data);
2061     }
2062
2063     if (!got)
2064         STRUT_PARTIAL_SET(strut, 0, 0, 0, 0,
2065                           0, 0, 0, 0, 0, 0, 0, 0);
2066
2067     if (!PARTIAL_STRUT_EQUAL(strut, self->strut)) {
2068         self->strut = strut;
2069
2070         /* updating here is pointless while we're being mapped cuz we're not in
2071            the client list yet */
2072         if (self->frame)
2073             screen_update_areas();
2074     }
2075 }
2076
2077 void client_update_icons(ObClient *self)
2078 {
2079     guint num;
2080     guint32 *data;
2081     guint w, h, i, j;
2082     guint num_seen;  /* number of icons present */
2083     RrImage *img;
2084
2085     img = NULL;
2086
2087     /* grab the server, because we might be setting the window's icon and
2088        we don't want them to set it in between and we overwrite their own
2089        icon */
2090     grab_server(TRUE);
2091
2092     if (OBT_PROP_GETA32(self->window, NET_WM_ICON, CARDINAL, &data, &num)) {
2093         /* figure out how many valid icons are in here */
2094         i = 0;
2095         num_seen = 0;
2096         while (i + 2 < num) { /* +2 is to make sure there is a w and h */
2097             w = data[i++];
2098             h = data[i++];
2099             /* watch for the data being too small for the specified size,
2100                or for zero sized icons. */
2101             if (i + w*h > num || w == 0 || h == 0) break;
2102
2103             /* convert it to the right bit order for ObRender */
2104             for (j = 0; j < w*h; ++j)
2105                 data[i+j] =
2106                     (((data[i+j] >> 24) & 0xff) << RrDefaultAlphaOffset) +
2107                     (((data[i+j] >> 16) & 0xff) << RrDefaultRedOffset)   +
2108                     (((data[i+j] >>  8) & 0xff) << RrDefaultGreenOffset) +
2109                     (((data[i+j] >>  0) & 0xff) << RrDefaultBlueOffset);
2110
2111             /* is it in the cache? */
2112             img = RrImageCacheFind(ob_rr_icons, &data[i], w, h);
2113             if (img) RrImageRef(img); /* own it */
2114
2115             i += w*h;
2116             ++num_seen;
2117
2118             /* don't bother looping anymore if we already found it in the cache
2119                since we'll just use that! */
2120             if (img) break;
2121         }
2122
2123         /* if it's not in the cache yet, then add it to the cache now.
2124            we have already converted it to the correct bit order above */
2125         if (!img && num_seen > 0) {
2126             img = RrImageNew(ob_rr_icons);
2127             i = 0;
2128             for (j = 0; j < num_seen; ++j) {
2129                 w = data[i++];
2130                 h = data[i++];
2131                 RrImageAddPicture(img, &data[i], w, h);
2132                 i += w*h;
2133             }
2134         }
2135
2136         g_free(data);
2137     }
2138
2139     /* if we didn't find an image from the NET_WM_ICON stuff, then try the
2140        legacy X hints */
2141     if (!img) {
2142         XWMHints *hints;
2143
2144         if ((hints = XGetWMHints(obt_display, self->window))) {
2145             if (hints->flags & IconPixmapHint) {
2146                 gboolean xicon;
2147                 obt_display_ignore_errors(TRUE);
2148                 xicon = RrPixmapToRGBA(ob_rr_inst,
2149                                        hints->icon_pixmap,
2150                                        (hints->flags & IconMaskHint ?
2151                                         hints->icon_mask : None),
2152                                        (gint*)&w, (gint*)&h, &data);
2153                 obt_display_ignore_errors(FALSE);
2154
2155                 if (xicon) {
2156                     if (w > 0 && h > 0) {
2157                         /* is this icon in the cache yet? */
2158                         img = RrImageCacheFind(ob_rr_icons, data, w, h);
2159                         if (img) RrImageRef(img); /* own it */
2160
2161                         /* if not, then add it */
2162                         if (!img) {
2163                             img = RrImageNew(ob_rr_icons);
2164                             RrImageAddPicture(img, data, w, h);
2165                         }
2166                     }
2167
2168                     g_free(data);
2169                 }
2170             }
2171             XFree(hints);
2172         }
2173     }
2174
2175     /* set the client's icons to be whatever we found */
2176     RrImageUnref(self->icon_set);
2177     self->icon_set = img;
2178
2179     /* if the client has no icon at all, then we set a default icon onto it.
2180        but, if it has parents, then one of them will have an icon already
2181     */
2182     if (!self->icon_set && !self->parents) {
2183         RrPixel32 *icon = ob_rr_theme->def_win_icon;
2184         gulong *ldata; /* use a long here to satisfy OBT_PROP_SETA32 */
2185
2186         w = ob_rr_theme->def_win_icon_w;
2187         h = ob_rr_theme->def_win_icon_h;
2188         ldata = g_new(gulong, w*h+2);
2189         ldata[0] = w;
2190         ldata[1] = h;
2191         for (i = 0; i < w*h; ++i)
2192             ldata[i+2] = (((icon[i] >> RrDefaultAlphaOffset) & 0xff) << 24) +
2193                 (((icon[i] >> RrDefaultRedOffset) & 0xff) << 16) +
2194                 (((icon[i] >> RrDefaultGreenOffset) & 0xff) << 8) +
2195                 (((icon[i] >> RrDefaultBlueOffset) & 0xff) << 0);
2196         OBT_PROP_SETA32(self->window, NET_WM_ICON, CARDINAL, ldata, w*h+2);
2197         g_free(ldata);
2198     } else if (self->frame)
2199         /* don't draw the icon empty if we're just setting one now anyways,
2200            we'll get the property change any second */
2201         frame_adjust_icon(self->frame);
2202
2203     grab_server(FALSE);
2204 }
2205
2206 void client_update_icon_geometry(ObClient *self)
2207 {
2208     guint num;
2209     guint32 *data;
2210
2211     RECT_SET(self->icon_geometry, 0, 0, 0, 0);
2212
2213     if (OBT_PROP_GETA32(self->window, NET_WM_ICON_GEOMETRY, CARDINAL,
2214                         &data, &num))
2215     {
2216         if (num == 4)
2217             /* don't let them set it with an area < 0 */
2218             RECT_SET(self->icon_geometry, data[0], data[1],
2219                      MAX(data[2],0), MAX(data[3],0));
2220         g_free(data);
2221     }
2222 }
2223
2224 static void client_get_session_ids(ObClient *self)
2225 {
2226     guint32 leader;
2227     gboolean got;
2228     gchar *s;
2229     gchar **ss;
2230
2231     if (!OBT_PROP_GET32(self->window, WM_CLIENT_LEADER, WINDOW, &leader))
2232         leader = None;
2233
2234     /* get the SM_CLIENT_ID */
2235     got = FALSE;
2236     if (leader)
2237         got = OBT_PROP_GETS(leader, SM_CLIENT_ID, locale, &self->sm_client_id);
2238     if (!got)
2239         OBT_PROP_GETS(self->window, SM_CLIENT_ID, locale, &self->sm_client_id);
2240
2241     /* get the WM_CLASS (name and class). make them "" if they are not
2242        provided */
2243     got = FALSE;
2244     if (leader)
2245         got = OBT_PROP_GETSS(leader, WM_CLASS, locale, &ss);
2246     if (!got)
2247         got = OBT_PROP_GETSS(self->window, WM_CLASS, locale, &ss);
2248
2249     if (got) {
2250         if (ss[0]) {
2251             self->name = g_strdup(ss[0]);
2252             if (ss[1])
2253                 self->class = g_strdup(ss[1]);
2254         }
2255         g_strfreev(ss);
2256     }
2257
2258     if (self->name == NULL) self->name = g_strdup("");
2259     if (self->class == NULL) self->class = g_strdup("");
2260
2261     /* get the WM_WINDOW_ROLE. make it "" if it is not provided */
2262     got = FALSE;
2263     if (leader)
2264         got = OBT_PROP_GETS(leader, WM_WINDOW_ROLE, locale, &s);
2265     if (!got)
2266         got = OBT_PROP_GETS(self->window, WM_WINDOW_ROLE, locale, &s);
2267
2268     if (got)
2269         self->role = s;
2270     else
2271         self->role = g_strdup("");
2272
2273     /* get the WM_COMMAND */
2274     got = FALSE;
2275
2276     if (leader)
2277         got = OBT_PROP_GETSS(leader, WM_COMMAND, locale, &ss);
2278     if (!got)
2279         got = OBT_PROP_GETSS(self->window, WM_COMMAND, locale, &ss);
2280
2281     if (got) {
2282         /* merge/mash them all together */
2283         gchar *merge = NULL;
2284         gint i;
2285
2286         for (i = 0; ss[i]; ++i) {
2287             gchar *tmp = merge;
2288             if (merge)
2289                 merge = g_strconcat(merge, ss[i], NULL);
2290             else
2291                 merge = g_strconcat(ss[i], NULL);
2292             g_free(tmp);
2293         }
2294         g_strfreev(ss);
2295
2296         self->wm_command = merge;
2297     }
2298
2299     /* get the WM_CLIENT_MACHINE */
2300     got = FALSE;
2301     if (leader)
2302         got = OBT_PROP_GETS(leader, WM_CLIENT_MACHINE, locale, &s);
2303     if (!got)
2304         got = OBT_PROP_GETS(self->window, WM_CLIENT_MACHINE, locale, &s);
2305
2306     if (got) {
2307         gchar localhost[128];
2308         guint32 pid;
2309
2310         gethostname(localhost, 127);
2311         localhost[127] = '\0';
2312         if (strcmp(localhost, s) != 0)
2313             self->client_machine = s;
2314         else
2315             g_free(s);
2316
2317         /* see if it has the PID set too (the PID requires that the
2318            WM_CLIENT_MACHINE be set) */
2319         if (OBT_PROP_GET32(self->window, NET_WM_PID, CARDINAL, &pid))
2320             self->pid = pid;
2321     }
2322 }
2323
2324 /*! Save the properties used for app matching rules, as seen by Openbox when
2325   the window mapped, so that users can still access them later if the app
2326   changes them */
2327 static void client_save_app_rule_values(ObClient *self)
2328 {
2329     const gchar *type;
2330
2331     OBT_PROP_SETS(self->window, OB_APP_ROLE, utf8, self->role);
2332     OBT_PROP_SETS(self->window, OB_APP_NAME, utf8, self->name);
2333     OBT_PROP_SETS(self->window, OB_APP_CLASS, utf8, self->class);
2334     OBT_PROP_SETS(self->window, OB_APP_TITLE, utf8, self->original_title);
2335
2336     switch (self->type) {
2337     case OB_CLIENT_TYPE_NORMAL:
2338         type = "normal"; break;
2339     case OB_CLIENT_TYPE_DIALOG:
2340         type = "dialog"; break;
2341     case OB_CLIENT_TYPE_UTILITY:
2342         type = "utility"; break;
2343     case OB_CLIENT_TYPE_MENU:
2344         type = "menu"; break;
2345     case OB_CLIENT_TYPE_TOOLBAR:
2346         type = "toolbar"; break;
2347     case OB_CLIENT_TYPE_SPLASH:
2348         type = "splash"; break;
2349     case OB_CLIENT_TYPE_DESKTOP:
2350         type = "desktop"; break;
2351     case OB_CLIENT_TYPE_DOCK:
2352         type = "dock"; break;
2353     }
2354     OBT_PROP_SETS(self->window, OB_APP_TYPE, utf8, type);
2355 }
2356
2357 static void client_change_wm_state(ObClient *self)
2358 {
2359     gulong state[2];
2360     glong old;
2361
2362     old = self->wmstate;
2363
2364     if (self->shaded || self->iconic ||
2365         (self->desktop != DESKTOP_ALL && self->desktop != screen_desktop))
2366     {
2367         self->wmstate = IconicState;
2368     } else
2369         self->wmstate = NormalState;
2370
2371     if (old != self->wmstate) {
2372         OBT_PROP_MSG(ob_screen, self->window, KDE_WM_CHANGE_STATE,
2373                      self->wmstate, 1, 0, 0, 0);
2374
2375         state[0] = self->wmstate;
2376         state[1] = None;
2377         OBT_PROP_SETA32(self->window, WM_STATE, WM_STATE, state, 2);
2378     }
2379 }
2380
2381 static void client_change_state(ObClient *self)
2382 {
2383     gulong netstate[12];
2384     guint num;
2385
2386     num = 0;
2387     if (self->modal)
2388         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_MODAL);
2389     if (self->shaded)
2390         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_SHADED);
2391     if (self->iconic)
2392         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_HIDDEN);
2393     if (self->skip_taskbar)
2394         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_SKIP_TASKBAR);
2395     if (self->skip_pager)
2396         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_SKIP_PAGER);
2397     if (self->fullscreen)
2398         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_FULLSCREEN);
2399     if (self->max_vert)
2400         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_VERT);
2401     if (self->max_horz)
2402         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_HORZ);
2403     if (self->above)
2404         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_ABOVE);
2405     if (self->below)
2406         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_BELOW);
2407     if (self->demands_attention)
2408         netstate[num++] = OBT_PROP_ATOM(NET_WM_STATE_DEMANDS_ATTENTION);
2409     if (self->undecorated)
2410         netstate[num++] = OBT_PROP_ATOM(OB_WM_STATE_UNDECORATED);
2411     OBT_PROP_SETA32(self->window, NET_WM_STATE, ATOM, netstate, num);
2412
2413     if (self->frame)
2414         frame_adjust_state(self->frame);
2415 }
2416
2417 ObClient *client_search_focus_tree(ObClient *self)
2418 {
2419     GSList *it;
2420     ObClient *ret;
2421
2422     for (it = self->transients; it; it = g_slist_next(it)) {
2423         if (client_focused(it->data)) return it->data;
2424         if ((ret = client_search_focus_tree(it->data))) return ret;
2425     }
2426     return NULL;
2427 }
2428
2429 ObClient *client_search_focus_tree_full(ObClient *self)
2430 {
2431     if (self->parents) {
2432         GSList *it;
2433
2434         for (it = self->parents; it; it = g_slist_next(it)) {
2435             ObClient *c = it->data;
2436             if ((c = client_search_focus_tree_full(c))) return c;
2437         }
2438
2439         return NULL;
2440     }
2441     else {
2442         /* this function checks the whole tree, the client_search_focus_tree
2443            does not, so we need to check this window */
2444         if (client_focused(self))
2445             return self;
2446         return client_search_focus_tree(self);
2447     }
2448 }
2449
2450 ObClient *client_search_focus_group_full(ObClient *self)
2451 {
2452     GSList *it;
2453
2454     if (self->group) {
2455         for (it = self->group->members; it; it = g_slist_next(it)) {
2456             ObClient *c = it->data;
2457
2458             if (client_focused(c)) return c;
2459             if ((c = client_search_focus_tree(it->data))) return c;
2460         }
2461     } else
2462         if (client_focused(self)) return self;
2463     return NULL;
2464 }
2465
2466 gboolean client_has_parent(ObClient *self)
2467 {
2468     return self->parents != NULL;
2469 }
2470
2471 gboolean client_is_oldfullscreen(const ObClient *self,
2472                                  const Rect *area)
2473 {
2474     const Rect *monitor, *allmonitors;
2475
2476     /* No decorations and fills the monitor = oldskool fullscreen.
2477        But not for maximized windows.
2478     */
2479
2480     if (self->decorations || self->max_horz || self->max_vert) return FALSE;
2481
2482     monitor = screen_physical_area_monitor(screen_find_monitor(area));
2483     allmonitors = screen_physical_area_all_monitors();
2484
2485     return (RECT_EQUAL(*area, *monitor) ||
2486             RECT_EQUAL(*area, *allmonitors));
2487 }
2488
2489 static ObStackingLayer calc_layer(ObClient *self)
2490 {
2491     ObStackingLayer l;
2492     const Rect *monitor, *allmonitors;
2493
2494     monitor = screen_physical_area_monitor(client_monitor(self));
2495     allmonitors = screen_physical_area_all_monitors();
2496
2497     if (self->type == OB_CLIENT_TYPE_DESKTOP)
2498         l = OB_STACKING_LAYER_DESKTOP;
2499     else if (self->type == OB_CLIENT_TYPE_DOCK) {
2500         if (self->below) l = OB_STACKING_LAYER_NORMAL;
2501         else l = OB_STACKING_LAYER_ABOVE;
2502     }
2503     else if ((self->fullscreen ||
2504               client_is_oldfullscreen(self, &self->area)) &&
2505              /* you are fullscreen while you or your children are focused.. */
2506              (client_focused(self) || client_search_focus_tree(self) ||
2507               /* you can be fullscreen if you're on another desktop */
2508               (self->desktop != screen_desktop &&
2509                self->desktop != DESKTOP_ALL) ||
2510               /* and you can also be fullscreen if the focused client is on
2511                  another monitor, or nothing else is focused */
2512               (!focus_client ||
2513                client_monitor(focus_client) != client_monitor(self))))
2514         l = OB_STACKING_LAYER_FULLSCREEN;
2515     else if (self->above) l = OB_STACKING_LAYER_ABOVE;
2516     else if (self->below) l = OB_STACKING_LAYER_BELOW;
2517     else l = OB_STACKING_LAYER_NORMAL;
2518
2519     return l;
2520 }
2521
2522 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
2523                                         ObStackingLayer min)
2524 {
2525     ObStackingLayer old, own;
2526     GSList *it;
2527
2528     old = self->layer;
2529     own = calc_layer(self);
2530     self->layer = MAX(own, min);
2531
2532     if (self->layer != old) {
2533         stacking_remove(CLIENT_AS_WINDOW(self));
2534         stacking_add_nonintrusive(CLIENT_AS_WINDOW(self));
2535     }
2536
2537     /* we've been restacked */
2538     self->visited = TRUE;
2539
2540     for (it = self->transients; it; it = g_slist_next(it))
2541         client_calc_layer_recursive(it->data, orig,
2542                                     self->layer);
2543 }
2544
2545 static void client_calc_layer_internal(ObClient *self)
2546 {
2547     GSList *sit;
2548
2549     /* transients take on the layer of their parents */
2550     sit = client_search_all_top_parents(self);
2551
2552     for (; sit; sit = g_slist_next(sit))
2553         client_calc_layer_recursive(sit->data, self, 0);
2554 }
2555
2556 void client_calc_layer(ObClient *self)
2557 {
2558     GList *it;
2559
2560     /* skip over stuff above fullscreen layer */
2561     for (it = stacking_list; it; it = g_list_next(it))
2562         if (window_layer(it->data) <= OB_STACKING_LAYER_FULLSCREEN) break;
2563
2564     /* find the windows in the fullscreen layer, and mark them not-visited */
2565     for (; it; it = g_list_next(it)) {
2566         if (window_layer(it->data) < OB_STACKING_LAYER_FULLSCREEN) break;
2567         else if (WINDOW_IS_CLIENT(it->data))
2568             WINDOW_AS_CLIENT(it->data)->visited = FALSE;
2569     }
2570
2571     client_calc_layer_internal(self);
2572
2573     /* skip over stuff above fullscreen layer */
2574     for (it = stacking_list; it; it = g_list_next(it))
2575         if (window_layer(it->data) <= OB_STACKING_LAYER_FULLSCREEN) break;
2576
2577     /* now recalc any windows in the fullscreen layer which have not
2578        had their layer recalced already */
2579     for (; it; it = g_list_next(it)) {
2580         if (window_layer(it->data) < OB_STACKING_LAYER_FULLSCREEN) break;
2581         else if (WINDOW_IS_CLIENT(it->data) &&
2582                  !WINDOW_AS_CLIENT(it->data)->visited)
2583             client_calc_layer_internal(it->data);
2584     }
2585 }
2586
2587 gboolean client_should_show(ObClient *self)
2588 {
2589     if (self->iconic)
2590         return FALSE;
2591     if (client_normal(self) && screen_showing_desktop)
2592         return FALSE;
2593     if (self->desktop == screen_desktop || self->desktop == DESKTOP_ALL)
2594         return TRUE;
2595
2596     return FALSE;
2597 }
2598
2599 gboolean client_show(ObClient *self)
2600 {
2601     gboolean show = FALSE;
2602
2603     if (client_should_show(self)) {
2604         /* replay pending pointer event before showing the window, in case it
2605            should be going to something under the window */
2606         mouse_replay_pointer();
2607
2608         frame_show(self->frame);
2609         show = TRUE;
2610
2611         /* According to the ICCCM (sec 4.1.3.1) when a window is not visible,
2612            it needs to be in IconicState. This includes when it is on another
2613            desktop!
2614         */
2615         client_change_wm_state(self);
2616     }
2617     return show;
2618 }
2619
2620 gboolean client_hide(ObClient *self)
2621 {
2622     gboolean hide = FALSE;
2623
2624     if (!client_should_show(self)) {
2625         /* We don't need to ignore enter events here.
2626            The window can hide/iconify in 3 different ways:
2627            1 - through an x message. in this case we ignore all enter events
2628                caused by responding to the x message (unless underMouse)
2629            2 - by a keyboard action. in this case we ignore all enter events
2630                caused by the action
2631            3 - by a mouse action. in this case they are doing stuff with the
2632                mouse and focus _should_ move.
2633
2634            Also in action_end, we simulate an enter event that can't be ignored
2635            so trying to ignore them is futile in case 3 anyways
2636         */
2637
2638         /* replay pending pointer event before hiding the window, in case it
2639            should be going to the window */
2640         mouse_replay_pointer();
2641
2642         frame_hide(self->frame);
2643         hide = TRUE;
2644
2645         /* According to the ICCCM (sec 4.1.3.1) when a window is not visible,
2646            it needs to be in IconicState. This includes when it is on another
2647            desktop!
2648         */
2649         client_change_wm_state(self);
2650     }
2651     return hide;
2652 }
2653
2654 void client_showhide(ObClient *self)
2655 {
2656     if (!client_show(self))
2657         client_hide(self);
2658 }
2659
2660 gboolean client_normal(ObClient *self) {
2661     return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
2662               self->type == OB_CLIENT_TYPE_DOCK ||
2663               self->type == OB_CLIENT_TYPE_SPLASH);
2664 }
2665
2666 gboolean client_helper(ObClient *self)
2667 {
2668     return (self->type == OB_CLIENT_TYPE_UTILITY ||
2669             self->type == OB_CLIENT_TYPE_MENU ||
2670             self->type == OB_CLIENT_TYPE_TOOLBAR);
2671 }
2672
2673 gboolean client_mouse_focusable(ObClient *self)
2674 {
2675     return !(self->type == OB_CLIENT_TYPE_MENU ||
2676              self->type == OB_CLIENT_TYPE_TOOLBAR ||
2677              self->type == OB_CLIENT_TYPE_SPLASH ||
2678              self->type == OB_CLIENT_TYPE_DOCK);
2679 }
2680
2681 gboolean client_enter_focusable(ObClient *self)
2682 {
2683     /* you can focus desktops but it shouldn't on enter */
2684     return (client_mouse_focusable(self) &&
2685             self->type != OB_CLIENT_TYPE_DESKTOP);
2686 }
2687
2688 static void client_apply_startup_state(ObClient *self,
2689                                        gint x, gint y, gint w, gint h)
2690 {
2691     /* save the states that we are going to apply */
2692     gboolean iconic = self->iconic;
2693     gboolean fullscreen = self->fullscreen;
2694     gboolean undecorated = self->undecorated;
2695     gboolean shaded = self->shaded;
2696     gboolean demands_attention = self->demands_attention;
2697     gboolean max_horz = self->max_horz;
2698     gboolean max_vert = self->max_vert;
2699     Rect oldarea;
2700     gint l;
2701
2702     /* turn them all off in the client, so they won't affect the window
2703        being placed */
2704     self->iconic = self->fullscreen = self->undecorated = self->shaded =
2705         self->demands_attention = self->max_horz = self->max_vert = FALSE;
2706
2707     /* move the client to its placed position, or it it's already there,
2708        generate a ConfigureNotify telling the client where it is.
2709
2710        do this after adjusting the frame. otherwise it gets all weird and
2711        clients don't work right
2712
2713        do this before applying the states so they have the correct
2714        pre-max/pre-fullscreen values
2715     */
2716     client_try_configure(self, &x, &y, &w, &h, &l, &l, FALSE);
2717     ob_debug("placed window 0x%x at %d, %d with size %d x %d",
2718              self->window, x, y, w, h);
2719     /* save the area, and make it where it should be for the premax stuff */
2720     oldarea = self->area;
2721     RECT_SET(self->area, x, y, w, h);
2722
2723     /* apply the states. these are in a carefully crafted order.. */
2724
2725     if (iconic)
2726         client_iconify(self, TRUE, FALSE, TRUE);
2727     if (fullscreen)
2728         client_fullscreen(self, TRUE);
2729     if (undecorated)
2730         client_set_undecorated(self, TRUE);
2731     if (shaded)
2732         client_shade(self, TRUE);
2733     if (demands_attention)
2734         client_hilite(self, TRUE);
2735
2736     if (max_vert && max_horz)
2737         client_maximize(self, TRUE, 0);
2738     else if (max_vert)
2739         client_maximize(self, TRUE, 2);
2740     else if (max_horz)
2741         client_maximize(self, TRUE, 1);
2742
2743     /* if the window hasn't been configured yet, then do so now, in fact the
2744        x,y,w,h may _not_ be the same as the area rect, which can end up
2745        meaning that the client isn't properly moved/resized by the fullscreen
2746        function
2747        pho can cause this because it maps at size of the screen but not 0,0
2748        so openbox moves it on screen to 0,0 (thus x,y=0,0 and area.x,y don't).
2749        then fullscreen'ing makes it go to 0,0 which it thinks it already is at
2750        cuz thats where the pre-fullscreen will be. however the actual area is
2751        not, so this needs to be called even if we have fullscreened/maxed
2752     */
2753     self->area = oldarea;
2754     client_configure(self, x, y, w, h, FALSE, TRUE, FALSE);
2755
2756     /* set the desktop hint, to make sure that it always exists */
2757     OBT_PROP_SET32(self->window, NET_WM_DESKTOP, CARDINAL, self->desktop);
2758
2759     /* nothing to do for the other states:
2760        skip_taskbar
2761        skip_pager
2762        modal
2763        above
2764        below
2765     */
2766 }
2767
2768 void client_gravity_resize_w(ObClient *self, gint *x, gint oldw, gint neww)
2769 {
2770     /* these should be the current values. this is for when you're not moving,
2771        just resizing */
2772     g_assert(*x == self->area.x);
2773     g_assert(oldw == self->area.width);
2774
2775     /* horizontal */
2776     switch (self->gravity) {
2777     default:
2778     case NorthWestGravity:
2779     case WestGravity:
2780     case SouthWestGravity:
2781     case StaticGravity:
2782     case ForgetGravity:
2783         break;
2784     case NorthGravity:
2785     case CenterGravity:
2786     case SouthGravity:
2787         *x -= (neww - oldw) / 2;
2788         break;
2789     case NorthEastGravity:
2790     case EastGravity:
2791     case SouthEastGravity:
2792         *x -= neww - oldw;
2793         break;
2794     }
2795 }
2796
2797 void client_gravity_resize_h(ObClient *self, gint *y, gint oldh, gint newh)
2798 {
2799     /* these should be the current values. this is for when you're not moving,
2800        just resizing */
2801     g_assert(*y == self->area.y);
2802     g_assert(oldh == self->area.height);
2803
2804     /* vertical */
2805     switch (self->gravity) {
2806     default:
2807     case NorthWestGravity:
2808     case NorthGravity:
2809     case NorthEastGravity:
2810     case StaticGravity:
2811     case ForgetGravity:
2812         break;
2813     case WestGravity:
2814     case CenterGravity:
2815     case EastGravity:
2816         *y -= (newh - oldh) / 2;
2817         break;
2818     case SouthWestGravity:
2819     case SouthGravity:
2820     case SouthEastGravity:
2821         *y -= newh - oldh;
2822         break;
2823     }
2824 }
2825
2826 void client_try_configure(ObClient *self, gint *x, gint *y, gint *w, gint *h,
2827                           gint *logicalw, gint *logicalh,
2828                           gboolean user)
2829 {
2830     Rect desired = {*x, *y, *w, *h};
2831     frame_rect_to_frame(self->frame, &desired);
2832
2833     /* make the frame recalculate its dimensions n shit without changing
2834        anything visible for real, this way the constraints below can work with
2835        the updated frame dimensions. */
2836     frame_adjust_area(self->frame, FALSE, TRUE, TRUE);
2837
2838     /* gets the frame's position */
2839     frame_client_gravity(self->frame, x, y);
2840
2841     /* these positions are frame positions, not client positions */
2842
2843     /* set the size and position if fullscreen */
2844     if (self->fullscreen) {
2845         const Rect *a;
2846         guint i;
2847
2848         i = screen_find_monitor(&desired);
2849         a = screen_physical_area_monitor(i);
2850
2851         *x = a->x;
2852         *y = a->y;
2853         *w = a->width;
2854         *h = a->height;
2855
2856         user = FALSE; /* ignore if the client can't be moved/resized when it
2857                          is fullscreening */
2858     } else if (self->max_horz || self->max_vert) {
2859         Rect *a;
2860         guint i;
2861
2862         /* use all possible struts when maximizing to the full screen */
2863         i = screen_find_monitor(&desired);
2864         a = screen_area(self->desktop, i,
2865                         (self->max_horz && self->max_vert ? NULL : &desired));
2866
2867         /* set the size and position if maximized */
2868         if (self->max_horz) {
2869             *x = a->x;
2870             *w = a->width - self->frame->size.left - self->frame->size.right;
2871         }
2872         if (self->max_vert) {
2873             *y = a->y;
2874             *h = a->height - self->frame->size.top - self->frame->size.bottom;
2875         }
2876
2877         user = FALSE; /* ignore if the client can't be moved/resized when it
2878                          is maximizing */
2879
2880         g_slice_free(Rect, a);
2881     }
2882
2883     /* gets the client's position */
2884     frame_frame_gravity(self->frame, x, y);
2885
2886     /* work within the preferred sizes given by the window, these may have
2887        changed rather than it's requested width and height, so always run
2888        through this code */
2889     {
2890         gint basew, baseh, minw, minh;
2891         gint incw, inch;
2892         gfloat minratio, maxratio;
2893
2894         incw = self->fullscreen || self->max_horz ? 1 : self->size_inc.width;
2895         inch = self->fullscreen || self->max_vert ? 1 : self->size_inc.height;
2896         minratio = self->fullscreen || (self->max_horz && self->max_vert) ?
2897             0 : self->min_ratio;
2898         maxratio = self->fullscreen || (self->max_horz && self->max_vert) ?
2899             0 : self->max_ratio;
2900
2901         /* base size is substituted with min size if not specified */
2902         if (self->base_size.width >= 0 || self->base_size.height >= 0) {
2903             basew = self->base_size.width;
2904             baseh = self->base_size.height;
2905         } else {
2906             basew = self->min_size.width;
2907             baseh = self->min_size.height;
2908         }
2909         /* min size is substituted with base size if not specified */
2910         if (self->min_size.width || self->min_size.height) {
2911             minw = self->min_size.width;
2912             minh = self->min_size.height;
2913         } else {
2914             minw = self->base_size.width;
2915             minh = self->base_size.height;
2916         }
2917
2918         /* This comment is no longer true */
2919         /* if this is a user-requested resize, then check against min/max
2920            sizes */
2921
2922         /* smaller than min size or bigger than max size? */
2923         if (*w > self->max_size.width) *w = self->max_size.width;
2924         if (*w < minw) *w = minw;
2925         if (*h > self->max_size.height) *h = self->max_size.height;
2926         if (*h < minh) *h = minh;
2927
2928         *w -= basew;
2929         *h -= baseh;
2930
2931         /* keep to the increments */
2932         *w /= incw;
2933         *h /= inch;
2934
2935         /* you cannot resize to nothing */
2936         if (basew + *w < 1) *w = 1 - basew;
2937         if (baseh + *h < 1) *h = 1 - baseh;
2938
2939         /* save the logical size */
2940         *logicalw = incw > 1 ? *w : *w + basew;
2941         *logicalh = inch > 1 ? *h : *h + baseh;
2942
2943         *w *= incw;
2944         *h *= inch;
2945
2946         *w += basew;
2947         *h += baseh;
2948
2949         /* adjust the height to match the width for the aspect ratios.
2950            for this, min size is not substituted for base size ever. */
2951         *w -= self->base_size.width;
2952         *h -= self->base_size.height;
2953
2954         if (minratio)
2955             if (*h * minratio > *w) {
2956                 *h = (gint)(*w / minratio);
2957
2958                 /* you cannot resize to nothing */
2959                 if (*h < 1) {
2960                     *h = 1;
2961                     *w = (gint)(*h * minratio);
2962                 }
2963             }
2964         if (maxratio)
2965             if (*h * maxratio < *w) {
2966                 *h = (gint)(*w / maxratio);
2967
2968                 /* you cannot resize to nothing */
2969                 if (*h < 1) {
2970                     *h = 1;
2971                     *w = (gint)(*h * minratio);
2972                 }
2973             }
2974
2975         *w += self->base_size.width;
2976         *h += self->base_size.height;
2977     }
2978
2979     /* these override the above states! if you cant move you can't move! */
2980     if (user) {
2981         if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
2982             *x = self->area.x;
2983             *y = self->area.y;
2984         }
2985         if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
2986             *w = self->area.width;
2987             *h = self->area.height;
2988         }
2989     }
2990
2991     g_assert(*w > 0);
2992     g_assert(*h > 0);
2993 }
2994
2995 void client_configure(ObClient *self, gint x, gint y, gint w, gint h,
2996                       gboolean user, gboolean final, gboolean force_reply)
2997 {
2998     Rect oldframe, oldclient;
2999     gboolean send_resize_client;
3000     gboolean moved = FALSE, resized = FALSE, rootmoved = FALSE;
3001     gboolean fmoved, fresized;
3002     guint fdecor = self->frame->decorations;
3003     gboolean fhorz = self->frame->max_horz;
3004     gboolean fvert = self->frame->max_vert;
3005     gint logicalw, logicalh;
3006
3007     /* find the new x, y, width, and height (and logical size) */
3008     client_try_configure(self, &x, &y, &w, &h, &logicalw, &logicalh, user);
3009
3010     /* set the logical size if things changed */
3011     if (!(w == self->area.width && h == self->area.height))
3012         SIZE_SET(self->logical_size, logicalw, logicalh);
3013
3014     /* figure out if we moved or resized or what */
3015     moved = (x != self->area.x || y != self->area.y);
3016     resized = (w != self->area.width || h != self->area.height);
3017
3018     oldframe = self->frame->area;
3019     oldclient = self->area;
3020     RECT_SET(self->area, x, y, w, h);
3021
3022     /* for app-requested resizes, always resize if 'resized' is true.
3023        for user-requested ones, only resize if final is true, or when
3024        resizing in redraw mode */
3025     send_resize_client = ((!user && resized) ||
3026                           (user && (final ||
3027                                     (resized && config_resize_redraw))));
3028
3029     /* if the client is enlarging, then resize the client before the frame */
3030     if (send_resize_client && (w > oldclient.width || h > oldclient.height)) {
3031         XMoveResizeWindow(obt_display, self->window,
3032                           self->frame->size.left, self->frame->size.top,
3033                           MAX(w, oldclient.width), MAX(h, oldclient.height));
3034         frame_adjust_client_area(self->frame);
3035     }
3036
3037     /* find the frame's dimensions and move/resize it */
3038     fmoved = moved;
3039     fresized = resized;
3040
3041     /* if decorations changed, then readjust everything for the frame */
3042     if (self->decorations != fdecor ||
3043         self->max_horz != fhorz || self->max_vert != fvert)
3044     {
3045         fmoved = fresized = TRUE;
3046     }
3047
3048     /* adjust the frame */
3049     if (fmoved || fresized) {
3050         gulong ignore_start;
3051         if (!user)
3052             ignore_start = event_start_ignore_all_enters();
3053
3054         /* replay pending pointer event before move the window, in case it
3055            would change what window gets the event */
3056         mouse_replay_pointer();
3057
3058         frame_adjust_area(self->frame, fmoved, fresized, FALSE);
3059
3060         if (!user)
3061             event_end_ignore_all_enters(ignore_start);
3062     }
3063
3064     if (!user || final) {
3065         gint oldrx = self->root_pos.x;
3066         gint oldry = self->root_pos.y;
3067         /* we have reset the client to 0 border width, so don't include
3068            it in these coords */
3069         POINT_SET(self->root_pos,
3070                   self->frame->area.x + self->frame->size.left -
3071                   self->border_width,
3072                   self->frame->area.y + self->frame->size.top -
3073                   self->border_width);
3074         if (self->root_pos.x != oldrx || self->root_pos.y != oldry)
3075             rootmoved = TRUE;
3076     }
3077
3078     /* This is kinda tricky and should not be changed.. let me explain!
3079
3080        When user = FALSE, then the request is coming from the application
3081        itself, and we are more strict about when to send a synthetic
3082        ConfigureNotify.  We strictly follow the rules of the ICCCM sec 4.1.5
3083        in this case (if force_reply is true)
3084
3085        When user = TRUE, then the request is coming from "us", like when we
3086        maximize a window or something.  In this case we are more lenient.  We
3087        used to follow the same rules as above, but _Java_ Swing can't handle
3088        this. So just to appease Swing, when user = TRUE, we always send
3089        a synthetic ConfigureNotify to give the window its root coordinates.
3090     */
3091     if ((!user && !resized && (rootmoved || force_reply)) ||
3092         (user && final && rootmoved))
3093     {
3094         XEvent event;
3095
3096         event.type = ConfigureNotify;
3097         event.xconfigure.display = obt_display;
3098         event.xconfigure.event = self->window;
3099         event.xconfigure.window = self->window;
3100
3101         ob_debug("Sending ConfigureNotify to %s for %d,%d %dx%d",
3102                  self->title, self->root_pos.x, self->root_pos.y, w, h);
3103
3104         /* root window real coords */
3105         event.xconfigure.x = self->root_pos.x;
3106         event.xconfigure.y = self->root_pos.y;
3107         event.xconfigure.width = w;
3108         event.xconfigure.height = h;
3109         event.xconfigure.border_width = self->border_width;
3110         event.xconfigure.above = None;
3111         event.xconfigure.override_redirect = FALSE;
3112         XSendEvent(event.xconfigure.display, event.xconfigure.window,
3113                    FALSE, StructureNotifyMask, &event);
3114     }
3115
3116     /* if the client is shrinking, then resize the frame before the client.
3117
3118        both of these resize sections may run, because the top one only resizes
3119        in the direction that is growing
3120      */
3121     if (send_resize_client && (w <= oldclient.width || h <= oldclient.height))
3122     {
3123         frame_adjust_client_area(self->frame);
3124         XMoveResizeWindow(obt_display, self->window,
3125                           self->frame->size.left, self->frame->size.top, w, h);
3126     }
3127
3128     XFlush(obt_display);
3129
3130     /* if it moved between monitors, then this can affect the stacking
3131        layer of this window or others - for fullscreen windows.
3132        also if it changed to/from oldschool fullscreen then its layer may
3133        change */
3134     if (screen_find_monitor(&self->frame->area) !=
3135         screen_find_monitor(&oldframe) ||
3136         (final && (client_is_oldfullscreen(self, &oldclient) !=
3137                    client_is_oldfullscreen(self, &self->area))))
3138     {
3139         client_calc_layer(self);
3140     }
3141 }
3142
3143 void client_fullscreen(ObClient *self, gboolean fs)
3144 {
3145     gint x, y, w, h;
3146
3147     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
3148         self->fullscreen == fs) return;                   /* already done */
3149
3150     self->fullscreen = fs;
3151     client_change_state(self); /* change the state hints on the client */
3152
3153     if (fs) {
3154         self->pre_fullscreen_area = self->area;
3155         self->pre_fullscreen_max_horz = self->max_horz;
3156         self->pre_fullscreen_max_vert = self->max_vert;
3157
3158         /* if the window is maximized, its area isn't all that meaningful.
3159            save its premax area instead. */
3160         if (self->max_horz) {
3161             self->pre_fullscreen_area.x = self->pre_max_area.x;
3162             self->pre_fullscreen_area.width = self->pre_max_area.width;
3163         }
3164         if (self->max_vert) {
3165             self->pre_fullscreen_area.y = self->pre_max_area.y;
3166             self->pre_fullscreen_area.height = self->pre_max_area.height;
3167         }
3168
3169         /* these will help configure_full figure out where to fullscreen
3170            the window */
3171         x = self->area.x;
3172         y = self->area.y;
3173         w = self->area.width;
3174         h = self->area.height;
3175     } else {
3176         g_assert(self->pre_fullscreen_area.width > 0 &&
3177                  self->pre_fullscreen_area.height > 0);
3178
3179         self->max_horz = self->pre_fullscreen_max_horz;
3180         self->max_vert = self->pre_fullscreen_max_vert;
3181         if (self->max_horz) {
3182             self->pre_max_area.x = self->pre_fullscreen_area.x;
3183             self->pre_max_area.width = self->pre_fullscreen_area.width;
3184         }
3185         if (self->max_vert) {
3186             self->pre_max_area.y = self->pre_fullscreen_area.y;
3187             self->pre_max_area.height = self->pre_fullscreen_area.height;
3188         }
3189
3190         x = self->pre_fullscreen_area.x;
3191         y = self->pre_fullscreen_area.y;
3192         w = self->pre_fullscreen_area.width;
3193         h = self->pre_fullscreen_area.height;
3194         RECT_SET(self->pre_fullscreen_area, 0, 0, 0, 0);
3195     }
3196
3197     ob_debug("Window %s going fullscreen (%d)",
3198              self->title, self->fullscreen);
3199
3200     client_setup_decor_and_functions(self, FALSE);
3201     client_move_resize(self, x, y, w, h);
3202
3203     /* and adjust our layer/stacking. do this after resizing the window,
3204        and applying decorations, because windows which fill the screen are
3205        considered "fullscreen" and it affects their layer */
3206     client_calc_layer(self);
3207
3208     if (fs) {
3209         /* try focus us when we go into fullscreen mode */
3210         client_focus(self);
3211     }
3212 }
3213
3214 static void client_iconify_recursive(ObClient *self,
3215                                      gboolean iconic, gboolean curdesk,
3216                                      gboolean hide_animation)
3217 {
3218     GSList *it;
3219     gboolean changed = FALSE;
3220
3221     if (self->iconic != iconic) {
3222         ob_debug("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
3223                  self->window);
3224
3225         if (iconic) {
3226             /* don't let non-normal windows iconify along with their parents
3227                or whatever */
3228             if (client_normal(self)) {
3229                 self->iconic = iconic;
3230
3231                 /* update the focus lists.. iconic windows go to the bottom of
3232                    the list. this will also call focus_cycle_addremove(). */
3233                 focus_order_to_bottom(self);
3234
3235                 changed = TRUE;
3236             }
3237         } else {
3238             self->iconic = iconic;
3239
3240             if (curdesk && self->desktop != screen_desktop &&
3241                 self->desktop != DESKTOP_ALL)
3242                 client_set_desktop(self, screen_desktop, FALSE, FALSE);
3243
3244             /* this puts it after the current focused window, this will
3245                also cause focus_cycle_addremove() to be called for the
3246                client */
3247             focus_order_like_new(self);
3248
3249             changed = TRUE;
3250         }
3251     }
3252
3253     if (changed) {
3254         client_change_state(self);
3255         if (config_animate_iconify && !hide_animation)
3256             frame_begin_iconify_animation(self->frame, iconic);
3257         /* do this after starting the animation so it doesn't flash */
3258         client_showhide(self);
3259     }
3260
3261     /* iconify all direct transients, and deiconify all transients
3262        (non-direct too) */
3263     for (it = self->transients; it; it = g_slist_next(it))
3264         if (it->data != self)
3265             if (client_is_direct_child(self, it->data) || !iconic)
3266                 client_iconify_recursive(it->data, iconic, curdesk,
3267                                          hide_animation);
3268 }
3269
3270 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk,
3271                     gboolean hide_animation)
3272 {
3273     if (self->functions & OB_CLIENT_FUNC_ICONIFY || !iconic) {
3274         /* move up the transient chain as far as possible first */
3275         self = client_search_top_direct_parent(self);
3276         client_iconify_recursive(self, iconic, curdesk, hide_animation);
3277     }
3278 }
3279
3280 void client_maximize(ObClient *self, gboolean max, gint dir)
3281 {
3282     gint x, y, w, h;
3283
3284     g_assert(dir == 0 || dir == 1 || dir == 2);
3285     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && max) return;/* can't */
3286
3287     /* check if already done */
3288     if (max) {
3289         if (dir == 0 && self->max_horz && self->max_vert) return;
3290         if (dir == 1 && self->max_horz) return;
3291         if (dir == 2 && self->max_vert) return;
3292     } else {
3293         if (dir == 0 && !self->max_horz && !self->max_vert) return;
3294         if (dir == 1 && !self->max_horz) return;
3295         if (dir == 2 && !self->max_vert) return;
3296     }
3297
3298     /* these will help configure_full figure out which screen to fill with
3299        the window */
3300     x = self->area.x;
3301     y = self->area.y;
3302     w = self->area.width;
3303     h = self->area.height;
3304
3305     if (max) {
3306         if ((dir == 0 || dir == 1) && !self->max_horz) { /* horz */
3307             RECT_SET(self->pre_max_area,
3308                      self->area.x, self->pre_max_area.y,
3309                      self->area.width, self->pre_max_area.height);
3310         }
3311         if ((dir == 0 || dir == 2) && !self->max_vert) { /* vert */
3312             RECT_SET(self->pre_max_area,
3313                      self->pre_max_area.x, self->area.y,
3314                      self->pre_max_area.width, self->area.height);
3315         }
3316     } else {
3317         if ((dir == 0 || dir == 1) && self->max_horz) { /* horz */
3318             g_assert(self->pre_max_area.width > 0);
3319
3320             x = self->pre_max_area.x;
3321             w = self->pre_max_area.width;
3322
3323             RECT_SET(self->pre_max_area, 0, self->pre_max_area.y,
3324                      0, self->pre_max_area.height);
3325         }
3326         if ((dir == 0 || dir == 2) && self->max_vert) { /* vert */
3327             g_assert(self->pre_max_area.height > 0);
3328
3329             y = self->pre_max_area.y;
3330             h = self->pre_max_area.height;
3331
3332             RECT_SET(self->pre_max_area, self->pre_max_area.x, 0,
3333                      self->pre_max_area.width, 0);
3334         }
3335     }
3336
3337     if (dir == 0 || dir == 1) /* horz */
3338         self->max_horz = max;
3339     if (dir == 0 || dir == 2) /* vert */
3340         self->max_vert = max;
3341
3342     client_change_state(self); /* change the state hints on the client */
3343
3344     client_setup_decor_and_functions(self, FALSE);
3345     client_move_resize(self, x, y, w, h);
3346 }
3347
3348 void client_shade(ObClient *self, gboolean shade)
3349 {
3350     if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
3351          shade) ||                         /* can't shade */
3352         self->shaded == shade) return;     /* already done */
3353
3354     self->shaded = shade;
3355     client_change_state(self);
3356     client_change_wm_state(self); /* the window is being hidden/shown */
3357     /* resize the frame to just the titlebar */
3358     frame_adjust_area(self->frame, FALSE, TRUE, FALSE);
3359 }
3360
3361 static void client_ping_event(ObClient *self, gboolean dead)
3362 {
3363     if (self->not_responding != dead) {
3364         self->not_responding = dead;
3365         client_update_title(self);
3366
3367         if (dead)
3368             /* the client isn't responding, so ask to kill it */
3369             client_prompt_kill(self);
3370         else {
3371             /* it came back to life ! */
3372
3373             if (self->kill_prompt) {
3374                 prompt_unref(self->kill_prompt);
3375                 self->kill_prompt = NULL;
3376             }
3377
3378             self->kill_level = 0;
3379         }
3380     }
3381 }
3382
3383 void client_close(ObClient *self)
3384 {
3385     if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
3386
3387     /* if closing an internal obprompt, that is just cancelling it */
3388     if (self->prompt) {
3389         prompt_cancel(self->prompt);
3390         return;
3391     }
3392
3393     /* in the case that the client provides no means to requesting that it
3394        close, we just kill it */
3395     if (!self->delete_window)
3396         /* don't use client_kill(), we should only kill based on PID in
3397            response to a lack of PING replies */
3398         XKillClient(obt_display, self->window);
3399     else {
3400         /* request the client to close with WM_DELETE_WINDOW */
3401         OBT_PROP_MSG_TO(self->window, self->window, WM_PROTOCOLS,
3402                         OBT_PROP_ATOM(WM_DELETE_WINDOW), event_curtime,
3403                         0, 0, 0, NoEventMask);
3404
3405         /* we're trying to close the window, so see if it is responding. if it
3406            is not, then we will let them kill the window */
3407         if (self->ping)
3408             ping_start(self, client_ping_event);
3409
3410         /* if we already know the window isn't responding (maybe they clicked
3411            no in the kill dialog but it hasn't come back to life), then show
3412            the kill dialog */
3413         if (self->not_responding)
3414             client_prompt_kill(self);
3415     }
3416 }
3417
3418 #define OB_KILL_RESULT_NO 0
3419 #define OB_KILL_RESULT_YES 1
3420
3421 static gboolean client_kill_requested(ObPrompt *p, gint result, gpointer data)
3422 {
3423     ObClient *self = data;
3424
3425     if (result == OB_KILL_RESULT_YES)
3426         client_kill(self);
3427     return TRUE; /* call the cleanup func */
3428 }
3429
3430 static void client_kill_cleanup(ObPrompt *p, gpointer data)
3431 {
3432     ObClient *self = data;
3433
3434     g_assert(p == self->kill_prompt);
3435
3436     prompt_unref(self->kill_prompt);
3437     self->kill_prompt = NULL;
3438 }
3439
3440 static void client_prompt_kill(ObClient *self)
3441 {
3442     /* check if we're already prompting */
3443     if (!self->kill_prompt) {
3444         ObPromptAnswer answers[] = {
3445             { 0, OB_KILL_RESULT_NO },
3446             { 0, OB_KILL_RESULT_YES }
3447         };
3448         gchar *m;
3449         const gchar *y, *title;
3450
3451         title = self->original_title;
3452         if (title[0] == '\0') {
3453             /* empty string, so use its parent */
3454             ObClient *p = client_search_top_direct_parent(self);
3455             if (p) title = p->original_title;
3456         }
3457
3458         if (client_on_localhost(self)) {
3459             const gchar *sig;
3460
3461             if (self->kill_level == 0)
3462                 sig = "terminate";
3463             else
3464                 sig = "kill";
3465
3466             m = g_strdup_printf
3467                 (_("The window \"%s\" does not seem to be responding.  Do you want to force it to exit by sending the %s signal?"),
3468                  title, sig);
3469             y = _("End Process");
3470         }
3471         else {
3472             m = g_strdup_printf
3473                 (_("The window \"%s\" does not seem to be responding.  Do you want to disconnect it from the X server?"),
3474                  title);
3475             y = _("Disconnect");
3476         }
3477         /* set the dialog buttons' text */
3478         answers[0].text = _("Cancel");  /* "no" */
3479         answers[1].text = y;            /* "yes" */
3480
3481         self->kill_prompt = prompt_new(m, NULL, answers,
3482                                        sizeof(answers)/sizeof(answers[0]),
3483                                        OB_KILL_RESULT_NO, /* default = no */
3484                                        OB_KILL_RESULT_NO, /* cancel = no */
3485                                        client_kill_requested,
3486                                        client_kill_cleanup,
3487                                        self);
3488         g_free(m);
3489     }
3490
3491     prompt_show(self->kill_prompt, self, TRUE);
3492 }
3493
3494 void client_kill(ObClient *self)
3495 {
3496     /* don't kill our own windows */
3497     if (self->prompt) return;
3498
3499     if (client_on_localhost(self) && self->pid) {
3500         /* running on the local host */
3501         if (self->kill_level == 0) {
3502             ob_debug("killing window 0x%x with pid %lu, with SIGTERM",
3503                      self->window, self->pid);
3504             kill(self->pid, SIGTERM);
3505             ++self->kill_level;
3506
3507             /* show that we're trying to kill it */
3508             client_update_title(self);
3509         }
3510         else {
3511             ob_debug("killing window 0x%x with pid %lu, with SIGKILL",
3512                      self->window, self->pid);
3513             kill(self->pid, SIGKILL); /* kill -9 */
3514         }
3515     }
3516     else {
3517         /* running on a remote host */
3518         XKillClient(obt_display, self->window);
3519     }
3520 }
3521
3522 void client_hilite(ObClient *self, gboolean hilite)
3523 {
3524     if (self->demands_attention == hilite)
3525         return; /* no change */
3526
3527     /* don't allow focused windows to hilite */
3528     self->demands_attention = hilite && !client_focused(self);
3529     if (self->frame != NULL) { /* if we're mapping, just set the state */
3530         if (self->demands_attention) {
3531             frame_flash_start(self->frame);
3532
3533             /* if the window is on another desktop then raise it and make it
3534                the most recently used window */
3535             if (self->desktop != screen_desktop &&
3536                 self->desktop != DESKTOP_ALL)
3537             {
3538                 stacking_raise(CLIENT_AS_WINDOW(self));
3539                 focus_order_to_top(self);
3540             }
3541         }
3542         else
3543             frame_flash_stop(self->frame);
3544         client_change_state(self);
3545     }
3546 }
3547
3548 static void client_set_desktop_recursive(ObClient *self,
3549                                          guint target,
3550                                          gboolean donthide,
3551                                          gboolean dontraise)
3552 {
3553     guint old;
3554     GSList *it;
3555
3556     if (target != self->desktop && self->type != OB_CLIENT_TYPE_DESKTOP) {
3557
3558         ob_debug("Setting desktop %u", target+1);
3559
3560         g_assert(target < screen_num_desktops || target == DESKTO