/**
 * A Poor Man's SDL Joystick tester
 * Compile with: gcc -o joytest joytest.c `pkg-config --cflags --libs sdl`
 *
 * Copyright (c) 2007, Thomas Perl <thpinfo.com>
 * All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 **/

#include "SDL.h"

#define PERCENTIZE(x) ((float)(x)/(float)(32768)*100.0)
#define BSTATE(x) ((x==SDL_PRESSED)?("pressed"):("released"))

int main() {
    int i, n;
    SDL_Event e;

    SDL_Init( SDL_INIT_EVERYTHING);
    n = SDL_NumJoysticks();
    printf( "Joysticks available: %d\n", n);
    for( i=0; i<n; i++) {
        printf( "Joystick #%d: %s\n", i, SDL_JoystickName( i));
        SDL_JoystickOpen( i);
    }

    SDL_JoystickEventState( SDL_ENABLE);
    while( SDL_WaitEvent( &e) && e.type != SDL_QUIT) {
        switch( e.type) {
            case SDL_JOYAXISMOTION:
                printf( "Axis %d on ``%s'': %.2f%%\n",
                        e.jaxis.axis,
                        SDL_JoystickName( e.jaxis.which),
                        PERCENTIZE(e.jaxis.value));
                break;
            case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP:
                printf( "Button %d %s on ``%s''\n",
                        e.jbutton.button,
                        BSTATE(e.jbutton.state),
                        SDL_JoystickName( e.jbutton.which));
                break;
            default:
                printf( "Unhandled event type #%d\n", e.type);
                break;
        }
    }

    SDL_Quit();
    return 0;
}

