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;
37 static void client_get_all(ObClient *self);
38 static void client_toggle_border(ObClient *self, gboolean show);
39 static void client_get_area(ObClient *self);
40 static void client_get_desktop(ObClient *self);
41 static void client_get_state(ObClient *self);
42 static void client_get_shaped(ObClient *self);
43 static void client_get_mwm_hints(ObClient *self);
44 static void client_get_gravity(ObClient *self);
45 static void client_showhide(ObClient *self);
46 static void client_change_allowed_actions(ObClient *self);
47 static void client_change_state(ObClient *self);
48 static void client_apply_startup_state(ObClient *self);
49 static void client_restore_session_state(ObClient *self);
50 static void client_restore_session_stacking(ObClient *self);
57 void client_shutdown()
61 void client_set_list()
63 Window *windows, *win_it;
65 guint size = g_list_length(client_list);
67 /* create an array of the window ids */
69 windows = g_new(Window, size);
71 for (it = client_list; it != NULL; it = it->next, ++win_it)
72 *win_it = ((ObClient*)it->data)->window;
76 PROP_SETA32(RootWindow(ob_display, ob_screen),
77 net_client_list, window, (guint32*)windows, size);
86 void client_foreach_transient(ObClient *self, ObClientForeachFunc func, void *data)
90 for (it = self->transients; it; it = it->next) {
91 if (!func(it->data, data)) return;
92 client_foreach_transient(it->data, func, data);
96 void client_foreach_ancestor(ObClient *self, ObClientForeachFunc func, void *data)
98 if (self->transient_for) {
99 if (self->transient_for != OB_TRAN_GROUP) {
100 if (!func(self->transient_for, data)) return;
101 client_foreach_ancestor(self->transient_for, func, data);
105 for (it = self->group->members; it; it = it->next)
106 if (it->data != self &&
107 !((ObClient*)it->data)->transient_for) {
108 if (!func(it->data, data)) return;
109 client_foreach_ancestor(it->data, func, data);
116 void client_manage_all()
118 unsigned int i, j, nchild;
121 XWindowAttributes attrib;
123 XQueryTree(ob_display, RootWindow(ob_display, ob_screen),
124 &w, &w, &children, &nchild);
126 /* remove all icon windows from the list */
127 for (i = 0; i < nchild; i++) {
128 if (children[i] == None) continue;
129 wmhints = XGetWMHints(ob_display, children[i]);
131 if ((wmhints->flags & IconWindowHint) &&
132 (wmhints->icon_window != children[i]))
133 for (j = 0; j < nchild; j++)
134 if (children[j] == wmhints->icon_window) {
142 for (i = 0; i < nchild; ++i) {
143 if (children[i] == None)
145 if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
146 if (attrib.override_redirect) continue;
148 if (attrib.map_state != IsUnmapped)
149 client_manage(children[i]);
154 /* stack them as they were on startup!
155 why with stacking_lower? Why, because then windows who aren't in the
156 stacking list are on the top where you can see them instead of buried
158 for (i = startup_stack_size; i > 0; --i) {
161 w = startup_stack_order[i-1];
162 obw = g_hash_table_lookup(window_map, &w);
164 g_assert(WINDOW_IS_CLIENT(obw));
165 stacking_lower(CLIENT_AS_WINDOW(obw));
168 g_free(startup_stack_order);
169 startup_stack_order = NULL;
170 startup_stack_size = 0;
172 if (config_focus_new) {
175 active = g_hash_table_lookup(window_map, &startup_active);
177 g_assert(WINDOW_IS_CLIENT(active));
178 if (!client_focus(WINDOW_AS_CLIENT(active)))
179 focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS);
181 focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS);
185 void client_manage(Window window)
189 XWindowAttributes attrib;
190 XSetWindowAttributes attrib_set;
192 gboolean activate = FALSE;
196 /* check if it has already been unmapped by the time we started mapping
197 the grab does a sync so we don't have to here */
198 if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
199 XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
200 XPutBackEvent(ob_display, &e);
203 return; /* don't manage it */
206 /* make sure it isn't an override-redirect window */
207 if (!XGetWindowAttributes(ob_display, window, &attrib) ||
208 attrib.override_redirect) {
210 return; /* don't manage it */
213 /* is the window a docking app */
214 if ((wmhint = XGetWMHints(ob_display, window))) {
215 if ((wmhint->flags & StateHint) &&
216 wmhint->initial_state == WithdrawnState) {
217 dock_add(window, wmhint);
225 ob_debug("Managing window: %lx\n", window);
227 /* choose the events we want to receive on the CLIENT window */
228 attrib_set.event_mask = CLIENT_EVENTMASK;
229 attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
230 XChangeWindowAttributes(ob_display, window,
231 CWEventMask|CWDontPropagate, &attrib_set);
234 /* create the ObClient struct, and populate it from the hints on the
236 self = g_new0(ObClient, 1);
237 self->obwin.type = Window_Client;
238 self->window = window;
240 client_get_all(self);
241 client_restore_session_state(self);
243 client_change_state(self);
245 /* remove the client's border (and adjust re gravity) */
246 client_toggle_border(self, FALSE);
248 /* specify that if we exit, the window should not be destroyed and should
249 be reparented back to root automatically */
250 XChangeSaveSet(ob_display, window, SetModeInsert);
252 /* create the decoration frame for the client window */
253 self->frame = frame_new();
255 frame_grab_client(self->frame, self);
259 client_apply_startup_state(self);
261 /* update the focus lists */
262 focus_order_add_new(self);
264 stacking_add(CLIENT_AS_WINDOW(self));
265 client_restore_session_stacking(self);
267 /* focus the new window? */
268 if (ob_state() != OB_STATE_STARTING && config_focus_new &&
269 /* note the check against Type_Normal/Dialog, not client_normal(self),
270 which would also include other types. in this case we want more
271 strict rules for focus */
272 (self->type == OB_CLIENT_TYPE_NORMAL ||
273 self->type == OB_CLIENT_TYPE_DIALOG))
277 if (self->desktop != screen_desktop) {
278 /* activate the window */
281 gboolean group_foc = FALSE;
286 for (it = self->group->members; it; it = it->next)
288 if (client_focused(it->data))
296 (!self->transient_for && (!self->group ||
297 !self->group->members->next))) ||
298 client_search_focus_tree_full(self) ||
300 !client_normal(focus_client))
302 /* activate the window */
309 if (ob_state() == OB_STATE_RUNNING) {
310 int x = self->area.x, ox = x;
311 int y = self->area.y, oy = y;
313 place_client(self, &x, &y);
315 client_find_onscreen(self, &x, &y,
316 self->frame->area.width,
317 self->frame->area.height,
318 client_normal(self));
320 if (x != ox || y != oy)
321 client_configure(self, OB_CORNER_TOPLEFT, x, y,
322 self->area.width, self->area.height,
326 /* make sure the window is visible */
327 client_move_onscreen(self, client_normal(self));
329 client_showhide(self);
331 /* use client_focus instead of client_activate cuz client_activate does
332 stuff like switch desktops etc and I'm not interested in all that when
333 a window maps since its not based on an action from the user like
334 clicking a window to activate is. so keep the new window out of the way
336 if (activate) client_focus(self);
338 /* client_activate does this but we aret using it so we have to do it
340 if (screen_showing_desktop)
341 screen_show_desktop(FALSE);
343 /* add to client list/map */
344 client_list = g_list_append(client_list, self);
345 g_hash_table_insert(window_map, &self->window, self);
347 /* this has to happen after we're in the client_list */
348 screen_update_areas();
350 /* update the list hints */
353 keyboard_grab_for_client(self, TRUE);
354 mouse_grab_for_client(self, TRUE);
356 ob_debug("Managed window 0x%lx (%s)\n", window, self->class);
359 void client_unmanage_all()
361 while (client_list != NULL)
362 client_unmanage(client_list->data);
365 void client_unmanage(ObClient *self)
370 ob_debug("Unmanaging window: %lx (%s)\n", self->window, self->class);
372 g_assert(self != NULL);
374 keyboard_grab_for_client(self, FALSE);
375 mouse_grab_for_client(self, FALSE);
377 /* remove the window from our save set */
378 XChangeSaveSet(ob_display, self->window, SetModeDelete);
380 /* we dont want events no more */
381 XSelectInput(ob_display, self->window, NoEventMask);
383 frame_hide(self->frame);
385 client_list = g_list_remove(client_list, self);
386 stacking_remove(self);
387 g_hash_table_remove(window_map, &self->window);
389 /* update the focus lists */
390 focus_order_remove(self);
392 /* once the client is out of the list, update the struts to remove it's
394 screen_update_areas();
396 /* tell our parent(s) that we're gone */
397 if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
400 for (it = self->group->members; it; it = it->next)
401 if (it->data != self)
402 ((ObClient*)it->data)->transients =
403 g_slist_remove(((ObClient*)it->data)->transients, self);
404 } else if (self->transient_for) { /* transient of window */
405 self->transient_for->transients =
406 g_slist_remove(self->transient_for->transients, self);
409 /* tell our transients that we're gone */
410 for (it = self->transients; it != NULL; it = it->next) {
411 if (((ObClient*)it->data)->transient_for != OB_TRAN_GROUP) {
412 ((ObClient*)it->data)->transient_for = NULL;
413 client_calc_layer(it->data);
417 if (moveresize_client == self)
418 moveresize_end(TRUE);
420 /* menus can be associated with a client, so close any that are since
421 we are disappearing now */
422 menu_frame_hide_all_client(self);
424 if (focus_client == self) {
427 /* focus the last focused window on the desktop, and ignore enter
428 events from the unmap so it doesnt mess with the focus */
429 while (XCheckTypedEvent(ob_display, EnterNotify, &e));
430 client_unfocus(self);
433 /* remove from its group */
435 group_remove(self->group, self);
439 /* give the client its border back */
440 client_toggle_border(self, TRUE);
442 /* reparent the window out of the frame, and free the frame */
443 frame_release_client(self->frame, self);
446 if (ob_state() != OB_STATE_EXITING) {
447 /* these values should not be persisted across a window
449 PROP_ERASE(self->window, net_wm_desktop);
450 PROP_ERASE(self->window, net_wm_state);
451 PROP_ERASE(self->window, wm_state);
453 /* if we're left in an iconic state, the client wont be mapped. this is
454 bad, since we will no longer be managing the window on restart */
456 XMapWindow(ob_display, self->window);
460 ob_debug("Unmanaged window 0x%lx\n", self->window);
462 /* free all data allocated in the client struct */
463 g_slist_free(self->transients);
464 for (j = 0; j < self->nicons; ++j)
465 g_free(self->icons[j].data);
466 if (self->nicons > 0)
469 g_free(self->icon_title);
475 /* update the list hints */
479 static void client_restore_session_state(ObClient *self)
483 if (!(it = session_state_find(self)))
486 self->session = it->data;
488 RECT_SET(self->area, self->session->x, self->session->y,
489 self->session->w, self->session->h);
490 self->positioned = TRUE;
491 XResizeWindow(ob_display, self->window,
492 self->session->w, self->session->h);
494 self->desktop = (self->session->desktop == DESKTOP_ALL ?
495 self->session->desktop :
496 MIN(screen_num_desktops - 1, self->session->desktop));
497 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
499 self->shaded = self->session->shaded;
500 self->iconic = self->session->iconic;
501 self->skip_pager = self->session->skip_pager;
502 self->skip_taskbar = self->session->skip_taskbar;
503 self->fullscreen = self->session->fullscreen;
504 self->above = self->session->above;
505 self->below = self->session->below;
506 self->max_horz = self->session->max_horz;
507 self->max_vert = self->session->max_vert;
510 static void client_restore_session_stacking(ObClient *self)
514 if (!self->session) return;
516 it = g_list_find(session_saved_state, self->session);
517 for (it = g_list_previous(it); it; it = g_list_previous(it)) {
520 for (cit = client_list; cit; cit = g_list_next(cit))
521 if (session_state_cmp(it->data, cit->data))
524 client_calc_layer(self);
525 stacking_below(CLIENT_AS_WINDOW(self),
526 CLIENT_AS_WINDOW(cit->data));
532 void client_move_onscreen(ObClient *self, gboolean rude)
534 int x = self->area.x;
535 int y = self->area.y;
536 if (client_find_onscreen(self, &x, &y,
537 self->frame->area.width,
538 self->frame->area.height, rude)) {
539 client_configure(self, OB_CORNER_TOPLEFT, x, y,
540 self->area.width, self->area.height,
545 gboolean client_find_onscreen(ObClient *self, int *x, int *y, int w, int h,
549 int ox = *x, oy = *y;
551 frame_client_gravity(self->frame, x, y); /* get where the frame
554 /* XXX watch for xinerama dead areas */
556 a = screen_area(self->desktop);
557 if (!self->strut.right && *x >= a->x + a->width - 1)
558 *x = a->x + a->width - self->frame->area.width;
559 if (!self->strut.bottom && *y >= a->y + a->height - 1)
560 *y = a->y + a->height - self->frame->area.height;
561 if (!self->strut.left && *x + self->frame->area.width - 1 < a->x)
563 if (!self->strut.top && *y + self->frame->area.height - 1 < a->y)
567 /* this is my MOZILLA BITCHSLAP. oh ya it fucking feels good.
568 Java can suck it too. */
570 /* dont let windows map/move into the strut unless they
571 are bigger than the available area */
573 if (!self->strut.left && *x < a->x) *x = a->x;
574 if (!self->strut.right && *x + w > a->x + a->width)
575 *x = a->x + a->width - w;
577 if (h <= a->height) {
578 if (!self->strut.top && *y < a->y) *y = a->y;
579 if (!self->strut.bottom && *y + h > a->y + a->height)
580 *y = a->y + a->height - h;
584 frame_frame_gravity(self->frame, x, y); /* get where the client
587 return ox != *x || oy != *y;
590 static void client_toggle_border(ObClient *self, gboolean show)
592 /* adjust our idea of where the client is, based on its border. When the
593 border is removed, the client should now be considered to be in a
595 when re-adding the border to the client, the same operation needs to be
597 int oldx = self->area.x, oldy = self->area.y;
598 int x = oldx, y = oldy;
599 switch(self->gravity) {
601 case NorthWestGravity:
603 case SouthWestGravity:
605 case NorthEastGravity:
607 case SouthEastGravity:
608 if (show) x -= self->border_width * 2;
609 else x += self->border_width * 2;
616 if (show) x -= self->border_width;
617 else x += self->border_width;
620 switch(self->gravity) {
622 case NorthWestGravity:
624 case NorthEastGravity:
626 case SouthWestGravity:
628 case SouthEastGravity:
629 if (show) y -= self->border_width * 2;
630 else y += self->border_width * 2;
637 if (show) y -= self->border_width;
638 else y += self->border_width;
645 XSetWindowBorderWidth(ob_display, self->window, self->border_width);
647 /* move the client so it is back it the right spot _with_ its
649 if (x != oldx || y != oldy)
650 XMoveWindow(ob_display, self->window, x, y);
652 XSetWindowBorderWidth(ob_display, self->window, 0);
656 static void client_get_all(ObClient *self)
658 /* non-zero defaults */
659 self->title_count = 1;
660 self->wmstate = NormalState;
662 self->decorate = TRUE;
664 client_get_area(self);
665 client_update_transient_for(self);
666 client_update_wmhints(self);
667 client_get_desktop(self);
668 client_get_state(self);
669 client_get_shaped(self);
671 client_get_mwm_hints(self);
672 client_get_type(self);/* this can change the mwmhints for special cases */
674 client_update_protocols(self);
676 client_get_gravity(self); /* get the attribute gravity */
677 client_update_normal_hints(self); /* this may override the attribute
680 /* got the type, the mwmhints, the protocols, and the normal hints
681 (min/max sizes), so we're ready to set up the decorations/functions */
682 client_setup_decor_and_functions(self);
684 client_update_title(self);
685 client_update_class(self);
686 client_update_strut(self);
687 client_update_icons(self);
690 static void client_get_area(ObClient *self)
692 XWindowAttributes wattrib;
695 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
696 g_assert(ret != BadWindow);
698 RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
699 self->border_width = wattrib.border_width;
702 static void client_get_desktop(ObClient *self)
704 guint32 d = screen_num_desktops; /* an always-invalid value */
706 if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
707 if (d >= screen_num_desktops && d != DESKTOP_ALL)
708 self->desktop = screen_num_desktops - 1;
712 gboolean trdesk = FALSE;
714 if (self->transient_for) {
715 if (self->transient_for != OB_TRAN_GROUP) {
716 self->desktop = self->transient_for->desktop;
721 for (it = self->group->members; it; it = it->next)
722 if (it->data != self &&
723 !((ObClient*)it->data)->transient_for) {
724 self->desktop = ((ObClient*)it->data)->desktop;
731 /* defaults to the current desktop */
732 self->desktop = screen_desktop;
734 if (self->desktop != d) {
735 /* set the desktop hint, to make sure that it always exists */
736 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
740 static void client_get_state(ObClient *self)
745 self->modal = self->shaded = self->max_horz = self->max_vert =
746 self->fullscreen = self->above = self->below = self->iconic =
747 self->skip_taskbar = self->skip_pager = FALSE;
749 if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
751 for (i = 0; i < num; ++i) {
752 if (state[i] == prop_atoms.net_wm_state_modal)
754 else if (state[i] == prop_atoms.net_wm_state_shaded)
756 else if (state[i] == prop_atoms.net_wm_state_hidden)
758 else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
759 self->skip_taskbar = TRUE;
760 else if (state[i] == prop_atoms.net_wm_state_skip_pager)
761 self->skip_pager = TRUE;
762 else if (state[i] == prop_atoms.net_wm_state_fullscreen)
763 self->fullscreen = TRUE;
764 else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
765 self->max_vert = TRUE;
766 else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
767 self->max_horz = TRUE;
768 else if (state[i] == prop_atoms.net_wm_state_above)
770 else if (state[i] == prop_atoms.net_wm_state_below)
778 static void client_get_shaped(ObClient *self)
780 self->shaped = FALSE;
782 if (extensions_shape) {
787 XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
789 XShapeQueryExtents(ob_display, self->window, &s, &foo,
790 &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
792 self->shaped = (s != 0);
797 void client_update_transient_for(ObClient *self)
800 ObClient *target = NULL;
802 if (XGetTransientForHint(ob_display, self->window, &t)) {
803 self->transient = TRUE;
804 if (t != self->window) { /* cant be transient to itself! */
805 target = g_hash_table_lookup(window_map, &t);
806 /* if this happens then we need to check for it*/
807 g_assert(target != self);
808 g_assert(!target || WINDOW_IS_CLIENT(target));
810 if (!target && self->group) {
811 /* not transient to a client, see if it is transient for a
813 if (t == self->group->leader ||
815 t == RootWindow(ob_display, ob_screen)) {
816 /* window is a transient for its group! */
817 target = OB_TRAN_GROUP;
822 self->transient = FALSE;
824 /* if anything has changed... */
825 if (target != self->transient_for) {
826 if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
829 /* remove from old parents */
830 for (it = self->group->members; it; it = g_slist_next(it)) {
831 ObClient *c = it->data;
832 if (c != self && !c->transient_for)
833 c->transients = g_slist_remove(c->transients, self);
835 } else if (self->transient_for != NULL) { /* transient of window */
836 /* remove from old parent */
837 self->transient_for->transients =
838 g_slist_remove(self->transient_for->transients, self);
840 self->transient_for = target;
841 if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
844 /* add to new parents */
845 for (it = self->group->members; it; it = g_slist_next(it)) {
846 ObClient *c = it->data;
847 if (c != self && !c->transient_for)
848 c->transients = g_slist_append(c->transients, self);
851 /* remove all transients which are in the group, that causes
852 circlular pointer hell of doom */
853 for (it = self->group->members; it; it = g_slist_next(it)) {
855 for (sit = self->transients; sit; sit = next) {
856 next = g_slist_next(sit);
857 if (sit->data == it->data)
859 g_slist_delete_link(self->transients, sit);
862 } else if (self->transient_for != NULL) { /* transient of window */
863 /* add to new parent */
864 self->transient_for->transients =
865 g_slist_append(self->transient_for->transients, self);
870 static void client_get_mwm_hints(ObClient *self)
875 self->mwmhints.flags = 0; /* default to none */
877 if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
879 if (num >= OB_MWM_ELEMENTS) {
880 self->mwmhints.flags = hints[0];
881 self->mwmhints.functions = hints[1];
882 self->mwmhints.decorations = hints[2];
888 void client_get_type(ObClient *self)
895 if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
896 /* use the first value that we know about in the array */
897 for (i = 0; i < num; ++i) {
898 if (val[i] == prop_atoms.net_wm_window_type_desktop)
899 self->type = OB_CLIENT_TYPE_DESKTOP;
900 else if (val[i] == prop_atoms.net_wm_window_type_dock)
901 self->type = OB_CLIENT_TYPE_DOCK;
902 else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
903 self->type = OB_CLIENT_TYPE_TOOLBAR;
904 else if (val[i] == prop_atoms.net_wm_window_type_menu)
905 self->type = OB_CLIENT_TYPE_MENU;
906 else if (val[i] == prop_atoms.net_wm_window_type_utility)
907 self->type = OB_CLIENT_TYPE_UTILITY;
908 else if (val[i] == prop_atoms.net_wm_window_type_splash)
909 self->type = OB_CLIENT_TYPE_SPLASH;
910 else if (val[i] == prop_atoms.net_wm_window_type_dialog)
911 self->type = OB_CLIENT_TYPE_DIALOG;
912 else if (val[i] == prop_atoms.net_wm_window_type_normal)
913 self->type = OB_CLIENT_TYPE_NORMAL;
914 else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
915 /* prevent this window from getting any decor or
917 self->mwmhints.flags &= (OB_MWM_FLAG_FUNCTIONS |
918 OB_MWM_FLAG_DECORATIONS);
919 self->mwmhints.decorations = 0;
920 self->mwmhints.functions = 0;
922 if (self->type != (ObClientType) -1)
923 break; /* grab the first legit type */
928 if (self->type == (ObClientType) -1) {
929 /*the window type hint was not set, which means we either classify
930 ourself as a normal window or a dialog, depending on if we are a
933 self->type = OB_CLIENT_TYPE_DIALOG;
935 self->type = OB_CLIENT_TYPE_NORMAL;
939 void client_update_protocols(ObClient *self)
944 self->focus_notify = FALSE;
945 self->delete_window = FALSE;
947 if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
948 for (i = 0; i < num_return; ++i) {
949 if (proto[i] == prop_atoms.wm_delete_window) {
950 /* this means we can request the window to close */
951 self->delete_window = TRUE;
952 } else if (proto[i] == prop_atoms.wm_take_focus)
953 /* if this protocol is requested, then the window will be
954 notified whenever we want it to receive focus */
955 self->focus_notify = TRUE;
961 static void client_get_gravity(ObClient *self)
963 XWindowAttributes wattrib;
966 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
967 g_assert(ret != BadWindow);
968 self->gravity = wattrib.win_gravity;
971 void client_update_normal_hints(ObClient *self)
975 int oldgravity = self->gravity;
978 self->min_ratio = 0.0f;
979 self->max_ratio = 0.0f;
980 SIZE_SET(self->size_inc, 1, 1);
981 SIZE_SET(self->base_size, 0, 0);
982 SIZE_SET(self->min_size, 0, 0);
983 SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
985 /* get the hints from the window */
986 if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
987 self->positioned = !!(size.flags & (PPosition|USPosition));
989 if (size.flags & PWinGravity) {
990 self->gravity = size.win_gravity;
992 /* if the client has a frame, i.e. has already been mapped and
993 is changing its gravity */
994 if (self->frame && self->gravity != oldgravity) {
995 /* move our idea of the client's position based on its new
997 self->area.x = self->frame->area.x;
998 self->area.y = self->frame->area.y;
999 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
1003 if (size.flags & PAspect) {
1004 if (size.min_aspect.y)
1005 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
1006 if (size.max_aspect.y)
1007 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
1010 if (size.flags & PMinSize)
1011 SIZE_SET(self->min_size, size.min_width, size.min_height);
1013 if (size.flags & PMaxSize)
1014 SIZE_SET(self->max_size, size.max_width, size.max_height);
1016 if (size.flags & PBaseSize)
1017 SIZE_SET(self->base_size, size.base_width, size.base_height);
1019 if (size.flags & PResizeInc)
1020 SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
1024 void client_setup_decor_and_functions(ObClient *self)
1026 /* start with everything (cept fullscreen) */
1027 self->decorations = (OB_FRAME_DECOR_TITLEBAR |
1028 OB_FRAME_DECOR_HANDLE |
1029 OB_FRAME_DECOR_GRIPS |
1030 OB_FRAME_DECOR_BORDER |
1031 OB_FRAME_DECOR_ICON |
1032 OB_FRAME_DECOR_ALLDESKTOPS |
1033 OB_FRAME_DECOR_ICONIFY |
1034 OB_FRAME_DECOR_MAXIMIZE |
1035 OB_FRAME_DECOR_SHADE);
1036 self->functions = (OB_CLIENT_FUNC_RESIZE |
1037 OB_CLIENT_FUNC_MOVE |
1038 OB_CLIENT_FUNC_ICONIFY |
1039 OB_CLIENT_FUNC_MAXIMIZE |
1040 OB_CLIENT_FUNC_SHADE);
1041 if (self->delete_window) {
1042 self->functions |= OB_CLIENT_FUNC_CLOSE;
1043 self->decorations |= OB_FRAME_DECOR_CLOSE;
1046 if (!(self->min_size.width < self->max_size.width ||
1047 self->min_size.height < self->max_size.height))
1048 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1050 switch (self->type) {
1051 case OB_CLIENT_TYPE_NORMAL:
1052 /* normal windows retain all of the possible decorations and
1053 functionality, and are the only windows that you can fullscreen */
1054 self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1057 case OB_CLIENT_TYPE_DIALOG:
1058 case OB_CLIENT_TYPE_UTILITY:
1059 /* these windows cannot be maximized */
1060 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1063 case OB_CLIENT_TYPE_MENU:
1064 case OB_CLIENT_TYPE_TOOLBAR:
1065 /* these windows get less functionality */
1066 self->functions &= ~(OB_CLIENT_FUNC_ICONIFY | OB_CLIENT_FUNC_RESIZE);
1069 case OB_CLIENT_TYPE_DESKTOP:
1070 case OB_CLIENT_TYPE_DOCK:
1071 case OB_CLIENT_TYPE_SPLASH:
1072 /* none of these windows are manipulated by the window manager */
1073 self->decorations = 0;
1074 self->functions = 0;
1078 /* Mwm Hints are applied subtractively to what has already been chosen for
1079 decor and functionality */
1080 if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
1081 if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
1082 if (! ((self->mwmhints.decorations & OB_MWM_DECOR_HANDLE) ||
1083 (self->mwmhints.decorations & OB_MWM_DECOR_TITLE)))
1084 /* if the mwm hints request no handle or title, then all
1085 decorations are disabled */
1086 self->decorations = 0;
1090 if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
1091 if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
1092 if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
1093 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1094 if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
1095 self->functions &= ~OB_CLIENT_FUNC_MOVE;
1096 /* dont let mwm hints kill any buttons
1097 if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
1098 self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
1099 if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
1100 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1102 /* dont let mwm hints kill the close button
1103 if (! (self->mwmhints.functions & MwmFunc_Close))
1104 self->functions &= ~OB_CLIENT_FUNC_CLOSE; */
1108 if (!(self->functions & OB_CLIENT_FUNC_SHADE))
1109 self->decorations &= ~OB_FRAME_DECOR_SHADE;
1110 if (!(self->functions & OB_CLIENT_FUNC_ICONIFY))
1111 self->decorations &= ~OB_FRAME_DECOR_ICONIFY;
1112 if (!(self->functions & OB_CLIENT_FUNC_RESIZE))
1113 self->decorations &= ~OB_FRAME_DECOR_GRIPS;
1115 /* can't maximize without moving/resizing */
1116 if (!((self->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
1117 (self->functions & OB_CLIENT_FUNC_MOVE) &&
1118 (self->functions & OB_CLIENT_FUNC_RESIZE))) {
1119 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1120 self->decorations &= ~OB_FRAME_DECOR_MAXIMIZE;
1123 /* kill the handle on fully maxed windows */
1124 if (self->max_vert && self->max_horz)
1125 self->decorations &= ~OB_FRAME_DECOR_HANDLE;
1127 /* finally, the user can have requested no decorations, which overrides
1129 if (!self->decorate)
1130 self->decorations = OB_FRAME_DECOR_BORDER;
1132 /* if we don't have a titlebar, then we cannot shade! */
1133 if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1134 self->functions &= ~OB_CLIENT_FUNC_SHADE;
1136 /* now we need to check against rules for the client's current state */
1137 if (self->fullscreen) {
1138 self->functions &= (OB_CLIENT_FUNC_CLOSE |
1139 OB_CLIENT_FUNC_FULLSCREEN |
1140 OB_CLIENT_FUNC_ICONIFY);
1141 self->decorations = 0;
1144 client_change_allowed_actions(self);
1147 /* adjust the client's decorations, etc. */
1148 client_reconfigure(self);
1150 /* this makes sure that these windows appear on all desktops */
1151 if (self->type == OB_CLIENT_TYPE_DESKTOP &&
1152 self->desktop != DESKTOP_ALL)
1154 self->desktop = DESKTOP_ALL;
1159 static void client_change_allowed_actions(ObClient *self)
1164 /* desktop windows are kept on all desktops */
1165 if (self->type != OB_CLIENT_TYPE_DESKTOP)
1166 actions[num++] = prop_atoms.net_wm_action_change_desktop;
1168 if (self->functions & OB_CLIENT_FUNC_SHADE)
1169 actions[num++] = prop_atoms.net_wm_action_shade;
1170 if (self->functions & OB_CLIENT_FUNC_CLOSE)
1171 actions[num++] = prop_atoms.net_wm_action_close;
1172 if (self->functions & OB_CLIENT_FUNC_MOVE)
1173 actions[num++] = prop_atoms.net_wm_action_move;
1174 if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1175 actions[num++] = prop_atoms.net_wm_action_minimize;
1176 if (self->functions & OB_CLIENT_FUNC_RESIZE)
1177 actions[num++] = prop_atoms.net_wm_action_resize;
1178 if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1179 actions[num++] = prop_atoms.net_wm_action_fullscreen;
1180 if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1181 actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1182 actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1185 PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1187 /* make sure the window isn't breaking any rules now */
1189 if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1190 if (self->frame) client_shade(self, FALSE);
1191 else self->shaded = FALSE;
1193 if (!(self->functions & OB_CLIENT_FUNC_ICONIFY) && self->iconic) {
1194 if (self->frame) client_iconify(self, FALSE, TRUE);
1195 else self->iconic = FALSE;
1197 if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1198 if (self->frame) client_fullscreen(self, FALSE, TRUE);
1199 else self->fullscreen = FALSE;
1201 if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
1203 if (self->frame) client_maximize(self, FALSE, 0, TRUE);
1204 else self->max_vert = self->max_horz = FALSE;
1208 void client_reconfigure(ObClient *self)
1210 /* by making this pass FALSE for user, we avoid the emacs event storm where
1211 every configurenotify causes an update in its normal hints, i think this
1212 is generally what we want anyways... */
1213 client_configure(self, OB_CORNER_TOPLEFT, self->area.x, self->area.y,
1214 self->area.width, self->area.height, FALSE, TRUE);
1217 void client_update_wmhints(ObClient *self)
1220 gboolean ur = FALSE;
1223 /* assume a window takes input if it doesnt specify */
1224 self->can_focus = TRUE;
1226 if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1227 if (hints->flags & InputHint)
1228 self->can_focus = hints->input;
1230 /* only do this when first managing the window *AND* when we aren't
1232 if (ob_state() != OB_STATE_STARTING && self->frame == NULL)
1233 if (hints->flags & StateHint)
1234 self->iconic = hints->initial_state == IconicState;
1236 if (hints->flags & XUrgencyHint)
1239 if (!(hints->flags & WindowGroupHint))
1240 hints->window_group = None;
1242 /* did the group state change? */
1243 if (hints->window_group !=
1244 (self->group ? self->group->leader : None)) {
1245 /* remove from the old group if there was one */
1246 if (self->group != NULL) {
1247 /* remove transients of the group */
1248 for (it = self->group->members; it; it = it->next)
1249 self->transients = g_slist_remove(self->transients,
1251 group_remove(self->group, self);
1254 if (hints->window_group != None) {
1255 self->group = group_add(hints->window_group, self);
1257 /* i can only have transients from the group if i am not
1259 if (!self->transient_for) {
1260 /* add other transients of the group that are already
1262 for (it = self->group->members; it; it = it->next) {
1263 ObClient *c = it->data;
1264 if (c != self && c->transient_for == OB_TRAN_GROUP)
1266 g_slist_append(self->transients, c);
1271 /* because the self->transient flag wont change from this call,
1272 we don't need to update the window's type and such, only its
1273 transient_for, and the transients lists of other windows in
1274 the group may be affected */
1275 client_update_transient_for(self);
1278 /* the WM_HINTS can contain an icon */
1279 client_update_icons(self);
1284 if (ur != self->urgent) {
1286 ob_debug("Urgent Hint for 0x%lx: %s\n", self->window,
1288 /* fire the urgent callback if we're mapped, otherwise, wait until
1289 after we're mapped */
1296 void client_update_title(ObClient *self)
1302 gboolean read_title;
1304 g_free(self->title);
1307 if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1308 /* try old x stuff */
1309 if (!PROP_GETS(self->window, wm_name, locale, &data))
1310 data = g_strdup("Unnamed Window");
1312 /* look for duplicates and append a number */
1314 for (it = client_list; it; it = it->next)
1315 if (it->data != self) {
1316 ObClient *c = it->data;
1317 if (0 == strncmp(c->title, data, strlen(data)))
1318 nums |= 1 << c->title_count;
1320 /* find first free number */
1321 for (i = 1; i <= 32; ++i)
1322 if (!(nums & (1 << i))) {
1323 if (self->title_count == 1 || i == 1)
1324 self->title_count = i;
1327 /* dont display the number for the first window */
1328 if (self->title_count > 1) {
1329 char *vdata, *ndata;
1330 ndata = g_strdup_printf(" - [%u]", self->title_count);
1331 vdata = g_strconcat(data, ndata, NULL);
1337 PROP_SETS(self->window, net_wm_visible_name, data);
1342 frame_adjust_title(self->frame);
1344 /* update the icon title */
1346 g_free(self->icon_title);
1350 if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1351 /* try old x stuff */
1352 if (!PROP_GETS(self->window, wm_icon_name, locale, &data)) {
1353 data = g_strdup(self->title);
1357 /* append the title count, dont display the number for the first window */
1358 if (read_title && self->title_count > 1) {
1359 char *vdata, *ndata;
1360 ndata = g_strdup_printf(" - [%u]", self->title_count);
1361 vdata = g_strconcat(data, ndata, NULL);
1367 PROP_SETS(self->window, net_wm_visible_icon_name, data);
1369 self->icon_title = data;
1372 void client_update_class(ObClient *self)
1377 if (self->name) g_free(self->name);
1378 if (self->class) g_free(self->class);
1379 if (self->role) g_free(self->role);
1381 self->name = self->class = self->role = NULL;
1383 if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1385 self->name = g_strdup(data[0]);
1387 self->class = g_strdup(data[1]);
1392 if (PROP_GETS(self->window, wm_window_role, locale, &s))
1393 self->role = g_strdup(s);
1395 if (self->name == NULL) self->name = g_strdup("");
1396 if (self->class == NULL) self->class = g_strdup("");
1397 if (self->role == NULL) self->role = g_strdup("");
1400 void client_update_strut(ObClient *self)
1404 gboolean got = FALSE;
1407 if (PROP_GETA32(self->window, net_wm_strut_partial, cardinal,
1411 STRUT_PARTIAL_SET(strut,
1412 data[0], data[2], data[1], data[3],
1413 data[4], data[5], data[8], data[9],
1414 data[6], data[7], data[10], data[11]);
1420 PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1423 STRUT_PARTIAL_SET(strut,
1424 data[0], data[2], data[1], data[3],
1425 0, 0, 0, 0, 0, 0, 0, 0);
1431 STRUT_PARTIAL_SET(strut, 0, 0, 0, 0,
1432 0, 0, 0, 0, 0, 0, 0, 0);
1434 if (!STRUT_EQUAL(strut, self->strut)) {
1435 self->strut = strut;
1437 /* updating here is pointless while we're being mapped cuz we're not in
1438 the client list yet */
1440 screen_update_areas();
1444 void client_update_icons(ObClient *self)
1450 for (i = 0; i < self->nicons; ++i)
1451 g_free(self->icons[i].data);
1452 if (self->nicons > 0)
1453 g_free(self->icons);
1456 if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1457 /* figure out how many valid icons are in here */
1459 while (num - i > 2) {
1463 if (i > num || w*h == 0) break;
1467 self->icons = g_new(ObClientIcon, self->nicons);
1469 /* store the icons */
1471 for (j = 0; j < self->nicons; ++j) {
1474 w = self->icons[j].width = data[i++];
1475 h = self->icons[j].height = data[i++];
1477 if (w*h == 0) continue;
1479 self->icons[j].data = g_new(RrPixel32, w * h);
1480 for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1485 self->icons[j].data[t] =
1486 (((data[i] >> 24) & 0xff) << RrDefaultAlphaOffset) +
1487 (((data[i] >> 16) & 0xff) << RrDefaultRedOffset) +
1488 (((data[i] >> 8) & 0xff) << RrDefaultGreenOffset) +
1489 (((data[i] >> 0) & 0xff) << RrDefaultBlueOffset);
1495 } else if (PROP_GETA32(self->window, kwm_win_icon,
1496 kwm_win_icon, &data, &num)) {
1499 self->icons = g_new(ObClientIcon, self->nicons);
1500 xerror_set_ignore(TRUE);
1501 if (!RrPixmapToRGBA(ob_rr_inst,
1503 &self->icons[self->nicons-1].width,
1504 &self->icons[self->nicons-1].height,
1505 &self->icons[self->nicons-1].data)) {
1506 g_free(&self->icons[self->nicons-1]);
1509 xerror_set_ignore(FALSE);
1515 if ((hints = XGetWMHints(ob_display, self->window))) {
1516 if (hints->flags & IconPixmapHint) {
1518 self->icons = g_new(ObClientIcon, self->nicons);
1519 xerror_set_ignore(TRUE);
1520 if (!RrPixmapToRGBA(ob_rr_inst,
1522 (hints->flags & IconMaskHint ?
1523 hints->icon_mask : None),
1524 &self->icons[self->nicons-1].width,
1525 &self->icons[self->nicons-1].height,
1526 &self->icons[self->nicons-1].data)){
1527 g_free(&self->icons[self->nicons-1]);
1530 xerror_set_ignore(FALSE);
1537 frame_adjust_icon(self->frame);
1540 static void client_change_state(ObClient *self)
1543 guint32 netstate[10];
1546 state[0] = self->wmstate;
1548 PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1552 netstate[num++] = prop_atoms.net_wm_state_modal;
1554 netstate[num++] = prop_atoms.net_wm_state_shaded;
1556 netstate[num++] = prop_atoms.net_wm_state_hidden;
1557 if (self->skip_taskbar)
1558 netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1559 if (self->skip_pager)
1560 netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1561 if (self->fullscreen)
1562 netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1564 netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1566 netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1568 netstate[num++] = prop_atoms.net_wm_state_above;
1570 netstate[num++] = prop_atoms.net_wm_state_below;
1571 PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1573 client_calc_layer(self);
1576 frame_adjust_state(self->frame);
1579 ObClient *client_search_focus_tree(ObClient *self)
1584 for (it = self->transients; it != NULL; it = it->next) {
1585 if (client_focused(it->data)) return it->data;
1586 if ((ret = client_search_focus_tree(it->data))) return ret;
1591 ObClient *client_search_focus_tree_full(ObClient *self)
1593 if (self->transient_for) {
1594 if (self->transient_for != OB_TRAN_GROUP) {
1595 return client_search_focus_tree_full(self->transient_for);
1598 gboolean recursed = FALSE;
1600 for (it = self->group->members; it; it = it->next)
1601 if (!((ObClient*)it->data)->transient_for) {
1603 if ((c = client_search_focus_tree_full(it->data)))
1612 /* this function checks the whole tree, the client_search_focus_tree~
1613 does not, so we need to check this window */
1614 if (client_focused(self))
1616 return client_search_focus_tree(self);
1619 static ObStackingLayer calc_layer(ObClient *self)
1623 if (self->fullscreen) l = OB_STACKING_LAYER_FULLSCREEN;
1624 else if (self->type == OB_CLIENT_TYPE_DESKTOP)
1625 l = OB_STACKING_LAYER_DESKTOP;
1626 else if (self->type == OB_CLIENT_TYPE_DOCK) {
1627 if (!self->below) l = OB_STACKING_LAYER_TOP;
1628 else l = OB_STACKING_LAYER_NORMAL;
1630 else if (self->above) l = OB_STACKING_LAYER_ABOVE;
1631 else if (self->below) l = OB_STACKING_LAYER_BELOW;
1632 else l = OB_STACKING_LAYER_NORMAL;
1637 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
1638 ObStackingLayer l, gboolean raised)
1640 ObStackingLayer old, own;
1644 own = calc_layer(self);
1645 self->layer = l > own ? l : own;
1647 for (it = self->transients; it; it = it->next)
1648 client_calc_layer_recursive(it->data, orig,
1649 l, raised ? raised : l != old);
1651 if (!raised && l != old)
1652 if (orig->frame) { /* only restack if the original window is managed */
1653 /* XXX add_non_intrusive ever? */
1654 stacking_remove(CLIENT_AS_WINDOW(self));
1655 stacking_add(CLIENT_AS_WINDOW(self));
1659 void client_calc_layer(ObClient *self)
1666 /* transients take on the layer of their parents */
1667 self = client_search_top_transient(self);
1669 l = calc_layer(self);
1671 client_calc_layer_recursive(self, orig, l, FALSE);
1674 gboolean client_should_show(ObClient *self)
1676 if (self->iconic) return FALSE;
1677 else if (!(self->desktop == screen_desktop ||
1678 self->desktop == DESKTOP_ALL)) return FALSE;
1679 else if (client_normal(self) && screen_showing_desktop) return FALSE;
1684 static void client_showhide(ObClient *self)
1687 if (client_should_show(self))
1688 frame_show(self->frame);
1690 frame_hide(self->frame);
1693 gboolean client_normal(ObClient *self) {
1694 return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
1695 self->type == OB_CLIENT_TYPE_DOCK ||
1696 self->type == OB_CLIENT_TYPE_SPLASH);
1699 static void client_apply_startup_state(ObClient *self)
1701 /* these are in a carefully crafted order.. */
1704 self->iconic = FALSE;
1705 client_iconify(self, TRUE, FALSE);
1707 if (self->fullscreen) {
1708 self->fullscreen = FALSE;
1709 client_fullscreen(self, TRUE, FALSE);
1712 self->shaded = FALSE;
1713 client_shade(self, TRUE);
1718 if (self->max_vert && self->max_horz) {
1719 self->max_vert = self->max_horz = FALSE;
1720 client_maximize(self, TRUE, 0, FALSE);
1721 } else if (self->max_vert) {
1722 self->max_vert = FALSE;
1723 client_maximize(self, TRUE, 2, FALSE);
1724 } else if (self->max_horz) {
1725 self->max_horz = FALSE;
1726 client_maximize(self, TRUE, 1, FALSE);
1729 /* nothing to do for the other states:
1738 void client_configure_full(ObClient *self, ObCorner anchor,
1739 int x, int y, int w, int h,
1740 gboolean user, gboolean final,
1741 gboolean force_reply)
1743 gboolean moved = FALSE, resized = FALSE;
1744 guint fdecor = self->frame->decorations;
1745 gboolean fhorz = self->frame->max_horz;
1747 /* make the frame recalculate its dimentions n shit without changing
1748 anything visible for real, this way the constraints below can work with
1749 the updated frame dimensions. */
1750 frame_adjust_area(self->frame, TRUE, TRUE, TRUE);
1752 /* gets the frame's position */
1753 frame_client_gravity(self->frame, &x, &y);
1755 /* these positions are frame positions, not client positions */
1757 /* set the size and position if fullscreen */
1758 if (self->fullscreen) {
1761 XF86VidModeModeLine mode;
1766 i = client_monitor(self);
1767 a = screen_physical_area_monitor(i);
1770 if (i == 0 && /* primary head */
1771 extensions_vidmode &&
1772 XF86VidModeGetViewPort(ob_display, ob_screen, &x, &y) &&
1773 /* get the mode last so the mode.privsize isnt freed incorrectly */
1774 XF86VidModeGetModeLine(ob_display, ob_screen, &dot, &mode)) {
1779 if (mode.privsize) XFree(mode.private);
1789 user = FALSE; /* ignore that increment etc shit when in fullscreen */
1793 a = screen_area_monitor(self->desktop, client_monitor(self));
1795 /* set the size and position if maximized */
1796 if (self->max_horz) {
1798 w = a->width - self->frame->size.left - self->frame->size.right;
1800 if (self->max_vert) {
1802 h = a->height - self->frame->size.top - self->frame->size.bottom;
1806 /* gets the client's position */
1807 frame_frame_gravity(self->frame, &x, &y);
1809 /* these override the above states! if you cant move you can't move! */
1811 if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
1815 if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
1816 w = self->area.width;
1817 h = self->area.height;
1821 if (!(w == self->area.width && h == self->area.height)) {
1822 int basew, baseh, minw, minh;
1825 /* base size is substituted with min size if not specified */
1826 if (self->base_size.width || self->base_size.height) {
1827 basew = self->base_size.width;
1828 baseh = self->base_size.height;
1830 basew = self->min_size.width;
1831 baseh = self->min_size.height;
1833 /* min size is substituted with base size if not specified */
1834 if (self->min_size.width || self->min_size.height) {
1835 minw = self->min_size.width;
1836 minh = self->min_size.height;
1838 minw = self->base_size.width;
1839 minh = self->base_size.height;
1842 /* for interactive resizing. have to move half an increment in each
1845 /* how far we are towards the next size inc */
1846 mw = (w - basew) % self->size_inc.width;
1847 mh = (h - baseh) % self->size_inc.height;
1849 aw = self->size_inc.width / 2;
1850 ah = self->size_inc.height / 2;
1851 /* don't let us move into a new size increment */
1852 if (mw + aw >= self->size_inc.width)
1853 aw = self->size_inc.width - mw - 1;
1854 if (mh + ah >= self->size_inc.height)
1855 ah = self->size_inc.height - mh - 1;
1859 /* if this is a user-requested resize, then check against min/max
1862 /* smaller than min size or bigger than max size? */
1863 if (w > self->max_size.width) w = self->max_size.width;
1864 if (w < minw) w = minw;
1865 if (h > self->max_size.height) h = self->max_size.height;
1866 if (h < minh) h = minh;
1871 /* keep to the increments */
1872 w /= self->size_inc.width;
1873 h /= self->size_inc.height;
1875 /* you cannot resize to nothing */
1876 if (basew + w < 1) w = 1 - basew;
1877 if (baseh + h < 1) h = 1 - baseh;
1879 /* store the logical size */
1880 SIZE_SET(self->logical_size,
1881 self->size_inc.width > 1 ? w : w + basew,
1882 self->size_inc.height > 1 ? h : h + baseh);
1884 w *= self->size_inc.width;
1885 h *= self->size_inc.height;
1890 /* adjust the height to match the width for the aspect ratios.
1891 for this, min size is not substituted for base size ever. */
1892 w -= self->base_size.width;
1893 h -= self->base_size.height;
1895 if (self->min_ratio)
1896 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1897 if (self->max_ratio)
1898 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1900 w += self->base_size.width;
1901 h += self->base_size.height;
1905 case OB_CORNER_TOPLEFT:
1907 case OB_CORNER_TOPRIGHT:
1908 x -= w - self->area.width;
1910 case OB_CORNER_BOTTOMLEFT:
1911 y -= h - self->area.height;
1913 case OB_CORNER_BOTTOMRIGHT:
1914 x -= w - self->area.width;
1915 y -= h - self->area.height;
1919 moved = x != self->area.x || y != self->area.y;
1920 resized = w != self->area.width || h != self->area.height;
1922 RECT_SET(self->area, x, y, w, h);
1924 /* for app-requested resizes, always resize if 'resized' is true.
1925 for user-requested ones, only resize if final is true, or when
1926 resizing in redraw mode */
1927 if ((!user && resized) ||
1928 (user && (final || (resized && config_redraw_resize))))
1929 XResizeWindow(ob_display, self->window, w, h);
1931 /* move/resize the frame to match the request */
1933 if (self->decorations != fdecor || self->max_horz != fhorz)
1934 moved = resized = TRUE;
1936 if (moved || resized)
1937 frame_adjust_area(self->frame, moved, resized, FALSE);
1939 if (!resized && (force_reply || ((!user && moved) || (user && final))))
1942 event.type = ConfigureNotify;
1943 event.xconfigure.display = ob_display;
1944 event.xconfigure.event = self->window;
1945 event.xconfigure.window = self->window;
1947 /* root window real coords */
1948 event.xconfigure.x = self->frame->area.x + self->frame->size.left -
1950 event.xconfigure.y = self->frame->area.y + self->frame->size.top -
1952 event.xconfigure.width = w;
1953 event.xconfigure.height = h;
1954 event.xconfigure.border_width = 0;
1955 event.xconfigure.above = self->frame->plate;
1956 event.xconfigure.override_redirect = FALSE;
1957 XSendEvent(event.xconfigure.display, event.xconfigure.window,
1958 FALSE, StructureNotifyMask, &event);
1963 void client_fullscreen(ObClient *self, gboolean fs, gboolean savearea)
1967 if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
1968 self->fullscreen == fs) return; /* already done */
1970 self->fullscreen = fs;
1971 client_change_state(self); /* change the state hints on the client,
1972 and adjust out layer/stacking */
1976 guint32 dimensions[4];
1977 dimensions[0] = self->area.x;
1978 dimensions[1] = self->area.y;
1979 dimensions[2] = self->area.width;
1980 dimensions[3] = self->area.height;
1982 PROP_SETA32(self->window, openbox_premax, cardinal,
1986 /* these are not actually used cuz client_configure will set them
1987 as appropriate when the window is fullscreened */
1994 /* pick some fallbacks... */
1995 a = screen_area_monitor(self->desktop, 0);
1996 x = a->x + a->width / 4;
1997 y = a->y + a->height / 4;
2001 if (PROP_GETA32(self->window, openbox_premax, cardinal,
2002 (guint32**)&dimensions, &num)) {
2013 client_setup_decor_and_functions(self);
2015 client_configure(self, OB_CORNER_TOPLEFT, x, y, w, h, TRUE, TRUE);
2017 /* try focus us when we go into fullscreen mode */
2021 static void client_iconify_recursive(ObClient *self,
2022 gboolean iconic, gboolean curdesk)
2025 gboolean changed = FALSE;
2028 if (self->iconic != iconic) {
2029 ob_debug("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"),
2032 self->iconic = iconic;
2035 if (self->functions & OB_CLIENT_FUNC_ICONIFY) {
2036 self->wmstate = IconicState;
2037 self->ignore_unmaps++;
2038 /* we unmap the client itself so that we can get MapRequest
2039 events, and because the ICCCM tells us to! */
2040 XUnmapWindow(ob_display, self->window);
2042 /* update the focus lists.. iconic windows go to the bottom of
2043 the list, put the new iconic window at the 'top of the
2045 focus_order_to_top(self);
2051 client_set_desktop(self, screen_desktop, FALSE);
2052 self->wmstate = self->shaded ? IconicState : NormalState;
2053 XMapWindow(ob_display, self->window);
2055 /* this puts it after the current focused window */
2056 focus_order_remove(self);
2057 focus_order_add_new(self);
2059 /* this is here cuz with the VIDMODE extension, the viewport can
2060 change while a fullscreen window is iconic, and when it
2061 uniconifies, it would be nice if it did so to the new position
2063 client_reconfigure(self);
2070 client_change_state(self);
2071 client_showhide(self);
2072 screen_update_areas();
2075 /* iconify all transients */
2076 for (it = self->transients; it != NULL; it = it->next)
2077 if (it->data != self) client_iconify_recursive(it->data,
2081 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk)
2083 /* move up the transient chain as far as possible first */
2084 self = client_search_top_transient(self);
2086 client_iconify_recursive(client_search_top_transient(self),
2090 void client_maximize(ObClient *self, gboolean max, int dir, gboolean savearea)
2094 g_assert(dir == 0 || dir == 1 || dir == 2);
2095 if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE)) return; /* can't */
2097 /* check if already done */
2099 if (dir == 0 && self->max_horz && self->max_vert) return;
2100 if (dir == 1 && self->max_horz) return;
2101 if (dir == 2 && self->max_vert) return;
2103 if (dir == 0 && !self->max_horz && !self->max_vert) return;
2104 if (dir == 1 && !self->max_horz) return;
2105 if (dir == 2 && !self->max_vert) return;
2108 /* work with the frame's coords */
2109 x = self->frame->area.x;
2110 y = self->frame->area.y;
2111 w = self->area.width;
2112 h = self->area.height;
2116 gint32 dimensions[4];
2125 /* get the property off the window and use it for the dimensions
2126 we are already maxed on */
2127 if (PROP_GETA32(self->window, openbox_premax, cardinal,
2128 (guint32**)&readdim, &num)) {
2130 if (self->max_horz) {
2131 dimensions[0] = readdim[0];
2132 dimensions[2] = readdim[2];
2134 if (self->max_vert) {
2135 dimensions[1] = readdim[1];
2136 dimensions[3] = readdim[3];
2142 PROP_SETA32(self->window, openbox_premax, cardinal,
2143 (guint32*)dimensions, 4);
2150 /* pick some fallbacks... */
2151 a = screen_area_monitor(self->desktop, 0);
2152 if (dir == 0 || dir == 1) { /* horz */
2153 x = a->x + a->width / 4;
2156 if (dir == 0 || dir == 2) { /* vert */
2157 y = a->y + a->height / 4;
2161 if (PROP_GETA32(self->window, openbox_premax, cardinal,
2162 (guint32**)&dimensions, &num)) {
2164 if (dir == 0 || dir == 1) { /* horz */
2168 if (dir == 0 || dir == 2) { /* vert */
2177 if (dir == 0 || dir == 1) /* horz */
2178 self->max_horz = max;
2179 if (dir == 0 || dir == 2) /* vert */
2180 self->max_vert = max;
2182 if (!self->max_horz && !self->max_vert)
2183 PROP_ERASE(self->window, openbox_premax);
2185 client_change_state(self); /* change the state hints on the client */
2187 client_setup_decor_and_functions(self);
2189 /* figure out where the client should be going */
2190 frame_frame_gravity(self->frame, &x, &y);
2191 client_configure(self, OB_CORNER_TOPLEFT, x, y, w, h, TRUE, TRUE);
2194 void client_shade(ObClient *self, gboolean shade)
2196 if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
2197 shade) || /* can't shade */
2198 self->shaded == shade) return; /* already done */
2200 /* when we're iconic, don't change the wmstate */
2202 self->wmstate = shade ? IconicState : NormalState;
2203 self->shaded = shade;
2204 client_change_state(self);
2205 /* resize the frame to just the titlebar */
2206 frame_adjust_area(self->frame, FALSE, FALSE, FALSE);
2209 void client_close(ObClient *self)
2213 if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
2216 XXX: itd be cool to do timeouts and shit here for killing the client's
2218 like... if the window is around after 5 seconds, then the close button
2219 turns a nice red, and if this function is called again, the client is
2223 ce.xclient.type = ClientMessage;
2224 ce.xclient.message_type = prop_atoms.wm_protocols;
2225 ce.xclient.display = ob_display;
2226 ce.xclient.window = self->window;
2227 ce.xclient.format = 32;
2228 ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
2229 ce.xclient.data.l[1] = event_lasttime;
2230 ce.xclient.data.l[2] = 0l;
2231 ce.xclient.data.l[3] = 0l;
2232 ce.xclient.data.l[4] = 0l;
2233 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2236 void client_kill(ObClient *self)
2238 XKillClient(ob_display, self->window);
2241 void client_set_desktop_recursive(ObClient *self,
2242 guint target, gboolean donthide)
2247 if (target != self->desktop) {
2249 ob_debug("Setting desktop %u\n", target+1);
2251 g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2253 /* remove from the old desktop(s) */
2254 focus_order_remove(self);
2256 old = self->desktop;
2257 self->desktop = target;
2258 PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2259 /* the frame can display the current desktop state */
2260 frame_adjust_state(self->frame);
2261 /* 'move' the window to the new desktop */
2263 client_showhide(self);
2264 /* raise if it was not already on the desktop */
2265 if (old != DESKTOP_ALL)
2266 stacking_raise(CLIENT_AS_WINDOW(self));
2267 screen_update_areas();
2269 /* add to the new desktop(s) */
2270 if (config_focus_new)
2271 focus_order_to_top(self);
2273 focus_order_to_bottom(self);
2276 /* move all transients */
2277 for (it = self->transients; it != NULL; it = it->next)
2278 if (it->data != self) client_set_desktop_recursive(it->data,
2282 void client_set_desktop(ObClient *self, guint target, gboolean donthide)
2284 client_set_desktop_recursive(client_search_top_transient(self),
2288 ObClient *client_search_modal_child(ObClient *self)
2293 for (it = self->transients; it != NULL; it = it->next) {
2294 ObClient *c = it->data;
2295 if ((ret = client_search_modal_child(c))) return ret;
2296 if (c->modal) return c;
2301 gboolean client_validate(ObClient *self)
2305 XSync(ob_display, FALSE); /* get all events on the server */
2307 if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2308 XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2309 XPutBackEvent(ob_display, &e);
2316 void client_set_wm_state(ObClient *self, long state)
2318 if (state == self->wmstate) return; /* no change */
2322 client_iconify(self, TRUE, TRUE);
2325 client_iconify(self, FALSE, TRUE);
2330 void client_set_state(ObClient *self, Atom action, long data1, long data2)
2332 gboolean shaded = self->shaded;
2333 gboolean fullscreen = self->fullscreen;
2334 gboolean max_horz = self->max_horz;
2335 gboolean max_vert = self->max_vert;
2338 if (!(action == prop_atoms.net_wm_state_add ||
2339 action == prop_atoms.net_wm_state_remove ||
2340 action == prop_atoms.net_wm_state_toggle))
2341 /* an invalid action was passed to the client message, ignore it */
2344 for (i = 0; i < 2; ++i) {
2345 Atom state = i == 0 ? data1 : data2;
2347 if (!state) continue;
2349 /* if toggling, then pick whether we're adding or removing */
2350 if (action == prop_atoms.net_wm_state_toggle) {
2351 if (state == prop_atoms.net_wm_state_modal)
2352 action = self->modal ? prop_atoms.net_wm_state_remove :
2353 prop_atoms.net_wm_state_add;
2354 else if (state == prop_atoms.net_wm_state_maximized_vert)
2355 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2356 prop_atoms.net_wm_state_add;
2357 else if (state == prop_atoms.net_wm_state_maximized_horz)
2358 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2359 prop_atoms.net_wm_state_add;
2360 else if (state == prop_atoms.net_wm_state_shaded)
2361 action = self->shaded ? prop_atoms.net_wm_state_remove :
2362 prop_atoms.net_wm_state_add;
2363 else if (state == prop_atoms.net_wm_state_skip_taskbar)
2364 action = self->skip_taskbar ?
2365 prop_atoms.net_wm_state_remove :
2366 prop_atoms.net_wm_state_add;
2367 else if (state == prop_atoms.net_wm_state_skip_pager)
2368 action = self->skip_pager ?
2369 prop_atoms.net_wm_state_remove :
2370 prop_atoms.net_wm_state_add;
2371 else if (state == prop_atoms.net_wm_state_fullscreen)
2372 action = self->fullscreen ?
2373 prop_atoms.net_wm_state_remove :
2374 prop_atoms.net_wm_state_add;
2375 else if (state == prop_atoms.net_wm_state_above)
2376 action = self->above ? prop_atoms.net_wm_state_remove :
2377 prop_atoms.net_wm_state_add;
2378 else if (state == prop_atoms.net_wm_state_below)
2379 action = self->below ? prop_atoms.net_wm_state_remove :
2380 prop_atoms.net_wm_state_add;
2383 if (action == prop_atoms.net_wm_state_add) {
2384 if (state == prop_atoms.net_wm_state_modal) {
2385 /* XXX raise here or something? */
2387 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2389 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2391 } else if (state == prop_atoms.net_wm_state_shaded) {
2393 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2394 self->skip_taskbar = TRUE;
2395 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2396 self->skip_pager = TRUE;
2397 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2399 } else if (state == prop_atoms.net_wm_state_above) {
2401 } else if (state == prop_atoms.net_wm_state_below) {
2405 } else { /* action == prop_atoms.net_wm_state_remove */
2406 if (state == prop_atoms.net_wm_state_modal) {
2407 self->modal = FALSE;
2408 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2410 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2412 } else if (state == prop_atoms.net_wm_state_shaded) {
2414 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2415 self->skip_taskbar = FALSE;
2416 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2417 self->skip_pager = FALSE;
2418 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2420 } else if (state == prop_atoms.net_wm_state_above) {
2421 self->above = FALSE;
2422 } else if (state == prop_atoms.net_wm_state_below) {
2423 self->below = FALSE;
2427 if (max_horz != self->max_horz || max_vert != self->max_vert) {
2428 if (max_horz != self->max_horz && max_vert != self->max_vert) {
2430 if (max_horz == max_vert) { /* both going the same way */
2431 client_maximize(self, max_horz, 0, TRUE);
2433 client_maximize(self, max_horz, 1, TRUE);
2434 client_maximize(self, max_vert, 2, TRUE);
2438 if (max_horz != self->max_horz)
2439 client_maximize(self, max_horz, 1, TRUE);
2441 client_maximize(self, max_vert, 2, TRUE);
2444 /* change fullscreen state before shading, as it will affect if the window
2446 if (fullscreen != self->fullscreen)
2447 client_fullscreen(self, fullscreen, TRUE);
2448 if (shaded != self->shaded)
2449 client_shade(self, shaded);
2450 client_calc_layer(self);
2451 client_change_state(self); /* change the hint to reflect these changes */
2454 ObClient *client_focus_target(ObClient *self)
2458 /* if we have a modal child, then focus it, not us */
2459 child = client_search_modal_child(self);
2460 if (child) return child;
2464 gboolean client_can_focus(ObClient *self)
2468 /* choose the correct target */
2469 self = client_focus_target(self);
2471 if (!self->frame->visible)
2474 if (!((self->can_focus || self->focus_notify) &&
2475 (self->desktop == screen_desktop ||
2476 self->desktop == DESKTOP_ALL) &&
2480 /* do a check to see if the window has already been unmapped or destroyed
2481 do this intelligently while watching out for unmaps we've generated
2482 (ignore_unmaps > 0) */
2483 if (XCheckTypedWindowEvent(ob_display, self->window,
2484 DestroyNotify, &ev)) {
2485 XPutBackEvent(ob_display, &ev);
2488 while (XCheckTypedWindowEvent(ob_display, self->window,
2489 UnmapNotify, &ev)) {
2490 if (self->ignore_unmaps) {
2491 self->ignore_unmaps--;
2493 XPutBackEvent(ob_display, &ev);
2501 gboolean client_focus(ObClient *self)
2503 /* choose the correct target */
2504 self = client_focus_target(self);
2506 if (!client_can_focus(self)) {
2507 if (!self->frame->visible) {
2508 /* update the focus lists */
2509 focus_order_to_top(self);
2514 if (self->can_focus)
2515 /* RevertToPointerRoot causes much more headache than RevertToNone, so
2516 I choose to use it always, hopefully to find errors quicker, if any
2517 are left. (I hate X. I hate focus events.) */
2518 XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2521 if (self->focus_notify) {
2523 ce.xclient.type = ClientMessage;
2524 ce.xclient.message_type = prop_atoms.wm_protocols;
2525 ce.xclient.display = ob_display;
2526 ce.xclient.window = self->window;
2527 ce.xclient.format = 32;
2528 ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2529 ce.xclient.data.l[1] = event_lasttime;
2530 ce.xclient.data.l[2] = 0l;
2531 ce.xclient.data.l[3] = 0l;
2532 ce.xclient.data.l[4] = 0l;
2533 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2537 ob_debug("%sively focusing %lx at %d\n",
2538 (self->can_focus ? "act" : "pass"),
2539 self->window, (int) event_lasttime);
2542 /* Cause the FocusIn to come back to us. Important for desktop switches,
2543 since otherwise we'll have no FocusIn on the queue and send it off to
2544 the focus_backup. */
2545 XSync(ob_display, FALSE);
2549 void client_unfocus(ObClient *self)
2551 g_assert(focus_client == self);
2553 ob_debug("client_unfocus for %lx\n", self->window);
2555 focus_fallback(OB_FOCUS_FALLBACK_UNFOCUSING);
2558 void client_activate(ObClient *self, gboolean here)
2560 if (client_normal(self) && screen_showing_desktop)
2561 screen_show_desktop(FALSE);
2563 client_iconify(self, FALSE, FALSE);
2564 if (self->desktop != DESKTOP_ALL &&
2565 self->desktop != screen_desktop) {
2567 client_set_desktop(self, screen_desktop, FALSE);
2569 screen_set_desktop(self->desktop);
2570 } else if (!self->frame->visible)
2571 /* if its not visible for other reasons, then don't mess
2575 client_shade(self, FALSE);
2577 stacking_raise(CLIENT_AS_WINDOW(self));
2580 gboolean client_focused(ObClient *self)
2582 return self == focus_client;
2585 ObClientIcon *client_icon(ObClient *self, int w, int h)
2588 /* si is the smallest image >= req */
2589 /* li is the largest image < req */
2590 unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2592 if (!self->nicons) return NULL;
2594 for (i = 0; i < self->nicons; ++i) {
2595 size = self->icons[i].width * self->icons[i].height;
2596 if (size < smallest && size >= (unsigned)(w * h)) {
2600 if (size > largest && size <= (unsigned)(w * h)) {
2605 if (largest == 0) /* didnt find one smaller than the requested size */
2606 return &self->icons[si];
2607 return &self->icons[li];
2610 /* this be mostly ripped from fvwm */
2611 ObClient *client_find_directional(ObClient *c, ObDirection dir)
2613 int my_cx, my_cy, his_cx, his_cy;
2616 int score, best_score;
2617 ObClient *best_client, *cur;
2623 /* first, find the centre coords of the currently focused window */
2624 my_cx = c->frame->area.x + c->frame->area.width / 2;
2625 my_cy = c->frame->area.y + c->frame->area.height / 2;
2630 for(it = g_list_first(client_list); it; it = it->next) {
2633 /* the currently selected window isn't interesting */
2636 if (!client_normal(cur))
2638 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2642 if(client_focus_target(cur) == cur &&
2643 !(cur->can_focus || cur->focus_notify))
2646 /* find the centre coords of this window, from the
2647 * currently focused window's point of view */
2648 his_cx = (cur->frame->area.x - my_cx)
2649 + cur->frame->area.width / 2;
2650 his_cy = (cur->frame->area.y - my_cy)
2651 + cur->frame->area.height / 2;
2653 if(dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
2654 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST) {
2656 /* Rotate the diagonals 45 degrees counterclockwise.
2657 * To do this, multiply the matrix /+h +h\ with the
2658 * vector (x y). \-h +h/
2659 * h = sqrt(0.5). We can set h := 1 since absolute
2660 * distance doesn't matter here. */
2661 tx = his_cx + his_cy;
2662 his_cy = -his_cx + his_cy;
2667 case OB_DIRECTION_NORTH:
2668 case OB_DIRECTION_SOUTH:
2669 case OB_DIRECTION_NORTHEAST:
2670 case OB_DIRECTION_SOUTHWEST:
2671 offset = (his_cx < 0) ? -his_cx : his_cx;
2672 distance = ((dir == OB_DIRECTION_NORTH ||
2673 dir == OB_DIRECTION_NORTHEAST) ?
2676 case OB_DIRECTION_EAST:
2677 case OB_DIRECTION_WEST:
2678 case OB_DIRECTION_SOUTHEAST:
2679 case OB_DIRECTION_NORTHWEST:
2680 offset = (his_cy < 0) ? -his_cy : his_cy;
2681 distance = ((dir == OB_DIRECTION_WEST ||
2682 dir == OB_DIRECTION_NORTHWEST) ?
2687 /* the target must be in the requested direction */
2691 /* Calculate score for this window. The smaller the better. */
2692 score = distance + offset;
2694 /* windows more than 45 degrees off the direction are
2695 * heavily penalized and will only be chosen if nothing
2696 * else within a million pixels */
2697 if(offset > distance)
2700 if(best_score == -1 || score < best_score)
2708 void client_set_layer(ObClient *self, int layer)
2712 self->above = FALSE;
2713 } else if (layer == 0) {
2714 self->below = self->above = FALSE;
2716 self->below = FALSE;
2719 client_calc_layer(self);
2720 client_change_state(self); /* reflect this in the state hints */
2723 guint client_monitor(ObClient *self)
2727 for (i = 0; i < screen_num_monitors; ++i) {
2728 Rect *area = screen_physical_area_monitor(i);
2729 if (RECT_INTERSECTS_RECT(*area, self->frame->area))
2732 if (i == screen_num_monitors) i = 0;
2733 g_assert(i < screen_num_monitors);
2737 ObClient *client_search_top_transient(ObClient *self)
2739 /* move up the transient chain as far as possible */
2740 if (self->transient_for) {
2741 if (self->transient_for != OB_TRAN_GROUP) {
2742 return client_search_top_transient(self->transient_for);
2746 for (it = self->group->members; it; it = it->next) {
2747 ObClient *c = it->data;
2749 /* checking transient_for prevents infinate loops! */
2750 if (c != self && !c->transient_for)
2761 ObClient *client_search_transient(ObClient *self, ObClient *search)
2765 for (sit = self->transients; sit; sit = g_slist_next(sit)) {
2766 if (sit->data == search)
2768 if (client_search_transient(sit->data, search))
2774 gchar* client_get_sm_client_id(ObClient *self)
2778 if (!PROP_GETS(self->window, sm_client_id, locale, &id) && self->group)
2779 PROP_GETS(self->group->leader, sm_client_id, locale, &id);
2783 /* finds the nearest edge in the given direction from the current client
2784 * note to self: the edge is the -frame- edge (the actual one), not the
2787 int client_directional_edge_search(ObClient *c, ObDirection dir)
2790 int my_edge_start, my_edge_end, my_offset;
2797 a = screen_area(c->desktop);
2800 case OB_DIRECTION_NORTH:
2801 my_edge_start = c->frame->area.x;
2802 my_edge_end = c->frame->area.x + c->frame->area.width;
2803 my_offset = c->frame->area.y;
2805 dest = a->y; /* default: top of screen */
2807 for(it = g_list_first(client_list); it; it = it->next) {
2808 int his_edge_start, his_edge_end, his_offset;
2809 ObClient *cur = it->data;
2813 if(!client_normal(cur))
2815 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2820 his_edge_start = cur->frame->area.x;
2821 his_edge_end = cur->frame->area.x + cur->frame->area.width;
2822 his_offset = cur->frame->area.y + cur->frame->area.height;
2824 if(his_offset + 1 > my_offset)
2827 if(his_offset < dest)
2830 if(his_edge_start >= my_edge_start &&
2831 his_edge_start <= my_edge_end)
2834 if(my_edge_start >= his_edge_start &&
2835 my_edge_start <= his_edge_end)
2840 case OB_DIRECTION_SOUTH:
2841 my_edge_start = c->frame->area.x;
2842 my_edge_end = c->frame->area.x + c->frame->area.width;
2843 my_offset = c->frame->area.y + c->frame->area.height;
2845 dest = a->y + a->height; /* default: bottom of screen */
2847 for(it = g_list_first(client_list); it; it = it->next) {
2848 int his_edge_start, his_edge_end, his_offset;
2849 ObClient *cur = it->data;
2853 if(!client_normal(cur))
2855 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2860 his_edge_start = cur->frame->area.x;
2861 his_edge_end = cur->frame->area.x + cur->frame->area.width;
2862 his_offset = cur->frame->area.y;
2865 if(his_offset - 1 < my_offset)
2868 if(his_offset > dest)
2871 if(his_edge_start >= my_edge_start &&
2872 his_edge_start <= my_edge_end)
2875 if(my_edge_start >= his_edge_start &&
2876 my_edge_start <= his_edge_end)
2881 case OB_DIRECTION_WEST:
2882 my_edge_start = c->frame->area.y;
2883 my_edge_end = c->frame->area.y + c->frame->area.height;
2884 my_offset = c->frame->area.x;
2886 dest = a->x; /* default: leftmost egde of screen */
2888 for(it = g_list_first(client_list); it; it = it->next) {
2889 int his_edge_start, his_edge_end, his_offset;
2890 ObClient *cur = it->data;
2894 if(!client_normal(cur))
2896 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2901 his_edge_start = cur->frame->area.y;
2902 his_edge_end = cur->frame->area.y + cur->frame->area.height;
2903 his_offset = cur->frame->area.x + cur->frame->area.width;
2905 if(his_offset + 1 > my_offset)
2908 if(his_offset < dest)
2911 if(his_edge_start >= my_edge_start &&
2912 his_edge_start <= my_edge_end)
2915 if(my_edge_start >= his_edge_start &&
2916 my_edge_start <= his_edge_end)
2922 case OB_DIRECTION_EAST:
2923 my_edge_start = c->frame->area.y;
2924 my_edge_end = c->frame->area.y + c->frame->area.height;
2925 my_offset = c->frame->area.x + c->frame->area.width;
2927 dest = a->x + a->width; /* default: rightmost edge of screen */
2929 for(it = g_list_first(client_list); it; it = it->next) {
2930 int his_edge_start, his_edge_end, his_offset;
2931 ObClient *cur = it->data;
2935 if(!client_normal(cur))
2937 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2942 his_edge_start = cur->frame->area.y;
2943 his_edge_end = cur->frame->area.y + cur->frame->area.height;
2944 his_offset = cur->frame->area.x;
2946 if(his_offset - 1 < my_offset)
2949 if(his_offset > dest)
2952 if(his_edge_start >= my_edge_start &&
2953 his_edge_start <= my_edge_end)
2956 if(my_edge_start >= his_edge_start &&
2957 my_edge_start <= his_edge_end)
2962 case OB_DIRECTION_NORTHEAST:
2963 case OB_DIRECTION_SOUTHEAST:
2964 case OB_DIRECTION_NORTHWEST:
2965 case OB_DIRECTION_SOUTHWEST:
2966 /* not implemented */
2969 g_assert_not_reached();