mirror of
https://github.com/Keychron/qmk_firmware.git
synced 2024-11-23 17:06:52 +06:00
Add dynamic macro keyboard callbacks (#24142)
This commit is contained in:
parent
faf51c7449
commit
ab4c13e835
|
@ -382,15 +382,23 @@ static bool is_on = false;
|
|||
static bool is_dynamic_recording = false;
|
||||
static uint16_t dynamic_loop_timer;
|
||||
|
||||
void dynamic_macro_record_start_user(int8_t direction) {
|
||||
bool dynamic_macro_record_start_kb(int8_t direction) {
|
||||
if (!dynamic_macro_record_start_user(direction)) {
|
||||
return false;
|
||||
}
|
||||
is_dynamic_recording = true;
|
||||
dynamic_loop_timer = timer_read();
|
||||
ergodox_right_led_1_on();
|
||||
return true;
|
||||
}
|
||||
|
||||
void dynamic_macro_record_end_user(int8_t direction) {
|
||||
bool dynamic_macro_record_end_kb(int8_t direction) {
|
||||
if (!dynamic_macro_record_end_user(direction)) {
|
||||
return false;
|
||||
}
|
||||
is_dynamic_recording = false;
|
||||
layer_state_set_user(layer_state);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
|
@ -206,11 +206,19 @@ void update_layer_leds(void) {
|
|||
|
||||
#endif
|
||||
|
||||
void dynamic_macro_record_start_user(int8_t direction) {
|
||||
bool dynamic_macro_record_start_kb(int8_t direction) {
|
||||
if (!dynamic_macro_record_start_user(direction)) {
|
||||
return false;
|
||||
}
|
||||
isRecording++;
|
||||
blink_cycle_timer = timer_read();
|
||||
return true;
|
||||
}
|
||||
|
||||
void dynamic_macro_record_end_user(int8_t direction) {
|
||||
if (isRecording) isRecording--;
|
||||
bool dynamic_macro_record_end_kb(int8_t direction) {
|
||||
if (!dynamic_macro_record_end_user(direction)) {
|
||||
return false;
|
||||
}
|
||||
if (isRecording) isRecording--;
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -26,11 +26,21 @@ bool is_launching = false;
|
|||
#ifdef DYNAMIC_MACRO_ENABLE
|
||||
static bool is_dynamic_recording = false;
|
||||
|
||||
void dynamic_macro_record_start_user(int8_t direction) { is_dynamic_recording = true; }
|
||||
bool dynamic_macro_record_start_kb(int8_t direction) {
|
||||
if (!dynamic_macro_record_start_user(direction)) {
|
||||
return false;
|
||||
}
|
||||
is_dynamic_recording = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void dynamic_macro_record_end_user(int8_t direction) {
|
||||
bool dynamic_macro_record_end_kb(int8_t direction) {
|
||||
if (!dynamic_macro_record_end_user(direction)) {
|
||||
return false;
|
||||
}
|
||||
is_dynamic_recording = false;
|
||||
ML_LED_3(false);
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
|
@ -21,18 +21,26 @@ static uint32_t dynamic_macro_led(uint32_t trigger_time, void *cb_arg) {
|
|||
return 100;
|
||||
}
|
||||
|
||||
void dynamic_macro_record_start_user(int8_t direction) {
|
||||
bool dynamic_macro_record_start_kb(int8_t direction) {
|
||||
if (!dynamic_macro_record_start_user(direction)) {
|
||||
return false;
|
||||
}
|
||||
if (dynamic_macro_token == INVALID_DEFERRED_TOKEN) {
|
||||
STATUS_LED_3(true);
|
||||
dynamic_macro_token = defer_exec(100, dynamic_macro_led, NULL);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void dynamic_macro_record_end_user(int8_t direction) {
|
||||
bool dynamic_macro_record_end_kb(int8_t direction) {
|
||||
if (!dynamic_macro_record_end_user(direction)) {
|
||||
return false;
|
||||
}
|
||||
if (cancel_deferred_exec(dynamic_macro_token)) {
|
||||
dynamic_macro_token = INVALID_DEFERRED_TOKEN;
|
||||
STATUS_LED_3(false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
# endif
|
||||
|
||||
|
|
|
@ -1,264 +0,0 @@
|
|||
/* Copyright 2016 Jack Humbert
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/* Author: Wojciech Siewierski < wojciech dot siewierski at onet dot pl > */
|
||||
#pragma once
|
||||
|
||||
/* Warn users that this is now deprecated and they should use the core feature instead. */
|
||||
#pragma message "Dynamic Macros is now a core feature. See updated documentation to see how to configure it: https://docs.qmk.fm/#/feature_dynamic_macros"
|
||||
|
||||
#include "action_layer.h"
|
||||
|
||||
#ifndef DYNAMIC_MACRO_SIZE
|
||||
/* May be overridden with a custom value. Be aware that the effective
|
||||
* macro length is half of this value: each keypress is recorded twice
|
||||
* because of the down-event and up-event. This is not a bug, it's the
|
||||
* intended behavior.
|
||||
*
|
||||
* Usually it should be fine to set the macro size to at least 256 but
|
||||
* there have been reports of it being too much in some users' cases,
|
||||
* so 128 is considered a safe default.
|
||||
*/
|
||||
# define DYNAMIC_MACRO_SIZE 128
|
||||
#endif
|
||||
|
||||
/* Blink the LEDs to notify the user about some event. */
|
||||
void dynamic_macro_led_blink(void) {
|
||||
#ifdef BACKLIGHT_ENABLE
|
||||
backlight_toggle();
|
||||
wait_ms(100);
|
||||
backlight_toggle();
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Convenience macros used for retrieving the debug info. All of them
|
||||
* need a `direction` variable accessible at the call site.
|
||||
*/
|
||||
#define DYNAMIC_MACRO_CURRENT_SLOT() (direction > 0 ? 1 : 2)
|
||||
#define DYNAMIC_MACRO_CURRENT_LENGTH(BEGIN, POINTER) ((int)(direction * ((POINTER) - (BEGIN))))
|
||||
#define DYNAMIC_MACRO_CURRENT_CAPACITY(BEGIN, END2) ((int)(direction * ((END2) - (BEGIN)) + 1))
|
||||
|
||||
/**
|
||||
* Start recording of the dynamic macro.
|
||||
*
|
||||
* @param[out] macro_pointer The new macro buffer iterator.
|
||||
* @param[in] macro_buffer The macro buffer used to initialize macro_pointer.
|
||||
*/
|
||||
void dynamic_macro_record_start(keyrecord_t **macro_pointer, keyrecord_t *macro_buffer) {
|
||||
dprintln("dynamic macro recording: started");
|
||||
|
||||
dynamic_macro_led_blink();
|
||||
|
||||
clear_keyboard();
|
||||
layer_clear();
|
||||
*macro_pointer = macro_buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Play the dynamic macro.
|
||||
*
|
||||
* @param macro_buffer[in] The beginning of the macro buffer being played.
|
||||
* @param macro_end[in] The element after the last macro buffer element.
|
||||
* @param direction[in] Either +1 or -1, which way to iterate the buffer.
|
||||
*/
|
||||
void dynamic_macro_play(keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction) {
|
||||
dprintf("dynamic macro: slot %d playback\n", DYNAMIC_MACRO_CURRENT_SLOT());
|
||||
|
||||
uint32_t saved_layer_state = layer_state;
|
||||
|
||||
clear_keyboard();
|
||||
layer_clear();
|
||||
|
||||
while (macro_buffer != macro_end) {
|
||||
process_record(macro_buffer);
|
||||
macro_buffer += direction;
|
||||
}
|
||||
|
||||
clear_keyboard();
|
||||
|
||||
layer_state = saved_layer_state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Record a single key in a dynamic macro.
|
||||
*
|
||||
* @param macro_buffer[in] The start of the used macro buffer.
|
||||
* @param macro_pointer[in,out] The current buffer position.
|
||||
* @param macro2_end[in] The end of the other macro.
|
||||
* @param direction[in] Either +1 or -1, which way to iterate the buffer.
|
||||
* @param record[in] The current keypress.
|
||||
*/
|
||||
void dynamic_macro_record_key(keyrecord_t *macro_buffer, keyrecord_t **macro_pointer, keyrecord_t *macro2_end, int8_t direction, keyrecord_t *record) {
|
||||
/* If we've just started recording, ignore all the key releases. */
|
||||
if (!record->event.pressed && *macro_pointer == macro_buffer) {
|
||||
dprintln("dynamic macro: ignoring a leading key-up event");
|
||||
return;
|
||||
}
|
||||
|
||||
/* The other end of the other macro is the last buffer element it
|
||||
* is safe to use before overwriting the other macro.
|
||||
*/
|
||||
if (*macro_pointer - direction != macro2_end) {
|
||||
**macro_pointer = *record;
|
||||
*macro_pointer += direction;
|
||||
} else {
|
||||
dynamic_macro_led_blink();
|
||||
}
|
||||
|
||||
dprintf("dynamic macro: slot %d length: %d/%d\n", DYNAMIC_MACRO_CURRENT_SLOT(), DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, *macro_pointer), DYNAMIC_MACRO_CURRENT_CAPACITY(macro_buffer, macro2_end));
|
||||
}
|
||||
|
||||
/**
|
||||
* End recording of the dynamic macro. Essentially just update the
|
||||
* pointer to the end of the macro.
|
||||
*/
|
||||
void dynamic_macro_record_end(keyrecord_t *macro_buffer, keyrecord_t *macro_pointer, int8_t direction, keyrecord_t **macro_end) {
|
||||
dynamic_macro_led_blink();
|
||||
|
||||
/* Do not save the keys being held when stopping the recording,
|
||||
* i.e. the keys used to access the layer DM_RSTP is on.
|
||||
*/
|
||||
while (macro_pointer != macro_buffer && (macro_pointer - direction)->event.pressed) {
|
||||
dprintln("dynamic macro: trimming a trailing key-down event");
|
||||
macro_pointer -= direction;
|
||||
}
|
||||
|
||||
dprintf("dynamic macro: slot %d saved, length: %d\n", DYNAMIC_MACRO_CURRENT_SLOT(), DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, macro_pointer));
|
||||
|
||||
*macro_end = macro_pointer;
|
||||
}
|
||||
|
||||
/* Handle the key events related to the dynamic macros. Should be
|
||||
* called from process_record_user() like this:
|
||||
*
|
||||
* bool process_record_user(uint16_t keycode, keyrecord_t *record) {
|
||||
* if (!process_record_dynamic_macro(keycode, record)) {
|
||||
* return false;
|
||||
* }
|
||||
* <...THE REST OF THE FUNCTION...>
|
||||
* }
|
||||
*/
|
||||
bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record) {
|
||||
/* Both macros use the same buffer but read/write on different
|
||||
* ends of it.
|
||||
*
|
||||
* Macro1 is written left-to-right starting from the beginning of
|
||||
* the buffer.
|
||||
*
|
||||
* Macro2 is written right-to-left starting from the end of the
|
||||
* buffer.
|
||||
*
|
||||
* ¯o_buffer macro_end
|
||||
* v v
|
||||
* +------------------------------------------------------------+
|
||||
* |>>>>>> MACRO1 >>>>>> <<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
|
||||
* +------------------------------------------------------------+
|
||||
* ^ ^
|
||||
* r_macro_end r_macro_buffer
|
||||
*
|
||||
* During the recording when one macro encounters the end of the
|
||||
* other macro, the recording is stopped. Apart from this, there
|
||||
* are no arbitrary limits for the macros' length in relation to
|
||||
* each other: for example one can either have two medium sized
|
||||
* macros or one long macro and one short macro. Or even one empty
|
||||
* and one using the whole buffer.
|
||||
*/
|
||||
static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE];
|
||||
|
||||
/* Pointer to the first buffer element after the first macro.
|
||||
* Initially points to the very beginning of the buffer since the
|
||||
* macro is empty. */
|
||||
static keyrecord_t *macro_end = macro_buffer;
|
||||
|
||||
/* The other end of the macro buffer. Serves as the beginning of
|
||||
* the second macro. */
|
||||
static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1;
|
||||
|
||||
/* Like macro_end but for the second macro. */
|
||||
static keyrecord_t *r_macro_end = r_macro_buffer;
|
||||
|
||||
/* A persistent pointer to the current macro position (iterator)
|
||||
* used during the recording. */
|
||||
static keyrecord_t *macro_pointer = NULL;
|
||||
|
||||
/* 0 - no macro is being recorded right now
|
||||
* 1,2 - either macro 1 or 2 is being recorded */
|
||||
static uint8_t macro_id = 0;
|
||||
|
||||
if (macro_id == 0) {
|
||||
/* No macro recording in progress. */
|
||||
if (!record->event.pressed) {
|
||||
switch (keycode) {
|
||||
case QK_DYNAMIC_MACRO_RECORD_START_1:
|
||||
dynamic_macro_record_start(¯o_pointer, macro_buffer);
|
||||
macro_id = 1;
|
||||
return false;
|
||||
case QK_DYNAMIC_MACRO_RECORD_START_2:
|
||||
dynamic_macro_record_start(¯o_pointer, r_macro_buffer);
|
||||
macro_id = 2;
|
||||
return false;
|
||||
case QK_DYNAMIC_MACRO_PLAY_1:
|
||||
dynamic_macro_play(macro_buffer, macro_end, +1);
|
||||
return false;
|
||||
case QK_DYNAMIC_MACRO_PLAY_2:
|
||||
dynamic_macro_play(r_macro_buffer, r_macro_end, -1);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* A macro is being recorded right now. */
|
||||
switch (keycode) {
|
||||
case QK_DYNAMIC_MACRO_RECORD_STOP:
|
||||
/* Stop the macro recording. */
|
||||
if (record->event.pressed) { /* Ignore the initial release
|
||||
* just after the recoding
|
||||
* starts. */
|
||||
switch (macro_id) {
|
||||
case 1:
|
||||
dynamic_macro_record_end(macro_buffer, macro_pointer, +1, ¯o_end);
|
||||
break;
|
||||
case 2:
|
||||
dynamic_macro_record_end(r_macro_buffer, macro_pointer, -1, &r_macro_end);
|
||||
break;
|
||||
}
|
||||
macro_id = 0;
|
||||
}
|
||||
return false;
|
||||
case QK_DYNAMIC_MACRO_PLAY_1:
|
||||
case QK_DYNAMIC_MACRO_PLAY_2:
|
||||
dprintln("dynamic macro: ignoring macro play key while recording");
|
||||
return false;
|
||||
default:
|
||||
/* Store the key in the macro buffer and process it normally. */
|
||||
switch (macro_id) {
|
||||
case 1:
|
||||
dynamic_macro_record_key(macro_buffer, ¯o_pointer, r_macro_end, +1, record);
|
||||
break;
|
||||
case 2:
|
||||
dynamic_macro_record_key(r_macro_buffer, ¯o_pointer, macro_end, -1, record);
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#undef DYNAMIC_MACRO_CURRENT_SLOT
|
||||
#undef DYNAMIC_MACRO_CURRENT_LENGTH
|
||||
#undef DYNAMIC_MACRO_CURRENT_CAPACITY
|
|
@ -38,20 +38,44 @@ void dynamic_macro_led_blink(void) {
|
|||
|
||||
/* User hooks for Dynamic Macros */
|
||||
|
||||
__attribute__((weak)) void dynamic_macro_record_start_user(int8_t direction) {
|
||||
dynamic_macro_led_blink();
|
||||
__attribute__((weak)) bool dynamic_macro_record_start_kb(int8_t direction) {
|
||||
return dynamic_macro_record_start_user(direction);
|
||||
}
|
||||
|
||||
__attribute__((weak)) void dynamic_macro_play_user(int8_t direction) {
|
||||
__attribute__((weak)) bool dynamic_macro_record_start_user(int8_t direction) {
|
||||
dynamic_macro_led_blink();
|
||||
return true;
|
||||
}
|
||||
|
||||
__attribute__((weak)) void dynamic_macro_record_key_user(int8_t direction, keyrecord_t *record) {
|
||||
dynamic_macro_led_blink();
|
||||
__attribute__((weak)) bool dynamic_macro_play_kb(int8_t direction) {
|
||||
return dynamic_macro_play_user(direction);
|
||||
}
|
||||
|
||||
__attribute__((weak)) void dynamic_macro_record_end_user(int8_t direction) {
|
||||
__attribute__((weak)) bool dynamic_macro_play_user(int8_t direction) {
|
||||
dynamic_macro_led_blink();
|
||||
return true;
|
||||
}
|
||||
|
||||
__attribute__((weak)) bool dynamic_macro_record_key_kb(int8_t direction, keyrecord_t *record) {
|
||||
return dynamic_macro_record_key_user(direction, record);
|
||||
}
|
||||
|
||||
__attribute__((weak)) bool dynamic_macro_record_key_user(int8_t direction, keyrecord_t *record) {
|
||||
dynamic_macro_led_blink();
|
||||
return true;
|
||||
}
|
||||
|
||||
__attribute__((weak)) bool dynamic_macro_record_end_kb(int8_t direction) {
|
||||
return dynamic_macro_record_end_user(direction);
|
||||
}
|
||||
|
||||
__attribute__((weak)) bool dynamic_macro_record_end_user(int8_t direction) {
|
||||
dynamic_macro_led_blink();
|
||||
return true;
|
||||
}
|
||||
|
||||
__attribute__((weak)) bool dynamic_macro_valid_key_kb(uint16_t keycode, keyrecord_t *record) {
|
||||
return dynamic_macro_valid_key_user(keycode, record);
|
||||
}
|
||||
|
||||
__attribute__((weak)) bool dynamic_macro_valid_key_user(uint16_t keycode, keyrecord_t *record) {
|
||||
|
@ -74,7 +98,7 @@ __attribute__((weak)) bool dynamic_macro_valid_key_user(uint16_t keycode, keyrec
|
|||
void dynamic_macro_record_start(keyrecord_t **macro_pointer, keyrecord_t *macro_buffer, int8_t direction) {
|
||||
dprintln("dynamic macro recording: started");
|
||||
|
||||
dynamic_macro_record_start_user(direction);
|
||||
dynamic_macro_record_start_kb(direction);
|
||||
|
||||
clear_keyboard();
|
||||
layer_clear();
|
||||
|
@ -108,7 +132,7 @@ void dynamic_macro_play(keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_
|
|||
|
||||
layer_state_set(saved_layer_state);
|
||||
|
||||
dynamic_macro_play_user(direction);
|
||||
dynamic_macro_play_kb(direction);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -134,7 +158,7 @@ void dynamic_macro_record_key(keyrecord_t *macro_buffer, keyrecord_t **macro_poi
|
|||
**macro_pointer = *record;
|
||||
*macro_pointer += direction;
|
||||
}
|
||||
dynamic_macro_record_key_user(direction, record);
|
||||
dynamic_macro_record_key_kb(direction, record);
|
||||
|
||||
dprintf("dynamic macro: slot %d length: %d/%d\n", DYNAMIC_MACRO_CURRENT_SLOT(), DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, *macro_pointer), DYNAMIC_MACRO_CURRENT_CAPACITY(macro_buffer, macro2_end));
|
||||
}
|
||||
|
@ -144,7 +168,7 @@ void dynamic_macro_record_key(keyrecord_t *macro_buffer, keyrecord_t **macro_poi
|
|||
* pointer to the end of the macro.
|
||||
*/
|
||||
void dynamic_macro_record_end(keyrecord_t *macro_buffer, keyrecord_t *macro_pointer, int8_t direction, keyrecord_t **macro_end) {
|
||||
dynamic_macro_record_end_user(direction);
|
||||
dynamic_macro_record_end_kb(direction);
|
||||
|
||||
/* Do not save the keys being held when stopping the recording,
|
||||
* i.e. the keys used to access the layer DM_RSTP is on.
|
||||
|
@ -220,15 +244,7 @@ void dynamic_macro_stop_recording(void) {
|
|||
macro_id = 0;
|
||||
}
|
||||
|
||||
/* Handle the key events related to the dynamic macros. Should be
|
||||
* called from process_record_user() like this:
|
||||
*
|
||||
* bool process_record_user(uint16_t keycode, keyrecord_t *record) {
|
||||
* if (!process_record_dynamic_macro(keycode, record)) {
|
||||
* return false;
|
||||
* }
|
||||
* <...THE REST OF THE FUNCTION...>
|
||||
* }
|
||||
/* Handle the key events related to the dynamic macros.
|
||||
*/
|
||||
bool process_dynamic_macro(uint16_t keycode, keyrecord_t *record) {
|
||||
if (macro_id == 0) {
|
||||
|
@ -271,7 +287,7 @@ bool process_dynamic_macro(uint16_t keycode, keyrecord_t *record) {
|
|||
return false;
|
||||
#endif
|
||||
default:
|
||||
if (dynamic_macro_valid_key_user(keycode, record)) {
|
||||
if (dynamic_macro_valid_key_kb(keycode, record)) {
|
||||
/* Store the key in the macro buffer and process it normally. */
|
||||
switch (macro_id) {
|
||||
case 1:
|
||||
|
|
|
@ -37,8 +37,14 @@
|
|||
|
||||
void dynamic_macro_led_blink(void);
|
||||
bool process_dynamic_macro(uint16_t keycode, keyrecord_t *record);
|
||||
void dynamic_macro_record_start_user(int8_t direction);
|
||||
void dynamic_macro_play_user(int8_t direction);
|
||||
void dynamic_macro_record_key_user(int8_t direction, keyrecord_t *record);
|
||||
void dynamic_macro_record_end_user(int8_t direction);
|
||||
bool dynamic_macro_record_start_kb(int8_t direction);
|
||||
bool dynamic_macro_record_start_user(int8_t direction);
|
||||
bool dynamic_macro_play_kb(int8_t direction);
|
||||
bool dynamic_macro_play_user(int8_t direction);
|
||||
bool dynamic_macro_record_key_kb(int8_t direction, keyrecord_t *record);
|
||||
bool dynamic_macro_record_key_user(int8_t direction, keyrecord_t *record);
|
||||
bool dynamic_macro_record_end_kb(int8_t direction);
|
||||
bool dynamic_macro_record_end_user(int8_t direction);
|
||||
bool dynamic_macro_valid_key_kb(uint16_t keycode, keyrecord_t *record);
|
||||
bool dynamic_macro_valid_key_user(uint16_t keycode, keyrecord_t *record);
|
||||
void dynamic_macro_stop_recording(void);
|
||||
|
|
Loading…
Reference in New Issue
Block a user