e3d3013eded536321b0648a754c8fd336f8facd6
[mikachu/openbox.git] / openbox / client.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    client.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "client.h"
21 #include "debug.h"
22 #include "startupnotify.h"
23 #include "dock.h"
24 #include "screen.h"
25 #include "moveresize.h"
26 #include "ping.h"
27 #include "place.h"
28 #include "frame.h"
29 #include "session.h"
30 #include "event.h"
31 #include "grab.h"
32 #include "prompt.h"
33 #include "focus.h"
34 #include "focus_cycle.h"
35 #include "stacking.h"
36 #include "openbox.h"
37 #include "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     }
3570
3571     prompt_show(self->kill_prompt, self, TRUE);
3572 }
3573
3574 void client_kill(ObClient *self)
3575 {
3576     /* don't kill our own windows */
3577     if (self->prompt) return;
3578
3579     if (client_on_localhost(self) && self->pid) {
3580         /* running on the local host */
3581         if (self->kill_level == 0) {
3582             ob_debug("killing window 0x%x with pid %lu, with SIGTERM",
3583                      self->window, self->pid);
3584             kill(self->pid, SIGTERM);
3585             ++self->kill_level;
3586
3587             /* show that we're trying to kill it */
3588             client_update_title(self);
3589         }
3590         else {
3591             ob_debug("killing window 0x%x with pid %lu, with SIGKILL",
3592                      self->window, self->pid);
3593             kill(self->pid, SIGKILL); /* kill -9 */
3594         }
3595     }
3596     else {
3597         /* running on a remote host */
3598         XKillClient(obt_display, self->window);
3599     }
3600 }
3601
3602 void client_hilite(ObClient *self, gboolean hilite)
3603 {
3604     if (self->demands_attention == hilite)
3605         return; /* no change */
3606
3607     /* don't allow focused windows to hilite */
3608     self->demands_attention = hilite && !client_focused(self);
3609     if (self->frame != NULL) { /* if we're mapping, just set the state */
3610         if (self->demands_attention) {
3611             frame_flash_start(self->frame);
3612
3613             /* if the window is on another desktop then raise it and make it
3614                the most recently used window */
3615             if (self->desktop != screen_desktop &&
3616                 self->desktop != DESKTOP_ALL)
3617             {
3618                 stacking_raise(CLIENT_AS_WINDOW(self));
3619                 focus_order_to_top(self);
3620             }
3621         }
3622         else
3623             frame_flash_stop(self->frame);
3624         client_change_state(self);
3625     }
3626 }
3627
3628 static void client_set_desktop_recursive(ObClient *self,
3629                                          guint target,
3630                                          gboolean donthide,
3631                                          gboolean dontraise)
3632 {
3633     guint old;
3634     GSList *it;
3635
3636     if (target != self->desktop && self->type != OB_CLIENT_TYPE_DESKTOP) {
3637
3638         ob_debug("Setting desktop %u", target+1);
3639
3640         g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
3641
3642         old = self->desktop;
3643         self->desktop = target;
3644         OBT_PROP_SET32(self->window, NET_WM_DESKTOP, CARDINAL, target);
3645         /* the frame can display the current desktop state */
3646         frame_adjust_state(self->frame);
3647         /* 'move' the window to the new desktop */
3648         if (!donthide)
3649             client_hide(self);
3650         client_show(self);
3651         /* raise if it was not already on the desktop */
3652         if (old != DESKTOP_ALL && !dontraise)
3653             stacking_raise(CLIENT_AS_WINDOW(self));
3654         if (STRUT_EXISTS(self->strut))
3655             screen_update_areas();
3656         else
3657             /* the new desktop's geometry may be different, so we may need to
3658                resize, for example if we are maximized */
3659             client_reconfigure(self, FALSE);
3660
3661         focus_cycle_addremove(self, FALSE);
3662     }
3663
3664     /* move all transients */
3665     for (it = self->transients; it; it = g_slist_next(it))
3666         if (it->data != self)
3667             if (client_is_direct_child(self, it->data))
3668                 client_set_desktop_recursive(it->data, target,
3669                                              donthide, dontraise);
3670 }
3671
3672 void client_set_desktop(ObClient *self, guint target,
3673                         gboolean donthide, gboolean dontraise)
3674 {
3675     self = client_search_top_direct_parent(self);
3676     client_set_desktop_recursive(self, target, donthide, dontraise);
3677
3678     focus_cycle_addremove(NULL, TRUE);
3679 }
3680
3681 gboolean client_is_direct_child(ObClient *parent, ObClient *child)
3682 {
3683     while (child != parent && (child = client_direct_parent(child)));
3684     return child == parent;
3685 }
3686
3687 ObClient *client_search_modal_child(ObClient *self)
3688 {
3689     GSList *it;
3690     ObClient *ret;
3691
3692     for (it = self->transients; it; it = g_slist_next(it)) {
3693         ObClient *c = it->data;
3694         if ((ret = client_search_modal_child(c))) return ret;
3695         if (c->modal) return c;
3696     }
3697     return NULL;
3698 }
3699
3700 struct ObClientFindDestroyUnmap {
3701     Window window;
3702     gint ignore_unmaps;
3703 };
3704
3705 static gboolean find_destroy_unmap(XEvent *e, gpointer data)
3706 {
3707     struct ObClientFindDestroyUnmap *find = data;
3708     if (e->type == DestroyNotify)
3709         return e->xdestroywindow.window == find->window;
3710     if (e->type == UnmapNotify && e->xunmap.window == find->window)
3711         /* ignore the first $find->ignore_unmaps$ many unmap events */
3712         return --find->ignore_unmaps < 0;
3713     return FALSE;
3714 }
3715
3716 gboolean client_validate(ObClient *self)
3717 {
3718     struct ObClientFindDestroyUnmap find;
3719
3720     XSync(obt_display, FALSE); /* get all events on the server */
3721
3722     find.window = self->window;
3723     find.ignore_unmaps = self->ignore_unmaps;
3724     if (xqueue_exists_local(find_destroy_unmap, &find))
3725         return FALSE;
3726
3727     return TRUE;
3728 }
3729
3730 void client_set_wm_state(ObClient *self, glong state)
3731 {
3732     if (state == self->wmstate) return; /* no change */
3733
3734     switch (state) {
3735     case IconicState:
3736         client_iconify(self, TRUE, TRUE, FALSE);
3737         break;
3738     case NormalState:
3739         client_iconify(self, FALSE, TRUE, FALSE);
3740         break;
3741     }
3742 }
3743
3744 void client_set_state(ObClient *self, Atom action, glong data1, glong data2)
3745 {
3746     gboolean shaded = self->shaded;
3747     gboolean fullscreen = self->fullscreen;
3748     gboolean undecorated = self->undecorated;
3749     gboolean max_horz = self->max_horz;
3750     gboolean max_vert = self->max_vert;
3751     gboolean modal = self->modal;
3752     gboolean iconic = self->iconic;
3753     gboolean demands_attention = self->demands_attention;
3754     gboolean above = self->above;
3755     gboolean below = self->below;
3756     gint i;
3757     gboolean value;
3758
3759     if (!(action == OBT_PROP_ATOM(NET_WM_STATE_ADD) ||
3760           action == OBT_PROP_ATOM(NET_WM_STATE_REMOVE) ||
3761           action == OBT_PROP_ATOM(NET_WM_STATE_TOGGLE)))
3762         /* an invalid action was passed to the client message, ignore it */
3763         return;
3764
3765     for (i = 0; i < 2; ++i) {
3766         Atom state = i == 0 ? data1 : data2;
3767
3768         if (!state) continue;
3769
3770         /* if toggling, then pick whether we're adding or removing */
3771         if (action == OBT_PROP_ATOM(NET_WM_STATE_TOGGLE)) {
3772             if (state == OBT_PROP_ATOM(NET_WM_STATE_MODAL))
3773                 value = modal;
3774             else if (state == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_VERT))
3775                 value = self->max_vert;
3776             else if (state == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_HORZ))
3777                 value = self->max_horz;
3778             else if (state == OBT_PROP_ATOM(NET_WM_STATE_SHADED))
3779                 value = shaded;
3780             else if (state == OBT_PROP_ATOM(NET_WM_STATE_SKIP_TASKBAR))
3781                 value = self->skip_taskbar;
3782             else if (state == OBT_PROP_ATOM(NET_WM_STATE_SKIP_PAGER))
3783                 value = self->skip_pager;
3784             else if (state == OBT_PROP_ATOM(NET_WM_STATE_HIDDEN))
3785                 value = self->iconic;
3786             else if (state == OBT_PROP_ATOM(NET_WM_STATE_FULLSCREEN))
3787                 value = fullscreen;
3788             else if (state == OBT_PROP_ATOM(NET_WM_STATE_ABOVE))
3789                 value = self->above;
3790             else if (state == OBT_PROP_ATOM(NET_WM_STATE_BELOW))
3791                 value = self->below;
3792             else if (state == OBT_PROP_ATOM(NET_WM_STATE_DEMANDS_ATTENTION))
3793                 value = self->demands_attention;
3794             else if (state == OBT_PROP_ATOM(OB_WM_STATE_UNDECORATED))
3795                 value = undecorated;
3796             else
3797                 g_assert_not_reached();
3798             action = value ? OBT_PROP_ATOM(NET_WM_STATE_REMOVE) :
3799                              OBT_PROP_ATOM(NET_WM_STATE_ADD);
3800         }
3801
3802         value = action == OBT_PROP_ATOM(NET_WM_STATE_ADD);
3803         if (state == OBT_PROP_ATOM(NET_WM_STATE_MODAL)) {
3804             modal = value;
3805         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_VERT)) {
3806             max_vert = value;
3807         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_HORZ)) {
3808             max_horz = value;
3809         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_SHADED)) {
3810             shaded = value;
3811         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_SKIP_TASKBAR)) {
3812             self->skip_taskbar = value;
3813         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_SKIP_PAGER)) {
3814             self->skip_pager = value;
3815         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_HIDDEN)) {
3816             iconic = value;
3817         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_FULLSCREEN)) {
3818             fullscreen = value;
3819         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_ABOVE)) {
3820             above = value;
3821             /* only unset below when setting above, otherwise you can't get to
3822                the normal layer */
3823             if (value)
3824                 below = FALSE;
3825         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_BELOW)) {
3826             /* and vice versa */
3827             if (value)
3828                 above = FALSE;
3829             below = value;
3830         } else if (state == OBT_PROP_ATOM(NET_WM_STATE_DEMANDS_ATTENTION)){
3831             demands_attention = value;
3832         } else if (state == OBT_PROP_ATOM(OB_WM_STATE_UNDECORATED)) {
3833             undecorated = value;
3834         }
3835     }
3836
3837     if (max_horz != self->max_horz || max_vert != self->max_vert) {
3838         if (max_horz != self->max_horz && max_vert != self->max_vert) {
3839             /* toggling both */
3840             if (max_horz == max_vert) { /* both going the same way */
3841                 client_maximize(self, max_horz, 0);
3842             } else {
3843                 client_maximize(self, max_horz, 1);
3844                 client_maximize(self, max_vert, 2);
3845             }
3846         } else {
3847             /* toggling one */
3848             if (max_horz != self->max_horz)
3849                 client_maximize(self, max_horz, 1);
3850             else
3851                 client_maximize(self, max_vert, 2);
3852         }
3853     }
3854     /* change fullscreen state before shading, as it will affect if the window
3855        can shade or not */
3856     if (fullscreen != self->fullscreen)
3857         client_fullscreen(self, fullscreen);
3858     if (shaded != self->shaded)
3859         client_shade(self, shaded);
3860     if (undecorated != self->undecorated)
3861         client_set_undecorated(self, undecorated);
3862     if (above != self->above || below != self->below) {
3863         self->above = above;
3864         self->below = below;
3865         client_calc_layer(self);
3866     }
3867
3868     if (modal != self->modal) {
3869         self->modal = modal;
3870         /* when a window changes modality, then its stacking order with its
3871            transients needs to change */
3872         stacking_raise(CLIENT_AS_WINDOW(self));
3873
3874         /* it also may get focused. if something is focused that shouldn't
3875            be focused anymore, then move the focus */
3876         if (focus_client && client_focus_target(focus_client) != focus_client)
3877             client_focus(focus_client);
3878     }
3879
3880     if (iconic != self->iconic)
3881         client_iconify(self, iconic, FALSE, FALSE);
3882
3883     if (demands_attention != self->demands_attention)
3884         client_hilite(self, demands_attention);
3885
3886     client_change_state(self); /* change the hint to reflect these changes */
3887
3888     focus_cycle_addremove(self, TRUE);
3889 }
3890
3891 ObClient *client_focus_target(ObClient *self)
3892 {
3893     ObClient *child = NULL;
3894
3895     child = client_search_modal_child(self);
3896     if (child) return child;
3897     return self;
3898 }
3899
3900 gboolean client_can_focus(ObClient *self)
3901 {
3902     /* choose the correct target */
3903     self = client_focus_target(self);
3904
3905     if (!self->frame->visible)
3906         return FALSE;
3907
3908     if (!(self->can_focus || self->focus_notify))
3909         return FALSE;
3910
3911     return TRUE;
3912 }
3913
3914 gboolean client_focus(ObClient *self)
3915 {
3916     if (!client_validate(self)) return FALSE;
3917
3918     /* we might not focus this window, so if we have modal children which would
3919        be focused instead, bring them to this desktop */
3920     client_bring_modal_windows(self);
3921
3922     /* choose the correct target */
3923     self = client_focus_target(self);
3924
3925     if (!client_can_focus(self)) {
3926         ob_debug_type(OB_DEBUG_FOCUS,
3927                       "Client %s can't be focused", self->title);
3928         return FALSE;
3929     }
3930
3931     ob_debug_type(OB_DEBUG_FOCUS,
3932                   "Focusing client \"%s\" (0x%x) at time %u",
3933                   self->title, self->window, event_time());
3934
3935     /* if using focus_delay, stop the timer now so that focus doesn't
3936        go moving on us */
3937     event_halt_focus_delay();
3938
3939     obt_display_ignore_errors(TRUE);
3940
3941     if (self->can_focus) {
3942         /* This can cause a BadMatch error with CurrentTime, or if an app
3943            passed in a bad time for _NET_WM_ACTIVE_WINDOW. */
3944         XSetInputFocus(obt_display, self->window, RevertToPointerRoot,
3945                        event_time());
3946     }
3947
3948     if (self->focus_notify) {
3949         XEvent ce;
3950         ce.xclient.type = ClientMessage;
3951         ce.xclient.message_type = OBT_PROP_ATOM(WM_PROTOCOLS);
3952         ce.xclient.display = obt_display;
3953         ce.xclient.window = self->window;
3954         ce.xclient.format = 32;
3955         ce.xclient.data.l[0] = OBT_PROP_ATOM(WM_TAKE_FOCUS);
3956         ce.xclient.data.l[1] = event_time();
3957         ce.xclient.data.l[2] = 0l;
3958         ce.xclient.data.l[3] = 0l;
3959         ce.xclient.data.l[4] = 0l;
3960         XSendEvent(obt_display, self->window, FALSE, NoEventMask, &ce);
3961     }
3962
3963     obt_display_ignore_errors(FALSE);
3964
3965     ob_debug_type(OB_DEBUG_FOCUS, "Error focusing? %d",
3966                   obt_display_error_occured);
3967     return !obt_display_error_occured;
3968 }
3969
3970 static void client_present(ObClient *self, gboolean here, gboolean raise,
3971                            gboolean unshade)
3972 {
3973     if (client_normal(self) && screen_showing_desktop)
3974         screen_show_desktop(FALSE, self);
3975     if (self->iconic)
3976         client_iconify(self, FALSE, here, FALSE);
3977     if (self->desktop != DESKTOP_ALL &&
3978         self->desktop != screen_desktop)
3979     {
3980         if (here)
3981             client_set_desktop(self, screen_desktop, FALSE, TRUE);
3982         else
3983             screen_set_desktop(self->desktop, FALSE);
3984     } else if (!self->frame->visible)
3985         /* if its not visible for other reasons, then don't mess
3986            with it */
3987         return;
3988     if (self->shaded && unshade)
3989         client_shade(self, FALSE);
3990     if (raise)
3991         stacking_raise(CLIENT_AS_WINDOW(self));
3992
3993     client_focus(self);
3994 }
3995
3996 /* this function exists to map to the net_active_window message in the ewmh */
3997 void client_activate(ObClient *self, gboolean desktop,
3998                      gboolean here, gboolean raise,
3999                      gboolean unshade, gboolean user)
4000 {
4001     self = client_focus_target(self);
4002
4003     if (client_can_steal_focus(self, desktop, user, event_time(), CurrentTime))
4004         client_present(self, here, raise, unshade);
4005     else
4006         client_hilite(self, TRUE);
4007 }
4008
4009 static void client_bring_windows_recursive(ObClient *self,
4010                                            guint desktop,
4011                                            gboolean helpers,
4012                                            gboolean modals,
4013                                            gboolean iconic)
4014 {
4015     GSList *it;
4016
4017     for (it = self->transients; it; it = g_slist_next(it))
4018         client_bring_windows_recursive(it->data, desktop,
4019                                        helpers, modals, iconic);
4020
4021     if (((helpers && client_helper(self)) ||
4022          (modals && self->modal)) &&
4023         ((self->desktop != desktop && self->desktop != DESKTOP_ALL) ||
4024          (iconic && self->iconic)))
4025     {
4026         if (iconic && self->iconic)
4027             client_iconify(self, FALSE, TRUE, FALSE);
4028         else
4029             client_set_desktop(self, desktop, FALSE, FALSE);
4030     }
4031 }
4032
4033 void client_bring_helper_windows(ObClient *self)
4034 {
4035     client_bring_windows_recursive(self, self->desktop, TRUE, FALSE, FALSE);
4036 }
4037
4038 void client_bring_modal_windows(ObClient *self)
4039 {
4040     client_bring_windows_recursive(self, self->desktop, FALSE, TRUE, TRUE);
4041 }
4042
4043 gboolean client_focused(ObClient *self)
4044 {
4045     return self == focus_client;
4046 }
4047
4048 RrImage* client_icon(ObClient *self)
4049 {
4050     RrImage *ret = NULL;
4051
4052     if (self->icon_set)
4053         ret = self->icon_set;
4054     else if (self->parents) {
4055         GSList *it;
4056         for (it = self->parents; it && !ret; it = g_slist_next(it))
4057             ret = client_icon(it->data);
4058     }
4059     if (!ret)
4060         ret = client_default_icon;
4061     return ret;
4062 }
4063
4064 void client_set_layer(ObClient *self, gint layer)
4065 {
4066     if (layer < 0) {
4067         self->below = TRUE;
4068         self->above = FALSE;
4069     } else if (layer == 0) {
4070         self->below = self->above = FALSE;
4071     } else {
4072         self->below = FALSE;
4073         self->above = TRUE;
4074     }
4075     client_calc_layer(self);
4076     client_change_state(self); /* reflect this in the state hints */
4077 }
4078
4079 void client_set_undecorated(ObClient *self, gboolean undecorated)
4080 {
4081     if (self->undecorated != undecorated &&
4082         /* don't let it undecorate if the function is missing, but let
4083            it redecorate */
4084         (self->functions & OB_CLIENT_FUNC_UNDECORATE || !undecorated))
4085     {
4086         self->undecorated = undecorated;
4087         client_setup_decor_and_functions(self, TRUE);
4088         client_change_state(self); /* reflect this in the state hints */
4089     }
4090 }
4091
4092 guint client_monitor(ObClient *self)
4093 {
4094     return screen_find_monitor(&self->frame->area);
4095 }
4096
4097 ObClient *client_direct_parent(ObClient *self)
4098 {
4099     if (!self->parents) return NULL;
4100     if (self->transient_for_group) return NULL;
4101     return self->parents->data;
4102 }
4103
4104 ObClient *client_search_top_direct_parent(ObClient *self)
4105 {
4106     ObClient *p;
4107     while ((p = client_direct_parent(self))) self = p;
4108     return self;
4109 }
4110
4111 static GSList *client_search_all_top_parents_internal(ObClient *self,
4112                                                       gboolean bylayer,
4113                                                       ObStackingLayer layer)
4114 {
4115     GSList *ret;
4116     ObClient *p;
4117
4118     /* move up the direct transient chain as far as possible */
4119     while ((p = client_direct_parent(self)) &&
4120            (!bylayer || p->layer == layer))
4121         self = p;
4122
4123     if (!self->parents)
4124         ret = g_slist_prepend(NULL, self);
4125     else
4126         ret = g_slist_copy(self->parents);
4127
4128     return ret;
4129 }
4130
4131 GSList *client_search_all_top_parents(ObClient *self)
4132 {
4133     return client_search_all_top_parents_internal(self, FALSE, 0);
4134 }
4135
4136 GSList *client_search_all_top_parents_layer(ObClient *self)
4137 {
4138     return client_search_all_top_parents_internal(self, TRUE, self->layer);
4139 }
4140
4141 ObClient *client_search_focus_parent(ObClient *self)
4142 {
4143     GSList *it;
4144
4145     for (it = self->parents; it; it = g_slist_next(it))
4146         if (client_focused(it->data)) return it->data;
4147
4148     return NULL;
4149 }
4150
4151 ObClient *client_search_focus_parent_full(ObClient *self)
4152 {
4153     GSList *it;
4154     ObClient *ret = NULL;
4155
4156     for (it = self->parents; it; it = g_slist_next(it)) {
4157         if (client_focused(it->data))
4158             ret = it->data;
4159         else
4160             ret = client_search_focus_parent_full(it->data);
4161         if (ret) break;
4162     }
4163     return ret;
4164 }
4165
4166 ObClient *client_search_parent(ObClient *self, ObClient *search)
4167 {
4168     GSList *it;
4169
4170     for (it = self->parents; it; it = g_slist_next(it))
4171         if (it->data == search) return search;
4172
4173     return NULL;
4174 }
4175
4176 ObClient *client_search_transient(ObClient *self, ObClient *search)
4177 {
4178     GSList *sit;
4179
4180     for (sit = self->transients; sit; sit = g_slist_next(sit)) {
4181         if (sit->data == search)
4182             return search;
4183         if (client_search_transient(sit->data, search))
4184             return search;
4185     }
4186     return NULL;
4187 }
4188
4189 static void detect_edge(Rect area, ObDirection dir,
4190                         gint my_head, gint my_size,
4191                         gint my_edge_start, gint my_edge_size,
4192                         gint *dest, gboolean *near_edge)
4193 {
4194     gint edge_start, edge_size, head, tail;
4195     gboolean skip_head = FALSE, skip_tail = FALSE;
4196
4197     switch (dir) {
4198         case OB_DIRECTION_NORTH:
4199         case OB_DIRECTION_SOUTH:
4200             edge_start = area.x;
4201             edge_size = area.width;
4202             break;
4203         case OB_DIRECTION_EAST:
4204         case OB_DIRECTION_WEST:
4205             edge_start = area.y;
4206             edge_size = area.height;
4207             break;
4208         default:
4209             g_assert_not_reached();
4210     }
4211
4212     /* do we collide with this window? */
4213     if (!RANGES_INTERSECT(my_edge_start, my_edge_size,
4214                 edge_start, edge_size))
4215         return;
4216
4217     switch (dir) {
4218         case OB_DIRECTION_NORTH:
4219             head = RECT_BOTTOM(area);
4220             tail = RECT_TOP(area);
4221             break;
4222         case OB_DIRECTION_SOUTH:
4223             head = RECT_TOP(area);
4224             tail = RECT_BOTTOM(area);
4225             break;
4226         case OB_DIRECTION_WEST:
4227             head = RECT_RIGHT(area);
4228             tail = RECT_LEFT(area);
4229             break;
4230         case OB_DIRECTION_EAST:
4231             head = RECT_LEFT(area);
4232             tail = RECT_RIGHT(area);
4233             break;
4234         default:
4235             g_assert_not_reached();
4236     }
4237     switch (dir) {
4238         case OB_DIRECTION_NORTH:
4239         case OB_DIRECTION_WEST:
4240             /* check if our window is past the head of this window */
4241             if (my_head <= head + 1)
4242                 skip_head = TRUE;
4243             /* check if our window's tail is past the tail of this window */
4244             if (my_head + my_size - 1 <= tail)
4245                 skip_tail = TRUE;
4246             /* check if the head of this window is closer than the previously
4247                chosen edge (take into account that the previously chosen
4248                edge might have been a tail, not a head) */
4249             if (head + (*near_edge ? 0 : my_size) <= *dest)
4250                 skip_head = TRUE;
4251             /* check if the tail of this window is closer than the previously
4252                chosen edge (take into account that the previously chosen
4253                edge might have been a head, not a tail) */
4254             if (tail - (!*near_edge ? 0 : my_size) <= *dest)
4255                 skip_tail = TRUE;
4256             break;
4257         case OB_DIRECTION_SOUTH:
4258         case OB_DIRECTION_EAST:
4259             /* check if our window is past the head of this window */
4260             if (my_head >= head - 1)
4261                 skip_head = TRUE;
4262             /* check if our window's tail is past the tail of this window */
4263             if (my_head - my_size + 1 >= tail)
4264                 skip_tail = TRUE;
4265             /* check if the head of this window is closer than the previously
4266                chosen edge (take into account that the previously chosen
4267                edge might have been a tail, not a head) */
4268             if (head - (*near_edge ? 0 : my_size) >= *dest)
4269                 skip_head = TRUE;
4270             /* check if the tail of this window is closer than the previously
4271                chosen edge (take into account that the previously chosen
4272                edge might have been a head, not a tail) */
4273             if (tail + (!*near_edge ? 0 : my_size) >= *dest)
4274                 skip_tail = TRUE;
4275             break;
4276         default:
4277             g_assert_not_reached();
4278     }
4279
4280     ob_debug("my head %d size %d", my_head, my_size);
4281     ob_debug("head %d tail %d dest %d", head, tail, *dest);
4282     if (!skip_head) {
4283         ob_debug("using near edge %d", head);
4284         *dest = head;
4285         *near_edge = TRUE;
4286     }
4287     else if (!skip_tail) {
4288         ob_debug("using far edge %d", tail);
4289         *dest = tail;
4290         *near_edge = FALSE;
4291     }
4292 }
4293
4294 void client_find_edge_directional(ObClient *self, ObDirection dir,
4295                                   gint my_head, gint my_size,
4296                                   gint my_edge_start, gint my_edge_size,
4297                                   gint *dest, gboolean *near_edge)
4298 {
4299     GList *it;
4300     Rect *a;
4301     Rect dock_area;
4302     gint edge;
4303     guint i;
4304
4305     a = screen_area(self->desktop, SCREEN_AREA_ALL_MONITORS,
4306                     &self->frame->area);
4307
4308     switch (dir) {
4309     case OB_DIRECTION_NORTH:
4310         edge = RECT_TOP(*a) - 1;
4311         break;
4312     case OB_DIRECTION_SOUTH:
4313         edge = RECT_BOTTOM(*a) + 1;
4314         break;
4315     case OB_DIRECTION_EAST:
4316         edge = RECT_RIGHT(*a) + 1;
4317         break;
4318     case OB_DIRECTION_WEST:
4319         edge = RECT_LEFT(*a) - 1;
4320         break;
4321     default:
4322         g_assert_not_reached();
4323     }
4324     /* default to the far edge, then narrow it down */
4325     *dest = edge;
4326     *near_edge = TRUE;
4327
4328     /* search for edges of monitors */
4329     for (i = 0; i < screen_num_monitors; ++i) {
4330         Rect *area = screen_area(self->desktop, i, NULL);
4331         detect_edge(*area, dir, my_head, my_size, my_edge_start,
4332                     my_edge_size, dest, near_edge);
4333         g_slice_free(Rect, area);
4334     }
4335
4336     /* search for edges of clients */
4337     for (it = client_list; it; it = g_list_next(it)) {
4338         ObClient *cur = it->data;
4339
4340         /* skip windows to not bump into */
4341         if (cur == self)
4342             continue;
4343         if (cur->iconic)
4344             continue;
4345         if (self->desktop != cur->desktop && cur->desktop != DESKTOP_ALL &&
4346             cur->desktop != screen_desktop)
4347             continue;
4348
4349         ob_debug("trying window %s", cur->title);
4350
4351         detect_edge(cur->frame->area, dir, my_head, my_size, my_edge_start,
4352                     my_edge_size, dest, near_edge);
4353     }
4354     dock_get_area(&dock_area);
4355     detect_edge(dock_area, dir, my_head, my_size, my_edge_start,
4356                 my_edge_size, dest, near_edge);
4357
4358     g_slice_free(Rect, a);
4359 }
4360
4361 void client_find_move_directional(ObClient *self, ObDirection dir,
4362                                   gint *x, gint *y)
4363 {
4364     gint head, size;
4365     gint e, e_start, e_size;
4366     gboolean near;
4367
4368     switch (dir) {
4369     case OB_DIRECTION_EAST:
4370         head = RECT_RIGHT(self->frame->area);
4371         size = self->frame->area.width;
4372         e_start = RECT_TOP(self->frame->area);
4373         e_size = self->frame->area.height;
4374         break;
4375     case OB_DIRECTION_WEST:
4376         head = RECT_LEFT(self->frame->area);
4377         size = self->frame->area.width;
4378         e_start = RECT_TOP(self->frame->area);
4379         e_size = self->frame->area.height;
4380         break;
4381     case OB_DIRECTION_NORTH:
4382         head = RECT_TOP(self->frame->area);
4383         size = self->frame->area.height;
4384         e_start = RECT_LEFT(self->frame->area);
4385         e_size = self->frame->area.width;
4386         break;
4387     case OB_DIRECTION_SOUTH:
4388         head = RECT_BOTTOM(self->frame->area);
4389         size = self->frame->area.height;
4390         e_start = RECT_LEFT(self->frame->area);
4391         e_size = self->frame->area.width;
4392         break;
4393     default:
4394         g_assert_not_reached();
4395     }
4396
4397     client_find_edge_directional(self, dir, head, size,
4398                                  e_start, e_size, &e, &near);
4399     *x = self->frame->area.x;
4400     *y = self->frame->area.y;
4401     switch (dir) {
4402     case OB_DIRECTION_EAST:
4403         if (near) e -= self->frame->area.width;
4404         else      e++;
4405         *x = e;
4406         break;
4407     case OB_DIRECTION_WEST:
4408         if (near) e++;
4409         else      e -= self->frame->area.width;
4410         *x = e;
4411         break;
4412     case OB_DIRECTION_NORTH:
4413         if (near) e++;
4414         else      e -= self->frame->area.height;
4415         *y = e;
4416         break;
4417     case OB_DIRECTION_SOUTH:
4418         if (near) e -= self->frame->area.height;
4419         else      e++;
4420         *y = e;
4421         break;
4422     default:
4423         g_assert_not_reached();
4424     }
4425     frame_frame_gravity(self->frame, x, y);
4426 }
4427
4428 void client_find_resize_directional(ObClient *self, ObDirection side,
4429                                     gboolean grow,
4430                                     gint *x, gint *y, gint *w, gint *h)
4431 {
4432     gint head;
4433     gint e, e_start, e_size, delta;
4434     gboolean near;
4435     ObDirection dir;
4436
4437     switch (side) {
4438     case OB_DIRECTION_EAST:
4439         head = RECT_RIGHT(self->frame->area) +
4440             (self->size_inc.width - 1) * (grow ? 1 : 0);
4441         e_start = RECT_TOP(self->frame->area);
4442         e_size = self->frame->area.height;
4443         dir = grow ? OB_DIRECTION_EAST : OB_DIRECTION_WEST;
4444         break;
4445     case OB_DIRECTION_WEST:
4446         head = RECT_LEFT(self->frame->area) -
4447             (self->size_inc.width - 1) * (grow ? 1 : 0);
4448         e_start = RECT_TOP(self->frame->area);
4449         e_size = self->frame->area.height;
4450         dir = grow ? OB_DIRECTION_WEST : OB_DIRECTION_EAST;
4451         break;
4452     case OB_DIRECTION_NORTH:
4453         head = RECT_TOP(self->frame->area) -
4454             (self->size_inc.height - 1) * (grow ? 1 : 0);
4455         e_start = RECT_LEFT(self->frame->area);
4456         e_size = self->frame->area.width;
4457         dir = grow ? OB_DIRECTION_NORTH : OB_DIRECTION_SOUTH;
4458         break;
4459     case OB_DIRECTION_SOUTH:
4460         head = RECT_BOTTOM(self->frame->area) +
4461             (self->size_inc.height - 1) * (grow ? 1 : 0);
4462         e_start = RECT_LEFT(self->frame->area);
4463         e_size = self->frame->area.width;
4464         dir = grow ? OB_DIRECTION_SOUTH : OB_DIRECTION_NORTH;
4465         break;
4466     default:
4467         g_assert_not_reached();
4468     }
4469
4470     ob_debug("head %d dir %d", head, dir);
4471     client_find_edge_directional(self, dir, head, 1,
4472                                  e_start, e_size, &e, &near);
4473     ob_debug("edge %d", e);
4474     *x = self->frame->area.x;
4475     *y = self->frame->area.y;
4476     *w = self->frame->area.width;
4477     *h = self->frame->area.height;
4478     switch (side) {
4479     case OB_DIRECTION_EAST:
4480         if (grow == near) --e;
4481         delta = e - RECT_RIGHT(self->frame->area);
4482         *w += delta;
4483         break;
4484     case OB_DIRECTION_WEST:
4485         if (grow == near) ++e;
4486         delta = RECT_LEFT(self->frame->area) - e;
4487         *x -= delta;
4488         *w += delta;
4489         break;
4490     case OB_DIRECTION_NORTH:
4491         if (grow == near) ++e;
4492         delta = RECT_TOP(self->frame->area) - e;
4493         *y -= delta;
4494         *h += delta;
4495         break;
4496     case OB_DIRECTION_SOUTH:
4497         if (grow == near) --e;
4498         delta = e - RECT_BOTTOM(self->frame->area);
4499         *h += delta;
4500         break;
4501     default:
4502         g_assert_not_reached();
4503     }
4504     frame_frame_gravity(self->frame, x, y);
4505     *w -= self->frame->size.left + self->frame->size.right;
4506     *h -= self->frame->size.top + self->frame->size.bottom;
4507 }
4508
4509 ObClient* client_under_pointer(void)
4510 {
4511     gint x, y;
4512     GList *it;
4513     ObClient *ret = NULL;
4514
4515     if (screen_pointer_pos(&x, &y)) {
4516         for (it = stacking_list; it; it = g_list_next(it)) {
4517             if (WINDOW_IS_CLIENT(it->data)) {
4518                 ObClient *c = WINDOW_AS_CLIENT(it->data);
4519                 if (c->frame->visible &&
4520                     /* check the desktop, this is done during desktop
4521                        switching and windows are shown/hidden status is not
4522                        reliable */
4523                     (c->desktop == screen_desktop ||
4524                      c->desktop == DESKTOP_ALL) &&
4525                     /* ignore all animating windows */
4526                     !frame_iconify_animating(c->frame) &&
4527                     RECT_CONTAINS(c->frame->area, x, y))
4528                 {
4529                     ret = c;
4530                     break;
4531                 }
4532             }
4533         }
4534     }
4535     return ret;
4536 }
4537
4538 gboolean client_has_group_siblings(ObClient *self)
4539 {
4540     return self->group && self->group->members->next;
4541 }
4542
4543 gboolean client_has_relative(ObClient *self)
4544 {
4545     return client_has_parent(self) ||
4546         client_has_group_siblings(self) ||
4547         client_has_children(self);
4548 }
4549
4550 /*! Returns TRUE if the client is running on the same machine as Openbox */
4551 gboolean client_on_localhost(ObClient *self)
4552 {
4553     return self->client_machine == NULL;
4554 }