2016-07-07 22:38:09 +08:00
|
|
|
#include <errno.h>
|
2012-04-30 12:55:08 +08:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
2017-04-17 22:39:06 +08:00
|
|
|
#include <linux/kernel.h>
|
2014-12-22 12:44:11 +08:00
|
|
|
#ifdef HAVE_BACKTRACE_SUPPORT
|
|
|
|
#include <execinfo.h>
|
|
|
|
#endif
|
2012-04-30 12:55:08 +08:00
|
|
|
|
|
|
|
#include "../../util/cache.h"
|
|
|
|
#include "../../util/debug.h"
|
2016-07-07 22:38:09 +08:00
|
|
|
#include "../../util/util.h"
|
2012-04-30 12:55:08 +08:00
|
|
|
#include "../browser.h"
|
|
|
|
#include "../helpline.h"
|
|
|
|
#include "../ui.h"
|
|
|
|
#include "../util.h"
|
|
|
|
#include "../libslang.h"
|
|
|
|
#include "../keysyms.h"
|
2013-10-24 01:08:48 +08:00
|
|
|
#include "tui.h"
|
2012-04-30 12:55:08 +08:00
|
|
|
|
|
|
|
static volatile int ui__need_resize;
|
|
|
|
|
2012-05-29 12:22:58 +08:00
|
|
|
extern struct perf_error_ops perf_tui_eops;
|
2015-01-20 14:40:50 +08:00
|
|
|
extern bool tui_helpline__set;
|
2012-05-29 12:22:58 +08:00
|
|
|
|
2012-09-03 10:53:09 +08:00
|
|
|
extern void hist_browser__init_hpp(void);
|
|
|
|
|
2012-04-30 12:55:08 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-11 06:15:03 +08:00
|
|
|
static void ui__sigwinch(int sig __maybe_unused)
|
2012-04-30 12:55:08 +08:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2014-12-22 12:44:11 +08:00
|
|
|
#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
|
|
|
|
|
2012-04-30 12:55:08 +08:00
|
|
|
static void ui__signal(int sig)
|
|
|
|
{
|
|
|
|
ui__exit(false);
|
|
|
|
psignal(sig, "perf");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2012-04-30 12:55:09 +08:00
|
|
|
int ui__init(void)
|
2012-04-30 12:55:08 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2013-03-28 22:34:10 +08:00
|
|
|
SLutf8_enable(-1);
|
|
|
|
SLtt_get_terminfo();
|
|
|
|
SLtt_get_screen_size();
|
|
|
|
|
|
|
|
err = SLsmg_init_smg();
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
2015-05-29 20:53:44 +08:00
|
|
|
err = SLang_init_tty(-1, 0, 0);
|
2013-03-28 22:34:10 +08:00
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
|
2012-04-30 12:55:08 +08:00
|
|
|
err = SLkp_init();
|
|
|
|
if (err < 0) {
|
|
|
|
pr_err("TUI initialization failed.\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
SLkp_define_keysym((char *)"^(kB)", SL_KEY_UNTAB);
|
|
|
|
|
2014-12-22 12:44:11 +08:00
|
|
|
signal(SIGSEGV, ui__signal_backtrace);
|
|
|
|
signal(SIGFPE, ui__signal_backtrace);
|
2012-04-30 12:55:08 +08:00
|
|
|
signal(SIGINT, ui__signal);
|
|
|
|
signal(SIGQUIT, ui__signal);
|
|
|
|
signal(SIGTERM, ui__signal);
|
2012-05-29 12:22:58 +08:00
|
|
|
|
|
|
|
perf_error__register(&perf_tui_eops);
|
2012-09-03 10:53:09 +08:00
|
|
|
|
2015-10-23 03:44:17 +08:00
|
|
|
ui_helpline__init();
|
|
|
|
ui_browser__init();
|
|
|
|
tui_progress__init();
|
|
|
|
|
2012-09-03 10:53:09 +08:00
|
|
|
hist_browser__init_hpp();
|
2012-04-30 12:55:08 +08:00
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui__exit(bool wait_for_ok)
|
|
|
|
{
|
2015-01-20 14:40:50 +08:00
|
|
|
if (wait_for_ok && tui_helpline__set)
|
2012-04-30 12:55:08 +08:00
|
|
|
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();
|
2012-05-29 12:22:58 +08:00
|
|
|
|
|
|
|
perf_error__unregister(&perf_tui_eops);
|
2012-04-30 12:55:08 +08:00
|
|
|
}
|