diff options
Diffstat (limited to 'counter.c')
| -rw-r--r-- | counter.c | 184 |
1 files changed, 113 insertions, 71 deletions
@@ -1,15 +1,12 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" -/* TODO add buttons */ - USAGE("[-2s]"); static volatile sig_atomic_t caught_sigterm = 0; static volatile sig_atomic_t caught_sigwinch = 1; -static volatile sig_atomic_t caught_sigio = 1; static int with_small = 0; static int with_quad = 0; @@ -39,13 +36,6 @@ sigwinch(int signo) (void) signo; } -static void -sigio(int signo) -{ - caught_sigio = 1; - (void) signo; -} - static void print_number(const char ***str, int left, size_t height) @@ -69,13 +59,16 @@ print_number(const char ***str, int left, size_t height) static int display_counter(void) { + union libterminput_input input; + struct libterminput_state termctx; char buf[sizeof(intmax_t) * 3 + 2]; const char **digits[sizeof(intmax_t) * 3 + 2]; intmax_t counter = {0}; int r, i, x = 0, y = 0, was_quad = 0, len, width; + int redraw = 1; struct winsize winsize; - char c; - ssize_t rd; + + memset(&termctx, 0, sizeof(termctx)); while (!caught_sigterm) { if (caught_sigwinch) { @@ -92,37 +85,83 @@ display_counter(void) for (r = 0; r < y; r++) printf("%s\033#5", r ? "\n" : "\033[H"); } - } - - if (caught_sigio) { - caught_sigio = 0; - for (;;) { - rd = read(STDIN_FILENO, &c, 1); - if (rd <= 0) { - if (!rd) - goto out; - if (errno == EAGAIN) - break; - if (errno == EINTR) - continue; - goto fail; - } - if (c == 'q') + redraw = 1; + } else { + r = libterminput_read(STDIN_FILENO, &input, &termctx); + if (r <= 0) { + if (!r) goto out; - else if (c == 'r') - counter = 0; - else if (c == '+' || c == 'C') + if (errno == EAGAIN) + break; + if (errno == EINTR) + continue; + goto fail; + } + if (input.type == LIBTERMINPUT_KEYPRESS) { + if (input.keypress.key == LIBTERMINPUT_UP || + input.keypress.key == LIBTERMINPUT_RIGHT) { counter += 1; - else if (c == '-' || c == 'D') + redraw = 1; + } else if (input.keypress.key == LIBTERMINPUT_DOWN || + input.keypress.key == LIBTERMINPUT_LEFT) { counter -= 1; + redraw = 1; + } else if (input.keypress.key == LIBTERMINPUT_SYMBOL) { + if (!strcmp(input.keypress.symbol, "q") || + !strcmp(input.keypress.symbol, "Q")) { + goto out; + } else if (!strcmp(input.keypress.symbol, "r") || + !strcmp(input.keypress.symbol, "R")) { + counter = 0; + redraw = 1; + } else if (!strcmp(input.keypress.symbol, "+")) { + counter += 1; + redraw = 1; + } else if (!strcmp(input.keypress.symbol, "-")) { + counter -= 1; + redraw = 1; + } else if (!strcmp(input.keypress.symbol, "L") && + (input.keypress.mods & LIBTERMINPUT_CTRL)) { + redraw = 1; + } + } + } else if (input.type == LIBTERMINPUT_MOUSEEVENT && + input.mouseevent.event == LIBTERMINPUT_PRESS && + input.mouseevent.button == LIBTERMINPUT_BUTTON1) { + if (input.mouseevent.y > (size_t)y - 3) { + if (input.mouseevent.x <= 10) { + goto out; + } else if (input.mouseevent.x > (size_t)x - 11) { + counter = 0; + redraw = 1; + } + } + if (!redraw) { + if (input.mouseevent.y - 1 < (size_t)(y - 1) / 2) { + counter += 1; + redraw = 1; + } else { + counter -= 1; + redraw = 1; + } + } } } + if (!redraw) + continue; + redraw = 0; + len = sprintf(buf, "%ji", counter); printf("\033[H"); - if (x >= (width = len * LARGE_X + (counter < 0 ? LARGE_XM - LARGE_X : 0)) && y >= LARGE_Y) { + if (x >= (int)sizeof("Click on lower half on screen to increase value by 1") - 1) { + printf("%*.sClick on upper half on screen to increase value by 1", + (x - ((int)sizeof("Click on upper half on screen to increase value by 1") - 1)) / 2, ""); + } + + if (x >= (width = len * LARGE_X + (counter < 0 ? LARGE_XM - LARGE_X : 0)) && y >= LARGE_Y + 5) { for (r = 0; r < (y - LARGE_Y) / 2; r++) printf("\033[K\n"); for (i = 0; i < len; i++) { @@ -133,7 +172,8 @@ display_counter(void) } digits[i] = NULL; print_number(digits, (x - width) / 2, LARGE_Y); - } else if (with_small && x >= (width = len * SMALL_X + (counter < 0 ? SMALL_XM - SMALL_X : 0)) && y > SMALL_Y + 1) { + r += LARGE_Y; + } else if (with_small && x >= (width = len * SMALL_X + (counter < 0 ? SMALL_XM - SMALL_X : 0)) && y > SMALL_Y + 6) { for (r = 0; r < (y - SMALL_Y) / 2; r++) printf("\033[K\n"); for (i = 0; i < len; i++) { @@ -144,19 +184,50 @@ display_counter(void) } digits[i] = NULL; print_number(digits, (x - width) / 2, SMALL_Y); - } else if (with_quad && x > len * 2 && y > 2) { + r += SMALL_Y; + } else if (with_quad && x > len * 2 && y > 3) { printf("\033#3%*.s%s\033[K\n", (x / 2 - len) / 2, "", buf); printf("\033#4%*.s%s\033[K\n", (x / 2 - len) / 2, "", buf); printf("\033#5"); was_quad = 1; + r = 2; } else { printf("%*.s%s\033[K", (x - len) / 2, "", buf); + r = 1; } - printf("\033[J"); - fflush(stdout); + if (x >= 21 + (int)sizeof("Click on lower half on screen to decrease value by 1") - 1 && r + 3 <= y) { + width = 21 + ((int)sizeof("Click on lower half on screen to decrease value by 1") - 1); + for (; r <= y - 3; r++) + printf("\033[K\n"); + printf("┌────────┐%*.s┌─────────┐\033[K\n", x - 21, ""); + printf("│ \033[1mQ\033[muit │%*.s│ \033[1mR\033[meset │\033[K\n", x - 21, ""); + printf("└────────┘%*.s%s%*.s└─────────┘\033[K", + (x - width) / 2, "", + "Click on lower half on screen to decrease value by 1", + x - (x - width) / 2 - width, ""); + } else if (x >= (int)sizeof("Click on lower half on screen to decrease value by 1") - 1 && r + 4 <= y) { + for (; r <= y - 4; r++) + printf("\033[K\n"); + printf("%*.sClick on lower half on screen to decrease value by 1\033[K", + (x - ((int)sizeof("Click on lower half on screen to decrease value by 1") - 1)) / 2, ""); + printf("\n┌────────┐%*.s┌─────────┐\033[K\n", x - 21, ""); + printf("│ \033[1mQ\033[muit │%*.s│ \033[1mR\033[meset │\033[K\n", x - 21, ""); + printf("└────────┘%*.s└─────────┘\033[K", x - 21, ""); + } else if (x >= 21 && r + 3 <= y) { + for (; r <= y - 3; r++) + printf("\033[K\n"); + printf("┌────────┐%*.s┌─────────┐\033[K\n", x - 21, ""); + printf("│ \033[1mQ\033[muit │%*.s│ \033[1mR\033[meset │\033[K\n", x - 21, ""); + printf("└────────┘%*.s└─────────┘\033[K", x - 21, ""); + } else { + printf("\033[J"); + fflush(stdout); + continue; + } - pause(); + printf("\033[D\033[J─┘"); + fflush(stdout); } out: @@ -170,11 +241,9 @@ fail: int main(int argc, char *argv[]) { - int tcset = 0, old_flags = -1, old_sig = 0, owner_set = 0; - int saved_errno; + int tcset = 0, saved_errno; struct sigaction sigact; struct termios stty, saved_stty; - struct f_owner_ex old_owner, new_owner; ARGBEGIN { case '2': @@ -190,7 +259,7 @@ main(int argc, char *argv[]) if (argc) usage(); - fprintf(stdout, "\033[?1049h\033[?25l"); + fprintf(stdout, "\033[?1049h\033[?25l\033[?1049;1003;1006h"); memset(&sigact, 0, sizeof(sigact)); @@ -202,24 +271,6 @@ main(int argc, char *argv[]) sigact.sa_handler = sigwinch; sigaction(SIGWINCH, &sigact, NULL); - sigact.sa_handler = sigio; - sigaction(SIGIO, &sigact, NULL); - sigaction(SIGURG, &sigact, NULL); - - if (fcntl(STDIN_FILENO, F_GETOWN_EX, &old_owner)) - goto fail; - memset(&new_owner, 0, sizeof(new_owner)); - new_owner.type = F_OWNER_PID; - new_owner.pid = getpid(); - if (fcntl(STDIN_FILENO, F_SETOWN_EX, &new_owner)) - goto fail; - owner_set = 1; - old_flags = fcntl(STDIN_FILENO, F_GETFL); - fcntl(STDIN_FILENO, F_SETFL, old_flags | FASYNC | O_NONBLOCK); - fcntl(STDIN_FILENO, F_GETSIG, &old_sig); - if (old_sig) - fcntl(STDIN_FILENO, F_SETSIG, 0); - if (!tcgetattr(STDIN_FILENO, &stty)) { saved_stty = stty; stty.c_lflag &= (tcflag_t)~(ECHO | ICANON); @@ -230,26 +281,17 @@ main(int argc, char *argv[]) if (display_counter()) goto fail; - fprintf(stdout, "\033[?25h\n\033[?1049l"); + fprintf(stdout, "\033[?1049;1003;1006l\033[?25h\n\033[?1049l"); fflush(stdout); - fcntl(STDIN_FILENO, F_SETOWN_EX, &old_owner); - fcntl(STDIN_FILENO, F_SETFL, old_flags); - fcntl(STDIN_FILENO, F_SETSIG, old_sig); tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_stty); return 0; fail: saved_errno = errno; - fprintf(stdout, "\033[?25h\n\033[?1049l"); + fprintf(stdout, "\033[?1049;1003;1006l\033[?25h\n\033[?1049l"); fflush(stdout); errno = saved_errno; perror(argv0 ? argv0 : "counter"); - if (owner_set) - fcntl(STDIN_FILENO, F_SETOWN_EX, &old_owner); - if (old_flags != -1) - fcntl(STDIN_FILENO, F_SETFL, old_flags); - if (old_sig) - fcntl(STDIN_FILENO, F_SETSIG, old_sig); if (tcset) tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_stty); return 1; |
