1import sys
2if sys.platform == 'win32':
3    #This module does not exist in windows
4    raise ImportError('No module named _curses')
5
6from cffi import FFI
7
8ffi = FFI()
9
10ffi.cdef("""
11typedef ... WINDOW;
12typedef ... SCREEN;
13typedef unsigned long... mmask_t;
14typedef unsigned char bool;
15typedef unsigned long... chtype;
16typedef chtype attr_t;
17
18typedef struct
19{
20    short id;           /* ID to distinguish multiple devices */
21    int x, y, z;        /* event coordinates (character-cell) */
22    mmask_t bstate;     /* button state bits */
23}
24MEVENT;
25
26static const int ERR, OK;
27static const int TRUE, FALSE;
28static const int KEY_MIN, KEY_MAX;
29
30static const int COLOR_BLACK;
31static const int COLOR_RED;
32static const int COLOR_GREEN;
33static const int COLOR_YELLOW;
34static const int COLOR_BLUE;
35static const int COLOR_MAGENTA;
36static const int COLOR_CYAN;
37static const int COLOR_WHITE;
38
39static const chtype A_ATTRIBUTES;
40static const chtype A_NORMAL;
41static const chtype A_STANDOUT;
42static const chtype A_UNDERLINE;
43static const chtype A_REVERSE;
44static const chtype A_BLINK;
45static const chtype A_DIM;
46static const chtype A_BOLD;
47static const chtype A_ALTCHARSET;
48static const chtype A_INVIS;
49static const chtype A_PROTECT;
50static const chtype A_CHARTEXT;
51static const chtype A_COLOR;
52
53static const int BUTTON1_RELEASED;
54static const int BUTTON1_PRESSED;
55static const int BUTTON1_CLICKED;
56static const int BUTTON1_DOUBLE_CLICKED;
57static const int BUTTON1_TRIPLE_CLICKED;
58static const int BUTTON2_RELEASED;
59static const int BUTTON2_PRESSED;
60static const int BUTTON2_CLICKED;
61static const int BUTTON2_DOUBLE_CLICKED;
62static const int BUTTON2_TRIPLE_CLICKED;
63static const int BUTTON3_RELEASED;
64static const int BUTTON3_PRESSED;
65static const int BUTTON3_CLICKED;
66static const int BUTTON3_DOUBLE_CLICKED;
67static const int BUTTON3_TRIPLE_CLICKED;
68static const int BUTTON4_RELEASED;
69static const int BUTTON4_PRESSED;
70static const int BUTTON4_CLICKED;
71static const int BUTTON4_DOUBLE_CLICKED;
72static const int BUTTON4_TRIPLE_CLICKED;
73static const int BUTTON_SHIFT;
74static const int BUTTON_CTRL;
75static const int BUTTON_ALT;
76static const int ALL_MOUSE_EVENTS;
77static const int REPORT_MOUSE_POSITION;
78
79int setupterm(char *, int, int *);
80
81WINDOW *stdscr;
82int COLORS;
83int COLOR_PAIRS;
84int COLS;
85int LINES;
86
87int baudrate(void);
88int beep(void);
89int box(WINDOW *, chtype, chtype);
90bool can_change_color(void);
91int cbreak(void);
92int clearok(WINDOW *, bool);
93int color_content(short, short*, short*, short*);
94int copywin(const WINDOW*, WINDOW*, int, int, int, int, int, int, int);
95int curs_set(int);
96int def_prog_mode(void);
97int def_shell_mode(void);
98int delay_output(int);
99int delwin(WINDOW *);
100WINDOW * derwin(WINDOW *, int, int, int, int);
101int doupdate(void);
102int echo(void);
103int endwin(void);
104char erasechar(void);
105void filter(void);
106int flash(void);
107int flushinp(void);
108chtype getbkgd(WINDOW *);
109WINDOW * getwin(FILE *);
110int halfdelay(int);
111bool has_colors(void);
112bool has_ic(void);
113bool has_il(void);
114void idcok(WINDOW *, bool);
115int idlok(WINDOW *, bool);
116void immedok(WINDOW *, bool);
117WINDOW * initscr(void);
118int init_color(short, short, short, short);
119int init_pair(short, short, short);
120int intrflush(WINDOW *, bool);
121bool isendwin(void);
122bool is_linetouched(WINDOW *, int);
123bool is_wintouched(WINDOW *);
124const char * keyname(int);
125int keypad(WINDOW *, bool);
126char killchar(void);
127int leaveok(WINDOW *, bool);
128char * longname(void);
129int meta(WINDOW *, bool);
130int mvderwin(WINDOW *, int, int);
131int mvwaddch(WINDOW *, int, int, const chtype);
132int mvwaddnstr(WINDOW *, int, int, const char *, int);
133int mvwaddstr(WINDOW *, int, int, const char *);
134int mvwchgat(WINDOW *, int, int, int, attr_t, short, const void *);
135int mvwdelch(WINDOW *, int, int);
136int mvwgetch(WINDOW *, int, int);
137int mvwgetnstr(WINDOW *, int, int, char *, int);
138int mvwin(WINDOW *, int, int);
139chtype mvwinch(WINDOW *, int, int);
140int mvwinnstr(WINDOW *, int, int, char *, int);
141int mvwinsch(WINDOW *, int, int, chtype);
142int mvwinsnstr(WINDOW *, int, int, const char *, int);
143int mvwinsstr(WINDOW *, int, int, const char *);
144int napms(int);
145WINDOW * newpad(int, int);
146WINDOW * newwin(int, int, int, int);
147int nl(void);
148int nocbreak(void);
149int nodelay(WINDOW *, bool);
150int noecho(void);
151int nonl(void);
152void noqiflush(void);
153int noraw(void);
154int notimeout(WINDOW *, bool);
155int overlay(const WINDOW*, WINDOW *);
156int overwrite(const WINDOW*, WINDOW *);
157int pair_content(short, short*, short*);
158int pechochar(WINDOW *, const chtype);
159int pnoutrefresh(WINDOW*, int, int, int, int, int, int);
160int prefresh(WINDOW *, int, int, int, int, int, int);
161int putwin(WINDOW *, FILE *);
162void qiflush(void);
163int raw(void);
164int redrawwin(WINDOW *);
165int resetty(void);
166int reset_prog_mode(void);
167int reset_shell_mode(void);
168int savetty(void);
169int scroll(WINDOW *);
170int scrollok(WINDOW *, bool);
171int start_color(void);
172WINDOW * subpad(WINDOW *, int, int, int, int);
173WINDOW * subwin(WINDOW *, int, int, int, int);
174int syncok(WINDOW *, bool);
175chtype termattrs(void);
176char * termname(void);
177int touchline(WINDOW *, int, int);
178int touchwin(WINDOW *);
179int typeahead(int);
180int ungetch(int);
181int untouchwin(WINDOW *);
182void use_env(bool);
183int waddch(WINDOW *, const chtype);
184int waddnstr(WINDOW *, const char *, int);
185int waddstr(WINDOW *, const char *);
186int wattron(WINDOW *, int);
187int wattroff(WINDOW *, int);
188int wattrset(WINDOW *, int);
189int wbkgd(WINDOW *, chtype);
190void wbkgdset(WINDOW *, chtype);
191int wborder(WINDOW *, chtype, chtype, chtype, chtype,
192            chtype, chtype, chtype, chtype);
193int wchgat(WINDOW *, int, attr_t, short, const void *);
194int wclear(WINDOW *);
195int wclrtobot(WINDOW *);
196int wclrtoeol(WINDOW *);
197void wcursyncup(WINDOW *);
198int wdelch(WINDOW *);
199int wdeleteln(WINDOW *);
200int wechochar(WINDOW *, const chtype);
201int werase(WINDOW *);
202int wgetch(WINDOW *);
203int wgetnstr(WINDOW *, char *, int);
204int whline(WINDOW *, chtype, int);
205chtype winch(WINDOW *);
206int winnstr(WINDOW *, char *, int);
207int winsch(WINDOW *, chtype);
208int winsdelln(WINDOW *, int);
209int winsertln(WINDOW *);
210int winsnstr(WINDOW *, const char *, int);
211int winsstr(WINDOW *, const char *);
212int wmove(WINDOW *, int, int);
213int wresize(WINDOW *, int, int);
214int wnoutrefresh(WINDOW *);
215int wredrawln(WINDOW *, int, int);
216int wrefresh(WINDOW *);
217int wscrl(WINDOW *, int);
218int wsetscrreg(WINDOW *, int, int);
219int wstandout(WINDOW *);
220int wstandend(WINDOW *);
221void wsyncdown(WINDOW *);
222void wsyncup(WINDOW *);
223void wtimeout(WINDOW *, int);
224int wtouchln(WINDOW *, int, int, int);
225int wvline(WINDOW *, chtype, int);
226int tigetflag(char *);
227int tigetnum(char *);
228char * tigetstr(char *);
229int putp(const char *);
230char * tparm(const char *, ...);
231int getattrs(const WINDOW *);
232int getcurx(const WINDOW *);
233int getcury(const WINDOW *);
234int getbegx(const WINDOW *);
235int getbegy(const WINDOW *);
236int getmaxx(const WINDOW *);
237int getmaxy(const WINDOW *);
238int getparx(const WINDOW *);
239int getpary(const WINDOW *);
240
241int getmouse(MEVENT *);
242int ungetmouse(MEVENT *);
243mmask_t mousemask(mmask_t, mmask_t *);
244bool wenclose(const WINDOW *, int, int);
245int mouseinterval(int);
246
247void setsyx(int y, int x);
248const char *unctrl(chtype);
249int use_default_colors(void);
250
251int has_key(int);
252bool is_term_resized(int, int);
253
254#define _m_STRICT_SYSV_CURSES ...
255#define _m_NCURSES_MOUSE_VERSION ...
256#define _m_NetBSD ...
257int _m_ispad(WINDOW *);
258
259chtype acs_map[];
260
261// For _curses_panel:
262
263typedef ... PANEL;
264
265WINDOW *panel_window(const PANEL *);
266void update_panels(void);
267int hide_panel(PANEL *);
268int show_panel(PANEL *);
269int del_panel(PANEL *);
270int top_panel(PANEL *);
271int bottom_panel(PANEL *);
272PANEL *new_panel(WINDOW *);
273PANEL *panel_above(const PANEL *);
274PANEL *panel_below(const PANEL *);
275int set_panel_userptr(PANEL *, void *);
276const void *panel_userptr(const PANEL *);
277int move_panel(PANEL *, int, int);
278int replace_panel(PANEL *,WINDOW *);
279int panel_hidden(const PANEL *);
280
281void _m_getsyx(int *yx);
282""")
283
284
285ffi.set_source("_curses_cffi", """
286#ifdef __APPLE__
287/* the following define is necessary for OS X 10.6+; without it, the
288   Apple-supplied ncurses.h sets NCURSES_OPAQUE to 1, and then Python
289   can't get at the WINDOW flags field. */
290#define NCURSES_OPAQUE 0
291#endif
292
293#include <ncurses.h>
294#include <panel.h>
295#include <term.h>
296
297#if defined STRICT_SYSV_CURSES
298#define _m_STRICT_SYSV_CURSES TRUE
299#else
300#define _m_STRICT_SYSV_CURSES FALSE
301#endif
302
303#if defined NCURSES_MOUSE_VERSION
304#define _m_NCURSES_MOUSE_VERSION TRUE
305#else
306#define _m_NCURSES_MOUSE_VERSION FALSE
307#endif
308
309#if defined __NetBSD__
310#define _m_NetBSD TRUE
311#else
312#define _m_NetBSD FALSE
313#endif
314
315int _m_ispad(WINDOW *win) {
316    // <curses.h> may not have _flags (and possibly _ISPAD),
317    // but for now let's assume that <ncurses.h> always has it
318    return (win->_flags & _ISPAD);
319}
320
321void _m_getsyx(int *yx) {
322    getsyx(yx[0], yx[1]);
323}
324""", libraries=['ncurses', 'panel'])
325
326if __name__ == '__main__':
327    ffi.compile()
328