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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 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. # Modifying Python Path, which is dumb, but the only way to import up one directory...
  25. sys.path.append( os.path.expanduser('..') )
  26. from kll_lib.containers import *
  27. ### Decorators ###
  28. ## Print Decorator Variables
  29. ERROR = '\033[5;1;31mERROR\033[0m:'
  30. ### Classes ###
  31. class Backend:
  32. # Initializes backend
  33. # Looks for template file and builds list of fill tags
  34. def __init__( self, templatePath ):
  35. # Does template exist?
  36. if not os.path.isfile( templatePath ):
  37. print ( "{0} '{1}' does not exist...".format( ERROR, templatePath ) )
  38. sys.exit( 1 )
  39. self.templatePath = templatePath
  40. self.fill_dict = dict()
  41. # Generate list of fill tags
  42. self.tagList = []
  43. with open( templatePath, 'r' ) as openFile:
  44. for line in openFile:
  45. match = re.findall( '<\|([^|>]+)\|>', line )
  46. for item in match:
  47. self.tagList.append( item )
  48. # USB Code Capability Name
  49. def usbCodeCapability( self ):
  50. return "usbKeyOut";
  51. # Processes content for fill tags and does any needed dataset calculations
  52. def process( self, capabilities, macros ):
  53. ## Capabilities ##
  54. self.fill_dict['CapabilitiesList'] = "const Capability CapabilitiesList[] = {\n"
  55. # Keys are pre-sorted
  56. for key in capabilities.keys():
  57. funcName = capabilities.funcName( key )
  58. argByteWidth = capabilities.totalArgBytes( key )
  59. self.fill_dict['CapabilitiesList'] += "\t{{ {0}, {1} }},\n".format( funcName, argByteWidth )
  60. self.fill_dict['CapabilitiesList'] += "};"
  61. ## Results Macros ##
  62. self.fill_dict['ResultMacros'] = ""
  63. # Iterate through each of the result macros
  64. for result in range( 0, len( macros.resultsIndexSorted ) ):
  65. self.fill_dict['ResultMacros'] += "Guide_RM( {0} ) = {{ ".format( result )
  66. # Add the result macro capability index guide (including capability arguments)
  67. # See kiibohd controller Macros/PartialMap/kll.h for exact formatting details
  68. for sequence in range( 0, len( macros.resultsIndexSorted[ result ] ) ):
  69. # For each combo in the sequence, add the length of the combo
  70. self.fill_dict['ResultMacros'] += "{0}, ".format( len( macros.resultsIndexSorted[ result ][ sequence ] ) )
  71. # For each combo, add each of the capabilities used and their arguments
  72. for combo in range( 0, len( macros.resultsIndexSorted[ result ][ sequence ] ) ):
  73. resultItem = macros.resultsIndexSorted[ result ][ sequence ][ combo ]
  74. # Add the capability index
  75. self.fill_dict['ResultMacros'] += "{0}, ".format( capabilities.getIndex( resultItem[0] ) )
  76. # Add each of the arguments of the capability
  77. for arg in range( 0, len( resultItem[1] ) ):
  78. self.fill_dict['ResultMacros'] += "0x{0:02X}, ".format( resultItem[1][ arg ] )
  79. # Add list ending 0 and end of list
  80. self.fill_dict['ResultMacros'] += "0 };\n"
  81. self.fill_dict['ResultMacros'] = self.fill_dict['ResultMacros'][:-1] # Remove last newline
  82. ## Result Macro List ##
  83. self.fill_dict['ResultMacroList'] = "ResultMacro ResultMacroList[] = {\n"
  84. # Iterate through each of the result macros
  85. for result in range( 0, len( macros.resultsIndexSorted ) ):
  86. self.fill_dict['ResultMacroList'] += "\tDefine_RM( {0} ),\n".format( result )
  87. self.fill_dict['ResultMacroList'] += "};"
  88. ## Trigger Macros ##
  89. self.fill_dict['TriggerMacros'] = ""
  90. # Iterate through each of the trigger macros
  91. for trigger in range( 0, len( macros.triggersIndexSorted ) ):
  92. self.fill_dict['TriggerMacros'] += "Guide_TM( {0} ) = {{ ".format( trigger )
  93. # Add the trigger macro scan code guide
  94. # See kiibohd controller Macros/PartialMap/kll.h for exact formatting details
  95. for sequence in range( 0, len( macros.triggersIndexSorted[ trigger ][ 0 ] ) ):
  96. # For each combo in the sequence, add the length of the combo
  97. self.fill_dict['TriggerMacros'] += "{0}, ".format( len( macros.triggersIndexSorted[ trigger ][0][ sequence ] ) )
  98. # For each combo, add the key type, key state and scan code
  99. for combo in range( 0, len( macros.triggersIndexSorted[ trigger ][ 0 ][ sequence ] ) ):
  100. triggerItem = macros.triggersIndexSorted[ trigger ][ 0 ][ sequence ][ combo ]
  101. # TODO Add support for Analog keys
  102. # TODO Add support for LED states
  103. self.fill_dict['TriggerMacros'] += "0x00, 0x01, 0x{0:02X}, ".format( triggerItem )
  104. # Add list ending 0 and end of list
  105. self.fill_dict['TriggerMacros'] += "0 };\n"
  106. self.fill_dict['TriggerMacros'] = self.fill_dict['TriggerMacros'][ :-1 ] # Remove last newline
  107. ## Trigger Macro List ##
  108. self.fill_dict['TriggerMacroList'] = "TriggerMacro TriggerMacroList[] = {\n"
  109. # Iterate through each of the trigger macros
  110. for trigger in range( 0, len( macros.triggersIndexSorted ) ):
  111. # Use TriggerMacro Index, and the corresponding ResultMacro Index
  112. self.fill_dict['TriggerMacroList'] += "\tDefine_TM( {0}, {1} ),\n".format( trigger, macros.triggersIndexSorted[ trigger ][1] )
  113. self.fill_dict['TriggerMacroList'] += "};"
  114. ## Max Scan Code ##
  115. self.fill_dict['MaxScanCode'] = "#define MaxScanCode 0x{0:X}".format( macros.overallMaxScanCode )
  116. ## Default Layer and Default Layer Scan Map ##
  117. self.fill_dict['DefaultLayerTriggerList'] = ""
  118. self.fill_dict['DefaultLayerScanMap'] = "const unsigned int *default_scanMap[] = {\n"
  119. # Iterate over triggerList and generate a C trigger array for the default map and default map array
  120. for triggerList in range( 0, len( macros.triggerList[ 0 ] ) ):
  121. # Generate ScanCode index and triggerList length
  122. self.fill_dict['DefaultLayerTriggerList'] += "Define_TL( default, 0x{0:02X} ) = {{ {1}".format( triggerList, len( macros.triggerList[ 0 ][ triggerList ] ) )
  123. # Add scanCode trigger list to Default Layer Scan Map
  124. self.fill_dict['DefaultLayerScanMap'] += "default_tl_0x{0:02X}, ".format( triggerList )
  125. # Add each item of the trigger list
  126. for trigger in macros.triggerList[ 0 ][ triggerList ]:
  127. self.fill_dict['DefaultLayerTriggerList'] += ", {0}".format( trigger )
  128. self.fill_dict['DefaultLayerTriggerList'] += " };\n"
  129. self.fill_dict['DefaultLayerTriggerList'] = self.fill_dict['DefaultLayerTriggerList'][:-1] # Remove last newline
  130. self.fill_dict['DefaultLayerScanMap'] = self.fill_dict['DefaultLayerScanMap'][:-2] # Remove last comma and space
  131. self.fill_dict['DefaultLayerScanMap'] += "\n};"
  132. ## Partial Layers and Partial Layer Scan Maps ##
  133. self.fill_dict['PartialLayerTriggerLists'] = ""
  134. self.fill_dict['PartialLayerScanMaps'] = ""
  135. # Iterate over each of the layers, excluding the default layer
  136. for layer in range( 1, len( macros.triggerList ) ):
  137. # Prepare each layer
  138. self.fill_dict['PartialLayerScanMaps'] += "// Partial Layer {0}\n".format( layer )
  139. self.fill_dict['PartialLayerScanMaps'] += "const unsigned int *layer{0}_scanMap[] = {{\n".format( layer )
  140. self.fill_dict['PartialLayerTriggerLists'] += "// Partial Layer {0}\n".format( layer )
  141. # Iterate over triggerList and generate a C trigger array for the layer
  142. for triggerList in range( 0, len( macros.triggerList[ layer ] ) ):
  143. # Generate ScanCode index and triggerList length
  144. self.fill_dict['PartialLayerTriggerLists'] += "Define_TL( layer{0}, 0x{1:02X} ) = {{ {2}".format( layer, triggerList, len( macros.triggerList[ 0 ][ triggerList ] ) )
  145. # Add scanCode trigger list to Default Layer Scan Map
  146. self.fill_dict['PartialLayerScanMaps'] += "layer{0}_tl_0x{1:02X}, ".format( layer, triggerList )
  147. # Add each item of the trigger list
  148. for trigger in macros.triggerList[ 0 ][ triggerList ]:
  149. self.fill_dict['PartialLayerTriggerLists'] += ", {0}".format( trigger )
  150. self.fill_dict['PartialLayerTriggerLists'] += " };\n"
  151. self.fill_dict['PartialLayerTriggerLists'] += "\n"
  152. self.fill_dict['PartialLayerScanMaps'] = self.fill_dict['PartialLayerScanMaps'][:-2] # Remove last comma and space
  153. self.fill_dict['PartialLayerScanMaps'] += "\n};\n\n"
  154. self.fill_dict['PartialLayerTriggerLists'] = self.fill_dict['PartialLayerTriggerLists'][:-2] # Remove last 2 newlines
  155. self.fill_dict['PartialLayerScanMaps'] = self.fill_dict['PartialLayerScanMaps'][:-2] # Remove last 2 newlines
  156. ## Layer Index List ##
  157. self.fill_dict['LayerIndexList'] = "Layer LayerIndex[] = {\n"
  158. # Iterate over each layer, adding it to the list
  159. for layer in range( 0, len( macros.triggerList ) ):
  160. # Default map is a special case, always the first index
  161. # TODO Fix names
  162. if layer == 0:
  163. self.fill_dict['LayerIndexList'] += '\tLayer_IN( default_scanMap, "DefaultMap" ),\n'
  164. else:
  165. self.fill_dict['LayerIndexList'] += '\tLayer_IN( layer{0}_scanMap, "Layer {0}" ),\n'.format( layer )
  166. self.fill_dict['LayerIndexList'] += "};"
  167. # Generates the output keymap with fill tags filled
  168. def generate( self, filepath ):
  169. # Process each line of the template, outputting to the target path
  170. with open( filepath, 'w' ) as outputFile:
  171. with open( self.templatePath, 'r' ) as templateFile:
  172. for line in templateFile:
  173. # TODO Support multiple replacements per line
  174. # TODO Support replacement with other text inline
  175. match = re.findall( '<\|([^|>]+)\|>', line )
  176. # If match, replace with processed variable
  177. if match:
  178. outputFile.write( self.fill_dict[ match[ 0 ] ] )
  179. outputFile.write("\n")
  180. # Otherwise, just append template to output file
  181. else:
  182. outputFile.write( line )