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