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
127 None touchline(start,count)
130 StringObject getstr(y,x)
134 None clearok(int) int=0 or int=1
135 None idlok(int) int=0 or int=1
136 None leaveok(int) int=0 or int=1
137 None scrollok(int) int=0 or int=1
138 None setscrreg(top,bottom)
139 None keypad(int) int=0 or int=1
140 None nodelay(int) int=0 or int=1
141 None notimeout(int) int=0 or int=1
142 ******************************************************************/
149 #ifdef HAVE_NCURSES_H
150 /* Now let's hope there aren't systems that have a broken ncurses.h */
159 } PyCursesScreenObject
;
165 } PyCursesWindowObject
;
173 staticforward PyTypeObject PyCursesScreen_Type
;
175 staticforward PyTypeObject PyCursesWindow_Type
;
177 staticforward PyTypeObject PyCursesPad_Type
;
180 #define PyCursesScreen_Check(v) ((v)->ob_type == &PyCursesScreen_Type)
181 #define PyCursesWindow_Check(v) ((v)->ob_type == &PyCursesWindow_Type)
182 #define PyCursesPad_Check(v) ((v)->ob_type == &PyCursesPad_Type)
185 static PyObject
*PyCursesError
; /* For exception curses.error */
187 /* Catch-all error messages */
188 static char *catchall_ERR
= "curses function returned ERR";
189 static char *catchall_NULL
= "curses function returned NULL";
191 /* Tells whether initscr() has been called to initialise curses */
192 static int initialised
= FALSE
;
194 #define ARG_COUNT(X) \
195 (((X) == NULL) ? 0 : (PyTuple_Check(X) ? PyTuple_Size(X) : 1))
197 /******************************************************************
201 Version 1.2: 95/02/23 (Steve Clift)
202 Fixed several potential core-dumping bugs.
203 Reworked arg parsing where variable arg lists are used.
204 Generate exceptions when ERR or NULL is returned by curses functions.
205 Changed return types to match SysV Curses manual descriptions.
206 Added keypad() to window method list.
207 Added border(), hline() and vline() window methods.
209 Version 1.1: 94/08/31:
210 Minor fixes given by Guido.
211 Changed 'ncurses' to 'curses'
212 Changed '__version__' to 'version'
213 Added PyErr_Clear() where needed
214 Moved ACS_* attribute initialization to PyCurses_InitScr() to fix
217 Version 1.0: 94/08/30:
218 This is the first release of this software.
219 Released to the Internet via python-list@cwi.nl
221 ******************************************************************/
223 static char *PyCursesVersion
= "1.2";
226 * Check the return code from a curses function and return None
227 * or raise an exception as appropriate.
231 PyCursesCheckERR(code
, fname
)
242 PyErr_SetString(PyCursesError
, catchall_ERR
);
245 strcat(buf
, "() returned ERR");
246 PyErr_SetString(PyCursesError
, buf
);
254 PyCursesInitialised()
256 if (initialised
== TRUE
)
259 PyErr_SetString(PyCursesError
, "must call initscr() first");
265 /* ------------- SCREEN routines --------------- */
269 PyCursesScreen_New(arg
)
274 PyFileObject
*out_fo
;
275 PyCursesScreenObject
*xp
;
276 xp
= PyObject_NEW(PyCursesScreenObject
, &PyCursesScreen_Type
);
279 return (PyObject
*)xp
;
284 /* ------------- WINDOW routines --------------- */
287 PyCursesWindow_New(win
)
290 PyCursesWindowObject
*wo
;
292 wo
= PyObject_NEW(PyCursesWindowObject
, &PyCursesWindow_Type
);
296 wo
->parent
= (WINDOW
*)NULL
;
297 return (PyObject
*)wo
;
301 PyCursesWindow_Dealloc(wo
)
302 PyCursesWindowObject
*wo
;
304 if (wo
->win
!= stdscr
)
310 PyCursesWindow_Refresh(self
,arg
)
311 PyCursesWindowObject
*self
;
314 if (!PyArg_NoArgs(arg
))
316 return PyCursesCheckERR(wrefresh(self
->win
), "wrefresh");
320 PyCursesWindow_NoOutRefresh(self
,arg
)
321 PyCursesWindowObject
*self
;
324 if (!PyArg_NoArgs(arg
))
326 return PyCursesCheckERR(wnoutrefresh(self
->win
), "wnoutrefresh");
330 PyCursesWindow_MoveWin(self
,arg
)
331 PyCursesWindowObject
*self
;
335 if (!PyArg_Parse(arg
,"(ii);y,x", &y
, &x
))
337 return PyCursesCheckERR(mvwin(self
->win
,y
,x
), "mvwin");
341 PyCursesWindow_Move(self
,arg
)
342 PyCursesWindowObject
*self
;
346 if (!PyArg_Parse(arg
,"(ii);y,x", &y
, &x
))
348 return PyCursesCheckERR(wmove(self
->win
,y
,x
), "wmove");
352 PyCursesWindow_SubWin(self
,arg
)
353 PyCursesWindowObject
*self
;
357 PyCursesWindowObject
*rtn_win
;
358 int nlines
, ncols
, begin_y
, begin_x
;
362 switch (ARG_COUNT(arg
)) {
364 if (!PyArg_Parse(arg
,"(ii);begin_y,begin_x",&begin_y
,&begin_x
))
368 if (!PyArg_Parse(arg
, "(iiii);nlines,ncols,begin_y,begin_x",
369 &nlines
,&ncols
,&begin_y
,&begin_x
))
373 PyErr_SetString(PyExc_TypeError
, "subwin requires 2 or 4 arguments");
376 win
= subwin(self
->win
,nlines
,ncols
,begin_y
,begin_x
);
378 PyErr_SetString(PyCursesError
, catchall_NULL
);
381 rtn_win
= (PyCursesWindowObject
*)PyCursesWindow_New(win
);
382 rtn_win
->parent
= self
->win
;
383 return (PyObject
*)rtn_win
;
387 PyCursesWindow_AddCh(self
,arg
)
388 PyCursesWindowObject
*self
;
394 int attr
, attr_old
= 0;
395 int use_xy
= FALSE
, use_attr
= FALSE
;
397 switch (ARG_COUNT(arg
)) {
399 if (!PyArg_Parse(arg
, "i;ch", &ch
))
403 if (!PyArg_Parse(arg
,"(ii);ch,attr", &ch
, &attr
))
408 if (!PyArg_Parse(arg
,"(iii);y,x,ch", &y
, &x
, &ch
))
413 if (!PyArg_Parse(arg
,"(iiii);y,x,ch,attr", &y
, &x
, &ch
, &attr
))
415 use_xy
= use_attr
= TRUE
;
418 PyErr_SetString(PyExc_TypeError
, "addch requires 1 to 4 arguments");
422 if (use_attr
== TRUE
) {
423 attr_old
= getattrs(self
->win
);
424 wattrset(self
->win
,attr
);
427 rtn
= mvwaddch(self
->win
,y
,x
,ch
);
429 rtn
= waddch(self
->win
,ch
);
430 if (use_attr
== TRUE
)
431 wattrset(self
->win
,attr_old
);
433 return PyCursesCheckERR(rtn
, "[mv]waddch");
437 PyCursesWindow_InsCh(self
,arg
)
438 PyCursesWindowObject
*self
;
444 int attr
, attr_old
= 0;
445 int use_xy
= TRUE
, use_attr
= FALSE
;
447 switch (ARG_COUNT(arg
)) {
449 if (!PyArg_Parse(arg
, "i;ch", &ch
))
453 if (!PyArg_Parse(arg
,"(ii);ch,attr", &ch
, &attr
))
458 if (!PyArg_Parse(arg
,"(iii);y,x,ch", &y
, &x
, &ch
))
463 if (!PyArg_Parse(arg
,"(iiii);y,x,ch,attr", &y
, &x
, &ch
, &attr
))
465 use_xy
= use_attr
= TRUE
;
468 PyErr_SetString(PyExc_TypeError
, "insch requires 1 to 4 arguments");
472 if (use_attr
== TRUE
) {
473 attr_old
= getattrs(self
->win
);
474 wattrset(self
->win
,attr
);
477 rtn
= mvwinsch(self
->win
,y
,x
,ch
);
479 rtn
= winsch(self
->win
,ch
);
480 if (use_attr
== TRUE
)
481 wattrset(self
->win
,attr_old
);
483 return PyCursesCheckERR(rtn
, "[mv]winsch");
487 PyCursesWindow_DelCh(self
,arg
)
488 PyCursesWindowObject
*self
;
494 switch (ARG_COUNT(arg
)) {
496 rtn
= wdelch(self
->win
);
499 if (!PyArg_Parse(arg
,"(ii);y,x", &y
, &x
))
501 rtn
= mvwdelch(self
->win
,y
,x
);
504 PyErr_SetString(PyExc_TypeError
, "delch requires 0 or 2 arguments");
508 return PyCursesCheckERR(rtn
, "[mv]wdelch");
512 PyCursesWindow_EchoChar(self
,arg
)
513 PyCursesWindowObject
*self
;
520 switch (ARG_COUNT(arg
)) {
522 if (!PyArg_Parse(arg
,"i;ch", &ch
))
524 rtn
= wechochar(self
->win
,ch
);
527 if (!PyArg_Parse(arg
,"(ii);ch,attr", &ch
, &attr
))
529 attr_old
= getattrs(self
->win
);
530 wattrset(self
->win
,attr
);
531 rtn
= wechochar(self
->win
,ch
);
532 wattrset(self
->win
,attr_old
);
535 PyErr_SetString(PyExc_TypeError
, "echochar requires 1 or 2 arguments");
539 return PyCursesCheckERR(rtn
, "wechochar");
543 PyCursesWindow_AddStr(self
,arg
)
544 PyCursesWindowObject
*self
;
550 int attr
, attr_old
= 0;
551 int use_xy
= FALSE
, use_attr
= FALSE
;
553 switch (ARG_COUNT(arg
)) {
555 if (!PyArg_Parse(arg
,"s;str", &str
))
559 if (!PyArg_Parse(arg
,"(si);str,attr", &str
, &attr
))
564 if (!PyArg_Parse(arg
,"(iis);y,x,str", &y
, &x
, &str
))
569 if (!PyArg_Parse(arg
,"(iisi);y,x,str,attr", &y
, &x
, &str
, &attr
))
571 use_xy
= use_attr
= TRUE
;
574 PyErr_SetString(PyExc_TypeError
, "addstr requires 1 to 4 arguments");
578 if (use_attr
== TRUE
) {
579 attr_old
= getattrs(self
->win
);
580 wattrset(self
->win
,attr
);
583 rtn
= mvwaddstr(self
->win
,y
,x
,str
);
585 rtn
= waddstr(self
->win
,str
);
586 if (use_attr
== TRUE
)
587 wattrset(self
->win
,attr_old
);
589 return PyCursesCheckERR(rtn
, "[mv]waddstr");
593 PyCursesWindow_AttrOn(self
,arg
)
594 PyCursesWindowObject
*self
;
598 if (!PyArg_Parse(arg
,"i;attr", &ch
))
600 wattron(self
->win
,ch
);
606 PyCursesWindow_AttrOff(self
,arg
)
607 PyCursesWindowObject
*self
;
611 if (!PyArg_Parse(arg
,"i;attr", &ch
))
613 wattroff(self
->win
,ch
);
619 PyCursesWindow_AttrSet(self
,arg
)
620 PyCursesWindowObject
*self
;
624 if (!PyArg_Parse(arg
,"i;attr", &ch
))
626 wattrset(self
->win
,ch
);
632 PyCursesWindow_StandEnd(self
,arg
)
633 PyCursesWindowObject
*self
;
636 if (!PyArg_NoArgs(arg
))
638 wstandend(self
->win
);
644 PyCursesWindow_StandOut(self
,arg
)
645 PyCursesWindowObject
*self
;
648 if (!PyArg_NoArgs(arg
))
650 wstandout(self
->win
);
656 PyCursesWindow_Border(self
, args
)
657 PyCursesWindowObject
*self
;
660 int ls
, rs
, ts
, bs
, tl
, tr
, bl
, br
;
661 ls
= rs
= ts
= bs
= tl
= tr
= bl
= br
= 0;
662 if (!PyArg_ParseTuple(args
,"|iiiiiiii;ls,rs,ts,bs,tl,tr,bl,br",
663 &ls
, &rs
, &ts
, &bs
, &tl
, &tr
, &bl
, &br
))
665 wborder(self
->win
, ls
, rs
, ts
, bs
, tl
, tr
, bl
, br
);
671 PyCursesWindow_Box(self
,arg
)
672 PyCursesWindowObject
*self
;
676 if (!PyArg_NoArgs(arg
)) {
678 if (!PyArg_Parse(arg
,"(ii);vertch,horch", &ch1
, &ch2
))
681 box(self
->win
,ch1
,ch2
);
687 PyCursesWindow_Hline(self
, args
)
688 PyCursesWindowObject
*self
;
691 int ch
, n
, x
, y
, code
= OK
;
692 switch (ARG_COUNT(args
)) {
694 if (!PyArg_Parse(args
, "(ii);ch,n", &ch
, &n
))
698 if (!PyArg_Parse(args
, "(iiii);y,x,ch,n", &y
, &x
, &ch
, &n
))
700 code
= wmove(self
->win
, y
, x
);
703 PyErr_SetString(PyExc_TypeError
, "hline requires 2 or 4 arguments");
707 whline(self
->win
, ch
, n
);
708 return PyCursesCheckERR(code
, "wmove");
712 PyCursesWindow_Vline(self
, args
)
713 PyCursesWindowObject
*self
;
716 int ch
, n
, x
, y
, code
= OK
;
717 switch (ARG_COUNT(args
)) {
719 if (!PyArg_Parse(args
, "(ii);ch,n", &ch
, &n
))
723 if (!PyArg_Parse(args
, "(iiii);y,x,ch,n", &y
, &x
, &ch
, &n
))
725 code
= wmove(self
->win
, y
, x
);
728 PyErr_SetString(PyExc_TypeError
, "vline requires 2 or 4 arguments");
732 wvline(self
->win
, ch
, n
);
733 return PyCursesCheckERR(code
, "wmove");
737 PyCursesWindow_Erase(self
,arg
)
738 PyCursesWindowObject
*self
;
741 if (!PyArg_NoArgs(arg
))
749 PyCursesWindow_DeleteLine(self
,arg
)
750 PyCursesWindowObject
*self
;
753 if (!PyArg_NoArgs(arg
))
755 return PyCursesCheckERR(wdeleteln(self
->win
), "wdeleteln");
759 PyCursesWindow_InsertLine(self
,arg
)
760 PyCursesWindowObject
*self
;
763 if (!PyArg_NoArgs(arg
))
765 return PyCursesCheckERR(winsertln(self
->win
), "winsertln");
769 PyCursesWindow_GetYX(self
,arg
)
770 PyCursesWindowObject
*self
;
774 if (!PyArg_NoArgs(arg
))
776 getyx(self
->win
,y
,x
);
777 return Py_BuildValue("(ii)", y
, x
);
781 PyCursesWindow_GetBegYX(self
,arg
)
782 PyCursesWindowObject
*self
;
786 if (!PyArg_NoArgs(arg
))
788 getbegyx(self
->win
,y
,x
);
789 return Py_BuildValue("(ii)", y
, x
);
793 PyCursesWindow_GetMaxYX(self
,arg
)
794 PyCursesWindowObject
*self
;
798 if (!PyArg_NoArgs(arg
))
800 getmaxyx(self
->win
,y
,x
);
801 return Py_BuildValue("(ii)", y
, x
);
805 PyCursesWindow_Clear(self
,arg
)
806 PyCursesWindowObject
*self
;
809 if (!PyArg_NoArgs(arg
))
817 PyCursesWindow_ClearToBottom(self
,arg
)
818 PyCursesWindowObject
*self
;
821 if (!PyArg_NoArgs(arg
))
823 wclrtobot(self
->win
);
829 PyCursesWindow_ClearToEOL(self
,arg
)
830 PyCursesWindowObject
*self
;
833 if (!PyArg_NoArgs(arg
))
835 wclrtoeol(self
->win
);
841 PyCursesWindow_Scroll(self
,arg
)
842 PyCursesWindowObject
*self
;
846 int use_nlines
= FALSE
;
847 switch (ARG_COUNT(arg
)) {
851 if (!PyArg_Parse(arg
, "i;nlines", &nlines
))
856 PyErr_SetString(PyExc_TypeError
, "scroll requires 0 or 1 arguments");
860 return PyCursesCheckERR(wscrl(self
->win
, nlines
), "scroll");
862 return PyCursesCheckERR(scroll(self
->win
), "scroll");
866 PyCursesWindow_TouchWin(self
,arg
)
867 PyCursesWindowObject
*self
;
870 if (!PyArg_NoArgs(arg
))
872 return PyCursesCheckERR(touchwin(self
->win
), "touchwin");
876 PyCursesWindow_TouchLine(self
,arg
)
877 PyCursesWindowObject
*self
;
881 if (!PyArg_Parse(arg
,"(ii);start,count",&st
,&cnt
))
883 return PyCursesCheckERR(touchline(self
->win
,st
,cnt
), "touchline");
887 PyCursesWindow_GetCh(self
,arg
)
888 PyCursesWindowObject
*self
;
894 switch (ARG_COUNT(arg
)) {
896 rtn
= wgetch(self
->win
);
899 if (!PyArg_Parse(arg
,"(ii);y,x",&y
,&x
))
901 rtn
= mvwgetch(self
->win
,y
,x
);
904 PyErr_SetString(PyExc_TypeError
, "getch requires 0 or 2 arguments");
908 return PyInt_FromLong((long) rtn
);
912 PyCursesWindow_GetStr(self
,arg
)
913 PyCursesWindowObject
*self
;
917 char rtn
[1024]; /* This should be big enough.. I hope */
920 switch (ARG_COUNT(arg
)) {
922 rtn2
= wgetstr(self
->win
,rtn
);
925 if (!PyArg_Parse(arg
,"(ii);y,x",&y
,&x
))
927 rtn2
= mvwgetstr(self
->win
,y
,x
,rtn
);
930 PyErr_SetString(PyExc_TypeError
, "getstr requires 0 or 2 arguments");
936 return PyString_FromString(rtn
);
940 PyCursesWindow_InCh(self
,arg
)
941 PyCursesWindowObject
*self
;
946 switch (ARG_COUNT(arg
)) {
948 rtn
= winch(self
->win
);
951 if (!PyArg_Parse(arg
,"(ii);y,x",&y
,&x
))
953 rtn
= mvwinch(self
->win
,y
,x
);
956 PyErr_SetString(PyExc_TypeError
, "inch requires 0 or 2 arguments");
960 return PyInt_FromLong((long) rtn
);
964 PyCursesWindow_ClearOk(self
,arg
)
965 PyCursesWindowObject
*self
;
969 if (!PyArg_Parse(arg
,"i;True(1) or False(0)",&val
))
971 clearok(self
->win
,val
);
977 PyCursesWindow_IdlOk(self
,arg
)
978 PyCursesWindowObject
*self
;
982 if (!PyArg_Parse(arg
,"i;True(1) or False(0)",&val
))
984 idlok(self
->win
,val
);
990 PyCursesWindow_LeaveOk(self
,arg
)
991 PyCursesWindowObject
*self
;
995 if (!PyArg_Parse(arg
,"i;True(1) or False(0)",&val
))
997 leaveok(self
->win
,val
);
1003 PyCursesWindow_ScrollOk(self
,arg
)
1004 PyCursesWindowObject
*self
;
1008 if (!PyArg_Parse(arg
,"i;True(1) or False(0)",&val
))
1010 scrollok(self
->win
,val
);
1016 PyCursesWindow_SetScrollRegion(self
,arg
)
1017 PyCursesWindowObject
*self
;
1021 if (!PyArg_Parse(arg
,"(ii);top, bottom",&y
,&x
))
1023 return PyCursesCheckERR(wsetscrreg(self
->win
,y
,x
), "wsetscrreg");
1027 PyCursesWindow_KeyPad(self
,arg
)
1028 PyCursesWindowObject
* self
;
1032 if (!PyArg_Parse(arg
,"i;True(1), False(0)",&ch
))
1034 keypad(self
->win
,ch
);
1040 PyCursesWindow_NoDelay(self
,arg
)
1041 PyCursesWindowObject
* self
;
1045 if (!PyArg_Parse(arg
,"i;True(1), False(0)",&ch
))
1047 nodelay(self
->win
,ch
);
1053 PyCursesWindow_NoTimeout(self
,arg
)
1054 PyCursesWindowObject
* self
;
1058 if (!PyArg_Parse(arg
,"i;True(1), False(0)",&ch
))
1060 notimeout(self
->win
,ch
);
1065 static PyMethodDef PyCursesWindow_Methods
[] = {
1066 {"refresh", (PyCFunction
)PyCursesWindow_Refresh
},
1067 {"nooutrefresh", (PyCFunction
)PyCursesWindow_NoOutRefresh
},
1068 {"mvwin", (PyCFunction
)PyCursesWindow_MoveWin
},
1069 {"move", (PyCFunction
)PyCursesWindow_Move
},
1070 {"subwin", (PyCFunction
)PyCursesWindow_SubWin
},
1071 {"addch", (PyCFunction
)PyCursesWindow_AddCh
},
1072 {"insch", (PyCFunction
)PyCursesWindow_InsCh
},
1073 {"delch", (PyCFunction
)PyCursesWindow_DelCh
},
1074 {"echochar", (PyCFunction
)PyCursesWindow_EchoChar
},
1075 {"addstr", (PyCFunction
)PyCursesWindow_AddStr
},
1076 {"attron", (PyCFunction
)PyCursesWindow_AttrOn
},
1077 {"attroff", (PyCFunction
)PyCursesWindow_AttrOff
},
1078 {"attrset", (PyCFunction
)PyCursesWindow_AttrSet
},
1079 {"standend", (PyCFunction
)PyCursesWindow_StandEnd
},
1080 {"standout", (PyCFunction
)PyCursesWindow_StandOut
},
1081 {"border", (PyCFunction
)PyCursesWindow_Border
, METH_VARARGS
},
1082 {"box", (PyCFunction
)PyCursesWindow_Box
},
1083 {"hline", (PyCFunction
)PyCursesWindow_Hline
},
1084 {"vline", (PyCFunction
)PyCursesWindow_Vline
},
1085 {"erase", (PyCFunction
)PyCursesWindow_Erase
},
1086 {"deleteln", (PyCFunction
)PyCursesWindow_DeleteLine
},
1087 {"insertln", (PyCFunction
)PyCursesWindow_InsertLine
},
1088 {"getyx", (PyCFunction
)PyCursesWindow_GetYX
},
1089 {"getbegyx", (PyCFunction
)PyCursesWindow_GetBegYX
},
1090 {"getmaxyx", (PyCFunction
)PyCursesWindow_GetMaxYX
},
1091 {"clear", (PyCFunction
)PyCursesWindow_Clear
},
1092 {"clrtobot", (PyCFunction
)PyCursesWindow_ClearToBottom
},
1093 {"clrtoeol", (PyCFunction
)PyCursesWindow_ClearToEOL
},
1094 {"scroll", (PyCFunction
)PyCursesWindow_Scroll
},
1095 {"touchwin", (PyCFunction
)PyCursesWindow_TouchWin
},
1096 {"touchline", (PyCFunction
)PyCursesWindow_TouchLine
},
1097 {"getch", (PyCFunction
)PyCursesWindow_GetCh
},
1098 {"getstr", (PyCFunction
)PyCursesWindow_GetStr
},
1099 {"inch", (PyCFunction
)PyCursesWindow_InCh
},
1100 {"clearok", (PyCFunction
)PyCursesWindow_ClearOk
},
1101 {"idlok", (PyCFunction
)PyCursesWindow_IdlOk
},
1102 {"leaveok", (PyCFunction
)PyCursesWindow_LeaveOk
},
1103 {"scrollok", (PyCFunction
)PyCursesWindow_ScrollOk
},
1104 {"setscrreg", (PyCFunction
)PyCursesWindow_SetScrollRegion
},
1105 {"keypad", (PyCFunction
)PyCursesWindow_KeyPad
},
1106 {"nodelay", (PyCFunction
)PyCursesWindow_NoDelay
},
1107 {"notimeout", (PyCFunction
)PyCursesWindow_NoTimeout
},
1108 {NULL
, NULL
} /* sentinel */
1112 PyCursesWindow_GetAttr(self
, name
)
1113 PyCursesWindowObject
*self
;
1116 return Py_FindMethod(PyCursesWindow_Methods
, (PyObject
*)self
, name
);
1120 /* --------------- PAD routines ---------------- */
1124 PyCursesPad_New(pad
)
1127 PyCursesPadObject
*po
;
1128 po
= PyObject_NEW(PyCursesPadObject
, &PyCursesPad_Type
);
1132 return (PyObject
*)po
;
1137 /* -------------------------------------------------------*/
1140 static PyTypeObject PyCursesScreen_Type
= {
1141 PyObject_HEAD_INIT(&PyType_Type
)
1143 "curses screen", /*tp_name*/
1144 sizeof(PyCursesScreenObject
), /*tp_basicsize*/
1147 (destructor
)0 /*PyCursesScreen_Dealloc*/, /*tp_dealloc*/
1149 (getattrfunc
)0, /*tp_getattr*/
1150 (setattrfunc
)0, /*tp_setattr*/
1154 0, /*tp_as_sequence*/
1155 0, /*tp_as_mapping*/
1160 static PyTypeObject PyCursesWindow_Type
= {
1161 PyObject_HEAD_INIT(&PyType_Type
)
1163 "curses window", /*tp_name*/
1164 sizeof(PyCursesWindowObject
), /*tp_basicsize*/
1167 (destructor
)PyCursesWindow_Dealloc
, /*tp_dealloc*/
1169 (getattrfunc
)PyCursesWindow_GetAttr
, /*tp_getattr*/
1170 (setattrfunc
)0, /*tp_setattr*/
1174 0, /*tp_as_sequence*/
1175 0, /*tp_as_mapping*/
1180 static PyTypeObject PyCursesPad_Type
= {
1181 PyObject_HEAD_INIT(&PyType_Type
)
1183 "curses pad", /*tp_name*/
1184 sizeof(PyCursesPadObject
), /*tp_basicsize*/
1187 (destructor
)0 /*PyCursesPad_Dealloc*/, /*tp_dealloc*/
1189 (getattrfunc
)0, /*tp_getattr*/
1190 (setattrfunc
)0, /*tp_setattr*/
1194 0, /*tp_as_sequence*/
1195 0, /*tp_as_mapping*/
1201 /* -------------------------------------------------------*/
1203 static PyObject
*ModDict
;
1206 PyCurses_InitScr(self
, args
)
1211 if (!PyArg_NoArgs(args
))
1213 if (initialised
== TRUE
) {
1215 return (PyObject
*)PyCursesWindow_New(stdscr
);
1220 PyErr_SetString(PyCursesError
, catchall_NULL
);
1226 /* This was moved from initcurses() because core dumped on SGI */
1227 /* Also, they are probably not defined until you've called initscr() */
1228 #define SetDictInt(string,ch) \
1229 PyDict_SetItemString(ModDict,string,PyInt_FromLong((long) (ch)));
1231 /* Here are some graphic symbols you can use */
1232 SetDictInt("ACS_ULCORNER",(ACS_ULCORNER
));
1233 SetDictInt("ACS_ULCORNER",(ACS_ULCORNER
));
1234 SetDictInt("ACS_LLCORNER",(ACS_LLCORNER
));
1235 SetDictInt("ACS_URCORNER",(ACS_URCORNER
));
1236 SetDictInt("ACS_LRCORNER",(ACS_LRCORNER
));
1237 SetDictInt("ACS_RTEE", (ACS_RTEE
));
1238 SetDictInt("ACS_LTEE", (ACS_LTEE
));
1239 SetDictInt("ACS_BTEE", (ACS_BTEE
));
1240 SetDictInt("ACS_TTEE", (ACS_TTEE
));
1241 SetDictInt("ACS_HLINE", (ACS_HLINE
));
1242 SetDictInt("ACS_VLINE", (ACS_VLINE
));
1243 SetDictInt("ACS_PLUS", (ACS_PLUS
));
1244 SetDictInt("ACS_S1", (ACS_S1
));
1245 SetDictInt("ACS_S9", (ACS_S9
));
1246 SetDictInt("ACS_DIAMOND", (ACS_DIAMOND
));
1247 SetDictInt("ACS_CKBOARD", (ACS_CKBOARD
));
1248 SetDictInt("ACS_DEGREE", (ACS_DEGREE
));
1249 SetDictInt("ACS_PLMINUS", (ACS_PLMINUS
));
1250 SetDictInt("ACS_BULLET", (ACS_BULLET
));
1251 SetDictInt("ACS_LARROW", (ACS_LARROW
));
1252 SetDictInt("ACS_RARROW", (ACS_RARROW
));
1253 SetDictInt("ACS_DARROW", (ACS_DARROW
));
1254 SetDictInt("ACS_UARROW", (ACS_UARROW
));
1255 SetDictInt("ACS_BOARD", (ACS_BOARD
));
1256 SetDictInt("ACS_LANTERN", (ACS_LANTERN
));
1257 SetDictInt("ACS_BLOCK", (ACS_BLOCK
));
1259 return (PyObject
*)PyCursesWindow_New(win
);
1263 PyCurses_EndWin(self
, args
)
1267 if (!PyArg_NoArgs(args
) || !PyCursesInitialised())
1269 return PyCursesCheckERR(endwin(), "endwin");
1273 PyCurses_IsEndWin(self
, args
)
1277 if (!PyArg_NoArgs(args
))
1279 if (isendwin() == FALSE
) {
1280 Py_INCREF(Py_False
);
1288 PyCurses_DoUpdate(self
,arg
)
1292 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1294 return PyCursesCheckERR(doupdate(), "doupdate");
1298 PyCurses_NewWindow(self
,arg
)
1303 int nlines
, ncols
, begin_y
, begin_x
;
1305 if (!PyCursesInitialised())
1308 switch (ARG_COUNT(arg
)) {
1310 if (!PyArg_Parse(arg
,"(ii);begin)_y,begin_x",&begin_y
,&begin_x
))
1314 if (!PyArg_Parse(arg
, "(iiii);nlines,ncols,begin_y,begin_x",
1315 &nlines
,&ncols
,&begin_y
,&begin_x
))
1319 PyErr_SetString(PyExc_TypeError
, "newwin requires 2 or 4 arguments");
1323 win
= newwin(nlines
,ncols
,begin_y
,begin_x
);
1325 PyErr_SetString(PyCursesError
, catchall_NULL
);
1329 return (PyObject
*)PyCursesWindow_New(win
);
1333 PyCurses_Beep(self
,arg
)
1337 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1345 PyCurses_Flash(self
,arg
)
1349 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1357 PyCurses_UngetCh(self
,arg
)
1362 if (!PyArg_Parse(arg
,"i;integer",&ch
) || !PyCursesInitialised())
1364 return PyCursesCheckERR(ungetch(ch
), "ungetch");
1368 PyCurses_FlushInp(self
,arg
)
1372 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1380 PyCurses_CBreak(self
,arg
)
1384 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1386 return PyCursesCheckERR(cbreak(), "cbreak");
1390 PyCurses_NoCBreak(self
,arg
)
1394 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1396 return PyCursesCheckERR(nocbreak(), "nocbreak");
1400 PyCurses_Echo(self
,arg
)
1404 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1406 return PyCursesCheckERR(echo(), "echo");
1410 PyCurses_NoEcho(self
,arg
)
1414 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1416 return PyCursesCheckERR(noecho(), "noecho");
1420 PyCurses_Nl(self
,arg
)
1424 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1426 return PyCursesCheckERR(nl(), "nl");
1430 PyCurses_NoNl(self
,arg
)
1434 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1436 return PyCursesCheckERR(nonl(), "nonl");
1440 PyCurses_Raw(self
,arg
)
1444 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1446 return PyCursesCheckERR(raw(), "raw");
1450 PyCurses_NoRaw(self
,arg
)
1454 if (!PyArg_NoArgs(arg
) || !PyCursesInitialised())
1456 return PyCursesCheckERR(noraw(), "noraw");
1460 PyCurses_IntrFlush(self
,arg
)
1465 if (!PyArg_Parse(arg
,"i;True(1), False(0)",&ch
))
1467 return PyCursesCheckERR(intrflush(NULL
,ch
), "intrflush");
1471 PyCurses_Meta(self
,arg
)
1476 if (!PyArg_Parse(arg
,"i;True(1), False(0)",&ch
) || !PyCursesInitialised())
1478 return PyCursesCheckERR(meta(stdscr
, ch
), "meta");
1482 PyCurses_KeyName(self
,arg
)
1488 if (!PyArg_Parse(arg
,"i",&ch
))
1491 return PyString_FromString((knp
== NULL
) ? "" : knp
);
1496 PyCurses_NewTerm(self
, args
)
1503 PyCurses_SetTerm(self
, args
)
1510 /* List of functions defined in the module */
1512 static PyMethodDef PyCurses_methods
[] = {
1513 {"initscr", (PyCFunction
)PyCurses_InitScr
},
1514 {"endwin", (PyCFunction
)PyCurses_EndWin
},
1515 {"isendwin", (PyCFunction
)PyCurses_IsEndWin
},
1516 {"doupdate", (PyCFunction
)PyCurses_DoUpdate
},
1517 {"newwin", (PyCFunction
)PyCurses_NewWindow
},
1518 {"beep", (PyCFunction
)PyCurses_Beep
},
1519 {"flash", (PyCFunction
)PyCurses_Flash
},
1520 {"ungetch", (PyCFunction
)PyCurses_UngetCh
},
1521 {"flushinp", (PyCFunction
)PyCurses_FlushInp
},
1522 {"cbreak", (PyCFunction
)PyCurses_CBreak
},
1523 {"nocbreak", (PyCFunction
)PyCurses_NoCBreak
},
1524 {"echo", (PyCFunction
)PyCurses_Echo
},
1525 {"noecho", (PyCFunction
)PyCurses_NoEcho
},
1526 {"nl", (PyCFunction
)PyCurses_Nl
},
1527 {"nonl", (PyCFunction
)PyCurses_NoNl
},
1528 {"raw", (PyCFunction
)PyCurses_Raw
},
1529 {"noraw", (PyCFunction
)PyCurses_NoRaw
},
1530 {"intrflush", (PyCFunction
)PyCurses_IntrFlush
},
1531 {"meta", (PyCFunction
)PyCurses_Meta
},
1532 {"keyname", (PyCFunction
)PyCurses_KeyName
},
1534 {"newterm", (PyCFunction
)PyCurses_NewTerm
},
1535 {"set_term", (PyCFunction
)PyCurses_SetTerm
},
1537 {NULL
, NULL
} /* sentinel */
1540 /* Initialization function for the module */
1545 PyObject
*m
, *d
, *v
;
1547 /* Create the module and add the functions */
1548 m
= Py_InitModule("curses", PyCurses_methods
);
1550 /* Add some symbolic constants to the module */
1551 d
= PyModule_GetDict(m
);
1552 ModDict
= d
; /* For PyCurses_InitScr */
1554 /* For exception curses.error */
1555 PyCursesError
= PyErr_NewException("curses.error", NULL
, NULL
);
1556 PyDict_SetItemString(d
, "error", PyCursesError
);
1558 /* Make the version available */
1559 v
= PyString_FromString(PyCursesVersion
);
1560 PyDict_SetItemString(d
, "version", v
);
1561 PyDict_SetItemString(d
, "__version__", v
);
1564 /* Here are some attributes you can add to chars to print */
1565 SetDictInt("A_NORMAL", A_NORMAL
);
1566 SetDictInt("A_STANDOUT", A_STANDOUT
);
1567 SetDictInt("A_UNDERLINE", A_UNDERLINE
);
1568 SetDictInt("A_REVERSE", A_REVERSE
);
1569 SetDictInt("A_BLINK", A_BLINK
);
1570 SetDictInt("A_DIM", A_DIM
);
1571 SetDictInt("A_BOLD", A_BOLD
);
1572 SetDictInt("A_ALTCHARSET", A_ALTCHARSET
);
1574 /* Now set everything up for KEY_ variables */
1579 for (key
=KEY_MIN
;key
< KEY_MAX
; key
++) {
1580 key_n
= (char *)keyname(key
);
1581 if (key_n
== NULL
|| strcmp(key_n
,"UNKNOWN KEY")==0)
1583 if (strncmp(key_n
,"KEY_F(",6)==0) {
1585 key_n2
= malloc(strlen(key_n
)+1);
1589 if (*p1
!= '(' && *p1
!= ')') {
1598 PyDict_SetItemString(d
,key_n2
,PyInt_FromLong((long) key
));
1599 if (key_n2
!= key_n
)
1602 SetDictInt("KEY_MIN", KEY_MIN
);
1603 SetDictInt("KEY_MAX", KEY_MAX
);