Qt/Input: piggyback on existing callback for battery_level

removes ds4 timer workaround
This commit is contained in:
Megamouse 2020-03-05 21:20:57 +01:00
parent f776910966
commit 9e449db0c2
15 changed files with 37 additions and 70 deletions

View File

@ -317,7 +317,7 @@ void PadHandlerBase::init_configs()
}
}
void PadHandlerBase::get_next_button_press(const std::string& pad_id, const std::function<void(u16, std::string, std::string, std::array<int, 6>)>& callback, const std::function<void(std::string)>& fail_callback, bool get_blacklist, const std::vector<std::string>& /*buttons*/)
void PadHandlerBase::get_next_button_press(const std::string& pad_id, const pad_callback& callback, const pad_fail_callback& fail_callback, bool get_blacklist, const std::vector<std::string>& /*buttons*/)
{
if (get_blacklist)
blacklist.clear();
@ -369,11 +369,12 @@ void PadHandlerBase::get_next_button_press(const std::string& pad_id, const std:
}
const auto preview_values = get_preview_values(data);
const auto battery_level = get_battery_level(pad_id);
if (pressed_button.first > 0)
return callback(pressed_button.first, pressed_button.second, pad_id, preview_values);
return callback(pressed_button.first, pressed_button.second, pad_id, battery_level, preview_values);
else
return callback(0, "", pad_id, preview_values);
return callback(0, "", pad_id, battery_level, preview_values);
return;
}

View File

@ -11,6 +11,10 @@ struct PadDevice
pad_config* config{ nullptr };
};
using pad_preview_values = std::array<int, 6>;
using pad_callback = std::function<void(u16 /*button_value*/, std::string /*button_name*/, std::string /*pad_name*/, u32 /*battery_level*/, pad_preview_values /*preview_values*/)>;
using pad_fail_callback = std::function<void(std::string /*pad_name*/)>;
class PadHandlerBase
{
protected:
@ -146,7 +150,6 @@ public:
// Sets window to config the controller(optional)
virtual void SetPadData(const std::string& /*padId*/, u32 /*largeMotor*/, u32 /*smallMotor*/, s32 /*r*/, s32 /*g*/, s32 /*b*/, bool /*battery_led*/, u32 /*battery_led_brightness*/) {}
virtual u32 get_battery_level(const std::string& /*padId*/) { return 0; }
virtual bool get_device_init(const std::string& /*padId*/) { return false; };
// Return list of devices for that handler
virtual std::vector<std::string> ListDevices() = 0;
// Callback called during pad_thread::ThreadFunc
@ -154,7 +157,7 @@ public:
// Binds a Pad to a device
virtual bool bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device);
virtual void init_config(pad_config* /*cfg*/, const std::string& /*name*/) = 0;
virtual void get_next_button_press(const std::string& padId, const std::function<void(u16, std::string, std::string, std::array<int, 6>)>& callback, const std::function<void(std::string)>& fail_callback, bool get_blacklist, const std::vector<std::string>& buttons = {});
virtual void get_next_button_press(const std::string& padId, const pad_callback& callback, const pad_fail_callback& fail_callback, bool get_blacklist, const std::vector<std::string>& buttons = {});
private:
virtual std::shared_ptr<PadDevice> get_device(const std::string& /*device*/) { return nullptr; };
@ -166,7 +169,7 @@ private:
virtual void get_extended_info(const std::shared_ptr<PadDevice>& /*device*/, const std::shared_ptr<Pad>& /*pad*/) {};
virtual void apply_pad_data(const std::shared_ptr<PadDevice>& /*device*/, const std::shared_ptr<Pad>& /*pad*/) {};
virtual std::unordered_map<u64, u16> get_button_values(const std::shared_ptr<PadDevice>& /*device*/) { return {}; };
virtual std::array<int, 6> get_preview_values(std::unordered_map<u64, u16> /*data*/) { return {}; };
virtual pad_preview_values get_preview_values(std::unordered_map<u64, u16> /*data*/) { return {}; };
protected:
virtual std::array<u32, PadHandlerBase::button::button_count> get_mapped_key_codes(const std::shared_ptr<PadDevice>& /*device*/, const pad_config* profile);

View File

@ -370,7 +370,7 @@ std::unordered_map<u64, u16> ds3_pad_handler::get_button_values(const std::share
return key_buf;
}
std::array<int, 6> ds3_pad_handler::get_preview_values(std::unordered_map<u64, u16> data)
pad_preview_values ds3_pad_handler::get_preview_values(std::unordered_map<u64, u16> data)
{
return { data[L2], data[R2], data[LSXPos] - data[LSXNeg], data[LSYPos] - data[LSYNeg], data[RSXPos] - data[RSXNeg], data[RSYPos] - data[RSYNeg] };
}

View File

@ -121,5 +121,5 @@ private:
void get_extended_info(const std::shared_ptr<PadDevice>& device, const std::shared_ptr<Pad>& pad) override;
void apply_pad_data(const std::shared_ptr<PadDevice>& device, const std::shared_ptr<Pad>& pad) override;
std::unordered_map<u64, u16> get_button_values(const std::shared_ptr<PadDevice>& device) override;
std::array<int, 6> get_preview_values(std::unordered_map<u64, u16> data) override;
pad_preview_values get_preview_values(std::unordered_map<u64, u16> data) override;
};

View File

@ -391,7 +391,7 @@ std::unordered_map<u64, u16> ds4_pad_handler::get_button_values(const std::share
return keyBuffer;
}
std::array<int, 6> ds4_pad_handler::get_preview_values(std::unordered_map<u64, u16> data)
pad_preview_values ds4_pad_handler::get_preview_values(std::unordered_map<u64, u16> data)
{
return { data[L2], data[R2], data[LSXPos] - data[LSXNeg], data[LSYPos] - data[LSYNeg], data[RSXPos] - data[RSXNeg], data[RSYPos] - data[RSYNeg] };
}
@ -782,16 +782,6 @@ std::shared_ptr<PadDevice> ds4_pad_handler::get_device(const std::string& device
return ds4device;
}
bool ds4_pad_handler::get_device_init(const std::string& padId)
{
std::shared_ptr<DS4Device> device = GetDS4Device(padId);
if (device == nullptr || device->hidDevice == nullptr)
{
return false;
}
return device->is_initialized;
}
bool ds4_pad_handler::get_is_left_trigger(u64 keyCode)
{
return keyCode == DS4KeyCodes::L2;

View File

@ -109,7 +109,6 @@ public:
std::vector<std::string> ListDevices() override;
void SetPadData(const std::string& padId, u32 largeMotor, u32 smallMotor, s32 r, s32 g, s32 b, bool battery_led, u32 battery_led_brightness) override;
u32 get_battery_level(const std::string& padId) override;
bool get_device_init(const std::string& padId) override;
void init_config(pad_config* cfg, const std::string& name) override;
private:
@ -149,5 +148,5 @@ private:
void get_extended_info(const std::shared_ptr<PadDevice>& device, const std::shared_ptr<Pad>& pad) override;
void apply_pad_data(const std::shared_ptr<PadDevice>& device, const std::shared_ptr<Pad>& pad) override;
std::unordered_map<u64, u16> get_button_values(const std::shared_ptr<PadDevice>& device) override;
std::array<int, 6> get_preview_values(std::unordered_map<u64, u16> data) override;
pad_preview_values get_preview_values(std::unordered_map<u64, u16> data) override;
};

View File

@ -270,7 +270,7 @@ std::shared_ptr<evdev_joystick_handler::EvdevDevice> evdev_joystick_handler::get
return std::static_pointer_cast<EvdevDevice>(dev.first);
}
void evdev_joystick_handler::get_next_button_press(const std::string& padId, const std::function<void(u16, std::string, std::string, std::array<int, 6>)>& callback, const std::function<void(std::string)>& fail_callback, bool get_blacklist, const std::vector<std::string>& buttons)
void evdev_joystick_handler::get_next_button_press(const std::string& padId, const pad_callback& callback, const pad_fail_callback& fail_callback, bool get_blacklist, const std::vector<std::string>& buttons)
{
if (get_blacklist)
blacklist.clear();
@ -303,7 +303,7 @@ void evdev_joystick_handler::get_next_button_press(const std::string& padId, con
return it != data.end() && dir == it->second.second ? it->second.first : 0;
};
std::array<int, 6> preview_values = {0, 0, 0, 0, 0, 0};
pad_preview_values preview_values = { 0, 0, 0, 0, 0, 0 };
if (buttons.size() == 10)
{
@ -317,7 +317,7 @@ void evdev_joystick_handler::get_next_button_press(const std::string& padId, con
// return if nothing new has happened. ignore this to get the current state for blacklist
if (!get_blacklist && ret < 0)
return callback(0, "", padId, preview_values);
return callback(0, "", padId, 0, preview_values);
std::pair<u16, std::string> pressed_button = { 0, "" };
@ -408,9 +408,9 @@ void evdev_joystick_handler::get_next_button_press(const std::string& padId, con
}
if (pressed_button.first > 0)
return callback(pressed_button.first, pressed_button.second, padId, preview_values);
return callback(pressed_button.first, pressed_button.second, padId, 0, preview_values);
else
return callback(0, "", padId, preview_values);
return callback(0, "", padId, 0, preview_values);
}
// https://github.com/dolphin-emu/dolphin/blob/master/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp

View File

@ -335,7 +335,7 @@ public:
std::vector<std::string> ListDevices() override;
bool bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device) override;
void Close();
void get_next_button_press(const std::string& padId, const std::function<void(u16, std::string, std::string, std::array<int, 6>)>& callback, const std::function<void(std::string)>& fail_callback, bool get_blacklist = false, const std::vector<std::string>& buttons = {}) override;
void get_next_button_press(const std::string& padId, const pad_callback& callback, const pad_fail_callback& fail_callback, bool get_blacklist = false, const std::vector<std::string>& buttons = {}) override;
void SetPadData(const std::string& padId, u32 largeMotor, u32 smallMotor, s32 r, s32 g, s32 b, bool battery_led, u32 battery_led_brightness) override;
private:

View File

@ -81,7 +81,7 @@ public:
void init_config(pad_config* cfg, const std::string& name) override;
std::vector<std::string> ListDevices() override;
void get_next_button_press(const std::string& /*padId*/, const std::function<void(u16, std::string, std::string, std::array<int, 6>)>& /*callback*/, const std::function<void(std::string)>& /*fail_callback*/, bool /*get_blacklist*/ = false, const std::vector<std::string>& /*buttons*/ = {}) override {};
void get_next_button_press(const std::string& /*padId*/, const pad_callback& /*callback*/, const pad_fail_callback& /*fail_callback*/, bool /*get_blacklist*/ = false, const std::vector<std::string>& /*buttons*/ = {}) override {};
bool bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device) override;
void ThreadProc() override;

View File

@ -177,7 +177,7 @@ std::array<u32, PadHandlerBase::button::button_count> mm_joystick_handler::get_m
return mapping;
}
void mm_joystick_handler::get_next_button_press(const std::string& padId, const std::function<void(u16, std::string, std::string, std::array<int, 6>)>& callback, const std::function<void(std::string)>& fail_callback, bool get_blacklist, const std::vector<std::string>& buttons)
void mm_joystick_handler::get_next_button_press(const std::string& padId, const pad_callback& callback, const pad_fail_callback& fail_callback, bool get_blacklist, const std::vector<std::string>& buttons)
{
if (get_blacklist)
blacklist.clear();
@ -299,7 +299,7 @@ void mm_joystick_handler::get_next_button_press(const std::string& padId, const
return static_cast<u64>(key);
};
std::array<int, 6> preview_values = { 0, 0, 0, 0, 0, 0 };
pad_preview_values preview_values = { 0, 0, 0, 0, 0, 0 };
if (buttons.size() == 10)
{
preview_values[0] = data[find_key(buttons[0])];
@ -311,9 +311,9 @@ void mm_joystick_handler::get_next_button_press(const std::string& padId, const
}
if (pressed_button.first > 0)
return callback(pressed_button.first, pressed_button.second, padId, preview_values);
return callback(pressed_button.first, pressed_button.second, padId, 0, preview_values);
else
return callback(0, "", padId, preview_values);
return callback(0, "", padId, 0, preview_values);
break;
}

View File

@ -106,7 +106,7 @@ public:
bool Init() override;
std::vector<std::string> ListDevices() override;
void get_next_button_press(const std::string& padId, const std::function<void(u16, std::string, std::string, std::array<int, 6>)>& callback, const std::function<void(std::string)>& fail_callback, bool get_blacklist = false, const std::vector<std::string>& buttons = {}) override;
void get_next_button_press(const std::string& padId, const pad_callback& callback, const pad_fail_callback& fail_callback, bool get_blacklist = false, const std::vector<std::string>& buttons = {}) override;
void init_config(pad_config* cfg, const std::string& name) override;
private:

View File

@ -287,7 +287,7 @@ xinput_pad_handler::PadButtonValues xinput_pad_handler::get_button_values_scp(co
return values;
}
std::array<int, 6> xinput_pad_handler::get_preview_values(std::unordered_map<u64, u16> data)
pad_preview_values xinput_pad_handler::get_preview_values(std::unordered_map<u64, u16> data)
{
return { data[LT], data[RT], data[LSXPos] - data[LSXNeg], data[LSYPos] - data[LSYNeg], data[RSXPos] - data[RSXNeg], data[RSYPos] - data[RSYNeg] };
}

View File

@ -128,5 +128,5 @@ private:
void get_extended_info(const std::shared_ptr<PadDevice>& device, const std::shared_ptr<Pad>& pad) override;
void apply_pad_data(const std::shared_ptr<PadDevice>& device, const std::shared_ptr<Pad>& pad) override;
std::unordered_map<u64, u16> get_button_values(const std::shared_ptr<PadDevice>& device) override;
std::array<int, 6> get_preview_values(std::unordered_map<u64, u16> data) override;
pad_preview_values get_preview_values(std::unordered_map<u64, u16> data) override;
};

View File

@ -113,11 +113,6 @@ pad_settings_dialog::pad_settings_dialog(QWidget *parent, const GameInfo *game)
cfg_log.error("Failed to convert device string: %s", m_device_name);
return;
}
// Update battery indicator (if one is present) if device selection is changed
if (m_enable_battery)
{
ui->pb_battery->setValue(m_handler->get_battery_level(m_device_name));
}
});
// Combobox: Profiles
@ -348,7 +343,7 @@ void pad_settings_dialog::InitButtons()
});
// Enable Button Remapping
const auto& callback = [this](u16 val, std::string name, std::string pad_name, std::array<int, 6> preview_values)
const auto& callback = [this](u16 val, std::string name, std::string pad_name, u32 battery_level, pad_preview_values preview_values)
{
SwitchPadInfo(pad_name, true);
@ -356,6 +351,7 @@ void pad_settings_dialog::InitButtons()
{
SwitchButtons(true);
}
if (m_handler->has_deadzones())
{
ui->preview_trigger_left->setValue(preview_values[0]);
@ -373,6 +369,11 @@ void pad_settings_dialog::InitButtons()
}
}
if (m_enable_battery)
{
ui->pb_battery->setValue(battery_level);
}
if (val <= 0)
{
return;
@ -426,19 +427,10 @@ void pad_settings_dialog::InitButtons()
}
const pad_device_info info = ui->chooseDevice->itemData(i).value<pad_device_info>();
m_handler->get_next_button_press(info.name,
[this](u16, std::string, std::string pad_name, std::array<int, 6>) { SwitchPadInfo(pad_name, true); },
[this](u16, std::string, std::string pad_name, u32, pad_preview_values) { SwitchPadInfo(pad_name, true); },
[this](std::string pad_name) { SwitchPadInfo(pad_name, false); }, false);
}
});
connect(&m_timer_ds4_battery, &QTimer::timeout, [this]()
{
if (m_handler->get_device_init(m_device_name))
{
ui->pb_battery->setValue(m_handler->get_battery_level(m_device_name));
pad_settings_dialog::m_timer_ds4_battery.stop();
}
});
}
void pad_settings_dialog::SetPadData(u32 large_motor, u32 small_motor)
@ -593,21 +585,6 @@ void pad_settings_dialog::ReloadButtons()
// Enable battery and LED group box
m_enable_battery = m_handler->has_battery();
ui->gb_battery->setVisible(m_enable_battery || m_enable_led);
// Poll the battery level if one is available and fill the progress bar
if (m_enable_battery)
{
if (m_handler->m_type == pad_handler::ds4)
{
// If DS4 is connected via BT, the battery level isn't available for a while.
// This ensures that the battery level isn't indicated prematurely.
m_timer_ds4_battery.start(4);
}
else
{
ui->pb_battery->setValue(m_handler->get_battery_level(m_device_name));
}
}
}
void pad_settings_dialog::ReactivateButtons()
@ -1119,7 +1096,7 @@ void pad_settings_dialog::ChangeInputType()
}
const pad_device_info info = ui->chooseDevice->itemData(i).value<pad_device_info>();
m_handler->get_next_button_press(info.name,
[this](u16, std::string, std::string pad_name, std::array<int, 6>) { SwitchPadInfo(pad_name, true); },
[this](u16, std::string, std::string pad_name, u32, pad_preview_values) { SwitchPadInfo(pad_name, true); },
[this](std::string pad_name) { SwitchPadInfo(pad_name, false); }, false);
if (info.name == device)
{

View File

@ -153,9 +153,6 @@ private:
// Input timer. Its Callback handles the input
QTimer m_timer_input;
// DS4 workaround timer
QTimer m_timer_ds4_battery;
// Set vibrate data while keeping the current color
void SetPadData(u32 large_motor, u32 small_motor);