%
% 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(CHECK_IMAGIC);\
	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);

CHECK_IMAGIC=Exec(echo 'P=`which convert 2> /dev/null|grep convert|grep -v no|wc -l|tr -d " "`;if [ "x$P" == "x1" ]; then true; else $(CfgDir)/Utils/message.sh "ERROR: ImageMagic is not installed"; fi'|bash -f -s);\
	Exec(echo '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'|bash -f -s);

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=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=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,echo 'if [ "x$(wm_rotate)" == "x-rotate 90" ]; then echo 0; else echo 1; fi;'|bash -f -s);\
	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,echo 'X=$(pr_x);if [ "x$(wm_rotate)" == "x-rotate 90" ]; then X=$(pr_y);fi;echo $X;'|bash -f -s);\
	Make(var,pos_y,echo 'Y=$(pr_y);if [ "x$(wm_rotate)" == "x-rotate 90" ]; then Y=`expr $(wm_dy) - $(pr_x)`;fi;echo $Y;'|bash -f -s);\
        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,echo 'X=$(pp_x);if [ "x$(wm_rotate)" == "x-rotate 90" ]; then X=$(pp_y);fi;echo $X;'|bash -f -s);\
        Make(var,pos_y,echo 'Y=$(pp_y);if [ "x$(wm_rotate)" == "x-rotate 90" ]; then Y=`expr $(wm_dy) - $(pp_x)`; fi;echo $Y;'|bash -f -s);\
        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 $HOME/.anyRemote/scr.png|cut -d ',' -f 2|tr -d ' '|cut -d 'x' -f 1);\
%	Make(var,im_y,file -b $HOME/.anyRemote/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 $HOME/.anyRemote/scr.png;echo $HOME/.anyRemote/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 $HOME/.anyRemote/scr.png;echo $HOME/.anyRemote/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,echo 'if [ "x$(wm_rotate)" == "x" ]; then echo "-rotate 90"; else echo ""; fi;'|bash -f -s);\
        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,echo 'XM=$(cur_tmp);OUT='0,0';if [ "x$(wm_rotate)" == "x-rotate 90" ]; then OUT="$XM,0"; fi;echo $OUT;'|bash -f -s);

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 $HOME/.anyRemote;xwininfo > xwi.tmp);Emulate(sleep,100000,mouse,1);\
	Make(var,wm_dx,sleep 1;cat $HOME/.anyRemote/xwi.tmp|grep "\-geometry"|tr -s ' '|cut -d ' ' -f 3|cut -d 'x' -f 1);\
        Make(var,wm_dy,cat $HOME/.anyRemote/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 $HOME/.anyRemote;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 $HOME/.anyRemote/scr.png);\
	Macro(GETIMAGEPROPS);


Update=Macro(SHOWWIN);
Close=Make(mode,default);
Back=Make(mode,default);

[ModeEnd]

[End]
