home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Modules / _cursesmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  63.8 KB  |  2,356 lines

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