| 1 |
from cffi import FFI, VerificationError |
| 2 |
import os |
| 3 |
import sys |
| 4 |
|
| 5 |
version_str = ''' |
| 6 |
static const int NCURSES_VERSION_MAJOR; |
| 7 |
static const int NCURSES_VERSION_MINOR; |
| 8 |
''' |
| 9 |
|
| 10 |
def find_library(options): |
| 11 |
for library in options: |
| 12 |
ffi = FFI() |
| 13 |
ffi.cdef(version_str) |
| 14 |
ffi.set_source("_curses_cffi_check", version_str, libraries=[library]) |
| 15 |
try: |
| 16 |
|
| 17 |
ffi.compile(verbose=1) |
| 18 |
except (VerificationError) as e: |
| 19 |
e_last = e |
| 20 |
continue |
| 21 |
else: |
| 22 |
return library |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
raise e_last |
| 27 |
|
| 28 |
def find_curses_dir_and_name(): |
| 29 |
for base in ('/usr', '/usr/local'): |
| 30 |
if os.path.exists(os.path.join(base, 'include', 'ncursesw')): |
| 31 |
return base, 'ncursesw' |
| 32 |
if os.path.exists(os.path.join(base, 'include', 'ncurses')): |
| 33 |
return base, 'ncurses' |
| 34 |
if sys.platform == 'darwin': |
| 35 |
return '', None |
| 36 |
if os.path.exists(os.path.join(base, 'lib', 'libncursesw.so')): |
| 37 |
return base, 'ncursesw' |
| 38 |
if os.path.exists(os.path.join(base, 'lib', 'libncurses.so')): |
| 39 |
return base, 'ncurses' |
| 40 |
return '', None |
| 41 |
|
| 42 |
base, name = find_curses_dir_and_name() |
| 43 |
if base: |
| 44 |
include_dirs = [os.path.join(base, 'include', name)] |
| 45 |
library_dirs = [os.path.join(base, 'lib')] |
| 46 |
libs = [name, name.replace('ncurses', 'panel')] |
| 47 |
print('using {} from {}'.format(name, base)) |
| 48 |
else: |
| 49 |
include_dirs = [] |
| 50 |
library_dirs = [] |
| 51 |
libs = [find_library(['ncursesw', 'ncurses']), |
| 52 |
find_library(['panelw', 'panel']), |
| 53 |
] |
| 54 |
print('using {} from general compiler paths'.format(libs[0])) |
| 55 |
|
| 56 |
ffi = FFI() |
| 57 |
ffi.set_source("_curses_cffi", """ |
| 58 |
#ifdef __APPLE__ |
| 59 |
/* the following define is necessary for OS X 10.6+; without it, the |
| 60 |
Apple-supplied ncurses.h sets NCURSES_OPAQUE to 1, and then Python |
| 61 |
can't get at the WINDOW flags field. */ |
| 62 |
#define NCURSES_OPAQUE 0 |
| 63 |
#endif |
| 64 |
|
| 65 |
/* explicitly opt into this, rather than relying on _XOPEN_SOURCE */ |
| 66 |
#define NCURSES_WIDECHAR 1 |
| 67 |
|
| 68 |
/* ncurses 6 change behaviour and makes all pointers opaque, |
| 69 |
lets define backward compatibility. It doesn't harm |
| 70 |
previous versions */ |
| 71 |
|
| 72 |
#define NCURSES_INTERNALS 1 |
| 73 |
#define NCURSES_REENTRANT 0 |
| 74 |
#include <ncurses.h> |
| 75 |
#include <panel.h> |
| 76 |
#include <term.h> |
| 77 |
|
| 78 |
#if defined STRICT_SYSV_CURSES |
| 79 |
#define _m_STRICT_SYSV_CURSES TRUE |
| 80 |
#else |
| 81 |
#define _m_STRICT_SYSV_CURSES FALSE |
| 82 |
#endif |
| 83 |
|
| 84 |
#if defined NCURSES_MOUSE_VERSION |
| 85 |
#define _m_NCURSES_MOUSE_VERSION TRUE |
| 86 |
#else |
| 87 |
#define _m_NCURSES_MOUSE_VERSION FALSE |
| 88 |
#endif |
| 89 |
|
| 90 |
#if defined __NetBSD__ |
| 91 |
#define _m_NetBSD TRUE |
| 92 |
#else |
| 93 |
#define _m_NetBSD FALSE |
| 94 |
#endif |
| 95 |
|
| 96 |
int _m_ispad(WINDOW *win) { |
| 97 |
// <curses.h> may not have _flags (and possibly _ISPAD), |
| 98 |
// but for now let's assume that <ncurses.h> always has it |
| 99 |
return (win->_flags & _ISPAD); |
| 100 |
} |
| 101 |
|
| 102 |
void _m_getsyx(int *yx) { |
| 103 |
getsyx(yx[0], yx[1]); |
| 104 |
} |
| 105 |
#ifndef NCURSES_EXT_COLORS |
| 106 |
static const int NCURSES_EXT_COLORS = 0; |
| 107 |
#endif |
| 108 |
#if NCURSES_EXT_FUNCS + 0 < 20170401 |
| 109 |
int (*init_extended_pair)(int pair, int f, int b) = NULL; |
| 110 |
int (*init_extended_color)(int color, int r, int g, int b) = NULL; |
| 111 |
int (*extended_color_content)(int, int*, int*, int*) = NULL; |
| 112 |
int (*extended_pair_content)(int, int*, int*) = NULL; |
| 113 |
#endif |
| 114 |
|
| 115 |
|
| 116 |
""", libraries=libs, |
| 117 |
library_dirs = library_dirs, |
| 118 |
include_dirs=include_dirs, |
| 119 |
) |
| 120 |
|
| 121 |
|
| 122 |
ffi.cdef(""" |
| 123 |
static const int NCURSES_EXT_FUNCS, NCURSES_EXT_COLORS; |
| 124 |
typedef ... WINDOW; |
| 125 |
typedef ... SCREEN; |
| 126 |
typedef unsigned long... mmask_t; |
| 127 |
typedef unsigned char bool; |
| 128 |
typedef unsigned long... chtype; |
| 129 |
typedef chtype attr_t; |
| 130 |
|
| 131 |
typedef struct |
| 132 |
{ |
| 133 |
short id; /* ID to distinguish multiple devices */ |
| 134 |
int x, y, z; /* event coordinates (character-cell) */ |
| 135 |
mmask_t bstate; /* button state bits */ |
| 136 |
} |
| 137 |
MEVENT; |
| 138 |
|
| 139 |
static const int ERR, OK; |
| 140 |
static const int TRUE, FALSE; |
| 141 |
static const int KEY_MIN, KEY_MAX; |
| 142 |
static const int KEY_CODE_YES; |
| 143 |
|
| 144 |
static const int COLOR_BLACK; |
| 145 |
static const int COLOR_RED; |
| 146 |
static const int COLOR_GREEN; |
| 147 |
static const int COLOR_YELLOW; |
| 148 |
static const int COLOR_BLUE; |
| 149 |
static const int COLOR_MAGENTA; |
| 150 |
static const int COLOR_CYAN; |
| 151 |
static const int COLOR_WHITE; |
| 152 |
|
| 153 |
static const chtype A_ATTRIBUTES; |
| 154 |
static const chtype A_NORMAL; |
| 155 |
static const chtype A_STANDOUT; |
| 156 |
static const chtype A_UNDERLINE; |
| 157 |
static const chtype A_REVERSE; |
| 158 |
static const chtype A_BLINK; |
| 159 |
static const chtype A_DIM; |
| 160 |
static const chtype A_BOLD; |
| 161 |
static const chtype A_ALTCHARSET; |
| 162 |
static const chtype A_INVIS; |
| 163 |
static const chtype A_PROTECT; |
| 164 |
static const chtype A_CHARTEXT; |
| 165 |
static const chtype A_COLOR; |
| 166 |
|
| 167 |
static const chtype A_HORIZONTAL; |
| 168 |
static const chtype A_LEFT; |
| 169 |
static const chtype A_LOW; |
| 170 |
static const chtype A_RIGHT; |
| 171 |
static const chtype A_TOP; |
| 172 |
static const chtype A_VERTICAL; |
| 173 |
|
| 174 |
static const int BUTTON1_RELEASED; |
| 175 |
static const int BUTTON1_PRESSED; |
| 176 |
static const int BUTTON1_CLICKED; |
| 177 |
static const int BUTTON1_DOUBLE_CLICKED; |
| 178 |
static const int BUTTON1_TRIPLE_CLICKED; |
| 179 |
static const int BUTTON2_RELEASED; |
| 180 |
static const int BUTTON2_PRESSED; |
| 181 |
static const int BUTTON2_CLICKED; |
| 182 |
static const int BUTTON2_DOUBLE_CLICKED; |
| 183 |
static const int BUTTON2_TRIPLE_CLICKED; |
| 184 |
static const int BUTTON3_RELEASED; |
| 185 |
static const int BUTTON3_PRESSED; |
| 186 |
static const int BUTTON3_CLICKED; |
| 187 |
static const int BUTTON3_DOUBLE_CLICKED; |
| 188 |
static const int BUTTON3_TRIPLE_CLICKED; |
| 189 |
static const int BUTTON4_RELEASED; |
| 190 |
static const int BUTTON4_PRESSED; |
| 191 |
static const int BUTTON4_CLICKED; |
| 192 |
static const int BUTTON4_DOUBLE_CLICKED; |
| 193 |
static const int BUTTON4_TRIPLE_CLICKED; |
| 194 |
static const int BUTTON_SHIFT; |
| 195 |
static const int BUTTON_CTRL; |
| 196 |
static const int BUTTON_ALT; |
| 197 |
static const int ALL_MOUSE_EVENTS; |
| 198 |
static const int REPORT_MOUSE_POSITION; |
| 199 |
|
| 200 |
int setupterm(char *, int, int *); |
| 201 |
|
| 202 |
extern WINDOW *stdscr; |
| 203 |
extern int COLORS; |
| 204 |
extern int COLOR_PAIRS; |
| 205 |
extern int COLS; |
| 206 |
extern int LINES; |
| 207 |
|
| 208 |
int baudrate(void); |
| 209 |
int beep(void); |
| 210 |
int box(WINDOW *, chtype, chtype); |
| 211 |
bool can_change_color(void); |
| 212 |
int cbreak(void); |
| 213 |
int clearok(WINDOW *, bool); |
| 214 |
int color_content(short, short*, short*, short*); |
| 215 |
int copywin(const WINDOW*, WINDOW*, int, int, int, int, int, int, int); |
| 216 |
int curs_set(int); |
| 217 |
int def_prog_mode(void); |
| 218 |
int def_shell_mode(void); |
| 219 |
int delay_output(int); |
| 220 |
int delwin(WINDOW *); |
| 221 |
WINDOW * derwin(WINDOW *, int, int, int, int); |
| 222 |
int doupdate(void); |
| 223 |
int echo(void); |
| 224 |
int endwin(void); |
| 225 |
char erasechar(void); |
| 226 |
int extended_color_content(int, int*, int*, int*); |
| 227 |
int extended_pair_content(int, int*, int*); |
| 228 |
void filter(void); |
| 229 |
int flash(void); |
| 230 |
int flushinp(void); |
| 231 |
chtype getbkgd(WINDOW *); |
| 232 |
WINDOW * getwin(FILE *); |
| 233 |
int halfdelay(int); |
| 234 |
bool has_colors(void); |
| 235 |
bool has_ic(void); |
| 236 |
bool has_il(void); |
| 237 |
void idcok(WINDOW *, bool); |
| 238 |
int idlok(WINDOW *, bool); |
| 239 |
void immedok(WINDOW *, bool); |
| 240 |
WINDOW * initscr(void); |
| 241 |
int init_color(short, short, short, short); |
| 242 |
int init_pair(short, short, short); |
| 243 |
int init_extended_pair(int pair, int f, int b); |
| 244 |
int init_extended_color(int color, int r, int g, int b); |
| 245 |
int intrflush(WINDOW *, bool); |
| 246 |
bool isendwin(void); |
| 247 |
bool is_linetouched(WINDOW *, int); |
| 248 |
bool is_wintouched(WINDOW *); |
| 249 |
const char * keyname(int); |
| 250 |
int keypad(WINDOW *, bool); |
| 251 |
char killchar(void); |
| 252 |
int leaveok(WINDOW *, bool); |
| 253 |
char * longname(void); |
| 254 |
int meta(WINDOW *, bool); |
| 255 |
int mvderwin(WINDOW *, int, int); |
| 256 |
int mvwaddch(WINDOW *, int, int, const chtype); |
| 257 |
int mvwaddnstr(WINDOW *, int, int, const char *, int); |
| 258 |
int mvwaddstr(WINDOW *, int, int, const char *); |
| 259 |
int mvwchgat(WINDOW *, int, int, int, attr_t, short, const void *); |
| 260 |
int mvwdelch(WINDOW *, int, int); |
| 261 |
int mvwgetch(WINDOW *, int, int); |
| 262 |
int mvwgetnstr(WINDOW *, int, int, char *, int); |
| 263 |
int mvwin(WINDOW *, int, int); |
| 264 |
chtype mvwinch(WINDOW *, int, int); |
| 265 |
int mvwinnstr(WINDOW *, int, int, char *, int); |
| 266 |
int mvwinsch(WINDOW *, int, int, chtype); |
| 267 |
int mvwinsnstr(WINDOW *, int, int, const char *, int); |
| 268 |
int mvwinsstr(WINDOW *, int, int, const char *); |
| 269 |
int napms(int); |
| 270 |
WINDOW * newpad(int, int); |
| 271 |
WINDOW * newwin(int, int, int, int); |
| 272 |
int nl(void); |
| 273 |
int nocbreak(void); |
| 274 |
int nodelay(WINDOW *, bool); |
| 275 |
int noecho(void); |
| 276 |
int nonl(void); |
| 277 |
void noqiflush(void); |
| 278 |
int noraw(void); |
| 279 |
int notimeout(WINDOW *, bool); |
| 280 |
int overlay(const WINDOW*, WINDOW *); |
| 281 |
int overwrite(const WINDOW*, WINDOW *); |
| 282 |
int pair_content(short, short*, short*); |
| 283 |
int pechochar(WINDOW *, const chtype); |
| 284 |
int pnoutrefresh(WINDOW*, int, int, int, int, int, int); |
| 285 |
int prefresh(WINDOW *, int, int, int, int, int, int); |
| 286 |
int putwin(WINDOW *, FILE *); |
| 287 |
void qiflush(void); |
| 288 |
int raw(void); |
| 289 |
int redrawwin(WINDOW *); |
| 290 |
int resetty(void); |
| 291 |
int reset_prog_mode(void); |
| 292 |
int reset_shell_mode(void); |
| 293 |
int resizeterm(int, int); |
| 294 |
int resize_term(int, int); |
| 295 |
int savetty(void); |
| 296 |
int scroll(WINDOW *); |
| 297 |
int scrollok(WINDOW *, bool); |
| 298 |
int start_color(void); |
| 299 |
WINDOW * subpad(WINDOW *, int, int, int, int); |
| 300 |
WINDOW * subwin(WINDOW *, int, int, int, int); |
| 301 |
int syncok(WINDOW *, bool); |
| 302 |
chtype termattrs(void); |
| 303 |
char * termname(void); |
| 304 |
int touchline(WINDOW *, int, int); |
| 305 |
int touchwin(WINDOW *); |
| 306 |
int typeahead(int); |
| 307 |
int ungetch(int); |
| 308 |
int untouchwin(WINDOW *); |
| 309 |
void use_env(bool); |
| 310 |
int waddch(WINDOW *, const chtype); |
| 311 |
int waddnstr(WINDOW *, const char *, int); |
| 312 |
int waddstr(WINDOW *, const char *); |
| 313 |
int wattron(WINDOW *, int); |
| 314 |
int wattroff(WINDOW *, int); |
| 315 |
int wattrset(WINDOW *, int); |
| 316 |
int wbkgd(WINDOW *, chtype); |
| 317 |
void wbkgdset(WINDOW *, chtype); |
| 318 |
int wborder(WINDOW *, chtype, chtype, chtype, chtype, |
| 319 |
chtype, chtype, chtype, chtype); |
| 320 |
int wchgat(WINDOW *, int, attr_t, short, const void *); |
| 321 |
int wclear(WINDOW *); |
| 322 |
int wclrtobot(WINDOW *); |
| 323 |
int wclrtoeol(WINDOW *); |
| 324 |
void wcursyncup(WINDOW *); |
| 325 |
int wdelch(WINDOW *); |
| 326 |
int wdeleteln(WINDOW *); |
| 327 |
int wechochar(WINDOW *, const chtype); |
| 328 |
int werase(WINDOW *); |
| 329 |
int wgetch(WINDOW *); |
| 330 |
int wgetnstr(WINDOW *, char *, int); |
| 331 |
int whline(WINDOW *, chtype, int); |
| 332 |
chtype winch(WINDOW *); |
| 333 |
int winnstr(WINDOW *, char *, int); |
| 334 |
int winsch(WINDOW *, chtype); |
| 335 |
int winsdelln(WINDOW *, int); |
| 336 |
int winsertln(WINDOW *); |
| 337 |
int winsnstr(WINDOW *, const char *, int); |
| 338 |
int winsstr(WINDOW *, const char *); |
| 339 |
int wmove(WINDOW *, int, int); |
| 340 |
int wresize(WINDOW *, int, int); |
| 341 |
int wnoutrefresh(WINDOW *); |
| 342 |
int wredrawln(WINDOW *, int, int); |
| 343 |
int wrefresh(WINDOW *); |
| 344 |
int wscrl(WINDOW *, int); |
| 345 |
int wsetscrreg(WINDOW *, int, int); |
| 346 |
int wstandout(WINDOW *); |
| 347 |
int wstandend(WINDOW *); |
| 348 |
void wsyncdown(WINDOW *); |
| 349 |
void wsyncup(WINDOW *); |
| 350 |
void wtimeout(WINDOW *, int); |
| 351 |
int wtouchln(WINDOW *, int, int, int); |
| 352 |
int wvline(WINDOW *, chtype, int); |
| 353 |
int tigetflag(char *); |
| 354 |
int tigetnum(char *); |
| 355 |
char * tigetstr(char *); |
| 356 |
int putp(const char *); |
| 357 |
int getattrs(const WINDOW *); |
| 358 |
int getcurx(const WINDOW *); |
| 359 |
int getcury(const WINDOW *); |
| 360 |
int getbegx(const WINDOW *); |
| 361 |
int getbegy(const WINDOW *); |
| 362 |
int getmaxx(const WINDOW *); |
| 363 |
int getmaxy(const WINDOW *); |
| 364 |
int getparx(const WINDOW *); |
| 365 |
int getpary(const WINDOW *); |
| 366 |
|
| 367 |
int getmouse(MEVENT *); |
| 368 |
int ungetmouse(MEVENT *); |
| 369 |
mmask_t mousemask(mmask_t, mmask_t *); |
| 370 |
bool wenclose(const WINDOW *, int, int); |
| 371 |
int mouseinterval(int); |
| 372 |
|
| 373 |
void setsyx(int y, int x); |
| 374 |
const char *unctrl(chtype); |
| 375 |
int use_default_colors(void); |
| 376 |
|
| 377 |
int has_key(int); |
| 378 |
bool is_term_resized(int, int); |
| 379 |
|
| 380 |
#define _m_STRICT_SYSV_CURSES ... |
| 381 |
#define _m_NCURSES_MOUSE_VERSION ... |
| 382 |
#define _m_NetBSD ... |
| 383 |
int _m_ispad(WINDOW *); |
| 384 |
|
| 385 |
extern chtype acs_map[]; |
| 386 |
|
| 387 |
// For _curses_panel: |
| 388 |
|
| 389 |
typedef ... PANEL; |
| 390 |
|
| 391 |
WINDOW *panel_window(const PANEL *); |
| 392 |
void update_panels(void); |
| 393 |
int hide_panel(PANEL *); |
| 394 |
int show_panel(PANEL *); |
| 395 |
int del_panel(PANEL *); |
| 396 |
int top_panel(PANEL *); |
| 397 |
int bottom_panel(PANEL *); |
| 398 |
PANEL *new_panel(WINDOW *); |
| 399 |
PANEL *panel_above(const PANEL *); |
| 400 |
PANEL *panel_below(const PANEL *); |
| 401 |
int set_panel_userptr(PANEL *, void *); |
| 402 |
const void *panel_userptr(const PANEL *); |
| 403 |
int move_panel(PANEL *, int, int); |
| 404 |
int replace_panel(PANEL *,WINDOW *); |
| 405 |
int panel_hidden(const PANEL *); |
| 406 |
|
| 407 |
void _m_getsyx(int *yx); |
| 408 |
""") |
| 409 |
|
| 410 |
if 'ncursesw' in libs: |
| 411 |
ffi.cdef(""" |
| 412 |
typedef int... wint_t; |
| 413 |
int wget_wch(WINDOW *, wint_t *); |
| 414 |
int mvwget_wch(WINDOW *, int, int, wint_t *); |
| 415 |
int unget_wch(const wchar_t); |
| 416 |
""") |
| 417 |
|
| 418 |
if sys.platform == "darwin": |
| 419 |
ffi.cdef(""" |
| 420 |
char * tparm(char *, ...); |
| 421 |
""") |
| 422 |
else: |
| 423 |
ffi.cdef(""" |
| 424 |
char * tparm(const char *, ...); |
| 425 |
""") |
| 426 |
|
| 427 |
|
| 428 |
if __name__ == "__main__": |
| 429 |
ffi.compile() |