diff --git a/src/termbox.c b/src/termbox.c index ff6a775..aeb1cb9 100644 --- a/src/termbox.c +++ b/src/termbox.c @@ -36,6 +36,8 @@ static struct cellbuf front_buffer; static struct bytebuffer output_buffer; static struct bytebuffer input_buffer; +static int initflags = TB_INIT_EVERYTHING; + static int termw = -1; static int termh = -1; @@ -74,7 +76,7 @@ static volatile int buffer_size_change_request; /* -------------------------------------------------------- */ -int tb_init_fd(int inout_) +int tb_init_fd(int inout_, int flags) { inout = inout_; if (inout == -1) { @@ -115,8 +117,11 @@ int tb_init_fd(int inout_) bytebuffer_init(&input_buffer, 128); bytebuffer_init(&output_buffer, 32 * 1024); - bytebuffer_puts(&output_buffer, funcs[T_ENTER_CA]); - bytebuffer_puts(&output_buffer, funcs[T_ENTER_KEYPAD]); + initflags = flags; + if (initflags & TB_INIT_ALTSCREEN) + bytebuffer_puts(&output_buffer, funcs[T_ENTER_CA]); + if (initflags & TB_INIT_KEYPAD) + bytebuffer_puts(&output_buffer, funcs[T_ENTER_KEYPAD]); bytebuffer_puts(&output_buffer, funcs[T_HIDE_CURSOR]); send_clear(); @@ -129,13 +134,19 @@ int tb_init_fd(int inout_) return 0; } -int tb_init_file(const char* name){ - return tb_init_fd(open(name, O_RDWR)); +int tb_init_file(const char* name) +{ + return tb_init_fd(open(name, O_RDWR), TB_INIT_EVERYTHING); +} + +int tb_init_with(int flags) +{ + return tb_init_fd(open("/dev/tty", O_RDWR), flags); } int tb_init(void) { - return tb_init_file("/dev/tty"); + return tb_init_fd(open("/dev/tty", O_RDWR), TB_INIT_EVERYTHING); } void tb_shutdown(void) @@ -148,8 +159,10 @@ void tb_shutdown(void) bytebuffer_puts(&output_buffer, funcs[T_SHOW_CURSOR]); bytebuffer_puts(&output_buffer, funcs[T_SGR0]); bytebuffer_puts(&output_buffer, funcs[T_CLEAR_SCREEN]); - bytebuffer_puts(&output_buffer, funcs[T_EXIT_CA]); - bytebuffer_puts(&output_buffer, funcs[T_EXIT_KEYPAD]); + if (initflags & TB_INIT_ALTSCREEN) + bytebuffer_puts(&output_buffer, funcs[T_EXIT_CA]); + if (initflags & TB_INIT_KEYPAD) + bytebuffer_puts(&output_buffer, funcs[T_EXIT_KEYPAD]); bytebuffer_puts(&output_buffer, funcs[T_EXIT_MOUSE]); bytebuffer_flush(&output_buffer, inout); tcsetattr(inout, TCSAFLUSH, &orig_tios); diff --git a/src/termbox.h b/src/termbox.h index dc2d372..873ddec 100644 --- a/src/termbox.h +++ b/src/termbox.h @@ -171,14 +171,21 @@ struct tb_event { #define TB_EFAILED_TO_OPEN_TTY -2 #define TB_EPIPE_TRAP_ERROR -3 +/* Flags passed to tb_init_with() to specify which features should be enabled. + */ +#define TB_INIT_ALTSCREEN 1 +#define TB_INIT_KEYPAD 2 +#define TB_INIT_EVERYTHING -1 + /* Initializes the termbox library. This function should be called before any * other functions. Function tb_init is same as tb_init_file("/dev/tty"). * After successful initialization, the library must be * finalized using the tb_shutdown() function. */ SO_IMPORT int tb_init(void); +SO_IMPORT int tb_init_with(int flags); SO_IMPORT int tb_init_file(const char* name); -SO_IMPORT int tb_init_fd(int inout); +SO_IMPORT int tb_init_fd(int inout, int flags); SO_IMPORT void tb_shutdown(void); /* Returns the size of the internal back buffer (which is the same as