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