3 #include "moveresize.h"
5 #include "extensions.h"
17 #include <X11/Xutil.h>
19 /*! The event mask to grab on client windows */
20 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
23 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
26 GList *client_list = NULL;
27 GHashTable *client_map = NULL;
29 static Window *client_startup_stack_order = NULL;
30 static guint client_startup_stack_size = 0;
32 static void client_get_all(Client *self);
33 static void client_toggle_border(Client *self, gboolean show);
34 static void client_get_area(Client *self);
35 static void client_get_desktop(Client *self);
36 static void client_get_state(Client *self);
37 static void client_get_shaped(Client *self);
38 static void client_get_mwm_hints(Client *self);
39 static void client_get_gravity(Client *self);
40 static void client_showhide(Client *self);
41 static void client_change_allowed_actions(Client *self);
42 static void client_change_state(Client *self);
43 static void client_move_onscreen(Client *self);
44 static Client *search_focus_tree(Client *node, Client *skip);
45 static void client_apply_startup_state(Client *self);
46 static Client *search_modal_tree(Client *node, Client *skip);
48 static guint map_hash(Window *w) { return *w; }
49 static gboolean map_key_comp(Window *w1, Window *w2) { return *w1 == *w2; }
53 client_map = g_hash_table_new((GHashFunc)map_hash,
54 (GEqualFunc)map_key_comp);
56 /* save the stacking order on startup! */
57 PROP_GETA32(ob_root, net_client_list_stacking, window,
58 (guint32**)&client_startup_stack_order,
59 &client_startup_stack_size);
64 void client_shutdown()
66 g_hash_table_destroy(client_map);
69 void client_set_list()
71 Window *windows, *win_it;
73 guint size = g_list_length(client_list);
75 /* create an array of the window ids */
77 windows = g_new(Window, size);
79 for (it = client_list; it != NULL; it = it->next, ++win_it)
80 *win_it = ((Client*)it->data)->window;
84 PROP_SETA32(ob_root, net_client_list, window, (guint32*)windows, size);
92 void client_manage_all()
94 unsigned int i, j, nchild;
97 XWindowAttributes attrib;
99 XQueryTree(ob_display, ob_root, &w, &w, &children, &nchild);
101 /* remove all icon windows from the list */
102 for (i = 0; i < nchild; i++) {
103 if (children[i] == None) continue;
104 wmhints = XGetWMHints(ob_display, children[i]);
106 if ((wmhints->flags & IconWindowHint) &&
107 (wmhints->icon_window != children[i]))
108 for (j = 0; j < nchild; j++)
109 if (children[j] == wmhints->icon_window) {
117 for (i = 0; i < nchild; ++i) {
118 if (children[i] == None)
120 if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
121 if (attrib.override_redirect) continue;
123 if (attrib.map_state != IsUnmapped)
124 client_manage(children[i]);
129 /* stack them as they were on startup!
130 why with stacking_lower? Why, because then windows who aren't in the
131 stacking list are on the top where you can see them instead of buried
133 for (i = client_startup_stack_size; i > 0; --i) {
136 w = client_startup_stack_order[i-1];
137 c = g_hash_table_lookup(client_map, &w);
138 if (c) stacking_lower(c);
140 g_free(client_startup_stack_order);
141 client_startup_stack_order = NULL;
142 client_startup_stack_size = 0;
144 if (config_focus_new)
145 focus_fallback(Fallback_NoFocus);
148 void client_manage(Window window)
152 XWindowAttributes attrib;
153 XSetWindowAttributes attrib_set;
154 /* XWMHints *wmhint; */
159 /* check if it has already been unmapped by the time we started mapping
160 the grab does a sync so we don't have to here */
161 if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
162 XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
163 XPutBackEvent(ob_display, &e);
166 return; /* don't manage it */
169 /* make sure it isn't an override-redirect window */
170 if (!XGetWindowAttributes(ob_display, window, &attrib) ||
171 attrib.override_redirect) {
173 return; /* don't manage it */
176 /* /\* is the window a docking app *\/
177 if ((wmhint = XGetWMHints(ob_display, window))) {
178 if ((wmhint->flags & StateHint) &&
179 wmhint->initial_state == WithdrawnState) {
180 /\* XXX: make dock apps work! *\/
188 g_message("Managing window: %lx", window);
190 /* choose the events we want to receive on the CLIENT window */
191 attrib_set.event_mask = CLIENT_EVENTMASK;
192 attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
193 XChangeWindowAttributes(ob_display, window,
194 CWEventMask|CWDontPropagate, &attrib_set);
197 /* create the Client struct, and populate it from the hints on the
199 self = g_new(Client, 1);
200 self->window = window;
201 client_get_all(self);
203 /* remove the client's border (and adjust re gravity) */
204 client_toggle_border(self, FALSE);
206 /* specify that if we exit, the window should not be destroyed and should
207 be reparented back to root automatically */
208 XChangeSaveSet(ob_display, window, SetModeInsert);
210 /* create the decoration frame for the client window */
211 self->frame = frame_new();
213 frame_grab_client(self->frame, self);
215 client_apply_startup_state(self);
219 client_list = g_list_append(client_list, self);
220 stacking_list = g_list_append(stacking_list, self);
221 g_assert(!g_hash_table_lookup(client_map, &self->window));
222 g_hash_table_insert(client_map, &self->window, self);
224 /* update the focus lists */
225 if (self->desktop == DESKTOP_ALL) {
226 for (i = 0; i < screen_num_desktops; ++i)
227 focus_order[i] = g_list_insert(focus_order[i], self, 1);
230 focus_order[i] = g_list_insert(focus_order[i], self, 1);
233 stacking_raise(self);
235 screen_update_struts();
237 dispatch_client(Event_Client_New, self, 0, 0);
239 client_showhide(self);
241 /* focus the new window? */
242 if (ob_state != State_Starting) {
244 gboolean group_foc = FALSE;
251 for (it = self->group->members; it; it = it->next)
252 if (client_focused(it->data)) {
257 if (!group_foc && self->transient_for) {
258 if (self->transient_for != TRAN_GROUP) {/* transient of a window */
259 parent = self->transient_for;
260 } else { /* transient of a group */
263 for (it = self->group->members; it; it = it->next)
264 if (it->data != self &&
265 ((Client*)it->data)->transient_for != TRAN_GROUP)
269 /* note the check against Type_Normal, not client_normal(self), which
270 would also include dialog types. in this case we want more strict
272 if ((config_focus_new &&
273 (self->type == Type_Normal ||
274 (self->type == Type_Dialog &&
276 (!parent && (!self->group ||
277 !self->group->members->next)))))) ||
278 (parent && (client_focused(parent) ||
279 search_focus_tree(parent, parent)))) {
284 /* update the list hints */
287 /* make sure the window is visible */
288 client_move_onscreen(self);
290 dispatch_client(Event_Client_Mapped, self, 0, 0);
292 g_message("Managed window 0x%lx", window);
295 void client_unmanage_all()
297 while (client_list != NULL)
298 client_unmanage(client_list->data);
301 void client_unmanage(Client *self)
307 g_message("Unmanaging window: %lx", self->window);
309 dispatch_client(Event_Client_Destroy, self, 0, 0);
310 g_assert(self != NULL);
312 /* remove the window from our save set */
313 XChangeSaveSet(ob_display, self->window, SetModeDelete);
315 /* we dont want events no more */
316 XSelectInput(ob_display, self->window, NoEventMask);
318 frame_hide(self->frame);
320 client_list = g_list_remove(client_list, self);
321 stacking_list = g_list_remove(stacking_list, self);
322 g_hash_table_remove(client_map, &self->window);
324 /* update the focus lists */
325 if (self->desktop == DESKTOP_ALL) {
326 for (i = 0; i < screen_num_desktops; ++i)
327 focus_order[i] = g_list_remove(focus_order[i], self);
330 focus_order[i] = g_list_remove(focus_order[i], self);
333 /* once the client is out of the list, update the struts to remove it's
335 screen_update_struts();
337 /* tell our parent(s) that we're gone */
338 if (self->transient_for == TRAN_GROUP) { /* transient of group */
341 for (it = self->group->members; it; it = it->next)
342 if (it->data != self)
343 ((Client*)it->data)->transients =
344 g_slist_remove(((Client*)it->data)->transients, self);
345 } else if (self->transient_for) { /* transient of window */
346 self->transient_for->transients =
347 g_slist_remove(self->transient_for->transients, self);
350 /* tell our transients that we're gone */
351 for (it = self->transients; it != NULL; it = it->next) {
352 if (((Client*)it->data)->transient_for != TRAN_GROUP) {
353 ((Client*)it->data)->transient_for = NULL;
354 client_calc_layer(it->data);
358 if (moveresize_client == self)
359 moveresize_end(TRUE);
361 if (focus_client == self) {
364 /* focus the last focused window on the desktop, and ignore enter
365 events from the unmap so it doesnt mess with the focus */
366 while (XCheckTypedEvent(ob_display, EnterNotify, &e));
367 client_unfocus(self);
370 /* remove from its group */
372 group_remove(self->group, self);
376 /* dispatch the unmapped event */
377 dispatch_client(Event_Client_Unmapped, self, 0, 0);
378 g_assert(self != NULL);
380 /* give the client its border back */
381 client_toggle_border(self, TRUE);
383 /* reparent the window out of the frame, and free the frame */
384 frame_release_client(self->frame, self);
387 if (ob_state != State_Exiting) {
388 /* these values should not be persisted across a window
390 prop_erase(self->window, prop_atoms.net_wm_desktop);
391 prop_erase(self->window, prop_atoms.net_wm_state);
393 /* if we're left in an iconic state, the client wont be mapped. this is
394 bad, since we will no longer be managing the window on restart */
396 XMapWindow(ob_display, self->window);
400 g_message("Unmanaged window 0x%lx", self->window);
402 /* free all data allocated in the client struct */
403 g_slist_free(self->transients);
404 for (j = 0; j < self->nicons; ++j)
405 g_free(self->icons[j].data);
406 if (self->nicons > 0)
409 g_free(self->icon_title);
415 /* update the list hints */
419 static void client_move_onscreen(Client *self)
422 int x = self->frame->area.x, y = self->frame->area.y;
424 a = screen_area(self->desktop);
425 if (x >= a->x + a->width - 1)
426 x = a->x + a->width - self->frame->area.width;
427 if (y >= a->y + a->height - 1)
428 y = a->y + a->height - self->frame->area.height;
429 if (x + self->frame->area.width - 1 < a->x)
431 if (y + self->frame->area.height - 1< a->y)
434 frame_frame_gravity(self->frame, &x, &y); /* get where the client
436 client_configure(self , Corner_TopLeft, x, y,
437 self->area.width, self->area.height,
441 static void client_toggle_border(Client *self, gboolean show)
443 /* adjust our idea of where the client is, based on its border. When the
444 border is removed, the client should now be considered to be in a
446 when re-adding the border to the client, the same operation needs to be
448 int oldx = self->area.x, oldy = self->area.y;
449 int x = oldx, y = oldy;
450 switch(self->gravity) {
452 case NorthWestGravity:
454 case SouthWestGravity:
456 case NorthEastGravity:
458 case SouthEastGravity:
459 if (show) x -= self->border_width * 2;
460 else x += self->border_width * 2;
467 if (show) x -= self->border_width;
468 else x += self->border_width;
471 switch(self->gravity) {
473 case NorthWestGravity:
475 case NorthEastGravity:
477 case SouthWestGravity:
479 case SouthEastGravity:
480 if (show) y -= self->border_width * 2;
481 else y += self->border_width * 2;
488 if (show) y -= self->border_width;
489 else y += self->border_width;
496 XSetWindowBorderWidth(ob_display, self->window, self->border_width);
498 /* move the client so it is back it the right spot _with_ its
500 if (x != oldx || y != oldy)
501 XMoveWindow(ob_display, self->window, x, y);
503 XSetWindowBorderWidth(ob_display, self->window, 0);
507 static void client_get_all(Client *self)
509 /* update EVERYTHING!! */
511 self->ignore_unmaps = 0;
515 self->title = self->icon_title = NULL;
516 self->name = self->class = self->role = NULL;
517 self->wmstate = NormalState;
518 self->transient = FALSE;
519 self->transients = NULL;
520 self->transient_for = NULL;
522 self->urgent = FALSE;
523 self->positioned = FALSE;
524 self->disabled_decorations = 0;
528 client_get_area(self);
529 client_update_transient_for(self);
530 client_update_wmhints(self);
531 client_get_desktop(self);
532 client_get_state(self);
533 client_get_shaped(self);
535 client_get_mwm_hints(self);
536 client_get_type(self);/* this can change the mwmhints for special cases */
538 client_update_protocols(self);
540 client_get_gravity(self); /* get the attribute gravity */
541 client_update_normal_hints(self); /* this may override the attribute
544 /* got the type, the mwmhints, the protocols, and the normal hints
545 (min/max sizes), so we're ready to set up the decorations/functions */
546 client_setup_decor_and_functions(self);
548 client_update_title(self);
549 client_update_icon_title(self);
550 client_update_class(self);
551 client_update_strut(self);
552 client_update_icons(self);
553 client_update_kwm_icon(self);
555 client_change_state(self);
558 static void client_get_area(Client *self)
560 XWindowAttributes wattrib;
563 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
564 g_assert(ret != BadWindow);
566 RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
567 self->border_width = wattrib.border_width;
570 static void client_get_desktop(Client *self)
574 if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
575 if (d >= screen_num_desktops && d != DESKTOP_ALL)
576 d = screen_num_desktops - 1;
579 gboolean trdesk = FALSE;
581 if (self->transient_for) {
582 if (self->transient_for != TRAN_GROUP) {
583 self->desktop = self->transient_for->desktop;
588 for (it = self->group->members; it; it = it->next)
589 if (it->data != self &&
590 ((Client*)it->data)->transient_for != TRAN_GROUP) {
591 self->desktop = ((Client*)it->data)->desktop;
598 /* defaults to the current desktop */
599 self->desktop = screen_desktop;
601 /* set the desktop hint, to make sure that it always exists */
602 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
606 static void client_get_state(Client *self)
611 self->modal = self->shaded = self->max_horz = self->max_vert =
612 self->fullscreen = self->above = self->below = self->iconic =
613 self->skip_taskbar = self->skip_pager = FALSE;
615 if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
617 for (i = 0; i < num; ++i) {
618 if (state[i] == prop_atoms.net_wm_state_modal)
620 else if (state[i] == prop_atoms.net_wm_state_shaded)
622 else if (state[i] == prop_atoms.net_wm_state_hidden)
624 else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
625 self->skip_taskbar = TRUE;
626 else if (state[i] == prop_atoms.net_wm_state_skip_pager)
627 self->skip_pager = TRUE;
628 else if (state[i] == prop_atoms.net_wm_state_fullscreen)
629 self->fullscreen = TRUE;
630 else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
631 self->max_vert = TRUE;
632 else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
633 self->max_horz = TRUE;
634 else if (state[i] == prop_atoms.net_wm_state_above)
636 else if (state[i] == prop_atoms.net_wm_state_below)
644 static void client_get_shaped(Client *self)
646 self->shaped = FALSE;
648 if (extensions_shape) {
653 XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
655 XShapeQueryExtents(ob_display, self->window, &s, &foo,
656 &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
658 self->shaped = (s != 0);
663 void client_update_transient_for(Client *self)
668 if (XGetTransientForHint(ob_display, self->window, &t) &&
669 t != self->window) { /* cant be transient to itself! */
670 self->transient = TRUE;
671 c = g_hash_table_lookup(client_map, &t);
672 g_assert(c != self);/* if this happens then we need to check for it*/
674 if (!c && self->group) {
675 /* not transient to a client, see if it is transient for a
677 if (t == self->group->leader ||
680 /* window is a transient for its group! */
685 self->transient = FALSE;
687 /* if anything has changed... */
688 if (c != self->transient_for) {
689 if (self->transient_for == TRAN_GROUP) { /* transient of group */
692 /* remove from old parents */
693 for (it = self->group->members; it; it = it->next)
694 if (it->data != self)
695 ((Client*)it->data)->transients =
696 g_slist_remove(((Client*)it->data)->transients, self);
697 } else if (self->transient_for != NULL) { /* transient of window */
698 /* remove from old parent */
699 self->transient_for->transients =
700 g_slist_remove(self->transient_for->transients, self);
702 self->transient_for = c;
703 if (self->transient_for == TRAN_GROUP) { /* transient of group */
706 /* add to new parents */
707 for (it = self->group->members; it; it = it->next)
708 if (it->data != self)
709 ((Client*)it->data)->transients =
710 g_slist_append(((Client*)it->data)->transients, self);
711 } else if (self->transient_for != NULL) { /* transient of window */
712 /* add to new parent */
713 self->transient_for->transients =
714 g_slist_append(self->transient_for->transients, self);
719 static void client_get_mwm_hints(Client *self)
724 self->mwmhints.flags = 0; /* default to none */
726 if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
728 if (num >= MWM_ELEMENTS) {
729 self->mwmhints.flags = hints[0];
730 self->mwmhints.functions = hints[1];
731 self->mwmhints.decorations = hints[2];
737 void client_get_type(Client *self)
744 if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
745 /* use the first value that we know about in the array */
746 for (i = 0; i < num; ++i) {
747 if (val[i] == prop_atoms.net_wm_window_type_desktop)
748 self->type = Type_Desktop;
749 else if (val[i] == prop_atoms.net_wm_window_type_dock)
750 self->type = Type_Dock;
751 else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
752 self->type = Type_Toolbar;
753 else if (val[i] == prop_atoms.net_wm_window_type_menu)
754 self->type = Type_Menu;
755 else if (val[i] == prop_atoms.net_wm_window_type_utility)
756 self->type = Type_Utility;
757 else if (val[i] == prop_atoms.net_wm_window_type_splash)
758 self->type = Type_Splash;
759 else if (val[i] == prop_atoms.net_wm_window_type_dialog)
760 self->type = Type_Dialog;
761 else if (val[i] == prop_atoms.net_wm_window_type_normal)
762 self->type = Type_Normal;
763 else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
764 /* prevent this window from getting any decor or
766 self->mwmhints.flags &= (MwmFlag_Functions |
767 MwmFlag_Decorations);
768 self->mwmhints.decorations = 0;
769 self->mwmhints.functions = 0;
771 if (self->type != (WindowType) -1)
772 break; /* grab the first legit type */
777 if (self->type == (WindowType) -1) {
778 /*the window type hint was not set, which means we either classify
779 ourself as a normal window or a dialog, depending on if we are a
782 self->type = Type_Dialog;
784 self->type = Type_Normal;
787 /* this makes sure that these windows appear on all desktops */
788 if (self->type == Type_Desktop)
789 self->desktop = DESKTOP_ALL;
792 void client_update_protocols(Client *self)
797 self->focus_notify = FALSE;
798 self->delete_window = FALSE;
800 if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
801 for (i = 0; i < num_return; ++i) {
802 if (proto[i] == prop_atoms.wm_delete_window) {
803 /* this means we can request the window to close */
804 self->delete_window = TRUE;
805 } else if (proto[i] == prop_atoms.wm_take_focus)
806 /* if this protocol is requested, then the window will be
807 notified whenever we want it to receive focus */
808 self->focus_notify = TRUE;
814 static void client_get_gravity(Client *self)
816 XWindowAttributes wattrib;
819 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
820 g_assert(ret != BadWindow);
821 self->gravity = wattrib.win_gravity;
824 void client_update_normal_hints(Client *self)
828 int oldgravity = self->gravity;
831 self->min_ratio = 0.0f;
832 self->max_ratio = 0.0f;
833 SIZE_SET(self->size_inc, 1, 1);
834 SIZE_SET(self->base_size, 0, 0);
835 SIZE_SET(self->min_size, 0, 0);
836 SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
838 /* get the hints from the window */
839 if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
840 self->positioned = !!(size.flags & (PPosition|USPosition));
842 if (size.flags & PWinGravity) {
843 self->gravity = size.win_gravity;
845 /* if the client has a frame, i.e. has already been mapped and
846 is changing its gravity */
847 if (self->frame && self->gravity != oldgravity) {
848 /* move our idea of the client's position based on its new
850 self->area.x = self->frame->area.x;
851 self->area.y = self->frame->area.y;
852 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
856 if (size.flags & PAspect) {
857 if (size.min_aspect.y)
858 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
859 if (size.max_aspect.y)
860 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
863 if (size.flags & PMinSize)
864 SIZE_SET(self->min_size, size.min_width, size.min_height);
866 if (size.flags & PMaxSize)
867 SIZE_SET(self->max_size, size.max_width, size.max_height);
869 if (size.flags & PBaseSize)
870 SIZE_SET(self->base_size, size.base_width, size.base_height);
872 if (size.flags & PResizeInc)
873 SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
877 void client_setup_decor_and_functions(Client *self)
879 /* start with everything (cept fullscreen) */
880 self->decorations = Decor_Titlebar | Decor_Handle | Decor_Border |
881 Decor_Icon | Decor_AllDesktops | Decor_Iconify | Decor_Maximize |
883 self->functions = Func_Resize | Func_Move | Func_Iconify | Func_Maximize |
885 if (self->delete_window) {
886 self->decorations |= Decor_Close;
887 self->functions |= Func_Close;
890 if (!(self->min_size.width < self->max_size.width ||
891 self->min_size.height < self->max_size.height)) {
892 self->decorations &= ~(Decor_Maximize | Decor_Handle);
893 self->functions &= ~(Func_Resize | Func_Maximize);
896 switch (self->type) {
898 /* normal windows retain all of the possible decorations and
899 functionality, and are the only windows that you can fullscreen */
900 self->functions |= Func_Fullscreen;
905 /* these windows cannot be maximized */
906 self->decorations &= ~Decor_Maximize;
907 self->functions &= ~Func_Maximize;
912 /* these windows get less functionality */
913 self->decorations &= ~(Decor_Iconify | Decor_Handle);
914 self->functions &= ~(Func_Iconify | Func_Resize);
920 /* none of these windows are manipulated by the window manager */
921 self->decorations = 0;
926 /* Mwm Hints are applied subtractively to what has already been chosen for
927 decor and functionality */
928 if (self->mwmhints.flags & MwmFlag_Decorations) {
929 if (! (self->mwmhints.decorations & MwmDecor_All)) {
930 if (! (self->mwmhints.decorations & MwmDecor_Border))
931 self->decorations &= ~Decor_Border;
932 if (! (self->mwmhints.decorations & MwmDecor_Handle))
933 self->decorations &= ~Decor_Handle;
934 if (! (self->mwmhints.decorations & MwmDecor_Title))
935 self->decorations &= ~Decor_Titlebar;
936 if (! (self->mwmhints.decorations & MwmDecor_Iconify))
937 self->decorations &= ~Decor_Iconify;
938 if (! (self->mwmhints.decorations & MwmDecor_Maximize))
939 self->decorations &= ~Decor_Maximize;
943 if (self->mwmhints.flags & MwmFlag_Functions) {
944 if (! (self->mwmhints.functions & MwmFunc_All)) {
945 if (! (self->mwmhints.functions & MwmFunc_Resize))
946 self->functions &= ~Func_Resize;
947 if (! (self->mwmhints.functions & MwmFunc_Move))
948 self->functions &= ~Func_Move;
949 if (! (self->mwmhints.functions & MwmFunc_Iconify))
950 self->functions &= ~Func_Iconify;
951 if (! (self->mwmhints.functions & MwmFunc_Maximize))
952 self->functions &= ~Func_Maximize;
953 /* dont let mwm hints kill the close button
954 if (! (self->mwmhints.functions & MwmFunc_Close))
955 self->functions &= ~Func_Close; */
959 /* can't maximize without moving/resizing */
960 if (!((self->functions & Func_Move) && (self->functions & Func_Resize)))
961 self->functions &= ~(Func_Maximize | Func_Fullscreen);
963 /* finally, user specified disabled decorations are applied to subtract
965 if (self->disabled_decorations & Decor_Titlebar)
966 self->decorations &= ~Decor_Titlebar;
967 if (self->disabled_decorations & Decor_Handle)
968 self->decorations &= ~Decor_Handle;
969 if (self->disabled_decorations & Decor_Border)
970 self->decorations &= ~Decor_Border;
971 if (self->disabled_decorations & Decor_Iconify)
972 self->decorations &= ~Decor_Iconify;
973 if (self->disabled_decorations & Decor_Maximize)
974 self->decorations &= ~Decor_Maximize;
975 if (self->disabled_decorations & Decor_AllDesktops)
976 self->decorations &= ~Decor_AllDesktops;
977 if (self->disabled_decorations & Decor_Shade)
978 self->decorations &= ~Decor_Shade;
979 if (self->disabled_decorations & Decor_Close)
980 self->decorations &= ~Decor_Close;
982 /* if we don't have a titlebar, then we cannot shade! */
983 if (!(self->decorations & Decor_Titlebar))
984 self->functions &= ~Func_Shade;
986 /* now we need to check against rules for the client's current state */
987 if (self->fullscreen) {
988 self->functions &= (Func_Close | Func_Fullscreen | Func_Iconify);
989 self->decorations = 0;
992 client_change_allowed_actions(self);
995 /* change the decors on the frame, and with more/less decorations,
996 we may also need to be repositioned */
997 frame_adjust_area(self->frame, TRUE, TRUE);
998 /* with new decor, the window's maximized size may change */
999 client_remaximize(self);
1003 static void client_change_allowed_actions(Client *self)
1008 actions[num++] = prop_atoms.net_wm_action_change_desktop;
1010 if (self->functions & Func_Shade)
1011 actions[num++] = prop_atoms.net_wm_action_shade;
1012 if (self->functions & Func_Close)
1013 actions[num++] = prop_atoms.net_wm_action_close;
1014 if (self->functions & Func_Move)
1015 actions[num++] = prop_atoms.net_wm_action_move;
1016 if (self->functions & Func_Iconify)
1017 actions[num++] = prop_atoms.net_wm_action_minimize;
1018 if (self->functions & Func_Resize)
1019 actions[num++] = prop_atoms.net_wm_action_resize;
1020 if (self->functions & Func_Fullscreen)
1021 actions[num++] = prop_atoms.net_wm_action_fullscreen;
1022 if (self->functions & Func_Maximize) {
1023 actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1024 actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1027 PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1029 /* make sure the window isn't breaking any rules now */
1031 if (!(self->functions & Func_Shade) && self->shaded) {
1032 if (self->frame) client_shade(self, FALSE);
1033 else self->shaded = FALSE;
1035 if (!(self->functions & Func_Iconify) && self->iconic) {
1036 g_message("UNSETTING ICONIC");
1037 if (self->frame) client_iconify(self, FALSE, TRUE);
1038 else self->iconic = FALSE;
1040 if (!(self->functions & Func_Fullscreen) && self->fullscreen) {
1041 if (self->frame) client_fullscreen(self, FALSE, TRUE);
1042 else self->fullscreen = FALSE;
1044 if (!(self->functions & Func_Maximize) && (self->max_horz ||
1046 if (self->frame) client_maximize(self, FALSE, 0, TRUE);
1047 else self->max_vert = self->max_horz = FALSE;
1051 void client_remaximize(Client *self)
1054 if (self->max_horz && self->max_vert)
1056 else if (self->max_horz)
1058 else if (self->max_vert)
1061 return; /* not maximized */
1062 self->max_horz = self->max_vert = FALSE;
1063 client_maximize(self, TRUE, dir, FALSE);
1066 void client_update_wmhints(Client *self)
1069 gboolean ur = FALSE;
1072 /* assume a window takes input if it doesnt specify */
1073 self->can_focus = TRUE;
1075 if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1076 if (hints->flags & InputHint)
1077 self->can_focus = hints->input;
1079 /* only do this when first managing the window *AND* when we aren't
1081 if (ob_state != State_Starting && self->frame == NULL)
1082 if (hints->flags & StateHint)
1083 self->iconic = hints->initial_state == IconicState;
1085 if (hints->flags & XUrgencyHint)
1088 if (!(hints->flags & WindowGroupHint))
1089 hints->window_group = None;
1091 /* did the group state change? */
1092 if (hints->window_group !=
1093 (self->group ? self->group->leader : None)) {
1094 /* remove from the old group if there was one */
1095 if (self->group != NULL) {
1096 /* remove transients of the group */
1097 for (it = self->group->members; it; it = it->next)
1098 if (it->data != self &&
1099 ((Client*)it->data)->transient_for == TRAN_GROUP) {
1100 self->transients = g_slist_remove(self->transients,
1103 group_remove(self->group, self);
1106 if (hints->window_group != None) {
1107 self->group = group_add(hints->window_group, self);
1109 /* add other transients of the group that are already
1111 for (it = self->group->members; it; it = it->next)
1112 if (it->data != self &&
1113 ((Client*)it->data)->transient_for == TRAN_GROUP)
1114 self->transients = g_slist_append(self->transients,
1118 /* because the self->transient flag wont change from this call,
1119 we don't need to update the window's type and such, only its
1120 transient_for, and the transients lists of other windows in
1121 the group may be affected */
1122 client_update_transient_for(self);
1125 client_update_kwm_icon(self);
1126 /* try get the kwm icon first, this is a fallback only */
1127 if (self->pixmap_icon == None) {
1128 if (hints->flags & IconPixmapHint) {
1129 if (self->pixmap_icon == None) {
1130 self->pixmap_icon = hints->icon_pixmap;
1131 if (hints->flags & IconMaskHint)
1132 self->pixmap_icon_mask = hints->icon_mask;
1134 self->pixmap_icon_mask = None;
1137 frame_adjust_icon(self->frame);
1145 if (ur != self->urgent) {
1147 g_message("Urgent Hint for 0x%lx: %s", self->window,
1149 /* fire the urgent callback if we're mapped, otherwise, wait until
1150 after we're mapped */
1152 dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1156 void client_update_title(Client *self)
1160 g_free(self->title);
1163 if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1164 /* try old x stuff */
1165 if (!PROP_GETS(self->window, wm_name, locale, &data))
1166 data = g_strdup("Unnamed Window");
1168 /* look for duplicates and append a number */
1170 PROP_SETS(self->window, net_wm_visible_name, data);
1175 frame_adjust_title(self->frame);
1178 void client_update_icon_title(Client *self)
1182 g_free(self->icon_title);
1185 if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1186 /* try old x stuff */
1187 if (!PROP_GETS(self->window, wm_icon_name, locale, &data))
1188 data = g_strdup("Unnamed Window");
1190 PROP_SETS(self->window, net_wm_visible_icon_name, data);
1192 self->icon_title = data;
1195 void client_update_class(Client *self)
1200 if (self->name) g_free(self->name);
1201 if (self->class) g_free(self->class);
1202 if (self->role) g_free(self->role);
1204 self->name = self->class = self->role = NULL;
1206 if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1208 self->name = g_strdup(data[0]);
1210 self->class = g_strdup(data[1]);
1215 if (PROP_GETS(self->window, wm_window_role, locale, &s))
1216 self->role = g_strdup(s);
1218 if (self->name == NULL) self->name = g_strdup("");
1219 if (self->class == NULL) self->class = g_strdup("");
1220 if (self->role == NULL) self->role = g_strdup("");
1223 void client_update_strut(Client *self)
1228 if (!PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1229 STRUT_SET(self->strut, 0, 0, 0, 0);
1232 STRUT_SET(self->strut, data[0], data[2], data[1], data[3]);
1234 STRUT_SET(self->strut, 0, 0, 0, 0);
1238 /* updating here is pointless while we're being mapped cuz we're not in
1239 the client list yet */
1241 screen_update_struts();
1244 void client_update_icons(Client *self)
1251 for (j = 0; j < self->nicons; ++j)
1252 g_free(self->icons[j].data);
1253 if (self->nicons > 0)
1254 g_free(self->icons);
1257 if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1258 /* figure out how many valid icons are in here */
1260 while (num - i > 2) {
1268 self->icons = g_new(Icon, self->nicons);
1270 /* store the icons */
1272 for (j = 0; j < self->nicons; ++j) {
1273 w = self->icons[j].width = data[i++];
1274 h = self->icons[j].height = data[i++];
1275 self->icons[j].data =
1276 g_memdup(&data[i], w * h * sizeof(gulong));
1285 frame_adjust_icon(self->frame);
1288 void client_update_kwm_icon(Client *self)
1293 if (!PROP_GETA32(self->window, kwm_win_icon, kwm_win_icon, &data, &num)) {
1294 self->pixmap_icon = self->pixmap_icon_mask = None;
1297 self->pixmap_icon = data[0];
1298 self->pixmap_icon_mask = data[1];
1300 self->pixmap_icon = self->pixmap_icon_mask = None;
1304 frame_adjust_icon(self->frame);
1307 static void client_change_state(Client *self)
1310 guint32 netstate[10];
1313 state[0] = self->wmstate;
1315 PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1319 netstate[num++] = prop_atoms.net_wm_state_modal;
1321 netstate[num++] = prop_atoms.net_wm_state_shaded;
1323 netstate[num++] = prop_atoms.net_wm_state_hidden;
1324 if (self->skip_taskbar)
1325 netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1326 if (self->skip_pager)
1327 netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1328 if (self->fullscreen)
1329 netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1331 netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1333 netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1335 netstate[num++] = prop_atoms.net_wm_state_above;
1337 netstate[num++] = prop_atoms.net_wm_state_below;
1338 PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1340 client_calc_layer(self);
1343 frame_adjust_state(self->frame);
1346 static Client *search_focus_tree(Client *node, Client *skip)
1351 for (it = node->transients; it != NULL; it = it->next) {
1352 Client *c = it->data;
1353 if (c == skip) continue; /* circular? */
1354 if ((ret = search_focus_tree(c, skip))) return ret;
1355 if (client_focused(c)) return c;
1360 static void calc_recursive(Client *self, StackLayer l, gboolean raised)
1368 for (it = self->transients; it; it = it->next)
1369 calc_recursive(it->data, l, raised ? raised : l != old);
1371 if (!raised && l != old)
1373 stacking_raise(self);
1376 void client_calc_layer(Client *self)
1381 /* transients take on the layer of their parents */
1382 if (self->transient_for) {
1383 if (self->transient_for != TRAN_GROUP) {
1384 self = self->transient_for;
1388 for (it = self->group->members; it; it = it->next)
1389 if (it->data != self &&
1390 ((Client*)it->data)->transient_for != TRAN_GROUP) {
1397 /* is us or one of our transients focused? */
1398 if (client_focused(self))
1400 else if (search_focus_tree(self, self))
1405 if (self->iconic) l = Layer_Icon;
1406 /* fullscreen windows are only in the fullscreen layer while focused */
1407 else if (self->fullscreen && f) l = Layer_Fullscreen;
1408 else if (self->type == Type_Desktop) l = Layer_Desktop;
1409 else if (self->type == Type_Dock) {
1410 if (!self->below) l = Layer_Top;
1411 else l = Layer_Normal;
1413 else if (self->above) l = Layer_Above;
1414 else if (self->below) l = Layer_Below;
1415 else l = Layer_Normal;
1417 calc_recursive(self, l, FALSE);
1420 gboolean client_should_show(Client *self)
1422 if (self->iconic) return FALSE;
1423 else if (!(self->desktop == screen_desktop ||
1424 self->desktop == DESKTOP_ALL)) return FALSE;
1425 else if (client_normal(self) && screen_showing_desktop) return FALSE;
1430 static void client_showhide(Client *self)
1433 if (client_should_show(self))
1434 frame_show(self->frame);
1436 frame_hide(self->frame);
1439 gboolean client_normal(Client *self) {
1440 return ! (self->type == Type_Desktop || self->type == Type_Dock ||
1441 self->type == Type_Splash);
1444 static void client_apply_startup_state(Client *self)
1446 /* these are in a carefully crafted order.. */
1449 self->iconic = FALSE;
1450 client_iconify(self, TRUE, FALSE);
1452 if (self->fullscreen) {
1453 self->fullscreen = FALSE;
1454 client_fullscreen(self, TRUE, FALSE);
1457 self->shaded = FALSE;
1458 client_shade(self, TRUE);
1461 dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1463 if (self->max_vert && self->max_horz) {
1464 self->max_vert = self->max_horz = FALSE;
1465 client_maximize(self, TRUE, 0, FALSE);
1466 } else if (self->max_vert) {
1467 self->max_vert = FALSE;
1468 client_maximize(self, TRUE, 2, FALSE);
1469 } else if (self->max_horz) {
1470 self->max_horz = FALSE;
1471 client_maximize(self, TRUE, 1, FALSE);
1474 /* nothing to do for the other states:
1483 void client_configure(Client *self, Corner anchor, int x, int y, int w, int h,
1484 gboolean user, gboolean final)
1486 gboolean moved = FALSE, resized = FALSE;
1488 /* gets the frame's position */
1489 frame_client_gravity(self->frame, &x, &y);
1491 /* these positions are frame positions, not client positions */
1493 /* set the size and position if fullscreen */
1494 if (self->fullscreen) {
1497 w = screen_physical_size.width;
1498 h = screen_physical_size.height;
1499 user = FALSE; /* ignore that increment etc shit when in fullscreen */
1501 /* set the size and position if maximized */
1502 if (self->max_horz) {
1503 x = screen_area(self->desktop)->x - self->frame->size.left;
1504 w = screen_area(self->desktop)->width;
1506 if (self->max_vert) {
1507 y = screen_area(self->desktop)->y;
1508 h = screen_area(self->desktop)->height -
1509 self->frame->size.top - self->frame->size.bottom;
1513 /* gets the client's position */
1514 frame_frame_gravity(self->frame, &x, &y);
1516 /* these override the above states! if you cant move you can't move! */
1518 if (!(self->functions & Func_Move)) {
1522 if (!(self->functions & Func_Resize)) {
1523 w = self->area.width;
1524 h = self->area.height;
1528 if (!(w == self->area.width && h == self->area.height)) {
1529 w -= self->base_size.width;
1530 h -= self->base_size.height;
1533 /* for interactive resizing. have to move half an increment in each
1536 /* how far we are towards the next size inc */
1537 int mw = w % self->size_inc.width;
1538 int mh = h % self->size_inc.height;
1540 int aw = self->size_inc.width / 2;
1541 int ah = self->size_inc.height / 2;
1542 /* don't let us move into a new size increment */
1543 if (mw + aw >= self->size_inc.width)
1544 aw = self->size_inc.width - mw - 1;
1545 if (mh + ah >= self->size_inc.height)
1546 ah = self->size_inc.height - mh - 1;
1550 /* if this is a user-requested resize, then check against min/max
1551 sizes and aspect ratios */
1553 /* smaller than min size or bigger than max size? */
1554 if (w > self->max_size.width) w = self->max_size.width;
1555 if (w < self->min_size.width) w = self->min_size.width;
1556 if (h > self->max_size.height) h = self->max_size.height;
1557 if (h < self->min_size.height) h = self->min_size.height;
1559 /* adjust the height ot match the width for the aspect ratios */
1560 if (self->min_ratio)
1561 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1562 if (self->max_ratio)
1563 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1566 /* keep to the increments */
1567 w /= self->size_inc.width;
1568 h /= self->size_inc.height;
1570 /* you cannot resize to nothing */
1574 /* store the logical size */
1575 SIZE_SET(self->logical_size, w, h);
1577 w *= self->size_inc.width;
1578 h *= self->size_inc.height;
1580 w += self->base_size.width;
1581 h += self->base_size.height;
1585 case Corner_TopLeft:
1587 case Corner_TopRight:
1588 x -= w - self->area.width;
1590 case Corner_BottomLeft:
1591 y -= h - self->area.height;
1593 case Corner_BottomRight:
1594 x -= w - self->area.width;
1595 y -= h - self->area.height;
1599 moved = x != self->area.x || y != self->area.y;
1600 resized = w != self->area.width || h != self->area.height;
1602 RECT_SET(self->area, x, y, w, h);
1605 XResizeWindow(ob_display, self->window, w, h);
1607 /* move/resize the frame to match the request */
1609 if (moved || resized)
1610 frame_adjust_area(self->frame, moved, resized);
1612 if (!user || final) {
1614 event.type = ConfigureNotify;
1615 event.xconfigure.display = ob_display;
1616 event.xconfigure.event = self->window;
1617 event.xconfigure.window = self->window;
1619 /* root window coords with border in mind */
1620 event.xconfigure.x = x - self->border_width +
1621 self->frame->size.left;
1622 event.xconfigure.y = y - self->border_width +
1623 self->frame->size.top;
1625 event.xconfigure.width = self->area.width;
1626 event.xconfigure.height = self->area.height;
1627 event.xconfigure.border_width = self->border_width;
1628 event.xconfigure.above = self->frame->plate;
1629 event.xconfigure.override_redirect = FALSE;
1630 XSendEvent(event.xconfigure.display, event.xconfigure.window,
1631 FALSE, StructureNotifyMask, &event);
1636 void client_fullscreen(Client *self, gboolean fs, gboolean savearea)
1640 if (!(self->functions & Func_Fullscreen) || /* can't */
1641 self->fullscreen == fs) return; /* already done */
1643 self->fullscreen = fs;
1644 client_change_state(self); /* change the state hints on the client,
1645 and adjust out layer/stacking */
1649 guint32 dimensions[4];
1650 dimensions[0] = self->area.x;
1651 dimensions[1] = self->area.y;
1652 dimensions[2] = self->area.width;
1653 dimensions[3] = self->area.height;
1655 PROP_SETA32(self->window, openbox_premax, cardinal,
1659 /* these are not actually used cuz client_configure will set them
1660 as appropriate when the window is fullscreened */
1666 /* pick some fallbacks... */
1667 x = screen_area(self->desktop)->x +
1668 screen_area(self->desktop)->width / 4;
1669 y = screen_area(self->desktop)->y +
1670 screen_area(self->desktop)->height / 4;
1671 w = screen_area(self->desktop)->width / 2;
1672 h = screen_area(self->desktop)->height / 2;
1674 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1675 (guint32**)&dimensions, &num)) {
1686 client_setup_decor_and_functions(self);
1688 client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1690 /* try focus us when we go into fullscreen mode */
1694 void client_iconify(Client *self, gboolean iconic, gboolean curdesk)
1698 /* move up the transient chain as far as possible first if deiconifying */
1700 while (self->transient_for) {
1701 if (self->transient_for != TRAN_GROUP) {
1702 if (self->transient_for->iconic == iconic)
1704 self = self->transient_for;
1708 /* the check for TRAN_GROUP is to prevent an infinate loop with
1709 2 transients of the same group at the head of the group's
1711 for (it = self->group->members; it; it = it->next) {
1712 Client *c = it->data;
1714 if (c != self && c->transient_for->iconic != iconic &&
1715 c->transient_for != TRAN_GROUP) {
1720 if (it == NULL) break;
1724 if (self->iconic == iconic) return; /* nothing to do */
1726 g_message("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
1729 self->iconic = iconic;
1732 self->wmstate = IconicState;
1733 self->ignore_unmaps++;
1734 /* we unmap the client itself so that we can get MapRequest events,
1735 and because the ICCCM tells us to! */
1736 XUnmapWindow(ob_display, self->window);
1739 client_set_desktop(self, screen_desktop, FALSE);
1740 self->wmstate = self->shaded ? IconicState : NormalState;
1741 XMapWindow(ob_display, self->window);
1743 client_change_state(self);
1744 client_showhide(self);
1745 screen_update_struts();
1747 dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
1750 /* iconify all transients */
1751 for (it = self->transients; it != NULL; it = it->next)
1752 if (it->data != self) client_iconify(it->data, iconic, curdesk);
1755 void client_maximize(Client *self, gboolean max, int dir, gboolean savearea)
1759 g_assert(dir == 0 || dir == 1 || dir == 2);
1760 if (!(self->functions & Func_Maximize)) return; /* can't */
1762 /* check if already done */
1764 if (dir == 0 && self->max_horz && self->max_vert) return;
1765 if (dir == 1 && self->max_horz) return;
1766 if (dir == 2 && self->max_vert) return;
1768 if (dir == 0 && !self->max_horz && !self->max_vert) return;
1769 if (dir == 1 && !self->max_horz) return;
1770 if (dir == 2 && !self->max_vert) return;
1773 /* work with the frame's coords */
1774 x = self->frame->area.x;
1775 y = self->frame->area.y;
1776 w = self->area.width;
1777 h = self->area.height;
1781 gint32 dimensions[4];
1790 /* get the property off the window and use it for the dimensions
1791 we are already maxed on */
1792 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1793 (guint32**)&readdim, &num)) {
1795 if (self->max_horz) {
1796 dimensions[0] = readdim[0];
1797 dimensions[2] = readdim[2];
1799 if (self->max_vert) {
1800 dimensions[1] = readdim[1];
1801 dimensions[3] = readdim[3];
1807 PROP_SETA32(self->window, openbox_premax, cardinal,
1808 (guint32*)dimensions, 4);
1814 /* pick some fallbacks... */
1815 if (dir == 0 || dir == 1) { /* horz */
1816 x = screen_area(self->desktop)->x +
1817 screen_area(self->desktop)->width / 4;
1818 w = screen_area(self->desktop)->width / 2;
1820 if (dir == 0 || dir == 2) { /* vert */
1821 y = screen_area(self->desktop)->y +
1822 screen_area(self->desktop)->height / 4;
1823 h = screen_area(self->desktop)->height / 2;
1826 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1827 (guint32**)&dimensions, &num)) {
1829 if (dir == 0 || dir == 1) { /* horz */
1833 if (dir == 0 || dir == 2) { /* vert */
1842 if (dir == 0 || dir == 1) /* horz */
1843 self->max_horz = max;
1844 if (dir == 0 || dir == 2) /* vert */
1845 self->max_vert = max;
1847 if (!self->max_horz && !self->max_vert)
1848 PROP_ERASE(self->window, openbox_premax);
1850 client_change_state(self); /* change the state hints on the client */
1852 /* figure out where the client should be going */
1853 frame_frame_gravity(self->frame, &x, &y);
1854 client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1857 void client_shade(Client *self, gboolean shade)
1859 if ((!(self->functions & Func_Shade) && shade) || /* can't shade */
1860 self->shaded == shade) return; /* already done */
1862 /* when we're iconic, don't change the wmstate */
1864 self->wmstate = shade ? IconicState : NormalState;
1865 self->shaded = shade;
1866 client_change_state(self);
1867 /* resize the frame to just the titlebar */
1868 frame_adjust_area(self->frame, FALSE, FALSE);
1871 void client_close(Client *self)
1875 if (!(self->functions & Func_Close)) return;
1878 XXX: itd be cool to do timeouts and shit here for killing the client's
1880 like... if the window is around after 5 seconds, then the close button
1881 turns a nice red, and if this function is called again, the client is
1885 ce.xclient.type = ClientMessage;
1886 ce.xclient.message_type = prop_atoms.wm_protocols;
1887 ce.xclient.display = ob_display;
1888 ce.xclient.window = self->window;
1889 ce.xclient.format = 32;
1890 ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
1891 ce.xclient.data.l[1] = event_lasttime;
1892 ce.xclient.data.l[2] = 0l;
1893 ce.xclient.data.l[3] = 0l;
1894 ce.xclient.data.l[4] = 0l;
1895 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
1898 void client_kill(Client *self)
1900 XKillClient(ob_display, self->window);
1903 void client_set_desktop(Client *self, guint target, gboolean donthide)
1907 if (target == self->desktop) return;
1909 g_message("Setting desktop %u", target);
1911 g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
1913 old = self->desktop;
1914 self->desktop = target;
1915 PROP_SET32(self->window, net_wm_desktop, cardinal, target);
1916 /* the frame can display the current desktop state */
1917 frame_adjust_state(self->frame);
1918 /* 'move' the window to the new desktop */
1920 client_showhide(self);
1921 /* raise if it was not already on the desktop */
1922 if (old != DESKTOP_ALL)
1923 stacking_raise(self);
1924 screen_update_struts();
1926 /* update the focus lists */
1927 if (old == DESKTOP_ALL) {
1928 for (i = 0; i < screen_num_desktops; ++i)
1929 focus_order[i] = g_list_remove(focus_order[i], self);
1931 focus_order[old] = g_list_remove(focus_order[old], self);
1932 if (target == DESKTOP_ALL) {
1933 for (i = 0; i < screen_num_desktops; ++i) {
1934 if (config_focus_new)
1935 focus_order[i] = g_list_prepend(focus_order[i], self);
1937 focus_order[i] = g_list_append(focus_order[i], self);
1940 if (config_focus_new)
1941 focus_order[target] = g_list_prepend(focus_order[target], self);
1943 focus_order[target] = g_list_append(focus_order[target], self);
1946 dispatch_client(Event_Client_Desktop, self, target, old);
1949 static Client *search_modal_tree(Client *node, Client *skip)
1954 for (it = node->transients; it != NULL; it = it->next) {
1955 Client *c = it->data;
1956 if (c == skip) continue; /* circular? */
1957 if ((ret = search_modal_tree(c, skip))) return ret;
1958 if (c->modal) return c;
1963 Client *client_find_modal_child(Client *self)
1965 return search_modal_tree(self, self);
1968 gboolean client_validate(Client *self)
1972 XSync(ob_display, FALSE); /* get all events on the server */
1974 if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
1975 XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
1976 XPutBackEvent(ob_display, &e);
1983 void client_set_wm_state(Client *self, long state)
1985 if (state == self->wmstate) return; /* no change */
1989 client_iconify(self, TRUE, TRUE);
1992 client_iconify(self, FALSE, TRUE);
1997 void client_set_state(Client *self, Atom action, long data1, long data2)
1999 gboolean shaded = self->shaded;
2000 gboolean fullscreen = self->fullscreen;
2001 gboolean max_horz = self->max_horz;
2002 gboolean max_vert = self->max_vert;
2005 if (!(action == prop_atoms.net_wm_state_add ||
2006 action == prop_atoms.net_wm_state_remove ||
2007 action == prop_atoms.net_wm_state_toggle))
2008 /* an invalid action was passed to the client message, ignore it */
2011 for (i = 0; i < 2; ++i) {
2012 Atom state = i == 0 ? data1 : data2;
2014 if (!state) continue;
2016 /* if toggling, then pick whether we're adding or removing */
2017 if (action == prop_atoms.net_wm_state_toggle) {
2018 if (state == prop_atoms.net_wm_state_modal)
2019 action = self->modal ? prop_atoms.net_wm_state_remove :
2020 prop_atoms.net_wm_state_add;
2021 else if (state == prop_atoms.net_wm_state_maximized_vert)
2022 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2023 prop_atoms.net_wm_state_add;
2024 else if (state == prop_atoms.net_wm_state_maximized_horz)
2025 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2026 prop_atoms.net_wm_state_add;
2027 else if (state == prop_atoms.net_wm_state_shaded)
2028 action = self->shaded ? prop_atoms.net_wm_state_remove :
2029 prop_atoms.net_wm_state_add;
2030 else if (state == prop_atoms.net_wm_state_skip_taskbar)
2031 action = self->skip_taskbar ?
2032 prop_atoms.net_wm_state_remove :
2033 prop_atoms.net_wm_state_add;
2034 else if (state == prop_atoms.net_wm_state_skip_pager)
2035 action = self->skip_pager ?
2036 prop_atoms.net_wm_state_remove :
2037 prop_atoms.net_wm_state_add;
2038 else if (state == prop_atoms.net_wm_state_fullscreen)
2039 action = self->fullscreen ?
2040 prop_atoms.net_wm_state_remove :
2041 prop_atoms.net_wm_state_add;
2042 else if (state == prop_atoms.net_wm_state_above)
2043 action = self->above ? prop_atoms.net_wm_state_remove :
2044 prop_atoms.net_wm_state_add;
2045 else if (state == prop_atoms.net_wm_state_below)
2046 action = self->below ? prop_atoms.net_wm_state_remove :
2047 prop_atoms.net_wm_state_add;
2050 if (action == prop_atoms.net_wm_state_add) {
2051 if (state == prop_atoms.net_wm_state_modal) {
2052 /* XXX raise here or something? */
2054 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2056 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2058 } else if (state == prop_atoms.net_wm_state_shaded) {
2060 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2061 self->skip_taskbar = TRUE;
2062 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2063 self->skip_pager = TRUE;
2064 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2066 } else if (state == prop_atoms.net_wm_state_above) {
2068 } else if (state == prop_atoms.net_wm_state_below) {
2072 } else { /* action == prop_atoms.net_wm_state_remove */
2073 if (state == prop_atoms.net_wm_state_modal) {
2074 self->modal = FALSE;
2075 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2077 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2079 } else if (state == prop_atoms.net_wm_state_shaded) {
2081 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2082 self->skip_taskbar = FALSE;
2083 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2084 self->skip_pager = FALSE;
2085 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2087 } else if (state == prop_atoms.net_wm_state_above) {
2088 self->above = FALSE;
2089 } else if (state == prop_atoms.net_wm_state_below) {
2090 self->below = FALSE;
2094 if (max_horz != self->max_horz || max_vert != self->max_vert) {
2095 if (max_horz != self->max_horz && max_vert != self->max_vert) {
2097 if (max_horz == max_vert) { /* both going the same way */
2098 client_maximize(self, max_horz, 0, TRUE);
2100 client_maximize(self, max_horz, 1, TRUE);
2101 client_maximize(self, max_vert, 2, TRUE);
2105 if (max_horz != self->max_horz)
2106 client_maximize(self, max_horz, 1, TRUE);
2108 client_maximize(self, max_vert, 2, TRUE);
2111 /* change fullscreen state before shading, as it will affect if the window
2113 if (fullscreen != self->fullscreen)
2114 client_fullscreen(self, fullscreen, TRUE);
2115 if (shaded != self->shaded)
2116 client_shade(self, shaded);
2117 client_calc_layer(self);
2118 client_change_state(self); /* change the hint to relect these changes */
2121 Client *client_focus_target(Client *self)
2125 /* if we have a modal child, then focus it, not us */
2126 child = client_find_modal_child(self);
2127 if (child) return child;
2131 gboolean client_focusable(Client *self)
2133 /* won't try focus if the client doesn't want it, or if the window isn't
2134 visible on the screen */
2135 return self->frame->visible &&
2136 (self->can_focus || self->focus_notify);
2139 gboolean client_focus(Client *self)
2144 /* choose the correct target */
2145 self = client_focus_target(self);
2147 if (self->desktop != DESKTOP_ALL && self->desktop != screen_desktop) {
2148 /* update the focus lists */
2149 if (self->desktop == DESKTOP_ALL) {
2150 for (i = 0; i < screen_num_desktops; ++i) {
2151 focus_order[i] = g_list_remove(focus_order[i], self);
2152 focus_order[i] = g_list_prepend(focus_order[i], self);
2156 focus_order[i] = g_list_remove(focus_order[i], self);
2157 focus_order[i] = g_list_prepend(focus_order[i], self);
2162 if (!client_focusable(self))
2165 /* do a check to see if the window has already been unmapped or destroyed
2166 do this intelligently while watching out for unmaps we've generated
2167 (ignore_unmaps > 0) */
2168 if (XCheckTypedWindowEvent(ob_display, self->window,
2169 DestroyNotify, &ev)) {
2170 XPutBackEvent(ob_display, &ev);
2173 while (XCheckTypedWindowEvent(ob_display, self->window,
2174 UnmapNotify, &ev)) {
2175 if (self->ignore_unmaps) {
2176 self->ignore_unmaps--;
2178 XPutBackEvent(ob_display, &ev);
2183 if (self->can_focus)
2184 /* RevertToPointerRoot causes much more headache than RevertToNone, so
2185 I choose to use it always, hopefully to find errors quicker, if any
2186 are left. (I hate X. I hate focus events.) */
2187 XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2190 if (self->focus_notify) {
2192 ce.xclient.type = ClientMessage;
2193 ce.xclient.message_type = prop_atoms.wm_protocols;
2194 ce.xclient.display = ob_display;
2195 ce.xclient.window = self->window;
2196 ce.xclient.format = 32;
2197 ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2198 ce.xclient.data.l[1] = event_lasttime;
2199 ce.xclient.data.l[2] = 0l;
2200 ce.xclient.data.l[3] = 0l;
2201 ce.xclient.data.l[4] = 0l;
2202 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2206 g_message("%sively focusing %lx at %d", (self->can_focus ? "act" : "pass"),
2211 /* Cause the FocusIn to come back to us. Important for desktop switches,
2212 since otherwise we'll have no FocusIn on the queue and send it off to
2213 the focus_backup. */
2214 XSync(ob_display, FALSE);
2218 void client_unfocus(Client *self)
2220 g_assert(focus_client == self);
2222 g_message("client_unfocus for %lx", self->window);
2224 focus_fallback(Fallback_Unfocusing);
2227 gboolean client_focused(Client *self)
2229 return self == focus_client;
2232 Icon *client_icon(Client *self, int w, int h)
2235 /* si is the smallest image >= req */
2236 /* li is the largest image < req */
2237 unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2239 if (!self->nicons) return NULL;
2241 for (i = 0; i < self->nicons; ++i) {
2242 size = self->icons[i].width * self->icons[i].height;
2243 if (size < smallest && size >= (unsigned)(w * h)) {
2247 if (size > largest && size <= (unsigned)(w * h)) {
2252 if (largest == 0) /* didnt find one smaller than the requested size */
2253 return &self->icons[si];
2254 return &self->icons[li];