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