2021-05-01 01:00:04 +00:00
""" Used by the make system to generate keyboard.h from info.json.
"""
from milc import cli
from qmk . info import info_json
2022-03-18 01:09:29 +00:00
from qmk . commands import dump_lines
2021-05-01 01:00:04 +00:00
from qmk . keyboard import keyboard_completer , keyboard_folder
2021-05-02 15:43:40 +00:00
from qmk . path import normpath
2022-03-18 01:09:29 +00:00
from qmk . constants import GPL2_HEADER_C_LIKE , GENERATED_HEADER_C_LIKE
2021-05-01 01:00:04 +00:00
def would_populate_layout_h ( keyboard ) :
""" Detect if a given keyboard is doing data driven layouts
"""
# Build the info.json file
kb_info_json = info_json ( keyboard )
for layout_name in kb_info_json [ ' layouts ' ] :
if kb_info_json [ ' layouts ' ] [ layout_name ] [ ' c_macro ' ] :
continue
if ' matrix ' not in kb_info_json [ ' layouts ' ] [ layout_name ] [ ' layout ' ] [ 0 ] :
cli . log . debug ( ' %s / %s : No matrix data! ' , keyboard , layout_name )
continue
return True
return False
@cli.argument ( ' -o ' , ' --output ' , arg_only = True , type = normpath , help = ' File to write to ' )
@cli.argument ( ' -q ' , ' --quiet ' , arg_only = True , action = ' store_true ' , help = " Quiet mode, only output error messages " )
2021-08-18 20:52:41 +00:00
@cli.argument ( ' -kb ' , ' --keyboard ' , arg_only = True , type = keyboard_folder , completer = keyboard_completer , required = True , help = ' Keyboard to generate keyboard.h for. ' )
2021-05-01 01:00:04 +00:00
@cli.subcommand ( ' Used by the make system to generate keyboard.h from info.json ' , hidden = True )
def generate_keyboard_h ( cli ) :
""" Generates the keyboard.h file.
"""
2021-08-18 20:52:41 +00:00
has_layout_h = would_populate_layout_h ( cli . args . keyboard )
2021-05-01 01:00:04 +00:00
# Build the layouts.h file.
2022-03-18 01:09:29 +00:00
keyboard_h_lines = [ GPL2_HEADER_C_LIKE , GENERATED_HEADER_C_LIKE , ' #pragma once ' , ' #include " quantum.h " ' ]
2021-05-01 01:00:04 +00:00
if not has_layout_h :
keyboard_h_lines . append ( ' #pragma error( " <keyboard>.h is only optional for data driven keyboards - kb.h == bad times " ) ' )
# Show the results
2022-03-18 01:09:29 +00:00
dump_lines ( cli . args . output , keyboard_h_lines , cli . args . quiet )