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