You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2892 lines
86 KiB

16 years ago
  1. /*
  2. * This is a curses module for Python.
  3. *
  4. * Based on prior work by Lance Ellinghaus and Oliver Andrich
  5. * Version 1.2 of this module: Copyright 1994 by Lance Ellinghouse,
  6. * Cathedral City, California Republic, United States of America.
  7. *
  8. * Version 1.5b1, heavily extended for ncurses by Oliver Andrich:
  9. * Copyright 1996,1997 by Oliver Andrich, Koblenz, Germany.
  10. *
  11. * Tidied for Python 1.6, and currently maintained by <amk@amk.ca>.
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining
  14. * a copy of this source file to use, copy, modify, merge, or publish it
  15. * subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included
  18. * in all copies or in any new file that contains a substantial portion of
  19. * this file.
  20. *
  21. * THE AUTHOR MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF
  22. * THE SOFTWARE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT
  23. * EXPRESS OR IMPLIED WARRANTY. THE AUTHOR DISCLAIMS ALL WARRANTIES
  24. * WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  25. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NON-INFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE
  27. * AUTHOR BE LIABLE TO YOU OR ANY OTHER PARTY FOR ANY SPECIAL,
  28. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  29. * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE, STRICT LIABILITY OR
  30. * ANY OTHER ACTION ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  31. * PERFORMANCE OF THIS SOFTWARE.
  32. */
  33. /*
  34. A number of SysV or ncurses functions don't have wrappers yet; if you
  35. need a given function, add it and send a patch. See
  36. http://www.python.org/dev/patches/ for instructions on how to submit
  37. patches to Python.
  38. Here's a list of currently unsupported functions:
  39. addchnstr addchstr color_set define_key
  40. del_curterm delscreen dupwin inchnstr inchstr innstr keyok
  41. mcprint mvaddchnstr mvaddchstr mvcur mvinchnstr
  42. mvinchstr mvinnstr mmvwaddchnstr mvwaddchstr
  43. mvwinchnstr mvwinchstr mvwinnstr newterm
  44. restartterm ripoffline scr_dump
  45. scr_init scr_restore scr_set scrl set_curterm set_term setterm
  46. tgetent tgetflag tgetnum tgetstr tgoto timeout tputs
  47. vidattr vidputs waddchnstr waddchstr
  48. wcolor_set winchnstr winchstr winnstr wmouse_trafo wscrl
  49. Low-priority:
  50. slk_attr slk_attr_off slk_attr_on slk_attr_set slk_attroff
  51. slk_attron slk_attrset slk_clear slk_color slk_init slk_label
  52. slk_noutrefresh slk_refresh slk_restore slk_set slk_touch
  53. Menu extension (ncurses and probably SYSV):
  54. current_item free_item free_menu item_count item_description
  55. item_index item_init item_name item_opts item_opts_off
  56. item_opts_on item_term item_userptr item_value item_visible
  57. menu_back menu_driver menu_fore menu_format menu_grey
  58. menu_init menu_items menu_mark menu_opts menu_opts_off
  59. menu_opts_on menu_pad menu_pattern menu_request_by_name
  60. menu_request_name menu_spacing menu_sub menu_term menu_userptr
  61. menu_win new_item new_menu pos_menu_cursor post_menu
  62. scale_menu set_current_item set_item_init set_item_opts
  63. set_item_term set_item_userptr set_item_value set_menu_back
  64. set_menu_fore set_menu_format set_menu_grey set_menu_init
  65. set_menu_items set_menu_mark set_menu_opts set_menu_pad
  66. set_menu_pattern set_menu_spacing set_menu_sub set_menu_term
  67. set_menu_userptr set_menu_win set_top_row top_row unpost_menu
  68. Form extension (ncurses and probably SYSV):
  69. current_field data_ahead data_behind dup_field
  70. dynamic_fieldinfo field_arg field_back field_buffer
  71. field_count field_fore field_index field_info field_init
  72. field_just field_opts field_opts_off field_opts_on field_pad
  73. field_status field_term field_type field_userptr form_driver
  74. form_fields form_init form_opts form_opts_off form_opts_on
  75. form_page form_request_by_name form_request_name form_sub
  76. form_term form_userptr form_win free_field free_form
  77. link_field link_fieldtype move_field new_field new_form
  78. new_page pos_form_cursor post_form scale_form
  79. set_current_field set_field_back set_field_buffer
  80. set_field_fore set_field_init set_field_just set_field_opts
  81. set_field_pad set_field_status set_field_term set_field_type
  82. set_field_userptr set_fieldtype_arg set_fieldtype_choice
  83. set_form_fields set_form_init set_form_opts set_form_page
  84. set_form_sub set_form_term set_form_userptr set_form_win
  85. set_max_field set_new_page unpost_form
  86. */
  87. /* Release Number */
  88. char *PyCursesVersion = "2.2";
  89. /* Includes */
  90. #include "Python.h"
  91. #ifdef __osf__
  92. #define STRICT_SYSV_CURSES /* Don't use ncurses extensions */
  93. #endif
  94. #ifdef __hpux
  95. #define STRICT_SYSV_CURSES
  96. #endif
  97. #define CURSES_MODULE
  98. #include "py_curses.h"
  99. /* These prototypes are in <term.h>, but including this header
  100. #defines many common symbols (such as "lines") which breaks the
  101. curses module in other ways. So the code will just specify
  102. explicit prototypes here. */
  103. extern int setupterm(char *,int,int *);
  104. #ifdef __sgi
  105. #include <term.h>
  106. #endif
  107. #if !defined(HAVE_NCURSES_H) && (defined(sgi) || defined(__sun) || defined(SCO5))
  108. #define STRICT_SYSV_CURSES /* Don't use ncurses extensions */
  109. typedef chtype attr_t; /* No attr_t type is available */
  110. #endif
  111. #if defined(_AIX)
  112. #define STRICT_SYSV_CURSES
  113. #endif
  114. /* Definition of exception curses.error */
  115. static PyObject *PyCursesError;
  116. /* Tells whether setupterm() has been called to initialise terminfo. */
  117. static int initialised_setupterm = FALSE;
  118. /* Tells whether initscr() has been called to initialise curses. */
  119. static int initialised = FALSE;
  120. /* Tells whether start_color() has been called to initialise color usage. */
  121. static int initialisedcolors = FALSE;
  122. /* Utility Macros */
  123. #define PyCursesSetupTermCalled \
  124. if (initialised_setupterm != TRUE) { \
  125. PyErr_SetString(PyCursesError, \
  126. "must call (at least) setupterm() first"); \
  127. return 0; }
  128. #define PyCursesInitialised \
  129. if (initialised != TRUE) { \
  130. PyErr_SetString(PyCursesError, \
  131. "must call initscr() first"); \
  132. return 0; }
  133. #define PyCursesInitialisedColor \
  134. if (initialisedcolors != TRUE) { \
  135. PyErr_SetString(PyCursesError, \
  136. "must call start_color() first"); \
  137. return 0; }
  138. #ifndef MIN
  139. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  140. #endif
  141. /* Utility Functions */
  142. /*
  143. * Check the return code from a curses function and return None
  144. * or raise an exception as appropriate. These are exported using the
  145. * capsule API.
  146. */
  147. static PyObject *
  148. PyCursesCheckERR(int code, char *fname)
  149. {
  150. if (code != ERR) {
  151. Py_INCREF(Py_None);
  152. return Py_None;
  153. } else {
  154. if (fname == NULL) {
  155. PyErr_SetString(PyCursesError, catchall_ERR);
  156. } else {
  157. PyErr_Format(PyCursesError, "%s() returned ERR", fname);
  158. }
  159. return NULL;
  160. }
  161. }
  162. static int
  163. PyCurses_ConvertToChtype(PyObject *obj, chtype *ch)
  164. {
  165. if (PyInt_Check(obj)) {
  166. *ch = (chtype) PyInt_AsLong(obj);
  167. } else if(PyString_Check(obj)
  168. && (PyString_Size(obj) == 1)) {
  169. *ch = (chtype) *PyString_AsString(obj);
  170. } else {
  171. return 0;
  172. }
  173. return 1;
  174. }
  175. /* Function versions of the 3 functions for testing whether curses has been
  176. initialised or not. */
  177. static int func_PyCursesSetupTermCalled(void)
  178. {
  179. PyCursesSetupTermCalled;
  180. return 1;
  181. }
  182. static int func_PyCursesInitialised(void)
  183. {
  184. PyCursesInitialised;
  185. return 1;
  186. }
  187. static int func_PyCursesInitialisedColor(void)
  188. {
  189. PyCursesInitialisedColor;
  190. return 1;
  191. }
  192. /*****************************************************************************
  193. The Window Object
  194. ******************************************************************************/
  195. /* Definition of the window type */
  196. PyTypeObject PyCursesWindow_Type;
  197. /* Function prototype macros for Window object
  198. X - function name
  199. TYPE - parameter Type
  200. ERGSTR - format string for construction of the return value
  201. PARSESTR - format string for argument parsing
  202. */
  203. #define Window_NoArgNoReturnFunction(X) \
  204. static PyObject *PyCursesWindow_ ## X \
  205. (PyCursesWindowObject *self, PyObject *args) \
  206. { return PyCursesCheckERR(X(self->win), # X); }
  207. #define Window_NoArgTrueFalseFunction(X) \
  208. static PyObject * PyCursesWindow_ ## X \
  209. (PyCursesWindowObject *self) \
  210. { \
  211. if (X (self->win) == FALSE) { Py_INCREF(Py_False); return Py_False; } \
  212. else { Py_INCREF(Py_True); return Py_True; } }
  213. #define Window_NoArgNoReturnVoidFunction(X) \
  214. static PyObject * PyCursesWindow_ ## X \
  215. (PyCursesWindowObject *self) \
  216. { \
  217. X(self->win); Py_INCREF(Py_None); return Py_None; }
  218. #define Window_NoArg2TupleReturnFunction(X, TYPE, ERGSTR) \
  219. static PyObject * PyCursesWindow_ ## X \
  220. (PyCursesWindowObject *self) \
  221. { \
  222. TYPE arg1, arg2; \
  223. X(self->win,arg1,arg2); return Py_BuildValue(ERGSTR, arg1, arg2); }
  224. #define Window_OneArgNoReturnVoidFunction(X, TYPE, PARSESTR) \
  225. static PyObject * PyCursesWindow_ ## X \
  226. (PyCursesWindowObject *self, PyObject *args) \
  227. { \
  228. TYPE arg1; \
  229. if (!PyArg_ParseTuple(args, PARSESTR, &arg1)) return NULL; \
  230. X(self->win,arg1); Py_INCREF(Py_None); return Py_None; }
  231. #define Window_OneArgNoReturnFunction(X, TYPE, PARSESTR) \
  232. static PyObject * PyCursesWindow_ ## X \
  233. (PyCursesWindowObject *self, PyObject *args) \
  234. { \
  235. TYPE arg1; \
  236. if (!PyArg_ParseTuple(args,PARSESTR, &arg1)) return NULL; \
  237. return PyCursesCheckERR(X(self->win, arg1), # X); }
  238. #define Window_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \
  239. static PyObject * PyCursesWindow_ ## X \
  240. (PyCursesWindowObject *self, PyObject *args) \
  241. { \
  242. TYPE arg1, arg2; \
  243. if (!PyArg_ParseTuple(args,PARSESTR, &arg1, &arg2)) return NULL; \
  244. return PyCursesCheckERR(X(self->win, arg1, arg2), # X); }
  245. /* ------------- WINDOW routines --------------- */
  246. Window_NoArgNoReturnFunction(untouchwin)
  247. Window_NoArgNoReturnFunction(touchwin)
  248. Window_NoArgNoReturnFunction(redrawwin)
  249. Window_NoArgNoReturnFunction(winsertln)
  250. Window_NoArgNoReturnFunction(werase)
  251. Window_NoArgNoReturnFunction(wdeleteln)
  252. Window_NoArgTrueFalseFunction(is_wintouched)
  253. Window_NoArgNoReturnVoidFunction(wsyncup)
  254. Window_NoArgNoReturnVoidFunction(wsyncdown)
  255. Window_NoArgNoReturnVoidFunction(wstandend)
  256. Window_NoArgNoReturnVoidFunction(wstandout)
  257. Window_NoArgNoReturnVoidFunction(wcursyncup)
  258. Window_NoArgNoReturnVoidFunction(wclrtoeol)
  259. Window_NoArgNoReturnVoidFunction(wclrtobot)
  260. Window_NoArgNoReturnVoidFunction(wclear)
  261. Window_OneArgNoReturnVoidFunction(idcok, int, "i;True(1) or False(0)")
  262. Window_OneArgNoReturnVoidFunction(immedok, int, "i;True(1) or False(0)")
  263. Window_OneArgNoReturnVoidFunction(wtimeout, int, "i;delay")
  264. Window_NoArg2TupleReturnFunction(getyx, int, "ii")
  265. Window_NoArg2TupleReturnFunction(getbegyx, int, "ii")
  266. Window_NoArg2TupleReturnFunction(getmaxyx, int, "ii")
  267. Window_NoArg2TupleReturnFunction(getparyx, int, "ii")
  268. Window_OneArgNoReturnFunction(clearok, int, "i;True(1) or False(0)")
  269. Window_OneArgNoReturnFunction(idlok, int, "i;True(1) or False(0)")
  270. #if defined(__NetBSD__)
  271. Window_OneArgNoReturnVoidFunction(keypad, int, "i;True(1) or False(0)")
  272. #else
  273. Window_OneArgNoReturnFunction(keypad, int, "i;True(1) or False(0)")
  274. #endif
  275. Window_OneArgNoReturnFunction(leaveok, int, "i;True(1) or False(0)")
  276. #if defined(__NetBSD__)
  277. Window_OneArgNoReturnVoidFunction(nodelay, int, "i;True(1) or False(0)")
  278. #else
  279. Window_OneArgNoReturnFunction(nodelay, int, "i;True(1) or False(0)")
  280. #endif
  281. Window_OneArgNoReturnFunction(notimeout, int, "i;True(1) or False(0)")
  282. Window_OneArgNoReturnFunction(scrollok, int, "i;True(1) or False(0)")
  283. Window_OneArgNoReturnFunction(winsdelln, int, "i;nlines")
  284. Window_OneArgNoReturnFunction(syncok, int, "i;True(1) or False(0)")
  285. Window_TwoArgNoReturnFunction(mvwin, int, "ii;y,x")
  286. Window_TwoArgNoReturnFunction(mvderwin, int, "ii;y,x")
  287. Window_TwoArgNoReturnFunction(wmove, int, "ii;y,x")
  288. #ifndef STRICT_SYSV_CURSES
  289. Window_TwoArgNoReturnFunction(wresize, int, "ii;lines,columns")
  290. #endif
  291. /* Allocation and deallocation of Window Objects */
  292. static PyObject *
  293. PyCursesWindow_New(WINDOW *win)
  294. {
  295. PyCursesWindowObject *wo;
  296. wo = PyObject_NEW(PyCursesWindowObject, &PyCursesWindow_Type);
  297. if (wo == NULL) return NULL;
  298. wo->win = win;
  299. return (PyObject *)wo;
  300. }
  301. static void
  302. PyCursesWindow_Dealloc(PyCursesWindowObject *wo)
  303. {
  304. if (wo->win != stdscr) delwin(wo->win);
  305. PyObject_DEL(wo);
  306. }
  307. /* Addch, Addstr, Addnstr */
  308. static PyObject *
  309. PyCursesWindow_AddCh(PyCursesWindowObject *self, PyObject *args)
  310. {
  311. int rtn, x, y, use_xy = FALSE;
  312. PyObject *temp;
  313. chtype ch = 0;
  314. attr_t attr = A_NORMAL;
  315. long lattr;
  316. switch (PyTuple_Size(args)) {
  317. case 1:
  318. if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
  319. return NULL;
  320. break;
  321. case 2:
  322. if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &temp, &lattr))
  323. return NULL;
  324. attr = lattr;
  325. break;
  326. case 3:
  327. if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &temp))
  328. return NULL;
  329. use_xy = TRUE;
  330. break;
  331. case 4:
  332. if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr",
  333. &y, &x, &temp, &lattr))
  334. return NULL;
  335. attr = lattr;
  336. use_xy = TRUE;
  337. break;
  338. default:
  339. PyErr_SetString(PyExc_TypeError, "addch requires 1 to 4 arguments");
  340. return NULL;
  341. }
  342. if (!PyCurses_ConvertToChtype(temp, &ch)) {
  343. PyErr_SetString(PyExc_TypeError, "argument 1 or 3 must be a ch or an int");
  344. return NULL;
  345. }
  346. if (use_xy == TRUE)
  347. rtn = mvwaddch(self->win,y,x, ch | attr);
  348. else {
  349. rtn = waddch(self->win, ch | attr);
  350. }
  351. return PyCursesCheckERR(rtn, "addch");
  352. }
  353. static PyObject *
  354. PyCursesWindow_AddStr(PyCursesWindowObject *self, PyObject *args)
  355. {
  356. int rtn;
  357. int x, y;
  358. char *str;
  359. attr_t attr = A_NORMAL , attr_old = A_NORMAL;
  360. long lattr;
  361. int use_xy = FALSE, use_attr = FALSE;
  362. switch (PyTuple_Size(args)) {
  363. case 1:
  364. if (!PyArg_ParseTuple(args,"s;str", &str))
  365. return NULL;
  366. break;
  367. case 2:
  368. if (!PyArg_ParseTuple(args,"sl;str,attr", &str, &lattr))
  369. return NULL;
  370. attr = lattr;
  371. use_attr = TRUE;
  372. break;
  373. case 3:
  374. if (!PyArg_ParseTuple(args,"iis;int,int,str", &y, &x, &str))
  375. return NULL;
  376. use_xy = TRUE;
  377. break;
  378. case 4:
  379. if (!PyArg_ParseTuple(args,"iisl;int,int,str,attr", &y, &x, &str, &lattr))
  380. return NULL;
  381. attr = lattr;
  382. use_xy = use_attr = TRUE;
  383. break;
  384. default:
  385. PyErr_SetString(PyExc_TypeError, "addstr requires 1 to 4 arguments");
  386. return NULL;
  387. }
  388. if (use_attr == TRUE) {
  389. attr_old = getattrs(self->win);
  390. (void)wattrset(self->win,attr);
  391. }
  392. if (use_xy == TRUE)
  393. rtn = mvwaddstr(self->win,y,x,str);
  394. else
  395. rtn = waddstr(self->win,str);
  396. if (use_attr == TRUE)
  397. (void)wattrset(self->win,attr_old);
  398. return PyCursesCheckERR(rtn, "addstr");
  399. }
  400. static PyObject *
  401. PyCursesWindow_AddNStr(PyCursesWindowObject *self, PyObject *args)
  402. {
  403. int rtn, x, y, n;
  404. char *str;
  405. attr_t attr = A_NORMAL , attr_old = A_NORMAL;
  406. long lattr;
  407. int use_xy = FALSE, use_attr = FALSE;
  408. switch (PyTuple_Size(args)) {
  409. case 2:
  410. if (!PyArg_ParseTuple(args,"si;str,n", &str, &n))
  411. return NULL;
  412. break;
  413. case 3:
  414. if (!PyArg_ParseTuple(args,"sil;str,n,attr", &str, &n, &lattr))
  415. return NULL;
  416. attr = lattr;
  417. use_attr = TRUE;
  418. break;
  419. case 4:
  420. if (!PyArg_ParseTuple(args,"iisi;y,x,str,n", &y, &x, &str, &n))
  421. return NULL;
  422. use_xy = TRUE;
  423. break;
  424. case 5:
  425. if (!PyArg_ParseTuple(args,"iisil;y,x,str,n,attr", &y, &x, &str, &n, &lattr))
  426. return NULL;
  427. attr = lattr;
  428. use_xy = use_attr = TRUE;
  429. break;
  430. default:
  431. PyErr_SetString(PyExc_TypeError, "addnstr requires 2 to 5 arguments");
  432. return NULL;
  433. }
  434. if (use_attr == TRUE) {
  435. attr_old = getattrs(self->win);
  436. (void)wattrset(self->win,attr);
  437. }
  438. if (use_xy == TRUE)
  439. rtn = mvwaddnstr(self->win,y,x,str,n);
  440. else
  441. rtn = waddnstr(self->win,str,n);
  442. if (use_attr == TRUE)
  443. (void)wattrset(self->win,attr_old);
  444. return PyCursesCheckERR(rtn, "addnstr");
  445. }
  446. static PyObject *
  447. PyCursesWindow_Bkgd(PyCursesWindowObject *self, PyObject *args)
  448. {
  449. PyObject *temp;
  450. chtype bkgd;
  451. attr_t attr = A_NORMAL;
  452. long lattr;
  453. switch (PyTuple_Size(args)) {
  454. case 1:
  455. if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
  456. return NULL;
  457. break;
  458. case 2:
  459. if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr))
  460. return NULL;
  461. attr = lattr;
  462. break;
  463. default:
  464. PyErr_SetString(PyExc_TypeError, "bkgd requires 1 or 2 arguments");
  465. return NULL;
  466. }
  467. if (!PyCurses_ConvertToChtype(temp, &bkgd)) {
  468. PyErr_SetString(PyExc_TypeError, "argument 1 or 3 must be a ch or an int");
  469. return NULL;
  470. }
  471. return PyCursesCheckERR(wbkgd(self->win, bkgd | attr), "bkgd");
  472. }
  473. static PyObject *
  474. PyCursesWindow_AttrOff(PyCursesWindowObject *self, PyObject *args)
  475. {
  476. long lattr;
  477. if (!PyArg_ParseTuple(args,"l;attr", &lattr))
  478. return NULL;
  479. return PyCursesCheckERR(wattroff(self->win, (attr_t)lattr), "attroff");
  480. }
  481. static PyObject *
  482. PyCursesWindow_AttrOn(PyCursesWindowObject *self, PyObject *args)
  483. {
  484. long lattr;
  485. if (!PyArg_ParseTuple(args,"l;attr", &lattr))
  486. return NULL;
  487. return PyCursesCheckERR(wattron(self->win, (attr_t)lattr), "attron");
  488. }
  489. static PyObject *
  490. PyCursesWindow_AttrSet(PyCursesWindowObject *self, PyObject *args)
  491. {
  492. long lattr;
  493. if (!PyArg_ParseTuple(args,"l;attr", &lattr))
  494. return NULL;
  495. return PyCursesCheckERR(wattrset(self->win, (attr_t)lattr), "attrset");
  496. }
  497. static PyObject *
  498. PyCursesWindow_BkgdSet(PyCursesWindowObject *self, PyObject *args)
  499. {
  500. PyObject *temp;
  501. chtype bkgd;
  502. attr_t attr = A_NORMAL;
  503. long lattr;
  504. switch (PyTuple_Size(args)) {
  505. case 1:
  506. if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
  507. return NULL;
  508. break;
  509. case 2:
  510. if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr))
  511. return NULL;
  512. attr = lattr;
  513. break;
  514. default:
  515. PyErr_SetString(PyExc_TypeError, "bkgdset requires 1 or 2 arguments");
  516. return NULL;
  517. }
  518. if (!PyCurses_ConvertToChtype(temp, &bkgd)) {
  519. PyErr_SetString(PyExc_TypeError, "argument 1 must be a ch or an int");
  520. return NULL;
  521. }
  522. wbkgdset(self->win, bkgd | attr);
  523. return PyCursesCheckERR(0, "bkgdset");
  524. }
  525. static PyObject *
  526. PyCursesWindow_Border(PyCursesWindowObject *self, PyObject *args)
  527. {
  528. PyObject *temp[8];
  529. chtype ch[8];
  530. int i;
  531. /* Clear the array of parameters */
  532. for(i=0; i<8; i++) {
  533. temp[i] = NULL;
  534. ch[i] = 0;
  535. }
  536. if (!PyArg_ParseTuple(args,"|OOOOOOOO;ls,rs,ts,bs,tl,tr,bl,br",
  537. &temp[0], &temp[1], &temp[2], &temp[3],
  538. &temp[4], &temp[5], &temp[6], &temp[7]))
  539. return NULL;
  540. for(i=0; i<8; i++) {
  541. if (temp[i] != NULL && !PyCurses_ConvertToChtype(temp[i], &ch[i])) {
  542. PyErr_Format(PyExc_TypeError,
  543. "argument %i must be a ch or an int", i+1);
  544. return NULL;
  545. }
  546. }
  547. wborder(self->win,
  548. ch[0], ch[1], ch[2], ch[3],
  549. ch[4], ch[5], ch[6], ch[7]);
  550. Py_INCREF(Py_None);
  551. return Py_None;
  552. }
  553. static PyObject *
  554. PyCursesWindow_Box(PyCursesWindowObject *self, PyObject *args)
  555. {
  556. chtype ch1=0,ch2=0;
  557. switch(PyTuple_Size(args)){
  558. case 0: break;
  559. default:
  560. if (!PyArg_ParseTuple(args,"ll;vertint,horint", &ch1, &ch2))
  561. return NULL;
  562. }
  563. box(self->win,ch1,ch2);
  564. Py_INCREF(Py_None);
  565. return Py_None;
  566. }
  567. #if defined(HAVE_NCURSES_H) || defined(MVWDELCH_IS_EXPRESSION)
  568. #define py_mvwdelch mvwdelch
  569. #else
  570. int py_mvwdelch(WINDOW *w, int y, int x)
  571. {
  572. mvwdelch(w,y,x);
  573. /* On HP/UX, mvwdelch already returns. On other systems,
  574. we may well run into this return statement. */
  575. return 0;
  576. }
  577. #endif
  578. /* chgat, added by Fabian Kreutz <fabian.kreutz at gmx.net> */
  579. static PyObject *
  580. PyCursesWindow_ChgAt(PyCursesWindowObject *self, PyObject *args)
  581. {
  582. int rtn;
  583. int x, y;
  584. int num = -1;
  585. short color;
  586. attr_t attr = A_NORMAL;
  587. long lattr;
  588. int use_xy = FALSE;
  589. switch (PyTuple_Size(args)) {
  590. case 1:
  591. if (!PyArg_ParseTuple(args,"l;attr", &lattr))
  592. return NULL;
  593. attr = lattr;
  594. break;
  595. case 2:
  596. if (!PyArg_ParseTuple(args,"il;n,attr", &num, &lattr))
  597. return NULL;
  598. attr = lattr;
  599. break;
  600. case 3:
  601. if (!PyArg_ParseTuple(args,"iil;int,int,attr", &y, &x, &lattr))
  602. return NULL;
  603. attr = lattr;
  604. use_xy = TRUE;
  605. break;
  606. case 4:
  607. if (!PyArg_ParseTuple(args,"iiil;int,int,n,attr", &y, &x, &num, &lattr))
  608. return NULL;
  609. attr = lattr;
  610. use_xy = TRUE;
  611. break;
  612. default:
  613. PyErr_SetString(PyExc_TypeError, "chgat requires 1 to 4 arguments");
  614. return NULL;
  615. }
  616. color = (short)((attr >> 8) & 0xff);
  617. attr = attr - (color << 8);
  618. if (use_xy == TRUE) {
  619. rtn = mvwchgat(self->win,y,x,num,attr,color,NULL);
  620. touchline(self->win,y,1);
  621. } else {
  622. getyx(self->win,y,x);
  623. rtn = wchgat(self->win,num,attr,color,NULL);
  624. touchline(self->win,y,1);
  625. }
  626. return PyCursesCheckERR(rtn, "chgat");
  627. }
  628. static PyObject *
  629. PyCursesWindow_DelCh(PyCursesWindowObject *self, PyObject *args)
  630. {
  631. int rtn;
  632. int x, y;
  633. switch (PyTuple_Size(args)) {
  634. case 0:
  635. rtn = wdelch(self->win);
  636. break;
  637. case 2:
  638. if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x))
  639. return NULL;
  640. rtn = py_mvwdelch(self->win,y,x);
  641. break;
  642. default:
  643. PyErr_SetString(PyExc_TypeError, "delch requires 0 or 2 arguments");
  644. return NULL;
  645. }
  646. return PyCursesCheckERR(rtn, "[mv]wdelch");
  647. }
  648. static PyObject *
  649. PyCursesWindow_DerWin(PyCursesWindowObject *self, PyObject *args)
  650. {
  651. WINDOW *win;
  652. int nlines, ncols, begin_y, begin_x;
  653. nlines = 0;
  654. ncols = 0;
  655. switch (PyTuple_Size(args)) {
  656. case 2:
  657. if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
  658. return NULL;
  659. break;
  660. case 4:
  661. if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
  662. &nlines,&ncols,&begin_y,&begin_x))
  663. return NULL;
  664. break;
  665. default:
  666. PyErr_SetString(PyExc_TypeError, "derwin requires 2 or 4 arguments");
  667. return NULL;
  668. }
  669. win = derwin(self->win,nlines,ncols,begin_y,begin_x);
  670. if (win == NULL) {
  671. PyErr_SetString(PyCursesError, catchall_NULL);
  672. return NULL;
  673. }
  674. return (PyObject *)PyCursesWindow_New(win);
  675. }
  676. static PyObject *
  677. PyCursesWindow_EchoChar(PyCursesWindowObject *self, PyObject *args)
  678. {
  679. PyObject *temp;
  680. chtype ch;
  681. attr_t attr = A_NORMAL;
  682. long lattr;
  683. switch (PyTuple_Size(args)) {
  684. case 1:
  685. if (!PyArg_ParseTuple(args,"O;ch or int", &temp))
  686. return NULL;
  687. break;
  688. case 2:
  689. if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr))
  690. return NULL;
  691. attr = lattr;
  692. break;
  693. default:
  694. PyErr_SetString(PyExc_TypeError, "echochar requires 1 or 2 arguments");
  695. return NULL;
  696. }
  697. if (!PyCurses_ConvertToChtype(temp, &ch)) {
  698. PyErr_SetString(PyExc_TypeError, "argument 1 must be a ch or an int");
  699. return NULL;
  700. }
  701. #ifdef WINDOW_HAS_FLAGS
  702. if (self->win->_flags & _ISPAD)
  703. return PyCursesCheckERR(pechochar(self->win, ch | attr),
  704. "echochar");
  705. else
  706. #endif
  707. return PyCursesCheckERR(wechochar(self->win, ch | attr),
  708. "echochar");
  709. }
  710. #ifdef NCURSES_MOUSE_VERSION
  711. static PyObject *
  712. PyCursesWindow_Enclose(PyCursesWindowObject *self, PyObject *args)
  713. {
  714. int x, y;
  715. if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x))
  716. return NULL;
  717. return PyInt_FromLong( wenclose(self->win,y,x) );
  718. }
  719. #endif
  720. static PyObject *
  721. PyCursesWindow_GetBkgd(PyCursesWindowObject *self)
  722. {
  723. return PyInt_FromLong((long) getbkgd(self->win));
  724. }
  725. static PyObject *
  726. PyCursesWindow_GetCh(PyCursesWindowObject *self, PyObject *args)
  727. {
  728. int x, y;
  729. int rtn;
  730. switch (PyTuple_Size(args)) {
  731. case 0:
  732. Py_BEGIN_ALLOW_THREADS
  733. rtn = wgetch(self->win);
  734. Py_END_ALLOW_THREADS
  735. break;
  736. case 2:
  737. if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
  738. return NULL;
  739. Py_BEGIN_ALLOW_THREADS
  740. rtn = mvwgetch(self->win,y,x);
  741. Py_END_ALLOW_THREADS
  742. break;
  743. default:
  744. PyErr_SetString(PyExc_TypeError, "getch requires 0 or 2 arguments");
  745. return NULL;
  746. }
  747. return PyInt_FromLong((long)rtn);
  748. }
  749. static PyObject *
  750. PyCursesWindow_GetKey(PyCursesWindowObject *self, PyObject *args)
  751. {
  752. int x, y;
  753. int rtn;
  754. switch (PyTuple_Size(args)) {
  755. case 0:
  756. Py_BEGIN_ALLOW_THREADS
  757. rtn = wgetch(self->win);
  758. Py_END_ALLOW_THREADS
  759. break;
  760. case 2:
  761. if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
  762. return NULL;
  763. Py_BEGIN_ALLOW_THREADS
  764. rtn = mvwgetch(self->win,y,x);
  765. Py_END_ALLOW_THREADS
  766. break;
  767. default:
  768. PyErr_SetString(PyExc_TypeError, "getkey requires 0 or 2 arguments");
  769. return NULL;
  770. }
  771. if (rtn == ERR) {
  772. /* getch() returns ERR in nodelay mode */
  773. PyErr_SetString(PyCursesError, "no input");
  774. return NULL;
  775. } else if (rtn<=255) {
  776. return Py_BuildValue("c", rtn);
  777. } else {
  778. const char *knp;
  779. #if defined(__NetBSD__)
  780. knp = unctrl(rtn);
  781. #else
  782. knp = keyname(rtn);
  783. #endif
  784. return PyString_FromString((knp == NULL) ? "" : knp);
  785. }
  786. }
  787. static PyObject *
  788. PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
  789. {
  790. int x, y, n;
  791. char rtn[1024]; /* This should be big enough.. I hope */
  792. int rtn2;
  793. switch (PyTuple_Size(args)) {
  794. case 0:
  795. Py_BEGIN_ALLOW_THREADS
  796. rtn2 = wgetnstr(self->win,rtn, 1023);
  797. Py_END_ALLOW_THREADS
  798. break;
  799. case 1:
  800. if (!PyArg_ParseTuple(args,"i;n", &n))
  801. return NULL;
  802. Py_BEGIN_ALLOW_THREADS
  803. rtn2 = wgetnstr(self->win,rtn,MIN(n, 1023));
  804. Py_END_ALLOW_THREADS
  805. break;
  806. case 2:
  807. if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
  808. return NULL;
  809. Py_BEGIN_ALLOW_THREADS
  810. #ifdef STRICT_SYSV_CURSES
  811. rtn2 = wmove(self->win,y,x)==ERR ? ERR : wgetnstr(self->win, rtn, 1023);
  812. #else
  813. rtn2 = mvwgetnstr(self->win,y,x,rtn, 1023);
  814. #endif
  815. Py_END_ALLOW_THREADS
  816. break;
  817. case 3:
  818. if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
  819. return NULL;
  820. #ifdef STRICT_SYSV_CURSES
  821. Py_BEGIN_ALLOW_THREADS
  822. rtn2 = wmove(self->win,y,x)==ERR ? ERR :
  823. wgetnstr(self->win, rtn, MIN(n, 1023));
  824. Py_END_ALLOW_THREADS
  825. #else
  826. Py_BEGIN_ALLOW_THREADS
  827. rtn2 = mvwgetnstr(self->win, y, x, rtn, MIN(n, 1023));
  828. Py_END_ALLOW_THREADS
  829. #endif
  830. break;
  831. default:
  832. PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 3 arguments");
  833. return NULL;
  834. }
  835. if (rtn2 == ERR)
  836. rtn[0] = 0;
  837. return PyString_FromString(rtn);
  838. }
  839. static PyObject *
  840. PyCursesWindow_Hline(PyCursesWindowObject *self, PyObject *args)
  841. {
  842. PyObject *temp;
  843. chtype ch;
  844. int n, x, y, code = OK;
  845. attr_t attr = A_NORMAL;
  846. long lattr;
  847. switch (PyTuple_Size(args)) {
  848. case 2:
  849. if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
  850. return NULL;
  851. break;
  852. case 3:
  853. if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &lattr))
  854. return NULL;
  855. attr = lattr;
  856. break;
  857. case 4:
  858. if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
  859. return NULL;
  860. code = wmove(self->win, y, x);
  861. break;
  862. case 5:
  863. if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
  864. &y, &x, &temp, &n, &lattr))
  865. return NULL;
  866. attr = lattr;
  867. code = wmove(self->win, y, x);
  868. break;
  869. default:
  870. PyErr_SetString(PyExc_TypeError, "hline requires 2 to 5 arguments");
  871. return NULL;
  872. }
  873. if (code != ERR) {
  874. if (!PyCurses_ConvertToChtype(temp, &ch)) {
  875. PyErr_SetString(PyExc_TypeError,
  876. "argument 1 or 3 must be a ch or an int");
  877. return NULL;
  878. }
  879. return PyCursesCheckERR(whline(self->win, ch | attr, n), "hline");
  880. } else
  881. return PyCursesCheckERR(code, "wmove");
  882. }
  883. static PyObject *
  884. PyCursesWindow_InsCh(PyCursesWindowObject *self, PyObject *args)
  885. {
  886. int rtn, x, y, use_xy = FALSE;
  887. PyObject *temp;
  888. chtype ch = 0;
  889. attr_t attr = A_NORMAL;
  890. long lattr;
  891. switch (PyTuple_Size(args)) {
  892. case 1:
  893. if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
  894. return NULL;
  895. break;
  896. case 2:
  897. if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &temp, &lattr))
  898. return NULL;
  899. attr = lattr;
  900. break;
  901. case 3:
  902. if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &temp))
  903. return NULL;
  904. use_xy = TRUE;
  905. break;
  906. case 4:
  907. if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr", &y, &x, &temp, &lattr))
  908. return NULL;
  909. attr = lattr;
  910. use_xy = TRUE;
  911. break;
  912. default:
  913. PyErr_SetString(PyExc_TypeError, "insch requires 1 or 4 arguments");
  914. return NULL;
  915. }
  916. if (!PyCurses_ConvertToChtype(temp, &ch)) {
  917. PyErr_SetString(PyExc_TypeError,
  918. "argument 1 or 3 must be a ch or an int");
  919. return NULL;
  920. }
  921. if (use_xy == TRUE)
  922. rtn = mvwinsch(self->win,y,x, ch | attr);
  923. else {
  924. rtn = winsch(self->win, ch | attr);
  925. }
  926. return PyCursesCheckERR(rtn, "insch");
  927. }
  928. static PyObject *
  929. PyCursesWindow_InCh(PyCursesWindowObject *self, PyObject *args)
  930. {
  931. int x, y, rtn;
  932. switch (PyTuple_Size(args)) {
  933. case 0:
  934. rtn = winch(self->win);
  935. break;
  936. case 2:
  937. if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
  938. return NULL;
  939. rtn = mvwinch(self->win,y,x);
  940. break;
  941. default:
  942. PyErr_SetString(PyExc_TypeError, "inch requires 0 or 2 arguments");
  943. return NULL;
  944. }
  945. return PyInt_FromLong((long) rtn);
  946. }
  947. static PyObject *
  948. PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
  949. {
  950. int x, y, n;
  951. char rtn[1024]; /* This should be big enough.. I hope */
  952. int rtn2;
  953. switch (PyTuple_Size(args)) {
  954. case 0:
  955. rtn2 = winnstr(self->win,rtn, 1023);
  956. break;
  957. case 1:
  958. if (!PyArg_ParseTuple(args,"i;n", &n))
  959. return NULL;
  960. rtn2 = winnstr(self->win,rtn,MIN(n,1023));
  961. break;
  962. case 2:
  963. if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
  964. return NULL;
  965. rtn2 = mvwinnstr(self->win,y,x,rtn,1023);
  966. break;
  967. case 3:
  968. if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
  969. return NULL;
  970. rtn2 = mvwinnstr(self->win, y, x, rtn, MIN(n,1023));
  971. break;
  972. default:
  973. PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments");
  974. return NULL;
  975. }
  976. if (rtn2 == ERR)
  977. rtn[0] = 0;
  978. return PyString_FromString(rtn);
  979. }
  980. static PyObject *
  981. PyCursesWindow_InsStr(PyCursesWindowObject *self, PyObject *args)
  982. {
  983. int rtn;
  984. int x, y;
  985. char *str;
  986. attr_t attr = A_NORMAL , attr_old = A_NORMAL;
  987. long lattr;
  988. int use_xy = FALSE, use_attr = FALSE;
  989. switch (PyTuple_Size(args)) {
  990. case 1:
  991. if (!PyArg_ParseTuple(args,"s;str", &str))
  992. return NULL;
  993. break;
  994. case 2:
  995. if (!PyArg_ParseTuple(args,"sl;str,attr", &str, &lattr))
  996. return NULL;
  997. attr = lattr;
  998. use_attr = TRUE;
  999. break;
  1000. case 3:
  1001. if (!PyArg_ParseTuple(args,"iis;y,x,str", &y, &x, &str))
  1002. return NULL;
  1003. use_xy = TRUE;
  1004. break;
  1005. case 4:
  1006. if (!PyArg_ParseTuple(args,"iisl;y,x,str,attr", &y, &x, &str, &lattr))
  1007. return NULL;
  1008. attr = lattr;
  1009. use_xy = use_attr = TRUE;
  1010. break;
  1011. default:
  1012. PyErr_SetString(PyExc_TypeError, "insstr requires 1 to 4 arguments");
  1013. return NULL;
  1014. }
  1015. if (use_attr == TRUE) {
  1016. attr_old = getattrs(self->win);
  1017. (void)wattrset(self->win,attr);
  1018. }
  1019. if (use_xy == TRUE)
  1020. rtn = mvwinsstr(self->win,y,x,str);
  1021. else
  1022. rtn = winsstr(self->win,str);
  1023. if (use_attr == TRUE)
  1024. (void)wattrset(self->win,attr_old);
  1025. return PyCursesCheckERR(rtn, "insstr");
  1026. }
  1027. static PyObject *
  1028. PyCursesWindow_InsNStr(PyCursesWindowObject *self, PyObject *args)
  1029. {
  1030. int rtn, x, y, n;
  1031. char *str;
  1032. attr_t attr = A_NORMAL , attr_old = A_NORMAL;
  1033. long lattr;
  1034. int use_xy = FALSE, use_attr = FALSE;
  1035. switch (PyTuple_Size(args)) {
  1036. case 2:
  1037. if (!PyArg_ParseTuple(args,"si;str,n", &str, &n))
  1038. return NULL;
  1039. break;
  1040. case 3:
  1041. if (!PyArg_ParseTuple(args,"sil;str,n,attr", &str, &n, &lattr))
  1042. return NULL;
  1043. attr = lattr;
  1044. use_attr = TRUE;
  1045. break;
  1046. case 4:
  1047. if (!PyArg_ParseTuple(args,"iisi;y,x,str,n", &y, &x, &str, &n))
  1048. return NULL;
  1049. use_xy = TRUE;
  1050. break;
  1051. case 5:
  1052. if (!PyArg_ParseTuple(args,"iisil;y,x,str,n,attr", &y, &x, &str, &n, &lattr))
  1053. return NULL;
  1054. attr = lattr;
  1055. use_xy = use_attr = TRUE;
  1056. break;
  1057. default:
  1058. PyErr_SetString(PyExc_TypeError, "insnstr requires 2 to 5 arguments");
  1059. return NULL;
  1060. }
  1061. if (use_attr == TRUE) {
  1062. attr_old = getattrs(self->win);
  1063. (void)wattrset(self->win,attr);
  1064. }
  1065. if (use_xy == TRUE)
  1066. rtn = mvwinsnstr(self->win,y,x,str,n);
  1067. else
  1068. rtn = winsnstr(self->win,str,n);
  1069. if (use_attr == TRUE)
  1070. (void)wattrset(self->win,attr_old);
  1071. return PyCursesCheckERR(rtn, "insnstr");
  1072. }
  1073. static PyObject *
  1074. PyCursesWindow_Is_LineTouched(PyCursesWindowObject *self, PyObject *args)
  1075. {
  1076. int line, erg;
  1077. if (!PyArg_ParseTuple(args,"i;line", &line))
  1078. return NULL;
  1079. erg = is_linetouched(self->win, line);
  1080. if (erg == ERR) {
  1081. PyErr_SetString(PyExc_TypeError,
  1082. "is_linetouched: line number outside of boundaries");
  1083. return NULL;
  1084. } else
  1085. if (erg == FALSE) {
  1086. Py_INCREF(Py_False);
  1087. return Py_False;
  1088. } else {
  1089. Py_INCREF(Py_True);
  1090. return Py_True;
  1091. }
  1092. }
  1093. static PyObject *
  1094. PyCursesWindow_NoOutRefresh(PyCursesWindowObject *self, PyObject *args)
  1095. {
  1096. int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
  1097. int rtn;
  1098. #ifndef WINDOW_HAS_FLAGS
  1099. if (0)
  1100. #else
  1101. if (self->win->_flags & _ISPAD)
  1102. #endif
  1103. {
  1104. switch(PyTuple_Size(args)) {
  1105. case 6:
  1106. if (!PyArg_ParseTuple(args,
  1107. "iiiiii;" \
  1108. "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
  1109. &pminrow, &pmincol, &sminrow,
  1110. &smincol, &smaxrow, &smaxcol))
  1111. return NULL;
  1112. Py_BEGIN_ALLOW_THREADS
  1113. rtn = pnoutrefresh(self->win,
  1114. pminrow, pmincol, sminrow,
  1115. smincol, smaxrow, smaxcol);
  1116. Py_END_ALLOW_THREADS
  1117. return PyCursesCheckERR(rtn, "pnoutrefresh");
  1118. default:
  1119. PyErr_SetString(PyCursesError,
  1120. "noutrefresh() called for a pad "
  1121. "requires 6 arguments");
  1122. return NULL;
  1123. }
  1124. } else {
  1125. if (!PyArg_ParseTuple(args, ":noutrefresh"))
  1126. return NULL;
  1127. Py_BEGIN_ALLOW_THREADS
  1128. rtn = wnoutrefresh(self->win);
  1129. Py_END_ALLOW_THREADS
  1130. return PyCursesCheckERR(rtn, "wnoutrefresh");
  1131. }
  1132. }
  1133. static PyObject *
  1134. PyCursesWindow_Overlay(PyCursesWindowObject *self, PyObject *args)
  1135. {
  1136. PyCursesWindowObject *temp;
  1137. int use_copywin = FALSE;
  1138. int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
  1139. int rtn;
  1140. switch (PyTuple_Size(args)) {
  1141. case 1:
  1142. if (!PyArg_ParseTuple(args, "O!;window object",
  1143. &PyCursesWindow_Type, &temp))
  1144. return NULL;
  1145. break;
  1146. case 7:
  1147. if (!PyArg_ParseTuple(args, "O!iiiiii;window object, int, int, int, int, int, int",
  1148. &PyCursesWindow_Type, &temp, &sminrow, &smincol,
  1149. &dminrow, &dmincol, &dmaxrow, &dmaxcol))
  1150. return NULL;
  1151. use_copywin = TRUE;
  1152. break;
  1153. default:
  1154. PyErr_SetString(PyExc_TypeError,
  1155. "overlay requires one or seven arguments");
  1156. return NULL;
  1157. }
  1158. if (use_copywin == TRUE) {
  1159. rtn = copywin(self->win, temp->win, sminrow, smincol,
  1160. dminrow, dmincol, dmaxrow, dmaxcol, TRUE);
  1161. return PyCursesCheckERR(rtn, "copywin");
  1162. }
  1163. else {
  1164. rtn = overlay(self->win, temp->win);
  1165. return PyCursesCheckERR(rtn, "overlay");
  1166. }
  1167. }
  1168. static PyObject *
  1169. PyCursesWindow_Overwrite(PyCursesWindowObject *self, PyObject *args)
  1170. {
  1171. PyCursesWindowObject *temp;
  1172. int use_copywin = FALSE;
  1173. int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
  1174. int rtn;
  1175. switch (PyTuple_Size(args)) {
  1176. case 1:
  1177. if (!PyArg_ParseTuple(args, "O!;window object",
  1178. &PyCursesWindow_Type, &temp))
  1179. return NULL;
  1180. break;
  1181. case 7:
  1182. if (!PyArg_ParseTuple(args, "O!iiiiii;window object, int, int, int, int, int, int",
  1183. &PyCursesWindow_Type, &temp, &sminrow, &smincol,
  1184. &dminrow, &dmincol, &dmaxrow, &dmaxcol))
  1185. return NULL;
  1186. use_copywin = TRUE;
  1187. break;
  1188. default:
  1189. PyErr_SetString(PyExc_TypeError,
  1190. "overwrite requires one or seven arguments");
  1191. return NULL;
  1192. }
  1193. if (use_copywin == TRUE) {
  1194. rtn = copywin(self->win, temp->win, sminrow, smincol,
  1195. dminrow, dmincol, dmaxrow, dmaxcol, FALSE);
  1196. return PyCursesCheckERR(rtn, "copywin");
  1197. }
  1198. else {
  1199. rtn = overwrite(self->win, temp->win);
  1200. return PyCursesCheckERR(rtn, "overwrite");
  1201. }
  1202. }
  1203. static PyObject *
  1204. PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *args)
  1205. {
  1206. PyObject *temp;
  1207. if (!PyArg_ParseTuple(args, "O;fileobj", &temp))
  1208. return NULL;
  1209. if (!PyFile_Check(temp)) {
  1210. PyErr_SetString(PyExc_TypeError, "argument must be a file object");
  1211. return NULL;
  1212. }
  1213. return PyCursesCheckERR(putwin(self->win, PyFile_AsFile(temp)),
  1214. "putwin");
  1215. }
  1216. static PyObject *
  1217. PyCursesWindow_RedrawLine(PyCursesWindowObject *self, PyObject *args)
  1218. {
  1219. int beg, num;
  1220. if (!PyArg_ParseTuple(args, "ii;beg,num", &beg, &num))
  1221. return NULL;
  1222. return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln");
  1223. }
  1224. static PyObject *
  1225. PyCursesWindow_Refresh(PyCursesWindowObject *self, PyObject *args)
  1226. {
  1227. int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
  1228. int rtn;
  1229. #ifndef WINDOW_HAS_FLAGS
  1230. if (0)
  1231. #else
  1232. if (self->win->_flags & _ISPAD)
  1233. #endif
  1234. {
  1235. switch(PyTuple_Size(args)) {
  1236. case 6:
  1237. if (!PyArg_ParseTuple(args,
  1238. "iiiiii;" \
  1239. "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
  1240. &pminrow, &pmincol, &sminrow,
  1241. &smincol, &smaxrow, &smaxcol))
  1242. return NULL;
  1243. Py_BEGIN_ALLOW_THREADS
  1244. rtn = prefresh(self->win,
  1245. pminrow, pmincol, sminrow,
  1246. smincol, smaxrow, smaxcol);
  1247. Py_END_ALLOW_THREADS
  1248. return PyCursesCheckERR(rtn, "prefresh");
  1249. default:
  1250. PyErr_SetString(PyCursesError,
  1251. "refresh() for a pad requires 6 arguments");
  1252. return NULL;
  1253. }
  1254. } else {
  1255. if (!PyArg_ParseTuple(args, ":refresh"))
  1256. return NULL;
  1257. Py_BEGIN_ALLOW_THREADS
  1258. rtn = wrefresh(self->win);
  1259. Py_END_ALLOW_THREADS
  1260. return PyCursesCheckERR(rtn, "prefresh");
  1261. }
  1262. }
  1263. static PyObject *
  1264. PyCursesWindow_SetScrollRegion(PyCursesWindowObject *self, PyObject *args)
  1265. {
  1266. int x, y;
  1267. if (!PyArg_ParseTuple(args,"ii;top, bottom",&y,&x))
  1268. return NULL;
  1269. return PyCursesCheckERR(wsetscrreg(self->win,y,x), "wsetscrreg");
  1270. }
  1271. static PyObject *
  1272. PyCursesWindow_SubWin(PyCursesWindowObject *self, PyObject *args)
  1273. {
  1274. WINDOW *win;
  1275. int nlines, ncols, begin_y, begin_x;
  1276. nlines = 0;
  1277. ncols = 0;
  1278. switch (PyTuple_Size(args)) {
  1279. case 2:
  1280. if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
  1281. return NULL;
  1282. break;
  1283. case 4:
  1284. if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
  1285. &nlines,&ncols,&begin_y,&begin_x))
  1286. return NULL;
  1287. break;
  1288. default:
  1289. PyErr_SetString(PyExc_TypeError, "subwin requires 2 or 4 arguments");
  1290. return NULL;
  1291. }
  1292. /* printf("Subwin: %i %i %i %i \n", nlines, ncols, begin_y, begin_x); */
  1293. #ifdef WINDOW_HAS_FLAGS
  1294. if (self->win->_flags & _ISPAD)
  1295. win = subpad(self->win, nlines, ncols, begin_y, begin_x);
  1296. else
  1297. #endif
  1298. win = subwin(self->win, nlines, ncols, begin_y, begin_x);
  1299. if (win == NULL) {
  1300. PyErr_SetString(PyCursesError, catchall_NULL);
  1301. return NULL;
  1302. }
  1303. return (PyObject *)PyCursesWindow_New(win);
  1304. }
  1305. static PyObject *
  1306. PyCursesWindow_Scroll(PyCursesWindowObject *self, PyObject *args)
  1307. {
  1308. int nlines;
  1309. switch(PyTuple_Size(args)) {
  1310. case 0:
  1311. return PyCursesCheckERR(scroll(self->win), "scroll");
  1312. case 1:
  1313. if (!PyArg_ParseTuple(args, "i;nlines", &nlines))
  1314. return NULL;
  1315. return PyCursesCheckERR(wscrl(self->win, nlines), "scroll");
  1316. default:
  1317. PyErr_SetString(PyExc_TypeError, "scroll requires 0 or 1 arguments");
  1318. return NULL;
  1319. }
  1320. }
  1321. static PyObject *
  1322. PyCursesWindow_TouchLine(PyCursesWindowObject *self, PyObject *args)
  1323. {
  1324. int st, cnt, val;
  1325. switch (PyTuple_Size(args)) {
  1326. case 2:
  1327. if (!PyArg_ParseTuple(args,"ii;start,count",&st,&cnt))
  1328. return NULL;
  1329. return PyCursesCheckERR(touchline(self->win,st,cnt), "touchline");
  1330. case 3:
  1331. if (!PyArg_ParseTuple(args, "iii;start,count,val", &st, &cnt, &val))
  1332. return NULL;
  1333. return PyCursesCheckERR(wtouchln(self->win, st, cnt, val), "touchline");
  1334. default:
  1335. PyErr_SetString(PyExc_TypeError, "touchline requires 2 or 3 arguments");
  1336. return NULL;
  1337. }
  1338. }
  1339. static PyObject *
  1340. PyCursesWindow_Vline(PyCursesWindowObject *self, PyObject *args)
  1341. {
  1342. PyObject *temp;
  1343. chtype ch;
  1344. int n, x, y, code = OK;
  1345. attr_t attr = A_NORMAL;
  1346. long lattr;
  1347. switch (PyTuple_Size(args)) {
  1348. case 2:
  1349. if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
  1350. return NULL;
  1351. break;
  1352. case 3:
  1353. if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &lattr))
  1354. return NULL;
  1355. attr = lattr;
  1356. break;
  1357. case 4:
  1358. if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
  1359. return NULL;
  1360. code = wmove(self->win, y, x);
  1361. break;
  1362. case 5:
  1363. if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
  1364. &y, &x, &temp, &n, &lattr))
  1365. return NULL;
  1366. attr = lattr;
  1367. code = wmove(self->win, y, x);
  1368. break;
  1369. default:
  1370. PyErr_SetString(PyExc_TypeError, "vline requires 2 to 5 arguments");
  1371. return NULL;
  1372. }
  1373. if (code != ERR) {
  1374. if (!PyCurses_ConvertToChtype(temp, &ch)) {
  1375. PyErr_SetString(PyExc_TypeError,
  1376. "argument 1 or 3 must be a ch or an int");
  1377. return NULL;
  1378. }
  1379. return PyCursesCheckERR(wvline(self->win, ch | attr, n), "vline");
  1380. } else
  1381. return PyCursesCheckERR(code, "wmove");
  1382. }
  1383. static PyMethodDef PyCursesWindow_Methods[] = {
  1384. {"addch", (PyCFunction)PyCursesWindow_AddCh, METH_VARARGS},
  1385. {"addnstr", (PyCFunction)PyCursesWindow_AddNStr, METH_VARARGS},
  1386. {"addstr", (PyCFunction)PyCursesWindow_AddStr, METH_VARARGS},
  1387. {"attroff", (PyCFunction)PyCursesWindow_AttrOff, METH_VARARGS},
  1388. {"attron", (PyCFunction)PyCursesWindow_AttrOn, METH_VARARGS},
  1389. {"attrset", (PyCFunction)PyCursesWindow_AttrSet, METH_VARARGS},
  1390. {"bkgd", (PyCFunction)PyCursesWindow_Bkgd, METH_VARARGS},
  1391. {"chgat", (PyCFunction)PyCursesWindow_ChgAt, METH_VARARGS},
  1392. {"bkgdset", (PyCFunction)PyCursesWindow_BkgdSet, METH_VARARGS},
  1393. {"border", (PyCFunction)PyCursesWindow_Border, METH_VARARGS},
  1394. {"box", (PyCFunction)PyCursesWindow_Box, METH_VARARGS},
  1395. {"clear", (PyCFunction)PyCursesWindow_wclear, METH_NOARGS},
  1396. {"clearok", (PyCFunction)PyCursesWindow_clearok, METH_VARARGS},
  1397. {"clrtobot", (PyCFunction)PyCursesWindow_wclrtobot, METH_NOARGS},
  1398. {"clrtoeol", (PyCFunction)PyCursesWindow_wclrtoeol, METH_NOARGS},
  1399. {"cursyncup", (PyCFunction)PyCursesWindow_wcursyncup, METH_NOARGS},
  1400. {"delch", (PyCFunction)PyCursesWindow_DelCh, METH_VARARGS},
  1401. {"deleteln", (PyCFunction)PyCursesWindow_wdeleteln, METH_NOARGS},
  1402. {"derwin", (PyCFunction)PyCursesWindow_DerWin, METH_VARARGS},
  1403. {"echochar", (PyCFunction)PyCursesWindow_EchoChar, METH_VARARGS},
  1404. #ifdef NCURSES_MOUSE_VERSION
  1405. {"enclose", (PyCFunction)PyCursesWindow_Enclose, METH_VARARGS},
  1406. #endif
  1407. {"erase", (PyCFunction)PyCursesWindow_werase, METH_NOARGS},
  1408. {"getbegyx", (PyCFunction)PyCursesWindow_getbegyx, METH_NOARGS},
  1409. {"getbkgd", (PyCFunction)PyCursesWindow_GetBkgd, METH_NOARGS},
  1410. {"getch", (PyCFunction)PyCursesWindow_GetCh, METH_VARARGS},
  1411. {"getkey", (PyCFunction)PyCursesWindow_GetKey, METH_VARARGS},
  1412. {"getmaxyx", (PyCFunction)PyCursesWindow_getmaxyx, METH_NOARGS},
  1413. {"getparyx", (PyCFunction)PyCursesWindow_getparyx, METH_NOARGS},
  1414. {"getstr", (PyCFunction)PyCursesWindow_GetStr, METH_VARARGS},
  1415. {"getyx", (PyCFunction)PyCursesWindow_getyx, METH_NOARGS},
  1416. {"hline", (PyCFunction)PyCursesWindow_Hline, METH_VARARGS},
  1417. {"idcok", (PyCFunction)PyCursesWindow_idcok, METH_VARARGS},
  1418. {"idlok", (PyCFunction)PyCursesWindow_idlok, METH_VARARGS},
  1419. {"immedok", (PyCFunction)PyCursesWindow_immedok, METH_VARARGS},
  1420. {"inch", (PyCFunction)PyCursesWindow_InCh, METH_VARARGS},
  1421. {"insch", (PyCFunction)PyCursesWindow_InsCh, METH_VARARGS},
  1422. {"insdelln", (PyCFunction)PyCursesWindow_winsdelln, METH_VARARGS},
  1423. {"insertln", (PyCFunction)PyCursesWindow_winsertln, METH_NOARGS},
  1424. {"insnstr", (PyCFunction)PyCursesWindow_InsNStr, METH_VARARGS},
  1425. {"insstr", (PyCFunction)PyCursesWindow_InsStr, METH_VARARGS},
  1426. {"instr", (PyCFunction)PyCursesWindow_InStr, METH_VARARGS},
  1427. {"is_linetouched", (PyCFunction)PyCursesWindow_Is_LineTouched, METH_VARARGS},
  1428. {"is_wintouched", (PyCFunction)PyCursesWindow_is_wintouched, METH_NOARGS},
  1429. {"keypad", (PyCFunction)PyCursesWindow_keypad, METH_VARARGS},
  1430. {"leaveok", (PyCFunction)PyCursesWindow_leaveok, METH_VARARGS},
  1431. {"move", (PyCFunction)PyCursesWindow_wmove, METH_VARARGS},
  1432. {"mvderwin", (PyCFunction)PyCursesWindow_mvderwin, METH_VARARGS},
  1433. {"mvwin", (PyCFunction)PyCursesWindow_mvwin, METH_VARARGS},
  1434. {"nodelay", (PyCFunction)PyCursesWindow_nodelay, METH_VARARGS},
  1435. {"notimeout", (PyCFunction)PyCursesWindow_notimeout, METH_VARARGS},
  1436. {"noutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
  1437. /* Backward compatibility alias -- remove in Python 2.3 */
  1438. {"nooutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
  1439. {"overlay", (PyCFunction)PyCursesWindow_Overlay, METH_VARARGS},
  1440. {"overwrite", (PyCFunction)PyCursesWindow_Overwrite,
  1441. METH_VARARGS},
  1442. {"putwin", (PyCFunction)PyCursesWindow_PutWin, METH_VARARGS},
  1443. {"redrawln", (PyCFunction)PyCursesWindow_RedrawLine, METH_VARARGS},
  1444. {"redrawwin", (PyCFunction)PyCursesWindow_redrawwin, METH_NOARGS},
  1445. {"refresh", (PyCFunction)PyCursesWindow_Refresh, METH_VARARGS},
  1446. #ifndef STRICT_SYSV_CURSES
  1447. {"resize", (PyCFunction)PyCursesWindow_wresize, METH_VARARGS},
  1448. #endif
  1449. {"scroll", (PyCFunction)PyCursesWindow_Scroll, METH_VARARGS},
  1450. {"scrollok", (PyCFunction)PyCursesWindow_scrollok, METH_VARARGS},
  1451. {"setscrreg", (PyCFunction)PyCursesWindow_SetScrollRegion, METH_VARARGS},
  1452. {"standend", (PyCFunction)PyCursesWindow_wstandend, METH_NOARGS},
  1453. {"standout", (PyCFunction)PyCursesWindow_wstandout, METH_NOARGS},
  1454. {"subpad", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
  1455. {"subwin", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
  1456. {"syncdown", (PyCFunction)PyCursesWindow_wsyncdown, METH_NOARGS},
  1457. {"syncok", (PyCFunction)PyCursesWindow_syncok, METH_VARARGS},
  1458. {"syncup", (PyCFunction)PyCursesWindow_wsyncup, METH_NOARGS},
  1459. {"timeout", (PyCFunction)PyCursesWindow_wtimeout, METH_VARARGS},
  1460. {"touchline", (PyCFunction)PyCursesWindow_TouchLine, METH_VARARGS},
  1461. {"touchwin", (PyCFunction)PyCursesWindow_touchwin, METH_NOARGS},
  1462. {"untouchwin", (PyCFunction)PyCursesWindow_untouchwin, METH_NOARGS},
  1463. {"vline", (PyCFunction)PyCursesWindow_Vline, METH_VARARGS},
  1464. {NULL, NULL} /* sentinel */
  1465. };
  1466. static PyObject *
  1467. PyCursesWindow_GetAttr(PyCursesWindowObject *self, char *name)
  1468. {
  1469. return Py_FindMethod(PyCursesWindow_Methods, (PyObject *)self, name);
  1470. }
  1471. /* -------------------------------------------------------*/
  1472. PyTypeObject PyCursesWindow_Type = {
  1473. PyVarObject_HEAD_INIT(NULL, 0)
  1474. "_curses.curses window", /*tp_name*/
  1475. sizeof(PyCursesWindowObject), /*tp_basicsize*/
  1476. 0, /*tp_itemsize*/
  1477. /* methods */
  1478. (destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/
  1479. 0, /*tp_print*/
  1480. (getattrfunc)PyCursesWindow_GetAttr, /*tp_getattr*/
  1481. (setattrfunc)0, /*tp_setattr*/
  1482. 0, /*tp_compare*/
  1483. 0, /*tp_repr*/
  1484. 0, /*tp_as_number*/
  1485. 0, /*tp_as_sequence*/
  1486. 0, /*tp_as_mapping*/
  1487. 0, /*tp_hash*/
  1488. };
  1489. /*********************************************************************
  1490. Global Functions
  1491. **********************************************************************/
  1492. NoArgNoReturnFunction(beep)
  1493. NoArgNoReturnFunction(def_prog_mode)
  1494. NoArgNoReturnFunction(def_shell_mode)
  1495. NoArgNoReturnFunction(doupdate)
  1496. NoArgNoReturnFunction(endwin)
  1497. NoArgNoReturnFunction(flash)
  1498. NoArgNoReturnFunction(nocbreak)
  1499. NoArgNoReturnFunction(noecho)
  1500. NoArgNoReturnFunction(nonl)
  1501. NoArgNoReturnFunction(noraw)
  1502. NoArgNoReturnFunction(reset_prog_mode)
  1503. NoArgNoReturnFunction(reset_shell_mode)
  1504. NoArgNoReturnFunction(resetty)
  1505. NoArgNoReturnFunction(savetty)
  1506. NoArgOrFlagNoReturnFunction(cbreak)
  1507. NoArgOrFlagNoReturnFunction(echo)
  1508. NoArgOrFlagNoReturnFunction(nl)
  1509. NoArgOrFlagNoReturnFunction(raw)
  1510. NoArgReturnIntFunction(baudrate)
  1511. NoArgReturnIntFunction(termattrs)
  1512. NoArgReturnStringFunction(termname)
  1513. NoArgReturnStringFunction(longname)
  1514. NoArgTrueFalseFunction(can_change_color)
  1515. NoArgTrueFalseFunction(has_colors)
  1516. NoArgTrueFalseFunction(has_ic)
  1517. NoArgTrueFalseFunction(has_il)
  1518. NoArgTrueFalseFunction(isendwin)
  1519. NoArgNoReturnVoidFunction(flushinp)
  1520. NoArgNoReturnVoidFunction(noqiflush)
  1521. static PyObject *
  1522. PyCurses_filter(PyObject *self)
  1523. {
  1524. /* not checking for PyCursesInitialised here since filter() must
  1525. be called before initscr() */
  1526. filter();
  1527. Py_INCREF(Py_None);
  1528. return Py_None;
  1529. }
  1530. static PyObject *
  1531. PyCurses_Color_Content(PyObject *self, PyObject *args)
  1532. {
  1533. short color,r,g,b;
  1534. PyCursesInitialised;
  1535. PyCursesInitialisedColor;
  1536. if (!PyArg_ParseTuple(args, "h:color_content", &color)) return NULL;
  1537. if (color_content(color, &r, &g, &b) != ERR)
  1538. return Py_BuildValue("(iii)", r, g, b);
  1539. else {
  1540. PyErr_SetString(PyCursesError,
  1541. "Argument 1 was out of range. Check value of COLORS.");
  1542. return NULL;
  1543. }
  1544. }
  1545. static PyObject *
  1546. PyCurses_color_pair(PyObject *self, PyObject *args)
  1547. {
  1548. int n;
  1549. PyCursesInitialised;
  1550. PyCursesInitialisedColor;
  1551. if (!PyArg_ParseTuple(args, "i:color_pair", &n)) return NULL;
  1552. return PyInt_FromLong((long) (n << 8));
  1553. }
  1554. static PyObject *
  1555. PyCurses_Curs_Set(PyObject *self, PyObject *args)
  1556. {
  1557. int vis,erg;
  1558. PyCursesInitialised;
  1559. if (!PyArg_ParseTuple(args, "i:curs_set", &vis)) return NULL;
  1560. erg = curs_set(vis);
  1561. if (erg == ERR) return PyCursesCheckERR(erg, "curs_set");
  1562. return PyInt_FromLong((long) erg);
  1563. }
  1564. static PyObject *
  1565. PyCurses_Delay_Output(PyObject *self, PyObject *args)
  1566. {
  1567. int ms;
  1568. PyCursesInitialised;
  1569. if (!PyArg_ParseTuple(args, "i:delay_output", &ms)) return NULL;
  1570. return PyCursesCheckERR(delay_output(ms), "delay_output");
  1571. }
  1572. static PyObject *
  1573. PyCurses_EraseChar(PyObject *self)
  1574. {
  1575. char ch;
  1576. PyCursesInitialised;
  1577. ch = erasechar();
  1578. return PyString_FromStringAndSize(&ch, 1);
  1579. }
  1580. static PyObject *
  1581. PyCurses_getsyx(PyObject *self)
  1582. {
  1583. int x = 0;
  1584. int y = 0;
  1585. PyCursesInitialised;
  1586. getsyx(y, x);
  1587. return Py_BuildValue("(ii)", y, x);
  1588. }
  1589. #ifdef NCURSES_MOUSE_VERSION
  1590. static PyObject *
  1591. PyCurses_GetMouse(PyObject *self)
  1592. {
  1593. int rtn;
  1594. MEVENT event;
  1595. PyCursesInitialised;
  1596. rtn = getmouse( &event );
  1597. if (rtn == ERR) {
  1598. PyErr_SetString(PyCursesError, "getmouse() returned ERR");
  1599. return NULL;
  1600. }
  1601. return Py_BuildValue("(hiiil)",
  1602. (short)event.id,
  1603. event.x, event.y, event.z,
  1604. (long) event.bstate);
  1605. }
  1606. static PyObject *
  1607. PyCurses_UngetMouse(PyObject *self, PyObject *args)
  1608. {
  1609. MEVENT event;
  1610. PyCursesInitialised;
  1611. if (!PyArg_ParseTuple(args, "hiiil",
  1612. &event.id,
  1613. &event.x, &event.y, &event.z,
  1614. (int *) &event.bstate))
  1615. return NULL;
  1616. return PyCursesCheckERR(ungetmouse(&event), "ungetmouse");
  1617. }
  1618. #endif
  1619. static PyObject *
  1620. PyCurses_GetWin(PyCursesWindowObject *self, PyObject *temp)
  1621. {
  1622. WINDOW *win;
  1623. PyCursesInitialised;
  1624. if (!PyFile_Check(temp)) {
  1625. PyErr_SetString(PyExc_TypeError, "argument must be a file object");
  1626. return NULL;
  1627. }
  1628. win = getwin(PyFile_AsFile(temp));
  1629. if (win == NULL) {
  1630. PyErr_SetString(PyCursesError, catchall_NULL);
  1631. return NULL;
  1632. }
  1633. return PyCursesWindow_New(win);
  1634. }
  1635. static PyObject *
  1636. PyCurses_HalfDelay(PyObject *self, PyObject *args)
  1637. {
  1638. unsigned char tenths;
  1639. PyCursesInitialised;
  1640. if (!PyArg_ParseTuple(args, "b:halfdelay", &tenths)) return NULL;
  1641. return PyCursesCheckERR(halfdelay(tenths), "halfdelay");
  1642. }
  1643. #ifndef STRICT_SYSV_CURSES
  1644. /* No has_key! */
  1645. static PyObject * PyCurses_has_key(PyObject *self, PyObject *args)
  1646. {
  1647. int ch;
  1648. PyCursesInitialised;
  1649. if (!PyArg_ParseTuple(args,"i",&ch)) return NULL;
  1650. if (has_key(ch) == FALSE) {
  1651. Py_INCREF(Py_False);
  1652. return Py_False;
  1653. }
  1654. Py_INCREF(Py_True);
  1655. return Py_True;
  1656. }
  1657. #endif /* STRICT_SYSV_CURSES */
  1658. static PyObject *
  1659. PyCurses_Init_Color(PyObject *self, PyObject *args)
  1660. {
  1661. short color, r, g, b;
  1662. PyCursesInitialised;
  1663. PyCursesInitialisedColor;
  1664. switch(PyTuple_Size(args)) {
  1665. case 4:
  1666. if (!PyArg_ParseTuple(args, "hhhh;color,r,g,b", &color, &r, &g, &b)) return NULL;
  1667. break;
  1668. default:
  1669. PyErr_SetString(PyExc_TypeError, "init_color requires 4 arguments");
  1670. return NULL;
  1671. }
  1672. return PyCursesCheckERR(init_color(color, r, g, b), "init_color");
  1673. }
  1674. static PyObject *
  1675. PyCurses_Init_Pair(PyObject *self, PyObject *args)
  1676. {
  1677. short pair, f, b;
  1678. PyCursesInitialised;
  1679. PyCursesInitialisedColor;
  1680. if (PyTuple_Size(args) != 3) {
  1681. PyErr_SetString(PyExc_TypeError, "init_pair requires 3 arguments");
  1682. return NULL;
  1683. }
  1684. if (!PyArg_ParseTuple(args, "hhh;pair, f, b", &pair, &f, &b)) return NULL;
  1685. return PyCursesCheckERR(init_pair(pair, f, b), "init_pair");
  1686. }
  1687. static PyObject *ModDict;
  1688. static PyObject *
  1689. PyCurses_InitScr(PyObject *self)
  1690. {
  1691. WINDOW *win;
  1692. if (initialised == TRUE) {
  1693. wrefresh(stdscr);
  1694. return (PyObject *)PyCursesWindow_New(stdscr);
  1695. }
  1696. win = initscr();
  1697. if (win == NULL) {
  1698. PyErr_SetString(PyCursesError, catchall_NULL);
  1699. return NULL;
  1700. }
  1701. initialised = initialised_setupterm = TRUE;
  1702. /* This was moved from initcurses() because it core dumped on SGI,
  1703. where they're not defined until you've called initscr() */
  1704. #define SetDictInt(string,ch) \
  1705. do { \
  1706. PyObject *o = PyInt_FromLong((long) (ch)); \
  1707. if (o && PyDict_SetItemString(ModDict, string, o) == 0) { \
  1708. Py_DECREF(o); \
  1709. } \
  1710. } while (0)
  1711. /* Here are some graphic symbols you can use */
  1712. SetDictInt("ACS_ULCORNER", (ACS_ULCORNER));
  1713. SetDictInt("ACS_LLCORNER", (ACS_LLCORNER));
  1714. SetDictInt("ACS_URCORNER", (ACS_URCORNER));
  1715. SetDictInt("ACS_LRCORNER", (ACS_LRCORNER));
  1716. SetDictInt("ACS_LTEE", (ACS_LTEE));
  1717. SetDictInt("ACS_RTEE", (ACS_RTEE));
  1718. SetDictInt("ACS_BTEE", (ACS_BTEE));
  1719. SetDictInt("ACS_TTEE", (ACS_TTEE));
  1720. SetDictInt("ACS_HLINE", (ACS_HLINE));
  1721. SetDictInt("ACS_VLINE", (ACS_VLINE));
  1722. SetDictInt("ACS_PLUS", (ACS_PLUS));
  1723. #if !defined(__hpux) || defined(HAVE_NCURSES_H)
  1724. /* On HP/UX 11, these are of type cchar_t, which is not an
  1725. integral type. If this is a problem on more platforms, a
  1726. configure test should be added to determine whether ACS_S1
  1727. is of integral type. */
  1728. SetDictInt("ACS_S1", (ACS_S1));
  1729. SetDictInt("ACS_S9", (ACS_S9));
  1730. SetDictInt("ACS_DIAMOND", (ACS_DIAMOND));
  1731. SetDictInt("ACS_CKBOARD", (ACS_CKBOARD));
  1732. SetDictInt("ACS_DEGREE", (ACS_DEGREE));
  1733. SetDictInt("ACS_PLMINUS", (ACS_PLMINUS));
  1734. SetDictInt("ACS_BULLET", (ACS_BULLET));
  1735. SetDictInt("ACS_LARROW", (ACS_LARROW));
  1736. SetDictInt("ACS_RARROW", (ACS_RARROW));
  1737. SetDictInt("ACS_DARROW", (ACS_DARROW));
  1738. SetDictInt("ACS_UARROW", (ACS_UARROW));
  1739. SetDictInt("ACS_BOARD", (ACS_BOARD));
  1740. SetDictInt("ACS_LANTERN", (ACS_LANTERN));
  1741. SetDictInt("ACS_BLOCK", (ACS_BLOCK));
  1742. #endif
  1743. SetDictInt("ACS_BSSB", (ACS_ULCORNER));
  1744. SetDictInt("ACS_SSBB", (ACS_LLCORNER));
  1745. SetDictInt("ACS_BBSS", (ACS_URCORNER));
  1746. SetDictInt("ACS_SBBS", (ACS_LRCORNER));
  1747. SetDictInt("ACS_SBSS", (ACS_RTEE));
  1748. SetDictInt("ACS_SSSB", (ACS_LTEE));
  1749. SetDictInt("ACS_SSBS", (ACS_BTEE));
  1750. SetDictInt("ACS_BSSS", (ACS_TTEE));
  1751. SetDictInt("ACS_BSBS", (ACS_HLINE));
  1752. SetDictInt("ACS_SBSB", (ACS_VLINE));
  1753. SetDictInt("ACS_SSSS", (ACS_PLUS));
  1754. /* The following are never available with strict SYSV curses */
  1755. #ifdef ACS_S3
  1756. SetDictInt("ACS_S3", (ACS_S3));
  1757. #endif
  1758. #ifdef ACS_S7
  1759. SetDictInt("ACS_S7", (ACS_S7));
  1760. #endif
  1761. #ifdef ACS_LEQUAL
  1762. SetDictInt("ACS_LEQUAL", (ACS_LEQUAL));
  1763. #endif
  1764. #ifdef ACS_GEQUAL
  1765. SetDictInt("ACS_GEQUAL", (ACS_GEQUAL));
  1766. #endif
  1767. #ifdef ACS_PI
  1768. SetDictInt("ACS_PI", (ACS_PI));
  1769. #endif
  1770. #ifdef ACS_NEQUAL
  1771. SetDictInt("ACS_NEQUAL", (ACS_NEQUAL));
  1772. #endif
  1773. #ifdef ACS_STERLING
  1774. SetDictInt("ACS_STERLING", (ACS_STERLING));
  1775. #endif
  1776. SetDictInt("LINES", LINES);
  1777. SetDictInt("COLS", COLS);
  1778. return (PyObject *)PyCursesWindow_New(win);
  1779. }
  1780. static PyObject *
  1781. PyCurses_setupterm(PyObject* self, PyObject *args, PyObject* keywds)
  1782. {
  1783. int fd = -1;
  1784. int err;
  1785. char* termstr = NULL;
  1786. static char *kwlist[] = {"term", "fd", NULL};
  1787. if (!PyArg_ParseTupleAndKeywords(
  1788. args, keywds, "|zi:setupterm", kwlist, &termstr, &fd)) {
  1789. return NULL;
  1790. }
  1791. if (fd == -1) {
  1792. PyObject* sys_stdout;
  1793. sys_stdout = PySys_GetObject("stdout");
  1794. if (sys_stdout == NULL) {
  1795. PyErr_SetString(
  1796. PyCursesError,
  1797. "lost sys.stdout");
  1798. return NULL;
  1799. }
  1800. fd = PyObject_AsFileDescriptor(sys_stdout);
  1801. if (fd == -1) {
  1802. return NULL;
  1803. }
  1804. }
  1805. if (!initialised_setupterm && setupterm(termstr,fd,&err) == ERR) {
  1806. char* s = "setupterm: unknown error";
  1807. if (err == 0) {
  1808. s = "setupterm: could not find terminal";
  1809. } else if (err == -1) {
  1810. s = "setupterm: could not find terminfo database";
  1811. }
  1812. PyErr_SetString(PyCursesError,s);
  1813. return NULL;
  1814. }
  1815. initialised_setupterm = TRUE;
  1816. Py_INCREF(Py_None);
  1817. return Py_None;
  1818. }
  1819. static PyObject *
  1820. PyCurses_IntrFlush(PyObject *self, PyObject *args)
  1821. {
  1822. int ch;
  1823. PyCursesInitialised;
  1824. switch(PyTuple_Size(args)) {
  1825. case 1:
  1826. if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL;
  1827. break;
  1828. default:
  1829. PyErr_SetString(PyExc_TypeError, "intrflush requires 1 argument");
  1830. return NULL;
  1831. }
  1832. return PyCursesCheckERR(intrflush(NULL,ch), "intrflush");
  1833. }
  1834. #ifdef HAVE_CURSES_IS_TERM_RESIZED
  1835. static PyObject *
  1836. PyCurses_Is_Term_Resized(PyObject *self, PyObject *args)
  1837. {
  1838. int lines;
  1839. int columns;
  1840. int result;
  1841. PyCursesInitialised;
  1842. if (!PyArg_ParseTuple(args,"ii:is_term_resized", &lines, &columns))
  1843. return NULL;
  1844. result = is_term_resized(lines, columns);
  1845. if (result == TRUE) {
  1846. Py_INCREF(Py_True);
  1847. return Py_True;
  1848. } else {
  1849. Py_INCREF(Py_False);
  1850. return Py_False;
  1851. }
  1852. }
  1853. #endif /* HAVE_CURSES_IS_TERM_RESIZED */
  1854. #if !defined(__NetBSD__)
  1855. static PyObject *
  1856. PyCurses_KeyName(PyObject *self, PyObject *args)
  1857. {
  1858. const char *knp;
  1859. int ch;
  1860. PyCursesInitialised;
  1861. if (!PyArg_ParseTuple(args,"i",&ch)) return NULL;
  1862. if (ch < 0) {
  1863. PyErr_SetString(PyExc_ValueError, "invalid key number");
  1864. return NULL;
  1865. }
  1866. knp = keyname(ch);
  1867. return PyString_FromString((knp == NULL) ? "" : (char *)knp);
  1868. }
  1869. #endif
  1870. static PyObject *
  1871. PyCurses_KillChar(PyObject *self)
  1872. {
  1873. char ch;
  1874. ch = killchar();
  1875. return PyString_FromStringAndSize(&ch, 1);
  1876. }
  1877. static PyObject *
  1878. PyCurses_Meta(PyObject *self, PyObject *args)
  1879. {
  1880. int ch;
  1881. PyCursesInitialised;
  1882. switch(PyTuple_Size(args)) {
  1883. case 1:
  1884. if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL;
  1885. break;
  1886. default:
  1887. PyErr_SetString(PyExc_TypeError, "meta requires 1 argument");
  1888. return NULL;
  1889. }
  1890. return PyCursesCheckERR(meta(stdscr, ch), "meta");
  1891. }
  1892. #ifdef NCURSES_MOUSE_VERSION
  1893. static PyObject *
  1894. PyCurses_MouseInterval(PyObject *self, PyObject *args)
  1895. {
  1896. int interval;
  1897. PyCursesInitialised;
  1898. if (!PyArg_ParseTuple(args,"i;interval",&interval))
  1899. return NULL;
  1900. return PyCursesCheckERR(mouseinterval(interval), "mouseinterval");
  1901. }
  1902. static PyObject *
  1903. PyCurses_MouseMask(PyObject *self, PyObject *args)
  1904. {
  1905. int newmask;
  1906. mmask_t oldmask, availmask;
  1907. PyCursesInitialised;
  1908. if (!PyArg_ParseTuple(args,"i;mousemask",&newmask))
  1909. return NULL;
  1910. availmask = mousemask(newmask, &oldmask);
  1911. return Py_BuildValue("(ll)", (long)availmask, (long)oldmask);
  1912. }
  1913. #endif
  1914. static PyObject *
  1915. PyCurses_Napms(PyObject *self, PyObject *args)
  1916. {
  1917. int ms;
  1918. PyCursesInitialised;
  1919. if (!PyArg_ParseTuple(args, "i;ms", &ms)) return NULL;
  1920. return Py_BuildValue("i", napms(ms));
  1921. }
  1922. static PyObject *
  1923. PyCurses_NewPad(PyObject *self, PyObject *args)
  1924. {
  1925. WINDOW *win;
  1926. int nlines, ncols;
  1927. PyCursesInitialised;
  1928. if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols)) return NULL;
  1929. win = newpad(nlines, ncols);
  1930. if (win == NULL) {
  1931. PyErr_SetString(PyCursesError, catchall_NULL);
  1932. return NULL;
  1933. }
  1934. return (PyObject *)PyCursesWindow_New(win);
  1935. }
  1936. static PyObject *
  1937. PyCurses_NewWindow(PyObject *self, PyObject *args)
  1938. {
  1939. WINDOW *win;
  1940. int nlines, ncols, begin_y=0, begin_x=0;
  1941. PyCursesInitialised;
  1942. switch (PyTuple_Size(args)) {
  1943. case 2:
  1944. if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols))
  1945. return NULL;
  1946. break;
  1947. case 4:
  1948. if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
  1949. &nlines,&ncols,&begin_y,&begin_x))
  1950. return NULL;
  1951. break;
  1952. default:
  1953. PyErr_SetString(PyExc_TypeError, "newwin requires 2 or 4 arguments");
  1954. return NULL;
  1955. }
  1956. win = newwin(nlines,ncols,begin_y,begin_x);
  1957. if (win == NULL) {
  1958. PyErr_SetString(PyCursesError, catchall_NULL);
  1959. return NULL;
  1960. }
  1961. return (PyObject *)PyCursesWindow_New(win);
  1962. }
  1963. static PyObject *
  1964. PyCurses_Pair_Content(PyObject *self, PyObject *args)
  1965. {
  1966. short pair,f,b;
  1967. PyCursesInitialised;
  1968. PyCursesInitialisedColor;
  1969. switch(PyTuple_Size(args)) {
  1970. case 1:
  1971. if (!PyArg_ParseTuple(args, "h;pair", &pair)) return NULL;
  1972. break;
  1973. default:
  1974. PyErr_SetString(PyExc_TypeError, "pair_content requires 1 argument");
  1975. return NULL;
  1976. }
  1977. if (pair_content(pair, &f, &b)==ERR) {
  1978. PyErr_SetString(PyCursesError,
  1979. "Argument 1 was out of range. (1..COLOR_PAIRS-1)");
  1980. return NULL;
  1981. }
  1982. return Py_BuildValue("(ii)", f, b);
  1983. }
  1984. static PyObject *
  1985. PyCurses_pair_number(PyObject *self, PyObject *args)
  1986. {
  1987. int n;
  1988. PyCursesInitialised;
  1989. PyCursesInitialisedColor;
  1990. switch(PyTuple_Size(args)) {
  1991. case 1:
  1992. if (!PyArg_ParseTuple(args, "i;pairvalue", &n)) return NULL;
  1993. break;
  1994. default:
  1995. PyErr_SetString(PyExc_TypeError,
  1996. "pair_number requires 1 argument");
  1997. return NULL;
  1998. }
  1999. return PyInt_FromLong((long) ((n & A_COLOR) >> 8));
  2000. }
  2001. static PyObject *
  2002. PyCurses_Putp(PyObject *self, PyObject *args)
  2003. {
  2004. char *str;
  2005. if (!PyArg_ParseTuple(args,"s;str", &str)) return NULL;
  2006. return PyCursesCheckERR(putp(str), "putp");
  2007. }
  2008. static PyObject *
  2009. PyCurses_QiFlush(PyObject *self, PyObject *args)
  2010. {
  2011. int flag = 0;
  2012. PyCursesInitialised;
  2013. switch(PyTuple_Size(args)) {
  2014. case 0:
  2015. qiflush();
  2016. Py_INCREF(Py_None);
  2017. return Py_None;
  2018. case 1:
  2019. if (!PyArg_ParseTuple(args, "i;True(1) or False(0)", &flag)) return NULL;
  2020. if (flag) qiflush();
  2021. else noqiflush();
  2022. Py_INCREF(Py_None);
  2023. return Py_None;
  2024. default:
  2025. PyErr_SetString(PyExc_TypeError, "qiflush requires 0 or 1 arguments");
  2026. return NULL;
  2027. }
  2028. }
  2029. /* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES
  2030. * and _curses.COLS */
  2031. #if defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM)
  2032. static int
  2033. update_lines_cols(void)
  2034. {
  2035. PyObject *o;
  2036. PyObject *m = PyImport_ImportModuleNoBlock("curses");
  2037. if (!m)
  2038. return 0;
  2039. o = PyInt_FromLong(LINES);
  2040. if (!o) {
  2041. Py_DECREF(m);
  2042. return 0;
  2043. }
  2044. if (PyObject_SetAttrString(m, "LINES", o)) {
  2045. Py_DECREF(m);
  2046. Py_DECREF(o);
  2047. return 0;
  2048. }
  2049. if (PyDict_SetItemString(ModDict, "LINES", o)) {
  2050. Py_DECREF(m);
  2051. Py_DECREF(o);
  2052. return 0;
  2053. }
  2054. Py_DECREF(o);
  2055. o = PyInt_FromLong(COLS);
  2056. if (!o) {
  2057. Py_DECREF(m);
  2058. return 0;
  2059. }
  2060. if (PyObject_SetAttrString(m, "COLS", o)) {
  2061. Py_DECREF(m);
  2062. Py_DECREF(o);
  2063. return 0;
  2064. }
  2065. if (PyDict_SetItemString(ModDict, "COLS", o)) {
  2066. Py_DECREF(m);
  2067. Py_DECREF(o);
  2068. return 0;
  2069. }
  2070. Py_DECREF(o);
  2071. Py_DECREF(m);
  2072. return 1;
  2073. }
  2074. #endif
  2075. #ifdef HAVE_CURSES_RESIZETERM
  2076. static PyObject *
  2077. PyCurses_ResizeTerm(PyObject *self, PyObject *args)
  2078. {
  2079. int lines;
  2080. int columns;
  2081. PyObject *result;
  2082. PyCursesInitialised;
  2083. if (!PyArg_ParseTuple(args,"ii:resizeterm", &lines, &columns))
  2084. return NULL;
  2085. result = PyCursesCheckERR(resizeterm(lines, columns), "resizeterm");
  2086. if (!result)
  2087. return NULL;
  2088. if (!update_lines_cols())
  2089. return NULL;
  2090. return result;
  2091. }
  2092. #endif
  2093. #ifdef HAVE_CURSES_RESIZE_TERM
  2094. static PyObject *
  2095. PyCurses_Resize_Term(PyObject *self, PyObject *args)
  2096. {
  2097. int lines;
  2098. int columns;
  2099. PyObject *result;
  2100. PyCursesInitialised;
  2101. if (!PyArg_ParseTuple(args,"ii:resize_term", &lines, &columns))
  2102. return NULL;
  2103. result = PyCursesCheckERR(resize_term(lines, columns), "resize_term");
  2104. if (!result)
  2105. return NULL;
  2106. if (!update_lines_cols())
  2107. return NULL;
  2108. return result;
  2109. }
  2110. #endif /* HAVE_CURSES_RESIZE_TERM */
  2111. static PyObject *
  2112. PyCurses_setsyx(PyObject *self, PyObject *args)
  2113. {
  2114. int y,x;
  2115. PyCursesInitialised;
  2116. if (PyTuple_Size(args)!=2) {
  2117. PyErr_SetString(PyExc_TypeError, "setsyx requires 2 arguments");
  2118. return NULL;
  2119. }
  2120. if (!PyArg_ParseTuple(args, "ii;y, x", &y, &x)) return NULL;
  2121. setsyx(y,x);
  2122. Py_INCREF(Py_None);
  2123. return Py_None;
  2124. }
  2125. static PyObject *
  2126. PyCurses_Start_Color(PyObject *self)
  2127. {
  2128. int code;
  2129. PyObject *c, *cp;
  2130. PyCursesInitialised;
  2131. code = start_color();
  2132. if (code != ERR) {
  2133. initialisedcolors = TRUE;
  2134. c = PyInt_FromLong((long) COLORS);
  2135. PyDict_SetItemString(ModDict, "COLORS", c);
  2136. Py_DECREF(c);
  2137. cp = PyInt_FromLong((long) COLOR_PAIRS);
  2138. PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp);
  2139. Py_DECREF(cp);
  2140. Py_INCREF(Py_None);
  2141. return Py_None;
  2142. } else {
  2143. PyErr_SetString(PyCursesError, "start_color() returned ERR");
  2144. return NULL;
  2145. }
  2146. }
  2147. static PyObject *
  2148. PyCurses_tigetflag(PyObject *self, PyObject *args)
  2149. {
  2150. char *capname;
  2151. PyCursesSetupTermCalled;
  2152. if (!PyArg_ParseTuple(args, "s", &capname))
  2153. return NULL;
  2154. return PyInt_FromLong( (long) tigetflag( capname ) );
  2155. }
  2156. static PyObject *
  2157. PyCurses_tigetnum(PyObject *self, PyObject *args)
  2158. {
  2159. char *capname;
  2160. PyCursesSetupTermCalled;
  2161. if (!PyArg_ParseTuple(args, "s", &capname))
  2162. return NULL;
  2163. return PyInt_FromLong( (long) tigetnum( capname ) );
  2164. }
  2165. static PyObject *
  2166. PyCurses_tigetstr(PyObject *self, PyObject *args)
  2167. {
  2168. char *capname;
  2169. PyCursesSetupTermCalled;
  2170. if (!PyArg_ParseTuple(args, "s", &capname))
  2171. return NULL;
  2172. capname = tigetstr( capname );
  2173. if (capname == 0 || capname == (char*) -1) {
  2174. Py_INCREF(Py_None);
  2175. return Py_None;
  2176. }
  2177. return PyString_FromString( capname );
  2178. }
  2179. static PyObject *
  2180. PyCurses_tparm(PyObject *self, PyObject *args)
  2181. {
  2182. char* fmt;
  2183. char* result = NULL;
  2184. int i1=0,i2=0,i3=0,i4=0,i5=0,i6=0,i7=0,i8=0,i9=0;
  2185. PyCursesSetupTermCalled;
  2186. if (!PyArg_ParseTuple(args, "s|iiiiiiiii:tparm",
  2187. &fmt, &i1, &i2, &i3, &i4,
  2188. &i5, &i6, &i7, &i8, &i9)) {
  2189. return NULL;
  2190. }
  2191. result = tparm(fmt,i1,i2,i3,i4,i5,i6,i7,i8,i9);
  2192. if (!result) {
  2193. PyErr_SetString(PyCursesError, "tparm() returned NULL");
  2194. return NULL;
  2195. }
  2196. return PyString_FromString(result);
  2197. }
  2198. static PyObject *
  2199. PyCurses_TypeAhead(PyObject *self, PyObject *args)
  2200. {
  2201. int fd;
  2202. PyCursesInitialised;
  2203. if (!PyArg_ParseTuple(args,"i;fd",&fd)) return NULL;
  2204. return PyCursesCheckERR(typeahead( fd ), "typeahead");
  2205. }
  2206. static PyObject *
  2207. PyCurses_UnCtrl(PyObject *self, PyObject *args)
  2208. {
  2209. PyObject *temp;
  2210. chtype ch;
  2211. PyCursesInitialised;
  2212. if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
  2213. if (PyInt_Check(temp))
  2214. ch = (chtype) PyInt_AsLong(temp);
  2215. else if (PyString_Check(temp))
  2216. ch = (chtype) *PyString_AsString(temp);
  2217. else {
  2218. PyErr_SetString(PyExc_TypeError, "argument must be a ch or an int");
  2219. return NULL;
  2220. }
  2221. return PyString_FromString(unctrl(ch));
  2222. }
  2223. static PyObject *
  2224. PyCurses_UngetCh(PyObject *self, PyObject *args)
  2225. {
  2226. PyObject *temp;
  2227. int ch;
  2228. PyCursesInitialised;
  2229. if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
  2230. if (PyInt_Check(temp))
  2231. ch = (int) PyInt_AsLong(temp);
  2232. else if (PyString_Check(temp))
  2233. ch = (int) *PyString_AsString(temp);
  2234. else {
  2235. PyErr_SetString(PyExc_TypeError, "argument must be a ch or an int");
  2236. return NULL;
  2237. }
  2238. return PyCursesCheckERR(ungetch(ch), "ungetch");
  2239. }
  2240. static PyObject *
  2241. PyCurses_Use_Env(PyObject *self, PyObject *args)
  2242. {
  2243. int flag;
  2244. switch(PyTuple_Size(args)) {
  2245. case 1:
  2246. if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&flag))
  2247. return NULL;
  2248. break;
  2249. default:
  2250. PyErr_SetString(PyExc_TypeError, "use_env requires 1 argument");
  2251. return NULL;
  2252. }
  2253. use_env(flag);
  2254. Py_INCREF(Py_None);
  2255. return Py_None;
  2256. }
  2257. #ifndef STRICT_SYSV_CURSES
  2258. static PyObject *
  2259. PyCurses_Use_Default_Colors(PyObject *self)
  2260. {
  2261. int code;
  2262. PyCursesInitialised;
  2263. PyCursesInitialisedColor;
  2264. code = use_default_colors();
  2265. if (code != ERR) {
  2266. Py_INCREF(Py_None);
  2267. return Py_None;
  2268. } else {
  2269. PyErr_SetString(PyCursesError, "use_default_colors() returned ERR");
  2270. return NULL;
  2271. }
  2272. }
  2273. #endif /* STRICT_SYSV_CURSES */
  2274. /* List of functions defined in the module */
  2275. static PyMethodDef PyCurses_methods[] = {
  2276. {"baudrate", (PyCFunction)PyCurses_baudrate, METH_NOARGS},
  2277. {"beep", (PyCFunction)PyCurses_beep, METH_NOARGS},
  2278. {"can_change_color", (PyCFunction)PyCurses_can_change_color, METH_NOARGS},
  2279. {"cbreak", (PyCFunction)PyCurses_cbreak, METH_VARARGS},
  2280. {"color_content", (PyCFunction)PyCurses_Color_Content, METH_VARARGS},
  2281. {"color_pair", (PyCFunction)PyCurses_color_pair, METH_VARARGS},
  2282. {"curs_set", (PyCFunction)PyCurses_Curs_Set, METH_VARARGS},
  2283. {"def_prog_mode", (PyCFunction)PyCurses_def_prog_mode, METH_NOARGS},
  2284. {"def_shell_mode", (PyCFunction)PyCurses_def_shell_mode, METH_NOARGS},
  2285. {"delay_output", (PyCFunction)PyCurses_Delay_Output, METH_VARARGS},
  2286. {"doupdate", (PyCFunction)PyCurses_doupdate, METH_NOARGS},
  2287. {"echo", (PyCFunction)PyCurses_echo, METH_VARARGS},
  2288. {"endwin", (PyCFunction)PyCurses_endwin, METH_NOARGS},
  2289. {"erasechar", (PyCFunction)PyCurses_EraseChar, METH_NOARGS},
  2290. {"filter", (PyCFunction)PyCurses_filter, METH_NOARGS},
  2291. {"flash", (PyCFunction)PyCurses_flash, METH_NOARGS},
  2292. {"flushinp", (PyCFunction)PyCurses_flushinp, METH_NOARGS},
  2293. #ifdef NCURSES_MOUSE_VERSION
  2294. {"getmouse", (PyCFunction)PyCurses_GetMouse, METH_NOARGS},
  2295. {"ungetmouse", (PyCFunction)PyCurses_UngetMouse, METH_VARARGS},
  2296. #endif
  2297. {"getsyx", (PyCFunction)PyCurses_getsyx, METH_NOARGS},
  2298. {"getwin", (PyCFunction)PyCurses_GetWin, METH_O},
  2299. {"has_colors", (PyCFunction)PyCurses_has_colors, METH_NOARGS},
  2300. {"has_ic", (PyCFunction)PyCurses_has_ic, METH_NOARGS},
  2301. {"has_il", (PyCFunction)PyCurses_has_il, METH_NOARGS},
  2302. #ifndef STRICT_SYSV_CURSES
  2303. {"has_key", (PyCFunction)PyCurses_has_key, METH_VARARGS},
  2304. #endif
  2305. {"halfdelay", (PyCFunction)PyCurses_HalfDelay, METH_VARARGS},
  2306. {"init_color", (PyCFunction)PyCurses_Init_Color, METH_VARARGS},
  2307. {"init_pair", (PyCFunction)PyCurses_Init_Pair, METH_VARARGS},
  2308. {"initscr", (PyCFunction)PyCurses_InitScr, METH_NOARGS},
  2309. {"intrflush", (PyCFunction)PyCurses_IntrFlush, METH_VARARGS},
  2310. {"isendwin", (PyCFunction)PyCurses_isendwin, METH_NOARGS},
  2311. #ifdef HAVE_CURSES_IS_TERM_RESIZED
  2312. {"is_term_resized", (PyCFunction)PyCurses_Is_Term_Resized, METH_VARARGS},
  2313. #endif
  2314. #if !defined(__NetBSD__)
  2315. {"keyname", (PyCFunction)PyCurses_KeyName, METH_VARARGS},
  2316. #endif
  2317. {"killchar", (PyCFunction)PyCurses_KillChar, METH_NOARGS},
  2318. {"longname", (PyCFunction)PyCurses_longname, METH_NOARGS},
  2319. {"meta", (PyCFunction)PyCurses_Meta, METH_VARARGS},
  2320. #ifdef NCURSES_MOUSE_VERSION
  2321. {"mouseinterval", (PyCFunction)PyCurses_MouseInterval, METH_VARARGS},
  2322. {"mousemask", (PyCFunction)PyCurses_MouseMask, METH_VARARGS},
  2323. #endif
  2324. {"napms", (PyCFunction)PyCurses_Napms, METH_VARARGS},
  2325. {"newpad", (PyCFunction)PyCurses_NewPad, METH_VARARGS},
  2326. {"newwin", (PyCFunction)PyCurses_NewWindow, METH_VARARGS},
  2327. {"nl", (PyCFunction)PyCurses_nl, METH_VARARGS},
  2328. {"nocbreak", (PyCFunction)PyCurses_nocbreak, METH_NOARGS},
  2329. {"noecho", (PyCFunction)PyCurses_noecho, METH_NOARGS},
  2330. {"nonl", (PyCFunction)PyCurses_nonl, METH_NOARGS},
  2331. {"noqiflush", (PyCFunction)PyCurses_noqiflush, METH_NOARGS},
  2332. {"noraw", (PyCFunction)PyCurses_noraw, METH_NOARGS},
  2333. {"pair_content", (PyCFunction)PyCurses_Pair_Content, METH_VARARGS},
  2334. {"pair_number", (PyCFunction)PyCurses_pair_number, METH_VARARGS},
  2335. {"putp", (PyCFunction)PyCurses_Putp, METH_VARARGS},
  2336. {"qiflush", (PyCFunction)PyCurses_QiFlush, METH_VARARGS},
  2337. {"raw", (PyCFunction)PyCurses_raw, METH_VARARGS},
  2338. {"reset_prog_mode", (PyCFunction)PyCurses_reset_prog_mode, METH_NOARGS},
  2339. {"reset_shell_mode", (PyCFunction)PyCurses_reset_shell_mode, METH_NOARGS},
  2340. {"resetty", (PyCFunction)PyCurses_resetty, METH_NOARGS},
  2341. #ifdef HAVE_CURSES_RESIZETERM
  2342. {"resizeterm", (PyCFunction)PyCurses_ResizeTerm, METH_VARARGS},
  2343. #endif
  2344. #ifdef HAVE_CURSES_RESIZE_TERM
  2345. {"resize_term", (PyCFunction)PyCurses_Resize_Term, METH_VARARGS},
  2346. #endif
  2347. {"savetty", (PyCFunction)PyCurses_savetty, METH_NOARGS},
  2348. {"setsyx", (PyCFunction)PyCurses_setsyx, METH_VARARGS},
  2349. {"setupterm", (PyCFunction)PyCurses_setupterm,
  2350. METH_VARARGS|METH_KEYWORDS},
  2351. {"start_color", (PyCFunction)PyCurses_Start_Color, METH_NOARGS},
  2352. {"termattrs", (PyCFunction)PyCurses_termattrs, METH_NOARGS},
  2353. {"termname", (PyCFunction)PyCurses_termname, METH_NOARGS},
  2354. {"tigetflag", (PyCFunction)PyCurses_tigetflag, METH_VARARGS},
  2355. {"tigetnum", (PyCFunction)PyCurses_tigetnum, METH_VARARGS},
  2356. {"tigetstr", (PyCFunction)PyCurses_tigetstr, METH_VARARGS},
  2357. {"tparm", (PyCFunction)PyCurses_tparm, METH_VARARGS},
  2358. {"typeahead", (PyCFunction)PyCurses_TypeAhead, METH_VARARGS},
  2359. {"unctrl", (PyCFunction)PyCurses_UnCtrl, METH_VARARGS},
  2360. {"ungetch", (PyCFunction)PyCurses_UngetCh, METH_VARARGS},
  2361. {"use_env", (PyCFunction)PyCurses_Use_Env, METH_VARARGS},
  2362. #ifndef STRICT_SYSV_CURSES
  2363. {"use_default_colors", (PyCFunction)PyCurses_Use_Default_Colors, METH_NOARGS},
  2364. #endif
  2365. {NULL, NULL} /* sentinel */
  2366. };
  2367. /* Initialization function for the module */
  2368. PyMODINIT_FUNC
  2369. init_curses(void)
  2370. {
  2371. PyObject *m, *d, *v, *c_api_object;
  2372. static void *PyCurses_API[PyCurses_API_pointers];
  2373. /* Initialize object type */
  2374. Py_TYPE(&PyCursesWindow_Type) = &PyType_Type;
  2375. /* Initialize the C API pointer array */
  2376. PyCurses_API[0] = (void *)&PyCursesWindow_Type;
  2377. PyCurses_API[1] = (void *)func_PyCursesSetupTermCalled;
  2378. PyCurses_API[2] = (void *)func_PyCursesInitialised;
  2379. PyCurses_API[3] = (void *)func_PyCursesInitialisedColor;
  2380. /* Create the module and add the functions */
  2381. m = Py_InitModule("_curses", PyCurses_methods);
  2382. if (m == NULL)
  2383. return;
  2384. /* Add some symbolic constants to the module */
  2385. d = PyModule_GetDict(m);
  2386. if (d == NULL)
  2387. return;
  2388. ModDict = d; /* For PyCurses_InitScr to use later */
  2389. /* Add a capsule for the C API */
  2390. c_api_object = PyCapsule_New(PyCurses_API, PyCurses_CAPSULE_NAME, NULL);
  2391. PyDict_SetItemString(d, "_C_API", c_api_object);
  2392. Py_DECREF(c_api_object);
  2393. /* For exception curses.error */
  2394. PyCursesError = PyErr_NewException("_curses.error", NULL, NULL);
  2395. PyDict_SetItemString(d, "error", PyCursesError);
  2396. /* Make the version available */
  2397. v = PyString_FromString(PyCursesVersion);
  2398. PyDict_SetItemString(d, "version", v);
  2399. PyDict_SetItemString(d, "__version__", v);
  2400. Py_DECREF(v);
  2401. SetDictInt("ERR", ERR);
  2402. SetDictInt("OK", OK);
  2403. /* Here are some attributes you can add to chars to print */
  2404. SetDictInt("A_ATTRIBUTES", A_ATTRIBUTES);
  2405. SetDictInt("A_NORMAL", A_NORMAL);
  2406. SetDictInt("A_STANDOUT", A_STANDOUT);
  2407. SetDictInt("A_UNDERLINE", A_UNDERLINE);
  2408. SetDictInt("A_REVERSE", A_REVERSE);
  2409. SetDictInt("A_BLINK", A_BLINK);
  2410. SetDictInt("A_DIM", A_DIM);
  2411. SetDictInt("A_BOLD", A_BOLD);
  2412. SetDictInt("A_ALTCHARSET", A_ALTCHARSET);
  2413. #if !defined(__NetBSD__)
  2414. SetDictInt("A_INVIS", A_INVIS);
  2415. #endif
  2416. SetDictInt("A_PROTECT", A_PROTECT);
  2417. SetDictInt("A_CHARTEXT", A_CHARTEXT);
  2418. SetDictInt("A_COLOR", A_COLOR);
  2419. /* The following are never available with strict SYSV curses */
  2420. #ifdef A_HORIZONTAL
  2421. SetDictInt("A_HORIZONTAL", A_HORIZONTAL);
  2422. #endif
  2423. #ifdef A_LEFT
  2424. SetDictInt("A_LEFT", A_LEFT);
  2425. #endif
  2426. #ifdef A_LOW
  2427. SetDictInt("A_LOW", A_LOW);
  2428. #endif
  2429. #ifdef A_RIGHT
  2430. SetDictInt("A_RIGHT", A_RIGHT);
  2431. #endif
  2432. #ifdef A_TOP
  2433. SetDictInt("A_TOP", A_TOP);
  2434. #endif
  2435. #ifdef A_VERTICAL
  2436. SetDictInt("A_VERTICAL", A_VERTICAL);
  2437. #endif
  2438. SetDictInt("COLOR_BLACK", COLOR_BLACK);
  2439. SetDictInt("COLOR_RED", COLOR_RED);
  2440. SetDictInt("COLOR_GREEN", COLOR_GREEN);
  2441. SetDictInt("COLOR_YELLOW", COLOR_YELLOW);
  2442. SetDictInt("COLOR_BLUE", COLOR_BLUE);
  2443. SetDictInt("COLOR_MAGENTA", COLOR_MAGENTA);
  2444. SetDictInt("COLOR_CYAN", COLOR_CYAN);
  2445. SetDictInt("COLOR_WHITE", COLOR_WHITE);
  2446. #ifdef NCURSES_MOUSE_VERSION
  2447. /* Mouse-related constants */
  2448. SetDictInt("BUTTON1_PRESSED", BUTTON1_PRESSED);
  2449. SetDictInt("BUTTON1_RELEASED", BUTTON1_RELEASED);
  2450. SetDictInt("BUTTON1_CLICKED", BUTTON1_CLICKED);
  2451. SetDictInt("BUTTON1_DOUBLE_CLICKED", BUTTON1_DOUBLE_CLICKED);
  2452. SetDictInt("BUTTON1_TRIPLE_CLICKED", BUTTON1_TRIPLE_CLICKED);
  2453. SetDictInt("BUTTON2_PRESSED", BUTTON2_PRESSED);
  2454. SetDictInt("BUTTON2_RELEASED", BUTTON2_RELEASED);
  2455. SetDictInt("BUTTON2_CLICKED", BUTTON2_CLICKED);
  2456. SetDictInt("BUTTON2_DOUBLE_CLICKED", BUTTON2_DOUBLE_CLICKED);
  2457. SetDictInt("BUTTON2_TRIPLE_CLICKED", BUTTON2_TRIPLE_CLICKED);
  2458. SetDictInt("BUTTON3_PRESSED", BUTTON3_PRESSED);
  2459. SetDictInt("BUTTON3_RELEASED", BUTTON3_RELEASED);
  2460. SetDictInt("BUTTON3_CLICKED", BUTTON3_CLICKED);
  2461. SetDictInt("BUTTON3_DOUBLE_CLICKED", BUTTON3_DOUBLE_CLICKED);
  2462. SetDictInt("BUTTON3_TRIPLE_CLICKED", BUTTON3_TRIPLE_CLICKED);
  2463. SetDictInt("BUTTON4_PRESSED", BUTTON4_PRESSED);
  2464. SetDictInt("BUTTON4_RELEASED", BUTTON4_RELEASED);
  2465. SetDictInt("BUTTON4_CLICKED", BUTTON4_CLICKED);
  2466. SetDictInt("BUTTON4_DOUBLE_CLICKED", BUTTON4_DOUBLE_CLICKED);
  2467. SetDictInt("BUTTON4_TRIPLE_CLICKED", BUTTON4_TRIPLE_CLICKED);
  2468. SetDictInt("BUTTON_SHIFT", BUTTON_SHIFT);
  2469. SetDictInt("BUTTON_CTRL", BUTTON_CTRL);
  2470. SetDictInt("BUTTON_ALT", BUTTON_ALT);
  2471. SetDictInt("ALL_MOUSE_EVENTS", ALL_MOUSE_EVENTS);
  2472. SetDictInt("REPORT_MOUSE_POSITION", REPORT_MOUSE_POSITION);
  2473. #endif
  2474. /* Now set everything up for KEY_ variables */
  2475. {
  2476. int key;
  2477. char *key_n;
  2478. char *key_n2;
  2479. #if !defined(__NetBSD__)
  2480. for (key=KEY_MIN;key < KEY_MAX; key++) {
  2481. key_n = (char *)keyname(key);
  2482. if (key_n == NULL || strcmp(key_n,"UNKNOWN KEY")==0)
  2483. continue;
  2484. if (strncmp(key_n,"KEY_F(",6)==0) {
  2485. char *p1, *p2;
  2486. key_n2 = malloc(strlen(key_n)+1);
  2487. if (!key_n2) {
  2488. PyErr_NoMemory();
  2489. break;
  2490. }
  2491. p1 = key_n;
  2492. p2 = key_n2;
  2493. while (*p1) {
  2494. if (*p1 != '(' && *p1 != ')') {
  2495. *p2 = *p1;
  2496. p2++;
  2497. }
  2498. p1++;
  2499. }
  2500. *p2 = (char)0;
  2501. } else
  2502. key_n2 = key_n;
  2503. SetDictInt(key_n2,key);
  2504. if (key_n2 != key_n)
  2505. free(key_n2);
  2506. }
  2507. #endif
  2508. SetDictInt("KEY_MIN", KEY_MIN);
  2509. SetDictInt("KEY_MAX", KEY_MAX);
  2510. }
  2511. }