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