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