]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | * Ncurses bindings example. | |
3 | * | |
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. | |
7 | * | |
8 | * Debian: install libncurses5-dev | |
9 | */ | |
10 | ||
11 | #include <curses.h> | |
12 | #include "duktape.h" | |
13 | ||
14 | static int ncurses_initscr(duk_context *ctx) { | |
15 | WINDOW *win; | |
16 | ||
17 | win = initscr(); | |
18 | duk_push_pointer(ctx, (void *) win); | |
19 | return 1; | |
20 | } | |
21 | ||
22 | static int ncurses_endwin(duk_context *ctx) { | |
23 | int rc; | |
24 | ||
25 | rc = endwin(); | |
26 | duk_push_int(ctx, rc); | |
27 | return 1; | |
28 | } | |
29 | ||
30 | static int ncurses_delscreen(duk_context *ctx) { | |
31 | /* XXX: no screen management now */ | |
32 | (void) ctx; | |
33 | return 0; | |
34 | } | |
35 | ||
36 | static int ncurses_getmaxyx(duk_context *ctx) { | |
37 | int row, col; | |
38 | ||
39 | getmaxyx(stdscr, row, col); | |
40 | ||
41 | duk_push_array(ctx); | |
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); | |
46 | return 1; | |
47 | } | |
48 | ||
49 | static int ncurses_printw(duk_context *ctx) { | |
50 | int rc; | |
51 | const char *str; | |
52 | ||
53 | str = duk_to_string(ctx, 0); | |
54 | rc = printw("%s", str); | |
55 | duk_push_int(ctx, rc); | |
56 | return 1; | |
57 | } | |
58 | ||
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); | |
63 | int rc; | |
64 | ||
65 | rc = mvprintw(y, x, "%s", str); | |
66 | duk_push_int(ctx, rc); | |
67 | return 1; | |
68 | } | |
69 | ||
70 | static int ncurses_refresh(duk_context *ctx) { | |
71 | int rc; | |
72 | ||
73 | rc = refresh(); | |
74 | duk_push_int(ctx, rc); | |
75 | return 1; | |
76 | } | |
77 | ||
78 | static int ncurses_getch(duk_context *ctx) { | |
79 | int rc; | |
80 | ||
81 | rc = getch(); | |
82 | duk_push_int(ctx, rc); | |
83 | return 1; | |
84 | } | |
85 | ||
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 }, | |
95 | { NULL, NULL, 0 } | |
96 | }; | |
97 | ||
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"); | |
104 | duk_pop(ctx); | |
105 | } |