2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2014-02-10 18:54:46 +00:00
|
|
|
|
|
|
|
#pragma once
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2017-11-04 14:37:03 +00:00
|
|
|
#include <atomic>
|
2017-02-19 08:00:00 +00:00
|
|
|
#include <functional>
|
2019-01-10 15:02:38 +00:00
|
|
|
#include <list>
|
2017-11-10 20:29:25 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2020-01-24 06:06:39 +00:00
|
|
|
#include "Common/Matrix.h"
|
2018-10-03 07:34:27 +00:00
|
|
|
#include "Common/WindowSystemInfo.h"
|
2020-09-15 11:34:41 +00:00
|
|
|
#include "InputCommon/ControllerInterface/CoreDevice.h"
|
2010-04-02 02:48:24 +00:00
|
|
|
|
|
|
|
// enable disable sources
|
|
|
|
#ifdef _WIN32
|
2017-11-10 17:56:13 +00:00
|
|
|
#define CIFACE_USE_WIN32
|
2010-07-16 19:17:35 +00:00
|
|
|
#endif
|
2021-02-03 00:25:06 +00:00
|
|
|
#ifdef HAVE_X11
|
2010-07-16 19:17:35 +00:00
|
|
|
#define CIFACE_USE_XLIB
|
|
|
|
#endif
|
2010-04-02 09:41:43 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#define CIFACE_USE_OSX
|
|
|
|
#endif
|
2015-06-29 00:17:35 +00:00
|
|
|
#if defined(HAVE_LIBEVDEV) && defined(HAVE_LIBUDEV)
|
|
|
|
#define CIFACE_USE_EVDEV
|
|
|
|
#endif
|
2015-10-25 03:20:03 +00:00
|
|
|
#if defined(USE_PIPES)
|
|
|
|
#define CIFACE_USE_PIPES
|
|
|
|
#endif
|
2019-10-26 16:05:16 +00:00
|
|
|
#define CIFACE_USE_DUALSHOCKUDPCLIENT
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2020-04-21 04:16:07 +00:00
|
|
|
namespace ciface
|
|
|
|
{
|
|
|
|
// A thread local "input channel" is maintained to handle the state of relative inputs.
|
|
|
|
// This allows simultaneous use of relative inputs across different input contexts.
|
|
|
|
// e.g. binding relative mouse movements to both GameCube controllers and FreeLook.
|
|
|
|
// These operate at different rates and processing one would break the other without separate state.
|
|
|
|
enum class InputChannel
|
|
|
|
{
|
|
|
|
Host,
|
|
|
|
SerialInterface,
|
|
|
|
Bluetooth,
|
|
|
|
FreeLook,
|
|
|
|
Count,
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ciface
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// ControllerInterface
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Some crazy shit I made to control different device inputs and outputs
|
|
|
|
// from lots of different sources, hopefully more easily.
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-09-05 00:41:42 +00:00
|
|
|
class ControllerInterface : public ciface::Core::DeviceContainer
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-01-10 15:02:38 +00:00
|
|
|
using HotplugCallbackHandle = std::list<std::function<void()>>::iterator;
|
|
|
|
|
2021-05-15 09:06:12 +00:00
|
|
|
enum class WindowChangeReason
|
|
|
|
{
|
|
|
|
// Application is shutting down
|
|
|
|
Exit,
|
|
|
|
Other
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class RefreshReason
|
|
|
|
{
|
|
|
|
// Only the window changed.
|
|
|
|
WindowChangeOnly,
|
|
|
|
// User requested, or any other internal reason (e.g. init).
|
|
|
|
// The window might have changed anyway.
|
|
|
|
Other
|
|
|
|
};
|
|
|
|
|
2018-10-03 07:34:27 +00:00
|
|
|
ControllerInterface() : m_is_init(false) {}
|
|
|
|
void Initialize(const WindowSystemInfo& wsi);
|
2021-05-15 09:06:12 +00:00
|
|
|
// Only call from one thread at a time.
|
|
|
|
void ChangeWindow(void* hwnd, WindowChangeReason reason = WindowChangeReason::Other);
|
|
|
|
// Can be called by any thread at any time (when initialized).
|
|
|
|
void RefreshDevices(RefreshReason reason = RefreshReason::Other);
|
2010-10-12 19:42:29 +00:00
|
|
|
void Shutdown();
|
2021-05-15 09:06:12 +00:00
|
|
|
bool AddDevice(std::shared_ptr<ciface::Core::Device> device);
|
|
|
|
// Removes all the devices the function returns true to.
|
|
|
|
// If all the devices shared ptrs need to be destroyed immediately,
|
|
|
|
// set force_devices_release to true.
|
|
|
|
void RemoveDevice(std::function<bool(const ciface::Core::Device*)> callback,
|
|
|
|
bool force_devices_release = false);
|
2021-05-15 08:32:00 +00:00
|
|
|
// This is mandatory to use on device populations functions that can be called concurrently by
|
|
|
|
// more than one thread, or that are called by a single other thread.
|
|
|
|
// Without this, our devices list might end up in a mixed state.
|
2020-12-13 00:30:27 +00:00
|
|
|
void PlatformPopulateDevices(std::function<void()> callback);
|
2010-06-21 03:12:16 +00:00
|
|
|
bool IsInit() const { return m_is_init; }
|
2014-11-13 08:55:14 +00:00
|
|
|
void UpdateInput();
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2020-01-24 06:06:39 +00:00
|
|
|
// Set adjustment from the full render window aspect-ratio to the drawn aspect-ratio.
|
|
|
|
// Used to fit mouse cursor inputs to the relevant region of the render window.
|
|
|
|
void SetAspectRatioAdjustment(float);
|
|
|
|
|
|
|
|
// Calculated from the aspect-ratio adjustment.
|
|
|
|
// Inputs based on window coordinates should be multiplied by this.
|
|
|
|
Common::Vec2 GetWindowInputScale() const;
|
|
|
|
|
2019-01-10 15:02:38 +00:00
|
|
|
HotplugCallbackHandle RegisterDevicesChangedCallback(std::function<void(void)> callback);
|
|
|
|
void UnregisterDevicesChangedCallback(const HotplugCallbackHandle& handle);
|
2017-11-04 14:36:30 +00:00
|
|
|
void InvokeDevicesChangedCallbacks() const;
|
2016-06-13 09:11:47 +00:00
|
|
|
|
2020-04-21 04:16:07 +00:00
|
|
|
static void SetCurrentInputChannel(ciface::InputChannel);
|
|
|
|
static ciface::InputChannel GetCurrentInputChannel();
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
private:
|
2021-05-15 09:06:12 +00:00
|
|
|
void ClearDevices();
|
|
|
|
|
2019-01-10 15:02:38 +00:00
|
|
|
std::list<std::function<void()>> m_devices_changed_callbacks;
|
2021-05-15 09:06:12 +00:00
|
|
|
mutable std::recursive_mutex m_devices_population_mutex;
|
2021-05-15 09:08:38 +00:00
|
|
|
mutable std::mutex m_pre_population_mutex;
|
2017-11-10 20:29:25 +00:00
|
|
|
mutable std::mutex m_callbacks_mutex;
|
2019-01-02 14:19:42 +00:00
|
|
|
std::atomic<bool> m_is_init;
|
2021-05-15 09:06:12 +00:00
|
|
|
// This is now always protected by m_devices_population_mutex, so
|
|
|
|
// it doesn't really need to be a counter or atomic anymore (it could be a raw bool),
|
|
|
|
// but we keep it so for simplicity, in case we changed the design.
|
|
|
|
std::atomic<int> m_populating_devices_counter;
|
2018-10-03 07:34:27 +00:00
|
|
|
WindowSystemInfo m_wsi;
|
2020-01-24 06:06:39 +00:00
|
|
|
std::atomic<float> m_aspect_ratio_adjustment = 1;
|
2010-04-02 02:48:24 +00:00
|
|
|
};
|
|
|
|
|
2020-04-21 04:16:07 +00:00
|
|
|
namespace ciface
|
|
|
|
{
|
|
|
|
template <typename T>
|
|
|
|
class RelativeInputState
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
const auto channel = int(ControllerInterface::GetCurrentInputChannel());
|
|
|
|
|
|
|
|
m_value[channel] = m_delta[channel];
|
|
|
|
m_delta[channel] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
T GetValue() const
|
|
|
|
{
|
|
|
|
const auto channel = int(ControllerInterface::GetCurrentInputChannel());
|
|
|
|
|
|
|
|
return m_value[channel];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Move(T delta)
|
|
|
|
{
|
|
|
|
for (auto& d : m_delta)
|
|
|
|
d += delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::array<T, int(InputChannel::Count)> m_value;
|
|
|
|
std::array<T, int(InputChannel::Count)> m_delta;
|
|
|
|
};
|
|
|
|
} // namespace ciface
|
|
|
|
|
2010-10-12 19:42:29 +00:00
|
|
|
extern ControllerInterface g_controller_interface;
|