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