From db95c6f0d0871ac971ada033feb99d01f501a96e Mon Sep 17 00:00:00 2001 From: 0xf8 <0xf8.dev@proton.me> Date: Sat, 25 Feb 2023 12:06:52 -0500 Subject: [PATCH] Initial commit Signed-off-by: 0xf8 <0xf8.dev@proton.me> --- .gitignore | 2 + Makefile | 16 ++++ src/main.c | 264 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.h | 41 +++++++++ 4 files changed, 323 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 src/main.c create mode 100644 src/main.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..21765f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +main.o +tempus \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..501545b --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +OBJ = $(patsubst src/%.c,%.o,$(wildcard src/*.c)) +HEADERS = $(wildcard src/*.h) +CC = clang +FLAGS = -g -std=c17 -fsanitize=address +LIB = -lncurses + +%.o: src/%.c $(HEADERS) + $(CC) $(FLAGS) -c $< -o $@ + +tempus: $(OBJ) + $(CC) $(LIB) $(FLAGS) $^ -o $@ + + +.PHONY: clean +clean: + rm $(OBJ) \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..2e846f0 --- /dev/null +++ b/src/main.c @@ -0,0 +1,264 @@ +#include "./main.h" +#include +#include + +#define true 1 +#define false 0 + +enum {SECS_TO_SLEEP = 0, NSEC_TO_SLEEP = 40000000}; + +void cleanup() { + endwin(); +} + +uint mimicks[10][5][3] = { + // 0 + { { 1, 1, 1 }, + { 1, 0, 1 }, + { 1, 0, 1 }, + { 1, 0, 1 }, + { 1, 1, 1 } }, + + // 1 + { { 1, 1, 0 }, + { 0, 1, 0 }, + { 0, 1, 0 }, + { 0, 1, 0 }, + { 1, 1, 1 } }, + + // 2 + { { 1, 1, 1 }, + { 0, 0, 1 }, + { 1, 1, 1 }, + { 1, 0, 0 }, + { 1, 1, 1 } }, + + // 3 + { { 1, 1, 1 }, + { 0, 0, 1 }, + { 1, 1, 1 }, + { 0, 0, 1 }, + { 1, 1, 1 } }, + + // 4 + { { 1, 0, 1 }, + { 1, 0, 1 }, + { 1, 1, 1 }, + { 0, 0, 1 }, + { 0, 0, 1 } }, + + // 5 + { { 1, 1, 1 }, + { 1, 0, 0 }, + { 1, 1, 1 }, + { 0, 0, 1 }, + { 1, 1, 1 } }, + + // 6 + { { 1, 1, 1 }, + { 1, 0, 0 }, + { 1, 1, 1 }, + { 1, 0, 1 }, + { 1, 1, 1 } }, + + // 7 + { { 1, 1, 1 }, + { 0, 0, 1 }, + { 0, 1, 1 }, + { 0, 1, 0 }, + { 0, 1, 0 } }, + + // 8 + { { 1, 1, 1 }, + { 1, 0, 1 }, + { 1, 1, 1 }, + { 1, 0, 1 }, + { 1, 1, 1 } }, + + // 9 + { { 1, 1, 1 }, + { 1, 0, 1 }, + { 1, 1, 1 }, + { 0, 0, 1 }, + { 1, 1, 1 } } +}; + +int main(int argc, char **argv) { + // tests + struct timespec remaining, request = {SECS_TO_SLEEP, NSEC_TO_SLEEP}; + + WINDOW *w = initscr(); + int rows = LINES - 1; + int cols = COLS - 1; + int midr = rows/2; + int midc = cols/2; + atexit(cleanup); + + start_color(); + init_pair(1,8,0); + init_pair(2,7,0); + + cbreak(); + noecho(); + + clear(); + + // TODO: add framelimiting + while (true) { + time_t *t = (time_t *)malloc(sizeof(time_t) + 1); + time(t); + + struct tm *lc = localtime(t); + + // hourul:minul:sec + int hour = lc->tm_hour; + int hourl = hour % 10; + int houru = (hour - hourl) / 10; + + int min = lc->tm_min; + int minl = min % 10; + int minu = (min - minl) / 10; + + int sec = lc->tm_sec; + + // print_number + // #hour + int sy = 0, sx = 0; + + struct bignumber_t *bn_houru = bignumber(00, 0, houru); + struct bignumber_t *bn_hourl = bignumber(15, 1, hourl); + struct bignumber_t *bn_minu = bignumber(30, 2, minu); + struct bignumber_t *bn_minl = bignumber(45, 3, minl); + + clear(); + + attron(A_DIM); + mvaddch(midr + 0, midc, '|'); + mvaddch(midr + 1, midc, '|'); + mvaddch(midr - 1, midc, '|'); + attroff(A_DIM); + + drawbignumber(bn_houru, sec, midc - 19, midr - 2); + drawbignumber(bn_hourl, sec, midc - 9, midr - 2); + drawbignumber(bn_minu, sec, midc + 2, midr - 2); + drawbignumber(bn_minl, sec, midc + 12, midr - 2); + free(bn_houru); + free(bn_hourl); + free(bn_minu); + free(bn_minl); + + refresh(); + + free(t); + + nanosleep(&request, &remaining); + } + + return 0; +} + +struct bignumber_t *bignumber(int start, uint index, uint mimick) { + struct bignumber_t *bn = (struct bignumber_t *)malloc(sizeof(struct bignumber_t) + 1); + + uint i = start; + for (int x = 0; x < 3; x++) + for (int y = 0; y < 5; y++) + bn->matrix[y][x] = i, i++; + + bn->index = index; + bn->mimick = mimick; + + return bn; +} + +void drawbignumber(struct bignumber_t *bn, int sec, int startx, int starty) { + for (int x = 0; x < 3; x++) { + for (int y = 0; y < 5; y++) { + move(starty + y, startx + (x * 3)); + + struct string *padded_num = pad_int(bn->matrix[y][x], 2, 2); + uint currentSec = (uint)sec == bn->matrix[y][x]; + uint partOfMimick = mimicks[bn->mimick][y][x]; + + if (partOfMimick) attron(A_BOLD); + else attron(COLOR_PAIR(1)); + + if (currentSec) attron(A_REVERSE),attron(A_BOLD),attron(COLOR_PAIR(2)); + + addstr(padded_num->s); + + attroff(A_BOLD); + attroff(A_REVERSE); + attroff(COLOR_PAIR(1)); + attroff(COLOR_PAIR(2)); + + free_string(padded_num); + } + } +} + + +// pad_uint +struct string *pad_int(int n, uint pad, uint maxsize) { + struct string *str = (struct string *)malloc(sizeof (struct string) + 1); + maxsize++; + + str->s = (char *)malloc((sizeof(char) * maxsize) + 1); + str->length = 0; + + memset(str->s, 0, maxsize); + + // Figure out how many place values are in n + int _n = abs(n); + uint places = 1; + + for (uint i = 0; i < pad; i++) { + if (_n >= 10) { + _n /= 10; + places++; + } + else break; + } + + char *padding = (char *)malloc((sizeof(char) * pad) + 2); + for (uint i = 0; i < pad + 1; i++) + padding[i] = '0'; + + uint required_padding = pad - places; + if (required_padding > pad) required_padding = 0; + + // Negative value + if (n < 0) { + required_padding++; + padding[0] = '-'; + } + + snprintf(str->s, maxsize, "%*.*s%i%c", 0, required_padding, padding, abs(n), 0); + free(padding); + + for (uint i = 0; i < maxsize + 1; i++) { + if (str->s[i] == 0) { + str->length = i; + break; + } + } + + return str; +} + +// alloc_string +struct string *alloc_string(uint l) { + struct string *s = (struct string *)malloc(sizeof(struct string) + 1); + s->s = (char *)malloc(l + 1); + s->length = l; + + memset(s->s, 0, l); + + return s; +} + +// free_string +void free_string(struct string *s) { + free(s->s); + free(s); +} diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..276a11f --- /dev/null +++ b/src/main.h @@ -0,0 +1,41 @@ +#ifndef __MAIN_H__ +#define __MAIN_H__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef unsigned long size; +typedef unsigned int uint; + +struct string { + char *s; + size length; +}; + +struct bignumber_t { + uint matrix[5][3]; + uint mimick; + uint index; +}; + + +struct string *alloc_string(uint l); +void free_string(struct string *s); + +// pads a number with 0's +struct string *pad_int(int n, uint pad, uint maxsize); + +// this creates a bignumber_t +struct bignumber_t *bignumber(int start, uint index, uint mimick); + +// this draws a big number +void drawbignumber(struct bignumber_t *, int sec, int startx, int starty); + +#endif // __MAIN_H__