commit dcfffa7b67a072f7d9e37bd8c0029c53b61aeb0f parent 0c87e2e7025140ca6105b7fec2639c78c3cd8109 Author: Joel Challis <git@zvecr.com> Date: Thu, 28 Oct 2021 22:31:59 +0100 Relocate protocol files within tmk_core/common/ (#14972) * Relocate non platform files within tmk_core/common/ * clang Diffstat:
19 files changed, 299 insertions(+), 292 deletions(-)
diff --git a/common_features.mk b/common_features.mk @@ -27,7 +27,7 @@ QUANTUM_SRC += \ $(QUANTUM_DIR)/keyboard.c \ $(QUANTUM_DIR)/keymap_common.c \ $(QUANTUM_DIR)/keycode_config.c \ - $(QUANTUM_DIR)/usb_device_state.c \ + $(QUANTUM_DIR)/sync_timer.c \ $(QUANTUM_DIR)/logging/debug.c \ $(QUANTUM_DIR)/logging/sendchar.c \ diff --git a/quantum/keyboard.c b/quantum/keyboard.c @@ -94,6 +94,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #ifdef DIGITIZER_ENABLE # include "digitizer.h" #endif +#ifdef VIRTSER_ENABLE +# include "virtser.h" +#endif static uint32_t last_input_modification_time = 0; uint32_t last_input_activity_time(void) { return last_input_modification_time; } diff --git a/tmk_core/common/raw_hid.h b/quantum/raw_hid.h diff --git a/tmk_core/common/sync_timer.c b/quantum/sync_timer.c diff --git a/tmk_core/common/sync_timer.h b/quantum/sync_timer.h diff --git a/quantum/virtser.h b/quantum/virtser.h @@ -0,0 +1,9 @@ +#pragma once + +void virtser_init(void); + +/* Define this function in your code to process incoming bytes */ +void virtser_recv(const uint8_t ch); + +/* Call this to send a character over the Virtual Serial Device */ +void virtser_send(const uint8_t byte); diff --git a/tmk_core/common.mk b/tmk_core/common.mk @@ -2,10 +2,6 @@ COMMON_DIR = common PLATFORM_COMMON_DIR = $(COMMON_DIR)/$(PLATFORM_KEY) TMK_COMMON_SRC += \ - $(COMMON_DIR)/host.c \ - $(COMMON_DIR)/report.c \ - $(COMMON_DIR)/sync_timer.c \ - $(COMMON_DIR)/usb_util.c \ $(PLATFORM_COMMON_DIR)/platform.c \ $(PLATFORM_COMMON_DIR)/suspend.c \ $(PLATFORM_COMMON_DIR)/timer.c \ diff --git a/tmk_core/common/report.c b/tmk_core/common/report.c @@ -1,280 +0,0 @@ -/* Copyright 2017 Fred Sundvik - * - * 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 2 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 <http://www.gnu.org/licenses/>. - */ - -#include "report.h" -#include "host.h" -#include "keycode_config.h" -#include "debug.h" -#include "util.h" -#include <string.h> - -#ifdef RING_BUFFERED_6KRO_REPORT_ENABLE -# define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS) -# define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS) -# define RO_INC(a) RO_ADD(a, 1) -# define RO_DEC(a) RO_SUB(a, 1) -static int8_t cb_head = 0; -static int8_t cb_tail = 0; -static int8_t cb_count = 0; -#endif - -/** \brief has_anykey - * - * FIXME: Needs doc - */ -uint8_t has_anykey(report_keyboard_t* keyboard_report) { - uint8_t cnt = 0; - uint8_t* p = keyboard_report->keys; - uint8_t lp = sizeof(keyboard_report->keys); -#ifdef NKRO_ENABLE - if (keyboard_protocol && keymap_config.nkro) { - p = keyboard_report->nkro.bits; - lp = sizeof(keyboard_report->nkro.bits); - } -#endif - while (lp--) { - if (*p++) cnt++; - } - return cnt; -} - -/** \brief get_first_key - * - * FIXME: Needs doc - */ -uint8_t get_first_key(report_keyboard_t* keyboard_report) { -#ifdef NKRO_ENABLE - if (keyboard_protocol && keymap_config.nkro) { - uint8_t i = 0; - for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++) - ; - return i << 3 | biton(keyboard_report->nkro.bits[i]); - } -#endif -#ifdef RING_BUFFERED_6KRO_REPORT_ENABLE - uint8_t i = cb_head; - do { - if (keyboard_report->keys[i] != 0) { - break; - } - i = RO_INC(i); - } while (i != cb_tail); - return keyboard_report->keys[i]; -#else - return keyboard_report->keys[0]; -#endif -} - -/** \brief Checks if a key is pressed in the report - * - * Returns true if the keyboard_report reports that the key is pressed, otherwise false - * Note: The function doesn't support modifers currently, and it returns false for KC_NO - */ -bool is_key_pressed(report_keyboard_t* keyboard_report, uint8_t key) { - if (key == KC_NO) { - return false; - } -#ifdef NKRO_ENABLE - if (keyboard_protocol && keymap_config.nkro) { - if ((key >> 3) < KEYBOARD_REPORT_BITS) { - return keyboard_report->nkro.bits[key >> 3] & 1 << (key & 7); - } else { - return false; - } - } -#endif - for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) { - if (keyboard_report->keys[i] == key) { - return true; - } - } - return false; -} - -/** \brief add key byte - * - * FIXME: Needs doc - */ -void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code) { -#ifdef RING_BUFFERED_6KRO_REPORT_ENABLE - int8_t i = cb_head; - int8_t empty = -1; - if (cb_count) { - do { - if (keyboard_report->keys[i] == code) { - return; - } - if (empty == -1 && keyboard_report->keys[i] == 0) { - empty = i; - } - i = RO_INC(i); - } while (i != cb_tail); - if (i == cb_tail) { - if (cb_tail == cb_head) { - // buffer is full - if (empty == -1) { - // pop head when has no empty space - cb_head = RO_INC(cb_head); - cb_count--; - } else { - // left shift when has empty space - uint8_t offset = 1; - i = RO_INC(empty); - do { - if (keyboard_report->keys[i] != 0) { - keyboard_report->keys[empty] = keyboard_report->keys[i]; - keyboard_report->keys[i] = 0; - empty = RO_INC(empty); - } else { - offset++; - } - i = RO_INC(i); - } while (i != cb_tail); - cb_tail = RO_SUB(cb_tail, offset); - } - } - } - } - // add to tail - keyboard_report->keys[cb_tail] = code; - cb_tail = RO_INC(cb_tail); - cb_count++; -#else - int8_t i = 0; - int8_t empty = -1; - for (; i < KEYBOARD_REPORT_KEYS; i++) { - if (keyboard_report->keys[i] == code) { - break; - } - if (empty == -1 && keyboard_report->keys[i] == 0) { - empty = i; - } - } - if (i == KEYBOARD_REPORT_KEYS) { - if (empty != -1) { - keyboard_report->keys[empty] = code; - } - } -#endif -} - -/** \brief del key byte - * - * FIXME: Needs doc - */ -void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code) { -#ifdef RING_BUFFERED_6KRO_REPORT_ENABLE - uint8_t i = cb_head; - if (cb_count) { - do { - if (keyboard_report->keys[i] == code) { - keyboard_report->keys[i] = 0; - cb_count--; - if (cb_count == 0) { - // reset head and tail - cb_tail = cb_head = 0; - } - if (i == RO_DEC(cb_tail)) { - // left shift when next to tail - do { - cb_tail = RO_DEC(cb_tail); - if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) { - break; - } - } while (cb_tail != cb_head); - } - break; - } - i = RO_INC(i); - } while (i != cb_tail); - } -#else - for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { - if (keyboard_report->keys[i] == code) { - keyboard_report->keys[i] = 0; - } - } -#endif -} - -#ifdef NKRO_ENABLE -/** \brief add key bit - * - * FIXME: Needs doc - */ -void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code) { - if ((code >> 3) < KEYBOARD_REPORT_BITS) { - keyboard_report->nkro.bits[code >> 3] |= 1 << (code & 7); - } else { - dprintf("add_key_bit: can't add: %02X\n", code); - } -} - -/** \brief del key bit - * - * FIXME: Needs doc - */ -void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code) { - if ((code >> 3) < KEYBOARD_REPORT_BITS) { - keyboard_report->nkro.bits[code >> 3] &= ~(1 << (code & 7)); - } else { - dprintf("del_key_bit: can't del: %02X\n", code); - } -} -#endif - -/** \brief add key to report - * - * FIXME: Needs doc - */ -void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key) { -#ifdef NKRO_ENABLE - if (keyboard_protocol && keymap_config.nkro) { - add_key_bit(keyboard_report, key); - return; - } -#endif - add_key_byte(keyboard_report, key); -} - -/** \brief del key from report - * - * FIXME: Needs doc - */ -void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key) { -#ifdef NKRO_ENABLE - if (keyboard_protocol && keymap_config.nkro) { - del_key_bit(keyboard_report, key); - return; - } -#endif - del_key_byte(keyboard_report, key); -} - -/** \brief clear key from report - * - * FIXME: Needs doc - */ -void clear_keys_from_report(report_keyboard_t* keyboard_report) { - // not clear mods -#ifdef NKRO_ENABLE - if (keyboard_protocol && keymap_config.nkro) { - memset(keyboard_report->nkro.bits, 0, sizeof(keyboard_report->nkro.bits)); - return; - } -#endif - memset(keyboard_report->keys, 0, sizeof(keyboard_report->keys)); -} diff --git a/tmk_core/common/virtser.h b/tmk_core/common/virtser.h @@ -1,7 +0,0 @@ -#pragma once - -/* Define this function in your code to process incoming bytes */ -void virtser_recv(const uint8_t ch); - -/* Call this to send a character over the Virtual Serial Device */ -void virtser_send(const uint8_t byte); diff --git a/tmk_core/protocol.mk b/tmk_core/protocol.mk @@ -1,5 +1,11 @@ PROTOCOL_DIR = protocol +TMK_COMMON_SRC += \ + $(PROTOCOL_DIR)/host.c \ + $(PROTOCOL_DIR)/report.c \ + $(PROTOCOL_DIR)/usb_device_state.c \ + $(PROTOCOL_DIR)/usb_util.c \ + ifeq ($(strip $(USB_HID_ENABLE)), yes) include $(TMK_DIR)/protocol/usb_hid.mk endif diff --git a/tmk_core/common/host.c b/tmk_core/protocol/host.c diff --git a/tmk_core/common/host.h b/tmk_core/protocol/host.h diff --git a/tmk_core/common/host_driver.h b/tmk_core/protocol/host_driver.h diff --git a/tmk_core/protocol/report.c b/tmk_core/protocol/report.c @@ -0,0 +1,280 @@ +/* Copyright 2017 Fred Sundvik + * + * 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 2 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 <http://www.gnu.org/licenses/>. + */ + +#include "report.h" +#include "host.h" +#include "keycode_config.h" +#include "debug.h" +#include "util.h" +#include <string.h> + +#ifdef RING_BUFFERED_6KRO_REPORT_ENABLE +# define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS) +# define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS) +# define RO_INC(a) RO_ADD(a, 1) +# define RO_DEC(a) RO_SUB(a, 1) +static int8_t cb_head = 0; +static int8_t cb_tail = 0; +static int8_t cb_count = 0; +#endif + +/** \brief has_anykey + * + * FIXME: Needs doc + */ +uint8_t has_anykey(report_keyboard_t* keyboard_report) { + uint8_t cnt = 0; + uint8_t* p = keyboard_report->keys; + uint8_t lp = sizeof(keyboard_report->keys); +#ifdef NKRO_ENABLE + if (keyboard_protocol && keymap_config.nkro) { + p = keyboard_report->nkro.bits; + lp = sizeof(keyboard_report->nkro.bits); + } +#endif + while (lp--) { + if (*p++) cnt++; + } + return cnt; +} + +/** \brief get_first_key + * + * FIXME: Needs doc + */ +uint8_t get_first_key(report_keyboard_t* keyboard_report) { +#ifdef NKRO_ENABLE + if (keyboard_protocol && keymap_config.nkro) { + uint8_t i = 0; + for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++) + ; + return i << 3 | biton(keyboard_report->nkro.bits[i]); + } +#endif +#ifdef RING_BUFFERED_6KRO_REPORT_ENABLE + uint8_t i = cb_head; + do { + if (keyboard_report->keys[i] != 0) { + break; + } + i = RO_INC(i); + } while (i != cb_tail); + return keyboard_report->keys[i]; +#else + return keyboard_report->keys[0]; +#endif +} + +/** \brief Checks if a key is pressed in the report + * + * Returns true if the keyboard_report reports that the key is pressed, otherwise false + * Note: The function doesn't support modifers currently, and it returns false for KC_NO + */ +bool is_key_pressed(report_keyboard_t* keyboard_report, uint8_t key) { + if (key == KC_NO) { + return false; + } +#ifdef NKRO_ENABLE + if (keyboard_protocol && keymap_config.nkro) { + if ((key >> 3) < KEYBOARD_REPORT_BITS) { + return keyboard_report->nkro.bits[key >> 3] & 1 << (key & 7); + } else { + return false; + } + } +#endif + for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) { + if (keyboard_report->keys[i] == key) { + return true; + } + } + return false; +} + +/** \brief add key byte + * + * FIXME: Needs doc + */ +void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code) { +#ifdef RING_BUFFERED_6KRO_REPORT_ENABLE + int8_t i = cb_head; + int8_t empty = -1; + if (cb_count) { + do { + if (keyboard_report->keys[i] == code) { + return; + } + if (empty == -1 && keyboard_report->keys[i] == 0) { + empty = i; + } + i = RO_INC(i); + } while (i != cb_tail); + if (i == cb_tail) { + if (cb_tail == cb_head) { + // buffer is full + if (empty == -1) { + // pop head when has no empty space + cb_head = RO_INC(cb_head); + cb_count--; + } else { + // left shift when has empty space + uint8_t offset = 1; + i = RO_INC(empty); + do { + if (keyboard_report->keys[i] != 0) { + keyboard_report->keys[empty] = keyboard_report->keys[i]; + keyboard_report->keys[i] = 0; + empty = RO_INC(empty); + } else { + offset++; + } + i = RO_INC(i); + } while (i != cb_tail); + cb_tail = RO_SUB(cb_tail, offset); + } + } + } + } + // add to tail + keyboard_report->keys[cb_tail] = code; + cb_tail = RO_INC(cb_tail); + cb_count++; +#else + int8_t i = 0; + int8_t empty = -1; + for (; i < KEYBOARD_REPORT_KEYS; i++) { + if (keyboard_report->keys[i] == code) { + break; + } + if (empty == -1 && keyboard_report->keys[i] == 0) { + empty = i; + } + } + if (i == KEYBOARD_REPORT_KEYS) { + if (empty != -1) { + keyboard_report->keys[empty] = code; + } + } +#endif +} + +/** \brief del key byte + * + * FIXME: Needs doc + */ +void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code) { +#ifdef RING_BUFFERED_6KRO_REPORT_ENABLE + uint8_t i = cb_head; + if (cb_count) { + do { + if (keyboard_report->keys[i] == code) { + keyboard_report->keys[i] = 0; + cb_count--; + if (cb_count == 0) { + // reset head and tail + cb_tail = cb_head = 0; + } + if (i == RO_DEC(cb_tail)) { + // left shift when next to tail + do { + cb_tail = RO_DEC(cb_tail); + if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) { + break; + } + } while (cb_tail != cb_head); + } + break; + } + i = RO_INC(i); + } while (i != cb_tail); + } +#else + for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { + if (keyboard_report->keys[i] == code) { + keyboard_report->keys[i] = 0; + } + } +#endif +} + +#ifdef NKRO_ENABLE +/** \brief add key bit + * + * FIXME: Needs doc + */ +void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code) { + if ((code >> 3) < KEYBOARD_REPORT_BITS) { + keyboard_report->nkro.bits[code >> 3] |= 1 << (code & 7); + } else { + dprintf("add_key_bit: can't add: %02X\n", code); + } +} + +/** \brief del key bit + * + * FIXME: Needs doc + */ +void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code) { + if ((code >> 3) < KEYBOARD_REPORT_BITS) { + keyboard_report->nkro.bits[code >> 3] &= ~(1 << (code & 7)); + } else { + dprintf("del_key_bit: can't del: %02X\n", code); + } +} +#endif + +/** \brief add key to report + * + * FIXME: Needs doc + */ +void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key) { +#ifdef NKRO_ENABLE + if (keyboard_protocol && keymap_config.nkro) { + add_key_bit(keyboard_report, key); + return; + } +#endif + add_key_byte(keyboard_report, key); +} + +/** \brief del key from report + * + * FIXME: Needs doc + */ +void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key) { +#ifdef NKRO_ENABLE + if (keyboard_protocol && keymap_config.nkro) { + del_key_bit(keyboard_report, key); + return; + } +#endif + del_key_byte(keyboard_report, key); +} + +/** \brief clear key from report + * + * FIXME: Needs doc + */ +void clear_keys_from_report(report_keyboard_t* keyboard_report) { + // not clear mods +#ifdef NKRO_ENABLE + if (keyboard_protocol && keymap_config.nkro) { + memset(keyboard_report->nkro.bits, 0, sizeof(keyboard_report->nkro.bits)); + return; + } +#endif + memset(keyboard_report->keys, 0, sizeof(keyboard_report->keys)); +} diff --git a/tmk_core/common/report.h b/tmk_core/protocol/report.h diff --git a/quantum/usb_device_state.c b/tmk_core/protocol/usb_device_state.c diff --git a/quantum/usb_device_state.h b/tmk_core/protocol/usb_device_state.h diff --git a/tmk_core/common/usb_util.c b/tmk_core/protocol/usb_util.c diff --git a/tmk_core/common/usb_util.h b/tmk_core/protocol/usb_util.h