Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[~shefty/rdma-dev.git] / tools / perf / util / ui / setup.c
1 #include <newt.h>
2 #include <signal.h>
3 #include <stdbool.h>
4
5 #include "../cache.h"
6 #include "../debug.h"
7 #include "browser.h"
8 #include "helpline.h"
9 #include "ui.h"
10 #include "libslang.h"
11
12 pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER;
13
14 static void newt_suspend(void *d __used)
15 {
16         newtSuspend();
17         raise(SIGTSTP);
18         newtResume();
19 }
20
21 static int ui__init(void)
22 {
23         int err = SLkp_init();
24
25         if (err < 0)
26                 goto out;
27
28         SLkp_define_keysym((char *)"^(kB)", SL_KEY_UNTAB);
29 out:
30         return err;
31 }
32
33 static void ui__exit(void)
34 {
35         SLtt_set_cursor_visibility(1);
36         SLsmg_refresh();
37         SLsmg_reset_smg();
38         SLang_reset_tty();
39 }
40
41 static void ui__signal(int sig)
42 {
43         ui__exit();
44         psignal(sig, "perf");
45         exit(0);
46 }
47
48 void setup_browser(bool fallback_to_pager)
49 {
50         if (!isatty(1) || !use_browser || dump_trace) {
51                 use_browser = 0;
52                 if (fallback_to_pager)
53                         setup_pager();
54                 return;
55         }
56
57         use_browser = 1;
58         newtInit();
59         ui__init();
60         newtSetSuspendCallback(newt_suspend, NULL);
61         ui_helpline__init();
62         ui_browser__init();
63
64         signal(SIGSEGV, ui__signal);
65         signal(SIGFPE, ui__signal);
66         signal(SIGINT, ui__signal);
67         signal(SIGQUIT, ui__signal);
68         signal(SIGTERM, ui__signal);
69 }
70
71 void exit_browser(bool wait_for_ok)
72 {
73         if (use_browser > 0) {
74                 if (wait_for_ok) {
75                         char title[] = "Fatal Error", ok[] = "Ok";
76                         newtWinMessage(title, ok, ui_helpline__last_msg);
77                 }
78                 ui__exit();
79         }
80 }