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 def layerInformation( self, name, date, author ):
69 self.fill_dict['Information'] += "// Name: {0}\n".format( "TODO" )
70 self.fill_dict['Information'] += "// Version: {0}\n".format( "TODO" )
71 self.fill_dict['Information'] += "// Date: {0}\n".format( "TODO" )
72 self.fill_dict['Information'] += "// Author: {0}\n".format( "TODO" )
75 # Processes content for fill tags and does any needed dataset calculations
76 def process( self, capabilities, macros ):
79 self.fill_dict['Information'] = "// This file was generated by the kll compiler, DO NOT EDIT.\n"
80 self.fill_dict['Information'] += "// Generation Date: {0}\n".format( "TODO" )
81 self.fill_dict['Information'] += "// Compiler arguments: {0}\n".format( "TODO" )
82 self.fill_dict['Information'] += "// KLL Backend: {0}\n".format( "TODO" )
83 self.fill_dict['Information'] += "// KLL Git Rev: {0}\n".format( "TODO" )
84 self.fill_dict['Information'] += "//\n"
85 self.fill_dict['Information'] += "// - Base Layer -\n"
86 self.fill_dict['Information'] += "// - Default Layer -\n"
87 self.fill_dict['Information'] += "// - Partial Layers -\n"
91 self.fill_dict['CapabilitiesList'] = "const Capability CapabilitiesList[] = {\n"
94 for key in capabilities.keys():
95 funcName = capabilities.funcName( key )
96 argByteWidth = capabilities.totalArgBytes( key )
97 self.fill_dict['CapabilitiesList'] += "\t{{ {0}, {1} }},\n".format( funcName, argByteWidth )
99 self.fill_dict['CapabilitiesList'] += "};"
103 self.fill_dict['ResultMacros'] = ""
105 # Iterate through each of the result macros
106 for result in range( 0, len( macros.resultsIndexSorted ) ):
107 self.fill_dict['ResultMacros'] += "Guide_RM( {0} ) = {{ ".format( result )
109 # Add the result macro capability index guide (including capability arguments)
110 # See kiibohd controller Macros/PartialMap/kll.h for exact formatting details
111 for sequence in range( 0, len( macros.resultsIndexSorted[ result ] ) ):
112 # For each combo in the sequence, add the length of the combo
113 self.fill_dict['ResultMacros'] += "{0}, ".format( len( macros.resultsIndexSorted[ result ][ sequence ] ) )
115 # For each combo, add each of the capabilities used and their arguments
116 for combo in range( 0, len( macros.resultsIndexSorted[ result ][ sequence ] ) ):
117 resultItem = macros.resultsIndexSorted[ result ][ sequence ][ combo ]
119 # Add the capability index
120 self.fill_dict['ResultMacros'] += "{0}, ".format( capabilities.getIndex( resultItem[0] ) )
122 # Add each of the arguments of the capability
123 for arg in range( 0, len( resultItem[1] ) ):
124 self.fill_dict['ResultMacros'] += "0x{0:02X}, ".format( resultItem[1][ arg ] )
126 # Add list ending 0 and end of list
127 self.fill_dict['ResultMacros'] += "0 };\n"
128 self.fill_dict['ResultMacros'] = self.fill_dict['ResultMacros'][:-1] # Remove last newline
131 ## Result Macro List ##
132 self.fill_dict['ResultMacroList'] = "const ResultMacro ResultMacroList[] = {\n"
134 # Iterate through each of the result macros
135 for result in range( 0, len( macros.resultsIndexSorted ) ):
136 self.fill_dict['ResultMacroList'] += "\tDefine_RM( {0} ),\n".format( result )
137 self.fill_dict['ResultMacroList'] += "};"
140 ## Result Macro Record ##
141 self.fill_dict['ResultMacroRecord'] = "ResultMacroRecord ResultMacroRecordList[ ResultMacroNum ];"
145 self.fill_dict['TriggerMacros'] = ""
147 # Iterate through each of the trigger macros
148 for trigger in range( 0, len( macros.triggersIndexSorted ) ):
149 self.fill_dict['TriggerMacros'] += "Guide_TM( {0} ) = {{ ".format( trigger )
151 # Add the trigger macro scan code guide
152 # See kiibohd controller Macros/PartialMap/kll.h for exact formatting details
153 for sequence in range( 0, len( macros.triggersIndexSorted[ trigger ][ 0 ] ) ):
154 # For each combo in the sequence, add the length of the combo
155 self.fill_dict['TriggerMacros'] += "{0}, ".format( len( macros.triggersIndexSorted[ trigger ][0][ sequence ] ) )
157 # For each combo, add the key type, key state and scan code
158 for combo in range( 0, len( macros.triggersIndexSorted[ trigger ][ 0 ][ sequence ] ) ):
159 triggerItem = macros.triggersIndexSorted[ trigger ][ 0 ][ sequence ][ combo ]
161 # TODO Add support for Analog keys
162 # TODO Add support for LED states
163 self.fill_dict['TriggerMacros'] += "0x00, 0x01, 0x{0:02X}, ".format( triggerItem )
165 # Add list ending 0 and end of list
166 self.fill_dict['TriggerMacros'] += "0 };\n"
167 self.fill_dict['TriggerMacros'] = self.fill_dict['TriggerMacros'][ :-1 ] # Remove last newline
170 ## Trigger Macro List ##
171 self.fill_dict['TriggerMacroList'] = "const TriggerMacro TriggerMacroList[] = {\n"
173 # Iterate through each of the trigger macros
174 for trigger in range( 0, len( macros.triggersIndexSorted ) ):
175 # Use TriggerMacro Index, and the corresponding ResultMacro Index
176 self.fill_dict['TriggerMacroList'] += "\tDefine_TM( {0}, {1} ),\n".format( trigger, macros.triggersIndexSorted[ trigger ][1] )
177 self.fill_dict['TriggerMacroList'] += "};"
180 ## Trigger Macro Record ##
181 self.fill_dict['TriggerMacroRecord'] = "TriggerMacroRecord TriggerMacroRecordList[ TriggerMacroNum ];"
185 self.fill_dict['MaxScanCode'] = "#define MaxScanCode 0x{0:X}".format( macros.overallMaxScanCode )
188 ## Default Layer and Default Layer Scan Map ##
189 self.fill_dict['DefaultLayerTriggerList'] = ""
190 self.fill_dict['DefaultLayerScanMap'] = "const nat_ptr_t *default_scanMap[] = {\n"
192 # Iterate over triggerList and generate a C trigger array for the default map and default map array
193 for triggerList in range( macros.firstScanCode[ 0 ], len( macros.triggerList[ 0 ] ) ):
194 # Generate ScanCode index and triggerList length
195 self.fill_dict['DefaultLayerTriggerList'] += "Define_TL( default, 0x{0:02X} ) = {{ {1}".format( triggerList, len( macros.triggerList[ 0 ][ triggerList ] ) )
197 # Add scanCode trigger list to Default Layer Scan Map
198 self.fill_dict['DefaultLayerScanMap'] += "default_tl_0x{0:02X}, ".format( triggerList )
200 # Add each item of the trigger list
201 for trigger in macros.triggerList[ 0 ][ triggerList ]:
202 self.fill_dict['DefaultLayerTriggerList'] += ", {0}".format( trigger )
204 self.fill_dict['DefaultLayerTriggerList'] += " };\n"
205 self.fill_dict['DefaultLayerTriggerList'] = self.fill_dict['DefaultLayerTriggerList'][:-1] # Remove last newline
206 self.fill_dict['DefaultLayerScanMap'] = self.fill_dict['DefaultLayerScanMap'][:-2] # Remove last comma and space
207 self.fill_dict['DefaultLayerScanMap'] += "\n};"
210 ## Partial Layers and Partial Layer Scan Maps ##
211 self.fill_dict['PartialLayerTriggerLists'] = ""
212 self.fill_dict['PartialLayerScanMaps'] = ""
214 # Iterate over each of the layers, excluding the default layer
215 for layer in range( 1, len( macros.triggerList ) ):
217 self.fill_dict['PartialLayerScanMaps'] += "// Partial Layer {0}\n".format( layer )
218 self.fill_dict['PartialLayerScanMaps'] += "const nat_ptr_t *layer{0}_scanMap[] = {{\n".format( layer )
219 self.fill_dict['PartialLayerTriggerLists'] += "// Partial Layer {0}\n".format( layer )
221 # Iterate over triggerList and generate a C trigger array for the layer
222 for triggerList in range( macros.firstScanCode[ layer ], len( macros.triggerList[ layer ] ) ):
223 # Generate ScanCode index and triggerList length
224 self.fill_dict['PartialLayerTriggerLists'] += "Define_TL( layer{0}, 0x{1:02X} ) = {{ {2}".format( layer, triggerList, len( macros.triggerList[ layer ][ triggerList ] ) )
226 # Add scanCode trigger list to Default Layer Scan Map
227 self.fill_dict['PartialLayerScanMaps'] += "layer{0}_tl_0x{1:02X}, ".format( layer, triggerList )
229 # Add each item of the trigger list
230 for trigger in macros.triggerList[ layer ][ triggerList ]:
231 self.fill_dict['PartialLayerTriggerLists'] += ", {0}".format( trigger )
233 self.fill_dict['PartialLayerTriggerLists'] += " };\n"
234 self.fill_dict['PartialLayerTriggerLists'] += "\n"
235 self.fill_dict['PartialLayerScanMaps'] = self.fill_dict['PartialLayerScanMaps'][:-2] # Remove last comma and space
236 self.fill_dict['PartialLayerScanMaps'] += "\n};\n\n"
237 self.fill_dict['PartialLayerTriggerLists'] = self.fill_dict['PartialLayerTriggerLists'][:-2] # Remove last 2 newlines
238 self.fill_dict['PartialLayerScanMaps'] = self.fill_dict['PartialLayerScanMaps'][:-2] # Remove last 2 newlines
241 ## Layer Index List ##
242 self.fill_dict['LayerIndexList'] = "const Layer LayerIndex[] = {\n"
244 # Iterate over each layer, adding it to the list
245 for layer in range( 0, len( macros.triggerList ) ):
246 # Lookup first scancode in map
247 firstScanCode = macros.firstScanCode[ layer ]
249 # Default map is a special case, always the first index
252 self.fill_dict['LayerIndexList'] += '\tLayer_IN( default_scanMap, "DefaultMap", 0x{0:02X} ),\n'.format( firstScanCode )
254 self.fill_dict['LayerIndexList'] += '\tLayer_IN( layer{0}_scanMap, "Layer {0}", 0x{1:02X} ),\n'.format( layer, firstScanCode )
255 self.fill_dict['LayerIndexList'] += "};"
259 self.fill_dict['LayerState'] = "uint8_t LayerState[ LayerNum ];"
262 # Generates the output keymap with fill tags filled
263 def generate( self, filepath ):
264 # Process each line of the template, outputting to the target path
265 with open( filepath, 'w' ) as outputFile:
266 with open( self.templatePath, 'r' ) as templateFile:
267 for line in templateFile:
268 # TODO Support multiple replacements per line
269 # TODO Support replacement with other text inline
270 match = re.findall( '<\|([^|>]+)\|>', line )
272 # If match, replace with processed variable
274 outputFile.write( self.fill_dict[ match[ 0 ] ] )
275 outputFile.write("\n")
277 # Otherwise, just append template to output file
279 outputFile.write( line )