More installation info. Bump alpha version.
[python/dscho.git] / Modules / _cursesmodule.c
blob4424dae6923f4db720e6b3df2c29b2ce472635f1
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 need
39 a given function, add it and send a patch. Here's a list of currently
40 unsupported functions:
42 addchnstr addchstr chgat color_set define_key
43 del_curterm delscreen dupwin inchnstr inchstr innstr keyok
44 mcprint mvaddchnstr mvaddchstr mvchgat mvcur mvinchnstr
45 mvinchstr mvinnstr mmvwaddchnstr mvwaddchstr mvwchgat
46 mvwgetnstr mvwinchnstr mvwinchstr mvwinnstr newterm
47 resizeterm restartterm ripoffline scr_dump
48 scr_init scr_restore scr_set scrl set_curterm set_term setterm
49 tgetent tgetflag tgetnum tgetstr tgoto timeout tputs
50 use_default_colors vidattr vidputs waddchnstr waddchstr wchgat
51 wcolor_set winchnstr winchstr winnstr wmouse_trafo wscrl
53 Low-priority:
54 slk_attr slk_attr_off slk_attr_on slk_attr_set slk_attroff
55 slk_attron slk_attrset slk_clear slk_color slk_init slk_label
56 slk_noutrefresh slk_refresh slk_restore slk_set slk_touch
58 Menu extension (ncurses and probably SYSV):
59 current_item free_item free_menu item_count item_description
60 item_index item_init item_name item_opts item_opts_off
61 item_opts_on item_term item_userptr item_value item_visible
62 menu_back menu_driver menu_fore menu_format menu_grey
63 menu_init menu_items menu_mark menu_opts menu_opts_off
64 menu_opts_on menu_pad menu_pattern menu_request_by_name
65 menu_request_name menu_spacing menu_sub menu_term menu_userptr
66 menu_win new_item new_menu pos_menu_cursor post_menu
67 scale_menu set_current_item set_item_init set_item_opts
68 set_item_term set_item_userptr set_item_value set_menu_back
69 set_menu_fore set_menu_format set_menu_grey set_menu_init
70 set_menu_items set_menu_mark set_menu_opts set_menu_pad
71 set_menu_pattern set_menu_spacing set_menu_sub set_menu_term
72 set_menu_userptr set_menu_win set_top_row top_row unpost_menu
74 Form extension (ncurses and probably SYSV):
75 current_field data_ahead data_behind dup_field
76 dynamic_fieldinfo field_arg field_back field_buffer
77 field_count field_fore field_index field_info field_init
78 field_just field_opts field_opts_off field_opts_on field_pad
79 field_status field_term field_type field_userptr form_driver
80 form_fields form_init form_opts form_opts_off form_opts_on
81 form_page form_request_by_name form_request_name form_sub
82 form_term form_userptr form_win free_field free_form
83 link_field link_fieldtype move_field new_field new_form
84 new_page pos_form_cursor post_form scale_form
85 set_current_field set_field_back set_field_buffer
86 set_field_fore set_field_init set_field_just set_field_opts
87 set_field_pad set_field_status set_field_term set_field_type
88 set_field_userptr set_fieldtype_arg set_fieldtype_choice
89 set_form_fields set_form_init set_form_opts set_form_page
90 set_form_sub set_form_term set_form_userptr set_form_win
91 set_max_field set_new_page unpost_form
96 /* Release Number */
98 char *PyCursesVersion = "2.2";
100 /* Includes */
102 #include "Python.h"
104 #ifdef __osf__
105 #define STRICT_SYSV_CURSES /* Don't use ncurses extensions */
106 #endif
108 #ifdef __hpux
109 #define STRICT_SYSV_CURSES
110 #endif
112 #define CURSES_MODULE
113 #include "py_curses.h"
115 /* These prototypes are in <term.h>, but including this header
116 #defines many common symbols (such as "lines") which breaks the
117 curses module in other ways. So the code will just specify
118 explicit prototypes here. */
119 extern int setupterm(char *,int,int *);
120 #ifdef __sgi
121 #include <term.h>
122 #endif
124 #if !defined(HAVE_NCURSES_H) && (defined(sgi) || defined(__sun) || defined(SCO5))
125 #define STRICT_SYSV_CURSES /* Don't use ncurses extensions */
126 typedef chtype attr_t; /* No attr_t type is available */
127 #endif
129 #if defined(_AIX)
130 #define STRICT_SYSV_CURSES
131 #endif
133 /* Definition of exception curses.error */
135 static PyObject *PyCursesError;
137 /* Tells whether setupterm() has been called to initialise terminfo. */
138 static int initialised_setupterm = FALSE;
140 /* Tells whether initscr() has been called to initialise curses. */
141 static int initialised = FALSE;
143 /* Tells whether start_color() has been called to initialise color usage. */
144 static int initialisedcolors = FALSE;
146 /* Utility Macros */
147 #define PyCursesSetupTermCalled \
148 if (initialised_setupterm != TRUE) { \
149 PyErr_SetString(PyCursesError, \
150 "must call (at least) setupterm() first"); \
151 return 0; }
153 #define PyCursesInitialised \
154 if (initialised != TRUE) { \
155 PyErr_SetString(PyCursesError, \
156 "must call initscr() first"); \
157 return 0; }
159 #define PyCursesInitialisedColor \
160 if (initialisedcolors != TRUE) { \
161 PyErr_SetString(PyCursesError, \
162 "must call start_color() first"); \
163 return 0; }
165 /* Utility Functions */
168 * Check the return code from a curses function and return None
169 * or raise an exception as appropriate. These are exported using the
170 * CObject API.
173 static PyObject *
174 PyCursesCheckERR(int code, char *fname)
176 if (code != ERR) {
177 Py_INCREF(Py_None);
178 return Py_None;
179 } else {
180 if (fname == NULL) {
181 PyErr_SetString(PyCursesError, catchall_ERR);
182 } else {
183 PyErr_Format(PyCursesError, "%s() returned ERR", fname);
185 return NULL;
189 static int
190 PyCurses_ConvertToChtype(PyObject *obj, chtype *ch)
192 if (PyInt_Check(obj)) {
193 *ch = (chtype) PyInt_AsLong(obj);
194 } else if(PyString_Check(obj)
195 && (PyString_Size(obj) == 1)) {
196 *ch = (chtype) *PyString_AsString(obj);
197 } else {
198 return 0;
200 return 1;
203 /* Function versions of the 3 functions for tested whether curses has been
204 initialised or not. */
206 static int func_PyCursesSetupTermCalled(void)
208 PyCursesSetupTermCalled;
209 return 1;
212 static int func_PyCursesInitialised(void)
214 PyCursesInitialised;
215 return 1;
218 static int func_PyCursesInitialisedColor(void)
220 PyCursesInitialisedColor;
221 return 1;
224 /*****************************************************************************
225 The Window Object
226 ******************************************************************************/
228 /* Definition of the window type */
230 PyTypeObject PyCursesWindow_Type;
232 /* Function prototype macros for Window object
234 X - function name
235 TYPE - parameter Type
236 ERGSTR - format string for construction of the return value
237 PARSESTR - format string for argument parsing
240 #define Window_NoArgNoReturnFunction(X) \
241 static PyObject *PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
242 { return PyCursesCheckERR(X(self->win), # X); }
244 #define Window_NoArgTrueFalseFunction(X) \
245 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self) \
247 if (X (self->win) == FALSE) { Py_INCREF(Py_False); return Py_False; } \
248 else { Py_INCREF(Py_True); return Py_True; } }
250 #define Window_NoArgNoReturnVoidFunction(X) \
251 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self) \
253 X(self->win); Py_INCREF(Py_None); return Py_None; }
255 #define Window_NoArg2TupleReturnFunction(X, TYPE, ERGSTR) \
256 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self) \
258 TYPE arg1, arg2; \
259 X(self->win,arg1,arg2); return Py_BuildValue(ERGSTR, arg1, arg2); }
261 #define Window_OneArgNoReturnVoidFunction(X, TYPE, PARSESTR) \
262 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
264 TYPE arg1; \
265 if (!PyArg_ParseTuple(args, PARSESTR, &arg1)) return NULL; \
266 X(self->win,arg1); Py_INCREF(Py_None); return Py_None; }
268 #define Window_OneArgNoReturnFunction(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 return PyCursesCheckERR(X(self->win, arg1), # X); }
275 #define Window_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \
276 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
278 TYPE arg1, arg2; \
279 if (!PyArg_ParseTuple(args,PARSESTR, &arg1, &arg2)) return NULL; \
280 return PyCursesCheckERR(X(self->win, arg1, arg2), # X); }
282 /* ------------- WINDOW routines --------------- */
284 Window_NoArgNoReturnFunction(untouchwin)
285 Window_NoArgNoReturnFunction(touchwin)
286 Window_NoArgNoReturnFunction(redrawwin)
287 Window_NoArgNoReturnFunction(winsertln)
288 Window_NoArgNoReturnFunction(werase)
289 Window_NoArgNoReturnFunction(wdeleteln)
291 Window_NoArgTrueFalseFunction(is_wintouched)
293 Window_NoArgNoReturnVoidFunction(wsyncup)
294 Window_NoArgNoReturnVoidFunction(wsyncdown)
295 Window_NoArgNoReturnVoidFunction(wstandend)
296 Window_NoArgNoReturnVoidFunction(wstandout)
297 Window_NoArgNoReturnVoidFunction(wcursyncup)
298 Window_NoArgNoReturnVoidFunction(wclrtoeol)
299 Window_NoArgNoReturnVoidFunction(wclrtobot)
300 Window_NoArgNoReturnVoidFunction(wclear)
302 Window_OneArgNoReturnVoidFunction(idcok, int, "i;True(1) or False(0)")
303 Window_OneArgNoReturnVoidFunction(immedok, int, "i;True(1) or False(0)")
304 Window_OneArgNoReturnVoidFunction(wtimeout, int, "i;delay")
306 Window_NoArg2TupleReturnFunction(getyx, int, "ii")
307 Window_NoArg2TupleReturnFunction(getbegyx, int, "ii")
308 Window_NoArg2TupleReturnFunction(getmaxyx, int, "ii")
309 Window_NoArg2TupleReturnFunction(getparyx, int, "ii")
311 Window_OneArgNoReturnFunction(wattron, attr_t, "l;attr")
312 Window_OneArgNoReturnFunction(wattroff, attr_t, "l;attr")
313 Window_OneArgNoReturnFunction(wattrset, attr_t, "l;attr")
314 Window_OneArgNoReturnFunction(clearok, int, "i;True(1) or False(0)")
315 Window_OneArgNoReturnFunction(idlok, int, "i;True(1) or False(0)")
316 #if defined(__NetBSD__)
317 Window_OneArgNoReturnVoidFunction(keypad, int, "i;True(1) or False(0)")
318 #else
319 Window_OneArgNoReturnFunction(keypad, int, "i;True(1) or False(0)")
320 #endif
321 Window_OneArgNoReturnFunction(leaveok, int, "i;True(1) or False(0)")
322 #if defined(__NetBSD__)
323 Window_OneArgNoReturnVoidFunction(nodelay, int, "i;True(1) or False(0)")
324 #else
325 Window_OneArgNoReturnFunction(nodelay, int, "i;True(1) or False(0)")
326 #endif
327 Window_OneArgNoReturnFunction(notimeout, int, "i;True(1) or False(0)")
328 Window_OneArgNoReturnFunction(scrollok, int, "i;True(1) or False(0)")
329 Window_OneArgNoReturnFunction(winsdelln, int, "i;nlines")
330 Window_OneArgNoReturnFunction(syncok, int, "i;True(1) or False(0)")
332 Window_TwoArgNoReturnFunction(mvwin, int, "ii;y,x")
333 Window_TwoArgNoReturnFunction(mvderwin, int, "ii;y,x")
334 Window_TwoArgNoReturnFunction(wmove, int, "ii;y,x")
335 #ifndef STRICT_SYSV_CURSES
336 Window_TwoArgNoReturnFunction(wresize, int, "ii;lines,columns")
337 #endif
339 /* Allocation and deallocation of Window Objects */
341 static PyObject *
342 PyCursesWindow_New(WINDOW *win)
344 PyCursesWindowObject *wo;
346 wo = PyObject_NEW(PyCursesWindowObject, &PyCursesWindow_Type);
347 if (wo == NULL) return NULL;
348 wo->win = win;
349 return (PyObject *)wo;
352 static void
353 PyCursesWindow_Dealloc(PyCursesWindowObject *wo)
355 if (wo->win != stdscr) delwin(wo->win);
356 PyObject_DEL(wo);
359 /* Addch, Addstr, Addnstr */
361 static PyObject *
362 PyCursesWindow_AddCh(PyCursesWindowObject *self, PyObject *args)
364 int rtn, x, y, use_xy = FALSE;
365 PyObject *temp;
366 chtype ch = 0;
367 attr_t attr = A_NORMAL;
369 switch (PyTuple_Size(args)) {
370 case 1:
371 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
372 return NULL;
373 break;
374 case 2:
375 if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &temp, &attr))
376 return NULL;
377 break;
378 case 3:
379 if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &temp))
380 return NULL;
381 use_xy = TRUE;
382 break;
383 case 4:
384 if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr",
385 &y, &x, &temp, &attr))
386 return NULL;
387 use_xy = TRUE;
388 break;
389 default:
390 PyErr_SetString(PyExc_TypeError, "addch requires 1 to 4 arguments");
391 return NULL;
394 if (!PyCurses_ConvertToChtype(temp, &ch)) {
395 PyErr_SetString(PyExc_TypeError, "argument 1 or 3 must be a ch or an int");
396 return NULL;
399 if (use_xy == TRUE)
400 rtn = mvwaddch(self->win,y,x, ch | attr);
401 else {
402 rtn = waddch(self->win, ch | attr);
404 return PyCursesCheckERR(rtn, "addch");
407 static PyObject *
408 PyCursesWindow_AddStr(PyCursesWindowObject *self, PyObject *args)
410 int rtn;
411 int x, y;
412 char *str;
413 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
414 int use_xy = FALSE, use_attr = FALSE;
416 switch (PyTuple_Size(args)) {
417 case 1:
418 if (!PyArg_ParseTuple(args,"s;str", &str))
419 return NULL;
420 break;
421 case 2:
422 if (!PyArg_ParseTuple(args,"sl;str,attr", &str, &attr))
423 return NULL;
424 use_attr = TRUE;
425 break;
426 case 3:
427 if (!PyArg_ParseTuple(args,"iis;int,int,str", &y, &x, &str))
428 return NULL;
429 use_xy = TRUE;
430 break;
431 case 4:
432 if (!PyArg_ParseTuple(args,"iisl;int,int,str,attr", &y, &x, &str, &attr))
433 return NULL;
434 use_xy = use_attr = TRUE;
435 break;
436 default:
437 PyErr_SetString(PyExc_TypeError, "addstr requires 1 to 4 arguments");
438 return NULL;
441 if (use_attr == TRUE) {
442 attr_old = getattrs(self->win);
443 wattrset(self->win,attr);
445 if (use_xy == TRUE)
446 rtn = mvwaddstr(self->win,y,x,str);
447 else
448 rtn = waddstr(self->win,str);
449 if (use_attr == TRUE)
450 wattrset(self->win,attr_old);
451 return PyCursesCheckERR(rtn, "addstr");
454 static PyObject *
455 PyCursesWindow_AddNStr(PyCursesWindowObject *self, PyObject *args)
457 int rtn, x, y, n;
458 char *str;
459 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
460 int use_xy = FALSE, use_attr = FALSE;
462 switch (PyTuple_Size(args)) {
463 case 2:
464 if (!PyArg_ParseTuple(args,"si;str,n", &str, &n))
465 return NULL;
466 break;
467 case 3:
468 if (!PyArg_ParseTuple(args,"sil;str,n,attr", &str, &n, &attr))
469 return NULL;
470 use_attr = TRUE;
471 break;
472 case 4:
473 if (!PyArg_ParseTuple(args,"iisi;y,x,str,n", &y, &x, &str, &n))
474 return NULL;
475 use_xy = TRUE;
476 break;
477 case 5:
478 if (!PyArg_ParseTuple(args,"iisil;y,x,str,n,attr", &y, &x, &str, &n, &attr))
479 return NULL;
480 use_xy = use_attr = TRUE;
481 break;
482 default:
483 PyErr_SetString(PyExc_TypeError, "addnstr requires 2 to 5 arguments");
484 return NULL;
487 if (use_attr == TRUE) {
488 attr_old = getattrs(self->win);
489 wattrset(self->win,attr);
491 if (use_xy == TRUE)
492 rtn = mvwaddnstr(self->win,y,x,str,n);
493 else
494 rtn = waddnstr(self->win,str,n);
495 if (use_attr == TRUE)
496 wattrset(self->win,attr_old);
497 return PyCursesCheckERR(rtn, "addnstr");
500 static PyObject *
501 PyCursesWindow_Bkgd(PyCursesWindowObject *self, PyObject *args)
503 PyObject *temp;
504 chtype bkgd;
505 attr_t attr = A_NORMAL;
507 switch (PyTuple_Size(args)) {
508 case 1:
509 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
510 return NULL;
511 break;
512 case 2:
513 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &attr))
514 return NULL;
515 break;
516 default:
517 PyErr_SetString(PyExc_TypeError, "bkgd requires 1 or 2 arguments");
518 return NULL;
521 if (!PyCurses_ConvertToChtype(temp, &bkgd)) {
522 PyErr_SetString(PyExc_TypeError, "argument 1 or 3 must be a ch or an int");
523 return NULL;
526 return PyCursesCheckERR(wbkgd(self->win, bkgd | attr), "bkgd");
529 static PyObject *
530 PyCursesWindow_BkgdSet(PyCursesWindowObject *self, PyObject *args)
532 PyObject *temp;
533 chtype bkgd;
534 attr_t attr = A_NORMAL;
536 switch (PyTuple_Size(args)) {
537 case 1:
538 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
539 return NULL;
540 break;
541 case 2:
542 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &attr))
543 return NULL;
544 break;
545 default:
546 PyErr_SetString(PyExc_TypeError, "bkgdset requires 1 or 2 arguments");
547 return NULL;
550 if (!PyCurses_ConvertToChtype(temp, &bkgd)) {
551 PyErr_SetString(PyExc_TypeError, "argument 1 must be a ch or an int");
552 return NULL;
555 wbkgdset(self->win, bkgd | attr);
556 return PyCursesCheckERR(0, "bkgdset");
559 static PyObject *
560 PyCursesWindow_Border(PyCursesWindowObject *self, PyObject *args)
562 PyObject *temp[8];
563 chtype ch[8];
564 int i;
566 /* Clear the array of parameters */
567 for(i=0; i<8; i++) {
568 temp[i] = NULL;
569 ch[i] = 0;
572 if (!PyArg_ParseTuple(args,"|OOOOOOOO;ls,rs,ts,bs,tl,tr,bl,br",
573 &temp[0], &temp[1], &temp[2], &temp[3],
574 &temp[4], &temp[5], &temp[6], &temp[7]))
575 return NULL;
577 for(i=0; i<8; i++) {
578 if (temp[i] != NULL && !PyCurses_ConvertToChtype(temp[i], &ch[i])) {
579 PyErr_Format(PyExc_TypeError,
580 "argument %i must be a ch or an int", i+1);
581 return NULL;
585 wborder(self->win,
586 ch[0], ch[1], ch[2], ch[3],
587 ch[4], ch[5], ch[6], ch[7]);
588 Py_INCREF(Py_None);
589 return Py_None;
592 static PyObject *
593 PyCursesWindow_Box(PyCursesWindowObject *self, PyObject *args)
595 chtype ch1=0,ch2=0;
596 switch(PyTuple_Size(args)){
597 case 0: break;
598 default:
599 if (!PyArg_ParseTuple(args,"ll;vertint,horint", &ch1, &ch2))
600 return NULL;
602 box(self->win,ch1,ch2);
603 Py_INCREF(Py_None);
604 return Py_None;
607 #if defined(HAVE_NCURSES_H) || defined(MVWDELCH_IS_EXPRESSION)
608 #define py_mvwdelch mvwdelch
609 #else
610 int py_mvwdelch(WINDOW *w, int y, int x)
612 mvwdelch(w,y,x);
613 /* On HP/UX, mvwdelch already returns. On other systems,
614 we may well run into this return statement. */
615 return 0;
617 #endif
620 static PyObject *
621 PyCursesWindow_DelCh(PyCursesWindowObject *self, PyObject *args)
623 int rtn;
624 int x, y;
626 switch (PyTuple_Size(args)) {
627 case 0:
628 rtn = wdelch(self->win);
629 break;
630 case 2:
631 if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x))
632 return NULL;
633 rtn = py_mvwdelch(self->win,y,x);
634 break;
635 default:
636 PyErr_SetString(PyExc_TypeError, "delch requires 0 or 2 arguments");
637 return NULL;
639 return PyCursesCheckERR(rtn, "[mv]wdelch");
642 static PyObject *
643 PyCursesWindow_DerWin(PyCursesWindowObject *self, PyObject *args)
645 WINDOW *win;
646 int nlines, ncols, begin_y, begin_x;
648 nlines = 0;
649 ncols = 0;
650 switch (PyTuple_Size(args)) {
651 case 2:
652 if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
653 return NULL;
654 break;
655 case 4:
656 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
657 &nlines,&ncols,&begin_y,&begin_x))
658 return NULL;
659 break;
660 default:
661 PyErr_SetString(PyExc_TypeError, "derwin requires 2 or 4 arguments");
662 return NULL;
665 win = derwin(self->win,nlines,ncols,begin_y,begin_x);
667 if (win == NULL) {
668 PyErr_SetString(PyCursesError, catchall_NULL);
669 return NULL;
672 return (PyObject *)PyCursesWindow_New(win);
675 static PyObject *
676 PyCursesWindow_EchoChar(PyCursesWindowObject *self, PyObject *args)
678 PyObject *temp;
679 chtype ch;
680 attr_t attr = A_NORMAL;
682 switch (PyTuple_Size(args)) {
683 case 1:
684 if (!PyArg_ParseTuple(args,"O;ch or int", &temp))
685 return NULL;
686 break;
687 case 2:
688 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &attr))
689 return NULL;
690 break;
691 default:
692 PyErr_SetString(PyExc_TypeError, "echochar requires 1 or 2 arguments");
695 return NULL;
698 if (!PyCurses_ConvertToChtype(temp, &ch)) {
699 PyErr_SetString(PyExc_TypeError, "argument 1 must be a ch or an int");
700 return NULL;
703 #ifdef WINDOW_HAS_FLAGS
704 if (self->win->_flags & _ISPAD)
705 return PyCursesCheckERR(pechochar(self->win, ch | attr),
706 "echochar");
707 else
708 #endif
709 return PyCursesCheckERR(wechochar(self->win, ch | attr),
710 "echochar");
713 #ifdef NCURSES_MOUSE_VERSION
714 static PyObject *
715 PyCursesWindow_Enclose(PyCursesWindowObject *self, PyObject *args)
717 int x, y;
718 if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x))
719 return NULL;
721 return PyInt_FromLong( wenclose(self->win,y,x) );
723 #endif
725 static PyObject *
726 PyCursesWindow_GetBkgd(PyCursesWindowObject *self)
728 return PyInt_FromLong((long) getbkgd(self->win));
731 static PyObject *
732 PyCursesWindow_GetCh(PyCursesWindowObject *self, PyObject *args)
734 int x, y;
735 int rtn;
737 switch (PyTuple_Size(args)) {
738 case 0:
739 Py_BEGIN_ALLOW_THREADS
740 rtn = wgetch(self->win);
741 Py_END_ALLOW_THREADS
742 break;
743 case 2:
744 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
745 return NULL;
746 Py_BEGIN_ALLOW_THREADS
747 rtn = mvwgetch(self->win,y,x);
748 Py_END_ALLOW_THREADS
749 break;
750 default:
751 PyErr_SetString(PyExc_TypeError, "getch requires 0 or 2 arguments");
752 return NULL;
754 return PyInt_FromLong((long)rtn);
757 static PyObject *
758 PyCursesWindow_GetKey(PyCursesWindowObject *self, PyObject *args)
760 int x, y;
761 int rtn;
763 switch (PyTuple_Size(args)) {
764 case 0:
765 Py_BEGIN_ALLOW_THREADS
766 rtn = wgetch(self->win);
767 Py_END_ALLOW_THREADS
768 break;
769 case 2:
770 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
771 return NULL;
772 Py_BEGIN_ALLOW_THREADS
773 rtn = mvwgetch(self->win,y,x);
774 Py_END_ALLOW_THREADS
775 break;
776 default:
777 PyErr_SetString(PyExc_TypeError, "getkey requires 0 or 2 arguments");
778 return NULL;
780 if (rtn == ERR) {
781 /* getch() returns ERR in nodelay mode */
782 PyErr_SetString(PyCursesError, "no input");
783 return NULL;
784 } else if (rtn<=255)
785 return Py_BuildValue("c", rtn);
786 else
787 #if defined(__NetBSD__)
788 return PyString_FromString(unctrl(rtn));
789 #else
790 return PyString_FromString((char *)keyname(rtn));
791 #endif
794 static PyObject *
795 PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
797 int x, y, n;
798 char rtn[1024]; /* This should be big enough.. I hope */
799 int rtn2;
801 switch (PyTuple_Size(args)) {
802 case 0:
803 Py_BEGIN_ALLOW_THREADS
804 rtn2 = wgetstr(self->win,rtn);
805 Py_END_ALLOW_THREADS
806 break;
807 case 1:
808 if (!PyArg_ParseTuple(args,"i;n", &n))
809 return NULL;
810 Py_BEGIN_ALLOW_THREADS
811 rtn2 = wgetnstr(self->win,rtn,n);
812 Py_END_ALLOW_THREADS
813 break;
814 case 2:
815 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
816 return NULL;
817 Py_BEGIN_ALLOW_THREADS
818 rtn2 = mvwgetstr(self->win,y,x,rtn);
819 Py_END_ALLOW_THREADS
820 break;
821 case 3:
822 if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
823 return NULL;
824 #ifdef STRICT_SYSV_CURSES
825 /* Untested */
826 Py_BEGIN_ALLOW_THREADS
827 rtn2 = wmove(self->win,y,x)==ERR ? ERR :
828 wgetnstr(self->win, rtn, n);
829 Py_END_ALLOW_THREADS
830 #else
831 Py_BEGIN_ALLOW_THREADS
832 rtn2 = mvwgetnstr(self->win, y, x, rtn, n);
833 Py_END_ALLOW_THREADS
834 #endif
835 break;
836 default:
837 PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 2 arguments");
838 return NULL;
840 if (rtn2 == ERR)
841 rtn[0] = 0;
842 return PyString_FromString(rtn);
845 static PyObject *
846 PyCursesWindow_Hline(PyCursesWindowObject *self, PyObject *args)
848 PyObject *temp;
849 chtype ch;
850 int n, x, y, code = OK;
851 attr_t attr = A_NORMAL;
853 switch (PyTuple_Size(args)) {
854 case 2:
855 if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
856 return NULL;
857 break;
858 case 3:
859 if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &attr))
860 return NULL;
861 break;
862 case 4:
863 if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
864 return NULL;
865 code = wmove(self->win, y, x);
866 break;
867 case 5:
868 if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
869 &y, &x, &temp, &n, &attr))
870 return NULL;
871 code = wmove(self->win, y, x);
872 break;
873 default:
874 PyErr_SetString(PyExc_TypeError, "hline requires 2 to 5 arguments");
875 return NULL;
878 if (code != ERR) {
879 if (!PyCurses_ConvertToChtype(temp, &ch)) {
880 PyErr_SetString(PyExc_TypeError,
881 "argument 1 or 3 must be a ch or an int");
882 return NULL;
884 return PyCursesCheckERR(whline(self->win, ch | attr, n), "hline");
885 } else
886 return PyCursesCheckERR(code, "wmove");
889 static PyObject *
890 PyCursesWindow_InsCh(PyCursesWindowObject *self, PyObject *args)
892 int rtn, x, y, use_xy = FALSE;
893 PyObject *temp;
894 chtype ch = 0;
895 attr_t attr = A_NORMAL;
897 switch (PyTuple_Size(args)) {
898 case 1:
899 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
900 return NULL;
901 break;
902 case 2:
903 if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &temp, &attr))
904 return NULL;
905 break;
906 case 3:
907 if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &temp))
908 return NULL;
909 use_xy = TRUE;
910 break;
911 case 4:
912 if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr", &y, &x, &temp, &attr))
913 return NULL;
914 use_xy = TRUE;
915 break;
916 default:
917 PyErr_SetString(PyExc_TypeError, "insch requires 1 or 4 arguments");
918 return NULL;
921 if (!PyCurses_ConvertToChtype(temp, &ch)) {
922 PyErr_SetString(PyExc_TypeError,
923 "argument 1 or 3 must be a ch or an int");
924 return NULL;
927 if (use_xy == TRUE)
928 rtn = mvwinsch(self->win,y,x, ch | attr);
929 else {
930 rtn = winsch(self->win, ch | attr);
932 return PyCursesCheckERR(rtn, "insch");
935 static PyObject *
936 PyCursesWindow_InCh(PyCursesWindowObject *self, PyObject *args)
938 int x, y, rtn;
940 switch (PyTuple_Size(args)) {
941 case 0:
942 rtn = winch(self->win);
943 break;
944 case 2:
945 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
946 return NULL;
947 rtn = mvwinch(self->win,y,x);
948 break;
949 default:
950 PyErr_SetString(PyExc_TypeError, "inch requires 0 or 2 arguments");
951 return NULL;
953 return PyInt_FromLong((long) rtn);
956 static PyObject *
957 PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
959 int x, y, n;
960 char rtn[1024]; /* This should be big enough.. I hope */
961 int rtn2;
963 switch (PyTuple_Size(args)) {
964 case 0:
965 rtn2 = winstr(self->win,rtn);
966 break;
967 case 1:
968 if (!PyArg_ParseTuple(args,"i;n", &n))
969 return NULL;
970 rtn2 = winnstr(self->win,rtn,n);
971 break;
972 case 2:
973 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
974 return NULL;
975 rtn2 = mvwinstr(self->win,y,x,rtn);
976 break;
977 case 3:
978 if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
979 return NULL;
980 rtn2 = mvwinnstr(self->win, y, x, rtn, n);
981 break;
982 default:
983 PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments");
984 return NULL;
986 if (rtn2 == ERR)
987 rtn[0] = 0;
988 return PyString_FromString(rtn);
991 static PyObject *
992 PyCursesWindow_InsStr(PyCursesWindowObject *self, PyObject *args)
994 int rtn;
995 int x, y;
996 char *str;
997 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
998 int use_xy = FALSE, use_attr = FALSE;
1000 switch (PyTuple_Size(args)) {
1001 case 1:
1002 if (!PyArg_ParseTuple(args,"s;str", &str))
1003 return NULL;
1004 break;
1005 case 2:
1006 if (!PyArg_ParseTuple(args,"sl;str,attr", &str, &attr))
1007 return NULL;
1008 use_attr = TRUE;
1009 break;
1010 case 3:
1011 if (!PyArg_ParseTuple(args,"iis;y,x,str", &y, &x, &str))
1012 return NULL;
1013 use_xy = TRUE;
1014 break;
1015 case 4:
1016 if (!PyArg_ParseTuple(args,"iisl;y,x,str,attr", &y, &x, &str, &attr))
1017 return NULL;
1018 use_xy = use_attr = TRUE;
1019 break;
1020 default:
1021 PyErr_SetString(PyExc_TypeError, "insstr requires 1 to 4 arguments");
1022 return NULL;
1025 if (use_attr == TRUE) {
1026 attr_old = getattrs(self->win);
1027 wattrset(self->win,attr);
1029 if (use_xy == TRUE)
1030 rtn = mvwinsstr(self->win,y,x,str);
1031 else
1032 rtn = winsstr(self->win,str);
1033 if (use_attr == TRUE)
1034 wattrset(self->win,attr_old);
1035 return PyCursesCheckERR(rtn, "insstr");
1038 static PyObject *
1039 PyCursesWindow_InsNStr(PyCursesWindowObject *self, PyObject *args)
1041 int rtn, x, y, n;
1042 char *str;
1043 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
1044 int use_xy = FALSE, use_attr = FALSE;
1046 switch (PyTuple_Size(args)) {
1047 case 2:
1048 if (!PyArg_ParseTuple(args,"si;str,n", &str, &n))
1049 return NULL;
1050 break;
1051 case 3:
1052 if (!PyArg_ParseTuple(args,"sil;str,n,attr", &str, &n, &attr))
1053 return NULL;
1054 use_attr = TRUE;
1055 break;
1056 case 4:
1057 if (!PyArg_ParseTuple(args,"iisi;y,x,str,n", &y, &x, &str, &n))
1058 return NULL;
1059 use_xy = TRUE;
1060 break;
1061 case 5:
1062 if (!PyArg_ParseTuple(args,"iisil;y,x,str,n,attr", &y, &x, &str, &n, &attr))
1063 return NULL;
1064 use_xy = use_attr = TRUE;
1065 break;
1066 default:
1067 PyErr_SetString(PyExc_TypeError, "insnstr requires 2 to 5 arguments");
1068 return NULL;
1071 if (use_attr == TRUE) {
1072 attr_old = getattrs(self->win);
1073 wattrset(self->win,attr);
1075 if (use_xy == TRUE)
1076 rtn = mvwinsnstr(self->win,y,x,str,n);
1077 else
1078 rtn = winsnstr(self->win,str,n);
1079 if (use_attr == TRUE)
1080 wattrset(self->win,attr_old);
1081 return PyCursesCheckERR(rtn, "insnstr");
1084 static PyObject *
1085 PyCursesWindow_Is_LineTouched(PyCursesWindowObject *self, PyObject *args)
1087 int line, erg;
1088 if (!PyArg_ParseTuple(args,"i;line", &line))
1089 return NULL;
1090 erg = is_linetouched(self->win, line);
1091 if (erg == ERR) {
1092 PyErr_SetString(PyExc_TypeError,
1093 "is_linetouched: line number outside of boundaries");
1094 return NULL;
1095 } else
1096 if (erg == FALSE) {
1097 Py_INCREF(Py_False);
1098 return Py_False;
1099 } else {
1100 Py_INCREF(Py_True);
1101 return Py_True;
1105 static PyObject *
1106 PyCursesWindow_NoOutRefresh(PyCursesWindowObject *self, PyObject *args)
1108 int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
1109 int rtn;
1111 #ifndef WINDOW_HAS_FLAGS
1112 if (0) {
1113 #else
1114 if (self->win->_flags & _ISPAD) {
1115 #endif
1116 switch(PyTuple_Size(args)) {
1117 case 6:
1118 if (!PyArg_ParseTuple(args,
1119 "iiiiii;" \
1120 "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
1121 &pminrow, &pmincol, &sminrow,
1122 &smincol, &smaxrow, &smaxcol))
1123 return NULL;
1124 Py_BEGIN_ALLOW_THREADS
1125 rtn = pnoutrefresh(self->win,
1126 pminrow, pmincol, sminrow,
1127 smincol, smaxrow, smaxcol);
1128 Py_END_ALLOW_THREADS
1129 return PyCursesCheckERR(rtn, "pnoutrefresh");
1130 default:
1131 PyErr_SetString(PyCursesError,
1132 "noutrefresh() called for a pad "
1133 "requires 6 arguments");
1134 return NULL;
1136 } else {
1137 if (!PyArg_ParseTuple(args, ":noutrefresh"))
1138 return NULL;
1140 Py_BEGIN_ALLOW_THREADS
1141 rtn = wnoutrefresh(self->win);
1142 Py_END_ALLOW_THREADS
1143 return PyCursesCheckERR(rtn, "wnoutrefresh");
1147 static PyObject *
1148 PyCursesWindow_Overlay(PyCursesWindowObject *self, PyObject *args)
1150 PyCursesWindowObject *temp;
1151 int use_copywin = FALSE;
1152 int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
1153 int rtn;
1155 switch (PyTuple_Size(args)) {
1156 case 1:
1157 if (!PyArg_ParseTuple(args, "O!;window object",
1158 &PyCursesWindow_Type, &temp))
1159 return NULL;
1160 break;
1161 case 7:
1162 if (!PyArg_ParseTuple(args, "(O!iiiiii);window object, int, int, int, int, int, int",
1163 &PyCursesWindow_Type, &temp, &sminrow, &smincol,
1164 &dminrow, &dmincol, &dmaxrow, &dmaxcol))
1165 return NULL;
1166 use_copywin = TRUE;
1167 break;
1168 default:
1169 PyErr_SetString(PyExc_TypeError,
1170 "overlay requires one or seven arguments");
1171 return NULL;
1174 if (use_copywin == TRUE) {
1175 rtn = copywin(self->win, temp->win, sminrow, smincol,
1176 dminrow, dmincol, dmaxrow, dmaxcol, TRUE);
1177 return PyCursesCheckERR(rtn, "copywin");
1179 else {
1180 rtn = overlay(self->win, temp->win);
1181 return PyCursesCheckERR(rtn, "overlay");
1185 static PyObject *
1186 PyCursesWindow_Overwrite(PyCursesWindowObject *self, PyObject *args)
1188 PyCursesWindowObject *temp;
1189 int use_copywin = FALSE;
1190 int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
1191 int rtn;
1193 switch (PyTuple_Size(args)) {
1194 case 1:
1195 if (!PyArg_ParseTuple(args, "O!;window object",
1196 &PyCursesWindow_Type, &temp))
1197 return NULL;
1198 break;
1199 case 7:
1200 if (!PyArg_ParseTuple(args, "(O!iiiiii);window object, int, int, int, int, int, int",
1201 &PyCursesWindow_Type, &temp, &sminrow, &smincol,
1202 &dminrow, &dmincol, &dmaxrow, &dmaxcol))
1203 return NULL;
1204 use_copywin = TRUE;
1205 break;
1206 default:
1207 PyErr_SetString(PyExc_TypeError,
1208 "overwrite requires one or seven arguments");
1209 return NULL;
1212 if (use_copywin == TRUE) {
1213 rtn = copywin(self->win, temp->win, sminrow, smincol,
1214 dminrow, dmincol, dmaxrow, dmaxcol, FALSE);
1215 return PyCursesCheckERR(rtn, "copywin");
1217 else {
1218 rtn = overwrite(self->win, temp->win);
1219 return PyCursesCheckERR(rtn, "overwrite");
1223 static PyObject *
1224 PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *args)
1226 PyObject *temp;
1228 if (!PyArg_ParseTuple(args, "O;fileobj", &temp))
1229 return NULL;
1230 if (!PyFile_Check(temp)) {
1231 PyErr_SetString(PyExc_TypeError, "argument must be a file object");
1232 return NULL;
1234 return PyCursesCheckERR(putwin(self->win, PyFile_AsFile(temp)),
1235 "putwin");
1238 static PyObject *
1239 PyCursesWindow_RedrawLine(PyCursesWindowObject *self, PyObject *args)
1241 int beg, num;
1242 if (!PyArg_ParseTuple(args,"ii;beg,num", &beg, &num))
1243 return NULL;
1244 return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln");
1247 static PyObject *
1248 PyCursesWindow_Refresh(PyCursesWindowObject *self, PyObject *args)
1250 int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
1251 int rtn;
1253 #ifndef WINDOW_HAS_FLAGS
1254 if (0) {
1255 #else
1256 if (self->win->_flags & _ISPAD) {
1257 #endif
1258 switch(PyTuple_Size(args)) {
1259 case 6:
1260 if (!PyArg_ParseTuple(args,
1261 "iiiiii;" \
1262 "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
1263 &pminrow, &pmincol, &sminrow,
1264 &smincol, &smaxrow, &smaxcol))
1265 return NULL;
1267 Py_BEGIN_ALLOW_THREADS
1268 rtn = prefresh(self->win,
1269 pminrow, pmincol, sminrow,
1270 smincol, smaxrow, smaxcol);
1271 Py_END_ALLOW_THREADS
1272 return PyCursesCheckERR(rtn, "prefresh");
1273 default:
1274 PyErr_SetString(PyCursesError,
1275 "refresh() for a pad requires 6 arguments");
1276 return NULL;
1278 } else {
1279 if (!PyArg_ParseTuple(args, ":refresh"))
1280 return NULL;
1281 Py_BEGIN_ALLOW_THREADS
1282 rtn = wrefresh(self->win);
1283 Py_END_ALLOW_THREADS
1284 return PyCursesCheckERR(rtn, "prefresh");
1288 static PyObject *
1289 PyCursesWindow_SetScrollRegion(PyCursesWindowObject *self, PyObject *args)
1291 int x, y;
1292 if (!PyArg_ParseTuple(args,"ii;top, bottom",&y,&x))
1293 return NULL;
1294 return PyCursesCheckERR(wsetscrreg(self->win,y,x), "wsetscrreg");
1297 static PyObject *
1298 PyCursesWindow_SubWin(PyCursesWindowObject *self, PyObject *args)
1300 WINDOW *win;
1301 int nlines, ncols, begin_y, begin_x;
1303 nlines = 0;
1304 ncols = 0;
1305 switch (PyTuple_Size(args)) {
1306 case 2:
1307 if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
1308 return NULL;
1309 break;
1310 case 4:
1311 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
1312 &nlines,&ncols,&begin_y,&begin_x))
1313 return NULL;
1314 break;
1315 default:
1316 PyErr_SetString(PyExc_TypeError, "subwin requires 2 or 4 arguments");
1317 return NULL;
1320 /* printf("Subwin: %i %i %i %i \n", nlines, ncols, begin_y, begin_x); */
1321 #ifdef WINDOW_HAS_FLAGS
1322 if (self->win->_flags & _ISPAD)
1323 win = subpad(self->win, nlines, ncols, begin_y, begin_x);
1324 else
1325 #endif
1326 win = subwin(self->win, nlines, ncols, begin_y, begin_x);
1328 if (win == NULL) {
1329 PyErr_SetString(PyCursesError, catchall_NULL);
1330 return NULL;
1333 return (PyObject *)PyCursesWindow_New(win);
1336 static PyObject *
1337 PyCursesWindow_Scroll(PyCursesWindowObject *self, PyObject *args)
1339 int nlines;
1340 switch(PyTuple_Size(args)) {
1341 case 0:
1342 return PyCursesCheckERR(scroll(self->win), "scroll");
1343 case 1:
1344 if (!PyArg_ParseTuple(args, "i;nlines", &nlines))
1345 return NULL;
1346 return PyCursesCheckERR(wscrl(self->win, nlines), "scroll");
1347 default:
1348 PyErr_SetString(PyExc_TypeError, "scroll requires 0 or 1 arguments");
1349 return NULL;
1353 static PyObject *
1354 PyCursesWindow_TouchLine(PyCursesWindowObject *self, PyObject *args)
1356 int st, cnt, val;
1357 switch (PyTuple_Size(args)) {
1358 case 2:
1359 if (!PyArg_ParseTuple(args,"ii;start,count",&st,&cnt))
1360 return NULL;
1361 return PyCursesCheckERR(touchline(self->win,st,cnt), "touchline");
1362 case 3:
1363 if (!PyArg_ParseTuple(args, "iii;start,count,val", &st, &cnt, &val))
1364 return NULL;
1365 return PyCursesCheckERR(wtouchln(self->win, st, cnt, val), "touchline");
1366 default:
1367 PyErr_SetString(PyExc_TypeError, "touchline requires 2 or 3 arguments");
1368 return NULL;
1372 static PyObject *
1373 PyCursesWindow_Vline(PyCursesWindowObject *self, PyObject *args)
1375 PyObject *temp;
1376 chtype ch;
1377 int n, x, y, code = OK;
1378 attr_t attr = A_NORMAL;
1380 switch (PyTuple_Size(args)) {
1381 case 2:
1382 if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
1383 return NULL;
1384 break;
1385 case 3:
1386 if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &attr))
1387 return NULL;
1388 break;
1389 case 4:
1390 if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
1391 return NULL;
1392 code = wmove(self->win, y, x);
1393 break;
1394 case 5:
1395 if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
1396 &y, &x, &temp, &n, &attr))
1397 return NULL;
1398 code = wmove(self->win, y, x);
1399 break;
1400 default:
1401 PyErr_SetString(PyExc_TypeError, "vline requires 2 to 5 arguments");
1402 return NULL;
1405 if (code != ERR) {
1406 if (!PyCurses_ConvertToChtype(temp, &ch)) {
1407 PyErr_SetString(PyExc_TypeError,
1408 "argument 1 or 3 must be a ch or an int");
1409 return NULL;
1411 return PyCursesCheckERR(wvline(self->win, ch | attr, n), "vline");
1412 } else
1413 return PyCursesCheckERR(code, "wmove");
1416 static PyMethodDef PyCursesWindow_Methods[] = {
1417 {"addch", (PyCFunction)PyCursesWindow_AddCh, METH_VARARGS},
1418 {"addnstr", (PyCFunction)PyCursesWindow_AddNStr, METH_VARARGS},
1419 {"addstr", (PyCFunction)PyCursesWindow_AddStr, METH_VARARGS},
1420 {"attroff", (PyCFunction)PyCursesWindow_wattroff, METH_VARARGS},
1421 {"attron", (PyCFunction)PyCursesWindow_wattron, METH_VARARGS},
1422 {"attrset", (PyCFunction)PyCursesWindow_wattrset, METH_VARARGS},
1423 {"bkgd", (PyCFunction)PyCursesWindow_Bkgd, METH_VARARGS},
1424 {"bkgdset", (PyCFunction)PyCursesWindow_BkgdSet, METH_VARARGS},
1425 {"border", (PyCFunction)PyCursesWindow_Border, METH_VARARGS},
1426 {"box", (PyCFunction)PyCursesWindow_Box, METH_VARARGS},
1427 {"clear", (PyCFunction)PyCursesWindow_wclear, METH_NOARGS},
1428 {"clearok", (PyCFunction)PyCursesWindow_clearok, METH_VARARGS},
1429 {"clrtobot", (PyCFunction)PyCursesWindow_wclrtobot, METH_NOARGS},
1430 {"clrtoeol", (PyCFunction)PyCursesWindow_wclrtoeol, METH_NOARGS},
1431 {"cursyncup", (PyCFunction)PyCursesWindow_wcursyncup, METH_NOARGS},
1432 {"delch", (PyCFunction)PyCursesWindow_DelCh, METH_VARARGS},
1433 {"deleteln", (PyCFunction)PyCursesWindow_wdeleteln, METH_NOARGS},
1434 {"derwin", (PyCFunction)PyCursesWindow_DerWin, METH_VARARGS},
1435 {"echochar", (PyCFunction)PyCursesWindow_EchoChar, METH_VARARGS},
1436 #ifdef NCURSES_MOUSE_VERSION
1437 {"enclose", (PyCFunction)PyCursesWindow_Enclose, METH_VARARGS},
1438 #endif
1439 {"erase", (PyCFunction)PyCursesWindow_werase, METH_NOARGS},
1440 {"getbegyx", (PyCFunction)PyCursesWindow_getbegyx, METH_NOARGS},
1441 {"getbkgd", (PyCFunction)PyCursesWindow_GetBkgd, METH_NOARGS},
1442 {"getch", (PyCFunction)PyCursesWindow_GetCh, METH_VARARGS},
1443 {"getkey", (PyCFunction)PyCursesWindow_GetKey, METH_VARARGS},
1444 {"getmaxyx", (PyCFunction)PyCursesWindow_getmaxyx, METH_NOARGS},
1445 {"getparyx", (PyCFunction)PyCursesWindow_getparyx, METH_NOARGS},
1446 {"getstr", (PyCFunction)PyCursesWindow_GetStr, METH_VARARGS},
1447 {"getyx", (PyCFunction)PyCursesWindow_getyx, METH_NOARGS},
1448 {"hline", (PyCFunction)PyCursesWindow_Hline, METH_VARARGS},
1449 {"idcok", (PyCFunction)PyCursesWindow_idcok, METH_VARARGS},
1450 {"idlok", (PyCFunction)PyCursesWindow_idlok, METH_VARARGS},
1451 {"immedok", (PyCFunction)PyCursesWindow_immedok, METH_VARARGS},
1452 {"inch", (PyCFunction)PyCursesWindow_InCh, METH_VARARGS},
1453 {"insch", (PyCFunction)PyCursesWindow_InsCh, METH_VARARGS},
1454 {"insdelln", (PyCFunction)PyCursesWindow_winsdelln, METH_VARARGS},
1455 {"insertln", (PyCFunction)PyCursesWindow_winsertln, METH_NOARGS},
1456 {"insnstr", (PyCFunction)PyCursesWindow_InsNStr, METH_VARARGS},
1457 {"insstr", (PyCFunction)PyCursesWindow_InsStr, METH_VARARGS},
1458 {"instr", (PyCFunction)PyCursesWindow_InStr, METH_VARARGS},
1459 {"is_linetouched", (PyCFunction)PyCursesWindow_Is_LineTouched, METH_VARARGS},
1460 {"is_wintouched", (PyCFunction)PyCursesWindow_is_wintouched, METH_NOARGS},
1461 {"keypad", (PyCFunction)PyCursesWindow_keypad, METH_VARARGS},
1462 {"leaveok", (PyCFunction)PyCursesWindow_leaveok, METH_VARARGS},
1463 {"move", (PyCFunction)PyCursesWindow_wmove, METH_VARARGS},
1464 {"mvderwin", (PyCFunction)PyCursesWindow_mvderwin, METH_VARARGS},
1465 {"mvwin", (PyCFunction)PyCursesWindow_mvwin, METH_VARARGS},
1466 {"nodelay", (PyCFunction)PyCursesWindow_nodelay, METH_VARARGS},
1467 {"notimeout", (PyCFunction)PyCursesWindow_notimeout, METH_VARARGS},
1468 {"noutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
1469 /* Backward compatibility alias -- remove in Python 2.3 */
1470 {"nooutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
1471 {"overlay", (PyCFunction)PyCursesWindow_Overlay, METH_VARARGS},
1472 {"overwrite", (PyCFunction)PyCursesWindow_Overwrite,
1473 METH_VARARGS},
1474 {"putwin", (PyCFunction)PyCursesWindow_PutWin, METH_VARARGS},
1475 {"redrawln", (PyCFunction)PyCursesWindow_RedrawLine},
1476 {"redrawwin", (PyCFunction)PyCursesWindow_redrawwin, METH_NOARGS},
1477 {"refresh", (PyCFunction)PyCursesWindow_Refresh, METH_VARARGS},
1478 #ifndef STRICT_SYSV_CURSES
1479 {"resize", (PyCFunction)PyCursesWindow_wresize, METH_VARARGS},
1480 #endif
1481 {"scroll", (PyCFunction)PyCursesWindow_Scroll, METH_VARARGS},
1482 {"scrollok", (PyCFunction)PyCursesWindow_scrollok, METH_VARARGS},
1483 {"setscrreg", (PyCFunction)PyCursesWindow_SetScrollRegion, METH_VARARGS},
1484 {"standend", (PyCFunction)PyCursesWindow_wstandend, METH_NOARGS},
1485 {"standout", (PyCFunction)PyCursesWindow_wstandout, METH_NOARGS},
1486 {"subpad", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
1487 {"subwin", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
1488 {"syncdown", (PyCFunction)PyCursesWindow_wsyncdown, METH_NOARGS},
1489 {"syncok", (PyCFunction)PyCursesWindow_syncok, METH_VARARGS},
1490 {"syncup", (PyCFunction)PyCursesWindow_wsyncup, METH_NOARGS},
1491 {"timeout", (PyCFunction)PyCursesWindow_wtimeout, METH_VARARGS},
1492 {"touchline", (PyCFunction)PyCursesWindow_TouchLine, METH_VARARGS},
1493 {"touchwin", (PyCFunction)PyCursesWindow_touchwin, METH_NOARGS},
1494 {"untouchwin", (PyCFunction)PyCursesWindow_untouchwin, METH_NOARGS},
1495 {"vline", (PyCFunction)PyCursesWindow_Vline, METH_VARARGS},
1496 {NULL, NULL} /* sentinel */
1499 static PyObject *
1500 PyCursesWindow_GetAttr(PyCursesWindowObject *self, char *name)
1502 return Py_FindMethod(PyCursesWindow_Methods, (PyObject *)self, name);
1505 /* -------------------------------------------------------*/
1507 PyTypeObject PyCursesWindow_Type = {
1508 PyObject_HEAD_INIT(NULL)
1509 0, /*ob_size*/
1510 "_curses.curses window", /*tp_name*/
1511 sizeof(PyCursesWindowObject), /*tp_basicsize*/
1512 0, /*tp_itemsize*/
1513 /* methods */
1514 (destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/
1515 0, /*tp_print*/
1516 (getattrfunc)PyCursesWindow_GetAttr, /*tp_getattr*/
1517 (setattrfunc)0, /*tp_setattr*/
1518 0, /*tp_compare*/
1519 0, /*tp_repr*/
1520 0, /*tp_as_number*/
1521 0, /*tp_as_sequence*/
1522 0, /*tp_as_mapping*/
1523 0, /*tp_hash*/
1526 /*********************************************************************
1527 Global Functions
1528 **********************************************************************/
1530 NoArgNoReturnFunction(beep)
1531 NoArgNoReturnFunction(def_prog_mode)
1532 NoArgNoReturnFunction(def_shell_mode)
1533 NoArgNoReturnFunction(doupdate)
1534 NoArgNoReturnFunction(endwin)
1535 NoArgNoReturnFunction(flash)
1536 NoArgNoReturnFunction(nocbreak)
1537 NoArgNoReturnFunction(noecho)
1538 NoArgNoReturnFunction(nonl)
1539 NoArgNoReturnFunction(noraw)
1540 NoArgNoReturnFunction(reset_prog_mode)
1541 NoArgNoReturnFunction(reset_shell_mode)
1542 NoArgNoReturnFunction(resetty)
1543 NoArgNoReturnFunction(savetty)
1545 NoArgOrFlagNoReturnFunction(cbreak)
1546 NoArgOrFlagNoReturnFunction(echo)
1547 NoArgOrFlagNoReturnFunction(nl)
1548 NoArgOrFlagNoReturnFunction(raw)
1550 NoArgReturnIntFunction(baudrate)
1551 NoArgReturnIntFunction(termattrs)
1553 NoArgReturnStringFunction(termname)
1554 NoArgReturnStringFunction(longname)
1556 NoArgTrueFalseFunction(can_change_color)
1557 NoArgTrueFalseFunction(has_colors)
1558 NoArgTrueFalseFunction(has_ic)
1559 NoArgTrueFalseFunction(has_il)
1560 NoArgTrueFalseFunction(isendwin)
1561 NoArgNoReturnVoidFunction(filter)
1562 NoArgNoReturnVoidFunction(flushinp)
1563 NoArgNoReturnVoidFunction(noqiflush)
1565 static PyObject *
1566 PyCurses_Color_Content(PyObject *self, PyObject *args)
1568 short color,r,g,b;
1570 PyCursesInitialised
1571 PyCursesInitialisedColor
1573 if (!PyArg_ParseTuple(args, "h:color_content", &color)) return NULL;
1575 if (color_content(color, &r, &g, &b) != ERR)
1576 return Py_BuildValue("(iii)", r, g, b);
1577 else {
1578 PyErr_SetString(PyCursesError,
1579 "Argument 1 was out of range. Check value of COLORS.");
1580 return NULL;
1584 static PyObject *
1585 PyCurses_color_pair(PyObject *self, PyObject *args)
1587 int n;
1589 PyCursesInitialised
1590 PyCursesInitialisedColor
1592 if (!PyArg_ParseTuple(args, "i:color_pair", &n)) return NULL;
1593 return PyInt_FromLong((long) (n << 8));
1596 static PyObject *
1597 PyCurses_Curs_Set(PyObject *self, PyObject *args)
1599 int vis,erg;
1601 PyCursesInitialised
1603 if (!PyArg_ParseTuple(args, "i:curs_set", &vis)) return NULL;
1605 erg = curs_set(vis);
1606 if (erg == ERR) return PyCursesCheckERR(erg, "curs_set");
1608 return PyInt_FromLong((long) erg);
1611 static PyObject *
1612 PyCurses_Delay_Output(PyObject *self, PyObject *args)
1614 int ms;
1616 PyCursesInitialised
1618 if (!PyArg_ParseTuple(args, "i:delay_output", &ms)) return NULL;
1620 return PyCursesCheckERR(delay_output(ms), "delay_output");
1623 static PyObject *
1624 PyCurses_EraseChar(PyObject *self)
1626 char ch;
1628 PyCursesInitialised
1630 ch = erasechar();
1632 return PyString_FromStringAndSize(&ch, 1);
1635 static PyObject *
1636 PyCurses_getsyx(PyObject *self)
1638 int x,y;
1640 PyCursesInitialised
1642 getsyx(y, x);
1644 return Py_BuildValue("(ii)", y, x);
1647 #ifdef NCURSES_MOUSE_VERSION
1648 static PyObject *
1649 PyCurses_GetMouse(PyObject *self)
1651 int rtn;
1652 MEVENT event;
1654 PyCursesInitialised
1656 rtn = getmouse( &event );
1657 if (rtn == ERR) {
1658 PyErr_SetString(PyCursesError, "getmouse() returned ERR");
1659 return NULL;
1661 return Py_BuildValue("(hiiil)",
1662 (short)event.id,
1663 event.x, event.y, event.z,
1664 (long) event.bstate);
1667 static PyObject *
1668 PyCurses_UngetMouse(PyObject *self, PyObject *args)
1670 MEVENT event;
1672 PyCursesInitialised
1673 if (!PyArg_ParseTuple(args, "(hiiil)",
1674 &event.id,
1675 &event.x, &event.y, &event.z,
1676 (int *) &event.bstate))
1677 return NULL;
1679 return PyCursesCheckERR(ungetmouse(&event), "ungetmouse");
1681 #endif
1683 static PyObject *
1684 PyCurses_GetWin(PyCursesWindowObject *self, PyObject *temp)
1686 WINDOW *win;
1688 PyCursesInitialised
1690 if (!PyFile_Check(temp)) {
1691 PyErr_SetString(PyExc_TypeError, "argument must be a file object");
1692 return NULL;
1695 win = getwin(PyFile_AsFile(temp));
1697 if (win == NULL) {
1698 PyErr_SetString(PyCursesError, catchall_NULL);
1699 return NULL;
1702 return PyCursesWindow_New(win);
1705 static PyObject *
1706 PyCurses_HalfDelay(PyObject *self, PyObject *args)
1708 unsigned char tenths;
1710 PyCursesInitialised
1712 if (!PyArg_ParseTuple(args, "b:halfdelay", &tenths)) return NULL;
1714 return PyCursesCheckERR(halfdelay(tenths), "halfdelay");
1717 #ifndef STRICT_SYSV_CURSES
1718 /* No has_key! */
1719 static PyObject * PyCurses_has_key(PyObject *self, PyObject *args)
1721 int ch;
1723 PyCursesInitialised
1725 if (!PyArg_ParseTuple(args,"i",&ch)) return NULL;
1727 if (has_key(ch) == FALSE) {
1728 Py_INCREF(Py_False);
1729 return Py_False;
1731 Py_INCREF(Py_True);
1732 return Py_True;
1734 #endif /* STRICT_SYSV_CURSES */
1736 static PyObject *
1737 PyCurses_Init_Color(PyObject *self, PyObject *args)
1739 short color, r, g, b;
1741 PyCursesInitialised
1742 PyCursesInitialisedColor
1744 switch(PyTuple_Size(args)) {
1745 case 4:
1746 if (!PyArg_ParseTuple(args, "hhhh;color,r,g,b", &color, &r, &g, &b)) return NULL;
1747 break;
1748 default:
1749 PyErr_SetString(PyExc_TypeError, "init_color requires 4 arguments");
1750 return NULL;
1753 return PyCursesCheckERR(init_color(color, r, g, b), "init_color");
1756 static PyObject *
1757 PyCurses_Init_Pair(PyObject *self, PyObject *args)
1759 short pair, f, b;
1761 PyCursesInitialised
1762 PyCursesInitialisedColor
1764 if (PyTuple_Size(args) != 3) {
1765 PyErr_SetString(PyExc_TypeError, "init_pair requires 3 arguments");
1766 return NULL;
1769 if (!PyArg_ParseTuple(args, "hhh;pair, f, b", &pair, &f, &b)) return NULL;
1771 return PyCursesCheckERR(init_pair(pair, f, b), "init_pair");
1774 static PyObject *ModDict;
1776 static PyObject *
1777 PyCurses_InitScr(PyObject *self)
1779 WINDOW *win;
1780 PyObject *nlines, *cols;
1782 if (initialised == TRUE) {
1783 wrefresh(stdscr);
1784 return (PyObject *)PyCursesWindow_New(stdscr);
1787 win = initscr();
1789 if (win == NULL) {
1790 PyErr_SetString(PyCursesError, catchall_NULL);
1791 return NULL;
1794 initialised = initialised_setupterm = TRUE;
1796 /* This was moved from initcurses() because it core dumped on SGI,
1797 where they're not defined until you've called initscr() */
1798 #define SetDictInt(string,ch) \
1799 PyDict_SetItemString(ModDict,string,PyInt_FromLong((long) (ch)));
1801 /* Here are some graphic symbols you can use */
1802 SetDictInt("ACS_ULCORNER", (ACS_ULCORNER));
1803 SetDictInt("ACS_LLCORNER", (ACS_LLCORNER));
1804 SetDictInt("ACS_URCORNER", (ACS_URCORNER));
1805 SetDictInt("ACS_LRCORNER", (ACS_LRCORNER));
1806 SetDictInt("ACS_LTEE", (ACS_LTEE));
1807 SetDictInt("ACS_RTEE", (ACS_RTEE));
1808 SetDictInt("ACS_BTEE", (ACS_BTEE));
1809 SetDictInt("ACS_TTEE", (ACS_TTEE));
1810 SetDictInt("ACS_HLINE", (ACS_HLINE));
1811 SetDictInt("ACS_VLINE", (ACS_VLINE));
1812 SetDictInt("ACS_PLUS", (ACS_PLUS));
1813 #if !defined(__hpux) || defined(HAVE_NCURSES_H)
1814 /* On HP/UX 11, these are of type cchar_t, which is not an
1815 integral type. If this is a problem on more platforms, a
1816 configure test should be added to determine whether ACS_S1
1817 is of integral type. */
1818 SetDictInt("ACS_S1", (ACS_S1));
1819 SetDictInt("ACS_S9", (ACS_S9));
1820 SetDictInt("ACS_DIAMOND", (ACS_DIAMOND));
1821 SetDictInt("ACS_CKBOARD", (ACS_CKBOARD));
1822 SetDictInt("ACS_DEGREE", (ACS_DEGREE));
1823 SetDictInt("ACS_PLMINUS", (ACS_PLMINUS));
1824 SetDictInt("ACS_BULLET", (ACS_BULLET));
1825 SetDictInt("ACS_LARROW", (ACS_LARROW));
1826 SetDictInt("ACS_RARROW", (ACS_RARROW));
1827 SetDictInt("ACS_DARROW", (ACS_DARROW));
1828 SetDictInt("ACS_UARROW", (ACS_UARROW));
1829 SetDictInt("ACS_BOARD", (ACS_BOARD));
1830 SetDictInt("ACS_LANTERN", (ACS_LANTERN));
1831 SetDictInt("ACS_BLOCK", (ACS_BLOCK));
1832 #endif
1833 SetDictInt("ACS_BSSB", (ACS_ULCORNER));
1834 SetDictInt("ACS_SSBB", (ACS_LLCORNER));
1835 SetDictInt("ACS_BBSS", (ACS_URCORNER));
1836 SetDictInt("ACS_SBBS", (ACS_LRCORNER));
1837 SetDictInt("ACS_SBSS", (ACS_RTEE));
1838 SetDictInt("ACS_SSSB", (ACS_LTEE));
1839 SetDictInt("ACS_SSBS", (ACS_BTEE));
1840 SetDictInt("ACS_BSSS", (ACS_TTEE));
1841 SetDictInt("ACS_BSBS", (ACS_HLINE));
1842 SetDictInt("ACS_SBSB", (ACS_VLINE));
1843 SetDictInt("ACS_SSSS", (ACS_PLUS));
1845 /* The following are never available with strict SYSV curses */
1846 #ifdef ACS_S3
1847 SetDictInt("ACS_S3", (ACS_S3));
1848 #endif
1849 #ifdef ACS_S7
1850 SetDictInt("ACS_S7", (ACS_S7));
1851 #endif
1852 #ifdef ACS_LEQUAL
1853 SetDictInt("ACS_LEQUAL", (ACS_LEQUAL));
1854 #endif
1855 #ifdef ACS_GEQUAL
1856 SetDictInt("ACS_GEQUAL", (ACS_GEQUAL));
1857 #endif
1858 #ifdef ACS_PI
1859 SetDictInt("ACS_PI", (ACS_PI));
1860 #endif
1861 #ifdef ACS_NEQUAL
1862 SetDictInt("ACS_NEQUAL", (ACS_NEQUAL));
1863 #endif
1864 #ifdef ACS_STERLING
1865 SetDictInt("ACS_STERLING", (ACS_STERLING));
1866 #endif
1868 nlines = PyInt_FromLong((long) LINES);
1869 PyDict_SetItemString(ModDict, "LINES", nlines);
1870 Py_DECREF(nlines);
1871 cols = PyInt_FromLong((long) COLS);
1872 PyDict_SetItemString(ModDict, "COLS", cols);
1873 Py_DECREF(cols);
1875 return (PyObject *)PyCursesWindow_New(win);
1878 static PyObject *
1879 PyCurses_setupterm(PyObject* self, PyObject *args, PyObject* keywds)
1881 int fd = -1;
1882 int err;
1883 char* termstr = NULL;
1885 static char *kwlist[] = {"term", "fd", NULL};
1887 if (!PyArg_ParseTupleAndKeywords(
1888 args,keywds,"|zi:setupterm",kwlist,&termstr,&fd)) {
1889 return NULL;
1892 if (fd == -1) {
1893 PyObject* sys_stdout;
1895 sys_stdout = PySys_GetObject("stdout");
1897 if (sys_stdout == NULL) {
1898 PyErr_SetString(
1899 PyCursesError,
1900 "lost sys.stdout");
1901 return NULL;
1904 fd = PyObject_AsFileDescriptor(sys_stdout);
1906 if (fd == -1) {
1907 return NULL;
1911 if (setupterm(termstr,fd,&err) == ERR) {
1912 char* s = "setupterm: unknown error";
1914 if (err == 0) {
1915 s = "setupterm: could not find terminal";
1916 } else if (err == -1) {
1917 s = "setupterm: could not find terminfo database";
1920 PyErr_SetString(PyCursesError,s);
1921 return NULL;
1924 initialised_setupterm = TRUE;
1926 Py_INCREF(Py_None);
1927 return Py_None;
1930 static PyObject *
1931 PyCurses_IntrFlush(PyObject *self, PyObject *args)
1933 int ch;
1935 PyCursesInitialised
1937 switch(PyTuple_Size(args)) {
1938 case 1:
1939 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL;
1940 break;
1941 default:
1942 PyErr_SetString(PyExc_TypeError, "intrflush requires 1 argument");
1943 return NULL;
1946 return PyCursesCheckERR(intrflush(NULL,ch), "intrflush");
1949 #if !defined(__NetBSD__)
1950 static PyObject *
1951 PyCurses_KeyName(PyObject *self, PyObject *args)
1953 const char *knp;
1954 int ch;
1956 PyCursesInitialised
1958 if (!PyArg_ParseTuple(args,"i",&ch)) return NULL;
1960 if (ch < 0) {
1961 PyErr_SetString(PyExc_ValueError, "invalid key number");
1962 return NULL;
1964 knp = keyname(ch);
1966 return PyString_FromString((knp == NULL) ? "" : (char *)knp);
1968 #endif
1970 static PyObject *
1971 PyCurses_KillChar(PyObject *self)
1973 char ch;
1975 ch = killchar();
1977 return PyString_FromStringAndSize(&ch, 1);
1980 static PyObject *
1981 PyCurses_Meta(PyObject *self, PyObject *args)
1983 int ch;
1985 PyCursesInitialised
1987 switch(PyTuple_Size(args)) {
1988 case 1:
1989 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL;
1990 break;
1991 default:
1992 PyErr_SetString(PyExc_TypeError, "meta requires 1 argument");
1993 return NULL;
1996 return PyCursesCheckERR(meta(stdscr, ch), "meta");
1999 #ifdef NCURSES_MOUSE_VERSION
2000 static PyObject *
2001 PyCurses_MouseInterval(PyObject *self, PyObject *args)
2003 int interval;
2004 PyCursesInitialised
2006 if (!PyArg_ParseTuple(args,"i;interval",&interval))
2007 return NULL;
2008 return PyCursesCheckERR(mouseinterval(interval), "mouseinterval");
2011 static PyObject *
2012 PyCurses_MouseMask(PyObject *self, PyObject *args)
2014 int newmask;
2015 mmask_t oldmask, availmask;
2017 PyCursesInitialised
2018 if (!PyArg_ParseTuple(args,"i;mousemask",&newmask))
2019 return NULL;
2020 availmask = mousemask(newmask, &oldmask);
2021 return Py_BuildValue("(ll)", (long)availmask, (long)oldmask);
2023 #endif
2025 static PyObject *
2026 PyCurses_Napms(PyObject *self, PyObject *args)
2028 int ms;
2030 PyCursesInitialised
2031 if (!PyArg_ParseTuple(args, "i;ms", &ms)) return NULL;
2033 return Py_BuildValue("i", napms(ms));
2037 static PyObject *
2038 PyCurses_NewPad(PyObject *self, PyObject *args)
2040 WINDOW *win;
2041 int nlines, ncols;
2043 PyCursesInitialised
2045 if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols)) return NULL;
2047 win = newpad(nlines, ncols);
2049 if (win == NULL) {
2050 PyErr_SetString(PyCursesError, catchall_NULL);
2051 return NULL;
2054 return (PyObject *)PyCursesWindow_New(win);
2057 static PyObject *
2058 PyCurses_NewWindow(PyObject *self, PyObject *args)
2060 WINDOW *win;
2061 int nlines, ncols, begin_y=0, begin_x=0;
2063 PyCursesInitialised
2065 switch (PyTuple_Size(args)) {
2066 case 2:
2067 if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols))
2068 return NULL;
2069 break;
2070 case 4:
2071 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
2072 &nlines,&ncols,&begin_y,&begin_x))
2073 return NULL;
2074 break;
2075 default:
2076 PyErr_SetString(PyExc_TypeError, "newwin requires 2 or 4 arguments");
2077 return NULL;
2080 win = newwin(nlines,ncols,begin_y,begin_x);
2081 if (win == NULL) {
2082 PyErr_SetString(PyCursesError, catchall_NULL);
2083 return NULL;
2086 return (PyObject *)PyCursesWindow_New(win);
2089 static PyObject *
2090 PyCurses_Pair_Content(PyObject *self, PyObject *args)
2092 short pair,f,b;
2094 PyCursesInitialised
2095 PyCursesInitialisedColor
2097 switch(PyTuple_Size(args)) {
2098 case 1:
2099 if (!PyArg_ParseTuple(args, "h;pair", &pair)) return NULL;
2100 break;
2101 default:
2102 PyErr_SetString(PyExc_TypeError, "pair_content requires 1 argument");
2103 return NULL;
2106 if (!pair_content(pair, &f, &b)) {
2107 PyErr_SetString(PyCursesError,
2108 "Argument 1 was out of range. (1..COLOR_PAIRS-1)");
2109 return NULL;
2112 return Py_BuildValue("(ii)", f, b);
2115 static PyObject *
2116 PyCurses_pair_number(PyObject *self, PyObject *args)
2118 int n;
2120 PyCursesInitialised
2121 PyCursesInitialisedColor
2123 switch(PyTuple_Size(args)) {
2124 case 1:
2125 if (!PyArg_ParseTuple(args, "i;pairvalue", &n)) return NULL;
2126 break;
2127 default:
2128 PyErr_SetString(PyExc_TypeError,
2129 "pair_number requires 1 argument");
2130 return NULL;
2133 return PyInt_FromLong((long) ((n & A_COLOR) >> 8));
2136 static PyObject *
2137 PyCurses_Putp(PyObject *self, PyObject *args)
2139 char *str;
2141 if (!PyArg_ParseTuple(args,"s;str", &str)) return NULL;
2142 return PyCursesCheckERR(putp(str), "putp");
2145 static PyObject *
2146 PyCurses_QiFlush(PyObject *self, PyObject *args)
2148 int flag = 0;
2150 PyCursesInitialised
2152 switch(PyTuple_Size(args)) {
2153 case 0:
2154 qiflush();
2155 Py_INCREF(Py_None);
2156 return Py_None;
2157 case 1:
2158 if (!PyArg_ParseTuple(args, "i;True(1) or False(0)", &flag)) return NULL;
2159 if (flag) qiflush();
2160 else noqiflush();
2161 Py_INCREF(Py_None);
2162 return Py_None;
2163 default:
2164 PyErr_SetString(PyExc_TypeError, "qiflush requires 0 or 1 arguments");
2165 return NULL;
2169 static PyObject *
2170 PyCurses_setsyx(PyObject *self, PyObject *args)
2172 int y,x;
2174 PyCursesInitialised
2176 if (PyTuple_Size(args)!=2) {
2177 PyErr_SetString(PyExc_TypeError, "setsyx requires 2 arguments");
2178 return NULL;
2181 if (!PyArg_ParseTuple(args, "ii;y, x", &y, &x)) return NULL;
2183 setsyx(y,x);
2185 Py_INCREF(Py_None);
2186 return Py_None;
2189 static PyObject *
2190 PyCurses_Start_Color(PyObject *self)
2192 int code;
2193 PyObject *c, *cp;
2195 PyCursesInitialised
2197 code = start_color();
2198 if (code != ERR) {
2199 initialisedcolors = TRUE;
2200 c = PyInt_FromLong((long) COLORS);
2201 PyDict_SetItemString(ModDict, "COLORS", c);
2202 Py_DECREF(c);
2203 cp = PyInt_FromLong((long) COLOR_PAIRS);
2204 PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp);
2205 Py_DECREF(cp);
2206 Py_INCREF(Py_None);
2207 return Py_None;
2208 } else {
2209 PyErr_SetString(PyCursesError, "start_color() returned ERR");
2210 return NULL;
2214 static PyObject *
2215 PyCurses_tigetflag(PyObject *self, PyObject *args)
2217 char *capname;
2219 PyCursesSetupTermCalled;
2221 if (!PyArg_ParseTuple(args, "z", &capname))
2222 return NULL;
2224 return PyInt_FromLong( (long) tigetflag( capname ) );
2227 static PyObject *
2228 PyCurses_tigetnum(PyObject *self, PyObject *args)
2230 char *capname;
2232 PyCursesSetupTermCalled;
2234 if (!PyArg_ParseTuple(args, "z", &capname))
2235 return NULL;
2237 return PyInt_FromLong( (long) tigetnum( capname ) );
2240 static PyObject *
2241 PyCurses_tigetstr(PyObject *self, PyObject *args)
2243 char *capname;
2245 PyCursesSetupTermCalled;
2247 if (!PyArg_ParseTuple(args, "z", &capname))
2248 return NULL;
2250 capname = tigetstr( capname );
2251 if (capname == 0 || capname == (char*) -1) {
2252 Py_INCREF(Py_None);
2253 return Py_None;
2255 return PyString_FromString( capname );
2258 static PyObject *
2259 PyCurses_tparm(PyObject *self, PyObject *args)
2261 char* fmt;
2262 char* result = NULL;
2263 int i1=0,i2=0,i3=0,i4=0,i5=0,i6=0,i7=0,i8=0,i9=0;
2265 PyCursesSetupTermCalled;
2267 if (!PyArg_ParseTuple(args, "s|iiiiiiiii:tparm",
2268 &fmt, &i1, &i2, &i3, &i4,
2269 &i5, &i6, &i7, &i8, &i9)) {
2270 return NULL;
2273 result = tparm(fmt,i1,i2,i3,i4,i5,i6,i7,i8,i9);
2275 return PyString_FromString(result);
2278 static PyObject *
2279 PyCurses_TypeAhead(PyObject *self, PyObject *args)
2281 int fd;
2283 PyCursesInitialised
2285 if (!PyArg_ParseTuple(args,"i;fd",&fd)) return NULL;
2287 PyCursesCheckERR(typeahead( fd ), "typeahead");
2288 Py_INCREF(Py_None);
2289 return Py_None;
2292 static PyObject *
2293 PyCurses_UnCtrl(PyObject *self, PyObject *args)
2295 PyObject *temp;
2296 chtype ch;
2298 PyCursesInitialised
2300 if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
2302 if (PyInt_Check(temp))
2303 ch = (chtype) PyInt_AsLong(temp);
2304 else if (PyString_Check(temp))
2305 ch = (chtype) *PyString_AsString(temp);
2306 else {
2307 PyErr_SetString(PyExc_TypeError, "argument must be a ch or an int");
2308 return NULL;
2311 return PyString_FromString(unctrl(ch));
2314 static PyObject *
2315 PyCurses_UngetCh(PyObject *self, PyObject *args)
2317 PyObject *temp;
2318 int ch;
2320 PyCursesInitialised
2322 if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
2324 if (PyInt_Check(temp))
2325 ch = (int) PyInt_AsLong(temp);
2326 else if (PyString_Check(temp))
2327 ch = (int) *PyString_AsString(temp);
2328 else {
2329 PyErr_SetString(PyExc_TypeError, "argument must be a ch or an int");
2330 return NULL;
2333 return PyCursesCheckERR(ungetch(ch), "ungetch");
2336 static PyObject *
2337 PyCurses_Use_Env(PyObject *self, PyObject *args)
2339 int flag;
2341 PyCursesInitialised
2343 switch(PyTuple_Size(args)) {
2344 case 1:
2345 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&flag))
2346 return NULL;
2347 break;
2348 default:
2349 PyErr_SetString(PyExc_TypeError, "use_env requires 1 argument");
2350 return NULL;
2352 use_env(flag);
2353 Py_INCREF(Py_None);
2354 return Py_None;
2357 /* List of functions defined in the module */
2359 static PyMethodDef PyCurses_methods[] = {
2360 {"baudrate", (PyCFunction)PyCurses_baudrate, METH_NOARGS},
2361 {"beep", (PyCFunction)PyCurses_beep, METH_NOARGS},
2362 {"can_change_color", (PyCFunction)PyCurses_can_change_color, METH_NOARGS},
2363 {"cbreak", (PyCFunction)PyCurses_cbreak, METH_VARARGS},
2364 {"color_content", (PyCFunction)PyCurses_Color_Content, METH_VARARGS},
2365 {"color_pair", (PyCFunction)PyCurses_color_pair, METH_VARARGS},
2366 {"curs_set", (PyCFunction)PyCurses_Curs_Set, METH_VARARGS},
2367 {"def_prog_mode", (PyCFunction)PyCurses_def_prog_mode, METH_NOARGS},
2368 {"def_shell_mode", (PyCFunction)PyCurses_def_shell_mode, METH_NOARGS},
2369 {"delay_output", (PyCFunction)PyCurses_Delay_Output, METH_VARARGS},
2370 {"doupdate", (PyCFunction)PyCurses_doupdate, METH_NOARGS},
2371 {"echo", (PyCFunction)PyCurses_echo, METH_VARARGS},
2372 {"endwin", (PyCFunction)PyCurses_endwin, METH_NOARGS},
2373 {"erasechar", (PyCFunction)PyCurses_EraseChar, METH_NOARGS},
2374 {"filter", (PyCFunction)PyCurses_filter, METH_NOARGS},
2375 {"flash", (PyCFunction)PyCurses_flash, METH_NOARGS},
2376 {"flushinp", (PyCFunction)PyCurses_flushinp, METH_NOARGS},
2377 #ifdef NCURSES_MOUSE_VERSION
2378 {"getmouse", (PyCFunction)PyCurses_GetMouse, METH_NOARGS},
2379 {"ungetmouse", (PyCFunction)PyCurses_UngetMouse, METH_VARARGS},
2380 #endif
2381 {"getsyx", (PyCFunction)PyCurses_getsyx, METH_NOARGS},
2382 {"getwin", (PyCFunction)PyCurses_GetWin, METH_O},
2383 {"has_colors", (PyCFunction)PyCurses_has_colors, METH_NOARGS},
2384 {"has_ic", (PyCFunction)PyCurses_has_ic, METH_NOARGS},
2385 {"has_il", (PyCFunction)PyCurses_has_il, METH_NOARGS},
2386 #ifndef STRICT_SYSV_CURSES
2387 {"has_key", (PyCFunction)PyCurses_has_key, METH_VARARGS},
2388 #endif
2389 {"halfdelay", (PyCFunction)PyCurses_HalfDelay, METH_VARARGS},
2390 {"init_color", (PyCFunction)PyCurses_Init_Color, METH_VARARGS},
2391 {"init_pair", (PyCFunction)PyCurses_Init_Pair, METH_VARARGS},
2392 {"initscr", (PyCFunction)PyCurses_InitScr, METH_NOARGS},
2393 {"intrflush", (PyCFunction)PyCurses_IntrFlush, METH_VARARGS},
2394 {"isendwin", (PyCFunction)PyCurses_isendwin, METH_NOARGS},
2395 #if !defined(__NetBSD__)
2396 {"keyname", (PyCFunction)PyCurses_KeyName, METH_VARARGS},
2397 #endif
2398 {"killchar", (PyCFunction)PyCurses_KillChar, METH_NOARGS},
2399 {"longname", (PyCFunction)PyCurses_longname, METH_NOARGS},
2400 {"meta", (PyCFunction)PyCurses_Meta, METH_VARARGS},
2401 #ifdef NCURSES_MOUSE_VERSION
2402 {"mouseinterval", (PyCFunction)PyCurses_MouseInterval, METH_VARARGS},
2403 {"mousemask", (PyCFunction)PyCurses_MouseMask, METH_VARARGS},
2404 #endif
2405 {"napms", (PyCFunction)PyCurses_Napms, METH_VARARGS},
2406 {"newpad", (PyCFunction)PyCurses_NewPad, METH_VARARGS},
2407 {"newwin", (PyCFunction)PyCurses_NewWindow, METH_VARARGS},
2408 {"nl", (PyCFunction)PyCurses_nl, METH_VARARGS},
2409 {"nocbreak", (PyCFunction)PyCurses_nocbreak, METH_NOARGS},
2410 {"noecho", (PyCFunction)PyCurses_noecho, METH_NOARGS},
2411 {"nonl", (PyCFunction)PyCurses_nonl, METH_NOARGS},
2412 {"noqiflush", (PyCFunction)PyCurses_noqiflush, METH_NOARGS},
2413 {"noraw", (PyCFunction)PyCurses_noraw, METH_NOARGS},
2414 {"pair_content", (PyCFunction)PyCurses_Pair_Content, METH_VARARGS},
2415 {"pair_number", (PyCFunction)PyCurses_pair_number, METH_VARARGS},
2416 {"putp", (PyCFunction)PyCurses_Putp, METH_VARARGS},
2417 {"qiflush", (PyCFunction)PyCurses_QiFlush, METH_VARARGS},
2418 {"raw", (PyCFunction)PyCurses_raw, METH_VARARGS},
2419 {"reset_prog_mode", (PyCFunction)PyCurses_reset_prog_mode, METH_NOARGS},
2420 {"reset_shell_mode", (PyCFunction)PyCurses_reset_shell_mode, METH_NOARGS},
2421 {"resetty", (PyCFunction)PyCurses_resetty, METH_NOARGS},
2422 {"savetty", (PyCFunction)PyCurses_savetty, METH_NOARGS},
2423 {"setsyx", (PyCFunction)PyCurses_setsyx, METH_VARARGS},
2424 {"setupterm", (PyCFunction)PyCurses_setupterm,
2425 METH_VARARGS|METH_KEYWORDS},
2426 {"start_color", (PyCFunction)PyCurses_Start_Color, METH_NOARGS},
2427 {"termattrs", (PyCFunction)PyCurses_termattrs, METH_NOARGS},
2428 {"termname", (PyCFunction)PyCurses_termname, METH_NOARGS},
2429 {"tigetflag", (PyCFunction)PyCurses_tigetflag, METH_VARARGS},
2430 {"tigetnum", (PyCFunction)PyCurses_tigetnum, METH_VARARGS},
2431 {"tigetstr", (PyCFunction)PyCurses_tigetstr, METH_VARARGS},
2432 {"tparm", (PyCFunction)PyCurses_tparm, METH_VARARGS},
2433 {"typeahead", (PyCFunction)PyCurses_TypeAhead, METH_VARARGS},
2434 {"unctrl", (PyCFunction)PyCurses_UnCtrl, METH_VARARGS},
2435 {"ungetch", (PyCFunction)PyCurses_UngetCh, METH_VARARGS},
2436 {"use_env", (PyCFunction)PyCurses_Use_Env, METH_VARARGS},
2437 {NULL, NULL} /* sentinel */
2440 /* Initialization function for the module */
2442 PyMODINIT_FUNC
2443 init_curses(void)
2445 PyObject *m, *d, *v, *c_api_object;
2446 static void *PyCurses_API[PyCurses_API_pointers];
2448 /* Initialize object type */
2449 PyCursesWindow_Type.ob_type = &PyType_Type;
2451 /* Initialize the C API pointer array */
2452 PyCurses_API[0] = (void *)&PyCursesWindow_Type;
2453 PyCurses_API[1] = (void *)func_PyCursesSetupTermCalled;
2454 PyCurses_API[2] = (void *)func_PyCursesInitialised;
2455 PyCurses_API[3] = (void *)func_PyCursesInitialisedColor;
2457 /* Create the module and add the functions */
2458 m = Py_InitModule("_curses", PyCurses_methods);
2460 /* Add some symbolic constants to the module */
2461 d = PyModule_GetDict(m);
2462 ModDict = d; /* For PyCurses_InitScr to use later */
2464 /* Add a CObject for the C API */
2465 c_api_object = PyCObject_FromVoidPtr((void *)PyCurses_API, NULL);
2466 PyDict_SetItemString(d, "_C_API", c_api_object);
2467 Py_DECREF(c_api_object);
2469 /* For exception curses.error */
2470 PyCursesError = PyErr_NewException("_curses.error", NULL, NULL);
2471 PyDict_SetItemString(d, "error", PyCursesError);
2473 /* Make the version available */
2474 v = PyString_FromString(PyCursesVersion);
2475 PyDict_SetItemString(d, "version", v);
2476 PyDict_SetItemString(d, "__version__", v);
2477 Py_DECREF(v);
2479 SetDictInt("ERR", ERR);
2480 SetDictInt("OK", OK);
2482 /* Here are some attributes you can add to chars to print */
2484 SetDictInt("A_ATTRIBUTES", A_ATTRIBUTES);
2485 SetDictInt("A_NORMAL", A_NORMAL);
2486 SetDictInt("A_STANDOUT", A_STANDOUT);
2487 SetDictInt("A_UNDERLINE", A_UNDERLINE);
2488 SetDictInt("A_REVERSE", A_REVERSE);
2489 SetDictInt("A_BLINK", A_BLINK);
2490 SetDictInt("A_DIM", A_DIM);
2491 SetDictInt("A_BOLD", A_BOLD);
2492 SetDictInt("A_ALTCHARSET", A_ALTCHARSET);
2493 #if !defined(__NetBSD__)
2494 SetDictInt("A_INVIS", A_INVIS);
2495 #endif
2496 SetDictInt("A_PROTECT", A_PROTECT);
2497 SetDictInt("A_CHARTEXT", A_CHARTEXT);
2498 SetDictInt("A_COLOR", A_COLOR);
2500 /* The following are never available with strict SYSV curses */
2501 #ifdef A_HORIZONTAL
2502 SetDictInt("A_HORIZONTAL", A_HORIZONTAL);
2503 #endif
2504 #ifdef A_LEFT
2505 SetDictInt("A_LEFT", A_LEFT);
2506 #endif
2507 #ifdef A_LOW
2508 SetDictInt("A_LOW", A_LOW);
2509 #endif
2510 #ifdef A_RIGHT
2511 SetDictInt("A_RIGHT", A_RIGHT);
2512 #endif
2513 #ifdef A_TOP
2514 SetDictInt("A_TOP", A_TOP);
2515 #endif
2516 #ifdef A_VERTICAL
2517 SetDictInt("A_VERTICAL", A_VERTICAL);
2518 #endif
2520 SetDictInt("COLOR_BLACK", COLOR_BLACK);
2521 SetDictInt("COLOR_RED", COLOR_RED);
2522 SetDictInt("COLOR_GREEN", COLOR_GREEN);
2523 SetDictInt("COLOR_YELLOW", COLOR_YELLOW);
2524 SetDictInt("COLOR_BLUE", COLOR_BLUE);
2525 SetDictInt("COLOR_MAGENTA", COLOR_MAGENTA);
2526 SetDictInt("COLOR_CYAN", COLOR_CYAN);
2527 SetDictInt("COLOR_WHITE", COLOR_WHITE);
2529 #ifdef NCURSES_MOUSE_VERSION
2530 /* Mouse-related constants */
2531 SetDictInt("BUTTON1_PRESSED", BUTTON1_PRESSED);
2532 SetDictInt("BUTTON1_RELEASED", BUTTON1_RELEASED);
2533 SetDictInt("BUTTON1_CLICKED", BUTTON1_CLICKED);
2534 SetDictInt("BUTTON1_DOUBLE_CLICKED", BUTTON1_DOUBLE_CLICKED);
2535 SetDictInt("BUTTON1_TRIPLE_CLICKED", BUTTON1_TRIPLE_CLICKED);
2537 SetDictInt("BUTTON2_PRESSED", BUTTON2_PRESSED);
2538 SetDictInt("BUTTON2_RELEASED", BUTTON2_RELEASED);
2539 SetDictInt("BUTTON2_CLICKED", BUTTON2_CLICKED);
2540 SetDictInt("BUTTON2_DOUBLE_CLICKED", BUTTON2_DOUBLE_CLICKED);
2541 SetDictInt("BUTTON2_TRIPLE_CLICKED", BUTTON2_TRIPLE_CLICKED);
2543 SetDictInt("BUTTON3_PRESSED", BUTTON3_PRESSED);
2544 SetDictInt("BUTTON3_RELEASED", BUTTON3_RELEASED);
2545 SetDictInt("BUTTON3_CLICKED", BUTTON3_CLICKED);
2546 SetDictInt("BUTTON3_DOUBLE_CLICKED", BUTTON3_DOUBLE_CLICKED);
2547 SetDictInt("BUTTON3_TRIPLE_CLICKED", BUTTON3_TRIPLE_CLICKED);
2549 SetDictInt("BUTTON4_PRESSED", BUTTON4_PRESSED);
2550 SetDictInt("BUTTON4_RELEASED", BUTTON4_RELEASED);
2551 SetDictInt("BUTTON4_CLICKED", BUTTON4_CLICKED);
2552 SetDictInt("BUTTON4_DOUBLE_CLICKED", BUTTON4_DOUBLE_CLICKED);
2553 SetDictInt("BUTTON4_TRIPLE_CLICKED", BUTTON4_TRIPLE_CLICKED);
2555 SetDictInt("BUTTON_SHIFT", BUTTON_SHIFT);
2556 SetDictInt("BUTTON_CTRL", BUTTON_CTRL);
2557 SetDictInt("BUTTON_ALT", BUTTON_ALT);
2559 SetDictInt("ALL_MOUSE_EVENTS", ALL_MOUSE_EVENTS);
2560 SetDictInt("REPORT_MOUSE_POSITION", REPORT_MOUSE_POSITION);
2561 #endif
2562 /* Now set everything up for KEY_ variables */
2564 int key;
2565 char *key_n;
2566 char *key_n2;
2567 #if !defined(__NetBSD__)
2568 for (key=KEY_MIN;key < KEY_MAX; key++) {
2569 key_n = (char *)keyname(key);
2570 if (key_n == NULL || strcmp(key_n,"UNKNOWN KEY")==0)
2571 continue;
2572 if (strncmp(key_n,"KEY_F(",6)==0) {
2573 char *p1, *p2;
2574 key_n2 = malloc(strlen(key_n)+1);
2575 p1 = key_n;
2576 p2 = key_n2;
2577 while (*p1) {
2578 if (*p1 != '(' && *p1 != ')') {
2579 *p2 = *p1;
2580 p2++;
2582 p1++;
2584 *p2 = (char)0;
2585 } else
2586 key_n2 = key_n;
2587 PyDict_SetItemString(d,key_n2,PyInt_FromLong((long) key));
2588 if (key_n2 != key_n)
2589 free(key_n2);
2591 #endif
2592 SetDictInt("KEY_MIN", KEY_MIN);
2593 SetDictInt("KEY_MAX", KEY_MAX);