Skip to content

Commit b8a95d4

Browse files
author
tangxinfa
committed
fix: redraw too late after system wakeup.
1 parent be2a08a commit b8a95d4

1 file changed

Lines changed: 21 additions & 12 deletions

File tree

unlock_indicator.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ static xcb_visualtype_t *vistype;
101101
unlock_state_t unlock_state;
102102
auth_state_t auth_state;
103103

104+
/* Displayed time text */
105+
static char timetext[100] = {'\0'};
106+
107+
/* Get current time text. */
108+
void get_current_timetext(char *str, uint32_t size) {
109+
time_t curtime = time(NULL);
110+
struct tm *tm = localtime(&curtime);
111+
if (use24hour)
112+
strftime(str, size, TIME_FORMAT_24, tm);
113+
else
114+
strftime(str, size, TIME_FORMAT_12, tm);
115+
}
116+
104117
/*
105118
* Draws global image with fill color onto a pixmap with the given
106119
* resolution and returns it.
@@ -234,14 +247,7 @@ xcb_pixmap_t draw_image(uint32_t *resolution) {
234247
cairo_stroke(ctx);
235248

236249
/* Display (centered) Time */
237-
char *timetext = malloc(6);
238-
239-
time_t curtime = time(NULL);
240-
struct tm *tm = localtime(&curtime);
241-
if (use24hour)
242-
strftime(timetext, 100, TIME_FORMAT_24, tm);
243-
else
244-
strftime(timetext, 100, TIME_FORMAT_12, tm);
250+
get_current_timetext(timetext, sizeof(timetext));
245251

246252
/* Text */
247253
set_auth_color('l');
@@ -259,8 +265,6 @@ xcb_pixmap_t draw_image(uint32_t *resolution) {
259265
cairo_show_text(ctx, timetext);
260266
cairo_close_path(ctx);
261267

262-
free(timetext);
263-
264268
if (auth_state == STATE_AUTH_WRONG && (modifier_string != NULL)) {
265269
cairo_text_extents_t extents;
266270
double x, y;
@@ -371,19 +375,24 @@ void clear_indicator(void) {
371375
/* Periodic redraw for clock updates - taken from github.com/ravinrabbid/i3lock-clock */
372376

373377
static void time_redraw_cb(struct ev_loop *loop, ev_periodic *w, int revents) {
378+
char current[sizeof(timetext)] = {'\0'};
379+
get_current_timetext(current, sizeof(current));
380+
if (strcmp(current, timetext) == 0) {
381+
return;
382+
}
374383
redraw_screen();
375384
}
376385

377386
void start_time_redraw_tick(struct ev_loop* main_loop) {
378387
if (time_redraw_tick) {
379-
ev_periodic_set(time_redraw_tick, 1.0, 60., 0);
388+
ev_periodic_set(time_redraw_tick, 1.0, 1., 0);
380389
ev_periodic_again(main_loop, time_redraw_tick);
381390
} else {
382391
/* When there is no memory, we just don’t have a timeout. We cannot
383392
* exit() here, since that would effectively unlock the screen. */
384393
if (!(time_redraw_tick = calloc(sizeof(struct ev_periodic), 1)))
385394
return;
386-
ev_periodic_init(time_redraw_tick,time_redraw_cb, 1.0, 60., 0);
395+
ev_periodic_init(time_redraw_tick,time_redraw_cb, 1.0, 1., 0);
387396
ev_periodic_start(main_loop, time_redraw_tick);
388397
}
389398
}

0 commit comments

Comments
 (0)