3 #include "moveresize.h"
5 #include "extensions.h"
17 #include <X11/Xutil.h>
19 /*! The event mask to grab on client windows */
20 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
23 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
26 GList *client_list = NULL;
27 GHashTable *client_map = NULL;
29 static Window *client_startup_stack_order = NULL;
30 static guint client_startup_stack_size = 0;
32 static void client_get_all(Client *self);
33 static void client_toggle_border(Client *self, gboolean show);
34 static void client_get_area(Client *self);
35 static void client_get_desktop(Client *self);
36 static void client_get_state(Client *self);
37 static void client_get_shaped(Client *self);
38 static void client_get_mwm_hints(Client *self);
39 static void client_get_gravity(Client *self);
40 static void client_showhide(Client *self);
41 static void client_change_allowed_actions(Client *self);
42 static void client_change_state(Client *self);
43 static void client_move_onscreen(Client *self);
44 static Client *search_focus_tree(Client *node, Client *skip);
45 static void client_apply_startup_state(Client *self);
46 static Client *search_modal_tree(Client *node, Client *skip);
48 static guint map_hash(Window *w) { return *w; }
49 static gboolean map_key_comp(Window *w1, Window *w2) { return *w1 == *w2; }
53 client_map = g_hash_table_new((GHashFunc)map_hash,
54 (GEqualFunc)map_key_comp);
56 /* save the stacking order on startup! */
57 PROP_GETA32(ob_root, net_client_list_stacking, window,
58 (guint32**)&client_startup_stack_order,
59 &client_startup_stack_size);
64 void client_shutdown()
66 g_hash_table_destroy(client_map);
69 void client_set_list()
71 Window *windows, *win_it;
73 guint size = g_list_length(client_list);
75 /* create an array of the window ids */
77 windows = g_new(Window, size);
79 for (it = client_list; it != NULL; it = it->next, ++win_it)
80 *win_it = ((Client*)it->data)->window;
84 PROP_SETA32(ob_root, net_client_list, window, (guint32*)windows, size);
92 void client_manage_all()
94 unsigned int i, j, nchild;
97 XWindowAttributes attrib;
99 XQueryTree(ob_display, ob_root, &w, &w, &children, &nchild);
101 /* remove all icon windows from the list */
102 for (i = 0; i < nchild; i++) {
103 if (children[i] == None) continue;
104 wmhints = XGetWMHints(ob_display, children[i]);
106 if ((wmhints->flags & IconWindowHint) &&
107 (wmhints->icon_window != children[i]))
108 for (j = 0; j < nchild; j++)
109 if (children[j] == wmhints->icon_window) {
117 for (i = 0; i < nchild; ++i) {
118 if (children[i] == None)
120 if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
121 if (attrib.override_redirect) continue;
123 if (attrib.map_state != IsUnmapped)
124 client_manage(children[i]);
129 /* stack them as they were on startup!
130 why with stacking_lower? Why, because then windows who aren't in the
131 stacking list are on the top where you can see them instead of buried
133 for (i = client_startup_stack_size; i > 0; --i) {
136 w = client_startup_stack_order[i-1];
137 c = g_hash_table_lookup(client_map, &w);
138 if (c) stacking_lower(c);
140 g_free(client_startup_stack_order);
141 client_startup_stack_order = NULL;
142 client_startup_stack_size = 0;
144 if (config_focus_new)
145 focus_fallback(Fallback_NoFocus);
148 void client_manage(Window window)
152 XWindowAttributes attrib;
153 XSetWindowAttributes attrib_set;
154 /* XWMHints *wmhint; */
159 /* check if it has already been unmapped by the time we started mapping
160 the grab does a sync so we don't have to here */
161 if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
162 XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
163 XPutBackEvent(ob_display, &e);
166 return; /* don't manage it */
169 /* make sure it isn't an override-redirect window */
170 if (!XGetWindowAttributes(ob_display, window, &attrib) ||
171 attrib.override_redirect) {
173 return; /* don't manage it */
176 /* /\* is the window a docking app *\/
177 if ((wmhint = XGetWMHints(ob_display, window))) {
178 if ((wmhint->flags & StateHint) &&
179 wmhint->initial_state == WithdrawnState) {
180 /\* XXX: make dock apps work! *\/
188 g_message("Managing window: %lx", window);
190 /* choose the events we want to receive on the CLIENT window */
191 attrib_set.event_mask = CLIENT_EVENTMASK;
192 attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
193 XChangeWindowAttributes(ob_display, window,
194 CWEventMask|CWDontPropagate, &attrib_set);
197 /* create the Client struct, and populate it from the hints on the
199 self = g_new(Client, 1);
200 self->window = window;
201 client_get_all(self);
203 /* remove the client's border (and adjust re gravity) */
204 client_toggle_border(self, FALSE);
206 /* specify that if we exit, the window should not be destroyed and should
207 be reparented back to root automatically */
208 XChangeSaveSet(ob_display, window, SetModeInsert);
210 /* create the decoration frame for the client window */
211 self->frame = frame_new();
213 frame_grab_client(self->frame, self);
215 client_apply_startup_state(self);
219 client_list = g_list_append(client_list, self);
220 stacking_list = g_list_append(stacking_list, self);
221 g_assert(!g_hash_table_lookup(client_map, &self->window));
222 g_hash_table_insert(client_map, &self->window, self);
224 /* update the focus lists */
225 if (self->desktop == DESKTOP_ALL) {
226 for (i = 0; i < screen_num_desktops; ++i)
227 focus_order[i] = g_list_append(focus_order[i], self);
230 focus_order[i] = g_list_append(focus_order[i], self);
233 stacking_raise(self);
235 screen_update_struts();
237 dispatch_client(Event_Client_New, self, 0, 0);
239 client_showhide(self);
241 /* focus the new window? */
242 if (ob_state != State_Starting && client_normal(self)) {
243 if (config_focus_new)
245 else if (self->transient_for) {
246 if (self->transient_for != TRAN_GROUP) {/* transient of a window */
247 if (focus_client == self->transient_for)
249 } else { /* transient of a group */
252 for (it = self->group->members; it; it = it->next)
253 if (focus_client == it->data) {
261 /* update the list hints */
264 /* make sure the window is visible */
265 client_move_onscreen(self);
267 dispatch_client(Event_Client_Mapped, self, 0, 0);
269 g_message("Managed window 0x%lx", window);
272 void client_unmanage_all()
274 while (client_list != NULL)
275 client_unmanage(client_list->data);
278 void client_unmanage(Client *self)
284 g_message("Unmanaging window: %lx", self->window);
286 dispatch_client(Event_Client_Destroy, self, 0, 0);
287 g_assert(self != NULL);
289 /* remove the window from our save set */
290 XChangeSaveSet(ob_display, self->window, SetModeDelete);
292 /* we dont want events no more */
293 XSelectInput(ob_display, self->window, NoEventMask);
295 frame_hide(self->frame);
297 client_list = g_list_remove(client_list, self);
298 stacking_list = g_list_remove(stacking_list, self);
299 g_hash_table_remove(client_map, &self->window);
301 /* update the focus lists */
302 if (self->desktop == DESKTOP_ALL) {
303 for (i = 0; i < screen_num_desktops; ++i)
304 focus_order[i] = g_list_remove(focus_order[i], self);
307 focus_order[i] = g_list_remove(focus_order[i], self);
310 /* once the client is out of the list, update the struts to remove it's
312 screen_update_struts();
314 /* tell our parent(s) that we're gone */
315 if (self->transient_for == TRAN_GROUP) { /* transient of group */
318 for (it = self->group->members; it; it = it->next)
319 if (it->data != self)
320 ((Client*)it->data)->transients =
321 g_slist_remove(((Client*)it->data)->transients, self);
322 } else if (self->transient_for) { /* transient of window */
323 self->transient_for->transients =
324 g_slist_remove(self->transient_for->transients, self);
327 /* tell our transients that we're gone */
328 for (it = self->transients; it != NULL; it = it->next) {
329 if (((Client*)it->data)->transient_for != TRAN_GROUP) {
330 ((Client*)it->data)->transient_for = NULL;
331 client_calc_layer(it->data);
335 if (moveresize_client == self)
336 moveresize_end(TRUE);
338 if (focus_client == self)
339 client_unfocus(self);
341 /* remove from its group */
343 group_remove(self->group, self);
347 /* dispatch the unmapped event */
348 dispatch_client(Event_Client_Unmapped, self, 0, 0);
349 g_assert(self != NULL);
351 /* give the client its border back */
352 client_toggle_border(self, TRUE);
354 /* reparent the window out of the frame, and free the frame */
355 frame_release_client(self->frame, self);
358 if (ob_state != State_Exiting) {
359 /* these values should not be persisted across a window
361 prop_erase(self->window, prop_atoms.net_wm_desktop);
362 prop_erase(self->window, prop_atoms.net_wm_state);
364 /* if we're left in an iconic state, the client wont be mapped. this is
365 bad, since we will no longer be managing the window on restart */
367 XMapWindow(ob_display, self->window);
370 g_message("Unmanaged window 0x%lx", self->window);
372 /* free all data allocated in the client struct */
373 g_slist_free(self->transients);
374 for (j = 0; j < self->nicons; ++j)
375 g_free(self->icons[j].data);
376 if (self->nicons > 0)
379 g_free(self->icon_title);
385 /* update the list hints */
389 static void client_move_onscreen(Client *self)
392 int x = self->frame->area.x, y = self->frame->area.y;
394 a = screen_area(self->desktop);
395 if (x >= a->x + a->width - 1)
396 x = a->x + a->width - self->frame->area.width;
397 if (y >= a->y + a->height - 1)
398 y = a->y + a->height - self->frame->area.height;
399 if (x + self->frame->area.width - 1 < a->x)
401 if (y + self->frame->area.height - 1< a->y)
404 frame_frame_gravity(self->frame, &x, &y); /* get where the client
406 client_configure(self , Corner_TopLeft, x, y,
407 self->area.width, self->area.height,
411 static void client_toggle_border(Client *self, gboolean show)
413 /* adjust our idea of where the client is, based on its border. When the
414 border is removed, the client should now be considered to be in a
416 when re-adding the border to the client, the same operation needs to be
418 int oldx = self->area.x, oldy = self->area.y;
419 int x = oldx, y = oldy;
420 switch(self->gravity) {
422 case NorthWestGravity:
424 case SouthWestGravity:
426 case NorthEastGravity:
428 case SouthEastGravity:
429 if (show) x -= self->border_width * 2;
430 else x += self->border_width * 2;
437 if (show) x -= self->border_width;
438 else x += self->border_width;
441 switch(self->gravity) {
443 case NorthWestGravity:
445 case NorthEastGravity:
447 case SouthWestGravity:
449 case SouthEastGravity:
450 if (show) y -= self->border_width * 2;
451 else y += self->border_width * 2;
458 if (show) y -= self->border_width;
459 else y += self->border_width;
466 XSetWindowBorderWidth(ob_display, self->window, self->border_width);
468 /* move the client so it is back it the right spot _with_ its
470 if (x != oldx || y != oldy)
471 XMoveWindow(ob_display, self->window, x, y);
473 XSetWindowBorderWidth(ob_display, self->window, 0);
477 static void client_get_all(Client *self)
479 /* update EVERYTHING!! */
481 self->ignore_unmaps = 0;
485 self->title = self->icon_title = NULL;
486 self->name = self->class = self->role = NULL;
487 self->wmstate = NormalState;
488 self->transient = FALSE;
489 self->transients = NULL;
490 self->transient_for = NULL;
492 self->urgent = FALSE;
493 self->positioned = FALSE;
494 self->disabled_decorations = 0;
498 client_get_area(self);
499 client_update_transient_for(self);
500 client_update_wmhints(self);
501 client_get_desktop(self);
502 client_get_state(self);
503 client_get_shaped(self);
505 client_get_mwm_hints(self);
506 client_get_type(self);/* this can change the mwmhints for special cases */
508 client_update_protocols(self);
510 client_get_gravity(self); /* get the attribute gravity */
511 client_update_normal_hints(self); /* this may override the attribute
514 /* got the type, the mwmhints, the protocols, and the normal hints
515 (min/max sizes), so we're ready to set up the decorations/functions */
516 client_setup_decor_and_functions(self);
518 client_update_title(self);
519 client_update_icon_title(self);
520 client_update_class(self);
521 client_update_strut(self);
522 client_update_icons(self);
523 client_update_kwm_icon(self);
525 client_change_state(self);
528 static void client_get_area(Client *self)
530 XWindowAttributes wattrib;
533 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
534 g_assert(ret != BadWindow);
536 RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
537 self->border_width = wattrib.border_width;
540 static void client_get_desktop(Client *self)
544 if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
545 if (d >= screen_num_desktops && d != DESKTOP_ALL)
546 d = screen_num_desktops - 1;
549 gboolean trdesk = FALSE;
551 if (self->transient_for) {
552 if (self->transient_for != TRAN_GROUP) {
553 self->desktop = self->transient_for->desktop;
558 for (it = self->group->members; it; it = it->next)
559 if (it->data != self &&
560 ((Client*)it->data)->transient_for != TRAN_GROUP) {
561 self->desktop = ((Client*)it->data)->desktop;
568 /* defaults to the current desktop */
569 self->desktop = screen_desktop;
571 /* set the desktop hint, to make sure that it always exists */
572 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
576 static void client_get_state(Client *self)
581 self->modal = self->shaded = self->max_horz = self->max_vert =
582 self->fullscreen = self->above = self->below = self->iconic =
583 self->skip_taskbar = self->skip_pager = FALSE;
585 if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
587 for (i = 0; i < num; ++i) {
588 if (state[i] == prop_atoms.net_wm_state_modal)
590 else if (state[i] == prop_atoms.net_wm_state_shaded)
592 else if (state[i] == prop_atoms.net_wm_state_hidden)
594 else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
595 self->skip_taskbar = TRUE;
596 else if (state[i] == prop_atoms.net_wm_state_skip_pager)
597 self->skip_pager = TRUE;
598 else if (state[i] == prop_atoms.net_wm_state_fullscreen)
599 self->fullscreen = TRUE;
600 else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
601 self->max_vert = TRUE;
602 else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
603 self->max_horz = TRUE;
604 else if (state[i] == prop_atoms.net_wm_state_above)
606 else if (state[i] == prop_atoms.net_wm_state_below)
614 static void client_get_shaped(Client *self)
616 self->shaped = FALSE;
618 if (extensions_shape) {
623 XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
625 XShapeQueryExtents(ob_display, self->window, &s, &foo,
626 &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
628 self->shaped = (s != 0);
633 void client_update_transient_for(Client *self)
638 if (XGetTransientForHint(ob_display, self->window, &t) &&
639 t != self->window) { /* cant be transient to itself! */
640 self->transient = TRUE;
641 c = g_hash_table_lookup(client_map, &t);
642 g_assert(c != self);/* if this happens then we need to check for it*/
644 if (!c && self->group) {
645 /* not transient to a client, see if it is transient for a
647 if (t == self->group->leader ||
650 /* window is a transient for its group! */
655 self->transient = FALSE;
657 /* if anything has changed... */
658 if (c != self->transient_for) {
659 if (self->transient_for == TRAN_GROUP) { /* transient of group */
662 /* remove from old parents */
663 for (it = self->group->members; it; it = it->next)
664 if (it->data != self)
665 ((Client*)it->data)->transients =
666 g_slist_remove(((Client*)it->data)->transients, self);
667 } else if (self->transient_for != NULL) { /* transient of window */
668 /* remove from old parent */
669 self->transient_for->transients =
670 g_slist_remove(self->transient_for->transients, self);
672 self->transient_for = c;
673 if (self->transient_for == TRAN_GROUP) { /* transient of group */
676 /* add to new parents */
677 for (it = self->group->members; it; it = it->next)
678 if (it->data != self)
679 ((Client*)it->data)->transients =
680 g_slist_append(((Client*)it->data)->transients, self);
681 } else if (self->transient_for != NULL) { /* transient of window */
682 /* add to new parent */
683 self->transient_for->transients =
684 g_slist_append(self->transient_for->transients, self);
689 static void client_get_mwm_hints(Client *self)
694 self->mwmhints.flags = 0; /* default to none */
696 if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
698 if (num >= MWM_ELEMENTS) {
699 self->mwmhints.flags = hints[0];
700 self->mwmhints.functions = hints[1];
701 self->mwmhints.decorations = hints[2];
707 void client_get_type(Client *self)
714 if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
715 /* use the first value that we know about in the array */
716 for (i = 0; i < num; ++i) {
717 if (val[i] == prop_atoms.net_wm_window_type_desktop)
718 self->type = Type_Desktop;
719 else if (val[i] == prop_atoms.net_wm_window_type_dock)
720 self->type = Type_Dock;
721 else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
722 self->type = Type_Toolbar;
723 else if (val[i] == prop_atoms.net_wm_window_type_menu)
724 self->type = Type_Menu;
725 else if (val[i] == prop_atoms.net_wm_window_type_utility)
726 self->type = Type_Utility;
727 else if (val[i] == prop_atoms.net_wm_window_type_splash)
728 self->type = Type_Splash;
729 else if (val[i] == prop_atoms.net_wm_window_type_dialog)
730 self->type = Type_Dialog;
731 else if (val[i] == prop_atoms.net_wm_window_type_normal)
732 self->type = Type_Normal;
733 else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
734 /* prevent this window from getting any decor or
736 self->mwmhints.flags &= (MwmFlag_Functions |
737 MwmFlag_Decorations);
738 self->mwmhints.decorations = 0;
739 self->mwmhints.functions = 0;
741 if (self->type != (WindowType) -1)
742 break; /* grab the first legit type */
747 if (self->type == (WindowType) -1) {
748 /*the window type hint was not set, which means we either classify
749 ourself as a normal window or a dialog, depending on if we are a
752 self->type = Type_Dialog;
754 self->type = Type_Normal;
757 /* this makes sure that these windows appear on all desktops */
758 if (self->type == Type_Desktop)
759 self->desktop = DESKTOP_ALL;
762 void client_update_protocols(Client *self)
767 self->focus_notify = FALSE;
768 self->delete_window = FALSE;
770 if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
771 for (i = 0; i < num_return; ++i) {
772 if (proto[i] == prop_atoms.wm_delete_window) {
773 /* this means we can request the window to close */
774 self->delete_window = TRUE;
775 } else if (proto[i] == prop_atoms.wm_take_focus)
776 /* if this protocol is requested, then the window will be
777 notified whenever we want it to receive focus */
778 self->focus_notify = TRUE;
784 static void client_get_gravity(Client *self)
786 XWindowAttributes wattrib;
789 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
790 g_assert(ret != BadWindow);
791 self->gravity = wattrib.win_gravity;
794 void client_update_normal_hints(Client *self)
798 int oldgravity = self->gravity;
801 self->min_ratio = 0.0f;
802 self->max_ratio = 0.0f;
803 SIZE_SET(self->size_inc, 1, 1);
804 SIZE_SET(self->base_size, 0, 0);
805 SIZE_SET(self->min_size, 0, 0);
806 SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
808 /* get the hints from the window */
809 if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
810 self->positioned = !!(size.flags & (PPosition|USPosition));
812 if (size.flags & PWinGravity) {
813 self->gravity = size.win_gravity;
815 /* if the client has a frame, i.e. has already been mapped and
816 is changing its gravity */
817 if (self->frame && self->gravity != oldgravity) {
818 /* move our idea of the client's position based on its new
820 self->area.x = self->frame->area.x;
821 self->area.y = self->frame->area.y;
822 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
826 if (size.flags & PAspect) {
827 if (size.min_aspect.y)
828 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
829 if (size.max_aspect.y)
830 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
833 if (size.flags & PMinSize)
834 SIZE_SET(self->min_size, size.min_width, size.min_height);
836 if (size.flags & PMaxSize)
837 SIZE_SET(self->max_size, size.max_width, size.max_height);
839 if (size.flags & PBaseSize)
840 SIZE_SET(self->base_size, size.base_width, size.base_height);
842 if (size.flags & PResizeInc)
843 SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
847 void client_setup_decor_and_functions(Client *self)
849 /* start with everything (cept fullscreen) */
850 self->decorations = Decor_Titlebar | Decor_Handle | Decor_Border |
851 Decor_Icon | Decor_AllDesktops | Decor_Iconify | Decor_Maximize |
853 self->functions = Func_Resize | Func_Move | Func_Iconify | Func_Maximize |
855 if (self->delete_window) {
856 self->decorations |= Decor_Close;
857 self->functions |= Func_Close;
860 if (!(self->min_size.width < self->max_size.width ||
861 self->min_size.height < self->max_size.height)) {
862 self->decorations &= ~(Decor_Maximize | Decor_Handle);
863 self->functions &= ~(Func_Resize | Func_Maximize);
866 switch (self->type) {
868 /* normal windows retain all of the possible decorations and
869 functionality, and are the only windows that you can fullscreen */
870 self->functions |= Func_Fullscreen;
875 /* these windows cannot be maximized */
876 self->decorations &= ~Decor_Maximize;
877 self->functions &= ~Func_Maximize;
882 /* these windows get less functionality */
883 self->decorations &= ~(Decor_Iconify | Decor_Handle);
884 self->functions &= ~(Func_Iconify | Func_Resize);
890 /* none of these windows are manipulated by the window manager */
891 self->decorations = 0;
896 /* Mwm Hints are applied subtractively to what has already been chosen for
897 decor and functionality */
898 if (self->mwmhints.flags & MwmFlag_Decorations) {
899 if (! (self->mwmhints.decorations & MwmDecor_All)) {
900 if (! (self->mwmhints.decorations & MwmDecor_Border))
901 self->decorations &= ~Decor_Border;
902 if (! (self->mwmhints.decorations & MwmDecor_Handle))
903 self->decorations &= ~Decor_Handle;
904 if (! (self->mwmhints.decorations & MwmDecor_Title))
905 self->decorations &= ~Decor_Titlebar;
906 if (! (self->mwmhints.decorations & MwmDecor_Iconify))
907 self->decorations &= ~Decor_Iconify;
908 if (! (self->mwmhints.decorations & MwmDecor_Maximize))
909 self->decorations &= ~Decor_Maximize;
913 if (self->mwmhints.flags & MwmFlag_Functions) {
914 if (! (self->mwmhints.functions & MwmFunc_All)) {
915 if (! (self->mwmhints.functions & MwmFunc_Resize))
916 self->functions &= ~Func_Resize;
917 if (! (self->mwmhints.functions & MwmFunc_Move))
918 self->functions &= ~Func_Move;
919 if (! (self->mwmhints.functions & MwmFunc_Iconify))
920 self->functions &= ~Func_Iconify;
921 if (! (self->mwmhints.functions & MwmFunc_Maximize))
922 self->functions &= ~Func_Maximize;
923 /* dont let mwm hints kill the close button
924 if (! (self->mwmhints.functions & MwmFunc_Close))
925 self->functions &= ~Func_Close; */
929 /* can't maximize without moving/resizing */
930 if (!((self->functions & Func_Move) && (self->functions & Func_Resize)))
931 self->functions &= ~(Func_Maximize | Func_Fullscreen);
933 /* finally, user specified disabled decorations are applied to subtract
935 if (self->disabled_decorations & Decor_Titlebar)
936 self->decorations &= ~Decor_Titlebar;
937 if (self->disabled_decorations & Decor_Handle)
938 self->decorations &= ~Decor_Handle;
939 if (self->disabled_decorations & Decor_Border)
940 self->decorations &= ~Decor_Border;
941 if (self->disabled_decorations & Decor_Iconify)
942 self->decorations &= ~Decor_Iconify;
943 if (self->disabled_decorations & Decor_Maximize)
944 self->decorations &= ~Decor_Maximize;
945 if (self->disabled_decorations & Decor_AllDesktops)
946 self->decorations &= ~Decor_AllDesktops;
947 if (self->disabled_decorations & Decor_Shade)
948 self->decorations &= ~Decor_Shade;
949 if (self->disabled_decorations & Decor_Close)
950 self->decorations &= ~Decor_Close;
952 /* if we don't have a titlebar, then we cannot shade! */
953 if (!(self->decorations & Decor_Titlebar))
954 self->functions &= ~Func_Shade;
956 /* now we need to check against rules for the client's current state */
957 if (self->fullscreen) {
958 self->functions &= (Func_Close | Func_Fullscreen | Func_Iconify);
959 self->decorations = 0;
962 client_change_allowed_actions(self);
965 /* change the decors on the frame, and with more/less decorations,
966 we may also need to be repositioned */
967 frame_adjust_area(self->frame, TRUE, TRUE);
968 /* with new decor, the window's maximized size may change */
969 client_remaximize(self);
973 static void client_change_allowed_actions(Client *self)
978 actions[num++] = prop_atoms.net_wm_action_change_desktop;
980 if (self->functions & Func_Shade)
981 actions[num++] = prop_atoms.net_wm_action_shade;
982 if (self->functions & Func_Close)
983 actions[num++] = prop_atoms.net_wm_action_close;
984 if (self->functions & Func_Move)
985 actions[num++] = prop_atoms.net_wm_action_move;
986 if (self->functions & Func_Iconify)
987 actions[num++] = prop_atoms.net_wm_action_minimize;
988 if (self->functions & Func_Resize)
989 actions[num++] = prop_atoms.net_wm_action_resize;
990 if (self->functions & Func_Fullscreen)
991 actions[num++] = prop_atoms.net_wm_action_fullscreen;
992 if (self->functions & Func_Maximize) {
993 actions[num++] = prop_atoms.net_wm_action_maximize_horz;
994 actions[num++] = prop_atoms.net_wm_action_maximize_vert;
997 PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
999 /* make sure the window isn't breaking any rules now */
1001 if (!(self->functions & Func_Shade) && self->shaded) {
1002 if (self->frame) client_shade(self, FALSE);
1003 else self->shaded = FALSE;
1005 if (!(self->functions & Func_Iconify) && self->iconic) {
1006 g_message("UNSETTING ICONIC");
1007 if (self->frame) client_iconify(self, FALSE, TRUE);
1008 else self->iconic = FALSE;
1010 if (!(self->functions & Func_Fullscreen) && self->fullscreen) {
1011 if (self->frame) client_fullscreen(self, FALSE, TRUE);
1012 else self->fullscreen = FALSE;
1014 if (!(self->functions & Func_Maximize) && (self->max_horz ||
1016 if (self->frame) client_maximize(self, FALSE, 0, TRUE);
1017 else self->max_vert = self->max_horz = FALSE;
1021 void client_remaximize(Client *self)
1024 if (self->max_horz && self->max_vert)
1026 else if (self->max_horz)
1028 else if (self->max_vert)
1031 return; /* not maximized */
1032 self->max_horz = self->max_vert = FALSE;
1033 client_maximize(self, TRUE, dir, FALSE);
1036 void client_update_wmhints(Client *self)
1039 gboolean ur = FALSE;
1042 /* assume a window takes input if it doesnt specify */
1043 self->can_focus = TRUE;
1045 if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1046 if (hints->flags & InputHint)
1047 self->can_focus = hints->input;
1049 /* only do this when first managing the window *AND* when we aren't
1051 if (ob_state != State_Starting && self->frame == NULL)
1052 if (hints->flags & StateHint)
1053 self->iconic = hints->initial_state == IconicState;
1055 if (hints->flags & XUrgencyHint)
1058 if (!(hints->flags & WindowGroupHint))
1059 hints->window_group = None;
1061 /* did the group state change? */
1062 if (hints->window_group !=
1063 (self->group ? self->group->leader : None)) {
1064 /* remove from the old group if there was one */
1065 if (self->group != NULL) {
1066 /* remove transients of the group */
1067 for (it = self->group->members; it; it = it->next)
1068 if (it->data != self &&
1069 ((Client*)it->data)->transient_for == TRAN_GROUP) {
1070 self->transients = g_slist_remove(self->transients,
1073 group_remove(self->group, self);
1076 if (hints->window_group != None) {
1077 self->group = group_add(hints->window_group, self);
1079 /* add other transients of the group that are already
1081 for (it = self->group->members; it; it = it->next)
1082 if (it->data != self &&
1083 ((Client*)it->data)->transient_for == TRAN_GROUP)
1084 self->transients = g_slist_append(self->transients,
1088 /* because the self->transient flag wont change from this call,
1089 we don't need to update the window's type and such, only its
1090 transient_for, and the transients lists of other windows in
1091 the group may be affected */
1092 client_update_transient_for(self);
1095 client_update_kwm_icon(self);
1096 /* try get the kwm icon first, this is a fallback only */
1097 if (self->pixmap_icon == None) {
1098 if (hints->flags & IconPixmapHint) {
1099 if (self->pixmap_icon == None) {
1100 self->pixmap_icon = hints->icon_pixmap;
1101 if (hints->flags & IconMaskHint)
1102 self->pixmap_icon_mask = hints->icon_mask;
1104 self->pixmap_icon_mask = None;
1107 frame_adjust_icon(self->frame);
1115 if (ur != self->urgent) {
1117 g_message("Urgent Hint for 0x%lx: %s", self->window,
1119 /* fire the urgent callback if we're mapped, otherwise, wait until
1120 after we're mapped */
1122 dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1126 void client_update_title(Client *self)
1130 g_free(self->title);
1133 if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1134 /* try old x stuff */
1135 if (!PROP_GETS(self->window, wm_name, locale, &data))
1136 data = g_strdup("Unnamed Window");
1138 /* look for duplicates and append a number */
1140 PROP_SETS(self->window, net_wm_visible_name, data);
1145 frame_adjust_title(self->frame);
1148 void client_update_icon_title(Client *self)
1152 g_free(self->icon_title);
1155 if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1156 /* try old x stuff */
1157 if (!PROP_GETS(self->window, wm_icon_name, locale, &data))
1158 data = g_strdup("Unnamed Window");
1160 PROP_SETS(self->window, net_wm_visible_icon_name, data);
1162 self->icon_title = data;
1165 void client_update_class(Client *self)
1170 if (self->name) g_free(self->name);
1171 if (self->class) g_free(self->class);
1172 if (self->role) g_free(self->role);
1174 self->name = self->class = self->role = NULL;
1176 if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1178 self->name = g_strdup(data[0]);
1180 self->class = g_strdup(data[1]);
1185 if (PROP_GETS(self->window, wm_window_role, locale, &s))
1186 self->role = g_strdup(s);
1188 if (self->name == NULL) self->name = g_strdup("");
1189 if (self->class == NULL) self->class = g_strdup("");
1190 if (self->role == NULL) self->role = g_strdup("");
1193 void client_update_strut(Client *self)
1198 if (!PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1199 STRUT_SET(self->strut, 0, 0, 0, 0);
1202 STRUT_SET(self->strut, data[0], data[2], data[1], data[3]);
1204 STRUT_SET(self->strut, 0, 0, 0, 0);
1208 /* updating here is pointless while we're being mapped cuz we're not in
1209 the client list yet */
1211 screen_update_struts();
1214 void client_update_icons(Client *self)
1221 for (j = 0; j < self->nicons; ++j)
1222 g_free(self->icons[j].data);
1223 if (self->nicons > 0)
1224 g_free(self->icons);
1227 if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1228 /* figure out how many valid icons are in here */
1230 while (num - i > 2) {
1238 self->icons = g_new(Icon, self->nicons);
1240 /* store the icons */
1242 for (j = 0; j < self->nicons; ++j) {
1243 w = self->icons[j].width = data[i++];
1244 h = self->icons[j].height = data[i++];
1245 self->icons[j].data =
1246 g_memdup(&data[i], w * h * sizeof(gulong));
1255 frame_adjust_icon(self->frame);
1258 void client_update_kwm_icon(Client *self)
1263 if (!PROP_GETA32(self->window, kwm_win_icon, kwm_win_icon, &data, &num)) {
1264 self->pixmap_icon = self->pixmap_icon_mask = None;
1267 self->pixmap_icon = data[0];
1268 self->pixmap_icon_mask = data[1];
1270 self->pixmap_icon = self->pixmap_icon_mask = None;
1274 frame_adjust_icon(self->frame);
1277 static void client_change_state(Client *self)
1280 guint32 netstate[10];
1283 state[0] = self->wmstate;
1285 PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1289 netstate[num++] = prop_atoms.net_wm_state_modal;
1291 netstate[num++] = prop_atoms.net_wm_state_shaded;
1293 netstate[num++] = prop_atoms.net_wm_state_hidden;
1294 if (self->skip_taskbar)
1295 netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1296 if (self->skip_pager)
1297 netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1298 if (self->fullscreen)
1299 netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1301 netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1303 netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1305 netstate[num++] = prop_atoms.net_wm_state_above;
1307 netstate[num++] = prop_atoms.net_wm_state_below;
1308 PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1310 client_calc_layer(self);
1313 frame_adjust_state(self->frame);
1316 static Client *search_focus_tree(Client *node, Client *skip)
1321 for (it = node->transients; it != NULL; it = it->next) {
1322 Client *c = it->data;
1323 if (c == skip) continue; /* circular? */
1324 if ((ret = search_focus_tree(c, skip))) return ret;
1325 if (client_focused(c)) return c;
1330 static void calc_recursive(Client *self, StackLayer l, gboolean raised)
1338 for (it = self->transients; it; it = it->next)
1339 calc_recursive(it->data, l, raised ? raised : l != old);
1341 if (!raised && l != old)
1343 stacking_raise(self);
1346 void client_calc_layer(Client *self)
1351 /* transients take on the layer of their parents */
1352 if (self->transient_for) {
1353 if (self->transient_for != TRAN_GROUP) {
1354 self = self->transient_for;
1358 for (it = self->group->members; it; it = it->next)
1359 if (it->data != self &&
1360 ((Client*)it->data)->transient_for != TRAN_GROUP) {
1367 /* is us or one of our transients focused? */
1368 if (client_focused(self))
1370 else if (search_focus_tree(self, self))
1375 if (self->iconic) l = Layer_Icon;
1376 /* fullscreen windows are only in the fullscreen layer while focused */
1377 else if (self->fullscreen && f) l = Layer_Fullscreen;
1378 else if (self->type == Type_Desktop) l = Layer_Desktop;
1379 else if (self->type == Type_Dock) {
1380 if (!self->below) l = Layer_Top;
1381 else l = Layer_Normal;
1383 else if (self->above) l = Layer_Above;
1384 else if (self->below) l = Layer_Below;
1385 else l = Layer_Normal;
1387 calc_recursive(self, l, FALSE);
1390 gboolean client_should_show(Client *self)
1392 if (self->iconic) return FALSE;
1393 else if (!(self->desktop == screen_desktop ||
1394 self->desktop == DESKTOP_ALL)) return FALSE;
1395 else if (client_normal(self) && screen_showing_desktop) return FALSE;
1400 static void client_showhide(Client *self)
1403 if (client_should_show(self))
1404 frame_show(self->frame);
1406 frame_hide(self->frame);
1409 gboolean client_normal(Client *self) {
1410 return ! (self->type == Type_Desktop || self->type == Type_Dock ||
1411 self->type == Type_Splash);
1414 static void client_apply_startup_state(Client *self)
1416 /* these are in a carefully crafted order.. */
1419 self->iconic = FALSE;
1420 client_iconify(self, TRUE, FALSE);
1422 if (self->fullscreen) {
1423 self->fullscreen = FALSE;
1424 client_fullscreen(self, TRUE, FALSE);
1427 self->shaded = FALSE;
1428 client_shade(self, TRUE);
1431 dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1433 if (self->max_vert && self->max_horz) {
1434 self->max_vert = self->max_horz = FALSE;
1435 client_maximize(self, TRUE, 0, FALSE);
1436 } else if (self->max_vert) {
1437 self->max_vert = FALSE;
1438 client_maximize(self, TRUE, 2, FALSE);
1439 } else if (self->max_horz) {
1440 self->max_horz = FALSE;
1441 client_maximize(self, TRUE, 1, FALSE);
1444 /* nothing to do for the other states:
1453 void client_configure(Client *self, Corner anchor, int x, int y, int w, int h,
1454 gboolean user, gboolean final)
1456 gboolean moved = FALSE, resized = FALSE;
1458 /* gets the frame's position */
1459 frame_client_gravity(self->frame, &x, &y);
1461 /* these positions are frame positions, not client positions */
1463 /* set the size and position if fullscreen */
1464 if (self->fullscreen) {
1467 w = screen_physical_size.width;
1468 h = screen_physical_size.height;
1469 user = FALSE; /* ignore that increment etc shit when in fullscreen */
1471 /* set the size and position if maximized */
1472 if (self->max_horz) {
1473 x = screen_area(self->desktop)->x - self->frame->size.left;
1474 w = screen_area(self->desktop)->width;
1476 if (self->max_vert) {
1477 y = screen_area(self->desktop)->y;
1478 h = screen_area(self->desktop)->height -
1479 self->frame->size.top - self->frame->size.bottom;
1483 /* gets the client's position */
1484 frame_frame_gravity(self->frame, &x, &y);
1486 /* these override the above states! if you cant move you can't move! */
1488 if (!(self->functions & Func_Move)) {
1492 if (!(self->functions & Func_Resize)) {
1493 w = self->area.width;
1494 h = self->area.height;
1498 if (!(w == self->area.width && h == self->area.height)) {
1499 w -= self->base_size.width;
1500 h -= self->base_size.height;
1503 /* for interactive resizing. have to move half an increment in each
1506 /* how far we are towards the next size inc */
1507 int mw = w % self->size_inc.width;
1508 int mh = h % self->size_inc.height;
1510 int aw = self->size_inc.width / 2;
1511 int ah = self->size_inc.height / 2;
1512 /* don't let us move into a new size increment */
1513 if (mw + aw >= self->size_inc.width)
1514 aw = self->size_inc.width - mw - 1;
1515 if (mh + ah >= self->size_inc.height)
1516 ah = self->size_inc.height - mh - 1;
1520 /* if this is a user-requested resize, then check against min/max
1521 sizes and aspect ratios */
1523 /* smaller than min size or bigger than max size? */
1524 if (w > self->max_size.width) w = self->max_size.width;
1525 if (w < self->min_size.width) w = self->min_size.width;
1526 if (h > self->max_size.height) h = self->max_size.height;
1527 if (h < self->min_size.height) h = self->min_size.height;
1529 /* adjust the height ot match the width for the aspect ratios */
1530 if (self->min_ratio)
1531 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1532 if (self->max_ratio)
1533 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1536 /* keep to the increments */
1537 w /= self->size_inc.width;
1538 h /= self->size_inc.height;
1540 /* you cannot resize to nothing */
1544 /* store the logical size */
1545 SIZE_SET(self->logical_size, w, h);
1547 w *= self->size_inc.width;
1548 h *= self->size_inc.height;
1550 w += self->base_size.width;
1551 h += self->base_size.height;
1555 case Corner_TopLeft:
1557 case Corner_TopRight:
1558 x -= w - self->area.width;
1560 case Corner_BottomLeft:
1561 y -= h - self->area.height;
1563 case Corner_BottomRight:
1564 x -= w - self->area.width;
1565 y -= h - self->area.height;
1569 moved = x != self->area.x || y != self->area.y;
1570 resized = w != self->area.width || h != self->area.height;
1572 RECT_SET(self->area, x, y, w, h);
1575 XResizeWindow(ob_display, self->window, w, h);
1577 /* move/resize the frame to match the request */
1579 if (moved || resized)
1580 frame_adjust_area(self->frame, moved, resized);
1582 if (!user || final) {
1584 event.type = ConfigureNotify;
1585 event.xconfigure.display = ob_display;
1586 event.xconfigure.event = self->window;
1587 event.xconfigure.window = self->window;
1589 /* root window coords with border in mind */
1590 event.xconfigure.x = x - self->border_width +
1591 self->frame->size.left;
1592 event.xconfigure.y = y - self->border_width +
1593 self->frame->size.top;
1595 event.xconfigure.width = self->area.width;
1596 event.xconfigure.height = self->area.height;
1597 event.xconfigure.border_width = self->border_width;
1598 event.xconfigure.above = self->frame->plate;
1599 event.xconfigure.override_redirect = FALSE;
1600 XSendEvent(event.xconfigure.display, event.xconfigure.window,
1601 FALSE, StructureNotifyMask, &event);
1606 void client_fullscreen(Client *self, gboolean fs, gboolean savearea)
1610 if (!(self->functions & Func_Fullscreen) || /* can't */
1611 self->fullscreen == fs) return; /* already done */
1613 self->fullscreen = fs;
1614 client_change_state(self); /* change the state hints on the client,
1615 and adjust out layer/stacking */
1619 guint32 dimensions[4];
1620 dimensions[0] = self->area.x;
1621 dimensions[1] = self->area.y;
1622 dimensions[2] = self->area.width;
1623 dimensions[3] = self->area.height;
1625 PROP_SETA32(self->window, openbox_premax, cardinal,
1629 /* these are not actually used cuz client_configure will set them
1630 as appropriate when the window is fullscreened */
1636 /* pick some fallbacks... */
1637 x = screen_area(self->desktop)->x +
1638 screen_area(self->desktop)->width / 4;
1639 y = screen_area(self->desktop)->y +
1640 screen_area(self->desktop)->height / 4;
1641 w = screen_area(self->desktop)->width / 2;
1642 h = screen_area(self->desktop)->height / 2;
1644 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1645 (guint32**)&dimensions, &num)) {
1656 client_setup_decor_and_functions(self);
1658 client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1660 /* try focus us when we go into fullscreen mode */
1664 void client_iconify(Client *self, gboolean iconic, gboolean curdesk)
1668 /* move up the transient chain as far as possible first if deiconifying */
1670 while (self->transient_for) {
1671 if (self->transient_for != TRAN_GROUP) {
1672 if (self->transient_for->iconic == iconic)
1674 self = self->transient_for;
1678 /* the check for TRAN_GROUP is to prevent an infinate loop with
1679 2 transients of the same group at the head of the group's
1681 for (it = self->group->members; it; it = it->next) {
1682 Client *c = it->data;
1684 if (c != self && c->transient_for->iconic != iconic &&
1685 c->transient_for != TRAN_GROUP) {
1690 if (it == NULL) break;
1694 if (self->iconic == iconic) return; /* nothing to do */
1696 g_message("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
1699 self->iconic = iconic;
1702 self->wmstate = IconicState;
1703 self->ignore_unmaps++;
1704 /* we unmap the client itself so that we can get MapRequest events,
1705 and because the ICCCM tells us to! */
1706 XUnmapWindow(ob_display, self->window);
1709 client_set_desktop(self, screen_desktop, FALSE);
1710 self->wmstate = self->shaded ? IconicState : NormalState;
1711 XMapWindow(ob_display, self->window);
1713 client_change_state(self);
1714 client_showhide(self);
1715 screen_update_struts();
1717 dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
1720 /* iconify all transients */
1721 for (it = self->transients; it != NULL; it = it->next)
1722 if (it->data != self) client_iconify(it->data, iconic, curdesk);
1725 void client_maximize(Client *self, gboolean max, int dir, gboolean savearea)
1729 g_assert(dir == 0 || dir == 1 || dir == 2);
1730 if (!(self->functions & Func_Maximize)) return; /* can't */
1732 /* check if already done */
1734 if (dir == 0 && self->max_horz && self->max_vert) return;
1735 if (dir == 1 && self->max_horz) return;
1736 if (dir == 2 && self->max_vert) return;
1738 if (dir == 0 && !self->max_horz && !self->max_vert) return;
1739 if (dir == 1 && !self->max_horz) return;
1740 if (dir == 2 && !self->max_vert) return;
1743 /* work with the frame's coords */
1744 x = self->frame->area.x;
1745 y = self->frame->area.y;
1746 w = self->area.width;
1747 h = self->area.height;
1751 gint32 dimensions[4];
1760 /* get the property off the window and use it for the dimensions
1761 we are already maxed on */
1762 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1763 (guint32**)&readdim, &num)) {
1765 if (self->max_horz) {
1766 dimensions[0] = readdim[0];
1767 dimensions[2] = readdim[2];
1769 if (self->max_vert) {
1770 dimensions[1] = readdim[1];
1771 dimensions[3] = readdim[3];
1777 PROP_SETA32(self->window, openbox_premax, cardinal,
1778 (guint32*)dimensions, 4);
1784 /* pick some fallbacks... */
1785 if (dir == 0 || dir == 1) { /* horz */
1786 x = screen_area(self->desktop)->x +
1787 screen_area(self->desktop)->width / 4;
1788 w = screen_area(self->desktop)->width / 2;
1790 if (dir == 0 || dir == 2) { /* vert */
1791 y = screen_area(self->desktop)->y +
1792 screen_area(self->desktop)->height / 4;
1793 h = screen_area(self->desktop)->height / 2;
1796 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1797 (guint32**)&dimensions, &num)) {
1799 if (dir == 0 || dir == 1) { /* horz */
1803 if (dir == 0 || dir == 2) { /* vert */
1812 if (dir == 0 || dir == 1) /* horz */
1813 self->max_horz = max;
1814 if (dir == 0 || dir == 2) /* vert */
1815 self->max_vert = max;
1817 if (!self->max_horz && !self->max_vert)
1818 PROP_ERASE(self->window, openbox_premax);
1820 client_change_state(self); /* change the state hints on the client */
1822 /* figure out where the client should be going */
1823 frame_frame_gravity(self->frame, &x, &y);
1824 client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1827 void client_shade(Client *self, gboolean shade)
1829 if ((!(self->functions & Func_Shade) && shade) || /* can't shade */
1830 self->shaded == shade) return; /* already done */
1832 /* when we're iconic, don't change the wmstate */
1834 self->wmstate = shade ? IconicState : NormalState;
1835 self->shaded = shade;
1836 client_change_state(self);
1837 /* resize the frame to just the titlebar */
1838 frame_adjust_area(self->frame, FALSE, FALSE);
1841 void client_close(Client *self)
1845 if (!(self->functions & Func_Close)) return;
1848 XXX: itd be cool to do timeouts and shit here for killing the client's
1850 like... if the window is around after 5 seconds, then the close button
1851 turns a nice red, and if this function is called again, the client is
1855 ce.xclient.type = ClientMessage;
1856 ce.xclient.message_type = prop_atoms.wm_protocols;
1857 ce.xclient.display = ob_display;
1858 ce.xclient.window = self->window;
1859 ce.xclient.format = 32;
1860 ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
1861 ce.xclient.data.l[1] = event_lasttime;
1862 ce.xclient.data.l[2] = 0l;
1863 ce.xclient.data.l[3] = 0l;
1864 ce.xclient.data.l[4] = 0l;
1865 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
1868 void client_kill(Client *self)
1870 XKillClient(ob_display, self->window);
1873 void client_set_desktop(Client *self, guint target, gboolean donthide)
1877 if (target == self->desktop) return;
1879 g_message("Setting desktop %u", target);
1881 g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
1883 old = self->desktop;
1884 self->desktop = target;
1885 PROP_SET32(self->window, net_wm_desktop, cardinal, target);
1886 /* the frame can display the current desktop state */
1887 frame_adjust_state(self->frame);
1888 /* 'move' the window to the new desktop */
1890 client_showhide(self);
1891 /* raise if it was not already on the desktop */
1892 if (old != DESKTOP_ALL)
1893 stacking_raise(self);
1894 screen_update_struts();
1896 /* update the focus lists */
1897 if (old == DESKTOP_ALL) {
1898 for (i = 0; i < screen_num_desktops; ++i)
1899 focus_order[i] = g_list_remove(focus_order[i], self);
1901 focus_order[old] = g_list_remove(focus_order[old], self);
1902 if (target == DESKTOP_ALL) {
1903 for (i = 0; i < screen_num_desktops; ++i) {
1904 if (config_focus_new)
1905 focus_order[i] = g_list_prepend(focus_order[i], self);
1907 focus_order[i] = g_list_append(focus_order[i], self);
1910 if (config_focus_new)
1911 focus_order[target] = g_list_prepend(focus_order[target], self);
1913 focus_order[target] = g_list_append(focus_order[target], self);
1916 dispatch_client(Event_Client_Desktop, self, target, old);
1919 static Client *search_modal_tree(Client *node, Client *skip)
1924 for (it = node->transients; it != NULL; it = it->next) {
1925 Client *c = it->data;
1926 if (c == skip) continue; /* circular? */
1927 if ((ret = search_modal_tree(c, skip))) return ret;
1928 if (c->modal) return c;
1933 Client *client_find_modal_child(Client *self)
1935 return search_modal_tree(self, self);
1938 gboolean client_validate(Client *self)
1942 XSync(ob_display, FALSE); /* get all events on the server */
1944 if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
1945 XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
1946 XPutBackEvent(ob_display, &e);
1953 void client_set_wm_state(Client *self, long state)
1955 if (state == self->wmstate) return; /* no change */
1959 client_iconify(self, TRUE, TRUE);
1962 client_iconify(self, FALSE, TRUE);
1967 void client_set_state(Client *self, Atom action, long data1, long data2)
1969 gboolean shaded = self->shaded;
1970 gboolean fullscreen = self->fullscreen;
1971 gboolean max_horz = self->max_horz;
1972 gboolean max_vert = self->max_vert;
1975 if (!(action == prop_atoms.net_wm_state_add ||
1976 action == prop_atoms.net_wm_state_remove ||
1977 action == prop_atoms.net_wm_state_toggle))
1978 /* an invalid action was passed to the client message, ignore it */
1981 for (i = 0; i < 2; ++i) {
1982 Atom state = i == 0 ? data1 : data2;
1984 if (!state) continue;
1986 /* if toggling, then pick whether we're adding or removing */
1987 if (action == prop_atoms.net_wm_state_toggle) {
1988 if (state == prop_atoms.net_wm_state_modal)
1989 action = self->modal ? prop_atoms.net_wm_state_remove :
1990 prop_atoms.net_wm_state_add;
1991 else if (state == prop_atoms.net_wm_state_maximized_vert)
1992 action = self->max_vert ? prop_atoms.net_wm_state_remove :
1993 prop_atoms.net_wm_state_add;
1994 else if (state == prop_atoms.net_wm_state_maximized_horz)
1995 action = self->max_horz ? prop_atoms.net_wm_state_remove :
1996 prop_atoms.net_wm_state_add;
1997 else if (state == prop_atoms.net_wm_state_shaded)
1998 action = self->shaded ? prop_atoms.net_wm_state_remove :
1999 prop_atoms.net_wm_state_add;
2000 else if (state == prop_atoms.net_wm_state_skip_taskbar)
2001 action = self->skip_taskbar ?
2002 prop_atoms.net_wm_state_remove :
2003 prop_atoms.net_wm_state_add;
2004 else if (state == prop_atoms.net_wm_state_skip_pager)
2005 action = self->skip_pager ?
2006 prop_atoms.net_wm_state_remove :
2007 prop_atoms.net_wm_state_add;
2008 else if (state == prop_atoms.net_wm_state_fullscreen)
2009 action = self->fullscreen ?
2010 prop_atoms.net_wm_state_remove :
2011 prop_atoms.net_wm_state_add;
2012 else if (state == prop_atoms.net_wm_state_above)
2013 action = self->above ? prop_atoms.net_wm_state_remove :
2014 prop_atoms.net_wm_state_add;
2015 else if (state == prop_atoms.net_wm_state_below)
2016 action = self->below ? prop_atoms.net_wm_state_remove :
2017 prop_atoms.net_wm_state_add;
2020 if (action == prop_atoms.net_wm_state_add) {
2021 if (state == prop_atoms.net_wm_state_modal) {
2022 /* XXX raise here or something? */
2024 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2026 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2028 } else if (state == prop_atoms.net_wm_state_shaded) {
2030 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2031 self->skip_taskbar = TRUE;
2032 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2033 self->skip_pager = TRUE;
2034 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2036 } else if (state == prop_atoms.net_wm_state_above) {
2038 } else if (state == prop_atoms.net_wm_state_below) {
2042 } else { /* action == prop_atoms.net_wm_state_remove */
2043 if (state == prop_atoms.net_wm_state_modal) {
2044 self->modal = FALSE;
2045 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2047 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2049 } else if (state == prop_atoms.net_wm_state_shaded) {
2051 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2052 self->skip_taskbar = FALSE;
2053 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2054 self->skip_pager = FALSE;
2055 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2057 } else if (state == prop_atoms.net_wm_state_above) {
2058 self->above = FALSE;
2059 } else if (state == prop_atoms.net_wm_state_below) {
2060 self->below = FALSE;
2064 if (max_horz != self->max_horz || max_vert != self->max_vert) {
2065 if (max_horz != self->max_horz && max_vert != self->max_vert) {
2067 if (max_horz == max_vert) { /* both going the same way */
2068 client_maximize(self, max_horz, 0, TRUE);
2070 client_maximize(self, max_horz, 1, TRUE);
2071 client_maximize(self, max_vert, 2, TRUE);
2075 if (max_horz != self->max_horz)
2076 client_maximize(self, max_horz, 1, TRUE);
2078 client_maximize(self, max_vert, 2, TRUE);
2081 /* change fullscreen state before shading, as it will affect if the window
2083 if (fullscreen != self->fullscreen)
2084 client_fullscreen(self, fullscreen, TRUE);
2085 if (shaded != self->shaded)
2086 client_shade(self, shaded);
2087 client_calc_layer(self);
2088 client_change_state(self); /* change the hint to relect these changes */
2091 Client *client_focus_target(Client *self)
2095 /* if we have a modal child, then focus it, not us */
2096 child = client_find_modal_child(self);
2097 if (child) return child;
2101 gboolean client_focusable(Client *self)
2103 /* won't try focus if the client doesn't want it, or if the window isn't
2104 visible on the screen */
2105 return self->frame->visible &&
2106 (self->can_focus || self->focus_notify);
2109 gboolean client_focus(Client *self)
2114 /* choose the correct target */
2115 self = client_focus_target(self);
2117 if (self->desktop != DESKTOP_ALL && self->desktop != screen_desktop) {
2118 /* update the focus lists */
2119 if (self->desktop == DESKTOP_ALL) {
2120 for (i = 0; i < screen_num_desktops; ++i) {
2121 focus_order[i] = g_list_remove(focus_order[i], self);
2122 focus_order[i] = g_list_prepend(focus_order[i], self);
2126 focus_order[i] = g_list_remove(focus_order[i], self);
2127 focus_order[i] = g_list_prepend(focus_order[i], self);
2132 if (!client_focusable(self))
2135 /* do a check to see if the window has already been unmapped or destroyed
2136 do this intelligently while watching out for unmaps we've generated
2137 (ignore_unmaps > 0) */
2138 if (XCheckTypedWindowEvent(ob_display, self->window,
2139 DestroyNotify, &ev)) {
2140 XPutBackEvent(ob_display, &ev);
2143 while (XCheckTypedWindowEvent(ob_display, self->window,
2144 UnmapNotify, &ev)) {
2145 if (self->ignore_unmaps) {
2146 self->ignore_unmaps--;
2148 XPutBackEvent(ob_display, &ev);
2153 if (self->can_focus)
2154 /* RevertToPointerRoot causes much more headache than TevertToNone, so
2155 I choose to use it always, hopefully to find errors quicker, if any
2156 are left. (I hate X. I hate focus events.) */
2157 XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2160 if (self->focus_notify) {
2162 ce.xclient.type = ClientMessage;
2163 ce.xclient.message_type = prop_atoms.wm_protocols;
2164 ce.xclient.display = ob_display;
2165 ce.xclient.window = self->window;
2166 ce.xclient.format = 32;
2167 ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2168 ce.xclient.data.l[1] = event_lasttime;
2169 ce.xclient.data.l[2] = 0l;
2170 ce.xclient.data.l[3] = 0l;
2171 ce.xclient.data.l[4] = 0l;
2172 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2176 g_message("focusing %lx", self->window);
2179 /* Cause the FocusIn to come back to us. Important for desktop switches,
2180 since otherwise we'll have no FocusIn on the queue and send it off to
2181 the focus_backup. */
2182 XSync(ob_display, FALSE);
2186 void client_unfocus(Client *self)
2188 g_assert(focus_client == self);
2190 g_message("client_unfocus");
2192 focus_fallback(Fallback_Unfocusing);
2195 gboolean client_focused(Client *self)
2197 return self == focus_client;
2200 Icon *client_icon(Client *self, int w, int h)
2203 /* si is the smallest image >= req */
2204 /* li is the largest image < req */
2205 unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2207 if (!self->nicons) return NULL;
2209 for (i = 0; i < self->nicons; ++i) {
2210 size = self->icons[i].width * self->icons[i].height;
2211 if (size < smallest && size >= (unsigned)(w * h)) {
2215 if (size > largest && size <= (unsigned)(w * h)) {
2220 if (largest == 0) /* didnt find one smaller than the requested size */
2221 return &self->icons[si];
2222 return &self->icons[li];