2 * ion/ioncore/clientwin.c
4 * Copyright (c) Tuomo Valkonen 1999-2008.
6 * See the included file LICENSE for details.
13 #include <libtu/objp.h>
14 #include <libtu/minmax.h>
15 #include <libextl/extl.h>
16 #include <libmainloop/defer.h>
17 #include <libmainloop/hooks.h>
24 #include "clientwin.h"
34 #include "fullscreen.h"
46 static void set_clientwin_state(WClientWin *cwin, int state);
47 static bool send_clientmsg(Window win, Atom a, Time stmp);
50 WHook *clientwin_do_manage_alt=NULL;
51 WHook *clientwin_mapped_hook=NULL;
52 WHook *clientwin_unmapped_hook=NULL;
53 WHook *clientwin_property_change_hook=NULL;
56 /*{{{ Get properties */
59 void clientwin_get_protocols(WClientWin *cwin)
61 Atom *protocols=NULL, *p;
64 cwin->flags&=~(CLIENTWIN_P_WM_DELETE|CLIENTWIN_P_WM_TAKE_FOCUS);
66 if(!XGetWMProtocols(ioncore_g.dpy, cwin->win, &protocols, &n))
69 for(p=protocols; n; n--, p++){
70 if(*p==ioncore_g.atom_wm_delete)
71 cwin->flags|=CLIENTWIN_P_WM_DELETE;
72 else if(*p==ioncore_g.atom_wm_take_focus)
73 cwin->flags|=CLIENTWIN_P_WM_TAKE_FOCUS;
77 XFree((char*)protocols);
81 static WSizePolicy get_sizepolicy_winprop(WClientWin *cwin,
87 if(extl_table_gets_s(cwin->proptab, propname, &szplcy)){
88 string2sizepolicy(szplcy, &value);
95 #define SIZEHINT_PROPS (CLIENTWIN_PROP_MAXSIZE| \
96 CLIENTWIN_PROP_MINSIZE| \
97 CLIENTWIN_PROP_ASPECT| \
98 CLIENTWIN_PROP_RSZINC| \
99 CLIENTWIN_PROP_I_MAXSIZE| \
100 CLIENTWIN_PROP_I_MINSIZE| \
101 CLIENTWIN_PROP_I_ASPECT| \
102 CLIENTWIN_PROP_I_RSZINC)
105 #define DO_SZH(NAME, FLAG, IFLAG, SZHFLAG, W, H, C) \
106 if(extl_table_is_bool_set(tab, "ignore_" NAME)){ \
107 cwin->flags|=IFLAG; \
108 }else if(extl_table_gets_t(tab, NAME, &tab2)){ \
109 if(extl_table_gets_i(tab2, "w", &i1) && \
110 extl_table_gets_i(tab2, "h", &i2)){ \
111 cwin->size_hints.W=i1; \
112 cwin->size_hints.H=i2; \
114 cwin->size_hints.flags|=SZHFLAG; \
117 extl_unref_table(tab2); \
121 static void clientwin_get_winprops(WClientWin *cwin)
127 tab=ioncore_get_winprop(cwin);
131 if(tab==extl_table_none())
134 if(extl_table_is_bool_set(tab, "transparent"))
135 cwin->flags|=CLIENTWIN_PROP_TRANSPARENT;
137 if(extl_table_is_bool_set(tab, "acrobatic"))
138 cwin->flags|=CLIENTWIN_PROP_ACROBATIC;
140 DO_SZH("max_size", CLIENTWIN_PROP_MAXSIZE, CLIENTWIN_PROP_I_MAXSIZE,
141 PMaxSize, max_width, max_height, );
143 DO_SZH("min_size", CLIENTWIN_PROP_MINSIZE, CLIENTWIN_PROP_I_MINSIZE,
144 PMinSize, min_width, min_height, );
146 DO_SZH("resizeinc", CLIENTWIN_PROP_RSZINC, CLIENTWIN_PROP_I_RSZINC,
147 PResizeInc, width_inc, height_inc, );
149 DO_SZH("aspect", CLIENTWIN_PROP_ASPECT, CLIENTWIN_PROP_I_ASPECT,
150 PAspect, min_aspect.x, min_aspect.y,
151 { cwin->size_hints.max_aspect.x=i1;
152 cwin->size_hints.max_aspect.y=i2;
155 if(extl_table_is_bool_set(tab, "ignore_cfgrq"))
156 cwin->flags|=CLIENTWIN_PROP_IGNORE_CFGRQ;
158 if(extl_table_gets_s(tab, "orientation", &s)){
159 if(strcmp(s, "vertical")==0)
160 cwin->flags|=CLIENTWIN_PROP_O_VERT;
161 else if(strcmp(s, "horizontal")==0)
162 cwin->flags|=CLIENTWIN_PROP_O_HORIZ;
168 void clientwin_get_size_hints(WClientWin *cwin)
170 XSizeHints tmp=cwin->size_hints;
172 xwindow_get_sizehints(cwin->win, &(cwin->size_hints));
174 if(cwin->flags&CLIENTWIN_PROP_I_MAXSIZE){
175 cwin->size_hints.flags&=~PMaxSize;
176 }else if(cwin->flags&CLIENTWIN_PROP_MAXSIZE){
177 cwin->size_hints.max_width=tmp.max_width;
178 cwin->size_hints.max_height=tmp.max_height;
179 cwin->size_hints.flags|=PMaxSize;
182 if(cwin->flags&CLIENTWIN_PROP_I_MINSIZE){
183 cwin->size_hints.flags&=~PMinSize;
184 }else if(cwin->flags&CLIENTWIN_PROP_MINSIZE){
185 cwin->size_hints.min_width=tmp.min_width;
186 cwin->size_hints.min_height=tmp.min_height;
187 cwin->size_hints.flags|=PMinSize;
190 if(cwin->flags&CLIENTWIN_PROP_I_ASPECT){
191 cwin->size_hints.flags&=~PAspect;
192 }else if(cwin->flags&CLIENTWIN_PROP_ASPECT){
193 cwin->size_hints.min_aspect=tmp.min_aspect;
194 cwin->size_hints.max_aspect=tmp.max_aspect;
195 cwin->size_hints.flags|=PAspect;
198 if(cwin->flags&CLIENTWIN_PROP_I_RSZINC){
199 cwin->size_hints.flags&=~PResizeInc;
200 }else if(cwin->flags&CLIENTWIN_PROP_RSZINC){
201 cwin->size_hints.width_inc=tmp.width_inc;
202 cwin->size_hints.height_inc=tmp.height_inc;
203 cwin->size_hints.flags|=PResizeInc;
208 void clientwin_get_set_name(WClientWin *cwin)
214 list=netwm_get_name(cwin);
217 list=xwindow_get_text_property(cwin->win, XA_WM_NAME, &n);
219 cwin->flags|=CLIENTWIN_USE_NET_WM_NAME;
223 /* Special condition kludge: property exists, but couldn't
224 * be converted to a string list.
226 clientwin_set_name(cwin, (n==-1 ? "???" : NULL));
228 clientwin_set_name(cwin, *list);
229 XFreeStringList(list);
234 /* Some standard winprops */
237 bool clientwin_get_switchto(const WClientWin *cwin)
241 if(ioncore_g.opmode==IONCORE_OPMODE_INIT)
244 if(extl_table_gets_b(cwin->proptab, "switchto", &b))
247 return ioncore_g.switchto_new;
251 int clientwin_get_transient_mode(const WClientWin *cwin)
254 int mode=TRANSIENT_MODE_NORMAL;
256 if(extl_table_gets_s(cwin->proptab, "transient_mode", &s)){
257 if(strcmp(s, "current")==0)
258 mode=TRANSIENT_MODE_CURRENT;
259 else if(strcmp(s, "off")==0)
260 mode=TRANSIENT_MODE_OFF;
270 /*{{{ Manage/create */
273 static void configure_cwin_bw(Window win, int bw)
276 ulong wcmask=CWBorderWidth;
279 XConfigureWindow(ioncore_g.dpy, win, wcmask, &wc);
283 static void set_sane_gravity(Window win)
285 XSetWindowAttributes attr;
287 attr.win_gravity=NorthWestGravity;
289 XChangeWindowAttributes(ioncore_g.dpy, win,
290 CWWinGravity, &attr);
294 static bool clientwin_init(WClientWin *cwin, WWindow *par, Window win,
295 XWindowAttributes *attr)
301 cwin->state=WithdrawnState;
307 fp.mode=REGION_FIT_EXACT;
309 /* The idiot who invented special server-supported window borders that
310 * are not accounted for in the window size should be "taken behind a
313 cwin->orig_bw=attr->border_width;
314 configure_cwin_bw(cwin->win, 0);
315 if(cwin->orig_bw!=0 && cwin->size_hints.flags&PWinGravity){
316 fp.g.x+=xgravity_deltax(cwin->size_hints.win_gravity,
317 -cwin->orig_bw, -cwin->orig_bw);
318 fp.g.y+=xgravity_deltay(cwin->size_hints.win_gravity,
319 -cwin->orig_bw, -cwin->orig_bw);
322 set_sane_gravity(cwin->win);
325 cwin->cmap=attr->colormap;
329 cwin->event_mask=IONCORE_EVENTMASK_CLIENTWIN;
331 region_init(&(cwin->region), par, &fp);
333 cwin->region.flags|=REGION_GRAB_ON_PARENT;
334 region_add_bindmap(&cwin->region, ioncore_clientwin_bindmap);
336 XSelectInput(ioncore_g.dpy, win, cwin->event_mask);
338 clientwin_register(cwin);
339 clientwin_get_set_name(cwin);
340 clientwin_get_colormaps(cwin);
341 clientwin_get_protocols(cwin);
342 clientwin_get_winprops(cwin);
343 clientwin_get_size_hints(cwin);
345 XSaveContext(ioncore_g.dpy, win, ioncore_g.win_context, (XPointer)cwin);
346 XAddToSaveSet(ioncore_g.dpy, win);
352 static WClientWin *create_clientwin(WWindow *par, Window win,
353 XWindowAttributes *attr)
355 CREATEOBJ_IMPL(WClientWin, clientwin, (p, par, win, attr));
360 WClientWin *clientwin_get_transient_for(const WClientWin *cwin)
363 WClientWin *tfor=NULL;
365 if(clientwin_get_transient_mode(cwin)!=TRANSIENT_MODE_NORMAL)
368 if(!XGetTransientForHint(ioncore_g.dpy, cwin->win, &tforwin))
374 tfor=XWINDOW_REGION_OF_T(tforwin, WClientWin);
377 warn(TR("The transient_for hint for \"%s\" points to itself."),
378 region_name((WRegion*)cwin));
379 }else if(tfor==NULL){
380 if(xwindow_region_of(tforwin)!=NULL){
381 warn(TR("Client window \"%s\" has broken transient_for hint. "
382 "(\"Extended WM hints\" multi-parent brain damage?)"),
383 region_name((WRegion*)cwin));
385 }else if(!region_same_rootwin((WRegion*)cwin, (WRegion*)tfor)){
386 warn(TR("The transient_for window for \"%s\" is not on the same "
387 "screen."), region_name((WRegion*)cwin));
396 static bool postmanage_check(WClientWin *cwin, XWindowAttributes *attr)
398 /* Check that the window exists. The previous check and selectinput
399 * do not seem to catch all cases of window destroyal.
401 XSync(ioncore_g.dpy, False);
403 if(XGetWindowAttributes(ioncore_g.dpy, cwin->win, attr))
406 warn(TR("Window %#x disappeared."), cwin->win);
412 static bool do_manage_mrsh(bool (*fn)(WClientWin *cwin, WManageParams *pm),
415 return fn((WClientWin*)p[0], (WManageParams*)p[1]);
420 static bool do_manage_mrsh_extl(ExtlFn fn, void **p)
422 WClientWin *cwin=(WClientWin*)p[0];
423 WManageParams *mp=(WManageParams*)p[1];
424 ExtlTab t=manageparams_to_table(mp);
427 extl_call(fn, "ot", "b", cwin, t, &ret);
431 return (ret && REGION_MANAGER(cwin)!=NULL);
435 /* This is called when a window is mapped on the root window.
436 * We want to check if we should manage the window and how and
439 WClientWin* ioncore_manage_clientwin(Window win, bool maprq)
442 WClientWin *cwin=NULL;
443 XWindowAttributes attr;
445 int init_state=NormalState;
446 WManageParams param=MANAGEPARAMS_INIT;
451 /* Is the window already being managed? */
452 cwin=XWINDOW_REGION_OF_T(win, WClientWin);
456 /* Select for UnmapNotify and DestroyNotify as the
457 * window might get destroyed or unmapped in the meanwhile.
459 xwindow_unmanaged_selectinput(win, StructureNotifyMask);
461 if(!XGetWindowAttributes(ioncore_g.dpy, win, &attr)){
463 warn(TR("Window %#x disappeared."), win);
469 hints=XGetWMHints(ioncore_g.dpy, win);
472 if(hints->flags&StateHint)
473 init_state=hints->initial_state;
475 if(!param.dockapp && init_state==WithdrawnState &&
476 hints->flags&IconWindowHint && hints->icon_window!=None){
477 Window icon_win=hints->icon_window;
478 XWindowAttributes icon_attr;
480 if(!XGetWindowAttributes(ioncore_g.dpy, icon_win, &icon_attr)){
482 warn(TR("Window %#x disappeared."), win);
488 if(attr.map_state==IsViewable){
489 /* The dockapp might be displaying its "main" window if no
490 * wm that understands dockapps has been managing it.
492 XUnmapWindow(ioncore_g.dpy, win);
495 /* Main window is unmapped on initial scan, but icon window
496 * is mapped. Let's hope it's a dockapp left by e.g. us.
498 if(icon_attr.map_state==IsViewable)
509 xwindow_unmanaged_selectinput(win, 0);
510 xwindow_unmanaged_selectinput(icon_win, StructureNotifyMask);
515 /* Copy WM_CLASS as _ION_DOCKAPP_HACK */
517 p=xwindow_get_text_property(win, XA_WM_CLASS, &n);
520 xwindow_set_text_property(icon_win, ioncore_g.atom_dockapp_hack,
521 (const char **)p, n);
524 const char *pdummy[2]={"unknowndockapp", "UnknownDockapp"};
525 xwindow_set_text_property(icon_win, ioncore_g.atom_dockapp_hack,
534 /* Do we really want to manage it? */
535 if(!param.dockapp && (attr.override_redirect ||
536 (!maprq && attr.map_state!=IsViewable))){
540 attr.width=maxof(attr.width, 1);
541 attr.height=maxof(attr.height, 1);
543 /* Find root window */
544 FOR_ALL_ROOTWINS(rootwin){
545 if(WROOTWIN_ROOT(rootwin)==attr.root)
550 warn(TR("Unable to find a matching root window!"));
554 /* Allocate and initialize */
555 cwin=create_clientwin((WWindow*)rootwin, win, &attr);
562 param.geom=REGION_GEOM(cwin);
564 param.switchto=(init_state!=IconicState && clientwin_get_switchto(cwin));
565 param.jumpto=extl_table_is_bool_set(cwin->proptab, "jumpto");
566 param.gravity=(cwin->size_hints.flags&PWinGravity
567 ? cwin->size_hints.win_gravity
569 param.tfor=clientwin_get_transient_for(cwin);
571 if(!extl_table_gets_b(cwin->proptab, "userpos", ¶m.userpos))
572 param.userpos=(cwin->size_hints.flags&USPosition);
574 if(cwin->flags&SIZEHINT_PROPS){
575 /* If size hints have been messed with, readjust requested geometry
576 * here. If programs themselves give incompatible geometries and
577 * things don't look good then, it's their fault.
579 region_size_hints_correct((WRegion*)cwin, ¶m.geom.w, ¶m.geom.h,
586 if(!hook_call_alt(clientwin_do_manage_alt, &mrshpm,
587 (WHookMarshall*)do_manage_mrsh,
588 (WHookMarshallExtl*)do_manage_mrsh_extl)){
589 warn(TR("Unable to manage client window %#x."), win);
593 if(ioncore_g.opmode==IONCORE_OPMODE_NORMAL &&
594 !region_is_fully_mapped((WRegion*)cwin) &&
595 !region_skip_focus((WRegion*)cwin)){
596 region_set_activity((WRegion*)cwin, SETPARAM_SET);
599 if(postmanage_check(cwin, &attr)){
600 if(param.jumpto && ioncore_g.focus_next==NULL)
601 region_goto((WRegion*)cwin);
602 hook_call_o(clientwin_mapped_hook, (Obj*)cwin);
607 clientwin_destroyed(cwin);
611 xwindow_unmanaged_selectinput(win, 0);
616 void clientwin_tfor_changed(WClientWin *cwin)
619 WManageParams param=MANAGEPARAMS_INIT;
620 bool succeeded=FALSE;
621 param.tfor=clientwin_get_transient_for(cwin);
625 region_rootpos((WRegion*)cwin, &(param.geom.x), &(param.geom.y));
626 param.geom.w=REGION_GEOM(cwin).w;
627 param.geom.h=REGION_GEOM(cwin).h;
630 param.switchto=region_may_control_focus((WRegion*)cwin);
631 param.jumpto=extl_table_is_bool_set(cwin->proptab, "jumpto");
632 param.gravity=ForgetGravity;
634 CALL_ALT_B(succeeded, clientwin_do_manage_alt, (cwin, ¶m));
635 warn("WM_TRANSIENT_FOR changed for \"%s\".",
636 region_name((WRegion*)cwin));
638 warn(TR("Changes is WM_TRANSIENT_FOR property are unsupported."));
646 /*{{{ Unmanage/destroy */
649 static bool reparent_root(WClientWin *cwin)
651 XWindowAttributes attr;
656 if(!XGetWindowAttributes(ioncore_g.dpy, cwin->win, &attr))
659 par=REGION_PARENT(cwin);
662 x=REGION_GEOM(cwin).x;
663 y=REGION_GEOM(cwin).y;
665 int dr=REGION_GEOM(par).w-REGION_GEOM(cwin).w-REGION_GEOM(cwin).x;
666 int db=REGION_GEOM(par).h-REGION_GEOM(cwin).h-REGION_GEOM(cwin).y;
670 XTranslateCoordinates(ioncore_g.dpy, par->win, attr.root, 0, 0,
673 x-=xgravity_deltax(cwin->size_hints.win_gravity,
674 maxof(0, REGION_GEOM(cwin).x), dr);
675 y-=xgravity_deltay(cwin->size_hints.win_gravity,
676 maxof(0, REGION_GEOM(cwin).y), db);
679 XReparentWindow(ioncore_g.dpy, cwin->win, attr.root, x, y);
685 void clientwin_deinit(WClientWin *cwin)
690 region_pointer_focus_hack(&cwin->region);
692 xwindow_unmanaged_selectinput(cwin->win, 0);
693 XUnmapWindow(ioncore_g.dpy, cwin->win);
696 configure_cwin_bw(cwin->win, cwin->orig_bw);
698 if(reparent_root(cwin)){
699 if(ioncore_g.opmode==IONCORE_OPMODE_DEINIT){
700 XMapWindow(ioncore_g.dpy, cwin->win);
701 /* Make sure the topmost window has focus; it doesn't really
702 * matter which one has as long as some has.
704 xwindow_do_set_focus(cwin->win);
706 set_clientwin_state(cwin, WithdrawnState);
707 netwm_delete_state(cwin);
711 XRemoveFromSaveSet(ioncore_g.dpy, cwin->win);
712 XDeleteContext(ioncore_g.dpy, cwin->win, ioncore_g.win_context);
715 clientwin_clear_colormaps(cwin);
717 region_deinit((WRegion*)cwin);
722 static bool mrsh_u_c(WHookDummy *fn, void *param)
728 static bool mrsh_u_extl(ExtlFn fn, void *param)
730 double d=*(Window*)param;
731 extl_call(fn, "d", NULL, d);
735 static void clientwin_do_unmapped(WClientWin *cwin, Window win)
737 cwin->flags|=CLIENTWIN_UNMAP_RQ;
739 /* First try a graceful chain-dispose */
740 if(!region_rqdispose((WRegion*)cwin)){
741 /* But force dispose anyway */
742 region_dispose((WRegion*)cwin);
745 hook_call(clientwin_unmapped_hook, &win, mrsh_u_c, mrsh_u_extl);
748 /* Used when the window was unmapped */
749 void clientwin_unmapped(WClientWin *cwin)
751 clientwin_do_unmapped(cwin, cwin->win);
755 /* Used when the window was deastroyed */
756 void clientwin_destroyed(WClientWin *cwin)
758 Window win=cwin->win;
759 XRemoveFromSaveSet(ioncore_g.dpy, cwin->win);
760 XDeleteContext(ioncore_g.dpy, cwin->win, ioncore_g.win_context);
761 xwindow_unmanaged_selectinput(cwin->win, 0);
763 clientwin_do_unmapped(cwin, win);
773 static bool send_clientmsg(Window win, Atom a, Time stmp)
775 XClientMessageEvent ev;
777 ev.type=ClientMessage;
779 ev.message_type=ioncore_g.atom_wm_protocols;
784 return (XSendEvent(ioncore_g.dpy, win, False, 0L, (XEvent*)&ev)!=0);
789 * Attempt to kill (with \code{XKillWindow}) the client that owns
790 * the X window correspoding to \var{cwin}.
793 void clientwin_kill(WClientWin *cwin)
795 XKillClient(ioncore_g.dpy, cwin->win);
799 void clientwin_rqclose(WClientWin *cwin, bool relocate_ignored)
801 /* Ignore relocate parameter -- client windows can always be
802 * destroyed by the application in any case, so way may just as
803 * well assume relocate is always set.
806 if(cwin->flags&CLIENTWIN_P_WM_DELETE){
807 send_clientmsg(cwin->win, ioncore_g.atom_wm_delete,
808 ioncore_get_timestamp());
810 warn(TR("Client does not support the WM_DELETE protocol."));
818 /*{{{ State (hide/show) */
821 static void set_clientwin_state(WClientWin *cwin, int state)
823 if(cwin->state!=state){
825 xwindow_set_state_property(cwin->win, state);
830 static void hide_clientwin(WClientWin *cwin)
832 region_pointer_focus_hack(&cwin->region);
834 if(cwin->flags&CLIENTWIN_PROP_ACROBATIC){
835 XMoveWindow(ioncore_g.dpy, cwin->win,
836 -2*REGION_GEOM(cwin).w, -2*REGION_GEOM(cwin).h);
840 set_clientwin_state(cwin, IconicState);
841 XSelectInput(ioncore_g.dpy, cwin->win,
842 cwin->event_mask&~(StructureNotifyMask|EnterWindowMask));
843 XUnmapWindow(ioncore_g.dpy, cwin->win);
844 XSelectInput(ioncore_g.dpy, cwin->win, cwin->event_mask);
848 static void show_clientwin(WClientWin *cwin)
850 if(cwin->flags&CLIENTWIN_PROP_ACROBATIC){
851 XMoveWindow(ioncore_g.dpy, cwin->win,
852 REGION_GEOM(cwin).x, REGION_GEOM(cwin).y);
853 if(cwin->state==NormalState)
857 XSelectInput(ioncore_g.dpy, cwin->win,
858 cwin->event_mask&~(StructureNotifyMask|EnterWindowMask));
859 XMapWindow(ioncore_g.dpy, cwin->win);
860 XSelectInput(ioncore_g.dpy, cwin->win, cwin->event_mask);
861 set_clientwin_state(cwin, NormalState);
868 /*{{{ Resize/reparent/reconf helpers */
871 void clientwin_notify_rootpos(WClientWin *cwin, int rootx, int rooty)
881 ce.xconfigure.type=ConfigureNotify;
882 ce.xconfigure.event=win;
883 ce.xconfigure.window=win;
884 ce.xconfigure.x=rootx-cwin->orig_bw;
885 ce.xconfigure.y=rooty-cwin->orig_bw;
886 ce.xconfigure.width=REGION_GEOM(cwin).w;
887 ce.xconfigure.height=REGION_GEOM(cwin).h;
888 ce.xconfigure.border_width=cwin->orig_bw;
889 ce.xconfigure.above=None;
890 ce.xconfigure.override_redirect=False;
892 XSelectInput(ioncore_g.dpy, win, cwin->event_mask&~StructureNotifyMask);
893 XSendEvent(ioncore_g.dpy, win, False, StructureNotifyMask, &ce);
894 XSelectInput(ioncore_g.dpy, win, cwin->event_mask);
898 static void sendconfig_clientwin(WClientWin *cwin)
902 region_rootpos(&cwin->region, &rootx, &rooty);
903 clientwin_notify_rootpos(cwin, rootx, rooty);
907 static void do_reparent_clientwin(WClientWin *cwin, Window win, int x, int y)
909 XSelectInput(ioncore_g.dpy, cwin->win,
910 cwin->event_mask&~StructureNotifyMask);
911 XReparentWindow(ioncore_g.dpy, cwin->win, win, x, y);
912 XSelectInput(ioncore_g.dpy, cwin->win, cwin->event_mask);
916 static void convert_geom(const WFitParams *fp,
917 WClientWin *cwin, WRectangle *geom)
919 WFitParams fptmp=*fp;
920 WSizePolicy szplcy=SIZEPOLICY_FULL_EXACT;
922 /*if(cwin->szplcy!=SIZEPOLICY_DEFAULT)
923 szplcy=cwin->szplcy;*/
925 sizepolicy(&szplcy, (WRegion*)cwin, NULL, REGION_RQGEOM_WEAK_ALL, &fptmp);
934 /*{{{ Region dynfuns */
937 static bool clientwin_fitrep(WClientWin *cwin, WWindow *np,
938 const WFitParams *fp)
944 if(np!=NULL && !region_same_rootwin((WRegion*)cwin, (WRegion*)np))
947 if(fp->mode®ION_FIT_WHATEVER){
950 geom.w=REGION_GEOM(cwin).w;
951 geom.h=REGION_GEOM(cwin).h;
956 changes=(REGION_GEOM(cwin).x!=geom.x ||
957 REGION_GEOM(cwin).y!=geom.y ||
958 REGION_GEOM(cwin).w!=geom.w ||
959 REGION_GEOM(cwin).h!=geom.h);
961 REGION_GEOM(cwin)=geom;
963 if(np==NULL && !changes)
967 region_unset_parent((WRegion*)cwin);
968 do_reparent_clientwin(cwin, np->win, geom.x, geom.y);
969 region_set_parent((WRegion*)cwin, np);
970 sendconfig_clientwin(cwin);
972 if(!REGION_IS_FULLSCREEN(cwin))
973 cwin->flags&=~CLIENTWIN_FS_RQ;
975 netwm_update_state(cwin);
981 if(cwin->flags&CLIENTWIN_PROP_ACROBATIC && !REGION_IS_MAPPED(cwin)){
982 XMoveResizeWindow(ioncore_g.dpy, cwin->win,
983 -2*REGION_GEOM(cwin).w, -2*REGION_GEOM(cwin).h,
986 XMoveResizeWindow(ioncore_g.dpy, cwin->win, geom.x, geom.y, w, h);
989 cwin->flags&=~CLIENTWIN_NEED_CFGNTFY;
995 static void clientwin_map(WClientWin *cwin)
997 show_clientwin(cwin);
998 REGION_MARK_MAPPED(cwin);
1002 static void clientwin_unmap(WClientWin *cwin)
1004 hide_clientwin(cwin);
1005 REGION_MARK_UNMAPPED(cwin);
1009 static void clientwin_do_set_focus(WClientWin *cwin, bool warp)
1011 if(cwin->flags&CLIENTWIN_P_WM_TAKE_FOCUS){
1012 Time stmp=ioncore_get_timestamp();
1013 send_clientmsg(cwin->win, ioncore_g.atom_wm_take_focus, stmp);
1016 region_finalise_focusing((WRegion*)cwin, cwin->win, warp);
1018 XSync(ioncore_g.dpy, 0);
1022 void clientwin_restack(WClientWin *cwin, Window other, int mode)
1024 xwindow_restack(cwin->win, other, mode);
1028 void clientwin_stacking(WClientWin *cwin, Window *bottomret, Window *topret)
1030 *bottomret=cwin->win;
1035 static Window clientwin_x_window(WClientWin *cwin)
1041 static void clientwin_activated(WClientWin *cwin)
1043 clientwin_install_colormap(cwin);
1047 static void clientwin_size_hints(WClientWin *cwin, WSizeHints *hints_ret)
1049 if(cwin->flags&CLIENTWIN_FS_RQ){
1050 /* Do not use size hints, when full screen mode has been
1051 * requested by the client window itself.
1053 sizehints_clear(hints_ret);
1055 xsizehints_to_sizehints(&cwin->size_hints, hints_ret);
1060 static int clientwin_orientation(WClientWin *cwin)
1062 return (cwin->flags&CLIENTWIN_PROP_O_VERT
1063 ? REGION_ORIENTATION_VERTICAL
1064 : (cwin->flags&CLIENTWIN_PROP_O_HORIZ
1065 ? REGION_ORIENTATION_HORIZONTAL
1066 : REGION_ORIENTATION_NONE));
1073 /*{{{ Identity & lookup */
1077 * Returns a table containing the properties \code{WM_CLASS} (table entries
1078 * \var{instance} and \var{class}) and \code{WM_WINDOW_ROLE} (\var{role})
1079 * properties for \var{cwin}. If a property is not set, the corresponding
1080 * field(s) are unset in the table.
1084 ExtlTab clientwin_get_ident(WClientWin *cwin)
1086 char **p=NULL, **p2=NULL, *wrole=NULL;
1087 int n=0, n2=0, n3=0, tmp=0;
1088 Window tforwin=None;
1090 bool dockapp_hack=FALSE;
1092 p=xwindow_get_text_property(cwin->win, XA_WM_CLASS, &n);
1094 p2=xwindow_get_text_property(cwin->win, ioncore_g.atom_dockapp_hack, &n2);
1096 dockapp_hack=(n2>0);
1099 /* Some dockapps do actually have WM_CLASS, so use it. */
1105 wrole=xwindow_get_string_property(cwin->win, ioncore_g.atom_wm_window_role,
1108 tab=extl_create_table();
1109 if(n>=2 && p[1]!=NULL)
1110 extl_table_sets_s(tab, "class", p[1]);
1111 if(n>=1 && p[0]!=NULL)
1112 extl_table_sets_s(tab, "instance", p[0]);
1114 extl_table_sets_s(tab, "role", wrole);
1116 if(XGetTransientForHint(ioncore_g.dpy, cwin->win, &tforwin)
1118 extl_table_sets_b(tab, "is_transient", TRUE);
1122 extl_table_sets_b(tab, "is_dockapp", TRUE);
1127 XFreeStringList(p2);
1138 /*{{{ ConfigureRequest */
1141 static bool check_fs_cfgrq(WClientWin *cwin, XConfigureRequestEvent *ev)
1143 /* check full screen request */
1144 if((ev->value_mask&(CWWidth|CWHeight))==(CWWidth|CWHeight)){
1145 WRegion *grp=region_groupleader_of((WRegion*)cwin);
1146 WScreen *scr=clientwin_fullscreen_chkrq(cwin, ev->width, ev->height);
1148 if(scr!=NULL && REGION_MANAGER(grp)!=(WRegion*)scr){
1149 bool sw=clientwin_fullscreen_may_switchto(cwin);
1151 cwin->flags|=CLIENTWIN_FS_RQ;
1153 if(!region_fullscreen_scr(grp, scr, sw))
1154 cwin->flags&=~CLIENTWIN_FS_RQ;
1164 static bool check_normal_cfgrq(WClientWin *cwin, XConfigureRequestEvent *ev)
1166 if(ev->value_mask&(CWX|CWY|CWWidth|CWHeight)){
1167 WRQGeomParams rq=RQGEOMPARAMS_INIT;
1170 rq.flags=REGION_RQGEOM_WEAK_ALL|REGION_RQGEOM_ABSOLUTE;
1172 if(cwin->size_hints.flags&PWinGravity){
1173 rq.flags|=REGION_RQGEOM_GRAVITY;
1174 rq.gravity=cwin->size_hints.win_gravity;
1177 /* Do I need to insert another disparaging comment on the person who
1178 * invented special server-supported window borders that are not
1179 * accounted for in the window size? Keep it simple, stupid!
1181 if(cwin->size_hints.flags&PWinGravity){
1182 gdx=xgravity_deltax(cwin->size_hints.win_gravity,
1183 -cwin->orig_bw, -cwin->orig_bw);
1184 gdy=xgravity_deltay(cwin->size_hints.win_gravity,
1185 -cwin->orig_bw, -cwin->orig_bw);
1188 region_rootpos((WRegion*)cwin, &(rq.geom.x), &(rq.geom.y));
1189 rq.geom.w=REGION_GEOM(cwin).w;
1190 rq.geom.h=REGION_GEOM(cwin).h;
1192 if(ev->value_mask&CWWidth){
1193 /* If x was not changed, keep reference point where it was */
1194 if(cwin->size_hints.flags&PWinGravity){
1195 rq.geom.x+=xgravity_deltax(cwin->size_hints.win_gravity, 0,
1196 ev->width-rq.geom.w);
1198 rq.geom.w=maxof(ev->width, 1);
1199 rq.flags&=~REGION_RQGEOM_WEAK_W;
1201 if(ev->value_mask&CWHeight){
1202 /* If y was not changed, keep reference point where it was */
1203 if(cwin->size_hints.flags&PWinGravity){
1204 rq.geom.y+=xgravity_deltay(cwin->size_hints.win_gravity, 0,
1205 ev->height-rq.geom.h);
1207 rq.geom.h=maxof(ev->height, 1);
1208 rq.flags&=~REGION_RQGEOM_WEAK_H;
1210 if(ev->value_mask&CWX){
1211 rq.geom.x=ev->x+gdx;
1212 rq.flags&=~REGION_RQGEOM_WEAK_X;
1214 if(ev->value_mask&CWY){
1215 rq.geom.y=ev->y+gdy;
1216 rq.flags&=~REGION_RQGEOM_WEAK_Y;
1219 region_rqgeom((WRegion*)cwin, &rq, NULL);
1228 void clientwin_handle_configure_request(WClientWin *cwin,
1229 XConfigureRequestEvent *ev)
1231 if(ev->value_mask&CWBorderWidth)
1232 cwin->orig_bw=ev->border_width;
1234 cwin->flags|=CLIENTWIN_NEED_CFGNTFY;
1236 if(!(cwin->flags&CLIENTWIN_PROP_IGNORE_CFGRQ)){
1237 if(!check_fs_cfgrq(cwin, ev))
1238 check_normal_cfgrq(cwin, ev);
1241 if(cwin->flags&CLIENTWIN_NEED_CFGNTFY){
1242 sendconfig_clientwin(cwin);
1243 cwin->flags&=~CLIENTWIN_NEED_CFGNTFY;
1255 * Attempts to fix window size problems with non-ICCCM compliant
1259 void clientwin_nudge(WClientWin *cwin)
1261 XResizeWindow(ioncore_g.dpy, cwin->win,
1262 2*REGION_GEOM(cwin).w, 2*REGION_GEOM(cwin).h);
1263 XFlush(ioncore_g.dpy);
1264 XResizeWindow(ioncore_g.dpy, cwin->win,
1265 REGION_GEOM(cwin).w, REGION_GEOM(cwin).h);
1276 * Return the X window id for the client window.
1280 double clientwin_xid(WClientWin *cwin)
1292 static int last_checkcode=1;
1295 static ExtlTab clientwin_get_configuration(WClientWin *cwin)
1299 SMCfgCallback *cfg_cb;
1300 SMAddCallback *add_cb;
1302 tab=region_get_base_configuration((WRegion*)cwin);
1304 extl_table_sets_d(tab, "windowid", (double)(cwin->win));
1306 if(last_checkcode!=0){
1307 chkc=last_checkcode++;
1308 xwindow_set_integer_property(cwin->win, ioncore_g.atom_checkcode,
1310 extl_table_sets_i(tab, "checkcode", chkc);
1313 ioncore_get_sm_callbacks(&add_cb, &cfg_cb);
1322 static void do_sm(ExtlTab tab)
1324 SMAddCallback *add_cb;
1325 SMCfgCallback *cfg_cb;
1328 ioncore_get_sm_callbacks(&add_cb, &cfg_cb);
1331 ph=ioncore_get_load_pholder();
1334 if(!add_cb(ph, tab))
1335 destroy_obj((Obj*)ph);
1341 WRegion *clientwin_load(WWindow *par, const WFitParams *fp, ExtlTab tab)
1345 int chkc=0, real_chkc=0;
1346 WClientWin *cwin=NULL;
1347 XWindowAttributes attr;
1349 bool got_chkc=FALSE;
1351 if(!extl_table_gets_d(tab, "windowid", &wind) ||
1352 !extl_table_gets_i(tab, "checkcode", &chkc)){
1358 if(XWINDOW_REGION_OF(win)!=NULL){
1359 warn("Client window %x already managed.", win);
1363 got_chkc=xwindow_get_integer_property(win, ioncore_g.atom_checkcode,
1366 if(!got_chkc || real_chkc!=chkc){
1373 if(!XGetWindowAttributes(ioncore_g.dpy, win, &attr)){
1374 warn(TR("Window %#x disappeared."), win);
1378 if(attr.root!=region_root_of((WRegion*)par))
1381 if(attr.override_redirect ||
1382 (ioncore_g.opmode==IONCORE_OPMODE_INIT && attr.map_state!=IsViewable)){
1383 warn(TR("Saved client window does not want to be managed."));
1387 cwin=create_clientwin(par, win, &attr);
1392 /* Reparent and resize taking limits set by size hints into account */
1393 convert_geom(fp, cwin, &rg);
1394 REGION_GEOM(cwin)=rg;
1395 do_reparent_clientwin(cwin, par->win, rg.x, rg.y);
1396 XResizeWindow(ioncore_g.dpy, win, maxof(1, rg.w), maxof(1, rg.h));
1398 if(!postmanage_check(cwin, &attr)){
1399 clientwin_destroyed(cwin);
1403 return (WRegion*)cwin;
1410 /*{{{ Dynfuntab and class info */
1413 static DynFunTab clientwin_dynfuntab[]={
1414 {(DynFun*)region_fitrep,
1415 (DynFun*)clientwin_fitrep},
1423 {region_do_set_focus,
1424 clientwin_do_set_focus},
1426 {region_notify_rootpos,
1427 clientwin_notify_rootpos},
1433 clientwin_stacking},
1435 {(DynFun*)region_xwindow,
1436 (DynFun*)clientwin_x_window},
1439 clientwin_activated},
1442 clientwin_size_hints},
1444 {(DynFun*)region_orientation,
1445 (DynFun*)clientwin_orientation},
1447 {(DynFun*)region_rqclose,
1448 (DynFun*)clientwin_rqclose},
1450 {(DynFun*)region_get_configuration,
1451 (DynFun*)clientwin_get_configuration},
1458 IMPLCLASS(WClientWin, WRegion, clientwin_deinit, clientwin_dynfuntab);