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