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

689 lines
21 KiB
C

#include <ncurses.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "main.h"
#include "file_handler.h"
#include "tui.h"
void initWindow(WINDOW *_win, int window_x, int terminal_x, int terminal_y, int window_colourpair, char *header)
{
start_color();
wbkgd(_win, COLOR_PAIR(window_colourpair));
wattron(_win, COLOR_PAIR(window_colourpair));
box(_win, 0, 0);
wattron(_win, A_BOLD);
mvwprintw(_win, 0, 2, "%s", header);
wattroff(_win, A_BOLD);
wattroff(_win, COLOR_PAIR(window_colourpair));
keypad(_win, true);
wrefresh(_win);
}
void dialogWindow(BYTE _type, char *_message, int terminal_x, int terminal_y)
{
int win_height = 5;
int win_width = 80;
int start_y = (terminal_y - win_height) / 2;
int start_x = (terminal_x - win_width) / 2;
WINDOW *dialog = newwin(win_height, win_width, start_y, start_x);
if (_type == 1)
{
initWindow(dialog, win_height, terminal_x, terminal_y, SUCCESS_WIN_COLOUR, "Success");
wattron(dialog, COLOR_PAIR(SUCCESS_WIN_COLOUR));
mvwprintw(dialog, 2, (win_width - strlen(_message)) / 2, "%s", _message);
wattroff(dialog, COLOR_PAIR(SUCCESS_WIN_COLOUR));
}
else if (_type == 2)
{
initWindow(dialog, win_height, terminal_x, terminal_y, WARNING_WIN_COLOUR, "Warning");
wattron(dialog, COLOR_PAIR(WARNING_WIN_COLOUR));
mvwprintw(dialog, 2, (win_width - strlen(_message)) / 2, "%s", _message);
wattroff(dialog, COLOR_PAIR(WARNING_WIN_COLOUR));
}
else if (_type == 3)
{
initWindow(dialog, win_height, terminal_x, terminal_y, ERROR_WIN_COLOUR, "Failure");
wattron(dialog, COLOR_PAIR(ERROR_WIN_COLOUR));
mvwprintw(dialog, 2, (win_width - strlen(_message)) / 2, "%s", _message);
wattroff(dialog, COLOR_PAIR(ERROR_WIN_COLOUR));
}
flushinp();
wgetch(dialog);
delwin(dialog);
touchwin(stdscr);
refresh();
return;
}
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);
initWindow(uartwin, win_width, terminal_x, terminal_y, STANDARD_WIN_COLOUR, header);
wattron(uartwin, COLOR_PAIR(1));
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;
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')
{
touchwin(stdscr);
delwin(uartwin);
refresh();
strcpy(uart_adress, input);
break;
}
}
}
}
}
void mainMenu(list_t *person_list, char *db_filename, 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", "Export", "Exit"};
char keyboard_input;
int highlight = 0;
start_color();
WINDOW *mainmenu = newwin(win_height, win_width, start_y, start_x);
while (1)
{
initWindow(mainmenu, win_width, terminal_x, terminal_y, STANDARD_WIN_COLOUR, "Administration");
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();
keyboard_input = wgetch(mainmenu);
if (keyboard_input == '\t')
{
if (highlight < 5)
highlight++;
else
highlight = 0;
}
else if (keyboard_input == '\n')
{
switch (highlight)
{
case 0:
personListing(person_list, terminal_x, terminal_y);
break;
case 1:
personSearch(person_list, terminal_x, terminal_y);
break;
case 3:
if (!saveListToCSV(db_filename, person_list))
dialogWindow(3, "Unknown error while saving.", terminal_x, terminal_y);
else
dialogWindow(1, "Database state succesfully saved.", terminal_x, terminal_y);
break;
case 4:
if (!exportDialog(person_list, terminal_x, terminal_y))
dialogWindow(3, "Unknown error while saving.", terminal_x, terminal_y);
else
dialogWindow(1, "Database succesfully exported.", terminal_x, terminal_y);
break;
case 5:
if (keyboard_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;
WINDOW *personlisting = newwin(win_height, win_width, start_y, start_x);
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 keyboard_input;
while (1)
{
initWindow(personlisting, win_width, terminal_x, terminal_y, STANDARD_WIN_COLOUR, "Filtered list");
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)
{
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);
keyboard_input = wgetch(personlisting);
if (keyboard_input == KEY_UP)
{
if (highlight > 0)
{
highlight--;
if (highlight < offset)
offset--;
}
}
else if (keyboard_input == KEY_DOWN)
{
if (highlight < total_count - 1)
{
highlight++;
if (highlight >= offset + max_rows)
offset++;
}
}
else if (keyboard_input == '\n')
{
int idx = 0;
node_t *selected = person_list->head;
while (selected && idx < highlight)
{
selected = selected->next;
idx++;
}
personInfo(person_list, selected, terminal_x, terminal_y);
}
else if (keyboard_input == 27 || keyboard_input == 'q')
{
break;
}
}
delwin(personlisting);
touchwin(stdscr);
refresh();
}
void personSearch(list_t *person_list, int terminal_x, int terminal_y)
{
int win_height = 11;
int win_width = 50;
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 "};
WINDOW *searchdialog = newwin(win_height, win_width, start_y, start_x);
char keyboard_input;
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)
{
box(searchdialog, 0, 0);
initWindow(searchdialog, win_width, terminal_x, terminal_y, STANDARD_WIN_COLOUR, "Search");
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 ", prompts[i]);
mvwprintw(searchdialog, field_y + i * 2, field_x + strlen(prompts[i]) + 3, "[%30s]", fields[i]);
if (strcmp(fields[i], "\0") == 0)
mvwprintw(searchdialog, field_y + i * 2, field_x + strlen(prompts[i]) + 3, "[______________________________]");
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);
keyboard_input = wgetch(searchdialog);
if (keyboard_input == '\t')
{
if (highlight < 3)
{
highlight++;
}
else
highlight = 0;
}
else if (keyboard_input == '\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();
return;
}
}
else if (keyboard_input == 27 || keyboard_input == 'q')
{ // ESC
delwin(searchdialog);
touchwin(stdscr);
refresh();
return;
}
}
}
void personInfo(list_t *_list, node_t *_person, int terminal_x, int terminal_y)
{
int win_height = 14;
int win_width = 60;
int start_y = (terminal_y - win_height) / 2;
int start_x = (terminal_x - win_width) / 2;
start_color();
WINDOW *personinfodialog = newwin(win_height, win_width, start_y, start_x);
int max_rows = win_height - 2;
int total_count = 0;
char buttons[3][20] = {"<Print to file...>", "<Add Time Event>", "<Back>"};
char prompts[6][32] = {"Name", "Surname", "Department", "Last time event", "Total time", "Available"};
char keyboard_input;
int highlight = 0;
int field_y = 2;
int field_x = 4;
int button_y = field_y + 15;
int button_x = field_x;
while (1)
{
initWindow(personinfodialog, win_width, terminal_x, terminal_y, CONTRAST_WIN_COLOUR, "Person Information");
char last_time_event_str[64];
printDateAndTimeInString(_person->user.last_time_event, last_time_event_str);
char total_time_str[64];
printTimeInString(_person->user.total, total_time_str);
wattron(personinfodialog, COLOR_PAIR(6));
mvwprintw(personinfodialog, field_y, field_x, "%s: %s", prompts[0], _person->user.name);
mvwprintw(personinfodialog, field_y + 1, field_x, "%s: %s", prompts[1], _person->user.surname);
mvwprintw(personinfodialog, field_y + 2, field_x, "%s: %d", prompts[2], _person->user.department);
mvwprintw(personinfodialog, field_y + 4, field_x, "%s: %s", prompts[3], last_time_event_str);
mvwprintw(personinfodialog, field_y + 5, field_x, "%s: %s", prompts[4], total_time_str);
mvwprintw(personinfodialog, field_y + 7, field_x, "%s: %s", prompts[5], _person->user.available ? "Yes" : "No ");
wattroff(personinfodialog, COLOR_PAIR(6));
wrefresh(personinfodialog);
for (int i = 0; i < 3; i++)
{
if (highlight == i)
wattron(personinfodialog, COLOR_PAIR(2));
else
wattron(personinfodialog, COLOR_PAIR(6));
wattron(personinfodialog, A_BOLD);
mvwprintw(personinfodialog, field_y + 10, field_x + i * 20, "%s", buttons[i]);
wattroff(personinfodialog, A_BOLD);
if (highlight == i)
wattroff(personinfodialog, COLOR_PAIR(2));
else
wattroff(personinfodialog, COLOR_PAIR(6));
}
wrefresh(personinfodialog);
keyboard_input = wgetch(personinfodialog);
if (keyboard_input == '\t')
{
if (highlight != 2)
highlight++;
else
highlight = 0;
}
else if (keyboard_input == '\n')
{
if (highlight == 0)
{
exportPersonInfo(_person);
}
else if (highlight == 1)
{
if (addTimeEvent(_person, _list))
dialogWindow(1, "Time event succesfully logged.", terminal_x, terminal_y);
else
dialogWindow(3, "Unknown error", terminal_x, terminal_y);
}
else if (highlight == 2)
{
delwin(personinfodialog);
touchwin(stdscr);
refresh();
return;
}
}
else if (keyboard_input == 27 || keyboard_input == 'q')
{
delwin(personinfodialog);
touchwin(stdscr);
refresh();
return;
}
}
}
int exportDialog(list_t *_list, int terminal_x, int terminal_y)
{
int win_height = 8;
int win_width = 80;
int start_y = (terminal_y - win_height) / 2;
int start_x = (terminal_x - win_width) / 2;
WINDOW *exportdialog = newwin(win_height, win_width, start_y, start_x);
initWindow(exportdialog, win_width, terminal_x, terminal_y, STANDARD_WIN_COLOUR, "Copy Database And Save");
int max_rows = win_height - 2;
int total_count = 0;
box(exportdialog, 0, 0);
wattron(exportdialog, COLOR_PAIR(1));
mvwprintw(exportdialog, 2, (win_width - strlen("Enter name of exported database")) / 2, "%s", "Enter name of exported database");
wattroff(exportdialog, COLOR_PAIR(1));
wrefresh(exportdialog);
char choice;
int highlight = 0;
int text_x = (win_width - 52) / 2;
int button_x = (win_width - 7) / 2;
char filename[51];
keypad(exportdialog, true);
while (1)
{
// Input array
if (highlight == 0)
wattron(exportdialog, COLOR_PAIR(2));
else
wattron(exportdialog, COLOR_PAIR(1));
mvwprintw(exportdialog, 4, text_x, "[%50s]", filename);
if (!strcmp(filename, "\0"))
mvwprintw(exportdialog, 4, text_x, "[__________________________________________________]");
if (highlight == 0)
wattroff(exportdialog, COLOR_PAIR(2));
else
wattroff(exportdialog, COLOR_PAIR(1));
// Enter button
if (highlight == 1)
wattron(exportdialog, COLOR_PAIR(2));
else
wattron(exportdialog, COLOR_PAIR(1));
wattron(exportdialog, A_BOLD);
mvwprintw(exportdialog, 6, button_x - 2, " %s ", "<Enter>");
if (highlight == 1)
wattroff(exportdialog, COLOR_PAIR(2));
else
wattroff(exportdialog, COLOR_PAIR(1));
wattroff(exportdialog, A_BOLD);
wrefresh(exportdialog);
flushinp();
choice = wgetch(exportdialog);
if (choice == '\t')
{
highlight = !highlight;
}
else if (choice == '\n')
{
if (highlight == 0)
{
mvwprintw(exportdialog, 4, (win_width - 52) / 2, "[__________________________________________________]");
echo();
wmove(exportdialog, 4, ((win_width - 52) / 2) + 1);
curs_set(1);
flushinp();
wgetnstr(exportdialog, filename, 50);
curs_set(0);
noecho();
}
else if (highlight == 1)
{
if (choice == '\n')
{
BYTE check = saveListToCSV(filename, _list);
wclear(exportdialog);
wrefresh(exportdialog);
touchwin(stdscr);
refresh();
delwin(exportdialog);
refresh();
return check;
}
}
}
}
}
void editDatabaseMenu(list_t *_person_list, int terminal_x, int terminal_y)
{
}