-
Notifications
You must be signed in to change notification settings - Fork 3
/
lua_uv_tty.cpp
92 lines (80 loc) · 2.16 KB
/
lua_uv_tty.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "luaopen_uv.h"
int lua_uv_tty(lua_State * L) {
uv_file fd = luaL_checkint(L, 1);
int readable = lua_toboolean(L, 2);
uv_loop_t * loop = lua_uv_loop(L);
uv_tty_t * tty = (uv_tty_t *)lua_newuserdata(L, sizeof(uv_tty_t));
uv_tty_init(loop, tty, fd, readable);
luaL_getmetatable(L, classname<uv_tty_t>());
lua_setmetatable(L, -2);
lua_uv_ok(L);
return 1;
}
int lua_uv_tty_set_mode(lua_State * L) {
uv_tty_t * tty = lua_generic_object<uv_tty_t>(L, 1);
int mode = luaL_checkinteger(L, 2);
uv_tty_set_mode(tty, mode);
lua_uv_ok(L);
return 0;
}
int lua_uv_tty_reset_mode(lua_State * L) {
uv_tty_reset_mode();
return 0;
}
///! Returns character size of TTY shell window
int lua_uv_tty_get_win_size(lua_State * L) {
uv_tty_t * tty = lua_generic_object<uv_tty_t>(L, 1);
int w, h;
uv_tty_get_winsize(tty, &w, &h);
lua_uv_ok(L);
lua_pushinteger(L, w);
lua_pushinteger(L, h);
return 2;
}
// bind loop as an upvalue of all the methods:
#define LUA_UV_CLOSURE(PREFIX, NAME) \
lua_pushvalue(L, loopidx); \
lua_pushcclosure(L, lua_##PREFIX##_##NAME, 1); \
lua_setfield(L, -2, #NAME);
void init_tty_metatable(lua_State * L, int loopidx) {
// uv_tty_t metatable:
luaL_newmetatable(L, classname<uv_tty_t>());
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_generic___tostring<uv_tty_t>);
lua_setfield(L, -2, "__tostring");
lua_pushcfunction(L, lua_uv_handle_close);
lua_setfield(L, -2, "__gc");
LUA_UV_CLOSURE(uv_tty, set_mode);
LUA_UV_CLOSURE(uv_tty, reset_mode);
LUA_UV_CLOSURE(uv_tty, get_win_size);
// uv_tty_t inherits uv_stream_t:
lua_super_metatable(L, -1, classname<uv_stream_t>());
lua_pop(L, 1);
}
//extern "C" int luaopen_uv_tty(lua_State * L) {
// // don't hold the event loop open for them
// uv_loop_t * loop = lua_uv_main_loop(L);
// int loopidx = lua_gettop(L);
//
// init_stream_metatable(L, loopidx);
// init_tty_metatable(L, loopidx);
//
// struct luaL_reg lib[] = {
// { NULL, NULL }
// };
// lua_newtable(L);
// luaL_register(L, "uv.tty", lib);
//
//
//
//
// LUA_UV_CLOSURE(uv_tty, new);
//
// // create stdin, stdout:
// // Tty:new(0)
// // uv_unref(loop);
// // uv_unref(loop);
//
// return 1;
//}