]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/client.c
when restoring from show-desktop mode because a window is going to be focused, then...
[mikachu/openbox.git] / openbox / client.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2    
3    client.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "client.h"
21 #include "debug.h"
22 #include "startupnotify.h"
23 #include "dock.h"
24 #include "xerror.h"
25 #include "screen.h"
26 #include "moveresize.h"
27 #include "place.h"
28 #include "prop.h"
29 #include "extensions.h"
30 #include "frame.h"
31 #include "session.h"
32 #include "event.h"
33 #include "grab.h"
34 #include "focus.h"
35 #include "stacking.h"
36 #include "openbox.h"
37 #include "group.h"
38 #include "config.h"
39 #include "menuframe.h"
40 #include "keyboard.h"
41 #include "mouse.h"
42 #include "render/render.h"
43
44 #ifdef HAVE_UNISTD_H
45 #  include <unistd.h>
46 #endif
47
48 #include <glib.h>
49 #include <X11/Xutil.h>
50
51 /*! The event mask to grab on client windows */
52 #define CLIENT_EVENTMASK (PropertyChangeMask | StructureNotifyMask | \
53                           ColormapChangeMask)
54
55 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
56                                 ButtonMotionMask)
57
58 typedef struct
59 {
60     ObClientDestructor func;
61     gpointer data;
62 } Destructor;
63
64 GList            *client_list        = NULL;
65
66 static GSList *client_destructors    = NULL;
67
68 static void client_get_all(ObClient *self);
69 static void client_toggle_border(ObClient *self, gboolean show);
70 static void client_get_startup_id(ObClient *self);
71 static void client_get_area(ObClient *self);
72 static void client_get_desktop(ObClient *self);
73 static void client_get_state(ObClient *self);
74 static void client_get_layer(ObClient *self);
75 static void client_get_shaped(ObClient *self);
76 static void client_get_mwm_hints(ObClient *self);
77 static void client_get_gravity(ObClient *self);
78 static void client_get_client_machine(ObClient *self);
79 static void client_get_colormap(ObClient *self);
80 static void client_get_transientness(ObClient *self);
81 static void client_change_allowed_actions(ObClient *self);
82 static void client_change_state(ObClient *self);
83 static void client_change_wm_state(ObClient *self);
84 static void client_apply_startup_state(ObClient *self, gint x, gint y);
85 static void client_restore_session_state(ObClient *self);
86 static void client_restore_session_stacking(ObClient *self);
87 static ObAppSettings *client_get_settings_state(ObClient *self);
88 static void client_update_transient_tree(ObClient *self,
89                                          ObGroup *oldgroup, ObGroup *newgroup,
90                                          ObClient* oldparent,
91                                          ObClient *newparent);
92
93 void client_startup(gboolean reconfig)
94 {
95     if (reconfig) return;
96
97     client_set_list();
98 }
99
100 void client_shutdown(gboolean reconfig)
101 {
102 }
103
104 void client_add_destructor(ObClientDestructor func, gpointer data)
105 {
106     Destructor *d = g_new(Destructor, 1);
107     d->func = func;
108     d->data = data;
109     client_destructors = g_slist_prepend(client_destructors, d);
110 }
111
112 void client_remove_destructor(ObClientDestructor func)
113 {
114     GSList *it;
115
116     for (it = client_destructors; it; it = g_slist_next(it)) {
117         Destructor *d = it->data;
118         if (d->func == func) {
119             g_free(d);
120             client_destructors = g_slist_delete_link(client_destructors, it);
121             break;
122         }
123     }
124 }
125
126 void client_set_list()
127 {
128     Window *windows, *win_it;
129     GList *it;
130     guint size = g_list_length(client_list);
131
132     /* create an array of the window ids */
133     if (size > 0) {
134         windows = g_new(Window, size);
135         win_it = windows;
136         for (it = client_list; it; it = g_list_next(it), ++win_it)
137             *win_it = ((ObClient*)it->data)->window;
138     } else
139         windows = NULL;
140
141     PROP_SETA32(RootWindow(ob_display, ob_screen),
142                 net_client_list, window, (gulong*)windows, size);
143
144     if (windows)
145         g_free(windows);
146
147     stacking_set_list();
148 }
149
150 /*
151   void client_foreach_transient(ObClient *self, ObClientForeachFunc func, gpointer data)
152   {
153   GSList *it;
154
155   for (it = self->transients; it; it = g_slist_next(it)) {
156   if (!func(it->data, data)) return;
157   client_foreach_transient(it->data, func, data);
158   }
159   }
160
161   void client_foreach_ancestor(ObClient *self, ObClientForeachFunc func, gpointer data)
162   {
163   if (self->transient_for) {
164   if (self->transient_for != OB_TRAN_GROUP) {
165   if (!func(self->transient_for, data)) return;
166   client_foreach_ancestor(self->transient_for, func, data);
167   } else {
168   GSList *it;
169
170   for (it = self->group->members; it; it = g_slist_next(it))
171   if (it->data != self &&
172   !((ObClient*)it->data)->transient_for) {
173   if (!func(it->data, data)) return;
174   client_foreach_ancestor(it->data, func, data);
175   }
176   }
177   }
178   }
179 */
180
181 void client_manage_all()
182 {
183     guint i, j, nchild;
184     Window w, *children;
185     XWMHints *wmhints;
186     XWindowAttributes attrib;
187
188     XQueryTree(ob_display, RootWindow(ob_display, ob_screen),
189                &w, &w, &children, &nchild);
190
191     /* remove all icon windows from the list */
192     for (i = 0; i < nchild; i++) {
193         if (children[i] == None) continue;
194         wmhints = XGetWMHints(ob_display, children[i]);
195         if (wmhints) {
196             if ((wmhints->flags & IconWindowHint) &&
197                 (wmhints->icon_window != children[i]))
198                 for (j = 0; j < nchild; j++)
199                     if (children[j] == wmhints->icon_window) {
200                         children[j] = None;
201                         break;
202                     }
203             XFree(wmhints);
204         }
205     }
206
207     for (i = 0; i < nchild; ++i) {
208         if (children[i] == None)
209             continue;
210         if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
211             if (attrib.override_redirect) continue;
212
213             if (attrib.map_state != IsUnmapped)
214                 client_manage(children[i]);
215         }
216     }
217     XFree(children);
218 }
219
220 void client_manage(Window window)
221 {
222     ObClient *self;
223     XEvent e;
224     XWindowAttributes attrib;
225     XSetWindowAttributes attrib_set;
226     XWMHints *wmhint;
227     gboolean activate = FALSE;
228     ObAppSettings *settings;
229     gint newx, newy;
230
231     grab_server(TRUE);
232
233     /* check if it has already been unmapped by the time we started mapping.
234        the grab does a sync so we don't have to here */
235     if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
236         XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e))
237     {
238         XPutBackEvent(ob_display, &e);
239
240         ob_debug("Trying to manage unmapped window. Aborting that.\n");
241         grab_server(FALSE);
242         return; /* don't manage it */
243     }
244
245     /* make sure it isn't an override-redirect window */
246     if (!XGetWindowAttributes(ob_display, window, &attrib) ||
247         attrib.override_redirect)
248     {
249         grab_server(FALSE);
250         return; /* don't manage it */
251     }
252   
253     /* is the window a docking app */
254     if ((wmhint = XGetWMHints(ob_display, window))) {
255         if ((wmhint->flags & StateHint) &&
256             wmhint->initial_state == WithdrawnState)
257         {
258             dock_add(window, wmhint);
259             grab_server(FALSE);
260             XFree(wmhint);
261             return;
262         }
263         XFree(wmhint);
264     }
265
266     ob_debug("Managing window: %lx\n", window);
267
268     /* choose the events we want to receive on the CLIENT window */
269     attrib_set.event_mask = CLIENT_EVENTMASK;
270     attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
271     XChangeWindowAttributes(ob_display, window,
272                             CWEventMask|CWDontPropagate, &attrib_set);
273
274
275     /* create the ObClient struct, and populate it from the hints on the
276        window */
277     self = g_new0(ObClient, 1);
278     self->obwin.type = Window_Client;
279     self->window = window;
280
281     /* non-zero defaults */
282     self->wmstate = WithdrawnState; /* make sure it gets updated first time */
283     self->layer = -1;
284     self->desktop = screen_num_desktops; /* always an invalid value */
285     self->user_time = focus_client ? focus_client->user_time : CurrentTime;
286
287     client_get_all(self);
288     /* per-app settings override stuff, and return the settings for other
289        uses too */
290     settings = client_get_settings_state(self);
291     /* the session should get the last say */
292     client_restore_session_state(self);
293
294     client_calc_layer(self);
295
296     {
297         Time t = sn_app_started(self->startup_id, self->class);
298         if (t) self->user_time = t;
299     }
300
301     /* update the focus lists, do this before the call to change_state or
302        it can end up in the list twice! */
303     focus_order_add_new(self);
304
305     /* remove the client's border (and adjust re gravity) */
306     client_toggle_border(self, FALSE);
307      
308     /* specify that if we exit, the window should not be destroyed and should
309        be reparented back to root automatically */
310     XChangeSaveSet(ob_display, window, SetModeInsert);
311
312     /* create the decoration frame for the client window */
313     self->frame = frame_new(self);
314
315     frame_grab_client(self->frame, self);
316
317     /* do this after we have a frame.. it uses the frame to help determine the
318        WM_STATE to apply. */
319     client_change_state(self);
320
321     grab_server(FALSE);
322
323     stacking_add_nonintrusive(CLIENT_AS_WINDOW(self));
324     client_restore_session_stacking(self);
325
326     /* focus the new window? */
327     if (ob_state() != OB_STATE_STARTING &&
328         !self->iconic &&
329         /* this means focus=true for window is same as config_focus_new=true */
330         ((config_focus_new || (settings && settings->focus == 1)) ||
331          client_search_focus_parent(self)) &&
332         /* this checks for focus=false for the window */
333         (!settings || settings->focus != 0) &&
334         /* note the check against Type_Normal/Dialog, not client_normal(self),
335            which would also include other types. in this case we want more
336            strict rules for focus */
337         (self->type == OB_CLIENT_TYPE_NORMAL ||
338          self->type == OB_CLIENT_TYPE_DIALOG))
339     {
340         activate = TRUE;
341     }
342
343     /* get the current position */
344     newx = self->area.x;
345     newy = self->area.y;
346
347     /* figure out placement for the window */
348     if (ob_state() == OB_STATE_RUNNING) {
349         gboolean transient;
350
351         transient = place_client(self, &newx, &newy, settings);
352
353         /* make sure the window is visible. */
354         client_find_onscreen(self, &newx, &newy,
355                              self->area.width,
356                              self->area.height,
357                              /* non-normal clients has less rules, and
358                                 windows that are being restored from a
359                                 session do also. we can assume you want
360                                 it back where you saved it. Clients saying
361                                 they placed themselves are subjected to
362                                 harder rules, ones that are placed by
363                                 place.c or by the user are allowed partially
364                                 off-screen and on xinerama divides (ie,
365                                 it is up to the placement routines to avoid
366                                 the xinerama divides) */
367                              transient ||
368                              (((self->positioned & PPosition) &&
369                                !(self->positioned & USPosition)) &&
370                               client_normal(self) &&
371                               !self->session));
372     }
373
374     /* do this after the window is placed, so the premax/prefullscreen numbers
375        won't be all wacko!!
376        also, this moves the window to the position where it has been placed
377     */
378     ob_debug("placing window 0x%x at %d, %d with size %d x %d\n",
379              self->window, newx, newy, self->area.width, self->area.height);
380     client_apply_startup_state(self, newx, newy);
381
382     mouse_grab_for_client(self, TRUE);
383
384     if (activate) {
385         guint32 last_time = focus_client ?
386             focus_client->user_time : CurrentTime;
387
388         /* This is focus stealing prevention */
389         ob_debug_type(OB_DEBUG_FOCUS,
390                       "Want to focus new window 0x%x with time %u "
391                       "(last time %u)\n",
392                       self->window, self->user_time, last_time);
393
394         /* if it's on another desktop */
395         if (!(self->desktop == screen_desktop || self->desktop == DESKTOP_ALL)
396             && /* the timestamp is from before you changed desktops */
397             self->user_time && screen_desktop_user_time &&
398             !event_time_after(self->user_time, screen_desktop_user_time))
399         {
400             activate = FALSE;
401             ob_debug_type(OB_DEBUG_FOCUS,
402                           "Not focusing the window because its on another "
403                           "desktop\n");
404         }
405         /* If something is focused, and it's not our parent... */
406         else if (focus_client && client_search_focus_parent(self) == NULL)
407         {
408             /* If time stamp is old, don't steal focus */
409             if (self->user_time && last_time &&
410                 !event_time_after(self->user_time, last_time))
411             {
412                 activate = FALSE;
413                 ob_debug_type(OB_DEBUG_FOCUS,
414                               "Not focusing the window because the time is "
415                               "too old\n");
416             }
417             /* Don't steal focus from globally active clients.
418                I stole this idea from KWin. It seems nice.
419              */
420             if (!(focus_client->can_focus || focus_client->focus_notify)) {
421                 activate = FALSE;
422                 ob_debug_type(OB_DEBUG_FOCUS,
423                               "Not focusing the window because a globally "
424                               "active client has focus\n");
425             }
426         }
427
428         if (!activate) {
429             ob_debug_type(OB_DEBUG_FOCUS,
430                           "Focus stealing prevention activated for %s with "
431                           "time %u (last time %u)\n",
432                           self->title, self->user_time, last_time);
433             /* if the client isn't focused, then hilite it so the user
434                knows it is there */
435             client_hilite(self, TRUE);
436         }
437     }
438     else {
439         /* This may look rather odd. Well it's because new windows are added
440            to the stacking order non-intrusively. If we're not going to focus
441            the new window or hilite it, then we raise it to the top. This will
442            take affect for things that don't get focused like splash screens.
443            Also if you don't have focus_new enabled, then it's going to get
444            raised to the top. Legacy begets legacy I guess?
445         */
446         client_raise(self);
447     }
448
449     /* this has to happen before we try focus the window, but we want it to
450        happen after the client's stacking has been determined or it looks bad
451     */
452     client_show(self);
453
454     /* use client_focus instead of client_activate cuz client_activate does
455        stuff like switch desktops etc and I'm not interested in all that when
456        a window maps since its not based on an action from the user like
457        clicking a window to activate it. so keep the new window out of the way
458        but do focus it. */
459     if (activate)
460         client_activate(self, FALSE, TRUE);
461
462     /* add to client list/map */
463     client_list = g_list_append(client_list, self);
464     g_hash_table_insert(window_map, &self->window, self);
465
466     /* this has to happen after we're in the client_list */
467     if (STRUT_EXISTS(self->strut))
468         screen_update_areas();
469
470     /* update the list hints */
471     client_set_list();
472
473     ob_debug("Managed window 0x%lx (%s)\n", window, self->class);
474 }
475
476 void client_unmanage_all()
477 {
478     while (client_list != NULL)
479         client_unmanage(client_list->data);
480 }
481
482 void client_unmanage(ObClient *self)
483 {
484     guint j;
485     GSList *it;
486
487     ob_debug("Unmanaging window: %lx (%s) (%s)\n", self->window, self->class,
488              self->title ? self->title : "");
489
490     g_assert(self != NULL);
491
492     /* we dont want events no more. do this before hiding the frame so we
493        don't generate more events */
494     XSelectInput(ob_display, self->window, NoEventMask);
495
496     frame_hide(self->frame);
497     /* flush to send the hide to the server quickly */
498     XFlush(ob_display);
499
500     /* ignore enter events from the unmap so it doesnt mess with the focus */
501     event_ignore_queued_enters();
502
503     mouse_grab_for_client(self, FALSE);
504
505     /* remove the window from our save set */
506     XChangeSaveSet(ob_display, self->window, SetModeDelete);
507
508     /* update the focus lists */
509     focus_order_remove(self);
510     if (client_focused(self)) {
511         /* don't leave an invalid focus_client */
512         focus_client = NULL;
513     }
514
515     client_list = g_list_remove(client_list, self);
516     stacking_remove(self);
517     g_hash_table_remove(window_map, &self->window);
518
519     /* once the client is out of the list, update the struts to remove its
520        influence */
521     if (STRUT_EXISTS(self->strut))
522         screen_update_areas();
523
524     for (it = client_destructors; it; it = g_slist_next(it)) {
525         Destructor *d = it->data;
526         d->func(self, d->data);
527     }
528
529     /* tell our parent(s) that we're gone */
530     if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
531         for (it = self->group->members; it; it = g_slist_next(it))
532             if (it->data != self)
533                 ((ObClient*)it->data)->transients =
534                     g_slist_remove(((ObClient*)it->data)->transients, self);
535     } else if (self->transient_for) {        /* transient of window */
536         self->transient_for->transients =
537             g_slist_remove(self->transient_for->transients, self);
538     }
539
540     /* tell our transients that we're gone */
541     for (it = self->transients; it; it = g_slist_next(it)) {
542         if (((ObClient*)it->data)->transient_for != OB_TRAN_GROUP) {
543             ((ObClient*)it->data)->transient_for = NULL;
544             client_calc_layer(it->data);
545         }
546     }
547
548     /* remove from its group */
549     if (self->group) {
550         group_remove(self->group, self);
551         self->group = NULL;
552     }
553
554     /* restore the window's original geometry so it is not lost */
555     {
556         Rect a = self->area;
557
558         if (self->fullscreen)
559             a = self->pre_fullscreen_area;
560         else if (self->max_horz || self->max_vert) {
561             if (self->max_horz) {
562                 a.x = self->pre_max_area.x;
563                 a.width = self->pre_max_area.width;
564             }
565             if (self->max_vert) {
566                 a.y = self->pre_max_area.y;
567                 a.height = self->pre_max_area.height;
568             }
569         }
570
571         /* give the client its border back */
572         client_toggle_border(self, TRUE);
573
574         self->fullscreen = self->max_horz = self->max_vert = FALSE;
575         self->decorations = 0; /* unmanaged windows have no decor */
576
577         client_move_resize(self, a.x, a.y, a.width, a.height);
578     }
579
580     /* reparent the window out of the frame, and free the frame */
581     frame_release_client(self->frame, self);
582     self->frame = NULL;
583
584     if (ob_state() != OB_STATE_EXITING) {
585         /* these values should not be persisted across a window
586            unmapping/mapping */
587         PROP_ERASE(self->window, net_wm_desktop);
588         PROP_ERASE(self->window, net_wm_state);
589         PROP_ERASE(self->window, wm_state);
590     } else {
591         /* if we're left in an unmapped state, the client wont be mapped. this
592            is bad, since we will no longer be managing the window on restart */
593         XMapWindow(ob_display, self->window);
594     }
595
596     ob_debug("Unmanaged window 0x%lx\n", self->window);
597
598     /* free all data allocated in the client struct */
599     g_slist_free(self->transients);
600     for (j = 0; j < self->nicons; ++j)
601         g_free(self->icons[j].data);
602     if (self->nicons > 0)
603         g_free(self->icons);
604     g_free(self->title);
605     g_free(self->icon_title);
606     g_free(self->name);
607     g_free(self->class);
608     g_free(self->role);
609     g_free(self->client_machine);
610     g_free(self->sm_client_id);
611     g_free(self);
612      
613     /* update the list hints */
614     client_set_list();
615 }
616
617 static ObAppSettings *client_get_settings_state(ObClient *self)
618 {
619     ObAppSettings *settings = NULL;
620     GSList *it;
621
622     for (it = config_per_app_settings; it; it = g_slist_next(it)) {
623         ObAppSettings *app = it->data;
624         
625         if ((app->name && !app->class && !strcmp(app->name, self->name))
626             || (app->class && !app->name && !strcmp(app->class, self->class))
627             || (app->class && app->name && !strcmp(app->class, self->class)
628                 && !strcmp(app->name, self->name)))
629         {
630             ob_debug("Window matching: %s\n", app->name);
631             /* Match if no role was specified in the per app setting, or if the
632              * string matches the beginning of the role, since apps like to set
633              * the role to things like browser-window-23c4b2f */
634             if (!app->role
635                 || !strncmp(app->role, self->role, strlen(app->role)))
636             {
637                 /* use this one */
638                 settings = app;
639                 break;
640             }
641         }
642     }
643
644     if (settings) {
645         if (settings->shade != -1)
646             self->shaded = !!settings->shade;
647         if (settings->decor != -1)
648             self->undecorated = !settings->decor;
649         if (settings->iconic != -1)
650             self->iconic = !!settings->iconic;
651         if (settings->skip_pager != -1)
652             self->skip_pager = !!settings->skip_pager;
653         if (settings->skip_taskbar != -1)
654             self->skip_taskbar = !!settings->skip_taskbar;
655
656         if (settings->max_vert != -1)
657             self->max_vert = !!settings->max_vert;
658         if (settings->max_horz != -1)
659             self->max_horz = !!settings->max_horz;
660
661         if (settings->fullscreen != -1)
662             self->fullscreen = !!settings->fullscreen;
663
664         if (settings->desktop < screen_num_desktops
665             || settings->desktop == DESKTOP_ALL)
666             self->desktop = settings->desktop;
667
668         if (settings->layer == -1) {
669             self->below = TRUE;
670             self->above = FALSE;
671         }
672         else if (settings->layer == 0) {
673             self->below = FALSE;
674             self->above = FALSE;
675         }
676         else if (settings->layer == 1) {
677             self->below = FALSE;
678             self->above = TRUE;
679         }
680     }
681     return settings;
682 }
683
684 static void client_restore_session_state(ObClient *self)
685 {
686     GList *it;
687
688     if (!(it = session_state_find(self)))
689         return;
690
691     self->session = it->data;
692
693     RECT_SET_POINT(self->area, self->session->x, self->session->y);
694     self->positioned = PPosition;
695     if (self->session->w > 0)
696         self->area.width = self->session->w;
697     if (self->session->h > 0)
698         self->area.height = self->session->h;
699     XResizeWindow(ob_display, self->window,
700                   self->area.width, self->area.height);
701
702     self->desktop = (self->session->desktop == DESKTOP_ALL ?
703                      self->session->desktop :
704                      MIN(screen_num_desktops - 1, self->session->desktop));
705     PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
706
707     self->shaded = self->session->shaded;
708     self->iconic = self->session->iconic;
709     self->skip_pager = self->session->skip_pager;
710     self->skip_taskbar = self->session->skip_taskbar;
711     self->fullscreen = self->session->fullscreen;
712     self->above = self->session->above;
713     self->below = self->session->below;
714     self->max_horz = self->session->max_horz;
715     self->max_vert = self->session->max_vert;
716 }
717
718 static void client_restore_session_stacking(ObClient *self)
719 {
720     GList *it;
721
722     if (!self->session) return;
723
724     it = g_list_find(session_saved_state, self->session);
725     for (it = g_list_previous(it); it; it = g_list_previous(it)) {
726         GList *cit;
727
728         for (cit = client_list; cit; cit = g_list_next(cit))
729             if (session_state_cmp(it->data, cit->data))
730                 break;
731         if (cit) {
732             client_calc_layer(self);
733             stacking_below(CLIENT_AS_WINDOW(self),
734                            CLIENT_AS_WINDOW(cit->data));
735             break;
736         }
737     }
738 }
739
740 void client_move_onscreen(ObClient *self, gboolean rude)
741 {
742     gint x = self->area.x;
743     gint y = self->area.y;
744     if (client_find_onscreen(self, &x, &y,
745                              self->area.width,
746                              self->area.height, rude)) {
747         client_move(self, x, y);
748     }
749 }
750
751 gboolean client_find_onscreen(ObClient *self, gint *x, gint *y, gint w, gint h,
752                               gboolean rude)
753 {
754     Rect *mon_a, *all_a;
755     gint ox = *x, oy = *y;
756     gboolean rudel = rude, ruder = rude, rudet = rude, rudeb = rude;
757     gint fw, fh;
758
759     all_a = screen_area(self->desktop);
760     mon_a = screen_area_monitor(self->desktop, client_monitor(self));
761
762     /* get where the frame would be */
763     frame_client_gravity(self->frame, x, y, w, h);
764
765     /* get the requested size of the window with decorations */
766     fw = self->frame->size.left + w + self->frame->size.right;
767     fh = self->frame->size.top + h + self->frame->size.bottom;
768
769     /* This makes sure windows aren't entirely outside of the screen so you
770        can't see them at all.
771        It makes sure 10% of the window is on the screen at least. At don't let
772        it move itself off the top of the screen, which would hide the titlebar
773        on you. (The user can still do this if they want too, it's only limiting
774        the application.
775
776        XXX watch for xinerama dead areas...
777     */
778     if (client_normal(self)) {
779         if (!self->strut.right && *x + fw/10 >= all_a->x + all_a->width - 1)
780             *x = all_a->x + all_a->width - fw/10;
781         if (!self->strut.bottom && *y + fh/10 >= all_a->y + all_a->height - 1)
782             *y = all_a->y + all_a->height - fh/10;
783         if (!self->strut.left && *x + fw*9/10 - 1 < all_a->x)
784             *x = all_a->x - fw*9/10;
785         if (!self->strut.top && *y + fh*9/10 - 1 < all_a->y)
786             *y = all_a->y - fw*9/10;
787     }
788
789     /* If rudeness wasn't requested, then figure out of the client is currently
790        entirely on the screen. If it is, and the position isn't changing by
791        request, and it is enlarging, then be rude even though it wasn't
792        requested */
793     if (!rude) {
794         Point oldtl, oldtr, oldbl, oldbr;
795         Point newtl, newtr, newbl, newbr;
796         gboolean stationary;
797
798         POINT_SET(oldtl, self->frame->area.x, self->frame->area.y);
799         POINT_SET(oldbr, self->frame->area.x + self->frame->area.width - 1,
800                   self->frame->area.y + self->frame->area.height - 1);
801         POINT_SET(oldtr, oldbr.x, oldtl.y);
802         POINT_SET(oldbl, oldtl.x, oldbr.y);
803
804         POINT_SET(newtl, *x, *y);
805         POINT_SET(newbr, *x + fw - 1, *y + fh - 1);
806         POINT_SET(newtr, newbr.x, newtl.y);
807         POINT_SET(newbl, newtl.x, newbr.y);
808
809         /* is it moving or just resizing from some corner? */
810         stationary = (POINT_EQUAL(oldtl, newtl) || POINT_EQUAL(oldtr, newtr) ||
811                       POINT_EQUAL(oldbl, newbl) || POINT_EQUAL(oldbr, newbr));
812
813         /* if left edge is growing */
814         if (stationary && newtl.x < oldtl.x)
815             rudel = TRUE;
816         /* if right edge is growing */
817         if (stationary && newtr.x > oldtr.x)
818             ruder = TRUE;
819         /* if top edge is growing */
820         if (stationary && newtl.y < oldtl.y)
821             rudet = TRUE;
822         /* if bottom edge is growing */
823         if (stationary && newbl.y > oldbl.y)
824             rudeb = TRUE;
825     }
826
827     /* This here doesn't let windows even a pixel outside the struts/screen.
828      * When called from client_manage, programs placing themselves are
829      * forced completely onscreen, while things like
830      * xterm -geometry resolution-width/2 will work fine. Trying to
831      * place it completely offscreen will be handled in the above code.
832      * Sorry for this confused comment, i am tired. */
833     if (fw <= mon_a->width) {
834         if (rudel && !self->strut.left && *x < mon_a->x) *x = mon_a->x;
835         if (ruder && !self->strut.right && *x + fw > mon_a->x + mon_a->width)
836             *x = mon_a->x + mon_a->width - fw;
837     }
838     if (fh <= mon_a->height) {
839         if (rudet && !self->strut.top && *y < mon_a->y) *y = mon_a->y;
840         if (rudeb && !self->strut.bottom && *y + fh > mon_a->y + mon_a->height)
841             *y = mon_a->y + mon_a->height - fh;
842     }
843
844     /* get where the client should be */
845     frame_frame_gravity(self->frame, x, y, w, h);
846
847     return ox != *x || oy != *y;
848 }
849
850 static void client_toggle_border(ObClient *self, gboolean show)
851 {
852     /* adjust our idea of where the client is, based on its border. When the
853        border is removed, the client should now be considered to be in a
854        different position.
855        when re-adding the border to the client, the same operation needs to be
856        reversed. */
857     gint oldx = self->area.x, oldy = self->area.y;
858     gint x = oldx, y = oldy;
859     switch(self->gravity) {
860     default:
861     case NorthWestGravity:
862     case WestGravity:
863     case SouthWestGravity:
864         break;
865     case NorthEastGravity:
866     case EastGravity:
867     case SouthEastGravity:
868         if (show) x -= self->border_width * 2;
869         else      x += self->border_width * 2;
870         break;
871     case NorthGravity:
872     case SouthGravity:
873     case CenterGravity:
874     case ForgetGravity:
875     case StaticGravity:
876         if (show) x -= self->border_width;
877         else      x += self->border_width;
878         break;
879     }
880     switch(self->gravity) {
881     default:
882     case NorthWestGravity:
883     case NorthGravity:
884     case NorthEastGravity:
885         break;
886     case SouthWestGravity:
887     case SouthGravity:
888     case SouthEastGravity:
889         if (show) y -= self->border_width * 2;
890         else      y += self->border_width * 2;
891         break;
892     case WestGravity:
893     case EastGravity:
894     case CenterGravity:
895     case ForgetGravity:
896     case StaticGravity:
897         if (show) y -= self->border_width;
898         else      y += self->border_width;
899         break;
900     }
901     self->area.x = x;
902     self->area.y = y;
903
904     if (show) {
905         XSetWindowBorderWidth(ob_display, self->window, self->border_width);
906
907         /* set border_width to 0 because there is no border to add into
908            calculations anymore */
909         self->border_width = 0;
910     } else
911         XSetWindowBorderWidth(ob_display, self->window, 0);
912 }
913
914
915 static void client_get_all(ObClient *self)
916 {
917     client_get_area(self);
918     client_get_mwm_hints(self);
919
920     /* The transient-ness of a window is used to pick a type, but the type can
921        also affect transiency.
922
923        Dialogs are always made transients for their group if they have one.
924
925        I also have made non-application type windows be transients for their
926        group (eg utility windows).
927     */
928     client_get_transientness(self);
929     client_get_type(self);/* this can change the mwmhints for special cases */
930     client_get_state(self);
931
932     client_update_wmhints(self);
933     /* this may have already been called from client_update_wmhints */
934     if (self->transient_for == NULL)
935         client_update_transient_for(self);
936     client_get_startup_id(self);
937     client_get_desktop(self);/* uses transient data/group/startup id if a
938                                 desktop is not specified */
939     client_get_shaped(self);
940
941     client_get_layer(self); /* if layer hasn't been specified, get it from
942                                other sources if possible */
943
944     {
945         /* a couple type-based defaults for new windows */
946
947         /* this makes sure that these windows appear on all desktops */
948         if (self->type == OB_CLIENT_TYPE_DESKTOP)
949             self->desktop = DESKTOP_ALL;
950     }
951
952     client_update_protocols(self);
953
954     client_get_gravity(self); /* get the attribute gravity */
955     client_update_normal_hints(self); /* this may override the attribute
956                                          gravity */
957
958     /* got the type, the mwmhints, the protocols, and the normal hints
959        (min/max sizes), so we're ready to set up the decorations/functions */
960     client_setup_decor_and_functions(self);
961   
962 #ifdef SYNC
963     client_update_sync_request_counter(self);
964 #endif
965     client_get_client_machine(self);
966     client_get_colormap(self);
967     client_update_title(self);
968     client_update_class(self);
969     client_update_sm_client_id(self);
970     client_update_strut(self);
971     client_update_icons(self);
972     client_update_user_time(self);
973 }
974
975 static void client_get_startup_id(ObClient *self)
976 {
977     if (!(PROP_GETS(self->window, net_startup_id, utf8, &self->startup_id)))
978         if (self->group)
979             PROP_GETS(self->group->leader,
980                       net_startup_id, utf8, &self->startup_id);
981 }
982
983 static void client_get_area(ObClient *self)
984 {
985     XWindowAttributes wattrib;
986     Status ret;
987   
988     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
989     g_assert(ret != BadWindow);
990
991     RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
992     POINT_SET(self->root_pos, wattrib.x, wattrib.y);
993     self->border_width = wattrib.border_width;
994
995     ob_debug("client area: %d %d  %d %d\n", wattrib.x, wattrib.y,
996              wattrib.width, wattrib.height);
997 }
998
999 static void client_get_desktop(ObClient *self)
1000 {
1001     guint32 d = screen_num_desktops; /* an always-invalid value */
1002
1003     if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
1004         if (d >= screen_num_desktops && d != DESKTOP_ALL)
1005             self->desktop = screen_num_desktops - 1;
1006         else
1007             self->desktop = d;
1008     } else {
1009         gboolean trdesk = FALSE;
1010
1011         if (self->transient_for) {
1012             if (self->transient_for != OB_TRAN_GROUP) {
1013                 self->desktop = self->transient_for->desktop;
1014                 trdesk = TRUE;
1015             } else {
1016                 GSList *it;
1017
1018                 for (it = self->group->members; it; it = g_slist_next(it))
1019                     if (it->data != self &&
1020                         !((ObClient*)it->data)->transient_for) {
1021                         self->desktop = ((ObClient*)it->data)->desktop;
1022                         trdesk = TRUE;
1023                         break;
1024                     }
1025             }
1026         }
1027         if (!trdesk) {
1028             /* try get from the startup-notification protocol */
1029             if (sn_get_desktop(self->startup_id, &self->desktop)) {
1030                 if (self->desktop >= screen_num_desktops &&
1031                     self->desktop != DESKTOP_ALL)
1032                     self->desktop = screen_num_desktops - 1;
1033             } else
1034                 /* defaults to the current desktop */
1035                 self->desktop = screen_desktop;
1036         }
1037     }
1038 }
1039
1040 static void client_get_layer(ObClient *self)
1041 {
1042     if (!(self->above || self->below)) {
1043         if (self->group) {
1044             /* apply stuff from the group */
1045             GSList *it;
1046             gint layer = -2;
1047
1048             for (it = self->group->members; it; it = g_slist_next(it)) {
1049                 ObClient *c = it->data;
1050                 if (c != self && !client_search_transient(self, c) &&
1051                     client_normal(self) && client_normal(c))
1052                 {
1053                     layer = MAX(layer,
1054                                 (c->above ? 1 : (c->below ? -1 : 0)));
1055                 }
1056             }
1057             switch (layer) {
1058             case -1:
1059                 self->below = TRUE;
1060                 break;
1061             case -2:
1062             case 0:
1063                 break;
1064             case 1:
1065                 self->above = TRUE;
1066                 break;
1067             default:
1068                 g_assert_not_reached();
1069                 break;
1070             }
1071         }
1072     }
1073 }
1074
1075 static void client_get_state(ObClient *self)
1076 {
1077     guint32 *state;
1078     guint num;
1079   
1080     if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
1081         gulong i;
1082         for (i = 0; i < num; ++i) {
1083             if (state[i] == prop_atoms.net_wm_state_modal)
1084                 self->modal = TRUE;
1085             else if (state[i] == prop_atoms.net_wm_state_shaded)
1086                 self->shaded = TRUE;
1087             else if (state[i] == prop_atoms.net_wm_state_hidden)
1088                 self->iconic = TRUE;
1089             else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
1090                 self->skip_taskbar = TRUE;
1091             else if (state[i] == prop_atoms.net_wm_state_skip_pager)
1092                 self->skip_pager = TRUE;
1093             else if (state[i] == prop_atoms.net_wm_state_fullscreen)
1094                 self->fullscreen = TRUE;
1095             else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
1096                 self->max_vert = TRUE;
1097             else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
1098                 self->max_horz = TRUE;
1099             else if (state[i] == prop_atoms.net_wm_state_above)
1100                 self->above = TRUE;
1101             else if (state[i] == prop_atoms.net_wm_state_below)
1102                 self->below = TRUE;
1103             else if (state[i] == prop_atoms.net_wm_state_demands_attention)
1104                 self->demands_attention = TRUE;
1105             else if (state[i] == prop_atoms.ob_wm_state_undecorated)
1106                 self->undecorated = TRUE;
1107         }
1108
1109         g_free(state);
1110     }
1111 }
1112
1113 static void client_get_shaped(ObClient *self)
1114 {
1115     self->shaped = FALSE;
1116 #ifdef   SHAPE
1117     if (extensions_shape) {
1118         gint foo;
1119         guint ufoo;
1120         gint s;
1121
1122         XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
1123
1124         XShapeQueryExtents(ob_display, self->window, &s, &foo,
1125                            &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
1126                            &ufoo);
1127         self->shaped = (s != 0);
1128     }
1129 #endif
1130 }
1131
1132 void client_get_transientness(ObClient *self)
1133 {
1134     Window t;
1135     if (XGetTransientForHint(ob_display, self->window, &t))
1136         self->transient = TRUE;
1137 }
1138
1139 void client_update_transient_for(ObClient *self)
1140 {
1141     Window t = None;
1142     ObClient *target = NULL;
1143
1144     if (XGetTransientForHint(ob_display, self->window, &t)) {
1145         self->transient = TRUE;
1146         if (t != self->window) { /* cant be transient to itself! */
1147             target = g_hash_table_lookup(window_map, &t);
1148             /* if this happens then we need to check for it*/
1149             g_assert(target != self);
1150             if (target && !WINDOW_IS_CLIENT(target)) {
1151                 /* this can happen when a dialog is a child of
1152                    a dockapp, for example */
1153                 target = NULL;
1154             }
1155
1156             /* THIS IS SO ANNOYING ! ! ! ! Let me explain.... have a seat..
1157
1158                Setting the transient_for to Root is actually illegal, however
1159                applications from time have done this to specify transient for
1160                their group.
1161
1162                Now you can do that by being a TYPE_DIALOG and not setting
1163                the transient_for hint at all on your window. But people still
1164                use Root, and Kwin is very strange in this regard.
1165
1166                KWin 3.0 will not consider windows with transient_for set to
1167                Root as transient for their group *UNLESS* they are also modal.
1168                In that case, it will make them transient for the group. This
1169                leads to all sorts of weird behavior from KDE apps which are
1170                only tested in KWin. I'd like to follow their behavior just to
1171                make this work right with KDE stuff, but that seems wrong.
1172             */
1173             if (!target && self->group) {
1174                 /* not transient to a client, see if it is transient for a
1175                    group */
1176                 if (t == RootWindow(ob_display, ob_screen)) {
1177                     /* window is a transient for its group! */
1178                     target = OB_TRAN_GROUP;
1179                 }
1180             }
1181         }
1182     } else if (self->type == OB_CLIENT_TYPE_DIALOG ||
1183                self->type == OB_CLIENT_TYPE_TOOLBAR ||
1184                self->type == OB_CLIENT_TYPE_MENU ||
1185                self->type == OB_CLIENT_TYPE_UTILITY)
1186     {
1187         self->transient = TRUE;
1188         if (self->group)
1189             target = OB_TRAN_GROUP;
1190     } else
1191         self->transient = FALSE;
1192
1193     client_update_transient_tree(self, self->group, self->group,
1194                                  self->transient_for, target);
1195     self->transient_for = target;
1196                           
1197 }
1198
1199 static void client_update_transient_tree(ObClient *self,
1200                                          ObGroup *oldgroup, ObGroup *newgroup,
1201                                          ObClient* oldparent,
1202                                          ObClient *newparent)
1203 {
1204     GSList *it, *next;
1205     ObClient *c;
1206
1207     /* No change has occured */
1208     if (oldgroup == newgroup && oldparent == newparent) return;
1209
1210     /** Remove the client from the transient tree wherever it has changed **/
1211
1212     /* If the window is becoming a direct transient for a window in its group
1213        then that window can't be a child of this window anymore */
1214     if (oldparent != newparent &&
1215         newparent != NULL && newparent != OB_TRAN_GROUP &&
1216         newparent->transient_for == OB_TRAN_GROUP &&
1217         newgroup != NULL && newgroup == oldgroup)
1218     {
1219         self->transients = g_slist_remove(self->transients, newparent);
1220     }
1221             
1222
1223     /* If the group changed then we need to remove any old group transient
1224        windows from our children. But if we're transient for the group, then
1225        other group transients are not our children. */
1226     if (oldgroup != newgroup && oldgroup != NULL &&
1227         oldparent != OB_TRAN_GROUP)
1228     {
1229         for (it = self->transients; it; it = next) {
1230             next = g_slist_next(it);
1231             c = it->data;
1232             if (c->group == oldgroup)
1233                 self->transients = g_slist_delete_link(self->transients, it);
1234         }
1235     }
1236
1237     /* If we used to be transient for a group and now we are not, or we're
1238        transient for a new group, then we need to remove ourselves from all
1239        our ex-parents */
1240     if (oldparent == OB_TRAN_GROUP && (oldgroup != newgroup ||
1241                                        oldparent != newparent))
1242     {
1243         for (it = oldgroup->members; it; it = g_slist_next(it)) {
1244             c = it->data;
1245             if (c != self && (!c->transient_for ||
1246                               c->transient_for != OB_TRAN_GROUP))
1247                 c->transients = g_slist_remove(c->transients, self);
1248         }
1249     }
1250     /* If we used to be transient for a single window and we are no longer
1251        transient for it, then we need to remove ourself from its children */
1252     else if (oldparent != NULL && oldparent != OB_TRAN_GROUP &&
1253              oldparent != newparent)
1254         oldparent->transients = g_slist_remove(oldparent->transients, self);
1255
1256
1257     /** Re-add the client to the transient tree wherever it has changed **/
1258
1259     /* If we're now transient for a group and we weren't transient for it
1260        before then we need to add ourselves to all our new parents */
1261     if (newparent == OB_TRAN_GROUP && (oldgroup != newgroup ||
1262                                        oldparent != newparent))
1263     {
1264         for (it = oldgroup->members; it; it = g_slist_next(it)) {
1265             c = it->data;
1266             if (c != self && (!c->transient_for ||
1267                               c->transient_for != OB_TRAN_GROUP))
1268                 c->transients = g_slist_append(c->transients, self);
1269         }
1270     }
1271     /* If we are now transient for a single window which we weren't before,
1272        we need to add ourselves to its children
1273
1274        WARNING: Cyclical transient ness is possible if two windows are
1275        transient for eachother.
1276     */
1277     else if (newparent != NULL && newparent != OB_TRAN_GROUP &&
1278              newparent != oldparent &&
1279              /* don't make ourself its child if it is already our child */
1280              !client_is_direct_child(self, newparent))
1281         newparent->transients = g_slist_append(newparent->transients, self);
1282
1283     /* If the group changed then we need to add any new group transient
1284        windows to our children. But if we're transient for the group, then
1285        other group transients are not our children.
1286
1287        WARNING: Cyclical transient-ness is possible. For e.g. if:
1288        A is transient for the group
1289        B is a member of the group and transient for A
1290     */
1291     if (oldgroup != newgroup && newgroup != NULL &&
1292         newparent != OB_TRAN_GROUP)
1293     {
1294         for (it = newgroup->members; it; it = g_slist_next(it)) {
1295             c = it->data;
1296             if (c != self && c->transient_for == OB_TRAN_GROUP &&
1297                 /* Don't make it our child if it is already our parent */
1298                 !client_is_direct_child(c, self))
1299             {
1300                 self->transients = g_slist_append(self->transients, c);
1301             }
1302         }
1303     }
1304 }
1305
1306 static void client_get_mwm_hints(ObClient *self)
1307 {
1308     guint num;
1309     guint32 *hints;
1310
1311     self->mwmhints.flags = 0; /* default to none */
1312
1313     if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
1314                     &hints, &num)) {
1315         if (num >= OB_MWM_ELEMENTS) {
1316             self->mwmhints.flags = hints[0];
1317             self->mwmhints.functions = hints[1];
1318             self->mwmhints.decorations = hints[2];
1319         }
1320         g_free(hints);
1321     }
1322 }
1323
1324 void client_get_type(ObClient *self)
1325 {
1326     guint num, i;
1327     guint32 *val;
1328
1329     self->type = -1;
1330   
1331     if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
1332         /* use the first value that we know about in the array */
1333         for (i = 0; i < num; ++i) {
1334             if (val[i] == prop_atoms.net_wm_window_type_desktop)
1335                 self->type = OB_CLIENT_TYPE_DESKTOP;
1336             else if (val[i] == prop_atoms.net_wm_window_type_dock)
1337                 self->type = OB_CLIENT_TYPE_DOCK;
1338             else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
1339                 self->type = OB_CLIENT_TYPE_TOOLBAR;
1340             else if (val[i] == prop_atoms.net_wm_window_type_menu)
1341                 self->type = OB_CLIENT_TYPE_MENU;
1342             else if (val[i] == prop_atoms.net_wm_window_type_utility)
1343                 self->type = OB_CLIENT_TYPE_UTILITY;
1344             else if (val[i] == prop_atoms.net_wm_window_type_splash)
1345                 self->type = OB_CLIENT_TYPE_SPLASH;
1346             else if (val[i] == prop_atoms.net_wm_window_type_dialog)
1347                 self->type = OB_CLIENT_TYPE_DIALOG;
1348             else if (val[i] == prop_atoms.net_wm_window_type_normal)
1349                 self->type = OB_CLIENT_TYPE_NORMAL;
1350             else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
1351                 /* prevent this window from getting any decor or
1352                    functionality */
1353                 self->mwmhints.flags &= (OB_MWM_FLAG_FUNCTIONS |
1354                                          OB_MWM_FLAG_DECORATIONS);
1355                 self->mwmhints.decorations = 0;
1356                 self->mwmhints.functions = 0;
1357             }
1358             if (self->type != (ObClientType) -1)
1359                 break; /* grab the first legit type */
1360         }
1361         g_free(val);
1362     }
1363     
1364     if (self->type == (ObClientType) -1) {
1365         /*the window type hint was not set, which means we either classify
1366           ourself as a normal window or a dialog, depending on if we are a
1367           transient. */
1368         if (self->transient)
1369             self->type = OB_CLIENT_TYPE_DIALOG;
1370         else
1371             self->type = OB_CLIENT_TYPE_NORMAL;
1372     }
1373 }
1374
1375 void client_update_protocols(ObClient *self)
1376 {
1377     guint32 *proto;
1378     guint num_return, i;
1379
1380     self->focus_notify = FALSE;
1381     self->delete_window = FALSE;
1382
1383     if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
1384         for (i = 0; i < num_return; ++i) {
1385             if (proto[i] == prop_atoms.wm_delete_window)
1386                 /* this means we can request the window to close */
1387                 self->delete_window = TRUE;
1388             else if (proto[i] == prop_atoms.wm_take_focus)
1389                 /* if this protocol is requested, then the window will be
1390                    notified whenever we want it to receive focus */
1391                 self->focus_notify = TRUE;
1392 #ifdef SYNC
1393             else if (proto[i] == prop_atoms.net_wm_sync_request) 
1394                 /* if this protocol is requested, then resizing the
1395                    window will be synchronized between the frame and the
1396                    client */
1397                 self->sync_request = TRUE;
1398 #endif
1399         }
1400         g_free(proto);
1401     }
1402 }
1403
1404 #ifdef SYNC
1405 void client_update_sync_request_counter(ObClient *self)
1406 {
1407     guint32 i;
1408
1409     if (PROP_GET32(self->window, net_wm_sync_request_counter, cardinal, &i)) {
1410         self->sync_counter = i;
1411     } else
1412         self->sync_counter = None;
1413 }
1414 #endif
1415
1416 static void client_get_gravity(ObClient *self)
1417 {
1418     XWindowAttributes wattrib;
1419     Status ret;
1420
1421     ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
1422     g_assert(ret != BadWindow);
1423     self->gravity = wattrib.win_gravity;
1424 }
1425
1426 void client_get_colormap(ObClient *self)
1427 {
1428     XWindowAttributes wa;
1429
1430     if (XGetWindowAttributes(ob_display, self->window, &wa))
1431         client_update_colormap(self, wa.colormap);
1432 }
1433
1434 void client_update_colormap(ObClient *self, Colormap colormap)
1435 {
1436     self->colormap = colormap;
1437 }
1438
1439 void client_update_normal_hints(ObClient *self)
1440 {
1441     XSizeHints size;
1442     glong ret;
1443     gint oldgravity = self->gravity;
1444
1445     /* defaults */
1446     self->min_ratio = 0.0f;
1447     self->max_ratio = 0.0f;
1448     SIZE_SET(self->size_inc, 1, 1);
1449     SIZE_SET(self->base_size, 0, 0);
1450     SIZE_SET(self->min_size, 0, 0);
1451     SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
1452
1453     /* get the hints from the window */
1454     if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
1455         /* normal windows can't request placement! har har
1456         if (!client_normal(self))
1457         */
1458         self->positioned = (size.flags & (PPosition|USPosition));
1459
1460         if (size.flags & PWinGravity) {
1461             self->gravity = size.win_gravity;
1462       
1463             /* if the client has a frame, i.e. has already been mapped and
1464                is changing its gravity */
1465             if (self->frame && self->gravity != oldgravity) {
1466                 /* move our idea of the client's position based on its new
1467                    gravity */
1468                 client_convert_gravity(self, oldgravity,
1469                                        &self->area.x, &self->area.y,
1470                                        self->area.width, self->area.height);
1471             }
1472         }
1473
1474         if (size.flags & PAspect) {
1475             if (size.min_aspect.y)
1476                 self->min_ratio =
1477                     (gfloat) size.min_aspect.x / size.min_aspect.y;
1478             if (size.max_aspect.y)
1479                 self->max_ratio =
1480                     (gfloat) size.max_aspect.x / size.max_aspect.y;
1481         }
1482
1483         if (size.flags & PMinSize)
1484             SIZE_SET(self->min_size, size.min_width, size.min_height);
1485     
1486         if (size.flags & PMaxSize)
1487             SIZE_SET(self->max_size, size.max_width, size.max_height);
1488     
1489         if (size.flags & PBaseSize)
1490             SIZE_SET(self->base_size, size.base_width, size.base_height);
1491     
1492         if (size.flags & PResizeInc && size.width_inc && size.height_inc)
1493             SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
1494     }
1495 }
1496
1497 void client_setup_decor_and_functions(ObClient *self)
1498 {
1499     /* start with everything (cept fullscreen) */
1500     self->decorations =
1501         (OB_FRAME_DECOR_TITLEBAR |
1502          OB_FRAME_DECOR_HANDLE |
1503          OB_FRAME_DECOR_GRIPS |
1504          OB_FRAME_DECOR_BORDER |
1505          OB_FRAME_DECOR_ICON |
1506          OB_FRAME_DECOR_ALLDESKTOPS |
1507          OB_FRAME_DECOR_ICONIFY |
1508          OB_FRAME_DECOR_MAXIMIZE |
1509          OB_FRAME_DECOR_SHADE |
1510          OB_FRAME_DECOR_CLOSE);
1511     self->functions =
1512         (OB_CLIENT_FUNC_RESIZE |
1513          OB_CLIENT_FUNC_MOVE |
1514          OB_CLIENT_FUNC_ICONIFY |
1515          OB_CLIENT_FUNC_MAXIMIZE |
1516          OB_CLIENT_FUNC_SHADE |
1517          OB_CLIENT_FUNC_CLOSE);
1518
1519     if (!(self->min_size.width < self->max_size.width ||
1520           self->min_size.height < self->max_size.height))
1521         self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1522
1523     switch (self->type) {
1524     case OB_CLIENT_TYPE_NORMAL:
1525         /* normal windows retain all of the possible decorations and
1526            functionality, and are the only windows that you can fullscreen */
1527         self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1528         break;
1529
1530     case OB_CLIENT_TYPE_DIALOG:
1531     case OB_CLIENT_TYPE_UTILITY:
1532         /* these windows cannot be maximized */
1533         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1534         break;
1535
1536     case OB_CLIENT_TYPE_MENU:
1537     case OB_CLIENT_TYPE_TOOLBAR:
1538         /* these windows get less functionality */
1539         self->functions &= ~(OB_CLIENT_FUNC_ICONIFY | OB_CLIENT_FUNC_RESIZE);
1540         break;
1541
1542     case OB_CLIENT_TYPE_DESKTOP:
1543     case OB_CLIENT_TYPE_DOCK:
1544     case OB_CLIENT_TYPE_SPLASH:
1545         /* none of these windows are manipulated by the window manager */
1546         self->decorations = 0;
1547         self->functions = 0;
1548         break;
1549     }
1550
1551     /* Mwm Hints are applied subtractively to what has already been chosen for
1552        decor and functionality */
1553     if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
1554         if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
1555             if (! ((self->mwmhints.decorations & OB_MWM_DECOR_HANDLE) ||
1556                    (self->mwmhints.decorations & OB_MWM_DECOR_TITLE)))
1557             {
1558                 /* if the mwm hints request no handle or title, then all
1559                    decorations are disabled, but keep the border if that's
1560                    specified */
1561                 if (self->mwmhints.decorations & OB_MWM_DECOR_BORDER)
1562                     self->decorations = OB_FRAME_DECOR_BORDER;
1563                 else
1564                     self->decorations = 0;
1565             }
1566         }
1567     }
1568
1569     if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
1570         if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
1571             if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
1572                 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1573             if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
1574                 self->functions &= ~OB_CLIENT_FUNC_MOVE;
1575             /* dont let mwm hints kill any buttons
1576                if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
1577                self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
1578                if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
1579                self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1580             */
1581             /* dont let mwm hints kill the close button
1582                if (! (self->mwmhints.functions & MwmFunc_Close))
1583                self->functions &= ~OB_CLIENT_FUNC_CLOSE; */
1584         }
1585     }
1586
1587     if (!(self->functions & OB_CLIENT_FUNC_SHADE))
1588         self->decorations &= ~OB_FRAME_DECOR_SHADE;
1589     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY))
1590         self->decorations &= ~OB_FRAME_DECOR_ICONIFY;
1591     if (!(self->functions & OB_CLIENT_FUNC_RESIZE))
1592         self->decorations &= ~OB_FRAME_DECOR_GRIPS;
1593
1594     /* can't maximize without moving/resizing */
1595     if (!((self->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
1596           (self->functions & OB_CLIENT_FUNC_MOVE) &&
1597           (self->functions & OB_CLIENT_FUNC_RESIZE))) {
1598         self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1599         self->decorations &= ~OB_FRAME_DECOR_MAXIMIZE;
1600     }
1601
1602     /* kill the handle on fully maxed windows */
1603     if (self->max_vert && self->max_horz)
1604         self->decorations &= ~OB_FRAME_DECOR_HANDLE;
1605
1606     /* finally, the user can have requested no decorations, which overrides
1607        everything (but doesnt give it a border if it doesnt have one) */
1608     if (self->undecorated) {
1609         if (config_theme_keepborder)
1610             self->decorations &= OB_FRAME_DECOR_BORDER;
1611         else
1612             self->decorations = 0;
1613     }
1614
1615     /* if we don't have a titlebar, then we cannot shade! */
1616     if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1617         self->functions &= ~OB_CLIENT_FUNC_SHADE;
1618
1619     /* now we need to check against rules for the client's current state */
1620     if (self->fullscreen) {
1621         self->functions &= (OB_CLIENT_FUNC_CLOSE |
1622                             OB_CLIENT_FUNC_FULLSCREEN |
1623                             OB_CLIENT_FUNC_ICONIFY);
1624         self->decorations = 0;
1625     }
1626
1627     client_change_allowed_actions(self);
1628
1629     if (self->frame) {
1630         /* adjust the client's decorations, etc. */
1631         client_reconfigure(self);
1632     }
1633 }
1634
1635 static void client_change_allowed_actions(ObClient *self)
1636 {
1637     gulong actions[9];
1638     gint num = 0;
1639
1640     /* desktop windows are kept on all desktops */
1641     if (self->type != OB_CLIENT_TYPE_DESKTOP)
1642         actions[num++] = prop_atoms.net_wm_action_change_desktop;
1643
1644     if (self->functions & OB_CLIENT_FUNC_SHADE)
1645         actions[num++] = prop_atoms.net_wm_action_shade;
1646     if (self->functions & OB_CLIENT_FUNC_CLOSE)
1647         actions[num++] = prop_atoms.net_wm_action_close;
1648     if (self->functions & OB_CLIENT_FUNC_MOVE)
1649         actions[num++] = prop_atoms.net_wm_action_move;
1650     if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1651         actions[num++] = prop_atoms.net_wm_action_minimize;
1652     if (self->functions & OB_CLIENT_FUNC_RESIZE)
1653         actions[num++] = prop_atoms.net_wm_action_resize;
1654     if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1655         actions[num++] = prop_atoms.net_wm_action_fullscreen;
1656     if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1657         actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1658         actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1659     }
1660
1661     PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1662
1663     /* make sure the window isn't breaking any rules now */
1664
1665     if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1666         if (self->frame) client_shade(self, FALSE);
1667         else self->shaded = FALSE;
1668     }
1669     if (!(self->functions & OB_CLIENT_FUNC_ICONIFY) && self->iconic) {
1670         if (self->frame) client_iconify(self, FALSE, TRUE);
1671         else self->iconic = FALSE;
1672     }
1673     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1674         if (self->frame) client_fullscreen(self, FALSE);
1675         else self->fullscreen = FALSE;
1676     }
1677     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
1678                                                          self->max_vert)) {
1679         if (self->frame) client_maximize(self, FALSE, 0);
1680         else self->max_vert = self->max_horz = FALSE;
1681     }
1682 }
1683
1684 void client_reconfigure(ObClient *self)
1685 {
1686     /* by making this pass FALSE for user, we avoid the emacs event storm where
1687        every configurenotify causes an update in its normal hints, i think this
1688        is generally what we want anyways... */
1689     client_configure(self, self->area.x, self->area.y,
1690                      self->area.width, self->area.height, FALSE, TRUE);
1691 }
1692
1693 void client_update_wmhints(ObClient *self)
1694 {
1695     XWMHints *hints;
1696
1697     /* assume a window takes input if it doesnt specify */
1698     self->can_focus = TRUE;
1699   
1700     if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1701         gboolean ur;
1702
1703         if (hints->flags & InputHint)
1704             self->can_focus = hints->input;
1705
1706         /* only do this when first managing the window *AND* when we aren't
1707            starting up! */
1708         if (ob_state() != OB_STATE_STARTING && self->frame == NULL)
1709             if (hints->flags & StateHint)
1710                 self->iconic = hints->initial_state == IconicState;
1711
1712         ur = self->urgent;
1713         self->urgent = (hints->flags & XUrgencyHint);
1714         if (self->urgent && !ur)
1715             client_hilite(self, TRUE);
1716         else if (!self->urgent && ur && self->demands_attention)
1717             client_hilite(self, FALSE);
1718
1719         if (!(hints->flags & WindowGroupHint))
1720             hints->window_group = None;
1721
1722         /* did the group state change? */
1723         if (hints->window_group !=
1724             (self->group ? self->group->leader : None))
1725         {
1726             ObGroup *oldgroup = self->group;
1727
1728             /* remove from the old group if there was one */
1729             if (self->group != NULL) {
1730                 group_remove(self->group, self);
1731                 self->group = NULL;
1732             }
1733
1734             /* add ourself to the group if we have one */
1735             if (hints->window_group != None) {
1736                 self->group = group_add(hints->window_group, self);
1737             }
1738
1739             /* Put ourselves into the new group's transient tree, and remove
1740                ourselves from the old group's */
1741             client_update_transient_tree(self, oldgroup, self->group,
1742                                          self->transient_for,
1743                                          self->transient_for);
1744
1745             /* Lastly, being in a group, or not, can change if the window is
1746                transient for anything.
1747
1748                The logic for this is:
1749                self->transient = TRUE always if the window wants to be
1750                transient for something, even if transient_for was NULL because
1751                it wasn't in a group before.
1752
1753                If transient_for was NULL and oldgroup was NULL we can assume
1754                that when we add the new group, it will become transient for
1755                something.
1756
1757                If transient_for was OB_TRAN_GROUP, then it must have already
1758                had a group. If it is getting a new group, the above call to
1759                client_update_transient_tree has already taken care of
1760                everything ! If it is losing all group status then it will
1761                no longer be transient for anything and that needs to be
1762                updated.
1763             */
1764             if (self->transient &&
1765                 ((self->transient_for == NULL && oldgroup == NULL) ||
1766                  (self->transient_for == OB_TRAN_GROUP && !self->group)))
1767                 client_update_transient_for(self);
1768         }
1769
1770         /* the WM_HINTS can contain an icon */
1771         client_update_icons(self);
1772
1773         XFree(hints);
1774     }
1775 }
1776
1777 void client_update_title(ObClient *self)
1778 {
1779     gchar *data = NULL;
1780     gchar *visible = NULL;
1781
1782     g_free(self->title);
1783      
1784     /* try netwm */
1785     if (!PROP_GETS(self->window, net_wm_name, utf8, &data)) {
1786         /* try old x stuff */
1787         if (!(PROP_GETS(self->window, wm_name, locale, &data)
1788               || PROP_GETS(self->window, wm_name, utf8, &data))) {
1789             if (self->transient) {
1790                 /*
1791                   GNOME alert windows are not given titles:
1792                   http://developer.gnome.org/projects/gup/hig/draft_hig_new/windows-alert.html
1793                 */
1794                 data = g_strdup("");
1795             } else
1796                 data = g_strdup("Unnamed Window");
1797         }
1798     }
1799
1800     if (self->client_machine) {
1801         visible = g_strdup_printf("%s (%s)", data, self->client_machine);
1802         g_free(data);
1803     } else
1804         visible = data;
1805
1806     PROP_SETS(self->window, net_wm_visible_name, visible);
1807     self->title = visible;
1808
1809     if (self->frame)
1810         frame_adjust_title(self->frame);
1811
1812     /* update the icon title */
1813     data = NULL;
1814     g_free(self->icon_title);
1815
1816     /* try netwm */
1817     if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1818         /* try old x stuff */
1819         if (!(PROP_GETS(self->window, wm_icon_name, locale, &data) ||
1820               PROP_GETS(self->window, wm_icon_name, utf8, &data)))
1821             data = g_strdup(self->title);
1822
1823     PROP_SETS(self->window, net_wm_visible_icon_name, data);
1824     self->icon_title = data;
1825 }
1826
1827 void client_update_class(ObClient *self)
1828 {
1829     gchar **data;
1830     gchar *s;
1831
1832     if (self->name) g_free(self->name);
1833     if (self->class) g_free(self->class);
1834     if (self->role) g_free(self->role);
1835
1836     self->name = self->class = self->role = NULL;
1837
1838     if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1839         if (data[0]) {
1840             self->name = g_strdup(data[0]);
1841             if (data[1])
1842                 self->class = g_strdup(data[1]);
1843         }
1844         g_strfreev(data);     
1845     }
1846
1847     if (PROP_GETS(self->window, wm_window_role, locale, &s))
1848         self->role = s;
1849
1850     if (self->name == NULL) self->name = g_strdup("");
1851     if (self->class == NULL) self->class = g_strdup("");
1852     if (self->role == NULL) self->role = g_strdup("");
1853 }
1854
1855 void client_update_strut(ObClient *self)
1856 {
1857     guint num;
1858     guint32 *data;
1859     gboolean got = FALSE;
1860     StrutPartial strut;
1861
1862     if (PROP_GETA32(self->window, net_wm_strut_partial, cardinal,
1863                     &data, &num)) {
1864         if (num == 12) {
1865             got = TRUE;
1866             STRUT_PARTIAL_SET(strut,
1867                               data[0], data[2], data[1], data[3],
1868                               data[4], data[5], data[8], data[9],
1869                               data[6], data[7], data[10], data[11]);
1870         }
1871         g_free(data);
1872     }
1873
1874     if (!got &&
1875         PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1876         if (num == 4) {
1877             const Rect *a;
1878
1879             got = TRUE;
1880
1881             /* use the screen's width/height */
1882             a = screen_physical_area();
1883
1884             STRUT_PARTIAL_SET(strut,
1885                               data[0], data[2], data[1], data[3],
1886                               a->y, a->y + a->height - 1,
1887                               a->x, a->x + a->width - 1,
1888                               a->y, a->y + a->height - 1,
1889                               a->x, a->x + a->width - 1);
1890         }
1891         g_free(data);
1892     }
1893
1894     if (!got)
1895         STRUT_PARTIAL_SET(strut, 0, 0, 0, 0,
1896                           0, 0, 0, 0, 0, 0, 0, 0);
1897
1898     if (!STRUT_EQUAL(strut, self->strut)) {
1899         self->strut = strut;
1900
1901         /* updating here is pointless while we're being mapped cuz we're not in
1902            the client list yet */
1903         if (self->frame)
1904             screen_update_areas();
1905     }
1906 }
1907
1908 void client_update_icons(ObClient *self)
1909 {
1910     guint num;
1911     guint32 *data;
1912     guint w, h, i, j;
1913
1914     for (i = 0; i < self->nicons; ++i)
1915         g_free(self->icons[i].data);
1916     if (self->nicons > 0)
1917         g_free(self->icons);
1918     self->nicons = 0;
1919
1920     if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1921         /* figure out how many valid icons are in here */
1922         i = 0;
1923         while (num - i > 2) {
1924             w = data[i++];
1925             h = data[i++];
1926             i += w * h;
1927             if (i > num || w*h == 0) break;
1928             ++self->nicons;
1929         }
1930
1931         self->icons = g_new(ObClientIcon, self->nicons);
1932     
1933         /* store the icons */
1934         i = 0;
1935         for (j = 0; j < self->nicons; ++j) {
1936             guint x, y, t;
1937
1938             w = self->icons[j].width = data[i++];
1939             h = self->icons[j].height = data[i++];
1940
1941             if (w*h == 0) continue;
1942
1943             self->icons[j].data = g_new(RrPixel32, w * h);
1944             for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1945                 if (x >= w) {
1946                     x = 0;
1947                     ++y;
1948                 }
1949                 self->icons[j].data[t] =
1950                     (((data[i] >> 24) & 0xff) << RrDefaultAlphaOffset) +
1951                     (((data[i] >> 16) & 0xff) << RrDefaultRedOffset) +
1952                     (((data[i] >> 8) & 0xff) << RrDefaultGreenOffset) +
1953                     (((data[i] >> 0) & 0xff) << RrDefaultBlueOffset);
1954             }
1955             g_assert(i <= num);
1956         }
1957
1958         g_free(data);
1959     } else {
1960         XWMHints *hints;
1961
1962         if ((hints = XGetWMHints(ob_display, self->window))) {
1963             if (hints->flags & IconPixmapHint) {
1964                 self->nicons++;
1965                 self->icons = g_new(ObClientIcon, self->nicons);
1966                 xerror_set_ignore(TRUE);
1967                 if (!RrPixmapToRGBA(ob_rr_inst,
1968                                     hints->icon_pixmap,
1969                                     (hints->flags & IconMaskHint ?
1970                                      hints->icon_mask : None),
1971                                     &self->icons[self->nicons-1].width,
1972                                     &self->icons[self->nicons-1].height,
1973                                     &self->icons[self->nicons-1].data)){
1974                     g_free(&self->icons[self->nicons-1]);
1975                     self->nicons--;
1976                 }
1977                 xerror_set_ignore(FALSE);
1978             }
1979             XFree(hints);
1980         }
1981     }
1982
1983     /* set the default icon onto the window
1984        in theory, this could be a race, but if a window doesn't set an icon
1985        or removes it entirely, it's not very likely it is going to set one
1986        right away afterwards */
1987     if (self->nicons == 0) {
1988         RrPixel32 *icon = ob_rr_theme->def_win_icon;
1989         gulong *data;
1990
1991         data = g_new(gulong, 48*48+2);
1992         data[0] = data[1] =  48;
1993         for (i = 0; i < 48*48; ++i)
1994             data[i+2] = (((icon[i] >> RrDefaultAlphaOffset) & 0xff) << 24) +
1995                 (((icon[i] >> RrDefaultRedOffset) & 0xff) << 16) +
1996                 (((icon[i] >> RrDefaultGreenOffset) & 0xff) << 8) +
1997                 (((icon[i] >> RrDefaultBlueOffset) & 0xff) << 0);
1998         PROP_SETA32(self->window, net_wm_icon, cardinal, data, 48*48+2);
1999         g_free(data);
2000     } else if (self->frame)
2001         /* don't draw the icon empty if we're just setting one now anyways,
2002            we'll get the property change any second */
2003         frame_adjust_icon(self->frame);
2004 }
2005
2006 void client_update_user_time(ObClient *self)
2007 {
2008     guint32 time;
2009
2010     if (PROP_GET32(self->window, net_wm_user_time, cardinal, &time)) {
2011         /* we set this every time, not just when it grows, because in practice
2012            sometimes time goes backwards! (ntpdate.. yay....) so.. if it goes
2013            backward we don't want all windows to stop focusing. we'll just
2014            assume noone is setting times older than the last one, cuz that
2015            would be pretty stupid anyways
2016         */
2017         self->user_time = time;
2018
2019         /*
2020         ob_debug("window %s user time %u\n", self->title, time);
2021         */
2022     }
2023 }
2024
2025 static void client_get_client_machine(ObClient *self)
2026 {
2027     gchar *data = NULL;
2028     gchar localhost[128];
2029
2030     g_free(self->client_machine);
2031
2032     if (PROP_GETS(self->window, wm_client_machine, locale, &data)) {
2033         gethostname(localhost, 127);
2034         localhost[127] = '\0';
2035         if (strcmp(localhost, data))
2036             self->client_machine = data;
2037     }
2038 }
2039
2040 static void client_change_wm_state(ObClient *self)
2041 {
2042     gulong state[2];
2043     glong old;
2044
2045     old = self->wmstate;
2046
2047     if (self->shaded || self->iconic || !self->frame->visible)
2048         self->wmstate = IconicState;
2049     else
2050         self->wmstate = NormalState;
2051
2052     if (old != self->wmstate) {
2053         PROP_MSG(self->window, kde_wm_change_state,
2054                  self->wmstate, 1, 0, 0);
2055
2056         state[0] = self->wmstate;
2057         state[1] = None;
2058         PROP_SETA32(self->window, wm_state, wm_state, state, 2);
2059     }
2060 }
2061
2062 static void client_change_state(ObClient *self)
2063 {
2064     gulong netstate[11];
2065     guint num;
2066
2067     num = 0;
2068     if (self->modal)
2069         netstate[num++] = prop_atoms.net_wm_state_modal;
2070     if (self->shaded)
2071         netstate[num++] = prop_atoms.net_wm_state_shaded;
2072     if (self->iconic)
2073         netstate[num++] = prop_atoms.net_wm_state_hidden;
2074     if (self->skip_taskbar)
2075         netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
2076     if (self->skip_pager)
2077         netstate[num++] = prop_atoms.net_wm_state_skip_pager;
2078     if (self->fullscreen)
2079         netstate[num++] = prop_atoms.net_wm_state_fullscreen;
2080     if (self->max_vert)
2081         netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
2082     if (self->max_horz)
2083         netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
2084     if (self->above)
2085         netstate[num++] = prop_atoms.net_wm_state_above;
2086     if (self->below)
2087         netstate[num++] = prop_atoms.net_wm_state_below;
2088     if (self->demands_attention)
2089         netstate[num++] = prop_atoms.net_wm_state_demands_attention;
2090     if (self->undecorated)
2091         netstate[num++] = prop_atoms.ob_wm_state_undecorated;
2092     PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
2093
2094     if (self->frame)
2095         frame_adjust_state(self->frame);
2096 }
2097
2098 ObClient *client_search_focus_tree(ObClient *self)
2099 {
2100     GSList *it;
2101     ObClient *ret;
2102
2103     for (it = self->transients; it; it = g_slist_next(it)) {
2104         if (client_focused(it->data)) return it->data;
2105         if ((ret = client_search_focus_tree(it->data))) return ret;
2106     }
2107     return NULL;
2108 }
2109
2110 ObClient *client_search_focus_tree_full(ObClient *self)
2111 {
2112     if (self->transient_for) {
2113         if (self->transient_for != OB_TRAN_GROUP) {
2114             return client_search_focus_tree_full(self->transient_for);
2115         } else {
2116             GSList *it;
2117             gboolean recursed = FALSE;
2118         
2119             for (it = self->group->members; it; it = g_slist_next(it))
2120                 if (!((ObClient*)it->data)->transient_for) {
2121                     ObClient *c;
2122                     if ((c = client_search_focus_tree_full(it->data)))
2123                         return c;
2124                     recursed = TRUE;
2125                 }
2126             if (recursed)
2127                 return NULL;
2128         }
2129     }
2130
2131     /* this function checks the whole tree, the client_search_focus_tree~
2132        does not, so we need to check this window */
2133     if (client_focused(self))
2134         return self;
2135     return client_search_focus_tree(self);
2136 }
2137
2138 static ObStackingLayer calc_layer(ObClient *self)
2139 {
2140     ObStackingLayer l;
2141
2142     if (self->fullscreen &&
2143         (client_focused(self) || client_search_focus_tree(self)))
2144         l = OB_STACKING_LAYER_FULLSCREEN;
2145     else if (self->type == OB_CLIENT_TYPE_DESKTOP)
2146         l = OB_STACKING_LAYER_DESKTOP;
2147     else if (self->type == OB_CLIENT_TYPE_DOCK) {
2148         if (self->below) l = OB_STACKING_LAYER_NORMAL;
2149         else l = OB_STACKING_LAYER_ABOVE;
2150     }
2151     else if (self->above) l = OB_STACKING_LAYER_ABOVE;
2152     else if (self->below) l = OB_STACKING_LAYER_BELOW;
2153     else l = OB_STACKING_LAYER_NORMAL;
2154
2155     return l;
2156 }
2157
2158 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
2159                                         ObStackingLayer min, gboolean raised)
2160 {
2161     ObStackingLayer old, own;
2162     GSList *it;
2163
2164     old = self->layer;
2165     own = calc_layer(self);
2166     self->layer = MAX(own, min);
2167
2168     for (it = self->transients; it; it = g_slist_next(it))
2169         client_calc_layer_recursive(it->data, orig,
2170                                     self->layer,
2171                                     raised ? raised : self->layer != old);
2172
2173     if (!raised && self->layer != old)
2174         if (orig->frame) { /* only restack if the original window is managed */
2175             stacking_remove(CLIENT_AS_WINDOW(self));
2176             stacking_add(CLIENT_AS_WINDOW(self));
2177         }
2178 }
2179
2180 void client_calc_layer(ObClient *self)
2181 {
2182     ObClient *orig;
2183     GSList *it;
2184
2185     orig = self;
2186
2187     /* transients take on the layer of their parents */
2188     it = client_search_all_top_parents(self);
2189
2190     for (; it; it = g_slist_next(it))
2191         client_calc_layer_recursive(it->data, orig, 0, FALSE);
2192 }
2193
2194 gboolean client_should_show(ObClient *self)
2195 {
2196     if (self->iconic)
2197         return FALSE;
2198     if (client_normal(self) && screen_showing_desktop)
2199         return FALSE;
2200     /*
2201     if (self->transient_for) {
2202         if (self->transient_for != OB_TRAN_GROUP)
2203             return client_should_show(self->transient_for);
2204         else {
2205             GSList *it;
2206
2207             for (it = self->group->members; it; it = g_slist_next(it)) {
2208                 ObClient *c = it->data;
2209                 if (c != self && !c->transient_for) {
2210                     if (client_should_show(c))
2211                         return TRUE;
2212                 }
2213             }
2214         }
2215     }
2216     */
2217     if (self->desktop == screen_desktop || self->desktop == DESKTOP_ALL)
2218         return TRUE;
2219     
2220     return FALSE;
2221 }
2222
2223 void client_show(ObClient *self)
2224 {
2225
2226     if (client_should_show(self)) {
2227         frame_show(self->frame);
2228     }
2229
2230     /* According to the ICCCM (sec 4.1.3.1) when a window is not visible, it
2231        needs to be in IconicState. This includes when it is on another
2232        desktop!
2233     */
2234     client_change_wm_state(self);
2235 }
2236
2237 void client_hide(ObClient *self)
2238 {
2239     if (!client_should_show(self)) {
2240         frame_hide(self->frame);
2241     }
2242
2243     /* According to the ICCCM (sec 4.1.3.1) when a window is not visible, it
2244        needs to be in IconicState. This includes when it is on another
2245        desktop!
2246     */
2247     client_change_wm_state(self);
2248 }
2249
2250 void client_showhide(ObClient *self)
2251 {
2252
2253     if (client_should_show(self)) {
2254         frame_show(self->frame);
2255     }
2256     else {
2257         frame_hide(self->frame);
2258     }
2259
2260     /* According to the ICCCM (sec 4.1.3.1) when a window is not visible, it
2261        needs to be in IconicState. This includes when it is on another
2262        desktop!
2263     */
2264     client_change_wm_state(self);
2265 }
2266
2267 gboolean client_normal(ObClient *self) {
2268     return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
2269               self->type == OB_CLIENT_TYPE_DOCK ||
2270               self->type == OB_CLIENT_TYPE_SPLASH);
2271 }
2272
2273 gboolean client_application(ObClient *self)
2274 {
2275     return (self->type == OB_CLIENT_TYPE_NORMAL ||
2276             self->type == OB_CLIENT_TYPE_DIALOG);
2277 }
2278
2279 static void client_apply_startup_state(ObClient *self, gint x, gint y)
2280 {
2281     gboolean pos = FALSE; /* has the window's position been configured? */
2282     gint ox, oy;
2283
2284     /* save the position, and set self->area for these to use */
2285     ox = self->area.x;
2286     oy = self->area.y;
2287     self->area.x = x;
2288     self->area.y = y;
2289
2290     /* set the desktop hint, to make sure that it always exists */
2291     PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
2292
2293     /* these are in a carefully crafted order.. */
2294
2295     if (self->iconic) {
2296         self->iconic = FALSE;
2297         client_iconify(self, TRUE, FALSE);
2298     }
2299     if (self->fullscreen) {
2300         self->fullscreen = FALSE;
2301         client_fullscreen(self, TRUE);
2302         pos = TRUE;
2303     }
2304     if (self->undecorated) {
2305         self->undecorated = FALSE;
2306         client_set_undecorated(self, TRUE);
2307     }
2308     if (self->shaded) {
2309         self->shaded = FALSE;
2310         client_shade(self, TRUE);
2311     }
2312     if (self->demands_attention) {
2313         self->demands_attention = FALSE;
2314         client_hilite(self, TRUE);
2315     }
2316   
2317     if (self->max_vert && self->max_horz) {
2318         self->max_vert = self->max_horz = FALSE;
2319         client_maximize(self, TRUE, 0);
2320         pos = TRUE;
2321     } else if (self->max_vert) {
2322         self->max_vert = FALSE;
2323         client_maximize(self, TRUE, 2);
2324         pos = TRUE;
2325     } else if (self->max_horz) {
2326         self->max_horz = FALSE;
2327         client_maximize(self, TRUE, 1);
2328         pos = TRUE;
2329     }
2330
2331     /* if the client didn't get positioned yet, then do so now
2332        call client_move even if the window is not being moved anywhere, because
2333        when we reparent it and decorate it, it is getting moved and we need to
2334        be telling it so with a ConfigureNotify event.
2335     */
2336     if (!pos) {
2337         /* use the saved position */
2338         self->area.x = ox;
2339         self->area.y = oy;
2340         client_move(self, x, y);
2341     }
2342
2343     /* nothing to do for the other states:
2344        skip_taskbar
2345        skip_pager
2346        modal
2347        above
2348        below
2349     */
2350 }
2351
2352 void client_convert_gravity(ObClient *self, gint gravity, gint *x, gint *y,
2353                             gint w, gint h)
2354 {
2355     gint oldg = self->gravity;
2356
2357     /* get the frame's position from the requested stuff */
2358     self->gravity = gravity;
2359     frame_client_gravity(self->frame, x, y, w, h);
2360     self->gravity = oldg;
2361
2362     /* get the client's position in its true gravity from that */
2363     frame_frame_gravity(self->frame, x, y, w, h);
2364 }
2365
2366 void client_try_configure(ObClient *self, gint *x, gint *y, gint *w, gint *h,
2367                           gint *logicalw, gint *logicalh,
2368                           gboolean user)
2369 {
2370     Rect desired_area = {*x, *y, *w, *h};
2371
2372     /* make the frame recalculate its dimentions n shit without changing
2373        anything visible for real, this way the constraints below can work with
2374        the updated frame dimensions. */
2375     frame_adjust_area(self->frame, TRUE, TRUE, TRUE);
2376
2377     /* work within the prefered sizes given by the window */
2378     if (!(*w == self->area.width && *h == self->area.height)) {
2379         gint basew, baseh, minw, minh;
2380
2381         /* base size is substituted with min size if not specified */
2382         if (self->base_size.width || self->base_size.height) {
2383             basew = self->base_size.width;
2384             baseh = self->base_size.height;
2385         } else {
2386             basew = self->min_size.width;
2387             baseh = self->min_size.height;
2388         }
2389         /* min size is substituted with base size if not specified */
2390         if (self->min_size.width || self->min_size.height) {
2391             minw = self->min_size.width;
2392             minh = self->min_size.height;
2393         } else {
2394             minw = self->base_size.width;
2395             minh = self->base_size.height;
2396         }
2397
2398         /* if this is a user-requested resize, then check against min/max
2399            sizes */
2400
2401         /* smaller than min size or bigger than max size? */
2402         if (*w > self->max_size.width) *w = self->max_size.width;
2403         if (*w < minw) *w = minw;
2404         if (*h > self->max_size.height) *h = self->max_size.height;
2405         if (*h < minh) *h = minh;
2406
2407         *w -= basew;
2408         *h -= baseh;
2409
2410         /* keep to the increments */
2411         *w /= self->size_inc.width;
2412         *h /= self->size_inc.height;
2413
2414         /* you cannot resize to nothing */
2415         if (basew + *w < 1) *w = 1 - basew;
2416         if (baseh + *h < 1) *h = 1 - baseh;
2417   
2418         /* save the logical size */
2419         *logicalw = self->size_inc.width > 1 ? *w : *w + basew;
2420         *logicalh = self->size_inc.height > 1 ? *h : *h + baseh;
2421
2422         *w *= self->size_inc.width;
2423         *h *= self->size_inc.height;
2424
2425         *w += basew;
2426         *h += baseh;
2427
2428         /* adjust the height to match the width for the aspect ratios.
2429            for this, min size is not substituted for base size ever. */
2430         *w -= self->base_size.width;
2431         *h -= self->base_size.height;
2432
2433         if (!self->fullscreen) {
2434             if (self->min_ratio)
2435                 if (*h * self->min_ratio > *w) {
2436                     *h = (gint)(*w / self->min_ratio);
2437
2438                     /* you cannot resize to nothing */
2439                     if (*h < 1) {
2440                         *h = 1;
2441                         *w = (gint)(*h * self->min_ratio);
2442                     }
2443                 }
2444             if (self->max_ratio)
2445                 if (*h * self->max_ratio < *w) {
2446                     *h = (gint)(*w / self->max_ratio);
2447
2448                     /* you cannot resize to nothing */
2449                     if (*h < 1) {
2450                         *h = 1;
2451                         *w = (gint)(*h * self->min_ratio);
2452                     }
2453                 }
2454         }
2455
2456         *w += self->base_size.width;
2457         *h += self->base_size.height;
2458     }
2459
2460     /* gets the frame's position */
2461     frame_client_gravity(self->frame, x, y, *w, *h);
2462
2463     /* these positions are frame positions, not client positions */
2464
2465     /* set the size and position if fullscreen */
2466     if (self->fullscreen) {
2467         Rect *a;
2468         guint i;
2469
2470         i = screen_find_monitor(&desired_area);
2471         a = screen_physical_area_monitor(i);
2472
2473         *x = a->x;
2474         *y = a->y;
2475         *w = a->width;
2476         *h = a->height;
2477
2478         user = FALSE; /* ignore if the client can't be moved/resized when it
2479                          is entering fullscreen */
2480     } else if (self->max_horz || self->max_vert) {
2481         Rect *a;
2482         guint i;
2483
2484         i = screen_find_monitor(&desired_area);
2485         a = screen_area_monitor(self->desktop, i);
2486
2487         /* set the size and position if maximized */
2488         if (self->max_horz) {
2489             *x = a->x;
2490             *w = a->width - self->frame->size.left - self->frame->size.right;
2491         }
2492         if (self->max_vert) {
2493             *y = a->y;
2494             *h = a->height - self->frame->size.top - self->frame->size.bottom;
2495         }
2496
2497         /* maximizing is not allowed if the user can't move+resize the window
2498          */
2499     }
2500
2501     /* gets the client's position */
2502     frame_frame_gravity(self->frame, x, y, *w, *h);
2503
2504     /* these override the above states! if you cant move you can't move! */
2505     if (user) {
2506         if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
2507             *x = self->area.x;
2508             *y = self->area.y;
2509         }
2510         if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
2511             *w = self->area.width;
2512             *h = self->area.height;
2513         }
2514     }
2515
2516     g_assert(*w > 0);
2517     g_assert(*h > 0);
2518 }
2519
2520
2521 void client_configure_full(ObClient *self, gint x, gint y, gint w, gint h,
2522                            gboolean user, gboolean final,
2523                            gboolean force_reply)
2524 {
2525     gint oldw, oldh, oldrx, oldry;
2526     gboolean send_resize_client;
2527     gboolean moved = FALSE, resized = FALSE, rootmoved = FALSE;
2528     guint fdecor = self->frame->decorations;
2529     gboolean fhorz = self->frame->max_horz;
2530     gint logicalw, logicalh;
2531
2532     /* find the new x, y, width, and height (and logical size) */
2533     client_try_configure(self, &x, &y, &w, &h, &logicalw, &logicalh, user);
2534
2535     /* set the logical size if things changed */
2536     if (!(w == self->area.width && h == self->area.height))
2537         SIZE_SET(self->logical_size, logicalw, logicalh);
2538
2539     /* figure out if we moved or resized or what */
2540     moved = x != self->area.x || y != self->area.y;
2541     resized = w != self->area.width || h != self->area.height;
2542
2543     oldw = self->area.width;
2544     oldh = self->area.height;
2545     RECT_SET(self->area, x, y, w, h);
2546
2547     /* for app-requested resizes, always resize if 'resized' is true.
2548        for user-requested ones, only resize if final is true, or when
2549        resizing in redraw mode */
2550     send_resize_client = ((!user && resized) ||
2551                           (user && (final ||
2552                                     (resized && config_resize_redraw))));
2553
2554     /* if the client is enlarging, then resize the client before the frame */
2555     if (send_resize_client && user && (w > oldw || h > oldh)) {
2556         XResizeWindow(ob_display, self->window, MAX(w, oldw), MAX(h, oldh));
2557         /* resize the plate to show the client padding color underneath */
2558         frame_adjust_client_area(self->frame);
2559     }
2560
2561     /* find the frame's dimensions and move/resize it */
2562     if (self->decorations != fdecor || self->max_horz != fhorz)
2563         moved = resized = TRUE;
2564     if (moved || resized)
2565         frame_adjust_area(self->frame, moved, resized, FALSE);
2566
2567     /* find the client's position relative to the root window */
2568     oldrx = self->root_pos.x;
2569     oldry = self->root_pos.y;
2570     rootmoved = (oldrx != (signed)(self->frame->area.x +
2571                                    self->frame->size.left -
2572                                    self->border_width) ||
2573                  oldry != (signed)(self->frame->area.y +
2574                                    self->frame->size.top -
2575                                    self->border_width));
2576
2577     if (force_reply || ((!user || (user && final)) && rootmoved))
2578     {
2579         XEvent event;
2580
2581         POINT_SET(self->root_pos,
2582                   self->frame->area.x + self->frame->size.left -
2583                   self->border_width,
2584                   self->frame->area.y + self->frame->size.top -
2585                   self->border_width);
2586
2587         event.type = ConfigureNotify;
2588         event.xconfigure.display = ob_display;
2589         event.xconfigure.event = self->window;
2590         event.xconfigure.window = self->window;
2591
2592         /* root window real coords */
2593         event.xconfigure.x = self->root_pos.x;
2594         event.xconfigure.y = self->root_pos.y;
2595         event.xconfigure.width = w;
2596         event.xconfigure.height = h;
2597         event.xconfigure.border_width = 0;
2598         event.xconfigure.above = self->frame->plate;
2599         event.xconfigure.override_redirect = FALSE;
2600         XSendEvent(event.xconfigure.display, event.xconfigure.window,
2601                    FALSE, StructureNotifyMask, &event);
2602     }
2603
2604     /* if the client is shrinking, then resize the frame before the client */
2605     if (send_resize_client && (!user || (w <= oldw || h <= oldh))) {
2606         /* resize the plate to show the client padding color underneath */
2607         frame_adjust_client_area(self->frame);
2608
2609         XResizeWindow(ob_display, self->window, w, h);
2610     }
2611
2612     XFlush(ob_display);
2613 }
2614
2615 void client_fullscreen(ObClient *self, gboolean fs)
2616 {
2617     gint x, y, w, h;
2618
2619     if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
2620         self->fullscreen == fs) return;                   /* already done */
2621
2622     self->fullscreen = fs;
2623     client_change_state(self); /* change the state hints on the client */
2624     client_calc_layer(self);   /* and adjust out layer/stacking */
2625
2626     if (fs) {
2627         self->pre_fullscreen_area = self->area;
2628         /* if the window is maximized, its area isn't all that meaningful.
2629            save it's premax area instead. */
2630         if (self->max_horz) {
2631             self->pre_fullscreen_area.x = self->pre_max_area.x;
2632             self->pre_fullscreen_area.width = self->pre_max_area.width;
2633         }
2634         if (self->max_vert) {
2635             self->pre_fullscreen_area.y = self->pre_max_area.y;
2636             self->pre_fullscreen_area.height = self->pre_max_area.height;
2637         }
2638
2639         /* these are not actually used cuz client_configure will set them
2640            as appropriate when the window is fullscreened */
2641         x = y = w = h = 0;
2642     } else {
2643         Rect *a;
2644
2645         if (self->pre_fullscreen_area.width > 0 &&
2646             self->pre_fullscreen_area.height > 0)
2647         {
2648             x = self->pre_fullscreen_area.x;
2649             y = self->pre_fullscreen_area.y;
2650             w = self->pre_fullscreen_area.width;
2651             h = self->pre_fullscreen_area.height;
2652             RECT_SET(self->pre_fullscreen_area, 0, 0, 0, 0);
2653         } else {
2654             /* pick some fallbacks... */
2655             a = screen_area_monitor(self->desktop, 0);
2656             x = a->x + a->width / 4;
2657             y = a->y + a->height / 4;
2658             w = a->width / 2;
2659             h = a->height / 2;
2660         }
2661     }
2662
2663     client_setup_decor_and_functions(self);
2664
2665     client_move_resize(self, x, y, w, h);
2666
2667     /* try focus us when we go into fullscreen mode */
2668     client_focus(self);
2669 }
2670
2671 static void client_iconify_recursive(ObClient *self,
2672                                      gboolean iconic, gboolean curdesk)
2673 {
2674     GSList *it;
2675     gboolean changed = FALSE;
2676
2677
2678     if (self->iconic != iconic) {
2679         ob_debug("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"),
2680                  self->window);
2681
2682         if (iconic) {
2683             if (self->functions & OB_CLIENT_FUNC_ICONIFY) {
2684                 self->iconic = iconic;
2685
2686                 /* update the focus lists.. iconic windows go to the bottom of
2687                    the list, put the new iconic window at the 'top of the
2688                    bottom'. */
2689                 focus_order_to_top(self);
2690
2691                 changed = TRUE;
2692             }
2693         } else {
2694             self->iconic = iconic;
2695
2696             if (curdesk && self->desktop != screen_desktop &&
2697                 self->desktop != DESKTOP_ALL)
2698                 client_set_desktop(self, screen_desktop, FALSE);
2699
2700             /* this puts it after the current focused window */
2701             focus_order_remove(self);
2702             focus_order_add_new(self);
2703
2704             changed = TRUE;
2705         }
2706     }
2707
2708     if (changed) {
2709         client_change_state(self);
2710         client_showhide(self);
2711         if (STRUT_EXISTS(self->strut))
2712             screen_update_areas();
2713     }
2714
2715     /* iconify all direct transients, and deiconify all transients
2716        (non-direct too) */
2717     for (it = self->transients; it; it = g_slist_next(it))
2718         if (it->data != self)
2719             if (client_is_direct_child(self, it->data) || !iconic)
2720                 client_iconify_recursive(it->data, iconic, curdesk);
2721 }
2722
2723 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk)
2724 {
2725     /* move up the transient chain as far as possible first */
2726     self = client_search_top_parent(self);
2727     client_iconify_recursive(self, iconic, curdesk);
2728 }
2729
2730 void client_maximize(ObClient *self, gboolean max, gint dir)
2731 {
2732     gint x, y, w, h;
2733      
2734     g_assert(dir == 0 || dir == 1 || dir == 2);
2735     if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE)) return; /* can't */
2736
2737     /* check if already done */
2738     if (max) {
2739         if (dir == 0 && self->max_horz && self->max_vert) return;
2740         if (dir == 1 && self->max_horz) return;
2741         if (dir == 2 && self->max_vert) return;
2742     } else {
2743         if (dir == 0 && !self->max_horz && !self->max_vert) return;
2744         if (dir == 1 && !self->max_horz) return;
2745         if (dir == 2 && !self->max_vert) return;
2746     }
2747
2748     /* we just tell it to configure in the same place and client_configure
2749        worries about filling the screen with the window */
2750     x = self->area.x;
2751     y = self->area.y;
2752     w = self->area.width;
2753     h = self->area.height;
2754
2755     if (max) {
2756         if ((dir == 0 || dir == 1) && !self->max_horz) { /* horz */
2757             RECT_SET(self->pre_max_area,
2758                      self->area.x, self->pre_max_area.y,
2759                      self->area.width, self->pre_max_area.height);
2760         }
2761         if ((dir == 0 || dir == 2) && !self->max_vert) { /* vert */
2762             RECT_SET(self->pre_max_area,
2763                      self->pre_max_area.x, self->area.y,
2764                      self->pre_max_area.width, self->area.height);
2765         }
2766     } else {
2767         Rect *a;
2768
2769         a = screen_area_monitor(self->desktop, 0);
2770         if ((dir == 0 || dir == 1) && self->max_horz) { /* horz */
2771             if (self->pre_max_area.width > 0) {
2772                 x = self->pre_max_area.x;
2773                 w = self->pre_max_area.width;
2774
2775                 RECT_SET(self->pre_max_area, 0, self->pre_max_area.y,
2776                          0, self->pre_max_area.height);
2777             } else {
2778                 /* pick some fallbacks... */
2779                 x = a->x + a->width / 4;
2780                 w = a->width / 2;
2781             }
2782         }
2783         if ((dir == 0 || dir == 2) && self->max_vert) { /* vert */
2784             if (self->pre_max_area.height > 0) {
2785                 y = self->pre_max_area.y;
2786                 h = self->pre_max_area.height;
2787
2788                 RECT_SET(self->pre_max_area, self->pre_max_area.x, 0,
2789                          self->pre_max_area.width, 0);
2790             } else {
2791                 /* pick some fallbacks... */
2792                 y = a->y + a->height / 4;
2793                 h = a->height / 2;
2794             }
2795         }
2796     }
2797
2798     if (dir == 0 || dir == 1) /* horz */
2799         self->max_horz = max;
2800     if (dir == 0 || dir == 2) /* vert */
2801         self->max_vert = max;
2802
2803     client_change_state(self); /* change the state hints on the client */
2804
2805     client_setup_decor_and_functions(self);
2806
2807     client_move_resize(self, x, y, w, h);
2808 }
2809
2810 void client_shade(ObClient *self, gboolean shade)
2811 {
2812     if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
2813          shade) ||                         /* can't shade */
2814         self->shaded == shade) return;     /* already done */
2815
2816     self->shaded = shade;
2817     client_change_state(self);
2818     client_change_wm_state(self); /* the window is being hidden/shown */
2819     /* resize the frame to just the titlebar */
2820     frame_adjust_area(self->frame, FALSE, FALSE, FALSE);
2821 }
2822
2823 void client_close(ObClient *self)
2824 {
2825     XEvent ce;
2826
2827     if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
2828
2829     /* in the case that the client provides no means to requesting that it
2830        close, we just kill it */
2831     if (!self->delete_window)
2832         client_kill(self);
2833     
2834     /*
2835       XXX: itd be cool to do timeouts and shit here for killing the client's
2836       process off
2837       like... if the window is around after 5 seconds, then the close button
2838       turns a nice red, and if this function is called again, the client is
2839       explicitly killed.
2840     */
2841
2842     ce.xclient.type = ClientMessage;
2843     ce.xclient.message_type =  prop_atoms.wm_protocols;
2844     ce.xclient.display = ob_display;
2845     ce.xclient.window = self->window;
2846     ce.xclient.format = 32;
2847     ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
2848     ce.xclient.data.l[1] = event_curtime;
2849     ce.xclient.data.l[2] = 0l;
2850     ce.xclient.data.l[3] = 0l;
2851     ce.xclient.data.l[4] = 0l;
2852     XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2853 }
2854
2855 void client_kill(ObClient *self)
2856 {
2857     XKillClient(ob_display, self->window);
2858 }
2859
2860 void client_hilite(ObClient *self, gboolean hilite)
2861 {
2862     if (self->demands_attention == hilite)
2863         return; /* no change */
2864
2865     /* don't allow focused windows to hilite */
2866     self->demands_attention = hilite && !client_focused(self);
2867     if (self->frame != NULL) { /* if we're mapping, just set the state */
2868         if (self->demands_attention)
2869             frame_flash_start(self->frame);
2870         else
2871             frame_flash_stop(self->frame);
2872         client_change_state(self);
2873     }
2874 }
2875
2876 void client_set_desktop_recursive(ObClient *self,
2877                                   guint target, gboolean donthide)
2878 {
2879     guint old;
2880     GSList *it;
2881
2882     if (target != self->desktop) {
2883
2884         ob_debug("Setting desktop %u\n", target+1);
2885
2886         g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2887
2888         /* remove from the old desktop(s) */
2889         focus_order_remove(self);
2890
2891         old = self->desktop;
2892         self->desktop = target;
2893         PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2894         /* the frame can display the current desktop state */
2895         frame_adjust_state(self->frame);
2896         /* 'move' the window to the new desktop */
2897         if (!donthide)
2898             client_showhide(self);
2899         /* raise if it was not already on the desktop */
2900         if (old != DESKTOP_ALL)
2901             client_raise(self);
2902         if (STRUT_EXISTS(self->strut))
2903             screen_update_areas();
2904
2905         /* add to the new desktop(s) */
2906         if (config_focus_new)
2907             focus_order_to_top(self);
2908         else
2909             focus_order_to_bottom(self);
2910     }
2911
2912     /* move all transients */
2913     for (it = self->transients; it; it = g_slist_next(it))
2914         if (it->data != self)
2915             if (client_is_direct_child(self, it->data))
2916                 client_set_desktop_recursive(it->data, target, donthide);
2917 }
2918
2919 void client_set_desktop(ObClient *self, guint target, gboolean donthide)
2920 {
2921     self = client_search_top_parent(self);
2922     client_set_desktop_recursive(self, target, donthide);
2923 }
2924
2925 gboolean client_is_direct_child(ObClient *parent, ObClient *child)
2926 {
2927     while (child != parent &&
2928            child->transient_for && child->transient_for != OB_TRAN_GROUP)
2929         child = child->transient_for;
2930     return child == parent;
2931 }
2932
2933 ObClient *client_search_modal_child(ObClient *self)
2934 {
2935     GSList *it;
2936     ObClient *ret;
2937   
2938     for (it = self->transients; it; it = g_slist_next(it)) {
2939         ObClient *c = it->data;
2940         if ((ret = client_search_modal_child(c))) return ret;
2941         if (c->modal) return c;
2942     }
2943     return NULL;
2944 }
2945
2946 gboolean client_validate(ObClient *self)
2947 {
2948     XEvent e; 
2949
2950     XSync(ob_display, FALSE); /* get all events on the server */
2951
2952     if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2953         XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2954         XPutBackEvent(ob_display, &e);
2955         return FALSE;
2956     }
2957
2958     return TRUE;
2959 }
2960
2961 void client_set_wm_state(ObClient *self, glong state)
2962 {
2963     if (state == self->wmstate) return; /* no change */
2964   
2965     switch (state) {
2966     case IconicState:
2967         client_iconify(self, TRUE, TRUE);
2968         break;
2969     case NormalState:
2970         client_iconify(self, FALSE, TRUE);
2971         break;
2972     }
2973 }
2974
2975 void client_set_state(ObClient *self, Atom action, glong data1, glong data2)
2976 {
2977     gboolean shaded = self->shaded;
2978     gboolean fullscreen = self->fullscreen;
2979     gboolean undecorated = self->undecorated;
2980     gboolean max_horz = self->max_horz;
2981     gboolean max_vert = self->max_vert;
2982     gboolean modal = self->modal;
2983     gboolean iconic = self->iconic;
2984     gboolean demands_attention = self->demands_attention;
2985     gint i;
2986
2987     if (!(action == prop_atoms.net_wm_state_add ||
2988           action == prop_atoms.net_wm_state_remove ||
2989           action == prop_atoms.net_wm_state_toggle))
2990         /* an invalid action was passed to the client message, ignore it */
2991         return; 
2992
2993     for (i = 0; i < 2; ++i) {
2994         Atom state = i == 0 ? data1 : data2;
2995     
2996         if (!state) continue;
2997
2998         /* if toggling, then pick whether we're adding or removing */
2999         if (action == prop_atoms.net_wm_state_toggle) {
3000             if (state == prop_atoms.net_wm_state_modal)
3001                 action = modal ? prop_atoms.net_wm_state_remove :
3002                     prop_atoms.net_wm_state_add;
3003             else if (state == prop_atoms.net_wm_state_maximized_vert)
3004                 action = self->max_vert ? prop_atoms.net_wm_state_remove :
3005                     prop_atoms.net_wm_state_add;
3006             else if (state == prop_atoms.net_wm_state_maximized_horz)
3007                 action = self->max_horz ? prop_atoms.net_wm_state_remove :
3008                     prop_atoms.net_wm_state_add;
3009             else if (state == prop_atoms.net_wm_state_shaded)
3010                 action = shaded ? prop_atoms.net_wm_state_remove :
3011                     prop_atoms.net_wm_state_add;
3012             else if (state == prop_atoms.net_wm_state_skip_taskbar)
3013                 action = self->skip_taskbar ?
3014                     prop_atoms.net_wm_state_remove :
3015                     prop_atoms.net_wm_state_add;
3016             else if (state == prop_atoms.net_wm_state_skip_pager)
3017                 action = self->skip_pager ?
3018                     prop_atoms.net_wm_state_remove :
3019                     prop_atoms.net_wm_state_add;
3020             else if (state == prop_atoms.net_wm_state_hidden)
3021                 action = self->iconic ?
3022                     prop_atoms.net_wm_state_remove :
3023                     prop_atoms.net_wm_state_add;
3024             else if (state == prop_atoms.net_wm_state_fullscreen)
3025                 action = fullscreen ?
3026                     prop_atoms.net_wm_state_remove :
3027                     prop_atoms.net_wm_state_add;
3028             else if (state == prop_atoms.net_wm_state_above)
3029                 action = self->above ? prop_atoms.net_wm_state_remove :
3030                     prop_atoms.net_wm_state_add;
3031             else if (state == prop_atoms.net_wm_state_below)
3032                 action = self->below ? prop_atoms.net_wm_state_remove :
3033                     prop_atoms.net_wm_state_add;
3034             else if (state == prop_atoms.net_wm_state_demands_attention)
3035                 action = self->demands_attention ?
3036                     prop_atoms.net_wm_state_remove :
3037                     prop_atoms.net_wm_state_add;
3038             else if (state == prop_atoms.ob_wm_state_undecorated)
3039                 action = undecorated ? prop_atoms.net_wm_state_remove :
3040                     prop_atoms.net_wm_state_add;
3041         }
3042     
3043         if (action == prop_atoms.net_wm_state_add) {
3044             if (state == prop_atoms.net_wm_state_modal) {
3045                 modal = TRUE;
3046             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
3047                 max_vert = TRUE;
3048             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
3049                 max_horz = TRUE;
3050             } else if (state == prop_atoms.net_wm_state_shaded) {
3051                 shaded = TRUE;
3052             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
3053                 self->skip_taskbar = TRUE;
3054             } else if (state == prop_atoms.net_wm_state_skip_pager) {
3055                 self->skip_pager = TRUE;
3056             } else if (state == prop_atoms.net_wm_state_hidden) {
3057                 iconic = TRUE;
3058             } else if (state == prop_atoms.net_wm_state_fullscreen) {
3059                 fullscreen = TRUE;
3060             } else if (state == prop_atoms.net_wm_state_above) {
3061                 self->above = TRUE;
3062                 self->below = FALSE;
3063             } else if (state == prop_atoms.net_wm_state_below) {
3064                 self->above = FALSE;
3065                 self->below = TRUE;
3066             } else if (state == prop_atoms.net_wm_state_demands_attention) {
3067                 demands_attention = TRUE;
3068             } else if (state == prop_atoms.ob_wm_state_undecorated) {
3069                 undecorated = TRUE;
3070             }
3071
3072         } else { /* action == prop_atoms.net_wm_state_remove */
3073             if (state == prop_atoms.net_wm_state_modal) {
3074                 modal = FALSE;
3075             } else if (state == prop_atoms.net_wm_state_maximized_vert) {
3076                 max_vert = FALSE;
3077             } else if (state == prop_atoms.net_wm_state_maximized_horz) {
3078                 max_horz = FALSE;
3079             } else if (state == prop_atoms.net_wm_state_shaded) {
3080                 shaded = FALSE;
3081             } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
3082                 self->skip_taskbar = FALSE;
3083             } else if (state == prop_atoms.net_wm_state_skip_pager) {
3084                 self->skip_pager = FALSE;
3085             } else if (state == prop_atoms.net_wm_state_hidden) {
3086                 iconic = FALSE;
3087             } else if (state == prop_atoms.net_wm_state_fullscreen) {
3088                 fullscreen = FALSE;
3089             } else if (state == prop_atoms.net_wm_state_above) {
3090                 self->above = FALSE;
3091             } else if (state == prop_atoms.net_wm_state_below) {
3092                 self->below = FALSE;
3093             } else if (state == prop_atoms.net_wm_state_demands_attention) {
3094                 demands_attention = FALSE;
3095             } else if (state == prop_atoms.ob_wm_state_undecorated) {
3096                 undecorated = FALSE;
3097             }
3098         }
3099     }
3100     if (max_horz != self->max_horz || max_vert != self->max_vert) {
3101         if (max_horz != self->max_horz && max_vert != self->max_vert) {
3102             /* toggling both */
3103             if (max_horz == max_vert) { /* both going the same way */
3104                 client_maximize(self, max_horz, 0);
3105             } else {
3106                 client_maximize(self, max_horz, 1);
3107                 client_maximize(self, max_vert, 2);
3108             }
3109         } else {
3110             /* toggling one */
3111             if (max_horz != self->max_horz)
3112                 client_maximize(self, max_horz, 1);
3113             else
3114                 client_maximize(self, max_vert, 2);
3115         }
3116     }
3117     /* change fullscreen state before shading, as it will affect if the window
3118        can shade or not */
3119     if (fullscreen != self->fullscreen)
3120         client_fullscreen(self, fullscreen);
3121     if (shaded != self->shaded)
3122         client_shade(self, shaded);
3123     if (undecorated != self->undecorated)
3124         client_set_undecorated(self, undecorated);
3125     if (modal != self->modal) {
3126         self->modal = modal;
3127         /* when a window changes modality, then its stacking order with its
3128            transients needs to change */
3129         client_raise(self);
3130     }
3131     if (iconic != self->iconic)
3132         client_iconify(self, iconic, FALSE);
3133
3134     if (demands_attention != self->demands_attention)
3135         client_hilite(self, demands_attention);
3136
3137     client_change_state(self); /* change the hint to reflect these changes */
3138 }
3139
3140 ObClient *client_focus_target(ObClient *self)
3141 {
3142     ObClient *child = NULL;
3143
3144     child = client_search_modal_child(self);
3145     if (child) return child;
3146     return self;
3147 }
3148
3149 gboolean client_can_focus(ObClient *self)
3150 {
3151     XEvent ev;
3152
3153     /* choose the correct target */
3154     self = client_focus_target(self);
3155
3156     if (!self->frame->visible)
3157         return FALSE;
3158
3159     if (!(self->can_focus || self->focus_notify))
3160         return FALSE;
3161
3162     /* do a check to see if the window has already been unmapped or destroyed
3163        do this intelligently while watching out for unmaps we've generated
3164        (ignore_unmaps > 0) */
3165     if (XCheckTypedWindowEvent(ob_display, self->window,
3166                                DestroyNotify, &ev)) {
3167         XPutBackEvent(ob_display, &ev);
3168         return FALSE;
3169     }
3170     while (XCheckTypedWindowEvent(ob_display, self->window,
3171                                   UnmapNotify, &ev)) {
3172         if (self->ignore_unmaps) {
3173             self->ignore_unmaps--;
3174         } else {
3175             XPutBackEvent(ob_display, &ev);
3176             return FALSE;
3177         }
3178     }
3179
3180     return TRUE;
3181 }
3182
3183 gboolean client_focus(ObClient *self)
3184 {
3185     /* choose the correct target */
3186     self = client_focus_target(self);
3187
3188     if (!client_can_focus(self)) {
3189         if (!self->frame->visible) {
3190             /* update the focus lists */
3191             focus_order_to_top(self);
3192         }
3193         return FALSE;
3194     }
3195
3196     ob_debug_type(OB_DEBUG_FOCUS,
3197                   "Focusing client \"%s\" at time %u\n",
3198                   self->title, event_curtime);
3199
3200     if (self->can_focus) {
3201         /* This can cause a BadMatch error with CurrentTime, or if an app
3202            passed in a bad time for _NET_WM_ACTIVE_WINDOW. */
3203         xerror_set_ignore(TRUE);
3204         XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
3205                        event_curtime);
3206         xerror_set_ignore(FALSE);
3207     }
3208
3209     if (self->focus_notify) {
3210         XEvent ce;
3211         ce.xclient.type = ClientMessage;
3212         ce.xclient.message_type = prop_atoms.wm_protocols;
3213         ce.xclient.display = ob_display;
3214         ce.xclient.window = self->window;
3215         ce.xclient.format = 32;
3216         ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
3217         ce.xclient.data.l[1] = event_curtime;
3218         ce.xclient.data.l[2] = 0l;
3219         ce.xclient.data.l[3] = 0l;
3220         ce.xclient.data.l[4] = 0l;
3221         XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
3222     }
3223
3224 #ifdef DEBUG_FOCUS
3225     ob_debug("%sively focusing %lx at %d\n",
3226              (self->can_focus ? "act" : "pass"),
3227              self->window, (gint) event_curtime);
3228 #endif
3229
3230     /* Cause the FocusIn to come back to us. Important for desktop switches,
3231        since otherwise we'll have no FocusIn on the queue and send it off to
3232        the focus_backup. */
3233     XSync(ob_display, FALSE);
3234     return TRUE;
3235 }
3236
3237 void client_activate(ObClient *self, gboolean here, gboolean user)
3238 {
3239     guint32 last_time = focus_client ? focus_client->user_time : CurrentTime;
3240
3241     /* XXX do some stuff here if user is false to determine if we really want
3242        to activate it or not (a parent or group member is currently
3243        active)?
3244     */
3245     ob_debug_type(OB_DEBUG_FOCUS,
3246                   "Want to activate window 0x%x with time %u (last time %u), "
3247                   "source=%s\n",
3248                   self->window, event_curtime, last_time,
3249                   (user ? "user" : "application"));
3250
3251     if (!user && event_curtime && last_time &&
3252         !event_time_after(event_curtime, last_time))
3253     {
3254         client_hilite(self, TRUE);
3255     } else {
3256         if (event_curtime != CurrentTime)
3257             self->user_time = event_curtime;
3258
3259         /* if using focus_delay, stop the timer now so that focus doesn't
3260            go moving on us */
3261         event_halt_focus_delay();
3262
3263         if (client_normal(self) && screen_showing_desktop)
3264             screen_show_desktop(FALSE, FALSE);
3265         if (self->iconic)
3266             client_iconify(self, FALSE, here);
3267         if (self->desktop != DESKTOP_ALL &&
3268             self->desktop != screen_desktop)
3269         {
3270             if (here)
3271                 client_set_desktop(self, screen_desktop, FALSE);
3272             else
3273                 screen_set_desktop(self->desktop);
3274         } else if (!self->frame->visible)
3275             /* if its not visible for other reasons, then don't mess
3276                with it */
3277             return;
3278         if (self->shaded)
3279             client_shade(self, FALSE);
3280
3281         client_focus(self);
3282
3283         /* we do this as an action here. this is rather important. this is
3284            because we want the results from the focus change to take place 
3285            BEFORE we go about raising the window. when a fullscreen window 
3286            loses focus, we need this or else the raise wont be able to raise 
3287            above the to-lose-focus fullscreen window. */
3288         client_raise(self);
3289     }
3290 }
3291
3292 void client_raise(ObClient *self)
3293 {
3294     action_run_string("Raise", self, CurrentTime);
3295 }
3296
3297 void client_lower(ObClient *self)
3298 {
3299     action_run_string("Lower", self, CurrentTime);
3300 }
3301
3302 gboolean client_focused(ObClient *self)
3303 {
3304     return self == focus_client;
3305 }
3306
3307 static ObClientIcon* client_icon_recursive(ObClient *self, gint w, gint h)
3308 {
3309     guint i;
3310     /* si is the smallest image >= req */
3311     /* li is the largest image < req */
3312     gulong size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
3313
3314     if (!self->nicons) {
3315         ObClientIcon *parent = NULL;
3316
3317         if (self->transient_for) {
3318             if (self->transient_for != OB_TRAN_GROUP)
3319                 parent = client_icon_recursive(self->transient_for, w, h);
3320             else {
3321                 GSList *it;
3322                 for (it = self->group->members; it; it = g_slist_next(it)) {
3323                     ObClient *c = it->data;
3324                     if (c != self && !c->transient_for) {
3325                         if ((parent = client_icon_recursive(c, w, h)))
3326                             break;
3327                     }
3328                 }
3329             }
3330         }
3331         
3332         return parent;
3333     }
3334
3335     for (i = 0; i < self->nicons; ++i) {
3336         size = self->icons[i].width * self->icons[i].height;
3337         if (size < smallest && size >= (unsigned)(w * h)) {
3338             smallest = size;
3339             si = i;
3340         }
3341         if (size > largest && size <= (unsigned)(w * h)) {
3342             largest = size;
3343             li = i;
3344         }
3345     }
3346     if (largest == 0) /* didnt find one smaller than the requested size */
3347         return &self->icons[si];
3348     return &self->icons[li];
3349 }
3350
3351 const ObClientIcon* client_icon(ObClient *self, gint w, gint h)
3352 {
3353     ObClientIcon *ret;
3354     static ObClientIcon deficon;
3355
3356     if (!(ret = client_icon_recursive(self, w, h))) {
3357         deficon.width = deficon.height = 48;
3358         deficon.data = ob_rr_theme->def_win_icon;
3359         ret = &deficon;
3360     }
3361     return ret;
3362 }
3363
3364 void client_set_layer(ObClient *self, gint layer)
3365 {
3366     if (layer < 0) {
3367         self->below = TRUE;
3368         self->above = FALSE;
3369     } else if (layer == 0) {
3370         self->below = self->above = FALSE;
3371     } else {
3372         self->below = FALSE;
3373         self->above = TRUE;
3374     }
3375     client_calc_layer(self);
3376     client_change_state(self); /* reflect this in the state hints */
3377 }
3378
3379 void client_set_undecorated(ObClient *self, gboolean undecorated)
3380 {
3381     if (self->undecorated != undecorated) {
3382         self->undecorated = undecorated;
3383         client_setup_decor_and_functions(self);
3384         /* Make sure the client knows it might have moved. Maybe there is a
3385          * better way of doing this so only one client_configure is sent, but
3386          * since 125 of these are sent per second when moving the window (with
3387          * user = FALSE) i doubt it matters much.
3388          */
3389         client_configure(self, self->area.x, self->area.y,
3390                          self->area.width, self->area.height, TRUE, TRUE);
3391         client_change_state(self); /* reflect this in the state hints */
3392     }
3393 }
3394
3395 guint client_monitor(ObClient *self)
3396 {
3397     return screen_find_monitor(&self->frame->area);
3398 }
3399
3400 ObClient *client_search_top_parent(ObClient *self)
3401 {
3402     while (self->transient_for && self->transient_for != OB_TRAN_GROUP &&
3403            client_normal(self))
3404         self = self->transient_for;
3405     return self;
3406 }
3407
3408 GSList *client_search_all_top_parents(ObClient *self)
3409 {
3410     GSList *ret = NULL;
3411
3412     /* move up the direct transient chain as far as possible */
3413     while (self->transient_for && self->transient_for != OB_TRAN_GROUP)
3414         self = self->transient_for;
3415
3416     if (!self->transient_for)
3417         ret = g_slist_prepend(ret, self);
3418     else {
3419             GSList *it;
3420
3421             g_assert(self->group);
3422
3423             for (it = self->group->members; it; it = g_slist_next(it)) {
3424                 ObClient *c = it->data;
3425
3426                 if (!c->transient_for && client_normal(c))
3427                     ret = g_slist_prepend(ret, c);
3428             }
3429
3430             if (ret == NULL) /* no group parents */
3431                 ret = g_slist_prepend(ret, self);
3432     }
3433
3434     return ret;
3435 }
3436
3437 ObClient *client_search_focus_parent(ObClient *self)
3438 {
3439     if (self->transient_for) {
3440         if (self->transient_for != OB_TRAN_GROUP) {
3441             if (client_focused(self->transient_for))
3442                 return self->transient_for;
3443         } else {
3444             GSList *it;
3445
3446             for (it = self->group->members; it; it = g_slist_next(it)) {
3447                 ObClient *c = it->data;
3448
3449                 /* checking transient_for prevents infinate loops! */
3450                 if (c != self && !c->transient_for)
3451                     if (client_focused(c))
3452                         return c;
3453             }
3454         }
3455     }
3456
3457     return NULL;
3458 }
3459
3460 ObClient *client_search_parent(ObClient *self, ObClient *search)
3461 {
3462     if (self->transient_for) {
3463         if (self->transient_for != OB_TRAN_GROUP) {
3464             if (self->transient_for == search)
3465                 return search;
3466         } else {
3467             GSList *it;
3468
3469             for (it = self->group->members; it; it = g_slist_next(it)) {
3470                 ObClient *c = it->data;
3471
3472                 /* checking transient_for prevents infinate loops! */
3473                 if (c != self && !c->transient_for)
3474                     if (c == search)
3475                         return search;
3476             }
3477         }
3478     }
3479
3480     return NULL;
3481 }
3482
3483 ObClient *client_search_transient(ObClient *self, ObClient *search)
3484 {
3485     GSList *sit;
3486
3487     for (sit = self->transients; sit; sit = g_slist_next(sit)) {
3488         if (sit->data == search)
3489             return search;
3490         if (client_search_transient(sit->data, search))
3491             return search;
3492     }
3493     return NULL;
3494 }
3495
3496 void client_update_sm_client_id(ObClient *self)
3497 {
3498     g_free(self->sm_client_id);
3499     self->sm_client_id = NULL;
3500
3501     if (!PROP_GETS(self->window, sm_client_id, locale, &self->sm_client_id) &&
3502         self->group)
3503         PROP_GETS(self->group->leader, sm_client_id, locale,
3504                   &self->sm_client_id);
3505 }
3506
3507 #define WANT_EDGE(cur, c) \
3508             if(cur == c)                                                      \
3509                 continue;                                                     \
3510             if(!client_normal(cur))                                   \
3511                 continue;                                                     \
3512             if(screen_desktop != cur->desktop && cur->desktop != DESKTOP_ALL) \
3513                 continue;                                                     \
3514             if(cur->iconic)                                                   \
3515                 continue;                                                     \
3516             if(cur->layer < c->layer && !config_resist_layers_below)          \
3517                 continue;
3518
3519 #define HIT_EDGE(my_edge_start, my_edge_end, his_edge_start, his_edge_end) \
3520             if ((his_edge_start >= my_edge_start && \
3521                  his_edge_start <= my_edge_end) ||  \
3522                 (my_edge_start >= his_edge_start && \
3523                  my_edge_start <= his_edge_end))    \
3524                 dest = his_offset;
3525
3526 /* finds the nearest edge in the given direction from the current client
3527  * note to self: the edge is the -frame- edge (the actual one), not the
3528  * client edge.
3529  */
3530 gint client_directional_edge_search(ObClient *c, ObDirection dir, gboolean hang)
3531 {
3532     gint dest, monitor_dest;
3533     gint my_edge_start, my_edge_end, my_offset;
3534     GList *it;
3535     Rect *a, *monitor;
3536     
3537     if(!client_list)
3538         return -1;
3539
3540     a = screen_area(c->desktop);
3541     monitor = screen_area_monitor(c->desktop, client_monitor(c));
3542
3543     switch(dir) {
3544     case OB_DIRECTION_NORTH:
3545         my_edge_start = c->frame->area.x;
3546         my_edge_end = c->frame->area.x + c->frame->area.width;
3547         my_offset = c->frame->area.y + (hang ? c->frame->area.height : 0);
3548         
3549         /* default: top of screen */
3550         dest = a->y + (hang ? c->frame->area.height : 0);
3551         monitor_dest = monitor->y + (hang ? c->frame->area.height : 0);
3552         /* if the monitor edge comes before the screen edge, */
3553         /* use that as the destination instead. (For xinerama) */
3554         if (monitor_dest != dest && my_offset > monitor_dest)
3555             dest = monitor_dest; 
3556
3557         for(it = client_list; it && my_offset != dest; it = g_list_next(it)) {
3558             gint his_edge_start, his_edge_end, his_offset;
3559             ObClient *cur = it->data;
3560
3561             WANT_EDGE(cur, c)
3562
3563             his_edge_start = cur->frame->area.x;
3564             his_edge_end = cur->frame->area.x + cur->frame->area.width;
3565             his_offset = cur->frame->area.y + 
3566                          (hang ? 0 : cur->frame->area.height);
3567
3568             if(his_offset + 1 > my_offset)
3569                 continue;
3570
3571             if(his_offset < dest)
3572                 continue;
3573
3574             HIT_EDGE(my_edge_start, my_edge_end, his_edge_start, his_edge_end)
3575         }
3576         break;
3577     case OB_DIRECTION_SOUTH:
3578         my_edge_start = c->frame->area.x;
3579         my_edge_end = c->frame->area.x + c->frame->area.width;
3580         my_offset = c->frame->area.y + (hang ? 0 : c->frame->area.height);
3581
3582         /* default: bottom of screen */
3583         dest = a->y + a->height - (hang ? c->frame->area.height : 0);
3584         monitor_dest = monitor->y + monitor->height -
3585                        (hang ? c->frame->area.height : 0);
3586         /* if the monitor edge comes before the screen edge, */
3587         /* use that as the destination instead. (For xinerama) */
3588         if (monitor_dest != dest && my_offset < monitor_dest)
3589             dest = monitor_dest; 
3590
3591         for(it = client_list; it && my_offset != dest; it = g_list_next(it)) {
3592             gint his_edge_start, his_edge_end, his_offset;
3593             ObClient *cur = it->data;
3594
3595             WANT_EDGE(cur, c)
3596
3597             his_edge_start = cur->frame->area.x;
3598             his_edge_end = cur->frame->area.x + cur->frame->area.width;
3599             his_offset = cur->frame->area.y +
3600                          (hang ? cur->frame->area.height : 0);
3601
3602
3603             if(his_offset - 1 < my_offset)
3604                 continue;
3605             
3606             if(his_offset > dest)
3607                 continue;
3608
3609             HIT_EDGE(my_edge_start, my_edge_end, his_edge_start, his_edge_end)
3610         }
3611         break;
3612     case OB_DIRECTION_WEST:
3613         my_edge_start = c->frame->area.y;
3614         my_edge_end = c->frame->area.y + c->frame->area.height;
3615         my_offset = c->frame->area.x + (hang ? c->frame->area.width : 0);
3616
3617         /* default: leftmost egde of screen */
3618         dest = a->x + (hang ? c->frame->area.width : 0);
3619         monitor_dest = monitor->x + (hang ? c->frame->area.width : 0);
3620         /* if the monitor edge comes before the screen edge, */
3621         /* use that as the destination instead. (For xinerama) */
3622         if (monitor_dest != dest && my_offset > monitor_dest)
3623             dest = monitor_dest;            
3624
3625         for(it = client_list; it && my_offset != dest; it = g_list_next(it)) {
3626             gint his_edge_start, his_edge_end, his_offset;
3627             ObClient *cur = it->data;
3628
3629             WANT_EDGE(cur, c)
3630
3631             his_edge_start = cur->frame->area.y;
3632             his_edge_end = cur->frame->area.y + cur->frame->area.height;
3633             his_offset = cur->frame->area.x +
3634                          (hang ? 0 : cur->frame->area.width);
3635
3636             if(his_offset + 1 > my_offset)
3637                 continue;
3638
3639             if(his_offset < dest)
3640                 continue;
3641
3642             HIT_EDGE(my_edge_start, my_edge_end, his_edge_start, his_edge_end)
3643         }
3644        break;
3645     case OB_DIRECTION_EAST:
3646         my_edge_start = c->frame->area.y;
3647         my_edge_end = c->frame->area.y + c->frame->area.height;
3648         my_offset = c->frame->area.x + (hang ? 0 : c->frame->area.width);
3649         
3650         /* default: rightmost edge of screen */
3651         dest = a->x + a->width - (hang ? c->frame->area.width : 0);
3652         monitor_dest = monitor->x + monitor->width -
3653                        (hang ? c->frame->area.width : 0);
3654         /* if the monitor edge comes before the screen edge, */
3655         /* use that as the destination instead. (For xinerama) */
3656         if (monitor_dest != dest && my_offset < monitor_dest)
3657             dest = monitor_dest;            
3658
3659         for(it = client_list; it && my_offset != dest; it = g_list_next(it)) {
3660             gint his_edge_start, his_edge_end, his_offset;
3661             ObClient *cur = it->data;
3662
3663             WANT_EDGE(cur, c)
3664
3665             his_edge_start = cur->frame->area.y;
3666             his_edge_end = cur->frame->area.y + cur->frame->area.height;
3667             his_offset = cur->frame->area.x +
3668                          (hang ? cur->frame->area.width : 0);
3669
3670             if(his_offset - 1 < my_offset)
3671                 continue;
3672             
3673             if(his_offset > dest)
3674                 continue;
3675
3676             HIT_EDGE(my_edge_start, my_edge_end, his_edge_start, his_edge_end)
3677         }
3678         break;
3679     case OB_DIRECTION_NORTHEAST:
3680     case OB_DIRECTION_SOUTHEAST:
3681     case OB_DIRECTION_NORTHWEST:
3682     case OB_DIRECTION_SOUTHWEST:
3683         /* not implemented */
3684     default:
3685         g_assert_not_reached();
3686         dest = 0; /* suppress warning */
3687     }
3688     return dest;
3689 }
3690
3691 ObClient* client_under_pointer()
3692 {
3693     gint x, y;
3694     GList *it;
3695     ObClient *ret = NULL;
3696
3697     if (screen_pointer_pos(&x, &y)) {
3698         for (it = stacking_list; it; it = g_list_next(it)) {
3699             if (WINDOW_IS_CLIENT(it->data)) {
3700                 ObClient *c = WINDOW_AS_CLIENT(it->data);
3701                 if (c->frame->visible &&
3702                     RECT_CONTAINS(c->frame->area, x, y)) {
3703                     ret = c;
3704                     break;
3705                 }
3706             }
3707         }
3708     }
3709     return ret;
3710 }
3711
3712 gboolean client_has_group_siblings(ObClient *self)
3713 {
3714     return self->group && self->group->members->next;
3715 }
3716
3717 gboolean client_has_application_group_siblings(ObClient *self)
3718 {
3719     GSList *it;
3720
3721     if (!self->group) return FALSE;
3722
3723     for (it = self->group->members; it; it = g_slist_next(it)) {
3724         ObClient *c = it->data;
3725         if (c != self && client_application(c))
3726             return TRUE;
3727     }
3728     return FALSE;
3729 }