KLL Compiler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

kiibohd.py 14KB

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