Skip to content

Commit

Permalink
Support game_pad_stick_horizontal_wheel_formula in Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jan 28, 2024
1 parent 4f75ea1 commit 0e24986
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ struct DevicesGamePadSettingsView: View {
name: "vertical wheel formula",
value: binding.gamePadStickVerticalWheelFormula
)

FormulaView(
name: "horizontal wheel formula",
value: binding.gamePadStickHorizontalWheelFormula
)
}
}.padding()
}
Expand Down
35 changes: 35 additions & 0 deletions src/apps/share/swift/LibKrbn/Models/ConnectedDeviceSetting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,40 @@ extension LibKrbn {
}
)

gamePadStickHorizontalWheelFormula = OptionalSettingValue<String>(
hasFunction: {
return
libkrbn_core_configuration_has_selected_profile_device_game_pad_stick_horizontal_wheel_formula(
Settings.shared.libkrbnCoreConfiguration,
connectedDevice.libkrbnDeviceIdentifiers
)
},
getFunction: {
var buffer = [Int8](repeating: 0, count: 16384)
libkrbn_core_configuration_get_selected_profile_device_game_pad_stick_horizontal_wheel_formula(
Settings.shared.libkrbnCoreConfiguration,
connectedDevice.libkrbnDeviceIdentifiers,
&buffer,
buffer.count
)

return String(cString: buffer).trimmingCharacters(in: .whitespacesAndNewlines)
},
setFunction: { (_ newValue: String) in
libkrbn_core_configuration_set_selected_profile_device_game_pad_stick_horizontal_wheel_formula(
Settings.shared.libkrbnCoreConfiguration,
connectedDevice.libkrbnDeviceIdentifiers,
newValue.cString(using: .utf8)
)
},
unsetFunction: {
libkrbn_core_configuration_unset_selected_profile_device_game_pad_stick_horizontal_wheel_formula(
Settings.shared.libkrbnCoreConfiguration,
connectedDevice.libkrbnDeviceIdentifiers
)
}
)

simpleModifications = LibKrbn.Settings.shared.makeSimpleModifications(connectedDevice)
fnFunctionKeys = LibKrbn.Settings.shared.makeFnFunctionKeys(connectedDevice)

Expand Down Expand Up @@ -494,6 +528,7 @@ extension LibKrbn {
var gamePadStickXFormula: OptionalSettingValue<String>
var gamePadStickYFormula: OptionalSettingValue<String>
var gamePadStickVerticalWheelFormula: OptionalSettingValue<String>
var gamePadStickHorizontalWheelFormula: OptionalSettingValue<String>

@Published var simpleModifications: [SimpleModification] = []
@Published var fnFunctionKeys: [SimpleModification] = []
Expand Down
14 changes: 14 additions & 0 deletions src/lib/libkrbn/include/libkrbn/libkrbn.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,20 @@ bool libkrbn_core_configuration_set_selected_profile_device_game_pad_stick_verti
void libkrbn_core_configuration_unset_selected_profile_device_game_pad_stick_vertical_wheel_formula(libkrbn_core_configuration* p,
const libkrbn_device_identifiers* device_identifiers);

// game_pad_stick_horizontal_wheel_formula

bool libkrbn_core_configuration_has_selected_profile_device_game_pad_stick_horizontal_wheel_formula(libkrbn_core_configuration* p,
const libkrbn_device_identifiers* device_identifiers);
bool libkrbn_core_configuration_get_selected_profile_device_game_pad_stick_horizontal_wheel_formula(libkrbn_core_configuration* p,
const libkrbn_device_identifiers* device_identifiers,
char* buffer,
size_t length);
bool libkrbn_core_configuration_set_selected_profile_device_game_pad_stick_horizontal_wheel_formula(libkrbn_core_configuration* p,
const libkrbn_device_identifiers* device_identifiers,
const char* value);
void libkrbn_core_configuration_unset_selected_profile_device_game_pad_stick_horizontal_wheel_formula(libkrbn_core_configuration* p,
const libkrbn_device_identifiers* device_identifiers);

// game_pad_*

bool libkrbn_core_configuration_game_pad_validate_stick_formula(const char* formula);
Expand Down
60 changes: 60 additions & 0 deletions src/lib/libkrbn/src/libkrbn_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,66 @@ void libkrbn_core_configuration_unset_selected_profile_device_game_pad_stick_ver
}
}

// game_pad_stick_horizontal_wheel_formula

bool libkrbn_core_configuration_has_selected_profile_device_game_pad_stick_horizontal_wheel_formula(libkrbn_core_configuration* p,
const libkrbn_device_identifiers* device_identifiers) {
if (auto c = reinterpret_cast<libkrbn_core_configuration_class*>(p)) {
if (device_identifiers) {
auto identifiers = libkrbn_cpp::make_device_identifiers(*device_identifiers);
return c->get_core_configuration().get_selected_profile().has_device_game_pad_stick_horizontal_wheel_formula(identifiers);
}
}
return false;
}

bool libkrbn_core_configuration_get_selected_profile_device_game_pad_stick_horizontal_wheel_formula(libkrbn_core_configuration* p,
const libkrbn_device_identifiers* device_identifiers,
char* buffer,
size_t length) {
if (auto c = reinterpret_cast<libkrbn_core_configuration_class*>(p)) {
if (device_identifiers) {
auto identifiers = libkrbn_cpp::make_device_identifiers(*device_identifiers);
auto formula = c->get_core_configuration().get_selected_profile().get_device_game_pad_stick_horizontal_wheel_formula(identifiers);
// Return false if no enough space.
if (formula.length() < length) {
strlcpy(buffer, formula.c_str(), length);
return true;
}
}
}
return false;
}

bool libkrbn_core_configuration_set_selected_profile_device_game_pad_stick_horizontal_wheel_formula(libkrbn_core_configuration* p,
const libkrbn_device_identifiers* device_identifiers,
const char* value) {
if (!krbn::core_configuration::details::device::validate_stick_formula(value)) {
return false;
}

if (auto c = reinterpret_cast<libkrbn_core_configuration_class*>(p)) {
if (device_identifiers) {
auto identifiers = libkrbn_cpp::make_device_identifiers(*device_identifiers);
c->get_core_configuration().get_selected_profile().set_device_game_pad_stick_horizontal_wheel_formula(identifiers, value);

return true;
}
}

return false;
}

void libkrbn_core_configuration_unset_selected_profile_device_game_pad_stick_horizontal_wheel_formula(libkrbn_core_configuration* p,
const libkrbn_device_identifiers* device_identifiers) {
if (auto c = reinterpret_cast<libkrbn_core_configuration_class*>(p)) {
if (device_identifiers) {
auto identifiers = libkrbn_cpp::make_device_identifiers(*device_identifiers);
c->get_core_configuration().get_selected_profile().set_device_game_pad_stick_horizontal_wheel_formula(identifiers, std::nullopt);
}
}
}

// game_pad_*

bool libkrbn_core_configuration_game_pad_validate_stick_formula(const char* formula) {
Expand Down
11 changes: 11 additions & 0 deletions src/share/core_configuration/details/profile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,17 @@ class profile final {
// game_pad_stick_horizontal_wheel_formula
//

bool has_device_game_pad_stick_horizontal_wheel_formula(const device_identifiers& identifiers) const {
for (const auto& d : devices_) {
if (d.get_identifiers() == identifiers) {
if (auto value = d.get_game_pad_stick_horizontal_wheel_formula()) {
return true;
}
}
}
return false;
}

std::string get_device_game_pad_stick_horizontal_wheel_formula(const device_identifiers& identifiers) const {
for (const auto& d : devices_) {
if (d.get_identifiers() == identifiers) {
Expand Down

0 comments on commit 0e24986

Please sign in to comment.