]> icculus.org git repositories - dana/openbox.git/blob - openbox/client.c
only cycle focus to normal windows
[dana/openbox.git] / openbox / client.c
1 #include "client.h"
2 #include "screen.h"
3 #include "prop.h"
4 #include "extensions.h"
5 #include "frame.h"
6 #include "engine.h"
7 #include "event.h"
8 #include "grab.h"
9 #include "focus.h"
10 #include "stacking.h"
11 #include "dispatch.h"
12 #include "group.h"
13
14 #include <glib.h>
15 #include <X11/Xutil.h>
16
17 /*! The event mask to grab on client windows */
18 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
19                           StructureNotifyMask)
20
21 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
22                                 ButtonMotionMask)
23
24 GList      *client_list      = NULL;
25 GHashTable *client_map       = NULL;
26
27 static Window *client_startup_stack_order = NULL;
28 static gulong  client_startup_stack_size = 0;
29
30 static void client_get_all(Client *self);
31 static void client_toggle_border(Client *self, gboolean show);
32 static void client_get_area(Client *self);
33 static void client_get_desktop(Client *self);
34 static void client_get_state(Client *self);
35 static void client_get_shaped(Client *self);
36 static void client_get_mwm_hints(Client *self);
37 static void client_get_gravity(Client *self);
38 static void client_showhide(Client *self);
39 static void client_change_allowed_actions(Client *self);
40 static void client_change_state(Client *self);
41 static void client_move_onscreen(Client *self);
42 static Client *search_focus_tree(Client *node, Client *skip);
43 static void client_apply_startup_state(Client *self);
44 static Client *search_modal_tree(Client *node, Client *skip);
45
46 static guint map_hash(Window *w) { return *w; }
47 static gboolean map_key_comp(Window *w1, Window *w2) { return *w1 == *w2; }
48
49 void client_startup()
50 {
51     client_map = g_hash_table_new((GHashFunc)map_hash,
52                                   (GEqualFunc)map_key_comp);
53
54     /* save the stacking order on startup! */
55     PROP_GET32U(ob_root, net_client_list_stacking, window,
56                 client_startup_stack_order, client_startup_stack_size);
57
58     client_set_list();
59 }
60
61 void client_shutdown()
62 {
63     g_hash_table_destroy(client_map);
64 }
65
66 void client_set_list()
67 {
68     Window *windows, *win_it;
69     GList *it;
70     guint size = g_list_length(client_list);
71
72     /* create an array of the window ids */
73     if (size > 0) {
74         windows = g_new(Window, size);
75         win_it = windows;
76         for (it = client_list; it != NULL; it = it->next, ++win_it)
77             *win_it = ((Client*)it->data)->window;
78     } else
79         windows = NULL;
80
81     PROP_SET32A(ob_root, net_client_list, window, windows, size);
82
83     if (windows)
84         g_free(windows);
85
86     stacking_set_list();
87 }
88
89 void client_manage_all()
90 {
91     unsigned int i, j, nchild;
92     Window w, *children;
93     XWMHints *wmhints;
94     XWindowAttributes attrib;
95
96     XQueryTree(ob_display, ob_root, &w, &w, &children, &nchild);
97
98     /* remove all icon windows from the list */
99     for (i = 0; i < nchild; i++) {
100         if (children[i] == None) continue;
101         wmhints = XGetWMHints(ob_display, children[i]);
102         if (wmhints) {
103             if ((wmhints->flags & IconWindowHint) &&
104                 (wmhints->icon_window != children[i]))
105                 for (j = 0; j < nchild; j++)
106                     if (children[j] == wmhints->icon_window) {
107                         children[j] = None;
108                         break;
109                     }
110             XFree(wmhints);
111         }
112     }
113
114     for (i = 0; i < nchild; ++i) {
115         if (children[i] == None)
116             continue;
117         if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
118             if (attrib.override_redirect) continue;
119
120             if (attrib.map_state != IsUnmapped)
121                 client_manage(children[i]);
122         }
123     }
124     XFree(children);
125
126     /* stack them as they were on startup!
127        why with stacking_lower? Why, because then windows who aren't in the
128        stacking list are on the top where you can see them instead of buried
129        at the bottom! */
130     for (i = client_startup_stack_size; i > 0; --i) {
131         Client *c;
132
133         w = client_startup_stack_order[i-1];
134         c = g_hash_table_lookup(client_map, &w);
135         if (c) stacking_lower(c);
136     }
137     g_free(client_startup_stack_order);
138     client_startup_stack_order = NULL;
139     client_startup_stack_size = 0;
140
141     if (focus_new)
142         focus_fallback(FALSE);
143 }
144
145 void client_manage(Window window)
146 {
147     Client *self;
148     XEvent e;
149     XWindowAttributes attrib;
150     XSetWindowAttributes attrib_set;
151 /*    XWMHints *wmhint; */
152     guint i;
153
154     grab_server(TRUE);
155
156     /* check if it has already been unmapped by the time we started mapping
157        the grab does a sync so we don't have to here */
158     if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
159         XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
160         XPutBackEvent(ob_display, &e);
161
162         grab_server(FALSE);
163         return; /* don't manage it */
164     }
165
166     /* make sure it isn't an override-redirect window */
167     if (!XGetWindowAttributes(ob_display, window, &attrib) ||
168         attrib.override_redirect) {
169         grab_server(FALSE);
170         return; /* don't manage it */
171     }
172   
173 /*    /\* is the window a docking app *\/
174     if ((wmhint = XGetWMHints(ob_display, window))) {
175         if ((wmhint->flags & StateHint) &&
176             wmhint->initial_state == WithdrawnState) {
177             /\* XXX: make dock apps work! *\/
178             grab_server(FALSE);
179             XFree(wmhint);
180             return;
181         }
182         XFree(wmhint);
183     }
184 */
185     g_message("Managing window: %lx", window);
186
187     /* choose the events we want to receive on the CLIENT window */
188     attrib_set.event_mask = CLIENT_EVENTMASK;
189     attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
190     XChangeWindowAttributes(ob_display, window,
191                             CWEventMask|CWDontPropagate, &attrib_set);
192
193
194     /* create the Client struct, and populate it from the hints on the
195        window */
196     self = g_new(Client, 1);
197     self->window = window;
198     client_get_all(self);
199
200     /* remove the client's border (and adjust re gravity) */
201     client_toggle_border(self, FALSE);
202      
203     /* specify that if we exit, the window should not be destroyed and should
204        be reparented back to root automatically */
205     XChangeSaveSet(ob_display, window, SetModeInsert);
206
207     /* create the decoration frame for the client window */
208     self->frame = engine_frame_new();
209
210     engine_frame_grab_client(self->frame, self);
211
212     client_apply_startup_state(self);
213
214     grab_server(FALSE);
215      
216     client_list = g_list_append(client_list, self);
217     stacking_list = g_list_append(stacking_list, self);
218     g_assert(!g_hash_table_lookup(client_map, &self->window));
219     g_hash_table_insert(client_map, &self->window, self);
220
221     /* update the focus lists */
222     if (self->desktop == DESKTOP_ALL) {
223         for (i = 0; i < screen_num_desktops; ++i)
224             focus_order[i] = g_list_append(focus_order[i], self);
225     } else {
226         i = self->desktop;
227         focus_order[i] = g_list_append(focus_order[i], self);
228     }
229
230     stacking_raise(self);
231
232     screen_update_struts();
233
234     dispatch_client(Event_Client_New, self, 0, 0);
235
236     client_showhide(self);
237
238     dispatch_client(Event_Client_Mapped, self, 0, 0);
239
240     if (ob_state != State_Starting && focus_new)
241         client_focus(self);
242
243     /* update the list hints */
244     client_set_list();
245
246     /* make sure the window is visible */
247     client_move_onscreen(self);
248
249     g_message("Managed window 0x%lx", window);
250 }
251
252 void client_unmanage_all()
253 {
254     while (client_list != NULL)
255         client_unmanage(client_list->data);
256 }
257
258 void client_unmanage(Client *self)
259 {
260     guint i;
261     int j;
262     GSList *it;
263
264     g_message("Unmanaging window: %lx", self->window);
265
266     dispatch_client(Event_Client_Destroy, self, 0, 0);
267     g_assert(self != NULL);
268
269     /* remove the window from our save set */
270     XChangeSaveSet(ob_display, self->window, SetModeDelete);
271
272     /* we dont want events no more */
273     XSelectInput(ob_display, self->window, NoEventMask);
274
275     engine_frame_hide(self->frame);
276
277     client_list = g_list_remove(client_list, self);
278     stacking_list = g_list_remove(stacking_list, self);
279     g_hash_table_remove(client_map, &self->window);
280
281     /* update the focus lists */
282     if (self->desktop == DESKTOP_ALL) {
283         for (i = 0; i < screen_num_desktops; ++i)
284             focus_order[i] = g_list_remove(focus_order[i], self);
285     } else {
286         i = self->desktop;
287         focus_order[i] = g_list_remove(focus_order[i], self);
288     }
289
290     /* once the client is out of the list, update the struts to remove it's
291        influence */
292     screen_update_struts();
293
294     /* tell our parent(s) that we're gone */
295     if (self->transient_for == TRAN_GROUP) { /* transient of group */
296         GSList *it;
297
298         for (it = self->group->members; it; it = it->next)
299             if (it->data != self)
300                 ((Client*)it->data)->transients =
301                     g_slist_remove(((Client*)it->data)->transients, self);
302     } else if (self->transient_for) {        /* transient of window */
303         self->transient_for->transients =
304             g_slist_remove(self->transient_for->transients, self);
305     }
306
307     /* tell our transients that we're gone */
308     for (it = self->transients; it != NULL; it = it->next) {
309         if (((Client*)it->data)->transient_for != TRAN_GROUP) {
310             ((Client*)it->data)->transient_for = NULL;
311             client_calc_layer(it->data);
312         }
313     }
314
315     focus_fallback(FALSE);
316
317     /* remove from its group */
318     if (self->group) {
319         group_remove(self->group, self);
320         self->group = NULL;
321     }
322
323     /* dispatch the unmapped event */
324     dispatch_client(Event_Client_Unmapped, self, 0, 0);
325     g_assert(self != NULL);
326
327     /* give the client its border back */
328     client_toggle_border(self, TRUE);
329
330     /* reparent the window out of the frame, and free the frame */
331     engine_frame_release_client(self->frame, self);
332     self->frame = NULL;
333      
334     if (ob_state != State_Exiting) {
335         /* these values should not be persisted across a window
336            unmapping/mapping */
337         prop_erase(self->window, prop_atoms.net_wm_desktop);
338         prop_erase(self->window, prop_atoms.net_wm_state);
339     } else {
340         /* if we're left in an iconic state, the client wont be mapped. this is
341            bad, since we will no longer be managing the window on restart */
342         if (self->iconic)
343             XMapWindow(ob_display, self->window);
344     }
345
346     g_message("Unmanaged window 0x%lx", self->window);
347
348     /* free all data allocated in the client struct */
349     g_slist_free(self->transients);
350     for (j = 0; j < self->nicons; ++j)
351         g_free(self->icons[j].data);
352     if (self->nicons > 0)
353         g_free(self->icons);
354     g_free(self->title);
355     g_free(self->icon_title);
356     g_free(self->name);
357     g_free(self->class);
358     g_free(self->role);
359     g_free(self);
360      
361     /* update the list hints */
362     client_set_list();
363 }
364
365 static void client_move_onscreen(Client *self)
366 {
367     Rect *a;
368     int x = self->frame->area.x, y = self->frame->area.y;
369
370     a = screen_area(self->desktop);
371     if (x >= a->x + a->width - 1)
372         x = a->x + a->width - self->frame->area.width;
373     if (y >= a->y + a->height - 1)
374         y = a->y + a->height - self->frame->area.height;
375     if (x + self->frame->area.width - 1 < a->x)
376         x = a->x;
377     if (y + self->frame->area.height - 1< a->y)
378         y = a->y;
379
380     frame_frame_gravity(self->frame, &x, &y); /* get where the client
381                                               should be */
382     client_configure(self , Corner_TopLeft, x, y,
383                      self->area.width, self->area.height,
384                      TRUE, TRUE);
385 }
386
387 static void client_toggle_border(Client *self, gboolean show)
388 {
389     /* adjust our idea of where the client is, based on its border. When the
390        border is removed, the client should now be considered to be in a
391        different position.
392        when re-adding the border to the client, the same operation needs to be
393        reversed. */
394     int oldx = self->area.x, oldy = self->area.y;
395     int x = oldx, y = oldy;
396     switch(self->gravity) {
397     default:
398     case NorthWestGravity:
399     case WestGravity:
400     case SouthWestGravity:
401         break;
402     case NorthEastGravity:
403     case EastGravity:
404     case SouthEastGravity:
405         if (show) x -= self->border_width * 2;
406         else      x += self->border_width * 2;
407         break;
408     case NorthGravity:
409     case SouthGravity:
410     case CenterGravity:
411     case ForgetGravity:
412     case StaticGravity:
413         if (show) x -= self->border_width;
414         else      x += self->border_width;
415         break;
416     }
417     switch(self->gravity) {
418     default:
419     case NorthWestGravity:
420     case NorthGravity:
421     case NorthEastGravity:
422         break;
423     case SouthWestGravity:
424     case SouthGravity:
425     case SouthEastGravity:
426         if (show) y -= self->border_width * 2;
427         else      y += self->border_width * 2;
428         break;
429     case WestGravity:
430     case EastGravity:
431     case CenterGravity:
432     case ForgetGravity:
433     case StaticGravity:
434         if (show) y -= self->border_width;
435         else      y += self->border_width;
436         break;
437     }
438     self->area.x = x;
439     self->area.y = y;
440
441     if (show) {
442         XSetWindowBorderWidth(ob_display, self->window, self->border_width);
443
444         /* move the client so it is back it the right spot _with_ its
445            border! */
446         if (x != oldx || y != oldy)
447             XMoveWindow(ob_display, self->window, x, y);
448     } else
449         XSetWindowBorderWidth(ob_display, self->window, 0);
450 }
451
452
453 static void client_get_all(Client *self)
454 {
455     /* update EVERYTHING!! */
456
457     self->ignore_unmaps = 0;
458   
459     /* defaults */
460     self->frame = NULL;
461     self->title = self->icon_title = NULL;
462     self->name = self->class = self->role = NULL;
463     self->wmstate = NormalState;
464     self->transient = FALSE;
465     self->transients = NULL;
466     self->transient_for = NULL;
467     self->layer = -1;
468     self->urgent = FALSE;
469     self->positioned = FALSE;
470     self->disabled_decorations = 0;
471     self->group = NULL;
472     self->nicons = 0;
473
474     client_get_area(self);
475     client_get_desktop(self);
476     client_get_state(self);
477     client_get_shaped(self);
478
479     client_update_transient_for(self);
480     client_get_mwm_hints(self);
481     client_get_type(self);/* this can change the mwmhints for special cases */
482
483     client_update_protocols(self);
484
485     client_get_gravity(self); /* get the attribute gravity */
486     client_update_normal_hints(self); /* this may override the attribute
487                                          gravity */
488
489     /* got the type, the mwmhints, the protocols, and the normal hints
490        (min/max sizes), so we're ready to set up the decorations/functions */
491     client_setup_decor_and_functions(self);
492   
493     client_update_wmhints(self);
494     client_update_title(self);
495     client_update_icon_title(self);
496     client_update_class(self);
497     client_update_strut(self);
498     client_update_icons(self);
499     client_update_kwm_icon(self);
500
501     /* this makes sure that these windows appear on all desktops */
502     if (self->type == Type_Desktop)
503         self->desktop = DESKTOP_ALL;
504
505     /* set the desktop hint, to make sure that it always exists, and to
506        reflect any changes we've made here */
507     PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
508
509     client_change_state(self);
510 }
511
512 static void client_get_area(Client *self)
513 {
514     XWindowAttributes wattrib;
515     Status ret;
516   
517     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
518     g_assert(ret != BadWindow);
519
520     RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
521     self->border_width = wattrib.border_width;
522 }
523
524 static void client_get_desktop(Client *self)
525 {
526     unsigned int d;
527
528     if (PROP_GET32(self->window, net_wm_desktop, cardinal, d)) {
529         if (d >= screen_num_desktops && d != DESKTOP_ALL)
530             d = screen_num_desktops - 1;
531         self->desktop = d;
532     } else {
533         /* defaults to the current desktop */
534         self->desktop = screen_desktop;
535     }
536 }
537
538 static void client_get_state(Client *self)
539 {
540     gulong *state;
541     gulong num;
542   
543     self->modal = self->shaded = self->max_horz = self->max_vert =
544         self->fullscreen = self->above = self->below = self->iconic =
545         self->skip_taskbar = self->skip_pager = FALSE;
546
547     if (PROP_GET32U(self->window, net_wm_state, atom, state, num)) {
548         gulong i;
549         for (i = 0; i < num; ++i) {
550             if (state[i] == prop_atoms.net_wm_state_modal)
551                 self->modal = TRUE;
552             else if (state[i] == prop_atoms.net_wm_state_shaded)
553                 self->shaded = TRUE;
554             else if (state[i] == prop_atoms.net_wm_state_hidden)
555                 self->iconic = TRUE;
556             else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
557                 self->skip_taskbar = TRUE;
558             else if (state[i] == prop_atoms.net_wm_state_skip_pager)
559                 self->skip_pager = TRUE;
560             else if (state[i] == prop_atoms.net_wm_state_fullscreen)
561                 self->fullscreen = TRUE;
562             else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
563                 self->max_vert = TRUE;
564             else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
565                 self->max_horz = TRUE;
566             else if (state[i] == prop_atoms.net_wm_state_above)
567                 self->above = TRUE;
568             else if (state[i] == prop_atoms.net_wm_state_below)
569                 self->below = TRUE;
570         }
571
572         g_free(state);
573     }
574 }
575
576 static void client_get_shaped(Client *self)
577 {
578     self->shaped = FALSE;
579 #ifdef   SHAPE
580     if (extensions_shape) {
581         int foo;
582         guint ufoo;
583         int s;
584
585         XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
586
587         XShapeQueryExtents(ob_display, self->window, &s, &foo,
588                            &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
589                            &ufoo);
590         self->shaped = (s != 0);
591     }
592 #endif
593 }
594
595 void client_update_transient_for(Client *self)
596 {
597     Window t = None;
598     Client *c = NULL;
599
600     if (XGetTransientForHint(ob_display, self->window, &t) &&
601         t != self->window) { /* cant be transient to itself! */
602         self->transient = TRUE;
603         c = g_hash_table_lookup(client_map, &t);
604         g_assert(c != self);/* if this happens then we need to check for it*/
605
606         if (!c && self->group) {
607             /* not transient to a client, see if it is transient for a
608                group */
609             if (t == self->group->leader ||
610                 t == None ||
611                 t == ob_root) {
612                 /* window is a transient for its group! */
613                 c = TRAN_GROUP;
614             }
615         }
616     } else
617         self->transient = FALSE;
618
619     /* if anything has changed... */
620     if (c != self->transient_for) {
621         if (self->transient_for == TRAN_GROUP) { /* transient of group */
622             GSList *it;
623
624             /* remove from old parents */
625             for (it = self->group->members; it; it = it->next)
626                 if (it->data != self)
627                     ((Client*)it->data)->transients =
628                         g_slist_remove(((Client*)it->data)->transients, self);
629         } else if (self->transient_for != NULL) { /* transient of window */
630             /* remove from old parent */
631             self->transient_for->transients =
632                 g_slist_remove(self->transient_for->transients, self);
633         }
634         self->transient_for = c;
635         if (self->transient_for == TRAN_GROUP) { /* transient of group */
636             GSList *it;
637
638             /* add to new parents */
639             for (it = self->group->members; it; it = it->next)
640                 if (it->data != self)
641                     ((Client*)it->data)->transients =
642                         g_slist_append(((Client*)it->data)->transients, self);
643         } else if (self->transient_for != NULL) { /* transient of window */
644             /* add to new parent */
645             self->transient_for->transients =
646                 g_slist_append(self->transient_for->transients, self);
647         }
648     }
649 }
650
651 static void client_get_mwm_hints(Client *self)
652 {
653     unsigned long num;
654     unsigned long *hints;
655
656     self->mwmhints.flags = 0; /* default to none */
657
658     if (PROP_GET32U(self->window, motif_wm_hints, motif_wm_hints, hints, num)) {
659         if (num >= MWM_ELEMENTS) {
660             self->mwmhints.flags = hints[0];
661             self->mwmhints.functions = hints[1];
662             self->mwmhints.decorations = hints[2];
663         }
664         g_free(hints);
665     }
666 }
667
668 void client_get_type(Client *self)
669 {
670     gulong *val, num, i;
671
672     self->type = -1;
673   
674     if (PROP_GET32U(self->window, net_wm_window_type, atom, val, num)) {
675         /* use the first value that we know about in the array */
676         for (i = 0; i < num; ++i) {
677             if (val[i] == prop_atoms.net_wm_window_type_desktop)
678                 self->type = Type_Desktop;
679             else if (val[i] == prop_atoms.net_wm_window_type_dock)
680                 self->type = Type_Dock;
681             else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
682                 self->type = Type_Toolbar;
683             else if (val[i] == prop_atoms.net_wm_window_type_menu)
684                 self->type = Type_Menu;
685             else if (val[i] == prop_atoms.net_wm_window_type_utility)
686                 self->type = Type_Utility;
687             else if (val[i] == prop_atoms.net_wm_window_type_splash)
688                 self->type = Type_Splash;
689             else if (val[i] == prop_atoms.net_wm_window_type_dialog)
690                 self->type = Type_Dialog;
691             else if (val[i] == prop_atoms.net_wm_window_type_normal)
692                 self->type = Type_Normal;
693             else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
694                 /* prevent this window from getting any decor or
695                    functionality */
696                 self->mwmhints.flags &= (MwmFlag_Functions |
697                                          MwmFlag_Decorations);
698                 self->mwmhints.decorations = 0;
699                 self->mwmhints.functions = 0;
700             }
701             if (self->type != (WindowType) -1)
702                 break; /* grab the first legit type */
703         }
704         g_free(val);
705     }
706     
707     if (self->type == (WindowType) -1) {
708         /*the window type hint was not set, which means we either classify
709           ourself as a normal window or a dialog, depending on if we are a
710           transient. */
711         if (self->transient)
712             self->type = Type_Dialog;
713         else
714             self->type = Type_Normal;
715     }
716 }
717
718 void client_update_protocols(Client *self)
719 {
720     Atom *proto;
721     gulong num_return, i;
722
723     self->focus_notify = FALSE;
724     self->delete_window = FALSE;
725
726     if (PROP_GET32U(self->window, wm_protocols, atom, proto, num_return)) {
727         for (i = 0; i < num_return; ++i) {
728             if (proto[i] == prop_atoms.wm_delete_window) {
729                 /* this means we can request the window to close */
730                 self->delete_window = TRUE;
731             } else if (proto[i] == prop_atoms.wm_take_focus)
732                 /* if this protocol is requested, then the window will be
733                    notified whenever we want it to receive focus */
734                 self->focus_notify = TRUE;
735         }
736         g_free(proto);
737     }
738 }
739
740 static void client_get_gravity(Client *self)
741 {
742     XWindowAttributes wattrib;
743     Status ret;
744
745     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
746     g_assert(ret != BadWindow);
747     self->gravity = wattrib.win_gravity;
748 }
749
750 void client_update_normal_hints(Client *self)
751 {
752     XSizeHints size;
753     long ret;
754     int oldgravity = self->gravity;
755
756     /* defaults */
757     self->min_ratio = 0.0f;
758     self->max_ratio = 0.0f;
759     SIZE_SET(self->size_inc, 1, 1);
760     SIZE_SET(self->base_size, 0, 0);
761     SIZE_SET(self->min_size, 0, 0);
762     SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
763
764     /* get the hints from the window */
765     if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
766         self->positioned = !!(size.flags & (PPosition|USPosition));
767
768         if (size.flags & PWinGravity) {
769             self->gravity = size.win_gravity;
770       
771             /* if the client has a frame, i.e. has already been mapped and
772                is changing its gravity */
773             if (self->frame && self->gravity != oldgravity) {
774                 /* move our idea of the client's position based on its new
775                    gravity */
776                 self->area.x = self->frame->area.x;
777                 self->area.y = self->frame->area.y;
778                 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
779             }
780         }
781
782         if (size.flags & PAspect) {
783             if (size.min_aspect.y)
784                 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
785             if (size.max_aspect.y)
786                 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
787         }
788
789         if (size.flags & PMinSize)
790             SIZE_SET(self->min_size, size.min_width, size.min_height);
791     
792         if (size.flags & PMaxSize)
793             SIZE_SET(self->max_size, size.max_width, size.max_height);
794     
795         if (size.flags & PBaseSize)
796             SIZE_SET(self->base_size, size.base_width, size.base_height);
797     
798         if (size.flags & PResizeInc)
799             SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
800     }
801 }
802
803 void client_setup_decor_and_functions(Client *self)
804 {
805     /* start with everything (cept fullscreen) */
806     self->decorations = Decor_Titlebar | Decor_Handle | Decor_Border |
807         Decor_Icon | Decor_AllDesktops | Decor_Iconify | Decor_Maximize |
808         Decor_Shade;
809     self->functions = Func_Resize | Func_Move | Func_Iconify | Func_Maximize |
810         Func_Shade;
811     if (self->delete_window) {
812         self->decorations |= Decor_Close;
813         self->functions |= Func_Close;
814     }
815
816     if (!(self->min_size.width < self->max_size.width ||
817           self->min_size.height < self->max_size.height)) {
818         self->decorations &= ~(Decor_Maximize | Decor_Handle);
819         self->functions &= ~(Func_Resize | Func_Maximize);
820     }
821
822     switch (self->type) {
823     case Type_Normal:
824         /* normal windows retain all of the possible decorations and
825            functionality, and are the only windows that you can fullscreen */
826         self->functions |= Func_Fullscreen;
827         break;
828
829     case Type_Dialog:
830         /* dialogs cannot be maximized */
831         self->decorations &= ~Decor_Maximize;
832         self->functions &= ~Func_Maximize;
833         break;
834
835     case Type_Menu:
836     case Type_Toolbar:
837     case Type_Utility:
838         /* these windows get less functionality */
839         self->decorations &= ~(Decor_Iconify | Decor_Handle);
840         self->functions &= ~(Func_Iconify | Func_Resize);
841         break;
842
843     case Type_Desktop:
844     case Type_Dock:
845     case Type_Splash:
846         /* none of these windows are manipulated by the window manager */
847         self->decorations = 0;
848         self->functions = 0;
849         break;
850     }
851
852     /* Mwm Hints are applied subtractively to what has already been chosen for
853        decor and functionality */
854     if (self->mwmhints.flags & MwmFlag_Decorations) {
855         if (! (self->mwmhints.decorations & MwmDecor_All)) {
856             if (! (self->mwmhints.decorations & MwmDecor_Border))
857                 self->decorations &= ~Decor_Border;
858             if (! (self->mwmhints.decorations & MwmDecor_Handle))
859                 self->decorations &= ~Decor_Handle;
860             if (! (self->mwmhints.decorations & MwmDecor_Title))
861                 self->decorations &= ~Decor_Titlebar;
862             if (! (self->mwmhints.decorations & MwmDecor_Iconify))
863                 self->decorations &= ~Decor_Iconify;
864             if (! (self->mwmhints.decorations & MwmDecor_Maximize))
865                 self->decorations &= ~Decor_Maximize;
866         }
867     }
868
869     if (self->mwmhints.flags & MwmFlag_Functions) {
870         if (! (self->mwmhints.functions & MwmFunc_All)) {
871             if (! (self->mwmhints.functions & MwmFunc_Resize))
872                 self->functions &= ~Func_Resize;
873             if (! (self->mwmhints.functions & MwmFunc_Move))
874                 self->functions &= ~Func_Move;
875             if (! (self->mwmhints.functions & MwmFunc_Iconify))
876                 self->functions &= ~Func_Iconify;
877             if (! (self->mwmhints.functions & MwmFunc_Maximize))
878                 self->functions &= ~Func_Maximize;
879             /* dont let mwm hints kill the close button
880                if (! (self->mwmhints.functions & MwmFunc_Close))
881                self->functions &= ~Func_Close; */
882         }
883     }
884
885     /* can't maximize without moving/resizing */
886     if (!((self->functions & Func_Move) && (self->functions & Func_Resize)))
887         self->functions &= ~(Func_Maximize | Func_Fullscreen);
888
889     /* finally, user specified disabled decorations are applied to subtract
890        decorations */
891     if (self->disabled_decorations & Decor_Titlebar)
892         self->decorations &= ~Decor_Titlebar;
893     if (self->disabled_decorations & Decor_Handle)
894         self->decorations &= ~Decor_Handle;
895     if (self->disabled_decorations & Decor_Border)
896         self->decorations &= ~Decor_Border;
897     if (self->disabled_decorations & Decor_Iconify)
898         self->decorations &= ~Decor_Iconify;
899     if (self->disabled_decorations & Decor_Maximize)
900         self->decorations &= ~Decor_Maximize;
901     if (self->disabled_decorations & Decor_AllDesktops)
902         self->decorations &= ~Decor_AllDesktops;
903     if (self->disabled_decorations & Decor_Shade)
904         self->decorations &= ~Decor_Shade;
905     if (self->disabled_decorations & Decor_Close)
906         self->decorations &= ~Decor_Close;
907
908     /* if we don't have a titlebar, then we cannot shade! */
909     if (!(self->decorations & Decor_Titlebar))
910         self->functions &= ~Func_Shade;
911
912     client_change_allowed_actions(self);
913
914     if (self->frame) {
915         /* change the decors on the frame, and with more/less decorations,
916            we may also need to be repositioned */
917         engine_frame_adjust_area(self->frame, TRUE, TRUE);
918         /* with new decor, the window's maximized size may change */
919         client_remaximize(self);
920     }
921 }
922
923 static void client_change_allowed_actions(Client *self)
924 {
925     Atom actions[9];
926     int num = 0;
927
928     actions[num++] = prop_atoms.net_wm_action_change_desktop;
929
930     if (self->functions & Func_Shade)
931         actions[num++] = prop_atoms.net_wm_action_shade;
932     if (self->functions & Func_Close)
933         actions[num++] = prop_atoms.net_wm_action_close;
934     if (self->functions & Func_Move)
935         actions[num++] = prop_atoms.net_wm_action_move;
936     if (self->functions & Func_Iconify)
937         actions[num++] = prop_atoms.net_wm_action_minimize;
938     if (self->functions & Func_Resize)
939         actions[num++] = prop_atoms.net_wm_action_resize;
940     if (self->functions & Func_Fullscreen)
941         actions[num++] = prop_atoms.net_wm_action_fullscreen;
942     if (self->functions & Func_Maximize) {
943         actions[num++] = prop_atoms.net_wm_action_maximize_horz;
944         actions[num++] = prop_atoms.net_wm_action_maximize_vert;
945     }
946
947     PROP_SET32A(self->window, net_wm_allowed_actions, atom, actions, num);
948
949     /* make sure the window isn't breaking any rules now */
950
951     if (!(self->functions & Func_Shade) && self->shaded) {
952         if (self->frame) client_shade(self, FALSE);
953         else self->shaded = FALSE;
954     }
955     if (!(self->functions & Func_Iconify) && self->iconic) {
956         g_message("UNSETTING ICONIC");
957         if (self->frame) client_iconify(self, FALSE, TRUE);
958         else self->iconic = FALSE;
959     }
960     if (!(self->functions & Func_Fullscreen) && self->fullscreen) {
961         if (self->frame) client_fullscreen(self, FALSE, TRUE);
962         else self->fullscreen = FALSE;
963     }
964     if (!(self->functions & Func_Maximize) && (self->max_horz ||
965                                                self->max_vert)) {
966         if (self->frame) client_maximize(self, FALSE, 0, TRUE);
967         else self->max_vert = self->max_horz = FALSE;
968     }
969 }
970
971 void client_remaximize(Client *self)
972 {
973     int dir;
974     if (self->max_horz && self->max_vert)
975         dir = 0;
976     else if (self->max_horz)
977         dir = 1;
978     else if (self->max_vert)
979         dir = 2;
980     else
981         return; /* not maximized */
982     self->max_horz = self->max_vert = FALSE;
983     client_maximize(self, TRUE, dir, FALSE);
984 }
985
986 void client_update_wmhints(Client *self)
987 {
988     XWMHints *hints;
989     gboolean ur = FALSE;
990
991     /* assume a window takes input if it doesnt specify */
992     self->can_focus = TRUE;
993   
994     if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
995         if (hints->flags & InputHint)
996             self->can_focus = hints->input;
997
998         /* only do this when first managing the window *AND* when we aren't
999            starting up! */
1000         if (ob_state != State_Starting && self->frame == NULL)
1001             if (hints->flags & StateHint)
1002                 self->iconic = hints->initial_state == IconicState;
1003
1004         if (hints->flags & XUrgencyHint)
1005             ur = TRUE;
1006
1007         if (!(hints->flags & WindowGroupHint))
1008             hints->window_group = None; /* no group */
1009         /* did the group state change? */
1010         if (hints->window_group != (self->group ? self->group->leader : None)){
1011             /* remove from the old group if there was one */
1012             if (self->group != NULL)
1013                 group_remove(self->group, self);
1014             if (hints->window_group != None)
1015                 self->group = group_add(hints->window_group, self);
1016
1017             /* because the self->transient flag wont change from this call,
1018                we don't need to update the window's type and such, only its
1019                transient_for, and the transients lists of other windows in the
1020                group may be affected */
1021             client_update_transient_for(self);
1022         }
1023
1024         if (hints->flags & IconPixmapHint) {
1025             client_update_kwm_icon(self);
1026             /* try get the kwm icon first, this is a fallback only */
1027             if (self->pixmap_icon == None) {
1028                 self->pixmap_icon = hints->icon_pixmap;
1029                 if (hints->flags & IconMaskHint)
1030                     self->pixmap_icon_mask = hints->icon_mask;
1031                 else
1032                     self->pixmap_icon_mask = None;
1033
1034                 if (self->frame)
1035                     engine_frame_adjust_icon(self->frame);
1036             }
1037         }
1038
1039         XFree(hints);
1040     }
1041
1042     if (ur != self->urgent) {
1043         self->urgent = ur;
1044         g_message("Urgent Hint for 0x%lx: %s", self->window,
1045                   ur ? "ON" : "OFF");
1046         /* fire the urgent callback if we're mapped, otherwise, wait until
1047            after we're mapped */
1048         if (self->frame)
1049             dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1050     }
1051 }
1052
1053 void client_update_title(Client *self)
1054 {
1055     gchar *data = NULL;
1056
1057     g_free(self->title);
1058      
1059     /* try netwm */
1060     if (!PROP_GETS(self->window, net_wm_name, utf8, data)) {
1061         /* try old x stuff */
1062         if (PROP_GETS(self->window, wm_name, string, data)) {
1063             /* convert it to UTF-8 */
1064             gsize r, w;
1065             gchar *u;
1066
1067             u = g_locale_to_utf8(data, -1, &r, &w, NULL);
1068             if (u == NULL) {
1069                 g_warning("Unable to convert string to UTF-8");
1070             } else {
1071                 g_free(data);
1072                 data = u;
1073             }
1074         }
1075         if (data == NULL)
1076             data = g_strdup("Unnamed Window");
1077
1078         PROP_SETS(self->window, net_wm_visible_name, utf8, data);
1079     }
1080
1081     self->title = data;
1082
1083     if (self->frame)
1084         engine_frame_adjust_title(self->frame);
1085 }
1086
1087 void client_update_icon_title(Client *self)
1088 {
1089     gchar *data = NULL;
1090
1091     g_free(self->icon_title);
1092      
1093     /* try netwm */
1094     if (!PROP_GETS(self->window, net_wm_icon_name, utf8, data)) {
1095         /* try old x stuff */
1096         if (PROP_GETS(self->window, wm_icon_name, string, data)) {
1097             /* convert it to UTF-8 */
1098             gsize r, w;
1099             gchar *u;
1100
1101             u = g_locale_to_utf8(data, -1, &r, &w, NULL);
1102             if (u == NULL) {
1103                 g_warning("Unable to convert string to UTF-8");
1104             } else {
1105                 g_free(data);
1106                 data = u;
1107             }
1108         }
1109         if (data == NULL)
1110             data = g_strdup("Unnamed Window");
1111
1112         PROP_SETS(self->window, net_wm_visible_icon_name, utf8, data);
1113     }
1114
1115     self->icon_title = data;
1116 }
1117
1118 void client_update_class(Client *self)
1119 {
1120     GPtrArray *data;
1121     gchar *s;
1122     guint i;
1123
1124     if (self->name) g_free(self->name);
1125     if (self->class) g_free(self->class);
1126     if (self->role) g_free(self->role);
1127
1128     self->name = self->class = self->role = NULL;
1129
1130     data = g_ptr_array_new();
1131      
1132     if (PROP_GETSA(self->window, wm_class, string, data)) {
1133         if (data->len > 0)
1134             self->name = g_strdup(g_ptr_array_index(data, 0));
1135         if (data->len > 1)
1136             self->class = g_strdup(g_ptr_array_index(data, 1));
1137     }
1138      
1139     for (i = 0; i < data->len; ++i)
1140         g_free(g_ptr_array_index(data, i));
1141     g_ptr_array_free(data, TRUE);
1142
1143     if (PROP_GETS(self->window, wm_window_role, string, s))
1144         self->role = g_strdup(s);
1145
1146     if (self->name == NULL) self->name = g_strdup("");
1147     if (self->class == NULL) self->class = g_strdup("");
1148     if (self->role == NULL) self->role = g_strdup("");
1149 }
1150
1151 void client_update_strut(Client *self)
1152 {
1153     gulong *data;
1154
1155     if (PROP_GET32A(self->window, net_wm_strut, cardinal, data, 4)) {
1156         STRUT_SET(self->strut, data[0], data[2], data[1], data[3]);
1157         g_free(data);
1158     } else
1159         STRUT_SET(self->strut, 0, 0, 0, 0);
1160
1161     /* updating here is pointless while we're being mapped cuz we're not in
1162        the client list yet */
1163     if (self->frame)
1164         screen_update_struts();
1165 }
1166
1167 void client_update_icons(Client *self)
1168 {
1169     unsigned long num;
1170     unsigned long *data;
1171     unsigned long w, h, i;
1172     int j;
1173
1174     for (j = 0; j < self->nicons; ++j)
1175         g_free(self->icons[j].data);
1176     if (self->nicons > 0)
1177         g_free(self->icons);
1178     self->nicons = 0;
1179
1180     if (PROP_GET32U(self->window, net_wm_icon, cardinal, data, num)) {
1181         /* figure out how many valid icons are in here */
1182         i = 0;
1183         while (num - i > 2) {
1184             w = data[i++];
1185             h = data[i++];
1186             i += w * h;
1187             if (i > num) break;
1188             ++self->nicons;
1189         }
1190
1191         self->icons = g_new(Icon, self->nicons);
1192     
1193         /* store the icons */
1194         i = 0;
1195         for (j = 0; j < self->nicons; ++j) {
1196             w = self->icons[j].width = data[i++];
1197             h = self->icons[j].height = data[i++];
1198             self->icons[j].data =
1199                 g_memdup(&data[i], w * h * sizeof(gulong));
1200             i += w * h;
1201             g_assert(i <= num);
1202         }
1203
1204         g_free(data);
1205     }
1206
1207     if (self->frame)
1208         engine_frame_adjust_icon(self->frame);
1209 }
1210
1211 void client_update_kwm_icon(Client *self)
1212 {
1213     Pixmap *data;
1214
1215     if (PROP_GET32A(self->window, kwm_win_icon, kwm_win_icon, data, 2)) {
1216         self->pixmap_icon = data[0];
1217         self->pixmap_icon_mask = data[1];
1218         g_free(data);
1219     } else {
1220         self->pixmap_icon = self->pixmap_icon_mask = None;
1221     }
1222     if (self->frame)
1223         engine_frame_adjust_icon(self->frame);
1224 }
1225
1226 static void client_change_state(Client *self)
1227 {
1228     unsigned long state[2];
1229     Atom netstate[10];
1230     int num;
1231
1232     state[0] = self->wmstate;
1233     state[1] = None;
1234     PROP_SET32A(self->window, wm_state, wm_state, state, 2);
1235
1236     num = 0;
1237     if (self->modal)
1238         netstate[num++] = prop_atoms.net_wm_state_modal;
1239     if (self->shaded)
1240         netstate[num++] = prop_atoms.net_wm_state_shaded;
1241     if (self->iconic)
1242         netstate[num++] = prop_atoms.net_wm_state_hidden;
1243     if (self->skip_taskbar)
1244         netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1245     if (self->skip_pager)
1246         netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1247     if (self->fullscreen)
1248         netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1249     if (self->max_vert)
1250         netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1251     if (self->max_horz)
1252         netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1253     if (self->above)
1254         netstate[num++] = prop_atoms.net_wm_state_above;
1255     if (self->below)
1256         netstate[num++] = prop_atoms.net_wm_state_below;
1257     PROP_SET32A(self->window, net_wm_state, atom, netstate, num);
1258
1259     client_calc_layer(self);
1260
1261     if (self->frame)
1262         engine_frame_adjust_state(self->frame);
1263 }
1264
1265 static Client *search_focus_tree(Client *node, Client *skip)
1266 {
1267     GSList *it;
1268     Client *ret;
1269
1270     for (it = node->transients; it != NULL; it = it->next) {
1271         Client *c = it->data;
1272         if (c == skip) continue; /* circular? */
1273         if ((ret = search_focus_tree(c, skip))) return ret;
1274         if (client_focused(c)) return c;
1275     }
1276     return NULL;
1277 }
1278
1279 void client_calc_layer(Client *self)
1280 {
1281     StackLayer l;
1282     gboolean fs;
1283     Client *c;
1284
1285     /* are we fullscreen, or do we have a fullscreen transient parent? */
1286     c = self;
1287     fs = FALSE;
1288     while (c && c != TRAN_GROUP) { /* XXX do smthng with the TRAN_GROUP case?*/
1289         if (c->fullscreen) {
1290             fs = TRUE;
1291             break;
1292         }
1293         c = c->transient_for;
1294     }
1295     if (!fs && self->fullscreen) {
1296         /* is one of our transients focused? */
1297         c = search_focus_tree(self, self);
1298         if (c != NULL) fs = TRUE;
1299     }
1300   
1301     if (self->iconic) l = Layer_Icon;
1302     else if (fs) l = Layer_Fullscreen;
1303     else if (self->type == Type_Desktop) l = Layer_Desktop;
1304     else if (self->type == Type_Dock) {
1305         if (!self->below) l = Layer_Top;
1306         else l = Layer_Normal;
1307     }
1308     else if (self->above) l = Layer_Above;
1309     else if (self->below) l = Layer_Below;
1310     else l = Layer_Normal;
1311      
1312     if (l != self->layer) {
1313         self->layer = l;
1314         if (self->frame)
1315             stacking_raise(self);
1316     }
1317 }
1318
1319 gboolean client_should_show(Client *self)
1320 {
1321     if (self->iconic) return FALSE;
1322     else if (!(self->desktop == screen_desktop ||
1323                self->desktop == DESKTOP_ALL)) return FALSE;
1324     else if (client_normal(self) && screen_showing_desktop) return FALSE;
1325     
1326     return TRUE;
1327 }
1328
1329 static void client_showhide(Client *self)
1330 {
1331
1332     if (client_should_show(self))
1333         engine_frame_show(self->frame);
1334     else
1335         engine_frame_hide(self->frame);
1336 }
1337
1338 gboolean client_normal(Client *self) {
1339     return ! (self->type == Type_Desktop || self->type == Type_Dock ||
1340               self->type == Type_Splash);
1341 }
1342
1343 static void client_apply_startup_state(Client *self)
1344 {
1345     /* these are in a carefully crafted order.. */
1346
1347     if (self->iconic) {
1348         self->iconic = FALSE;
1349         client_iconify(self, TRUE, FALSE);
1350     }
1351     if (self->fullscreen) {
1352         self->fullscreen = FALSE;
1353         client_fullscreen(self, TRUE, FALSE);
1354     }
1355     if (self->shaded) {
1356         self->shaded = FALSE;
1357         client_shade(self, TRUE);
1358     }
1359     if (self->urgent)
1360         dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1361   
1362     if (self->max_vert && self->max_horz) {
1363         self->max_vert = self->max_horz = FALSE;
1364         client_maximize(self, TRUE, 0, FALSE);
1365     } else if (self->max_vert) {
1366         self->max_vert = FALSE;
1367         client_maximize(self, TRUE, 2, FALSE);
1368     } else if (self->max_horz) {
1369         self->max_horz = FALSE;
1370         client_maximize(self, TRUE, 1, FALSE);
1371     }
1372
1373     /* nothing to do for the other states:
1374        skip_taskbar
1375        skip_pager
1376        modal
1377        above
1378        below
1379     */
1380 }
1381
1382 void client_configure(Client *self, Corner anchor, int x, int y, int w, int h,
1383                       gboolean user, gboolean final)
1384 {
1385     gboolean moved = FALSE, resized = FALSE;
1386
1387     /* gets the frame's position */
1388     frame_client_gravity(self->frame, &x, &y);
1389
1390     /* these positions are frame positions, not client positions */
1391
1392     /* set the size and position if fullscreen */
1393     if (self->fullscreen) {
1394         x = 0;
1395         y = 0;
1396         w = screen_physical_size.width;
1397         h = screen_physical_size.height;
1398     } else {
1399         /* set the size and position if maximized */
1400         if (self->max_horz) {
1401             x = screen_area(self->desktop)->x - self->frame->size.left;
1402             w = screen_area(self->desktop)->width;
1403         }
1404         if (self->max_vert) {
1405             y = screen_area(self->desktop)->y;
1406             h = screen_area(self->desktop)->height -
1407                 self->frame->size.top - self->frame->size.bottom;
1408         }
1409     }
1410
1411     /* gets the client's position */
1412     frame_frame_gravity(self->frame, &x, &y);
1413
1414     /* these override the above states! if you cant move you can't move! */
1415     if (user) {
1416         if (!(self->functions & Func_Move)) {
1417             x = self->area.x;
1418             y = self->area.y;
1419         }
1420         if (!(self->functions & Func_Resize)) {
1421             w = self->area.width;
1422             h = self->area.height;
1423         }
1424     }
1425
1426     if (!(w == self->area.width && h == self->area.height)) {
1427         w -= self->base_size.width;
1428         h -= self->base_size.height;
1429
1430         if (user) {
1431             /* for interactive resizing. have to move half an increment in each
1432                direction. */
1433
1434             /* how far we are towards the next size inc */
1435             int mw = w % self->size_inc.width; 
1436             int mh = h % self->size_inc.height;
1437             /* amount to add */
1438             int aw = self->size_inc.width / 2;
1439             int ah = self->size_inc.height / 2;
1440             /* don't let us move into a new size increment */
1441             if (mw + aw >= self->size_inc.width)
1442                 aw = self->size_inc.width - mw - 1;
1443             if (mh + ah >= self->size_inc.height)
1444                 ah = self->size_inc.height - mh - 1;
1445             w += aw;
1446             h += ah;
1447     
1448             /* if this is a user-requested resize, then check against min/max
1449                sizes and aspect ratios */
1450
1451             /* smaller than min size or bigger than max size? */
1452             if (w > self->max_size.width) w = self->max_size.width;
1453             if (w < self->min_size.width) w = self->min_size.width;
1454             if (h > self->max_size.height) h = self->max_size.height;
1455             if (h < self->min_size.height) h = self->min_size.height;
1456
1457             /* adjust the height ot match the width for the aspect ratios */
1458             if (self->min_ratio)
1459                 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1460             if (self->max_ratio)
1461                 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1462         }
1463
1464         /* keep to the increments */
1465         w /= self->size_inc.width;
1466         h /= self->size_inc.height;
1467
1468         /* you cannot resize to nothing */
1469         if (w < 1) w = 1;
1470         if (h < 1) h = 1;
1471   
1472         /* store the logical size */
1473         SIZE_SET(self->logical_size, w, h);
1474
1475         w *= self->size_inc.width;
1476         h *= self->size_inc.height;
1477
1478         w += self->base_size.width;
1479         h += self->base_size.height;
1480     }
1481
1482     switch (anchor) {
1483     case Corner_TopLeft:
1484         break;
1485     case Corner_TopRight:
1486         x -= w - self->area.width;
1487         break;
1488     case Corner_BottomLeft:
1489         y -= h - self->area.height;
1490         break;
1491     case Corner_BottomRight:
1492         x -= w - self->area.width;
1493         y -= h - self->area.height;
1494         break;
1495     }
1496
1497     moved = x != self->area.x || y != self->area.y;
1498     resized = w != self->area.width || h != self->area.height;
1499
1500     RECT_SET(self->area, x, y, w, h);
1501
1502     if (resized)
1503         XResizeWindow(ob_display, self->window, w, h);
1504
1505     /* move/resize the frame to match the request */
1506     if (self->frame) {
1507         if (moved || resized)
1508             engine_frame_adjust_area(self->frame, moved, resized);
1509
1510         if (!user || final) {
1511             XEvent event;
1512             event.type = ConfigureNotify;
1513             event.xconfigure.display = ob_display;
1514             event.xconfigure.event = self->window;
1515             event.xconfigure.window = self->window;
1516     
1517             /* root window coords with border in mind */
1518             event.xconfigure.x = x - self->border_width +
1519                 self->frame->size.left;
1520             event.xconfigure.y = y - self->border_width +
1521                 self->frame->size.top;
1522     
1523             event.xconfigure.width = self->area.width;
1524             event.xconfigure.height = self->area.height;
1525             event.xconfigure.border_width = self->border_width;
1526             event.xconfigure.above = self->frame->plate;
1527             event.xconfigure.override_redirect = FALSE;
1528             XSendEvent(event.xconfigure.display, event.xconfigure.window,
1529                        FALSE, StructureNotifyMask, &event);
1530         }
1531     }
1532 }
1533
1534 void client_fullscreen(Client *self, gboolean fs, gboolean savearea)
1535 {
1536     int x, y, w, h;
1537
1538     if (!(self->functions & Func_Fullscreen) || /* can't */
1539         self->fullscreen == fs) return;         /* already done */
1540
1541     self->fullscreen = fs;
1542     client_change_state(self); /* change the state hints on the client */
1543
1544     if (fs) {
1545         /* save the functions and remove them */
1546         self->pre_fs_func = self->functions;
1547         self->functions &= (Func_Close | Func_Fullscreen |
1548                             Func_Iconify);
1549         /* save the decorations and remove them */
1550         self->pre_fs_decor = self->decorations;
1551         self->decorations = 0;
1552         if (savearea) {
1553             long dimensions[4];
1554             dimensions[0] = self->area.x;
1555             dimensions[1] = self->area.y;
1556             dimensions[2] = self->area.width;
1557             dimensions[3] = self->area.height;
1558   
1559             PROP_SET32A(self->window, openbox_premax, cardinal,
1560                         dimensions, 4);
1561         }
1562
1563         /* these are not actually used cuz client_configure will set them
1564            as appropriate when the window is fullscreened */
1565         x = y = w = h = 0;
1566     } else {
1567         long *dimensions;
1568
1569         self->functions = self->pre_fs_func;
1570         self->decorations = self->pre_fs_decor;
1571           
1572         if (PROP_GET32A(self->window, openbox_premax, cardinal,
1573                         dimensions, 4)) {
1574             x = dimensions[0];
1575             y = dimensions[1];
1576             w = dimensions[2];
1577             h = dimensions[3];
1578             g_free(dimensions);
1579         } else {
1580             /* pick some fallbacks... */
1581             x = screen_area(self->desktop)->x +
1582                 screen_area(self->desktop)->width / 4;
1583             y = screen_area(self->desktop)->y +
1584                 screen_area(self->desktop)->height / 4;
1585             w = screen_area(self->desktop)->width / 2;
1586             h = screen_area(self->desktop)->height / 2;
1587         }
1588     }
1589
1590     client_change_allowed_actions(self); /* based on the new _functions */
1591
1592     /* when fullscreening, don't obey things like increments, fill the
1593        screen */
1594     client_configure(self, Corner_TopLeft, x, y, w, h, !fs, TRUE);
1595
1596     /* raise (back) into our stacking layer */
1597     stacking_raise(self);
1598
1599     /* try focus us when we go into fullscreen mode */
1600     client_focus(self);
1601 }
1602
1603 void client_iconify(Client *self, gboolean iconic, gboolean curdesk)
1604 {
1605     if (self->iconic == iconic) return; /* nothing to do */
1606
1607     g_message("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
1608               self->window);
1609
1610     self->iconic = iconic;
1611
1612     if (iconic) {
1613         self->wmstate = IconicState;
1614         self->ignore_unmaps++;
1615         /* we unmap the client itself so that we can get MapRequest events,
1616            and because the ICCCM tells us to! */
1617         XUnmapWindow(ob_display, self->window);
1618     } else {
1619         if (curdesk)
1620             client_set_desktop(self, screen_desktop, FALSE);
1621         self->wmstate = self->shaded ? IconicState : NormalState;
1622         XMapWindow(ob_display, self->window);
1623     }
1624     client_change_state(self);
1625     client_showhide(self);
1626     screen_update_struts();
1627
1628     dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
1629                     self, 0, 0);
1630
1631     /* iconify all transients */
1632     if (self->transients) {
1633         GSList *it;
1634
1635         for (it = self->transients; it != NULL; it = it->next)
1636             if (it->data != self) client_iconify(it->data, iconic, curdesk);
1637     }
1638 }
1639
1640 void client_maximize(Client *self, gboolean max, int dir, gboolean savearea)
1641 {
1642     int x, y, w, h;
1643      
1644     g_assert(dir == 0 || dir == 1 || dir == 2);
1645     if (!(self->functions & Func_Maximize)) return; /* can't */
1646
1647     /* check if already done */
1648     if (max) {
1649         if (dir == 0 && self->max_horz && self->max_vert) return;
1650         if (dir == 1 && self->max_horz) return;
1651         if (dir == 2 && self->max_vert) return;
1652     } else {
1653         if (dir == 0 && !self->max_horz && !self->max_vert) return;
1654         if (dir == 1 && !self->max_horz) return;
1655         if (dir == 2 && !self->max_vert) return;
1656     }
1657
1658     /* work with the frame's coords */
1659     x = self->frame->area.x;
1660     y = self->frame->area.y;
1661     w = self->area.width;
1662     h = self->area.height;
1663
1664     if (max) {
1665         if (savearea) {
1666             long dimensions[4];
1667             long *readdim;
1668
1669             dimensions[0] = x;
1670             dimensions[1] = y;
1671             dimensions[2] = w;
1672             dimensions[3] = h;
1673
1674             /* get the property off the window and use it for the dimensions
1675                we are already maxed on */
1676             if (PROP_GET32A(self->window, openbox_premax, cardinal,
1677                             readdim, 4)) {
1678                 if (self->max_horz) {
1679                     dimensions[0] = readdim[0];
1680                     dimensions[2] = readdim[2];
1681                 }
1682                 if (self->max_vert) {
1683                     dimensions[1] = readdim[1];
1684                     dimensions[3] = readdim[3];
1685                 }
1686                 g_free(readdim);
1687             }
1688
1689             PROP_SET32A(self->window, openbox_premax, cardinal,
1690                         dimensions, 4);
1691         }
1692     } else {
1693         long *dimensions;
1694
1695         if (PROP_GET32A(self->window, openbox_premax, cardinal,
1696                         dimensions, 4)) {
1697             if (dir == 0 || dir == 1) { /* horz */
1698                 x = dimensions[0];
1699                 w = dimensions[2];
1700             }
1701             if (dir == 0 || dir == 2) { /* vert */
1702                 y = dimensions[1];
1703                 h = dimensions[3];
1704             }
1705             g_free(dimensions);
1706         } else {
1707             /* pick some fallbacks... */
1708             if (dir == 0 || dir == 1) { /* horz */
1709                 x = screen_area(self->desktop)->x +
1710                     screen_area(self->desktop)->width / 4;
1711                 w = screen_area(self->desktop)->width / 2;
1712             }
1713             if (dir == 0 || dir == 2) { /* vert */
1714                 y = screen_area(self->desktop)->y +
1715                     screen_area(self->desktop)->height / 4;
1716                 h = screen_area(self->desktop)->height / 2;
1717             }
1718         }
1719     }
1720
1721     if (dir == 0 || dir == 1) /* horz */
1722         self->max_horz = max;
1723     if (dir == 0 || dir == 2) /* vert */
1724         self->max_vert = max;
1725
1726     if (!self->max_horz && !self->max_vert)
1727         PROP_ERASE(self->window, openbox_premax);
1728
1729     client_change_state(self); /* change the state hints on the client */
1730
1731     /* figure out where the client should be going */
1732     frame_frame_gravity(self->frame, &x, &y);
1733     client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1734 }
1735
1736 void client_shade(Client *self, gboolean shade)
1737 {
1738     if ((!(self->functions & Func_Shade) && shade) || /* can't shade */
1739         self->shaded == shade) return;     /* already done */
1740
1741     /* when we're iconic, don't change the wmstate */
1742     if (!self->iconic)
1743         self->wmstate = shade ? IconicState : NormalState;
1744     self->shaded = shade;
1745     client_change_state(self);
1746     /* resize the frame to just the titlebar */
1747     engine_frame_adjust_area(self->frame, FALSE, FALSE);
1748 }
1749
1750 void client_close(Client *self)
1751 {
1752     XEvent ce;
1753
1754     if (!(self->functions & Func_Close)) return;
1755
1756     /*
1757       XXX: itd be cool to do timeouts and shit here for killing the client's
1758       process off
1759       like... if the window is around after 5 seconds, then the close button
1760       turns a nice red, and if this function is called again, the client is
1761       explicitly killed.
1762     */
1763
1764     ce.xclient.type = ClientMessage;
1765     ce.xclient.message_type =  prop_atoms.wm_protocols;
1766     ce.xclient.display = ob_display;
1767     ce.xclient.window = self->window;
1768     ce.xclient.format = 32;
1769     ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
1770     ce.xclient.data.l[1] = event_lasttime;
1771     ce.xclient.data.l[2] = 0l;
1772     ce.xclient.data.l[3] = 0l;
1773     ce.xclient.data.l[4] = 0l;
1774     XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
1775 }
1776
1777 void client_kill(Client *self)
1778 {
1779     XKillClient(ob_display, self->window);
1780 }
1781
1782 void client_set_desktop(Client *self, guint target, gboolean donthide)
1783 {
1784     guint old, i;
1785
1786     if (target == self->desktop) return;
1787   
1788     g_message("Setting desktop %u", target);
1789
1790     g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
1791
1792     old = self->desktop;
1793     self->desktop = target;
1794     PROP_SET32(self->window, net_wm_desktop, cardinal, target);
1795     /* the frame can display the current desktop state */
1796     engine_frame_adjust_state(self->frame);
1797     /* 'move' the window to the new desktop */
1798     if (!donthide)
1799         client_showhide(self);
1800     /* raise if it was not already on the desktop */
1801     if (old != DESKTOP_ALL)
1802         stacking_raise(self);
1803     screen_update_struts();
1804
1805     /* update the focus lists */
1806     if (old == DESKTOP_ALL) {
1807         for (i = 0; i < screen_num_desktops; ++i)
1808             focus_order[i] = g_list_remove(focus_order[i], self);
1809     } else
1810         focus_order[old] = g_list_remove(focus_order[old], self);
1811     if (target == DESKTOP_ALL) {
1812         for (i = 0; i < screen_num_desktops; ++i) {
1813             if (focus_new)
1814                 focus_order[i] = g_list_prepend(focus_order[i], self);
1815             else
1816                 focus_order[i] = g_list_append(focus_order[i], self);
1817         }
1818     } else {
1819         if (focus_new)
1820             focus_order[target] = g_list_prepend(focus_order[target], self);
1821         else
1822             focus_order[target] = g_list_append(focus_order[target], self);
1823     }
1824
1825     dispatch_client(Event_Client_Desktop, self, target, old);
1826 }
1827
1828 static Client *search_modal_tree(Client *node, Client *skip)
1829 {
1830     GSList *it;
1831     Client *ret;
1832   
1833     for (it = node->transients; it != NULL; it = it->next) {
1834         Client *c = it->data;
1835         if (c == skip) continue; /* circular? */
1836         if ((ret = search_modal_tree(c, skip))) return ret;
1837         if (c->modal) return c;
1838     }
1839     return NULL;
1840 }
1841
1842 Client *client_find_modal_child(Client *self)
1843 {
1844     return search_modal_tree(self, self);
1845 }
1846
1847 gboolean client_validate(Client *self)
1848 {
1849     XEvent e; 
1850
1851     XSync(ob_display, FALSE); /* get all events on the server */
1852
1853     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
1854         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
1855         XPutBackEvent(ob_display, &e);
1856         return FALSE;
1857     }
1858
1859     return TRUE;
1860 }
1861
1862 void client_set_wm_state(Client *self, long state)
1863 {
1864     if (state == self->wmstate) return; /* no change */
1865   
1866     switch (state) {
1867     case IconicState:
1868         client_iconify(self, TRUE, TRUE);
1869         break;
1870     case NormalState:
1871         client_iconify(self, FALSE, TRUE);
1872         break;
1873     }
1874 }
1875
1876 void client_set_state(Client *self, Atom action, long data1, long data2)
1877 {
1878     gboolean shaded = self->shaded;
1879     gboolean fullscreen = self->fullscreen;
1880     gboolean max_horz = self->max_horz;
1881     gboolean max_vert = self->max_vert;
1882     int i;
1883
1884     if (!(action == prop_atoms.net_wm_state_add ||
1885           action == prop_atoms.net_wm_state_remove ||
1886           action == prop_atoms.net_wm_state_toggle))
1887         /* an invalid action was passed to the client message, ignore it */
1888         return; 
1889
1890     for (i = 0; i < 2; ++i) {
1891         Atom state = i == 0 ? data1 : data2;
1892     
1893         if (!state) continue;
1894
1895         /* if toggling, then pick whether we're adding or removing */
1896         if (action == prop_atoms.net_wm_state_toggle) {
1897             if (state == prop_atoms.net_wm_state_modal)
1898                 action = self->modal ? prop_atoms.net_wm_state_remove :
1899                     prop_atoms.net_wm_state_add;
1900             else if (state == prop_atoms.net_wm_state_maximized_vert)
1901                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
1902                     prop_atoms.net_wm_state_add;
1903             else if (state == prop_atoms.net_wm_state_maximized_horz)
1904                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
1905                     prop_atoms.net_wm_state_add;
1906             else if (state == prop_atoms.net_wm_state_shaded)
1907                 action = self->shaded ? prop_atoms.net_wm_state_remove :
1908                     prop_atoms.net_wm_state_add;
1909             else if (state == prop_atoms.net_wm_state_skip_taskbar)
1910                 action = self->skip_taskbar ?
1911                     prop_atoms.net_wm_state_remove :
1912                     prop_atoms.net_wm_state_add;
1913             else if (state == prop_atoms.net_wm_state_skip_pager)
1914                 action = self->skip_pager ?
1915                     prop_atoms.net_wm_state_remove :
1916                     prop_atoms.net_wm_state_add;
1917             else if (state == prop_atoms.net_wm_state_fullscreen)
1918                 action = self->fullscreen ?
1919                     prop_atoms.net_wm_state_remove :
1920                     prop_atoms.net_wm_state_add;
1921             else if (state == prop_atoms.net_wm_state_above)
1922                 action = self->above ? prop_atoms.net_wm_state_remove :
1923                     prop_atoms.net_wm_state_add;
1924             else if (state == prop_atoms.net_wm_state_below)
1925                 action = self->below ? prop_atoms.net_wm_state_remove :
1926                     prop_atoms.net_wm_state_add;
1927         }
1928     
1929         if (action == prop_atoms.net_wm_state_add) {
1930             if (state == prop_atoms.net_wm_state_modal) {
1931                 /* XXX raise here or something? */
1932                 self->modal = TRUE;
1933             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
1934                 max_vert = TRUE;
1935             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
1936                 max_horz = TRUE;
1937             } else if (state == prop_atoms.net_wm_state_shaded) {
1938                 shaded = TRUE;
1939             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
1940                 self->skip_taskbar = TRUE;
1941             } else if (state == prop_atoms.net_wm_state_skip_pager) {
1942                 self->skip_pager = TRUE;
1943             } else if (state == prop_atoms.net_wm_state_fullscreen) {
1944                 fullscreen = TRUE;
1945             } else if (state == prop_atoms.net_wm_state_above) {
1946                 self->above = TRUE;
1947             } else if (state == prop_atoms.net_wm_state_below) {
1948                 self->below = TRUE;
1949             }
1950
1951         } else { /* action == prop_atoms.net_wm_state_remove */
1952             if (state == prop_atoms.net_wm_state_modal) {
1953                 self->modal = FALSE;
1954             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
1955                 max_vert = FALSE;
1956             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
1957                 max_horz = FALSE;
1958             } else if (state == prop_atoms.net_wm_state_shaded) {
1959                 shaded = FALSE;
1960             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
1961                 self->skip_taskbar = FALSE;
1962             } else if (state == prop_atoms.net_wm_state_skip_pager) {
1963                 self->skip_pager = FALSE;
1964             } else if (state == prop_atoms.net_wm_state_fullscreen) {
1965                 fullscreen = FALSE;
1966             } else if (state == prop_atoms.net_wm_state_above) {
1967                 self->above = FALSE;
1968             } else if (state == prop_atoms.net_wm_state_below) {
1969                 self->below = FALSE;
1970             }
1971         }
1972     }
1973     if (max_horz != self->max_horz || max_vert != self->max_vert) {
1974         if (max_horz != self->max_horz && max_vert != self->max_vert) {
1975             /* toggling both */
1976             if (max_horz == max_vert) { /* both going the same way */
1977                 client_maximize(self, max_horz, 0, TRUE);
1978             } else {
1979                 client_maximize(self, max_horz, 1, TRUE);
1980                 client_maximize(self, max_vert, 2, TRUE);
1981             }
1982         } else {
1983             /* toggling one */
1984             if (max_horz != self->max_horz)
1985                 client_maximize(self, max_horz, 1, TRUE);
1986             else
1987                 client_maximize(self, max_vert, 2, TRUE);
1988         }
1989     }
1990     /* change fullscreen state before shading, as it will affect if the window
1991        can shade or not */
1992     if (fullscreen != self->fullscreen)
1993         client_fullscreen(self, fullscreen, TRUE);
1994     if (shaded != self->shaded)
1995         client_shade(self, shaded);
1996     client_calc_layer(self);
1997     client_change_state(self); /* change the hint to relect these changes */
1998 }
1999
2000 Client *client_focus_target(Client *self)
2001 {
2002     Client *child;
2003      
2004     /* if we have a modal child, then focus it, not us */
2005     child = client_find_modal_child(self);
2006     if (child) return child;
2007     return self;
2008 }
2009
2010 gboolean client_focusable(Client *self)
2011 {
2012     /* won't try focus if the client doesn't want it, or if the window isn't
2013        visible on the screen */
2014     return self->frame->visible &&
2015         (self->can_focus || self->focus_notify);
2016 }
2017
2018 gboolean client_focus(Client *self)
2019 {
2020     XEvent ev;
2021
2022     /* choose the correct target */
2023     self = client_focus_target(self);
2024
2025     if (!client_focusable(self))
2026         return FALSE;
2027
2028     /* do a check to see if the window has already been unmapped or destroyed
2029        do this intelligently while watching out for unmaps we've generated
2030        (ignore_unmaps > 0) */
2031     if (XCheckTypedWindowEvent(ob_display, self->window,
2032                                DestroyNotify, &ev)) {
2033         XPutBackEvent(ob_display, &ev);
2034         return FALSE;
2035     }
2036     while (XCheckTypedWindowEvent(ob_display, self->window,
2037                                   UnmapNotify, &ev)) {
2038         if (self->ignore_unmaps) {
2039             self->ignore_unmaps--;
2040         } else {
2041             XPutBackEvent(ob_display, &ev);
2042             return FALSE;
2043         }
2044     }
2045
2046     if (self->can_focus)
2047         /* RevertToPointerRoot causes much more headache than TevertToNone, so
2048            I choose to use it always, hopefully to find errors quicker, if any
2049            are left. (I hate X. I hate focus events.) */
2050         XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2051                        event_lasttime);
2052
2053     if (self->focus_notify) {
2054         XEvent ce;
2055         ce.xclient.type = ClientMessage;
2056         ce.xclient.message_type = prop_atoms.wm_protocols;
2057         ce.xclient.display = ob_display;
2058         ce.xclient.window = self->window;
2059         ce.xclient.format = 32;
2060         ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2061         ce.xclient.data.l[1] = event_lasttime;
2062         ce.xclient.data.l[2] = 0l;
2063         ce.xclient.data.l[3] = 0l;
2064         ce.xclient.data.l[4] = 0l;
2065         XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2066     }
2067
2068 #ifdef DEBUG_FOCUS
2069     g_message("focusing %lx", self->window);
2070 #endif
2071
2072     /* Cause the FocusIn to come back to us. Important for desktop switches,
2073        since otherwise we'll have no FocusIn on the queue and send it off to
2074        the focus_backup. */
2075     XSync(ob_display, FALSE);
2076     return TRUE;
2077 }
2078
2079 void client_unfocus(Client *self)
2080 {
2081     g_assert(focus_client == self);
2082     g_message("client_unfocus");
2083     focus_fallback(FALSE);
2084 }
2085
2086 gboolean client_focused(Client *self)
2087 {
2088     return self == focus_client;
2089 }
2090
2091 Icon *client_icon(Client *self, int w, int h)
2092 {
2093     int i;
2094     /* si is the smallest image >= req */
2095     /* li is the largest image < req */
2096     unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2097
2098     if (!self->nicons) return NULL;
2099
2100     for (i = 0; i < self->nicons; ++i) {
2101         size = self->icons[i].width * self->icons[i].height;
2102         if (size < smallest && size >= (unsigned)(w * h)) {
2103             smallest = size;
2104             si = i;
2105         }
2106         if (size > largest && size <= (unsigned)(w * h)) {
2107             largest = size;
2108             li = i;
2109         }
2110     }
2111     if (largest == 0) /* didnt find one smaller than the requested size */
2112         return &self->icons[si];
2113     return &self->icons[li];
2114 }