PageRenderTime 94ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/Modules/_cursesmodule.c

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