mirror of
				https://git.suyu.dev/suyu/suyu
				synced 2025-11-04 00:49:02 -06:00 
			
		
		
		
	core: hid: Enable pulling color data from controllers
This commit is contained in:
		@@ -93,6 +93,7 @@ void EmulatedController::ReloadFromSettings() {
 | 
			
		||||
        motion_params[index] = Common::ParamPackage(player.motions[index]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    controller.color_values = {};
 | 
			
		||||
    controller.colors_state.fullkey = {
 | 
			
		||||
        .body = GetNpadColor(player.body_color_left),
 | 
			
		||||
        .button = GetNpadColor(player.button_color_left),
 | 
			
		||||
@@ -132,6 +133,11 @@ void EmulatedController::LoadDevices() {
 | 
			
		||||
    trigger_params[LeftIndex] = button_params[Settings::NativeButton::ZL];
 | 
			
		||||
    trigger_params[RightIndex] = button_params[Settings::NativeButton::ZR];
 | 
			
		||||
 | 
			
		||||
    color_params[LeftIndex] = left_joycon;
 | 
			
		||||
    color_params[RightIndex] = right_joycon;
 | 
			
		||||
    color_params[LeftIndex].Set("color", true);
 | 
			
		||||
    color_params[RightIndex].Set("color", true);
 | 
			
		||||
 | 
			
		||||
    battery_params[LeftIndex] = left_joycon;
 | 
			
		||||
    battery_params[RightIndex] = right_joycon;
 | 
			
		||||
    battery_params[LeftIndex].Set("battery", true);
 | 
			
		||||
@@ -160,6 +166,7 @@ void EmulatedController::LoadDevices() {
 | 
			
		||||
                           Common::Input::CreateInputDevice);
 | 
			
		||||
    std::ranges::transform(battery_params, battery_devices.begin(),
 | 
			
		||||
                           Common::Input::CreateInputDevice);
 | 
			
		||||
    std::ranges::transform(color_params, color_devices.begin(), Common::Input::CreateInputDevice);
 | 
			
		||||
    camera_devices = Common::Input::CreateInputDevice(camera_params);
 | 
			
		||||
    ring_analog_device = Common::Input::CreateInputDevice(ring_params);
 | 
			
		||||
    nfc_devices = Common::Input::CreateInputDevice(nfc_params);
 | 
			
		||||
@@ -324,6 +331,19 @@ void EmulatedController::ReloadInput() {
 | 
			
		||||
        battery_devices[index]->ForceUpdate();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (std::size_t index = 0; index < color_devices.size(); ++index) {
 | 
			
		||||
        if (!color_devices[index]) {
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        color_devices[index]->SetCallback({
 | 
			
		||||
            .on_change =
 | 
			
		||||
                [this, index](const Common::Input::CallbackStatus& callback) {
 | 
			
		||||
                    SetColors(callback, index);
 | 
			
		||||
                },
 | 
			
		||||
        });
 | 
			
		||||
        color_devices[index]->ForceUpdate();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (std::size_t index = 0; index < motion_devices.size(); ++index) {
 | 
			
		||||
        if (!motion_devices[index]) {
 | 
			
		||||
            continue;
 | 
			
		||||
@@ -429,6 +449,9 @@ void EmulatedController::UnloadInput() {
 | 
			
		||||
    for (auto& battery : battery_devices) {
 | 
			
		||||
        battery.reset();
 | 
			
		||||
    }
 | 
			
		||||
    for (auto& color : color_devices) {
 | 
			
		||||
        color.reset();
 | 
			
		||||
    }
 | 
			
		||||
    for (auto& output : output_devices) {
 | 
			
		||||
        output.reset();
 | 
			
		||||
    }
 | 
			
		||||
@@ -458,6 +481,11 @@ void EmulatedController::EnableConfiguration() {
 | 
			
		||||
void EmulatedController::DisableConfiguration() {
 | 
			
		||||
    is_configuring = false;
 | 
			
		||||
 | 
			
		||||
    // Get Joycon colors before turning on the controller
 | 
			
		||||
    for (const auto& color_device : color_devices) {
 | 
			
		||||
        color_device->ForceUpdate();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Apply temporary npad type to the real controller
 | 
			
		||||
    if (tmp_npad_type != npad_type) {
 | 
			
		||||
        if (is_connected) {
 | 
			
		||||
@@ -926,6 +954,58 @@ void EmulatedController::SetMotion(const Common::Input::CallbackStatus& callback
 | 
			
		||||
    TriggerOnChange(ControllerTriggerType::Motion, true);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void EmulatedController::SetColors(const Common::Input::CallbackStatus& callback,
 | 
			
		||||
                                   std::size_t index) {
 | 
			
		||||
    if (index >= controller.color_values.size()) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    std::unique_lock lock{mutex};
 | 
			
		||||
    controller.color_values[index] = TransformToColor(callback);
 | 
			
		||||
 | 
			
		||||
    if (is_configuring) {
 | 
			
		||||
        lock.unlock();
 | 
			
		||||
        TriggerOnChange(ControllerTriggerType::Color, false);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (controller.color_values[index].body == 0) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    controller.colors_state.fullkey = {
 | 
			
		||||
        .body = GetNpadColor(controller.color_values[index].body),
 | 
			
		||||
        .button = GetNpadColor(controller.color_values[index].buttons),
 | 
			
		||||
    };
 | 
			
		||||
    if (npad_type == NpadStyleIndex::ProController) {
 | 
			
		||||
        controller.colors_state.left = {
 | 
			
		||||
            .body = GetNpadColor(controller.color_values[index].left_grip),
 | 
			
		||||
            .button = GetNpadColor(controller.color_values[index].buttons),
 | 
			
		||||
        };
 | 
			
		||||
        controller.colors_state.right = {
 | 
			
		||||
            .body = GetNpadColor(controller.color_values[index].right_grip),
 | 
			
		||||
            .button = GetNpadColor(controller.color_values[index].buttons),
 | 
			
		||||
        };
 | 
			
		||||
    } else {
 | 
			
		||||
        switch (index) {
 | 
			
		||||
        case LeftIndex:
 | 
			
		||||
            controller.colors_state.left = {
 | 
			
		||||
                .body = GetNpadColor(controller.color_values[index].body),
 | 
			
		||||
                .button = GetNpadColor(controller.color_values[index].buttons),
 | 
			
		||||
            };
 | 
			
		||||
            break;
 | 
			
		||||
        case RightIndex:
 | 
			
		||||
            controller.colors_state.right = {
 | 
			
		||||
                .body = GetNpadColor(controller.color_values[index].body),
 | 
			
		||||
                .button = GetNpadColor(controller.color_values[index].buttons),
 | 
			
		||||
            };
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    lock.unlock();
 | 
			
		||||
    TriggerOnChange(ControllerTriggerType::Color, true);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void EmulatedController::SetBattery(const Common::Input::CallbackStatus& callback,
 | 
			
		||||
                                    std::size_t index) {
 | 
			
		||||
    if (index >= controller.battery_values.size()) {
 | 
			
		||||
 
 | 
			
		||||
@@ -35,6 +35,8 @@ using ControllerMotionDevices =
 | 
			
		||||
    std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeMotion::NumMotions>;
 | 
			
		||||
using TriggerDevices =
 | 
			
		||||
    std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeTrigger::NumTriggers>;
 | 
			
		||||
using ColorDevices =
 | 
			
		||||
    std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
 | 
			
		||||
using BatteryDevices =
 | 
			
		||||
    std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
 | 
			
		||||
using CameraDevices = std::unique_ptr<Common::Input::InputDevice>;
 | 
			
		||||
@@ -46,6 +48,7 @@ using ButtonParams = std::array<Common::ParamPackage, Settings::NativeButton::Nu
 | 
			
		||||
using StickParams = std::array<Common::ParamPackage, Settings::NativeAnalog::NumAnalogs>;
 | 
			
		||||
using ControllerMotionParams = std::array<Common::ParamPackage, Settings::NativeMotion::NumMotions>;
 | 
			
		||||
using TriggerParams = std::array<Common::ParamPackage, Settings::NativeTrigger::NumTriggers>;
 | 
			
		||||
using ColorParams = std::array<Common::ParamPackage, max_emulated_controllers>;
 | 
			
		||||
using BatteryParams = std::array<Common::ParamPackage, max_emulated_controllers>;
 | 
			
		||||
using CameraParams = Common::ParamPackage;
 | 
			
		||||
using RingAnalogParams = Common::ParamPackage;
 | 
			
		||||
@@ -457,6 +460,13 @@ private:
 | 
			
		||||
     */
 | 
			
		||||
    void SetMotion(const Common::Input::CallbackStatus& callback, std::size_t index);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Updates the color status of the controller
 | 
			
		||||
     * @param callback A CallbackStatus containing the color status
 | 
			
		||||
     * @param index color ID of the to be updated
 | 
			
		||||
     */
 | 
			
		||||
    void SetColors(const Common::Input::CallbackStatus& callback, std::size_t index);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Updates the battery status of the controller
 | 
			
		||||
     * @param callback A CallbackStatus containing the battery status
 | 
			
		||||
@@ -515,6 +525,7 @@ private:
 | 
			
		||||
    ControllerMotionParams motion_params;
 | 
			
		||||
    TriggerParams trigger_params;
 | 
			
		||||
    BatteryParams battery_params;
 | 
			
		||||
    ColorParams color_params;
 | 
			
		||||
    CameraParams camera_params;
 | 
			
		||||
    RingAnalogParams ring_params;
 | 
			
		||||
    NfcParams nfc_params;
 | 
			
		||||
@@ -525,6 +536,7 @@ private:
 | 
			
		||||
    ControllerMotionDevices motion_devices;
 | 
			
		||||
    TriggerDevices trigger_devices;
 | 
			
		||||
    BatteryDevices battery_devices;
 | 
			
		||||
    ColorDevices color_devices;
 | 
			
		||||
    CameraDevices camera_devices;
 | 
			
		||||
    RingAnalogDevice ring_analog_device;
 | 
			
		||||
    NfcDevices nfc_devices;
 | 
			
		||||
 
 | 
			
		||||
@@ -304,6 +304,20 @@ Common::Input::NfcStatus TransformToNfc(const Common::Input::CallbackStatus& cal
 | 
			
		||||
    return nfc;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Common::Input::BodyColorStatus TransformToColor(const Common::Input::CallbackStatus& callback) {
 | 
			
		||||
    Common::Input::BodyColorStatus color{};
 | 
			
		||||
    switch (callback.type) {
 | 
			
		||||
    case Common::Input::InputType::Color:
 | 
			
		||||
        color = callback.color_status;
 | 
			
		||||
        break;
 | 
			
		||||
    default:
 | 
			
		||||
        LOG_ERROR(Input, "Conversion from type {} to color not implemented", callback.type);
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return color;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SanitizeAnalog(Common::Input::AnalogStatus& analog, bool clamp_value) {
 | 
			
		||||
    const auto& properties = analog.properties;
 | 
			
		||||
    float& raw_value = analog.raw_value;
 | 
			
		||||
 
 | 
			
		||||
@@ -88,10 +88,18 @@ Common::Input::CameraStatus TransformToCamera(const Common::Input::CallbackStatu
 | 
			
		||||
 * Converts raw input data into a valid nfc status.
 | 
			
		||||
 *
 | 
			
		||||
 * @param callback Supported callbacks: Nfc.
 | 
			
		||||
 * @return A valid CameraObject object.
 | 
			
		||||
 * @return A valid data tag vector.
 | 
			
		||||
 */
 | 
			
		||||
Common::Input::NfcStatus TransformToNfc(const Common::Input::CallbackStatus& callback);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Converts raw input data into a valid color status.
 | 
			
		||||
 *
 | 
			
		||||
 * @param callback Supported callbacks: Color.
 | 
			
		||||
 * @return A valid Color object.
 | 
			
		||||
 */
 | 
			
		||||
Common::Input::BodyColorStatus TransformToColor(const Common::Input::CallbackStatus& callback);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Converts raw analog data into a valid analog value
 | 
			
		||||
 * @param analog An analog object containing raw data and properties
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user