[HID] Pass 'is active' callback to input drivers.

This commit is contained in:
gibbed
2020-11-27 05:20:17 -06:00
committed by Rick Gibbed
parent f8e6ac4108
commit dd25e69e66
8 changed files with 39 additions and 13 deletions

View File

@@ -10,7 +10,10 @@
#ifndef XENIA_HID_INPUT_DRIVER_H_
#define XENIA_HID_INPUT_DRIVER_H_
#include <functional>
#include "xenia/hid/input.h"
#include "xenia/ui/window.h"
#include "xenia/xbox.h"
namespace xe {
@@ -38,10 +41,22 @@ class InputDriver {
virtual X_RESULT GetKeystroke(uint32_t user_index, uint32_t flags,
X_INPUT_KEYSTROKE* out_keystroke) = 0;
void set_is_active_callback(std::function<bool()> is_active_callback) {
is_active_callback_ = is_active_callback;
}
private:
xe::ui::Window* window_ = nullptr;
std::function<bool()> is_active_callback_ = nullptr;
protected:
explicit InputDriver(xe::ui::Window* window);
xe::ui::Window* window_ = nullptr;
xe::ui::Window* window() const { return window_; }
bool is_active() const {
return !is_active_callback_ || is_active_callback_();
}
};
} // namespace hid

View File

@@ -65,7 +65,7 @@ X_STATUS SDLInputDriver::Setup() {
// SDL_PumpEvents should only be run in the thread that initialized SDL - we
// are hijacking the window loop thread for that.
window_->loop()->PostSynchronous([&]() {
window()->loop()->PostSynchronous([&]() {
if (!xe::helper::sdl::SDLHelper::Prepare()) {
return;
}
@@ -385,7 +385,7 @@ X_RESULT SDLInputDriver::GetKeystroke(uint32_t users, uint32_t flags,
}
void SDLInputDriver::OnControllerDeviceAdded(SDL_Event* event) {
assert(window_->loop()->is_on_loop_thread());
assert(window()->loop()->is_on_loop_thread());
std::unique_lock<std::mutex> guard(controllers_mutex_);
// Open the controller.
@@ -424,7 +424,7 @@ void SDLInputDriver::OnControllerDeviceAdded(SDL_Event* event) {
}
void SDLInputDriver::OnControllerDeviceRemoved(SDL_Event* event) {
assert(window_->loop()->is_on_loop_thread());
assert(window()->loop()->is_on_loop_thread());
std::unique_lock<std::mutex> guard(controllers_mutex_);
// Find the disconnected gamecontroller and close it.
@@ -436,7 +436,7 @@ void SDLInputDriver::OnControllerDeviceRemoved(SDL_Event* event) {
}
void SDLInputDriver::OnControllerDeviceAxisMotion(SDL_Event* event) {
assert(window_->loop()->is_on_loop_thread());
assert(window()->loop()->is_on_loop_thread());
std::unique_lock<std::mutex> guard(controllers_mutex_);
auto [found, i] = GetControllerIndexFromInstanceID(event->caxis.which);
@@ -469,7 +469,7 @@ void SDLInputDriver::OnControllerDeviceAxisMotion(SDL_Event* event) {
}
void SDLInputDriver::OnControllerDeviceButtonChanged(SDL_Event* event) {
assert(window_->loop()->is_on_loop_thread());
assert(window()->loop()->is_on_loop_thread());
std::unique_lock<std::mutex> guard(controllers_mutex_);
// Define a lookup table to map between SDL and XInput button codes.
@@ -569,7 +569,7 @@ void SDLInputDriver::QueueControllerUpdate() {
bool is_queued = false;
sdl_pumpevents_queued_.compare_exchange_strong(is_queued, true);
if (!is_queued) {
window_->loop()->Post([this]() {
window()->loop()->Post([this]() {
SDL_PumpEvents();
sdl_pumpevents_queued_ = false;
});

View File

@@ -21,7 +21,7 @@ namespace winkey {
WinKeyInputDriver::WinKeyInputDriver(xe::ui::Window* window)
: InputDriver(window), packet_number_(1) {
// Register a key listener.
window_->on_key_down.AddListener([this](ui::KeyEvent* evt) {
window->on_key_down.AddListener([this](ui::KeyEvent* evt) {
auto global_lock = global_critical_region_.Acquire();
KeyEvent key;
@@ -31,7 +31,7 @@ WinKeyInputDriver::WinKeyInputDriver(xe::ui::Window* window)
key.repeat_count = evt->repeat_count();
key_events_.push(key);
});
window_->on_key_up.AddListener([this](ui::KeyEvent* evt) {
window->on_key_up.AddListener([this](ui::KeyEvent* evt) {
auto global_lock = global_critical_region_.Acquire();
KeyEvent key;
@@ -88,7 +88,7 @@ X_RESULT WinKeyInputDriver::GetState(uint32_t user_index,
int16_t thumb_rx = 0;
int16_t thumb_ry = 0;
if (window_->has_focus()) {
if (window()->has_focus()) {
if (IS_KEY_TOGGLED(VK_CAPITAL) || IS_KEY_DOWN(VK_SHIFT)) {
// dpad toggled
if (IS_KEY_DOWN('A')) {