4 #include "extensions.h"
16 #include <X11/Xutil.h>
18 /*! The event mask to grab on client windows */
19 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
22 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
25 GList *client_list = NULL;
26 GHashTable *client_map = NULL;
28 static Window *client_startup_stack_order = NULL;
29 static guint client_startup_stack_size = 0;
31 static void client_get_all(Client *self);
32 static void client_toggle_border(Client *self, gboolean show);
33 static void client_get_area(Client *self);
34 static void client_get_desktop(Client *self);
35 static void client_get_state(Client *self);
36 static void client_get_shaped(Client *self);
37 static void client_get_mwm_hints(Client *self);
38 static void client_get_gravity(Client *self);
39 static void client_showhide(Client *self);
40 static void client_change_allowed_actions(Client *self);
41 static void client_change_state(Client *self);
42 static void client_move_onscreen(Client *self);
43 static Client *search_focus_tree(Client *node, Client *skip);
44 static void client_apply_startup_state(Client *self);
45 static Client *search_modal_tree(Client *node, Client *skip);
47 static guint map_hash(Window *w) { return *w; }
48 static gboolean map_key_comp(Window *w1, Window *w2) { return *w1 == *w2; }
52 client_map = g_hash_table_new((GHashFunc)map_hash,
53 (GEqualFunc)map_key_comp);
55 /* save the stacking order on startup! */
56 PROP_GETA32(ob_root, net_client_list_stacking, window,
57 &client_startup_stack_order, &client_startup_stack_size);
62 void client_shutdown()
64 g_hash_table_destroy(client_map);
67 void client_set_list()
69 Window *windows, *win_it;
71 guint size = g_list_length(client_list);
73 /* create an array of the window ids */
75 windows = g_new(Window, size);
77 for (it = client_list; it != NULL; it = it->next, ++win_it)
78 *win_it = ((Client*)it->data)->window;
82 PROP_SETA32(ob_root, net_client_list, window, windows, size);
90 void client_manage_all()
92 unsigned int i, j, nchild;
95 XWindowAttributes attrib;
97 XQueryTree(ob_display, ob_root, &w, &w, &children, &nchild);
99 /* remove all icon windows from the list */
100 for (i = 0; i < nchild; i++) {
101 if (children[i] == None) continue;
102 wmhints = XGetWMHints(ob_display, children[i]);
104 if ((wmhints->flags & IconWindowHint) &&
105 (wmhints->icon_window != children[i]))
106 for (j = 0; j < nchild; j++)
107 if (children[j] == wmhints->icon_window) {
115 for (i = 0; i < nchild; ++i) {
116 if (children[i] == None)
118 if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
119 if (attrib.override_redirect) continue;
121 if (attrib.map_state != IsUnmapped)
122 client_manage(children[i]);
127 /* stack them as they were on startup!
128 why with stacking_lower? Why, because then windows who aren't in the
129 stacking list are on the top where you can see them instead of buried
131 for (i = client_startup_stack_size; i > 0; --i) {
134 w = client_startup_stack_order[i-1];
135 c = g_hash_table_lookup(client_map, &w);
136 if (c) stacking_lower(c);
138 g_free(client_startup_stack_order);
139 client_startup_stack_order = NULL;
140 client_startup_stack_size = 0;
142 if (config_focus_new)
143 focus_fallback(Fallback_NoFocus);
146 void client_manage(Window window)
150 XWindowAttributes attrib;
151 XSetWindowAttributes attrib_set;
152 /* XWMHints *wmhint; */
157 /* check if it has already been unmapped by the time we started mapping
158 the grab does a sync so we don't have to here */
159 if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
160 XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
161 XPutBackEvent(ob_display, &e);
164 return; /* don't manage it */
167 /* make sure it isn't an override-redirect window */
168 if (!XGetWindowAttributes(ob_display, window, &attrib) ||
169 attrib.override_redirect) {
171 return; /* don't manage it */
174 /* /\* is the window a docking app *\/
175 if ((wmhint = XGetWMHints(ob_display, window))) {
176 if ((wmhint->flags & StateHint) &&
177 wmhint->initial_state == WithdrawnState) {
178 /\* XXX: make dock apps work! *\/
186 g_message("Managing window: %lx", window);
188 /* choose the events we want to receive on the CLIENT window */
189 attrib_set.event_mask = CLIENT_EVENTMASK;
190 attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
191 XChangeWindowAttributes(ob_display, window,
192 CWEventMask|CWDontPropagate, &attrib_set);
195 /* create the Client struct, and populate it from the hints on the
197 self = g_new(Client, 1);
198 self->window = window;
199 client_get_all(self);
201 /* remove the client's border (and adjust re gravity) */
202 client_toggle_border(self, FALSE);
204 /* specify that if we exit, the window should not be destroyed and should
205 be reparented back to root automatically */
206 XChangeSaveSet(ob_display, window, SetModeInsert);
208 /* create the decoration frame for the client window */
209 self->frame = frame_new();
211 frame_grab_client(self->frame, self);
213 client_apply_startup_state(self);
217 client_list = g_list_append(client_list, self);
218 stacking_list = g_list_append(stacking_list, self);
219 g_assert(!g_hash_table_lookup(client_map, &self->window));
220 g_hash_table_insert(client_map, &self->window, self);
222 /* update the focus lists */
223 if (self->desktop == DESKTOP_ALL) {
224 for (i = 0; i < screen_num_desktops; ++i)
225 focus_order[i] = g_list_append(focus_order[i], self);
228 focus_order[i] = g_list_append(focus_order[i], self);
231 stacking_raise(self);
233 screen_update_struts();
235 dispatch_client(Event_Client_New, self, 0, 0);
237 client_showhide(self);
239 dispatch_client(Event_Client_Mapped, self, 0, 0);
241 /* focus the new window? */
242 if (ob_state != State_Starting && client_normal(self)) {
243 if (config_focus_new)
245 else if (self->transient_for) {
246 if (self->transient_for != TRAN_GROUP) {/* transient of a window */
247 if (focus_client == self->transient_for)
249 } else { /* transient of a group */
252 for (it = self->group->members; it; it = it->next)
253 if (focus_client == it->data) {
261 /* update the list hints */
264 /* make sure the window is visible */
265 client_move_onscreen(self);
267 g_message("Managed window 0x%lx", window);
270 void client_unmanage_all()
272 while (client_list != NULL)
273 client_unmanage(client_list->data);
276 void client_unmanage(Client *self)
282 g_message("Unmanaging window: %lx", self->window);
284 dispatch_client(Event_Client_Destroy, self, 0, 0);
285 g_assert(self != NULL);
287 /* remove the window from our save set */
288 XChangeSaveSet(ob_display, self->window, SetModeDelete);
290 /* we dont want events no more */
291 XSelectInput(ob_display, self->window, NoEventMask);
293 frame_hide(self->frame);
295 client_list = g_list_remove(client_list, self);
296 stacking_list = g_list_remove(stacking_list, self);
297 g_hash_table_remove(client_map, &self->window);
299 /* update the focus lists */
300 if (self->desktop == DESKTOP_ALL) {
301 for (i = 0; i < screen_num_desktops; ++i)
302 focus_order[i] = g_list_remove(focus_order[i], self);
305 focus_order[i] = g_list_remove(focus_order[i], self);
308 /* once the client is out of the list, update the struts to remove it's
310 screen_update_struts();
312 /* tell our parent(s) that we're gone */
313 if (self->transient_for == TRAN_GROUP) { /* transient of group */
316 for (it = self->group->members; it; it = it->next)
317 if (it->data != self)
318 ((Client*)it->data)->transients =
319 g_slist_remove(((Client*)it->data)->transients, self);
320 } else if (self->transient_for) { /* transient of window */
321 self->transient_for->transients =
322 g_slist_remove(self->transient_for->transients, self);
325 /* tell our transients that we're gone */
326 for (it = self->transients; it != NULL; it = it->next) {
327 if (((Client*)it->data)->transient_for != TRAN_GROUP) {
328 ((Client*)it->data)->transient_for = NULL;
329 client_calc_layer(it->data);
333 if (focus_client == self)
334 client_unfocus(self);
336 /* remove from its group */
338 group_remove(self->group, self);
342 /* dispatch the unmapped event */
343 dispatch_client(Event_Client_Unmapped, self, 0, 0);
344 g_assert(self != NULL);
346 /* give the client its border back */
347 client_toggle_border(self, TRUE);
349 /* reparent the window out of the frame, and free the frame */
350 frame_release_client(self->frame, self);
353 if (ob_state != State_Exiting) {
354 /* these values should not be persisted across a window
356 prop_erase(self->window, prop_atoms.net_wm_desktop);
357 prop_erase(self->window, prop_atoms.net_wm_state);
359 /* if we're left in an iconic state, the client wont be mapped. this is
360 bad, since we will no longer be managing the window on restart */
362 XMapWindow(ob_display, self->window);
365 g_message("Unmanaged window 0x%lx", self->window);
367 /* free all data allocated in the client struct */
368 g_slist_free(self->transients);
369 for (j = 0; j < self->nicons; ++j)
370 g_free(self->icons[j].data);
371 if (self->nicons > 0)
374 g_free(self->icon_title);
380 /* update the list hints */
384 static void client_move_onscreen(Client *self)
387 int x = self->frame->area.x, y = self->frame->area.y;
389 a = screen_area(self->desktop);
390 if (x >= a->x + a->width - 1)
391 x = a->x + a->width - self->frame->area.width;
392 if (y >= a->y + a->height - 1)
393 y = a->y + a->height - self->frame->area.height;
394 if (x + self->frame->area.width - 1 < a->x)
396 if (y + self->frame->area.height - 1< a->y)
399 frame_frame_gravity(self->frame, &x, &y); /* get where the client
401 client_configure(self , Corner_TopLeft, x, y,
402 self->area.width, self->area.height,
406 static void client_toggle_border(Client *self, gboolean show)
408 /* adjust our idea of where the client is, based on its border. When the
409 border is removed, the client should now be considered to be in a
411 when re-adding the border to the client, the same operation needs to be
413 int oldx = self->area.x, oldy = self->area.y;
414 int x = oldx, y = oldy;
415 switch(self->gravity) {
417 case NorthWestGravity:
419 case SouthWestGravity:
421 case NorthEastGravity:
423 case SouthEastGravity:
424 if (show) x -= self->border_width * 2;
425 else x += self->border_width * 2;
432 if (show) x -= self->border_width;
433 else x += self->border_width;
436 switch(self->gravity) {
438 case NorthWestGravity:
440 case NorthEastGravity:
442 case SouthWestGravity:
444 case SouthEastGravity:
445 if (show) y -= self->border_width * 2;
446 else y += self->border_width * 2;
453 if (show) y -= self->border_width;
454 else y += self->border_width;
461 XSetWindowBorderWidth(ob_display, self->window, self->border_width);
463 /* move the client so it is back it the right spot _with_ its
465 if (x != oldx || y != oldy)
466 XMoveWindow(ob_display, self->window, x, y);
468 XSetWindowBorderWidth(ob_display, self->window, 0);
472 static void client_get_all(Client *self)
474 /* update EVERYTHING!! */
476 self->ignore_unmaps = 0;
480 self->title = self->icon_title = NULL;
481 self->name = self->class = self->role = NULL;
482 self->wmstate = NormalState;
483 self->transient = FALSE;
484 self->transients = NULL;
485 self->transient_for = NULL;
487 self->urgent = FALSE;
488 self->positioned = FALSE;
489 self->disabled_decorations = 0;
493 client_get_area(self);
494 client_get_desktop(self);
495 client_get_state(self);
496 client_get_shaped(self);
498 client_update_transient_for(self);
499 client_get_mwm_hints(self);
500 client_get_type(self);/* this can change the mwmhints for special cases */
502 client_update_protocols(self);
504 client_get_gravity(self); /* get the attribute gravity */
505 client_update_normal_hints(self); /* this may override the attribute
508 /* got the type, the mwmhints, the protocols, and the normal hints
509 (min/max sizes), so we're ready to set up the decorations/functions */
510 client_setup_decor_and_functions(self);
512 client_update_wmhints(self);
513 client_update_title(self);
514 client_update_icon_title(self);
515 client_update_class(self);
516 client_update_strut(self);
517 client_update_icons(self);
518 client_update_kwm_icon(self);
520 /* this makes sure that these windows appear on all desktops */
521 if (self->type == Type_Desktop)
522 self->desktop = DESKTOP_ALL;
524 /* set the desktop hint, to make sure that it always exists, and to
525 reflect any changes we've made here */
526 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
528 client_change_state(self);
531 static void client_get_area(Client *self)
533 XWindowAttributes wattrib;
536 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
537 g_assert(ret != BadWindow);
539 RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
540 self->border_width = wattrib.border_width;
543 static void client_get_desktop(Client *self)
547 if (PROP_GET32(self->window, net_wm_desktop, cardinal, d)) {
548 if (d >= screen_num_desktops && d != DESKTOP_ALL)
549 d = screen_num_desktops - 1;
552 /* defaults to the current desktop */
553 self->desktop = screen_desktop;
557 static void client_get_state(Client *self)
562 self->modal = self->shaded = self->max_horz = self->max_vert =
563 self->fullscreen = self->above = self->below = self->iconic =
564 self->skip_taskbar = self->skip_pager = FALSE;
566 if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
568 for (i = 0; i < num; ++i) {
569 if (state[i] == prop_atoms.net_wm_state_modal)
571 else if (state[i] == prop_atoms.net_wm_state_shaded)
573 else if (state[i] == prop_atoms.net_wm_state_hidden)
575 else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
576 self->skip_taskbar = TRUE;
577 else if (state[i] == prop_atoms.net_wm_state_skip_pager)
578 self->skip_pager = TRUE;
579 else if (state[i] == prop_atoms.net_wm_state_fullscreen)
580 self->fullscreen = TRUE;
581 else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
582 self->max_vert = TRUE;
583 else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
584 self->max_horz = TRUE;
585 else if (state[i] == prop_atoms.net_wm_state_above)
587 else if (state[i] == prop_atoms.net_wm_state_below)
595 static void client_get_shaped(Client *self)
597 self->shaped = FALSE;
599 if (extensions_shape) {
604 XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
606 XShapeQueryExtents(ob_display, self->window, &s, &foo,
607 &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
609 self->shaped = (s != 0);
614 void client_update_transient_for(Client *self)
619 if (XGetTransientForHint(ob_display, self->window, &t) &&
620 t != self->window) { /* cant be transient to itself! */
621 self->transient = TRUE;
622 c = g_hash_table_lookup(client_map, &t);
623 g_assert(c != self);/* if this happens then we need to check for it*/
625 if (!c && self->group) {
626 /* not transient to a client, see if it is transient for a
628 if (t == self->group->leader ||
631 /* window is a transient for its group! */
636 self->transient = FALSE;
638 /* if anything has changed... */
639 if (c != self->transient_for) {
640 if (self->transient_for == TRAN_GROUP) { /* transient of group */
643 /* remove from old parents */
644 for (it = self->group->members; it; it = it->next)
645 if (it->data != self)
646 ((Client*)it->data)->transients =
647 g_slist_remove(((Client*)it->data)->transients, self);
648 } else if (self->transient_for != NULL) { /* transient of window */
649 /* remove from old parent */
650 self->transient_for->transients =
651 g_slist_remove(self->transient_for->transients, self);
653 self->transient_for = c;
654 if (self->transient_for == TRAN_GROUP) { /* transient of group */
657 /* add to new parents */
658 for (it = self->group->members; it; it = it->next)
659 if (it->data != self)
660 ((Client*)it->data)->transients =
661 g_slist_append(((Client*)it->data)->transients, self);
662 } else if (self->transient_for != NULL) { /* transient of window */
663 /* add to new parent */
664 self->transient_for->transients =
665 g_slist_append(self->transient_for->transients, self);
670 static void client_get_mwm_hints(Client *self)
675 self->mwmhints.flags = 0; /* default to none */
677 if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
679 if (num >= MWM_ELEMENTS) {
680 self->mwmhints.flags = hints[0];
681 self->mwmhints.functions = hints[1];
682 self->mwmhints.decorations = hints[2];
688 void client_get_type(Client *self)
695 if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
696 /* use the first value that we know about in the array */
697 for (i = 0; i < num; ++i) {
698 if (val[i] == prop_atoms.net_wm_window_type_desktop)
699 self->type = Type_Desktop;
700 else if (val[i] == prop_atoms.net_wm_window_type_dock)
701 self->type = Type_Dock;
702 else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
703 self->type = Type_Toolbar;
704 else if (val[i] == prop_atoms.net_wm_window_type_menu)
705 self->type = Type_Menu;
706 else if (val[i] == prop_atoms.net_wm_window_type_utility)
707 self->type = Type_Utility;
708 else if (val[i] == prop_atoms.net_wm_window_type_splash)
709 self->type = Type_Splash;
710 else if (val[i] == prop_atoms.net_wm_window_type_dialog)
711 self->type = Type_Dialog;
712 else if (val[i] == prop_atoms.net_wm_window_type_normal)
713 self->type = Type_Normal;
714 else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
715 /* prevent this window from getting any decor or
717 self->mwmhints.flags &= (MwmFlag_Functions |
718 MwmFlag_Decorations);
719 self->mwmhints.decorations = 0;
720 self->mwmhints.functions = 0;
722 if (self->type != (WindowType) -1)
723 break; /* grab the first legit type */
728 if (self->type == (WindowType) -1) {
729 /*the window type hint was not set, which means we either classify
730 ourself as a normal window or a dialog, depending on if we are a
733 self->type = Type_Dialog;
735 self->type = Type_Normal;
739 void client_update_protocols(Client *self)
744 self->focus_notify = FALSE;
745 self->delete_window = FALSE;
747 if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
748 for (i = 0; i < num_return; ++i) {
749 if (proto[i] == prop_atoms.wm_delete_window) {
750 /* this means we can request the window to close */
751 self->delete_window = TRUE;
752 } else if (proto[i] == prop_atoms.wm_take_focus)
753 /* if this protocol is requested, then the window will be
754 notified whenever we want it to receive focus */
755 self->focus_notify = TRUE;
761 static void client_get_gravity(Client *self)
763 XWindowAttributes wattrib;
766 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
767 g_assert(ret != BadWindow);
768 self->gravity = wattrib.win_gravity;
771 void client_update_normal_hints(Client *self)
775 int oldgravity = self->gravity;
778 self->min_ratio = 0.0f;
779 self->max_ratio = 0.0f;
780 SIZE_SET(self->size_inc, 1, 1);
781 SIZE_SET(self->base_size, 0, 0);
782 SIZE_SET(self->min_size, 0, 0);
783 SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
785 /* get the hints from the window */
786 if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
787 self->positioned = !!(size.flags & (PPosition|USPosition));
789 if (size.flags & PWinGravity) {
790 self->gravity = size.win_gravity;
792 /* if the client has a frame, i.e. has already been mapped and
793 is changing its gravity */
794 if (self->frame && self->gravity != oldgravity) {
795 /* move our idea of the client's position based on its new
797 self->area.x = self->frame->area.x;
798 self->area.y = self->frame->area.y;
799 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
803 if (size.flags & PAspect) {
804 if (size.min_aspect.y)
805 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
806 if (size.max_aspect.y)
807 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
810 if (size.flags & PMinSize)
811 SIZE_SET(self->min_size, size.min_width, size.min_height);
813 if (size.flags & PMaxSize)
814 SIZE_SET(self->max_size, size.max_width, size.max_height);
816 if (size.flags & PBaseSize)
817 SIZE_SET(self->base_size, size.base_width, size.base_height);
819 if (size.flags & PResizeInc)
820 SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
824 void client_setup_decor_and_functions(Client *self)
826 /* start with everything (cept fullscreen) */
827 self->decorations = Decor_Titlebar | Decor_Handle | Decor_Border |
828 Decor_Icon | Decor_AllDesktops | Decor_Iconify | Decor_Maximize |
830 self->functions = Func_Resize | Func_Move | Func_Iconify | Func_Maximize |
832 if (self->delete_window) {
833 self->decorations |= Decor_Close;
834 self->functions |= Func_Close;
837 if (!(self->min_size.width < self->max_size.width ||
838 self->min_size.height < self->max_size.height)) {
839 self->decorations &= ~(Decor_Maximize | Decor_Handle);
840 self->functions &= ~(Func_Resize | Func_Maximize);
843 switch (self->type) {
845 /* normal windows retain all of the possible decorations and
846 functionality, and are the only windows that you can fullscreen */
847 self->functions |= Func_Fullscreen;
852 /* these windows cannot be maximized */
853 self->decorations &= ~Decor_Maximize;
854 self->functions &= ~Func_Maximize;
859 /* these windows get less functionality */
860 self->decorations &= ~(Decor_Iconify | Decor_Handle);
861 self->functions &= ~(Func_Iconify | Func_Resize);
867 /* none of these windows are manipulated by the window manager */
868 self->decorations = 0;
873 /* Mwm Hints are applied subtractively to what has already been chosen for
874 decor and functionality */
875 if (self->mwmhints.flags & MwmFlag_Decorations) {
876 if (! (self->mwmhints.decorations & MwmDecor_All)) {
877 if (! (self->mwmhints.decorations & MwmDecor_Border))
878 self->decorations &= ~Decor_Border;
879 if (! (self->mwmhints.decorations & MwmDecor_Handle))
880 self->decorations &= ~Decor_Handle;
881 if (! (self->mwmhints.decorations & MwmDecor_Title))
882 self->decorations &= ~Decor_Titlebar;
883 if (! (self->mwmhints.decorations & MwmDecor_Iconify))
884 self->decorations &= ~Decor_Iconify;
885 if (! (self->mwmhints.decorations & MwmDecor_Maximize))
886 self->decorations &= ~Decor_Maximize;
890 if (self->mwmhints.flags & MwmFlag_Functions) {
891 if (! (self->mwmhints.functions & MwmFunc_All)) {
892 if (! (self->mwmhints.functions & MwmFunc_Resize))
893 self->functions &= ~Func_Resize;
894 if (! (self->mwmhints.functions & MwmFunc_Move))
895 self->functions &= ~Func_Move;
896 if (! (self->mwmhints.functions & MwmFunc_Iconify))
897 self->functions &= ~Func_Iconify;
898 if (! (self->mwmhints.functions & MwmFunc_Maximize))
899 self->functions &= ~Func_Maximize;
900 /* dont let mwm hints kill the close button
901 if (! (self->mwmhints.functions & MwmFunc_Close))
902 self->functions &= ~Func_Close; */
906 /* can't maximize without moving/resizing */
907 if (!((self->functions & Func_Move) && (self->functions & Func_Resize)))
908 self->functions &= ~(Func_Maximize | Func_Fullscreen);
910 /* finally, user specified disabled decorations are applied to subtract
912 if (self->disabled_decorations & Decor_Titlebar)
913 self->decorations &= ~Decor_Titlebar;
914 if (self->disabled_decorations & Decor_Handle)
915 self->decorations &= ~Decor_Handle;
916 if (self->disabled_decorations & Decor_Border)
917 self->decorations &= ~Decor_Border;
918 if (self->disabled_decorations & Decor_Iconify)
919 self->decorations &= ~Decor_Iconify;
920 if (self->disabled_decorations & Decor_Maximize)
921 self->decorations &= ~Decor_Maximize;
922 if (self->disabled_decorations & Decor_AllDesktops)
923 self->decorations &= ~Decor_AllDesktops;
924 if (self->disabled_decorations & Decor_Shade)
925 self->decorations &= ~Decor_Shade;
926 if (self->disabled_decorations & Decor_Close)
927 self->decorations &= ~Decor_Close;
929 /* if we don't have a titlebar, then we cannot shade! */
930 if (!(self->decorations & Decor_Titlebar))
931 self->functions &= ~Func_Shade;
933 /* now we need to check against rules for the client's current state */
934 if (self->fullscreen) {
935 self->functions &= (Func_Close | Func_Fullscreen | Func_Iconify);
936 self->decorations = 0;
939 client_change_allowed_actions(self);
942 /* change the decors on the frame, and with more/less decorations,
943 we may also need to be repositioned */
944 frame_adjust_area(self->frame, TRUE, TRUE);
945 /* with new decor, the window's maximized size may change */
946 client_remaximize(self);
950 static void client_change_allowed_actions(Client *self)
955 actions[num++] = prop_atoms.net_wm_action_change_desktop;
957 if (self->functions & Func_Shade)
958 actions[num++] = prop_atoms.net_wm_action_shade;
959 if (self->functions & Func_Close)
960 actions[num++] = prop_atoms.net_wm_action_close;
961 if (self->functions & Func_Move)
962 actions[num++] = prop_atoms.net_wm_action_move;
963 if (self->functions & Func_Iconify)
964 actions[num++] = prop_atoms.net_wm_action_minimize;
965 if (self->functions & Func_Resize)
966 actions[num++] = prop_atoms.net_wm_action_resize;
967 if (self->functions & Func_Fullscreen)
968 actions[num++] = prop_atoms.net_wm_action_fullscreen;
969 if (self->functions & Func_Maximize) {
970 actions[num++] = prop_atoms.net_wm_action_maximize_horz;
971 actions[num++] = prop_atoms.net_wm_action_maximize_vert;
974 PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
976 /* make sure the window isn't breaking any rules now */
978 if (!(self->functions & Func_Shade) && self->shaded) {
979 if (self->frame) client_shade(self, FALSE);
980 else self->shaded = FALSE;
982 if (!(self->functions & Func_Iconify) && self->iconic) {
983 g_message("UNSETTING ICONIC");
984 if (self->frame) client_iconify(self, FALSE, TRUE);
985 else self->iconic = FALSE;
987 if (!(self->functions & Func_Fullscreen) && self->fullscreen) {
988 if (self->frame) client_fullscreen(self, FALSE, TRUE);
989 else self->fullscreen = FALSE;
991 if (!(self->functions & Func_Maximize) && (self->max_horz ||
993 if (self->frame) client_maximize(self, FALSE, 0, TRUE);
994 else self->max_vert = self->max_horz = FALSE;
998 void client_remaximize(Client *self)
1001 if (self->max_horz && self->max_vert)
1003 else if (self->max_horz)
1005 else if (self->max_vert)
1008 return; /* not maximized */
1009 self->max_horz = self->max_vert = FALSE;
1010 client_maximize(self, TRUE, dir, FALSE);
1013 void client_update_wmhints(Client *self)
1016 gboolean ur = FALSE;
1018 /* assume a window takes input if it doesnt specify */
1019 self->can_focus = TRUE;
1021 if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1022 if (hints->flags & InputHint)
1023 self->can_focus = hints->input;
1025 /* only do this when first managing the window *AND* when we aren't
1027 if (ob_state != State_Starting && self->frame == NULL)
1028 if (hints->flags & StateHint)
1029 self->iconic = hints->initial_state == IconicState;
1031 if (hints->flags & XUrgencyHint)
1034 if (!(hints->flags & WindowGroupHint))
1035 hints->window_group = None; /* no group */
1036 /* did the group state change? */
1037 if (hints->window_group != (self->group ? self->group->leader : None)){
1038 /* remove from the old group if there was one */
1039 if (self->group != NULL)
1040 group_remove(self->group, self);
1041 if (hints->window_group != None)
1042 self->group = group_add(hints->window_group, self);
1044 /* because the self->transient flag wont change from this call,
1045 we don't need to update the window's type and such, only its
1046 transient_for, and the transients lists of other windows in the
1047 group may be affected */
1048 client_update_transient_for(self);
1051 if (hints->flags & IconPixmapHint) {
1052 client_update_kwm_icon(self);
1053 /* try get the kwm icon first, this is a fallback only */
1054 if (self->pixmap_icon == None) {
1055 self->pixmap_icon = hints->icon_pixmap;
1056 if (hints->flags & IconMaskHint)
1057 self->pixmap_icon_mask = hints->icon_mask;
1059 self->pixmap_icon_mask = None;
1062 frame_adjust_icon(self->frame);
1069 if (ur != self->urgent) {
1071 g_message("Urgent Hint for 0x%lx: %s", self->window,
1073 /* fire the urgent callback if we're mapped, otherwise, wait until
1074 after we're mapped */
1076 dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1080 void client_update_title(Client *self)
1084 g_free(self->title);
1087 if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1088 /* try old x stuff */
1089 if (!PROP_GETS(self->window, wm_name, locale, &data))
1090 data = g_strdup("Unnamed Window");
1092 /* look for duplicates and append a number */
1094 PROP_SETS(self->window, net_wm_visible_name, data);
1099 frame_adjust_title(self->frame);
1102 void client_update_icon_title(Client *self)
1106 g_free(self->icon_title);
1109 if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1110 /* try old x stuff */
1111 if (!PROP_GETS(self->window, wm_icon_name, locale, &data))
1112 data = g_strdup("Unnamed Window");
1114 PROP_SETS(self->window, net_wm_visible_icon_name, data);
1116 self->icon_title = data;
1119 void client_update_class(Client *self)
1124 if (self->name) g_free(self->name);
1125 if (self->class) g_free(self->class);
1126 if (self->role) g_free(self->role);
1128 self->name = self->class = self->role = NULL;
1130 if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1132 self->name = g_strdup(data[0]);
1134 self->class = g_strdup(data[1]);
1140 if (PROP_GETS(self->window, wm_window_role, locale, &s))
1141 self->role = g_strdup(s);
1143 if (self->name == NULL) self->name = g_strdup("");
1144 if (self->class == NULL) self->class = g_strdup("");
1145 if (self->role == NULL) self->role = g_strdup("");
1148 void client_update_strut(Client *self)
1153 if (!PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1154 STRUT_SET(self->strut, 0, 0, 0, 0);
1157 STRUT_SET(self->strut, data[0], data[2], data[1], data[3]);
1159 STRUT_SET(self->strut, 0, 0, 0, 0);
1163 /* updating here is pointless while we're being mapped cuz we're not in
1164 the client list yet */
1166 screen_update_struts();
1169 void client_update_icons(Client *self)
1176 for (j = 0; j < self->nicons; ++j)
1177 g_free(self->icons[j].data);
1178 if (self->nicons > 0)
1179 g_free(self->icons);
1182 if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1183 /* figure out how many valid icons are in here */
1185 while (num - i > 2) {
1193 self->icons = g_new(Icon, self->nicons);
1195 /* store the icons */
1197 for (j = 0; j < self->nicons; ++j) {
1198 w = self->icons[j].width = data[i++];
1199 h = self->icons[j].height = data[i++];
1200 self->icons[j].data =
1201 g_memdup(&data[i], w * h * sizeof(gulong));
1210 frame_adjust_icon(self->frame);
1213 void client_update_kwm_icon(Client *self)
1218 if (!PROP_GETA32(self->window, kwm_win_icon, kwm_win_icon, &data, &num)) {
1219 self->pixmap_icon = self->pixmap_icon_mask = None;
1222 self->pixmap_icon = data[0];
1223 self->pixmap_icon_mask = data[1];
1225 self->pixmap_icon = self->pixmap_icon_mask = None;
1229 frame_adjust_icon(self->frame);
1232 static void client_change_state(Client *self)
1234 unsigned long state[2];
1238 state[0] = self->wmstate;
1240 PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1244 netstate[num++] = prop_atoms.net_wm_state_modal;
1246 netstate[num++] = prop_atoms.net_wm_state_shaded;
1248 netstate[num++] = prop_atoms.net_wm_state_hidden;
1249 if (self->skip_taskbar)
1250 netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1251 if (self->skip_pager)
1252 netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1253 if (self->fullscreen)
1254 netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1256 netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1258 netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1260 netstate[num++] = prop_atoms.net_wm_state_above;
1262 netstate[num++] = prop_atoms.net_wm_state_below;
1263 PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1265 client_calc_layer(self);
1268 frame_adjust_state(self->frame);
1271 static Client *search_focus_tree(Client *node, Client *skip)
1276 for (it = node->transients; it != NULL; it = it->next) {
1277 Client *c = it->data;
1278 if (c == skip) continue; /* circular? */
1279 if ((ret = search_focus_tree(c, skip))) return ret;
1280 if (client_focused(c)) return c;
1285 static void calc_recursive(Client *self, StackLayer l, gboolean raised)
1293 for (it = self->transients; it; it = it->next)
1294 calc_recursive(it->data, l, raised ? raised : l != old);
1296 if (!raised && l != old)
1298 stacking_raise(self);
1301 void client_calc_layer(Client *self)
1306 /* transients take on the layer of their parents */
1307 if (self->transient_for) {
1308 if (self->transient_for != TRAN_GROUP) {
1309 self = self->transient_for;
1313 for (it = self->group->members; it; it = it->next)
1314 if (it->data != self &&
1315 ((Client*)it->data)->transient_for != TRAN_GROUP) {
1316 self = self->transient_for;
1322 /* is us or one of our transients focused? */
1323 if (client_focused(self))
1325 else if (search_focus_tree(self, self))
1330 if (self->iconic) l = Layer_Icon;
1331 /* fullscreen windows are only in the fullscreen layer while focused */
1332 else if (self->fullscreen && f) l = Layer_Fullscreen;
1333 else if (self->type == Type_Desktop) l = Layer_Desktop;
1334 else if (self->type == Type_Dock) {
1335 if (!self->below) l = Layer_Top;
1336 else l = Layer_Normal;
1338 else if (self->above) l = Layer_Above;
1339 else if (self->below) l = Layer_Below;
1340 else l = Layer_Normal;
1342 calc_recursive(self, l, FALSE);
1345 gboolean client_should_show(Client *self)
1347 if (self->iconic) return FALSE;
1348 else if (!(self->desktop == screen_desktop ||
1349 self->desktop == DESKTOP_ALL)) return FALSE;
1350 else if (client_normal(self) && screen_showing_desktop) return FALSE;
1355 static void client_showhide(Client *self)
1358 if (client_should_show(self))
1359 frame_show(self->frame);
1361 frame_hide(self->frame);
1364 gboolean client_normal(Client *self) {
1365 return ! (self->type == Type_Desktop || self->type == Type_Dock ||
1366 self->type == Type_Splash);
1369 static void client_apply_startup_state(Client *self)
1371 /* these are in a carefully crafted order.. */
1374 self->iconic = FALSE;
1375 client_iconify(self, TRUE, FALSE);
1377 if (self->fullscreen) {
1378 self->fullscreen = FALSE;
1379 client_fullscreen(self, TRUE, FALSE);
1382 self->shaded = FALSE;
1383 client_shade(self, TRUE);
1386 dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1388 if (self->max_vert && self->max_horz) {
1389 self->max_vert = self->max_horz = FALSE;
1390 client_maximize(self, TRUE, 0, FALSE);
1391 } else if (self->max_vert) {
1392 self->max_vert = FALSE;
1393 client_maximize(self, TRUE, 2, FALSE);
1394 } else if (self->max_horz) {
1395 self->max_horz = FALSE;
1396 client_maximize(self, TRUE, 1, FALSE);
1399 /* nothing to do for the other states:
1408 void client_configure(Client *self, Corner anchor, int x, int y, int w, int h,
1409 gboolean user, gboolean final)
1411 gboolean moved = FALSE, resized = FALSE;
1413 /* gets the frame's position */
1414 frame_client_gravity(self->frame, &x, &y);
1416 /* these positions are frame positions, not client positions */
1418 /* set the size and position if fullscreen */
1419 if (self->fullscreen) {
1422 w = screen_physical_size.width;
1423 h = screen_physical_size.height;
1424 user = FALSE; /* ignore that increment etc shit when in fullscreen */
1426 /* set the size and position if maximized */
1427 if (self->max_horz) {
1428 x = screen_area(self->desktop)->x - self->frame->size.left;
1429 w = screen_area(self->desktop)->width;
1431 if (self->max_vert) {
1432 y = screen_area(self->desktop)->y;
1433 h = screen_area(self->desktop)->height -
1434 self->frame->size.top - self->frame->size.bottom;
1438 /* gets the client's position */
1439 frame_frame_gravity(self->frame, &x, &y);
1441 /* these override the above states! if you cant move you can't move! */
1443 if (!(self->functions & Func_Move)) {
1447 if (!(self->functions & Func_Resize)) {
1448 w = self->area.width;
1449 h = self->area.height;
1453 if (!(w == self->area.width && h == self->area.height)) {
1454 w -= self->base_size.width;
1455 h -= self->base_size.height;
1458 /* for interactive resizing. have to move half an increment in each
1461 /* how far we are towards the next size inc */
1462 int mw = w % self->size_inc.width;
1463 int mh = h % self->size_inc.height;
1465 int aw = self->size_inc.width / 2;
1466 int ah = self->size_inc.height / 2;
1467 /* don't let us move into a new size increment */
1468 if (mw + aw >= self->size_inc.width)
1469 aw = self->size_inc.width - mw - 1;
1470 if (mh + ah >= self->size_inc.height)
1471 ah = self->size_inc.height - mh - 1;
1475 /* if this is a user-requested resize, then check against min/max
1476 sizes and aspect ratios */
1478 /* smaller than min size or bigger than max size? */
1479 if (w > self->max_size.width) w = self->max_size.width;
1480 if (w < self->min_size.width) w = self->min_size.width;
1481 if (h > self->max_size.height) h = self->max_size.height;
1482 if (h < self->min_size.height) h = self->min_size.height;
1484 /* adjust the height ot match the width for the aspect ratios */
1485 if (self->min_ratio)
1486 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1487 if (self->max_ratio)
1488 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1491 /* keep to the increments */
1492 w /= self->size_inc.width;
1493 h /= self->size_inc.height;
1495 /* you cannot resize to nothing */
1499 /* store the logical size */
1500 SIZE_SET(self->logical_size, w, h);
1502 w *= self->size_inc.width;
1503 h *= self->size_inc.height;
1505 w += self->base_size.width;
1506 h += self->base_size.height;
1510 case Corner_TopLeft:
1512 case Corner_TopRight:
1513 x -= w - self->area.width;
1515 case Corner_BottomLeft:
1516 y -= h - self->area.height;
1518 case Corner_BottomRight:
1519 x -= w - self->area.width;
1520 y -= h - self->area.height;
1524 moved = x != self->area.x || y != self->area.y;
1525 resized = w != self->area.width || h != self->area.height;
1527 RECT_SET(self->area, x, y, w, h);
1530 XResizeWindow(ob_display, self->window, w, h);
1532 /* move/resize the frame to match the request */
1534 if (moved || resized)
1535 frame_adjust_area(self->frame, moved, resized);
1537 if (!user || final) {
1539 event.type = ConfigureNotify;
1540 event.xconfigure.display = ob_display;
1541 event.xconfigure.event = self->window;
1542 event.xconfigure.window = self->window;
1544 /* root window coords with border in mind */
1545 event.xconfigure.x = x - self->border_width +
1546 self->frame->size.left;
1547 event.xconfigure.y = y - self->border_width +
1548 self->frame->size.top;
1550 event.xconfigure.width = self->area.width;
1551 event.xconfigure.height = self->area.height;
1552 event.xconfigure.border_width = self->border_width;
1553 event.xconfigure.above = self->frame->plate;
1554 event.xconfigure.override_redirect = FALSE;
1555 XSendEvent(event.xconfigure.display, event.xconfigure.window,
1556 FALSE, StructureNotifyMask, &event);
1561 void client_fullscreen(Client *self, gboolean fs, gboolean savearea)
1565 if (!(self->functions & Func_Fullscreen) || /* can't */
1566 self->fullscreen == fs) return; /* already done */
1568 self->fullscreen = fs;
1569 client_change_state(self); /* change the state hints on the client,
1570 and adjust out layer/stacking */
1574 guint32 dimensions[4];
1575 dimensions[0] = self->area.x;
1576 dimensions[1] = self->area.y;
1577 dimensions[2] = self->area.width;
1578 dimensions[3] = self->area.height;
1580 PROP_SETA32(self->window, openbox_premax, cardinal,
1584 /* these are not actually used cuz client_configure will set them
1585 as appropriate when the window is fullscreened */
1589 guint32 *dimensions;
1591 /* pick some fallbacks... */
1592 x = screen_area(self->desktop)->x +
1593 screen_area(self->desktop)->width / 4;
1594 y = screen_area(self->desktop)->y +
1595 screen_area(self->desktop)->height / 4;
1596 w = screen_area(self->desktop)->width / 2;
1597 h = screen_area(self->desktop)->height / 2;
1599 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1600 dimensions, &num)) {
1611 client_setup_decor_and_functions(self);
1613 client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1615 /* try focus us when we go into fullscreen mode */
1619 void client_iconify(Client *self, gboolean iconic, gboolean curdesk)
1623 /* move up the transient chain as far as possible first if deiconifying */
1625 while (self->transient_for) {
1626 if (self->transient_for != TRAN_GROUP) {
1627 if (self->transient_for->iconic == iconic)
1629 self = self->transient_for;
1633 /* the check for TRAN_GROUP is to prevent an infinate loop with
1634 2 transients of the same group at the head of the group's
1636 for (it = self->group->members; it; it = it->next) {
1637 Client *c = it->data;
1639 if (c != self && c->transient_for->iconic != iconic &&
1640 c->transient_for != TRAN_GROUP) {
1645 if (it == NULL) break;
1649 if (self->iconic == iconic) return; /* nothing to do */
1651 g_message("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
1654 self->iconic = iconic;
1657 self->wmstate = IconicState;
1658 self->ignore_unmaps++;
1659 /* we unmap the client itself so that we can get MapRequest events,
1660 and because the ICCCM tells us to! */
1661 XUnmapWindow(ob_display, self->window);
1664 client_set_desktop(self, screen_desktop, FALSE);
1665 self->wmstate = self->shaded ? IconicState : NormalState;
1666 XMapWindow(ob_display, self->window);
1668 client_change_state(self);
1669 client_showhide(self);
1670 screen_update_struts();
1672 dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
1675 /* iconify all transients */
1676 for (it = self->transients; it != NULL; it = it->next)
1677 if (it->data != self) client_iconify(it->data, iconic, curdesk);
1680 void client_maximize(Client *self, gboolean max, int dir, gboolean savearea)
1684 g_assert(dir == 0 || dir == 1 || dir == 2);
1685 if (!(self->functions & Func_Maximize)) return; /* can't */
1687 /* check if already done */
1689 if (dir == 0 && self->max_horz && self->max_vert) return;
1690 if (dir == 1 && self->max_horz) return;
1691 if (dir == 2 && self->max_vert) return;
1693 if (dir == 0 && !self->max_horz && !self->max_vert) return;
1694 if (dir == 1 && !self->max_horz) return;
1695 if (dir == 2 && !self->max_vert) return;
1698 /* work with the frame's coords */
1699 x = self->frame->area.x;
1700 y = self->frame->area.y;
1701 w = self->area.width;
1702 h = self->area.height;
1706 gint32 dimensions[4];
1715 /* get the property off the window and use it for the dimensions
1716 we are already maxed on */
1717 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1720 if (self->max_horz) {
1721 dimensions[0] = readdim[0];
1722 dimensions[2] = readdim[2];
1724 if (self->max_vert) {
1725 dimensions[1] = readdim[1];
1726 dimensions[3] = readdim[3];
1732 PROP_SETA32(self->window, openbox_premax, cardinal,
1739 /* pick some fallbacks... */
1740 if (dir == 0 || dir == 1) { /* horz */
1741 x = screen_area(self->desktop)->x +
1742 screen_area(self->desktop)->width / 4;
1743 w = screen_area(self->desktop)->width / 2;
1745 if (dir == 0 || dir == 2) { /* vert */
1746 y = screen_area(self->desktop)->y +
1747 screen_area(self->desktop)->height / 4;
1748 h = screen_area(self->desktop)->height / 2;
1751 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1752 &dimensions, &num)) {
1754 if (dir == 0 || dir == 1) { /* horz */
1758 if (dir == 0 || dir == 2) { /* vert */
1767 if (dir == 0 || dir == 1) /* horz */
1768 self->max_horz = max;
1769 if (dir == 0 || dir == 2) /* vert */
1770 self->max_vert = max;
1772 if (!self->max_horz && !self->max_vert)
1773 PROP_ERASE(self->window, openbox_premax);
1775 client_change_state(self); /* change the state hints on the client */
1777 /* figure out where the client should be going */
1778 frame_frame_gravity(self->frame, &x, &y);
1779 client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1782 void client_shade(Client *self, gboolean shade)
1784 if ((!(self->functions & Func_Shade) && shade) || /* can't shade */
1785 self->shaded == shade) return; /* already done */
1787 /* when we're iconic, don't change the wmstate */
1789 self->wmstate = shade ? IconicState : NormalState;
1790 self->shaded = shade;
1791 client_change_state(self);
1792 /* resize the frame to just the titlebar */
1793 frame_adjust_area(self->frame, FALSE, FALSE);
1796 void client_close(Client *self)
1800 if (!(self->functions & Func_Close)) return;
1803 XXX: itd be cool to do timeouts and shit here for killing the client's
1805 like... if the window is around after 5 seconds, then the close button
1806 turns a nice red, and if this function is called again, the client is
1810 ce.xclient.type = ClientMessage;
1811 ce.xclient.message_type = prop_atoms.wm_protocols;
1812 ce.xclient.display = ob_display;
1813 ce.xclient.window = self->window;
1814 ce.xclient.format = 32;
1815 ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
1816 ce.xclient.data.l[1] = event_lasttime;
1817 ce.xclient.data.l[2] = 0l;
1818 ce.xclient.data.l[3] = 0l;
1819 ce.xclient.data.l[4] = 0l;
1820 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
1823 void client_kill(Client *self)
1825 XKillClient(ob_display, self->window);
1828 void client_set_desktop(Client *self, guint target, gboolean donthide)
1832 if (target == self->desktop) return;
1834 g_message("Setting desktop %u", target);
1836 g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
1838 old = self->desktop;
1839 self->desktop = target;
1840 PROP_SET32(self->window, net_wm_desktop, cardinal, target);
1841 /* the frame can display the current desktop state */
1842 frame_adjust_state(self->frame);
1843 /* 'move' the window to the new desktop */
1845 client_showhide(self);
1846 /* raise if it was not already on the desktop */
1847 if (old != DESKTOP_ALL)
1848 stacking_raise(self);
1849 screen_update_struts();
1851 /* update the focus lists */
1852 if (old == DESKTOP_ALL) {
1853 for (i = 0; i < screen_num_desktops; ++i)
1854 focus_order[i] = g_list_remove(focus_order[i], self);
1856 focus_order[old] = g_list_remove(focus_order[old], self);
1857 if (target == DESKTOP_ALL) {
1858 for (i = 0; i < screen_num_desktops; ++i) {
1859 if (config_focus_new)
1860 focus_order[i] = g_list_prepend(focus_order[i], self);
1862 focus_order[i] = g_list_append(focus_order[i], self);
1865 if (config_focus_new)
1866 focus_order[target] = g_list_prepend(focus_order[target], self);
1868 focus_order[target] = g_list_append(focus_order[target], self);
1871 dispatch_client(Event_Client_Desktop, self, target, old);
1874 static Client *search_modal_tree(Client *node, Client *skip)
1879 for (it = node->transients; it != NULL; it = it->next) {
1880 Client *c = it->data;
1881 if (c == skip) continue; /* circular? */
1882 if ((ret = search_modal_tree(c, skip))) return ret;
1883 if (c->modal) return c;
1888 Client *client_find_modal_child(Client *self)
1890 return search_modal_tree(self, self);
1893 gboolean client_validate(Client *self)
1897 XSync(ob_display, FALSE); /* get all events on the server */
1899 if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
1900 XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
1901 XPutBackEvent(ob_display, &e);
1908 void client_set_wm_state(Client *self, long state)
1910 if (state == self->wmstate) return; /* no change */
1914 client_iconify(self, TRUE, TRUE);
1917 client_iconify(self, FALSE, TRUE);
1922 void client_set_state(Client *self, Atom action, long data1, long data2)
1924 gboolean shaded = self->shaded;
1925 gboolean fullscreen = self->fullscreen;
1926 gboolean max_horz = self->max_horz;
1927 gboolean max_vert = self->max_vert;
1930 if (!(action == prop_atoms.net_wm_state_add ||
1931 action == prop_atoms.net_wm_state_remove ||
1932 action == prop_atoms.net_wm_state_toggle))
1933 /* an invalid action was passed to the client message, ignore it */
1936 for (i = 0; i < 2; ++i) {
1937 Atom state = i == 0 ? data1 : data2;
1939 if (!state) continue;
1941 /* if toggling, then pick whether we're adding or removing */
1942 if (action == prop_atoms.net_wm_state_toggle) {
1943 if (state == prop_atoms.net_wm_state_modal)
1944 action = self->modal ? prop_atoms.net_wm_state_remove :
1945 prop_atoms.net_wm_state_add;
1946 else if (state == prop_atoms.net_wm_state_maximized_vert)
1947 action = self->max_vert ? prop_atoms.net_wm_state_remove :
1948 prop_atoms.net_wm_state_add;
1949 else if (state == prop_atoms.net_wm_state_maximized_horz)
1950 action = self->max_horz ? prop_atoms.net_wm_state_remove :
1951 prop_atoms.net_wm_state_add;
1952 else if (state == prop_atoms.net_wm_state_shaded)
1953 action = self->shaded ? prop_atoms.net_wm_state_remove :
1954 prop_atoms.net_wm_state_add;
1955 else if (state == prop_atoms.net_wm_state_skip_taskbar)
1956 action = self->skip_taskbar ?
1957 prop_atoms.net_wm_state_remove :
1958 prop_atoms.net_wm_state_add;
1959 else if (state == prop_atoms.net_wm_state_skip_pager)
1960 action = self->skip_pager ?
1961 prop_atoms.net_wm_state_remove :
1962 prop_atoms.net_wm_state_add;
1963 else if (state == prop_atoms.net_wm_state_fullscreen)
1964 action = self->fullscreen ?
1965 prop_atoms.net_wm_state_remove :
1966 prop_atoms.net_wm_state_add;
1967 else if (state == prop_atoms.net_wm_state_above)
1968 action = self->above ? prop_atoms.net_wm_state_remove :
1969 prop_atoms.net_wm_state_add;
1970 else if (state == prop_atoms.net_wm_state_below)
1971 action = self->below ? prop_atoms.net_wm_state_remove :
1972 prop_atoms.net_wm_state_add;
1975 if (action == prop_atoms.net_wm_state_add) {
1976 if (state == prop_atoms.net_wm_state_modal) {
1977 /* XXX raise here or something? */
1979 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
1981 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
1983 } else if (state == prop_atoms.net_wm_state_shaded) {
1985 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
1986 self->skip_taskbar = TRUE;
1987 } else if (state == prop_atoms.net_wm_state_skip_pager) {
1988 self->skip_pager = TRUE;
1989 } else if (state == prop_atoms.net_wm_state_fullscreen) {
1991 } else if (state == prop_atoms.net_wm_state_above) {
1993 } else if (state == prop_atoms.net_wm_state_below) {
1997 } else { /* action == prop_atoms.net_wm_state_remove */
1998 if (state == prop_atoms.net_wm_state_modal) {
1999 self->modal = FALSE;
2000 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2002 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2004 } else if (state == prop_atoms.net_wm_state_shaded) {
2006 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2007 self->skip_taskbar = FALSE;
2008 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2009 self->skip_pager = FALSE;
2010 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2012 } else if (state == prop_atoms.net_wm_state_above) {
2013 self->above = FALSE;
2014 } else if (state == prop_atoms.net_wm_state_below) {
2015 self->below = FALSE;
2019 if (max_horz != self->max_horz || max_vert != self->max_vert) {
2020 if (max_horz != self->max_horz && max_vert != self->max_vert) {
2022 if (max_horz == max_vert) { /* both going the same way */
2023 client_maximize(self, max_horz, 0, TRUE);
2025 client_maximize(self, max_horz, 1, TRUE);
2026 client_maximize(self, max_vert, 2, TRUE);
2030 if (max_horz != self->max_horz)
2031 client_maximize(self, max_horz, 1, TRUE);
2033 client_maximize(self, max_vert, 2, TRUE);
2036 /* change fullscreen state before shading, as it will affect if the window
2038 if (fullscreen != self->fullscreen)
2039 client_fullscreen(self, fullscreen, TRUE);
2040 if (shaded != self->shaded)
2041 client_shade(self, shaded);
2042 client_calc_layer(self);
2043 client_change_state(self); /* change the hint to relect these changes */
2046 Client *client_focus_target(Client *self)
2050 /* if we have a modal child, then focus it, not us */
2051 child = client_find_modal_child(self);
2052 if (child) return child;
2056 gboolean client_focusable(Client *self)
2058 /* won't try focus if the client doesn't want it, or if the window isn't
2059 visible on the screen */
2060 return self->frame->visible &&
2061 (self->can_focus || self->focus_notify);
2064 gboolean client_focus(Client *self)
2068 /* choose the correct target */
2069 self = client_focus_target(self);
2071 if (!client_focusable(self))
2074 /* do a check to see if the window has already been unmapped or destroyed
2075 do this intelligently while watching out for unmaps we've generated
2076 (ignore_unmaps > 0) */
2077 if (XCheckTypedWindowEvent(ob_display, self->window,
2078 DestroyNotify, &ev)) {
2079 XPutBackEvent(ob_display, &ev);
2082 while (XCheckTypedWindowEvent(ob_display, self->window,
2083 UnmapNotify, &ev)) {
2084 if (self->ignore_unmaps) {
2085 self->ignore_unmaps--;
2087 XPutBackEvent(ob_display, &ev);
2092 if (self->can_focus)
2093 /* RevertToPointerRoot causes much more headache than TevertToNone, so
2094 I choose to use it always, hopefully to find errors quicker, if any
2095 are left. (I hate X. I hate focus events.) */
2096 XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2099 if (self->focus_notify) {
2101 ce.xclient.type = ClientMessage;
2102 ce.xclient.message_type = prop_atoms.wm_protocols;
2103 ce.xclient.display = ob_display;
2104 ce.xclient.window = self->window;
2105 ce.xclient.format = 32;
2106 ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2107 ce.xclient.data.l[1] = event_lasttime;
2108 ce.xclient.data.l[2] = 0l;
2109 ce.xclient.data.l[3] = 0l;
2110 ce.xclient.data.l[4] = 0l;
2111 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2115 g_message("focusing %lx", self->window);
2118 /* Cause the FocusIn to come back to us. Important for desktop switches,
2119 since otherwise we'll have no FocusIn on the queue and send it off to
2120 the focus_backup. */
2121 XSync(ob_display, FALSE);
2125 void client_unfocus(Client *self)
2127 g_assert(focus_client == self);
2129 g_message("client_unfocus");
2131 focus_fallback(Fallback_Unfocusing);
2134 gboolean client_focused(Client *self)
2136 return self == focus_client;
2139 Icon *client_icon(Client *self, int w, int h)
2142 /* si is the smallest image >= req */
2143 /* li is the largest image < req */
2144 unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2146 if (!self->nicons) return NULL;
2148 for (i = 0; i < self->nicons; ++i) {
2149 size = self->icons[i].width * self->icons[i].height;
2150 if (size < smallest && size >= (unsigned)(w * h)) {
2154 if (size > largest && size <= (unsigned)(w * h)) {
2159 if (largest == 0) /* didnt find one smaller than the requested size */
2160 return &self->icons[si];
2161 return &self->icons[li];