blob: 4e1449bf69f9c6c1170187ebe0964a9cdeb2d156 [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. Kuchlingfb6bbf52000-07-27 11:58:01 +000034/*
35
Victor Stinner26486ea2010-05-15 22:23:53 +000036 A number of SysV or ncurses functions don't have wrappers yet; if you
37 need a given function, add it and send a patch. See
38 http://www.python.org/dev/patches/ for instructions on how to submit
39 patches to Python.
Andrew M. Kuchlingfb6bbf52000-07-27 11:58:01 +000040
Victor Stinner26486ea2010-05-15 22:23:53 +000041 Here's a list of currently unsupported functions:
Guido van Rossumd8faa362007-04-27 19:54:29 +000042
Victor Stinner26486ea2010-05-15 22:23:53 +000043 addchnstr addchstr color_set define_key
44 del_curterm delscreen dupwin inchnstr inchstr innstr keyok
45 mcprint mvaddchnstr mvaddchstr mvcur mvinchnstr
46 mvinchstr mvinnstr mmvwaddchnstr mvwaddchstr
47 mvwinchnstr mvwinchstr mvwinnstr newterm
48 restartterm ripoffline scr_dump
49 scr_init scr_restore scr_set scrl set_curterm set_term setterm
50 tgetent tgetflag tgetnum tgetstr tgoto timeout tputs
51 vidattr vidputs waddchnstr waddchstr
52 wcolor_set winchnstr winchstr winnstr wmouse_trafo wscrl
Andrew M. Kuchlingfb6bbf52000-07-27 11:58:01 +000053
Victor Stinner26486ea2010-05-15 22:23:53 +000054 Low-priority:
55 slk_attr slk_attr_off slk_attr_on slk_attr_set slk_attroff
56 slk_attron slk_attrset slk_clear slk_color slk_init slk_label
57 slk_noutrefresh slk_refresh slk_restore slk_set slk_touch
Andrew M. Kuchlingfb6bbf52000-07-27 11:58:01 +000058
Victor Stinner26486ea2010-05-15 22:23:53 +000059 Menu extension (ncurses and probably SYSV):
60 current_item free_item free_menu item_count item_description
61 item_index item_init item_name item_opts item_opts_off
62 item_opts_on item_term item_userptr item_value item_visible
63 menu_back menu_driver menu_fore menu_format menu_grey
64 menu_init menu_items menu_mark menu_opts menu_opts_off
65 menu_opts_on menu_pad menu_pattern menu_request_by_name
66 menu_request_name menu_spacing menu_sub menu_term menu_userptr
67 menu_win new_item new_menu pos_menu_cursor post_menu
68 scale_menu set_current_item set_item_init set_item_opts
69 set_item_term set_item_userptr set_item_value set_menu_back
70 set_menu_fore set_menu_format set_menu_grey set_menu_init
71 set_menu_items set_menu_mark set_menu_opts set_menu_pad
72 set_menu_pattern set_menu_spacing set_menu_sub set_menu_term
73 set_menu_userptr set_menu_win set_top_row top_row unpost_menu
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +000074
Victor Stinner26486ea2010-05-15 22:23:53 +000075 Form extension (ncurses and probably SYSV):
76 current_field data_ahead data_behind dup_field
77 dynamic_fieldinfo field_arg field_back field_buffer
78 field_count field_fore field_index field_info field_init
79 field_just field_opts field_opts_off field_opts_on field_pad
80 field_status field_term field_type field_userptr form_driver
81 form_fields form_init form_opts form_opts_off form_opts_on
82 form_page form_request_by_name form_request_name form_sub
83 form_term form_userptr form_win free_field free_form
84 link_field link_fieldtype move_field new_field new_form
85 new_page pos_form_cursor post_form scale_form
86 set_current_field set_field_back set_field_buffer
87 set_field_fore set_field_init set_field_just set_field_opts
88 set_field_pad set_field_status set_field_term set_field_type
89 set_field_userptr set_fieldtype_arg set_fieldtype_choice
90 set_form_fields set_form_init set_form_opts set_form_page
91 set_form_sub set_form_term set_form_userptr set_form_win
92 set_max_field set_new_page unpost_form
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +000093
94
Victor Stinner26486ea2010-05-15 22:23:53 +000095*/
Andrew M. Kuchlingfb6bbf52000-07-27 11:58:01 +000096
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +000097/* Release Number */
Guido van Rossumf6971e21994-08-30 12:25:20 +000098
Andrew M. Kuchling16e65a02001-10-20 16:05:52 +000099char *PyCursesVersion = "2.2";
Guido van Rossumf6971e21994-08-30 12:25:20 +0000100
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000101/* Includes */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000102
Mark Dickinsone047cfa2010-05-11 17:57:09 +0000103#define PY_SSIZE_T_CLEAN
104
Guido van Rossum602099a1994-09-14 13:32:22 +0000105#include "Python.h"
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000106
Benjamin Petersonb173f782009-05-05 22:31:58 +0000107
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000108#ifdef __hpux
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000109#define STRICT_SYSV_CURSES
110#endif
111
Guido van Rossum858ca0f2001-04-10 19:53:37 +0000112#define CURSES_MODULE
113#include "py_curses.h"
114
Victor Stinner26486ea2010-05-15 22:23:53 +0000115/* These prototypes are in <term.h>, but including this header
116 #defines many common symbols (such as "lines") which breaks the
117 curses module in other ways. So the code will just specify
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +0000118 explicit prototypes here. */
119extern int setupterm(char *,int,int *);
Fred Drake0368bc42001-07-19 20:48:32 +0000120#ifdef __sgi
121#include <term.h>
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +0000122#endif
123
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100124#ifdef HAVE_LANGINFO_H
125#include <langinfo.h>
126#endif
127
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000128#if !defined(HAVE_NCURSES_H) && (defined(sgi) || defined(__sun) || defined(SCO5))
Andrew M. Kuchlingbf680262000-07-25 12:37:41 +0000129#define STRICT_SYSV_CURSES /* Don't use ncurses extensions */
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +0000130typedef chtype attr_t; /* No attr_t type is available */
Guido van Rossum1266a011996-02-25 04:50:31 +0000131#endif
Guido van Rossumf6971e21994-08-30 12:25:20 +0000132
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000133#if defined(_AIX)
134#define STRICT_SYSV_CURSES
135#endif
136
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000137/* Definition of exception curses.error */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000138
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000139static PyObject *PyCursesError;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000140
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +0000141/* Tells whether setupterm() has been called to initialise terminfo. */
142static int initialised_setupterm = FALSE;
143
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000144/* Tells whether initscr() has been called to initialise curses. */
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000145static int initialised = FALSE;
146
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000147/* Tells whether start_color() has been called to initialise color usage. */
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000148static int initialisedcolors = FALSE;
149
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100150static char *screen_encoding = NULL;
151
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000152/* Utility Macros */
Victor Stinner26486ea2010-05-15 22:23:53 +0000153#define PyCursesSetupTermCalled \
154 if (initialised_setupterm != TRUE) { \
155 PyErr_SetString(PyCursesError, \
156 "must call (at least) setupterm() first"); \
157 return 0; }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +0000158
Victor Stinner26486ea2010-05-15 22:23:53 +0000159#define PyCursesInitialised \
160 if (initialised != TRUE) { \
161 PyErr_SetString(PyCursesError, \
162 "must call initscr() first"); \
163 return 0; }
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000164
Victor Stinner26486ea2010-05-15 22:23:53 +0000165#define PyCursesInitialisedColor \
166 if (initialisedcolors != TRUE) { \
167 PyErr_SetString(PyCursesError, \
168 "must call start_color() first"); \
169 return 0; }
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000170
Andrew M. Kuchling52a14c32005-06-09 17:53:27 +0000171#ifndef MIN
172#define MIN(x,y) ((x) < (y) ? (x) : (y))
173#endif
174
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000175/* Utility Functions */
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000176
Guido van Rossum85738471995-02-17 13:50:17 +0000177/*
Victor Stinner26486ea2010-05-15 22:23:53 +0000178 * Check the return code from a curses function and return None
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000179 * or raise an exception as appropriate. These are exported using the
Victor Stinner26486ea2010-05-15 22:23:53 +0000180 * capsule API.
Guido van Rossum85738471995-02-17 13:50:17 +0000181 */
182
183static PyObject *
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100184PyCursesCheckERR(int code, const char *fname)
Guido van Rossum85738471995-02-17 13:50:17 +0000185{
Victor Stinner26486ea2010-05-15 22:23:53 +0000186 if (code != ERR) {
187 Py_INCREF(Py_None);
188 return Py_None;
Guido van Rossum85738471995-02-17 13:50:17 +0000189 } else {
Victor Stinner26486ea2010-05-15 22:23:53 +0000190 if (fname == NULL) {
191 PyErr_SetString(PyCursesError, catchall_ERR);
192 } else {
193 PyErr_Format(PyCursesError, "%s() returned ERR", fname);
194 }
195 return NULL;
Guido van Rossum85738471995-02-17 13:50:17 +0000196 }
Guido van Rossum85738471995-02-17 13:50:17 +0000197}
198
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100199/* Convert an object to a byte (an integer of type chtype):
200
201 - int
202 - bytes of length 1
203 - str of length 1
204
205 Return 1 on success, 0 on error (invalid type or integer overflow). */
Victor Stinner26486ea2010-05-15 22:23:53 +0000206static int
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100207PyCurses_ConvertToChtype(PyCursesWindowObject *win, PyObject *obj, chtype *ch)
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000208{
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100209 long value;
210 if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) {
211 value = (unsigned char)PyBytes_AsString(obj)[0];
212 }
213 else if (PyUnicode_Check(obj)) {
214 if (PyUnicode_GetLength(obj) != 1) {
215 PyErr_Format(PyExc_TypeError,
216 "expect bytes or str of length 1, or int, "
217 "got a str of length %zi",
218 PyUnicode_GET_LENGTH(obj));
Victor Stinner26486ea2010-05-15 22:23:53 +0000219 return 0;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100220 }
221 value = PyUnicode_READ_CHAR(obj, 0);
222 if (128 < value) {
223 PyObject *bytes;
224 const char *encoding;
225 if (win)
226 encoding = win->encoding;
227 else
228 encoding = screen_encoding;
229 bytes = PyUnicode_AsEncodedObject(obj, encoding, NULL);
230 if (bytes == NULL)
231 return 0;
232 if (PyBytes_GET_SIZE(bytes) == 1)
233 value = (unsigned char)PyBytes_AS_STRING(bytes)[0];
234 else
235 value = -1;
236 Py_DECREF(bytes);
237 if (value < 0)
238 goto overflow;
239 }
240 }
241 else if (PyLong_CheckExact(obj)) {
242 int long_overflow;
243 value = PyLong_AsLongAndOverflow(obj, &long_overflow);
244 if (long_overflow)
245 goto overflow;
246 }
247 else {
248 PyErr_Format(PyExc_TypeError,
249 "expect bytes or str of length 1, or int, got %s",
250 Py_TYPE(obj)->tp_name);
Victor Stinner26486ea2010-05-15 22:23:53 +0000251 return 0;
252 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100253 *ch = (chtype)value;
254 if ((long)*ch != value)
255 goto overflow;
Victor Stinner26486ea2010-05-15 22:23:53 +0000256 return 1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100257
258overflow:
259 PyErr_SetString(PyExc_OverflowError,
260 "byte doesn't fit in chtype");
261 return 0;
262}
263
264/* Convert an object to a byte (chtype) or a character (cchar_t):
265
266 - int
267 - bytes of length 1
268 - str of length 1
269
270 Return:
271
272 - 2 if obj is a character (written into *wch)
273 - 1 if obj is a byte (written into *ch)
274 - 0 on error: raise an exception */
275static int
276PyCurses_ConvertToCchar_t(PyCursesWindowObject *win, PyObject *obj,
277 chtype *ch
278#ifdef HAVE_NCURSESW
279 , cchar_t *wch
280#endif
281 )
282{
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100283 long value;
284#ifdef HAVE_NCURSESW
285 wchar_t buffer[2];
286#endif
287
288 if (PyUnicode_Check(obj)) {
289#ifdef HAVE_NCURSESW
290 if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) {
291 PyErr_Format(PyExc_TypeError,
292 "expect bytes or str of length 1, or int, "
293 "got a str of length %zi",
294 PyUnicode_GET_LENGTH(obj));
295 return 0;
296 }
297 memset(wch->chars, 0, sizeof(wch->chars));
298 wch->chars[0] = buffer[0];
299 return 2;
300#else
301 return PyCurses_ConvertToChtype(win, obj, ch);
302#endif
303 }
304 else if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) {
305 value = (unsigned char)PyBytes_AsString(obj)[0];
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100306 }
307 else if (PyLong_CheckExact(obj)) {
308 int overflow;
309 value = PyLong_AsLongAndOverflow(obj, &overflow);
310 if (overflow) {
311 PyErr_SetString(PyExc_OverflowError,
312 "int doesn't fit in long");
313 return 0;
314 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100315 }
316 else {
317 PyErr_Format(PyExc_TypeError,
318 "expect bytes or str of length 1, or int, got %s",
319 Py_TYPE(obj)->tp_name);
320 return 0;
321 }
Victor Stinner9f16e442012-09-01 15:00:34 +0200322
323 *ch = (chtype)value;
324 if ((long)*ch != value) {
325 PyErr_Format(PyExc_OverflowError,
326 "byte doesn't fit in chtype");
327 return 0;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100328 }
Victor Stinner9f16e442012-09-01 15:00:34 +0200329 return 1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100330}
331
332/* Convert an object to a byte string (char*) or a wide character string
333 (wchar_t*). Return:
334
335 - 2 if obj is a character string (written into *wch)
336 - 1 if obj is a byte string (written into *bytes)
337 - 0 on error: raise an exception */
338static int
339PyCurses_ConvertToString(PyCursesWindowObject *win, PyObject *obj,
340 PyObject **bytes, wchar_t **wstr)
341{
342 if (PyUnicode_Check(obj)) {
343#ifdef HAVE_NCURSESW
344 assert (wstr != NULL);
345 *wstr = PyUnicode_AsWideCharString(obj, NULL);
346 if (*wstr == NULL)
347 return 0;
348 return 2;
349#else
350 assert (wstr == NULL);
351 *bytes = PyUnicode_AsEncodedObject(obj, win->encoding, NULL);
352 if (*bytes == NULL)
353 return 0;
354 return 1;
355#endif
356 }
357 else if (PyBytes_Check(obj)) {
358 Py_INCREF(obj);
359 *bytes = obj;
360 return 1;
361 }
362
363 PyErr_Format(PyExc_TypeError, "expect bytes or str, got %s",
364 Py_TYPE(obj)->tp_name);
365 return 0;
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000366}
367
Benjamin Peterson21896a32010-03-21 22:03:03 +0000368/* Function versions of the 3 functions for testing whether curses has been
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000369 initialised or not. */
Victor Stinner26486ea2010-05-15 22:23:53 +0000370
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000371static int func_PyCursesSetupTermCalled(void)
372{
373 PyCursesSetupTermCalled;
374 return 1;
375}
376
377static int func_PyCursesInitialised(void)
378{
379 PyCursesInitialised;
380 return 1;
381}
382
383static int func_PyCursesInitialisedColor(void)
384{
385 PyCursesInitialisedColor;
386 return 1;
387}
388
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000389/*****************************************************************************
390 The Window Object
391******************************************************************************/
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000392
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000393/* Definition of the window type */
Guido van Rossum85738471995-02-17 13:50:17 +0000394
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000395PyTypeObject PyCursesWindow_Type;
396
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000397/* Function prototype macros for Window object
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000398
399 X - function name
400 TYPE - parameter Type
401 ERGSTR - format string for construction of the return value
402 PARSESTR - format string for argument parsing
Victor Stinner26486ea2010-05-15 22:23:53 +0000403*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000404
Victor Stinner26486ea2010-05-15 22:23:53 +0000405#define Window_NoArgNoReturnFunction(X) \
406 static PyObject *PyCursesWindow_ ## X \
407 (PyCursesWindowObject *self, PyObject *args) \
408 { return PyCursesCheckERR(X(self->win), # X); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000409
Victor Stinner26486ea2010-05-15 22:23:53 +0000410#define Window_NoArgTrueFalseFunction(X) \
411 static PyObject * PyCursesWindow_ ## X \
412 (PyCursesWindowObject *self) \
413 { \
414 if (X (self->win) == FALSE) { Py_INCREF(Py_False); return Py_False; } \
415 else { Py_INCREF(Py_True); return Py_True; } }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000416
Victor Stinner26486ea2010-05-15 22:23:53 +0000417#define Window_NoArgNoReturnVoidFunction(X) \
418 static PyObject * PyCursesWindow_ ## X \
419 (PyCursesWindowObject *self) \
420 { \
421 X(self->win); Py_INCREF(Py_None); return Py_None; }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000422
Victor Stinner26486ea2010-05-15 22:23:53 +0000423#define Window_NoArg2TupleReturnFunction(X, TYPE, ERGSTR) \
424 static PyObject * PyCursesWindow_ ## X \
425 (PyCursesWindowObject *self) \
426 { \
427 TYPE arg1, arg2; \
428 X(self->win,arg1,arg2); return Py_BuildValue(ERGSTR, arg1, arg2); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000429
Victor Stinner26486ea2010-05-15 22:23:53 +0000430#define Window_OneArgNoReturnVoidFunction(X, TYPE, PARSESTR) \
431 static PyObject * PyCursesWindow_ ## X \
432 (PyCursesWindowObject *self, PyObject *args) \
433 { \
434 TYPE arg1; \
435 if (!PyArg_ParseTuple(args, PARSESTR, &arg1)) return NULL; \
436 X(self->win,arg1); Py_INCREF(Py_None); return Py_None; }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000437
Victor Stinner26486ea2010-05-15 22:23:53 +0000438#define Window_OneArgNoReturnFunction(X, TYPE, PARSESTR) \
439 static PyObject * PyCursesWindow_ ## X \
440 (PyCursesWindowObject *self, PyObject *args) \
441 { \
442 TYPE arg1; \
443 if (!PyArg_ParseTuple(args,PARSESTR, &arg1)) return NULL; \
444 return PyCursesCheckERR(X(self->win, arg1), # X); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000445
Victor Stinner26486ea2010-05-15 22:23:53 +0000446#define Window_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \
447 static PyObject * PyCursesWindow_ ## X \
448 (PyCursesWindowObject *self, PyObject *args) \
449 { \
450 TYPE arg1, arg2; \
451 if (!PyArg_ParseTuple(args,PARSESTR, &arg1, &arg2)) return NULL; \
452 return PyCursesCheckERR(X(self->win, arg1, arg2), # X); }
Guido van Rossum85738471995-02-17 13:50:17 +0000453
Guido van Rossumf6971e21994-08-30 12:25:20 +0000454/* ------------- WINDOW routines --------------- */
Guido van Rossum85738471995-02-17 13:50:17 +0000455
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000456Window_NoArgNoReturnFunction(untouchwin)
457Window_NoArgNoReturnFunction(touchwin)
458Window_NoArgNoReturnFunction(redrawwin)
459Window_NoArgNoReturnFunction(winsertln)
460Window_NoArgNoReturnFunction(werase)
461Window_NoArgNoReturnFunction(wdeleteln)
462
463Window_NoArgTrueFalseFunction(is_wintouched)
464
465Window_NoArgNoReturnVoidFunction(wsyncup)
466Window_NoArgNoReturnVoidFunction(wsyncdown)
467Window_NoArgNoReturnVoidFunction(wstandend)
468Window_NoArgNoReturnVoidFunction(wstandout)
469Window_NoArgNoReturnVoidFunction(wcursyncup)
470Window_NoArgNoReturnVoidFunction(wclrtoeol)
471Window_NoArgNoReturnVoidFunction(wclrtobot)
472Window_NoArgNoReturnVoidFunction(wclear)
473
474Window_OneArgNoReturnVoidFunction(idcok, int, "i;True(1) or False(0)")
475Window_OneArgNoReturnVoidFunction(immedok, int, "i;True(1) or False(0)")
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +0000476Window_OneArgNoReturnVoidFunction(wtimeout, int, "i;delay")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000477
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000478Window_NoArg2TupleReturnFunction(getyx, int, "ii")
479Window_NoArg2TupleReturnFunction(getbegyx, int, "ii")
480Window_NoArg2TupleReturnFunction(getmaxyx, int, "ii")
481Window_NoArg2TupleReturnFunction(getparyx, int, "ii")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000482
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000483Window_OneArgNoReturnFunction(clearok, int, "i;True(1) or False(0)")
484Window_OneArgNoReturnFunction(idlok, int, "i;True(1) or False(0)")
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000485#if defined(__NetBSD__)
486Window_OneArgNoReturnVoidFunction(keypad, int, "i;True(1) or False(0)")
487#else
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000488Window_OneArgNoReturnFunction(keypad, int, "i;True(1) or False(0)")
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000489#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000490Window_OneArgNoReturnFunction(leaveok, int, "i;True(1) or False(0)")
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000491#if defined(__NetBSD__)
492Window_OneArgNoReturnVoidFunction(nodelay, int, "i;True(1) or False(0)")
493#else
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000494Window_OneArgNoReturnFunction(nodelay, int, "i;True(1) or False(0)")
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000495#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000496Window_OneArgNoReturnFunction(notimeout, int, "i;True(1) or False(0)")
497Window_OneArgNoReturnFunction(scrollok, int, "i;True(1) or False(0)")
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +0000498Window_OneArgNoReturnFunction(winsdelln, int, "i;nlines")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000499Window_OneArgNoReturnFunction(syncok, int, "i;True(1) or False(0)")
500
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000501Window_TwoArgNoReturnFunction(mvwin, int, "ii;y,x")
502Window_TwoArgNoReturnFunction(mvderwin, int, "ii;y,x")
503Window_TwoArgNoReturnFunction(wmove, int, "ii;y,x")
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +0000504#ifndef STRICT_SYSV_CURSES
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000505Window_TwoArgNoReturnFunction(wresize, int, "ii;lines,columns")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000506#endif
507
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +0000508/* Allocation and deallocation of Window Objects */
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000509
Guido van Rossumf6971e21994-08-30 12:25:20 +0000510static PyObject *
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100511PyCursesWindow_New(WINDOW *win, const char *encoding)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000512{
Victor Stinner26486ea2010-05-15 22:23:53 +0000513 PyCursesWindowObject *wo;
Guido van Rossum85738471995-02-17 13:50:17 +0000514
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100515 if (encoding == NULL) {
516#if defined(MS_WINDOWS)
517 char *buffer[100];
518 UINT cp;
519 cp = GetConsoleOutputCP();
520 if (cp != 0) {
521 PyOS_snprintf(buffer, sizeof(buffer), "cp%u", cp);
522 encoding = buffer;
523 }
524#elif defined(CODESET)
525 const char *codeset = nl_langinfo(CODESET);
526 if (codeset != NULL && codeset[0] != 0)
527 encoding = codeset;
528#endif
529 if (encoding == NULL)
530 encoding = "utf-8";
531 }
532
Victor Stinner26486ea2010-05-15 22:23:53 +0000533 wo = PyObject_NEW(PyCursesWindowObject, &PyCursesWindow_Type);
534 if (wo == NULL) return NULL;
535 wo->win = win;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100536 wo->encoding = strdup(encoding);
537 if (wo->encoding == NULL) {
538 Py_DECREF(wo);
539 PyErr_NoMemory();
540 return NULL;
541 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000542 return (PyObject *)wo;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000543}
544
545static void
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000546PyCursesWindow_Dealloc(PyCursesWindowObject *wo)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000547{
Victor Stinner26486ea2010-05-15 22:23:53 +0000548 if (wo->win != stdscr) delwin(wo->win);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100549 if (wo->encoding != NULL)
550 free(wo->encoding);
Victor Stinner26486ea2010-05-15 22:23:53 +0000551 PyObject_DEL(wo);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000552}
553
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000554/* Addch, Addstr, Addnstr */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000555
556static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000557PyCursesWindow_AddCh(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000558{
Victor Stinner26486ea2010-05-15 22:23:53 +0000559 int rtn, x, y, use_xy = FALSE;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100560 PyObject *chobj;
561 int type;
562 chtype ch;
563#ifdef HAVE_NCURSESW
564 cchar_t wch;
565#endif
Victor Stinner26486ea2010-05-15 22:23:53 +0000566 attr_t attr = A_NORMAL;
567 long lattr;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100568 const char *funcname;
Guido van Rossum85738471995-02-17 13:50:17 +0000569
Victor Stinner26486ea2010-05-15 22:23:53 +0000570 switch (PyTuple_Size(args)) {
571 case 1:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100572 if (!PyArg_ParseTuple(args, "O;ch or int", &chobj))
Victor Stinner26486ea2010-05-15 22:23:53 +0000573 return NULL;
574 break;
575 case 2:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100576 if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &chobj, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +0000577 return NULL;
578 attr = lattr;
579 break;
580 case 3:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100581 if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &chobj))
Victor Stinner26486ea2010-05-15 22:23:53 +0000582 return NULL;
583 use_xy = TRUE;
584 break;
585 case 4:
586 if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr",
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100587 &y, &x, &chobj, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +0000588 return NULL;
589 attr = lattr;
590 use_xy = TRUE;
591 break;
592 default:
593 PyErr_SetString(PyExc_TypeError, "addch requires 1 to 4 arguments");
594 return NULL;
595 }
596
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100597#ifdef HAVE_NCURSESW
598 type = PyCurses_ConvertToCchar_t(self, chobj, &ch, &wch);
599 if (type == 2) {
600 funcname = "add_wch";
601 wch.attr = attr;
602 if (use_xy == TRUE)
603 rtn = mvwadd_wch(self->win,y,x, &wch);
604 else {
605 rtn = wadd_wch(self->win, &wch);
606 }
607 }
608 else
609#else
610 type = PyCurses_ConvertToCchar_t(self, chobj, &ch);
611#endif
612 if (type == 1) {
613 funcname = "addch";
614 if (use_xy == TRUE)
615 rtn = mvwaddch(self->win,y,x, ch | attr);
616 else {
617 rtn = waddch(self->win, ch | attr);
618 }
619 }
620 else {
Victor Stinner26486ea2010-05-15 22:23:53 +0000621 return NULL;
622 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100623 return PyCursesCheckERR(rtn, funcname);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000624}
625
626static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000627PyCursesWindow_AddStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000628{
Victor Stinner26486ea2010-05-15 22:23:53 +0000629 int rtn;
630 int x, y;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100631 int strtype;
Benjamin Peterson5e545192012-03-14 18:21:35 -0500632 PyObject *strobj, *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100633#ifdef HAVE_NCURSESW
634 wchar_t *wstr = NULL;
635#endif
Victor Stinner26486ea2010-05-15 22:23:53 +0000636 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
637 long lattr;
638 int use_xy = FALSE, use_attr = FALSE;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100639 const char *funcname;
Guido van Rossum85738471995-02-17 13:50:17 +0000640
Victor Stinner26486ea2010-05-15 22:23:53 +0000641 switch (PyTuple_Size(args)) {
642 case 1:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100643 if (!PyArg_ParseTuple(args,"O;str", &strobj))
Victor Stinner26486ea2010-05-15 22:23:53 +0000644 return NULL;
645 break;
646 case 2:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100647 if (!PyArg_ParseTuple(args,"Ol;str,attr", &strobj, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +0000648 return NULL;
649 attr = lattr;
650 use_attr = TRUE;
651 break;
652 case 3:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100653 if (!PyArg_ParseTuple(args,"iiO;int,int,str", &y, &x, &strobj))
Victor Stinner26486ea2010-05-15 22:23:53 +0000654 return NULL;
655 use_xy = TRUE;
656 break;
657 case 4:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100658 if (!PyArg_ParseTuple(args,"iiOl;int,int,str,attr", &y, &x, &strobj, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +0000659 return NULL;
660 attr = lattr;
661 use_xy = use_attr = TRUE;
662 break;
663 default:
664 PyErr_SetString(PyExc_TypeError, "addstr requires 1 to 4 arguments");
665 return NULL;
666 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100667#ifdef HAVE_NCURSESW
668 strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, &wstr);
669#else
670 strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, NULL);
671#endif
672 if (strtype == 0)
673 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +0000674 if (use_attr == TRUE) {
675 attr_old = getattrs(self->win);
676 (void)wattrset(self->win,attr);
677 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100678#ifdef HAVE_NCURSESW
679 if (strtype == 2) {
680 funcname = "addwstr";
681 if (use_xy == TRUE)
682 rtn = mvwaddwstr(self->win,y,x,wstr);
683 else
684 rtn = waddwstr(self->win,wstr);
685 PyMem_Free(wstr);
686 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000687 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100688#endif
689 {
690 char *str = PyBytes_AS_STRING(bytesobj);
691 funcname = "addstr";
692 if (use_xy == TRUE)
693 rtn = mvwaddstr(self->win,y,x,str);
694 else
695 rtn = waddstr(self->win,str);
696 Py_DECREF(bytesobj);
697 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000698 if (use_attr == TRUE)
699 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100700 return PyCursesCheckERR(rtn, funcname);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000701}
Guido van Rossum85738471995-02-17 13:50:17 +0000702
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000703static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000704PyCursesWindow_AddNStr(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000705{
Victor Stinner26486ea2010-05-15 22:23:53 +0000706 int rtn, x, y, n;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100707 int strtype;
Benjamin Peterson5e545192012-03-14 18:21:35 -0500708 PyObject *strobj, *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100709#ifdef HAVE_NCURSESW
710 wchar_t *wstr = NULL;
711#endif
Victor Stinner26486ea2010-05-15 22:23:53 +0000712 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
713 long lattr;
714 int use_xy = FALSE, use_attr = FALSE;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100715 const char *funcname;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000716
Victor Stinner26486ea2010-05-15 22:23:53 +0000717 switch (PyTuple_Size(args)) {
718 case 2:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100719 if (!PyArg_ParseTuple(args,"Oi;str,n", &strobj, &n))
Victor Stinner26486ea2010-05-15 22:23:53 +0000720 return NULL;
721 break;
722 case 3:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100723 if (!PyArg_ParseTuple(args,"Oil;str,n,attr", &strobj, &n, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +0000724 return NULL;
725 attr = lattr;
726 use_attr = TRUE;
727 break;
728 case 4:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100729 if (!PyArg_ParseTuple(args,"iiOi;y,x,str,n", &y, &x, &strobj, &n))
Victor Stinner26486ea2010-05-15 22:23:53 +0000730 return NULL;
731 use_xy = TRUE;
732 break;
733 case 5:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100734 if (!PyArg_ParseTuple(args,"iiOil;y,x,str,n,attr", &y, &x, &strobj, &n, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +0000735 return NULL;
736 attr = lattr;
737 use_xy = use_attr = TRUE;
738 break;
739 default:
740 PyErr_SetString(PyExc_TypeError, "addnstr requires 2 to 5 arguments");
741 return NULL;
742 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100743#ifdef HAVE_NCURSESW
744 strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, &wstr);
745#else
746 strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, NULL);
747#endif
748 if (strtype == 0)
749 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000750
Victor Stinner26486ea2010-05-15 22:23:53 +0000751 if (use_attr == TRUE) {
752 attr_old = getattrs(self->win);
753 (void)wattrset(self->win,attr);
754 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100755#ifdef HAVE_NCURSESW
756 if (strtype == 2) {
757 funcname = "addnwstr";
758 if (use_xy == TRUE)
759 rtn = mvwaddnwstr(self->win,y,x,wstr,n);
760 else
761 rtn = waddnwstr(self->win,wstr,n);
762 PyMem_Free(wstr);
763 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000764 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100765#endif
766 {
767 char *str = PyBytes_AS_STRING(bytesobj);
768 funcname = "addnstr";
769 if (use_xy == TRUE)
770 rtn = mvwaddnstr(self->win,y,x,str,n);
771 else
772 rtn = waddnstr(self->win,str,n);
773 Py_DECREF(bytesobj);
774 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000775 if (use_attr == TRUE)
776 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100777 return PyCursesCheckERR(rtn, funcname);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000778}
779
780static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000781PyCursesWindow_Bkgd(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000782{
Victor Stinner26486ea2010-05-15 22:23:53 +0000783 PyObject *temp;
784 chtype bkgd;
785 attr_t attr = A_NORMAL;
786 long lattr;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000787
Victor Stinner26486ea2010-05-15 22:23:53 +0000788 switch (PyTuple_Size(args)) {
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000789 case 1:
Victor Stinner26486ea2010-05-15 22:23:53 +0000790 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
791 return NULL;
792 break;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000793 case 2:
Victor Stinner26486ea2010-05-15 22:23:53 +0000794 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr))
795 return NULL;
796 attr = lattr;
797 break;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000798 default:
Victor Stinner26486ea2010-05-15 22:23:53 +0000799 PyErr_SetString(PyExc_TypeError, "bkgd requires 1 or 2 arguments");
800 return NULL;
801 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000802
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100803 if (!PyCurses_ConvertToChtype(self, temp, &bkgd))
Victor Stinner26486ea2010-05-15 22:23:53 +0000804 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000805
Victor Stinner26486ea2010-05-15 22:23:53 +0000806 return PyCursesCheckERR(wbkgd(self->win, bkgd | attr), "bkgd");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000807}
808
809static PyObject *
Christian Heimes2380ac72008-01-09 00:17:24 +0000810PyCursesWindow_AttrOff(PyCursesWindowObject *self, PyObject *args)
811{
Victor Stinner26486ea2010-05-15 22:23:53 +0000812 long lattr;
813 if (!PyArg_ParseTuple(args,"l;attr", &lattr))
814 return NULL;
815 return PyCursesCheckERR(wattroff(self->win, (attr_t)lattr), "attroff");
Christian Heimes2380ac72008-01-09 00:17:24 +0000816}
817
818static PyObject *
819PyCursesWindow_AttrOn(PyCursesWindowObject *self, PyObject *args)
820{
Victor Stinner26486ea2010-05-15 22:23:53 +0000821 long lattr;
822 if (!PyArg_ParseTuple(args,"l;attr", &lattr))
823 return NULL;
824 return PyCursesCheckERR(wattron(self->win, (attr_t)lattr), "attron");
Christian Heimes2380ac72008-01-09 00:17:24 +0000825}
826
827static PyObject *
828PyCursesWindow_AttrSet(PyCursesWindowObject *self, PyObject *args)
829{
Victor Stinner26486ea2010-05-15 22:23:53 +0000830 long lattr;
831 if (!PyArg_ParseTuple(args,"l;attr", &lattr))
832 return NULL;
833 return PyCursesCheckERR(wattrset(self->win, (attr_t)lattr), "attrset");
Christian Heimes2380ac72008-01-09 00:17:24 +0000834}
835
836static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000837PyCursesWindow_BkgdSet(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000838{
Victor Stinner26486ea2010-05-15 22:23:53 +0000839 PyObject *temp;
840 chtype bkgd;
841 attr_t attr = A_NORMAL;
842 long lattr;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000843
Victor Stinner26486ea2010-05-15 22:23:53 +0000844 switch (PyTuple_Size(args)) {
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000845 case 1:
Victor Stinner26486ea2010-05-15 22:23:53 +0000846 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
847 return NULL;
848 break;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000849 case 2:
Victor Stinner26486ea2010-05-15 22:23:53 +0000850 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr))
851 return NULL;
852 attr = lattr;
853 break;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000854 default:
Victor Stinner26486ea2010-05-15 22:23:53 +0000855 PyErr_SetString(PyExc_TypeError, "bkgdset requires 1 or 2 arguments");
856 return NULL;
857 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000858
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100859 if (!PyCurses_ConvertToChtype(self, temp, &bkgd))
Victor Stinner26486ea2010-05-15 22:23:53 +0000860 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000861
Victor Stinner26486ea2010-05-15 22:23:53 +0000862 wbkgdset(self->win, bkgd | attr);
863 return PyCursesCheckERR(0, "bkgdset");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000864}
865
866static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000867PyCursesWindow_Border(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000868{
Victor Stinner26486ea2010-05-15 22:23:53 +0000869 PyObject *temp[8];
870 chtype ch[8];
871 int i;
Andrew M. Kuchling16e65a02001-10-20 16:05:52 +0000872
Victor Stinner26486ea2010-05-15 22:23:53 +0000873 /* Clear the array of parameters */
874 for(i=0; i<8; i++) {
875 temp[i] = NULL;
876 ch[i] = 0;
877 }
Andrew M. Kuchling16e65a02001-10-20 16:05:52 +0000878
Victor Stinner26486ea2010-05-15 22:23:53 +0000879 if (!PyArg_ParseTuple(args,"|OOOOOOOO;ls,rs,ts,bs,tl,tr,bl,br",
880 &temp[0], &temp[1], &temp[2], &temp[3],
881 &temp[4], &temp[5], &temp[6], &temp[7]))
882 return NULL;
883
884 for(i=0; i<8; i++) {
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100885 if (temp[i] != NULL && !PyCurses_ConvertToChtype(self, temp[i], &ch[i]))
Victor Stinner26486ea2010-05-15 22:23:53 +0000886 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +0000887 }
888
889 wborder(self->win,
890 ch[0], ch[1], ch[2], ch[3],
891 ch[4], ch[5], ch[6], ch[7]);
892 Py_INCREF(Py_None);
893 return Py_None;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000894}
895
896static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000897PyCursesWindow_Box(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000898{
Victor Stinner26486ea2010-05-15 22:23:53 +0000899 chtype ch1=0,ch2=0;
900 switch(PyTuple_Size(args)){
901 case 0: break;
902 default:
903 if (!PyArg_ParseTuple(args,"ll;vertint,horint", &ch1, &ch2))
904 return NULL;
905 }
906 box(self->win,ch1,ch2);
907 Py_INCREF(Py_None);
908 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000909}
910
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000911#if defined(HAVE_NCURSES_H) || defined(MVWDELCH_IS_EXPRESSION)
912#define py_mvwdelch mvwdelch
913#else
914int py_mvwdelch(WINDOW *w, int y, int x)
915{
Victor Stinner26486ea2010-05-15 22:23:53 +0000916 mvwdelch(w,y,x);
917 /* On HP/UX, mvwdelch already returns. On other systems,
918 we may well run into this return statement. */
919 return 0;
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000920}
921#endif
922
Guido van Rossumd8faa362007-04-27 19:54:29 +0000923/* chgat, added by Fabian Kreutz <fabian.kreutz at gmx.net> */
924
925static PyObject *
926PyCursesWindow_ChgAt(PyCursesWindowObject *self, PyObject *args)
927{
Victor Stinner26486ea2010-05-15 22:23:53 +0000928 int rtn;
929 int x, y;
930 int num = -1;
931 short color;
932 attr_t attr = A_NORMAL;
933 long lattr;
934 int use_xy = FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000935
Victor Stinner26486ea2010-05-15 22:23:53 +0000936 switch (PyTuple_Size(args)) {
937 case 1:
938 if (!PyArg_ParseTuple(args,"l;attr", &lattr))
939 return NULL;
940 attr = lattr;
941 break;
942 case 2:
943 if (!PyArg_ParseTuple(args,"il;n,attr", &num, &lattr))
944 return NULL;
945 attr = lattr;
946 break;
947 case 3:
948 if (!PyArg_ParseTuple(args,"iil;int,int,attr", &y, &x, &lattr))
949 return NULL;
950 attr = lattr;
951 use_xy = TRUE;
952 break;
953 case 4:
954 if (!PyArg_ParseTuple(args,"iiil;int,int,n,attr", &y, &x, &num, &lattr))
955 return NULL;
956 attr = lattr;
957 use_xy = TRUE;
958 break;
959 default:
960 PyErr_SetString(PyExc_TypeError, "chgat requires 1 to 4 arguments");
961 return NULL;
962 }
Guido van Rossumd8faa362007-04-27 19:54:29 +0000963
Victor Stinner26486ea2010-05-15 22:23:53 +0000964 color = (short)((attr >> 8) & 0xff);
965 attr = attr - (color << 8);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000966
Victor Stinner26486ea2010-05-15 22:23:53 +0000967 if (use_xy == TRUE) {
968 rtn = mvwchgat(self->win,y,x,num,attr,color,NULL);
969 touchline(self->win,y,1);
970 } else {
971 getyx(self->win,y,x);
972 rtn = wchgat(self->win,num,attr,color,NULL);
973 touchline(self->win,y,1);
974 }
975 return PyCursesCheckERR(rtn, "chgat");
Guido van Rossumd8faa362007-04-27 19:54:29 +0000976}
977
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000978
Guido van Rossumf6971e21994-08-30 12:25:20 +0000979static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000980PyCursesWindow_DelCh(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000981{
Victor Stinner26486ea2010-05-15 22:23:53 +0000982 int rtn;
983 int x, y;
Guido van Rossum85738471995-02-17 13:50:17 +0000984
Victor Stinner26486ea2010-05-15 22:23:53 +0000985 switch (PyTuple_Size(args)) {
986 case 0:
987 rtn = wdelch(self->win);
988 break;
989 case 2:
990 if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x))
991 return NULL;
992 rtn = py_mvwdelch(self->win,y,x);
993 break;
994 default:
995 PyErr_SetString(PyExc_TypeError, "delch requires 0 or 2 arguments");
996 return NULL;
997 }
998 return PyCursesCheckERR(rtn, "[mv]wdelch");
Guido van Rossumf6971e21994-08-30 12:25:20 +0000999}
1000
1001static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001002PyCursesWindow_DerWin(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001003{
Victor Stinner26486ea2010-05-15 22:23:53 +00001004 WINDOW *win;
1005 int nlines, ncols, begin_y, begin_x;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001006
Victor Stinner26486ea2010-05-15 22:23:53 +00001007 nlines = 0;
1008 ncols = 0;
1009 switch (PyTuple_Size(args)) {
1010 case 2:
1011 if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
1012 return NULL;
1013 break;
1014 case 4:
1015 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
1016 &nlines,&ncols,&begin_y,&begin_x))
1017 return NULL;
1018 break;
1019 default:
1020 PyErr_SetString(PyExc_TypeError, "derwin requires 2 or 4 arguments");
1021 return NULL;
1022 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001023
Victor Stinner26486ea2010-05-15 22:23:53 +00001024 win = derwin(self->win,nlines,ncols,begin_y,begin_x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001025
Victor Stinner26486ea2010-05-15 22:23:53 +00001026 if (win == NULL) {
1027 PyErr_SetString(PyCursesError, catchall_NULL);
1028 return NULL;
1029 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001030
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001031 return (PyObject *)PyCursesWindow_New(win, NULL);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001032}
1033
1034static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001035PyCursesWindow_EchoChar(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001036{
Victor Stinner26486ea2010-05-15 22:23:53 +00001037 PyObject *temp;
1038 chtype ch;
1039 attr_t attr = A_NORMAL;
1040 long lattr;
Guido van Rossum85738471995-02-17 13:50:17 +00001041
Victor Stinner26486ea2010-05-15 22:23:53 +00001042 switch (PyTuple_Size(args)) {
1043 case 1:
1044 if (!PyArg_ParseTuple(args,"O;ch or int", &temp))
1045 return NULL;
1046 break;
1047 case 2:
1048 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr))
1049 return NULL;
1050 attr = lattr;
1051 break;
1052 default:
1053 PyErr_SetString(PyExc_TypeError, "echochar requires 1 or 2 arguments");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001054
1055
Victor Stinner26486ea2010-05-15 22:23:53 +00001056 return NULL;
1057 }
Guido van Rossum85738471995-02-17 13:50:17 +00001058
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001059 if (!PyCurses_ConvertToChtype(self, temp, &ch))
Victor Stinner26486ea2010-05-15 22:23:53 +00001060 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00001061
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001062#ifdef WINDOW_HAS_FLAGS
Victor Stinner26486ea2010-05-15 22:23:53 +00001063 if (self->win->_flags & _ISPAD)
1064 return PyCursesCheckERR(pechochar(self->win, ch | attr),
1065 "echochar");
1066 else
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001067#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001068 return PyCursesCheckERR(wechochar(self->win, ch | attr),
1069 "echochar");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001070}
1071
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001072#ifdef NCURSES_MOUSE_VERSION
1073static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001074PyCursesWindow_Enclose(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001075{
Victor Stinner26486ea2010-05-15 22:23:53 +00001076 int x, y;
1077 if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x))
1078 return NULL;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001079
Victor Stinner26486ea2010-05-15 22:23:53 +00001080 return PyLong_FromLong( wenclose(self->win,y,x) );
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001081}
1082#endif
1083
Guido van Rossumf6971e21994-08-30 12:25:20 +00001084static PyObject *
Martin v. Löwisc0e16712002-01-17 23:08:27 +00001085PyCursesWindow_GetBkgd(PyCursesWindowObject *self)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001086{
Victor Stinner26486ea2010-05-15 22:23:53 +00001087 return PyLong_FromLong((long) getbkgd(self->win));
Guido van Rossumf6971e21994-08-30 12:25:20 +00001088}
1089
1090static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001091PyCursesWindow_GetCh(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001092{
Victor Stinner26486ea2010-05-15 22:23:53 +00001093 int x, y;
1094 int rtn;
Guido van Rossum85738471995-02-17 13:50:17 +00001095
Victor Stinner26486ea2010-05-15 22:23:53 +00001096 switch (PyTuple_Size(args)) {
1097 case 0:
1098 Py_BEGIN_ALLOW_THREADS
1099 rtn = wgetch(self->win);
1100 Py_END_ALLOW_THREADS
1101 break;
1102 case 2:
1103 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1104 return NULL;
1105 Py_BEGIN_ALLOW_THREADS
1106 rtn = mvwgetch(self->win,y,x);
1107 Py_END_ALLOW_THREADS
1108 break;
1109 default:
1110 PyErr_SetString(PyExc_TypeError, "getch requires 0 or 2 arguments");
1111 return NULL;
1112 }
1113 return PyLong_FromLong((long)rtn);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001114}
Guido van Rossum85738471995-02-17 13:50:17 +00001115
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001116static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001117PyCursesWindow_GetKey(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001118{
Victor Stinner26486ea2010-05-15 22:23:53 +00001119 int x, y;
1120 int rtn;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001121
Victor Stinner26486ea2010-05-15 22:23:53 +00001122 switch (PyTuple_Size(args)) {
1123 case 0:
1124 Py_BEGIN_ALLOW_THREADS
1125 rtn = wgetch(self->win);
1126 Py_END_ALLOW_THREADS
1127 break;
1128 case 2:
1129 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1130 return NULL;
1131 Py_BEGIN_ALLOW_THREADS
1132 rtn = mvwgetch(self->win,y,x);
1133 Py_END_ALLOW_THREADS
1134 break;
1135 default:
1136 PyErr_SetString(PyExc_TypeError, "getkey requires 0 or 2 arguments");
1137 return NULL;
1138 }
1139 if (rtn == ERR) {
1140 /* getch() returns ERR in nodelay mode */
1141 PyErr_SetString(PyCursesError, "no input");
1142 return NULL;
1143 } else if (rtn<=255) {
1144 return Py_BuildValue("C", rtn);
1145 } else {
1146 const char *knp;
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001147#if defined(__NetBSD__)
Victor Stinner26486ea2010-05-15 22:23:53 +00001148 knp = unctrl(rtn);
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001149#else
Victor Stinner26486ea2010-05-15 22:23:53 +00001150 knp = keyname(rtn);
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001151#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001152 return PyUnicode_FromString((knp == NULL) ? "" : knp);
1153 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001154}
1155
Nadeem Vawda9e2e9902011-07-31 15:01:11 +02001156#ifdef HAVE_NCURSESW
Guido van Rossumf6971e21994-08-30 12:25:20 +00001157static PyObject *
Victor Stinnera7878b72011-07-14 23:07:44 +02001158PyCursesWindow_Get_WCh(PyCursesWindowObject *self, PyObject *args)
1159{
1160 int x, y;
1161 int ct;
1162 wint_t rtn;
1163
1164 switch (PyTuple_Size(args)) {
1165 case 0:
1166 Py_BEGIN_ALLOW_THREADS
1167 ct = wget_wch(self->win,&rtn);
1168 Py_END_ALLOW_THREADS
1169 break;
1170 case 2:
1171 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1172 return NULL;
1173 Py_BEGIN_ALLOW_THREADS
1174 ct = mvwget_wch(self->win,y,x,&rtn);
1175 Py_END_ALLOW_THREADS
1176 break;
1177 default:
1178 PyErr_SetString(PyExc_TypeError, "get_wch requires 0 or 2 arguments");
1179 return NULL;
1180 }
1181 if (ct == ERR) {
1182 /* get_wch() returns ERR in nodelay mode */
1183 PyErr_SetString(PyCursesError, "no input");
1184 return NULL;
1185 }
Victor Stinner1d39cde2012-08-29 01:40:57 +02001186 if (ct == KEY_CODE_YES)
1187 return PyLong_FromLong(rtn);
1188 else
1189 return PyUnicode_FromOrdinal(rtn);
Victor Stinnera7878b72011-07-14 23:07:44 +02001190}
Nadeem Vawda9e2e9902011-07-31 15:01:11 +02001191#endif
Victor Stinnera7878b72011-07-14 23:07:44 +02001192
1193static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001194PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001195{
Victor Stinner26486ea2010-05-15 22:23:53 +00001196 int x, y, n;
1197 char rtn[1024]; /* This should be big enough.. I hope */
1198 int rtn2;
Guido van Rossum85738471995-02-17 13:50:17 +00001199
Victor Stinner26486ea2010-05-15 22:23:53 +00001200 switch (PyTuple_Size(args)) {
1201 case 0:
1202 Py_BEGIN_ALLOW_THREADS
1203 rtn2 = wgetnstr(self->win,rtn, 1023);
1204 Py_END_ALLOW_THREADS
1205 break;
1206 case 1:
1207 if (!PyArg_ParseTuple(args,"i;n", &n))
1208 return NULL;
1209 Py_BEGIN_ALLOW_THREADS
1210 rtn2 = wgetnstr(self->win,rtn,MIN(n, 1023));
1211 Py_END_ALLOW_THREADS
1212 break;
1213 case 2:
1214 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1215 return NULL;
1216 Py_BEGIN_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001217#ifdef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00001218 rtn2 = wmove(self->win,y,x)==ERR ? ERR : wgetnstr(self->win, rtn, 1023);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001219#else
Victor Stinner26486ea2010-05-15 22:23:53 +00001220 rtn2 = mvwgetnstr(self->win,y,x,rtn, 1023);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001221#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001222 Py_END_ALLOW_THREADS
1223 break;
1224 case 3:
1225 if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
1226 return NULL;
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +00001227#ifdef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00001228 Py_BEGIN_ALLOW_THREADS
1229 rtn2 = wmove(self->win,y,x)==ERR ? ERR :
1230 wgetnstr(self->win, rtn, MIN(n, 1023));
1231 Py_END_ALLOW_THREADS
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001232#else
Victor Stinner26486ea2010-05-15 22:23:53 +00001233 Py_BEGIN_ALLOW_THREADS
1234 rtn2 = mvwgetnstr(self->win, y, x, rtn, MIN(n, 1023));
1235 Py_END_ALLOW_THREADS
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001236#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001237 break;
1238 default:
1239 PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 3 arguments");
1240 return NULL;
1241 }
1242 if (rtn2 == ERR)
1243 rtn[0] = 0;
1244 return PyBytes_FromString(rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001245}
1246
1247static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001248PyCursesWindow_Hline(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001249{
Victor Stinner26486ea2010-05-15 22:23:53 +00001250 PyObject *temp;
1251 chtype ch;
1252 int n, x, y, code = OK;
1253 attr_t attr = A_NORMAL;
1254 long lattr;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001255
Victor Stinner26486ea2010-05-15 22:23:53 +00001256 switch (PyTuple_Size(args)) {
1257 case 2:
1258 if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
1259 return NULL;
1260 break;
1261 case 3:
1262 if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &lattr))
1263 return NULL;
1264 attr = lattr;
1265 break;
1266 case 4:
1267 if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
1268 return NULL;
1269 code = wmove(self->win, y, x);
1270 break;
1271 case 5:
1272 if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
1273 &y, &x, &temp, &n, &lattr))
1274 return NULL;
1275 attr = lattr;
1276 code = wmove(self->win, y, x);
1277 break;
1278 default:
1279 PyErr_SetString(PyExc_TypeError, "hline requires 2 to 5 arguments");
1280 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001281 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001282
1283 if (code != ERR) {
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001284 if (!PyCurses_ConvertToChtype(self, temp, &ch))
Victor Stinner26486ea2010-05-15 22:23:53 +00001285 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00001286 return PyCursesCheckERR(whline(self->win, ch | attr, n), "hline");
1287 } else
1288 return PyCursesCheckERR(code, "wmove");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001289}
1290
1291static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001292PyCursesWindow_InsCh(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001293{
Victor Stinner26486ea2010-05-15 22:23:53 +00001294 int rtn, x, y, use_xy = FALSE;
1295 PyObject *temp;
1296 chtype ch = 0;
1297 attr_t attr = A_NORMAL;
1298 long lattr;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001299
Victor Stinner26486ea2010-05-15 22:23:53 +00001300 switch (PyTuple_Size(args)) {
1301 case 1:
1302 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
1303 return NULL;
1304 break;
1305 case 2:
1306 if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &temp, &lattr))
1307 return NULL;
1308 attr = lattr;
1309 break;
1310 case 3:
1311 if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &temp))
1312 return NULL;
1313 use_xy = TRUE;
1314 break;
1315 case 4:
1316 if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr", &y, &x, &temp, &lattr))
1317 return NULL;
1318 attr = lattr;
1319 use_xy = TRUE;
1320 break;
1321 default:
1322 PyErr_SetString(PyExc_TypeError, "insch requires 1 or 4 arguments");
1323 return NULL;
1324 }
1325
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001326 if (!PyCurses_ConvertToChtype(self, temp, &ch))
Victor Stinner26486ea2010-05-15 22:23:53 +00001327 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00001328
1329 if (use_xy == TRUE)
1330 rtn = mvwinsch(self->win,y,x, ch | attr);
1331 else {
1332 rtn = winsch(self->win, ch | attr);
1333 }
1334 return PyCursesCheckERR(rtn, "insch");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001335}
1336
1337static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001338PyCursesWindow_InCh(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001339{
Victor Stinner26486ea2010-05-15 22:23:53 +00001340 int x, y, rtn;
Guido van Rossum85738471995-02-17 13:50:17 +00001341
Victor Stinner26486ea2010-05-15 22:23:53 +00001342 switch (PyTuple_Size(args)) {
1343 case 0:
1344 rtn = winch(self->win);
1345 break;
1346 case 2:
1347 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1348 return NULL;
1349 rtn = mvwinch(self->win,y,x);
1350 break;
1351 default:
1352 PyErr_SetString(PyExc_TypeError, "inch requires 0 or 2 arguments");
1353 return NULL;
1354 }
1355 return PyLong_FromLong((long) rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001356}
1357
1358static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001359PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001360{
Victor Stinner26486ea2010-05-15 22:23:53 +00001361 int x, y, n;
1362 char rtn[1024]; /* This should be big enough.. I hope */
1363 int rtn2;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001364
Victor Stinner26486ea2010-05-15 22:23:53 +00001365 switch (PyTuple_Size(args)) {
1366 case 0:
1367 rtn2 = winnstr(self->win,rtn, 1023);
1368 break;
1369 case 1:
1370 if (!PyArg_ParseTuple(args,"i;n", &n))
1371 return NULL;
1372 rtn2 = winnstr(self->win,rtn,MIN(n,1023));
1373 break;
1374 case 2:
1375 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1376 return NULL;
1377 rtn2 = mvwinnstr(self->win,y,x,rtn,1023);
1378 break;
1379 case 3:
1380 if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
1381 return NULL;
1382 rtn2 = mvwinnstr(self->win, y, x, rtn, MIN(n,1023));
1383 break;
1384 default:
1385 PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments");
1386 return NULL;
1387 }
1388 if (rtn2 == ERR)
1389 rtn[0] = 0;
1390 return PyBytes_FromString(rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001391}
1392
1393static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001394PyCursesWindow_InsStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001395{
Victor Stinner26486ea2010-05-15 22:23:53 +00001396 int rtn;
1397 int x, y;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001398 int strtype;
Benjamin Peterson5e545192012-03-14 18:21:35 -05001399 PyObject *strobj, *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001400#ifdef HAVE_NCURSESW
1401 wchar_t *wstr = NULL;
1402#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001403 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
1404 long lattr;
1405 int use_xy = FALSE, use_attr = FALSE;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001406 const char *funcname;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001407
Victor Stinner26486ea2010-05-15 22:23:53 +00001408 switch (PyTuple_Size(args)) {
1409 case 1:
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001410 if (!PyArg_ParseTuple(args,"O;str", &strobj))
Victor Stinner26486ea2010-05-15 22:23:53 +00001411 return NULL;
1412 break;
1413 case 2:
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001414 if (!PyArg_ParseTuple(args,"Ol;str,attr", &strobj, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +00001415 return NULL;
1416 attr = lattr;
1417 use_attr = TRUE;
1418 break;
1419 case 3:
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001420 if (!PyArg_ParseTuple(args,"iiO;y,x,str", &y, &x, &strobj))
Victor Stinner26486ea2010-05-15 22:23:53 +00001421 return NULL;
1422 use_xy = TRUE;
1423 break;
1424 case 4:
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001425 if (!PyArg_ParseTuple(args,"iiOl;y,x,str,attr", &y, &x, &strobj, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +00001426 return NULL;
1427 attr = lattr;
1428 use_xy = use_attr = TRUE;
1429 break;
1430 default:
1431 PyErr_SetString(PyExc_TypeError, "insstr requires 1 to 4 arguments");
1432 return NULL;
1433 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001434
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001435#ifdef HAVE_NCURSESW
1436 strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, &wstr);
1437#else
1438 strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, NULL);
1439#endif
1440 if (strtype == 0)
1441 return NULL;
1442
Victor Stinner26486ea2010-05-15 22:23:53 +00001443 if (use_attr == TRUE) {
1444 attr_old = getattrs(self->win);
1445 (void)wattrset(self->win,attr);
1446 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001447#ifdef HAVE_NCURSESW
1448 if (strtype == 2) {
1449 funcname = "inswstr";
1450 if (use_xy == TRUE)
1451 rtn = mvwins_wstr(self->win,y,x,wstr);
1452 else
1453 rtn = wins_wstr(self->win,wstr);
1454 PyMem_Free(wstr);
1455 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001456 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001457#endif
1458 {
1459 char *str = PyBytes_AS_STRING(bytesobj);
1460 funcname = "insstr";
1461 if (use_xy == TRUE)
1462 rtn = mvwinsstr(self->win,y,x,str);
1463 else
1464 rtn = winsstr(self->win,str);
1465 Py_DECREF(bytesobj);
1466 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001467 if (use_attr == TRUE)
1468 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001469 return PyCursesCheckERR(rtn, funcname);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001470}
1471
1472static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001473PyCursesWindow_InsNStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001474{
Victor Stinner26486ea2010-05-15 22:23:53 +00001475 int rtn, x, y, n;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001476 int strtype;
Benjamin Peterson5e545192012-03-14 18:21:35 -05001477 PyObject *strobj, *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001478#ifdef HAVE_NCURSESW
1479 wchar_t *wstr = NULL;
1480#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001481 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
1482 long lattr;
1483 int use_xy = FALSE, use_attr = FALSE;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001484 const char *funcname;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001485
Victor Stinner26486ea2010-05-15 22:23:53 +00001486 switch (PyTuple_Size(args)) {
1487 case 2:
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001488 if (!PyArg_ParseTuple(args,"Oi;str,n", &strobj, &n))
Victor Stinner26486ea2010-05-15 22:23:53 +00001489 return NULL;
1490 break;
1491 case 3:
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001492 if (!PyArg_ParseTuple(args,"Oil;str,n,attr", &strobj, &n, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +00001493 return NULL;
1494 attr = lattr;
1495 use_attr = TRUE;
1496 break;
1497 case 4:
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001498 if (!PyArg_ParseTuple(args,"iiOi;y,x,str,n", &y, &x, &strobj, &n))
Victor Stinner26486ea2010-05-15 22:23:53 +00001499 return NULL;
1500 use_xy = TRUE;
1501 break;
1502 case 5:
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001503 if (!PyArg_ParseTuple(args,"iiOil;y,x,str,n,attr", &y, &x, &strobj, &n, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +00001504 return NULL;
1505 attr = lattr;
1506 use_xy = use_attr = TRUE;
1507 break;
1508 default:
1509 PyErr_SetString(PyExc_TypeError, "insnstr requires 2 to 5 arguments");
1510 return NULL;
1511 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001512
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001513#ifdef HAVE_NCURSESW
1514 strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, &wstr);
1515#else
1516 strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, NULL);
1517#endif
1518 if (strtype == 0)
1519 return NULL;
1520
Victor Stinner26486ea2010-05-15 22:23:53 +00001521 if (use_attr == TRUE) {
1522 attr_old = getattrs(self->win);
1523 (void)wattrset(self->win,attr);
1524 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001525#ifdef HAVE_NCURSESW
1526 if (strtype == 2) {
1527 funcname = "insn_wstr";
1528 if (use_xy == TRUE)
1529 rtn = mvwins_nwstr(self->win,y,x,wstr,n);
1530 else
1531 rtn = wins_nwstr(self->win,wstr,n);
1532 PyMem_Free(wstr);
1533 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001534 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001535#endif
1536 {
1537 char *str = PyBytes_AS_STRING(bytesobj);
1538 funcname = "insnstr";
1539 if (use_xy == TRUE)
1540 rtn = mvwinsnstr(self->win,y,x,str,n);
1541 else
1542 rtn = winsnstr(self->win,str,n);
1543 Py_DECREF(bytesobj);
1544 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001545 if (use_attr == TRUE)
1546 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001547 return PyCursesCheckERR(rtn, funcname);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001548}
1549
1550static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001551PyCursesWindow_Is_LineTouched(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001552{
Victor Stinner26486ea2010-05-15 22:23:53 +00001553 int line, erg;
1554 if (!PyArg_ParseTuple(args,"i;line", &line))
1555 return NULL;
1556 erg = is_linetouched(self->win, line);
1557 if (erg == ERR) {
1558 PyErr_SetString(PyExc_TypeError,
1559 "is_linetouched: line number outside of boundaries");
1560 return NULL;
1561 } else
1562 if (erg == FALSE) {
1563 Py_INCREF(Py_False);
1564 return Py_False;
1565 } else {
1566 Py_INCREF(Py_True);
1567 return Py_True;
1568 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001569}
1570
1571static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001572PyCursesWindow_NoOutRefresh(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001573{
Victor Stinner26486ea2010-05-15 22:23:53 +00001574 int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
1575 int rtn;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001576
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001577#ifndef WINDOW_HAS_FLAGS
Victor Stinner26486ea2010-05-15 22:23:53 +00001578 if (0)
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001579#else
Victor Stinner26486ea2010-05-15 22:23:53 +00001580 if (self->win->_flags & _ISPAD)
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001581#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001582 {
1583 switch(PyTuple_Size(args)) {
1584 case 6:
1585 if (!PyArg_ParseTuple(args,
1586 "iiiiii;" \
1587 "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
1588 &pminrow, &pmincol, &sminrow,
1589 &smincol, &smaxrow, &smaxcol))
1590 return NULL;
1591 Py_BEGIN_ALLOW_THREADS
1592 rtn = pnoutrefresh(self->win,
1593 pminrow, pmincol, sminrow,
1594 smincol, smaxrow, smaxcol);
1595 Py_END_ALLOW_THREADS
1596 return PyCursesCheckERR(rtn, "pnoutrefresh");
1597 default:
1598 PyErr_SetString(PyCursesError,
1599 "noutrefresh() called for a pad "
1600 "requires 6 arguments");
1601 return NULL;
1602 }
1603 } else {
1604 if (!PyArg_ParseTuple(args, ":noutrefresh"))
1605 return NULL;
Andrew M. Kuchlingf4195722000-06-23 01:36:21 +00001606
Victor Stinner26486ea2010-05-15 22:23:53 +00001607 Py_BEGIN_ALLOW_THREADS
1608 rtn = wnoutrefresh(self->win);
1609 Py_END_ALLOW_THREADS
1610 return PyCursesCheckERR(rtn, "wnoutrefresh");
1611 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001612}
1613
1614static PyObject *
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001615PyCursesWindow_Overlay(PyCursesWindowObject *self, PyObject *args)
1616{
1617 PyCursesWindowObject *temp;
1618 int use_copywin = FALSE;
1619 int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
1620 int rtn;
Victor Stinner26486ea2010-05-15 22:23:53 +00001621
Martin v. Löwisc0e16712002-01-17 23:08:27 +00001622 switch (PyTuple_Size(args)) {
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001623 case 1:
Victor Stinner26486ea2010-05-15 22:23:53 +00001624 if (!PyArg_ParseTuple(args, "O!;window object",
1625 &PyCursesWindow_Type, &temp))
1626 return NULL;
1627 break;
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001628 case 7:
Victor Stinner26486ea2010-05-15 22:23:53 +00001629 if (!PyArg_ParseTuple(args, "O!iiiiii;window object, int, int, int, int, int, int",
1630 &PyCursesWindow_Type, &temp, &sminrow, &smincol,
1631 &dminrow, &dmincol, &dmaxrow, &dmaxcol))
1632 return NULL;
1633 use_copywin = TRUE;
1634 break;
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001635 default:
Victor Stinner26486ea2010-05-15 22:23:53 +00001636 PyErr_SetString(PyExc_TypeError,
1637 "overlay requires one or seven arguments");
1638 return NULL;
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001639 }
1640
1641 if (use_copywin == TRUE) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001642 rtn = copywin(self->win, temp->win, sminrow, smincol,
1643 dminrow, dmincol, dmaxrow, dmaxcol, TRUE);
1644 return PyCursesCheckERR(rtn, "copywin");
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001645 }
1646 else {
Victor Stinner26486ea2010-05-15 22:23:53 +00001647 rtn = overlay(self->win, temp->win);
1648 return PyCursesCheckERR(rtn, "overlay");
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001649 }
1650}
1651
1652static PyObject *
1653PyCursesWindow_Overwrite(PyCursesWindowObject *self, PyObject *args)
1654{
1655 PyCursesWindowObject *temp;
1656 int use_copywin = FALSE;
1657 int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
1658 int rtn;
Victor Stinner26486ea2010-05-15 22:23:53 +00001659
Martin v. Löwisc0e16712002-01-17 23:08:27 +00001660 switch (PyTuple_Size(args)) {
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001661 case 1:
Victor Stinner26486ea2010-05-15 22:23:53 +00001662 if (!PyArg_ParseTuple(args, "O!;window object",
1663 &PyCursesWindow_Type, &temp))
1664 return NULL;
1665 break;
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001666 case 7:
Victor Stinner26486ea2010-05-15 22:23:53 +00001667 if (!PyArg_ParseTuple(args, "O!iiiiii;window object, int, int, int, int, int, int",
1668 &PyCursesWindow_Type, &temp, &sminrow, &smincol,
1669 &dminrow, &dmincol, &dmaxrow, &dmaxcol))
1670 return NULL;
1671 use_copywin = TRUE;
1672 break;
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001673 default:
Victor Stinner26486ea2010-05-15 22:23:53 +00001674 PyErr_SetString(PyExc_TypeError,
1675 "overwrite requires one or seven arguments");
1676 return NULL;
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001677 }
1678
1679 if (use_copywin == TRUE) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001680 rtn = copywin(self->win, temp->win, sminrow, smincol,
1681 dminrow, dmincol, dmaxrow, dmaxcol, FALSE);
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001682 return PyCursesCheckERR(rtn, "copywin");
1683 }
1684 else {
Victor Stinner26486ea2010-05-15 22:23:53 +00001685 rtn = overwrite(self->win, temp->win);
1686 return PyCursesCheckERR(rtn, "overwrite");
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001687 }
1688}
1689
1690static PyObject *
Guido van Rossum6c95da32007-07-24 00:16:38 +00001691PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *stream)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001692{
Victor Stinner26486ea2010-05-15 22:23:53 +00001693 /* We have to simulate this by writing to a temporary FILE*,
1694 then reading back, then writing to the argument stream. */
1695 char fn[100];
1696 int fd;
1697 FILE *fp;
1698 PyObject *res;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00001699
Victor Stinner26486ea2010-05-15 22:23:53 +00001700 strcpy(fn, "/tmp/py.curses.putwin.XXXXXX");
1701 fd = mkstemp(fn);
1702 if (fd < 0)
1703 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
1704 fp = fdopen(fd, "wb+");
1705 if (fp == NULL) {
1706 close(fd);
1707 remove(fn);
1708 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
1709 }
1710 res = PyCursesCheckERR(putwin(self->win, fp), "putwin");
1711 if (res == NULL) {
1712 fclose(fp);
1713 remove(fn);
1714 return res;
1715 }
1716 fseek(fp, 0, 0);
1717 while (1) {
1718 char buf[BUFSIZ];
1719 Py_ssize_t n = fread(buf, 1, BUFSIZ, fp);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001720 _Py_IDENTIFIER(write);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001721
Victor Stinner26486ea2010-05-15 22:23:53 +00001722 if (n <= 0)
1723 break;
1724 Py_DECREF(res);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001725 res = _PyObject_CallMethodId(stream, &PyId_write, "y#", buf, n);
Victor Stinner26486ea2010-05-15 22:23:53 +00001726 if (res == NULL)
1727 break;
1728 }
Guido van Rossum6c95da32007-07-24 00:16:38 +00001729 fclose(fp);
Guido van Rossum150b7d72007-08-30 23:34:01 +00001730 remove(fn);
Guido van Rossum6c95da32007-07-24 00:16:38 +00001731 return res;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001732}
1733
1734static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001735PyCursesWindow_RedrawLine(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001736{
Victor Stinner26486ea2010-05-15 22:23:53 +00001737 int beg, num;
1738 if (!PyArg_ParseTuple(args, "ii;beg,num", &beg, &num))
1739 return NULL;
1740 return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001741}
1742
1743static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001744PyCursesWindow_Refresh(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001745{
Victor Stinner26486ea2010-05-15 22:23:53 +00001746 int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
1747 int rtn;
Andrew M. Kuchlingf4195722000-06-23 01:36:21 +00001748
Victor Stinner26486ea2010-05-15 22:23:53 +00001749#ifndef WINDOW_HAS_FLAGS
1750 if (0)
1751#else
1752 if (self->win->_flags & _ISPAD)
1753#endif
1754 {
1755 switch(PyTuple_Size(args)) {
1756 case 6:
1757 if (!PyArg_ParseTuple(args,
1758 "iiiiii;" \
1759 "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
1760 &pminrow, &pmincol, &sminrow,
1761 &smincol, &smaxrow, &smaxcol))
1762 return NULL;
1763
1764 Py_BEGIN_ALLOW_THREADS
1765 rtn = prefresh(self->win,
1766 pminrow, pmincol, sminrow,
1767 smincol, smaxrow, smaxcol);
1768 Py_END_ALLOW_THREADS
1769 return PyCursesCheckERR(rtn, "prefresh");
1770 default:
1771 PyErr_SetString(PyCursesError,
1772 "refresh() for a pad requires 6 arguments");
1773 return NULL;
1774 }
1775 } else {
1776 if (!PyArg_ParseTuple(args, ":refresh"))
1777 return NULL;
1778 Py_BEGIN_ALLOW_THREADS
1779 rtn = wrefresh(self->win);
1780 Py_END_ALLOW_THREADS
1781 return PyCursesCheckERR(rtn, "prefresh");
1782 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001783}
1784
1785static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001786PyCursesWindow_SetScrollRegion(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001787{
Victor Stinner26486ea2010-05-15 22:23:53 +00001788 int x, y;
1789 if (!PyArg_ParseTuple(args,"ii;top, bottom",&y,&x))
1790 return NULL;
1791 return PyCursesCheckERR(wsetscrreg(self->win,y,x), "wsetscrreg");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001792}
1793
1794static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001795PyCursesWindow_SubWin(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001796{
Victor Stinner26486ea2010-05-15 22:23:53 +00001797 WINDOW *win;
1798 int nlines, ncols, begin_y, begin_x;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001799
Victor Stinner26486ea2010-05-15 22:23:53 +00001800 nlines = 0;
1801 ncols = 0;
1802 switch (PyTuple_Size(args)) {
1803 case 2:
1804 if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
1805 return NULL;
1806 break;
1807 case 4:
1808 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
1809 &nlines,&ncols,&begin_y,&begin_x))
1810 return NULL;
1811 break;
1812 default:
1813 PyErr_SetString(PyExc_TypeError, "subwin requires 2 or 4 arguments");
1814 return NULL;
1815 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001816
Victor Stinner26486ea2010-05-15 22:23:53 +00001817 /* printf("Subwin: %i %i %i %i \n", nlines, ncols, begin_y, begin_x); */
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001818#ifdef WINDOW_HAS_FLAGS
Victor Stinner26486ea2010-05-15 22:23:53 +00001819 if (self->win->_flags & _ISPAD)
1820 win = subpad(self->win, nlines, ncols, begin_y, begin_x);
1821 else
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001822#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001823 win = subwin(self->win, nlines, ncols, begin_y, begin_x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001824
Victor Stinner26486ea2010-05-15 22:23:53 +00001825 if (win == NULL) {
1826 PyErr_SetString(PyCursesError, catchall_NULL);
1827 return NULL;
1828 }
1829
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001830 return (PyObject *)PyCursesWindow_New(win, self->encoding);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001831}
1832
1833static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001834PyCursesWindow_Scroll(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001835{
Victor Stinner26486ea2010-05-15 22:23:53 +00001836 int nlines;
1837 switch(PyTuple_Size(args)) {
1838 case 0:
1839 return PyCursesCheckERR(scroll(self->win), "scroll");
1840 case 1:
1841 if (!PyArg_ParseTuple(args, "i;nlines", &nlines))
1842 return NULL;
1843 return PyCursesCheckERR(wscrl(self->win, nlines), "scroll");
1844 default:
1845 PyErr_SetString(PyExc_TypeError, "scroll requires 0 or 1 arguments");
1846 return NULL;
1847 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001848}
1849
1850static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001851PyCursesWindow_TouchLine(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001852{
Victor Stinner26486ea2010-05-15 22:23:53 +00001853 int st, cnt, val;
1854 switch (PyTuple_Size(args)) {
1855 case 2:
1856 if (!PyArg_ParseTuple(args,"ii;start,count",&st,&cnt))
1857 return NULL;
1858 return PyCursesCheckERR(touchline(self->win,st,cnt), "touchline");
1859 case 3:
1860 if (!PyArg_ParseTuple(args, "iii;start,count,val", &st, &cnt, &val))
1861 return NULL;
1862 return PyCursesCheckERR(wtouchln(self->win, st, cnt, val), "touchline");
1863 default:
1864 PyErr_SetString(PyExc_TypeError, "touchline requires 2 or 3 arguments");
1865 return NULL;
1866 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001867}
1868
1869static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001870PyCursesWindow_Vline(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001871{
Victor Stinner26486ea2010-05-15 22:23:53 +00001872 PyObject *temp;
1873 chtype ch;
1874 int n, x, y, code = OK;
1875 attr_t attr = A_NORMAL;
1876 long lattr;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001877
Victor Stinner26486ea2010-05-15 22:23:53 +00001878 switch (PyTuple_Size(args)) {
1879 case 2:
1880 if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
1881 return NULL;
1882 break;
1883 case 3:
1884 if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &lattr))
1885 return NULL;
1886 attr = lattr;
1887 break;
1888 case 4:
1889 if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
1890 return NULL;
1891 code = wmove(self->win, y, x);
1892 break;
1893 case 5:
1894 if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
1895 &y, &x, &temp, &n, &lattr))
1896 return NULL;
1897 attr = lattr;
1898 code = wmove(self->win, y, x);
1899 break;
1900 default:
1901 PyErr_SetString(PyExc_TypeError, "vline requires 2 to 5 arguments");
1902 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001903 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001904
1905 if (code != ERR) {
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001906 if (!PyCurses_ConvertToChtype(self, temp, &ch))
Victor Stinner26486ea2010-05-15 22:23:53 +00001907 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00001908 return PyCursesCheckERR(wvline(self->win, ch | attr, n), "vline");
1909 } else
1910 return PyCursesCheckERR(code, "wmove");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001911}
1912
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001913static PyObject *
1914PyCursesWindow_get_encoding(PyCursesWindowObject *self, void *closure)
1915{
1916 return PyUnicode_FromString(self->encoding);
1917}
1918
1919static int
1920PyCursesWindow_set_encoding(PyCursesWindowObject *self, PyObject *value)
1921{
1922 PyObject *ascii;
1923 char *encoding;
1924
1925 /* It is illegal to del win.encoding */
1926 if (value == NULL) {
1927 PyErr_SetString(PyExc_TypeError,
1928 "encoding may not be deleted");
1929 return -1;
1930 }
1931
1932 if (!PyUnicode_Check(value)) {
1933 PyErr_SetString(PyExc_TypeError,
1934 "setting encoding to a non-string");
1935 return -1;
1936 }
1937 ascii = PyUnicode_AsASCIIString(value);
1938 if (ascii == NULL)
1939 return -1;
1940 encoding = strdup(PyBytes_AS_STRING(ascii));
Ross Lagerwall2dabaf62012-09-07 08:34:23 +02001941 Py_DECREF(ascii);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001942 if (encoding == NULL) {
1943 PyErr_NoMemory();
1944 return -1;
1945 }
1946 free(self->encoding);
1947 self->encoding = encoding;
1948 return 0;
1949}
1950
1951
Guido van Rossumf6971e21994-08-30 12:25:20 +00001952static PyMethodDef PyCursesWindow_Methods[] = {
Victor Stinner26486ea2010-05-15 22:23:53 +00001953 {"addch", (PyCFunction)PyCursesWindow_AddCh, METH_VARARGS},
1954 {"addnstr", (PyCFunction)PyCursesWindow_AddNStr, METH_VARARGS},
1955 {"addstr", (PyCFunction)PyCursesWindow_AddStr, METH_VARARGS},
1956 {"attroff", (PyCFunction)PyCursesWindow_AttrOff, METH_VARARGS},
1957 {"attron", (PyCFunction)PyCursesWindow_AttrOn, METH_VARARGS},
1958 {"attrset", (PyCFunction)PyCursesWindow_AttrSet, METH_VARARGS},
1959 {"bkgd", (PyCFunction)PyCursesWindow_Bkgd, METH_VARARGS},
1960 {"chgat", (PyCFunction)PyCursesWindow_ChgAt, METH_VARARGS},
1961 {"bkgdset", (PyCFunction)PyCursesWindow_BkgdSet, METH_VARARGS},
1962 {"border", (PyCFunction)PyCursesWindow_Border, METH_VARARGS},
1963 {"box", (PyCFunction)PyCursesWindow_Box, METH_VARARGS},
1964 {"clear", (PyCFunction)PyCursesWindow_wclear, METH_NOARGS},
1965 {"clearok", (PyCFunction)PyCursesWindow_clearok, METH_VARARGS},
1966 {"clrtobot", (PyCFunction)PyCursesWindow_wclrtobot, METH_NOARGS},
1967 {"clrtoeol", (PyCFunction)PyCursesWindow_wclrtoeol, METH_NOARGS},
1968 {"cursyncup", (PyCFunction)PyCursesWindow_wcursyncup, METH_NOARGS},
1969 {"delch", (PyCFunction)PyCursesWindow_DelCh, METH_VARARGS},
1970 {"deleteln", (PyCFunction)PyCursesWindow_wdeleteln, METH_NOARGS},
1971 {"derwin", (PyCFunction)PyCursesWindow_DerWin, METH_VARARGS},
1972 {"echochar", (PyCFunction)PyCursesWindow_EchoChar, METH_VARARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001973#ifdef NCURSES_MOUSE_VERSION
Victor Stinner26486ea2010-05-15 22:23:53 +00001974 {"enclose", (PyCFunction)PyCursesWindow_Enclose, METH_VARARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001975#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001976 {"erase", (PyCFunction)PyCursesWindow_werase, METH_NOARGS},
1977 {"getbegyx", (PyCFunction)PyCursesWindow_getbegyx, METH_NOARGS},
1978 {"getbkgd", (PyCFunction)PyCursesWindow_GetBkgd, METH_NOARGS},
1979 {"getch", (PyCFunction)PyCursesWindow_GetCh, METH_VARARGS},
1980 {"getkey", (PyCFunction)PyCursesWindow_GetKey, METH_VARARGS},
Nadeem Vawda9e2e9902011-07-31 15:01:11 +02001981#ifdef HAVE_NCURSESW
Victor Stinnera7878b72011-07-14 23:07:44 +02001982 {"get_wch", (PyCFunction)PyCursesWindow_Get_WCh, METH_VARARGS},
Nadeem Vawda9e2e9902011-07-31 15:01:11 +02001983#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001984 {"getmaxyx", (PyCFunction)PyCursesWindow_getmaxyx, METH_NOARGS},
1985 {"getparyx", (PyCFunction)PyCursesWindow_getparyx, METH_NOARGS},
1986 {"getstr", (PyCFunction)PyCursesWindow_GetStr, METH_VARARGS},
1987 {"getyx", (PyCFunction)PyCursesWindow_getyx, METH_NOARGS},
1988 {"hline", (PyCFunction)PyCursesWindow_Hline, METH_VARARGS},
1989 {"idcok", (PyCFunction)PyCursesWindow_idcok, METH_VARARGS},
1990 {"idlok", (PyCFunction)PyCursesWindow_idlok, METH_VARARGS},
1991 {"immedok", (PyCFunction)PyCursesWindow_immedok, METH_VARARGS},
1992 {"inch", (PyCFunction)PyCursesWindow_InCh, METH_VARARGS},
1993 {"insch", (PyCFunction)PyCursesWindow_InsCh, METH_VARARGS},
1994 {"insdelln", (PyCFunction)PyCursesWindow_winsdelln, METH_VARARGS},
1995 {"insertln", (PyCFunction)PyCursesWindow_winsertln, METH_NOARGS},
1996 {"insnstr", (PyCFunction)PyCursesWindow_InsNStr, METH_VARARGS},
1997 {"insstr", (PyCFunction)PyCursesWindow_InsStr, METH_VARARGS},
1998 {"instr", (PyCFunction)PyCursesWindow_InStr, METH_VARARGS},
1999 {"is_linetouched", (PyCFunction)PyCursesWindow_Is_LineTouched, METH_VARARGS},
2000 {"is_wintouched", (PyCFunction)PyCursesWindow_is_wintouched, METH_NOARGS},
2001 {"keypad", (PyCFunction)PyCursesWindow_keypad, METH_VARARGS},
2002 {"leaveok", (PyCFunction)PyCursesWindow_leaveok, METH_VARARGS},
2003 {"move", (PyCFunction)PyCursesWindow_wmove, METH_VARARGS},
2004 {"mvderwin", (PyCFunction)PyCursesWindow_mvderwin, METH_VARARGS},
2005 {"mvwin", (PyCFunction)PyCursesWindow_mvwin, METH_VARARGS},
2006 {"nodelay", (PyCFunction)PyCursesWindow_nodelay, METH_VARARGS},
2007 {"notimeout", (PyCFunction)PyCursesWindow_notimeout, METH_VARARGS},
2008 {"noutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
2009 /* Backward compatibility alias -- remove in Python 2.3 */
2010 {"nooutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
2011 {"overlay", (PyCFunction)PyCursesWindow_Overlay, METH_VARARGS},
2012 {"overwrite", (PyCFunction)PyCursesWindow_Overwrite,
2013 METH_VARARGS},
2014 {"putwin", (PyCFunction)PyCursesWindow_PutWin, METH_O},
2015 {"redrawln", (PyCFunction)PyCursesWindow_RedrawLine, METH_VARARGS},
2016 {"redrawwin", (PyCFunction)PyCursesWindow_redrawwin, METH_NOARGS},
2017 {"refresh", (PyCFunction)PyCursesWindow_Refresh, METH_VARARGS},
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +00002018#ifndef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00002019 {"resize", (PyCFunction)PyCursesWindow_wresize, METH_VARARGS},
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002020#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00002021 {"scroll", (PyCFunction)PyCursesWindow_Scroll, METH_VARARGS},
2022 {"scrollok", (PyCFunction)PyCursesWindow_scrollok, METH_VARARGS},
2023 {"setscrreg", (PyCFunction)PyCursesWindow_SetScrollRegion, METH_VARARGS},
2024 {"standend", (PyCFunction)PyCursesWindow_wstandend, METH_NOARGS},
2025 {"standout", (PyCFunction)PyCursesWindow_wstandout, METH_NOARGS},
2026 {"subpad", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
2027 {"subwin", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
2028 {"syncdown", (PyCFunction)PyCursesWindow_wsyncdown, METH_NOARGS},
2029 {"syncok", (PyCFunction)PyCursesWindow_syncok, METH_VARARGS},
2030 {"syncup", (PyCFunction)PyCursesWindow_wsyncup, METH_NOARGS},
2031 {"timeout", (PyCFunction)PyCursesWindow_wtimeout, METH_VARARGS},
2032 {"touchline", (PyCFunction)PyCursesWindow_TouchLine, METH_VARARGS},
2033 {"touchwin", (PyCFunction)PyCursesWindow_touchwin, METH_NOARGS},
2034 {"untouchwin", (PyCFunction)PyCursesWindow_untouchwin, METH_NOARGS},
2035 {"vline", (PyCFunction)PyCursesWindow_Vline, METH_VARARGS},
2036 {NULL, NULL} /* sentinel */
Guido van Rossumf6971e21994-08-30 12:25:20 +00002037};
2038
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002039static PyGetSetDef PyCursesWindow_getsets[] = {
2040 {"encoding",
2041 (getter)PyCursesWindow_get_encoding,
2042 (setter)PyCursesWindow_set_encoding,
Ronald Oussoren2a7fe032012-01-17 16:53:20 +01002043 "the typecode character used to create the array"},
2044 {NULL, NULL, NULL, NULL } /* sentinel */
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002045};
2046
Guido van Rossumf6971e21994-08-30 12:25:20 +00002047/* -------------------------------------------------------*/
Guido van Rossum85738471995-02-17 13:50:17 +00002048
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002049PyTypeObject PyCursesWindow_Type = {
Victor Stinner26486ea2010-05-15 22:23:53 +00002050 PyVarObject_HEAD_INIT(NULL, 0)
2051 "_curses.curses window", /*tp_name*/
2052 sizeof(PyCursesWindowObject), /*tp_basicsize*/
2053 0, /*tp_itemsize*/
2054 /* methods */
2055 (destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/
2056 0, /*tp_print*/
2057 (getattrfunc)0, /*tp_getattr*/
2058 (setattrfunc)0, /*tp_setattr*/
2059 0, /*tp_reserved*/
2060 0, /*tp_repr*/
2061 0, /*tp_as_number*/
2062 0, /*tp_as_sequence*/
2063 0, /*tp_as_mapping*/
2064 0, /*tp_hash*/
2065 0, /*tp_call*/
2066 0, /*tp_str*/
2067 0, /*tp_getattro*/
2068 0, /*tp_setattro*/
2069 0, /*tp_as_buffer*/
2070 Py_TPFLAGS_DEFAULT, /*tp_flags*/
2071 0, /*tp_doc*/
2072 0, /*tp_traverse*/
2073 0, /*tp_clear*/
2074 0, /*tp_richcompare*/
2075 0, /*tp_weaklistoffset*/
2076 0, /*tp_iter*/
2077 0, /*tp_iternext*/
2078 PyCursesWindow_Methods, /*tp_methods*/
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002079 0, /* tp_members */
2080 PyCursesWindow_getsets, /* tp_getset */
Guido van Rossumf6971e21994-08-30 12:25:20 +00002081};
2082
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002083/*********************************************************************
2084 Global Functions
2085**********************************************************************/
Guido van Rossumf6971e21994-08-30 12:25:20 +00002086
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002087NoArgNoReturnFunction(beep)
2088NoArgNoReturnFunction(def_prog_mode)
2089NoArgNoReturnFunction(def_shell_mode)
2090NoArgNoReturnFunction(doupdate)
2091NoArgNoReturnFunction(endwin)
2092NoArgNoReturnFunction(flash)
2093NoArgNoReturnFunction(nocbreak)
2094NoArgNoReturnFunction(noecho)
2095NoArgNoReturnFunction(nonl)
2096NoArgNoReturnFunction(noraw)
2097NoArgNoReturnFunction(reset_prog_mode)
2098NoArgNoReturnFunction(reset_shell_mode)
2099NoArgNoReturnFunction(resetty)
2100NoArgNoReturnFunction(savetty)
2101
2102NoArgOrFlagNoReturnFunction(cbreak)
2103NoArgOrFlagNoReturnFunction(echo)
2104NoArgOrFlagNoReturnFunction(nl)
2105NoArgOrFlagNoReturnFunction(raw)
2106
2107NoArgReturnIntFunction(baudrate)
2108NoArgReturnIntFunction(termattrs)
2109
2110NoArgReturnStringFunction(termname)
2111NoArgReturnStringFunction(longname)
2112
2113NoArgTrueFalseFunction(can_change_color)
2114NoArgTrueFalseFunction(has_colors)
2115NoArgTrueFalseFunction(has_ic)
2116NoArgTrueFalseFunction(has_il)
2117NoArgTrueFalseFunction(isendwin)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002118NoArgNoReturnVoidFunction(flushinp)
2119NoArgNoReturnVoidFunction(noqiflush)
2120
2121static PyObject *
Christian Heimesaf98da12008-01-27 15:18:18 +00002122PyCurses_filter(PyObject *self)
2123{
Victor Stinner26486ea2010-05-15 22:23:53 +00002124 /* not checking for PyCursesInitialised here since filter() must
2125 be called before initscr() */
2126 filter();
2127 Py_INCREF(Py_None);
2128 return Py_None;
Christian Heimesaf98da12008-01-27 15:18:18 +00002129}
2130
2131static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002132PyCurses_Color_Content(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002133{
Victor Stinner26486ea2010-05-15 22:23:53 +00002134 short color,r,g,b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002135
Victor Stinner26486ea2010-05-15 22:23:53 +00002136 PyCursesInitialised;
2137 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002138
Victor Stinner26486ea2010-05-15 22:23:53 +00002139 if (!PyArg_ParseTuple(args, "h:color_content", &color)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002140
Victor Stinner26486ea2010-05-15 22:23:53 +00002141 if (color_content(color, &r, &g, &b) != ERR)
2142 return Py_BuildValue("(iii)", r, g, b);
2143 else {
2144 PyErr_SetString(PyCursesError,
2145 "Argument 1 was out of range. Check value of COLORS.");
2146 return NULL;
2147 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002148}
2149
2150static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002151PyCurses_color_pair(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002152{
Victor Stinner26486ea2010-05-15 22:23:53 +00002153 int n;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002154
Victor Stinner26486ea2010-05-15 22:23:53 +00002155 PyCursesInitialised;
2156 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002157
Victor Stinner26486ea2010-05-15 22:23:53 +00002158 if (!PyArg_ParseTuple(args, "i:color_pair", &n)) return NULL;
2159 return PyLong_FromLong((long) (n << 8));
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002160}
2161
2162static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002163PyCurses_Curs_Set(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002164{
Victor Stinner26486ea2010-05-15 22:23:53 +00002165 int vis,erg;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002166
Victor Stinner26486ea2010-05-15 22:23:53 +00002167 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002168
Victor Stinner26486ea2010-05-15 22:23:53 +00002169 if (!PyArg_ParseTuple(args, "i:curs_set", &vis)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002170
Victor Stinner26486ea2010-05-15 22:23:53 +00002171 erg = curs_set(vis);
2172 if (erg == ERR) return PyCursesCheckERR(erg, "curs_set");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002173
Victor Stinner26486ea2010-05-15 22:23:53 +00002174 return PyLong_FromLong((long) erg);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002175}
2176
2177static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002178PyCurses_Delay_Output(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002179{
Victor Stinner26486ea2010-05-15 22:23:53 +00002180 int ms;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002181
Victor Stinner26486ea2010-05-15 22:23:53 +00002182 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002183
Victor Stinner26486ea2010-05-15 22:23:53 +00002184 if (!PyArg_ParseTuple(args, "i:delay_output", &ms)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002185
Victor Stinner26486ea2010-05-15 22:23:53 +00002186 return PyCursesCheckERR(delay_output(ms), "delay_output");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002187}
2188
2189static PyObject *
Martin v. Löwisc0e16712002-01-17 23:08:27 +00002190PyCurses_EraseChar(PyObject *self)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002191{
Victor Stinner26486ea2010-05-15 22:23:53 +00002192 char ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002193
Victor Stinner26486ea2010-05-15 22:23:53 +00002194 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002195
Victor Stinner26486ea2010-05-15 22:23:53 +00002196 ch = erasechar();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002197
Victor Stinner26486ea2010-05-15 22:23:53 +00002198 return PyBytes_FromStringAndSize(&ch, 1);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002199}
2200
2201static PyObject *
Martin v. Löwisc0e16712002-01-17 23:08:27 +00002202PyCurses_getsyx(PyObject *self)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002203{
Victor Stinner26486ea2010-05-15 22:23:53 +00002204 int x = 0;
2205 int y = 0;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002206
Victor Stinner26486ea2010-05-15 22:23:53 +00002207 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002208
Victor Stinner26486ea2010-05-15 22:23:53 +00002209 getsyx(y, x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002210
Victor Stinner26486ea2010-05-15 22:23:53 +00002211 return Py_BuildValue("(ii)", y, x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002212}
2213
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002214#ifdef NCURSES_MOUSE_VERSION
2215static PyObject *
Martin v. Löwisc0e16712002-01-17 23:08:27 +00002216PyCurses_GetMouse(PyObject *self)
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002217{
Victor Stinner26486ea2010-05-15 22:23:53 +00002218 int rtn;
2219 MEVENT event;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002220
Victor Stinner26486ea2010-05-15 22:23:53 +00002221 PyCursesInitialised;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002222
Victor Stinner26486ea2010-05-15 22:23:53 +00002223 rtn = getmouse( &event );
2224 if (rtn == ERR) {
2225 PyErr_SetString(PyCursesError, "getmouse() returned ERR");
2226 return NULL;
2227 }
2228 return Py_BuildValue("(hiiil)",
2229 (short)event.id,
2230 event.x, event.y, event.z,
2231 (long) event.bstate);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002232}
2233
2234static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002235PyCurses_UngetMouse(PyObject *self, PyObject *args)
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002236{
Victor Stinner26486ea2010-05-15 22:23:53 +00002237 MEVENT event;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002238
Victor Stinner26486ea2010-05-15 22:23:53 +00002239 PyCursesInitialised;
2240 if (!PyArg_ParseTuple(args, "hiiil",
2241 &event.id,
2242 &event.x, &event.y, &event.z,
2243 (int *) &event.bstate))
2244 return NULL;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002245
Victor Stinner26486ea2010-05-15 22:23:53 +00002246 return PyCursesCheckERR(ungetmouse(&event), "ungetmouse");
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002247}
2248#endif
2249
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002250static PyObject *
Guido van Rossum6c95da32007-07-24 00:16:38 +00002251PyCurses_GetWin(PyCursesWindowObject *self, PyObject *stream)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002252{
Victor Stinner26486ea2010-05-15 22:23:53 +00002253 char fn[100];
2254 int fd;
2255 FILE *fp;
2256 PyObject *data;
2257 size_t datalen;
2258 WINDOW *win;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002259
Victor Stinner26486ea2010-05-15 22:23:53 +00002260 PyCursesInitialised;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002261 _Py_IDENTIFIER(read);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002262
Victor Stinner26486ea2010-05-15 22:23:53 +00002263 strcpy(fn, "/tmp/py.curses.getwin.XXXXXX");
2264 fd = mkstemp(fn);
2265 if (fd < 0)
2266 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
2267 fp = fdopen(fd, "wb+");
2268 if (fp == NULL) {
2269 close(fd);
2270 remove(fn);
2271 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
2272 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002273 data = _PyObject_CallMethodId(stream, &PyId_read, "");
Victor Stinner26486ea2010-05-15 22:23:53 +00002274 if (data == NULL) {
2275 fclose(fp);
2276 remove(fn);
2277 return NULL;
2278 }
2279 if (!PyBytes_Check(data)) {
2280 PyErr_Format(PyExc_TypeError,
2281 "f.read() returned %.100s instead of bytes",
2282 data->ob_type->tp_name);
2283 Py_DECREF(data);
2284 fclose(fp);
2285 remove(fn);
2286 return NULL;
2287 }
2288 datalen = PyBytes_GET_SIZE(data);
2289 if (fwrite(PyBytes_AS_STRING(data), 1, datalen, fp) != datalen) {
2290 Py_DECREF(data);
2291 fclose(fp);
2292 remove(fn);
2293 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
2294 }
Guido van Rossum6c95da32007-07-24 00:16:38 +00002295 Py_DECREF(data);
Victor Stinner26486ea2010-05-15 22:23:53 +00002296 fseek(fp, 0, 0);
2297 win = getwin(fp);
Guido van Rossum6c95da32007-07-24 00:16:38 +00002298 fclose(fp);
Guido van Rossum150b7d72007-08-30 23:34:01 +00002299 remove(fn);
Victor Stinner26486ea2010-05-15 22:23:53 +00002300 if (win == NULL) {
2301 PyErr_SetString(PyCursesError, catchall_NULL);
2302 return NULL;
2303 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002304 return PyCursesWindow_New(win, NULL);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002305}
2306
2307static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002308PyCurses_HalfDelay(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002309{
Victor Stinner26486ea2010-05-15 22:23:53 +00002310 unsigned char tenths;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002311
Victor Stinner26486ea2010-05-15 22:23:53 +00002312 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002313
Victor Stinner26486ea2010-05-15 22:23:53 +00002314 if (!PyArg_ParseTuple(args, "b:halfdelay", &tenths)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002315
Victor Stinner26486ea2010-05-15 22:23:53 +00002316 return PyCursesCheckERR(halfdelay(tenths), "halfdelay");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002317}
2318
Andrew M. Kuchlingbf680262000-07-25 12:37:41 +00002319#ifndef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00002320/* No has_key! */
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002321static PyObject * PyCurses_has_key(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002322{
Victor Stinner26486ea2010-05-15 22:23:53 +00002323 int ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002324
Victor Stinner26486ea2010-05-15 22:23:53 +00002325 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002326
Victor Stinner26486ea2010-05-15 22:23:53 +00002327 if (!PyArg_ParseTuple(args,"i",&ch)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002328
Victor Stinner26486ea2010-05-15 22:23:53 +00002329 if (has_key(ch) == FALSE) {
2330 Py_INCREF(Py_False);
2331 return Py_False;
2332 }
2333 Py_INCREF(Py_True);
2334 return Py_True;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002335}
Andrew M. Kuchlingbf680262000-07-25 12:37:41 +00002336#endif /* STRICT_SYSV_CURSES */
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002337
2338static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002339PyCurses_Init_Color(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002340{
Victor Stinner26486ea2010-05-15 22:23:53 +00002341 short color, r, g, b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002342
Victor Stinner26486ea2010-05-15 22:23:53 +00002343 PyCursesInitialised;
2344 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002345
Victor Stinner26486ea2010-05-15 22:23:53 +00002346 switch(PyTuple_Size(args)) {
2347 case 4:
2348 if (!PyArg_ParseTuple(args, "hhhh;color,r,g,b", &color, &r, &g, &b)) return NULL;
2349 break;
2350 default:
2351 PyErr_SetString(PyExc_TypeError, "init_color requires 4 arguments");
2352 return NULL;
2353 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002354
Victor Stinner26486ea2010-05-15 22:23:53 +00002355 return PyCursesCheckERR(init_color(color, r, g, b), "init_color");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002356}
2357
2358static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002359PyCurses_Init_Pair(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002360{
Victor Stinner26486ea2010-05-15 22:23:53 +00002361 short pair, f, b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002362
Victor Stinner26486ea2010-05-15 22:23:53 +00002363 PyCursesInitialised;
2364 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002365
Victor Stinner26486ea2010-05-15 22:23:53 +00002366 if (PyTuple_Size(args) != 3) {
2367 PyErr_SetString(PyExc_TypeError, "init_pair requires 3 arguments");
2368 return NULL;
2369 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002370
Victor Stinner26486ea2010-05-15 22:23:53 +00002371 if (!PyArg_ParseTuple(args, "hhh;pair, f, b", &pair, &f, &b)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002372
Victor Stinner26486ea2010-05-15 22:23:53 +00002373 return PyCursesCheckERR(init_pair(pair, f, b), "init_pair");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002374}
2375
Andrew M. Kuchling32552682000-12-22 21:52:27 +00002376static PyObject *ModDict;
2377
Victor Stinner26486ea2010-05-15 22:23:53 +00002378static PyObject *
Martin v. Löwisc0e16712002-01-17 23:08:27 +00002379PyCurses_InitScr(PyObject *self)
Guido van Rossumf6971e21994-08-30 12:25:20 +00002380{
Victor Stinner26486ea2010-05-15 22:23:53 +00002381 WINDOW *win;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002382 PyCursesWindowObject *winobj;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002383
Victor Stinner26486ea2010-05-15 22:23:53 +00002384 if (initialised == TRUE) {
2385 wrefresh(stdscr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002386 return (PyObject *)PyCursesWindow_New(stdscr, NULL);
Victor Stinner26486ea2010-05-15 22:23:53 +00002387 }
Guido van Rossum56bf2351994-08-31 22:06:24 +00002388
Victor Stinner26486ea2010-05-15 22:23:53 +00002389 win = initscr();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002390
Victor Stinner26486ea2010-05-15 22:23:53 +00002391 if (win == NULL) {
2392 PyErr_SetString(PyCursesError, catchall_NULL);
2393 return NULL;
2394 }
Guido van Rossum85738471995-02-17 13:50:17 +00002395
Victor Stinner26486ea2010-05-15 22:23:53 +00002396 initialised = initialised_setupterm = TRUE;
Guido van Rossum56bf2351994-08-31 22:06:24 +00002397
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00002398/* This was moved from initcurses() because it core dumped on SGI,
2399 where they're not defined until you've called initscr() */
Victor Stinner26486ea2010-05-15 22:23:53 +00002400#define SetDictInt(string,ch) \
2401 do { \
2402 PyObject *o = PyLong_FromLong((long) (ch)); \
2403 if (o && PyDict_SetItemString(ModDict, string, o) == 0) { \
2404 Py_DECREF(o); \
2405 } \
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002406 } while (0)
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00002407
Victor Stinner26486ea2010-05-15 22:23:53 +00002408 /* Here are some graphic symbols you can use */
2409 SetDictInt("ACS_ULCORNER", (ACS_ULCORNER));
2410 SetDictInt("ACS_LLCORNER", (ACS_LLCORNER));
2411 SetDictInt("ACS_URCORNER", (ACS_URCORNER));
2412 SetDictInt("ACS_LRCORNER", (ACS_LRCORNER));
2413 SetDictInt("ACS_LTEE", (ACS_LTEE));
2414 SetDictInt("ACS_RTEE", (ACS_RTEE));
2415 SetDictInt("ACS_BTEE", (ACS_BTEE));
2416 SetDictInt("ACS_TTEE", (ACS_TTEE));
2417 SetDictInt("ACS_HLINE", (ACS_HLINE));
2418 SetDictInt("ACS_VLINE", (ACS_VLINE));
2419 SetDictInt("ACS_PLUS", (ACS_PLUS));
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00002420#if !defined(__hpux) || defined(HAVE_NCURSES_H)
Victor Stinner26486ea2010-05-15 22:23:53 +00002421 /* On HP/UX 11, these are of type cchar_t, which is not an
2422 integral type. If this is a problem on more platforms, a
2423 configure test should be added to determine whether ACS_S1
2424 is of integral type. */
2425 SetDictInt("ACS_S1", (ACS_S1));
2426 SetDictInt("ACS_S9", (ACS_S9));
2427 SetDictInt("ACS_DIAMOND", (ACS_DIAMOND));
2428 SetDictInt("ACS_CKBOARD", (ACS_CKBOARD));
2429 SetDictInt("ACS_DEGREE", (ACS_DEGREE));
2430 SetDictInt("ACS_PLMINUS", (ACS_PLMINUS));
2431 SetDictInt("ACS_BULLET", (ACS_BULLET));
2432 SetDictInt("ACS_LARROW", (ACS_LARROW));
2433 SetDictInt("ACS_RARROW", (ACS_RARROW));
2434 SetDictInt("ACS_DARROW", (ACS_DARROW));
2435 SetDictInt("ACS_UARROW", (ACS_UARROW));
2436 SetDictInt("ACS_BOARD", (ACS_BOARD));
2437 SetDictInt("ACS_LANTERN", (ACS_LANTERN));
2438 SetDictInt("ACS_BLOCK", (ACS_BLOCK));
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00002439#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00002440 SetDictInt("ACS_BSSB", (ACS_ULCORNER));
2441 SetDictInt("ACS_SSBB", (ACS_LLCORNER));
2442 SetDictInt("ACS_BBSS", (ACS_URCORNER));
2443 SetDictInt("ACS_SBBS", (ACS_LRCORNER));
2444 SetDictInt("ACS_SBSS", (ACS_RTEE));
2445 SetDictInt("ACS_SSSB", (ACS_LTEE));
2446 SetDictInt("ACS_SSBS", (ACS_BTEE));
2447 SetDictInt("ACS_BSSS", (ACS_TTEE));
2448 SetDictInt("ACS_BSBS", (ACS_HLINE));
2449 SetDictInt("ACS_SBSB", (ACS_VLINE));
2450 SetDictInt("ACS_SSSS", (ACS_PLUS));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002451
Victor Stinner26486ea2010-05-15 22:23:53 +00002452 /* The following are never available with strict SYSV curses */
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002453#ifdef ACS_S3
Victor Stinner26486ea2010-05-15 22:23:53 +00002454 SetDictInt("ACS_S3", (ACS_S3));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002455#endif
2456#ifdef ACS_S7
Victor Stinner26486ea2010-05-15 22:23:53 +00002457 SetDictInt("ACS_S7", (ACS_S7));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002458#endif
2459#ifdef ACS_LEQUAL
Victor Stinner26486ea2010-05-15 22:23:53 +00002460 SetDictInt("ACS_LEQUAL", (ACS_LEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002461#endif
2462#ifdef ACS_GEQUAL
Victor Stinner26486ea2010-05-15 22:23:53 +00002463 SetDictInt("ACS_GEQUAL", (ACS_GEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002464#endif
2465#ifdef ACS_PI
Victor Stinner26486ea2010-05-15 22:23:53 +00002466 SetDictInt("ACS_PI", (ACS_PI));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002467#endif
2468#ifdef ACS_NEQUAL
Victor Stinner26486ea2010-05-15 22:23:53 +00002469 SetDictInt("ACS_NEQUAL", (ACS_NEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002470#endif
2471#ifdef ACS_STERLING
Victor Stinner26486ea2010-05-15 22:23:53 +00002472 SetDictInt("ACS_STERLING", (ACS_STERLING));
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00002473#endif
2474
Victor Stinner26486ea2010-05-15 22:23:53 +00002475 SetDictInt("LINES", LINES);
2476 SetDictInt("COLS", COLS);
Guido van Rossum56bf2351994-08-31 22:06:24 +00002477
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002478 winobj = (PyCursesWindowObject *)PyCursesWindow_New(win, NULL);
2479 screen_encoding = winobj->encoding;
2480 return (PyObject *)winobj;
Guido van Rossumf6971e21994-08-30 12:25:20 +00002481}
2482
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002483static PyObject *
2484PyCurses_setupterm(PyObject* self, PyObject *args, PyObject* keywds)
2485{
Victor Stinner26486ea2010-05-15 22:23:53 +00002486 int fd = -1;
2487 int err;
2488 char* termstr = NULL;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002489
Victor Stinner26486ea2010-05-15 22:23:53 +00002490 static char *kwlist[] = {"term", "fd", NULL};
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002491
Victor Stinner26486ea2010-05-15 22:23:53 +00002492 if (!PyArg_ParseTupleAndKeywords(
2493 args, keywds, "|zi:setupterm", kwlist, &termstr, &fd)) {
2494 return NULL;
2495 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002496
Victor Stinner26486ea2010-05-15 22:23:53 +00002497 if (fd == -1) {
2498 PyObject* sys_stdout;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002499
Victor Stinner26486ea2010-05-15 22:23:53 +00002500 sys_stdout = PySys_GetObject("stdout");
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002501
Victor Stinner26486ea2010-05-15 22:23:53 +00002502 if (sys_stdout == NULL || sys_stdout == Py_None) {
2503 PyErr_SetString(
2504 PyCursesError,
2505 "lost sys.stdout");
2506 return NULL;
2507 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002508
Victor Stinner26486ea2010-05-15 22:23:53 +00002509 fd = PyObject_AsFileDescriptor(sys_stdout);
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002510
Victor Stinner26486ea2010-05-15 22:23:53 +00002511 if (fd == -1) {
2512 return NULL;
2513 }
2514 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002515
Matthias Klose635edd12010-07-30 21:40:57 +00002516 if (!initialised_setupterm && setupterm(termstr,fd,&err) == ERR) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002517 char* s = "setupterm: unknown error";
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002518
Victor Stinner26486ea2010-05-15 22:23:53 +00002519 if (err == 0) {
2520 s = "setupterm: could not find terminal";
2521 } else if (err == -1) {
2522 s = "setupterm: could not find terminfo database";
2523 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002524
Victor Stinner26486ea2010-05-15 22:23:53 +00002525 PyErr_SetString(PyCursesError,s);
2526 return NULL;
2527 }
2528
2529 initialised_setupterm = TRUE;
2530
2531 Py_INCREF(Py_None);
2532 return Py_None;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002533}
Guido van Rossumf6971e21994-08-30 12:25:20 +00002534
2535static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002536PyCurses_IntrFlush(PyObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00002537{
Victor Stinner26486ea2010-05-15 22:23:53 +00002538 int ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002539
Victor Stinner26486ea2010-05-15 22:23:53 +00002540 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002541
Victor Stinner26486ea2010-05-15 22:23:53 +00002542 switch(PyTuple_Size(args)) {
2543 case 1:
2544 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL;
2545 break;
2546 default:
2547 PyErr_SetString(PyExc_TypeError, "intrflush requires 1 argument");
2548 return NULL;
2549 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002550
Victor Stinner26486ea2010-05-15 22:23:53 +00002551 return PyCursesCheckERR(intrflush(NULL,ch), "intrflush");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002552}
2553
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002554#ifdef HAVE_CURSES_IS_TERM_RESIZED
2555static PyObject *
2556PyCurses_Is_Term_Resized(PyObject *self, PyObject *args)
2557{
Victor Stinner26486ea2010-05-15 22:23:53 +00002558 int lines;
2559 int columns;
2560 int result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002561
Victor Stinner26486ea2010-05-15 22:23:53 +00002562 PyCursesInitialised;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002563
Victor Stinner26486ea2010-05-15 22:23:53 +00002564 if (!PyArg_ParseTuple(args,"ii:is_term_resized", &lines, &columns))
2565 return NULL;
2566 result = is_term_resized(lines, columns);
2567 if (result == TRUE) {
2568 Py_INCREF(Py_True);
2569 return Py_True;
2570 } else {
2571 Py_INCREF(Py_False);
2572 return Py_False;
2573 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002574}
2575#endif /* HAVE_CURSES_IS_TERM_RESIZED */
2576
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00002577#if !defined(__NetBSD__)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002578static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002579PyCurses_KeyName(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002580{
Victor Stinner26486ea2010-05-15 22:23:53 +00002581 const char *knp;
2582 int ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002583
Victor Stinner26486ea2010-05-15 22:23:53 +00002584 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002585
Victor Stinner26486ea2010-05-15 22:23:53 +00002586 if (!PyArg_ParseTuple(args,"i",&ch)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002587
Victor Stinner26486ea2010-05-15 22:23:53 +00002588 if (ch < 0) {
2589 PyErr_SetString(PyExc_ValueError, "invalid key number");
2590 return NULL;
2591 }
2592 knp = keyname(ch);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002593
Victor Stinner26486ea2010-05-15 22:23:53 +00002594 return PyBytes_FromString((knp == NULL) ? "" : (char *)knp);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002595}
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00002596#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002597
Victor Stinner26486ea2010-05-15 22:23:53 +00002598static PyObject *
2599PyCurses_KillChar(PyObject *self)
2600{
2601 char ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002602
Victor Stinner26486ea2010-05-15 22:23:53 +00002603 ch = killchar();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002604
Victor Stinner26486ea2010-05-15 22:23:53 +00002605 return PyBytes_FromStringAndSize(&ch, 1);
2606}
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002607
2608static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002609PyCurses_Meta(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002610{
Victor Stinner26486ea2010-05-15 22:23:53 +00002611 int ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002612
Victor Stinner26486ea2010-05-15 22:23:53 +00002613 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002614
Victor Stinner26486ea2010-05-15 22:23:53 +00002615 switch(PyTuple_Size(args)) {
2616 case 1:
2617 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL;
2618 break;
2619 default:
2620 PyErr_SetString(PyExc_TypeError, "meta requires 1 argument");
2621 return NULL;
2622 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002623
Victor Stinner26486ea2010-05-15 22:23:53 +00002624 return PyCursesCheckERR(meta(stdscr, ch), "meta");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002625}
2626
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002627#ifdef NCURSES_MOUSE_VERSION
2628static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002629PyCurses_MouseInterval(PyObject *self, PyObject *args)
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002630{
Victor Stinner26486ea2010-05-15 22:23:53 +00002631 int interval;
2632 PyCursesInitialised;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002633
Victor Stinner26486ea2010-05-15 22:23:53 +00002634 if (!PyArg_ParseTuple(args,"i;interval",&interval))
2635 return NULL;
2636 return PyCursesCheckERR(mouseinterval(interval), "mouseinterval");
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002637}
2638
2639static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002640PyCurses_MouseMask(PyObject *self, PyObject *args)
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002641{
Victor Stinner26486ea2010-05-15 22:23:53 +00002642 int newmask;
2643 mmask_t oldmask, availmask;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002644
Victor Stinner26486ea2010-05-15 22:23:53 +00002645 PyCursesInitialised;
2646 if (!PyArg_ParseTuple(args,"i;mousemask",&newmask))
2647 return NULL;
2648 availmask = mousemask(newmask, &oldmask);
2649 return Py_BuildValue("(ll)", (long)availmask, (long)oldmask);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002650}
2651#endif
2652
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002653static PyObject *
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +00002654PyCurses_Napms(PyObject *self, PyObject *args)
2655{
2656 int ms;
2657
Andrew M. Kuchling2b146762010-05-10 23:30:25 +00002658 PyCursesInitialised;
Martin v. Löwisc0e16712002-01-17 23:08:27 +00002659 if (!PyArg_ParseTuple(args, "i;ms", &ms)) return NULL;
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +00002660
2661 return Py_BuildValue("i", napms(ms));
2662}
2663
2664
2665static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002666PyCurses_NewPad(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002667{
Victor Stinner26486ea2010-05-15 22:23:53 +00002668 WINDOW *win;
2669 int nlines, ncols;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002670
Victor Stinner26486ea2010-05-15 22:23:53 +00002671 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002672
Victor Stinner26486ea2010-05-15 22:23:53 +00002673 if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002674
Victor Stinner26486ea2010-05-15 22:23:53 +00002675 win = newpad(nlines, ncols);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002676
Victor Stinner26486ea2010-05-15 22:23:53 +00002677 if (win == NULL) {
2678 PyErr_SetString(PyCursesError, catchall_NULL);
2679 return NULL;
2680 }
2681
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002682 return (PyObject *)PyCursesWindow_New(win, NULL);
Guido van Rossumf6971e21994-08-30 12:25:20 +00002683}
2684
2685static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002686PyCurses_NewWindow(PyObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00002687{
Victor Stinner26486ea2010-05-15 22:23:53 +00002688 WINDOW *win;
2689 int nlines, ncols, begin_y=0, begin_x=0;
Guido van Rossum85738471995-02-17 13:50:17 +00002690
Victor Stinner26486ea2010-05-15 22:23:53 +00002691 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002692
Victor Stinner26486ea2010-05-15 22:23:53 +00002693 switch (PyTuple_Size(args)) {
2694 case 2:
2695 if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols))
2696 return NULL;
2697 break;
2698 case 4:
2699 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
2700 &nlines,&ncols,&begin_y,&begin_x))
2701 return NULL;
2702 break;
2703 default:
2704 PyErr_SetString(PyExc_TypeError, "newwin requires 2 or 4 arguments");
2705 return NULL;
2706 }
Guido van Rossum85738471995-02-17 13:50:17 +00002707
Victor Stinner26486ea2010-05-15 22:23:53 +00002708 win = newwin(nlines,ncols,begin_y,begin_x);
2709 if (win == NULL) {
2710 PyErr_SetString(PyCursesError, catchall_NULL);
2711 return NULL;
2712 }
Guido van Rossum85738471995-02-17 13:50:17 +00002713
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002714 return (PyObject *)PyCursesWindow_New(win, NULL);
Guido van Rossumf6971e21994-08-30 12:25:20 +00002715}
2716
2717static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002718PyCurses_Pair_Content(PyObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00002719{
Victor Stinner26486ea2010-05-15 22:23:53 +00002720 short pair,f,b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002721
Victor Stinner26486ea2010-05-15 22:23:53 +00002722 PyCursesInitialised;
2723 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002724
Victor Stinner26486ea2010-05-15 22:23:53 +00002725 switch(PyTuple_Size(args)) {
2726 case 1:
2727 if (!PyArg_ParseTuple(args, "h;pair", &pair)) return NULL;
2728 break;
2729 default:
2730 PyErr_SetString(PyExc_TypeError, "pair_content requires 1 argument");
2731 return NULL;
2732 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002733
Victor Stinner26486ea2010-05-15 22:23:53 +00002734 if (pair_content(pair, &f, &b)==ERR) {
2735 PyErr_SetString(PyCursesError,
2736 "Argument 1 was out of range. (1..COLOR_PAIRS-1)");
2737 return NULL;
2738 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002739
Victor Stinner26486ea2010-05-15 22:23:53 +00002740 return Py_BuildValue("(ii)", f, b);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002741}
2742
2743static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002744PyCurses_pair_number(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002745{
Victor Stinner26486ea2010-05-15 22:23:53 +00002746 int n;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002747
Victor Stinner26486ea2010-05-15 22:23:53 +00002748 PyCursesInitialised;
2749 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002750
Victor Stinner26486ea2010-05-15 22:23:53 +00002751 switch(PyTuple_Size(args)) {
2752 case 1:
2753 if (!PyArg_ParseTuple(args, "i;pairvalue", &n)) return NULL;
2754 break;
2755 default:
2756 PyErr_SetString(PyExc_TypeError,
2757 "pair_number requires 1 argument");
2758 return NULL;
2759 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002760
Victor Stinner26486ea2010-05-15 22:23:53 +00002761 return PyLong_FromLong((long) ((n & A_COLOR) >> 8));
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002762}
2763
2764static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002765PyCurses_Putp(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002766{
Victor Stinner26486ea2010-05-15 22:23:53 +00002767 char *str;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002768
Victor Stinnerb3bc7e72011-11-03 20:35:40 +01002769 if (!PyArg_ParseTuple(args,"y;str", &str))
2770 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00002771 return PyCursesCheckERR(putp(str), "putp");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002772}
2773
2774static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002775PyCurses_QiFlush(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002776{
Victor Stinner26486ea2010-05-15 22:23:53 +00002777 int flag = 0;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002778
Victor Stinner26486ea2010-05-15 22:23:53 +00002779 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002780
Victor Stinner26486ea2010-05-15 22:23:53 +00002781 switch(PyTuple_Size(args)) {
2782 case 0:
2783 qiflush();
2784 Py_INCREF(Py_None);
2785 return Py_None;
2786 case 1:
2787 if (!PyArg_ParseTuple(args, "i;True(1) or False(0)", &flag)) return NULL;
2788 if (flag) qiflush();
2789 else noqiflush();
2790 Py_INCREF(Py_None);
2791 return Py_None;
2792 default:
2793 PyErr_SetString(PyExc_TypeError, "qiflush requires 0 or 1 arguments");
2794 return NULL;
2795 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002796}
2797
Guido van Rossumd8faa362007-04-27 19:54:29 +00002798/* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES
2799 * and _curses.COLS */
Benjamin Petersonfea6a942008-07-02 16:11:42 +00002800#if defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002801static int
2802update_lines_cols(void)
2803{
Victor Stinner26486ea2010-05-15 22:23:53 +00002804 PyObject *o;
2805 PyObject *m = PyImport_ImportModuleNoBlock("curses");
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002806 _Py_IDENTIFIER(LINES);
2807 _Py_IDENTIFIER(COLS);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002808
Victor Stinner26486ea2010-05-15 22:23:53 +00002809 if (!m)
2810 return 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002811
Victor Stinner26486ea2010-05-15 22:23:53 +00002812 o = PyLong_FromLong(LINES);
2813 if (!o) {
2814 Py_DECREF(m);
2815 return 0;
2816 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002817 if (_PyObject_SetAttrId(m, &PyId_LINES, o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002818 Py_DECREF(m);
2819 Py_DECREF(o);
2820 return 0;
2821 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002822 /* PyId_LINES.object will be initialized here. */
2823 if (PyDict_SetItem(ModDict, PyId_LINES.object, o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002824 Py_DECREF(m);
2825 Py_DECREF(o);
2826 return 0;
2827 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002828 Py_DECREF(o);
Victor Stinner26486ea2010-05-15 22:23:53 +00002829 o = PyLong_FromLong(COLS);
2830 if (!o) {
2831 Py_DECREF(m);
2832 return 0;
2833 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002834 if (_PyObject_SetAttrId(m, &PyId_COLS, o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002835 Py_DECREF(m);
2836 Py_DECREF(o);
2837 return 0;
2838 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002839 if (PyDict_SetItem(ModDict, PyId_COLS.object, o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002840 Py_DECREF(m);
2841 Py_DECREF(o);
2842 return 0;
2843 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002844 Py_DECREF(o);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002845 Py_DECREF(m);
Victor Stinner26486ea2010-05-15 22:23:53 +00002846 return 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002847}
Benjamin Petersonfea6a942008-07-02 16:11:42 +00002848#endif
Guido van Rossumd8faa362007-04-27 19:54:29 +00002849
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002850#ifdef HAVE_CURSES_RESIZETERM
2851static PyObject *
2852PyCurses_ResizeTerm(PyObject *self, PyObject *args)
2853{
Victor Stinner26486ea2010-05-15 22:23:53 +00002854 int lines;
2855 int columns;
2856 PyObject *result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002857
Victor Stinner26486ea2010-05-15 22:23:53 +00002858 PyCursesInitialised;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002859
Victor Stinner26486ea2010-05-15 22:23:53 +00002860 if (!PyArg_ParseTuple(args,"ii:resizeterm", &lines, &columns))
2861 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002862
Victor Stinner26486ea2010-05-15 22:23:53 +00002863 result = PyCursesCheckERR(resizeterm(lines, columns), "resizeterm");
2864 if (!result)
2865 return NULL;
2866 if (!update_lines_cols())
2867 return NULL;
2868 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002869}
2870
2871#endif
2872
2873#ifdef HAVE_CURSES_RESIZE_TERM
2874static PyObject *
2875PyCurses_Resize_Term(PyObject *self, PyObject *args)
2876{
Victor Stinner26486ea2010-05-15 22:23:53 +00002877 int lines;
2878 int columns;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002879
Victor Stinner26486ea2010-05-15 22:23:53 +00002880 PyObject *result;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002881
Victor Stinner26486ea2010-05-15 22:23:53 +00002882 PyCursesInitialised;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002883
Victor Stinner26486ea2010-05-15 22:23:53 +00002884 if (!PyArg_ParseTuple(args,"ii:resize_term", &lines, &columns))
2885 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002886
Victor Stinner26486ea2010-05-15 22:23:53 +00002887 result = PyCursesCheckERR(resize_term(lines, columns), "resize_term");
2888 if (!result)
2889 return NULL;
2890 if (!update_lines_cols())
2891 return NULL;
2892 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002893}
2894#endif /* HAVE_CURSES_RESIZE_TERM */
2895
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002896static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002897PyCurses_setsyx(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002898{
Victor Stinner26486ea2010-05-15 22:23:53 +00002899 int y,x;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002900
Victor Stinner26486ea2010-05-15 22:23:53 +00002901 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002902
Victor Stinner26486ea2010-05-15 22:23:53 +00002903 if (PyTuple_Size(args)!=2) {
2904 PyErr_SetString(PyExc_TypeError, "setsyx requires 2 arguments");
2905 return NULL;
2906 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002907
Victor Stinner26486ea2010-05-15 22:23:53 +00002908 if (!PyArg_ParseTuple(args, "ii;y, x", &y, &x)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002909
Victor Stinner26486ea2010-05-15 22:23:53 +00002910 setsyx(y,x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002911
Victor Stinner26486ea2010-05-15 22:23:53 +00002912 Py_INCREF(Py_None);
2913 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +00002914}
2915
2916static PyObject *
Martin v. Löwisc0e16712002-01-17 23:08:27 +00002917PyCurses_Start_Color(PyObject *self)
Guido van Rossumf6971e21994-08-30 12:25:20 +00002918{
Victor Stinner26486ea2010-05-15 22:23:53 +00002919 int code;
2920 PyObject *c, *cp;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002921
Victor Stinner26486ea2010-05-15 22:23:53 +00002922 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002923
Victor Stinner26486ea2010-05-15 22:23:53 +00002924 code = start_color();
2925 if (code != ERR) {
2926 initialisedcolors = TRUE;
2927 c = PyLong_FromLong((long) COLORS);
2928 PyDict_SetItemString(ModDict, "COLORS", c);
2929 Py_DECREF(c);
2930 cp = PyLong_FromLong((long) COLOR_PAIRS);
2931 PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp);
2932 Py_DECREF(cp);
2933 Py_INCREF(Py_None);
2934 return Py_None;
2935 } else {
2936 PyErr_SetString(PyCursesError, "start_color() returned ERR");
2937 return NULL;
2938 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002939}
2940
2941static PyObject *
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002942PyCurses_tigetflag(PyObject *self, PyObject *args)
2943{
Victor Stinner26486ea2010-05-15 22:23:53 +00002944 char *capname;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002945
Victor Stinner26486ea2010-05-15 22:23:53 +00002946 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002947
Victor Stinner26486ea2010-05-15 22:23:53 +00002948 if (!PyArg_ParseTuple(args, "s", &capname))
2949 return NULL;
2950
2951 return PyLong_FromLong( (long) tigetflag( capname ) );
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002952}
2953
2954static PyObject *
2955PyCurses_tigetnum(PyObject *self, PyObject *args)
2956{
Victor Stinner26486ea2010-05-15 22:23:53 +00002957 char *capname;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002958
Victor Stinner26486ea2010-05-15 22:23:53 +00002959 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002960
Victor Stinner26486ea2010-05-15 22:23:53 +00002961 if (!PyArg_ParseTuple(args, "s", &capname))
2962 return NULL;
2963
2964 return PyLong_FromLong( (long) tigetnum( capname ) );
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002965}
2966
2967static PyObject *
2968PyCurses_tigetstr(PyObject *self, PyObject *args)
2969{
Victor Stinner26486ea2010-05-15 22:23:53 +00002970 char *capname;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002971
Victor Stinner26486ea2010-05-15 22:23:53 +00002972 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002973
Victor Stinner26486ea2010-05-15 22:23:53 +00002974 if (!PyArg_ParseTuple(args, "s", &capname))
2975 return NULL;
2976
2977 capname = tigetstr( capname );
2978 if (capname == 0 || capname == (char*) -1) {
2979 Py_INCREF(Py_None);
2980 return Py_None;
2981 }
2982 return PyBytes_FromString( capname );
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002983}
2984
2985static PyObject *
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00002986PyCurses_tparm(PyObject *self, PyObject *args)
2987{
Victor Stinner26486ea2010-05-15 22:23:53 +00002988 char* fmt;
2989 char* result = NULL;
2990 int i1=0,i2=0,i3=0,i4=0,i5=0,i6=0,i7=0,i8=0,i9=0;
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00002991
Victor Stinner26486ea2010-05-15 22:23:53 +00002992 PyCursesSetupTermCalled;
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00002993
Victor Stinner26621332011-11-02 23:45:29 +01002994 if (!PyArg_ParseTuple(args, "y|iiiiiiiii:tparm",
Victor Stinner26486ea2010-05-15 22:23:53 +00002995 &fmt, &i1, &i2, &i3, &i4,
2996 &i5, &i6, &i7, &i8, &i9)) {
2997 return NULL;
2998 }
Michael W. Hudson21007982002-11-21 14:17:51 +00002999
Victor Stinner26486ea2010-05-15 22:23:53 +00003000 result = tparm(fmt,i1,i2,i3,i4,i5,i6,i7,i8,i9);
3001 if (!result) {
3002 PyErr_SetString(PyCursesError, "tparm() returned NULL");
3003 return NULL;
3004 }
Michael W. Hudson21007982002-11-21 14:17:51 +00003005
Victor Stinner26486ea2010-05-15 22:23:53 +00003006 return PyBytes_FromString(result);
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00003007}
3008
3009static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00003010PyCurses_TypeAhead(PyObject *self, PyObject *args)
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00003011{
Victor Stinner26486ea2010-05-15 22:23:53 +00003012 int fd;
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00003013
Victor Stinner26486ea2010-05-15 22:23:53 +00003014 PyCursesInitialised;
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00003015
Victor Stinner26486ea2010-05-15 22:23:53 +00003016 if (!PyArg_ParseTuple(args,"i;fd",&fd)) return NULL;
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00003017
Victor Stinner26486ea2010-05-15 22:23:53 +00003018 return PyCursesCheckERR(typeahead( fd ), "typeahead");
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00003019}
3020
3021static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00003022PyCurses_UnCtrl(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003023{
Victor Stinner26486ea2010-05-15 22:23:53 +00003024 PyObject *temp;
3025 chtype ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003026
Victor Stinner26486ea2010-05-15 22:23:53 +00003027 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003028
Victor Stinner26486ea2010-05-15 22:23:53 +00003029 if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003030
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003031 if (!PyCurses_ConvertToChtype(NULL, temp, &ch))
Victor Stinner26486ea2010-05-15 22:23:53 +00003032 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003033
Victor Stinner26486ea2010-05-15 22:23:53 +00003034 return PyBytes_FromString(unctrl(ch));
Guido van Rossumf6971e21994-08-30 12:25:20 +00003035}
3036
3037static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00003038PyCurses_UngetCh(PyObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00003039{
Victor Stinner26486ea2010-05-15 22:23:53 +00003040 PyObject *temp;
3041 chtype ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003042
Victor Stinner26486ea2010-05-15 22:23:53 +00003043 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003044
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003045 if (!PyArg_ParseTuple(args,"O;ch or int",&temp))
Victor Stinner26486ea2010-05-15 22:23:53 +00003046 return NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003047
3048 if (!PyCurses_ConvertToChtype(NULL, temp, &ch))
3049 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003050
Victor Stinner26486ea2010-05-15 22:23:53 +00003051 return PyCursesCheckERR(ungetch(ch), "ungetch");
Guido van Rossumf6971e21994-08-30 12:25:20 +00003052}
3053
Victor Stinner71e44cb2011-09-06 01:53:03 +02003054#ifdef HAVE_NCURSESW
3055/* Convert an object to a character (wchar_t):
3056
3057 - int
3058 - str of length 1
3059
3060 Return 1 on success, 0 on error. */
3061static int
3062PyCurses_ConvertToWchar_t(PyObject *obj,
3063 wchar_t *wch)
3064{
3065 if (PyUnicode_Check(obj)) {
3066 wchar_t buffer[2];
3067 if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) {
3068 PyErr_Format(PyExc_TypeError,
3069 "expect bytes or str of length 1, or int, "
3070 "got a str of length %zi",
Victor Stinnerc4f281e2011-10-11 22:11:42 +02003071 PyUnicode_GET_LENGTH(obj));
Victor Stinner71e44cb2011-09-06 01:53:03 +02003072 return 0;
3073 }
3074 *wch = buffer[0];
3075 return 2;
3076 }
3077 else if (PyLong_CheckExact(obj)) {
3078 long value;
3079 int overflow;
3080 value = PyLong_AsLongAndOverflow(obj, &overflow);
3081 if (overflow) {
3082 PyErr_SetString(PyExc_OverflowError,
3083 "int doesn't fit in long");
3084 return 0;
3085 }
3086 *wch = (wchar_t)value;
3087 if ((long)*wch != value) {
3088 PyErr_Format(PyExc_OverflowError,
3089 "character doesn't fit in wchar_t");
3090 return 0;
3091 }
3092 return 1;
3093 }
3094 else {
3095 PyErr_Format(PyExc_TypeError,
3096 "expect bytes or str of length 1, or int, got %s",
3097 Py_TYPE(obj)->tp_name);
3098 return 0;
3099 }
3100}
3101
3102static PyObject *
3103PyCurses_Unget_Wch(PyObject *self, PyObject *args)
3104{
3105 PyObject *obj;
3106 wchar_t wch;
3107
3108 PyCursesInitialised;
3109
3110 if (!PyArg_ParseTuple(args,"O", &obj))
3111 return NULL;
3112
3113 if (!PyCurses_ConvertToWchar_t(obj, &wch))
3114 return NULL;
3115 return PyCursesCheckERR(unget_wch(wch), "unget_wch");
3116}
3117#endif
3118
Guido van Rossumf6971e21994-08-30 12:25:20 +00003119static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00003120PyCurses_Use_Env(PyObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00003121{
Victor Stinner26486ea2010-05-15 22:23:53 +00003122 int flag;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003123
Victor Stinner26486ea2010-05-15 22:23:53 +00003124 switch(PyTuple_Size(args)) {
3125 case 1:
3126 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&flag))
3127 return NULL;
3128 break;
3129 default:
3130 PyErr_SetString(PyExc_TypeError, "use_env requires 1 argument");
3131 return NULL;
3132 }
3133 use_env(flag);
3134 Py_INCREF(Py_None);
3135 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +00003136}
3137
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00003138#ifndef STRICT_SYSV_CURSES
3139static PyObject *
3140PyCurses_Use_Default_Colors(PyObject *self)
3141{
Victor Stinner26486ea2010-05-15 22:23:53 +00003142 int code;
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00003143
Victor Stinner26486ea2010-05-15 22:23:53 +00003144 PyCursesInitialised;
3145 PyCursesInitialisedColor;
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00003146
Victor Stinner26486ea2010-05-15 22:23:53 +00003147 code = use_default_colors();
3148 if (code != ERR) {
3149 Py_INCREF(Py_None);
3150 return Py_None;
3151 } else {
3152 PyErr_SetString(PyCursesError, "use_default_colors() returned ERR");
3153 return NULL;
3154 }
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00003155}
3156#endif /* STRICT_SYSV_CURSES */
3157
Guido van Rossumf6971e21994-08-30 12:25:20 +00003158/* List of functions defined in the module */
3159
3160static PyMethodDef PyCurses_methods[] = {
Victor Stinner26486ea2010-05-15 22:23:53 +00003161 {"baudrate", (PyCFunction)PyCurses_baudrate, METH_NOARGS},
3162 {"beep", (PyCFunction)PyCurses_beep, METH_NOARGS},
3163 {"can_change_color", (PyCFunction)PyCurses_can_change_color, METH_NOARGS},
3164 {"cbreak", (PyCFunction)PyCurses_cbreak, METH_VARARGS},
3165 {"color_content", (PyCFunction)PyCurses_Color_Content, METH_VARARGS},
3166 {"color_pair", (PyCFunction)PyCurses_color_pair, METH_VARARGS},
3167 {"curs_set", (PyCFunction)PyCurses_Curs_Set, METH_VARARGS},
3168 {"def_prog_mode", (PyCFunction)PyCurses_def_prog_mode, METH_NOARGS},
3169 {"def_shell_mode", (PyCFunction)PyCurses_def_shell_mode, METH_NOARGS},
3170 {"delay_output", (PyCFunction)PyCurses_Delay_Output, METH_VARARGS},
3171 {"doupdate", (PyCFunction)PyCurses_doupdate, METH_NOARGS},
3172 {"echo", (PyCFunction)PyCurses_echo, METH_VARARGS},
3173 {"endwin", (PyCFunction)PyCurses_endwin, METH_NOARGS},
3174 {"erasechar", (PyCFunction)PyCurses_EraseChar, METH_NOARGS},
3175 {"filter", (PyCFunction)PyCurses_filter, METH_NOARGS},
3176 {"flash", (PyCFunction)PyCurses_flash, METH_NOARGS},
3177 {"flushinp", (PyCFunction)PyCurses_flushinp, METH_NOARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003178#ifdef NCURSES_MOUSE_VERSION
Victor Stinner26486ea2010-05-15 22:23:53 +00003179 {"getmouse", (PyCFunction)PyCurses_GetMouse, METH_NOARGS},
3180 {"ungetmouse", (PyCFunction)PyCurses_UngetMouse, METH_VARARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003181#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003182 {"getsyx", (PyCFunction)PyCurses_getsyx, METH_NOARGS},
3183 {"getwin", (PyCFunction)PyCurses_GetWin, METH_O},
3184 {"has_colors", (PyCFunction)PyCurses_has_colors, METH_NOARGS},
3185 {"has_ic", (PyCFunction)PyCurses_has_ic, METH_NOARGS},
3186 {"has_il", (PyCFunction)PyCurses_has_il, METH_NOARGS},
Andrew M. Kuchlingbf680262000-07-25 12:37:41 +00003187#ifndef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00003188 {"has_key", (PyCFunction)PyCurses_has_key, METH_VARARGS},
Guido van Rossumf6971e21994-08-30 12:25:20 +00003189#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003190 {"halfdelay", (PyCFunction)PyCurses_HalfDelay, METH_VARARGS},
3191 {"init_color", (PyCFunction)PyCurses_Init_Color, METH_VARARGS},
3192 {"init_pair", (PyCFunction)PyCurses_Init_Pair, METH_VARARGS},
3193 {"initscr", (PyCFunction)PyCurses_InitScr, METH_NOARGS},
3194 {"intrflush", (PyCFunction)PyCurses_IntrFlush, METH_VARARGS},
3195 {"isendwin", (PyCFunction)PyCurses_isendwin, METH_NOARGS},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003196#ifdef HAVE_CURSES_IS_TERM_RESIZED
Victor Stinner26486ea2010-05-15 22:23:53 +00003197 {"is_term_resized", (PyCFunction)PyCurses_Is_Term_Resized, METH_VARARGS},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003198#endif
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00003199#if !defined(__NetBSD__)
Victor Stinner26486ea2010-05-15 22:23:53 +00003200 {"keyname", (PyCFunction)PyCurses_KeyName, METH_VARARGS},
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00003201#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003202 {"killchar", (PyCFunction)PyCurses_KillChar, METH_NOARGS},
3203 {"longname", (PyCFunction)PyCurses_longname, METH_NOARGS},
3204 {"meta", (PyCFunction)PyCurses_Meta, METH_VARARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003205#ifdef NCURSES_MOUSE_VERSION
Victor Stinner26486ea2010-05-15 22:23:53 +00003206 {"mouseinterval", (PyCFunction)PyCurses_MouseInterval, METH_VARARGS},
3207 {"mousemask", (PyCFunction)PyCurses_MouseMask, METH_VARARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003208#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003209 {"napms", (PyCFunction)PyCurses_Napms, METH_VARARGS},
3210 {"newpad", (PyCFunction)PyCurses_NewPad, METH_VARARGS},
3211 {"newwin", (PyCFunction)PyCurses_NewWindow, METH_VARARGS},
3212 {"nl", (PyCFunction)PyCurses_nl, METH_VARARGS},
3213 {"nocbreak", (PyCFunction)PyCurses_nocbreak, METH_NOARGS},
3214 {"noecho", (PyCFunction)PyCurses_noecho, METH_NOARGS},
3215 {"nonl", (PyCFunction)PyCurses_nonl, METH_NOARGS},
3216 {"noqiflush", (PyCFunction)PyCurses_noqiflush, METH_NOARGS},
3217 {"noraw", (PyCFunction)PyCurses_noraw, METH_NOARGS},
3218 {"pair_content", (PyCFunction)PyCurses_Pair_Content, METH_VARARGS},
3219 {"pair_number", (PyCFunction)PyCurses_pair_number, METH_VARARGS},
3220 {"putp", (PyCFunction)PyCurses_Putp, METH_VARARGS},
3221 {"qiflush", (PyCFunction)PyCurses_QiFlush, METH_VARARGS},
3222 {"raw", (PyCFunction)PyCurses_raw, METH_VARARGS},
3223 {"reset_prog_mode", (PyCFunction)PyCurses_reset_prog_mode, METH_NOARGS},
3224 {"reset_shell_mode", (PyCFunction)PyCurses_reset_shell_mode, METH_NOARGS},
3225 {"resetty", (PyCFunction)PyCurses_resetty, METH_NOARGS},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003226#ifdef HAVE_CURSES_RESIZETERM
Victor Stinner26486ea2010-05-15 22:23:53 +00003227 {"resizeterm", (PyCFunction)PyCurses_ResizeTerm, METH_VARARGS},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003228#endif
3229#ifdef HAVE_CURSES_RESIZE_TERM
Victor Stinner26486ea2010-05-15 22:23:53 +00003230 {"resize_term", (PyCFunction)PyCurses_Resize_Term, METH_VARARGS},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003231#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003232 {"savetty", (PyCFunction)PyCurses_savetty, METH_NOARGS},
3233 {"setsyx", (PyCFunction)PyCurses_setsyx, METH_VARARGS},
3234 {"setupterm", (PyCFunction)PyCurses_setupterm,
3235 METH_VARARGS|METH_KEYWORDS},
3236 {"start_color", (PyCFunction)PyCurses_Start_Color, METH_NOARGS},
3237 {"termattrs", (PyCFunction)PyCurses_termattrs, METH_NOARGS},
3238 {"termname", (PyCFunction)PyCurses_termname, METH_NOARGS},
3239 {"tigetflag", (PyCFunction)PyCurses_tigetflag, METH_VARARGS},
3240 {"tigetnum", (PyCFunction)PyCurses_tigetnum, METH_VARARGS},
3241 {"tigetstr", (PyCFunction)PyCurses_tigetstr, METH_VARARGS},
3242 {"tparm", (PyCFunction)PyCurses_tparm, METH_VARARGS},
3243 {"typeahead", (PyCFunction)PyCurses_TypeAhead, METH_VARARGS},
3244 {"unctrl", (PyCFunction)PyCurses_UnCtrl, METH_VARARGS},
3245 {"ungetch", (PyCFunction)PyCurses_UngetCh, METH_VARARGS},
Victor Stinner71e44cb2011-09-06 01:53:03 +02003246#ifdef HAVE_NCURSESW
3247 {"unget_wch", (PyCFunction)PyCurses_Unget_Wch, METH_VARARGS},
3248#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003249 {"use_env", (PyCFunction)PyCurses_Use_Env, METH_VARARGS},
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00003250#ifndef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00003251 {"use_default_colors", (PyCFunction)PyCurses_Use_Default_Colors, METH_NOARGS},
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00003252#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003253 {NULL, NULL} /* sentinel */
Guido van Rossumf6971e21994-08-30 12:25:20 +00003254};
3255
3256/* Initialization function for the module */
3257
Martin v. Löwis1a214512008-06-11 05:26:20 +00003258
3259static struct PyModuleDef _cursesmodule = {
Victor Stinner26486ea2010-05-15 22:23:53 +00003260 PyModuleDef_HEAD_INIT,
3261 "_curses",
3262 NULL,
3263 -1,
3264 PyCurses_methods,
3265 NULL,
3266 NULL,
3267 NULL,
3268 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00003269};
3270
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003271PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003272PyInit__curses(void)
Guido van Rossumf6971e21994-08-30 12:25:20 +00003273{
Victor Stinner26486ea2010-05-15 22:23:53 +00003274 PyObject *m, *d, *v, *c_api_object;
3275 static void *PyCurses_API[PyCurses_API_pointers];
Andrew M. Kuchling32552682000-12-22 21:52:27 +00003276
Victor Stinner26486ea2010-05-15 22:23:53 +00003277 /* Initialize object type */
3278 if (PyType_Ready(&PyCursesWindow_Type) < 0)
3279 return NULL;
Guido van Rossum6915c4d2001-01-19 00:28:08 +00003280
Victor Stinner26486ea2010-05-15 22:23:53 +00003281 /* Initialize the C API pointer array */
3282 PyCurses_API[0] = (void *)&PyCursesWindow_Type;
3283 PyCurses_API[1] = (void *)func_PyCursesSetupTermCalled;
3284 PyCurses_API[2] = (void *)func_PyCursesInitialised;
3285 PyCurses_API[3] = (void *)func_PyCursesInitialisedColor;
Guido van Rossumf6971e21994-08-30 12:25:20 +00003286
Victor Stinner26486ea2010-05-15 22:23:53 +00003287 /* Create the module and add the functions */
3288 m = PyModule_Create(&_cursesmodule);
3289 if (m == NULL)
3290 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +00003291
Victor Stinner26486ea2010-05-15 22:23:53 +00003292 /* Add some symbolic constants to the module */
3293 d = PyModule_GetDict(m);
3294 if (d == NULL)
3295 return NULL;
3296 ModDict = d; /* For PyCurses_InitScr to use later */
Andrew M. Kuchling32552682000-12-22 21:52:27 +00003297
Victor Stinner26486ea2010-05-15 22:23:53 +00003298 /* Add a capsule for the C API */
3299 c_api_object = PyCapsule_New(PyCurses_API, PyCurses_CAPSULE_NAME, NULL);
3300 PyDict_SetItemString(d, "_C_API", c_api_object);
3301 Py_DECREF(c_api_object);
Guido van Rossumfbea2f31994-08-31 22:05:27 +00003302
Victor Stinner26486ea2010-05-15 22:23:53 +00003303 /* For exception curses.error */
3304 PyCursesError = PyErr_NewException("_curses.error", NULL, NULL);
3305 PyDict_SetItemString(d, "error", PyCursesError);
Guido van Rossumfbea2f31994-08-31 22:05:27 +00003306
Victor Stinner26486ea2010-05-15 22:23:53 +00003307 /* Make the version available */
3308 v = PyBytes_FromString(PyCursesVersion);
3309 PyDict_SetItemString(d, "version", v);
3310 PyDict_SetItemString(d, "__version__", v);
3311 Py_DECREF(v);
Guido van Rossumf6971e21994-08-30 12:25:20 +00003312
Victor Stinner26486ea2010-05-15 22:23:53 +00003313 SetDictInt("ERR", ERR);
3314 SetDictInt("OK", OK);
Andrew M. Kuchlingcaefb372000-12-26 15:57:01 +00003315
Victor Stinner26486ea2010-05-15 22:23:53 +00003316 /* Here are some attributes you can add to chars to print */
3317
3318 SetDictInt("A_ATTRIBUTES", A_ATTRIBUTES);
3319 SetDictInt("A_NORMAL", A_NORMAL);
3320 SetDictInt("A_STANDOUT", A_STANDOUT);
3321 SetDictInt("A_UNDERLINE", A_UNDERLINE);
3322 SetDictInt("A_REVERSE", A_REVERSE);
3323 SetDictInt("A_BLINK", A_BLINK);
3324 SetDictInt("A_DIM", A_DIM);
3325 SetDictInt("A_BOLD", A_BOLD);
3326 SetDictInt("A_ALTCHARSET", A_ALTCHARSET);
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00003327#if !defined(__NetBSD__)
Victor Stinner26486ea2010-05-15 22:23:53 +00003328 SetDictInt("A_INVIS", A_INVIS);
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00003329#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003330 SetDictInt("A_PROTECT", A_PROTECT);
3331 SetDictInt("A_CHARTEXT", A_CHARTEXT);
3332 SetDictInt("A_COLOR", A_COLOR);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003333
Victor Stinner26486ea2010-05-15 22:23:53 +00003334 /* The following are never available with strict SYSV curses */
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003335#ifdef A_HORIZONTAL
Victor Stinner26486ea2010-05-15 22:23:53 +00003336 SetDictInt("A_HORIZONTAL", A_HORIZONTAL);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003337#endif
3338#ifdef A_LEFT
Victor Stinner26486ea2010-05-15 22:23:53 +00003339 SetDictInt("A_LEFT", A_LEFT);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003340#endif
3341#ifdef A_LOW
Victor Stinner26486ea2010-05-15 22:23:53 +00003342 SetDictInt("A_LOW", A_LOW);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003343#endif
3344#ifdef A_RIGHT
Victor Stinner26486ea2010-05-15 22:23:53 +00003345 SetDictInt("A_RIGHT", A_RIGHT);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003346#endif
3347#ifdef A_TOP
Victor Stinner26486ea2010-05-15 22:23:53 +00003348 SetDictInt("A_TOP", A_TOP);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003349#endif
3350#ifdef A_VERTICAL
Victor Stinner26486ea2010-05-15 22:23:53 +00003351 SetDictInt("A_VERTICAL", A_VERTICAL);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003352#endif
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00003353
Victor Stinner26486ea2010-05-15 22:23:53 +00003354 SetDictInt("COLOR_BLACK", COLOR_BLACK);
3355 SetDictInt("COLOR_RED", COLOR_RED);
3356 SetDictInt("COLOR_GREEN", COLOR_GREEN);
3357 SetDictInt("COLOR_YELLOW", COLOR_YELLOW);
3358 SetDictInt("COLOR_BLUE", COLOR_BLUE);
3359 SetDictInt("COLOR_MAGENTA", COLOR_MAGENTA);
3360 SetDictInt("COLOR_CYAN", COLOR_CYAN);
3361 SetDictInt("COLOR_WHITE", COLOR_WHITE);
Guido van Rossumf6971e21994-08-30 12:25:20 +00003362
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003363#ifdef NCURSES_MOUSE_VERSION
Victor Stinner26486ea2010-05-15 22:23:53 +00003364 /* Mouse-related constants */
3365 SetDictInt("BUTTON1_PRESSED", BUTTON1_PRESSED);
3366 SetDictInt("BUTTON1_RELEASED", BUTTON1_RELEASED);
3367 SetDictInt("BUTTON1_CLICKED", BUTTON1_CLICKED);
3368 SetDictInt("BUTTON1_DOUBLE_CLICKED", BUTTON1_DOUBLE_CLICKED);
3369 SetDictInt("BUTTON1_TRIPLE_CLICKED", BUTTON1_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003370
Victor Stinner26486ea2010-05-15 22:23:53 +00003371 SetDictInt("BUTTON2_PRESSED", BUTTON2_PRESSED);
3372 SetDictInt("BUTTON2_RELEASED", BUTTON2_RELEASED);
3373 SetDictInt("BUTTON2_CLICKED", BUTTON2_CLICKED);
3374 SetDictInt("BUTTON2_DOUBLE_CLICKED", BUTTON2_DOUBLE_CLICKED);
3375 SetDictInt("BUTTON2_TRIPLE_CLICKED", BUTTON2_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003376
Victor Stinner26486ea2010-05-15 22:23:53 +00003377 SetDictInt("BUTTON3_PRESSED", BUTTON3_PRESSED);
3378 SetDictInt("BUTTON3_RELEASED", BUTTON3_RELEASED);
3379 SetDictInt("BUTTON3_CLICKED", BUTTON3_CLICKED);
3380 SetDictInt("BUTTON3_DOUBLE_CLICKED", BUTTON3_DOUBLE_CLICKED);
3381 SetDictInt("BUTTON3_TRIPLE_CLICKED", BUTTON3_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003382
Victor Stinner26486ea2010-05-15 22:23:53 +00003383 SetDictInt("BUTTON4_PRESSED", BUTTON4_PRESSED);
3384 SetDictInt("BUTTON4_RELEASED", BUTTON4_RELEASED);
3385 SetDictInt("BUTTON4_CLICKED", BUTTON4_CLICKED);
3386 SetDictInt("BUTTON4_DOUBLE_CLICKED", BUTTON4_DOUBLE_CLICKED);
3387 SetDictInt("BUTTON4_TRIPLE_CLICKED", BUTTON4_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003388
Victor Stinner26486ea2010-05-15 22:23:53 +00003389 SetDictInt("BUTTON_SHIFT", BUTTON_SHIFT);
3390 SetDictInt("BUTTON_CTRL", BUTTON_CTRL);
3391 SetDictInt("BUTTON_ALT", BUTTON_ALT);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003392
Victor Stinner26486ea2010-05-15 22:23:53 +00003393 SetDictInt("ALL_MOUSE_EVENTS", ALL_MOUSE_EVENTS);
3394 SetDictInt("REPORT_MOUSE_POSITION", REPORT_MOUSE_POSITION);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003395#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003396 /* Now set everything up for KEY_ variables */
3397 {
3398 int key;
3399 char *key_n;
3400 char *key_n2;
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00003401#if !defined(__NetBSD__)
Victor Stinner26486ea2010-05-15 22:23:53 +00003402 for (key=KEY_MIN;key < KEY_MAX; key++) {
3403 key_n = (char *)keyname(key);
3404 if (key_n == NULL || strcmp(key_n,"UNKNOWN KEY")==0)
3405 continue;
3406 if (strncmp(key_n,"KEY_F(",6)==0) {
3407 char *p1, *p2;
3408 key_n2 = malloc(strlen(key_n)+1);
3409 if (!key_n2) {
3410 PyErr_NoMemory();
3411 break;
3412 }
3413 p1 = key_n;
3414 p2 = key_n2;
3415 while (*p1) {
3416 if (*p1 != '(' && *p1 != ')') {
3417 *p2 = *p1;
3418 p2++;
3419 }
3420 p1++;
3421 }
3422 *p2 = (char)0;
3423 } else
3424 key_n2 = key_n;
3425 SetDictInt(key_n2,key);
3426 if (key_n2 != key_n)
3427 free(key_n2);
3428 }
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00003429#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003430 SetDictInt("KEY_MIN", KEY_MIN);
3431 SetDictInt("KEY_MAX", KEY_MAX);
3432 }
3433 return m;
Guido van Rossumf6971e21994-08-30 12:25:20 +00003434}