^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <pthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "../helpline.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "../ui.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "../libslang.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) char ui_helpline__last_msg[1024];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) bool tui_helpline__set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static void tui_helpline__pop(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static void tui_helpline__push(const char *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) const size_t sz = sizeof(ui_helpline__current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) SLsmg_gotorc(SLtt_Screen_Rows - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) SLsmg_set_color(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) SLsmg_write_nstring((char *)msg, SLtt_Screen_Cols);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) SLsmg_refresh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) strlcpy(ui_helpline__current, msg, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static int tui_helpline__show(const char *format, va_list ap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static int backlog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) pthread_mutex_lock(&ui__lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) ret = vscnprintf(ui_helpline__last_msg + backlog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) sizeof(ui_helpline__last_msg) - backlog, format, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) backlog += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) tui_helpline__set = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (ui_helpline__last_msg[backlog - 1] == '\n') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) ui_helpline__puts(ui_helpline__last_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) SLsmg_refresh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) backlog = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) pthread_mutex_unlock(&ui__lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct ui_helpline tui_helpline_fns = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) .pop = tui_helpline__pop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) .push = tui_helpline__push,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) .show = tui_helpline__show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) void ui_helpline__init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) helpline_fns = &tui_helpline_fns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ui_helpline__puts(" ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }