blob: 960752cce373d65673d8b7884b4d7dc455a7cb60 [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
Larry Hastings61272b72014-01-07 12:41:53 -0800137/*[clinic input]
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800138module curses
Larry Hastingsc2047262014-01-25 20:43:29 -0800139class curses.window "PyCursesWindowObject *" "&PyCursesWindow_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800140[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800141/*[clinic end generated code: output=da39a3ee5e6b4b0d input=88c860abdbb50e0c]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800142
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300143#include "clinic/_cursesmodule.c.h"
144
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000145/* Definition of exception curses.error */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000146
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000147static PyObject *PyCursesError;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000148
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +0000149/* Tells whether setupterm() has been called to initialise terminfo. */
150static int initialised_setupterm = FALSE;
151
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000152/* Tells whether initscr() has been called to initialise curses. */
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000153static int initialised = FALSE;
154
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000155/* Tells whether start_color() has been called to initialise color usage. */
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000156static int initialisedcolors = FALSE;
157
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100158static char *screen_encoding = NULL;
159
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000160/* Utility Macros */
Victor Stinner26486ea2010-05-15 22:23:53 +0000161#define PyCursesSetupTermCalled \
162 if (initialised_setupterm != TRUE) { \
163 PyErr_SetString(PyCursesError, \
164 "must call (at least) setupterm() first"); \
165 return 0; }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +0000166
Victor Stinner26486ea2010-05-15 22:23:53 +0000167#define PyCursesInitialised \
168 if (initialised != TRUE) { \
169 PyErr_SetString(PyCursesError, \
170 "must call initscr() first"); \
171 return 0; }
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000172
Victor Stinner26486ea2010-05-15 22:23:53 +0000173#define PyCursesInitialisedColor \
174 if (initialisedcolors != TRUE) { \
175 PyErr_SetString(PyCursesError, \
176 "must call start_color() first"); \
177 return 0; }
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000178
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000179/* Utility Functions */
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000180
Guido van Rossum85738471995-02-17 13:50:17 +0000181/*
Victor Stinner26486ea2010-05-15 22:23:53 +0000182 * Check the return code from a curses function and return None
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000183 * or raise an exception as appropriate. These are exported using the
Victor Stinner26486ea2010-05-15 22:23:53 +0000184 * capsule API.
Guido van Rossum85738471995-02-17 13:50:17 +0000185 */
186
187static PyObject *
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100188PyCursesCheckERR(int code, const char *fname)
Guido van Rossum85738471995-02-17 13:50:17 +0000189{
Victor Stinner26486ea2010-05-15 22:23:53 +0000190 if (code != ERR) {
191 Py_INCREF(Py_None);
192 return Py_None;
Guido van Rossum85738471995-02-17 13:50:17 +0000193 } else {
Victor Stinner26486ea2010-05-15 22:23:53 +0000194 if (fname == NULL) {
195 PyErr_SetString(PyCursesError, catchall_ERR);
196 } else {
197 PyErr_Format(PyCursesError, "%s() returned ERR", fname);
198 }
199 return NULL;
Guido van Rossum85738471995-02-17 13:50:17 +0000200 }
Guido van Rossum85738471995-02-17 13:50:17 +0000201}
202
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100203/* Convert an object to a byte (an integer of type chtype):
204
205 - int
206 - bytes of length 1
207 - str of length 1
208
209 Return 1 on success, 0 on error (invalid type or integer overflow). */
Victor Stinner26486ea2010-05-15 22:23:53 +0000210static int
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100211PyCurses_ConvertToChtype(PyCursesWindowObject *win, PyObject *obj, chtype *ch)
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000212{
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100213 long value;
214 if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) {
215 value = (unsigned char)PyBytes_AsString(obj)[0];
216 }
217 else if (PyUnicode_Check(obj)) {
218 if (PyUnicode_GetLength(obj) != 1) {
219 PyErr_Format(PyExc_TypeError,
220 "expect bytes or str of length 1, or int, "
221 "got a str of length %zi",
222 PyUnicode_GET_LENGTH(obj));
Victor Stinner26486ea2010-05-15 22:23:53 +0000223 return 0;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100224 }
225 value = PyUnicode_READ_CHAR(obj, 0);
226 if (128 < value) {
227 PyObject *bytes;
228 const char *encoding;
229 if (win)
230 encoding = win->encoding;
231 else
232 encoding = screen_encoding;
233 bytes = PyUnicode_AsEncodedObject(obj, encoding, NULL);
234 if (bytes == NULL)
235 return 0;
236 if (PyBytes_GET_SIZE(bytes) == 1)
237 value = (unsigned char)PyBytes_AS_STRING(bytes)[0];
238 else
239 value = -1;
240 Py_DECREF(bytes);
241 if (value < 0)
242 goto overflow;
243 }
244 }
245 else if (PyLong_CheckExact(obj)) {
246 int long_overflow;
247 value = PyLong_AsLongAndOverflow(obj, &long_overflow);
248 if (long_overflow)
249 goto overflow;
250 }
251 else {
252 PyErr_Format(PyExc_TypeError,
253 "expect bytes or str of length 1, or int, got %s",
254 Py_TYPE(obj)->tp_name);
Victor Stinner26486ea2010-05-15 22:23:53 +0000255 return 0;
256 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100257 *ch = (chtype)value;
258 if ((long)*ch != value)
259 goto overflow;
Victor Stinner26486ea2010-05-15 22:23:53 +0000260 return 1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100261
262overflow:
263 PyErr_SetString(PyExc_OverflowError,
264 "byte doesn't fit in chtype");
265 return 0;
266}
267
268/* Convert an object to a byte (chtype) or a character (cchar_t):
269
270 - int
271 - bytes of length 1
272 - str of length 1
273
274 Return:
275
276 - 2 if obj is a character (written into *wch)
277 - 1 if obj is a byte (written into *ch)
278 - 0 on error: raise an exception */
279static int
280PyCurses_ConvertToCchar_t(PyCursesWindowObject *win, PyObject *obj,
281 chtype *ch
282#ifdef HAVE_NCURSESW
283 , cchar_t *wch
284#endif
285 )
286{
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100287 long value;
288#ifdef HAVE_NCURSESW
289 wchar_t buffer[2];
290#endif
291
292 if (PyUnicode_Check(obj)) {
293#ifdef HAVE_NCURSESW
294 if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) {
295 PyErr_Format(PyExc_TypeError,
296 "expect bytes or str of length 1, or int, "
297 "got a str of length %zi",
298 PyUnicode_GET_LENGTH(obj));
299 return 0;
300 }
301 memset(wch->chars, 0, sizeof(wch->chars));
302 wch->chars[0] = buffer[0];
303 return 2;
304#else
305 return PyCurses_ConvertToChtype(win, obj, ch);
306#endif
307 }
308 else if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) {
309 value = (unsigned char)PyBytes_AsString(obj)[0];
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100310 }
311 else if (PyLong_CheckExact(obj)) {
312 int overflow;
313 value = PyLong_AsLongAndOverflow(obj, &overflow);
314 if (overflow) {
315 PyErr_SetString(PyExc_OverflowError,
316 "int doesn't fit in long");
317 return 0;
318 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100319 }
320 else {
321 PyErr_Format(PyExc_TypeError,
322 "expect bytes or str of length 1, or int, got %s",
323 Py_TYPE(obj)->tp_name);
324 return 0;
325 }
Victor Stinner9f16e442012-09-01 15:00:34 +0200326
327 *ch = (chtype)value;
328 if ((long)*ch != value) {
329 PyErr_Format(PyExc_OverflowError,
330 "byte doesn't fit in chtype");
331 return 0;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100332 }
Victor Stinner9f16e442012-09-01 15:00:34 +0200333 return 1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100334}
335
336/* Convert an object to a byte string (char*) or a wide character string
337 (wchar_t*). Return:
338
339 - 2 if obj is a character string (written into *wch)
340 - 1 if obj is a byte string (written into *bytes)
341 - 0 on error: raise an exception */
342static int
343PyCurses_ConvertToString(PyCursesWindowObject *win, PyObject *obj,
344 PyObject **bytes, wchar_t **wstr)
345{
346 if (PyUnicode_Check(obj)) {
347#ifdef HAVE_NCURSESW
348 assert (wstr != NULL);
349 *wstr = PyUnicode_AsWideCharString(obj, NULL);
350 if (*wstr == NULL)
351 return 0;
352 return 2;
353#else
354 assert (wstr == NULL);
355 *bytes = PyUnicode_AsEncodedObject(obj, win->encoding, NULL);
356 if (*bytes == NULL)
357 return 0;
358 return 1;
359#endif
360 }
361 else if (PyBytes_Check(obj)) {
362 Py_INCREF(obj);
363 *bytes = obj;
364 return 1;
365 }
366
367 PyErr_Format(PyExc_TypeError, "expect bytes or str, got %s",
368 Py_TYPE(obj)->tp_name);
369 return 0;
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000370}
371
Benjamin Peterson21896a32010-03-21 22:03:03 +0000372/* Function versions of the 3 functions for testing whether curses has been
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000373 initialised or not. */
Victor Stinner26486ea2010-05-15 22:23:53 +0000374
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000375static int func_PyCursesSetupTermCalled(void)
376{
377 PyCursesSetupTermCalled;
378 return 1;
379}
380
381static int func_PyCursesInitialised(void)
382{
383 PyCursesInitialised;
384 return 1;
385}
386
387static int func_PyCursesInitialisedColor(void)
388{
389 PyCursesInitialisedColor;
390 return 1;
391}
392
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000393/*****************************************************************************
394 The Window Object
395******************************************************************************/
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000396
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000397/* Definition of the window type */
Guido van Rossum85738471995-02-17 13:50:17 +0000398
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000399PyTypeObject PyCursesWindow_Type;
400
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000401/* Function prototype macros for Window object
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000402
403 X - function name
404 TYPE - parameter Type
405 ERGSTR - format string for construction of the return value
406 PARSESTR - format string for argument parsing
Victor Stinner26486ea2010-05-15 22:23:53 +0000407*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000408
Victor Stinner26486ea2010-05-15 22:23:53 +0000409#define Window_NoArgNoReturnFunction(X) \
410 static PyObject *PyCursesWindow_ ## X \
411 (PyCursesWindowObject *self, PyObject *args) \
412 { return PyCursesCheckERR(X(self->win), # X); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000413
Victor Stinner26486ea2010-05-15 22:23:53 +0000414#define Window_NoArgTrueFalseFunction(X) \
415 static PyObject * PyCursesWindow_ ## X \
416 (PyCursesWindowObject *self) \
417 { \
418 if (X (self->win) == FALSE) { Py_INCREF(Py_False); return Py_False; } \
419 else { Py_INCREF(Py_True); return Py_True; } }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000420
Victor Stinner26486ea2010-05-15 22:23:53 +0000421#define Window_NoArgNoReturnVoidFunction(X) \
422 static PyObject * PyCursesWindow_ ## X \
423 (PyCursesWindowObject *self) \
424 { \
425 X(self->win); Py_INCREF(Py_None); return Py_None; }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000426
Victor Stinner26486ea2010-05-15 22:23:53 +0000427#define Window_NoArg2TupleReturnFunction(X, TYPE, ERGSTR) \
428 static PyObject * PyCursesWindow_ ## X \
429 (PyCursesWindowObject *self) \
430 { \
431 TYPE arg1, arg2; \
432 X(self->win,arg1,arg2); return Py_BuildValue(ERGSTR, arg1, arg2); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000433
Victor Stinner26486ea2010-05-15 22:23:53 +0000434#define Window_OneArgNoReturnVoidFunction(X, TYPE, PARSESTR) \
435 static PyObject * PyCursesWindow_ ## X \
436 (PyCursesWindowObject *self, PyObject *args) \
437 { \
438 TYPE arg1; \
439 if (!PyArg_ParseTuple(args, PARSESTR, &arg1)) return NULL; \
440 X(self->win,arg1); Py_INCREF(Py_None); return Py_None; }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000441
Victor Stinner26486ea2010-05-15 22:23:53 +0000442#define Window_OneArgNoReturnFunction(X, TYPE, PARSESTR) \
443 static PyObject * PyCursesWindow_ ## X \
444 (PyCursesWindowObject *self, PyObject *args) \
445 { \
446 TYPE arg1; \
447 if (!PyArg_ParseTuple(args,PARSESTR, &arg1)) return NULL; \
448 return PyCursesCheckERR(X(self->win, arg1), # X); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000449
Victor Stinner26486ea2010-05-15 22:23:53 +0000450#define Window_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \
451 static PyObject * PyCursesWindow_ ## X \
452 (PyCursesWindowObject *self, PyObject *args) \
453 { \
454 TYPE arg1, arg2; \
455 if (!PyArg_ParseTuple(args,PARSESTR, &arg1, &arg2)) return NULL; \
456 return PyCursesCheckERR(X(self->win, arg1, arg2), # X); }
Guido van Rossum85738471995-02-17 13:50:17 +0000457
Guido van Rossumf6971e21994-08-30 12:25:20 +0000458/* ------------- WINDOW routines --------------- */
Guido van Rossum85738471995-02-17 13:50:17 +0000459
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000460Window_NoArgNoReturnFunction(untouchwin)
461Window_NoArgNoReturnFunction(touchwin)
462Window_NoArgNoReturnFunction(redrawwin)
463Window_NoArgNoReturnFunction(winsertln)
464Window_NoArgNoReturnFunction(werase)
465Window_NoArgNoReturnFunction(wdeleteln)
466
467Window_NoArgTrueFalseFunction(is_wintouched)
468
469Window_NoArgNoReturnVoidFunction(wsyncup)
470Window_NoArgNoReturnVoidFunction(wsyncdown)
471Window_NoArgNoReturnVoidFunction(wstandend)
472Window_NoArgNoReturnVoidFunction(wstandout)
473Window_NoArgNoReturnVoidFunction(wcursyncup)
474Window_NoArgNoReturnVoidFunction(wclrtoeol)
475Window_NoArgNoReturnVoidFunction(wclrtobot)
476Window_NoArgNoReturnVoidFunction(wclear)
477
478Window_OneArgNoReturnVoidFunction(idcok, int, "i;True(1) or False(0)")
479Window_OneArgNoReturnVoidFunction(immedok, int, "i;True(1) or False(0)")
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +0000480Window_OneArgNoReturnVoidFunction(wtimeout, int, "i;delay")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000481
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000482Window_NoArg2TupleReturnFunction(getyx, int, "ii")
483Window_NoArg2TupleReturnFunction(getbegyx, int, "ii")
484Window_NoArg2TupleReturnFunction(getmaxyx, int, "ii")
485Window_NoArg2TupleReturnFunction(getparyx, int, "ii")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000486
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000487Window_OneArgNoReturnFunction(clearok, int, "i;True(1) or False(0)")
488Window_OneArgNoReturnFunction(idlok, int, "i;True(1) or False(0)")
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000489#if defined(__NetBSD__)
490Window_OneArgNoReturnVoidFunction(keypad, int, "i;True(1) or False(0)")
491#else
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000492Window_OneArgNoReturnFunction(keypad, int, "i;True(1) or False(0)")
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000493#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000494Window_OneArgNoReturnFunction(leaveok, int, "i;True(1) or False(0)")
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000495#if defined(__NetBSD__)
496Window_OneArgNoReturnVoidFunction(nodelay, int, "i;True(1) or False(0)")
497#else
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000498Window_OneArgNoReturnFunction(nodelay, int, "i;True(1) or False(0)")
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000499#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000500Window_OneArgNoReturnFunction(notimeout, int, "i;True(1) or False(0)")
501Window_OneArgNoReturnFunction(scrollok, int, "i;True(1) or False(0)")
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +0000502Window_OneArgNoReturnFunction(winsdelln, int, "i;nlines")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000503Window_OneArgNoReturnFunction(syncok, int, "i;True(1) or False(0)")
504
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000505Window_TwoArgNoReturnFunction(mvwin, int, "ii;y,x")
506Window_TwoArgNoReturnFunction(mvderwin, int, "ii;y,x")
507Window_TwoArgNoReturnFunction(wmove, int, "ii;y,x")
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +0000508#ifndef STRICT_SYSV_CURSES
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000509Window_TwoArgNoReturnFunction(wresize, int, "ii;lines,columns")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000510#endif
511
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +0000512/* Allocation and deallocation of Window Objects */
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000513
Guido van Rossumf6971e21994-08-30 12:25:20 +0000514static PyObject *
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100515PyCursesWindow_New(WINDOW *win, const char *encoding)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000516{
Victor Stinner26486ea2010-05-15 22:23:53 +0000517 PyCursesWindowObject *wo;
Guido van Rossum85738471995-02-17 13:50:17 +0000518
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100519 if (encoding == NULL) {
520#if defined(MS_WINDOWS)
521 char *buffer[100];
522 UINT cp;
523 cp = GetConsoleOutputCP();
524 if (cp != 0) {
525 PyOS_snprintf(buffer, sizeof(buffer), "cp%u", cp);
526 encoding = buffer;
527 }
528#elif defined(CODESET)
529 const char *codeset = nl_langinfo(CODESET);
530 if (codeset != NULL && codeset[0] != 0)
531 encoding = codeset;
532#endif
533 if (encoding == NULL)
534 encoding = "utf-8";
535 }
536
Victor Stinner26486ea2010-05-15 22:23:53 +0000537 wo = PyObject_NEW(PyCursesWindowObject, &PyCursesWindow_Type);
538 if (wo == NULL) return NULL;
539 wo->win = win;
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200540 wo->encoding = _PyMem_Strdup(encoding);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100541 if (wo->encoding == NULL) {
542 Py_DECREF(wo);
543 PyErr_NoMemory();
544 return NULL;
545 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000546 return (PyObject *)wo;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000547}
548
549static void
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000550PyCursesWindow_Dealloc(PyCursesWindowObject *wo)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000551{
Victor Stinner26486ea2010-05-15 22:23:53 +0000552 if (wo->win != stdscr) delwin(wo->win);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100553 if (wo->encoding != NULL)
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200554 PyMem_Free(wo->encoding);
Victor Stinner26486ea2010-05-15 22:23:53 +0000555 PyObject_DEL(wo);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000556}
557
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000558/* Addch, Addstr, Addnstr */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000559
Larry Hastings61272b72014-01-07 12:41:53 -0800560/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -0700561
562curses.window.addch
563
564 [
Larry Hastings31826802013-10-19 00:09:25 -0700565 y: int
566 Y-coordinate.
Larry Hastings9147a962014-05-04 04:41:18 -0700567 x: int
568 X-coordinate.
Larry Hastings31826802013-10-19 00:09:25 -0700569 ]
570
571 ch: object
572 Character to add.
573
574 [
575 attr: long
576 Attributes for the character.
577 ]
578 /
579
580Paint character ch at (y, x) with attributes attr.
581
582Paint character ch at (y, x) with attributes attr,
583overwriting any character previously painted at that location.
584By default, the character position and attributes are the
585current settings for the window object.
Larry Hastings61272b72014-01-07 12:41:53 -0800586[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700587
Larry Hastings31826802013-10-19 00:09:25 -0700588static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400589curses_window_addch_impl(PyCursesWindowObject *self, int group_left_1, int y,
590 int x, PyObject *ch, int group_right_1, long attr)
591/*[clinic end generated code: output=99f7f85078ec06c3 input=5a41efb34a2de338]*/
Larry Hastings31826802013-10-19 00:09:25 -0700592{
593 PyCursesWindowObject *cwself = (PyCursesWindowObject *)self;
594 int coordinates_group = group_left_1;
595 int attr_group = group_right_1;
596 int rtn;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100597 int type;
Larry Hastings31826802013-10-19 00:09:25 -0700598 chtype cch;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100599#ifdef HAVE_NCURSESW
600 cchar_t wch;
601#endif
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100602 const char *funcname;
Guido van Rossum85738471995-02-17 13:50:17 +0000603
Larry Hastings31826802013-10-19 00:09:25 -0700604 if (!attr_group)
605 attr = A_NORMAL;
Victor Stinner26486ea2010-05-15 22:23:53 +0000606
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100607#ifdef HAVE_NCURSESW
Larry Hastings31826802013-10-19 00:09:25 -0700608 type = PyCurses_ConvertToCchar_t(cwself, ch, &cch, &wch);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100609 if (type == 2) {
610 funcname = "add_wch";
611 wch.attr = attr;
Larry Hastings31826802013-10-19 00:09:25 -0700612 if (coordinates_group)
613 rtn = mvwadd_wch(cwself->win,y,x, &wch);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100614 else {
Larry Hastings31826802013-10-19 00:09:25 -0700615 rtn = wadd_wch(cwself->win, &wch);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100616 }
617 }
618 else
619#else
Serhiy Storchakaa412f762013-10-19 10:45:48 +0300620 type = PyCurses_ConvertToCchar_t(cwself, ch, &cch);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100621#endif
622 if (type == 1) {
623 funcname = "addch";
Larry Hastings31826802013-10-19 00:09:25 -0700624 if (coordinates_group)
625 rtn = mvwaddch(cwself->win,y,x, cch | attr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100626 else {
Larry Hastings31826802013-10-19 00:09:25 -0700627 rtn = waddch(cwself->win, cch | attr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100628 }
629 }
630 else {
Victor Stinner26486ea2010-05-15 22:23:53 +0000631 return NULL;
632 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100633 return PyCursesCheckERR(rtn, funcname);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000634}
635
636static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000637PyCursesWindow_AddStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000638{
Victor Stinner26486ea2010-05-15 22:23:53 +0000639 int rtn;
640 int x, y;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100641 int strtype;
Benjamin Peterson5e545192012-03-14 18:21:35 -0500642 PyObject *strobj, *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100643#ifdef HAVE_NCURSESW
644 wchar_t *wstr = NULL;
645#endif
Victor Stinner26486ea2010-05-15 22:23:53 +0000646 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
647 long lattr;
648 int use_xy = FALSE, use_attr = FALSE;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100649 const char *funcname;
Guido van Rossum85738471995-02-17 13:50:17 +0000650
Victor Stinner26486ea2010-05-15 22:23:53 +0000651 switch (PyTuple_Size(args)) {
652 case 1:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100653 if (!PyArg_ParseTuple(args,"O;str", &strobj))
Victor Stinner26486ea2010-05-15 22:23:53 +0000654 return NULL;
655 break;
656 case 2:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100657 if (!PyArg_ParseTuple(args,"Ol;str,attr", &strobj, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +0000658 return NULL;
659 attr = lattr;
660 use_attr = TRUE;
661 break;
662 case 3:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100663 if (!PyArg_ParseTuple(args,"iiO;int,int,str", &y, &x, &strobj))
Victor Stinner26486ea2010-05-15 22:23:53 +0000664 return NULL;
665 use_xy = TRUE;
666 break;
667 case 4:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100668 if (!PyArg_ParseTuple(args,"iiOl;int,int,str,attr", &y, &x, &strobj, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +0000669 return NULL;
670 attr = lattr;
671 use_xy = use_attr = TRUE;
672 break;
673 default:
674 PyErr_SetString(PyExc_TypeError, "addstr requires 1 to 4 arguments");
675 return NULL;
676 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100677#ifdef HAVE_NCURSESW
678 strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, &wstr);
679#else
680 strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, NULL);
681#endif
682 if (strtype == 0)
683 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +0000684 if (use_attr == TRUE) {
685 attr_old = getattrs(self->win);
686 (void)wattrset(self->win,attr);
687 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100688#ifdef HAVE_NCURSESW
689 if (strtype == 2) {
690 funcname = "addwstr";
691 if (use_xy == TRUE)
692 rtn = mvwaddwstr(self->win,y,x,wstr);
693 else
694 rtn = waddwstr(self->win,wstr);
695 PyMem_Free(wstr);
696 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000697 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100698#endif
699 {
700 char *str = PyBytes_AS_STRING(bytesobj);
701 funcname = "addstr";
702 if (use_xy == TRUE)
703 rtn = mvwaddstr(self->win,y,x,str);
704 else
705 rtn = waddstr(self->win,str);
706 Py_DECREF(bytesobj);
707 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000708 if (use_attr == TRUE)
709 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100710 return PyCursesCheckERR(rtn, funcname);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000711}
Guido van Rossum85738471995-02-17 13:50:17 +0000712
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000713static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000714PyCursesWindow_AddNStr(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000715{
Victor Stinner26486ea2010-05-15 22:23:53 +0000716 int rtn, x, y, n;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100717 int strtype;
Benjamin Peterson5e545192012-03-14 18:21:35 -0500718 PyObject *strobj, *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100719#ifdef HAVE_NCURSESW
720 wchar_t *wstr = NULL;
721#endif
Victor Stinner26486ea2010-05-15 22:23:53 +0000722 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
723 long lattr;
724 int use_xy = FALSE, use_attr = FALSE;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100725 const char *funcname;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000726
Victor Stinner26486ea2010-05-15 22:23:53 +0000727 switch (PyTuple_Size(args)) {
728 case 2:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100729 if (!PyArg_ParseTuple(args,"Oi;str,n", &strobj, &n))
Victor Stinner26486ea2010-05-15 22:23:53 +0000730 return NULL;
731 break;
732 case 3:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100733 if (!PyArg_ParseTuple(args,"Oil;str,n,attr", &strobj, &n, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +0000734 return NULL;
735 attr = lattr;
736 use_attr = TRUE;
737 break;
738 case 4:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100739 if (!PyArg_ParseTuple(args,"iiOi;y,x,str,n", &y, &x, &strobj, &n))
Victor Stinner26486ea2010-05-15 22:23:53 +0000740 return NULL;
741 use_xy = TRUE;
742 break;
743 case 5:
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100744 if (!PyArg_ParseTuple(args,"iiOil;y,x,str,n,attr", &y, &x, &strobj, &n, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +0000745 return NULL;
746 attr = lattr;
747 use_xy = use_attr = TRUE;
748 break;
749 default:
750 PyErr_SetString(PyExc_TypeError, "addnstr requires 2 to 5 arguments");
751 return NULL;
752 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100753#ifdef HAVE_NCURSESW
754 strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, &wstr);
755#else
756 strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, NULL);
757#endif
758 if (strtype == 0)
759 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000760
Victor Stinner26486ea2010-05-15 22:23:53 +0000761 if (use_attr == TRUE) {
762 attr_old = getattrs(self->win);
763 (void)wattrset(self->win,attr);
764 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100765#ifdef HAVE_NCURSESW
766 if (strtype == 2) {
767 funcname = "addnwstr";
768 if (use_xy == TRUE)
769 rtn = mvwaddnwstr(self->win,y,x,wstr,n);
770 else
771 rtn = waddnwstr(self->win,wstr,n);
772 PyMem_Free(wstr);
773 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000774 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100775#endif
776 {
777 char *str = PyBytes_AS_STRING(bytesobj);
778 funcname = "addnstr";
779 if (use_xy == TRUE)
780 rtn = mvwaddnstr(self->win,y,x,str,n);
781 else
782 rtn = waddnstr(self->win,str,n);
783 Py_DECREF(bytesobj);
784 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000785 if (use_attr == TRUE)
786 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100787 return PyCursesCheckERR(rtn, funcname);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000788}
789
790static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000791PyCursesWindow_Bkgd(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000792{
Victor Stinner26486ea2010-05-15 22:23:53 +0000793 PyObject *temp;
794 chtype bkgd;
795 attr_t attr = A_NORMAL;
796 long lattr;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000797
Victor Stinner26486ea2010-05-15 22:23:53 +0000798 switch (PyTuple_Size(args)) {
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000799 case 1:
Victor Stinner26486ea2010-05-15 22:23:53 +0000800 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
801 return NULL;
802 break;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000803 case 2:
Victor Stinner26486ea2010-05-15 22:23:53 +0000804 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr))
805 return NULL;
806 attr = lattr;
807 break;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000808 default:
Victor Stinner26486ea2010-05-15 22:23:53 +0000809 PyErr_SetString(PyExc_TypeError, "bkgd requires 1 or 2 arguments");
810 return NULL;
811 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000812
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100813 if (!PyCurses_ConvertToChtype(self, temp, &bkgd))
Victor Stinner26486ea2010-05-15 22:23:53 +0000814 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000815
Victor Stinner26486ea2010-05-15 22:23:53 +0000816 return PyCursesCheckERR(wbkgd(self->win, bkgd | attr), "bkgd");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000817}
818
819static PyObject *
Christian Heimes2380ac72008-01-09 00:17:24 +0000820PyCursesWindow_AttrOff(PyCursesWindowObject *self, PyObject *args)
821{
Victor Stinner26486ea2010-05-15 22:23:53 +0000822 long lattr;
823 if (!PyArg_ParseTuple(args,"l;attr", &lattr))
824 return NULL;
825 return PyCursesCheckERR(wattroff(self->win, (attr_t)lattr), "attroff");
Christian Heimes2380ac72008-01-09 00:17:24 +0000826}
827
828static PyObject *
829PyCursesWindow_AttrOn(PyCursesWindowObject *self, PyObject *args)
830{
Victor Stinner26486ea2010-05-15 22:23:53 +0000831 long lattr;
832 if (!PyArg_ParseTuple(args,"l;attr", &lattr))
833 return NULL;
834 return PyCursesCheckERR(wattron(self->win, (attr_t)lattr), "attron");
Christian Heimes2380ac72008-01-09 00:17:24 +0000835}
836
837static PyObject *
838PyCursesWindow_AttrSet(PyCursesWindowObject *self, PyObject *args)
839{
Victor Stinner26486ea2010-05-15 22:23:53 +0000840 long lattr;
841 if (!PyArg_ParseTuple(args,"l;attr", &lattr))
842 return NULL;
843 return PyCursesCheckERR(wattrset(self->win, (attr_t)lattr), "attrset");
Christian Heimes2380ac72008-01-09 00:17:24 +0000844}
845
846static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000847PyCursesWindow_BkgdSet(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000848{
Victor Stinner26486ea2010-05-15 22:23:53 +0000849 PyObject *temp;
850 chtype bkgd;
851 attr_t attr = A_NORMAL;
852 long lattr;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000853
Victor Stinner26486ea2010-05-15 22:23:53 +0000854 switch (PyTuple_Size(args)) {
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000855 case 1:
Victor Stinner26486ea2010-05-15 22:23:53 +0000856 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
857 return NULL;
858 break;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000859 case 2:
Victor Stinner26486ea2010-05-15 22:23:53 +0000860 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr))
861 return NULL;
862 attr = lattr;
863 break;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000864 default:
Victor Stinner26486ea2010-05-15 22:23:53 +0000865 PyErr_SetString(PyExc_TypeError, "bkgdset requires 1 or 2 arguments");
866 return NULL;
867 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000868
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100869 if (!PyCurses_ConvertToChtype(self, temp, &bkgd))
Victor Stinner26486ea2010-05-15 22:23:53 +0000870 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000871
Victor Stinner26486ea2010-05-15 22:23:53 +0000872 wbkgdset(self->win, bkgd | attr);
873 return PyCursesCheckERR(0, "bkgdset");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000874}
875
876static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000877PyCursesWindow_Border(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000878{
Victor Stinner26486ea2010-05-15 22:23:53 +0000879 PyObject *temp[8];
880 chtype ch[8];
881 int i;
Andrew M. Kuchling16e65a02001-10-20 16:05:52 +0000882
Victor Stinner26486ea2010-05-15 22:23:53 +0000883 /* Clear the array of parameters */
884 for(i=0; i<8; i++) {
885 temp[i] = NULL;
886 ch[i] = 0;
887 }
Andrew M. Kuchling16e65a02001-10-20 16:05:52 +0000888
Victor Stinner26486ea2010-05-15 22:23:53 +0000889 if (!PyArg_ParseTuple(args,"|OOOOOOOO;ls,rs,ts,bs,tl,tr,bl,br",
890 &temp[0], &temp[1], &temp[2], &temp[3],
891 &temp[4], &temp[5], &temp[6], &temp[7]))
892 return NULL;
893
894 for(i=0; i<8; i++) {
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100895 if (temp[i] != NULL && !PyCurses_ConvertToChtype(self, temp[i], &ch[i]))
Victor Stinner26486ea2010-05-15 22:23:53 +0000896 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +0000897 }
898
899 wborder(self->win,
900 ch[0], ch[1], ch[2], ch[3],
901 ch[4], ch[5], ch[6], ch[7]);
902 Py_INCREF(Py_None);
903 return Py_None;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000904}
905
906static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000907PyCursesWindow_Box(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000908{
Victor Stinner26486ea2010-05-15 22:23:53 +0000909 chtype ch1=0,ch2=0;
910 switch(PyTuple_Size(args)){
911 case 0: break;
912 default:
913 if (!PyArg_ParseTuple(args,"ll;vertint,horint", &ch1, &ch2))
914 return NULL;
915 }
916 box(self->win,ch1,ch2);
917 Py_INCREF(Py_None);
918 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000919}
920
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000921#if defined(HAVE_NCURSES_H) || defined(MVWDELCH_IS_EXPRESSION)
922#define py_mvwdelch mvwdelch
923#else
924int py_mvwdelch(WINDOW *w, int y, int x)
925{
Victor Stinner26486ea2010-05-15 22:23:53 +0000926 mvwdelch(w,y,x);
927 /* On HP/UX, mvwdelch already returns. On other systems,
928 we may well run into this return statement. */
929 return 0;
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000930}
931#endif
932
Guido van Rossumd8faa362007-04-27 19:54:29 +0000933/* chgat, added by Fabian Kreutz <fabian.kreutz at gmx.net> */
934
935static PyObject *
936PyCursesWindow_ChgAt(PyCursesWindowObject *self, PyObject *args)
937{
Victor Stinner26486ea2010-05-15 22:23:53 +0000938 int rtn;
939 int x, y;
940 int num = -1;
941 short color;
942 attr_t attr = A_NORMAL;
943 long lattr;
944 int use_xy = FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000945
Victor Stinner26486ea2010-05-15 22:23:53 +0000946 switch (PyTuple_Size(args)) {
947 case 1:
948 if (!PyArg_ParseTuple(args,"l;attr", &lattr))
949 return NULL;
950 attr = lattr;
951 break;
952 case 2:
953 if (!PyArg_ParseTuple(args,"il;n,attr", &num, &lattr))
954 return NULL;
955 attr = lattr;
956 break;
957 case 3:
958 if (!PyArg_ParseTuple(args,"iil;int,int,attr", &y, &x, &lattr))
959 return NULL;
960 attr = lattr;
961 use_xy = TRUE;
962 break;
963 case 4:
964 if (!PyArg_ParseTuple(args,"iiil;int,int,n,attr", &y, &x, &num, &lattr))
965 return NULL;
966 attr = lattr;
967 use_xy = TRUE;
968 break;
969 default:
970 PyErr_SetString(PyExc_TypeError, "chgat requires 1 to 4 arguments");
971 return NULL;
972 }
Guido van Rossumd8faa362007-04-27 19:54:29 +0000973
Victor Stinner26486ea2010-05-15 22:23:53 +0000974 color = (short)((attr >> 8) & 0xff);
975 attr = attr - (color << 8);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000976
Victor Stinner26486ea2010-05-15 22:23:53 +0000977 if (use_xy == TRUE) {
978 rtn = mvwchgat(self->win,y,x,num,attr,color,NULL);
979 touchline(self->win,y,1);
980 } else {
981 getyx(self->win,y,x);
982 rtn = wchgat(self->win,num,attr,color,NULL);
983 touchline(self->win,y,1);
984 }
985 return PyCursesCheckERR(rtn, "chgat");
Guido van Rossumd8faa362007-04-27 19:54:29 +0000986}
987
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000988
Guido van Rossumf6971e21994-08-30 12:25:20 +0000989static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000990PyCursesWindow_DelCh(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000991{
Victor Stinner26486ea2010-05-15 22:23:53 +0000992 int rtn;
993 int x, y;
Guido van Rossum85738471995-02-17 13:50:17 +0000994
Victor Stinner26486ea2010-05-15 22:23:53 +0000995 switch (PyTuple_Size(args)) {
996 case 0:
997 rtn = wdelch(self->win);
998 break;
999 case 2:
1000 if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x))
1001 return NULL;
1002 rtn = py_mvwdelch(self->win,y,x);
1003 break;
1004 default:
1005 PyErr_SetString(PyExc_TypeError, "delch requires 0 or 2 arguments");
1006 return NULL;
1007 }
1008 return PyCursesCheckERR(rtn, "[mv]wdelch");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001009}
1010
1011static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001012PyCursesWindow_DerWin(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001013{
Victor Stinner26486ea2010-05-15 22:23:53 +00001014 WINDOW *win;
1015 int nlines, ncols, begin_y, begin_x;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001016
Victor Stinner26486ea2010-05-15 22:23:53 +00001017 nlines = 0;
1018 ncols = 0;
1019 switch (PyTuple_Size(args)) {
1020 case 2:
1021 if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
1022 return NULL;
1023 break;
1024 case 4:
1025 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
1026 &nlines,&ncols,&begin_y,&begin_x))
1027 return NULL;
1028 break;
1029 default:
1030 PyErr_SetString(PyExc_TypeError, "derwin requires 2 or 4 arguments");
1031 return NULL;
1032 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001033
Victor Stinner26486ea2010-05-15 22:23:53 +00001034 win = derwin(self->win,nlines,ncols,begin_y,begin_x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001035
Victor Stinner26486ea2010-05-15 22:23:53 +00001036 if (win == NULL) {
1037 PyErr_SetString(PyCursesError, catchall_NULL);
1038 return NULL;
1039 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001040
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001041 return (PyObject *)PyCursesWindow_New(win, NULL);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001042}
1043
1044static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001045PyCursesWindow_EchoChar(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001046{
Victor Stinner26486ea2010-05-15 22:23:53 +00001047 PyObject *temp;
1048 chtype ch;
1049 attr_t attr = A_NORMAL;
1050 long lattr;
Guido van Rossum85738471995-02-17 13:50:17 +00001051
Victor Stinner26486ea2010-05-15 22:23:53 +00001052 switch (PyTuple_Size(args)) {
1053 case 1:
1054 if (!PyArg_ParseTuple(args,"O;ch or int", &temp))
1055 return NULL;
1056 break;
1057 case 2:
1058 if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr))
1059 return NULL;
1060 attr = lattr;
1061 break;
1062 default:
1063 PyErr_SetString(PyExc_TypeError, "echochar requires 1 or 2 arguments");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001064
1065
Victor Stinner26486ea2010-05-15 22:23:53 +00001066 return NULL;
1067 }
Guido van Rossum85738471995-02-17 13:50:17 +00001068
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001069 if (!PyCurses_ConvertToChtype(self, temp, &ch))
Victor Stinner26486ea2010-05-15 22:23:53 +00001070 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00001071
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001072#ifdef WINDOW_HAS_FLAGS
Victor Stinner26486ea2010-05-15 22:23:53 +00001073 if (self->win->_flags & _ISPAD)
1074 return PyCursesCheckERR(pechochar(self->win, ch | attr),
1075 "echochar");
1076 else
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001077#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001078 return PyCursesCheckERR(wechochar(self->win, ch | attr),
1079 "echochar");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001080}
1081
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001082#ifdef NCURSES_MOUSE_VERSION
1083static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001084PyCursesWindow_Enclose(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001085{
Victor Stinner26486ea2010-05-15 22:23:53 +00001086 int x, y;
1087 if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x))
1088 return NULL;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001089
Victor Stinner26486ea2010-05-15 22:23:53 +00001090 return PyLong_FromLong( wenclose(self->win,y,x) );
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001091}
1092#endif
1093
Guido van Rossumf6971e21994-08-30 12:25:20 +00001094static PyObject *
Martin v. Löwisc0e16712002-01-17 23:08:27 +00001095PyCursesWindow_GetBkgd(PyCursesWindowObject *self)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001096{
Victor Stinner26486ea2010-05-15 22:23:53 +00001097 return PyLong_FromLong((long) getbkgd(self->win));
Guido van Rossumf6971e21994-08-30 12:25:20 +00001098}
1099
1100static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001101PyCursesWindow_GetCh(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001102{
Victor Stinner26486ea2010-05-15 22:23:53 +00001103 int x, y;
1104 int rtn;
Guido van Rossum85738471995-02-17 13:50:17 +00001105
Victor Stinner26486ea2010-05-15 22:23:53 +00001106 switch (PyTuple_Size(args)) {
1107 case 0:
1108 Py_BEGIN_ALLOW_THREADS
1109 rtn = wgetch(self->win);
1110 Py_END_ALLOW_THREADS
1111 break;
1112 case 2:
1113 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1114 return NULL;
1115 Py_BEGIN_ALLOW_THREADS
1116 rtn = mvwgetch(self->win,y,x);
1117 Py_END_ALLOW_THREADS
1118 break;
1119 default:
1120 PyErr_SetString(PyExc_TypeError, "getch requires 0 or 2 arguments");
1121 return NULL;
1122 }
1123 return PyLong_FromLong((long)rtn);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001124}
Guido van Rossum85738471995-02-17 13:50:17 +00001125
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001126static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001127PyCursesWindow_GetKey(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001128{
Victor Stinner26486ea2010-05-15 22:23:53 +00001129 int x, y;
1130 int rtn;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001131
Victor Stinner26486ea2010-05-15 22:23:53 +00001132 switch (PyTuple_Size(args)) {
1133 case 0:
1134 Py_BEGIN_ALLOW_THREADS
1135 rtn = wgetch(self->win);
1136 Py_END_ALLOW_THREADS
1137 break;
1138 case 2:
1139 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1140 return NULL;
1141 Py_BEGIN_ALLOW_THREADS
1142 rtn = mvwgetch(self->win,y,x);
1143 Py_END_ALLOW_THREADS
1144 break;
1145 default:
1146 PyErr_SetString(PyExc_TypeError, "getkey requires 0 or 2 arguments");
1147 return NULL;
1148 }
1149 if (rtn == ERR) {
1150 /* getch() returns ERR in nodelay mode */
R David Murrayf5d7cc22013-03-19 16:23:09 -04001151 PyErr_CheckSignals();
1152 if (!PyErr_Occurred())
1153 PyErr_SetString(PyCursesError, "no input");
Victor Stinner26486ea2010-05-15 22:23:53 +00001154 return NULL;
1155 } else if (rtn<=255) {
1156 return Py_BuildValue("C", rtn);
1157 } else {
1158 const char *knp;
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001159#if defined(__NetBSD__)
Victor Stinner26486ea2010-05-15 22:23:53 +00001160 knp = unctrl(rtn);
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001161#else
Victor Stinner26486ea2010-05-15 22:23:53 +00001162 knp = keyname(rtn);
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001163#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001164 return PyUnicode_FromString((knp == NULL) ? "" : knp);
1165 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001166}
1167
Nadeem Vawda9e2e9902011-07-31 15:01:11 +02001168#ifdef HAVE_NCURSESW
Guido van Rossumf6971e21994-08-30 12:25:20 +00001169static PyObject *
Victor Stinnera7878b72011-07-14 23:07:44 +02001170PyCursesWindow_Get_WCh(PyCursesWindowObject *self, PyObject *args)
1171{
1172 int x, y;
1173 int ct;
1174 wint_t rtn;
1175
1176 switch (PyTuple_Size(args)) {
1177 case 0:
1178 Py_BEGIN_ALLOW_THREADS
1179 ct = wget_wch(self->win,&rtn);
1180 Py_END_ALLOW_THREADS
1181 break;
1182 case 2:
1183 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1184 return NULL;
1185 Py_BEGIN_ALLOW_THREADS
1186 ct = mvwget_wch(self->win,y,x,&rtn);
1187 Py_END_ALLOW_THREADS
1188 break;
1189 default:
1190 PyErr_SetString(PyExc_TypeError, "get_wch requires 0 or 2 arguments");
1191 return NULL;
1192 }
1193 if (ct == ERR) {
Victor Stinnerbd2d30c2013-03-21 12:21:06 +01001194 if (PyErr_CheckSignals())
1195 return NULL;
1196
Victor Stinnera7878b72011-07-14 23:07:44 +02001197 /* get_wch() returns ERR in nodelay mode */
1198 PyErr_SetString(PyCursesError, "no input");
1199 return NULL;
1200 }
Victor Stinner1d39cde2012-08-29 01:40:57 +02001201 if (ct == KEY_CODE_YES)
1202 return PyLong_FromLong(rtn);
1203 else
1204 return PyUnicode_FromOrdinal(rtn);
Victor Stinnera7878b72011-07-14 23:07:44 +02001205}
Nadeem Vawda9e2e9902011-07-31 15:01:11 +02001206#endif
Victor Stinnera7878b72011-07-14 23:07:44 +02001207
1208static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001209PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001210{
Victor Stinner26486ea2010-05-15 22:23:53 +00001211 int x, y, n;
1212 char rtn[1024]; /* This should be big enough.. I hope */
1213 int rtn2;
Guido van Rossum85738471995-02-17 13:50:17 +00001214
Victor Stinner26486ea2010-05-15 22:23:53 +00001215 switch (PyTuple_Size(args)) {
1216 case 0:
1217 Py_BEGIN_ALLOW_THREADS
1218 rtn2 = wgetnstr(self->win,rtn, 1023);
1219 Py_END_ALLOW_THREADS
1220 break;
1221 case 1:
1222 if (!PyArg_ParseTuple(args,"i;n", &n))
1223 return NULL;
Benjamin Peterson40a77c32016-08-13 18:15:28 -07001224 if (n < 0) {
1225 PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1226 return NULL;
1227 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001228 Py_BEGIN_ALLOW_THREADS
Victor Stinner640c35c2013-06-04 23:14:37 +02001229 rtn2 = wgetnstr(self->win, rtn, Py_MIN(n, 1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001230 Py_END_ALLOW_THREADS
1231 break;
1232 case 2:
1233 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1234 return NULL;
1235 Py_BEGIN_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001236#ifdef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00001237 rtn2 = wmove(self->win,y,x)==ERR ? ERR : wgetnstr(self->win, rtn, 1023);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001238#else
Victor Stinner26486ea2010-05-15 22:23:53 +00001239 rtn2 = mvwgetnstr(self->win,y,x,rtn, 1023);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001240#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001241 Py_END_ALLOW_THREADS
1242 break;
1243 case 3:
1244 if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
1245 return NULL;
Benjamin Peterson40a77c32016-08-13 18:15:28 -07001246 if (n < 0) {
1247 PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1248 return NULL;
1249 }
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +00001250#ifdef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00001251 Py_BEGIN_ALLOW_THREADS
1252 rtn2 = wmove(self->win,y,x)==ERR ? ERR :
Victor Stinner640c35c2013-06-04 23:14:37 +02001253 wgetnstr(self->win, rtn, Py_MIN(n, 1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001254 Py_END_ALLOW_THREADS
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001255#else
Victor Stinner26486ea2010-05-15 22:23:53 +00001256 Py_BEGIN_ALLOW_THREADS
Victor Stinner640c35c2013-06-04 23:14:37 +02001257 rtn2 = mvwgetnstr(self->win, y, x, rtn, Py_MIN(n, 1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001258 Py_END_ALLOW_THREADS
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001259#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001260 break;
1261 default:
1262 PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 3 arguments");
1263 return NULL;
1264 }
1265 if (rtn2 == ERR)
1266 rtn[0] = 0;
1267 return PyBytes_FromString(rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001268}
1269
1270static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001271PyCursesWindow_Hline(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001272{
Victor Stinner26486ea2010-05-15 22:23:53 +00001273 PyObject *temp;
1274 chtype ch;
1275 int n, x, y, code = OK;
1276 attr_t attr = A_NORMAL;
1277 long lattr;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001278
Victor Stinner26486ea2010-05-15 22:23:53 +00001279 switch (PyTuple_Size(args)) {
1280 case 2:
1281 if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
1282 return NULL;
1283 break;
1284 case 3:
1285 if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &lattr))
1286 return NULL;
1287 attr = lattr;
1288 break;
1289 case 4:
1290 if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
1291 return NULL;
1292 code = wmove(self->win, y, x);
1293 break;
1294 case 5:
1295 if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
1296 &y, &x, &temp, &n, &lattr))
1297 return NULL;
1298 attr = lattr;
1299 code = wmove(self->win, y, x);
1300 break;
1301 default:
1302 PyErr_SetString(PyExc_TypeError, "hline requires 2 to 5 arguments");
1303 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001304 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001305
1306 if (code != ERR) {
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001307 if (!PyCurses_ConvertToChtype(self, temp, &ch))
Victor Stinner26486ea2010-05-15 22:23:53 +00001308 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00001309 return PyCursesCheckERR(whline(self->win, ch | attr, n), "hline");
1310 } else
1311 return PyCursesCheckERR(code, "wmove");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001312}
1313
1314static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001315PyCursesWindow_InsCh(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001316{
Victor Stinner26486ea2010-05-15 22:23:53 +00001317 int rtn, x, y, use_xy = FALSE;
1318 PyObject *temp;
1319 chtype ch = 0;
1320 attr_t attr = A_NORMAL;
1321 long lattr;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001322
Victor Stinner26486ea2010-05-15 22:23:53 +00001323 switch (PyTuple_Size(args)) {
1324 case 1:
1325 if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
1326 return NULL;
1327 break;
1328 case 2:
1329 if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &temp, &lattr))
1330 return NULL;
1331 attr = lattr;
1332 break;
1333 case 3:
1334 if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &temp))
1335 return NULL;
1336 use_xy = TRUE;
1337 break;
1338 case 4:
1339 if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr", &y, &x, &temp, &lattr))
1340 return NULL;
1341 attr = lattr;
1342 use_xy = TRUE;
1343 break;
1344 default:
1345 PyErr_SetString(PyExc_TypeError, "insch requires 1 or 4 arguments");
1346 return NULL;
1347 }
1348
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001349 if (!PyCurses_ConvertToChtype(self, temp, &ch))
Victor Stinner26486ea2010-05-15 22:23:53 +00001350 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00001351
1352 if (use_xy == TRUE)
1353 rtn = mvwinsch(self->win,y,x, ch | attr);
1354 else {
1355 rtn = winsch(self->win, ch | attr);
1356 }
1357 return PyCursesCheckERR(rtn, "insch");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001358}
1359
1360static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001361PyCursesWindow_InCh(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001362{
Christian Heimesabbc8ca2013-12-04 08:50:22 +01001363 int x, y;
1364 unsigned long rtn;
Guido van Rossum85738471995-02-17 13:50:17 +00001365
Victor Stinner26486ea2010-05-15 22:23:53 +00001366 switch (PyTuple_Size(args)) {
1367 case 0:
1368 rtn = winch(self->win);
1369 break;
1370 case 2:
1371 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1372 return NULL;
1373 rtn = mvwinch(self->win,y,x);
1374 break;
1375 default:
1376 PyErr_SetString(PyExc_TypeError, "inch requires 0 or 2 arguments");
1377 return NULL;
1378 }
Christian Heimesabbc8ca2013-12-04 08:50:22 +01001379 return PyLong_FromUnsignedLong(rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001380}
1381
1382static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001383PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001384{
Victor Stinner26486ea2010-05-15 22:23:53 +00001385 int x, y, n;
1386 char rtn[1024]; /* This should be big enough.. I hope */
1387 int rtn2;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001388
Victor Stinner26486ea2010-05-15 22:23:53 +00001389 switch (PyTuple_Size(args)) {
1390 case 0:
1391 rtn2 = winnstr(self->win,rtn, 1023);
1392 break;
1393 case 1:
1394 if (!PyArg_ParseTuple(args,"i;n", &n))
1395 return NULL;
Benjamin Peterson432ea4f2016-08-15 21:40:14 -07001396 if (n < 0) {
1397 PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1398 return NULL;
1399 }
Victor Stinner640c35c2013-06-04 23:14:37 +02001400 rtn2 = winnstr(self->win, rtn, Py_MIN(n, 1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001401 break;
1402 case 2:
1403 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1404 return NULL;
1405 rtn2 = mvwinnstr(self->win,y,x,rtn,1023);
1406 break;
1407 case 3:
1408 if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
1409 return NULL;
Benjamin Peterson432ea4f2016-08-15 21:40:14 -07001410 if (n < 0) {
1411 PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1412 return NULL;
1413 }
Victor Stinner640c35c2013-06-04 23:14:37 +02001414 rtn2 = mvwinnstr(self->win, y, x, rtn, Py_MIN(n,1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001415 break;
1416 default:
1417 PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments");
1418 return NULL;
1419 }
1420 if (rtn2 == ERR)
1421 rtn[0] = 0;
1422 return PyBytes_FromString(rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001423}
1424
1425static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001426PyCursesWindow_InsStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001427{
Victor Stinner26486ea2010-05-15 22:23:53 +00001428 int rtn;
1429 int x, y;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001430 int strtype;
Benjamin Peterson5e545192012-03-14 18:21:35 -05001431 PyObject *strobj, *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001432#ifdef HAVE_NCURSESW
1433 wchar_t *wstr = NULL;
1434#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001435 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
1436 long lattr;
1437 int use_xy = FALSE, use_attr = FALSE;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001438 const char *funcname;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001439
Victor Stinner26486ea2010-05-15 22:23:53 +00001440 switch (PyTuple_Size(args)) {
1441 case 1:
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001442 if (!PyArg_ParseTuple(args,"O;str", &strobj))
Victor Stinner26486ea2010-05-15 22:23:53 +00001443 return NULL;
1444 break;
1445 case 2:
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001446 if (!PyArg_ParseTuple(args,"Ol;str,attr", &strobj, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +00001447 return NULL;
1448 attr = lattr;
1449 use_attr = TRUE;
1450 break;
1451 case 3:
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001452 if (!PyArg_ParseTuple(args,"iiO;y,x,str", &y, &x, &strobj))
Victor Stinner26486ea2010-05-15 22:23:53 +00001453 return NULL;
1454 use_xy = TRUE;
1455 break;
1456 case 4:
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001457 if (!PyArg_ParseTuple(args,"iiOl;y,x,str,attr", &y, &x, &strobj, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +00001458 return NULL;
1459 attr = lattr;
1460 use_xy = use_attr = TRUE;
1461 break;
1462 default:
1463 PyErr_SetString(PyExc_TypeError, "insstr requires 1 to 4 arguments");
1464 return NULL;
1465 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001466
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001467#ifdef HAVE_NCURSESW
1468 strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, &wstr);
1469#else
1470 strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, NULL);
1471#endif
1472 if (strtype == 0)
1473 return NULL;
1474
Victor Stinner26486ea2010-05-15 22:23:53 +00001475 if (use_attr == TRUE) {
1476 attr_old = getattrs(self->win);
1477 (void)wattrset(self->win,attr);
1478 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001479#ifdef HAVE_NCURSESW
1480 if (strtype == 2) {
1481 funcname = "inswstr";
1482 if (use_xy == TRUE)
1483 rtn = mvwins_wstr(self->win,y,x,wstr);
1484 else
1485 rtn = wins_wstr(self->win,wstr);
1486 PyMem_Free(wstr);
1487 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001488 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001489#endif
1490 {
1491 char *str = PyBytes_AS_STRING(bytesobj);
1492 funcname = "insstr";
1493 if (use_xy == TRUE)
1494 rtn = mvwinsstr(self->win,y,x,str);
1495 else
1496 rtn = winsstr(self->win,str);
1497 Py_DECREF(bytesobj);
1498 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001499 if (use_attr == TRUE)
1500 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001501 return PyCursesCheckERR(rtn, funcname);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001502}
1503
1504static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001505PyCursesWindow_InsNStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001506{
Victor Stinner26486ea2010-05-15 22:23:53 +00001507 int rtn, x, y, n;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001508 int strtype;
Benjamin Peterson5e545192012-03-14 18:21:35 -05001509 PyObject *strobj, *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001510#ifdef HAVE_NCURSESW
1511 wchar_t *wstr = NULL;
1512#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001513 attr_t attr = A_NORMAL , attr_old = A_NORMAL;
1514 long lattr;
1515 int use_xy = FALSE, use_attr = FALSE;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001516 const char *funcname;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001517
Victor Stinner26486ea2010-05-15 22:23:53 +00001518 switch (PyTuple_Size(args)) {
1519 case 2:
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001520 if (!PyArg_ParseTuple(args,"Oi;str,n", &strobj, &n))
Victor Stinner26486ea2010-05-15 22:23:53 +00001521 return NULL;
1522 break;
1523 case 3:
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001524 if (!PyArg_ParseTuple(args,"Oil;str,n,attr", &strobj, &n, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +00001525 return NULL;
1526 attr = lattr;
1527 use_attr = TRUE;
1528 break;
1529 case 4:
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001530 if (!PyArg_ParseTuple(args,"iiOi;y,x,str,n", &y, &x, &strobj, &n))
Victor Stinner26486ea2010-05-15 22:23:53 +00001531 return NULL;
1532 use_xy = TRUE;
1533 break;
1534 case 5:
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001535 if (!PyArg_ParseTuple(args,"iiOil;y,x,str,n,attr", &y, &x, &strobj, &n, &lattr))
Victor Stinner26486ea2010-05-15 22:23:53 +00001536 return NULL;
1537 attr = lattr;
1538 use_xy = use_attr = TRUE;
1539 break;
1540 default:
1541 PyErr_SetString(PyExc_TypeError, "insnstr requires 2 to 5 arguments");
1542 return NULL;
1543 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001544
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001545#ifdef HAVE_NCURSESW
1546 strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, &wstr);
1547#else
1548 strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, NULL);
1549#endif
1550 if (strtype == 0)
1551 return NULL;
1552
Victor Stinner26486ea2010-05-15 22:23:53 +00001553 if (use_attr == TRUE) {
1554 attr_old = getattrs(self->win);
1555 (void)wattrset(self->win,attr);
1556 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001557#ifdef HAVE_NCURSESW
1558 if (strtype == 2) {
1559 funcname = "insn_wstr";
1560 if (use_xy == TRUE)
1561 rtn = mvwins_nwstr(self->win,y,x,wstr,n);
1562 else
1563 rtn = wins_nwstr(self->win,wstr,n);
1564 PyMem_Free(wstr);
1565 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001566 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001567#endif
1568 {
1569 char *str = PyBytes_AS_STRING(bytesobj);
1570 funcname = "insnstr";
1571 if (use_xy == TRUE)
1572 rtn = mvwinsnstr(self->win,y,x,str,n);
1573 else
1574 rtn = winsnstr(self->win,str,n);
1575 Py_DECREF(bytesobj);
1576 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001577 if (use_attr == TRUE)
1578 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001579 return PyCursesCheckERR(rtn, funcname);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001580}
1581
1582static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001583PyCursesWindow_Is_LineTouched(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001584{
Victor Stinner26486ea2010-05-15 22:23:53 +00001585 int line, erg;
1586 if (!PyArg_ParseTuple(args,"i;line", &line))
1587 return NULL;
1588 erg = is_linetouched(self->win, line);
1589 if (erg == ERR) {
1590 PyErr_SetString(PyExc_TypeError,
1591 "is_linetouched: line number outside of boundaries");
1592 return NULL;
1593 } else
1594 if (erg == FALSE) {
1595 Py_INCREF(Py_False);
1596 return Py_False;
1597 } else {
1598 Py_INCREF(Py_True);
1599 return Py_True;
1600 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001601}
1602
1603static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001604PyCursesWindow_NoOutRefresh(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001605{
Victor Stinner26486ea2010-05-15 22:23:53 +00001606 int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
1607 int rtn;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001608
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001609#ifndef WINDOW_HAS_FLAGS
Victor Stinner26486ea2010-05-15 22:23:53 +00001610 if (0)
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001611#else
Victor Stinner26486ea2010-05-15 22:23:53 +00001612 if (self->win->_flags & _ISPAD)
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001613#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001614 {
1615 switch(PyTuple_Size(args)) {
1616 case 6:
1617 if (!PyArg_ParseTuple(args,
1618 "iiiiii;" \
1619 "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
1620 &pminrow, &pmincol, &sminrow,
1621 &smincol, &smaxrow, &smaxcol))
1622 return NULL;
1623 Py_BEGIN_ALLOW_THREADS
1624 rtn = pnoutrefresh(self->win,
1625 pminrow, pmincol, sminrow,
1626 smincol, smaxrow, smaxcol);
1627 Py_END_ALLOW_THREADS
1628 return PyCursesCheckERR(rtn, "pnoutrefresh");
1629 default:
1630 PyErr_SetString(PyCursesError,
1631 "noutrefresh() called for a pad "
1632 "requires 6 arguments");
1633 return NULL;
1634 }
1635 } else {
1636 if (!PyArg_ParseTuple(args, ":noutrefresh"))
1637 return NULL;
Andrew M. Kuchlingf4195722000-06-23 01:36:21 +00001638
Victor Stinner26486ea2010-05-15 22:23:53 +00001639 Py_BEGIN_ALLOW_THREADS
1640 rtn = wnoutrefresh(self->win);
1641 Py_END_ALLOW_THREADS
1642 return PyCursesCheckERR(rtn, "wnoutrefresh");
1643 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001644}
1645
1646static PyObject *
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001647PyCursesWindow_Overlay(PyCursesWindowObject *self, PyObject *args)
1648{
1649 PyCursesWindowObject *temp;
1650 int use_copywin = FALSE;
1651 int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
1652 int rtn;
Victor Stinner26486ea2010-05-15 22:23:53 +00001653
Martin v. Löwisc0e16712002-01-17 23:08:27 +00001654 switch (PyTuple_Size(args)) {
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001655 case 1:
Victor Stinner26486ea2010-05-15 22:23:53 +00001656 if (!PyArg_ParseTuple(args, "O!;window object",
1657 &PyCursesWindow_Type, &temp))
1658 return NULL;
1659 break;
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001660 case 7:
Victor Stinner26486ea2010-05-15 22:23:53 +00001661 if (!PyArg_ParseTuple(args, "O!iiiiii;window object, int, int, int, int, int, int",
1662 &PyCursesWindow_Type, &temp, &sminrow, &smincol,
1663 &dminrow, &dmincol, &dmaxrow, &dmaxcol))
1664 return NULL;
1665 use_copywin = TRUE;
1666 break;
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001667 default:
Victor Stinner26486ea2010-05-15 22:23:53 +00001668 PyErr_SetString(PyExc_TypeError,
1669 "overlay requires one or seven arguments");
1670 return NULL;
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001671 }
1672
1673 if (use_copywin == TRUE) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001674 rtn = copywin(self->win, temp->win, sminrow, smincol,
1675 dminrow, dmincol, dmaxrow, dmaxcol, TRUE);
1676 return PyCursesCheckERR(rtn, "copywin");
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001677 }
1678 else {
Victor Stinner26486ea2010-05-15 22:23:53 +00001679 rtn = overlay(self->win, temp->win);
1680 return PyCursesCheckERR(rtn, "overlay");
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001681 }
1682}
1683
1684static PyObject *
1685PyCursesWindow_Overwrite(PyCursesWindowObject *self, PyObject *args)
1686{
1687 PyCursesWindowObject *temp;
1688 int use_copywin = FALSE;
1689 int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
1690 int rtn;
Victor Stinner26486ea2010-05-15 22:23:53 +00001691
Martin v. Löwisc0e16712002-01-17 23:08:27 +00001692 switch (PyTuple_Size(args)) {
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001693 case 1:
Victor Stinner26486ea2010-05-15 22:23:53 +00001694 if (!PyArg_ParseTuple(args, "O!;window object",
1695 &PyCursesWindow_Type, &temp))
1696 return NULL;
1697 break;
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001698 case 7:
Victor Stinner26486ea2010-05-15 22:23:53 +00001699 if (!PyArg_ParseTuple(args, "O!iiiiii;window object, int, int, int, int, int, int",
1700 &PyCursesWindow_Type, &temp, &sminrow, &smincol,
1701 &dminrow, &dmincol, &dmaxrow, &dmaxcol))
1702 return NULL;
1703 use_copywin = TRUE;
1704 break;
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001705 default:
Victor Stinner26486ea2010-05-15 22:23:53 +00001706 PyErr_SetString(PyExc_TypeError,
1707 "overwrite requires one or seven arguments");
1708 return NULL;
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001709 }
1710
1711 if (use_copywin == TRUE) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001712 rtn = copywin(self->win, temp->win, sminrow, smincol,
1713 dminrow, dmincol, dmaxrow, dmaxcol, FALSE);
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001714 return PyCursesCheckERR(rtn, "copywin");
1715 }
1716 else {
Victor Stinner26486ea2010-05-15 22:23:53 +00001717 rtn = overwrite(self->win, temp->win);
1718 return PyCursesCheckERR(rtn, "overwrite");
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001719 }
1720}
1721
1722static PyObject *
Guido van Rossum6c95da32007-07-24 00:16:38 +00001723PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *stream)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001724{
Victor Stinner26486ea2010-05-15 22:23:53 +00001725 /* We have to simulate this by writing to a temporary FILE*,
1726 then reading back, then writing to the argument stream. */
1727 char fn[100];
Victor Stinnerdaf45552013-08-28 00:53:59 +02001728 int fd = -1;
1729 FILE *fp = NULL;
1730 PyObject *res = NULL;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00001731
Victor Stinner26486ea2010-05-15 22:23:53 +00001732 strcpy(fn, "/tmp/py.curses.putwin.XXXXXX");
1733 fd = mkstemp(fn);
1734 if (fd < 0)
1735 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001736 if (_Py_set_inheritable(fd, 0, NULL) < 0)
1737 goto exit;
Victor Stinner26486ea2010-05-15 22:23:53 +00001738 fp = fdopen(fd, "wb+");
1739 if (fp == NULL) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001740 PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
1741 goto exit;
Victor Stinner26486ea2010-05-15 22:23:53 +00001742 }
1743 res = PyCursesCheckERR(putwin(self->win, fp), "putwin");
Victor Stinnerdaf45552013-08-28 00:53:59 +02001744 if (res == NULL)
1745 goto exit;
Victor Stinner26486ea2010-05-15 22:23:53 +00001746 fseek(fp, 0, 0);
1747 while (1) {
1748 char buf[BUFSIZ];
1749 Py_ssize_t n = fread(buf, 1, BUFSIZ, fp);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001750 _Py_IDENTIFIER(write);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001751
Victor Stinner26486ea2010-05-15 22:23:53 +00001752 if (n <= 0)
1753 break;
1754 Py_DECREF(res);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001755 res = _PyObject_CallMethodId(stream, &PyId_write, "y#", buf, n);
Victor Stinner26486ea2010-05-15 22:23:53 +00001756 if (res == NULL)
1757 break;
1758 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02001759
1760exit:
1761 if (fp != NULL)
1762 fclose(fp);
1763 else if (fd != -1)
1764 close(fd);
Guido van Rossum150b7d72007-08-30 23:34:01 +00001765 remove(fn);
Guido van Rossum6c95da32007-07-24 00:16:38 +00001766 return res;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001767}
1768
1769static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001770PyCursesWindow_RedrawLine(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001771{
Victor Stinner26486ea2010-05-15 22:23:53 +00001772 int beg, num;
1773 if (!PyArg_ParseTuple(args, "ii;beg,num", &beg, &num))
1774 return NULL;
1775 return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001776}
1777
1778static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001779PyCursesWindow_Refresh(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001780{
Victor Stinner26486ea2010-05-15 22:23:53 +00001781 int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
1782 int rtn;
Andrew M. Kuchlingf4195722000-06-23 01:36:21 +00001783
Victor Stinner26486ea2010-05-15 22:23:53 +00001784#ifndef WINDOW_HAS_FLAGS
1785 if (0)
1786#else
1787 if (self->win->_flags & _ISPAD)
1788#endif
1789 {
1790 switch(PyTuple_Size(args)) {
1791 case 6:
1792 if (!PyArg_ParseTuple(args,
1793 "iiiiii;" \
1794 "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
1795 &pminrow, &pmincol, &sminrow,
1796 &smincol, &smaxrow, &smaxcol))
1797 return NULL;
1798
1799 Py_BEGIN_ALLOW_THREADS
1800 rtn = prefresh(self->win,
1801 pminrow, pmincol, sminrow,
1802 smincol, smaxrow, smaxcol);
1803 Py_END_ALLOW_THREADS
1804 return PyCursesCheckERR(rtn, "prefresh");
1805 default:
1806 PyErr_SetString(PyCursesError,
1807 "refresh() for a pad requires 6 arguments");
1808 return NULL;
1809 }
1810 } else {
1811 if (!PyArg_ParseTuple(args, ":refresh"))
1812 return NULL;
1813 Py_BEGIN_ALLOW_THREADS
1814 rtn = wrefresh(self->win);
1815 Py_END_ALLOW_THREADS
1816 return PyCursesCheckERR(rtn, "prefresh");
1817 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001818}
1819
1820static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001821PyCursesWindow_SetScrollRegion(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001822{
Victor Stinner26486ea2010-05-15 22:23:53 +00001823 int x, y;
1824 if (!PyArg_ParseTuple(args,"ii;top, bottom",&y,&x))
1825 return NULL;
1826 return PyCursesCheckERR(wsetscrreg(self->win,y,x), "wsetscrreg");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001827}
1828
1829static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001830PyCursesWindow_SubWin(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001831{
Victor Stinner26486ea2010-05-15 22:23:53 +00001832 WINDOW *win;
1833 int nlines, ncols, begin_y, begin_x;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001834
Victor Stinner26486ea2010-05-15 22:23:53 +00001835 nlines = 0;
1836 ncols = 0;
1837 switch (PyTuple_Size(args)) {
1838 case 2:
1839 if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
1840 return NULL;
1841 break;
1842 case 4:
1843 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
1844 &nlines,&ncols,&begin_y,&begin_x))
1845 return NULL;
1846 break;
1847 default:
1848 PyErr_SetString(PyExc_TypeError, "subwin requires 2 or 4 arguments");
1849 return NULL;
1850 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001851
Victor Stinner26486ea2010-05-15 22:23:53 +00001852 /* printf("Subwin: %i %i %i %i \n", nlines, ncols, begin_y, begin_x); */
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001853#ifdef WINDOW_HAS_FLAGS
Victor Stinner26486ea2010-05-15 22:23:53 +00001854 if (self->win->_flags & _ISPAD)
1855 win = subpad(self->win, nlines, ncols, begin_y, begin_x);
1856 else
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001857#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001858 win = subwin(self->win, nlines, ncols, begin_y, begin_x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001859
Victor Stinner26486ea2010-05-15 22:23:53 +00001860 if (win == NULL) {
1861 PyErr_SetString(PyCursesError, catchall_NULL);
1862 return NULL;
1863 }
1864
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001865 return (PyObject *)PyCursesWindow_New(win, self->encoding);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001866}
1867
1868static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001869PyCursesWindow_Scroll(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001870{
Victor Stinner26486ea2010-05-15 22:23:53 +00001871 int nlines;
1872 switch(PyTuple_Size(args)) {
1873 case 0:
1874 return PyCursesCheckERR(scroll(self->win), "scroll");
1875 case 1:
1876 if (!PyArg_ParseTuple(args, "i;nlines", &nlines))
1877 return NULL;
1878 return PyCursesCheckERR(wscrl(self->win, nlines), "scroll");
1879 default:
1880 PyErr_SetString(PyExc_TypeError, "scroll requires 0 or 1 arguments");
1881 return NULL;
1882 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001883}
1884
1885static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001886PyCursesWindow_TouchLine(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001887{
Victor Stinner26486ea2010-05-15 22:23:53 +00001888 int st, cnt, val;
1889 switch (PyTuple_Size(args)) {
1890 case 2:
1891 if (!PyArg_ParseTuple(args,"ii;start,count",&st,&cnt))
1892 return NULL;
1893 return PyCursesCheckERR(touchline(self->win,st,cnt), "touchline");
1894 case 3:
1895 if (!PyArg_ParseTuple(args, "iii;start,count,val", &st, &cnt, &val))
1896 return NULL;
1897 return PyCursesCheckERR(wtouchln(self->win, st, cnt, val), "touchline");
1898 default:
1899 PyErr_SetString(PyExc_TypeError, "touchline requires 2 or 3 arguments");
1900 return NULL;
1901 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001902}
1903
1904static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001905PyCursesWindow_Vline(PyCursesWindowObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001906{
Victor Stinner26486ea2010-05-15 22:23:53 +00001907 PyObject *temp;
1908 chtype ch;
1909 int n, x, y, code = OK;
1910 attr_t attr = A_NORMAL;
1911 long lattr;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001912
Victor Stinner26486ea2010-05-15 22:23:53 +00001913 switch (PyTuple_Size(args)) {
1914 case 2:
1915 if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
1916 return NULL;
1917 break;
1918 case 3:
1919 if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &lattr))
1920 return NULL;
1921 attr = lattr;
1922 break;
1923 case 4:
1924 if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
1925 return NULL;
1926 code = wmove(self->win, y, x);
1927 break;
1928 case 5:
1929 if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
1930 &y, &x, &temp, &n, &lattr))
1931 return NULL;
1932 attr = lattr;
1933 code = wmove(self->win, y, x);
1934 break;
1935 default:
1936 PyErr_SetString(PyExc_TypeError, "vline requires 2 to 5 arguments");
1937 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001938 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001939
1940 if (code != ERR) {
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001941 if (!PyCurses_ConvertToChtype(self, temp, &ch))
Victor Stinner26486ea2010-05-15 22:23:53 +00001942 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00001943 return PyCursesCheckERR(wvline(self->win, ch | attr, n), "vline");
1944 } else
1945 return PyCursesCheckERR(code, "wmove");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001946}
1947
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001948static PyObject *
1949PyCursesWindow_get_encoding(PyCursesWindowObject *self, void *closure)
1950{
1951 return PyUnicode_FromString(self->encoding);
1952}
1953
1954static int
1955PyCursesWindow_set_encoding(PyCursesWindowObject *self, PyObject *value)
1956{
1957 PyObject *ascii;
1958 char *encoding;
1959
1960 /* It is illegal to del win.encoding */
1961 if (value == NULL) {
1962 PyErr_SetString(PyExc_TypeError,
1963 "encoding may not be deleted");
1964 return -1;
1965 }
1966
1967 if (!PyUnicode_Check(value)) {
1968 PyErr_SetString(PyExc_TypeError,
1969 "setting encoding to a non-string");
1970 return -1;
1971 }
1972 ascii = PyUnicode_AsASCIIString(value);
1973 if (ascii == NULL)
1974 return -1;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001975 encoding = _PyMem_Strdup(PyBytes_AS_STRING(ascii));
Ross Lagerwall2dabaf62012-09-07 08:34:23 +02001976 Py_DECREF(ascii);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001977 if (encoding == NULL) {
1978 PyErr_NoMemory();
1979 return -1;
1980 }
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001981 PyMem_Free(self->encoding);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001982 self->encoding = encoding;
1983 return 0;
1984}
1985
1986
Guido van Rossumf6971e21994-08-30 12:25:20 +00001987static PyMethodDef PyCursesWindow_Methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07001988 CURSES_WINDOW_ADDCH_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00001989 {"addnstr", (PyCFunction)PyCursesWindow_AddNStr, METH_VARARGS},
1990 {"addstr", (PyCFunction)PyCursesWindow_AddStr, METH_VARARGS},
1991 {"attroff", (PyCFunction)PyCursesWindow_AttrOff, METH_VARARGS},
1992 {"attron", (PyCFunction)PyCursesWindow_AttrOn, METH_VARARGS},
1993 {"attrset", (PyCFunction)PyCursesWindow_AttrSet, METH_VARARGS},
1994 {"bkgd", (PyCFunction)PyCursesWindow_Bkgd, METH_VARARGS},
1995 {"chgat", (PyCFunction)PyCursesWindow_ChgAt, METH_VARARGS},
1996 {"bkgdset", (PyCFunction)PyCursesWindow_BkgdSet, METH_VARARGS},
1997 {"border", (PyCFunction)PyCursesWindow_Border, METH_VARARGS},
1998 {"box", (PyCFunction)PyCursesWindow_Box, METH_VARARGS},
1999 {"clear", (PyCFunction)PyCursesWindow_wclear, METH_NOARGS},
2000 {"clearok", (PyCFunction)PyCursesWindow_clearok, METH_VARARGS},
2001 {"clrtobot", (PyCFunction)PyCursesWindow_wclrtobot, METH_NOARGS},
2002 {"clrtoeol", (PyCFunction)PyCursesWindow_wclrtoeol, METH_NOARGS},
2003 {"cursyncup", (PyCFunction)PyCursesWindow_wcursyncup, METH_NOARGS},
2004 {"delch", (PyCFunction)PyCursesWindow_DelCh, METH_VARARGS},
2005 {"deleteln", (PyCFunction)PyCursesWindow_wdeleteln, METH_NOARGS},
2006 {"derwin", (PyCFunction)PyCursesWindow_DerWin, METH_VARARGS},
2007 {"echochar", (PyCFunction)PyCursesWindow_EchoChar, METH_VARARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002008#ifdef NCURSES_MOUSE_VERSION
Victor Stinner26486ea2010-05-15 22:23:53 +00002009 {"enclose", (PyCFunction)PyCursesWindow_Enclose, METH_VARARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002010#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00002011 {"erase", (PyCFunction)PyCursesWindow_werase, METH_NOARGS},
2012 {"getbegyx", (PyCFunction)PyCursesWindow_getbegyx, METH_NOARGS},
2013 {"getbkgd", (PyCFunction)PyCursesWindow_GetBkgd, METH_NOARGS},
2014 {"getch", (PyCFunction)PyCursesWindow_GetCh, METH_VARARGS},
2015 {"getkey", (PyCFunction)PyCursesWindow_GetKey, METH_VARARGS},
Nadeem Vawda9e2e9902011-07-31 15:01:11 +02002016#ifdef HAVE_NCURSESW
Victor Stinnera7878b72011-07-14 23:07:44 +02002017 {"get_wch", (PyCFunction)PyCursesWindow_Get_WCh, METH_VARARGS},
Nadeem Vawda9e2e9902011-07-31 15:01:11 +02002018#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00002019 {"getmaxyx", (PyCFunction)PyCursesWindow_getmaxyx, METH_NOARGS},
2020 {"getparyx", (PyCFunction)PyCursesWindow_getparyx, METH_NOARGS},
2021 {"getstr", (PyCFunction)PyCursesWindow_GetStr, METH_VARARGS},
2022 {"getyx", (PyCFunction)PyCursesWindow_getyx, METH_NOARGS},
2023 {"hline", (PyCFunction)PyCursesWindow_Hline, METH_VARARGS},
2024 {"idcok", (PyCFunction)PyCursesWindow_idcok, METH_VARARGS},
2025 {"idlok", (PyCFunction)PyCursesWindow_idlok, METH_VARARGS},
2026 {"immedok", (PyCFunction)PyCursesWindow_immedok, METH_VARARGS},
2027 {"inch", (PyCFunction)PyCursesWindow_InCh, METH_VARARGS},
2028 {"insch", (PyCFunction)PyCursesWindow_InsCh, METH_VARARGS},
2029 {"insdelln", (PyCFunction)PyCursesWindow_winsdelln, METH_VARARGS},
2030 {"insertln", (PyCFunction)PyCursesWindow_winsertln, METH_NOARGS},
2031 {"insnstr", (PyCFunction)PyCursesWindow_InsNStr, METH_VARARGS},
2032 {"insstr", (PyCFunction)PyCursesWindow_InsStr, METH_VARARGS},
2033 {"instr", (PyCFunction)PyCursesWindow_InStr, METH_VARARGS},
2034 {"is_linetouched", (PyCFunction)PyCursesWindow_Is_LineTouched, METH_VARARGS},
2035 {"is_wintouched", (PyCFunction)PyCursesWindow_is_wintouched, METH_NOARGS},
2036 {"keypad", (PyCFunction)PyCursesWindow_keypad, METH_VARARGS},
2037 {"leaveok", (PyCFunction)PyCursesWindow_leaveok, METH_VARARGS},
2038 {"move", (PyCFunction)PyCursesWindow_wmove, METH_VARARGS},
2039 {"mvderwin", (PyCFunction)PyCursesWindow_mvderwin, METH_VARARGS},
2040 {"mvwin", (PyCFunction)PyCursesWindow_mvwin, METH_VARARGS},
2041 {"nodelay", (PyCFunction)PyCursesWindow_nodelay, METH_VARARGS},
2042 {"notimeout", (PyCFunction)PyCursesWindow_notimeout, METH_VARARGS},
2043 {"noutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
Victor Stinner26486ea2010-05-15 22:23:53 +00002044 {"overlay", (PyCFunction)PyCursesWindow_Overlay, METH_VARARGS},
2045 {"overwrite", (PyCFunction)PyCursesWindow_Overwrite,
2046 METH_VARARGS},
2047 {"putwin", (PyCFunction)PyCursesWindow_PutWin, METH_O},
2048 {"redrawln", (PyCFunction)PyCursesWindow_RedrawLine, METH_VARARGS},
2049 {"redrawwin", (PyCFunction)PyCursesWindow_redrawwin, METH_NOARGS},
2050 {"refresh", (PyCFunction)PyCursesWindow_Refresh, METH_VARARGS},
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +00002051#ifndef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00002052 {"resize", (PyCFunction)PyCursesWindow_wresize, METH_VARARGS},
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002053#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00002054 {"scroll", (PyCFunction)PyCursesWindow_Scroll, METH_VARARGS},
2055 {"scrollok", (PyCFunction)PyCursesWindow_scrollok, METH_VARARGS},
2056 {"setscrreg", (PyCFunction)PyCursesWindow_SetScrollRegion, METH_VARARGS},
2057 {"standend", (PyCFunction)PyCursesWindow_wstandend, METH_NOARGS},
2058 {"standout", (PyCFunction)PyCursesWindow_wstandout, METH_NOARGS},
2059 {"subpad", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
2060 {"subwin", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
2061 {"syncdown", (PyCFunction)PyCursesWindow_wsyncdown, METH_NOARGS},
2062 {"syncok", (PyCFunction)PyCursesWindow_syncok, METH_VARARGS},
2063 {"syncup", (PyCFunction)PyCursesWindow_wsyncup, METH_NOARGS},
2064 {"timeout", (PyCFunction)PyCursesWindow_wtimeout, METH_VARARGS},
2065 {"touchline", (PyCFunction)PyCursesWindow_TouchLine, METH_VARARGS},
2066 {"touchwin", (PyCFunction)PyCursesWindow_touchwin, METH_NOARGS},
2067 {"untouchwin", (PyCFunction)PyCursesWindow_untouchwin, METH_NOARGS},
2068 {"vline", (PyCFunction)PyCursesWindow_Vline, METH_VARARGS},
2069 {NULL, NULL} /* sentinel */
Guido van Rossumf6971e21994-08-30 12:25:20 +00002070};
2071
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002072static PyGetSetDef PyCursesWindow_getsets[] = {
2073 {"encoding",
2074 (getter)PyCursesWindow_get_encoding,
2075 (setter)PyCursesWindow_set_encoding,
Ronald Oussoren2a7fe032012-01-17 16:53:20 +01002076 "the typecode character used to create the array"},
2077 {NULL, NULL, NULL, NULL } /* sentinel */
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002078};
2079
Guido van Rossumf6971e21994-08-30 12:25:20 +00002080/* -------------------------------------------------------*/
Guido van Rossum85738471995-02-17 13:50:17 +00002081
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002082PyTypeObject PyCursesWindow_Type = {
Victor Stinner26486ea2010-05-15 22:23:53 +00002083 PyVarObject_HEAD_INIT(NULL, 0)
2084 "_curses.curses window", /*tp_name*/
2085 sizeof(PyCursesWindowObject), /*tp_basicsize*/
2086 0, /*tp_itemsize*/
2087 /* methods */
2088 (destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/
2089 0, /*tp_print*/
2090 (getattrfunc)0, /*tp_getattr*/
2091 (setattrfunc)0, /*tp_setattr*/
2092 0, /*tp_reserved*/
2093 0, /*tp_repr*/
2094 0, /*tp_as_number*/
2095 0, /*tp_as_sequence*/
2096 0, /*tp_as_mapping*/
2097 0, /*tp_hash*/
2098 0, /*tp_call*/
2099 0, /*tp_str*/
2100 0, /*tp_getattro*/
2101 0, /*tp_setattro*/
2102 0, /*tp_as_buffer*/
2103 Py_TPFLAGS_DEFAULT, /*tp_flags*/
2104 0, /*tp_doc*/
2105 0, /*tp_traverse*/
2106 0, /*tp_clear*/
2107 0, /*tp_richcompare*/
2108 0, /*tp_weaklistoffset*/
2109 0, /*tp_iter*/
2110 0, /*tp_iternext*/
2111 PyCursesWindow_Methods, /*tp_methods*/
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002112 0, /* tp_members */
2113 PyCursesWindow_getsets, /* tp_getset */
Guido van Rossumf6971e21994-08-30 12:25:20 +00002114};
2115
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002116/*********************************************************************
2117 Global Functions
2118**********************************************************************/
Guido van Rossumf6971e21994-08-30 12:25:20 +00002119
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002120NoArgNoReturnFunction(beep)
2121NoArgNoReturnFunction(def_prog_mode)
2122NoArgNoReturnFunction(def_shell_mode)
2123NoArgNoReturnFunction(doupdate)
2124NoArgNoReturnFunction(endwin)
2125NoArgNoReturnFunction(flash)
2126NoArgNoReturnFunction(nocbreak)
2127NoArgNoReturnFunction(noecho)
2128NoArgNoReturnFunction(nonl)
2129NoArgNoReturnFunction(noraw)
2130NoArgNoReturnFunction(reset_prog_mode)
2131NoArgNoReturnFunction(reset_shell_mode)
2132NoArgNoReturnFunction(resetty)
2133NoArgNoReturnFunction(savetty)
2134
2135NoArgOrFlagNoReturnFunction(cbreak)
2136NoArgOrFlagNoReturnFunction(echo)
2137NoArgOrFlagNoReturnFunction(nl)
2138NoArgOrFlagNoReturnFunction(raw)
2139
2140NoArgReturnIntFunction(baudrate)
2141NoArgReturnIntFunction(termattrs)
2142
2143NoArgReturnStringFunction(termname)
2144NoArgReturnStringFunction(longname)
2145
2146NoArgTrueFalseFunction(can_change_color)
2147NoArgTrueFalseFunction(has_colors)
2148NoArgTrueFalseFunction(has_ic)
2149NoArgTrueFalseFunction(has_il)
2150NoArgTrueFalseFunction(isendwin)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002151NoArgNoReturnVoidFunction(flushinp)
2152NoArgNoReturnVoidFunction(noqiflush)
2153
2154static PyObject *
Christian Heimesaf98da12008-01-27 15:18:18 +00002155PyCurses_filter(PyObject *self)
2156{
Victor Stinner26486ea2010-05-15 22:23:53 +00002157 /* not checking for PyCursesInitialised here since filter() must
2158 be called before initscr() */
2159 filter();
2160 Py_INCREF(Py_None);
2161 return Py_None;
Christian Heimesaf98da12008-01-27 15:18:18 +00002162}
2163
2164static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002165PyCurses_Color_Content(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002166{
Victor Stinner26486ea2010-05-15 22:23:53 +00002167 short color,r,g,b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002168
Victor Stinner26486ea2010-05-15 22:23:53 +00002169 PyCursesInitialised;
2170 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002171
Victor Stinner26486ea2010-05-15 22:23:53 +00002172 if (!PyArg_ParseTuple(args, "h:color_content", &color)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002173
Victor Stinner26486ea2010-05-15 22:23:53 +00002174 if (color_content(color, &r, &g, &b) != ERR)
2175 return Py_BuildValue("(iii)", r, g, b);
2176 else {
2177 PyErr_SetString(PyCursesError,
2178 "Argument 1 was out of range. Check value of COLORS.");
2179 return NULL;
2180 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002181}
2182
2183static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002184PyCurses_color_pair(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002185{
Victor Stinner26486ea2010-05-15 22:23:53 +00002186 int n;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002187
Victor Stinner26486ea2010-05-15 22:23:53 +00002188 PyCursesInitialised;
2189 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002190
Victor Stinner26486ea2010-05-15 22:23:53 +00002191 if (!PyArg_ParseTuple(args, "i:color_pair", &n)) return NULL;
2192 return PyLong_FromLong((long) (n << 8));
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002193}
2194
2195static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002196PyCurses_Curs_Set(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002197{
Victor Stinner26486ea2010-05-15 22:23:53 +00002198 int vis,erg;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002199
Victor Stinner26486ea2010-05-15 22:23:53 +00002200 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002201
Victor Stinner26486ea2010-05-15 22:23:53 +00002202 if (!PyArg_ParseTuple(args, "i:curs_set", &vis)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002203
Victor Stinner26486ea2010-05-15 22:23:53 +00002204 erg = curs_set(vis);
2205 if (erg == ERR) return PyCursesCheckERR(erg, "curs_set");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002206
Victor Stinner26486ea2010-05-15 22:23:53 +00002207 return PyLong_FromLong((long) erg);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002208}
2209
2210static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002211PyCurses_Delay_Output(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002212{
Victor Stinner26486ea2010-05-15 22:23:53 +00002213 int ms;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002214
Victor Stinner26486ea2010-05-15 22:23:53 +00002215 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002216
Victor Stinner26486ea2010-05-15 22:23:53 +00002217 if (!PyArg_ParseTuple(args, "i:delay_output", &ms)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002218
Victor Stinner26486ea2010-05-15 22:23:53 +00002219 return PyCursesCheckERR(delay_output(ms), "delay_output");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002220}
2221
2222static PyObject *
Martin v. Löwisc0e16712002-01-17 23:08:27 +00002223PyCurses_EraseChar(PyObject *self)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002224{
Victor Stinner26486ea2010-05-15 22:23:53 +00002225 char ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002226
Victor Stinner26486ea2010-05-15 22:23:53 +00002227 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002228
Victor Stinner26486ea2010-05-15 22:23:53 +00002229 ch = erasechar();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002230
Victor Stinner26486ea2010-05-15 22:23:53 +00002231 return PyBytes_FromStringAndSize(&ch, 1);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002232}
2233
2234static PyObject *
Martin v. Löwisc0e16712002-01-17 23:08:27 +00002235PyCurses_getsyx(PyObject *self)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002236{
Victor Stinner26486ea2010-05-15 22:23:53 +00002237 int x = 0;
2238 int y = 0;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002239
Victor Stinner26486ea2010-05-15 22:23:53 +00002240 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002241
Victor Stinner26486ea2010-05-15 22:23:53 +00002242 getsyx(y, x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002243
Victor Stinner26486ea2010-05-15 22:23:53 +00002244 return Py_BuildValue("(ii)", y, x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002245}
2246
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002247#ifdef NCURSES_MOUSE_VERSION
2248static PyObject *
Martin v. Löwisc0e16712002-01-17 23:08:27 +00002249PyCurses_GetMouse(PyObject *self)
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002250{
Victor Stinner26486ea2010-05-15 22:23:53 +00002251 int rtn;
2252 MEVENT event;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002253
Victor Stinner26486ea2010-05-15 22:23:53 +00002254 PyCursesInitialised;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002255
Victor Stinner26486ea2010-05-15 22:23:53 +00002256 rtn = getmouse( &event );
2257 if (rtn == ERR) {
2258 PyErr_SetString(PyCursesError, "getmouse() returned ERR");
2259 return NULL;
2260 }
2261 return Py_BuildValue("(hiiil)",
2262 (short)event.id,
2263 event.x, event.y, event.z,
2264 (long) event.bstate);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002265}
2266
2267static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002268PyCurses_UngetMouse(PyObject *self, PyObject *args)
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002269{
Victor Stinner26486ea2010-05-15 22:23:53 +00002270 MEVENT event;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002271
Victor Stinner26486ea2010-05-15 22:23:53 +00002272 PyCursesInitialised;
2273 if (!PyArg_ParseTuple(args, "hiiil",
2274 &event.id,
2275 &event.x, &event.y, &event.z,
2276 (int *) &event.bstate))
2277 return NULL;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002278
Victor Stinner26486ea2010-05-15 22:23:53 +00002279 return PyCursesCheckERR(ungetmouse(&event), "ungetmouse");
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002280}
2281#endif
2282
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002283static PyObject *
Guido van Rossum6c95da32007-07-24 00:16:38 +00002284PyCurses_GetWin(PyCursesWindowObject *self, PyObject *stream)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002285{
Victor Stinner26486ea2010-05-15 22:23:53 +00002286 char fn[100];
Victor Stinnerdaf45552013-08-28 00:53:59 +02002287 int fd = -1;
2288 FILE *fp = NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00002289 PyObject *data;
2290 size_t datalen;
2291 WINDOW *win;
Benjamin Peterson18b71912013-05-16 15:29:44 -05002292 _Py_IDENTIFIER(read);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002293 PyObject *res = NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002294
Victor Stinner26486ea2010-05-15 22:23:53 +00002295 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002296
Victor Stinner26486ea2010-05-15 22:23:53 +00002297 strcpy(fn, "/tmp/py.curses.getwin.XXXXXX");
2298 fd = mkstemp(fn);
2299 if (fd < 0)
2300 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002301 if (_Py_set_inheritable(fd, 0, NULL) < 0)
2302 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00002303 fp = fdopen(fd, "wb+");
2304 if (fp == NULL) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02002305 PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
2306 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00002307 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02002308
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002309 data = _PyObject_CallMethodId(stream, &PyId_read, "");
Victor Stinnerdaf45552013-08-28 00:53:59 +02002310 if (data == NULL)
2311 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00002312 if (!PyBytes_Check(data)) {
2313 PyErr_Format(PyExc_TypeError,
2314 "f.read() returned %.100s instead of bytes",
2315 data->ob_type->tp_name);
2316 Py_DECREF(data);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002317 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00002318 }
2319 datalen = PyBytes_GET_SIZE(data);
2320 if (fwrite(PyBytes_AS_STRING(data), 1, datalen, fp) != datalen) {
2321 Py_DECREF(data);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002322 PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
2323 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00002324 }
Guido van Rossum6c95da32007-07-24 00:16:38 +00002325 Py_DECREF(data);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002326
Victor Stinner26486ea2010-05-15 22:23:53 +00002327 fseek(fp, 0, 0);
2328 win = getwin(fp);
Victor Stinner26486ea2010-05-15 22:23:53 +00002329 if (win == NULL) {
2330 PyErr_SetString(PyCursesError, catchall_NULL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002331 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00002332 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02002333 res = PyCursesWindow_New(win, NULL);
2334
2335error:
2336 if (fp != NULL)
2337 fclose(fp);
2338 else if (fd != -1)
2339 close(fd);
2340 remove(fn);
2341 return res;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002342}
2343
2344static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002345PyCurses_HalfDelay(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002346{
Victor Stinner26486ea2010-05-15 22:23:53 +00002347 unsigned char tenths;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002348
Victor Stinner26486ea2010-05-15 22:23:53 +00002349 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002350
Victor Stinner26486ea2010-05-15 22:23:53 +00002351 if (!PyArg_ParseTuple(args, "b:halfdelay", &tenths)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002352
Victor Stinner26486ea2010-05-15 22:23:53 +00002353 return PyCursesCheckERR(halfdelay(tenths), "halfdelay");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002354}
2355
Andrew M. Kuchlingbf680262000-07-25 12:37:41 +00002356#ifndef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00002357/* No has_key! */
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002358static PyObject * PyCurses_has_key(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002359{
Victor Stinner26486ea2010-05-15 22:23:53 +00002360 int ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002361
Victor Stinner26486ea2010-05-15 22:23:53 +00002362 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002363
Victor Stinner26486ea2010-05-15 22:23:53 +00002364 if (!PyArg_ParseTuple(args,"i",&ch)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002365
Victor Stinner26486ea2010-05-15 22:23:53 +00002366 if (has_key(ch) == FALSE) {
2367 Py_INCREF(Py_False);
2368 return Py_False;
2369 }
2370 Py_INCREF(Py_True);
2371 return Py_True;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002372}
Andrew M. Kuchlingbf680262000-07-25 12:37:41 +00002373#endif /* STRICT_SYSV_CURSES */
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002374
2375static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002376PyCurses_Init_Color(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002377{
Victor Stinner26486ea2010-05-15 22:23:53 +00002378 short color, r, g, b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002379
Victor Stinner26486ea2010-05-15 22:23:53 +00002380 PyCursesInitialised;
2381 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002382
Victor Stinner26486ea2010-05-15 22:23:53 +00002383 switch(PyTuple_Size(args)) {
2384 case 4:
2385 if (!PyArg_ParseTuple(args, "hhhh;color,r,g,b", &color, &r, &g, &b)) return NULL;
2386 break;
2387 default:
2388 PyErr_SetString(PyExc_TypeError, "init_color requires 4 arguments");
2389 return NULL;
2390 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002391
Victor Stinner26486ea2010-05-15 22:23:53 +00002392 return PyCursesCheckERR(init_color(color, r, g, b), "init_color");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002393}
2394
2395static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002396PyCurses_Init_Pair(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002397{
Victor Stinner26486ea2010-05-15 22:23:53 +00002398 short pair, f, b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002399
Victor Stinner26486ea2010-05-15 22:23:53 +00002400 PyCursesInitialised;
2401 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002402
Victor Stinner26486ea2010-05-15 22:23:53 +00002403 if (PyTuple_Size(args) != 3) {
2404 PyErr_SetString(PyExc_TypeError, "init_pair requires 3 arguments");
2405 return NULL;
2406 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002407
Victor Stinner26486ea2010-05-15 22:23:53 +00002408 if (!PyArg_ParseTuple(args, "hhh;pair, f, b", &pair, &f, &b)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002409
Victor Stinner26486ea2010-05-15 22:23:53 +00002410 return PyCursesCheckERR(init_pair(pair, f, b), "init_pair");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002411}
2412
Andrew M. Kuchling32552682000-12-22 21:52:27 +00002413static PyObject *ModDict;
2414
Victor Stinner26486ea2010-05-15 22:23:53 +00002415static PyObject *
Martin v. Löwisc0e16712002-01-17 23:08:27 +00002416PyCurses_InitScr(PyObject *self)
Guido van Rossumf6971e21994-08-30 12:25:20 +00002417{
Victor Stinner26486ea2010-05-15 22:23:53 +00002418 WINDOW *win;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002419 PyCursesWindowObject *winobj;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002420
Victor Stinner26486ea2010-05-15 22:23:53 +00002421 if (initialised == TRUE) {
2422 wrefresh(stdscr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002423 return (PyObject *)PyCursesWindow_New(stdscr, NULL);
Victor Stinner26486ea2010-05-15 22:23:53 +00002424 }
Guido van Rossum56bf2351994-08-31 22:06:24 +00002425
Victor Stinner26486ea2010-05-15 22:23:53 +00002426 win = initscr();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002427
Victor Stinner26486ea2010-05-15 22:23:53 +00002428 if (win == NULL) {
2429 PyErr_SetString(PyCursesError, catchall_NULL);
2430 return NULL;
2431 }
Guido van Rossum85738471995-02-17 13:50:17 +00002432
Victor Stinner26486ea2010-05-15 22:23:53 +00002433 initialised = initialised_setupterm = TRUE;
Guido van Rossum56bf2351994-08-31 22:06:24 +00002434
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00002435/* This was moved from initcurses() because it core dumped on SGI,
2436 where they're not defined until you've called initscr() */
Victor Stinner26486ea2010-05-15 22:23:53 +00002437#define SetDictInt(string,ch) \
2438 do { \
2439 PyObject *o = PyLong_FromLong((long) (ch)); \
2440 if (o && PyDict_SetItemString(ModDict, string, o) == 0) { \
2441 Py_DECREF(o); \
2442 } \
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002443 } while (0)
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00002444
Victor Stinner26486ea2010-05-15 22:23:53 +00002445 /* Here are some graphic symbols you can use */
2446 SetDictInt("ACS_ULCORNER", (ACS_ULCORNER));
2447 SetDictInt("ACS_LLCORNER", (ACS_LLCORNER));
2448 SetDictInt("ACS_URCORNER", (ACS_URCORNER));
2449 SetDictInt("ACS_LRCORNER", (ACS_LRCORNER));
2450 SetDictInt("ACS_LTEE", (ACS_LTEE));
2451 SetDictInt("ACS_RTEE", (ACS_RTEE));
2452 SetDictInt("ACS_BTEE", (ACS_BTEE));
2453 SetDictInt("ACS_TTEE", (ACS_TTEE));
2454 SetDictInt("ACS_HLINE", (ACS_HLINE));
2455 SetDictInt("ACS_VLINE", (ACS_VLINE));
2456 SetDictInt("ACS_PLUS", (ACS_PLUS));
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00002457#if !defined(__hpux) || defined(HAVE_NCURSES_H)
Victor Stinner26486ea2010-05-15 22:23:53 +00002458 /* On HP/UX 11, these are of type cchar_t, which is not an
2459 integral type. If this is a problem on more platforms, a
2460 configure test should be added to determine whether ACS_S1
2461 is of integral type. */
2462 SetDictInt("ACS_S1", (ACS_S1));
2463 SetDictInt("ACS_S9", (ACS_S9));
2464 SetDictInt("ACS_DIAMOND", (ACS_DIAMOND));
2465 SetDictInt("ACS_CKBOARD", (ACS_CKBOARD));
2466 SetDictInt("ACS_DEGREE", (ACS_DEGREE));
2467 SetDictInt("ACS_PLMINUS", (ACS_PLMINUS));
2468 SetDictInt("ACS_BULLET", (ACS_BULLET));
2469 SetDictInt("ACS_LARROW", (ACS_LARROW));
2470 SetDictInt("ACS_RARROW", (ACS_RARROW));
2471 SetDictInt("ACS_DARROW", (ACS_DARROW));
2472 SetDictInt("ACS_UARROW", (ACS_UARROW));
2473 SetDictInt("ACS_BOARD", (ACS_BOARD));
2474 SetDictInt("ACS_LANTERN", (ACS_LANTERN));
2475 SetDictInt("ACS_BLOCK", (ACS_BLOCK));
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00002476#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00002477 SetDictInt("ACS_BSSB", (ACS_ULCORNER));
2478 SetDictInt("ACS_SSBB", (ACS_LLCORNER));
2479 SetDictInt("ACS_BBSS", (ACS_URCORNER));
2480 SetDictInt("ACS_SBBS", (ACS_LRCORNER));
2481 SetDictInt("ACS_SBSS", (ACS_RTEE));
2482 SetDictInt("ACS_SSSB", (ACS_LTEE));
2483 SetDictInt("ACS_SSBS", (ACS_BTEE));
2484 SetDictInt("ACS_BSSS", (ACS_TTEE));
2485 SetDictInt("ACS_BSBS", (ACS_HLINE));
2486 SetDictInt("ACS_SBSB", (ACS_VLINE));
2487 SetDictInt("ACS_SSSS", (ACS_PLUS));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002488
Victor Stinner26486ea2010-05-15 22:23:53 +00002489 /* The following are never available with strict SYSV curses */
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002490#ifdef ACS_S3
Victor Stinner26486ea2010-05-15 22:23:53 +00002491 SetDictInt("ACS_S3", (ACS_S3));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002492#endif
2493#ifdef ACS_S7
Victor Stinner26486ea2010-05-15 22:23:53 +00002494 SetDictInt("ACS_S7", (ACS_S7));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002495#endif
2496#ifdef ACS_LEQUAL
Victor Stinner26486ea2010-05-15 22:23:53 +00002497 SetDictInt("ACS_LEQUAL", (ACS_LEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002498#endif
2499#ifdef ACS_GEQUAL
Victor Stinner26486ea2010-05-15 22:23:53 +00002500 SetDictInt("ACS_GEQUAL", (ACS_GEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002501#endif
2502#ifdef ACS_PI
Victor Stinner26486ea2010-05-15 22:23:53 +00002503 SetDictInt("ACS_PI", (ACS_PI));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002504#endif
2505#ifdef ACS_NEQUAL
Victor Stinner26486ea2010-05-15 22:23:53 +00002506 SetDictInt("ACS_NEQUAL", (ACS_NEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00002507#endif
2508#ifdef ACS_STERLING
Victor Stinner26486ea2010-05-15 22:23:53 +00002509 SetDictInt("ACS_STERLING", (ACS_STERLING));
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00002510#endif
2511
Victor Stinner26486ea2010-05-15 22:23:53 +00002512 SetDictInt("LINES", LINES);
2513 SetDictInt("COLS", COLS);
Guido van Rossum56bf2351994-08-31 22:06:24 +00002514
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002515 winobj = (PyCursesWindowObject *)PyCursesWindow_New(win, NULL);
2516 screen_encoding = winobj->encoding;
2517 return (PyObject *)winobj;
Guido van Rossumf6971e21994-08-30 12:25:20 +00002518}
2519
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002520static PyObject *
2521PyCurses_setupterm(PyObject* self, PyObject *args, PyObject* keywds)
2522{
Victor Stinner26486ea2010-05-15 22:23:53 +00002523 int fd = -1;
2524 int err;
2525 char* termstr = NULL;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002526
Victor Stinner26486ea2010-05-15 22:23:53 +00002527 static char *kwlist[] = {"term", "fd", NULL};
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002528
Victor Stinner26486ea2010-05-15 22:23:53 +00002529 if (!PyArg_ParseTupleAndKeywords(
2530 args, keywds, "|zi:setupterm", kwlist, &termstr, &fd)) {
2531 return NULL;
2532 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002533
Victor Stinner26486ea2010-05-15 22:23:53 +00002534 if (fd == -1) {
2535 PyObject* sys_stdout;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002536
Victor Stinnerbd303c12013-11-07 23:07:29 +01002537 sys_stdout = PySys_GetObject("stdout");
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002538
Victor Stinner26486ea2010-05-15 22:23:53 +00002539 if (sys_stdout == NULL || sys_stdout == Py_None) {
2540 PyErr_SetString(
2541 PyCursesError,
2542 "lost sys.stdout");
2543 return NULL;
2544 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002545
Victor Stinner26486ea2010-05-15 22:23:53 +00002546 fd = PyObject_AsFileDescriptor(sys_stdout);
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002547
Victor Stinner26486ea2010-05-15 22:23:53 +00002548 if (fd == -1) {
2549 return NULL;
2550 }
2551 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002552
Matthias Klose635edd12010-07-30 21:40:57 +00002553 if (!initialised_setupterm && setupterm(termstr,fd,&err) == ERR) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002554 char* s = "setupterm: unknown error";
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002555
Victor Stinner26486ea2010-05-15 22:23:53 +00002556 if (err == 0) {
2557 s = "setupterm: could not find terminal";
2558 } else if (err == -1) {
2559 s = "setupterm: could not find terminfo database";
2560 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002561
Victor Stinner26486ea2010-05-15 22:23:53 +00002562 PyErr_SetString(PyCursesError,s);
2563 return NULL;
2564 }
2565
2566 initialised_setupterm = TRUE;
2567
2568 Py_INCREF(Py_None);
2569 return Py_None;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00002570}
Guido van Rossumf6971e21994-08-30 12:25:20 +00002571
2572static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002573PyCurses_IntrFlush(PyObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00002574{
Victor Stinner26486ea2010-05-15 22:23:53 +00002575 int ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002576
Victor Stinner26486ea2010-05-15 22:23:53 +00002577 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002578
Victor Stinner26486ea2010-05-15 22:23:53 +00002579 switch(PyTuple_Size(args)) {
2580 case 1:
2581 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL;
2582 break;
2583 default:
2584 PyErr_SetString(PyExc_TypeError, "intrflush requires 1 argument");
2585 return NULL;
2586 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002587
Victor Stinner26486ea2010-05-15 22:23:53 +00002588 return PyCursesCheckERR(intrflush(NULL,ch), "intrflush");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002589}
2590
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002591#ifdef HAVE_CURSES_IS_TERM_RESIZED
2592static PyObject *
2593PyCurses_Is_Term_Resized(PyObject *self, PyObject *args)
2594{
Victor Stinner26486ea2010-05-15 22:23:53 +00002595 int lines;
2596 int columns;
2597 int result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002598
Victor Stinner26486ea2010-05-15 22:23:53 +00002599 PyCursesInitialised;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002600
Victor Stinner26486ea2010-05-15 22:23:53 +00002601 if (!PyArg_ParseTuple(args,"ii:is_term_resized", &lines, &columns))
2602 return NULL;
2603 result = is_term_resized(lines, columns);
2604 if (result == TRUE) {
2605 Py_INCREF(Py_True);
2606 return Py_True;
2607 } else {
2608 Py_INCREF(Py_False);
2609 return Py_False;
2610 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002611}
2612#endif /* HAVE_CURSES_IS_TERM_RESIZED */
2613
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00002614#if !defined(__NetBSD__)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002615static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002616PyCurses_KeyName(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002617{
Victor Stinner26486ea2010-05-15 22:23:53 +00002618 const char *knp;
2619 int ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002620
Victor Stinner26486ea2010-05-15 22:23:53 +00002621 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002622
Victor Stinner26486ea2010-05-15 22:23:53 +00002623 if (!PyArg_ParseTuple(args,"i",&ch)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002624
Victor Stinner26486ea2010-05-15 22:23:53 +00002625 if (ch < 0) {
2626 PyErr_SetString(PyExc_ValueError, "invalid key number");
2627 return NULL;
2628 }
2629 knp = keyname(ch);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002630
Serhiy Storchaka20b39b22014-09-28 11:27:24 +03002631 return PyBytes_FromString((knp == NULL) ? "" : knp);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002632}
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00002633#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002634
Victor Stinner26486ea2010-05-15 22:23:53 +00002635static PyObject *
2636PyCurses_KillChar(PyObject *self)
2637{
2638 char ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002639
Victor Stinner26486ea2010-05-15 22:23:53 +00002640 ch = killchar();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002641
Victor Stinner26486ea2010-05-15 22:23:53 +00002642 return PyBytes_FromStringAndSize(&ch, 1);
2643}
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002644
2645static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002646PyCurses_Meta(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002647{
Victor Stinner26486ea2010-05-15 22:23:53 +00002648 int ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002649
Victor Stinner26486ea2010-05-15 22:23:53 +00002650 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002651
Victor Stinner26486ea2010-05-15 22:23:53 +00002652 switch(PyTuple_Size(args)) {
2653 case 1:
2654 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL;
2655 break;
2656 default:
2657 PyErr_SetString(PyExc_TypeError, "meta requires 1 argument");
2658 return NULL;
2659 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002660
Victor Stinner26486ea2010-05-15 22:23:53 +00002661 return PyCursesCheckERR(meta(stdscr, ch), "meta");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002662}
2663
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002664#ifdef NCURSES_MOUSE_VERSION
2665static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002666PyCurses_MouseInterval(PyObject *self, PyObject *args)
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002667{
Victor Stinner26486ea2010-05-15 22:23:53 +00002668 int interval;
2669 PyCursesInitialised;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002670
Victor Stinner26486ea2010-05-15 22:23:53 +00002671 if (!PyArg_ParseTuple(args,"i;interval",&interval))
2672 return NULL;
2673 return PyCursesCheckERR(mouseinterval(interval), "mouseinterval");
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002674}
2675
2676static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002677PyCurses_MouseMask(PyObject *self, PyObject *args)
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002678{
Victor Stinner26486ea2010-05-15 22:23:53 +00002679 int newmask;
2680 mmask_t oldmask, availmask;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002681
Victor Stinner26486ea2010-05-15 22:23:53 +00002682 PyCursesInitialised;
2683 if (!PyArg_ParseTuple(args,"i;mousemask",&newmask))
2684 return NULL;
2685 availmask = mousemask(newmask, &oldmask);
2686 return Py_BuildValue("(ll)", (long)availmask, (long)oldmask);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002687}
2688#endif
2689
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002690static PyObject *
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +00002691PyCurses_Napms(PyObject *self, PyObject *args)
2692{
2693 int ms;
2694
Andrew M. Kuchling2b146762010-05-10 23:30:25 +00002695 PyCursesInitialised;
Martin v. Löwisc0e16712002-01-17 23:08:27 +00002696 if (!PyArg_ParseTuple(args, "i;ms", &ms)) return NULL;
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +00002697
2698 return Py_BuildValue("i", napms(ms));
2699}
2700
2701
2702static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002703PyCurses_NewPad(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002704{
Victor Stinner26486ea2010-05-15 22:23:53 +00002705 WINDOW *win;
2706 int nlines, ncols;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002707
Victor Stinner26486ea2010-05-15 22:23:53 +00002708 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002709
Victor Stinner26486ea2010-05-15 22:23:53 +00002710 if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002711
Victor Stinner26486ea2010-05-15 22:23:53 +00002712 win = newpad(nlines, ncols);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002713
Victor Stinner26486ea2010-05-15 22:23:53 +00002714 if (win == NULL) {
2715 PyErr_SetString(PyCursesError, catchall_NULL);
2716 return NULL;
2717 }
2718
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002719 return (PyObject *)PyCursesWindow_New(win, NULL);
Guido van Rossumf6971e21994-08-30 12:25:20 +00002720}
2721
2722static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002723PyCurses_NewWindow(PyObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00002724{
Victor Stinner26486ea2010-05-15 22:23:53 +00002725 WINDOW *win;
2726 int nlines, ncols, begin_y=0, begin_x=0;
Guido van Rossum85738471995-02-17 13:50:17 +00002727
Victor Stinner26486ea2010-05-15 22:23:53 +00002728 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002729
Victor Stinner26486ea2010-05-15 22:23:53 +00002730 switch (PyTuple_Size(args)) {
2731 case 2:
2732 if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols))
2733 return NULL;
2734 break;
2735 case 4:
2736 if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
2737 &nlines,&ncols,&begin_y,&begin_x))
2738 return NULL;
2739 break;
2740 default:
2741 PyErr_SetString(PyExc_TypeError, "newwin requires 2 or 4 arguments");
2742 return NULL;
2743 }
Guido van Rossum85738471995-02-17 13:50:17 +00002744
Victor Stinner26486ea2010-05-15 22:23:53 +00002745 win = newwin(nlines,ncols,begin_y,begin_x);
2746 if (win == NULL) {
2747 PyErr_SetString(PyCursesError, catchall_NULL);
2748 return NULL;
2749 }
Guido van Rossum85738471995-02-17 13:50:17 +00002750
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002751 return (PyObject *)PyCursesWindow_New(win, NULL);
Guido van Rossumf6971e21994-08-30 12:25:20 +00002752}
2753
2754static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002755PyCurses_Pair_Content(PyObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00002756{
Victor Stinner26486ea2010-05-15 22:23:53 +00002757 short pair,f,b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002758
Victor Stinner26486ea2010-05-15 22:23:53 +00002759 PyCursesInitialised;
2760 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002761
Victor Stinner26486ea2010-05-15 22:23:53 +00002762 switch(PyTuple_Size(args)) {
2763 case 1:
2764 if (!PyArg_ParseTuple(args, "h;pair", &pair)) return NULL;
2765 break;
2766 default:
2767 PyErr_SetString(PyExc_TypeError, "pair_content requires 1 argument");
2768 return NULL;
2769 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002770
Victor Stinner26486ea2010-05-15 22:23:53 +00002771 if (pair_content(pair, &f, &b)==ERR) {
2772 PyErr_SetString(PyCursesError,
2773 "Argument 1 was out of range. (1..COLOR_PAIRS-1)");
2774 return NULL;
2775 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002776
Victor Stinner26486ea2010-05-15 22:23:53 +00002777 return Py_BuildValue("(ii)", f, b);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002778}
2779
2780static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002781PyCurses_pair_number(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002782{
Victor Stinner26486ea2010-05-15 22:23:53 +00002783 int n;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002784
Victor Stinner26486ea2010-05-15 22:23:53 +00002785 PyCursesInitialised;
2786 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002787
Victor Stinner26486ea2010-05-15 22:23:53 +00002788 switch(PyTuple_Size(args)) {
2789 case 1:
2790 if (!PyArg_ParseTuple(args, "i;pairvalue", &n)) return NULL;
2791 break;
2792 default:
2793 PyErr_SetString(PyExc_TypeError,
2794 "pair_number requires 1 argument");
2795 return NULL;
2796 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002797
Victor Stinner26486ea2010-05-15 22:23:53 +00002798 return PyLong_FromLong((long) ((n & A_COLOR) >> 8));
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002799}
2800
2801static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002802PyCurses_Putp(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002803{
Victor Stinner26486ea2010-05-15 22:23:53 +00002804 char *str;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002805
Victor Stinnerb3bc7e72011-11-03 20:35:40 +01002806 if (!PyArg_ParseTuple(args,"y;str", &str))
2807 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00002808 return PyCursesCheckERR(putp(str), "putp");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002809}
2810
2811static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002812PyCurses_QiFlush(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002813{
Victor Stinner26486ea2010-05-15 22:23:53 +00002814 int flag = 0;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002815
Victor Stinner26486ea2010-05-15 22:23:53 +00002816 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002817
Victor Stinner26486ea2010-05-15 22:23:53 +00002818 switch(PyTuple_Size(args)) {
2819 case 0:
2820 qiflush();
2821 Py_INCREF(Py_None);
2822 return Py_None;
2823 case 1:
2824 if (!PyArg_ParseTuple(args, "i;True(1) or False(0)", &flag)) return NULL;
2825 if (flag) qiflush();
2826 else noqiflush();
2827 Py_INCREF(Py_None);
2828 return Py_None;
2829 default:
2830 PyErr_SetString(PyExc_TypeError, "qiflush requires 0 or 1 arguments");
2831 return NULL;
2832 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002833}
2834
Guido van Rossumd8faa362007-04-27 19:54:29 +00002835/* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES
2836 * and _curses.COLS */
Benjamin Petersonfea6a942008-07-02 16:11:42 +00002837#if defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002838static int
2839update_lines_cols(void)
2840{
Victor Stinner26486ea2010-05-15 22:23:53 +00002841 PyObject *o;
2842 PyObject *m = PyImport_ImportModuleNoBlock("curses");
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002843 _Py_IDENTIFIER(LINES);
2844 _Py_IDENTIFIER(COLS);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002845
Victor Stinner26486ea2010-05-15 22:23:53 +00002846 if (!m)
2847 return 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002848
Victor Stinner26486ea2010-05-15 22:23:53 +00002849 o = PyLong_FromLong(LINES);
2850 if (!o) {
2851 Py_DECREF(m);
2852 return 0;
2853 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002854 if (_PyObject_SetAttrId(m, &PyId_LINES, o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002855 Py_DECREF(m);
2856 Py_DECREF(o);
2857 return 0;
2858 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002859 /* PyId_LINES.object will be initialized here. */
2860 if (PyDict_SetItem(ModDict, PyId_LINES.object, o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002861 Py_DECREF(m);
2862 Py_DECREF(o);
2863 return 0;
2864 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002865 Py_DECREF(o);
Victor Stinner26486ea2010-05-15 22:23:53 +00002866 o = PyLong_FromLong(COLS);
2867 if (!o) {
2868 Py_DECREF(m);
2869 return 0;
2870 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002871 if (_PyObject_SetAttrId(m, &PyId_COLS, o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002872 Py_DECREF(m);
2873 Py_DECREF(o);
2874 return 0;
2875 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002876 if (PyDict_SetItem(ModDict, PyId_COLS.object, o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002877 Py_DECREF(m);
2878 Py_DECREF(o);
2879 return 0;
2880 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002881 Py_DECREF(o);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002882 Py_DECREF(m);
Victor Stinner26486ea2010-05-15 22:23:53 +00002883 return 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002884}
Steve Dowerd2bc3892015-04-15 18:06:05 -04002885
2886static PyObject *
2887PyCurses_update_lines_cols(PyObject *self)
2888{
2889 return PyLong_FromLong((long) update_lines_cols());
2890}
2891
Benjamin Petersonfea6a942008-07-02 16:11:42 +00002892#endif
Guido van Rossumd8faa362007-04-27 19:54:29 +00002893
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002894#ifdef HAVE_CURSES_RESIZETERM
2895static PyObject *
2896PyCurses_ResizeTerm(PyObject *self, PyObject *args)
2897{
Victor Stinner26486ea2010-05-15 22:23:53 +00002898 int lines;
2899 int columns;
2900 PyObject *result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002901
Victor Stinner26486ea2010-05-15 22:23:53 +00002902 PyCursesInitialised;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002903
Victor Stinner26486ea2010-05-15 22:23:53 +00002904 if (!PyArg_ParseTuple(args,"ii:resizeterm", &lines, &columns))
2905 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002906
Victor Stinner26486ea2010-05-15 22:23:53 +00002907 result = PyCursesCheckERR(resizeterm(lines, columns), "resizeterm");
2908 if (!result)
2909 return NULL;
2910 if (!update_lines_cols())
2911 return NULL;
2912 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002913}
2914
2915#endif
2916
2917#ifdef HAVE_CURSES_RESIZE_TERM
2918static PyObject *
2919PyCurses_Resize_Term(PyObject *self, PyObject *args)
2920{
Victor Stinner26486ea2010-05-15 22:23:53 +00002921 int lines;
2922 int columns;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002923
Victor Stinner26486ea2010-05-15 22:23:53 +00002924 PyObject *result;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002925
Victor Stinner26486ea2010-05-15 22:23:53 +00002926 PyCursesInitialised;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002927
Victor Stinner26486ea2010-05-15 22:23:53 +00002928 if (!PyArg_ParseTuple(args,"ii:resize_term", &lines, &columns))
2929 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002930
Victor Stinner26486ea2010-05-15 22:23:53 +00002931 result = PyCursesCheckERR(resize_term(lines, columns), "resize_term");
2932 if (!result)
2933 return NULL;
2934 if (!update_lines_cols())
2935 return NULL;
2936 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002937}
2938#endif /* HAVE_CURSES_RESIZE_TERM */
2939
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002940static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00002941PyCurses_setsyx(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002942{
Victor Stinner26486ea2010-05-15 22:23:53 +00002943 int y,x;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002944
Victor Stinner26486ea2010-05-15 22:23:53 +00002945 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002946
Victor Stinner26486ea2010-05-15 22:23:53 +00002947 if (PyTuple_Size(args)!=2) {
2948 PyErr_SetString(PyExc_TypeError, "setsyx requires 2 arguments");
2949 return NULL;
2950 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002951
Victor Stinner26486ea2010-05-15 22:23:53 +00002952 if (!PyArg_ParseTuple(args, "ii;y, x", &y, &x)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002953
Victor Stinner26486ea2010-05-15 22:23:53 +00002954 setsyx(y,x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002955
Victor Stinner26486ea2010-05-15 22:23:53 +00002956 Py_INCREF(Py_None);
2957 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +00002958}
2959
2960static PyObject *
Martin v. Löwisc0e16712002-01-17 23:08:27 +00002961PyCurses_Start_Color(PyObject *self)
Guido van Rossumf6971e21994-08-30 12:25:20 +00002962{
Victor Stinner26486ea2010-05-15 22:23:53 +00002963 int code;
2964 PyObject *c, *cp;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002965
Victor Stinner26486ea2010-05-15 22:23:53 +00002966 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002967
Victor Stinner26486ea2010-05-15 22:23:53 +00002968 code = start_color();
2969 if (code != ERR) {
2970 initialisedcolors = TRUE;
2971 c = PyLong_FromLong((long) COLORS);
Christian Heimesa956e642013-07-26 14:45:37 +02002972 if (c == NULL)
2973 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00002974 PyDict_SetItemString(ModDict, "COLORS", c);
2975 Py_DECREF(c);
2976 cp = PyLong_FromLong((long) COLOR_PAIRS);
Christian Heimesa956e642013-07-26 14:45:37 +02002977 if (cp == NULL)
2978 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00002979 PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp);
2980 Py_DECREF(cp);
2981 Py_INCREF(Py_None);
2982 return Py_None;
2983 } else {
2984 PyErr_SetString(PyCursesError, "start_color() returned ERR");
2985 return NULL;
2986 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002987}
2988
2989static PyObject *
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002990PyCurses_tigetflag(PyObject *self, PyObject *args)
2991{
Victor Stinner26486ea2010-05-15 22:23:53 +00002992 char *capname;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002993
Victor Stinner26486ea2010-05-15 22:23:53 +00002994 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00002995
Victor Stinner26486ea2010-05-15 22:23:53 +00002996 if (!PyArg_ParseTuple(args, "s", &capname))
2997 return NULL;
2998
2999 return PyLong_FromLong( (long) tigetflag( capname ) );
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00003000}
3001
3002static PyObject *
3003PyCurses_tigetnum(PyObject *self, PyObject *args)
3004{
Victor Stinner26486ea2010-05-15 22:23:53 +00003005 char *capname;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00003006
Victor Stinner26486ea2010-05-15 22:23:53 +00003007 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00003008
Victor Stinner26486ea2010-05-15 22:23:53 +00003009 if (!PyArg_ParseTuple(args, "s", &capname))
3010 return NULL;
3011
3012 return PyLong_FromLong( (long) tigetnum( capname ) );
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00003013}
3014
3015static PyObject *
3016PyCurses_tigetstr(PyObject *self, PyObject *args)
3017{
Victor Stinner26486ea2010-05-15 22:23:53 +00003018 char *capname;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00003019
Victor Stinner26486ea2010-05-15 22:23:53 +00003020 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00003021
Victor Stinner26486ea2010-05-15 22:23:53 +00003022 if (!PyArg_ParseTuple(args, "s", &capname))
3023 return NULL;
3024
3025 capname = tigetstr( capname );
3026 if (capname == 0 || capname == (char*) -1) {
3027 Py_INCREF(Py_None);
3028 return Py_None;
3029 }
3030 return PyBytes_FromString( capname );
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00003031}
3032
3033static PyObject *
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00003034PyCurses_tparm(PyObject *self, PyObject *args)
3035{
Victor Stinner26486ea2010-05-15 22:23:53 +00003036 char* fmt;
3037 char* result = NULL;
3038 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 +00003039
Victor Stinner26486ea2010-05-15 22:23:53 +00003040 PyCursesSetupTermCalled;
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00003041
Victor Stinner26621332011-11-02 23:45:29 +01003042 if (!PyArg_ParseTuple(args, "y|iiiiiiiii:tparm",
Victor Stinner26486ea2010-05-15 22:23:53 +00003043 &fmt, &i1, &i2, &i3, &i4,
3044 &i5, &i6, &i7, &i8, &i9)) {
3045 return NULL;
3046 }
Michael W. Hudson21007982002-11-21 14:17:51 +00003047
Victor Stinner26486ea2010-05-15 22:23:53 +00003048 result = tparm(fmt,i1,i2,i3,i4,i5,i6,i7,i8,i9);
3049 if (!result) {
3050 PyErr_SetString(PyCursesError, "tparm() returned NULL");
3051 return NULL;
3052 }
Michael W. Hudson21007982002-11-21 14:17:51 +00003053
Victor Stinner26486ea2010-05-15 22:23:53 +00003054 return PyBytes_FromString(result);
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00003055}
3056
3057static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00003058PyCurses_TypeAhead(PyObject *self, PyObject *args)
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00003059{
Victor Stinner26486ea2010-05-15 22:23:53 +00003060 int fd;
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00003061
Victor Stinner26486ea2010-05-15 22:23:53 +00003062 PyCursesInitialised;
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00003063
Victor Stinner26486ea2010-05-15 22:23:53 +00003064 if (!PyArg_ParseTuple(args,"i;fd",&fd)) return NULL;
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00003065
Victor Stinner26486ea2010-05-15 22:23:53 +00003066 return PyCursesCheckERR(typeahead( fd ), "typeahead");
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00003067}
3068
3069static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00003070PyCurses_UnCtrl(PyObject *self, PyObject *args)
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003071{
Victor Stinner26486ea2010-05-15 22:23:53 +00003072 PyObject *temp;
3073 chtype ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003074
Victor Stinner26486ea2010-05-15 22:23:53 +00003075 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003076
Victor Stinner26486ea2010-05-15 22:23:53 +00003077 if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003078
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003079 if (!PyCurses_ConvertToChtype(NULL, temp, &ch))
Victor Stinner26486ea2010-05-15 22:23:53 +00003080 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003081
Victor Stinner26486ea2010-05-15 22:23:53 +00003082 return PyBytes_FromString(unctrl(ch));
Guido van Rossumf6971e21994-08-30 12:25:20 +00003083}
3084
3085static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00003086PyCurses_UngetCh(PyObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00003087{
Victor Stinner26486ea2010-05-15 22:23:53 +00003088 PyObject *temp;
3089 chtype ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003090
Victor Stinner26486ea2010-05-15 22:23:53 +00003091 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003092
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003093 if (!PyArg_ParseTuple(args,"O;ch or int",&temp))
Victor Stinner26486ea2010-05-15 22:23:53 +00003094 return NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003095
3096 if (!PyCurses_ConvertToChtype(NULL, temp, &ch))
3097 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003098
Victor Stinner26486ea2010-05-15 22:23:53 +00003099 return PyCursesCheckERR(ungetch(ch), "ungetch");
Guido van Rossumf6971e21994-08-30 12:25:20 +00003100}
3101
Victor Stinner71e44cb2011-09-06 01:53:03 +02003102#ifdef HAVE_NCURSESW
3103/* Convert an object to a character (wchar_t):
3104
3105 - int
3106 - str of length 1
3107
3108 Return 1 on success, 0 on error. */
3109static int
3110PyCurses_ConvertToWchar_t(PyObject *obj,
3111 wchar_t *wch)
3112{
3113 if (PyUnicode_Check(obj)) {
3114 wchar_t buffer[2];
3115 if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) {
3116 PyErr_Format(PyExc_TypeError,
3117 "expect bytes or str of length 1, or int, "
3118 "got a str of length %zi",
Victor Stinnerc4f281e2011-10-11 22:11:42 +02003119 PyUnicode_GET_LENGTH(obj));
Victor Stinner71e44cb2011-09-06 01:53:03 +02003120 return 0;
3121 }
3122 *wch = buffer[0];
3123 return 2;
3124 }
3125 else if (PyLong_CheckExact(obj)) {
3126 long value;
3127 int overflow;
3128 value = PyLong_AsLongAndOverflow(obj, &overflow);
3129 if (overflow) {
3130 PyErr_SetString(PyExc_OverflowError,
3131 "int doesn't fit in long");
3132 return 0;
3133 }
3134 *wch = (wchar_t)value;
3135 if ((long)*wch != value) {
3136 PyErr_Format(PyExc_OverflowError,
3137 "character doesn't fit in wchar_t");
3138 return 0;
3139 }
3140 return 1;
3141 }
3142 else {
3143 PyErr_Format(PyExc_TypeError,
3144 "expect bytes or str of length 1, or int, got %s",
3145 Py_TYPE(obj)->tp_name);
3146 return 0;
3147 }
3148}
3149
3150static PyObject *
3151PyCurses_Unget_Wch(PyObject *self, PyObject *args)
3152{
3153 PyObject *obj;
3154 wchar_t wch;
3155
3156 PyCursesInitialised;
3157
3158 if (!PyArg_ParseTuple(args,"O", &obj))
3159 return NULL;
3160
3161 if (!PyCurses_ConvertToWchar_t(obj, &wch))
3162 return NULL;
3163 return PyCursesCheckERR(unget_wch(wch), "unget_wch");
3164}
3165#endif
3166
Guido van Rossumf6971e21994-08-30 12:25:20 +00003167static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00003168PyCurses_Use_Env(PyObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00003169{
Victor Stinner26486ea2010-05-15 22:23:53 +00003170 int flag;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003171
Victor Stinner26486ea2010-05-15 22:23:53 +00003172 switch(PyTuple_Size(args)) {
3173 case 1:
3174 if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&flag))
3175 return NULL;
3176 break;
3177 default:
3178 PyErr_SetString(PyExc_TypeError, "use_env requires 1 argument");
3179 return NULL;
3180 }
3181 use_env(flag);
3182 Py_INCREF(Py_None);
3183 return Py_None;
Guido van Rossumf6971e21994-08-30 12:25:20 +00003184}
3185
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00003186#ifndef STRICT_SYSV_CURSES
3187static PyObject *
3188PyCurses_Use_Default_Colors(PyObject *self)
3189{
Victor Stinner26486ea2010-05-15 22:23:53 +00003190 int code;
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00003191
Victor Stinner26486ea2010-05-15 22:23:53 +00003192 PyCursesInitialised;
3193 PyCursesInitialisedColor;
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00003194
Victor Stinner26486ea2010-05-15 22:23:53 +00003195 code = use_default_colors();
3196 if (code != ERR) {
3197 Py_INCREF(Py_None);
3198 return Py_None;
3199 } else {
3200 PyErr_SetString(PyCursesError, "use_default_colors() returned ERR");
3201 return NULL;
3202 }
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00003203}
3204#endif /* STRICT_SYSV_CURSES */
3205
Guido van Rossumf6971e21994-08-30 12:25:20 +00003206/* List of functions defined in the module */
3207
3208static PyMethodDef PyCurses_methods[] = {
Victor Stinner26486ea2010-05-15 22:23:53 +00003209 {"baudrate", (PyCFunction)PyCurses_baudrate, METH_NOARGS},
3210 {"beep", (PyCFunction)PyCurses_beep, METH_NOARGS},
3211 {"can_change_color", (PyCFunction)PyCurses_can_change_color, METH_NOARGS},
3212 {"cbreak", (PyCFunction)PyCurses_cbreak, METH_VARARGS},
3213 {"color_content", (PyCFunction)PyCurses_Color_Content, METH_VARARGS},
3214 {"color_pair", (PyCFunction)PyCurses_color_pair, METH_VARARGS},
3215 {"curs_set", (PyCFunction)PyCurses_Curs_Set, METH_VARARGS},
3216 {"def_prog_mode", (PyCFunction)PyCurses_def_prog_mode, METH_NOARGS},
3217 {"def_shell_mode", (PyCFunction)PyCurses_def_shell_mode, METH_NOARGS},
3218 {"delay_output", (PyCFunction)PyCurses_Delay_Output, METH_VARARGS},
3219 {"doupdate", (PyCFunction)PyCurses_doupdate, METH_NOARGS},
3220 {"echo", (PyCFunction)PyCurses_echo, METH_VARARGS},
3221 {"endwin", (PyCFunction)PyCurses_endwin, METH_NOARGS},
3222 {"erasechar", (PyCFunction)PyCurses_EraseChar, METH_NOARGS},
3223 {"filter", (PyCFunction)PyCurses_filter, METH_NOARGS},
3224 {"flash", (PyCFunction)PyCurses_flash, METH_NOARGS},
3225 {"flushinp", (PyCFunction)PyCurses_flushinp, METH_NOARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003226#ifdef NCURSES_MOUSE_VERSION
Victor Stinner26486ea2010-05-15 22:23:53 +00003227 {"getmouse", (PyCFunction)PyCurses_GetMouse, METH_NOARGS},
3228 {"ungetmouse", (PyCFunction)PyCurses_UngetMouse, METH_VARARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003229#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003230 {"getsyx", (PyCFunction)PyCurses_getsyx, METH_NOARGS},
3231 {"getwin", (PyCFunction)PyCurses_GetWin, METH_O},
3232 {"has_colors", (PyCFunction)PyCurses_has_colors, METH_NOARGS},
3233 {"has_ic", (PyCFunction)PyCurses_has_ic, METH_NOARGS},
3234 {"has_il", (PyCFunction)PyCurses_has_il, METH_NOARGS},
Andrew M. Kuchlingbf680262000-07-25 12:37:41 +00003235#ifndef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00003236 {"has_key", (PyCFunction)PyCurses_has_key, METH_VARARGS},
Guido van Rossumf6971e21994-08-30 12:25:20 +00003237#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003238 {"halfdelay", (PyCFunction)PyCurses_HalfDelay, METH_VARARGS},
3239 {"init_color", (PyCFunction)PyCurses_Init_Color, METH_VARARGS},
3240 {"init_pair", (PyCFunction)PyCurses_Init_Pair, METH_VARARGS},
3241 {"initscr", (PyCFunction)PyCurses_InitScr, METH_NOARGS},
3242 {"intrflush", (PyCFunction)PyCurses_IntrFlush, METH_VARARGS},
3243 {"isendwin", (PyCFunction)PyCurses_isendwin, METH_NOARGS},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003244#ifdef HAVE_CURSES_IS_TERM_RESIZED
Victor Stinner26486ea2010-05-15 22:23:53 +00003245 {"is_term_resized", (PyCFunction)PyCurses_Is_Term_Resized, METH_VARARGS},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003246#endif
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00003247#if !defined(__NetBSD__)
Victor Stinner26486ea2010-05-15 22:23:53 +00003248 {"keyname", (PyCFunction)PyCurses_KeyName, METH_VARARGS},
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00003249#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003250 {"killchar", (PyCFunction)PyCurses_KillChar, METH_NOARGS},
3251 {"longname", (PyCFunction)PyCurses_longname, METH_NOARGS},
3252 {"meta", (PyCFunction)PyCurses_Meta, METH_VARARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003253#ifdef NCURSES_MOUSE_VERSION
Victor Stinner26486ea2010-05-15 22:23:53 +00003254 {"mouseinterval", (PyCFunction)PyCurses_MouseInterval, METH_VARARGS},
3255 {"mousemask", (PyCFunction)PyCurses_MouseMask, METH_VARARGS},
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003256#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003257 {"napms", (PyCFunction)PyCurses_Napms, METH_VARARGS},
3258 {"newpad", (PyCFunction)PyCurses_NewPad, METH_VARARGS},
3259 {"newwin", (PyCFunction)PyCurses_NewWindow, METH_VARARGS},
3260 {"nl", (PyCFunction)PyCurses_nl, METH_VARARGS},
3261 {"nocbreak", (PyCFunction)PyCurses_nocbreak, METH_NOARGS},
3262 {"noecho", (PyCFunction)PyCurses_noecho, METH_NOARGS},
3263 {"nonl", (PyCFunction)PyCurses_nonl, METH_NOARGS},
3264 {"noqiflush", (PyCFunction)PyCurses_noqiflush, METH_NOARGS},
3265 {"noraw", (PyCFunction)PyCurses_noraw, METH_NOARGS},
3266 {"pair_content", (PyCFunction)PyCurses_Pair_Content, METH_VARARGS},
3267 {"pair_number", (PyCFunction)PyCurses_pair_number, METH_VARARGS},
3268 {"putp", (PyCFunction)PyCurses_Putp, METH_VARARGS},
3269 {"qiflush", (PyCFunction)PyCurses_QiFlush, METH_VARARGS},
3270 {"raw", (PyCFunction)PyCurses_raw, METH_VARARGS},
3271 {"reset_prog_mode", (PyCFunction)PyCurses_reset_prog_mode, METH_NOARGS},
3272 {"reset_shell_mode", (PyCFunction)PyCurses_reset_shell_mode, METH_NOARGS},
3273 {"resetty", (PyCFunction)PyCurses_resetty, METH_NOARGS},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003274#ifdef HAVE_CURSES_RESIZETERM
Victor Stinner26486ea2010-05-15 22:23:53 +00003275 {"resizeterm", (PyCFunction)PyCurses_ResizeTerm, METH_VARARGS},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003276#endif
3277#ifdef HAVE_CURSES_RESIZE_TERM
Victor Stinner26486ea2010-05-15 22:23:53 +00003278 {"resize_term", (PyCFunction)PyCurses_Resize_Term, METH_VARARGS},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003279#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003280 {"savetty", (PyCFunction)PyCurses_savetty, METH_NOARGS},
3281 {"setsyx", (PyCFunction)PyCurses_setsyx, METH_VARARGS},
3282 {"setupterm", (PyCFunction)PyCurses_setupterm,
3283 METH_VARARGS|METH_KEYWORDS},
3284 {"start_color", (PyCFunction)PyCurses_Start_Color, METH_NOARGS},
3285 {"termattrs", (PyCFunction)PyCurses_termattrs, METH_NOARGS},
3286 {"termname", (PyCFunction)PyCurses_termname, METH_NOARGS},
3287 {"tigetflag", (PyCFunction)PyCurses_tigetflag, METH_VARARGS},
3288 {"tigetnum", (PyCFunction)PyCurses_tigetnum, METH_VARARGS},
3289 {"tigetstr", (PyCFunction)PyCurses_tigetstr, METH_VARARGS},
3290 {"tparm", (PyCFunction)PyCurses_tparm, METH_VARARGS},
3291 {"typeahead", (PyCFunction)PyCurses_TypeAhead, METH_VARARGS},
3292 {"unctrl", (PyCFunction)PyCurses_UnCtrl, METH_VARARGS},
3293 {"ungetch", (PyCFunction)PyCurses_UngetCh, METH_VARARGS},
Steve Dowerd2bc3892015-04-15 18:06:05 -04003294#if defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM)
3295 {"update_lines_cols", (PyCFunction)PyCurses_update_lines_cols, METH_NOARGS},
3296#endif
Victor Stinner71e44cb2011-09-06 01:53:03 +02003297#ifdef HAVE_NCURSESW
3298 {"unget_wch", (PyCFunction)PyCurses_Unget_Wch, METH_VARARGS},
3299#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003300 {"use_env", (PyCFunction)PyCurses_Use_Env, METH_VARARGS},
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00003301#ifndef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00003302 {"use_default_colors", (PyCFunction)PyCurses_Use_Default_Colors, METH_NOARGS},
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00003303#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003304 {NULL, NULL} /* sentinel */
Guido van Rossumf6971e21994-08-30 12:25:20 +00003305};
3306
3307/* Initialization function for the module */
3308
Martin v. Löwis1a214512008-06-11 05:26:20 +00003309
3310static struct PyModuleDef _cursesmodule = {
Victor Stinner26486ea2010-05-15 22:23:53 +00003311 PyModuleDef_HEAD_INIT,
3312 "_curses",
3313 NULL,
3314 -1,
3315 PyCurses_methods,
3316 NULL,
3317 NULL,
3318 NULL,
3319 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00003320};
3321
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003322PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003323PyInit__curses(void)
Guido van Rossumf6971e21994-08-30 12:25:20 +00003324{
Victor Stinner26486ea2010-05-15 22:23:53 +00003325 PyObject *m, *d, *v, *c_api_object;
3326 static void *PyCurses_API[PyCurses_API_pointers];
Andrew M. Kuchling32552682000-12-22 21:52:27 +00003327
Victor Stinner26486ea2010-05-15 22:23:53 +00003328 /* Initialize object type */
3329 if (PyType_Ready(&PyCursesWindow_Type) < 0)
3330 return NULL;
Guido van Rossum6915c4d2001-01-19 00:28:08 +00003331
Victor Stinner26486ea2010-05-15 22:23:53 +00003332 /* Initialize the C API pointer array */
3333 PyCurses_API[0] = (void *)&PyCursesWindow_Type;
3334 PyCurses_API[1] = (void *)func_PyCursesSetupTermCalled;
3335 PyCurses_API[2] = (void *)func_PyCursesInitialised;
3336 PyCurses_API[3] = (void *)func_PyCursesInitialisedColor;
Guido van Rossumf6971e21994-08-30 12:25:20 +00003337
Victor Stinner26486ea2010-05-15 22:23:53 +00003338 /* Create the module and add the functions */
3339 m = PyModule_Create(&_cursesmodule);
3340 if (m == NULL)
3341 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +00003342
Victor Stinner26486ea2010-05-15 22:23:53 +00003343 /* Add some symbolic constants to the module */
3344 d = PyModule_GetDict(m);
3345 if (d == NULL)
3346 return NULL;
3347 ModDict = d; /* For PyCurses_InitScr to use later */
Andrew M. Kuchling32552682000-12-22 21:52:27 +00003348
Victor Stinner26486ea2010-05-15 22:23:53 +00003349 /* Add a capsule for the C API */
3350 c_api_object = PyCapsule_New(PyCurses_API, PyCurses_CAPSULE_NAME, NULL);
3351 PyDict_SetItemString(d, "_C_API", c_api_object);
3352 Py_DECREF(c_api_object);
Guido van Rossumfbea2f31994-08-31 22:05:27 +00003353
Victor Stinner26486ea2010-05-15 22:23:53 +00003354 /* For exception curses.error */
3355 PyCursesError = PyErr_NewException("_curses.error", NULL, NULL);
3356 PyDict_SetItemString(d, "error", PyCursesError);
Guido van Rossumfbea2f31994-08-31 22:05:27 +00003357
Victor Stinner26486ea2010-05-15 22:23:53 +00003358 /* Make the version available */
3359 v = PyBytes_FromString(PyCursesVersion);
3360 PyDict_SetItemString(d, "version", v);
3361 PyDict_SetItemString(d, "__version__", v);
3362 Py_DECREF(v);
Guido van Rossumf6971e21994-08-30 12:25:20 +00003363
Victor Stinner26486ea2010-05-15 22:23:53 +00003364 SetDictInt("ERR", ERR);
3365 SetDictInt("OK", OK);
Andrew M. Kuchlingcaefb372000-12-26 15:57:01 +00003366
Victor Stinner26486ea2010-05-15 22:23:53 +00003367 /* Here are some attributes you can add to chars to print */
3368
3369 SetDictInt("A_ATTRIBUTES", A_ATTRIBUTES);
3370 SetDictInt("A_NORMAL", A_NORMAL);
3371 SetDictInt("A_STANDOUT", A_STANDOUT);
3372 SetDictInt("A_UNDERLINE", A_UNDERLINE);
3373 SetDictInt("A_REVERSE", A_REVERSE);
3374 SetDictInt("A_BLINK", A_BLINK);
3375 SetDictInt("A_DIM", A_DIM);
3376 SetDictInt("A_BOLD", A_BOLD);
3377 SetDictInt("A_ALTCHARSET", A_ALTCHARSET);
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00003378#if !defined(__NetBSD__)
Victor Stinner26486ea2010-05-15 22:23:53 +00003379 SetDictInt("A_INVIS", A_INVIS);
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00003380#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003381 SetDictInt("A_PROTECT", A_PROTECT);
3382 SetDictInt("A_CHARTEXT", A_CHARTEXT);
3383 SetDictInt("A_COLOR", A_COLOR);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003384
Victor Stinner26486ea2010-05-15 22:23:53 +00003385 /* The following are never available with strict SYSV curses */
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003386#ifdef A_HORIZONTAL
Victor Stinner26486ea2010-05-15 22:23:53 +00003387 SetDictInt("A_HORIZONTAL", A_HORIZONTAL);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003388#endif
3389#ifdef A_LEFT
Victor Stinner26486ea2010-05-15 22:23:53 +00003390 SetDictInt("A_LEFT", A_LEFT);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003391#endif
3392#ifdef A_LOW
Victor Stinner26486ea2010-05-15 22:23:53 +00003393 SetDictInt("A_LOW", A_LOW);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003394#endif
3395#ifdef A_RIGHT
Victor Stinner26486ea2010-05-15 22:23:53 +00003396 SetDictInt("A_RIGHT", A_RIGHT);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003397#endif
3398#ifdef A_TOP
Victor Stinner26486ea2010-05-15 22:23:53 +00003399 SetDictInt("A_TOP", A_TOP);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003400#endif
3401#ifdef A_VERTICAL
Victor Stinner26486ea2010-05-15 22:23:53 +00003402 SetDictInt("A_VERTICAL", A_VERTICAL);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003403#endif
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00003404
Victor Stinner26486ea2010-05-15 22:23:53 +00003405 SetDictInt("COLOR_BLACK", COLOR_BLACK);
3406 SetDictInt("COLOR_RED", COLOR_RED);
3407 SetDictInt("COLOR_GREEN", COLOR_GREEN);
3408 SetDictInt("COLOR_YELLOW", COLOR_YELLOW);
3409 SetDictInt("COLOR_BLUE", COLOR_BLUE);
3410 SetDictInt("COLOR_MAGENTA", COLOR_MAGENTA);
3411 SetDictInt("COLOR_CYAN", COLOR_CYAN);
3412 SetDictInt("COLOR_WHITE", COLOR_WHITE);
Guido van Rossumf6971e21994-08-30 12:25:20 +00003413
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003414#ifdef NCURSES_MOUSE_VERSION
Victor Stinner26486ea2010-05-15 22:23:53 +00003415 /* Mouse-related constants */
3416 SetDictInt("BUTTON1_PRESSED", BUTTON1_PRESSED);
3417 SetDictInt("BUTTON1_RELEASED", BUTTON1_RELEASED);
3418 SetDictInt("BUTTON1_CLICKED", BUTTON1_CLICKED);
3419 SetDictInt("BUTTON1_DOUBLE_CLICKED", BUTTON1_DOUBLE_CLICKED);
3420 SetDictInt("BUTTON1_TRIPLE_CLICKED", BUTTON1_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003421
Victor Stinner26486ea2010-05-15 22:23:53 +00003422 SetDictInt("BUTTON2_PRESSED", BUTTON2_PRESSED);
3423 SetDictInt("BUTTON2_RELEASED", BUTTON2_RELEASED);
3424 SetDictInt("BUTTON2_CLICKED", BUTTON2_CLICKED);
3425 SetDictInt("BUTTON2_DOUBLE_CLICKED", BUTTON2_DOUBLE_CLICKED);
3426 SetDictInt("BUTTON2_TRIPLE_CLICKED", BUTTON2_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003427
Victor Stinner26486ea2010-05-15 22:23:53 +00003428 SetDictInt("BUTTON3_PRESSED", BUTTON3_PRESSED);
3429 SetDictInt("BUTTON3_RELEASED", BUTTON3_RELEASED);
3430 SetDictInt("BUTTON3_CLICKED", BUTTON3_CLICKED);
3431 SetDictInt("BUTTON3_DOUBLE_CLICKED", BUTTON3_DOUBLE_CLICKED);
3432 SetDictInt("BUTTON3_TRIPLE_CLICKED", BUTTON3_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003433
Victor Stinner26486ea2010-05-15 22:23:53 +00003434 SetDictInt("BUTTON4_PRESSED", BUTTON4_PRESSED);
3435 SetDictInt("BUTTON4_RELEASED", BUTTON4_RELEASED);
3436 SetDictInt("BUTTON4_CLICKED", BUTTON4_CLICKED);
3437 SetDictInt("BUTTON4_DOUBLE_CLICKED", BUTTON4_DOUBLE_CLICKED);
3438 SetDictInt("BUTTON4_TRIPLE_CLICKED", BUTTON4_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003439
Victor Stinner26486ea2010-05-15 22:23:53 +00003440 SetDictInt("BUTTON_SHIFT", BUTTON_SHIFT);
3441 SetDictInt("BUTTON_CTRL", BUTTON_CTRL);
3442 SetDictInt("BUTTON_ALT", BUTTON_ALT);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003443
Victor Stinner26486ea2010-05-15 22:23:53 +00003444 SetDictInt("ALL_MOUSE_EVENTS", ALL_MOUSE_EVENTS);
3445 SetDictInt("REPORT_MOUSE_POSITION", REPORT_MOUSE_POSITION);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003446#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003447 /* Now set everything up for KEY_ variables */
3448 {
3449 int key;
3450 char *key_n;
3451 char *key_n2;
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00003452#if !defined(__NetBSD__)
Victor Stinner26486ea2010-05-15 22:23:53 +00003453 for (key=KEY_MIN;key < KEY_MAX; key++) {
3454 key_n = (char *)keyname(key);
3455 if (key_n == NULL || strcmp(key_n,"UNKNOWN KEY")==0)
3456 continue;
3457 if (strncmp(key_n,"KEY_F(",6)==0) {
3458 char *p1, *p2;
Victor Stinnerb6404912013-07-07 16:21:41 +02003459 key_n2 = PyMem_Malloc(strlen(key_n)+1);
Victor Stinner26486ea2010-05-15 22:23:53 +00003460 if (!key_n2) {
3461 PyErr_NoMemory();
3462 break;
3463 }
3464 p1 = key_n;
3465 p2 = key_n2;
3466 while (*p1) {
3467 if (*p1 != '(' && *p1 != ')') {
3468 *p2 = *p1;
3469 p2++;
3470 }
3471 p1++;
3472 }
3473 *p2 = (char)0;
3474 } else
3475 key_n2 = key_n;
3476 SetDictInt(key_n2,key);
3477 if (key_n2 != key_n)
Victor Stinnerb6404912013-07-07 16:21:41 +02003478 PyMem_Free(key_n2);
Victor Stinner26486ea2010-05-15 22:23:53 +00003479 }
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00003480#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003481 SetDictInt("KEY_MIN", KEY_MIN);
3482 SetDictInt("KEY_MAX", KEY_MAX);
3483 }
3484 return m;
Guido van Rossumf6971e21994-08-30 12:25:20 +00003485}