2017-06-17 12:19:15 +00:00
|
|
|
/* 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"
|
2023-10-23 04:43:46 +00:00
|
|
|
#include "action_util.h"
|
2017-06-17 12:19:15 +00:00
|
|
|
#include "host.h"
|
|
|
|
#include "keycode_config.h"
|
|
|
|
#include "debug.h"
|
2024-10-18 07:57:08 +00:00
|
|
|
#include "usb_device_state.h"
|
2017-06-17 12:19:15 +00:00
|
|
|
#include "util.h"
|
2018-11-16 06:22:05 +00:00
|
|
|
#include <string.h>
|
2017-06-17 12:19:15 +00:00
|
|
|
|
2018-03-22 06:50:38 +00:00
|
|
|
/** \brief has_anykey
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2023-10-23 04:43:46 +00:00
|
|
|
uint8_t has_anykey(void) {
|
2019-08-30 18:19:03 +00:00
|
|
|
uint8_t cnt = 0;
|
|
|
|
uint8_t* p = keyboard_report->keys;
|
|
|
|
uint8_t lp = sizeof(keyboard_report->keys);
|
2018-11-16 06:22:05 +00:00
|
|
|
#ifdef NKRO_ENABLE
|
2024-10-18 07:57:08 +00:00
|
|
|
if (usb_device_state_get_protocol() == USB_PROTOCOL_REPORT && keymap_config.nkro) {
|
2023-10-23 04:43:46 +00:00
|
|
|
p = nkro_report->bits;
|
|
|
|
lp = sizeof(nkro_report->bits);
|
2018-11-16 06:22:05 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
while (lp--) {
|
2019-08-30 18:19:03 +00:00
|
|
|
if (*p++) cnt++;
|
2017-06-17 12:19:15 +00:00
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2018-03-22 06:50:38 +00:00
|
|
|
/** \brief get_first_key
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2023-10-23 04:43:46 +00:00
|
|
|
uint8_t get_first_key(void) {
|
2017-06-17 12:19:15 +00:00
|
|
|
#ifdef NKRO_ENABLE
|
2024-10-18 07:57:08 +00:00
|
|
|
if (usb_device_state_get_protocol() == USB_PROTOCOL_REPORT && keymap_config.nkro) {
|
2017-06-17 12:19:15 +00:00
|
|
|
uint8_t i = 0;
|
2023-10-23 04:43:46 +00:00
|
|
|
for (; i < NKRO_REPORT_BITS && !nkro_report->bits[i]; i++)
|
2017-06-17 12:19:15 +00:00
|
|
|
;
|
2023-10-23 04:43:46 +00:00
|
|
|
return i << 3 | biton(nkro_report->bits[i]);
|
2017-06-17 12:19:15 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return keyboard_report->keys[0];
|
|
|
|
}
|
|
|
|
|
2020-03-13 18:09:38 +00:00
|
|
|
/** \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
|
|
|
|
*/
|
2023-10-23 04:43:46 +00:00
|
|
|
bool is_key_pressed(uint8_t key) {
|
2020-03-13 18:44:56 +00:00
|
|
|
if (key == KC_NO) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-13 18:09:38 +00:00
|
|
|
#ifdef NKRO_ENABLE
|
2024-10-18 07:57:08 +00:00
|
|
|
if (usb_device_state_get_protocol() == USB_PROTOCOL_REPORT && keymap_config.nkro) {
|
2023-10-14 11:21:20 +00:00
|
|
|
if ((key >> 3) < NKRO_REPORT_BITS) {
|
2023-10-23 04:43:46 +00:00
|
|
|
return nkro_report->bits[key >> 3] & 1 << (key & 7);
|
2020-03-13 18:44:56 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-13 18:09:38 +00:00
|
|
|
}
|
|
|
|
#endif
|
2020-03-13 18:44:56 +00:00
|
|
|
for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
|
|
|
|
if (keyboard_report->keys[i] == key) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2020-03-13 18:09:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 06:50:38 +00:00
|
|
|
/** \brief add key byte
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2019-08-30 18:19:03 +00:00
|
|
|
void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code) {
|
|
|
|
int8_t i = 0;
|
2017-06-17 12:19:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 06:50:38 +00:00
|
|
|
/** \brief del key byte
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2019-08-30 18:19:03 +00:00
|
|
|
void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code) {
|
2017-06-17 12:19:15 +00:00
|
|
|
for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
|
|
|
|
if (keyboard_report->keys[i] == code) {
|
|
|
|
keyboard_report->keys[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NKRO_ENABLE
|
2018-03-22 06:50:38 +00:00
|
|
|
/** \brief add key bit
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2023-10-23 04:43:46 +00:00
|
|
|
void add_key_bit(report_nkro_t* nkro_report, uint8_t code) {
|
2023-10-14 11:21:20 +00:00
|
|
|
if ((code >> 3) < NKRO_REPORT_BITS) {
|
2023-10-23 04:43:46 +00:00
|
|
|
nkro_report->bits[code >> 3] |= 1 << (code & 7);
|
2017-06-17 12:19:15 +00:00
|
|
|
} else {
|
|
|
|
dprintf("add_key_bit: can't add: %02X\n", code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 06:50:38 +00:00
|
|
|
/** \brief del key bit
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2023-10-23 04:43:46 +00:00
|
|
|
void del_key_bit(report_nkro_t* nkro_report, uint8_t code) {
|
2023-10-14 11:21:20 +00:00
|
|
|
if ((code >> 3) < NKRO_REPORT_BITS) {
|
2023-10-23 04:43:46 +00:00
|
|
|
nkro_report->bits[code >> 3] &= ~(1 << (code & 7));
|
2017-06-17 12:19:15 +00:00
|
|
|
} else {
|
|
|
|
dprintf("del_key_bit: can't del: %02X\n", code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2017-06-17 14:55:44 +00:00
|
|
|
|
2018-03-22 06:50:38 +00:00
|
|
|
/** \brief add key to report
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2023-10-23 04:43:46 +00:00
|
|
|
void add_key_to_report(uint8_t key) {
|
2017-06-17 14:55:44 +00:00
|
|
|
#ifdef NKRO_ENABLE
|
2024-10-18 07:57:08 +00:00
|
|
|
if (usb_device_state_get_protocol() == USB_PROTOCOL_REPORT && keymap_config.nkro) {
|
2023-10-23 04:43:46 +00:00
|
|
|
add_key_bit(nkro_report, key);
|
2017-06-17 14:55:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
add_key_byte(keyboard_report, key);
|
|
|
|
}
|
|
|
|
|
2018-03-22 06:50:38 +00:00
|
|
|
/** \brief del key from report
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2023-10-23 04:43:46 +00:00
|
|
|
void del_key_from_report(uint8_t key) {
|
2017-06-17 14:55:44 +00:00
|
|
|
#ifdef NKRO_ENABLE
|
2024-10-18 07:57:08 +00:00
|
|
|
if (usb_device_state_get_protocol() == USB_PROTOCOL_REPORT && keymap_config.nkro) {
|
2023-10-23 04:43:46 +00:00
|
|
|
del_key_bit(nkro_report, key);
|
2017-06-17 14:55:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
del_key_byte(keyboard_report, key);
|
|
|
|
}
|
|
|
|
|
2018-03-22 06:50:38 +00:00
|
|
|
/** \brief clear key from report
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2023-10-23 04:43:46 +00:00
|
|
|
void clear_keys_from_report(void) {
|
2017-06-17 14:55:44 +00:00
|
|
|
// not clear mods
|
2018-11-16 06:22:05 +00:00
|
|
|
#ifdef NKRO_ENABLE
|
2024-10-18 07:57:08 +00:00
|
|
|
if (usb_device_state_get_protocol() == USB_PROTOCOL_REPORT && keymap_config.nkro) {
|
2023-10-23 04:43:46 +00:00
|
|
|
memset(nkro_report->bits, 0, sizeof(nkro_report->bits));
|
2018-11-16 06:22:05 +00:00
|
|
|
return;
|
2017-06-17 14:55:44 +00:00
|
|
|
}
|
2018-11-16 06:22:05 +00:00
|
|
|
#endif
|
|
|
|
memset(keyboard_report->keys, 0, sizeof(keyboard_report->keys));
|
2017-12-19 08:41:51 +00:00
|
|
|
}
|
2022-03-14 00:01:47 +00:00
|
|
|
|
|
|
|
#ifdef MOUSE_ENABLE
|
|
|
|
/**
|
2023-08-02 11:47:25 +00:00
|
|
|
* @brief Compares 2 mouse reports for difference and returns result. Empty
|
|
|
|
* reports always evaluate as unchanged.
|
2022-03-14 00:01:47 +00:00
|
|
|
*
|
|
|
|
* @param[in] new_report report_mouse_t
|
|
|
|
* @param[in] old_report report_mouse_t
|
|
|
|
* @return bool result
|
|
|
|
*/
|
|
|
|
__attribute__((weak)) bool has_mouse_report_changed(report_mouse_t* new_report, report_mouse_t* old_report) {
|
2023-08-02 11:47:25 +00:00
|
|
|
// memcmp doesn't work here because of the `report_id` field when using
|
|
|
|
// shared mouse endpoint
|
|
|
|
bool changed = ((new_report->buttons != old_report->buttons) ||
|
|
|
|
# ifdef MOUSE_EXTENDED_REPORT
|
|
|
|
(new_report->boot_x != 0 && new_report->boot_x != old_report->boot_x) || (new_report->boot_y != 0 && new_report->boot_y != old_report->boot_y) ||
|
|
|
|
# endif
|
|
|
|
(new_report->x != 0 && new_report->x != old_report->x) || (new_report->y != 0 && new_report->y != old_report->y) || (new_report->h != 0 && new_report->h != old_report->h) || (new_report->v != 0 && new_report->v != old_report->v));
|
|
|
|
return changed;
|
2022-03-14 00:01:47 +00:00
|
|
|
}
|
|
|
|
#endif
|