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