-
Notifications
You must be signed in to change notification settings - Fork 1
/
term.c
295 lines (270 loc) · 7.15 KB
/
term.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "term.h"
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <ncurses.h>
#define TERM_WIDTH 80
#define TERM_HEIGHT 25
unsigned int cursorX = 0, cursorY = 0;
unsigned char colors = 0x07;
const unsigned char colorMap[16] = {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
void termInit() {
int i;
initscr();
keypad(stdscr, TRUE);
noecho();
cbreak();
nodelay(stdscr, TRUE);
start_color();
for (i = 0; i < 256; i++) init_pair(i, i & 0x0f, i >> 4);
}
void termClose() {
echo();
nocbreak();
nodelay(stdscr, FALSE);
keypad(stdscr, FALSE);
endwin();
}
#define setchar(x, y, c) mvaddch(y, x, c);
int term_write(lua_State *L) {
int i;
const char * str = lua_tostring(L, 1);
#ifdef TESTING
printf("%s\n", str);
#endif
for (i = 0; i < strlen(str) && cursorX < TERM_WIDTH; i++, cursorX++)
setchar(cursorX, cursorY, str[i]);
refresh();
return 0;
}
int term_scroll(lua_State *L) {
scrl(lua_tointeger(L, 1));
refresh();
return 0;
}
int term_setCursorPos(lua_State *L) {
cursorX = lua_tointeger(L, 1) - 1;
cursorY = lua_tointeger(L, 2) - 1;
move(cursorY, cursorX);
refresh();
return 0;
}
int term_setCursorBlink(lua_State *L) {
curs_set(lua_toboolean(L, 1));
refresh();
return 0;
}
int term_getCursorPos(lua_State *L) {
lua_pushinteger(L, cursorX + 1);
lua_pushinteger(L, cursorY + 1);
return 2;
}
int term_getSize(lua_State *L) {
lua_pushinteger(L, TERM_WIDTH);
lua_pushinteger(L, TERM_HEIGHT);
return 2;
}
int term_clear(lua_State *L) {
clear();
refresh();
return 0;
}
int term_clearLine(lua_State *L) {
move(cursorY, 0);
clrtoeol();
refresh();
return 0;
}
int log2i(int num) {
int retval;
for (retval = 0; (num & 1) == 0; retval++) num = num >> 1;
return retval;
}
int term_setTextColor(lua_State *L) {
int c = colorMap[log2i(lua_tointeger(L, 1))];
attroff(COLOR_PAIR(colors));
colors = (colors & 0xf0) | c;
attron(COLOR_PAIR(colors));
return 0;
}
int term_setBackgroundColor(lua_State *L) {
int c = colorMap[log2i(lua_tointeger(L, 1))];
attroff(COLOR_PAIR(colors));
colors = (colors & 0x0f) | (c << 4);
attron(COLOR_PAIR(colors));
return 0;
}
int term_isColor(lua_State *L) {
lua_pushboolean(L, true);
return 1;
}
int term_getTextColor(lua_State *L) {
lua_pushinteger(L, 1 >> colorMap[colors & 0x0f]);
return 1;
}
int term_getBackgroundColor(lua_State *L) {
lua_pushinteger(L, 1 >> colorMap[colors >> 4]);
return 1;
}
char htoi(char c) {
if (c >= '0' && c <= '9') return c - '0';
else if (c >= 'a' && c <= 'f') return c - 'a' + 10;
else if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return 0;
}
int term_blit(lua_State *L) {
int i;
const char * str = lua_tostring(L, 1);
const char * fg = lua_tostring(L, 2);
const char * bg = lua_tostring(L, 3);
for (i = 0; i < strlen(str) && cursorX < TERM_WIDTH; i++, cursorX++) {
attroff(COLOR_PAIR(colors));
colors = colorMap[htoi(bg[i])] << 4 | colorMap[htoi(fg[i])];
attron(COLOR_PAIR(colors));
setchar(cursorX, cursorY, str[i]);
}
refresh();
return 0;
}
int term_getPaletteColor(lua_State *L) {
int color = colorMap[log2i(lua_tointeger(L, 1))];
switch (color) {
case 0:
lua_pushnumber(L, 0.0/255.0);
lua_pushnumber(L, 0.0/255.0);
lua_pushnumber(L, 0.0/255.0);
return 3;
case 1:
lua_pushnumber(L, 0.0/255.0);
lua_pushnumber(L, 0.0/255.0);
lua_pushnumber(L, 170.0/255.0);
return 3;
case 2:
lua_pushnumber(L, 0.0/255.0);
lua_pushnumber(L, 170.0/255.0);
lua_pushnumber(L, 0.0/255.0);
return 3;
case 3:
lua_pushnumber(L, 0.0/255.0);
lua_pushnumber(L, 170.0/255.0);
lua_pushnumber(L, 170.0/255.0);
return 3;
case 4:
lua_pushnumber(L, 170.0/255.0);
lua_pushnumber(L, 0.0/255.0);
lua_pushnumber(L, 0.0/255.0);
return 3;
case 5:
lua_pushnumber(L, 170.0/255.0);
lua_pushnumber(L, 0.0/255.0);
lua_pushnumber(L, 170.0/255.0);
return 3;
case 6:
lua_pushnumber(L, 170.0/255.0);
lua_pushnumber(L, 85.0/255.0);
lua_pushnumber(L, 0.0/255.0);
return 3;
case 7:
lua_pushnumber(L, 170.0/255.0);
lua_pushnumber(L, 170.0/255.0);
lua_pushnumber(L, 170.0/255.0);
return 3;
case 8:
lua_pushnumber(L, 85.0/255.0);
lua_pushnumber(L, 85.0/255.0);
lua_pushnumber(L, 85.0/255.0);
return 3;
case 9:
lua_pushnumber(L, 85.0/255.0);
lua_pushnumber(L, 85.0/255.0);
lua_pushnumber(L, 255.0/255.0);
return 3;
case 10:
lua_pushnumber(L, 85.0/255.0);
lua_pushnumber(L, 255.0/255.0);
lua_pushnumber(L, 85.0/255.0);
return 3;
case 11:
lua_pushnumber(L, 85.0/255.0);
lua_pushnumber(L, 255.0/255.0);
lua_pushnumber(L, 255.0/255.0);
return 3;
case 12:
lua_pushnumber(L, 255.0/255.0);
lua_pushnumber(L, 85.0/255.0);
lua_pushnumber(L, 85.0/255.0);
return 3;
case 13:
lua_pushnumber(L, 255.0/255.0);
lua_pushnumber(L, 85.0/255.0);
lua_pushnumber(L, 255.0/255.0);
return 3;
case 14:
lua_pushnumber(L, 255.0/255.0);
lua_pushnumber(L, 255.0/255.0);
lua_pushnumber(L, 85.0/255.0);
return 3;
case 15:
lua_pushnumber(L, 255.0/255.0);
lua_pushnumber(L, 255.0/255.0);
lua_pushnumber(L, 255.0/255.0);
return 3;
default:
return 0;
}
}
int term_setPaletteColor(lua_State *L) {
return 0;
}
const char * term_keys[23] = {
"write",
"scroll",
"setCursorPos",
"setCursorBlink",
"getCursorPos",
"getSize",
"clear",
"clearLine",
"setTextColour",
"setTextColor",
"setBackgroundColour",
"setBackgroundColor",
"isColour",
"isColor",
"getTextColour",
"getTextColor",
"getBackgroundColour",
"getBackgroundColor",
"blit",
"getPaletteColor",
"getPaletteColour",
"setPaletteColor",
"setPaletteColour"
};
lua_CFunction term_values[23] = {
term_write,
term_scroll,
term_setCursorPos,
term_setCursorBlink,
term_getCursorPos,
term_getSize,
term_clear,
term_clearLine,
term_setTextColor,
term_setTextColor,
term_setBackgroundColor,
term_setBackgroundColor,
term_isColor,
term_isColor,
term_getTextColor,
term_getTextColor,
term_getBackgroundColor,
term_getBackgroundColor,
term_blit,
term_getPaletteColor,
term_getPaletteColor,
term_setPaletteColor,
term_setPaletteColor
};
library_t term_lib = {"term", 23, term_keys, term_values};