From ce9283b5dde7bee0dc45634314dec655a0890e9f Mon Sep 17 00:00:00 2001 From: Valeri Ochinski Date: Wed, 18 Oct 2023 23:02:49 +0300 Subject: [PATCH] Allow running without a config file Introduce AllowlistMode option to accomodate for this. It can be disabled when using a config file too if you prefer so. --- ExampleConfig.ini | 1 + README.md | 9 ++++++--- src/Config.vala | 25 ++++++++++++++++++++----- src/main.vala | 26 +++++++++++++++----------- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/ExampleConfig.ini b/ExampleConfig.ini index d00f79e..cc4af39 100644 --- a/ExampleConfig.ini +++ b/ExampleConfig.ini @@ -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 diff --git a/README.md b/README.md index 1a36823..61d7a22 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/src/Config.vala b/src/Config.vala index 429d2a1..f7464ef 100644 --- a/src/Config.vala +++ b/src/Config.vala @@ -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; @@ -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 { @@ -55,6 +60,15 @@ namespace Linuxmotehook { private Gee.HashMap 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_; } @@ -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_)) { @@ -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) { @@ -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) diff --git a/src/main.vala b/src/main.vala index b60d730..788086a 100644 --- a/src/main.vala +++ b/src/main.vala @@ -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 This program is free software: you can redistribute it and/or modify @@ -54,17 +54,23 @@ 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) { @@ -72,13 +78,11 @@ the Free Software Foundation, either version 3 of the License, or 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) {