Added formattable clock

This commit is contained in:
SpaghettiBorgar 2022-11-18 20:59:16 +01:00
parent 4f348f7a4a
commit 2cbd825b25
7 changed files with 53 additions and 17 deletions

View File

@ -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 = *

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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
{

View File

@ -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

View File

@ -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;