-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
762 lines (637 loc) · 22.3 KB
/
main.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
/**
* Copyright 2021 Johannes Marbach
* Copyright 2024 Bardia Moshiri
* Copyright 2024 David Badiei
*
* This file is part of bootman, hereafter referred to as the program.
*
* This program 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 program 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 program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "backends.h"
#include "command_line.h"
#include "config.h"
#include "indev.h"
#include "bootman.h"
#include "terminal.h"
#include "theme.h"
#include "themes.h"
#include "lv_drv_conf.h"
#if USE_FBDEV
#include "lv_drivers/display/fbdev.h"
#endif /* USE_FBDEV */
#if USE_DRM
#include "lv_drivers/display/drm.h"
#endif /* USE_DRM */
#if USE_MINUI
#include "lv_drivers/display/minui.h"
#endif /* USE_MINUI */
#include "lvgl/lvgl.h"
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/reboot.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/time.h>
#define MAX_PARTITIONS 50
#define MAX_LINE_LENGTH 256
#define PERSIST_PARTITION "/dev/disk/by-partlabel/furios_persist"
#define MOUNT_POINT "/furios_persist"
#define PARTITIONS_FILE "/furios_persist/bootman/partitions"
typedef struct {
char *name;
char *label;
} PartitionEntry;
typedef struct {
PartitionEntry entries[MAX_PARTITIONS];
size_t count;
} PartitionList;
/**
* Static variables
*/
cli_opts cli_options;
config_opts conf_opts;
static lv_color_t *buf = NULL;
static lv_disp_draw_buf_t disp_buf;
bool is_alternate_theme = false;
lv_obj_t *reboot_btn;
lv_obj_t *shutdown_btn;
/**
* Static prototypes
*/
/**
* Set the UI theme.
*
* @param is_dark true if the dark theme should be applied, false if the light theme should be applied
*/
static void set_theme(bool is_dark);
/**
* Handle LV_EVENT_CLICKED events from the shutdown button.
*
* @param event the event object
*/
static void shutdown_btn_clicked_cb(lv_event_t *event);
/**
* Handle LV_EVENT_VALUE_CHANGED events from the shutdown message box.
*
* @param event the event object
*/
static void shutdown_mbox_value_changed_cb(lv_event_t *event);
/**
* Handle LV_EVENT_CLICKED events from the reboot button.
*
* @param event the event object
*/
static void reboot_btn_clicked_cb(lv_event_t *event);
/**
* Handle LV_EVENT_VALUE_CHANGED events from the reboot message box.
*
* @param event the event object
*/
static void reboot_mbox_value_changed_cb(lv_event_t *event);
/**
* Handle clicks on partition buttons
*
* @param event the event object containing partition data
*/
static void partition_btn_clicked_cb(lv_event_t *event);
/**
* Check partition, read version, and flash boot images.
*
* @param partition_name name of the partition to boot
* @param error_msg buffer to store error message on failure
* @param error_msg_size size of error message buffer
* @return 0 on success, -1 on failure with error_msg set
*/
static int check_and_flash_partition(const char *partition_name, char *error_msg, size_t error_msg_size);
/**
* Handle partition boot confirmation dialog events.
*
* @param event the event object
*/
static void partition_boot_confirm_cb(lv_event_t *event);
/**
* Show error message dialog.
*
* @param message the error message to display
*/
static void show_error_dialog(const char *message);
/**
* Handle error dialog button events.
*
* @param event the event object
*/
static void error_mbox_event_cb(lv_event_t *event);
/**
* Read and parse partition entries from config file
*
* @return PartitionList* List of parsed partitions or NULL on error
*/
static PartitionList* read_partition_entries(void);
/**
* Check if system is encrypted by looking for droidian_encrypted mapper.
*
* @return true if system is encrypted, false otherwise
*/
static bool is_encrypted(void);
/**
* Free memory allocated for partition list
*
* @param list PartitionList to free
*/
static void free_partition_list(PartitionList *list);
/**
* Create partition buttons based on parsed entries
*
* @param label_container Container to place buttons in
* @param list List of partitions to create buttons for
*/
static void create_partition_buttons(lv_obj_t *label_container, PartitionList *list);
/**
* Create main UI
*
* @param hor_res horizontal resolution
* @param ver_res vertical resolution
*/
static void create_ui(uint32_t hor_res, uint32_t ver_res);
/**
* Initialize UI
*/
static void initialize_ui(void);
/**
* Reboots the device.
*/
static void reboot_device(void);
/**
* Shuts down the device.
*/
static void shutdown(void);
/**
* Handle termination signals sent to the process.
*
* @param signum the signal's number
*/
static void sigaction_handler(int signum);
/**
* Static functions
*/
static void set_theme(bool is_alternate) {
theme_apply(&(themes_themes[is_alternate ? conf_opts.theme.alternate_id : conf_opts.theme.default_id]));
}
static void shutdown_btn_clicked_cb(lv_event_t *event) {
LV_UNUSED(event);
static const char *btns[] = { "Yes", "No", "" };
lv_obj_t *mbox = lv_msgbox_create(NULL, NULL, "Shutdown device?", btns, false);
lv_obj_set_size(mbox, 400, LV_SIZE_CONTENT);
lv_obj_add_event_cb(mbox, shutdown_mbox_value_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);
lv_obj_center(mbox);
}
static void shutdown_mbox_value_changed_cb(lv_event_t *event) {
lv_obj_t *mbox = lv_event_get_current_target(event);
if (lv_msgbox_get_active_btn(mbox) == 0)
shutdown();
lv_msgbox_close(mbox);
}
static void reboot_btn_clicked_cb(lv_event_t *event) {
LV_UNUSED(event);
static const char *btns[] = { "Yes", "No", "" };
lv_obj_t *mbox = lv_msgbox_create(NULL, NULL, "Reboot device?", btns, false);
lv_obj_set_size(mbox, 400, LV_SIZE_CONTENT);
lv_obj_add_event_cb(mbox, reboot_mbox_value_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);
lv_obj_center(mbox);
}
static void reboot_mbox_value_changed_cb(lv_event_t *event) {
lv_obj_t *mbox = lv_event_get_current_target(event);
if (lv_msgbox_get_active_btn(mbox) == 0)
reboot_device();
lv_msgbox_close(mbox);
}
static void partition_btn_clicked_cb(lv_event_t *e) {
char *partition_name = (char *)lv_event_get_user_data(e);
if (partition_name) {
static const char *btns[] = { "Yes", "No", "" };
lv_obj_t *mbox = lv_msgbox_create(NULL, NULL, "Boot this partition?", btns, false);
lv_obj_set_size(mbox, 400, LV_SIZE_CONTENT);
lv_obj_add_event_cb(mbox, partition_boot_confirm_cb, LV_EVENT_VALUE_CHANGED, partition_name);
lv_obj_center(mbox);
}
}
static int check_and_flash_partition(const char *partition_name, char *error_msg, size_t error_msg_size) {
char lvm_path[256];
char mount_point[] = "/mnt_tmp";
char version[256];
struct stat st;
char cmd[1024];
snprintf(lvm_path, sizeof(lvm_path), "/dev/droidian/%s", partition_name);
if (stat(lvm_path, &st) != 0) {
snprintf(error_msg, error_msg_size, "Partition %s does not exist", lvm_path);
return -1;
}
if (stat(mount_point, &st) != 0) {
if (mkdir(mount_point, 0755) != 0) {
snprintf(error_msg, error_msg_size, "Failed to create mount point: %s", strerror(errno));
return -1;
}
}
if (mount(lvm_path, mount_point, "ext4", 0, NULL) != 0) {
snprintf(error_msg, error_msg_size, "Failed to mount partition: %s", strerror(errno));
return -1;
}
/* this was added on 13.0.6 and thus the boot manager will only work with versions 13.0.6 or newer */
char config_path[512];
snprintf(config_path, sizeof(config_path), "%s/usr/lib/furios/device/flash-bootimage.conf", mount_point);
FILE *fp = fopen(config_path, "r");
if (!fp) {
snprintf(error_msg, error_msg_size, "Config file not found: %s", config_path);
umount(mount_point);
return -1;
}
char line[256];
bool version_found = false;
while (fgets(line, sizeof(line), fp)) {
if (strncmp(line, "VERSION=", 8) == 0) {
strncpy(version, line + 8, sizeof(version) - 1);
version[strcspn(version, "\n")] = 0;
version_found = true;
break;
}
}
fclose(fp);
if (!version_found) {
snprintf(error_msg, error_msg_size, "VERSION not found in config file");
umount(mount_point);
return -1;
}
char boot_image_path[512];
snprintf(boot_image_path, sizeof(boot_image_path), "%s/boot/boot.img-%s", mount_point, version);
if (stat(boot_image_path, &st) != 0) {
snprintf(error_msg, error_msg_size, "Boot image not found: %s", boot_image_path);
umount(mount_point);
return -1;
}
char dtbo_image_path[512];
snprintf(dtbo_image_path, sizeof(dtbo_image_path), "%s/boot/dtbo.img-%s", mount_point, version);
if (stat(dtbo_image_path, &st) != 0) {
snprintf(error_msg, error_msg_size, "DTBO image not found: %s", dtbo_image_path);
umount(mount_point);
return -1;
}
snprintf(cmd, sizeof(cmd), "dd if='%s' of=/dev/disk/by-partlabel/boot_a bs=4M", boot_image_path);
printf("Executing: %s\n", cmd);
if (system(cmd) != 0) {
snprintf(error_msg, error_msg_size, "Failed to flash boot image");
umount(mount_point);
return -1;
}
sync();
snprintf(cmd, sizeof(cmd), "dd if='%s' of=/dev/disk/by-partlabel/dtbo_a bs=4M", dtbo_image_path);
printf("Executing: %s\n", cmd);
if (system(cmd) != 0) {
snprintf(error_msg, error_msg_size, "Failed to flash dtbo image");
umount(mount_point);
return -1;
}
sync();
FILE *next_boot = fopen("/furios_persist/bootman/next-boot", "w");
if (!next_boot) {
snprintf(error_msg, error_msg_size, "Failed to create next-boot file");
umount(mount_point);
return -1;
}
fprintf(next_boot, "%s", partition_name);
fclose(next_boot);
sync();
umount(mount_point);
return 0;
}
static void partition_boot_confirm_cb(lv_event_t *event) {
lv_obj_t *mbox = lv_event_get_current_target(event);
char *partition_name = (char *)lv_event_get_user_data(event);
if (lv_msgbox_get_active_btn(mbox) == 0) {
printf("Preparing to boot partition: %s\n", partition_name);
char error_msg[512];
if (check_and_flash_partition(partition_name, error_msg, sizeof(error_msg)) == 0) {
printf("Successfully prepared boot for partition: %s\n", partition_name);
static const char *btns[] = {"OK", ""};
lv_obj_t *success_mbox = lv_msgbox_create(NULL, "Success", "Boot partition prepared successfully. System will now reboot.", btns, false);
lv_obj_set_size(success_mbox, 400, LV_SIZE_CONTENT);
lv_obj_center(success_mbox);
lv_obj_add_event_cb(success_mbox, reboot_mbox_value_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);
} else {
show_error_dialog(error_msg);
}
}
free(partition_name);
lv_msgbox_close(mbox);
}
static void show_error_dialog(const char *message) {
static const char *btns[] = {"OK", ""};
lv_obj_t *error_mbox = lv_msgbox_create(NULL, "Error", message, btns, false);
lv_obj_set_size(error_mbox, 400, LV_SIZE_CONTENT);
lv_obj_add_event_cb(error_mbox, error_mbox_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
lv_obj_center(error_mbox);
}
static void error_mbox_event_cb(lv_event_t *event) {
lv_obj_t *mbox = lv_event_get_current_target(event);
lv_msgbox_close(mbox);
}
static void reboot_device(void) {
sync();
reboot(RB_AUTOBOOT);
}
static void shutdown(void) {
sync();
reboot(RB_POWER_OFF);
}
static void sigaction_handler(int signum) {
LV_UNUSED(signum);
terminal_reset_current_terminal();
exit(0);
}
static bool is_encrypted(void) {
struct stat st;
return (stat("/dev/mapper/droidian_encrypted", &st) == 0);
}
static void free_partition_list(PartitionList *list) {
for (size_t i = 0; i < list->count; i++) {
free(list->entries[i].name);
free(list->entries[i].label);
}
}
static PartitionList* read_partition_entries(void) {
struct stat st;
PartitionList *list = calloc(1, sizeof(PartitionList));
if (!list)
return NULL;
printf("Reading partition entries...\n");
if (stat(PERSIST_PARTITION, &st) != 0) {
printf("Persist partition not found\n");
free(list);
return NULL;
}
if (stat(MOUNT_POINT, &st) != 0) {
if (mkdir(MOUNT_POINT, 0755) != 0) {
printf("Failed to create mount point: %s\n", strerror(errno));
free(list);
return NULL;
}
}
FILE *mtab = fopen("/proc/mounts", "r");
char line[256];
int is_mounted = 0;
while (fgets(line, sizeof(line), mtab)) {
if (strstr(line, MOUNT_POINT)) {
is_mounted = 1;
break;
}
}
fclose(mtab);
if (!is_mounted) {
if (mount(PERSIST_PARTITION, MOUNT_POINT, "ext4", 0, NULL) != 0) {
printf("Failed to mount partition: %s\n", strerror(errno));
free(list);
return NULL;
}
printf("Mounted %s at %s\n", PERSIST_PARTITION, MOUNT_POINT);
}
FILE *fp = fopen(PARTITIONS_FILE, "r");
if (!fp) {
printf("Failed to open partitions file: %s\n", strerror(errno));
if (!is_mounted)
umount(MOUNT_POINT);
free(list);
return NULL;
}
printf("Parsing partition entries:\n");
char buffer[MAX_LINE_LENGTH];
while (fgets(buffer, sizeof(buffer), fp) && list->count < MAX_PARTITIONS) {
if (buffer[0] == '\n' || buffer[0] == '\0')
continue;
buffer[strcspn(buffer, "\n")] = 0;
printf("Found entry: %s\n", buffer);
char *str = buffer;
char *token;
char *saveptr;
token = strtok_r(str, ":", &saveptr);
if (!token)
continue;
list->entries[list->count].name = strdup(token);
token = strtok_r(NULL, "", &saveptr);
if (!token)
list->entries[list->count].label = strdup(list->entries[list->count].name);
else
list->entries[list->count].label = strdup(token);
printf("Added partition %zu: name='%s', label='%s'\n",
list->count,
list->entries[list->count].name,
list->entries[list->count].label);
list->count++;
}
printf("Found total %zu partitions\n", list->count);
fclose(fp);
return list;
}
static void create_partition_buttons(lv_obj_t *label_container, PartitionList *list) {
if (!list)
return;
int base_y_offset = 150;
int button_spacing = 120;
for (size_t i = 0; i < list->count; i++) {
lv_obj_t *btn = lv_btn_create(label_container);
lv_obj_set_width(btn, LV_PCT(100));
lv_obj_set_height(btn, 100);
lv_obj_t *btn_label = lv_label_create(btn);
lv_label_set_text(btn_label, list->entries[i].label);
char *partition_name = strdup(list->entries[i].name);
lv_obj_add_event_cb(btn, partition_btn_clicked_cb, LV_EVENT_CLICKED, partition_name);
lv_obj_align(btn, LV_ALIGN_TOP_MID, 0, base_y_offset + (i * button_spacing));
lv_obj_set_flex_flow(btn, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(btn, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
}
reboot_btn = lv_btn_create(label_container);
lv_obj_set_width(reboot_btn, LV_PCT(100));
lv_obj_set_height(reboot_btn, 100);
lv_obj_t *reboot_label = lv_label_create(reboot_btn);
lv_label_set_text(reboot_label, "Reboot");
lv_obj_add_event_cb(reboot_btn, reboot_btn_clicked_cb, LV_EVENT_CLICKED, NULL);
lv_obj_align(reboot_btn, LV_ALIGN_TOP_MID, 0, base_y_offset + (list->count * button_spacing));
lv_obj_set_flex_flow(reboot_btn, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(reboot_btn, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
shutdown_btn = lv_btn_create(label_container);
lv_obj_set_width(shutdown_btn, LV_PCT(100));
lv_obj_set_height(shutdown_btn, 100);
lv_obj_t *shutdown_label = lv_label_create(shutdown_btn);
lv_label_set_text(shutdown_label, "Shutdown");
lv_obj_add_event_cb(shutdown_btn, shutdown_btn_clicked_cb, LV_EVENT_CLICKED, NULL);
lv_obj_align(shutdown_btn, LV_ALIGN_TOP_MID, 0, base_y_offset + ((list->count + 1) * button_spacing));
lv_obj_set_flex_flow(shutdown_btn, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(shutdown_btn, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
}
static void create_ui(uint32_t hor_res, uint32_t ver_res) {
/* Clear the screen */
lv_obj_clean(lv_scr_act());
/* Check for encryption */
if (is_encrypted()) {
printf("System is encrypted, cannot proceed\n");
exit(1);
}
/* Figure out a few numbers for sizing and positioning */
const int keyboard_height = ver_res > hor_res ? ver_res / 3 : ver_res / 2;
const int padding = keyboard_height / 8;
const int label_width = hor_res - 2 * padding;
/* Main flexbox */
lv_obj_t *container = lv_obj_create(lv_scr_act());
lv_obj_set_flex_flow(container, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(container, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_obj_set_size(container, LV_PCT(100), ver_res - keyboard_height);
lv_obj_set_pos(container, 0, 0);
lv_obj_set_style_pad_row(container, padding, LV_PART_MAIN);
lv_obj_set_style_pad_bottom(container, padding, LV_PART_MAIN);
/* Label container */
lv_obj_t *label_container = lv_obj_create(container);
lv_obj_set_size(label_container, label_width, LV_PCT(100));
lv_obj_set_flex_grow(label_container, 1);
/* FuriOS label container */
lv_obj_t *furios_label_container = lv_obj_create(lv_scr_act());
lv_obj_set_width(furios_label_container, LV_PCT(100));
lv_obj_set_height(furios_label_container, LV_SIZE_CONTENT);
lv_obj_set_align(furios_label_container, LV_ALIGN_BOTTOM_MID);
/* FuriOS label text */
lv_obj_t *furios_label = lv_label_create(furios_label_container);
lv_label_set_text(furios_label, "FuriOS Boot Manager");
lv_obj_align(furios_label, LV_ALIGN_BOTTOM_MID, 0, 0);
/* Create partition buttons */
PartitionList *list = read_partition_entries();
create_partition_buttons(label_container, list);
if (list) {
free_partition_list(list);
free(list);
} else {
printf("No partitions found in persist\n");
exit(1);
}
}
static void initialize_ui(void) {
/* Initialise LVGL and set up logging callback */
lv_init();
/* Initialise display driver */
static lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
/* Initialise framebuffer driver and query display size */
uint32_t hor_res = 0;
uint32_t ver_res = 0;
uint32_t dpi = 0;
switch (conf_opts.general.backend) {
#if USE_FBDEV
case BACKENDS_BACKEND_FBDEV:
fbdev_init();
fbdev_get_sizes(&hor_res, &ver_res, &dpi);
disp_drv.flush_cb = fbdev_flush;
break;
#endif /* USE_FBDEV */
#if USE_DRM
case BACKENDS_BACKEND_DRM:
drm_init();
drm_get_sizes((lv_coord_t *)&hor_res, (lv_coord_t *)&ver_res, &dpi);
disp_drv.flush_cb = drm_flush;
break;
#endif /* USE_DRM */
#if USE_MINUI
case BACKENDS_BACKEND_MINUI:
minui_init();
minui_get_sizes(&hor_res, &ver_res, &dpi);
disp_drv.flush_cb = minui_flush;
break;
#endif /* USE_MINUI */
default:
printf("Unable to find suitable backend\n");
exit(EXIT_FAILURE);
}
/* Override display parameters with command line options if necessary */
if (cli_options.hor_res > 0)
hor_res = cli_options.hor_res;
if (cli_options.ver_res > 0)
ver_res = cli_options.ver_res;
if (cli_options.dpi > 0)
dpi = cli_options.dpi;
/* Prepare display buffer */
const size_t buf_size = hor_res * ver_res / 10; /* At least 1/10 of the display size is recommended */
if (buf == NULL)
buf = (lv_color_t *)malloc(buf_size * sizeof(lv_color_t));
lv_disp_draw_buf_init(&disp_buf, buf, NULL, buf_size);
/* Register display driver */
disp_drv.draw_buf = &disp_buf;
disp_drv.hor_res = hor_res;
disp_drv.ver_res = ver_res;
disp_drv.offset_x = cli_options.x_offset;
disp_drv.offset_y = cli_options.y_offset;
disp_drv.dpi = dpi;
lv_disp_drv_register(&disp_drv);
printf("Display resolution: %dx%d, DPI: %d, Offset: (%d, %d)\n",
hor_res, ver_res, dpi, cli_options.x_offset, cli_options.y_offset);
/* Connect input devices */
indev_auto_connect(conf_opts.input.keyboard, conf_opts.input.pointer, conf_opts.input.touchscreen);
indev_set_up_mouse_cursor();
/* Initialise theme */
set_theme(is_alternate_theme);
/* Create UI elements */
create_ui(hor_res, ver_res);
}
/**
* Main
*/
int main(int argc, char *argv[]) {
/* Parse command line options */
cli_parse_opts(argc, argv, &cli_options);
/* Parse config files */
config_parse(cli_options.config_files, cli_options.num_config_files, &conf_opts);
/* Prepare current TTY and clean up on termination */
terminal_prepare_current_terminal();
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = sigaction_handler;
sigaction(SIGINT, &action, NULL);
sigaction(SIGTERM, &action, NULL);
initialize_ui();
/* Run lvgl in "tickless" mode */
while(1) {
lv_task_handler();
usleep(5000);
}
return 0;
}
/**
* Tick generation
*/
/**
* Generate tick for LVGL.
*
* @return tick in ms
*/
uint32_t get_tick(void) {
static uint64_t start_ms = 0;
if (start_ms == 0) {
struct timeval tv_start;
gettimeofday(&tv_start, NULL);
start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
}
struct timeval tv_now;
gettimeofday(&tv_now, NULL);
uint64_t now_ms;
now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;
uint32_t time_ms = now_ms - start_ms;
return time_ms;
}