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