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