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);
464 g_free(self->sm_client_id);
467 /* update the list hints */
471 static void client_urgent_notify(ObClient *self)
474 frame_flash_start(self->frame);
476 frame_flash_stop(self->frame);
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_move(self, x, y);
543 gboolean client_find_onscreen(ObClient *self, int *x, int *y, int w, int h,
547 int ox = *x, oy = *y;
549 frame_client_gravity(self->frame, x, y); /* get where the frame
552 /* XXX watch for xinerama dead areas */
554 a = screen_area(self->desktop);
555 if (client_normal(self)) {
556 if (!self->strut.right && *x >= a->x + a->width - 1)
557 *x = a->x + a->width - self->frame->area.width;
558 if (!self->strut.bottom && *y >= a->y + a->height - 1)
559 *y = a->y + a->height - self->frame->area.height;
560 if (!self->strut.left && *x + self->frame->area.width - 1 < a->x)
562 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 client_get_area(self);
659 client_update_transient_for(self);
660 client_update_wmhints(self);
661 client_get_startup_id(self);
662 client_get_desktop(self);
663 client_get_state(self);
664 client_get_shaped(self);
666 client_get_mwm_hints(self);
667 client_get_type(self);/* this can change the mwmhints for special cases */
669 client_update_protocols(self);
671 client_get_gravity(self); /* get the attribute gravity */
672 client_update_normal_hints(self); /* this may override the attribute
675 /* got the type, the mwmhints, the protocols, and the normal hints
676 (min/max sizes), so we're ready to set up the decorations/functions */
677 client_setup_decor_and_functions(self);
679 client_update_title(self);
680 client_update_class(self);
681 client_update_sm_client_id(self);
682 client_update_strut(self);
683 client_update_icons(self);
686 static void client_get_startup_id(ObClient *self)
688 if (!(PROP_GETS(self->window, net_startup_id, utf8, &self->startup_id)))
690 PROP_GETS(self->group->leader,
691 net_startup_id, utf8, &self->startup_id);
694 static void client_get_area(ObClient *self)
696 XWindowAttributes wattrib;
699 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
700 g_assert(ret != BadWindow);
702 RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
703 self->border_width = wattrib.border_width;
706 static void client_get_desktop(ObClient *self)
708 guint32 d = screen_num_desktops; /* an always-invalid value */
710 if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
711 if (d >= screen_num_desktops && d != DESKTOP_ALL)
712 self->desktop = screen_num_desktops - 1;
716 gboolean trdesk = FALSE;
718 if (self->transient_for) {
719 if (self->transient_for != OB_TRAN_GROUP) {
720 self->desktop = self->transient_for->desktop;
725 for (it = self->group->members; it; it = it->next)
726 if (it->data != self &&
727 !((ObClient*)it->data)->transient_for) {
728 self->desktop = ((ObClient*)it->data)->desktop;
735 /* try get from the startup-notification protocol */
736 if (sn_get_desktop(self->startup_id, &self->desktop)) {
737 if (self->desktop >= screen_num_desktops &&
738 self->desktop != DESKTOP_ALL)
739 self->desktop = screen_num_desktops - 1;
741 /* defaults to the current desktop */
742 self->desktop = screen_desktop;
745 if (self->desktop != d) {
746 /* set the desktop hint, to make sure that it always exists */
747 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
751 static void client_get_state(ObClient *self)
756 if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
758 for (i = 0; i < num; ++i) {
759 if (state[i] == prop_atoms.net_wm_state_modal)
761 else if (state[i] == prop_atoms.net_wm_state_shaded)
763 else if (state[i] == prop_atoms.net_wm_state_hidden)
765 else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
766 self->skip_taskbar = TRUE;
767 else if (state[i] == prop_atoms.net_wm_state_skip_pager)
768 self->skip_pager = TRUE;
769 else if (state[i] == prop_atoms.net_wm_state_fullscreen)
770 self->fullscreen = TRUE;
771 else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
772 self->max_vert = TRUE;
773 else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
774 self->max_horz = TRUE;
775 else if (state[i] == prop_atoms.net_wm_state_above)
777 else if (state[i] == prop_atoms.net_wm_state_below)
785 static void client_get_shaped(ObClient *self)
787 self->shaped = FALSE;
789 if (extensions_shape) {
794 XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
796 XShapeQueryExtents(ob_display, self->window, &s, &foo,
797 &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
799 self->shaped = (s != 0);
804 void client_update_transient_for(ObClient *self)
807 ObClient *target = NULL;
809 if (XGetTransientForHint(ob_display, self->window, &t)) {
810 self->transient = TRUE;
811 if (t != self->window) { /* cant be transient to itself! */
812 target = g_hash_table_lookup(window_map, &t);
813 /* if this happens then we need to check for it*/
814 g_assert(target != self);
815 if (target && !WINDOW_IS_CLIENT(target)) {
816 /* this can happen when a dialog is a child of
817 a dockapp, for example */
821 if (!target && self->group) {
822 /* not transient to a client, see if it is transient for a
824 if (t == self->group->leader ||
826 t == RootWindow(ob_display, ob_screen)) {
827 /* window is a transient for its group! */
828 target = OB_TRAN_GROUP;
833 self->transient = FALSE;
835 /* if anything has changed... */
836 if (target != self->transient_for) {
837 if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
840 /* remove from old parents */
841 for (it = self->group->members; it; it = g_slist_next(it)) {
842 ObClient *c = it->data;
843 if (c != self && !c->transient_for)
844 c->transients = g_slist_remove(c->transients, self);
846 } else if (self->transient_for != NULL) { /* transient of window */
847 /* remove from old parent */
848 self->transient_for->transients =
849 g_slist_remove(self->transient_for->transients, self);
851 self->transient_for = target;
852 if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
855 /* add to new parents */
856 for (it = self->group->members; it; it = g_slist_next(it)) {
857 ObClient *c = it->data;
858 if (c != self && !c->transient_for)
859 c->transients = g_slist_append(c->transients, self);
862 /* remove all transients which are in the group, that causes
863 circlular pointer hell of doom */
864 for (it = self->group->members; it; it = g_slist_next(it)) {
866 for (sit = self->transients; sit; sit = next) {
867 next = g_slist_next(sit);
868 if (sit->data == it->data)
870 g_slist_delete_link(self->transients, sit);
873 } else if (self->transient_for != NULL) { /* transient of window */
874 /* add to new parent */
875 self->transient_for->transients =
876 g_slist_append(self->transient_for->transients, self);
881 static void client_get_mwm_hints(ObClient *self)
886 self->mwmhints.flags = 0; /* default to none */
888 if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
890 if (num >= OB_MWM_ELEMENTS) {
891 self->mwmhints.flags = hints[0];
892 self->mwmhints.functions = hints[1];
893 self->mwmhints.decorations = hints[2];
899 void client_get_type(ObClient *self)
906 if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
907 /* use the first value that we know about in the array */
908 for (i = 0; i < num; ++i) {
909 if (val[i] == prop_atoms.net_wm_window_type_desktop)
910 self->type = OB_CLIENT_TYPE_DESKTOP;
911 else if (val[i] == prop_atoms.net_wm_window_type_dock)
912 self->type = OB_CLIENT_TYPE_DOCK;
913 else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
914 self->type = OB_CLIENT_TYPE_TOOLBAR;
915 else if (val[i] == prop_atoms.net_wm_window_type_menu)
916 self->type = OB_CLIENT_TYPE_MENU;
917 else if (val[i] == prop_atoms.net_wm_window_type_utility)
918 self->type = OB_CLIENT_TYPE_UTILITY;
919 else if (val[i] == prop_atoms.net_wm_window_type_splash)
920 self->type = OB_CLIENT_TYPE_SPLASH;
921 else if (val[i] == prop_atoms.net_wm_window_type_dialog)
922 self->type = OB_CLIENT_TYPE_DIALOG;
923 else if (val[i] == prop_atoms.net_wm_window_type_normal)
924 self->type = OB_CLIENT_TYPE_NORMAL;
925 else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
926 /* prevent this window from getting any decor or
928 self->mwmhints.flags &= (OB_MWM_FLAG_FUNCTIONS |
929 OB_MWM_FLAG_DECORATIONS);
930 self->mwmhints.decorations = 0;
931 self->mwmhints.functions = 0;
933 if (self->type != (ObClientType) -1)
934 break; /* grab the first legit type */
939 if (self->type == (ObClientType) -1) {
940 /*the window type hint was not set, which means we either classify
941 ourself as a normal window or a dialog, depending on if we are a
944 self->type = OB_CLIENT_TYPE_DIALOG;
946 self->type = OB_CLIENT_TYPE_NORMAL;
950 void client_update_protocols(ObClient *self)
955 self->focus_notify = FALSE;
956 self->delete_window = FALSE;
958 if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
959 for (i = 0; i < num_return; ++i) {
960 if (proto[i] == prop_atoms.wm_delete_window) {
961 /* this means we can request the window to close */
962 self->delete_window = TRUE;
963 } else if (proto[i] == prop_atoms.wm_take_focus)
964 /* if this protocol is requested, then the window will be
965 notified whenever we want it to receive focus */
966 self->focus_notify = TRUE;
972 static void client_get_gravity(ObClient *self)
974 XWindowAttributes wattrib;
977 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
978 g_assert(ret != BadWindow);
979 self->gravity = wattrib.win_gravity;
982 void client_update_normal_hints(ObClient *self)
986 int oldgravity = self->gravity;
989 self->min_ratio = 0.0f;
990 self->max_ratio = 0.0f;
991 SIZE_SET(self->size_inc, 1, 1);
992 SIZE_SET(self->base_size, 0, 0);
993 SIZE_SET(self->min_size, 0, 0);
994 SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
996 /* get the hints from the window */
997 if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
998 self->positioned = !!(size.flags & (PPosition|USPosition));
1000 if (size.flags & PWinGravity) {
1001 self->gravity = size.win_gravity;
1003 /* if the client has a frame, i.e. has already been mapped and
1004 is changing its gravity */
1005 if (self->frame && self->gravity != oldgravity) {
1006 /* move our idea of the client's position based on its new
1008 self->area.x = self->frame->area.x;
1009 self->area.y = self->frame->area.y;
1010 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
1014 if (size.flags & PAspect) {
1015 if (size.min_aspect.y)
1016 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
1017 if (size.max_aspect.y)
1018 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
1021 if (size.flags & PMinSize)
1022 SIZE_SET(self->min_size, size.min_width, size.min_height);
1024 if (size.flags & PMaxSize)
1025 SIZE_SET(self->max_size, size.max_width, size.max_height);
1027 if (size.flags & PBaseSize)
1028 SIZE_SET(self->base_size, size.base_width, size.base_height);
1030 if (size.flags & PResizeInc)
1031 SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
1035 void client_setup_decor_and_functions(ObClient *self)
1037 /* start with everything (cept fullscreen) */
1039 (OB_FRAME_DECOR_TITLEBAR |
1040 (ob_rr_theme->show_handle ? OB_FRAME_DECOR_HANDLE : 0) |
1041 OB_FRAME_DECOR_GRIPS |
1042 OB_FRAME_DECOR_BORDER |
1043 OB_FRAME_DECOR_ICON |
1044 OB_FRAME_DECOR_ALLDESKTOPS |
1045 OB_FRAME_DECOR_ICONIFY |
1046 OB_FRAME_DECOR_MAXIMIZE |
1047 OB_FRAME_DECOR_SHADE);
1049 (OB_CLIENT_FUNC_RESIZE |
1050 OB_CLIENT_FUNC_MOVE |
1051 OB_CLIENT_FUNC_ICONIFY |
1052 OB_CLIENT_FUNC_MAXIMIZE |
1053 OB_CLIENT_FUNC_SHADE);
1054 if (self->delete_window) {
1055 self->functions |= OB_CLIENT_FUNC_CLOSE;
1056 self->decorations |= OB_FRAME_DECOR_CLOSE;
1059 if (!(self->min_size.width < self->max_size.width ||
1060 self->min_size.height < self->max_size.height))
1061 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1063 switch (self->type) {
1064 case OB_CLIENT_TYPE_NORMAL:
1065 /* normal windows retain all of the possible decorations and
1066 functionality, and are the only windows that you can fullscreen */
1067 self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1070 case OB_CLIENT_TYPE_DIALOG:
1071 case OB_CLIENT_TYPE_UTILITY:
1072 /* these windows cannot be maximized */
1073 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1076 case OB_CLIENT_TYPE_MENU:
1077 case OB_CLIENT_TYPE_TOOLBAR:
1078 /* these windows get less functionality */
1079 self->functions &= ~(OB_CLIENT_FUNC_ICONIFY | OB_CLIENT_FUNC_RESIZE);
1082 case OB_CLIENT_TYPE_DESKTOP:
1083 case OB_CLIENT_TYPE_DOCK:
1084 case OB_CLIENT_TYPE_SPLASH:
1085 /* none of these windows are manipulated by the window manager */
1086 self->decorations = 0;
1087 self->functions = 0;
1091 /* Mwm Hints are applied subtractively to what has already been chosen for
1092 decor and functionality */
1093 if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
1094 if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
1095 if (! ((self->mwmhints.decorations & OB_MWM_DECOR_HANDLE) ||
1096 (self->mwmhints.decorations & OB_MWM_DECOR_TITLE)))
1097 /* if the mwm hints request no handle or title, then all
1098 decorations are disabled */
1099 self->decorations = 0;
1103 if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
1104 if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
1105 if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
1106 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1107 if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
1108 self->functions &= ~OB_CLIENT_FUNC_MOVE;
1109 /* dont let mwm hints kill any buttons
1110 if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
1111 self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
1112 if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
1113 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1115 /* dont let mwm hints kill the close button
1116 if (! (self->mwmhints.functions & MwmFunc_Close))
1117 self->functions &= ~OB_CLIENT_FUNC_CLOSE; */
1121 if (!(self->functions & OB_CLIENT_FUNC_SHADE))
1122 self->decorations &= ~OB_FRAME_DECOR_SHADE;
1123 if (!(self->functions & OB_CLIENT_FUNC_ICONIFY))
1124 self->decorations &= ~OB_FRAME_DECOR_ICONIFY;
1125 if (!(self->functions & OB_CLIENT_FUNC_RESIZE))
1126 self->decorations &= ~OB_FRAME_DECOR_GRIPS;
1128 /* can't maximize without moving/resizing */
1129 if (!((self->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
1130 (self->functions & OB_CLIENT_FUNC_MOVE) &&
1131 (self->functions & OB_CLIENT_FUNC_RESIZE))) {
1132 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1133 self->decorations &= ~OB_FRAME_DECOR_MAXIMIZE;
1136 /* kill the handle on fully maxed windows */
1137 if (self->max_vert && self->max_horz)
1138 self->decorations &= ~OB_FRAME_DECOR_HANDLE;
1140 /* finally, the user can have requested no decorations, which overrides
1142 if (!self->decorate)
1143 self->decorations = OB_FRAME_DECOR_BORDER;
1145 /* if we don't have a titlebar, then we cannot shade! */
1146 if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1147 self->functions &= ~OB_CLIENT_FUNC_SHADE;
1149 /* now we need to check against rules for the client's current state */
1150 if (self->fullscreen) {
1151 self->functions &= (OB_CLIENT_FUNC_CLOSE |
1152 OB_CLIENT_FUNC_FULLSCREEN |
1153 OB_CLIENT_FUNC_ICONIFY);
1154 self->decorations = 0;
1157 client_change_allowed_actions(self);
1160 /* adjust the client's decorations, etc. */
1161 client_reconfigure(self);
1163 /* this makes sure that these windows appear on all desktops */
1164 if (self->type == OB_CLIENT_TYPE_DESKTOP &&
1165 self->desktop != DESKTOP_ALL)
1167 self->desktop = DESKTOP_ALL;
1172 static void client_change_allowed_actions(ObClient *self)
1177 /* desktop windows are kept on all desktops */
1178 if (self->type != OB_CLIENT_TYPE_DESKTOP)
1179 actions[num++] = prop_atoms.net_wm_action_change_desktop;
1181 if (self->functions & OB_CLIENT_FUNC_SHADE)
1182 actions[num++] = prop_atoms.net_wm_action_shade;
1183 if (self->functions & OB_CLIENT_FUNC_CLOSE)
1184 actions[num++] = prop_atoms.net_wm_action_close;
1185 if (self->functions & OB_CLIENT_FUNC_MOVE)
1186 actions[num++] = prop_atoms.net_wm_action_move;
1187 if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1188 actions[num++] = prop_atoms.net_wm_action_minimize;
1189 if (self->functions & OB_CLIENT_FUNC_RESIZE)
1190 actions[num++] = prop_atoms.net_wm_action_resize;
1191 if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1192 actions[num++] = prop_atoms.net_wm_action_fullscreen;
1193 if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1194 actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1195 actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1198 PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1200 /* make sure the window isn't breaking any rules now */
1202 if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1203 if (self->frame) client_shade(self, FALSE);
1204 else self->shaded = FALSE;
1206 if (!(self->functions & OB_CLIENT_FUNC_ICONIFY) && self->iconic) {
1207 if (self->frame) client_iconify(self, FALSE, TRUE);
1208 else self->iconic = FALSE;
1210 if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1211 if (self->frame) client_fullscreen(self, FALSE, TRUE);
1212 else self->fullscreen = FALSE;
1214 if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
1216 if (self->frame) client_maximize(self, FALSE, 0, TRUE);
1217 else self->max_vert = self->max_horz = FALSE;
1221 void client_reconfigure(ObClient *self)
1223 /* by making this pass FALSE for user, we avoid the emacs event storm where
1224 every configurenotify causes an update in its normal hints, i think this
1225 is generally what we want anyways... */
1226 client_configure(self, OB_CORNER_TOPLEFT, self->area.x, self->area.y,
1227 self->area.width, self->area.height, FALSE, TRUE);
1230 void client_update_wmhints(ObClient *self)
1233 gboolean ur = FALSE;
1236 /* assume a window takes input if it doesnt specify */
1237 self->can_focus = TRUE;
1239 if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1240 if (hints->flags & InputHint)
1241 self->can_focus = hints->input;
1243 /* only do this when first managing the window *AND* when we aren't
1245 if (ob_state() != OB_STATE_STARTING && self->frame == NULL)
1246 if (hints->flags & StateHint)
1247 self->iconic = hints->initial_state == IconicState;
1249 if (hints->flags & XUrgencyHint)
1252 if (!(hints->flags & WindowGroupHint))
1253 hints->window_group = None;
1255 /* did the group state change? */
1256 if (hints->window_group !=
1257 (self->group ? self->group->leader : None)) {
1258 /* remove from the old group if there was one */
1259 if (self->group != NULL) {
1260 /* remove transients of the group */
1261 for (it = self->group->members; it; it = it->next)
1262 self->transients = g_slist_remove(self->transients,
1264 group_remove(self->group, self);
1267 if (hints->window_group != None) {
1268 self->group = group_add(hints->window_group, self);
1270 /* i can only have transients from the group if i am not
1272 if (!self->transient_for) {
1273 /* add other transients of the group that are already
1275 for (it = self->group->members; it; it = it->next) {
1276 ObClient *c = it->data;
1277 if (c != self && c->transient_for == OB_TRAN_GROUP)
1279 g_slist_append(self->transients, c);
1284 /* because the self->transient flag wont change from this call,
1285 we don't need to update the window's type and such, only its
1286 transient_for, and the transients lists of other windows in
1287 the group may be affected */
1288 client_update_transient_for(self);
1291 /* the WM_HINTS can contain an icon */
1292 client_update_icons(self);
1297 if (ur != self->urgent) {
1299 ob_debug("Urgent Hint for 0x%lx: %s\n", self->window,
1301 /* fire the urgent callback if we're mapped, otherwise, wait until
1302 after we're mapped */
1304 client_urgent_notify(self);
1308 void client_update_title(ObClient *self)
1314 gboolean read_title;
1317 old_title = self->title;
1320 if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1321 /* try old x stuff */
1322 if (!PROP_GETS(self->window, wm_name, locale, &data))
1323 data = g_strdup("Unnamed Window");
1325 /* did the title change? then reset the title_count */
1326 if (old_title && 0 != strncmp(old_title, data, strlen(data)))
1327 self->title_count = 1;
1329 /* look for duplicates and append a number */
1331 for (it = client_list; it; it = it->next)
1332 if (it->data != self) {
1333 ObClient *c = it->data;
1334 if (0 == strncmp(c->title, data, strlen(data)))
1335 nums |= 1 << c->title_count;
1337 /* find first free number */
1338 for (i = 1; i <= 32; ++i)
1339 if (!(nums & (1 << i))) {
1340 if (self->title_count == 1 || i == 1)
1341 self->title_count = i;
1344 /* dont display the number for the first window */
1345 if (self->title_count > 1) {
1347 ndata = g_strdup_printf("%s - [%u]", data, self->title_count);
1352 PROP_SETS(self->window, net_wm_visible_name, data);
1357 frame_adjust_title(self->frame);
1361 /* update the icon title */
1363 g_free(self->icon_title);
1367 if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1368 /* try old x stuff */
1369 if (!PROP_GETS(self->window, wm_icon_name, locale, &data)) {
1370 data = g_strdup(self->title);
1374 /* append the title count, dont display the number for the first window */
1375 if (read_title && self->title_count > 1) {
1376 char *vdata, *ndata;
1377 ndata = g_strdup_printf(" - [%u]", self->title_count);
1378 vdata = g_strconcat(data, ndata, NULL);
1384 PROP_SETS(self->window, net_wm_visible_icon_name, data);
1386 self->icon_title = data;
1389 void client_update_class(ObClient *self)
1394 if (self->name) g_free(self->name);
1395 if (self->class) g_free(self->class);
1396 if (self->role) g_free(self->role);
1398 self->name = self->class = self->role = NULL;
1400 if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1402 self->name = g_strdup(data[0]);
1404 self->class = g_strdup(data[1]);
1409 if (PROP_GETS(self->window, wm_window_role, locale, &s))
1412 if (self->name == NULL) self->name = g_strdup("");
1413 if (self->class == NULL) self->class = g_strdup("");
1414 if (self->role == NULL) self->role = g_strdup("");
1417 void client_update_strut(ObClient *self)
1421 gboolean got = FALSE;
1424 if (PROP_GETA32(self->window, net_wm_strut_partial, cardinal,
1428 STRUT_PARTIAL_SET(strut,
1429 data[0], data[2], data[1], data[3],
1430 data[4], data[5], data[8], data[9],
1431 data[6], data[7], data[10], data[11]);
1437 PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1440 STRUT_PARTIAL_SET(strut,
1441 data[0], data[2], data[1], data[3],
1442 0, 0, 0, 0, 0, 0, 0, 0);
1448 STRUT_PARTIAL_SET(strut, 0, 0, 0, 0,
1449 0, 0, 0, 0, 0, 0, 0, 0);
1451 if (!STRUT_EQUAL(strut, self->strut)) {
1452 self->strut = strut;
1454 /* updating here is pointless while we're being mapped cuz we're not in
1455 the client list yet */
1457 screen_update_areas();
1461 void client_update_icons(ObClient *self)
1467 for (i = 0; i < self->nicons; ++i)
1468 g_free(self->icons[i].data);
1469 if (self->nicons > 0)
1470 g_free(self->icons);
1473 if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1474 /* figure out how many valid icons are in here */
1476 while (num - i > 2) {
1480 if (i > num || w*h == 0) break;
1484 self->icons = g_new(ObClientIcon, self->nicons);
1486 /* store the icons */
1488 for (j = 0; j < self->nicons; ++j) {
1491 w = self->icons[j].width = data[i++];
1492 h = self->icons[j].height = data[i++];
1494 if (w*h == 0) continue;
1496 self->icons[j].data = g_new(RrPixel32, w * h);
1497 for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1502 self->icons[j].data[t] =
1503 (((data[i] >> 24) & 0xff) << RrDefaultAlphaOffset) +
1504 (((data[i] >> 16) & 0xff) << RrDefaultRedOffset) +
1505 (((data[i] >> 8) & 0xff) << RrDefaultGreenOffset) +
1506 (((data[i] >> 0) & 0xff) << RrDefaultBlueOffset);
1512 } else if (PROP_GETA32(self->window, kwm_win_icon,
1513 kwm_win_icon, &data, &num)) {
1516 self->icons = g_new(ObClientIcon, self->nicons);
1517 xerror_set_ignore(TRUE);
1518 if (!RrPixmapToRGBA(ob_rr_inst,
1520 &self->icons[self->nicons-1].width,
1521 &self->icons[self->nicons-1].height,
1522 &self->icons[self->nicons-1].data)) {
1523 g_free(&self->icons[self->nicons-1]);
1526 xerror_set_ignore(FALSE);
1532 if ((hints = XGetWMHints(ob_display, self->window))) {
1533 if (hints->flags & IconPixmapHint) {
1535 self->icons = g_new(ObClientIcon, self->nicons);
1536 xerror_set_ignore(TRUE);
1537 if (!RrPixmapToRGBA(ob_rr_inst,
1539 (hints->flags & IconMaskHint ?
1540 hints->icon_mask : None),
1541 &self->icons[self->nicons-1].width,
1542 &self->icons[self->nicons-1].height,
1543 &self->icons[self->nicons-1].data)){
1544 g_free(&self->icons[self->nicons-1]);
1547 xerror_set_ignore(FALSE);
1553 if (!self->nicons) {
1555 self->icons = g_new(ObClientIcon, self->nicons);
1556 self->icons[self->nicons-1].width = 48;
1557 self->icons[self->nicons-1].height = 48;
1558 self->icons[self->nicons-1].data = g_memdup(ob_rr_theme->def_win_icon,
1564 frame_adjust_icon(self->frame);
1567 static void client_change_state(ObClient *self)
1570 guint32 netstate[10];
1573 state[0] = self->wmstate;
1575 PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1579 netstate[num++] = prop_atoms.net_wm_state_modal;
1581 netstate[num++] = prop_atoms.net_wm_state_shaded;
1583 netstate[num++] = prop_atoms.net_wm_state_hidden;
1584 if (self->skip_taskbar)
1585 netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1586 if (self->skip_pager)
1587 netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1588 if (self->fullscreen)
1589 netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1591 netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1593 netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1595 netstate[num++] = prop_atoms.net_wm_state_above;
1597 netstate[num++] = prop_atoms.net_wm_state_below;
1598 PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1600 client_calc_layer(self);
1603 frame_adjust_state(self->frame);
1606 ObClient *client_search_focus_tree(ObClient *self)
1611 for (it = self->transients; it != NULL; it = it->next) {
1612 if (client_focused(it->data)) return it->data;
1613 if ((ret = client_search_focus_tree(it->data))) return ret;
1618 ObClient *client_search_focus_tree_full(ObClient *self)
1620 if (self->transient_for) {
1621 if (self->transient_for != OB_TRAN_GROUP) {
1622 return client_search_focus_tree_full(self->transient_for);
1625 gboolean recursed = FALSE;
1627 for (it = self->group->members; it; it = it->next)
1628 if (!((ObClient*)it->data)->transient_for) {
1630 if ((c = client_search_focus_tree_full(it->data)))
1639 /* this function checks the whole tree, the client_search_focus_tree~
1640 does not, so we need to check this window */
1641 if (client_focused(self))
1643 return client_search_focus_tree(self);
1646 static ObStackingLayer calc_layer(ObClient *self)
1650 if (self->fullscreen) l = OB_STACKING_LAYER_FULLSCREEN;
1651 else if (self->type == OB_CLIENT_TYPE_DESKTOP)
1652 l = OB_STACKING_LAYER_DESKTOP;
1653 else if (self->type == OB_CLIENT_TYPE_DOCK) {
1654 if (!self->below) l = OB_STACKING_LAYER_TOP;
1655 else l = OB_STACKING_LAYER_NORMAL;
1657 else if (self->above) l = OB_STACKING_LAYER_ABOVE;
1658 else if (self->below) l = OB_STACKING_LAYER_BELOW;
1659 else l = OB_STACKING_LAYER_NORMAL;
1664 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
1665 ObStackingLayer l, gboolean raised)
1667 ObStackingLayer old, own;
1671 own = calc_layer(self);
1672 self->layer = l > own ? l : own;
1674 for (it = self->transients; it; it = it->next)
1675 client_calc_layer_recursive(it->data, orig,
1676 l, raised ? raised : l != old);
1678 if (!raised && l != old)
1679 if (orig->frame) { /* only restack if the original window is managed */
1680 /* XXX add_non_intrusive ever? */
1681 stacking_remove(CLIENT_AS_WINDOW(self));
1682 stacking_add(CLIENT_AS_WINDOW(self));
1686 void client_calc_layer(ObClient *self)
1693 /* transients take on the layer of their parents */
1694 self = client_search_top_transient(self);
1696 l = calc_layer(self);
1698 client_calc_layer_recursive(self, orig, l, FALSE);
1701 gboolean client_should_show(ObClient *self)
1703 if (self->iconic) return FALSE;
1704 else if (!(self->desktop == screen_desktop ||
1705 self->desktop == DESKTOP_ALL)) return FALSE;
1706 else if (client_normal(self) && screen_showing_desktop) return FALSE;
1711 static void client_showhide(ObClient *self)
1714 if (client_should_show(self))
1715 frame_show(self->frame);
1717 frame_hide(self->frame);
1720 gboolean client_normal(ObClient *self) {
1721 return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
1722 self->type == OB_CLIENT_TYPE_DOCK ||
1723 self->type == OB_CLIENT_TYPE_SPLASH);
1726 static void client_apply_startup_state(ObClient *self)
1728 /* these are in a carefully crafted order.. */
1731 self->iconic = FALSE;
1732 client_iconify(self, TRUE, FALSE);
1734 if (self->fullscreen) {
1735 self->fullscreen = FALSE;
1736 client_fullscreen(self, TRUE, FALSE);
1739 self->shaded = FALSE;
1740 client_shade(self, TRUE);
1743 client_urgent_notify(self);
1745 if (self->max_vert && self->max_horz) {
1746 self->max_vert = self->max_horz = FALSE;
1747 client_maximize(self, TRUE, 0, FALSE);
1748 } else if (self->max_vert) {
1749 self->max_vert = FALSE;
1750 client_maximize(self, TRUE, 2, FALSE);
1751 } else if (self->max_horz) {
1752 self->max_horz = FALSE;
1753 client_maximize(self, TRUE, 1, FALSE);
1756 /* nothing to do for the other states:
1765 void client_configure_full(ObClient *self, ObCorner anchor,
1766 int x, int y, int w, int h,
1767 gboolean user, gboolean final,
1768 gboolean force_reply)
1771 gboolean send_resize_client;
1772 gboolean moved = FALSE, resized = FALSE;
1773 guint fdecor = self->frame->decorations;
1774 gboolean fhorz = self->frame->max_horz;
1776 /* make the frame recalculate its dimentions n shit without changing
1777 anything visible for real, this way the constraints below can work with
1778 the updated frame dimensions. */
1779 frame_adjust_area(self->frame, TRUE, TRUE, TRUE);
1781 /* gets the frame's position */
1782 frame_client_gravity(self->frame, &x, &y);
1784 /* these positions are frame positions, not client positions */
1786 /* set the size and position if fullscreen */
1787 if (self->fullscreen) {
1790 XF86VidModeModeLine mode;
1795 i = client_monitor(self);
1796 a = screen_physical_area_monitor(i);
1799 if (i == 0 && /* primary head */
1800 extensions_vidmode &&
1801 XF86VidModeGetViewPort(ob_display, ob_screen, &x, &y) &&
1802 /* get the mode last so the mode.privsize isnt freed incorrectly */
1803 XF86VidModeGetModeLine(ob_display, ob_screen, &dot, &mode)) {
1808 if (mode.privsize) XFree(mode.private);
1818 user = FALSE; /* ignore that increment etc shit when in fullscreen */
1822 a = screen_area_monitor(self->desktop, client_monitor(self));
1824 /* set the size and position if maximized */
1825 if (self->max_horz) {
1827 w = a->width - self->frame->size.left - self->frame->size.right;
1829 if (self->max_vert) {
1831 h = a->height - self->frame->size.top - self->frame->size.bottom;
1835 /* gets the client's position */
1836 frame_frame_gravity(self->frame, &x, &y);
1838 /* these override the above states! if you cant move you can't move! */
1840 if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
1844 if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
1845 w = self->area.width;
1846 h = self->area.height;
1850 if (!(w == self->area.width && h == self->area.height)) {
1851 int basew, baseh, minw, minh;
1853 /* base size is substituted with min size if not specified */
1854 if (self->base_size.width || self->base_size.height) {
1855 basew = self->base_size.width;
1856 baseh = self->base_size.height;
1858 basew = self->min_size.width;
1859 baseh = self->min_size.height;
1861 /* min size is substituted with base size if not specified */
1862 if (self->min_size.width || self->min_size.height) {
1863 minw = self->min_size.width;
1864 minh = self->min_size.height;
1866 minw = self->base_size.width;
1867 minh = self->base_size.height;
1870 /* if this is a user-requested resize, then check against min/max
1873 /* smaller than min size or bigger than max size? */
1874 if (w > self->max_size.width) w = self->max_size.width;
1875 if (w < minw) w = minw;
1876 if (h > self->max_size.height) h = self->max_size.height;
1877 if (h < minh) h = minh;
1882 /* keep to the increments */
1883 w /= self->size_inc.width;
1884 h /= self->size_inc.height;
1886 /* you cannot resize to nothing */
1887 if (basew + w < 1) w = 1 - basew;
1888 if (baseh + h < 1) h = 1 - baseh;
1890 /* store the logical size */
1891 SIZE_SET(self->logical_size,
1892 self->size_inc.width > 1 ? w : w + basew,
1893 self->size_inc.height > 1 ? h : h + baseh);
1895 w *= self->size_inc.width;
1896 h *= self->size_inc.height;
1901 /* adjust the height to match the width for the aspect ratios.
1902 for this, min size is not substituted for base size ever. */
1903 w -= self->base_size.width;
1904 h -= self->base_size.height;
1906 if (self->min_ratio)
1907 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1908 if (self->max_ratio)
1909 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1911 w += self->base_size.width;
1912 h += self->base_size.height;
1916 case OB_CORNER_TOPLEFT:
1918 case OB_CORNER_TOPRIGHT:
1919 x -= w - self->area.width;
1921 case OB_CORNER_BOTTOMLEFT:
1922 y -= h - self->area.height;
1924 case OB_CORNER_BOTTOMRIGHT:
1925 x -= w - self->area.width;
1926 y -= h - self->area.height;
1930 moved = x != self->area.x || y != self->area.y;
1931 resized = w != self->area.width || h != self->area.height;
1933 oldw = self->area.width;
1934 oldh = self->area.height;
1935 RECT_SET(self->area, x, y, w, h);
1937 /* for app-requested resizes, always resize if 'resized' is true.
1938 for user-requested ones, only resize if final is true, or when
1939 resizing in redraw mode */
1940 send_resize_client = ((!user && resized) ||
1942 (resized && config_redraw_resize))));
1944 /* if the client is enlarging, the resize the client before the frame */
1945 if (send_resize_client && (w > oldw || h > oldh))
1946 XResizeWindow(ob_display, self->window, MAX(w, oldw), MAX(h, oldh));
1948 /* move/resize the frame to match the request */
1950 if (self->decorations != fdecor || self->max_horz != fhorz)
1951 moved = resized = TRUE;
1953 if (moved || resized)
1954 frame_adjust_area(self->frame, moved, resized, FALSE);
1956 if (!resized && (force_reply || ((!user && moved) || (user && final))))
1959 event.type = ConfigureNotify;
1960 event.xconfigure.display = ob_display;
1961 event.xconfigure.event = self->window;
1962 event.xconfigure.window = self->window;
1964 /* root window real coords */
1965 event.xconfigure.x = self->frame->area.x + self->frame->size.left -
1967 event.xconfigure.y = self->frame->area.y + self->frame->size.top -
1969 event.xconfigure.width = w;
1970 event.xconfigure.height = h;
1971 event.xconfigure.border_width = 0;
1972 event.xconfigure.above = self->frame->plate;
1973 event.xconfigure.override_redirect = FALSE;
1974 XSendEvent(event.xconfigure.display, event.xconfigure.window,
1975 FALSE, StructureNotifyMask, &event);
1979 /* if the client is shrinking, then resize the frame before the client */
1980 if (send_resize_client && (w <= oldw || h <= oldh))
1981 XResizeWindow(ob_display, self->window, w, h);
1986 void client_fullscreen(ObClient *self, gboolean fs, gboolean savearea)
1990 if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
1991 self->fullscreen == fs) return; /* already done */
1993 self->fullscreen = fs;
1994 client_change_state(self); /* change the state hints on the client,
1995 and adjust out layer/stacking */
1999 guint32 dimensions[4];
2000 dimensions[0] = self->area.x;
2001 dimensions[1] = self->area.y;
2002 dimensions[2] = self->area.width;
2003 dimensions[3] = self->area.height;
2005 PROP_SETA32(self->window, openbox_premax, cardinal,
2009 /* these are not actually used cuz client_configure will set them
2010 as appropriate when the window is fullscreened */
2017 /* pick some fallbacks... */
2018 a = screen_area_monitor(self->desktop, 0);
2019 x = a->x + a->width / 4;
2020 y = a->y + a->height / 4;
2024 if (PROP_GETA32(self->window, openbox_premax, cardinal,
2025 (guint32**)&dimensions, &num)) {
2036 client_setup_decor_and_functions(self);
2038 client_move_resize(self, x, y, w, h);
2040 /* try focus us when we go into fullscreen mode */
2044 static void client_iconify_recursive(ObClient *self,
2045 gboolean iconic, gboolean curdesk)
2048 gboolean changed = FALSE;
2051 if (self->iconic != iconic) {
2052 ob_debug("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"),
2055 self->iconic = iconic;
2058 if (self->functions & OB_CLIENT_FUNC_ICONIFY) {
2059 self->wmstate = IconicState;
2060 self->ignore_unmaps++;
2061 /* we unmap the client itself so that we can get MapRequest
2062 events, and because the ICCCM tells us to! */
2063 XUnmapWindow(ob_display, self->window);
2065 /* update the focus lists.. iconic windows go to the bottom of
2066 the list, put the new iconic window at the 'top of the
2068 focus_order_to_top(self);
2074 client_set_desktop(self, screen_desktop, FALSE);
2075 self->wmstate = self->shaded ? IconicState : NormalState;
2076 XMapWindow(ob_display, self->window);
2078 /* this puts it after the current focused window */
2079 focus_order_remove(self);
2080 focus_order_add_new(self);
2082 /* this is here cuz with the VIDMODE extension, the viewport can
2083 change while a fullscreen window is iconic, and when it
2084 uniconifies, it would be nice if it did so to the new position
2086 client_reconfigure(self);
2093 client_change_state(self);
2094 client_showhide(self);
2095 screen_update_areas();
2098 /* iconify all transients */
2099 for (it = self->transients; it != NULL; it = it->next)
2100 if (it->data != self) client_iconify_recursive(it->data,
2104 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk)
2106 /* move up the transient chain as far as possible first */
2107 self = client_search_top_transient(self);
2109 client_iconify_recursive(client_search_top_transient(self),
2113 void client_maximize(ObClient *self, gboolean max, int dir, gboolean savearea)
2117 g_assert(dir == 0 || dir == 1 || dir == 2);
2118 if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE)) return; /* can't */
2120 /* check if already done */
2122 if (dir == 0 && self->max_horz && self->max_vert) return;
2123 if (dir == 1 && self->max_horz) return;
2124 if (dir == 2 && self->max_vert) return;
2126 if (dir == 0 && !self->max_horz && !self->max_vert) return;
2127 if (dir == 1 && !self->max_horz) return;
2128 if (dir == 2 && !self->max_vert) return;
2131 /* work with the frame's coords */
2132 x = self->frame->area.x;
2133 y = self->frame->area.y;
2134 w = self->area.width;
2135 h = self->area.height;
2139 gint32 dimensions[4];
2148 /* get the property off the window and use it for the dimensions
2149 we are already maxed on */
2150 if (PROP_GETA32(self->window, openbox_premax, cardinal,
2151 (guint32**)&readdim, &num)) {
2153 if (self->max_horz) {
2154 dimensions[0] = readdim[0];
2155 dimensions[2] = readdim[2];
2157 if (self->max_vert) {
2158 dimensions[1] = readdim[1];
2159 dimensions[3] = readdim[3];
2165 PROP_SETA32(self->window, openbox_premax, cardinal,
2166 (guint32*)dimensions, 4);
2173 /* pick some fallbacks... */
2174 a = screen_area_monitor(self->desktop, 0);
2175 if (dir == 0 || dir == 1) { /* horz */
2176 x = a->x + a->width / 4;
2179 if (dir == 0 || dir == 2) { /* vert */
2180 y = a->y + a->height / 4;
2184 if (PROP_GETA32(self->window, openbox_premax, cardinal,
2185 (guint32**)&dimensions, &num)) {
2187 if (dir == 0 || dir == 1) { /* horz */
2191 if (dir == 0 || dir == 2) { /* vert */
2200 if (dir == 0 || dir == 1) /* horz */
2201 self->max_horz = max;
2202 if (dir == 0 || dir == 2) /* vert */
2203 self->max_vert = max;
2205 if (!self->max_horz && !self->max_vert)
2206 PROP_ERASE(self->window, openbox_premax);
2208 client_change_state(self); /* change the state hints on the client */
2210 client_setup_decor_and_functions(self);
2212 /* figure out where the client should be going */
2213 frame_frame_gravity(self->frame, &x, &y);
2214 client_move_resize(self, x, y, w, h);
2217 void client_shade(ObClient *self, gboolean shade)
2219 if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
2220 shade) || /* can't shade */
2221 self->shaded == shade) return; /* already done */
2223 /* when we're iconic, don't change the wmstate */
2225 self->wmstate = shade ? IconicState : NormalState;
2226 self->shaded = shade;
2227 client_change_state(self);
2228 /* resize the frame to just the titlebar */
2229 frame_adjust_area(self->frame, FALSE, FALSE, FALSE);
2232 void client_close(ObClient *self)
2236 if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
2239 XXX: itd be cool to do timeouts and shit here for killing the client's
2241 like... if the window is around after 5 seconds, then the close button
2242 turns a nice red, and if this function is called again, the client is
2246 ce.xclient.type = ClientMessage;
2247 ce.xclient.message_type = prop_atoms.wm_protocols;
2248 ce.xclient.display = ob_display;
2249 ce.xclient.window = self->window;
2250 ce.xclient.format = 32;
2251 ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
2252 ce.xclient.data.l[1] = event_lasttime;
2253 ce.xclient.data.l[2] = 0l;
2254 ce.xclient.data.l[3] = 0l;
2255 ce.xclient.data.l[4] = 0l;
2256 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2259 void client_kill(ObClient *self)
2261 XKillClient(ob_display, self->window);
2264 void client_set_desktop_recursive(ObClient *self,
2265 guint target, gboolean donthide)
2270 if (target != self->desktop) {
2272 ob_debug("Setting desktop %u\n", target+1);
2274 g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2276 /* remove from the old desktop(s) */
2277 focus_order_remove(self);
2279 old = self->desktop;
2280 self->desktop = target;
2281 PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2282 /* the frame can display the current desktop state */
2283 frame_adjust_state(self->frame);
2284 /* 'move' the window to the new desktop */
2286 client_showhide(self);
2287 /* raise if it was not already on the desktop */
2288 if (old != DESKTOP_ALL)
2289 stacking_raise(CLIENT_AS_WINDOW(self));
2290 screen_update_areas();
2292 /* add to the new desktop(s) */
2293 if (config_focus_new)
2294 focus_order_to_top(self);
2296 focus_order_to_bottom(self);
2299 /* move all transients */
2300 for (it = self->transients; it != NULL; it = it->next)
2301 if (it->data != self) client_set_desktop_recursive(it->data,
2305 void client_set_desktop(ObClient *self, guint target, gboolean donthide)
2307 client_set_desktop_recursive(client_search_top_transient(self),
2311 ObClient *client_search_modal_child(ObClient *self)
2316 for (it = self->transients; it != NULL; it = it->next) {
2317 ObClient *c = it->data;
2318 if ((ret = client_search_modal_child(c))) return ret;
2319 if (c->modal) return c;
2324 gboolean client_validate(ObClient *self)
2328 XSync(ob_display, FALSE); /* get all events on the server */
2330 if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2331 XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2332 XPutBackEvent(ob_display, &e);
2339 void client_set_wm_state(ObClient *self, long state)
2341 if (state == self->wmstate) return; /* no change */
2345 client_iconify(self, TRUE, TRUE);
2348 client_iconify(self, FALSE, TRUE);
2353 void client_set_state(ObClient *self, Atom action, long data1, long data2)
2355 gboolean shaded = self->shaded;
2356 gboolean fullscreen = self->fullscreen;
2357 gboolean max_horz = self->max_horz;
2358 gboolean max_vert = self->max_vert;
2361 if (!(action == prop_atoms.net_wm_state_add ||
2362 action == prop_atoms.net_wm_state_remove ||
2363 action == prop_atoms.net_wm_state_toggle))
2364 /* an invalid action was passed to the client message, ignore it */
2367 for (i = 0; i < 2; ++i) {
2368 Atom state = i == 0 ? data1 : data2;
2370 if (!state) continue;
2372 /* if toggling, then pick whether we're adding or removing */
2373 if (action == prop_atoms.net_wm_state_toggle) {
2374 if (state == prop_atoms.net_wm_state_modal)
2375 action = self->modal ? prop_atoms.net_wm_state_remove :
2376 prop_atoms.net_wm_state_add;
2377 else if (state == prop_atoms.net_wm_state_maximized_vert)
2378 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2379 prop_atoms.net_wm_state_add;
2380 else if (state == prop_atoms.net_wm_state_maximized_horz)
2381 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2382 prop_atoms.net_wm_state_add;
2383 else if (state == prop_atoms.net_wm_state_shaded)
2384 action = self->shaded ? prop_atoms.net_wm_state_remove :
2385 prop_atoms.net_wm_state_add;
2386 else if (state == prop_atoms.net_wm_state_skip_taskbar)
2387 action = self->skip_taskbar ?
2388 prop_atoms.net_wm_state_remove :
2389 prop_atoms.net_wm_state_add;
2390 else if (state == prop_atoms.net_wm_state_skip_pager)
2391 action = self->skip_pager ?
2392 prop_atoms.net_wm_state_remove :
2393 prop_atoms.net_wm_state_add;
2394 else if (state == prop_atoms.net_wm_state_fullscreen)
2395 action = self->fullscreen ?
2396 prop_atoms.net_wm_state_remove :
2397 prop_atoms.net_wm_state_add;
2398 else if (state == prop_atoms.net_wm_state_above)
2399 action = self->above ? prop_atoms.net_wm_state_remove :
2400 prop_atoms.net_wm_state_add;
2401 else if (state == prop_atoms.net_wm_state_below)
2402 action = self->below ? prop_atoms.net_wm_state_remove :
2403 prop_atoms.net_wm_state_add;
2406 if (action == prop_atoms.net_wm_state_add) {
2407 if (state == prop_atoms.net_wm_state_modal) {
2408 /* XXX raise here or something? */
2410 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2412 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2414 } else if (state == prop_atoms.net_wm_state_shaded) {
2416 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2417 self->skip_taskbar = TRUE;
2418 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2419 self->skip_pager = TRUE;
2420 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2422 } else if (state == prop_atoms.net_wm_state_above) {
2424 } else if (state == prop_atoms.net_wm_state_below) {
2428 } else { /* action == prop_atoms.net_wm_state_remove */
2429 if (state == prop_atoms.net_wm_state_modal) {
2430 self->modal = FALSE;
2431 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2433 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2435 } else if (state == prop_atoms.net_wm_state_shaded) {
2437 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2438 self->skip_taskbar = FALSE;
2439 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2440 self->skip_pager = FALSE;
2441 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2443 } else if (state == prop_atoms.net_wm_state_above) {
2444 self->above = FALSE;
2445 } else if (state == prop_atoms.net_wm_state_below) {
2446 self->below = FALSE;
2450 if (max_horz != self->max_horz || max_vert != self->max_vert) {
2451 if (max_horz != self->max_horz && max_vert != self->max_vert) {
2453 if (max_horz == max_vert) { /* both going the same way */
2454 client_maximize(self, max_horz, 0, TRUE);
2456 client_maximize(self, max_horz, 1, TRUE);
2457 client_maximize(self, max_vert, 2, TRUE);
2461 if (max_horz != self->max_horz)
2462 client_maximize(self, max_horz, 1, TRUE);
2464 client_maximize(self, max_vert, 2, TRUE);
2467 /* change fullscreen state before shading, as it will affect if the window
2469 if (fullscreen != self->fullscreen)
2470 client_fullscreen(self, fullscreen, TRUE);
2471 if (shaded != self->shaded)
2472 client_shade(self, shaded);
2473 client_calc_layer(self);
2474 client_change_state(self); /* change the hint to reflect these changes */
2477 ObClient *client_focus_target(ObClient *self)
2481 /* if we have a modal child, then focus it, not us */
2482 child = client_search_modal_child(self);
2483 if (child) return child;
2487 gboolean client_can_focus(ObClient *self)
2491 /* choose the correct target */
2492 self = client_focus_target(self);
2494 if (!self->frame->visible)
2497 if (!((self->can_focus || self->focus_notify) &&
2498 (self->desktop == screen_desktop ||
2499 self->desktop == DESKTOP_ALL) &&
2503 /* do a check to see if the window has already been unmapped or destroyed
2504 do this intelligently while watching out for unmaps we've generated
2505 (ignore_unmaps > 0) */
2506 if (XCheckTypedWindowEvent(ob_display, self->window,
2507 DestroyNotify, &ev)) {
2508 XPutBackEvent(ob_display, &ev);
2511 while (XCheckTypedWindowEvent(ob_display, self->window,
2512 UnmapNotify, &ev)) {
2513 if (self->ignore_unmaps) {
2514 self->ignore_unmaps--;
2516 XPutBackEvent(ob_display, &ev);
2524 gboolean client_focus(ObClient *self)
2526 /* choose the correct target */
2527 self = client_focus_target(self);
2529 if (!client_can_focus(self)) {
2530 if (!self->frame->visible) {
2531 /* update the focus lists */
2532 focus_order_to_top(self);
2537 if (self->can_focus) {
2538 /* RevertToPointerRoot causes much more headache than RevertToNone, so
2539 I choose to use it always, hopefully to find errors quicker, if any
2540 are left. (I hate X. I hate focus events.)
2542 Update: Changing this to RevertToNone fixed a bug with mozilla (bug
2543 #799. So now it is RevertToNone again.
2545 XSetInputFocus(ob_display, self->window, RevertToNone,
2549 if (self->focus_notify) {
2551 ce.xclient.type = ClientMessage;
2552 ce.xclient.message_type = prop_atoms.wm_protocols;
2553 ce.xclient.display = ob_display;
2554 ce.xclient.window = self->window;
2555 ce.xclient.format = 32;
2556 ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2557 ce.xclient.data.l[1] = event_lasttime;
2558 ce.xclient.data.l[2] = 0l;
2559 ce.xclient.data.l[3] = 0l;
2560 ce.xclient.data.l[4] = 0l;
2561 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2565 ob_debug("%sively focusing %lx at %d\n",
2566 (self->can_focus ? "act" : "pass"),
2567 self->window, (int) event_lasttime);
2570 /* Cause the FocusIn to come back to us. Important for desktop switches,
2571 since otherwise we'll have no FocusIn on the queue and send it off to
2572 the focus_backup. */
2573 XSync(ob_display, FALSE);
2577 void client_unfocus(ObClient *self)
2579 g_assert(focus_client == self);
2581 ob_debug("client_unfocus for %lx\n", self->window);
2583 focus_fallback(OB_FOCUS_FALLBACK_UNFOCUSING);
2586 void client_activate(ObClient *self, gboolean here)
2588 if (client_normal(self) && screen_showing_desktop)
2589 screen_show_desktop(FALSE);
2591 client_iconify(self, FALSE, FALSE);
2592 if (self->desktop != DESKTOP_ALL &&
2593 self->desktop != screen_desktop) {
2595 client_set_desktop(self, screen_desktop, FALSE);
2597 screen_set_desktop(self->desktop);
2598 } else if (!self->frame->visible)
2599 /* if its not visible for other reasons, then don't mess
2603 client_shade(self, FALSE);
2605 stacking_raise(CLIENT_AS_WINDOW(self));
2608 gboolean client_focused(ObClient *self)
2610 return self == focus_client;
2613 ObClientIcon *client_icon(ObClient *self, int w, int h)
2616 /* si is the smallest image >= req */
2617 /* li is the largest image < req */
2618 unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2620 for (i = 0; i < self->nicons; ++i) {
2621 size = self->icons[i].width * self->icons[i].height;
2622 if (size < smallest && size >= (unsigned)(w * h)) {
2626 if (size > largest && size <= (unsigned)(w * h)) {
2631 if (largest == 0) /* didnt find one smaller than the requested size */
2632 return &self->icons[si];
2633 return &self->icons[li];
2636 /* this be mostly ripped from fvwm */
2637 ObClient *client_find_directional(ObClient *c, ObDirection dir)
2639 int my_cx, my_cy, his_cx, his_cy;
2642 int score, best_score;
2643 ObClient *best_client, *cur;
2649 /* first, find the centre coords of the currently focused window */
2650 my_cx = c->frame->area.x + c->frame->area.width / 2;
2651 my_cy = c->frame->area.y + c->frame->area.height / 2;
2656 for(it = g_list_first(client_list); it; it = it->next) {
2659 /* the currently selected window isn't interesting */
2662 if (!client_normal(cur))
2664 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2668 if(client_focus_target(cur) == cur &&
2669 !(cur->can_focus || cur->focus_notify))
2672 /* find the centre coords of this window, from the
2673 * currently focused window's point of view */
2674 his_cx = (cur->frame->area.x - my_cx)
2675 + cur->frame->area.width / 2;
2676 his_cy = (cur->frame->area.y - my_cy)
2677 + cur->frame->area.height / 2;
2679 if(dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
2680 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST) {
2682 /* Rotate the diagonals 45 degrees counterclockwise.
2683 * To do this, multiply the matrix /+h +h\ with the
2684 * vector (x y). \-h +h/
2685 * h = sqrt(0.5). We can set h := 1 since absolute
2686 * distance doesn't matter here. */
2687 tx = his_cx + his_cy;
2688 his_cy = -his_cx + his_cy;
2693 case OB_DIRECTION_NORTH:
2694 case OB_DIRECTION_SOUTH:
2695 case OB_DIRECTION_NORTHEAST:
2696 case OB_DIRECTION_SOUTHWEST:
2697 offset = (his_cx < 0) ? -his_cx : his_cx;
2698 distance = ((dir == OB_DIRECTION_NORTH ||
2699 dir == OB_DIRECTION_NORTHEAST) ?
2702 case OB_DIRECTION_EAST:
2703 case OB_DIRECTION_WEST:
2704 case OB_DIRECTION_SOUTHEAST:
2705 case OB_DIRECTION_NORTHWEST:
2706 offset = (his_cy < 0) ? -his_cy : his_cy;
2707 distance = ((dir == OB_DIRECTION_WEST ||
2708 dir == OB_DIRECTION_NORTHWEST) ?
2713 /* the target must be in the requested direction */
2717 /* Calculate score for this window. The smaller the better. */
2718 score = distance + offset;
2720 /* windows more than 45 degrees off the direction are
2721 * heavily penalized and will only be chosen if nothing
2722 * else within a million pixels */
2723 if(offset > distance)
2726 if(best_score == -1 || score < best_score)
2734 void client_set_layer(ObClient *self, int layer)
2738 self->above = FALSE;
2739 } else if (layer == 0) {
2740 self->below = self->above = FALSE;
2742 self->below = FALSE;
2745 client_calc_layer(self);
2746 client_change_state(self); /* reflect this in the state hints */
2749 guint client_monitor(ObClient *self)
2753 for (i = 0; i < screen_num_monitors; ++i) {
2754 Rect *area = screen_physical_area_monitor(i);
2755 if (RECT_INTERSECTS_RECT(*area, self->frame->area))
2758 if (i == screen_num_monitors) i = 0;
2759 g_assert(i < screen_num_monitors);
2763 ObClient *client_search_top_transient(ObClient *self)
2765 /* move up the transient chain as far as possible */
2766 if (self->transient_for) {
2767 if (self->transient_for != OB_TRAN_GROUP) {
2768 return client_search_top_transient(self->transient_for);
2772 for (it = self->group->members; it; it = it->next) {
2773 ObClient *c = it->data;
2775 /* checking transient_for prevents infinate loops! */
2776 if (c != self && !c->transient_for)
2787 ObClient *client_search_transient(ObClient *self, ObClient *search)
2791 for (sit = self->transients; sit; sit = g_slist_next(sit)) {
2792 if (sit->data == search)
2794 if (client_search_transient(sit->data, search))
2800 void client_update_sm_client_id(ObClient *self)
2802 g_free(self->sm_client_id);
2803 self->sm_client_id = NULL;
2805 if (!PROP_GETS(self->window, sm_client_id, locale, &self->sm_client_id) &&
2807 PROP_GETS(self->group->leader, sm_client_id, locale,
2808 &self->sm_client_id);
2811 /* finds the nearest edge in the given direction from the current client
2812 * note to self: the edge is the -frame- edge (the actual one), not the
2815 int client_directional_edge_search(ObClient *c, ObDirection dir)
2818 int my_edge_start, my_edge_end, my_offset;
2825 a = screen_area(c->desktop);
2828 case OB_DIRECTION_NORTH:
2829 my_edge_start = c->frame->area.x;
2830 my_edge_end = c->frame->area.x + c->frame->area.width;
2831 my_offset = c->frame->area.y;
2833 dest = a->y; /* default: top of screen */
2835 for(it = g_list_first(client_list); it; it = it->next) {
2836 int his_edge_start, his_edge_end, his_offset;
2837 ObClient *cur = it->data;
2841 if(!client_normal(cur))
2843 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2848 his_edge_start = cur->frame->area.x;
2849 his_edge_end = cur->frame->area.x + cur->frame->area.width;
2850 his_offset = cur->frame->area.y + cur->frame->area.height;
2852 if(his_offset + 1 > my_offset)
2855 if(his_offset < dest)
2858 if(his_edge_start >= my_edge_start &&
2859 his_edge_start <= my_edge_end)
2862 if(my_edge_start >= his_edge_start &&
2863 my_edge_start <= his_edge_end)
2868 case OB_DIRECTION_SOUTH:
2869 my_edge_start = c->frame->area.x;
2870 my_edge_end = c->frame->area.x + c->frame->area.width;
2871 my_offset = c->frame->area.y + c->frame->area.height;
2873 dest = a->y + a->height; /* default: bottom of screen */
2875 for(it = g_list_first(client_list); it; it = it->next) {
2876 int his_edge_start, his_edge_end, his_offset;
2877 ObClient *cur = it->data;
2881 if(!client_normal(cur))
2883 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2888 his_edge_start = cur->frame->area.x;
2889 his_edge_end = cur->frame->area.x + cur->frame->area.width;
2890 his_offset = cur->frame->area.y;
2893 if(his_offset - 1 < my_offset)
2896 if(his_offset > dest)
2899 if(his_edge_start >= my_edge_start &&
2900 his_edge_start <= my_edge_end)
2903 if(my_edge_start >= his_edge_start &&
2904 my_edge_start <= his_edge_end)
2909 case OB_DIRECTION_WEST:
2910 my_edge_start = c->frame->area.y;
2911 my_edge_end = c->frame->area.y + c->frame->area.height;
2912 my_offset = c->frame->area.x;
2914 dest = a->x; /* default: leftmost egde of screen */
2916 for(it = g_list_first(client_list); it; it = it->next) {
2917 int his_edge_start, his_edge_end, his_offset;
2918 ObClient *cur = it->data;
2922 if(!client_normal(cur))
2924 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2929 his_edge_start = cur->frame->area.y;
2930 his_edge_end = cur->frame->area.y + cur->frame->area.height;
2931 his_offset = cur->frame->area.x + cur->frame->area.width;
2933 if(his_offset + 1 > my_offset)
2936 if(his_offset < dest)
2939 if(his_edge_start >= my_edge_start &&
2940 his_edge_start <= my_edge_end)
2943 if(my_edge_start >= his_edge_start &&
2944 my_edge_start <= his_edge_end)
2950 case OB_DIRECTION_EAST:
2951 my_edge_start = c->frame->area.y;
2952 my_edge_end = c->frame->area.y + c->frame->area.height;
2953 my_offset = c->frame->area.x + c->frame->area.width;
2955 dest = a->x + a->width; /* default: rightmost edge of screen */
2957 for(it = g_list_first(client_list); it; it = it->next) {
2958 int his_edge_start, his_edge_end, his_offset;
2959 ObClient *cur = it->data;
2963 if(!client_normal(cur))
2965 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2970 his_edge_start = cur->frame->area.y;
2971 his_edge_end = cur->frame->area.y + cur->frame->area.height;
2972 his_offset = cur->frame->area.x;
2974 if(his_offset - 1 < my_offset)
2977 if(his_offset > dest)
2980 if(his_edge_start >= my_edge_start &&
2981 his_edge_start <= my_edge_end)
2984 if(my_edge_start >= his_edge_start &&
2985 my_edge_start <= his_edge_end)
2990 case OB_DIRECTION_NORTHEAST:
2991 case OB_DIRECTION_SOUTHEAST:
2992 case OB_DIRECTION_NORTHWEST:
2993 case OB_DIRECTION_SOUTHWEST:
2994 /* not implemented */
2997 g_assert_not_reached();