RetroArch/input/dinput.c

1021 lines
26 KiB
C
Raw Normal View History

2012-04-21 23:13:50 +02:00
/* RetroArch - A frontend for libretro.
2014-01-01 01:50:59 +01:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2015-01-07 17:46:50 +01:00
* Copyright (C) 2011-2015 - Daniel De Matteis
2011-06-10 16:55:05 +02:00
*
2012-04-21 23:13:50 +02:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2011-06-10 16:55:05 +02: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 23:13:50 +02:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2011-06-10 16:55:05 +02: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 23:31:57 +02:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2011-06-10 16:55:05 +02:00
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef _MSC_VER
2012-11-15 09:40:31 +01:00
#pragma comment(lib, "dinput8")
#endif
2012-09-28 22:38:42 +02:00
#undef DIRECTINPUT_VERSION
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
#include "../general.h"
#include <boolean.h>
#include "input_autodetect.h"
2012-09-28 22:38:42 +02:00
#include "input_common.h"
#include "input_joypad.h"
#include "input_keymaps.h"
#include "retroarch_logger.h"
2011-06-10 16:55:05 +02:00
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
2013-11-05 20:49:04 +01:00
#include <windowsx.h>
2011-06-10 16:55:05 +02:00
2014-09-09 18:15:17 +02:00
/* Context has to be global as joypads also ride on this context. */
2012-09-28 22:38:42 +02:00
static LPDIRECTINPUT8 g_ctx;
2013-11-05 20:49:04 +01:00
struct pointer_status
{
int pointer_id;
int pointer_x;
int pointer_y;
struct pointer_status *next;
};
2012-09-30 11:26:26 +02:00
struct dinput_input
{
LPDIRECTINPUTDEVICE8 keyboard;
2012-10-28 10:42:20 +01:00
LPDIRECTINPUTDEVICE8 mouse;
2012-09-30 11:26:26 +02:00
const rarch_joypad_driver_t *joypad;
uint8_t state[256];
2012-10-28 10:42:20 +01:00
int mouse_rel_x;
int mouse_rel_y;
int mouse_x;
int mouse_y;
bool mouse_l, mouse_r, mouse_m, mouse_wu, mouse_wd;
2014-09-09 18:15:17 +02:00
struct pointer_status pointer_head; /* dummy head for easier iteration */
2012-09-30 11:26:26 +02:00
};
2012-09-28 22:38:42 +02:00
struct dinput_joypad
{
LPDIRECTINPUTDEVICE8 joypad;
DIJOYSTATE2 joy_state;
char* joy_name;
2012-09-28 22:38:42 +02:00
};
static unsigned g_joypad_cnt;
2015-01-05 01:58:00 +01:00
static struct dinput_joypad g_pads[MAX_USERS];
2012-09-28 22:38:42 +02:00
2012-09-30 11:26:26 +02:00
static void dinput_destroy_context(void)
{
if (!g_ctx)
return;
IDirectInput8_Release(g_ctx);
g_ctx = NULL;
2012-09-30 11:26:26 +02:00
}
static bool dinput_init_context(void)
{
if (g_ctx)
return true;
CoInitialize(NULL);
2014-09-09 18:15:17 +02:00
/* Who said we shouldn't have same call signature in a COM API? <_< */
2012-09-30 11:26:26 +02:00
#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)
{
struct dinput_input *di = NULL;
2012-09-30 11:26:26 +02:00
if (!dinput_init_context())
{
RARCH_ERR("Failed to start DirectInput driver.\n");
2012-09-30 11:26:26 +02:00
return NULL;
}
2012-09-30 11:26:26 +02:00
di = (struct dinput_input*)calloc(1, sizeof(*di));
2012-09-30 11:26:26 +02:00
if (!di)
return NULL;
#ifdef __cplusplus
if (FAILED(IDirectInput8_CreateDevice(g_ctx, GUID_SysKeyboard, &di->keyboard, NULL)))
2014-01-11 18:09:03 +01:00
{
RARCH_ERR("Failed to create keyboard device.\n");
di->keyboard = NULL;
2014-01-11 18:09:03 +01:00
}
2012-10-28 10:42:20 +01:00
if (FAILED(IDirectInput8_CreateDevice(g_ctx, GUID_SysMouse, &di->mouse, NULL)))
2014-01-11 18:09:03 +01:00
{
RARCH_ERR("Failed to create mouse device.\n");
di->mouse = NULL;
2014-01-11 18:09:03 +01:00
}
2012-09-30 11:26:26 +02:00
#else
if (FAILED(IDirectInput8_CreateDevice(g_ctx, &GUID_SysKeyboard, &di->keyboard, NULL)))
2014-01-11 18:09:03 +01:00
{
RARCH_ERR("Failed to create keyboard device.\n");
di->keyboard = NULL;
2014-01-11 18:09:03 +01:00
}
2012-10-28 10:42:20 +01:00
if (FAILED(IDirectInput8_CreateDevice(g_ctx, &GUID_SysMouse, &di->mouse, NULL)))
2014-01-11 18:09:03 +01:00
{
RARCH_ERR("Failed to create mouse device.\n");
di->mouse = NULL;
2014-01-11 18:09:03 +01:00
}
2012-10-28 10:42:20 +01:00
#endif
2012-09-30 11:26:26 +02:00
if (di->keyboard)
{
IDirectInputDevice8_SetDataFormat(di->keyboard, &c_dfDIKeyboard);
IDirectInputDevice8_SetCooperativeLevel(di->keyboard,
(HWND)driver.video_window, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
IDirectInputDevice8_Acquire(di->keyboard);
}
2012-09-30 11:26:26 +02:00
if (di->mouse)
{
IDirectInputDevice8_SetDataFormat(di->mouse, &c_dfDIMouse2);
IDirectInputDevice8_SetCooperativeLevel(di->mouse, (HWND)driver.video_window,
DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
IDirectInputDevice8_Acquire(di->mouse);
}
2012-10-28 10:42:20 +01:00
input_keymaps_init_keyboard_lut(rarch_key_map_dinput);
2013-05-04 10:22:31 +02:00
di->joypad = input_joypad_init_driver(g_settings.input.joypad_driver);
2012-09-30 11:26:26 +02:00
return di;
}
static void dinput_poll(void *data)
{
struct dinput_input *di = (struct dinput_input*)data;
memset(di->state, 0, sizeof(di->state));
if (di->keyboard)
2012-09-30 11:26:26 +02:00
{
2014-09-09 18:15:17 +02:00
if (FAILED(IDirectInputDevice8_GetDeviceState(
di->keyboard, sizeof(di->state), di->state)))
{
IDirectInputDevice8_Acquire(di->keyboard);
2014-09-09 18:15:17 +02:00
if (FAILED(IDirectInputDevice8_GetDeviceState(
di->keyboard, sizeof(di->state), di->state)))
memset(di->state, 0, sizeof(di->state));
}
2012-09-30 11:26:26 +02:00
}
if (di->mouse)
2012-10-28 10:42:20 +01:00
{
DIMOUSESTATE2 mouse_state;
memset(&mouse_state, 0, sizeof(mouse_state));
2014-09-09 18:15:17 +02:00
if (FAILED(IDirectInputDevice8_GetDeviceState(
di->mouse, sizeof(mouse_state), &mouse_state)))
{
IDirectInputDevice8_Acquire(di->mouse);
2014-09-09 18:15:17 +02:00
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];
di->mouse_wu = mouse_state.rgbButtons[3];
di->mouse_wd = mouse_state.rgbButtons[4];
2014-09-09 18:15:17 +02:00
/* 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-10-28 10:42:20 +01:00
}
if (di->joypad)
di->joypad->poll();
2012-09-30 11:26:26 +02:00
}
static bool dinput_keyboard_pressed(struct dinput_input *di, unsigned key)
{
unsigned sym;
2012-09-30 11:26:26 +02:00
if (key >= RETROK_LAST)
return false;
sym = input_keymaps_translate_rk_to_keysym((enum retro_key)key);
return di->state[sym] & 0x80;
2012-09-30 11:26:26 +02:00
}
2014-09-09 18:15:17 +02:00
static bool dinput_is_pressed(struct dinput_input *di,
const struct retro_keybind *binds,
2012-09-30 11:26:26 +02:00
unsigned port, unsigned id)
{
const struct retro_keybind *bind = &binds[id];
2012-09-30 11:26:26 +02:00
if (id >= RARCH_BIND_LIST_END)
return false;
2014-09-09 18:15:17 +02:00
return dinput_keyboard_pressed(di, bind->key) ||
input_joypad_pressed(di->joypad, port, binds, id);
2012-09-30 11:26:26 +02:00
}
static int16_t dinput_pressed_analog(struct dinput_input *di,
const struct retro_keybind *binds,
2014-10-20 20:31:00 +02:00
unsigned idx, unsigned id)
{
const struct retro_keybind *bind_minus, *bind_plus;
int16_t pressed_minus = 0, pressed_plus = 0;
unsigned id_minus = 0, id_plus = 0;
2014-10-20 20:31:00 +02:00
input_conv_analog_id_to_bind_id(idx, id, &id_minus, &id_plus);
bind_minus = &binds[id_minus];
bind_plus = &binds[id_plus];
if (!bind_minus->valid || !bind_plus->valid)
return 0;
if (dinput_keyboard_pressed(di, bind_minus->key))
pressed_minus = -0x7fff;
if (dinput_keyboard_pressed(di, bind_plus->key))
pressed_plus = 0x7fff;
return pressed_plus + pressed_minus;
}
2012-09-30 11:26:26 +02:00
static bool dinput_key_pressed(void *data, int key)
{
2014-09-09 18:15:17 +02:00
return dinput_is_pressed((struct dinput_input*)data,
g_settings.input.binds[0], 0, key);
2012-09-30 11:26:26 +02:00
}
2012-10-28 10:42:20 +01: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;
}
2014-09-09 18:15:17 +02:00
return 0;
2012-10-28 10:42:20 +01:00
}
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;
case RETRO_DEVICE_ID_MOUSE_WHEELUP:
return di->mouse_wu;
case RETRO_DEVICE_ID_MOUSE_WHEELDOWN:
return di->mouse_wd;
2014-04-25 23:44:53 +02:00
case RETRO_DEVICE_ID_MOUSE_MIDDLE:
return di->mouse_m;
2012-10-28 10:42:20 +01:00
}
2014-09-09 18:15:17 +02:00
return 0;
2012-10-28 10:42:20 +01:00
}
2014-09-09 18:15:17 +02:00
static int16_t dinput_pointer_state(struct dinput_input *di,
2014-10-20 20:31:00 +02:00
unsigned idx, unsigned id, bool screen)
2012-10-28 10:42:20 +01:00
{
bool pointer_down, valid, inside;
int x, y;
2013-11-05 20:49:04 +01:00
int16_t res_x = 0, res_y = 0, res_screen_x = 0, res_screen_y = 0;
unsigned num = 0;
struct pointer_status *check_pos = di->pointer_head.next;
2014-10-20 20:31:00 +02:00
while (check_pos && num < idx)
2013-11-05 20:49:04 +01:00
{
num++;
check_pos = check_pos->next;
}
2014-10-20 20:31:00 +02:00
if (!check_pos && idx > 0) /* idx = 0 has mouse fallback. */
2012-12-27 12:26:13 +01:00
return 0;
x = check_pos ? check_pos->pointer_x : di->mouse_x;
y = check_pos ? check_pos->pointer_y : di->mouse_y;
pointer_down = check_pos ? true : di->mouse_l;
2013-11-05 20:49:04 +01:00
valid = input_translate_coord_viewport(x, y,
2013-01-11 16:23:04 +01:00
&res_x, &res_y, &res_screen_x, &res_screen_y);
2012-10-28 10:42:20 +01:00
if (!valid)
return 0;
2013-01-11 16:23:04 +01:00
if (screen)
{
res_x = res_screen_x;
res_y = res_screen_y;
}
inside = (res_x >= -0x7fff) && (res_y >= -0x7fff);
2012-10-28 10:42:20 +01:00
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:
2013-11-05 20:49:04 +01:00
return pointer_down;
2012-10-28 10:42:20 +01:00
default:
return 0;
}
}
2012-09-30 11:26:26 +02:00
static int16_t dinput_input_state(void *data,
const struct retro_keybind **binds, unsigned port,
2014-10-20 20:31:00 +02:00
unsigned device, unsigned idx, unsigned id)
2012-09-30 11:26:26 +02:00
{
int16_t ret;
struct dinput_input *di = (struct dinput_input*)data;
2012-09-30 11:26:26 +02:00
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:
2014-10-20 20:31:00 +02:00
ret = dinput_pressed_analog(di, binds[port], idx, id);
if (!ret)
2014-09-09 18:15:17 +02:00
ret = input_joypad_analog(di->joypad, port,
2014-10-20 20:31:00 +02:00
idx, id, g_settings.input.binds[port]);
return ret;
2012-09-30 11:26:26 +02:00
2012-10-28 10:42:20 +01:00
case RETRO_DEVICE_MOUSE:
return dinput_mouse_state(di, id);
case RETRO_DEVICE_POINTER:
2013-01-11 16:23:04 +01:00
case RARCH_DEVICE_POINTER_SCREEN:
2014-10-20 20:31:00 +02:00
return dinput_pointer_state(di, idx, id,
2014-09-09 18:15:17 +02:00
device == RARCH_DEVICE_POINTER_SCREEN);
2012-10-28 10:42:20 +01:00
2012-09-30 11:26:26 +02:00
case RETRO_DEVICE_LIGHTGUN:
2012-10-28 10:42:20 +01:00
return dinput_lightgun_state(di, id);
2012-09-30 11:26:26 +02:00
}
return 0;
2012-09-30 11:26:26 +02:00
}
/* These are defined in later SDKs, thus ifdeffed. */
2013-11-05 20:49:04 +01:00
#ifndef WM_POINTERUPDATE
#define WM_POINTERUPDATE 0x0245
#endif
2013-11-05 20:49:04 +01:00
#ifndef WM_POINTERDOWN
#define WM_POINTERDOWN 0x0246
#endif
2013-11-05 20:49:04 +01:00
#ifndef WM_POINTERUP
#define WM_POINTERUP 0x0247
#endif
2013-11-05 20:49:04 +01:00
#ifndef GET_POINTERID_WPARAM
2014-09-09 18:15:17 +02:00
#define GET_POINTERID_WPARAM(wParam) (LOWORD(wParam))
2013-11-05 20:49:04 +01:00
#endif
2014-09-09 18:15:17 +02:00
/* Stores x/y in client coordinates. */
2013-11-05 20:49:04 +01:00
void dinput_pointer_store_pos(struct pointer_status *pointer, WPARAM lParam)
{
POINT point;
2013-11-05 20:49:04 +01:00
point.x = GET_X_LPARAM(lParam);
point.y = GET_Y_LPARAM(lParam);
ScreenToClient((HWND)driver.video_window, &point);
pointer->pointer_x = point.x;
pointer->pointer_y = point.y;
}
2014-09-09 18:15:17 +02:00
void dinput_add_pointer(struct dinput_input *di,
struct pointer_status *new_pointer)
2013-11-05 20:49:04 +01:00
{
struct pointer_status *insert_pos = NULL;
2013-11-05 20:49:04 +01:00
new_pointer->next = NULL;
insert_pos = &di->pointer_head;
2013-11-05 20:49:04 +01:00
while (insert_pos->next)
insert_pos = insert_pos->next;
insert_pos->next = new_pointer;
}
void dinput_delete_pointer(struct dinput_input *di, int pointer_id)
{
struct pointer_status *check_pos = &di->pointer_head;
2013-11-05 20:49:04 +01:00
while (check_pos && check_pos->next)
{
if (check_pos->next->pointer_id == pointer_id)
{
struct pointer_status *to_delete = check_pos->next;
2013-11-05 20:49:04 +01:00
check_pos->next = check_pos->next->next;
free(to_delete);
}
check_pos = check_pos->next;
}
}
2014-09-09 18:15:17 +02:00
struct pointer_status *dinput_find_pointer(struct dinput_input *di,
int pointer_id)
2013-11-05 20:49:04 +01:00
{
struct pointer_status *check_pos = di->pointer_head.next;
2013-11-05 20:49:04 +01:00
while (check_pos)
{
if (check_pos->pointer_id == pointer_id)
break;
check_pos = check_pos->next;
}
2013-11-05 20:49:04 +01:00
return check_pos;
}
void dinput_clear_pointers(struct dinput_input *di)
{
struct pointer_status *pointer = &di->pointer_head;
2013-11-05 20:49:04 +01:00
while (pointer->next)
{
struct pointer_status *del = pointer->next;
2013-11-05 20:49:04 +01:00
pointer->next = pointer->next->next;
free(del);
}
}
#ifdef __cplusplus
extern "C"
#endif
bool dinput_handle_message(void *dinput, UINT message, WPARAM wParam, LPARAM lParam)
{
struct dinput_input *di = (struct dinput_input *)dinput;
2014-09-09 18:15:17 +02:00
/* WM_POINTERDOWN : Arrives for each new touch event
* with a new ID - add to list.
2014-09-09 18:15:17 +02:00
* WM_POINTERUP : Arrives once the pointer is no
* longer down - remove from list.
2014-09-09 18:15:17 +02:00
* WM_POINTERUPDATE : arrives for both pressed and
* hovering pointers - ignore hovering
2013-11-05 20:49:04 +01:00
*/
2014-09-09 18:15:17 +02:00
2013-11-05 20:49:04 +01:00
switch (message)
{
2014-01-16 08:17:20 +01:00
case WM_POINTERDOWN:
{
2014-09-09 18:15:17 +02:00
struct pointer_status *new_pointer =
(struct pointer_status *)malloc(sizeof(struct pointer_status));
if (!new_pointer)
{
RARCH_ERR("dinput_handle_message: pointer allocation in WM_POINTERDOWN failed.\n");
return false;
}
2014-01-16 08:17:20 +01:00
new_pointer->pointer_id = GET_POINTERID_WPARAM(wParam);
dinput_pointer_store_pos(new_pointer, lParam);
dinput_add_pointer(di, new_pointer);
return true;
}
case WM_POINTERUP:
{
int pointer_id = GET_POINTERID_WPARAM(wParam);
dinput_delete_pointer(di, pointer_id);
return true;
}
case WM_POINTERUPDATE:
{
int pointer_id = GET_POINTERID_WPARAM(wParam);
struct pointer_status *pointer = dinput_find_pointer(di, pointer_id);
if (pointer)
dinput_pointer_store_pos(pointer, lParam);
return true;
}
case WM_DEVICECHANGE:
{
if (di->joypad)
di->joypad->destroy();
di->joypad = input_joypad_init_driver(g_settings.input.joypad_driver);
break;
}
2013-11-05 20:49:04 +01:00
}
return false;
}
2012-09-30 11:26:26 +02:00
static void dinput_free(void *data)
{
struct dinput_input *di = (struct dinput_input*)data;
LPDIRECTINPUT8 hold_ctx = g_ctx;
if (di)
{
2014-09-09 18:15:17 +02:00
/* Prevent a joypad driver to kill our context prematurely. */
g_ctx = NULL;
2014-01-16 08:17:20 +01:00
if (di->joypad)
di->joypad->destroy();
2012-09-30 11:26:26 +02:00
g_ctx = hold_ctx;
2014-09-09 18:15:17 +02:00
/* Clear any leftover pointers. */
dinput_clear_pointers(di);
2013-11-05 20:49:04 +01:00
2012-09-30 11:26:26 +02:00
if (di->keyboard)
IDirectInputDevice8_Release(di->keyboard);
2012-10-28 10:42:20 +01:00
if (di->mouse)
IDirectInputDevice8_Release(di->mouse);
2012-09-30 11:26:26 +02:00
free(di);
}
dinput_destroy_context();
}
2013-04-04 22:35:05 +02:00
static void dinput_grab_mouse(void *data, bool state)
{
struct dinput_input *di = (struct dinput_input*)data;
2013-04-04 22:35:05 +02:00
IDirectInputDevice8_Unacquire(di->mouse);
IDirectInputDevice8_SetCooperativeLevel(di->mouse,
(HWND)driver.video_window,
state ?
(DISCL_EXCLUSIVE | DISCL_FOREGROUND) :
2013-04-04 22:56:52 +02:00
(DISCL_NONEXCLUSIVE | DISCL_FOREGROUND));
2013-04-04 22:35:05 +02:00
IDirectInputDevice8_Acquire(di->mouse);
}
2014-09-09 18:15:17 +02:00
static bool dinput_set_rumble(void *data, unsigned port,
enum retro_rumble_effect effect, uint16_t strength)
{
struct dinput_input *di = (struct dinput_input*)data;
if (!di)
return false;
return input_joypad_set_rumble(di->joypad, port, effect, strength);
}
static const rarch_joypad_driver_t *dinput_get_joypad_driver(void *data)
{
struct dinput_input *di = (struct dinput_input*)data;
if (!di)
return false;
return di->joypad;
}
2013-11-02 23:36:06 +01:00
static uint64_t dinput_get_capabilities(void *data)
{
uint64_t caps = 0;
caps |= (1 << RETRO_DEVICE_JOYPAD);
caps |= (1 << RETRO_DEVICE_MOUSE);
caps |= (1 << RETRO_DEVICE_KEYBOARD);
caps |= (1 << RETRO_DEVICE_LIGHTGUN);
caps |= (1 << RETRO_DEVICE_POINTER);
caps |= (1 << RETRO_DEVICE_ANALOG);
return caps;
}
2014-09-11 07:37:39 +02:00
input_driver_t input_dinput = {
2012-09-30 11:26:26 +02:00
dinput_init,
dinput_poll,
dinput_input_state,
dinput_key_pressed,
dinput_free,
2013-03-15 10:43:42 +01:00
NULL,
NULL,
2013-11-02 23:36:06 +01:00
dinput_get_capabilities,
2012-09-30 11:26:26 +02:00
"dinput",
2013-04-04 22:35:05 +02:00
dinput_grab_mouse,
dinput_set_rumble,
dinput_get_joypad_driver,
2012-09-30 11:26:26 +02:00
};
2014-09-09 18:15:17 +02:00
/* Keep track of which pad indexes are 360 controllers.
* Not static, will be read in winxinput_joypad.c
* -1 = not xbox pad, otherwise 0..3
*/
2015-01-05 01:58:00 +01:00
int g_xinput_pad_indexes[MAX_USERS];
bool g_xinput_block_pads;
2012-09-30 11:26:26 +02:00
static void dinput_joypad_destroy(void)
2011-06-10 16:55:05 +02:00
{
2013-10-22 21:26:33 +02:00
unsigned i;
2015-01-05 01:58:00 +01:00
for (i = 0; i < MAX_USERS; i++)
2011-06-10 16:55:05 +02:00
{
2012-09-28 22:38:42 +02:00
if (g_pads[i].joypad)
2011-06-10 16:55:05 +02:00
{
2012-09-28 22:38:42 +02:00
IDirectInputDevice8_Unacquire(g_pads[i].joypad);
IDirectInputDevice8_Release(g_pads[i].joypad);
2011-06-10 16:55:05 +02:00
}
2013-08-29 09:27:13 +01:00
free(g_pads[i].joy_name);
g_pads[i].joy_name = NULL;
2014-01-16 00:04:56 +01:00
*g_settings.input.device_names[i] = '\0';
2012-09-28 22:38:42 +02:00
}
2011-06-10 16:55:05 +02:00
2012-09-28 22:38:42 +02:00
g_joypad_cnt = 0;
memset(g_pads, 0, sizeof(g_pads));
2012-09-30 11:26:26 +02:00
2014-09-09 18:15:17 +02:00
/* Can be blocked by global Dinput context. */
2012-09-30 11:26:26 +02:00
dinput_destroy_context();
2011-06-10 16:55:05 +02:00
}
2014-09-09 18:15:17 +02:00
static BOOL CALLBACK enum_axes_cb(
const DIDEVICEOBJECTINSTANCE *inst, void *p)
2011-06-10 16:55:05 +02:00
{
DIPROPRANGE range;
2011-12-24 13:46:12 +01:00
LPDIRECTINPUTDEVICE8 joypad = (LPDIRECTINPUTDEVICE8)p;
2011-06-10 16:55:05 +02:00
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 11:26:26 +02:00
range.lMin = -0x7fff;
range.lMax = 0x7fff;
2011-06-10 16:55:05 +02:00
IDirectInputDevice8_SetProperty(joypad, DIPROP_RANGE, &range.diph);
return DIENUM_CONTINUE;
}
static const GUID common_xinput_guids[] = {
{MAKELONG(0x28DE, 0x11FF),0x0000,0x0000,{0x00,0x00,0x50,0x49,0x44,0x56,0x49,0x44}}, /* Valve streaming pad */
{MAKELONG(0x045E, 0x02A1),0x0000,0x0000,{0x00,0x00,0x50,0x49,0x44,0x56,0x49,0x44}}, /* Wired 360 pad */
{MAKELONG(0x045E, 0x028E),0x0000,0x0000,{0x00,0x00,0x50,0x49,0x44,0x56,0x49,0x44}} /* wireless 360 pad */
2013-08-25 17:17:23 +01:00
};
2014-09-09 18:15:17 +02:00
/* Based on SDL2's implementation. */
static bool guid_is_xinput_device(const GUID* product_guid)
2013-08-25 17:17:23 +01:00
{
unsigned i, num_raw_devs = 0;
PRAWINPUTDEVICELIST raw_devs = NULL;
2014-09-09 18:15:17 +02:00
/* Check for well known XInput device GUIDs,
* thereby removing the need for the IG_ check.
* This lets us skip RAWINPUT for popular devices.
*
* Also, we need to do this for the Valve Streaming Gamepad
* because it's virtualized and doesn't show up in the device list. */
for (i = 0; i < ARRAY_SIZE(common_xinput_guids); ++i)
2013-08-25 17:17:23 +01:00
{
if (memcmp(product_guid, &common_xinput_guids[i], sizeof(GUID)) == 0)
2013-08-25 17:17:23 +01:00
return true;
}
2013-10-22 21:26:33 +02:00
/* Go through RAWINPUT (WinXP and later) to find HID devices. */
if (!raw_devs)
{
2014-09-09 18:15:17 +02:00
if ((GetRawInputDeviceList(NULL, &num_raw_devs,
sizeof(RAWINPUTDEVICELIST)) == (UINT)-1) || (!num_raw_devs))
return false;
2014-09-09 18:15:17 +02:00
raw_devs = (PRAWINPUTDEVICELIST)
malloc(sizeof(RAWINPUTDEVICELIST) * num_raw_devs);
if (!raw_devs)
return false;
2014-09-09 18:15:17 +02:00
if (GetRawInputDeviceList(raw_devs, &num_raw_devs,
sizeof(RAWINPUTDEVICELIST)) == (UINT)-1)
{
free(raw_devs);
raw_devs = NULL;
return false;
}
}
for (i = 0; i < num_raw_devs; i++)
{
RID_DEVICE_INFO rdi;
char devName[128];
UINT rdiSize = sizeof(rdi);
UINT nameSize = sizeof(devName);
rdi.cbSize = sizeof (rdi);
if ((raw_devs[i].dwType == RIM_TYPEHID) &&
(GetRawInputDeviceInfoA(raw_devs[i].hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) != ((UINT)-1)) &&
(MAKELONG(rdi.hid.dwVendorId, rdi.hid.dwProductId) == ((LONG)product_guid->Data1)) &&
(GetRawInputDeviceInfoA(raw_devs[i].hDevice, RIDI_DEVICENAME, devName, &nameSize) != ((UINT)-1)) &&
(strstr(devName, "IG_") != NULL) )
{
2014-02-04 19:59:15 +00:00
free(raw_devs);
raw_devs = NULL;
return true;
}
}
2014-02-04 19:59:15 +00:00
free(raw_devs);
raw_devs = NULL;
2013-10-22 21:26:33 +02:00
return false;
2013-08-25 17:17:23 +01:00
}
2014-09-09 18:15:17 +02:00
/* Forward declaration */
static const char *dinput_joypad_name(unsigned pad);
2014-09-09 18:15:17 +02:00
2014-10-20 20:31:00 +02:00
static unsigned g_last_xinput_pad_idx;
2011-06-10 16:55:05 +02:00
static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p)
{
bool is_xinput_pad;
LPDIRECTINPUTDEVICE8 *pad = NULL;
2012-09-28 22:38:42 +02:00
(void)p;
2015-01-05 01:58:00 +01:00
if (g_joypad_cnt == MAX_USERS)
2012-09-28 22:38:42 +02:00
return DIENUM_STOP;
2011-06-10 16:55:05 +02:00
pad = &g_pads[g_joypad_cnt].joypad;
2011-06-10 16:55:05 +02:00
2011-12-24 13:46:12 +01:00
#ifdef __cplusplus
2014-09-09 18:15:17 +02:00
if (FAILED(IDirectInput8_CreateDevice(
g_ctx, inst->guidInstance, pad, NULL)))
2011-12-24 13:46:12 +01:00
#else
2014-09-09 18:15:17 +02:00
if (FAILED(IDirectInput8_CreateDevice(
g_ctx, &inst->guidInstance, pad, NULL)))
2011-12-24 13:46:12 +01:00
#endif
2013-08-23 13:09:59 +01:00
return DIENUM_CONTINUE;
2013-08-29 09:27:13 +01:00
g_pads[g_joypad_cnt].joy_name = strdup(inst->tszProductName);
2013-08-26 12:13:41 +01:00
#ifdef HAVE_WINXINPUT
2014-09-09 18:15:17 +02:00
#if 0
is_xinput_pad = g_xinput_block_pads
2014-09-09 18:15:17 +02:00
&& name_is_xinput_pad(inst->tszProductName);
#endif
is_xinput_pad = g_xinput_block_pads
2014-09-09 18:15:17 +02:00
&& guid_is_xinput_device(&inst->guidProduct);
2013-08-23 13:09:59 +01:00
if (is_xinput_pad)
2013-08-23 13:09:59 +01:00
{
2014-10-20 20:31:00 +02:00
if (g_last_xinput_pad_idx < 4)
g_xinput_pad_indexes[g_joypad_cnt] = g_last_xinput_pad_idx++;
goto enum_iteration_done;
2013-08-23 13:09:59 +01:00
}
#endif
2011-06-10 16:55:05 +02:00
2012-09-28 22:38:42 +02:00
IDirectInputDevice8_SetDataFormat(*pad, &c_dfDIJoystick2);
IDirectInputDevice8_SetCooperativeLevel(*pad, (HWND)driver.video_window,
2011-06-10 16:55:05 +02:00
DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
2012-09-28 22:38:42 +02:00
IDirectInputDevice8_EnumObjects(*pad, enum_axes_cb,
*pad, DIDFT_ABSAXIS);
#ifdef HAVE_WINXINPUT
if (!is_xinput_pad)
#endif
{
2014-09-09 18:15:17 +02:00
strlcpy(g_settings.input.device_names[g_joypad_cnt],
dinput_joypad_name(g_joypad_cnt),
sizeof(g_settings.input.device_names[g_joypad_cnt]));
/* TODO - implement VID/PID? */
2014-09-09 18:15:17 +02:00
input_config_autoconfigure_joypad(g_joypad_cnt,
dinput_joypad_name(g_joypad_cnt), 0, 0,
2014-09-09 18:15:17 +02:00
dinput_joypad.ident);
}
enum_iteration_done:
2012-09-28 22:38:42 +02:00
g_joypad_cnt++;
2011-06-10 16:55:05 +02:00
return DIENUM_CONTINUE;
}
2012-09-30 11:26:26 +02:00
static bool dinput_joypad_init(void)
2011-06-10 16:55:05 +02:00
{
2013-10-22 21:26:33 +02:00
unsigned i;
2012-09-30 11:26:26 +02:00
if (!dinput_init_context())
2012-09-28 22:38:42 +02:00
return false;
2014-10-20 20:31:00 +02:00
g_last_xinput_pad_idx = 0;
2015-01-05 01:58:00 +01:00
for (i = 0; i < MAX_USERS; ++i)
{
g_xinput_pad_indexes[i] = -1;
g_pads[i].joy_name = NULL;
}
2011-06-10 16:55:05 +02:00
2012-09-30 11:26:26 +02: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 22:38:42 +02:00
return true;
2011-06-10 16:55:05 +02:00
}
2012-09-30 11:26:26 +02:00
static bool dinput_joypad_button(unsigned port_num, uint16_t joykey)
2011-06-10 16:55:05 +02:00
{
const struct dinput_joypad *pad = NULL;
2011-06-10 16:55:05 +02:00
if (joykey == NO_BTN)
return false;
pad = &g_pads[port_num];
if (!pad->joypad)
return false;
2012-09-28 22:38:42 +02:00
/* Check hat. */
2011-06-10 16:55:05 +02:00
if (GET_HAT_DIR(joykey))
{
unsigned pov;
unsigned hat = GET_HAT(joykey);
2014-09-09 18:15:17 +02:00
unsigned elems = sizeof(pad->joy_state.rgdwPOV) /
sizeof(pad->joy_state.rgdwPOV[0]);
2011-06-10 16:55:05 +02:00
if (hat >= elems)
return false;
pov = pad->joy_state.rgdwPOV[hat];
2011-06-10 16:55:05 +02:00
/* Magic numbers I'm not sure where originate from. */
2011-06-10 16:55:05 +02:00
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
{
2014-09-09 18:15:17 +02:00
unsigned elems = sizeof(pad->joy_state.rgbButtons) /
sizeof(pad->joy_state.rgbButtons[0]);
2011-06-10 16:55:05 +02:00
if (joykey < elems)
2012-09-28 22:38:42 +02:00
return pad->joy_state.rgbButtons[joykey];
2011-06-10 16:55:05 +02:00
}
return false;
}
2012-09-30 11:26:26 +02:00
static int16_t dinput_joypad_axis(unsigned port_num, uint32_t joyaxis)
2011-06-10 16:55:05 +02:00
{
const struct dinput_joypad *pad = NULL;
int val = 0;
int axis = -1;
bool is_neg = false;
bool is_pos = false;
2011-06-10 16:55:05 +02:00
if (joyaxis == AXIS_NONE)
2012-06-28 17:57:50 +02:00
return 0;
2011-06-10 16:55:05 +02:00
pad = &g_pads[port_num];
if (!pad->joypad)
return 0;
2012-09-28 22:38:42 +02:00
2012-06-28 17:57:50 +02:00
if (AXIS_NEG_GET(joyaxis) <= 5)
2011-06-10 16:55:05 +02:00
{
2012-06-28 17:57:50 +02: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 16:55:05 +02:00
}
2012-06-28 17:57:50 +02:00
switch (axis)
2011-06-10 16:55:05 +02:00
{
2014-09-09 18:15:17 +02: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 16:55:05 +02:00
}
2012-06-28 17:57:50 +02:00
if (is_neg && val > 0)
val = 0;
else if (is_pos && val < 0)
val = 0;
return val;
}
2012-09-30 11:26:26 +02:00
static void dinput_joypad_poll(void)
2011-06-10 16:55:05 +02:00
{
2013-10-22 21:26:33 +02:00
unsigned i;
2015-01-05 01:58:00 +01:00
for (i = 0; i < MAX_USERS; i++)
2011-06-10 16:55:05 +02:00
{
2012-09-28 22:38:42 +02:00
struct dinput_joypad *pad = &g_pads[i];
bool polled = g_xinput_pad_indexes[i] < 0;
if (!pad || !pad->joypad || !polled)
continue;
2012-09-28 22:38:42 +02:00
memset(&pad->joy_state, 0, sizeof(pad->joy_state));
if (FAILED(IDirectInputDevice8_Poll(pad->joypad)))
2011-06-10 16:55:05 +02:00
{
if (FAILED(IDirectInputDevice8_Acquire(pad->joypad)))
{
memset(&pad->joy_state, 0, sizeof(DIJOYSTATE2));
continue;
}
2012-09-28 22:38:42 +02:00
/* If this fails, something *really* bad must have happened. */
2012-09-28 22:38:42 +02:00
if (FAILED(IDirectInputDevice8_Poll(pad->joypad)))
2011-06-10 16:55:05 +02:00
{
memset(&pad->joy_state, 0, sizeof(DIJOYSTATE2));
continue;
2011-06-10 16:55:05 +02:00
}
}
IDirectInputDevice8_GetDeviceState(pad->joypad,
sizeof(DIJOYSTATE2), &pad->joy_state);
2011-06-10 16:55:05 +02:00
}
}
2012-09-30 11:26:26 +02:00
static bool dinput_joypad_query_pad(unsigned pad)
{
2015-01-05 01:58:00 +01:00
return pad < MAX_USERS && g_pads[pad].joypad;
}
static const char *dinput_joypad_name(unsigned pad)
{
2015-01-05 01:58:00 +01:00
if (pad < MAX_USERS)
return g_pads[pad].joy_name;
return NULL;
}
2014-09-11 07:06:20 +02:00
rarch_joypad_driver_t dinput_joypad = {
2012-09-30 11:26:26 +02:00
dinput_joypad_init,
dinput_joypad_query_pad,
dinput_joypad_destroy,
dinput_joypad_button,
dinput_joypad_axis,
dinput_joypad_poll,
NULL,
dinput_joypad_name,
2012-09-30 11:26:26 +02:00
"dinput",
2012-09-28 22:38:42 +02:00
};