Python-2.7.3/Modules/_cursesmodule.c

No issues found

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