3 #include "startupnotify.h"
7 #include "moveresize.h"
10 #include "extensions.h"
20 #include "menuframe.h"
23 #include "render/render.h"
26 #include <X11/Xutil.h>
28 /*! The event mask to grab on client windows */
29 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
32 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
35 GList *client_list = NULL;
36 GSList *client_destructors = NULL;
38 static void client_get_all(ObClient *self);
39 static void client_toggle_border(ObClient *self, gboolean show);
40 static void client_get_startup_id(ObClient *self);
41 static void client_get_area(ObClient *self);
42 static void client_get_desktop(ObClient *self);
43 static void client_get_state(ObClient *self);
44 static void client_get_shaped(ObClient *self);
45 static void client_get_mwm_hints(ObClient *self);
46 static void client_get_gravity(ObClient *self);
47 static void client_showhide(ObClient *self);
48 static void client_change_allowed_actions(ObClient *self);
49 static void client_change_state(ObClient *self);
50 static void client_apply_startup_state(ObClient *self);
51 static void client_restore_session_state(ObClient *self);
52 static void client_restore_session_stacking(ObClient *self);
53 static void client_urgent_notify(ObClient *self);
55 void client_startup(gboolean reconfig)
62 void client_shutdown(gboolean reconfig)
66 void client_add_destructor(GDestroyNotify func)
68 client_destructors = g_slist_prepend(client_destructors, (gpointer)func);
71 void client_remove_destructor(GDestroyNotify func)
73 client_destructors = g_slist_remove(client_destructors, (gpointer)func);
76 void client_set_list()
78 Window *windows, *win_it;
80 guint size = g_list_length(client_list);
82 /* create an array of the window ids */
84 windows = g_new(Window, size);
86 for (it = client_list; it != NULL; it = it->next, ++win_it)
87 *win_it = ((ObClient*)it->data)->window;
91 PROP_SETA32(RootWindow(ob_display, ob_screen),
92 net_client_list, window, (guint32*)windows, size);
101 void client_foreach_transient(ObClient *self, ObClientForeachFunc func, void *data)
105 for (it = self->transients; it; it = it->next) {
106 if (!func(it->data, data)) return;
107 client_foreach_transient(it->data, func, data);
111 void client_foreach_ancestor(ObClient *self, ObClientForeachFunc func, void *data)
113 if (self->transient_for) {
114 if (self->transient_for != OB_TRAN_GROUP) {
115 if (!func(self->transient_for, data)) return;
116 client_foreach_ancestor(self->transient_for, func, data);
120 for (it = self->group->members; it; it = it->next)
121 if (it->data != self &&
122 !((ObClient*)it->data)->transient_for) {
123 if (!func(it->data, data)) return;
124 client_foreach_ancestor(it->data, func, data);
131 void client_manage_all()
133 unsigned int i, j, nchild;
136 XWindowAttributes attrib;
138 XQueryTree(ob_display, RootWindow(ob_display, ob_screen),
139 &w, &w, &children, &nchild);
141 /* remove all icon windows from the list */
142 for (i = 0; i < nchild; i++) {
143 if (children[i] == None) continue;
144 wmhints = XGetWMHints(ob_display, children[i]);
146 if ((wmhints->flags & IconWindowHint) &&
147 (wmhints->icon_window != children[i]))
148 for (j = 0; j < nchild; j++)
149 if (children[j] == wmhints->icon_window) {
157 for (i = 0; i < nchild; ++i) {
158 if (children[i] == None)
160 if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
161 if (attrib.override_redirect) continue;
163 if (attrib.map_state != IsUnmapped)
164 client_manage(children[i]);
169 if (config_focus_new)
170 focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS);
173 void client_manage(Window window)
177 XWindowAttributes attrib;
178 XSetWindowAttributes attrib_set;
180 gboolean activate = FALSE;
184 /* check if it has already been unmapped by the time we started mapping
185 the grab does a sync so we don't have to here */
186 if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
187 XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
188 XPutBackEvent(ob_display, &e);
191 return; /* don't manage it */
194 /* make sure it isn't an override-redirect window */
195 if (!XGetWindowAttributes(ob_display, window, &attrib) ||
196 attrib.override_redirect) {
198 return; /* don't manage it */
201 /* is the window a docking app */
202 if ((wmhint = XGetWMHints(ob_display, window))) {
203 if ((wmhint->flags & StateHint) &&
204 wmhint->initial_state == WithdrawnState) {
205 dock_add(window, wmhint);
213 ob_debug("Managing window: %lx\n", window);
215 /* choose the events we want to receive on the CLIENT window */
216 attrib_set.event_mask = CLIENT_EVENTMASK;
217 attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
218 XChangeWindowAttributes(ob_display, window,
219 CWEventMask|CWDontPropagate, &attrib_set);
222 /* create the ObClient struct, and populate it from the hints on the
224 self = g_new0(ObClient, 1);
225 self->obwin.type = Window_Client;
226 self->window = window;
228 /* non-zero defaults */
229 self->title_count = 1;
230 self->wmstate = NormalState;
232 self->decorate = TRUE;
233 self->desktop = screen_num_desktops; /* always an invalid value */
235 client_get_all(self);
236 client_restore_session_state(self);
238 sn_app_started(self->class);
240 client_change_state(self);
242 /* remove the client's border (and adjust re gravity) */
243 client_toggle_border(self, FALSE);
245 /* specify that if we exit, the window should not be destroyed and should
246 be reparented back to root automatically */
247 XChangeSaveSet(ob_display, window, SetModeInsert);
249 /* create the decoration frame for the client window */
250 self->frame = frame_new();
252 frame_grab_client(self->frame, self);
256 client_apply_startup_state(self);
258 /* update the focus lists */
259 focus_order_add_new(self);
261 stacking_add(CLIENT_AS_WINDOW(self));
262 client_restore_session_stacking(self);
264 /* focus the new window? */
265 if (ob_state() != OB_STATE_STARTING && config_focus_new &&
266 /* note the check against Type_Normal/Dialog, not client_normal(self),
267 which would also include other types. in this case we want more
268 strict rules for focus */
269 (self->type == OB_CLIENT_TYPE_NORMAL ||
270 self->type == OB_CLIENT_TYPE_DIALOG))
274 if (self->desktop != screen_desktop) {
275 /* activate the window */
278 gboolean group_foc = FALSE;
283 for (it = self->group->members; it; it = it->next)
285 if (client_focused(it->data))
293 (!self->transient_for && (!self->group ||
294 !self->group->members->next))) ||
295 client_search_focus_tree_full(self) ||
297 !client_normal(focus_client))
299 /* activate the window */
306 if (ob_state() == OB_STATE_RUNNING) {
307 int x = self->area.x, ox = x;
308 int y = self->area.y, oy = y;
310 place_client(self, &x, &y);
312 /* make sure the window is visible */
313 client_find_onscreen(self, &x, &y,
314 self->frame->area.width,
315 self->frame->area.height,
316 client_normal(self));
318 if (x != ox || y != oy)
319 client_move(self, x, y);
322 client_showhide(self);
324 /* use client_focus instead of client_activate cuz client_activate does
325 stuff like switch desktops etc and I'm not interested in all that when
326 a window maps since its not based on an action from the user like
327 clicking a window to activate is. so keep the new window out of the way
329 if (activate) client_focus(self);
331 /* client_activate does this but we aret using it so we have to do it
333 if (screen_showing_desktop)
334 screen_show_desktop(FALSE);
336 /* add to client list/map */
337 client_list = g_list_append(client_list, self);
338 g_hash_table_insert(window_map, &self->window, self);
340 /* this has to happen after we're in the client_list */
341 screen_update_areas();
343 /* update the list hints */
346 keyboard_grab_for_client(self, TRUE);
347 mouse_grab_for_client(self, TRUE);
349 ob_debug("Managed window 0x%lx (%s)\n", window, self->class);
352 void client_unmanage_all()
354 while (client_list != NULL)
355 client_unmanage(client_list->data);
358 void client_unmanage(ObClient *self)
363 ob_debug("Unmanaging window: %lx (%s)\n", self->window, self->class);
365 g_assert(self != NULL);
367 keyboard_grab_for_client(self, FALSE);
368 mouse_grab_for_client(self, FALSE);
370 /* remove the window from our save set */
371 XChangeSaveSet(ob_display, self->window, SetModeDelete);
373 /* we dont want events no more */
374 XSelectInput(ob_display, self->window, NoEventMask);
376 frame_hide(self->frame);
378 client_list = g_list_remove(client_list, self);
379 stacking_remove(self);
380 g_hash_table_remove(window_map, &self->window);
382 /* update the focus lists */
383 focus_order_remove(self);
385 /* once the client is out of the list, update the struts to remove it's
387 screen_update_areas();
389 /* tell our parent(s) that we're gone */
390 if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
393 for (it = self->group->members; it; it = it->next)
394 if (it->data != self)
395 ((ObClient*)it->data)->transients =
396 g_slist_remove(((ObClient*)it->data)->transients, self);
397 } else if (self->transient_for) { /* transient of window */
398 self->transient_for->transients =
399 g_slist_remove(self->transient_for->transients, self);
402 /* tell our transients that we're gone */
403 for (it = self->transients; it != NULL; it = it->next) {
404 if (((ObClient*)it->data)->transient_for != OB_TRAN_GROUP) {
405 ((ObClient*)it->data)->transient_for = NULL;
406 client_calc_layer(it->data);
410 for (it = client_destructors; it; it = g_slist_next(it)) {
411 GDestroyNotify func = (GDestroyNotify) it->data;
415 if (focus_client == self) {
418 /* focus the last focused window on the desktop, and ignore enter
419 events from the unmap so it doesnt mess with the focus */
420 while (XCheckTypedEvent(ob_display, EnterNotify, &e));
421 client_unfocus(self);
424 /* remove from its group */
426 group_remove(self->group, self);
430 /* give the client its border back */
431 client_toggle_border(self, TRUE);
433 /* reparent the window out of the frame, and free the frame */
434 frame_release_client(self->frame, self);
437 if (ob_state() != OB_STATE_EXITING) {
438 /* these values should not be persisted across a window
440 PROP_ERASE(self->window, net_wm_desktop);
441 PROP_ERASE(self->window, net_wm_state);
442 PROP_ERASE(self->window, wm_state);
444 /* if we're left in an iconic state, the client wont be mapped. this is
445 bad, since we will no longer be managing the window on restart */
447 XMapWindow(ob_display, self->window);
451 ob_debug("Unmanaged window 0x%lx\n", self->window);
453 /* free all data allocated in the client struct */
454 g_slist_free(self->transients);
455 for (j = 0; j < self->nicons; ++j)
456 g_free(self->icons[j].data);
457 if (self->nicons > 0)
460 g_free(self->icon_title);
466 /* update the list hints */
470 static void client_urgent_notify(ObClient *self)
473 frame_flash_start(self->frame);
475 frame_flash_stop(self->frame);
478 static void client_restore_session_state(ObClient *self)
482 if (!(it = session_state_find(self)))
485 self->session = it->data;
487 RECT_SET(self->area, self->session->x, self->session->y,
488 self->session->w, self->session->h);
489 self->positioned = TRUE;
490 XResizeWindow(ob_display, self->window,
491 self->session->w, self->session->h);
493 self->desktop = (self->session->desktop == DESKTOP_ALL ?
494 self->session->desktop :
495 MIN(screen_num_desktops - 1, self->session->desktop));
496 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
498 self->shaded = self->session->shaded;
499 self->iconic = self->session->iconic;
500 self->skip_pager = self->session->skip_pager;
501 self->skip_taskbar = self->session->skip_taskbar;
502 self->fullscreen = self->session->fullscreen;
503 self->above = self->session->above;
504 self->below = self->session->below;
505 self->max_horz = self->session->max_horz;
506 self->max_vert = self->session->max_vert;
509 static void client_restore_session_stacking(ObClient *self)
513 if (!self->session) return;
515 it = g_list_find(session_saved_state, self->session);
516 for (it = g_list_previous(it); it; it = g_list_previous(it)) {
519 for (cit = client_list; cit; cit = g_list_next(cit))
520 if (session_state_cmp(it->data, cit->data))
523 client_calc_layer(self);
524 stacking_below(CLIENT_AS_WINDOW(self),
525 CLIENT_AS_WINDOW(cit->data));
531 void client_move_onscreen(ObClient *self, gboolean rude)
533 int x = self->area.x;
534 int y = self->area.y;
535 if (client_find_onscreen(self, &x, &y,
536 self->frame->area.width,
537 self->frame->area.height, rude)) {
538 client_move(self, x, y);
542 gboolean client_find_onscreen(ObClient *self, int *x, int *y, int w, int h,
546 int ox = *x, oy = *y;
548 frame_client_gravity(self->frame, x, y); /* get where the frame
551 /* XXX watch for xinerama dead areas */
553 a = screen_area(self->desktop);
554 if (!self->strut.right && *x >= a->x + a->width - 1)
555 *x = a->x + a->width - self->frame->area.width;
556 if (!self->strut.bottom && *y >= a->y + a->height - 1)
557 *y = a->y + a->height - self->frame->area.height;
558 if (!self->strut.left && *x + self->frame->area.width - 1 < a->x)
560 if (!self->strut.top && *y + self->frame->area.height - 1 < a->y)
564 /* this is my MOZILLA BITCHSLAP. oh ya it fucking feels good.
565 Java can suck it too. */
567 /* dont let windows map/move into the strut unless they
568 are bigger than the available area */
570 if (!self->strut.left && *x < a->x) *x = a->x;
571 if (!self->strut.right && *x + w > a->x + a->width)
572 *x = a->x + a->width - w;
574 if (h <= a->height) {
575 if (!self->strut.top && *y < a->y) *y = a->y;
576 if (!self->strut.bottom && *y + h > a->y + a->height)
577 *y = a->y + a->height - h;
581 frame_frame_gravity(self->frame, x, y); /* get where the client
584 return ox != *x || oy != *y;
587 static void client_toggle_border(ObClient *self, gboolean show)
589 /* adjust our idea of where the client is, based on its border. When the
590 border is removed, the client should now be considered to be in a
592 when re-adding the border to the client, the same operation needs to be
594 int oldx = self->area.x, oldy = self->area.y;
595 int x = oldx, y = oldy;
596 switch(self->gravity) {
598 case NorthWestGravity:
600 case SouthWestGravity:
602 case NorthEastGravity:
604 case SouthEastGravity:
605 if (show) x -= self->border_width * 2;
606 else x += self->border_width * 2;
613 if (show) x -= self->border_width;
614 else x += self->border_width;
617 switch(self->gravity) {
619 case NorthWestGravity:
621 case NorthEastGravity:
623 case SouthWestGravity:
625 case SouthEastGravity:
626 if (show) y -= self->border_width * 2;
627 else y += self->border_width * 2;
634 if (show) y -= self->border_width;
635 else y += self->border_width;
642 XSetWindowBorderWidth(ob_display, self->window, self->border_width);
644 /* move the client so it is back it the right spot _with_ its
646 if (x != oldx || y != oldy)
647 XMoveWindow(ob_display, self->window, x, y);
649 XSetWindowBorderWidth(ob_display, self->window, 0);
653 static void client_get_all(ObClient *self)
655 client_get_area(self);
656 client_update_transient_for(self);
657 client_update_wmhints(self);
658 client_get_startup_id(self);
659 client_get_desktop(self);
660 client_get_state(self);
661 client_get_shaped(self);
663 g_message("self->iconic %d", self->iconic);
665 client_get_mwm_hints(self);
666 client_get_type(self);/* this can change the mwmhints for special cases */
668 client_update_protocols(self);
670 client_get_gravity(self); /* get the attribute gravity */
671 client_update_normal_hints(self); /* this may override the attribute
674 /* got the type, the mwmhints, the protocols, and the normal hints
675 (min/max sizes), so we're ready to set up the decorations/functions */
676 client_setup_decor_and_functions(self);
678 client_update_title(self);
679 client_update_class(self);
680 client_update_strut(self);
681 client_update_icons(self);
684 static void client_get_startup_id(ObClient *self)
686 if (!(PROP_GETS(self->window, net_startup_id, utf8, &self->startup_id)))
688 PROP_GETS(self->group->leader,
689 net_startup_id, utf8, &self->startup_id);
692 static void client_get_area(ObClient *self)
694 XWindowAttributes wattrib;
697 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
698 g_assert(ret != BadWindow);
700 RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
701 self->border_width = wattrib.border_width;
704 static void client_get_desktop(ObClient *self)
706 guint32 d = screen_num_desktops; /* an always-invalid value */
708 if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
709 if (d >= screen_num_desktops && d != DESKTOP_ALL)
710 self->desktop = screen_num_desktops - 1;
714 gboolean trdesk = FALSE;
716 if (self->transient_for) {
717 if (self->transient_for != OB_TRAN_GROUP) {
718 self->desktop = self->transient_for->desktop;
723 for (it = self->group->members; it; it = it->next)
724 if (it->data != self &&
725 !((ObClient*)it->data)->transient_for) {
726 self->desktop = ((ObClient*)it->data)->desktop;
733 /* try get from the startup-notification protocol */
734 if (sn_get_desktop(self->startup_id, &self->desktop)) {
735 if (self->desktop >= screen_num_desktops &&
736 self->desktop != DESKTOP_ALL)
737 self->desktop = screen_num_desktops - 1;
739 /* defaults to the current desktop */
740 self->desktop = screen_desktop;
743 if (self->desktop != d) {
744 /* set the desktop hint, to make sure that it always exists */
745 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
749 static void client_get_state(ObClient *self)
754 if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
756 for (i = 0; i < num; ++i) {
757 if (state[i] == prop_atoms.net_wm_state_modal)
759 else if (state[i] == prop_atoms.net_wm_state_shaded)
761 else if (state[i] == prop_atoms.net_wm_state_hidden)
763 else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
764 self->skip_taskbar = TRUE;
765 else if (state[i] == prop_atoms.net_wm_state_skip_pager)
766 self->skip_pager = TRUE;
767 else if (state[i] == prop_atoms.net_wm_state_fullscreen)
768 self->fullscreen = TRUE;
769 else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
770 self->max_vert = TRUE;
771 else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
772 self->max_horz = TRUE;
773 else if (state[i] == prop_atoms.net_wm_state_above)
775 else if (state[i] == prop_atoms.net_wm_state_below)
783 static void client_get_shaped(ObClient *self)
785 self->shaped = FALSE;
787 if (extensions_shape) {
792 XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
794 XShapeQueryExtents(ob_display, self->window, &s, &foo,
795 &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
797 self->shaped = (s != 0);
802 void client_update_transient_for(ObClient *self)
805 ObClient *target = NULL;
807 if (XGetTransientForHint(ob_display, self->window, &t)) {
808 self->transient = TRUE;
809 if (t != self->window) { /* cant be transient to itself! */
810 target = g_hash_table_lookup(window_map, &t);
811 /* if this happens then we need to check for it*/
812 g_assert(target != self);
813 if (target && !WINDOW_IS_CLIENT(target)) {
814 /* this can happen when a dialog is a child of
815 a dockapp, for example */
819 if (!target && self->group) {
820 /* not transient to a client, see if it is transient for a
822 if (t == self->group->leader ||
824 t == RootWindow(ob_display, ob_screen)) {
825 /* window is a transient for its group! */
826 target = OB_TRAN_GROUP;
831 self->transient = FALSE;
833 /* if anything has changed... */
834 if (target != self->transient_for) {
835 if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
838 /* remove from old parents */
839 for (it = self->group->members; it; it = g_slist_next(it)) {
840 ObClient *c = it->data;
841 if (c != self && !c->transient_for)
842 c->transients = g_slist_remove(c->transients, self);
844 } else if (self->transient_for != NULL) { /* transient of window */
845 /* remove from old parent */
846 self->transient_for->transients =
847 g_slist_remove(self->transient_for->transients, self);
849 self->transient_for = target;
850 if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
853 /* add to new parents */
854 for (it = self->group->members; it; it = g_slist_next(it)) {
855 ObClient *c = it->data;
856 if (c != self && !c->transient_for)
857 c->transients = g_slist_append(c->transients, self);
860 /* remove all transients which are in the group, that causes
861 circlular pointer hell of doom */
862 for (it = self->group->members; it; it = g_slist_next(it)) {
864 for (sit = self->transients; sit; sit = next) {
865 next = g_slist_next(sit);
866 if (sit->data == it->data)
868 g_slist_delete_link(self->transients, sit);
871 } else if (self->transient_for != NULL) { /* transient of window */
872 /* add to new parent */
873 self->transient_for->transients =
874 g_slist_append(self->transient_for->transients, self);
879 static void client_get_mwm_hints(ObClient *self)
884 self->mwmhints.flags = 0; /* default to none */
886 if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
888 if (num >= OB_MWM_ELEMENTS) {
889 self->mwmhints.flags = hints[0];
890 self->mwmhints.functions = hints[1];
891 self->mwmhints.decorations = hints[2];
897 void client_get_type(ObClient *self)
904 if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
905 /* use the first value that we know about in the array */
906 for (i = 0; i < num; ++i) {
907 if (val[i] == prop_atoms.net_wm_window_type_desktop)
908 self->type = OB_CLIENT_TYPE_DESKTOP;
909 else if (val[i] == prop_atoms.net_wm_window_type_dock)
910 self->type = OB_CLIENT_TYPE_DOCK;
911 else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
912 self->type = OB_CLIENT_TYPE_TOOLBAR;
913 else if (val[i] == prop_atoms.net_wm_window_type_menu)
914 self->type = OB_CLIENT_TYPE_MENU;
915 else if (val[i] == prop_atoms.net_wm_window_type_utility)
916 self->type = OB_CLIENT_TYPE_UTILITY;
917 else if (val[i] == prop_atoms.net_wm_window_type_splash)
918 self->type = OB_CLIENT_TYPE_SPLASH;
919 else if (val[i] == prop_atoms.net_wm_window_type_dialog)
920 self->type = OB_CLIENT_TYPE_DIALOG;
921 else if (val[i] == prop_atoms.net_wm_window_type_normal)
922 self->type = OB_CLIENT_TYPE_NORMAL;
923 else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
924 /* prevent this window from getting any decor or
926 self->mwmhints.flags &= (OB_MWM_FLAG_FUNCTIONS |
927 OB_MWM_FLAG_DECORATIONS);
928 self->mwmhints.decorations = 0;
929 self->mwmhints.functions = 0;
931 if (self->type != (ObClientType) -1)
932 break; /* grab the first legit type */
937 if (self->type == (ObClientType) -1) {
938 /*the window type hint was not set, which means we either classify
939 ourself as a normal window or a dialog, depending on if we are a
942 self->type = OB_CLIENT_TYPE_DIALOG;
944 self->type = OB_CLIENT_TYPE_NORMAL;
948 void client_update_protocols(ObClient *self)
953 self->focus_notify = FALSE;
954 self->delete_window = FALSE;
956 if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
957 for (i = 0; i < num_return; ++i) {
958 if (proto[i] == prop_atoms.wm_delete_window) {
959 /* this means we can request the window to close */
960 self->delete_window = TRUE;
961 } else if (proto[i] == prop_atoms.wm_take_focus)
962 /* if this protocol is requested, then the window will be
963 notified whenever we want it to receive focus */
964 self->focus_notify = TRUE;
970 static void client_get_gravity(ObClient *self)
972 XWindowAttributes wattrib;
975 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
976 g_assert(ret != BadWindow);
977 self->gravity = wattrib.win_gravity;
980 void client_update_normal_hints(ObClient *self)
984 int oldgravity = self->gravity;
987 self->min_ratio = 0.0f;
988 self->max_ratio = 0.0f;
989 SIZE_SET(self->size_inc, 1, 1);
990 SIZE_SET(self->base_size, 0, 0);
991 SIZE_SET(self->min_size, 0, 0);
992 SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
994 /* get the hints from the window */
995 if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
996 self->positioned = !!(size.flags & (PPosition|USPosition));
998 if (size.flags & PWinGravity) {
999 self->gravity = size.win_gravity;
1001 /* if the client has a frame, i.e. has already been mapped and
1002 is changing its gravity */
1003 if (self->frame && self->gravity != oldgravity) {
1004 /* move our idea of the client's position based on its new
1006 self->area.x = self->frame->area.x;
1007 self->area.y = self->frame->area.y;
1008 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
1012 if (size.flags & PAspect) {
1013 if (size.min_aspect.y)
1014 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
1015 if (size.max_aspect.y)
1016 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
1019 if (size.flags & PMinSize)
1020 SIZE_SET(self->min_size, size.min_width, size.min_height);
1022 if (size.flags & PMaxSize)
1023 SIZE_SET(self->max_size, size.max_width, size.max_height);
1025 if (size.flags & PBaseSize)
1026 SIZE_SET(self->base_size, size.base_width, size.base_height);
1028 if (size.flags & PResizeInc)
1029 SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
1033 void client_setup_decor_and_functions(ObClient *self)
1035 /* start with everything (cept fullscreen) */
1037 (OB_FRAME_DECOR_TITLEBAR |
1038 (ob_rr_theme->show_handle ? OB_FRAME_DECOR_HANDLE : 0) |
1039 OB_FRAME_DECOR_GRIPS |
1040 OB_FRAME_DECOR_BORDER |
1041 OB_FRAME_DECOR_ICON |
1042 OB_FRAME_DECOR_ALLDESKTOPS |
1043 OB_FRAME_DECOR_ICONIFY |
1044 OB_FRAME_DECOR_MAXIMIZE |
1045 OB_FRAME_DECOR_SHADE);
1047 (OB_CLIENT_FUNC_RESIZE |
1048 OB_CLIENT_FUNC_MOVE |
1049 OB_CLIENT_FUNC_ICONIFY |
1050 OB_CLIENT_FUNC_MAXIMIZE |
1051 OB_CLIENT_FUNC_SHADE);
1052 if (self->delete_window) {
1053 self->functions |= OB_CLIENT_FUNC_CLOSE;
1054 self->decorations |= OB_FRAME_DECOR_CLOSE;
1057 if (!(self->min_size.width < self->max_size.width ||
1058 self->min_size.height < self->max_size.height))
1059 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1061 switch (self->type) {
1062 case OB_CLIENT_TYPE_NORMAL:
1063 /* normal windows retain all of the possible decorations and
1064 functionality, and are the only windows that you can fullscreen */
1065 self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1068 case OB_CLIENT_TYPE_DIALOG:
1069 case OB_CLIENT_TYPE_UTILITY:
1070 /* these windows cannot be maximized */
1071 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1074 case OB_CLIENT_TYPE_MENU:
1075 case OB_CLIENT_TYPE_TOOLBAR:
1076 /* these windows get less functionality */
1077 self->functions &= ~(OB_CLIENT_FUNC_ICONIFY | OB_CLIENT_FUNC_RESIZE);
1080 case OB_CLIENT_TYPE_DESKTOP:
1081 case OB_CLIENT_TYPE_DOCK:
1082 case OB_CLIENT_TYPE_SPLASH:
1083 /* none of these windows are manipulated by the window manager */
1084 self->decorations = 0;
1085 self->functions = 0;
1089 /* Mwm Hints are applied subtractively to what has already been chosen for
1090 decor and functionality */
1091 if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
1092 if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
1093 if (! ((self->mwmhints.decorations & OB_MWM_DECOR_HANDLE) ||
1094 (self->mwmhints.decorations & OB_MWM_DECOR_TITLE)))
1095 /* if the mwm hints request no handle or title, then all
1096 decorations are disabled */
1097 self->decorations = 0;
1101 if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
1102 if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
1103 if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
1104 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1105 if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
1106 self->functions &= ~OB_CLIENT_FUNC_MOVE;
1107 /* dont let mwm hints kill any buttons
1108 if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
1109 self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
1110 if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
1111 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1113 /* dont let mwm hints kill the close button
1114 if (! (self->mwmhints.functions & MwmFunc_Close))
1115 self->functions &= ~OB_CLIENT_FUNC_CLOSE; */
1119 if (!(self->functions & OB_CLIENT_FUNC_SHADE))
1120 self->decorations &= ~OB_FRAME_DECOR_SHADE;
1121 if (!(self->functions & OB_CLIENT_FUNC_ICONIFY))
1122 self->decorations &= ~OB_FRAME_DECOR_ICONIFY;
1123 if (!(self->functions & OB_CLIENT_FUNC_RESIZE))
1124 self->decorations &= ~OB_FRAME_DECOR_GRIPS;
1126 /* can't maximize without moving/resizing */
1127 if (!((self->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
1128 (self->functions & OB_CLIENT_FUNC_MOVE) &&
1129 (self->functions & OB_CLIENT_FUNC_RESIZE))) {
1130 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1131 self->decorations &= ~OB_FRAME_DECOR_MAXIMIZE;
1134 /* kill the handle on fully maxed windows */
1135 if (self->max_vert && self->max_horz)
1136 self->decorations &= ~OB_FRAME_DECOR_HANDLE;
1138 /* finally, the user can have requested no decorations, which overrides
1140 if (!self->decorate)
1141 self->decorations = OB_FRAME_DECOR_BORDER;
1143 /* if we don't have a titlebar, then we cannot shade! */
1144 if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1145 self->functions &= ~OB_CLIENT_FUNC_SHADE;
1147 /* now we need to check against rules for the client's current state */
1148 if (self->fullscreen) {
1149 self->functions &= (OB_CLIENT_FUNC_CLOSE |
1150 OB_CLIENT_FUNC_FULLSCREEN |
1151 OB_CLIENT_FUNC_ICONIFY);
1152 self->decorations = 0;
1155 client_change_allowed_actions(self);
1158 /* adjust the client's decorations, etc. */
1159 client_reconfigure(self);
1161 /* this makes sure that these windows appear on all desktops */
1162 if (self->type == OB_CLIENT_TYPE_DESKTOP &&
1163 self->desktop != DESKTOP_ALL)
1165 self->desktop = DESKTOP_ALL;
1170 static void client_change_allowed_actions(ObClient *self)
1175 /* desktop windows are kept on all desktops */
1176 if (self->type != OB_CLIENT_TYPE_DESKTOP)
1177 actions[num++] = prop_atoms.net_wm_action_change_desktop;
1179 if (self->functions & OB_CLIENT_FUNC_SHADE)
1180 actions[num++] = prop_atoms.net_wm_action_shade;
1181 if (self->functions & OB_CLIENT_FUNC_CLOSE)
1182 actions[num++] = prop_atoms.net_wm_action_close;
1183 if (self->functions & OB_CLIENT_FUNC_MOVE)
1184 actions[num++] = prop_atoms.net_wm_action_move;
1185 if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1186 actions[num++] = prop_atoms.net_wm_action_minimize;
1187 if (self->functions & OB_CLIENT_FUNC_RESIZE)
1188 actions[num++] = prop_atoms.net_wm_action_resize;
1189 if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1190 actions[num++] = prop_atoms.net_wm_action_fullscreen;
1191 if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1192 actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1193 actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1196 PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1198 /* make sure the window isn't breaking any rules now */
1200 if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1201 if (self->frame) client_shade(self, FALSE);
1202 else self->shaded = FALSE;
1204 if (!(self->functions & OB_CLIENT_FUNC_ICONIFY) && self->iconic) {
1205 if (self->frame) client_iconify(self, FALSE, TRUE);
1206 else self->iconic = FALSE;
1208 if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1209 if (self->frame) client_fullscreen(self, FALSE, TRUE);
1210 else self->fullscreen = FALSE;
1212 if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
1214 if (self->frame) client_maximize(self, FALSE, 0, TRUE);
1215 else self->max_vert = self->max_horz = FALSE;
1219 void client_reconfigure(ObClient *self)
1221 /* by making this pass FALSE for user, we avoid the emacs event storm where
1222 every configurenotify causes an update in its normal hints, i think this
1223 is generally what we want anyways... */
1224 client_configure(self, OB_CORNER_TOPLEFT, self->area.x, self->area.y,
1225 self->area.width, self->area.height, FALSE, TRUE);
1228 void client_update_wmhints(ObClient *self)
1231 gboolean ur = FALSE;
1234 /* assume a window takes input if it doesnt specify */
1235 self->can_focus = TRUE;
1237 if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1238 if (hints->flags & InputHint)
1239 self->can_focus = hints->input;
1241 /* only do this when first managing the window *AND* when we aren't
1243 if (ob_state() != OB_STATE_STARTING && self->frame == NULL)
1244 if (hints->flags & StateHint)
1245 self->iconic = hints->initial_state == IconicState;
1247 if (hints->flags & XUrgencyHint)
1250 if (!(hints->flags & WindowGroupHint))
1251 hints->window_group = None;
1253 /* did the group state change? */
1254 if (hints->window_group !=
1255 (self->group ? self->group->leader : None)) {
1256 /* remove from the old group if there was one */
1257 if (self->group != NULL) {
1258 /* remove transients of the group */
1259 for (it = self->group->members; it; it = it->next)
1260 self->transients = g_slist_remove(self->transients,
1262 group_remove(self->group, self);
1265 if (hints->window_group != None) {
1266 self->group = group_add(hints->window_group, self);
1268 /* i can only have transients from the group if i am not
1270 if (!self->transient_for) {
1271 /* add other transients of the group that are already
1273 for (it = self->group->members; it; it = it->next) {
1274 ObClient *c = it->data;
1275 if (c != self && c->transient_for == OB_TRAN_GROUP)
1277 g_slist_append(self->transients, c);
1282 /* because the self->transient flag wont change from this call,
1283 we don't need to update the window's type and such, only its
1284 transient_for, and the transients lists of other windows in
1285 the group may be affected */
1286 client_update_transient_for(self);
1289 /* the WM_HINTS can contain an icon */
1290 client_update_icons(self);
1295 if (ur != self->urgent) {
1297 ob_debug("Urgent Hint for 0x%lx: %s\n", self->window,
1299 /* fire the urgent callback if we're mapped, otherwise, wait until
1300 after we're mapped */
1302 client_urgent_notify(self);
1306 void client_update_title(ObClient *self)
1312 gboolean read_title;
1314 g_free(self->title);
1317 if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1318 /* try old x stuff */
1319 if (!PROP_GETS(self->window, wm_name, locale, &data))
1320 data = g_strdup("Unnamed Window");
1322 /* look for duplicates and append a number */
1324 for (it = client_list; it; it = it->next)
1325 if (it->data != self) {
1326 ObClient *c = it->data;
1327 if (0 == strncmp(c->title, data, strlen(data)))
1328 nums |= 1 << c->title_count;
1330 /* find first free number */
1331 for (i = 1; i <= 32; ++i)
1332 if (!(nums & (1 << i))) {
1333 if (self->title_count == 1 || i == 1)
1334 self->title_count = i;
1337 /* dont display the number for the first window */
1338 if (self->title_count > 1) {
1339 char *vdata, *ndata;
1340 ndata = g_strdup_printf(" - [%u]", self->title_count);
1341 vdata = g_strconcat(data, ndata, NULL);
1347 PROP_SETS(self->window, net_wm_visible_name, data);
1352 frame_adjust_title(self->frame);
1354 /* update the icon title */
1356 g_free(self->icon_title);
1360 if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1361 /* try old x stuff */
1362 if (!PROP_GETS(self->window, wm_icon_name, locale, &data)) {
1363 data = g_strdup(self->title);
1367 /* append the title count, dont display the number for the first window */
1368 if (read_title && self->title_count > 1) {
1369 char *vdata, *ndata;
1370 ndata = g_strdup_printf(" - [%u]", self->title_count);
1371 vdata = g_strconcat(data, ndata, NULL);
1377 PROP_SETS(self->window, net_wm_visible_icon_name, data);
1379 self->icon_title = data;
1382 void client_update_class(ObClient *self)
1387 if (self->name) g_free(self->name);
1388 if (self->class) g_free(self->class);
1389 if (self->role) g_free(self->role);
1391 self->name = self->class = self->role = NULL;
1393 if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1395 self->name = g_strdup(data[0]);
1397 self->class = g_strdup(data[1]);
1402 if (PROP_GETS(self->window, wm_window_role, locale, &s))
1403 self->role = g_strdup(s);
1405 if (self->name == NULL) self->name = g_strdup("");
1406 if (self->class == NULL) self->class = g_strdup("");
1407 if (self->role == NULL) self->role = g_strdup("");
1410 void client_update_strut(ObClient *self)
1414 gboolean got = FALSE;
1417 if (PROP_GETA32(self->window, net_wm_strut_partial, cardinal,
1421 STRUT_PARTIAL_SET(strut,
1422 data[0], data[2], data[1], data[3],
1423 data[4], data[5], data[8], data[9],
1424 data[6], data[7], data[10], data[11]);
1430 PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1433 STRUT_PARTIAL_SET(strut,
1434 data[0], data[2], data[1], data[3],
1435 0, 0, 0, 0, 0, 0, 0, 0);
1441 STRUT_PARTIAL_SET(strut, 0, 0, 0, 0,
1442 0, 0, 0, 0, 0, 0, 0, 0);
1444 if (!STRUT_EQUAL(strut, self->strut)) {
1445 self->strut = strut;
1447 /* updating here is pointless while we're being mapped cuz we're not in
1448 the client list yet */
1450 screen_update_areas();
1454 void client_update_icons(ObClient *self)
1460 for (i = 0; i < self->nicons; ++i)
1461 g_free(self->icons[i].data);
1462 if (self->nicons > 0)
1463 g_free(self->icons);
1466 if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1467 /* figure out how many valid icons are in here */
1469 while (num - i > 2) {
1473 if (i > num || w*h == 0) break;
1477 self->icons = g_new(ObClientIcon, self->nicons);
1479 /* store the icons */
1481 for (j = 0; j < self->nicons; ++j) {
1484 w = self->icons[j].width = data[i++];
1485 h = self->icons[j].height = data[i++];
1487 if (w*h == 0) continue;
1489 self->icons[j].data = g_new(RrPixel32, w * h);
1490 for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1495 self->icons[j].data[t] =
1496 (((data[i] >> 24) & 0xff) << RrDefaultAlphaOffset) +
1497 (((data[i] >> 16) & 0xff) << RrDefaultRedOffset) +
1498 (((data[i] >> 8) & 0xff) << RrDefaultGreenOffset) +
1499 (((data[i] >> 0) & 0xff) << RrDefaultBlueOffset);
1505 } else if (PROP_GETA32(self->window, kwm_win_icon,
1506 kwm_win_icon, &data, &num)) {
1509 self->icons = g_new(ObClientIcon, self->nicons);
1510 xerror_set_ignore(TRUE);
1511 if (!RrPixmapToRGBA(ob_rr_inst,
1513 &self->icons[self->nicons-1].width,
1514 &self->icons[self->nicons-1].height,
1515 &self->icons[self->nicons-1].data)) {
1516 g_free(&self->icons[self->nicons-1]);
1519 xerror_set_ignore(FALSE);
1525 if ((hints = XGetWMHints(ob_display, self->window))) {
1526 if (hints->flags & IconPixmapHint) {
1528 self->icons = g_new(ObClientIcon, self->nicons);
1529 xerror_set_ignore(TRUE);
1530 if (!RrPixmapToRGBA(ob_rr_inst,
1532 (hints->flags & IconMaskHint ?
1533 hints->icon_mask : None),
1534 &self->icons[self->nicons-1].width,
1535 &self->icons[self->nicons-1].height,
1536 &self->icons[self->nicons-1].data)){
1537 g_free(&self->icons[self->nicons-1]);
1540 xerror_set_ignore(FALSE);
1547 frame_adjust_icon(self->frame);
1550 static void client_change_state(ObClient *self)
1553 guint32 netstate[10];
1556 state[0] = self->wmstate;
1558 PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1562 netstate[num++] = prop_atoms.net_wm_state_modal;
1564 netstate[num++] = prop_atoms.net_wm_state_shaded;
1566 netstate[num++] = prop_atoms.net_wm_state_hidden;
1567 if (self->skip_taskbar)
1568 netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1569 if (self->skip_pager)
1570 netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1571 if (self->fullscreen)
1572 netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1574 netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1576 netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1578 netstate[num++] = prop_atoms.net_wm_state_above;
1580 netstate[num++] = prop_atoms.net_wm_state_below;
1581 PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1583 client_calc_layer(self);
1586 frame_adjust_state(self->frame);
1589 ObClient *client_search_focus_tree(ObClient *self)
1594 for (it = self->transients; it != NULL; it = it->next) {
1595 if (client_focused(it->data)) return it->data;
1596 if ((ret = client_search_focus_tree(it->data))) return ret;
1601 ObClient *client_search_focus_tree_full(ObClient *self)
1603 if (self->transient_for) {
1604 if (self->transient_for != OB_TRAN_GROUP) {
1605 return client_search_focus_tree_full(self->transient_for);
1608 gboolean recursed = FALSE;
1610 for (it = self->group->members; it; it = it->next)
1611 if (!((ObClient*)it->data)->transient_for) {
1613 if ((c = client_search_focus_tree_full(it->data)))
1622 /* this function checks the whole tree, the client_search_focus_tree~
1623 does not, so we need to check this window */
1624 if (client_focused(self))
1626 return client_search_focus_tree(self);
1629 static ObStackingLayer calc_layer(ObClient *self)
1633 if (self->fullscreen) l = OB_STACKING_LAYER_FULLSCREEN;
1634 else if (self->type == OB_CLIENT_TYPE_DESKTOP)
1635 l = OB_STACKING_LAYER_DESKTOP;
1636 else if (self->type == OB_CLIENT_TYPE_DOCK) {
1637 if (!self->below) l = OB_STACKING_LAYER_TOP;
1638 else l = OB_STACKING_LAYER_NORMAL;
1640 else if (self->above) l = OB_STACKING_LAYER_ABOVE;
1641 else if (self->below) l = OB_STACKING_LAYER_BELOW;
1642 else l = OB_STACKING_LAYER_NORMAL;
1647 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
1648 ObStackingLayer l, gboolean raised)
1650 ObStackingLayer old, own;
1654 own = calc_layer(self);
1655 self->layer = l > own ? l : own;
1657 for (it = self->transients; it; it = it->next)
1658 client_calc_layer_recursive(it->data, orig,
1659 l, raised ? raised : l != old);
1661 if (!raised && l != old)
1662 if (orig->frame) { /* only restack if the original window is managed */
1663 /* XXX add_non_intrusive ever? */
1664 stacking_remove(CLIENT_AS_WINDOW(self));
1665 stacking_add(CLIENT_AS_WINDOW(self));
1669 void client_calc_layer(ObClient *self)
1676 /* transients take on the layer of their parents */
1677 self = client_search_top_transient(self);
1679 l = calc_layer(self);
1681 client_calc_layer_recursive(self, orig, l, FALSE);
1684 gboolean client_should_show(ObClient *self)
1686 if (self->iconic) return FALSE;
1687 else if (!(self->desktop == screen_desktop ||
1688 self->desktop == DESKTOP_ALL)) return FALSE;
1689 else if (client_normal(self) && screen_showing_desktop) return FALSE;
1694 static void client_showhide(ObClient *self)
1697 if (client_should_show(self))
1698 frame_show(self->frame);
1700 frame_hide(self->frame);
1703 gboolean client_normal(ObClient *self) {
1704 return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
1705 self->type == OB_CLIENT_TYPE_DOCK ||
1706 self->type == OB_CLIENT_TYPE_SPLASH);
1709 static void client_apply_startup_state(ObClient *self)
1711 /* these are in a carefully crafted order.. */
1713 g_message("self->iconic %d", self->iconic);
1715 self->iconic = FALSE;
1716 client_iconify(self, TRUE, FALSE);
1718 if (self->fullscreen) {
1719 self->fullscreen = FALSE;
1720 client_fullscreen(self, TRUE, FALSE);
1723 self->shaded = FALSE;
1724 client_shade(self, TRUE);
1727 client_urgent_notify(self);
1729 if (self->max_vert && self->max_horz) {
1730 self->max_vert = self->max_horz = FALSE;
1731 client_maximize(self, TRUE, 0, FALSE);
1732 } else if (self->max_vert) {
1733 self->max_vert = FALSE;
1734 client_maximize(self, TRUE, 2, FALSE);
1735 } else if (self->max_horz) {
1736 self->max_horz = FALSE;
1737 client_maximize(self, TRUE, 1, FALSE);
1740 /* nothing to do for the other states:
1749 void client_configure_full(ObClient *self, ObCorner anchor,
1750 int x, int y, int w, int h,
1751 gboolean user, gboolean final,
1752 gboolean force_reply)
1754 gboolean moved = FALSE, resized = FALSE;
1755 guint fdecor = self->frame->decorations;
1756 gboolean fhorz = self->frame->max_horz;
1758 /* make the frame recalculate its dimentions n shit without changing
1759 anything visible for real, this way the constraints below can work with
1760 the updated frame dimensions. */
1761 frame_adjust_area(self->frame, TRUE, TRUE, TRUE);
1763 /* gets the frame's position */
1764 frame_client_gravity(self->frame, &x, &y);
1766 /* these positions are frame positions, not client positions */
1768 /* set the size and position if fullscreen */
1769 if (self->fullscreen) {
1772 XF86VidModeModeLine mode;
1777 i = client_monitor(self);
1778 a = screen_physical_area_monitor(i);
1781 if (i == 0 && /* primary head */
1782 extensions_vidmode &&
1783 XF86VidModeGetViewPort(ob_display, ob_screen, &x, &y) &&
1784 /* get the mode last so the mode.privsize isnt freed incorrectly */
1785 XF86VidModeGetModeLine(ob_display, ob_screen, &dot, &mode)) {
1790 if (mode.privsize) XFree(mode.private);
1800 user = FALSE; /* ignore that increment etc shit when in fullscreen */
1804 a = screen_area_monitor(self->desktop, client_monitor(self));
1806 /* set the size and position if maximized */
1807 if (self->max_horz) {
1809 w = a->width - self->frame->size.left - self->frame->size.right;
1811 if (self->max_vert) {
1813 h = a->height - self->frame->size.top - self->frame->size.bottom;
1817 /* gets the client's position */
1818 frame_frame_gravity(self->frame, &x, &y);
1820 /* these override the above states! if you cant move you can't move! */
1822 if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
1826 if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
1827 w = self->area.width;
1828 h = self->area.height;
1832 if (!(w == self->area.width && h == self->area.height)) {
1833 int basew, baseh, minw, minh;
1835 /* base size is substituted with min size if not specified */
1836 if (self->base_size.width || self->base_size.height) {
1837 basew = self->base_size.width;
1838 baseh = self->base_size.height;
1840 basew = self->min_size.width;
1841 baseh = self->min_size.height;
1843 /* min size is substituted with base size if not specified */
1844 if (self->min_size.width || self->min_size.height) {
1845 minw = self->min_size.width;
1846 minh = self->min_size.height;
1848 minw = self->base_size.width;
1849 minh = self->base_size.height;
1852 /* if this is a user-requested resize, then check against min/max
1855 /* smaller than min size or bigger than max size? */
1856 if (w > self->max_size.width) w = self->max_size.width;
1857 if (w < minw) w = minw;
1858 if (h > self->max_size.height) h = self->max_size.height;
1859 if (h < minh) h = minh;
1864 /* keep to the increments */
1865 w /= self->size_inc.width;
1866 h /= self->size_inc.height;
1868 /* you cannot resize to nothing */
1869 if (basew + w < 1) w = 1 - basew;
1870 if (baseh + h < 1) h = 1 - baseh;
1872 /* store the logical size */
1873 SIZE_SET(self->logical_size,
1874 self->size_inc.width > 1 ? w : w + basew,
1875 self->size_inc.height > 1 ? h : h + baseh);
1877 w *= self->size_inc.width;
1878 h *= self->size_inc.height;
1883 /* adjust the height to match the width for the aspect ratios.
1884 for this, min size is not substituted for base size ever. */
1885 w -= self->base_size.width;
1886 h -= self->base_size.height;
1888 if (self->min_ratio)
1889 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1890 if (self->max_ratio)
1891 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1893 w += self->base_size.width;
1894 h += self->base_size.height;
1898 case OB_CORNER_TOPLEFT:
1900 case OB_CORNER_TOPRIGHT:
1901 x -= w - self->area.width;
1903 case OB_CORNER_BOTTOMLEFT:
1904 y -= h - self->area.height;
1906 case OB_CORNER_BOTTOMRIGHT:
1907 x -= w - self->area.width;
1908 y -= h - self->area.height;
1912 moved = x != self->area.x || y != self->area.y;
1913 resized = w != self->area.width || h != self->area.height;
1915 RECT_SET(self->area, x, y, w, h);
1917 /* for app-requested resizes, always resize if 'resized' is true.
1918 for user-requested ones, only resize if final is true, or when
1919 resizing in redraw mode */
1920 if ((!user && resized) ||
1921 (user && (final || (resized && config_redraw_resize))))
1922 XResizeWindow(ob_display, self->window, w, h);
1924 /* move/resize the frame to match the request */
1926 if (self->decorations != fdecor || self->max_horz != fhorz)
1927 moved = resized = TRUE;
1929 if (moved || resized)
1930 frame_adjust_area(self->frame, moved, resized, FALSE);
1932 if (!resized && (force_reply || ((!user && moved) || (user && final))))
1935 event.type = ConfigureNotify;
1936 event.xconfigure.display = ob_display;
1937 event.xconfigure.event = self->window;
1938 event.xconfigure.window = self->window;
1940 /* root window real coords */
1941 event.xconfigure.x = self->frame->area.x + self->frame->size.left -
1943 event.xconfigure.y = self->frame->area.y + self->frame->size.top -
1945 event.xconfigure.width = w;
1946 event.xconfigure.height = h;
1947 event.xconfigure.border_width = 0;
1948 event.xconfigure.above = self->frame->plate;
1949 event.xconfigure.override_redirect = FALSE;
1950 XSendEvent(event.xconfigure.display, event.xconfigure.window,
1951 FALSE, StructureNotifyMask, &event);
1956 void client_fullscreen(ObClient *self, gboolean fs, gboolean savearea)
1960 if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
1961 self->fullscreen == fs) return; /* already done */
1963 self->fullscreen = fs;
1964 client_change_state(self); /* change the state hints on the client,
1965 and adjust out layer/stacking */
1969 guint32 dimensions[4];
1970 dimensions[0] = self->area.x;
1971 dimensions[1] = self->area.y;
1972 dimensions[2] = self->area.width;
1973 dimensions[3] = self->area.height;
1975 PROP_SETA32(self->window, openbox_premax, cardinal,
1979 /* these are not actually used cuz client_configure will set them
1980 as appropriate when the window is fullscreened */
1987 /* pick some fallbacks... */
1988 a = screen_area_monitor(self->desktop, 0);
1989 x = a->x + a->width / 4;
1990 y = a->y + a->height / 4;
1994 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1995 (guint32**)&dimensions, &num)) {
2006 client_setup_decor_and_functions(self);
2008 client_move_resize(self, x, y, w, h);
2010 /* try focus us when we go into fullscreen mode */
2014 static void client_iconify_recursive(ObClient *self,
2015 gboolean iconic, gboolean curdesk)
2018 gboolean changed = FALSE;
2021 if (self->iconic != iconic) {
2022 ob_debug("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"),
2025 self->iconic = iconic;
2028 if (self->functions & OB_CLIENT_FUNC_ICONIFY) {
2029 self->wmstate = IconicState;
2030 self->ignore_unmaps++;
2031 /* we unmap the client itself so that we can get MapRequest
2032 events, and because the ICCCM tells us to! */
2033 XUnmapWindow(ob_display, self->window);
2035 /* update the focus lists.. iconic windows go to the bottom of
2036 the list, put the new iconic window at the 'top of the
2038 focus_order_to_top(self);
2044 client_set_desktop(self, screen_desktop, FALSE);
2045 self->wmstate = self->shaded ? IconicState : NormalState;
2046 XMapWindow(ob_display, self->window);
2048 /* this puts it after the current focused window */
2049 focus_order_remove(self);
2050 focus_order_add_new(self);
2052 /* this is here cuz with the VIDMODE extension, the viewport can
2053 change while a fullscreen window is iconic, and when it
2054 uniconifies, it would be nice if it did so to the new position
2056 client_reconfigure(self);
2063 client_change_state(self);
2064 client_showhide(self);
2065 screen_update_areas();
2068 /* iconify all transients */
2069 for (it = self->transients; it != NULL; it = it->next)
2070 if (it->data != self) client_iconify_recursive(it->data,
2074 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk)
2076 /* move up the transient chain as far as possible first */
2077 self = client_search_top_transient(self);
2079 client_iconify_recursive(client_search_top_transient(self),
2083 void client_maximize(ObClient *self, gboolean max, int dir, gboolean savearea)
2087 g_assert(dir == 0 || dir == 1 || dir == 2);
2088 if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE)) return; /* can't */
2090 /* check if already done */
2092 if (dir == 0 && self->max_horz && self->max_vert) return;
2093 if (dir == 1 && self->max_horz) return;
2094 if (dir == 2 && self->max_vert) return;
2096 if (dir == 0 && !self->max_horz && !self->max_vert) return;
2097 if (dir == 1 && !self->max_horz) return;
2098 if (dir == 2 && !self->max_vert) return;
2101 /* work with the frame's coords */
2102 x = self->frame->area.x;
2103 y = self->frame->area.y;
2104 w = self->area.width;
2105 h = self->area.height;
2109 gint32 dimensions[4];
2118 /* get the property off the window and use it for the dimensions
2119 we are already maxed on */
2120 if (PROP_GETA32(self->window, openbox_premax, cardinal,
2121 (guint32**)&readdim, &num)) {
2123 if (self->max_horz) {
2124 dimensions[0] = readdim[0];
2125 dimensions[2] = readdim[2];
2127 if (self->max_vert) {
2128 dimensions[1] = readdim[1];
2129 dimensions[3] = readdim[3];
2135 PROP_SETA32(self->window, openbox_premax, cardinal,
2136 (guint32*)dimensions, 4);
2143 /* pick some fallbacks... */
2144 a = screen_area_monitor(self->desktop, 0);
2145 if (dir == 0 || dir == 1) { /* horz */
2146 x = a->x + a->width / 4;
2149 if (dir == 0 || dir == 2) { /* vert */
2150 y = a->y + a->height / 4;
2154 if (PROP_GETA32(self->window, openbox_premax, cardinal,
2155 (guint32**)&dimensions, &num)) {
2157 if (dir == 0 || dir == 1) { /* horz */
2161 if (dir == 0 || dir == 2) { /* vert */
2170 if (dir == 0 || dir == 1) /* horz */
2171 self->max_horz = max;
2172 if (dir == 0 || dir == 2) /* vert */
2173 self->max_vert = max;
2175 if (!self->max_horz && !self->max_vert)
2176 PROP_ERASE(self->window, openbox_premax);
2178 client_change_state(self); /* change the state hints on the client */
2180 client_setup_decor_and_functions(self);
2182 /* figure out where the client should be going */
2183 frame_frame_gravity(self->frame, &x, &y);
2184 client_move_resize(self, x, y, w, h);
2187 void client_shade(ObClient *self, gboolean shade)
2189 if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
2190 shade) || /* can't shade */
2191 self->shaded == shade) return; /* already done */
2193 /* when we're iconic, don't change the wmstate */
2195 self->wmstate = shade ? IconicState : NormalState;
2196 self->shaded = shade;
2197 client_change_state(self);
2198 /* resize the frame to just the titlebar */
2199 frame_adjust_area(self->frame, FALSE, FALSE, FALSE);
2202 void client_close(ObClient *self)
2206 if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
2209 XXX: itd be cool to do timeouts and shit here for killing the client's
2211 like... if the window is around after 5 seconds, then the close button
2212 turns a nice red, and if this function is called again, the client is
2216 ce.xclient.type = ClientMessage;
2217 ce.xclient.message_type = prop_atoms.wm_protocols;
2218 ce.xclient.display = ob_display;
2219 ce.xclient.window = self->window;
2220 ce.xclient.format = 32;
2221 ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
2222 ce.xclient.data.l[1] = event_lasttime;
2223 ce.xclient.data.l[2] = 0l;
2224 ce.xclient.data.l[3] = 0l;
2225 ce.xclient.data.l[4] = 0l;
2226 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2229 void client_kill(ObClient *self)
2231 XKillClient(ob_display, self->window);
2234 void client_set_desktop_recursive(ObClient *self,
2235 guint target, gboolean donthide)
2240 if (target != self->desktop) {
2242 ob_debug("Setting desktop %u\n", target+1);
2244 g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2246 /* remove from the old desktop(s) */
2247 focus_order_remove(self);
2249 old = self->desktop;
2250 self->desktop = target;
2251 PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2252 /* the frame can display the current desktop state */
2253 frame_adjust_state(self->frame);
2254 /* 'move' the window to the new desktop */
2256 client_showhide(self);
2257 /* raise if it was not already on the desktop */
2258 if (old != DESKTOP_ALL)
2259 stacking_raise(CLIENT_AS_WINDOW(self));
2260 screen_update_areas();
2262 /* add to the new desktop(s) */
2263 if (config_focus_new)
2264 focus_order_to_top(self);
2266 focus_order_to_bottom(self);
2269 /* move all transients */
2270 for (it = self->transients; it != NULL; it = it->next)
2271 if (it->data != self) client_set_desktop_recursive(it->data,
2275 void client_set_desktop(ObClient *self, guint target, gboolean donthide)
2277 client_set_desktop_recursive(client_search_top_transient(self),
2281 ObClient *client_search_modal_child(ObClient *self)
2286 for (it = self->transients; it != NULL; it = it->next) {
2287 ObClient *c = it->data;
2288 if ((ret = client_search_modal_child(c))) return ret;
2289 if (c->modal) return c;
2294 gboolean client_validate(ObClient *self)
2298 XSync(ob_display, FALSE); /* get all events on the server */
2300 if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2301 XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2302 XPutBackEvent(ob_display, &e);
2309 void client_set_wm_state(ObClient *self, long state)
2311 if (state == self->wmstate) return; /* no change */
2315 client_iconify(self, TRUE, TRUE);
2318 client_iconify(self, FALSE, TRUE);
2323 void client_set_state(ObClient *self, Atom action, long data1, long data2)
2325 gboolean shaded = self->shaded;
2326 gboolean fullscreen = self->fullscreen;
2327 gboolean max_horz = self->max_horz;
2328 gboolean max_vert = self->max_vert;
2331 if (!(action == prop_atoms.net_wm_state_add ||
2332 action == prop_atoms.net_wm_state_remove ||
2333 action == prop_atoms.net_wm_state_toggle))
2334 /* an invalid action was passed to the client message, ignore it */
2337 for (i = 0; i < 2; ++i) {
2338 Atom state = i == 0 ? data1 : data2;
2340 if (!state) continue;
2342 /* if toggling, then pick whether we're adding or removing */
2343 if (action == prop_atoms.net_wm_state_toggle) {
2344 if (state == prop_atoms.net_wm_state_modal)
2345 action = self->modal ? prop_atoms.net_wm_state_remove :
2346 prop_atoms.net_wm_state_add;
2347 else if (state == prop_atoms.net_wm_state_maximized_vert)
2348 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2349 prop_atoms.net_wm_state_add;
2350 else if (state == prop_atoms.net_wm_state_maximized_horz)
2351 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2352 prop_atoms.net_wm_state_add;
2353 else if (state == prop_atoms.net_wm_state_shaded)
2354 action = self->shaded ? prop_atoms.net_wm_state_remove :
2355 prop_atoms.net_wm_state_add;
2356 else if (state == prop_atoms.net_wm_state_skip_taskbar)
2357 action = self->skip_taskbar ?
2358 prop_atoms.net_wm_state_remove :
2359 prop_atoms.net_wm_state_add;
2360 else if (state == prop_atoms.net_wm_state_skip_pager)
2361 action = self->skip_pager ?
2362 prop_atoms.net_wm_state_remove :
2363 prop_atoms.net_wm_state_add;
2364 else if (state == prop_atoms.net_wm_state_fullscreen)
2365 action = self->fullscreen ?
2366 prop_atoms.net_wm_state_remove :
2367 prop_atoms.net_wm_state_add;
2368 else if (state == prop_atoms.net_wm_state_above)
2369 action = self->above ? prop_atoms.net_wm_state_remove :
2370 prop_atoms.net_wm_state_add;
2371 else if (state == prop_atoms.net_wm_state_below)
2372 action = self->below ? prop_atoms.net_wm_state_remove :
2373 prop_atoms.net_wm_state_add;
2376 if (action == prop_atoms.net_wm_state_add) {
2377 if (state == prop_atoms.net_wm_state_modal) {
2378 /* XXX raise here or something? */
2380 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2382 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2384 } else if (state == prop_atoms.net_wm_state_shaded) {
2386 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2387 self->skip_taskbar = TRUE;
2388 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2389 self->skip_pager = TRUE;
2390 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2392 } else if (state == prop_atoms.net_wm_state_above) {
2394 } else if (state == prop_atoms.net_wm_state_below) {
2398 } else { /* action == prop_atoms.net_wm_state_remove */
2399 if (state == prop_atoms.net_wm_state_modal) {
2400 self->modal = FALSE;
2401 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2403 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2405 } else if (state == prop_atoms.net_wm_state_shaded) {
2407 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2408 self->skip_taskbar = FALSE;
2409 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2410 self->skip_pager = FALSE;
2411 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2413 } else if (state == prop_atoms.net_wm_state_above) {
2414 self->above = FALSE;
2415 } else if (state == prop_atoms.net_wm_state_below) {
2416 self->below = FALSE;
2420 if (max_horz != self->max_horz || max_vert != self->max_vert) {
2421 if (max_horz != self->max_horz && max_vert != self->max_vert) {
2423 if (max_horz == max_vert) { /* both going the same way */
2424 client_maximize(self, max_horz, 0, TRUE);
2426 client_maximize(self, max_horz, 1, TRUE);
2427 client_maximize(self, max_vert, 2, TRUE);
2431 if (max_horz != self->max_horz)
2432 client_maximize(self, max_horz, 1, TRUE);
2434 client_maximize(self, max_vert, 2, TRUE);
2437 /* change fullscreen state before shading, as it will affect if the window
2439 if (fullscreen != self->fullscreen)
2440 client_fullscreen(self, fullscreen, TRUE);
2441 if (shaded != self->shaded)
2442 client_shade(self, shaded);
2443 client_calc_layer(self);
2444 client_change_state(self); /* change the hint to reflect these changes */
2447 ObClient *client_focus_target(ObClient *self)
2451 /* if we have a modal child, then focus it, not us */
2452 child = client_search_modal_child(self);
2453 if (child) return child;
2457 gboolean client_can_focus(ObClient *self)
2461 /* choose the correct target */
2462 self = client_focus_target(self);
2464 if (!self->frame->visible)
2467 if (!((self->can_focus || self->focus_notify) &&
2468 (self->desktop == screen_desktop ||
2469 self->desktop == DESKTOP_ALL) &&
2473 /* do a check to see if the window has already been unmapped or destroyed
2474 do this intelligently while watching out for unmaps we've generated
2475 (ignore_unmaps > 0) */
2476 if (XCheckTypedWindowEvent(ob_display, self->window,
2477 DestroyNotify, &ev)) {
2478 XPutBackEvent(ob_display, &ev);
2481 while (XCheckTypedWindowEvent(ob_display, self->window,
2482 UnmapNotify, &ev)) {
2483 if (self->ignore_unmaps) {
2484 self->ignore_unmaps--;
2486 XPutBackEvent(ob_display, &ev);
2494 gboolean client_focus(ObClient *self)
2496 /* choose the correct target */
2497 self = client_focus_target(self);
2499 if (!client_can_focus(self)) {
2500 if (!self->frame->visible) {
2501 /* update the focus lists */
2502 focus_order_to_top(self);
2507 if (self->can_focus) {
2508 /* RevertToPointerRoot causes much more headache than RevertToNone, so
2509 I choose to use it always, hopefully to find errors quicker, if any
2510 are left. (I hate X. I hate focus events.)
2512 Update: Changing this to RevertToNone fixed a bug with mozilla (bug
2513 #799. So now it is RevertToNone again.
2515 XSetInputFocus(ob_display, self->window, RevertToNone,
2519 if (self->focus_notify) {
2521 ce.xclient.type = ClientMessage;
2522 ce.xclient.message_type = prop_atoms.wm_protocols;
2523 ce.xclient.display = ob_display;
2524 ce.xclient.window = self->window;
2525 ce.xclient.format = 32;
2526 ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2527 ce.xclient.data.l[1] = event_lasttime;
2528 ce.xclient.data.l[2] = 0l;
2529 ce.xclient.data.l[3] = 0l;
2530 ce.xclient.data.l[4] = 0l;
2531 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2535 ob_debug("%sively focusing %lx at %d\n",
2536 (self->can_focus ? "act" : "pass"),
2537 self->window, (int) event_lasttime);
2540 /* Cause the FocusIn to come back to us. Important for desktop switches,
2541 since otherwise we'll have no FocusIn on the queue and send it off to
2542 the focus_backup. */
2543 XSync(ob_display, FALSE);
2547 void client_unfocus(ObClient *self)
2549 g_assert(focus_client == self);
2551 ob_debug("client_unfocus for %lx\n", self->window);
2553 focus_fallback(OB_FOCUS_FALLBACK_UNFOCUSING);
2556 void client_activate(ObClient *self, gboolean here)
2558 if (client_normal(self) && screen_showing_desktop)
2559 screen_show_desktop(FALSE);
2561 client_iconify(self, FALSE, FALSE);
2562 if (self->desktop != DESKTOP_ALL &&
2563 self->desktop != screen_desktop) {
2565 client_set_desktop(self, screen_desktop, FALSE);
2567 screen_set_desktop(self->desktop);
2568 } else if (!self->frame->visible)
2569 /* if its not visible for other reasons, then don't mess
2573 client_shade(self, FALSE);
2575 stacking_raise(CLIENT_AS_WINDOW(self));
2578 gboolean client_focused(ObClient *self)
2580 return self == focus_client;
2583 ObClientIcon *client_icon(ObClient *self, int w, int h)
2586 /* si is the smallest image >= req */
2587 /* li is the largest image < req */
2588 unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2590 if (!self->nicons) return NULL;
2592 for (i = 0; i < self->nicons; ++i) {
2593 size = self->icons[i].width * self->icons[i].height;
2594 if (size < smallest && size >= (unsigned)(w * h)) {
2598 if (size > largest && size <= (unsigned)(w * h)) {
2603 if (largest == 0) /* didnt find one smaller than the requested size */
2604 return &self->icons[si];
2605 return &self->icons[li];
2608 /* this be mostly ripped from fvwm */
2609 ObClient *client_find_directional(ObClient *c, ObDirection dir)
2611 int my_cx, my_cy, his_cx, his_cy;
2614 int score, best_score;
2615 ObClient *best_client, *cur;
2621 /* first, find the centre coords of the currently focused window */
2622 my_cx = c->frame->area.x + c->frame->area.width / 2;
2623 my_cy = c->frame->area.y + c->frame->area.height / 2;
2628 for(it = g_list_first(client_list); it; it = it->next) {
2631 /* the currently selected window isn't interesting */
2634 if (!client_normal(cur))
2636 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2640 if(client_focus_target(cur) == cur &&
2641 !(cur->can_focus || cur->focus_notify))
2644 /* find the centre coords of this window, from the
2645 * currently focused window's point of view */
2646 his_cx = (cur->frame->area.x - my_cx)
2647 + cur->frame->area.width / 2;
2648 his_cy = (cur->frame->area.y - my_cy)
2649 + cur->frame->area.height / 2;
2651 if(dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
2652 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST) {
2654 /* Rotate the diagonals 45 degrees counterclockwise.
2655 * To do this, multiply the matrix /+h +h\ with the
2656 * vector (x y). \-h +h/
2657 * h = sqrt(0.5). We can set h := 1 since absolute
2658 * distance doesn't matter here. */
2659 tx = his_cx + his_cy;
2660 his_cy = -his_cx + his_cy;
2665 case OB_DIRECTION_NORTH:
2666 case OB_DIRECTION_SOUTH:
2667 case OB_DIRECTION_NORTHEAST:
2668 case OB_DIRECTION_SOUTHWEST:
2669 offset = (his_cx < 0) ? -his_cx : his_cx;
2670 distance = ((dir == OB_DIRECTION_NORTH ||
2671 dir == OB_DIRECTION_NORTHEAST) ?
2674 case OB_DIRECTION_EAST:
2675 case OB_DIRECTION_WEST:
2676 case OB_DIRECTION_SOUTHEAST:
2677 case OB_DIRECTION_NORTHWEST:
2678 offset = (his_cy < 0) ? -his_cy : his_cy;
2679 distance = ((dir == OB_DIRECTION_WEST ||
2680 dir == OB_DIRECTION_NORTHWEST) ?
2685 /* the target must be in the requested direction */
2689 /* Calculate score for this window. The smaller the better. */
2690 score = distance + offset;
2692 /* windows more than 45 degrees off the direction are
2693 * heavily penalized and will only be chosen if nothing
2694 * else within a million pixels */
2695 if(offset > distance)
2698 if(best_score == -1 || score < best_score)
2706 void client_set_layer(ObClient *self, int layer)
2710 self->above = FALSE;
2711 } else if (layer == 0) {
2712 self->below = self->above = FALSE;
2714 self->below = FALSE;
2717 client_calc_layer(self);
2718 client_change_state(self); /* reflect this in the state hints */
2721 guint client_monitor(ObClient *self)
2725 for (i = 0; i < screen_num_monitors; ++i) {
2726 Rect *area = screen_physical_area_monitor(i);
2727 if (RECT_INTERSECTS_RECT(*area, self->frame->area))
2730 if (i == screen_num_monitors) i = 0;
2731 g_assert(i < screen_num_monitors);
2735 ObClient *client_search_top_transient(ObClient *self)
2737 /* move up the transient chain as far as possible */
2738 if (self->transient_for) {
2739 if (self->transient_for != OB_TRAN_GROUP) {
2740 return client_search_top_transient(self->transient_for);
2744 for (it = self->group->members; it; it = it->next) {
2745 ObClient *c = it->data;
2747 /* checking transient_for prevents infinate loops! */
2748 if (c != self && !c->transient_for)
2759 ObClient *client_search_transient(ObClient *self, ObClient *search)
2763 for (sit = self->transients; sit; sit = g_slist_next(sit)) {
2764 if (sit->data == search)
2766 if (client_search_transient(sit->data, search))
2772 gchar* client_get_sm_client_id(ObClient *self)
2776 if (!PROP_GETS(self->window, sm_client_id, locale, &id) && self->group)
2777 PROP_GETS(self->group->leader, sm_client_id, locale, &id);
2781 /* finds the nearest edge in the given direction from the current client
2782 * note to self: the edge is the -frame- edge (the actual one), not the
2785 int client_directional_edge_search(ObClient *c, ObDirection dir)
2788 int my_edge_start, my_edge_end, my_offset;
2795 a = screen_area(c->desktop);
2798 case OB_DIRECTION_NORTH:
2799 my_edge_start = c->frame->area.x;
2800 my_edge_end = c->frame->area.x + c->frame->area.width;
2801 my_offset = c->frame->area.y;
2803 dest = a->y; /* default: top of screen */
2805 for(it = g_list_first(client_list); it; it = it->next) {
2806 int his_edge_start, his_edge_end, his_offset;
2807 ObClient *cur = it->data;
2811 if(!client_normal(cur))
2813 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2818 his_edge_start = cur->frame->area.x;
2819 his_edge_end = cur->frame->area.x + cur->frame->area.width;
2820 his_offset = cur->frame->area.y + cur->frame->area.height;
2822 if(his_offset + 1 > my_offset)
2825 if(his_offset < dest)
2828 if(his_edge_start >= my_edge_start &&
2829 his_edge_start <= my_edge_end)
2832 if(my_edge_start >= his_edge_start &&
2833 my_edge_start <= his_edge_end)
2838 case OB_DIRECTION_SOUTH:
2839 my_edge_start = c->frame->area.x;
2840 my_edge_end = c->frame->area.x + c->frame->area.width;
2841 my_offset = c->frame->area.y + c->frame->area.height;
2843 dest = a->y + a->height; /* default: bottom of screen */
2845 for(it = g_list_first(client_list); it; it = it->next) {
2846 int his_edge_start, his_edge_end, his_offset;
2847 ObClient *cur = it->data;
2851 if(!client_normal(cur))
2853 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2858 his_edge_start = cur->frame->area.x;
2859 his_edge_end = cur->frame->area.x + cur->frame->area.width;
2860 his_offset = cur->frame->area.y;
2863 if(his_offset - 1 < my_offset)
2866 if(his_offset > dest)
2869 if(his_edge_start >= my_edge_start &&
2870 his_edge_start <= my_edge_end)
2873 if(my_edge_start >= his_edge_start &&
2874 my_edge_start <= his_edge_end)
2879 case OB_DIRECTION_WEST:
2880 my_edge_start = c->frame->area.y;
2881 my_edge_end = c->frame->area.y + c->frame->area.height;
2882 my_offset = c->frame->area.x;
2884 dest = a->x; /* default: leftmost egde of screen */
2886 for(it = g_list_first(client_list); it; it = it->next) {
2887 int his_edge_start, his_edge_end, his_offset;
2888 ObClient *cur = it->data;
2892 if(!client_normal(cur))
2894 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2899 his_edge_start = cur->frame->area.y;
2900 his_edge_end = cur->frame->area.y + cur->frame->area.height;
2901 his_offset = cur->frame->area.x + cur->frame->area.width;
2903 if(his_offset + 1 > my_offset)
2906 if(his_offset < dest)
2909 if(his_edge_start >= my_edge_start &&
2910 his_edge_start <= my_edge_end)
2913 if(my_edge_start >= his_edge_start &&
2914 my_edge_start <= his_edge_end)
2920 case OB_DIRECTION_EAST:
2921 my_edge_start = c->frame->area.y;
2922 my_edge_end = c->frame->area.y + c->frame->area.height;
2923 my_offset = c->frame->area.x + c->frame->area.width;
2925 dest = a->x + a->width; /* default: rightmost edge of screen */
2927 for(it = g_list_first(client_list); it; it = it->next) {
2928 int his_edge_start, his_edge_end, his_offset;
2929 ObClient *cur = it->data;
2933 if(!client_normal(cur))
2935 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2940 his_edge_start = cur->frame->area.y;
2941 his_edge_end = cur->frame->area.y + cur->frame->area.height;
2942 his_offset = cur->frame->area.x;
2944 if(his_offset - 1 < my_offset)
2947 if(his_offset > dest)
2950 if(his_edge_start >= my_edge_start &&
2951 his_edge_start <= my_edge_end)
2954 if(my_edge_start >= his_edge_start &&
2955 my_edge_start <= his_edge_end)
2960 case OB_DIRECTION_NORTHEAST:
2961 case OB_DIRECTION_SOUTHEAST:
2962 case OB_DIRECTION_NORTHWEST:
2963 case OB_DIRECTION_SOUTHWEST:
2964 /* not implemented */
2967 g_assert_not_reached();