about summary refs log tree commit diff stats
path: root/src/lcurseslib.c
blob: 441a76c85b628e45027a22eea56c36aaaa814834 (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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include <ncurses.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "lua.h"
#include "llimits.h"

#include "lauxlib.h"
#include "lualib.h"

void cleanup_curses (void) {
  if (!isendwin()) {
    wclear(stdscr);
    wrefresh(stdscr);
    endwin();
  }
}


int menu_column = 0;
void draw_string_on_menu (const char* s) {
  mvaddstr(LINES-1, menu_column, " ");
  ++menu_column;
  mvaddstr(LINES-1, menu_column, s);
  menu_column += strlen(s);
  mvaddstr(LINES-1, menu_column, " ");
  ++menu_column;
}
void draw_menu_item (const char* key, const char* name) {
  attroff(A_REVERSE);
  draw_string_on_menu(key);
  attron(A_REVERSE);
  draw_string_on_menu(name);
}

void draw_menu (lua_State *L) {
  attron(A_BOLD|A_REVERSE);
  for (int x = 0; x < COLS; ++x)
    mvaddch(LINES-1, x, ' ');
  menu_column = 2;
  draw_menu_item("^x", "exit");
  draw_menu_item("^e", "edit");

  /* render any app-specific items */
  lua_getglobal(L, "menu");
  int table = lua_gettop(L);
  if (lua_istable(L, -1))
    for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1))
      draw_menu_item(lua_tostring(L, -2), lua_tostring(L, -1));

  lua_pop(L, 1);
  attroff(A_BOLD|A_REVERSE);
}


static int Prefresh (lua_State *L) {
  refresh();
  draw_menu(L);
  return 1;
}


static int argtypeerror (lua_State *L, int narg, const char *expected) {
  const char *got = luaL_typename(L, narg);
  return luaL_argerror(L, narg,
          lua_pushfstring(L, "%s expected, got %s", expected, got));
}


static void checktype (lua_State *L, int narg, int t, const char *expected) {
  if (lua_type(L, narg) != t)
    argtypeerror(L, narg, expected);
}


static lua_Integer checkinteger (lua_State *L, int narg, const char *expected) {
  lua_Integer d = lua_tointeger(L, narg);
  if (d == 0 && !lua_isnumber(L, narg))
    argtypeerror(L, narg, expected);
  return d;
}


static int Pstdscr (lua_State *L) {
  lua_pushstring(L, "curses:stdscr");
  lua_rawget(L, LUA_REGISTRYINDEX);
  return 1;
}


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


static int Pinit_pair (lua_State *L) {
  int pair = checkinteger(L, 1, "int");
  short f = checkinteger(L, 2, "int");
  short b = checkinteger(L, 3, "int");
  init_pair(pair, f, b);
  return 1;
}


static int Pcolor_pair (lua_State *L)
{
  int n = checkinteger(L, 1, "int");
  lua_pushinteger(L, COLOR_PAIR(n));
  return 1;
}


extern void switch_to_editor (lua_State *L, const char *message);
static int Pgetch (lua_State *L) {
  int c = wgetch(stdscr);
  if (c == ERR)
    return 0;
  if (c == 24)  /* ctrl-x */
    exit(0);
  if (c == 5)  /* ctrl-e */
    switch_to_editor(L, "");
  /* handle other standard menu hotkeys here */
  lua_pushinteger(L, c);
  return 1;
}


static const struct luaL_Reg curseslib [] = {
  {"color_pairs", Pcolor_pairs},
  {"color_pair", Pcolor_pair},
  {"getch", Pgetch},
  {"init_pair", Pinit_pair},
  {"refresh", Prefresh},
  {"stdscr", Pstdscr},
  {NULL, NULL}
};


static void curses_newwin (lua_State *L, WINDOW *nw) {
  if (nw) {
    WINDOW **w = lua_newuserdata(L, sizeof(WINDOW*));
    luaL_getmetatable(L, "curses:window");
    lua_setmetatable(L, -2);
    *w = nw;
  }
  else {
    lua_pushliteral(L, "failed to create window");
    lua_error(L);
  }
}


static WINDOW **lc_getwin (lua_State *L, int offset) {
  WINDOW **w = (WINDOW**)luaL_checkudata(L, offset, "curses:window");
  if (w == NULL)
    luaL_argerror(L, offset, "bad curses window");
  return w;
}


static WINDOW *checkwin (lua_State *L, int offset) {
  WINDOW **w = lc_getwin(L, offset);
  if (*w == NULL)
    luaL_argerror(L, offset, "attempt to use closed curses window");
  return *w;
}


static chtype checkch (lua_State *L, int narg) {
  if (lua_isnumber(L, narg))
    return cast(chtype, checkinteger(L, narg, "int"));
  if (lua_isstring(L, narg))
    return *lua_tostring(L, narg);

  return argtypeerror(L, narg, "int or char");
}


static int optint (lua_State *L, int narg, lua_Integer def) {
  if (lua_isnoneornil(L, narg))
    return cast(int, def);
  return cast(int, checkinteger(L, narg, "int or nil"));
}


static int W__tostring (lua_State *L) {
  WINDOW **w = lc_getwin(L, 1);
  char buff[34];
  if (*w == NULL)
    strcpy(buff, "closed");
  else
    sprintf(buff, "%p", lua_touserdata(L, 1));
  lua_pushfstring(L, "curses window (%s)", buff);
  return 1;
}


static int Waddstr (lua_State *L) {
  WINDOW *w = checkwin(L, 1);
  const char *str = luaL_checkstring(L, 2);
  int n = optint(L, 3, -1);
  lua_pushboolean(L, waddnstr(w, str, n));
  return 1;
}


static int Wattroff (lua_State *L) {
  WINDOW *w = checkwin(L, 1);
  int attrs = checkinteger(L, 2, "int");
  lua_pushboolean(L, wattroff(w, attrs));
  return 1;
}


static int Wattron (lua_State *L) {
  WINDOW *w = checkwin(L, 1);
  int attrs = checkinteger(L, 2, "int");
  lua_pushboolean(L, wattron(w, attrs));
  return 1;
}


static int Wclear (lua_State *L) {
  lua_pushboolean(L, wclear(checkwin(L, 1)));
  return 1;
}


static int Wgetyx (lua_State *L) {
  WINDOW *w = checkwin(L, 1);
  int y, x;
  getyx(w, y, x);
  lua_pushinteger(L, y);
  lua_pushinteger(L, x);
  return 2;
}


static int Wgetmaxyx (lua_State *L) {
  WINDOW *w = checkwin(L, 1);
  int y, x;
  getmaxyx(w, y, x);
  --y;  // set aside space for the menu bar
  lua_pushinteger(L, y);
  lua_pushinteger(L, x);
  return 2;
}


static int Wmvaddch (lua_State *L) {
  WINDOW *w = checkwin(L, 1);
  int y = checkinteger(L, 2, "int");
  int x = checkinteger(L, 3, "int");
  chtype ch = checkch(L, 4);
  mvwaddch(w, y, x, ch);
  return 1;
}


static int Wmvaddstr (lua_State *L) {
  WINDOW *w = checkwin(L, 1);
  int y = checkinteger(L, 2, "int");
  int x = checkinteger(L, 3, "int");
  const char *str = luaL_checkstring(L, 4);
  int n = optint(L, 5, -1);
  mvwaddnstr(w, y, x, str, n);
  return 1;
}


static int Wnodelay (lua_State *L) {
  WINDOW *w = checkwin(L, 1);
  checktype(L, 2, LUA_TBOOLEAN, "boolean or nil");
  int bf = (int)lua_toboolean(L, 2);
  lua_pushboolean(L, nodelay(w, bf));
  return 1;
}


static const luaL_Reg curses_window_methods[] =
{
  {"__tostring", W__tostring},
  {"addstr", Waddstr},
  {"attroff", Wattroff},
  {"attron", Wattron},
  {"clear", Wclear},
  {"getmaxyx", Wgetmaxyx},
  {"getyx", Wgetyx},
  {"mvaddch", Wmvaddch},
  {"mvaddstr", Wmvaddstr},
  {"nodelay", Wnodelay},
  {NULL, NULL}
};


static void register_curses_constant (lua_State *L, const char* name, int val) {
  lua_pushstring(L, name);
  lua_pushinteger(L, val);
  lua_settable(L, -3);
}


static void register_curses_constants (lua_State *L) {
#define CC(s) register_curses_constant(L, #s, s)
  /* colors */
  CC(COLOR_BLACK);
  CC(COLOR_RED);
  CC(COLOR_GREEN);
  CC(COLOR_YELLOW);
  CC(COLOR_BLUE);
  CC(COLOR_MAGENTA);
  CC(COLOR_CYAN);
  CC(COLOR_WHITE);

  /* alternate character set */
  CC(ACS_BLOCK);
  CC(ACS_BOARD);

  CC(ACS_BTEE);
  CC(ACS_TTEE);
  CC(ACS_LTEE);
  CC(ACS_RTEE);
  CC(ACS_LLCORNER);
  CC(ACS_LRCORNER);
  CC(ACS_URCORNER);
  CC(ACS_ULCORNER);

  CC(ACS_LARROW);
  CC(ACS_RARROW);
  CC(ACS_UARROW);
  CC(ACS_DARROW);

  CC(ACS_HLINE);
  CC(ACS_VLINE);

  CC(ACS_BULLET);
  CC(ACS_CKBOARD);
  CC(ACS_LANTERN);
  CC(ACS_DEGREE);
  CC(ACS_DIAMOND);

  CC(ACS_PLMINUS);
  CC(ACS_PLUS);
  CC(ACS_S1);
  CC(ACS_S9);

  /* attributes */
  CC(A_NORMAL);
  CC(A_STANDOUT);
  CC(A_UNDERLINE);
  CC(A_REVERSE);
  CC(A_BLINK);
  CC(A_DIM);
  CC(A_BOLD);
  CC(A_PROTECT);
  CC(A_INVIS);
  CC(A_ALTCHARSET);
  CC(A_CHARTEXT);
  CC(A_ATTRIBUTES);
  CC(A_COLOR);

  /* key functions */
  CC(KEY_BREAK);
  CC(KEY_DOWN);
  CC(KEY_UP);
  CC(KEY_LEFT);
  CC(KEY_RIGHT);
  CC(KEY_HOME);
  CC(KEY_BACKSPACE);

  CC(KEY_DL);
  CC(KEY_IL);
  CC(KEY_DC);
  CC(KEY_IC);
  CC(KEY_EIC);
  CC(KEY_CLEAR);
  CC(KEY_EOS);
  CC(KEY_EOL);
  CC(KEY_SF);
  CC(KEY_SR);
  CC(KEY_NPAGE);
  CC(KEY_PPAGE);
  CC(KEY_STAB);
  CC(KEY_CTAB);
  CC(KEY_CATAB);
  CC(KEY_ENTER);
  CC(KEY_SRESET);
  CC(KEY_RESET);
  CC(KEY_PRINT);
  CC(KEY_LL);
  CC(KEY_A1);
  CC(KEY_A3);
  CC(KEY_B2);
  CC(KEY_C1);
  CC(KEY_C3);
  CC(KEY_BTAB);
  CC(KEY_BEG);
  CC(KEY_CANCEL);
  CC(KEY_CLOSE);
  CC(KEY_COMMAND);
  CC(KEY_COPY);
  CC(KEY_CREATE);
  CC(KEY_END);
  CC(KEY_EXIT);
  CC(KEY_FIND);
  CC(KEY_HELP);
  CC(KEY_MARK);
  CC(KEY_MESSAGE); /* ncurses extension: CC(KEY_MOUSE); */
  CC(KEY_MOVE);
  CC(KEY_NEXT);
  CC(KEY_OPEN);
  CC(KEY_OPTIONS);
  CC(KEY_PREVIOUS);
  CC(KEY_REDO);
  CC(KEY_REFERENCE);
  CC(KEY_REFRESH);
  CC(KEY_REPLACE);
  CC(KEY_RESIZE);
  CC(KEY_RESTART);
  CC(KEY_RESUME);
  CC(KEY_SAVE);
  CC(KEY_SBEG);
  CC(KEY_SCANCEL);
  CC(KEY_SCOMMAND);
  CC(KEY_SCOPY);
  CC(KEY_SCREATE);
  CC(KEY_SDC);
  CC(KEY_SDL);
  CC(KEY_SELECT);
  CC(KEY_SEND);
  CC(KEY_SEOL);
  CC(KEY_SEXIT);
  CC(KEY_SFIND);
  CC(KEY_SHELP);
  CC(KEY_SHOME);
  CC(KEY_SIC);
  CC(KEY_SLEFT);
  CC(KEY_SMESSAGE);
  CC(KEY_SMOVE);
  CC(KEY_SNEXT);
  CC(KEY_SOPTIONS);
  CC(KEY_SPREVIOUS);
  CC(KEY_SPRINT);
  CC(KEY_SREDO);
  CC(KEY_SREPLACE);
  CC(KEY_SRIGHT);
  CC(KEY_SRSUME);
  CC(KEY_SSAVE);
  CC(KEY_SSUSPEND);
  CC(KEY_SUNDO);
  CC(KEY_SUSPEND);
  CC(KEY_UNDO);
#undef CC
}


LUALIB_API int luaopen_curses (lua_State *L) {
  luaL_newmetatable(L, "curses:window");

  /* metatable.__index = metatable */
  lua_pushstring(L, "__index");
  lua_pushvalue(L, -2);
  lua_settable(L, -3);

  luaL_register(L, NULL, curses_window_methods);

  luaL_register(L, "curses", curseslib);

  /* save main window on registry */
  curses_newwin(L, stdscr);
  lua_pushstring(L, "curses:stdscr");
  lua_pushvalue(L, -2);
  lua_rawset(L, LUA_REGISTRYINDEX);

  lua_pushvalue(L, -2);
  register_curses_constants(L);

  atexit(cleanup_curses);
  return 1;
}