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