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