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