This repository has been archived on 2026-05-29. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
LightTAM/src/tui.c
T

445 lines
13 KiB
C

#include <ncurses.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "main.h"
#include "tui.h"
void uartDialog(char *uart_adress, int terminal_x, int terminal_y)
{
char *prompt = "Please enter UART device address for RFID reader (probably /dev/ttyUSB0): ";
char *header = "Enter UART Address";
char input[51] = {"\0"};
int win_height = 8;
int win_width = 80;
int start_y = (terminal_y - win_height) / 2;
int start_x = (terminal_x - win_width) / 2;
start_color();
WINDOW *uartwin = newwin(win_height, win_width, start_y, start_x);
wbkgd(uartwin, COLOR_PAIR(1));
box(uartwin, 0, 0);
wattron(uartwin, COLOR_PAIR(1));
wattron(uartwin, A_BOLD);
mvwprintw(uartwin, 0, (win_width - strlen(header)) / 2, "%s", header);
wattroff(uartwin, A_BOLD);
mvwprintw(uartwin, 2, (win_width - strlen(prompt)) / 2, "%s", prompt);
wattroff(uartwin, COLOR_PAIR(1));
wrefresh(uartwin);
char choice;
int highlight = 0;
int text_x = (win_width - 52) / 2;
int button_x = (win_width - 7) / 2;
keypad(uartwin, true);
while (1)
{
// Input array
if (highlight == 0)
wattron(uartwin, COLOR_PAIR(2));
else
wattron(uartwin, COLOR_PAIR(1));
mvwprintw(uartwin, 4, text_x, "[%50s]", input);
if (!strcmp(input, "\0"))
mvwprintw(uartwin, 4, text_x, "[__________________________________________________]");
if (highlight == 0)
wattroff(uartwin, COLOR_PAIR(2));
else
wattroff(uartwin, COLOR_PAIR(1));
// Enter button
if (highlight == 1)
wattron(uartwin, COLOR_PAIR(2));
else
wattron(uartwin, COLOR_PAIR(1));
wattron(uartwin, A_BOLD);
mvwprintw(uartwin, 6, button_x - 2, " %s ", "<Enter>");
if (highlight == 1)
wattroff(uartwin, COLOR_PAIR(2));
else
wattroff(uartwin, COLOR_PAIR(1));
wattroff(uartwin, A_BOLD);
wrefresh(uartwin);
flushinp();
choice = wgetch(uartwin);
if (choice == '\t')
{
highlight = !highlight;
}
else if (choice == '\n')
{
if (highlight == 0)
{
mvwprintw(uartwin, 4, (win_width - 52) / 2, "[__________________________________________________]");
echo();
wmove(uartwin, 4, ((win_width - 52) / 2) + 1);
curs_set(1);
flushinp();
wgetnstr(uartwin, input, 50);
curs_set(0);
noecho();
}
else if (highlight == 1)
{
if (choice == '\n')
{
wclear(uartwin);
wrefresh(uartwin);
touchwin(stdscr);
refresh();
delwin(uartwin);
refresh();
strcpy(uart_adress, input);
break;
}
}
}
}
}
void mainMenu(list_t *person_list, int terminal_x, int terminal_y)
{
int win_height = 10;
int win_width = 50;
int start_y = (terminal_y - win_height) / 2;
int start_x = (terminal_x - win_width) / 2;
char choices[6][30] = {"List all employees", "Find employee", "Edit database", "Save", "Save as...", "Exit"};
char keboard_input;
int highlight = 0;
start_color();
WINDOW *mainmenu = newwin(win_height, win_width, start_y, start_x);
wbkgd(mainmenu, COLOR_PAIR(1));
box(mainmenu, 0, 0);
wattron(mainmenu, COLOR_PAIR(1));
wattron(mainmenu, A_BOLD);
mvwprintw(mainmenu, 0, (win_width - strlen("Administration")) / 2, "%s", "Administration");
wattroff(mainmenu, A_BOLD);
wattroff(mainmenu, COLOR_PAIR(1));
keypad(mainmenu, true);
int text_x = (win_width - 52) / 2;
wrefresh(mainmenu);
while (1)
{
for (int i = 0; i < 6; i++)
{
if (i == highlight)
wattron(mainmenu, COLOR_PAIR(2));
else
wattron(mainmenu, COLOR_PAIR(1));
wattron(mainmenu, A_BOLD);
mvwprintw(mainmenu, 2 + i, (win_width - strlen(choices[i])) / 2, "%s", choices[i]);
wattroff(mainmenu, A_BOLD);
if (i == highlight)
wattroff(mainmenu, COLOR_PAIR(2));
else
wattroff(mainmenu, COLOR_PAIR(1));
}
wrefresh(mainmenu);
flushinp();
keboard_input = wgetch(mainmenu);
if (keboard_input == '\t')
{
if (highlight < 5)
highlight++;
else
highlight = 0;
}
else if (keboard_input == '\n')
{
switch (highlight)
{
case 0:
personListing(person_list, terminal_x, terminal_y);
break;
case 1:
personSearch(person_list, terminal_x, terminal_y);
case 5:
if (keboard_input == '\n')
{
wclear(mainmenu);
wrefresh(mainmenu);
touchwin(stdscr);
refresh();
delwin(mainmenu);
refresh();
return;
}
break;
}
}
}
}
void personListing(list_t *person_list, int terminal_x, int terminal_y)
{
int win_height = 20;
int win_width = 76;
int start_y = (terminal_y - win_height) / 2;
int start_x = (terminal_x - win_width) / 2;
start_color();
WINDOW *personlisting = newwin(win_height, win_width, start_y, start_x);
wbkgd(personlisting, COLOR_PAIR(1));
box(personlisting, 0, 0);
wattron(personlisting, COLOR_PAIR(1));
wattron(personlisting, A_BOLD);
mvwprintw(personlisting, 0, (win_width - strlen("Filtered list")) / 2, "%s", "Filtered list");
wattroff(personlisting, A_BOLD);
wattroff(personlisting, COLOR_PAIR(1));
keypad(personlisting, TRUE);
int max_rows = win_height - 2;
int total_count = 0;
node_t *current = person_list->head;
while (current)
{
total_count++;
current = current->next;
}
int offset = 0;
int highlight = 0;
int keboard_input;
while (1)
{
werase(personlisting);
box(personlisting, 0, 0);
wattron(personlisting, COLOR_PAIR(1));
wattron(personlisting, A_BOLD);
mvwprintw(personlisting, 0, (win_width - strlen("Filtered list")) / 2, "%s", "Filtered list");
wattroff(personlisting, A_BOLD);
wattroff(personlisting, COLOR_PAIR(1));
wattron(personlisting, A_BOLD);
mvwprintw(personlisting, 1, 2, "UUID Surname Name Available");
wattroff(personlisting, A_BOLD);
current = person_list->head;
int idx = 0, row = 2;
while (current && row < win_height - 1)
{
if (current->user.uuid)
if (idx >= offset && row < win_height - 1)
{
if (idx == highlight)
wattron(personlisting, COLOR_PAIR(2));
mvwprintw(personlisting, row, 2, "%-10hu %-20s %-20s %s",
current->user.uuid,
current->user.surname,
current->user.name,
current->user.available ? "Yes " : "No ");
if (idx == highlight)
wattroff(personlisting, COLOR_PAIR(2));
row++;
}
idx++;
current = current->next;
}
wrefresh(personlisting);
keboard_input = wgetch(personlisting);
if (keboard_input == KEY_UP)
{
if (highlight > 0)
{
highlight--;
if (highlight < offset)
offset--;
}
}
else if (keboard_input == KEY_DOWN)
{
if (highlight < total_count - 1)
{
highlight++;
if (highlight >= offset + max_rows)
offset++;
}
}
else if (keboard_input == '\n')
{
int idx = 0;
node_t *selected = person_list->head;
while (selected && idx < highlight)
{
selected = selected->next;
idx++;
}
}
else if (keboard_input == 27)
{
break;
}
}
delwin(personlisting);
touchwin(stdscr);
refresh();
}
void personSearch(list_t *person_list, int terminal_x, int terminal_y)
{
int win_height = 20;
int win_width = 76;
int start_y = (terminal_y - win_height) / 2;
int start_x = (terminal_x - win_width) / 2;
char buttons[2][10] = {"<Search>", "<Back>"};
char prompts[2][10] = {"Name ", "Surname "};
start_color();
WINDOW *searchdialog = newwin(win_height, win_width, start_y, start_x);
wbkgd(searchdialog, COLOR_PAIR(1));
box(searchdialog, 0, 0);
wattron(searchdialog, COLOR_PAIR(1));
wattron(searchdialog, A_BOLD);
mvwprintw(searchdialog, 0, (win_width - strlen("Search")) / 2, "%s", "Search");
wattroff(searchdialog, A_BOLD);
wattroff(searchdialog, COLOR_PAIR(1));
keypad(searchdialog, TRUE);
char choice;
int highlight = 0;
int field_y = 2;
int field_x = 4;
static char name[32] = "";
static char surname[32] = "";
char *fields[2] = {name, surname};
int field_lens[2] = {31, 31};
uint16_t * uuids_found;
while (1)
{
for (int i = 0; i < 2; i++)
{
if (highlight == i)
wattron(searchdialog, COLOR_PAIR(2));
else
wattron(searchdialog, COLOR_PAIR(1));
mvwprintw(searchdialog, field_y + i * 2, field_x, "%s [%30s]", prompts[i], fields[i]);
if (highlight == i)
wattroff(searchdialog, COLOR_PAIR(2));
else
wattroff(searchdialog, COLOR_PAIR(1));
}
int button_y = field_y + 7;
int button_x = field_x;
for (int i = 0; i < 2; i++)
{
if (highlight == 2 + i)
wattron(searchdialog, COLOR_PAIR(2));
else
wattron(searchdialog, COLOR_PAIR(1));
wattron(searchdialog, A_BOLD);
mvwprintw(searchdialog, button_y, button_x + i * 12, "%s", buttons[i]);
wattroff(searchdialog, A_BOLD);
if (highlight == 2 + i)
wattroff(searchdialog, COLOR_PAIR(2));
else
wattroff(searchdialog, COLOR_PAIR(1));
}
wrefresh(searchdialog);
choice = wgetch(searchdialog);
if (choice == '\t')
{
if (highlight < 3)
{
highlight++;
}
else
highlight = 0;
}
else if (choice == '\n')
{
if (highlight < 2)
{
echo();
curs_set(1);
mvwprintw(searchdialog, field_y + highlight * 2, field_x + strlen(prompts[highlight]) + 3, "%-30s", "");
wmove(searchdialog, field_y + highlight * 2, field_x + strlen(prompts[highlight]) + 4);
wgetnstr(searchdialog, fields[highlight], field_lens[highlight]);
curs_set(0);
noecho();
}
else if (highlight == 2)
{
uint16_t count = searchUUIDByName(person_list, name, surname, uuids_found);
list_t found_pesons;
found_pesons.head = NULL;
node_t **last_ptr = &found_pesons.head;
for (uint16_t i = 0; i < count; i++) {
node_t *cur = person_list->head;
while (cur) {
if (cur->user.uuid == uuids_found[i]) {
node_t *new_node = malloc(sizeof(node_t));
if (new_node) {
new_node->user = cur->user;
new_node->next = NULL;
*last_ptr = new_node;
last_ptr = &new_node->next;
}
break;
}
cur = cur->next;
}
}
personListing(&found_pesons, terminal_x, terminal_y);
}
else if (highlight == 3)
{
delwin(searchdialog);
touchwin(stdscr);
refresh();
break;
}
}
else if (choice == 27)
{ // ESC
delwin(searchdialog);
touchwin(stdscr);
refresh();
break;
}
}
}