Move keyboard event handling into action.c

Since keyboard_layout.[ch] has gained more features, and handles more than just
filling out the keyboard report, move it to action.[ch] instead. I see no sane
way to separate the keyboard and mouse report filling, unless we want to loop
over key states twice, hence them being in the same file.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
This commit is contained in:
Gergely Nagy
2016-12-15 18:32:28 +01:00
parent 440c6d8ca8
commit 39dd0df58c
8 changed files with 123 additions and 158 deletions

View File

@@ -1,4 +1,4 @@
#include "keyboard_layout.h"
#include "action.h"
#include "led_display.h"
#include "layer.h"
#include "usb_interface_mouse.h"
@@ -38,7 +38,7 @@ static void clearKeymasks(const uint8_t *leftKeyStates, const uint8_t *rightKeyS
}
}
bool pressKey(uhk_key_t key, int scancodeIdx, usb_keyboard_report_t *report) {
static bool pressKey(uhk_key_t key, int scancodeIdx, usb_keyboard_report_t *report) {
if (key.type != UHK_KEY_SIMPLE) {
return false;
}
@@ -57,19 +57,19 @@ bool pressKey(uhk_key_t key, int scancodeIdx, usb_keyboard_report_t *report) {
return true;
}
bool key_toggled_on(const uint8_t *prevKeyStates, const uint8_t *currKeyStates, uint8_t keyId) {
static bool key_toggled_on(const uint8_t *prevKeyStates, const uint8_t *currKeyStates, uint8_t keyId) {
return (!prevKeyStates[keyId]) && currKeyStates[keyId];
}
bool key_is_pressed(const uint8_t *prevKeyStates, const uint8_t *currKeyStates, uint8_t keyId) {
static bool key_is_pressed(const uint8_t *prevKeyStates, const uint8_t *currKeyStates, uint8_t keyId) {
return currKeyStates[keyId];
}
bool key_toggled_off(const uint8_t *prevKeyStates, const uint8_t *currKeyStates, uint8_t keyId) {
static bool key_toggled_off(const uint8_t *prevKeyStates, const uint8_t *currKeyStates, uint8_t keyId) {
return (!currKeyStates[keyId]) && prevKeyStates[keyId];
}
bool handleKey(uhk_key_t key, int scancodeIdx, usb_keyboard_report_t *report, const uint8_t *prevKeyStates, const uint8_t *currKeyStates, uint8_t keyId) {
static bool handleKey(uhk_key_t key, int scancodeIdx, usb_keyboard_report_t *report, const uint8_t *prevKeyStates, const uint8_t *currKeyStates, uint8_t keyId) {
switch (key.type) {
case UHK_KEY_SIMPLE:
if (key_is_pressed(prevKeyStates, currKeyStates, keyId)) {
@@ -86,9 +86,6 @@ bool handleKey(uhk_key_t key, int scancodeIdx, usb_keyboard_report_t *report, co
LedDisplay_SetLayerLed(ActiveLayer);
return false;
break;
case UHK_KEY_MOUSE:
fillMouseReport(key, prevKeyStates, currKeyStates, keyId);
return false;
default:
break;
}
@@ -107,7 +104,7 @@ static uint8_t mouseSpeedAccelDivisorCounter = 0;
static uint8_t mouseSpeed = 3;
static bool wasPreviousMouseActionWheelAction = false;
void HandleMouseKey(usb_mouse_report_t *report, uhk_key_t key, const uint8_t *prevKeyStates, const uint8_t *currKeyStates, uint8_t keyId)
static void handleMouseKey(usb_mouse_report_t *report, uhk_key_t key, const uint8_t *prevKeyStates, const uint8_t *currKeyStates, uint8_t keyId)
{
bool isWheelAction;
@@ -169,7 +166,7 @@ void HandleMouseKey(usb_mouse_report_t *report, uhk_key_t key, const uint8_t *pr
wasPreviousMouseActionWheelAction = isWheelAction;
}
void fillKeyboardReport(usb_keyboard_report_t *report, const uint8_t *leftKeyStates, const uint8_t *rightKeyStates) {
void HandleKeyboardEvents(usb_keyboard_report_t *keyboardReport, usb_mouse_report_t *mouseReport, const uint8_t *leftKeyStates, const uint8_t *rightKeyStates) {
int scancodeIdx = 0;
clearKeymasks(leftKeyStates, rightKeyStates);
@@ -181,8 +178,12 @@ void fillKeyboardReport(usb_keyboard_report_t *report, const uint8_t *leftKeySta
uhk_key_t code = getKeycode(SLOT_ID_RIGHT_KEYBOARD_HALF, keyId);
if (handleKey(code, scancodeIdx, report, prevKeyStates[SLOT_ID_RIGHT_KEYBOARD_HALF], rightKeyStates, keyId)) {
scancodeIdx++;
if (code.type == UHK_KEY_MOUSE) {
handleMouseKey(mouseReport, code, prevKeyStates[SLOT_ID_RIGHT_KEYBOARD_HALF], rightKeyStates, keyId);
} else {
if (handleKey(code, scancodeIdx, keyboardReport, prevKeyStates[SLOT_ID_RIGHT_KEYBOARD_HALF], rightKeyStates, keyId)) {
scancodeIdx++;
}
}
}
@@ -193,8 +194,12 @@ void fillKeyboardReport(usb_keyboard_report_t *report, const uint8_t *leftKeySta
uhk_key_t code = getKeycode(SLOT_ID_LEFT_KEYBOARD_HALF, keyId);
if (handleKey(code, scancodeIdx, report, prevKeyStates[SLOT_ID_LEFT_KEYBOARD_HALF], leftKeyStates, keyId)) {
scancodeIdx++;
if (code.type == UHK_KEY_MOUSE) {
handleMouseKey(mouseReport, code, prevKeyStates[SLOT_ID_LEFT_KEYBOARD_HALF], leftKeyStates, keyId);
} else {
if (handleKey(code, scancodeIdx, keyboardReport, prevKeyStates[SLOT_ID_LEFT_KEYBOARD_HALF], leftKeyStates, keyId)) {
scancodeIdx++;
}
}
}

View File

@@ -1,39 +1,105 @@
#ifndef __ACTION_H__
#define __ACTION_H__
#ifndef __UHK_ACTION_H_
#define __UHK_ACTION_H_
// Macros:
#include <stdint.h>
#include "lufa/HIDClassCommon.h"
#include "usb_composite_device.h"
// The value of action ID can be any valid HID_KEYBOARD_SC_* scancode constants of LUFA.
// Hence, ACTION_ID_* values must not conflict with any of the HID_KEYBOARD_SC_* constants.
#define ACTION_ID_NONE 0xFF
#define ACTION_ID_SWITCH_LAYER 0xFE
#define ACTION_ID_MOUSE 0xFD
#define ACTION_ID_SWITCH_KEYMAP 0xFC
#define ACTION_ID_PLAY_MACRO 0xFB
#include "module.h"
#define ACTION_ARG_NONE 0
// Keyboard layout is a 2D array of scan codes.
//
// First dimension is the Key ID of a given key. Key IDs are the indices of the
// of the active keys of the key_matrix_t structure. In case of left half, an
// offset of 35 is added.
//
// For each Key ID, there are 4 different possible scan codes:
// - default, when no modifiers are pressed
// - mod layer
// - fn layer
// - mod+fn layer
#define ACTION_ARG_SWITCH_LAYER_MOD 0
#define ACTION_ARG_SWITCH_LAYER_FN 1
#define ACTION_ARG_SWITCH_LAYER_MOUSE 2
#define KEY_STATE_COUNT (5*7)
#define ACTION_ARG_MOUSE_MOVE_UP 0
#define ACTION_ARG_MOUSE_MOVE_DOWN 1
#define ACTION_ARG_MOUSE_MOVE_LEFT 3
#define ACTION_ARG_MOUSE_MOVE_RIGHT 4
#define ACTION_ARG_MOUSE_CLICK_LEFT 5
#define ACTION_ARG_MOUSE_CLICK_MIDDLE 6
#define ACTION_ARG_MOUSE_CLICK_RIGHT 7
#define ACTION_ARG_MOUSE_WHEEL_UP 8
#define ACTION_ARG_MOUSE_WHEEL_DOWN 9
#define ACTION_ARG_MOUSE_WHEEL_LEFT 10
#define ACTION_ARG_MOUSE_WHEEL_RIGHT 11
typedef enum {
UHK_KEY_NONE,
UHK_KEY_SIMPLE,
UHK_KEY_MOUSE,
UHK_KEY_LAYER,
UHK_KEY_LAYER_TOGGLE,
UHK_KEY_KEYMAP,
UHK_KEY_MACRO,
UHK_KEY_LPRESSMOD,
UHK_KEY_LPRESSLAYER,
} uhk_key_type_t;
// Typedefs:
enum {
UHK_MOUSE_BUTTON_LEFT = (1 << 0),
UHK_MOUSE_BUTTON_RIGHT = (1 << 1),
UHK_MOUSE_BUTTON_MIDDLE = (1 << 2),
UHK_MOUSE_BUTTON_4 = (1 << 3),
UHK_MOUSE_BUTTON_5 = (1 << 4),
UHK_MOUSE_BUTTON_6 = (1 << 5),
};
typedef struct {
uint8_t id;
uint8_t arg;
} action_t;
enum {
UHK_MOUSE_MOVE_UP = (1 << 0),
UHK_MOUSE_MOVE_DOWN = (1 << 1),
UHK_MOUSE_MOVE_LEFT = (1 << 2),
UHK_MOUSE_MOVE_RIGHT = (1 << 3),
UHK_MOUSE_ACCELERATE = (1 << 4),
UHK_MOUSE_DECELERATE = (1 << 5),
};
enum {
UHK_MOUSE_SCROLL_UP = (1 << 0),
UHK_MOUSE_SCROLL_DOWN = (1 << 1),
UHK_MOUSE_SCROLL_LEFT = (1 << 2),
UHK_MOUSE_SCROLL_RIGHT = (1 << 3),
};
typedef struct {
uint8_t type;
union {
struct {
uint8_t __unused_bits;
uint8_t mods;
uint8_t key;
} __attribute__ ((packed)) simple;
struct {
uint8_t buttonActions; // bitfield
uint8_t scrollActions; // bitfield
uint8_t moveActions; // bitfield
} __attribute__ ((packed)) mouse;
struct {
uint16_t __unused_bits;
uint8_t target;
} __attribute__ ((packed)) layer;
struct {
uint16_t __unused_bits;
uint8_t target;
} __attribute__ ((packed)) keymap;
struct {
uint8_t __unused_bits;
uint16_t index;
} __attribute__ ((packed)) macro;
struct {
uint8_t longPressMod; // single mod, or bitfield?
uint8_t mods; // for the alternate action
uint8_t key;
} __attribute__ ((packed)) longpressMod;
struct {
uint8_t longPressLayer;
uint8_t mods;
uint8_t key;
} __attribute__ ((packed)) longpressLayer;
};
} __attribute__ ((packed)) uhk_key_t;
extern uint8_t prevKeyStates[SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE];
extern uhk_key_t CurrentKeymap[LAYER_COUNT][SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE];
void HandleKeyboardEvents(usb_keyboard_report_t *keyboardReport, usb_mouse_report_t *mouseReport, const uint8_t *leftKeyStates, const uint8_t *rightKeyStates);
#endif

View File

@@ -1,4 +1,4 @@
#include "keyboard_layout.h"
#include "action.h"
uhk_key_t CurrentKeymap[LAYER_COUNT][SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE] = {
// Layer 0

View File

@@ -1,107 +0,0 @@
#ifndef KEYBOARD_LAYOUT_H_
#define KEYBOARD_LAYOUT_H_
#include <stdint.h>
#include "lufa/HIDClassCommon.h"
#include "usb_composite_device.h"
#include "module.h"
// Keyboard layout is a 2D array of scan codes.
//
// First dimension is the Key ID of a given key. Key IDs are the indices of the
// of the active keys of the key_matrix_t structure. In case of left half, an
// offset of 35 is added.
//
// For each Key ID, there are 4 different possible scan codes:
// - default, when no modifiers are pressed
// - mod layer
// - fn layer
// - mod+fn layer
#define KEY_STATE_COUNT (5*7)
typedef enum {
UHK_KEY_NONE,
UHK_KEY_SIMPLE,
UHK_KEY_MOUSE,
UHK_KEY_LAYER,
UHK_KEY_LAYER_TOGGLE,
UHK_KEY_KEYMAP,
UHK_KEY_MACRO,
UHK_KEY_LPRESSMOD,
UHK_KEY_LPRESSLAYER,
} uhk_key_type_t;
enum {
UHK_MOUSE_BUTTON_LEFT = (1 << 0),
UHK_MOUSE_BUTTON_RIGHT = (1 << 1),
UHK_MOUSE_BUTTON_MIDDLE = (1 << 2),
UHK_MOUSE_BUTTON_4 = (1 << 3),
UHK_MOUSE_BUTTON_5 = (1 << 4),
UHK_MOUSE_BUTTON_6 = (1 << 5),
};
enum {
UHK_MOUSE_MOVE_UP = (1 << 0),
UHK_MOUSE_MOVE_DOWN = (1 << 1),
UHK_MOUSE_MOVE_LEFT = (1 << 2),
UHK_MOUSE_MOVE_RIGHT = (1 << 3),
UHK_MOUSE_ACCELERATE = (1 << 4),
UHK_MOUSE_DECELERATE = (1 << 5),
};
enum {
UHK_MOUSE_SCROLL_UP = (1 << 0),
UHK_MOUSE_SCROLL_DOWN = (1 << 1),
UHK_MOUSE_SCROLL_LEFT = (1 << 2),
UHK_MOUSE_SCROLL_RIGHT = (1 << 3),
};
typedef struct {
uint8_t type;
union {
struct {
uint8_t __unused_bits;
uint8_t mods;
uint8_t key;
} __attribute__ ((packed)) simple;
struct {
uint8_t buttonActions; // bitfield
uint8_t scrollActions; // bitfield
uint8_t moveActions; // bitfield
} __attribute__ ((packed)) mouse;
struct {
uint16_t __unused_bits;
uint8_t target;
} __attribute__ ((packed)) layer;
struct {
uint16_t __unused_bits;
uint8_t target;
} __attribute__ ((packed)) keymap;
struct {
uint8_t __unused_bits;
uint16_t index;
} __attribute__ ((packed)) macro;
struct {
uint8_t longPressMod; // single mod, or bitfield?
uint8_t mods; // for the alternate action
uint8_t key;
} __attribute__ ((packed)) longpressMod;
struct {
uint8_t longPressLayer;
uint8_t mods;
uint8_t key;
} __attribute__ ((packed)) longpressLayer;
};
} __attribute__ ((packed)) uhk_key_t;
extern uint8_t prevKeyStates[SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE];
extern uhk_key_t CurrentKeymap[LAYER_COUNT][SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE];
void fillKeyboardReport(usb_keyboard_report_t *report, const uint8_t *leftKeyStates, const uint8_t *rightKeyStates);
void HandleMouseKey(usb_mouse_report_t *report, uhk_key_t key, const uint8_t *prevKeyStates, const uint8_t *currKeyStates, uint8_t keyId);
extern void fillMouseReport(uhk_key_t key, const uint8_t *prevKeyStates, const uint8_t *currKeyStates, uint8_t keyId);
#endif

View File

@@ -3,7 +3,6 @@
// Includes:
#include "action.h"
#include "layer.h"
#include "slot.h"

View File

@@ -1,3 +1,4 @@
#include "action.h"
#include "fsl_port.h"
#include "usb_api.h"
#include "usb_composite_device.h"
@@ -6,7 +7,6 @@
#include "fsl_i2c.h"
#include "i2c.h"
#include "i2c_addresses.h"
#include "keyboard_layout.h"
static usb_device_endpoint_struct_t UsbKeyboardEndpoints[USB_KEYBOARD_ENDPOINT_COUNT] = {{
USB_KEYBOARD_ENDPOINT_INDEX | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT),
@@ -100,7 +100,7 @@ void UsbKeyboadTask(){
readLeftKeys(leftKeyStates);
fillKeyboardReport(&UsbKeyboardReport[newLayout], leftKeyStates, keyMatrix.keyStates);
HandleKeyboardEvents(&UsbKeyboardReport[newLayout], &UsbMouseReport, leftKeyStates, keyMatrix.keyStates);
// Change to the new layout in atomic operation (int copy). Even if
// the copy is not atomic itself, only single bit changes. So it can

View File

@@ -3,7 +3,7 @@
#include "fsl_i2c.h"
#include "i2c.h"
#include "reset_button.h"
#include "keyboard_layout.h"
#include "action.h"
static usb_device_endpoint_struct_t UsbMouseEndpoints[USB_MOUSE_ENDPOINT_COUNT] = {{
USB_MOUSE_ENDPOINT_INDEX | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT),
@@ -37,7 +37,7 @@ usb_device_class_struct_t UsbMouseClass = {
USB_DEVICE_CONFIGURATION_COUNT,
};
static usb_mouse_report_t UsbMouseReport;
usb_mouse_report_t UsbMouseReport;
static volatile usb_status_t UsbMouseAction(void)
{

View File

@@ -38,4 +38,6 @@
extern usb_status_t UsbMouseSetConfiguration(class_handle_t handle, uint8_t configuration);
extern usb_status_t UsbMouseSetInterface(class_handle_t handle, uint8_t interface, uint8_t alternateSetting);
extern usb_mouse_report_t UsbMouseReport;
#endif