-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdisplay.c
1319 lines (1132 loc) · 36.5 KB
/
display.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
#include <ctype.h>
#include <ncurses.h>
#include <errno.h>
#include <systemd/sd-event.h>
#include <signal.h>
#include "sm_err.h"
#include "service.h"
#include "display.h"
#include "lib/toml.h"
#include <sys/ioctl.h>
#include "config.h"
#define MAX(a, b) ((a) > (b) ? (a) : (b))
extern char *colors[];
static uint64_t start_time = 0;
static enum service_type mode = SERVICE;
static enum bus_type type = SYSTEM;
static int index_start = 0;
static int position = 0;
static uid_t euid = INT32_MAX;
static sd_event *event = NULL;
static sd_event_source *event_source = NULL;
int colorscheme = 0;
int D_XLOAD = 84;
int D_XACTIVE = 94;
int D_XSUB = 104;
int D_XDESCRIPTION = 114;
/**
* Calculates column widths for display based on terminal width.
*
* Dynamically adjusts column positions for unit names, active state,
* sub-state, and description columns. Ensures columns fit within
* terminal width while maintaining minimum unit name width.
*
* @param terminal_width Total width of the terminal screen
*/
void calculate_columns(int terminal_width)
{
const int MIN_UNIT_WIDTH = 20; // Minimum width for unit names
const int STATE_WIDTH = 10; // Fixed width for state columns
// Unit column gets 50% of the width, but at least MIN_UNIT_WIDTH
D_XLOAD = MAX(terminal_width * 0.50, MIN_UNIT_WIDTH);
// Each state column gets fixed width
D_XACTIVE = D_XLOAD + STATE_WIDTH;
D_XSUB = D_XACTIVE + STATE_WIDTH;
D_XDESCRIPTION = D_XSUB + STATE_WIDTH;
// Ensure we don't exceed terminal width
if (D_XDESCRIPTION >= terminal_width - 1)
{
// Adjust columns to fit within terminal
int excess = D_XDESCRIPTION - (terminal_width - 1);
D_XLOAD = MAX(MIN_UNIT_WIDTH, D_XLOAD - excess);
D_XACTIVE = D_XLOAD + STATE_WIDTH;
D_XSUB = D_XACTIVE + STATE_WIDTH;
D_XDESCRIPTION = D_XSUB + STATE_WIDTH;
}
}
static void init_color_pairs(void)
{
// Initialize the basic color pairs used throughout the application
init_pair(BLACK_WHITE, COLOR_BLACK, COLOR_WHITE);
init_pair(CYAN_BLACK, COLOR_CYAN, COLOR_BLACK);
init_pair(WHITE_BLACK, COLOR_WHITE, COLOR_BLACK);
init_pair(RED_BLACK, COLOR_RED, COLOR_BLACK);
init_pair(GREEN_BLACK, COLOR_GREEN, COLOR_BLACK);
init_pair(YELLOW_BLACK, COLOR_YELLOW, COLOR_BLACK);
init_pair(BLUE_BLACK, COLOR_BLUE, COLOR_BLACK);
init_pair(MAGENTA_BLACK, COLOR_MAGENTA, COLOR_BLACK);
init_pair(WHITE_BLUE, COLOR_WHITE, COLOR_BLUE);
init_pair(WHITE_RED, COLOR_WHITE, COLOR_RED);
init_pair(BLACK_GREEN, COLOR_BLACK, COLOR_GREEN);
init_pair(RED_YELLOW, COLOR_RED, COLOR_YELLOW);
}
void set_color_scheme(int scheme)
{
colorscheme = scheme;
init_color_pairs();
}
// Convert RGB value to ncurses color value
static short rgb_to_ncurses(short value)
{
return (short)((value * 1000) / 255);
}
static void apply_color_scheme(const ColorScheme *scheme)
{
if (!can_change_color())
{
endwin();
printf("Your terminal does not support custom colors.\n");
exit(EXIT_FAILURE);
}
// Apply the custom color scheme
init_color(COLOR_BLACK,
rgb_to_ncurses(scheme->black[0]),
rgb_to_ncurses(scheme->black[1]),
rgb_to_ncurses(scheme->black[2]));
init_color(COLOR_RED,
rgb_to_ncurses(scheme->red[0]),
rgb_to_ncurses(scheme->red[1]),
rgb_to_ncurses(scheme->red[2]));
init_color(COLOR_GREEN,
rgb_to_ncurses(scheme->green[0]),
rgb_to_ncurses(scheme->green[1]),
rgb_to_ncurses(scheme->green[2]));
init_color(COLOR_YELLOW,
rgb_to_ncurses(scheme->yellow[0]),
rgb_to_ncurses(scheme->yellow[1]),
rgb_to_ncurses(scheme->yellow[2]));
init_color(COLOR_BLUE,
rgb_to_ncurses(scheme->blue[0]),
rgb_to_ncurses(scheme->blue[1]),
rgb_to_ncurses(scheme->blue[2]));
init_color(COLOR_MAGENTA,
rgb_to_ncurses(scheme->magenta[0]),
rgb_to_ncurses(scheme->magenta[1]),
rgb_to_ncurses(scheme->magenta[2]));
init_color(COLOR_CYAN,
rgb_to_ncurses(scheme->cyan[0]),
rgb_to_ncurses(scheme->cyan[1]),
rgb_to_ncurses(scheme->cyan[2]));
init_color(COLOR_WHITE,
rgb_to_ncurses(scheme->white[0]),
rgb_to_ncurses(scheme->white[1]),
rgb_to_ncurses(scheme->white[2]));
}
/**
* Handles the display of a service row with all its details.
*
* @param svc The service to display
* @param row The row number (relative position)
* @param spc The spacing/offset from the top
*/
static void display_service_row(Service *svc, int row, int spc)
{
int i;
char short_unit[D_XLOAD - 4];
char short_unit_file_state[10];
char *short_description;
size_t maxx_description = getmaxx(stdscr) - D_XDESCRIPTION - 1;
// Clear the unit name column
for (i = 1; i < D_XLOAD - 1; i++)
mvaddch(row + spc, i, ' ');
// If the unit name is too long, truncate it and add ...
if (strlen(svc->unit) >= (size_t)(D_XLOAD - 4))
{
strncpy(short_unit, svc->unit, D_XLOAD - 4);
mvaddstr(row + spc, 1, short_unit);
mvaddstr(row + spc, D_XLOAD - 4, "...");
}
else
mvaddstr(row + spc, 1, svc->unit);
// Clear the state column
for (i = D_XLOAD; i < D_XACTIVE - 1; i++)
mvaddch(row + spc, i, ' ');
// If the state is too long, truncate it (enabled-runtime will be enabled-r)
if (!svc->unit_file_state || strlen(svc->unit_file_state) == 0)
mvprintw(row + spc, D_XLOAD, "%s", svc->load);
else if (strlen(svc->unit_file_state) > 9)
{
strncpy(short_unit_file_state, svc->unit_file_state, 9);
short_unit_file_state[9] = '\0';
mvaddstr(row + spc, D_XLOAD, short_unit_file_state);
}
else
mvprintw(row + spc, D_XLOAD, "%s", svc->unit_file_state ? svc->unit_file_state : svc->load);
// Clear the active column
for (i = D_XACTIVE; i < D_XSUB - 1; i++)
mvaddch(row + spc, i, ' ');
mvprintw(row + spc, D_XACTIVE, "%s", svc->active);
// Clear the sub column
for (i = D_XSUB; i < D_XDESCRIPTION - 1; i++)
mvaddch(row + spc, i, ' ');
mvprintw(row + spc, D_XSUB, "%s", svc->sub);
// Clear the description column
for (i = D_XDESCRIPTION; i < getmaxx(stdscr) - 1; i++)
mvaddch(row + spc, i, ' ');
// If the description is too long, truncate it and add ...
if (strlen(svc->description) >= maxx_description)
{
short_description = alloca(maxx_description + 1);
memset(short_description, 0, maxx_description + 1);
strncpy(short_description, svc->description, maxx_description - 3);
mvaddstr(row + spc, D_XDESCRIPTION, short_description);
mvaddstr(row + spc, D_XDESCRIPTION + maxx_description - 3, "...");
}
else
mvaddstr(row + spc, D_XDESCRIPTION, svc->description);
// Save the y-position of the service
svc->ypos = row + spc;
}
/**
* Displays the list of services on the screen.
*
* This function is responsible for rendering the list of services,
* handling pagination, and applying highlighting to the selected service.
* It also manages the display of services based on the current mode and
* available screen space.
*
* @param bus Pointer to the Bus structure containing service information.
*/
static void display_services(Bus *bus)
{
int max_rows, maxy;
int row = 0;
int idx = index_start;
Service *svc;
int headerrow = 3;
struct winsize size;
int visible_services = 0;
int total_services = 0;
int dummy_maxx;
getmaxyx(stdscr, maxy, dummy_maxx);
(void)dummy_maxx;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
if (size.ws_col < (strlen(D_FUNCTIONS) + strlen(D_SERVICE_TYPES) + 2))
{
headerrow = 4;
}
int spc = headerrow + 2;
max_rows = maxy - spc - 1;
// Count the total number of services of the current type
for (int i = 0;; i++)
{
svc = service_nth(bus, i);
if (!svc)
break;
if (mode == ALL || mode == svc->type)
total_services++;
}
services_invalidate_ypos(bus);
while (true)
{
svc = service_nth(bus, idx);
if (!svc)
break;
if (row >= max_rows)
break;
if (mode != ALL && mode != svc->type)
{
idx++;
continue;
}
if (row == position)
{
// Monochrome theme needs a different color pair
!strcmp(color_schemes[colorscheme].name, "Monochrome") ? attron(COLOR_PAIR(WHITE_RED)) : attron(COLOR_PAIR(WHITE_BLUE));
attron(A_BOLD);
}
display_service_row(svc, row, spc);
if (row == position)
{
// Monochrome theme needs a different color pair
!strcmp(color_schemes[colorscheme].name, "Monochrome") ? attroff(COLOR_PAIR(WHITE_RED)) : attroff(COLOR_PAIR(WHITE_BLUE));
attroff(A_BOLD);
}
row++;
idx++;
visible_services++;
}
}
/**
* Prints the text and lines for the main user interface.
* This function is responsible for rendering the header, function keys, and mode indicators
* on the screen. It also updates the position and mode information based on the current state.
*/
static void display_text_and_lines(Bus *bus)
{
int x = D_XLOAD / 2 - 10;
int maxx, maxy;
char tmptype[16] = {0};
int headerrow = 3;
char navigation[256]; // Buffer for the complete navigation text
struct winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
if (size.ws_col < (strlen(D_FUNCTIONS) + strlen(D_SERVICE_TYPES) + 2))
{
headerrow++;
}
getmaxyx(stdscr, maxy, maxx);
// Solarized light theme needs a different color pair
!strcmp(color_schemes[colorscheme].name, "Solarized Light") ? attron(COLOR_PAIR(MAGENTA_BLACK)) : attron(COLOR_PAIR(BLACK_WHITE));
border(0, 0, 0, 0, 0, 0, 0, 0);
// Create the navigation text with the current theme name
snprintf(navigation, sizeof(navigation), D_NAVIGATION_BASE, color_schemes[colorscheme].name);
attron(A_BOLD);
mvaddstr(1, 1, D_HEADLINE);
mvaddstr(1, strlen(D_HEADLINE) + 1 + ((size.ws_col - strlen(D_HEADLINE) - strlen(D_QUIT) - strlen(navigation) - 2) / 2), navigation);
mvaddstr(1, size.ws_col - strlen(D_QUIT) - 1, D_QUIT);
attron(COLOR_PAIR(WHITE_RED));
mvaddstr(2, 1, D_FUNCTIONS);
attroff(COLOR_PAIR(WHITE_RED));
attron(COLOR_PAIR(BLACK_GREEN));
if (size.ws_col < (strlen(D_FUNCTIONS) + strlen(D_SERVICE_TYPES) + 2))
{
mvaddstr(3, 1, D_SERVICE_TYPES);
}
else
{
mvaddstr(2, size.ws_col - strlen(D_SERVICE_TYPES) - 1, D_SERVICE_TYPES);
}
attroff(COLOR_PAIR(BLACK_GREEN));
attroff(A_BOLD);
// Solarized light theme needs a different color pair
!strcmp(color_schemes[colorscheme].name, "Solarized Light") ? attron(COLOR_PAIR(MAGENTA_BLACK)) : attron(COLOR_PAIR(BLACK_WHITE));
mvprintw(headerrow, D_XLOAD - 10, "Pos.:%3d", position + index_start);
mvprintw(headerrow, 1, "UNIT:");
attron(COLOR_PAIR(GREEN_BLACK));
mvprintw(headerrow, 7, "(%s)", type ? "USER" : "SYSTEM");
attroff(COLOR_PAIR(GREEN_BLACK));
// Solarized light theme needs a different color pair
!strcmp(color_schemes[colorscheme].name, "Solarized Light") ? attron(COLOR_PAIR(MAGENTA_BLACK)) : attron(COLOR_PAIR(BLACK_WHITE));
mvprintw(headerrow, D_XLOAD, "STATE:");
mvprintw(headerrow, D_XACTIVE, "ACTIVE:");
mvprintw(headerrow, D_XSUB, "SUB:");
mvprintw(headerrow, D_XDESCRIPTION, "DESCRIPTION:");
attron(COLOR_PAIR(GREEN_BLACK));
attron(A_UNDERLINE);
// Sets the type count
strncpy(tmptype, service_string_type(mode), 16);
tmptype[sizeof(tmptype) - 1] = '\0';
tmptype[0] = toupper(tmptype[0]);
mvprintw(headerrow, x, "%s: %d", tmptype, bus->total_types[mode]);
attroff(COLOR_PAIR(GREEN_BLACK));
attroff(A_UNDERLINE);
attroff(A_BOLD);
mvhline(headerrow + 1, 1, ACS_HLINE, maxx - 2);
mvvline(headerrow, D_XLOAD - 1, ACS_VLINE, maxy - 3);
mvvline(headerrow, D_XACTIVE - 1, ACS_VLINE, maxy - 3);
mvvline(headerrow, D_XSUB - 1, ACS_VLINE, maxy - 3);
mvvline(headerrow, D_XDESCRIPTION - 1, ACS_VLINE, maxy - 3);
}
/**
* Handles user input and performs various operations on systemd services.
* This function is responsible for:
* - Handling user input from the keyboard, including navigation, service operations, and mode changes
* - Updating the display based on the current state and user actions
* - Calling appropriate functions to perform service operations (start, stop, restart, etc.)
* - Reloading the service list when necessary
*
* @param s The event source that triggered the callback.
* @param fd The file descriptor associated with the event source.
* @param revents The events that occurred on the file descriptor.
* @param data Arbitrary user data passed to the callback.
* @return 0 to indicate the event was handled successfully.
*/
int display_key_pressed(sd_event_source *s, int fd, uint32_t revents, void *data)
{
(void)fd;
int c;
char *status = NULL;
int max_services = 0;
int maxy;
int maxx;
// Mouse reset
printf("\033[?1003l");
mousemask(0, NULL);
getmaxyx(stdscr, maxy, maxx);
int headerrow = 3;
struct winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
if (size.ws_col < (strlen(D_FUNCTIONS) + strlen(D_SERVICE_TYPES) + 2))
{
headerrow = 4;
}
int spc = headerrow + 2; // +2 for the separator line and a space
int max_visible_rows = maxy - spc - 1; // Exact calculation of visible rows
int page_scroll = max_visible_rows; // For Page Up/Down
bool update_state = false;
Service *svc = NULL;
Bus *bus = (Bus *)data;
if ((revents & (EPOLLHUP | EPOLLERR | EPOLLRDHUP)) > 0)
return 0;
c = getch();
// Count the total number of services of the current type
for (int i = 0;; i++)
{
svc = service_nth(bus, i);
if (!svc)
break;
if (mode == ALL || mode == svc->type)
max_services++;
}
svc = service_nth(bus, position + index_start);
set_escdelay(25);
switch (c)
{
case 'f': // Search function
{
Service *found_service = NULL;
int max_input_length = 50; // Internal buffer (can be longer than the window)
char search_query[51] = {0}; // Space for 50 characters + null termination
int win_height = 3, win_width = 80;
int starty = (maxy - win_height) / 2;
int startx = (maxx - win_width) / 2;
int cursor = 0; // Current position in search_query
int ch;
static bool search_in_progress = false;
if (search_in_progress)
break;
search_in_progress = true;
WINDOW *input_win = newwin(win_height, win_width, starty, startx);
box(input_win, 0, 0);
// Information line in window, show maximum length:
mvwprintw(input_win, 1, 1, "Search unit: ");
wrefresh(input_win);
echo(); // Show characters directly (optional, since we render ourselves)
curs_set(1);
// Switch the key input mode to non-blocking input field
keypad(input_win, TRUE);
// We only show the part of the input that fits in the window.
// Assume the visible area starts after displaying the static characters.
int offset = 1 + strlen("Search unit: ");
// How many characters fit in the visible area?
int visible_length = win_width - offset - 2; // 2 for border
// Process character-by-character input
while ((ch = wgetch(input_win)) != KEY_RETURN)
{
if ((ch == KEY_BACKSPACE || ch == 127) && cursor > 0)
{
cursor--;
search_query[cursor] = '\0';
}
else if (cursor < max_input_length && ch >= 32 && ch <= 126)
{
search_query[cursor++] = ch;
search_query[cursor] = '\0';
}
// Rendering: We calculate the start index so that if the input is longer than visible_length,
// only the last visible_length characters are displayed.
int start_index = 0;
if (cursor > visible_length)
start_index = cursor - visible_length;
// Clear the input area
for (int i = offset; i < win_width - 1; i++)
{
mvwaddch(input_win, 1, i, ' ');
}
// Display the visible part of the input
mvwprintw(input_win, 1, offset, "%s", &search_query[start_index]);
wmove(input_win, 1, offset + ((cursor > visible_length) ? visible_length : cursor));
wrefresh(input_win);
}
noecho();
curs_set(0);
// Clean up input window
delwin(input_win);
refresh();
flushinp();
// Clear any remaining input
while (getch() != ERR)
;
// Exit search if query is empty
if (strlen(search_query) == 0)
{
search_in_progress = false;
break;
}
// Store current mode and switch to ALL to search across all services
enum service_type current_mode = mode;
mode = ALL;
// Search for service matching query
for (int i = 0;; i++)
{
Service *svc_iter = service_nth(bus, i);
if (!svc_iter)
break;
if (strcasestr(svc_iter->unit, search_query) != NULL)
{
found_service = svc_iter;
break;
}
}
if (found_service)
{
// Switch to found service's type
mode = found_service->type;
int filtered_pos = 0;
// Calculate position of found service in filtered list
for (int i = 0;; i++)
{
Service *svc = service_nth(bus, i);
if (!svc)
break;
if (svc->type == mode)
{
if (svc == found_service)
{
// Adjust scroll position to show found service
if (filtered_pos >= max_visible_rows)
{
index_start = filtered_pos - max_visible_rows + 1;
position = max_visible_rows - 1;
}
else
{
index_start = 0;
position = filtered_pos;
}
break;
}
filtered_pos++;
}
}
// Redraw display with found service
clear();
display_services(bus);
display_text_and_lines(bus);
}
else
{
// Restore previous mode if no service found
mode = current_mode;
display_status_window("No matching service found.", "Search");
clear();
display_services(bus);
display_text_and_lines(bus);
}
refresh();
// Reset search state and update start time
search_in_progress = false;
start_time = service_now();
return 0;
}
case KEY_ESC:
nodelay(stdscr, TRUE); // Non-blocking mode
int esc_timeout = 50; // 50ms Timeout for Escape sequences
wtimeout(stdscr, esc_timeout);
// Buffer to store escape sequence characters
char seq[10] = {0};
int i = 0, c;
// Read escape sequence characters until ERR or '~' is encountered
while ((c = getch()) != ERR && i < 9)
{
seq[i++] = c;
if (c == '~')
break;
}
seq[i] = '\0';
// Handle different escape sequences for function keys, Some terminals send escape sequences for function keys
if (strcmp(seq, "[11~") == 0)
{
d_op(bus, svc, START, "Start");
}
else if (strcmp(seq, "[12~") == 0)
{
d_op(bus, svc, STOP, "Stop");
}
else if (strcmp(seq, "[13~") == 0)
{
d_op(bus, svc, RESTART, "Restart");
}
else if (strcmp(seq, "[14~") == 0)
{
d_op(bus, svc, ENABLE, "Enable");
update_state = true;
}
else
{
// Exit if ESC was pressed and enough time has passed since start
if ((service_now() - start_time) < D_ESCOFF_MS)
break;
endwin();
exit(EXIT_SUCCESS);
}
nodelay(stdscr, FALSE); // back to normal mode
break;
case KEY_F(1):
d_op(bus, svc, START, "Start");
break;
case KEY_F(2):
d_op(bus, svc, STOP, "Stop");
break;
case KEY_F(3):
d_op(bus, svc, RESTART, "Restart");
break;
case KEY_F(4):
d_op(bus, svc, ENABLE, "Enable");
update_state = true;
break;
case KEY_F(5):
d_op(bus, svc, DISABLE, "Disable");
update_state = true;
break;
case KEY_F(6):
d_op(bus, svc, MASK, "Mask");
update_state = true;
break;
case KEY_F(7):
d_op(bus, svc, UNMASK, "Unmask");
update_state = true;
break;
case KEY_F(8):
d_op(bus, svc, RELOAD, "Reload");
break;
case KEY_UP:
if (position > 0)
{
// If position > 0, just move the cursor up
position--;
}
else if (index_start > 0)
{
// If we're at the top edge and there are more entries above, scroll up
index_start--;
}
break;
case KEY_DOWN:
if (position + index_start >= max_services - 1)
{
// Already at the last entry, prevent further scrolling
break;
}
if (position < max_visible_rows - 1 && position + index_start < max_services - 1)
{
// If we're not at the bottom edge and there are more entries, move the cursor
position++;
}
else if (position + index_start < max_services - 1)
{
// If we're at the bottom edge and there are more entries, scroll down
index_start++;
}
break;
case KEY_PPAGE: // Page Up
if (index_start > 0)
{
// Scroll one page up
index_start -= page_scroll;
if (index_start < 0)
index_start = 0;
erase();
}
position = 0;
break;
case KEY_NPAGE: // Page Down
if (index_start + max_visible_rows < max_services)
{
// Scroll one page down
index_start += page_scroll;
if (index_start + max_visible_rows > max_services)
index_start = max_services - max_visible_rows;
if (index_start < 0)
index_start = 0;
erase();
}
position = 0;
break;
case KEY_LEFT:
if (mode > ALL)
D_MODE(mode - 1);
break;
case KEY_RIGHT:
if (mode < SNAPSHOT)
D_MODE(mode + 1);
break;
case KEY_SPACE:
if (bus_system_only())
{
display_status_window("Only system bus is available as root.", "sudo mode !");
break;
}
type ^= 0x1;
bus = bus_currently_displayed();
sd_event_source_set_userdata(s, bus);
erase();
break;
case KEY_RETURN:
svc = service_nth(bus, position + index_start);
if (!svc)
break;
status = service_status_info(bus, svc);
display_status_window(status ? status : "No status information available.", "Status:");
free(status);
break;
case 'a':
D_MODE(ALL);
break;
case 'd':
D_MODE(DEVICE);
break;
case 'i':
D_MODE(SLICE);
break;
case 's':
D_MODE(SERVICE);
break;
case 'o':
D_MODE(SOCKET);
break;
case 't':
D_MODE(TARGET);
break;
case 'r':
D_MODE(TIMER);
break;
case 'm':
D_MODE(MOUNT);
break;
case 'c':
D_MODE(SCOPE);
break;
case 'n':
D_MODE(AUTOMOUNT);
break;
case 'w':
D_MODE(SWAP);
break;
case 'p':
D_MODE(PATH);
break;
case 'h':
D_MODE(SNAPSHOT);
break;
case 'q':
endwin();
exit(EXIT_SUCCESS);
break;
case '+':
if (colorscheme < scheme_count - 1)
{
colorscheme++;
apply_color_scheme(&color_schemes[colorscheme]);
set_color_scheme(colorscheme);
erase();
}
break;
case '-':
if (colorscheme > 0)
{
colorscheme--;
apply_color_scheme(&color_schemes[colorscheme]);
set_color_scheme(colorscheme);
erase();
}
break;
default:
break;
}
if (update_state)
{
if (svc != NULL)
{ // NULL pointer protection
bus_update_unit_file_state(bus, svc);
display_redraw_row(svc);
svc->changed = 0;
}
update_state = false; // reset after processing
}
// Make sure we are not going over the end of the list
if (index_start + position >= max_services)
{
if (max_services > 0)
{
if (max_services > max_visible_rows)
{
index_start = max_services - max_visible_rows;
position = max_visible_rows - 1;
}
else
{
index_start = 0;
position = max_services - 1;
}
}
else
{
index_start = 0;
position = 0;
}
}
// Full redraw of the screen
erase();
display_redraw(bus);
refresh();
return 0;
}
/**
* Returns the current bus type.
*
* This function provides access to the static 'type' variable,
* which represents the current bus type (SYSTEM or USER).
*
* @return The current bus type.
*/
enum bus_type display_bus_type(void)
{
return type;
}
/**
* Returns the current service type mode.
*
* This function provides access to the static 'mode' variable,
* which represents the current service type filter (ALL, DEVICE, SERVICE, etc.).
*
* @return The current service type mode.
*/
enum service_type display_mode(void)
{
return mode;
}
/**
* Redraws the entire display with the current bus information.
*
* This function performs a complete redraw of the screen by:
* 1. Displaying all services from the provided bus
* 2. Clearing any remaining space below the services list
* 3. Drawing the text headers and separator lines
* 4. Refreshing the screen to show the changes
*
* @param bus Pointer to the Bus structure containing services to display
*/
void display_redraw(Bus *bus)
{
struct winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
// Create the headline text with root marking
char headline[100];
snprintf(headline, sizeof(headline), "%s%s%s",
D_HEADLINE,
(geteuid() == 0) ? " " : "",
(geteuid() == 0) ? "(root)" : "");
mvaddstr(1, 1, headline);
if (geteuid() == 0)
{
// Position directly after the already written text
int root_pos = 1 + strlen(D_HEADLINE) + 1;
move(1, root_pos);
attron(COLOR_PAIR(RED_BLACK) | A_BOLD); // Red and bold
printw("(root)");
attroff(COLOR_PAIR(RED_BLACK) | A_BOLD);
}
display_services(bus);
clrtobot();
display_text_and_lines(bus);
refresh();
}
/**
* Refreshes the display row for the given service.
*
* If the service is currently displayed on the screen, this function will
* clear the row for the service and redraw it to ensure the display is
* up-to-date.
*
* @param svc The service to refresh the display row for.
*/
void display_redraw_row(Service *svc)
{
// If the service is on the screen, invalidate the row so it refreshes
// correctly
int x, y;
if (svc->ypos < 0)
return;
getyx(stdscr, y, x);
wmove(stdscr, svc->ypos, D_XLOAD);
wclrtoeol(stdscr);
wmove(stdscr, y, x);
}
void display_erase(void)
{
erase();
}
/**
* Sets the current bus type for the display.
*
* This function updates the static 'type' variable that determines
* whether system or user services are being displayed.
*
* @param ty The bus type to set (SYSTEM or USER)
*/