append(): Fixing the test for convertability after consultation with
[python/dscho.git] / Modules / _cursesmodule.c
blobaab24cfb555ef60844a33385f81453fd68a26a66
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
12 * <akuchlin@mems-exchange.org>.
14 * Permission is hereby granted, free of charge, to any person obtaining
15 * a copy of this source file to use, copy, modify, merge, or publish it
16 * subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or in any new file that contains a substantial portion of
20 * this file.
22 * THE AUTHOR MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF
23 * THE SOFTWARE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT
24 * EXPRESS OR IMPLIED WARRANTY. THE AUTHOR DISCLAIMS ALL WARRANTIES
25 * WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
26 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NON-INFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE
28 * AUTHOR BE LIABLE TO YOU OR ANY OTHER PARTY FOR ANY SPECIAL,
29 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
30 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE, STRICT LIABILITY OR
31 * ANY OTHER ACTION ARISING OUT OF OR IN CONNECTION WITH THE USE OR
32 * PERFORMANCE OF THIS SOFTWARE.
35 /* CVS: $Id$ */
39 A number of SysV or ncurses functions don't have wrappers yet; if you need
40 a given function, add it and send a patch. Here's a list of currently
41 unsupported functions:
43 addchnstr addchstr chgat color_set define_key
44 del_curterm delscreen dupwin inchnstr inchstr innstr keyok
45 mcprint mvaddchnstr mvaddchstr mvchgat mvcur mvinchnstr
46 mvinchstr mvinnstr mmvwaddchnstr mvwaddchstr mvwchgat
47 mvwgetnstr mvwinchnstr mvwinchstr mvwinnstr newterm
48 resizeterm 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 use_default_colors vidattr vidputs waddchnstr waddchstr wchgat
52 wcolor_set winchnstr winchstr winnstr wmouse_trafo wscrl
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
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
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
97 /* Release Number */
99 char *PyCursesVersion = "2.2";
101 /* Includes */
103 #include "Python.h"
105 #ifdef __osf__
106 #define _XOPEN_SOURCE_EXTENDED /* Define macro for OSF/1 */
107 #define STRICT_SYSV_CURSES /* Don't use ncurses extensions */
108 #endif
110 #ifdef __hpux
111 #define _XOPEN_SOURCE_EXTENDED
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 /* Utility Functions */
171 * Check the return code from a curses function and return None
172 * or raise an exception as appropriate. These are exported using the
173 * CObject API.
176 static PyObject *
177 PyCursesCheckERR(int code, char *fname)
179 if (code != ERR) {
180 Py_INCREF(Py_None);
181 return Py_None;
182 } else {
183 if (fname == NULL) {
184 PyErr_SetString(PyCursesError, catchall_ERR);
185 } else {
186 PyErr_Format(PyCursesError, "%s() returned ERR", fname);
188 return NULL;
192 static int
193 PyCurses_ConvertToChtype(PyObject *obj, chtype *ch)
195 if (PyInt_Check(obj)) {
196 *ch = (chtype) PyInt_AsLong(obj);
197 } else if(PyString_Check(obj)
198 && (PyString_Size(obj) == 1)) {
199 *ch = (chtype) *PyString_AsString(obj);
200 } else {
201 return 0;
203 return 1;
206 /* Function versions of the 3 functions for tested whether curses has been
207 initialised or not. */
209 static int func_PyCursesSetupTermCalled(void)
211 PyCursesSetupTermCalled;
212 return 1;
215 static int func_PyCursesInitialised(void)
217 PyCursesInitialised;
218 return 1;
221 static int func_PyCursesInitialisedColor(void)
223 PyCursesInitialisedColor;
224 return 1;
227 /*****************************************************************************
228 The Window Object
229 ******************************************************************************/
231 /* Definition of the window type */
233 PyTypeObject PyCursesWindow_Type;
235 /* Function prototype macros for Window object
237 X - function name
238 TYPE - parameter Type
239 ERGSTR - format string for construction of the return value
240 PARSESTR - format string for argument parsing
243 #define Window_NoArgNoReturnFunction(X) \
244 static PyObject *PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
245 { return PyCursesCheckERR(X(self->win), # X); }
247 #define Window_NoArgTrueFalseFunction(X) \
248 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self) \
250 if (X (self->win) == FALSE) { Py_INCREF(Py_False); return Py_False; } \
251 else { Py_INCREF(Py_True); return Py_True; } }
253 #define Window_NoArgNoReturnVoidFunction(X) \
254 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self) \
256 X(self->win); Py_INCREF(Py_None); return Py_None; }
258 #define Window_NoArg2TupleReturnFunction(X, TYPE, ERGSTR) \
259 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self) \
261 TYPE arg1, arg2; \
262 X(self->win,arg1,arg2); return Py_BuildValue(ERGSTR, arg1, arg2); }
264 #define Window_OneArgNoReturnVoidFunction(X, TYPE, PARSESTR) \
265 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
267 TYPE arg1; \
268 if (!PyArg_ParseTuple(args, PARSESTR, &arg1)) return NULL; \
269 X(self->win,arg1); Py_INCREF(Py_None); return Py_None; }
271 #define Window_OneArgNoReturnFunction(X, TYPE, PARSESTR) \
272 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
274 TYPE arg1; \
275 if (!PyArg_ParseTuple(args,PARSESTR, &arg1)) return NULL; \
276 return PyCursesCheckERR(X(self->win, arg1), # X); }
278 #define Window_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \
279 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
281 TYPE arg1, arg2; \
282 if (!PyArg_ParseTuple(args,PARSESTR, &arg1, &arg2)) return NULL; \
283 return PyCursesCheckERR(X(self->win, arg1, arg2), # X); }
285 /* ------------- WINDOW routines --------------- */
287 Window_NoArgNoReturnFunction(untouchwin)
288 Window_NoArgNoReturnFunction(touchwin)
289 Window_NoArgNoReturnFunction(redrawwin)
290 Window_NoArgNoReturnFunction(winsertln)
291 Window_NoArgNoReturnFunction(werase)
292 Window_NoArgNoReturnFunction(wdeleteln)
294 Window_NoArgTrueFalseFunction(is_wintouched)
296 Window_NoArgNoReturnVoidFunction(wsyncup)
297 Window_NoArgNoReturnVoidFunction(wsyncdown)
298 Window_NoArgNoReturnVoidFunction(wstandend)
299 Window_NoArgNoReturnVoidFunction(wstandout)
300 Window_NoArgNoReturnVoidFunction(wcursyncup)
301 Window_NoArgNoReturnVoidFunction(wclrtoeol)
302 Window_NoArgNoReturnVoidFunction(wclrtobot)
303 Window_NoArgNoReturnVoidFunction(wclear)
305 Window_OneArgNoReturnVoidFunction(idcok, int, "i;True(1) or False(0)")
306 Window_OneArgNoReturnVoidFunction(immedok, int, "i;True(1) or False(0)")
307 Window_OneArgNoReturnVoidFunction(wtimeout, int, "i;delay")
309 Window_NoArg2TupleReturnFunction(getyx, int, "ii")
310 Window_NoArg2TupleReturnFunction(getbegyx, int, "ii")
311 Window_NoArg2TupleReturnFunction(getmaxyx, int, "ii")
312 Window_NoArg2TupleReturnFunction(getparyx, int, "ii")
314 Window_OneArgNoReturnFunction(wattron, attr_t, "l;attr")
315 Window_OneArgNoReturnFunction(wattroff, attr_t, "l;attr")
316 Window_OneArgNoReturnFunction(wattrset, attr_t, "l;attr")
317 Window_OneArgNoReturnFunction(clearok, int, "i;True(1) or False(0)")
318 Window_OneArgNoReturnFunction(idlok, int, "i;True(1) or False(0)")
319 #if defined(__NetBSD__)
320 Window_OneArgNoReturnVoidFunction(keypad, int, "i;True(1) or False(0)")
321 #else
322 Window_OneArgNoReturnFunction(keypad, int, "i;True(1) or False(0)")
323 #endif
324 Window_OneArgNoReturnFunction(leaveok, int, "i;True(1) or False(0)")
325 #if defined(__NetBSD__)
326 Window_OneArgNoReturnVoidFunction(nodelay, int, "i;True(1) or False(0)")
327 #else
328 Window_OneArgNoReturnFunction(nodelay, int, "i;True(1) or False(0)")
329 #endif
330 Window_OneArgNoReturnFunction(notimeout, int, "i;True(1) or False(0)")
331 Window_OneArgNoReturnFunction(scrollok, int, "i;True(1) or False(0)")
332 Window_OneArgNoReturnFunction(winsdelln, int, "i;nlines")
333 Window_OneArgNoReturnFunction(syncok, int, "i;True(1) or False(0)")
335 Window_TwoArgNoReturnFunction(mvwin, int, "ii;y,x")
336 Window_TwoArgNoReturnFunction(mvderwin, int, "ii;y,x")
337 Window_TwoArgNoReturnFunction(wmove, int, "ii;y,x")
338 #ifndef STRICT_SYSV_CURSES
339 Window_TwoArgNoReturnFunction(wresize, int, "ii;lines,columns")
340 #endif
342 /* Allocation and deallocation of Window Objects */
344 static PyObject *
345 PyCursesWindow_New(WINDOW *win)
347 PyCursesWindowObject *wo;
349 wo = PyObject_NEW(PyCursesWindowObject, &PyCursesWindow_Type);
350 if (wo == NULL) return NULL;
351 wo->win = win;
352 return (PyObject *)wo;
355 static void
356 PyCursesWindow_Dealloc(PyCursesWindowObject *wo)
358 if (wo->win != stdscr) delwin(wo->win);
359 PyObject_DEL(wo);
362 /* Addch, Addstr, Addnstr */
364 static PyObject *
365 PyCursesWindow_AddCh(PyCursesWindowObject *self, PyObject *args)
367 int rtn, x, y, use_xy = FALSE;
368 PyObject *temp;
369 chtype ch = 0;
370 attr_t attr = A_NORMAL;
372 switch (PyTuple_Size(args)) {
373 case 1:
374 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
375 return NULL;
376 break;
377 case 2:
378 if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &temp, &attr))
379 return NULL;
380 break;
381 case 3:
382 if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &temp))
383 return NULL;
384 use_xy = TRUE;
385 break;
386 case 4:
387 if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr",
388 &y, &x, &temp, &attr))
389 return NULL;
390 use_xy = TRUE;
391 break;
392 default:
393 PyErr_SetString(PyExc_TypeError, "addch requires 1 to 4 arguments");
394 return NULL;
397 if (!PyCurses_ConvertToChtype(temp, &ch)) {
398 PyErr_SetString(PyExc_TypeError, "argument 1 or 3 must be a ch or an int");
399 return NULL;
402 if (use_xy == TRUE)
403 rtn = mvwaddch(self->win,y,x, ch | attr);
404 else {
405 rtn = waddch(self->win, ch | attr);
407 return PyCursesCheckERR(rtn, "addch");
410 static PyObject *
411 PyCursesWindow_AddStr(PyCursesWindowObject *self, PyObject *args)
413 int rtn;
414 int x, y;
415 char *str;
416 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
417 int use_xy = FALSE, use_attr = FALSE;
419 switch (PyTuple_Size(args)) {
420 case 1:
421 if (!PyArg_ParseTuple(args,"s;str", &str))
422 return NULL;
423 break;
424 case 2:
425 if (!PyArg_ParseTuple(args,"sl;str,attr", &str, &attr))
426 return NULL;
427 use_attr = TRUE;
428 break;
429 case 3:
430 if (!PyArg_ParseTuple(args,"iis;int,int,str", &y, &x, &str))
431 return NULL;
432 use_xy = TRUE;
433 break;
434 case 4:
435 if (!PyArg_ParseTuple(args,"iisl;int,int,str,attr", &y, &x, &str, &attr))
436 return NULL;
437 use_xy = use_attr = TRUE;
438 break;
439 default:
440 PyErr_SetString(PyExc_TypeError, "addstr requires 1 to 4 arguments");
441 return NULL;
444 if (use_attr == TRUE) {
445 attr_old = getattrs(self->win);
446 wattrset(self->win,attr);
448 if (use_xy == TRUE)
449 rtn = mvwaddstr(self->win,y,x,str);
450 else
451 rtn = waddstr(self->win,str);
452 if (use_attr == TRUE)
453 wattrset(self->win,attr_old);
454 return PyCursesCheckERR(rtn, "addstr");
457 static PyObject *
458 PyCursesWindow_AddNStr(PyCursesWindowObject *self, PyObject *args)
460 int rtn, x, y, n;
461 char *str;
462 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
463 int use_xy = FALSE, use_attr = FALSE;
465 switch (PyTuple_Size(args)) {
466 case 2:
467 if (!PyArg_ParseTuple(args,"si;str,n", &str, &n))
468 return NULL;
469 break;
470 case 3:
471 if (!PyArg_ParseTuple(args,"sil;str,n,attr", &str, &n, &attr))
472 return NULL;
473 use_attr = TRUE;
474 break;
475 case 4:
476 if (!PyArg_ParseTuple(args,"iisi;y,x,str,n", &y, &x, &str, &n))
477 return NULL;
478 use_xy = TRUE;
479 break;
480 case 5:
481 if (!PyArg_ParseTuple(args,"iisil;y,x,str,n,attr", &y, &x, &str, &n, &attr))
482 return NULL;
483 use_xy = use_attr = TRUE;
484 break;
485 default:
486 PyErr_SetString(PyExc_TypeError, "addnstr requires 2 to 5 arguments");
487 return NULL;
490 if (use_attr == TRUE) {
491 attr_old = getattrs(self->win);
492 wattrset(self->win,attr);
494 if (use_xy == TRUE)
495 rtn = mvwaddnstr(self->win,y,x,str,n);
496 else
497 rtn = waddnstr(self->win,str,n);
498 if (use_attr == TRUE)
499 wattrset(self->win,attr_old);
500 return PyCursesCheckERR(rtn, "addnstr");
503 static PyObject *
504 PyCursesWindow_Bkgd(PyCursesWindowObject *self, PyObject *args)
506 PyObject *temp;
507 chtype bkgd;
508 attr_t attr = A_NORMAL;
510 switch (PyTuple_Size(args)) {
511 case 1:
512 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
513 return NULL;
514 break;
515 case 2:
516 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &attr))
517 return NULL;
518 break;
519 default:
520 PyErr_SetString(PyExc_TypeError, "bkgd requires 1 or 2 arguments");
521 return NULL;
524 if (!PyCurses_ConvertToChtype(temp, &bkgd)) {
525 PyErr_SetString(PyExc_TypeError, "argument 1 or 3 must be a ch or an int");
526 return NULL;
529 return PyCursesCheckERR(wbkgd(self->win, bkgd | attr), "bkgd");
532 static PyObject *
533 PyCursesWindow_BkgdSet(PyCursesWindowObject *self, PyObject *args)
535 PyObject *temp;
536 chtype bkgd;
537 attr_t attr = A_NORMAL;
539 switch (PyTuple_Size(args)) {
540 case 1:
541 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
542 return NULL;
543 break;
544 case 2:
545 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &attr))
546 return NULL;
547 break;
548 default:
549 PyErr_SetString(PyExc_TypeError, "bkgdset requires 1 or 2 arguments");
550 return NULL;
553 if (!PyCurses_ConvertToChtype(temp, &bkgd)) {
554 PyErr_SetString(PyExc_TypeError, "argument 1 must be a ch or an int");
555 return NULL;
558 wbkgdset(self->win, bkgd | attr);
559 return PyCursesCheckERR(0, "bkgdset");
562 static PyObject *
563 PyCursesWindow_Border(PyCursesWindowObject *self, PyObject *args)
565 PyObject *temp[8];
566 chtype ch[8];
567 int i;
569 /* Clear the array of parameters */
570 for(i=0; i<8; i++) {
571 temp[i] = NULL;
572 ch[i] = 0;
575 if (!PyArg_ParseTuple(args,"|OOOOOOOO;ls,rs,ts,bs,tl,tr,bl,br",
576 &temp[0], &temp[1], &temp[2], &temp[3],
577 &temp[4], &temp[5], &temp[6], &temp[7]))
578 return NULL;
580 for(i=0; i<8; i++) {
581 if (temp[i] != NULL && !PyCurses_ConvertToChtype(temp[i], &ch[i])) {
582 PyErr_Format(PyExc_TypeError,
583 "argument %i must be a ch or an int", i+1);
584 return NULL;
588 wborder(self->win,
589 ch[0], ch[1], ch[2], ch[3],
590 ch[4], ch[5], ch[6], ch[7]);
591 Py_INCREF(Py_None);
592 return Py_None;
595 static PyObject *
596 PyCursesWindow_Box(PyCursesWindowObject *self, PyObject *args)
598 chtype ch1=0,ch2=0;
599 switch(PyTuple_Size(args)){
600 case 0: break;
601 default:
602 if (!PyArg_ParseTuple(args,"ll;vertint,horint", &ch1, &ch2))
603 return NULL;
605 box(self->win,ch1,ch2);
606 Py_INCREF(Py_None);
607 return Py_None;
610 #if defined(HAVE_NCURSES_H) || defined(MVWDELCH_IS_EXPRESSION)
611 #define py_mvwdelch mvwdelch
612 #else
613 int py_mvwdelch(WINDOW *w, int y, int x)
615 mvwdelch(w,y,x);
616 /* On HP/UX, mvwdelch already returns. On other systems,
617 we may well run into this return statement. */
618 return 0;
620 #endif
623 static PyObject *
624 PyCursesWindow_DelCh(PyCursesWindowObject *self, PyObject *args)
626 int rtn;
627 int x, y;
629 switch (PyTuple_Size(args)) {
630 case 0:
631 rtn = wdelch(self->win);
632 break;
633 case 2:
634 if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x))
635 return NULL;
636 rtn = py_mvwdelch(self->win,y,x);
637 break;
638 default:
639 PyErr_SetString(PyExc_TypeError, "delch requires 0 or 2 arguments");
640 return NULL;
642 return PyCursesCheckERR(rtn, "[mv]wdelch");
645 static PyObject *
646 PyCursesWindow_DerWin(PyCursesWindowObject *self, PyObject *args)
648 WINDOW *win;
649 int nlines, ncols, begin_y, begin_x;
651 nlines = 0;
652 ncols = 0;
653 switch (PyTuple_Size(args)) {
654 case 2:
655 if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
656 return NULL;
657 break;
658 case 4:
659 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
660 &nlines,&ncols,&begin_y,&begin_x))
661 return NULL;
662 break;
663 default:
664 PyErr_SetString(PyExc_TypeError, "derwin requires 2 or 4 arguments");
665 return NULL;
668 win = derwin(self->win,nlines,ncols,begin_y,begin_x);
670 if (win == NULL) {
671 PyErr_SetString(PyCursesError, catchall_NULL);
672 return NULL;
675 return (PyObject *)PyCursesWindow_New(win);
678 static PyObject *
679 PyCursesWindow_EchoChar(PyCursesWindowObject *self, PyObject *args)
681 PyObject *temp;
682 chtype ch;
683 attr_t attr = A_NORMAL;
685 switch (PyTuple_Size(args)) {
686 case 1:
687 if (!PyArg_ParseTuple(args,"O;ch or int", &temp))
688 return NULL;
689 break;
690 case 2:
691 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &attr))
692 return NULL;
693 break;
694 default:
695 PyErr_SetString(PyExc_TypeError, "echochar requires 1 or 2 arguments");
698 return NULL;
701 if (!PyCurses_ConvertToChtype(temp, &ch)) {
702 PyErr_SetString(PyExc_TypeError, "argument 1 must be a ch or an int");
703 return NULL;
706 #ifdef WINDOW_HAS_FLAGS
707 if (self->win->_flags & _ISPAD)
708 return PyCursesCheckERR(pechochar(self->win, ch | attr),
709 "echochar");
710 else
711 #endif
712 return PyCursesCheckERR(wechochar(self->win, ch | attr),
713 "echochar");
716 #ifdef NCURSES_MOUSE_VERSION
717 static PyObject *
718 PyCursesWindow_Enclose(PyCursesWindowObject *self, PyObject *args)
720 int x, y;
721 if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x))
722 return NULL;
724 return PyInt_FromLong( wenclose(self->win,y,x) );
726 #endif
728 static PyObject *
729 PyCursesWindow_GetBkgd(PyCursesWindowObject *self)
731 return PyInt_FromLong((long) getbkgd(self->win));
734 static PyObject *
735 PyCursesWindow_GetCh(PyCursesWindowObject *self, PyObject *args)
737 int x, y;
738 chtype rtn;
740 switch (PyTuple_Size(args)) {
741 case 0:
742 Py_BEGIN_ALLOW_THREADS
743 rtn = wgetch(self->win);
744 Py_END_ALLOW_THREADS
745 break;
746 case 2:
747 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
748 return NULL;
749 Py_BEGIN_ALLOW_THREADS
750 rtn = mvwgetch(self->win,y,x);
751 Py_END_ALLOW_THREADS
752 break;
753 default:
754 PyErr_SetString(PyExc_TypeError, "getch requires 0 or 2 arguments");
755 return NULL;
757 return PyInt_FromLong((long)rtn);
760 static PyObject *
761 PyCursesWindow_GetKey(PyCursesWindowObject *self, PyObject *args)
763 int x, y;
764 chtype rtn;
766 switch (PyTuple_Size(args)) {
767 case 0:
768 Py_BEGIN_ALLOW_THREADS
769 rtn = wgetch(self->win);
770 Py_END_ALLOW_THREADS
771 break;
772 case 2:
773 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
774 return NULL;
775 Py_BEGIN_ALLOW_THREADS
776 rtn = mvwgetch(self->win,y,x);
777 Py_END_ALLOW_THREADS
778 break;
779 default:
780 PyErr_SetString(PyExc_TypeError, "getkey requires 0 or 2 arguments");
781 return NULL;
783 if (rtn<=255)
784 return Py_BuildValue("c", rtn);
785 else
786 #if defined(__NetBSD__)
787 return PyString_FromString(unctrl(rtn));
788 #else
789 return PyString_FromString((char *)keyname(rtn));
790 #endif
793 static PyObject *
794 PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
796 int x, y, n;
797 char rtn[1024]; /* This should be big enough.. I hope */
798 int rtn2;
800 switch (PyTuple_Size(args)) {
801 case 0:
802 Py_BEGIN_ALLOW_THREADS
803 rtn2 = wgetstr(self->win,rtn);
804 Py_END_ALLOW_THREADS
805 break;
806 case 1:
807 if (!PyArg_ParseTuple(args,"i;n", &n))
808 return NULL;
809 Py_BEGIN_ALLOW_THREADS
810 rtn2 = wgetnstr(self->win,rtn,n);
811 Py_END_ALLOW_THREADS
812 break;
813 case 2:
814 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
815 return NULL;
816 Py_BEGIN_ALLOW_THREADS
817 rtn2 = mvwgetstr(self->win,y,x,rtn);
818 Py_END_ALLOW_THREADS
819 break;
820 case 3:
821 if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
822 return NULL;
823 #ifdef STRICT_SYSV_CURSES
824 /* Untested */
825 Py_BEGIN_ALLOW_THREADS
826 rtn2 = wmove(self->win,y,x)==ERR ? ERR :
827 wgetnstr(self->win, rtn, n);
828 Py_END_ALLOW_THREADS
829 #else
830 Py_BEGIN_ALLOW_THREADS
831 rtn2 = mvwgetnstr(self->win, y, x, rtn, n);
832 Py_END_ALLOW_THREADS
833 #endif
834 break;
835 default:
836 PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 2 arguments");
837 return NULL;
839 if (rtn2 == ERR)
840 rtn[0] = 0;
841 return PyString_FromString(rtn);
844 static PyObject *
845 PyCursesWindow_Hline(PyCursesWindowObject *self, PyObject *args)
847 PyObject *temp;
848 chtype ch;
849 int n, x, y, code = OK;
850 attr_t attr = A_NORMAL;
852 switch (PyTuple_Size(args)) {
853 case 2:
854 if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
855 return NULL;
856 break;
857 case 3:
858 if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &attr))
859 return NULL;
860 break;
861 case 4:
862 if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
863 return NULL;
864 code = wmove(self->win, y, x);
865 break;
866 case 5:
867 if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
868 &y, &x, &temp, &n, &attr))
869 return NULL;
870 code = wmove(self->win, y, x);
871 break;
872 default:
873 PyErr_SetString(PyExc_TypeError, "hline requires 2 to 5 arguments");
874 return NULL;
877 if (code != ERR) {
878 if (!PyCurses_ConvertToChtype(temp, &ch)) {
879 PyErr_SetString(PyExc_TypeError,
880 "argument 1 or 3 must be a ch or an int");
881 return NULL;
883 return PyCursesCheckERR(whline(self->win, ch | attr, n), "hline");
884 } else
885 return PyCursesCheckERR(code, "wmove");
888 static PyObject *
889 PyCursesWindow_InsCh(PyCursesWindowObject *self, PyObject *args)
891 int rtn, x, y, use_xy = FALSE;
892 PyObject *temp;
893 chtype ch = 0;
894 attr_t attr = A_NORMAL;
896 switch (PyTuple_Size(args)) {
897 case 1:
898 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
899 return NULL;
900 break;
901 case 2:
902 if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &temp, &attr))
903 return NULL;
904 break;
905 case 3:
906 if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &temp))
907 return NULL;
908 use_xy = TRUE;
909 break;
910 case 4:
911 if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr", &y, &x, &temp, &attr))
912 return NULL;
913 use_xy = TRUE;
914 break;
915 default:
916 PyErr_SetString(PyExc_TypeError, "insch requires 1 or 4 arguments");
917 return NULL;
920 if (!PyCurses_ConvertToChtype(temp, &ch)) {
921 PyErr_SetString(PyExc_TypeError,
922 "argument 1 or 3 must be a ch or an int");
923 return NULL;
926 if (use_xy == TRUE)
927 rtn = mvwinsch(self->win,y,x, ch | attr);
928 else {
929 rtn = winsch(self->win, ch | attr);
931 return PyCursesCheckERR(rtn, "insch");
934 static PyObject *
935 PyCursesWindow_InCh(PyCursesWindowObject *self, PyObject *args)
937 int x, y, rtn;
939 switch (PyTuple_Size(args)) {
940 case 0:
941 rtn = winch(self->win);
942 break;
943 case 2:
944 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
945 return NULL;
946 rtn = mvwinch(self->win,y,x);
947 break;
948 default:
949 PyErr_SetString(PyExc_TypeError, "inch requires 0 or 2 arguments");
950 return NULL;
952 return PyInt_FromLong((long) rtn);
955 static PyObject *
956 PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
958 int x, y, n;
959 char rtn[1024]; /* This should be big enough.. I hope */
960 int rtn2;
962 switch (PyTuple_Size(args)) {
963 case 0:
964 rtn2 = winstr(self->win,rtn);
965 break;
966 case 1:
967 if (!PyArg_ParseTuple(args,"i;n", &n))
968 return NULL;
969 rtn2 = winnstr(self->win,rtn,n);
970 break;
971 case 2:
972 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
973 return NULL;
974 rtn2 = mvwinstr(self->win,y,x,rtn);
975 break;
976 case 3:
977 if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
978 return NULL;
979 rtn2 = mvwinnstr(self->win, y, x, rtn, n);
980 break;
981 default:
982 PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments");
983 return NULL;
985 if (rtn2 == ERR)
986 rtn[0] = 0;
987 return PyString_FromString(rtn);
990 static PyObject *
991 PyCursesWindow_InsStr(PyCursesWindowObject *self, PyObject *args)
993 int rtn;
994 int x, y;
995 char *str;
996 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
997 int use_xy = FALSE, use_attr = FALSE;
999 switch (PyTuple_Size(args)) {
1000 case 1:
1001 if (!PyArg_ParseTuple(args,"s;str", &str))
1002 return NULL;
1003 break;
1004 case 2:
1005 if (!PyArg_ParseTuple(args,"sl;str,attr", &str, &attr))
1006 return NULL;
1007 use_attr = TRUE;
1008 break;
1009 case 3:
1010 if (!PyArg_ParseTuple(args,"iis;y,x,str", &y, &x, &str))
1011 return NULL;
1012 use_xy = TRUE;
1013 break;
1014 case 4:
1015 if (!PyArg_ParseTuple(args,"iisl;y,x,str,attr", &y, &x, &str, &attr))
1016 return NULL;
1017 use_xy = use_attr = TRUE;
1018 break;
1019 default:
1020 PyErr_SetString(PyExc_TypeError, "insstr requires 1 to 4 arguments");
1021 return NULL;
1024 if (use_attr == TRUE) {
1025 attr_old = getattrs(self->win);
1026 wattrset(self->win,attr);
1028 if (use_xy == TRUE)
1029 rtn = mvwinsstr(self->win,y,x,str);
1030 else
1031 rtn = winsstr(self->win,str);
1032 if (use_attr == TRUE)
1033 wattrset(self->win,attr_old);
1034 return PyCursesCheckERR(rtn, "insstr");
1037 static PyObject *
1038 PyCursesWindow_InsNStr(PyCursesWindowObject *self, PyObject *args)
1040 int rtn, x, y, n;
1041 char *str;
1042 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
1043 int use_xy = FALSE, use_attr = FALSE;
1045 switch (PyTuple_Size(args)) {
1046 case 2:
1047 if (!PyArg_ParseTuple(args,"si;str,n", &str, &n))
1048 return NULL;
1049 break;
1050 case 3:
1051 if (!PyArg_ParseTuple(args,"sil;str,n,attr", &str, &n, &attr))
1052 return NULL;
1053 use_attr = TRUE;
1054 break;
1055 case 4:
1056 if (!PyArg_ParseTuple(args,"iisi;y,x,str,n", &y, &x, &str, &n))
1057 return NULL;
1058 use_xy = TRUE;
1059 break;
1060 case 5:
1061 if (!PyArg_ParseTuple(args,"iisil;y,x,str,n,attr", &y, &x, &str, &n, &attr))
1062 return NULL;
1063 use_xy = use_attr = TRUE;
1064 break;
1065 default:
1066 PyErr_SetString(PyExc_TypeError, "insnstr requires 2 to 5 arguments");
1067 return NULL;
1070 if (use_attr == TRUE) {
1071 attr_old = getattrs(self->win);
1072 wattrset(self->win,attr);
1074 if (use_xy == TRUE)
1075 rtn = mvwinsnstr(self->win,y,x,str,n);
1076 else
1077 rtn = winsnstr(self->win,str,n);
1078 if (use_attr == TRUE)
1079 wattrset(self->win,attr_old);
1080 return PyCursesCheckERR(rtn, "insnstr");
1083 static PyObject *
1084 PyCursesWindow_Is_LineTouched(PyCursesWindowObject *self, PyObject *args)
1086 int line, erg;
1087 if (!PyArg_ParseTuple(args,"i;line", &line))
1088 return NULL;
1089 erg = is_linetouched(self->win, line);
1090 if (erg == ERR) {
1091 PyErr_SetString(PyExc_TypeError,
1092 "is_linetouched: line number outside of boundaries");
1093 return NULL;
1094 } else
1095 if (erg == FALSE) {
1096 Py_INCREF(Py_False);
1097 return Py_False;
1098 } else {
1099 Py_INCREF(Py_True);
1100 return Py_True;
1104 static PyObject *
1105 PyCursesWindow_NoOutRefresh(PyCursesWindowObject *self, PyObject *args)
1107 int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
1108 int rtn;
1110 #ifndef WINDOW_HAS_FLAGS
1111 if (0) {
1112 #else
1113 if (self->win->_flags & _ISPAD) {
1114 #endif
1115 switch(PyTuple_Size(args)) {
1116 case 6:
1117 if (!PyArg_ParseTuple(args,
1118 "iiiiii;" \
1119 "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
1120 &pminrow, &pmincol, &sminrow,
1121 &smincol, &smaxrow, &smaxcol))
1122 return NULL;
1123 Py_BEGIN_ALLOW_THREADS
1124 rtn = pnoutrefresh(self->win,
1125 pminrow, pmincol, sminrow,
1126 smincol, smaxrow, smaxcol);
1127 Py_END_ALLOW_THREADS
1128 return PyCursesCheckERR(rtn, "pnoutrefresh");
1129 default:
1130 PyErr_SetString(PyCursesError,
1131 "noutrefresh() called for a pad "
1132 "requires 6 arguments");
1133 return NULL;
1135 } else {
1136 if (!PyArg_ParseTuple(args, ":noutrefresh"))
1137 return NULL;
1139 Py_BEGIN_ALLOW_THREADS
1140 rtn = wnoutrefresh(self->win);
1141 Py_END_ALLOW_THREADS
1142 return PyCursesCheckERR(rtn, "wnoutrefresh");
1146 static PyObject *
1147 PyCursesWindow_Overlay(PyCursesWindowObject *self, PyObject *args)
1149 PyCursesWindowObject *temp;
1150 int use_copywin = FALSE;
1151 int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
1152 int rtn;
1154 switch (PyTuple_Size(args)) {
1155 case 1:
1156 if (!PyArg_ParseTuple(args, "O!;window object",
1157 &PyCursesWindow_Type, &temp))
1158 return NULL;
1159 break;
1160 case 7:
1161 if (!PyArg_ParseTuple(args, "(O!iiiiii);window object, int, int, int, int, int, int",
1162 &PyCursesWindow_Type, &temp, &sminrow, &smincol,
1163 &dminrow, &dmincol, &dmaxrow, &dmaxcol))
1164 return NULL;
1165 use_copywin = TRUE;
1166 break;
1167 default:
1168 PyErr_SetString(PyExc_TypeError,
1169 "overlay requires one or seven arguments");
1170 return NULL;
1173 if (use_copywin == TRUE) {
1174 rtn = copywin(self->win, temp->win, sminrow, smincol,
1175 dminrow, dmincol, dmaxrow, dmaxcol, TRUE);
1176 return PyCursesCheckERR(rtn, "copywin");
1178 else {
1179 rtn = overlay(self->win, temp->win);
1180 return PyCursesCheckERR(rtn, "overlay");
1184 static PyObject *
1185 PyCursesWindow_Overwrite(PyCursesWindowObject *self, PyObject *args)
1187 PyCursesWindowObject *temp;
1188 int use_copywin = FALSE;
1189 int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
1190 int rtn;
1192 switch (PyTuple_Size(args)) {
1193 case 1:
1194 if (!PyArg_ParseTuple(args, "O!;window object",
1195 &PyCursesWindow_Type, &temp))
1196 return NULL;
1197 break;
1198 case 7:
1199 if (!PyArg_ParseTuple(args, "(O!iiiiii);window object, int, int, int, int, int, int",
1200 &PyCursesWindow_Type, &temp, &sminrow, &smincol,
1201 &dminrow, &dmincol, &dmaxrow, &dmaxcol))
1202 return NULL;
1203 use_copywin = TRUE;
1204 break;
1205 default:
1206 PyErr_SetString(PyExc_TypeError,
1207 "overwrite requires one or seven arguments");
1208 return NULL;
1211 if (use_copywin == TRUE) {
1212 rtn = copywin(self->win, temp->win, sminrow, smincol,
1213 dminrow, dmincol, dmaxrow, dmaxcol, FALSE);
1214 return PyCursesCheckERR(rtn, "copywin");
1216 else {
1217 rtn = overwrite(self->win, temp->win);
1218 return PyCursesCheckERR(rtn, "overwrite");
1222 static PyObject *
1223 PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *args)
1225 PyObject *temp;
1227 if (!PyArg_ParseTuple(args, "O;fileobj", &temp))
1228 return NULL;
1229 if (!PyFile_Check(temp)) {
1230 PyErr_SetString(PyExc_TypeError, "argument must be a file object");
1231 return NULL;
1233 return PyCursesCheckERR(putwin(self->win, PyFile_AsFile(temp)),
1234 "putwin");
1237 static PyObject *
1238 PyCursesWindow_RedrawLine(PyCursesWindowObject *self, PyObject *args)
1240 int beg, num;
1241 if (!PyArg_ParseTuple(args,"ii;beg,num", &beg, &num))
1242 return NULL;
1243 return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln");
1246 static PyObject *
1247 PyCursesWindow_Refresh(PyCursesWindowObject *self, PyObject *args)
1249 int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
1250 int rtn;
1252 #ifndef WINDOW_HAS_FLAGS
1253 if (0) {
1254 #else
1255 if (self->win->_flags & _ISPAD) {
1256 #endif
1257 switch(PyTuple_Size(args)) {
1258 case 6:
1259 if (!PyArg_ParseTuple(args,
1260 "iiiiii;" \
1261 "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
1262 &pminrow, &pmincol, &sminrow,
1263 &smincol, &smaxrow, &smaxcol))
1264 return NULL;
1266 Py_BEGIN_ALLOW_THREADS
1267 rtn = prefresh(self->win,
1268 pminrow, pmincol, sminrow,
1269 smincol, smaxrow, smaxcol);
1270 Py_END_ALLOW_THREADS
1271 return PyCursesCheckERR(rtn, "prefresh");
1272 default:
1273 PyErr_SetString(PyCursesError,
1274 "refresh() for a pad requires 6 arguments");
1275 return NULL;
1277 } else {
1278 if (!PyArg_ParseTuple(args, ":refresh"))
1279 return NULL;
1280 Py_BEGIN_ALLOW_THREADS
1281 rtn = wrefresh(self->win);
1282 Py_END_ALLOW_THREADS
1283 return PyCursesCheckERR(rtn, "prefresh");
1287 static PyObject *
1288 PyCursesWindow_SetScrollRegion(PyCursesWindowObject *self, PyObject *args)
1290 int x, y;
1291 if (!PyArg_ParseTuple(args,"ii;top, bottom",&y,&x))
1292 return NULL;
1293 return PyCursesCheckERR(wsetscrreg(self->win,y,x), "wsetscrreg");
1296 static PyObject *
1297 PyCursesWindow_SubWin(PyCursesWindowObject *self, PyObject *args)
1299 WINDOW *win;
1300 int nlines, ncols, begin_y, begin_x;
1302 nlines = 0;
1303 ncols = 0;
1304 switch (PyTuple_Size(args)) {
1305 case 2:
1306 if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
1307 return NULL;
1308 break;
1309 case 4:
1310 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
1311 &nlines,&ncols,&begin_y,&begin_x))
1312 return NULL;
1313 break;
1314 default:
1315 PyErr_SetString(PyExc_TypeError, "subwin requires 2 or 4 arguments");
1316 return NULL;
1319 /* printf("Subwin: %i %i %i %i \n", nlines, ncols, begin_y, begin_x); */
1320 #ifdef WINDOW_HAS_FLAGS
1321 if (self->win->_flags & _ISPAD)
1322 win = subpad(self->win, nlines, ncols, begin_y, begin_x);
1323 else
1324 #endif
1325 win = subwin(self->win, nlines, ncols, begin_y, begin_x);
1327 if (win == NULL) {
1328 PyErr_SetString(PyCursesError, catchall_NULL);
1329 return NULL;
1332 return (PyObject *)PyCursesWindow_New(win);
1335 static PyObject *
1336 PyCursesWindow_Scroll(PyCursesWindowObject *self, PyObject *args)
1338 int nlines;
1339 switch(PyTuple_Size(args)) {
1340 case 0:
1341 return PyCursesCheckERR(scroll(self->win), "scroll");
1342 case 1:
1343 if (!PyArg_ParseTuple(args, "i;nlines", &nlines))
1344 return NULL;
1345 return PyCursesCheckERR(wscrl(self->win, nlines), "scroll");
1346 default:
1347 PyErr_SetString(PyExc_TypeError, "scroll requires 0 or 1 arguments");
1348 return NULL;
1352 static PyObject *
1353 PyCursesWindow_TouchLine(PyCursesWindowObject *self, PyObject *args)
1355 int st, cnt, val;
1356 switch (PyTuple_Size(args)) {
1357 case 2:
1358 if (!PyArg_ParseTuple(args,"ii;start,count",&st,&cnt))
1359 return NULL;
1360 return PyCursesCheckERR(touchline(self->win,st,cnt), "touchline");
1361 case 3:
1362 if (!PyArg_ParseTuple(args, "iii;start,count,val", &st, &cnt, &val))
1363 return NULL;
1364 return PyCursesCheckERR(wtouchln(self->win, st, cnt, val), "touchline");
1365 default:
1366 PyErr_SetString(PyExc_TypeError, "touchline requires 2 or 3 arguments");
1367 return NULL;
1371 static PyObject *
1372 PyCursesWindow_Vline(PyCursesWindowObject *self, PyObject *args)
1374 PyObject *temp;
1375 chtype ch;
1376 int n, x, y, code = OK;
1377 attr_t attr = A_NORMAL;
1379 switch (PyTuple_Size(args)) {
1380 case 2:
1381 if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
1382 return NULL;
1383 break;
1384 case 3:
1385 if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &attr))
1386 return NULL;
1387 break;
1388 case 4:
1389 if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
1390 return NULL;
1391 code = wmove(self->win, y, x);
1392 break;
1393 case 5:
1394 if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
1395 &y, &x, &temp, &n, &attr))
1396 return NULL;
1397 code = wmove(self->win, y, x);
1398 break;
1399 default:
1400 PyErr_SetString(PyExc_TypeError, "vline requires 2 to 5 arguments");
1401 return NULL;
1404 if (code != ERR) {
1405 if (!PyCurses_ConvertToChtype(temp, &ch)) {
1406 PyErr_SetString(PyExc_TypeError,
1407 "argument 1 or 3 must be a ch or an int");
1408 return NULL;
1410 return PyCursesCheckERR(wvline(self->win, ch | attr, n), "vline");
1411 } else
1412 return PyCursesCheckERR(code, "wmove");
1415 static PyMethodDef PyCursesWindow_Methods[] = {
1416 {"addch", (PyCFunction)PyCursesWindow_AddCh, METH_VARARGS},
1417 {"addnstr", (PyCFunction)PyCursesWindow_AddNStr, METH_VARARGS},
1418 {"addstr", (PyCFunction)PyCursesWindow_AddStr, METH_VARARGS},
1419 {"attroff", (PyCFunction)PyCursesWindow_wattroff, METH_VARARGS},
1420 {"attron", (PyCFunction)PyCursesWindow_wattron, METH_VARARGS},
1421 {"attrset", (PyCFunction)PyCursesWindow_wattrset, METH_VARARGS},
1422 {"bkgd", (PyCFunction)PyCursesWindow_Bkgd, METH_VARARGS},
1423 {"bkgdset", (PyCFunction)PyCursesWindow_BkgdSet, METH_VARARGS},
1424 {"border", (PyCFunction)PyCursesWindow_Border, METH_VARARGS},
1425 {"box", (PyCFunction)PyCursesWindow_Box, METH_VARARGS},
1426 {"clear", (PyCFunction)PyCursesWindow_wclear, METH_NOARGS},
1427 {"clearok", (PyCFunction)PyCursesWindow_clearok, METH_VARARGS},
1428 {"clrtobot", (PyCFunction)PyCursesWindow_wclrtobot, METH_NOARGS},
1429 {"clrtoeol", (PyCFunction)PyCursesWindow_wclrtoeol, METH_NOARGS},
1430 {"cursyncup", (PyCFunction)PyCursesWindow_wcursyncup, METH_NOARGS},
1431 {"delch", (PyCFunction)PyCursesWindow_DelCh, METH_VARARGS},
1432 {"deleteln", (PyCFunction)PyCursesWindow_wdeleteln, METH_NOARGS},
1433 {"derwin", (PyCFunction)PyCursesWindow_DerWin, METH_VARARGS},
1434 {"echochar", (PyCFunction)PyCursesWindow_EchoChar, METH_VARARGS},
1435 #ifdef NCURSES_MOUSE_VERSION
1436 {"enclose", (PyCFunction)PyCursesWindow_Enclose, METH_VARARGS},
1437 #endif
1438 {"erase", (PyCFunction)PyCursesWindow_werase, METH_NOARGS},
1439 {"getbegyx", (PyCFunction)PyCursesWindow_getbegyx, METH_NOARGS},
1440 {"getbkgd", (PyCFunction)PyCursesWindow_GetBkgd, METH_NOARGS},
1441 {"getch", (PyCFunction)PyCursesWindow_GetCh, METH_VARARGS},
1442 {"getkey", (PyCFunction)PyCursesWindow_GetKey, METH_VARARGS},
1443 {"getmaxyx", (PyCFunction)PyCursesWindow_getmaxyx, METH_NOARGS},
1444 {"getparyx", (PyCFunction)PyCursesWindow_getparyx, METH_NOARGS},
1445 {"getstr", (PyCFunction)PyCursesWindow_GetStr, METH_VARARGS},
1446 {"getyx", (PyCFunction)PyCursesWindow_getyx, METH_NOARGS},
1447 {"hline", (PyCFunction)PyCursesWindow_Hline, METH_VARARGS},
1448 {"idcok", (PyCFunction)PyCursesWindow_idcok, METH_VARARGS},
1449 {"idlok", (PyCFunction)PyCursesWindow_idlok, METH_VARARGS},
1450 {"immedok", (PyCFunction)PyCursesWindow_immedok, METH_VARARGS},
1451 {"inch", (PyCFunction)PyCursesWindow_InCh, METH_VARARGS},
1452 {"insch", (PyCFunction)PyCursesWindow_InsCh, METH_VARARGS},
1453 {"insdelln", (PyCFunction)PyCursesWindow_winsdelln, METH_VARARGS},
1454 {"insertln", (PyCFunction)PyCursesWindow_winsertln, METH_NOARGS},
1455 {"insnstr", (PyCFunction)PyCursesWindow_InsNStr, METH_VARARGS},
1456 {"insstr", (PyCFunction)PyCursesWindow_InsStr, METH_VARARGS},
1457 {"instr", (PyCFunction)PyCursesWindow_InStr, METH_VARARGS},
1458 {"is_linetouched", (PyCFunction)PyCursesWindow_Is_LineTouched, METH_VARARGS},
1459 {"is_wintouched", (PyCFunction)PyCursesWindow_is_wintouched, METH_NOARGS},
1460 {"keypad", (PyCFunction)PyCursesWindow_keypad, METH_VARARGS},
1461 {"leaveok", (PyCFunction)PyCursesWindow_leaveok, METH_VARARGS},
1462 {"move", (PyCFunction)PyCursesWindow_wmove, METH_VARARGS},
1463 {"mvderwin", (PyCFunction)PyCursesWindow_mvderwin, METH_VARARGS},
1464 {"mvwin", (PyCFunction)PyCursesWindow_mvwin, METH_VARARGS},
1465 {"nodelay", (PyCFunction)PyCursesWindow_nodelay, METH_VARARGS},
1466 {"notimeout", (PyCFunction)PyCursesWindow_notimeout, METH_VARARGS},
1467 {"noutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
1468 /* Backward compatibility alias -- remove in Python 2.3 */
1469 {"nooutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
1470 {"overlay", (PyCFunction)PyCursesWindow_Overlay, METH_VARARGS},
1471 {"overwrite", (PyCFunction)PyCursesWindow_Overwrite,
1472 METH_VARARGS},
1473 {"putwin", (PyCFunction)PyCursesWindow_PutWin, METH_VARARGS},
1474 {"redrawln", (PyCFunction)PyCursesWindow_RedrawLine},
1475 {"redrawwin", (PyCFunction)PyCursesWindow_redrawwin, METH_NOARGS},
1476 {"refresh", (PyCFunction)PyCursesWindow_Refresh, METH_VARARGS},
1477 #ifndef STRICT_SYSV_CURSES
1478 {"resize", (PyCFunction)PyCursesWindow_wresize, METH_VARARGS},
1479 #endif
1480 {"scroll", (PyCFunction)PyCursesWindow_Scroll, METH_VARARGS},
1481 {"scrollok", (PyCFunction)PyCursesWindow_scrollok, METH_VARARGS},
1482 {"setscrreg", (PyCFunction)PyCursesWindow_SetScrollRegion, METH_VARARGS},
1483 {"standend", (PyCFunction)PyCursesWindow_wstandend, METH_NOARGS},
1484 {"standout", (PyCFunction)PyCursesWindow_wstandout, METH_NOARGS},
1485 {"subpad", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
1486 {"subwin", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
1487 {"syncdown", (PyCFunction)PyCursesWindow_wsyncdown, METH_NOARGS},
1488 {"syncok", (PyCFunction)PyCursesWindow_syncok, METH_VARARGS},
1489 {"syncup", (PyCFunction)PyCursesWindow_wsyncup, METH_NOARGS},
1490 {"timeout", (PyCFunction)PyCursesWindow_wtimeout, METH_VARARGS},
1491 {"touchline", (PyCFunction)PyCursesWindow_TouchLine, METH_VARARGS},
1492 {"touchwin", (PyCFunction)PyCursesWindow_touchwin, METH_NOARGS},
1493 {"untouchwin", (PyCFunction)PyCursesWindow_untouchwin, METH_NOARGS},
1494 {"vline", (PyCFunction)PyCursesWindow_Vline, METH_VARARGS},
1495 {NULL, NULL} /* sentinel */
1498 static PyObject *
1499 PyCursesWindow_GetAttr(PyCursesWindowObject *self, char *name)
1501 return Py_FindMethod(PyCursesWindow_Methods, (PyObject *)self, name);
1504 /* -------------------------------------------------------*/
1506 PyTypeObject PyCursesWindow_Type = {
1507 PyObject_HEAD_INIT(NULL)
1508 0, /*ob_size*/
1509 "_curses.curses window", /*tp_name*/
1510 sizeof(PyCursesWindowObject), /*tp_basicsize*/
1511 0, /*tp_itemsize*/
1512 /* methods */
1513 (destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/
1514 0, /*tp_print*/
1515 (getattrfunc)PyCursesWindow_GetAttr, /*tp_getattr*/
1516 (setattrfunc)0, /*tp_setattr*/
1517 0, /*tp_compare*/
1518 0, /*tp_repr*/
1519 0, /*tp_as_number*/
1520 0, /*tp_as_sequence*/
1521 0, /*tp_as_mapping*/
1522 0, /*tp_hash*/
1525 /*********************************************************************
1526 Global Functions
1527 **********************************************************************/
1529 NoArgNoReturnFunction(beep)
1530 NoArgNoReturnFunction(def_prog_mode)
1531 NoArgNoReturnFunction(def_shell_mode)
1532 NoArgNoReturnFunction(doupdate)
1533 NoArgNoReturnFunction(endwin)
1534 NoArgNoReturnFunction(flash)
1535 NoArgNoReturnFunction(nocbreak)
1536 NoArgNoReturnFunction(noecho)
1537 NoArgNoReturnFunction(nonl)
1538 NoArgNoReturnFunction(noraw)
1539 NoArgNoReturnFunction(reset_prog_mode)
1540 NoArgNoReturnFunction(reset_shell_mode)
1541 NoArgNoReturnFunction(resetty)
1542 NoArgNoReturnFunction(savetty)
1544 NoArgOrFlagNoReturnFunction(cbreak)
1545 NoArgOrFlagNoReturnFunction(echo)
1546 NoArgOrFlagNoReturnFunction(nl)
1547 NoArgOrFlagNoReturnFunction(raw)
1549 NoArgReturnIntFunction(baudrate)
1550 NoArgReturnIntFunction(termattrs)
1552 NoArgReturnStringFunction(termname)
1553 NoArgReturnStringFunction(longname)
1555 NoArgTrueFalseFunction(can_change_color)
1556 NoArgTrueFalseFunction(has_colors)
1557 NoArgTrueFalseFunction(has_ic)
1558 NoArgTrueFalseFunction(has_il)
1559 NoArgTrueFalseFunction(isendwin)
1560 NoArgNoReturnVoidFunction(filter)
1561 NoArgNoReturnVoidFunction(flushinp)
1562 NoArgNoReturnVoidFunction(noqiflush)
1564 static PyObject *
1565 PyCurses_Color_Content(PyObject *self, PyObject *args)
1567 short color,r,g,b;
1569 PyCursesInitialised
1570 PyCursesInitialisedColor
1572 if (!PyArg_ParseTuple(args, "h:color_content", &color)) return NULL;
1574 if (color_content(color, &r, &g, &b) != ERR)
1575 return Py_BuildValue("(iii)", r, g, b);
1576 else {
1577 PyErr_SetString(PyCursesError,
1578 "Argument 1 was out of range. Check value of COLORS.");
1579 return NULL;
1583 static PyObject *
1584 PyCurses_color_pair(PyObject *self, PyObject *args)
1586 int n;
1588 PyCursesInitialised
1589 PyCursesInitialisedColor
1591 if (!PyArg_ParseTuple(args, "i:color_pair", &n)) return NULL;
1592 return PyInt_FromLong((long) (n << 8));
1595 static PyObject *
1596 PyCurses_Curs_Set(PyObject *self, PyObject *args)
1598 int vis,erg;
1600 PyCursesInitialised
1602 if (!PyArg_ParseTuple(args, "i:curs_set", &vis)) return NULL;
1604 erg = curs_set(vis);
1605 if (erg == ERR) return PyCursesCheckERR(erg, "curs_set");
1607 return PyInt_FromLong((long) erg);
1610 static PyObject *
1611 PyCurses_Delay_Output(PyObject *self, PyObject *args)
1613 int ms;
1615 PyCursesInitialised
1617 if (!PyArg_ParseTuple(args, "i:delay_output", &ms)) return NULL;
1619 return PyCursesCheckERR(delay_output(ms), "delay_output");
1622 static PyObject *
1623 PyCurses_EraseChar(PyObject *self)
1625 char ch;
1627 PyCursesInitialised
1629 ch = erasechar();
1631 return PyString_FromStringAndSize(&ch, 1);
1634 static PyObject *
1635 PyCurses_getsyx(PyObject *self)
1637 int x,y;
1639 PyCursesInitialised
1641 getsyx(y, x);
1643 return Py_BuildValue("(ii)", y, x);
1646 #ifdef NCURSES_MOUSE_VERSION
1647 static PyObject *
1648 PyCurses_GetMouse(PyObject *self)
1650 int rtn;
1651 MEVENT event;
1653 PyCursesInitialised
1655 rtn = getmouse( &event );
1656 if (rtn == ERR) {
1657 PyErr_SetString(PyCursesError, "getmouse() returned ERR");
1658 return NULL;
1660 return Py_BuildValue("(hiiil)",
1661 (short)event.id,
1662 event.x, event.y, event.z,
1663 (long) event.bstate);
1666 static PyObject *
1667 PyCurses_UngetMouse(PyObject *self, PyObject *args)
1669 MEVENT event;
1671 PyCursesInitialised
1672 if (!PyArg_ParseTuple(args, "(hiiil)",
1673 &event.id,
1674 &event.x, &event.y, &event.z,
1675 (int *) &event.bstate))
1676 return NULL;
1678 return PyCursesCheckERR(ungetmouse(&event), "ungetmouse");
1680 #endif
1682 static PyObject *
1683 PyCurses_GetWin(PyCursesWindowObject *self, PyObject *temp)
1685 WINDOW *win;
1687 PyCursesInitialised
1689 if (!PyFile_Check(temp)) {
1690 PyErr_SetString(PyExc_TypeError, "argument must be a file object");
1691 return NULL;
1694 win = getwin(PyFile_AsFile(temp));
1696 if (win == NULL) {
1697 PyErr_SetString(PyCursesError, catchall_NULL);
1698 return NULL;
1701 return PyCursesWindow_New(win);
1704 static PyObject *
1705 PyCurses_HalfDelay(PyObject *self, PyObject *args)
1707 unsigned char tenths;
1709 PyCursesInitialised
1711 if (!PyArg_ParseTuple(args, "b:halfdelay", &tenths)) return NULL;
1713 return PyCursesCheckERR(halfdelay(tenths), "halfdelay");
1716 #ifndef STRICT_SYSV_CURSES
1717 /* No has_key! */
1718 static PyObject * PyCurses_has_key(PyObject *self, PyObject *args)
1720 int ch;
1722 PyCursesInitialised
1724 if (!PyArg_ParseTuple(args,"i",&ch)) return NULL;
1726 if (has_key(ch) == FALSE) {
1727 Py_INCREF(Py_False);
1728 return Py_False;
1730 Py_INCREF(Py_True);
1731 return Py_True;
1733 #endif /* STRICT_SYSV_CURSES */
1735 static PyObject *
1736 PyCurses_Init_Color(PyObject *self, PyObject *args)
1738 short color, r, g, b;
1740 PyCursesInitialised
1741 PyCursesInitialisedColor
1743 switch(PyTuple_Size(args)) {
1744 case 4:
1745 if (!PyArg_ParseTuple(args, "hhhh;color,r,g,b", &color, &r, &g, &b)) return NULL;
1746 break;
1747 default:
1748 PyErr_SetString(PyExc_TypeError, "init_color requires 4 arguments");
1749 return NULL;
1752 return PyCursesCheckERR(init_color(color, r, g, b), "init_color");
1755 static PyObject *
1756 PyCurses_Init_Pair(PyObject *self, PyObject *args)
1758 short pair, f, b;
1760 PyCursesInitialised
1761 PyCursesInitialisedColor
1763 if (PyTuple_Size(args) != 3) {
1764 PyErr_SetString(PyExc_TypeError, "init_pair requires 3 arguments");
1765 return NULL;
1768 if (!PyArg_ParseTuple(args, "hhh;pair, f, b", &pair, &f, &b)) return NULL;
1770 return PyCursesCheckERR(init_pair(pair, f, b), "init_pair");
1773 static PyObject *ModDict;
1775 static PyObject *
1776 PyCurses_InitScr(PyObject *self)
1778 WINDOW *win;
1779 PyObject *nlines, *cols;
1781 if (initialised == TRUE) {
1782 wrefresh(stdscr);
1783 return (PyObject *)PyCursesWindow_New(stdscr);
1786 win = initscr();
1788 if (win == NULL) {
1789 PyErr_SetString(PyCursesError, catchall_NULL);
1790 return NULL;
1793 initialised = initialised_setupterm = TRUE;
1795 /* This was moved from initcurses() because it core dumped on SGI,
1796 where they're not defined until you've called initscr() */
1797 #define SetDictInt(string,ch) \
1798 PyDict_SetItemString(ModDict,string,PyInt_FromLong((long) (ch)));
1800 /* Here are some graphic symbols you can use */
1801 SetDictInt("ACS_ULCORNER", (ACS_ULCORNER));
1802 SetDictInt("ACS_LLCORNER", (ACS_LLCORNER));
1803 SetDictInt("ACS_URCORNER", (ACS_URCORNER));
1804 SetDictInt("ACS_LRCORNER", (ACS_LRCORNER));
1805 SetDictInt("ACS_LTEE", (ACS_LTEE));
1806 SetDictInt("ACS_RTEE", (ACS_RTEE));
1807 SetDictInt("ACS_BTEE", (ACS_BTEE));
1808 SetDictInt("ACS_TTEE", (ACS_TTEE));
1809 SetDictInt("ACS_HLINE", (ACS_HLINE));
1810 SetDictInt("ACS_VLINE", (ACS_VLINE));
1811 SetDictInt("ACS_PLUS", (ACS_PLUS));
1812 #if !defined(__hpux) || defined(HAVE_NCURSES_H)
1813 /* On HP/UX 11, these are of type cchar_t, which is not an
1814 integral type. If this is a problem on more platforms, a
1815 configure test should be added to determine whether ACS_S1
1816 is of integral type. */
1817 SetDictInt("ACS_S1", (ACS_S1));
1818 SetDictInt("ACS_S9", (ACS_S9));
1819 SetDictInt("ACS_DIAMOND", (ACS_DIAMOND));
1820 SetDictInt("ACS_CKBOARD", (ACS_CKBOARD));
1821 SetDictInt("ACS_DEGREE", (ACS_DEGREE));
1822 SetDictInt("ACS_PLMINUS", (ACS_PLMINUS));
1823 SetDictInt("ACS_BULLET", (ACS_BULLET));
1824 SetDictInt("ACS_LARROW", (ACS_LARROW));
1825 SetDictInt("ACS_RARROW", (ACS_RARROW));
1826 SetDictInt("ACS_DARROW", (ACS_DARROW));
1827 SetDictInt("ACS_UARROW", (ACS_UARROW));
1828 SetDictInt("ACS_BOARD", (ACS_BOARD));
1829 SetDictInt("ACS_LANTERN", (ACS_LANTERN));
1830 SetDictInt("ACS_BLOCK", (ACS_BLOCK));
1831 #endif
1832 SetDictInt("ACS_BSSB", (ACS_ULCORNER));
1833 SetDictInt("ACS_SSBB", (ACS_LLCORNER));
1834 SetDictInt("ACS_BBSS", (ACS_URCORNER));
1835 SetDictInt("ACS_SBBS", (ACS_LRCORNER));
1836 SetDictInt("ACS_SBSS", (ACS_RTEE));
1837 SetDictInt("ACS_SSSB", (ACS_LTEE));
1838 SetDictInt("ACS_SSBS", (ACS_BTEE));
1839 SetDictInt("ACS_BSSS", (ACS_TTEE));
1840 SetDictInt("ACS_BSBS", (ACS_HLINE));
1841 SetDictInt("ACS_SBSB", (ACS_VLINE));
1842 SetDictInt("ACS_SSSS", (ACS_PLUS));
1844 /* The following are never available with strict SYSV curses */
1845 #ifdef ACS_S3
1846 SetDictInt("ACS_S3", (ACS_S3));
1847 #endif
1848 #ifdef ACS_S7
1849 SetDictInt("ACS_S7", (ACS_S7));
1850 #endif
1851 #ifdef ACS_LEQUAL
1852 SetDictInt("ACS_LEQUAL", (ACS_LEQUAL));
1853 #endif
1854 #ifdef ACS_GEQUAL
1855 SetDictInt("ACS_GEQUAL", (ACS_GEQUAL));
1856 #endif
1857 #ifdef ACS_PI
1858 SetDictInt("ACS_PI", (ACS_PI));
1859 #endif
1860 #ifdef ACS_NEQUAL
1861 SetDictInt("ACS_NEQUAL", (ACS_NEQUAL));
1862 #endif
1863 #ifdef ACS_STERLING
1864 SetDictInt("ACS_STERLING", (ACS_STERLING));
1865 #endif
1867 nlines = PyInt_FromLong((long) LINES);
1868 PyDict_SetItemString(ModDict, "LINES", nlines);
1869 Py_DECREF(nlines);
1870 cols = PyInt_FromLong((long) COLS);
1871 PyDict_SetItemString(ModDict, "COLS", cols);
1872 Py_DECREF(cols);
1874 return (PyObject *)PyCursesWindow_New(win);
1877 static PyObject *
1878 PyCurses_setupterm(PyObject* self, PyObject *args, PyObject* keywds)
1880 int fd = -1;
1881 int err;
1882 char* termstr = NULL;
1884 static char *kwlist[] = {"term", "fd", NULL};
1886 if (!PyArg_ParseTupleAndKeywords(
1887 args,keywds,"|zi:setupterm",kwlist,&termstr,&fd)) {
1888 return NULL;
1891 if (fd == -1) {
1892 PyObject* sys_stdout;
1894 sys_stdout = PySys_GetObject("stdout");
1896 if (sys_stdout == NULL) {
1897 PyErr_SetString(
1898 PyCursesError,
1899 "lost sys.stdout");
1900 return NULL;
1903 fd = PyObject_AsFileDescriptor(sys_stdout);
1905 if (fd == -1) {
1906 return NULL;
1910 if (setupterm(termstr,fd,&err) == ERR) {
1911 char* s = "setupterm: unknown error";
1913 if (err == 0) {
1914 s = "setupterm: could not find terminal";
1915 } else if (err == -1) {
1916 s = "setupterm: could not find terminfo database";
1919 PyErr_SetString(PyCursesError,s);
1920 return NULL;
1923 initialised_setupterm = TRUE;
1925 Py_INCREF(Py_None);
1926 return Py_None;
1929 static PyObject *
1930 PyCurses_IntrFlush(PyObject *self, PyObject *args)
1932 int ch;
1934 PyCursesInitialised
1936 switch(PyTuple_Size(args)) {
1937 case 1:
1938 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL;
1939 break;
1940 default:
1941 PyErr_SetString(PyExc_TypeError, "intrflush requires 1 argument");
1942 return NULL;
1945 return PyCursesCheckERR(intrflush(NULL,ch), "intrflush");
1948 #if !defined(__NetBSD__)
1949 static PyObject *
1950 PyCurses_KeyName(PyObject *self, PyObject *args)
1952 const char *knp;
1953 int ch;
1955 PyCursesInitialised
1957 if (!PyArg_ParseTuple(args,"i",&ch)) return NULL;
1959 knp = keyname(ch);
1961 return PyString_FromString((knp == NULL) ? "" : (char *)knp);
1963 #endif
1965 static PyObject *
1966 PyCurses_KillChar(PyObject *self)
1968 char ch;
1970 ch = killchar();
1972 return PyString_FromStringAndSize(&ch, 1);
1975 static PyObject *
1976 PyCurses_Meta(PyObject *self, PyObject *args)
1978 int ch;
1980 PyCursesInitialised
1982 switch(PyTuple_Size(args)) {
1983 case 1:
1984 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL;
1985 break;
1986 default:
1987 PyErr_SetString(PyExc_TypeError, "meta requires 1 argument");
1988 return NULL;
1991 return PyCursesCheckERR(meta(stdscr, ch), "meta");
1994 #ifdef NCURSES_MOUSE_VERSION
1995 static PyObject *
1996 PyCurses_MouseInterval(PyObject *self, PyObject *args)
1998 int interval;
1999 PyCursesInitialised
2001 if (!PyArg_ParseTuple(args,"i;interval",&interval))
2002 return NULL;
2003 return PyCursesCheckERR(mouseinterval(interval), "mouseinterval");
2006 static PyObject *
2007 PyCurses_MouseMask(PyObject *self, PyObject *args)
2009 int newmask;
2010 mmask_t oldmask, availmask;
2012 PyCursesInitialised
2013 if (!PyArg_ParseTuple(args,"i;mousemask",&newmask))
2014 return NULL;
2015 availmask = mousemask(newmask, &oldmask);
2016 return Py_BuildValue("(ll)", (long)availmask, (long)oldmask);
2018 #endif
2020 static PyObject *
2021 PyCurses_Napms(PyObject *self, PyObject *args)
2023 int ms;
2025 PyCursesInitialised
2026 if (!PyArg_ParseTuple(args, "i;ms", &ms)) return NULL;
2028 return Py_BuildValue("i", napms(ms));
2032 static PyObject *
2033 PyCurses_NewPad(PyObject *self, PyObject *args)
2035 WINDOW *win;
2036 int nlines, ncols;
2038 PyCursesInitialised
2040 if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols)) return NULL;
2042 win = newpad(nlines, ncols);
2044 if (win == NULL) {
2045 PyErr_SetString(PyCursesError, catchall_NULL);
2046 return NULL;
2049 return (PyObject *)PyCursesWindow_New(win);
2052 static PyObject *
2053 PyCurses_NewWindow(PyObject *self, PyObject *args)
2055 WINDOW *win;
2056 int nlines, ncols, begin_y=0, begin_x=0;
2058 PyCursesInitialised
2060 switch (PyTuple_Size(args)) {
2061 case 2:
2062 if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols))
2063 return NULL;
2064 break;
2065 case 4:
2066 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
2067 &nlines,&ncols,&begin_y,&begin_x))
2068 return NULL;
2069 break;
2070 default:
2071 PyErr_SetString(PyExc_TypeError, "newwin requires 2 or 4 arguments");
2072 return NULL;
2075 win = newwin(nlines,ncols,begin_y,begin_x);
2076 if (win == NULL) {
2077 PyErr_SetString(PyCursesError, catchall_NULL);
2078 return NULL;
2081 return (PyObject *)PyCursesWindow_New(win);
2084 static PyObject *
2085 PyCurses_Pair_Content(PyObject *self, PyObject *args)
2087 short pair,f,b;
2089 PyCursesInitialised
2090 PyCursesInitialisedColor
2092 switch(PyTuple_Size(args)) {
2093 case 1:
2094 if (!PyArg_ParseTuple(args, "h;pair", &pair)) return NULL;
2095 break;
2096 default:
2097 PyErr_SetString(PyExc_TypeError, "pair_content requires 1 argument");
2098 return NULL;
2101 if (!pair_content(pair, &f, &b)) {
2102 PyErr_SetString(PyCursesError,
2103 "Argument 1 was out of range. (1..COLOR_PAIRS-1)");
2104 return NULL;
2107 return Py_BuildValue("(ii)", f, b);
2110 static PyObject *
2111 PyCurses_pair_number(PyObject *self, PyObject *args)
2113 int n;
2115 PyCursesInitialised
2116 PyCursesInitialisedColor
2118 switch(PyTuple_Size(args)) {
2119 case 1:
2120 if (!PyArg_ParseTuple(args, "i;pairvalue", &n)) return NULL;
2121 break;
2122 default:
2123 PyErr_SetString(PyExc_TypeError,
2124 "pair_number requires 1 argument");
2125 return NULL;
2128 return PyInt_FromLong((long) ((n & A_COLOR) >> 8));
2131 static PyObject *
2132 PyCurses_Putp(PyObject *self, PyObject *args)
2134 char *str;
2136 if (!PyArg_ParseTuple(args,"s;str", &str)) return NULL;
2137 return PyCursesCheckERR(putp(str), "putp");
2140 static PyObject *
2141 PyCurses_QiFlush(PyObject *self, PyObject *args)
2143 int flag = 0;
2145 PyCursesInitialised
2147 switch(PyTuple_Size(args)) {
2148 case 0:
2149 qiflush();
2150 Py_INCREF(Py_None);
2151 return Py_None;
2152 case 1:
2153 if (!PyArg_ParseTuple(args, "i;True(1) or False(0)", &flag)) return NULL;
2154 if (flag) qiflush();
2155 else noqiflush();
2156 Py_INCREF(Py_None);
2157 return Py_None;
2158 default:
2159 PyErr_SetString(PyExc_TypeError, "qiflush requires 0 or 1 arguments");
2160 return NULL;
2164 static PyObject *
2165 PyCurses_setsyx(PyObject *self, PyObject *args)
2167 int y,x;
2169 PyCursesInitialised
2171 if (PyTuple_Size(args)!=2) {
2172 PyErr_SetString(PyExc_TypeError, "setsyx requires 2 arguments");
2173 return NULL;
2176 if (!PyArg_ParseTuple(args, "ii;y, x", &y, &x)) return NULL;
2178 setsyx(y,x);
2180 Py_INCREF(Py_None);
2181 return Py_None;
2184 static PyObject *
2185 PyCurses_Start_Color(PyObject *self)
2187 int code;
2188 PyObject *c, *cp;
2190 PyCursesInitialised
2192 code = start_color();
2193 if (code != ERR) {
2194 initialisedcolors = TRUE;
2195 c = PyInt_FromLong((long) COLORS);
2196 PyDict_SetItemString(ModDict, "COLORS", c);
2197 Py_DECREF(c);
2198 cp = PyInt_FromLong((long) COLOR_PAIRS);
2199 PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp);
2200 Py_DECREF(cp);
2201 Py_INCREF(Py_None);
2202 return Py_None;
2203 } else {
2204 PyErr_SetString(PyCursesError, "start_color() returned ERR");
2205 return NULL;
2209 static PyObject *
2210 PyCurses_tigetflag(PyObject *self, PyObject *args)
2212 char *capname;
2214 PyCursesSetupTermCalled;
2216 if (!PyArg_ParseTuple(args, "z", &capname))
2217 return NULL;
2219 return PyInt_FromLong( (long) tigetflag( capname ) );
2222 static PyObject *
2223 PyCurses_tigetnum(PyObject *self, PyObject *args)
2225 char *capname;
2227 PyCursesSetupTermCalled;
2229 if (!PyArg_ParseTuple(args, "z", &capname))
2230 return NULL;
2232 return PyInt_FromLong( (long) tigetnum( capname ) );
2235 static PyObject *
2236 PyCurses_tigetstr(PyObject *self, PyObject *args)
2238 char *capname;
2240 PyCursesSetupTermCalled;
2242 if (!PyArg_ParseTuple(args, "z", &capname))
2243 return NULL;
2245 capname = tigetstr( capname );
2246 if (capname == 0 || capname == (char*) -1) {
2247 Py_INCREF(Py_None);
2248 return Py_None;
2250 return PyString_FromString( capname );
2253 static PyObject *
2254 PyCurses_tparm(PyObject *self, PyObject *args)
2256 char* fmt;
2257 char* result = NULL;
2258 int i1,i2,i3,i4,i5,i6,i7,i8,i9;
2260 PyCursesSetupTermCalled;
2262 if (!PyArg_ParseTuple(args, "s|iiiiiiiii:tparm",
2263 &fmt, &i1, &i2, &i3, &i4,
2264 &i5, &i6, &i7, &i8, &i9)) {
2265 return NULL;
2268 #if defined(__hpux) || defined(_AIX)
2269 /* tparm is declared with 10 arguments on a few platforms
2270 (HP-UX, AIX). If this proves to be a problem on other
2271 platforms as well, perhaps an autoconf test should be
2272 added to determine whether tparm can be called with a
2273 variable number of arguments. Perhaps the other arguments
2274 should be initialized in this case also. */
2275 result = tparm(fmt,i1,i2,i3,i4,i5,i6,i7,i8,i9);
2276 #else
2277 switch (PyTuple_GET_SIZE(args)) {
2278 case 1:
2279 result = tparm(fmt);
2280 break;
2281 case 2:
2282 result = tparm(fmt,i1);
2283 break;
2284 case 3:
2285 result = tparm(fmt,i1,i2);
2286 break;
2287 case 4:
2288 result = tparm(fmt,i1,i2,i3);
2289 break;
2290 case 5:
2291 result = tparm(fmt,i1,i2,i3,i4);
2292 break;
2293 case 6:
2294 result = tparm(fmt,i1,i2,i3,i4,i5);
2295 break;
2296 case 7:
2297 result = tparm(fmt,i1,i2,i3,i4,i5,i6);
2298 break;
2299 case 8:
2300 result = tparm(fmt,i1,i2,i3,i4,i5,i6,i7);
2301 break;
2302 case 9:
2303 result = tparm(fmt,i1,i2,i3,i4,i5,i6,i7,i8);
2304 break;
2305 case 10:
2306 result = tparm(fmt,i1,i2,i3,i4,i5,i6,i7,i8,i9);
2307 break;
2309 #endif /* defined(__hpux) || defined(_AIX) */
2310 return PyString_FromString(result);
2313 static PyObject *
2314 PyCurses_TypeAhead(PyObject *self, PyObject *args)
2316 int fd;
2318 PyCursesInitialised
2320 if (!PyArg_ParseTuple(args,"i;fd",&fd)) return NULL;
2322 PyCursesCheckERR(typeahead( fd ), "typeahead");
2323 Py_INCREF(Py_None);
2324 return Py_None;
2327 static PyObject *
2328 PyCurses_UnCtrl(PyObject *self, PyObject *args)
2330 PyObject *temp;
2331 chtype ch;
2333 PyCursesInitialised
2335 if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
2337 if (PyInt_Check(temp))
2338 ch = (chtype) PyInt_AsLong(temp);
2339 else if (PyString_Check(temp))
2340 ch = (chtype) *PyString_AsString(temp);
2341 else {
2342 PyErr_SetString(PyExc_TypeError, "argument must be a ch or an int");
2343 return NULL;
2346 return PyString_FromString(unctrl(ch));
2349 static PyObject *
2350 PyCurses_UngetCh(PyObject *self, PyObject *args)
2352 PyObject *temp;
2353 chtype ch;
2355 PyCursesInitialised
2357 if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
2359 if (PyInt_Check(temp))
2360 ch = (chtype) PyInt_AsLong(temp);
2361 else if (PyString_Check(temp))
2362 ch = (chtype) *PyString_AsString(temp);
2363 else {
2364 PyErr_SetString(PyExc_TypeError, "argument must be a ch or an int");
2365 return NULL;
2368 return PyCursesCheckERR(ungetch(ch), "ungetch");
2371 static PyObject *
2372 PyCurses_Use_Env(PyObject *self, PyObject *args)
2374 int flag;
2376 PyCursesInitialised
2378 switch(PyTuple_Size(args)) {
2379 case 1:
2380 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&flag))
2381 return NULL;
2382 break;
2383 default:
2384 PyErr_SetString(PyExc_TypeError, "use_env requires 1 argument");
2385 return NULL;
2387 use_env(flag);
2388 Py_INCREF(Py_None);
2389 return Py_None;
2392 /* List of functions defined in the module */
2394 static PyMethodDef PyCurses_methods[] = {
2395 {"baudrate", (PyCFunction)PyCurses_baudrate, METH_NOARGS},
2396 {"beep", (PyCFunction)PyCurses_beep, METH_NOARGS},
2397 {"can_change_color", (PyCFunction)PyCurses_can_change_color, METH_NOARGS},
2398 {"cbreak", (PyCFunction)PyCurses_cbreak, METH_VARARGS},
2399 {"color_content", (PyCFunction)PyCurses_Color_Content, METH_VARARGS},
2400 {"color_pair", (PyCFunction)PyCurses_color_pair, METH_VARARGS},
2401 {"curs_set", (PyCFunction)PyCurses_Curs_Set, METH_VARARGS},
2402 {"def_prog_mode", (PyCFunction)PyCurses_def_prog_mode, METH_NOARGS},
2403 {"def_shell_mode", (PyCFunction)PyCurses_def_shell_mode, METH_NOARGS},
2404 {"delay_output", (PyCFunction)PyCurses_Delay_Output, METH_VARARGS},
2405 {"doupdate", (PyCFunction)PyCurses_doupdate, METH_NOARGS},
2406 {"echo", (PyCFunction)PyCurses_echo, METH_VARARGS},
2407 {"endwin", (PyCFunction)PyCurses_endwin, METH_NOARGS},
2408 {"erasechar", (PyCFunction)PyCurses_EraseChar, METH_NOARGS},
2409 {"filter", (PyCFunction)PyCurses_filter, METH_NOARGS},
2410 {"flash", (PyCFunction)PyCurses_flash, METH_NOARGS},
2411 {"flushinp", (PyCFunction)PyCurses_flushinp, METH_NOARGS},
2412 #ifdef NCURSES_MOUSE_VERSION
2413 {"getmouse", (PyCFunction)PyCurses_GetMouse, METH_NOARGS},
2414 {"ungetmouse", (PyCFunction)PyCurses_UngetMouse, METH_VARARGS},
2415 #endif
2416 {"getsyx", (PyCFunction)PyCurses_getsyx, METH_NOARGS},
2417 {"getwin", (PyCFunction)PyCurses_GetWin, METH_O},
2418 {"has_colors", (PyCFunction)PyCurses_has_colors, METH_NOARGS},
2419 {"has_ic", (PyCFunction)PyCurses_has_ic, METH_NOARGS},
2420 {"has_il", (PyCFunction)PyCurses_has_il, METH_NOARGS},
2421 #ifndef STRICT_SYSV_CURSES
2422 {"has_key", (PyCFunction)PyCurses_has_key, METH_VARARGS},
2423 #endif
2424 {"halfdelay", (PyCFunction)PyCurses_HalfDelay, METH_VARARGS},
2425 {"init_color", (PyCFunction)PyCurses_Init_Color, METH_VARARGS},
2426 {"init_pair", (PyCFunction)PyCurses_Init_Pair, METH_VARARGS},
2427 {"initscr", (PyCFunction)PyCurses_InitScr, METH_NOARGS},
2428 {"intrflush", (PyCFunction)PyCurses_IntrFlush, METH_VARARGS},
2429 {"isendwin", (PyCFunction)PyCurses_isendwin, METH_NOARGS},
2430 #if !defined(__NetBSD__)
2431 {"keyname", (PyCFunction)PyCurses_KeyName, METH_VARARGS},
2432 #endif
2433 {"killchar", (PyCFunction)PyCurses_KillChar, METH_NOARGS},
2434 {"longname", (PyCFunction)PyCurses_longname, METH_NOARGS},
2435 {"meta", (PyCFunction)PyCurses_Meta, METH_VARARGS},
2436 #ifdef NCURSES_MOUSE_VERSION
2437 {"mouseinterval", (PyCFunction)PyCurses_MouseInterval, METH_VARARGS},
2438 {"mousemask", (PyCFunction)PyCurses_MouseMask, METH_VARARGS},
2439 #endif
2440 {"napms", (PyCFunction)PyCurses_Napms, METH_VARARGS},
2441 {"newpad", (PyCFunction)PyCurses_NewPad, METH_VARARGS},
2442 {"newwin", (PyCFunction)PyCurses_NewWindow, METH_VARARGS},
2443 {"nl", (PyCFunction)PyCurses_nl, METH_VARARGS},
2444 {"nocbreak", (PyCFunction)PyCurses_nocbreak, METH_NOARGS},
2445 {"noecho", (PyCFunction)PyCurses_noecho, METH_NOARGS},
2446 {"nonl", (PyCFunction)PyCurses_nonl, METH_NOARGS},
2447 {"noqiflush", (PyCFunction)PyCurses_noqiflush, METH_NOARGS},
2448 {"noraw", (PyCFunction)PyCurses_noraw, METH_NOARGS},
2449 {"pair_content", (PyCFunction)PyCurses_Pair_Content, METH_VARARGS},
2450 {"pair_number", (PyCFunction)PyCurses_pair_number, METH_VARARGS},
2451 {"putp", (PyCFunction)PyCurses_Putp, METH_VARARGS},
2452 {"qiflush", (PyCFunction)PyCurses_QiFlush, METH_VARARGS},
2453 {"raw", (PyCFunction)PyCurses_raw, METH_VARARGS},
2454 {"reset_prog_mode", (PyCFunction)PyCurses_reset_prog_mode, METH_NOARGS},
2455 {"reset_shell_mode", (PyCFunction)PyCurses_reset_shell_mode, METH_NOARGS},
2456 {"resetty", (PyCFunction)PyCurses_resetty, METH_NOARGS},
2457 {"savetty", (PyCFunction)PyCurses_savetty, METH_NOARGS},
2458 {"setsyx", (PyCFunction)PyCurses_setsyx, METH_VARARGS},
2459 {"setupterm", (PyCFunction)PyCurses_setupterm,
2460 METH_VARARGS|METH_KEYWORDS},
2461 {"start_color", (PyCFunction)PyCurses_Start_Color, METH_NOARGS},
2462 {"termattrs", (PyCFunction)PyCurses_termattrs, METH_NOARGS},
2463 {"termname", (PyCFunction)PyCurses_termname, METH_NOARGS},
2464 {"tigetflag", (PyCFunction)PyCurses_tigetflag, METH_VARARGS},
2465 {"tigetnum", (PyCFunction)PyCurses_tigetnum, METH_VARARGS},
2466 {"tigetstr", (PyCFunction)PyCurses_tigetstr, METH_VARARGS},
2467 {"tparm", (PyCFunction)PyCurses_tparm, METH_VARARGS},
2468 {"typeahead", (PyCFunction)PyCurses_TypeAhead, METH_VARARGS},
2469 {"unctrl", (PyCFunction)PyCurses_UnCtrl, METH_VARARGS},
2470 {"ungetch", (PyCFunction)PyCurses_UngetCh, METH_VARARGS},
2471 {"use_env", (PyCFunction)PyCurses_Use_Env, METH_VARARGS},
2472 {NULL, NULL} /* sentinel */
2475 /* Initialization function for the module */
2477 PyMODINIT_FUNC
2478 init_curses(void)
2480 PyObject *m, *d, *v, *c_api_object;
2481 static void *PyCurses_API[PyCurses_API_pointers];
2483 /* Initialize object type */
2484 PyCursesWindow_Type.ob_type = &PyType_Type;
2486 /* Initialize the C API pointer array */
2487 PyCurses_API[0] = (void *)&PyCursesWindow_Type;
2488 PyCurses_API[1] = (void *)func_PyCursesSetupTermCalled;
2489 PyCurses_API[2] = (void *)func_PyCursesInitialised;
2490 PyCurses_API[3] = (void *)func_PyCursesInitialisedColor;
2492 /* Create the module and add the functions */
2493 m = Py_InitModule("_curses", PyCurses_methods);
2495 /* Add some symbolic constants to the module */
2496 d = PyModule_GetDict(m);
2497 ModDict = d; /* For PyCurses_InitScr to use later */
2499 /* Add a CObject for the C API */
2500 c_api_object = PyCObject_FromVoidPtr((void *)PyCurses_API, NULL);
2501 PyDict_SetItemString(d, "_C_API", c_api_object);
2502 Py_DECREF(c_api_object);
2504 /* For exception curses.error */
2505 PyCursesError = PyErr_NewException("_curses.error", NULL, NULL);
2506 PyDict_SetItemString(d, "error", PyCursesError);
2508 /* Make the version available */
2509 v = PyString_FromString(PyCursesVersion);
2510 PyDict_SetItemString(d, "version", v);
2511 PyDict_SetItemString(d, "__version__", v);
2512 Py_DECREF(v);
2514 SetDictInt("ERR", ERR);
2515 SetDictInt("OK", OK);
2517 /* Here are some attributes you can add to chars to print */
2519 SetDictInt("A_ATTRIBUTES", A_ATTRIBUTES);
2520 SetDictInt("A_NORMAL", A_NORMAL);
2521 SetDictInt("A_STANDOUT", A_STANDOUT);
2522 SetDictInt("A_UNDERLINE", A_UNDERLINE);
2523 SetDictInt("A_REVERSE", A_REVERSE);
2524 SetDictInt("A_BLINK", A_BLINK);
2525 SetDictInt("A_DIM", A_DIM);
2526 SetDictInt("A_BOLD", A_BOLD);
2527 SetDictInt("A_ALTCHARSET", A_ALTCHARSET);
2528 #if !defined(__NetBSD__)
2529 SetDictInt("A_INVIS", A_INVIS);
2530 #endif
2531 SetDictInt("A_PROTECT", A_PROTECT);
2532 SetDictInt("A_CHARTEXT", A_CHARTEXT);
2533 SetDictInt("A_COLOR", A_COLOR);
2535 /* The following are never available with strict SYSV curses */
2536 #ifdef A_HORIZONTAL
2537 SetDictInt("A_HORIZONTAL", A_HORIZONTAL);
2538 #endif
2539 #ifdef A_LEFT
2540 SetDictInt("A_LEFT", A_LEFT);
2541 #endif
2542 #ifdef A_LOW
2543 SetDictInt("A_LOW", A_LOW);
2544 #endif
2545 #ifdef A_RIGHT
2546 SetDictInt("A_RIGHT", A_RIGHT);
2547 #endif
2548 #ifdef A_TOP
2549 SetDictInt("A_TOP", A_TOP);
2550 #endif
2551 #ifdef A_VERTICAL
2552 SetDictInt("A_VERTICAL", A_VERTICAL);
2553 #endif
2555 SetDictInt("COLOR_BLACK", COLOR_BLACK);
2556 SetDictInt("COLOR_RED", COLOR_RED);
2557 SetDictInt("COLOR_GREEN", COLOR_GREEN);
2558 SetDictInt("COLOR_YELLOW", COLOR_YELLOW);
2559 SetDictInt("COLOR_BLUE", COLOR_BLUE);
2560 SetDictInt("COLOR_MAGENTA", COLOR_MAGENTA);
2561 SetDictInt("COLOR_CYAN", COLOR_CYAN);
2562 SetDictInt("COLOR_WHITE", COLOR_WHITE);
2564 #ifdef NCURSES_MOUSE_VERSION
2565 /* Mouse-related constants */
2566 SetDictInt("BUTTON1_PRESSED", BUTTON1_PRESSED);
2567 SetDictInt("BUTTON1_RELEASED", BUTTON1_RELEASED);
2568 SetDictInt("BUTTON1_CLICKED", BUTTON1_CLICKED);
2569 SetDictInt("BUTTON1_DOUBLE_CLICKED", BUTTON1_DOUBLE_CLICKED);
2570 SetDictInt("BUTTON1_TRIPLE_CLICKED", BUTTON1_TRIPLE_CLICKED);
2572 SetDictInt("BUTTON2_PRESSED", BUTTON2_PRESSED);
2573 SetDictInt("BUTTON2_RELEASED", BUTTON2_RELEASED);
2574 SetDictInt("BUTTON2_CLICKED", BUTTON2_CLICKED);
2575 SetDictInt("BUTTON2_DOUBLE_CLICKED", BUTTON2_DOUBLE_CLICKED);
2576 SetDictInt("BUTTON2_TRIPLE_CLICKED", BUTTON2_TRIPLE_CLICKED);
2578 SetDictInt("BUTTON3_PRESSED", BUTTON3_PRESSED);
2579 SetDictInt("BUTTON3_RELEASED", BUTTON3_RELEASED);
2580 SetDictInt("BUTTON3_CLICKED", BUTTON3_CLICKED);
2581 SetDictInt("BUTTON3_DOUBLE_CLICKED", BUTTON3_DOUBLE_CLICKED);
2582 SetDictInt("BUTTON3_TRIPLE_CLICKED", BUTTON3_TRIPLE_CLICKED);
2584 SetDictInt("BUTTON4_PRESSED", BUTTON4_PRESSED);
2585 SetDictInt("BUTTON4_RELEASED", BUTTON4_RELEASED);
2586 SetDictInt("BUTTON4_CLICKED", BUTTON4_CLICKED);
2587 SetDictInt("BUTTON4_DOUBLE_CLICKED", BUTTON4_DOUBLE_CLICKED);
2588 SetDictInt("BUTTON4_TRIPLE_CLICKED", BUTTON4_TRIPLE_CLICKED);
2590 SetDictInt("BUTTON_SHIFT", BUTTON_SHIFT);
2591 SetDictInt("BUTTON_CTRL", BUTTON_CTRL);
2592 SetDictInt("BUTTON_ALT", BUTTON_ALT);
2594 SetDictInt("ALL_MOUSE_EVENTS", ALL_MOUSE_EVENTS);
2595 SetDictInt("REPORT_MOUSE_POSITION", REPORT_MOUSE_POSITION);
2596 #endif
2597 /* Now set everything up for KEY_ variables */
2599 int key;
2600 char *key_n;
2601 char *key_n2;
2602 #if !defined(__NetBSD__)
2603 for (key=KEY_MIN;key < KEY_MAX; key++) {
2604 key_n = (char *)keyname(key);
2605 if (key_n == NULL || strcmp(key_n,"UNKNOWN KEY")==0)
2606 continue;
2607 if (strncmp(key_n,"KEY_F(",6)==0) {
2608 char *p1, *p2;
2609 key_n2 = malloc(strlen(key_n)+1);
2610 p1 = key_n;
2611 p2 = key_n2;
2612 while (*p1) {
2613 if (*p1 != '(' && *p1 != ')') {
2614 *p2 = *p1;
2615 p2++;
2617 p1++;
2619 *p2 = (char)0;
2620 } else
2621 key_n2 = key_n;
2622 PyDict_SetItemString(d,key_n2,PyInt_FromLong((long) key));
2623 if (key_n2 != key_n)
2624 free(key_n2);
2626 #endif
2627 SetDictInt("KEY_MIN", KEY_MIN);
2628 SetDictInt("KEY_MAX", KEY_MAX);