1 /***********************************************************
2 Copyright 1994 by Lance Ellinghouse,
3 Cathedral City, California Republic, United States of America.
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the name of Lance Ellinghouse
12 not be used in advertising or publicity pertaining to distribution
13 of the software without specific, written prior permission.
15 LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL,
18 INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
19 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
21 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /******************************************************************
26 This is a curses implementation. I have tried to be as complete
27 as possible. If there are functions you need that are not included,
28 please let me know and/or send me some diffs.
30 There are 3 basic types exported by this module:
31 1) Screen - This is not currently used
32 2) Window - This is the basic type. This is equivalent to "WINDOW *".
33 3) Pad - This is similar to Window, but works with Pads as defined
36 Most of the routines can be looked up using the curses man page.
38 Here is a list of the currently supported methods and attributes
41 Return Value Func/Attr Description
42 --------------------------------------------------------------------------
43 StringObject version A string representing the current
44 version of this module.
45 WindowObject initscr() This initializes the screen for use
46 None endwin() Closes down the screen and returns
47 things as they were before calling
49 True/FalseObject isendwin() Has endwin() been called?
50 None doupdate() Updates screen
51 WindowObject newwin(nlines,ncols,begin_y,begin_x)
52 newwin(begin_y,begin_x)
53 newwin() creates and returns
55 None beep() Beep the screen if possible
56 None flash() Flash the screen if possible
57 None ungetch(int) Push the int back so next getch()
59 Note: argument is an INT, not a CHAR
60 None flushinp() Flush all input buffers
61 None cbreak() Enter cbreak mode
62 None nocbreak() Leave cbreak mode
63 None echo() Enter echo mode
64 None noecho() Leave echo mode
65 None nl() Enter nl mode
66 None nonl() Leave nl mode
67 None raw() Enter raw mode
68 None noraw() Leave raw mode
69 None intrflush(int) Set or reset interruptable flush
70 mode, int=1 if set, 0 if notset.
71 None meta(int) Allow 8 bit or 7 bit chars.
72 int=1 is 8 bit, int=0 is 7 bit
73 StringObject keyname(int) return the text representation
74 of a KEY_ value. (see below)
76 Here is a list of the currently supported methods and attributes
79 Return Value Func/Attr Description
80 --------------------------------------------------------------------------
81 None refresh() Do refresh
82 None nooutrefresh() Mark for refresh but wait
83 None mvwin(new_y,new_x) Move Window
84 None move(new_y,new_x) Move Cursor
85 WindowObject subwin(nlines,ncols,begin_y,begin_x)
86 subwin(begin_y,begin_x)
87 None addch(y,x,ch,attr)
91 None insch(y,x,ch,attr)
97 None echochar(ch,attr)
99 None addstr(y,x,str,attr)
108 None border(ls,rs,ts,bs,tl,tr,bl,br) (accepts 0-8 INT args)
109 None box(vertch,horch) vertch and horch are INTS
126 None touchline(start,count)
129 StringObject getstr(y,x)
133 None clearok(int) int=0 or int=1
134 None idlok(int) int=0 or int=1
135 None leaveok(int) int=0 or int=1
136 None scrollok(int) int=0 or int=1
137 None setscrreg(top,bottom)
138 None keypad(int) int=0 or int=1
139 None nodelay(int) int=0 or int=1
140 None notimeout(int) int=0 or int=1
141 ******************************************************************/
153 } PyCursesScreenObject
;
159 } PyCursesWindowObject
;
166 staticforward PyTypeObject PyCursesScreen_Type
;
167 staticforward PyTypeObject PyCursesWindow_Type
;
168 staticforward PyTypeObject PyCursesPad_Type
;
170 #define PyCursesScreen_Check(v) ((v)->ob_type == &PyCursesScreen_Type)
171 #define PyCursesWindow_Check(v) ((v)->ob_type == &PyCursesWindow_Type)
172 #define PyCursesPad_Check(v) ((v)->ob_type == &PyCursesPad_Type)
175 static PyObject
*PyCursesError
; /* For exception curses.error */
177 /* Catch-all error messages */
178 static char *catchall_ERR
= "curses function returned ERR";
179 static char *catchall_NULL
= "curses function returned NULL";
181 /* Tells whether initscr() has been called to initialise curses */
182 static int initialised
= FALSE
;
184 #define ARG_COUNT(X) \
185 (((X) == NULL) ? 0 : (PyTuple_Check(X) ? PyTuple_Size(X) : 1))
187 /******************************************************************
191 Version 1.2: 95/02/23 (Steve Clift)
192 Fixed several potential core-dumping bugs.
193 Reworked arg parsing where variable arg lists are used.
194 Generate exceptions when ERR or NULL is returned by curses functions.
195 Changed return types to match SysV Curses manual descriptions.
196 Added keypad() to window method list.
197 Added border(), hline() and vline() window methods.
199 Version 1.1: 94/08/31:
200 Minor fixes given by Guido.
201 Changed 'ncurses' to 'curses'
202 Changed '__version__' to 'version'
203 Added PyErr_Clear() where needed
204 Moved ACS_* attribute initialization to PyCurses_InitScr() to fix
207 Version 1.0: 94/08/30:
208 This is the first release of this software.
209 Released to the Internet via python-list@cwi.nl
211 ******************************************************************/
213 char *PyCursesVersion
= "1.2";
216 * Check the return code from a curses function and return None
217 * or raise an exception as appropriate.
221 PyCursesCheckERR(code
, fname
)
232 PyErr_SetString(PyCursesError
, catchall_ERR
);
235 strcat(buf
, "() returned ERR");
236 PyErr_SetString(PyCursesError
, buf
);
244 PyCursesInitialised()
246 if (initialised
== TRUE
)
249 PyErr_SetString(PyCursesError
, "must call initscr() first");
255 /* ------------- SCREEN routines --------------- */
259 PyCursesScreen_New(arg
)
264 PyFileObject
*out_fo
;
265 PyCursesScreenObject
*xp
;
266 xp
= PyObject_NEW(PyCursesScreenObject
, &PyCursesScreen_Type
);
269 return (PyObject
*)xp
;
274 /* ------------- WINDOW routines --------------- */
277 PyCursesWindow_New(win
)
280 PyCursesWindowObject
*wo
;
282 wo
= PyObject_NEW(PyCursesWindowObject
, &PyCursesWindow_Type
);
286 wo
->parent
= (WINDOW
*)NULL
;
287 return (PyObject
*)wo
;
291 PyCursesWindow_Dealloc(wo
)
292 PyCursesWindowObject
*wo
;
294 if (wo
->win
!= stdscr
)
300 PyCursesWindow_Refresh(self
,arg
)
301 PyCursesWindowObject
*self
;
304 if (!PyArg_NoArgs(arg
))
306 return PyCursesCheckERR(wrefresh(self
->win
), "wrefresh");
310 PyCursesWindow_NoOutRefresh(self
,arg
)
311 PyCursesWindowObject
*self
;
314 if (!PyArg_NoArgs(arg
))
316 return PyCursesCheckERR(wnoutrefresh(self
->win
), "wnoutrefresh");
320 PyCursesWindow_MoveWin(self
,arg
)
321 PyCursesWindowObject
*self
;
325 if (!PyArg_Parse(arg
,"(ii);y,x", &y
, &x
))
327 return PyCursesCheckERR(mvwin(self
->win
,y
,x
), "mvwin");
331 PyCursesWindow_Move(self
,arg
)
332 PyCursesWindowObject
*self
;
336 if (!PyArg_Parse(arg
,"(ii);y,x", &y
, &x
))
338 return PyCursesCheckERR(wmove(self
->win
,y
,x
), "wmove");
342 PyCursesWindow_SubWin(self
,arg
)
343 PyCursesWindowObject
*self
;
347 PyCursesWindowObject
*rtn_win
;
348 int nlines
, ncols
, begin_y
, begin_x
;
352 switch (ARG_COUNT(arg
)) {
354 if (!PyArg_Parse(arg
,"(ii);begin_y,begin_x",&begin_y
,&begin_x
))
358 if (!PyArg_Parse(arg
, "(iiii);nlines,ncols,begin_y,begin_x",
359 &nlines
,&ncols
,&begin_y
,&begin_x
))
363 PyErr_SetString(PyExc_TypeError
, "subwin requires 2 or 4 arguments");
366 win
= subwin(self
->win
,nlines
,ncols
,begin_y
,begin_x
);
368 PyErr_SetString(PyCursesError
, catchall_NULL
);
371 rtn_win
= (PyCursesWindowObject
*)PyCursesWindow_New(win
);
372 rtn_win
->parent
= self
->win
;
373 return (PyObject
*)rtn_win
;
377 PyCursesWindow_AddCh(self
,arg
)
378 PyCursesWindowObject
*self
;
385 int use_xy
= FALSE
, use_attr
= FALSE
;
387 switch (ARG_COUNT(arg
)) {
389 if (!PyArg_Parse(arg
, "i;ch", &ch
))
393 if (!PyArg_Parse(arg
,"(ii);ch,attr", &ch
, &attr
))
398 if (!PyArg_Parse(arg
,"(iii);y,x,ch", &y
, &x
, &ch
))
403 if (!PyArg_Parse(arg
,"(iiii);y,x,ch,attr", &y
, &x
, &ch
, &attr
))
405 use_xy
= use_attr
= TRUE
;
408 PyErr_SetString(PyExc_TypeError
, "addch requires 1 to 4 arguments");
412 if (use_attr
== TRUE
) {
413 attr_old
= getattrs(self
->win
);
414 wattrset(self
->win
,attr
);
417 rtn
= mvwaddch(self
->win
,y
,x
,ch
);
419 rtn
= waddch(self
->win
,ch
);
420 if (use_attr
== TRUE
)
421 wattrset(self
->win
,attr_old
);
423 return PyCursesCheckERR(rtn
, "[mv]waddch");
427 PyCursesWindow_InsCh(self
,arg
)
428 PyCursesWindowObject
*self
;
435 int use_xy
= TRUE
, use_attr
= FALSE
;
437 switch (ARG_COUNT(arg
)) {
439 if (!PyArg_Parse(arg
, "i;ch", &ch
))
443 if (!PyArg_Parse(arg
,"(ii);ch,attr", &ch
, &attr
))
448 if (!PyArg_Parse(arg
,"(iii);y,x,ch", &y
, &x
, &ch
))
453 if (!PyArg_Parse(arg
,"(iiii);y,x,ch,attr", &y
, &x
, &ch
, &attr
))
455 use_xy
= use_attr
= TRUE
;
458 PyErr_SetString(PyExc_TypeError
, "insch requires 1 to 4 arguments");
462 if (use_attr
== TRUE
) {
463 attr_old
= getattrs(self
->win
);
464 wattrset(self
->win
,attr
);
467 rtn
= mvwinsch(self
->win
,y
,x
,ch
);
469 rtn
= winsch(self
->win
,ch
);
470 if (use_attr
== TRUE
)
471 wattrset(self
->win
,attr_old
);
473 return PyCursesCheckERR(rtn
, "[mv]winsch");
477 PyCursesWindow_DelCh(self
,arg
)
478 PyCursesWindowObject
*self
;
484 switch (ARG_COUNT(arg
)) {
486 rtn
= wdelch(self
->win
);
489 if (!PyArg_Parse(arg
,"(ii);y,x", &y
, &x
))
491 rtn
= mvwdelch(self
->win
,y
,x
);
494 PyErr_SetString(PyExc_TypeError
, "delch requires 0 or 2 arguments");
498 return PyCursesCheckERR(rtn
, "[mv]wdelch");
502 PyCursesWindow_EchoChar(self
,arg
)
503 PyCursesWindowObject
*self
;
510 switch (ARG_COUNT(arg
)) {
512 if (!PyArg_Parse(arg
,"i;ch", &ch
))
514 rtn
= wechochar(self
->win
,ch
);
517 if (!PyArg_Parse(arg
,"(ii);ch,attr", &ch
, &attr
))
519 attr_old
= getattrs(self
->win
);
520 wattrset(self
->win
,attr
);
521 rtn
= wechochar(self
->win
,ch
);
522 wattrset(self
->win
,attr_old
);
525 PyErr_SetString(PyExc_TypeError
, "echochar requires 1 or 2 arguments");
529 return PyCursesCheckERR(rtn
, "wechochar");
533 PyCursesWindow_AddStr(self
,arg
)
534 PyCursesWindowObject
*self
;
541 int use_xy
= FALSE
, use_attr
= FALSE
;
543 switch (ARG_COUNT(arg
)) {
545 if (!PyArg_Parse(arg
,"s;str", &str
))
549 if (!PyArg_Parse(arg
,"(si);str,attr", &str
, &attr
))
554 if (!PyArg_Parse(arg
,"(iis);y,x,str", &y
, &x
, &str
))
559 if (!PyArg_Parse(arg
,"(iisi);y,x,str,attr", &y
, &x
, &str
, &attr
))
561 use_xy
= use_attr
= TRUE
;
564 PyErr_SetString(PyExc_TypeError
, "addstr requires 1 to 4 arguments");
568 if (use_attr
== TRUE
) {
569 attr_old
= getattrs(self
->win
);
570 wattrset(self
->win
,attr
);
573 rtn
= mvwaddstr(self
->win
,y
,x
,str
);
575 rtn
= waddstr(self
->win
,str
);
576 if (use_attr
== TRUE
)
577 wattrset(self
->win
,attr_old
);
579 return PyCursesCheckERR(rtn
, "[mv]waddstr");
583 PyCursesWindow_AttrOn(self
,arg
)
584 PyCursesWindowObject
*self
;
588 if (!PyArg_Parse(arg
,"i;attr", &ch
))
590 wattron(self
->win
,ch
);
596 PyCursesWindow_AttrOff(self
,arg
)
597 PyCursesWindowObject
*self
;
601 if (!PyArg_Parse(arg
,"i;attr", &ch
))
603 wattroff(self
->win
,ch
);
609 PyCursesWindow_AttrSet(self
,arg
)
610 PyCursesWindowObject
*self
;
614 if (!PyArg_Parse(arg
,"i;attr", &ch
))
616 wattrset(self
->win
,ch
);
622 PyCursesWindow_StandEnd(self
,arg
)
623 PyCursesWindowObject
*self
;
626 if (!PyArg_NoArgs(arg
))
628 wstandend(self
->win
);
634 PyCursesWindow_StandOut(self
,arg
)
635 PyCursesWindowObject
*self
;
638 if (!PyArg_NoArgs(arg
))
640 wstandout(self
->win
);
646 PyCursesWindow_Border(self
, args
)
647 PyCursesWindowObject
*self
;
650 int ls
, rs
, ts
, bs
, tl
, tr
, bl
, br
;
651 ls
= rs
= ts
= bs
= tl
= tr
= bl
= br
= 0;
652 if (!PyArg_ParseTuple(args
,"|iiiiiiii;ls,rs,ts,bs,tl,tr,bl,br",
653 &ls
, &rs
, &ts
, &bs
, &tl
, &tr
, &bl
, &br
))
655 wborder(self
->win
, ls
, rs
, ts
, bs
, tl
, tr
, bl
, br
);
661 PyCursesWindow_Box(self
,arg
)
662 PyCursesWindowObject
*self
;
666 if (!PyArg_NoArgs(arg
)) {
668 if (!PyArg_Parse(arg
,"(ii);vertch,horch", &ch1
, &ch2
))
671 box(self
->win
,ch1
,ch2
);
677 PyCursesWindow_Hline(self
, args
)
678 PyCursesWindowObject
*self
;
681 int ch
, n
, x
, y
, code
= OK
;
682 switch (ARG_COUNT(args
)) {
684 if (!PyArg_Parse(args
, "(ii);ch,n", &ch
, &n
))
688 if (!PyArg_Parse(args
, "(iiii);y,x,ch,n", &y
, &x
, &ch
, &n
))
690 code
= wmove(self
->win
, y
, x
);
693 PyErr_SetString(PyExc_TypeError
, "hline requires 2 or 4 arguments");
697 whline(self
->win
, ch
, n
);
698 return PyCursesCheckERR(code
, "wmove");
702 PyCursesWindow_Vline(self
, args
)
703 PyCursesWindowObject
*self
;
706 int ch
, n
, x
, y
, code
= OK
;
707 switch (ARG_COUNT(args
)) {
709 if (!PyArg_Parse(args
, "(ii);ch,n", &ch
, &n
))
713 if (!PyArg_Parse(args
, "(iiii);y,x,ch,n", &y
, &x
, &ch
, &n
))
715 code
= wmove(self
->win
, y
, x
);
718 PyErr_SetString(PyExc_TypeError
, "vline requires 2 or 4 arguments");
722 wvline(self
->win
, ch
, n
);
723 return PyCursesCheckERR(code
, "wmove");
727 PyCursesWindow_Erase(self
,arg
)
728 PyCursesWindowObject
*self
;
731 if (!PyArg_NoArgs(arg
))
739 PyCursesWindow_DeleteLine(self
,arg
)
740 PyCursesWindowObject
*self
;
743 if (!PyArg_NoArgs(arg
))
745 return PyCursesCheckERR(wdeleteln(self
->win
), "wdeleteln");
749 PyCursesWindow_InsertLine(self
,arg
)
750 PyCursesWindowObject
*self
;
753 if (!PyArg_NoArgs(arg
))
755 return PyCursesCheckERR(winsertln(self
->win
), "winsertln");
759 PyCursesWindow_GetYX(self
,arg
)
760 PyCursesWindowObject
*self
;
764 if (!PyArg_NoArgs(arg
))
766 getyx(self
->win
,y
,x
);
767 return Py_BuildValue("(ii)", y
, x
);
771 PyCursesWindow_GetBegYX(self
,arg
)
772 PyCursesWindowObject
*self
;
776 if (!PyArg_NoArgs(arg
))
778 getbegyx(self
->win
,y
,x
);
779 return Py_BuildValue("(ii)", y
, x
);
783 PyCursesWindow_GetMaxYX(self
,arg
)
784 PyCursesWindowObject
*self
;
788 if (!PyArg_NoArgs(arg
))
790 getmaxyx(self
->win
,y
,x
);
791 return Py_BuildValue("(ii)", y
, x
);
795 PyCursesWindow_Clear(self
,arg
)
796 PyCursesWindowObject
*self
;
799 if (!PyArg_NoArgs(arg
))
807 PyCursesWindow_ClearToBottom(self
,arg
)
808 PyCursesWindowObject
*self
;
811 if (!PyArg_NoArgs(arg
))
813 wclrtobot(self
->win
);
819 PyCursesWindow_ClearToEOL(self
,arg
)
820 PyCursesWindowObject
*self
;
823 if (!PyArg_NoArgs(arg
))
825 wclrtoeol(self
->win
);
831 PyCursesWindow_Scroll(self
,arg
)
832 PyCursesWindowObject
*self
;
835 if (!PyArg_NoArgs(arg
))
837 return PyCursesCheckERR(scroll(self
->win
), "scroll");
841 PyCursesWindow_TouchWin(self
,arg
)
842 PyCursesWindowObject
*self
;
845 if (!PyArg_NoArgs(arg
))
847 return PyCursesCheckERR(touchwin(self
->win
), "touchwin");
851 PyCursesWindow_TouchLine(self
,arg
)
852 PyCursesWindowObject
*self
;
856 if (!PyArg_Parse(arg
,"(ii);start,count",&st
,&cnt
))
858 return PyCursesCheckERR(touchline(self
->win
,st
,cnt
), "touchline");
862 PyCursesWindow_GetCh(self
,arg
)
863 PyCursesWindowObject
*self
;
869 switch (ARG_COUNT(arg
)) {
871 rtn
= wgetch(self
->win
);
874 if (!PyArg_Parse(arg
,"(ii);y,x",&y
,&x
))
876 rtn
= mvwgetch(self
->win
,y
,x
);
879 PyErr_SetString(PyExc_TypeError
, "getch requires 0 or 2 arguments");
883 return PyInt_FromLong((long) rtn
);
887 PyCursesWindow_GetStr(self
,arg
)
888 PyCursesWindowObject
*self
;
892 char rtn
[1024]; /* This should be big enough.. I hope */
895 switch (ARG_COUNT(arg
)) {
897 rtn2
= wgetstr(self
->win
,rtn
);
900 if (!PyArg_Parse(arg
,"(ii);y,x",&y
,&x
))
902 rtn2
= mvwgetstr(self
->win
,y
,x
,rtn
);
905 PyErr_SetString(PyExc_TypeError
, "getstr requires 0 or 2 arguments");
911 return PyString_FromString(rtn
);
915 PyCursesWindow_InCh(self
,arg
)
916 PyCursesWindowObject
*self
;
921 switch (ARG_COUNT(arg
)) {
923 rtn
= winch(self
->win
);
926 if (!PyArg_Parse(arg
,"(ii);y,x",&y
,&x
))
928 rtn
= mvwinch(self
->win
,y
,x
);
931 PyErr_SetString(PyExc_TypeError
, "inch requires 0 or 2 arguments");
935 return PyInt_FromLong((long) rtn
);
939 PyCursesWindow_ClearOk(self
,arg
)
940 PyCursesWindowObject
*self
;
944 if (!PyArg_Parse(arg
,"i;True(1) or False(0)",&val
))
946 clearok(self
->win
,val
);
952 PyCursesWindow_IdlOk(self
,arg
)
953 PyCursesWindowObject
*self
;
957 if (!PyArg_Parse(arg
,"i;True(1) or False(0)",&val
))
959 idlok(self
->win
,val
);
965 PyCursesWindow_LeaveOk(self
,arg
)
966 PyCursesWindowObject
*self
;
970 if (!PyArg_Parse(arg
,"i;True(1) or False(0)",&val
))
972 leaveok(self
->win
,val
);
978 PyCursesWindow_ScrollOk(self
,arg
)
979 PyCursesWindowObject
*self
;
983 if (!PyArg_Parse(arg
,"i;True(1) or False(0)",&val
))
985 scrollok(self
->win
,val
);
991 PyCursesWindow_SetScrollRegion(self
,arg
)
992 PyCursesWindowObject
*self
;
996 if (!PyArg_Parse(arg
,"(ii);top, bottom",&y
,&x
))
998 return PyCursesCheckERR(wsetscrreg(self
->win
,y
,x
), "wsetscrreg");
1002 PyCursesWindow_KeyPad(self
,arg
)
1003 PyCursesWindowObject
* self
;
1007 if (!PyArg_Parse(arg
,"i;True(1), False(0)",&ch
))
1009 keypad(self
->win
,ch
);
1015 PyCursesWindow_NoDelay(self
,arg
)
1016 PyCursesWindowObject
* self
;
1020 if (!PyArg_Parse(arg
,"i;True(1), False(0)",&ch
))
1022 nodelay(self
->win
,ch
);
1028 PyCursesWindow_NoTimeout(self
,arg
)
1029 PyCursesWindowObject
* self
;
1033 if (!PyArg_Parse(arg
,"i;True(1), False(0)",&ch
))
1035 notimeout(self
->win
,ch
);
1040 static PyMethodDef PyCursesWindow_Methods
[] = {
1041 {"refresh", (PyCFunction
)PyCursesWindow_Refresh
},
1042 {"nooutrefresh", (PyCFunction
)PyCursesWindow_NoOutRefresh
},
1043 {"mvwin", (PyCFunction
)PyCursesWindow_MoveWin
},
1044 {"move", (PyCFunction
)PyCursesWindow_Move
},
1045 {"subwin", (PyCFunction
)PyCursesWindow_SubWin
},
1046 {"addch", (PyCFunction
)PyCursesWindow_AddCh
},
1047 {"insch", (PyCFunction
)PyCursesWindow_InsCh
},
1048 {"delch", (PyCFunction
)PyCursesWindow_DelCh
},
1049 {"echochar", (PyCFunction
)PyCursesWindow_EchoChar
},
1050 {"addstr", (PyCFunction
)PyCursesWindow_AddStr
},
1051 {"attron", (PyCFunction
)PyCursesWindow_AttrOn
},
1052 {"attroff", (PyCFunction
)PyCursesWindow_AttrOff
},
1053 {"attrset", (PyCFunction
)PyCursesWindow_AttrSet
},
1054 {"standend", (PyCFunction
)PyCursesWindow_StandEnd
},
1055 {"standout", (PyCFunction
)PyCursesWindow_StandOut
},
1056 {"border", (PyCFunction
)PyCursesWindow_Border
, METH_VARARGS
},
1057 {"box", (PyCFunction
)PyCursesWindow_Box
},
1058 {"hline", (PyCFunction
)PyCursesWindow_Hline
},
1059 {"vline", (PyCFunction
)PyCursesWindow_Vline
},
1060 {"erase", (PyCFunction
)PyCursesWindow_Erase
},
1061 {"deleteln", (PyCFunction
)PyCursesWindow_DeleteLine
},
1062 {"insertln", (PyCFunction
)PyCursesWindow_InsertLine
},
1063 {"getyx", (PyCFunction
)PyCursesWindow_GetYX
},
1064 {"getbegyx", (PyCFunction
)PyCursesWindow_GetBegYX
},
1065 {"getmaxyx", (PyCFunction
)PyCursesWindow_GetMaxYX
},
1066 {"clear", (PyCFunction
)PyCursesWindow_Clear
},
1067 {"clrtobot", (PyCFunction
)PyCursesWindow_ClearToBottom
},
1068 {"clrtoeol", (PyCFunction
)PyCursesWindow_ClearToEOL
},
1069 {"scroll", (PyCFunction
)PyCursesWindow_Scroll
},
1070 {"touchwin", (PyCFunction
)PyCursesWindow_TouchWin
},
1071 {"touchline", (PyCFunction
)PyCursesWindow_TouchLine
},
1072 {"getch", (PyCFunction
)PyCursesWindow_GetCh
},
1073 {"getstr", (PyCFunction
)PyCursesWindow_GetStr
},
1074 {"inch", (PyCFunction
)PyCursesWindow_InCh
},
1075 {"clearok", (PyCFunction
)PyCursesWindow_ClearOk
},
1076 {"idlok", (PyCFunction
)PyCursesWindow_IdlOk
},
1077 {"leaveok", (PyCFunction
)PyCursesWindow_LeaveOk
},
1078 {"scrollok", (PyCFunction
)PyCursesWindow_ScrollOk
},
1079 {"setscrreg", (PyCFunction
)PyCursesWindow_SetScrollRegion
},
1080 {"keypad", (PyCFunction
)PyCursesWindow_KeyPad
},
1081 {"nodelay", (PyCFunction
)PyCursesWindow_NoDelay
},
1082 {"notimeout", (PyCFunction
)PyCursesWindow_NoTimeout
},
1083 {NULL
, NULL
} /* sentinel */
1087 PyCursesWindow_GetAttr(self
, name
)
1088 PyCursesWindowObject
*self
;
1091 return Py_FindMethod(PyCursesWindow_Methods
, (PyObject
*)self
, name
);
1095 /* --------------- PAD routines ---------------- */
1099 PyCursesPad_New(pad
)
1102 PyCursesPadObject
*po
;
1103 po
= PyObject_NEW(PyCursesPadObject
, &PyCursesPad_Type
);
1107 return (PyObject
*)po
;
1112 /* -------------------------------------------------------*/
1114 static PyTypeObject PyCursesScreen_Type
= {
1115 PyObject_HEAD_INIT(&PyType_Type
)
1117 "curses screen", /*tp_name*/
1118 sizeof(PyCursesScreenObject
), /*tp_basicsize*/
1121 (destructor
)0 /*PyCursesScreen_Dealloc*/, /*tp_dealloc*/
1123 (getattrfunc
)0, /*tp_getattr*/
1124 (setattrfunc
)0, /*tp_setattr*/
1128 0, /*tp_as_sequence*/
1129 0, /*tp_as_mapping*/
1133 static PyTypeObject PyCursesWindow_Type
= {
1134 PyObject_HEAD_INIT(&PyType_Type
)
1136 "curses window", /*tp_name*/
1137 sizeof(PyCursesWindowObject
), /*tp_basicsize*/
1140 (destructor
)PyCursesWindow_Dealloc
, /*tp_dealloc*/
1142 (getattrfunc
)PyCursesWindow_GetAttr
, /*tp_getattr*/
1143 (setattrfunc
)0, /*tp_setattr*/
1147 0, /*tp_as_sequence*/
1148 0, /*tp_as_mapping*/
1152 static PyTypeObject PyCursesPad_Type
= {
1153 PyObject_HEAD_INIT(&PyType_Type
)
1155 "curses pad", /*tp_name*/
1156 sizeof(PyCursesPadObject
), /*tp_basicsize*/
1159 (destructor
)0 /*PyCursesPad_Dealloc*/, /*tp_dealloc*/
1161 (getattrfunc
)0, /*tp_getattr*/
1162 (setattrfunc
)0, /*tp_setattr*/
1166 0, /*tp_as_sequence*/
1167 0, /*tp_as_mapping*/
1172 /* -------------------------------------------------------*/
1174 static PyObject
*ModDict
;
1177 PyCurses_InitScr(self
, args
)
1182 if (!PyArg_NoArgs(args
))
1184 if (initialised
== TRUE
) {
1186 return (PyObject
*)PyCursesWindow_New(stdscr
);
1191 PyErr_SetString(PyCursesError
, catchall_NULL
);
1197 /* This was moved from initcurses() because core dumped on SGI */
1198 /* Also, they are probably not defined until you've called initscr() */
1199 #define SetDictInt(string,ch) \
1200 PyDict_SetItemString(ModDict,string,PyInt_FromLong((long) (ch)));
1202 /* Here are some graphic symbols you can use */
1203 SetDictInt("ACS_ULCORNER",(ACS_ULCORNER
));
1204 SetDictInt("ACS_ULCORNER",(ACS_ULCORNER
));
1205 SetDictInt("ACS_LLCORNER",(ACS_LLCORNER
));
1206 SetDictInt("ACS_URCORNER",(ACS_URCORNER
));
1207 SetDictInt("ACS_LRCORNER",(ACS_LRCORNER
));
1208 SetDictInt("ACS_RTEE", (ACS_RTEE
));
1209 SetDictInt("ACS_LTEE", (ACS_LTEE
));
1210 SetDictInt("ACS_BTEE", (ACS_BTEE
));
1211 SetDictInt("ACS_TTEE", (ACS_TTEE
));
1212 SetDictInt("ACS_HLINE", (ACS_HLINE
));
1213 SetDictInt("ACS_VLINE", (ACS_VLINE
));
1214 SetDictInt("ACS_PLUS", (ACS_PLUS
));
1215 SetDictInt("ACS_S1", (ACS_S1
));
1216 SetDictInt("ACS_S9", (ACS_S9
));
1217 SetDictInt("ACS_DIAMOND", (ACS_DIAMOND
));
1218 SetDictInt("ACS_CKBOARD", (ACS_CKBOARD
));
1219 SetDictInt("ACS_DEGREE", (ACS_DEGREE
));
1220 SetDictInt("ACS_PLMINUS", (ACS_PLMINUS
));
1221 SetDictInt("ACS_BULLET", (ACS_BULLET
));
1222 SetDictInt("ACS_LARROW", (ACS_RARROW
));
1223 SetDictInt("ACS_DARROW", (ACS_DARROW
));
1224 SetDictInt("ACS_UARROW", (ACS_UARROW
));
1225 SetDictInt("ACS_BOARD", (ACS_BOARD
));
1226 SetDictInt("ACS_LANTERN", (ACS_LANTERN
));
1227 SetDictInt("ACS_BLOCK", (ACS_BLOCK
));
1229 return (PyObject
*)PyCursesWindow_New(win
);
1233 PyCurses_EndWin(self
, args
)
1237 if (!PyArg_NoArgs(args
) || !PyCursesInitialised())
1239 return PyCursesCheckERR(endwin(), "endwin");
1243 PyCurses_IsEndWin(self
, args
)
1247 if (!PyArg_NoArgs(args
))
1249 if (isendwin() == FALSE
) {
1250 Py_INCREF(Py_False
);
1258 PyCurses_DoUpdate(self
,arg
)
1262 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1264 return PyCursesCheckERR(doupdate(), "doupdate");
1268 PyCurses_NewWindow(self
,arg
)
1273 int nlines
, ncols
, begin_y
, begin_x
;
1275 if (!PyCursesInitialised())
1278 switch (ARG_COUNT(arg
)) {
1280 if (!PyArg_Parse(arg
,"(ii);begin)_y,begin_x",&begin_y
,&begin_x
))
1284 if (!PyArg_Parse(arg
, "(iiii);nlines,ncols,begin_y,begin_x",
1285 &nlines
,&ncols
,&begin_y
,&begin_x
))
1289 PyErr_SetString(PyExc_TypeError
, "newwin requires 2 or 4 arguments");
1293 win
= newwin(nlines
,ncols
,begin_y
,begin_x
);
1295 PyErr_SetString(PyCursesError
, catchall_NULL
);
1299 return (PyObject
*)PyCursesWindow_New(win
);
1303 PyCurses_Beep(self
,arg
)
1307 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1315 PyCurses_Flash(self
,arg
)
1319 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1327 PyCurses_UngetCh(self
,arg
)
1332 if (!PyArg_Parse(arg
,"i;integer",&ch
) || !PyCursesInitialised())
1334 return PyCursesCheckERR(ungetch(ch
), "ungetch");
1338 PyCurses_FlushInp(self
,arg
)
1342 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1350 PyCurses_CBreak(self
,arg
)
1354 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1356 return PyCursesCheckERR(cbreak(), "cbreak");
1360 PyCurses_NoCBreak(self
,arg
)
1364 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1366 return PyCursesCheckERR(nocbreak(), "nocbreak");
1370 PyCurses_Echo(self
,arg
)
1374 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1376 return PyCursesCheckERR(echo(), "echo");
1380 PyCurses_NoEcho(self
,arg
)
1384 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1386 return PyCursesCheckERR(noecho(), "noecho");
1390 PyCurses_Nl(self
,arg
)
1394 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1396 return PyCursesCheckERR(nl(), "nl");
1400 PyCurses_NoNl(self
,arg
)
1404 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1406 return PyCursesCheckERR(nonl(), "nonl");
1410 PyCurses_Raw(self
,arg
)
1414 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1416 return PyCursesCheckERR(raw(), "raw");
1420 PyCurses_NoRaw(self
,arg
)
1424 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1426 return PyCursesCheckERR(noraw(), "noraw");
1430 PyCurses_IntrFlush(self
,arg
)
1435 if (!PyArg_Parse(arg
,"i;True(1), False(0)",&ch
))
1437 return PyCursesCheckERR(intrflush(NULL
,ch
), "intrflush");
1441 PyCurses_Meta(self
,arg
)
1446 if (!PyArg_Parse(arg
,"i;True(1), False(0)",&ch
) || !PyCursesInitialised())
1448 return PyCursesCheckERR(meta(stdscr
, ch
), "meta");
1452 PyCurses_KeyName(self
,arg
)
1458 if (!PyArg_Parse(arg
,"i",&ch
))
1461 return PyString_FromString((knp
== NULL
) ? "" : knp
);
1466 PyCurses_NewTerm(self
, args
)
1473 PyCurses_SetTerm(self
, args
)
1480 /* List of functions defined in the module */
1482 static PyMethodDef PyCurses_methods
[] = {
1483 {"initscr", (PyCFunction
)PyCurses_InitScr
},
1484 {"endwin", (PyCFunction
)PyCurses_EndWin
},
1485 {"isendwin", (PyCFunction
)PyCurses_IsEndWin
},
1486 {"doupdate", (PyCFunction
)PyCurses_DoUpdate
},
1487 {"newwin", (PyCFunction
)PyCurses_NewWindow
},
1488 {"beep", (PyCFunction
)PyCurses_Beep
},
1489 {"flash", (PyCFunction
)PyCurses_Flash
},
1490 {"ungetch", (PyCFunction
)PyCurses_UngetCh
},
1491 {"flushinp", (PyCFunction
)PyCurses_FlushInp
},
1492 {"cbreak", (PyCFunction
)PyCurses_CBreak
},
1493 {"nocbreak", (PyCFunction
)PyCurses_NoCBreak
},
1494 {"echo", (PyCFunction
)PyCurses_Echo
},
1495 {"noecho", (PyCFunction
)PyCurses_NoEcho
},
1496 {"nl", (PyCFunction
)PyCurses_Nl
},
1497 {"nonl", (PyCFunction
)PyCurses_NoNl
},
1498 {"raw", (PyCFunction
)PyCurses_Raw
},
1499 {"noraw", (PyCFunction
)PyCurses_NoRaw
},
1500 {"intrflush", (PyCFunction
)PyCurses_IntrFlush
},
1501 {"meta", (PyCFunction
)PyCurses_Meta
},
1502 {"keyname", (PyCFunction
)PyCurses_KeyName
},
1504 {"newterm", (PyCFunction
)PyCurses_NewTerm
},
1505 {"set_term", (PyCFunction
)PyCurses_SetTerm
},
1507 {NULL
, NULL
} /* sentinel */
1510 /* Initialization function for the module */
1515 PyObject
*m
, *d
, *v
;
1517 /* Create the module and add the functions */
1518 m
= Py_InitModule("curses", PyCurses_methods
);
1520 /* Add some symbolic constants to the module */
1521 d
= PyModule_GetDict(m
);
1522 ModDict
= d
; /* For PyCurses_InitScr */
1524 /* For exception curses.error */
1525 PyCursesError
= PyString_FromString("curses.error");
1526 PyDict_SetItemString(d
, "error", PyCursesError
);
1528 /* Make the version available */
1529 v
= PyString_FromString(PyCursesVersion
);
1530 PyDict_SetItemString(d
, "version", v
);
1531 PyDict_SetItemString(d
, "__version__", v
);
1534 /* Here are some attributes you can add to chars to print */
1535 SetDictInt("A_NORMAL", A_NORMAL
);
1536 SetDictInt("A_STANDOUT", A_STANDOUT
);
1537 SetDictInt("A_UNDERLINE", A_UNDERLINE
);
1538 SetDictInt("A_REVERSE", A_REVERSE
);
1539 SetDictInt("A_BLINK", A_BLINK
);
1540 SetDictInt("A_DIM", A_DIM
);
1541 SetDictInt("A_BOLD", A_BOLD
);
1542 SetDictInt("A_ALTCHARSET", A_ALTCHARSET
);
1544 /* Now set everything up for KEY_ variables */
1549 for (key
=KEY_MIN
;key
< KEY_MAX
; key
++) {
1550 key_n
= (char *)keyname(key
);
1551 if (key_n
== NULL
|| strcmp(key_n
,"UNKNOWN KEY")==0)
1553 if (strncmp(key_n
,"KEY_F(",6)==0) {
1555 key_n2
= malloc(strlen(key_n
)+1);
1559 if (*p1
!= '(' && *p1
!= ')') {
1568 PyDict_SetItemString(d
,key_n2
,PyInt_FromLong((long) key
));
1569 if (key_n2
!= key_n
)
1572 SetDictInt("KEY_MIN", KEY_MIN
);
1573 SetDictInt("KEY_MAX", KEY_MAX
);
1576 /* Check for errors */
1577 if (PyErr_Occurred())
1578 Py_FatalError("can't initialize module curses");