add some more client tree searching functions. make transients always get focused...
[dana/openbox.git] / openbox / client.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    client.c for the Openbox window manager
4    Copyright (c) 2003        Ben Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "client.h"
20 #include "debug.h"
21 #include "startupnotify.h"
22 #include "dock.h"
23 #include "xerror.h"
24 #include "screen.h"
25 #include "moveresize.h"
26 #include "place.h"
27 #include "prop.h"
28 #include "extensions.h"
29 #include "frame.h"
30 #include "session.h"
31 #include "event.h"
32 #include "grab.h"
33 #include "focus.h"
34 #include "stacking.h"
35 #include "openbox.h"
36 #include "group.h"
37 #include "config.h"
38 #include "menuframe.h"
39 #include "keyboard.h"
40 #include "mouse.h"
41 #include "render/render.h"
42
43 #include <glib.h>
44 #include <X11/Xutil.h>
45
46 /*! The event mask to grab on client windows */
47 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
48                           StructureNotifyMask)
49
50 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
51                                 ButtonMotionMask)
52
53 GList      *client_list        = NULL;
54 GSList     *client_destructors = NULL;
55
56 static void client_get_all(ObClient *self);
57 static void client_toggle_border(ObClient *self, gboolean show);
58 static void client_get_startup_id(ObClient *self);
59 static void client_get_area(ObClient *self);
60 static void client_get_desktop(ObClient *self);
61 static void client_get_state(ObClient *self);
62 static void client_get_shaped(ObClient *self);
63 static void client_get_mwm_hints(ObClient *self);
64 static void client_get_gravity(ObClient *self);
65 static void client_showhide(ObClient *self);
66 static void client_change_allowed_actions(ObClient *self);
67 static void client_change_state(ObClient *self);
68 static void client_apply_startup_state(ObClient *self);
69 static void client_restore_session_state(ObClient *self);
70 static void client_restore_session_stacking(ObClient *self);
71 static void client_urgent_notify(ObClient *self);
72
73 void client_startup(gboolean reconfig)
74 {
75     if (reconfig) return;
76
77     client_set_list();
78 }
79
80 void client_shutdown(gboolean reconfig)
81 {
82 }
83
84 void client_add_destructor(GDestroyNotify func)
85 {
86     client_destructors = g_slist_prepend(client_destructors, (gpointer)func);
87 }
88
89 void client_remove_destructor(GDestroyNotify func)
90 {
91     client_destructors = g_slist_remove(client_destructors, (gpointer)func);
92 }
93
94 void client_set_list()
95 {
96     Window *windows, *win_it;
97     GList *it;
98     guint size = g_list_length(client_list);
99
100     /* create an array of the window ids */
101     if (size > 0) {
102         windows = g_new(Window, size);
103         win_it = windows;
104         for (it = client_list; it != NULL; it = it->next, ++win_it)
105             *win_it = ((ObClient*)it->data)->window;
106     } else
107         windows = NULL;
108
109     PROP_SETA32(RootWindow(ob_display, ob_screen),
110                 net_client_list, window, (guint32*)windows, size);
111
112     if (windows)
113         g_free(windows);
114
115     stacking_set_list();
116 }
117
118 /*
119 void client_foreach_transient(ObClient *self, ObClientForeachFunc func, void *data)
120 {
121     GSList *it;
122
123     for (it = self->transients; it; it = it->next) {
124         if (!func(it->data, data)) return;
125         client_foreach_transient(it->data, func, data);
126     }
127 }
128
129 void client_foreach_ancestor(ObClient *self, ObClientForeachFunc func, void *data)
130 {
131     if (self->transient_for) {
132         if (self->transient_for != OB_TRAN_GROUP) {
133             if (!func(self->transient_for, data)) return;
134             client_foreach_ancestor(self->transient_for, func, data);
135         } else {
136             GSList *it;
137
138             for (it = self->group->members; it; it = it->next)
139                 if (it->data != self &&
140                     !((ObClient*)it->data)->transient_for) {
141                     if (!func(it->data, data)) return;
142                     client_foreach_ancestor(it->data, func, data);
143                 }
144         }
145     }
146 }
147 */
148
149 void client_manage_all()
150 {
151     unsigned int i, j, nchild;
152     Window w, *children;
153     XWMHints *wmhints;
154     XWindowAttributes attrib;
155
156     XQueryTree(ob_display, RootWindow(ob_display, ob_screen),
157                &w, &w, &children, &nchild);
158
159     /* remove all icon windows from the list */
160     for (i = 0; i < nchild; i++) {
161         if (children[i] == None) continue;
162         wmhints = XGetWMHints(ob_display, children[i]);
163         if (wmhints) {
164             if ((wmhints->flags & IconWindowHint) &&
165                 (wmhints->icon_window != children[i]))
166                 for (j = 0; j < nchild; j++)
167                     if (children[j] == wmhints->icon_window) {
168                         children[j] = None;
169                         break;
170                     }
171             XFree(wmhints);
172         }
173     }
174
175     for (i = 0; i < nchild; ++i) {
176         if (children[i] == None)
177             continue;
178         if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
179             if (attrib.override_redirect) continue;
180
181             if (attrib.map_state != IsUnmapped)
182                 client_manage(children[i]);
183         }
184     }
185     XFree(children);
186 }
187
188 void client_manage(Window window)
189 {
190     ObClient *self;
191     XEvent e;
192     XWindowAttributes attrib;
193     XSetWindowAttributes attrib_set;
194     XWMHints *wmhint;
195     gboolean activate = FALSE;
196
197     grab_server(TRUE);
198
199     /* check if it has already been unmapped by the time we started mapping
200        the grab does a sync so we don't have to here */
201     if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
202         XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
203         XPutBackEvent(ob_display, &e);
204
205         grab_server(FALSE);
206         return; /* don't manage it */
207     }
208
209     /* make sure it isn't an override-redirect window */
210     if (!XGetWindowAttributes(ob_display, window, &attrib) ||
211         attrib.override_redirect) {
212         grab_server(FALSE);
213         return; /* don't manage it */
214     }
215   
216     /* is the window a docking app */
217     if ((wmhint = XGetWMHints(ob_display, window))) {
218         if ((wmhint->flags & StateHint) &&
219             wmhint->initial_state == WithdrawnState) {
220             dock_add(window, wmhint);
221             grab_server(FALSE);
222             XFree(wmhint);
223             return;
224         }
225         XFree(wmhint);
226     }
227
228     ob_debug("Managing window: %lx\n", window);
229
230     /* choose the events we want to receive on the CLIENT window */
231     attrib_set.event_mask = CLIENT_EVENTMASK;
232     attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
233     XChangeWindowAttributes(ob_display, window,
234                             CWEventMask|CWDontPropagate, &attrib_set);
235
236
237     /* create the ObClient struct, and populate it from the hints on the
238        window */
239     self = g_new0(ObClient, 1);
240     self->obwin.type = Window_Client;
241     self->window = window;
242
243     /* non-zero defaults */
244     self->title_count = 1;
245     self->wmstate = NormalState;
246     self->layer = -1;
247     self->desktop = screen_num_desktops; /* always an invalid value */
248
249     client_get_all(self);
250     client_restore_session_state(self);
251
252     sn_app_started(self->class);
253
254     client_change_state(self);
255
256     /* remove the client's border (and adjust re gravity) */
257     client_toggle_border(self, FALSE);
258      
259     /* specify that if we exit, the window should not be destroyed and should
260        be reparented back to root automatically */
261     XChangeSaveSet(ob_display, window, SetModeInsert);
262
263     /* create the decoration frame for the client window */
264     self->frame = frame_new();
265
266     frame_grab_client(self->frame, self);
267
268     grab_server(FALSE);
269
270     client_apply_startup_state(self);
271
272     /* update the focus lists */
273     focus_order_add_new(self);
274
275     stacking_add(CLIENT_AS_WINDOW(self));
276     client_restore_session_stacking(self);
277
278     /* focus the new window? */
279     if (ob_state() != OB_STATE_STARTING &&
280         (config_focus_new || client_search_focus_parent(self)) &&
281         /* note the check against Type_Normal/Dialog, not client_normal(self),
282            which would also include other types. in this case we want more
283            strict rules for focus */
284         (self->type == OB_CLIENT_TYPE_NORMAL ||
285          self->type == OB_CLIENT_TYPE_DIALOG))
286     {        
287         activate = TRUE;
288 #if 0
289         if (self->desktop != screen_desktop) {
290             /* activate the window */
291             activate = TRUE;
292         } else {
293             gboolean group_foc = FALSE;
294
295             if (self->group) {
296                 GSList *it;
297
298                 for (it = self->group->members; it; it = it->next)
299                 {
300                     if (client_focused(it->data))
301                     {
302                         group_foc = TRUE;
303                         break;
304                     }
305                 }
306             }
307             if ((group_foc ||
308                  (!self->transient_for && (!self->group ||
309                                            !self->group->members->next))) ||
310                 client_search_focus_tree_full(self) ||
311                 !focus_client ||
312                 !client_normal(focus_client))
313             {
314                 /* activate the window */
315                 activate = TRUE;
316             }
317         }
318 #endif
319     }
320
321     if (ob_state() == OB_STATE_RUNNING) {
322         int x = self->area.x, ox = x;
323         int y = self->area.y, oy = y;
324
325         place_client(self, &x, &y);
326
327         /* make sure the window is visible */
328         client_find_onscreen(self, &x, &y,
329                              self->frame->area.width,
330                              self->frame->area.height,
331                              client_normal(self));
332
333         if (x != ox || y != oy)
334             client_move(self, x, y);
335     }
336
337     client_showhide(self);
338
339     /* use client_focus instead of client_activate cuz client_activate does
340        stuff like switch desktops etc and I'm not interested in all that when
341        a window maps since its not based on an action from the user like
342        clicking a window to activate is. so keep the new window out of the way
343        but do focus it. */
344     if (activate) client_focus(self);
345
346     /* client_activate does this but we aret using it so we have to do it
347        here as well */
348     if (screen_showing_desktop)
349         screen_show_desktop(FALSE);
350
351     /* add to client list/map */
352     client_list = g_list_append(client_list, self);
353     g_hash_table_insert(window_map, &self->window, self);
354
355     /* this has to happen after we're in the client_list */
356     screen_update_areas();
357
358     /* update the list hints */
359     client_set_list();
360
361     keyboard_grab_for_client(self, TRUE);
362     mouse_grab_for_client(self, TRUE);
363
364     ob_debug("Managed window 0x%lx (%s)\n", window, self->class);
365 }
366
367 void client_unmanage_all()
368 {
369     while (client_list != NULL)
370         client_unmanage(client_list->data);
371 }
372
373 void client_unmanage(ObClient *self)
374 {
375     guint j;
376     GSList *it;
377
378     ob_debug("Unmanaging window: %lx (%s)\n", self->window, self->class);
379
380     g_assert(self != NULL);
381
382     keyboard_grab_for_client(self, FALSE);
383     mouse_grab_for_client(self, FALSE);
384
385     /* remove the window from our save set */
386     XChangeSaveSet(ob_display, self->window, SetModeDelete);
387
388     /* we dont want events no more */
389     XSelectInput(ob_display, self->window, NoEventMask);
390
391     frame_hide(self->frame);
392
393     client_list = g_list_remove(client_list, self);
394     stacking_remove(self);
395     g_hash_table_remove(window_map, &self->window);
396
397     /* update the focus lists */
398     focus_order_remove(self);
399
400     /* once the client is out of the list, update the struts to remove it's
401        influence */
402     screen_update_areas();
403
404     /* tell our parent(s) that we're gone */
405     if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
406         GSList *it;
407
408         for (it = self->group->members; it; it = it->next)
409             if (it->data != self)
410                 ((ObClient*)it->data)->transients =
411                     g_slist_remove(((ObClient*)it->data)->transients, self);
412     } else if (self->transient_for) {        /* transient of window */
413         self->transient_for->transients =
414             g_slist_remove(self->transient_for->transients, self);
415     }
416
417     /* tell our transients that we're gone */
418     for (it = self->transients; it != NULL; it = it->next) {
419         if (((ObClient*)it->data)->transient_for != OB_TRAN_GROUP) {
420             ((ObClient*)it->data)->transient_for = NULL;
421             client_calc_layer(it->data);
422         }
423     }
424
425     for (it = client_destructors; it; it = g_slist_next(it)) {
426         GDestroyNotify func = (GDestroyNotify) it->data;
427         func(self);
428     }
429         
430     if (focus_client == self) {
431         XEvent e;
432
433         /* focus the last focused window on the desktop, and ignore enter
434            events from the unmap so it doesnt mess with the focus */
435         while (XCheckTypedEvent(ob_display, EnterNotify, &e));
436         client_unfocus(self);
437     }
438
439     /* remove from its group */
440     if (self->group) {
441         group_remove(self->group, self);
442         self->group = NULL;
443     }
444
445     /* give the client its border back */
446     client_toggle_border(self, TRUE);
447
448     /* reparent the window out of the frame, and free the frame */
449     frame_release_client(self->frame, self);
450     self->frame = NULL;
451      
452     if (ob_state() != OB_STATE_EXITING) {
453         /* these values should not be persisted across a window
454            unmapping/mapping */
455         PROP_ERASE(self->window, net_wm_desktop);
456         PROP_ERASE(self->window, net_wm_state);
457         PROP_ERASE(self->window, wm_state);
458     } else {
459         /* if we're left in an iconic state, the client wont be mapped. this is
460            bad, since we will no longer be managing the window on restart */
461         if (self->iconic)
462             XMapWindow(ob_display, self->window);
463     }
464
465
466     ob_debug("Unmanaged window 0x%lx\n", self->window);
467
468     /* free all data allocated in the client struct */
469     g_slist_free(self->transients);
470     for (j = 0; j < self->nicons; ++j)
471         g_free(self->icons[j].data);
472     if (self->nicons > 0)
473         g_free(self->icons);
474     g_free(self->title);
475     g_free(self->icon_title);
476     g_free(self->name);
477     g_free(self->class);
478     g_free(self->role);
479     g_free(self->sm_client_id);
480     g_free(self);
481      
482     /* update the list hints */
483     client_set_list();
484 }
485
486 static void client_urgent_notify(ObClient *self)
487 {
488     if (self->urgent)
489         frame_flash_start(self->frame);
490     else
491         frame_flash_stop(self->frame);
492 }
493
494 static void client_restore_session_state(ObClient *self)
495 {
496     GList *it;
497
498     if (!(it = session_state_find(self)))
499         return;
500
501     self->session = it->data;
502
503     RECT_SET(self->area, self->session->x, self->session->y,
504              self->session->w, self->session->h);
505     self->positioned = TRUE;
506     XResizeWindow(ob_display, self->window,
507                   self->session->w, self->session->h);
508
509     self->desktop = (self->session->desktop == DESKTOP_ALL ?
510                      self->session->desktop :
511                      MIN(screen_num_desktops - 1, self->session->desktop));
512     PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
513
514     self->shaded = self->session->shaded;
515     self->iconic = self->session->iconic;
516     self->skip_pager = self->session->skip_pager;
517     self->skip_taskbar = self->session->skip_taskbar;
518     self->fullscreen = self->session->fullscreen;
519     self->above = self->session->above;
520     self->below = self->session->below;
521     self->max_horz = self->session->max_horz;
522     self->max_vert = self->session->max_vert;
523 }
524
525 static void client_restore_session_stacking(ObClient *self)
526 {
527     GList *it;
528
529     if (!self->session) return;
530
531     it = g_list_find(session_saved_state, self->session);
532     for (it = g_list_previous(it); it; it = g_list_previous(it)) {
533         GList *cit;
534
535         for (cit = client_list; cit; cit = g_list_next(cit))
536             if (session_state_cmp(it->data, cit->data))
537                 break;
538         if (cit) {
539             client_calc_layer(self);
540             stacking_below(CLIENT_AS_WINDOW(self),
541                            CLIENT_AS_WINDOW(cit->data));
542             break;
543         }
544     }
545 }
546
547 void client_move_onscreen(ObClient *self, gboolean rude)
548 {
549     int x = self->area.x;
550     int y = self->area.y;
551     if (client_find_onscreen(self, &x, &y,
552                              self->frame->area.width,
553                              self->frame->area.height, rude)) {
554         client_move(self, x, y);
555     }
556 }
557
558 gboolean client_find_onscreen(ObClient *self, int *x, int *y, int w, int h,
559                               gboolean rude)
560 {
561     Rect *a;
562     int ox = *x, oy = *y;
563
564     frame_client_gravity(self->frame, x, y); /* get where the frame
565                                                 would be */
566
567     /* XXX watch for xinerama dead areas */
568
569     a = screen_area(self->desktop);
570     if (client_normal(self)) {
571         if (!self->strut.right && *x >= a->x + a->width - 1)
572             *x = a->x + a->width - self->frame->area.width;
573         if (!self->strut.bottom && *y >= a->y + a->height - 1)
574             *y = a->y + a->height - self->frame->area.height;
575         if (!self->strut.left && *x + self->frame->area.width - 1 < a->x)
576             *x = a->x;
577         if (!self->strut.top && *y + self->frame->area.height - 1 < a->y)
578             *y = a->y;
579     }
580
581     if (rude) {
582         /* this is my MOZILLA BITCHSLAP. oh ya it fucking feels good.
583            Java can suck it too. */
584
585         /* dont let windows map/move into the strut unless they
586            are bigger than the available area */
587         if (w <= a->width) {
588             if (!self->strut.left && *x < a->x) *x = a->x;
589             if (!self->strut.right && *x + w > a->x + a->width)
590                 *x = a->x + a->width - w;
591         }
592         if (h <= a->height) {
593             if (!self->strut.top && *y < a->y) *y = a->y;
594             if (!self->strut.bottom && *y + h > a->y + a->height)
595                 *y = a->y + a->height - h;
596         }
597     }
598
599     frame_frame_gravity(self->frame, x, y); /* get where the client
600                                                should be */
601
602     return ox != *x || oy != *y;
603 }
604
605 static void client_toggle_border(ObClient *self, gboolean show)
606 {
607     /* adjust our idea of where the client is, based on its border. When the
608        border is removed, the client should now be considered to be in a
609        different position.
610        when re-adding the border to the client, the same operation needs to be
611        reversed. */
612     int oldx = self->area.x, oldy = self->area.y;
613     int x = oldx, y = oldy;
614     switch(self->gravity) {
615     default:
616     case NorthWestGravity:
617     case WestGravity:
618     case SouthWestGravity:
619         break;
620     case NorthEastGravity:
621     case EastGravity:
622     case SouthEastGravity:
623         if (show) x -= self->border_width * 2;
624         else      x += self->border_width * 2;
625         break;
626     case NorthGravity:
627     case SouthGravity:
628     case CenterGravity:
629     case ForgetGravity:
630     case StaticGravity:
631         if (show) x -= self->border_width;
632         else      x += self->border_width;
633         break;
634     }
635     switch(self->gravity) {
636     default:
637     case NorthWestGravity:
638     case NorthGravity:
639     case NorthEastGravity:
640         break;
641     case SouthWestGravity:
642     case SouthGravity:
643     case SouthEastGravity:
644         if (show) y -= self->border_width * 2;
645         else      y += self->border_width * 2;
646         break;
647     case WestGravity:
648     case EastGravity:
649     case CenterGravity:
650     case ForgetGravity:
651     case StaticGravity:
652         if (show) y -= self->border_width;
653         else      y += self->border_width;
654         break;
655     }
656     self->area.x = x;
657     self->area.y = y;
658
659     if (show) {
660         XSetWindowBorderWidth(ob_display, self->window, self->border_width);
661
662         /* move the client so it is back it the right spot _with_ its
663            border! */
664         if (x != oldx || y != oldy)
665             XMoveWindow(ob_display, self->window, x, y);
666     } else
667         XSetWindowBorderWidth(ob_display, self->window, 0);
668 }
669
670
671 static void client_get_all(ObClient *self)
672 {
673     client_get_area(self);
674     client_update_transient_for(self);
675     client_update_wmhints(self);
676     client_get_startup_id(self);
677     client_get_desktop(self);
678     client_get_state(self);
679     client_get_shaped(self);
680
681     client_get_mwm_hints(self);
682     client_get_type(self);/* this can change the mwmhints for special cases */
683
684     client_update_protocols(self);
685
686     client_get_gravity(self); /* get the attribute gravity */
687     client_update_normal_hints(self); /* this may override the attribute
688                                          gravity */
689
690     /* got the type, the mwmhints, the protocols, and the normal hints
691        (min/max sizes), so we're ready to set up the decorations/functions */
692     client_setup_decor_and_functions(self);
693   
694     client_update_title(self);
695     client_update_class(self);
696     client_update_sm_client_id(self);
697     client_update_strut(self);
698     client_update_icons(self);
699 }
700
701 static void client_get_startup_id(ObClient *self)
702 {
703     if (!(PROP_GETS(self->window, net_startup_id, utf8, &self->startup_id)))
704         if (self->group)
705             PROP_GETS(self->group->leader,
706                       net_startup_id, utf8, &self->startup_id);
707 }
708
709 static void client_get_area(ObClient *self)
710 {
711     XWindowAttributes wattrib;
712     Status ret;
713   
714     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
715     g_assert(ret != BadWindow);
716
717     RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
718     self->border_width = wattrib.border_width;
719 }
720
721 static void client_get_desktop(ObClient *self)
722 {
723     guint32 d = screen_num_desktops; /* an always-invalid value */
724
725     if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
726         if (d >= screen_num_desktops && d != DESKTOP_ALL)
727             self->desktop = screen_num_desktops - 1;
728         else
729             self->desktop = d;
730     } else {
731         gboolean trdesk = FALSE;
732
733        if (self->transient_for) {
734            if (self->transient_for != OB_TRAN_GROUP) {
735                 self->desktop = self->transient_for->desktop;
736                 trdesk = TRUE;
737             } else {
738                 GSList *it;
739
740                 for (it = self->group->members; it; it = it->next)
741                     if (it->data != self &&
742                         !((ObClient*)it->data)->transient_for) {
743                         self->desktop = ((ObClient*)it->data)->desktop;
744                         trdesk = TRUE;
745                         break;
746                     }
747             }
748        }
749        if (!trdesk) {
750            /* try get from the startup-notification protocol */
751            if (sn_get_desktop(self->startup_id, &self->desktop)) {
752                if (self->desktop >= screen_num_desktops &&
753                    self->desktop != DESKTOP_ALL)
754                    self->desktop = screen_num_desktops - 1;
755            } else
756                /* defaults to the current desktop */
757                self->desktop = screen_desktop;
758        }
759     }
760     if (self->desktop != d) {
761         /* set the desktop hint, to make sure that it always exists */
762         PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
763     }
764 }
765
766 static void client_get_state(ObClient *self)
767 {
768     guint32 *state;
769     guint num;
770   
771     if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
772         gulong i;
773         for (i = 0; i < num; ++i) {
774             if (state[i] == prop_atoms.net_wm_state_modal)
775                 self->modal = TRUE;
776             else if (state[i] == prop_atoms.net_wm_state_shaded)
777                 self->shaded = TRUE;
778             else if (state[i] == prop_atoms.net_wm_state_hidden)
779                 self->iconic = TRUE;
780             else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
781                 self->skip_taskbar = TRUE;
782             else if (state[i] == prop_atoms.net_wm_state_skip_pager)
783                 self->skip_pager = TRUE;
784             else if (state[i] == prop_atoms.net_wm_state_fullscreen)
785                 self->fullscreen = TRUE;
786             else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
787                 self->max_vert = TRUE;
788             else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
789                 self->max_horz = TRUE;
790             else if (state[i] == prop_atoms.net_wm_state_above)
791                 self->above = TRUE;
792             else if (state[i] == prop_atoms.net_wm_state_below)
793                 self->below = TRUE;
794             else if (state[i] == prop_atoms.ob_wm_state_undecorated)
795                 self->undecorated = TRUE;
796         }
797
798         g_free(state);
799     }
800 }
801
802 static void client_get_shaped(ObClient *self)
803 {
804     self->shaped = FALSE;
805 #ifdef   SHAPE
806     if (extensions_shape) {
807         int foo;
808         guint ufoo;
809         int s;
810
811         XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
812
813         XShapeQueryExtents(ob_display, self->window, &s, &foo,
814                            &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
815                            &ufoo);
816         self->shaped = (s != 0);
817     }
818 #endif
819 }
820
821 void client_update_transient_for(ObClient *self)
822 {
823     Window t = None;
824     ObClient *target = NULL;
825
826     if (XGetTransientForHint(ob_display, self->window, &t)) {
827         self->transient = TRUE;
828         if (t != self->window) { /* cant be transient to itself! */
829             target = g_hash_table_lookup(window_map, &t);
830             /* if this happens then we need to check for it*/
831             g_assert(target != self);
832             if (target && !WINDOW_IS_CLIENT(target)) {
833                 /* this can happen when a dialog is a child of
834                    a dockapp, for example */
835                 target = NULL;
836             }
837             
838             if (!target && self->group) {
839                 /* not transient to a client, see if it is transient for a
840                    group */
841                 if (t == self->group->leader ||
842                     t == None ||
843                     t == RootWindow(ob_display, ob_screen)) {
844                     /* window is a transient for its group! */
845                     target = OB_TRAN_GROUP;
846                 }
847             }
848         }
849     } else
850         self->transient = FALSE;
851
852     /* if anything has changed... */
853     if (target != self->transient_for) {
854         if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
855             GSList *it;
856
857             /* remove from old parents */
858             for (it = self->group->members; it; it = g_slist_next(it)) {
859                 ObClient *c = it->data;
860                 if (c != self && !c->transient_for)
861                     c->transients = g_slist_remove(c->transients, self);
862             }
863         } else if (self->transient_for != NULL) { /* transient of window */
864             /* remove from old parent */
865             self->transient_for->transients =
866                 g_slist_remove(self->transient_for->transients, self);
867         }
868         self->transient_for = target;
869         if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
870             GSList *it;
871
872             /* add to new parents */
873             for (it = self->group->members; it; it = g_slist_next(it)) {
874                 ObClient *c = it->data;
875                 if (c != self && !c->transient_for)
876                     c->transients = g_slist_append(c->transients, self);
877             }
878
879             /* remove all transients which are in the group, that causes
880                circlular pointer hell of doom */
881             for (it = self->group->members; it; it = g_slist_next(it)) {
882                 GSList *sit, *next;
883                 for (sit = self->transients; sit; sit = next) {
884                     next = g_slist_next(sit);
885                     if (sit->data == it->data)
886                         self->transients =
887                             g_slist_delete_link(self->transients, sit);
888                 }
889             }
890         } else if (self->transient_for != NULL) { /* transient of window */
891             /* add to new parent */
892             self->transient_for->transients =
893                 g_slist_append(self->transient_for->transients, self);
894         }
895     }
896 }
897
898 static void client_get_mwm_hints(ObClient *self)
899 {
900     guint num;
901     guint32 *hints;
902
903     self->mwmhints.flags = 0; /* default to none */
904
905     if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
906                     &hints, &num)) {
907         if (num >= OB_MWM_ELEMENTS) {
908             self->mwmhints.flags = hints[0];
909             self->mwmhints.functions = hints[1];
910             self->mwmhints.decorations = hints[2];
911         }
912         g_free(hints);
913     }
914 }
915
916 void client_get_type(ObClient *self)
917 {
918     guint num, i;
919     guint32 *val;
920
921     self->type = -1;
922   
923     if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
924         /* use the first value that we know about in the array */
925         for (i = 0; i < num; ++i) {
926             if (val[i] == prop_atoms.net_wm_window_type_desktop)
927                 self->type = OB_CLIENT_TYPE_DESKTOP;
928             else if (val[i] == prop_atoms.net_wm_window_type_dock)
929                 self->type = OB_CLIENT_TYPE_DOCK;
930             else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
931                 self->type = OB_CLIENT_TYPE_TOOLBAR;
932             else if (val[i] == prop_atoms.net_wm_window_type_menu)
933                 self->type = OB_CLIENT_TYPE_MENU;
934             else if (val[i] == prop_atoms.net_wm_window_type_utility)
935                 self->type = OB_CLIENT_TYPE_UTILITY;
936             else if (val[i] == prop_atoms.net_wm_window_type_splash)
937                 self->type = OB_CLIENT_TYPE_SPLASH;
938             else if (val[i] == prop_atoms.net_wm_window_type_dialog)
939                 self->type = OB_CLIENT_TYPE_DIALOG;
940             else if (val[i] == prop_atoms.net_wm_window_type_normal)
941                 self->type = OB_CLIENT_TYPE_NORMAL;
942             else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
943                 /* prevent this window from getting any decor or
944                    functionality */
945                 self->mwmhints.flags &= (OB_MWM_FLAG_FUNCTIONS |
946                                          OB_MWM_FLAG_DECORATIONS);
947                 self->mwmhints.decorations = 0;
948                 self->mwmhints.functions = 0;
949             }
950             if (self->type != (ObClientType) -1)
951                 break; /* grab the first legit type */
952         }
953         g_free(val);
954     }
955     
956     if (self->type == (ObClientType) -1) {
957         /*the window type hint was not set, which means we either classify
958           ourself as a normal window or a dialog, depending on if we are a
959           transient. */
960         if (self->transient)
961             self->type = OB_CLIENT_TYPE_DIALOG;
962         else
963             self->type = OB_CLIENT_TYPE_NORMAL;
964     }
965 }
966
967 void client_update_protocols(ObClient *self)
968 {
969     guint32 *proto;
970     guint num_return, i;
971
972     self->focus_notify = FALSE;
973     self->delete_window = FALSE;
974
975     if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
976         for (i = 0; i < num_return; ++i) {
977             if (proto[i] == prop_atoms.wm_delete_window) {
978                 /* this means we can request the window to close */
979                 self->delete_window = TRUE;
980             } else if (proto[i] == prop_atoms.wm_take_focus)
981                 /* if this protocol is requested, then the window will be
982                    notified whenever we want it to receive focus */
983                 self->focus_notify = TRUE;
984         }
985         g_free(proto);
986     }
987 }
988
989 static void client_get_gravity(ObClient *self)
990 {
991     XWindowAttributes wattrib;
992     Status ret;
993
994     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
995     g_assert(ret != BadWindow);
996     self->gravity = wattrib.win_gravity;
997 }
998
999 void client_update_normal_hints(ObClient *self)
1000 {
1001     XSizeHints size;
1002     long ret;
1003     int oldgravity = self->gravity;
1004
1005     /* defaults */
1006     self->min_ratio = 0.0f;
1007     self->max_ratio = 0.0f;
1008     SIZE_SET(self->size_inc, 1, 1);
1009     SIZE_SET(self->base_size, 0, 0);
1010     SIZE_SET(self->min_size, 0, 0);
1011     SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
1012
1013     /* get the hints from the window */
1014     if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
1015         self->positioned = !!(size.flags & (PPosition|USPosition));
1016
1017         if (size.flags & PWinGravity) {
1018             self->gravity = size.win_gravity;
1019       
1020             /* if the client has a frame, i.e. has already been mapped and
1021                is changing its gravity */
1022             if (self->frame && self->gravity != oldgravity) {
1023                 /* move our idea of the client's position based on its new
1024                    gravity */
1025                 self->area.x = self->frame->area.x;
1026                 self->area.y = self->frame->area.y;
1027                 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
1028             }
1029         }
1030
1031         if (size.flags & PAspect) {
1032             if (size.min_aspect.y)
1033                 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
1034             if (size.max_aspect.y)
1035                 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
1036         }
1037
1038         if (size.flags & PMinSize)
1039             SIZE_SET(self->min_size, size.min_width, size.min_height);
1040     
1041         if (size.flags & PMaxSize)
1042             SIZE_SET(self->max_size, size.max_width, size.max_height);
1043     
1044         if (size.flags & PBaseSize)
1045             SIZE_SET(self->base_size, size.base_width, size.base_height);
1046     
1047         if (size.flags & PResizeInc)
1048             SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
1049     }
1050 }
1051
1052 void client_setup_decor_and_functions(ObClient *self)
1053 {
1054     /* start with everything (cept fullscreen) */
1055     self->decorations =
1056         (OB_FRAME_DECOR_TITLEBAR |
1057          (ob_rr_theme->show_handle ? OB_FRAME_DECOR_HANDLE : 0) |
1058          OB_FRAME_DECOR_GRIPS |
1059          OB_FRAME_DECOR_BORDER |
1060          OB_FRAME_DECOR_ICON |
1061          OB_FRAME_DECOR_ALLDESKTOPS |
1062          OB_FRAME_DECOR_ICONIFY |
1063          OB_FRAME_DECOR_MAXIMIZE |
1064          OB_FRAME_DECOR_SHADE);
1065     self->functions =
1066         (OB_CLIENT_FUNC_RESIZE |
1067          OB_CLIENT_FUNC_MOVE |
1068          OB_CLIENT_FUNC_ICONIFY |
1069          OB_CLIENT_FUNC_MAXIMIZE |
1070          OB_CLIENT_FUNC_SHADE);
1071     if (self->delete_window) {
1072         self->functions |= OB_CLIENT_FUNC_CLOSE;
1073         self->decorations |= OB_FRAME_DECOR_CLOSE;
1074     }
1075
1076     if (!(self->min_size.width < self->max_size.width ||
1077           self->min_size.height < self->max_size.height))
1078         self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1079
1080     switch (self->type) {
1081     case OB_CLIENT_TYPE_NORMAL:
1082         /* normal windows retain all of the possible decorations and
1083            functionality, and are the only windows that you can fullscreen */
1084         self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1085         break;
1086
1087     case OB_CLIENT_TYPE_DIALOG:
1088     case OB_CLIENT_TYPE_UTILITY:
1089         /* these windows cannot be maximized */
1090         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1091         break;
1092
1093     case OB_CLIENT_TYPE_MENU:
1094     case OB_CLIENT_TYPE_TOOLBAR:
1095         /* these windows get less functionality */
1096         self->functions &= ~(OB_CLIENT_FUNC_ICONIFY | OB_CLIENT_FUNC_RESIZE);
1097         break;
1098
1099     case OB_CLIENT_TYPE_DESKTOP:
1100     case OB_CLIENT_TYPE_DOCK:
1101     case OB_CLIENT_TYPE_SPLASH:
1102         /* none of these windows are manipulated by the window manager */
1103         self->decorations = 0;
1104         self->functions = 0;
1105         break;
1106     }
1107
1108     /* Mwm Hints are applied subtractively to what has already been chosen for
1109        decor and functionality */
1110     if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
1111         if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
1112             if (! ((self->mwmhints.decorations & OB_MWM_DECOR_HANDLE) ||
1113                    (self->mwmhints.decorations & OB_MWM_DECOR_TITLE)))
1114                 /* if the mwm hints request no handle or title, then all
1115                    decorations are disabled */
1116                 self->decorations = 0;
1117         }
1118     }
1119
1120     if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
1121         if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
1122             if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
1123                 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1124             if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
1125                 self->functions &= ~OB_CLIENT_FUNC_MOVE;
1126             /* dont let mwm hints kill any buttons
1127             if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
1128                 self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
1129             if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
1130                 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1131             */
1132             /* dont let mwm hints kill the close button
1133                if (! (self->mwmhints.functions & MwmFunc_Close))
1134                self->functions &= ~OB_CLIENT_FUNC_CLOSE; */
1135         }
1136     }
1137
1138     if (!(self->functions & OB_CLIENT_FUNC_SHADE))
1139         self->decorations &= ~OB_FRAME_DECOR_SHADE;
1140     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY))
1141         self->decorations &= ~OB_FRAME_DECOR_ICONIFY;
1142     if (!(self->functions & OB_CLIENT_FUNC_RESIZE))
1143         self->decorations &= ~OB_FRAME_DECOR_GRIPS;
1144
1145     /* can't maximize without moving/resizing */
1146     if (!((self->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
1147           (self->functions & OB_CLIENT_FUNC_MOVE) &&
1148           (self->functions & OB_CLIENT_FUNC_RESIZE))) {
1149         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1150         self->decorations &= ~OB_FRAME_DECOR_MAXIMIZE;
1151     }
1152
1153     /* kill the handle on fully maxed windows */
1154     if (self->max_vert && self->max_horz)
1155         self->decorations &= ~OB_FRAME_DECOR_HANDLE;
1156
1157     /* finally, the user can have requested no decorations, which overrides
1158        everything */
1159     if (self->undecorated)
1160         self->decorations = OB_FRAME_DECOR_BORDER;
1161
1162     /* if we don't have a titlebar, then we cannot shade! */
1163     if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1164         self->functions &= ~OB_CLIENT_FUNC_SHADE;
1165
1166     /* now we need to check against rules for the client's current state */
1167     if (self->fullscreen) {
1168         self->functions &= (OB_CLIENT_FUNC_CLOSE |
1169                             OB_CLIENT_FUNC_FULLSCREEN |
1170                             OB_CLIENT_FUNC_ICONIFY);
1171         self->decorations = 0;
1172     }
1173
1174     client_change_allowed_actions(self);
1175
1176     if (self->frame) {
1177         /* adjust the client's decorations, etc. */
1178         client_reconfigure(self);
1179     } else {
1180         /* this makes sure that these windows appear on all desktops */
1181         if (self->type == OB_CLIENT_TYPE_DESKTOP &&
1182             self->desktop != DESKTOP_ALL)
1183         {
1184             self->desktop = DESKTOP_ALL;
1185         }
1186     }
1187 }
1188
1189 static void client_change_allowed_actions(ObClient *self)
1190 {
1191     guint32 actions[9];
1192     int num = 0;
1193
1194     /* desktop windows are kept on all desktops */
1195     if (self->type != OB_CLIENT_TYPE_DESKTOP)
1196         actions[num++] = prop_atoms.net_wm_action_change_desktop;
1197
1198     if (self->functions & OB_CLIENT_FUNC_SHADE)
1199         actions[num++] = prop_atoms.net_wm_action_shade;
1200     if (self->functions & OB_CLIENT_FUNC_CLOSE)
1201         actions[num++] = prop_atoms.net_wm_action_close;
1202     if (self->functions & OB_CLIENT_FUNC_MOVE)
1203         actions[num++] = prop_atoms.net_wm_action_move;
1204     if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1205         actions[num++] = prop_atoms.net_wm_action_minimize;
1206     if (self->functions & OB_CLIENT_FUNC_RESIZE)
1207         actions[num++] = prop_atoms.net_wm_action_resize;
1208     if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1209         actions[num++] = prop_atoms.net_wm_action_fullscreen;
1210     if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1211         actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1212         actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1213     }
1214
1215     PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1216
1217     /* make sure the window isn't breaking any rules now */
1218
1219     if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1220         if (self->frame) client_shade(self, FALSE);
1221         else self->shaded = FALSE;
1222     }
1223     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY) && self->iconic) {
1224         if (self->frame) client_iconify(self, FALSE, TRUE);
1225         else self->iconic = FALSE;
1226     }
1227     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1228         if (self->frame) client_fullscreen(self, FALSE, TRUE);
1229         else self->fullscreen = FALSE;
1230     }
1231     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
1232                                                          self->max_vert)) {
1233         if (self->frame) client_maximize(self, FALSE, 0, TRUE);
1234         else self->max_vert = self->max_horz = FALSE;
1235     }
1236 }
1237
1238 void client_reconfigure(ObClient *self)
1239 {
1240     /* by making this pass FALSE for user, we avoid the emacs event storm where
1241        every configurenotify causes an update in its normal hints, i think this
1242        is generally what we want anyways... */
1243     client_configure(self, OB_CORNER_TOPLEFT, self->area.x, self->area.y,
1244                      self->area.width, self->area.height, FALSE, TRUE);
1245 }
1246
1247 void client_update_wmhints(ObClient *self)
1248 {
1249     XWMHints *hints;
1250     gboolean ur = FALSE;
1251     GSList *it;
1252
1253     /* assume a window takes input if it doesnt specify */
1254     self->can_focus = TRUE;
1255   
1256     if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1257         if (hints->flags & InputHint)
1258             self->can_focus = hints->input;
1259
1260         /* only do this when first managing the window *AND* when we aren't
1261            starting up! */
1262         if (ob_state() != OB_STATE_STARTING && self->frame == NULL)
1263             if (hints->flags & StateHint)
1264                 self->iconic = hints->initial_state == IconicState;
1265
1266         if (hints->flags & XUrgencyHint)
1267             ur = TRUE;
1268
1269         if (!(hints->flags & WindowGroupHint))
1270             hints->window_group = None;
1271
1272         /* did the group state change? */
1273         if (hints->window_group !=
1274             (self->group ? self->group->leader : None)) {
1275             /* remove from the old group if there was one */
1276             if (self->group != NULL) {
1277                 /* remove transients of the group */
1278                 for (it = self->group->members; it; it = it->next)
1279                     self->transients = g_slist_remove(self->transients,
1280                                                       it->data);
1281                 group_remove(self->group, self);
1282                 self->group = NULL;
1283             }
1284             if (hints->window_group != None) {
1285                 self->group = group_add(hints->window_group, self);
1286
1287                 /* i can only have transients from the group if i am not
1288                    transient myself */
1289                 if (!self->transient_for) {
1290                     /* add other transients of the group that are already
1291                        set up */
1292                     for (it = self->group->members; it; it = it->next) {
1293                         ObClient *c = it->data;
1294                         if (c != self && c->transient_for == OB_TRAN_GROUP)
1295                             self->transients =
1296                                 g_slist_append(self->transients, c);
1297                     }
1298                 }
1299             }
1300
1301             /* because the self->transient flag wont change from this call,
1302                we don't need to update the window's type and such, only its
1303                transient_for, and the transients lists of other windows in
1304                the group may be affected */
1305             client_update_transient_for(self);
1306         }
1307
1308         /* the WM_HINTS can contain an icon */
1309         client_update_icons(self);
1310
1311         XFree(hints);
1312     }
1313
1314     if (ur != self->urgent) {
1315         self->urgent = ur;
1316         /* fire the urgent callback if we're mapped, otherwise, wait until
1317            after we're mapped */
1318         if (self->frame)
1319             client_urgent_notify(self);
1320     }
1321 }
1322
1323 void client_update_title(ObClient *self)
1324 {
1325     GList *it;
1326     guint32 nums;
1327     guint i;
1328     gchar *data = NULL;
1329     gboolean read_title;
1330     gchar *old_title;
1331
1332     old_title = self->title;
1333      
1334     /* try netwm */
1335     if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1336         /* try old x stuff */
1337         if (!PROP_GETS(self->window, wm_name, locale, &data))
1338             data = g_strdup("Unnamed Window");
1339
1340     /* did the title change? then reset the title_count */
1341     if (old_title && 0 != strncmp(old_title, data, strlen(data)))
1342         self->title_count = 1;
1343
1344     /* look for duplicates and append a number */
1345     nums = 0;
1346     for (it = client_list; it; it = it->next)
1347         if (it->data != self) {
1348             ObClient *c = it->data;
1349             if (0 == strncmp(c->title, data, strlen(data)))
1350                 nums |= 1 << c->title_count;
1351         }
1352     /* find first free number */
1353     for (i = 1; i <= 32; ++i)
1354         if (!(nums & (1 << i))) {
1355             if (self->title_count == 1 || i == 1)
1356                 self->title_count = i;
1357             break;
1358         }
1359     /* dont display the number for the first window */
1360     if (self->title_count > 1) {
1361         char *ndata;
1362         ndata = g_strdup_printf("%s - [%u]", data, self->title_count);
1363         g_free(data);
1364         data = ndata;
1365     }
1366
1367     PROP_SETS(self->window, net_wm_visible_name, data);
1368
1369     self->title = data;
1370
1371     if (self->frame)
1372         frame_adjust_title(self->frame);
1373
1374     g_free(old_title);
1375
1376     /* update the icon title */
1377     data = NULL;
1378     g_free(self->icon_title);
1379
1380     read_title = TRUE;
1381     /* try netwm */
1382     if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1383         /* try old x stuff */
1384         if (!PROP_GETS(self->window, wm_icon_name, locale, &data)) {
1385             data = g_strdup(self->title);
1386             read_title = FALSE;
1387         }
1388
1389     /* append the title count, dont display the number for the first window */
1390     if (read_title && self->title_count > 1) {
1391         char *vdata, *ndata;
1392         ndata = g_strdup_printf(" - [%u]", self->title_count);
1393         vdata = g_strconcat(data, ndata, NULL);
1394         g_free(ndata);
1395         g_free(data);
1396         data = vdata;
1397     }
1398
1399     PROP_SETS(self->window, net_wm_visible_icon_name, data);
1400
1401     self->icon_title = data;
1402 }
1403
1404 void client_update_class(ObClient *self)
1405 {
1406     char **data;
1407     char *s;
1408
1409     if (self->name) g_free(self->name);
1410     if (self->class) g_free(self->class);
1411     if (self->role) g_free(self->role);
1412
1413     self->name = self->class = self->role = NULL;
1414
1415     if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1416         if (data[0]) {
1417             self->name = g_strdup(data[0]);
1418             if (data[1])
1419                 self->class = g_strdup(data[1]);
1420         }
1421         g_strfreev(data);     
1422     }
1423
1424     if (PROP_GETS(self->window, wm_window_role, locale, &s))
1425         self->role = s;
1426
1427     if (self->name == NULL) self->name = g_strdup("");
1428     if (self->class == NULL) self->class = g_strdup("");
1429     if (self->role == NULL) self->role = g_strdup("");
1430 }
1431
1432 void client_update_strut(ObClient *self)
1433 {
1434     guint num;
1435     guint32 *data;
1436     gboolean got = FALSE;
1437     StrutPartial strut;
1438
1439     if (PROP_GETA32(self->window, net_wm_strut_partial, cardinal,
1440                     &data, &num)) {
1441         if (num == 12) {
1442             got = TRUE;
1443             STRUT_PARTIAL_SET(strut,
1444                               data[0], data[2], data[1], data[3],
1445                               data[4], data[5], data[8], data[9],
1446                               data[6], data[7], data[10], data[11]);
1447         }
1448         g_free(data);
1449     }
1450
1451     if (!got &&
1452         PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1453         if (num == 4) {
1454             got = TRUE;
1455             STRUT_PARTIAL_SET(strut,
1456                               data[0], data[2], data[1], data[3],
1457                               0, 0, 0, 0, 0, 0, 0, 0);
1458         }
1459         g_free(data);
1460     }
1461
1462     if (!got)
1463         STRUT_PARTIAL_SET(strut, 0, 0, 0, 0,
1464                           0, 0, 0, 0, 0, 0, 0, 0);
1465
1466     if (!STRUT_EQUAL(strut, self->strut)) {
1467         self->strut = strut;
1468
1469         /* updating here is pointless while we're being mapped cuz we're not in
1470            the client list yet */
1471         if (self->frame)
1472             screen_update_areas();
1473     }
1474 }
1475
1476 void client_update_icons(ObClient *self)
1477 {
1478     guint num;
1479     guint32 *data;
1480     guint w, h, i, j;
1481
1482     for (i = 0; i < self->nicons; ++i)
1483         g_free(self->icons[i].data);
1484     if (self->nicons > 0)
1485         g_free(self->icons);
1486     self->nicons = 0;
1487
1488     if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1489         /* figure out how many valid icons are in here */
1490         i = 0;
1491         while (num - i > 2) {
1492             w = data[i++];
1493             h = data[i++];
1494             i += w * h;
1495             if (i > num || w*h == 0) break;
1496             ++self->nicons;
1497         }
1498
1499         self->icons = g_new(ObClientIcon, self->nicons);
1500     
1501         /* store the icons */
1502         i = 0;
1503         for (j = 0; j < self->nicons; ++j) {
1504             guint x, y, t;
1505
1506             w = self->icons[j].width = data[i++];
1507             h = self->icons[j].height = data[i++];
1508
1509             if (w*h == 0) continue;
1510
1511             self->icons[j].data = g_new(RrPixel32, w * h);
1512             for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1513                 if (x >= w) {
1514                     x = 0;
1515                     ++y;
1516                 }
1517                 self->icons[j].data[t] =
1518                     (((data[i] >> 24) & 0xff) << RrDefaultAlphaOffset) +
1519                     (((data[i] >> 16) & 0xff) << RrDefaultRedOffset) +
1520                     (((data[i] >> 8) & 0xff) << RrDefaultGreenOffset) +
1521                     (((data[i] >> 0) & 0xff) << RrDefaultBlueOffset);
1522             }
1523             g_assert(i <= num);
1524         }
1525
1526         g_free(data);
1527     } else if (PROP_GETA32(self->window, kwm_win_icon,
1528                            kwm_win_icon, &data, &num)) {
1529         if (num == 2) {
1530             self->nicons++;
1531             self->icons = g_new(ObClientIcon, self->nicons);
1532             xerror_set_ignore(TRUE);
1533             if (!RrPixmapToRGBA(ob_rr_inst,
1534                                 data[0], data[1],
1535                                 &self->icons[self->nicons-1].width,
1536                                 &self->icons[self->nicons-1].height,
1537                                 &self->icons[self->nicons-1].data)) {
1538                 g_free(&self->icons[self->nicons-1]);
1539                 self->nicons--;
1540             }
1541             xerror_set_ignore(FALSE);
1542         }
1543         g_free(data);
1544     } else {
1545         XWMHints *hints;
1546
1547         if ((hints = XGetWMHints(ob_display, self->window))) {
1548             if (hints->flags & IconPixmapHint) {
1549                 self->nicons++;
1550                 self->icons = g_new(ObClientIcon, self->nicons);
1551                 xerror_set_ignore(TRUE);
1552                 if (!RrPixmapToRGBA(ob_rr_inst,
1553                                     hints->icon_pixmap,
1554                                     (hints->flags & IconMaskHint ?
1555                                      hints->icon_mask : None),
1556                                     &self->icons[self->nicons-1].width,
1557                                     &self->icons[self->nicons-1].height,
1558                                     &self->icons[self->nicons-1].data)){
1559                     g_free(&self->icons[self->nicons-1]);
1560                     self->nicons--;
1561                 }
1562                 xerror_set_ignore(FALSE);
1563             }
1564             XFree(hints);
1565         }
1566     }
1567
1568     if (!self->nicons) {
1569         self->nicons++;
1570         self->icons = g_new(ObClientIcon, self->nicons);
1571         self->icons[self->nicons-1].width = 48;
1572         self->icons[self->nicons-1].height = 48;
1573         self->icons[self->nicons-1].data = g_memdup(ob_rr_theme->def_win_icon,
1574                                                     sizeof(RrPixel32)
1575                                                     * 48 * 48);
1576     }
1577
1578     if (self->frame)
1579         frame_adjust_icon(self->frame);
1580 }
1581
1582 static void client_change_state(ObClient *self)
1583 {
1584     guint32 state[2];
1585     guint32 netstate[11];
1586     guint num;
1587
1588     state[0] = self->wmstate;
1589     state[1] = None;
1590     PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1591
1592     num = 0;
1593     if (self->modal)
1594         netstate[num++] = prop_atoms.net_wm_state_modal;
1595     if (self->shaded)
1596         netstate[num++] = prop_atoms.net_wm_state_shaded;
1597     if (self->iconic)
1598         netstate[num++] = prop_atoms.net_wm_state_hidden;
1599     if (self->skip_taskbar)
1600         netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1601     if (self->skip_pager)
1602         netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1603     if (self->fullscreen)
1604         netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1605     if (self->max_vert)
1606         netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1607     if (self->max_horz)
1608         netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1609     if (self->above)
1610         netstate[num++] = prop_atoms.net_wm_state_above;
1611     if (self->below)
1612         netstate[num++] = prop_atoms.net_wm_state_below;
1613     if (self->undecorated)
1614         netstate[num++] = prop_atoms.ob_wm_state_undecorated;
1615     PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1616
1617     client_calc_layer(self);
1618
1619     if (self->frame)
1620         frame_adjust_state(self->frame);
1621 }
1622
1623 ObClient *client_search_focus_tree(ObClient *self)
1624 {
1625     GSList *it;
1626     ObClient *ret;
1627
1628     for (it = self->transients; it != NULL; it = it->next) {
1629         if (client_focused(it->data)) return it->data;
1630         if ((ret = client_search_focus_tree(it->data))) return ret;
1631     }
1632     return NULL;
1633 }
1634
1635 ObClient *client_search_focus_tree_full(ObClient *self)
1636 {
1637     if (self->transient_for) {
1638         if (self->transient_for != OB_TRAN_GROUP) {
1639             return client_search_focus_tree_full(self->transient_for);
1640         } else {
1641             GSList *it;
1642             gboolean recursed = FALSE;
1643         
1644             for (it = self->group->members; it; it = it->next)
1645                 if (!((ObClient*)it->data)->transient_for) {
1646                     ObClient *c;
1647                     if ((c = client_search_focus_tree_full(it->data)))
1648                         return c;
1649                     recursed = TRUE;
1650                 }
1651             if (recursed)
1652               return NULL;
1653         }
1654     }
1655
1656     /* this function checks the whole tree, the client_search_focus_tree~
1657        does not, so we need to check this window */
1658     if (client_focused(self))
1659       return self;
1660     return client_search_focus_tree(self);
1661 }
1662
1663 static ObStackingLayer calc_layer(ObClient *self)
1664 {
1665     ObStackingLayer l;
1666
1667     if (self->fullscreen) l = OB_STACKING_LAYER_FULLSCREEN;
1668     else if (self->type == OB_CLIENT_TYPE_DESKTOP)
1669         l = OB_STACKING_LAYER_DESKTOP;
1670     else if (self->type == OB_CLIENT_TYPE_DOCK) {
1671         if (!self->below) l = OB_STACKING_LAYER_TOP;
1672         else l = OB_STACKING_LAYER_NORMAL;
1673     }
1674     else if (self->above) l = OB_STACKING_LAYER_ABOVE;
1675     else if (self->below) l = OB_STACKING_LAYER_BELOW;
1676     else l = OB_STACKING_LAYER_NORMAL;
1677
1678     return l;
1679 }
1680
1681 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
1682                                         ObStackingLayer l, gboolean raised)
1683 {
1684     ObStackingLayer old, own;
1685     GSList *it;
1686
1687     old = self->layer;
1688     own = calc_layer(self);
1689     self->layer = l > own ? l : own;
1690
1691     for (it = self->transients; it; it = it->next)
1692         client_calc_layer_recursive(it->data, orig,
1693                                     l, raised ? raised : l != old);
1694
1695     if (!raised && l != old)
1696         if (orig->frame) { /* only restack if the original window is managed */
1697             /* XXX add_non_intrusive ever? */
1698             stacking_remove(CLIENT_AS_WINDOW(self));
1699             stacking_add(CLIENT_AS_WINDOW(self));
1700         }
1701 }
1702
1703 void client_calc_layer(ObClient *self)
1704 {
1705     ObStackingLayer l;
1706     ObClient *orig;
1707
1708     orig = self;
1709
1710     /* transients take on the layer of their parents */
1711     self = client_search_top_transient(self);
1712
1713     l = calc_layer(self);
1714
1715     client_calc_layer_recursive(self, orig, l, FALSE);
1716 }
1717
1718 gboolean client_should_show(ObClient *self)
1719 {
1720     if (self->iconic) return FALSE;
1721     else if (!(self->desktop == screen_desktop ||
1722                self->desktop == DESKTOP_ALL)) return FALSE;
1723     else if (client_normal(self) && screen_showing_desktop) return FALSE;
1724     
1725     return TRUE;
1726 }
1727
1728 static void client_showhide(ObClient *self)
1729 {
1730
1731     if (client_should_show(self))
1732         frame_show(self->frame);
1733     else
1734         frame_hide(self->frame);
1735 }
1736
1737 gboolean client_normal(ObClient *self) {
1738     return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
1739               self->type == OB_CLIENT_TYPE_DOCK ||
1740               self->type == OB_CLIENT_TYPE_SPLASH);
1741 }
1742
1743 static void client_apply_startup_state(ObClient *self)
1744 {
1745     /* these are in a carefully crafted order.. */
1746
1747     if (self->iconic) {
1748         self->iconic = FALSE;
1749         client_iconify(self, TRUE, FALSE);
1750     }
1751     if (self->fullscreen) {
1752         self->fullscreen = FALSE;
1753         client_fullscreen(self, TRUE, FALSE);
1754     }
1755     if (self->undecorated) {
1756         self->undecorated = FALSE;
1757         client_set_undecorated(self, TRUE);
1758     }
1759     if (self->shaded) {
1760         self->shaded = FALSE;
1761         client_shade(self, TRUE);
1762     }
1763     if (self->urgent)
1764         client_urgent_notify(self);
1765   
1766     if (self->max_vert && self->max_horz) {
1767         self->max_vert = self->max_horz = FALSE;
1768         client_maximize(self, TRUE, 0, FALSE);
1769     } else if (self->max_vert) {
1770         self->max_vert = FALSE;
1771         client_maximize(self, TRUE, 2, FALSE);
1772     } else if (self->max_horz) {
1773         self->max_horz = FALSE;
1774         client_maximize(self, TRUE, 1, FALSE);
1775     }
1776
1777     /* nothing to do for the other states:
1778        skip_taskbar
1779        skip_pager
1780        modal
1781        above
1782        below
1783     */
1784 }
1785
1786 void client_configure_full(ObClient *self, ObCorner anchor,
1787                            int x, int y, int w, int h,
1788                            gboolean user, gboolean final,
1789                            gboolean force_reply)
1790 {
1791     gint oldw, oldh;
1792     gboolean send_resize_client;
1793     gboolean moved = FALSE, resized = FALSE;
1794     guint fdecor = self->frame->decorations;
1795     gboolean fhorz = self->frame->max_horz;
1796
1797     /* make the frame recalculate its dimentions n shit without changing
1798        anything visible for real, this way the constraints below can work with
1799        the updated frame dimensions. */
1800     frame_adjust_area(self->frame, TRUE, TRUE, TRUE);
1801
1802     /* gets the frame's position */
1803     frame_client_gravity(self->frame, &x, &y);
1804
1805     /* these positions are frame positions, not client positions */
1806
1807     /* set the size and position if fullscreen */
1808     if (self->fullscreen) {
1809 #ifdef VIDMODE
1810         int dot;
1811         XF86VidModeModeLine mode;
1812 #endif
1813         Rect *a;
1814         guint i;
1815
1816         i = client_monitor(self);
1817         a = screen_physical_area_monitor(i);
1818
1819 #ifdef VIDMODE
1820         if (i == 0 && /* primary head */
1821             extensions_vidmode &&
1822             XF86VidModeGetViewPort(ob_display, ob_screen, &x, &y) &&
1823             /* get the mode last so the mode.privsize isnt freed incorrectly */
1824             XF86VidModeGetModeLine(ob_display, ob_screen, &dot, &mode)) {
1825             x += a->x;
1826             y += a->y;
1827             w = mode.hdisplay;
1828             h = mode.vdisplay;
1829             if (mode.privsize) XFree(mode.private);
1830         } else
1831 #endif
1832         {
1833             x = a->x;
1834             y = a->y;
1835             w = a->width;
1836             h = a->height;
1837         }
1838
1839         user = FALSE; /* ignore that increment etc shit when in fullscreen */
1840     } else {
1841         Rect *a;
1842
1843         a = screen_area_monitor(self->desktop, client_monitor(self));
1844
1845         /* set the size and position if maximized */
1846         if (self->max_horz) {
1847             x = a->x;
1848             w = a->width - self->frame->size.left - self->frame->size.right;
1849         }
1850         if (self->max_vert) {
1851             y = a->y;
1852             h = a->height - self->frame->size.top - self->frame->size.bottom;
1853         }
1854     }
1855
1856     /* gets the client's position */
1857     frame_frame_gravity(self->frame, &x, &y);
1858
1859     /* these override the above states! if you cant move you can't move! */
1860     if (user) {
1861         if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
1862             x = self->area.x;
1863             y = self->area.y;
1864         }
1865         if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
1866             w = self->area.width;
1867             h = self->area.height;
1868         }
1869     }
1870
1871     if (!(w == self->area.width && h == self->area.height)) {
1872         int basew, baseh, minw, minh;
1873
1874         /* base size is substituted with min size if not specified */
1875         if (self->base_size.width || self->base_size.height) {
1876             basew = self->base_size.width;
1877             baseh = self->base_size.height;
1878         } else {
1879             basew = self->min_size.width;
1880             baseh = self->min_size.height;
1881         }
1882         /* min size is substituted with base size if not specified */
1883         if (self->min_size.width || self->min_size.height) {
1884             minw = self->min_size.width;
1885             minh = self->min_size.height;
1886         } else {
1887             minw = self->base_size.width;
1888             minh = self->base_size.height;
1889         }
1890
1891         /* if this is a user-requested resize, then check against min/max
1892            sizes */
1893
1894         /* smaller than min size or bigger than max size? */
1895         if (w > self->max_size.width) w = self->max_size.width;
1896         if (w < minw) w = minw;
1897         if (h > self->max_size.height) h = self->max_size.height;
1898         if (h < minh) h = minh;
1899
1900         w -= basew;
1901         h -= baseh;
1902
1903         /* keep to the increments */
1904         w /= self->size_inc.width;
1905         h /= self->size_inc.height;
1906
1907         /* you cannot resize to nothing */
1908         if (basew + w < 1) w = 1 - basew;
1909         if (baseh + h < 1) h = 1 - baseh;
1910   
1911         /* store the logical size */
1912         SIZE_SET(self->logical_size,
1913                  self->size_inc.width > 1 ? w : w + basew,
1914                  self->size_inc.height > 1 ? h : h + baseh);
1915
1916         w *= self->size_inc.width;
1917         h *= self->size_inc.height;
1918
1919         w += basew;
1920         h += baseh;
1921
1922         /* adjust the height to match the width for the aspect ratios.
1923            for this, min size is not substituted for base size ever. */
1924         w -= self->base_size.width;
1925         h -= self->base_size.height;
1926
1927         if (self->min_ratio)
1928             if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1929         if (self->max_ratio)
1930             if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1931
1932         w += self->base_size.width;
1933         h += self->base_size.height;
1934     }
1935
1936     switch (anchor) {
1937     case OB_CORNER_TOPLEFT:
1938         break;
1939     case OB_CORNER_TOPRIGHT:
1940         x -= w - self->area.width;
1941         break;
1942     case OB_CORNER_BOTTOMLEFT:
1943         y -= h - self->area.height;
1944         break;
1945     case OB_CORNER_BOTTOMRIGHT:
1946         x -= w - self->area.width;
1947         y -= h - self->area.height;
1948         break;
1949     }
1950
1951     moved = x != self->area.x || y != self->area.y;
1952     resized = w != self->area.width || h != self->area.height;
1953
1954     oldw = self->area.width;
1955     oldh = self->area.height;
1956     RECT_SET(self->area, x, y, w, h);
1957
1958     /* for app-requested resizes, always resize if 'resized' is true.
1959        for user-requested ones, only resize if final is true, or when
1960        resizing in redraw mode */
1961     send_resize_client = ((!user && resized) ||
1962                           (user && (final ||
1963                                     (resized && config_redraw_resize))));
1964
1965     /* if the client is enlarging, the resize the client before the frame */
1966     if (send_resize_client && user && (w > oldw || h > oldh))
1967         XResizeWindow(ob_display, self->window, MAX(w, oldw), MAX(h, oldh));
1968
1969     /* move/resize the frame to match the request */
1970     if (self->frame) {
1971         if (self->decorations != fdecor || self->max_horz != fhorz)
1972             moved = resized = TRUE;
1973
1974         if (moved || resized)
1975             frame_adjust_area(self->frame, moved, resized, FALSE);
1976
1977         if (!resized && (force_reply || ((!user && moved) || (user && final))))
1978         {
1979             XEvent event;
1980             event.type = ConfigureNotify;
1981             event.xconfigure.display = ob_display;
1982             event.xconfigure.event = self->window;
1983             event.xconfigure.window = self->window;
1984
1985             /* root window real coords */
1986             event.xconfigure.x = self->frame->area.x + self->frame->size.left -
1987                 self->border_width;
1988             event.xconfigure.y = self->frame->area.y + self->frame->size.top -
1989                 self->border_width;
1990             event.xconfigure.width = w;
1991             event.xconfigure.height = h;
1992             event.xconfigure.border_width = 0;
1993             event.xconfigure.above = self->frame->plate;
1994             event.xconfigure.override_redirect = FALSE;
1995             XSendEvent(event.xconfigure.display, event.xconfigure.window,
1996                        FALSE, StructureNotifyMask, &event);
1997         }
1998     }
1999
2000     /* if the client is shrinking, then resize the frame before the client */
2001     if (send_resize_client && (!user || (w <= oldw || h <= oldh)))
2002         XResizeWindow(ob_display, self->window, w, h);
2003
2004     XFlush(ob_display);
2005 }
2006
2007 void client_fullscreen(ObClient *self, gboolean fs, gboolean savearea)
2008 {
2009     int x, y, w, h;
2010
2011     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
2012         self->fullscreen == fs) return;                   /* already done */
2013
2014     self->fullscreen = fs;
2015     client_change_state(self); /* change the state hints on the client,
2016                                   and adjust out layer/stacking */
2017
2018     if (fs) {
2019         if (savearea)
2020             self->pre_fullscreen_area = self->area;
2021
2022         /* these are not actually used cuz client_configure will set them
2023            as appropriate when the window is fullscreened */
2024         x = y = w = h = 0;
2025     } else {
2026         Rect *a;
2027
2028         if (self->pre_fullscreen_area.width > 0 &&
2029             self->pre_fullscreen_area.height > 0)
2030         {
2031             x = self->pre_fullscreen_area.x;
2032             y = self->pre_fullscreen_area.y;
2033             w = self->pre_fullscreen_area.width;
2034             h = self->pre_fullscreen_area.height;
2035             RECT_SET(self->pre_fullscreen_area, 0, 0, 0, 0);
2036         } else {
2037             /* pick some fallbacks... */
2038             a = screen_area_monitor(self->desktop, 0);
2039             x = a->x + a->width / 4;
2040             y = a->y + a->height / 4;
2041             w = a->width / 2;
2042             h = a->height / 2;
2043         }
2044     }
2045
2046     client_setup_decor_and_functions(self);
2047
2048     client_move_resize(self, x, y, w, h);
2049
2050     /* try focus us when we go into fullscreen mode */
2051     client_focus(self);
2052 }
2053
2054 static void client_iconify_recursive(ObClient *self,
2055                                      gboolean iconic, gboolean curdesk)
2056 {
2057     GSList *it;
2058     gboolean changed = FALSE;
2059
2060
2061     if (self->iconic != iconic) {
2062         ob_debug("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"),
2063                  self->window);
2064
2065         self->iconic = iconic;
2066
2067         if (iconic) {
2068             if (self->functions & OB_CLIENT_FUNC_ICONIFY) {
2069                 long old;
2070
2071                 old = self->wmstate;
2072                 self->wmstate = IconicState;
2073                 if (old != self->wmstate)
2074                     PROP_MSG(self->window, kde_wm_change_state,
2075                              self->wmstate, 1, 0, 0);
2076
2077                 self->ignore_unmaps++;
2078                 /* we unmap the client itself so that we can get MapRequest
2079                    events, and because the ICCCM tells us to! */
2080                 XUnmapWindow(ob_display, self->window);
2081
2082                 /* update the focus lists.. iconic windows go to the bottom of
2083                    the list, put the new iconic window at the 'top of the
2084                    bottom'. */
2085                 focus_order_to_top(self);
2086
2087                 changed = TRUE;
2088             }
2089         } else {
2090             long old;
2091
2092             if (curdesk)
2093                 client_set_desktop(self, screen_desktop, FALSE);
2094
2095             old = self->wmstate;
2096             self->wmstate = self->shaded ? IconicState : NormalState;
2097             if (old != self->wmstate)
2098                 PROP_MSG(self->window, kde_wm_change_state,
2099                          self->wmstate, 1, 0, 0);
2100
2101             XMapWindow(ob_display, self->window);
2102
2103             /* this puts it after the current focused window */
2104             focus_order_remove(self);
2105             focus_order_add_new(self);
2106
2107             /* this is here cuz with the VIDMODE extension, the viewport can
2108                change while a fullscreen window is iconic, and when it
2109                uniconifies, it would be nice if it did so to the new position
2110                of the viewport */
2111             client_reconfigure(self);
2112
2113             changed = TRUE;
2114         }
2115     }
2116
2117     if (changed) {
2118         client_change_state(self);
2119         client_showhide(self);
2120         screen_update_areas();
2121     }
2122
2123     /* iconify all transients */
2124     for (it = self->transients; it != NULL; it = it->next)
2125         if (it->data != self) client_iconify_recursive(it->data,
2126                                                        iconic, curdesk);
2127 }
2128
2129 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk)
2130 {
2131     /* move up the transient chain as far as possible first */
2132     self = client_search_top_transient(self);
2133
2134     client_iconify_recursive(client_search_top_transient(self),
2135                              iconic, curdesk);
2136 }
2137
2138 void client_maximize(ObClient *self, gboolean max, int dir, gboolean savearea)
2139 {
2140     int x, y, w, h;
2141      
2142     g_assert(dir == 0 || dir == 1 || dir == 2);
2143     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE)) return; /* can't */
2144
2145     /* check if already done */
2146     if (max) {
2147         if (dir == 0 && self->max_horz && self->max_vert) return;
2148         if (dir == 1 && self->max_horz) return;
2149         if (dir == 2 && self->max_vert) return;
2150     } else {
2151         if (dir == 0 && !self->max_horz && !self->max_vert) return;
2152         if (dir == 1 && !self->max_horz) return;
2153         if (dir == 2 && !self->max_vert) return;
2154     }
2155
2156     /* we just tell it to configure in the same place and client_configure
2157        worries about filling the screen with the window */
2158     x = self->area.x;
2159     y = self->area.y;
2160     w = self->area.width;
2161     h = self->area.height;
2162
2163     if (max) {
2164         if (savearea)
2165             self->pre_max_area = self->area;
2166     } else {
2167         Rect *a;
2168
2169         a = screen_area_monitor(self->desktop, 0);
2170         if ((dir == 0 || dir == 1) && self->max_horz) { /* horz */
2171             if (self->pre_max_area.width > 0) {
2172                 x = self->pre_max_area.x;
2173                 w = self->pre_max_area.width;
2174
2175                 RECT_SET(self->pre_max_area, 0, self->pre_max_area.y,
2176                          0, self->pre_max_area.height);
2177             } else {
2178                 /* pick some fallbacks... */
2179                 x = a->x + a->width / 4;
2180                 w = a->width / 2;
2181             }
2182         }
2183         if ((dir == 0 || dir == 2) && self->max_vert) { /* vert */
2184             if (self->pre_max_area.width > 0) {
2185                 y = self->pre_max_area.y;
2186                 h = self->pre_max_area.height;
2187
2188                 RECT_SET(self->pre_max_area, self->pre_max_area.x, 0,
2189                          self->pre_max_area.width, 0);
2190             } else {
2191                 /* pick some fallbacks... */
2192                 y = a->y + a->height / 4;
2193                 h = a->height / 2;
2194             }
2195         }
2196     }
2197
2198     if (dir == 0 || dir == 1) /* horz */
2199         self->max_horz = max;
2200     if (dir == 0 || dir == 2) /* vert */
2201         self->max_vert = max;
2202
2203     client_change_state(self); /* change the state hints on the client */
2204
2205     client_setup_decor_and_functions(self);
2206
2207     client_move_resize(self, x, y, w, h);
2208 }
2209
2210 void client_shade(ObClient *self, gboolean shade)
2211 {
2212     if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
2213          shade) ||                         /* can't shade */
2214         self->shaded == shade) return;     /* already done */
2215
2216     /* when we're iconic, don't change the wmstate */
2217     if (!self->iconic) {
2218         long old;
2219
2220         old = self->wmstate;
2221         self->wmstate = shade ? IconicState : NormalState;
2222         if (old != self->wmstate)
2223             PROP_MSG(self->window, kde_wm_change_state,
2224                      self->wmstate, 1, 0, 0);
2225     }
2226
2227     self->shaded = shade;
2228     client_change_state(self);
2229     /* resize the frame to just the titlebar */
2230     frame_adjust_area(self->frame, FALSE, FALSE, FALSE);
2231 }
2232
2233 void client_close(ObClient *self)
2234 {
2235     XEvent ce;
2236
2237     if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
2238
2239     /*
2240       XXX: itd be cool to do timeouts and shit here for killing the client's
2241       process off
2242       like... if the window is around after 5 seconds, then the close button
2243       turns a nice red, and if this function is called again, the client is
2244       explicitly killed.
2245     */
2246
2247     ce.xclient.type = ClientMessage;
2248     ce.xclient.message_type =  prop_atoms.wm_protocols;
2249     ce.xclient.display = ob_display;
2250     ce.xclient.window = self->window;
2251     ce.xclient.format = 32;
2252     ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
2253     ce.xclient.data.l[1] = event_lasttime;
2254     ce.xclient.data.l[2] = 0l;
2255     ce.xclient.data.l[3] = 0l;
2256     ce.xclient.data.l[4] = 0l;
2257     XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2258 }
2259
2260 void client_kill(ObClient *self)
2261 {
2262     XKillClient(ob_display, self->window);
2263 }
2264
2265 void client_set_desktop_recursive(ObClient *self,
2266                                   guint target, gboolean donthide)
2267 {
2268     guint old;
2269     GSList *it;
2270
2271     if (target != self->desktop) {
2272
2273         ob_debug("Setting desktop %u\n", target+1);
2274
2275         g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2276
2277         /* remove from the old desktop(s) */
2278         focus_order_remove(self);
2279
2280         old = self->desktop;
2281         self->desktop = target;
2282         PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2283         /* the frame can display the current desktop state */
2284         frame_adjust_state(self->frame);
2285         /* 'move' the window to the new desktop */
2286         if (!donthide)
2287             client_showhide(self);
2288         /* raise if it was not already on the desktop */
2289         if (old != DESKTOP_ALL)
2290             stacking_raise(CLIENT_AS_WINDOW(self));
2291         screen_update_areas();
2292
2293         /* add to the new desktop(s) */
2294         if (config_focus_new)
2295             focus_order_to_top(self);
2296         else
2297             focus_order_to_bottom(self);
2298     }
2299
2300     /* move all transients */
2301     for (it = self->transients; it != NULL; it = it->next)
2302         if (it->data != self) client_set_desktop_recursive(it->data,
2303                                                            target, donthide);
2304 }
2305
2306 void client_set_desktop(ObClient *self, guint target, gboolean donthide)
2307 {
2308     client_set_desktop_recursive(client_search_top_transient(self),
2309                                  target, donthide);
2310 }
2311
2312 ObClient *client_search_modal_child(ObClient *self)
2313 {
2314     GSList *it;
2315     ObClient *ret;
2316   
2317     for (it = self->transients; it != NULL; it = it->next) {
2318         ObClient *c = it->data;
2319         if ((ret = client_search_modal_child(c))) return ret;
2320         if (c->modal) return c;
2321     }
2322     return NULL;
2323 }
2324
2325 gboolean client_validate(ObClient *self)
2326 {
2327     XEvent e; 
2328
2329     XSync(ob_display, FALSE); /* get all events on the server */
2330
2331     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2332         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2333         XPutBackEvent(ob_display, &e);
2334         return FALSE;
2335     }
2336
2337     return TRUE;
2338 }
2339
2340 void client_set_wm_state(ObClient *self, long state)
2341 {
2342     if (state == self->wmstate) return; /* no change */
2343   
2344     switch (state) {
2345     case IconicState:
2346         client_iconify(self, TRUE, TRUE);
2347         break;
2348     case NormalState:
2349         client_iconify(self, FALSE, TRUE);
2350         break;
2351     }
2352 }
2353
2354 void client_set_state(ObClient *self, Atom action, long data1, long data2)
2355 {
2356     gboolean shaded = self->shaded;
2357     gboolean fullscreen = self->fullscreen;
2358     gboolean undecorated = self->undecorated;
2359     gboolean max_horz = self->max_horz;
2360     gboolean max_vert = self->max_vert;
2361     int i;
2362
2363     if (!(action == prop_atoms.net_wm_state_add ||
2364           action == prop_atoms.net_wm_state_remove ||
2365           action == prop_atoms.net_wm_state_toggle))
2366         /* an invalid action was passed to the client message, ignore it */
2367         return; 
2368
2369     for (i = 0; i < 2; ++i) {
2370         Atom state = i == 0 ? data1 : data2;
2371     
2372         if (!state) continue;
2373
2374         /* if toggling, then pick whether we're adding or removing */
2375         if (action == prop_atoms.net_wm_state_toggle) {
2376             if (state == prop_atoms.net_wm_state_modal)
2377                 action = self->modal ? prop_atoms.net_wm_state_remove :
2378                     prop_atoms.net_wm_state_add;
2379             else if (state == prop_atoms.net_wm_state_maximized_vert)
2380                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2381                     prop_atoms.net_wm_state_add;
2382             else if (state == prop_atoms.net_wm_state_maximized_horz)
2383                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2384                     prop_atoms.net_wm_state_add;
2385             else if (state == prop_atoms.net_wm_state_shaded)
2386                 action = shaded ? prop_atoms.net_wm_state_remove :
2387                     prop_atoms.net_wm_state_add;
2388             else if (state == prop_atoms.net_wm_state_skip_taskbar)
2389                 action = self->skip_taskbar ?
2390                     prop_atoms.net_wm_state_remove :
2391                     prop_atoms.net_wm_state_add;
2392             else if (state == prop_atoms.net_wm_state_skip_pager)
2393                 action = self->skip_pager ?
2394                     prop_atoms.net_wm_state_remove :
2395                     prop_atoms.net_wm_state_add;
2396             else if (state == prop_atoms.net_wm_state_fullscreen)
2397                 action = fullscreen ?
2398                     prop_atoms.net_wm_state_remove :
2399                     prop_atoms.net_wm_state_add;
2400             else if (state == prop_atoms.net_wm_state_above)
2401                 action = self->above ? prop_atoms.net_wm_state_remove :
2402                     prop_atoms.net_wm_state_add;
2403             else if (state == prop_atoms.net_wm_state_below)
2404                 action = self->below ? prop_atoms.net_wm_state_remove :
2405                     prop_atoms.net_wm_state_add;
2406             else if (state == prop_atoms.ob_wm_state_undecorated)
2407                 action = undecorated ? prop_atoms.net_wm_state_remove :
2408                     prop_atoms.net_wm_state_add;
2409         }
2410     
2411         if (action == prop_atoms.net_wm_state_add) {
2412             if (state == prop_atoms.net_wm_state_modal) {
2413                 /* XXX raise here or something? */
2414                 self->modal = TRUE;
2415             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2416                 max_vert = TRUE;
2417             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2418                 max_horz = TRUE;
2419             } else if (state == prop_atoms.net_wm_state_shaded) {
2420                 shaded = TRUE;
2421             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2422                 self->skip_taskbar = TRUE;
2423             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2424                 self->skip_pager = TRUE;
2425             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2426                 fullscreen = TRUE;
2427             } else if (state == prop_atoms.net_wm_state_above) {
2428                 self->above = TRUE;
2429             } else if (state == prop_atoms.net_wm_state_below) {
2430                 self->below = TRUE;
2431             } else if (state == prop_atoms.ob_wm_state_undecorated) {
2432                 undecorated = TRUE;
2433             }
2434
2435         } else { /* action == prop_atoms.net_wm_state_remove */
2436             if (state == prop_atoms.net_wm_state_modal) {
2437                 self->modal = FALSE;
2438             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2439                 max_vert = FALSE;
2440             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2441                 max_horz = FALSE;
2442             } else if (state == prop_atoms.net_wm_state_shaded) {
2443                 shaded = FALSE;
2444             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2445                 self->skip_taskbar = FALSE;
2446             } else if (state == prop_atoms.net_wm_state_skip_pager) {
2447                 self->skip_pager = FALSE;
2448             } else if (state == prop_atoms.net_wm_state_fullscreen) {
2449                 fullscreen = FALSE;
2450             } else if (state == prop_atoms.net_wm_state_above) {
2451                 self->above = FALSE;
2452             } else if (state == prop_atoms.net_wm_state_below) {
2453                 self->below = FALSE;
2454             } else if (state == prop_atoms.ob_wm_state_undecorated) {
2455                 undecorated = FALSE;
2456             }
2457         }
2458     }
2459     if (max_horz != self->max_horz || max_vert != self->max_vert) {
2460         if (max_horz != self->max_horz && max_vert != self->max_vert) {
2461             /* toggling both */
2462             if (max_horz == max_vert) { /* both going the same way */
2463                 client_maximize(self, max_horz, 0, TRUE);
2464             } else {
2465                 client_maximize(self, max_horz, 1, TRUE);
2466                 client_maximize(self, max_vert, 2, TRUE);
2467             }
2468         } else {
2469             /* toggling one */
2470             if (max_horz != self->max_horz)
2471                 client_maximize(self, max_horz, 1, TRUE);
2472             else
2473                 client_maximize(self, max_vert, 2, TRUE);
2474         }
2475     }
2476     /* change fullscreen state before shading, as it will affect if the window
2477        can shade or not */
2478     if (fullscreen != self->fullscreen)
2479         client_fullscreen(self, fullscreen, TRUE);
2480     if (shaded != self->shaded)
2481         client_shade(self, shaded);
2482     if (undecorated != self->undecorated)
2483         client_set_undecorated(self, undecorated);
2484     client_calc_layer(self);
2485     client_change_state(self); /* change the hint to reflect these changes */
2486 }
2487
2488 ObClient *client_focus_target(ObClient *self)
2489 {
2490     ObClient *child;
2491      
2492     /* if we have a modal child, then focus it, not us */
2493     child = client_search_modal_child(self);
2494     if (child) return child;
2495     return self;
2496 }
2497
2498 gboolean client_can_focus(ObClient *self)
2499 {
2500     XEvent ev;
2501
2502     /* choose the correct target */
2503     self = client_focus_target(self);
2504
2505     if (!self->frame->visible)
2506         return FALSE;
2507
2508     if (!((self->can_focus || self->focus_notify) &&
2509           (self->desktop == screen_desktop ||
2510            self->desktop == DESKTOP_ALL) &&
2511           !self->iconic))
2512         return FALSE;
2513
2514     /* do a check to see if the window has already been unmapped or destroyed
2515        do this intelligently while watching out for unmaps we've generated
2516        (ignore_unmaps > 0) */
2517     if (XCheckTypedWindowEvent(ob_display, self->window,
2518                                DestroyNotify, &ev)) {
2519         XPutBackEvent(ob_display, &ev);
2520         return FALSE;
2521     }
2522     while (XCheckTypedWindowEvent(ob_display, self->window,
2523                                   UnmapNotify, &ev)) {
2524         if (self->ignore_unmaps) {
2525             self->ignore_unmaps--;
2526         } else {
2527             XPutBackEvent(ob_display, &ev);
2528             return FALSE;
2529         }
2530     }
2531
2532     return TRUE;
2533 }
2534
2535 gboolean client_focus(ObClient *self)
2536 {
2537     /* choose the correct target */
2538     self = client_focus_target(self);
2539
2540     if (!client_can_focus(self)) {
2541         if (!self->frame->visible) {
2542             /* update the focus lists */
2543             focus_order_to_top(self);
2544         }
2545         return FALSE;
2546     }
2547
2548     if (self->can_focus) {
2549         /* RevertToPointerRoot causes much more headache than RevertToNone, so
2550            I choose to use it always, hopefully to find errors quicker, if any
2551            are left. (I hate X. I hate focus events.)
2552            
2553            Update: Changing this to RevertToNone fixed a bug with mozilla (bug
2554            #799. So now it is RevertToNone again.
2555         */
2556         XSetInputFocus(ob_display, self->window, RevertToNone,
2557                        event_lasttime);
2558     }
2559
2560     if (self->focus_notify) {
2561         XEvent ce;
2562         ce.xclient.type = ClientMessage;
2563         ce.xclient.message_type = prop_atoms.wm_protocols;
2564         ce.xclient.display = ob_display;
2565         ce.xclient.window = self->window;
2566         ce.xclient.format = 32;
2567         ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2568         ce.xclient.data.l[1] = event_lasttime;
2569         ce.xclient.data.l[2] = 0l;
2570         ce.xclient.data.l[3] = 0l;
2571         ce.xclient.data.l[4] = 0l;
2572         XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2573     }
2574
2575 #ifdef DEBUG_FOCUS
2576     ob_debug("%sively focusing %lx at %d\n",
2577              (self->can_focus ? "act" : "pass"),
2578              self->window, (int) event_lasttime);
2579 #endif
2580
2581     /* Cause the FocusIn to come back to us. Important for desktop switches,
2582        since otherwise we'll have no FocusIn on the queue and send it off to
2583        the focus_backup. */
2584     XSync(ob_display, FALSE);
2585     return TRUE;
2586 }
2587
2588 void client_unfocus(ObClient *self)
2589 {
2590     if (focus_client == self) {
2591 #ifdef DEBUG_FOCUS
2592         ob_debug("client_unfocus for %lx\n", self->window);
2593 #endif
2594         focus_fallback(OB_FOCUS_FALLBACK_UNFOCUSING);
2595     }
2596 }
2597
2598 void client_activate(ObClient *self, gboolean here)
2599 {
2600     if (client_normal(self) && screen_showing_desktop)
2601         screen_show_desktop(FALSE);
2602     if (self->iconic)
2603         client_iconify(self, FALSE, FALSE);
2604     if (self->desktop != DESKTOP_ALL &&
2605         self->desktop != screen_desktop) {
2606         if (here)
2607             client_set_desktop(self, screen_desktop, FALSE);
2608         else
2609             screen_set_desktop(self->desktop);
2610     } else if (!self->frame->visible)
2611         /* if its not visible for other reasons, then don't mess
2612            with it */
2613         return;
2614     if (self->shaded)
2615         client_shade(self, FALSE);
2616     client_focus(self);
2617     stacking_raise(CLIENT_AS_WINDOW(self));
2618 }
2619
2620 gboolean client_focused(ObClient *self)
2621 {
2622     return self == focus_client;
2623 }
2624
2625 ObClientIcon *client_icon(ObClient *self, int w, int h)
2626 {
2627     guint i;
2628     /* si is the smallest image >= req */
2629     /* li is the largest image < req */
2630     unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2631
2632     for (i = 0; i < self->nicons; ++i) {
2633         size = self->icons[i].width * self->icons[i].height;
2634         if (size < smallest && size >= (unsigned)(w * h)) {
2635             smallest = size;
2636             si = i;
2637         }
2638         if (size > largest && size <= (unsigned)(w * h)) {
2639             largest = size;
2640             li = i;
2641         }
2642     }
2643     if (largest == 0) /* didnt find one smaller than the requested size */
2644         return &self->icons[si];
2645     return &self->icons[li];
2646 }
2647
2648 /* this be mostly ripped from fvwm */
2649 ObClient *client_find_directional(ObClient *c, ObDirection dir) 
2650 {
2651     int my_cx, my_cy, his_cx, his_cy;
2652     int offset = 0;
2653     int distance = 0;
2654     int score, best_score;
2655     ObClient *best_client, *cur;
2656     GList *it;
2657
2658     if(!client_list)
2659         return NULL;
2660
2661     /* first, find the centre coords of the currently focused window */
2662     my_cx = c->frame->area.x + c->frame->area.width / 2;
2663     my_cy = c->frame->area.y + c->frame->area.height / 2;
2664
2665     best_score = -1;
2666     best_client = NULL;
2667
2668     for(it = g_list_first(client_list); it; it = it->next) {
2669         cur = it->data;
2670
2671         /* the currently selected window isn't interesting */
2672         if(cur == c)
2673             continue;
2674         if (!client_normal(cur))
2675             continue;
2676         if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2677             continue;
2678         if(cur->iconic)
2679             continue;
2680         if(client_focus_target(cur) == cur &&
2681            !(cur->can_focus || cur->focus_notify))
2682             continue;
2683
2684         /* find the centre coords of this window, from the
2685          * currently focused window's point of view */
2686         his_cx = (cur->frame->area.x - my_cx)
2687             + cur->frame->area.width / 2;
2688         his_cy = (cur->frame->area.y - my_cy)
2689             + cur->frame->area.height / 2;
2690
2691         if(dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
2692            dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST) {
2693             int tx;
2694             /* Rotate the diagonals 45 degrees counterclockwise.
2695              * To do this, multiply the matrix /+h +h\ with the
2696              * vector (x y).                   \-h +h/
2697              * h = sqrt(0.5). We can set h := 1 since absolute
2698              * distance doesn't matter here. */
2699             tx = his_cx + his_cy;
2700             his_cy = -his_cx + his_cy;
2701             his_cx = tx;
2702         }
2703
2704         switch(dir) {
2705         case OB_DIRECTION_NORTH:
2706         case OB_DIRECTION_SOUTH:
2707         case OB_DIRECTION_NORTHEAST:
2708         case OB_DIRECTION_SOUTHWEST:
2709             offset = (his_cx < 0) ? -his_cx : his_cx;
2710             distance = ((dir == OB_DIRECTION_NORTH ||
2711                         dir == OB_DIRECTION_NORTHEAST) ?
2712                         -his_cy : his_cy);
2713             break;
2714         case OB_DIRECTION_EAST:
2715         case OB_DIRECTION_WEST:
2716         case OB_DIRECTION_SOUTHEAST:
2717         case OB_DIRECTION_NORTHWEST:
2718             offset = (his_cy < 0) ? -his_cy : his_cy;
2719             distance = ((dir == OB_DIRECTION_WEST ||
2720                         dir == OB_DIRECTION_NORTHWEST) ?
2721                         -his_cx : his_cx);
2722             break;
2723         }
2724
2725         /* the target must be in the requested direction */
2726         if(distance <= 0)
2727             continue;
2728
2729         /* Calculate score for this window.  The smaller the better. */
2730         score = distance + offset;
2731
2732         /* windows more than 45 degrees off the direction are
2733          * heavily penalized and will only be chosen if nothing
2734          * else within a million pixels */
2735         if(offset > distance)
2736             score += 1000000;
2737
2738         if(best_score == -1 || score < best_score)
2739             best_client = cur,
2740                 best_score = score;
2741     }
2742
2743     return best_client;
2744 }
2745
2746 void client_set_layer(ObClient *self, int layer)
2747 {
2748     if (layer < 0) {
2749         self->below = TRUE;
2750         self->above = FALSE;
2751     } else if (layer == 0) {
2752         self->below = self->above = FALSE;
2753     } else {
2754         self->below = FALSE;
2755         self->above = TRUE;
2756     }
2757     client_calc_layer(self);
2758     client_change_state(self); /* reflect this in the state hints */
2759 }
2760
2761 void client_set_undecorated(ObClient *self, gboolean undecorated)
2762 {
2763     if (self->undecorated != undecorated) {
2764         self->undecorated = undecorated;
2765         client_setup_decor_and_functions(self);
2766         client_change_state(self); /* reflect this in the state hints */
2767     }
2768 }
2769
2770 guint client_monitor(ObClient *self)
2771 {
2772     guint i;
2773
2774     for (i = 0; i < screen_num_monitors; ++i) {
2775         Rect *area = screen_physical_area_monitor(i);
2776         if (RECT_INTERSECTS_RECT(*area, self->frame->area))
2777             break;
2778     }
2779     if (i == screen_num_monitors) i = 0;
2780     g_assert(i < screen_num_monitors);
2781     return i;
2782 }
2783
2784 ObClient *client_search_top_transient(ObClient *self)
2785 {
2786     /* move up the transient chain as far as possible */
2787     if (self->transient_for) {
2788         if (self->transient_for != OB_TRAN_GROUP) {
2789             return client_search_top_transient(self->transient_for);
2790         } else {
2791             GSList *it;
2792
2793             for (it = self->group->members; it; it = it->next) {
2794                 ObClient *c = it->data;
2795
2796                 /* checking transient_for prevents infinate loops! */
2797                 if (c != self && !c->transient_for)
2798                     break;
2799             }
2800             if (it)
2801                 return it->data;
2802         }
2803     }
2804
2805     return self;
2806 }
2807
2808 ObClient *client_search_focus_parent(ObClient *self)
2809 {
2810     if (self->transient_for) {
2811         if (self->transient_for != OB_TRAN_GROUP) {
2812             if (client_focused(self->transient_for))
2813                 return self->transient_for;
2814         } else {
2815             GSList *it;
2816
2817             for (it = self->group->members; it; it = it->next) {
2818                 ObClient *c = it->data;
2819
2820                 /* checking transient_for prevents infinate loops! */
2821                 if (c != self && !c->transient_for)
2822                     if (client_focused(c))
2823                         return c;
2824             }
2825         }
2826     }
2827
2828     return NULL;
2829 }
2830
2831 ObClient *client_search_parent(ObClient *self, ObClient *search)
2832 {
2833     if (self->transient_for) {
2834         if (self->transient_for != OB_TRAN_GROUP) {
2835             if (self->transient_for == search)
2836                 return search;
2837         } else {
2838             GSList *it;
2839
2840             for (it = self->group->members; it; it = it->next) {
2841                 ObClient *c = it->data;
2842
2843                 /* checking transient_for prevents infinate loops! */
2844                 if (c != self && !c->transient_for)
2845                     if (c == search)
2846                         return search;
2847             }
2848         }
2849     }
2850
2851     return NULL;
2852 }
2853
2854 ObClient *client_search_transient(ObClient *self, ObClient *search)
2855 {
2856     GSList *sit;
2857
2858     for (sit = self->transients; sit; sit = g_slist_next(sit)) {
2859         if (sit->data == search)
2860             return search;
2861         if (client_search_transient(sit->data, search))
2862             return search;
2863     }
2864     return NULL;
2865 }
2866
2867 void client_update_sm_client_id(ObClient *self)
2868 {
2869     g_free(self->sm_client_id);
2870     self->sm_client_id = NULL;
2871
2872     if (!PROP_GETS(self->window, sm_client_id, locale, &self->sm_client_id) &&
2873         self->group)
2874         PROP_GETS(self->group->leader, sm_client_id, locale,
2875                   &self->sm_client_id);
2876 }
2877
2878 /* finds the nearest edge in the given direction from the current client
2879  * note to self: the edge is the -frame- edge (the actual one), not the
2880  * client edge.
2881  */
2882 int client_directional_edge_search(ObClient *c, ObDirection dir)
2883 {
2884     int dest;
2885     int my_edge_start, my_edge_end, my_offset;
2886     GList *it;
2887     Rect *a;
2888     
2889     if(!client_list)
2890         return -1;
2891
2892     a = screen_area(c->desktop);
2893
2894     switch(dir) {
2895     case OB_DIRECTION_NORTH:
2896         my_edge_start = c->frame->area.x;
2897         my_edge_end = c->frame->area.x + c->frame->area.width;
2898         my_offset = c->frame->area.y;
2899         
2900         dest = a->y; /* default: top of screen */
2901
2902         for(it = g_list_first(client_list); it; it = it->next) {
2903             int his_edge_start, his_edge_end, his_offset;
2904             ObClient *cur = it->data;
2905
2906             if(cur == c)
2907                 continue;
2908             if(!client_normal(cur))
2909                 continue;
2910             if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2911                 continue;
2912             if(cur->iconic)
2913                 continue;
2914
2915             his_edge_start = cur->frame->area.x;
2916             his_edge_end = cur->frame->area.x + cur->frame->area.width;
2917             his_offset = cur->frame->area.y + cur->frame->area.height;
2918
2919             if(his_offset + 1 > my_offset)
2920                 continue;
2921
2922             if(his_offset < dest)
2923                 continue;
2924             
2925             if(his_edge_start >= my_edge_start &&
2926                his_edge_start <= my_edge_end)
2927                 dest = his_offset;
2928
2929             if(my_edge_start >= his_edge_start &&
2930                my_edge_start <= his_edge_end)
2931                 dest = his_offset;
2932
2933         }
2934         break;
2935     case OB_DIRECTION_SOUTH:
2936         my_edge_start = c->frame->area.x;
2937         my_edge_end = c->frame->area.x + c->frame->area.width;
2938         my_offset = c->frame->area.y + c->frame->area.height;
2939         
2940         dest = a->y + a->height; /* default: bottom of screen */
2941
2942         for(it = g_list_first(client_list); it; it = it->next) {
2943             int his_edge_start, his_edge_end, his_offset;
2944             ObClient *cur = it->data;
2945
2946             if(cur == c)
2947                 continue;
2948             if(!client_normal(cur))
2949                 continue;
2950             if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2951                 continue;
2952             if(cur->iconic)
2953                 continue;
2954
2955             his_edge_start = cur->frame->area.x;
2956             his_edge_end = cur->frame->area.x + cur->frame->area.width;
2957             his_offset = cur->frame->area.y;
2958
2959
2960             if(his_offset - 1 < my_offset)
2961                 continue;
2962             
2963             if(his_offset > dest)
2964                 continue;
2965             
2966             if(his_edge_start >= my_edge_start &&
2967                his_edge_start <= my_edge_end)
2968                 dest = his_offset;
2969
2970             if(my_edge_start >= his_edge_start &&
2971                my_edge_start <= his_edge_end)
2972                 dest = his_offset;
2973
2974         }
2975         break;
2976     case OB_DIRECTION_WEST:
2977         my_edge_start = c->frame->area.y;
2978         my_edge_end = c->frame->area.y + c->frame->area.height;
2979         my_offset = c->frame->area.x;
2980
2981         dest = a->x; /* default: leftmost egde of screen */
2982
2983         for(it = g_list_first(client_list); it; it = it->next) {
2984             int his_edge_start, his_edge_end, his_offset;
2985             ObClient *cur = it->data;
2986
2987             if(cur == c)
2988                 continue;
2989             if(!client_normal(cur))
2990                 continue;
2991             if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2992                 continue;
2993             if(cur->iconic)
2994                 continue;
2995
2996             his_edge_start = cur->frame->area.y;
2997             his_edge_end = cur->frame->area.y + cur->frame->area.height;
2998             his_offset = cur->frame->area.x + cur->frame->area.width;
2999
3000             if(his_offset + 1 > my_offset)
3001                 continue;
3002             
3003             if(his_offset < dest)
3004                 continue;
3005             
3006             if(his_edge_start >= my_edge_start &&
3007                his_edge_start <= my_edge_end)
3008                 dest = his_offset;
3009
3010             if(my_edge_start >= his_edge_start &&
3011                my_edge_start <= his_edge_end)
3012                 dest = his_offset;
3013                 
3014
3015         }
3016         break;
3017     case OB_DIRECTION_EAST:
3018         my_edge_start = c->frame->area.y;
3019         my_edge_end = c->frame->area.y + c->frame->area.height;
3020         my_offset = c->frame->area.x + c->frame->area.width;
3021         
3022         dest = a->x + a->width; /* default: rightmost edge of screen */
3023
3024         for(it = g_list_first(client_list); it; it = it->next) {
3025             int his_edge_start, his_edge_end, his_offset;
3026             ObClient *cur = it->data;
3027
3028             if(cur == c)
3029                 continue;
3030             if(!client_normal(cur))
3031                 continue;
3032             if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
3033                 continue;
3034             if(cur->iconic)
3035                 continue;
3036
3037             his_edge_start = cur->frame->area.y;
3038             his_edge_end = cur->frame->area.y + cur->frame->area.height;
3039             his_offset = cur->frame->area.x;
3040
3041             if(his_offset - 1 < my_offset)
3042                 continue;
3043             
3044             if(his_offset > dest)
3045                 continue;
3046             
3047             if(his_edge_start >= my_edge_start &&
3048                his_edge_start <= my_edge_end)
3049                 dest = his_offset;
3050
3051             if(my_edge_start >= his_edge_start &&
3052                my_edge_start <= his_edge_end)
3053                 dest = his_offset;
3054
3055         }
3056         break;
3057     case OB_DIRECTION_NORTHEAST:
3058     case OB_DIRECTION_SOUTHEAST:
3059     case OB_DIRECTION_NORTHWEST:
3060     case OB_DIRECTION_SOUTHWEST:
3061         /* not implemented */
3062     default:
3063         g_assert_not_reached();
3064     }
3065     return dest;
3066 }
3067
3068 ObClient* client_under_pointer()
3069 {
3070     int x, y;
3071     GList *it;
3072     ObClient *ret = NULL;
3073
3074     if (screen_pointer_pos(&x, &y)) {
3075         for (it = stacking_list; it != NULL; it = it->next) {
3076             if (WINDOW_IS_CLIENT(it->data)) {
3077                 ObClient *c = WINDOW_AS_CLIENT(it->data);
3078                 if (c->desktop == screen_desktop &&
3079                     RECT_CONTAINS(c->frame->area, x, y)) {
3080                     ret = c;
3081                     break;
3082                 }
3083             }
3084         }
3085     }
3086     return ret;
3087 }