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