TUI pretty much works

This commit is contained in:
Lexi Quinn 2021-08-28 01:43:33 +10:00
parent 5d65153dae
commit fb42d58fd1
4 changed files with 212 additions and 60 deletions

View File

@ -1,5 +1,5 @@
TARGET = qtimer TARGET = qtimer
LIBS = -lm -lncurses -luiohook LIBS = -lm -luiohook
CC = gcc CC = gcc
CFLAGS = -g -Wall CFLAGS = -g -Wall

70
display.c Normal file
View File

@ -0,0 +1,70 @@
#include "display.h"
void setBGColor(struct color c)
{
printf("\033[48;2;%d;%d;%dm", c.r, c.g, c.b);
}
void setFGColor(struct color c)
{
printf("\033[38;2;%d;%d;%dm", c.r, c.g, c.b);
}
void clrScreen()
{
printf("\033[2J\n");
}
void disableCursor()
{
printf("\033[?25l\n");
}
void enableCursor()
{
printf("\033[?25h\n");
}
void altBuffer()
{
printf("\033[?1049h\n");
}
void stdBuffer()
{
printf("\033[?1049l\n");
}
void initScreen(struct color bg, struct color fg)
{
altBuffer();
setBGColor(bg);
setFGColor(fg);
disableCursor();
clrScreen();
}
void resetScreen()
{
clrScreen();
enableCursor();
stdBuffer();
}
void cntrPrint(int row, int col, int maxlen, char *text)
{
printf("\033[%d;%ldH%.*s", row, col - (strlen(text) / 2), maxlen, text);
}
void leftPrint(int row, int maxlen, char *text)
{
printf("\033[%d;1H%.*s", row, maxlen, text);
}
void rghtPrint(int row, int maxlen, char* text)
{
if (strlen(text) < maxlen)
printf("\033[%d;1H%*s", row, maxlen, text);
else
printf("\033[%d;1H%.*s", row, maxlen, text);
}

21
display.h Normal file
View File

@ -0,0 +1,21 @@
#include <stdio.h>
#include <string.h>
struct color {
int r;
int g;
int b;
};
void setBGColor(struct color);
void setFGColor(struct color);
void clrScreen();
void disableCursor();
void enableCursor();
void altBuffer();
void stdBuffer();
void initScreen(struct color, struct color);
void resetScreen();
void cntrPrint(int row, int col, int maxlen, char *text);
void leftPrint(int row, int maxlen, char *text);
void rghtPrint(int row, int maxlem, char *text);

179
timer.c
View File

@ -1,7 +1,7 @@
#include "display.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <ncurses.h>
#include <unistd.h> #include <unistd.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdarg.h> #include <stdarg.h>
@ -11,12 +11,15 @@
#include <wchar.h> #include <wchar.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/ioctl.h>
#include <signal.h>
#define NS_PER_S 1000000000 #define NS_PER_S 1000000000
#define K_START 1 #define K_START 1
#define K_STOP 2 #define K_STOP 2
#define K_PAUSE 3 #define K_PAUSE 3
#define K_SPLIT 4 #define K_SPLIT 4
#define K_CLOSE 5
struct keymap struct keymap
{ {
@ -24,22 +27,32 @@ struct keymap
uint16_t STOP; uint16_t STOP;
uint16_t PAUSE; uint16_t PAUSE;
uint16_t SPLIT; uint16_t SPLIT;
uint16_t CLOSE;
};
struct segment
{
char *name;
int *startMS;
int *endMS;
}; };
char buf; char buf;
int pipefd[2]; int pipefd[2];
struct timespec start, finish, delta; struct timespec timestart, finish;
char *gameTitle, *catagoryTitle; char *gameTitle, *catagoryTitle;
char *splitPath; char *splitPath;
struct keymap km; struct keymap km;
int h, w;
bool timerActive; bool timerActive;
void loadKeymap(); void loadKeymap();
void startTimer(); void start();
void stopTimer(); void stop();
void splitTimer(); void split();
void handleInput(); void tpause();
int handleInput();
void sub_timespec(struct timespec t1, struct timespec t2, struct timespec* td) void sub_timespec(struct timespec t1, struct timespec t2, struct timespec* td)
{ {
@ -71,91 +84,135 @@ bool logger_proc(unsigned int level, const char *format, ...) {
void dispatch_proc(uiohook_event * const event) { void dispatch_proc(uiohook_event * const event) {
switch (event->type) { switch (event->type) {
case EVENT_KEY_PRESSED: case EVENT_KEY_PRESSED:
if (event->data.keyboard.keycode == VC_R) if (event->data.keyboard.keycode == km.START)
buf = K_START; buf = K_START;
if (event->data.keyboard.keycode == VC_F) if (event->data.keyboard.keycode == km.STOP)
buf = K_STOP; buf = K_STOP;
if (event->data.keyboard.keycode == VC_D) if (event->data.keyboard.keycode == km.PAUSE)
buf = K_PAUSE; buf = K_PAUSE;
if (event->data.keyboard.keycode == VC_E) if (event->data.keyboard.keycode == km.SPLIT)
buf = K_SPLIT; buf = K_SPLIT;
if (event->data.keyboard.keycode == km.CLOSE)
buf = K_CLOSE;
write(pipefd[1], &buf, 1); write(pipefd[1], &buf, 1);
default: default:
break; break;
} }
} }
void handleInput() int handleInput()
{ {
if (read(pipefd[0], &buf, 1) == -1) if (read(pipefd[0], &buf, 1) == -1)
return; return 0;
switch(buf) { if (buf == K_SPLIT)
case K_SPLIT: split();
splitTimer(); if (buf == K_START)
break; start();
case K_START: if (buf == K_STOP)
startTimer(); stop();
break; if (buf == K_PAUSE)
case K_STOP: tpause();
stopTimer(); if (buf == K_CLOSE)
break; return 1;
case K_PAUSE: return 0;
break;
}
} }
void ncDisplay() void start()
{
erase();
int minutes = (int)delta.tv_sec / 60;
int hours = minutes / 60;
printw("%2d:%02d:%02d.%ld\n", hours, minutes, (int)delta.tv_sec%60, delta.tv_nsec/1000000);
refresh();
}
void startTimer()
{ {
if (timerActive) if (timerActive)
return; return;
clock_gettime(CLOCK_REALTIME, &start); clock_gettime(CLOCK_REALTIME, &timestart);
timerActive = true; timerActive = true;
} }
void stopTimer() void stop()
{ {
if (!timerActive) if (!timerActive)
return; return;
timerActive = false; timerActive = false;
} }
void splitTimer() void split()
{
}
void tpause()
{ {
} }
void loadKeymap() void loadKeymap()
{ {
char path[256];
strcat(strcpy(path, getenv("HOME")), "/.config/qtimer");
mkdir(path, 0777);
strcat(strcpy(path, getenv("HOME")), "/.config/qtimer/keymaps");
mkdir(path, 0777);
strcat(strcpy(path, getenv("HOME")), "/.config/qtimer/keymaps/default");
FILE* fp = fopen(path, "r");
if (fp == NULL) {
fp = fopen(path, "w");
fprintf(fp, "some text");
fclose(fp);
}
km.START = VC_R; km.START = VC_R;
km.STOP = VC_F; km.STOP = VC_F;
km.PAUSE = VC_D; km.PAUSE = VC_D;
km.SPLIT = VC_E; km.SPLIT = VC_E;
km.CLOSE = VC_C;
//char path[256];
//strcat(strcpy(path, getenv("HOME")), "/.config/qtimer");
//mkdir(path, 0777);
//strcat(strcpy(path, getenv("HOME")), "/.config/qtimer/keymaps");
//mkdir(path, 0777);
//strcat(strcpy(path, getenv("HOME")), "/.config/qtimer/keymaps/default");
//FILE* fp = fopen(path, "r");
//if (fp == NULL) {
//km.START = VC_R;
//km.STOP = VC_F;
//km.PAUSE = VC_D;
//km.SPLIT = VC_E;
//fp = fopen(path, "w");
//fprintf(fp, "START = R\n");
//fprintf(fp, "STOP = F\n");
//fprintf(fp, "PAUSE = D\n");
//fprintf(fp, "SPLIT = E\n");
//fclose(fp);
//} else {
//}
//fclose(fp);
} }
int main(int argc, char* argv[]) void drawHLine(int row)
{
for (int i = 0; i <= w; i++)
printf("\033[%d;%dH─", row, i);
}
void printTime(int row, struct timespec t1, struct timespec t2)
{
struct timespec delta;
sub_timespec(timestart, finish, &delta);
int minutes = delta.tv_sec / 60;
int hours = minutes / 60;
printf("\033[%d;1H%01d:%02d:%02ld.%02ld", row, hours, minutes, delta.tv_sec, delta.tv_nsec / 10000000);
}
void drawDisplay()
{
clrScreen();
cntrPrint(1, w / 2, w, "Game Name");
cntrPrint(2, w / 2, w, "Catagory Name");
char cols[41];
sprintf(cols, "%10s%10s%10s%10s", "Delta", "Sgmt", "Time", "PB");
rghtPrint(4, w, cols);
drawHLine(5);
printTime(6, timestart, finish);
fflush(stdout);
}
void resize(int i)
{
struct winsize ws;
ioctl(1, TIOCGWINSZ, &ws);
w = ws.ws_col;
h = ws.ws_row;
}
int main(int argc, char **argv)
{ {
timerActive = false; timerActive = false;
hook_set_logger_proc(&logger_proc); hook_set_logger_proc(&logger_proc);
@ -174,16 +231,20 @@ int main(int argc, char* argv[])
hook_run(); hook_run();
} else { } else {
close(pipefd[1]); close(pipefd[1]);
initscr(); signal(SIGWINCH, resize);
while(1) { resize(0);
handleInput(); struct color bg = { 47, 53, 66};
struct color fg = {247, 248, 242};
initScreen(bg, fg);
while(!handleInput()) {
drawDisplay();
if (timerActive) { if (timerActive) {
clock_gettime(CLOCK_REALTIME, &finish); clock_gettime(CLOCK_REALTIME, &finish);
sub_timespec(start, finish, &delta);
} }
ncDisplay(); usleep(3000);
} }
endwin(); resetScreen();
kill(cpid, SIGTERM);
} }
return 0; return 0;
} }