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