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