From 2cbd825b258ac5a577b5d115cea2ee6891e20587 Mon Sep 17 00:00:00 2001 From: SpaghettiBorgar Date: Fri, 18 Nov 2022 20:59:16 +0100 Subject: [PATCH] Added formattable clock --- res/config.ini | 7 +++++-- src/bigclock.h | 12 +++++++----- src/config.c | 3 +++ src/config.h | 1 + src/draw.c | 22 ++++++++++++++++++---- src/draw.h | 1 + src/main.c | 24 ++++++++++++++++++------ 7 files changed, 53 insertions(+), 17 deletions(-) diff --git a/res/config.ini b/res/config.ini index e086c96..e454e10 100644 --- a/res/config.ini +++ b/res/config.ini @@ -6,8 +6,11 @@ # 1 -> CMatrix #animation = 0 -# Whether a big clock should be displayed -#clock = true +# format string for clock in top right corner (see strftime specification) +#clock = %c + +# enable/disable big clock +#bigclock = true # The character used to mask the password #asterisk = * diff --git a/src/bigclock.h b/src/bigclock.h index aed0c95..2de834a 100644 --- a/src/bigclock.h +++ b/src/bigclock.h @@ -3,11 +3,13 @@ #define CLOCK_W 5 #define CLOCK_H 5 -// #define X (char) 219 // block char -// #define _ (char) 032 // space - -#define X 0x2593 -#define _ 0x0000 +#if defined(__linux__) || defined(__FreeBSD__) + #define X 0x2593 + #define _ 0x0000 +#else + #define X '#' + #define _ 0 +#endif #if CLOCK_W == 5 && CLOCK_H == 5 diff --git a/src/config.c b/src/config.c index 3756b94..1704d9d 100644 --- a/src/config.c +++ b/src/config.c @@ -164,6 +164,7 @@ void config_load(const char *cfg_path) {"bigclock", &config.bigclock, config_handle_bool}, {"blank_box", &config.blank_box, config_handle_bool}, {"blank_password", &config.blank_password, config_handle_bool}, + {"clock", &config.clock, config_handle_str}, {"console_dev", &config.console_dev, config_handle_str}, {"default_input", &config.default_input, config_handle_u8}, {"fg", &config.fg, config_handle_u8}, @@ -273,6 +274,7 @@ void config_defaults() config.bigclock = false; config.blank_box = true; config.blank_password = false; + config.clock = NULL; config.console_dev = strdup("/dev/console"); config.default_input = LOGIN_INPUT; config.fg = 9; @@ -356,6 +358,7 @@ void lang_free() void config_free() { + free(config.clock); free(config.console_dev); free(config.lang); free(config.mcookie_cmd); diff --git a/src/config.h b/src/config.h index c3182b7..914cf05 100644 --- a/src/config.h +++ b/src/config.h @@ -68,6 +68,7 @@ struct config bool bigclock; bool blank_box; bool blank_password; + char* clock; char* console_dev; uint8_t default_input; uint8_t fg; diff --git a/src/draw.c b/src/draw.c index 025cb57..5870a48 100644 --- a/src/draw.c +++ b/src/draw.c @@ -179,16 +179,17 @@ void draw_box(struct term_buf* buf) } } -char* bigclock_str() +char* time_str(char* fmt, int maxlen) { time_t timer; - char* buffer = malloc(6); + char* buffer = malloc(maxlen); struct tm* tm_info; timer = time(NULL); tm_info = localtime(&timer); - strftime(buffer, 6, "%H:%M", tm_info); + if (strftime(buffer, maxlen, fmt, tm_info) == 0) + buffer[0] = '\0'; return buffer; } @@ -239,7 +240,7 @@ void draw_bigclock(struct term_buf* buf) int xo = buf->box_x + buf->box_width / 2 - (5 * (CLOCK_W + 1)) / 2; int yo = buf->box_y - CLOCK_H - 2; - char* clockstr = bigclock_str(); + char* clockstr = time_str("%H:%M", 6); struct tb_cell* clockcell; for (int i = 0; i < 5; i++) @@ -252,6 +253,19 @@ void draw_bigclock(struct term_buf* buf) free(clockstr); } +void draw_clock(struct term_buf* buf) +{ + if (config.clock == NULL || strlen(config.clock) == 0) + return; + + char* clockstr = time_str(config.clock, 32); + int clockstrlen = strlen(clockstr); + + struct tb_cell* cells = strn_cell(clockstr, clockstrlen); + tb_blit(buf->width - clockstrlen, 0, clockstrlen, 1, cells); + + free(clockstr); +} struct tb_cell* strn_cell(char* s, uint16_t len) // throws { diff --git a/src/draw.h b/src/draw.h index 2d5ef55..0e6d164 100644 --- a/src/draw.h +++ b/src/draw.h @@ -87,5 +87,6 @@ void animate(struct term_buf* buf); bool cascade(struct term_buf* buf, uint8_t* fails); void draw_bigclock(struct term_buf *buf); +void draw_clock(struct term_buf *buf); #endif diff --git a/src/main.c b/src/main.c index 921d37f..8178ab7 100644 --- a/src/main.c +++ b/src/main.c @@ -190,6 +190,7 @@ int main(int argc, char** argv) animate(&buf); draw_bigclock(&buf); draw_box(&buf); + draw_clock(&buf); draw_labels(&buf); if(!config.hide_f1_commands) draw_f_commands(); @@ -209,16 +210,27 @@ int main(int argc, char** argv) tb_present(); } - if (config.animate) { - error = tb_peek_event(&event, config.min_refresh_delta); - } else if (config.bigclock) { + int timeout = -1; + + if (config.animate) + { + timeout = config.min_refresh_delta; + } + else + { struct timeval tv; gettimeofday(&tv, NULL); - error = tb_peek_event(&event, (60 - tv.tv_sec % 60) * 1000 - tv.tv_usec / 1000); - } else { - error = tb_poll_event(&event); + if (config.bigclock) + timeout = (60 - tv.tv_sec % 60) * 1000 - tv.tv_usec / 1000 + 1; + if (config.clock) + timeout = 1000 - tv.tv_usec / 1000 + 1; } + if (timeout == -1) + error = tb_poll_event(&event); + else + error = tb_peek_event(&event, timeout); + if (error < 0) { continue;