setup.c 3.0 KB
Newer Older
1 2
#include <signal.h>
#include <stdbool.h>
3 4 5
#ifdef HAVE_BACKTRACE_SUPPORT
#include <execinfo.h>
#endif
6 7 8 9 10 11 12 13 14

#include "../../util/cache.h"
#include "../../util/debug.h"
#include "../browser.h"
#include "../helpline.h"
#include "../ui.h"
#include "../util.h"
#include "../libslang.h"
#include "../keysyms.h"
15
#include "tui.h"
16 17 18

static volatile int ui__need_resize;

19
extern struct perf_error_ops perf_tui_eops;
20
extern bool tui_helpline__set;
21

22 23
extern void hist_browser__init_hpp(void);

24 25 26 27 28 29 30 31 32 33 34
void ui__refresh_dimensions(bool force)
{
	if (force || ui__need_resize) {
		ui__need_resize = 0;
		pthread_mutex_lock(&ui__lock);
		SLtt_get_screen_size();
		SLsmg_reinit_smg();
		pthread_mutex_unlock(&ui__lock);
	}
}

35
static void ui__sigwinch(int sig __maybe_unused)
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
{
	ui__need_resize = 1;
}

static void ui__setup_sigwinch(void)
{
	static bool done;

	if (done)
		return;

	done = true;
	pthread__unblock_sigwinch();
	signal(SIGWINCH, ui__sigwinch);
}

int ui__getch(int delay_secs)
{
	struct timeval timeout, *ptimeout = delay_secs ? &timeout : NULL;
	fd_set read_set;
	int err, key;

	ui__setup_sigwinch();

	FD_ZERO(&read_set);
	FD_SET(0, &read_set);

	if (delay_secs) {
		timeout.tv_sec = delay_secs;
		timeout.tv_usec = 0;
	}

        err = select(1, &read_set, NULL, NULL, ptimeout);

	if (err == 0)
		return K_TIMER;

	if (err == -1) {
		if (errno == EINTR)
			return K_RESIZE;
		return K_ERROR;
	}

	key = SLang_getkey();
	if (key != K_ESC)
		return key;

	FD_ZERO(&read_set);
	FD_SET(0, &read_set);
	timeout.tv_sec = 0;
	timeout.tv_usec = 20;
        err = select(1, &read_set, NULL, NULL, &timeout);
	if (err == 0)
		return K_ESC;

	SLang_ungetkey(key);
	return SLkp_getkey();
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#ifdef HAVE_BACKTRACE_SUPPORT
static void ui__signal_backtrace(int sig)
{
	void *stackdump[32];
	size_t size;

	ui__exit(false);
	psignal(sig, "perf");

	printf("-------- backtrace --------\n");
	size = backtrace(stackdump, ARRAY_SIZE(stackdump));
	backtrace_symbols_fd(stackdump, size, STDOUT_FILENO);

	exit(0);
}
#else
# define ui__signal_backtrace  ui__signal
#endif

114 115 116 117 118 119 120
static void ui__signal(int sig)
{
	ui__exit(false);
	psignal(sig, "perf");
	exit(0);
}

121
int ui__init(void)
122 123 124
{
	int err;

125 126 127 128 129 130 131
	SLutf8_enable(-1);
	SLtt_get_terminfo();
	SLtt_get_screen_size();

	err = SLsmg_init_smg();
	if (err < 0)
		goto out;
132
	err = SLang_init_tty(-1, 0, 0);
133 134 135
	if (err < 0)
		goto out;

136 137 138 139 140 141 142 143
	err = SLkp_init();
	if (err < 0) {
		pr_err("TUI initialization failed.\n");
		goto out;
	}

	SLkp_define_keysym((char *)"^(kB)", SL_KEY_UNTAB);

144 145
	signal(SIGSEGV, ui__signal_backtrace);
	signal(SIGFPE, ui__signal_backtrace);
146 147 148
	signal(SIGINT, ui__signal);
	signal(SIGQUIT, ui__signal);
	signal(SIGTERM, ui__signal);
149 150

	perf_error__register(&perf_tui_eops);
151

152 153 154 155
	ui_helpline__init();
	ui_browser__init();
	tui_progress__init();

156
	hist_browser__init_hpp();
157 158 159 160 161 162
out:
	return err;
}

void ui__exit(bool wait_for_ok)
{
163
	if (wait_for_ok && tui_helpline__set)
164 165 166 167 168 169 170 171
		ui__question_window("Fatal Error",
				    ui_helpline__last_msg,
				    "Press any key...", 0);

	SLtt_set_cursor_visibility(1);
	SLsmg_refresh();
	SLsmg_reset_smg();
	SLang_reset_tty();
172 173

	perf_error__unregister(&perf_tui_eops);
174
}