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