Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New init function to selectively enable/disable some features #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/termbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();

Expand All @@ -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)
Expand All @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion src/termbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down