Skip to content

Commit

Permalink
qmk_settings: allow arbitrary getters and setters
Browse files Browse the repository at this point in the history
  • Loading branch information
xyzz committed May 22, 2022
1 parent b8c1cf4 commit ab0bff8
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 49 deletions.
123 changes: 78 additions & 45 deletions quantum/qmk_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
#include "process_combo.h"
#include "action_tapping.h"

static int eeprom_settings_get(const qmk_settings_proto_t *proto, void *setting, size_t maxsz);
static int eeprom_settings_set(const qmk_settings_proto_t *proto, const void *setting, size_t maxsz);

qmk_settings_t QS;

#define DECLARE_SETTING(id, field) { .qsid=id, .ptr=&QS.field, .sz=sizeof(QS.field) }
#define DECLARE_SETTING_CB(id, field, callback) { .qsid=id, .ptr=&QS.field, .sz=sizeof(QS.field), .cb=callback }
#define DECLARE_SETTING_NOTIFY(id, _get, _set, _notify) { .qsid=id, .get=_get, .set=_set, .notify=_notify }
#define DECLARE_SETTING(id, _get, _set) DECLARE_SETTING_NOTIFY(id, _get, _set, NULL)
#define DECLARE_STATIC_SETTING_NOTIFY(id, field, notify_) { .qsid=id, .ptr=&QS.field, .sz=sizeof(QS.field), .get=eeprom_settings_get, .set=eeprom_settings_set, .notify=notify_ }
#define DECLARE_STATIC_SETTING(id, field) DECLARE_STATIC_SETTING_NOTIFY(id, field, NULL)

static void auto_shift_timeout_apply(void) {
set_autoshift_timeout(QS.auto_shift_timeout);
Expand All @@ -33,46 +38,39 @@ static void mousekey_apply(void) {
#endif

static const qmk_settings_proto_t protos[] PROGMEM = {
DECLARE_SETTING(1, grave_esc_override),
DECLARE_SETTING(2, combo_term),
DECLARE_SETTING(3, auto_shift),
DECLARE_SETTING_CB(4, auto_shift_timeout, auto_shift_timeout_apply),
DECLARE_SETTING(5, osk_tap_toggle),
DECLARE_SETTING(6, osk_timeout),
DECLARE_SETTING(7, tapping_term),
DECLARE_SETTING(8, tapping),
DECLARE_STATIC_SETTING(1, grave_esc_override),
DECLARE_STATIC_SETTING(2, combo_term),
DECLARE_STATIC_SETTING(3, auto_shift),
DECLARE_STATIC_SETTING_NOTIFY(4, auto_shift_timeout, auto_shift_timeout_apply),
DECLARE_STATIC_SETTING(5, osk_tap_toggle),
DECLARE_STATIC_SETTING(6, osk_timeout),
DECLARE_STATIC_SETTING(7, tapping_term),
DECLARE_STATIC_SETTING(8, tapping),
#ifdef MOUSEKEY_ENABLE
DECLARE_SETTING_CB(9, mousekey_delay, mousekey_apply),
DECLARE_SETTING_CB(10, mousekey_interval, mousekey_apply),
DECLARE_SETTING_CB(11, mousekey_move_delta, mousekey_apply),
DECLARE_SETTING_CB(12, mousekey_max_speed, mousekey_apply),
DECLARE_SETTING_CB(13, mousekey_time_to_max, mousekey_apply),
DECLARE_SETTING_CB(14, mousekey_wheel_delay, mousekey_apply),
DECLARE_SETTING_CB(15, mousekey_wheel_interval, mousekey_apply),
DECLARE_SETTING_CB(16, mousekey_wheel_max_speed, mousekey_apply),
DECLARE_SETTING_CB(17, mousekey_wheel_time_to_max, mousekey_apply),
DECLARE_STATIC_SETTING_NOTIFY(9, mousekey_delay, mousekey_apply),
DECLARE_STATIC_SETTING_NOTIFY(10, mousekey_interval, mousekey_apply),
DECLARE_STATIC_SETTING_NOTIFY(11, mousekey_move_delta, mousekey_apply),
DECLARE_STATIC_SETTING_NOTIFY(12, mousekey_max_speed, mousekey_apply),
DECLARE_STATIC_SETTING_NOTIFY(13, mousekey_time_to_max, mousekey_apply),
DECLARE_STATIC_SETTING_NOTIFY(14, mousekey_wheel_delay, mousekey_apply),
DECLARE_STATIC_SETTING_NOTIFY(15, mousekey_wheel_interval, mousekey_apply),
DECLARE_STATIC_SETTING_NOTIFY(16, mousekey_wheel_max_speed, mousekey_apply),
DECLARE_STATIC_SETTING_NOTIFY(17, mousekey_wheel_time_to_max, mousekey_apply),
#endif
DECLARE_SETTING(18, tap_code_delay),
DECLARE_SETTING(19, tap_hold_caps_delay),
DECLARE_SETTING(20, tapping_toggle),
DECLARE_STATIC_SETTING(18, tap_code_delay),
DECLARE_STATIC_SETTING(19, tap_hold_caps_delay),
DECLARE_STATIC_SETTING(20, tapping_toggle),
};

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;
}

static void load_settings(void) {
static void eeprom_settings_load(void) {
for (size_t i = 0; i < sizeof(qmk_settings_t); ++i) {
uint8_t byte;
byte = dynamic_keymap_get_qmk_settings(i);
memcpy((char*)&QS + i, &byte, 1);
}
}

static void save_settings(void) {
static void eeprom_settings_save(void) {
for (size_t i = 0; i < sizeof(qmk_settings_t); ++i) {
uint8_t old_byte, new_byte;
old_byte = dynamic_keymap_get_qmk_settings(i);
Expand All @@ -82,14 +80,31 @@ static void save_settings(void) {
}
}

static int eeprom_settings_get(const qmk_settings_proto_t *proto, void *setting, size_t maxsz) {
uint16_t sz = pgm_read_word(&proto->sz);
if (sz > maxsz)
return -1;
memcpy(setting, pgm_read_ptr(&proto->ptr), sz);
return 0;
}

static int eeprom_settings_set(const qmk_settings_proto_t *proto, const void *setting, size_t maxsz) {
uint16_t sz = pgm_read_word(&proto->sz);
if (pgm_read_word(&proto->sz) > maxsz)
return -1;
memcpy(pgm_read_ptr(&proto->ptr), setting, sz);
eeprom_settings_save();
return 0;
}

void qmk_settings_init(void) {
load_settings();
eeprom_settings_load();
/* execute all callbacks to initialize the settings */
for (size_t i = 0; i < sizeof(protos)/sizeof(*protos); ++i) {
const qmk_settings_proto_t *proto = &protos[i];
qmk_setting_callback_t cb = pgm_read_ptr(&proto->cb);
if (cb)
cb();
qmk_settings_notify_t notify = pgm_read_ptr(&proto->notify);
if (notify)
notify();
}
}

Expand Down Expand Up @@ -119,7 +134,7 @@ void qmk_settings_reset(void) {
QS.tap_hold_caps_delay = TAP_HOLD_CAPS_DELAY;
QS.tapping_toggle = TAPPING_TOGGLE;

save_settings();
eeprom_settings_save();
/* to trigger all callbacks */
qmk_settings_init();
}
Expand All @@ -144,23 +159,41 @@ void qmk_settings_query(uint16_t qsid_gt, void *buffer, size_t sz) {
}
}

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)
if (!proto)
return -1;
memcpy(setting, pgm_read_ptr(&proto->ptr), pgm_read_word(&proto->sz));
return 0;

qmk_settings_get_t get = pgm_read_ptr(&proto->get);
if (!get)
return -1;

return get(proto, setting, maxsz);
}

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)
if (!proto)
return -1;
memcpy(pgm_read_ptr(&proto->ptr), setting, pgm_read_word(&proto->sz));
save_settings();
qmk_setting_callback_t cb = pgm_read_ptr(&proto->cb);
if (cb)
cb();

qmk_settings_set_t set = pgm_read_ptr(&proto->set);
if (!set)
return -1;

int ret = set(proto, setting, maxsz);
if (ret != 0)
return ret;

qmk_settings_notify_t notify = pgm_read_ptr(&proto->notify);
if (notify)
notify();
return 0;
}

Expand Down
14 changes: 10 additions & 4 deletions quantum/qmk_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,20 @@ typedef struct {
} qmk_settings_t;
_Static_assert(sizeof(qmk_settings_t) == 36, "unexpected size of the qmk_settings_t structure");

typedef void (*qmk_setting_callback_t)(void);
struct qmk_settings_proto_t;

/* setting prototype - describes how to get/set settings, stored in flash */
typedef struct {
typedef void (*qmk_settings_notify_t)(void);
typedef int (*qmk_settings_get_t)(const struct qmk_settings_proto_t *proto, void *setting, size_t maxsz);
typedef int (*qmk_settings_set_t)(const struct qmk_settings_proto_t *proto, const void *setting, size_t maxsz);

/* setting prototype - describes how to get/set settings; this structure is stored in flash */
typedef struct qmk_settings_proto_t {
uint16_t qsid;
uint16_t sz;
void *ptr;
qmk_setting_callback_t cb;
qmk_settings_get_t get;
qmk_settings_set_t set;
qmk_settings_notify_t notify;
} qmk_settings_proto_t;

void qmk_settings_init(void);
Expand Down

0 comments on commit ab0bff8

Please sign in to comment.