2 * Ncurses bindings example.
4 * VALGRIND NOTE: when you use ncurses, there seems to be no way to get a
5 * clean valgrind run. Even if ncurses state is properly shut down, there
6 * will still be some residual leaks.
8 * Debian: install libncurses5-dev
14 static int ncurses_initscr(duk_context
*ctx
) {
18 duk_push_pointer(ctx
, (void *) win
);
22 static int ncurses_endwin(duk_context
*ctx
) {
26 duk_push_int(ctx
, rc
);
30 static int ncurses_delscreen(duk_context
*ctx
) {
31 /* XXX: no screen management now */
36 static int ncurses_getmaxyx(duk_context
*ctx
) {
39 getmaxyx(stdscr
, row
, col
);
42 duk_push_int(ctx
, row
);
43 duk_put_prop_index(ctx
, -2, 0);
44 duk_push_int(ctx
, col
);
45 duk_put_prop_index(ctx
, -2, 1);
49 static int ncurses_printw(duk_context
*ctx
) {
53 str
= duk_to_string(ctx
, 0);
54 rc
= printw("%s", str
);
55 duk_push_int(ctx
, rc
);
59 static int ncurses_mvprintw(duk_context
*ctx
) {
60 int y
= duk_to_int(ctx
, 0);
61 int x
= duk_to_int(ctx
, 1);
62 const char *str
= duk_to_string(ctx
, 2);
65 rc
= mvprintw(y
, x
, "%s", str
);
66 duk_push_int(ctx
, rc
);
70 static int ncurses_refresh(duk_context
*ctx
) {
74 duk_push_int(ctx
, rc
);
78 static int ncurses_getch(duk_context
*ctx
) {
82 duk_push_int(ctx
, rc
);
86 static duk_function_list_entry ncurses_funcs
[] = {
87 { "initscr", ncurses_initscr
, 0 },
88 { "endwin", ncurses_endwin
, 0 },
89 { "delscreen", ncurses_delscreen
, 0 },
90 { "getmaxyx", ncurses_getmaxyx
, 0 },
91 { "printw", ncurses_printw
, 1 },
92 { "mvprintw", ncurses_mvprintw
, 3 },
93 { "refresh", ncurses_refresh
, 0 },
94 { "getch", ncurses_getch
, 0 },
98 void ncurses_register(duk_context
*ctx
) {
99 /* Set global 'Ncurses'. */
100 duk_push_global_object(ctx
);
101 duk_push_object(ctx
);
102 duk_put_function_list(ctx
, -1, ncurses_funcs
);
103 duk_put_prop_string(ctx
, -2, "Ncurses");