• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 *
PyCursesCheckERR(int code,char * fname)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
PyCurses_ConvertToChtype(PyObject * obj,chtype * ch)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 
func_PyCursesSetupTermCalled(void)211 static int func_PyCursesSetupTermCalled(void)
212 {
213     PyCursesSetupTermCalled;
214     return 1;
215 }
216 
func_PyCursesInitialised(void)217 static int func_PyCursesInitialised(void)
218 {
219     PyCursesInitialised;
220     return 1;
221 }
222 
func_PyCursesInitialisedColor(void)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)
Window_NoArgNoReturnFunction(touchwin)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
PyCursesWindow_Dealloc(PyCursesWindowObject * wo)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 *
PyCursesWindow_AddCh(PyCursesWindowObject * self,PyObject * args)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 *
PyCursesWindow_AddStr(PyCursesWindowObject * self,PyObject * args)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 *
PyCursesWindow_AddNStr(PyCursesWindowObject * self,PyObject * args)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 *
PyCursesWindow_Bkgd(PyCursesWindowObject * self,PyObject * args)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 *
PyCursesWindow_AttrOff(PyCursesWindowObject * self,PyObject * args)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 *
PyCursesWindow_AttrOn(PyCursesWindowObject * self,PyObject * args)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 *
PyCursesWindow_AttrSet(PyCursesWindowObject * self,PyObject * args)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 *
PyCursesWindow_BkgdSet(PyCursesWindowObject * self,PyObject * args)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 *
PyCursesWindow_Border(PyCursesWindowObject * self,PyObject * args)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 *
PyCursesWindow_Box(PyCursesWindowObject * self,PyObject * args)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
py_mvwdelch(WINDOW * w,int y,int x)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 *
PyCursesWindow_ChgAt(PyCursesWindowObject * self,PyObject * args)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 *
PyCursesWindow_DelCh(PyCursesWindowObject * self,PyObject * args)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 *
PyCursesWindow_DerWin(PyCursesWindowObject * self,PyObject * args)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 *
PyCursesWindow_EchoChar(PyCursesWindowObject * self,PyObject * args)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 *
PyCursesWindow_Enclose(PyCursesWindowObject * self,PyObject * args)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 *
PyCursesWindow_GetBkgd(PyCursesWindowObject * self)832 PyCursesWindow_GetBkgd(PyCursesWindowObject *self)
833 {
834     return PyInt_FromLong((long) getbkgd(self->win));
835 }
836 
837 static PyObject *
PyCursesWindow_GetCh(PyCursesWindowObject * self,PyObject * args)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 *
PyCursesWindow_GetKey(PyCursesWindowObject * self,PyObject * args)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_CheckSignals();
889         if (!PyErr_Occurred())
890             PyErr_SetString(PyCursesError, "no input");
891         return NULL;
892     } else if (rtn<=255) {
893         return Py_BuildValue("c", rtn);
894     } else {
895         const char *knp;
896 #if defined(__NetBSD__)
897         knp = unctrl(rtn);
898 #else
899         knp = keyname(rtn);
900 #endif
901         return PyString_FromString((knp == NULL) ? "" : knp);
902     }
903 }
904 
905 static PyObject *
PyCursesWindow_GetStr(PyCursesWindowObject * self,PyObject * args)906 PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
907 {
908     int x, y, n;
909     char rtn[1024]; /* This should be big enough.. I hope */
910     int rtn2;
911 
912     switch (PyTuple_Size(args)) {
913     case 0:
914         Py_BEGIN_ALLOW_THREADS
915         rtn2 = wgetnstr(self->win,rtn, 1023);
916         Py_END_ALLOW_THREADS
917         break;
918     case 1:
919         if (!PyArg_ParseTuple(args,"i;n", &n))
920             return NULL;
921         if (n < 0) {
922             PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
923             return NULL;
924         }
925         Py_BEGIN_ALLOW_THREADS
926         rtn2 = wgetnstr(self->win,rtn,MIN(n, 1023));
927         Py_END_ALLOW_THREADS
928         break;
929     case 2:
930         if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
931             return NULL;
932         Py_BEGIN_ALLOW_THREADS
933 #ifdef STRICT_SYSV_CURSES
934         rtn2 = wmove(self->win,y,x)==ERR ? ERR : wgetnstr(self->win, rtn, 1023);
935 #else
936         rtn2 = mvwgetnstr(self->win,y,x,rtn, 1023);
937 #endif
938         Py_END_ALLOW_THREADS
939         break;
940     case 3:
941         if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
942             return NULL;
943         if (n < 0) {
944             PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
945             return NULL;
946         }
947 #ifdef STRICT_SYSV_CURSES
948         Py_BEGIN_ALLOW_THREADS
949         rtn2 = wmove(self->win,y,x)==ERR ? ERR :
950         wgetnstr(self->win, rtn, MIN(n, 1023));
951         Py_END_ALLOW_THREADS
952 #else
953         Py_BEGIN_ALLOW_THREADS
954         rtn2 = mvwgetnstr(self->win, y, x, rtn, MIN(n, 1023));
955         Py_END_ALLOW_THREADS
956 #endif
957         break;
958     default:
959         PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 3 arguments");
960         return NULL;
961     }
962     if (rtn2 == ERR)
963         rtn[0] = 0;
964     return PyString_FromString(rtn);
965 }
966 
967 static PyObject *
PyCursesWindow_Hline(PyCursesWindowObject * self,PyObject * args)968 PyCursesWindow_Hline(PyCursesWindowObject *self, PyObject *args)
969 {
970     PyObject *temp;
971     chtype ch;
972     int n, x, y, code = OK;
973     attr_t attr = A_NORMAL;
974     long lattr;
975 
976     switch (PyTuple_Size(args)) {
977     case 2:
978         if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
979             return NULL;
980         break;
981     case 3:
982         if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &lattr))
983             return NULL;
984         attr = lattr;
985         break;
986     case 4:
987         if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
988             return NULL;
989         code = wmove(self->win, y, x);
990         break;
991     case 5:
992         if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
993                               &y, &x, &temp, &n, &lattr))
994             return NULL;
995         attr = lattr;
996         code = wmove(self->win, y, x);
997         break;
998     default:
999         PyErr_SetString(PyExc_TypeError, "hline requires 2 to 5 arguments");
1000         return NULL;
1001     }
1002 
1003     if (code != ERR) {
1004         if (!PyCurses_ConvertToChtype(temp, &ch)) {
1005             PyErr_SetString(PyExc_TypeError,
1006                             "argument 1 or 3 must be a ch or an int");
1007             return NULL;
1008         }
1009         return PyCursesCheckERR(whline(self->win, ch | attr, n), "hline");
1010     } else
1011         return PyCursesCheckERR(code, "wmove");
1012 }
1013 
1014 static PyObject *
PyCursesWindow_InsCh(PyCursesWindowObject * self,PyObject * args)1015 PyCursesWindow_InsCh(PyCursesWindowObject *self, PyObject *args)
1016 {
1017     int rtn, x, y, use_xy = FALSE;
1018     PyObject *temp;
1019     chtype ch = 0;
1020     attr_t attr = A_NORMAL;
1021     long lattr;
1022 
1023     switch (PyTuple_Size(args)) {
1024     case 1:
1025         if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
1026             return NULL;
1027         break;
1028     case 2:
1029         if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &temp, &lattr))
1030             return NULL;
1031         attr = lattr;
1032         break;
1033     case 3:
1034         if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &temp))
1035             return NULL;
1036         use_xy = TRUE;
1037         break;
1038     case 4:
1039         if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr", &y, &x, &temp, &lattr))
1040             return NULL;
1041         attr = lattr;
1042         use_xy = TRUE;
1043         break;
1044     default:
1045         PyErr_SetString(PyExc_TypeError, "insch requires 1 or 4 arguments");
1046         return NULL;
1047     }
1048 
1049     if (!PyCurses_ConvertToChtype(temp, &ch)) {
1050         PyErr_SetString(PyExc_TypeError,
1051                         "argument 1 or 3 must be a ch or an int");
1052         return NULL;
1053     }
1054 
1055     if (use_xy == TRUE)
1056         rtn = mvwinsch(self->win,y,x, ch | attr);
1057     else {
1058         rtn = winsch(self->win, ch | attr);
1059     }
1060     return PyCursesCheckERR(rtn, "insch");
1061 }
1062 
1063 static PyObject *
PyCursesWindow_InCh(PyCursesWindowObject * self,PyObject * args)1064 PyCursesWindow_InCh(PyCursesWindowObject *self, PyObject *args)
1065 {
1066     int x, y, rtn;
1067 
1068     switch (PyTuple_Size(args)) {
1069     case 0:
1070         rtn = winch(self->win);
1071         break;
1072     case 2:
1073         if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1074             return NULL;
1075         rtn = mvwinch(self->win,y,x);
1076         break;
1077     default:
1078         PyErr_SetString(PyExc_TypeError, "inch requires 0 or 2 arguments");
1079         return NULL;
1080     }
1081     return PyInt_FromLong((long) rtn);
1082 }
1083 
1084 static PyObject *
PyCursesWindow_InStr(PyCursesWindowObject * self,PyObject * args)1085 PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
1086 {
1087     int x, y, n;
1088     char rtn[1024]; /* This should be big enough.. I hope */
1089     int rtn2;
1090 
1091     switch (PyTuple_Size(args)) {
1092     case 0:
1093         rtn2 = winnstr(self->win,rtn, 1023);
1094         break;
1095     case 1:
1096         if (!PyArg_ParseTuple(args,"i;n", &n))
1097             return NULL;
1098         if (n < 0) {
1099             PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1100             return NULL;
1101         }
1102         rtn2 = winnstr(self->win,rtn,MIN(n,1023));
1103         break;
1104     case 2:
1105         if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1106             return NULL;
1107         rtn2 = mvwinnstr(self->win,y,x,rtn,1023);
1108         break;
1109     case 3:
1110         if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
1111             return NULL;
1112         if (n < 0) {
1113             PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1114             return NULL;
1115         }
1116         rtn2 = mvwinnstr(self->win, y, x, rtn, MIN(n,1023));
1117         break;
1118     default:
1119         PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments");
1120         return NULL;
1121     }
1122     if (rtn2 == ERR)
1123         rtn[0] = 0;
1124     return PyString_FromString(rtn);
1125 }
1126 
1127 static PyObject *
PyCursesWindow_InsStr(PyCursesWindowObject * self,PyObject * args)1128 PyCursesWindow_InsStr(PyCursesWindowObject *self, PyObject *args)
1129 {
1130     int rtn;
1131     int x, y;
1132     char *str;
1133     attr_t attr = A_NORMAL , attr_old = A_NORMAL;
1134     long lattr;
1135     int use_xy = FALSE, use_attr = FALSE;
1136 
1137     switch (PyTuple_Size(args)) {
1138     case 1:
1139         if (!PyArg_ParseTuple(args,"s;str", &str))
1140             return NULL;
1141         break;
1142     case 2:
1143         if (!PyArg_ParseTuple(args,"sl;str,attr", &str, &lattr))
1144             return NULL;
1145         attr = lattr;
1146         use_attr = TRUE;
1147         break;
1148     case 3:
1149         if (!PyArg_ParseTuple(args,"iis;y,x,str", &y, &x, &str))
1150             return NULL;
1151         use_xy = TRUE;
1152         break;
1153     case 4:
1154         if (!PyArg_ParseTuple(args,"iisl;y,x,str,attr", &y, &x, &str, &lattr))
1155             return NULL;
1156         attr = lattr;
1157         use_xy = use_attr = TRUE;
1158         break;
1159     default:
1160         PyErr_SetString(PyExc_TypeError, "insstr requires 1 to 4 arguments");
1161         return NULL;
1162     }
1163 
1164     if (use_attr == TRUE) {
1165         attr_old = getattrs(self->win);
1166         (void)wattrset(self->win,attr);
1167     }
1168     if (use_xy == TRUE)
1169         rtn = mvwinsstr(self->win,y,x,str);
1170     else
1171         rtn = winsstr(self->win,str);
1172     if (use_attr == TRUE)
1173         (void)wattrset(self->win,attr_old);
1174     return PyCursesCheckERR(rtn, "insstr");
1175 }
1176 
1177 static PyObject *
PyCursesWindow_InsNStr(PyCursesWindowObject * self,PyObject * args)1178 PyCursesWindow_InsNStr(PyCursesWindowObject *self, PyObject *args)
1179 {
1180     int rtn, x, y, n;
1181     char *str;
1182     attr_t attr = A_NORMAL , attr_old = A_NORMAL;
1183     long lattr;
1184     int use_xy = FALSE, use_attr = FALSE;
1185 
1186     switch (PyTuple_Size(args)) {
1187     case 2:
1188         if (!PyArg_ParseTuple(args,"si;str,n", &str, &n))
1189             return NULL;
1190         break;
1191     case 3:
1192         if (!PyArg_ParseTuple(args,"sil;str,n,attr", &str, &n, &lattr))
1193             return NULL;
1194         attr = lattr;
1195         use_attr = TRUE;
1196         break;
1197     case 4:
1198         if (!PyArg_ParseTuple(args,"iisi;y,x,str,n", &y, &x, &str, &n))
1199             return NULL;
1200         use_xy = TRUE;
1201         break;
1202     case 5:
1203         if (!PyArg_ParseTuple(args,"iisil;y,x,str,n,attr", &y, &x, &str, &n, &lattr))
1204             return NULL;
1205         attr = lattr;
1206         use_xy = use_attr = TRUE;
1207         break;
1208     default:
1209         PyErr_SetString(PyExc_TypeError, "insnstr requires 2 to 5 arguments");
1210         return NULL;
1211     }
1212 
1213     if (use_attr == TRUE) {
1214         attr_old = getattrs(self->win);
1215         (void)wattrset(self->win,attr);
1216     }
1217     if (use_xy == TRUE)
1218         rtn = mvwinsnstr(self->win,y,x,str,n);
1219     else
1220         rtn = winsnstr(self->win,str,n);
1221     if (use_attr == TRUE)
1222         (void)wattrset(self->win,attr_old);
1223     return PyCursesCheckERR(rtn, "insnstr");
1224 }
1225 
1226 static PyObject *
PyCursesWindow_Is_LineTouched(PyCursesWindowObject * self,PyObject * args)1227 PyCursesWindow_Is_LineTouched(PyCursesWindowObject *self, PyObject *args)
1228 {
1229     int line, erg;
1230     if (!PyArg_ParseTuple(args,"i;line", &line))
1231         return NULL;
1232     erg = is_linetouched(self->win, line);
1233     if (erg == ERR) {
1234         PyErr_SetString(PyExc_TypeError,
1235                         "is_linetouched: line number outside of boundaries");
1236         return NULL;
1237     } else
1238         if (erg == FALSE) {
1239             Py_INCREF(Py_False);
1240             return Py_False;
1241         } else {
1242             Py_INCREF(Py_True);
1243             return Py_True;
1244         }
1245 }
1246 
1247 static PyObject *
PyCursesWindow_NoOutRefresh(PyCursesWindowObject * self,PyObject * args)1248 PyCursesWindow_NoOutRefresh(PyCursesWindowObject *self, PyObject *args)
1249 {
1250     int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
1251     int rtn;
1252 
1253 #ifndef WINDOW_HAS_FLAGS
1254     if (0)
1255 #else
1256         if (self->win->_flags & _ISPAD)
1257 #endif
1258         {
1259             switch(PyTuple_Size(args)) {
1260             case 6:
1261                 if (!PyArg_ParseTuple(args,
1262                                       "iiiiii;" \
1263                                       "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
1264                                       &pminrow, &pmincol, &sminrow,
1265                                       &smincol, &smaxrow, &smaxcol))
1266                     return NULL;
1267                 Py_BEGIN_ALLOW_THREADS
1268                 rtn = pnoutrefresh(self->win,
1269                                    pminrow, pmincol, sminrow,
1270                                    smincol, smaxrow, smaxcol);
1271                 Py_END_ALLOW_THREADS
1272                 return PyCursesCheckERR(rtn, "pnoutrefresh");
1273             default:
1274                 PyErr_SetString(PyCursesError,
1275                                 "noutrefresh() called for a pad "
1276                                 "requires 6 arguments");
1277                 return NULL;
1278             }
1279         } else {
1280             if (!PyArg_ParseTuple(args, ":noutrefresh"))
1281                 return NULL;
1282 
1283             Py_BEGIN_ALLOW_THREADS
1284             rtn = wnoutrefresh(self->win);
1285             Py_END_ALLOW_THREADS
1286             return PyCursesCheckERR(rtn, "wnoutrefresh");
1287         }
1288 }
1289 
1290 static PyObject *
PyCursesWindow_Overlay(PyCursesWindowObject * self,PyObject * args)1291 PyCursesWindow_Overlay(PyCursesWindowObject *self, PyObject *args)
1292 {
1293     PyCursesWindowObject *temp;
1294     int use_copywin = FALSE;
1295     int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
1296     int rtn;
1297 
1298     switch (PyTuple_Size(args)) {
1299     case 1:
1300         if (!PyArg_ParseTuple(args, "O!;window object",
1301                               &PyCursesWindow_Type, &temp))
1302             return NULL;
1303         break;
1304     case 7:
1305         if (!PyArg_ParseTuple(args, "O!iiiiii;window object, int, int, int, int, int, int",
1306                               &PyCursesWindow_Type, &temp, &sminrow, &smincol,
1307                               &dminrow, &dmincol, &dmaxrow, &dmaxcol))
1308             return NULL;
1309         use_copywin = TRUE;
1310         break;
1311     default:
1312         PyErr_SetString(PyExc_TypeError,
1313                         "overlay requires one or seven arguments");
1314         return NULL;
1315     }
1316 
1317     if (use_copywin == TRUE) {
1318         rtn = copywin(self->win, temp->win, sminrow, smincol,
1319                       dminrow, dmincol, dmaxrow, dmaxcol, TRUE);
1320         return PyCursesCheckERR(rtn, "copywin");
1321     }
1322     else {
1323         rtn = overlay(self->win, temp->win);
1324         return PyCursesCheckERR(rtn, "overlay");
1325     }
1326 }
1327 
1328 static PyObject *
PyCursesWindow_Overwrite(PyCursesWindowObject * self,PyObject * args)1329 PyCursesWindow_Overwrite(PyCursesWindowObject *self, PyObject *args)
1330 {
1331     PyCursesWindowObject *temp;
1332     int use_copywin = FALSE;
1333     int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
1334     int rtn;
1335 
1336     switch (PyTuple_Size(args)) {
1337     case 1:
1338         if (!PyArg_ParseTuple(args, "O!;window object",
1339                               &PyCursesWindow_Type, &temp))
1340             return NULL;
1341         break;
1342     case 7:
1343         if (!PyArg_ParseTuple(args, "O!iiiiii;window object, int, int, int, int, int, int",
1344                               &PyCursesWindow_Type, &temp, &sminrow, &smincol,
1345                               &dminrow, &dmincol, &dmaxrow, &dmaxcol))
1346             return NULL;
1347         use_copywin = TRUE;
1348         break;
1349     default:
1350         PyErr_SetString(PyExc_TypeError,
1351                         "overwrite requires one or seven arguments");
1352         return NULL;
1353     }
1354 
1355     if (use_copywin == TRUE) {
1356         rtn = copywin(self->win, temp->win, sminrow, smincol,
1357                       dminrow, dmincol, dmaxrow, dmaxcol, FALSE);
1358         return PyCursesCheckERR(rtn, "copywin");
1359     }
1360     else {
1361         rtn = overwrite(self->win, temp->win);
1362         return PyCursesCheckERR(rtn, "overwrite");
1363     }
1364 }
1365 
1366 static PyObject *
PyCursesWindow_PutWin(PyCursesWindowObject * self,PyObject * args)1367 PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *args)
1368 {
1369     PyObject *temp;
1370 
1371     if (!PyArg_ParseTuple(args, "O;fileobj", &temp))
1372         return NULL;
1373     if (!PyFile_Check(temp)) {
1374         PyErr_SetString(PyExc_TypeError, "argument must be a file object");
1375         return NULL;
1376     }
1377     return PyCursesCheckERR(putwin(self->win, PyFile_AsFile(temp)),
1378                             "putwin");
1379 }
1380 
1381 static PyObject *
PyCursesWindow_RedrawLine(PyCursesWindowObject * self,PyObject * args)1382 PyCursesWindow_RedrawLine(PyCursesWindowObject *self, PyObject *args)
1383 {
1384     int beg, num;
1385     if (!PyArg_ParseTuple(args, "ii;beg,num", &beg, &num))
1386         return NULL;
1387     return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln");
1388 }
1389 
1390 static PyObject *
PyCursesWindow_Refresh(PyCursesWindowObject * self,PyObject * args)1391 PyCursesWindow_Refresh(PyCursesWindowObject *self, PyObject *args)
1392 {
1393     int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
1394     int rtn;
1395 
1396 #ifndef WINDOW_HAS_FLAGS
1397     if (0)
1398 #else
1399         if (self->win->_flags & _ISPAD)
1400 #endif
1401         {
1402             switch(PyTuple_Size(args)) {
1403             case 6:
1404                 if (!PyArg_ParseTuple(args,
1405                                       "iiiiii;" \
1406                                       "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
1407                                       &pminrow, &pmincol, &sminrow,
1408                                       &smincol, &smaxrow, &smaxcol))
1409                     return NULL;
1410 
1411                 Py_BEGIN_ALLOW_THREADS
1412                 rtn = prefresh(self->win,
1413                                pminrow, pmincol, sminrow,
1414                                smincol, smaxrow, smaxcol);
1415                 Py_END_ALLOW_THREADS
1416                 return PyCursesCheckERR(rtn, "prefresh");
1417             default:
1418                 PyErr_SetString(PyCursesError,
1419                                 "refresh() for a pad requires 6 arguments");
1420                 return NULL;
1421             }
1422         } else {
1423             if (!PyArg_ParseTuple(args, ":refresh"))
1424                 return NULL;
1425             Py_BEGIN_ALLOW_THREADS
1426             rtn = wrefresh(self->win);
1427             Py_END_ALLOW_THREADS
1428             return PyCursesCheckERR(rtn, "prefresh");
1429         }
1430 }
1431 
1432 static PyObject *
PyCursesWindow_SetScrollRegion(PyCursesWindowObject * self,PyObject * args)1433 PyCursesWindow_SetScrollRegion(PyCursesWindowObject *self, PyObject *args)
1434 {
1435     int x, y;
1436     if (!PyArg_ParseTuple(args,"ii;top, bottom",&y,&x))
1437         return NULL;
1438     return PyCursesCheckERR(wsetscrreg(self->win,y,x), "wsetscrreg");
1439 }
1440 
1441 static PyObject *
PyCursesWindow_SubWin(PyCursesWindowObject * self,PyObject * args)1442 PyCursesWindow_SubWin(PyCursesWindowObject *self, PyObject *args)
1443 {
1444     WINDOW *win;
1445     int nlines, ncols, begin_y, begin_x;
1446 
1447     nlines = 0;
1448     ncols  = 0;
1449     switch (PyTuple_Size(args)) {
1450     case 2:
1451         if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
1452             return NULL;
1453         break;
1454     case 4:
1455         if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
1456                               &nlines,&ncols,&begin_y,&begin_x))
1457             return NULL;
1458         break;
1459     default:
1460         PyErr_SetString(PyExc_TypeError, "subwin requires 2 or 4 arguments");
1461         return NULL;
1462     }
1463 
1464     /* printf("Subwin: %i %i %i %i   \n", nlines, ncols, begin_y, begin_x); */
1465 #ifdef WINDOW_HAS_FLAGS
1466     if (self->win->_flags & _ISPAD)
1467         win = subpad(self->win, nlines, ncols, begin_y, begin_x);
1468     else
1469 #endif
1470         win = subwin(self->win, nlines, ncols, begin_y, begin_x);
1471 
1472     if (win == NULL) {
1473         PyErr_SetString(PyCursesError, catchall_NULL);
1474         return NULL;
1475     }
1476 
1477     return (PyObject *)PyCursesWindow_New(win);
1478 }
1479 
1480 static PyObject *
PyCursesWindow_Scroll(PyCursesWindowObject * self,PyObject * args)1481 PyCursesWindow_Scroll(PyCursesWindowObject *self, PyObject *args)
1482 {
1483     int nlines;
1484     switch(PyTuple_Size(args)) {
1485     case 0:
1486         return PyCursesCheckERR(scroll(self->win), "scroll");
1487     case 1:
1488         if (!PyArg_ParseTuple(args, "i;nlines", &nlines))
1489             return NULL;
1490         return PyCursesCheckERR(wscrl(self->win, nlines), "scroll");
1491     default:
1492         PyErr_SetString(PyExc_TypeError, "scroll requires 0 or 1 arguments");
1493         return NULL;
1494     }
1495 }
1496 
1497 static PyObject *
PyCursesWindow_TouchLine(PyCursesWindowObject * self,PyObject * args)1498 PyCursesWindow_TouchLine(PyCursesWindowObject *self, PyObject *args)
1499 {
1500     int st, cnt, val;
1501     switch (PyTuple_Size(args)) {
1502     case 2:
1503         if (!PyArg_ParseTuple(args,"ii;start,count",&st,&cnt))
1504             return NULL;
1505         return PyCursesCheckERR(touchline(self->win,st,cnt), "touchline");
1506     case 3:
1507         if (!PyArg_ParseTuple(args, "iii;start,count,val", &st, &cnt, &val))
1508             return NULL;
1509         return PyCursesCheckERR(wtouchln(self->win, st, cnt, val), "touchline");
1510     default:
1511         PyErr_SetString(PyExc_TypeError, "touchline requires 2 or 3 arguments");
1512         return NULL;
1513     }
1514 }
1515 
1516 static PyObject *
PyCursesWindow_Vline(PyCursesWindowObject * self,PyObject * args)1517 PyCursesWindow_Vline(PyCursesWindowObject *self, PyObject *args)
1518 {
1519     PyObject *temp;
1520     chtype ch;
1521     int n, x, y, code = OK;
1522     attr_t attr = A_NORMAL;
1523     long lattr;
1524 
1525     switch (PyTuple_Size(args)) {
1526     case 2:
1527         if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
1528             return NULL;
1529         break;
1530     case 3:
1531         if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &lattr))
1532             return NULL;
1533         attr = lattr;
1534         break;
1535     case 4:
1536         if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
1537             return NULL;
1538         code = wmove(self->win, y, x);
1539         break;
1540     case 5:
1541         if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
1542                               &y, &x, &temp, &n, &lattr))
1543             return NULL;
1544         attr = lattr;
1545         code = wmove(self->win, y, x);
1546         break;
1547     default:
1548         PyErr_SetString(PyExc_TypeError, "vline requires 2 to 5 arguments");
1549         return NULL;
1550     }
1551 
1552     if (code != ERR) {
1553         if (!PyCurses_ConvertToChtype(temp, &ch)) {
1554             PyErr_SetString(PyExc_TypeError,
1555                             "argument 1 or 3 must be a ch or an int");
1556             return NULL;
1557         }
1558         return PyCursesCheckERR(wvline(self->win, ch | attr, n), "vline");
1559     } else
1560         return PyCursesCheckERR(code, "wmove");
1561 }
1562 
1563 static PyMethodDef PyCursesWindow_Methods[] = {
1564     {"addch",           (PyCFunction)PyCursesWindow_AddCh, METH_VARARGS},
1565     {"addnstr",         (PyCFunction)PyCursesWindow_AddNStr, METH_VARARGS},
1566     {"addstr",          (PyCFunction)PyCursesWindow_AddStr, METH_VARARGS},
1567     {"attroff",         (PyCFunction)PyCursesWindow_AttrOff, METH_VARARGS},
1568     {"attron",          (PyCFunction)PyCursesWindow_AttrOn, METH_VARARGS},
1569     {"attrset",         (PyCFunction)PyCursesWindow_AttrSet, METH_VARARGS},
1570     {"bkgd",            (PyCFunction)PyCursesWindow_Bkgd, METH_VARARGS},
1571     {"chgat",           (PyCFunction)PyCursesWindow_ChgAt, METH_VARARGS},
1572     {"bkgdset",         (PyCFunction)PyCursesWindow_BkgdSet, METH_VARARGS},
1573     {"border",          (PyCFunction)PyCursesWindow_Border, METH_VARARGS},
1574     {"box",             (PyCFunction)PyCursesWindow_Box, METH_VARARGS},
1575     {"clear",           (PyCFunction)PyCursesWindow_wclear, METH_NOARGS},
1576     {"clearok",         (PyCFunction)PyCursesWindow_clearok, METH_VARARGS},
1577     {"clrtobot",        (PyCFunction)PyCursesWindow_wclrtobot, METH_NOARGS},
1578     {"clrtoeol",        (PyCFunction)PyCursesWindow_wclrtoeol, METH_NOARGS},
1579     {"cursyncup",       (PyCFunction)PyCursesWindow_wcursyncup, METH_NOARGS},
1580     {"delch",           (PyCFunction)PyCursesWindow_DelCh, METH_VARARGS},
1581     {"deleteln",        (PyCFunction)PyCursesWindow_wdeleteln, METH_NOARGS},
1582     {"derwin",          (PyCFunction)PyCursesWindow_DerWin, METH_VARARGS},
1583     {"echochar",        (PyCFunction)PyCursesWindow_EchoChar, METH_VARARGS},
1584 #ifdef NCURSES_MOUSE_VERSION
1585     {"enclose",         (PyCFunction)PyCursesWindow_Enclose, METH_VARARGS},
1586 #endif
1587     {"erase",           (PyCFunction)PyCursesWindow_werase, METH_NOARGS},
1588     {"getbegyx",        (PyCFunction)PyCursesWindow_getbegyx, METH_NOARGS},
1589     {"getbkgd",         (PyCFunction)PyCursesWindow_GetBkgd, METH_NOARGS},
1590     {"getch",           (PyCFunction)PyCursesWindow_GetCh, METH_VARARGS},
1591     {"getkey",          (PyCFunction)PyCursesWindow_GetKey, METH_VARARGS},
1592     {"getmaxyx",        (PyCFunction)PyCursesWindow_getmaxyx, METH_NOARGS},
1593     {"getparyx",        (PyCFunction)PyCursesWindow_getparyx, METH_NOARGS},
1594     {"getstr",          (PyCFunction)PyCursesWindow_GetStr, METH_VARARGS},
1595     {"getyx",           (PyCFunction)PyCursesWindow_getyx, METH_NOARGS},
1596     {"hline",           (PyCFunction)PyCursesWindow_Hline, METH_VARARGS},
1597     {"idcok",           (PyCFunction)PyCursesWindow_idcok, METH_VARARGS},
1598     {"idlok",           (PyCFunction)PyCursesWindow_idlok, METH_VARARGS},
1599     {"immedok",         (PyCFunction)PyCursesWindow_immedok, METH_VARARGS},
1600     {"inch",            (PyCFunction)PyCursesWindow_InCh, METH_VARARGS},
1601     {"insch",           (PyCFunction)PyCursesWindow_InsCh, METH_VARARGS},
1602     {"insdelln",        (PyCFunction)PyCursesWindow_winsdelln, METH_VARARGS},
1603     {"insertln",        (PyCFunction)PyCursesWindow_winsertln, METH_NOARGS},
1604     {"insnstr",         (PyCFunction)PyCursesWindow_InsNStr, METH_VARARGS},
1605     {"insstr",          (PyCFunction)PyCursesWindow_InsStr, METH_VARARGS},
1606     {"instr",           (PyCFunction)PyCursesWindow_InStr, METH_VARARGS},
1607     {"is_linetouched",  (PyCFunction)PyCursesWindow_Is_LineTouched, METH_VARARGS},
1608     {"is_wintouched",   (PyCFunction)PyCursesWindow_is_wintouched, METH_NOARGS},
1609     {"keypad",          (PyCFunction)PyCursesWindow_keypad, METH_VARARGS},
1610     {"leaveok",         (PyCFunction)PyCursesWindow_leaveok, METH_VARARGS},
1611     {"move",            (PyCFunction)PyCursesWindow_wmove, METH_VARARGS},
1612     {"mvderwin",        (PyCFunction)PyCursesWindow_mvderwin, METH_VARARGS},
1613     {"mvwin",           (PyCFunction)PyCursesWindow_mvwin, METH_VARARGS},
1614     {"nodelay",         (PyCFunction)PyCursesWindow_nodelay, METH_VARARGS},
1615     {"notimeout",       (PyCFunction)PyCursesWindow_notimeout, METH_VARARGS},
1616     {"noutrefresh",     (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
1617     /* Backward compatibility alias -- remove in Python 2.3 */
1618     {"nooutrefresh",    (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
1619     {"overlay",         (PyCFunction)PyCursesWindow_Overlay, METH_VARARGS},
1620     {"overwrite",       (PyCFunction)PyCursesWindow_Overwrite,
1621      METH_VARARGS},
1622     {"putwin",          (PyCFunction)PyCursesWindow_PutWin, METH_VARARGS},
1623     {"redrawln",        (PyCFunction)PyCursesWindow_RedrawLine, METH_VARARGS},
1624     {"redrawwin",       (PyCFunction)PyCursesWindow_redrawwin, METH_NOARGS},
1625     {"refresh",         (PyCFunction)PyCursesWindow_Refresh, METH_VARARGS},
1626 #ifndef STRICT_SYSV_CURSES
1627     {"resize",          (PyCFunction)PyCursesWindow_wresize, METH_VARARGS},
1628 #endif
1629     {"scroll",          (PyCFunction)PyCursesWindow_Scroll, METH_VARARGS},
1630     {"scrollok",        (PyCFunction)PyCursesWindow_scrollok, METH_VARARGS},
1631     {"setscrreg",       (PyCFunction)PyCursesWindow_SetScrollRegion, METH_VARARGS},
1632     {"standend",        (PyCFunction)PyCursesWindow_wstandend, METH_NOARGS},
1633     {"standout",        (PyCFunction)PyCursesWindow_wstandout, METH_NOARGS},
1634     {"subpad",          (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
1635     {"subwin",          (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
1636     {"syncdown",        (PyCFunction)PyCursesWindow_wsyncdown, METH_NOARGS},
1637     {"syncok",          (PyCFunction)PyCursesWindow_syncok, METH_VARARGS},
1638     {"syncup",          (PyCFunction)PyCursesWindow_wsyncup, METH_NOARGS},
1639     {"timeout",         (PyCFunction)PyCursesWindow_wtimeout, METH_VARARGS},
1640     {"touchline",       (PyCFunction)PyCursesWindow_TouchLine, METH_VARARGS},
1641     {"touchwin",        (PyCFunction)PyCursesWindow_touchwin, METH_NOARGS},
1642     {"untouchwin",      (PyCFunction)PyCursesWindow_untouchwin, METH_NOARGS},
1643     {"vline",           (PyCFunction)PyCursesWindow_Vline, METH_VARARGS},
1644     {NULL,                  NULL}   /* sentinel */
1645 };
1646 
1647 static PyObject *
PyCursesWindow_GetAttr(PyCursesWindowObject * self,char * name)1648 PyCursesWindow_GetAttr(PyCursesWindowObject *self, char *name)
1649 {
1650     return Py_FindMethod(PyCursesWindow_Methods, (PyObject *)self, name);
1651 }
1652 
1653 /* -------------------------------------------------------*/
1654 
1655 PyTypeObject PyCursesWindow_Type = {
1656     PyVarObject_HEAD_INIT(NULL, 0)
1657     "_curses.curses window",            /*tp_name*/
1658     sizeof(PyCursesWindowObject),       /*tp_basicsize*/
1659     0,                          /*tp_itemsize*/
1660     /* methods */
1661     (destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/
1662     0,                          /*tp_print*/
1663     (getattrfunc)PyCursesWindow_GetAttr, /*tp_getattr*/
1664     (setattrfunc)0, /*tp_setattr*/
1665     0,                          /*tp_compare*/
1666     0,                          /*tp_repr*/
1667     0,                          /*tp_as_number*/
1668     0,                          /*tp_as_sequence*/
1669     0,                          /*tp_as_mapping*/
1670     0,                          /*tp_hash*/
1671 };
1672 
1673 /*********************************************************************
1674  Global Functions
1675 **********************************************************************/
1676 
1677 NoArgNoReturnFunction(beep)
NoArgNoReturnFunction(def_prog_mode)1678 NoArgNoReturnFunction(def_prog_mode)
1679 NoArgNoReturnFunction(def_shell_mode)
1680 NoArgNoReturnFunction(doupdate)
1681 NoArgNoReturnFunction(endwin)
1682 NoArgNoReturnFunction(flash)
1683 NoArgNoReturnFunction(nocbreak)
1684 NoArgNoReturnFunction(noecho)
1685 NoArgNoReturnFunction(nonl)
1686 NoArgNoReturnFunction(noraw)
1687 NoArgNoReturnFunction(reset_prog_mode)
1688 NoArgNoReturnFunction(reset_shell_mode)
1689 NoArgNoReturnFunction(resetty)
1690 NoArgNoReturnFunction(savetty)
1691 
1692 NoArgOrFlagNoReturnFunction(cbreak)
1693 NoArgOrFlagNoReturnFunction(echo)
1694 NoArgOrFlagNoReturnFunction(nl)
1695 NoArgOrFlagNoReturnFunction(raw)
1696 
1697 NoArgReturnIntFunction(baudrate)
1698 NoArgReturnIntFunction(termattrs)
1699 
1700 NoArgReturnStringFunction(termname)
1701 NoArgReturnStringFunction(longname)
1702 
1703 NoArgTrueFalseFunction(can_change_color)
1704 NoArgTrueFalseFunction(has_colors)
1705 NoArgTrueFalseFunction(has_ic)
1706 NoArgTrueFalseFunction(has_il)
1707 NoArgTrueFalseFunction(isendwin)
1708 NoArgNoReturnVoidFunction(flushinp)
1709 NoArgNoReturnVoidFunction(noqiflush)
1710 
1711 static PyObject *
1712 PyCurses_filter(PyObject *self)
1713 {
1714     /* not checking for PyCursesInitialised here since filter() must
1715        be called before initscr() */
1716     filter();
1717     Py_INCREF(Py_None);
1718     return Py_None;
1719 }
1720 
1721 static PyObject *
PyCurses_Color_Content(PyObject * self,PyObject * args)1722 PyCurses_Color_Content(PyObject *self, PyObject *args)
1723 {
1724     short color,r,g,b;
1725 
1726     PyCursesInitialised;
1727     PyCursesInitialisedColor;
1728 
1729     if (!PyArg_ParseTuple(args, "h:color_content", &color)) return NULL;
1730 
1731     if (color_content(color, &r, &g, &b) != ERR)
1732         return Py_BuildValue("(iii)", r, g, b);
1733     else {
1734         PyErr_SetString(PyCursesError,
1735                         "Argument 1 was out of range. Check value of COLORS.");
1736         return NULL;
1737     }
1738 }
1739 
1740 static PyObject *
PyCurses_color_pair(PyObject * self,PyObject * args)1741 PyCurses_color_pair(PyObject *self, PyObject *args)
1742 {
1743     int n;
1744 
1745     PyCursesInitialised;
1746     PyCursesInitialisedColor;
1747 
1748     if (!PyArg_ParseTuple(args, "i:color_pair", &n)) return NULL;
1749     return PyInt_FromLong((long) (n << 8));
1750 }
1751 
1752 static PyObject *
PyCurses_Curs_Set(PyObject * self,PyObject * args)1753 PyCurses_Curs_Set(PyObject *self, PyObject *args)
1754 {
1755     int vis,erg;
1756 
1757     PyCursesInitialised;
1758 
1759     if (!PyArg_ParseTuple(args, "i:curs_set", &vis)) return NULL;
1760 
1761     erg = curs_set(vis);
1762     if (erg == ERR) return PyCursesCheckERR(erg, "curs_set");
1763 
1764     return PyInt_FromLong((long) erg);
1765 }
1766 
1767 static PyObject *
PyCurses_Delay_Output(PyObject * self,PyObject * args)1768 PyCurses_Delay_Output(PyObject *self, PyObject *args)
1769 {
1770     int ms;
1771 
1772     PyCursesInitialised;
1773 
1774     if (!PyArg_ParseTuple(args, "i:delay_output", &ms)) return NULL;
1775 
1776     return PyCursesCheckERR(delay_output(ms), "delay_output");
1777 }
1778 
1779 static PyObject *
PyCurses_EraseChar(PyObject * self)1780 PyCurses_EraseChar(PyObject *self)
1781 {
1782     char ch;
1783 
1784     PyCursesInitialised;
1785 
1786     ch = erasechar();
1787 
1788     return PyString_FromStringAndSize(&ch, 1);
1789 }
1790 
1791 static PyObject *
PyCurses_getsyx(PyObject * self)1792 PyCurses_getsyx(PyObject *self)
1793 {
1794     int x = 0;
1795     int y = 0;
1796 
1797     PyCursesInitialised;
1798 
1799     getsyx(y, x);
1800 
1801     return Py_BuildValue("(ii)", y, x);
1802 }
1803 
1804 #ifdef NCURSES_MOUSE_VERSION
1805 static PyObject *
PyCurses_GetMouse(PyObject * self)1806 PyCurses_GetMouse(PyObject *self)
1807 {
1808     int rtn;
1809     MEVENT event;
1810 
1811     PyCursesInitialised;
1812 
1813     rtn = getmouse( &event );
1814     if (rtn == ERR) {
1815         PyErr_SetString(PyCursesError, "getmouse() returned ERR");
1816         return NULL;
1817     }
1818     return Py_BuildValue("(hiiil)",
1819                          (short)event.id,
1820                          event.x, event.y, event.z,
1821                          (long) event.bstate);
1822 }
1823 
1824 static PyObject *
PyCurses_UngetMouse(PyObject * self,PyObject * args)1825 PyCurses_UngetMouse(PyObject *self, PyObject *args)
1826 {
1827     MEVENT event;
1828 
1829     PyCursesInitialised;
1830     if (!PyArg_ParseTuple(args, "hiiil",
1831                           &event.id,
1832                           &event.x, &event.y, &event.z,
1833                           (int *) &event.bstate))
1834         return NULL;
1835 
1836     return PyCursesCheckERR(ungetmouse(&event), "ungetmouse");
1837 }
1838 #endif
1839 
1840 static PyObject *
PyCurses_GetWin(PyCursesWindowObject * self,PyObject * temp)1841 PyCurses_GetWin(PyCursesWindowObject *self, PyObject *temp)
1842 {
1843     WINDOW *win;
1844 
1845     PyCursesInitialised;
1846 
1847     if (!PyFile_Check(temp)) {
1848         PyErr_SetString(PyExc_TypeError, "argument must be a file object");
1849         return NULL;
1850     }
1851 
1852     win = getwin(PyFile_AsFile(temp));
1853 
1854     if (win == NULL) {
1855         PyErr_SetString(PyCursesError, catchall_NULL);
1856         return NULL;
1857     }
1858 
1859     return PyCursesWindow_New(win);
1860 }
1861 
1862 static PyObject *
PyCurses_HalfDelay(PyObject * self,PyObject * args)1863 PyCurses_HalfDelay(PyObject *self, PyObject *args)
1864 {
1865     unsigned char tenths;
1866 
1867     PyCursesInitialised;
1868 
1869     if (!PyArg_ParseTuple(args, "b:halfdelay", &tenths)) return NULL;
1870 
1871     return PyCursesCheckERR(halfdelay(tenths), "halfdelay");
1872 }
1873 
1874 #ifndef STRICT_SYSV_CURSES
1875 /* No has_key! */
PyCurses_has_key(PyObject * self,PyObject * args)1876 static PyObject * PyCurses_has_key(PyObject *self, PyObject *args)
1877 {
1878     int ch;
1879 
1880     PyCursesInitialised;
1881 
1882     if (!PyArg_ParseTuple(args,"i",&ch)) return NULL;
1883 
1884     if (has_key(ch) == FALSE) {
1885         Py_INCREF(Py_False);
1886         return Py_False;
1887     }
1888     Py_INCREF(Py_True);
1889     return Py_True;
1890 }
1891 #endif /* STRICT_SYSV_CURSES */
1892 
1893 static PyObject *
PyCurses_Init_Color(PyObject * self,PyObject * args)1894 PyCurses_Init_Color(PyObject *self, PyObject *args)
1895 {
1896     short color, r, g, b;
1897 
1898     PyCursesInitialised;
1899     PyCursesInitialisedColor;
1900 
1901     switch(PyTuple_Size(args)) {
1902     case 4:
1903         if (!PyArg_ParseTuple(args, "hhhh;color,r,g,b", &color, &r, &g, &b)) return NULL;
1904         break;
1905     default:
1906         PyErr_SetString(PyExc_TypeError, "init_color requires 4 arguments");
1907         return NULL;
1908     }
1909 
1910     return PyCursesCheckERR(init_color(color, r, g, b), "init_color");
1911 }
1912 
1913 static PyObject *
PyCurses_Init_Pair(PyObject * self,PyObject * args)1914 PyCurses_Init_Pair(PyObject *self, PyObject *args)
1915 {
1916     short pair, f, b;
1917 
1918     PyCursesInitialised;
1919     PyCursesInitialisedColor;
1920 
1921     if (PyTuple_Size(args) != 3) {
1922         PyErr_SetString(PyExc_TypeError, "init_pair requires 3 arguments");
1923         return NULL;
1924     }
1925 
1926     if (!PyArg_ParseTuple(args, "hhh;pair, f, b", &pair, &f, &b)) return NULL;
1927 
1928     return PyCursesCheckERR(init_pair(pair, f, b), "init_pair");
1929 }
1930 
1931 static PyObject *ModDict;
1932 
1933 static PyObject *
PyCurses_InitScr(PyObject * self)1934 PyCurses_InitScr(PyObject *self)
1935 {
1936     WINDOW *win;
1937 
1938     if (initialised == TRUE) {
1939         wrefresh(stdscr);
1940         return (PyObject *)PyCursesWindow_New(stdscr);
1941     }
1942 
1943     win = initscr();
1944 
1945     if (win == NULL) {
1946         PyErr_SetString(PyCursesError, catchall_NULL);
1947         return NULL;
1948     }
1949 
1950     initialised = initialised_setupterm = TRUE;
1951 
1952 /* This was moved from initcurses() because it core dumped on SGI,
1953    where they're not defined until you've called initscr() */
1954 #define SetDictInt(string,ch)                                           \
1955     do {                                                                \
1956         PyObject *o = PyInt_FromLong((long) (ch));                      \
1957         if (o && PyDict_SetItemString(ModDict, string, o) == 0)     {   \
1958             Py_DECREF(o);                                               \
1959         }                                                               \
1960     } while (0)
1961 
1962     /* Here are some graphic symbols you can use */
1963     SetDictInt("ACS_ULCORNER",      (ACS_ULCORNER));
1964     SetDictInt("ACS_LLCORNER",      (ACS_LLCORNER));
1965     SetDictInt("ACS_URCORNER",      (ACS_URCORNER));
1966     SetDictInt("ACS_LRCORNER",      (ACS_LRCORNER));
1967     SetDictInt("ACS_LTEE",          (ACS_LTEE));
1968     SetDictInt("ACS_RTEE",          (ACS_RTEE));
1969     SetDictInt("ACS_BTEE",          (ACS_BTEE));
1970     SetDictInt("ACS_TTEE",          (ACS_TTEE));
1971     SetDictInt("ACS_HLINE",         (ACS_HLINE));
1972     SetDictInt("ACS_VLINE",         (ACS_VLINE));
1973     SetDictInt("ACS_PLUS",          (ACS_PLUS));
1974 #if !defined(__hpux) || defined(HAVE_NCURSES_H)
1975     /* On HP/UX 11, these are of type cchar_t, which is not an
1976        integral type. If this is a problem on more platforms, a
1977        configure test should be added to determine whether ACS_S1
1978        is of integral type. */
1979     SetDictInt("ACS_S1",            (ACS_S1));
1980     SetDictInt("ACS_S9",            (ACS_S9));
1981     SetDictInt("ACS_DIAMOND",       (ACS_DIAMOND));
1982     SetDictInt("ACS_CKBOARD",       (ACS_CKBOARD));
1983     SetDictInt("ACS_DEGREE",        (ACS_DEGREE));
1984     SetDictInt("ACS_PLMINUS",       (ACS_PLMINUS));
1985     SetDictInt("ACS_BULLET",        (ACS_BULLET));
1986     SetDictInt("ACS_LARROW",        (ACS_LARROW));
1987     SetDictInt("ACS_RARROW",        (ACS_RARROW));
1988     SetDictInt("ACS_DARROW",        (ACS_DARROW));
1989     SetDictInt("ACS_UARROW",        (ACS_UARROW));
1990     SetDictInt("ACS_BOARD",         (ACS_BOARD));
1991     SetDictInt("ACS_LANTERN",       (ACS_LANTERN));
1992     SetDictInt("ACS_BLOCK",         (ACS_BLOCK));
1993 #endif
1994     SetDictInt("ACS_BSSB",          (ACS_ULCORNER));
1995     SetDictInt("ACS_SSBB",          (ACS_LLCORNER));
1996     SetDictInt("ACS_BBSS",          (ACS_URCORNER));
1997     SetDictInt("ACS_SBBS",          (ACS_LRCORNER));
1998     SetDictInt("ACS_SBSS",          (ACS_RTEE));
1999     SetDictInt("ACS_SSSB",          (ACS_LTEE));
2000     SetDictInt("ACS_SSBS",          (ACS_BTEE));
2001     SetDictInt("ACS_BSSS",          (ACS_TTEE));
2002     SetDictInt("ACS_BSBS",          (ACS_HLINE));
2003     SetDictInt("ACS_SBSB",          (ACS_VLINE));
2004     SetDictInt("ACS_SSSS",          (ACS_PLUS));
2005 
2006     /* The following are never available with strict SYSV curses */
2007 #ifdef ACS_S3
2008     SetDictInt("ACS_S3",            (ACS_S3));
2009 #endif
2010 #ifdef ACS_S7
2011     SetDictInt("ACS_S7",            (ACS_S7));
2012 #endif
2013 #ifdef ACS_LEQUAL
2014     SetDictInt("ACS_LEQUAL",        (ACS_LEQUAL));
2015 #endif
2016 #ifdef ACS_GEQUAL
2017     SetDictInt("ACS_GEQUAL",        (ACS_GEQUAL));
2018 #endif
2019 #ifdef ACS_PI
2020     SetDictInt("ACS_PI",            (ACS_PI));
2021 #endif
2022 #ifdef ACS_NEQUAL
2023     SetDictInt("ACS_NEQUAL",        (ACS_NEQUAL));
2024 #endif
2025 #ifdef ACS_STERLING
2026     SetDictInt("ACS_STERLING",      (ACS_STERLING));
2027 #endif
2028 
2029     SetDictInt("LINES", LINES);
2030     SetDictInt("COLS", COLS);
2031 
2032     return (PyObject *)PyCursesWindow_New(win);
2033 }
2034 
2035 static PyObject *
PyCurses_setupterm(PyObject * self,PyObject * args,PyObject * keywds)2036 PyCurses_setupterm(PyObject* self, PyObject *args, PyObject* keywds)
2037 {
2038     int fd = -1;
2039     int err;
2040     char* termstr = NULL;
2041 
2042     static char *kwlist[] = {"term", "fd", NULL};
2043 
2044     if (!PyArg_ParseTupleAndKeywords(
2045             args, keywds, "|zi:setupterm", kwlist, &termstr, &fd)) {
2046         return NULL;
2047     }
2048 
2049     if (fd == -1) {
2050         PyObject* sys_stdout;
2051 
2052         sys_stdout = PySys_GetObject("stdout");
2053 
2054         if (sys_stdout == NULL) {
2055             PyErr_SetString(
2056                 PyCursesError,
2057                 "lost sys.stdout");
2058             return NULL;
2059         }
2060 
2061         fd = PyObject_AsFileDescriptor(sys_stdout);
2062 
2063         if (fd == -1) {
2064             return NULL;
2065         }
2066     }
2067 
2068     if (!initialised_setupterm && setupterm(termstr,fd,&err) == ERR) {
2069         char* s = "setupterm: unknown error";
2070 
2071         if (err == 0) {
2072             s = "setupterm: could not find terminal";
2073         } else if (err == -1) {
2074             s = "setupterm: could not find terminfo database";
2075         }
2076 
2077         PyErr_SetString(PyCursesError,s);
2078         return NULL;
2079     }
2080 
2081     initialised_setupterm = TRUE;
2082 
2083     Py_INCREF(Py_None);
2084     return Py_None;
2085 }
2086 
2087 static PyObject *
PyCurses_IntrFlush(PyObject * self,PyObject * args)2088 PyCurses_IntrFlush(PyObject *self, PyObject *args)
2089 {
2090     int ch;
2091 
2092     PyCursesInitialised;
2093 
2094     switch(PyTuple_Size(args)) {
2095     case 1:
2096         if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL;
2097         break;
2098     default:
2099         PyErr_SetString(PyExc_TypeError, "intrflush requires 1 argument");
2100         return NULL;
2101     }
2102 
2103     return PyCursesCheckERR(intrflush(NULL,ch), "intrflush");
2104 }
2105 
2106 #ifdef HAVE_CURSES_IS_TERM_RESIZED
2107 static PyObject *
PyCurses_Is_Term_Resized(PyObject * self,PyObject * args)2108 PyCurses_Is_Term_Resized(PyObject *self, PyObject *args)
2109 {
2110     int lines;
2111     int columns;
2112     int result;
2113 
2114     PyCursesInitialised;
2115 
2116     if (!PyArg_ParseTuple(args,"ii:is_term_resized", &lines, &columns))
2117         return NULL;
2118     result = is_term_resized(lines, columns);
2119     if (result == TRUE) {
2120         Py_INCREF(Py_True);
2121         return Py_True;
2122     } else {
2123         Py_INCREF(Py_False);
2124         return Py_False;
2125     }
2126 }
2127 #endif /* HAVE_CURSES_IS_TERM_RESIZED */
2128 
2129 #if !defined(__NetBSD__)
2130 static PyObject *
PyCurses_KeyName(PyObject * self,PyObject * args)2131 PyCurses_KeyName(PyObject *self, PyObject *args)
2132 {
2133     const char *knp;
2134     int ch;
2135 
2136     PyCursesInitialised;
2137 
2138     if (!PyArg_ParseTuple(args,"i",&ch)) return NULL;
2139 
2140     if (ch < 0) {
2141         PyErr_SetString(PyExc_ValueError, "invalid key number");
2142         return NULL;
2143     }
2144     knp = keyname(ch);
2145 
2146     return PyString_FromString((knp == NULL) ? "" : (char *)knp);
2147 }
2148 #endif
2149 
2150 static PyObject *
PyCurses_KillChar(PyObject * self)2151 PyCurses_KillChar(PyObject *self)
2152 {
2153     char ch;
2154 
2155     ch = killchar();
2156 
2157     return PyString_FromStringAndSize(&ch, 1);
2158 }
2159 
2160 static PyObject *
PyCurses_Meta(PyObject * self,PyObject * args)2161 PyCurses_Meta(PyObject *self, PyObject *args)
2162 {
2163     int ch;
2164 
2165     PyCursesInitialised;
2166 
2167     switch(PyTuple_Size(args)) {
2168     case 1:
2169         if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL;
2170         break;
2171     default:
2172         PyErr_SetString(PyExc_TypeError, "meta requires 1 argument");
2173         return NULL;
2174     }
2175 
2176     return PyCursesCheckERR(meta(stdscr, ch), "meta");
2177 }
2178 
2179 #ifdef NCURSES_MOUSE_VERSION
2180 static PyObject *
PyCurses_MouseInterval(PyObject * self,PyObject * args)2181 PyCurses_MouseInterval(PyObject *self, PyObject *args)
2182 {
2183     int interval;
2184     PyCursesInitialised;
2185 
2186     if (!PyArg_ParseTuple(args,"i;interval",&interval))
2187         return NULL;
2188     return PyCursesCheckERR(mouseinterval(interval), "mouseinterval");
2189 }
2190 
2191 static PyObject *
PyCurses_MouseMask(PyObject * self,PyObject * args)2192 PyCurses_MouseMask(PyObject *self, PyObject *args)
2193 {
2194     int newmask;
2195     mmask_t oldmask, availmask;
2196 
2197     PyCursesInitialised;
2198     if (!PyArg_ParseTuple(args,"i;mousemask",&newmask))
2199         return NULL;
2200     availmask = mousemask(newmask, &oldmask);
2201     return Py_BuildValue("(ll)", (long)availmask, (long)oldmask);
2202 }
2203 #endif
2204 
2205 static PyObject *
PyCurses_Napms(PyObject * self,PyObject * args)2206 PyCurses_Napms(PyObject *self, PyObject *args)
2207 {
2208     int ms;
2209 
2210     PyCursesInitialised;
2211     if (!PyArg_ParseTuple(args, "i;ms", &ms)) return NULL;
2212 
2213     return Py_BuildValue("i", napms(ms));
2214 }
2215 
2216 
2217 static PyObject *
PyCurses_NewPad(PyObject * self,PyObject * args)2218 PyCurses_NewPad(PyObject *self, PyObject *args)
2219 {
2220     WINDOW *win;
2221     int nlines, ncols;
2222 
2223     PyCursesInitialised;
2224 
2225     if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols)) return NULL;
2226 
2227     win = newpad(nlines, ncols);
2228 
2229     if (win == NULL) {
2230         PyErr_SetString(PyCursesError, catchall_NULL);
2231         return NULL;
2232     }
2233 
2234     return (PyObject *)PyCursesWindow_New(win);
2235 }
2236 
2237 static PyObject *
PyCurses_NewWindow(PyObject * self,PyObject * args)2238 PyCurses_NewWindow(PyObject *self, PyObject *args)
2239 {
2240     WINDOW *win;
2241     int nlines, ncols, begin_y=0, begin_x=0;
2242 
2243     PyCursesInitialised;
2244 
2245     switch (PyTuple_Size(args)) {
2246     case 2:
2247         if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols))
2248             return NULL;
2249         break;
2250     case 4:
2251         if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
2252                               &nlines,&ncols,&begin_y,&begin_x))
2253             return NULL;
2254         break;
2255     default:
2256         PyErr_SetString(PyExc_TypeError, "newwin requires 2 or 4 arguments");
2257         return NULL;
2258     }
2259 
2260     win = newwin(nlines,ncols,begin_y,begin_x);
2261     if (win == NULL) {
2262         PyErr_SetString(PyCursesError, catchall_NULL);
2263         return NULL;
2264     }
2265 
2266     return (PyObject *)PyCursesWindow_New(win);
2267 }
2268 
2269 static PyObject *
PyCurses_Pair_Content(PyObject * self,PyObject * args)2270 PyCurses_Pair_Content(PyObject *self, PyObject *args)
2271 {
2272     short pair,f,b;
2273 
2274     PyCursesInitialised;
2275     PyCursesInitialisedColor;
2276 
2277     switch(PyTuple_Size(args)) {
2278     case 1:
2279         if (!PyArg_ParseTuple(args, "h;pair", &pair)) return NULL;
2280         break;
2281     default:
2282         PyErr_SetString(PyExc_TypeError, "pair_content requires 1 argument");
2283         return NULL;
2284     }
2285 
2286     if (pair_content(pair, &f, &b)==ERR) {
2287         PyErr_SetString(PyCursesError,
2288                         "Argument 1 was out of range. (1..COLOR_PAIRS-1)");
2289         return NULL;
2290     }
2291 
2292     return Py_BuildValue("(ii)", f, b);
2293 }
2294 
2295 static PyObject *
PyCurses_pair_number(PyObject * self,PyObject * args)2296 PyCurses_pair_number(PyObject *self, PyObject *args)
2297 {
2298     int n;
2299 
2300     PyCursesInitialised;
2301     PyCursesInitialisedColor;
2302 
2303     switch(PyTuple_Size(args)) {
2304     case 1:
2305         if (!PyArg_ParseTuple(args, "i;pairvalue", &n)) return NULL;
2306         break;
2307     default:
2308         PyErr_SetString(PyExc_TypeError,
2309                         "pair_number requires 1 argument");
2310         return NULL;
2311     }
2312 
2313     return PyInt_FromLong((long) ((n & A_COLOR) >> 8));
2314 }
2315 
2316 static PyObject *
PyCurses_Putp(PyObject * self,PyObject * args)2317 PyCurses_Putp(PyObject *self, PyObject *args)
2318 {
2319     char *str;
2320 
2321     if (!PyArg_ParseTuple(args,"s;str", &str)) return NULL;
2322     return PyCursesCheckERR(putp(str), "putp");
2323 }
2324 
2325 static PyObject *
PyCurses_QiFlush(PyObject * self,PyObject * args)2326 PyCurses_QiFlush(PyObject *self, PyObject *args)
2327 {
2328     int flag = 0;
2329 
2330     PyCursesInitialised;
2331 
2332     switch(PyTuple_Size(args)) {
2333     case 0:
2334         qiflush();
2335         Py_INCREF(Py_None);
2336         return Py_None;
2337     case 1:
2338         if (!PyArg_ParseTuple(args, "i;True(1) or False(0)", &flag)) return NULL;
2339         if (flag) qiflush();
2340         else noqiflush();
2341         Py_INCREF(Py_None);
2342         return Py_None;
2343     default:
2344         PyErr_SetString(PyExc_TypeError, "qiflush requires 0 or 1 arguments");
2345         return NULL;
2346     }
2347 }
2348 
2349 /* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES
2350  * and _curses.COLS */
2351 #if defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM)
2352 static int
update_lines_cols(void)2353 update_lines_cols(void)
2354 {
2355     PyObject *o;
2356     PyObject *m = PyImport_ImportModuleNoBlock("curses");
2357 
2358     if (!m)
2359         return 0;
2360 
2361     o = PyInt_FromLong(LINES);
2362     if (!o) {
2363         Py_DECREF(m);
2364         return 0;
2365     }
2366     if (PyObject_SetAttrString(m, "LINES", o)) {
2367         Py_DECREF(m);
2368         Py_DECREF(o);
2369         return 0;
2370     }
2371     if (PyDict_SetItemString(ModDict, "LINES", o)) {
2372         Py_DECREF(m);
2373         Py_DECREF(o);
2374         return 0;
2375     }
2376     Py_DECREF(o);
2377     o = PyInt_FromLong(COLS);
2378     if (!o) {
2379         Py_DECREF(m);
2380         return 0;
2381     }
2382     if (PyObject_SetAttrString(m, "COLS", o)) {
2383         Py_DECREF(m);
2384         Py_DECREF(o);
2385         return 0;
2386     }
2387     if (PyDict_SetItemString(ModDict, "COLS", o)) {
2388         Py_DECREF(m);
2389         Py_DECREF(o);
2390         return 0;
2391     }
2392     Py_DECREF(o);
2393     Py_DECREF(m);
2394     return 1;
2395 }
2396 #endif
2397 
2398 #ifdef HAVE_CURSES_RESIZETERM
2399 static PyObject *
PyCurses_ResizeTerm(PyObject * self,PyObject * args)2400 PyCurses_ResizeTerm(PyObject *self, PyObject *args)
2401 {
2402     int lines;
2403     int columns;
2404     PyObject *result;
2405 
2406     PyCursesInitialised;
2407 
2408     if (!PyArg_ParseTuple(args,"ii:resizeterm", &lines, &columns))
2409         return NULL;
2410 
2411     result = PyCursesCheckERR(resizeterm(lines, columns), "resizeterm");
2412     if (!result)
2413         return NULL;
2414     if (!update_lines_cols())
2415         return NULL;
2416     return result;
2417 }
2418 
2419 #endif
2420 
2421 #ifdef HAVE_CURSES_RESIZE_TERM
2422 static PyObject *
PyCurses_Resize_Term(PyObject * self,PyObject * args)2423 PyCurses_Resize_Term(PyObject *self, PyObject *args)
2424 {
2425     int lines;
2426     int columns;
2427 
2428     PyObject *result;
2429 
2430     PyCursesInitialised;
2431 
2432     if (!PyArg_ParseTuple(args,"ii:resize_term", &lines, &columns))
2433         return NULL;
2434 
2435     result = PyCursesCheckERR(resize_term(lines, columns), "resize_term");
2436     if (!result)
2437         return NULL;
2438     if (!update_lines_cols())
2439         return NULL;
2440     return result;
2441 }
2442 #endif /* HAVE_CURSES_RESIZE_TERM */
2443 
2444 static PyObject *
PyCurses_setsyx(PyObject * self,PyObject * args)2445 PyCurses_setsyx(PyObject *self, PyObject *args)
2446 {
2447     int y,x;
2448 
2449     PyCursesInitialised;
2450 
2451     if (PyTuple_Size(args)!=2) {
2452         PyErr_SetString(PyExc_TypeError, "setsyx requires 2 arguments");
2453         return NULL;
2454     }
2455 
2456     if (!PyArg_ParseTuple(args, "ii;y, x", &y, &x)) return NULL;
2457 
2458     setsyx(y,x);
2459 
2460     Py_INCREF(Py_None);
2461     return Py_None;
2462 }
2463 
2464 static PyObject *
PyCurses_Start_Color(PyObject * self)2465 PyCurses_Start_Color(PyObject *self)
2466 {
2467     int code;
2468     PyObject *c, *cp;
2469 
2470     PyCursesInitialised;
2471 
2472     code = start_color();
2473     if (code != ERR) {
2474         initialisedcolors = TRUE;
2475         c = PyInt_FromLong((long) COLORS);
2476         PyDict_SetItemString(ModDict, "COLORS", c);
2477         Py_DECREF(c);
2478         cp = PyInt_FromLong((long) COLOR_PAIRS);
2479         PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp);
2480         Py_DECREF(cp);
2481         Py_INCREF(Py_None);
2482         return Py_None;
2483     } else {
2484         PyErr_SetString(PyCursesError, "start_color() returned ERR");
2485         return NULL;
2486     }
2487 }
2488 
2489 static PyObject *
PyCurses_tigetflag(PyObject * self,PyObject * args)2490 PyCurses_tigetflag(PyObject *self, PyObject *args)
2491 {
2492     char *capname;
2493 
2494     PyCursesSetupTermCalled;
2495 
2496     if (!PyArg_ParseTuple(args, "s", &capname))
2497         return NULL;
2498 
2499     return PyInt_FromLong( (long) tigetflag( capname ) );
2500 }
2501 
2502 static PyObject *
PyCurses_tigetnum(PyObject * self,PyObject * args)2503 PyCurses_tigetnum(PyObject *self, PyObject *args)
2504 {
2505     char *capname;
2506 
2507     PyCursesSetupTermCalled;
2508 
2509     if (!PyArg_ParseTuple(args, "s", &capname))
2510         return NULL;
2511 
2512     return PyInt_FromLong( (long) tigetnum( capname ) );
2513 }
2514 
2515 static PyObject *
PyCurses_tigetstr(PyObject * self,PyObject * args)2516 PyCurses_tigetstr(PyObject *self, PyObject *args)
2517 {
2518     char *capname;
2519 
2520     PyCursesSetupTermCalled;
2521 
2522     if (!PyArg_ParseTuple(args, "s", &capname))
2523         return NULL;
2524 
2525     capname = tigetstr( capname );
2526     if (capname == 0 || capname == (char*) -1) {
2527         Py_INCREF(Py_None);
2528         return Py_None;
2529     }
2530     return PyString_FromString( capname );
2531 }
2532 
2533 static PyObject *
PyCurses_tparm(PyObject * self,PyObject * args)2534 PyCurses_tparm(PyObject *self, PyObject *args)
2535 {
2536     char* fmt;
2537     char* result = NULL;
2538     int i1=0,i2=0,i3=0,i4=0,i5=0,i6=0,i7=0,i8=0,i9=0;
2539 
2540     PyCursesSetupTermCalled;
2541 
2542     if (!PyArg_ParseTuple(args, "s|iiiiiiiii:tparm",
2543                           &fmt, &i1, &i2, &i3, &i4,
2544                           &i5, &i6, &i7, &i8, &i9)) {
2545         return NULL;
2546     }
2547 
2548     result = tparm(fmt,i1,i2,i3,i4,i5,i6,i7,i8,i9);
2549     if (!result) {
2550         PyErr_SetString(PyCursesError, "tparm() returned NULL");
2551         return NULL;
2552     }
2553 
2554     return PyString_FromString(result);
2555 }
2556 
2557 static PyObject *
PyCurses_TypeAhead(PyObject * self,PyObject * args)2558 PyCurses_TypeAhead(PyObject *self, PyObject *args)
2559 {
2560     int fd;
2561 
2562     PyCursesInitialised;
2563 
2564     if (!PyArg_ParseTuple(args,"i;fd",&fd)) return NULL;
2565 
2566     return PyCursesCheckERR(typeahead( fd ), "typeahead");
2567 }
2568 
2569 static PyObject *
PyCurses_UnCtrl(PyObject * self,PyObject * args)2570 PyCurses_UnCtrl(PyObject *self, PyObject *args)
2571 {
2572     PyObject *temp;
2573     chtype ch;
2574 
2575     PyCursesInitialised;
2576 
2577     if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
2578 
2579     if (PyInt_Check(temp))
2580         ch = (chtype) PyInt_AsLong(temp);
2581     else if (PyString_Check(temp))
2582         ch = (chtype) *PyString_AsString(temp);
2583     else {
2584         PyErr_SetString(PyExc_TypeError, "argument must be a ch or an int");
2585         return NULL;
2586     }
2587 
2588     return PyString_FromString(unctrl(ch));
2589 }
2590 
2591 static PyObject *
PyCurses_UngetCh(PyObject * self,PyObject * args)2592 PyCurses_UngetCh(PyObject *self, PyObject *args)
2593 {
2594     PyObject *temp;
2595     int ch;
2596 
2597     PyCursesInitialised;
2598 
2599     if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
2600 
2601     if (PyInt_Check(temp))
2602         ch = (int) PyInt_AsLong(temp);
2603     else if (PyString_Check(temp))
2604         ch = (int) *PyString_AsString(temp);
2605     else {
2606         PyErr_SetString(PyExc_TypeError, "argument must be a ch or an int");
2607         return NULL;
2608     }
2609 
2610     return PyCursesCheckERR(ungetch(ch), "ungetch");
2611 }
2612 
2613 static PyObject *
PyCurses_Use_Env(PyObject * self,PyObject * args)2614 PyCurses_Use_Env(PyObject *self, PyObject *args)
2615 {
2616     int flag;
2617 
2618     switch(PyTuple_Size(args)) {
2619     case 1:
2620         if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&flag))
2621             return NULL;
2622         break;
2623     default:
2624         PyErr_SetString(PyExc_TypeError, "use_env requires 1 argument");
2625         return NULL;
2626     }
2627     use_env(flag);
2628     Py_INCREF(Py_None);
2629     return Py_None;
2630 }
2631 
2632 #ifndef STRICT_SYSV_CURSES
2633 static PyObject *
PyCurses_Use_Default_Colors(PyObject * self)2634 PyCurses_Use_Default_Colors(PyObject *self)
2635 {
2636     int code;
2637 
2638     PyCursesInitialised;
2639     PyCursesInitialisedColor;
2640 
2641     code = use_default_colors();
2642     if (code != ERR) {
2643         Py_INCREF(Py_None);
2644         return Py_None;
2645     } else {
2646         PyErr_SetString(PyCursesError, "use_default_colors() returned ERR");
2647         return NULL;
2648     }
2649 }
2650 #endif /* STRICT_SYSV_CURSES */
2651 
2652 /* List of functions defined in the module */
2653 
2654 static PyMethodDef PyCurses_methods[] = {
2655     {"baudrate",            (PyCFunction)PyCurses_baudrate, METH_NOARGS},
2656     {"beep",                (PyCFunction)PyCurses_beep, METH_NOARGS},
2657     {"can_change_color",    (PyCFunction)PyCurses_can_change_color, METH_NOARGS},
2658     {"cbreak",              (PyCFunction)PyCurses_cbreak, METH_VARARGS},
2659     {"color_content",       (PyCFunction)PyCurses_Color_Content, METH_VARARGS},
2660     {"color_pair",          (PyCFunction)PyCurses_color_pair, METH_VARARGS},
2661     {"curs_set",            (PyCFunction)PyCurses_Curs_Set, METH_VARARGS},
2662     {"def_prog_mode",       (PyCFunction)PyCurses_def_prog_mode, METH_NOARGS},
2663     {"def_shell_mode",      (PyCFunction)PyCurses_def_shell_mode, METH_NOARGS},
2664     {"delay_output",        (PyCFunction)PyCurses_Delay_Output, METH_VARARGS},
2665     {"doupdate",            (PyCFunction)PyCurses_doupdate, METH_NOARGS},
2666     {"echo",                (PyCFunction)PyCurses_echo, METH_VARARGS},
2667     {"endwin",              (PyCFunction)PyCurses_endwin, METH_NOARGS},
2668     {"erasechar",           (PyCFunction)PyCurses_EraseChar, METH_NOARGS},
2669     {"filter",              (PyCFunction)PyCurses_filter, METH_NOARGS},
2670     {"flash",               (PyCFunction)PyCurses_flash, METH_NOARGS},
2671     {"flushinp",            (PyCFunction)PyCurses_flushinp, METH_NOARGS},
2672 #ifdef NCURSES_MOUSE_VERSION
2673     {"getmouse",            (PyCFunction)PyCurses_GetMouse, METH_NOARGS},
2674     {"ungetmouse",          (PyCFunction)PyCurses_UngetMouse, METH_VARARGS},
2675 #endif
2676     {"getsyx",              (PyCFunction)PyCurses_getsyx, METH_NOARGS},
2677     {"getwin",              (PyCFunction)PyCurses_GetWin, METH_O},
2678     {"has_colors",          (PyCFunction)PyCurses_has_colors, METH_NOARGS},
2679     {"has_ic",              (PyCFunction)PyCurses_has_ic, METH_NOARGS},
2680     {"has_il",              (PyCFunction)PyCurses_has_il, METH_NOARGS},
2681 #ifndef STRICT_SYSV_CURSES
2682     {"has_key",             (PyCFunction)PyCurses_has_key, METH_VARARGS},
2683 #endif
2684     {"halfdelay",           (PyCFunction)PyCurses_HalfDelay, METH_VARARGS},
2685     {"init_color",          (PyCFunction)PyCurses_Init_Color, METH_VARARGS},
2686     {"init_pair",           (PyCFunction)PyCurses_Init_Pair, METH_VARARGS},
2687     {"initscr",             (PyCFunction)PyCurses_InitScr, METH_NOARGS},
2688     {"intrflush",           (PyCFunction)PyCurses_IntrFlush, METH_VARARGS},
2689     {"isendwin",            (PyCFunction)PyCurses_isendwin, METH_NOARGS},
2690 #ifdef HAVE_CURSES_IS_TERM_RESIZED
2691     {"is_term_resized",     (PyCFunction)PyCurses_Is_Term_Resized, METH_VARARGS},
2692 #endif
2693 #if !defined(__NetBSD__)
2694     {"keyname",             (PyCFunction)PyCurses_KeyName, METH_VARARGS},
2695 #endif
2696     {"killchar",            (PyCFunction)PyCurses_KillChar, METH_NOARGS},
2697     {"longname",            (PyCFunction)PyCurses_longname, METH_NOARGS},
2698     {"meta",                (PyCFunction)PyCurses_Meta, METH_VARARGS},
2699 #ifdef NCURSES_MOUSE_VERSION
2700     {"mouseinterval",       (PyCFunction)PyCurses_MouseInterval, METH_VARARGS},
2701     {"mousemask",           (PyCFunction)PyCurses_MouseMask, METH_VARARGS},
2702 #endif
2703     {"napms",               (PyCFunction)PyCurses_Napms, METH_VARARGS},
2704     {"newpad",              (PyCFunction)PyCurses_NewPad, METH_VARARGS},
2705     {"newwin",              (PyCFunction)PyCurses_NewWindow, METH_VARARGS},
2706     {"nl",                  (PyCFunction)PyCurses_nl, METH_VARARGS},
2707     {"nocbreak",            (PyCFunction)PyCurses_nocbreak, METH_NOARGS},
2708     {"noecho",              (PyCFunction)PyCurses_noecho, METH_NOARGS},
2709     {"nonl",                (PyCFunction)PyCurses_nonl, METH_NOARGS},
2710     {"noqiflush",           (PyCFunction)PyCurses_noqiflush, METH_NOARGS},
2711     {"noraw",               (PyCFunction)PyCurses_noraw, METH_NOARGS},
2712     {"pair_content",        (PyCFunction)PyCurses_Pair_Content, METH_VARARGS},
2713     {"pair_number",         (PyCFunction)PyCurses_pair_number, METH_VARARGS},
2714     {"putp",                (PyCFunction)PyCurses_Putp, METH_VARARGS},
2715     {"qiflush",             (PyCFunction)PyCurses_QiFlush, METH_VARARGS},
2716     {"raw",                 (PyCFunction)PyCurses_raw, METH_VARARGS},
2717     {"reset_prog_mode",     (PyCFunction)PyCurses_reset_prog_mode, METH_NOARGS},
2718     {"reset_shell_mode",    (PyCFunction)PyCurses_reset_shell_mode, METH_NOARGS},
2719     {"resetty",             (PyCFunction)PyCurses_resetty, METH_NOARGS},
2720 #ifdef HAVE_CURSES_RESIZETERM
2721     {"resizeterm",          (PyCFunction)PyCurses_ResizeTerm, METH_VARARGS},
2722 #endif
2723 #ifdef HAVE_CURSES_RESIZE_TERM
2724     {"resize_term",         (PyCFunction)PyCurses_Resize_Term, METH_VARARGS},
2725 #endif
2726     {"savetty",             (PyCFunction)PyCurses_savetty, METH_NOARGS},
2727     {"setsyx",              (PyCFunction)PyCurses_setsyx, METH_VARARGS},
2728     {"setupterm",           (PyCFunction)PyCurses_setupterm,
2729      METH_VARARGS|METH_KEYWORDS},
2730     {"start_color",         (PyCFunction)PyCurses_Start_Color, METH_NOARGS},
2731     {"termattrs",           (PyCFunction)PyCurses_termattrs, METH_NOARGS},
2732     {"termname",            (PyCFunction)PyCurses_termname, METH_NOARGS},
2733     {"tigetflag",           (PyCFunction)PyCurses_tigetflag, METH_VARARGS},
2734     {"tigetnum",            (PyCFunction)PyCurses_tigetnum, METH_VARARGS},
2735     {"tigetstr",            (PyCFunction)PyCurses_tigetstr, METH_VARARGS},
2736     {"tparm",               (PyCFunction)PyCurses_tparm, METH_VARARGS},
2737     {"typeahead",           (PyCFunction)PyCurses_TypeAhead, METH_VARARGS},
2738     {"unctrl",              (PyCFunction)PyCurses_UnCtrl, METH_VARARGS},
2739     {"ungetch",             (PyCFunction)PyCurses_UngetCh, METH_VARARGS},
2740     {"use_env",             (PyCFunction)PyCurses_Use_Env, METH_VARARGS},
2741 #ifndef STRICT_SYSV_CURSES
2742     {"use_default_colors",  (PyCFunction)PyCurses_Use_Default_Colors, METH_NOARGS},
2743 #endif
2744     {NULL,                  NULL}         /* sentinel */
2745 };
2746 
2747 /* Initialization function for the module */
2748 
2749 PyMODINIT_FUNC
init_curses(void)2750 init_curses(void)
2751 {
2752     PyObject *m, *d, *v, *c_api_object;
2753     static void *PyCurses_API[PyCurses_API_pointers];
2754 
2755     /* Initialize object type */
2756     Py_TYPE(&PyCursesWindow_Type) = &PyType_Type;
2757 
2758     /* Initialize the C API pointer array */
2759     PyCurses_API[0] = (void *)&PyCursesWindow_Type;
2760     PyCurses_API[1] = (void *)func_PyCursesSetupTermCalled;
2761     PyCurses_API[2] = (void *)func_PyCursesInitialised;
2762     PyCurses_API[3] = (void *)func_PyCursesInitialisedColor;
2763 
2764     /* Create the module and add the functions */
2765     m = Py_InitModule("_curses", PyCurses_methods);
2766     if (m == NULL)
2767         return;
2768 
2769     /* Add some symbolic constants to the module */
2770     d = PyModule_GetDict(m);
2771     if (d == NULL)
2772         return;
2773     ModDict = d; /* For PyCurses_InitScr to use later */
2774 
2775     /* Add a capsule for the C API */
2776     c_api_object = PyCapsule_New(PyCurses_API, PyCurses_CAPSULE_NAME, NULL);
2777     PyDict_SetItemString(d, "_C_API", c_api_object);
2778     Py_DECREF(c_api_object);
2779 
2780     /* For exception curses.error */
2781     PyCursesError = PyErr_NewException("_curses.error", NULL, NULL);
2782     PyDict_SetItemString(d, "error", PyCursesError);
2783 
2784     /* Make the version available */
2785     v = PyString_FromString(PyCursesVersion);
2786     PyDict_SetItemString(d, "version", v);
2787     PyDict_SetItemString(d, "__version__", v);
2788     Py_DECREF(v);
2789 
2790     SetDictInt("ERR", ERR);
2791     SetDictInt("OK", OK);
2792 
2793     /* Here are some attributes you can add to chars to print */
2794 
2795     SetDictInt("A_ATTRIBUTES",      A_ATTRIBUTES);
2796     SetDictInt("A_NORMAL",              A_NORMAL);
2797     SetDictInt("A_STANDOUT",            A_STANDOUT);
2798     SetDictInt("A_UNDERLINE",           A_UNDERLINE);
2799     SetDictInt("A_REVERSE",             A_REVERSE);
2800     SetDictInt("A_BLINK",               A_BLINK);
2801     SetDictInt("A_DIM",                 A_DIM);
2802     SetDictInt("A_BOLD",                A_BOLD);
2803     SetDictInt("A_ALTCHARSET",          A_ALTCHARSET);
2804 #if !defined(__NetBSD__)
2805     SetDictInt("A_INVIS",           A_INVIS);
2806 #endif
2807     SetDictInt("A_PROTECT",         A_PROTECT);
2808     SetDictInt("A_CHARTEXT",        A_CHARTEXT);
2809     SetDictInt("A_COLOR",           A_COLOR);
2810 
2811     /* The following are never available with strict SYSV curses */
2812 #ifdef A_HORIZONTAL
2813     SetDictInt("A_HORIZONTAL",      A_HORIZONTAL);
2814 #endif
2815 #ifdef A_LEFT
2816     SetDictInt("A_LEFT",            A_LEFT);
2817 #endif
2818 #ifdef A_LOW
2819     SetDictInt("A_LOW",             A_LOW);
2820 #endif
2821 #ifdef A_RIGHT
2822     SetDictInt("A_RIGHT",           A_RIGHT);
2823 #endif
2824 #ifdef A_TOP
2825     SetDictInt("A_TOP",             A_TOP);
2826 #endif
2827 #ifdef A_VERTICAL
2828     SetDictInt("A_VERTICAL",        A_VERTICAL);
2829 #endif
2830 
2831     SetDictInt("COLOR_BLACK",       COLOR_BLACK);
2832     SetDictInt("COLOR_RED",         COLOR_RED);
2833     SetDictInt("COLOR_GREEN",       COLOR_GREEN);
2834     SetDictInt("COLOR_YELLOW",      COLOR_YELLOW);
2835     SetDictInt("COLOR_BLUE",        COLOR_BLUE);
2836     SetDictInt("COLOR_MAGENTA",     COLOR_MAGENTA);
2837     SetDictInt("COLOR_CYAN",        COLOR_CYAN);
2838     SetDictInt("COLOR_WHITE",       COLOR_WHITE);
2839 
2840 #ifdef NCURSES_MOUSE_VERSION
2841     /* Mouse-related constants */
2842     SetDictInt("BUTTON1_PRESSED",          BUTTON1_PRESSED);
2843     SetDictInt("BUTTON1_RELEASED",         BUTTON1_RELEASED);
2844     SetDictInt("BUTTON1_CLICKED",          BUTTON1_CLICKED);
2845     SetDictInt("BUTTON1_DOUBLE_CLICKED",   BUTTON1_DOUBLE_CLICKED);
2846     SetDictInt("BUTTON1_TRIPLE_CLICKED",   BUTTON1_TRIPLE_CLICKED);
2847 
2848     SetDictInt("BUTTON2_PRESSED",          BUTTON2_PRESSED);
2849     SetDictInt("BUTTON2_RELEASED",         BUTTON2_RELEASED);
2850     SetDictInt("BUTTON2_CLICKED",          BUTTON2_CLICKED);
2851     SetDictInt("BUTTON2_DOUBLE_CLICKED",   BUTTON2_DOUBLE_CLICKED);
2852     SetDictInt("BUTTON2_TRIPLE_CLICKED",   BUTTON2_TRIPLE_CLICKED);
2853 
2854     SetDictInt("BUTTON3_PRESSED",          BUTTON3_PRESSED);
2855     SetDictInt("BUTTON3_RELEASED",         BUTTON3_RELEASED);
2856     SetDictInt("BUTTON3_CLICKED",          BUTTON3_CLICKED);
2857     SetDictInt("BUTTON3_DOUBLE_CLICKED",   BUTTON3_DOUBLE_CLICKED);
2858     SetDictInt("BUTTON3_TRIPLE_CLICKED",   BUTTON3_TRIPLE_CLICKED);
2859 
2860     SetDictInt("BUTTON4_PRESSED",          BUTTON4_PRESSED);
2861     SetDictInt("BUTTON4_RELEASED",         BUTTON4_RELEASED);
2862     SetDictInt("BUTTON4_CLICKED",          BUTTON4_CLICKED);
2863     SetDictInt("BUTTON4_DOUBLE_CLICKED",   BUTTON4_DOUBLE_CLICKED);
2864     SetDictInt("BUTTON4_TRIPLE_CLICKED",   BUTTON4_TRIPLE_CLICKED);
2865 
2866     SetDictInt("BUTTON_SHIFT",             BUTTON_SHIFT);
2867     SetDictInt("BUTTON_CTRL",              BUTTON_CTRL);
2868     SetDictInt("BUTTON_ALT",               BUTTON_ALT);
2869 
2870     SetDictInt("ALL_MOUSE_EVENTS",         ALL_MOUSE_EVENTS);
2871     SetDictInt("REPORT_MOUSE_POSITION",    REPORT_MOUSE_POSITION);
2872 #endif
2873     /* Now set everything up for KEY_ variables */
2874     {
2875         int key;
2876         char *key_n;
2877         char *key_n2;
2878 #if !defined(__NetBSD__)
2879         for (key=KEY_MIN;key < KEY_MAX; key++) {
2880             key_n = (char *)keyname(key);
2881             if (key_n == NULL || strcmp(key_n,"UNKNOWN KEY")==0)
2882                 continue;
2883             if (strncmp(key_n,"KEY_F(",6)==0) {
2884                 char *p1, *p2;
2885                 key_n2 = malloc(strlen(key_n)+1);
2886                 if (!key_n2) {
2887                     PyErr_NoMemory();
2888                     break;
2889                 }
2890                 p1 = key_n;
2891                 p2 = key_n2;
2892                 while (*p1) {
2893                     if (*p1 != '(' && *p1 != ')') {
2894                         *p2 = *p1;
2895                         p2++;
2896                     }
2897                     p1++;
2898                 }
2899                 *p2 = (char)0;
2900             } else
2901                 key_n2 = key_n;
2902             SetDictInt(key_n2,key);
2903             if (key_n2 != key_n)
2904                 free(key_n2);
2905         }
2906 #endif
2907         SetDictInt("KEY_MIN", KEY_MIN);
2908         SetDictInt("KEY_MAX", KEY_MAX);
2909     }
2910 }
2911