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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. # List of non-complex ResultMacros
  108. # These are defined as a single combo (sequence depth of 1)
  109. simpleResultMacros = []
  110. self.fill_dict['ResultMacroGuides'] = ""
  111. self.fill_dict['ResultMacroRecords'] = ""
  112. # Iterate through each of the result macros
  113. for result in range( 0, len( macros.resultsIndexSorted ) ):
  114. self.fill_dict['ResultMacroGuides'] += "Guide_RM( {0} ) = {{ ".format( result )
  115. # Determine if the ResultMacro is simple
  116. if len( macros.resultsIndexSorted[ result ] ) == 1:
  117. simpleResultMacros.append( result )
  118. # Otherwise add a Record
  119. else:
  120. self.fill_dict['ResultMacroRecords'] += "Record_RM( {0} );\n".format( result )
  121. # Add the result macro capability index guide (including capability arguments)
  122. # See kiibohd controller Macros/PartialMap/kll.h for exact formatting details
  123. for sequence in range( 0, len( macros.resultsIndexSorted[ result ] ) ):
  124. # If the sequence is longer than 1, prepend a sequence spacer
  125. # Needed for USB behaviour, otherwise, repeated keys will not work
  126. if sequence > 0:
  127. # <single element>, <usbCodeSend capability>, <USB Code 0x00>
  128. self.fill_dict['ResultMacroGuides'] += "1, {0}, 0x00, ".format( capabilities.getIndex( self.usbCodeCapability() ) )
  129. # For each combo in the sequence, add the length of the combo
  130. self.fill_dict['ResultMacroGuides'] += "{0}, ".format( len( macros.resultsIndexSorted[ result ][ sequence ] ) )
  131. # For each combo, add each of the capabilities used and their arguments
  132. for combo in range( 0, len( macros.resultsIndexSorted[ result ][ sequence ] ) ):
  133. resultItem = macros.resultsIndexSorted[ result ][ sequence ][ combo ]
  134. # Add the capability index
  135. self.fill_dict['ResultMacroGuides'] += "{0}, ".format( capabilities.getIndex( resultItem[0] ) )
  136. # Add each of the arguments of the capability
  137. for arg in range( 0, len( resultItem[1] ) ):
  138. self.fill_dict['ResultMacroGuides'] += "{0}, ".format( resultItem[1][ arg ] )
  139. # If sequence is longer than 1, append a sequence spacer at the end of the sequence
  140. # Required by USB to end at sequence without holding the key down
  141. if len( macros.resultsIndexSorted[ result ] ) > 1:
  142. # <single element>, <usbCodeSend capability>, <USB Code 0x00>
  143. self.fill_dict['ResultMacroGuides'] += "1, {0}, 0x00, ".format( capabilities.getIndex( self.usbCodeCapability() ) )
  144. # Add list ending 0 and end of list
  145. self.fill_dict['ResultMacroGuides'] += "0 };\n"
  146. self.fill_dict['ResultMacroGuides'] = self.fill_dict['ResultMacroGuides'][:-1] # Remove last newline
  147. # Remove last newline if a record was added
  148. if self.fill_dict['ResultMacroRecords'] != "":
  149. self.fill_dict['ResultMacroRecords'] = self.fill_dict['ResultMacroRecords'][:-1]
  150. else:
  151. self.fill_dict['ResultMacroRecords'] = "// <None>"
  152. ## Result Macro List ##
  153. self.fill_dict['ResultMacroList'] = "const ResultMacro ResultMacroList[] = {\n"
  154. # Iterate through each of the result macros
  155. for result in range( 0, len( macros.resultsIndexSorted ) ):
  156. # Check if this is a simple ResultMacro
  157. self.fill_dict['ResultMacroList'] += "\tDefine_RM_{1}( {0} ),\n".format(
  158. result,
  159. "Simple"
  160. if result in simpleResultMacros else
  161. "Normal"
  162. )
  163. self.fill_dict['ResultMacroList'] += "};"
  164. ## Trigger Macros ##
  165. # List of non-complex TriggerMacros
  166. # These are defined as a single combo (sequence depth of 1)
  167. simpleTriggerMacros = []
  168. self.fill_dict['TriggerMacroGuides'] = ""
  169. self.fill_dict['TriggerMacroRecords'] = ""
  170. # Iterate through each of the trigger macros
  171. for trigger in range( 0, len( macros.triggersIndexSorted ) ):
  172. self.fill_dict['TriggerMacroGuides'] += "Guide_TM( {0} ) = {{ ".format( trigger )
  173. # Determine if TriggerMacro is simple
  174. if len( macros.triggersIndexSorted[ trigger ][ 0 ] ) == 1:
  175. simpleTriggerMacros.append( trigger )
  176. # Otherwise add a Record
  177. else:
  178. self.fill_dict['TriggerMacroRecords'] += "Record_TM( {0} );\n".format( trigger )
  179. # Add the trigger macro scan code guide
  180. # See kiibohd controller Macros/PartialMap/kll.h for exact formatting details
  181. for sequence in range( 0, len( macros.triggersIndexSorted[ trigger ][ 0 ] ) ):
  182. # For each combo in the sequence, add the length of the combo
  183. self.fill_dict['TriggerMacroGuides'] += "{0}, ".format( len( macros.triggersIndexSorted[ trigger ][0][ sequence ] ) )
  184. # For each combo, add the key type, key state and scan code
  185. for combo in range( 0, len( macros.triggersIndexSorted[ trigger ][ 0 ][ sequence ] ) ):
  186. triggerItem = macros.triggersIndexSorted[ trigger ][ 0 ][ sequence ][ combo ]
  187. # TODO Add support for Analog keys
  188. # TODO Add support for LED states
  189. self.fill_dict['TriggerMacroGuides'] += "0x00, 0x01, 0x{0:02X}, ".format( triggerItem )
  190. # Add list ending 0 and end of list
  191. self.fill_dict['TriggerMacroGuides'] += "0 };\n"
  192. self.fill_dict['TriggerMacroGuides'] = self.fill_dict['TriggerMacroGuides'][:-1] # Remove last newline
  193. # Remove last newline if a record was added
  194. if self.fill_dict['TriggerMacroRecords'] != "":
  195. self.fill_dict['TriggerMacroRecords'] = self.fill_dict['TriggerMacroRecords'][:-1]
  196. else:
  197. self.fill_dict['TriggerMacroRecords'] = "// <None>"
  198. ## Trigger Macro List ##
  199. self.fill_dict['TriggerMacroList'] = "const TriggerMacro TriggerMacroList[] = {\n"
  200. # Iterate through each of the trigger macros
  201. for trigger in range( 0, len( macros.triggersIndexSorted ) ):
  202. # Use TriggerMacro Index, and the corresponding ResultMacro Index
  203. # Check if this is a simple TriggerMacro
  204. self.fill_dict['TriggerMacroList'] += "\tDefine_TM_{2}( {0}, {1} ),\n".format(
  205. trigger, macros.triggersIndexSorted[ trigger ][1],
  206. "Simple"
  207. if trigger in simpleTriggerMacros else
  208. "Normal"
  209. )
  210. self.fill_dict['TriggerMacroList'] += "};"
  211. ## Max Scan Code ##
  212. self.fill_dict['MaxScanCode'] = "#define MaxScanCode 0x{0:X}".format( macros.overallMaxScanCode )
  213. ## Default Layer and Default Layer Scan Map ##
  214. self.fill_dict['DefaultLayerTriggerList'] = ""
  215. self.fill_dict['DefaultLayerScanMap'] = "const nat_ptr_t *default_scanMap[] = {\n"
  216. # Iterate over triggerList and generate a C trigger array for the default map and default map array
  217. for triggerList in range( macros.firstScanCode[ 0 ], len( macros.triggerList[ 0 ] ) ):
  218. # Generate ScanCode index and triggerList length
  219. self.fill_dict['DefaultLayerTriggerList'] += "Define_TL( default, 0x{0:02X} ) = {{ {1}".format( triggerList, len( macros.triggerList[ 0 ][ triggerList ] ) )
  220. # Add scanCode trigger list to Default Layer Scan Map
  221. self.fill_dict['DefaultLayerScanMap'] += "default_tl_0x{0:02X}, ".format( triggerList )
  222. # Add each item of the trigger list
  223. for trigger in macros.triggerList[ 0 ][ triggerList ]:
  224. self.fill_dict['DefaultLayerTriggerList'] += ", {0}".format( trigger )
  225. self.fill_dict['DefaultLayerTriggerList'] += " };\n"
  226. self.fill_dict['DefaultLayerTriggerList'] = self.fill_dict['DefaultLayerTriggerList'][:-1] # Remove last newline
  227. self.fill_dict['DefaultLayerScanMap'] = self.fill_dict['DefaultLayerScanMap'][:-2] # Remove last comma and space
  228. self.fill_dict['DefaultLayerScanMap'] += "\n};"
  229. ## Partial Layers and Partial Layer Scan Maps ##
  230. self.fill_dict['PartialLayerTriggerLists'] = ""
  231. self.fill_dict['PartialLayerScanMaps'] = ""
  232. # Iterate over each of the layers, excluding the default layer
  233. for layer in range( 1, len( macros.triggerList ) ):
  234. # Prepare each layer
  235. self.fill_dict['PartialLayerScanMaps'] += "// Partial Layer {0}\n".format( layer )
  236. self.fill_dict['PartialLayerScanMaps'] += "const nat_ptr_t *layer{0}_scanMap[] = {{\n".format( layer )
  237. self.fill_dict['PartialLayerTriggerLists'] += "// Partial Layer {0}\n".format( layer )
  238. # Iterate over triggerList and generate a C trigger array for the layer
  239. for triggerList in range( macros.firstScanCode[ layer ], len( macros.triggerList[ layer ] ) ):
  240. # Generate ScanCode index and triggerList length
  241. self.fill_dict['PartialLayerTriggerLists'] += "Define_TL( layer{0}, 0x{1:02X} ) = {{ {2}".format( layer, triggerList, len( macros.triggerList[ layer ][ triggerList ] ) )
  242. # Add scanCode trigger list to Default Layer Scan Map
  243. self.fill_dict['PartialLayerScanMaps'] += "layer{0}_tl_0x{1:02X}, ".format( layer, triggerList )
  244. # Add each item of the trigger list
  245. for trigger in macros.triggerList[ layer ][ triggerList ]:
  246. self.fill_dict['PartialLayerTriggerLists'] += ", {0}".format( trigger )
  247. self.fill_dict['PartialLayerTriggerLists'] += " };\n"
  248. self.fill_dict['PartialLayerTriggerLists'] += "\n"
  249. self.fill_dict['PartialLayerScanMaps'] = self.fill_dict['PartialLayerScanMaps'][:-2] # Remove last comma and space
  250. self.fill_dict['PartialLayerScanMaps'] += "\n};\n\n"
  251. self.fill_dict['PartialLayerTriggerLists'] = self.fill_dict['PartialLayerTriggerLists'][:-2] # Remove last 2 newlines
  252. self.fill_dict['PartialLayerScanMaps'] = self.fill_dict['PartialLayerScanMaps'][:-2] # Remove last 2 newlines
  253. ## Layer Index List ##
  254. self.fill_dict['LayerIndexList'] = "const Layer LayerIndex[] = {\n"
  255. # Iterate over each layer, adding it to the list
  256. for layer in range( 0, len( macros.triggerList ) ):
  257. # Lookup first scancode in map
  258. firstScanCode = macros.firstScanCode[ layer ]
  259. # Generate stacked name
  260. stackName = ""
  261. if '*NameStack' in variables.layerVariables[ layer ].keys():
  262. for name in range( 0, len( variables.layerVariables[ layer ]['*NameStack'] ) ):
  263. stackName += "{0} + ".format( variables.layerVariables[ layer ]['*NameStack'][ name ] )
  264. stackName = stackName[:-3]
  265. # Default map is a special case, always the first index
  266. if layer == 0:
  267. self.fill_dict['LayerIndexList'] += '\tLayer_IN( default_scanMap, "D: {1}", 0x{0:02X} ),\n'.format( firstScanCode, stackName )
  268. else:
  269. self.fill_dict['LayerIndexList'] += '\tLayer_IN( layer{0}_scanMap, "{0}: {2}", 0x{1:02X} ),\n'.format( layer, firstScanCode, stackName )
  270. self.fill_dict['LayerIndexList'] += "};"
  271. ## Layer State ##
  272. self.fill_dict['LayerState'] = "uint8_t LayerState[ LayerNum ];"