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