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