2015-12-18 06:12:53 +01:00
|
|
|
/*************************************************************************/
|
2019-02-12 15:43:54 +01:00
|
|
|
/* joypad_windows.h */
|
2015-12-18 06:12:53 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2015-12-18 06:12:53 +01:00
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2015-12-18 06:12:53 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2019-02-12 15:43:54 +01:00
|
|
|
#ifndef JOYPAD_WINDOWS_H
|
|
|
|
#define JOYPAD_WINDOWS_H
|
2015-12-18 06:12:53 +01:00
|
|
|
|
|
|
|
#include "os_windows.h"
|
2019-02-12 15:43:54 +01:00
|
|
|
|
2015-12-18 06:12:53 +01:00
|
|
|
#define DIRECTINPUT_VERSION 0x0800
|
|
|
|
#include <dinput.h>
|
|
|
|
#include <xinput.h> // on unix the file is called "xinput.h", on windows I'm sure it won't mind
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#ifndef SAFE_RELEASE // when Windows Media Device M? is not present
|
2015-12-18 06:12:53 +01:00
|
|
|
#define SAFE_RELEASE(x) \
|
2020-04-02 01:20:12 +02:00
|
|
|
if (x != nullptr) { \
|
2017-03-05 16:44:50 +01:00
|
|
|
x->Release(); \
|
2020-04-02 01:20:12 +02:00
|
|
|
x = nullptr; \
|
2017-03-05 16:44:50 +01:00
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
#endif
|
|
|
|
|
2016-01-05 01:16:10 +01:00
|
|
|
#ifndef XUSER_MAX_COUNT
|
|
|
|
#define XUSER_MAX_COUNT 4
|
|
|
|
#endif
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
class JoypadWindows {
|
2015-12-18 06:12:53 +01:00
|
|
|
public:
|
2017-01-08 23:52:38 +01:00
|
|
|
JoypadWindows();
|
2020-03-09 16:56:48 +01:00
|
|
|
JoypadWindows(HWND *hwnd);
|
2017-01-08 23:52:38 +01:00
|
|
|
~JoypadWindows();
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
void probe_joypads();
|
2017-03-26 15:59:13 +02:00
|
|
|
void process_joypads();
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
private:
|
2015-12-18 19:15:32 +01:00
|
|
|
enum {
|
2017-01-08 21:05:51 +01:00
|
|
|
JOYPADS_MAX = 16,
|
2015-12-18 19:15:32 +01:00
|
|
|
JOY_AXIS_COUNT = 6,
|
|
|
|
MIN_JOY_AXIS = 10,
|
|
|
|
MAX_JOY_AXIS = 32768,
|
|
|
|
MAX_JOY_BUTTONS = 128,
|
|
|
|
KEY_EVENT_BUFFER_SIZE = 512,
|
|
|
|
MAX_TRIGGER = 255
|
|
|
|
};
|
|
|
|
|
|
|
|
struct dinput_gamepad {
|
|
|
|
int id;
|
|
|
|
bool attached;
|
|
|
|
bool confirmed;
|
|
|
|
bool last_buttons[MAX_JOY_BUTTONS];
|
|
|
|
DWORD last_pad;
|
|
|
|
|
|
|
|
LPDIRECTINPUTDEVICE8 di_joy;
|
2020-11-06 11:17:28 +01:00
|
|
|
List<LONG> joy_axis;
|
2015-12-18 19:15:32 +01:00
|
|
|
GUID guid;
|
|
|
|
|
|
|
|
dinput_gamepad() {
|
|
|
|
id = -1;
|
|
|
|
last_pad = -1;
|
|
|
|
attached = false;
|
|
|
|
confirmed = false;
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_JOY_BUTTONS; i++)
|
|
|
|
last_buttons[i] = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct xinput_gamepad {
|
2020-11-24 10:12:55 +01:00
|
|
|
int id = 0;
|
|
|
|
bool attached = false;
|
|
|
|
bool vibrating = false;
|
|
|
|
DWORD last_packet = 0;
|
2015-12-18 19:15:32 +01:00
|
|
|
XINPUT_STATE state;
|
2020-11-24 10:12:55 +01:00
|
|
|
uint64_t ff_timestamp = 0;
|
|
|
|
uint64_t ff_end_timestamp = 0;
|
2015-12-18 19:15:32 +01:00
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
typedef DWORD(WINAPI *XInputGetState_t)(DWORD dwUserIndex, XINPUT_STATE *pState);
|
|
|
|
typedef DWORD(WINAPI *XInputSetState_t)(DWORD dwUserIndex, XINPUT_VIBRATION *pVibration);
|
2015-12-18 19:15:32 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
HWND *hWnd;
|
2015-12-18 19:15:32 +01:00
|
|
|
HANDLE xinput_dll;
|
|
|
|
LPDIRECTINPUT8 dinput;
|
2020-04-28 15:19:37 +02:00
|
|
|
Input *input;
|
2015-12-18 19:15:32 +01:00
|
|
|
|
|
|
|
int id_to_change;
|
2020-06-20 11:03:15 +02:00
|
|
|
int slider_count;
|
2017-01-08 21:05:51 +01:00
|
|
|
int joypad_count;
|
|
|
|
bool attached_joypads[JOYPADS_MAX];
|
|
|
|
dinput_gamepad d_joypads[JOYPADS_MAX];
|
|
|
|
xinput_gamepad x_joypads[XUSER_MAX_COUNT];
|
2015-12-18 19:15:32 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE *p_instance, void *p_context);
|
|
|
|
static BOOL CALLBACK objectsCallback(const DIDEVICEOBJECTINSTANCE *instance, void *context);
|
2015-12-18 19:15:32 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void setup_joypad_object(const DIDEVICEOBJECTINSTANCE *ob, int p_joy_id);
|
2017-01-08 21:05:51 +01:00
|
|
|
void close_joypad(int id = -1);
|
2015-12-18 19:15:32 +01:00
|
|
|
void load_xinput();
|
|
|
|
void unload_xinput();
|
|
|
|
|
2017-03-26 15:59:13 +02:00
|
|
|
void post_hat(int p_device, DWORD p_dpad);
|
2015-12-18 19:15:32 +01:00
|
|
|
|
|
|
|
bool have_device(const GUID &p_guid);
|
2017-03-05 16:44:50 +01:00
|
|
|
bool is_xinput_device(const GUID *p_guid);
|
|
|
|
bool setup_dinput_joypad(const DIDEVICEINSTANCE *instance);
|
2017-01-08 21:05:51 +01:00
|
|
|
void joypad_vibration_start_xinput(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp);
|
|
|
|
void joypad_vibration_stop_xinput(int p_device, uint64_t p_timestamp);
|
2015-12-18 19:15:32 +01:00
|
|
|
|
2020-05-14 00:27:34 +02:00
|
|
|
Input::JoyAxisValue axis_correct(int p_val, bool p_xinput = false, bool p_trigger = false, bool p_negate = false) const;
|
2015-12-18 19:15:32 +01:00
|
|
|
XInputGetState_t xinput_get_state;
|
2016-06-15 14:40:57 +02:00
|
|
|
XInputSetState_t xinput_set_state;
|
2015-12-18 06:12:53 +01:00
|
|
|
};
|
|
|
|
|
2019-02-12 15:43:54 +01:00
|
|
|
#endif // JOYPAD_WINDOWS_H
|