Skip to content

Commit

Permalink
Allow codegen of keymap.json => keymap.c without requiring layers…
Browse files Browse the repository at this point in the history
…/layout etc. (#23451)
  • Loading branch information
tzarc authored Nov 24, 2024
1 parent 859dab8 commit 7d8f193
Show file tree
Hide file tree
Showing 22 changed files with 105 additions and 120 deletions.
5 changes: 4 additions & 1 deletion builddefs/build_keyboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ endif
# Have we found a keymap.json?
ifneq ("$(wildcard $(KEYMAP_JSON))", "")
ifneq ("$(wildcard $(KEYMAP_C))", "")
$(call WARNING_MESSAGE,Keymap is specified as both keymap.json and keymap.c -- keymap.json file wins.)
# Allow a separately-found keymap.c next to keymap.json -- the keymap.c
# generator will include the other keymap.c in the process, if supplied.
OTHER_KEYMAP_C := $(KEYMAP_C)
OPT_DEFS += -DOTHER_KEYMAP_C=\"$(OTHER_KEYMAP_C)\"
endif

KEYMAP_PATH := $(KEYMAP_JSON_PATH)
Expand Down
7 changes: 1 addition & 6 deletions data/schemas/keymap.jsonschema
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,5 @@
"notes": {
"type": "string"
}
},
"required": [
"keyboard",
"layout",
"layers"
]
}
}
3 changes: 0 additions & 3 deletions keyboards/handwired/onekey/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
"pid": "0x6465",
"device_version": "0.0.1"
},
"tapping": {
"term": 500
},
"diode_direction": "COL2ROW",
"features": {
"bootmagic": false,
Expand Down
5 changes: 1 addition & 4 deletions keyboards/handwired/onekey/kb2040/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

#pragma once


#define DEBUG_MATRIX_SCAN_RATE

#define QMK_WAITING_TEST_BUSY_PIN GP8
#define QMK_WAITING_TEST_YIELD_PIN GP9

Expand All @@ -18,4 +15,4 @@
#define I2C_DRIVER I2CD0
#define I2C1_SDA_PIN GP12
#define I2C1_SCL_PIN GP13

10 changes: 10 additions & 0 deletions keyboards/handwired/onekey/keymaps/adc/keymap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config": {
"features": {
"console": true
},
"tapping": {
"term": 500
}
}
}
2 changes: 0 additions & 2 deletions keyboards/handwired/onekey/keymaps/adc/rules.mk
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
CONSOLE_ENABLE = yes

ANALOG_DRIVER_REQUIRED = yes
14 changes: 0 additions & 14 deletions keyboards/handwired/onekey/keymaps/apa102/config.h

This file was deleted.

29 changes: 29 additions & 0 deletions keyboards/handwired/onekey/keymaps/apa102/keymap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"config": {
"features": {
"rgblight": true
},
"tapping": {
"term": 500
},
"rgblight": {
"driver": "apa102",
"led_count": 40,
"default": {
"val": 5
},
"animations": {
"breathing": true,
"rainbow_mood": true,
"rainbow_swirl": true,
"snake": true,
"knight": true,
"christmas": true,
"static_gradient": true,
"rgb_test": true,
"alternating": true,
"twinkle": true
}
}
}
}
2 changes: 0 additions & 2 deletions keyboards/handwired/onekey/keymaps/apa102/rules.mk

This file was deleted.

3 changes: 0 additions & 3 deletions keyboards/handwired/onekey/keymaps/backlight/config.h

This file was deleted.

14 changes: 14 additions & 0 deletions keyboards/handwired/onekey/keymaps/backlight/keymap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"config": {
"features": {
"backlight": true,
"tap_dance": true
},
"tapping": {
"term": 500
},
"backlight": {
"breathing": true
}
}
}
2 changes: 0 additions & 2 deletions keyboards/handwired/onekey/keymaps/backlight/rules.mk

This file was deleted.

8 changes: 8 additions & 0 deletions keyboards/handwired/onekey/keymaps/console/keymap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"config": {
"features": {
"console": true,
"debug_matrix_scan_rate": true
}
}
}
2 changes: 0 additions & 2 deletions keyboards/handwired/onekey/keymaps/console/rules.mk

This file was deleted.

7 changes: 7 additions & 0 deletions keyboards/handwired/onekey/keymaps/default/keymap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"config": {
"tapping": {
"term": 500
}
}
}
3 changes: 0 additions & 3 deletions keyboards/handwired/onekey/rp2040/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

#pragma once


#define DEBUG_MATRIX_SCAN_RATE

#define QMK_WAITING_TEST_BUSY_PIN GP8
#define QMK_WAITING_TEST_YIELD_PIN GP9

Expand Down
2 changes: 1 addition & 1 deletion keyboards/handwired/pterodactyl/keymaps/default/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
* |------+------+------+------+------+------| |------+------+------+------+------+------|
* | | % | ^ | [ | ] | ~ | | & | 1 | 2 | 3 | \ | |
* |------+------+------+------+------+------' `------+------+------+------+------+------|
* |QK_BOOT | | | | | | | . | 0 | = | |
* |BtLdr | | | | | | | . | 0 | = | |
* `----------------------------------' `----------------------------------'
* ,-------------. ,-------------.
* | | | | | |
Expand Down
43 changes: 0 additions & 43 deletions keyboards/handwired/pterodactyl/keymaps/default/keymap.json

This file was deleted.

2 changes: 1 addition & 1 deletion lib/python/qmk/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def parse_configurator_json(configurator_file):
cli.log.error(f'Invalid JSON keymap: {configurator_file} : {e.message}')
maybe_exit(1)

keyboard = user_keymap['keyboard']
keyboard = user_keymap.get('keyboard', None)
aliases = keyboard_alias_definitions()

while keyboard in aliases:
Expand Down
29 changes: 17 additions & 12 deletions lib/python/qmk/keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,40 @@
* This file was generated by qmk json2c. You may or may not want to
* edit it directly.
*/
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
__KEYMAP_GOES_HERE__
};
#if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS] = {
__KEYMAP_GOES_HERE__
__ENCODER_MAP_GOES_HERE__
};
#endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
__MACRO_OUTPUT_GOES_HERE__
#ifdef OTHER_KEYMAP_C
# include OTHER_KEYMAP_C
#endif // OTHER_KEYMAP_C
"""


def _generate_keymap_table(keymap_json):
lines = []
lines = ['const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {']
for layer_num, layer in enumerate(keymap_json['layers']):
if layer_num != 0:
lines[-1] = lines[-1] + ','
layer = map(_strip_any, layer)
layer_keys = ', '.join(layer)
lines.append(' [%s] = %s(%s)' % (layer_num, keymap_json['layout'], layer_keys))
lines.append('};')
return lines


def _generate_encodermap_table(keymap_json):
lines = []
lines = [
'#if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)',
'const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS] = {',
]
for layer_num, layer in enumerate(keymap_json['encoders']):
if layer_num != 0:
lines[-1] = lines[-1] + ','
encoder_keycode_txt = ', '.join([f'ENCODER_CCW_CW({_strip_any(e["ccw"])}, {_strip_any(e["cw"])})' for e in layer])
lines.append(' [%s] = {%s}' % (layer_num, encoder_keycode_txt))
lines.extend(['};', '#endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)'])
return lines


Expand Down Expand Up @@ -271,8 +273,11 @@ def generate_c(keymap_json):
A sequence of strings containing macros to implement for this keyboard.
"""
new_keymap = DEFAULT_KEYMAP_C
layer_txt = _generate_keymap_table(keymap_json)
keymap = '\n'.join(layer_txt)

keymap = ''
if 'layers' in keymap_json and keymap_json['layers'] is not None:
layer_txt = _generate_keymap_table(keymap_json)
keymap = '\n'.join(layer_txt)
new_keymap = new_keymap.replace('__KEYMAP_GOES_HERE__', keymap)

encodermap = ''
Expand Down
24 changes: 8 additions & 16 deletions lib/python/qmk/tests/test_cli_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,16 @@ def test_json2c():
* This file was generated by qmk json2c. You may or may not want to
* edit it directly.
*/
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[0] = LAYOUT_ortho_1x1(KC_A)
};
#if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS] = {
};
#endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
#ifdef OTHER_KEYMAP_C
# include OTHER_KEYMAP_C
#endif // OTHER_KEYMAP_C
"""
Expand Down Expand Up @@ -190,28 +189,21 @@ def test_json2c_stdin():
* This file was generated by qmk json2c. You may or may not want to
* edit it directly.
*/
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[0] = LAYOUT_ortho_1x1(KC_A)
};
#if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS] = {
};
#endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
#ifdef OTHER_KEYMAP_C
# include OTHER_KEYMAP_C
#endif // OTHER_KEYMAP_C
"""


def test_json2c_wrong_json():
result = check_subcommand('json2c', 'keyboards/handwired/pytest/info.json')
check_returncode(result, [1])
assert 'Invalid JSON keymap' in result.stdout


def test_json2c_no_json():
result = check_subcommand('json2c', 'keyboards/handwired/pytest/basic/keymaps/default/keymap.c')
check_returncode(result, [1])
Expand Down
9 changes: 4 additions & 5 deletions lib/python/qmk/tests/test_qmk_keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ def test_generate_c_pytest_basic():
* This file was generated by qmk json2c. You may or may not want to
* edit it directly.
*/
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[0] = LAYOUT(KC_A)
};
#if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS] = {
};
#endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
#ifdef OTHER_KEYMAP_C
# include OTHER_KEYMAP_C
#endif // OTHER_KEYMAP_C
"""


Expand Down

0 comments on commit 7d8f193

Please sign in to comment.