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