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