-
Notifications
You must be signed in to change notification settings - Fork 2
/
Ambilight.c
1709 lines (1437 loc) · 49.1 KB
/
Ambilight.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
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
/*
* About:
* The "Ambilight" ELF daemon utility with GUI settings for some funlights and bias lighting functionality.
*
* Author:
* EXL
*
* License:
* MIT
*
* Application type:
* Deamon + GUI + ATI + Funlights.
*/
#include <loader.h>
#include <apps.h>
#include <mem.h>
#include <uis.h>
#include <res_def.h>
#include <utilities.h>
#include <filesystem.h>
#include <dl.h>
#include <ati.h>
#include "icons/icon_ambilight_48x48.h"
#define MAX_NUMBER_LENGTH (6)
#define MIN_NUMBER_VALUE (10)
#define MAX_NUMBER_VALUE (60000)
#define MAX_HEX_LENGTH (10)
#define MAX_HEX_VALUE (0xFFF)
#define KEY_LONG_PRESS_START_MS (1500)
#define KEY_LONG_PRESS_STOP_MS (2500)
#define NETWORK_MS_GAP_CONSTANT (80)
#define AMBILIGHT_RECT_CONSTANT (16)
#define LIGHT_SENSOR_MAX_VALUE (0xE2)
typedef enum {
APP_STATE_ANY,
APP_STATE_INIT,
APP_STATE_MAIN,
APP_STATE_EDIT,
APP_STATE_SELECT,
APP_STATE_POPUP,
APP_STATE_WARNING,
APP_STATE_VIEW,
APP_STATE_MAX
} APP_STATE_T;
typedef enum {
APP_DISPLAY_HIDE,
APP_DISPLAY_SHOW
} APP_DISPLAY_T;
typedef enum {
APP_TIMER_EXIT = 0xE490,
APP_TIMER_DO_LIGHTS
} APP_TIMER_T;
typedef enum {
APP_RESOURCE_STRING_NAME,
APP_RESOURCE_ICON_AMBILIGHT,
APP_RESOURCE_STRING_MODE,
APP_RESOURCE_STRING_DELAY,
APP_RESOURCE_STRING_COLOR,
APP_RESOURCE_MAX
} APP_RESOURCES_T;
typedef enum {
APP_MENU_ITEM_FIRST,
APP_MENU_ITEM_MODE = APP_MENU_ITEM_FIRST,
APP_MENU_ITEM_DELAY,
APP_MENU_ITEM_COLOR,
APP_MENU_ITEM_START_LIGHTS,
APP_MENU_ITEM_STOP_LIGHTS,
APP_MENU_ITEM_RESET,
APP_MENU_ITEM_HELP,
APP_MENU_ITEM_ABOUT,
APP_MENU_ITEM_EXIT,
APP_MENU_ITEM_MAX
} APP_MENU_ITEM_T;
typedef enum {
APP_SELECT_ITEM_FIRST,
APP_SELECT_ITEM_AMBILIGHT = APP_SELECT_ITEM_FIRST,
APP_SELECT_ITEM_COLOR,
APP_SELECT_ITEM_COLOR_BLINK,
APP_SELECT_ITEM_FLASH_25,
APP_SELECT_ITEM_FLASH_50,
APP_SELECT_ITEM_FLASH_100,
APP_SELECT_ITEM_NETWORK,
APP_SELECT_ITEM_BATTERY,
APP_SELECT_ITEM_RAINBOW,
APP_SELECT_ITEM_RANDOM,
APP_SELECT_ITEM_STROBOSCOPE,
APP_SELECT_ITEM_STROBO_COLOR,
APP_SELECT_ITEM_BACKLIGHT,
APP_SELECT_ITEM_MAX
} APP_SELECT_ITEM_T;
typedef enum {
APP_POPUP_CHANGED,
APP_POPUP_WARNING,
APP_POPUP_STARTED,
APP_POPUP_STOPPED,
APP_POPUP_RESETED
} APP_POPUP_T;
typedef enum {
APP_WARNING_FLASH,
APP_WARNING_RESET
} APP_WARNING_T;
typedef enum {
APP_VIEW_HELP,
APP_VIEW_ABOUT
} APP_VIEW_T;
typedef enum {
APP_EDIT_DELAY,
APP_EDIT_COLOR
} APP_EDIT_T;
typedef struct {
APP_SELECT_ITEM_T mode;
UINT32 delay;
UINT16 color;
} APP_OPTIONS_T;
typedef struct {
UINT8 r;
UINT8 g;
UINT8 b;
} APP_COLOR_T;
typedef enum {
RAINBOW_RED,
RAINBOW_ORANGE,
RAINBOW_YELLOW,
RAINBOW_GREEN,
RAINBOW_LIGHT_BLUE,
RAINBOW_BLUE,
RAINBOW_VIOLET,
RAINBOW_WHITE
} RAINBOW_COLOR_T;
typedef enum {
STATE_NETWORK_SIGNAL,
STATE_BATTERY
} PHONE_STATE_T;
typedef struct {
APPLICATION_T app;
RESOURCE_ID resources[APP_RESOURCE_MAX];
APP_DISPLAY_T state;
APP_POPUP_T popup;
APP_VIEW_T view;
APP_EDIT_T edit;
APP_MENU_ITEM_T menu_current_item_index;
APP_WARNING_T warning;
APP_OPTIONS_T options;
UINT64 ms_key_press_start;
UINT32 timer_handle;
BOOL is_lights_started;
APP_COLOR_T color_current;
APP_COLOR_T color_pattern;
RAINBOW_COLOR_T rainbow;
UINT32 blink;
} APP_INSTANCE_T;
UINT32 Register(const char *elf_path_uri, const char *args, UINT32 ev_code);
static UINT32 ApplicationStart(EVENT_STACK_T *ev_st, REG_ID_T reg_id, void *reg_hdl);
static UINT32 ApplicationStop(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 ApplicationDisplay(EVENT_STACK_T *ev_st, APPLICATION_T *app, APP_DISPLAY_T display);
static UINT32 InitResourses(RESOURCE_ID *resources);
static UINT32 FreeResourses(RESOURCE_ID *resources);
static UINT32 HandleStateEnter(EVENT_STACK_T *ev_st, APPLICATION_T *app, ENTER_STATE_TYPE_T state);
static UINT32 HandleStateExit(EVENT_STACK_T *ev_st, APPLICATION_T *app, EXIT_STATE_TYPE_T state);
static UINT32 DeleteDialog(APPLICATION_T *app);
static void HandleEventMain(EVENT_STACK_T *ev_st, APPLICATION_T *app, APP_ID_T app_id, REG_ID_T reg_id);
static UINT32 HandleEventHide(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventShow(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventSelect(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventMenuRequestListItems(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventKeyPress(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventKeyRelease(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventTimerExpired(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventEditData(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventEditDone(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventSelectDone(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventSelectRequestListItems(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventYes(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventRequestListItems(EVENT_STACK_T *ev_st, APPLICATION_T *app, APP_STATE_T app_state);
static UINT32 HandleEventCancel(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 SendMenuItemsToList(EVENT_STACK_T *ev_st, APPLICATION_T *app, UINT32 start, UINT32 count);
static UINT32 SendSelectItemsToList(EVENT_STACK_T *ev_st, APPLICATION_T *app, UINT32 start, UINT32 count);
static const WCHAR *GetTriggerOptionString(APP_SELECT_ITEM_T item);
static UINT32 ResetSettingsToDefaultValues(APPLICATION_T *app);
static UINT32 ReadFileConfig(APPLICATION_T *app, const WCHAR *file_config_path);
static UINT32 SaveFileConfig(APPLICATION_T *app, const WCHAR *file_config_path);
static BOOL IsKeyPadLocked(void);
static UINT32 SetLoopTimer(APPLICATION_T *app, UINT32 period);
static UINT32 ConvertDecToHexColor(UINT32 number, WCHAR *heximal);
static UINT32 ConvertHexColorToDec(const WCHAR *heximal, UINT32 *number);
static UINT32 StartLights(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 ProcessLights(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 StopLights(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 Ambilight(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 PhoneState(EVENT_STACK_T *ev_st, APPLICATION_T *app, PHONE_STATE_T state);
static UINT32 Rainbow(EVENT_STACK_T *ev_st, APPLICATION_T *app, BOOL random_colors);
static UINT32 Stroboscope(EVENT_STACK_T *ev_st, APPLICATION_T *app, BOOL flashlight, BOOL color, BOOL random);
static UINT16 GetAverageColorRGB444(EVENT_STACK_T *ev_st, APPLICATION_T *app, UINT16 start, UINT16 size);
static UINT32 Backlight(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static const char g_app_name[APP_NAME_LEN] = "Ambilight";
static const WCHAR g_str_app_name[] = L"Ambilight";
static const WCHAR g_str_mode[] = L"Light Mode:";
static const WCHAR g_str_e_mode[] = L"Light Mode";
static const WCHAR g_str_mode_ambilight[] = L"Ambilight";
static const WCHAR g_str_mode_color[] = L"Color";
static const WCHAR g_str_mode_color_blink[] = L"Color Blink";
static const WCHAR g_str_mode_flash25[] = L"Flash 25%";
static const WCHAR g_str_mode_flash50[] = L"Flash 50%";
static const WCHAR g_str_mode_flash100[] = L"Flash 100%";
static const WCHAR g_str_mode_network[] = L"Network";
static const WCHAR g_str_mode_battery[] = L"Battery";
static const WCHAR g_str_mode_rainbow[] = L"Rainbow";
static const WCHAR g_str_mode_random[] = L"Random";
static const WCHAR g_str_mode_stroboscope[] = L"Stroboscope";
static const WCHAR g_str_mode_strobo_color[] = L"Strobo Color";
static const WCHAR g_str_mode_backlight[] = L"Backlight";
static const WCHAR g_str_color[] = L"RGB Color:";
static const WCHAR g_str_e_color[] = L"HEX Color";
static const WCHAR g_str_delay[] = L"Delay (in ms):";
static const WCHAR g_str_e_delay[] = L"Delay (in ms)";
static const WCHAR g_str_start_ligths[] = L"Start Lights!";
static const WCHAR g_str_stop_ligths[] = L"Stop Lights!";
static const WCHAR g_str_reset[] = L"Reset to default";
static const WCHAR g_str_help[] = L"Help...";
static const WCHAR g_str_e_help[] = L"Help";
static const WCHAR g_str_about[] = L"About...";
static const WCHAR g_str_e_about[] = L"About";
static const WCHAR g_str_exit[] = L"Exit";
static const WCHAR g_str_changed[] = L"Changed:";
static const WCHAR g_str_started[] = L"Lights service started!";
static const WCHAR g_str_stopped[] = L"Lights service stopped!";
static const WCHAR g_str_warn_activated[] = L"The \"Flash 100%\" mode is activated, don't use this mode for too long!";
static const WCHAR g_str_warn_flash[] = L"This can be dangerous to the phone's flash diode, are you sure?";
static const WCHAR g_str_reseted[] = L"All settings have been reset to default values!";
static const WCHAR g_str_warn_reset[] = L"Do you want to reset settings to default?";
static const WCHAR g_str_help_content_p1[] =
L"Bias lighting (ambilight) daemon program for Motorola P2K phones.\n\n"
L"Press and hold the right soft key for 1.5-2.5 seconds to bring up the main application menu.\n\n"
L"The following modes are available for use:\n\n"
L"Ambilight - The side LEDs use the most common color on the screen as source of light.\n\n"
L"Color - The side LEDs use the specified HEX string with (0xRGB) format as source of light.\n\n"
L"Color Blink - The side LEDs blink with the specified HEX string with (0xRGB) format as source of light.\n\n"
L"Flash 25% - The flash LED lights up at 25% brightness of maximum.\n\n"
L"Flash 50% - The flash LED lights up at 50% brightness of maximum.\n\n"
L"Flash 100% - The flash LED lights up at maximum brightness. Danger! This mode may damage the flash LED!\n\n"
L"Network - The side LEDs blink on network signal strength: 0..30% - red, 30..60% - yellow, 60..100% - green\n\n"
L"Battery - The side LEDs blink depending on battery: 0..30% - red, 30..60% - yellow, 60..100% - green\n\n"
L"Rainbow - Smooth rainbow effect on the side LEDs.\n\n"
L"Random - Smooth color transitions to random colors.\n\n"
L"Stroboscope - Strobe mode with side LEDs (white color) and flash LED.\n\n"
L"Strobo Color - Strobe mode with side LEDs (random color) and flash LED.\n\n"
L"Backlight - Smooth change in backlight brightness based on the Light Sensor.\n\n"
L"Some modes require setting the delay and the color.\n\n"
L"Managing of lighting services is available in the main menu of the ELF application.\n";
static const WCHAR g_str_about_content_p1[] = L"Version: 1.0";
static const WCHAR g_str_about_content_p2[] = L"\x00A9 EXL, 13-Sep-2023.";
static const WCHAR g_str_about_content_p3[] = L"https://github.com/EXL/P2kElfs/tree/master/Ambilight";
static const WCHAR g_str_about_content_p4[] = L" "; /* HACK: gap */
static const UINT8 g_key_app_menu = KEY_SOFT_LEFT;
static const UINT8 g_key_app_exit = KEY_0;
static WCHAR g_config_file_path[FS_MAX_URI_NAME_LENGTH]; /* TODO: Can it be non-global? */
static const EVENT_HANDLER_ENTRY_T g_state_any_hdls[] = {
{ EV_REVOKE_TOKEN, APP_HandleUITokenRevoked },
{ EV_KEY_PRESS, HandleEventKeyPress },
{ EV_KEY_RELEASE, HandleEventKeyRelease },
{ EV_TIMER_EXPIRED, HandleEventTimerExpired },
{ STATE_HANDLERS_END, NULL }
};
static const EVENT_HANDLER_ENTRY_T g_state_init_hdls[] = {
/*
* EXL, 01-Dec-2022:
* Please comment out the `{ EV_GRANT_TOKEN, APP_HandleUITokenGranted }` construction
* then change `app_instance->state` variable to `APP_DISPLAY_HIDE` value
* then change `routing_stack` argument in `APP_InitAppData()` function to `APP_DISPLAY_HIDE` value
* for pure daemon behavior with no GUI at start.
*/
/* { EV_GRANT_TOKEN, APP_HandleUITokenGranted }, */
{ STATE_HANDLERS_END, NULL }
};
static const EVENT_HANDLER_ENTRY_T g_state_main_hdls[] = {
{ EV_REQUEST_LIST_ITEMS, HandleEventMenuRequestListItems },
{ EV_DONE, HandleEventHide },
{ EV_DIALOG_DONE, HandleEventHide },
{ EV_SELECT, HandleEventSelect },
{ STATE_HANDLERS_END, NULL }
};
static const EVENT_HANDLER_ENTRY_T g_state_edit_hdls[] = {
{ EV_DATA, HandleEventEditData },
{ EV_DONE, HandleEventEditDone },
{ EV_DIALOG_DONE, HandleEventCancel },
{ EV_CANCEL, HandleEventCancel },
{ STATE_HANDLERS_END, NULL }
};
static const EVENT_HANDLER_ENTRY_T g_state_select_hdls[] = {
{ EV_DONE, HandleEventSelectDone },
{ EV_DIALOG_DONE, HandleEventCancel },
{ EV_CANCEL, HandleEventCancel },
{ EV_REQUEST_LIST_ITEMS, HandleEventSelectRequestListItems },
{ STATE_HANDLERS_END, NULL }
};
static const EVENT_HANDLER_ENTRY_T g_state_popup_hdls[] = {
{ EV_DONE, HandleEventCancel },
{ EV_DIALOG_DONE, HandleEventCancel },
{ STATE_HANDLERS_END, NULL }
};
static const EVENT_HANDLER_ENTRY_T g_state_warn_hdls[] = {
{ EV_DONE, HandleEventCancel },
{ EV_DIALOG_DONE, HandleEventCancel },
{ EV_NO, HandleEventCancel },
{ EV_YES, HandleEventYes },
{ STATE_HANDLERS_END, NULL }
};
static const STATE_HANDLERS_ENTRY_T g_state_table_hdls[] = {
{ APP_STATE_ANY, NULL, NULL, g_state_any_hdls },
{ APP_STATE_INIT, NULL, NULL, g_state_init_hdls },
{ APP_STATE_MAIN, HandleStateEnter, HandleStateExit, g_state_main_hdls },
{ APP_STATE_EDIT, HandleStateEnter, HandleStateExit, g_state_edit_hdls },
{ APP_STATE_SELECT, HandleStateEnter, HandleStateExit, g_state_select_hdls },
{ APP_STATE_POPUP, HandleStateEnter, HandleStateExit, g_state_popup_hdls },
{ APP_STATE_WARNING, HandleStateEnter, HandleStateExit, g_state_warn_hdls },
{ APP_STATE_VIEW, HandleStateEnter, HandleStateExit, g_state_popup_hdls } /* Same as popups. */
};
UINT32 Register(const char *elf_path_uri, const char *args, UINT32 ev_code) {
UINT32 status;
UINT32 ev_code_base;
ev_code_base = ev_code;
status = APP_Register(&ev_code_base, 1, g_state_table_hdls, APP_STATE_MAX, (void *) ApplicationStart);
u_atou(elf_path_uri, g_config_file_path);
g_config_file_path[u_strlen(g_config_file_path) - 3] = '\0';
u_strcat(g_config_file_path, L"cfg");
LdrStartApp(ev_code_base);
return status;
}
static UINT32 ApplicationStart(EVENT_STACK_T *ev_st, REG_ID_T reg_id, void *reg_hdl) {
UINT32 status;
APP_INSTANCE_T *app_instance;
status = RESULT_FAIL;
if (AFW_InquireRoutingStackByRegId(reg_id) != RESULT_OK) {
app_instance = (APP_INSTANCE_T *) APP_InitAppData((void *) HandleEventMain, sizeof(APP_INSTANCE_T),
reg_id, 0, 1, 1, 1, APP_DISPLAY_HIDE, 0);
InitResourses(app_instance->resources);
app_instance->state = APP_DISPLAY_HIDE;
app_instance->popup = APP_POPUP_CHANGED;
app_instance->view = APP_VIEW_HELP;
app_instance->edit = APP_EDIT_DELAY;
app_instance->warning = APP_WARNING_FLASH;
app_instance->menu_current_item_index = APP_MENU_ITEM_FIRST;
app_instance->ms_key_press_start = 0LLU;
app_instance->is_lights_started = FALSE;
app_instance->color_current.r = 0xF;
app_instance->color_current.g = 0x0;
app_instance->color_current.b = 0x0;
app_instance->color_pattern.r = 0xF;
app_instance->color_pattern.g = 0x0;
app_instance->color_pattern.b = 0x0;
app_instance->rainbow = RAINBOW_RED;
app_instance->blink = 0;
ResetSettingsToDefaultValues((APPLICATION_T *) app_instance);
if (DL_FsFFileExist(g_config_file_path)) {
ReadFileConfig((APPLICATION_T *) app_instance, g_config_file_path);
} else {
SaveFileConfig((APPLICATION_T *) app_instance, g_config_file_path);
}
status = APP_Start(ev_st, &app_instance->app, APP_STATE_INIT,
g_state_table_hdls, HandleEventHide, g_app_name, 0);
StartLights(ev_st, (APPLICATION_T *) app_instance);
}
return status;
}
static UINT32 ApplicationStop(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
UINT32 status;
APP_INSTANCE_T *app_instance;
status = RESULT_OK;
app_instance = (APP_INSTANCE_T *) app;
APP_ConsumeEv(ev_st, app);
DeleteDialog(app);
FreeResourses(app_instance->resources);
status |= StopLights(ev_st, app);
status |= APP_Exit(ev_st, app, 0);
LdrUnloadELF(&Lib);
return status;
}
static UINT32 InitResourses(RESOURCE_ID *resources) {
UINT32 status;
status = RESULT_OK;
status |= DRM_CreateResource(&resources[APP_RESOURCE_STRING_NAME], RES_TYPE_STRING,
(void *) g_str_app_name, (u_strlen(g_str_app_name) + 1) * sizeof(WCHAR));
status |= DRM_CreateResource(&resources[APP_RESOURCE_STRING_MODE], RES_TYPE_STRING,
(void *) g_str_e_mode, (u_strlen(g_str_e_mode) + 1) * sizeof(WCHAR));
status |= DRM_CreateResource(&resources[APP_RESOURCE_STRING_DELAY], RES_TYPE_STRING,
(void *) g_str_e_delay, (u_strlen(g_str_e_delay) + 1) * sizeof(WCHAR));
status |= DRM_CreateResource(&resources[APP_RESOURCE_STRING_COLOR], RES_TYPE_STRING,
(void *) g_str_e_color, (u_strlen(g_str_e_color) + 1) * sizeof(WCHAR));
status |= DRM_CreateResource(&resources[APP_RESOURCE_ICON_AMBILIGHT], RES_TYPE_GRAPHICS,
(void *) ambilight_48x48_gif, sizeof(ambilight_48x48_gif));
return status;
}
static UINT32 FreeResourses(RESOURCE_ID *resources) {
UINT32 status;
UINT32 i;
status = RESULT_OK;
for (i = 0; i < APP_RESOURCE_MAX; ++i) {
if (resources[i]) {
status |= DRM_ClearResource(resources[i]);
}
}
return status;
}
static UINT32 ApplicationDisplay(EVENT_STACK_T *ev_st, APPLICATION_T *app, APP_DISPLAY_T display) {
UINT32 status;
APP_INSTANCE_T *app_instance;
void *hdl;
UINT32 routing_stack;
status = RESULT_OK;
app_instance = (APP_INSTANCE_T *) app;
if (app_instance->state != display) {
app_instance->state = display;
hdl = (void *) ((app_instance->state == APP_DISPLAY_SHOW) ? APP_HandleEvent : APP_HandleEventPrepost);
routing_stack = (app_instance->state == APP_DISPLAY_SHOW);
status = APP_ChangeRoutingStack(app, ev_st, hdl, routing_stack, 0, 1, 1);
}
return status;
}
static UINT32 HandleStateEnter(EVENT_STACK_T *ev_st, APPLICATION_T *app, ENTER_STATE_TYPE_T state) {
APP_INSTANCE_T *app_instance;
SU_PORT_T port;
CONTENT_T content;
UIS_DIALOG_T dialog;
APP_STATE_T app_state;
UINT32 starting_list_item;
RESOURCE_ID edit_title;
WCHAR edit_buffer[MAX_HEX_LENGTH];
if (state != ENTER_STATE_ENTER) {
return RESULT_OK;
}
app_instance = (APP_INSTANCE_T *) app;
DeleteDialog(app);
port = app->port;
app_state = app->state;
memclr(&content, sizeof(CONTENT_T));
switch (app_state) {
case APP_STATE_MAIN:
starting_list_item = APP_MENU_ITEM_FIRST;
dialog = UIS_CreateList(&port, 0, APP_MENU_ITEM_MAX, 0, &starting_list_item, 0, 2, NULL,
app_instance->resources[APP_RESOURCE_STRING_NAME]);
/* Insert cursor to proper position. */
if (app_instance->menu_current_item_index != APP_MENU_ITEM_FIRST) {
APP_UtilAddEvUISListChange(ev_st, app, 0, app_instance->menu_current_item_index + 1, APP_MENU_ITEM_MAX,
FALSE, 2, NULL, NULL, NULL);
UIS_HandleEvent(dialog, ev_st);
}
break;
case APP_STATE_EDIT:
switch (app_instance->edit) {
case APP_EDIT_DELAY:
edit_title = app_instance->resources[APP_RESOURCE_STRING_DELAY];
u_ltou(app_instance->options.delay, edit_buffer);
dialog = UIS_CreateCharacterEditor(&port, edit_buffer, 32 /* Numbers only. */, MAX_NUMBER_LENGTH,
FALSE, NULL, edit_title);
break;
case APP_EDIT_COLOR:
edit_title = app_instance->resources[APP_RESOURCE_STRING_COLOR];
ConvertDecToHexColor(app_instance->options.color, edit_buffer);
dialog = UIS_CreateCharacterEditor(&port, edit_buffer, 5 /* Text. */, MAX_NUMBER_LENGTH,
FALSE, NULL, edit_title);
break;
default:
break;
}
break;
case APP_STATE_SELECT:
starting_list_item = APP_MENU_ITEM_FIRST;
dialog = UIS_CreateSelectionEditor(&port, 0, APP_SELECT_ITEM_MAX, app_instance->options.mode + 1,
&starting_list_item, 0, NULL, app_instance->resources[APP_RESOURCE_STRING_MODE]);
break;
case APP_STATE_POPUP:
switch (app_instance->popup) {
default:
case APP_POPUP_CHANGED:
UIS_MakeContentFromString("MCq0NMCq1NMCq2", &content,
g_str_changed, g_str_e_mode, GetTriggerOptionString(app_instance->options.mode));
break;
case APP_POPUP_WARNING:
UIS_MakeContentFromString("MCq0", &content, g_str_warn_activated);
break;
case APP_POPUP_STARTED:
UIS_MakeContentFromString("MCq0", &content, g_str_started);
break;
case APP_POPUP_STOPPED:
UIS_MakeContentFromString("MCq0", &content, g_str_stopped);
break;
case APP_POPUP_RESETED:
UIS_MakeContentFromString("MCq0", &content, g_str_reseted);
break;
}
dialog = UIS_CreateTransientNotice(&port, &content, NOTICE_TYPE_OK);
break;
case APP_STATE_WARNING:
switch (app_instance->warning) {
default:
case APP_WARNING_FLASH:
UIS_MakeContentFromString("MCq0", &content, g_str_warn_flash);
break;
case APP_WARNING_RESET:
UIS_MakeContentFromString("MCq0", &content, g_str_warn_reset);
break;
}
dialog = UIS_CreateConfirmation(&port, &content);
break;
case APP_STATE_VIEW:
switch (app_instance->view) {
default:
case APP_VIEW_HELP:
UIS_MakeContentFromString("q0Nq1", &content, g_str_e_help,
g_str_help_content_p1
);
break;
case APP_VIEW_ABOUT:
UIS_MakeContentFromString("q0NMCp1NMCq2NMCq3NMCq4NMCq5NMCq6", &content, g_str_app_name,
app_instance->resources[APP_RESOURCE_ICON_AMBILIGHT],
g_str_about_content_p1, g_str_about_content_p2, g_str_about_content_p3,
g_str_about_content_p4, g_str_about_content_p4);
break;
}
dialog = UIS_CreateViewer(&port, &content, NULL);
break;
default:
dialog = DialogType_None;
break;
}
if (dialog == DialogType_None) {
return RESULT_FAIL;
}
app->dialog = dialog;
switch (app_state) {
case APP_STATE_MAIN:
SendMenuItemsToList(ev_st, app, 1, APP_MENU_ITEM_MAX);
break;
case APP_STATE_SELECT:
SendSelectItemsToList(ev_st, app, 1, APP_SELECT_ITEM_MAX);
break;
default:
break;
}
return RESULT_OK;
}
static UINT32 HandleStateExit(EVENT_STACK_T *ev_st, APPLICATION_T *app, EXIT_STATE_TYPE_T state) {
if (state == EXIT_STATE_EXIT) {
DeleteDialog(app);
return RESULT_OK;
}
return RESULT_FAIL;
}
static UINT32 DeleteDialog(APPLICATION_T *app) {
if (app->dialog != DialogType_None) {
UIS_Delete(app->dialog);
app->dialog = DialogType_None;
return RESULT_OK;
}
return RESULT_FAIL;
}
static void HandleEventMain(EVENT_STACK_T *ev_st, APPLICATION_T *app, APP_ID_T app_id, REG_ID_T reg_id) {
APP_INSTANCE_T *app_instance;
app_instance = (APP_INSTANCE_T *) app;
if (app_instance->state == APP_DISPLAY_SHOW) {
APP_HandleEvent(ev_st, app, app_id, reg_id);
} else {
APP_HandleEventPrepost(ev_st, app, app_id, reg_id);
}
}
static UINT32 HandleEventHide(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
ApplicationDisplay(ev_st, app, APP_DISPLAY_HIDE);
return RESULT_OK;
}
static UINT32 HandleEventShow(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
if (app->state != APP_STATE_MAIN) {
APP_UtilChangeState(APP_STATE_MAIN, ev_st, app);
}
ApplicationDisplay(ev_st, app, APP_DISPLAY_SHOW);
return RESULT_OK;
}
static UINT32 HandleEventSelect(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
UINT32 status;
APP_INSTANCE_T *app_instance;
EVENT_T *event;
status = RESULT_OK;
app_instance = (APP_INSTANCE_T *) app;
event = AFW_GetEv(ev_st);
app_instance->menu_current_item_index = event->data.index - 1;
switch (app_instance->menu_current_item_index) {
case APP_MENU_ITEM_MODE:
status |= APP_UtilChangeState(APP_STATE_SELECT, ev_st, app);
break;
case APP_MENU_ITEM_DELAY:
app_instance->edit = APP_EDIT_DELAY;
status |= APP_UtilChangeState(APP_STATE_EDIT, ev_st, app);
break;
case APP_MENU_ITEM_COLOR:
app_instance->edit = APP_EDIT_COLOR;
status |= APP_UtilChangeState(APP_STATE_EDIT, ev_st, app);
break;
case APP_MENU_ITEM_START_LIGHTS:
if (app_instance->options.mode == APP_SELECT_ITEM_FLASH_100) {
app_instance->warning = APP_WARNING_FLASH;
status |= APP_UtilChangeState(APP_STATE_WARNING, ev_st, app);
} else {
app_instance->popup = APP_POPUP_STARTED;
StartLights(ev_st, app);
status |= APP_UtilChangeState(APP_STATE_POPUP, ev_st, app);
}
break;
case APP_MENU_ITEM_STOP_LIGHTS:
app_instance->popup = APP_POPUP_STOPPED;
StopLights(ev_st, app);
status |= APP_UtilChangeState(APP_STATE_POPUP, ev_st, app);
break;
case APP_MENU_ITEM_RESET:
app_instance->warning = APP_WARNING_RESET;
status |= APP_UtilChangeState(APP_STATE_WARNING, ev_st, app);
break;
case APP_MENU_ITEM_HELP:
app_instance->view = APP_VIEW_HELP;
status |= APP_UtilChangeState(APP_STATE_VIEW, ev_st, app);
break;
case APP_MENU_ITEM_ABOUT:
app_instance->view = APP_VIEW_ABOUT;
status |= APP_UtilChangeState(APP_STATE_VIEW, ev_st, app);
break;
case APP_MENU_ITEM_EXIT:
status |= APP_UtilStartTimer(100, APP_TIMER_EXIT, app);
break;
default:
break;
}
status |= APP_ConsumeEv(ev_st, app);
return status;
}
static UINT32 HandleEventMenuRequestListItems(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
return HandleEventRequestListItems(ev_st, app, APP_STATE_MAIN);
}
static UINT32 HandleEventKeyPress(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
APP_INSTANCE_T *app_instance;
EVENT_T *event;
UINT8 key;
app_instance = (APP_INSTANCE_T *) app;
event = AFW_GetEv(ev_st);
key = event->data.key_pressed;
if (key == g_key_app_menu || key == g_key_app_exit) {
app_instance->ms_key_press_start = suPalTicksToMsec(suPalReadTime());
}
return RESULT_OK;
}
static UINT32 HandleEventKeyRelease(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
APP_INSTANCE_T *app_instance;
EVENT_T *event;
UINT8 key;
UINT32 ms_key_release_stop;
app_instance = (APP_INSTANCE_T *) app;
event = AFW_GetEv(ev_st);
key = event->data.key_pressed;
if (key == g_key_app_menu || key == g_key_app_exit) {
/*
* Detect long key press between 500 ms (0.5 s) and 1500 ms (1.5 s) and ignore rest.
*/
ms_key_release_stop = (UINT32) (suPalTicksToMsec(suPalReadTime()) - app_instance->ms_key_press_start);
if ((ms_key_release_stop >= KEY_LONG_PRESS_START_MS) && (ms_key_release_stop <= KEY_LONG_PRESS_STOP_MS)) {
if (key == g_key_app_menu) {
if (!IsKeyPadLocked()) {
APP_ConsumeEv(ev_st, app);
return HandleEventShow(ev_st, app);
}
} else if (key == g_key_app_exit) {
#ifdef EXIT_BY_KEY
APP_UtilStartTimer(100, APP_TIMER_EXIT, app);
#endif
}
}
}
return RESULT_OK;
}
static UINT32 HandleEventTimerExpired(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
EVENT_T *event;
APP_TIMER_T timer_id;
event = AFW_GetEv(ev_st);
timer_id = ((DL_TIMER_DATA_T *) event->attachment)->ID;
if (timer_id == APP_TIMER_EXIT) {
/* Play an exit sound using quiet speaker. */
DL_AudPlayTone(0x00, 0xFF);
/* Exit App! */
return ApplicationStop(ev_st, app);
} else if (timer_id == APP_TIMER_DO_LIGHTS) {
ProcessLights(ev_st, app);
}
return RESULT_OK;
}
static UINT32 HandleEventEditData(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
UINT32 status;
APP_INSTANCE_T *app_instance;
EVENT_T *event;
UINT32 data;
status = RESULT_OK;
app_instance = (APP_INSTANCE_T *) app;
event = AFW_GetEv(ev_st);
if (event->attachment != NULL) {
switch (app_instance->edit) {
case APP_EDIT_DELAY:
data = u_atol(event->attachment);
if (data > MAX_NUMBER_VALUE) {
data = MAX_NUMBER_VALUE;
}
if (data < MIN_NUMBER_VALUE) {
data = MIN_NUMBER_VALUE;
}
app_instance->options.delay = data;
break;
case APP_EDIT_COLOR:
ConvertHexColorToDec(event->attachment, &data);
app_instance->options.color = data;
break;
default:
break;
}
}
if (app_instance->is_lights_started) {
if (app_instance->options.mode == APP_SELECT_ITEM_FLASH_100) {
StopLights(ev_st, app);
} else {
StartLights(ev_st, app);
}
}
status |= SaveFileConfig(app, g_config_file_path);
status |= APP_UtilChangeState(APP_STATE_MAIN, ev_st, app);
return status;
}
static UINT32 HandleEventEditDone(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
UINT32 status;
ADD_EVENT_DATA_T *event_data;
status = RESULT_OK;
/*
* EXL, 05-Jan-2023:
* I don't know how, but this trick with uninitialised pointer just works.
* Structure `ADD_EVENT_DATA_T &event_data;` on stack leads to a strange problem
* when editing the list with deleting one symbol, only one character appears after editing.
*/
status |= AFW_AddEvEvD(ev_st, EV_REQUEST_DATA, event_data);
status |= UIS_HandleEvent(app->dialog, ev_st);
return status;
}
static UINT32 HandleEventSelectDone(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
UINT32 status;
APP_INSTANCE_T *app_instance;
EVENT_T *event;
status = RESULT_OK;
app_instance = (APP_INSTANCE_T *) app;
event = AFW_GetEv(ev_st);
app_instance->options.mode = event->data.index - 1;
app_instance->popup = APP_POPUP_CHANGED;
if (app_instance->is_lights_started) {
if (app_instance->options.mode == APP_SELECT_ITEM_FLASH_100) {
StopLights(ev_st, app);
} else {
StartLights(ev_st, app);
}
}
status |= SaveFileConfig(app, g_config_file_path);
status |= APP_UtilChangeState(APP_STATE_POPUP, ev_st, app);
return status;
}
static UINT32 HandleEventSelectRequestListItems(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
return HandleEventRequestListItems(ev_st, app, APP_STATE_SELECT);
}
static UINT32 HandleEventYes(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
UINT32 status;
APP_INSTANCE_T *app_instance;
status = RESULT_OK;
app_instance = (APP_INSTANCE_T *) app;
switch (app_instance->warning) {
default:
case APP_WARNING_FLASH:
app_instance->popup = APP_POPUP_WARNING;
break;
case APP_WARNING_RESET:
app_instance->popup = APP_POPUP_RESETED;
ResetSettingsToDefaultValues(app);
break;
}
StartLights(ev_st, app);
status |= SaveFileConfig(app, g_config_file_path);
status |= APP_UtilChangeState(APP_STATE_POPUP, ev_st, app);
return status;
}
static UINT32 HandleEventRequestListItems(EVENT_STACK_T *ev_st, APPLICATION_T *app, APP_STATE_T app_state) {
UINT32 status;
EVENT_T *event;
UINT32 start;
UINT32 count;
status = RESULT_OK;
if (!app->focused) {
return status;
}
event = AFW_GetEv(ev_st);
start = event->data.list_items_req.begin_idx;
count = event->data.list_items_req.count;
status |= APP_ConsumeEv(ev_st, app);
switch (app_state) {
default:
case APP_STATE_MAIN:
status |= SendMenuItemsToList(ev_st, app, start, count);
break;
case APP_STATE_SELECT:
SendSelectItemsToList(ev_st, app, start, count);
break;
}
return status;
}
static UINT32 HandleEventCancel(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
UINT32 status;
status = RESULT_OK;
status |= APP_UtilChangeState(APP_STATE_MAIN, ev_st, app);
return status;
}
static UINT32 SendMenuItemsToList(EVENT_STACK_T *ev_st, APPLICATION_T *app, UINT32 start, UINT32 count) {
UINT32 status;
INT32 result;
APP_INSTANCE_T *app_instance;
UINT32 i;
LIST_ENTRY_T *list;
WCHAR hex[MAX_HEX_LENGTH];
status = RESULT_OK;
result = RESULT_OK;
app_instance = (APP_INSTANCE_T *) app;
if (count == 0) {
return RESULT_FAIL;
}
list = (LIST_ENTRY_T *) suAllocMem(sizeof(LIST_ENTRY_T) * APP_MENU_ITEM_MAX, &result);
if (result != RESULT_OK) {
return RESULT_FAIL;
}
for (i = 0; i < APP_MENU_ITEM_MAX; ++i) {
memclr(&list[i], sizeof(LIST_ENTRY_T));
list[i].editable = FALSE;