18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <stdio.h>
38c2ecf20Sopenharmony_ci#include <stdlib.h>
48c2ecf20Sopenharmony_ci#include <string.h>
58c2ecf20Sopenharmony_ci#include <pthread.h>
68c2ecf20Sopenharmony_ci#include <linux/kernel.h>
78c2ecf20Sopenharmony_ci#include <linux/string.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include "../helpline.h"
108c2ecf20Sopenharmony_ci#include "../ui.h"
118c2ecf20Sopenharmony_ci#include "../libslang.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cichar ui_helpline__last_msg[1024];
148c2ecf20Sopenharmony_cibool tui_helpline__set;
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistatic void tui_helpline__pop(void)
178c2ecf20Sopenharmony_ci{
188c2ecf20Sopenharmony_ci}
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic void tui_helpline__push(const char *msg)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	const size_t sz = sizeof(ui_helpline__current);
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	SLsmg_gotorc(SLtt_Screen_Rows - 1, 0);
258c2ecf20Sopenharmony_ci	SLsmg_set_color(0);
268c2ecf20Sopenharmony_ci	SLsmg_write_nstring((char *)msg, SLtt_Screen_Cols);
278c2ecf20Sopenharmony_ci	SLsmg_refresh();
288c2ecf20Sopenharmony_ci	strlcpy(ui_helpline__current, msg, sz);
298c2ecf20Sopenharmony_ci}
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic int tui_helpline__show(const char *format, va_list ap)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	int ret;
348c2ecf20Sopenharmony_ci	static int backlog;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	pthread_mutex_lock(&ui__lock);
378c2ecf20Sopenharmony_ci	ret = vscnprintf(ui_helpline__last_msg + backlog,
388c2ecf20Sopenharmony_ci			sizeof(ui_helpline__last_msg) - backlog, format, ap);
398c2ecf20Sopenharmony_ci	backlog += ret;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	tui_helpline__set = true;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	if (ui_helpline__last_msg[backlog - 1] == '\n') {
448c2ecf20Sopenharmony_ci		ui_helpline__puts(ui_helpline__last_msg);
458c2ecf20Sopenharmony_ci		SLsmg_refresh();
468c2ecf20Sopenharmony_ci		backlog = 0;
478c2ecf20Sopenharmony_ci	}
488c2ecf20Sopenharmony_ci	pthread_mutex_unlock(&ui__lock);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	return ret;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistruct ui_helpline tui_helpline_fns = {
548c2ecf20Sopenharmony_ci	.pop	= tui_helpline__pop,
558c2ecf20Sopenharmony_ci	.push	= tui_helpline__push,
568c2ecf20Sopenharmony_ci	.show	= tui_helpline__show,
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_civoid ui_helpline__init(void)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	helpline_fns = &tui_helpline_fns;
628c2ecf20Sopenharmony_ci	ui_helpline__puts(" ");
638c2ecf20Sopenharmony_ci}
64