X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=kll.py;h=e9ea19b6d26b4647f0829d6c63173610995240c6;hb=cf5bba703932f11bb686d9968d0c4bb9eab2a32c;hp=7844b8266a0d1b33e12b1aedef644e52197afa5b;hpb=bbf2c3ffaf4d579a5865c36c986720202d11f04a;p=kiibohd-kll.git diff --git a/kll.py b/kll.py index 7844b82..e9ea19b 100755 --- a/kll.py +++ b/kll.py @@ -2,7 +2,7 @@ # KLL Compiler # Keyboard Layout Langauge # -# Copyright (C) 2014 by Jacob Alexander +# Copyright (C) 2014-2015 by Jacob Alexander # # This file is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -20,19 +20,19 @@ ### Imports ### import argparse +import importlib import io import os import re import sys import token -import importlib -from tokenize import generate_tokens -from re import VERBOSE from pprint import pformat +from re import VERBOSE +from tokenize import generate_tokens -from kll_lib.hid_dict import * from kll_lib.containers import * +from kll_lib.hid_dict import * from funcparserlib.lexer import make_tokenizer, Token, LexerError from funcparserlib.parser import (some, a, many, oneplus, skip, finished, maybe, skip, forward_decl, NoParseError) @@ -83,19 +83,20 @@ def processCommandLineArgs(): # Optional Arguments pArgs.add_argument( '-b', '--backend', type=str, default="kiibohd", help="Specify target backend for the KLL compiler.\n" - "Default: kiibohd" ) + "Default: kiibohd\n" + "Options: kiibohd, json" ) pArgs.add_argument( '-d', '--default', type=str, nargs='+', help="Specify .kll files to layer on top of the default map to create a combined map." ) pArgs.add_argument( '-p', '--partial', type=str, nargs='+', action='append', help="Specify .kll files to generate partial map, multiple files per flag.\n" "Each -p defines another partial map.\n" "Base .kll files (that define the scan code maps) must be defined for each partial map." ) - pArgs.add_argument( '-t', '--template', type=str, default="templates/kiibohdKeymap.h", + pArgs.add_argument( '-t', '--templates', type=str, nargs='+', help="Specify template used to generate the keymap.\n" - "Default: templates/kiibohdKeymap.h" ) - pArgs.add_argument( '-o', '--output', type=str, default="templateKeymap.h", + "Default: " ) + pArgs.add_argument( '-o', '--outputs', type=str, nargs='+', help="Specify output file. Writes to current working directory by default.\n" - "Default: generatedKeymap.h" ) + "Default: " ) pArgs.add_argument( '-h', '--help', action="help", help="This message." ) @@ -107,7 +108,7 @@ def processCommandLineArgs(): defaultFiles = args.default partialFileSets = args.partial if defaultFiles is None: - partialFileSets = [] + defaultFiles = [] if partialFileSets is None: partialFileSets = [[]] @@ -122,7 +123,7 @@ def processCommandLineArgs(): for filename in partial: checkFileExists( filename ) - return (baseFiles, defaultFiles, partialFileSets, args.backend, args.template, args.output) + return (baseFiles, defaultFiles, partialFileSets, args.backend, args.templates, args.outputs) @@ -137,6 +138,12 @@ def tokenize( string ): ( 'Space', ( r'[ \t\r\n]+', ) ), ( 'USBCode', ( r'U(("[^"]+")|(0x[0-9a-fA-F]+)|([0-9]+))', ) ), ( 'USBCodeStart', ( r'U\[', ) ), + ( 'ConsCode', ( r'CONS(("[^"]+")|(0x[0-9a-fA-F]+)|([0-9]+))', ) ), + ( 'ConsCodeStart', ( r'CONS\[', ) ), + ( 'SysCode', ( r'SYS(("[^"]+")|(0x[0-9a-fA-F]+)|([0-9]+))', ) ), + ( 'SysCodeStart', ( r'SYS\[', ) ), + ( 'LedCode', ( r'LED(("[^"]+")|(0x[0-9a-fA-F]+)|([0-9]+))', ) ), + ( 'LedCodeStart', ( r'LED\[', ) ), ( 'ScanCode', ( r'S((0x[0-9a-fA-F]+)|([0-9]+))', ) ), ( 'ScanCodeStart', ( r'S\[', ) ), ( 'CodeEnd', ( r'\]', ) ), @@ -147,6 +154,7 @@ def tokenize( string ): ( 'Dash', ( r'-', ) ), ( 'Plus', ( r'\+', ) ), ( 'Parenthesis', ( r'\(|\)', ) ), + ( 'None', ( r'None', ) ), ( 'Number', ( r'-?(0x[0-9a-fA-F]+)|(0|([1-9][0-9]*))', VERBOSE ) ), ( 'Name', ( r'[A-Za-z_][A-Za-z_0-9]*', ) ), ( 'VariableContents', ( r'''[^"' ;:=>()]+''', ) ), @@ -165,7 +173,7 @@ def tokenize( string ): ## Map Arrays macros_map = Macros() -variable_dict = dict() +variables_dict = Variables() capabilities_dict = Capabilities() @@ -174,31 +182,84 @@ capabilities_dict = Capabilities() def make_scanCode( token ): scanCode = int( token[1:], 0 ) # Check size, to make sure it's valid - if scanCode > 0xFF: - print ( "{0} ScanCode value {1} is larger than 255".format( ERROR, scanCode ) ) - raise + # XXX Add better check that takes symbolic names into account (i.e. U"Latch5") + #if scanCode > 0xFF: + # print ( "{0} ScanCode value {1} is larger than 255".format( ERROR, scanCode ) ) + # raise return scanCode -def make_usbCode( token ): +def make_hidCode( type, token ): # If first character is a U, strip if token[0] == "U": token = token[1:] + # CONS specifier + elif 'CONS' in token: + token = token[4:] + # SYS specifier + elif 'SYS' in token: + token = token[3:] # If using string representation of USB Code, do lookup, case-insensitive if '"' in token: try: - usbCode = kll_hid_lookup_dictionary[ token[1:-1].upper() ] + hidCode = kll_hid_lookup_dictionary[ type ][ token[1:-1].upper() ][1] except LookupError as err: - print ( "{0} {1} is an invalid USB Code Lookup...".format( ERROR, err ) ) + print ( "{0} {1} is an invalid USB HID Code Lookup...".format( ERROR, err ) ) raise else: - usbCode = int( token, 0 ) + # Already tokenized + if type == 'USBCode' and token[0] == 'USB' or type == 'SysCode' and token[0] == 'SYS' or type == 'ConsCode' and token[0] == 'CONS': + hidCode = token[1] + # Convert + else: + hidCode = int( token, 0 ) + + # Check size if a USB Code, to make sure it's valid + # XXX Add better check that takes symbolic names into account (i.e. U"Latch5") + #if type == 'USBCode' and hidCode > 0xFF: + # print ( "{0} USBCode value {1} is larger than 255".format( ERROR, hidCode ) ) + # raise + + # Return a tuple, identifying which type it is + if type == 'USBCode': + return make_usbCode_number( hidCode ) + elif type == 'ConsCode': + return make_consCode_number( hidCode ) + elif type == 'SysCode': + return make_sysCode_number( hidCode ) + + print ( "{0} Unknown HID Specifier '{1}'".format( ERROR, type ) ) + raise - # Check size, to make sure it's valid - if usbCode > 0xFF: - print ( "{0} USBCode value {1} is larger than 255".format( ERROR, usbCode ) ) - raise - return usbCode +def make_usbCode( token ): + return make_hidCode( 'USBCode', token ) + +def make_consCode( token ): + return make_hidCode( 'ConsCode', token ) + +def make_sysCode( token ): + return make_hidCode( 'SysCode', token ) + +def make_hidCode_number( type, token ): + lookup = { + 'ConsCode' : 'CONS', + 'SysCode' : 'SYS', + 'USBCode' : 'USB', + } + return ( lookup[ type ], token ) + +def make_usbCode_number( token ): + return make_hidCode_number( 'USBCode', token ) + +def make_consCode_number( token ): + return make_hidCode_number( 'ConsCode', token ) + +def make_sysCode_number( token ): + return make_hidCode_number( 'SysCode', token ) + + # Replace key-word with None specifier (which indicates a noneOut capability) +def make_none( token ): + return [[[('NONE', 0)]]] def make_seqString( token ): # Shifted Characters, and amount to move by to get non-shifted version @@ -214,14 +275,13 @@ def make_seqString( token ): ( ":", 0x01 ), ( "^", -0x10 ), ( "_", -0x18 ), - ( "{}|", -0x1E ), - ( "~", -0x20 ), + ( "{}|~", -0x1E ), ( "@", -0x32 ), ( "?", -0x38 ), ) listOfLists = [] - shiftKey = kll_hid_lookup_dictionary["SHIFT"] + shiftKey = kll_hid_lookup_dictionary['USBCode']["SHIFT"] # Creates a list of USB codes from the string: sequence (list) of combos (lists) for char in token[1:-1]: @@ -239,7 +299,7 @@ def make_seqString( token ): # Do KLL HID Lookup on non-shifted character # NOTE: Case-insensitive, which is why the shift must be pre-computed - usbCode = kll_hid_lookup_dictionary[ processedChar.upper() ] + usbCode = kll_hid_lookup_dictionary['USBCode'][ processedChar.upper() ] # Create Combo for this character, add shift key if shifted charCombo = [] @@ -255,6 +315,9 @@ def make_seqString( token ): def make_string( token ): return token[1:-1] +def make_unseqString( token ): + return token[1:-1] + def make_number( token ): return int( token, 0 ) @@ -271,27 +334,40 @@ def make_scanCode_range( rangeVals ): return list( range( start, end + 1 ) ) # Range can go from high to low or low to high - # Warn on 0-9 (as this does not do what one would expect) TODO + # Warn on 0-9 for USBCodes (as this does not do what one would expect) TODO # Lookup USB HID tags and convert to a number -def make_usbCode_range( rangeVals ): +def make_hidCode_range( type, rangeVals ): # Check if already integers if isinstance( rangeVals[0], int ): start = rangeVals[0] else: - start = make_usbCode( rangeVals[0] ) + start = make_hidCode( type, rangeVals[0] )[1] if isinstance( rangeVals[1], int ): end = rangeVals[1] else: - end = make_usbCode( rangeVals[1] ) + end = make_hidCode( type, rangeVals[1] )[1] # Swap start, end if start is greater than end if start > end: start, end = end, start # Iterate from start to end, and generate the range - return list( range( start, end + 1 ) ) - pass + listRange = list( range( start, end + 1 ) ) + + # Convert each item in the list to a tuple + for item in range( len( listRange ) ): + listRange[ item ] = make_hidCode_number( type, listRange[ item ] ) + return listRange + +def make_usbCode_range( rangeVals ): + return make_hidCode_range( 'USBCode', rangeVals ) + +def make_sysCode_range( rangeVals ): + return make_hidCode_range( 'SysCode', rangeVals ) + +def make_consCode_range( rangeVals ): + return make_hidCode_range( 'ConsCode', rangeVals ) ## Base Rules @@ -321,6 +397,21 @@ def oneLayerFlatten( items ): return mainList + # Capability arguments may need to be expanded (e.g. 1 16 bit argument needs to be 2 8 bit arguments for the state machine) +def capArgExpander( items ): + newArgs = [] + # For each defined argument in the capability definition + for arg in range( 0, len( capabilities_dict[ items[0] ][1] ) ): + argLen = capabilities_dict[ items[0] ][1][ arg ][1] + num = items[1][ arg ] + byteForm = num.to_bytes( argLen, byteorder='little' ) # XXX Yes, little endian from how the uC structs work + + # For each sub-argument, split into byte-sized chunks + for byte in range( 0, argLen ): + newArgs.append( byteForm[ byte ] ) + + return tuple( [ items[0], tuple( newArgs ) ] ) + # Expand ranges of values in the 3rd dimension of the list, to a list of 2nd lists # i.e. [ sequence, [ combo, [ range ] ] ] --> [ [ sequence, [ combo ] ],