blob: a3146f8263df8218a216f77646e3a877c2ea6852 [file] [log] [blame]
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001/*
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00002 * This is a curses module for Python.
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003 *
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00004 * Based on prior work by Lance Ellinghaus and Oliver Andrich
5 * Version 1.2 of this module: Copyright 1994 by Lance Ellinghouse,
6 * Cathedral City, California Republic, United States of America.
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00007 *
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00008 * Version 1.5b1, heavily extended for ncurses by Oliver Andrich:
9 * Copyright 1996,1997 by Oliver Andrich, Koblenz, Germany.
10 *
Andrew M. Kuchling3adefcc2002-10-30 21:08:34 +000011 * Tidied for Python 1.6, and currently maintained by <amk@amk.ca>.
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +000012 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this source file to use, copy, modify, merge, or publish it
15 * subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or in any new file that contains a substantial portion of
19 * this file.
20 *
21 * THE AUTHOR MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF
22 * THE SOFTWARE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT
23 * EXPRESS OR IMPLIED WARRANTY. THE AUTHOR DISCLAIMS ALL WARRANTIES
24 * WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
25 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NON-INFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE
27 * AUTHOR BE LIABLE TO YOU OR ANY OTHER PARTY FOR ANY SPECIAL,
28 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
29 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE, STRICT LIABILITY OR
30 * ANY OTHER ACTION ARISING OUT OF OR IN CONNECTION WITH THE USE OR
31 * PERFORMANCE OF THIS SOFTWARE.
32 */
Guido van Rossumf6971e21994-08-30 12:25:20 +000033
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +000034/* CVS: $Id$ */
Guido van Rossumf6971e21994-08-30 12:25:20 +000035
Andrew M. Kuchlingfb6bbf52000-07-27 11:58:01 +000036/*
37
Victor Stinnerec234762010-05-15 22:26:41 +000038 A number of SysV or ncurses functions don't have wrappers yet; if you
39 need a given function, add it and send a patch. See
40 http://www.python.org/dev/patches/ for instructions on how to submit
41 patches to Python.
Andrew M. Kuchlinga6fbc1eab2007-04-11 13:42:25 +000042
Victor Stinnerec234762010-05-15 22:26:41 +000043 Here's a list of currently unsupported functions:
Andrew M. Kuchlingfb6bbf52000-07-27 11:58:01 +000044
Victor Stinnerec234762010-05-15 22:26:41 +000045 addchnstr addchstr color_set define_key
46 del_curterm delscreen dupwin inchnstr inchstr innstr keyok
47 mcprint mvaddchnstr mvaddchstr mvcur mvinchnstr
48 mvinchstr mvinnstr mmvwaddchnstr mvwaddchstr
49 mvwinchnstr mvwinchstr mvwinnstr newterm
50 restartterm ripoffline scr_dump
51 scr_init scr_restore scr_set scrl set_curterm set_term setterm
52 tgetent tgetflag tgetnum tgetstr tgoto timeout tputs
53 vidattr vidputs waddchnstr waddchstr
54 wcolor_set winchnstr winchstr winnstr wmouse_trafo wscrl
Andrew M. Kuchlingfb6bbf52000-07-27 11:58:01 +000055
Victor Stinnerec234762010-05-15 22:26:41 +000056 Low-priority:
57 slk_attr slk_attr_off slk_attr_on slk_attr_set slk_attroff
58 slk_attron slk_attrset slk_clear slk_color slk_init slk_label
59 slk_noutrefresh slk_refresh slk_restore slk_set slk_touch
Andrew M. Kuchlingfb6bbf52000-07-27 11:58:01 +000060
Victor Stinnerec234762010-05-15 22:26:41 +000061 Menu extension (ncurses and probably SYSV):
62 current_item free_item free_menu item_count item_description
63 item_index item_init item_name item_opts item_opts_off
64 item_opts_on item_term item_userptr item_value item_visible
65 menu_back menu_driver menu_fore menu_format menu_grey
66 menu_init menu_items menu_mark menu_opts menu_opts_off
67 menu_opts_on menu_pad menu_pattern menu_request_by_name
68 menu_request_name menu_spacing menu_sub menu_term menu_userptr
69 menu_win new_item new_menu pos_menu_cursor post_menu
70 scale_menu set_current_item set_item_init set_item_opts
71 set_item_term set_item_userptr set_item_value set_menu_back
72 set_menu_fore set_menu_format set_menu_grey set_menu_init
73 set_menu_items set_menu_mark set_menu_opts set_menu_pad
74 set_menu_pattern set_menu_spacing set_menu_sub set_menu_term
75 set_menu_userptr set_menu_win set_top_row top_row unpost_menu
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +000076
Victor Stinnerec234762010-05-15 22:26:41 +000077 Form extension (ncurses and probably SYSV):
78 current_field data_ahead data_behind dup_field
79 dynamic_fieldinfo field_arg field_back field_buffer
80 field_count field_fore field_index field_info field_init
81 field_just field_opts field_opts_off field_opts_on field_pad
82 field_status field_term field_type field_userptr form_driver
83 form_fields form_init form_opts form_opts_off form_opts_on
84 form_page form_request_by_name form_request_name form_sub
85 form_term form_userptr form_win free_field free_form
86 link_field link_fieldtype move_field new_field new_form
87 new_page pos_form_cursor post_form scale_form
88 set_current_field set_field_back set_field_buffer
89 set_field_fore set_field_init set_field_just set_field_opts
90 set_field_pad set_field_status set_field_term set_field_type
91 set_field_userptr set_fieldtype_arg set_fieldtype_choice
92 set_form_fields set_form_init set_form_opts set_form_page
93 set_form_sub set_form_term set_form_userptr set_form_win
94 set_max_field set_new_page unpost_form
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +000095
96
Victor Stinnerec234762010-05-15 22:26:41 +000097*/
Andrew M. Kuchlingfb6bbf52000-07-27 11:58:01 +000098
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +000099/* Release Number */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000100
Andrew M. Kuchling16e65a02001-10-20 16:05:52 +0000101char *PyCursesVersion = "2.2";
Guido van Rossumf6971e21994-08-30 12:25:20 +0000102
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000103/* Includes */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000104
Guido van Rossum602099a1994-09-14 13:32:22 +0000105#include "Python.h"
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000106
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +0000107#ifdef __osf__
Andrew M. Kuchlingbf680262000-07-25 12:37:41 +0000108#define STRICT_SYSV_CURSES /* Don't use ncurses extensions */
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +0000109#endif
110
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000111#ifdef __hpux
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000112#define STRICT_SYSV_CURSES
113#endif
114
Guido van Rossum858ca0f2001-04-10 19:53:37 +0000115#define CURSES_MODULE
116#include "py_curses.h"
117
Victor Stinnerec234762010-05-15 22:26:41 +0000118/* These prototypes are in <term.h>, but including this header
119 #defines many common symbols (such as "lines") which breaks the
120 curses module in other ways. So the code will just specify
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +0000121 explicit prototypes here. */
122extern int setupterm(char *,int,int *);
Fred Drake0368bc42001-07-19 20:48:32 +0000123#ifdef __sgi
124#include <term.h>
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +0000125#endif
126
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000127#if !defined(HAVE_NCURSES_H) && (defined(sgi) || defined(__sun) || defined(SCO5))
Andrew M. Kuchlingbf680262000-07-25 12:37:41 +0000128#define STRICT_SYSV_CURSES /* Don't use ncurses extensions */
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +0000129typedef chtype attr_t; /* No attr_t type is available */
Guido van Rossum1266a011996-02-25 04:50:31 +0000130#endif
Guido van Rossumf6971e21994-08-30 12:25:20 +0000131
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000132#if defined(_AIX)
133#define STRICT_SYSV_CURSES
134#endif
135
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000136/* Definition of exception curses.error */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000137
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000138static PyObject *PyCursesError;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000139
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +0000140/* Tells whether setupterm() has been called to initialise terminfo. */
141static int initialised_setupterm = FALSE;
142
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000143/* Tells whether initscr() has been called to initialise curses. */
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000144static int initialised = FALSE;
145
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000146/* Tells whether start_color() has been called to initialise color usage. */
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000147static int initialisedcolors = FALSE;
148
149/* Utility Macros */
Victor Stinnerec234762010-05-15 22:26:41 +0000150#define PyCursesSetupTermCalled \
151 if (initialised_setupterm != TRUE) { \
152 PyErr_SetString(PyCursesError, \
153 "must call (at least) setupterm() first"); \
154 return 0; }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +0000155
Victor Stinnerec234762010-05-15 22:26:41 +0000156#define PyCursesInitialised \
157 if (initialised != TRUE) { \
158 PyErr_SetString(PyCursesError, \
159 "must call initscr() first"); \
160 return 0; }
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000161
Victor Stinnerec234762010-05-15 22:26:41 +0000162#define PyCursesInitialisedColor \
163 if (initialisedcolors != TRUE) { \
164 PyErr_SetString(PyCursesError, \
165 "must call start_color() first"); \
166 return 0; }
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000167
Andrew M. Kuchling52a14c32005-06-09 17:53:27 +0000168#ifndef MIN
169#define MIN(x,y) ((x) < (y) ? (x) : (y))
170#endif
171
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000172/* Utility Functions */
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000173
Guido van Rossum85738471995-02-17 13:50:17 +0000174/*
Victor Stinnerec234762010-05-15 22:26:41 +0000175 * Check the return code from a curses function and return None
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000176 * or raise an exception as appropriate. These are exported using the
Victor Stinnerec234762010-05-15 22:26:41 +0000177 * CObject API.
Guido van Rossum85738471995-02-17 13:50:17 +0000178 */
179
180static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000181PyCursesCheckERR(int code, char *fname)
Guido van Rossum85738471995-02-17 13:50:17 +0000182{
Victor Stinnerec234762010-05-15 22:26:41 +0000183 if (code != ERR) {
184 Py_INCREF(Py_None);
185 return Py_None;
Guido van Rossum85738471995-02-17 13:50:17 +0000186 } else {
Victor Stinnerec234762010-05-15 22:26:41 +0000187 if (fname == NULL) {
188 PyErr_SetString(PyCursesError, catchall_ERR);
189 } else {
190 PyErr_Format(PyCursesError, "%s() returned ERR", fname);
191 }
192 return NULL;
Guido van Rossum85738471995-02-17 13:50:17 +0000193 }
Guido van Rossum85738471995-02-17 13:50:17 +0000194}
195
Victor Stinnerec234762010-05-15 22:26:41 +0000196static int
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000197PyCurses_ConvertToChtype(PyObject *obj, chtype *ch)
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000198{
Victor Stinnerec234762010-05-15 22:26:41 +0000199 if (PyInt_Check(obj)) {
200 *ch = (chtype) PyInt_AsLong(obj);
201 } else if(PyString_Check(obj)
202 && (PyString_Size(obj) == 1)) {
203 *ch = (chtype) *PyString_AsString(obj);
204 } else {
205 return 0;
206 }
207 return 1;
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000208}
209
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000210/* Function versions of the 3 functions for tested whether curses has been
211 initialised or not. */
Victor Stinnerec234762010-05-15 22:26:41 +0000212
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000213static int func_PyCursesSetupTermCalled(void)
214{
215 PyCursesSetupTermCalled;
216 return 1;
217}
218
219static int func_PyCursesInitialised(void)
220{
221 PyCursesInitialised;
222 return 1;
223}
224
225static int func_PyCursesInitialisedColor(void)
226{
227 PyCursesInitialisedColor;
228 return 1;
229}
230
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000231/*****************************************************************************
232 The Window Object
233******************************************************************************/
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000234
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000235/* Definition of the window type */
Guido van Rossum85738471995-02-17 13:50:17 +0000236
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000237PyTypeObject PyCursesWindow_Type;
238
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000239/* Function prototype macros for Window object
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000240
241 X - function name
242 TYPE - parameter Type
243 ERGSTR - format string for construction of the return value
244 PARSESTR - format string for argument parsing
Victor Stinnerec234762010-05-15 22:26:41 +0000245*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000246
Victor Stinnerec234762010-05-15 22:26:41 +0000247#define Window_NoArgNoReturnFunction(X) \
248 static PyObject *PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
249 { return PyCursesCheckERR(X(self->win), # X); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000250
Victor Stinnerec234762010-05-15 22:26:41 +0000251#define Window_NoArgTrueFalseFunction(X) \
252 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self) \
253 { \
254 if (X (self->win) == FALSE) { Py_INCREF(Py_False); return Py_False; } \
255 else { Py_INCREF(Py_True); return Py_True; } }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000256
Victor Stinnerec234762010-05-15 22:26:41 +0000257#define Window_NoArgNoReturnVoidFunction(X) \
258 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self) \
259 { \
260 X(self->win); Py_INCREF(Py_None); return Py_None; }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000261
Victor Stinnerec234762010-05-15 22:26:41 +0000262#define Window_NoArg2TupleReturnFunction(X, TYPE, ERGSTR) \
263 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self) \
264 { \
265 TYPE arg1, arg2; \
266 X(self->win,arg1,arg2); return Py_BuildValue(ERGSTR, arg1, arg2); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000267
Victor Stinnerec234762010-05-15 22:26:41 +0000268#define Window_OneArgNoReturnVoidFunction(X, TYPE, PARSESTR) \
269 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
270 { \
271 TYPE arg1; \
272 if (!PyArg_ParseTuple(args, PARSESTR, &arg1)) return NULL; \
273 X(self->win,arg1); Py_INCREF(Py_None); return Py_None; }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000274
Victor Stinnerec234762010-05-15 22:26:41 +0000275#define Window_OneArgNoReturnFunction(X, TYPE, PARSESTR) \
276 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
277 { \
278 TYPE arg1; \
279 if (!PyArg_ParseTuple(args,PARSESTR, &arg1)) return NULL; \
280 return PyCursesCheckERR(X(self->win, arg1), # X); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000281
Victor Stinnerec234762010-05-15 22:26:41 +0000282#define Window_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \
283 static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
284 { \
285 TYPE arg1, arg2; \
286 if (!PyArg_ParseTuple(args,PARSESTR, &arg1, &arg2)) return NULL; \
287 return PyCursesCheckERR(X(self->win, arg1, arg2), # X); }
Guido van Rossum85738471995-02-17 13:50:17 +0000288
Guido van Rossumf6971e21994-08-30 12:25:20 +0000289/* ------------- WINDOW routines --------------- */
Guido van Rossum85738471995-02-17 13:50:17 +0000290
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000291Window_NoArgNoReturnFunction(untouchwin)
292Window_NoArgNoReturnFunction(touchwin)
293Window_NoArgNoReturnFunction(redrawwin)
294Window_NoArgNoReturnFunction(winsertln)
295Window_NoArgNoReturnFunction(werase)
296Window_NoArgNoReturnFunction(wdeleteln)
297
298Window_NoArgTrueFalseFunction(is_wintouched)
299
300Window_NoArgNoReturnVoidFunction(wsyncup)
301Window_NoArgNoReturnVoidFunction(wsyncdown)
302Window_NoArgNoReturnVoidFunction(wstandend)
303Window_NoArgNoReturnVoidFunction(wstandout)
304Window_NoArgNoReturnVoidFunction(wcursyncup)
305Window_NoArgNoReturnVoidFunction(wclrtoeol)
306Window_NoArgNoReturnVoidFunction(wclrtobot)
307Window_NoArgNoReturnVoidFunction(wclear)
308
309Window_OneArgNoReturnVoidFunction(idcok, int, "i;True(1) or False(0)")
310Window_OneArgNoReturnVoidFunction(immedok, int, "i;True(1) or False(0)")
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +0000311Window_OneArgNoReturnVoidFunction(wtimeout, int, "i;delay")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000312
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000313Window_NoArg2TupleReturnFunction(getyx, int, "ii")
314Window_NoArg2TupleReturnFunction(getbegyx, int, "ii")
315Window_NoArg2TupleReturnFunction(getmaxyx, int, "ii")
316Window_NoArg2TupleReturnFunction(getparyx, int, "ii")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000317
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000318Window_OneArgNoReturnFunction(clearok, int, "i;True(1) or False(0)")
319Window_OneArgNoReturnFunction(idlok, int, "i;True(1) or False(0)")
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000320#if defined(__NetBSD__)
321Window_OneArgNoReturnVoidFunction(keypad, int, "i;True(1) or False(0)")
322#else
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000323Window_OneArgNoReturnFunction(keypad, int, "i;True(1) or False(0)")
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000324#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000325Window_OneArgNoReturnFunction(leaveok, int, "i;True(1) or False(0)")
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000326#if defined(__NetBSD__)
327Window_OneArgNoReturnVoidFunction(nodelay, int, "i;True(1) or False(0)")
328#else
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000329Window_OneArgNoReturnFunction(nodelay, int, "i;True(1) or False(0)")
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000330#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000331Window_OneArgNoReturnFunction(notimeout, int, "i;True(1) or False(0)")
332Window_OneArgNoReturnFunction(scrollok, int, "i;True(1) or False(0)")
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +0000333Window_OneArgNoReturnFunction(winsdelln, int, "i;nlines")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000334Window_OneArgNoReturnFunction(syncok, int, "i;True(1) or False(0)")
335
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000336Window_TwoArgNoReturnFunction(mvwin, int, "ii;y,x")
337Window_TwoArgNoReturnFunction(mvderwin, int, "ii;y,x")
338Window_TwoArgNoReturnFunction(wmove, int, "ii;y,x")
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +0000339#ifndef STRICT_SYSV_CURSES
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000340Window_TwoArgNoReturnFunction(wresize, int, "ii;lines,columns")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000341#endif
342
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +0000343/* Allocation and deallocation of Window Objects */
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000344
Guido van Rossumf6971e21994-08-30 12:25:20 +0000345static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000346PyCursesWindow_New(WINDOW *win)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000347{
Victor Stinnerec234762010-05-15 22:26:41 +0000348 PyCursesWindowObject *wo;
Guido van Rossum85738471995-02-17 13:50:17 +0000349
Victor Stinnerec234762010-05-15 22:26:41 +0000350 wo = PyObject_NEW(PyCursesWindowObject, &PyCursesWindow_Type);
351 if (wo == NULL) return NULL;
352 wo->win = win;
353 return (PyObject *)wo;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000354}
355
356static void
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000357PyCursesWindow_Dealloc(PyCursesWindowObject *wo)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000358{
Victor Stinnerec234762010-05-15 22:26:41 +0000359 if (wo->win != stdscr) delwin(wo->win);
360 PyObject_DEL(wo);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000361}
362
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000363/* Addch, Addstr, Addnstr */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000364
365static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000366PyCursesWindow_AddCh(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000367{
Victor Stinnerec234762010-05-15 22:26:41 +0000368 int rtn, x, y, use_xy = FALSE;
369 PyObject *temp;
370 chtype ch = 0;
371 attr_t attr = A_NORMAL;
372 long lattr;
Guido van Rossum85738471995-02-17 13:50:17 +0000373
Victor Stinnerec234762010-05-15 22:26:41 +0000374 switch (PyTuple_Size(args)) {
375 case 1:
376 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
377 return NULL;
378 break;
379 case 2:
380 if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &temp, &lattr))
381 return NULL;
382 attr = lattr;
383 break;
384 case 3:
385 if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &temp))
386 return NULL;
387 use_xy = TRUE;
388 break;
389 case 4:
390 if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr",
391 &y, &x, &temp, &lattr))
392 return NULL;
393 attr = lattr;
394 use_xy = TRUE;
395 break;
396 default:
397 PyErr_SetString(PyExc_TypeError, "addch requires 1 to 4 arguments");
398 return NULL;
399 }
400
401 if (!PyCurses_ConvertToChtype(temp, &ch)) {
402 PyErr_SetString(PyExc_TypeError, "argument 1 or 3 must be a ch or an int");
403 return NULL;
404 }
405
406 if (use_xy == TRUE)
407 rtn = mvwaddch(self->win,y,x, ch | attr);
408 else {
409 rtn = waddch(self->win, ch | attr);
410 }
411 return PyCursesCheckERR(rtn, "addch");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000412}
413
414static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000415PyCursesWindow_AddStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000416{
Victor Stinnerec234762010-05-15 22:26:41 +0000417 int rtn;
418 int x, y;
419 char *str;
420 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
421 long lattr;
422 int use_xy = FALSE, use_attr = FALSE;
Guido van Rossum85738471995-02-17 13:50:17 +0000423
Victor Stinnerec234762010-05-15 22:26:41 +0000424 switch (PyTuple_Size(args)) {
425 case 1:
426 if (!PyArg_ParseTuple(args,"s;str", &str))
427 return NULL;
428 break;
429 case 2:
430 if (!PyArg_ParseTuple(args,"sl;str,attr", &str, &lattr))
431 return NULL;
432 attr = lattr;
433 use_attr = TRUE;
434 break;
435 case 3:
436 if (!PyArg_ParseTuple(args,"iis;int,int,str", &y, &x, &str))
437 return NULL;
438 use_xy = TRUE;
439 break;
440 case 4:
441 if (!PyArg_ParseTuple(args,"iisl;int,int,str,attr", &y, &x, &str, &lattr))
442 return NULL;
443 attr = lattr;
444 use_xy = use_attr = TRUE;
445 break;
446 default:
447 PyErr_SetString(PyExc_TypeError, "addstr requires 1 to 4 arguments");
448 return NULL;
449 }
Guido van Rossum85738471995-02-17 13:50:17 +0000450
Victor Stinnerec234762010-05-15 22:26:41 +0000451 if (use_attr == TRUE) {
452 attr_old = getattrs(self->win);
453 (void)wattrset(self->win,attr);
454 }
455 if (use_xy == TRUE)
456 rtn = mvwaddstr(self->win,y,x,str);
457 else
458 rtn = waddstr(self->win,str);
459 if (use_attr == TRUE)
460 (void)wattrset(self->win,attr_old);
461 return PyCursesCheckERR(rtn, "addstr");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000462}
Guido van Rossum85738471995-02-17 13:50:17 +0000463
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000464static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000465PyCursesWindow_AddNStr(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000466{
Victor Stinnerec234762010-05-15 22:26:41 +0000467 int rtn, x, y, n;
468 char *str;
469 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
470 long lattr;
471 int use_xy = FALSE, use_attr = FALSE;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000472
Victor Stinnerec234762010-05-15 22:26:41 +0000473 switch (PyTuple_Size(args)) {
474 case 2:
475 if (!PyArg_ParseTuple(args,"si;str,n", &str, &n))
476 return NULL;
477 break;
478 case 3:
479 if (!PyArg_ParseTuple(args,"sil;str,n,attr", &str, &n, &lattr))
480 return NULL;
481 attr = lattr;
482 use_attr = TRUE;
483 break;
484 case 4:
485 if (!PyArg_ParseTuple(args,"iisi;y,x,str,n", &y, &x, &str, &n))
486 return NULL;
487 use_xy = TRUE;
488 break;
489 case 5:
490 if (!PyArg_ParseTuple(args,"iisil;y,x,str,n,attr", &y, &x, &str, &n, &lattr))
491 return NULL;
492 attr = lattr;
493 use_xy = use_attr = TRUE;
494 break;
495 default:
496 PyErr_SetString(PyExc_TypeError, "addnstr requires 2 to 5 arguments");
497 return NULL;
498 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000499
Victor Stinnerec234762010-05-15 22:26:41 +0000500 if (use_attr == TRUE) {
501 attr_old = getattrs(self->win);
502 (void)wattrset(self->win,attr);
503 }
504 if (use_xy == TRUE)
505 rtn = mvwaddnstr(self->win,y,x,str,n);
506 else
507 rtn = waddnstr(self->win,str,n);
508 if (use_attr == TRUE)
509 (void)wattrset(self->win,attr_old);
510 return PyCursesCheckERR(rtn, "addnstr");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000511}
512
513static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000514PyCursesWindow_Bkgd(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000515{
Victor Stinnerec234762010-05-15 22:26:41 +0000516 PyObject *temp;
517 chtype bkgd;
518 attr_t attr = A_NORMAL;
519 long lattr;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000520
Victor Stinnerec234762010-05-15 22:26:41 +0000521 switch (PyTuple_Size(args)) {
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000522 case 1:
Victor Stinnerec234762010-05-15 22:26:41 +0000523 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
524 return NULL;
525 break;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000526 case 2:
Victor Stinnerec234762010-05-15 22:26:41 +0000527 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr))
528 return NULL;
529 attr = lattr;
530 break;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000531 default:
Victor Stinnerec234762010-05-15 22:26:41 +0000532 PyErr_SetString(PyExc_TypeError, "bkgd requires 1 or 2 arguments");
533 return NULL;
534 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000535
Victor Stinnerec234762010-05-15 22:26:41 +0000536 if (!PyCurses_ConvertToChtype(temp, &bkgd)) {
537 PyErr_SetString(PyExc_TypeError, "argument 1 or 3 must be a ch or an int");
538 return NULL;
539 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000540
Victor Stinnerec234762010-05-15 22:26:41 +0000541 return PyCursesCheckERR(wbkgd(self->win, bkgd | attr), "bkgd");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000542}
543
544static PyObject *
Andrew M. Kuchling62182c82008-01-08 14:56:02 +0000545PyCursesWindow_AttrOff(PyCursesWindowObject *self, PyObject *args)
546{
Victor Stinnerec234762010-05-15 22:26:41 +0000547 long lattr;
548 if (!PyArg_ParseTuple(args,"l;attr", &lattr))
549 return NULL;
550 return PyCursesCheckERR(wattroff(self->win, (attr_t)lattr), "attroff");
Andrew M. Kuchling62182c82008-01-08 14:56:02 +0000551}
552
553static PyObject *
554PyCursesWindow_AttrOn(PyCursesWindowObject *self, PyObject *args)
555{
Victor Stinnerec234762010-05-15 22:26:41 +0000556 long lattr;
557 if (!PyArg_ParseTuple(args,"l;attr", &lattr))
558 return NULL;
559 return PyCursesCheckERR(wattron(self->win, (attr_t)lattr), "attron");
Andrew M. Kuchling62182c82008-01-08 14:56:02 +0000560}
561
562static PyObject *
563PyCursesWindow_AttrSet(PyCursesWindowObject *self, PyObject *args)
564{
Victor Stinnerec234762010-05-15 22:26:41 +0000565 long lattr;
566 if (!PyArg_ParseTuple(args,"l;attr", &lattr))
567 return NULL;
568 return PyCursesCheckERR(wattrset(self->win, (attr_t)lattr), "attrset");
Andrew M. Kuchling62182c82008-01-08 14:56:02 +0000569}
570
571static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000572PyCursesWindow_BkgdSet(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000573{
Victor Stinnerec234762010-05-15 22:26:41 +0000574 PyObject *temp;
575 chtype bkgd;
576 attr_t attr = A_NORMAL;
577 long lattr;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000578
Victor Stinnerec234762010-05-15 22:26:41 +0000579 switch (PyTuple_Size(args)) {
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000580 case 1:
Victor Stinnerec234762010-05-15 22:26:41 +0000581 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
582 return NULL;
583 break;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000584 case 2:
Victor Stinnerec234762010-05-15 22:26:41 +0000585 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr))
586 return NULL;
587 attr = lattr;
588 break;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000589 default:
Victor Stinnerec234762010-05-15 22:26:41 +0000590 PyErr_SetString(PyExc_TypeError, "bkgdset requires 1 or 2 arguments");
591 return NULL;
592 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000593
Victor Stinnerec234762010-05-15 22:26:41 +0000594 if (!PyCurses_ConvertToChtype(temp, &bkgd)) {
595 PyErr_SetString(PyExc_TypeError, "argument 1 must be a ch or an int");
596 return NULL;
597 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000598
Victor Stinnerec234762010-05-15 22:26:41 +0000599 wbkgdset(self->win, bkgd | attr);
600 return PyCursesCheckERR(0, "bkgdset");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000601}
602
603static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000604PyCursesWindow_Border(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000605{
Victor Stinnerec234762010-05-15 22:26:41 +0000606 PyObject *temp[8];
607 chtype ch[8];
608 int i;
Andrew M. Kuchling16e65a02001-10-20 16:05:52 +0000609
Victor Stinnerec234762010-05-15 22:26:41 +0000610 /* Clear the array of parameters */
611 for(i=0; i<8; i++) {
612 temp[i] = NULL;
613 ch[i] = 0;
614 }
Andrew M. Kuchling16e65a02001-10-20 16:05:52 +0000615
Victor Stinnerec234762010-05-15 22:26:41 +0000616 if (!PyArg_ParseTuple(args,"|OOOOOOOO;ls,rs,ts,bs,tl,tr,bl,br",
617 &temp[0], &temp[1], &temp[2], &temp[3],
618 &temp[4], &temp[5], &temp[6], &temp[7]))
619 return NULL;
620
621 for(i=0; i<8; i++) {
622 if (temp[i] != NULL && !PyCurses_ConvertToChtype(temp[i], &ch[i])) {
623 PyErr_Format(PyExc_TypeError,
624 "argument %i must be a ch or an int", i+1);
625 return NULL;
626 }
627 }
628
629 wborder(self->win,
630 ch[0], ch[1], ch[2], ch[3],
631 ch[4], ch[5], ch[6], ch[7]);
632 Py_INCREF(Py_None);
633 return Py_None;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000634}
635
636static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000637PyCursesWindow_Box(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000638{
Victor Stinnerec234762010-05-15 22:26:41 +0000639 chtype ch1=0,ch2=0;
640 switch(PyTuple_Size(args)){
641 case 0: break;
642 default:
643 if (!PyArg_ParseTuple(args,"ll;vertint,horint", &ch1, &ch2))
644 return NULL;
645 }
646 box(self->win,ch1,ch2);
647 Py_INCREF(Py_None);
648 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000649}
650
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000651#if defined(HAVE_NCURSES_H) || defined(MVWDELCH_IS_EXPRESSION)
652#define py_mvwdelch mvwdelch
653#else
654int py_mvwdelch(WINDOW *w, int y, int x)
655{
Victor Stinnerec234762010-05-15 22:26:41 +0000656 mvwdelch(w,y,x);
657 /* On HP/UX, mvwdelch already returns. On other systems,
658 we may well run into this return statement. */
659 return 0;
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000660}
661#endif
662
Andrew M. Kuchling400a49b2007-04-11 13:39:00 +0000663/* chgat, added by Fabian Kreutz <fabian.kreutz at gmx.net> */
664
665static PyObject *
666PyCursesWindow_ChgAt(PyCursesWindowObject *self, PyObject *args)
667{
Victor Stinnerec234762010-05-15 22:26:41 +0000668 int rtn;
669 int x, y;
670 int num = -1;
671 short color;
672 attr_t attr = A_NORMAL;
673 long lattr;
674 int use_xy = FALSE;
Andrew M. Kuchling400a49b2007-04-11 13:39:00 +0000675
Victor Stinnerec234762010-05-15 22:26:41 +0000676 switch (PyTuple_Size(args)) {
677 case 1:
678 if (!PyArg_ParseTuple(args,"l;attr", &lattr))
679 return NULL;
680 attr = lattr;
681 break;
682 case 2:
683 if (!PyArg_ParseTuple(args,"il;n,attr", &num, &lattr))
684 return NULL;
685 attr = lattr;
686 break;
687 case 3:
688 if (!PyArg_ParseTuple(args,"iil;int,int,attr", &y, &x, &lattr))
689 return NULL;
690 attr = lattr;
691 use_xy = TRUE;
692 break;
693 case 4:
694 if (!PyArg_ParseTuple(args,"iiil;int,int,n,attr", &y, &x, &num, &lattr))
695 return NULL;
696 attr = lattr;
697 use_xy = TRUE;
698 break;
699 default:
700 PyErr_SetString(PyExc_TypeError, "chgat requires 1 to 4 arguments");
701 return NULL;
702 }
Andrew M. Kuchling400a49b2007-04-11 13:39:00 +0000703
Victor Stinnerec234762010-05-15 22:26:41 +0000704 color = (short)((attr >> 8) & 0xff);
705 attr = attr - (color << 8);
Andrew M. Kuchling400a49b2007-04-11 13:39:00 +0000706
Victor Stinnerec234762010-05-15 22:26:41 +0000707 if (use_xy == TRUE) {
708 rtn = mvwchgat(self->win,y,x,num,attr,color,NULL);
709 touchline(self->win,y,1);
710 } else {
711 getyx(self->win,y,x);
712 rtn = wchgat(self->win,num,attr,color,NULL);
713 touchline(self->win,y,1);
714 }
715 return PyCursesCheckERR(rtn, "chgat");
Andrew M. Kuchling400a49b2007-04-11 13:39:00 +0000716}
717
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000718
Guido van Rossumf6971e21994-08-30 12:25:20 +0000719static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000720PyCursesWindow_DelCh(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000721{
Victor Stinnerec234762010-05-15 22:26:41 +0000722 int rtn;
723 int x, y;
Guido van Rossum85738471995-02-17 13:50:17 +0000724
Victor Stinnerec234762010-05-15 22:26:41 +0000725 switch (PyTuple_Size(args)) {
726 case 0:
727 rtn = wdelch(self->win);
728 break;
729 case 2:
730 if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x))
731 return NULL;
732 rtn = py_mvwdelch(self->win,y,x);
733 break;
734 default:
735 PyErr_SetString(PyExc_TypeError, "delch requires 0 or 2 arguments");
736 return NULL;
737 }
738 return PyCursesCheckERR(rtn, "[mv]wdelch");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000739}
740
741static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000742PyCursesWindow_DerWin(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000743{
Victor Stinnerec234762010-05-15 22:26:41 +0000744 WINDOW *win;
745 int nlines, ncols, begin_y, begin_x;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000746
Victor Stinnerec234762010-05-15 22:26:41 +0000747 nlines = 0;
748 ncols = 0;
749 switch (PyTuple_Size(args)) {
750 case 2:
751 if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
752 return NULL;
753 break;
754 case 4:
755 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
756 &nlines,&ncols,&begin_y,&begin_x))
757 return NULL;
758 break;
759 default:
760 PyErr_SetString(PyExc_TypeError, "derwin requires 2 or 4 arguments");
761 return NULL;
762 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000763
Victor Stinnerec234762010-05-15 22:26:41 +0000764 win = derwin(self->win,nlines,ncols,begin_y,begin_x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000765
Victor Stinnerec234762010-05-15 22:26:41 +0000766 if (win == NULL) {
767 PyErr_SetString(PyCursesError, catchall_NULL);
768 return NULL;
769 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000770
Victor Stinnerec234762010-05-15 22:26:41 +0000771 return (PyObject *)PyCursesWindow_New(win);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000772}
773
774static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000775PyCursesWindow_EchoChar(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000776{
Victor Stinnerec234762010-05-15 22:26:41 +0000777 PyObject *temp;
778 chtype ch;
779 attr_t attr = A_NORMAL;
780 long lattr;
Guido van Rossum85738471995-02-17 13:50:17 +0000781
Victor Stinnerec234762010-05-15 22:26:41 +0000782 switch (PyTuple_Size(args)) {
783 case 1:
784 if (!PyArg_ParseTuple(args,"O;ch or int", &temp))
785 return NULL;
786 break;
787 case 2:
788 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr))
789 return NULL;
790 attr = lattr;
791 break;
792 default:
793 PyErr_SetString(PyExc_TypeError, "echochar requires 1 or 2 arguments");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000794
795
Victor Stinnerec234762010-05-15 22:26:41 +0000796 return NULL;
797 }
Guido van Rossum85738471995-02-17 13:50:17 +0000798
Victor Stinnerec234762010-05-15 22:26:41 +0000799 if (!PyCurses_ConvertToChtype(temp, &ch)) {
800 PyErr_SetString(PyExc_TypeError, "argument 1 must be a ch or an int");
801 return NULL;
802 }
803
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000804#ifdef WINDOW_HAS_FLAGS
Victor Stinnerec234762010-05-15 22:26:41 +0000805 if (self->win->_flags & _ISPAD)
806 return PyCursesCheckERR(pechochar(self->win, ch | attr),
807 "echochar");
808 else
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000809#endif
Victor Stinnerec234762010-05-15 22:26:41 +0000810 return PyCursesCheckERR(wechochar(self->win, ch | attr),
811 "echochar");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000812}
813
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +0000814#ifdef NCURSES_MOUSE_VERSION
815static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000816PyCursesWindow_Enclose(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +0000817{
Victor Stinnerec234762010-05-15 22:26:41 +0000818 int x, y;
819 if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x))
820 return NULL;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +0000821
Victor Stinnerec234762010-05-15 22:26:41 +0000822 return PyInt_FromLong( wenclose(self->win,y,x) );
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +0000823}
824#endif
825
Guido van Rossumf6971e21994-08-30 12:25:20 +0000826static PyObject *
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000827PyCursesWindow_GetBkgd(PyCursesWindowObject *self)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000828{
Victor Stinnerec234762010-05-15 22:26:41 +0000829 return PyInt_FromLong((long) getbkgd(self->win));
Guido van Rossumf6971e21994-08-30 12:25:20 +0000830}
831
832static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000833PyCursesWindow_GetCh(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000834{
Victor Stinnerec234762010-05-15 22:26:41 +0000835 int x, y;
836 int rtn;
Guido van Rossum85738471995-02-17 13:50:17 +0000837
Victor Stinnerec234762010-05-15 22:26:41 +0000838 switch (PyTuple_Size(args)) {
839 case 0:
840 Py_BEGIN_ALLOW_THREADS
841 rtn = wgetch(self->win);
842 Py_END_ALLOW_THREADS
843 break;
844 case 2:
845 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
846 return NULL;
847 Py_BEGIN_ALLOW_THREADS
848 rtn = mvwgetch(self->win,y,x);
849 Py_END_ALLOW_THREADS
850 break;
851 default:
852 PyErr_SetString(PyExc_TypeError, "getch requires 0 or 2 arguments");
853 return NULL;
854 }
855 return PyInt_FromLong((long)rtn);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000856}
Guido van Rossum85738471995-02-17 13:50:17 +0000857
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000858static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000859PyCursesWindow_GetKey(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000860{
Victor Stinnerec234762010-05-15 22:26:41 +0000861 int x, y;
862 int rtn;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000863
Victor Stinnerec234762010-05-15 22:26:41 +0000864 switch (PyTuple_Size(args)) {
865 case 0:
866 Py_BEGIN_ALLOW_THREADS
867 rtn = wgetch(self->win);
868 Py_END_ALLOW_THREADS
869 break;
870 case 2:
871 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
872 return NULL;
873 Py_BEGIN_ALLOW_THREADS
874 rtn = mvwgetch(self->win,y,x);
875 Py_END_ALLOW_THREADS
876 break;
877 default:
878 PyErr_SetString(PyExc_TypeError, "getkey requires 0 or 2 arguments");
879 return NULL;
880 }
881 if (rtn == ERR) {
882 /* getch() returns ERR in nodelay mode */
883 PyErr_SetString(PyCursesError, "no input");
884 return NULL;
885 } else if (rtn<=255) {
886 return Py_BuildValue("c", rtn);
887 } else {
888 const char *knp;
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000889#if defined(__NetBSD__)
Victor Stinnerec234762010-05-15 22:26:41 +0000890 knp = unctrl(rtn);
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000891#else
Victor Stinnerec234762010-05-15 22:26:41 +0000892 knp = keyname(rtn);
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000893#endif
Victor Stinnerec234762010-05-15 22:26:41 +0000894 return PyString_FromString((knp == NULL) ? "" : knp);
895 }
Guido van Rossumf6971e21994-08-30 12:25:20 +0000896}
897
898static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000899PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000900{
Victor Stinnerec234762010-05-15 22:26:41 +0000901 int x, y, n;
902 char rtn[1024]; /* This should be big enough.. I hope */
903 int rtn2;
Guido van Rossum85738471995-02-17 13:50:17 +0000904
Victor Stinnerec234762010-05-15 22:26:41 +0000905 switch (PyTuple_Size(args)) {
906 case 0:
907 Py_BEGIN_ALLOW_THREADS
908 rtn2 = wgetnstr(self->win,rtn, 1023);
909 Py_END_ALLOW_THREADS
910 break;
911 case 1:
912 if (!PyArg_ParseTuple(args,"i;n", &n))
913 return NULL;
914 Py_BEGIN_ALLOW_THREADS
915 rtn2 = wgetnstr(self->win,rtn,MIN(n, 1023));
916 Py_END_ALLOW_THREADS
917 break;
918 case 2:
919 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
920 return NULL;
921 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling55b0a0e2006-07-26 17:16:52 +0000922#ifdef STRICT_SYSV_CURSES
Victor Stinnerec234762010-05-15 22:26:41 +0000923 rtn2 = wmove(self->win,y,x)==ERR ? ERR : wgetnstr(self->win, rtn, 1023);
Andrew M. Kuchling55b0a0e2006-07-26 17:16:52 +0000924#else
Victor Stinnerec234762010-05-15 22:26:41 +0000925 rtn2 = mvwgetnstr(self->win,y,x,rtn, 1023);
Andrew M. Kuchling55b0a0e2006-07-26 17:16:52 +0000926#endif
Victor Stinnerec234762010-05-15 22:26:41 +0000927 Py_END_ALLOW_THREADS
928 break;
929 case 3:
930 if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
931 return NULL;
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +0000932#ifdef STRICT_SYSV_CURSES
Victor Stinnerec234762010-05-15 22:26:41 +0000933 Py_BEGIN_ALLOW_THREADS
934 rtn2 = wmove(self->win,y,x)==ERR ? ERR :
935 wgetnstr(self->win, rtn, MIN(n, 1023));
936 Py_END_ALLOW_THREADS
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000937#else
Victor Stinnerec234762010-05-15 22:26:41 +0000938 Py_BEGIN_ALLOW_THREADS
939 rtn2 = mvwgetnstr(self->win, y, x, rtn, MIN(n, 1023));
940 Py_END_ALLOW_THREADS
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000941#endif
Victor Stinnerec234762010-05-15 22:26:41 +0000942 break;
943 default:
944 PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 3 arguments");
945 return NULL;
946 }
947 if (rtn2 == ERR)
948 rtn[0] = 0;
949 return PyString_FromString(rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000950}
951
952static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000953PyCursesWindow_Hline(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000954{
Victor Stinnerec234762010-05-15 22:26:41 +0000955 PyObject *temp;
956 chtype ch;
957 int n, x, y, code = OK;
958 attr_t attr = A_NORMAL;
959 long lattr;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000960
Victor Stinnerec234762010-05-15 22:26:41 +0000961 switch (PyTuple_Size(args)) {
962 case 2:
963 if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
964 return NULL;
965 break;
966 case 3:
967 if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &lattr))
968 return NULL;
969 attr = lattr;
970 break;
971 case 4:
972 if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
973 return NULL;
974 code = wmove(self->win, y, x);
975 break;
976 case 5:
977 if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
978 &y, &x, &temp, &n, &lattr))
979 return NULL;
980 attr = lattr;
981 code = wmove(self->win, y, x);
982 break;
983 default:
984 PyErr_SetString(PyExc_TypeError, "hline requires 2 to 5 arguments");
985 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000986 }
Victor Stinnerec234762010-05-15 22:26:41 +0000987
988 if (code != ERR) {
989 if (!PyCurses_ConvertToChtype(temp, &ch)) {
990 PyErr_SetString(PyExc_TypeError,
991 "argument 1 or 3 must be a ch or an int");
992 return NULL;
993 }
994 return PyCursesCheckERR(whline(self->win, ch | attr, n), "hline");
995 } else
996 return PyCursesCheckERR(code, "wmove");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000997}
998
999static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001000PyCursesWindow_InsCh(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001001{
Victor Stinnerec234762010-05-15 22:26:41 +00001002 int rtn, x, y, use_xy = FALSE;
1003 PyObject *temp;
1004 chtype ch = 0;
1005 attr_t attr = A_NORMAL;
1006 long lattr;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001007
Victor Stinnerec234762010-05-15 22:26:41 +00001008 switch (PyTuple_Size(args)) {
1009 case 1:
1010 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
1011 return NULL;
1012 break;
1013 case 2:
1014 if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &temp, &lattr))
1015 return NULL;
1016 attr = lattr;
1017 break;
1018 case 3:
1019 if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &temp))
1020 return NULL;
1021 use_xy = TRUE;
1022 break;
1023 case 4:
1024 if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr", &y, &x, &temp, &lattr))
1025 return NULL;
1026 attr = lattr;
1027 use_xy = TRUE;
1028 break;
1029 default:
1030 PyErr_SetString(PyExc_TypeError, "insch requires 1 or 4 arguments");
1031 return NULL;
1032 }
1033
1034 if (!PyCurses_ConvertToChtype(temp, &ch)) {
1035 PyErr_SetString(PyExc_TypeError,
1036 "argument 1 or 3 must be a ch or an int");
1037 return NULL;
1038 }
1039
1040 if (use_xy == TRUE)
1041 rtn = mvwinsch(self->win,y,x, ch | attr);
1042 else {
1043 rtn = winsch(self->win, ch | attr);
1044 }
1045 return PyCursesCheckERR(rtn, "insch");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001046}
1047
1048static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001049PyCursesWindow_InCh(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001050{
Victor Stinnerec234762010-05-15 22:26:41 +00001051 int x, y, rtn;
Guido van Rossum85738471995-02-17 13:50:17 +00001052
Victor Stinnerec234762010-05-15 22:26:41 +00001053 switch (PyTuple_Size(args)) {
1054 case 0:
1055 rtn = winch(self->win);
1056 break;
1057 case 2:
1058 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1059 return NULL;
1060 rtn = mvwinch(self->win,y,x);
1061 break;
1062 default:
1063 PyErr_SetString(PyExc_TypeError, "inch requires 0 or 2 arguments");
1064 return NULL;
1065 }
1066 return PyInt_FromLong((long) rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001067}
1068
1069static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001070PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001071{
Victor Stinnerec234762010-05-15 22:26:41 +00001072 int x, y, n;
1073 char rtn[1024]; /* This should be big enough.. I hope */
1074 int rtn2;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001075
Victor Stinnerec234762010-05-15 22:26:41 +00001076 switch (PyTuple_Size(args)) {
1077 case 0:
1078 rtn2 = winnstr(self->win,rtn, 1023);
1079 break;
1080 case 1:
1081 if (!PyArg_ParseTuple(args,"i;n", &n))
1082 return NULL;
1083 rtn2 = winnstr(self->win,rtn,MIN(n,1023));
1084 break;
1085 case 2:
1086 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1087 return NULL;
1088 rtn2 = mvwinnstr(self->win,y,x,rtn,1023);
1089 break;
1090 case 3:
1091 if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
1092 return NULL;
1093 rtn2 = mvwinnstr(self->win, y, x, rtn, MIN(n,1023));
1094 break;
1095 default:
1096 PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments");
1097 return NULL;
1098 }
1099 if (rtn2 == ERR)
1100 rtn[0] = 0;
1101 return PyString_FromString(rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001102}
1103
1104static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001105PyCursesWindow_InsStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001106{
Victor Stinnerec234762010-05-15 22:26:41 +00001107 int rtn;
1108 int x, y;
1109 char *str;
1110 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
1111 long lattr;
1112 int use_xy = FALSE, use_attr = FALSE;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001113
Victor Stinnerec234762010-05-15 22:26:41 +00001114 switch (PyTuple_Size(args)) {
1115 case 1:
1116 if (!PyArg_ParseTuple(args,"s;str", &str))
1117 return NULL;
1118 break;
1119 case 2:
1120 if (!PyArg_ParseTuple(args,"sl;str,attr", &str, &lattr))
1121 return NULL;
1122 attr = lattr;
1123 use_attr = TRUE;
1124 break;
1125 case 3:
1126 if (!PyArg_ParseTuple(args,"iis;y,x,str", &y, &x, &str))
1127 return NULL;
1128 use_xy = TRUE;
1129 break;
1130 case 4:
1131 if (!PyArg_ParseTuple(args,"iisl;y,x,str,attr", &y, &x, &str, &lattr))
1132 return NULL;
1133 attr = lattr;
1134 use_xy = use_attr = TRUE;
1135 break;
1136 default:
1137 PyErr_SetString(PyExc_TypeError, "insstr requires 1 to 4 arguments");
1138 return NULL;
1139 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001140
Victor Stinnerec234762010-05-15 22:26:41 +00001141 if (use_attr == TRUE) {
1142 attr_old = getattrs(self->win);
1143 (void)wattrset(self->win,attr);
1144 }
1145 if (use_xy == TRUE)
1146 rtn = mvwinsstr(self->win,y,x,str);
1147 else
1148 rtn = winsstr(self->win,str);
1149 if (use_attr == TRUE)
1150 (void)wattrset(self->win,attr_old);
1151 return PyCursesCheckERR(rtn, "insstr");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001152}
1153
1154static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001155PyCursesWindow_InsNStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001156{
Victor Stinnerec234762010-05-15 22:26:41 +00001157 int rtn, x, y, n;
1158 char *str;
1159 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
1160 long lattr;
1161 int use_xy = FALSE, use_attr = FALSE;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001162
Victor Stinnerec234762010-05-15 22:26:41 +00001163 switch (PyTuple_Size(args)) {
1164 case 2:
1165 if (!PyArg_ParseTuple(args,"si;str,n", &str, &n))
1166 return NULL;
1167 break;
1168 case 3:
1169 if (!PyArg_ParseTuple(args,"sil;str,n,attr", &str, &n, &lattr))
1170 return NULL;
1171 attr = lattr;
1172 use_attr = TRUE;
1173 break;
1174 case 4:
1175 if (!PyArg_ParseTuple(args,"iisi;y,x,str,n", &y, &x, &str, &n))
1176 return NULL;
1177 use_xy = TRUE;
1178 break;
1179 case 5:
1180 if (!PyArg_ParseTuple(args,"iisil;y,x,str,n,attr", &y, &x, &str, &n, &lattr))
1181 return NULL;
1182 attr = lattr;
1183 use_xy = use_attr = TRUE;
1184 break;
1185 default:
1186 PyErr_SetString(PyExc_TypeError, "insnstr requires 2 to 5 arguments");
1187 return NULL;
1188 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001189
Victor Stinnerec234762010-05-15 22:26:41 +00001190 if (use_attr == TRUE) {
1191 attr_old = getattrs(self->win);
1192 (void)wattrset(self->win,attr);
1193 }
1194 if (use_xy == TRUE)
1195 rtn = mvwinsnstr(self->win,y,x,str,n);
1196 else
1197 rtn = winsnstr(self->win,str,n);
1198 if (use_attr == TRUE)
1199 (void)wattrset(self->win,attr_old);
1200 return PyCursesCheckERR(rtn, "insnstr");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001201}
1202
1203static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001204PyCursesWindow_Is_LineTouched(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001205{
Victor Stinnerec234762010-05-15 22:26:41 +00001206 int line, erg;
1207 if (!PyArg_ParseTuple(args,"i;line", &line))
1208 return NULL;
1209 erg = is_linetouched(self->win, line);
1210 if (erg == ERR) {
1211 PyErr_SetString(PyExc_TypeError,
1212 "is_linetouched: line number outside of boundaries");
1213 return NULL;
1214 } else
1215 if (erg == FALSE) {
1216 Py_INCREF(Py_False);
1217 return Py_False;
1218 } else {
1219 Py_INCREF(Py_True);
1220 return Py_True;
1221 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001222}
1223
1224static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001225PyCursesWindow_NoOutRefresh(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001226{
Victor Stinnerec234762010-05-15 22:26:41 +00001227 int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
1228 int rtn;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001229
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001230#ifndef WINDOW_HAS_FLAGS
Victor Stinnerec234762010-05-15 22:26:41 +00001231 if (0) {
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001232#else
Victor Stinnerec234762010-05-15 22:26:41 +00001233 if (self->win->_flags & _ISPAD) {
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001234#endif
Victor Stinnerec234762010-05-15 22:26:41 +00001235 switch(PyTuple_Size(args)) {
1236 case 6:
1237 if (!PyArg_ParseTuple(args,
1238 "iiiiii;" \
1239 "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
1240 &pminrow, &pmincol, &sminrow,
1241 &smincol, &smaxrow, &smaxcol))
1242 return NULL;
1243 Py_BEGIN_ALLOW_THREADS
1244 rtn = pnoutrefresh(self->win,
1245 pminrow, pmincol, sminrow,
1246 smincol, smaxrow, smaxcol);
1247 Py_END_ALLOW_THREADS
1248 return PyCursesCheckERR(rtn, "pnoutrefresh");
1249 default:
1250 PyErr_SetString(PyCursesError,
1251 "noutrefresh() called for a pad "
1252 "requires 6 arguments");
1253 return NULL;
1254 }
1255 } else {
1256 if (!PyArg_ParseTuple(args, ":noutrefresh"))
1257 return NULL;
Andrew M. Kuchlingf4195722000-06-23 01:36:21 +00001258
Victor Stinnerec234762010-05-15 22:26:41 +00001259 Py_BEGIN_ALLOW_THREADS
1260 rtn = wnoutrefresh(self->win);
1261 Py_END_ALLOW_THREADS
1262 return PyCursesCheckERR(rtn, "wnoutrefresh");
1263 }
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001264 }
1265
Victor Stinnerec234762010-05-15 22:26:41 +00001266 static PyObject *
1267 PyCursesWindow_Overlay(PyCursesWindowObject *self, PyObject *args)
1268 {
1269 PyCursesWindowObject *temp;
1270 int use_copywin = FALSE;
1271 int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
1272 int rtn;
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001273
Victor Stinnerec234762010-05-15 22:26:41 +00001274 switch (PyTuple_Size(args)) {
1275 case 1:
1276 if (!PyArg_ParseTuple(args, "O!;window object",
1277 &PyCursesWindow_Type, &temp))
1278 return NULL;
1279 break;
1280 case 7:
1281 if (!PyArg_ParseTuple(args, "O!iiiiii;window object, int, int, int, int, int, int",
1282 &PyCursesWindow_Type, &temp, &sminrow, &smincol,
1283 &dminrow, &dmincol, &dmaxrow, &dmaxcol))
1284 return NULL;
1285 use_copywin = TRUE;
1286 break;
1287 default:
1288 PyErr_SetString(PyExc_TypeError,
1289 "overlay requires one or seven arguments");
1290 return NULL;
1291 }
1292
1293 if (use_copywin == TRUE) {
1294 rtn = copywin(self->win, temp->win, sminrow, smincol,
1295 dminrow, dmincol, dmaxrow, dmaxcol, TRUE);
1296 return PyCursesCheckERR(rtn, "copywin");
1297 }
1298 else {
1299 rtn = overlay(self->win, temp->win);
1300 return PyCursesCheckERR(rtn, "overlay");
1301 }
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001302 }
1303
Victor Stinnerec234762010-05-15 22:26:41 +00001304 static PyObject *
1305 PyCursesWindow_Overwrite(PyCursesWindowObject *self, PyObject *args)
1306 {
1307 PyCursesWindowObject *temp;
1308 int use_copywin = FALSE;
1309 int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
1310 int rtn;
1311
1312 switch (PyTuple_Size(args)) {
1313 case 1:
1314 if (!PyArg_ParseTuple(args, "O!;window object",
1315 &PyCursesWindow_Type, &temp))
1316 return NULL;
1317 break;
1318 case 7:
1319 if (!PyArg_ParseTuple(args, "O!iiiiii;window object, int, int, int, int, int, int",
1320 &PyCursesWindow_Type, &temp, &sminrow, &smincol,
1321 &dminrow, &dmincol, &dmaxrow, &dmaxcol))
1322 return NULL;
1323 use_copywin = TRUE;
1324 break;
1325 default:
1326 PyErr_SetString(PyExc_TypeError,
1327 "overwrite requires one or seven arguments");
1328 return NULL;
1329 }
1330
1331 if (use_copywin == TRUE) {
1332 rtn = copywin(self->win, temp->win, sminrow, smincol,
1333 dminrow, dmincol, dmaxrow, dmaxcol, FALSE);
1334 return PyCursesCheckERR(rtn, "copywin");
1335 }
1336 else {
1337 rtn = overwrite(self->win, temp->win);
1338 return PyCursesCheckERR(rtn, "overwrite");
1339 }
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001340 }
Victor Stinnerec234762010-05-15 22:26:41 +00001341
1342 static PyObject *
1343 PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *args)
1344 {
1345 PyObject *temp;
1346
1347 if (!PyArg_ParseTuple(args, "O;fileobj", &temp))
1348 return NULL;
1349 if (!PyFile_Check(temp)) {
1350 PyErr_SetString(PyExc_TypeError, "argument must be a file object");
1351 return NULL;
1352 }
1353 return PyCursesCheckERR(putwin(self->win, PyFile_AsFile(temp)),
1354 "putwin");
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001355 }
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001356
Victor Stinnerec234762010-05-15 22:26:41 +00001357 static PyObject *
1358 PyCursesWindow_RedrawLine(PyCursesWindowObject *self, PyObject *args)
1359 {
1360 int beg, num;
1361 if (!PyArg_ParseTuple(args, "ii;beg,num", &beg, &num))
1362 return NULL;
1363 return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln");
1364 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001365
Victor Stinnerec234762010-05-15 22:26:41 +00001366 static PyObject *
1367 PyCursesWindow_Refresh(PyCursesWindowObject *self, PyObject *args)
1368 {
1369 int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
1370 int rtn;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001371
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001372#ifndef WINDOW_HAS_FLAGS
Victor Stinnerec234762010-05-15 22:26:41 +00001373 if (0) {
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001374#else
Victor Stinnerec234762010-05-15 22:26:41 +00001375 if (self->win->_flags & _ISPAD) {
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001376#endif
Victor Stinnerec234762010-05-15 22:26:41 +00001377 switch(PyTuple_Size(args)) {
1378 case 6:
1379 if (!PyArg_ParseTuple(args,
1380 "iiiiii;" \
1381 "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
1382 &pminrow, &pmincol, &sminrow,
1383 &smincol, &smaxrow, &smaxcol))
1384 return NULL;
Andrew M. Kuchlingf4195722000-06-23 01:36:21 +00001385
Victor Stinnerec234762010-05-15 22:26:41 +00001386 Py_BEGIN_ALLOW_THREADS
1387 rtn = prefresh(self->win,
1388 pminrow, pmincol, sminrow,
1389 smincol, smaxrow, smaxcol);
1390 Py_END_ALLOW_THREADS
1391 return PyCursesCheckERR(rtn, "prefresh");
1392 default:
1393 PyErr_SetString(PyCursesError,
1394 "refresh() for a pad requires 6 arguments");
1395 return NULL;
1396 }
1397 } else {
1398 if (!PyArg_ParseTuple(args, ":refresh"))
1399 return NULL;
1400 Py_BEGIN_ALLOW_THREADS
1401 rtn = wrefresh(self->win);
1402 Py_END_ALLOW_THREADS
1403 return PyCursesCheckERR(rtn, "prefresh");
1404 }
1405 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001406
Victor Stinnerec234762010-05-15 22:26:41 +00001407 static PyObject *
1408 PyCursesWindow_SetScrollRegion(PyCursesWindowObject *self, PyObject *args)
1409 {
1410 int x, y;
1411 if (!PyArg_ParseTuple(args,"ii;top, bottom",&y,&x))
1412 return NULL;
1413 return PyCursesCheckERR(wsetscrreg(self->win,y,x), "wsetscrreg");
1414 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001415
Victor Stinnerec234762010-05-15 22:26:41 +00001416 static PyObject *
1417 PyCursesWindow_SubWin(PyCursesWindowObject *self, PyObject *args)
1418 {
1419 WINDOW *win;
1420 int nlines, ncols, begin_y, begin_x;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001421
Victor Stinnerec234762010-05-15 22:26:41 +00001422 nlines = 0;
1423 ncols = 0;
1424 switch (PyTuple_Size(args)) {
1425 case 2:
1426 if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
1427 return NULL;
1428 break;
1429 case 4:
1430 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
1431 &nlines,&ncols,&begin_y,&begin_x))
1432 return NULL;
1433 break;
1434 default:
1435 PyErr_SetString(PyExc_TypeError, "subwin requires 2 or 4 arguments");
1436 return NULL;
1437 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001438
Victor Stinnerec234762010-05-15 22:26:41 +00001439 /* printf("Subwin: %i %i %i %i \n", nlines, ncols, begin_y, begin_x); */
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001440#ifdef WINDOW_HAS_FLAGS
Victor Stinnerec234762010-05-15 22:26:41 +00001441 if (self->win->_flags & _ISPAD)
1442 win = subpad(self->win, nlines, ncols, begin_y, begin_x);
1443 else
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001444#endif
Victor Stinnerec234762010-05-15 22:26:41 +00001445 win = subwin(self->win, nlines, ncols, begin_y, begin_x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001446
Victor Stinnerec234762010-05-15 22:26:41 +00001447 if (win == NULL) {
1448 PyErr_SetString(PyCursesError, catchall_NULL);
1449 return NULL;
1450 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001451
Victor Stinnerec234762010-05-15 22:26:41 +00001452 return (PyObject *)PyCursesWindow_New(win);
1453 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001454
Victor Stinnerec234762010-05-15 22:26:41 +00001455 static PyObject *
1456 PyCursesWindow_Scroll(PyCursesWindowObject *self, PyObject *args)
1457 {
1458 int nlines;
1459 switch(PyTuple_Size(args)) {
1460 case 0:
1461 return PyCursesCheckERR(scroll(self->win), "scroll");
1462 case 1:
1463 if (!PyArg_ParseTuple(args, "i;nlines", &nlines))
1464 return NULL;
1465 return PyCursesCheckERR(wscrl(self->win, nlines), "scroll");
1466 default:
1467 PyErr_SetString(PyExc_TypeError, "scroll requires 0 or 1 arguments");
1468 return NULL;
1469 }
1470 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001471
Victor Stinnerec234762010-05-15 22:26:41 +00001472 static PyObject *
1473 PyCursesWindow_TouchLine(PyCursesWindowObject *self, PyObject *args)
1474 {
1475 int st, cnt, val;
1476 switch (PyTuple_Size(args)) {
1477 case 2:
1478 if (!PyArg_ParseTuple(args,"ii;start,count",&st,&cnt))
1479 return NULL;
1480 return PyCursesCheckERR(touchline(self->win,st,cnt), "touchline");
1481 case 3:
1482 if (!PyArg_ParseTuple(args, "iii;start,count,val", &st, &cnt, &val))
1483 return NULL;
1484 return PyCursesCheckERR(wtouchln(self->win, st, cnt, val), "touchline");
1485 default:
1486 PyErr_SetString(PyExc_TypeError, "touchline requires 2 or 3 arguments");
1487 return NULL;
1488 }
1489 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001490
Victor Stinnerec234762010-05-15 22:26:41 +00001491 static PyObject *
1492 PyCursesWindow_Vline(PyCursesWindowObject *self, PyObject *args)
1493 {
1494 PyObject *temp;
1495 chtype ch;
1496 int n, x, y, code = OK;
1497 attr_t attr = A_NORMAL;
1498 long lattr;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001499
Victor Stinnerec234762010-05-15 22:26:41 +00001500 switch (PyTuple_Size(args)) {
1501 case 2:
1502 if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
1503 return NULL;
1504 break;
1505 case 3:
1506 if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &lattr))
1507 return NULL;
1508 attr = lattr;
1509 break;
1510 case 4:
1511 if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
1512 return NULL;
1513 code = wmove(self->win, y, x);
1514 break;
1515 case 5:
1516 if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
1517 &y, &x, &temp, &n, &lattr))
1518 return NULL;
1519 attr = lattr;
1520 code = wmove(self->win, y, x);
1521 break;
1522 default:
1523 PyErr_SetString(PyExc_TypeError, "vline requires 2 to 5 arguments");
1524 return NULL;
1525 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001526
Victor Stinnerec234762010-05-15 22:26:41 +00001527 if (code != ERR) {
1528 if (!PyCurses_ConvertToChtype(temp, &ch)) {
1529 PyErr_SetString(PyExc_TypeError,
1530 "argument 1 or 3 must be a ch or an int");
1531 return NULL;
1532 }
1533 return PyCursesCheckERR(wvline(self->win, ch | attr, n), "vline");
1534 } else
1535 return PyCursesCheckERR(code, "wmove");
1536 }
1537
1538 static PyMethodDef PyCursesWindow_Methods[] = {
1539 {"addch", (PyCFunction)PyCursesWindow_AddCh, METH_VARARGS},
1540 {"addnstr", (PyCFunction)PyCursesWindow_AddNStr, METH_VARARGS},
1541 {"addstr", (PyCFunction)PyCursesWindow_AddStr, METH_VARARGS},
1542 {"attroff", (PyCFunction)PyCursesWindow_AttrOff, METH_VARARGS},
1543 {"attron", (PyCFunction)PyCursesWindow_AttrOn, METH_VARARGS},
1544 {"attrset", (PyCFunction)PyCursesWindow_AttrSet, METH_VARARGS},
1545 {"bkgd", (PyCFunction)PyCursesWindow_Bkgd, METH_VARARGS},
1546 {"chgat", (PyCFunction)PyCursesWindow_ChgAt, METH_VARARGS},
1547 {"bkgdset", (PyCFunction)PyCursesWindow_BkgdSet, METH_VARARGS},
1548 {"border", (PyCFunction)PyCursesWindow_Border, METH_VARARGS},
1549 {"box", (PyCFunction)PyCursesWindow_Box, METH_VARARGS},
1550 {"clear", (PyCFunction)PyCursesWindow_wclear, METH_NOARGS},
1551 {"clearok", (PyCFunction)PyCursesWindow_clearok, METH_VARARGS},
1552 {"clrtobot", (PyCFunction)PyCursesWindow_wclrtobot, METH_NOARGS},
1553 {"clrtoeol", (PyCFunction)PyCursesWindow_wclrtoeol, METH_NOARGS},
1554 {"cursyncup", (PyCFunction)PyCursesWindow_wcursyncup, METH_NOARGS},
1555 {"delch", (PyCFunction)PyCursesWindow_DelCh, METH_VARARGS},
1556 {"deleteln", (PyCFunction)PyCursesWindow_wdeleteln, METH_NOARGS},
1557 {"derwin", (PyCFunction)PyCursesWindow_DerWin, METH_VARARGS},
1558 {"echochar", (PyCFunction)PyCursesWindow_EchoChar, METH_VARARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001559#ifdef NCURSES_MOUSE_VERSION
Victor Stinnerec234762010-05-15 22:26:41 +00001560 {"enclose", (PyCFunction)PyCursesWindow_Enclose, METH_VARARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001561#endif
Victor Stinnerec234762010-05-15 22:26:41 +00001562 {"erase", (PyCFunction)PyCursesWindow_werase, METH_NOARGS},
1563 {"getbegyx", (PyCFunction)PyCursesWindow_getbegyx, METH_NOARGS},
1564 {"getbkgd", (PyCFunction)PyCursesWindow_GetBkgd, METH_NOARGS},
1565 {"getch", (PyCFunction)PyCursesWindow_GetCh, METH_VARARGS},
1566 {"getkey", (PyCFunction)PyCursesWindow_GetKey, METH_VARARGS},
1567 {"getmaxyx", (PyCFunction)PyCursesWindow_getmaxyx, METH_NOARGS},
1568 {"getparyx", (PyCFunction)PyCursesWindow_getparyx, METH_NOARGS},
1569 {"getstr", (PyCFunction)PyCursesWindow_GetStr, METH_VARARGS},
1570 {"getyx", (PyCFunction)PyCursesWindow_getyx, METH_NOARGS},
1571 {"hline", (PyCFunction)PyCursesWindow_Hline, METH_VARARGS},
1572 {"idcok", (PyCFunction)PyCursesWindow_idcok, METH_VARARGS},
1573 {"idlok", (PyCFunction)PyCursesWindow_idlok, METH_VARARGS},
1574 {"immedok", (PyCFunction)PyCursesWindow_immedok, METH_VARARGS},
1575 {"inch", (PyCFunction)PyCursesWindow_InCh, METH_VARARGS},
1576 {"insch", (PyCFunction)PyCursesWindow_InsCh, METH_VARARGS},
1577 {"insdelln", (PyCFunction)PyCursesWindow_winsdelln, METH_VARARGS},
1578 {"insertln", (PyCFunction)PyCursesWindow_winsertln, METH_NOARGS},
1579 {"insnstr", (PyCFunction)PyCursesWindow_InsNStr, METH_VARARGS},
1580 {"insstr", (PyCFunction)PyCursesWindow_InsStr, METH_VARARGS},
1581 {"instr", (PyCFunction)PyCursesWindow_InStr, METH_VARARGS},
1582 {"is_linetouched", (PyCFunction)PyCursesWindow_Is_LineTouched, METH_VARARGS},
1583 {"is_wintouched", (PyCFunction)PyCursesWindow_is_wintouched, METH_NOARGS},
1584 {"keypad", (PyCFunction)PyCursesWindow_keypad, METH_VARARGS},
1585 {"leaveok", (PyCFunction)PyCursesWindow_leaveok, METH_VARARGS},
1586 {"move", (PyCFunction)PyCursesWindow_wmove, METH_VARARGS},
1587 {"mvderwin", (PyCFunction)PyCursesWindow_mvderwin, METH_VARARGS},
1588 {"mvwin", (PyCFunction)PyCursesWindow_mvwin, METH_VARARGS},
1589 {"nodelay", (PyCFunction)PyCursesWindow_nodelay, METH_VARARGS},
1590 {"notimeout", (PyCFunction)PyCursesWindow_notimeout, METH_VARARGS},
1591 {"noutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
1592 /* Backward compatibility alias -- remove in Python 2.3 */
1593 {"nooutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
1594 {"overlay", (PyCFunction)PyCursesWindow_Overlay, METH_VARARGS},
1595 {"overwrite", (PyCFunction)PyCursesWindow_Overwrite,
1596 METH_VARARGS},
1597 {"putwin", (PyCFunction)PyCursesWindow_PutWin, METH_VARARGS},
1598 {"redrawln", (PyCFunction)PyCursesWindow_RedrawLine, METH_VARARGS},
1599 {"redrawwin", (PyCFunction)PyCursesWindow_redrawwin, METH_NOARGS},
1600 {"refresh", (PyCFunction)PyCursesWindow_Refresh, METH_VARARGS},
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +00001601#ifndef STRICT_SYSV_CURSES
Victor Stinnerec234762010-05-15 22:26:41 +00001602 {"resize", (PyCFunction)PyCursesWindow_wresize, METH_VARARGS},
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001603#endif
Victor Stinnerec234762010-05-15 22:26:41 +00001604 {"scroll", (PyCFunction)PyCursesWindow_Scroll, METH_VARARGS},
1605 {"scrollok", (PyCFunction)PyCursesWindow_scrollok, METH_VARARGS},
1606 {"setscrreg", (PyCFunction)PyCursesWindow_SetScrollRegion, METH_VARARGS},
1607 {"standend", (PyCFunction)PyCursesWindow_wstandend, METH_NOARGS},
1608 {"standout", (PyCFunction)PyCursesWindow_wstandout, METH_NOARGS},
1609 {"subpad", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
1610 {"subwin", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
1611 {"syncdown", (PyCFunction)PyCursesWindow_wsyncdown, METH_NOARGS},
1612 {"syncok", (PyCFunction)PyCursesWindow_syncok, METH_VARARGS},
1613 {"syncup", (PyCFunction)PyCursesWindow_wsyncup, METH_NOARGS},
1614 {"timeout", (PyCFunction)PyCursesWindow_wtimeout, METH_VARARGS},
1615 {"touchline", (PyCFunction)PyCursesWindow_TouchLine, METH_VARARGS},
1616 {"touchwin", (PyCFunction)PyCursesWindow_touchwin, METH_NOARGS},
1617 {"untouchwin", (PyCFunction)PyCursesWindow_untouchwin, METH_NOARGS},
1618 {"vline", (PyCFunction)PyCursesWindow_Vline, METH_VARARGS},
1619 {NULL, NULL} /* sentinel */
1620 };
Guido van Rossumf6971e21994-08-30 12:25:20 +00001621
Victor Stinnerec234762010-05-15 22:26:41 +00001622 static PyObject *
1623 PyCursesWindow_GetAttr(PyCursesWindowObject *self, char *name)
1624 {
1625 return Py_FindMethod(PyCursesWindow_Methods, (PyObject *)self, name);
1626 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001627
Guido van Rossumf6971e21994-08-30 12:25:20 +00001628/* -------------------------------------------------------*/
Guido van Rossum85738471995-02-17 13:50:17 +00001629
Victor Stinnerec234762010-05-15 22:26:41 +00001630 PyTypeObject PyCursesWindow_Type = {
1631 PyVarObject_HEAD_INIT(NULL, 0)
1632 "_curses.curses window", /*tp_name*/
1633 sizeof(PyCursesWindowObject), /*tp_basicsize*/
1634 0, /*tp_itemsize*/
1635 /* methods */
1636 (destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/
1637 0, /*tp_print*/
1638 (getattrfunc)PyCursesWindow_GetAttr, /*tp_getattr*/
1639 (setattrfunc)0, /*tp_setattr*/
1640 0, /*tp_compare*/
1641 0, /*tp_repr*/
1642 0, /*tp_as_number*/
1643 0, /*tp_as_sequence*/
1644 0, /*tp_as_mapping*/
1645 0, /*tp_hash*/
1646 };
Guido van Rossumf6971e21994-08-30 12:25:20 +00001647
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001648/*********************************************************************
1649 Global Functions
1650**********************************************************************/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001651
Victor Stinnerec234762010-05-15 22:26:41 +00001652 NoArgNoReturnFunction(beep)
1653 NoArgNoReturnFunction(def_prog_mode)
1654 NoArgNoReturnFunction(def_shell_mode)
1655 NoArgNoReturnFunction(doupdate)
1656 NoArgNoReturnFunction(endwin)
1657 NoArgNoReturnFunction(flash)
1658 NoArgNoReturnFunction(nocbreak)
1659 NoArgNoReturnFunction(noecho)
1660 NoArgNoReturnFunction(nonl)
1661 NoArgNoReturnFunction(noraw)
1662 NoArgNoReturnFunction(reset_prog_mode)
1663 NoArgNoReturnFunction(reset_shell_mode)
1664 NoArgNoReturnFunction(resetty)
1665 NoArgNoReturnFunction(savetty)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001666
Victor Stinnerec234762010-05-15 22:26:41 +00001667 NoArgOrFlagNoReturnFunction(cbreak)
1668 NoArgOrFlagNoReturnFunction(echo)
1669 NoArgOrFlagNoReturnFunction(nl)
1670 NoArgOrFlagNoReturnFunction(raw)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001671
Victor Stinnerec234762010-05-15 22:26:41 +00001672 NoArgReturnIntFunction(baudrate)
1673 NoArgReturnIntFunction(termattrs)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001674
Victor Stinnerec234762010-05-15 22:26:41 +00001675 NoArgReturnStringFunction(termname)
1676 NoArgReturnStringFunction(longname)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001677
Victor Stinnerec234762010-05-15 22:26:41 +00001678 NoArgTrueFalseFunction(can_change_color)
1679 NoArgTrueFalseFunction(has_colors)
1680 NoArgTrueFalseFunction(has_ic)
1681 NoArgTrueFalseFunction(has_il)
1682 NoArgTrueFalseFunction(isendwin)
1683 NoArgNoReturnVoidFunction(flushinp)
1684 NoArgNoReturnVoidFunction(noqiflush)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001685
Victor Stinnerec234762010-05-15 22:26:41 +00001686 static PyObject *
1687 PyCurses_filter(PyObject *self)
1688 {
1689 /* not checking for PyCursesInitialised here since filter() must
1690 be called before initscr() */
1691 filter();
1692 Py_INCREF(Py_None);
1693 return Py_None;
1694 }
Georg Brandl29604a12008-01-26 14:03:47 +00001695
Victor Stinnerec234762010-05-15 22:26:41 +00001696 static PyObject *
1697 PyCurses_Color_Content(PyObject *self, PyObject *args)
1698 {
1699 short color,r,g,b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001700
Victor Stinnerec234762010-05-15 22:26:41 +00001701 PyCursesInitialised
1702 PyCursesInitialisedColor
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001703
Victor Stinnerec234762010-05-15 22:26:41 +00001704 if (!PyArg_ParseTuple(args, "h:color_content", &color)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001705
Victor Stinnerec234762010-05-15 22:26:41 +00001706 if (color_content(color, &r, &g, &b) != ERR)
1707 return Py_BuildValue("(iii)", r, g, b);
1708 else {
1709 PyErr_SetString(PyCursesError,
1710 "Argument 1 was out of range. Check value of COLORS.");
1711 return NULL;
1712 }
1713 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001714
Victor Stinnerec234762010-05-15 22:26:41 +00001715 static PyObject *
1716 PyCurses_color_pair(PyObject *self, PyObject *args)
1717 {
1718 int n;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001719
Victor Stinnerec234762010-05-15 22:26:41 +00001720 PyCursesInitialised
1721 PyCursesInitialisedColor
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001722
Victor Stinnerec234762010-05-15 22:26:41 +00001723 if (!PyArg_ParseTuple(args, "i:color_pair", &n)) return NULL;
1724 return PyInt_FromLong((long) (n << 8));
1725 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001726
Victor Stinnerec234762010-05-15 22:26:41 +00001727 static PyObject *
1728 PyCurses_Curs_Set(PyObject *self, PyObject *args)
1729 {
1730 int vis,erg;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001731
Victor Stinnerec234762010-05-15 22:26:41 +00001732 PyCursesInitialised
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001733
Victor Stinnerec234762010-05-15 22:26:41 +00001734 if (!PyArg_ParseTuple(args, "i:curs_set", &vis)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001735
Victor Stinnerec234762010-05-15 22:26:41 +00001736 erg = curs_set(vis);
1737 if (erg == ERR) return PyCursesCheckERR(erg, "curs_set");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001738
Victor Stinnerec234762010-05-15 22:26:41 +00001739 return PyInt_FromLong((long) erg);
1740 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001741
Victor Stinnerec234762010-05-15 22:26:41 +00001742 static PyObject *
1743 PyCurses_Delay_Output(PyObject *self, PyObject *args)
1744 {
1745 int ms;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001746
Victor Stinnerec234762010-05-15 22:26:41 +00001747 PyCursesInitialised
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001748
Victor Stinnerec234762010-05-15 22:26:41 +00001749 if (!PyArg_ParseTuple(args, "i:delay_output", &ms)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001750
Victor Stinnerec234762010-05-15 22:26:41 +00001751 return PyCursesCheckERR(delay_output(ms), "delay_output");
1752 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001753
Victor Stinnerec234762010-05-15 22:26:41 +00001754 static PyObject *
1755 PyCurses_EraseChar(PyObject *self)
1756 {
1757 char ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001758
Victor Stinnerec234762010-05-15 22:26:41 +00001759 PyCursesInitialised
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001760
Victor Stinnerec234762010-05-15 22:26:41 +00001761 ch = erasechar();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001762
Victor Stinnerec234762010-05-15 22:26:41 +00001763 return PyString_FromStringAndSize(&ch, 1);
1764 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001765
Victor Stinnerec234762010-05-15 22:26:41 +00001766 static PyObject *
1767 PyCurses_getsyx(PyObject *self)
1768 {
1769 int x,y;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001770
Victor Stinnerec234762010-05-15 22:26:41 +00001771 PyCursesInitialised
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001772
Victor Stinnerec234762010-05-15 22:26:41 +00001773 getsyx(y, x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001774
Victor Stinnerec234762010-05-15 22:26:41 +00001775 return Py_BuildValue("(ii)", y, x);
1776 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001777
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001778#ifdef NCURSES_MOUSE_VERSION
Victor Stinnerec234762010-05-15 22:26:41 +00001779 static PyObject *
1780 PyCurses_GetMouse(PyObject *self)
1781 {
1782 int rtn;
1783 MEVENT event;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001784
Victor Stinnerec234762010-05-15 22:26:41 +00001785 PyCursesInitialised
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001786
Victor Stinnerec234762010-05-15 22:26:41 +00001787 rtn = getmouse( &event );
1788 if (rtn == ERR) {
1789 PyErr_SetString(PyCursesError, "getmouse() returned ERR");
1790 return NULL;
1791 }
1792 return Py_BuildValue("(hiiil)",
1793 (short)event.id,
1794 event.x, event.y, event.z,
1795 (long) event.bstate);
1796 }
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001797
Victor Stinnerec234762010-05-15 22:26:41 +00001798 static PyObject *
1799 PyCurses_UngetMouse(PyObject *self, PyObject *args)
1800 {
1801 MEVENT event;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001802
Victor Stinnerec234762010-05-15 22:26:41 +00001803 PyCursesInitialised
1804 if (!PyArg_ParseTuple(args, "hiiil",
1805 &event.id,
1806 &event.x, &event.y, &event.z,
1807 (int *) &event.bstate))
1808 return NULL;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001809
Victor Stinnerec234762010-05-15 22:26:41 +00001810 return PyCursesCheckERR(ungetmouse(&event), "ungetmouse");
1811 }
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001812#endif
1813
Victor Stinnerec234762010-05-15 22:26:41 +00001814 static PyObject *
1815 PyCurses_GetWin(PyCursesWindowObject *self, PyObject *temp)
1816 {
1817 WINDOW *win;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001818
Victor Stinnerec234762010-05-15 22:26:41 +00001819 PyCursesInitialised
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001820
Victor Stinnerec234762010-05-15 22:26:41 +00001821 if (!PyFile_Check(temp)) {
1822 PyErr_SetString(PyExc_TypeError, "argument must be a file object");
1823 return NULL;
1824 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001825
Victor Stinnerec234762010-05-15 22:26:41 +00001826 win = getwin(PyFile_AsFile(temp));
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001827
Victor Stinnerec234762010-05-15 22:26:41 +00001828 if (win == NULL) {
1829 PyErr_SetString(PyCursesError, catchall_NULL);
1830 return NULL;
1831 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001832
Victor Stinnerec234762010-05-15 22:26:41 +00001833 return PyCursesWindow_New(win);
1834 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001835
Victor Stinnerec234762010-05-15 22:26:41 +00001836 static PyObject *
1837 PyCurses_HalfDelay(PyObject *self, PyObject *args)
1838 {
1839 unsigned char tenths;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001840
Victor Stinnerec234762010-05-15 22:26:41 +00001841 PyCursesInitialised
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001842
Victor Stinnerec234762010-05-15 22:26:41 +00001843 if (!PyArg_ParseTuple(args, "b:halfdelay", &tenths)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001844
Victor Stinnerec234762010-05-15 22:26:41 +00001845 return PyCursesCheckERR(halfdelay(tenths), "halfdelay");
1846 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001847
Andrew M. Kuchlingbf680262000-07-25 12:37:41 +00001848#ifndef STRICT_SYSV_CURSES
Victor Stinnerec234762010-05-15 22:26:41 +00001849 /* No has_key! */
1850 static PyObject * PyCurses_has_key(PyObject *self, PyObject *args)
1851 {
1852 int ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001853
Victor Stinnerec234762010-05-15 22:26:41 +00001854 PyCursesInitialised
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001855
Victor Stinnerec234762010-05-15 22:26:41 +00001856 if (!PyArg_ParseTuple(args,"i",&ch)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001857
Victor Stinnerec234762010-05-15 22:26:41 +00001858 if (has_key(ch) == FALSE) {
1859 Py_INCREF(Py_False);
1860 return Py_False;
1861 }
1862 Py_INCREF(Py_True);
1863 return Py_True;
1864 }
Andrew M. Kuchlingbf680262000-07-25 12:37:41 +00001865#endif /* STRICT_SYSV_CURSES */
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001866
Victor Stinnerec234762010-05-15 22:26:41 +00001867 static PyObject *
1868 PyCurses_Init_Color(PyObject *self, PyObject *args)
1869 {
1870 short color, r, g, b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001871
Victor Stinnerec234762010-05-15 22:26:41 +00001872 PyCursesInitialised
1873 PyCursesInitialisedColor
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001874
Victor Stinnerec234762010-05-15 22:26:41 +00001875 switch(PyTuple_Size(args)) {
1876 case 4:
1877 if (!PyArg_ParseTuple(args, "hhhh;color,r,g,b", &color, &r, &g, &b)) return NULL;
1878 break;
1879 default:
1880 PyErr_SetString(PyExc_TypeError, "init_color requires 4 arguments");
1881 return NULL;
1882 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001883
Victor Stinnerec234762010-05-15 22:26:41 +00001884 return PyCursesCheckERR(init_color(color, r, g, b), "init_color");
1885 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001886
Victor Stinnerec234762010-05-15 22:26:41 +00001887 static PyObject *
1888 PyCurses_Init_Pair(PyObject *self, PyObject *args)
1889 {
1890 short pair, f, b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001891
Victor Stinnerec234762010-05-15 22:26:41 +00001892 PyCursesInitialised
1893 PyCursesInitialisedColor
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001894
Victor Stinnerec234762010-05-15 22:26:41 +00001895 if (PyTuple_Size(args) != 3) {
1896 PyErr_SetString(PyExc_TypeError, "init_pair requires 3 arguments");
1897 return NULL;
1898 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001899
Victor Stinnerec234762010-05-15 22:26:41 +00001900 if (!PyArg_ParseTuple(args, "hhh;pair, f, b", &pair, &f, &b)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001901
Victor Stinnerec234762010-05-15 22:26:41 +00001902 return PyCursesCheckERR(init_pair(pair, f, b), "init_pair");
1903 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001904
Victor Stinnerec234762010-05-15 22:26:41 +00001905 static PyObject *ModDict;
Andrew M. Kuchling32552682000-12-22 21:52:27 +00001906
Victor Stinnerec234762010-05-15 22:26:41 +00001907 static PyObject *
1908 PyCurses_InitScr(PyObject *self)
1909 {
1910 WINDOW *win;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001911
Victor Stinnerec234762010-05-15 22:26:41 +00001912 if (initialised == TRUE) {
1913 wrefresh(stdscr);
1914 return (PyObject *)PyCursesWindow_New(stdscr);
1915 }
Guido van Rossum56bf2351994-08-31 22:06:24 +00001916
Victor Stinnerec234762010-05-15 22:26:41 +00001917 win = initscr();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001918
Victor Stinnerec234762010-05-15 22:26:41 +00001919 if (win == NULL) {
1920 PyErr_SetString(PyCursesError, catchall_NULL);
1921 return NULL;
1922 }
Guido van Rossum85738471995-02-17 13:50:17 +00001923
Victor Stinnerec234762010-05-15 22:26:41 +00001924 initialised = initialised_setupterm = TRUE;
Guido van Rossum56bf2351994-08-31 22:06:24 +00001925
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00001926/* This was moved from initcurses() because it core dumped on SGI,
1927 where they're not defined until you've called initscr() */
Victor Stinnerec234762010-05-15 22:26:41 +00001928#define SetDictInt(string,ch) \
1929 do { \
1930 PyObject *o = PyInt_FromLong((long) (ch)); \
1931 if (o && PyDict_SetItemString(ModDict, string, o) == 0) { \
1932 Py_DECREF(o); \
1933 } \
1934 } while (0)
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00001935
Victor Stinnerec234762010-05-15 22:26:41 +00001936 /* Here are some graphic symbols you can use */
1937 SetDictInt("ACS_ULCORNER", (ACS_ULCORNER));
1938 SetDictInt("ACS_LLCORNER", (ACS_LLCORNER));
1939 SetDictInt("ACS_URCORNER", (ACS_URCORNER));
1940 SetDictInt("ACS_LRCORNER", (ACS_LRCORNER));
1941 SetDictInt("ACS_LTEE", (ACS_LTEE));
1942 SetDictInt("ACS_RTEE", (ACS_RTEE));
1943 SetDictInt("ACS_BTEE", (ACS_BTEE));
1944 SetDictInt("ACS_TTEE", (ACS_TTEE));
1945 SetDictInt("ACS_HLINE", (ACS_HLINE));
1946 SetDictInt("ACS_VLINE", (ACS_VLINE));
1947 SetDictInt("ACS_PLUS", (ACS_PLUS));
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001948#if !defined(__hpux) || defined(HAVE_NCURSES_H)
Victor Stinnerec234762010-05-15 22:26:41 +00001949 /* On HP/UX 11, these are of type cchar_t, which is not an
1950 integral type. If this is a problem on more platforms, a
1951 configure test should be added to determine whether ACS_S1
1952 is of integral type. */
1953 SetDictInt("ACS_S1", (ACS_S1));
1954 SetDictInt("ACS_S9", (ACS_S9));
1955 SetDictInt("ACS_DIAMOND", (ACS_DIAMOND));
1956 SetDictInt("ACS_CKBOARD", (ACS_CKBOARD));
1957 SetDictInt("ACS_DEGREE", (ACS_DEGREE));
1958 SetDictInt("ACS_PLMINUS", (ACS_PLMINUS));
1959 SetDictInt("ACS_BULLET", (ACS_BULLET));
1960 SetDictInt("ACS_LARROW", (ACS_LARROW));
1961 SetDictInt("ACS_RARROW", (ACS_RARROW));
1962 SetDictInt("ACS_DARROW", (ACS_DARROW));
1963 SetDictInt("ACS_UARROW", (ACS_UARROW));
1964 SetDictInt("ACS_BOARD", (ACS_BOARD));
1965 SetDictInt("ACS_LANTERN", (ACS_LANTERN));
1966 SetDictInt("ACS_BLOCK", (ACS_BLOCK));
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001967#endif
Victor Stinnerec234762010-05-15 22:26:41 +00001968 SetDictInt("ACS_BSSB", (ACS_ULCORNER));
1969 SetDictInt("ACS_SSBB", (ACS_LLCORNER));
1970 SetDictInt("ACS_BBSS", (ACS_URCORNER));
1971 SetDictInt("ACS_SBBS", (ACS_LRCORNER));
1972 SetDictInt("ACS_SBSS", (ACS_RTEE));
1973 SetDictInt("ACS_SSSB", (ACS_LTEE));
1974 SetDictInt("ACS_SSBS", (ACS_BTEE));
1975 SetDictInt("ACS_BSSS", (ACS_TTEE));
1976 SetDictInt("ACS_BSBS", (ACS_HLINE));
1977 SetDictInt("ACS_SBSB", (ACS_VLINE));
1978 SetDictInt("ACS_SSSS", (ACS_PLUS));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00001979
Victor Stinnerec234762010-05-15 22:26:41 +00001980 /* The following are never available with strict SYSV curses */
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00001981#ifdef ACS_S3
Victor Stinnerec234762010-05-15 22:26:41 +00001982 SetDictInt("ACS_S3", (ACS_S3));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00001983#endif
1984#ifdef ACS_S7
Victor Stinnerec234762010-05-15 22:26:41 +00001985 SetDictInt("ACS_S7", (ACS_S7));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00001986#endif
1987#ifdef ACS_LEQUAL
Victor Stinnerec234762010-05-15 22:26:41 +00001988 SetDictInt("ACS_LEQUAL", (ACS_LEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00001989#endif
1990#ifdef ACS_GEQUAL
Victor Stinnerec234762010-05-15 22:26:41 +00001991 SetDictInt("ACS_GEQUAL", (ACS_GEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00001992#endif
1993#ifdef ACS_PI
Victor Stinnerec234762010-05-15 22:26:41 +00001994 SetDictInt("ACS_PI", (ACS_PI));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00001995#endif
1996#ifdef ACS_NEQUAL
Victor Stinnerec234762010-05-15 22:26:41 +00001997 SetDictInt("ACS_NEQUAL", (ACS_NEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00001998#endif
1999#ifdef ACS_STERLING
Victor Stinnerec234762010-05-15 22:26:41 +00002000 SetDictInt("ACS_STERLING", (ACS_STERLING));
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00002001#endif
2002
Victor Stinnerec234762010-05-15 22:26:41 +00002003 SetDictInt("LINES", LINES);
2004 SetDictInt("COLS", COLS);
Guido van Rossum56bf2351994-08-31 22:06:24 +00002005
Victor Stinnerec234762010-05-15 22:26:41 +00002006 return (PyObject *)PyCursesWindow_New(win);
2007 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00002008
Victor Stinnerec234762010-05-15 22:26:41 +00002009 static PyObject *
2010 PyCurses_setupterm(PyObject* self, PyObject *args, PyObject* keywds)
2011 {
2012 int fd = -1;
2013 int err;
2014 char* termstr = NULL;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002015
Victor Stinnerec234762010-05-15 22:26:41 +00002016 static char *kwlist[] = {"term", "fd", NULL};
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002017
Victor Stinnerec234762010-05-15 22:26:41 +00002018 if (!PyArg_ParseTupleAndKeywords(
2019 args, keywds, "|zi:setupterm", kwlist, &termstr, &fd)) {
2020 return NULL;
2021 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002022
Victor Stinnerec234762010-05-15 22:26:41 +00002023 if (fd == -1) {
2024 PyObject* sys_stdout;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002025
Victor Stinnerec234762010-05-15 22:26:41 +00002026 sys_stdout = PySys_GetObject("stdout");
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002027
Victor Stinnerec234762010-05-15 22:26:41 +00002028 if (sys_stdout == NULL) {
2029 PyErr_SetString(
2030 PyCursesError,
2031 "lost sys.stdout");
2032 return NULL;
2033 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002034
Victor Stinnerec234762010-05-15 22:26:41 +00002035 fd = PyObject_AsFileDescriptor(sys_stdout);
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002036
Victor Stinnerec234762010-05-15 22:26:41 +00002037 if (fd == -1) {
2038 return NULL;
2039 }
2040 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002041
Victor Stinnerec234762010-05-15 22:26:41 +00002042 if (setupterm(termstr,fd,&err) == ERR) {
2043 char* s = "setupterm: unknown error";
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002044
Victor Stinnerec234762010-05-15 22:26:41 +00002045 if (err == 0) {
2046 s = "setupterm: could not find terminal";
2047 } else if (err == -1) {
2048 s = "setupterm: could not find terminfo database";
2049 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002050
Victor Stinnerec234762010-05-15 22:26:41 +00002051 PyErr_SetString(PyCursesError,s);
2052 return NULL;
2053 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00002054
Victor Stinnerec234762010-05-15 22:26:41 +00002055 initialised_setupterm = TRUE;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002056
Victor Stinnerec234762010-05-15 22:26:41 +00002057 Py_INCREF(Py_None);
2058 return Py_None;
2059 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002060
Victor Stinnerec234762010-05-15 22:26:41 +00002061 static PyObject *
2062 PyCurses_IntrFlush(PyObject *self, PyObject *args)
2063 {
2064 int ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002065
Victor Stinnerec234762010-05-15 22:26:41 +00002066 PyCursesInitialised
2067
2068 switch(PyTuple_Size(args)) {
2069 case 1:
2070 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL;
2071 break;
2072 default:
2073 PyErr_SetString(PyExc_TypeError, "intrflush requires 1 argument");
2074 return NULL;
2075 }
2076
2077 return PyCursesCheckERR(intrflush(NULL,ch), "intrflush");
2078 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002079
Walter Dörwald4994d952006-06-19 08:07:50 +00002080#ifdef HAVE_CURSES_IS_TERM_RESIZED
Victor Stinnerec234762010-05-15 22:26:41 +00002081 static PyObject *
2082 PyCurses_Is_Term_Resized(PyObject *self, PyObject *args)
2083 {
2084 int lines;
2085 int columns;
2086 int result;
Walter Dörwald4994d952006-06-19 08:07:50 +00002087
Victor Stinnerec234762010-05-15 22:26:41 +00002088 PyCursesInitialised
Walter Dörwald4994d952006-06-19 08:07:50 +00002089
Victor Stinnerec234762010-05-15 22:26:41 +00002090 if (!PyArg_ParseTuple(args,"ii:is_term_resized", &lines, &columns))
2091 return NULL;
2092 result = is_term_resized(lines, columns);
2093 if (result == TRUE) {
2094 Py_INCREF(Py_True);
2095 return Py_True;
2096 } else {
2097 Py_INCREF(Py_False);
2098 return Py_False;
2099 }
2100 }
Walter Dörwald4994d952006-06-19 08:07:50 +00002101#endif /* HAVE_CURSES_IS_TERM_RESIZED */
2102
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00002103#if !defined(__NetBSD__)
Victor Stinnerec234762010-05-15 22:26:41 +00002104 static PyObject *
2105 PyCurses_KeyName(PyObject *self, PyObject *args)
2106 {
2107 const char *knp;
2108 int ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002109
Victor Stinnerec234762010-05-15 22:26:41 +00002110 PyCursesInitialised
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002111
Victor Stinnerec234762010-05-15 22:26:41 +00002112 if (!PyArg_ParseTuple(args,"i",&ch)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002113
Victor Stinnerec234762010-05-15 22:26:41 +00002114 if (ch < 0) {
2115 PyErr_SetString(PyExc_ValueError, "invalid key number");
2116 return NULL;
2117 }
2118 knp = keyname(ch);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002119
Victor Stinnerec234762010-05-15 22:26:41 +00002120 return PyString_FromString((knp == NULL) ? "" : (char *)knp);
2121 }
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00002122#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002123
Victor Stinnerec234762010-05-15 22:26:41 +00002124 static PyObject *
2125 PyCurses_KillChar(PyObject *self)
2126 {
2127 char ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002128
Victor Stinnerec234762010-05-15 22:26:41 +00002129 ch = killchar();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002130
Victor Stinnerec234762010-05-15 22:26:41 +00002131 return PyString_FromStringAndSize(&ch, 1);
2132 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002133
Victor Stinnerec234762010-05-15 22:26:41 +00002134 static PyObject *
2135 PyCurses_Meta(PyObject *self, PyObject *args)
2136 {
2137 int ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002138
Victor Stinnerec234762010-05-15 22:26:41 +00002139 PyCursesInitialised
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002140
Victor Stinnerec234762010-05-15 22:26:41 +00002141 switch(PyTuple_Size(args)) {
2142 case 1:
2143 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL;
2144 break;
2145 default:
2146 PyErr_SetString(PyExc_TypeError, "meta requires 1 argument");
2147 return NULL;
2148 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002149
Victor Stinnerec234762010-05-15 22:26:41 +00002150 return PyCursesCheckERR(meta(stdscr, ch), "meta");
2151 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002152
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002153#ifdef NCURSES_MOUSE_VERSION
Victor Stinnerec234762010-05-15 22:26:41 +00002154 static PyObject *
2155 PyCurses_MouseInterval(PyObject *self, PyObject *args)
2156 {
2157 int interval;
2158 PyCursesInitialised
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002159
Victor Stinnerec234762010-05-15 22:26:41 +00002160 if (!PyArg_ParseTuple(args,"i;interval",&interval))
2161 return NULL;
2162 return PyCursesCheckERR(mouseinterval(interval), "mouseinterval");
2163 }
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002164
Victor Stinnerec234762010-05-15 22:26:41 +00002165 static PyObject *
2166 PyCurses_MouseMask(PyObject *self, PyObject *args)
2167 {
2168 int newmask;
2169 mmask_t oldmask, availmask;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002170
Victor Stinnerec234762010-05-15 22:26:41 +00002171 PyCursesInitialised
2172 if (!PyArg_ParseTuple(args,"i;mousemask",&newmask))
2173 return NULL;
2174 availmask = mousemask(newmask, &oldmask);
2175 return Py_BuildValue("(ll)", (long)availmask, (long)oldmask);
2176 }
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002177#endif
2178
Victor Stinnerec234762010-05-15 22:26:41 +00002179 static PyObject *
2180 PyCurses_Napms(PyObject *self, PyObject *args)
2181 {
2182 int ms;
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +00002183
Victor Stinnerec234762010-05-15 22:26:41 +00002184 PyCursesInitialised
2185 if (!PyArg_ParseTuple(args, "i;ms", &ms)) return NULL;
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +00002186
Victor Stinnerec234762010-05-15 22:26:41 +00002187 return Py_BuildValue("i", napms(ms));
2188 }
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +00002189
2190
Victor Stinnerec234762010-05-15 22:26:41 +00002191 static PyObject *
2192 PyCurses_NewPad(PyObject *self, PyObject *args)
2193 {
2194 WINDOW *win;
2195 int nlines, ncols;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002196
Victor Stinnerec234762010-05-15 22:26:41 +00002197 PyCursesInitialised
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002198
Victor Stinnerec234762010-05-15 22:26:41 +00002199 if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002200
Victor Stinnerec234762010-05-15 22:26:41 +00002201 win = newpad(nlines, ncols);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002202
Victor Stinnerec234762010-05-15 22:26:41 +00002203 if (win == NULL) {
2204 PyErr_SetString(PyCursesError, catchall_NULL);
2205 return NULL;
2206 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00002207
Victor Stinnerec234762010-05-15 22:26:41 +00002208 return (PyObject *)PyCursesWindow_New(win);
2209 }
Guido van Rossum85738471995-02-17 13:50:17 +00002210
Victor Stinnerec234762010-05-15 22:26:41 +00002211 static PyObject *
2212 PyCurses_NewWindow(PyObject *self, PyObject *args)
2213 {
2214 WINDOW *win;
2215 int nlines, ncols, begin_y=0, begin_x=0;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002216
Victor Stinnerec234762010-05-15 22:26:41 +00002217 PyCursesInitialised
Guido van Rossum85738471995-02-17 13:50:17 +00002218
Victor Stinnerec234762010-05-15 22:26:41 +00002219 switch (PyTuple_Size(args)) {
2220 case 2:
2221 if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols))
2222 return NULL;
2223 break;
2224 case 4:
2225 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
2226 &nlines,&ncols,&begin_y,&begin_x))
2227 return NULL;
2228 break;
2229 default:
2230 PyErr_SetString(PyExc_TypeError, "newwin requires 2 or 4 arguments");
2231 return NULL;
2232 }
Guido van Rossum85738471995-02-17 13:50:17 +00002233
Victor Stinnerec234762010-05-15 22:26:41 +00002234 win = newwin(nlines,ncols,begin_y,begin_x);
2235 if (win == NULL) {
2236 PyErr_SetString(PyCursesError, catchall_NULL);
2237 return NULL;
2238 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00002239
Victor Stinnerec234762010-05-15 22:26:41 +00002240 return (PyObject *)PyCursesWindow_New(win);
2241 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002242
Victor Stinnerec234762010-05-15 22:26:41 +00002243 static PyObject *
2244 PyCurses_Pair_Content(PyObject *self, PyObject *args)
2245 {
2246 short pair,f,b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002247
Victor Stinnerec234762010-05-15 22:26:41 +00002248 PyCursesInitialised
2249 PyCursesInitialisedColor
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002250
Victor Stinnerec234762010-05-15 22:26:41 +00002251 switch(PyTuple_Size(args)) {
2252 case 1:
2253 if (!PyArg_ParseTuple(args, "h;pair", &pair)) return NULL;
2254 break;
2255 default:
2256 PyErr_SetString(PyExc_TypeError, "pair_content requires 1 argument");
2257 return NULL;
2258 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002259
Victor Stinnerec234762010-05-15 22:26:41 +00002260 if (pair_content(pair, &f, &b)==ERR) {
2261 PyErr_SetString(PyCursesError,
2262 "Argument 1 was out of range. (1..COLOR_PAIRS-1)");
2263 return NULL;
2264 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002265
Victor Stinnerec234762010-05-15 22:26:41 +00002266 return Py_BuildValue("(ii)", f, b);
2267 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002268
Victor Stinnerec234762010-05-15 22:26:41 +00002269 static PyObject *
2270 PyCurses_pair_number(PyObject *self, PyObject *args)
2271 {
2272 int n;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002273
Victor Stinnerec234762010-05-15 22:26:41 +00002274 PyCursesInitialised
2275 PyCursesInitialisedColor
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002276
Victor Stinnerec234762010-05-15 22:26:41 +00002277 switch(PyTuple_Size(args)) {
2278 case 1:
2279 if (!PyArg_ParseTuple(args, "i;pairvalue", &n)) return NULL;
2280 break;
2281 default:
2282 PyErr_SetString(PyExc_TypeError,
2283 "pair_number requires 1 argument");
2284 return NULL;
2285 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002286
Victor Stinnerec234762010-05-15 22:26:41 +00002287 return PyInt_FromLong((long) ((n & A_COLOR) >> 8));
2288 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002289
Victor Stinnerec234762010-05-15 22:26:41 +00002290 static PyObject *
2291 PyCurses_Putp(PyObject *self, PyObject *args)
2292 {
2293 char *str;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002294
Victor Stinnerec234762010-05-15 22:26:41 +00002295 if (!PyArg_ParseTuple(args,"s;str", &str)) return NULL;
2296 return PyCursesCheckERR(putp(str), "putp");
2297 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002298
Victor Stinnerec234762010-05-15 22:26:41 +00002299 static PyObject *
2300 PyCurses_QiFlush(PyObject *self, PyObject *args)
2301 {
2302 int flag = 0;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002303
Victor Stinnerec234762010-05-15 22:26:41 +00002304 PyCursesInitialised
2305
2306 switch(PyTuple_Size(args)) {
2307 case 0:
2308 qiflush();
2309 Py_INCREF(Py_None);
2310 return Py_None;
2311 case 1:
2312 if (!PyArg_ParseTuple(args, "i;True(1) or False(0)", &flag)) return NULL;
2313 if (flag) qiflush();
2314 else noqiflush();
2315 Py_INCREF(Py_None);
2316 return Py_None;
2317 default:
2318 PyErr_SetString(PyExc_TypeError, "qiflush requires 0 or 1 arguments");
2319 return NULL;
2320 }
2321 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002322
Walter Dörwaldd391f082007-03-06 20:38:57 +00002323/* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES
2324 * and _curses.COLS */
Andrew M. Kuchling3d8f8c12008-06-19 14:02:30 +00002325#if defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM)
Victor Stinnerec234762010-05-15 22:26:41 +00002326 static int
2327 update_lines_cols(void)
2328 {
2329 PyObject *o;
2330 PyObject *m = PyImport_ImportModuleNoBlock("curses");
Walter Dörwaldd391f082007-03-06 20:38:57 +00002331
Victor Stinnerec234762010-05-15 22:26:41 +00002332 if (!m)
2333 return 0;
Walter Dörwaldd391f082007-03-06 20:38:57 +00002334
Victor Stinnerec234762010-05-15 22:26:41 +00002335 o = PyInt_FromLong(LINES);
2336 if (!o) {
2337 Py_DECREF(m);
2338 return 0;
2339 }
2340 if (PyObject_SetAttrString(m, "LINES", o)) {
2341 Py_DECREF(m);
2342 Py_DECREF(o);
2343 return 0;
2344 }
2345 if (PyDict_SetItemString(ModDict, "LINES", o)) {
2346 Py_DECREF(m);
2347 Py_DECREF(o);
2348 return 0;
2349 }
2350 Py_DECREF(o);
2351 o = PyInt_FromLong(COLS);
2352 if (!o) {
2353 Py_DECREF(m);
2354 return 0;
2355 }
2356 if (PyObject_SetAttrString(m, "COLS", o)) {
2357 Py_DECREF(m);
2358 Py_DECREF(o);
2359 return 0;
2360 }
2361 if (PyDict_SetItemString(ModDict, "COLS", o)) {
2362 Py_DECREF(m);
2363 Py_DECREF(o);
2364 return 0;
2365 }
2366 Py_DECREF(o);
2367 Py_DECREF(m);
2368 return 1;
2369 }
Andrew M. Kuchling3d8f8c12008-06-19 14:02:30 +00002370#endif
Walter Dörwaldd391f082007-03-06 20:38:57 +00002371
Walter Dörwald4994d952006-06-19 08:07:50 +00002372#ifdef HAVE_CURSES_RESIZETERM
Victor Stinnerec234762010-05-15 22:26:41 +00002373 static PyObject *
2374 PyCurses_ResizeTerm(PyObject *self, PyObject *args)
2375 {
2376 int lines;
2377 int columns;
2378 PyObject *result;
Walter Dörwald4994d952006-06-19 08:07:50 +00002379
Victor Stinnerec234762010-05-15 22:26:41 +00002380 PyCursesInitialised
Walter Dörwald4994d952006-06-19 08:07:50 +00002381
Victor Stinnerec234762010-05-15 22:26:41 +00002382 if (!PyArg_ParseTuple(args,"ii:resizeterm", &lines, &columns))
2383 return NULL;
Walter Dörwald4994d952006-06-19 08:07:50 +00002384
Victor Stinnerec234762010-05-15 22:26:41 +00002385 result = PyCursesCheckERR(resizeterm(lines, columns), "resizeterm");
2386 if (!result)
2387 return NULL;
2388 if (!update_lines_cols())
2389 return NULL;
2390 return result;
2391 }
Walter Dörwald4994d952006-06-19 08:07:50 +00002392
2393#endif
2394
2395#ifdef HAVE_CURSES_RESIZE_TERM
Victor Stinnerec234762010-05-15 22:26:41 +00002396 static PyObject *
2397 PyCurses_Resize_Term(PyObject *self, PyObject *args)
2398 {
2399 int lines;
2400 int columns;
Walter Dörwald4994d952006-06-19 08:07:50 +00002401
Victor Stinnerec234762010-05-15 22:26:41 +00002402 PyObject *result;
Walter Dörwaldd391f082007-03-06 20:38:57 +00002403
Victor Stinnerec234762010-05-15 22:26:41 +00002404 PyCursesInitialised
Walter Dörwald4994d952006-06-19 08:07:50 +00002405
Victor Stinnerec234762010-05-15 22:26:41 +00002406 if (!PyArg_ParseTuple(args,"ii:resize_term", &lines, &columns))
2407 return NULL;
Walter Dörwald4994d952006-06-19 08:07:50 +00002408
Victor Stinnerec234762010-05-15 22:26:41 +00002409 result = PyCursesCheckERR(resize_term(lines, columns), "resize_term");
2410 if (!result)
2411 return NULL;
2412 if (!update_lines_cols())
2413 return NULL;
2414 return result;
2415 }
Walter Dörwald4994d952006-06-19 08:07:50 +00002416#endif /* HAVE_CURSES_RESIZE_TERM */
2417
Victor Stinnerec234762010-05-15 22:26:41 +00002418 static PyObject *
2419 PyCurses_setsyx(PyObject *self, PyObject *args)
2420 {
2421 int y,x;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002422
Victor Stinnerec234762010-05-15 22:26:41 +00002423 PyCursesInitialised
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002424
Victor Stinnerec234762010-05-15 22:26:41 +00002425 if (PyTuple_Size(args)!=2) {
2426 PyErr_SetString(PyExc_TypeError, "setsyx requires 2 arguments");
2427 return NULL;
2428 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002429
Victor Stinnerec234762010-05-15 22:26:41 +00002430 if (!PyArg_ParseTuple(args, "ii;y, x", &y, &x)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002431
Victor Stinnerec234762010-05-15 22:26:41 +00002432 setsyx(y,x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002433
Victor Stinnerec234762010-05-15 22:26:41 +00002434 Py_INCREF(Py_None);
2435 return Py_None;
2436 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00002437
Victor Stinnerec234762010-05-15 22:26:41 +00002438 static PyObject *
2439 PyCurses_Start_Color(PyObject *self)
2440 {
2441 int code;
2442 PyObject *c, *cp;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002443
Victor Stinnerec234762010-05-15 22:26:41 +00002444 PyCursesInitialised
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002445
Victor Stinnerec234762010-05-15 22:26:41 +00002446 code = start_color();
2447 if (code != ERR) {
2448 initialisedcolors = TRUE;
2449 c = PyInt_FromLong((long) COLORS);
2450 PyDict_SetItemString(ModDict, "COLORS", c);
2451 Py_DECREF(c);
2452 cp = PyInt_FromLong((long) COLOR_PAIRS);
2453 PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp);
2454 Py_DECREF(cp);
2455 Py_INCREF(Py_None);
2456 return Py_None;
2457 } else {
2458 PyErr_SetString(PyCursesError, "start_color() returned ERR");
2459 return NULL;
2460 }
2461 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002462
Victor Stinnerec234762010-05-15 22:26:41 +00002463 static PyObject *
2464 PyCurses_tigetflag(PyObject *self, PyObject *args)
2465 {
2466 char *capname;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002467
Victor Stinnerec234762010-05-15 22:26:41 +00002468 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002469
Victor Stinnerec234762010-05-15 22:26:41 +00002470 if (!PyArg_ParseTuple(args, "s", &capname))
2471 return NULL;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002472
Victor Stinnerec234762010-05-15 22:26:41 +00002473 return PyInt_FromLong( (long) tigetflag( capname ) );
2474 }
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002475
Victor Stinnerec234762010-05-15 22:26:41 +00002476 static PyObject *
2477 PyCurses_tigetnum(PyObject *self, PyObject *args)
2478 {
2479 char *capname;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002480
Victor Stinnerec234762010-05-15 22:26:41 +00002481 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002482
Victor Stinnerec234762010-05-15 22:26:41 +00002483 if (!PyArg_ParseTuple(args, "s", &capname))
2484 return NULL;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002485
Victor Stinnerec234762010-05-15 22:26:41 +00002486 return PyInt_FromLong( (long) tigetnum( capname ) );
2487 }
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002488
Victor Stinnerec234762010-05-15 22:26:41 +00002489 static PyObject *
2490 PyCurses_tigetstr(PyObject *self, PyObject *args)
2491 {
2492 char *capname;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002493
Victor Stinnerec234762010-05-15 22:26:41 +00002494 PyCursesSetupTermCalled;
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00002495
Victor Stinnerec234762010-05-15 22:26:41 +00002496 if (!PyArg_ParseTuple(args, "s", &capname))
2497 return NULL;
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00002498
Victor Stinnerec234762010-05-15 22:26:41 +00002499 capname = tigetstr( capname );
2500 if (capname == 0 || capname == (char*) -1) {
2501 Py_INCREF(Py_None);
2502 return Py_None;
2503 }
2504 return PyString_FromString( capname );
2505 }
Michael W. Hudson21007982002-11-21 14:17:51 +00002506
Victor Stinnerec234762010-05-15 22:26:41 +00002507 static PyObject *
2508 PyCurses_tparm(PyObject *self, PyObject *args)
2509 {
2510 char* fmt;
2511 char* result = NULL;
2512 int i1=0,i2=0,i3=0,i4=0,i5=0,i6=0,i7=0,i8=0,i9=0;
Michael W. Hudson21007982002-11-21 14:17:51 +00002513
Victor Stinnerec234762010-05-15 22:26:41 +00002514 PyCursesSetupTermCalled;
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00002515
Victor Stinnerec234762010-05-15 22:26:41 +00002516 if (!PyArg_ParseTuple(args, "s|iiiiiiiii:tparm",
2517 &fmt, &i1, &i2, &i3, &i4,
2518 &i5, &i6, &i7, &i8, &i9)) {
2519 return NULL;
2520 }
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00002521
Victor Stinnerec234762010-05-15 22:26:41 +00002522 result = tparm(fmt,i1,i2,i3,i4,i5,i6,i7,i8,i9);
2523 if (!result) {
2524 PyErr_SetString(PyCursesError, "tparm() returned NULL");
2525 return NULL;
2526 }
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00002527
Victor Stinnerec234762010-05-15 22:26:41 +00002528 return PyString_FromString(result);
2529 }
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00002530
Victor Stinnerec234762010-05-15 22:26:41 +00002531 static PyObject *
2532 PyCurses_TypeAhead(PyObject *self, PyObject *args)
2533 {
2534 int fd;
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00002535
Victor Stinnerec234762010-05-15 22:26:41 +00002536 PyCursesInitialised
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002537
Victor Stinnerec234762010-05-15 22:26:41 +00002538 if (!PyArg_ParseTuple(args,"i;fd",&fd)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002539
Victor Stinnerec234762010-05-15 22:26:41 +00002540 return PyCursesCheckERR(typeahead( fd ), "typeahead");
2541 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002542
Victor Stinnerec234762010-05-15 22:26:41 +00002543 static PyObject *
2544 PyCurses_UnCtrl(PyObject *self, PyObject *args)
2545 {
2546 PyObject *temp;
2547 chtype ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002548
Victor Stinnerec234762010-05-15 22:26:41 +00002549 PyCursesInitialised
Guido van Rossumf6971e21994-08-30 12:25:20 +00002550
Victor Stinnerec234762010-05-15 22:26:41 +00002551 if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002552
Victor Stinnerec234762010-05-15 22:26:41 +00002553 if (PyInt_Check(temp))
2554 ch = (chtype) PyInt_AsLong(temp);
2555 else if (PyString_Check(temp))
2556 ch = (chtype) *PyString_AsString(temp);
2557 else {
2558 PyErr_SetString(PyExc_TypeError, "argument must be a ch or an int");
2559 return NULL;
2560 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002561
Victor Stinnerec234762010-05-15 22:26:41 +00002562 return PyString_FromString(unctrl(ch));
2563 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002564
Victor Stinnerec234762010-05-15 22:26:41 +00002565 static PyObject *
2566 PyCurses_UngetCh(PyObject *self, PyObject *args)
2567 {
2568 PyObject *temp;
2569 int ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002570
Victor Stinnerec234762010-05-15 22:26:41 +00002571 PyCursesInitialised
Guido van Rossumf6971e21994-08-30 12:25:20 +00002572
Victor Stinnerec234762010-05-15 22:26:41 +00002573 if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002574
Victor Stinnerec234762010-05-15 22:26:41 +00002575 if (PyInt_Check(temp))
2576 ch = (int) PyInt_AsLong(temp);
2577 else if (PyString_Check(temp))
2578 ch = (int) *PyString_AsString(temp);
2579 else {
2580 PyErr_SetString(PyExc_TypeError, "argument must be a ch or an int");
2581 return NULL;
2582 }
2583
2584 return PyCursesCheckERR(ungetch(ch), "ungetch");
2585 }
2586
2587 static PyObject *
2588 PyCurses_Use_Env(PyObject *self, PyObject *args)
2589 {
2590 int flag;
2591
2592 switch(PyTuple_Size(args)) {
2593 case 1:
2594 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&flag))
2595 return NULL;
2596 break;
2597 default:
2598 PyErr_SetString(PyExc_TypeError, "use_env requires 1 argument");
2599 return NULL;
2600 }
2601 use_env(flag);
2602 Py_INCREF(Py_None);
2603 return Py_None;
2604 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00002605
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00002606#ifndef STRICT_SYSV_CURSES
Victor Stinnerec234762010-05-15 22:26:41 +00002607 static PyObject *
2608 PyCurses_Use_Default_Colors(PyObject *self)
2609 {
2610 int code;
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00002611
Victor Stinnerec234762010-05-15 22:26:41 +00002612 PyCursesInitialised
2613 PyCursesInitialisedColor
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00002614
Victor Stinnerec234762010-05-15 22:26:41 +00002615 code = use_default_colors();
2616 if (code != ERR) {
2617 Py_INCREF(Py_None);
2618 return Py_None;
2619 } else {
2620 PyErr_SetString(PyCursesError, "use_default_colors() returned ERR");
2621 return NULL;
2622 }
2623 }
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00002624#endif /* STRICT_SYSV_CURSES */
2625
Guido van Rossumf6971e21994-08-30 12:25:20 +00002626/* List of functions defined in the module */
2627
Victor Stinnerec234762010-05-15 22:26:41 +00002628 static PyMethodDef PyCurses_methods[] = {
2629 {"baudrate", (PyCFunction)PyCurses_baudrate, METH_NOARGS},
2630 {"beep", (PyCFunction)PyCurses_beep, METH_NOARGS},
2631 {"can_change_color", (PyCFunction)PyCurses_can_change_color, METH_NOARGS},
2632 {"cbreak", (PyCFunction)PyCurses_cbreak, METH_VARARGS},
2633 {"color_content", (PyCFunction)PyCurses_Color_Content, METH_VARARGS},
2634 {"color_pair", (PyCFunction)PyCurses_color_pair, METH_VARARGS},
2635 {"curs_set", (PyCFunction)PyCurses_Curs_Set, METH_VARARGS},
2636 {"def_prog_mode", (PyCFunction)PyCurses_def_prog_mode, METH_NOARGS},
2637 {"def_shell_mode", (PyCFunction)PyCurses_def_shell_mode, METH_NOARGS},
2638 {"delay_output", (PyCFunction)PyCurses_Delay_Output, METH_VARARGS},
2639 {"doupdate", (PyCFunction)PyCurses_doupdate, METH_NOARGS},
2640 {"echo", (PyCFunction)PyCurses_echo, METH_VARARGS},
2641 {"endwin", (PyCFunction)PyCurses_endwin, METH_NOARGS},
2642 {"erasechar", (PyCFunction)PyCurses_EraseChar, METH_NOARGS},
2643 {"filter", (PyCFunction)PyCurses_filter, METH_NOARGS},
2644 {"flash", (PyCFunction)PyCurses_flash, METH_NOARGS},
2645 {"flushinp", (PyCFunction)PyCurses_flushinp, METH_NOARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002646#ifdef NCURSES_MOUSE_VERSION
Victor Stinnerec234762010-05-15 22:26:41 +00002647 {"getmouse", (PyCFunction)PyCurses_GetMouse, METH_NOARGS},
2648 {"ungetmouse", (PyCFunction)PyCurses_UngetMouse, METH_VARARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002649#endif
Victor Stinnerec234762010-05-15 22:26:41 +00002650 {"getsyx", (PyCFunction)PyCurses_getsyx, METH_NOARGS},
2651 {"getwin", (PyCFunction)PyCurses_GetWin, METH_O},
2652 {"has_colors", (PyCFunction)PyCurses_has_colors, METH_NOARGS},
2653 {"has_ic", (PyCFunction)PyCurses_has_ic, METH_NOARGS},
2654 {"has_il", (PyCFunction)PyCurses_has_il, METH_NOARGS},
Andrew M. Kuchlingbf680262000-07-25 12:37:41 +00002655#ifndef STRICT_SYSV_CURSES
Victor Stinnerec234762010-05-15 22:26:41 +00002656 {"has_key", (PyCFunction)PyCurses_has_key, METH_VARARGS},
Guido van Rossumf6971e21994-08-30 12:25:20 +00002657#endif
Victor Stinnerec234762010-05-15 22:26:41 +00002658 {"halfdelay", (PyCFunction)PyCurses_HalfDelay, METH_VARARGS},
2659 {"init_color", (PyCFunction)PyCurses_Init_Color, METH_VARARGS},
2660 {"init_pair", (PyCFunction)PyCurses_Init_Pair, METH_VARARGS},
2661 {"initscr", (PyCFunction)PyCurses_InitScr, METH_NOARGS},
2662 {"intrflush", (PyCFunction)PyCurses_IntrFlush, METH_VARARGS},
2663 {"isendwin", (PyCFunction)PyCurses_isendwin, METH_NOARGS},
Walter Dörwald4994d952006-06-19 08:07:50 +00002664#ifdef HAVE_CURSES_IS_TERM_RESIZED
Victor Stinnerec234762010-05-15 22:26:41 +00002665 {"is_term_resized", (PyCFunction)PyCurses_Is_Term_Resized, METH_VARARGS},
Walter Dörwald4994d952006-06-19 08:07:50 +00002666#endif
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00002667#if !defined(__NetBSD__)
Victor Stinnerec234762010-05-15 22:26:41 +00002668 {"keyname", (PyCFunction)PyCurses_KeyName, METH_VARARGS},
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00002669#endif
Victor Stinnerec234762010-05-15 22:26:41 +00002670 {"killchar", (PyCFunction)PyCurses_KillChar, METH_NOARGS},
2671 {"longname", (PyCFunction)PyCurses_longname, METH_NOARGS},
2672 {"meta", (PyCFunction)PyCurses_Meta, METH_VARARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002673#ifdef NCURSES_MOUSE_VERSION
Victor Stinnerec234762010-05-15 22:26:41 +00002674 {"mouseinterval", (PyCFunction)PyCurses_MouseInterval, METH_VARARGS},
2675 {"mousemask", (PyCFunction)PyCurses_MouseMask, METH_VARARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002676#endif
Victor Stinnerec234762010-05-15 22:26:41 +00002677 {"napms", (PyCFunction)PyCurses_Napms, METH_VARARGS},
2678 {"newpad", (PyCFunction)PyCurses_NewPad, METH_VARARGS},
2679 {"newwin", (PyCFunction)PyCurses_NewWindow, METH_VARARGS},
2680 {"nl", (PyCFunction)PyCurses_nl, METH_VARARGS},
2681 {"nocbreak", (PyCFunction)PyCurses_nocbreak, METH_NOARGS},
2682 {"noecho", (PyCFunction)PyCurses_noecho, METH_NOARGS},
2683 {"nonl", (PyCFunction)PyCurses_nonl, METH_NOARGS},
2684 {"noqiflush", (PyCFunction)PyCurses_noqiflush, METH_NOARGS},
2685 {"noraw", (PyCFunction)PyCurses_noraw, METH_NOARGS},
2686 {"pair_content", (PyCFunction)PyCurses_Pair_Content, METH_VARARGS},
2687 {"pair_number", (PyCFunction)PyCurses_pair_number, METH_VARARGS},
2688 {"putp", (PyCFunction)PyCurses_Putp, METH_VARARGS},
2689 {"qiflush", (PyCFunction)PyCurses_QiFlush, METH_VARARGS},
2690 {"raw", (PyCFunction)PyCurses_raw, METH_VARARGS},
2691 {"reset_prog_mode", (PyCFunction)PyCurses_reset_prog_mode, METH_NOARGS},
2692 {"reset_shell_mode", (PyCFunction)PyCurses_reset_shell_mode, METH_NOARGS},
2693 {"resetty", (PyCFunction)PyCurses_resetty, METH_NOARGS},
Walter Dörwald4994d952006-06-19 08:07:50 +00002694#ifdef HAVE_CURSES_RESIZETERM
Victor Stinnerec234762010-05-15 22:26:41 +00002695 {"resizeterm", (PyCFunction)PyCurses_ResizeTerm, METH_VARARGS},
Walter Dörwald4994d952006-06-19 08:07:50 +00002696#endif
2697#ifdef HAVE_CURSES_RESIZE_TERM
Victor Stinnerec234762010-05-15 22:26:41 +00002698 {"resize_term", (PyCFunction)PyCurses_Resize_Term, METH_VARARGS},
Walter Dörwald4994d952006-06-19 08:07:50 +00002699#endif
Victor Stinnerec234762010-05-15 22:26:41 +00002700 {"savetty", (PyCFunction)PyCurses_savetty, METH_NOARGS},
2701 {"setsyx", (PyCFunction)PyCurses_setsyx, METH_VARARGS},
2702 {"setupterm", (PyCFunction)PyCurses_setupterm,
2703 METH_VARARGS|METH_KEYWORDS},
2704 {"start_color", (PyCFunction)PyCurses_Start_Color, METH_NOARGS},
2705 {"termattrs", (PyCFunction)PyCurses_termattrs, METH_NOARGS},
2706 {"termname", (PyCFunction)PyCurses_termname, METH_NOARGS},
2707 {"tigetflag", (PyCFunction)PyCurses_tigetflag, METH_VARARGS},
2708 {"tigetnum", (PyCFunction)PyCurses_tigetnum, METH_VARARGS},
2709 {"tigetstr", (PyCFunction)PyCurses_tigetstr, METH_VARARGS},
2710 {"tparm", (PyCFunction)PyCurses_tparm, METH_VARARGS},
2711 {"typeahead", (PyCFunction)PyCurses_TypeAhead, METH_VARARGS},
2712 {"unctrl", (PyCFunction)PyCurses_UnCtrl, METH_VARARGS},
2713 {"ungetch", (PyCFunction)PyCurses_UngetCh, METH_VARARGS},
2714 {"use_env", (PyCFunction)PyCurses_Use_Env, METH_VARARGS},
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00002715#ifndef STRICT_SYSV_CURSES
Victor Stinnerec234762010-05-15 22:26:41 +00002716 {"use_default_colors", (PyCFunction)PyCurses_Use_Default_Colors, METH_NOARGS},
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00002717#endif
Victor Stinnerec234762010-05-15 22:26:41 +00002718 {NULL, NULL} /* sentinel */
2719 };
Guido van Rossumf6971e21994-08-30 12:25:20 +00002720
2721/* Initialization function for the module */
2722
Victor Stinnerec234762010-05-15 22:26:41 +00002723 PyMODINIT_FUNC
2724 init_curses(void)
2725 {
2726 PyObject *m, *d, *v, *c_api_object;
2727 static void *PyCurses_API[PyCurses_API_pointers];
Andrew M. Kuchling32552682000-12-22 21:52:27 +00002728
Victor Stinnerec234762010-05-15 22:26:41 +00002729 /* Initialize object type */
2730 Py_TYPE(&PyCursesWindow_Type) = &PyType_Type;
Guido van Rossum6915c4d2001-01-19 00:28:08 +00002731
Victor Stinnerec234762010-05-15 22:26:41 +00002732 /* Initialize the C API pointer array */
2733 PyCurses_API[0] = (void *)&PyCursesWindow_Type;
2734 PyCurses_API[1] = (void *)func_PyCursesSetupTermCalled;
2735 PyCurses_API[2] = (void *)func_PyCursesInitialised;
2736 PyCurses_API[3] = (void *)func_PyCursesInitialisedColor;
Guido van Rossumf6971e21994-08-30 12:25:20 +00002737
Victor Stinnerec234762010-05-15 22:26:41 +00002738 /* Create the module and add the functions */
2739 m = Py_InitModule("_curses", PyCurses_methods);
2740 if (m == NULL)
2741 return;
Guido van Rossumf6971e21994-08-30 12:25:20 +00002742
Victor Stinnerec234762010-05-15 22:26:41 +00002743 /* Add some symbolic constants to the module */
2744 d = PyModule_GetDict(m);
2745 if (d == NULL)
2746 return;
2747 ModDict = d; /* For PyCurses_InitScr to use later */
Andrew M. Kuchling32552682000-12-22 21:52:27 +00002748
Victor Stinnerec234762010-05-15 22:26:41 +00002749 /* Add a CObject for the C API */
2750 c_api_object = PyCObject_FromVoidPtr((void *)PyCurses_API, NULL);
2751 PyDict_SetItemString(d, "_C_API", c_api_object);
2752 Py_DECREF(c_api_object);
Guido van Rossumfbea2f31994-08-31 22:05:27 +00002753
Victor Stinnerec234762010-05-15 22:26:41 +00002754 /* For exception curses.error */
2755 PyCursesError = PyErr_NewException("_curses.error", NULL, NULL);
2756 PyDict_SetItemString(d, "error", PyCursesError);
Guido van Rossumfbea2f31994-08-31 22:05:27 +00002757
Victor Stinnerec234762010-05-15 22:26:41 +00002758 /* Make the version available */
2759 v = PyString_FromString(PyCursesVersion);
2760 PyDict_SetItemString(d, "version", v);
2761 PyDict_SetItemString(d, "__version__", v);
2762 Py_DECREF(v);
Guido van Rossumf6971e21994-08-30 12:25:20 +00002763
Victor Stinnerec234762010-05-15 22:26:41 +00002764 SetDictInt("ERR", ERR);
2765 SetDictInt("OK", OK);
Andrew M. Kuchlingcaefb372000-12-26 15:57:01 +00002766
Victor Stinnerec234762010-05-15 22:26:41 +00002767 /* Here are some attributes you can add to chars to print */
2768
2769 SetDictInt("A_ATTRIBUTES", A_ATTRIBUTES);
2770 SetDictInt("A_NORMAL", A_NORMAL);
2771 SetDictInt("A_STANDOUT", A_STANDOUT);
2772 SetDictInt("A_UNDERLINE", A_UNDERLINE);
2773 SetDictInt("A_REVERSE", A_REVERSE);
2774 SetDictInt("A_BLINK", A_BLINK);
2775 SetDictInt("A_DIM", A_DIM);
2776 SetDictInt("A_BOLD", A_BOLD);
2777 SetDictInt("A_ALTCHARSET", A_ALTCHARSET);
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00002778#if !defined(__NetBSD__)
Victor Stinnerec234762010-05-15 22:26:41 +00002779 SetDictInt("A_INVIS", A_INVIS);
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00002780#endif
Victor Stinnerec234762010-05-15 22:26:41 +00002781 SetDictInt("A_PROTECT", A_PROTECT);
2782 SetDictInt("A_CHARTEXT", A_CHARTEXT);
2783 SetDictInt("A_COLOR", A_COLOR);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002784
Victor Stinnerec234762010-05-15 22:26:41 +00002785 /* The following are never available with strict SYSV curses */
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002786#ifdef A_HORIZONTAL
Victor Stinnerec234762010-05-15 22:26:41 +00002787 SetDictInt("A_HORIZONTAL", A_HORIZONTAL);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002788#endif
2789#ifdef A_LEFT
Victor Stinnerec234762010-05-15 22:26:41 +00002790 SetDictInt("A_LEFT", A_LEFT);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002791#endif
2792#ifdef A_LOW
Victor Stinnerec234762010-05-15 22:26:41 +00002793 SetDictInt("A_LOW", A_LOW);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002794#endif
2795#ifdef A_RIGHT
Victor Stinnerec234762010-05-15 22:26:41 +00002796 SetDictInt("A_RIGHT", A_RIGHT);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002797#endif
2798#ifdef A_TOP
Victor Stinnerec234762010-05-15 22:26:41 +00002799 SetDictInt("A_TOP", A_TOP);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002800#endif
2801#ifdef A_VERTICAL
Victor Stinnerec234762010-05-15 22:26:41 +00002802 SetDictInt("A_VERTICAL", A_VERTICAL);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002803#endif
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00002804
Victor Stinnerec234762010-05-15 22:26:41 +00002805 SetDictInt("COLOR_BLACK", COLOR_BLACK);
2806 SetDictInt("COLOR_RED", COLOR_RED);
2807 SetDictInt("COLOR_GREEN", COLOR_GREEN);
2808 SetDictInt("COLOR_YELLOW", COLOR_YELLOW);
2809 SetDictInt("COLOR_BLUE", COLOR_BLUE);
2810 SetDictInt("COLOR_MAGENTA", COLOR_MAGENTA);
2811 SetDictInt("COLOR_CYAN", COLOR_CYAN);
2812 SetDictInt("COLOR_WHITE", COLOR_WHITE);
Guido van Rossumf6971e21994-08-30 12:25:20 +00002813
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002814#ifdef NCURSES_MOUSE_VERSION
Victor Stinnerec234762010-05-15 22:26:41 +00002815 /* Mouse-related constants */
2816 SetDictInt("BUTTON1_PRESSED", BUTTON1_PRESSED);
2817 SetDictInt("BUTTON1_RELEASED", BUTTON1_RELEASED);
2818 SetDictInt("BUTTON1_CLICKED", BUTTON1_CLICKED);
2819 SetDictInt("BUTTON1_DOUBLE_CLICKED", BUTTON1_DOUBLE_CLICKED);
2820 SetDictInt("BUTTON1_TRIPLE_CLICKED", BUTTON1_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002821
Victor Stinnerec234762010-05-15 22:26:41 +00002822 SetDictInt("BUTTON2_PRESSED", BUTTON2_PRESSED);
2823 SetDictInt("BUTTON2_RELEASED", BUTTON2_RELEASED);
2824 SetDictInt("BUTTON2_CLICKED", BUTTON2_CLICKED);
2825 SetDictInt("BUTTON2_DOUBLE_CLICKED", BUTTON2_DOUBLE_CLICKED);
2826 SetDictInt("BUTTON2_TRIPLE_CLICKED", BUTTON2_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002827
Victor Stinnerec234762010-05-15 22:26:41 +00002828 SetDictInt("BUTTON3_PRESSED", BUTTON3_PRESSED);
2829 SetDictInt("BUTTON3_RELEASED", BUTTON3_RELEASED);
2830 SetDictInt("BUTTON3_CLICKED", BUTTON3_CLICKED);
2831 SetDictInt("BUTTON3_DOUBLE_CLICKED", BUTTON3_DOUBLE_CLICKED);
2832 SetDictInt("BUTTON3_TRIPLE_CLICKED", BUTTON3_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002833
Victor Stinnerec234762010-05-15 22:26:41 +00002834 SetDictInt("BUTTON4_PRESSED", BUTTON4_PRESSED);
2835 SetDictInt("BUTTON4_RELEASED", BUTTON4_RELEASED);
2836 SetDictInt("BUTTON4_CLICKED", BUTTON4_CLICKED);
2837 SetDictInt("BUTTON4_DOUBLE_CLICKED", BUTTON4_DOUBLE_CLICKED);
2838 SetDictInt("BUTTON4_TRIPLE_CLICKED", BUTTON4_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002839
Victor Stinnerec234762010-05-15 22:26:41 +00002840 SetDictInt("BUTTON_SHIFT", BUTTON_SHIFT);
2841 SetDictInt("BUTTON_CTRL", BUTTON_CTRL);
2842 SetDictInt("BUTTON_ALT", BUTTON_ALT);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002843
Victor Stinnerec234762010-05-15 22:26:41 +00002844 SetDictInt("ALL_MOUSE_EVENTS", ALL_MOUSE_EVENTS);
2845 SetDictInt("REPORT_MOUSE_POSITION", REPORT_MOUSE_POSITION);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002846#endif
Victor Stinnerec234762010-05-15 22:26:41 +00002847 /* Now set everything up for KEY_ variables */
2848 {
2849 int key;
2850 char *key_n;
2851 char *key_n2;
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00002852#if !defined(__NetBSD__)
Victor Stinnerec234762010-05-15 22:26:41 +00002853 for (key=KEY_MIN;key < KEY_MAX; key++) {
2854 key_n = (char *)keyname(key);
2855 if (key_n == NULL || strcmp(key_n,"UNKNOWN KEY")==0)
2856 continue;
2857 if (strncmp(key_n,"KEY_F(",6)==0) {
2858 char *p1, *p2;
2859 key_n2 = malloc(strlen(key_n)+1);
2860 if (!key_n2) {
2861 PyErr_NoMemory();
2862 break;
2863 }
2864 p1 = key_n;
2865 p2 = key_n2;
2866 while (*p1) {
2867 if (*p1 != '(' && *p1 != ')') {
2868 *p2 = *p1;
2869 p2++;
2870 }
2871 p1++;
2872 }
2873 *p2 = (char)0;
2874 } else
2875 key_n2 = key_n;
2876 SetDictInt(key_n2,key);
2877 if (key_n2 != key_n)
2878 free(key_n2);
2879 }
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00002880#endif
Victor Stinnerec234762010-05-15 22:26:41 +00002881 SetDictInt("KEY_MIN", KEY_MIN);
2882 SetDictInt("KEY_MAX", KEY_MAX);
2883 }
2884 }