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