HID_BadUSB_Azerty_Layout/furi_hal_usb_hid.h

271 lines
8.1 KiB
C
Raw Permalink Normal View History

2024-02-16 22:48:02 +01:00
#pragma once
#include "hid_usage_desktop.h"
#include "hid_usage_button.h"
#include "hid_usage_keyboard.h"
#include "hid_usage_consumer.h"
#include "hid_usage_led.h"
#ifdef __cplusplus
extern "C" {
#endif
#define HID_MANUF_PRODUCT_NAME_LEN 32
#define HID_VID_DEFAULT 0x046D
#define HID_PID_DEFAULT 0xC529
/** Max number of simultaneously pressed keys (keyboard) */
#define HID_KB_MAX_KEYS 6
/** Max number of simultaneously pressed keys (consumer control) */
#define HID_CONSUMER_MAX_KEYS 2
#define HID_KEYBOARD_NONE 0x00
/** HID keyboard modifier keys */
enum HidKeyboardMods {
KEY_MOD_LEFT_CTRL = (1 << 8),
KEY_MOD_LEFT_SHIFT = (1 << 9),
KEY_MOD_LEFT_ALT = (1 << 10),
KEY_MOD_LEFT_GUI = (1 << 11),
KEY_MOD_RIGHT_CTRL = (1 << 12),
KEY_MOD_RIGHT_SHIFT = (1 << 13),
KEY_MOD_RIGHT_ALT = (1 << 14),
KEY_MOD_RIGHT_GUI = (1 << 15),
};
/** ASCII to keycode conversion table */
static const uint16_t hid_asciimap[] = {
HID_KEYBOARD_NONE, // NUL
HID_KEYBOARD_NONE, // SOH
HID_KEYBOARD_NONE, // STX
HID_KEYBOARD_NONE, // ETX
HID_KEYBOARD_NONE, // EOT
HID_KEYBOARD_NONE, // ENQ
HID_KEYBOARD_NONE, // ACK
HID_KEYBOARD_NONE, // BEL
HID_KEYBOARD_DELETE, // BS Backspace
HID_KEYBOARD_TAB, // TAB Tab
HID_KEYBOARD_RETURN, // LF Enter
HID_KEYBOARD_NONE, // VT
HID_KEYBOARD_NONE, // FF
HID_KEYBOARD_NONE, // CR
HID_KEYBOARD_NONE, // SO
HID_KEYBOARD_NONE, // SI
HID_KEYBOARD_NONE, // DEL
HID_KEYBOARD_NONE, // DC1
HID_KEYBOARD_NONE, // DC2
HID_KEYBOARD_NONE, // DC3
HID_KEYBOARD_NONE, // DC4
HID_KEYBOARD_NONE, // NAK
HID_KEYBOARD_NONE, // SYN
HID_KEYBOARD_NONE, // ETB
HID_KEYBOARD_NONE, // CAN
HID_KEYBOARD_NONE, // EM
HID_KEYBOARD_NONE, // SUB
HID_KEYBOARD_NONE, // ESC
HID_KEYBOARD_NONE, // FS
HID_KEYBOARD_NONE, // GS
HID_KEYBOARD_NONE, // RS
HID_KEYBOARD_NONE, // US
HID_KEYBOARD_SPACEBAR, // ' ' Space
HID_KEYBOARD_FR_EXCLAMATION, // !
HID_KEYBOARD_FR_DOUBLE_QUOTE, // "
HID_KEYBOARD_FR_DIESE | KEY_MOD_RIGHT_ALT, // #
HID_KEYBOARD_FR_DOLLARS, // $
HID_KEYBOARD_FR_POURCENTAGE | KEY_MOD_LEFT_SHIFT, // %
HID_KEYBOARD_FR_ET_COMMERCIAL, // &
HID_KEYBOARD_FR_SIMPLE_QUOTE, // '
HID_KEYBOARD_FR_PAREN_OUVRANTE, // (
HID_KEYBOARD_FR_PAREN_FERMANTE, // )
HID_KEYBOARD_FR_ETOILE, // *
HID_KEYBOARD_FR_PLUS | KEY_MOD_LEFT_SHIFT, // +
HID_KEYBOARD_FR_VIRGULE, // ,
HID_KEYBOARD_FR_TIRET, // -
HID_KEYBOARD_FR_POINT | KEY_MOD_LEFT_SHIFT, // .
HID_KEYBOARD_FR_SLASH | KEY_MOD_LEFT_SHIFT, // /
HID_KEYBOARD_0 | KEY_MOD_LEFT_SHIFT, // 0
HID_KEYBOARD_1 | KEY_MOD_LEFT_SHIFT, // 1
HID_KEYBOARD_2 | KEY_MOD_LEFT_SHIFT, // 2
HID_KEYBOARD_3 | KEY_MOD_LEFT_SHIFT, // 3
HID_KEYBOARD_4 | KEY_MOD_LEFT_SHIFT, // 4
HID_KEYBOARD_5 | KEY_MOD_LEFT_SHIFT, // 5
HID_KEYBOARD_6 | KEY_MOD_LEFT_SHIFT, // 6
HID_KEYBOARD_7 | KEY_MOD_LEFT_SHIFT, // 7
HID_KEYBOARD_8 | KEY_MOD_LEFT_SHIFT, // 8
HID_KEYBOARD_9 | KEY_MOD_LEFT_SHIFT, // 9
HID_KEYBOARD_FR_DEUX_POINTS, // :
HID_KEYBOARD_FR_POINT_VIRG, // ;
HID_KEYBOARD_FR_INFERIEUR, // <
HID_KEYBOARD_FR_EGAL, // =
HID_KEYBOARD_FR_SUPERIEUR | KEY_MOD_LEFT_SHIFT, // >
HID_KEYBOARD_FR_INTERROGATION | KEY_MOD_LEFT_SHIFT, // ?
HID_KEYBOARD_FR_AROBASE | KEY_MOD_RIGHT_ALT, // @
HID_KEYBOARD_FR_A | KEY_MOD_LEFT_SHIFT, // A
HID_KEYBOARD_B | KEY_MOD_LEFT_SHIFT, // B
HID_KEYBOARD_C | KEY_MOD_LEFT_SHIFT, // C
HID_KEYBOARD_D | KEY_MOD_LEFT_SHIFT, // D
HID_KEYBOARD_E | KEY_MOD_LEFT_SHIFT, // E
HID_KEYBOARD_F | KEY_MOD_LEFT_SHIFT, // F
HID_KEYBOARD_G | KEY_MOD_LEFT_SHIFT, // G
HID_KEYBOARD_H | KEY_MOD_LEFT_SHIFT, // H
HID_KEYBOARD_I | KEY_MOD_LEFT_SHIFT, // I
HID_KEYBOARD_J | KEY_MOD_LEFT_SHIFT, // J
HID_KEYBOARD_K | KEY_MOD_LEFT_SHIFT, // K
HID_KEYBOARD_L | KEY_MOD_LEFT_SHIFT, // L
HID_KEYBOARD_FR_M | KEY_MOD_LEFT_SHIFT, // M
HID_KEYBOARD_N | KEY_MOD_LEFT_SHIFT, // N
HID_KEYBOARD_O | KEY_MOD_LEFT_SHIFT, // O
HID_KEYBOARD_P | KEY_MOD_LEFT_SHIFT, // P
HID_KEYBOARD_FR_Q | KEY_MOD_LEFT_SHIFT, // Q
HID_KEYBOARD_R | KEY_MOD_LEFT_SHIFT, // R
HID_KEYBOARD_S | KEY_MOD_LEFT_SHIFT, // S
HID_KEYBOARD_T | KEY_MOD_LEFT_SHIFT, // T
HID_KEYBOARD_U | KEY_MOD_LEFT_SHIFT, // U
HID_KEYBOARD_V | KEY_MOD_LEFT_SHIFT, // V
HID_KEYBOARD_FR_W | KEY_MOD_LEFT_SHIFT, // W
HID_KEYBOARD_X | KEY_MOD_LEFT_SHIFT, // X
HID_KEYBOARD_Y | KEY_MOD_LEFT_SHIFT, // Y
HID_KEYBOARD_FR_Z | KEY_MOD_LEFT_SHIFT, // Z
HID_KEYBOARD_FR_BRACK_OUVRANT | KEY_MOD_RIGHT_ALT, // [
HID_KEYBOARD_FR_BACKSLASH | KEY_MOD_RIGHT_ALT, // bslash
HID_KEYBOARD_FR_BRACK_FERMANT | KEY_MOD_RIGHT_ALT, // ]
HID_KEYBOARD_FR_CHAPEAU, // ^
HID_KEYBOARD_FR_UNDERSCORE, // _
HID_KEYBOARD_FR_BACK_QUOTE | KEY_MOD_RIGHT_ALT, // `
2024-02-16 23:29:54 +01:00
HID_KEYBOARD_FR_A, // a
2024-02-16 22:48:02 +01:00
HID_KEYBOARD_B, // b
HID_KEYBOARD_C, // c
HID_KEYBOARD_D, // d
HID_KEYBOARD_E, // e
HID_KEYBOARD_F, // f
HID_KEYBOARD_G, // g
HID_KEYBOARD_H, // h
HID_KEYBOARD_I, // i
HID_KEYBOARD_J, // j
HID_KEYBOARD_K, // k
HID_KEYBOARD_L, // l
2024-02-16 23:29:54 +01:00
HID_KEYBOARD_FR_M, // m
2024-02-16 22:48:02 +01:00
HID_KEYBOARD_N, // n
HID_KEYBOARD_O, // o
HID_KEYBOARD_P, // p
2024-02-16 23:29:54 +01:00
HID_KEYBOARD_FR_Q, // q
2024-02-16 22:48:02 +01:00
HID_KEYBOARD_R, // r
HID_KEYBOARD_S, // s
HID_KEYBOARD_T, // t
HID_KEYBOARD_U, // u
HID_KEYBOARD_V, // v
2024-02-16 23:29:54 +01:00
HID_KEYBOARD_FR_W, // w
2024-02-16 22:48:02 +01:00
HID_KEYBOARD_X, // x
HID_KEYBOARD_Y, // y
2024-02-16 23:29:54 +01:00
HID_KEYBOARD_FR_Z, // z
2024-02-16 22:48:02 +01:00
HID_KEYBOARD_FR_CRO_OUVRANT | KEY_MOD_RIGHT_ALT, // {
HID_KEYBOARD_FR_PIPE | KEY_MOD_RIGHT_ALT, // |
HID_KEYBOARD_FR_CRO_FERMANT | KEY_MOD_RIGHT_ALT, // }
HID_KEYBOARD_FR_TILD | KEY_MOD_RIGHT_ALT, // ~
HID_KEYBOARD_NONE, // DEL
};
typedef struct {
// Good job knobheads, these should be uint16_t
uint32_t vid;
uint32_t pid;
char manuf[HID_MANUF_PRODUCT_NAME_LEN];
char product[HID_MANUF_PRODUCT_NAME_LEN];
} FuriHalUsbHidConfig;
typedef void (*HidStateCallback)(bool state, void* context);
/** ASCII to keycode conversion macro */
#define HID_ASCII_TO_KEY(x) (((uint8_t)x < 128) ? (hid_asciimap[(uint8_t)x]) : HID_KEYBOARD_NONE)
/** HID keyboard leds */
enum HidKeyboardLeds {
HID_KB_LED_NUM = (1 << 0),
HID_KB_LED_CAPS = (1 << 1),
HID_KB_LED_SCROLL = (1 << 2),
};
/** HID mouse buttons */
enum HidMouseButtons {
HID_MOUSE_BTN_LEFT = (1 << 0),
HID_MOUSE_BTN_RIGHT = (1 << 1),
HID_MOUSE_BTN_WHEEL = (1 << 2),
};
/** Get USB HID connection state
*
* @return true / false
*/
bool furi_hal_hid_is_connected();
/** Get USB HID keyboard leds state
*
* @return leds state
*/
uint8_t furi_hal_hid_get_led_state();
/** Set USB HID connect/disconnect callback
*
* @param cb callback
* @param ctx callback context
*/
void furi_hal_hid_set_state_callback(HidStateCallback cb, void* ctx);
/** Set the following key to pressed state and send HID report
*
* @param button key code
*/
bool furi_hal_hid_kb_press(uint16_t button);
/** Set the following key to released state and send HID report
*
* @param button key code
*/
bool furi_hal_hid_kb_release(uint16_t button);
/** Clear all pressed keys and send HID report
*
*/
bool furi_hal_hid_kb_release_all();
/** Set mouse movement and send HID report
*
* @param dx x coordinate delta
* @param dy y coordinate delta
*/
bool furi_hal_hid_mouse_move(int8_t dx, int8_t dy);
/** Set mouse button to pressed state and send HID report
*
* @param button key code
*/
bool furi_hal_hid_mouse_press(uint8_t button);
/** Set mouse button to released state and send HID report
*
* @param button key code
*/
bool furi_hal_hid_mouse_release(uint8_t button);
/** Set mouse wheel position and send HID report
*
* @param delta number of scroll steps
*/
bool furi_hal_hid_mouse_scroll(int8_t delta);
/** Set the following consumer key to pressed state and send HID report
*
* @param button key code
*/
bool furi_hal_hid_consumer_key_press(uint16_t button);
/** Set the following consumer key to released state and send HID report
*
* @param button key code
*/
bool furi_hal_hid_consumer_key_release(uint16_t button);
#ifdef __cplusplus
}
#endif