diff options
Diffstat (limited to 'extras.c')
| -rw-r--r-- | extras.c | 102 |
1 files changed, 102 insertions, 0 deletions
| @@ -132,6 +132,61 @@ static void i3lock_draw_led(cairo_t *cr, | |||
| 132 | } | 132 | } |
| 133 | 133 | ||
| 134 | 134 | ||
| 135 | static char * i3lock_replace_str(char *to, | ||
| 136 | const char *from, | ||
| 137 | const char *old_substr, | ||
| 138 | const char *new_substr, | ||
| 139 | size_t max_length) { | ||
| 140 | |||
| 141 | int offset; /* the offset in the tmp_str after each match */ | ||
| 142 | |||
| 143 | /* the offset in from needed to pick up the right side after a match */ | ||
| 144 | int from_offset; | ||
| 145 | |||
| 146 | int from_len = strlen(from); | ||
| 147 | int new_substr_len = strlen(new_substr); | ||
| 148 | int old_substr_len = strlen(old_substr); | ||
| 149 | int len_diff = old_substr_len - new_substr_len; | ||
| 150 | size_t free_space = max_length - 1; | ||
| 151 | char tmp_str[max_length]; | ||
| 152 | char *tmp_str_ptr; | ||
| 153 | |||
| 154 | /* reserve the last byte for '\0' in case of clipping */ | ||
| 155 | strncpy(tmp_str, from, free_space); | ||
| 156 | |||
| 157 | for (int i = 0; ; ++i) { | ||
| 158 | tmp_str_ptr = strstr(tmp_str, old_substr); | ||
| 159 | if (tmp_str_ptr == NULL) { | ||
| 160 | /* no more instances of old_substr */ | ||
| 161 | break; | ||
| 162 | } | ||
| 163 | |||
| 164 | offset = tmp_str_ptr - tmp_str; | ||
| 165 | free_space -= offset; | ||
| 166 | if (free_space <= new_substr_len) { | ||
| 167 | strncpy(tmp_str_ptr, new_substr, free_space); | ||
| 168 | break; | ||
| 169 | } | ||
| 170 | strncpy(tmp_str_ptr, new_substr, new_substr_len); | ||
| 171 | free_space -= new_substr_len; | ||
| 172 | /* tmp_str offset -> from_offset mapping */ | ||
| 173 | from_offset = offset + old_substr_len + i * len_diff; | ||
| 174 | if (free_space <= from_len - from_offset) { | ||
| 175 | strncpy(tmp_str_ptr + new_substr_len, from + from_offset, free_space); | ||
| 176 | break; | ||
| 177 | } | ||
| 178 | strncpy(tmp_str_ptr + new_substr_len, from + from_offset, from_len - from_offset + 1); | ||
| 179 | |||
| 180 | } | ||
| 181 | |||
| 182 | strncpy(to, tmp_str, max_length - 1); | ||
| 183 | *(to + (max_length - 1)) = '\0'; | ||
| 184 | |||
| 185 | return to; | ||
| 186 | |||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 135 | void i3lock_draw_digital_clock(cairo_t *cr, | 190 | void i3lock_draw_digital_clock(cairo_t *cr, |
| 136 | const i3lock_digital_clock_t *dc, | 191 | const i3lock_digital_clock_t *dc, |
| 137 | const uint32_t *resolution, | 192 | const uint32_t *resolution, |
| @@ -440,9 +495,56 @@ void i3lock_draw_led_clock(cairo_t *cr, | |||
| 440 | brokentime->tm_sec & 1, | 495 | brokentime->tm_sec & 1, |
| 441 | lc); | 496 | lc); |
| 442 | } | 497 | } |
| 498 | |||
| 443 | return; | 499 | return; |
| 500 | |||
| 444 | } | 501 | } |
| 445 | 502 | ||
| 446 | } | 503 | } |
| 447 | 504 | ||
| 448 | 505 | ||
| 506 | char * i3lock_format_elapsed_time(char *to, | ||
| 507 | const char *template, | ||
| 508 | int max_size, | ||
| 509 | int seconds) { | ||
| 510 | |||
| 511 | char tmp_str[max_size]; | ||
| 512 | char int_str[32]; /* more than enough for int */ | ||
| 513 | int days = seconds / 86400; | ||
| 514 | int hours = (seconds - days * 86400) / 3600; | ||
| 515 | int minutes = (seconds - days * 86400 - hours * 3600) / 60; | ||
| 516 | int rest_seconds = seconds - days * 86400 - hours * 3600 - minutes * 60; | ||
| 517 | |||
| 518 | strncpy(tmp_str, template, max_size); | ||
| 519 | if (strlen(template) >= max_size) { | ||
| 520 | /* do a proper clipping */ | ||
| 521 | *(tmp_str + (max_size - 1)) = '\0'; | ||
| 522 | } | ||
| 523 | |||
| 524 | /* days */ | ||
| 525 | snprintf(int_str, 32, "%d", days); | ||
| 526 | i3lock_replace_str(to, tmp_str, "%d", int_str, max_size); | ||
| 527 | strcpy(tmp_str, to); | ||
| 528 | |||
| 529 | /* hours */ | ||
| 530 | snprintf(int_str, 32, "%d", hours); | ||
| 531 | i3lock_replace_str(to, tmp_str, "%h", int_str, max_size); | ||
| 532 | strcpy(tmp_str, to); | ||
| 533 | |||
| 534 | /* minutes */ | ||
| 535 | snprintf(int_str, 32, "%d", minutes); | ||
| 536 | i3lock_replace_str(to, tmp_str, "%m", int_str, max_size); | ||
| 537 | strcpy(tmp_str, to); | ||
| 538 | |||
| 539 | /* rest. seconds */ | ||
| 540 | snprintf(int_str, 32, "%d", rest_seconds); | ||
| 541 | i3lock_replace_str(to, tmp_str, "%s", int_str, max_size); | ||
| 542 | strcpy(tmp_str, to); | ||
| 543 | |||
| 544 | /* seconds */ | ||
| 545 | snprintf(int_str, 32, "%d", seconds); | ||
| 546 | i3lock_replace_str(to, tmp_str, "%S", int_str, max_size); | ||
| 547 | |||
| 548 | return to; | ||
| 549 | |||
| 550 | } | ||
