improved mac uefi keys handling

This commit is contained in:
Devaev Maxim
2021-04-22 03:21:06 +03:00
parent e6ecbb2a9c
commit 4279ae5bc3
3 changed files with 68 additions and 8 deletions

View File

@@ -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;
}

32
hid/src/tools.h Normal file
View File

@@ -0,0 +1,32 @@
/*****************************************************************************
# #
# KVMD - The main Pi-KVM daemon. #
# #
# Copyright (C) 2018-2021 Maxim Devaev <mdevaev@gmail.com> #
# #
# 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)
);
}

View File

@@ -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 \