LuPPC/src/c/termutils.c

61 lines
1.3 KiB
C
Raw Normal View History

2016-01-07 02:58:05 +11:00
#include "lupi.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <string.h>
#include <signal.h>
#include <sys/ioctl.h>
2016-01-15 08:44:49 +11:00
#include <termios.h>
2016-01-07 02:58:05 +11:00
#include <stdio.h>
#include <unistd.h>
2016-02-10 03:42:11 +11:00
struct termios old, new;
2016-01-07 02:58:05 +11:00
static void handle_winch(int sig){
signal(SIGWINCH, SIG_IGN);
2016-01-19 07:24:13 +11:00
/* FIXME: Prerelease: Implement */
2016-01-07 02:58:05 +11:00
signal(SIGWINCH, handle_winch);
}
static int l_get_term_sz (lua_State *L) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
lua_pushnumber(L, w.ws_col);
lua_pushnumber(L, w.ws_row);
return 2;
}
2016-02-10 03:42:11 +11:00
static int l_term_restore (lua_State *L) {
tcsetattr (STDOUT_FILENO, TCSAFLUSH, &old);
return 0;
}
static int l_term_init (lua_State *L) {
tcsetattr (STDOUT_FILENO, TCSAFLUSH, &new);
return 0;
}
2016-01-07 02:58:05 +11:00
void termutils_start(lua_State *L) {
signal(SIGWINCH, handle_winch);
2016-01-15 08:44:49 +11:00
if (tcgetattr (STDOUT_FILENO, &old) != 0)
return;
2016-01-16 05:58:26 +11:00
new = old;
2016-01-19 07:24:13 +11:00
new.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
| INLCR | IGNCR | ICRNL | IXON);
new.c_oflag &= ~OPOST;
new.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
new.c_cflag &= ~(CSIZE | PARENB);
new.c_cflag |= CS8;
2016-02-26 02:57:08 +11:00
struct luaL_Reg termlib[] = {
{"getSize", l_get_term_sz},
{"init", l_term_init},
{"restore", l_term_restore},
{NULL, NULL}
};
luaL_openlib(L, "termutils", termlib, 0);
2016-01-07 02:58:05 +11:00
}