5 #include "moveresize.h"
7 #include "extensions.h"
18 #include "render/render.h"
21 #include <X11/Xutil.h>
23 /*! The event mask to grab on client windows */
24 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
27 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
30 GList *client_list = NULL;
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_apply_startup_state(Client *self);
50 void client_shutdown()
54 void client_set_list()
56 Window *windows, *win_it;
58 guint size = g_list_length(client_list);
60 /* create an array of the window ids */
62 windows = g_new(Window, size);
64 for (it = client_list; it != NULL; it = it->next, ++win_it)
65 *win_it = ((Client*)it->data)->window;
69 PROP_SETA32(ob_root, net_client_list, window, (guint32*)windows, size);
78 void client_foreach_transient(Client *self, ClientForeachFunc func, void *data)
82 for (it = self->transients; it; it = it->next) {
83 if (!func(it->data, data)) return;
84 client_foreach_transient(it->data, func, data);
88 void client_foreach_ancestor(Client *self, ClientForeachFunc func, void *data)
90 if (self->transient_for) {
91 if (self->transient_for != TRAN_GROUP) {
92 if (!func(self->transient_for, data)) return;
93 client_foreach_ancestor(self->transient_for, func, data);
97 for (it = self->group->members; it; it = it->next)
98 if (it->data != self &&
99 !((Client*)it->data)->transient_for) {
100 if (!func(it->data, data)) return;
101 client_foreach_ancestor(it->data, func, data);
108 void client_manage_all()
110 unsigned int i, j, nchild;
113 XWindowAttributes attrib;
115 XQueryTree(ob_display, ob_root, &w, &w, &children, &nchild);
117 /* remove all icon windows from the list */
118 for (i = 0; i < nchild; i++) {
119 if (children[i] == None) continue;
120 wmhints = XGetWMHints(ob_display, children[i]);
122 if ((wmhints->flags & IconWindowHint) &&
123 (wmhints->icon_window != children[i]))
124 for (j = 0; j < nchild; j++)
125 if (children[j] == wmhints->icon_window) {
133 for (i = 0; i < nchild; ++i) {
134 if (children[i] == None)
136 if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
137 if (attrib.override_redirect) continue;
139 if (attrib.map_state != IsUnmapped)
140 client_manage(children[i]);
145 /* stack them as they were on startup!
146 why with stacking_lower? Why, because then windows who aren't in the
147 stacking list are on the top where you can see them instead of buried
149 for (i = startup_stack_size; i > 0; --i) {
152 w = startup_stack_order[i-1];
153 obw = g_hash_table_lookup(window_map, &w);
155 g_assert(WINDOW_IS_CLIENT(obw));
156 stacking_lower(CLIENT_AS_WINDOW(obw));
159 g_free(startup_stack_order);
160 startup_stack_order = NULL;
161 startup_stack_size = 0;
163 if (config_focus_new) {
166 active = g_hash_table_lookup(window_map, &startup_active);
168 g_assert(WINDOW_IS_CLIENT(active));
169 if (!client_focus(WINDOW_AS_CLIENT(active)))
170 focus_fallback(Fallback_NoFocus);
172 focus_fallback(Fallback_NoFocus);
176 void client_manage(Window window)
180 XWindowAttributes attrib;
181 XSetWindowAttributes attrib_set;
183 gboolean activate = FALSE;
187 /* check if it has already been unmapped by the time we started mapping
188 the grab does a sync so we don't have to here */
189 if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
190 XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
191 XPutBackEvent(ob_display, &e);
194 return; /* don't manage it */
197 /* make sure it isn't an override-redirect window */
198 if (!XGetWindowAttributes(ob_display, window, &attrib) ||
199 attrib.override_redirect) {
201 return; /* don't manage it */
204 /* is the window a docking app */
205 if ((wmhint = XGetWMHints(ob_display, window))) {
206 if ((wmhint->flags & StateHint) &&
207 wmhint->initial_state == WithdrawnState) {
208 dock_add(window, wmhint);
216 g_message("Managing window: %lx", window);
218 /* choose the events we want to receive on the CLIENT window */
219 attrib_set.event_mask = CLIENT_EVENTMASK;
220 attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
221 XChangeWindowAttributes(ob_display, window,
222 CWEventMask|CWDontPropagate, &attrib_set);
225 /* create the Client struct, and populate it from the hints on the
227 self = g_new(Client, 1);
228 self->obwin.type = Window_Client;
229 self->window = window;
230 client_get_all(self);
232 /* remove the client's border (and adjust re gravity) */
233 client_toggle_border(self, FALSE);
235 /* specify that if we exit, the window should not be destroyed and should
236 be reparented back to root automatically */
237 XChangeSaveSet(ob_display, window, SetModeInsert);
239 /* create the decoration frame for the client window */
240 self->frame = frame_new();
242 frame_grab_client(self->frame, self);
244 client_apply_startup_state(self);
248 /* add to client list/map */
249 client_list = g_list_append(client_list, self);
250 g_hash_table_insert(window_map, &self->window, self);
252 /* update the focus lists */
253 focus_order_add_new(self);
255 /* focus the new window? */
256 if (ob_state != State_Starting && config_focus_new &&
257 (self->type == Type_Normal || self->type == Type_Dialog)) {
258 gboolean group_foc = FALSE;
263 for (it = self->group->members; it; it = it->next)
264 if (client_focused(it->data)) {
269 /* note the check against Type_Normal/Dialog, not client_normal(self),
270 which would also include other types. in this case we want more
271 strict rules for focus */
273 (!self->transient_for && (!self->group ||
274 !self->group->members->next))) ||
275 client_search_focus_tree_full(self) ||
277 !client_normal(focus_client)) {
278 /* activate the window */
279 stacking_add(CLIENT_AS_WINDOW(self));
282 /* try to not get in the way */
283 stacking_add_nonintrusive(CLIENT_AS_WINDOW(self));
286 stacking_add(CLIENT_AS_WINDOW(self));
289 screen_update_struts();
291 /* make sure the window is visible */
292 client_move_onscreen(self);
294 dispatch_client(Event_Client_New, self, 0, 0);
296 client_showhide(self);
298 if (activate) client_activate(self);
300 /* update the list hints */
303 dispatch_client(Event_Client_Mapped, self, 0, 0);
305 g_message("Managed window 0x%lx (%s)", window, self->class);
308 void client_unmanage_all()
310 while (client_list != NULL)
311 client_unmanage(client_list->data);
314 /* called by client_unmanage() to close any menus referencing this client */
315 void client_close_menus(gpointer key, gpointer value, gpointer self)
317 if (((Menu *)value)->client == (Client *)self)
318 menu_hide((Menu *)value);
321 void client_unmanage(Client *self)
326 g_message("Unmanaging window: %lx (%s)", self->window, self->class);
328 dispatch_client(Event_Client_Destroy, self, 0, 0);
329 g_assert(self != NULL);
331 /* remove the window from our save set */
332 XChangeSaveSet(ob_display, self->window, SetModeDelete);
334 /* we dont want events no more */
335 XSelectInput(ob_display, self->window, NoEventMask);
337 frame_hide(self->frame);
339 client_list = g_list_remove(client_list, self);
340 stacking_remove(self);
341 g_hash_table_remove(window_map, &self->window);
343 /* update the focus lists */
344 focus_order_remove(self);
346 /* once the client is out of the list, update the struts to remove it's
348 screen_update_struts();
350 /* tell our parent(s) that we're gone */
351 if (self->transient_for == TRAN_GROUP) { /* transient of group */
354 for (it = self->group->members; it; it = it->next)
355 if (it->data != self)
356 ((Client*)it->data)->transients =
357 g_slist_remove(((Client*)it->data)->transients, self);
358 } else if (self->transient_for) { /* transient of window */
359 self->transient_for->transients =
360 g_slist_remove(self->transient_for->transients, self);
363 /* tell our transients that we're gone */
364 for (it = self->transients; it != NULL; it = it->next) {
365 if (((Client*)it->data)->transient_for != TRAN_GROUP) {
366 ((Client*)it->data)->transient_for = NULL;
367 client_calc_layer(it->data);
371 if (moveresize_client == self)
372 moveresize_end(TRUE);
374 /* close any windows that are attached to this window */
375 g_hash_table_foreach(menu_hash, client_close_menus, self);
378 if (focus_client == self) {
381 /* focus the last focused window on the desktop, and ignore enter
382 events from the unmap so it doesnt mess with the focus */
383 while (XCheckTypedEvent(ob_display, EnterNotify, &e));
384 client_unfocus(self);
387 /* remove from its group */
389 group_remove(self->group, self);
393 /* dispatch the unmapped event */
394 dispatch_client(Event_Client_Unmapped, self, 0, 0);
395 g_assert(self != NULL);
397 /* give the client its border back */
398 client_toggle_border(self, TRUE);
400 /* reparent the window out of the frame, and free the frame */
401 frame_release_client(self->frame, self);
404 if (ob_state != State_Exiting) {
405 /* these values should not be persisted across a window
407 prop_erase(self->window, prop_atoms.net_wm_desktop);
408 prop_erase(self->window, prop_atoms.net_wm_state);
410 /* if we're left in an iconic state, the client wont be mapped. this is
411 bad, since we will no longer be managing the window on restart */
413 XMapWindow(ob_display, self->window);
417 g_message("Unmanaged window 0x%lx", self->window);
419 /* free all data allocated in the client struct */
420 g_slist_free(self->transients);
421 for (j = 0; j < self->nicons; ++j)
422 g_free(self->icons[j].data);
423 if (self->nicons > 0)
426 g_free(self->icon_title);
432 /* update the list hints */
436 void client_move_onscreen(Client *self)
439 int x = self->frame->area.x, y = self->frame->area.y;
441 a = screen_area(self->desktop);
442 if (x >= a->x + a->width - 1)
443 x = a->x + a->width - self->frame->area.width;
444 if (y >= a->y + a->height - 1)
445 y = a->y + a->height - self->frame->area.height;
446 if (x + self->frame->area.width - 1 < a->x)
448 if (y + self->frame->area.height - 1< a->y)
451 frame_frame_gravity(self->frame, &x, &y); /* get where the client
453 client_configure(self , Corner_TopLeft, x, y,
454 self->area.width, self->area.height,
458 static void client_toggle_border(Client *self, gboolean show)
460 /* adjust our idea of where the client is, based on its border. When the
461 border is removed, the client should now be considered to be in a
463 when re-adding the border to the client, the same operation needs to be
465 int oldx = self->area.x, oldy = self->area.y;
466 int x = oldx, y = oldy;
467 switch(self->gravity) {
469 case NorthWestGravity:
471 case SouthWestGravity:
473 case NorthEastGravity:
475 case SouthEastGravity:
476 if (show) x -= self->border_width * 2;
477 else x += self->border_width * 2;
484 if (show) x -= self->border_width;
485 else x += self->border_width;
488 switch(self->gravity) {
490 case NorthWestGravity:
492 case NorthEastGravity:
494 case SouthWestGravity:
496 case SouthEastGravity:
497 if (show) y -= self->border_width * 2;
498 else y += self->border_width * 2;
505 if (show) y -= self->border_width;
506 else y += self->border_width;
513 XSetWindowBorderWidth(ob_display, self->window, self->border_width);
515 /* move the client so it is back it the right spot _with_ its
517 if (x != oldx || y != oldy)
518 XMoveWindow(ob_display, self->window, x, y);
520 XSetWindowBorderWidth(ob_display, self->window, 0);
524 static void client_get_all(Client *self)
526 /* update EVERYTHING!! */
528 self->ignore_unmaps = 0;
532 self->title = self->icon_title = NULL;
533 self->title_count = 1;
534 self->name = self->class = self->role = NULL;
535 self->wmstate = NormalState;
536 self->transient = FALSE;
537 self->transients = NULL;
538 self->transient_for = NULL;
540 self->urgent = FALSE;
541 self->positioned = FALSE;
542 self->disabled_decorations = 0;
546 client_get_area(self);
547 client_update_transient_for(self);
548 client_update_wmhints(self);
549 client_get_desktop(self);
550 client_get_state(self);
551 client_get_shaped(self);
553 client_get_mwm_hints(self);
554 client_get_type(self);/* this can change the mwmhints for special cases */
556 client_update_protocols(self);
558 client_get_gravity(self); /* get the attribute gravity */
559 client_update_normal_hints(self); /* this may override the attribute
562 /* got the type, the mwmhints, the protocols, and the normal hints
563 (min/max sizes), so we're ready to set up the decorations/functions */
564 client_setup_decor_and_functions(self);
566 client_update_title(self);
567 client_update_class(self);
568 client_update_strut(self);
569 client_update_icons(self);
571 client_change_state(self);
574 static void client_get_area(Client *self)
576 XWindowAttributes wattrib;
579 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
580 g_assert(ret != BadWindow);
582 RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
583 self->border_width = wattrib.border_width;
586 static void client_get_desktop(Client *self)
590 if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
591 if (d >= screen_num_desktops && d != DESKTOP_ALL)
592 d = screen_num_desktops - 1;
595 gboolean trdesk = FALSE;
597 if (self->transient_for) {
598 if (self->transient_for != TRAN_GROUP) {
599 self->desktop = self->transient_for->desktop;
604 for (it = self->group->members; it; it = it->next)
605 if (it->data != self &&
606 !((Client*)it->data)->transient_for) {
607 self->desktop = ((Client*)it->data)->desktop;
614 /* defaults to the current desktop */
615 self->desktop = screen_desktop;
617 /* set the desktop hint, to make sure that it always exists */
618 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
622 static void client_get_state(Client *self)
627 self->modal = self->shaded = self->max_horz = self->max_vert =
628 self->fullscreen = self->above = self->below = self->iconic =
629 self->skip_taskbar = self->skip_pager = FALSE;
631 if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
633 for (i = 0; i < num; ++i) {
634 if (state[i] == prop_atoms.net_wm_state_modal)
636 else if (state[i] == prop_atoms.net_wm_state_shaded)
638 else if (state[i] == prop_atoms.net_wm_state_hidden)
640 else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
641 self->skip_taskbar = TRUE;
642 else if (state[i] == prop_atoms.net_wm_state_skip_pager)
643 self->skip_pager = TRUE;
644 else if (state[i] == prop_atoms.net_wm_state_fullscreen)
645 self->fullscreen = TRUE;
646 else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
647 self->max_vert = TRUE;
648 else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
649 self->max_horz = TRUE;
650 else if (state[i] == prop_atoms.net_wm_state_above)
652 else if (state[i] == prop_atoms.net_wm_state_below)
660 static void client_get_shaped(Client *self)
662 self->shaped = FALSE;
664 if (extensions_shape) {
669 XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
671 XShapeQueryExtents(ob_display, self->window, &s, &foo,
672 &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
674 self->shaped = (s != 0);
679 void client_update_transient_for(Client *self)
684 if (XGetTransientForHint(ob_display, self->window, &t)) {
685 self->transient = TRUE;
686 if (t != self->window) { /* cant be transient to itself! */
687 c = g_hash_table_lookup(window_map, &t);
688 /* if this happens then we need to check for it*/
690 g_assert(!c || WINDOW_IS_CLIENT(c));
692 if (!c && self->group) {
693 /* not transient to a client, see if it is transient for a
695 if (t == self->group->leader ||
698 /* window is a transient for its group! */
704 self->transient = FALSE;
706 /* if anything has changed... */
707 if (c != self->transient_for) {
708 if (self->transient_for == TRAN_GROUP) { /* transient of group */
711 /* remove from old parents */
712 for (it = self->group->members; it; it = it->next)
713 if (it->data != self &&
714 !((Client*)it->data)->transient_for)
715 ((Client*)it->data)->transients =
716 g_slist_remove(((Client*)it->data)->transients, self);
717 } else if (self->transient_for != NULL) { /* transient of window */
718 /* remove from old parent */
719 self->transient_for->transients =
720 g_slist_remove(self->transient_for->transients, self);
722 self->transient_for = c;
723 if (self->transient_for == TRAN_GROUP) { /* transient of group */
726 /* add to new parents */
727 for (it = self->group->members; it; it = it->next)
728 if (it->data != self &&
729 !((Client*)it->data)->transient_for)
730 ((Client*)it->data)->transients =
731 g_slist_append(((Client*)it->data)->transients, self);
733 /* remove all transients which are in the group, that causes
734 circlular pointer hell of doom */
735 for (it = self->group->members; it; it = it->next) {
737 for (sit = self->transients; sit; sit = next) {
739 if (sit->data == it->data)
740 self->transients = g_slist_remove(self->transients,
744 } else if (self->transient_for != NULL) { /* transient of window */
745 /* add to new parent */
746 self->transient_for->transients =
747 g_slist_append(self->transient_for->transients, self);
752 static void client_get_mwm_hints(Client *self)
757 self->mwmhints.flags = 0; /* default to none */
759 if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
761 if (num >= MWM_ELEMENTS) {
762 self->mwmhints.flags = hints[0];
763 self->mwmhints.functions = hints[1];
764 self->mwmhints.decorations = hints[2];
770 void client_get_type(Client *self)
777 if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
778 /* use the first value that we know about in the array */
779 for (i = 0; i < num; ++i) {
780 if (val[i] == prop_atoms.net_wm_window_type_desktop)
781 self->type = Type_Desktop;
782 else if (val[i] == prop_atoms.net_wm_window_type_dock)
783 self->type = Type_Dock;
784 else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
785 self->type = Type_Toolbar;
786 else if (val[i] == prop_atoms.net_wm_window_type_menu)
787 self->type = Type_Menu;
788 else if (val[i] == prop_atoms.net_wm_window_type_utility)
789 self->type = Type_Utility;
790 else if (val[i] == prop_atoms.net_wm_window_type_splash)
791 self->type = Type_Splash;
792 else if (val[i] == prop_atoms.net_wm_window_type_dialog)
793 self->type = Type_Dialog;
794 else if (val[i] == prop_atoms.net_wm_window_type_normal)
795 self->type = Type_Normal;
796 else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
797 /* prevent this window from getting any decor or
799 self->mwmhints.flags &= (MwmFlag_Functions |
800 MwmFlag_Decorations);
801 self->mwmhints.decorations = 0;
802 self->mwmhints.functions = 0;
804 if (self->type != (WindowType) -1)
805 break; /* grab the first legit type */
810 if (self->type == (WindowType) -1) {
811 /*the window type hint was not set, which means we either classify
812 ourself as a normal window or a dialog, depending on if we are a
815 self->type = Type_Dialog;
817 self->type = Type_Normal;
821 void client_update_protocols(Client *self)
826 self->focus_notify = FALSE;
827 self->delete_window = FALSE;
829 if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
830 for (i = 0; i < num_return; ++i) {
831 if (proto[i] == prop_atoms.wm_delete_window) {
832 /* this means we can request the window to close */
833 self->delete_window = TRUE;
834 } else if (proto[i] == prop_atoms.wm_take_focus)
835 /* if this protocol is requested, then the window will be
836 notified whenever we want it to receive focus */
837 self->focus_notify = TRUE;
843 static void client_get_gravity(Client *self)
845 XWindowAttributes wattrib;
848 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
849 g_assert(ret != BadWindow);
850 self->gravity = wattrib.win_gravity;
853 void client_update_normal_hints(Client *self)
857 int oldgravity = self->gravity;
860 self->min_ratio = 0.0f;
861 self->max_ratio = 0.0f;
862 SIZE_SET(self->size_inc, 1, 1);
863 SIZE_SET(self->base_size, 0, 0);
864 SIZE_SET(self->min_size, 0, 0);
865 SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
867 /* get the hints from the window */
868 if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
869 self->positioned = !!(size.flags & (PPosition|USPosition));
871 if (size.flags & PWinGravity) {
872 self->gravity = size.win_gravity;
874 /* if the client has a frame, i.e. has already been mapped and
875 is changing its gravity */
876 if (self->frame && self->gravity != oldgravity) {
877 /* move our idea of the client's position based on its new
879 self->area.x = self->frame->area.x;
880 self->area.y = self->frame->area.y;
881 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
885 if (size.flags & PAspect) {
886 if (size.min_aspect.y)
887 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
888 if (size.max_aspect.y)
889 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
892 if (size.flags & PMinSize)
893 SIZE_SET(self->min_size, size.min_width, size.min_height);
895 if (size.flags & PMaxSize)
896 SIZE_SET(self->max_size, size.max_width, size.max_height);
898 if (size.flags & PBaseSize)
899 SIZE_SET(self->base_size, size.base_width, size.base_height);
901 if (size.flags & PResizeInc)
902 SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
906 void client_setup_decor_and_functions(Client *self)
908 /* start with everything (cept fullscreen) */
909 self->decorations = Decor_Titlebar | Decor_Handle | Decor_Border |
910 Decor_Icon | Decor_AllDesktops | Decor_Iconify | Decor_Maximize |
912 self->functions = Func_Resize | Func_Move | Func_Iconify | Func_Maximize |
914 if (self->delete_window) {
915 self->decorations |= Decor_Close;
916 self->functions |= Func_Close;
919 if (!(self->min_size.width < self->max_size.width ||
920 self->min_size.height < self->max_size.height)) {
921 self->decorations &= ~(Decor_Maximize | Decor_Handle);
922 self->functions &= ~(Func_Resize | Func_Maximize);
925 switch (self->type) {
927 /* normal windows retain all of the possible decorations and
928 functionality, and are the only windows that you can fullscreen */
929 self->functions |= Func_Fullscreen;
934 /* these windows cannot be maximized */
935 self->decorations &= ~Decor_Maximize;
936 self->functions &= ~Func_Maximize;
941 /* these windows get less functionality */
942 self->decorations &= ~(Decor_Iconify | Decor_Handle);
943 self->functions &= ~(Func_Iconify | Func_Resize);
949 /* none of these windows are manipulated by the window manager */
950 self->decorations = 0;
955 /* Mwm Hints are applied subtractively to what has already been chosen for
956 decor and functionality */
957 if (self->mwmhints.flags & MwmFlag_Decorations) {
958 if (! (self->mwmhints.decorations & MwmDecor_All)) {
959 if (! (self->mwmhints.decorations & MwmDecor_Border))
960 self->decorations &= ~Decor_Border;
961 if (! (self->mwmhints.decorations & MwmDecor_Handle))
962 self->decorations &= ~Decor_Handle;
963 if (! (self->mwmhints.decorations & MwmDecor_Title))
964 self->decorations &= ~Decor_Titlebar;
965 if (! (self->mwmhints.decorations & MwmDecor_Iconify))
966 self->decorations &= ~Decor_Iconify;
967 if (! (self->mwmhints.decorations & MwmDecor_Maximize))
968 self->decorations &= ~Decor_Maximize;
972 if (self->mwmhints.flags & MwmFlag_Functions) {
973 if (! (self->mwmhints.functions & MwmFunc_All)) {
974 if (! (self->mwmhints.functions & MwmFunc_Resize))
975 self->functions &= ~Func_Resize;
976 if (! (self->mwmhints.functions & MwmFunc_Move))
977 self->functions &= ~Func_Move;
978 if (! (self->mwmhints.functions & MwmFunc_Iconify))
979 self->functions &= ~Func_Iconify;
980 if (! (self->mwmhints.functions & MwmFunc_Maximize))
981 self->functions &= ~Func_Maximize;
982 /* dont let mwm hints kill the close button
983 if (! (self->mwmhints.functions & MwmFunc_Close))
984 self->functions &= ~Func_Close; */
988 /* can't maximize without moving/resizing */
989 if (!((self->functions & Func_Move) && (self->functions & Func_Resize)))
990 self->functions &= ~(Func_Maximize | Func_Fullscreen);
992 /* finally, user specified disabled decorations are applied to subtract
994 if (self->disabled_decorations & Decor_Titlebar)
995 self->decorations &= ~Decor_Titlebar;
996 if (self->disabled_decorations & Decor_Handle)
997 self->decorations &= ~Decor_Handle;
998 if (self->disabled_decorations & Decor_Border)
999 self->decorations &= ~Decor_Border;
1000 if (self->disabled_decorations & Decor_Iconify)
1001 self->decorations &= ~Decor_Iconify;
1002 if (self->disabled_decorations & Decor_Maximize)
1003 self->decorations &= ~Decor_Maximize;
1004 if (self->disabled_decorations & Decor_AllDesktops)
1005 self->decorations &= ~Decor_AllDesktops;
1006 if (self->disabled_decorations & Decor_Shade)
1007 self->decorations &= ~Decor_Shade;
1008 if (self->disabled_decorations & Decor_Close)
1009 self->decorations &= ~Decor_Close;
1011 /* if we don't have a titlebar, then we cannot shade! */
1012 if (!(self->decorations & Decor_Titlebar))
1013 self->functions &= ~Func_Shade;
1015 /* now we need to check against rules for the client's current state */
1016 if (self->fullscreen) {
1017 self->functions &= (Func_Close | Func_Fullscreen | Func_Iconify);
1018 self->decorations = 0;
1021 client_change_allowed_actions(self);
1024 /* this makes sure that these windows appear on all desktops */
1025 if (self->type == Type_Desktop && self->desktop != DESKTOP_ALL)
1026 client_set_desktop(self, DESKTOP_ALL, FALSE);
1028 /* change the decors on the frame, and with more/less decorations,
1029 we may also need to be repositioned */
1030 frame_adjust_area(self->frame, TRUE, TRUE);
1031 /* with new decor, the window's maximized size may change */
1032 client_remaximize(self);
1034 /* this makes sure that these windows appear on all desktops */
1035 if (self->type == Type_Desktop && self->desktop != DESKTOP_ALL)
1036 self->desktop = DESKTOP_ALL;
1040 static void client_change_allowed_actions(Client *self)
1045 /* desktop windows are kept on all desktops */
1046 if (self->type != Type_Desktop)
1047 actions[num++] = prop_atoms.net_wm_action_change_desktop;
1049 if (self->functions & Func_Shade)
1050 actions[num++] = prop_atoms.net_wm_action_shade;
1051 if (self->functions & Func_Close)
1052 actions[num++] = prop_atoms.net_wm_action_close;
1053 if (self->functions & Func_Move)
1054 actions[num++] = prop_atoms.net_wm_action_move;
1055 if (self->functions & Func_Iconify)
1056 actions[num++] = prop_atoms.net_wm_action_minimize;
1057 if (self->functions & Func_Resize)
1058 actions[num++] = prop_atoms.net_wm_action_resize;
1059 if (self->functions & Func_Fullscreen)
1060 actions[num++] = prop_atoms.net_wm_action_fullscreen;
1061 if (self->functions & Func_Maximize) {
1062 actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1063 actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1066 PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1068 /* make sure the window isn't breaking any rules now */
1070 if (!(self->functions & Func_Shade) && self->shaded) {
1071 if (self->frame) client_shade(self, FALSE);
1072 else self->shaded = FALSE;
1074 if (!(self->functions & Func_Iconify) && self->iconic) {
1075 g_message("UNSETTING ICONIC");
1076 if (self->frame) client_iconify(self, FALSE, TRUE);
1077 else self->iconic = FALSE;
1079 if (!(self->functions & Func_Fullscreen) && self->fullscreen) {
1080 if (self->frame) client_fullscreen(self, FALSE, TRUE);
1081 else self->fullscreen = FALSE;
1083 if (!(self->functions & Func_Maximize) && (self->max_horz ||
1085 if (self->frame) client_maximize(self, FALSE, 0, TRUE);
1086 else self->max_vert = self->max_horz = FALSE;
1090 void client_remaximize(Client *self)
1093 if (self->max_horz && self->max_vert)
1095 else if (self->max_horz)
1097 else if (self->max_vert)
1100 return; /* not maximized */
1101 self->max_horz = self->max_vert = FALSE;
1102 client_maximize(self, TRUE, dir, FALSE);
1105 void client_update_wmhints(Client *self)
1108 gboolean ur = FALSE;
1111 /* assume a window takes input if it doesnt specify */
1112 self->can_focus = TRUE;
1114 if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1115 if (hints->flags & InputHint)
1116 self->can_focus = hints->input;
1118 /* only do this when first managing the window *AND* when we aren't
1120 if (ob_state != State_Starting && self->frame == NULL)
1121 if (hints->flags & StateHint)
1122 self->iconic = hints->initial_state == IconicState;
1124 if (hints->flags & XUrgencyHint)
1127 if (!(hints->flags & WindowGroupHint))
1128 hints->window_group = None;
1130 /* did the group state change? */
1131 if (hints->window_group !=
1132 (self->group ? self->group->leader : None)) {
1133 /* remove from the old group if there was one */
1134 if (self->group != NULL) {
1135 /* remove transients of the group */
1136 for (it = self->group->members; it; it = it->next)
1137 self->transients = g_slist_remove(self->transients,
1139 group_remove(self->group, self);
1142 /* i can only have transients from the group if i am not transient
1144 if (hints->window_group != None && !self->transient_for) {
1145 self->group = group_add(hints->window_group, self);
1147 /* add other transients of the group that are already
1149 for (it = self->group->members; it; it = it->next)
1150 if (it->data != self &&
1151 ((Client*)it->data)->transient_for == TRAN_GROUP)
1152 self->transients = g_slist_append(self->transients,
1156 /* the WM_HINTS can contain an icon */
1157 client_update_icons(self);
1159 /* because the self->transient flag wont change from this call,
1160 we don't need to update the window's type and such, only its
1161 transient_for, and the transients lists of other windows in
1162 the group may be affected */
1163 client_update_transient_for(self);
1169 if (ur != self->urgent) {
1171 g_message("Urgent Hint for 0x%lx: %s", self->window,
1173 /* fire the urgent callback if we're mapped, otherwise, wait until
1174 after we're mapped */
1176 dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1180 void client_update_title(Client *self)
1186 gboolean read_title;
1188 g_free(self->title);
1191 if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1192 /* try old x stuff */
1193 if (!PROP_GETS(self->window, wm_name, locale, &data))
1194 data = g_strdup("Unnamed Window");
1196 /* look for duplicates and append a number */
1198 for (it = client_list; it; it = it->next)
1199 if (it->data != self) {
1200 Client *c = it->data;
1201 if (0 == strncmp(c->title, data, strlen(data)))
1202 nums |= 1 << c->title_count;
1204 /* find first free number */
1205 for (i = 1; i <= 32; ++i)
1206 if (!(nums & (1 << i))) {
1207 if (self->title_count == 1 || i == 1)
1208 self->title_count = i;
1211 /* dont display the number for the first window */
1212 if (self->title_count > 1) {
1213 char *vdata, *ndata;
1214 ndata = g_strdup_printf(" - [%u]", self->title_count);
1215 vdata = g_strconcat(data, ndata, NULL);
1221 PROP_SETS(self->window, net_wm_visible_name, data);
1226 frame_adjust_title(self->frame);
1228 /* update the icon title */
1230 g_free(self->icon_title);
1234 if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1235 /* try old x stuff */
1236 if (!PROP_GETS(self->window, wm_icon_name, locale, &data)) {
1237 data = g_strdup(self->title);
1241 /* append the title count, dont display the number for the first window */
1242 if (read_title && self->title_count > 1) {
1243 char *vdata, *ndata;
1244 ndata = g_strdup_printf(" - [%u]", self->title_count);
1245 vdata = g_strconcat(data, ndata, NULL);
1251 PROP_SETS(self->window, net_wm_visible_icon_name, data);
1253 self->icon_title = data;
1256 void client_update_class(Client *self)
1261 if (self->name) g_free(self->name);
1262 if (self->class) g_free(self->class);
1263 if (self->role) g_free(self->role);
1265 self->name = self->class = self->role = NULL;
1267 if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1269 self->name = g_strdup(data[0]);
1271 self->class = g_strdup(data[1]);
1276 if (PROP_GETS(self->window, wm_window_role, locale, &s))
1277 self->role = g_strdup(s);
1279 if (self->name == NULL) self->name = g_strdup("");
1280 if (self->class == NULL) self->class = g_strdup("");
1281 if (self->role == NULL) self->role = g_strdup("");
1284 void client_update_strut(Client *self)
1289 if (!PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1290 STRUT_SET(self->strut, 0, 0, 0, 0);
1293 STRUT_SET(self->strut, data[0], data[2], data[1], data[3]);
1295 STRUT_SET(self->strut, 0, 0, 0, 0);
1299 /* updating here is pointless while we're being mapped cuz we're not in
1300 the client list yet */
1302 screen_update_struts();
1305 void client_update_icons(Client *self)
1312 for (j = 0; j < self->nicons; ++j)
1313 g_free(self->icons[j].data);
1314 if (self->nicons > 0)
1315 g_free(self->icons);
1318 if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1319 /* figure out how many valid icons are in here */
1321 while (num - i > 2) {
1329 self->icons = g_new(Icon, self->nicons);
1331 /* store the icons */
1333 for (j = 0; j < self->nicons; ++j) {
1336 w = self->icons[j].width = data[i++];
1337 h = self->icons[j].height = data[i++];
1339 if (w*h == 0) continue;
1341 self->icons[j].data = g_new(pixel32, w * h);
1342 for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1347 self->icons[j].data[t] =
1348 (((data[i] >> 24) & 0xff) << default_alpha_offset) +
1349 (((data[i] >> 16) & 0xff) << default_red_offset) +
1350 (((data[i] >> 8) & 0xff) << default_green_offset) +
1351 (((data[i] >> 0) & 0xff) << default_blue_offset);
1357 } else if (PROP_GETA32(self->window, kwm_win_icon,
1358 kwm_win_icon, &data, &num)) {
1361 self->icons = g_new(Icon, self->nicons);
1362 if (!render_pixmap_to_rgba(data[0], data[1],
1363 &self->icons[self->nicons-1].width,
1364 &self->icons[self->nicons-1].height,
1365 &self->icons[self->nicons-1].data)) {
1366 g_free(&self->icons[self->nicons-1]);
1374 if ((hints = XGetWMHints(ob_display, self->window))) {
1375 if (hints->flags & IconPixmapHint) {
1377 self->icons = g_new(Icon, self->nicons);
1378 if (!render_pixmap_to_rgba(hints->icon_pixmap,
1379 (hints->flags & IconMaskHint ?
1380 hints->icon_mask : None),
1381 &self->icons[self->nicons-1].width,
1382 &self->icons[self->nicons-1].height,
1383 &self->icons[self->nicons-1].data)){
1384 g_free(&self->icons[self->nicons-1]);
1393 frame_adjust_icon(self->frame);
1396 static void client_change_state(Client *self)
1399 guint32 netstate[10];
1402 state[0] = self->wmstate;
1404 PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1408 netstate[num++] = prop_atoms.net_wm_state_modal;
1410 netstate[num++] = prop_atoms.net_wm_state_shaded;
1412 netstate[num++] = prop_atoms.net_wm_state_hidden;
1413 if (self->skip_taskbar)
1414 netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1415 if (self->skip_pager)
1416 netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1417 if (self->fullscreen)
1418 netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1420 netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1422 netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1424 netstate[num++] = prop_atoms.net_wm_state_above;
1426 netstate[num++] = prop_atoms.net_wm_state_below;
1427 PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1429 client_calc_layer(self);
1432 frame_adjust_state(self->frame);
1435 Client *client_search_focus_tree(Client *self)
1440 for (it = self->transients; it != NULL; it = it->next) {
1441 if (client_focused(it->data)) return it->data;
1442 if ((ret = client_search_focus_tree(it->data))) return ret;
1447 Client *client_search_focus_tree_full(Client *self)
1449 if (self->transient_for) {
1450 if (self->transient_for != TRAN_GROUP) {
1451 return client_search_focus_tree_full(self->transient_for);
1454 gboolean recursed = FALSE;
1456 for (it = self->group->members; it; it = it->next)
1457 if (!((Client*)it->data)->transient_for) {
1459 if ((c = client_search_focus_tree_full(it->data)))
1468 /* this function checks the whole tree, the client_search_focus_tree~
1469 does not, so we need to check this window */
1470 if (client_focused(self))
1472 return client_search_focus_tree(self);
1475 static StackLayer calc_layer(Client *self)
1479 if (self->fullscreen) l = Layer_Fullscreen;
1480 else if (self->type == Type_Desktop) l = Layer_Desktop;
1481 else if (self->type == Type_Dock) {
1482 if (!self->below) l = Layer_Top;
1483 else l = Layer_Normal;
1485 else if (self->above) l = Layer_Above;
1486 else if (self->below) l = Layer_Below;
1487 else l = Layer_Normal;
1492 static void calc_recursive(Client *self, Client *orig, StackLayer l,
1495 StackLayer old, own;
1499 own = calc_layer(self);
1500 self->layer = l > own ? l : own;
1502 for (it = self->transients; it; it = it->next)
1503 calc_recursive(it->data, orig, l, raised ? raised : l != old);
1505 if (!raised && l != old)
1506 if (orig->frame) /* only restack if the original window is managed */
1507 stacking_raise(CLIENT_AS_WINDOW(self));
1510 void client_calc_layer(Client *self)
1517 /* transients take on the layer of their parents */
1518 if (self->transient_for) {
1519 if (self->transient_for != TRAN_GROUP) {
1520 self = self->transient_for;
1524 for (it = self->group->members; it; it = it->next)
1525 if (it->data != self &&
1526 !((Client*)it->data)->transient_for) {
1533 l = calc_layer(self);
1535 calc_recursive(self, orig, l, FALSE);
1538 gboolean client_should_show(Client *self)
1540 if (self->iconic) return FALSE;
1541 else if (!(self->desktop == screen_desktop ||
1542 self->desktop == DESKTOP_ALL)) return FALSE;
1543 else if (client_normal(self) && screen_showing_desktop) return FALSE;
1548 static void client_showhide(Client *self)
1551 if (client_should_show(self))
1552 frame_show(self->frame);
1554 frame_hide(self->frame);
1557 gboolean client_normal(Client *self) {
1558 return ! (self->type == Type_Desktop || self->type == Type_Dock ||
1559 self->type == Type_Splash);
1562 static void client_apply_startup_state(Client *self)
1564 /* these are in a carefully crafted order.. */
1567 self->iconic = FALSE;
1568 client_iconify(self, TRUE, FALSE);
1570 if (self->fullscreen) {
1571 self->fullscreen = FALSE;
1572 client_fullscreen(self, TRUE, FALSE);
1575 self->shaded = FALSE;
1576 client_shade(self, TRUE);
1579 dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1581 if (self->max_vert && self->max_horz) {
1582 self->max_vert = self->max_horz = FALSE;
1583 client_maximize(self, TRUE, 0, FALSE);
1584 } else if (self->max_vert) {
1585 self->max_vert = FALSE;
1586 client_maximize(self, TRUE, 2, FALSE);
1587 } else if (self->max_horz) {
1588 self->max_horz = FALSE;
1589 client_maximize(self, TRUE, 1, FALSE);
1592 /* nothing to do for the other states:
1601 void client_configure(Client *self, Corner anchor, int x, int y, int w, int h,
1602 gboolean user, gboolean final)
1604 gboolean moved = FALSE, resized = FALSE;
1606 /* gets the frame's position */
1607 frame_client_gravity(self->frame, &x, &y);
1609 /* these positions are frame positions, not client positions */
1611 /* set the size and position if fullscreen */
1612 if (self->fullscreen) {
1615 w = screen_physical_size.width;
1616 h = screen_physical_size.height;
1617 user = FALSE; /* ignore that increment etc shit when in fullscreen */
1619 /* set the size and position if maximized */
1620 if (self->max_horz) {
1621 x = screen_area(self->desktop)->x - self->frame->size.left;
1622 w = screen_area(self->desktop)->width;
1624 if (self->max_vert) {
1625 y = screen_area(self->desktop)->y;
1626 h = screen_area(self->desktop)->height -
1627 self->frame->size.top - self->frame->size.bottom;
1631 /* gets the client's position */
1632 frame_frame_gravity(self->frame, &x, &y);
1634 /* these override the above states! if you cant move you can't move! */
1636 if (!(self->functions & Func_Move)) {
1640 if (!(self->functions & Func_Resize)) {
1641 w = self->area.width;
1642 h = self->area.height;
1646 if (!(w == self->area.width && h == self->area.height)) {
1647 w -= self->base_size.width;
1648 h -= self->base_size.height;
1651 /* for interactive resizing. have to move half an increment in each
1654 /* how far we are towards the next size inc */
1655 int mw = w % self->size_inc.width;
1656 int mh = h % self->size_inc.height;
1658 int aw = self->size_inc.width / 2;
1659 int ah = self->size_inc.height / 2;
1660 /* don't let us move into a new size increment */
1661 if (mw + aw >= self->size_inc.width)
1662 aw = self->size_inc.width - mw - 1;
1663 if (mh + ah >= self->size_inc.height)
1664 ah = self->size_inc.height - mh - 1;
1668 /* if this is a user-requested resize, then check against min/max
1669 sizes and aspect ratios */
1671 /* smaller than min size or bigger than max size? */
1672 if (w > self->max_size.width) w = self->max_size.width;
1673 if (w < self->min_size.width) w = self->min_size.width;
1674 if (h > self->max_size.height) h = self->max_size.height;
1675 if (h < self->min_size.height) h = self->min_size.height;
1677 /* adjust the height ot match the width for the aspect ratios */
1678 if (self->min_ratio)
1679 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1680 if (self->max_ratio)
1681 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1684 /* keep to the increments */
1685 w /= self->size_inc.width;
1686 h /= self->size_inc.height;
1688 /* you cannot resize to nothing */
1692 /* store the logical size */
1693 SIZE_SET(self->logical_size, w, h);
1695 w *= self->size_inc.width;
1696 h *= self->size_inc.height;
1698 w += self->base_size.width;
1699 h += self->base_size.height;
1703 case Corner_TopLeft:
1705 case Corner_TopRight:
1706 x -= w - self->area.width;
1708 case Corner_BottomLeft:
1709 y -= h - self->area.height;
1711 case Corner_BottomRight:
1712 x -= w - self->area.width;
1713 y -= h - self->area.height;
1717 moved = x != self->area.x || y != self->area.y;
1718 resized = w != self->area.width || h != self->area.height;
1720 RECT_SET(self->area, x, y, w, h);
1723 XResizeWindow(ob_display, self->window, w, h);
1725 /* move/resize the frame to match the request */
1727 if (moved || resized)
1728 frame_adjust_area(self->frame, moved, resized);
1730 if (!user || final) {
1732 event.type = ConfigureNotify;
1733 event.xconfigure.display = ob_display;
1734 event.xconfigure.event = self->window;
1735 event.xconfigure.window = self->window;
1737 /* root window real coords */
1738 event.xconfigure.x = self->frame->area.x + self->frame->size.left;
1739 event.xconfigure.y = self->frame->area.y + self->frame->size.top;
1741 event.xconfigure.width = w;
1742 event.xconfigure.height = h;
1743 event.xconfigure.border_width = 0;
1744 event.xconfigure.above = self->frame->plate;
1745 event.xconfigure.override_redirect = FALSE;
1746 XSendEvent(event.xconfigure.display, event.xconfigure.window,
1747 FALSE, StructureNotifyMask, &event);
1752 void client_fullscreen(Client *self, gboolean fs, gboolean savearea)
1756 if (!(self->functions & Func_Fullscreen) || /* can't */
1757 self->fullscreen == fs) return; /* already done */
1759 self->fullscreen = fs;
1760 client_change_state(self); /* change the state hints on the client,
1761 and adjust out layer/stacking */
1765 guint32 dimensions[4];
1766 dimensions[0] = self->area.x;
1767 dimensions[1] = self->area.y;
1768 dimensions[2] = self->area.width;
1769 dimensions[3] = self->area.height;
1771 PROP_SETA32(self->window, openbox_premax, cardinal,
1775 /* these are not actually used cuz client_configure will set them
1776 as appropriate when the window is fullscreened */
1782 /* pick some fallbacks... */
1783 x = screen_area(self->desktop)->x +
1784 screen_area(self->desktop)->width / 4;
1785 y = screen_area(self->desktop)->y +
1786 screen_area(self->desktop)->height / 4;
1787 w = screen_area(self->desktop)->width / 2;
1788 h = screen_area(self->desktop)->height / 2;
1790 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1791 (guint32**)&dimensions, &num)) {
1802 client_setup_decor_and_functions(self);
1804 client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1806 /* try focus us when we go into fullscreen mode */
1810 void client_iconify(Client *self, gboolean iconic, gboolean curdesk)
1814 /* move up the transient chain as far as possible first */
1815 if (self->transient_for) {
1816 if (self->transient_for != TRAN_GROUP) {
1817 if (self->transient_for->iconic != iconic) {
1818 client_iconify(self->transient_for, iconic, curdesk);
1824 for (it = self->group->members; it; it = it->next) {
1825 Client *c = it->data;
1826 if (c != self && c->iconic != iconic &&
1827 !c->transient_for) {
1828 client_iconify(it->data, iconic, curdesk);
1832 if (it != NULL) return;
1836 if (self->iconic == iconic) return; /* nothing to do */
1838 g_message("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
1841 self->iconic = iconic;
1844 self->wmstate = IconicState;
1845 self->ignore_unmaps++;
1846 /* we unmap the client itself so that we can get MapRequest events,
1847 and because the ICCCM tells us to! */
1848 XUnmapWindow(ob_display, self->window);
1850 /* update the focus lists.. iconic windows go to the bottom of the
1851 list, put the new iconic window at the 'top of the bottom'. */
1852 focus_order_to_top(self);
1855 client_set_desktop(self, screen_desktop, FALSE);
1856 self->wmstate = self->shaded ? IconicState : NormalState;
1857 XMapWindow(ob_display, self->window);
1859 /* this puts it after the current focused window */
1860 focus_order_remove(self);
1861 focus_order_add_new(self);
1863 client_change_state(self);
1864 client_showhide(self);
1865 screen_update_struts();
1867 dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
1870 /* iconify all transients */
1871 for (it = self->transients; it != NULL; it = it->next)
1872 if (it->data != self) client_iconify(it->data, iconic, curdesk);
1875 void client_maximize(Client *self, gboolean max, int dir, gboolean savearea)
1879 g_assert(dir == 0 || dir == 1 || dir == 2);
1880 if (!(self->functions & Func_Maximize)) return; /* can't */
1882 /* check if already done */
1884 if (dir == 0 && self->max_horz && self->max_vert) return;
1885 if (dir == 1 && self->max_horz) return;
1886 if (dir == 2 && self->max_vert) return;
1888 if (dir == 0 && !self->max_horz && !self->max_vert) return;
1889 if (dir == 1 && !self->max_horz) return;
1890 if (dir == 2 && !self->max_vert) return;
1893 /* work with the frame's coords */
1894 x = self->frame->area.x;
1895 y = self->frame->area.y;
1896 w = self->area.width;
1897 h = self->area.height;
1901 gint32 dimensions[4];
1910 /* get the property off the window and use it for the dimensions
1911 we are already maxed on */
1912 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1913 (guint32**)&readdim, &num)) {
1915 if (self->max_horz) {
1916 dimensions[0] = readdim[0];
1917 dimensions[2] = readdim[2];
1919 if (self->max_vert) {
1920 dimensions[1] = readdim[1];
1921 dimensions[3] = readdim[3];
1927 PROP_SETA32(self->window, openbox_premax, cardinal,
1928 (guint32*)dimensions, 4);
1934 /* pick some fallbacks... */
1935 if (dir == 0 || dir == 1) { /* horz */
1936 x = screen_area(self->desktop)->x +
1937 screen_area(self->desktop)->width / 4;
1938 w = screen_area(self->desktop)->width / 2;
1940 if (dir == 0 || dir == 2) { /* vert */
1941 y = screen_area(self->desktop)->y +
1942 screen_area(self->desktop)->height / 4;
1943 h = screen_area(self->desktop)->height / 2;
1946 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1947 (guint32**)&dimensions, &num)) {
1949 if (dir == 0 || dir == 1) { /* horz */
1953 if (dir == 0 || dir == 2) { /* vert */
1962 if (dir == 0 || dir == 1) /* horz */
1963 self->max_horz = max;
1964 if (dir == 0 || dir == 2) /* vert */
1965 self->max_vert = max;
1967 if (!self->max_horz && !self->max_vert)
1968 PROP_ERASE(self->window, openbox_premax);
1970 client_change_state(self); /* change the state hints on the client */
1972 /* figure out where the client should be going */
1973 frame_frame_gravity(self->frame, &x, &y);
1974 client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1977 void client_shade(Client *self, gboolean shade)
1979 if ((!(self->functions & Func_Shade) && shade) || /* can't shade */
1980 self->shaded == shade) return; /* already done */
1982 /* when we're iconic, don't change the wmstate */
1984 self->wmstate = shade ? IconicState : NormalState;
1985 self->shaded = shade;
1986 client_change_state(self);
1987 /* resize the frame to just the titlebar */
1988 frame_adjust_area(self->frame, FALSE, FALSE);
1991 void client_close(Client *self)
1995 if (!(self->functions & Func_Close)) return;
1998 XXX: itd be cool to do timeouts and shit here for killing the client's
2000 like... if the window is around after 5 seconds, then the close button
2001 turns a nice red, and if this function is called again, the client is
2005 ce.xclient.type = ClientMessage;
2006 ce.xclient.message_type = prop_atoms.wm_protocols;
2007 ce.xclient.display = ob_display;
2008 ce.xclient.window = self->window;
2009 ce.xclient.format = 32;
2010 ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
2011 ce.xclient.data.l[1] = event_lasttime;
2012 ce.xclient.data.l[2] = 0l;
2013 ce.xclient.data.l[3] = 0l;
2014 ce.xclient.data.l[4] = 0l;
2015 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2018 void client_kill(Client *self)
2020 XKillClient(ob_display, self->window);
2023 void client_set_desktop(Client *self, guint target, gboolean donthide)
2027 if (target == self->desktop) return;
2029 g_message("Setting desktop %u", target+1);
2031 g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2033 /* remove from the old desktop(s) */
2034 focus_order_remove(self);
2036 old = self->desktop;
2037 self->desktop = target;
2038 PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2039 /* the frame can display the current desktop state */
2040 frame_adjust_state(self->frame);
2041 /* 'move' the window to the new desktop */
2043 client_showhide(self);
2044 /* raise if it was not already on the desktop */
2045 if (old != DESKTOP_ALL)
2046 stacking_raise(CLIENT_AS_WINDOW(self));
2047 screen_update_struts();
2049 /* add to the new desktop(s) */
2050 if (config_focus_new)
2051 focus_order_to_top(self);
2053 focus_order_to_bottom(self);
2055 dispatch_client(Event_Client_Desktop, self, target, old);
2058 Client *client_search_modal_child(Client *self)
2063 for (it = self->transients; it != NULL; it = it->next) {
2064 Client *c = it->data;
2065 if ((ret = client_search_modal_child(c))) return ret;
2066 if (c->modal) return c;
2071 gboolean client_validate(Client *self)
2075 XSync(ob_display, FALSE); /* get all events on the server */
2077 if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2078 XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2079 XPutBackEvent(ob_display, &e);
2086 void client_set_wm_state(Client *self, long state)
2088 if (state == self->wmstate) return; /* no change */
2092 client_iconify(self, TRUE, TRUE);
2095 client_iconify(self, FALSE, TRUE);
2100 void client_set_state(Client *self, Atom action, long data1, long data2)
2102 gboolean shaded = self->shaded;
2103 gboolean fullscreen = self->fullscreen;
2104 gboolean max_horz = self->max_horz;
2105 gboolean max_vert = self->max_vert;
2108 if (!(action == prop_atoms.net_wm_state_add ||
2109 action == prop_atoms.net_wm_state_remove ||
2110 action == prop_atoms.net_wm_state_toggle))
2111 /* an invalid action was passed to the client message, ignore it */
2114 for (i = 0; i < 2; ++i) {
2115 Atom state = i == 0 ? data1 : data2;
2117 if (!state) continue;
2119 /* if toggling, then pick whether we're adding or removing */
2120 if (action == prop_atoms.net_wm_state_toggle) {
2121 if (state == prop_atoms.net_wm_state_modal)
2122 action = self->modal ? prop_atoms.net_wm_state_remove :
2123 prop_atoms.net_wm_state_add;
2124 else if (state == prop_atoms.net_wm_state_maximized_vert)
2125 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2126 prop_atoms.net_wm_state_add;
2127 else if (state == prop_atoms.net_wm_state_maximized_horz)
2128 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2129 prop_atoms.net_wm_state_add;
2130 else if (state == prop_atoms.net_wm_state_shaded)
2131 action = self->shaded ? prop_atoms.net_wm_state_remove :
2132 prop_atoms.net_wm_state_add;
2133 else if (state == prop_atoms.net_wm_state_skip_taskbar)
2134 action = self->skip_taskbar ?
2135 prop_atoms.net_wm_state_remove :
2136 prop_atoms.net_wm_state_add;
2137 else if (state == prop_atoms.net_wm_state_skip_pager)
2138 action = self->skip_pager ?
2139 prop_atoms.net_wm_state_remove :
2140 prop_atoms.net_wm_state_add;
2141 else if (state == prop_atoms.net_wm_state_fullscreen)
2142 action = self->fullscreen ?
2143 prop_atoms.net_wm_state_remove :
2144 prop_atoms.net_wm_state_add;
2145 else if (state == prop_atoms.net_wm_state_above)
2146 action = self->above ? prop_atoms.net_wm_state_remove :
2147 prop_atoms.net_wm_state_add;
2148 else if (state == prop_atoms.net_wm_state_below)
2149 action = self->below ? prop_atoms.net_wm_state_remove :
2150 prop_atoms.net_wm_state_add;
2153 if (action == prop_atoms.net_wm_state_add) {
2154 if (state == prop_atoms.net_wm_state_modal) {
2155 /* XXX raise here or something? */
2157 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2159 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2161 } else if (state == prop_atoms.net_wm_state_shaded) {
2163 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2164 self->skip_taskbar = TRUE;
2165 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2166 self->skip_pager = TRUE;
2167 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2169 } else if (state == prop_atoms.net_wm_state_above) {
2171 } else if (state == prop_atoms.net_wm_state_below) {
2175 } else { /* action == prop_atoms.net_wm_state_remove */
2176 if (state == prop_atoms.net_wm_state_modal) {
2177 self->modal = FALSE;
2178 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2180 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2182 } else if (state == prop_atoms.net_wm_state_shaded) {
2184 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2185 self->skip_taskbar = FALSE;
2186 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2187 self->skip_pager = FALSE;
2188 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2190 } else if (state == prop_atoms.net_wm_state_above) {
2191 self->above = FALSE;
2192 } else if (state == prop_atoms.net_wm_state_below) {
2193 self->below = FALSE;
2197 if (max_horz != self->max_horz || max_vert != self->max_vert) {
2198 if (max_horz != self->max_horz && max_vert != self->max_vert) {
2200 if (max_horz == max_vert) { /* both going the same way */
2201 client_maximize(self, max_horz, 0, TRUE);
2203 client_maximize(self, max_horz, 1, TRUE);
2204 client_maximize(self, max_vert, 2, TRUE);
2208 if (max_horz != self->max_horz)
2209 client_maximize(self, max_horz, 1, TRUE);
2211 client_maximize(self, max_vert, 2, TRUE);
2214 /* change fullscreen state before shading, as it will affect if the window
2216 if (fullscreen != self->fullscreen)
2217 client_fullscreen(self, fullscreen, TRUE);
2218 if (shaded != self->shaded)
2219 client_shade(self, shaded);
2220 client_calc_layer(self);
2221 client_change_state(self); /* change the hint to reflect these changes */
2224 Client *client_focus_target(Client *self)
2228 /* if we have a modal child, then focus it, not us */
2229 child = client_search_modal_child(self);
2230 if (child) return child;
2234 gboolean client_focus(Client *self)
2238 /* choose the correct target */
2239 self = client_focus_target(self);
2241 if (!self->frame->visible) {
2242 /* update the focus lists */
2243 focus_order_to_top(self);
2247 if (!((self->can_focus || self->focus_notify) &&
2248 (self->desktop == screen_desktop ||
2249 self->desktop == DESKTOP_ALL) &&
2253 /* do a check to see if the window has already been unmapped or destroyed
2254 do this intelligently while watching out for unmaps we've generated
2255 (ignore_unmaps > 0) */
2256 if (XCheckTypedWindowEvent(ob_display, self->window,
2257 DestroyNotify, &ev)) {
2258 XPutBackEvent(ob_display, &ev);
2261 while (XCheckTypedWindowEvent(ob_display, self->window,
2262 UnmapNotify, &ev)) {
2263 if (self->ignore_unmaps) {
2264 self->ignore_unmaps--;
2266 XPutBackEvent(ob_display, &ev);
2271 if (self->can_focus)
2272 /* RevertToPointerRoot causes much more headache than RevertToNone, so
2273 I choose to use it always, hopefully to find errors quicker, if any
2274 are left. (I hate X. I hate focus events.) */
2275 XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2278 if (self->focus_notify) {
2280 ce.xclient.type = ClientMessage;
2281 ce.xclient.message_type = prop_atoms.wm_protocols;
2282 ce.xclient.display = ob_display;
2283 ce.xclient.window = self->window;
2284 ce.xclient.format = 32;
2285 ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2286 ce.xclient.data.l[1] = event_lasttime;
2287 ce.xclient.data.l[2] = 0l;
2288 ce.xclient.data.l[3] = 0l;
2289 ce.xclient.data.l[4] = 0l;
2290 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2294 g_message("%sively focusing %lx at %d", (self->can_focus ? "act" : "pass"),
2299 /* Cause the FocusIn to come back to us. Important for desktop switches,
2300 since otherwise we'll have no FocusIn on the queue and send it off to
2301 the focus_backup. */
2302 XSync(ob_display, FALSE);
2306 void client_unfocus(Client *self)
2308 g_assert(focus_client == self);
2310 g_message("client_unfocus for %lx", self->window);
2312 focus_fallback(Fallback_Unfocusing);
2315 void client_activate(Client *self)
2317 if (client_normal(self) && screen_showing_desktop)
2318 screen_show_desktop(FALSE);
2320 client_iconify(self, FALSE, TRUE);
2321 else if (!self->frame->visible)
2322 /* if its not visible for other reasons, then don't mess
2326 client_shade(self, FALSE);
2328 stacking_raise(CLIENT_AS_WINDOW(self));
2331 gboolean client_focused(Client *self)
2333 return self == focus_client;
2336 Icon *client_icon(Client *self, int w, int h)
2339 /* si is the smallest image >= req */
2340 /* li is the largest image < req */
2341 unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2343 if (!self->nicons) return NULL;
2345 for (i = 0; i < self->nicons; ++i) {
2346 size = self->icons[i].width * self->icons[i].height;
2347 if (size < smallest && size >= (unsigned)(w * h)) {
2351 if (size > largest && size <= (unsigned)(w * h)) {
2356 if (largest == 0) /* didnt find one smaller than the requested size */
2357 return &self->icons[si];
2358 return &self->icons[li];
2361 /* this be mostly ripped from fvwm */
2362 Client *client_find_directional(Client *c, Direction dir)
2364 int my_cx, my_cy, his_cx, his_cy;
2367 int score, best_score;
2368 Client *best_client, *cur;
2374 /* first, find the centre coords of the currently focused window */
2375 my_cx = c->frame->area.x + c->frame->area.width / 2;
2376 my_cy = c->frame->area.y + c->frame->area.height / 2;
2381 for(it = g_list_first(client_list); it; it = it->next) {
2384 /* the currently selected window isn't interesting */
2387 if (!client_normal(cur))
2389 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2393 if(client_focus_target(cur) == cur &&
2394 !(cur->can_focus || cur->focus_notify))
2397 /* find the centre coords of this window, from the
2398 * currently focused window's point of view */
2399 his_cx = (cur->frame->area.x - my_cx)
2400 + cur->frame->area.width / 2;
2401 his_cy = (cur->frame->area.y - my_cy)
2402 + cur->frame->area.height / 2;
2406 /* Rotate the diagonals 45 degrees counterclockwise.
2407 * To do this, multiply the matrix /+h +h\ with the
2408 * vector (x y). \-h +h/
2409 * h = sqrt(0.5). We can set h := 1 since absolute
2410 * distance doesn't matter here. */
2411 tx = his_cx + his_cy;
2412 his_cy = -his_cx + his_cy;
2417 case Direction_North :
2418 case Direction_South :
2419 case Direction_NorthEast :
2420 case Direction_SouthWest :
2421 offset = (his_cx < 0) ? -his_cx : his_cx;
2422 distance = (dir == Direction_North || dir == Direction_NorthEast) ?
2425 case Direction_East :
2426 case Direction_West :
2427 case Direction_SouthEast :
2428 case Direction_NorthWest :
2429 offset = (his_cy < 0) ? -his_cy : his_cy;
2430 distance = (dir == Direction_West || dir == Direction_NorthWest) ?
2435 /* the target must be in the requested direction */
2439 /* Calculate score for this window. The smaller the better. */
2440 score = distance + offset;
2442 /* windows more than 45 degrees off the direction are
2443 * heavily penalized and will only be chosen if nothing
2444 * else within a million pixels */
2445 if(offset > distance)
2448 if(best_score == -1 || score < best_score)
2456 void client_set_layer(Client *self, int layer)
2460 self->above = FALSE;
2461 } else if (layer == 0) {
2462 self->below = self->above = FALSE;
2464 self->below = FALSE;
2467 client_calc_layer(self);
2468 client_change_state(self); /* reflect this in the state hints */