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