+ // Lookup TriggerMacro
+ TriggerMacro *macro = &TriggerMacroList[ triggerMacroIndex ];
+
+ // Check if macro has finished and should be incremented sequence elements
+ if ( macro->state == TriggerMacro_Release )
+ {
+ macro->state = TriggerMacro_Waiting;
+ macro->pos = macro->pos + macro->guide[ macro->pos ] * TriggerGuideSize + 1;
+ }
+
+ // Current Macro position
+ unsigned int pos = macro->pos;
+
+ // Length of the combo being processed
+ uint8_t comboLength = macro->guide[ pos ] * TriggerGuideSize;
+
+ // If no combo items are left, remove the TriggerMacro from the pending list
+ if ( comboLength == 0 )
+ {
+ return TriggerMacroEval_Remove;
+ }
+
+ // Check if this is a long Trigger Macro
+ uint8_t longMacro = Macro_isLongTriggerMacro( macro );
+
+ // Iterate through the items in the combo, voting the on the key state
+ // If any of the pressed keys do not match, fail the macro
+ //
+ // The macro is waiting for input when in the TriggerMacro_Waiting state
+ // Once all keys have been pressed/held (only those keys), entered TriggerMacro_Press state (passing)
+ // Transition to the next combo (if it exists) when a single key is released (TriggerMacro_Release state)
+ // On scan after position increment, change to TriggerMacro_Waiting state
+ // TODO Add support for system LED states (NumLock, CapsLock, etc.)
+ // TODO Add support for analog key states
+ // TODO Add support for 0x00 Key state (not pressing a key, not all that useful in general)
+ // TODO Add support for Press/Hold/Release differentiation when evaluating (not sure if useful)
+ TriggerMacroVote overallVote = TriggerMacroVote_Invalid;
+ for ( uint8_t comboItem = pos + 1; comboItem < pos + comboLength + 1; comboItem += TriggerGuideSize )
+ {
+ // Assign TriggerGuide element (key type, state and scancode)
+ TriggerGuide *guide = (TriggerGuide*)(¯o->guide[ comboItem ]);
+
+ TriggerMacroVote vote = TriggerMacroVote_Invalid;
+ // Iterate through the key buffer, comparing to each key in the combo
+ for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
+ {
+ // Lookup key information
+ TriggerGuide *keyInfo = ¯oTriggerListBuffer[ key ];
+
+ // If vote is a pass (>= 0x08, no more keys in the combo need to be looked at)
+ // Also mask all of the non-passing votes
+ vote |= longMacro
+ ? Macro_evalLongTriggerMacroVote( keyInfo, guide )
+ : Macro_evalShortTriggerMacroVote( keyInfo, guide );
+ if ( vote >= TriggerMacroVote_Pass )
+ {
+ vote &= TriggerMacroVote_Release | TriggerMacroVote_PassRelease | TriggerMacroVote_Pass;
+ break;
+ }
+ }
+
+ // If no pass vote was found after scanning all of the keys
+ // Fail the combo, if this is a short macro (long macros already will have a fail vote)
+ if ( !longMacro && vote < TriggerMacroVote_Pass )
+ vote |= TriggerMacroVote_Fail;
+
+ // After voting, append to overall vote
+ overallVote |= vote;
+ }
+
+ // If no pass vote was found after scanning the entire combo
+ // And this is the first position in the combo, just remove it (nothing important happened)
+ if ( longMacro && overallVote & TriggerMacroVote_DoNothingRelease && pos == 0 )
+ overallVote |= TriggerMacroVote_Fail;
+
+ // Decide new state of macro after voting
+ // Fail macro, remove from pending list
+ if ( overallVote & TriggerMacroVote_Fail )
+ {
+ return TriggerMacroEval_Remove;
+ }
+ // Do nothing, incorrect key is being held or released
+ else if ( overallVote & TriggerMacroVote_DoNothing && longMacro )
+ {
+ // Just doing nothing :)
+ }
+ // If passing and in Waiting state, set macro state to Press
+ else if ( overallVote & TriggerMacroVote_Pass
+ && ( macro->state == TriggerMacro_Waiting || macro->state == TriggerMacro_Press ) )
+ {
+ macro->state = TriggerMacro_Press;
+
+ // If in press state, and this is the final combo, send request for ResultMacro
+ // Check to see if the result macro only has a single element
+ // If this result macro has more than 1 key, only send once
+ // TODO Add option to have long macro repeat rate
+ if ( macro->guide[ pos + comboLength + 1 ] == 0 )
+ {
+ // Long result macro (more than 1 combo)
+ if ( Macro_isLongResultMacro( &ResultMacroList[ macro->result ] ) )
+ {
+ // Only ever trigger result once, on press
+ if ( overallVote == TriggerMacroVote_Pass )
+ {
+ return TriggerMacroEval_DoResultAndRemove;
+ }
+ }
+ // Short result macro
+ else
+ {
+ // Only trigger result once, on press, if long trigger (more than 1 combo)
+ if ( Macro_isLongTriggerMacro( macro ) )
+ {
+ return TriggerMacroEval_DoResultAndRemove;
+ }
+ // Otherwise, trigger result continuously
+ else
+ {
+ return TriggerMacroEval_DoResult;
+ }
+ }
+ }
+ }
+ // If ready for transition and in Press state, set to Waiting and increment combo position
+ // Position is incremented (and possibly remove the macro from the pending list) on the next iteration
+ else if ( overallVote & TriggerMacroVote_Release && macro->state == TriggerMacro_Press )
+ {
+ macro->state = TriggerMacro_Release;
+
+ // If this is the last combo in the sequence, remove from the pending list
+ if ( macro->guide[ macro->pos + macro->guide[ macro->pos ] * TriggerGuideSize + 1 ] == 0 )
+ return TriggerMacroEval_Remove;
+ }
+ // Otherwise, just remove the macro on key release
+ // XXX Might cause some issues
+ else if ( overallVote & TriggerMacroVote_Release )
+ {
+ return TriggerMacroEval_Remove;
+ }
+
+ // If this is a short macro, just remove it
+ // The state can be rebuilt on the next iteration
+ if ( !longMacro )
+ return TriggerMacroEval_Remove;
+
+ return TriggerMacroEval_DoNothing;