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