From a57b0371b1f27db8f4a1892a6109cf44d4d77cdd Mon Sep 17 00:00:00 2001 From: 0xf8 <0xf8.dev@proton.me> Date: Sat, 25 Feb 2023 12:06:54 -0500 Subject: [PATCH] Update 0.4 Signed-off-by: 0xf8 <0xf8.dev@proton.me> --- src/main.c | 379 ++++++++++++++++++++++++++--------------------------- src/main.h | 8 +- 2 files changed, 192 insertions(+), 195 deletions(-) diff --git a/src/main.c b/src/main.c index ee7831e..c470ebd 100644 --- a/src/main.c +++ b/src/main.c @@ -16,93 +16,90 @@ You should have received a copy of the GNU General Public License along with thi #include #include -#define true 1 -#define false 0 - enum {SECS_TO_SLEEP = 0, NSEC_TO_SLEEP = 250000000}; struct notcurses *_nc; void cleanup() { - notcurses_stop(_nc); + notcurses_stop(_nc); } 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 } }, + // 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 } }, + // 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 } }, + // 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 } }, + // 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 } }, + // 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 } }, + // 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 } }, + // 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 } }, + // 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 } } + // 9 + { { 1, 1, 1 }, + { 1, 0, 1 }, + { 1, 1, 1 }, + { 0, 0, 1 }, + { 1, 1, 1 } } }; int rows; int cols; int midr; int midc; -uint stop = false; +uint stop = 0; void *notcurses_thread(void *_) { uint32_t input = 0; @@ -110,7 +107,7 @@ void *notcurses_thread(void *_) { while ((input = notcurses_get(_nc, 0, &ni)) != (uint32_t)-1) { if (ni.evtype == NCTYPE_RELEASE) continue; - if (input == 'q') { stop = true; break; } + if (input == 'q') { stop = 1; break; } if (ni.id == NCKEY_RESIZE) { unsigned dimx, dimy, oldx, oldy; @@ -129,186 +126,186 @@ void *notcurses_thread(void *_) { int main(int argc, char **argv) { - struct timespec remaining, request = {SECS_TO_SLEEP, NSEC_TO_SLEEP}; + struct timespec remaining, request = {SECS_TO_SLEEP, NSEC_TO_SLEEP}; - setlocale(LC_ALL, "C"); + setlocale(LC_ALL, "C"); - struct notcurses* nc = notcurses_init(NULL, stdout); - _nc = nc; - notcurses_enter_alternate_screen(nc); - atexit(cleanup); + struct notcurses* nc = notcurses_init(NULL, stdout); + _nc = nc; + notcurses_enter_alternate_screen(nc); + atexit(cleanup); - rows = LINES - 1; - cols = COLS - 1; + rows = LINES - 1; + cols = COLS - 1; - midr = rows / 2; - midc = cols / 2; + midr = rows / 2; + midc = cols / 2; - cbreak(); - noecho(); + cbreak(); + noecho(); - pthread_t thread_id; - pthread_create(&thread_id, NULL, notcurses_thread, NULL); + pthread_t thread_id; + pthread_create(&thread_id, NULL, notcurses_thread, NULL); - while (!stop) { - 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; + while (!stop) { + 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 min = lc->tm_min; + int minl = min % 10; + int minu = (min - minl) / 10; - int sec = lc->tm_sec; - - ncplane_erase(notcurses_stdplane(nc)); + int sec = lc->tm_sec; + + ncplane_erase(notcurses_stdplane(nc)); - ncplane_set_fg_rgb(notcurses_stdplane(nc), 0x777777); + ncplane_set_fg_rgb(notcurses_stdplane(nc), 0x777777); - ncplane_putchar_yx(notcurses_stdplane(nc), midr - 1, midc, '|'); - ncplane_putchar_yx(notcurses_stdplane(nc), midr + 0, midc, '|'); - ncplane_putchar_yx(notcurses_stdplane(nc), midr + 1, midc, '|'); + ncplane_putchar_yx(notcurses_stdplane(nc), midr - 1, midc, '|'); + ncplane_putchar_yx(notcurses_stdplane(nc), midr + 0, midc, '|'); + ncplane_putchar_yx(notcurses_stdplane(nc), midr + 1, midc, '|'); - struct bignumber_t *bn_houru = bignumber(00, houru); - drawbignumber(bn_houru, sec, midc - 20, midr - 2, notcurses_stdplane(nc)); - free(bn_houru); + struct bignumber_t *bn_houru = bignumber(00, houru); + drawbignumber(bn_houru, sec, midc - 20, midr - 2, notcurses_stdplane(nc)); + free(bn_houru); - struct bignumber_t *bn_hourl = bignumber(15, hourl); - drawbignumber(bn_hourl, sec, midc - 10, midr - 2, notcurses_stdplane(nc)); - free(bn_hourl); + struct bignumber_t *bn_hourl = bignumber(15, hourl); + drawbignumber(bn_hourl, sec, midc - 10, midr - 2, notcurses_stdplane(nc)); + free(bn_hourl); - struct bignumber_t *bn_minu = bignumber(30, minu); - drawbignumber(bn_minu, sec, midc + 3, midr - 2, notcurses_stdplane(nc)); - free(bn_minu); + struct bignumber_t *bn_minu = bignumber(30, minu); + drawbignumber(bn_minu, sec, midc + 3, midr - 2, notcurses_stdplane(nc)); + free(bn_minu); - struct bignumber_t *bn_minl = bignumber(45, minl); - drawbignumber(bn_minl, sec, midc + 13, midr - 2, notcurses_stdplane(nc)); - free(bn_minl); + struct bignumber_t *bn_minl = bignumber(45, minl); + drawbignumber(bn_minl, sec, midc + 13, midr - 2, notcurses_stdplane(nc)); + free(bn_minl); - refresh(); - notcurses_render(nc); + refresh(); + notcurses_render(nc); - free(t); + free(t); - nanosleep(&request, &remaining); - } + nanosleep(&request, &remaining); + } - pthread_kill(thread_id, SIGKILL); + pthread_kill(thread_id, SIGKILL); - return 0; + return 0; } struct bignumber_t *bignumber(int start, uint mimick) { - struct bignumber_t *bn = (struct bignumber_t *)malloc(sizeof(struct bignumber_t) + 1); + 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->mimick = mimick; + uint i = start; + for (int x = 0; x < 3; x++) + for (int y = 0; y < 5; y++) + bn->matrix[y][x] = i, i++; - return bn; + bn->mimick = mimick; + + return bn; } void drawbignumber(struct bignumber_t *bn, int sec, int startx, int starty, struct ncplane *plane) { - for (int x = 0; x < 3; x++) { - for (int y = 0; y < 5; y++) { - 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]; + for (int x = 0; x < 3; x++) { + for (int y = 0; y < 5; y++) { + 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]; - ncplane_set_bg_alpha(plane, NCALPHA_TRANSPARENT); - ncplane_set_fg_alpha(plane, NCALPHA_OPAQUE); + ncplane_set_bg_alpha(plane, NCALPHA_TRANSPARENT); + ncplane_set_fg_alpha(plane, NCALPHA_OPAQUE); - uint32_t bg = ncplane_bg_rgb(plane); - uint32_t fg = ncplane_fg_rgb(plane); - if (partOfMimick) ncplane_set_fg_rgb(plane, 0xCCCCCC), ncplane_set_styles(plane, 0x0002u); - else ncplane_set_fg_rgb(plane, 0x555555); + uint32_t bg = ncplane_bg_rgb(plane); + uint32_t fg = ncplane_fg_rgb(plane); + if (partOfMimick) ncplane_set_fg_rgb(plane, 0xCCCCCC), ncplane_set_styles(plane, 0x0002u); + else ncplane_set_fg_rgb(plane, 0x555555); - if (currentSec) ncplane_set_bg_rgb(plane, 0xFFFFFF), ncplane_set_fg_rgb(plane, 0x333333), ncplane_set_bg_alpha(plane, NCALPHA_OPAQUE); + if (currentSec) ncplane_set_bg_rgb(plane, 0xFFFFFF), ncplane_set_fg_rgb(plane, 0x333333), ncplane_set_bg_alpha(plane, NCALPHA_OPAQUE); - ncplane_putstr_yx(plane, starty + y, startx + (x * 3), padded_num->s); + ncplane_putstr_yx(plane, starty + y, startx + (x * 3), padded_num->s); - ncplane_set_styles(plane, 0x0000); - ncplane_set_bg_rgb(plane, bg); - ncplane_set_fg_rgb(plane, fg); - ncplane_set_bg_alpha(plane, NCALPHA_TRANSPARENT); - ncplane_set_fg_alpha(plane, NCALPHA_OPAQUE); + ncplane_set_styles(plane, 0x0000); + ncplane_set_bg_rgb(plane, bg); + ncplane_set_fg_rgb(plane, fg); + ncplane_set_bg_alpha(plane, NCALPHA_TRANSPARENT); + ncplane_set_fg_alpha(plane, NCALPHA_OPAQUE); - free_string(padded_num); - } + 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; + 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); + memset(str->s, 0, maxsize); - // Figure out how many place values are in n - int _n = abs(n); - uint places = 1; + // 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; + 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'; + 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; + uint required_padding = pad - places; + if (required_padding > pad) required_padding = 0; - // Negative value - if (n < 0) { - required_padding++; - 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; } + } - 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; + 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); + 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; + return s; } // free_string void free_string(struct string *s) { - free(s->s); - free(s); + free(s->s); + free(s); } diff --git a/src/main.h b/src/main.h index 958a0ca..35fd5d1 100644 --- a/src/main.h +++ b/src/main.h @@ -31,13 +31,13 @@ typedef unsigned long size; typedef unsigned int uint; struct string { - char *s; - size length; + char *s; + size length; }; struct bignumber_t { - uint matrix[5][3]; - uint mimick; + uint matrix[5][3]; + uint mimick; };