diff --git a/res/config.ini b/res/config.ini index 45dc7da..e086c96 100644 --- a/res/config.ini +++ b/res/config.ini @@ -6,6 +6,8 @@ # 1 -> CMatrix #animation = 0 +# Whether a big clock should be displayed +#clock = true # The character used to mask the password #asterisk = * diff --git a/src/bigclock.h b/src/bigclock.h new file mode 100644 index 0000000..aed0c95 --- /dev/null +++ b/src/bigclock.h @@ -0,0 +1,144 @@ +#include + +#define CLOCK_W 5 +#define CLOCK_H 5 + +// #define X (char) 219 // block char +// #define _ (char) 032 // space + +#define X 0x2593 +#define _ 0x0000 + +#if CLOCK_W == 5 && CLOCK_H == 5 + +uint32_t CLOCK_0[] = { + X,X,X,X,X, + X,X,_,X,X, + X,X,_,X,X, + X,X,_,X,X, + X,X,X,X,X +}; + +uint32_t CLOCK_1[] = { + _,_,_,X,X, + _,_,_,X,X, + _,_,_,X,X, + _,_,_,X,X, + _,_,_,X,X +}; + +uint32_t CLOCK_2[] = { + X,X,X,X,X, + _,_,_,X,X, + X,X,X,X,X, + X,X,_,_,_, + X,X,X,X,X +}; + +uint32_t CLOCK_3[] = { + X,X,X,X,X, + _,_,_,X,X, + X,X,X,X,X, + _,_,_,X,X, + X,X,X,X,X +}; + +uint32_t CLOCK_4[] = { + X,X,_,X,X, + X,X,_,X,X, + X,X,X,X,X, + _,_,_,X,X, + _,_,_,X,X +}; + +uint32_t CLOCK_5[] = { + X,X,X,X,X, + X,X,_,_,_, + X,X,X,X,X, + _,_,_,X,X, + X,X,X,X,X +}; + +uint32_t CLOCK_6[] = { + X,X,X,X,X, + X,X,_,_,_, + X,X,X,X,X, + X,X,_,X,X, + X,X,X,X,X, +}; + +uint32_t CLOCK_7[] = { + X,X,X,X,X, + _,_,_,X,X, + _,_,_,X,X, + _,_,_,X,X, + _,_,_,X,X +}; + +uint32_t CLOCK_8[] = { + X,X,X,X,X, + X,X,_,X,X, + X,X,X,X,X, + X,X,_,X,X, + X,X,X,X,X +}; + +uint32_t CLOCK_9[] = { + X,X,X,X,X, + X,X,_,X,X, + X,X,X,X,X, + _,_,_,X,X, + X,X,X,X,X +}; + +uint32_t CLOCK_S[] = { + _,_,_,_,_, + _,_,X,_,_, + _,_,_,_,_, + _,_,X,_,_, + _,_,_,_,_ +}; + +uint32_t CLOCK_E[] = { + _,_,_,_,_, + _,_,_,_,_, + _,_,_,_,_, + _,_,_,_,_, + _,_,_,_,_ +}; + +#endif + +#undef X +#undef _ + +static inline uint32_t* CLOCK_N(char c) +{ + switch(c) + { + case '0': + return CLOCK_0; + case '1': + return CLOCK_1; + case '2': + return CLOCK_2; + case '3': + return CLOCK_3; + case '4': + return CLOCK_4; + case '5': + return CLOCK_5; + case '6': + return CLOCK_6; + case '7': + return CLOCK_7; + case '8': + return CLOCK_8; + case '9': + return CLOCK_9; + case ':': + return CLOCK_S; + default: + return CLOCK_E; + } +} diff --git a/src/config.c b/src/config.c index bf7f20e..fbf6851 100644 --- a/src/config.c +++ b/src/config.c @@ -163,6 +163,7 @@ void config_load(const char *cfg_path) {"bg", &config.bg, config_handle_u8}, {"blank_box", &config.blank_box, config_handle_bool}, {"blank_password", &config.blank_password, config_handle_bool}, + {"clock", &config.clock, config_handle_bool}, {"console_dev", &config.console_dev, config_handle_str}, {"default_input", &config.default_input, config_handle_u8}, {"fg", &config.fg, config_handle_u8}, diff --git a/src/config.h b/src/config.h index 67985bb..5a4ed27 100644 --- a/src/config.h +++ b/src/config.h @@ -67,6 +67,7 @@ struct config uint8_t bg; bool blank_box; bool blank_password; + bool clock; char* console_dev; uint8_t default_input; uint8_t fg; diff --git a/src/draw.c b/src/draw.c index 81de187..e2e567a 100644 --- a/src/draw.c +++ b/src/draw.c @@ -5,6 +5,7 @@ #include "utils.h" #include "config.h" #include "draw.h" +#include "bigclock.h" #include #include @@ -14,7 +15,9 @@ #include #include #include +#include #include +#include #if defined(__DragonFly__) || defined(__FreeBSD__) #include @@ -176,6 +179,75 @@ void draw_box(struct term_buf* buf) } } +char* get_clock_string() +{ + time_t timer; + char* buffer = malloc(6); + struct tm* tm_info; + + timer = time(NULL); + tm_info = localtime(&timer); + + strftime(buffer, 6, "%H:%M", tm_info); + + return buffer; +} + +extern inline uint32_t* CLOCK_N(char c); + +struct tb_cell* clock_cell(char c) +{ + struct tb_cell* cells = malloc(sizeof(struct tb_cell) * CLOCK_W * CLOCK_H); + + struct timeval tv; + gettimeofday(&tv, NULL); + if (config.animate && c == ':' && tv.tv_usec / 500000) + c = ' '; + uint32_t* clockchars = CLOCK_N(c); + + for (int i = 0; i < CLOCK_W * CLOCK_H; i++) + { + cells[i].ch = clockchars[i]; + cells[i].fg = config.fg; + cells[i].bg = config.bg; + } + + return cells; +} + +void alpha_blit(struct tb_cell* buf, int x, int y, int w, int h, struct tb_cell* cells) +{ + for (int i = 0; i < h; i++) + { + for (int j = 0; j < w; j++) + { + struct tb_cell cell = cells[i * w + j]; + if (cell.ch) + buf[(y + i) * tb_width() + (x + j)] = cell; + } + } +} + +void draw_clock(struct term_buf* buf) +{ + if (!config.clock) + return; + + int xo = buf->box_x + buf->box_width / 2 - (5 * (CLOCK_W + 1)) / 2; + int yo = buf->box_y - CLOCK_H - 2; + + char* clockstr = get_clock_string(); + struct tb_cell* clockcell; + + for (int i = 0; i < 5; i++) + { + clockcell = clock_cell(clockstr[i]); + alpha_blit(tb_cell_buffer(), xo + i * (CLOCK_W + 1), yo, CLOCK_W, CLOCK_H, clockcell); + free(clockcell); + } +} + + struct tb_cell* strn_cell(char* s, uint16_t len) // throws { struct tb_cell* cells = malloc((sizeof (struct tb_cell)) * len); diff --git a/src/draw.h b/src/draw.h index ce4cf60..28a6477 100644 --- a/src/draw.h +++ b/src/draw.h @@ -86,4 +86,6 @@ void animate_init(struct term_buf* buf); void animate(struct term_buf* buf); bool cascade(struct term_buf* buf, uint8_t* fails); +void draw_clock(struct term_buf *buf); + #endif diff --git a/src/main.c b/src/main.c index 8aea32f..22316c9 100644 --- a/src/main.c +++ b/src/main.c @@ -187,6 +187,7 @@ int main(int argc, char** argv) (*input_handles[active_input])(input_structs[active_input], NULL); tb_clear(); animate(&buf); + draw_clock(&buf); draw_box(&buf); draw_labels(&buf); if(!config.hide_f1_commands)