#!/bin/sh
# -*- ksh -*-

# Small time to wait before sending events
export SMALL1=0.1
# Time to wait before making the dump
export SMALL2=0.2
# X server number for Xvfb
export ZMW_XVFBN=9
export ZMW_XVFB=`hostname`:$ZMW_XVFBN.0

zmw_small_scale() {
SAVE_SMALL1=$SMALL1
SAVE_SMALL2=$SMALL2

SMALL1=`echo "$SMALL1 * $1" | bc`
SMALL2=`echo "$SMALL2 * $1" | bc`
}

zmw_small_restore() {
SMALL1=$ZMW_SMALL1
SMALL2=$ZMW_SMALL2
}

export KEY_A=37
export KEY_B=38
export KEY_LEFT=39
export KEY_CONTROL=40
export KEY_SLASH=41
export KEY_BACKSPACE=42
export KEY_F1=43
export KEY_PAGEUP=44
export KEY_UP=45
export KEY_ALT=46
export KEY_META=47
export KEY_SHIFT=48
export KEY_ESCAPE=49
export KEY_TAB=50

start_xvfb() {
  if true
  then
    # depth 32 does NOT work, the "xwd" command does not work correctly
    Xvfb :$ZMW_XVFBN -ac -screen 0 700x700x24 -noreset &
  else
    Xnest :$ZMW_XVFBN -ac -noreset &
  fi
  echo $! >xvfb.pid
  sleep 1
  xsetroot -display $ZMW_XVFB -solid white
  # Clear the keyboard
  xmodmap -display $ZMW_XVFB -pke | sed 's/= .*/=/' |
          xmodmap -display $ZMW_XVFB -
  # Define the keyboard
  xmodmap -display $ZMW_XVFB -e "keycode $KEY_A = a" \
   -e "keycode $KEY_B = b" \
   -e "keycode $KEY_SLASH = slash" \
   -e "keycode $KEY_LEFT = Left" \
   -e "keycode $KEY_BACKSPACE = BackSpace" \
   -e "keycode $KEY_CONTROL = Control_L" \
   -e "keycode $KEY_F1 = F1" \
   -e "keycode $KEY_PAGEUP = Prior" \
   -e "keycode $KEY_ALT = Alt_L" \
   -e "keycode $KEY_META = Meta_L" \
   -e "keycode $KEY_SHIFT = Shift_L" \
   -e "keycode $KEY_UP = Up" \
   -e "keycode $KEY_ESCAPE = Escape" \
   -e "keycode $KEY_TAB = Tab" \
   -e "clear Lock" \
   -e "clear Shift" \
   -e "clear Mod1" \
   -e "clear Mod2" \
   -e "clear Mod3" \
   -e "clear Mod4" \
   -e "clear Mod5" \
   -e "clear Control" \
   -e "add Shift = Shift_L" \
   -e "add Mod1 = Alt_L" \
   -e "add Mod2 = Meta_L" \
   -e "add Control = Control_L"
  fvwm2 -display $ZMW_XVFB -f `pwd`/.fvwm2rc &
}

stop_xvfb() {
  kill `cat xvfb.pid`
  rm xvfb.pid || true
}


zmw_send() {
    echo "$1,0,1001
0,6,$X,$Y,0,0,0,1002" | xnee --replay  2>/dev/null >/dev/null
}

zmw_move_cursor_to() {
        X="$1"
        Y="$2"
        zmw_send "0,6,$1,$2,0,0"
	zmw_sleep $SMALL1
        zmw_send "0,6,`expr $1 + 1`,$2,0,0"
	zmw_sleep $SMALL1
}

zmw_button_press() {
	zmw_send "0,4,0,0,1,0"
}

zmw_button_release() {
	zmw_send "0,5,0,0,1,0"
}

zmw_button_click() {
	zmw_button_press
	zmw_button_release
}

zmw_button2_press() {
	zmw_send "0,4,0,0,2,0"
}

zmw_button2_release() {
	zmw_send "0,5,0,0,2,0"
}

zmw_button2_click() {
	zmw_button2_press
	zmw_button2_release
}
zmw_key_press() {
	zmw_send "0,2,0,0,0,$1"
}

zmw_key_release() {
	zmw_send "0,3,0,0,0,$1"
}

zmw_key() {
    zmw_key_press "$1"
    zmw_key_release "$1"
}

zmw_sleep() {
	sleep $1 || sleep 1
}

zmw_dump_screen() {
	echo -n "$1"
	zmw_sleep $SMALL2
	xwd -root -screen |
	    xwdtopnm  |
	    pnmcrop -white >"xxx.$NAME.$1.ppm"
}

