/**
 * Joystick-to-Keyboard Thingie
 * Copyright 2010-11-08 Thomas Perl <thp.io/about>
 *
 * Useful in combination with USB Hostmode and a Joystick/Gamepad for games and
 * applications that only accept keyboard input.
 *
 * Based on:
 * A Poor Man's SDL Joystick tester
 * Compile with: gcc -o jsthingie jsthingie.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"

#include <linux/input.h>
#include <linux/uinput.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <assert.h>

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


static struct uinput_user_dev device;
static struct input_event event;
static int uinput_fd;

void press_key(int code) {
    memset(&event, 0, sizeof event);
    gettimeofday(&event.time, NULL);
    event.type = EV_KEY;
    event.code = code;
    event.value = 1;
    write(uinput_fd, &event, sizeof event);

    memset(&event, 0, sizeof event);
    gettimeofday(&event.time, NULL);
    event.type = EV_SYN;
    event.code = SYN_REPORT;
    event.value = 0;
    write(uinput_fd, &event, sizeof event);
}

void release_key(int code) {
    memset(&event, 0, sizeof event);
    gettimeofday(&event.time, NULL);
    event.type = EV_KEY;
    event.code = code;
    event.value = 0;
    write(uinput_fd, &event, sizeof event);

    memset(&event, 0, sizeof event);
    gettimeofday(&event.time, NULL);
    event.type = EV_SYN;
    event.code = SYN_REPORT;
    event.value = 0;
    write(uinput_fd, &event, sizeof event);
}

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

    SDL_Init( SDL_INIT_EVERYTHING);
    n = SDL_NumJoysticks();

    if (n == 0) {
        fprintf(stderr, "No joysticks found.\n");
        SDL_Quit();
        exit(1);
    } else {
        printf("Joysticks available: %d\n", n);
        for(i=0; i<n; i++) {
            printf("Joystick #%d: %s\n", i, SDL_JoystickName(i));
            SDL_JoystickOpen(i);
        }
    }

    uinput_fd = open("/dev/input/uinput", O_WRONLY | O_NDELAY);
    assert(uinput_fd != 0);

    memset(&device, 0, sizeof device);
    strncpy(device.name, SDL_JoystickName(0), UINPUT_MAX_NAME_SIZE);
    /* ??? */
    device.id.version = 5;
    device.id.bustype = BUS_USB;

    ioctl(uinput_fd, UI_SET_EVBIT, EV_KEY);

    for (i=0; i<256; i++) {
        ioctl(uinput_fd, UI_SET_KEYBIT, i);
    }

    write(uinput_fd, &device, sizeof device);
    if (ioctl(uinput_fd, UI_DEV_CREATE)) {
        fprintf(stderr, "Unable to create uinput device\n");
        exit(1);
    }

    SDL_JoystickEventState( SDL_ENABLE);
    while( SDL_WaitEvent( &e) && e.type != SDL_QUIT) {
        switch( e.type) {
            case SDL_JOYAXISMOTION:
                /*printf( "Axis %d on ``%s'': %.2f%% (%ld)\n",
                        e.jaxis.axis,
                        SDL_JoystickName( e.jaxis.which),
                        PERCENTIZE(e.jaxis.value),
                        (long)e.jaxis.value);*/

                /* FIXME: Add support for analog joysticks */
                switch (e.jaxis.axis) {
                    case 0:
                        if (e.jaxis.value == -32767) {
                            press_key(KEY_LEFT);
                        } else if (e.jaxis.value == 32767) {
                            press_key(KEY_RIGHT);
                        } else {
                            release_key(KEY_LEFT);
                            release_key(KEY_RIGHT);
                        }
                        break;
                    case 1:
                        if (e.jaxis.value == -32767) {
                            press_key(KEY_UP);
                        } else if (e.jaxis.value == 32767) {
                            press_key(KEY_DOWN);
                        } else {
                            release_key(KEY_UP);
                            release_key(KEY_DOWN);
                        }
                        break;
                }
                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));*/
                if (e.type == SDL_JOYBUTTONDOWN) {
                    press_key(KEY_A + e.jbutton.button);
                } else if (e.type == SDL_JOYBUTTONUP) {
                    release_key(KEY_A + e.jbutton.button);
                }

                /*if (e.jbutton.button == 0) {
                    SDL_Quit();
                }*/
                break;
            default:
                printf( "Unhandled event type #%d\n", e.type);
                break;
        }
    }

    ioctl(uinput_fd, UI_DEV_DESTROY);
    close(uinput_fd);

    return 0;
}


