function initClient()
{
   exec("~/defaults.cs");
   exec("./common/canvas.cs");

   initCanvas("TAP -- RumbleJoystick Sample");
   
   // Load-up GUIs.
   exec("./RumbleJoystick.gui");
   Canvas.setContent(RumbleJoystick);
   Canvas.setCursor(DefaultCursor);

   $enableDirectInput = "1";
   activateDirectInput();
   enableJoystick();

   // Bind some global keypresses
   GlobalActionMap.bind(keyboard, "tilde", "toggleConsole");
   GlobalActionMap.bindCmd(keyboard, "alt enter", "", "toggleFullScreen();");
   GlobalActionMap.bindCmd(keyboard, "escape", "", "quit();");
   bindJoystickEvents();

   $leftMotor     = 0;
   $rightMotor    = 0;
   $rumbleLock    = false;
   $buttonPressed = false;

   updateGuiControls();
}

function updateGuiControls()
{
   if ( isJoystickEnabled() )
      updateEnabled();
   else
      updateDisabled();

   // Update the gui 30 times a second
   schedule( ( 1 / 30 ) * 1000, 0, "updateGuiControls" );
}

function updateEnabled()
{
   if ( !$rumbleLock )
   {
      $leftMotor  = $joyXAXIS;
      $rightMotor = $joyYAXIS;
      rumble("joystick" @ %cid, $leftMotor[%cid], $rightMotor[%cid]);
   }

   if ( $joyBTN0 == 1 ) $buttonPressed = true;
   if ( $joyBTN0 == 0 && $buttonPressed)
   {
      $rumbleLock = !$rumbleLock;
      $buttonPressed = 0;
   }

   txtJoystick.setText( "Joystick: Enabled" );
   txtLMotor.setText( "Left Motor Speed: " @ $leftMotor );
   txtRMotor.setText( "Right Motor Speed: " @ $rightMotor );
   txtRumbleLock.setText( "Rumble Lock: " @ $rumbleLock );

   txtLMotor.setVisible( 1 );
   txtRMotor.setVisible( 1 );
   txtRumbleLock.setVisible( 1 );
}

function updateDisabled()
{
   txtJoystick.setText( "Joystick: Disabled" );
   txtLMotor.setVisible( 0 );
   txtRMotor.setVisible( 0 );
   txtRumbleLock.setVisible( 0 );
}

function bindJoystickEvents()
{
   GlobalActionMap.bind("joystick0", "xaxis", "joyXAXIS");
   GlobalActionMap.bind("joystick0", "yaxis", "joyYAXIS");
   GlobalActionMap.bind("joystick0", "rxaxis", "joyRXAXIS");
   GlobalActionMap.bind("joystick0", "ryaxis", "joyRYAXIS");
   GlobalActionMap.bind("joystick0", "zaxis", "joyZAXIS");
   GlobalActionMap.bind("joystick0", "rzaxis", "joyRZAXIS");
   GlobalActionMap.bind("joystick0", "xpov", "joyXPOV");
   GlobalActionMap.bind("joystick0", "ypov", "joyYPOV");
   GlobalActionMap.bind("joystick0", "button0", "joyBTN0");
   GlobalActionMap.bind("joystick0", "button1", "joyBTN1");
   GlobalActionMap.bind("joystick0", "button2", "joyBTN2");
   GlobalActionMap.bind("joystick0", "button3", "joyBTN3");
   GlobalActionMap.bind("joystick0", "button4", "joyBTN4");
   GlobalActionMap.bind("joystick0", "button5", "joyBTN5");
   GlobalActionMap.bind("joystick0", "button6", "joyBTN6");
   GlobalActionMap.bind("joystick0", "button7", "joyBTN7");
   GlobalActionMap.bind("joystick0", "button8", "joyBTN8");
   GlobalActionMap.bind("joystick0", "button9", "joyBTN9");
   $joyXPOV = 0.5;
   $joyYPOV = 0.5;
}

function joyXAXIS( %val )       { $joyXAXIS = %val; }
function joyYAXIS( %val )       { $joyYAXIS = %val; }
function joyRXAXIS( %val )      { $joyRXAXIS = %val; }
function joyRYAXIS( %val )      { $joyRYAXIS = %val; }
function joyZAXIS( %val )       { $joyZAXIS = %val; }
function joyRZAXIS( %val )      { $joyRZAXIS = %val; }
function joyXPOV( %val )        { $joyXPOV = %val; }
function joyYPOV( %val )        { $joyYPOV = %val; }
function joyBTN0( %val )        { $joyBTN0 = %val; }
function joyBTN1( %val )        { $joyBTN1 = %val; }
function joyBTN2( %val )        { $joyBTN2 = %val; }
function joyBTN3( %val )        { $joyBTN3 = %val; }
function joyBTN4( %val )        { $joyBTN4 = %val; }
function joyBTN5( %val )        { $joyBTN5 = %val; }
function joyBTN6( %val )        { $joyBTN6 = %val; }
function joyBTN7( %val )        { $joyBTN7 = %val; }
function joyBTN8( %val )        { $joyBTN8 = %val; }
function joyBTN9( %val )        { $joyBTN9 = %val; }

initClient();
