forked from rdkcentral/RDKShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompositorcontroller.cpp
2614 lines (2370 loc) · 89.6 KB
/
compositorcontroller.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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* If not stated otherwise in this file or this component's LICENSE
* file the following copyright and licenses apply:
*
* Copyright 2020 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#include "compositorcontroller.h"
#include "essosinstance.h"
#include "animation.h"
#include "rdkshell.h"
#include "application.h"
#include "logger.h"
#include "linuxkeys.h"
#include "eastereggs.h"
#include "rdkcompositornested.h"
#include "rdkcompositorsurface.h"
#include "string.h"
#include "rdkshellimage.h"
#include "rdkshellrect.h"
#include "cursor.h"
#include <iostream>
#include <map>
#include <ctime>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define RDKSHELL_ANY_KEY 65536
#define RDKSHELL_DEFAULT_INACTIVITY_TIMEOUT_IN_SECONDS 15*60
#define RDKSHELL_WILDCARD_KEY_CODE 255
#define RDKSHELL_WATERMARK_ID 65536
namespace RdkShell
{
struct KeyListenerInfo
{
KeyListenerInfo() : keyCode(-1), flags(0), activate(false), propagate(true) {}
uint32_t keyCode;
uint32_t flags;
bool activate;
bool propagate;
};
struct CompositorInfo
{
CompositorInfo() : name(), compositor(nullptr), eventListeners(), mimeType() {}
std::string name;
std::shared_ptr<RdkCompositor> compositor;
std::map<uint32_t, std::vector<KeyListenerInfo>> keyListenerInfo;
std::vector<std::shared_ptr<RdkShellEventListener>> eventListeners;
std::string mimeType;
bool autoDestroy;
};
struct KeyInterceptInfo
{
KeyInterceptInfo() : keyCode(-1), flags(0), always(true), client(), compositorInfo() {}
uint32_t keyCode;
uint32_t flags;
bool always;
std::string client;
struct CompositorInfo compositorInfo;
};
enum RdkShellCompositorType
{
NESTED,
SURFACE
};
struct WatermarkImage
{
WatermarkImage(uint32_t imageId, uint32_t imageZOrder): id(imageId), zorder(imageZOrder), image(nullptr) {}
uint32_t id;
uint32_t zorder;
std::shared_ptr<RdkShell::Image> image;
};
struct KeyRepeatConfig
{
KeyRepeatConfig() : enabled(false), initialDelay(500), repeatInterval(250) {}
int initialDelay;
int repeatInterval;
bool enabled;
};
struct GenerateKeyEvent
{
GenerateKeyEvent(const std::string& client, uint32_t keyCode, uint32_t modifiers, double triggerTime) :
client(client) , triggerTime(triggerTime), keyCode(keyCode), modifiers(modifiers) {}
std::string client;
double triggerTime;
uint32_t keyCode;
uint32_t modifiers;
};
typedef std::vector<CompositorInfo> CompositorList;
typedef CompositorList::iterator CompositorListIterator;
CompositorList gCompositorList;
CompositorList gTopmostCompositorList;
CompositorInfo gFocusedCompositor;
std::vector<std::shared_ptr<RdkCompositor>> gPendingKeyUpListeners;
CompositorList gDeletedCompositors;
static std::map<uint32_t, std::vector<KeyInterceptInfo>> gKeyInterceptInfoMap;
bool gEnableInactivityReporting = false;
double gInactivityIntervalInSeconds = RDKSHELL_DEFAULT_INACTIVITY_TIMEOUT_IN_SECONDS;
double gLastKeyEventTime = RdkShell::seconds();
double gNextInactiveEventTime = RdkShell::seconds() + gInactivityIntervalInSeconds;
uint32_t gLastKeyCode = 0;
uint32_t gLastKeyModifiers = 0;
uint64_t gLastKeyMetadata = 0;
std::shared_ptr<RdkShellEventListener> gRdkShellEventListener;
double gLastKeyPressStartTime = 0.0;
double gLastKeyRepeatTime = 0.0;
RdkShellCompositorType gRdkShellCompositorType = NESTED;
std::shared_ptr<RdkShell::Image> gSplashImage = nullptr;
bool gShowSplashImage = false;
uint32_t gSplashDisplayTimeInSeconds = 0;
double gSplashStartTime = 0;
std::shared_ptr<RdkShell::Image> gRdkShellWatermarkImage = nullptr;
std::vector<WatermarkImage> gWatermarkImages;
bool gShowWatermarkImage = false;
bool gAlwaysShowWatermarkImageOnTop = false;
std::shared_ptr<RdkShell::Image> gFullScreenImage = nullptr;
bool gShowFullScreenImage = false;
std::string gCurrentFullScreenImage = "";
uint32_t gPowerKeyCode = 0;
uint32_t gFrontPanelButtonCode = 0;
bool gPowerKeyEnabled = false;
bool gPowerKeyReleaseReceived = false;
bool gRdkShellPowerKeyReleaseOnlyEnabled = false;
bool gIgnoreKeyInputEnabled = false;
std::shared_ptr<Cursor> gCursor = nullptr;
KeyRepeatConfig gKeyRepeatConfig;
std::vector<GenerateKeyEvent> gGenerateKeyEvents;
std::string standardizeName(const std::string& clientName)
{
std::string displayName = clientName;
std::transform(displayName.begin(), displayName.end(), displayName.begin(), [](unsigned char c){ return std::tolower(c); });
return displayName;
}
/*
getCompositorInfo searches gCompositorList and gTopmostCompositoList for compositor info with
client name equal to clientName parameter.
Returns true if compositor info was found in any of the lists and false otherwise.
Iterator for found compositor info is stored in it parameter foundIt.
If compositorList parameter is not null, it will store the compositor list in which
compositor info was found.
*/
bool getCompositorInfo(const std::string& clientName, CompositorListIterator& foundIt,
CompositorList** compositorList = nullptr)
{
std::string stdClientName = standardizeName(clientName);
for (auto it = gCompositorList.begin(); it != gCompositorList.end(); ++it)
{
if (it->name == stdClientName)
{
foundIt = it;
if (compositorList)
*compositorList = &gCompositorList;
return true;
}
}
for (auto it = gTopmostCompositorList.begin(); it != gTopmostCompositorList.end(); ++it)
{
if (it->name == stdClientName)
{
foundIt = it;
if (compositorList)
*compositorList = &gTopmostCompositorList;
return true;
}
}
return false;
}
/*
getCompositorInfo searches gCompositorList and gTopmostCompositoList for compositor info with
RdkCompositor equal to compositor parameter.
Returns true if compositor info was found in any of the lists and false otherwise.
*/
bool getCompositorInfo(const RdkCompositor* compositor, CompositorListIterator& foundIt)
{
for (auto it = gCompositorList.begin(); it != gCompositorList.end(); ++it)
{
if (it->compositor.get() == compositor)
{
foundIt = it;
return true;
}
}
for (auto it = gTopmostCompositorList.begin(); it != gTopmostCompositorList.end(); ++it)
{
if (it->compositor.get() == compositor)
{
foundIt = it;
return true;
}
}
return false;
}
size_t getNumCompositorInfo()
{
return gCompositorList.size() + gTopmostCompositorList.size();
}
void sendApplicationEvent(std::shared_ptr<RdkShellEventListener>& listener, const std::string& eventName, const std::string& client)
{
if (eventName.compare(RDKSHELL_EVENT_APPLICATION_LAUNCHED) == 0)
{
listener->onApplicationLaunched(client);
}
else if(eventName.compare(RDKSHELL_EVENT_APPLICATION_TERMINATED) == 0)
{
listener->onApplicationTerminated(client);
}
else if(eventName.compare(RDKSHELL_EVENT_APPLICATION_CONNECTED) == 0)
{
listener->onApplicationConnected(client);
}
else if(eventName.compare(RDKSHELL_EVENT_APPLICATION_DISCONNECTED) == 0)
{
listener->onApplicationDisconnected(client);
}
else if(eventName.compare(RDKSHELL_EVENT_APPLICATION_FIRST_FRAME) == 0)
{
listener->onApplicationFirstFrame(client);
}
else if(eventName.compare(RDKSHELL_EVENT_APPLICATION_SUSPENDED) == 0)
{
listener->onApplicationSuspended(client);
}
else if(eventName.compare(RDKSHELL_EVENT_APPLICATION_RESUMED) == 0)
{
listener->onApplicationResumed(client);
}
else if(eventName.compare(RDKSHELL_EVENT_SIZE_CHANGE_COMPLETE) == 0)
{
listener->onSizeChangeComplete(client);
}
}
bool interceptKey(uint32_t keycode, uint32_t flags, uint64_t metadata, bool isPressed)
{
bool ret = false;
if (gKeyInterceptInfoMap.end() != gKeyInterceptInfoMap.find(keycode))
{
for (int i=0; i<gKeyInterceptInfoMap[keycode].size(); i++)
{
struct KeyInterceptInfo& info = gKeyInterceptInfoMap[keycode][i];
if(info.compositorInfo.compositor == nullptr)
{
CompositorListIterator it;
if (getCompositorInfo(info.client, it))
{
info.compositorInfo = *it;
}
}
if(info.compositorInfo.compositor != nullptr)
{
if (info.flags == flags && info.compositorInfo.compositor->getInputEventsEnabled())
{
if (info.always || info.compositorInfo.name == gFocusedCompositor.name)
{
Logger::log(Debug, "Key %d intercepted by client %s always: %d", keycode, info.compositorInfo.name.c_str(), info.always);
if (isPressed)
{
info.compositorInfo.compositor->onKeyPress(keycode, flags, metadata);
}
else
{
info.compositorInfo.compositor->onKeyRelease(keycode, flags, metadata);
}
}
ret = true;
}
}
}
}
return ret;
}
void evaluateKeyListeners(struct CompositorInfo& compositor, uint32_t keycode, uint32_t flags, bool& foundlistener, bool& activate, bool& propagate)
{
std::map<uint32_t, std::vector<KeyListenerInfo>>& keyListenerInfo = compositor.keyListenerInfo;
if (keyListenerInfo.end() != keyListenerInfo.find(keycode))
{
for (size_t i=0; i<keyListenerInfo[keycode].size(); i++)
{
struct KeyListenerInfo& info = keyListenerInfo[keycode][i];
if (info.flags == flags)
{
foundlistener = true;
activate = info.activate;
propagate = info.propagate;
break;
}
}
}
// handle wildcard if no listener found
if ((false == foundlistener) && (keyListenerInfo.find(RDKSHELL_ANY_KEY) != keyListenerInfo.end()))
{
struct KeyListenerInfo& info = keyListenerInfo[RDKSHELL_ANY_KEY][0];
foundlistener = true;
activate = info.activate;
propagate = info.propagate;
}
}
void bubbleKey(uint32_t keycode, uint32_t flags, uint64_t metadata, bool isPressed)
{
std::vector<CompositorInfo>::iterator compositorIterator = gCompositorList.begin();
std::string focusedCompositorName = gFocusedCompositor.name;
#ifndef RDKSHELL_ENABLE_KEYBUBBING_TOP_MODE
for (compositorIterator = gCompositorList.begin(); compositorIterator != gCompositorList.end(); compositorIterator++)
{
if (compositorIterator->name == gFocusedCompositor.name)
{
break;
}
}
#else
Logger::log(Debug, "Key bubbling is made from top application");
#endif //RDKSHELL_ENABLE_KEYBUBBING_TOP_MODE
bool activateCompositor = false, propagateKey = true, foundListener = false;
bool stopPropagation = false;
bool isFocusedCompositor = true;
while (compositorIterator != gCompositorList.end())
{
if (!compositorIterator->compositor->getInputEventsEnabled())
{
compositorIterator++;
continue;
}
#ifdef RDKSHELL_ENABLE_KEYBUBBING_TOP_MODE
if (compositorIterator->name == focusedCompositorName)
{
compositorIterator++;
continue;
}
isFocusedCompositor = false;
#endif //RDKSHELL_ENABLE_KEYBUBBING_TOP_MODE
activateCompositor = false;
propagateKey = true;
foundListener = false;
evaluateKeyListeners(*compositorIterator, keycode, flags, foundListener, activateCompositor, propagateKey);
if ((false == isFocusedCompositor) && (true == foundListener))
{
Logger::log(Debug, "Key %d sent to listener %s", keycode, compositorIterator->name.c_str());
if (isPressed)
{
compositorIterator->compositor->onKeyPress(keycode, flags, metadata);
gPendingKeyUpListeners.push_back(compositorIterator->compositor);
}
else
{
compositorIterator->compositor->onKeyRelease(keycode, flags, metadata);
}
}
isFocusedCompositor = false;
if (activateCompositor)
{
if (gFocusedCompositor.name != compositorIterator->name)
{
std::string previousFocusedClient = !gFocusedCompositor.name.empty() ? gFocusedCompositor.name:"none";
Logger::log(LogLevel::Information, "rdkshell_focus bubbleKey: the focused client is now %s . previous: %s", (*compositorIterator).name.c_str(), previousFocusedClient.c_str());
if ((gFocusedCompositor.compositor) && (gFocusedCompositor.compositor->isKeyPressed()))
{
gPendingKeyUpListeners.push_back(gFocusedCompositor.compositor);
}
gFocusedCompositor = *compositorIterator;
if (gRdkShellEventListener)
{
gRdkShellEventListener->onApplicationActivated(gFocusedCompositor.name);
}
}
}
//propagate is false, stopping here
if (false == propagateKey)
{
break;
}
compositorIterator++;
}
}
void updateKeyRepeat()
{
if (gKeyRepeatConfig.enabled)
{
if (gLastKeyPressStartTime > 0.0)
{
double currentTime = RdkShell::seconds();
if (gLastKeyRepeatTime == 0.0)
{
if ((currentTime - gLastKeyPressStartTime) * 1000.0 > gKeyRepeatConfig.initialDelay)
{
CompositorController::onKeyPress(gLastKeyCode, gLastKeyModifiers, gLastKeyMetadata);
gLastKeyRepeatTime = currentTime;
}
}
else
{
if ((currentTime - gLastKeyRepeatTime) * 1000.0 > gKeyRepeatConfig.repeatInterval)
{
CompositorController::onKeyPress(gLastKeyCode, gLastKeyModifiers, gLastKeyMetadata);
gLastKeyRepeatTime = currentTime;
}
}
}
}
}
void updateGenerateKeyEvents()
{
double currentTime = RdkShell::seconds();
auto it = gGenerateKeyEvents.begin();
while (it != gGenerateKeyEvents.end())
{
if (it->triggerTime <= currentTime)
{
if (it->client.empty())
{
CompositorController::onKeyRelease(it->keyCode, it->modifiers, 0, false);
}
else
{
CompositorListIterator cit;
if (getCompositorInfo(it->client, cit))
{
cit->compositor->onKeyRelease(it->keyCode, it->modifiers, 0);
}
}
it = gGenerateKeyEvents.erase(it);
}
else
++it;
}
}
std::shared_ptr<RdkCompositor> CompositorController::getCompositor(const std::string& displayName)
{
auto lambda = [displayName](CompositorInfo& info)
{
std::string compositorDisplayName;
info.compositor->displayName(compositorDisplayName);
return compositorDisplayName == displayName;
};
auto it = std::find_if(gCompositorList.begin(), gCompositorList.end(), lambda);
if (it == gCompositorList.end())
{
it = std::find_if(gTopmostCompositorList.begin(), gTopmostCompositorList.end(), lambda);
if (it == gTopmostCompositorList.end())
{
return nullptr;
}
}
return it->compositor;
}
void CompositorController::initialize()
{
static bool sCompositorInitialized = false;
if (sCompositorInitialized)
return;
char const *rdkshellPowerKey = getenv("RDKSHELL_POWER_KEY_CODE");
if (rdkshellPowerKey)
{
int keyCode = atoi(rdkshellPowerKey);
if (keyCode > 0)
{
gPowerKeyCode = (uint32_t)keyCode;
}
}
Logger::log(LogLevel::Information, "the power key is set to %d", gPowerKeyCode);
char const *rdkshellPowerKeyEnable = getenv("RDKSHELL_ENABLE_POWER_KEY");
if (rdkshellPowerKeyEnable)
{
int powerValue = atoi(rdkshellPowerKeyEnable);
if (powerValue > 0)
{
gPowerKeyEnabled = true;
}
}
Logger::log(LogLevel::Information, "power key support enabled: %d", gPowerKeyEnabled);
char const *rdkshellFrontPanelButton = getenv("RDKSHELL_FRONT_PANEL_BUTTON_CODE");
if (rdkshellFrontPanelButton)
{
int keyCode = atoi(rdkshellFrontPanelButton);
if (keyCode > 0)
{
gFrontPanelButtonCode = (uint32_t)keyCode;
}
}
Logger::log(LogLevel::Information, "the front panel code is set to %d", gFrontPanelButtonCode);
char const *rdkshellPowerKeyReleaseOnlyEnabled = getenv("RDKSHELL_POWER_RELEASE_ONLY");
if (rdkshellPowerKeyReleaseOnlyEnabled && (strcmp(rdkshellPowerKeyReleaseOnlyEnabled,"1") == 0))
{
gRdkShellPowerKeyReleaseOnlyEnabled = true;
}
Logger::log(LogLevel::Information, "rdkshell power key release only enabled %d", gRdkShellPowerKeyReleaseOnlyEnabled);
char const *rdkshellKeyIgnore = getenv("RDKSHELL_ENABLE_KEY_IGNORE");
Logger::log(LogLevel::Information, "key ignore feature enabled status [%d]", (NULL==rdkshellKeyIgnore));
if (NULL != rdkshellKeyIgnore)
{
int keyIgnoreValue = atoi(rdkshellKeyIgnore);
Logger::log(LogLevel::Information, "key ignore feature [%d]", keyIgnoreValue);
if (keyIgnoreValue > 0)
{
gIgnoreKeyInputEnabled = true;
Logger::log(LogLevel::Information, "key ignore feature is enabled");
}
}
char const *rdkshellCompositorType = getenv("RDKSHELL_COMPOSITOR_TYPE");
if (NULL == rdkshellCompositorType)
{
Logger::log(LogLevel::Information, "compositor type is empty, setting to nested by default");
}
else
{
if (strcmp(rdkshellCompositorType, "surface") == 0)
{
gRdkShellCompositorType = SURFACE;
uint32_t width = 0;
uint32_t height = 0;
RdkShell::EssosInstance::instance()->resolution(width, height);
RdkCompositorSurface::createMainCompositor("rdkshell_display", width, height);
}
else if (strcmp(rdkshellCompositorType, "nested") != 0)
{
Logger::log(LogLevel::Information, "invalid compositor type, setting to nested by default ");
}
}
const char* cursorImageName = getenv("RDKSHELL_CURSOR_IMAGE");
if (cursorImageName == nullptr)
{
Logger::log(LogLevel::Information, "cursor image not set");
}
else
{
gCursor = std::make_shared<Cursor>(std::string(cursorImageName));
}
sCompositorInitialized = true;
}
bool CompositorController::moveToFront(const std::string& client)
{
CompositorListIterator it;
CompositorList* compositorInfoList = nullptr;
if (!getCompositorInfo(client, it, &compositorInfoList))
{
Logger::log(LogLevel::Information, "%s not found and cannot move to front ", client.c_str());
return false;
}
if (it == compositorInfoList->begin())
{
Logger::log(LogLevel::Information, "%s is already in front and cannot move to front ", client.c_str());
return false;
}
auto compositorInfo = *it;
compositorInfoList->erase(it);
compositorInfoList->insert(compositorInfoList->begin(), compositorInfo);
return true;
}
bool CompositorController::moveToBack(const std::string& client)
{
CompositorListIterator it;
CompositorList* compositorInfoList = nullptr;
if (!getCompositorInfo(client, it, &compositorInfoList))
{
Logger::log(LogLevel::Information, "%s not found and cannot be moved behind ", client.c_str());
return false;
}
auto compositorInfo = *it;
compositorInfoList->erase(it);
compositorInfoList->push_back(compositorInfo);
return true;
}
bool CompositorController::moveBehind(const std::string& client, const std::string& target)
{
CompositorListIterator clientIt;
CompositorList* clientCompositorList = nullptr;
if (!getCompositorInfo(client, clientIt, &clientCompositorList))
{
Logger::log(LogLevel::Information, "%s not found and cannot be moved behind ", client.c_str());
return false;
}
CompositorListIterator targetIt;
CompositorList* targetCompositorList = nullptr;
if (!getCompositorInfo(target, targetIt, &targetCompositorList))
{
Logger::log(LogLevel::Information, "%s not found and cannot be moved behind ", target.c_str());
return false;
}
if (clientCompositorList != targetCompositorList)
{
Logger::log(LogLevel::Information, "%s and %s not on the same compositor stack ", client.c_str(), target.c_str());
return false;
}
auto compositorInfo = *clientIt;
targetCompositorList->erase(clientIt);
if (getCompositorInfo(target, targetIt))
{
targetCompositorList->insert(++targetIt, compositorInfo);
return true;
}
return false;
}
bool CompositorController::getFocused(std::string& client)
{
client = "";
if (gFocusedCompositor.compositor)
{
client = gFocusedCompositor.name;
}
return true;
}
bool CompositorController::setFocus(const std::string& client)
{
CompositorListIterator it;
if (getCompositorInfo(client, it))
{
std::string previousFocusedClient = !gFocusedCompositor.name.empty() ? gFocusedCompositor.name:"none";
Logger::log(LogLevel::Information, "rdkshell_focus setFocus: the focused client is now %s. previous: %s", it->name.c_str(), previousFocusedClient.c_str());
if ((gFocusedCompositor.compositor) && (gFocusedCompositor.compositor->isKeyPressed()))
{
gPendingKeyUpListeners.push_back(gFocusedCompositor.compositor);
}
if (gFocusedCompositor.compositor)
{
gFocusedCompositor.compositor->setFocused(false);
}
gFocusedCompositor = *it;
gFocusedCompositor.compositor->setFocused(true);
return true;
}
return false;
}
bool CompositorController::kill(const std::string& client)
{
CompositorListIterator it;
CompositorList* compositorInfoList;
if (getCompositorInfo(client, it, &compositorInfoList))
{
std::string clientDisplayName = standardizeName(client);
RdkShell::Animator::instance()->stopAnimation(clientDisplayName);
// cleanup key intercepts
std::vector<std::map<uint32_t, std::vector<KeyInterceptInfo>>::iterator> emptyKeyCodeEntries;
std::map<uint32_t, std::vector<KeyInterceptInfo>>::iterator entry = gKeyInterceptInfoMap.begin();
while(entry != gKeyInterceptInfoMap.end())
{
std::vector<KeyInterceptInfo>& interceptMap = entry->second;
std::vector<KeyInterceptInfo>::iterator interceptMapEntry=interceptMap.begin();
while (interceptMapEntry != interceptMap.end())
{
if ((*interceptMapEntry).client == clientDisplayName)
{
//interceptMapEntry = interceptMap.erase(interceptMapEntry);
CompositorInfo info;
(*interceptMapEntry).compositorInfo = info;
interceptMapEntry++;
}
else
{
interceptMapEntry++;
}
}
if (interceptMap.size() == 0)
{
entry = gKeyInterceptInfoMap.erase(entry);
}
else
{
entry++;
}
}
// cleanup key listeners
for (std::map<uint32_t, std::vector<KeyListenerInfo>>::iterator iter = it->keyListenerInfo.begin(); iter != it->keyListenerInfo.end(); iter++)
{
iter->second.clear();
}
it->keyListenerInfo.clear();
it->eventListeners.clear();
std::cout << "adding " << clientDisplayName << " to the deleted list\n";
gDeletedCompositors.push_back(*it);
compositorInfoList->erase(it);
if (gFocusedCompositor.name == clientDisplayName)
{
// this may be changed to next available compositor
gFocusedCompositor.name = "";
gFocusedCompositor.compositor = nullptr;
Logger::log(LogLevel::Information, "rdkshell_focus kill: the focused client has been killed: %s. there is no focused client.", clientDisplayName.c_str());
}
return true;
}
return false;
}
bool CompositorController::addKeyIntercept(const std::string& client, const uint32_t& keyCode, const uint32_t& flags)
{
//Logger::log(LogLevel::Information, "key intercept added " << keyCode << " flags " << flags << std::endl;
return setKeyIntercept(client, keyCode, flags, true);
}
bool CompositorController::setKeyIntercept(const std::string& client, const uint32_t& keyCode, const uint32_t& flags, const bool always)
{
CompositorListIterator it;
struct KeyInterceptInfo info;
info.keyCode = keyCode;
info.flags = flags;
info.always = always;
info.client = standardizeName(client);
if (getCompositorInfo(client, it))
{
info.compositorInfo = *it;
}
if (gKeyInterceptInfoMap.end() == gKeyInterceptInfoMap.find(keyCode))
{
gKeyInterceptInfoMap[keyCode] = std::vector<KeyInterceptInfo>();
gKeyInterceptInfoMap[keyCode].push_back(info);
}
else
{
std::string clientDisplayName = standardizeName(client);
bool isEntryAvailable = false;
for (int i=0; i<gKeyInterceptInfoMap[keyCode].size(); i++)
{
struct KeyInterceptInfo& info_t = gKeyInterceptInfoMap[keyCode][i];
if ((info_t.flags == flags) && (info_t.client == clientDisplayName))
{
isEntryAvailable = true;
if (info_t.always == always)
{
break;
}
else
{
gKeyInterceptInfoMap[keyCode][i] = info;
}
}
}
if (false == isEntryAvailable)
{
gKeyInterceptInfoMap[keyCode].push_back(info);
}
}
return true;
}
bool CompositorController::removeKeyIntercept(const std::string& client, const uint32_t& keyCode, const uint32_t& flags)
{
if (keyCode == RDKSHELL_WILDCARD_KEY_CODE)
{
std::string clientDisplayName = standardizeName(client);
for (std::map<uint32_t, std::vector<KeyInterceptInfo>>::iterator keyInterceptIterator = gKeyInterceptInfoMap.begin(); keyInterceptIterator != gKeyInterceptInfoMap.end(); keyInterceptIterator++)
{
std::vector<KeyInterceptInfo>& interceptInfo = keyInterceptIterator->second;
std::vector<KeyInterceptInfo>::iterator interceptInfoIterator = interceptInfo.begin();
while(interceptInfoIterator != interceptInfo.end())
{
if ((*interceptInfoIterator).client == clientDisplayName)
{
interceptInfoIterator = interceptInfo.erase(interceptInfoIterator);
}
else
{
interceptInfoIterator++;
}
}
}
}
if (client == "*")
{
std::vector<std::vector<KeyInterceptInfo>::iterator> keyMapEntries;
std::map<uint32_t, std::vector<KeyInterceptInfo>>::iterator it = gKeyInterceptInfoMap.find(keyCode);
if (it != gKeyInterceptInfoMap.end())
{
std::vector<KeyInterceptInfo>::iterator entry = gKeyInterceptInfoMap[keyCode].begin();
while(entry != gKeyInterceptInfoMap[keyCode].end())
{
if ((*entry).flags == flags)
{
if (((*entry).compositorInfo.compositor) && ((*entry).compositorInfo.compositor->isKeyPressed()))
{
gPendingKeyUpListeners.push_back((*entry).compositorInfo.compositor);
}
entry = gKeyInterceptInfoMap[keyCode].erase(entry);
}
else
{
entry++;
}
}
if ( gKeyInterceptInfoMap[keyCode].size() == 0)
{
gKeyInterceptInfoMap.erase(keyCode);
}
}
return true;
}
std::string clientDisplayName = standardizeName(client);
CompositorListIterator it;
if (getCompositorInfo(client, it))
{
if (gKeyInterceptInfoMap.end() != gKeyInterceptInfoMap.find(keyCode))
{
bool isEntryAvailable = false;
std::vector<KeyInterceptInfo>::iterator entryPos = gKeyInterceptInfoMap[keyCode].end();
for (std::vector<KeyInterceptInfo>::iterator it = gKeyInterceptInfoMap[keyCode].begin() ; it != gKeyInterceptInfoMap[keyCode].end(); ++it)
{
if (((*it).flags == flags) && ((*it).client == clientDisplayName))
{
entryPos = it;
isEntryAvailable = true;
break;
}
}
if (true == isEntryAvailable)
{
if (((*entryPos).compositorInfo.compositor) && ((*entryPos).compositorInfo.compositor->isKeyPressed()))
{
gPendingKeyUpListeners.push_back((*entryPos).compositorInfo.compositor);
}
gKeyInterceptInfoMap[keyCode].erase(entryPos);
if (gKeyInterceptInfoMap[keyCode].size() == 0)
{
gKeyInterceptInfoMap.erase(keyCode);
}
}
}
return true;
}
return false;
}
bool CompositorController::addKeyListener(const std::string& client, const uint32_t& keyCode, const uint32_t& flags, std::map<std::string, RdkShellData> &listenerProperties)
{
bool activate = false, propagate = true;
for ( const auto &property : listenerProperties)
{
if (property.first == "activate")
{
activate = property.second.toBoolean();
}
else if (property.first == "propagate")
{
propagate = property.second.toBoolean();
}
}
Logger::log(LogLevel::Information, "key listener added client: %s activate: %d propagate: %d RDKShell keyCode: %d flags: %d", client.c_str(), activate, propagate, keyCode, flags);
CompositorListIterator it;
if (getCompositorInfo(client, it))
{
struct KeyListenerInfo info;
info.keyCode = keyCode;
info.flags = flags;
info.activate = activate;
info.propagate = propagate;
if (it->keyListenerInfo.end() == it->keyListenerInfo.find(keyCode))
{
it->keyListenerInfo[keyCode] = std::vector<KeyListenerInfo>();
it->keyListenerInfo[keyCode].push_back(info);
}
else
{
std::vector<KeyListenerInfo>& keyListenerEntry = it->keyListenerInfo[keyCode];
bool isEntryAvailable = false;
for (int i = 0; i < keyListenerEntry.size(); i++)
{
struct KeyListenerInfo& listenerInfo = keyListenerEntry[i];
if (listenerInfo.flags == flags)
{
listenerInfo.activate = activate;
listenerInfo.propagate = propagate;
isEntryAvailable = true;
break;
}
}
if (false == isEntryAvailable)
{
keyListenerEntry.push_back(info);
}
}
return true;
}
return false;
}
bool CompositorController::addNativeKeyListener(const std::string& client, const uint32_t& keyCode, const uint32_t& flags, std::map<std::string, RdkShellData> &listenerProperties)
{
uint32_t mappedKeyCode = 0, mappedFlags = 0;
keyCodeFromWayland(keyCode, flags, mappedKeyCode, mappedFlags);
Logger::log(LogLevel::Information, "Native keyCode: %d flags: %d converted to RDKShell keyCode: %d flags: %d", keyCode, flags, mappedKeyCode, mappedFlags);
return CompositorController::addKeyListener(client, mappedKeyCode, mappedFlags, listenerProperties);
}
bool CompositorController::removeKeyListener(const std::string& client, const uint32_t& keyCode, const uint32_t& flags)
{
Logger::log(LogLevel::Information, "key listener removed client: %s RDKShell keyCode %d flags %d", client.c_str(), keyCode, flags);
CompositorListIterator it;
if (getCompositorInfo(client, it))
{
if (it->keyListenerInfo.end() != it->keyListenerInfo.find(keyCode))
{
bool isEntryAvailable = false;
std::vector<KeyListenerInfo>::iterator entryPos = it->keyListenerInfo[keyCode].end();
for (std::vector<KeyListenerInfo>::iterator iter = it->keyListenerInfo[keyCode].begin() ; iter != it->keyListenerInfo[keyCode].end(); ++iter)
{
if ((*iter).flags == flags)
{
entryPos = iter;
isEntryAvailable = true;
break;
}
}
if (true == isEntryAvailable)