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