-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
1198 lines (998 loc) · 35.9 KB
/
main.c
File metadata and controls
1198 lines (998 loc) · 35.9 KB
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
/**
* Copyright 2021 Johannes Marbach
* Copyright 2024 David Badiei
* Copyright 2025 Bardia Moshiri
*
* 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 <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <dirent.h>
#include <sys/reboot.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <libinput.h>
#include <linux/input.h>
#define MAX_PARTITIONS 50
#define MAX_LINE_LENGTH 256
#define PERSIST_PARTITION "/dev/disk/by-partlabel/vendor_boot_a"
#define MOUNT_POINT "/furios-persist"
#define PARTITIONS_FILE "/furios-persist/bootman/partitions"
#define DROIDIAN_VG_PATH "/dev/droidian"
#define FURIOS_VG_PATH "/dev/furios"
typedef struct {
char *name;
char *label;
char *vg_path;
} 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 PartitionEntry **allocated_entries = NULL;
static size_t allocated_entries_count = 0;
/* Navigation variables */
lv_obj_t **nav_buttons = NULL;
int nav_button_count = 0;
int current_button_index = 0;
pthread_t key_thread;
volatile bool key_thread_running = true;
/**
* 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 vg_path volume group path (DROIDIAN_VG_PATH or FURIOS_VG_PATH) or NULL for external paths
* @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, const char *vg_path, char *error_msg, size_t error_msg_size);
/**
* Check if a volume group exists
*
* @param vg_path Path to the volume group
* @return true if the volume group exists, false otherwise
*/
static bool volume_group_exists(const char *vg_path);
/**
* 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
* Checks for both FuriOS and Droidian volume groups
*
* @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 or furios_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);
/**
* Initialize button navigation
*
* @param total_buttons Total number of buttons for navigation
*/
static void init_button_navigation(int total_buttons);
/**
* Update button highlighting
*/
static void update_button_highlight(void);
/**
* Check if a file is an input device
*
* @param path Path to the input device
* @return 1 if it's an input device, 0 otherwise
*/
static int is_input_device(const char *path);
/**
* Initialize libinput and monitor for key events
*
* @param arg *arg is unused
*/
static void* key_input_thread(void *arg);
/**
* Open callback for libinput
*
* @param path Device path to open
* @param flags Open flags
* @param user_data User data pointer (user_data is unused)
* @return File descriptor or negative error code
*/
static int open_restricted(const char *path, int flags, void *user_data);
/**
* Close callback for libinput
*
* @param fd File descriptor to close
* @param user_data User data pointer (user_data is unused)
*/
static void close_restricted(int fd, void *user_data);
/**
* 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) {
PartitionEntry *entry = (PartitionEntry *)lv_event_get_user_data(e);
if (!entry || !entry->name) {
printf("PartitionEntry or name is NULL\n");
return;
}
/* Compute the selected-boot identifier exactly as we write it later */
char selected_boot[512];
if (entry->name[0] == '/' || entry->vg_path == NULL)
snprintf(selected_boot, sizeof(selected_boot), "%s", entry->name);
else
snprintf(selected_boot, sizeof(selected_boot), "%s/%s", entry->vg_path, entry->name);
/* Read old-boot file and compare */
const char *old_boot_file = "/furios-persist/bootman/old-boot";
FILE *oldf = fopen(old_boot_file, "r");
if (oldf) {
char old_boot_value[512];
if (fgets(old_boot_value, sizeof(old_boot_value), oldf)) {
/* strip newline */
old_boot_value[strcspn(old_boot_value, "\n")] = '\0';
if (strcmp(old_boot_value, selected_boot) == 0) {
/* Same as last boot. do nothing and let boot continue */
fclose(oldf);
indev_cleanup();
exit(0);
}
}
fclose(oldf);
}
/* Otherwise proceed with normal flash-and-reboot */
printf("Preparing to boot partition: %s from VG: %s\n",
entry->name,
(entry->vg_path && *entry->vg_path) ? entry->vg_path : "external");
char error_msg[512];
if (check_and_flash_partition(entry->name, entry->vg_path, error_msg, sizeof(error_msg)) == 0) {
printf("Successfully prepared boot for partition: %s\n", entry->name);
reboot_device();
} else {
show_error_dialog(error_msg);
}
}
static int check_and_flash_partition(const char *partition_name, const char *vg_path, char *error_msg, size_t error_msg_size) {
char device_path[256];
char mount_point[] = "/mnt_tmp";
char version[256];
struct stat st;
char cmd[1024];
/* if this is a direct path then its likely some external device (such as an sdcard) */
if (partition_name[0] == '/' || vg_path == NULL) {
strncpy(device_path, partition_name, sizeof(device_path) - 1);
device_path[sizeof(device_path) - 1] = '\0';
} else {
snprintf(device_path, sizeof(device_path), "%s/%s", vg_path, partition_name);
}
if (stat(device_path, &st) != 0) {
snprintf(error_msg, error_msg_size, "Partition %s does not exist", device_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(device_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();
/* Store full path for partitions with volume group */
char next_boot_value[512];
if (partition_name[0] == '/' || vg_path == NULL) {
snprintf(next_boot_value, sizeof(next_boot_value), "%s", partition_name);
} else {
snprintf(next_boot_value, sizeof(next_boot_value), "%s/%s", vg_path, partition_name);
}
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", next_boot_value);
fclose(next_boot);
sync();
umount(mount_point);
return 0;
}
static bool volume_group_exists(const char *vg_path) {
struct stat st;
return (stat(vg_path, &st) == 0);
}
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) {
indev_cleanup();
sync();
reboot(RB_AUTOBOOT);
}
static void shutdown(void) {
indev_cleanup();
sync();
reboot(RB_POWER_OFF);
}
static void sigaction_handler(int signum) {
LV_UNUSED(signum);
key_thread_running = false;
if (pthread_join(key_thread, NULL) != 0) {
printf("Warning: Failed to join key input thread\n");
}
if (nav_buttons != NULL) {
free(nav_buttons);
nav_buttons = NULL;
}
if (allocated_entries) {
for (size_t i = 0; i < allocated_entries_count; i++) {
if (allocated_entries[i]) {
free(allocated_entries[i]->name);
free(allocated_entries[i]->label);
free(allocated_entries[i]->vg_path);
free(allocated_entries[i]);
}
}
free(allocated_entries);
allocated_entries = NULL;
allocated_entries_count = 0;
}
if (buf != NULL) {
free(buf);
buf = NULL;
}
terminal_reset_current_terminal();
indev_cleanup();
exit(0);
}
static bool is_encrypted(void) {
struct stat st;
return (stat("/dev/mapper/droidian_encrypted", &st) == 0 ||
stat("/dev/mapper/furios_encrypted", &st) == 0);
}
static void free_partition_list(PartitionList *list) {
if (list) {
for (size_t i = 0; i < list->count; i++) {
free(list->entries[i].name);
free(list->entries[i].label);
}
free(list);
}
}
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;
}
/* Check which volume groups exist */
bool furios_vg_exists = volume_group_exists(FURIOS_VG_PATH);
bool droidian_vg_exists = volume_group_exists(DROIDIAN_VG_PATH);
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;
if (strcmp(token, "ubuntu-userdata") == 0) {
printf("Skipping ubuntu-userdata partition\n");
continue;
}
/* If the token is a direct path, use it as-is */
if (token[0] == '/') {
list->entries[list->count].name = strdup(token);
list->entries[list->count].vg_path = NULL; /* Direct path */
} else {
/* Add the partition to both VGs if they exist */
if (furios_vg_exists) {
list->entries[list->count].name = strdup(token);
list->entries[list->count].vg_path = FURIOS_VG_PATH;
/* Get the label */
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++;
if (list->count >= MAX_PARTITIONS)
break;
}
if (droidian_vg_exists) {
char *original_token = token;
list->entries[list->count].name = strdup(original_token ? original_token : str);
list->entries[list->count].vg_path = DROIDIAN_VG_PATH;
/* Reset the strtok_r for this iteration */
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++;
}
/* Continue to the next line */
continue;
}
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 external 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 init_button_navigation(int total_buttons) {
if (nav_buttons != NULL) {
free(nav_buttons);
nav_buttons = NULL;
}
nav_buttons = calloc(total_buttons, sizeof(lv_obj_t *));
if (!nav_buttons) {
printf("Failed to allocate memory for navigation buttons\n");
nav_button_count = 0;
current_button_index = 0;
return;
}
nav_button_count = total_buttons;
current_button_index = 0;
printf("Initialized navigation with %d buttons\n", total_buttons);
}
static void update_button_highlight(void) {
/* Remove highlight from all buttons first */
for (int i = 0; i < nav_button_count; i++) {
lv_obj_clear_state(nav_buttons[i], LV_STATE_FOCUSED);
}
/* Add highlight to current button */
lv_obj_add_state(nav_buttons[current_button_index], LV_STATE_FOCUSED);
printf("Button %d highlighted\n", current_button_index);
}
static int is_input_device(const char *path) {
int fd;
char name[256];
fd = open(path, O_RDONLY);
if (fd < 0)
return 0;
if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) {
close(fd);
return 0;
}
close(fd);
return 1;
}
static int open_restricted(const char *path, int flags, void *user_data) {
(void)user_data;
int fd = open(path, flags);
return fd < 0 ? -errno : fd;
}
static void close_restricted(int fd, void *user_data) {
(void)user_data;
close(fd);
}
static const struct libinput_interface interface = {
.open_restricted = open_restricted,
.close_restricted = close_restricted,
};
static void* key_input_thread(void *arg) {
(void)arg;
struct libinput *li = NULL;
struct libinput_event *event;
int rc;
DIR *dir = NULL;
li = libinput_path_create_context(&interface, NULL);
if (!li) {
fprintf(stderr, "Failed to initialize libinput context\n");
return NULL;
}
struct dirent *entry;
char path[PATH_MAX];
dir = opendir("/dev/input");
if (!dir) {
fprintf(stderr, "Failed to open /dev/input directory\n");
libinput_unref(li);
return NULL;
}
int device_count = 0;
while ((entry = readdir(dir)) != NULL) {
if (strncmp(entry->d_name, "event", 5) == 0) {
snprintf(path, sizeof(path), "/dev/input/%s", entry->d_name);
if (is_input_device(path)) {
struct libinput_device *device;
device = libinput_path_add_device(li, path);
if (!device) {
fprintf(stderr, "Failed to add device: %s\n", path);
} else {
printf("Added input device: %s\n", path);
device_count++;
}
}
}
}
closedir(dir);
dir = NULL;
if (device_count == 0) {
fprintf(stderr, "No input devices were added\n");
libinput_unref(li);
return NULL;
}
printf("Monitoring %d input devices for key events\n", device_count);
libinput_dispatch(li);
while (key_thread_running) {
fd_set fds;
int fd = libinput_get_fd(li);
FD_ZERO(&fds);
FD_SET(fd, &fds);
rc = select(fd + 1, &fds, NULL, NULL, NULL);
if (rc < 0 && errno != EINTR) {
fprintf(stderr, "select() failed: %s\n", strerror(errno));
break;
}
if (rc > 0 && FD_ISSET(fd, &fds)) {
libinput_dispatch(li);
while ((event = libinput_get_event(li)) != NULL) {
if (libinput_event_get_type(event) == LIBINPUT_EVENT_KEYBOARD_KEY) {
struct libinput_event_keyboard *key_event;
enum libinput_key_state state;
uint32_t key;
key_event = libinput_event_get_keyboard_event(event);
key = libinput_event_keyboard_get_key(key_event);
state = libinput_event_keyboard_get_key_state(key_event);
struct libinput_device *device = libinput_event_get_device(event);
const char *device_name = libinput_device_get_name(device);
printf("Key event from '%s': key=%d, state=%d\n",
device_name, key, state);
if (state == LIBINPUT_KEY_STATE_PRESSED) {
switch (key) {
case KEY_VOLUMEUP:
/* Navigate up */
if (current_button_index > 0)
current_button_index--;
else
/* Wrap around to bottom */
current_button_index = nav_button_count - 1;
update_button_highlight();
break;
case KEY_VOLUMEDOWN:
/* Navigate down */
if (current_button_index < nav_button_count - 1)
current_button_index++;
else
/* Wrap around to top */
current_button_index = 0;
update_button_highlight();
break;
case KEY_POWER:
if (current_button_index >= 0 && current_button_index < nav_button_count)
lv_event_send(nav_buttons[current_button_index], LV_EVENT_CLICKED, NULL);
break;
}
}
}
libinput_event_destroy(event);
}
}
}
libinput_unref(li);
return NULL;
}
static void create_partition_buttons(lv_obj_t *label_container, PartitionList *list) {
if (!list)
return;
int base_y_offset = 150;
int button_spacing = 120;
int total_buttons = list->count + 2;
init_button_navigation(total_buttons);
int btn_index = 0;
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);
PartitionEntry *entry = malloc(sizeof(PartitionEntry));
if (entry == NULL) {
printf("failed to allocate memory for PartitionEntry\n");
continue;
}
entry->name = strdup(list->entries[i].name);
entry->label = strdup(list->entries[i].label);
if (list->entries[i].vg_path)
entry->vg_path = strdup(list->entries[i].vg_path);
else
entry->vg_path = NULL;
if (!entry->name || !entry->label ||
(list->entries[i].vg_path && !entry->vg_path)) {
printf("Memory allocation failed for partition entry\n");
free(entry->name);
free(entry->label);
free(entry->vg_path);
free(entry);
continue;
}
allocated_entries = realloc(allocated_entries,
(allocated_entries_count + 1) * sizeof(PartitionEntry *));
if (allocated_entries) {
allocated_entries[allocated_entries_count] = entry;
allocated_entries_count++;
}
lv_obj_add_event_cb(btn, partition_btn_clicked_cb, LV_EVENT_CLICKED, entry);
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);
nav_buttons[btn_index++] = btn;
}
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);
nav_buttons[btn_index++] = reboot_btn;
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);
nav_buttons[btn_index++] = shutdown_btn;
update_button_highlight();
}