Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
duktape-1.8.0/examples/eventloop/ncurses.c
Go to the documentation of this file.
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
14static 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
22static int ncurses_endwin(duk_context *ctx) {
23 int rc;
24
25 rc = endwin();
26 duk_push_int(ctx, rc);
27 return 1;
28}
29
31 /* XXX: no screen management now */
32 (void) ctx;
33 return 0;
34}
35
36static 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
49static 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
59static 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
70static int ncurses_refresh(duk_context *ctx) {
71 int rc;
72
73 rc = refresh();
74 duk_push_int(ctx, rc);
75 return 1;
76}
77
78static int ncurses_getch(duk_context *ctx) {
79 int rc;
80
81 rc = getch();
82 duk_push_int(ctx, rc);
83 return 1;
84}
85
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
99 /* Set global 'Ncurses'. */
101 duk_push_object(ctx);
103 duk_put_prop_string(ctx, -2, "Ncurses");
104 duk_pop(ctx);
105}
DUK_EXTERNAL duk_bool_t duk_put_prop_index(duk_context *ctx, duk_idx_t obj_idx, duk_uarridx_t arr_idx)
DUK_EXTERNAL duk_bool_t duk_put_prop_string(duk_context *ctx, duk_idx_t obj_idx, const char *key)
DUK_EXTERNAL void duk_push_int(duk_context *ctx, duk_int_t val)
DUK_EXTERNAL void duk_push_pointer(duk_context *ctx, void *val)
DUK_EXTERNAL duk_idx_t duk_push_object(duk_context *ctx)
DUK_EXTERNAL void duk_push_global_object(duk_context *ctx)
DUK_EXTERNAL void duk_put_function_list(duk_context *ctx, duk_idx_t obj_index, const duk_function_list_entry *funcs)
DUK_EXTERNAL const char * duk_to_string(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_pop(duk_context *ctx)
DUK_EXTERNAL duk_idx_t duk_push_array(duk_context *ctx)
DUK_EXTERNAL duk_int_t duk_to_int(duk_context *ctx, duk_idx_t index)
static int ncurses_mvprintw(duk_context *ctx)
static int ncurses_initscr(duk_context *ctx)
static int ncurses_getch(duk_context *ctx)
static int ncurses_getmaxyx(duk_context *ctx)
static duk_function_list_entry ncurses_funcs[]
static int ncurses_endwin(duk_context *ctx)
static int ncurses_delscreen(duk_context *ctx)
static int ncurses_refresh(duk_context *ctx)
static int ncurses_printw(duk_context *ctx)
void ncurses_register(duk_context *ctx)
#define NULL
Definition gmacros.h:924