]> git.donarmstrong.com Git - kiibohd-kll.git/blob - backends/kiibohd.py
Adding inheritance to Backend class
[kiibohd-kll.git] / backends / kiibohd.py
1 #!/usr/bin/env python3
2 # KLL Compiler - Kiibohd Backend
3 #
4 # Backend code generator for the Kiibohd Controller firmware.
5 #
6 # Copyright (C) 2014-2015 by Jacob Alexander
7 #
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.
12 #
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.
17 #
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/>.
20
21 ### Imports ###
22
23 import os
24 import sys
25 import re
26
27 from datetime import date
28
29 # Modifying Python Path, which is dumb, but the only way to import up one directory...
30 sys.path.append( os.path.expanduser('..') )
31
32 from kll_lib.containers import *
33 from kll_lib.backends import *
34
35
36 ### Classes ###
37
38 class Backend( BackendBase ):
39         # USB Code Capability Name
40         def usbCodeCapability( self ):
41                 return "usbKeyOut";
42
43
44         # TODO
45         def layerInformation( self, name, date, author ):
46                 self.fill_dict['Information'] += "//  Name:    {0}\n".format( "TODO" )
47                 self.fill_dict['Information'] += "//  Version: {0}\n".format( "TODO" )
48                 self.fill_dict['Information'] += "//  Date:    {0}\n".format( "TODO" )
49                 self.fill_dict['Information'] += "//  Author:  {0}\n".format( "TODO" )
50
51
52         # Processes content for fill tags and does any needed dataset calculations
53         def process( self, capabilities, macros, variables, gitRev, gitChanges ):
54                 # Build string list of compiler arguments
55                 compilerArgs = ""
56                 for arg in sys.argv:
57                         if "--" in arg or ".py" in arg:
58                                 compilerArgs += "//    {0}\n".format( arg )
59                         else:
60                                 compilerArgs += "//      {0}\n".format( arg )
61
62                 # Build a string of modified files, if any
63                 gitChangesStr = "\n"
64                 if len( gitChanges ) > 0:
65                         for gitFile in gitChanges:
66                                 gitChangesStr += "//    {0}\n".format( gitFile )
67                 else:
68                         gitChangesStr = "    None\n"
69
70                 # Prepare BaseLayout and Layer Info
71                 baseLayoutInfo = ""
72                 defaultLayerInfo = ""
73                 partialLayersInfo = ""
74                 for file, name in zip( variables.baseLayout['*LayerFiles'], variables.baseLayout['*NameStack'] ):
75                         baseLayoutInfo += "//    {0}\n//      {1}\n".format( name, file )
76                 if '*LayerFiles' in variables.layerVariables[0].keys():
77                         for file, name in zip( variables.layerVariables[0]['*LayerFiles'], variables.layerVariables[0]['*NameStack'] ):
78                                 defaultLayerInfo += "//    {0}\n//      {1}\n".format( name, file )
79                 if '*LayerFiles' in variables.layerVariables[1].keys():
80                         for layer in range( 1, len( variables.layerVariables ) ):
81                                 partialLayersInfo += "//    Layer {0}\n".format( layer )
82                                 if len( variables.layerVariables[ layer ]['*LayerFiles'] ) > 0:
83                                         for file, name in zip( variables.layerVariables[ layer ]['*LayerFiles'], variables.layerVariables[ layer ]['*NameStack'] ):
84                                                 partialLayersInfo += "//     {0}\n//       {1}\n".format( name, file )
85
86
87                 ## Information ##
88                 self.fill_dict['Information']  = "// This file was generated by the kll compiler, DO NOT EDIT.\n"
89                 self.fill_dict['Information'] += "// Generation Date:    {0}\n".format( date.today() )
90                 self.fill_dict['Information'] += "// KLL Backend:        {0}\n".format( "kiibohd" )
91                 self.fill_dict['Information'] += "// KLL Git Rev:        {0}\n".format( gitRev )
92                 self.fill_dict['Information'] += "// KLL Git Changes:{0}".format( gitChangesStr )
93                 self.fill_dict['Information'] += "// Compiler arguments:\n{0}".format( compilerArgs )
94                 self.fill_dict['Information'] += "//\n"
95                 self.fill_dict['Information'] += "// - Base Layer -\n{0}".format( baseLayoutInfo )
96                 self.fill_dict['Information'] += "// - Default Layer -\n{0}".format( defaultLayerInfo )
97                 self.fill_dict['Information'] += "// - Partial Layers -\n{0}".format( partialLayersInfo )
98
99
100                 ## Variable Information ##
101                 self.fill_dict['VariableInformation'] = ""
102
103                 # Iterate through the variables, output, and indicate the last file that modified it's value
104                 # Output separate tables per file, per table and overall
105                 # TODO
106
107
108                 ## Defines ##
109                 self.fill_dict['Defines'] = ""
110
111                 # Iterate through defines and lookup the variables
112                 for define in variables.defines.keys():
113                         if define in variables.overallVariables.keys():
114                                 self.fill_dict['Defines'] += "\n#define {0} {1}".format( variables.defines[ define ], variables.overallVariables[ define ] )
115                         else:
116                                 print( "{0} '{1}' not defined...".format( WARNING, define ) )
117
118
119                 ## Capabilities ##
120                 self.fill_dict['CapabilitiesList'] = "const Capability CapabilitiesList[] = {\n"
121
122                 # Keys are pre-sorted
123                 for key in capabilities.keys():
124                         funcName = capabilities.funcName( key )
125                         argByteWidth = capabilities.totalArgBytes( key )
126                         self.fill_dict['CapabilitiesList'] += "\t{{ {0}, {1} }},\n".format( funcName, argByteWidth )
127
128                 self.fill_dict['CapabilitiesList'] += "};"
129
130
131                 ## Results Macros ##
132                 self.fill_dict['ResultMacros'] = ""
133
134                 # Iterate through each of the result macros
135                 for result in range( 0, len( macros.resultsIndexSorted ) ):
136                         self.fill_dict['ResultMacros'] += "Guide_RM( {0} ) = {{ ".format( result )
137
138                         # Add the result macro capability index guide (including capability arguments)
139                         # See kiibohd controller Macros/PartialMap/kll.h for exact formatting details
140                         for sequence in range( 0, len( macros.resultsIndexSorted[ result ] ) ):
141                                 # If the sequence is longer than 1, prepend a sequence spacer
142                                 # Needed for USB behaviour, otherwise, repeated keys will not work
143                                 if sequence > 0:
144                                         # <single element>, <usbCodeSend capability>, <USB Code 0x00>
145                                         self.fill_dict['ResultMacros'] += "1, {0}, 0x00, ".format( capabilities.getIndex( self.usbCodeCapability() ) )
146
147                                 # For each combo in the sequence, add the length of the combo
148                                 self.fill_dict['ResultMacros'] += "{0}, ".format( len( macros.resultsIndexSorted[ result ][ sequence ] ) )
149
150                                 # For each combo, add each of the capabilities used and their arguments
151                                 for combo in range( 0, len( macros.resultsIndexSorted[ result ][ sequence ] ) ):
152                                         resultItem = macros.resultsIndexSorted[ result ][ sequence ][ combo ]
153
154                                         # Add the capability index
155                                         self.fill_dict['ResultMacros'] += "{0}, ".format( capabilities.getIndex( resultItem[0] ) )
156
157                                         # Add each of the arguments of the capability
158                                         for arg in range( 0, len( resultItem[1] ) ):
159                                                 self.fill_dict['ResultMacros'] += "{0}, ".format( resultItem[1][ arg ] )
160
161                         # If sequence is longer than 1, append a sequence spacer at the end of the sequence
162                         # Required by USB to end at sequence without holding the key down
163                         if len( macros.resultsIndexSorted[ result ] ) > 1:
164                                 # <single element>, <usbCodeSend capability>, <USB Code 0x00>
165                                 self.fill_dict['ResultMacros'] += "1, {0}, 0x00, ".format( capabilities.getIndex( self.usbCodeCapability() ) )
166
167                         # Add list ending 0 and end of list
168                         self.fill_dict['ResultMacros'] += "0 };\n"
169                 self.fill_dict['ResultMacros'] = self.fill_dict['ResultMacros'][:-1] # Remove last newline
170
171
172                 ## Result Macro List ##
173                 self.fill_dict['ResultMacroList'] = "const ResultMacro ResultMacroList[] = {\n"
174
175                 # Iterate through each of the result macros
176                 for result in range( 0, len( macros.resultsIndexSorted ) ):
177                         self.fill_dict['ResultMacroList'] += "\tDefine_RM( {0} ),\n".format( result )
178                 self.fill_dict['ResultMacroList'] += "};"
179
180
181                 ## Result Macro Record ##
182                 self.fill_dict['ResultMacroRecord'] = "ResultMacroRecord ResultMacroRecordList[ ResultMacroNum ];"
183
184
185                 ## Trigger Macros ##
186                 self.fill_dict['TriggerMacros'] = ""
187
188                 # Iterate through each of the trigger macros
189                 for trigger in range( 0, len( macros.triggersIndexSorted ) ):
190                         self.fill_dict['TriggerMacros'] += "Guide_TM( {0} ) = {{ ".format( trigger )
191
192                         # Add the trigger macro scan code guide
193                         # See kiibohd controller Macros/PartialMap/kll.h for exact formatting details
194                         for sequence in range( 0, len( macros.triggersIndexSorted[ trigger ][ 0 ] ) ):
195                                 # For each combo in the sequence, add the length of the combo
196                                 self.fill_dict['TriggerMacros'] += "{0}, ".format( len( macros.triggersIndexSorted[ trigger ][0][ sequence ] ) )
197
198                                 # For each combo, add the key type, key state and scan code
199                                 for combo in range( 0, len( macros.triggersIndexSorted[ trigger ][ 0 ][ sequence ] ) ):
200                                         triggerItem = macros.triggersIndexSorted[ trigger ][ 0 ][ sequence ][ combo ]
201
202                                         # TODO Add support for Analog keys
203                                         # TODO Add support for LED states
204                                         self.fill_dict['TriggerMacros'] += "0x00, 0x01, 0x{0:02X}, ".format( triggerItem )
205
206                         # Add list ending 0 and end of list
207                         self.fill_dict['TriggerMacros'] += "0 };\n"
208                 self.fill_dict['TriggerMacros'] = self.fill_dict['TriggerMacros'][ :-1 ] # Remove last newline
209
210
211                 ## Trigger Macro List ##
212                 self.fill_dict['TriggerMacroList'] = "const TriggerMacro TriggerMacroList[] = {\n"
213
214                 # Iterate through each of the trigger macros
215                 for trigger in range( 0, len( macros.triggersIndexSorted ) ):
216                         # Use TriggerMacro Index, and the corresponding ResultMacro Index
217                         self.fill_dict['TriggerMacroList'] += "\tDefine_TM( {0}, {1} ),\n".format( trigger, macros.triggersIndexSorted[ trigger ][1] )
218                 self.fill_dict['TriggerMacroList'] += "};"
219
220
221                 ## Trigger Macro Record ##
222                 self.fill_dict['TriggerMacroRecord'] = "TriggerMacroRecord TriggerMacroRecordList[ TriggerMacroNum ];"
223
224
225                 ## Max Scan Code ##
226                 self.fill_dict['MaxScanCode'] = "#define MaxScanCode 0x{0:X}".format( macros.overallMaxScanCode )
227
228
229                 ## Default Layer and Default Layer Scan Map ##
230                 self.fill_dict['DefaultLayerTriggerList'] = ""
231                 self.fill_dict['DefaultLayerScanMap'] = "const nat_ptr_t *default_scanMap[] = {\n"
232
233                 # Iterate over triggerList and generate a C trigger array for the default map and default map array
234                 for triggerList in range( macros.firstScanCode[ 0 ], len( macros.triggerList[ 0 ] ) ):
235                         # Generate ScanCode index and triggerList length
236                         self.fill_dict['DefaultLayerTriggerList'] += "Define_TL( default, 0x{0:02X} ) = {{ {1}".format( triggerList, len( macros.triggerList[ 0 ][ triggerList ] ) )
237
238                         # Add scanCode trigger list to Default Layer Scan Map
239                         self.fill_dict['DefaultLayerScanMap'] += "default_tl_0x{0:02X}, ".format( triggerList )
240
241                         # Add each item of the trigger list
242                         for trigger in macros.triggerList[ 0 ][ triggerList ]:
243                                 self.fill_dict['DefaultLayerTriggerList'] += ", {0}".format( trigger )
244
245                         self.fill_dict['DefaultLayerTriggerList'] += " };\n"
246                 self.fill_dict['DefaultLayerTriggerList'] = self.fill_dict['DefaultLayerTriggerList'][:-1] # Remove last newline
247                 self.fill_dict['DefaultLayerScanMap'] = self.fill_dict['DefaultLayerScanMap'][:-2] # Remove last comma and space
248                 self.fill_dict['DefaultLayerScanMap'] += "\n};"
249
250
251                 ## Partial Layers and Partial Layer Scan Maps ##
252                 self.fill_dict['PartialLayerTriggerLists'] = ""
253                 self.fill_dict['PartialLayerScanMaps'] = ""
254
255                 # Iterate over each of the layers, excluding the default layer
256                 for layer in range( 1, len( macros.triggerList ) ):
257                         # Prepare each layer
258                         self.fill_dict['PartialLayerScanMaps'] += "// Partial Layer {0}\n".format( layer )
259                         self.fill_dict['PartialLayerScanMaps'] += "const nat_ptr_t *layer{0}_scanMap[] = {{\n".format( layer )
260                         self.fill_dict['PartialLayerTriggerLists'] += "// Partial Layer {0}\n".format( layer )
261
262                         # Iterate over triggerList and generate a C trigger array for the layer
263                         for triggerList in range( macros.firstScanCode[ layer ], len( macros.triggerList[ layer ] ) ):
264                                 # Generate ScanCode index and triggerList length
265                                 self.fill_dict['PartialLayerTriggerLists'] += "Define_TL( layer{0}, 0x{1:02X} ) = {{ {2}".format( layer, triggerList, len( macros.triggerList[ layer ][ triggerList ] ) )
266
267                                 # Add scanCode trigger list to Default Layer Scan Map
268                                 self.fill_dict['PartialLayerScanMaps'] += "layer{0}_tl_0x{1:02X}, ".format( layer, triggerList )
269
270                                 # Add each item of the trigger list
271                                 for trigger in macros.triggerList[ layer ][ triggerList ]:
272                                         self.fill_dict['PartialLayerTriggerLists'] += ", {0}".format( trigger )
273
274                                 self.fill_dict['PartialLayerTriggerLists'] += " };\n"
275                         self.fill_dict['PartialLayerTriggerLists'] += "\n"
276                         self.fill_dict['PartialLayerScanMaps'] = self.fill_dict['PartialLayerScanMaps'][:-2] # Remove last comma and space
277                         self.fill_dict['PartialLayerScanMaps'] += "\n};\n\n"
278                 self.fill_dict['PartialLayerTriggerLists'] = self.fill_dict['PartialLayerTriggerLists'][:-2] # Remove last 2 newlines
279                 self.fill_dict['PartialLayerScanMaps'] = self.fill_dict['PartialLayerScanMaps'][:-2] # Remove last 2 newlines
280
281
282                 ## Layer Index List ##
283                 self.fill_dict['LayerIndexList'] = "const Layer LayerIndex[] = {\n"
284
285                 # Iterate over each layer, adding it to the list
286                 for layer in range( 0, len( macros.triggerList ) ):
287                         # Lookup first scancode in map
288                         firstScanCode = macros.firstScanCode[ layer ]
289
290                         # Generate stacked name
291                         stackName = ""
292                         if '*NameStack' in variables.layerVariables[ layer ].keys():
293                                 for name in range( 0, len( variables.layerVariables[ layer ]['*NameStack'] ) ):
294                                         stackName += "{0} + ".format( variables.layerVariables[ layer ]['*NameStack'][ name ] )
295                                 stackName = stackName[:-3]
296
297                         # Default map is a special case, always the first index
298                         if layer == 0:
299                                 self.fill_dict['LayerIndexList'] += '\tLayer_IN( default_scanMap, "D: {1}", 0x{0:02X} ),\n'.format( firstScanCode, stackName )
300                         else:
301                                 self.fill_dict['LayerIndexList'] += '\tLayer_IN( layer{0}_scanMap, "{0}: {2}", 0x{1:02X} ),\n'.format( layer, firstScanCode, stackName )
302                 self.fill_dict['LayerIndexList'] += "};"
303
304
305                 ## Layer State ##
306                 self.fill_dict['LayerState'] = "uint8_t LayerState[ LayerNum ];"
307