/**
 * @file joystick.c
 *
 * @brief Handles joystick initialization.
 */

#include <SDL/SDL.h>
#include <string.h>
#include "lephisto.h"
#include "log.h"
#include "joystick.h"

static SDL_Joystick* joystick = NULL; /**< Current joystick in use. */


/**
 * @fn int joystick_get(char* namjoystick)
 *
 * @brief Get the joystick index by name.
 *    @param namjoystick Looks for given string in the joystick name.
 *    @return The index if found, defaults to 0 if it isn't found.
 */
int joystick_get(char* namjoystick) {
  int i;
  for(i = 0; i < SDL_NumJoysticks(); i++)
    if(strstr(SDL_JoystickName(i), namjoystick))
      return i;

  WARN("Joystick '%s' not found, using default joystick '%s'",
       namjoystick, SDL_JoystickName(0));
  return 0;
}

/**
 * @fn int joystick_use(int indjoystick)
 *
 * @brief Make the game use a joystick by index.
 *    @param indjoystick Index of the joystick to use.
 *    @return 0 on success.
 */
int joystick_use(int indjoystick) {
  if(indjoystick < 0 || indjoystick >= SDL_NumJoysticks()) {
    WARN("Joystick of index number %d does not exist. Switching to default (0)",
         indjoystick);
    indjoystick = 0;
  }
  if(joystick)
    /* Might as well close it if it is open already. */
    SDL_JoystickClose(joystick);
  /* Start using the joystick. */
  LOG("Using joystick %d", indjoystick);
  joystick = SDL_JoystickOpen(indjoystick);
  if(joystick == NULL) {
    WARN("Error opening joystick %d [%s]",
         indjoystick, SDL_JoystickName(indjoystick));
    return -1;
  }
  DEBUG("\t\tWith %d axes, %d buttons, %d balls, and %d hats",
        SDL_JoystickNumAxes(joystick), SDL_JoystickNumButtons(joystick),
        SDL_JoystickNumBalls(joystick), SDL_JoystickNumHats(joystick));

  return 0;
}

/**
 * @fn int joystick_init(void)
 *
 * @brief Initializes the joystick subsystem.
 *    @return 0 on success.
 */
int joystick_init(void) {
  int numjoysticks, i;

  /* Init the SDL subsys. */
  if(SDL_InitSubSystem(SDL_INIT_JOYSTICK)) {
    WARN("Unable to init the joystick subsystem.");
    return -1;
  }

  /* Figure out how many joysticks there are. */
  numjoysticks = SDL_NumJoysticks();
  LOG("%d joystick%s detected", numjoysticks, (numjoysticks==1)?"":"s");
  for(i = 0; i < numjoysticks; i++)
    LOG("\t\t%d. %s", i, SDL_JoystickName(i));

  /* Enable joystick events. */
  SDL_JoystickEventState(SDL_ENABLE);

  return 0;
}

/**
 * @fn void joystick_exit(void)
 *
 * @brief Exit the joystick subsystem.
 */
void joystick_exit(void) {
  SDL_JoystickClose(joystick);
}

