RetroArch/input/dinput.c

546 lines
14 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2012-01-08 00:08:18 +00:00
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
2011-06-10 14:55:05 +00:00
*
2012-04-21 21:13:50 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2011-06-10 14:55:05 +00:00
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
2012-04-21 21:13:50 +00:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2011-06-10 14:55:05 +00:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2011-06-10 14:55:05 +00:00
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef _MSC_VER
2012-11-15 08:40:31 +00:00
#pragma comment(lib, "dinput8")
#endif
2012-09-28 20:38:42 +00:00
#undef DIRECTINPUT_VERSION
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
#include "../general.h"
2011-12-24 12:46:12 +00:00
#include "../boolean.h"
2012-09-28 20:38:42 +00:00
#include "input_common.h"
2011-06-10 14:55:05 +00:00
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
2012-09-30 09:26:26 +00:00
// Context has to be global as joypads also ride on this context.
2012-09-28 20:38:42 +00:00
static LPDIRECTINPUT8 g_ctx;
2012-09-30 09:26:26 +00:00
struct dinput_input
{
LPDIRECTINPUTDEVICE8 keyboard;
2012-10-28 09:42:20 +00:00
LPDIRECTINPUTDEVICE8 mouse;
2012-09-30 09:26:26 +00:00
const rarch_joypad_driver_t *joypad;
uint8_t state[256];
2012-10-28 09:42:20 +00:00
int mouse_rel_x;
int mouse_rel_y;
int mouse_x;
int mouse_y;
bool mouse_l, mouse_r, mouse_m;
2012-09-30 09:26:26 +00:00
};
2012-09-28 20:38:42 +00:00
struct dinput_joypad
{
LPDIRECTINPUTDEVICE8 joypad;
DIJOYSTATE2 joy_state;
};
static unsigned g_joypad_cnt;
static struct dinput_joypad g_pads[MAX_PLAYERS];
2012-09-30 09:26:26 +00:00
static void dinput_destroy_context(void)
{
if (g_ctx)
{
IDirectInput8_Release(g_ctx);
g_ctx = NULL;
}
}
static bool dinput_init_context(void)
{
if (g_ctx)
return true;
if (driver.display_type != RARCH_DISPLAY_WIN32)
{
RARCH_ERR("Cannot open DInput as no Win32 window is present.\n");
return false;
}
CoInitialize(NULL);
// Who said we shouldn't have same call signature in a COM API? <_<
#ifdef __cplusplus
if (FAILED(DirectInput8Create(
GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8,
(void**)&g_ctx, NULL)))
#else
if (FAILED(DirectInput8Create(
GetModuleHandle(NULL), DIRECTINPUT_VERSION, &IID_IDirectInput8,
(void**)&g_ctx, NULL)))
#endif
{
RARCH_ERR("Failed to init DirectInput.\n");
return false;
}
return true;
}
static void *dinput_init(void)
{
if (!dinput_init_context())
return NULL;
struct dinput_input *di = (struct dinput_input*)calloc(1, sizeof(*di));
if (!di)
return NULL;
#ifdef __cplusplus
if (FAILED(IDirectInput8_CreateDevice(g_ctx, GUID_SysKeyboard, &di->keyboard, NULL)))
2012-10-28 09:42:20 +00:00
goto error;
if (FAILED(IDirectInput8_CreateDevice(g_ctx, GUID_SysMouse, &di->mouse, NULL)))
goto error;
2012-09-30 09:26:26 +00:00
#else
if (FAILED(IDirectInput8_CreateDevice(g_ctx, &GUID_SysKeyboard, &di->keyboard, NULL)))
goto error;
2012-10-28 09:42:20 +00:00
if (FAILED(IDirectInput8_CreateDevice(g_ctx, &GUID_SysMouse, &di->mouse, NULL)))
goto error;
#endif
2012-09-30 09:26:26 +00:00
IDirectInputDevice8_SetDataFormat(di->keyboard, &c_dfDIKeyboard);
IDirectInputDevice8_SetCooperativeLevel(di->keyboard,
(HWND)driver.video_window, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
IDirectInputDevice8_Acquire(di->keyboard);
2012-10-28 09:42:20 +00:00
IDirectInputDevice8_SetDataFormat(di->mouse, &c_dfDIMouse2);
IDirectInputDevice8_SetCooperativeLevel(di->mouse, (HWND)driver.video_window,
DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
IDirectInputDevice8_Acquire(di->mouse);
input_init_keyboard_lut(rarch_key_map_dinput);
2012-09-30 09:26:26 +00:00
di->joypad = input_joypad_init_first();
return di;
error:
dinput_destroy_context();
free(di);
return NULL;
}
static void dinput_poll(void *data)
{
struct dinput_input *di = (struct dinput_input*)data;
memset(di->state, 0, sizeof(di->state));
2012-10-28 09:42:20 +00:00
if (FAILED(IDirectInputDevice8_GetDeviceState(di->keyboard, sizeof(di->state), di->state)))
2012-09-30 09:26:26 +00:00
{
IDirectInputDevice8_Acquire(di->keyboard);
if (FAILED(IDirectInputDevice8_GetDeviceState(di->keyboard, sizeof(di->state), di->state)))
memset(di->state, 0, sizeof(di->state));
2012-09-30 09:26:26 +00:00
}
2012-10-28 09:42:20 +00:00
DIMOUSESTATE2 mouse_state;
memset(&mouse_state, 0, sizeof(mouse_state));
if (FAILED(IDirectInputDevice8_GetDeviceState(di->mouse, sizeof(mouse_state), &mouse_state)))
{
IDirectInputDevice8_Acquire(di->mouse);
if (FAILED(IDirectInputDevice8_GetDeviceState(di->mouse, sizeof(mouse_state), &mouse_state)))
memset(&mouse_state, 0, sizeof(mouse_state));
}
di->mouse_rel_x = mouse_state.lX;
di->mouse_rel_y = mouse_state.lY;
di->mouse_l = mouse_state.rgbButtons[0];
di->mouse_r = mouse_state.rgbButtons[1];
di->mouse_m = mouse_state.rgbButtons[2];
// No simple way to get absolute coordinates for RETRO_DEVICE_POINTER. Just use Win32 APIs.
POINT point = {0};
GetCursorPos(&point);
ScreenToClient((HWND)driver.video_window, &point);
di->mouse_x = point.x;
di->mouse_y = point.y;
2012-09-30 09:26:26 +00:00
input_joypad_poll(di->joypad);
}
static bool dinput_keyboard_pressed(struct dinput_input *di, unsigned key)
{
if (key >= RETROK_LAST)
return false;
2012-12-19 20:27:27 +00:00
unsigned sym = input_translate_rk_to_keysym((enum retro_key)key);
return di->state[sym] & 0x80;
2012-09-30 09:26:26 +00:00
}
static bool dinput_is_pressed(struct dinput_input *di, const struct retro_keybind *binds,
unsigned port, unsigned id)
{
if (id >= RARCH_BIND_LIST_END)
return false;
const struct retro_keybind *bind = &binds[id];
return dinput_keyboard_pressed(di, bind->key) || input_joypad_pressed(di->joypad, port, bind);
}
static bool dinput_key_pressed(void *data, int key)
{
return dinput_is_pressed((struct dinput_input*)data, g_settings.input.binds[0], 0, key);
}
2012-10-28 09:42:20 +00:00
static int16_t dinput_lightgun_state(struct dinput_input *di, unsigned id)
{
switch (id)
{
case RETRO_DEVICE_ID_LIGHTGUN_X:
return di->mouse_rel_x;
case RETRO_DEVICE_ID_LIGHTGUN_Y:
return di->mouse_rel_y;
case RETRO_DEVICE_ID_LIGHTGUN_TRIGGER:
return di->mouse_l;
case RETRO_DEVICE_ID_LIGHTGUN_CURSOR:
return di->mouse_m;
case RETRO_DEVICE_ID_LIGHTGUN_TURBO:
return di->mouse_r;
case RETRO_DEVICE_ID_LIGHTGUN_START:
return di->mouse_m && di->mouse_r;
case RETRO_DEVICE_ID_LIGHTGUN_PAUSE:
return di->mouse_m && di->mouse_l;
default:
return 0;
}
}
static int16_t dinput_mouse_state(struct dinput_input *di, unsigned id)
{
switch (id)
{
case RETRO_DEVICE_ID_MOUSE_X:
return di->mouse_rel_x;
case RETRO_DEVICE_ID_MOUSE_Y:
return di->mouse_rel_y;
case RETRO_DEVICE_ID_MOUSE_LEFT:
return di->mouse_l;
case RETRO_DEVICE_ID_MOUSE_RIGHT:
return di->mouse_r;
default:
return 0;
}
}
2012-12-27 11:26:13 +00:00
static int16_t dinput_pointer_state(struct dinput_input *di, unsigned index, unsigned id)
2012-10-28 09:42:20 +00:00
{
2012-12-27 11:26:13 +00:00
if (index != 0)
return 0;
2012-10-28 09:42:20 +00:00
int16_t res_x = 0, res_y = 0;
bool valid = input_translate_coord_viewport(di->mouse_x, di->mouse_y, &res_x, &res_y);
if (!valid)
return 0;
bool inside = (res_x >= -0x7fff) && (res_x <= 0x7fff) &&
(res_y >= -0x7fff) && (res_y <= 0x7fff);
if (!inside)
return 0;
switch (id)
{
case RETRO_DEVICE_ID_POINTER_X:
return res_x;
case RETRO_DEVICE_ID_POINTER_Y:
return res_y;
case RETRO_DEVICE_ID_POINTER_PRESSED:
return di->mouse_l;
default:
return 0;
}
}
2012-09-30 09:26:26 +00:00
static int16_t dinput_input_state(void *data,
const struct retro_keybind **binds, unsigned port,
unsigned device, unsigned index, unsigned id)
{
struct dinput_input *di = (struct dinput_input*)data;
switch (device)
{
case RETRO_DEVICE_JOYPAD:
return dinput_is_pressed(di, binds[port], port, id);
case RETRO_DEVICE_KEYBOARD:
return dinput_keyboard_pressed(di, id);
case RETRO_DEVICE_ANALOG:
return input_joypad_analog(di->joypad, port, index, id, g_settings.input.binds[port]);
2012-10-28 09:42:20 +00:00
case RETRO_DEVICE_MOUSE:
return dinput_mouse_state(di, id);
case RETRO_DEVICE_POINTER:
2012-12-27 11:26:13 +00:00
return dinput_pointer_state(di, index, id);
2012-10-28 09:42:20 +00:00
2012-09-30 09:26:26 +00:00
case RETRO_DEVICE_LIGHTGUN:
2012-10-28 09:42:20 +00:00
return dinput_lightgun_state(di, id);
2012-09-30 09:26:26 +00:00
default:
return 0;
}
}
static void dinput_free(void *data)
{
struct dinput_input *di = (struct dinput_input*)data;
LPDIRECTINPUT8 hold_ctx = g_ctx;
if (di)
{
g_ctx = NULL; // Prevent a joypad driver to kill our context prematurely.
di->joypad->destroy();
g_ctx = hold_ctx;
if (di->keyboard)
IDirectInputDevice8_Release(di->keyboard);
2012-10-28 09:42:20 +00:00
if (di->mouse)
IDirectInputDevice8_Release(di->mouse);
2012-09-30 09:26:26 +00:00
free(di);
}
dinput_destroy_context();
}
const input_driver_t input_dinput = {
dinput_init,
dinput_poll,
dinput_input_state,
dinput_key_pressed,
dinput_free,
"dinput",
};
static void dinput_joypad_destroy(void)
2011-06-10 14:55:05 +00:00
{
2012-09-28 20:38:42 +00:00
for (unsigned i = 0; i < MAX_PLAYERS; i++)
2011-06-10 14:55:05 +00:00
{
2012-09-28 20:38:42 +00:00
if (g_pads[i].joypad)
2011-06-10 14:55:05 +00:00
{
2012-09-28 20:38:42 +00:00
IDirectInputDevice8_Unacquire(g_pads[i].joypad);
IDirectInputDevice8_Release(g_pads[i].joypad);
2011-06-10 14:55:05 +00:00
}
2012-09-28 20:38:42 +00:00
}
2011-06-10 14:55:05 +00:00
2012-09-28 20:38:42 +00:00
g_joypad_cnt = 0;
memset(g_pads, 0, sizeof(g_pads));
2012-09-30 09:26:26 +00:00
// Can be blocked by global Dinput context.
dinput_destroy_context();
2011-06-10 14:55:05 +00:00
}
static BOOL CALLBACK enum_axes_cb(const DIDEVICEOBJECTINSTANCE *inst, void *p)
{
2011-12-24 12:46:12 +00:00
LPDIRECTINPUTDEVICE8 joypad = (LPDIRECTINPUTDEVICE8)p;
2011-06-10 14:55:05 +00:00
DIPROPRANGE range;
memset(&range, 0, sizeof(range));
range.diph.dwSize = sizeof(DIPROPRANGE);
range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
range.diph.dwHow = DIPH_BYID;
range.diph.dwObj = inst->dwType;
2012-09-30 09:26:26 +00:00
range.lMin = -0x7fff;
range.lMax = 0x7fff;
2011-06-10 14:55:05 +00:00
IDirectInputDevice8_SetProperty(joypad, DIPROP_RANGE, &range.diph);
return DIENUM_CONTINUE;
}
static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p)
{
2012-09-28 20:38:42 +00:00
(void)p;
if (g_joypad_cnt == MAX_PLAYERS)
return DIENUM_STOP;
2011-06-10 14:55:05 +00:00
2012-09-28 20:38:42 +00:00
LPDIRECTINPUTDEVICE8 *pad = &g_pads[g_joypad_cnt].joypad;
2011-06-10 14:55:05 +00:00
2011-12-24 12:46:12 +00:00
#ifdef __cplusplus
2012-09-28 20:38:42 +00:00
if (FAILED(IDirectInput8_CreateDevice(g_ctx, inst->guidInstance, pad, NULL)))
2011-12-24 12:46:12 +00:00
#else
2012-09-28 20:38:42 +00:00
if (FAILED(IDirectInput8_CreateDevice(g_ctx, &inst->guidInstance, pad, NULL)))
2011-12-24 12:46:12 +00:00
#endif
return DIENUM_CONTINUE;
2011-06-10 14:55:05 +00:00
2012-09-28 20:38:42 +00:00
IDirectInputDevice8_SetDataFormat(*pad, &c_dfDIJoystick2);
IDirectInputDevice8_SetCooperativeLevel(*pad, (HWND)driver.video_window,
2011-06-10 14:55:05 +00:00
DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
2012-09-28 20:38:42 +00:00
IDirectInputDevice8_EnumObjects(*pad, enum_axes_cb,
*pad, DIDFT_ABSAXIS);
g_joypad_cnt++;
2011-06-10 14:55:05 +00:00
return DIENUM_CONTINUE;
}
2012-09-30 09:26:26 +00:00
static bool dinput_joypad_init(void)
2011-06-10 14:55:05 +00:00
{
2012-09-30 09:26:26 +00:00
if (!dinput_init_context())
2012-09-28 20:38:42 +00:00
return false;
2011-06-10 14:55:05 +00:00
2012-09-30 09:26:26 +00:00
RARCH_LOG("Enumerating DInput joypads ...\n");
IDirectInput8_EnumDevices(g_ctx, DI8DEVCLASS_GAMECTRL,
enum_joypad_cb, NULL, DIEDFL_ATTACHEDONLY);
RARCH_LOG("Done enumerating DInput joypads ...\n");
2012-09-28 20:38:42 +00:00
return true;
2011-06-10 14:55:05 +00:00
}
2012-09-30 09:26:26 +00:00
static bool dinput_joypad_button(unsigned port_num, uint16_t joykey)
2011-06-10 14:55:05 +00:00
{
if (joykey == NO_BTN)
return false;
2012-09-28 20:38:42 +00:00
const struct dinput_joypad *pad = &g_pads[port_num];
if (!pad->joypad)
return false;
2012-09-28 20:38:42 +00:00
2011-06-10 14:55:05 +00:00
// Check hat.
if (GET_HAT_DIR(joykey))
{
unsigned hat = GET_HAT(joykey);
2012-09-28 20:38:42 +00:00
unsigned elems = sizeof(pad->joy_state.rgdwPOV) / sizeof(pad->joy_state.rgdwPOV[0]);
2011-06-10 14:55:05 +00:00
if (hat >= elems)
return false;
2012-09-28 20:38:42 +00:00
unsigned pov = pad->joy_state.rgdwPOV[hat];
2011-06-10 14:55:05 +00:00
// Magic numbers I'm not sure where originate from.
if (pov < 36000)
{
switch (GET_HAT_DIR(joykey))
{
case HAT_UP_MASK:
return (pov >= 31500) || (pov <= 4500);
case HAT_RIGHT_MASK:
return (pov >= 4500) && (pov <= 13500);
case HAT_DOWN_MASK:
return (pov >= 13500) && (pov <= 22500);
case HAT_LEFT_MASK:
return (pov >= 22500) && (pov <= 31500);
}
}
return false;
}
else
{
2012-09-28 20:38:42 +00:00
unsigned elems = sizeof(pad->joy_state.rgbButtons) / sizeof(pad->joy_state.rgbButtons[0]);
2011-06-10 14:55:05 +00:00
if (joykey < elems)
2012-09-28 20:38:42 +00:00
return pad->joy_state.rgbButtons[joykey];
2011-06-10 14:55:05 +00:00
}
return false;
}
2012-09-30 09:26:26 +00:00
static int16_t dinput_joypad_axis(unsigned port_num, uint32_t joyaxis)
2011-06-10 14:55:05 +00:00
{
if (joyaxis == AXIS_NONE)
2012-06-28 15:57:50 +00:00
return 0;
2011-06-10 14:55:05 +00:00
2012-09-28 20:38:42 +00:00
const struct dinput_joypad *pad = &g_pads[port_num];
if (!pad->joypad)
return 0;
2012-09-28 20:38:42 +00:00
2012-06-28 15:57:50 +00:00
int val = 0;
2011-06-10 14:55:05 +00:00
2012-06-28 15:57:50 +00:00
int axis = -1;
bool is_neg = false;
bool is_pos = false;
if (AXIS_NEG_GET(joyaxis) <= 5)
2011-06-10 14:55:05 +00:00
{
2012-06-28 15:57:50 +00:00
axis = AXIS_NEG_GET(joyaxis);
is_neg = true;
}
else if (AXIS_POS_GET(joyaxis) <= 5)
{
axis = AXIS_POS_GET(joyaxis);
is_pos = true;
2011-06-10 14:55:05 +00:00
}
2012-06-28 15:57:50 +00:00
switch (axis)
2011-06-10 14:55:05 +00:00
{
2012-09-28 20:38:42 +00:00
case 0: val = pad->joy_state.lX; break;
case 1: val = pad->joy_state.lY; break;
case 2: val = pad->joy_state.lZ; break;
case 3: val = pad->joy_state.lRx; break;
case 4: val = pad->joy_state.lRy; break;
case 5: val = pad->joy_state.lRz; break;
2011-06-10 14:55:05 +00:00
}
2012-06-28 15:57:50 +00:00
if (is_neg && val > 0)
val = 0;
else if (is_pos && val < 0)
val = 0;
return val;
}
2012-09-30 09:26:26 +00:00
static void dinput_joypad_poll(void)
2011-06-10 14:55:05 +00:00
{
for (unsigned i = 0; i < MAX_PLAYERS; i++)
{
2012-09-28 20:38:42 +00:00
struct dinput_joypad *pad = &g_pads[i];
if (pad->joypad)
2011-06-10 14:55:05 +00:00
{
2012-09-28 20:38:42 +00:00
memset(&pad->joy_state, 0, sizeof(pad->joy_state));
if (FAILED(IDirectInputDevice8_Poll(pad->joypad)))
2011-06-10 14:55:05 +00:00
{
2012-09-30 09:26:26 +00:00
if (FAILED(IDirectInputDevice8_Acquire(pad->joypad)))
{
memset(&pad->joy_state, 0, sizeof(DIJOYSTATE2));
continue;
}
// If this fails, something *really* bad must have happened.
if (FAILED(IDirectInputDevice8_Poll(pad->joypad)))
{
memset(&pad->joy_state, 0, sizeof(DIJOYSTATE2));
2012-09-30 09:26:26 +00:00
continue;
}
2011-06-10 14:55:05 +00:00
}
2012-09-30 09:26:26 +00:00
IDirectInputDevice8_GetDeviceState(pad->joypad,
sizeof(DIJOYSTATE2), &pad->joy_state);
2011-06-10 14:55:05 +00:00
}
}
}
2012-09-30 09:26:26 +00:00
static bool dinput_joypad_query_pad(unsigned pad)
{
return pad < MAX_PLAYERS && g_pads[pad].joypad;
}
2012-09-28 20:38:42 +00:00
const rarch_joypad_driver_t dinput_joypad = {
2012-09-30 09:26:26 +00:00
dinput_joypad_init,
dinput_joypad_query_pad,
dinput_joypad_destroy,
dinput_joypad_button,
dinput_joypad_axis,
dinput_joypad_poll,
"dinput",
2012-09-28 20:38:42 +00:00
};