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