2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2014-02-10 18:54:46 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2016-06-25 19:46:39 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
2010-04-02 02:48:24 +00:00
|
|
|
|
|
|
|
#ifdef CIFACE_USE_XINPUT
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/ControllerInterface/XInput/XInput.h"
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2010-07-03 08:04:10 +00:00
|
|
|
#ifdef CIFACE_USE_DINPUT
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/ControllerInterface/DInput/DInput.h"
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_XLIB
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/ControllerInterface/Xlib/XInput2.h"
|
|
|
|
#endif
|
2010-04-02 09:41:43 +00:00
|
|
|
#ifdef CIFACE_USE_OSX
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/ControllerInterface/OSX/OSX.h"
|
2016-08-08 16:38:22 +00:00
|
|
|
#include "InputCommon/ControllerInterface/Quartz/Quartz.h"
|
2010-04-02 09:41:43 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
#ifdef CIFACE_USE_SDL
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/ControllerInterface/SDL/SDL.h"
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2013-04-15 04:02:53 +00:00
|
|
|
#ifdef CIFACE_USE_ANDROID
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/ControllerInterface/Android/Android.h"
|
2013-04-15 04:02:53 +00:00
|
|
|
#endif
|
2015-06-29 00:17:35 +00:00
|
|
|
#ifdef CIFACE_USE_EVDEV
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/ControllerInterface/evdev/evdev.h"
|
2015-06-29 00:17:35 +00:00
|
|
|
#endif
|
2015-10-25 03:20:03 +00:00
|
|
|
#ifdef CIFACE_USE_PIPES
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/ControllerInterface/Pipes/Pipes.h"
|
2015-10-25 03:20:03 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2010-10-12 19:42:29 +00:00
|
|
|
ControllerInterface g_controller_interface;
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Init
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Detect devices and inputs outputs / will make refresh function later
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-10-13 16:45:47 +00:00
|
|
|
void ControllerInterface::Initialize(void* const hwnd)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_is_init)
|
|
|
|
return;
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_hwnd = hwnd;
|
2014-10-13 16:45:47 +00:00
|
|
|
|
2010-07-03 08:04:10 +00:00
|
|
|
#ifdef CIFACE_USE_DINPUT
|
2016-10-16 20:39:05 +00:00
|
|
|
// nothing needed
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2010-06-12 12:57:28 +00:00
|
|
|
#ifdef CIFACE_USE_XINPUT
|
2016-06-12 15:08:04 +00:00
|
|
|
ciface::XInput::Init();
|
2010-06-12 12:57:28 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
#ifdef CIFACE_USE_XLIB
|
2016-10-16 20:39:05 +00:00
|
|
|
// nothing needed
|
2016-06-24 08:43:46 +00:00
|
|
|
#endif
|
2010-04-02 09:41:43 +00:00
|
|
|
#ifdef CIFACE_USE_OSX
|
2016-06-12 15:08:04 +00:00
|
|
|
ciface::OSX::Init(hwnd);
|
2016-10-16 20:39:05 +00:00
|
|
|
// nothing needed for Quartz
|
2010-04-02 09:41:43 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
#ifdef CIFACE_USE_SDL
|
2016-06-12 15:08:04 +00:00
|
|
|
ciface::SDL::Init();
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2013-04-15 04:02:53 +00:00
|
|
|
#ifdef CIFACE_USE_ANDROID
|
2016-10-16 20:39:05 +00:00
|
|
|
// nothing needed
|
2013-04-15 04:02:53 +00:00
|
|
|
#endif
|
2015-06-29 00:17:35 +00:00
|
|
|
#ifdef CIFACE_USE_EVDEV
|
2016-06-12 15:08:04 +00:00
|
|
|
ciface::evdev::Init();
|
2015-06-29 00:17:35 +00:00
|
|
|
#endif
|
2015-10-25 03:20:03 +00:00
|
|
|
#ifdef CIFACE_USE_PIPES
|
2016-10-16 20:39:05 +00:00
|
|
|
// nothing needed
|
2015-10-25 03:20:03 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_is_init = true;
|
2016-10-16 20:39:05 +00:00
|
|
|
RefreshDevices();
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 20:39:05 +00:00
|
|
|
void ControllerInterface::RefreshDevices()
|
2014-10-13 16:45:47 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!m_is_init)
|
|
|
|
return;
|
2014-10-13 16:45:47 +00:00
|
|
|
|
2016-10-16 20:39:05 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(m_devices_mutex);
|
|
|
|
m_devices.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CIFACE_USE_DINPUT
|
|
|
|
ciface::DInput::PopulateDevices(reinterpret_cast<HWND>(m_hwnd));
|
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_XINPUT
|
|
|
|
ciface::XInput::PopulateDevices();
|
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_XLIB
|
|
|
|
ciface::XInput2::PopulateDevices(m_hwnd);
|
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_OSX
|
|
|
|
ciface::OSX::PopulateDevices(m_hwnd);
|
|
|
|
ciface::Quartz::PopulateDevices(m_hwnd);
|
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_SDL
|
|
|
|
ciface::SDL::PopulateDevices();
|
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_ANDROID
|
|
|
|
ciface::Android::PopulateDevices();
|
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_EVDEV
|
|
|
|
ciface::evdev::PopulateDevices();
|
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_PIPES
|
|
|
|
ciface::Pipes::PopulateDevices();
|
|
|
|
#endif
|
2014-10-13 16:45:47 +00:00
|
|
|
}
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// DeInit
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Remove all devices/ call library cleanup functions
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2010-10-12 19:42:29 +00:00
|
|
|
void ControllerInterface::Shutdown()
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!m_is_init)
|
|
|
|
return;
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2016-10-11 19:41:29 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(m_devices_mutex);
|
|
|
|
|
|
|
|
for (const auto& d : m_devices)
|
|
|
|
{
|
|
|
|
// Set outputs to ZERO before destroying device
|
|
|
|
for (ciface::Core::Device::Output* o : d->Outputs())
|
|
|
|
o->SetState(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_devices.clear();
|
|
|
|
}
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
#ifdef CIFACE_USE_XINPUT
|
2016-06-24 08:43:46 +00:00
|
|
|
ciface::XInput::DeInit();
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2010-07-03 08:04:10 +00:00
|
|
|
#ifdef CIFACE_USE_DINPUT
|
2016-06-24 08:43:46 +00:00
|
|
|
// nothing needed
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_XLIB
|
2016-06-24 08:43:46 +00:00
|
|
|
// nothing needed
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2010-04-02 09:41:43 +00:00
|
|
|
#ifdef CIFACE_USE_OSX
|
2016-06-24 08:43:46 +00:00
|
|
|
ciface::OSX::DeInit();
|
2016-08-08 16:38:22 +00:00
|
|
|
ciface::Quartz::DeInit();
|
2010-04-02 09:41:43 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
#ifdef CIFACE_USE_SDL
|
2016-06-24 08:43:46 +00:00
|
|
|
// TODO: there seems to be some sort of memory leak with SDL, quit isn't freeing everything up
|
|
|
|
SDL_Quit();
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2013-04-15 04:02:53 +00:00
|
|
|
#ifdef CIFACE_USE_ANDROID
|
2016-06-24 08:43:46 +00:00
|
|
|
// nothing needed
|
2013-04-15 04:02:53 +00:00
|
|
|
#endif
|
2016-07-14 15:50:35 +00:00
|
|
|
#ifdef CIFACE_USE_EVDEV
|
|
|
|
ciface::evdev::Shutdown();
|
|
|
|
#endif
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_is_init = false;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 19:46:39 +00:00
|
|
|
void ControllerInterface::AddDevice(std::shared_ptr<ciface::Core::Device> device)
|
2016-06-12 15:08:04 +00:00
|
|
|
{
|
2016-06-12 15:31:41 +00:00
|
|
|
std::lock_guard<std::mutex> lk(m_devices_mutex);
|
2016-07-14 08:25:52 +00:00
|
|
|
// Try to find an ID for this device
|
|
|
|
int id = 0;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
const auto it = std::find_if(m_devices.begin(), m_devices.end(), [&device, &id](const auto& d) {
|
|
|
|
return d->GetSource() == device->GetSource() && d->GetName() == device->GetName() &&
|
|
|
|
d->GetId() == id;
|
|
|
|
});
|
|
|
|
if (it == m_devices.end()) // no device with the same name with this ID, so we can use it
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
id++;
|
|
|
|
}
|
|
|
|
device->SetId(id);
|
2016-06-25 19:46:39 +00:00
|
|
|
m_devices.emplace_back(std::move(device));
|
2016-06-12 15:08:04 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 15:45:59 +00:00
|
|
|
void ControllerInterface::RemoveDevice(std::function<bool(const ciface::Core::Device*)> callback)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(m_devices_mutex);
|
|
|
|
m_devices.erase(std::remove_if(m_devices.begin(), m_devices.end(),
|
|
|
|
[&callback](const auto& dev) { return callback(dev.get()); }),
|
|
|
|
m_devices.end());
|
|
|
|
}
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// UpdateInput
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2016-01-09 18:42:56 +00:00
|
|
|
// Update input for all devices
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-11-13 08:55:14 +00:00
|
|
|
void ControllerInterface::UpdateInput()
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-07-15 09:34:18 +00:00
|
|
|
// Don't block the UI or CPU thread (to avoid a short but noticeable frame drop)
|
|
|
|
if (m_devices_mutex.try_lock())
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(m_devices_mutex, std::adopt_lock);
|
|
|
|
for (const auto& d : m_devices)
|
|
|
|
d->UpdateInput();
|
|
|
|
}
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2016-06-13 09:11:47 +00:00
|
|
|
//
|
|
|
|
// RegisterHotplugCallback
|
|
|
|
//
|
|
|
|
// Register a callback to be called from the input backends' hotplug thread
|
|
|
|
// when there is a new device
|
|
|
|
//
|
|
|
|
void ControllerInterface::RegisterHotplugCallback(std::function<void()> callback)
|
|
|
|
{
|
|
|
|
m_hotplug_callbacks.emplace_back(std::move(callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// InvokeHotplugCallbacks
|
|
|
|
//
|
|
|
|
// Invoke all callbacks that were registered
|
|
|
|
//
|
|
|
|
void ControllerInterface::InvokeHotplugCallbacks() const
|
|
|
|
{
|
|
|
|
for (const auto& callback : m_hotplug_callbacks)
|
|
|
|
callback();
|
|
|
|
}
|