%
% anyremote configuration file for basic window manager operations (Server-mode)
% prerequisites: xwd, ImageMagic
%

% STATUS stable
% TODO handle FIRE joystick button on Nokia's
% XTEST yes
% SOUND no
% ENV no

GuiAppName=Window manager
GuiAppBinary=true
GuiAppType=Custom
GuiAppDesc=Shows screen of PC on screen of the phone. Beware: Can cause java crash on some Nokia's and Samsung's
GuiAppProtocols=Server

[Protocol]=Server

(Init)=\
	Include($(CfgDir)/Utils/aliases-server.cfg);\
	Macro(TEST_IMAGEMAGIC);\
	Macro(TEST_XWD);\
	Macro(DISPLAYSIZE);\
	Make(var,wm_scale,by_value,15);\
	Make(var,wm_cx,by_value,120);\
	Make(var,wm_cy,by_value,160);\
	Make(var,wm_px,by_value,120);\
	Make(var,wm_py,by_value,160);\
	Make(var,wm_rotate,by_value,);

(Connect)=\
	Set(parameter,debug,on);\
    Set(status,Press 1 to see screenshot);\
	Set(icons,WinManager,\
        1,folder,2,fullscreen,3,default,\
        4,default,5,default,6,default,\
        7,default,8,default,9,default,\
        *,question,0,no,#,default);\
	Get(screen_size);

TEST_XWD=\
	Exec(P=`which xwd 2> /dev/null|grep xwd|grep -v no|wc -l|tr -d " "`;\
            if [ "x$P" = "x1" ]; then true; \
            else $(CfgDir)/Utils/message.sh "ERROR: xwd is not installed"; fi);

DISPLAYSIZE=\
	Make(var,dpy_tmp,xdpyinfo|grep dimensions|tr -s ' '|cut -f 3 -d ' ');\
	Make(var,wm_dx,echo $(dpy_tmp)|cut -f 1 -d 'x');\
    Make(var,wm_dy,echo $(dpy_tmp)|cut -f 2 -d 'x');

Cancel=\
    Make(exit);

* *=Set(text,replace,Help, Window manager.\n 1 - to show screenshot\nthen use joystick to move a cursor);

Back=\
    Set(list,close);Set(text,close);Set(image,close);Set(menu,replace,)

0=Make(exit);
1=Macro(SHOW);
2=Set(fullscreen,toggle);Get(screen_size);

% sure that SizeY will be second one
SizeX($$)=\
	Make(var,wm_cx,by_value,$(Index));\
	Make(var,wm_px,by_value,$(Index));
	
SizeY($$)=\
	Make(var,wm_cy,by_value,$(Index));\
	Make(var,wm_py,by_value,$(Index));\
	Macro(SWAP,if [ "x$(wm_rotate)" = "x-rotate 90" ]; then echo 0; else echo 1; fi;);\
	Macro(SETSCALE);

% sure that PressedY will be second one
PressedX($$)=\
	Make(var,pr_x,echo "$(Index) $(wm_scale)"|awk '{n=split($0,a);print int(a[1]*a[2]) }');

PressedY($$)=\
	Make(var,pr_y,echo "$(Index) $(wm_scale)"|awk '{n=split($0,a);print int(a[1]*a[2]) }');\
	Make(var,pos_x,X=$(pr_x);if [ "x$(wm_rotate)" = "x-rotate 90" ]; then X=$(pr_y);fi;echo $X);\
	Make(var,pos_y,Y=$(pr_y);if [ "x$(wm_rotate)" = "x-rotate 90" ]; then Y=`expr $(wm_dy) - $(pr_x)`;fi;echo $Y);\
    Emulate(mousemove,$(pos_x),$(pos_y),sleep,200000,mouse,1);\
    Macro(SHOW2);

PosX($$)=\
	Make(var,pp_x,echo "$(Index) $(wm_scale)"|awk '{n=split($0,a);print int(a[1]*a[2]) }');

PosY($$)=\
	Make(var,pp_y,echo "$(Index) $(wm_scale)"|awk '{n=split($0,a);print int(a[1]*a[2]) }');\
	Make(var,pos_x,X=$(pp_x);if [ "x$(wm_rotate)" = "x-rotate 90" ]; then X=$(pp_y);fi;echo $X);\
    Make(var,pos_y,Y=$(pp_y);if [ "x$(wm_rotate)" = "x-rotate 90" ]; then Y=`expr $(wm_dy) - $(pp_x)`; fi;echo $Y);\
    Emulate(mousemove,$(pos_x),$(pos_y));

SWAP=\
	Make(var,tmp,by_value,$(wm_cx));\
	Make(var,wm_cx,by_value,$(wm_cy));\
	Make(var,wm_cy,by_value,$(tmp));

WINMENU=\
	Set(menu,replace,Zoom,Close Window,Update,Rotate,Center Cursor,Cursor to UL,Dynamic);

%GETIMAGEPROPS=\
%	Make(var,im_x,file -b $(TmpDir)/scr.png|cut -d ',' -f 2|tr -d ' '|cut -d 'x' -f 1);\
%	Make(var,im_y,file -b $(TmpDir)/scr.png|cut -d ',' -f 2|tr -d ' '|cut -d 'x' -f 2);

SETSCALE=\
	Make(var,wm_scale,echo "$(wm_cx) $(wm_cy) $(wm_dx) $(wm_dy)"|\
                    awk '{n=split($0,a);r1=a[3]/a[1];r2=a[4]/a[2];if(r2>r1) r1=r2; z=sprintf("%f",r1);print z;}');

SHOW=\
	ExecAndSet(image,window,xwd -silent -root|\
            convert xwd:- -resize $(wm_cx)x$(wm_cy) $(wm_rotate) -depth 8 $(TmpDir)/scr.png;echo $(TmpDir)/scr.png);\
	Macro(WINMENU);

SHOW2=\
	ExecAndSet(image,window,sleep 1;xwd -silent -root|\
                convert xwd:- -resize $(wm_cx)x$(wm_cy) $(wm_rotate) -depth 8 $(TmpDir)/scr.png;echo $(TmpDir)/scr.png);\
	Macro(WINMENU);

Close Window=\
	Emulate(keydown,Alt_L,key,F4,keyup,Alt_L);\
	Macro(SHOW2);
	
Update=\
	Macro(SHOW);

Rotate=\
	Make(var,wm_rotate,if [ "x$(wm_rotate)" = "x" ]; then echo "-rotate 90"; else echo ""; fi);\
    Macro(SWAP);\
    Macro(SETSCALE);\
    Macro(SHOW2);
	
Cursor to UL=\
	Emulate(mousemove,0,0);\
	Make(var,cur_tmp,echo "$(wm_dy) $(wm_scale)"|awk '{n=split($0,a);print int(a[1]/a[2]);}');\
	ExecAndSet(image,set_cursor,XM=$(cur_tmp);OUT='0,0';\
        if [ "x$(wm_rotate)" = "x-rotate 90" ]; then OUT="$XM,0"; fi;echo $OUT);

Center Cursor=\
	Make(var,ccx,echo $(wm_dx)|awk '{print $0/2;}');\
	Make(var,ccy,echo $(wm_dy)|awk '{print $0/2;}');\
	Emulate(mousemove,$(ccx),$(ccy));\
    ExecAndSet(image,set_cursor,echo "$(ccx) $(ccy) `echo $(wm_scale)|cut -f 3 -d ' '`"|\
        awk '{n=split($0,a);z=sprintf("%d,%d",a[1]/a[3],a[2]/a[3]);print z;}');

Dynamic=\
	Set(image,dynamic_cursor);\
	Set(menu,replace,Zoom,Close Window,Update,Rotate,Center Cursor,Cursor to UL,NonDynamic);

NonDynamic=\
	Set(image,cursor);\
	Set(menu,replace,Zoom,Close Window,Update,Rotate,Center Cursor,Cursor to UL,Dynamic);

Zoom=\
	Make(mode,Zoom);

[Mode]=Zoom

(EnterMode)=\
	Macro(WINSIZE);\
	Macro(SETSCALE);\
	Macro(SHOWWIN);\
	Set(menu,replace,Update,Close);\
	Set(image,nocursor);
	
(ExitMode)=\
	Macro(DISPLAYSIZE);\
	Macro(SETSCALE);\
	Macro(SHOW2);\
	Macro(WINMENU);\
	Set(image,cursor);

WINSIZE=\
	Exec(cd $(TmpDir);xwininfo > xwi.tmp);Emulate(sleep,100000,mouse,1);\
	Make(var,wm_dx,sleep 1;cat $(TmpDir)/xwi.tmp|grep "\-geometry"|tr -s ' '|cut -d ' ' -f 3|cut -d 'x' -f 1);\
    Make(var,wm_dy,cat $(TmpDir)/xwi.tmp|grep "\-geometry"|tr -s ' '|cut -d ' ' -f 3|cut -d 'x' -f 2|cut -d '-' -f 1);

SHOWWIN=\
	ExecAndSet(image,window,sleep 1;\
                    cd $(TmpDir);xwd -id `grep "Window id" xwi.tmp|\
                    cut -d ' ' -f 4` -silent|convert xwd:- -resize $(wm_cx)x$(wm_cy) $(wm_rotate) -depth 8 scr.png;\
                    echo $(TmpDir)/scr.png);\
	Macro(GETIMAGEPROPS);

Update=\
	Macro(SHOWWIN);

Close=\
	Make(mode,default);

Back=\
	Make(mode,default);

[ModeEnd]

[End]
