-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcblib.c
210 lines (171 loc) · 3.75 KB
/
lcblib.c
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "lcbdef.c"
#include <lauxlib.h>
#include <lua.h>
#include <ncurses.h>
#define LCB_VERSION "lcblib_0.1"
static int echo_enabled = 0;
static WINDOW *towin(lua_State *L, int i) {
WINDOW *win = (WINDOW *)lua_touserdata(L, i);
return win;
}
static int l_initscr(lua_State *L) {
initscr();
start_color();
lua_getfield(L, LUA_REGISTRYINDEX, "lcb");
lua_pushlightuserdata(L, stdscr);
lua_setfield(L, -2, "stdscr");
lua_pushinteger(L, LINES);
lua_setfield(L, -2, "LINES");
lua_pushinteger(L, COLS);
lua_setfield(L, -2, "COLS");
return 0;
}
static int l_endwin() {
endwin();
return 0;
}
static int l_printw(lua_State *L) {
printw(luaL_checkstring(L, -1));
return 0;
}
static int l_addstr(lua_State *L) {
while (lua_gettop(L) != 0) {
addstr(luaL_checkstring(L, -1));
lua_pop(L, -1);
}
return 0;
}
static int l_getch(lua_State *L) {
int ch = getch();
lua_pushinteger(L, ch);
return 1;
}
static int l_cbreak(lua_State *L) {
cbreak();
return 0;
}
static int l_echo(lua_State *L) {
echo();
echo_enabled = 1;
return 0;
}
static int l_noecho(lua_State *L) {
noecho();
echo_enabled = 0;
return 0;
}
static int l_newwin(lua_State *L) {
WINDOW *window = newwin(
luaL_checkinteger(L, 1),
luaL_checkinteger(L, 2),
luaL_checkinteger(L, 3),
luaL_checkinteger(L, 4)
);
lua_pushlightuserdata(L, window);
return 1;
}
static int l_delwin(lua_State *L) {
delwin(towin(L, 1));
return 1;
}
static int l_keypad(lua_State *L) {
WINDOW *win = towin(L, 1);
keypad(win, lua_toboolean(L, 2));
return 0;
}
static int l_colorpair(lua_State *L) {
int foreground = luaL_checkinteger(L, -1);
int background = luaL_checkinteger(L, -2);
int index = luaL_checkinteger(L, -3);
init_pair(index, foreground, background);
return 0;
}
static int l_wrefresh(lua_State *L) {
wrefresh(towin(L, 1));
return 0;
}
static int l_wbkgd(lua_State *L) {
wbkgd(stdscr, luaL_checkinteger(L, -1));
return 0;
}
static int l_move(lua_State *L) {
move(luaL_checkinteger(L, -1), luaL_checkinteger(L, -2));
return 0;
}
static int l_getmaxyx(lua_State *L) {
int l = luaL_checkinteger(L, -1);
int c = luaL_checkinteger(L, -2);
int ml, mc = getmaxyx(stdscr, l, c);
lua_pushinteger(L, ml);
lua_pushinteger(L, mc);
return 2;
}
static int l_attron(lua_State *L) {
int attr = luaL_checkinteger(L, -1);
attron(attr);
return 0;
}
static int l_attroff(lua_State *L) {
int attr = luaL_checkinteger(L, -1);
attroff(attr);
return 0;
}
static int l_COLOR_PAIR(lua_State *L) {
int p = luaL_checkinteger(L, -1);
COLOR_PAIR(p);
return 0;
}
static int l_nocbreak() {
nocbreak();
return 0;
}
static int l_refresh() {
refresh();
return 0;
}
static int l_clear() {
clear();
return 0;
}
static luaL_Reg lcbl[] = {
{"initscr", l_initscr},
{"endwin", l_endwin},
{"printw", l_printw},
{"addstr", l_addstr},
{"getch", l_getch},
{"cbreak", l_cbreak},
{"echo", l_echo},
{"noecho", l_noecho},
{"keypad", l_keypad},
{"initpair", l_colorpair},
{"wrefresh", l_wrefresh},
{"delwin", l_delwin},
{"newwin", l_newwin},
{"wbkgd", l_wbkgd},
{"move", l_move},
{"getmaxyx", l_getmaxyx},
{"attron", l_attron},
{"attroff", l_attroff},
{"COLOR_PAIR", l_COLOR_PAIR},
{"nocbreak", l_nocbreak},
{"clear", l_clear},
{"refresh", l_refresh},
{NULL, NULL}
};
int luaopen_lcblib(lua_State *L) {
luaL_newlib(L, lcbl);
lua_pushlightuserdata(L, stdscr);
lua_setfield(L, -2, "stdscr");
lua_pushinteger(L, LINES);
lua_setfield(L, -2, "LINES");
lua_pushinteger(L, COLS);
lua_setfield(L, -2, "COLUMNS");
lua_pushstring(L, LCB_VERSION);
lua_setfield(L, -2, "lcb_version");
define_ncurses_attbr(L);
define_ncurses_keys(L);
define_ncurses_colors(L);
lua_pushvalue(L, -1);
lua_setfield(L, LUA_REGISTRYINDEX, "lcb");
return 1;
}