4 #include "extensions.h"
16 #include <X11/Xutil.h>
18 /*! The event mask to grab on client windows */
19 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
22 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
25 GList *client_list = NULL;
26 GHashTable *client_map = NULL;
28 static Window *client_startup_stack_order = NULL;
29 static gulong client_startup_stack_size = 0;
31 static void client_get_all(Client *self);
32 static void client_toggle_border(Client *self, gboolean show);
33 static void client_get_area(Client *self);
34 static void client_get_desktop(Client *self);
35 static void client_get_state(Client *self);
36 static void client_get_shaped(Client *self);
37 static void client_get_mwm_hints(Client *self);
38 static void client_get_gravity(Client *self);
39 static void client_showhide(Client *self);
40 static void client_change_allowed_actions(Client *self);
41 static void client_change_state(Client *self);
42 static void client_move_onscreen(Client *self);
43 static Client *search_focus_tree(Client *node, Client *skip);
44 static void client_apply_startup_state(Client *self);
45 static Client *search_modal_tree(Client *node, Client *skip);
47 static guint map_hash(Window *w) { return *w; }
48 static gboolean map_key_comp(Window *w1, Window *w2) { return *w1 == *w2; }
52 client_map = g_hash_table_new((GHashFunc)map_hash,
53 (GEqualFunc)map_key_comp);
55 /* save the stacking order on startup! */
56 PROP_GET32U(ob_root, net_client_list_stacking, window,
57 client_startup_stack_order, client_startup_stack_size);
62 void client_shutdown()
64 g_hash_table_destroy(client_map);
67 void client_set_list()
69 Window *windows, *win_it;
71 guint size = g_list_length(client_list);
73 /* create an array of the window ids */
75 windows = g_new(Window, size);
77 for (it = client_list; it != NULL; it = it->next, ++win_it)
78 *win_it = ((Client*)it->data)->window;
82 PROP_SET32A(ob_root, net_client_list, window, windows, size);
90 void client_manage_all()
92 unsigned int i, j, nchild;
95 XWindowAttributes attrib;
97 XQueryTree(ob_display, ob_root, &w, &w, &children, &nchild);
99 /* remove all icon windows from the list */
100 for (i = 0; i < nchild; i++) {
101 if (children[i] == None) continue;
102 wmhints = XGetWMHints(ob_display, children[i]);
104 if ((wmhints->flags & IconWindowHint) &&
105 (wmhints->icon_window != children[i]))
106 for (j = 0; j < nchild; j++)
107 if (children[j] == wmhints->icon_window) {
115 for (i = 0; i < nchild; ++i) {
116 if (children[i] == None)
118 if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
119 if (attrib.override_redirect) continue;
121 if (attrib.map_state != IsUnmapped)
122 client_manage(children[i]);
127 /* stack them as they were on startup!
128 why with stacking_lower? Why, because then windows who aren't in the
129 stacking list are on the top where you can see them instead of buried
131 for (i = client_startup_stack_size; i > 0; --i) {
134 w = client_startup_stack_order[i-1];
135 c = g_hash_table_lookup(client_map, &w);
136 if (c) stacking_lower(c);
138 g_free(client_startup_stack_order);
139 client_startup_stack_order = NULL;
140 client_startup_stack_size = 0;
142 if (config_focus_new)
143 focus_fallback(Fallback_NoFocus);
146 void client_manage(Window window)
150 XWindowAttributes attrib;
151 XSetWindowAttributes attrib_set;
152 /* XWMHints *wmhint; */
157 /* check if it has already been unmapped by the time we started mapping
158 the grab does a sync so we don't have to here */
159 if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
160 XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
161 XPutBackEvent(ob_display, &e);
164 return; /* don't manage it */
167 /* make sure it isn't an override-redirect window */
168 if (!XGetWindowAttributes(ob_display, window, &attrib) ||
169 attrib.override_redirect) {
171 return; /* don't manage it */
174 /* /\* is the window a docking app *\/
175 if ((wmhint = XGetWMHints(ob_display, window))) {
176 if ((wmhint->flags & StateHint) &&
177 wmhint->initial_state == WithdrawnState) {
178 /\* XXX: make dock apps work! *\/
186 g_message("Managing window: %lx", window);
188 /* choose the events we want to receive on the CLIENT window */
189 attrib_set.event_mask = CLIENT_EVENTMASK;
190 attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
191 XChangeWindowAttributes(ob_display, window,
192 CWEventMask|CWDontPropagate, &attrib_set);
195 /* create the Client struct, and populate it from the hints on the
197 self = g_new(Client, 1);
198 self->window = window;
199 client_get_all(self);
201 /* remove the client's border (and adjust re gravity) */
202 client_toggle_border(self, FALSE);
204 /* specify that if we exit, the window should not be destroyed and should
205 be reparented back to root automatically */
206 XChangeSaveSet(ob_display, window, SetModeInsert);
208 /* create the decoration frame for the client window */
209 self->frame = engine_frame_new();
211 engine_frame_grab_client(self->frame, self);
213 client_apply_startup_state(self);
217 client_list = g_list_append(client_list, self);
218 stacking_list = g_list_append(stacking_list, self);
219 g_assert(!g_hash_table_lookup(client_map, &self->window));
220 g_hash_table_insert(client_map, &self->window, self);
222 /* update the focus lists */
223 if (self->desktop == DESKTOP_ALL) {
224 for (i = 0; i < screen_num_desktops; ++i)
225 focus_order[i] = g_list_append(focus_order[i], self);
228 focus_order[i] = g_list_append(focus_order[i], self);
231 stacking_raise(self);
233 screen_update_struts();
235 dispatch_client(Event_Client_New, self, 0, 0);
237 client_showhide(self);
239 dispatch_client(Event_Client_Mapped, self, 0, 0);
241 if (ob_state != State_Starting && config_focus_new)
244 /* update the list hints */
247 /* make sure the window is visible */
248 client_move_onscreen(self);
250 g_message("Managed window 0x%lx", window);
253 void client_unmanage_all()
255 while (client_list != NULL)
256 client_unmanage(client_list->data);
259 void client_unmanage(Client *self)
265 g_message("Unmanaging window: %lx", self->window);
267 dispatch_client(Event_Client_Destroy, self, 0, 0);
268 g_assert(self != NULL);
270 /* remove the window from our save set */
271 XChangeSaveSet(ob_display, self->window, SetModeDelete);
273 /* we dont want events no more */
274 XSelectInput(ob_display, self->window, NoEventMask);
276 engine_frame_hide(self->frame);
278 client_list = g_list_remove(client_list, self);
279 stacking_list = g_list_remove(stacking_list, self);
280 g_hash_table_remove(client_map, &self->window);
282 /* update the focus lists */
283 if (self->desktop == DESKTOP_ALL) {
284 for (i = 0; i < screen_num_desktops; ++i)
285 focus_order[i] = g_list_remove(focus_order[i], self);
288 focus_order[i] = g_list_remove(focus_order[i], self);
291 /* once the client is out of the list, update the struts to remove it's
293 screen_update_struts();
295 /* tell our parent(s) that we're gone */
296 if (self->transient_for == TRAN_GROUP) { /* transient of group */
299 for (it = self->group->members; it; it = it->next)
300 if (it->data != self)
301 ((Client*)it->data)->transients =
302 g_slist_remove(((Client*)it->data)->transients, self);
303 } else if (self->transient_for) { /* transient of window */
304 self->transient_for->transients =
305 g_slist_remove(self->transient_for->transients, self);
308 /* tell our transients that we're gone */
309 for (it = self->transients; it != NULL; it = it->next) {
310 if (((Client*)it->data)->transient_for != TRAN_GROUP) {
311 ((Client*)it->data)->transient_for = NULL;
312 client_calc_layer(it->data);
316 if (focus_client == self)
317 client_unfocus(self);
319 /* remove from its group */
321 group_remove(self->group, self);
325 /* dispatch the unmapped event */
326 dispatch_client(Event_Client_Unmapped, self, 0, 0);
327 g_assert(self != NULL);
329 /* give the client its border back */
330 client_toggle_border(self, TRUE);
332 /* reparent the window out of the frame, and free the frame */
333 engine_frame_release_client(self->frame, self);
336 if (ob_state != State_Exiting) {
337 /* these values should not be persisted across a window
339 prop_erase(self->window, prop_atoms.net_wm_desktop);
340 prop_erase(self->window, prop_atoms.net_wm_state);
342 /* if we're left in an iconic state, the client wont be mapped. this is
343 bad, since we will no longer be managing the window on restart */
345 XMapWindow(ob_display, self->window);
348 g_message("Unmanaged window 0x%lx", self->window);
350 /* free all data allocated in the client struct */
351 g_slist_free(self->transients);
352 for (j = 0; j < self->nicons; ++j)
353 g_free(self->icons[j].data);
354 if (self->nicons > 0)
357 g_free(self->icon_title);
363 /* update the list hints */
367 static void client_move_onscreen(Client *self)
370 int x = self->frame->area.x, y = self->frame->area.y;
372 a = screen_area(self->desktop);
373 if (x >= a->x + a->width - 1)
374 x = a->x + a->width - self->frame->area.width;
375 if (y >= a->y + a->height - 1)
376 y = a->y + a->height - self->frame->area.height;
377 if (x + self->frame->area.width - 1 < a->x)
379 if (y + self->frame->area.height - 1< a->y)
382 frame_frame_gravity(self->frame, &x, &y); /* get where the client
384 client_configure(self , Corner_TopLeft, x, y,
385 self->area.width, self->area.height,
389 static void client_toggle_border(Client *self, gboolean show)
391 /* adjust our idea of where the client is, based on its border. When the
392 border is removed, the client should now be considered to be in a
394 when re-adding the border to the client, the same operation needs to be
396 int oldx = self->area.x, oldy = self->area.y;
397 int x = oldx, y = oldy;
398 switch(self->gravity) {
400 case NorthWestGravity:
402 case SouthWestGravity:
404 case NorthEastGravity:
406 case SouthEastGravity:
407 if (show) x -= self->border_width * 2;
408 else x += self->border_width * 2;
415 if (show) x -= self->border_width;
416 else x += self->border_width;
419 switch(self->gravity) {
421 case NorthWestGravity:
423 case NorthEastGravity:
425 case SouthWestGravity:
427 case SouthEastGravity:
428 if (show) y -= self->border_width * 2;
429 else y += self->border_width * 2;
436 if (show) y -= self->border_width;
437 else y += self->border_width;
444 XSetWindowBorderWidth(ob_display, self->window, self->border_width);
446 /* move the client so it is back it the right spot _with_ its
448 if (x != oldx || y != oldy)
449 XMoveWindow(ob_display, self->window, x, y);
451 XSetWindowBorderWidth(ob_display, self->window, 0);
455 static void client_get_all(Client *self)
457 /* update EVERYTHING!! */
459 self->ignore_unmaps = 0;
463 self->title = self->icon_title = NULL;
464 self->name = self->class = self->role = NULL;
465 self->wmstate = NormalState;
466 self->transient = FALSE;
467 self->transients = NULL;
468 self->transient_for = NULL;
470 self->urgent = FALSE;
471 self->positioned = FALSE;
472 self->disabled_decorations = 0;
476 client_get_area(self);
477 client_get_desktop(self);
478 client_get_state(self);
479 client_get_shaped(self);
481 client_update_transient_for(self);
482 client_get_mwm_hints(self);
483 client_get_type(self);/* this can change the mwmhints for special cases */
485 client_update_protocols(self);
487 client_get_gravity(self); /* get the attribute gravity */
488 client_update_normal_hints(self); /* this may override the attribute
491 /* got the type, the mwmhints, the protocols, and the normal hints
492 (min/max sizes), so we're ready to set up the decorations/functions */
493 client_setup_decor_and_functions(self);
495 client_update_wmhints(self);
496 client_update_title(self);
497 client_update_icon_title(self);
498 client_update_class(self);
499 client_update_strut(self);
500 client_update_icons(self);
501 client_update_kwm_icon(self);
503 /* this makes sure that these windows appear on all desktops */
504 if (self->type == Type_Desktop)
505 self->desktop = DESKTOP_ALL;
507 /* set the desktop hint, to make sure that it always exists, and to
508 reflect any changes we've made here */
509 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
511 client_change_state(self);
514 static void client_get_area(Client *self)
516 XWindowAttributes wattrib;
519 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
520 g_assert(ret != BadWindow);
522 RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
523 self->border_width = wattrib.border_width;
526 static void client_get_desktop(Client *self)
530 if (PROP_GET32(self->window, net_wm_desktop, cardinal, d)) {
531 if (d >= screen_num_desktops && d != DESKTOP_ALL)
532 d = screen_num_desktops - 1;
535 /* defaults to the current desktop */
536 self->desktop = screen_desktop;
540 static void client_get_state(Client *self)
545 self->modal = self->shaded = self->max_horz = self->max_vert =
546 self->fullscreen = self->above = self->below = self->iconic =
547 self->skip_taskbar = self->skip_pager = FALSE;
549 if (PROP_GET32U(self->window, net_wm_state, atom, state, num)) {
551 for (i = 0; i < num; ++i) {
552 if (state[i] == prop_atoms.net_wm_state_modal)
554 else if (state[i] == prop_atoms.net_wm_state_shaded)
556 else if (state[i] == prop_atoms.net_wm_state_hidden)
558 else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
559 self->skip_taskbar = TRUE;
560 else if (state[i] == prop_atoms.net_wm_state_skip_pager)
561 self->skip_pager = TRUE;
562 else if (state[i] == prop_atoms.net_wm_state_fullscreen)
563 self->fullscreen = TRUE;
564 else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
565 self->max_vert = TRUE;
566 else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
567 self->max_horz = TRUE;
568 else if (state[i] == prop_atoms.net_wm_state_above)
570 else if (state[i] == prop_atoms.net_wm_state_below)
578 static void client_get_shaped(Client *self)
580 self->shaped = FALSE;
582 if (extensions_shape) {
587 XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
589 XShapeQueryExtents(ob_display, self->window, &s, &foo,
590 &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
592 self->shaped = (s != 0);
597 void client_update_transient_for(Client *self)
602 if (XGetTransientForHint(ob_display, self->window, &t) &&
603 t != self->window) { /* cant be transient to itself! */
604 self->transient = TRUE;
605 c = g_hash_table_lookup(client_map, &t);
606 g_assert(c != self);/* if this happens then we need to check for it*/
608 if (!c && self->group) {
609 /* not transient to a client, see if it is transient for a
611 if (t == self->group->leader ||
614 /* window is a transient for its group! */
619 self->transient = FALSE;
621 /* if anything has changed... */
622 if (c != self->transient_for) {
623 if (self->transient_for == TRAN_GROUP) { /* transient of group */
626 /* remove from old parents */
627 for (it = self->group->members; it; it = it->next)
628 if (it->data != self)
629 ((Client*)it->data)->transients =
630 g_slist_remove(((Client*)it->data)->transients, self);
631 } else if (self->transient_for != NULL) { /* transient of window */
632 /* remove from old parent */
633 self->transient_for->transients =
634 g_slist_remove(self->transient_for->transients, self);
636 self->transient_for = c;
637 if (self->transient_for == TRAN_GROUP) { /* transient of group */
640 /* add to new parents */
641 for (it = self->group->members; it; it = it->next)
642 if (it->data != self)
643 ((Client*)it->data)->transients =
644 g_slist_append(((Client*)it->data)->transients, self);
645 } else if (self->transient_for != NULL) { /* transient of window */
646 /* add to new parent */
647 self->transient_for->transients =
648 g_slist_append(self->transient_for->transients, self);
653 static void client_get_mwm_hints(Client *self)
656 unsigned long *hints;
658 self->mwmhints.flags = 0; /* default to none */
660 if (PROP_GET32U(self->window, motif_wm_hints, motif_wm_hints, hints, num)) {
661 if (num >= MWM_ELEMENTS) {
662 self->mwmhints.flags = hints[0];
663 self->mwmhints.functions = hints[1];
664 self->mwmhints.decorations = hints[2];
670 void client_get_type(Client *self)
676 if (PROP_GET32U(self->window, net_wm_window_type, atom, val, num)) {
677 /* use the first value that we know about in the array */
678 for (i = 0; i < num; ++i) {
679 if (val[i] == prop_atoms.net_wm_window_type_desktop)
680 self->type = Type_Desktop;
681 else if (val[i] == prop_atoms.net_wm_window_type_dock)
682 self->type = Type_Dock;
683 else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
684 self->type = Type_Toolbar;
685 else if (val[i] == prop_atoms.net_wm_window_type_menu)
686 self->type = Type_Menu;
687 else if (val[i] == prop_atoms.net_wm_window_type_utility)
688 self->type = Type_Utility;
689 else if (val[i] == prop_atoms.net_wm_window_type_splash)
690 self->type = Type_Splash;
691 else if (val[i] == prop_atoms.net_wm_window_type_dialog)
692 self->type = Type_Dialog;
693 else if (val[i] == prop_atoms.net_wm_window_type_normal)
694 self->type = Type_Normal;
695 else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
696 /* prevent this window from getting any decor or
698 self->mwmhints.flags &= (MwmFlag_Functions |
699 MwmFlag_Decorations);
700 self->mwmhints.decorations = 0;
701 self->mwmhints.functions = 0;
703 if (self->type != (WindowType) -1)
704 break; /* grab the first legit type */
709 if (self->type == (WindowType) -1) {
710 /*the window type hint was not set, which means we either classify
711 ourself as a normal window or a dialog, depending on if we are a
714 self->type = Type_Dialog;
716 self->type = Type_Normal;
720 void client_update_protocols(Client *self)
723 gulong num_return, i;
725 self->focus_notify = FALSE;
726 self->delete_window = FALSE;
728 if (PROP_GET32U(self->window, wm_protocols, atom, proto, num_return)) {
729 for (i = 0; i < num_return; ++i) {
730 if (proto[i] == prop_atoms.wm_delete_window) {
731 /* this means we can request the window to close */
732 self->delete_window = TRUE;
733 } else if (proto[i] == prop_atoms.wm_take_focus)
734 /* if this protocol is requested, then the window will be
735 notified whenever we want it to receive focus */
736 self->focus_notify = TRUE;
742 static void client_get_gravity(Client *self)
744 XWindowAttributes wattrib;
747 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
748 g_assert(ret != BadWindow);
749 self->gravity = wattrib.win_gravity;
752 void client_update_normal_hints(Client *self)
756 int oldgravity = self->gravity;
759 self->min_ratio = 0.0f;
760 self->max_ratio = 0.0f;
761 SIZE_SET(self->size_inc, 1, 1);
762 SIZE_SET(self->base_size, 0, 0);
763 SIZE_SET(self->min_size, 0, 0);
764 SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
766 /* get the hints from the window */
767 if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
768 self->positioned = !!(size.flags & (PPosition|USPosition));
770 if (size.flags & PWinGravity) {
771 self->gravity = size.win_gravity;
773 /* if the client has a frame, i.e. has already been mapped and
774 is changing its gravity */
775 if (self->frame && self->gravity != oldgravity) {
776 /* move our idea of the client's position based on its new
778 self->area.x = self->frame->area.x;
779 self->area.y = self->frame->area.y;
780 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
784 if (size.flags & PAspect) {
785 if (size.min_aspect.y)
786 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
787 if (size.max_aspect.y)
788 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
791 if (size.flags & PMinSize)
792 SIZE_SET(self->min_size, size.min_width, size.min_height);
794 if (size.flags & PMaxSize)
795 SIZE_SET(self->max_size, size.max_width, size.max_height);
797 if (size.flags & PBaseSize)
798 SIZE_SET(self->base_size, size.base_width, size.base_height);
800 if (size.flags & PResizeInc)
801 SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
805 void client_setup_decor_and_functions(Client *self)
807 /* start with everything (cept fullscreen) */
808 self->decorations = Decor_Titlebar | Decor_Handle | Decor_Border |
809 Decor_Icon | Decor_AllDesktops | Decor_Iconify | Decor_Maximize |
811 self->functions = Func_Resize | Func_Move | Func_Iconify | Func_Maximize |
813 if (self->delete_window) {
814 self->decorations |= Decor_Close;
815 self->functions |= Func_Close;
818 if (!(self->min_size.width < self->max_size.width ||
819 self->min_size.height < self->max_size.height)) {
820 self->decorations &= ~(Decor_Maximize | Decor_Handle);
821 self->functions &= ~(Func_Resize | Func_Maximize);
824 switch (self->type) {
826 /* normal windows retain all of the possible decorations and
827 functionality, and are the only windows that you can fullscreen */
828 self->functions |= Func_Fullscreen;
832 /* dialogs cannot be maximized */
833 self->decorations &= ~Decor_Maximize;
834 self->functions &= ~Func_Maximize;
840 /* these windows get less functionality */
841 self->decorations &= ~(Decor_Iconify | Decor_Handle);
842 self->functions &= ~(Func_Iconify | Func_Resize);
848 /* none of these windows are manipulated by the window manager */
849 self->decorations = 0;
854 /* Mwm Hints are applied subtractively to what has already been chosen for
855 decor and functionality */
856 if (self->mwmhints.flags & MwmFlag_Decorations) {
857 if (! (self->mwmhints.decorations & MwmDecor_All)) {
858 if (! (self->mwmhints.decorations & MwmDecor_Border))
859 self->decorations &= ~Decor_Border;
860 if (! (self->mwmhints.decorations & MwmDecor_Handle))
861 self->decorations &= ~Decor_Handle;
862 if (! (self->mwmhints.decorations & MwmDecor_Title))
863 self->decorations &= ~Decor_Titlebar;
864 if (! (self->mwmhints.decorations & MwmDecor_Iconify))
865 self->decorations &= ~Decor_Iconify;
866 if (! (self->mwmhints.decorations & MwmDecor_Maximize))
867 self->decorations &= ~Decor_Maximize;
871 if (self->mwmhints.flags & MwmFlag_Functions) {
872 if (! (self->mwmhints.functions & MwmFunc_All)) {
873 if (! (self->mwmhints.functions & MwmFunc_Resize))
874 self->functions &= ~Func_Resize;
875 if (! (self->mwmhints.functions & MwmFunc_Move))
876 self->functions &= ~Func_Move;
877 if (! (self->mwmhints.functions & MwmFunc_Iconify))
878 self->functions &= ~Func_Iconify;
879 if (! (self->mwmhints.functions & MwmFunc_Maximize))
880 self->functions &= ~Func_Maximize;
881 /* dont let mwm hints kill the close button
882 if (! (self->mwmhints.functions & MwmFunc_Close))
883 self->functions &= ~Func_Close; */
887 /* can't maximize without moving/resizing */
888 if (!((self->functions & Func_Move) && (self->functions & Func_Resize)))
889 self->functions &= ~(Func_Maximize | Func_Fullscreen);
891 /* finally, user specified disabled decorations are applied to subtract
893 if (self->disabled_decorations & Decor_Titlebar)
894 self->decorations &= ~Decor_Titlebar;
895 if (self->disabled_decorations & Decor_Handle)
896 self->decorations &= ~Decor_Handle;
897 if (self->disabled_decorations & Decor_Border)
898 self->decorations &= ~Decor_Border;
899 if (self->disabled_decorations & Decor_Iconify)
900 self->decorations &= ~Decor_Iconify;
901 if (self->disabled_decorations & Decor_Maximize)
902 self->decorations &= ~Decor_Maximize;
903 if (self->disabled_decorations & Decor_AllDesktops)
904 self->decorations &= ~Decor_AllDesktops;
905 if (self->disabled_decorations & Decor_Shade)
906 self->decorations &= ~Decor_Shade;
907 if (self->disabled_decorations & Decor_Close)
908 self->decorations &= ~Decor_Close;
910 /* if we don't have a titlebar, then we cannot shade! */
911 if (!(self->decorations & Decor_Titlebar))
912 self->functions &= ~Func_Shade;
914 client_change_allowed_actions(self);
917 /* change the decors on the frame, and with more/less decorations,
918 we may also need to be repositioned */
919 engine_frame_adjust_area(self->frame, TRUE, TRUE);
920 /* with new decor, the window's maximized size may change */
921 client_remaximize(self);
925 static void client_change_allowed_actions(Client *self)
930 actions[num++] = prop_atoms.net_wm_action_change_desktop;
932 if (self->functions & Func_Shade)
933 actions[num++] = prop_atoms.net_wm_action_shade;
934 if (self->functions & Func_Close)
935 actions[num++] = prop_atoms.net_wm_action_close;
936 if (self->functions & Func_Move)
937 actions[num++] = prop_atoms.net_wm_action_move;
938 if (self->functions & Func_Iconify)
939 actions[num++] = prop_atoms.net_wm_action_minimize;
940 if (self->functions & Func_Resize)
941 actions[num++] = prop_atoms.net_wm_action_resize;
942 if (self->functions & Func_Fullscreen)
943 actions[num++] = prop_atoms.net_wm_action_fullscreen;
944 if (self->functions & Func_Maximize) {
945 actions[num++] = prop_atoms.net_wm_action_maximize_horz;
946 actions[num++] = prop_atoms.net_wm_action_maximize_vert;
949 PROP_SET32A(self->window, net_wm_allowed_actions, atom, actions, num);
951 /* make sure the window isn't breaking any rules now */
953 if (!(self->functions & Func_Shade) && self->shaded) {
954 if (self->frame) client_shade(self, FALSE);
955 else self->shaded = FALSE;
957 if (!(self->functions & Func_Iconify) && self->iconic) {
958 g_message("UNSETTING ICONIC");
959 if (self->frame) client_iconify(self, FALSE, TRUE);
960 else self->iconic = FALSE;
962 if (!(self->functions & Func_Fullscreen) && self->fullscreen) {
963 if (self->frame) client_fullscreen(self, FALSE, TRUE);
964 else self->fullscreen = FALSE;
966 if (!(self->functions & Func_Maximize) && (self->max_horz ||
968 if (self->frame) client_maximize(self, FALSE, 0, TRUE);
969 else self->max_vert = self->max_horz = FALSE;
973 void client_remaximize(Client *self)
976 if (self->max_horz && self->max_vert)
978 else if (self->max_horz)
980 else if (self->max_vert)
983 return; /* not maximized */
984 self->max_horz = self->max_vert = FALSE;
985 client_maximize(self, TRUE, dir, FALSE);
988 void client_update_wmhints(Client *self)
993 /* assume a window takes input if it doesnt specify */
994 self->can_focus = TRUE;
996 if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
997 if (hints->flags & InputHint)
998 self->can_focus = hints->input;
1000 /* only do this when first managing the window *AND* when we aren't
1002 if (ob_state != State_Starting && self->frame == NULL)
1003 if (hints->flags & StateHint)
1004 self->iconic = hints->initial_state == IconicState;
1006 if (hints->flags & XUrgencyHint)
1009 if (!(hints->flags & WindowGroupHint))
1010 hints->window_group = None; /* no group */
1011 /* did the group state change? */
1012 if (hints->window_group != (self->group ? self->group->leader : None)){
1013 /* remove from the old group if there was one */
1014 if (self->group != NULL)
1015 group_remove(self->group, self);
1016 if (hints->window_group != None)
1017 self->group = group_add(hints->window_group, self);
1019 /* because the self->transient flag wont change from this call,
1020 we don't need to update the window's type and such, only its
1021 transient_for, and the transients lists of other windows in the
1022 group may be affected */
1023 client_update_transient_for(self);
1026 if (hints->flags & IconPixmapHint) {
1027 client_update_kwm_icon(self);
1028 /* try get the kwm icon first, this is a fallback only */
1029 if (self->pixmap_icon == None) {
1030 self->pixmap_icon = hints->icon_pixmap;
1031 if (hints->flags & IconMaskHint)
1032 self->pixmap_icon_mask = hints->icon_mask;
1034 self->pixmap_icon_mask = None;
1037 engine_frame_adjust_icon(self->frame);
1044 if (ur != self->urgent) {
1046 g_message("Urgent Hint for 0x%lx: %s", self->window,
1048 /* fire the urgent callback if we're mapped, otherwise, wait until
1049 after we're mapped */
1051 dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1055 void client_update_title(Client *self)
1059 g_free(self->title);
1062 if (!PROP_GETS(self->window, net_wm_name, utf8, data)) {
1063 /* try old x stuff */
1064 if (PROP_GETS(self->window, wm_name, string, data)) {
1065 /* convert it to UTF-8 */
1069 u = g_locale_to_utf8(data, -1, &r, &w, NULL);
1071 g_warning("Unable to convert string to UTF-8");
1078 data = g_strdup("Unnamed Window");
1080 PROP_SETS(self->window, net_wm_visible_name, utf8, data);
1086 engine_frame_adjust_title(self->frame);
1089 void client_update_icon_title(Client *self)
1093 g_free(self->icon_title);
1096 if (!PROP_GETS(self->window, net_wm_icon_name, utf8, data)) {
1097 /* try old x stuff */
1098 if (PROP_GETS(self->window, wm_icon_name, string, data)) {
1099 /* convert it to UTF-8 */
1103 u = g_locale_to_utf8(data, -1, &r, &w, NULL);
1105 g_warning("Unable to convert string to UTF-8");
1112 data = g_strdup("Unnamed Window");
1114 PROP_SETS(self->window, net_wm_visible_icon_name, utf8, data);
1117 self->icon_title = data;
1120 void client_update_class(Client *self)
1126 if (self->name) g_free(self->name);
1127 if (self->class) g_free(self->class);
1128 if (self->role) g_free(self->role);
1130 self->name = self->class = self->role = NULL;
1132 data = g_ptr_array_new();
1134 if (PROP_GETSA(self->window, wm_class, string, data)) {
1136 self->name = g_strdup(g_ptr_array_index(data, 0));
1138 self->class = g_strdup(g_ptr_array_index(data, 1));
1141 for (i = 0; i < data->len; ++i)
1142 g_free(g_ptr_array_index(data, i));
1143 g_ptr_array_free(data, TRUE);
1145 if (PROP_GETS(self->window, wm_window_role, string, s))
1146 self->role = g_strdup(s);
1148 if (self->name == NULL) self->name = g_strdup("");
1149 if (self->class == NULL) self->class = g_strdup("");
1150 if (self->role == NULL) self->role = g_strdup("");
1153 void client_update_strut(Client *self)
1157 if (PROP_GET32A(self->window, net_wm_strut, cardinal, data, 4)) {
1158 STRUT_SET(self->strut, data[0], data[2], data[1], data[3]);
1161 STRUT_SET(self->strut, 0, 0, 0, 0);
1163 /* updating here is pointless while we're being mapped cuz we're not in
1164 the client list yet */
1166 screen_update_struts();
1169 void client_update_icons(Client *self)
1172 unsigned long *data;
1173 unsigned long w, h, i;
1176 for (j = 0; j < self->nicons; ++j)
1177 g_free(self->icons[j].data);
1178 if (self->nicons > 0)
1179 g_free(self->icons);
1182 if (PROP_GET32U(self->window, net_wm_icon, cardinal, data, num)) {
1183 /* figure out how many valid icons are in here */
1185 while (num - i > 2) {
1193 self->icons = g_new(Icon, self->nicons);
1195 /* store the icons */
1197 for (j = 0; j < self->nicons; ++j) {
1198 w = self->icons[j].width = data[i++];
1199 h = self->icons[j].height = data[i++];
1200 self->icons[j].data =
1201 g_memdup(&data[i], w * h * sizeof(gulong));
1210 engine_frame_adjust_icon(self->frame);
1213 void client_update_kwm_icon(Client *self)
1217 if (PROP_GET32A(self->window, kwm_win_icon, kwm_win_icon, data, 2)) {
1218 self->pixmap_icon = data[0];
1219 self->pixmap_icon_mask = data[1];
1222 self->pixmap_icon = self->pixmap_icon_mask = None;
1225 engine_frame_adjust_icon(self->frame);
1228 static void client_change_state(Client *self)
1230 unsigned long state[2];
1234 state[0] = self->wmstate;
1236 PROP_SET32A(self->window, wm_state, wm_state, state, 2);
1240 netstate[num++] = prop_atoms.net_wm_state_modal;
1242 netstate[num++] = prop_atoms.net_wm_state_shaded;
1244 netstate[num++] = prop_atoms.net_wm_state_hidden;
1245 if (self->skip_taskbar)
1246 netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1247 if (self->skip_pager)
1248 netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1249 if (self->fullscreen)
1250 netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1252 netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1254 netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1256 netstate[num++] = prop_atoms.net_wm_state_above;
1258 netstate[num++] = prop_atoms.net_wm_state_below;
1259 PROP_SET32A(self->window, net_wm_state, atom, netstate, num);
1261 client_calc_layer(self);
1264 engine_frame_adjust_state(self->frame);
1267 static Client *search_focus_tree(Client *node, Client *skip)
1272 for (it = node->transients; it != NULL; it = it->next) {
1273 Client *c = it->data;
1274 if (c == skip) continue; /* circular? */
1275 if ((ret = search_focus_tree(c, skip))) return ret;
1276 if (client_focused(c)) return c;
1281 void client_calc_layer(Client *self)
1287 /* are we fullscreen, or do we have a fullscreen transient parent? */
1290 while (c && c != TRAN_GROUP) { /* XXX do smthng with the TRAN_GROUP case?*/
1291 if (c->fullscreen) {
1295 c = c->transient_for;
1297 if (!fs && self->fullscreen) {
1298 /* is one of our transients focused? */
1299 c = search_focus_tree(self, self);
1300 if (c != NULL) fs = TRUE;
1303 if (self->iconic) l = Layer_Icon;
1304 else if (fs) l = Layer_Fullscreen;
1305 else if (self->type == Type_Desktop) l = Layer_Desktop;
1306 else if (self->type == Type_Dock) {
1307 if (!self->below) l = Layer_Top;
1308 else l = Layer_Normal;
1310 else if (self->above) l = Layer_Above;
1311 else if (self->below) l = Layer_Below;
1312 else l = Layer_Normal;
1314 if (l != self->layer) {
1317 stacking_raise(self);
1321 gboolean client_should_show(Client *self)
1323 if (self->iconic) return FALSE;
1324 else if (!(self->desktop == screen_desktop ||
1325 self->desktop == DESKTOP_ALL)) return FALSE;
1326 else if (client_normal(self) && screen_showing_desktop) return FALSE;
1331 static void client_showhide(Client *self)
1334 if (client_should_show(self))
1335 engine_frame_show(self->frame);
1337 engine_frame_hide(self->frame);
1340 gboolean client_normal(Client *self) {
1341 return ! (self->type == Type_Desktop || self->type == Type_Dock ||
1342 self->type == Type_Splash);
1345 static void client_apply_startup_state(Client *self)
1347 /* these are in a carefully crafted order.. */
1350 self->iconic = FALSE;
1351 client_iconify(self, TRUE, FALSE);
1353 if (self->fullscreen) {
1354 self->fullscreen = FALSE;
1355 client_fullscreen(self, TRUE, FALSE);
1358 self->shaded = FALSE;
1359 client_shade(self, TRUE);
1362 dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1364 if (self->max_vert && self->max_horz) {
1365 self->max_vert = self->max_horz = FALSE;
1366 client_maximize(self, TRUE, 0, FALSE);
1367 } else if (self->max_vert) {
1368 self->max_vert = FALSE;
1369 client_maximize(self, TRUE, 2, FALSE);
1370 } else if (self->max_horz) {
1371 self->max_horz = FALSE;
1372 client_maximize(self, TRUE, 1, FALSE);
1375 /* nothing to do for the other states:
1384 void client_configure(Client *self, Corner anchor, int x, int y, int w, int h,
1385 gboolean user, gboolean final)
1387 gboolean moved = FALSE, resized = FALSE;
1389 /* gets the frame's position */
1390 frame_client_gravity(self->frame, &x, &y);
1392 /* these positions are frame positions, not client positions */
1394 /* set the size and position if fullscreen */
1395 if (self->fullscreen) {
1398 w = screen_physical_size.width;
1399 h = screen_physical_size.height;
1401 /* set the size and position if maximized */
1402 if (self->max_horz) {
1403 x = screen_area(self->desktop)->x - self->frame->size.left;
1404 w = screen_area(self->desktop)->width;
1406 if (self->max_vert) {
1407 y = screen_area(self->desktop)->y;
1408 h = screen_area(self->desktop)->height -
1409 self->frame->size.top - self->frame->size.bottom;
1413 /* gets the client's position */
1414 frame_frame_gravity(self->frame, &x, &y);
1416 /* these override the above states! if you cant move you can't move! */
1418 if (!(self->functions & Func_Move)) {
1422 if (!(self->functions & Func_Resize)) {
1423 w = self->area.width;
1424 h = self->area.height;
1428 if (!(w == self->area.width && h == self->area.height)) {
1429 w -= self->base_size.width;
1430 h -= self->base_size.height;
1433 /* for interactive resizing. have to move half an increment in each
1436 /* how far we are towards the next size inc */
1437 int mw = w % self->size_inc.width;
1438 int mh = h % self->size_inc.height;
1440 int aw = self->size_inc.width / 2;
1441 int ah = self->size_inc.height / 2;
1442 /* don't let us move into a new size increment */
1443 if (mw + aw >= self->size_inc.width)
1444 aw = self->size_inc.width - mw - 1;
1445 if (mh + ah >= self->size_inc.height)
1446 ah = self->size_inc.height - mh - 1;
1450 /* if this is a user-requested resize, then check against min/max
1451 sizes and aspect ratios */
1453 /* smaller than min size or bigger than max size? */
1454 if (w > self->max_size.width) w = self->max_size.width;
1455 if (w < self->min_size.width) w = self->min_size.width;
1456 if (h > self->max_size.height) h = self->max_size.height;
1457 if (h < self->min_size.height) h = self->min_size.height;
1459 /* adjust the height ot match the width for the aspect ratios */
1460 if (self->min_ratio)
1461 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1462 if (self->max_ratio)
1463 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1466 /* keep to the increments */
1467 w /= self->size_inc.width;
1468 h /= self->size_inc.height;
1470 /* you cannot resize to nothing */
1474 /* store the logical size */
1475 SIZE_SET(self->logical_size, w, h);
1477 w *= self->size_inc.width;
1478 h *= self->size_inc.height;
1480 w += self->base_size.width;
1481 h += self->base_size.height;
1485 case Corner_TopLeft:
1487 case Corner_TopRight:
1488 x -= w - self->area.width;
1490 case Corner_BottomLeft:
1491 y -= h - self->area.height;
1493 case Corner_BottomRight:
1494 x -= w - self->area.width;
1495 y -= h - self->area.height;
1499 moved = x != self->area.x || y != self->area.y;
1500 resized = w != self->area.width || h != self->area.height;
1502 RECT_SET(self->area, x, y, w, h);
1505 XResizeWindow(ob_display, self->window, w, h);
1507 /* move/resize the frame to match the request */
1509 if (moved || resized)
1510 engine_frame_adjust_area(self->frame, moved, resized);
1512 if (!user || final) {
1514 event.type = ConfigureNotify;
1515 event.xconfigure.display = ob_display;
1516 event.xconfigure.event = self->window;
1517 event.xconfigure.window = self->window;
1519 /* root window coords with border in mind */
1520 event.xconfigure.x = x - self->border_width +
1521 self->frame->size.left;
1522 event.xconfigure.y = y - self->border_width +
1523 self->frame->size.top;
1525 event.xconfigure.width = self->area.width;
1526 event.xconfigure.height = self->area.height;
1527 event.xconfigure.border_width = self->border_width;
1528 event.xconfigure.above = self->frame->plate;
1529 event.xconfigure.override_redirect = FALSE;
1530 XSendEvent(event.xconfigure.display, event.xconfigure.window,
1531 FALSE, StructureNotifyMask, &event);
1536 void client_fullscreen(Client *self, gboolean fs, gboolean savearea)
1540 if (!(self->functions & Func_Fullscreen) || /* can't */
1541 self->fullscreen == fs) return; /* already done */
1543 self->fullscreen = fs;
1544 client_change_state(self); /* change the state hints on the client */
1547 /* save the functions and remove them */
1548 self->pre_fs_func = self->functions;
1549 self->functions &= (Func_Close | Func_Fullscreen |
1551 /* save the decorations and remove them */
1552 self->pre_fs_decor = self->decorations;
1553 self->decorations = 0;
1556 dimensions[0] = self->area.x;
1557 dimensions[1] = self->area.y;
1558 dimensions[2] = self->area.width;
1559 dimensions[3] = self->area.height;
1561 PROP_SET32A(self->window, openbox_premax, cardinal,
1565 /* these are not actually used cuz client_configure will set them
1566 as appropriate when the window is fullscreened */
1571 self->functions = self->pre_fs_func;
1572 self->decorations = self->pre_fs_decor;
1574 if (PROP_GET32A(self->window, openbox_premax, cardinal,
1582 /* pick some fallbacks... */
1583 x = screen_area(self->desktop)->x +
1584 screen_area(self->desktop)->width / 4;
1585 y = screen_area(self->desktop)->y +
1586 screen_area(self->desktop)->height / 4;
1587 w = screen_area(self->desktop)->width / 2;
1588 h = screen_area(self->desktop)->height / 2;
1592 client_change_allowed_actions(self); /* based on the new _functions */
1594 /* when fullscreening, don't obey things like increments, fill the
1596 client_configure(self, Corner_TopLeft, x, y, w, h, !fs, TRUE);
1598 /* raise (back) into our stacking layer */
1599 stacking_raise(self);
1601 /* try focus us when we go into fullscreen mode */
1605 void client_iconify(Client *self, gboolean iconic, gboolean curdesk)
1607 if (self->iconic == iconic) return; /* nothing to do */
1609 g_message("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
1612 self->iconic = iconic;
1615 self->wmstate = IconicState;
1616 self->ignore_unmaps++;
1617 /* we unmap the client itself so that we can get MapRequest events,
1618 and because the ICCCM tells us to! */
1619 XUnmapWindow(ob_display, self->window);
1622 client_set_desktop(self, screen_desktop, FALSE);
1623 self->wmstate = self->shaded ? IconicState : NormalState;
1624 XMapWindow(ob_display, self->window);
1626 client_change_state(self);
1627 client_showhide(self);
1628 screen_update_struts();
1630 dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
1633 /* iconify all transients */
1634 if (self->transients) {
1637 for (it = self->transients; it != NULL; it = it->next)
1638 if (it->data != self) client_iconify(it->data, iconic, curdesk);
1642 void client_maximize(Client *self, gboolean max, int dir, gboolean savearea)
1646 g_assert(dir == 0 || dir == 1 || dir == 2);
1647 if (!(self->functions & Func_Maximize)) return; /* can't */
1649 /* check if already done */
1651 if (dir == 0 && self->max_horz && self->max_vert) return;
1652 if (dir == 1 && self->max_horz) return;
1653 if (dir == 2 && self->max_vert) return;
1655 if (dir == 0 && !self->max_horz && !self->max_vert) return;
1656 if (dir == 1 && !self->max_horz) return;
1657 if (dir == 2 && !self->max_vert) return;
1660 /* work with the frame's coords */
1661 x = self->frame->area.x;
1662 y = self->frame->area.y;
1663 w = self->area.width;
1664 h = self->area.height;
1676 /* get the property off the window and use it for the dimensions
1677 we are already maxed on */
1678 if (PROP_GET32A(self->window, openbox_premax, cardinal,
1680 if (self->max_horz) {
1681 dimensions[0] = readdim[0];
1682 dimensions[2] = readdim[2];
1684 if (self->max_vert) {
1685 dimensions[1] = readdim[1];
1686 dimensions[3] = readdim[3];
1691 PROP_SET32A(self->window, openbox_premax, cardinal,
1697 if (PROP_GET32A(self->window, openbox_premax, cardinal,
1699 if (dir == 0 || dir == 1) { /* horz */
1703 if (dir == 0 || dir == 2) { /* vert */
1709 /* pick some fallbacks... */
1710 if (dir == 0 || dir == 1) { /* horz */
1711 x = screen_area(self->desktop)->x +
1712 screen_area(self->desktop)->width / 4;
1713 w = screen_area(self->desktop)->width / 2;
1715 if (dir == 0 || dir == 2) { /* vert */
1716 y = screen_area(self->desktop)->y +
1717 screen_area(self->desktop)->height / 4;
1718 h = screen_area(self->desktop)->height / 2;
1723 if (dir == 0 || dir == 1) /* horz */
1724 self->max_horz = max;
1725 if (dir == 0 || dir == 2) /* vert */
1726 self->max_vert = max;
1728 if (!self->max_horz && !self->max_vert)
1729 PROP_ERASE(self->window, openbox_premax);
1731 client_change_state(self); /* change the state hints on the client */
1733 /* figure out where the client should be going */
1734 frame_frame_gravity(self->frame, &x, &y);
1735 client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1738 void client_shade(Client *self, gboolean shade)
1740 if ((!(self->functions & Func_Shade) && shade) || /* can't shade */
1741 self->shaded == shade) return; /* already done */
1743 /* when we're iconic, don't change the wmstate */
1745 self->wmstate = shade ? IconicState : NormalState;
1746 self->shaded = shade;
1747 client_change_state(self);
1748 /* resize the frame to just the titlebar */
1749 engine_frame_adjust_area(self->frame, FALSE, FALSE);
1752 void client_close(Client *self)
1756 if (!(self->functions & Func_Close)) return;
1759 XXX: itd be cool to do timeouts and shit here for killing the client's
1761 like... if the window is around after 5 seconds, then the close button
1762 turns a nice red, and if this function is called again, the client is
1766 ce.xclient.type = ClientMessage;
1767 ce.xclient.message_type = prop_atoms.wm_protocols;
1768 ce.xclient.display = ob_display;
1769 ce.xclient.window = self->window;
1770 ce.xclient.format = 32;
1771 ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
1772 ce.xclient.data.l[1] = event_lasttime;
1773 ce.xclient.data.l[2] = 0l;
1774 ce.xclient.data.l[3] = 0l;
1775 ce.xclient.data.l[4] = 0l;
1776 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
1779 void client_kill(Client *self)
1781 XKillClient(ob_display, self->window);
1784 void client_set_desktop(Client *self, guint target, gboolean donthide)
1788 if (target == self->desktop) return;
1790 g_message("Setting desktop %u", target);
1792 g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
1794 old = self->desktop;
1795 self->desktop = target;
1796 PROP_SET32(self->window, net_wm_desktop, cardinal, target);
1797 /* the frame can display the current desktop state */
1798 engine_frame_adjust_state(self->frame);
1799 /* 'move' the window to the new desktop */
1801 client_showhide(self);
1802 /* raise if it was not already on the desktop */
1803 if (old != DESKTOP_ALL)
1804 stacking_raise(self);
1805 screen_update_struts();
1807 /* update the focus lists */
1808 if (old == DESKTOP_ALL) {
1809 for (i = 0; i < screen_num_desktops; ++i)
1810 focus_order[i] = g_list_remove(focus_order[i], self);
1812 focus_order[old] = g_list_remove(focus_order[old], self);
1813 if (target == DESKTOP_ALL) {
1814 for (i = 0; i < screen_num_desktops; ++i) {
1815 if (config_focus_new)
1816 focus_order[i] = g_list_prepend(focus_order[i], self);
1818 focus_order[i] = g_list_append(focus_order[i], self);
1821 if (config_focus_new)
1822 focus_order[target] = g_list_prepend(focus_order[target], self);
1824 focus_order[target] = g_list_append(focus_order[target], self);
1827 dispatch_client(Event_Client_Desktop, self, target, old);
1830 static Client *search_modal_tree(Client *node, Client *skip)
1835 for (it = node->transients; it != NULL; it = it->next) {
1836 Client *c = it->data;
1837 if (c == skip) continue; /* circular? */
1838 if ((ret = search_modal_tree(c, skip))) return ret;
1839 if (c->modal) return c;
1844 Client *client_find_modal_child(Client *self)
1846 return search_modal_tree(self, self);
1849 gboolean client_validate(Client *self)
1853 XSync(ob_display, FALSE); /* get all events on the server */
1855 if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
1856 XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
1857 XPutBackEvent(ob_display, &e);
1864 void client_set_wm_state(Client *self, long state)
1866 if (state == self->wmstate) return; /* no change */
1870 client_iconify(self, TRUE, TRUE);
1873 client_iconify(self, FALSE, TRUE);
1878 void client_set_state(Client *self, Atom action, long data1, long data2)
1880 gboolean shaded = self->shaded;
1881 gboolean fullscreen = self->fullscreen;
1882 gboolean max_horz = self->max_horz;
1883 gboolean max_vert = self->max_vert;
1886 if (!(action == prop_atoms.net_wm_state_add ||
1887 action == prop_atoms.net_wm_state_remove ||
1888 action == prop_atoms.net_wm_state_toggle))
1889 /* an invalid action was passed to the client message, ignore it */
1892 for (i = 0; i < 2; ++i) {
1893 Atom state = i == 0 ? data1 : data2;
1895 if (!state) continue;
1897 /* if toggling, then pick whether we're adding or removing */
1898 if (action == prop_atoms.net_wm_state_toggle) {
1899 if (state == prop_atoms.net_wm_state_modal)
1900 action = self->modal ? prop_atoms.net_wm_state_remove :
1901 prop_atoms.net_wm_state_add;
1902 else if (state == prop_atoms.net_wm_state_maximized_vert)
1903 action = self->max_vert ? prop_atoms.net_wm_state_remove :
1904 prop_atoms.net_wm_state_add;
1905 else if (state == prop_atoms.net_wm_state_maximized_horz)
1906 action = self->max_horz ? prop_atoms.net_wm_state_remove :
1907 prop_atoms.net_wm_state_add;
1908 else if (state == prop_atoms.net_wm_state_shaded)
1909 action = self->shaded ? prop_atoms.net_wm_state_remove :
1910 prop_atoms.net_wm_state_add;
1911 else if (state == prop_atoms.net_wm_state_skip_taskbar)
1912 action = self->skip_taskbar ?
1913 prop_atoms.net_wm_state_remove :
1914 prop_atoms.net_wm_state_add;
1915 else if (state == prop_atoms.net_wm_state_skip_pager)
1916 action = self->skip_pager ?
1917 prop_atoms.net_wm_state_remove :
1918 prop_atoms.net_wm_state_add;
1919 else if (state == prop_atoms.net_wm_state_fullscreen)
1920 action = self->fullscreen ?
1921 prop_atoms.net_wm_state_remove :
1922 prop_atoms.net_wm_state_add;
1923 else if (state == prop_atoms.net_wm_state_above)
1924 action = self->above ? prop_atoms.net_wm_state_remove :
1925 prop_atoms.net_wm_state_add;
1926 else if (state == prop_atoms.net_wm_state_below)
1927 action = self->below ? prop_atoms.net_wm_state_remove :
1928 prop_atoms.net_wm_state_add;
1931 if (action == prop_atoms.net_wm_state_add) {
1932 if (state == prop_atoms.net_wm_state_modal) {
1933 /* XXX raise here or something? */
1935 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
1937 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
1939 } else if (state == prop_atoms.net_wm_state_shaded) {
1941 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
1942 self->skip_taskbar = TRUE;
1943 } else if (state == prop_atoms.net_wm_state_skip_pager) {
1944 self->skip_pager = TRUE;
1945 } else if (state == prop_atoms.net_wm_state_fullscreen) {
1947 } else if (state == prop_atoms.net_wm_state_above) {
1949 } else if (state == prop_atoms.net_wm_state_below) {
1953 } else { /* action == prop_atoms.net_wm_state_remove */
1954 if (state == prop_atoms.net_wm_state_modal) {
1955 self->modal = FALSE;
1956 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
1958 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
1960 } else if (state == prop_atoms.net_wm_state_shaded) {
1962 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
1963 self->skip_taskbar = FALSE;
1964 } else if (state == prop_atoms.net_wm_state_skip_pager) {
1965 self->skip_pager = FALSE;
1966 } else if (state == prop_atoms.net_wm_state_fullscreen) {
1968 } else if (state == prop_atoms.net_wm_state_above) {
1969 self->above = FALSE;
1970 } else if (state == prop_atoms.net_wm_state_below) {
1971 self->below = FALSE;
1975 if (max_horz != self->max_horz || max_vert != self->max_vert) {
1976 if (max_horz != self->max_horz && max_vert != self->max_vert) {
1978 if (max_horz == max_vert) { /* both going the same way */
1979 client_maximize(self, max_horz, 0, TRUE);
1981 client_maximize(self, max_horz, 1, TRUE);
1982 client_maximize(self, max_vert, 2, TRUE);
1986 if (max_horz != self->max_horz)
1987 client_maximize(self, max_horz, 1, TRUE);
1989 client_maximize(self, max_vert, 2, TRUE);
1992 /* change fullscreen state before shading, as it will affect if the window
1994 if (fullscreen != self->fullscreen)
1995 client_fullscreen(self, fullscreen, TRUE);
1996 if (shaded != self->shaded)
1997 client_shade(self, shaded);
1998 client_calc_layer(self);
1999 client_change_state(self); /* change the hint to relect these changes */
2002 Client *client_focus_target(Client *self)
2006 /* if we have a modal child, then focus it, not us */
2007 child = client_find_modal_child(self);
2008 if (child) return child;
2012 gboolean client_focusable(Client *self)
2014 /* won't try focus if the client doesn't want it, or if the window isn't
2015 visible on the screen */
2016 return self->frame->visible &&
2017 (self->can_focus || self->focus_notify);
2020 gboolean client_focus(Client *self)
2024 /* choose the correct target */
2025 self = client_focus_target(self);
2027 if (!client_focusable(self))
2030 /* do a check to see if the window has already been unmapped or destroyed
2031 do this intelligently while watching out for unmaps we've generated
2032 (ignore_unmaps > 0) */
2033 if (XCheckTypedWindowEvent(ob_display, self->window,
2034 DestroyNotify, &ev)) {
2035 XPutBackEvent(ob_display, &ev);
2038 while (XCheckTypedWindowEvent(ob_display, self->window,
2039 UnmapNotify, &ev)) {
2040 if (self->ignore_unmaps) {
2041 self->ignore_unmaps--;
2043 XPutBackEvent(ob_display, &ev);
2048 if (self->can_focus)
2049 /* RevertToPointerRoot causes much more headache than TevertToNone, so
2050 I choose to use it always, hopefully to find errors quicker, if any
2051 are left. (I hate X. I hate focus events.) */
2052 XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2055 if (self->focus_notify) {
2057 ce.xclient.type = ClientMessage;
2058 ce.xclient.message_type = prop_atoms.wm_protocols;
2059 ce.xclient.display = ob_display;
2060 ce.xclient.window = self->window;
2061 ce.xclient.format = 32;
2062 ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2063 ce.xclient.data.l[1] = event_lasttime;
2064 ce.xclient.data.l[2] = 0l;
2065 ce.xclient.data.l[3] = 0l;
2066 ce.xclient.data.l[4] = 0l;
2067 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2071 g_message("focusing %lx", self->window);
2074 /* Cause the FocusIn to come back to us. Important for desktop switches,
2075 since otherwise we'll have no FocusIn on the queue and send it off to
2076 the focus_backup. */
2077 XSync(ob_display, FALSE);
2081 void client_unfocus(Client *self)
2083 g_assert(focus_client == self);
2084 g_message("client_unfocus");
2085 focus_fallback(Fallback_Unfocusing);
2088 gboolean client_focused(Client *self)
2090 return self == focus_client;
2093 Icon *client_icon(Client *self, int w, int h)
2096 /* si is the smallest image >= req */
2097 /* li is the largest image < req */
2098 unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2100 if (!self->nicons) return NULL;
2102 for (i = 0; i < self->nicons; ++i) {
2103 size = self->icons[i].width * self->icons[i].height;
2104 if (size < smallest && size >= (unsigned)(w * h)) {
2108 if (size > largest && size <= (unsigned)(w * h)) {
2113 if (largest == 0) /* didnt find one smaller than the requested size */
2114 return &self->icons[si];
2115 return &self->icons[li];