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.

containers.py 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!/usr/bin/env python3
  2. # KLL Compiler Containers
  3. #
  4. # Copyright (C) 2014 by Jacob Alexander
  5. #
  6. # This file is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This file is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this file. If not, see <http://www.gnu.org/licenses/>.
  18. ### Imports ###
  19. ### Decorators ###
  20. ## Print Decorator Variables
  21. ERROR = '\033[5;1;31mERROR\033[0m:'
  22. ### Parsing ###
  23. ## Containers
  24. class Capabilities:
  25. # Container for capabilities dictionary and convenience functions
  26. def __init__( self ):
  27. self.capabilities = dict()
  28. def __getitem__( self, name ):
  29. return self.capabilities[ name ]
  30. def __setitem__( self, name, contents ):
  31. self.capabilities[ name ] = contents
  32. def __repr__( self ):
  33. return "Capabilities => {0}\nIndexed Capabilities => {1}".format( self.capabilities, sorted( self.capabilities, key = self.capabilities.get ) )
  34. # Total bytes needed to store arguments
  35. def totalArgBytes( self, name ):
  36. totalBytes = 0
  37. # Iterate over the arguments, summing the total bytes
  38. for arg in self.capabilities[ name ][ 1 ]:
  39. totalBytes += int( arg[ 1 ] )
  40. return totalBytes
  41. # Name of the capability function
  42. def funcName( self, name ):
  43. return self.capabilities[ name ][ 0 ]
  44. # Only valid while dictionary keys are not added/removed
  45. def getIndex( self, name ):
  46. return sorted( self.capabilities, key = self.capabilities.get ).index( name )
  47. def getName( self, index ):
  48. return sorted( self.capabilities, key = self.capabilities.get )[ index ]
  49. def keys( self ):
  50. return sorted( self.capabilities, key = self.capabilities.get )
  51. class Macros:
  52. # Container for Trigger Macro : Result Macro correlation
  53. # Layer selection for generating TriggerLists
  54. #
  55. # Only convert USB Code list once all the ResultMacros have been accumulated (does a macro reduction; not reversible)
  56. # Two staged list for ResultMacros:
  57. # 1) USB Code/Non-converted (may contain capabilities)
  58. # 2) Capabilities
  59. def __init__( self ):
  60. # Default layer (0)
  61. self.layer = 0
  62. # Macro Storage
  63. self.macros = [ dict() ]
  64. # Correlated Macro Data
  65. self.resultsIndex = dict()
  66. self.triggersIndex = dict()
  67. self.resultsIndexSorted = []
  68. self.triggersIndexSorted = []
  69. self.triggerList = []
  70. self.maxScanCode = []
  71. # USBCode Assignment Cache
  72. self.assignmentCache = []
  73. def __repr__( self ):
  74. return "{0}".format( self.macros )
  75. def addLayer( self ):
  76. # Increment layer count, and append another macros dictionary
  77. self.layer += 1
  78. self.macros.append( dict() )
  79. # Use for ScanCode trigger macros
  80. def appendScanCode( self, trigger, result ):
  81. if not trigger in self.macros[ self.layer ]:
  82. self.replaceScanCode( trigger, result )
  83. else:
  84. self.macros[ self.layer ][ trigger ].append( result )
  85. # Remove the given trigger/result pair
  86. def removeScanCode( self, trigger, result ):
  87. # Remove all instances of the given trigger/result pair
  88. while result in self.macros[ self.layer ][ trigger ]:
  89. self.macros[ self.layer ][ trigger ].remove( result )
  90. # Replaces the given trigger with the given result
  91. # If multiple results for a given trigger, clear, then add
  92. def replaceScanCode( self, trigger, result ):
  93. self.macros[ self.layer ][ trigger ] = [ result ]
  94. # Return a list of ScanCode triggers with the given USB Code trigger
  95. def lookupUSBCodes( self, usbCode ):
  96. scanCodeList = []
  97. # Scan current layer for USB Codes
  98. for macro in self.macros[ self.layer ].keys():
  99. if usbCode in self.macros[ self.layer ][ macro ]:
  100. scanCodeList.append( macro )
  101. return scanCodeList
  102. # Cache USBCode Assignment
  103. def cacheAssignment( self, operator, scanCode, result ):
  104. self.assignmentCache.append( [ operator, scanCode, result ] )
  105. # Assign cached USBCode Assignments
  106. def replayCachedAssignments( self ):
  107. # Iterate over each item in the assignment cache
  108. for item in self.assignmentCache:
  109. # Check operator, and choose the specified assignment action
  110. # Append Case
  111. if item[0] == ":+":
  112. self.appendScanCode( item[1], item[2] )
  113. # Remove Case
  114. elif item[0] == ":-":
  115. self.removeScanCode( item[1], item[2] )
  116. # Replace Case
  117. elif item[0] == ":":
  118. self.replaceScanCode( item[1], item[2] )
  119. # Clear assignment cache
  120. self.assignmentCache = []
  121. # Generate/Correlate Layers
  122. def generate( self ):
  123. self.generateIndices()
  124. self.sortIndexLists()
  125. self.generateTriggerLists()
  126. # Generates Index of Results and Triggers
  127. def generateIndices( self ):
  128. # Iterate over every trigger result, and add to the resultsIndex and triggersIndex
  129. for layer in range( 0, len( self.macros ) ):
  130. for trigger in self.macros[ layer ].keys():
  131. # Each trigger has a list of results
  132. for result in self.macros[ layer ][ trigger ]:
  133. # Only add, with an index, if result hasn't been added yet
  134. if not result in self.resultsIndex:
  135. self.resultsIndex[ result ] = len( self.resultsIndex )
  136. # Then add a trigger for each result, if trigger hasn't been added yet
  137. triggerItem = tuple( [ trigger, self.resultsIndex[ result ] ] )
  138. if not triggerItem in self.triggersIndex:
  139. self.triggersIndex[ triggerItem ] = len( self.triggersIndex )
  140. # Sort Index Lists using the indices rather than triggers/results
  141. def sortIndexLists( self ):
  142. self.resultsIndexSorted = [ None ] * len( self.resultsIndex )
  143. # Iterate over the resultsIndex and sort by index
  144. for result in self.resultsIndex.keys():
  145. self.resultsIndexSorted[ self.resultsIndex[ result ] ] = result
  146. self.triggersIndexSorted = [ None ] * len( self.triggersIndex )
  147. # Iterate over the triggersIndex and sort by index
  148. for trigger in self.triggersIndex.keys():
  149. self.triggersIndexSorted[ self.triggersIndex[ trigger ] ] = trigger
  150. # Generates Trigger Lists per layer using index lists
  151. def generateTriggerLists( self ):
  152. for layer in range( 0, len( self.macros ) ):
  153. # Set max scancode to 0xFF (255)
  154. # But keep track of the actual max scancode and reduce the list size
  155. self.triggerList.append( [ [] ] * 0xFF )
  156. self.maxScanCode.append( 0x00 )
  157. # Iterate through triggersIndex to locate necessary ScanCodes and corresponding triggerIndex
  158. for triggerItem in self.triggersIndex.keys():
  159. # Iterate over the trigger portion of the triggerItem (other part is the index)
  160. for sequence in triggerItem[ 0 ]:
  161. for combo in sequence:
  162. # Append triggerIndex for each found scanCode of the Trigger List
  163. # Do not re-add if triggerIndex is already in the Trigger List
  164. if not triggerItem[1] in self.triggerList[ layer ][ combo ]:
  165. # Append is working strangely with list pre-initialization
  166. # Doing a 0 check replacement instead -HaaTa
  167. if len( self.triggerList[ layer ][ combo ] ) == 0:
  168. self.triggerList[ layer ][ combo ] = [ triggerItem[ 1 ] ]
  169. else:
  170. self.triggerList[ layer ][ combo ].append( triggerItem[1] )
  171. # Look for max Scan Code
  172. if combo > self.maxScanCode[ layer ]:
  173. self.maxScanCode[ layer ] = combo
  174. # Shrink triggerList to actual max size
  175. self.triggerList[ layer ] = self.triggerList[ layer ][ : self.maxScanCode[ layer ] + 1 ]
  176. # Determine overall maxScanCode
  177. self.overallMaxScanCode = 0x00
  178. for maxVal in self.maxScanCode:
  179. if maxVal > self.overallMaxScanCode:
  180. self.overallMaxScanCode = maxVal