From fd29e5c4cc26cd853a03ac7b0e08b367b7070c84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Sun, 12 Jun 2016 17:08:04 +0200 Subject: [PATCH 1/3] ControllerInterface: Don't pass m_devices to the backends Previously, the devices vector would be passed to all backends. They would then manually push_back to it to add new devices. This was fine but caused issues when trying to add synchronisation. Instead, backends now call AddDevice() to fill m_devices so that it is not accessible from the outside. --- .../ControllerInterface/Android/Android.cpp | 13 ++++------- .../ControllerInterface/Android/Android.h | 4 ++-- .../ControllerInterface.cpp | 23 +++++++++++-------- .../ControllerInterface/ControllerInterface.h | 1 + .../ControllerInterface/DInput/DInput.cpp | 10 ++++---- .../ControllerInterface/DInput/DInput.h | 3 +-- .../DInput/DInputJoystick.cpp | 5 ++-- .../DInput/DInputJoystick.h | 2 +- .../DInput/DInputKeyboardMouse.cpp | 5 ++-- .../DInput/DInputKeyboardMouse.h | 2 +- .../InputCommon/ControllerInterface/OSX/OSX.h | 4 +--- .../ControllerInterface/OSX/OSX.mm | 16 ++++++------- .../ControllerInterface/Pipes/Pipes.cpp | 5 ++-- .../ControllerInterface/Pipes/Pipes.h | 4 +--- .../ControllerInterface/SDL/SDL.cpp | 5 ++-- .../InputCommon/ControllerInterface/SDL/SDL.h | 4 +--- .../ControllerInterface/XInput/XInput.cpp | 4 ++-- .../ControllerInterface/XInput/XInput.h | 4 ++-- .../ControllerInterface/Xlib/XInput2.cpp | 6 ++--- .../ControllerInterface/Xlib/XInput2.h | 4 ++-- .../ControllerInterface/Xlib/Xlib.cpp | 4 ++-- .../ControllerInterface/Xlib/Xlib.h | 4 ++-- .../ControllerInterface/evdev/evdev.cpp | 5 ++-- .../ControllerInterface/evdev/evdev.h | 4 +--- 24 files changed, 69 insertions(+), 72 deletions(-) diff --git a/Source/Core/InputCommon/ControllerInterface/Android/Android.cpp b/Source/Core/InputCommon/ControllerInterface/Android/Android.cpp index 3d68be633f..f6957e3ca1 100644 --- a/Source/Core/InputCommon/ControllerInterface/Android/Android.cpp +++ b/Source/Core/InputCommon/ControllerInterface/Android/Android.cpp @@ -4,21 +4,16 @@ #include "InputCommon/ControllerInterface/Android/Android.h" #include +#include "InputCommon/ControllerInterface/ControllerInterface.h" namespace ciface { namespace Android { -void Init(std::vector& devices) +void Init() { - devices.push_back(new Touchscreen(0)); - devices.push_back(new Touchscreen(1)); - devices.push_back(new Touchscreen(2)); - devices.push_back(new Touchscreen(3)); - devices.push_back(new Touchscreen(4)); - devices.push_back(new Touchscreen(5)); - devices.push_back(new Touchscreen(6)); - devices.push_back(new Touchscreen(7)); + for (int i = 0; i < 8; ++i) + g_controller_interface.AddDevice(new Touchscreen(i)); } // Touchscreens and stuff diff --git a/Source/Core/InputCommon/ControllerInterface/Android/Android.h b/Source/Core/InputCommon/ControllerInterface/Android/Android.h index 53b578f699..29cc16d7ef 100644 --- a/Source/Core/InputCommon/ControllerInterface/Android/Android.h +++ b/Source/Core/InputCommon/ControllerInterface/Android/Android.h @@ -4,14 +4,14 @@ #pragma once -#include "InputCommon/ControllerInterface/Device.h" +#include "InputCommon/ControllerInterface/ControllerInterface.h" #include "jni/ButtonManager.h" namespace ciface { namespace Android { -void Init(std::vector& devices); +void Init(); class Touchscreen : public Core::Device { private: diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp index 55643ece5f..c139d3b787 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp @@ -55,31 +55,31 @@ void ControllerInterface::Initialize(void* const hwnd) m_hwnd = hwnd; #ifdef CIFACE_USE_DINPUT - ciface::DInput::Init(m_devices, (HWND)hwnd); + ciface::DInput::Init((HWND)hwnd); #endif #ifdef CIFACE_USE_XINPUT - ciface::XInput::Init(m_devices); + ciface::XInput::Init(); #endif #ifdef CIFACE_USE_XLIB - ciface::Xlib::Init(m_devices, hwnd); + ciface::Xlib::Init(hwnd); #ifdef CIFACE_USE_X11_XINPUT2 - ciface::XInput2::Init(m_devices, hwnd); + ciface::XInput2::Init(hwnd); #endif #endif #ifdef CIFACE_USE_OSX - ciface::OSX::Init(m_devices, hwnd); + ciface::OSX::Init(hwnd); #endif #ifdef CIFACE_USE_SDL - ciface::SDL::Init(m_devices); + ciface::SDL::Init(); #endif #ifdef CIFACE_USE_ANDROID - ciface::Android::Init(m_devices); + ciface::Android::Init(); #endif #ifdef CIFACE_USE_EVDEV - ciface::evdev::Init(m_devices); + ciface::evdev::Init(); #endif #ifdef CIFACE_USE_PIPES - ciface::Pipes::Init(m_devices); + ciface::Pipes::Init(); #endif m_is_init = true; @@ -139,6 +139,11 @@ void ControllerInterface::Shutdown() m_is_init = false; } +void ControllerInterface::AddDevice(ciface::Core::Device* device) +{ + m_devices.push_back(device); +} + // // UpdateInput // diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h index 25154ede40..3d1354dcc7 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h @@ -121,6 +121,7 @@ public: void Initialize(void* const hwnd); void Reinitialize(); void Shutdown(); + void AddDevice(ciface::Core::Device* device); bool IsInit() const { return m_is_init; } void UpdateReference(ControlReference* control, const ciface::Core::DeviceQualifier& default_device) const; diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/DInput.cpp b/Source/Core/InputCommon/ControllerInterface/DInput/DInput.cpp index 09227b5932..83394852af 100644 --- a/Source/Core/InputCommon/ControllerInterface/DInput/DInput.cpp +++ b/Source/Core/InputCommon/ControllerInterface/DInput/DInput.cpp @@ -2,9 +2,9 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. -#include "Common/StringUtil.h" - #include "InputCommon/ControllerInterface/DInput/DInput.h" +#include "Common/StringUtil.h" +#include "InputCommon/ControllerInterface/ControllerInterface.h" #include "InputCommon/ControllerInterface/DInput/DInputJoystick.h" #include "InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.h" @@ -44,7 +44,7 @@ std::string GetDeviceName(const LPDIRECTINPUTDEVICE8 device) return result; } -void Init(std::vector& devices, HWND hwnd) +void Init(HWND hwnd) { IDirectInput8* idi8; if (FAILED(DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION, IID_IDirectInput8, @@ -53,8 +53,8 @@ void Init(std::vector& devices, HWND hwnd) return; } - InitKeyboardMouse(idi8, devices, hwnd); - InitJoystick(idi8, devices, hwnd); + InitKeyboardMouse(idi8, hwnd); + InitJoystick(idi8, hwnd); idi8->Release(); } diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/DInput.h b/Source/Core/InputCommon/ControllerInterface/DInput/DInput.h index 2f5e2f83a7..94e4cb4fe3 100644 --- a/Source/Core/InputCommon/ControllerInterface/DInput/DInput.h +++ b/Source/Core/InputCommon/ControllerInterface/DInput/DInput.h @@ -10,7 +10,6 @@ #include #include "InputCommon/ControllerInterface/DInput/DInput8.h" -#include "InputCommon/ControllerInterface/Device.h" namespace ciface { @@ -21,6 +20,6 @@ BOOL CALLBACK DIEnumDeviceObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVO BOOL CALLBACK DIEnumDevicesCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef); std::string GetDeviceName(const LPDIRECTINPUTDEVICE8 device); -void Init(std::vector& devices, HWND hwnd); +void Init(HWND hwnd); } } diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/DInputJoystick.cpp b/Source/Core/InputCommon/ControllerInterface/DInput/DInputJoystick.cpp index bf2c1f3e7e..ea059fde73 100644 --- a/Source/Core/InputCommon/ControllerInterface/DInput/DInputJoystick.cpp +++ b/Source/Core/InputCommon/ControllerInterface/DInput/DInputJoystick.cpp @@ -6,6 +6,7 @@ #include #include +#include "InputCommon/ControllerInterface/ControllerInterface.h" #include "InputCommon/ControllerInterface/DInput/DInput.h" #include "InputCommon/ControllerInterface/DInput/DInputJoystick.h" #include "InputCommon/ControllerInterface/DInput/XInputFilter.h" @@ -16,7 +17,7 @@ namespace DInput { #define DATA_BUFFER_SIZE 32 -void InitJoystick(IDirectInput8* const idi8, std::vector& devices, HWND hwnd) +void InitJoystick(IDirectInput8* const idi8, HWND hwnd) { std::list joysticks; idi8->EnumDevices(DI8DEVCLASS_GAMECTRL, DIEnumDevicesCallback, (LPVOID)&joysticks, @@ -60,7 +61,7 @@ void InitJoystick(IDirectInput8* const idi8, std::vector& devices Joystick* js = new Joystick(/*&*i, */ js_device, name_counts[joystick.tszInstanceName]++); // only add if it has some inputs/outputs if (js->Inputs().size() || js->Outputs().size()) - devices.push_back(js); + g_controller_interface.AddDevice(js); else delete js; } diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/DInputJoystick.h b/Source/Core/InputCommon/ControllerInterface/DInput/DInputJoystick.h index 9a14e910de..d66c28cfba 100644 --- a/Source/Core/InputCommon/ControllerInterface/DInput/DInputJoystick.h +++ b/Source/Core/InputCommon/ControllerInterface/DInput/DInputJoystick.h @@ -11,7 +11,7 @@ namespace ciface { namespace DInput { -void InitJoystick(IDirectInput8* const idi8, std::vector& devices, HWND hwnd); +void InitJoystick(IDirectInput8* const idi8, HWND hwnd); class Joystick : public ForceFeedback::ForceFeedbackDevice { diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.cpp b/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.cpp index 92c8514825..3d9b692ba0 100644 --- a/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.cpp +++ b/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.cpp @@ -4,6 +4,7 @@ #include +#include "InputCommon/ControllerInterface/ControllerInterface.h" #include "InputCommon/ControllerInterface/DInput/DInput.h" #include "InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.h" @@ -31,7 +32,7 @@ static const struct // lil silly static HWND m_hwnd; -void InitKeyboardMouse(IDirectInput8* const idi8, std::vector& devices, HWND _hwnd) +void InitKeyboardMouse(IDirectInput8* const idi8, HWND _hwnd) { m_hwnd = _hwnd; @@ -56,7 +57,7 @@ void InitKeyboardMouse(IDirectInput8* const idi8, std::vector& de if (SUCCEEDED( mo_device->SetCooperativeLevel(nullptr, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE))) { - devices.push_back(new KeyboardMouse(kb_device, mo_device)); + g_controller_interface.AddDevice(new KeyboardMouse(kb_device, mo_device)); return; } } diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.h b/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.h index 1ba9022f9d..38de8e219d 100644 --- a/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.h +++ b/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.h @@ -13,7 +13,7 @@ namespace ciface { namespace DInput { -void InitKeyboardMouse(IDirectInput8* const idi8, std::vector& devices, HWND _hwnd); +void InitKeyboardMouse(IDirectInput8* const idi8, HWND _hwnd); class KeyboardMouse : public Core::Device { diff --git a/Source/Core/InputCommon/ControllerInterface/OSX/OSX.h b/Source/Core/InputCommon/ControllerInterface/OSX/OSX.h index c257d7e8f4..c2104774b7 100644 --- a/Source/Core/InputCommon/ControllerInterface/OSX/OSX.h +++ b/Source/Core/InputCommon/ControllerInterface/OSX/OSX.h @@ -4,13 +4,11 @@ #pragma once -#include "InputCommon/ControllerInterface/Device.h" - namespace ciface { namespace OSX { -void Init(std::vector& devices, void* window); +void Init(void* window); void DeInit(); void DeviceElementDebugPrint(const void*, void*); diff --git a/Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm b/Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm index 1335b01967..ea9723c6b9 100644 --- a/Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm +++ b/Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm @@ -6,6 +6,7 @@ #include #include +#include "InputCommon/ControllerInterface/ControllerInterface.h" #include "InputCommon/ControllerInterface/OSX/OSX.h" #include "InputCommon/ControllerInterface/OSX/OSXJoystick.h" #include "InputCommon/ControllerInterface/OSX/OSXKeyboard.h" @@ -140,22 +141,21 @@ static void DeviceMatching_callback(void* inContext, IOReturn inResult, void* in DeviceDebugPrint(inIOHIDDeviceRef); - std::vector* devices = (std::vector*)inContext; - - // Add to the devices vector if it's of a type we want + // Add a device if it's of a type we want if (IOHIDDeviceConformsTo(inIOHIDDeviceRef, kHIDPage_GenericDesktop, kHIDUsage_GD_Keyboard)) - devices->push_back(new Keyboard(inIOHIDDeviceRef, name, kbd_name_counts[name]++, g_window)); + g_controller_interface.AddDevice( + new Keyboard(inIOHIDDeviceRef, name, kbd_name_counts[name]++, g_window)); #if 0 else if (IOHIDDeviceConformsTo(inIOHIDDeviceRef, kHIDPage_GenericDesktop, kHIDUsage_GD_Mouse)) - devices->push_back(new Mouse(inIOHIDDeviceRef, + g_controller_interface.AddDevice(new Mouse(inIOHIDDeviceRef, name, mouse_name_counts[name]++)); #endif else - devices->push_back(new Joystick(inIOHIDDeviceRef, name, joy_name_counts[name]++)); + g_controller_interface.AddDevice(new Joystick(inIOHIDDeviceRef, name, joy_name_counts[name]++)); } -void Init(std::vector& devices, void* window) +void Init(void* window) { HIDManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); if (!HIDManager) @@ -166,7 +166,7 @@ void Init(std::vector& devices, void* window) IOHIDManagerSetDeviceMatching(HIDManager, nullptr); // Callbacks for acquisition or loss of a matching device - IOHIDManagerRegisterDeviceMatchingCallback(HIDManager, DeviceMatching_callback, (void*)&devices); + IOHIDManagerRegisterDeviceMatchingCallback(HIDManager, DeviceMatching_callback, nullptr); // Match devices that are plugged in right now IOHIDManagerScheduleWithRunLoop(HIDManager, CFRunLoopGetCurrent(), OurRunLoop); diff --git a/Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.cpp b/Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.cpp index 95bdcc4da2..fc89d8a1e6 100644 --- a/Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.cpp +++ b/Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.cpp @@ -17,6 +17,7 @@ #include "Common/FileUtil.h" #include "Common/MathUtil.h" #include "Common/StringUtil.h" +#include "InputCommon/ControllerInterface/ControllerInterface.h" #include "InputCommon/ControllerInterface/Pipes/Pipes.h" namespace ciface @@ -40,7 +41,7 @@ static double StringToDouble(const std::string& text) return result; } -void Init(std::vector& devices) +void Init() { // Search the Pipes directory for files that we can open in read-only, // non-blocking mode. The device name is the virtual name of the file. @@ -60,7 +61,7 @@ void Init(std::vector& devices) int fd = open(child.physicalName.c_str(), O_RDONLY | O_NONBLOCK); if (fd < 0) continue; - devices.push_back(new PipeDevice(fd, child.virtualName, found++)); + g_controller_interface.AddDevice(new PipeDevice(fd, child.virtualName, found++)); } } diff --git a/Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.h b/Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.h index 855c45953b..66ce56ff1c 100644 --- a/Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.h +++ b/Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.h @@ -8,8 +8,6 @@ #include #include -#include "InputCommon/ControllerInterface/Device.h" - namespace ciface { namespace Pipes @@ -24,7 +22,7 @@ namespace Pipes // SET {L, R} [0, 1] // SET {MAIN, C} [0, 1] [0, 1] -void Init(std::vector& devices); +void Init(); class PipeDevice : public Core::Device { diff --git a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp index 8b87261480..870f0387d7 100644 --- a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp +++ b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp @@ -7,6 +7,7 @@ #include #include "Common/StringUtil.h" +#include "InputCommon/ControllerInterface/ControllerInterface.h" #include "InputCommon/ControllerInterface/SDL/SDL.h" #ifdef _WIN32 @@ -32,7 +33,7 @@ static std::string GetJoystickName(int index) #endif } -void Init(std::vector& devices) +void Init() { // this is used to number the joysticks // multiple joysticks with the same name shall get unique ids starting at 0 @@ -60,7 +61,7 @@ void Init(std::vector& devices) Joystick* js = new Joystick(dev, i, name_counts[GetJoystickName(i)]++); // only add if it has some inputs/outputs if (js->Inputs().size() || js->Outputs().size()) - devices.push_back(js); + g_controller_interface.AddDevice(js); else delete js; } diff --git a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.h b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.h index 10c3fab6ee..746eb20b8e 100644 --- a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.h +++ b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.h @@ -8,8 +8,6 @@ #include -#include "InputCommon/ControllerInterface/Device.h" - #if SDL_VERSION_ATLEAST(1, 3, 0) #define USE_SDL_HAPTIC #endif @@ -22,7 +20,7 @@ namespace ciface { namespace SDL { -void Init(std::vector& devices); +void Init(); class Joystick : public Core::Device { diff --git a/Source/Core/InputCommon/ControllerInterface/XInput/XInput.cpp b/Source/Core/InputCommon/ControllerInterface/XInput/XInput.cpp index d7ed3343a0..982764d4b4 100644 --- a/Source/Core/InputCommon/ControllerInterface/XInput/XInput.cpp +++ b/Source/Core/InputCommon/ControllerInterface/XInput/XInput.cpp @@ -50,7 +50,7 @@ static XInputGetState_t PXInputGetState = nullptr; static bool haveGuideButton = false; -void Init(std::vector& devices) +void Init() { if (!hXInput) { @@ -89,7 +89,7 @@ void Init(std::vector& devices) XINPUT_CAPABILITIES caps; for (int i = 0; i != 4; ++i) if (ERROR_SUCCESS == PXInputGetCapabilities(i, 0, &caps)) - devices.push_back(new Device(caps, i)); + g_controller_interface.AddDevice(new Device(caps, i)); } void DeInit() diff --git a/Source/Core/InputCommon/ControllerInterface/XInput/XInput.h b/Source/Core/InputCommon/ControllerInterface/XInput/XInput.h index 204b1ecf81..e4a047d53c 100644 --- a/Source/Core/InputCommon/ControllerInterface/XInput/XInput.h +++ b/Source/Core/InputCommon/ControllerInterface/XInput/XInput.h @@ -12,7 +12,7 @@ #include #include -#include "InputCommon/ControllerInterface/Device.h" +#include "InputCommon/ControllerInterface/ControllerInterface.h" #ifndef XINPUT_DEVSUBTYPE_FLIGHT_STICK #error You are building this module against the wrong version of DirectX. You probably need to remove DXSDK_DIR from your include path and/or _WIN32_WINNT is wrong. @@ -22,7 +22,7 @@ namespace ciface { namespace XInput { -void Init(std::vector& devices); +void Init(); void DeInit(); class Device : public Core::Device diff --git a/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.cpp b/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.cpp index 79b86a487d..d46f1cf4f6 100644 --- a/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.cpp +++ b/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.cpp @@ -46,7 +46,7 @@ namespace ciface namespace XInput2 { // This function will add zero or more KeyboardMouse objects to devices. -void Init(std::vector& devices, void* const hwnd) +void Init(void* const hwnd) { Display* dpy = XOpenDisplay(nullptr); @@ -78,8 +78,8 @@ void Init(std::vector& devices, void* const hwnd) if (current_master->use == XIMasterPointer) // Since current_master is a master pointer, its attachment must // be a master keyboard. - devices.push_back(new KeyboardMouse((Window)hwnd, xi_opcode, current_master->deviceid, - current_master->attachment)); + g_controller_interface.AddDevice(new KeyboardMouse( + (Window)hwnd, xi_opcode, current_master->deviceid, current_master->attachment)); } XCloseDisplay(dpy); diff --git a/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.h b/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.h index e8f2e2e06b..10499ea22c 100644 --- a/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.h +++ b/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.h @@ -12,13 +12,13 @@ extern "C" { #include } -#include "InputCommon/ControllerInterface/Device.h" +#include "InputCommon/ControllerInterface/ControllerInterface.h" namespace ciface { namespace XInput2 { -void Init(std::vector& devices, void* const hwnd); +void Init(void* const hwnd); class KeyboardMouse : public Core::Device { diff --git a/Source/Core/InputCommon/ControllerInterface/Xlib/Xlib.cpp b/Source/Core/InputCommon/ControllerInterface/Xlib/Xlib.cpp index 71269c74f7..a7d94bbcd0 100644 --- a/Source/Core/InputCommon/ControllerInterface/Xlib/Xlib.cpp +++ b/Source/Core/InputCommon/ControllerInterface/Xlib/Xlib.cpp @@ -11,9 +11,9 @@ namespace ciface { namespace Xlib { -void Init(std::vector& devices, void* const hwnd) +void Init(void* const hwnd) { - devices.push_back(new KeyboardMouse((Window)hwnd)); + g_controller_interface.AddDevice(new KeyboardMouse((Window)hwnd)); } KeyboardMouse::KeyboardMouse(Window window) : m_window(window) diff --git a/Source/Core/InputCommon/ControllerInterface/Xlib/Xlib.h b/Source/Core/InputCommon/ControllerInterface/Xlib/Xlib.h index 94388211c0..18e5bad635 100644 --- a/Source/Core/InputCommon/ControllerInterface/Xlib/Xlib.h +++ b/Source/Core/InputCommon/ControllerInterface/Xlib/Xlib.h @@ -7,13 +7,13 @@ #include #include -#include "InputCommon/ControllerInterface/Device.h" +#include "InputCommon/ControllerInterface/ControllerInterface.h" namespace ciface { namespace Xlib { -void Init(std::vector& devices, void* const hwnd); +void Init(void* const hwnd); class KeyboardMouse : public Core::Device { diff --git a/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp b/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp index 7a6ba50713..5da77e585a 100644 --- a/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp +++ b/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp @@ -9,6 +9,7 @@ #include "Common/Assert.h" #include "Common/Logging/Log.h" +#include "InputCommon/ControllerInterface/ControllerInterface.h" #include "InputCommon/ControllerInterface/evdev/evdev.h" namespace ciface @@ -31,7 +32,7 @@ static std::string GetName(const std::string& devnode) return res; } -void Init(std::vector& controllerDevices) +void Init() { // this is used to number the joysticks // multiple joysticks with the same name shall get unique ids starting at 0 @@ -71,7 +72,7 @@ void Init(std::vector& controllerDevices) if (input->IsInteresting()) { - controllerDevices.push_back(input); + g_controller_interface.AddDevice(input); num_controllers++; } else diff --git a/Source/Core/InputCommon/ControllerInterface/evdev/evdev.h b/Source/Core/InputCommon/ControllerInterface/evdev/evdev.h index ce7831bfdc..36c25cb42b 100644 --- a/Source/Core/InputCommon/ControllerInterface/evdev/evdev.h +++ b/Source/Core/InputCommon/ControllerInterface/evdev/evdev.h @@ -8,13 +8,11 @@ #include #include -#include "InputCommon/ControllerInterface/Device.h" - namespace ciface { namespace evdev { -void Init(std::vector& devices); +void Init(); class evdevDevice : public Core::Device { From d3e2ae35ff853d115aca077eaa10b88114ef7c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Sun, 12 Jun 2016 17:31:41 +0200 Subject: [PATCH 2/3] ControllerInterface: Add synchronisation Since we may have to add/access devices from different threads, this adds synchronisation to anything that touches m_devices. --- .../InputCommon/ControllerInterface/ControllerInterface.cpp | 4 ++++ .../InputCommon/ControllerInterface/ControllerInterface.h | 1 + Source/Core/InputCommon/ControllerInterface/Device.h | 2 ++ 3 files changed, 7 insertions(+) diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp index c139d3b787..b36387162d 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp @@ -104,6 +104,8 @@ void ControllerInterface::Shutdown() if (!m_is_init) return; + std::lock_guard lk(m_devices_mutex); + for (ciface::Core::Device* d : m_devices) { // Set outputs to ZERO before destroying device @@ -141,6 +143,7 @@ void ControllerInterface::Shutdown() void ControllerInterface::AddDevice(ciface::Core::Device* device) { + std::lock_guard lk(m_devices_mutex); m_devices.push_back(device); } @@ -151,6 +154,7 @@ void ControllerInterface::AddDevice(ciface::Core::Device* device) // void ControllerInterface::UpdateInput() { + std::lock_guard lk(m_devices_mutex); for (ciface::Core::Device* d : m_devices) d->UpdateInput(); } diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h index 3d1354dcc7..2571e5f3ab 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h @@ -6,6 +6,7 @@ #include #include +#include #include #include #include diff --git a/Source/Core/InputCommon/ControllerInterface/Device.h b/Source/Core/InputCommon/ControllerInterface/Device.h index dc60691b56..c3cddb3a79 100644 --- a/Source/Core/InputCommon/ControllerInterface/Device.h +++ b/Source/Core/InputCommon/ControllerInterface/Device.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include @@ -167,6 +168,7 @@ public: Device* FindDevice(const DeviceQualifier& devq) const; protected: + std::mutex m_devices_mutex; std::vector m_devices; }; } From 9b075556ea826699800d9478d1eed3a80368c348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Fri, 24 Jun 2016 16:28:27 +0200 Subject: [PATCH 3/3] Fix a small formatting issue --- .../DInput/DInputKeyboardMouse.cpp | 27 ++++++------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.cpp b/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.cpp index 3d9b692ba0..fc02238980 100644 --- a/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.cpp +++ b/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.cpp @@ -44,26 +44,15 @@ void InitKeyboardMouse(IDirectInput8* const idi8, HWND _hwnd) LPDIRECTINPUTDEVICE8 kb_device = nullptr; LPDIRECTINPUTDEVICE8 mo_device = nullptr; - if (SUCCEEDED(idi8->CreateDevice(GUID_SysKeyboard, &kb_device, nullptr))) + if (SUCCEEDED(idi8->CreateDevice(GUID_SysKeyboard, &kb_device, nullptr)) && + SUCCEEDED(kb_device->SetDataFormat(&c_dfDIKeyboard)) && + SUCCEEDED(kb_device->SetCooperativeLevel(nullptr, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)) && + SUCCEEDED(idi8->CreateDevice(GUID_SysMouse, &mo_device, nullptr)) && + SUCCEEDED(mo_device->SetDataFormat(&c_dfDIMouse2)) && + SUCCEEDED(mo_device->SetCooperativeLevel(nullptr, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE))) { - if (SUCCEEDED(kb_device->SetDataFormat(&c_dfDIKeyboard))) - { - if (SUCCEEDED(kb_device->SetCooperativeLevel(nullptr, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE))) - { - if (SUCCEEDED(idi8->CreateDevice(GUID_SysMouse, &mo_device, nullptr))) - { - if (SUCCEEDED(mo_device->SetDataFormat(&c_dfDIMouse2))) - { - if (SUCCEEDED( - mo_device->SetCooperativeLevel(nullptr, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE))) - { - g_controller_interface.AddDevice(new KeyboardMouse(kb_device, mo_device)); - return; - } - } - } - } - } + g_controller_interface.AddDevice(new KeyboardMouse(kb_device, mo_device)); + return; } if (kb_device)