
//#include "swis.h"
#include <stdlib.h>
#include <stdint.h>
//#include <stdio.h>
//#include <string.h>
//#include "Global/NewErrors.h"
//#include "Global/RISCOS.h"
//#include "Global/Keyboard.h"

#include "debugrep.h"
#include "usbjoystickHdr.h"
#include "usbjoystick.h"

#include "device.h"
#include "map.h"


void auto_map(uint32_t i)
{
  if (joy_data[i].in_use == FALSE || joy_data[i].open == FALSE)
    return;

  // 8-bit axes mapping
  osbool mapped8 = find_and_map_x_y_8(i, AXIS_DPADX, AXIS_DPADY);

  if (!mapped8)
    mapped8 = find_and_map_x_y_8(i, AXIS_HATX, AXIS_HATY);

  if (!mapped8)
    mapped8 = find_and_map_x_y_8(i, AXIS_X, AXIS_Y);

  // 16-bit axes mapping
  osbool mapped16 = find_and_map_x_y_16(i, AXIS_X, AXIS_Y);

  if (!mapped16)
    mapped16 = find_and_map_x_y_8(i, AXIS_DPADX, AXIS_DPADY);

  if (!mapped16)
    mapped16 = find_and_map_x_y_8(i, AXIS_HATX, AXIS_HATY);

  // Auto flip any axes that this 'driver' wants us to
  for (uint32_t a=0; a<joy_data[i].device->auto_flip_axes_length; a++) {
    for (uint32_t j=0; j<joy_data[i].num_axes; j++) {
      if (joy_data[i].axes[j].type == joy_data[i].device->auto_flip_axes[a])
        joy_data[i].axes[j].flip = TRUE;
    }
  }

  // Automatically map the buttons
  uint32_t maxbutton = joy_data[i].num_buttons;

  //if (joy_data[i].num_buttons > ACORN_BUTTONS)
  //  maxbutton = ACORN_BUTTONS;

  for (uint32_t b=0; b<maxbutton; b++)
    joy_data[i].mapped_buttons[b] = b;

  // Automatically assign a 'stick number' based on the highest used so far
  int32_t highest_stick = NOT_MAPPED;
  for (uint32_t j=0; j<JOY_MAX; j++) {
    if (joy_data[j].mapped_number > highest_stick)
      highest_stick = joy_data[j].mapped_number;
  }

  if (highest_stick == NOT_MAPPED)
    joy_data[i].mapped_number = 0;
  else
    joy_data[i].mapped_number = highest_stick + 1;

  // Auto map the mouse axes and buttons
  joy_data[i].mapped_mouse_x = joy_data[i].mapped_x_16;
  joy_data[i].mapped_mouse_y = joy_data[i].mapped_y_16;
  joy_data[i].mapped_mouse_select = joy_data[i].mapped_buttons[0];
  joy_data[i].mapped_mouse_menu = joy_data[i].mapped_buttons[1];
  joy_data[i].mapped_mouse_adjust = joy_data[i].mapped_buttons[2];

}





void unmap(uint32_t i)
{
  // don't need to do anything if we were not mapped
  if (joy_data[i].mapped_number == NOT_MAPPED)
    return;

  // decrement any higher mapped stick numbers
  int32_t old_map = joy_data[i].mapped_number;
  joy_data[i].mapped_number = NOT_MAPPED;

  for (uint32_t j=0; j<JOY_MAX; j++) {
    if (joy_data[j].mapped_number > old_map)
      joy_data[j].mapped_number--;
  }

}





osbool find_and_map_x_y_8(uint32_t i, uint32_t x_type, uint32_t y_type)
{
  int32_t x = NOT_MAPPED;
  int32_t y = NOT_MAPPED;

  for (uint32_t j=0; j<joy_data[i].num_axes; j++) {
    if (joy_data[i].axes[j].type == x_type)
      x = j;
    else if (joy_data[i].axes[j].type == y_type)
      y = j;
  }

  if (x != NOT_MAPPED && y != NOT_MAPPED) {
      joy_data[i].mapped_x_8 = x;
      joy_data[i].mapped_y_8 = y;
      return true;
  }
  else {
    return false;
  }
}




osbool find_and_map_x_y_16(uint32_t i, uint32_t x_type, uint32_t y_type)
{
  int32_t x = NOT_MAPPED;
  int32_t y = NOT_MAPPED;

  for (uint32_t j=0; j<joy_data[i].num_axes; j++) {
    if (joy_data[i].axes[j].type == x_type)
      x = j;
    else if (joy_data[i].axes[j].type == y_type)
      y = j;
  }

  if (x != NOT_MAPPED && y != NOT_MAPPED) {
      joy_data[i].mapped_x_16 = x;
      joy_data[i].mapped_y_16 = y;
      return true;
  }
  else {
    return false;
  }
}


