Added section about adding contextual information to log output.
[python.git] / Modules / _cursesmodule.c
bloba805b2d05fb57c4d0e331df46c1ee77dd959aea4
1 /*
2 * This is a curses module for Python.
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.
8 * Version 1.5b1, heavily extended for ncurses by Oliver Andrich:
9 * Copyright 1996,1997 by Oliver Andrich, Koblenz, Germany.
11 * Tidied for Python 1.6, and currently maintained by <amk@amk.ca>.
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:
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.
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.
34 /* CVS: $Id$ */
38 A number of SysV or ncurses functions don't have wrappers yet; if you
39 need a given function, add it and send a patch. See
40 http://www.python.org/dev/patches/ for instructions on how to submit
41 patches to Python.
43 Here's a list of currently unsupported functions:
45 addchnstr addchstr color_set define_key
46 del_curterm delscreen dupwin inchnstr inchstr innstr keyok
47 mcprint mvaddchnstr mvaddchstr mvcur mvinchnstr
48 mvinchstr mvinnstr mmvwaddchnstr mvwaddchstr
49 mvwinchnstr mvwinchstr mvwinnstr newterm
50 restartterm ripoffline scr_dump
51 scr_init scr_restore scr_set scrl set_curterm set_term setterm
52 tgetent tgetflag tgetnum tgetstr tgoto timeout tputs
53 vidattr vidputs waddchnstr waddchstr
54 wcolor_set winchnstr winchstr winnstr wmouse_trafo wscrl
56 Low-priority:
57 slk_attr slk_attr_off slk_attr_on slk_attr_set slk_attroff
58 slk_attron slk_attrset slk_clear slk_color slk_init slk_label
59 slk_noutrefresh slk_refresh slk_restore slk_set slk_touch
61 Menu extension (ncurses and probably SYSV):
62 current_item free_item free_menu item_count item_description
63 item_index item_init item_name item_opts item_opts_off
64 item_opts_on item_term item_userptr item_value item_visible
65 menu_back menu_driver menu_fore menu_format menu_grey
66 menu_init menu_items menu_mark menu_opts menu_opts_off
67 menu_opts_on menu_pad menu_pattern menu_request_by_name
68 menu_request_name menu_spacing menu_sub menu_term menu_userptr
69 menu_win new_item new_menu pos_menu_cursor post_menu
70 scale_menu set_current_item set_item_init set_item_opts
71 set_item_term set_item_userptr set_item_value set_menu_back
72 set_menu_fore set_menu_format set_menu_grey set_menu_init
73 set_menu_items set_menu_mark set_menu_opts set_menu_pad
74 set_menu_pattern set_menu_spacing set_menu_sub set_menu_term
75 set_menu_userptr set_menu_win set_top_row top_row unpost_menu
77 Form extension (ncurses and probably SYSV):
78 current_field data_ahead data_behind dup_field
79 dynamic_fieldinfo field_arg field_back field_buffer
80 field_count field_fore field_index field_info field_init
81 field_just field_opts field_opts_off field_opts_on field_pad
82 field_status field_term field_type field_userptr form_driver
83 form_fields form_init form_opts form_opts_off form_opts_on
84 form_page form_request_by_name form_request_name form_sub
85 form_term form_userptr form_win free_field free_form
86 link_field link_fieldtype move_field new_field new_form
87 new_page pos_form_cursor post_form scale_form
88 set_current_field set_field_back set_field_buffer
89 set_field_fore set_field_init set_field_just set_field_opts
90 set_field_pad set_field_status set_field_term set_field_type
91 set_field_userptr set_fieldtype_arg set_fieldtype_choice
92 set_form_fields set_form_init set_form_opts set_form_page
93 set_form_sub set_form_term set_form_userptr set_form_win
94 set_max_field set_new_page unpost_form
99 /* Release Number */
101 char *PyCursesVersion = "2.2";
103 /* Includes */
105 #include "Python.h"
107 #ifdef __osf__
108 #define STRICT_SYSV_CURSES /* Don't use ncurses extensions */
109 #endif
111 #ifdef __hpux
112 #define STRICT_SYSV_CURSES
113 #endif
115 #define CURSES_MODULE
116 #include "py_curses.h"
118 /* These prototypes are in <term.h>, but including this header
119 #defines many common symbols (such as "lines") which breaks the
120 curses module in other ways. So the code will just specify
121 explicit prototypes here. */
122 extern int setupterm(char *,int,int *);
123 #ifdef __sgi
124 #include <term.h>
125 #endif
127 #if !defined(HAVE_NCURSES_H) && (defined(sgi) || defined(__sun) || defined(SCO5))
128 #define STRICT_SYSV_CURSES /* Don't use ncurses extensions */
129 typedef chtype attr_t; /* No attr_t type is available */
130 #endif
132 #if defined(_AIX)
133 #define STRICT_SYSV_CURSES
134 #endif
136 /* Definition of exception curses.error */
138 static PyObject *PyCursesError;
140 /* Tells whether setupterm() has been called to initialise terminfo. */
141 static int initialised_setupterm = FALSE;
143 /* Tells whether initscr() has been called to initialise curses. */
144 static int initialised = FALSE;
146 /* Tells whether start_color() has been called to initialise color usage. */
147 static int initialisedcolors = FALSE;
149 /* Utility Macros */
150 #define PyCursesSetupTermCalled \
151 if (initialised_setupterm != TRUE) { \
152 PyErr_SetString(PyCursesError, \
153 "must call (at least) setupterm() first"); \
154 return 0; }
156 #define PyCursesInitialised \
157 if (initialised != TRUE) { \
158 PyErr_SetString(PyCursesError, \
159 "must call initscr() first"); \
160 return 0; }
162 #define PyCursesInitialisedColor \
163 if (initialisedcolors != TRUE) { \
164 PyErr_SetString(PyCursesError, \
165 "must call start_color() first"); \
166 return 0; }
168 #ifndef MIN
169 #define MIN(x,y) ((x) < (y) ? (x) : (y))
170 #endif
172 /* Utility Functions */
175 * Check the return code from a curses function and return None
176 * or raise an exception as appropriate. These are exported using the
177 * CObject API.
180 static PyObject *
181 PyCursesCheckERR(int code, char *fname)
183 if (code != ERR) {
184 Py_INCREF(Py_None);
185 return Py_None;
186 } else {
187 if (fname == NULL) {
188 PyErr_SetString(PyCursesError, catchall_ERR);
189 } else {
190 PyErr_Format(PyCursesError, "%s() returned ERR", fname);
192 return NULL;
196 static int
197 PyCurses_ConvertToChtype(PyObject *obj, chtype *ch)
199 if (PyInt_Check(obj)) {
200 *ch = (chtype) PyInt_AsLong(obj);
201 } else if(PyString_Check(obj)
202 && (PyString_Size(obj) == 1)) {
203 *ch = (chtype) *PyString_AsString(obj);
204 } else {
205 return 0;
207 return 1;
210 /* Function versions of the 3 functions for tested whether curses has been
211 initialised or not. */
213 static int func_PyCursesSetupTermCalled(void)
215 PyCursesSetupTermCalled;
216 return 1;
219 static int func_PyCursesInitialised(void)
221 PyCursesInitialised;
222 return 1;
225 static int func_PyCursesInitialisedColor(void)
227 PyCursesInitialisedColor;
228 return 1;
231 /*****************************************************************************
232 The Window Object
233 ******************************************************************************/
235 /* Definition of the window type */
237 PyTypeObject PyCursesWindow_Type;
239 /* Function prototype macros for Window object
241 X - function name
242 TYPE - parameter Type
243 ERGSTR - format string for construction of the return value
244 PARSESTR - format string for argument parsing
247 #define Window_NoArgNoReturnFunction(X) \
248 static PyObject *PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
249 { return PyCursesCheckERR(X(self->win), # X); }
251 #define Window_NoArgTrueFalseFunction(X) \
252 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self) \
254 if (X (self->win) == FALSE) { Py_INCREF(Py_False); return Py_False; } \
255 else { Py_INCREF(Py_True); return Py_True; } }
257 #define Window_NoArgNoReturnVoidFunction(X) \
258 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self) \
260 X(self->win); Py_INCREF(Py_None); return Py_None; }
262 #define Window_NoArg2TupleReturnFunction(X, TYPE, ERGSTR) \
263 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self) \
265 TYPE arg1, arg2; \
266 X(self->win,arg1,arg2); return Py_BuildValue(ERGSTR, arg1, arg2); }
268 #define Window_OneArgNoReturnVoidFunction(X, TYPE, PARSESTR) \
269 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
271 TYPE arg1; \
272 if (!PyArg_ParseTuple(args, PARSESTR, &arg1)) return NULL; \
273 X(self->win,arg1); Py_INCREF(Py_None); return Py_None; }
275 #define Window_OneArgNoReturnFunction(X, TYPE, PARSESTR) \
276 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
278 TYPE arg1; \
279 if (!PyArg_ParseTuple(args,PARSESTR, &arg1)) return NULL; \
280 return PyCursesCheckERR(X(self->win, arg1), # X); }
282 #define Window_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \
283 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
285 TYPE arg1, arg2; \
286 if (!PyArg_ParseTuple(args,PARSESTR, &arg1, &arg2)) return NULL; \
287 return PyCursesCheckERR(X(self->win, arg1, arg2), # X); }
289 /* ------------- WINDOW routines --------------- */
291 Window_NoArgNoReturnFunction(untouchwin)
292 Window_NoArgNoReturnFunction(touchwin)
293 Window_NoArgNoReturnFunction(redrawwin)
294 Window_NoArgNoReturnFunction(winsertln)
295 Window_NoArgNoReturnFunction(werase)
296 Window_NoArgNoReturnFunction(wdeleteln)
298 Window_NoArgTrueFalseFunction(is_wintouched)
300 Window_NoArgNoReturnVoidFunction(wsyncup)
301 Window_NoArgNoReturnVoidFunction(wsyncdown)
302 Window_NoArgNoReturnVoidFunction(wstandend)
303 Window_NoArgNoReturnVoidFunction(wstandout)
304 Window_NoArgNoReturnVoidFunction(wcursyncup)
305 Window_NoArgNoReturnVoidFunction(wclrtoeol)
306 Window_NoArgNoReturnVoidFunction(wclrtobot)
307 Window_NoArgNoReturnVoidFunction(wclear)
309 Window_OneArgNoReturnVoidFunction(idcok, int, "i;True(1) or False(0)")
310 Window_OneArgNoReturnVoidFunction(immedok, int, "i;True(1) or False(0)")
311 Window_OneArgNoReturnVoidFunction(wtimeout, int, "i;delay")
313 Window_NoArg2TupleReturnFunction(getyx, int, "ii")
314 Window_NoArg2TupleReturnFunction(getbegyx, int, "ii")
315 Window_NoArg2TupleReturnFunction(getmaxyx, int, "ii")
316 Window_NoArg2TupleReturnFunction(getparyx, int, "ii")
318 Window_OneArgNoReturnFunction(wattron, attr_t, "l;attr")
319 Window_OneArgNoReturnFunction(wattroff, attr_t, "l;attr")
320 Window_OneArgNoReturnFunction(wattrset, attr_t, "l;attr")
321 Window_OneArgNoReturnFunction(clearok, int, "i;True(1) or False(0)")
322 Window_OneArgNoReturnFunction(idlok, int, "i;True(1) or False(0)")
323 #if defined(__NetBSD__)
324 Window_OneArgNoReturnVoidFunction(keypad, int, "i;True(1) or False(0)")
325 #else
326 Window_OneArgNoReturnFunction(keypad, int, "i;True(1) or False(0)")
327 #endif
328 Window_OneArgNoReturnFunction(leaveok, int, "i;True(1) or False(0)")
329 #if defined(__NetBSD__)
330 Window_OneArgNoReturnVoidFunction(nodelay, int, "i;True(1) or False(0)")
331 #else
332 Window_OneArgNoReturnFunction(nodelay, int, "i;True(1) or False(0)")
333 #endif
334 Window_OneArgNoReturnFunction(notimeout, int, "i;True(1) or False(0)")
335 Window_OneArgNoReturnFunction(scrollok, int, "i;True(1) or False(0)")
336 Window_OneArgNoReturnFunction(winsdelln, int, "i;nlines")
337 Window_OneArgNoReturnFunction(syncok, int, "i;True(1) or False(0)")
339 Window_TwoArgNoReturnFunction(mvwin, int, "ii;y,x")
340 Window_TwoArgNoReturnFunction(mvderwin, int, "ii;y,x")
341 Window_TwoArgNoReturnFunction(wmove, int, "ii;y,x")
342 #ifndef STRICT_SYSV_CURSES
343 Window_TwoArgNoReturnFunction(wresize, int, "ii;lines,columns")
344 #endif
346 /* Allocation and deallocation of Window Objects */
348 static PyObject *
349 PyCursesWindow_New(WINDOW *win)
351 PyCursesWindowObject *wo;
353 wo = PyObject_NEW(PyCursesWindowObject, &PyCursesWindow_Type);
354 if (wo == NULL) return NULL;
355 wo->win = win;
356 return (PyObject *)wo;
359 static void
360 PyCursesWindow_Dealloc(PyCursesWindowObject *wo)
362 if (wo->win != stdscr) delwin(wo->win);
363 PyObject_DEL(wo);
366 /* Addch, Addstr, Addnstr */
368 static PyObject *
369 PyCursesWindow_AddCh(PyCursesWindowObject *self, PyObject *args)
371 int rtn, x, y, use_xy = FALSE;
372 PyObject *temp;
373 chtype ch = 0;
374 attr_t attr = A_NORMAL;
376 switch (PyTuple_Size(args)) {
377 case 1:
378 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
379 return NULL;
380 break;
381 case 2:
382 if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &temp, &attr))
383 return NULL;
384 break;
385 case 3:
386 if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &temp))
387 return NULL;
388 use_xy = TRUE;
389 break;
390 case 4:
391 if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr",
392 &y, &x, &temp, &attr))
393 return NULL;
394 use_xy = TRUE;
395 break;
396 default:
397 PyErr_SetString(PyExc_TypeError, "addch requires 1 to 4 arguments");
398 return NULL;
401 if (!PyCurses_ConvertToChtype(temp, &ch)) {
402 PyErr_SetString(PyExc_TypeError, "argument 1 or 3 must be a ch or an int");
403 return NULL;
406 if (use_xy == TRUE)
407 rtn = mvwaddch(self->win,y,x, ch | attr);
408 else {
409 rtn = waddch(self->win, ch | attr);
411 return PyCursesCheckERR(rtn, "addch");
414 static PyObject *
415 PyCursesWindow_AddStr(PyCursesWindowObject *self, PyObject *args)
417 int rtn;
418 int x, y;
419 char *str;
420 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
421 int use_xy = FALSE, use_attr = FALSE;
423 switch (PyTuple_Size(args)) {
424 case 1:
425 if (!PyArg_ParseTuple(args,"s;str", &str))
426 return NULL;
427 break;
428 case 2:
429 if (!PyArg_ParseTuple(args,"sl;str,attr", &str, &attr))
430 return NULL;
431 use_attr = TRUE;
432 break;
433 case 3:
434 if (!PyArg_ParseTuple(args,"iis;int,int,str", &y, &x, &str))
435 return NULL;
436 use_xy = TRUE;
437 break;
438 case 4:
439 if (!PyArg_ParseTuple(args,"iisl;int,int,str,attr", &y, &x, &str, &attr))
440 return NULL;
441 use_xy = use_attr = TRUE;
442 break;
443 default:
444 PyErr_SetString(PyExc_TypeError, "addstr requires 1 to 4 arguments");
445 return NULL;
448 if (use_attr == TRUE) {
449 attr_old = getattrs(self->win);
450 wattrset(self->win,attr);
452 if (use_xy == TRUE)
453 rtn = mvwaddstr(self->win,y,x,str);
454 else
455 rtn = waddstr(self->win,str);
456 if (use_attr == TRUE)
457 wattrset(self->win,attr_old);
458 return PyCursesCheckERR(rtn, "addstr");
461 static PyObject *
462 PyCursesWindow_AddNStr(PyCursesWindowObject *self, PyObject *args)
464 int rtn, x, y, n;
465 char *str;
466 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
467 int use_xy = FALSE, use_attr = FALSE;
469 switch (PyTuple_Size(args)) {
470 case 2:
471 if (!PyArg_ParseTuple(args,"si;str,n", &str, &n))
472 return NULL;
473 break;
474 case 3:
475 if (!PyArg_ParseTuple(args,"sil;str,n,attr", &str, &n, &attr))
476 return NULL;
477 use_attr = TRUE;
478 break;
479 case 4:
480 if (!PyArg_ParseTuple(args,"iisi;y,x,str,n", &y, &x, &str, &n))
481 return NULL;
482 use_xy = TRUE;
483 break;
484 case 5:
485 if (!PyArg_ParseTuple(args,"iisil;y,x,str,n,attr", &y, &x, &str, &n, &attr))
486 return NULL;
487 use_xy = use_attr = TRUE;
488 break;
489 default:
490 PyErr_SetString(PyExc_TypeError, "addnstr requires 2 to 5 arguments");
491 return NULL;
494 if (use_attr == TRUE) {
495 attr_old = getattrs(self->win);
496 wattrset(self->win,attr);
498 if (use_xy == TRUE)
499 rtn = mvwaddnstr(self->win,y,x,str,n);
500 else
501 rtn = waddnstr(self->win,str,n);
502 if (use_attr == TRUE)
503 wattrset(self->win,attr_old);
504 return PyCursesCheckERR(rtn, "addnstr");
507 static PyObject *
508 PyCursesWindow_Bkgd(PyCursesWindowObject *self, PyObject *args)
510 PyObject *temp;
511 chtype bkgd;
512 attr_t attr = A_NORMAL;
514 switch (PyTuple_Size(args)) {
515 case 1:
516 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
517 return NULL;
518 break;
519 case 2:
520 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &attr))
521 return NULL;
522 break;
523 default:
524 PyErr_SetString(PyExc_TypeError, "bkgd requires 1 or 2 arguments");
525 return NULL;
528 if (!PyCurses_ConvertToChtype(temp, &bkgd)) {
529 PyErr_SetString(PyExc_TypeError, "argument 1 or 3 must be a ch or an int");
530 return NULL;
533 return PyCursesCheckERR(wbkgd(self->win, bkgd | attr), "bkgd");
536 static PyObject *
537 PyCursesWindow_BkgdSet(PyCursesWindowObject *self, PyObject *args)
539 PyObject *temp;
540 chtype bkgd;
541 attr_t attr = A_NORMAL;
543 switch (PyTuple_Size(args)) {
544 case 1:
545 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
546 return NULL;
547 break;
548 case 2:
549 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &attr))
550 return NULL;
551 break;
552 default:
553 PyErr_SetString(PyExc_TypeError, "bkgdset requires 1 or 2 arguments");
554 return NULL;
557 if (!PyCurses_ConvertToChtype(temp, &bkgd)) {
558 PyErr_SetString(PyExc_TypeError, "argument 1 must be a ch or an int");
559 return NULL;
562 wbkgdset(self->win, bkgd | attr);
563 return PyCursesCheckERR(0, "bkgdset");
566 static PyObject *
567 PyCursesWindow_Border(PyCursesWindowObject *self, PyObject *args)
569 PyObject *temp[8];
570 chtype ch[8];
571 int i;
573 /* Clear the array of parameters */
574 for(i=0; i<8; i++) {
575 temp[i] = NULL;
576 ch[i] = 0;
579 if (!PyArg_ParseTuple(args,"|OOOOOOOO;ls,rs,ts,bs,tl,tr,bl,br",
580 &temp[0], &temp[1], &temp[2], &temp[3],
581 &temp[4], &temp[5], &temp[6], &temp[7]))
582 return NULL;
584 for(i=0; i<8; i++) {
585 if (temp[i] != NULL && !PyCurses_ConvertToChtype(temp[i], &ch[i])) {
586 PyErr_Format(PyExc_TypeError,
587 "argument %i must be a ch or an int", i+1);
588 return NULL;
592 wborder(self->win,
593 ch[0], ch[1], ch[2], ch[3],
594 ch[4], ch[5], ch[6], ch[7]);
595 Py_INCREF(Py_None);
596 return Py_None;
599 static PyObject *
600 PyCursesWindow_Box(PyCursesWindowObject *self, PyObject *args)
602 chtype ch1=0,ch2=0;
603 switch(PyTuple_Size(args)){
604 case 0: break;
605 default:
606 if (!PyArg_ParseTuple(args,"ll;vertint,horint", &ch1, &ch2))
607 return NULL;
609 box(self->win,ch1,ch2);
610 Py_INCREF(Py_None);
611 return Py_None;
614 #if defined(HAVE_NCURSES_H) || defined(MVWDELCH_IS_EXPRESSION)
615 #define py_mvwdelch mvwdelch
616 #else
617 int py_mvwdelch(WINDOW *w, int y, int x)
619 mvwdelch(w,y,x);
620 /* On HP/UX, mvwdelch already returns. On other systems,
621 we may well run into this return statement. */
622 return 0;
624 #endif
626 /* chgat, added by Fabian Kreutz <fabian.kreutz at gmx.net> */
628 static PyObject *
629 PyCursesWindow_ChgAt(PyCursesWindowObject *self, PyObject *args)
631 int rtn;
632 int x, y;
633 int num = -1;
634 short color;
635 attr_t attr = A_NORMAL;
636 int use_xy = FALSE;
638 switch (PyTuple_Size(args)) {
639 case 1:
640 if (!PyArg_ParseTuple(args,"l;attr", &attr))
641 return NULL;
642 break;
643 case 2:
644 if (!PyArg_ParseTuple(args,"il;n,attr", &num, &attr))
645 return NULL;
646 break;
647 case 3:
648 if (!PyArg_ParseTuple(args,"iil;int,int,attr", &y, &x, &attr))
649 return NULL;
650 use_xy = TRUE;
651 break;
652 case 4:
653 if (!PyArg_ParseTuple(args,"iiil;int,int,n,attr", &y, &x, &num, &attr))
654 return NULL;
655 use_xy = TRUE;
656 break;
657 default:
658 PyErr_SetString(PyExc_TypeError, "chgat requires 1 to 4 arguments");
659 return NULL;
662 color = (short)((attr >> 8) & 0xff);
663 attr = attr - (color << 8);
665 if (use_xy == TRUE) {
666 rtn = mvwchgat(self->win,y,x,num,attr,color,NULL);
667 touchline(self->win,y,1);
668 } else {
669 getyx(self->win,y,x);
670 rtn = wchgat(self->win,num,attr,color,NULL);
671 touchline(self->win,y,1);
673 return PyCursesCheckERR(rtn, "chgat");
677 static PyObject *
678 PyCursesWindow_DelCh(PyCursesWindowObject *self, PyObject *args)
680 int rtn;
681 int x, y;
683 switch (PyTuple_Size(args)) {
684 case 0:
685 rtn = wdelch(self->win);
686 break;
687 case 2:
688 if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x))
689 return NULL;
690 rtn = py_mvwdelch(self->win,y,x);
691 break;
692 default:
693 PyErr_SetString(PyExc_TypeError, "delch requires 0 or 2 arguments");
694 return NULL;
696 return PyCursesCheckERR(rtn, "[mv]wdelch");
699 static PyObject *
700 PyCursesWindow_DerWin(PyCursesWindowObject *self, PyObject *args)
702 WINDOW *win;
703 int nlines, ncols, begin_y, begin_x;
705 nlines = 0;
706 ncols = 0;
707 switch (PyTuple_Size(args)) {
708 case 2:
709 if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
710 return NULL;
711 break;
712 case 4:
713 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
714 &nlines,&ncols,&begin_y,&begin_x))
715 return NULL;
716 break;
717 default:
718 PyErr_SetString(PyExc_TypeError, "derwin requires 2 or 4 arguments");
719 return NULL;
722 win = derwin(self->win,nlines,ncols,begin_y,begin_x);
724 if (win == NULL) {
725 PyErr_SetString(PyCursesError, catchall_NULL);
726 return NULL;
729 return (PyObject *)PyCursesWindow_New(win);
732 static PyObject *
733 PyCursesWindow_EchoChar(PyCursesWindowObject *self, PyObject *args)
735 PyObject *temp;
736 chtype ch;
737 attr_t attr = A_NORMAL;
739 switch (PyTuple_Size(args)) {
740 case 1:
741 if (!PyArg_ParseTuple(args,"O;ch or int", &temp))
742 return NULL;
743 break;
744 case 2:
745 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &attr))
746 return NULL;
747 break;
748 default:
749 PyErr_SetString(PyExc_TypeError, "echochar requires 1 or 2 arguments");
752 return NULL;
755 if (!PyCurses_ConvertToChtype(temp, &ch)) {
756 PyErr_SetString(PyExc_TypeError, "argument 1 must be a ch or an int");
757 return NULL;
760 #ifdef WINDOW_HAS_FLAGS
761 if (self->win->_flags & _ISPAD)
762 return PyCursesCheckERR(pechochar(self->win, ch | attr),
763 "echochar");
764 else
765 #endif
766 return PyCursesCheckERR(wechochar(self->win, ch | attr),
767 "echochar");
770 #ifdef NCURSES_MOUSE_VERSION
771 static PyObject *
772 PyCursesWindow_Enclose(PyCursesWindowObject *self, PyObject *args)
774 int x, y;
775 if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x))
776 return NULL;
778 return PyInt_FromLong( wenclose(self->win,y,x) );
780 #endif
782 static PyObject *
783 PyCursesWindow_GetBkgd(PyCursesWindowObject *self)
785 return PyInt_FromLong((long) getbkgd(self->win));
788 static PyObject *
789 PyCursesWindow_GetCh(PyCursesWindowObject *self, PyObject *args)
791 int x, y;
792 int rtn;
794 switch (PyTuple_Size(args)) {
795 case 0:
796 Py_BEGIN_ALLOW_THREADS
797 rtn = wgetch(self->win);
798 Py_END_ALLOW_THREADS
799 break;
800 case 2:
801 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
802 return NULL;
803 Py_BEGIN_ALLOW_THREADS
804 rtn = mvwgetch(self->win,y,x);
805 Py_END_ALLOW_THREADS
806 break;
807 default:
808 PyErr_SetString(PyExc_TypeError, "getch requires 0 or 2 arguments");
809 return NULL;
811 return PyInt_FromLong((long)rtn);
814 static PyObject *
815 PyCursesWindow_GetKey(PyCursesWindowObject *self, PyObject *args)
817 int x, y;
818 int rtn;
820 switch (PyTuple_Size(args)) {
821 case 0:
822 Py_BEGIN_ALLOW_THREADS
823 rtn = wgetch(self->win);
824 Py_END_ALLOW_THREADS
825 break;
826 case 2:
827 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
828 return NULL;
829 Py_BEGIN_ALLOW_THREADS
830 rtn = mvwgetch(self->win,y,x);
831 Py_END_ALLOW_THREADS
832 break;
833 default:
834 PyErr_SetString(PyExc_TypeError, "getkey requires 0 or 2 arguments");
835 return NULL;
837 if (rtn == ERR) {
838 /* getch() returns ERR in nodelay mode */
839 PyErr_SetString(PyCursesError, "no input");
840 return NULL;
841 } else if (rtn<=255)
842 return Py_BuildValue("c", rtn);
843 else
844 #if defined(__NetBSD__)
845 return PyString_FromString(unctrl(rtn));
846 #else
847 return PyString_FromString((char *)keyname(rtn));
848 #endif
851 static PyObject *
852 PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
854 int x, y, n;
855 char rtn[1024]; /* This should be big enough.. I hope */
856 int rtn2;
858 switch (PyTuple_Size(args)) {
859 case 0:
860 Py_BEGIN_ALLOW_THREADS
861 rtn2 = wgetnstr(self->win,rtn, 1023);
862 Py_END_ALLOW_THREADS
863 break;
864 case 1:
865 if (!PyArg_ParseTuple(args,"i;n", &n))
866 return NULL;
867 Py_BEGIN_ALLOW_THREADS
868 rtn2 = wgetnstr(self->win,rtn,MIN(n, 1023));
869 Py_END_ALLOW_THREADS
870 break;
871 case 2:
872 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
873 return NULL;
874 Py_BEGIN_ALLOW_THREADS
875 #ifdef STRICT_SYSV_CURSES
876 rtn2 = wmove(self->win,y,x)==ERR ? ERR : wgetnstr(self->win, rtn, 1023);
877 #else
878 rtn2 = mvwgetnstr(self->win,y,x,rtn, 1023);
879 #endif
880 Py_END_ALLOW_THREADS
881 break;
882 case 3:
883 if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
884 return NULL;
885 #ifdef STRICT_SYSV_CURSES
886 Py_BEGIN_ALLOW_THREADS
887 rtn2 = wmove(self->win,y,x)==ERR ? ERR :
888 wgetnstr(self->win, rtn, MIN(n, 1023));
889 Py_END_ALLOW_THREADS
890 #else
891 Py_BEGIN_ALLOW_THREADS
892 rtn2 = mvwgetnstr(self->win, y, x, rtn, MIN(n, 1023));
893 Py_END_ALLOW_THREADS
894 #endif
895 break;
896 default:
897 PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 3 arguments");
898 return NULL;
900 if (rtn2 == ERR)
901 rtn[0] = 0;
902 return PyString_FromString(rtn);
905 static PyObject *
906 PyCursesWindow_Hline(PyCursesWindowObject *self, PyObject *args)
908 PyObject *temp;
909 chtype ch;
910 int n, x, y, code = OK;
911 attr_t attr = A_NORMAL;
913 switch (PyTuple_Size(args)) {
914 case 2:
915 if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
916 return NULL;
917 break;
918 case 3:
919 if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &attr))
920 return NULL;
921 break;
922 case 4:
923 if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
924 return NULL;
925 code = wmove(self->win, y, x);
926 break;
927 case 5:
928 if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
929 &y, &x, &temp, &n, &attr))
930 return NULL;
931 code = wmove(self->win, y, x);
932 break;
933 default:
934 PyErr_SetString(PyExc_TypeError, "hline requires 2 to 5 arguments");
935 return NULL;
938 if (code != ERR) {
939 if (!PyCurses_ConvertToChtype(temp, &ch)) {
940 PyErr_SetString(PyExc_TypeError,
941 "argument 1 or 3 must be a ch or an int");
942 return NULL;
944 return PyCursesCheckERR(whline(self->win, ch | attr, n), "hline");
945 } else
946 return PyCursesCheckERR(code, "wmove");
949 static PyObject *
950 PyCursesWindow_InsCh(PyCursesWindowObject *self, PyObject *args)
952 int rtn, x, y, use_xy = FALSE;
953 PyObject *temp;
954 chtype ch = 0;
955 attr_t attr = A_NORMAL;
957 switch (PyTuple_Size(args)) {
958 case 1:
959 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
960 return NULL;
961 break;
962 case 2:
963 if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &temp, &attr))
964 return NULL;
965 break;
966 case 3:
967 if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &temp))
968 return NULL;
969 use_xy = TRUE;
970 break;
971 case 4:
972 if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr", &y, &x, &temp, &attr))
973 return NULL;
974 use_xy = TRUE;
975 break;
976 default:
977 PyErr_SetString(PyExc_TypeError, "insch requires 1 or 4 arguments");
978 return NULL;
981 if (!PyCurses_ConvertToChtype(temp, &ch)) {
982 PyErr_SetString(PyExc_TypeError,
983 "argument 1 or 3 must be a ch or an int");
984 return NULL;
987 if (use_xy == TRUE)
988 rtn = mvwinsch(self->win,y,x, ch | attr);
989 else {
990 rtn = winsch(self->win, ch | attr);
992 return PyCursesCheckERR(rtn, "insch");
995 static PyObject *
996 PyCursesWindow_InCh(PyCursesWindowObject *self, PyObject *args)
998 int x, y, rtn;
1000 switch (PyTuple_Size(args)) {
1001 case 0:
1002 rtn = winch(self->win);
1003 break;
1004 case 2:
1005 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1006 return NULL;
1007 rtn = mvwinch(self->win,y,x);
1008 break;
1009 default:
1010 PyErr_SetString(PyExc_TypeError, "inch requires 0 or 2 arguments");
1011 return NULL;
1013 return PyInt_FromLong((long) rtn);
1016 static PyObject *
1017 PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
1019 int x, y, n;
1020 char rtn[1024]; /* This should be big enough.. I hope */
1021 int rtn2;
1023 switch (PyTuple_Size(args)) {
1024 case 0:
1025 rtn2 = winnstr(self->win,rtn, 1023);
1026 break;
1027 case 1:
1028 if (!PyArg_ParseTuple(args,"i;n", &n))
1029 return NULL;
1030 rtn2 = winnstr(self->win,rtn,MIN(n,1023));
1031 break;
1032 case 2:
1033 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1034 return NULL;
1035 rtn2 = mvwinnstr(self->win,y,x,rtn,1023);
1036 break;
1037 case 3:
1038 if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
1039 return NULL;
1040 rtn2 = mvwinnstr(self->win, y, x, rtn, MIN(n,1023));
1041 break;
1042 default:
1043 PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments");
1044 return NULL;
1046 if (rtn2 == ERR)
1047 rtn[0] = 0;
1048 return PyString_FromString(rtn);
1051 static PyObject *
1052 PyCursesWindow_InsStr(PyCursesWindowObject *self, PyObject *args)
1054 int rtn;
1055 int x, y;
1056 char *str;
1057 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
1058 int use_xy = FALSE, use_attr = FALSE;
1060 switch (PyTuple_Size(args)) {
1061 case 1:
1062 if (!PyArg_ParseTuple(args,"s;str", &str))
1063 return NULL;
1064 break;
1065 case 2:
1066 if (!PyArg_ParseTuple(args,"sl;str,attr", &str, &attr))
1067 return NULL;
1068 use_attr = TRUE;
1069 break;
1070 case 3:
1071 if (!PyArg_ParseTuple(args,"iis;y,x,str", &y, &x, &str))
1072 return NULL;
1073 use_xy = TRUE;
1074 break;
1075 case 4:
1076 if (!PyArg_ParseTuple(args,"iisl;y,x,str,attr", &y, &x, &str, &attr))
1077 return NULL;
1078 use_xy = use_attr = TRUE;
1079 break;
1080 default:
1081 PyErr_SetString(PyExc_TypeError, "insstr requires 1 to 4 arguments");
1082 return NULL;
1085 if (use_attr == TRUE) {
1086 attr_old = getattrs(self->win);
1087 wattrset(self->win,attr);
1089 if (use_xy == TRUE)
1090 rtn = mvwinsstr(self->win,y,x,str);
1091 else
1092 rtn = winsstr(self->win,str);
1093 if (use_attr == TRUE)
1094 wattrset(self->win,attr_old);
1095 return PyCursesCheckERR(rtn, "insstr");
1098 static PyObject *
1099 PyCursesWindow_InsNStr(PyCursesWindowObject *self, PyObject *args)
1101 int rtn, x, y, n;
1102 char *str;
1103 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
1104 int use_xy = FALSE, use_attr = FALSE;
1106 switch (PyTuple_Size(args)) {
1107 case 2:
1108 if (!PyArg_ParseTuple(args,"si;str,n", &str, &n))
1109 return NULL;
1110 break;
1111 case 3:
1112 if (!PyArg_ParseTuple(args,"sil;str,n,attr", &str, &n, &attr))
1113 return NULL;
1114 use_attr = TRUE;
1115 break;
1116 case 4:
1117 if (!PyArg_ParseTuple(args,"iisi;y,x,str,n", &y, &x, &str, &n))
1118 return NULL;
1119 use_xy = TRUE;
1120 break;
1121 case 5:
1122 if (!PyArg_ParseTuple(args,"iisil;y,x,str,n,attr", &y, &x, &str, &n, &attr))
1123 return NULL;
1124 use_xy = use_attr = TRUE;
1125 break;
1126 default:
1127 PyErr_SetString(PyExc_TypeError, "insnstr requires 2 to 5 arguments");
1128 return NULL;
1131 if (use_attr == TRUE) {
1132 attr_old = getattrs(self->win);
1133 wattrset(self->win,attr);
1135 if (use_xy == TRUE)
1136 rtn = mvwinsnstr(self->win,y,x,str,n);
1137 else
1138 rtn = winsnstr(self->win,str,n);
1139 if (use_attr == TRUE)
1140 wattrset(self->win,attr_old);
1141 return PyCursesCheckERR(rtn, "insnstr");
1144 static PyObject *
1145 PyCursesWindow_Is_LineTouched(PyCursesWindowObject *self, PyObject *args)
1147 int line, erg;
1148 if (!PyArg_ParseTuple(args,"i;line", &line))
1149 return NULL;
1150 erg = is_linetouched(self->win, line);
1151 if (erg == ERR) {
1152 PyErr_SetString(PyExc_TypeError,
1153 "is_linetouched: line number outside of boundaries");
1154 return NULL;
1155 } else
1156 if (erg == FALSE) {
1157 Py_INCREF(Py_False);
1158 return Py_False;
1159 } else {
1160 Py_INCREF(Py_True);
1161 return Py_True;
1165 static PyObject *
1166 PyCursesWindow_NoOutRefresh(PyCursesWindowObject *self, PyObject *args)
1168 int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
1169 int rtn;
1171 #ifndef WINDOW_HAS_FLAGS
1172 if (0) {
1173 #else
1174 if (self->win->_flags & _ISPAD) {
1175 #endif
1176 switch(PyTuple_Size(args)) {
1177 case 6:
1178 if (!PyArg_ParseTuple(args,
1179 "iiiiii;" \
1180 "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
1181 &pminrow, &pmincol, &sminrow,
1182 &smincol, &smaxrow, &smaxcol))
1183 return NULL;
1184 Py_BEGIN_ALLOW_THREADS
1185 rtn = pnoutrefresh(self->win,
1186 pminrow, pmincol, sminrow,
1187 smincol, smaxrow, smaxcol);
1188 Py_END_ALLOW_THREADS
1189 return PyCursesCheckERR(rtn, "pnoutrefresh");
1190 default:
1191 PyErr_SetString(PyCursesError,
1192 "noutrefresh() called for a pad "
1193 "requires 6 arguments");
1194 return NULL;
1196 } else {
1197 if (!PyArg_ParseTuple(args, ":noutrefresh"))
1198 return NULL;
1200 Py_BEGIN_ALLOW_THREADS
1201 rtn = wnoutrefresh(self->win);
1202 Py_END_ALLOW_THREADS
1203 return PyCursesCheckERR(rtn, "wnoutrefresh");
1207 static PyObject *
1208 PyCursesWindow_Overlay(PyCursesWindowObject *self, PyObject *args)
1210 PyCursesWindowObject *temp;
1211 int use_copywin = FALSE;
1212 int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
1213 int rtn;
1215 switch (PyTuple_Size(args)) {
1216 case 1:
1217 if (!PyArg_ParseTuple(args, "O!;window object",
1218 &PyCursesWindow_Type, &temp))
1219 return NULL;
1220 break;
1221 case 7:
1222 if (!PyArg_ParseTuple(args, "O!iiiiii;window object, int, int, int, int, int, int",
1223 &PyCursesWindow_Type, &temp, &sminrow, &smincol,
1224 &dminrow, &dmincol, &dmaxrow, &dmaxcol))
1225 return NULL;
1226 use_copywin = TRUE;
1227 break;
1228 default:
1229 PyErr_SetString(PyExc_TypeError,
1230 "overlay requires one or seven arguments");
1231 return NULL;
1234 if (use_copywin == TRUE) {
1235 rtn = copywin(self->win, temp->win, sminrow, smincol,
1236 dminrow, dmincol, dmaxrow, dmaxcol, TRUE);
1237 return PyCursesCheckERR(rtn, "copywin");
1239 else {
1240 rtn = overlay(self->win, temp->win);
1241 return PyCursesCheckERR(rtn, "overlay");
1245 static PyObject *
1246 PyCursesWindow_Overwrite(PyCursesWindowObject *self, PyObject *args)
1248 PyCursesWindowObject *temp;
1249 int use_copywin = FALSE;
1250 int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
1251 int rtn;
1253 switch (PyTuple_Size(args)) {
1254 case 1:
1255 if (!PyArg_ParseTuple(args, "O!;window object",
1256 &PyCursesWindow_Type, &temp))
1257 return NULL;
1258 break;
1259 case 7:
1260 if (!PyArg_ParseTuple(args, "O!iiiiii;window object, int, int, int, int, int, int",
1261 &PyCursesWindow_Type, &temp, &sminrow, &smincol,
1262 &dminrow, &dmincol, &dmaxrow, &dmaxcol))
1263 return NULL;
1264 use_copywin = TRUE;
1265 break;
1266 default:
1267 PyErr_SetString(PyExc_TypeError,
1268 "overwrite requires one or seven arguments");
1269 return NULL;
1272 if (use_copywin == TRUE) {
1273 rtn = copywin(self->win, temp->win, sminrow, smincol,
1274 dminrow, dmincol, dmaxrow, dmaxcol, FALSE);
1275 return PyCursesCheckERR(rtn, "copywin");
1277 else {
1278 rtn = overwrite(self->win, temp->win);
1279 return PyCursesCheckERR(rtn, "overwrite");
1283 static PyObject *
1284 PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *args)
1286 PyObject *temp;
1288 if (!PyArg_ParseTuple(args, "O;fileobj", &temp))
1289 return NULL;
1290 if (!PyFile_Check(temp)) {
1291 PyErr_SetString(PyExc_TypeError, "argument must be a file object");
1292 return NULL;
1294 return PyCursesCheckERR(putwin(self->win, PyFile_AsFile(temp)),
1295 "putwin");
1298 static PyObject *
1299 PyCursesWindow_RedrawLine(PyCursesWindowObject *self, PyObject *args)
1301 int beg, num;
1302 if (!PyArg_ParseTuple(args, "ii;beg,num", &beg, &num))
1303 return NULL;
1304 return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln");
1307 static PyObject *
1308 PyCursesWindow_Refresh(PyCursesWindowObject *self, PyObject *args)
1310 int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
1311 int rtn;
1313 #ifndef WINDOW_HAS_FLAGS
1314 if (0) {
1315 #else
1316 if (self->win->_flags & _ISPAD) {
1317 #endif
1318 switch(PyTuple_Size(args)) {
1319 case 6:
1320 if (!PyArg_ParseTuple(args,
1321 "iiiiii;" \
1322 "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
1323 &pminrow, &pmincol, &sminrow,
1324 &smincol, &smaxrow, &smaxcol))
1325 return NULL;
1327 Py_BEGIN_ALLOW_THREADS
1328 rtn = prefresh(self->win,
1329 pminrow, pmincol, sminrow,
1330 smincol, smaxrow, smaxcol);
1331 Py_END_ALLOW_THREADS
1332 return PyCursesCheckERR(rtn, "prefresh");
1333 default:
1334 PyErr_SetString(PyCursesError,
1335 "refresh() for a pad requires 6 arguments");
1336 return NULL;
1338 } else {
1339 if (!PyArg_ParseTuple(args, ":refresh"))
1340 return NULL;
1341 Py_BEGIN_ALLOW_THREADS
1342 rtn = wrefresh(self->win);
1343 Py_END_ALLOW_THREADS
1344 return PyCursesCheckERR(rtn, "prefresh");
1348 static PyObject *
1349 PyCursesWindow_SetScrollRegion(PyCursesWindowObject *self, PyObject *args)
1351 int x, y;
1352 if (!PyArg_ParseTuple(args,"ii;top, bottom",&y,&x))
1353 return NULL;
1354 return PyCursesCheckERR(wsetscrreg(self->win,y,x), "wsetscrreg");
1357 static PyObject *
1358 PyCursesWindow_SubWin(PyCursesWindowObject *self, PyObject *args)
1360 WINDOW *win;
1361 int nlines, ncols, begin_y, begin_x;
1363 nlines = 0;
1364 ncols = 0;
1365 switch (PyTuple_Size(args)) {
1366 case 2:
1367 if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
1368 return NULL;
1369 break;
1370 case 4:
1371 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
1372 &nlines,&ncols,&begin_y,&begin_x))
1373 return NULL;
1374 break;
1375 default:
1376 PyErr_SetString(PyExc_TypeError, "subwin requires 2 or 4 arguments");
1377 return NULL;
1380 /* printf("Subwin: %i %i %i %i \n", nlines, ncols, begin_y, begin_x); */
1381 #ifdef WINDOW_HAS_FLAGS
1382 if (self->win->_flags & _ISPAD)
1383 win = subpad(self->win, nlines, ncols, begin_y, begin_x);
1384 else
1385 #endif
1386 win = subwin(self->win, nlines, ncols, begin_y, begin_x);
1388 if (win == NULL) {
1389 PyErr_SetString(PyCursesError, catchall_NULL);
1390 return NULL;
1393 return (PyObject *)PyCursesWindow_New(win);
1396 static PyObject *
1397 PyCursesWindow_Scroll(PyCursesWindowObject *self, PyObject *args)
1399 int nlines;
1400 switch(PyTuple_Size(args)) {
1401 case 0:
1402 return PyCursesCheckERR(scroll(self->win), "scroll");
1403 case 1:
1404 if (!PyArg_ParseTuple(args, "i;nlines", &nlines))
1405 return NULL;
1406 return PyCursesCheckERR(wscrl(self->win, nlines), "scroll");
1407 default:
1408 PyErr_SetString(PyExc_TypeError, "scroll requires 0 or 1 arguments");
1409 return NULL;
1413 static PyObject *
1414 PyCursesWindow_TouchLine(PyCursesWindowObject *self, PyObject *args)
1416 int st, cnt, val;
1417 switch (PyTuple_Size(args)) {
1418 case 2:
1419 if (!PyArg_ParseTuple(args,"ii;start,count",&st,&cnt))
1420 return NULL;
1421 return PyCursesCheckERR(touchline(self->win,st,cnt), "touchline");
1422 case 3:
1423 if (!PyArg_ParseTuple(args, "iii;start,count,val", &st, &cnt, &val))
1424 return NULL;
1425 return PyCursesCheckERR(wtouchln(self->win, st, cnt, val), "touchline");
1426 default:
1427 PyErr_SetString(PyExc_TypeError, "touchline requires 2 or 3 arguments");
1428 return NULL;
1432 static PyObject *
1433 PyCursesWindow_Vline(PyCursesWindowObject *self, PyObject *args)
1435 PyObject *temp;
1436 chtype ch;
1437 int n, x, y, code = OK;
1438 attr_t attr = A_NORMAL;
1440 switch (PyTuple_Size(args)) {
1441 case 2:
1442 if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
1443 return NULL;
1444 break;
1445 case 3:
1446 if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &attr))
1447 return NULL;
1448 break;
1449 case 4:
1450 if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
1451 return NULL;
1452 code = wmove(self->win, y, x);
1453 break;
1454 case 5:
1455 if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
1456 &y, &x, &temp, &n, &attr))
1457 return NULL;
1458 code = wmove(self->win, y, x);
1459 break;
1460 default:
1461 PyErr_SetString(PyExc_TypeError, "vline requires 2 to 5 arguments");
1462 return NULL;
1465 if (code != ERR) {
1466 if (!PyCurses_ConvertToChtype(temp, &ch)) {
1467 PyErr_SetString(PyExc_TypeError,
1468 "argument 1 or 3 must be a ch or an int");
1469 return NULL;
1471 return PyCursesCheckERR(wvline(self->win, ch | attr, n), "vline");
1472 } else
1473 return PyCursesCheckERR(code, "wmove");
1476 static PyMethodDef PyCursesWindow_Methods[] = {
1477 {"addch", (PyCFunction)PyCursesWindow_AddCh, METH_VARARGS},
1478 {"addnstr", (PyCFunction)PyCursesWindow_AddNStr, METH_VARARGS},
1479 {"addstr", (PyCFunction)PyCursesWindow_AddStr, METH_VARARGS},
1480 {"attroff", (PyCFunction)PyCursesWindow_wattroff, METH_VARARGS},
1481 {"attron", (PyCFunction)PyCursesWindow_wattron, METH_VARARGS},
1482 {"attrset", (PyCFunction)PyCursesWindow_wattrset, METH_VARARGS},
1483 {"bkgd", (PyCFunction)PyCursesWindow_Bkgd, METH_VARARGS},
1484 {"chgat", (PyCFunction)PyCursesWindow_ChgAt, METH_VARARGS},
1485 {"bkgdset", (PyCFunction)PyCursesWindow_BkgdSet, METH_VARARGS},
1486 {"border", (PyCFunction)PyCursesWindow_Border, METH_VARARGS},
1487 {"box", (PyCFunction)PyCursesWindow_Box, METH_VARARGS},
1488 {"clear", (PyCFunction)PyCursesWindow_wclear, METH_NOARGS},
1489 {"clearok", (PyCFunction)PyCursesWindow_clearok, METH_VARARGS},
1490 {"clrtobot", (PyCFunction)PyCursesWindow_wclrtobot, METH_NOARGS},
1491 {"clrtoeol", (PyCFunction)PyCursesWindow_wclrtoeol, METH_NOARGS},
1492 {"cursyncup", (PyCFunction)PyCursesWindow_wcursyncup, METH_NOARGS},
1493 {"delch", (PyCFunction)PyCursesWindow_DelCh, METH_VARARGS},
1494 {"deleteln", (PyCFunction)PyCursesWindow_wdeleteln, METH_NOARGS},
1495 {"derwin", (PyCFunction)PyCursesWindow_DerWin, METH_VARARGS},
1496 {"echochar", (PyCFunction)PyCursesWindow_EchoChar, METH_VARARGS},
1497 #ifdef NCURSES_MOUSE_VERSION
1498 {"enclose", (PyCFunction)PyCursesWindow_Enclose, METH_VARARGS},
1499 #endif
1500 {"erase", (PyCFunction)PyCursesWindow_werase, METH_NOARGS},
1501 {"getbegyx", (PyCFunction)PyCursesWindow_getbegyx, METH_NOARGS},
1502 {"getbkgd", (PyCFunction)PyCursesWindow_GetBkgd, METH_NOARGS},
1503 {"getch", (PyCFunction)PyCursesWindow_GetCh, METH_VARARGS},
1504 {"getkey", (PyCFunction)PyCursesWindow_GetKey, METH_VARARGS},
1505 {"getmaxyx", (PyCFunction)PyCursesWindow_getmaxyx, METH_NOARGS},
1506 {"getparyx", (PyCFunction)PyCursesWindow_getparyx, METH_NOARGS},
1507 {"getstr", (PyCFunction)PyCursesWindow_GetStr, METH_VARARGS},
1508 {"getyx", (PyCFunction)PyCursesWindow_getyx, METH_NOARGS},
1509 {"hline", (PyCFunction)PyCursesWindow_Hline, METH_VARARGS},
1510 {"idcok", (PyCFunction)PyCursesWindow_idcok, METH_VARARGS},
1511 {"idlok", (PyCFunction)PyCursesWindow_idlok, METH_VARARGS},
1512 {"immedok", (PyCFunction)PyCursesWindow_immedok, METH_VARARGS},
1513 {"inch", (PyCFunction)PyCursesWindow_InCh, METH_VARARGS},
1514 {"insch", (PyCFunction)PyCursesWindow_InsCh, METH_VARARGS},
1515 {"insdelln", (PyCFunction)PyCursesWindow_winsdelln, METH_VARARGS},
1516 {"insertln", (PyCFunction)PyCursesWindow_winsertln, METH_NOARGS},
1517 {"insnstr", (PyCFunction)PyCursesWindow_InsNStr, METH_VARARGS},
1518 {"insstr", (PyCFunction)PyCursesWindow_InsStr, METH_VARARGS},
1519 {"instr", (PyCFunction)PyCursesWindow_InStr, METH_VARARGS},
1520 {"is_linetouched", (PyCFunction)PyCursesWindow_Is_LineTouched, METH_VARARGS},
1521 {"is_wintouched", (PyCFunction)PyCursesWindow_is_wintouched, METH_NOARGS},
1522 {"keypad", (PyCFunction)PyCursesWindow_keypad, METH_VARARGS},
1523 {"leaveok", (PyCFunction)PyCursesWindow_leaveok, METH_VARARGS},
1524 {"move", (PyCFunction)PyCursesWindow_wmove, METH_VARARGS},
1525 {"mvderwin", (PyCFunction)PyCursesWindow_mvderwin, METH_VARARGS},
1526 {"mvwin", (PyCFunction)PyCursesWindow_mvwin, METH_VARARGS},
1527 {"nodelay", (PyCFunction)PyCursesWindow_nodelay, METH_VARARGS},
1528 {"notimeout", (PyCFunction)PyCursesWindow_notimeout, METH_VARARGS},
1529 {"noutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
1530 /* Backward compatibility alias -- remove in Python 2.3 */
1531 {"nooutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
1532 {"overlay", (PyCFunction)PyCursesWindow_Overlay, METH_VARARGS},
1533 {"overwrite", (PyCFunction)PyCursesWindow_Overwrite,
1534 METH_VARARGS},
1535 {"putwin", (PyCFunction)PyCursesWindow_PutWin, METH_VARARGS},
1536 {"redrawln", (PyCFunction)PyCursesWindow_RedrawLine, METH_VARARGS},
1537 {"redrawwin", (PyCFunction)PyCursesWindow_redrawwin, METH_NOARGS},
1538 {"refresh", (PyCFunction)PyCursesWindow_Refresh, METH_VARARGS},
1539 #ifndef STRICT_SYSV_CURSES
1540 {"resize", (PyCFunction)PyCursesWindow_wresize, METH_VARARGS},
1541 #endif
1542 {"scroll", (PyCFunction)PyCursesWindow_Scroll, METH_VARARGS},
1543 {"scrollok", (PyCFunction)PyCursesWindow_scrollok, METH_VARARGS},
1544 {"setscrreg", (PyCFunction)PyCursesWindow_SetScrollRegion, METH_VARARGS},
1545 {"standend", (PyCFunction)PyCursesWindow_wstandend, METH_NOARGS},
1546 {"standout", (PyCFunction)PyCursesWindow_wstandout, METH_NOARGS},
1547 {"subpad", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
1548 {"subwin", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
1549 {"syncdown", (PyCFunction)PyCursesWindow_wsyncdown, METH_NOARGS},
1550 {"syncok", (PyCFunction)PyCursesWindow_syncok, METH_VARARGS},
1551 {"syncup", (PyCFunction)PyCursesWindow_wsyncup, METH_NOARGS},
1552 {"timeout", (PyCFunction)PyCursesWindow_wtimeout, METH_VARARGS},
1553 {"touchline", (PyCFunction)PyCursesWindow_TouchLine, METH_VARARGS},
1554 {"touchwin", (PyCFunction)PyCursesWindow_touchwin, METH_NOARGS},
1555 {"untouchwin", (PyCFunction)PyCursesWindow_untouchwin, METH_NOARGS},
1556 {"vline", (PyCFunction)PyCursesWindow_Vline, METH_VARARGS},
1557 {NULL, NULL} /* sentinel */
1560 static PyObject *
1561 PyCursesWindow_GetAttr(PyCursesWindowObject *self, char *name)
1563 return Py_FindMethod(PyCursesWindow_Methods, (PyObject *)self, name);
1566 /* -------------------------------------------------------*/
1568 PyTypeObject PyCursesWindow_Type = {
1569 PyVarObject_HEAD_INIT(NULL, 0)
1570 "_curses.curses window", /*tp_name*/
1571 sizeof(PyCursesWindowObject), /*tp_basicsize*/
1572 0, /*tp_itemsize*/
1573 /* methods */
1574 (destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/
1575 0, /*tp_print*/
1576 (getattrfunc)PyCursesWindow_GetAttr, /*tp_getattr*/
1577 (setattrfunc)0, /*tp_setattr*/
1578 0, /*tp_compare*/
1579 0, /*tp_repr*/
1580 0, /*tp_as_number*/
1581 0, /*tp_as_sequence*/
1582 0, /*tp_as_mapping*/
1583 0, /*tp_hash*/
1586 /*********************************************************************
1587 Global Functions
1588 **********************************************************************/
1590 NoArgNoReturnFunction(beep)
1591 NoArgNoReturnFunction(def_prog_mode)
1592 NoArgNoReturnFunction(def_shell_mode)
1593 NoArgNoReturnFunction(doupdate)
1594 NoArgNoReturnFunction(endwin)
1595 NoArgNoReturnFunction(flash)
1596 NoArgNoReturnFunction(nocbreak)
1597 NoArgNoReturnFunction(noecho)
1598 NoArgNoReturnFunction(nonl)
1599 NoArgNoReturnFunction(noraw)
1600 NoArgNoReturnFunction(reset_prog_mode)
1601 NoArgNoReturnFunction(reset_shell_mode)
1602 NoArgNoReturnFunction(resetty)
1603 NoArgNoReturnFunction(savetty)
1605 NoArgOrFlagNoReturnFunction(cbreak)
1606 NoArgOrFlagNoReturnFunction(echo)
1607 NoArgOrFlagNoReturnFunction(nl)
1608 NoArgOrFlagNoReturnFunction(raw)
1610 NoArgReturnIntFunction(baudrate)
1611 NoArgReturnIntFunction(termattrs)
1613 NoArgReturnStringFunction(termname)
1614 NoArgReturnStringFunction(longname)
1616 NoArgTrueFalseFunction(can_change_color)
1617 NoArgTrueFalseFunction(has_colors)
1618 NoArgTrueFalseFunction(has_ic)
1619 NoArgTrueFalseFunction(has_il)
1620 NoArgTrueFalseFunction(isendwin)
1621 NoArgNoReturnVoidFunction(filter)
1622 NoArgNoReturnVoidFunction(flushinp)
1623 NoArgNoReturnVoidFunction(noqiflush)
1625 static PyObject *
1626 PyCurses_Color_Content(PyObject *self, PyObject *args)
1628 short color,r,g,b;
1630 PyCursesInitialised
1631 PyCursesInitialisedColor
1633 if (!PyArg_ParseTuple(args, "h:color_content", &color)) return NULL;
1635 if (color_content(color, &r, &g, &b) != ERR)
1636 return Py_BuildValue("(iii)", r, g, b);
1637 else {
1638 PyErr_SetString(PyCursesError,
1639 "Argument 1 was out of range. Check value of COLORS.");
1640 return NULL;
1644 static PyObject *
1645 PyCurses_color_pair(PyObject *self, PyObject *args)
1647 int n;
1649 PyCursesInitialised
1650 PyCursesInitialisedColor
1652 if (!PyArg_ParseTuple(args, "i:color_pair", &n)) return NULL;
1653 return PyInt_FromLong((long) (n << 8));
1656 static PyObject *
1657 PyCurses_Curs_Set(PyObject *self, PyObject *args)
1659 int vis,erg;
1661 PyCursesInitialised
1663 if (!PyArg_ParseTuple(args, "i:curs_set", &vis)) return NULL;
1665 erg = curs_set(vis);
1666 if (erg == ERR) return PyCursesCheckERR(erg, "curs_set");
1668 return PyInt_FromLong((long) erg);
1671 static PyObject *
1672 PyCurses_Delay_Output(PyObject *self, PyObject *args)
1674 int ms;
1676 PyCursesInitialised
1678 if (!PyArg_ParseTuple(args, "i:delay_output", &ms)) return NULL;
1680 return PyCursesCheckERR(delay_output(ms), "delay_output");
1683 static PyObject *
1684 PyCurses_EraseChar(PyObject *self)
1686 char ch;
1688 PyCursesInitialised
1690 ch = erasechar();
1692 return PyString_FromStringAndSize(&ch, 1);
1695 static PyObject *
1696 PyCurses_getsyx(PyObject *self)
1698 int x,y;
1700 PyCursesInitialised
1702 getsyx(y, x);
1704 return Py_BuildValue("(ii)", y, x);
1707 #ifdef NCURSES_MOUSE_VERSION
1708 static PyObject *
1709 PyCurses_GetMouse(PyObject *self)
1711 int rtn;
1712 MEVENT event;
1714 PyCursesInitialised
1716 rtn = getmouse( &event );
1717 if (rtn == ERR) {
1718 PyErr_SetString(PyCursesError, "getmouse() returned ERR");
1719 return NULL;
1721 return Py_BuildValue("(hiiil)",
1722 (short)event.id,
1723 event.x, event.y, event.z,
1724 (long) event.bstate);
1727 static PyObject *
1728 PyCurses_UngetMouse(PyObject *self, PyObject *args)
1730 MEVENT event;
1732 PyCursesInitialised
1733 if (!PyArg_ParseTuple(args, "hiiil",
1734 &event.id,
1735 &event.x, &event.y, &event.z,
1736 (int *) &event.bstate))
1737 return NULL;
1739 return PyCursesCheckERR(ungetmouse(&event), "ungetmouse");
1741 #endif
1743 static PyObject *
1744 PyCurses_GetWin(PyCursesWindowObject *self, PyObject *temp)
1746 WINDOW *win;
1748 PyCursesInitialised
1750 if (!PyFile_Check(temp)) {
1751 PyErr_SetString(PyExc_TypeError, "argument must be a file object");
1752 return NULL;
1755 win = getwin(PyFile_AsFile(temp));
1757 if (win == NULL) {
1758 PyErr_SetString(PyCursesError, catchall_NULL);
1759 return NULL;
1762 return PyCursesWindow_New(win);
1765 static PyObject *
1766 PyCurses_HalfDelay(PyObject *self, PyObject *args)
1768 unsigned char tenths;
1770 PyCursesInitialised
1772 if (!PyArg_ParseTuple(args, "b:halfdelay", &tenths)) return NULL;
1774 return PyCursesCheckERR(halfdelay(tenths), "halfdelay");
1777 #ifndef STRICT_SYSV_CURSES
1778 /* No has_key! */
1779 static PyObject * PyCurses_has_key(PyObject *self, PyObject *args)
1781 int ch;
1783 PyCursesInitialised
1785 if (!PyArg_ParseTuple(args,"i",&ch)) return NULL;
1787 if (has_key(ch) == FALSE) {
1788 Py_INCREF(Py_False);
1789 return Py_False;
1791 Py_INCREF(Py_True);
1792 return Py_True;
1794 #endif /* STRICT_SYSV_CURSES */
1796 static PyObject *
1797 PyCurses_Init_Color(PyObject *self, PyObject *args)
1799 short color, r, g, b;
1801 PyCursesInitialised
1802 PyCursesInitialisedColor
1804 switch(PyTuple_Size(args)) {
1805 case 4:
1806 if (!PyArg_ParseTuple(args, "hhhh;color,r,g,b", &color, &r, &g, &b)) return NULL;
1807 break;
1808 default:
1809 PyErr_SetString(PyExc_TypeError, "init_color requires 4 arguments");
1810 return NULL;
1813 return PyCursesCheckERR(init_color(color, r, g, b), "init_color");
1816 static PyObject *
1817 PyCurses_Init_Pair(PyObject *self, PyObject *args)
1819 short pair, f, b;
1821 PyCursesInitialised
1822 PyCursesInitialisedColor
1824 if (PyTuple_Size(args) != 3) {
1825 PyErr_SetString(PyExc_TypeError, "init_pair requires 3 arguments");
1826 return NULL;
1829 if (!PyArg_ParseTuple(args, "hhh;pair, f, b", &pair, &f, &b)) return NULL;
1831 return PyCursesCheckERR(init_pair(pair, f, b), "init_pair");
1834 static PyObject *ModDict;
1836 static PyObject *
1837 PyCurses_InitScr(PyObject *self)
1839 WINDOW *win;
1841 if (initialised == TRUE) {
1842 wrefresh(stdscr);
1843 return (PyObject *)PyCursesWindow_New(stdscr);
1846 win = initscr();
1848 if (win == NULL) {
1849 PyErr_SetString(PyCursesError, catchall_NULL);
1850 return NULL;
1853 initialised = initialised_setupterm = TRUE;
1855 /* This was moved from initcurses() because it core dumped on SGI,
1856 where they're not defined until you've called initscr() */
1857 #define SetDictInt(string,ch) \
1858 do { \
1859 PyObject *o = PyInt_FromLong((long) (ch)); \
1860 if (o && PyDict_SetItemString(ModDict, string, o) == 0) { \
1861 Py_DECREF(o); \
1863 } while (0)
1865 /* Here are some graphic symbols you can use */
1866 SetDictInt("ACS_ULCORNER", (ACS_ULCORNER));
1867 SetDictInt("ACS_LLCORNER", (ACS_LLCORNER));
1868 SetDictInt("ACS_URCORNER", (ACS_URCORNER));
1869 SetDictInt("ACS_LRCORNER", (ACS_LRCORNER));
1870 SetDictInt("ACS_LTEE", (ACS_LTEE));
1871 SetDictInt("ACS_RTEE", (ACS_RTEE));
1872 SetDictInt("ACS_BTEE", (ACS_BTEE));
1873 SetDictInt("ACS_TTEE", (ACS_TTEE));
1874 SetDictInt("ACS_HLINE", (ACS_HLINE));
1875 SetDictInt("ACS_VLINE", (ACS_VLINE));
1876 SetDictInt("ACS_PLUS", (ACS_PLUS));
1877 #if !defined(__hpux) || defined(HAVE_NCURSES_H)
1878 /* On HP/UX 11, these are of type cchar_t, which is not an
1879 integral type. If this is a problem on more platforms, a
1880 configure test should be added to determine whether ACS_S1
1881 is of integral type. */
1882 SetDictInt("ACS_S1", (ACS_S1));
1883 SetDictInt("ACS_S9", (ACS_S9));
1884 SetDictInt("ACS_DIAMOND", (ACS_DIAMOND));
1885 SetDictInt("ACS_CKBOARD", (ACS_CKBOARD));
1886 SetDictInt("ACS_DEGREE", (ACS_DEGREE));
1887 SetDictInt("ACS_PLMINUS", (ACS_PLMINUS));
1888 SetDictInt("ACS_BULLET", (ACS_BULLET));
1889 SetDictInt("ACS_LARROW", (ACS_LARROW));
1890 SetDictInt("ACS_RARROW", (ACS_RARROW));
1891 SetDictInt("ACS_DARROW", (ACS_DARROW));
1892 SetDictInt("ACS_UARROW", (ACS_UARROW));
1893 SetDictInt("ACS_BOARD", (ACS_BOARD));
1894 SetDictInt("ACS_LANTERN", (ACS_LANTERN));
1895 SetDictInt("ACS_BLOCK", (ACS_BLOCK));
1896 #endif
1897 SetDictInt("ACS_BSSB", (ACS_ULCORNER));
1898 SetDictInt("ACS_SSBB", (ACS_LLCORNER));
1899 SetDictInt("ACS_BBSS", (ACS_URCORNER));
1900 SetDictInt("ACS_SBBS", (ACS_LRCORNER));
1901 SetDictInt("ACS_SBSS", (ACS_RTEE));
1902 SetDictInt("ACS_SSSB", (ACS_LTEE));
1903 SetDictInt("ACS_SSBS", (ACS_BTEE));
1904 SetDictInt("ACS_BSSS", (ACS_TTEE));
1905 SetDictInt("ACS_BSBS", (ACS_HLINE));
1906 SetDictInt("ACS_SBSB", (ACS_VLINE));
1907 SetDictInt("ACS_SSSS", (ACS_PLUS));
1909 /* The following are never available with strict SYSV curses */
1910 #ifdef ACS_S3
1911 SetDictInt("ACS_S3", (ACS_S3));
1912 #endif
1913 #ifdef ACS_S7
1914 SetDictInt("ACS_S7", (ACS_S7));
1915 #endif
1916 #ifdef ACS_LEQUAL
1917 SetDictInt("ACS_LEQUAL", (ACS_LEQUAL));
1918 #endif
1919 #ifdef ACS_GEQUAL
1920 SetDictInt("ACS_GEQUAL", (ACS_GEQUAL));
1921 #endif
1922 #ifdef ACS_PI
1923 SetDictInt("ACS_PI", (ACS_PI));
1924 #endif
1925 #ifdef ACS_NEQUAL
1926 SetDictInt("ACS_NEQUAL", (ACS_NEQUAL));
1927 #endif
1928 #ifdef ACS_STERLING
1929 SetDictInt("ACS_STERLING", (ACS_STERLING));
1930 #endif
1932 SetDictInt("LINES", LINES);
1933 SetDictInt("COLS", COLS);
1935 return (PyObject *)PyCursesWindow_New(win);
1938 static PyObject *
1939 PyCurses_setupterm(PyObject* self, PyObject *args, PyObject* keywds)
1941 int fd = -1;
1942 int err;
1943 char* termstr = NULL;
1945 static char *kwlist[] = {"term", "fd", NULL};
1947 if (!PyArg_ParseTupleAndKeywords(
1948 args, keywds, "|zi:setupterm", kwlist, &termstr, &fd)) {
1949 return NULL;
1952 if (fd == -1) {
1953 PyObject* sys_stdout;
1955 sys_stdout = PySys_GetObject("stdout");
1957 if (sys_stdout == NULL) {
1958 PyErr_SetString(
1959 PyCursesError,
1960 "lost sys.stdout");
1961 return NULL;
1964 fd = PyObject_AsFileDescriptor(sys_stdout);
1966 if (fd == -1) {
1967 return NULL;
1971 if (setupterm(termstr,fd,&err) == ERR) {
1972 char* s = "setupterm: unknown error";
1974 if (err == 0) {
1975 s = "setupterm: could not find terminal";
1976 } else if (err == -1) {
1977 s = "setupterm: could not find terminfo database";
1980 PyErr_SetString(PyCursesError,s);
1981 return NULL;
1984 initialised_setupterm = TRUE;
1986 Py_INCREF(Py_None);
1987 return Py_None;
1990 static PyObject *
1991 PyCurses_IntrFlush(PyObject *self, PyObject *args)
1993 int ch;
1995 PyCursesInitialised
1997 switch(PyTuple_Size(args)) {
1998 case 1:
1999 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL;
2000 break;
2001 default:
2002 PyErr_SetString(PyExc_TypeError, "intrflush requires 1 argument");
2003 return NULL;
2006 return PyCursesCheckERR(intrflush(NULL,ch), "intrflush");
2009 #ifdef HAVE_CURSES_IS_TERM_RESIZED
2010 static PyObject *
2011 PyCurses_Is_Term_Resized(PyObject *self, PyObject *args)
2013 int lines;
2014 int columns;
2015 int result;
2017 PyCursesInitialised
2019 if (!PyArg_ParseTuple(args,"ii:is_term_resized", &lines, &columns))
2020 return NULL;
2021 result = is_term_resized(lines, columns);
2022 if (result == TRUE) {
2023 Py_INCREF(Py_True);
2024 return Py_True;
2025 } else {
2026 Py_INCREF(Py_False);
2027 return Py_False;
2030 #endif /* HAVE_CURSES_IS_TERM_RESIZED */
2032 #if !defined(__NetBSD__)
2033 static PyObject *
2034 PyCurses_KeyName(PyObject *self, PyObject *args)
2036 const char *knp;
2037 int ch;
2039 PyCursesInitialised
2041 if (!PyArg_ParseTuple(args,"i",&ch)) return NULL;
2043 if (ch < 0) {
2044 PyErr_SetString(PyExc_ValueError, "invalid key number");
2045 return NULL;
2047 knp = keyname(ch);
2049 return PyString_FromString((knp == NULL) ? "" : (char *)knp);
2051 #endif
2053 static PyObject *
2054 PyCurses_KillChar(PyObject *self)
2056 char ch;
2058 ch = killchar();
2060 return PyString_FromStringAndSize(&ch, 1);
2063 static PyObject *
2064 PyCurses_Meta(PyObject *self, PyObject *args)
2066 int ch;
2068 PyCursesInitialised
2070 switch(PyTuple_Size(args)) {
2071 case 1:
2072 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL;
2073 break;
2074 default:
2075 PyErr_SetString(PyExc_TypeError, "meta requires 1 argument");
2076 return NULL;
2079 return PyCursesCheckERR(meta(stdscr, ch), "meta");
2082 #ifdef NCURSES_MOUSE_VERSION
2083 static PyObject *
2084 PyCurses_MouseInterval(PyObject *self, PyObject *args)
2086 int interval;
2087 PyCursesInitialised
2089 if (!PyArg_ParseTuple(args,"i;interval",&interval))
2090 return NULL;
2091 return PyCursesCheckERR(mouseinterval(interval), "mouseinterval");
2094 static PyObject *
2095 PyCurses_MouseMask(PyObject *self, PyObject *args)
2097 int newmask;
2098 mmask_t oldmask, availmask;
2100 PyCursesInitialised
2101 if (!PyArg_ParseTuple(args,"i;mousemask",&newmask))
2102 return NULL;
2103 availmask = mousemask(newmask, &oldmask);
2104 return Py_BuildValue("(ll)", (long)availmask, (long)oldmask);
2106 #endif
2108 static PyObject *
2109 PyCurses_Napms(PyObject *self, PyObject *args)
2111 int ms;
2113 PyCursesInitialised
2114 if (!PyArg_ParseTuple(args, "i;ms", &ms)) return NULL;
2116 return Py_BuildValue("i", napms(ms));
2120 static PyObject *
2121 PyCurses_NewPad(PyObject *self, PyObject *args)
2123 WINDOW *win;
2124 int nlines, ncols;
2126 PyCursesInitialised
2128 if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols)) return NULL;
2130 win = newpad(nlines, ncols);
2132 if (win == NULL) {
2133 PyErr_SetString(PyCursesError, catchall_NULL);
2134 return NULL;
2137 return (PyObject *)PyCursesWindow_New(win);
2140 static PyObject *
2141 PyCurses_NewWindow(PyObject *self, PyObject *args)
2143 WINDOW *win;
2144 int nlines, ncols, begin_y=0, begin_x=0;
2146 PyCursesInitialised
2148 switch (PyTuple_Size(args)) {
2149 case 2:
2150 if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols))
2151 return NULL;
2152 break;
2153 case 4:
2154 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
2155 &nlines,&ncols,&begin_y,&begin_x))
2156 return NULL;
2157 break;
2158 default:
2159 PyErr_SetString(PyExc_TypeError, "newwin requires 2 or 4 arguments");
2160 return NULL;
2163 win = newwin(nlines,ncols,begin_y,begin_x);
2164 if (win == NULL) {
2165 PyErr_SetString(PyCursesError, catchall_NULL);
2166 return NULL;
2169 return (PyObject *)PyCursesWindow_New(win);
2172 static PyObject *
2173 PyCurses_Pair_Content(PyObject *self, PyObject *args)
2175 short pair,f,b;
2177 PyCursesInitialised
2178 PyCursesInitialisedColor
2180 switch(PyTuple_Size(args)) {
2181 case 1:
2182 if (!PyArg_ParseTuple(args, "h;pair", &pair)) return NULL;
2183 break;
2184 default:
2185 PyErr_SetString(PyExc_TypeError, "pair_content requires 1 argument");
2186 return NULL;
2189 if (pair_content(pair, &f, &b)==ERR) {
2190 PyErr_SetString(PyCursesError,
2191 "Argument 1 was out of range. (1..COLOR_PAIRS-1)");
2192 return NULL;
2195 return Py_BuildValue("(ii)", f, b);
2198 static PyObject *
2199 PyCurses_pair_number(PyObject *self, PyObject *args)
2201 int n;
2203 PyCursesInitialised
2204 PyCursesInitialisedColor
2206 switch(PyTuple_Size(args)) {
2207 case 1:
2208 if (!PyArg_ParseTuple(args, "i;pairvalue", &n)) return NULL;
2209 break;
2210 default:
2211 PyErr_SetString(PyExc_TypeError,
2212 "pair_number requires 1 argument");
2213 return NULL;
2216 return PyInt_FromLong((long) ((n & A_COLOR) >> 8));
2219 static PyObject *
2220 PyCurses_Putp(PyObject *self, PyObject *args)
2222 char *str;
2224 if (!PyArg_ParseTuple(args,"s;str", &str)) return NULL;
2225 return PyCursesCheckERR(putp(str), "putp");
2228 static PyObject *
2229 PyCurses_QiFlush(PyObject *self, PyObject *args)
2231 int flag = 0;
2233 PyCursesInitialised
2235 switch(PyTuple_Size(args)) {
2236 case 0:
2237 qiflush();
2238 Py_INCREF(Py_None);
2239 return Py_None;
2240 case 1:
2241 if (!PyArg_ParseTuple(args, "i;True(1) or False(0)", &flag)) return NULL;
2242 if (flag) qiflush();
2243 else noqiflush();
2244 Py_INCREF(Py_None);
2245 return Py_None;
2246 default:
2247 PyErr_SetString(PyExc_TypeError, "qiflush requires 0 or 1 arguments");
2248 return NULL;
2252 /* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES
2253 * and _curses.COLS */
2254 static int
2255 update_lines_cols(void)
2257 PyObject *o;
2258 PyObject *m = PyImport_ImportModuleNoBlock("curses");
2260 if (!m)
2261 return 0;
2263 o = PyInt_FromLong(LINES);
2264 if (!o) {
2265 Py_DECREF(m);
2266 return 0;
2268 if (PyObject_SetAttrString(m, "LINES", o)) {
2269 Py_DECREF(m);
2270 Py_DECREF(o);
2271 return 0;
2273 if (PyDict_SetItemString(ModDict, "LINES", o)) {
2274 Py_DECREF(m);
2275 Py_DECREF(o);
2276 return 0;
2278 Py_DECREF(o);
2279 o = PyInt_FromLong(COLS);
2280 if (!o) {
2281 Py_DECREF(m);
2282 return 0;
2284 if (PyObject_SetAttrString(m, "COLS", o)) {
2285 Py_DECREF(m);
2286 Py_DECREF(o);
2287 return 0;
2289 if (PyDict_SetItemString(ModDict, "COLS", o)) {
2290 Py_DECREF(m);
2291 Py_DECREF(o);
2292 return 0;
2294 Py_DECREF(o);
2295 Py_DECREF(m);
2296 return 1;
2299 #ifdef HAVE_CURSES_RESIZETERM
2300 static PyObject *
2301 PyCurses_ResizeTerm(PyObject *self, PyObject *args)
2303 int lines;
2304 int columns;
2305 PyObject *result;
2307 PyCursesInitialised
2309 if (!PyArg_ParseTuple(args,"ii:resizeterm", &lines, &columns))
2310 return NULL;
2312 result = PyCursesCheckERR(resizeterm(lines, columns), "resizeterm");
2313 if (!result)
2314 return NULL;
2315 if (!update_lines_cols())
2316 return NULL;
2317 return result;
2320 #endif
2322 #ifdef HAVE_CURSES_RESIZE_TERM
2323 static PyObject *
2324 PyCurses_Resize_Term(PyObject *self, PyObject *args)
2326 int lines;
2327 int columns;
2329 PyObject *result;
2331 PyCursesInitialised
2333 if (!PyArg_ParseTuple(args,"ii:resize_term", &lines, &columns))
2334 return NULL;
2336 result = PyCursesCheckERR(resize_term(lines, columns), "resize_term");
2337 if (!result)
2338 return NULL;
2339 if (!update_lines_cols())
2340 return NULL;
2341 return result;
2343 #endif /* HAVE_CURSES_RESIZE_TERM */
2345 static PyObject *
2346 PyCurses_setsyx(PyObject *self, PyObject *args)
2348 int y,x;
2350 PyCursesInitialised
2352 if (PyTuple_Size(args)!=2) {
2353 PyErr_SetString(PyExc_TypeError, "setsyx requires 2 arguments");
2354 return NULL;
2357 if (!PyArg_ParseTuple(args, "ii;y, x", &y, &x)) return NULL;
2359 setsyx(y,x);
2361 Py_INCREF(Py_None);
2362 return Py_None;
2365 static PyObject *
2366 PyCurses_Start_Color(PyObject *self)
2368 int code;
2369 PyObject *c, *cp;
2371 PyCursesInitialised
2373 code = start_color();
2374 if (code != ERR) {
2375 initialisedcolors = TRUE;
2376 c = PyInt_FromLong((long) COLORS);
2377 PyDict_SetItemString(ModDict, "COLORS", c);
2378 Py_DECREF(c);
2379 cp = PyInt_FromLong((long) COLOR_PAIRS);
2380 PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp);
2381 Py_DECREF(cp);
2382 Py_INCREF(Py_None);
2383 return Py_None;
2384 } else {
2385 PyErr_SetString(PyCursesError, "start_color() returned ERR");
2386 return NULL;
2390 static PyObject *
2391 PyCurses_tigetflag(PyObject *self, PyObject *args)
2393 char *capname;
2395 PyCursesSetupTermCalled;
2397 if (!PyArg_ParseTuple(args, "z", &capname))
2398 return NULL;
2400 return PyInt_FromLong( (long) tigetflag( capname ) );
2403 static PyObject *
2404 PyCurses_tigetnum(PyObject *self, PyObject *args)
2406 char *capname;
2408 PyCursesSetupTermCalled;
2410 if (!PyArg_ParseTuple(args, "z", &capname))
2411 return NULL;
2413 return PyInt_FromLong( (long) tigetnum( capname ) );
2416 static PyObject *
2417 PyCurses_tigetstr(PyObject *self, PyObject *args)
2419 char *capname;
2421 PyCursesSetupTermCalled;
2423 if (!PyArg_ParseTuple(args, "z", &capname))
2424 return NULL;
2426 capname = tigetstr( capname );
2427 if (capname == 0 || capname == (char*) -1) {
2428 Py_INCREF(Py_None);
2429 return Py_None;
2431 return PyString_FromString( capname );
2434 static PyObject *
2435 PyCurses_tparm(PyObject *self, PyObject *args)
2437 char* fmt;
2438 char* result = NULL;
2439 int i1=0,i2=0,i3=0,i4=0,i5=0,i6=0,i7=0,i8=0,i9=0;
2441 PyCursesSetupTermCalled;
2443 if (!PyArg_ParseTuple(args, "s|iiiiiiiii:tparm",
2444 &fmt, &i1, &i2, &i3, &i4,
2445 &i5, &i6, &i7, &i8, &i9)) {
2446 return NULL;
2449 result = tparm(fmt,i1,i2,i3,i4,i5,i6,i7,i8,i9);
2450 if (!result) {
2451 PyErr_SetString(PyCursesError, "tparm() returned NULL");
2452 return NULL;
2455 return PyString_FromString(result);
2458 static PyObject *
2459 PyCurses_TypeAhead(PyObject *self, PyObject *args)
2461 int fd;
2463 PyCursesInitialised
2465 if (!PyArg_ParseTuple(args,"i;fd",&fd)) return NULL;
2467 return PyCursesCheckERR(typeahead( fd ), "typeahead");
2470 static PyObject *
2471 PyCurses_UnCtrl(PyObject *self, PyObject *args)
2473 PyObject *temp;
2474 chtype ch;
2476 PyCursesInitialised
2478 if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
2480 if (PyInt_Check(temp))
2481 ch = (chtype) PyInt_AsLong(temp);
2482 else if (PyString_Check(temp))
2483 ch = (chtype) *PyString_AsString(temp);
2484 else {
2485 PyErr_SetString(PyExc_TypeError, "argument must be a ch or an int");
2486 return NULL;
2489 return PyString_FromString(unctrl(ch));
2492 static PyObject *
2493 PyCurses_UngetCh(PyObject *self, PyObject *args)
2495 PyObject *temp;
2496 int ch;
2498 PyCursesInitialised
2500 if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
2502 if (PyInt_Check(temp))
2503 ch = (int) PyInt_AsLong(temp);
2504 else if (PyString_Check(temp))
2505 ch = (int) *PyString_AsString(temp);
2506 else {
2507 PyErr_SetString(PyExc_TypeError, "argument must be a ch or an int");
2508 return NULL;
2511 return PyCursesCheckERR(ungetch(ch), "ungetch");
2514 static PyObject *
2515 PyCurses_Use_Env(PyObject *self, PyObject *args)
2517 int flag;
2519 PyCursesInitialised
2521 switch(PyTuple_Size(args)) {
2522 case 1:
2523 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&flag))
2524 return NULL;
2525 break;
2526 default:
2527 PyErr_SetString(PyExc_TypeError, "use_env requires 1 argument");
2528 return NULL;
2530 use_env(flag);
2531 Py_INCREF(Py_None);
2532 return Py_None;
2535 #ifndef STRICT_SYSV_CURSES
2536 static PyObject *
2537 PyCurses_Use_Default_Colors(PyObject *self)
2539 int code;
2541 PyCursesInitialised
2542 PyCursesInitialisedColor
2544 code = use_default_colors();
2545 if (code != ERR) {
2546 Py_INCREF(Py_None);
2547 return Py_None;
2548 } else {
2549 PyErr_SetString(PyCursesError, "use_default_colors() returned ERR");
2550 return NULL;
2553 #endif /* STRICT_SYSV_CURSES */
2555 /* List of functions defined in the module */
2557 static PyMethodDef PyCurses_methods[] = {
2558 {"baudrate", (PyCFunction)PyCurses_baudrate, METH_NOARGS},
2559 {"beep", (PyCFunction)PyCurses_beep, METH_NOARGS},
2560 {"can_change_color", (PyCFunction)PyCurses_can_change_color, METH_NOARGS},
2561 {"cbreak", (PyCFunction)PyCurses_cbreak, METH_VARARGS},
2562 {"color_content", (PyCFunction)PyCurses_Color_Content, METH_VARARGS},
2563 {"color_pair", (PyCFunction)PyCurses_color_pair, METH_VARARGS},
2564 {"curs_set", (PyCFunction)PyCurses_Curs_Set, METH_VARARGS},
2565 {"def_prog_mode", (PyCFunction)PyCurses_def_prog_mode, METH_NOARGS},
2566 {"def_shell_mode", (PyCFunction)PyCurses_def_shell_mode, METH_NOARGS},
2567 {"delay_output", (PyCFunction)PyCurses_Delay_Output, METH_VARARGS},
2568 {"doupdate", (PyCFunction)PyCurses_doupdate, METH_NOARGS},
2569 {"echo", (PyCFunction)PyCurses_echo, METH_VARARGS},
2570 {"endwin", (PyCFunction)PyCurses_endwin, METH_NOARGS},
2571 {"erasechar", (PyCFunction)PyCurses_EraseChar, METH_NOARGS},
2572 {"filter", (PyCFunction)PyCurses_filter, METH_NOARGS},
2573 {"flash", (PyCFunction)PyCurses_flash, METH_NOARGS},
2574 {"flushinp", (PyCFunction)PyCurses_flushinp, METH_NOARGS},
2575 #ifdef NCURSES_MOUSE_VERSION
2576 {"getmouse", (PyCFunction)PyCurses_GetMouse, METH_NOARGS},
2577 {"ungetmouse", (PyCFunction)PyCurses_UngetMouse, METH_VARARGS},
2578 #endif
2579 {"getsyx", (PyCFunction)PyCurses_getsyx, METH_NOARGS},
2580 {"getwin", (PyCFunction)PyCurses_GetWin, METH_O},
2581 {"has_colors", (PyCFunction)PyCurses_has_colors, METH_NOARGS},
2582 {"has_ic", (PyCFunction)PyCurses_has_ic, METH_NOARGS},
2583 {"has_il", (PyCFunction)PyCurses_has_il, METH_NOARGS},
2584 #ifndef STRICT_SYSV_CURSES
2585 {"has_key", (PyCFunction)PyCurses_has_key, METH_VARARGS},
2586 #endif
2587 {"halfdelay", (PyCFunction)PyCurses_HalfDelay, METH_VARARGS},
2588 {"init_color", (PyCFunction)PyCurses_Init_Color, METH_VARARGS},
2589 {"init_pair", (PyCFunction)PyCurses_Init_Pair, METH_VARARGS},
2590 {"initscr", (PyCFunction)PyCurses_InitScr, METH_NOARGS},
2591 {"intrflush", (PyCFunction)PyCurses_IntrFlush, METH_VARARGS},
2592 {"isendwin", (PyCFunction)PyCurses_isendwin, METH_NOARGS},
2593 #ifdef HAVE_CURSES_IS_TERM_RESIZED
2594 {"is_term_resized", (PyCFunction)PyCurses_Is_Term_Resized, METH_VARARGS},
2595 #endif
2596 #if !defined(__NetBSD__)
2597 {"keyname", (PyCFunction)PyCurses_KeyName, METH_VARARGS},
2598 #endif
2599 {"killchar", (PyCFunction)PyCurses_KillChar, METH_NOARGS},
2600 {"longname", (PyCFunction)PyCurses_longname, METH_NOARGS},
2601 {"meta", (PyCFunction)PyCurses_Meta, METH_VARARGS},
2602 #ifdef NCURSES_MOUSE_VERSION
2603 {"mouseinterval", (PyCFunction)PyCurses_MouseInterval, METH_VARARGS},
2604 {"mousemask", (PyCFunction)PyCurses_MouseMask, METH_VARARGS},
2605 #endif
2606 {"napms", (PyCFunction)PyCurses_Napms, METH_VARARGS},
2607 {"newpad", (PyCFunction)PyCurses_NewPad, METH_VARARGS},
2608 {"newwin", (PyCFunction)PyCurses_NewWindow, METH_VARARGS},
2609 {"nl", (PyCFunction)PyCurses_nl, METH_VARARGS},
2610 {"nocbreak", (PyCFunction)PyCurses_nocbreak, METH_NOARGS},
2611 {"noecho", (PyCFunction)PyCurses_noecho, METH_NOARGS},
2612 {"nonl", (PyCFunction)PyCurses_nonl, METH_NOARGS},
2613 {"noqiflush", (PyCFunction)PyCurses_noqiflush, METH_NOARGS},
2614 {"noraw", (PyCFunction)PyCurses_noraw, METH_NOARGS},
2615 {"pair_content", (PyCFunction)PyCurses_Pair_Content, METH_VARARGS},
2616 {"pair_number", (PyCFunction)PyCurses_pair_number, METH_VARARGS},
2617 {"putp", (PyCFunction)PyCurses_Putp, METH_VARARGS},
2618 {"qiflush", (PyCFunction)PyCurses_QiFlush, METH_VARARGS},
2619 {"raw", (PyCFunction)PyCurses_raw, METH_VARARGS},
2620 {"reset_prog_mode", (PyCFunction)PyCurses_reset_prog_mode, METH_NOARGS},
2621 {"reset_shell_mode", (PyCFunction)PyCurses_reset_shell_mode, METH_NOARGS},
2622 {"resetty", (PyCFunction)PyCurses_resetty, METH_NOARGS},
2623 #ifdef HAVE_CURSES_RESIZETERM
2624 {"resizeterm", (PyCFunction)PyCurses_ResizeTerm, METH_VARARGS},
2625 #endif
2626 #ifdef HAVE_CURSES_RESIZE_TERM
2627 {"resize_term", (PyCFunction)PyCurses_Resize_Term, METH_VARARGS},
2628 #endif
2629 {"savetty", (PyCFunction)PyCurses_savetty, METH_NOARGS},
2630 {"setsyx", (PyCFunction)PyCurses_setsyx, METH_VARARGS},
2631 {"setupterm", (PyCFunction)PyCurses_setupterm,
2632 METH_VARARGS|METH_KEYWORDS},
2633 {"start_color", (PyCFunction)PyCurses_Start_Color, METH_NOARGS},
2634 {"termattrs", (PyCFunction)PyCurses_termattrs, METH_NOARGS},
2635 {"termname", (PyCFunction)PyCurses_termname, METH_NOARGS},
2636 {"tigetflag", (PyCFunction)PyCurses_tigetflag, METH_VARARGS},
2637 {"tigetnum", (PyCFunction)PyCurses_tigetnum, METH_VARARGS},
2638 {"tigetstr", (PyCFunction)PyCurses_tigetstr, METH_VARARGS},
2639 {"tparm", (PyCFunction)PyCurses_tparm, METH_VARARGS},
2640 {"typeahead", (PyCFunction)PyCurses_TypeAhead, METH_VARARGS},
2641 {"unctrl", (PyCFunction)PyCurses_UnCtrl, METH_VARARGS},
2642 {"ungetch", (PyCFunction)PyCurses_UngetCh, METH_VARARGS},
2643 {"use_env", (PyCFunction)PyCurses_Use_Env, METH_VARARGS},
2644 #ifndef STRICT_SYSV_CURSES
2645 {"use_default_colors", (PyCFunction)PyCurses_Use_Default_Colors, METH_NOARGS},
2646 #endif
2647 {NULL, NULL} /* sentinel */
2650 /* Initialization function for the module */
2652 PyMODINIT_FUNC
2653 init_curses(void)
2655 PyObject *m, *d, *v, *c_api_object;
2656 static void *PyCurses_API[PyCurses_API_pointers];
2658 /* Initialize object type */
2659 Py_TYPE(&PyCursesWindow_Type) = &PyType_Type;
2661 /* Initialize the C API pointer array */
2662 PyCurses_API[0] = (void *)&PyCursesWindow_Type;
2663 PyCurses_API[1] = (void *)func_PyCursesSetupTermCalled;
2664 PyCurses_API[2] = (void *)func_PyCursesInitialised;
2665 PyCurses_API[3] = (void *)func_PyCursesInitialisedColor;
2667 /* Create the module and add the functions */
2668 m = Py_InitModule("_curses", PyCurses_methods);
2669 if (m == NULL)
2670 return;
2672 /* Add some symbolic constants to the module */
2673 d = PyModule_GetDict(m);
2674 if (d == NULL)
2675 return;
2676 ModDict = d; /* For PyCurses_InitScr to use later */
2678 /* Add a CObject for the C API */
2679 c_api_object = PyCObject_FromVoidPtr((void *)PyCurses_API, NULL);
2680 PyDict_SetItemString(d, "_C_API", c_api_object);
2681 Py_DECREF(c_api_object);
2683 /* For exception curses.error */
2684 PyCursesError = PyErr_NewException("_curses.error", NULL, NULL);
2685 PyDict_SetItemString(d, "error", PyCursesError);
2687 /* Make the version available */
2688 v = PyString_FromString(PyCursesVersion);
2689 PyDict_SetItemString(d, "version", v);
2690 PyDict_SetItemString(d, "__version__", v);
2691 Py_DECREF(v);
2693 SetDictInt("ERR", ERR);
2694 SetDictInt("OK", OK);
2696 /* Here are some attributes you can add to chars to print */
2698 SetDictInt("A_ATTRIBUTES", A_ATTRIBUTES);
2699 SetDictInt("A_NORMAL", A_NORMAL);
2700 SetDictInt("A_STANDOUT", A_STANDOUT);
2701 SetDictInt("A_UNDERLINE", A_UNDERLINE);
2702 SetDictInt("A_REVERSE", A_REVERSE);
2703 SetDictInt("A_BLINK", A_BLINK);
2704 SetDictInt("A_DIM", A_DIM);
2705 SetDictInt("A_BOLD", A_BOLD);
2706 SetDictInt("A_ALTCHARSET", A_ALTCHARSET);
2707 #if !defined(__NetBSD__)
2708 SetDictInt("A_INVIS", A_INVIS);
2709 #endif
2710 SetDictInt("A_PROTECT", A_PROTECT);
2711 SetDictInt("A_CHARTEXT", A_CHARTEXT);
2712 SetDictInt("A_COLOR", A_COLOR);
2714 /* The following are never available with strict SYSV curses */
2715 #ifdef A_HORIZONTAL
2716 SetDictInt("A_HORIZONTAL", A_HORIZONTAL);
2717 #endif
2718 #ifdef A_LEFT
2719 SetDictInt("A_LEFT", A_LEFT);
2720 #endif
2721 #ifdef A_LOW
2722 SetDictInt("A_LOW", A_LOW);
2723 #endif
2724 #ifdef A_RIGHT
2725 SetDictInt("A_RIGHT", A_RIGHT);
2726 #endif
2727 #ifdef A_TOP
2728 SetDictInt("A_TOP", A_TOP);
2729 #endif
2730 #ifdef A_VERTICAL
2731 SetDictInt("A_VERTICAL", A_VERTICAL);
2732 #endif
2734 SetDictInt("COLOR_BLACK", COLOR_BLACK);
2735 SetDictInt("COLOR_RED", COLOR_RED);
2736 SetDictInt("COLOR_GREEN", COLOR_GREEN);
2737 SetDictInt("COLOR_YELLOW", COLOR_YELLOW);
2738 SetDictInt("COLOR_BLUE", COLOR_BLUE);
2739 SetDictInt("COLOR_MAGENTA", COLOR_MAGENTA);
2740 SetDictInt("COLOR_CYAN", COLOR_CYAN);
2741 SetDictInt("COLOR_WHITE", COLOR_WHITE);
2743 #ifdef NCURSES_MOUSE_VERSION
2744 /* Mouse-related constants */
2745 SetDictInt("BUTTON1_PRESSED", BUTTON1_PRESSED);
2746 SetDictInt("BUTTON1_RELEASED", BUTTON1_RELEASED);
2747 SetDictInt("BUTTON1_CLICKED", BUTTON1_CLICKED);
2748 SetDictInt("BUTTON1_DOUBLE_CLICKED", BUTTON1_DOUBLE_CLICKED);
2749 SetDictInt("BUTTON1_TRIPLE_CLICKED", BUTTON1_TRIPLE_CLICKED);
2751 SetDictInt("BUTTON2_PRESSED", BUTTON2_PRESSED);
2752 SetDictInt("BUTTON2_RELEASED", BUTTON2_RELEASED);
2753 SetDictInt("BUTTON2_CLICKED", BUTTON2_CLICKED);
2754 SetDictInt("BUTTON2_DOUBLE_CLICKED", BUTTON2_DOUBLE_CLICKED);
2755 SetDictInt("BUTTON2_TRIPLE_CLICKED", BUTTON2_TRIPLE_CLICKED);
2757 SetDictInt("BUTTON3_PRESSED", BUTTON3_PRESSED);
2758 SetDictInt("BUTTON3_RELEASED", BUTTON3_RELEASED);
2759 SetDictInt("BUTTON3_CLICKED", BUTTON3_CLICKED);
2760 SetDictInt("BUTTON3_DOUBLE_CLICKED", BUTTON3_DOUBLE_CLICKED);
2761 SetDictInt("BUTTON3_TRIPLE_CLICKED", BUTTON3_TRIPLE_CLICKED);
2763 SetDictInt("BUTTON4_PRESSED", BUTTON4_PRESSED);
2764 SetDictInt("BUTTON4_RELEASED", BUTTON4_RELEASED);
2765 SetDictInt("BUTTON4_CLICKED", BUTTON4_CLICKED);
2766 SetDictInt("BUTTON4_DOUBLE_CLICKED", BUTTON4_DOUBLE_CLICKED);
2767 SetDictInt("BUTTON4_TRIPLE_CLICKED", BUTTON4_TRIPLE_CLICKED);
2769 SetDictInt("BUTTON_SHIFT", BUTTON_SHIFT);
2770 SetDictInt("BUTTON_CTRL", BUTTON_CTRL);
2771 SetDictInt("BUTTON_ALT", BUTTON_ALT);
2773 SetDictInt("ALL_MOUSE_EVENTS", ALL_MOUSE_EVENTS);
2774 SetDictInt("REPORT_MOUSE_POSITION", REPORT_MOUSE_POSITION);
2775 #endif
2776 /* Now set everything up for KEY_ variables */
2778 int key;
2779 char *key_n;
2780 char *key_n2;
2781 #if !defined(__NetBSD__)
2782 for (key=KEY_MIN;key < KEY_MAX; key++) {
2783 key_n = (char *)keyname(key);
2784 if (key_n == NULL || strcmp(key_n,"UNKNOWN KEY")==0)
2785 continue;
2786 if (strncmp(key_n,"KEY_F(",6)==0) {
2787 char *p1, *p2;
2788 key_n2 = malloc(strlen(key_n)+1);
2789 if (!key_n2) {
2790 PyErr_NoMemory();
2791 break;
2793 p1 = key_n;
2794 p2 = key_n2;
2795 while (*p1) {
2796 if (*p1 != '(' && *p1 != ')') {
2797 *p2 = *p1;
2798 p2++;
2800 p1++;
2802 *p2 = (char)0;
2803 } else
2804 key_n2 = key_n;
2805 SetDictInt(key_n2,key);
2806 if (key_n2 != key_n)
2807 free(key_n2);
2809 #endif
2810 SetDictInt("KEY_MIN", KEY_MIN);
2811 SetDictInt("KEY_MAX", KEY_MAX);