blob: 151305d882ce936f2fc31d65939eb581bf159295 [file] [log] [blame]
Guido van Rossumf6971e21994-08-30 12:25:20 +00001/***********************************************************
2Copyright 1994 by Lance Ellinghouse,
3Cathedral City, California Republic, United States of America.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the name of Lance Ellinghouse
12not be used in advertising or publicity pertaining to distribution
13of the software without specific, written prior permission.
14
15LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL,
18INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
19FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
21WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossumfbea2f31994-08-31 22:05:27 +000025/******************************************************************
Guido van Rossum56bf2351994-08-31 22:06:24 +000026This is a curses implementation. I have tried to be as complete
Guido van Rossumfbea2f31994-08-31 22:05:27 +000027as possible. If there are functions you need that are not included,
28please let me know and/or send me some diffs.
29
30There 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
34 in curses.
35
36Most of the routines can be looked up using the curses man page.
37
38Here is a list of the currently supported methods and attributes
39in the curses module:
40
41Return Value Func/Attr Description
42--------------------------------------------------------------------------
Guido van Rossum85738471995-02-17 13:50:17 +000043StringObject version A string representing the current
44 version of this module.
Guido van Rossumfbea2f31994-08-31 22:05:27 +000045WindowObject initscr() This initializes the screen for use
46None endwin() Closes down the screen and returns
47 things as they were before calling
48 initscr()
49True/FalseObject isendwin() Has endwin() been called?
Guido van Rossum85738471995-02-17 13:50:17 +000050None doupdate() Updates screen
Guido van Rossumfbea2f31994-08-31 22:05:27 +000051WindowObject newwin(nlines,ncols,begin_y,begin_x)
52 newwin(begin_y,begin_x)
53 newwin() creates and returns
54 a new window.
55None beep() Beep the screen if possible
56None flash() Flash the screen if possible
57None ungetch(int) Push the int back so next getch()
58 will return it.
59 Note: argument is an INT, not a CHAR
60None flushinp() Flush all input buffers
61None cbreak() Enter cbreak mode
62None nocbreak() Leave cbreak mode
63None echo() Enter echo mode
64None noecho() Leave echo mode
65None nl() Enter nl mode
66None nonl() Leave nl mode
67None raw() Enter raw mode
68None noraw() Leave raw mode
69None intrflush(int) Set or reset interruptable flush
70 mode, int=1 if set, 0 if notset.
71None meta(int) Allow 8 bit or 7 bit chars.
72 int=1 is 8 bit, int=0 is 7 bit
73StringObject keyname(int) return the text representation
74 of a KEY_ value. (see below)
75
76Here is a list of the currently supported methods and attributes
77in the WindowObject:
78
79Return Value Func/Attr Description
80--------------------------------------------------------------------------
Guido van Rossum85738471995-02-17 13:50:17 +000081None refresh() Do refresh
82None nooutrefresh() Mark for refresh but wait
83None mvwin(new_y,new_x) Move Window
84None move(new_y,new_x) Move Cursor
Guido van Rossumfbea2f31994-08-31 22:05:27 +000085WindowObject subwin(nlines,ncols,begin_y,begin_x)
86 subwin(begin_y,begin_x)
Guido van Rossum85738471995-02-17 13:50:17 +000087None addch(y,x,ch,attr)
Guido van Rossumfbea2f31994-08-31 22:05:27 +000088 addch(y,x,ch)
89 addch(ch,attr)
90 addch(ch)
Guido van Rossum85738471995-02-17 13:50:17 +000091None insch(y,x,ch,attr)
Guido van Rossumfbea2f31994-08-31 22:05:27 +000092 insch(y,x,ch)
93 insch(ch,attr)
94 insch(ch)
Guido van Rossum85738471995-02-17 13:50:17 +000095None delch(y,x)
Guido van Rossumfbea2f31994-08-31 22:05:27 +000096 delch()
Guido van Rossum85738471995-02-17 13:50:17 +000097None echochar(ch,attr)
Guido van Rossumfbea2f31994-08-31 22:05:27 +000098 echochar(ch)
Guido van Rossum85738471995-02-17 13:50:17 +000099None addstr(y,x,str,attr)
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000100 addstr(y,x,str)
101 addstr(str,attr)
102 addstr(str)
Guido van Rossum85738471995-02-17 13:50:17 +0000103None attron(attr)
104None attroff(attr)
105None attrset(sttr)
106None standend()
107None standout()
108None box(vertch,horch) vertch and horch are INTS
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000109 box()
110None erase()
111None deleteln()
112None insertln()
113(y,x) getyx()
114(y,x) getbegyx()
115(y,x) getmaxyx()
116None clear()
117None clrtobot()
118None clrtoeol()
119None scroll()
120None touchwin()
121None touchline(start,count)
122IntObject getch(y,x)
123 getch()
124StringObject getstr(y,x)
125 getstr()
126IntObject inch(y,x)
127 inch()
128None clearok(int) int=0 or int=1
129None idlok(int) int=0 or int=1
130None leaveok(int) int=0 or int=1
131None scrollok(int) int=0 or int=1
132None setscrreg(top,bottom)
Guido van Rossum8fbf82b1995-02-17 13:54:04 +0000133None keypad(int) int=0 or int=1
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000134None nodelay(int) int=0 or int=1
135None notimeout(int) int=0 or int=1
136******************************************************************/
137
138
Guido van Rossumf6971e21994-08-30 12:25:20 +0000139/* curses module */
140
Guido van Rossum602099a1994-09-14 13:32:22 +0000141#include "Python.h"
Guido van Rossumf6971e21994-08-30 12:25:20 +0000142
143#include <curses.h>
144
Guido van Rossumf6971e21994-08-30 12:25:20 +0000145typedef struct {
146 PyObject_HEAD
147 SCREEN *scr;
148} PyCursesScreenObject;
149
150typedef struct {
151 PyObject_HEAD
152 WINDOW *win;
153 WINDOW *parent;
154} PyCursesWindowObject;
155
156typedef struct {
157 PyObject_HEAD
158 WINDOW *pad;
159} PyCursesPadObject;
160
161staticforward PyTypeObject PyCursesScreen_Type;
162staticforward PyTypeObject PyCursesWindow_Type;
163staticforward PyTypeObject PyCursesPad_Type;
164
165#define PyCursesScreen_Check(v) ((v)->ob_type == &PyCursesScreen_Type)
166#define PyCursesWindow_Check(v) ((v)->ob_type == &PyCursesWindow_Type)
167#define PyCursesPad_Check(v) ((v)->ob_type == &PyCursesPad_Type)
168
169/* Defines */
Guido van Rossum85738471995-02-17 13:50:17 +0000170static PyObject *PyCursesError; /* For exception curses.error */
171
172/* Catch-all error messages */
173static char *catchall_ERR = "curses function returned ERR";
174static char *catchall_NULL = "curses function returned NULL";
175
176#define ARG_COUNT(X) \
177 (((X) == NULL) ? 0 : (PyTuple_Check(X) ? PyTuple_Size(X) : 1))
Guido van Rossumf6971e21994-08-30 12:25:20 +0000178
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000179/******************************************************************
180
181Change Log:
182
Guido van Rossum85738471995-02-17 13:50:17 +0000183Version 1.2: 95/02/17 (Steve Clift)
184 Fixed several potential core-dumping bugs.
185 Reworked arg parsing where variable arg lists are used.
186 Generate exceptions when ERR or NULL is returned by curses functions.
187 Changed return types to match SysV Curses manual descriptions.
188 Added keypad() to window method list doco above.
189
Guido van Rossum56bf2351994-08-31 22:06:24 +0000190Version 1.1: 94/08/31:
191 Minor fixes given by Guido.
192 Changed 'ncurses' to 'curses'
193 Changed '__version__' to 'version'
194 Added PyErr_Clear() where needed
195 Moved ACS_* attribute initialization to PyCurses_InitScr() to fix
196 crash on SGI
Guido van Rossum85738471995-02-17 13:50:17 +0000197
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000198Version 1.0: 94/08/30:
199 This is the first release of this software.
200 Released to the Internet via python-list@cwi.nl
201
202******************************************************************/
Guido van Rossum85738471995-02-17 13:50:17 +0000203
Guido van Rossum8fbf82b1995-02-17 13:54:04 +0000204char *PyCursesVersion = "1.2";
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000205
Guido van Rossum85738471995-02-17 13:50:17 +0000206/*
207 * Check the return code from a curses function and return None
208 * or raise an exception as appropriate.
209 */
210
211static PyObject *
212PyCursesCheckERR(code, fname)
213 int code;
214 char *fname;
215{
216 char buf[100];
217
218 if (code != ERR) {
219 Py_INCREF(Py_None);
220 return Py_None;
221 } else {
222 if (fname == NULL) {
223 PyErr_SetString(PyCursesError, catchall_ERR);
224 } else {
225 strcpy(buf, fname);
226 strcat(buf, "() returned ERR");
227 PyErr_SetString(PyCursesError, buf);
228 }
229 return NULL;
230 }
231}
232
233
Guido van Rossumf6971e21994-08-30 12:25:20 +0000234/* ------------- SCREEN routines --------------- */
Guido van Rossum85738471995-02-17 13:50:17 +0000235
Guido van Rossumf6971e21994-08-30 12:25:20 +0000236#ifdef NOT_YET
237static PyObject *
238PyCursesScreen_New(arg)
239 PyObject * arg;
240{
241 char *term_type;
242 PyFileObject *in_fo;
243 PyFileObject *out_fo;
244 PyCursesScreenObject *xp;
Guido van Rossum85738471995-02-17 13:50:17 +0000245 xp = PyObject_NEW(PyCursesScreenObject, &PyCursesScreen_Type);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000246 if (xp == NULL)
247 return NULL;
248 return (PyObject *)xp;
249}
250#endif
Guido van Rossum85738471995-02-17 13:50:17 +0000251
252
Guido van Rossumf6971e21994-08-30 12:25:20 +0000253/* ------------- WINDOW routines --------------- */
Guido van Rossum85738471995-02-17 13:50:17 +0000254
Guido van Rossumf6971e21994-08-30 12:25:20 +0000255static PyObject *
256PyCursesWindow_New(win)
257 WINDOW *win;
258{
259 PyCursesWindowObject *wo;
Guido van Rossum85738471995-02-17 13:50:17 +0000260
261 wo = PyObject_NEW(PyCursesWindowObject, &PyCursesWindow_Type);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000262 if (wo == NULL)
263 return NULL;
264 wo->win = win;
265 wo->parent = (WINDOW *)NULL;
266 return (PyObject *)wo;
267}
268
269static void
270PyCursesWindow_Dealloc(wo)
271 PyCursesWindowObject *wo;
272{
273 if (wo->win != stdscr)
274 delwin(wo->win);
275 PyMem_DEL(wo);
276}
277
278static PyObject *
279PyCursesWindow_Refresh(self,arg)
280 PyCursesWindowObject *self;
281 PyObject * arg;
282{
283 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +0000284 return NULL;
285 return PyCursesCheckERR(wrefresh(self->win), "wrefresh");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000286}
287
288static PyObject *
289PyCursesWindow_NoOutRefresh(self,arg)
290 PyCursesWindowObject *self;
291 PyObject * arg;
292{
293 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +0000294 return NULL;
295 return PyCursesCheckERR(wnoutrefresh(self->win), "wnoutrefresh");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000296}
297
298static PyObject *
299PyCursesWindow_MoveWin(self,arg)
300 PyCursesWindowObject *self;
301 PyObject * arg;
302{
Guido van Rossumf6971e21994-08-30 12:25:20 +0000303 int x, y;
Guido van Rossum85738471995-02-17 13:50:17 +0000304 if (!PyArg_Parse(arg,"(ii);y,x", &y, &x))
305 return NULL;
306 return PyCursesCheckERR(mvwin(self->win,y,x), "mvwin");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000307}
308
309static PyObject *
310PyCursesWindow_Move(self,arg)
311 PyCursesWindowObject *self;
312 PyObject * arg;
313{
Guido van Rossumf6971e21994-08-30 12:25:20 +0000314 int x, y;
Guido van Rossum85738471995-02-17 13:50:17 +0000315 if (!PyArg_Parse(arg,"(ii);y,x", &y, &x))
316 return NULL;
317 return PyCursesCheckERR(wmove(self->win,y,x), "wmove");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000318}
319
320static PyObject *
321PyCursesWindow_SubWin(self,arg)
322 PyCursesWindowObject *self;
323 PyObject * arg;
324{
325 WINDOW *win;
326 PyCursesWindowObject *rtn_win;
327 int nlines, ncols, begin_y, begin_x;
Guido van Rossum85738471995-02-17 13:50:17 +0000328
Guido van Rossumf6971e21994-08-30 12:25:20 +0000329 nlines = 0;
330 ncols = 0;
Guido van Rossum85738471995-02-17 13:50:17 +0000331 switch (ARG_COUNT(arg)) {
332 case 2:
333 if (!PyArg_Parse(arg,"(ii);begin_y,begin_x",&begin_y,&begin_x))
334 return NULL;
335 break;
336 case 4:
337 if (!PyArg_Parse(arg, "(iiii);nlines,ncols,begin_y,begin_x",
Guido van Rossumf6971e21994-08-30 12:25:20 +0000338 &nlines,&ncols,&begin_y,&begin_x))
Guido van Rossum85738471995-02-17 13:50:17 +0000339 return NULL;
340 break;
341 default:
342 PyErr_SetString(PyExc_TypeError, "subwin requires 2 or 4 arguments");
343 return NULL;
344 }
Guido van Rossumf6971e21994-08-30 12:25:20 +0000345 win = subwin(self->win,nlines,ncols,begin_y,begin_x);
346 if (win == NULL) {
Guido van Rossum85738471995-02-17 13:50:17 +0000347 PyErr_SetString(PyCursesError, catchall_NULL);
348 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000349 }
350 rtn_win = (PyCursesWindowObject *)PyCursesWindow_New(win);
351 rtn_win->parent = self->win;
352 return (PyObject *)rtn_win;
353}
354
355static PyObject *
356PyCursesWindow_AddCh(self,arg)
357 PyCursesWindowObject *self;
358 PyObject * arg;
359{
360 int rtn;
361 int x, y;
362 int ch;
Guido van Rossum85738471995-02-17 13:50:17 +0000363 int attr, attr_old;
364 int use_xy = FALSE, use_attr = FALSE;
365
366 switch (ARG_COUNT(arg)) {
367 case 1:
368 if (!PyArg_Parse(arg, "i;ch", &ch))
369 return NULL;
370 break;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000371 case 2:
Guido van Rossum85738471995-02-17 13:50:17 +0000372 if (!PyArg_Parse(arg,"(ii);ch,attr", &ch, &attr))
373 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000374 use_attr = TRUE;
375 break;
Guido van Rossum85738471995-02-17 13:50:17 +0000376 case 3:
377 if (!PyArg_Parse(arg,"(iii);y,x,ch", &y, &x, &ch))
378 return NULL;
379 use_xy = TRUE;
380 break;
381 case 4:
382 if (!PyArg_Parse(arg,"(iiii);y,x,ch,attr", &y, &x, &ch, &attr))
383 return NULL;
384 use_xy = use_attr = TRUE;
385 break;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000386 default:
Guido van Rossum85738471995-02-17 13:50:17 +0000387 PyErr_SetString(PyExc_TypeError, "addch requires 1 to 4 arguments");
388 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000389 }
Guido van Rossum85738471995-02-17 13:50:17 +0000390
Guido van Rossumf6971e21994-08-30 12:25:20 +0000391 if (use_attr == TRUE) {
392 attr_old = getattrs(self->win);
393 wattrset(self->win,attr);
394 }
395 if (use_xy == TRUE)
396 rtn = mvwaddch(self->win,y,x,ch);
397 else
398 rtn = waddch(self->win,ch);
399 if (use_attr == TRUE)
400 wattrset(self->win,attr_old);
Guido van Rossum85738471995-02-17 13:50:17 +0000401
402 return PyCursesCheckERR(rtn, "[mv]waddch");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000403}
404
405static PyObject *
406PyCursesWindow_InsCh(self,arg)
407 PyCursesWindowObject *self;
408 PyObject * arg;
409{
410 int rtn;
411 int x, y;
412 int ch;
Guido van Rossum85738471995-02-17 13:50:17 +0000413 int attr, attr_old;
414 int use_xy = TRUE, use_attr = FALSE;
415
416 switch (ARG_COUNT(arg)) {
417 case 1:
418 if (!PyArg_Parse(arg, "i;ch", &ch))
419 return NULL;
420 break;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000421 case 2:
Guido van Rossum85738471995-02-17 13:50:17 +0000422 if (!PyArg_Parse(arg,"(ii);ch,attr", &ch, &attr))
423 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000424 use_attr = TRUE;
425 break;
Guido van Rossum85738471995-02-17 13:50:17 +0000426 case 3:
427 if (!PyArg_Parse(arg,"(iii);y,x,ch", &y, &x, &ch))
428 return NULL;
429 use_xy = TRUE;
430 break;
431 case 4:
432 if (!PyArg_Parse(arg,"(iiii);y,x,ch,attr", &y, &x, &ch, &attr))
433 return NULL;
434 use_xy = use_attr = TRUE;
435 break;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000436 default:
Guido van Rossum85738471995-02-17 13:50:17 +0000437 PyErr_SetString(PyExc_TypeError, "insch requires 1 to 4 arguments");
438 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000439 }
Guido van Rossum85738471995-02-17 13:50:17 +0000440
Guido van Rossumf6971e21994-08-30 12:25:20 +0000441 if (use_attr == TRUE) {
442 attr_old = getattrs(self->win);
443 wattrset(self->win,attr);
444 }
445 if (use_xy == TRUE)
446 rtn = mvwinsch(self->win,y,x,ch);
447 else
448 rtn = winsch(self->win,ch);
449 if (use_attr == TRUE)
450 wattrset(self->win,attr_old);
Guido van Rossum85738471995-02-17 13:50:17 +0000451
452 return PyCursesCheckERR(rtn, "[mv]winsch");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000453}
454
455static PyObject *
456PyCursesWindow_DelCh(self,arg)
457 PyCursesWindowObject *self;
458 PyObject * arg;
459{
460 int rtn;
461 int x, y;
Guido van Rossum85738471995-02-17 13:50:17 +0000462
463 switch (ARG_COUNT(arg)) {
464 case 0:
Guido van Rossumf6971e21994-08-30 12:25:20 +0000465 rtn = wdelch(self->win);
Guido van Rossum85738471995-02-17 13:50:17 +0000466 break;
467 case 2:
468 if (!PyArg_Parse(arg,"(ii);y,x", &y, &x))
469 return NULL;
470 rtn = mvwdelch(self->win,y,x);
471 break;
472 default:
473 PyErr_SetString(PyExc_TypeError, "delch requires 0 or 2 arguments");
474 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000475 }
Guido van Rossum85738471995-02-17 13:50:17 +0000476
477 return PyCursesCheckERR(rtn, "[mv]wdelch");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000478}
479
480static PyObject *
481PyCursesWindow_EchoChar(self,arg)
482 PyCursesWindowObject *self;
483 PyObject * arg;
484{
485 int rtn;
486 int ch;
Guido van Rossum85738471995-02-17 13:50:17 +0000487 int attr, attr_old;
488
489 switch (ARG_COUNT(arg)) {
490 case 1:
Guido van Rossumf6971e21994-08-30 12:25:20 +0000491 if (!PyArg_Parse(arg,"i;ch", &ch))
Guido van Rossum85738471995-02-17 13:50:17 +0000492 return NULL;
493 rtn = wechochar(self->win,ch);
494 break;
495 case 2:
496 if (!PyArg_Parse(arg,"(ii);ch,attr", &ch, &attr))
497 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000498 attr_old = getattrs(self->win);
499 wattrset(self->win,attr);
Guido van Rossum85738471995-02-17 13:50:17 +0000500 rtn = wechochar(self->win,ch);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000501 wattrset(self->win,attr_old);
Guido van Rossum85738471995-02-17 13:50:17 +0000502 break;
503 default:
504 PyErr_SetString(PyExc_TypeError, "echochar requires 1 or 2 arguments");
505 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000506 }
Guido van Rossum85738471995-02-17 13:50:17 +0000507
508 return PyCursesCheckERR(rtn, "wechochar");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000509}
510
511static PyObject *
512PyCursesWindow_AddStr(self,arg)
513 PyCursesWindowObject *self;
514 PyObject * arg;
515{
516 int rtn;
517 int x, y;
518 char *str;
Guido van Rossum85738471995-02-17 13:50:17 +0000519 int attr, attr_old;
520 int use_xy = FALSE, use_attr = FALSE;
521
522 switch (ARG_COUNT(arg)) {
523 case 1:
524 if (!PyArg_Parse(arg,"s;str", &str))
525 return NULL;
526 break;
527 case 2:
528 if (!PyArg_Parse(arg,"(si);str,attr", &str, &attr))
529 return NULL;
530 use_attr = TRUE;
531 break;
532 case 3:
533 if (!PyArg_Parse(arg,"(iis);y,x,str", &y, &x, &str))
534 return NULL;
535 use_xy = TRUE;
536 break;
537 case 4:
538 if (!PyArg_Parse(arg,"(iisi);y,x,str,attr", &y, &x, &str, &attr))
539 return NULL;
540 use_xy = use_attr = TRUE;
541 break;
542 default:
543 PyErr_SetString(PyExc_TypeError, "addstr requires 1 to 4 arguments");
544 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000545 }
Guido van Rossum85738471995-02-17 13:50:17 +0000546
Guido van Rossumf6971e21994-08-30 12:25:20 +0000547 if (use_attr == TRUE) {
548 attr_old = getattrs(self->win);
549 wattrset(self->win,attr);
550 }
551 if (use_xy == TRUE)
552 rtn = mvwaddstr(self->win,y,x,str);
553 else
554 rtn = waddstr(self->win,str);
555 if (use_attr == TRUE)
556 wattrset(self->win,attr_old);
Guido van Rossum85738471995-02-17 13:50:17 +0000557
558 return PyCursesCheckERR(rtn, "[mv]waddstr");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000559}
560
561static PyObject *
562PyCursesWindow_AttrOn(self,arg)
563 PyCursesWindowObject *self;
564 PyObject * arg;
565{
Guido van Rossumf6971e21994-08-30 12:25:20 +0000566 int ch;
567 if (!PyArg_Parse(arg,"i;attr", &ch))
Guido van Rossum85738471995-02-17 13:50:17 +0000568 return NULL;
569 wattron(self->win,ch);
570 Py_INCREF(Py_None);
571 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000572}
573
574static PyObject *
575PyCursesWindow_AttrOff(self,arg)
576 PyCursesWindowObject *self;
577 PyObject * arg;
578{
Guido van Rossumf6971e21994-08-30 12:25:20 +0000579 int ch;
580 if (!PyArg_Parse(arg,"i;attr", &ch))
Guido van Rossum85738471995-02-17 13:50:17 +0000581 return NULL;
582 wattroff(self->win,ch);
583 Py_INCREF(Py_None);
584 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000585}
586
587static PyObject *
588PyCursesWindow_AttrSet(self,arg)
589 PyCursesWindowObject *self;
590 PyObject * arg;
591{
Guido van Rossumf6971e21994-08-30 12:25:20 +0000592 int ch;
593 if (!PyArg_Parse(arg,"i;attr", &ch))
Guido van Rossum85738471995-02-17 13:50:17 +0000594 return NULL;
595 wattrset(self->win,ch);
596 Py_INCREF(Py_None);
597 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000598}
599
600static PyObject *
601PyCursesWindow_StandEnd(self,arg)
602 PyCursesWindowObject *self;
603 PyObject * arg;
604{
Guido van Rossumf6971e21994-08-30 12:25:20 +0000605 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +0000606 return NULL;
607 wstandend(self->win);
608 Py_INCREF(Py_None);
609 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000610}
611
612static PyObject *
613PyCursesWindow_StandOut(self,arg)
614 PyCursesWindowObject *self;
615 PyObject * arg;
616{
Guido van Rossumf6971e21994-08-30 12:25:20 +0000617 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +0000618 return NULL;
619 wstandout(self->win);
620 Py_INCREF(Py_None);
621 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000622}
623
624static PyObject *
625PyCursesWindow_Box(self,arg)
626 PyCursesWindowObject *self;
627 PyObject * arg;
628{
Guido van Rossumf6971e21994-08-30 12:25:20 +0000629 int ch1=0,ch2=0;
630 if (!PyArg_Parse(arg,"(ii);vertch,horch", &ch1, &ch2))
631 PyErr_Clear();
Guido van Rossum85738471995-02-17 13:50:17 +0000632 box(self->win,ch1,ch2);
633 Py_INCREF(Py_None);
634 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000635}
636
637static PyObject *
638PyCursesWindow_Erase(self,arg)
639 PyCursesWindowObject *self;
640 PyObject * arg;
641{
642 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +0000643 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000644 werase(self->win);
645 Py_INCREF(Py_None);
Guido van Rossum85738471995-02-17 13:50:17 +0000646 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000647}
648
649static PyObject *
650PyCursesWindow_DeleteLine(self,arg)
651 PyCursesWindowObject *self;
652 PyObject * arg;
653{
654 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +0000655 return NULL;
656 return PyCursesCheckERR(wdeleteln(self->win), "wdeleteln");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000657}
658
659static PyObject *
660PyCursesWindow_InsertLine(self,arg)
661 PyCursesWindowObject *self;
662 PyObject * arg;
663{
664 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +0000665 return NULL;
666 return PyCursesCheckERR(winsertln(self->win), "winsertln");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000667}
668
669static PyObject *
670PyCursesWindow_GetYX(self,arg)
671 PyCursesWindowObject *self;
672 PyObject * arg;
673{
674 int x, y;
675 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +0000676 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000677 getyx(self->win,y,x);
Guido van Rossum85738471995-02-17 13:50:17 +0000678 return Py_BuildValue("(ii)", y, x);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000679}
680
681static PyObject *
682PyCursesWindow_GetBegYX(self,arg)
683 PyCursesWindowObject *self;
684 PyObject * arg;
685{
686 int x, y;
687 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +0000688 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000689 getbegyx(self->win,y,x);
Guido van Rossum85738471995-02-17 13:50:17 +0000690 return Py_BuildValue("(ii)", y, x);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000691}
692
693static PyObject *
694PyCursesWindow_GetMaxYX(self,arg)
695 PyCursesWindowObject *self;
696 PyObject * arg;
697{
698 int x, y;
699 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +0000700 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000701 getmaxyx(self->win,y,x);
Guido van Rossum85738471995-02-17 13:50:17 +0000702 return Py_BuildValue("(ii)", y, x);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000703}
704
705static PyObject *
706PyCursesWindow_Clear(self,arg)
707 PyCursesWindowObject *self;
708 PyObject * arg;
709{
710 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +0000711 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000712 wclear(self->win);
713 Py_INCREF(Py_None);
Guido van Rossum85738471995-02-17 13:50:17 +0000714 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000715}
716
717static PyObject *
718PyCursesWindow_ClearToBottom(self,arg)
719 PyCursesWindowObject *self;
720 PyObject * arg;
721{
722 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +0000723 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000724 wclrtobot(self->win);
725 Py_INCREF(Py_None);
Guido van Rossum85738471995-02-17 13:50:17 +0000726 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000727}
728
729static PyObject *
730PyCursesWindow_ClearToEOL(self,arg)
731 PyCursesWindowObject *self;
732 PyObject * arg;
733{
734 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +0000735 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000736 wclrtoeol(self->win);
737 Py_INCREF(Py_None);
Guido van Rossum85738471995-02-17 13:50:17 +0000738 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000739}
740
741static PyObject *
742PyCursesWindow_Scroll(self,arg)
743 PyCursesWindowObject *self;
744 PyObject * arg;
745{
746 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +0000747 return NULL;
748 return PyCursesCheckERR(scroll(self->win), "scroll");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000749}
750
751static PyObject *
752PyCursesWindow_TouchWin(self,arg)
753 PyCursesWindowObject *self;
754 PyObject * arg;
755{
756 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +0000757 return NULL;
758 return PyCursesCheckERR(touchwin(self->win), "touchwin");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000759}
760
761static PyObject *
762PyCursesWindow_TouchLine(self,arg)
763 PyCursesWindowObject *self;
764 PyObject * arg;
765{
766 int st, cnt;
Guido van Rossum85738471995-02-17 13:50:17 +0000767 if (!PyArg_Parse(arg,"(ii);start,count",&st,&cnt))
768 return NULL;
769 return PyCursesCheckERR(touchline(self->win,st,cnt), "touchline");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000770}
771
772static PyObject *
773PyCursesWindow_GetCh(self,arg)
774 PyCursesWindowObject *self;
775 PyObject * arg;
776{
777 int x, y;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000778 int rtn;
Guido van Rossum85738471995-02-17 13:50:17 +0000779
780 switch (ARG_COUNT(arg)) {
781 case 0:
Guido van Rossumf6971e21994-08-30 12:25:20 +0000782 rtn = wgetch(self->win);
Guido van Rossum85738471995-02-17 13:50:17 +0000783 break;
784 case 2:
785 if (!PyArg_Parse(arg,"(ii);y,x",&y,&x))
786 return NULL;
787 rtn = mvwgetch(self->win,y,x);
788 break;
789 default:
790 PyErr_SetString(PyExc_TypeError, "getch requires 0 or 2 arguments");
791 return NULL;
792 }
793
794 return PyInt_FromLong((long) rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000795}
796
797static PyObject *
798PyCursesWindow_GetStr(self,arg)
799 PyCursesWindowObject *self;
800 PyObject * arg;
801{
802 int x, y;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000803 char rtn[1024]; /* This should be big enough.. I hope */
804 int rtn2;
Guido van Rossum85738471995-02-17 13:50:17 +0000805
806 switch (ARG_COUNT(arg)) {
807 case 0:
Guido van Rossumf6971e21994-08-30 12:25:20 +0000808 rtn2 = wgetstr(self->win,rtn);
Guido van Rossum85738471995-02-17 13:50:17 +0000809 break;
810 case 2:
811 if (!PyArg_Parse(arg,"(ii);y,x",&y,&x))
812 return NULL;
813 rtn2 = mvwgetstr(self->win,y,x,rtn);
814 break;
815 default:
816 PyErr_SetString(PyExc_TypeError, "getstr requires 0 or 2 arguments");
817 return NULL;
818 }
819
Guido van Rossumf6971e21994-08-30 12:25:20 +0000820 if (rtn2 == ERR)
821 rtn[0] = 0;
Guido van Rossum85738471995-02-17 13:50:17 +0000822 return PyString_FromString(rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000823}
824
825static PyObject *
826PyCursesWindow_InCh(self,arg)
827 PyCursesWindowObject *self;
828 PyObject * arg;
829{
Guido van Rossum85738471995-02-17 13:50:17 +0000830 int x, y, rtn;
831
832 switch (ARG_COUNT(arg)) {
833 case 0:
Guido van Rossumf6971e21994-08-30 12:25:20 +0000834 rtn = winch(self->win);
Guido van Rossum85738471995-02-17 13:50:17 +0000835 break;
836 case 2:
837 if (!PyArg_Parse(arg,"(ii);y,x",&y,&x))
838 return NULL;
839 rtn = mvwinch(self->win,y,x);
840 break;
841 default:
842 PyErr_SetString(PyExc_TypeError, "inch requires 0 or 2 arguments");
843 return NULL;
844 }
845
846 return PyInt_FromLong((long) rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000847}
848
849static PyObject *
850PyCursesWindow_ClearOk(self,arg)
851 PyCursesWindowObject *self;
852 PyObject * arg;
853{
854 int val;
855 if (!PyArg_Parse(arg,"i;True(1) or False(0)",&val))
Guido van Rossum85738471995-02-17 13:50:17 +0000856 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000857 clearok(self->win,val);
858 Py_INCREF(Py_None);
Guido van Rossum85738471995-02-17 13:50:17 +0000859 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000860}
861
862static PyObject *
863PyCursesWindow_IdlOk(self,arg)
864 PyCursesWindowObject *self;
865 PyObject * arg;
866{
867 int val;
868 if (!PyArg_Parse(arg,"i;True(1) or False(0)",&val))
Guido van Rossum85738471995-02-17 13:50:17 +0000869 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000870 idlok(self->win,val);
871 Py_INCREF(Py_None);
Guido van Rossum85738471995-02-17 13:50:17 +0000872 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000873}
874
875static PyObject *
876PyCursesWindow_LeaveOk(self,arg)
877 PyCursesWindowObject *self;
878 PyObject * arg;
879{
880 int val;
881 if (!PyArg_Parse(arg,"i;True(1) or False(0)",&val))
Guido van Rossum85738471995-02-17 13:50:17 +0000882 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000883 leaveok(self->win,val);
884 Py_INCREF(Py_None);
Guido van Rossum85738471995-02-17 13:50:17 +0000885 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000886}
887
888static PyObject *
889PyCursesWindow_ScrollOk(self,arg)
890 PyCursesWindowObject *self;
891 PyObject * arg;
892{
893 int val;
894 if (!PyArg_Parse(arg,"i;True(1) or False(0)",&val))
Guido van Rossum85738471995-02-17 13:50:17 +0000895 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000896 scrollok(self->win,val);
897 Py_INCREF(Py_None);
Guido van Rossum85738471995-02-17 13:50:17 +0000898 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000899}
900
901static PyObject *
902PyCursesWindow_SetScrollRegion(self,arg)
903 PyCursesWindowObject *self;
904 PyObject * arg;
905{
906 int x, y;
907 if (!PyArg_Parse(arg,"(ii);top, bottom",&y,&x))
Guido van Rossum85738471995-02-17 13:50:17 +0000908 return NULL;
909 return PyCursesCheckERR(wsetscrreg(self->win,y,x), "wsetscrreg");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000910}
911
912static PyObject *
913PyCursesWindow_KeyPad(self,arg)
914 PyCursesWindowObject * self;
915 PyObject * arg;
916{
917 int ch;
918 if (!PyArg_Parse(arg,"i;True(1), False(0)",&ch))
Guido van Rossum85738471995-02-17 13:50:17 +0000919 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000920 keypad(self->win,ch);
921 Py_INCREF(Py_None);
Guido van Rossum85738471995-02-17 13:50:17 +0000922 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000923}
924
925static PyObject *
926PyCursesWindow_NoDelay(self,arg)
927 PyCursesWindowObject * self;
928 PyObject * arg;
929{
930 int ch;
931 if (!PyArg_Parse(arg,"i;True(1), False(0)",&ch))
Guido van Rossum85738471995-02-17 13:50:17 +0000932 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000933 nodelay(self->win,ch);
934 Py_INCREF(Py_None);
Guido van Rossum85738471995-02-17 13:50:17 +0000935 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000936}
937
938static PyObject *
939PyCursesWindow_NoTimeout(self,arg)
940 PyCursesWindowObject * self;
941 PyObject * arg;
942{
943 int ch;
944 if (!PyArg_Parse(arg,"i;True(1), False(0)",&ch))
Guido van Rossum85738471995-02-17 13:50:17 +0000945 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000946 notimeout(self->win,ch);
947 Py_INCREF(Py_None);
Guido van Rossum85738471995-02-17 13:50:17 +0000948 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000949}
950
951static PyMethodDef PyCursesWindow_Methods[] = {
952 {"refresh", (PyCFunction)PyCursesWindow_Refresh},
953 {"nooutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh},
954 {"mvwin", (PyCFunction)PyCursesWindow_MoveWin},
955 {"move", (PyCFunction)PyCursesWindow_Move},
956 {"subwin", (PyCFunction)PyCursesWindow_SubWin},
957 {"addch", (PyCFunction)PyCursesWindow_AddCh},
958 {"insch", (PyCFunction)PyCursesWindow_InsCh},
959 {"delch", (PyCFunction)PyCursesWindow_DelCh},
960 {"echochar", (PyCFunction)PyCursesWindow_EchoChar},
961 {"addstr", (PyCFunction)PyCursesWindow_AddStr},
962 {"attron", (PyCFunction)PyCursesWindow_AttrOn},
963 {"attroff", (PyCFunction)PyCursesWindow_AttrOff},
964 {"attrset", (PyCFunction)PyCursesWindow_AttrSet},
965 {"standend", (PyCFunction)PyCursesWindow_StandEnd},
966 {"standout", (PyCFunction)PyCursesWindow_StandOut},
967 {"box", (PyCFunction)PyCursesWindow_Box},
968 {"erase", (PyCFunction)PyCursesWindow_Erase},
969 {"deleteln", (PyCFunction)PyCursesWindow_DeleteLine},
970 {"insertln", (PyCFunction)PyCursesWindow_InsertLine},
971 {"getyx", (PyCFunction)PyCursesWindow_GetYX},
972 {"getbegyx", (PyCFunction)PyCursesWindow_GetBegYX},
973 {"getmaxyx", (PyCFunction)PyCursesWindow_GetMaxYX},
974 {"clear", (PyCFunction)PyCursesWindow_Clear},
975 {"clrtobot", (PyCFunction)PyCursesWindow_ClearToBottom},
976 {"clrtoeol", (PyCFunction)PyCursesWindow_ClearToEOL},
977 {"scroll", (PyCFunction)PyCursesWindow_Scroll},
978 {"touchwin", (PyCFunction)PyCursesWindow_TouchWin},
979 {"touchline", (PyCFunction)PyCursesWindow_TouchLine},
980 {"getch", (PyCFunction)PyCursesWindow_GetCh},
981 {"getstr", (PyCFunction)PyCursesWindow_GetStr},
982 {"inch", (PyCFunction)PyCursesWindow_InCh},
983 {"clearok", (PyCFunction)PyCursesWindow_ClearOk},
984 {"idlok", (PyCFunction)PyCursesWindow_IdlOk},
985 {"leaveok", (PyCFunction)PyCursesWindow_LeaveOk},
986 {"scrollok", (PyCFunction)PyCursesWindow_ScrollOk},
987 {"setscrreg", (PyCFunction)PyCursesWindow_SetScrollRegion},
988 {"keypad", (PyCFunction)PyCursesWindow_KeyPad},
989 {"nodelay", (PyCFunction)PyCursesWindow_NoDelay},
990 {"notimeout", (PyCFunction)PyCursesWindow_NoTimeout},
991 {NULL, (PyCFunction)NULL} /* sentinel */
992};
993
994static PyObject *
995PyCursesWindow_GetAttr(self, name)
996 PyCursesWindowObject *self;
997 char *name;
998{
Guido van Rossuma597dde1995-01-10 20:56:29 +0000999 return Py_FindMethod(PyCursesWindow_Methods, (PyObject *)self, name);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001000}
1001
Guido van Rossum85738471995-02-17 13:50:17 +00001002
Guido van Rossumf6971e21994-08-30 12:25:20 +00001003/* --------------- PAD routines ---------------- */
Guido van Rossum85738471995-02-17 13:50:17 +00001004
1005#ifdef NOT_YET
Guido van Rossumf6971e21994-08-30 12:25:20 +00001006static PyObject *
1007PyCursesPad_New(pad)
1008 WINDOW *pad;
1009{
1010 PyCursesPadObject *po;
Guido van Rossum85738471995-02-17 13:50:17 +00001011 po = PyObject_NEW(PyCursesPadObject, &PyCursesPad_Type);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001012 if (po == NULL)
1013 return NULL;
1014 po->pad = pad;
1015 return (PyObject *)po;
1016}
Guido van Rossum85738471995-02-17 13:50:17 +00001017#endif
1018
Guido van Rossumf6971e21994-08-30 12:25:20 +00001019
1020/* -------------------------------------------------------*/
Guido van Rossum85738471995-02-17 13:50:17 +00001021
Guido van Rossumf6971e21994-08-30 12:25:20 +00001022static PyTypeObject PyCursesScreen_Type = {
1023 PyObject_HEAD_INIT(&PyType_Type)
1024 0, /*ob_size*/
1025 "curses screen", /*tp_name*/
1026 sizeof(PyCursesScreenObject), /*tp_basicsize*/
1027 0, /*tp_itemsize*/
1028 /* methods */
1029 (destructor)0 /*PyCursesScreen_Dealloc*/, /*tp_dealloc*/
1030 0, /*tp_print*/
1031 (getattrfunc)0, /*tp_getattr*/
1032 (setattrfunc)0, /*tp_setattr*/
1033 0, /*tp_compare*/
1034 0, /*tp_repr*/
1035 0, /*tp_as_number*/
1036 0, /*tp_as_sequence*/
1037 0, /*tp_as_mapping*/
1038 0, /*tp_hash*/
1039};
1040
1041static PyTypeObject PyCursesWindow_Type = {
1042 PyObject_HEAD_INIT(&PyType_Type)
1043 0, /*ob_size*/
1044 "curses window", /*tp_name*/
1045 sizeof(PyCursesWindowObject), /*tp_basicsize*/
1046 0, /*tp_itemsize*/
1047 /* methods */
1048 (destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/
1049 0, /*tp_print*/
1050 (getattrfunc)PyCursesWindow_GetAttr, /*tp_getattr*/
1051 (setattrfunc)0, /*tp_setattr*/
1052 0, /*tp_compare*/
1053 0, /*tp_repr*/
1054 0, /*tp_as_number*/
1055 0, /*tp_as_sequence*/
1056 0, /*tp_as_mapping*/
1057 0, /*tp_hash*/
1058};
1059
1060static PyTypeObject PyCursesPad_Type = {
1061 PyObject_HEAD_INIT(&PyType_Type)
1062 0, /*ob_size*/
1063 "curses pad", /*tp_name*/
1064 sizeof(PyCursesPadObject), /*tp_basicsize*/
1065 0, /*tp_itemsize*/
1066 /* methods */
1067 (destructor)0 /*PyCursesPad_Dealloc*/, /*tp_dealloc*/
1068 0, /*tp_print*/
1069 (getattrfunc)0, /*tp_getattr*/
1070 (setattrfunc)0, /*tp_setattr*/
1071 0, /*tp_compare*/
1072 0, /*tp_repr*/
1073 0, /*tp_as_number*/
1074 0, /*tp_as_sequence*/
1075 0, /*tp_as_mapping*/
1076 0, /*tp_hash*/
1077};
1078
Guido van Rossum85738471995-02-17 13:50:17 +00001079
Guido van Rossumf6971e21994-08-30 12:25:20 +00001080/* -------------------------------------------------------*/
1081
Guido van Rossume4485b01994-09-07 14:32:49 +00001082static PyObject *ModDict;
1083
Guido van Rossumf6971e21994-08-30 12:25:20 +00001084static PyObject *
1085PyCurses_InitScr(self, args)
1086 PyObject * self;
1087 PyObject * args;
1088{
1089 static int already_inited = FALSE;
Guido van Rossum56bf2351994-08-31 22:06:24 +00001090 WINDOW *win;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001091 if (!PyArg_NoArgs(args))
Guido van Rossum85738471995-02-17 13:50:17 +00001092 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001093 if (already_inited == TRUE) {
1094 wrefresh(stdscr);
1095 return (PyObject *)PyCursesWindow_New(stdscr);
1096 }
Guido van Rossum56bf2351994-08-31 22:06:24 +00001097
1098 win = initscr();
Guido van Rossum85738471995-02-17 13:50:17 +00001099 if (win == NULL) {
1100 PyErr_SetString(PyCursesError, catchall_NULL);
1101 return NULL;
1102 }
1103
1104 already_inited = TRUE;
Guido van Rossum56bf2351994-08-31 22:06:24 +00001105
1106/* This was moved from initcurses() because core dumped on SGI */
Guido van Rossum85738471995-02-17 13:50:17 +00001107/* Also, they are probably not defined until you've called initscr() */
1108#define SetDictInt(string,ch) \
1109 PyDict_SetItemString(ModDict,string,PyInt_FromLong((long) (ch)));
Guido van Rossum56bf2351994-08-31 22:06:24 +00001110
1111 /* Here are some graphic symbols you can use */
Guido van Rossum85738471995-02-17 13:50:17 +00001112 SetDictInt("ACS_ULCORNER",(ACS_ULCORNER));
1113 SetDictInt("ACS_ULCORNER",(ACS_ULCORNER));
1114 SetDictInt("ACS_LLCORNER",(ACS_LLCORNER));
1115 SetDictInt("ACS_URCORNER",(ACS_URCORNER));
1116 SetDictInt("ACS_LRCORNER",(ACS_LRCORNER));
1117 SetDictInt("ACS_RTEE", (ACS_RTEE));
1118 SetDictInt("ACS_LTEE", (ACS_LTEE));
1119 SetDictInt("ACS_BTEE", (ACS_BTEE));
1120 SetDictInt("ACS_TTEE", (ACS_TTEE));
1121 SetDictInt("ACS_HLINE", (ACS_HLINE));
1122 SetDictInt("ACS_VLINE", (ACS_VLINE));
1123 SetDictInt("ACS_PLUS", (ACS_PLUS));
1124 SetDictInt("ACS_S1", (ACS_S1));
1125 SetDictInt("ACS_S9", (ACS_S9));
1126 SetDictInt("ACS_DIAMOND", (ACS_DIAMOND));
1127 SetDictInt("ACS_CKBOARD", (ACS_CKBOARD));
1128 SetDictInt("ACS_DEGREE", (ACS_DEGREE));
1129 SetDictInt("ACS_PLMINUS", (ACS_PLMINUS));
1130 SetDictInt("ACS_BULLET", (ACS_BULLET));
1131 SetDictInt("ACS_LARROW", (ACS_RARROW));
1132 SetDictInt("ACS_DARROW", (ACS_DARROW));
1133 SetDictInt("ACS_UARROW", (ACS_UARROW));
1134 SetDictInt("ACS_BOARD", (ACS_BOARD));
1135 SetDictInt("ACS_LANTERN", (ACS_LANTERN));
1136 SetDictInt("ACS_BLOCK", (ACS_BLOCK));
Guido van Rossum56bf2351994-08-31 22:06:24 +00001137
1138 return (PyObject *)PyCursesWindow_New(win);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001139}
1140
1141static PyObject *
1142PyCurses_EndWin(self, args)
1143 PyObject * self;
1144 PyObject * args;
1145{
1146 if (!PyArg_NoArgs(args))
Guido van Rossum85738471995-02-17 13:50:17 +00001147 return NULL;
1148 return PyCursesCheckERR(endwin(), "endwin");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001149}
1150
1151static PyObject *
1152PyCurses_IsEndWin(self, args)
1153 PyObject * self;
1154 PyObject * args;
1155{
1156 if (!PyArg_NoArgs(args))
Guido van Rossum85738471995-02-17 13:50:17 +00001157 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001158 if (isendwin() == FALSE) {
1159 Py_INCREF(Py_False);
1160 return Py_False;
1161 }
1162 Py_INCREF(Py_True);
1163 return Py_True;
1164}
1165
1166static PyObject *
1167PyCurses_DoUpdate(self,arg)
1168 PyObject * self;
1169 PyObject * arg;
1170{
1171 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +00001172 return NULL;
1173 return PyCursesCheckERR(doupdate(), "doupdate");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001174}
1175
1176static PyObject *
1177PyCurses_NewWindow(self,arg)
1178 PyObject * self;
1179 PyObject * arg;
1180{
1181 WINDOW *win;
1182 int nlines, ncols, begin_y, begin_x;
Guido van Rossum85738471995-02-17 13:50:17 +00001183
1184 nlines = ncols = 0;
1185 switch (ARG_COUNT(arg)) {
1186 case 2:
1187 if (!PyArg_Parse(arg,"(ii);begin)_y,begin_x",&begin_y,&begin_x))
1188 return NULL;
1189 break;
1190 case 4:
1191 if (!PyArg_Parse(arg, "(iiii);nlines,ncols,begin_y,begin_x",
Guido van Rossumf6971e21994-08-30 12:25:20 +00001192 &nlines,&ncols,&begin_y,&begin_x))
Guido van Rossum85738471995-02-17 13:50:17 +00001193 return NULL;
1194 break;
1195 default:
1196 PyErr_SetString(PyExc_TypeError, "newwin requires 2 or 4 arguments");
1197 return NULL;
1198 }
1199
Guido van Rossumf6971e21994-08-30 12:25:20 +00001200 win = newwin(nlines,ncols,begin_y,begin_x);
1201 if (win == NULL) {
Guido van Rossum85738471995-02-17 13:50:17 +00001202 PyErr_SetString(PyCursesError, catchall_NULL);
1203 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001204 }
Guido van Rossum85738471995-02-17 13:50:17 +00001205
Guido van Rossumf6971e21994-08-30 12:25:20 +00001206 return (PyObject *)PyCursesWindow_New(win);
1207}
1208
1209static PyObject *
1210PyCurses_Beep(self,arg)
1211 PyObject * self;
1212 PyObject * arg;
1213{
1214 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +00001215 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001216 beep();
1217 Py_INCREF(Py_None);
Guido van Rossum85738471995-02-17 13:50:17 +00001218 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001219}
1220
1221static PyObject *
1222PyCurses_Flash(self,arg)
1223 PyObject * self;
1224 PyObject * arg;
1225{
1226 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +00001227 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001228 flash();
1229 Py_INCREF(Py_None);
Guido van Rossum85738471995-02-17 13:50:17 +00001230 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001231}
1232
1233static PyObject *
1234PyCurses_UngetCh(self,arg)
1235 PyObject * self;
1236 PyObject * arg;
1237{
1238 int ch;
1239 if (!PyArg_Parse(arg,"i;integer",&ch))
Guido van Rossum85738471995-02-17 13:50:17 +00001240 return NULL;
1241 return PyCursesCheckERR(ungetch(ch), "ungetch");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001242}
1243
1244static PyObject *
1245PyCurses_FlushInp(self,arg)
1246 PyObject * self;
1247 PyObject * arg;
1248{
1249 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +00001250 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001251 flushinp();
1252 Py_INCREF(Py_None);
Guido van Rossum85738471995-02-17 13:50:17 +00001253 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001254}
1255
1256static PyObject *
1257PyCurses_CBreak(self,arg)
1258 PyObject * self;
1259 PyObject * arg;
1260{
1261 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +00001262 return NULL;
1263 return PyCursesCheckERR(cbreak(), "cbreak");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001264}
1265
1266static PyObject *
1267PyCurses_NoCBreak(self,arg)
1268 PyObject * self;
1269 PyObject * arg;
1270{
1271 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +00001272 return NULL;
1273 return PyCursesCheckERR(nocbreak(), "nocbreak");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001274}
1275
1276static PyObject *
1277PyCurses_Echo(self,arg)
1278 PyObject * self;
1279 PyObject * arg;
1280{
1281 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +00001282 return NULL;
1283 return PyCursesCheckERR(echo(), "echo");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001284}
1285
1286static PyObject *
1287PyCurses_NoEcho(self,arg)
1288 PyObject * self;
1289 PyObject * arg;
1290{
1291 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +00001292 return NULL;
1293 return PyCursesCheckERR(noecho(), "noecho");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001294}
1295
1296static PyObject *
1297PyCurses_Nl(self,arg)
1298 PyObject * self;
1299 PyObject * arg;
1300{
1301 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +00001302 return NULL;
1303 return PyCursesCheckERR(nl(), "nl");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001304}
1305
1306static PyObject *
1307PyCurses_NoNl(self,arg)
1308 PyObject * self;
1309 PyObject * arg;
1310{
1311 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +00001312 return NULL;
1313 return PyCursesCheckERR(nonl(), "nonl");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001314}
1315
1316static PyObject *
1317PyCurses_Raw(self,arg)
1318 PyObject * self;
1319 PyObject * arg;
1320{
1321 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +00001322 return NULL;
1323 return PyCursesCheckERR(raw(), "raw");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001324}
1325
1326static PyObject *
1327PyCurses_NoRaw(self,arg)
1328 PyObject * self;
1329 PyObject * arg;
1330{
1331 if (!PyArg_NoArgs(arg))
Guido van Rossum85738471995-02-17 13:50:17 +00001332 return NULL;
1333 return PyCursesCheckERR(noraw(), "noraw");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001334}
1335
1336static PyObject *
1337PyCurses_IntrFlush(self,arg)
1338 PyObject * self;
1339 PyObject * arg;
1340{
1341 int ch;
1342 if (!PyArg_Parse(arg,"i;True(1), False(0)",&ch))
Guido van Rossum85738471995-02-17 13:50:17 +00001343 return NULL;
1344 return PyCursesCheckERR(intrflush(NULL,ch), "intrflush");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001345}
1346
1347static PyObject *
1348PyCurses_Meta(self,arg)
1349 PyObject * self;
1350 PyObject * arg;
1351{
1352 int ch;
1353 if (!PyArg_Parse(arg,"i;True(1), False(0)",&ch))
Guido van Rossum85738471995-02-17 13:50:17 +00001354 return NULL;
1355 return PyCursesCheckERR(meta(stdscr, ch), "meta");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001356}
1357
1358static PyObject *
1359PyCurses_KeyName(self,arg)
1360 PyObject * self;
1361 PyObject * arg;
1362{
Guido van Rossum85738471995-02-17 13:50:17 +00001363 char *knp;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001364 int ch;
1365 if (!PyArg_Parse(arg,"i",&ch))
Guido van Rossum85738471995-02-17 13:50:17 +00001366 return NULL;
1367 knp = keyname(ch);
1368 return PyString_FromString((knp == NULL) ? "" : knp);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001369}
1370
1371#ifdef NOT_YET
1372static PyObject *
1373PyCurses_NewTerm(self, args)
1374 PyObject * self;
1375 PyObject * args;
1376{
1377}
1378
1379static PyObject *
1380PyCurses_SetTerm(self, args)
1381 PyObject * self;
1382 PyObject * args;
1383{
1384}
1385#endif
1386
1387/* List of functions defined in the module */
1388
1389static PyMethodDef PyCurses_methods[] = {
1390 {"initscr", (PyCFunction)PyCurses_InitScr},
1391 {"endwin", (PyCFunction)PyCurses_EndWin},
1392 {"isendwin", (PyCFunction)PyCurses_IsEndWin},
1393 {"doupdate", (PyCFunction)PyCurses_DoUpdate},
1394 {"newwin", (PyCFunction)PyCurses_NewWindow},
1395 {"beep", (PyCFunction)PyCurses_Beep},
1396 {"flash", (PyCFunction)PyCurses_Flash},
1397 {"ungetch", (PyCFunction)PyCurses_UngetCh},
1398 {"flushinp", (PyCFunction)PyCurses_FlushInp},
1399 {"cbreak", (PyCFunction)PyCurses_CBreak},
1400 {"nocbreak", (PyCFunction)PyCurses_NoCBreak},
1401 {"echo", (PyCFunction)PyCurses_Echo},
1402 {"noecho", (PyCFunction)PyCurses_NoEcho},
1403 {"nl", (PyCFunction)PyCurses_Nl},
1404 {"nonl", (PyCFunction)PyCurses_NoNl},
1405 {"raw", (PyCFunction)PyCurses_Raw},
1406 {"noraw", (PyCFunction)PyCurses_NoRaw},
1407 {"intrflush", (PyCFunction)PyCurses_IntrFlush},
1408 {"meta", (PyCFunction)PyCurses_Meta},
1409 {"keyname", (PyCFunction)PyCurses_KeyName},
1410#ifdef NOT_YET
1411 {"newterm", (PyCFunction)PyCurses_NewTerm},
1412 {"set_term", (PyCFunction)PyCurses_SetTerm},
1413#endif
1414 {NULL, NULL} /* sentinel */
1415};
1416
1417/* Initialization function for the module */
1418
1419void
Guido van Rossum56bf2351994-08-31 22:06:24 +00001420initcurses()
Guido van Rossumf6971e21994-08-30 12:25:20 +00001421{
Guido van Rossum8fbf82b1995-02-17 13:54:04 +00001422 PyObject *m, *d, *v;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001423
1424 /* Create the module and add the functions */
Guido van Rossum56bf2351994-08-31 22:06:24 +00001425 m = Py_InitModule("curses", PyCurses_methods);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001426
Guido van Rossumf6971e21994-08-30 12:25:20 +00001427 /* Add some symbolic constants to the module */
1428 d = PyModule_GetDict(m);
Guido van Rossume4485b01994-09-07 14:32:49 +00001429 ModDict = d; /* For PyCurses_InitScr */
Guido van Rossumfbea2f31994-08-31 22:05:27 +00001430
Guido van Rossum85738471995-02-17 13:50:17 +00001431 /* For exception curses.error */
1432 PyCursesError = PyString_FromString("curses.error");
1433 PyDict_SetItemString(d, "error", PyCursesError);
Guido van Rossumfbea2f31994-08-31 22:05:27 +00001434
Guido van Rossum85738471995-02-17 13:50:17 +00001435 /* Make the version available */
Guido van Rossum8fbf82b1995-02-17 13:54:04 +00001436 v = PyString_FromString(PyCursesVersion);
1437 PyDict_SetItemString(d, "version", v);
1438 PyDict_SetItemString(d, "__version__", v);
1439 Py_DECREF(v);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001440
Guido van Rossumf6971e21994-08-30 12:25:20 +00001441 /* Here are some attributes you can add to chars to print */
Guido van Rossum85738471995-02-17 13:50:17 +00001442 SetDictInt("A_NORMAL", A_NORMAL);
1443 SetDictInt("A_STANDOUT", A_STANDOUT);
1444 SetDictInt("A_UNDERLINE", A_UNDERLINE);
1445 SetDictInt("A_REVERSE", A_REVERSE);
1446 SetDictInt("A_BLINK", A_BLINK);
1447 SetDictInt("A_DIM", A_DIM);
1448 SetDictInt("A_BOLD", A_BOLD);
1449 SetDictInt("A_ALTCHARSET", A_ALTCHARSET);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001450
1451 /* Now set everything up for KEY_ variables */
1452 {
1453 int key;
1454 char *key_n;
1455 char *key_n2;
1456 for (key=KEY_MIN;key < KEY_MAX; key++) {
1457 key_n = (char *)keyname(key);
Guido van Rossumf5c6d471995-02-07 15:38:32 +00001458 if (key_n == NULL || strcmp(key_n,"UNKNOWN KEY")==0)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001459 continue;
1460 if (strncmp(key_n,"KEY_F(",6)==0) {
1461 char *p1, *p2;
1462 key_n2 = malloc(strlen(key_n)+1);
1463 p1 = key_n;
1464 p2 = key_n2;
1465 while (*p1) {
1466 if (*p1 != '(' && *p1 != ')') {
1467 *p2 = *p1;
1468 p2++;
1469 }
1470 p1++;
1471 }
1472 *p2 = (char)0;
1473 } else
1474 key_n2 = key_n;
Guido van Rossum85738471995-02-17 13:50:17 +00001475 PyDict_SetItemString(d,key_n2,PyInt_FromLong((long) key));
Guido van Rossumf6971e21994-08-30 12:25:20 +00001476 if (key_n2 != key_n)
1477 free(key_n2);
1478 }
Guido van Rossum85738471995-02-17 13:50:17 +00001479 SetDictInt("KEY_MIN", KEY_MIN);
1480 SetDictInt("KEY_MAX", KEY_MAX);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001481 }
1482
1483 /* Check for errors */
1484 if (PyErr_Occurred())
Guido van Rossum85738471995-02-17 13:50:17 +00001485 Py_FatalError("can't initialize module curses");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001486}