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