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