Skip to content

Commit

Permalink
Merge pull request #3892 from leoetlino/ciface-synchronisation
Browse files Browse the repository at this point in the history
ControllerInterface: Add synchronisation
  • Loading branch information
Parlane committed Jun 25, 2016
2 parents 2f9a911 + 9b07555 commit a744827
Show file tree
Hide file tree
Showing 25 changed files with 83 additions and 90 deletions.
13 changes: 4 additions & 9 deletions Source/Core/InputCommon/ControllerInterface/Android/Android.cpp
Expand Up @@ -4,21 +4,16 @@

#include "InputCommon/ControllerInterface/Android/Android.h"
#include <sstream>
#include "InputCommon/ControllerInterface/ControllerInterface.h"

namespace ciface
{
namespace Android
{
void Init(std::vector<Core::Device*>& 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
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/InputCommon/ControllerInterface/Android/Android.h
Expand Up @@ -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<Core::Device*>& devices);
void Init();
class Touchscreen : public Core::Device
{
private:
Expand Down
Expand Up @@ -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;
Expand All @@ -104,6 +104,8 @@ void ControllerInterface::Shutdown()
if (!m_is_init)
return;

std::lock_guard<std::mutex> lk(m_devices_mutex);

for (ciface::Core::Device* d : m_devices)
{
// Set outputs to ZERO before destroying device
Expand Down Expand Up @@ -139,13 +141,20 @@ void ControllerInterface::Shutdown()
m_is_init = false;
}

void ControllerInterface::AddDevice(ciface::Core::Device* device)
{
std::lock_guard<std::mutex> lk(m_devices_mutex);
m_devices.push_back(device);
}

//
// UpdateInput
//
// Update input for all devices
//
void ControllerInterface::UpdateInput()
{
std::lock_guard<std::mutex> lk(m_devices_mutex);
for (ciface::Core::Device* d : m_devices)
d->UpdateInput();
}
Expand Down
Expand Up @@ -6,6 +6,7 @@

#include <algorithm>
#include <map>
#include <mutex>
#include <sstream>
#include <string>
#include <vector>
Expand Down Expand Up @@ -121,6 +122,7 @@ class ControllerInterface : public ciface::Core::DeviceContainer
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;
Expand Down
10 changes: 5 additions & 5 deletions Source/Core/InputCommon/ControllerInterface/DInput/DInput.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -44,7 +44,7 @@ std::string GetDeviceName(const LPDIRECTINPUTDEVICE8 device)
return result;
}

void Init(std::vector<Core::Device*>& devices, HWND hwnd)
void Init(HWND hwnd)
{
IDirectInput8* idi8;
if (FAILED(DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION, IID_IDirectInput8,
Expand All @@ -53,8 +53,8 @@ void Init(std::vector<Core::Device*>& devices, HWND hwnd)
return;
}

InitKeyboardMouse(idi8, devices, hwnd);
InitJoystick(idi8, devices, hwnd);
InitKeyboardMouse(idi8, hwnd);
InitJoystick(idi8, hwnd);

idi8->Release();
}
Expand Down
3 changes: 1 addition & 2 deletions Source/Core/InputCommon/ControllerInterface/DInput/DInput.h
Expand Up @@ -10,7 +10,6 @@
#include <windows.h>

#include "InputCommon/ControllerInterface/DInput/DInput8.h"
#include "InputCommon/ControllerInterface/Device.h"

namespace ciface
{
Expand All @@ -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<Core::Device*>& devices, HWND hwnd);
void Init(HWND hwnd);
}
}
Expand Up @@ -6,6 +6,7 @@
#include <map>
#include <sstream>

#include "InputCommon/ControllerInterface/ControllerInterface.h"
#include "InputCommon/ControllerInterface/DInput/DInput.h"
#include "InputCommon/ControllerInterface/DInput/DInputJoystick.h"
#include "InputCommon/ControllerInterface/DInput/XInputFilter.h"
Expand All @@ -16,7 +17,7 @@ namespace DInput
{
#define DATA_BUFFER_SIZE 32

void InitJoystick(IDirectInput8* const idi8, std::vector<Core::Device*>& devices, HWND hwnd)
void InitJoystick(IDirectInput8* const idi8, HWND hwnd)
{
std::list<DIDEVICEINSTANCE> joysticks;
idi8->EnumDevices(DI8DEVCLASS_GAMECTRL, DIEnumDevicesCallback, (LPVOID)&joysticks,
Expand Down Expand Up @@ -60,7 +61,7 @@ void InitJoystick(IDirectInput8* const idi8, std::vector<Core::Device*>& 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;
}
Expand Down
Expand Up @@ -11,7 +11,7 @@ namespace ciface
{
namespace DInput
{
void InitJoystick(IDirectInput8* const idi8, std::vector<Core::Device*>& devices, HWND hwnd);
void InitJoystick(IDirectInput8* const idi8, HWND hwnd);

class Joystick : public ForceFeedback::ForceFeedbackDevice
{
Expand Down
Expand Up @@ -4,6 +4,7 @@

#include <algorithm>

#include "InputCommon/ControllerInterface/ControllerInterface.h"
#include "InputCommon/ControllerInterface/DInput/DInput.h"
#include "InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.h"

Expand Down Expand Up @@ -31,7 +32,7 @@ static const struct
// lil silly
static HWND m_hwnd;

void InitKeyboardMouse(IDirectInput8* const idi8, std::vector<Core::Device*>& devices, HWND _hwnd)
void InitKeyboardMouse(IDirectInput8* const idi8, HWND _hwnd)
{
m_hwnd = _hwnd;

Expand All @@ -43,26 +44,15 @@ void InitKeyboardMouse(IDirectInput8* const idi8, std::vector<Core::Device*>& de
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)))
{
devices.push_back(new KeyboardMouse(kb_device, mo_device));
return;
}
}
}
}
}
g_controller_interface.AddDevice(new KeyboardMouse(kb_device, mo_device));
return;
}

if (kb_device)
Expand Down
Expand Up @@ -13,7 +13,7 @@ namespace ciface
{
namespace DInput
{
void InitKeyboardMouse(IDirectInput8* const idi8, std::vector<Core::Device*>& devices, HWND _hwnd);
void InitKeyboardMouse(IDirectInput8* const idi8, HWND _hwnd);

class KeyboardMouse : public Core::Device
{
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/InputCommon/ControllerInterface/Device.h
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <mutex>
#include <string>
#include <vector>

Expand Down Expand Up @@ -167,6 +168,7 @@ class DeviceContainer
Device* FindDevice(const DeviceQualifier& devq) const;

protected:
std::mutex m_devices_mutex;
std::vector<Device*> m_devices;
};
}
Expand Down
4 changes: 1 addition & 3 deletions Source/Core/InputCommon/ControllerInterface/OSX/OSX.h
Expand Up @@ -4,13 +4,11 @@

#pragma once

#include "InputCommon/ControllerInterface/Device.h"

namespace ciface
{
namespace OSX
{
void Init(std::vector<Core::Device*>& devices, void* window);
void Init(void* window);
void DeInit();

void DeviceElementDebugPrint(const void*, void*);
Expand Down
16 changes: 8 additions & 8 deletions Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm
Expand Up @@ -6,6 +6,7 @@
#include <Foundation/Foundation.h>
#include <IOKit/hid/IOHIDLib.h>

#include "InputCommon/ControllerInterface/ControllerInterface.h"
#include "InputCommon/ControllerInterface/OSX/OSX.h"
#include "InputCommon/ControllerInterface/OSX/OSXJoystick.h"
#include "InputCommon/ControllerInterface/OSX/OSXKeyboard.h"
Expand Down Expand Up @@ -140,22 +141,21 @@ static void DeviceMatching_callback(void* inContext, IOReturn inResult, void* in

DeviceDebugPrint(inIOHIDDeviceRef);

std::vector<Core::Device*>* devices = (std::vector<Core::Device*>*)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<Core::Device*>& devices, void* window)
void Init(void* window)
{
HIDManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
if (!HIDManager)
Expand All @@ -166,7 +166,7 @@ void Init(std::vector<Core::Device*>& 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);
Expand Down
5 changes: 3 additions & 2 deletions Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.cpp
Expand Up @@ -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
Expand All @@ -40,7 +41,7 @@ static double StringToDouble(const std::string& text)
return result;
}

void Init(std::vector<Core::Device*>& 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.
Expand All @@ -60,7 +61,7 @@ void Init(std::vector<Core::Device*>& 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++));
}
}

Expand Down
4 changes: 1 addition & 3 deletions Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.h
Expand Up @@ -8,8 +8,6 @@
#include <string>
#include <vector>

#include "InputCommon/ControllerInterface/Device.h"

namespace ciface
{
namespace Pipes
Expand All @@ -24,7 +22,7 @@ namespace Pipes
// SET {L, R} [0, 1]
// SET {MAIN, C} [0, 1] [0, 1]

void Init(std::vector<Core::Device*>& devices);
void Init();

class PipeDevice : public Core::Device
{
Expand Down

0 comments on commit a744827

Please sign in to comment.