1054 lines
31 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
2020-08-30 19:04:50 +02:00
* Copyright (C) 2011-2020 - Daniel De Matteis
*
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
2015-03-12 22:47:58 -05:00
2012-09-28 22:38:42 +02:00
#include <dinput.h>
#include <dbt.h>
2012-09-28 22:38:42 +02:00
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <boolean.h>
#include <windowsx.h>
2018-01-20 20:05:32 +01:00
#ifndef WM_MOUSEHWHEEL
2020-01-19 04:56:12 +01:00
#define WM_MOUSEHWHEEL 0x20e
2018-01-20 20:05:32 +01:00
#endif
#ifndef WM_MOUSEWHEEL
2020-01-19 04:56:12 +01:00
#define WM_MOUSEWHEEL 0x020A
2018-01-20 20:05:32 +01:00
#endif
2016-09-11 13:21:56 +02:00
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
2017-01-10 20:11:51 +01:00
#include <string/stdstring.h>
#ifndef _XBOX
#include "../../gfx/common/win32_common.h"
#endif
2015-01-12 06:16:52 +01:00
#include "../input_keymaps.h"
2011-06-10 16:55:05 +02:00
#include "../../configuration.h"
#include "../../retroarch.h"
2017-01-10 20:11:51 +01:00
#include "../../verbosity.h"
2014-09-09 18:15:17 +02:00
/* Context has to be global as joypads also ride on this context. */
LPDIRECTINPUT8 g_dinput_ctx;
2012-09-28 22:38:42 +02:00
struct dinput_pointer_status
2013-11-05 20:49:04 +01:00
{
struct dinput_pointer_status *next;
2013-11-05 20:49:04 +01:00
int pointer_id;
int pointer_x;
int pointer_y;
};
2012-09-30 11:26:26 +02:00
struct dinput_input
{
char *joypad_driver_name;
2012-09-30 11:26:26 +02:00
LPDIRECTINPUTDEVICE8 keyboard;
2012-10-28 10:42:20 +01:00
LPDIRECTINPUTDEVICE8 mouse;
const input_device_driver_t *joypad;
struct dinput_pointer_status pointer_head; /* dummy head for easier iteration */
2012-10-28 10:42:20 +01:00
int window_pos_x;
int window_pos_y;
2017-03-31 17:28:49 +02:00
int mouse_rel_x;
int mouse_rel_y;
2012-10-28 10:42:20 +01:00
int mouse_x;
int mouse_y;
2020-08-14 18:31:28 +02:00
uint8_t state[256];
2020-11-09 15:19:41 +02:00
bool shift_l, shift_r, alt_l;
bool doubleclick_on_titlebar;
2017-11-17 12:00:24 +00:00
bool mouse_l, mouse_r, mouse_m, mouse_b4, mouse_b5, mouse_wu, mouse_wd, mouse_hwu, mouse_hwd;
2012-09-30 11:26:26 +02:00
};
2015-04-08 02:02:10 +02:00
void dinput_destroy_context(void)
2012-09-30 11:26:26 +02:00
{
if (!g_dinput_ctx)
return;
IDirectInput8_Release(g_dinput_ctx);
g_dinput_ctx = NULL;
2012-09-30 11:26:26 +02:00
}
2015-04-08 02:04:02 +02:00
bool dinput_init_context(void)
2012-09-30 11:26:26 +02:00
{
if (g_dinput_ctx)
2012-09-30 11:26:26 +02:00
return true;
2014-09-09 18:15:17 +02:00
/* Who said we shouldn't have same call signature in a COM API? <_< */
2017-08-09 15:51:27 +02:00
#ifdef __cplusplus
2017-08-09 16:53:06 +02:00
if (!(SUCCEEDED(DirectInput8Create(
GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8,
(void**)&g_dinput_ctx, NULL))))
2015-11-14 21:58:19 +01:00
#else
2017-08-09 16:53:06 +02:00
if (!(SUCCEEDED(DirectInput8Create(
GetModuleHandle(NULL), DIRECTINPUT_VERSION,
&IID_IDirectInput8,
(void**)&g_dinput_ctx, NULL))))
2015-11-14 21:58:19 +01:00
#endif
2020-09-02 05:09:37 +02:00
return false;
2012-09-30 11:26:26 +02:00
return true;
}
static void *dinput_init(const char *joypad_driver)
2012-09-30 11:26:26 +02:00
{
struct dinput_input *di = NULL;
2012-09-30 11:26:26 +02:00
if (!dinput_init_context())
return NULL;
di = (struct dinput_input*)calloc(1, sizeof(*di));
2012-09-30 11:26:26 +02:00
if (!di)
return NULL;
if (!string_is_empty(joypad_driver))
di->joypad_driver_name = strdup(joypad_driver);
2015-11-14 21:58:19 +01:00
#ifdef __cplusplus
2017-08-09 16:53:06 +02:00
if (FAILED(IDirectInput8_CreateDevice(g_dinput_ctx,
2017-08-09 15:51:27 +02:00
GUID_SysKeyboard,
2017-08-09 16:53:06 +02:00
&di->keyboard, NULL)))
2017-08-09 15:51:27 +02:00
#else
2017-08-09 16:53:06 +02:00
if (FAILED(IDirectInput8_CreateDevice(g_dinput_ctx,
2017-08-09 15:51:27 +02:00
&GUID_SysKeyboard,
&di->keyboard, NULL)))
2017-08-09 16:53:06 +02:00
#endif
2014-01-11 18:09:03 +01:00
{
di->keyboard = NULL;
2014-01-11 18:09:03 +01:00
}
2017-08-09 15:51:27 +02:00
#ifdef __cplusplus
2017-08-09 16:53:06 +02:00
if (FAILED(IDirectInput8_CreateDevice(g_dinput_ctx,
2017-08-09 15:51:27 +02:00
GUID_SysMouse,
2017-08-09 16:53:06 +02:00
&di->mouse, NULL)))
2015-11-14 21:58:19 +01:00
#else
2017-08-09 16:53:06 +02:00
if (FAILED(IDirectInput8_CreateDevice(g_dinput_ctx,
2017-08-09 15:51:27 +02:00
&GUID_SysMouse,
&di->mouse, NULL)))
2017-08-09 16:53:06 +02:00
#endif
2015-11-14 21:58:19 +01:00
{
di->mouse = NULL;
}
2012-09-30 11:26:26 +02:00
if (di->keyboard)
{
DWORD flags;
settings_t *settings;
settings = config_get_ptr();
flags = DISCL_NONEXCLUSIVE | DISCL_FOREGROUND;
if (settings->bools.input_nowinkey_enable)
flags |= DISCL_NOWINKEY;
IDirectInputDevice8_SetDataFormat(di->keyboard, &c_dfDIKeyboard);
IDirectInputDevice8_SetCooperativeLevel(di->keyboard,
(HWND)video_driver_window_get(), flags);
IDirectInputDevice8_Acquire(di->keyboard);
}
2012-09-30 11:26:26 +02:00
if (di->mouse)
{
2017-03-31 17:28:49 +02:00
IDirectInputDevice8_SetDataFormat(di->mouse, &c_dfDIMouse2);
IDirectInputDevice8_SetCooperativeLevel(di->mouse, (HWND)video_driver_window_get(),
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);
2012-09-30 11:26:26 +02:00
#ifndef _XBOX
2022-05-17 13:21:56 +02:00
SetWindowLongPtr(main_window.hwnd, GWLP_USERDATA, (LONG_PTR)di);
#endif
2012-09-30 11:26:26 +02:00
return di;
}
2020-11-09 15:19:41 +02:00
static void dinput_keyboard_mods(struct dinput_input *di, int mod)
{
switch (mod)
{
case RETROKMOD_SHIFT:
{
unsigned vk_shift_l = GetAsyncKeyState(VK_LSHIFT) >> 1;
unsigned vk_shift_r = GetAsyncKeyState(VK_RSHIFT) >> 1;
if ( (vk_shift_l && !di->shift_l) ||
(!vk_shift_l && di->shift_l))
{
input_keyboard_event(vk_shift_l, RETROK_LSHIFT,
0, RETROKMOD_SHIFT, RETRO_DEVICE_KEYBOARD);
di->shift_l = !di->shift_l;
}
if ( (vk_shift_r && !di->shift_r) ||
(!vk_shift_r && di->shift_r))
{
input_keyboard_event(vk_shift_r, RETROK_RSHIFT,
0, RETROKMOD_SHIFT, RETRO_DEVICE_KEYBOARD);
di->shift_r = !di->shift_r;
}
}
break;
case RETROKMOD_ALT:
{
unsigned vk_alt_l = GetAsyncKeyState(VK_LMENU) >> 1;
if (vk_alt_l && !di->alt_l)
di->alt_l = !di->alt_l;
else if (!vk_alt_l && di->alt_l)
{
input_keyboard_event(vk_alt_l, RETROK_LALT,
0, RETROKMOD_ALT, RETRO_DEVICE_KEYBOARD);
di->alt_l = !di->alt_l;
}
}
break;
}
}
static void dinput_poll(void *data)
2012-09-30 11:26:26 +02:00
{
struct dinput_input *di = (struct dinput_input*)data;
uint8_t *kb_state = NULL;
2012-09-30 11:26:26 +02:00
2016-11-30 02:07:17 -05:00
if (!di)
return;
kb_state = &di->state[0];
for (
; kb_state < di->state + 256
; kb_state++)
*kb_state = 0;
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)))
2020-06-29 02:37:06 +02:00
{
for (
; kb_state < di->state + 256
; kb_state++)
*kb_state = 0;
2020-06-29 02:37:06 +02:00
}
}
2020-11-09 15:19:41 +02:00
else
{
2020-11-09 15:19:41 +02:00
/* Shifts only when window focused */
dinput_keyboard_mods(di, RETROKMOD_SHIFT);
/* Ignore 'unknown/undefined' key */
di->state[RETROK_UNKNOWN] = 0;
}
2020-11-09 15:19:41 +02:00
/* Left alt keyup when unfocused, to prevent alt-tab sticky */
dinput_keyboard_mods(di, RETROKMOD_ALT);
2012-09-30 11:26:26 +02:00
}
if (di->mouse)
2012-10-28 10:42:20 +01:00
{
2019-06-21 09:00:59 +02:00
POINT point;
DIMOUSESTATE2 mouse_state;
BYTE *rgb_buttons_ptr = &mouse_state.rgbButtons[0];
2019-06-21 09:00:59 +02:00
point.x = 0;
point.y = 0;
2017-08-12 18:15:26 +02:00
2020-06-29 02:37:06 +02:00
mouse_state.lX = 0;
mouse_state.lY = 0;
mouse_state.lZ = 0;
for (
; rgb_buttons_ptr < mouse_state.rgbButtons + 8
; rgb_buttons_ptr++)
*rgb_buttons_ptr = 0;
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)))
2020-06-29 02:37:06 +02:00
{
mouse_state.lX = 0;
mouse_state.lY = 0;
mouse_state.lZ = 0;
for (
; rgb_buttons_ptr < mouse_state.rgbButtons + 8
; rgb_buttons_ptr++)
*rgb_buttons_ptr = 0;
2020-06-29 02:37:06 +02:00
}
}
2017-03-31 17:28:49 +02:00
di->mouse_rel_x = mouse_state.lX;
di->mouse_rel_y = mouse_state.lY;
2017-08-12 18:15:26 +02:00
if (!mouse_state.rgbButtons[0])
di->doubleclick_on_titlebar = false;
if (di->doubleclick_on_titlebar)
di->mouse_l = 0;
else
2016-07-16 17:44:21 +02:00
di->mouse_l = mouse_state.rgbButtons[0];
di->mouse_r = mouse_state.rgbButtons[1];
di->mouse_m = mouse_state.rgbButtons[2];
2017-11-17 12:00:24 +00:00
di->mouse_b4 = mouse_state.rgbButtons[3];
di->mouse_b5 = mouse_state.rgbButtons[4];
/* No simple way to get absolute coordinates
2014-09-09 18:15:17 +02:00
* for RETRO_DEVICE_POINTER. Just use Win32 APIs. */
GetCursorPos(&point);
ScreenToClient((HWND)video_driver_window_get(), &point);
di->mouse_x = point.x;
di->mouse_y = point.y;
2012-10-28 10:42:20 +01:00
}
2012-09-30 11:26:26 +02:00
}
2019-06-21 09:00:59 +02:00
static bool dinput_mouse_button_pressed(
struct dinput_input *di, unsigned port, unsigned key)
{
bool result;
2019-07-08 02:40:09 +02:00
switch (key)
2019-06-21 09:00:59 +02:00
{
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_MIDDLE:
return di->mouse_m;
case RETRO_DEVICE_ID_MOUSE_BUTTON_4:
return di->mouse_b4;
case RETRO_DEVICE_ID_MOUSE_BUTTON_5:
return di->mouse_b5;
2019-06-21 09:00:59 +02:00
case RETRO_DEVICE_ID_MOUSE_WHEELUP:
result = di->mouse_wu;
di->mouse_wu = false;
return result;
2019-06-21 09:00:59 +02:00
case RETRO_DEVICE_ID_MOUSE_WHEELDOWN:
result = di->mouse_wd;
di->mouse_wd = false;
return result;
case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELUP:
result = di->mouse_hwu;
di->mouse_hwu = false;
return result;
case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN:
result = di->mouse_hwd;
di->mouse_hwd = false;
return result;
}
return false;
}
2020-06-11 06:34:59 +02:00
static int16_t dinput_lightgun_aiming_state(
struct dinput_input *di, unsigned idx, unsigned id)
2012-10-28 10:42:20 +01:00
{
2019-07-08 02:40:09 +02:00
struct video_viewport vp;
2020-06-11 16:49:27 +02:00
const int edge_detect = 32700;
bool inside = false;
int16_t res_x = 0;
int16_t res_y = 0;
int16_t res_screen_x = 0;
int16_t res_screen_y = 0;
int x = 0;
int y = 0;
unsigned num = 0;
struct dinput_pointer_status
2020-06-11 16:49:27 +02:00
*check_pos = di->pointer_head.next;
vp.x = 0;
vp.y = 0;
vp.width = 0;
vp.height = 0;
vp.full_width = 0;
vp.full_height = 0;
2019-07-08 02:40:09 +02:00
2020-02-23 06:05:23 +01:00
while (check_pos && num < idx)
2019-07-08 02:40:09 +02:00
{
num++;
check_pos = check_pos->next;
}
2019-06-23 03:14:21 +02:00
2019-07-08 02:40:09 +02:00
if (!check_pos && idx > 0) /* idx = 0 has mouse fallback. */
return 0;
2019-06-23 03:14:21 +02:00
2019-07-08 02:40:09 +02:00
x = di->mouse_x;
y = di->mouse_y;
2019-06-23 03:14:21 +02:00
2019-07-08 02:40:09 +02:00
if (check_pos)
{
x = check_pos->pointer_x;
y = check_pos->pointer_y;
}
2020-02-23 06:05:23 +01:00
if (!(video_driver_translate_coord_viewport_wrap(
2020-06-11 16:49:27 +02:00
&vp, x, y,
&res_x, &res_y, &res_screen_x, &res_screen_y)))
2019-07-08 02:40:09 +02:00
return 0;
2020-06-11 16:49:27 +02:00
inside = (res_x >= -edge_detect)
&& (res_y >= -edge_detect)
&& (res_x <= edge_detect)
&& (res_y <= edge_detect);
2019-07-08 02:40:09 +02:00
2020-06-11 16:49:27 +02:00
switch ( id )
2019-07-08 02:40:09 +02:00
{
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X:
2020-06-11 16:49:27 +02:00
if (inside)
return res_x;
break;
2019-07-08 02:40:09 +02:00
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y:
2020-06-11 16:49:27 +02:00
if (inside)
return res_y;
break;
2019-07-08 02:40:09 +02:00
case RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN:
return !inside;
default:
break;
}
return 0;
2012-10-28 10:42:20 +01:00
}
static unsigned dinput_retro_id_to_rarch(unsigned id)
{
switch (id)
{
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_RIGHT:
return RARCH_LIGHTGUN_DPAD_RIGHT;
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_LEFT:
return RARCH_LIGHTGUN_DPAD_LEFT;
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_UP:
return RARCH_LIGHTGUN_DPAD_UP;
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_DOWN:
return RARCH_LIGHTGUN_DPAD_DOWN;
case RETRO_DEVICE_ID_LIGHTGUN_SELECT:
return RARCH_LIGHTGUN_SELECT;
case RETRO_DEVICE_ID_LIGHTGUN_PAUSE:
return RARCH_LIGHTGUN_START;
case RETRO_DEVICE_ID_LIGHTGUN_RELOAD:
return RARCH_LIGHTGUN_RELOAD;
case RETRO_DEVICE_ID_LIGHTGUN_TRIGGER:
return RARCH_LIGHTGUN_TRIGGER;
case RETRO_DEVICE_ID_LIGHTGUN_AUX_A:
return RARCH_LIGHTGUN_AUX_A;
case RETRO_DEVICE_ID_LIGHTGUN_AUX_B:
return RARCH_LIGHTGUN_AUX_B;
case RETRO_DEVICE_ID_LIGHTGUN_AUX_C:
return RARCH_LIGHTGUN_AUX_C;
case RETRO_DEVICE_ID_LIGHTGUN_START:
return RARCH_LIGHTGUN_START;
default:
break;
}
return 0;
}
2020-08-30 05:29:32 +02:00
static int16_t dinput_input_state(
void *data,
const input_device_driver_t *joypad,
const input_device_driver_t *sec_joypad,
2020-02-27 07:33:14 +01:00
rarch_joypad_info_t *joypad_info,
const retro_keybind_set *binds,
bool keyboard_mapping_blocked,
unsigned port,
unsigned device,
unsigned idx,
unsigned id)
2012-09-30 11:26:26 +02:00
{
settings_t *settings;
2022-06-04 08:55:35 +02:00
int16_t ret = 0;
struct dinput_input *di = (struct dinput_input*)data;
if (port >= MAX_USERS)
return 0;
2012-09-30 11:26:26 +02:00
switch (device)
{
case RETRO_DEVICE_JOYPAD:
2019-07-08 02:40:09 +02:00
{
settings = config_get_ptr();
2019-07-21 17:53:51 +02:00
if (id == RETRO_DEVICE_ID_JOYPAD_MASK)
{
unsigned i;
if (settings->uints.input_mouse_index[port] == 0)
2019-07-21 17:53:51 +02:00
{
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
2019-07-21 17:53:51 +02:00
{
if (binds[port][i].valid)
{
if (dinput_mouse_button_pressed(
di, port, binds[port][i].mbutton)
)
ret |= (1 << i);
}
2019-07-21 17:53:51 +02:00
}
}
if (!keyboard_mapping_blocked)
{
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
2019-07-21 17:53:51 +02:00
{
2020-06-12 18:28:07 +02:00
if (binds[port][i].valid)
{
if ((binds[port][i].key < RETROK_LAST) &&
2020-06-12 18:28:07 +02:00
di->state[rarch_keysym_lut
[(enum retro_key)binds[port][i].key]] & 0x80)
ret |= (1 << i);
}
2019-07-21 17:53:51 +02:00
}
}
return ret;
2019-07-08 02:40:09 +02:00
}
if (id < RARCH_BIND_LIST_END)
2019-07-21 17:53:51 +02:00
{
if (binds[port][id].valid)
2019-07-21 17:53:51 +02:00
{
if (binds[port][id].key < RETROK_LAST
&& (di->state[rarch_keysym_lut
[(enum retro_key)binds[port][id].key]] & 0x80)
&& ( (id == RARCH_GAME_FOCUS_TOGGLE)
|| !keyboard_mapping_blocked)
)
return 1;
else if (
settings->uints.input_mouse_index[port] == 0
&& dinput_mouse_button_pressed(
di, port, binds[port][id].mbutton)
)
return 1;
2019-07-21 17:53:51 +02:00
}
}
2019-07-22 01:20:00 +02:00
}
break;
case RETRO_DEVICE_KEYBOARD:
return (id < RETROK_LAST) &&
di->state[rarch_keysym_lut[(enum retro_key)id]] & 0x80;
2012-09-30 11:26:26 +02:00
case RETRO_DEVICE_ANALOG:
{
int id_minus_key = 0;
int id_plus_key = 0;
unsigned id_minus = 0;
unsigned id_plus = 0;
bool id_plus_valid = false;
bool id_minus_valid = false;
input_conv_analog_id_to_bind_id(idx, id, id_minus, id_plus);
id_minus_valid = binds[port][id_minus].valid;
id_plus_valid = binds[port][id_plus].valid;
id_minus_key = binds[port][id_minus].key;
id_plus_key = binds[port][id_plus].key;
if (id_plus_valid && id_plus_key < RETROK_LAST)
{
unsigned sym = rarch_keysym_lut[(enum retro_key)id_plus_key];
if (di->state[sym] & 0x80)
ret = 0x7fff;
}
if (id_minus_valid && id_minus_key < RETROK_LAST)
{
unsigned sym = rarch_keysym_lut[(enum retro_key)id_minus_key];
if (di->state[sym] & 0x80)
ret += -0x7fff;
}
}
2022-06-04 08:55:35 +02:00
return ret;
2020-08-31 00:32:49 +02:00
case RARCH_DEVICE_MOUSE_SCREEN:
settings = config_get_ptr();
if (settings->uints.input_mouse_index[ port ] != 0)
break;
switch (id)
{
2020-08-31 00:32:49 +02:00
case RETRO_DEVICE_ID_MOUSE_X:
return di->mouse_x;
case RETRO_DEVICE_ID_MOUSE_Y:
return di->mouse_y;
default:
break;
}
2020-08-31 00:32:49 +02:00
/* fall-through */
case RETRO_DEVICE_MOUSE:
settings = config_get_ptr();
if (settings->uints.input_mouse_index[port] == 0)
{
2020-08-31 00:32:49 +02:00
int16_t state = 0;
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:
if (di->mouse_wu)
state = 1;
di->mouse_wu = false;
return state;
case RETRO_DEVICE_ID_MOUSE_WHEELDOWN:
if (di->mouse_wd)
state = 1;
di->mouse_wd = false;
return state;
case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELUP:
if (di->mouse_hwu)
state = 1;
di->mouse_hwu = false;
return state;
case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN:
if (di->mouse_hwd)
state = 1;
di->mouse_hwd = false;
return state;
case RETRO_DEVICE_ID_MOUSE_MIDDLE:
return di->mouse_m;
case RETRO_DEVICE_ID_MOUSE_BUTTON_4:
return di->mouse_b4;
case RETRO_DEVICE_ID_MOUSE_BUTTON_5:
return di->mouse_b5;
}
}
break;
2012-10-28 10:42:20 +01:00
case RETRO_DEVICE_POINTER:
2013-01-11 16:23:04 +01:00
case RARCH_DEVICE_POINTER_SCREEN:
2020-08-31 00:32:49 +02:00
{
struct video_viewport vp;
bool pointer_down = false;
bool inside = false;
int x = 0;
int y = 0;
int16_t res_x = 0;
int16_t res_y = 0;
int16_t res_screen_x = 0;
int16_t res_screen_y = 0;
unsigned num = 0;
struct dinput_pointer_status *
2020-08-31 00:32:49 +02:00
check_pos = di->pointer_head.next;
vp.x = 0;
vp.y = 0;
vp.width = 0;
vp.height = 0;
vp.full_width = 0;
vp.full_height = 0;
while (check_pos && num < idx)
{
num++;
check_pos = check_pos->next;
}
if (!check_pos && idx > 0) /* idx = 0 has mouse fallback. */
return 0;
2012-10-28 10:42:20 +01:00
2020-08-31 00:32:49 +02:00
x = di->mouse_x;
y = di->mouse_y;
pointer_down = di->mouse_l;
if (check_pos)
{
x = check_pos->pointer_x;
y = check_pos->pointer_y;
pointer_down = true;
}
if (!(video_driver_translate_coord_viewport_wrap(&vp, x, y,
&res_x, &res_y, &res_screen_x, &res_screen_y)))
return 0;
if (device == RARCH_DEVICE_POINTER_SCREEN)
{
res_x = res_screen_x;
res_y = res_screen_y;
}
if (!(inside = (res_x >= -0x7fff) && (res_y >= -0x7fff)))
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 pointer_down;
default:
break;
}
}
break;
2012-09-30 11:26:26 +02:00
case RETRO_DEVICE_LIGHTGUN:
2019-07-21 17:53:51 +02:00
switch (id)
{
/*aiming*/
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X:
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y:
case RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN:
2020-02-23 06:05:23 +01:00
return dinput_lightgun_aiming_state(di, idx, id);
2019-07-21 17:53:51 +02:00
/*buttons*/
case RETRO_DEVICE_ID_LIGHTGUN_TRIGGER:
case RETRO_DEVICE_ID_LIGHTGUN_RELOAD:
case RETRO_DEVICE_ID_LIGHTGUN_AUX_A:
case RETRO_DEVICE_ID_LIGHTGUN_AUX_B:
case RETRO_DEVICE_ID_LIGHTGUN_AUX_C:
case RETRO_DEVICE_ID_LIGHTGUN_START:
case RETRO_DEVICE_ID_LIGHTGUN_SELECT:
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_UP:
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_DOWN:
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_LEFT:
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_RIGHT:
case RETRO_DEVICE_ID_LIGHTGUN_PAUSE:
{
2021-04-15 06:55:35 +02:00
unsigned new_id = dinput_retro_id_to_rarch(id);
const uint64_t bind_joykey = input_config_binds[port][new_id].joykey;
const uint64_t bind_joyaxis = input_config_binds[port][new_id].joyaxis;
const uint64_t autobind_joykey = input_autoconf_binds[port][new_id].joykey;
const uint64_t autobind_joyaxis= input_autoconf_binds[port][new_id].joyaxis;
uint16_t port = joypad_info->joy_idx;
float axis_threshold = joypad_info->axis_threshold;
const uint64_t joykey = (bind_joykey != NO_BTN)
? bind_joykey : autobind_joykey;
const uint32_t joyaxis = (bind_joyaxis != AXIS_NONE)
? bind_joyaxis : autobind_joyaxis;
2019-07-21 17:53:51 +02:00
if (binds[port][new_id].valid)
{
2021-04-15 06:55:35 +02:00
if ((uint16_t)joykey != NO_BTN && joypad->button(
port, (uint16_t)joykey))
return 1;
if (joyaxis != AXIS_NONE &&
((float)abs(joypad->axis(port, joyaxis))
/ 0x8000) > axis_threshold)
2020-06-12 18:28:07 +02:00
return 1;
else if (
2021-04-15 06:55:35 +02:00
binds[port][new_id].key < RETROK_LAST
&& !keyboard_mapping_blocked
2020-06-12 18:28:07 +02:00
&& di->state[rarch_keysym_lut
[(enum retro_key)binds[port][new_id].key]] & 0x80
)
return 1;
else
{
settings = config_get_ptr();
if (
2021-04-15 06:55:35 +02:00
settings->uints.input_mouse_index[port] == 0
&& dinput_mouse_button_pressed(
di, port, binds[port][new_id].mbutton)
2020-06-12 18:28:07 +02:00
)
2021-04-15 06:55:35 +02:00
return 1;
}
}
2019-07-21 17:53:51 +02:00
}
break;
/*deprecated*/
case RETRO_DEVICE_ID_LIGHTGUN_X:
return di->mouse_rel_x;
case RETRO_DEVICE_ID_LIGHTGUN_Y:
return di->mouse_rel_y;
}
2017-11-26 17:09:15 +00:00
break;
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. */
static void dinput_pointer_store_pos(
struct dinput_pointer_status *pointer, WPARAM lParam)
2013-11-05 20:49:04 +01:00
{
POINT point;
point.x = GET_X_LPARAM(lParam);
point.y = GET_Y_LPARAM(lParam);
ScreenToClient((HWND)video_driver_window_get(), &point);
2013-11-05 20:49:04 +01:00
pointer->pointer_x = point.x;
pointer->pointer_y = point.y;
}
static void dinput_add_pointer(struct dinput_input *di,
struct dinput_pointer_status *new_pointer)
2013-11-05 20:49:04 +01:00
{
struct dinput_pointer_status *insert_pos = NULL;
2020-08-14 18:31:28 +02:00
new_pointer->next = NULL;
insert_pos = &di->pointer_head;
2013-11-05 20:49:04 +01:00
while (insert_pos->next)
2020-08-14 18:31:28 +02:00
insert_pos = insert_pos->next;
insert_pos->next = new_pointer;
2013-11-05 20:49:04 +01:00
}
static void dinput_delete_pointer(struct dinput_input *di, int pointer_id)
2013-11-05 20:49:04 +01:00
{
struct dinput_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 dinput_pointer_status *to_delete = check_pos->next;
2020-08-14 18:31:28 +02:00
check_pos->next = check_pos->next->next;
2013-11-05 20:49:04 +01:00
free(to_delete);
}
check_pos = check_pos->next;
}
}
static struct dinput_pointer_status *dinput_find_pointer(
struct dinput_input *di, int pointer_id)
2013-11-05 20:49:04 +01:00
{
struct dinput_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;
}
static void dinput_clear_pointers(struct dinput_input *di)
2013-11-05 20:49:04 +01:00
{
struct dinput_pointer_status *pointer = &di->pointer_head;
2013-11-05 20:49:04 +01:00
while (pointer->next)
{
struct dinput_pointer_status *del = pointer->next;
2013-11-05 20:49:04 +01:00
pointer->next = pointer->next->next;
free(del);
}
}
bool dinput_handle_message(void *data,
UINT message, WPARAM wParam, LPARAM lParam)
2013-11-05 20:49:04 +01:00
{
struct dinput_input *di = (struct dinput_input *)data;
/* WM_POINTERDOWN : Arrives for each new touch event
* with a new ID - add to list.
* WM_POINTERUP : Arrives once the pointer is no
* longer down - remove from list.
* 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)
{
case WM_NCLBUTTONDBLCLK:
di->doubleclick_on_titlebar = true;
break;
case WM_MOUSEMOVE:
2015-11-28 18:39:43 +01:00
di->window_pos_x = GET_X_LPARAM(lParam);
di->window_pos_y = GET_Y_LPARAM(lParam);
break;
2014-01-16 08:17:20 +01:00
case WM_POINTERDOWN:
{
struct dinput_pointer_status *new_pointer =
(struct dinput_pointer_status *)malloc(sizeof(struct dinput_pointer_status));
2015-11-28 18:39:43 +01:00
if (!new_pointer)
return false;
new_pointer->pointer_id = GET_POINTERID_WPARAM(wParam);
dinput_pointer_store_pos(new_pointer, lParam);
2015-11-28 18:39:43 +01:00
dinput_add_pointer(di, new_pointer);
return true;
}
2014-01-16 08:17:20 +01:00
case WM_POINTERUP:
2015-11-28 18:39:43 +01:00
{
int pointer_id = GET_POINTERID_WPARAM(wParam);
dinput_delete_pointer(di, pointer_id);
return true;
}
2014-01-16 08:17:20 +01:00
case WM_POINTERUPDATE:
2015-11-28 18:39:43 +01:00
{
2020-08-28 02:27:55 +02:00
int pointer_id = GET_POINTERID_WPARAM(wParam);
struct dinput_pointer_status *pointer = dinput_find_pointer(di, pointer_id);
2015-11-28 18:39:43 +01:00
if (pointer)
dinput_pointer_store_pos(pointer, lParam);
2015-11-28 18:39:43 +01:00
return true;
}
2014-01-16 08:17:20 +01:00
case WM_DEVICECHANGE:
#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0500 /* 2K */
2020-08-28 02:27:55 +02:00
if ( wParam == DBT_DEVICEARRIVAL ||
wParam == DBT_DEVICEREMOVECOMPLETE)
{
PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR)lParam;
if(pHdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
{
2019-07-04 21:54:51 +02:00
#if 0
2020-08-28 02:27:55 +02:00
PDEV_BROADCAST_DEVICEINTERFACE pDevInf =
(PDEV_BROADCAST_DEVICEINTERFACE)pHdr;
2019-07-04 21:54:51 +02:00
#endif
2017-12-27 15:43:03 -05:00
2020-08-28 02:27:55 +02:00
/* TODO/FIXME: Don't destroy everything, let's just
* handle new devices gracefully */
2020-08-30 05:29:32 +02:00
joypad_driver_reinit(di, di->joypad_driver_name);
}
2020-08-28 02:27:55 +02:00
}
#endif
2006-01-17 06:28:35 +01:00
break;
case WM_MOUSEWHEEL:
2020-08-28 02:27:55 +02:00
if (((short) HIWORD(wParam))/120 > 0)
di->mouse_wu = true;
if (((short) HIWORD(wParam))/120 < 0)
di->mouse_wd = true;
2015-11-28 18:39:43 +01:00
break;
2015-03-12 13:35:37 +01:00
case WM_MOUSEHWHEEL:
2020-08-28 02:27:55 +02:00
if (((short) HIWORD(wParam))/120 > 0)
di->mouse_hwu = true;
if (((short) HIWORD(wParam))/120 < 0)
di->mouse_hwd = true;
2015-11-28 18:39:43 +01:00
break;
2013-11-05 20:49:04 +01:00
}
2015-03-20 22:32:09 +01:00
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_dinput_ctx;
2012-09-30 11:26:26 +02:00
if (!di)
return;
/* Prevent a joypad driver to kill our context prematurely. */
g_dinput_ctx = NULL;
#ifndef _XBOX
2022-05-17 13:21:56 +02:00
SetWindowLongPtr(main_window.hwnd, GWLP_USERDATA, 0);
#endif
2012-09-30 11:26:26 +02:00
g_dinput_ctx = hold_ctx;
2013-11-05 20:49:04 +01:00
/* Clear any leftover pointers. */
dinput_clear_pointers(di);
2012-09-30 11:26:26 +02:00
if (di->keyboard)
IDirectInputDevice8_Release(di->keyboard);
if (di->mouse)
IDirectInputDevice8_Release(di->mouse);
2012-10-28 10:42:20 +01:00
if (di->joypad_driver_name)
free(di->joypad_driver_name);
2020-08-30 05:29:32 +02:00
di->joypad_driver_name = NULL;
free(di);
2012-09-30 11:26:26 +02:00
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;
if (!di->mouse)
return;
2013-04-04 22:35:05 +02:00
IDirectInputDevice8_Unacquire(di->mouse);
IDirectInputDevice8_SetCooperativeLevel(di->mouse,
(HWND)video_driver_window_get(),
(DISCL_NONEXCLUSIVE | DISCL_FOREGROUND));
2013-04-04 22:35:05 +02:00
IDirectInputDevice8_Acquire(di->mouse);
#ifndef _XBOX
win32_clip_window(state);
#endif
2013-04-04 22:35:05 +02:00
}
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_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,
NULL
2012-09-30 11:26:26 +02:00
};