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.
|
|
|
|
|
2017-11-09 20:14:21 +00:00
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
|
|
|
|
2017-11-10 20:29:25 +00:00
|
|
|
#include <algorithm>
|
2016-06-25 19:46:39 +00:00
|
|
|
|
2017-11-09 20:14:21 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2020-01-22 00:50:05 +00:00
|
|
|
#include "Core/HW/WiimoteReal/WiimoteReal.h"
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2017-11-10 17:56:13 +00:00
|
|
|
#ifdef CIFACE_USE_WIN32
|
|
|
|
#include "InputCommon/ControllerInterface/Win32/Win32.h"
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_XLIB
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "InputCommon/ControllerInterface/Xlib/XInput2.h"
|
2013-07-10 06:49:58 +00:00
|
|
|
#endif
|
2010-04-02 09:41:43 +00:00
|
|
|
#ifdef CIFACE_USE_OSX
|
2014-02-17 10:18:15 +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
|
2014-02-17 10:18:15 +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
|
2014-02-17 10:18:15 +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
|
|
|
|
#include "InputCommon/ControllerInterface/evdev/evdev.h"
|
|
|
|
#endif
|
2015-10-25 03:20:03 +00:00
|
|
|
#ifdef CIFACE_USE_PIPES
|
|
|
|
#include "InputCommon/ControllerInterface/Pipes/Pipes.h"
|
|
|
|
#endif
|
2019-10-26 16:05:16 +00:00
|
|
|
#ifdef CIFACE_USE_DUALSHOCKUDPCLIENT
|
|
|
|
#include "InputCommon/ControllerInterface/DualShockUDPClient/DualShockUDPClient.h"
|
2019-09-06 15:09:30 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2010-10-12 19:42:29 +00:00
|
|
|
ControllerInterface g_controller_interface;
|
|
|
|
|
2018-10-03 07:34:27 +00:00
|
|
|
void ControllerInterface::Initialize(const WindowSystemInfo& wsi)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-10-12 19:42:29 +00:00
|
|
|
if (m_is_init)
|
2010-04-02 02:48:24 +00:00
|
|
|
return;
|
|
|
|
|
2018-10-03 07:34:27 +00:00
|
|
|
m_wsi = wsi;
|
2019-01-02 14:19:42 +00:00
|
|
|
|
|
|
|
// Allow backends to add devices as soon as they are initialized.
|
|
|
|
m_is_init = true;
|
|
|
|
|
2017-11-04 14:37:03 +00:00
|
|
|
m_is_populating_devices = true;
|
2014-10-13 16:45:47 +00:00
|
|
|
|
2017-11-10 17:56:13 +00:00
|
|
|
#ifdef CIFACE_USE_WIN32
|
2020-03-11 13:09:28 +00:00
|
|
|
ciface::Win32::Init(wsi.render_window);
|
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
|
2013-07-10 06:49:58 +00:00
|
|
|
#endif
|
2010-04-02 09:41:43 +00:00
|
|
|
#ifdef CIFACE_USE_OSX
|
2018-10-03 07:34:27 +00:00
|
|
|
if (m_wsi.type == WindowSystemType::MacOS)
|
2020-03-11 13:09:28 +00:00
|
|
|
ciface::OSX::Init(wsi.render_window);
|
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
|
2019-09-06 15:09:30 +00:00
|
|
|
#endif
|
2019-10-26 16:05:16 +00:00
|
|
|
#ifdef CIFACE_USE_DUALSHOCKUDPCLIENT
|
|
|
|
ciface::DualShockUDPClient::Init();
|
2015-10-25 03:20:03 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2016-10-16 20:39:05 +00:00
|
|
|
RefreshDevices();
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 10:06:35 +00:00
|
|
|
void ControllerInterface::ChangeWindow(void* hwnd)
|
|
|
|
{
|
|
|
|
if (!m_is_init)
|
|
|
|
return;
|
|
|
|
|
2020-03-11 13:09:28 +00:00
|
|
|
// This shouldn't use render_surface so no need to update it.
|
|
|
|
m_wsi.render_window = hwnd;
|
2018-10-27 10:06:35 +00:00
|
|
|
RefreshDevices();
|
|
|
|
}
|
|
|
|
|
2016-10-16 20:39:05 +00:00
|
|
|
void ControllerInterface::RefreshDevices()
|
2014-10-13 16:45:47 +00:00
|
|
|
{
|
|
|
|
if (!m_is_init)
|
|
|
|
return;
|
|
|
|
|
2016-10-16 20:39:05 +00:00
|
|
|
{
|
2020-01-22 00:52:58 +00:00
|
|
|
std::lock_guard lk(m_devices_mutex);
|
2016-10-16 20:39:05 +00:00
|
|
|
m_devices.clear();
|
|
|
|
}
|
|
|
|
|
2017-11-04 14:37:03 +00:00
|
|
|
m_is_populating_devices = true;
|
|
|
|
|
2019-03-09 15:57:37 +00:00
|
|
|
// Make sure shared_ptr<Device> objects are released before repopulating.
|
|
|
|
InvokeDevicesChangedCallbacks();
|
|
|
|
|
2017-11-10 17:56:13 +00:00
|
|
|
#ifdef CIFACE_USE_WIN32
|
2020-05-06 00:30:49 +00:00
|
|
|
// Should this be changed for other platforms? I don't have the means to test...
|
|
|
|
ciface::Win32::PopulateDevices(m_wsi.render_window);
|
2016-10-16 20:39:05 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_XLIB
|
2018-10-03 07:34:27 +00:00
|
|
|
if (m_wsi.type == WindowSystemType::X11)
|
|
|
|
ciface::XInput2::PopulateDevices(m_wsi.render_surface);
|
2016-10-16 20:39:05 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_OSX
|
2018-10-03 07:34:27 +00:00
|
|
|
if (m_wsi.type == WindowSystemType::MacOS)
|
|
|
|
{
|
|
|
|
ciface::OSX::PopulateDevices(m_wsi.render_surface);
|
|
|
|
ciface::Quartz::PopulateDevices(m_wsi.render_surface);
|
|
|
|
}
|
2016-10-16 20:39:05 +00:00
|
|
|
#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
|
2019-10-26 16:05:16 +00:00
|
|
|
#ifdef CIFACE_USE_DUALSHOCKUDPCLIENT
|
|
|
|
ciface::DualShockUDPClient::PopulateDevices();
|
2019-09-06 15:09:30 +00:00
|
|
|
#endif
|
2020-01-22 00:50:05 +00:00
|
|
|
|
|
|
|
WiimoteReal::ProcessWiimotePool();
|
2017-11-04 14:37:03 +00:00
|
|
|
|
|
|
|
m_is_populating_devices = false;
|
|
|
|
InvokeDevicesChangedCallbacks();
|
2014-10-13 16:45:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 14:19:42 +00:00
|
|
|
// Remove all devices and call library cleanup functions
|
2010-10-12 19:42:29 +00:00
|
|
|
void ControllerInterface::Shutdown()
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2014-01-31 00:51:21 +00:00
|
|
|
if (!m_is_init)
|
2010-04-02 02:48:24 +00:00
|
|
|
return;
|
|
|
|
|
2019-01-02 14:19:42 +00:00
|
|
|
// Prevent additional devices from being added during shutdown.
|
|
|
|
m_is_init = false;
|
|
|
|
|
2016-10-11 19:41:29 +00:00
|
|
|
{
|
2020-01-22 00:52:58 +00:00
|
|
|
std::lock_guard lk(m_devices_mutex);
|
2016-10-11 19:41:29 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-01-02 14:19:42 +00:00
|
|
|
// This will update control references so shared_ptr<Device>s are freed up
|
|
|
|
// BEFORE we shutdown the backends.
|
|
|
|
InvokeDevicesChangedCallbacks();
|
|
|
|
|
2017-11-10 17:56:13 +00:00
|
|
|
#ifdef CIFACE_USE_WIN32
|
|
|
|
ciface::Win32::DeInit();
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_XLIB
|
2017-11-10 20:30:37 +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
|
2010-04-25 18:04:55 +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
|
2017-11-09 22:06:58 +00:00
|
|
|
ciface::SDL::DeInit();
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2013-04-15 04:02:53 +00:00
|
|
|
#ifdef CIFACE_USE_ANDROID
|
|
|
|
// nothing needed
|
|
|
|
#endif
|
2016-07-14 15:50:35 +00:00
|
|
|
#ifdef CIFACE_USE_EVDEV
|
|
|
|
ciface::evdev::Shutdown();
|
|
|
|
#endif
|
2019-10-26 16:05:16 +00:00
|
|
|
#ifdef CIFACE_USE_DUALSHOCKUDPCLIENT
|
|
|
|
ciface::DualShockUDPClient::DeInit();
|
2019-09-06 15:09:30 +00:00
|
|
|
#endif
|
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
|
|
|
{
|
2019-01-02 14:19:42 +00:00
|
|
|
// If we are shutdown (or in process of shutting down) ignore this request:
|
|
|
|
if (!m_is_init)
|
|
|
|
return;
|
|
|
|
|
2016-07-14 08:25:52 +00:00
|
|
|
{
|
2020-01-22 00:52:58 +00:00
|
|
|
std::lock_guard lk(m_devices_mutex);
|
2019-03-09 15:57:37 +00:00
|
|
|
|
|
|
|
const auto is_id_in_use = [&device, this](int id) {
|
|
|
|
return std::any_of(m_devices.begin(), m_devices.end(), [&device, &id](const auto& d) {
|
|
|
|
return d->GetSource() == device->GetSource() && d->GetName() == device->GetName() &&
|
|
|
|
d->GetId() == id;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto preferred_id = device->GetPreferredId();
|
|
|
|
if (preferred_id.has_value() && !is_id_in_use(*preferred_id))
|
2017-11-09 20:14:21 +00:00
|
|
|
{
|
2019-03-09 15:57:37 +00:00
|
|
|
// Use the device's preferred ID if available.
|
|
|
|
device->SetId(*preferred_id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Find the first available ID to use.
|
|
|
|
int id = 0;
|
|
|
|
while (is_id_in_use(id))
|
|
|
|
++id;
|
|
|
|
|
|
|
|
device->SetId(id);
|
2017-11-09 20:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NOTICE_LOG(SERIALINTERFACE, "Added device: %s", device->GetQualifiedName().c_str());
|
|
|
|
m_devices.emplace_back(std::move(device));
|
2016-07-14 08:25:52 +00:00
|
|
|
}
|
2017-11-04 14:37:03 +00:00
|
|
|
|
|
|
|
if (!m_is_populating_devices)
|
|
|
|
InvokeDevicesChangedCallbacks();
|
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)
|
|
|
|
{
|
2017-11-09 20:14:21 +00:00
|
|
|
{
|
2020-01-22 00:52:58 +00:00
|
|
|
std::lock_guard lk(m_devices_mutex);
|
2017-11-09 20:14:21 +00:00
|
|
|
auto it = std::remove_if(m_devices.begin(), m_devices.end(), [&callback](const auto& dev) {
|
|
|
|
if (callback(dev.get()))
|
|
|
|
{
|
|
|
|
NOTICE_LOG(SERIALINTERFACE, "Removed device: %s", dev->GetQualifiedName().c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
m_devices.erase(it, m_devices.end());
|
|
|
|
}
|
2017-11-04 14:37:03 +00:00
|
|
|
|
|
|
|
if (!m_is_populating_devices)
|
|
|
|
InvokeDevicesChangedCallbacks();
|
2016-07-14 15:45:59 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 14:19:42 +00:00
|
|
|
// Update input for all devices if lock can be acquired without waiting.
|
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())
|
|
|
|
{
|
2020-01-22 00:52:58 +00:00
|
|
|
std::lock_guard lk(m_devices_mutex, std::adopt_lock);
|
2016-07-15 09:34:18 +00:00
|
|
|
for (const auto& d : m_devices)
|
|
|
|
d->UpdateInput();
|
|
|
|
}
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 06:06:39 +00:00
|
|
|
void ControllerInterface::SetAspectRatioAdjustment(float value)
|
|
|
|
{
|
|
|
|
m_aspect_ratio_adjustment = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::Vec2 ControllerInterface::GetWindowInputScale() const
|
|
|
|
{
|
|
|
|
const auto ar = m_aspect_ratio_adjustment.load();
|
|
|
|
|
|
|
|
if (ar > 1)
|
|
|
|
return {1.f, ar};
|
|
|
|
else
|
|
|
|
return {1 / ar, 1.f};
|
|
|
|
}
|
|
|
|
|
2017-11-04 14:36:30 +00:00
|
|
|
// Register a callback to be called when a device is added or removed (as from the input backends'
|
|
|
|
// hotplug thread), or when devices are refreshed
|
2019-01-10 15:02:38 +00:00
|
|
|
// Returns a handle for later removing the callback.
|
|
|
|
ControllerInterface::HotplugCallbackHandle
|
|
|
|
ControllerInterface::RegisterDevicesChangedCallback(std::function<void()> callback)
|
2016-06-13 09:11:47 +00:00
|
|
|
{
|
2017-11-10 20:29:25 +00:00
|
|
|
std::lock_guard<std::mutex> lk(m_callbacks_mutex);
|
2017-11-04 14:36:30 +00:00
|
|
|
m_devices_changed_callbacks.emplace_back(std::move(callback));
|
2019-01-10 15:02:38 +00:00
|
|
|
return std::prev(m_devices_changed_callbacks.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unregister a device callback.
|
|
|
|
void ControllerInterface::UnregisterDevicesChangedCallback(const HotplugCallbackHandle& handle)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(m_callbacks_mutex);
|
|
|
|
m_devices_changed_callbacks.erase(handle);
|
2016-06-13 09:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke all callbacks that were registered
|
2017-11-04 14:36:30 +00:00
|
|
|
void ControllerInterface::InvokeDevicesChangedCallbacks() const
|
2016-06-13 09:11:47 +00:00
|
|
|
{
|
2017-11-10 20:29:25 +00:00
|
|
|
std::lock_guard<std::mutex> lk(m_callbacks_mutex);
|
2017-11-04 14:36:30 +00:00
|
|
|
for (const auto& callback : m_devices_changed_callbacks)
|
2016-06-13 09:11:47 +00:00
|
|
|
callback();
|
|
|
|
}
|