qmk_settings: initial prototype

This commit is contained in:
Ilya Zhuravlev 2021-06-29 19:43:11 -04:00
parent c0cd1bfc6c
commit 8dc2d07288
6 changed files with 143 additions and 8 deletions

View File

@ -476,6 +476,11 @@ ifeq ($(strip $(DYNAMIC_KEYMAP_ENABLE)), yes)
SRC += $(QUANTUM_DIR)/dynamic_keymap.c
endif
ifeq ($(strip $(QMK_SETTINGS)), yes)
SRC += $(QUANTUM_DIR)/qmk_settings.c
OPT_DEFS += -DQMK_SETTINGS
endif
ifeq ($(strip $(DIP_SWITCH_ENABLE)), yes)
OPT_DEFS += -DDIP_SWITCH_ENABLE
SRC += $(QUANTUM_DIR)/dip_switch.c

View File

@ -14,6 +14,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "process_grave_esc.h"
#include "qmk_settings.h"
/* true if the last press of GRAVE_ESC was shifted (i.e. GUI or SHIFT were pressed), false otherwise.
* Used to ensure that the correct keycode is released if the key is released.
@ -25,35 +26,35 @@ bool process_grave_esc(uint16_t keycode, keyrecord_t *record) {
const uint8_t mods = get_mods();
uint8_t shifted = mods & MOD_MASK_SG;
#ifdef GRAVE_ESC_ALT_OVERRIDE
if (QS_grave_esc_alt_override) {
// if ALT is pressed, ESC is always sent
// this is handy for the cmd+opt+esc shortcut on macOS, among other things.
if (mods & MOD_MASK_ALT) {
shifted = 0;
}
#endif
}
#ifdef GRAVE_ESC_CTRL_OVERRIDE
if (QS_grave_esc_ctrl_override) {
// if CTRL is pressed, ESC is always sent
// this is handy for the ctrl+shift+esc shortcut on windows, among other things.
if (mods & MOD_MASK_CTRL) {
shifted = 0;
}
#endif
}
#ifdef GRAVE_ESC_GUI_OVERRIDE
if (QS_grave_esc_gui_override) {
// if GUI is pressed, ESC is always sent
if (mods & MOD_MASK_GUI) {
shifted = 0;
}
#endif
}
#ifdef GRAVE_ESC_SHIFT_OVERRIDE
if (QS_grave_esc_shift_override) {
// if SHIFT is pressed, ESC is always sent
if (mods & MOD_MASK_SHIFT) {
shifted = 0;
}
#endif
}
if (record->event.pressed) {
grave_esc_was_shifted = shifted;

36
quantum/qmk_settings.c Normal file
View File

@ -0,0 +1,36 @@
#include "qmk_settings.h"
#include <stddef.h>
#include "progmem.h"
#include <string.h>
qmk_settings_t QS;
#define DECLARE_SETTING(id, field) { .qsid=id, .ptr=&QS.field, .sz=sizeof(QS.field) }
static const qmk_settings_proto_t protos[] PROGMEM = {
DECLARE_SETTING(1, grave_esc_override),
};
static const qmk_settings_proto_t *find_setting(uint16_t qsid) {
for (size_t i = 0; i < sizeof(protos)/sizeof(*protos); ++i)
if (pgm_read_word(&protos[i].qsid) == qsid)
return &protos[i];
return NULL;
}
int qmk_settings_get(uint16_t qsid, void *setting, size_t maxsz) {
const qmk_settings_proto_t *proto = find_setting(qsid);
if (!proto || pgm_read_word(&proto->sz) > maxsz)
return -1;
memcpy(setting, pgm_read_ptr(&proto->ptr), pgm_read_word(&proto->sz));
return 0;
}
int qmk_settings_set(uint16_t qsid, const void *setting, size_t maxsz) {
const qmk_settings_proto_t *proto = find_setting(qsid);
if (!proto || pgm_read_word(&proto->sz) > maxsz)
return -1;
memcpy(pgm_read_ptr(&proto->ptr), setting, pgm_read_word(&proto->sz));
return 0;
}

71
quantum/qmk_settings.h Normal file
View File

@ -0,0 +1,71 @@
#pragma once
#include <inttypes.h>
#include <stddef.h>
/* take qmk config macros and set up helper variables for default settings */
/* ========================================================================== */
/* Grave escape */
/* ========================================================================== */
#ifdef GRAVE_ESC_ALT_OVERRIDE
#define GRAVE_ESC_ALT_OVERRIDE_Defined 1
#else
#define GRAVE_ESC_ALT_OVERRIDE_Defined 0
#endif
#ifdef GRAVE_ESC_CTRL_OVERRIDE
#define GRAVE_ESC_CTRL_OVERRIDE_Defined 1
#else
#define GRAVE_ESC_CTRL_OVERRIDE_Defined 0
#endif
#ifdef GRAVE_ESC_GUI_OVERRIDE
#define GRAVE_ESC_GUI_OVERRIDE_Defined 1
#else
#define GRAVE_ESC_GUI_OVERRIDE_Defined 0
#endif
#ifdef GRAVE_ESC_SHIFT_OVERRIDE
#define GRAVE_ESC_SHIFT_OVERRIDE_Defined 1
#else
#define GRAVE_ESC_SHIFT_OVERRIDE_Defined 0
#endif
#ifdef QMK_SETTINGS
/* dynamic settings framework is enabled */
/* actual settings - stored in RAM and backed by EEPROM */
typedef struct {
uint8_t grave_esc_override;
} qmk_settings_t;
/* setting prototype - describes how to get/set settings, stored in flash */
typedef struct {
uint16_t qsid;
uint16_t sz;
void *ptr;
} qmk_settings_proto_t;
int qmk_settings_get(uint16_t qsid, void *setting, size_t maxsz);
int qmk_settings_set(uint16_t qsid, const void *setting, size_t maxsz);
extern qmk_settings_t QS;
/* Grave escape */
#define QS_grave_esc_alt_override (QS.grave_esc_override & 1)
#define QS_grave_esc_ctrl_override (QS.grave_esc_override & 2)
#define QS_grave_esc_gui_override (QS.grave_esc_override & 4)
#define QS_grave_esc_shift_override (QS.grave_esc_override & 8)
#else
/* dynamic settings framework is disabled => hardcode the settings and let the compiler optimize extra branches out */
/* Grave escape */
#define QS_grave_esc_alt_override GRAVE_ESC_ALT_OVERRIDE_Defined
#define QS_grave_esc_ctrl_override GRAVE_ESC_CTRL_OVERRIDE_Defined
#define QS_grave_esc_gui_override GRAVE_ESC_GUI_OVERRIDE_Defined
#define QS_grave_esc_shift_override GRAVE_ESC_SHIFT_OVERRIDE_Defined
#endif

View File

@ -50,6 +50,10 @@ _Static_assert(sizeof(vial_unlock_combo_rows) == sizeof(vial_unlock_combo_cols),
#define VIAL_ENCODER_KEYCODE_DELAY 10
#endif
#ifdef QMK_SETTINGS
#include "qmk_settings.h"
#endif
void vial_handle_cmd(uint8_t *msg, uint8_t length) {
/* All packets must be fixed 32 bytes */
if (length != VIAL_RAW_EPSIZE)
@ -160,6 +164,21 @@ void vial_handle_cmd(uint8_t *msg, uint8_t length) {
#endif
break;
}
case vial_qmk_settings_query: {
break;
}
case vial_qmk_settings_get: {
uint16_t qsid = msg[2] | (msg[3] << 8);
msg[0] = qmk_settings_get(qsid, &msg[1], length - 1);
break;
}
case vial_qmk_settings_set: {
uint16_t qsid = msg[2] | (msg[3] << 8);
msg[0] = qmk_settings_set(qsid, &msg[4], length - 4);
break;
}
}
}

View File

@ -40,6 +40,9 @@ enum {
vial_unlock_start = 0x06,
vial_unlock_poll = 0x07,
vial_lock = 0x08,
vial_qmk_settings_query = 0x09,
vial_qmk_settings_get = 0x0A,
vial_qmk_settings_set = 0x0B,
};
/* Fake encoder position in keyboard matrix, can't use 255 as that is immediately rejected by IS_NOEVENT */