Skip to content

Commit

Permalink
Move terminal reset to atexit() to catch abnormal exits.
Browse files Browse the repository at this point in the history
  • Loading branch information
wenottingham committed Nov 9, 2023
1 parent e929689 commit 8369c08
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* use MicroConf to parse config file
* make it possible to have vim-like seek keys
(configure hjkl keyboard shortcuts to arrow keys)
- fix terminal settings after crash

wishlist:
- wav output with --wav foo.wav
Expand Down
31 changes: 18 additions & 13 deletions src/terminal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ using std::map;
static char term_buffer[4096];
static char term_buffer2[4096];
static char *term_p = term_buffer2;
static struct termios tio_orig;
static Terminal *terminal_instance;

static GPollFD stdin_poll_fd = { 0, G_IO_IN, 0 };
Expand All @@ -57,6 +58,17 @@ stdin_check (GSource *source)
return FALSE;
}

static void
reset_terminal ()
{
tcsetattr(0, TCSANOW, &tio_orig);
char *ret = tgetstr ("ke", &term_p);
// disable keypad xmit
if (ret)
printf ("%s", ret);
fflush (stdout);
}

gboolean
Terminal::stdin_dispatch (GSource *source,
GSourceFunc callback,
Expand All @@ -83,15 +95,6 @@ Terminal::signal_sig_cont (int)
terminal_instance->init_terminal();
}

void
Terminal::print_term (const char *key)
{
char *ret = tgetstr (const_cast<char *> (key), &term_p);

if (ret)
printf ("%s", ret);
}

void
Terminal::init_terminal()
{
Expand All @@ -105,7 +108,10 @@ Terminal::init_terminal()
tcsetattr (0, TCSANOW, &tio_new);

// enable keypad_xmit
print_term ("ks");
char *ret = tgetstr ("ks", &term_p);
// disable keypad xmit
if (ret)
printf ("%s", ret);
fflush (stdout);
}

Expand Down Expand Up @@ -134,6 +140,7 @@ Terminal::init (GMainLoop *loop, KeyHandler *key_handler)

// initialize termios & keypad xmit
init_terminal();
atexit(reset_terminal);

// initialize common keyboard escape sequences
bind_key ("ku", KEY_HANDLER_UP);
Expand All @@ -155,9 +162,7 @@ Terminal::init (GMainLoop *loop, KeyHandler *key_handler)
void
Terminal::end()
{
tcsetattr(0,TCSANOW,&tio_orig);
// disable keypad xmit
print_term("ke");
reset_terminal();
}

void
Expand Down
1 change: 0 additions & 1 deletion src/terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

class Terminal
{
struct termios tio_orig;
std::string terminal_type;
std::vector<int> chars;
std::map<std::string, int> keys;
Expand Down

0 comments on commit 8369c08

Please sign in to comment.