1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 client.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 See the COPYING file for a copy of the GNU General Public License.
21 #include "startupnotify.h"
25 #include "moveresize.h"
28 #include "extensions.h"
38 #include "menuframe.h"
41 #include "render/render.h"
44 #include <X11/Xutil.h>
46 /*! The event mask to grab on client windows */
47 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
50 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
53 GList *client_list = NULL;
54 GSList *client_destructors = NULL;
56 static void client_get_all(ObClient *self);
57 static void client_toggle_border(ObClient *self, gboolean show);
58 static void client_get_startup_id(ObClient *self);
59 static void client_get_area(ObClient *self);
60 static void client_get_desktop(ObClient *self);
61 static void client_get_state(ObClient *self);
62 static void client_get_shaped(ObClient *self);
63 static void client_get_mwm_hints(ObClient *self);
64 static void client_get_gravity(ObClient *self);
65 static void client_showhide(ObClient *self);
66 static void client_change_allowed_actions(ObClient *self);
67 static void client_change_state(ObClient *self);
68 static void client_apply_startup_state(ObClient *self);
69 static void client_restore_session_state(ObClient *self);
70 static void client_restore_session_stacking(ObClient *self);
71 static void client_urgent_notify(ObClient *self);
73 void client_startup(gboolean reconfig)
80 void client_shutdown(gboolean reconfig)
84 void client_add_destructor(GDestroyNotify func)
86 client_destructors = g_slist_prepend(client_destructors, (gpointer)func);
89 void client_remove_destructor(GDestroyNotify func)
91 client_destructors = g_slist_remove(client_destructors, (gpointer)func);
94 void client_set_list()
96 Window *windows, *win_it;
98 guint size = g_list_length(client_list);
100 /* create an array of the window ids */
102 windows = g_new(Window, size);
104 for (it = client_list; it != NULL; it = it->next, ++win_it)
105 *win_it = ((ObClient*)it->data)->window;
109 PROP_SETA32(RootWindow(ob_display, ob_screen),
110 net_client_list, window, (guint32*)windows, size);
119 void client_foreach_transient(ObClient *self, ObClientForeachFunc func, void *data)
123 for (it = self->transients; it; it = it->next) {
124 if (!func(it->data, data)) return;
125 client_foreach_transient(it->data, func, data);
129 void client_foreach_ancestor(ObClient *self, ObClientForeachFunc func, void *data)
131 if (self->transient_for) {
132 if (self->transient_for != OB_TRAN_GROUP) {
133 if (!func(self->transient_for, data)) return;
134 client_foreach_ancestor(self->transient_for, func, data);
138 for (it = self->group->members; it; it = it->next)
139 if (it->data != self &&
140 !((ObClient*)it->data)->transient_for) {
141 if (!func(it->data, data)) return;
142 client_foreach_ancestor(it->data, func, data);
149 void client_manage_all()
151 unsigned int i, j, nchild;
154 XWindowAttributes attrib;
156 XQueryTree(ob_display, RootWindow(ob_display, ob_screen),
157 &w, &w, &children, &nchild);
159 /* remove all icon windows from the list */
160 for (i = 0; i < nchild; i++) {
161 if (children[i] == None) continue;
162 wmhints = XGetWMHints(ob_display, children[i]);
164 if ((wmhints->flags & IconWindowHint) &&
165 (wmhints->icon_window != children[i]))
166 for (j = 0; j < nchild; j++)
167 if (children[j] == wmhints->icon_window) {
175 for (i = 0; i < nchild; ++i) {
176 if (children[i] == None)
178 if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
179 if (attrib.override_redirect) continue;
181 if (attrib.map_state != IsUnmapped)
182 client_manage(children[i]);
188 void client_manage(Window window)
192 XWindowAttributes attrib;
193 XSetWindowAttributes attrib_set;
195 gboolean activate = FALSE;
199 /* check if it has already been unmapped by the time we started mapping
200 the grab does a sync so we don't have to here */
201 if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
202 XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
203 XPutBackEvent(ob_display, &e);
206 return; /* don't manage it */
209 /* make sure it isn't an override-redirect window */
210 if (!XGetWindowAttributes(ob_display, window, &attrib) ||
211 attrib.override_redirect) {
213 return; /* don't manage it */
216 /* is the window a docking app */
217 if ((wmhint = XGetWMHints(ob_display, window))) {
218 if ((wmhint->flags & StateHint) &&
219 wmhint->initial_state == WithdrawnState) {
220 dock_add(window, wmhint);
228 ob_debug("Managing window: %lx\n", window);
230 /* choose the events we want to receive on the CLIENT window */
231 attrib_set.event_mask = CLIENT_EVENTMASK;
232 attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
233 XChangeWindowAttributes(ob_display, window,
234 CWEventMask|CWDontPropagate, &attrib_set);
237 /* create the ObClient struct, and populate it from the hints on the
239 self = g_new0(ObClient, 1);
240 self->obwin.type = Window_Client;
241 self->window = window;
243 /* non-zero defaults */
244 self->title_count = 1;
245 self->wmstate = NormalState;
247 self->desktop = screen_num_desktops; /* always an invalid value */
249 client_get_all(self);
250 client_restore_session_state(self);
252 sn_app_started(self->class);
254 client_change_state(self);
256 /* remove the client's border (and adjust re gravity) */
257 client_toggle_border(self, FALSE);
259 /* specify that if we exit, the window should not be destroyed and should
260 be reparented back to root automatically */
261 XChangeSaveSet(ob_display, window, SetModeInsert);
263 /* create the decoration frame for the client window */
264 self->frame = frame_new();
266 frame_grab_client(self->frame, self);
270 client_apply_startup_state(self);
272 /* update the focus lists */
273 focus_order_add_new(self);
275 stacking_add(CLIENT_AS_WINDOW(self));
276 client_restore_session_stacking(self);
278 /* focus the new window? */
279 if (ob_state() != OB_STATE_STARTING &&
280 (config_focus_new || (self->transient_for &&
281 self->transient_for != OB_TRAN_GROUP &&
282 client_focused(self->transient_for))) &&
283 /* note the check against Type_Normal/Dialog, not client_normal(self),
284 which would also include other types. in this case we want more
285 strict rules for focus */
286 (self->type == OB_CLIENT_TYPE_NORMAL ||
287 self->type == OB_CLIENT_TYPE_DIALOG))
291 if (self->desktop != screen_desktop) {
292 /* activate the window */
295 gboolean group_foc = FALSE;
300 for (it = self->group->members; it; it = it->next)
302 if (client_focused(it->data))
310 (!self->transient_for && (!self->group ||
311 !self->group->members->next))) ||
312 client_search_focus_tree_full(self) ||
314 !client_normal(focus_client))
316 /* activate the window */
323 if (ob_state() == OB_STATE_RUNNING) {
324 int x = self->area.x, ox = x;
325 int y = self->area.y, oy = y;
327 place_client(self, &x, &y);
329 /* make sure the window is visible */
330 client_find_onscreen(self, &x, &y,
331 self->frame->area.width,
332 self->frame->area.height,
333 client_normal(self));
335 if (x != ox || y != oy)
336 client_move(self, x, y);
339 client_showhide(self);
341 /* use client_focus instead of client_activate cuz client_activate does
342 stuff like switch desktops etc and I'm not interested in all that when
343 a window maps since its not based on an action from the user like
344 clicking a window to activate is. so keep the new window out of the way
346 if (activate) client_focus(self);
348 /* client_activate does this but we aret using it so we have to do it
350 if (screen_showing_desktop)
351 screen_show_desktop(FALSE);
353 /* add to client list/map */
354 client_list = g_list_append(client_list, self);
355 g_hash_table_insert(window_map, &self->window, self);
357 /* this has to happen after we're in the client_list */
358 screen_update_areas();
360 /* update the list hints */
363 keyboard_grab_for_client(self, TRUE);
364 mouse_grab_for_client(self, TRUE);
366 ob_debug("Managed window 0x%lx (%s)\n", window, self->class);
369 void client_unmanage_all()
371 while (client_list != NULL)
372 client_unmanage(client_list->data);
375 void client_unmanage(ObClient *self)
380 ob_debug("Unmanaging window: %lx (%s)\n", self->window, self->class);
382 g_assert(self != NULL);
384 keyboard_grab_for_client(self, FALSE);
385 mouse_grab_for_client(self, FALSE);
387 /* remove the window from our save set */
388 XChangeSaveSet(ob_display, self->window, SetModeDelete);
390 /* we dont want events no more */
391 XSelectInput(ob_display, self->window, NoEventMask);
393 frame_hide(self->frame);
395 client_list = g_list_remove(client_list, self);
396 stacking_remove(self);
397 g_hash_table_remove(window_map, &self->window);
399 /* update the focus lists */
400 focus_order_remove(self);
402 /* once the client is out of the list, update the struts to remove it's
404 screen_update_areas();
406 /* tell our parent(s) that we're gone */
407 if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
410 for (it = self->group->members; it; it = it->next)
411 if (it->data != self)
412 ((ObClient*)it->data)->transients =
413 g_slist_remove(((ObClient*)it->data)->transients, self);
414 } else if (self->transient_for) { /* transient of window */
415 self->transient_for->transients =
416 g_slist_remove(self->transient_for->transients, self);
419 /* tell our transients that we're gone */
420 for (it = self->transients; it != NULL; it = it->next) {
421 if (((ObClient*)it->data)->transient_for != OB_TRAN_GROUP) {
422 ((ObClient*)it->data)->transient_for = NULL;
423 client_calc_layer(it->data);
427 for (it = client_destructors; it; it = g_slist_next(it)) {
428 GDestroyNotify func = (GDestroyNotify) it->data;
432 if (focus_client == self) {
435 /* focus the last focused window on the desktop, and ignore enter
436 events from the unmap so it doesnt mess with the focus */
437 while (XCheckTypedEvent(ob_display, EnterNotify, &e));
438 client_unfocus(self);
441 /* remove from its group */
443 group_remove(self->group, self);
447 /* give the client its border back */
448 client_toggle_border(self, TRUE);
450 /* reparent the window out of the frame, and free the frame */
451 frame_release_client(self->frame, self);
454 if (ob_state() != OB_STATE_EXITING) {
455 /* these values should not be persisted across a window
457 PROP_ERASE(self->window, net_wm_desktop);
458 PROP_ERASE(self->window, net_wm_state);
459 PROP_ERASE(self->window, wm_state);
461 /* if we're left in an iconic state, the client wont be mapped. this is
462 bad, since we will no longer be managing the window on restart */
464 XMapWindow(ob_display, self->window);
468 ob_debug("Unmanaged window 0x%lx\n", self->window);
470 /* free all data allocated in the client struct */
471 g_slist_free(self->transients);
472 for (j = 0; j < self->nicons; ++j)
473 g_free(self->icons[j].data);
474 if (self->nicons > 0)
477 g_free(self->icon_title);
481 g_free(self->sm_client_id);
484 /* update the list hints */
488 static void client_urgent_notify(ObClient *self)
491 frame_flash_start(self->frame);
493 frame_flash_stop(self->frame);
496 static void client_restore_session_state(ObClient *self)
500 if (!(it = session_state_find(self)))
503 self->session = it->data;
505 RECT_SET(self->area, self->session->x, self->session->y,
506 self->session->w, self->session->h);
507 self->positioned = TRUE;
508 XResizeWindow(ob_display, self->window,
509 self->session->w, self->session->h);
511 self->desktop = (self->session->desktop == DESKTOP_ALL ?
512 self->session->desktop :
513 MIN(screen_num_desktops - 1, self->session->desktop));
514 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
516 self->shaded = self->session->shaded;
517 self->iconic = self->session->iconic;
518 self->skip_pager = self->session->skip_pager;
519 self->skip_taskbar = self->session->skip_taskbar;
520 self->fullscreen = self->session->fullscreen;
521 self->above = self->session->above;
522 self->below = self->session->below;
523 self->max_horz = self->session->max_horz;
524 self->max_vert = self->session->max_vert;
527 static void client_restore_session_stacking(ObClient *self)
531 if (!self->session) return;
533 it = g_list_find(session_saved_state, self->session);
534 for (it = g_list_previous(it); it; it = g_list_previous(it)) {
537 for (cit = client_list; cit; cit = g_list_next(cit))
538 if (session_state_cmp(it->data, cit->data))
541 client_calc_layer(self);
542 stacking_below(CLIENT_AS_WINDOW(self),
543 CLIENT_AS_WINDOW(cit->data));
549 void client_move_onscreen(ObClient *self, gboolean rude)
551 int x = self->area.x;
552 int y = self->area.y;
553 if (client_find_onscreen(self, &x, &y,
554 self->frame->area.width,
555 self->frame->area.height, rude)) {
556 client_move(self, x, y);
560 gboolean client_find_onscreen(ObClient *self, int *x, int *y, int w, int h,
564 int ox = *x, oy = *y;
566 frame_client_gravity(self->frame, x, y); /* get where the frame
569 /* XXX watch for xinerama dead areas */
571 a = screen_area(self->desktop);
572 if (client_normal(self)) {
573 if (!self->strut.right && *x >= a->x + a->width - 1)
574 *x = a->x + a->width - self->frame->area.width;
575 if (!self->strut.bottom && *y >= a->y + a->height - 1)
576 *y = a->y + a->height - self->frame->area.height;
577 if (!self->strut.left && *x + self->frame->area.width - 1 < a->x)
579 if (!self->strut.top && *y + self->frame->area.height - 1 < a->y)
584 /* this is my MOZILLA BITCHSLAP. oh ya it fucking feels good.
585 Java can suck it too. */
587 /* dont let windows map/move into the strut unless they
588 are bigger than the available area */
590 if (!self->strut.left && *x < a->x) *x = a->x;
591 if (!self->strut.right && *x + w > a->x + a->width)
592 *x = a->x + a->width - w;
594 if (h <= a->height) {
595 if (!self->strut.top && *y < a->y) *y = a->y;
596 if (!self->strut.bottom && *y + h > a->y + a->height)
597 *y = a->y + a->height - h;
601 frame_frame_gravity(self->frame, x, y); /* get where the client
604 return ox != *x || oy != *y;
607 static void client_toggle_border(ObClient *self, gboolean show)
609 /* adjust our idea of where the client is, based on its border. When the
610 border is removed, the client should now be considered to be in a
612 when re-adding the border to the client, the same operation needs to be
614 int oldx = self->area.x, oldy = self->area.y;
615 int x = oldx, y = oldy;
616 switch(self->gravity) {
618 case NorthWestGravity:
620 case SouthWestGravity:
622 case NorthEastGravity:
624 case SouthEastGravity:
625 if (show) x -= self->border_width * 2;
626 else x += self->border_width * 2;
633 if (show) x -= self->border_width;
634 else x += self->border_width;
637 switch(self->gravity) {
639 case NorthWestGravity:
641 case NorthEastGravity:
643 case SouthWestGravity:
645 case SouthEastGravity:
646 if (show) y -= self->border_width * 2;
647 else y += self->border_width * 2;
654 if (show) y -= self->border_width;
655 else y += self->border_width;
662 XSetWindowBorderWidth(ob_display, self->window, self->border_width);
664 /* move the client so it is back it the right spot _with_ its
666 if (x != oldx || y != oldy)
667 XMoveWindow(ob_display, self->window, x, y);
669 XSetWindowBorderWidth(ob_display, self->window, 0);
673 static void client_get_all(ObClient *self)
675 client_get_area(self);
676 client_update_transient_for(self);
677 client_update_wmhints(self);
678 client_get_startup_id(self);
679 client_get_desktop(self);
680 client_get_state(self);
681 client_get_shaped(self);
683 client_get_mwm_hints(self);
684 client_get_type(self);/* this can change the mwmhints for special cases */
686 client_update_protocols(self);
688 client_get_gravity(self); /* get the attribute gravity */
689 client_update_normal_hints(self); /* this may override the attribute
692 /* got the type, the mwmhints, the protocols, and the normal hints
693 (min/max sizes), so we're ready to set up the decorations/functions */
694 client_setup_decor_and_functions(self);
696 client_update_title(self);
697 client_update_class(self);
698 client_update_sm_client_id(self);
699 client_update_strut(self);
700 client_update_icons(self);
703 static void client_get_startup_id(ObClient *self)
705 if (!(PROP_GETS(self->window, net_startup_id, utf8, &self->startup_id)))
707 PROP_GETS(self->group->leader,
708 net_startup_id, utf8, &self->startup_id);
711 static void client_get_area(ObClient *self)
713 XWindowAttributes wattrib;
716 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
717 g_assert(ret != BadWindow);
719 RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
720 self->border_width = wattrib.border_width;
723 static void client_get_desktop(ObClient *self)
725 guint32 d = screen_num_desktops; /* an always-invalid value */
727 if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
728 if (d >= screen_num_desktops && d != DESKTOP_ALL)
729 self->desktop = screen_num_desktops - 1;
733 gboolean trdesk = FALSE;
735 if (self->transient_for) {
736 if (self->transient_for != OB_TRAN_GROUP) {
737 self->desktop = self->transient_for->desktop;
742 for (it = self->group->members; it; it = it->next)
743 if (it->data != self &&
744 !((ObClient*)it->data)->transient_for) {
745 self->desktop = ((ObClient*)it->data)->desktop;
752 /* try get from the startup-notification protocol */
753 if (sn_get_desktop(self->startup_id, &self->desktop)) {
754 if (self->desktop >= screen_num_desktops &&
755 self->desktop != DESKTOP_ALL)
756 self->desktop = screen_num_desktops - 1;
758 /* defaults to the current desktop */
759 self->desktop = screen_desktop;
762 if (self->desktop != d) {
763 /* set the desktop hint, to make sure that it always exists */
764 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
768 static void client_get_state(ObClient *self)
773 if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
775 for (i = 0; i < num; ++i) {
776 if (state[i] == prop_atoms.net_wm_state_modal)
778 else if (state[i] == prop_atoms.net_wm_state_shaded)
780 else if (state[i] == prop_atoms.net_wm_state_hidden)
782 else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
783 self->skip_taskbar = TRUE;
784 else if (state[i] == prop_atoms.net_wm_state_skip_pager)
785 self->skip_pager = TRUE;
786 else if (state[i] == prop_atoms.net_wm_state_fullscreen)
787 self->fullscreen = TRUE;
788 else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
789 self->max_vert = TRUE;
790 else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
791 self->max_horz = TRUE;
792 else if (state[i] == prop_atoms.net_wm_state_above)
794 else if (state[i] == prop_atoms.net_wm_state_below)
796 else if (state[i] == prop_atoms.ob_wm_state_undecorated)
797 self->undecorated = TRUE;
804 static void client_get_shaped(ObClient *self)
806 self->shaped = FALSE;
808 if (extensions_shape) {
813 XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
815 XShapeQueryExtents(ob_display, self->window, &s, &foo,
816 &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
818 self->shaped = (s != 0);
823 void client_update_transient_for(ObClient *self)
826 ObClient *target = NULL;
828 if (XGetTransientForHint(ob_display, self->window, &t)) {
829 self->transient = TRUE;
830 if (t != self->window) { /* cant be transient to itself! */
831 target = g_hash_table_lookup(window_map, &t);
832 /* if this happens then we need to check for it*/
833 g_assert(target != self);
834 if (target && !WINDOW_IS_CLIENT(target)) {
835 /* this can happen when a dialog is a child of
836 a dockapp, for example */
840 if (!target && self->group) {
841 /* not transient to a client, see if it is transient for a
843 if (t == self->group->leader ||
845 t == RootWindow(ob_display, ob_screen)) {
846 /* window is a transient for its group! */
847 target = OB_TRAN_GROUP;
852 self->transient = FALSE;
854 /* if anything has changed... */
855 if (target != self->transient_for) {
856 if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
859 /* remove from old parents */
860 for (it = self->group->members; it; it = g_slist_next(it)) {
861 ObClient *c = it->data;
862 if (c != self && !c->transient_for)
863 c->transients = g_slist_remove(c->transients, self);
865 } else if (self->transient_for != NULL) { /* transient of window */
866 /* remove from old parent */
867 self->transient_for->transients =
868 g_slist_remove(self->transient_for->transients, self);
870 self->transient_for = target;
871 if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
874 /* add to new parents */
875 for (it = self->group->members; it; it = g_slist_next(it)) {
876 ObClient *c = it->data;
877 if (c != self && !c->transient_for)
878 c->transients = g_slist_append(c->transients, self);
881 /* remove all transients which are in the group, that causes
882 circlular pointer hell of doom */
883 for (it = self->group->members; it; it = g_slist_next(it)) {
885 for (sit = self->transients; sit; sit = next) {
886 next = g_slist_next(sit);
887 if (sit->data == it->data)
889 g_slist_delete_link(self->transients, sit);
892 } else if (self->transient_for != NULL) { /* transient of window */
893 /* add to new parent */
894 self->transient_for->transients =
895 g_slist_append(self->transient_for->transients, self);
900 static void client_get_mwm_hints(ObClient *self)
905 self->mwmhints.flags = 0; /* default to none */
907 if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
909 if (num >= OB_MWM_ELEMENTS) {
910 self->mwmhints.flags = hints[0];
911 self->mwmhints.functions = hints[1];
912 self->mwmhints.decorations = hints[2];
918 void client_get_type(ObClient *self)
925 if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
926 /* use the first value that we know about in the array */
927 for (i = 0; i < num; ++i) {
928 if (val[i] == prop_atoms.net_wm_window_type_desktop)
929 self->type = OB_CLIENT_TYPE_DESKTOP;
930 else if (val[i] == prop_atoms.net_wm_window_type_dock)
931 self->type = OB_CLIENT_TYPE_DOCK;
932 else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
933 self->type = OB_CLIENT_TYPE_TOOLBAR;
934 else if (val[i] == prop_atoms.net_wm_window_type_menu)
935 self->type = OB_CLIENT_TYPE_MENU;
936 else if (val[i] == prop_atoms.net_wm_window_type_utility)
937 self->type = OB_CLIENT_TYPE_UTILITY;
938 else if (val[i] == prop_atoms.net_wm_window_type_splash)
939 self->type = OB_CLIENT_TYPE_SPLASH;
940 else if (val[i] == prop_atoms.net_wm_window_type_dialog)
941 self->type = OB_CLIENT_TYPE_DIALOG;
942 else if (val[i] == prop_atoms.net_wm_window_type_normal)
943 self->type = OB_CLIENT_TYPE_NORMAL;
944 else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
945 /* prevent this window from getting any decor or
947 self->mwmhints.flags &= (OB_MWM_FLAG_FUNCTIONS |
948 OB_MWM_FLAG_DECORATIONS);
949 self->mwmhints.decorations = 0;
950 self->mwmhints.functions = 0;
952 if (self->type != (ObClientType) -1)
953 break; /* grab the first legit type */
958 if (self->type == (ObClientType) -1) {
959 /*the window type hint was not set, which means we either classify
960 ourself as a normal window or a dialog, depending on if we are a
963 self->type = OB_CLIENT_TYPE_DIALOG;
965 self->type = OB_CLIENT_TYPE_NORMAL;
969 void client_update_protocols(ObClient *self)
974 self->focus_notify = FALSE;
975 self->delete_window = FALSE;
977 if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
978 for (i = 0; i < num_return; ++i) {
979 if (proto[i] == prop_atoms.wm_delete_window) {
980 /* this means we can request the window to close */
981 self->delete_window = TRUE;
982 } else if (proto[i] == prop_atoms.wm_take_focus)
983 /* if this protocol is requested, then the window will be
984 notified whenever we want it to receive focus */
985 self->focus_notify = TRUE;
991 static void client_get_gravity(ObClient *self)
993 XWindowAttributes wattrib;
996 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
997 g_assert(ret != BadWindow);
998 self->gravity = wattrib.win_gravity;
1001 void client_update_normal_hints(ObClient *self)
1005 int oldgravity = self->gravity;
1008 self->min_ratio = 0.0f;
1009 self->max_ratio = 0.0f;
1010 SIZE_SET(self->size_inc, 1, 1);
1011 SIZE_SET(self->base_size, 0, 0);
1012 SIZE_SET(self->min_size, 0, 0);
1013 SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
1015 /* get the hints from the window */
1016 if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
1017 self->positioned = !!(size.flags & (PPosition|USPosition));
1019 if (size.flags & PWinGravity) {
1020 self->gravity = size.win_gravity;
1022 /* if the client has a frame, i.e. has already been mapped and
1023 is changing its gravity */
1024 if (self->frame && self->gravity != oldgravity) {
1025 /* move our idea of the client's position based on its new
1027 self->area.x = self->frame->area.x;
1028 self->area.y = self->frame->area.y;
1029 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
1033 if (size.flags & PAspect) {
1034 if (size.min_aspect.y)
1035 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
1036 if (size.max_aspect.y)
1037 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
1040 if (size.flags & PMinSize)
1041 SIZE_SET(self->min_size, size.min_width, size.min_height);
1043 if (size.flags & PMaxSize)
1044 SIZE_SET(self->max_size, size.max_width, size.max_height);
1046 if (size.flags & PBaseSize)
1047 SIZE_SET(self->base_size, size.base_width, size.base_height);
1049 if (size.flags & PResizeInc)
1050 SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
1054 void client_setup_decor_and_functions(ObClient *self)
1056 /* start with everything (cept fullscreen) */
1058 (OB_FRAME_DECOR_TITLEBAR |
1059 (ob_rr_theme->show_handle ? OB_FRAME_DECOR_HANDLE : 0) |
1060 OB_FRAME_DECOR_GRIPS |
1061 OB_FRAME_DECOR_BORDER |
1062 OB_FRAME_DECOR_ICON |
1063 OB_FRAME_DECOR_ALLDESKTOPS |
1064 OB_FRAME_DECOR_ICONIFY |
1065 OB_FRAME_DECOR_MAXIMIZE |
1066 OB_FRAME_DECOR_SHADE);
1068 (OB_CLIENT_FUNC_RESIZE |
1069 OB_CLIENT_FUNC_MOVE |
1070 OB_CLIENT_FUNC_ICONIFY |
1071 OB_CLIENT_FUNC_MAXIMIZE |
1072 OB_CLIENT_FUNC_SHADE);
1073 if (self->delete_window) {
1074 self->functions |= OB_CLIENT_FUNC_CLOSE;
1075 self->decorations |= OB_FRAME_DECOR_CLOSE;
1078 if (!(self->min_size.width < self->max_size.width ||
1079 self->min_size.height < self->max_size.height))
1080 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1082 switch (self->type) {
1083 case OB_CLIENT_TYPE_NORMAL:
1084 /* normal windows retain all of the possible decorations and
1085 functionality, and are the only windows that you can fullscreen */
1086 self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1089 case OB_CLIENT_TYPE_DIALOG:
1090 case OB_CLIENT_TYPE_UTILITY:
1091 /* these windows cannot be maximized */
1092 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1095 case OB_CLIENT_TYPE_MENU:
1096 case OB_CLIENT_TYPE_TOOLBAR:
1097 /* these windows get less functionality */
1098 self->functions &= ~(OB_CLIENT_FUNC_ICONIFY | OB_CLIENT_FUNC_RESIZE);
1101 case OB_CLIENT_TYPE_DESKTOP:
1102 case OB_CLIENT_TYPE_DOCK:
1103 case OB_CLIENT_TYPE_SPLASH:
1104 /* none of these windows are manipulated by the window manager */
1105 self->decorations = 0;
1106 self->functions = 0;
1110 /* Mwm Hints are applied subtractively to what has already been chosen for
1111 decor and functionality */
1112 if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
1113 if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
1114 if (! ((self->mwmhints.decorations & OB_MWM_DECOR_HANDLE) ||
1115 (self->mwmhints.decorations & OB_MWM_DECOR_TITLE)))
1116 /* if the mwm hints request no handle or title, then all
1117 decorations are disabled */
1118 self->decorations = 0;
1122 if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
1123 if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
1124 if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
1125 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1126 if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
1127 self->functions &= ~OB_CLIENT_FUNC_MOVE;
1128 /* dont let mwm hints kill any buttons
1129 if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
1130 self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
1131 if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
1132 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1134 /* dont let mwm hints kill the close button
1135 if (! (self->mwmhints.functions & MwmFunc_Close))
1136 self->functions &= ~OB_CLIENT_FUNC_CLOSE; */
1140 if (!(self->functions & OB_CLIENT_FUNC_SHADE))
1141 self->decorations &= ~OB_FRAME_DECOR_SHADE;
1142 if (!(self->functions & OB_CLIENT_FUNC_ICONIFY))
1143 self->decorations &= ~OB_FRAME_DECOR_ICONIFY;
1144 if (!(self->functions & OB_CLIENT_FUNC_RESIZE))
1145 self->decorations &= ~OB_FRAME_DECOR_GRIPS;
1147 /* can't maximize without moving/resizing */
1148 if (!((self->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
1149 (self->functions & OB_CLIENT_FUNC_MOVE) &&
1150 (self->functions & OB_CLIENT_FUNC_RESIZE))) {
1151 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1152 self->decorations &= ~OB_FRAME_DECOR_MAXIMIZE;
1155 /* kill the handle on fully maxed windows */
1156 if (self->max_vert && self->max_horz)
1157 self->decorations &= ~OB_FRAME_DECOR_HANDLE;
1159 /* finally, the user can have requested no decorations, which overrides
1161 if (self->undecorated)
1162 self->decorations = OB_FRAME_DECOR_BORDER;
1164 /* if we don't have a titlebar, then we cannot shade! */
1165 if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1166 self->functions &= ~OB_CLIENT_FUNC_SHADE;
1168 /* now we need to check against rules for the client's current state */
1169 if (self->fullscreen) {
1170 self->functions &= (OB_CLIENT_FUNC_CLOSE |
1171 OB_CLIENT_FUNC_FULLSCREEN |
1172 OB_CLIENT_FUNC_ICONIFY);
1173 self->decorations = 0;
1176 client_change_allowed_actions(self);
1179 /* adjust the client's decorations, etc. */
1180 client_reconfigure(self);
1182 /* this makes sure that these windows appear on all desktops */
1183 if (self->type == OB_CLIENT_TYPE_DESKTOP &&
1184 self->desktop != DESKTOP_ALL)
1186 self->desktop = DESKTOP_ALL;
1191 static void client_change_allowed_actions(ObClient *self)
1196 /* desktop windows are kept on all desktops */
1197 if (self->type != OB_CLIENT_TYPE_DESKTOP)
1198 actions[num++] = prop_atoms.net_wm_action_change_desktop;
1200 if (self->functions & OB_CLIENT_FUNC_SHADE)
1201 actions[num++] = prop_atoms.net_wm_action_shade;
1202 if (self->functions & OB_CLIENT_FUNC_CLOSE)
1203 actions[num++] = prop_atoms.net_wm_action_close;
1204 if (self->functions & OB_CLIENT_FUNC_MOVE)
1205 actions[num++] = prop_atoms.net_wm_action_move;
1206 if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1207 actions[num++] = prop_atoms.net_wm_action_minimize;
1208 if (self->functions & OB_CLIENT_FUNC_RESIZE)
1209 actions[num++] = prop_atoms.net_wm_action_resize;
1210 if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1211 actions[num++] = prop_atoms.net_wm_action_fullscreen;
1212 if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1213 actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1214 actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1217 PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1219 /* make sure the window isn't breaking any rules now */
1221 if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1222 if (self->frame) client_shade(self, FALSE);
1223 else self->shaded = FALSE;
1225 if (!(self->functions & OB_CLIENT_FUNC_ICONIFY) && self->iconic) {
1226 if (self->frame) client_iconify(self, FALSE, TRUE);
1227 else self->iconic = FALSE;
1229 if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1230 if (self->frame) client_fullscreen(self, FALSE, TRUE);
1231 else self->fullscreen = FALSE;
1233 if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
1235 if (self->frame) client_maximize(self, FALSE, 0, TRUE);
1236 else self->max_vert = self->max_horz = FALSE;
1240 void client_reconfigure(ObClient *self)
1242 /* by making this pass FALSE for user, we avoid the emacs event storm where
1243 every configurenotify causes an update in its normal hints, i think this
1244 is generally what we want anyways... */
1245 client_configure(self, OB_CORNER_TOPLEFT, self->area.x, self->area.y,
1246 self->area.width, self->area.height, FALSE, TRUE);
1249 void client_update_wmhints(ObClient *self)
1252 gboolean ur = FALSE;
1255 /* assume a window takes input if it doesnt specify */
1256 self->can_focus = TRUE;
1258 if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1259 if (hints->flags & InputHint)
1260 self->can_focus = hints->input;
1262 /* only do this when first managing the window *AND* when we aren't
1264 if (ob_state() != OB_STATE_STARTING && self->frame == NULL)
1265 if (hints->flags & StateHint)
1266 self->iconic = hints->initial_state == IconicState;
1268 if (hints->flags & XUrgencyHint)
1271 if (!(hints->flags & WindowGroupHint))
1272 hints->window_group = None;
1274 /* did the group state change? */
1275 if (hints->window_group !=
1276 (self->group ? self->group->leader : None)) {
1277 /* remove from the old group if there was one */
1278 if (self->group != NULL) {
1279 /* remove transients of the group */
1280 for (it = self->group->members; it; it = it->next)
1281 self->transients = g_slist_remove(self->transients,
1283 group_remove(self->group, self);
1286 if (hints->window_group != None) {
1287 self->group = group_add(hints->window_group, self);
1289 /* i can only have transients from the group if i am not
1291 if (!self->transient_for) {
1292 /* add other transients of the group that are already
1294 for (it = self->group->members; it; it = it->next) {
1295 ObClient *c = it->data;
1296 if (c != self && c->transient_for == OB_TRAN_GROUP)
1298 g_slist_append(self->transients, c);
1303 /* because the self->transient flag wont change from this call,
1304 we don't need to update the window's type and such, only its
1305 transient_for, and the transients lists of other windows in
1306 the group may be affected */
1307 client_update_transient_for(self);
1310 /* the WM_HINTS can contain an icon */
1311 client_update_icons(self);
1316 if (ur != self->urgent) {
1318 ob_debug("Urgent Hint for 0x%lx: %s\n", self->window,
1320 /* fire the urgent callback if we're mapped, otherwise, wait until
1321 after we're mapped */
1323 client_urgent_notify(self);
1327 void client_update_title(ObClient *self)
1333 gboolean read_title;
1336 old_title = self->title;
1339 if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1340 /* try old x stuff */
1341 if (!PROP_GETS(self->window, wm_name, locale, &data))
1342 data = g_strdup("Unnamed Window");
1344 /* did the title change? then reset the title_count */
1345 if (old_title && 0 != strncmp(old_title, data, strlen(data)))
1346 self->title_count = 1;
1348 /* look for duplicates and append a number */
1350 for (it = client_list; it; it = it->next)
1351 if (it->data != self) {
1352 ObClient *c = it->data;
1353 if (0 == strncmp(c->title, data, strlen(data)))
1354 nums |= 1 << c->title_count;
1356 /* find first free number */
1357 for (i = 1; i <= 32; ++i)
1358 if (!(nums & (1 << i))) {
1359 if (self->title_count == 1 || i == 1)
1360 self->title_count = i;
1363 /* dont display the number for the first window */
1364 if (self->title_count > 1) {
1366 ndata = g_strdup_printf("%s - [%u]", data, self->title_count);
1371 PROP_SETS(self->window, net_wm_visible_name, data);
1376 frame_adjust_title(self->frame);
1380 /* update the icon title */
1382 g_free(self->icon_title);
1386 if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1387 /* try old x stuff */
1388 if (!PROP_GETS(self->window, wm_icon_name, locale, &data)) {
1389 data = g_strdup(self->title);
1393 /* append the title count, dont display the number for the first window */
1394 if (read_title && self->title_count > 1) {
1395 char *vdata, *ndata;
1396 ndata = g_strdup_printf(" - [%u]", self->title_count);
1397 vdata = g_strconcat(data, ndata, NULL);
1403 PROP_SETS(self->window, net_wm_visible_icon_name, data);
1405 self->icon_title = data;
1408 void client_update_class(ObClient *self)
1413 if (self->name) g_free(self->name);
1414 if (self->class) g_free(self->class);
1415 if (self->role) g_free(self->role);
1417 self->name = self->class = self->role = NULL;
1419 if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1421 self->name = g_strdup(data[0]);
1423 self->class = g_strdup(data[1]);
1428 if (PROP_GETS(self->window, wm_window_role, locale, &s))
1431 if (self->name == NULL) self->name = g_strdup("");
1432 if (self->class == NULL) self->class = g_strdup("");
1433 if (self->role == NULL) self->role = g_strdup("");
1436 void client_update_strut(ObClient *self)
1440 gboolean got = FALSE;
1443 if (PROP_GETA32(self->window, net_wm_strut_partial, cardinal,
1447 STRUT_PARTIAL_SET(strut,
1448 data[0], data[2], data[1], data[3],
1449 data[4], data[5], data[8], data[9],
1450 data[6], data[7], data[10], data[11]);
1456 PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1459 STRUT_PARTIAL_SET(strut,
1460 data[0], data[2], data[1], data[3],
1461 0, 0, 0, 0, 0, 0, 0, 0);
1467 STRUT_PARTIAL_SET(strut, 0, 0, 0, 0,
1468 0, 0, 0, 0, 0, 0, 0, 0);
1470 if (!STRUT_EQUAL(strut, self->strut)) {
1471 self->strut = strut;
1473 /* updating here is pointless while we're being mapped cuz we're not in
1474 the client list yet */
1476 screen_update_areas();
1480 void client_update_icons(ObClient *self)
1486 for (i = 0; i < self->nicons; ++i)
1487 g_free(self->icons[i].data);
1488 if (self->nicons > 0)
1489 g_free(self->icons);
1492 if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1493 /* figure out how many valid icons are in here */
1495 while (num - i > 2) {
1499 if (i > num || w*h == 0) break;
1503 self->icons = g_new(ObClientIcon, self->nicons);
1505 /* store the icons */
1507 for (j = 0; j < self->nicons; ++j) {
1510 w = self->icons[j].width = data[i++];
1511 h = self->icons[j].height = data[i++];
1513 if (w*h == 0) continue;
1515 self->icons[j].data = g_new(RrPixel32, w * h);
1516 for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1521 self->icons[j].data[t] =
1522 (((data[i] >> 24) & 0xff) << RrDefaultAlphaOffset) +
1523 (((data[i] >> 16) & 0xff) << RrDefaultRedOffset) +
1524 (((data[i] >> 8) & 0xff) << RrDefaultGreenOffset) +
1525 (((data[i] >> 0) & 0xff) << RrDefaultBlueOffset);
1531 } else if (PROP_GETA32(self->window, kwm_win_icon,
1532 kwm_win_icon, &data, &num)) {
1535 self->icons = g_new(ObClientIcon, self->nicons);
1536 xerror_set_ignore(TRUE);
1537 if (!RrPixmapToRGBA(ob_rr_inst,
1539 &self->icons[self->nicons-1].width,
1540 &self->icons[self->nicons-1].height,
1541 &self->icons[self->nicons-1].data)) {
1542 g_free(&self->icons[self->nicons-1]);
1545 xerror_set_ignore(FALSE);
1551 if ((hints = XGetWMHints(ob_display, self->window))) {
1552 if (hints->flags & IconPixmapHint) {
1554 self->icons = g_new(ObClientIcon, self->nicons);
1555 xerror_set_ignore(TRUE);
1556 if (!RrPixmapToRGBA(ob_rr_inst,
1558 (hints->flags & IconMaskHint ?
1559 hints->icon_mask : None),
1560 &self->icons[self->nicons-1].width,
1561 &self->icons[self->nicons-1].height,
1562 &self->icons[self->nicons-1].data)){
1563 g_free(&self->icons[self->nicons-1]);
1566 xerror_set_ignore(FALSE);
1572 if (!self->nicons) {
1574 self->icons = g_new(ObClientIcon, self->nicons);
1575 self->icons[self->nicons-1].width = 48;
1576 self->icons[self->nicons-1].height = 48;
1577 self->icons[self->nicons-1].data = g_memdup(ob_rr_theme->def_win_icon,
1583 frame_adjust_icon(self->frame);
1586 static void client_change_state(ObClient *self)
1589 guint32 netstate[11];
1592 state[0] = self->wmstate;
1594 PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1598 netstate[num++] = prop_atoms.net_wm_state_modal;
1600 netstate[num++] = prop_atoms.net_wm_state_shaded;
1602 netstate[num++] = prop_atoms.net_wm_state_hidden;
1603 if (self->skip_taskbar)
1604 netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1605 if (self->skip_pager)
1606 netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1607 if (self->fullscreen)
1608 netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1610 netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1612 netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1614 netstate[num++] = prop_atoms.net_wm_state_above;
1616 netstate[num++] = prop_atoms.net_wm_state_below;
1617 if (self->undecorated)
1618 netstate[num++] = prop_atoms.ob_wm_state_undecorated;
1619 PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1621 client_calc_layer(self);
1624 frame_adjust_state(self->frame);
1627 ObClient *client_search_focus_tree(ObClient *self)
1632 for (it = self->transients; it != NULL; it = it->next) {
1633 if (client_focused(it->data)) return it->data;
1634 if ((ret = client_search_focus_tree(it->data))) return ret;
1639 ObClient *client_search_focus_tree_full(ObClient *self)
1641 if (self->transient_for) {
1642 if (self->transient_for != OB_TRAN_GROUP) {
1643 return client_search_focus_tree_full(self->transient_for);
1646 gboolean recursed = FALSE;
1648 for (it = self->group->members; it; it = it->next)
1649 if (!((ObClient*)it->data)->transient_for) {
1651 if ((c = client_search_focus_tree_full(it->data)))
1660 /* this function checks the whole tree, the client_search_focus_tree~
1661 does not, so we need to check this window */
1662 if (client_focused(self))
1664 return client_search_focus_tree(self);
1667 static ObStackingLayer calc_layer(ObClient *self)
1671 if (self->fullscreen) l = OB_STACKING_LAYER_FULLSCREEN;
1672 else if (self->type == OB_CLIENT_TYPE_DESKTOP)
1673 l = OB_STACKING_LAYER_DESKTOP;
1674 else if (self->type == OB_CLIENT_TYPE_DOCK) {
1675 if (!self->below) l = OB_STACKING_LAYER_TOP;
1676 else l = OB_STACKING_LAYER_NORMAL;
1678 else if (self->above) l = OB_STACKING_LAYER_ABOVE;
1679 else if (self->below) l = OB_STACKING_LAYER_BELOW;
1680 else l = OB_STACKING_LAYER_NORMAL;
1685 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
1686 ObStackingLayer l, gboolean raised)
1688 ObStackingLayer old, own;
1692 own = calc_layer(self);
1693 self->layer = l > own ? l : own;
1695 for (it = self->transients; it; it = it->next)
1696 client_calc_layer_recursive(it->data, orig,
1697 l, raised ? raised : l != old);
1699 if (!raised && l != old)
1700 if (orig->frame) { /* only restack if the original window is managed */
1701 /* XXX add_non_intrusive ever? */
1702 stacking_remove(CLIENT_AS_WINDOW(self));
1703 stacking_add(CLIENT_AS_WINDOW(self));
1707 void client_calc_layer(ObClient *self)
1714 /* transients take on the layer of their parents */
1715 self = client_search_top_transient(self);
1717 l = calc_layer(self);
1719 client_calc_layer_recursive(self, orig, l, FALSE);
1722 gboolean client_should_show(ObClient *self)
1724 if (self->iconic) return FALSE;
1725 else if (!(self->desktop == screen_desktop ||
1726 self->desktop == DESKTOP_ALL)) return FALSE;
1727 else if (client_normal(self) && screen_showing_desktop) return FALSE;
1732 static void client_showhide(ObClient *self)
1735 if (client_should_show(self))
1736 frame_show(self->frame);
1738 frame_hide(self->frame);
1741 gboolean client_normal(ObClient *self) {
1742 return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
1743 self->type == OB_CLIENT_TYPE_DOCK ||
1744 self->type == OB_CLIENT_TYPE_SPLASH);
1747 static void client_apply_startup_state(ObClient *self)
1749 /* these are in a carefully crafted order.. */
1752 self->iconic = FALSE;
1753 client_iconify(self, TRUE, FALSE);
1755 if (self->fullscreen) {
1756 self->fullscreen = FALSE;
1757 client_fullscreen(self, TRUE, FALSE);
1759 if (self->undecorated) {
1760 self->undecorated = FALSE;
1761 client_set_undecorated(self, TRUE);
1764 self->shaded = FALSE;
1765 client_shade(self, TRUE);
1768 client_urgent_notify(self);
1770 if (self->max_vert && self->max_horz) {
1771 self->max_vert = self->max_horz = FALSE;
1772 client_maximize(self, TRUE, 0, FALSE);
1773 } else if (self->max_vert) {
1774 self->max_vert = FALSE;
1775 client_maximize(self, TRUE, 2, FALSE);
1776 } else if (self->max_horz) {
1777 self->max_horz = FALSE;
1778 client_maximize(self, TRUE, 1, FALSE);
1781 /* nothing to do for the other states:
1790 void client_configure_full(ObClient *self, ObCorner anchor,
1791 int x, int y, int w, int h,
1792 gboolean user, gboolean final,
1793 gboolean force_reply)
1796 gboolean send_resize_client;
1797 gboolean moved = FALSE, resized = FALSE;
1798 guint fdecor = self->frame->decorations;
1799 gboolean fhorz = self->frame->max_horz;
1801 /* make the frame recalculate its dimentions n shit without changing
1802 anything visible for real, this way the constraints below can work with
1803 the updated frame dimensions. */
1804 frame_adjust_area(self->frame, TRUE, TRUE, TRUE);
1806 /* gets the frame's position */
1807 frame_client_gravity(self->frame, &x, &y);
1809 /* these positions are frame positions, not client positions */
1811 /* set the size and position if fullscreen */
1812 if (self->fullscreen) {
1815 XF86VidModeModeLine mode;
1820 i = client_monitor(self);
1821 a = screen_physical_area_monitor(i);
1824 if (i == 0 && /* primary head */
1825 extensions_vidmode &&
1826 XF86VidModeGetViewPort(ob_display, ob_screen, &x, &y) &&
1827 /* get the mode last so the mode.privsize isnt freed incorrectly */
1828 XF86VidModeGetModeLine(ob_display, ob_screen, &dot, &mode)) {
1833 if (mode.privsize) XFree(mode.private);
1843 user = FALSE; /* ignore that increment etc shit when in fullscreen */
1847 a = screen_area_monitor(self->desktop, client_monitor(self));
1849 /* set the size and position if maximized */
1850 if (self->max_horz) {
1852 w = a->width - self->frame->size.left - self->frame->size.right;
1854 if (self->max_vert) {
1856 h = a->height - self->frame->size.top - self->frame->size.bottom;
1860 /* gets the client's position */
1861 frame_frame_gravity(self->frame, &x, &y);
1863 /* these override the above states! if you cant move you can't move! */
1865 if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
1869 if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
1870 w = self->area.width;
1871 h = self->area.height;
1875 if (!(w == self->area.width && h == self->area.height)) {
1876 int basew, baseh, minw, minh;
1878 /* base size is substituted with min size if not specified */
1879 if (self->base_size.width || self->base_size.height) {
1880 basew = self->base_size.width;
1881 baseh = self->base_size.height;
1883 basew = self->min_size.width;
1884 baseh = self->min_size.height;
1886 /* min size is substituted with base size if not specified */
1887 if (self->min_size.width || self->min_size.height) {
1888 minw = self->min_size.width;
1889 minh = self->min_size.height;
1891 minw = self->base_size.width;
1892 minh = self->base_size.height;
1895 /* if this is a user-requested resize, then check against min/max
1898 /* smaller than min size or bigger than max size? */
1899 if (w > self->max_size.width) w = self->max_size.width;
1900 if (w < minw) w = minw;
1901 if (h > self->max_size.height) h = self->max_size.height;
1902 if (h < minh) h = minh;
1907 /* keep to the increments */
1908 w /= self->size_inc.width;
1909 h /= self->size_inc.height;
1911 /* you cannot resize to nothing */
1912 if (basew + w < 1) w = 1 - basew;
1913 if (baseh + h < 1) h = 1 - baseh;
1915 /* store the logical size */
1916 SIZE_SET(self->logical_size,
1917 self->size_inc.width > 1 ? w : w + basew,
1918 self->size_inc.height > 1 ? h : h + baseh);
1920 w *= self->size_inc.width;
1921 h *= self->size_inc.height;
1926 /* adjust the height to match the width for the aspect ratios.
1927 for this, min size is not substituted for base size ever. */
1928 w -= self->base_size.width;
1929 h -= self->base_size.height;
1931 if (self->min_ratio)
1932 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1933 if (self->max_ratio)
1934 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1936 w += self->base_size.width;
1937 h += self->base_size.height;
1941 case OB_CORNER_TOPLEFT:
1943 case OB_CORNER_TOPRIGHT:
1944 x -= w - self->area.width;
1946 case OB_CORNER_BOTTOMLEFT:
1947 y -= h - self->area.height;
1949 case OB_CORNER_BOTTOMRIGHT:
1950 x -= w - self->area.width;
1951 y -= h - self->area.height;
1955 moved = x != self->area.x || y != self->area.y;
1956 resized = w != self->area.width || h != self->area.height;
1958 oldw = self->area.width;
1959 oldh = self->area.height;
1960 RECT_SET(self->area, x, y, w, h);
1962 /* for app-requested resizes, always resize if 'resized' is true.
1963 for user-requested ones, only resize if final is true, or when
1964 resizing in redraw mode */
1965 send_resize_client = ((!user && resized) ||
1967 (resized && config_redraw_resize))));
1969 /* if the client is enlarging, the resize the client before the frame */
1970 if (send_resize_client && user && (w > oldw || h > oldh))
1971 XResizeWindow(ob_display, self->window, MAX(w, oldw), MAX(h, oldh));
1973 /* move/resize the frame to match the request */
1975 if (self->decorations != fdecor || self->max_horz != fhorz)
1976 moved = resized = TRUE;
1978 if (moved || resized)
1979 frame_adjust_area(self->frame, moved, resized, FALSE);
1981 if (!resized && (force_reply || ((!user && moved) || (user && final))))
1984 event.type = ConfigureNotify;
1985 event.xconfigure.display = ob_display;
1986 event.xconfigure.event = self->window;
1987 event.xconfigure.window = self->window;
1989 /* root window real coords */
1990 event.xconfigure.x = self->frame->area.x + self->frame->size.left -
1992 event.xconfigure.y = self->frame->area.y + self->frame->size.top -
1994 event.xconfigure.width = w;
1995 event.xconfigure.height = h;
1996 event.xconfigure.border_width = 0;
1997 event.xconfigure.above = self->frame->plate;
1998 event.xconfigure.override_redirect = FALSE;
1999 XSendEvent(event.xconfigure.display, event.xconfigure.window,
2000 FALSE, StructureNotifyMask, &event);
2004 /* if the client is shrinking, then resize the frame before the client */
2005 if (send_resize_client && (!user || (w <= oldw || h <= oldh)))
2006 XResizeWindow(ob_display, self->window, w, h);
2011 void client_fullscreen(ObClient *self, gboolean fs, gboolean savearea)
2015 if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
2016 self->fullscreen == fs) return; /* already done */
2018 self->fullscreen = fs;
2019 client_change_state(self); /* change the state hints on the client,
2020 and adjust out layer/stacking */
2024 self->pre_fullscreen_area = self->area;
2026 /* these are not actually used cuz client_configure will set them
2027 as appropriate when the window is fullscreened */
2032 if (self->pre_fullscreen_area.width > 0 &&
2033 self->pre_fullscreen_area.height > 0)
2035 x = self->pre_fullscreen_area.x;
2036 y = self->pre_fullscreen_area.y;
2037 w = self->pre_fullscreen_area.width;
2038 h = self->pre_fullscreen_area.height;
2039 RECT_SET(self->pre_fullscreen_area, 0, 0, 0, 0);
2041 /* pick some fallbacks... */
2042 a = screen_area_monitor(self->desktop, 0);
2043 x = a->x + a->width / 4;
2044 y = a->y + a->height / 4;
2050 client_setup_decor_and_functions(self);
2052 client_move_resize(self, x, y, w, h);
2054 /* try focus us when we go into fullscreen mode */
2058 static void client_iconify_recursive(ObClient *self,
2059 gboolean iconic, gboolean curdesk)
2062 gboolean changed = FALSE;
2065 if (self->iconic != iconic) {
2066 ob_debug("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"),
2069 self->iconic = iconic;
2072 if (self->functions & OB_CLIENT_FUNC_ICONIFY) {
2075 old = self->wmstate;
2076 self->wmstate = IconicState;
2077 if (old != self->wmstate)
2078 PROP_MSG(self->window, kde_wm_change_state,
2079 self->wmstate, 1, 0, 0);
2081 self->ignore_unmaps++;
2082 /* we unmap the client itself so that we can get MapRequest
2083 events, and because the ICCCM tells us to! */
2084 XUnmapWindow(ob_display, self->window);
2086 /* update the focus lists.. iconic windows go to the bottom of
2087 the list, put the new iconic window at the 'top of the
2089 focus_order_to_top(self);
2097 client_set_desktop(self, screen_desktop, FALSE);
2099 old = self->wmstate;
2100 self->wmstate = self->shaded ? IconicState : NormalState;
2101 if (old != self->wmstate)
2102 PROP_MSG(self->window, kde_wm_change_state,
2103 self->wmstate, 1, 0, 0);
2105 XMapWindow(ob_display, self->window);
2107 /* this puts it after the current focused window */
2108 focus_order_remove(self);
2109 focus_order_add_new(self);
2111 /* this is here cuz with the VIDMODE extension, the viewport can
2112 change while a fullscreen window is iconic, and when it
2113 uniconifies, it would be nice if it did so to the new position
2115 client_reconfigure(self);
2122 client_change_state(self);
2123 client_showhide(self);
2124 screen_update_areas();
2127 /* iconify all transients */
2128 for (it = self->transients; it != NULL; it = it->next)
2129 if (it->data != self) client_iconify_recursive(it->data,
2133 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk)
2135 /* move up the transient chain as far as possible first */
2136 self = client_search_top_transient(self);
2138 client_iconify_recursive(client_search_top_transient(self),
2142 void client_maximize(ObClient *self, gboolean max, int dir, gboolean savearea)
2146 g_assert(dir == 0 || dir == 1 || dir == 2);
2147 if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE)) return; /* can't */
2149 /* check if already done */
2151 if (dir == 0 && self->max_horz && self->max_vert) return;
2152 if (dir == 1 && self->max_horz) return;
2153 if (dir == 2 && self->max_vert) return;
2155 if (dir == 0 && !self->max_horz && !self->max_vert) return;
2156 if (dir == 1 && !self->max_horz) return;
2157 if (dir == 2 && !self->max_vert) return;
2160 /* we just tell it to configure in the same place and client_configure
2161 worries about filling the screen with the window */
2164 w = self->area.width;
2165 h = self->area.height;
2169 self->pre_max_area = self->area;
2173 a = screen_area_monitor(self->desktop, 0);
2174 if ((dir == 0 || dir == 1) && self->max_horz) { /* horz */
2175 if (self->pre_max_area.width > 0) {
2176 x = self->pre_max_area.x;
2177 w = self->pre_max_area.width;
2179 RECT_SET(self->pre_max_area, 0, self->pre_max_area.y,
2180 0, self->pre_max_area.height);
2182 /* pick some fallbacks... */
2183 x = a->x + a->width / 4;
2187 if ((dir == 0 || dir == 2) && self->max_vert) { /* vert */
2188 if (self->pre_max_area.width > 0) {
2189 y = self->pre_max_area.y;
2190 h = self->pre_max_area.height;
2192 RECT_SET(self->pre_max_area, self->pre_max_area.x, 0,
2193 self->pre_max_area.width, 0);
2195 /* pick some fallbacks... */
2196 y = a->y + a->height / 4;
2202 if (dir == 0 || dir == 1) /* horz */
2203 self->max_horz = max;
2204 if (dir == 0 || dir == 2) /* vert */
2205 self->max_vert = max;
2207 client_change_state(self); /* change the state hints on the client */
2209 client_setup_decor_and_functions(self);
2211 client_move_resize(self, x, y, w, h);
2214 void client_shade(ObClient *self, gboolean shade)
2216 if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
2217 shade) || /* can't shade */
2218 self->shaded == shade) return; /* already done */
2220 /* when we're iconic, don't change the wmstate */
2221 if (!self->iconic) {
2224 old = self->wmstate;
2225 self->wmstate = shade ? IconicState : NormalState;
2226 if (old != self->wmstate)
2227 PROP_MSG(self->window, kde_wm_change_state,
2228 self->wmstate, 1, 0, 0);
2231 self->shaded = shade;
2232 client_change_state(self);
2233 /* resize the frame to just the titlebar */
2234 frame_adjust_area(self->frame, FALSE, FALSE, FALSE);
2237 void client_close(ObClient *self)
2241 if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
2244 XXX: itd be cool to do timeouts and shit here for killing the client's
2246 like... if the window is around after 5 seconds, then the close button
2247 turns a nice red, and if this function is called again, the client is
2251 ce.xclient.type = ClientMessage;
2252 ce.xclient.message_type = prop_atoms.wm_protocols;
2253 ce.xclient.display = ob_display;
2254 ce.xclient.window = self->window;
2255 ce.xclient.format = 32;
2256 ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
2257 ce.xclient.data.l[1] = event_lasttime;
2258 ce.xclient.data.l[2] = 0l;
2259 ce.xclient.data.l[3] = 0l;
2260 ce.xclient.data.l[4] = 0l;
2261 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2264 void client_kill(ObClient *self)
2266 XKillClient(ob_display, self->window);
2269 void client_set_desktop_recursive(ObClient *self,
2270 guint target, gboolean donthide)
2275 if (target != self->desktop) {
2277 ob_debug("Setting desktop %u\n", target+1);
2279 g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2281 /* remove from the old desktop(s) */
2282 focus_order_remove(self);
2284 old = self->desktop;
2285 self->desktop = target;
2286 PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2287 /* the frame can display the current desktop state */
2288 frame_adjust_state(self->frame);
2289 /* 'move' the window to the new desktop */
2291 client_showhide(self);
2292 /* raise if it was not already on the desktop */
2293 if (old != DESKTOP_ALL)
2294 stacking_raise(CLIENT_AS_WINDOW(self));
2295 screen_update_areas();
2297 /* add to the new desktop(s) */
2298 if (config_focus_new)
2299 focus_order_to_top(self);
2301 focus_order_to_bottom(self);
2304 /* move all transients */
2305 for (it = self->transients; it != NULL; it = it->next)
2306 if (it->data != self) client_set_desktop_recursive(it->data,
2310 void client_set_desktop(ObClient *self, guint target, gboolean donthide)
2312 client_set_desktop_recursive(client_search_top_transient(self),
2316 ObClient *client_search_modal_child(ObClient *self)
2321 for (it = self->transients; it != NULL; it = it->next) {
2322 ObClient *c = it->data;
2323 if ((ret = client_search_modal_child(c))) return ret;
2324 if (c->modal) return c;
2329 gboolean client_validate(ObClient *self)
2333 XSync(ob_display, FALSE); /* get all events on the server */
2335 if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2336 XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2337 XPutBackEvent(ob_display, &e);
2344 void client_set_wm_state(ObClient *self, long state)
2346 if (state == self->wmstate) return; /* no change */
2350 client_iconify(self, TRUE, TRUE);
2353 client_iconify(self, FALSE, TRUE);
2358 void client_set_state(ObClient *self, Atom action, long data1, long data2)
2360 gboolean shaded = self->shaded;
2361 gboolean fullscreen = self->fullscreen;
2362 gboolean undecorated = self->undecorated;
2363 gboolean max_horz = self->max_horz;
2364 gboolean max_vert = self->max_vert;
2367 if (!(action == prop_atoms.net_wm_state_add ||
2368 action == prop_atoms.net_wm_state_remove ||
2369 action == prop_atoms.net_wm_state_toggle))
2370 /* an invalid action was passed to the client message, ignore it */
2373 for (i = 0; i < 2; ++i) {
2374 Atom state = i == 0 ? data1 : data2;
2376 if (!state) continue;
2378 /* if toggling, then pick whether we're adding or removing */
2379 if (action == prop_atoms.net_wm_state_toggle) {
2380 if (state == prop_atoms.net_wm_state_modal)
2381 action = self->modal ? prop_atoms.net_wm_state_remove :
2382 prop_atoms.net_wm_state_add;
2383 else if (state == prop_atoms.net_wm_state_maximized_vert)
2384 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2385 prop_atoms.net_wm_state_add;
2386 else if (state == prop_atoms.net_wm_state_maximized_horz)
2387 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2388 prop_atoms.net_wm_state_add;
2389 else if (state == prop_atoms.net_wm_state_shaded)
2390 action = shaded ? prop_atoms.net_wm_state_remove :
2391 prop_atoms.net_wm_state_add;
2392 else if (state == prop_atoms.net_wm_state_skip_taskbar)
2393 action = self->skip_taskbar ?
2394 prop_atoms.net_wm_state_remove :
2395 prop_atoms.net_wm_state_add;
2396 else if (state == prop_atoms.net_wm_state_skip_pager)
2397 action = self->skip_pager ?
2398 prop_atoms.net_wm_state_remove :
2399 prop_atoms.net_wm_state_add;
2400 else if (state == prop_atoms.net_wm_state_fullscreen)
2401 action = fullscreen ?
2402 prop_atoms.net_wm_state_remove :
2403 prop_atoms.net_wm_state_add;
2404 else if (state == prop_atoms.net_wm_state_above)
2405 action = self->above ? prop_atoms.net_wm_state_remove :
2406 prop_atoms.net_wm_state_add;
2407 else if (state == prop_atoms.net_wm_state_below)
2408 action = self->below ? prop_atoms.net_wm_state_remove :
2409 prop_atoms.net_wm_state_add;
2410 else if (state == prop_atoms.ob_wm_state_undecorated)
2411 action = undecorated ? prop_atoms.net_wm_state_remove :
2412 prop_atoms.net_wm_state_add;
2415 if (action == prop_atoms.net_wm_state_add) {
2416 if (state == prop_atoms.net_wm_state_modal) {
2417 /* XXX raise here or something? */
2419 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2421 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2423 } else if (state == prop_atoms.net_wm_state_shaded) {
2425 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2426 self->skip_taskbar = TRUE;
2427 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2428 self->skip_pager = TRUE;
2429 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2431 } else if (state == prop_atoms.net_wm_state_above) {
2433 } else if (state == prop_atoms.net_wm_state_below) {
2435 } else if (state == prop_atoms.ob_wm_state_undecorated) {
2439 } else { /* action == prop_atoms.net_wm_state_remove */
2440 if (state == prop_atoms.net_wm_state_modal) {
2441 self->modal = FALSE;
2442 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2444 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2446 } else if (state == prop_atoms.net_wm_state_shaded) {
2448 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2449 self->skip_taskbar = FALSE;
2450 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2451 self->skip_pager = FALSE;
2452 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2454 } else if (state == prop_atoms.net_wm_state_above) {
2455 self->above = FALSE;
2456 } else if (state == prop_atoms.net_wm_state_below) {
2457 self->below = FALSE;
2458 } else if (state == prop_atoms.ob_wm_state_undecorated) {
2459 undecorated = FALSE;
2463 if (max_horz != self->max_horz || max_vert != self->max_vert) {
2464 if (max_horz != self->max_horz && max_vert != self->max_vert) {
2466 if (max_horz == max_vert) { /* both going the same way */
2467 client_maximize(self, max_horz, 0, TRUE);
2469 client_maximize(self, max_horz, 1, TRUE);
2470 client_maximize(self, max_vert, 2, TRUE);
2474 if (max_horz != self->max_horz)
2475 client_maximize(self, max_horz, 1, TRUE);
2477 client_maximize(self, max_vert, 2, TRUE);
2480 /* change fullscreen state before shading, as it will affect if the window
2482 if (fullscreen != self->fullscreen)
2483 client_fullscreen(self, fullscreen, TRUE);
2484 if (shaded != self->shaded)
2485 client_shade(self, shaded);
2486 if (undecorated != self->undecorated)
2487 client_set_undecorated(self, undecorated);
2488 client_calc_layer(self);
2489 client_change_state(self); /* change the hint to reflect these changes */
2492 ObClient *client_focus_target(ObClient *self)
2496 /* if we have a modal child, then focus it, not us */
2497 child = client_search_modal_child(self);
2498 if (child) return child;
2502 gboolean client_can_focus(ObClient *self)
2506 /* choose the correct target */
2507 self = client_focus_target(self);
2509 if (!self->frame->visible)
2512 if (!((self->can_focus || self->focus_notify) &&
2513 (self->desktop == screen_desktop ||
2514 self->desktop == DESKTOP_ALL) &&
2518 /* do a check to see if the window has already been unmapped or destroyed
2519 do this intelligently while watching out for unmaps we've generated
2520 (ignore_unmaps > 0) */
2521 if (XCheckTypedWindowEvent(ob_display, self->window,
2522 DestroyNotify, &ev)) {
2523 XPutBackEvent(ob_display, &ev);
2526 while (XCheckTypedWindowEvent(ob_display, self->window,
2527 UnmapNotify, &ev)) {
2528 if (self->ignore_unmaps) {
2529 self->ignore_unmaps--;
2531 XPutBackEvent(ob_display, &ev);
2539 gboolean client_focus(ObClient *self)
2541 /* choose the correct target */
2542 self = client_focus_target(self);
2544 if (!client_can_focus(self)) {
2545 if (!self->frame->visible) {
2546 /* update the focus lists */
2547 focus_order_to_top(self);
2552 if (self->can_focus) {
2553 /* RevertToPointerRoot causes much more headache than RevertToNone, so
2554 I choose to use it always, hopefully to find errors quicker, if any
2555 are left. (I hate X. I hate focus events.)
2557 Update: Changing this to RevertToNone fixed a bug with mozilla (bug
2558 #799. So now it is RevertToNone again.
2560 XSetInputFocus(ob_display, self->window, RevertToNone,
2564 if (self->focus_notify) {
2566 ce.xclient.type = ClientMessage;
2567 ce.xclient.message_type = prop_atoms.wm_protocols;
2568 ce.xclient.display = ob_display;
2569 ce.xclient.window = self->window;
2570 ce.xclient.format = 32;
2571 ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2572 ce.xclient.data.l[1] = event_lasttime;
2573 ce.xclient.data.l[2] = 0l;
2574 ce.xclient.data.l[3] = 0l;
2575 ce.xclient.data.l[4] = 0l;
2576 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2580 ob_debug("%sively focusing %lx at %d\n",
2581 (self->can_focus ? "act" : "pass"),
2582 self->window, (int) event_lasttime);
2585 /* Cause the FocusIn to come back to us. Important for desktop switches,
2586 since otherwise we'll have no FocusIn on the queue and send it off to
2587 the focus_backup. */
2588 XSync(ob_display, FALSE);
2592 void client_unfocus(ObClient *self)
2594 if (focus_client == self) {
2596 ob_debug("client_unfocus for %lx\n", self->window);
2598 focus_fallback(OB_FOCUS_FALLBACK_UNFOCUSING);
2602 void client_activate(ObClient *self, gboolean here)
2604 if (client_normal(self) && screen_showing_desktop)
2605 screen_show_desktop(FALSE);
2607 client_iconify(self, FALSE, FALSE);
2608 if (self->desktop != DESKTOP_ALL &&
2609 self->desktop != screen_desktop) {
2611 client_set_desktop(self, screen_desktop, FALSE);
2613 screen_set_desktop(self->desktop);
2614 } else if (!self->frame->visible)
2615 /* if its not visible for other reasons, then don't mess
2619 client_shade(self, FALSE);
2621 stacking_raise(CLIENT_AS_WINDOW(self));
2624 gboolean client_focused(ObClient *self)
2626 return self == focus_client;
2629 ObClientIcon *client_icon(ObClient *self, int w, int h)
2632 /* si is the smallest image >= req */
2633 /* li is the largest image < req */
2634 unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2636 for (i = 0; i < self->nicons; ++i) {
2637 size = self->icons[i].width * self->icons[i].height;
2638 if (size < smallest && size >= (unsigned)(w * h)) {
2642 if (size > largest && size <= (unsigned)(w * h)) {
2647 if (largest == 0) /* didnt find one smaller than the requested size */
2648 return &self->icons[si];
2649 return &self->icons[li];
2652 /* this be mostly ripped from fvwm */
2653 ObClient *client_find_directional(ObClient *c, ObDirection dir)
2655 int my_cx, my_cy, his_cx, his_cy;
2658 int score, best_score;
2659 ObClient *best_client, *cur;
2665 /* first, find the centre coords of the currently focused window */
2666 my_cx = c->frame->area.x + c->frame->area.width / 2;
2667 my_cy = c->frame->area.y + c->frame->area.height / 2;
2672 for(it = g_list_first(client_list); it; it = it->next) {
2675 /* the currently selected window isn't interesting */
2678 if (!client_normal(cur))
2680 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2684 if(client_focus_target(cur) == cur &&
2685 !(cur->can_focus || cur->focus_notify))
2688 /* find the centre coords of this window, from the
2689 * currently focused window's point of view */
2690 his_cx = (cur->frame->area.x - my_cx)
2691 + cur->frame->area.width / 2;
2692 his_cy = (cur->frame->area.y - my_cy)
2693 + cur->frame->area.height / 2;
2695 if(dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
2696 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST) {
2698 /* Rotate the diagonals 45 degrees counterclockwise.
2699 * To do this, multiply the matrix /+h +h\ with the
2700 * vector (x y). \-h +h/
2701 * h = sqrt(0.5). We can set h := 1 since absolute
2702 * distance doesn't matter here. */
2703 tx = his_cx + his_cy;
2704 his_cy = -his_cx + his_cy;
2709 case OB_DIRECTION_NORTH:
2710 case OB_DIRECTION_SOUTH:
2711 case OB_DIRECTION_NORTHEAST:
2712 case OB_DIRECTION_SOUTHWEST:
2713 offset = (his_cx < 0) ? -his_cx : his_cx;
2714 distance = ((dir == OB_DIRECTION_NORTH ||
2715 dir == OB_DIRECTION_NORTHEAST) ?
2718 case OB_DIRECTION_EAST:
2719 case OB_DIRECTION_WEST:
2720 case OB_DIRECTION_SOUTHEAST:
2721 case OB_DIRECTION_NORTHWEST:
2722 offset = (his_cy < 0) ? -his_cy : his_cy;
2723 distance = ((dir == OB_DIRECTION_WEST ||
2724 dir == OB_DIRECTION_NORTHWEST) ?
2729 /* the target must be in the requested direction */
2733 /* Calculate score for this window. The smaller the better. */
2734 score = distance + offset;
2736 /* windows more than 45 degrees off the direction are
2737 * heavily penalized and will only be chosen if nothing
2738 * else within a million pixels */
2739 if(offset > distance)
2742 if(best_score == -1 || score < best_score)
2750 void client_set_layer(ObClient *self, int layer)
2754 self->above = FALSE;
2755 } else if (layer == 0) {
2756 self->below = self->above = FALSE;
2758 self->below = FALSE;
2761 client_calc_layer(self);
2762 client_change_state(self); /* reflect this in the state hints */
2765 void client_set_undecorated(ObClient *self, gboolean undecorated)
2767 if (self->undecorated != undecorated) {
2768 self->undecorated = undecorated;
2769 client_setup_decor_and_functions(self);
2770 client_change_state(self); /* reflect this in the state hints */
2774 guint client_monitor(ObClient *self)
2778 for (i = 0; i < screen_num_monitors; ++i) {
2779 Rect *area = screen_physical_area_monitor(i);
2780 if (RECT_INTERSECTS_RECT(*area, self->frame->area))
2783 if (i == screen_num_monitors) i = 0;
2784 g_assert(i < screen_num_monitors);
2788 ObClient *client_search_top_transient(ObClient *self)
2790 /* move up the transient chain as far as possible */
2791 if (self->transient_for) {
2792 if (self->transient_for != OB_TRAN_GROUP) {
2793 return client_search_top_transient(self->transient_for);
2797 for (it = self->group->members; it; it = it->next) {
2798 ObClient *c = it->data;
2800 /* checking transient_for prevents infinate loops! */
2801 if (c != self && !c->transient_for)
2812 ObClient *client_search_transient(ObClient *self, ObClient *search)
2816 for (sit = self->transients; sit; sit = g_slist_next(sit)) {
2817 if (sit->data == search)
2819 if (client_search_transient(sit->data, search))
2825 void client_update_sm_client_id(ObClient *self)
2827 g_free(self->sm_client_id);
2828 self->sm_client_id = NULL;
2830 if (!PROP_GETS(self->window, sm_client_id, locale, &self->sm_client_id) &&
2832 PROP_GETS(self->group->leader, sm_client_id, locale,
2833 &self->sm_client_id);
2836 /* finds the nearest edge in the given direction from the current client
2837 * note to self: the edge is the -frame- edge (the actual one), not the
2840 int client_directional_edge_search(ObClient *c, ObDirection dir)
2843 int my_edge_start, my_edge_end, my_offset;
2850 a = screen_area(c->desktop);
2853 case OB_DIRECTION_NORTH:
2854 my_edge_start = c->frame->area.x;
2855 my_edge_end = c->frame->area.x + c->frame->area.width;
2856 my_offset = c->frame->area.y;
2858 dest = a->y; /* default: top of screen */
2860 for(it = g_list_first(client_list); it; it = it->next) {
2861 int his_edge_start, his_edge_end, his_offset;
2862 ObClient *cur = it->data;
2866 if(!client_normal(cur))
2868 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2873 his_edge_start = cur->frame->area.x;
2874 his_edge_end = cur->frame->area.x + cur->frame->area.width;
2875 his_offset = cur->frame->area.y + cur->frame->area.height;
2877 if(his_offset + 1 > my_offset)
2880 if(his_offset < dest)
2883 if(his_edge_start >= my_edge_start &&
2884 his_edge_start <= my_edge_end)
2887 if(my_edge_start >= his_edge_start &&
2888 my_edge_start <= his_edge_end)
2893 case OB_DIRECTION_SOUTH:
2894 my_edge_start = c->frame->area.x;
2895 my_edge_end = c->frame->area.x + c->frame->area.width;
2896 my_offset = c->frame->area.y + c->frame->area.height;
2898 dest = a->y + a->height; /* default: bottom of screen */
2900 for(it = g_list_first(client_list); it; it = it->next) {
2901 int his_edge_start, his_edge_end, his_offset;
2902 ObClient *cur = it->data;
2906 if(!client_normal(cur))
2908 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2913 his_edge_start = cur->frame->area.x;
2914 his_edge_end = cur->frame->area.x + cur->frame->area.width;
2915 his_offset = cur->frame->area.y;
2918 if(his_offset - 1 < my_offset)
2921 if(his_offset > dest)
2924 if(his_edge_start >= my_edge_start &&
2925 his_edge_start <= my_edge_end)
2928 if(my_edge_start >= his_edge_start &&
2929 my_edge_start <= his_edge_end)
2934 case OB_DIRECTION_WEST:
2935 my_edge_start = c->frame->area.y;
2936 my_edge_end = c->frame->area.y + c->frame->area.height;
2937 my_offset = c->frame->area.x;
2939 dest = a->x; /* default: leftmost egde of screen */
2941 for(it = g_list_first(client_list); it; it = it->next) {
2942 int his_edge_start, his_edge_end, his_offset;
2943 ObClient *cur = it->data;
2947 if(!client_normal(cur))
2949 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2954 his_edge_start = cur->frame->area.y;
2955 his_edge_end = cur->frame->area.y + cur->frame->area.height;
2956 his_offset = cur->frame->area.x + cur->frame->area.width;
2958 if(his_offset + 1 > my_offset)
2961 if(his_offset < dest)
2964 if(his_edge_start >= my_edge_start &&
2965 his_edge_start <= my_edge_end)
2968 if(my_edge_start >= his_edge_start &&
2969 my_edge_start <= his_edge_end)
2975 case OB_DIRECTION_EAST:
2976 my_edge_start = c->frame->area.y;
2977 my_edge_end = c->frame->area.y + c->frame->area.height;
2978 my_offset = c->frame->area.x + c->frame->area.width;
2980 dest = a->x + a->width; /* default: rightmost edge of screen */
2982 for(it = g_list_first(client_list); it; it = it->next) {
2983 int his_edge_start, his_edge_end, his_offset;
2984 ObClient *cur = it->data;
2988 if(!client_normal(cur))
2990 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2995 his_edge_start = cur->frame->area.y;
2996 his_edge_end = cur->frame->area.y + cur->frame->area.height;
2997 his_offset = cur->frame->area.x;
2999 if(his_offset - 1 < my_offset)
3002 if(his_offset > dest)
3005 if(his_edge_start >= my_edge_start &&
3006 his_edge_start <= my_edge_end)
3009 if(my_edge_start >= his_edge_start &&
3010 my_edge_start <= his_edge_end)
3015 case OB_DIRECTION_NORTHEAST:
3016 case OB_DIRECTION_SOUTHEAST:
3017 case OB_DIRECTION_NORTHWEST:
3018 case OB_DIRECTION_SOUTHWEST:
3019 /* not implemented */
3021 g_assert_not_reached();
3026 ObClient* client_under_pointer()
3030 ObClient *ret = NULL;
3032 if (screen_pointer_pos(&x, &y)) {
3033 for (it = stacking_list; it != NULL; it = it->next) {
3034 if (WINDOW_IS_CLIENT(it->data)) {
3035 ObClient *c = WINDOW_AS_CLIENT(it->data);
3036 if (c->desktop == screen_desktop &&
3037 RECT_CONTAINS(c->frame->area, x, y)) {