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