Skip to content

Commit

Permalink
Allow running without a config file
Browse files Browse the repository at this point in the history
Introduce AllowlistMode option to accomodate for this.
It can be disabled when using a config file too if you prefer so.
  • Loading branch information
v1993 committed Oct 18, 2023
1 parent 1c97dc1 commit ce9283b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
1 change: 1 addition & 0 deletions ExampleConfig.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# GyroNormalizationFactor controls how quickly gyro drift is accounted for
# Default value of 50 should be good in 99%+ of cases, but rarely one may want to tweak it
[Linuxmotehook]
AllowlistMode=true
Port=26760
Orientation=sideways-left
SendButtons=false
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
* Support for reporting IR as touch (potentially useful with dolphin)
* Support for more Wiimote extensions - if requested

## Configuration

No configuration file is required to get started - just run without any arguments to expose all Wiimotes with default settings.
However, you may want one for changing port, controller orientation, or something else.
Check out [wiki](https://github.com/v1993/linuxmotehook2/wiki) for information on how to write a configuration file.

## Quick build guide

```bash
Expand Down Expand Up @@ -56,6 +62,3 @@ sudo apt-get install build-essential \
meson
```

## Configuration

Check out [wiki](https://github.com/v1993/linuxmotehook2/wiki) for information on how to write configuration file.
25 changes: 20 additions & 5 deletions src/Config.vala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Linuxmotehook {
public int pro_controller_stick_calibration[8];

// TODO: initialize fields directly once initializing from arrays is fixed
// Also don't incherit from Object once that's done
// Also don't inherit from Object once that's done
construct {
gyro_calibration = {0, 0, 0};
orientation = NORMAL;
Expand All @@ -37,6 +37,11 @@ namespace Linuxmotehook {
classic_controller_stick_calibration = {0, 0, 25, 25, 0, 0, 25, 25};
pro_controller_stick_calibration = {0, 0, 1000, 1000, 0, 0, 1000, 1000};
}

public WiimoteConfig(Config global_conf) {
send_buttons = global_conf.send_buttons;
orientation = global_conf.orientation;
}
}

private int[] kfile_get_integer_list_checked(KeyFile kfile, string group, string key, size_t length) throws KeyFileError {
Expand All @@ -55,6 +60,15 @@ namespace Linuxmotehook {

private Gee.HashMap<uint64?, WiimoteConfig> wiimote_configs;

private bool allowlist_mode_ = false;
public bool allowlist_mode {
get { return allowlist_mode_; }
set {
allowlist_mode_ = value;
kfile.set_boolean(MAIN_GROUP, "AllowlistMode", value);
}
}

private uint16 port_ = 26760;
public uint16 port {
get { return port_; }
Expand Down Expand Up @@ -109,6 +123,9 @@ namespace Linuxmotehook {
case "Port":
port_ = (uint16)kfile.get_uint64(MAIN_GROUP, key);
break;
case "AllowlistMode":
allowlist_mode_ = kfile.get_boolean(MAIN_GROUP, key);
break;
case "Orientation":
var orient = kfile.get_string(MAIN_GROUP, key);
if (!Cemuhook.DeviceOrientation.try_parse(orient, out orientation_)) {
Expand Down Expand Up @@ -146,9 +163,7 @@ namespace Linuxmotehook {
assert(mac != Cemuhook.MAC_UNAVAILABLE);
assert((mac >> 48) == 0);

var conf = new WiimoteConfig();
conf.orientation = orientation;
conf.send_buttons = send_buttons;
var conf = new WiimoteConfig(this);

foreach (unowned string key in kfile.get_keys(group)) {
switch(key) {
Expand Down Expand Up @@ -189,7 +204,7 @@ namespace Linuxmotehook {
return wiimote_configs[mac];
}

return null;
return allowlist_mode ? null : new WiimoteConfig(this);
}

public void set_device_config(uint64 mac, WiimoteConfig conf)
Expand Down
26 changes: 15 additions & 11 deletions src/main.vala
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ class LMApplication : Application {
};

add_main_option_entries(options);
set_option_context_parameter_string("config-file.ini");
set_option_context_parameter_string("[config-file.ini]");
set_option_context_summary("""Summary:
Cemuhook UDP motion server for WiiMotes on Linux.""");
set_option_context_description("""I plan to soon make a handy GUI configuration tool for this program. Once done, I'll mention it here.
set_option_context_description("""I plan to eventually make a handy GUI configuration tool for this program. Once done, I'll mention it here.
Copyright 2022 v1993 <[email protected]>
This program is free software: you can redistribute it and/or modify
Expand All @@ -54,31 +54,35 @@ the Free Software Foundation, either version 3 of the License, or
}

public override void activate() {
print("No config file specified - call with `--help' for usage information.\n");
Process.exit(1);
try {
hold();
server = new Server(config.port);
} catch(Error e) {
print("Failed to start server: %s\n", e.message);
return;
}
}

public override void open(File[] files, string hint) {
if (files.length != 1) {
print("Exactly single config file must be provided.");
print("Zero or one config files must be provided.\n");
Process.exit(1);
}

try {
config.allowlist_mode = true; // For compatibility with older versions
config.kfile.load_from_file(files[0].get_path(), NONE);
config.init_from_keyfile();
} catch (Error e) {
print("Error reading config file: %s\n", e.message);
return;
}

try {
hold();
server = new Server(config.port);
} catch(Error e) {
print("Failed to start server: %s\n", e.message);
return;
if (config.allowlist_mode) {
print("Allowlist mode enabled - only devices with a section in config will be served.\n");
}

activate();
}

public override int handle_local_options(VariantDict opt) {
Expand Down

0 comments on commit ce9283b

Please sign in to comment.