summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2021-04-22 03:21:06 +0300
committerDevaev Maxim <[email protected]>2021-04-22 03:21:06 +0300
commit4279ae5bc3ffbd4f645195c38fe5a60a1d28ec01 (patch)
tree4b690b657adc3d942c86e051fceafa237ef618d5
parente6ecbb2a9ca6de372ba8cb627c79636832595805 (diff)
improved mac uefi keys handling
-rw-r--r--hid/src/main.cpp12
-rw-r--r--hid/src/tools.h32
-rw-r--r--hid/src/usb/hid.h32
3 files changed, 68 insertions, 8 deletions
diff --git a/hid/src/main.cpp b/hid/src/main.cpp
index 72ad60b8..118e0d4e 100644
--- a/hid/src/main.cpp
+++ b/hid/src/main.cpp
@@ -36,6 +36,7 @@
# include <avr/eeprom.h>
#endif
+#include "tools.h"
#include "proto.h"
#ifdef CMD_SPI
# include "spi.h"
@@ -337,6 +338,11 @@ int main() {
aumProxyUsbVbus();
# endif
+# ifdef HID_WITH_USB
+ if (_usb_kbd) {
+ _usb_kbd->periodic();
+ }
+# endif
# ifdef HID_WITH_PS2
if (_ps2_kbd) {
_ps2_kbd->periodic();
@@ -354,11 +360,7 @@ int main() {
++index;
}
} else if (index > 0) {
- unsigned long now = micros();
- if (
- (now >= last && now - last > CMD_SERIAL_TIMEOUT)
- || (now < last && ((unsigned long)-1) - last + now > CMD_SERIAL_TIMEOUT)
- ) {
+ if (is_micros_timed_out(last, CMD_SERIAL_TIMEOUT)) {
_sendResponse(PROTO::RESP::TIMEOUT_ERROR);
index = 0;
}
diff --git a/hid/src/tools.h b/hid/src/tools.h
new file mode 100644
index 00000000..6dc7d510
--- /dev/null
+++ b/hid/src/tools.h
@@ -0,0 +1,32 @@
+/*****************************************************************************
+# #
+# KVMD - The main Pi-KVM daemon. #
+# #
+# Copyright (C) 2018-2021 Maxim Devaev <[email protected]> #
+# #
+# 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, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# 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 <https://www.gnu.org/licenses/>. #
+# #
+*****************************************************************************/
+
+
+#pragma once
+
+
+bool is_micros_timed_out(unsigned long start_ts, unsigned long timeout) {
+ unsigned long now = micros();
+ return (
+ (now >= start_ts && now - start_ts > timeout)
+ || (now < start_ts && ((unsigned long)-1) - start_ts + now > timeout)
+ );
+}
diff --git a/hid/src/usb/hid.h b/hid/src/usb/hid.h
index c23f8715..92519329 100644
--- a/hid/src/usb/hid.h
+++ b/hid/src/usb/hid.h
@@ -25,6 +25,7 @@
#include <Arduino.h>
#include <HID-Project.h>
+#include "../tools.h"
#ifdef AUM
# include "../aum.h"
#endif
@@ -73,16 +74,27 @@ class UsbKeyboard {
_kbd.begin();
}
+ void periodic() {
+# ifdef HID_USB_CHECK_ENDPOINT
+ if (is_micros_timed_out(_last, 10000)) {
+ if (!_sent) {
+ _sendCurrent();
+ }
+ _last = micros();
+ }
+# endif
+ }
+
void clear() {
_kbd.releaseAll();
}
void sendKey(uint8_t code, bool state) {
- CHECK_HID_EP;
KeyboardKeycode usb_code = keymapUsb(code);
if (usb_code != KEY_ERROR_UNDEFINED) {
- if (state) _kbd.press(usb_code);
- else _kbd.release(usb_code);
+ if (state ? _kbd.add(usb_code) : _kbd.remove(usb_code)) {
+ _sendCurrent();
+ }
}
}
@@ -99,6 +111,20 @@ class UsbKeyboard {
private:
BootKeyboard_ _kbd;
+ bool _sent = true;
+ unsigned long _last = 0;
+
+ void _sendCurrent() {
+# ifdef HID_USB_CHECK_ENDPOINT
+ if (getOfflineAs(1)) {
+ _sent = false;
+ } else {
+# endif
+ _sent = (_kbd.send() >= 0);
+# ifdef HID_USB_CHECK_ENDPOINT
+ }
+# endif
+ }
};
#define CLS_SEND_BUTTONS \