From 985e31b48b8022c77fa22c92c1c3acfc0fc2f1ac Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Wed, 10 Oct 2018 00:36:55 +0200 Subject: [PATCH] Greatly simplify the plugin by removing auto-detection The `FingerprintUSBHost` library isn't the most reliable thing, so instead of setting false expectations, remove auto-detection instead. This makes the plugin a whole lost simpler, and depend on one less library. Fixes #9. Signed-off-by: Gergely Nagy --- README.md | 26 +++------- examples/HostOS/HostOS.ino | 1 - src/Kaleidoscope-HostOS.h | 3 +- src/Kaleidoscope/HostOS-Focus.cpp | 6 +-- src/Kaleidoscope/HostOS-Focus.h | 4 +- src/Kaleidoscope/HostOS-Guesser.cpp | 47 ------------------- src/Kaleidoscope/HostOS-Guesser.h | 34 -------------- src/Kaleidoscope/HostOS-select.h | 9 +--- .../{HostOS-Base.cpp => HostOS.cpp} | 24 +++------- src/Kaleidoscope/{HostOS-Base.h => HostOS.h} | 28 +++++++---- 10 files changed, 37 insertions(+), 145 deletions(-) delete mode 100644 src/Kaleidoscope/HostOS-Guesser.cpp delete mode 100644 src/Kaleidoscope/HostOS-Guesser.h rename src/Kaleidoscope/{HostOS-Base.cpp => HostOS.cpp} (76%) rename src/Kaleidoscope/{HostOS-Base.h => HostOS.h} (62%) diff --git a/README.md b/README.md index bbaed972..7e26288f 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,11 @@ The `HostOS` extension is not all that useful in itself, rather, it is a building block other plugins and extensions can use to not repeat the same -guesswork and logic. Its primary purpose is to help either detect, or keep track -of the host operating system. The detection part is not the most reliable thing, -mind you. +guesswork and logic. -The goal is to have a single place that remembers the host OS, either detected, -or set by the end-user, in a Sketch, or via a macro, or some other way. This -information can then be reused by other plugins. +The goal is to have a single place that remembers the host OS, whether set by +the end-user in a Sketch, or via a macro, or some other way. This information +can then be reused by other plugins. See the [Unicode][plugin:unicode] extension for an example about how to use `HostOS` in practice. @@ -22,20 +20,11 @@ See the [Unicode][plugin:unicode] extension for an example about how to use ## Using the extension -The extension provides a `HostOS` singleton object. It can either be a simple -one without auto-detection (the default), or one that will try to detect the -Host OS, using the [FingerprintUSBHost][fprdetect] library. To enable -auto-detection, `KALEIDOSCOPE_HOSTOS_GUESSER` must be defined before including -the `HostOS` library header. - - [fprdetect]: https://github.com/keyboardio/FingerprintUSBHost +The extension provides a `HostOS` singleton object. ```c++ -#define KALEIDOSCOPE_HOSTOS_GUESSER 1 - #include #include -#include void someFunction(void) { if (HostOS.os() == kaleidoscope::hostos::LINUX) { @@ -46,16 +35,13 @@ void someFunction(void) { } } -KALEIDOSCOPE_INIT_PLUGINS(HostOS); +KALEIDOSCOPE_INIT_PLUGINS(HostOS) void setup(void) { Kaleidoscope.setup (); } ``` -To be able to choose between the two variants, one must also include the -`Kaleidoscope/HostOS-select.h` header. - ## Extension methods The extension provides the following methods on the `HostOS` singleton: diff --git a/examples/HostOS/HostOS.ino b/examples/HostOS/HostOS.ino index cc1834d9..9fb1599e 100644 --- a/examples/HostOS/HostOS.ino +++ b/examples/HostOS/HostOS.ino @@ -16,7 +16,6 @@ */ #include -#include // *INDENT-OFF* const Key keymaps[][ROWS][COLS] PROGMEM = { diff --git a/src/Kaleidoscope-HostOS.h b/src/Kaleidoscope-HostOS.h index 3a137545..2db78a77 100644 --- a/src/Kaleidoscope-HostOS.h +++ b/src/Kaleidoscope-HostOS.h @@ -17,6 +17,5 @@ #pragma once -#include +#include #include -#include diff --git a/src/Kaleidoscope/HostOS-Focus.cpp b/src/Kaleidoscope/HostOS-Focus.cpp index 930d6d6e..3b967499 100644 --- a/src/Kaleidoscope/HostOS-Focus.cpp +++ b/src/Kaleidoscope/HostOS-Focus.cpp @@ -19,7 +19,6 @@ #include namespace kaleidoscope { -namespace hostos { EventHandlerResult FocusHostOSCommand::onFocusEvent(const char *command) { const char *cmd = PSTR("hostos.type"); @@ -33,14 +32,13 @@ EventHandlerResult FocusHostOSCommand::onFocusEvent(const char *command) { Serial.println(::HostOS.os()); } else { uint8_t new_os = Serial.parseInt(); - ::HostOS.os((Type) new_os); + ::HostOS.os((hostos::Type) new_os); } Serial.read(); return EventHandlerResult::EVENT_CONSUMED; } -} } -kaleidoscope::hostos::FocusHostOSCommand FocusHostOSCommand; +kaleidoscope::FocusHostOSCommand FocusHostOSCommand; diff --git a/src/Kaleidoscope/HostOS-Focus.h b/src/Kaleidoscope/HostOS-Focus.h index e40a47df..1d93752e 100644 --- a/src/Kaleidoscope/HostOS-Focus.h +++ b/src/Kaleidoscope/HostOS-Focus.h @@ -20,7 +20,6 @@ #include namespace kaleidoscope { -namespace hostos { class FocusHostOSCommand : public kaleidoscope::Plugin { public: @@ -28,7 +27,6 @@ class FocusHostOSCommand : public kaleidoscope::Plugin { EventHandlerResult onFocusEvent(const char *command); }; -} } -extern kaleidoscope::hostos::FocusHostOSCommand FocusHostOSCommand; +extern kaleidoscope::FocusHostOSCommand FocusHostOSCommand; diff --git a/src/Kaleidoscope/HostOS-Guesser.cpp b/src/Kaleidoscope/HostOS-Guesser.cpp deleted file mode 100644 index 355fa7f7..00000000 --- a/src/Kaleidoscope/HostOS-Guesser.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* -*- mode: c++ -*- - * Kaleidoscope-HostOS -- Host OS detection and tracking for Kaleidoscope - * Copyright (C) 2016, 2017, 2018 Keyboard.io, Inc - * - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ - -#include - -#include - -namespace kaleidoscope { -namespace hostos { - -void Guesser::autoDetect(void) { - Serial.begin(9600); - - delay(15000); - - switch (FingerprintUSBHost.guessHostOS()) { - case GuessedHost::WINDOWS: - os_ = WINDOWS; - break; - case GuessedHost::LINUX: - os_ = LINUX; - break; - case GuessedHost::MACOS: - os_ = OSX; - break; - default: - os_ = OTHER; - break; - } -} - -} -} diff --git a/src/Kaleidoscope/HostOS-Guesser.h b/src/Kaleidoscope/HostOS-Guesser.h deleted file mode 100644 index 1dc81f68..00000000 --- a/src/Kaleidoscope/HostOS-Guesser.h +++ /dev/null @@ -1,34 +0,0 @@ -/* -*- mode: c++ -*- - * Kaleidoscope-HostOS -- Host OS detection and tracking for Kaleidoscope - * Copyright (C) 2016, 2017, 2018 Keyboard.io, Inc - * - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ - -#pragma once - -#include - -namespace kaleidoscope { -namespace hostos { - -class Guesser : public Base { - public: - Guesser(void) {} - - protected: - void autoDetect(void) final; -}; - -} -} diff --git a/src/Kaleidoscope/HostOS-select.h b/src/Kaleidoscope/HostOS-select.h index ac2d5e22..7a080cd7 100644 --- a/src/Kaleidoscope/HostOS-select.h +++ b/src/Kaleidoscope/HostOS-select.h @@ -17,11 +17,4 @@ #pragma once -#include -#include - -#if KALEIDOSCOPE_HOSTOS_GUESSER -kaleidoscope::hostos::Base HostOS = kaleidoscope::hostos::Guesser(); -#else -kaleidoscope::hostos::Base HostOS; -#endif +#warning is an obsolete header, and can be safely removed. diff --git a/src/Kaleidoscope/HostOS-Base.cpp b/src/Kaleidoscope/HostOS.cpp similarity index 76% rename from src/Kaleidoscope/HostOS-Base.cpp rename to src/Kaleidoscope/HostOS.cpp index d3207bcf..ac85faf1 100644 --- a/src/Kaleidoscope/HostOS-Base.cpp +++ b/src/Kaleidoscope/HostOS.cpp @@ -15,15 +15,14 @@ * this program. If not, see . */ -#include +#include #include #include namespace kaleidoscope { -namespace hostos { -EventHandlerResult Base::onSetup(void) { +EventHandlerResult HostOS::onSetup(void) { if (is_configured_) return EventHandlerResult::OK; @@ -31,29 +30,20 @@ EventHandlerResult Base::onSetup(void) { is_configured_ = true; - if (os_ != AUTO) { + if (os_ != hostos::UNKNOWN) { return EventHandlerResult::OK; } - if ((os_ = (Type)EEPROM.read(eeprom_slice_)) != AUTO) - return EventHandlerResult::OK; - - autoDetect(); + os_ = (hostos::Type)EEPROM.read(eeprom_slice_); return EventHandlerResult::OK; } -Type Base::os(void) { - if (os_ == AUTO) - autoDetect(); - - return os_; -} - -void Base::os(Type new_os) { +void HostOS::os(hostos::Type new_os) { os_ = new_os; EEPROM.update(eeprom_slice_, os_); } } -} + +kaleidoscope::HostOS HostOS; diff --git a/src/Kaleidoscope/HostOS-Base.h b/src/Kaleidoscope/HostOS.h similarity index 62% rename from src/Kaleidoscope/HostOS-Base.h rename to src/Kaleidoscope/HostOS.h index 84d546e1..af592e95 100644 --- a/src/Kaleidoscope/HostOS-Base.h +++ b/src/Kaleidoscope/HostOS.h @@ -28,26 +28,36 @@ typedef enum { WINDOWS, OTHER, - AUTO = 0xff, + UNKNOWN = 0xff, + AUTO = UNKNOWN } Type; -class Base : public kaleidoscope::Plugin { +} + +#define _DEPRECATED_MESSAGE_AUTODETECT \ + "The auto-detect mechanism of HostOS was removed, because it was\n" \ + "too unreliable. As such, the HostOS.autoDetect() method does\n" \ + "nothing anymore. Please consider other ways of changing the\n" \ + "HostOS type." + +class HostOS : public kaleidoscope::Plugin { public: + HostOS() {} EventHandlerResult onSetup(); - Type os(void); - void os(Type new_os); + hostos::Type os(void) { + return os_; + } + void os(hostos::Type new_os); - protected: - virtual void autoDetect(void) {} - Type os_; + void autoDetect() DEPRECATED(AUTODETECT) {} private: + hostos::Type os_; uint16_t eeprom_slice_; bool is_configured_ = false; }; -} } -extern kaleidoscope::hostos::Base HostOS; +extern kaleidoscope::HostOS HostOS;