/**
 * Stepmania Launch Daemon
 * (based on joytest.c written by Thomas Perl in 2007)
 *
 * --- BEGIN MAKEFILE ---
 *  PKGS = sdl
 *
 *  CFLAGS += `pkg-config --cflags $(PKGS)`
 *  LDFLAGS += `pkg-config --libs $(PKGS)`
 *
 *  APP = stepmaniad
 *  OBJS = stepmaniad.o
 *
 *  default: $(APP)
 *
 *  $(APP): $(OBJS)
 *
 *  clean:
 *          rm -f $(OBJS) $(APP)
 *
 *  .PHONY: default clean
 *  .DEFAULT: default
 * ---  END MAKEFILE  ---
 *
 * Copyright (c) 2010 Thomas Perl <thpinfo.com/about>
 * 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 <unistd.h>

#include "SDL.h"

/* How many buttons need to be pressed? */
#define REQUIRED_BUTTONS 2

/* How long do the buttons need to be pressed */
#define REQUIRED_MS 3000

struct StepmaniaDaemon {
    char* command;
    SDL_TimerID timer_id;
    int status;
};

Uint32 start_stepmania(Uint32 interval, void* parameter) {
    struct StepmaniaDaemon* data = (struct StepmaniaDaemon*)parameter;

    if (data->timer_id) {
        pid_t pid;

        pid = fork();
        if (pid == 0) {
            execlp(data->command, data->command, NULL);
        } else {
            waitpid(pid, &data->status, 0);
        }

        data->timer_id = 0;
    }
    return 0;
}

int main(int argc, char** argv) {
    int i, n;
    SDL_Event e;
    int pressed_buttons = 0;

    struct StepmaniaDaemon data = { .timer_id = 0, .status = 0 };

    if (argc == 2) {
        data.command = argv[1];
    } else {
        data.command = "./stepmania";
    }

    SDL_Init(SDL_INIT_EVERYTHING);

    for(i=0; i<SDL_NumJoysticks(); 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_JOYBUTTONDOWN:
            case SDL_JOYBUTTONUP:
                if (e.jbutton.state == SDL_PRESSED) {
                    pressed_buttons++;
                } else {
                    pressed_buttons--;
                }

                if (pressed_buttons == REQUIRED_BUTTONS) {
                    data.timer_id = SDL_AddTimer(REQUIRED_MS, start_stepmania, &data);
                } else if (data.timer_id) {
                    SDL_RemoveTimer(data.timer_id);
                    data.timer_id = 0;
                }

                break;
            default:
                break;
        }
    }

    SDL_Quit();
    return EXIT_SUCCESS;
}


