aboutsummaryrefslogtreecommitdiffstats
path: root/src/curses.c
blob: 5be65d146b16b828552ebe6fbdf8e01f058e7e0e (plain) (blame)
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <curses.h>
#include <lua.h>
#include <lauxlib.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>

#define REG_TABLE "luancurses"

typedef struct _pos {
    int x;
    int y;
} pos;

static int ncolors = 1, ncolor_pairs = 1;

/* necessary because atexit() expects a function that returns void, and gcc
 * whines otherwise */
static void _endwin(void)
{
    endwin();
}

static void init_colors(lua_State* L)
{
    lua_pushinteger(L, COLOR_BLACK);
    lua_setfield(L, -2, "black");
    lua_pushinteger(L, COLOR_RED);
    lua_setfield(L, -2, "red");
    lua_pushinteger(L, COLOR_GREEN);
    lua_setfield(L, -2, "green");
    lua_pushinteger(L, COLOR_YELLOW);
    lua_setfield(L, -2, "yellow");
    lua_pushinteger(L, COLOR_BLUE);
    lua_setfield(L, -2, "blue");
    lua_pushinteger(L, COLOR_MAGENTA);
    lua_setfield(L, -2, "magenta");
    lua_pushinteger(L, COLOR_CYAN);
    lua_setfield(L, -2, "cyan");
    lua_pushinteger(L, COLOR_WHITE);
    lua_setfield(L, -2, "white");

    ncolors = 8;
}

static pos get_pos(lua_State* L)
{
    pos ret;

    getyx(stdscr, ret.y, ret.x);

    lua_getfield(L, 1, "x");
    if (lua_isnumber(L, -1)) {
        ret.x = lua_tonumber(L, -1);
    }
    lua_pop(L, 1);

    lua_getfield(L, 1, "y");
    if (lua_isnumber(L, -1)) {
        ret.y = lua_tonumber(L, -1);
    }
    lua_pop(L, 1);

    return ret;
}

static int l_initscr(lua_State* L)
{
    lua_pushboolean(L, initscr() == OK);
    return 1;
}

static int l_start_color(lua_State* L)
{
    if (has_colors()) {
        lua_getfield(L, LUA_REGISTRYINDEX, REG_TABLE);
        lua_newtable(L);
        lua_setfield(L, -2, "color_pairs");
        lua_newtable(L);
        init_colors(L);
        lua_setfield(L, -2, "colors");
        lua_setfield(L, LUA_REGISTRYINDEX, REG_TABLE);
        lua_pushboolean(L, start_color() == OK);
    }
    else {
        lua_pushboolean(L, FALSE);
    }

    return 1;
}

static int l_setup_term(lua_State* L)
{
    int ret = 0;

    luaL_checktype(L, 1, LUA_TTABLE);

    lua_pushnil(L);
    while (lua_next(L, 1) != 0) {
        if (lua_isstring(L, -2)) {
            const char* str;

            str = lua_tostring(L, -2);
            /* XXX: this certainly needs expansion */
            if (!strcmp(str, "nl")) {
                ret += ((lua_toboolean(L, -1) ? nl() : nonl()) == OK);
            }
            else if (!strcmp(str, "cbreak")) {
                ret += ((lua_toboolean(L, -1) ? cbreak() : nocbreak()) == OK);
            }
            else if (!strcmp(str, "echo")) {
                ret += ((lua_toboolean(L, -1) ? echo() : noecho()) == OK);
            }
            else if (!strcmp(str, "keypad")) {
                ret += (keypad(stdscr, lua_toboolean(L, -1)) == OK);
            }
            else if (!strcmp(str, "scroll")) {
                ret += (scrollok(stdscr, lua_toboolean(L, -1)) == OK);
            }
            else {
                luaL_error(L, "Unknown or unimplemented terminal mode %s", str);
            }
        }
        lua_pop(L, 1);
    }

    lua_pushnumber(L, ret);
    return 1;
}

static int l_init_color(lua_State* L)
{
    /* test can_change_color here */
    return 0;
}

static int l_init_pair(lua_State* L)
{
    const char *name, *fg, *bg;
    int name_val, fg_val, bg_val;

    /* check the arguments, and get them */
    name = luaL_checklstring(L, 1, NULL);
    fg =   luaL_optlstring(L, 2, "white", NULL);
    bg =   luaL_optlstring(L, 3, "black", NULL);

    lua_getfield(L, LUA_REGISTRYINDEX, REG_TABLE);

    /* figure out which pair value to use */
    lua_getfield(L, -1, "color_pairs");
    lua_getfield(L, -1, name);
    if (lua_isnil(L, -1)) {
        /* if it was nil, we want to set a new value in the color_pairs table,
         * and we want to leave that C color_pair value on top of the stack
         * for consistency */
        lua_pop(L, 1);
        lua_pushinteger(L, ncolor_pairs++);
        lua_pushvalue(L, -1);
        lua_setfield(L, -3, name);
    }
    name_val = lua_tointeger(L, -1);
    lua_pop(L, 2);

    /* figure out which foreground value to use */
    lua_getfield(L, -1, "colors");
    lua_getfield(L, -1, fg);
    if (lua_isnil(L, -1)) {
        return luaL_error(L, "init_pair: Trying to use a non-existant foreground color");
    }
    fg_val = lua_tointeger(L, -1);
    lua_pop(L, 1);

    /* and background value */
    lua_getfield(L, -1, bg);
    if (lua_isnil(L, -1)) {
        return luaL_error(L, "init_pair: Trying to use a non-existant background color");
    }
    bg_val = lua_tointeger(L, -1);
    lua_pop(L, 3);

    lua_pushboolean(L, (init_pair(name_val, fg_val, bg_val) == OK));
    return 1;
}

static int l_getch(lua_State* L)
{
    int c;
    
    if (lua_istable(L, 1)) {
        pos p;

        p = get_pos(L);
        c = mvgetch(p.y, p.x);
    }
    else {
        c = getch();
    }
    if (c == ERR) {
        lua_pushboolean(L, 0);
        return 1;
    }

    switch (c) {
        case KEY_LEFT:
            lua_pushstring(L, "left");
        break;
        case KEY_RIGHT:
            lua_pushstring(L, "right");
        break;
        case KEY_UP:
            lua_pushstring(L, "up");
        break;
        case KEY_DOWN:
            lua_pushstring(L, "down");
        break;
        case KEY_HOME:
            lua_pushstring(L, "home");
        break;
        case KEY_END:
            lua_pushstring(L, "end");
        break;
        case KEY_BACKSPACE:
            lua_pushstring(L, "backspace");
        break;
        case KEY_ENTER:
            lua_pushstring(L, "enter");
        break;
        case KEY_NPAGE:
            lua_pushstring(L, "page down");
        break;
        case KEY_PPAGE:
            lua_pushstring(L, "page up");
        break;
        default:
            if (c >= KEY_F(1) && c <= KEY_F(64)) {
                lua_pushfstring(L, "F%d", c - KEY_F0);
            }
            else {
                char s[1];

                s[0] = c;
                lua_pushlstring(L, s, 1);
            }
        break;
    }

    return 1;
}

static int l_move(lua_State* L)
{
    if (lua_istable(L, 1)) {
        pos p;

        p = get_pos(L);
        lua_pushboolean(L, (move(p.y, p.x) == OK));
    }
    else {
        int x, y;

        y = luaL_checkinteger(L, 1);
        x = luaL_checkinteger(L, 2);

        lua_pushboolean(L, (move(y, x) == OK));
    }

    return 1;
}

static int l_addstr(lua_State* L)
{
    if (lua_istable(L, 1)) {
        pos p;
        size_t l;
        const char* str;

        p = get_pos(L);
        str = luaL_checklstring(L, 2, &l);
        if (l == 1) {
            lua_pushboolean(L, mvaddch(p.y, p.x, *str) == OK);
        }
        else {
            lua_pushboolean(L, mvaddstr(p.y, p.x, str) == OK);
        }
    }
    else {
        size_t l;
        const char* str;

        str = luaL_checklstring(L, 1, &l);
        if (l == 1) {
            lua_pushboolean(L, addch(*str) == OK);
        }
        else {
            lua_pushboolean(L, addstr(str) == OK);
        }
    }

    return 1;
}

static int l_refresh(lua_State* L)
{
    lua_pushboolean(L, (refresh() == OK));
    return 1;
}

static int l_getmaxyx(lua_State* L)
{
    int x, y;

    getmaxyx(stdscr, y, x);

    lua_pushnumber(L, y);
    lua_pushnumber(L, x);
    return 2;
}

static int l_getyx(lua_State* L)
{
    int x, y;

    getyx(stdscr, y, x);

    lua_pushnumber(L, y);
    lua_pushnumber(L, x);
    return 2;
}

static int l_colors(lua_State* L)
{
    lua_pushinteger(L, COLORS);
    return 1;
}

static int l_color_pairs(lua_State* L)
{
    lua_pushinteger(L, COLOR_PAIRS);
    return 1;
}

const luaL_Reg reg[] = {
    { "initscr", l_initscr },
    { "start_color", l_start_color },
    { "setup_term", l_setup_term },
    { "init_color", l_init_color },
    { "init_pair", l_init_pair },
    { "getch", l_getch },
    { "move", l_move },
    { "addstr", l_addstr },
    { "refresh", l_refresh },
    { "getmaxyx", l_getmaxyx },
    { "getyx", l_getyx },
    { "colors", l_colors },
    { "color_pairs", l_color_pairs },
    { NULL, NULL },
};

extern int luaopen_curses(lua_State* L)
{
    /* XXX: do we want to do this? how important is cleaning up? */
    signal(SIGTERM, exit);
    atexit(_endwin);

    lua_newtable(L);
    lua_setfield(L, LUA_REGISTRYINDEX, REG_TABLE);

    luaL_register(L, "curses", reg);

    return 1;
}