6 #include "moveresize.h"
8 #include "extensions.h"
19 #include "render/render.h"
22 #include <X11/Xutil.h>
24 /*! The event mask to grab on client windows */
25 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
28 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
31 GList *client_list = NULL;
33 static void client_get_all(Client *self);
34 static void client_toggle_border(Client *self, gboolean show);
35 static void client_get_area(Client *self);
36 static void client_get_desktop(Client *self);
37 static void client_get_state(Client *self);
38 static void client_get_shaped(Client *self);
39 static void client_get_mwm_hints(Client *self);
40 static void client_get_gravity(Client *self);
41 static void client_showhide(Client *self);
42 static void client_change_allowed_actions(Client *self);
43 static void client_change_state(Client *self);
44 static void client_apply_startup_state(Client *self);
51 void client_shutdown()
55 void client_set_list()
57 Window *windows, *win_it;
59 guint size = g_list_length(client_list);
61 /* create an array of the window ids */
63 windows = g_new(Window, size);
65 for (it = client_list; it != NULL; it = it->next, ++win_it)
66 *win_it = ((Client*)it->data)->window;
70 PROP_SETA32(ob_root, net_client_list, window, (guint32*)windows, size);
79 void client_foreach_transient(Client *self, ClientForeachFunc func, void *data)
83 for (it = self->transients; it; it = it->next) {
84 if (!func(it->data, data)) return;
85 client_foreach_transient(it->data, func, data);
89 void client_foreach_ancestor(Client *self, ClientForeachFunc func, void *data)
91 if (self->transient_for) {
92 if (self->transient_for != TRAN_GROUP) {
93 if (!func(self->transient_for, data)) return;
94 client_foreach_ancestor(self->transient_for, func, data);
98 for (it = self->group->members; it; it = it->next)
99 if (it->data != self &&
100 !((Client*)it->data)->transient_for) {
101 if (!func(it->data, data)) return;
102 client_foreach_ancestor(it->data, func, data);
109 void client_manage_all()
111 unsigned int i, j, nchild;
114 XWindowAttributes attrib;
116 XQueryTree(ob_display, ob_root, &w, &w, &children, &nchild);
118 /* remove all icon windows from the list */
119 for (i = 0; i < nchild; i++) {
120 if (children[i] == None) continue;
121 wmhints = XGetWMHints(ob_display, children[i]);
123 if ((wmhints->flags & IconWindowHint) &&
124 (wmhints->icon_window != children[i]))
125 for (j = 0; j < nchild; j++)
126 if (children[j] == wmhints->icon_window) {
134 for (i = 0; i < nchild; ++i) {
135 if (children[i] == None)
137 if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
138 if (attrib.override_redirect) continue;
140 if (attrib.map_state != IsUnmapped)
141 client_manage(children[i]);
146 /* stack them as they were on startup!
147 why with stacking_lower? Why, because then windows who aren't in the
148 stacking list are on the top where you can see them instead of buried
150 for (i = startup_stack_size; i > 0; --i) {
153 w = startup_stack_order[i-1];
154 obw = g_hash_table_lookup(window_map, &w);
156 g_assert(WINDOW_IS_CLIENT(obw));
157 stacking_lower(CLIENT_AS_WINDOW(obw));
160 g_free(startup_stack_order);
161 startup_stack_order = NULL;
162 startup_stack_size = 0;
164 if (config_focus_new) {
167 active = g_hash_table_lookup(window_map, &startup_active);
169 g_assert(WINDOW_IS_CLIENT(active));
170 if (!client_focus(WINDOW_AS_CLIENT(active)))
171 focus_fallback(Fallback_NoFocus);
173 focus_fallback(Fallback_NoFocus);
177 void client_manage(Window window)
181 XWindowAttributes attrib;
182 XSetWindowAttributes attrib_set;
184 gboolean activate = FALSE;
188 /* check if it has already been unmapped by the time we started mapping
189 the grab does a sync so we don't have to here */
190 if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
191 XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
192 XPutBackEvent(ob_display, &e);
195 return; /* don't manage it */
198 /* make sure it isn't an override-redirect window */
199 if (!XGetWindowAttributes(ob_display, window, &attrib) ||
200 attrib.override_redirect) {
202 return; /* don't manage it */
205 /* is the window a docking app */
206 if ((wmhint = XGetWMHints(ob_display, window))) {
207 if ((wmhint->flags & StateHint) &&
208 wmhint->initial_state == WithdrawnState) {
209 dock_add(window, wmhint);
217 g_message("Managing window: %lx", window);
219 /* choose the events we want to receive on the CLIENT window */
220 attrib_set.event_mask = CLIENT_EVENTMASK;
221 attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
222 XChangeWindowAttributes(ob_display, window,
223 CWEventMask|CWDontPropagate, &attrib_set);
226 /* create the Client struct, and populate it from the hints on the
228 self = g_new(Client, 1);
229 self->obwin.type = Window_Client;
230 self->window = window;
231 client_get_all(self);
233 /* remove the client's border (and adjust re gravity) */
234 client_toggle_border(self, FALSE);
236 /* specify that if we exit, the window should not be destroyed and should
237 be reparented back to root automatically */
238 XChangeSaveSet(ob_display, window, SetModeInsert);
240 /* create the decoration frame for the client window */
241 self->frame = frame_new();
243 frame_grab_client(self->frame, self);
245 client_apply_startup_state(self);
249 /* add to client list/map */
250 client_list = g_list_append(client_list, self);
251 g_hash_table_insert(window_map, &self->window, self);
253 /* update the focus lists */
254 focus_order_add_new(self);
256 /* focus the new window? */
257 if (ob_state != State_Starting && config_focus_new &&
258 (self->type == Type_Normal || self->type == Type_Dialog)) {
259 gboolean group_foc = FALSE;
264 for (it = self->group->members; it; it = it->next) {
265 if (client_focused(it->data)) {
271 /* note the check against Type_Normal/Dialog, not client_normal(self),
272 which would also include other types. in this case we want more
273 strict rules for focus */
275 (!self->transient_for && (!self->group ||
276 !self->group->members->next))) ||
277 client_search_focus_tree_full(self) ||
279 !client_normal(focus_client)) {
280 /* activate the window */
281 stacking_add(CLIENT_AS_WINDOW(self));
284 /* try to not get in the way */
285 stacking_add_nonintrusive(CLIENT_AS_WINDOW(self));
288 stacking_add(CLIENT_AS_WINDOW(self));
291 screen_update_struts();
293 /* make sure the window is visible */
294 client_move_onscreen(self);
296 dispatch_client(Event_Client_New, self, 0, 0);
298 client_showhide(self);
300 if (activate) client_activate(self);
302 /* update the list hints */
305 dispatch_client(Event_Client_Mapped, self, 0, 0);
307 g_message("Managed window 0x%lx (%s)", window, self->class);
310 void client_unmanage_all()
312 while (client_list != NULL)
313 client_unmanage(client_list->data);
316 /* called by client_unmanage() to close any menus referencing this client */
317 void client_close_menus(gpointer key, gpointer value, gpointer self)
319 if (((Menu *)value)->client == (Client *)self)
320 menu_hide((Menu *)value);
323 void client_unmanage(Client *self)
328 g_message("Unmanaging window: %lx (%s)", self->window, self->class);
330 dispatch_client(Event_Client_Destroy, self, 0, 0);
331 g_assert(self != NULL);
333 /* remove the window from our save set */
334 XChangeSaveSet(ob_display, self->window, SetModeDelete);
336 /* we dont want events no more */
337 XSelectInput(ob_display, self->window, NoEventMask);
339 frame_hide(self->frame);
341 client_list = g_list_remove(client_list, self);
342 stacking_remove(self);
343 g_hash_table_remove(window_map, &self->window);
345 /* update the focus lists */
346 focus_order_remove(self);
348 /* once the client is out of the list, update the struts to remove it's
350 screen_update_struts();
352 /* tell our parent(s) that we're gone */
353 if (self->transient_for == TRAN_GROUP) { /* transient of group */
356 for (it = self->group->members; it; it = it->next)
357 if (it->data != self)
358 ((Client*)it->data)->transients =
359 g_slist_remove(((Client*)it->data)->transients, self);
360 } else if (self->transient_for) { /* transient of window */
361 self->transient_for->transients =
362 g_slist_remove(self->transient_for->transients, self);
365 /* tell our transients that we're gone */
366 for (it = self->transients; it != NULL; it = it->next) {
367 if (((Client*)it->data)->transient_for != TRAN_GROUP) {
368 ((Client*)it->data)->transient_for = NULL;
369 client_calc_layer(it->data);
373 if (moveresize_client == self)
374 moveresize_end(TRUE);
376 /* close any windows that are attached to this window */
377 g_hash_table_foreach(menu_hash, client_close_menus, self);
380 if (focus_client == self) {
383 /* focus the last focused window on the desktop, and ignore enter
384 events from the unmap so it doesnt mess with the focus */
385 while (XCheckTypedEvent(ob_display, EnterNotify, &e));
386 client_unfocus(self);
389 /* remove from its group */
391 group_remove(self->group, self);
395 /* dispatch the unmapped event */
396 dispatch_client(Event_Client_Unmapped, self, 0, 0);
397 g_assert(self != NULL);
399 /* give the client its border back */
400 client_toggle_border(self, TRUE);
402 /* reparent the window out of the frame, and free the frame */
403 frame_release_client(self->frame, self);
406 if (ob_state != State_Exiting) {
407 /* these values should not be persisted across a window
409 prop_erase(self->window, prop_atoms.net_wm_desktop);
410 prop_erase(self->window, prop_atoms.net_wm_state);
412 /* if we're left in an iconic state, the client wont be mapped. this is
413 bad, since we will no longer be managing the window on restart */
415 XMapWindow(ob_display, self->window);
419 g_message("Unmanaged window 0x%lx", self->window);
421 /* free all data allocated in the client struct */
422 g_slist_free(self->transients);
423 for (j = 0; j < self->nicons; ++j)
424 g_free(self->icons[j].data);
425 if (self->nicons > 0)
428 g_free(self->icon_title);
434 /* update the list hints */
438 void client_move_onscreen(Client *self)
441 int x = self->frame->area.x, y = self->frame->area.y;
443 a = screen_area(self->desktop);
444 if (x >= a->x + a->width - 1)
445 x = a->x + a->width - self->frame->area.width;
446 if (y >= a->y + a->height - 1)
447 y = a->y + a->height - self->frame->area.height;
448 if (x + self->frame->area.width - 1 < a->x)
450 if (y + self->frame->area.height - 1< a->y)
453 frame_frame_gravity(self->frame, &x, &y); /* get where the client
455 client_configure(self , Corner_TopLeft, x, y,
456 self->area.width, self->area.height,
460 static void client_toggle_border(Client *self, gboolean show)
462 /* adjust our idea of where the client is, based on its border. When the
463 border is removed, the client should now be considered to be in a
465 when re-adding the border to the client, the same operation needs to be
467 int oldx = self->area.x, oldy = self->area.y;
468 int x = oldx, y = oldy;
469 switch(self->gravity) {
471 case NorthWestGravity:
473 case SouthWestGravity:
475 case NorthEastGravity:
477 case SouthEastGravity:
478 if (show) x -= self->border_width * 2;
479 else x += self->border_width * 2;
486 if (show) x -= self->border_width;
487 else x += self->border_width;
490 switch(self->gravity) {
492 case NorthWestGravity:
494 case NorthEastGravity:
496 case SouthWestGravity:
498 case SouthEastGravity:
499 if (show) y -= self->border_width * 2;
500 else y += self->border_width * 2;
507 if (show) y -= self->border_width;
508 else y += self->border_width;
515 XSetWindowBorderWidth(ob_display, self->window, self->border_width);
517 /* move the client so it is back it the right spot _with_ its
519 if (x != oldx || y != oldy)
520 XMoveWindow(ob_display, self->window, x, y);
522 XSetWindowBorderWidth(ob_display, self->window, 0);
526 static void client_get_all(Client *self)
528 /* update EVERYTHING!! */
530 self->ignore_unmaps = 0;
534 self->title = self->icon_title = NULL;
535 self->title_count = 1;
536 self->name = self->class = self->role = NULL;
537 self->wmstate = NormalState;
538 self->transient = FALSE;
539 self->transients = NULL;
540 self->transient_for = NULL;
542 self->urgent = FALSE;
543 self->positioned = FALSE;
544 self->disabled_decorations = 0;
548 client_get_area(self);
549 client_update_transient_for(self);
550 client_update_wmhints(self);
551 client_get_desktop(self);
552 client_get_state(self);
553 client_get_shaped(self);
555 client_get_mwm_hints(self);
556 client_get_type(self);/* this can change the mwmhints for special cases */
558 client_update_protocols(self);
560 client_get_gravity(self); /* get the attribute gravity */
561 client_update_normal_hints(self); /* this may override the attribute
564 /* got the type, the mwmhints, the protocols, and the normal hints
565 (min/max sizes), so we're ready to set up the decorations/functions */
566 client_setup_decor_and_functions(self);
568 client_update_title(self);
569 client_update_class(self);
570 client_update_strut(self);
571 client_update_icons(self);
573 client_change_state(self);
576 static void client_get_area(Client *self)
578 XWindowAttributes wattrib;
581 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
582 g_assert(ret != BadWindow);
584 RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
585 self->border_width = wattrib.border_width;
588 static void client_get_desktop(Client *self)
592 if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
593 if (d >= screen_num_desktops && d != DESKTOP_ALL)
594 d = screen_num_desktops - 1;
597 gboolean trdesk = FALSE;
599 if (self->transient_for) {
600 if (self->transient_for != TRAN_GROUP) {
601 self->desktop = self->transient_for->desktop;
606 for (it = self->group->members; it; it = it->next)
607 if (it->data != self &&
608 !((Client*)it->data)->transient_for) {
609 self->desktop = ((Client*)it->data)->desktop;
616 /* defaults to the current desktop */
617 self->desktop = screen_desktop;
619 /* set the desktop hint, to make sure that it always exists */
620 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
624 static void client_get_state(Client *self)
629 self->modal = self->shaded = self->max_horz = self->max_vert =
630 self->fullscreen = self->above = self->below = self->iconic =
631 self->skip_taskbar = self->skip_pager = FALSE;
633 if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
635 for (i = 0; i < num; ++i) {
636 if (state[i] == prop_atoms.net_wm_state_modal)
638 else if (state[i] == prop_atoms.net_wm_state_shaded)
640 else if (state[i] == prop_atoms.net_wm_state_hidden)
642 else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
643 self->skip_taskbar = TRUE;
644 else if (state[i] == prop_atoms.net_wm_state_skip_pager)
645 self->skip_pager = TRUE;
646 else if (state[i] == prop_atoms.net_wm_state_fullscreen)
647 self->fullscreen = TRUE;
648 else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
649 self->max_vert = TRUE;
650 else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
651 self->max_horz = TRUE;
652 else if (state[i] == prop_atoms.net_wm_state_above)
654 else if (state[i] == prop_atoms.net_wm_state_below)
662 static void client_get_shaped(Client *self)
664 self->shaped = FALSE;
666 if (extensions_shape) {
671 XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
673 XShapeQueryExtents(ob_display, self->window, &s, &foo,
674 &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
676 self->shaped = (s != 0);
681 void client_update_transient_for(Client *self)
686 if (XGetTransientForHint(ob_display, self->window, &t)) {
687 self->transient = TRUE;
688 if (t != self->window) { /* cant be transient to itself! */
689 c = g_hash_table_lookup(window_map, &t);
690 /* if this happens then we need to check for it*/
692 g_assert(!c || WINDOW_IS_CLIENT(c));
694 if (!c && self->group) {
695 /* not transient to a client, see if it is transient for a
697 if (t == self->group->leader ||
700 /* window is a transient for its group! */
706 self->transient = FALSE;
708 /* if anything has changed... */
709 if (c != self->transient_for) {
710 if (self->transient_for == TRAN_GROUP) { /* transient of group */
713 /* remove from old parents */
714 for (it = self->group->members; it; it = it->next)
715 if (it->data != self &&
716 !((Client*)it->data)->transient_for)
717 ((Client*)it->data)->transients =
718 g_slist_remove(((Client*)it->data)->transients, self);
719 } else if (self->transient_for != NULL) { /* transient of window */
720 /* remove from old parent */
721 self->transient_for->transients =
722 g_slist_remove(self->transient_for->transients, self);
724 self->transient_for = c;
725 if (self->transient_for == TRAN_GROUP) { /* transient of group */
728 /* add to new parents */
729 for (it = self->group->members; it; it = it->next)
730 if (it->data != self &&
731 !((Client*)it->data)->transient_for)
732 ((Client*)it->data)->transients =
733 g_slist_append(((Client*)it->data)->transients, self);
735 /* remove all transients which are in the group, that causes
736 circlular pointer hell of doom */
737 for (it = self->group->members; it; it = it->next) {
739 for (sit = self->transients; sit; sit = next) {
741 if (sit->data == it->data)
742 self->transients = g_slist_remove(self->transients,
746 } else if (self->transient_for != NULL) { /* transient of window */
747 /* add to new parent */
748 self->transient_for->transients =
749 g_slist_append(self->transient_for->transients, self);
754 static void client_get_mwm_hints(Client *self)
759 self->mwmhints.flags = 0; /* default to none */
761 if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
763 if (num >= MWM_ELEMENTS) {
764 self->mwmhints.flags = hints[0];
765 self->mwmhints.functions = hints[1];
766 self->mwmhints.decorations = hints[2];
772 void client_get_type(Client *self)
779 if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
780 /* use the first value that we know about in the array */
781 for (i = 0; i < num; ++i) {
782 if (val[i] == prop_atoms.net_wm_window_type_desktop)
783 self->type = Type_Desktop;
784 else if (val[i] == prop_atoms.net_wm_window_type_dock)
785 self->type = Type_Dock;
786 else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
787 self->type = Type_Toolbar;
788 else if (val[i] == prop_atoms.net_wm_window_type_menu)
789 self->type = Type_Menu;
790 else if (val[i] == prop_atoms.net_wm_window_type_utility)
791 self->type = Type_Utility;
792 else if (val[i] == prop_atoms.net_wm_window_type_splash)
793 self->type = Type_Splash;
794 else if (val[i] == prop_atoms.net_wm_window_type_dialog)
795 self->type = Type_Dialog;
796 else if (val[i] == prop_atoms.net_wm_window_type_normal)
797 self->type = Type_Normal;
798 else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
799 /* prevent this window from getting any decor or
801 self->mwmhints.flags &= (MwmFlag_Functions |
802 MwmFlag_Decorations);
803 self->mwmhints.decorations = 0;
804 self->mwmhints.functions = 0;
806 if (self->type != (WindowType) -1)
807 break; /* grab the first legit type */
812 if (self->type == (WindowType) -1) {
813 /*the window type hint was not set, which means we either classify
814 ourself as a normal window or a dialog, depending on if we are a
817 self->type = Type_Dialog;
819 self->type = Type_Normal;
823 void client_update_protocols(Client *self)
828 self->focus_notify = FALSE;
829 self->delete_window = FALSE;
831 if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
832 for (i = 0; i < num_return; ++i) {
833 if (proto[i] == prop_atoms.wm_delete_window) {
834 /* this means we can request the window to close */
835 self->delete_window = TRUE;
836 } else if (proto[i] == prop_atoms.wm_take_focus)
837 /* if this protocol is requested, then the window will be
838 notified whenever we want it to receive focus */
839 self->focus_notify = TRUE;
845 static void client_get_gravity(Client *self)
847 XWindowAttributes wattrib;
850 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
851 g_assert(ret != BadWindow);
852 self->gravity = wattrib.win_gravity;
855 void client_update_normal_hints(Client *self)
859 int oldgravity = self->gravity;
862 self->min_ratio = 0.0f;
863 self->max_ratio = 0.0f;
864 SIZE_SET(self->size_inc, 1, 1);
865 SIZE_SET(self->base_size, 0, 0);
866 SIZE_SET(self->min_size, 0, 0);
867 SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
869 /* get the hints from the window */
870 if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
871 self->positioned = !!(size.flags & (PPosition|USPosition));
873 if (size.flags & PWinGravity) {
874 self->gravity = size.win_gravity;
876 /* if the client has a frame, i.e. has already been mapped and
877 is changing its gravity */
878 if (self->frame && self->gravity != oldgravity) {
879 /* move our idea of the client's position based on its new
881 self->area.x = self->frame->area.x;
882 self->area.y = self->frame->area.y;
883 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
887 if (size.flags & PAspect) {
888 if (size.min_aspect.y)
889 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
890 if (size.max_aspect.y)
891 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
894 if (size.flags & PMinSize)
895 SIZE_SET(self->min_size, size.min_width, size.min_height);
897 if (size.flags & PMaxSize)
898 SIZE_SET(self->max_size, size.max_width, size.max_height);
900 if (size.flags & PBaseSize)
901 SIZE_SET(self->base_size, size.base_width, size.base_height);
903 if (size.flags & PResizeInc)
904 SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
908 void client_setup_decor_and_functions(Client *self)
910 /* start with everything (cept fullscreen) */
911 self->decorations = Decor_Titlebar | Decor_Handle | Decor_Border |
912 Decor_Icon | Decor_AllDesktops | Decor_Iconify | Decor_Maximize |
914 self->functions = Func_Resize | Func_Move | Func_Iconify | Func_Maximize |
916 if (self->delete_window) {
917 self->decorations |= Decor_Close;
918 self->functions |= Func_Close;
921 if (!(self->min_size.width < self->max_size.width ||
922 self->min_size.height < self->max_size.height)) {
923 self->decorations &= ~(Decor_Maximize | Decor_Handle);
924 self->functions &= ~(Func_Resize | Func_Maximize);
927 switch (self->type) {
929 /* normal windows retain all of the possible decorations and
930 functionality, and are the only windows that you can fullscreen */
931 self->functions |= Func_Fullscreen;
936 /* these windows cannot be maximized */
937 self->decorations &= ~Decor_Maximize;
938 self->functions &= ~Func_Maximize;
943 /* these windows get less functionality */
944 self->decorations &= ~(Decor_Iconify | Decor_Handle);
945 self->functions &= ~(Func_Iconify | Func_Resize);
951 /* none of these windows are manipulated by the window manager */
952 self->decorations = 0;
957 /* Mwm Hints are applied subtractively to what has already been chosen for
958 decor and functionality */
959 if (self->mwmhints.flags & MwmFlag_Decorations) {
960 if (! (self->mwmhints.decorations & MwmDecor_All)) {
961 if (! (self->mwmhints.decorations & MwmDecor_Border))
962 self->decorations &= ~Decor_Border;
963 if (! (self->mwmhints.decorations & MwmDecor_Handle))
964 self->decorations &= ~Decor_Handle;
965 if (! (self->mwmhints.decorations & MwmDecor_Title))
966 self->decorations &= ~Decor_Titlebar;
967 if (! (self->mwmhints.decorations & MwmDecor_Iconify))
968 self->decorations &= ~Decor_Iconify;
969 if (! (self->mwmhints.decorations & MwmDecor_Maximize))
970 self->decorations &= ~Decor_Maximize;
974 if (self->mwmhints.flags & MwmFlag_Functions) {
975 if (! (self->mwmhints.functions & MwmFunc_All)) {
976 if (! (self->mwmhints.functions & MwmFunc_Resize))
977 self->functions &= ~Func_Resize;
978 if (! (self->mwmhints.functions & MwmFunc_Move))
979 self->functions &= ~Func_Move;
980 if (! (self->mwmhints.functions & MwmFunc_Iconify))
981 self->functions &= ~Func_Iconify;
982 if (! (self->mwmhints.functions & MwmFunc_Maximize))
983 self->functions &= ~Func_Maximize;
984 /* dont let mwm hints kill the close button
985 if (! (self->mwmhints.functions & MwmFunc_Close))
986 self->functions &= ~Func_Close; */
990 /* can't maximize without moving/resizing */
991 if (!((self->functions & Func_Move) && (self->functions & Func_Resize)))
992 self->functions &= ~(Func_Maximize | Func_Fullscreen);
994 /* finally, user specified disabled decorations are applied to subtract
996 if (self->disabled_decorations & Decor_Titlebar)
997 self->decorations &= ~Decor_Titlebar;
998 if (self->disabled_decorations & Decor_Handle)
999 self->decorations &= ~Decor_Handle;
1000 if (self->disabled_decorations & Decor_Border)
1001 self->decorations &= ~Decor_Border;
1002 if (self->disabled_decorations & Decor_Iconify)
1003 self->decorations &= ~Decor_Iconify;
1004 if (self->disabled_decorations & Decor_Maximize)
1005 self->decorations &= ~Decor_Maximize;
1006 if (self->disabled_decorations & Decor_AllDesktops)
1007 self->decorations &= ~Decor_AllDesktops;
1008 if (self->disabled_decorations & Decor_Shade)
1009 self->decorations &= ~Decor_Shade;
1010 if (self->disabled_decorations & Decor_Close)
1011 self->decorations &= ~Decor_Close;
1013 /* if we don't have a titlebar, then we cannot shade! */
1014 if (!(self->decorations & Decor_Titlebar))
1015 self->functions &= ~Func_Shade;
1017 /* now we need to check against rules for the client's current state */
1018 if (self->fullscreen) {
1019 self->functions &= (Func_Close | Func_Fullscreen | Func_Iconify);
1020 self->decorations = 0;
1023 client_change_allowed_actions(self);
1026 /* this makes sure that these windows appear on all desktops */
1027 if (self->type == Type_Desktop && self->desktop != DESKTOP_ALL)
1028 client_set_desktop(self, DESKTOP_ALL, FALSE);
1030 /* change the decors on the frame, and with more/less decorations,
1031 we may also need to be repositioned */
1032 frame_adjust_area(self->frame, TRUE, TRUE);
1033 /* with new decor, the window's maximized size may change */
1034 client_remaximize(self);
1036 /* this makes sure that these windows appear on all desktops */
1037 if (self->type == Type_Desktop && self->desktop != DESKTOP_ALL)
1038 self->desktop = DESKTOP_ALL;
1042 static void client_change_allowed_actions(Client *self)
1047 /* desktop windows are kept on all desktops */
1048 if (self->type != Type_Desktop)
1049 actions[num++] = prop_atoms.net_wm_action_change_desktop;
1051 if (self->functions & Func_Shade)
1052 actions[num++] = prop_atoms.net_wm_action_shade;
1053 if (self->functions & Func_Close)
1054 actions[num++] = prop_atoms.net_wm_action_close;
1055 if (self->functions & Func_Move)
1056 actions[num++] = prop_atoms.net_wm_action_move;
1057 if (self->functions & Func_Iconify)
1058 actions[num++] = prop_atoms.net_wm_action_minimize;
1059 if (self->functions & Func_Resize)
1060 actions[num++] = prop_atoms.net_wm_action_resize;
1061 if (self->functions & Func_Fullscreen)
1062 actions[num++] = prop_atoms.net_wm_action_fullscreen;
1063 if (self->functions & Func_Maximize) {
1064 actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1065 actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1068 PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1070 /* make sure the window isn't breaking any rules now */
1072 if (!(self->functions & Func_Shade) && self->shaded) {
1073 if (self->frame) client_shade(self, FALSE);
1074 else self->shaded = FALSE;
1076 if (!(self->functions & Func_Iconify) && self->iconic) {
1077 g_message("UNSETTING ICONIC");
1078 if (self->frame) client_iconify(self, FALSE, TRUE);
1079 else self->iconic = FALSE;
1081 if (!(self->functions & Func_Fullscreen) && self->fullscreen) {
1082 if (self->frame) client_fullscreen(self, FALSE, TRUE);
1083 else self->fullscreen = FALSE;
1085 if (!(self->functions & Func_Maximize) && (self->max_horz ||
1087 if (self->frame) client_maximize(self, FALSE, 0, TRUE);
1088 else self->max_vert = self->max_horz = FALSE;
1092 void client_remaximize(Client *self)
1095 if (self->max_horz && self->max_vert)
1097 else if (self->max_horz)
1099 else if (self->max_vert)
1102 return; /* not maximized */
1103 self->max_horz = self->max_vert = FALSE;
1104 client_maximize(self, TRUE, dir, FALSE);
1107 void client_update_wmhints(Client *self)
1110 gboolean ur = FALSE;
1113 /* assume a window takes input if it doesnt specify */
1114 self->can_focus = TRUE;
1116 if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1117 if (hints->flags & InputHint)
1118 self->can_focus = hints->input;
1120 /* only do this when first managing the window *AND* when we aren't
1122 if (ob_state != State_Starting && self->frame == NULL)
1123 if (hints->flags & StateHint)
1124 self->iconic = hints->initial_state == IconicState;
1126 if (hints->flags & XUrgencyHint)
1129 if (!(hints->flags & WindowGroupHint))
1130 hints->window_group = None;
1132 /* did the group state change? */
1133 if (hints->window_group !=
1134 (self->group ? self->group->leader : None)) {
1135 /* remove from the old group if there was one */
1136 if (self->group != NULL) {
1137 /* remove transients of the group */
1138 for (it = self->group->members; it; it = it->next)
1139 self->transients = g_slist_remove(self->transients,
1141 group_remove(self->group, self);
1144 if (hints->window_group != None) {
1145 self->group = group_add(hints->window_group, self);
1147 /* i can only have transients from the group if i am not
1149 if (!self->transient_for) {
1150 /* add other transients of the group that are already
1152 for (it = self->group->members; it; it = it->next)
1153 if (it->data != self &&
1154 ((Client*)it->data)->transient_for == TRAN_GROUP)
1155 self->transients = g_slist_append(self->transients,
1160 /* the WM_HINTS can contain an icon */
1161 client_update_icons(self);
1163 /* because the self->transient flag wont change from this call,
1164 we don't need to update the window's type and such, only its
1165 transient_for, and the transients lists of other windows in
1166 the group may be affected */
1167 client_update_transient_for(self);
1173 if (ur != self->urgent) {
1175 g_message("Urgent Hint for 0x%lx: %s", self->window,
1177 /* fire the urgent callback if we're mapped, otherwise, wait until
1178 after we're mapped */
1180 dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1184 void client_update_title(Client *self)
1190 gboolean read_title;
1192 g_free(self->title);
1195 if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1196 /* try old x stuff */
1197 if (!PROP_GETS(self->window, wm_name, locale, &data))
1198 data = g_strdup("Unnamed Window");
1200 /* look for duplicates and append a number */
1202 for (it = client_list; it; it = it->next)
1203 if (it->data != self) {
1204 Client *c = it->data;
1205 if (0 == strncmp(c->title, data, strlen(data)))
1206 nums |= 1 << c->title_count;
1208 /* find first free number */
1209 for (i = 1; i <= 32; ++i)
1210 if (!(nums & (1 << i))) {
1211 if (self->title_count == 1 || i == 1)
1212 self->title_count = i;
1215 /* dont display the number for the first window */
1216 if (self->title_count > 1) {
1217 char *vdata, *ndata;
1218 ndata = g_strdup_printf(" - [%u]", self->title_count);
1219 vdata = g_strconcat(data, ndata, NULL);
1225 PROP_SETS(self->window, net_wm_visible_name, data);
1230 frame_adjust_title(self->frame);
1232 /* update the icon title */
1234 g_free(self->icon_title);
1238 if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1239 /* try old x stuff */
1240 if (!PROP_GETS(self->window, wm_icon_name, locale, &data)) {
1241 data = g_strdup(self->title);
1245 /* append the title count, dont display the number for the first window */
1246 if (read_title && self->title_count > 1) {
1247 char *vdata, *ndata;
1248 ndata = g_strdup_printf(" - [%u]", self->title_count);
1249 vdata = g_strconcat(data, ndata, NULL);
1255 PROP_SETS(self->window, net_wm_visible_icon_name, data);
1257 self->icon_title = data;
1260 void client_update_class(Client *self)
1265 if (self->name) g_free(self->name);
1266 if (self->class) g_free(self->class);
1267 if (self->role) g_free(self->role);
1269 self->name = self->class = self->role = NULL;
1271 if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1273 self->name = g_strdup(data[0]);
1275 self->class = g_strdup(data[1]);
1280 if (PROP_GETS(self->window, wm_window_role, locale, &s))
1281 self->role = g_strdup(s);
1283 if (self->name == NULL) self->name = g_strdup("");
1284 if (self->class == NULL) self->class = g_strdup("");
1285 if (self->role == NULL) self->role = g_strdup("");
1288 void client_update_strut(Client *self)
1293 if (!PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1294 STRUT_SET(self->strut, 0, 0, 0, 0);
1297 STRUT_SET(self->strut, data[0], data[2], data[1], data[3]);
1299 STRUT_SET(self->strut, 0, 0, 0, 0);
1303 /* updating here is pointless while we're being mapped cuz we're not in
1304 the client list yet */
1306 screen_update_struts();
1309 void client_update_icons(Client *self)
1316 for (j = 0; j < self->nicons; ++j)
1317 g_free(self->icons[j].data);
1318 if (self->nicons > 0)
1319 g_free(self->icons);
1322 if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1323 /* figure out how many valid icons are in here */
1325 while (num - i > 2) {
1329 if (i > num || w*h == 0) break;
1333 self->icons = g_new(Icon, self->nicons);
1335 /* store the icons */
1337 for (j = 0; j < self->nicons; ++j) {
1340 w = self->icons[j].width = data[i++];
1341 h = self->icons[j].height = data[i++];
1343 if (w*h == 0) continue;
1345 self->icons[j].data = g_new(pixel32, w * h);
1346 for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1351 self->icons[j].data[t] =
1352 (((data[i] >> 24) & 0xff) << default_alpha_offset) +
1353 (((data[i] >> 16) & 0xff) << default_red_offset) +
1354 (((data[i] >> 8) & 0xff) << default_green_offset) +
1355 (((data[i] >> 0) & 0xff) << default_blue_offset);
1361 } else if (PROP_GETA32(self->window, kwm_win_icon,
1362 kwm_win_icon, &data, &num)) {
1365 self->icons = g_new(Icon, self->nicons);
1366 xerror_set_ignore(TRUE);
1367 if (!render_pixmap_to_rgba(data[0], data[1],
1368 &self->icons[self->nicons-1].width,
1369 &self->icons[self->nicons-1].height,
1370 &self->icons[self->nicons-1].data)) {
1371 g_free(&self->icons[self->nicons-1]);
1374 xerror_set_ignore(FALSE);
1380 if ((hints = XGetWMHints(ob_display, self->window))) {
1381 if (hints->flags & IconPixmapHint) {
1383 self->icons = g_new(Icon, self->nicons);
1384 xerror_set_ignore(TRUE);
1385 if (!render_pixmap_to_rgba(hints->icon_pixmap,
1386 (hints->flags & IconMaskHint ?
1387 hints->icon_mask : None),
1388 &self->icons[self->nicons-1].width,
1389 &self->icons[self->nicons-1].height,
1390 &self->icons[self->nicons-1].data)){
1391 g_free(&self->icons[self->nicons-1]);
1394 xerror_set_ignore(FALSE);
1401 frame_adjust_icon(self->frame);
1404 static void client_change_state(Client *self)
1407 guint32 netstate[10];
1410 state[0] = self->wmstate;
1412 PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1416 netstate[num++] = prop_atoms.net_wm_state_modal;
1418 netstate[num++] = prop_atoms.net_wm_state_shaded;
1420 netstate[num++] = prop_atoms.net_wm_state_hidden;
1421 if (self->skip_taskbar)
1422 netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1423 if (self->skip_pager)
1424 netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1425 if (self->fullscreen)
1426 netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1428 netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1430 netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1432 netstate[num++] = prop_atoms.net_wm_state_above;
1434 netstate[num++] = prop_atoms.net_wm_state_below;
1435 PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1437 client_calc_layer(self);
1440 frame_adjust_state(self->frame);
1443 Client *client_search_focus_tree(Client *self)
1448 for (it = self->transients; it != NULL; it = it->next) {
1449 if (client_focused(it->data)) return it->data;
1450 if ((ret = client_search_focus_tree(it->data))) return ret;
1455 Client *client_search_focus_tree_full(Client *self)
1457 if (self->transient_for) {
1458 if (self->transient_for != TRAN_GROUP) {
1459 return client_search_focus_tree_full(self->transient_for);
1462 gboolean recursed = FALSE;
1464 for (it = self->group->members; it; it = it->next)
1465 if (!((Client*)it->data)->transient_for) {
1467 if ((c = client_search_focus_tree_full(it->data)))
1476 /* this function checks the whole tree, the client_search_focus_tree~
1477 does not, so we need to check this window */
1478 if (client_focused(self))
1480 return client_search_focus_tree(self);
1483 static StackLayer calc_layer(Client *self)
1487 if (self->fullscreen) l = Layer_Fullscreen;
1488 else if (self->type == Type_Desktop) l = Layer_Desktop;
1489 else if (self->type == Type_Dock) {
1490 if (!self->below) l = Layer_Top;
1491 else l = Layer_Normal;
1493 else if (self->above) l = Layer_Above;
1494 else if (self->below) l = Layer_Below;
1495 else l = Layer_Normal;
1500 static void calc_recursive(Client *self, Client *orig, StackLayer l,
1503 StackLayer old, own;
1507 own = calc_layer(self);
1508 self->layer = l > own ? l : own;
1510 for (it = self->transients; it; it = it->next)
1511 calc_recursive(it->data, orig, l, raised ? raised : l != old);
1513 if (!raised && l != old)
1514 if (orig->frame) /* only restack if the original window is managed */
1515 stacking_raise(CLIENT_AS_WINDOW(self));
1518 void client_calc_layer(Client *self)
1525 /* transients take on the layer of their parents */
1526 if (self->transient_for) {
1527 if (self->transient_for != TRAN_GROUP) {
1528 self = self->transient_for;
1532 for (it = self->group->members; it; it = it->next)
1533 if (it->data != self &&
1534 !((Client*)it->data)->transient_for) {
1541 l = calc_layer(self);
1543 calc_recursive(self, orig, l, FALSE);
1546 gboolean client_should_show(Client *self)
1548 if (self->iconic) return FALSE;
1549 else if (!(self->desktop == screen_desktop ||
1550 self->desktop == DESKTOP_ALL)) return FALSE;
1551 else if (client_normal(self) && screen_showing_desktop) return FALSE;
1556 static void client_showhide(Client *self)
1559 if (client_should_show(self))
1560 frame_show(self->frame);
1562 frame_hide(self->frame);
1565 gboolean client_normal(Client *self) {
1566 return ! (self->type == Type_Desktop || self->type == Type_Dock ||
1567 self->type == Type_Splash);
1570 static void client_apply_startup_state(Client *self)
1572 /* these are in a carefully crafted order.. */
1575 self->iconic = FALSE;
1576 client_iconify(self, TRUE, FALSE);
1578 if (self->fullscreen) {
1579 self->fullscreen = FALSE;
1580 client_fullscreen(self, TRUE, FALSE);
1583 self->shaded = FALSE;
1584 client_shade(self, TRUE);
1587 dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1589 if (self->max_vert && self->max_horz) {
1590 self->max_vert = self->max_horz = FALSE;
1591 client_maximize(self, TRUE, 0, FALSE);
1592 } else if (self->max_vert) {
1593 self->max_vert = FALSE;
1594 client_maximize(self, TRUE, 2, FALSE);
1595 } else if (self->max_horz) {
1596 self->max_horz = FALSE;
1597 client_maximize(self, TRUE, 1, FALSE);
1600 /* nothing to do for the other states:
1609 void client_configure(Client *self, Corner anchor, int x, int y, int w, int h,
1610 gboolean user, gboolean final)
1612 gboolean moved = FALSE, resized = FALSE;
1614 /* gets the frame's position */
1615 frame_client_gravity(self->frame, &x, &y);
1617 /* these positions are frame positions, not client positions */
1619 /* set the size and position if fullscreen */
1620 if (self->fullscreen) {
1623 w = screen_physical_size.width;
1624 h = screen_physical_size.height;
1625 user = FALSE; /* ignore that increment etc shit when in fullscreen */
1627 /* set the size and position if maximized */
1628 if (self->max_horz) {
1629 x = screen_area(self->desktop)->x - self->frame->size.left;
1630 w = screen_area(self->desktop)->width;
1632 if (self->max_vert) {
1633 y = screen_area(self->desktop)->y;
1634 h = screen_area(self->desktop)->height -
1635 self->frame->size.top - self->frame->size.bottom;
1639 /* gets the client's position */
1640 frame_frame_gravity(self->frame, &x, &y);
1642 /* these override the above states! if you cant move you can't move! */
1644 if (!(self->functions & Func_Move)) {
1648 if (!(self->functions & Func_Resize)) {
1649 w = self->area.width;
1650 h = self->area.height;
1654 if (!(w == self->area.width && h == self->area.height)) {
1655 w -= self->base_size.width;
1656 h -= self->base_size.height;
1659 /* for interactive resizing. have to move half an increment in each
1662 /* how far we are towards the next size inc */
1663 int mw = w % self->size_inc.width;
1664 int mh = h % self->size_inc.height;
1666 int aw = self->size_inc.width / 2;
1667 int ah = self->size_inc.height / 2;
1668 /* don't let us move into a new size increment */
1669 if (mw + aw >= self->size_inc.width)
1670 aw = self->size_inc.width - mw - 1;
1671 if (mh + ah >= self->size_inc.height)
1672 ah = self->size_inc.height - mh - 1;
1676 /* if this is a user-requested resize, then check against min/max
1677 sizes and aspect ratios */
1679 /* smaller than min size or bigger than max size? */
1680 if (w > self->max_size.width) w = self->max_size.width;
1681 if (w < self->min_size.width) w = self->min_size.width;
1682 if (h > self->max_size.height) h = self->max_size.height;
1683 if (h < self->min_size.height) h = self->min_size.height;
1685 /* adjust the height ot match the width for the aspect ratios */
1686 if (self->min_ratio)
1687 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1688 if (self->max_ratio)
1689 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1692 /* keep to the increments */
1693 w /= self->size_inc.width;
1694 h /= self->size_inc.height;
1696 /* you cannot resize to nothing */
1700 /* store the logical size */
1701 SIZE_SET(self->logical_size, w, h);
1703 w *= self->size_inc.width;
1704 h *= self->size_inc.height;
1706 w += self->base_size.width;
1707 h += self->base_size.height;
1711 case Corner_TopLeft:
1713 case Corner_TopRight:
1714 x -= w - self->area.width;
1716 case Corner_BottomLeft:
1717 y -= h - self->area.height;
1719 case Corner_BottomRight:
1720 x -= w - self->area.width;
1721 y -= h - self->area.height;
1725 moved = x != self->area.x || y != self->area.y;
1726 resized = w != self->area.width || h != self->area.height;
1728 RECT_SET(self->area, x, y, w, h);
1731 XResizeWindow(ob_display, self->window, w, h);
1733 /* move/resize the frame to match the request */
1735 if (moved || resized)
1736 frame_adjust_area(self->frame, moved, resized);
1738 if (!user || final) {
1740 event.type = ConfigureNotify;
1741 event.xconfigure.display = ob_display;
1742 event.xconfigure.event = self->window;
1743 event.xconfigure.window = self->window;
1745 /* root window real coords */
1746 event.xconfigure.x = self->frame->area.x + self->frame->size.left;
1747 event.xconfigure.y = self->frame->area.y + self->frame->size.top;
1749 event.xconfigure.width = w;
1750 event.xconfigure.height = h;
1751 event.xconfigure.border_width = 0;
1752 event.xconfigure.above = self->frame->plate;
1753 event.xconfigure.override_redirect = FALSE;
1754 XSendEvent(event.xconfigure.display, event.xconfigure.window,
1755 FALSE, StructureNotifyMask, &event);
1760 void client_fullscreen(Client *self, gboolean fs, gboolean savearea)
1764 if (!(self->functions & Func_Fullscreen) || /* can't */
1765 self->fullscreen == fs) return; /* already done */
1767 self->fullscreen = fs;
1768 client_change_state(self); /* change the state hints on the client,
1769 and adjust out layer/stacking */
1773 guint32 dimensions[4];
1774 dimensions[0] = self->area.x;
1775 dimensions[1] = self->area.y;
1776 dimensions[2] = self->area.width;
1777 dimensions[3] = self->area.height;
1779 PROP_SETA32(self->window, openbox_premax, cardinal,
1783 /* these are not actually used cuz client_configure will set them
1784 as appropriate when the window is fullscreened */
1790 /* pick some fallbacks... */
1791 x = screen_area(self->desktop)->x +
1792 screen_area(self->desktop)->width / 4;
1793 y = screen_area(self->desktop)->y +
1794 screen_area(self->desktop)->height / 4;
1795 w = screen_area(self->desktop)->width / 2;
1796 h = screen_area(self->desktop)->height / 2;
1798 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1799 (guint32**)&dimensions, &num)) {
1810 client_setup_decor_and_functions(self);
1812 client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1814 /* try focus us when we go into fullscreen mode */
1818 void client_iconify(Client *self, gboolean iconic, gboolean curdesk)
1822 /* move up the transient chain as far as possible first */
1823 if (self->transient_for) {
1824 if (self->transient_for != TRAN_GROUP) {
1825 if (self->transient_for->iconic != iconic) {
1826 client_iconify(self->transient_for, iconic, curdesk);
1832 for (it = self->group->members; it; it = it->next) {
1833 Client *c = it->data;
1834 if (c != self && c->iconic != iconic &&
1835 !c->transient_for) {
1836 client_iconify(it->data, iconic, curdesk);
1840 if (it != NULL) return;
1844 if (self->iconic == iconic) return; /* nothing to do */
1846 g_message("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
1849 self->iconic = iconic;
1852 self->wmstate = IconicState;
1853 self->ignore_unmaps++;
1854 /* we unmap the client itself so that we can get MapRequest events,
1855 and because the ICCCM tells us to! */
1856 XUnmapWindow(ob_display, self->window);
1858 /* update the focus lists.. iconic windows go to the bottom of the
1859 list, put the new iconic window at the 'top of the bottom'. */
1860 focus_order_to_top(self);
1863 client_set_desktop(self, screen_desktop, FALSE);
1864 self->wmstate = self->shaded ? IconicState : NormalState;
1865 XMapWindow(ob_display, self->window);
1867 /* this puts it after the current focused window */
1868 focus_order_remove(self);
1869 focus_order_add_new(self);
1871 client_change_state(self);
1872 client_showhide(self);
1873 screen_update_struts();
1875 dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
1878 /* iconify all transients */
1879 for (it = self->transients; it != NULL; it = it->next)
1880 if (it->data != self) client_iconify(it->data, iconic, curdesk);
1883 void client_maximize(Client *self, gboolean max, int dir, gboolean savearea)
1887 g_assert(dir == 0 || dir == 1 || dir == 2);
1888 if (!(self->functions & Func_Maximize)) return; /* can't */
1890 /* check if already done */
1892 if (dir == 0 && self->max_horz && self->max_vert) return;
1893 if (dir == 1 && self->max_horz) return;
1894 if (dir == 2 && self->max_vert) return;
1896 if (dir == 0 && !self->max_horz && !self->max_vert) return;
1897 if (dir == 1 && !self->max_horz) return;
1898 if (dir == 2 && !self->max_vert) return;
1901 /* work with the frame's coords */
1902 x = self->frame->area.x;
1903 y = self->frame->area.y;
1904 w = self->area.width;
1905 h = self->area.height;
1909 gint32 dimensions[4];
1918 /* get the property off the window and use it for the dimensions
1919 we are already maxed on */
1920 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1921 (guint32**)&readdim, &num)) {
1923 if (self->max_horz) {
1924 dimensions[0] = readdim[0];
1925 dimensions[2] = readdim[2];
1927 if (self->max_vert) {
1928 dimensions[1] = readdim[1];
1929 dimensions[3] = readdim[3];
1935 PROP_SETA32(self->window, openbox_premax, cardinal,
1936 (guint32*)dimensions, 4);
1942 /* pick some fallbacks... */
1943 if (dir == 0 || dir == 1) { /* horz */
1944 x = screen_area(self->desktop)->x +
1945 screen_area(self->desktop)->width / 4;
1946 w = screen_area(self->desktop)->width / 2;
1948 if (dir == 0 || dir == 2) { /* vert */
1949 y = screen_area(self->desktop)->y +
1950 screen_area(self->desktop)->height / 4;
1951 h = screen_area(self->desktop)->height / 2;
1954 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1955 (guint32**)&dimensions, &num)) {
1957 if (dir == 0 || dir == 1) { /* horz */
1961 if (dir == 0 || dir == 2) { /* vert */
1970 if (dir == 0 || dir == 1) /* horz */
1971 self->max_horz = max;
1972 if (dir == 0 || dir == 2) /* vert */
1973 self->max_vert = max;
1975 if (!self->max_horz && !self->max_vert)
1976 PROP_ERASE(self->window, openbox_premax);
1978 client_change_state(self); /* change the state hints on the client */
1980 /* figure out where the client should be going */
1981 frame_frame_gravity(self->frame, &x, &y);
1982 client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1985 void client_shade(Client *self, gboolean shade)
1987 if ((!(self->functions & Func_Shade) && shade) || /* can't shade */
1988 self->shaded == shade) return; /* already done */
1990 /* when we're iconic, don't change the wmstate */
1992 self->wmstate = shade ? IconicState : NormalState;
1993 self->shaded = shade;
1994 client_change_state(self);
1995 /* resize the frame to just the titlebar */
1996 frame_adjust_area(self->frame, FALSE, FALSE);
1999 void client_close(Client *self)
2003 if (!(self->functions & Func_Close)) return;
2006 XXX: itd be cool to do timeouts and shit here for killing the client's
2008 like... if the window is around after 5 seconds, then the close button
2009 turns a nice red, and if this function is called again, the client is
2013 ce.xclient.type = ClientMessage;
2014 ce.xclient.message_type = prop_atoms.wm_protocols;
2015 ce.xclient.display = ob_display;
2016 ce.xclient.window = self->window;
2017 ce.xclient.format = 32;
2018 ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
2019 ce.xclient.data.l[1] = event_lasttime;
2020 ce.xclient.data.l[2] = 0l;
2021 ce.xclient.data.l[3] = 0l;
2022 ce.xclient.data.l[4] = 0l;
2023 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2026 void client_kill(Client *self)
2028 XKillClient(ob_display, self->window);
2031 void client_set_desktop(Client *self, guint target, gboolean donthide)
2035 if (target == self->desktop) return;
2037 g_message("Setting desktop %u", target+1);
2039 g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2041 /* remove from the old desktop(s) */
2042 focus_order_remove(self);
2044 old = self->desktop;
2045 self->desktop = target;
2046 PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2047 /* the frame can display the current desktop state */
2048 frame_adjust_state(self->frame);
2049 /* 'move' the window to the new desktop */
2051 client_showhide(self);
2052 /* raise if it was not already on the desktop */
2053 if (old != DESKTOP_ALL)
2054 stacking_raise(CLIENT_AS_WINDOW(self));
2055 screen_update_struts();
2057 /* add to the new desktop(s) */
2058 if (config_focus_new)
2059 focus_order_to_top(self);
2061 focus_order_to_bottom(self);
2063 dispatch_client(Event_Client_Desktop, self, target, old);
2066 Client *client_search_modal_child(Client *self)
2071 for (it = self->transients; it != NULL; it = it->next) {
2072 Client *c = it->data;
2073 if ((ret = client_search_modal_child(c))) return ret;
2074 if (c->modal) return c;
2079 gboolean client_validate(Client *self)
2083 XSync(ob_display, FALSE); /* get all events on the server */
2085 if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2086 XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2087 XPutBackEvent(ob_display, &e);
2094 void client_set_wm_state(Client *self, long state)
2096 if (state == self->wmstate) return; /* no change */
2100 client_iconify(self, TRUE, TRUE);
2103 client_iconify(self, FALSE, TRUE);
2108 void client_set_state(Client *self, Atom action, long data1, long data2)
2110 gboolean shaded = self->shaded;
2111 gboolean fullscreen = self->fullscreen;
2112 gboolean max_horz = self->max_horz;
2113 gboolean max_vert = self->max_vert;
2116 if (!(action == prop_atoms.net_wm_state_add ||
2117 action == prop_atoms.net_wm_state_remove ||
2118 action == prop_atoms.net_wm_state_toggle))
2119 /* an invalid action was passed to the client message, ignore it */
2122 for (i = 0; i < 2; ++i) {
2123 Atom state = i == 0 ? data1 : data2;
2125 if (!state) continue;
2127 /* if toggling, then pick whether we're adding or removing */
2128 if (action == prop_atoms.net_wm_state_toggle) {
2129 if (state == prop_atoms.net_wm_state_modal)
2130 action = self->modal ? prop_atoms.net_wm_state_remove :
2131 prop_atoms.net_wm_state_add;
2132 else if (state == prop_atoms.net_wm_state_maximized_vert)
2133 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2134 prop_atoms.net_wm_state_add;
2135 else if (state == prop_atoms.net_wm_state_maximized_horz)
2136 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2137 prop_atoms.net_wm_state_add;
2138 else if (state == prop_atoms.net_wm_state_shaded)
2139 action = self->shaded ? prop_atoms.net_wm_state_remove :
2140 prop_atoms.net_wm_state_add;
2141 else if (state == prop_atoms.net_wm_state_skip_taskbar)
2142 action = self->skip_taskbar ?
2143 prop_atoms.net_wm_state_remove :
2144 prop_atoms.net_wm_state_add;
2145 else if (state == prop_atoms.net_wm_state_skip_pager)
2146 action = self->skip_pager ?
2147 prop_atoms.net_wm_state_remove :
2148 prop_atoms.net_wm_state_add;
2149 else if (state == prop_atoms.net_wm_state_fullscreen)
2150 action = self->fullscreen ?
2151 prop_atoms.net_wm_state_remove :
2152 prop_atoms.net_wm_state_add;
2153 else if (state == prop_atoms.net_wm_state_above)
2154 action = self->above ? prop_atoms.net_wm_state_remove :
2155 prop_atoms.net_wm_state_add;
2156 else if (state == prop_atoms.net_wm_state_below)
2157 action = self->below ? prop_atoms.net_wm_state_remove :
2158 prop_atoms.net_wm_state_add;
2161 if (action == prop_atoms.net_wm_state_add) {
2162 if (state == prop_atoms.net_wm_state_modal) {
2163 /* XXX raise here or something? */
2165 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2167 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2169 } else if (state == prop_atoms.net_wm_state_shaded) {
2171 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2172 self->skip_taskbar = TRUE;
2173 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2174 self->skip_pager = TRUE;
2175 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2177 } else if (state == prop_atoms.net_wm_state_above) {
2179 } else if (state == prop_atoms.net_wm_state_below) {
2183 } else { /* action == prop_atoms.net_wm_state_remove */
2184 if (state == prop_atoms.net_wm_state_modal) {
2185 self->modal = FALSE;
2186 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2188 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2190 } else if (state == prop_atoms.net_wm_state_shaded) {
2192 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2193 self->skip_taskbar = FALSE;
2194 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2195 self->skip_pager = FALSE;
2196 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2198 } else if (state == prop_atoms.net_wm_state_above) {
2199 self->above = FALSE;
2200 } else if (state == prop_atoms.net_wm_state_below) {
2201 self->below = FALSE;
2205 if (max_horz != self->max_horz || max_vert != self->max_vert) {
2206 if (max_horz != self->max_horz && max_vert != self->max_vert) {
2208 if (max_horz == max_vert) { /* both going the same way */
2209 client_maximize(self, max_horz, 0, TRUE);
2211 client_maximize(self, max_horz, 1, TRUE);
2212 client_maximize(self, max_vert, 2, TRUE);
2216 if (max_horz != self->max_horz)
2217 client_maximize(self, max_horz, 1, TRUE);
2219 client_maximize(self, max_vert, 2, TRUE);
2222 /* change fullscreen state before shading, as it will affect if the window
2224 if (fullscreen != self->fullscreen)
2225 client_fullscreen(self, fullscreen, TRUE);
2226 if (shaded != self->shaded)
2227 client_shade(self, shaded);
2228 client_calc_layer(self);
2229 client_change_state(self); /* change the hint to reflect these changes */
2232 Client *client_focus_target(Client *self)
2236 /* if we have a modal child, then focus it, not us */
2237 child = client_search_modal_child(self);
2238 if (child) return child;
2242 gboolean client_can_focus(Client *self)
2244 /* same code as in client_focus */
2246 /* choose the correct target */
2247 self = client_focus_target(self);
2249 if (!self->frame->visible)
2252 if (!((self->can_focus || self->focus_notify) &&
2253 (self->desktop == screen_desktop ||
2254 self->desktop == DESKTOP_ALL) &&
2260 gboolean client_focus(Client *self)
2264 /* same code as in client_can_focus */
2266 /* choose the correct target */
2267 self = client_focus_target(self);
2269 if (!self->frame->visible) {
2270 /* update the focus lists */
2271 focus_order_to_top(self);
2275 if (!((self->can_focus || self->focus_notify) &&
2276 (self->desktop == screen_desktop ||
2277 self->desktop == DESKTOP_ALL) &&
2281 /* do a check to see if the window has already been unmapped or destroyed
2282 do this intelligently while watching out for unmaps we've generated
2283 (ignore_unmaps > 0) */
2284 if (XCheckTypedWindowEvent(ob_display, self->window,
2285 DestroyNotify, &ev)) {
2286 XPutBackEvent(ob_display, &ev);
2289 while (XCheckTypedWindowEvent(ob_display, self->window,
2290 UnmapNotify, &ev)) {
2291 if (self->ignore_unmaps) {
2292 self->ignore_unmaps--;
2294 XPutBackEvent(ob_display, &ev);
2299 if (self->can_focus)
2300 /* RevertToPointerRoot causes much more headache than RevertToNone, so
2301 I choose to use it always, hopefully to find errors quicker, if any
2302 are left. (I hate X. I hate focus events.) */
2303 XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2306 if (self->focus_notify) {
2308 ce.xclient.type = ClientMessage;
2309 ce.xclient.message_type = prop_atoms.wm_protocols;
2310 ce.xclient.display = ob_display;
2311 ce.xclient.window = self->window;
2312 ce.xclient.format = 32;
2313 ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2314 ce.xclient.data.l[1] = event_lasttime;
2315 ce.xclient.data.l[2] = 0l;
2316 ce.xclient.data.l[3] = 0l;
2317 ce.xclient.data.l[4] = 0l;
2318 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2322 g_message("%sively focusing %lx at %d", (self->can_focus ? "act" : "pass"),
2327 /* Cause the FocusIn to come back to us. Important for desktop switches,
2328 since otherwise we'll have no FocusIn on the queue and send it off to
2329 the focus_backup. */
2330 XSync(ob_display, FALSE);
2334 void client_unfocus(Client *self)
2336 g_assert(focus_client == self);
2338 g_message("client_unfocus for %lx", self->window);
2340 focus_fallback(Fallback_Unfocusing);
2343 void client_activate(Client *self)
2345 if (client_normal(self) && screen_showing_desktop)
2346 screen_show_desktop(FALSE);
2348 client_iconify(self, FALSE, TRUE);
2349 else if (!self->frame->visible)
2350 /* if its not visible for other reasons, then don't mess
2354 client_shade(self, FALSE);
2356 stacking_raise(CLIENT_AS_WINDOW(self));
2359 gboolean client_focused(Client *self)
2361 return self == focus_client;
2364 Icon *client_icon(Client *self, int w, int h)
2367 /* si is the smallest image >= req */
2368 /* li is the largest image < req */
2369 unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2371 if (!self->nicons) return NULL;
2373 for (i = 0; i < self->nicons; ++i) {
2374 size = self->icons[i].width * self->icons[i].height;
2375 if (size < smallest && size >= (unsigned)(w * h)) {
2379 if (size > largest && size <= (unsigned)(w * h)) {
2384 if (largest == 0) /* didnt find one smaller than the requested size */
2385 return &self->icons[si];
2386 return &self->icons[li];
2389 /* this be mostly ripped from fvwm */
2390 Client *client_find_directional(Client *c, Direction dir)
2392 int my_cx, my_cy, his_cx, his_cy;
2395 int score, best_score;
2396 Client *best_client, *cur;
2402 /* first, find the centre coords of the currently focused window */
2403 my_cx = c->frame->area.x + c->frame->area.width / 2;
2404 my_cy = c->frame->area.y + c->frame->area.height / 2;
2409 for(it = g_list_first(client_list); it; it = it->next) {
2412 /* the currently selected window isn't interesting */
2415 if (!client_normal(cur))
2417 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2421 if(client_focus_target(cur) == cur &&
2422 !(cur->can_focus || cur->focus_notify))
2425 /* find the centre coords of this window, from the
2426 * currently focused window's point of view */
2427 his_cx = (cur->frame->area.x - my_cx)
2428 + cur->frame->area.width / 2;
2429 his_cy = (cur->frame->area.y - my_cy)
2430 + cur->frame->area.height / 2;
2434 /* Rotate the diagonals 45 degrees counterclockwise.
2435 * To do this, multiply the matrix /+h +h\ with the
2436 * vector (x y). \-h +h/
2437 * h = sqrt(0.5). We can set h := 1 since absolute
2438 * distance doesn't matter here. */
2439 tx = his_cx + his_cy;
2440 his_cy = -his_cx + his_cy;
2445 case Direction_North :
2446 case Direction_South :
2447 case Direction_NorthEast :
2448 case Direction_SouthWest :
2449 offset = (his_cx < 0) ? -his_cx : his_cx;
2450 distance = (dir == Direction_North || dir == Direction_NorthEast) ?
2453 case Direction_East :
2454 case Direction_West :
2455 case Direction_SouthEast :
2456 case Direction_NorthWest :
2457 offset = (his_cy < 0) ? -his_cy : his_cy;
2458 distance = (dir == Direction_West || dir == Direction_NorthWest) ?
2463 /* the target must be in the requested direction */
2467 /* Calculate score for this window. The smaller the better. */
2468 score = distance + offset;
2470 /* windows more than 45 degrees off the direction are
2471 * heavily penalized and will only be chosen if nothing
2472 * else within a million pixels */
2473 if(offset > distance)
2476 if(best_score == -1 || score < best_score)
2484 void client_set_layer(Client *self, int layer)
2488 self->above = FALSE;
2489 } else if (layer == 0) {
2490 self->below = self->above = FALSE;
2492 self->below = FALSE;
2495 client_calc_layer(self);
2496 client_change_state(self); /* reflect this in the state hints */