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