-
Notifications
You must be signed in to change notification settings - Fork 271
/
layer.c
590 lines (482 loc) · 14.5 KB
/
layer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/* Copyright (C) 2014-2020 by Jacob Alexander
*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this file. If not, see <http://www.gnu.org/licenses/>.
*/
// ----- Includes -----
// Compiler Includes
#include <Lib/MacroLib.h>
// Project Includes
#include <cli.h>
#include <led.h>
#include <print.h>
// Connect Includes
#if defined(ConnectEnabled_define)
#include <connect_scan.h>
#endif
// Generated Includes
#include <kll_defs.h>
// Local Includes
#include "kll.h"
#include "trigger.h"
// ----- Function Declarations -----
void Layer_clearLayers();
// ----- Variables -----
// Layer debug flag - If set, displays any changes to layers and the full layer stack on change
uint8_t layerDebugMode;
// Incoming Trigger Event Buffer
var_uint_t macroTriggerEventLayerCache[ MaxScanCode_KLL + 1 ];
// Layer Index Stack
// * When modifying layer state and the state is non-0x0, the stack must be adjusted
index_uint_t macroLayerIndexStack[ LayerNum ] = { 0 };
index_uint_t macroLayerIndexStackSize = 0;
// Current rotated layer
uint16_t Layer_rotationLayer;
// Layer State Information
extern LayerStateType LayerState[];
// Layer Index List
extern const Layer LayerIndex[];
// ----- Capabilities -----
// Sets the given layer with the specified layerState
void Layer_layerStateSet( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint16_t layer, uint8_t layerState )
{
// Ignore if layer does not exist or trying to manipulate layer 0/Default layer
if ( layer >= LayerNum || layer == 0 )
return;
// Check current layer state
uint8_t oldState = LayerState[ layer ];
// Is layer in the LayerIndexStack?
uint8_t inLayerIndexStack = 0;
uint16_t stackItem = 0;
while ( stackItem < macroLayerIndexStackSize )
{
// Flag if layer is already in the LayerIndexStack
if ( macroLayerIndexStack[ stackItem ] == layer )
{
inLayerIndexStack = 1;
break;
}
// Increment to next item
stackItem++;
}
// Toggle Layer State Byte
if ( LayerState[ layer ] & layerState )
{
// Unset
LayerState[ layer ] &= ~layerState;
}
else
{
// Set
LayerState[ layer ] |= layerState;
}
// If the layer was not in the LayerIndexStack add it
if ( !inLayerIndexStack )
{
macroLayerIndexStack[ macroLayerIndexStackSize++ ] = layer;
}
// If the layer is in the LayerIndexStack and the state is 0x00, remove
uint8_t newState = LayerState[ layer ];
if ( newState == LayerStateType_Off && inLayerIndexStack )
{
// Remove the layer from the LayerIndexStack
// Using the already positioned stackItem variable from the loop above
while ( stackItem < macroLayerIndexStackSize )
{
macroLayerIndexStack[ stackItem ] = macroLayerIndexStack[ stackItem + 1 ];
stackItem++;
}
// Reduce LayerIndexStack size
macroLayerIndexStackSize--;
}
// Determine what signal to send about layer
if ( oldState && newState )
{
// On -> On (Layer still active)
// Send current layer state
Macro_layerState( layer, ScheduleType_On | (LayerState[ layer ] << 4) );
}
else if ( !oldState && newState )
{
// Off -> On (Activate)
// Send layer state that activated
Macro_layerState( layer, ScheduleType_A | (layerState << 4) );
}
else if ( oldState && !newState )
{
// On -> Off (Deactivate)
// Send layer state that deactivated
Macro_layerState( layer, ScheduleType_D | (layerState << 4) );
}
// Layer Debug Mode
if ( layerDebugMode )
{
print("\033[1;36mL\033[0m ");
// Iterate over each of the layers displaying the state as a hex value
for ( index_uint_t index = 0; index < LayerNum; index++ )
{
printHex_op( LayerState[ index ], 0 );
}
// Always show the default layer (it's always 0)
print(" 0");
// Iterate over the layer stack starting from the bottom of the stack
for ( index_uint_t index = macroLayerIndexStackSize; index > 0; index-- )
{
print(":");
printHex_op( macroLayerIndexStack[ index - 1 ], 0 );
}
print( NL );
}
}
// Modifies the specified Layer control byte
// Argument #1: Layer Index -> uint16_t
// Argument #2: Layer State -> uint8_t
void Layer_layerState_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
case CapabilityState_Last:
// Only use capability on press or release
break;
case CapabilityState_Debug:
// Display capability name
print("Layer_layerState(layerIndex,layerState)");
// Read arg if not set to 0
if ( args != 0 )
{
uint16_t key = *(uint16_t*)(&args[0]);
print(" -> ");
printInt16( key );
}
return;
default:
return;
}
// Get layer index from arguments
// Cast pointer to uint8_t to uint16_t then access that memory location
uint16_t layer = *(uint16_t*)(&args[0]);
// Get layer toggle byte
uint8_t layerState = args[ sizeof(uint16_t) ];
Layer_layerStateSet( trigger, state, stateType, layer, layerState );
}
// Latches given layer
// Argument #1: Layer Index -> uint16_t
void Layer_layerLatch_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Last:
// Only use capability on release
break;
case CapabilityState_Debug:
// Display capability name
print("Layer_layerLatch(layerIndex)");
// Read arg if not set to 0
if ( args != 0 )
{
uint16_t key = *(uint16_t*)(&args[0]);
print(" -> ");
printInt16( key );
}
return;
default:
return;
}
// Get layer index from arguments
// Cast pointer to uint8_t to uint16_t then access that memory location
uint16_t layer = *(uint16_t*)(&args[0]);
Layer_layerStateSet( trigger, state, stateType, layer, LayerStateType_Latch );
}
// Locks given layer
// Argument #1: Layer Index -> uint16_t
void Layer_layerLock_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
// Only use capability on press
break;
case CapabilityState_Debug:
// Display capability name
print("Layer_layerLock(layerIndex)");
// Read arg if not set to 0
if ( args != 0 )
{
uint16_t key = *(uint16_t*)(&args[0]);
print(" -> ");
printInt16( key );
}
return;
default:
return;
}
// Get layer index from arguments
// Cast pointer to uint8_t to uint16_t then access that memory location
uint16_t layer = *(uint16_t*)(&args[0]);
Layer_layerStateSet( trigger, state, stateType, layer, LayerStateType_Lock );
}
// Shifts given layer
// Argument #1: Layer Index -> uint16_t
void Layer_layerShift_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
// Get layer index from arguments
// Cast pointer to uint8_t to uint16_t then access that memory location
uint16_t layer = *(uint16_t*)(&args[0]);
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
// Ignore if layer does not exist or trying to manipulate layer 0/Default layer
if ( layer >= LayerNum || layer == 0 )
return;
// Press
// Only set the layer if it is disabled
if ( (LayerState[ layer ] & LayerStateType_Shift) != LayerStateType_Off )
{
Layer_layerStateSet( trigger, state, stateType, layer, LayerStateType_Off );
return;
}
break;
case CapabilityState_Last:
// Ignore if layer does not exist or trying to manipulate layer 0/Default layer
if ( layer >= LayerNum || layer == 0 )
return;
// Release
// Only unset the layer if it is enabled
if ( (LayerState[ layer ] & LayerStateType_Shift) == LayerStateType_Off )
{
Layer_layerStateSet( trigger, state, stateType, layer, LayerStateType_Off );
return;
}
break;
case CapabilityState_Debug:
// Display capability name
print("Layer_layerShift(layerIndex)");
// Read arg if not set to 0
if ( args != 0 )
{
uint16_t key = *(uint16_t*)(&args[0]);
print(" -> ");
printInt16( key );
}
default:
return;
}
Layer_layerStateSet( trigger, state, stateType, layer, LayerStateType_Shift );
}
// Rotate layer to next/previous
// Uses state variable to keep track of the current layer position
// Layers are still evaluated using the layer stack
void Layer_layerRotate_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
// Only use capability on press
break;
case CapabilityState_Debug:
// Display capability name
print("Layer_layerRotate(previous)");
// Read arg if not set to 0
if ( args != 0 )
{
uint8_t key = args[0];
print(" -> ");
printInt8( key );
}
return;
default:
return;
}
// Unset previous rotation layer if not 0
if ( Layer_rotationLayer != 0 )
{
Layer_layerStateSet( trigger, state, stateType, Layer_rotationLayer, LayerStateType_Lock );
}
// Get direction of rotation, 0, next, non-zero previous
uint8_t direction = *args;
// Next
if ( !direction )
{
Layer_rotationLayer++;
// Invalid layer
if ( Layer_rotationLayer >= LayerNum )
Layer_rotationLayer = 0;
}
// Previous
else
{
Layer_rotationLayer--;
// Layer wrap
if ( Layer_rotationLayer >= LayerNum )
Layer_rotationLayer = LayerNum - 1;
}
// Toggle the computed layer rotation
Layer_layerStateSet( trigger, state, stateType, Layer_rotationLayer, LayerStateType_Lock );
}
// Clear layer states
// XXX (HaaTa): Does not send trigger events
void Layer_clearLayers_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
// Only use capability on press
break;
case CapabilityState_Debug:
// Display capability name
print("Layer_clearLayers()");
return;
default:
return;
}
// Clear layer states
Layer_clearLayers();
}
// ----- Functions -----
// Clears the current layer state
void Layer_clearLayers()
{
// Clear layer stack
macroLayerIndexStackSize = 0;
// Clear layer states
memset( &LayerState, 0, sizeof(LayerStateType) * LayerNum );
}
// Setup layers
void Layer_setup()
{
// Cleanup layers
Layer_clearLayers();
// Set the current rotated layer to 0
Layer_rotationLayer = 0;
// Layer debug mode
layerDebugMode = 0;
}
// Looks up the trigger list for the given scan code (from the active layer)
// NOTE: Calling function must handle the NULL pointer case
nat_ptr_t *Layer_layerLookup( TriggerEvent *event, uint8_t latch_expire )
{
uint8_t index = event->index;
// Cached Lookup (for handling layer latches)
uint8_t cache_lookup = 0;
CapabilityState cstate = KLL_CapabilityState( event->state, event->type );
switch ( cstate )
{
case CapabilityState_Any:
case CapabilityState_Last:
// Ignore press, off and debug
cache_lookup = 1;
case CapabilityState_Initial:
default:
break;
}
// Do a cached lookup if necessary
if ( cache_lookup )
{
// Cached layer
var_uint_t cachedLayer = macroTriggerEventLayerCache[ index ];
// Lookup map, then layer
nat_ptr_t **map = (nat_ptr_t**)LayerIndex[ cachedLayer ].triggerMap;
const Layer *layer = &LayerIndex[ cachedLayer ];
// Cache trigger list before attempting to expire latch
nat_ptr_t *trigger_list = map[ index - layer->first ];
// Check if latch has been pressed for this layer
uint8_t latch = LayerState[ cachedLayer ] & LayerStateType_Latch;
if ( latch && latch_expire )
{
Layer_layerStateSet( 0, 0, 0, cachedLayer, LayerStateType_Latch );
}
return trigger_list;
}
// If no trigger macro is defined at the given layer, fallthrough to the next layer
for ( uint16_t layerIndex = macroLayerIndexStackSize; layerIndex != 0xFFFF; layerIndex-- )
{
// If this is a Layer trigger event, ignore other layers, always check the default map
switch ( event->type )
{
case TriggerType_Layer1:
case TriggerType_Layer2:
case TriggerType_Layer3:
case TriggerType_Layer4:
layerIndex = 0;
continue;
default:
break;
}
// Lookup Layer
const Layer *layer = &LayerIndex[ macroLayerIndexStack[ layerIndex ] ];
// Lookup each of the states
uint8_t shift = LayerState[ macroLayerIndexStack[ layerIndex ] ] & LayerStateType_Shift;
uint8_t latch = LayerState[ macroLayerIndexStack[ layerIndex ] ] & LayerStateType_Latch;
uint8_t lock = LayerState[ macroLayerIndexStack[ layerIndex ] ] & LayerStateType_Lock;
// Check if latch has been pressed for this layer
// XXX Regardless of whether a key is found, the latch is removed on first lookup
if ( latch && latch_expire )
{
Layer_layerStateSet( 0, 0, 0, macroLayerIndexStack[ layerIndex ], LayerStateType_Latch );
}
// Only use layer, if state is valid
// XOR each of the state bits
// If only two are enabled, do not use this state
if ( (shift) ^ (latch>>1) ^ (lock>>2) )
{
// Lookup layer
nat_ptr_t **map = (nat_ptr_t**)layer->triggerMap;
// Determine if layer has key defined
// Make sure index is between layer first and last scancodes
if ( map != 0
&& index <= layer->last
&& index >= layer->first
&& *map[ index - layer->first ] != 0 )
{
// Set the layer cache
macroTriggerEventLayerCache[ index ] = macroLayerIndexStack[ layerIndex ];
return map[ index - layer->first ];
}
}
}
// Do lookup on default layer
nat_ptr_t **map = (nat_ptr_t**)LayerIndex[0].triggerMap;
// Lookup default layer
const Layer *layer = &LayerIndex[0];
// Make sure index is between layer first and last scancodes
if ( map != 0
&& index <= layer->last
&& index >= layer->first
&& *map[ index - layer->first ] != 0 )
{
// Set the layer cache to default map
macroTriggerEventLayerCache[ index ] = 0;
return map[ index - layer->first ];
}
// Otherwise no defined Trigger Macro
// Just ignore it
return 0;
}
// Layer active at top of the stack
index_uint_t Layer_topActive()
{
// Top of stack
if ( macroLayerIndexStackSize > 0 )
{
return macroLayerIndexStack[macroLayerIndexStackSize - 1];
}
// Default layer
return 0;
}