forked from sle118/FreeTouchDeck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FTAction.cpp
629 lines (608 loc) · 20.2 KB
/
FTAction.cpp
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#include "FTAction.h"
#include "UserConfig.h"
#include "MenuNavigation.h"
#include "Menu.h"
#include "globals.hpp"
#include <algorithm>
#include <cstdio>
#include "ConfigLoad.h"
#include "System.h"
static const char *module = "FTAction";
using namespace std;
static char printBuffer[201] = {0};
namespace FreeTouchDeck
{
const char *splitterFormat = "%[^.:,]%*s";
const char *separatorFormat = "%[ .:,]";
SemaphoreHandle_t xQueueSemaphore = xSemaphoreCreateMutex();
std::queue<FTAction *> Queue;
std::queue<FTAction *> ScreenQueue;
std::string emptyString;
const char *unknown = "Unknown";
const char *FTAction::JsonLabelType = "type";
const char *FTAction::JsonLabelValue = "value";
const char *FTAction::JsonLabelSymbol = "symbol";
FTAction FTAction::rebootSystem;
FTAction::FTAction()
{
Type = ActionTypes::NONE;
}
const char *enum_to_string(ActionTypes type)
{
switch (type)
{
ENUM_TO_STRING_HELPER(ActionTypes, NONE);
ENUM_TO_STRING_HELPER(ActionTypes, KEYBOARD);
ENUM_TO_STRING_HELPER(ActionTypes, LOCAL);
default:
return unknown;
}
}
#define KEY_TO_MAP(k) \
{ \
QUOTE(k), { KEY_##k } \
}
#define MEDIAKEY_TO_MAP(k) \
{ \
QUOTE(k), { KEY_MEDIA_##k[0], KEY_MEDIA_##k[1] } \
}
#define START_END_KEY \
{ \
"", { 0 } \
}
const KeyMap_t NeedReleaseKeys{
{KEY_TO_MAP(LEFT_CTRL), KEY_TO_MAP(LEFT_SHIFT), KEY_TO_MAP(LEFT_ALT), KEY_TO_MAP(LEFT_GUI), KEY_TO_MAP(RIGHT_CTRL), KEY_TO_MAP(RIGHT_SHIFT), KEY_TO_MAP(RIGHT_ALT), KEY_TO_MAP(RIGHT_GUI)}};
const KeyMap_t NeedDoubleBytes{
{MEDIAKEY_TO_MAP(MUTE),
MEDIAKEY_TO_MAP(VOLUME_DOWN),
MEDIAKEY_TO_MAP(VOLUME_UP),
MEDIAKEY_TO_MAP(PLAY_PAUSE),
MEDIAKEY_TO_MAP(STOP),
MEDIAKEY_TO_MAP(NEXT_TRACK),
MEDIAKEY_TO_MAP(PREVIOUS_TRACK),
MEDIAKEY_TO_MAP(WWW_HOME),
MEDIAKEY_TO_MAP(LOCAL_MACHINE_BROWSER),
MEDIAKEY_TO_MAP(CALCULATOR),
MEDIAKEY_TO_MAP(WWW_BOOKMARKS),
MEDIAKEY_TO_MAP(WWW_SEARCH),
MEDIAKEY_TO_MAP(WWW_STOP),
MEDIAKEY_TO_MAP(WWW_BACK),
MEDIAKEY_TO_MAP(CONSUMER_CONTROL_CONFIGURATION),
MEDIAKEY_TO_MAP(EMAIL_READER)}};
const KeyMap_t KeyMap = {
{MEDIAKEY_TO_MAP(MUTE), MEDIAKEY_TO_MAP(VOLUME_DOWN), MEDIAKEY_TO_MAP(VOLUME_UP), MEDIAKEY_TO_MAP(PLAY_PAUSE), MEDIAKEY_TO_MAP(STOP), MEDIAKEY_TO_MAP(NEXT_TRACK), MEDIAKEY_TO_MAP(PREVIOUS_TRACK), MEDIAKEY_TO_MAP(WWW_HOME), MEDIAKEY_TO_MAP(LOCAL_MACHINE_BROWSER), MEDIAKEY_TO_MAP(CALCULATOR), MEDIAKEY_TO_MAP(WWW_BOOKMARKS), MEDIAKEY_TO_MAP(WWW_SEARCH), MEDIAKEY_TO_MAP(WWW_STOP), MEDIAKEY_TO_MAP(WWW_BACK), MEDIAKEY_TO_MAP(CONSUMER_CONTROL_CONFIGURATION), MEDIAKEY_TO_MAP(EMAIL_READER),
KEY_TO_MAP(F1), KEY_TO_MAP(F2), KEY_TO_MAP(F3), KEY_TO_MAP(F4), KEY_TO_MAP(F5), KEY_TO_MAP(F6), KEY_TO_MAP(F7), KEY_TO_MAP(F8), KEY_TO_MAP(F9), KEY_TO_MAP(F10), KEY_TO_MAP(F11), KEY_TO_MAP(F12), KEY_TO_MAP(F13), KEY_TO_MAP(F14), KEY_TO_MAP(F15), KEY_TO_MAP(F16), KEY_TO_MAP(F17), KEY_TO_MAP(F18), KEY_TO_MAP(F19), KEY_TO_MAP(F20), KEY_TO_MAP(F21), KEY_TO_MAP(F22), KEY_TO_MAP(F23), KEY_TO_MAP(F24),
KEY_TO_MAP(UP_ARROW), KEY_TO_MAP(DOWN_ARROW), KEY_TO_MAP(LEFT_ARROW), KEY_TO_MAP(RIGHT_ARROW), KEY_TO_MAP(BACKSPACE), KEY_TO_MAP(TAB), KEY_TO_MAP(RETURN), KEY_TO_MAP(PAGE_UP), KEY_TO_MAP(PAGE_DOWN), KEY_TO_MAP(DELETE),
KEY_TO_MAP(LEFT_CTRL), KEY_TO_MAP(LEFT_SHIFT), KEY_TO_MAP(LEFT_ALT), KEY_TO_MAP(LEFT_GUI), KEY_TO_MAP(RIGHT_CTRL), KEY_TO_MAP(RIGHT_SHIFT), KEY_TO_MAP(RIGHT_ALT), KEY_TO_MAP(RIGHT_GUI)}};
FTAction::~FTAction()
{
Values.clear();
Parameters.clear();
}
void FTAction::InitConstants()
{
LOC_LOGD(module, "Initializing actions constants");
rebootSystem = FTAction(ParametersList_t({"REBOOT"}));
}
cJSON *KeyNamesJson()
{
cJSON *doc = cJSON_CreateArray();
for (auto k : KeyMap)
{
cJSON_AddItemToArray(doc, cJSON_CreateString(k.first));
}
return doc;
}
bool FTAction::IsValidKey(const char *name, char **foundValue)
{
bool found = false;
FREE_AND_ASSIGNED_IF_PASSED(foundValue, NULL);
if (ISNULLSTRING(name))
{
LOC_LOGD(module, "Empty key passed");
}
else
{
char *foundKey = NULL;
for (auto k : KeyMap)
{
if (strcmp(k.first, name) == 0)
{
found = true;
break;
}
}
if (found)
{
LOC_LOGD(module, "Found Key %s", foundKey);
if (foundValue)
{
*foundValue = ps_strdup(name);
}
else
{
// Caller did not request the resulting key
// so free up memory
LOC_LOGD(module, "Result key was not requested. Freeing up memory");
FREE_AND_NULL(foundKey);
}
}
}
LOC_LOGD(module, "Key %s was %s parsed.", STRING_OR_DEFAULT(name, ""), found ? "successfully" : "not");
return found;
}
bool FTAction::ParseToken(const char *token, std::vector<FTAction *> &actions)
{
PrintMemInfo(__FUNCTION__, __LINE__);
bool success = false;
ParametersList_t parameters;
uint16_t delay = 0;
SplitParameters(token, parameters);
const char *tokenName = GetParameter(0, parameters).c_str();
if (parameters.size() > 1 && atol(GetParameter(1, parameters).c_str()) > 0)
{
delay = atol(GetParameter(1, parameters).c_str());
}
for (auto k : KeyMap)
{
if (strcmp(k.first, tokenName) == 0)
{
PrintMemInfo(__FUNCTION__, __LINE__);
LOC_LOGD(module, "Found Keyboard symbol %s, press delay %d ", tokenName, delay);
actions.push_back(new FTAction(k.first, k.second));
actions.back()->HoldTime = delay;
PrintMemInfo(__FUNCTION__, __LINE__);
success = true;
break;
}
}
if (!success && delay > 0)
{
if (strlen(tokenName) == 1)
{
PrintMemInfo(__FUNCTION__, __LINE__);
LOC_LOGD(module, "Found Keyboard symbol %s with delay %d", tokenName, delay);
actions.push_back(new FTAction((tokenName), KeyValue_t({(unsigned char)tokenName[0]})));
actions.back()->HoldTime = delay;
PrintMemInfo(__FUNCTION__, __LINE__);
success = true;
}
}
if (!success)
{
if (parameters.size() == 0)
{
LOC_LOGE(module, "No parameters or action name found. ");
}
else if (IsActionCallback(parameters))
{
LOC_LOGD(module, "Found user Action %s with %d parameter(s)", GetParameter(0, parameters).c_str(), parameters.size() - 1);
actions.push_back(new FTAction(parameters));
success = true;
}
}
if (!success)
{
LOC_LOGE(module, "Invalid local action type %s with %d parameter(s)", token, parameters.size());
}
PrintMemInfo(__FUNCTION__, __LINE__);
return success;
}
const char *FTAction::ActionName()
{
LOC_LOGV(module, "Getting Action name from %s", toString());
return GetParameter(0).c_str();
}
const char *FTAction::FirstParameter()
{
LOC_LOGV(module, "Getting first parameter from %s", toString());
return GetParameter(1).c_str();
}
const char *FTAction::SecondParameter()
{
LOC_LOGV(module, "Getting second parameter from %s", toString());
return GetParameter(2).c_str();
}
const char *FTAction::ThirdParameter()
{
LOC_LOGV(module, "Getting third parameter from %s", toString());
return GetParameter(3).c_str();
}
std::string &FTAction::ActionNameStr()
{
LOC_LOGV(module, "Getting Action name from %s", toString());
return GetParameter(0);
}
std::string &FTAction::FirstParameterStr()
{
return GetParameter(1);
}
std::string &FTAction::SecondParameterStr()
{
return GetParameter(2);
}
std::string &FTAction::ThirdParameterStr()
{
return GetParameter(3);
}
std::string &FTAction::GetParameter(int index, ParametersList_t ¶meters)
{
LOC_LOGV(module, "Getting parameter %d a total of %d entries", index, parameters.size());
ParametersList_t::iterator it;
if (parameters.size() > index)
{
it = parameters.begin();
std::advance(it, index);
if (it != parameters.end())
{
LOC_LOGV(module, "Found entry %d : %s", index, (*it).c_str());
return *it;
}
}
LOC_LOGE(module, "No parameter #%d was found. Size is %d", index, parameters.size());
return emptyString;
}
std::string &FTAction::GetParameter(int index)
{
return GetParameter(index, Parameters);
}
bool FTAction::SplitParameters(const char *parmString, ParametersList_t ¶meters)
{
PrintMemInfo(__FUNCTION__, __LINE__);
char *token = ps_strdup(parmString);
char *separators = ps_strdup(parmString);
const char *p = parmString;
const char *s = parmString;
int res = 0;
int resSeparators = 0;
do
{
res = sscanf(p, splitterFormat, token);
if (res > 0)
{
p += strlen(token);
resSeparators = sscanf(p, separatorFormat, separators);
if (resSeparators > 0)
{
p += strlen(separators);
}
else
{
strcpy(separators, "");
}
LOC_LOGD(module, "Found parameter : %s", STRING_OR_DEFAULT(token, ""));
parameters.push_back(token);
}
else
{
break;
}
} while (p && *p);
FREE_AND_NULL(token);
FREE_AND_NULL(separators);
PrintMemInfo(__FUNCTION__, __LINE__);
LOC_LOGV(module, "End of parameters list");
return true;
}
bool FTAction::KeyNeedsRelease(const char *keyName)
{
for (auto k : NeedReleaseKeys)
{
if (strcmp(k.first, keyName) == 0)
{
return true;
}
}
return false;
}
bool FTAction::KeyIsDoubleBytes(const char *keyName)
{
for (auto k : NeedDoubleBytes)
{
if (strcmp(k.first, keyName) == 0)
{
return true;
}
}
return false;
}
bool checkForStop()
{
if (isTouched())
{
EmptyQueue();
return true;
}
return false;
}
FTAction::FTAction(const char *keyName, const KeyValue_t &values)
{
PrintMemInfo(__FUNCTION__, __LINE__);
Type = ActionTypes::KEYBOARD;
if (ISNULLSTRING(keyName))
{
LOC_LOGE(module, "Empty key name received");
}
else
{
LOC_LOGD(module, "New action with key names %s, length of %d", keyName, Values.size());
Parameters.push_back(keyName);
PrintMemInfo(__FUNCTION__, __LINE__);
NeedsRelease = KeyNeedsRelease(keyName);
NeedsDoubleBytes = KeyIsDoubleBytes(keyName);
}
PrintMemInfo(__FUNCTION__, __LINE__);
Values = values;
}
FTAction::FTAction(const KeyValue_t &values)
{
Type = ActionTypes::KEYBOARD;
Values = values;
}
FTAction::FTAction(const ParametersList_t ¶meters)
{
Type = ActionTypes::LOCAL;
Parameters = parameters;
}
void FTAction::Execute()
{
bool wasStopped = false;
MediaKeyReport MediaKey;
LOC_LOGI(module, "Executing Action %s", toString());
if (checkForStop())
{
return;
}
switch (Type)
{
case ActionTypes::NONE:
break;
case ActionTypes::KEYBOARD:
if (!bleKeyboard.isConnected())
{
LOC_LOGW(module, "Skipping action %s. Bluetooth Keyboard not connected", toString());
return;
}
if (NeedsDoubleBytes)
{
MediaKey[0] = Values[0];
MediaKey[1] = Values[1];
if (NeedsRelease)
{
bleKeyboard.press(MediaKey);
}
else
{
bleKeyboard.write(MediaKey);
}
delay(generalconfig.keyDelay);
}
else
{
if (NeedsRelease)
{
for (auto ks : Values)
{
// Use keyboard press for each character
// as this method does not release each key
// individually
bleKeyboard.press(ks);
delay(generalconfig.keyDelay);
wasStopped=checkForStop();
if(wasStopped) break;
}
}
else
{
for (auto ks : Values)
{
if (HoldTime > 0)
{
LOC_LOGI(module, "Pressing key with hold of %d ms", HoldTime);
}
bleKeyboard.press(ks);
delay(HoldTime);
bleKeyboard.release(ks);
delay(generalconfig.keyDelay);
wasStopped=checkForStop();
if(wasStopped) break;
}
}
}
break;
case ActionTypes::LOCAL:
CallActionCallback(false);
default:
break;
}
if (wasStopped && NeedsRelease)
{
LOC_LOGI(module,"Releasing all keys");
bleKeyboard.releaseAll();
}
}
const char *FTAction::toString()
{
switch (Type)
{
case ActionTypes::NONE:
break;
case ActionTypes::LOCAL:
case ActionTypes::KEYBOARD:
snprintf(printBuffer, sizeof(printBuffer), "%s:%s:",IsScreen()?"SCREEN":"LOCAL", enum_to_string(Type));
for (auto p : Parameters)
{
snprintf(printBuffer, sizeof(printBuffer), "%s %s", printBuffer, p.c_str());
}
snprintf(printBuffer, sizeof(printBuffer), "%s, Values count: %d, %s", printBuffer, Values.size(), NeedsRelease?"NEEDS RELEASE":"");
break;
default:
break;
}
return printBuffer;
}
FTAction *PopScreenQueue()
{
FTAction *Action = NULL;
if (QueueLock(portMAX_DELAY / portTICK_PERIOD_MS))
{
if (!ScreenQueue.empty())
{
LOC_LOGV(module, "Screen Action Queue Length : %d", ScreenQueue.size());
Action = ScreenQueue.front();
ScreenQueue.pop();
LOC_LOGV(module, "Screen Action Queue Length : %d", ScreenQueue.size());
}
QueueUnlock();
}
else
{
LOC_LOGE(module, "Unable to screen lock Action queue");
}
return Action;
}
void EmptyQueue()
{
if (QueueLock(portMAX_DELAY / portTICK_PERIOD_MS))
{
while (!Queue.empty())
{
Queue.pop();
}
QueueUnlock();
}
}
size_t QueueSize()
{
return Queue.size() + ScreenQueue.size();
}
FTAction *PopQueue()
{
FTAction *Action = NULL;
if (QueueLock(portMAX_DELAY / portTICK_PERIOD_MS))
{
if (!Queue.empty())
{
LOC_LOGV(module, "Action Queue Length : %d", Queue.size());
Action = Queue.front();
Queue.pop();
LOC_LOGV(module, "Action Queue Length : %d", Queue.size());
}
QueueUnlock();
}
else
{
LOC_LOGE(module, "Unable to lock Action queue");
}
return Action;
}
bool QueueLock(TickType_t xTicksToWait)
{
LOC_LOGV(module, "Locking Action Queue object");
if (xSemaphoreTake(xQueueSemaphore, xTicksToWait) == pdTRUE)
{
LOC_LOGV(module, "Action Queue object locked!");
return true;
}
else
{
LOC_LOGE(module, "Unable to lock the Action queue object");
return false;
}
}
void QueueUnlock()
{
LOC_LOGV(module, "Unlocking the Action queue object");
xSemaphoreGive(xQueueSemaphore);
}
bool QueueAction(FTAction *action)
{
if (!QueueLock(100 / portTICK_PERIOD_MS))
{
LOC_LOGE(module, "Unable to queue new action ");
return false;
}
if (action->IsScreen())
{
LOC_LOGD(module, "Pushing action %s to screen queue", action->toString());
ScreenQueue.push(action);
}
else
{
LOC_LOGD(module, "Pushing action %s to keyboard queue", action->toString());
Queue.push(action);
}
QueueUnlock();
return true;
}
bool FTAction::CallActionCallback(ParametersList_t ¶meters, FTAction *action, bool checkOnly)
{
ActionCallbackFn_t callbackFn = NULL;
PrintMemInfo(__FUNCTION__, __LINE__);
std::string name = GetParameter(0, parameters);
auto callback = FreeTouchDeck::UserActions.find(name.c_str());
if (callback == FreeTouchDeck::UserActions.end())
{
LOC_LOGW(module, "Find failed. Trying to iterate entries");
for (auto c : FreeTouchDeck::UserActions)
{
if (c.first == name)
{
callbackFn = c.second;
LOC_LOGD(module, "Found the callback with a loop in the map values");
break;
}
}
}
else
{
LOC_LOGD(module, "Found the callback in the map");
callbackFn = callback->second;
}
if (callbackFn)
{
if (checkOnly)
{
LOC_LOGD(module, "Check only, returning success");
return true;
}
else if (!action)
{
LOC_LOGE(module, "Action pointer is null. Unable to invoke callback!");
}
else
{
LOC_LOGD(module, "Calling function %s", name.c_str());
bool res = callbackFn(action);
PrintMemInfo(__FUNCTION__, __LINE__);
return res;
}
}
else
{
LOC_LOGE(module, "Invalid callback name %s. Valid callbacks are: ", name.c_str());
for (auto c : FreeTouchDeck::UserActions)
{
LOC_LOGE(module, " %s", c.first.c_str());
}
}
return false;
}
bool FTAction::IsActionCallback(ParametersList_t ¶meters)
{
return CallActionCallback(parameters, NULL, true);
}
bool FTAction::CallActionCallback(bool checkOnly)
{
return CallActionCallback(Parameters, this, checkOnly);
}
cJSON *UserActionsJson()
{
cJSON *doc = cJSON_CreateArray();
for (auto a : UserActions)
{
cJSON_AddItemToArray(doc, cJSON_CreateString(a.first.c_str()));
}
return doc;
}
}