2017-06-17 14:19:15 +02: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"
|
|
|
|
#include "host.h"
|
|
|
|
#include "keycode_config.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "util.h"
|
2018-11-16 07:22:05 +01:00
|
|
|
#include <string.h>
|
2017-06-17 14:19:15 +02:00
|
|
|
|
2018-03-22 07:50:38 +01:00
|
|
|
/** \brief has_anykey
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2019-08-30 20:19:03 +02:00
|
|
|
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);
|
2018-11-16 07:22:05 +01:00
|
|
|
#ifdef NKRO_ENABLE
|
|
|
|
if (keyboard_protocol && keymap_config.nkro) {
|
2019-08-30 20:19:03 +02:00
|
|
|
p = keyboard_report->nkro.bits;
|
2018-11-16 07:22:05 +01:00
|
|
|
lp = sizeof(keyboard_report->nkro.bits);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
while (lp--) {
|
2019-08-30 20:19:03 +02:00
|
|
|
if (*p++) cnt++;
|
2017-06-17 14:19:15 +02:00
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2018-03-22 07:50:38 +01:00
|
|
|
/** \brief get_first_key
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2019-08-30 20:19:03 +02:00
|
|
|
uint8_t get_first_key(report_keyboard_t* keyboard_report) {
|
2017-06-17 14:19:15 +02:00
|
|
|
#ifdef NKRO_ENABLE
|
|
|
|
if (keyboard_protocol && keymap_config.nkro) {
|
|
|
|
uint8_t i = 0;
|
|
|
|
for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
|
|
|
|
;
|
2019-08-30 20:19:03 +02:00
|
|
|
return i << 3 | biton(keyboard_report->nkro.bits[i]);
|
2017-06-17 14:19:15 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef USB_6KRO_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
|
|
|
|
}
|
|
|
|
|
2020-03-13 19:09:38 +01: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
|
|
|
|
*/
|
|
|
|
bool is_key_pressed(report_keyboard_t* keyboard_report, uint8_t key) {
|
2020-03-13 19:44:56 +01:00
|
|
|
if (key == KC_NO) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-13 19:09:38 +01:00
|
|
|
#ifdef NKRO_ENABLE
|
2020-03-13 19:44:56 +01:00
|
|
|
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;
|
|
|
|
}
|
2020-03-13 19:09:38 +01:00
|
|
|
}
|
|
|
|
#endif
|
2020-03-13 19:44:56 +01:00
|
|
|
for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
|
|
|
|
if (keyboard_report->keys[i] == key) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2020-03-13 19:09:38 +01:00
|
|
|
}
|
|
|
|
|
2018-03-22 07:50:38 +01:00
|
|
|
/** \brief add key byte
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2019-08-30 20:19:03 +02:00
|
|
|
void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code) {
|
2017-06-17 14:19:15 +02:00
|
|
|
#ifdef USB_6KRO_ENABLE
|
2019-08-30 20:19:03 +02:00
|
|
|
int8_t i = cb_head;
|
2017-06-17 14:19:15 +02:00
|
|
|
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--;
|
2019-08-30 20:19:03 +02:00
|
|
|
} else {
|
2017-06-17 14:19:15 +02:00
|
|
|
// left shift when has empty space
|
|
|
|
uint8_t offset = 1;
|
2019-08-30 20:19:03 +02:00
|
|
|
i = RO_INC(empty);
|
2017-06-17 14:19:15 +02:00
|
|
|
do {
|
|
|
|
if (keyboard_report->keys[i] != 0) {
|
|
|
|
keyboard_report->keys[empty] = keyboard_report->keys[i];
|
2019-08-30 20:19:03 +02:00
|
|
|
keyboard_report->keys[i] = 0;
|
|
|
|
empty = RO_INC(empty);
|
|
|
|
} else {
|
2017-06-17 14:19:15 +02:00
|
|
|
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;
|
2019-08-30 20:19:03 +02:00
|
|
|
cb_tail = RO_INC(cb_tail);
|
2017-06-17 14:19:15 +02:00
|
|
|
cb_count++;
|
|
|
|
#else
|
2019-08-30 20:19:03 +02:00
|
|
|
int8_t i = 0;
|
2017-06-17 14:19:15 +02: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-03-22 07:50:38 +01:00
|
|
|
/** \brief del key byte
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2019-08-30 20:19:03 +02:00
|
|
|
void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code) {
|
2017-06-17 14:19:15 +02:00
|
|
|
#ifdef USB_6KRO_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
|
2018-03-22 07:50:38 +01:00
|
|
|
/** \brief add key bit
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2019-08-30 20:19:03 +02:00
|
|
|
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);
|
2017-06-17 14:19:15 +02:00
|
|
|
} else {
|
|
|
|
dprintf("add_key_bit: can't add: %02X\n", code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 07:50:38 +01:00
|
|
|
/** \brief del key bit
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2019-08-30 20:19:03 +02:00
|
|
|
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));
|
2017-06-17 14:19:15 +02:00
|
|
|
} else {
|
|
|
|
dprintf("del_key_bit: can't del: %02X\n", code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2017-06-17 16:55:44 +02:00
|
|
|
|
2018-03-22 07:50:38 +01:00
|
|
|
/** \brief add key to report
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2019-08-30 20:19:03 +02:00
|
|
|
void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key) {
|
2017-06-17 16:55:44 +02:00
|
|
|
#ifdef NKRO_ENABLE
|
|
|
|
if (keyboard_protocol && keymap_config.nkro) {
|
|
|
|
add_key_bit(keyboard_report, key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
add_key_byte(keyboard_report, key);
|
|
|
|
}
|
|
|
|
|
2018-03-22 07:50:38 +01:00
|
|
|
/** \brief del key from report
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2019-08-30 20:19:03 +02:00
|
|
|
void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key) {
|
2017-06-17 16:55:44 +02:00
|
|
|
#ifdef NKRO_ENABLE
|
|
|
|
if (keyboard_protocol && keymap_config.nkro) {
|
|
|
|
del_key_bit(keyboard_report, key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
del_key_byte(keyboard_report, key);
|
|
|
|
}
|
|
|
|
|
2018-03-22 07:50:38 +01:00
|
|
|
/** \brief clear key from report
|
|
|
|
*
|
|
|
|
* FIXME: Needs doc
|
|
|
|
*/
|
2019-08-30 20:19:03 +02:00
|
|
|
void clear_keys_from_report(report_keyboard_t* keyboard_report) {
|
2017-06-17 16:55:44 +02:00
|
|
|
// not clear mods
|
2018-11-16 07:22:05 +01:00
|
|
|
#ifdef NKRO_ENABLE
|
|
|
|
if (keyboard_protocol && keymap_config.nkro) {
|
|
|
|
memset(keyboard_report->nkro.bits, 0, sizeof(keyboard_report->nkro.bits));
|
|
|
|
return;
|
2017-06-17 16:55:44 +02:00
|
|
|
}
|
2018-11-16 07:22:05 +01:00
|
|
|
#endif
|
|
|
|
memset(keyboard_report->keys, 0, sizeof(keyboard_report->keys));
|
2017-12-19 09:41:51 +01:00
|
|
|
}
|