2 # KLL Compiler - Kiibohd Backend
4 # Backend code generator for the Kiibohd Controller firmware.
6 # Copyright (C) 2014 by Jacob Alexander
8 # This file is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This file is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this file. If not, see <http://www.gnu.org/licenses/>.
27 # Modifying Python Path, which is dumb, but the only way to import up one directory...
28 sys.path.append( os.path.expanduser('..') )
30 from kll_lib.containers import *
35 ## Print Decorator Variables
36 ERROR = '\033[5;1;31mERROR\033[0m:'
44 # Looks for template file and builds list of fill tags
45 def __init__( self, templatePath ):
46 # Does template exist?
47 if not os.path.isfile( templatePath ):
48 print ( "{0} '{1}' does not exist...".format( ERROR, templatePath ) )
51 self.templatePath = templatePath
52 self.fill_dict = dict()
54 # Generate list of fill tags
56 with open( templatePath, 'r' ) as openFile:
58 match = re.findall( '<\|([^|>]+)\|>', line )
60 self.tagList.append( item )
63 # USB Code Capability Name
64 def usbCodeCapability( self ):
68 # Processes content for fill tags and does any needed dataset calculations
69 def process( self, capabilities, macros ):
71 self.fill_dict['CapabilitiesList'] = "const Capability CapabilitiesList[] = {\n"
74 for key in capabilities.keys():
75 funcName = capabilities.funcName( key )
76 argByteWidth = capabilities.totalArgBytes( key )
77 self.fill_dict['CapabilitiesList'] += "\t{{ {0}, {1} }},\n".format( funcName, argByteWidth )
79 self.fill_dict['CapabilitiesList'] += "};"
83 self.fill_dict['ResultMacros'] = ""
85 # Iterate through each of the result macros
86 for result in range( 0, len( macros.resultsIndexSorted ) ):
87 self.fill_dict['ResultMacros'] += "Guide_RM( {0} ) = {{ ".format( result )
89 # Add the result macro capability index guide (including capability arguments)
90 # See kiibohd controller Macros/PartialMap/kll.h for exact formatting details
91 for sequence in range( 0, len( macros.resultsIndexSorted[ result ] ) ):
92 # For each combo in the sequence, add the length of the combo
93 self.fill_dict['ResultMacros'] += "{0}, ".format( len( macros.resultsIndexSorted[ result ][ sequence ] ) )
95 # For each combo, add each of the capabilities used and their arguments
96 for combo in range( 0, len( macros.resultsIndexSorted[ result ][ sequence ] ) ):
97 resultItem = macros.resultsIndexSorted[ result ][ sequence ][ combo ]
99 # Add the capability index
100 self.fill_dict['ResultMacros'] += "{0}, ".format( capabilities.getIndex( resultItem[0] ) )
102 # Add each of the arguments of the capability
103 for arg in range( 0, len( resultItem[1] ) ):
104 self.fill_dict['ResultMacros'] += "0x{0:02X}, ".format( resultItem[1][ arg ] )
106 # Add list ending 0 and end of list
107 self.fill_dict['ResultMacros'] += "0 };\n"
108 self.fill_dict['ResultMacros'] = self.fill_dict['ResultMacros'][:-1] # Remove last newline
111 ## Result Macro List ##
112 self.fill_dict['ResultMacroList'] = "ResultMacro ResultMacroList[] = {\n"
114 # Iterate through each of the result macros
115 for result in range( 0, len( macros.resultsIndexSorted ) ):
116 self.fill_dict['ResultMacroList'] += "\tDefine_RM( {0} ),\n".format( result )
117 self.fill_dict['ResultMacroList'] += "};"
121 self.fill_dict['TriggerMacros'] = ""
123 # Iterate through each of the trigger macros
124 for trigger in range( 0, len( macros.triggersIndexSorted ) ):
125 self.fill_dict['TriggerMacros'] += "Guide_TM( {0} ) = {{ ".format( trigger )
127 # Add the trigger macro scan code guide
128 # See kiibohd controller Macros/PartialMap/kll.h for exact formatting details
129 for sequence in range( 0, len( macros.triggersIndexSorted[ trigger ][ 0 ] ) ):
130 # For each combo in the sequence, add the length of the combo
131 self.fill_dict['TriggerMacros'] += "{0}, ".format( len( macros.triggersIndexSorted[ trigger ][0][ sequence ] ) )
133 # For each combo, add the key type, key state and scan code
134 for combo in range( 0, len( macros.triggersIndexSorted[ trigger ][ 0 ][ sequence ] ) ):
135 triggerItem = macros.triggersIndexSorted[ trigger ][ 0 ][ sequence ][ combo ]
137 # TODO Add support for Analog keys
138 # TODO Add support for LED states
139 self.fill_dict['TriggerMacros'] += "0x00, 0x01, 0x{0:02X}, ".format( triggerItem )
141 # Add list ending 0 and end of list
142 self.fill_dict['TriggerMacros'] += "0 };\n"
143 self.fill_dict['TriggerMacros'] = self.fill_dict['TriggerMacros'][ :-1 ] # Remove last newline
146 ## Trigger Macro List ##
147 self.fill_dict['TriggerMacroList'] = "TriggerMacro TriggerMacroList[] = {\n"
149 # Iterate through each of the trigger macros
150 for trigger in range( 0, len( macros.triggersIndexSorted ) ):
151 # Use TriggerMacro Index, and the corresponding ResultMacro Index
152 self.fill_dict['TriggerMacroList'] += "\tDefine_TM( {0}, {1} ),\n".format( trigger, macros.triggersIndexSorted[ trigger ][1] )
153 self.fill_dict['TriggerMacroList'] += "};"
157 self.fill_dict['MaxScanCode'] = "#define MaxScanCode 0x{0:X}".format( macros.overallMaxScanCode )
160 ## Default Layer and Default Layer Scan Map ##
161 self.fill_dict['DefaultLayerTriggerList'] = ""
162 self.fill_dict['DefaultLayerScanMap'] = "const unsigned int *default_scanMap[] = {\n"
164 # Iterate over triggerList and generate a C trigger array for the default map and default map array
165 for triggerList in range( 0, len( macros.triggerList[ 0 ] ) ):
166 # Generate ScanCode index and triggerList length
167 self.fill_dict['DefaultLayerTriggerList'] += "Define_TL( default, 0x{0:02X} ) = {{ {1}".format( triggerList, len( macros.triggerList[ 0 ][ triggerList ] ) )
169 # Add scanCode trigger list to Default Layer Scan Map
170 self.fill_dict['DefaultLayerScanMap'] += "default_tl_0x{0:02X}, ".format( triggerList )
172 # Add each item of the trigger list
173 for trigger in macros.triggerList[ 0 ][ triggerList ]:
174 self.fill_dict['DefaultLayerTriggerList'] += ", {0}".format( trigger )
176 self.fill_dict['DefaultLayerTriggerList'] += " };\n"
177 self.fill_dict['DefaultLayerTriggerList'] = self.fill_dict['DefaultLayerTriggerList'][:-1] # Remove last newline
178 self.fill_dict['DefaultLayerScanMap'] = self.fill_dict['DefaultLayerScanMap'][:-2] # Remove last comma and space
179 self.fill_dict['DefaultLayerScanMap'] += "\n};"
181 #print( self.fill_dict['DefaultLayerTriggerList'] )
182 #print( self.fill_dict['DefaultLayerScanMap'] )
186 self.fill_dict['PartialLayerTriggerLists'] = ""
188 #print( self.fill_dict['PartialLayerTriggerLists'] )
191 ## Partial Layer Scan Maps ##
192 self.fill_dict['PartialLayerScanMaps'] = ""
194 #print( self.fill_dict['PartialLayerScanMaps'] )
197 ## Layer Index List ##
198 self.fill_dict['LayerIndexList'] = "Layer LayerIndex[] = {\n"
200 # Iterate over each layer, adding it to the list
201 for layer in range( 0, len( macros.triggerList ) ):
202 # Default map is a special case, always the first index
204 self.fill_dict['LayerIndexList'] += '\tLayer_IN( default_scanMap, "DefaultMap" ),\n'
208 self.fill_dict['LayerIndexList'] += "};"
210 #print( self.fill_dict['LayerIndexList'] )
213 # Generates the output keymap with fill tags filled
214 def generate( self, filepath ):
215 # Process each line of the template, outputting to the target path
216 with open( filepath, 'w' ) as outputFile:
217 with open( self.templatePath, 'r' ) as templateFile:
218 for line in templateFile:
219 # TODO Support multiple replacements per line
220 # TODO Support replacement with other text inline
221 match = re.findall( '<\|([^|>]+)\|>', line )
223 # If match, replace with processed variable
225 outputFile.write( self.fill_dict[ match[ 0 ] ] )
226 outputFile.write("\n")
228 # Otherwise, just append template to output file
230 outputFile.write( line )