blob: 9a1d2efd256ea5da56d21d24c2d784dd47bc4323 [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
Serhiy Storchakae2f92de2017-11-11 13:06:26 +020099static const char 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
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200115#if defined(HAVE_TERM_H) || defined(__sgi)
116/* For termname, longname, putp, tigetflag, tigetnum, tigetstr, tparm
117 which are not declared in SysV curses and for setupterm. */
Fred Drake0368bc42001-07-19 20:48:32 +0000118#include <term.h>
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200119/* Including <term.h> #defines many common symbols. */
120#undef lines
121#undef columns
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]
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300138module _curses
139class _curses.window "PyCursesWindowObject *" "&PyCursesWindow_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800140[clinic start generated code]*/
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300141/*[clinic end generated code: output=da39a3ee5e6b4b0d input=43265c372c2887d6]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300142
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000143/* Definition of exception curses.error */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000144
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000145static PyObject *PyCursesError;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000146
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +0000147/* Tells whether setupterm() has been called to initialise terminfo. */
148static int initialised_setupterm = FALSE;
149
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000150/* Tells whether initscr() has been called to initialise curses. */
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000151static int initialised = FALSE;
152
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000153/* Tells whether start_color() has been called to initialise color usage. */
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000154static int initialisedcolors = FALSE;
155
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100156static char *screen_encoding = NULL;
157
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000158/* Utility Macros */
Victor Stinner26486ea2010-05-15 22:23:53 +0000159#define PyCursesSetupTermCalled \
160 if (initialised_setupterm != TRUE) { \
161 PyErr_SetString(PyCursesError, \
162 "must call (at least) setupterm() first"); \
163 return 0; }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +0000164
Victor Stinner26486ea2010-05-15 22:23:53 +0000165#define PyCursesInitialised \
166 if (initialised != TRUE) { \
167 PyErr_SetString(PyCursesError, \
168 "must call initscr() first"); \
169 return 0; }
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000170
Victor Stinner26486ea2010-05-15 22:23:53 +0000171#define PyCursesInitialisedColor \
172 if (initialisedcolors != TRUE) { \
173 PyErr_SetString(PyCursesError, \
174 "must call start_color() first"); \
175 return 0; }
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000176
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000177/* Utility Functions */
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000178
Guido van Rossum85738471995-02-17 13:50:17 +0000179/*
Victor Stinner26486ea2010-05-15 22:23:53 +0000180 * Check the return code from a curses function and return None
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000181 * or raise an exception as appropriate. These are exported using the
Victor Stinner26486ea2010-05-15 22:23:53 +0000182 * capsule API.
Guido van Rossum85738471995-02-17 13:50:17 +0000183 */
184
185static PyObject *
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100186PyCursesCheckERR(int code, const char *fname)
Guido van Rossum85738471995-02-17 13:50:17 +0000187{
Victor Stinner26486ea2010-05-15 22:23:53 +0000188 if (code != ERR) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200189 Py_RETURN_NONE;
Guido van Rossum85738471995-02-17 13:50:17 +0000190 } else {
Victor Stinner26486ea2010-05-15 22:23:53 +0000191 if (fname == NULL) {
192 PyErr_SetString(PyCursesError, catchall_ERR);
193 } else {
194 PyErr_Format(PyCursesError, "%s() returned ERR", fname);
195 }
196 return NULL;
Guido van Rossum85738471995-02-17 13:50:17 +0000197 }
Guido van Rossum85738471995-02-17 13:50:17 +0000198}
199
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100200/* Convert an object to a byte (an integer of type chtype):
201
202 - int
203 - bytes of length 1
204 - str of length 1
205
206 Return 1 on success, 0 on error (invalid type or integer overflow). */
Victor Stinner26486ea2010-05-15 22:23:53 +0000207static int
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100208PyCurses_ConvertToChtype(PyCursesWindowObject *win, PyObject *obj, chtype *ch)
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000209{
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100210 long value;
211 if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) {
212 value = (unsigned char)PyBytes_AsString(obj)[0];
213 }
214 else if (PyUnicode_Check(obj)) {
215 if (PyUnicode_GetLength(obj) != 1) {
216 PyErr_Format(PyExc_TypeError,
217 "expect bytes or str of length 1, or int, "
218 "got a str of length %zi",
219 PyUnicode_GET_LENGTH(obj));
Victor Stinner26486ea2010-05-15 22:23:53 +0000220 return 0;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100221 }
222 value = PyUnicode_READ_CHAR(obj, 0);
223 if (128 < value) {
224 PyObject *bytes;
225 const char *encoding;
226 if (win)
227 encoding = win->encoding;
228 else
229 encoding = screen_encoding;
Serhiy Storchakab29cee42016-10-27 19:31:49 +0300230 bytes = PyUnicode_AsEncodedString(obj, encoding, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100231 if (bytes == NULL)
232 return 0;
233 if (PyBytes_GET_SIZE(bytes) == 1)
234 value = (unsigned char)PyBytes_AS_STRING(bytes)[0];
235 else
236 value = -1;
237 Py_DECREF(bytes);
238 if (value < 0)
239 goto overflow;
240 }
241 }
242 else if (PyLong_CheckExact(obj)) {
243 int long_overflow;
244 value = PyLong_AsLongAndOverflow(obj, &long_overflow);
245 if (long_overflow)
246 goto overflow;
247 }
248 else {
249 PyErr_Format(PyExc_TypeError,
250 "expect bytes or str of length 1, or int, got %s",
251 Py_TYPE(obj)->tp_name);
Victor Stinner26486ea2010-05-15 22:23:53 +0000252 return 0;
253 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100254 *ch = (chtype)value;
255 if ((long)*ch != value)
256 goto overflow;
Victor Stinner26486ea2010-05-15 22:23:53 +0000257 return 1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100258
259overflow:
260 PyErr_SetString(PyExc_OverflowError,
261 "byte doesn't fit in chtype");
262 return 0;
263}
264
265/* Convert an object to a byte (chtype) or a character (cchar_t):
266
267 - int
268 - bytes of length 1
269 - str of length 1
270
271 Return:
272
273 - 2 if obj is a character (written into *wch)
274 - 1 if obj is a byte (written into *ch)
275 - 0 on error: raise an exception */
276static int
277PyCurses_ConvertToCchar_t(PyCursesWindowObject *win, PyObject *obj,
278 chtype *ch
279#ifdef HAVE_NCURSESW
Serhiy Storchaka0bcd89b2016-10-30 22:52:06 +0200280 , wchar_t *wch
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100281#endif
282 )
283{
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100284 long value;
285#ifdef HAVE_NCURSESW
286 wchar_t buffer[2];
287#endif
288
289 if (PyUnicode_Check(obj)) {
290#ifdef HAVE_NCURSESW
291 if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) {
292 PyErr_Format(PyExc_TypeError,
293 "expect bytes or str of length 1, or int, "
294 "got a str of length %zi",
295 PyUnicode_GET_LENGTH(obj));
296 return 0;
297 }
Serhiy Storchaka0bcd89b2016-10-30 22:52:06 +0200298 *wch = buffer[0];
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100299 return 2;
300#else
301 return PyCurses_ConvertToChtype(win, obj, ch);
302#endif
303 }
304 else if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) {
305 value = (unsigned char)PyBytes_AsString(obj)[0];
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100306 }
307 else if (PyLong_CheckExact(obj)) {
308 int overflow;
309 value = PyLong_AsLongAndOverflow(obj, &overflow);
310 if (overflow) {
311 PyErr_SetString(PyExc_OverflowError,
312 "int doesn't fit in long");
313 return 0;
314 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100315 }
316 else {
317 PyErr_Format(PyExc_TypeError,
318 "expect bytes or str of length 1, or int, got %s",
319 Py_TYPE(obj)->tp_name);
320 return 0;
321 }
Victor Stinner9f16e442012-09-01 15:00:34 +0200322
323 *ch = (chtype)value;
324 if ((long)*ch != value) {
325 PyErr_Format(PyExc_OverflowError,
326 "byte doesn't fit in chtype");
327 return 0;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100328 }
Victor Stinner9f16e442012-09-01 15:00:34 +0200329 return 1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100330}
331
332/* Convert an object to a byte string (char*) or a wide character string
333 (wchar_t*). Return:
334
335 - 2 if obj is a character string (written into *wch)
336 - 1 if obj is a byte string (written into *bytes)
337 - 0 on error: raise an exception */
338static int
339PyCurses_ConvertToString(PyCursesWindowObject *win, PyObject *obj,
340 PyObject **bytes, wchar_t **wstr)
341{
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300342 char *str;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100343 if (PyUnicode_Check(obj)) {
344#ifdef HAVE_NCURSESW
345 assert (wstr != NULL);
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300346
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100347 *wstr = PyUnicode_AsWideCharString(obj, NULL);
348 if (*wstr == NULL)
349 return 0;
350 return 2;
351#else
352 assert (wstr == NULL);
Serhiy Storchakab29cee42016-10-27 19:31:49 +0300353 *bytes = PyUnicode_AsEncodedString(obj, win->encoding, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100354 if (*bytes == NULL)
355 return 0;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300356 /* check for embedded null bytes */
357 if (PyBytes_AsStringAndSize(*bytes, &str, NULL) < 0) {
358 return 0;
359 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100360 return 1;
361#endif
362 }
363 else if (PyBytes_Check(obj)) {
364 Py_INCREF(obj);
365 *bytes = obj;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300366 /* check for embedded null bytes */
367 if (PyBytes_AsStringAndSize(*bytes, &str, NULL) < 0) {
368 return 0;
369 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100370 return 1;
371 }
372
373 PyErr_Format(PyExc_TypeError, "expect bytes or str, got %s",
374 Py_TYPE(obj)->tp_name);
375 return 0;
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000376}
377
Benjamin Peterson21896a32010-03-21 22:03:03 +0000378/* Function versions of the 3 functions for testing whether curses has been
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000379 initialised or not. */
Victor Stinner26486ea2010-05-15 22:23:53 +0000380
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000381static int func_PyCursesSetupTermCalled(void)
382{
383 PyCursesSetupTermCalled;
384 return 1;
385}
386
387static int func_PyCursesInitialised(void)
388{
389 PyCursesInitialised;
390 return 1;
391}
392
393static int func_PyCursesInitialisedColor(void)
394{
395 PyCursesInitialisedColor;
396 return 1;
397}
398
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000399/*****************************************************************************
400 The Window Object
401******************************************************************************/
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000402
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000403/* Definition of the window type */
Guido van Rossum85738471995-02-17 13:50:17 +0000404
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000405PyTypeObject PyCursesWindow_Type;
406
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000407/* Function prototype macros for Window object
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000408
409 X - function name
410 TYPE - parameter Type
411 ERGSTR - format string for construction of the return value
412 PARSESTR - format string for argument parsing
Victor Stinner26486ea2010-05-15 22:23:53 +0000413*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000414
Serhiy Storchaka81524022018-11-27 13:05:02 +0200415#define Window_NoArgNoReturnFunction(X) \
416 static PyObject *PyCursesWindow_ ## X \
417 (PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \
Victor Stinner26486ea2010-05-15 22:23:53 +0000418 { return PyCursesCheckERR(X(self->win), # X); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000419
Victor Stinner26486ea2010-05-15 22:23:53 +0000420#define Window_NoArgTrueFalseFunction(X) \
421 static PyObject * PyCursesWindow_ ## X \
Serhiy Storchaka81524022018-11-27 13:05:02 +0200422 (PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \
Victor Stinner26486ea2010-05-15 22:23:53 +0000423 { \
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300424 return PyBool_FromLong(X(self->win)); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000425
Victor Stinner26486ea2010-05-15 22:23:53 +0000426#define Window_NoArgNoReturnVoidFunction(X) \
427 static PyObject * PyCursesWindow_ ## X \
Serhiy Storchaka81524022018-11-27 13:05:02 +0200428 (PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \
Victor Stinner26486ea2010-05-15 22:23:53 +0000429 { \
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200430 X(self->win); Py_RETURN_NONE; }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000431
Victor Stinner26486ea2010-05-15 22:23:53 +0000432#define Window_NoArg2TupleReturnFunction(X, TYPE, ERGSTR) \
433 static PyObject * PyCursesWindow_ ## X \
Serhiy Storchaka81524022018-11-27 13:05:02 +0200434 (PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \
Victor Stinner26486ea2010-05-15 22:23:53 +0000435 { \
436 TYPE arg1, arg2; \
437 X(self->win,arg1,arg2); return Py_BuildValue(ERGSTR, arg1, arg2); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000438
Victor Stinner26486ea2010-05-15 22:23:53 +0000439#define Window_OneArgNoReturnVoidFunction(X, TYPE, PARSESTR) \
440 static PyObject * PyCursesWindow_ ## X \
441 (PyCursesWindowObject *self, PyObject *args) \
442 { \
443 TYPE arg1; \
444 if (!PyArg_ParseTuple(args, PARSESTR, &arg1)) return NULL; \
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200445 X(self->win,arg1); Py_RETURN_NONE; }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000446
Victor Stinner26486ea2010-05-15 22:23:53 +0000447#define Window_OneArgNoReturnFunction(X, TYPE, PARSESTR) \
448 static PyObject * PyCursesWindow_ ## X \
449 (PyCursesWindowObject *self, PyObject *args) \
450 { \
451 TYPE arg1; \
452 if (!PyArg_ParseTuple(args,PARSESTR, &arg1)) return NULL; \
453 return PyCursesCheckERR(X(self->win, arg1), # X); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000454
Victor Stinner26486ea2010-05-15 22:23:53 +0000455#define Window_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \
456 static PyObject * PyCursesWindow_ ## X \
457 (PyCursesWindowObject *self, PyObject *args) \
458 { \
459 TYPE arg1, arg2; \
460 if (!PyArg_ParseTuple(args,PARSESTR, &arg1, &arg2)) return NULL; \
461 return PyCursesCheckERR(X(self->win, arg1, arg2), # X); }
Guido van Rossum85738471995-02-17 13:50:17 +0000462
Guido van Rossumf6971e21994-08-30 12:25:20 +0000463/* ------------- WINDOW routines --------------- */
Guido van Rossum85738471995-02-17 13:50:17 +0000464
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000465Window_NoArgNoReturnFunction(untouchwin)
466Window_NoArgNoReturnFunction(touchwin)
467Window_NoArgNoReturnFunction(redrawwin)
468Window_NoArgNoReturnFunction(winsertln)
469Window_NoArgNoReturnFunction(werase)
470Window_NoArgNoReturnFunction(wdeleteln)
471
472Window_NoArgTrueFalseFunction(is_wintouched)
473
474Window_NoArgNoReturnVoidFunction(wsyncup)
475Window_NoArgNoReturnVoidFunction(wsyncdown)
476Window_NoArgNoReturnVoidFunction(wstandend)
477Window_NoArgNoReturnVoidFunction(wstandout)
478Window_NoArgNoReturnVoidFunction(wcursyncup)
479Window_NoArgNoReturnVoidFunction(wclrtoeol)
480Window_NoArgNoReturnVoidFunction(wclrtobot)
481Window_NoArgNoReturnVoidFunction(wclear)
482
483Window_OneArgNoReturnVoidFunction(idcok, int, "i;True(1) or False(0)")
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200484#ifdef HAVE_CURSES_IMMEDOK
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000485Window_OneArgNoReturnVoidFunction(immedok, int, "i;True(1) or False(0)")
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200486#endif
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +0000487Window_OneArgNoReturnVoidFunction(wtimeout, int, "i;delay")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000488
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000489Window_NoArg2TupleReturnFunction(getyx, int, "ii")
490Window_NoArg2TupleReturnFunction(getbegyx, int, "ii")
491Window_NoArg2TupleReturnFunction(getmaxyx, int, "ii")
492Window_NoArg2TupleReturnFunction(getparyx, int, "ii")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000493
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000494Window_OneArgNoReturnFunction(clearok, int, "i;True(1) or False(0)")
495Window_OneArgNoReturnFunction(idlok, int, "i;True(1) or False(0)")
496Window_OneArgNoReturnFunction(keypad, int, "i;True(1) or False(0)")
497Window_OneArgNoReturnFunction(leaveok, int, "i;True(1) or False(0)")
498Window_OneArgNoReturnFunction(nodelay, int, "i;True(1) or False(0)")
499Window_OneArgNoReturnFunction(notimeout, int, "i;True(1) or False(0)")
500Window_OneArgNoReturnFunction(scrollok, int, "i;True(1) or False(0)")
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +0000501Window_OneArgNoReturnFunction(winsdelln, int, "i;nlines")
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200502#ifdef HAVE_CURSES_SYNCOK
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000503Window_OneArgNoReturnFunction(syncok, int, "i;True(1) or False(0)")
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200504#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000505
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000506Window_TwoArgNoReturnFunction(mvwin, int, "ii;y,x")
507Window_TwoArgNoReturnFunction(mvderwin, int, "ii;y,x")
508Window_TwoArgNoReturnFunction(wmove, int, "ii;y,x")
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +0000509#ifndef STRICT_SYSV_CURSES
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000510Window_TwoArgNoReturnFunction(wresize, int, "ii;lines,columns")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000511#endif
512
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +0000513/* Allocation and deallocation of Window Objects */
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000514
Guido van Rossumf6971e21994-08-30 12:25:20 +0000515static PyObject *
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100516PyCursesWindow_New(WINDOW *win, const char *encoding)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000517{
Victor Stinner26486ea2010-05-15 22:23:53 +0000518 PyCursesWindowObject *wo;
Guido van Rossum85738471995-02-17 13:50:17 +0000519
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100520 if (encoding == NULL) {
521#if defined(MS_WINDOWS)
522 char *buffer[100];
523 UINT cp;
524 cp = GetConsoleOutputCP();
525 if (cp != 0) {
526 PyOS_snprintf(buffer, sizeof(buffer), "cp%u", cp);
527 encoding = buffer;
528 }
529#elif defined(CODESET)
530 const char *codeset = nl_langinfo(CODESET);
531 if (codeset != NULL && codeset[0] != 0)
532 encoding = codeset;
533#endif
534 if (encoding == NULL)
535 encoding = "utf-8";
536 }
537
Victor Stinner26486ea2010-05-15 22:23:53 +0000538 wo = PyObject_NEW(PyCursesWindowObject, &PyCursesWindow_Type);
539 if (wo == NULL) return NULL;
540 wo->win = win;
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200541 wo->encoding = _PyMem_Strdup(encoding);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100542 if (wo->encoding == NULL) {
543 Py_DECREF(wo);
544 PyErr_NoMemory();
545 return NULL;
546 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000547 return (PyObject *)wo;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000548}
549
550static void
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000551PyCursesWindow_Dealloc(PyCursesWindowObject *wo)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000552{
Victor Stinner26486ea2010-05-15 22:23:53 +0000553 if (wo->win != stdscr) delwin(wo->win);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100554 if (wo->encoding != NULL)
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200555 PyMem_Free(wo->encoding);
Victor Stinner26486ea2010-05-15 22:23:53 +0000556 PyObject_DEL(wo);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000557}
558
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000559/* Addch, Addstr, Addnstr */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000560
Larry Hastings61272b72014-01-07 12:41:53 -0800561/*[clinic input]
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300562_curses.window.addch
Larry Hastings31826802013-10-19 00:09:25 -0700563
564 [
Larry Hastings31826802013-10-19 00:09:25 -0700565 y: int
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300566 Y-coordinate.
Larry Hastings9147a962014-05-04 04:41:18 -0700567 x: int
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300568 X-coordinate.
Larry Hastings31826802013-10-19 00:09:25 -0700569 ]
570
571 ch: object
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300572 Character to add.
Larry Hastings31826802013-10-19 00:09:25 -0700573
574 [
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300575 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
576 Attributes for the character.
Larry Hastings31826802013-10-19 00:09:25 -0700577 ]
578 /
579
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300580Paint the character.
Larry Hastings31826802013-10-19 00:09:25 -0700581
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 *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300589_curses_window_addch_impl(PyCursesWindowObject *self, int group_left_1,
590 int y, int x, PyObject *ch, int group_right_1,
591 long attr)
592/*[clinic end generated code: output=00f4c37af3378f45 input=95ce131578458196]*/
Larry Hastings31826802013-10-19 00:09:25 -0700593{
Larry Hastings31826802013-10-19 00:09:25 -0700594 int coordinates_group = group_left_1;
Larry Hastings31826802013-10-19 00:09:25 -0700595 int rtn;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100596 int type;
Victor Stinnerb110dad2016-12-09 17:06:43 +0100597 chtype cch = 0;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100598#ifdef HAVE_NCURSESW
Serhiy Storchaka0bcd89b2016-10-30 22:52:06 +0200599 wchar_t wstr[2];
600 cchar_t wcval;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100601#endif
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100602 const char *funcname;
Guido van Rossum85738471995-02-17 13:50:17 +0000603
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100604#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300605 type = PyCurses_ConvertToCchar_t(self, ch, &cch, wstr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100606 if (type == 2) {
607 funcname = "add_wch";
Serhiy Storchaka0bcd89b2016-10-30 22:52:06 +0200608 wstr[1] = L'\0';
609 setcchar(&wcval, wstr, attr, 0, NULL);
Larry Hastings31826802013-10-19 00:09:25 -0700610 if (coordinates_group)
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300611 rtn = mvwadd_wch(self->win,y,x, &wcval);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100612 else {
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300613 rtn = wadd_wch(self->win, &wcval);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100614 }
615 }
616 else
617#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300618 type = PyCurses_ConvertToCchar_t(self, ch, &cch);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100619#endif
620 if (type == 1) {
621 funcname = "addch";
Larry Hastings31826802013-10-19 00:09:25 -0700622 if (coordinates_group)
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300623 rtn = mvwaddch(self->win,y,x, cch | (attr_t) attr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100624 else {
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300625 rtn = waddch(self->win, cch | (attr_t) attr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100626 }
627 }
628 else {
Victor Stinner26486ea2010-05-15 22:23:53 +0000629 return NULL;
630 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100631 return PyCursesCheckERR(rtn, funcname);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000632}
633
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300634/*[clinic input]
635_curses.window.addstr
636
637 [
638 y: int
639 Y-coordinate.
640 x: int
641 X-coordinate.
642 ]
643
644 str: object
645 String to add.
646
647 [
648 attr: long
649 Attributes for characters.
650 ]
651 /
652
653Paint the string.
654
655Paint the string str at (y, x) with attributes attr,
656overwriting anything previously on the display.
657By default, the character position and attributes are the
658current settings for the window object.
659[clinic start generated code]*/
660
Guido van Rossumf6971e21994-08-30 12:25:20 +0000661static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300662_curses_window_addstr_impl(PyCursesWindowObject *self, int group_left_1,
663 int y, int x, PyObject *str, int group_right_1,
664 long attr)
665/*[clinic end generated code: output=65a928ea85ff3115 input=ff6cbb91448a22a3]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +0000666{
Victor Stinner26486ea2010-05-15 22:23:53 +0000667 int rtn;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100668 int strtype;
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300669 PyObject *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100670#ifdef HAVE_NCURSESW
671 wchar_t *wstr = NULL;
672#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300673 attr_t attr_old = A_NORMAL;
674 int use_xy = group_left_1, use_attr = group_right_1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100675 const char *funcname;
Guido van Rossum85738471995-02-17 13:50:17 +0000676
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100677#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300678 strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100679#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300680 strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100681#endif
682 if (strtype == 0)
683 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300684 if (use_attr) {
Victor Stinner26486ea2010-05-15 22:23:53 +0000685 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";
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300691 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100692 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";
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300702 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100703 rtn = mvwaddstr(self->win,y,x,str);
704 else
705 rtn = waddstr(self->win,str);
706 Py_DECREF(bytesobj);
707 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300708 if (use_attr)
Victor Stinner26486ea2010-05-15 22:23:53 +0000709 (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
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300713/*[clinic input]
714_curses.window.addnstr
715
716 [
717 y: int
718 Y-coordinate.
719 x: int
720 X-coordinate.
721 ]
722
723 str: object
724 String to add.
725
726 n: int
727 Maximal number of characters.
728
729 [
730 attr: long
731 Attributes for characters.
732 ]
733 /
734
735Paint at most n characters of the string.
736
737Paint at most n characters of the string str at (y, x) with
738attributes attr, overwriting anything previously on the display.
739By default, the character position and attributes are the
740current settings for the window object.
741[clinic start generated code]*/
742
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000743static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300744_curses_window_addnstr_impl(PyCursesWindowObject *self, int group_left_1,
745 int y, int x, PyObject *str, int n,
746 int group_right_1, long attr)
747/*[clinic end generated code: output=6d21cee2ce6876d9 input=72718415c2744a2a]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000748{
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300749 int rtn;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100750 int strtype;
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300751 PyObject *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100752#ifdef HAVE_NCURSESW
753 wchar_t *wstr = NULL;
754#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300755 attr_t attr_old = A_NORMAL;
756 int use_xy = group_left_1, use_attr = group_right_1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100757 const char *funcname;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000758
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100759#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300760 strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100761#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300762 strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100763#endif
764 if (strtype == 0)
765 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000766
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300767 if (use_attr) {
Victor Stinner26486ea2010-05-15 22:23:53 +0000768 attr_old = getattrs(self->win);
769 (void)wattrset(self->win,attr);
770 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100771#ifdef HAVE_NCURSESW
772 if (strtype == 2) {
773 funcname = "addnwstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300774 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100775 rtn = mvwaddnwstr(self->win,y,x,wstr,n);
776 else
777 rtn = waddnwstr(self->win,wstr,n);
778 PyMem_Free(wstr);
779 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000780 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100781#endif
782 {
783 char *str = PyBytes_AS_STRING(bytesobj);
784 funcname = "addnstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300785 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100786 rtn = mvwaddnstr(self->win,y,x,str,n);
787 else
788 rtn = waddnstr(self->win,str,n);
789 Py_DECREF(bytesobj);
790 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300791 if (use_attr)
Victor Stinner26486ea2010-05-15 22:23:53 +0000792 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100793 return PyCursesCheckERR(rtn, funcname);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000794}
795
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300796/*[clinic input]
797_curses.window.bkgd
798
799 ch: object
800 Background character.
801 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
802 Background attributes.
803 /
804
805Set the background property of the window.
806[clinic start generated code]*/
807
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000808static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300809_curses_window_bkgd_impl(PyCursesWindowObject *self, PyObject *ch, long attr)
810/*[clinic end generated code: output=058290afb2cf4034 input=634015bcb339283d]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000811{
Victor Stinner26486ea2010-05-15 22:23:53 +0000812 chtype bkgd;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000813
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300814 if (!PyCurses_ConvertToChtype(self, ch, &bkgd))
Victor Stinner26486ea2010-05-15 22:23:53 +0000815 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000816
Victor Stinner26486ea2010-05-15 22:23:53 +0000817 return PyCursesCheckERR(wbkgd(self->win, bkgd | attr), "bkgd");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000818}
819
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300820/*[clinic input]
821_curses.window.attroff
822
823 attr: long
824 /
825
826Remove attribute attr from the "background" set.
827[clinic start generated code]*/
Christian Heimes2380ac72008-01-09 00:17:24 +0000828
829static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300830_curses_window_attroff_impl(PyCursesWindowObject *self, long attr)
831/*[clinic end generated code: output=8a2fcd4df682fc64 input=786beedf06a7befe]*/
Christian Heimes2380ac72008-01-09 00:17:24 +0000832{
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300833 return PyCursesCheckERR(wattroff(self->win, (attr_t)attr), "attroff");
Christian Heimes2380ac72008-01-09 00:17:24 +0000834}
835
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300836/*[clinic input]
837_curses.window.attron
838
839 attr: long
840 /
841
842Add attribute attr from the "background" set.
843[clinic start generated code]*/
Christian Heimes2380ac72008-01-09 00:17:24 +0000844
845static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300846_curses_window_attron_impl(PyCursesWindowObject *self, long attr)
847/*[clinic end generated code: output=7afea43b237fa870 input=5a88fba7b1524f32]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000848{
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300849 return PyCursesCheckERR(wattron(self->win, (attr_t)attr), "attron");
850}
851
852/*[clinic input]
853_curses.window.attrset
854
855 attr: long
856 /
857
858Set the "background" set of attributes.
859[clinic start generated code]*/
860
861static PyObject *
862_curses_window_attrset_impl(PyCursesWindowObject *self, long attr)
863/*[clinic end generated code: output=84e379bff20c0433 input=42e400c0d0154ab5]*/
864{
865 return PyCursesCheckERR(wattrset(self->win, (attr_t)attr), "attrset");
866}
867
868/*[clinic input]
869_curses.window.bkgdset
870
871 ch: object
872 Background character.
873 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
874 Background attributes.
875 /
876
877Set the window's background.
878[clinic start generated code]*/
879
880static PyObject *
881_curses_window_bkgdset_impl(PyCursesWindowObject *self, PyObject *ch,
882 long attr)
883/*[clinic end generated code: output=8cb994fc4d7e2496 input=e09c682425c9e45b]*/
884{
Victor Stinner26486ea2010-05-15 22:23:53 +0000885 chtype bkgd;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000886
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300887 if (!PyCurses_ConvertToChtype(self, ch, &bkgd))
Victor Stinner26486ea2010-05-15 22:23:53 +0000888 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000889
Victor Stinner26486ea2010-05-15 22:23:53 +0000890 wbkgdset(self->win, bkgd | attr);
891 return PyCursesCheckERR(0, "bkgdset");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000892}
893
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300894/*[clinic input]
895_curses.window.border
896
897 ls: object(c_default="NULL") = _curses.ACS_VLINE
898 Left side.
899 rs: object(c_default="NULL") = _curses.ACS_VLINE
900 Right side.
901 ts: object(c_default="NULL") = _curses.ACS_HLINE
902 Top side.
903 bs: object(c_default="NULL") = _curses.ACS_HLINE
904 Bottom side.
905 tl: object(c_default="NULL") = _curses.ACS_ULCORNER
906 Upper-left corner.
907 tr: object(c_default="NULL") = _curses.ACS_URCORNER
908 Upper-right corner.
909 bl: object(c_default="NULL") = _curses.ACS_LLCORNER
910 Bottom-left corner.
911 br: object(c_default="NULL") = _curses.ACS_LRCORNER
912 Bottom-right corner.
913 /
914
915Draw a border around the edges of the window.
916
917Each parameter specifies the character to use for a specific part of the
918border. The characters can be specified as integers or as one-character
919strings. A 0 value for any parameter will cause the default character to be
920used for that parameter.
921[clinic start generated code]*/
922
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000923static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300924_curses_window_border_impl(PyCursesWindowObject *self, PyObject *ls,
925 PyObject *rs, PyObject *ts, PyObject *bs,
926 PyObject *tl, PyObject *tr, PyObject *bl,
927 PyObject *br)
928/*[clinic end generated code: output=670ef38d3d7c2aa3 input=e015f735d67a240b]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000929{
Victor Stinner26486ea2010-05-15 22:23:53 +0000930 chtype ch[8];
931 int i;
Andrew M. Kuchling16e65a02001-10-20 16:05:52 +0000932
Victor Stinner26486ea2010-05-15 22:23:53 +0000933 /* Clear the array of parameters */
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300934 for(i=0; i<8; i++)
Victor Stinner26486ea2010-05-15 22:23:53 +0000935 ch[i] = 0;
Andrew M. Kuchling16e65a02001-10-20 16:05:52 +0000936
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300937#define CONVERTTOCHTYPE(obj, i) \
938 if ((obj) != NULL && !PyCurses_ConvertToChtype(self, (obj), &ch[(i)])) \
Victor Stinner26486ea2010-05-15 22:23:53 +0000939 return NULL;
940
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300941 CONVERTTOCHTYPE(ls, 0);
942 CONVERTTOCHTYPE(rs, 1);
943 CONVERTTOCHTYPE(ts, 2);
944 CONVERTTOCHTYPE(bs, 3);
945 CONVERTTOCHTYPE(tl, 4);
946 CONVERTTOCHTYPE(tr, 5);
947 CONVERTTOCHTYPE(bl, 6);
948 CONVERTTOCHTYPE(br, 7);
949
950#undef CONVERTTOCHTYPE
Victor Stinner26486ea2010-05-15 22:23:53 +0000951
952 wborder(self->win,
953 ch[0], ch[1], ch[2], ch[3],
954 ch[4], ch[5], ch[6], ch[7]);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200955 Py_RETURN_NONE;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000956}
957
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300958/*[clinic input]
959_curses.window.box
960
961 [
962 verch: object(c_default="_PyLong_Zero") = 0
963 Left and right side.
964 horch: object(c_default="_PyLong_Zero") = 0
965 Top and bottom side.
966 ]
967 /
968
969Draw a border around the edges of the window.
970
971Similar to border(), but both ls and rs are verch and both ts and bs are
972horch. The default corner characters are always used by this function.
973[clinic start generated code]*/
974
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000975static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300976_curses_window_box_impl(PyCursesWindowObject *self, int group_right_1,
977 PyObject *verch, PyObject *horch)
978/*[clinic end generated code: output=f3fcb038bb287192 input=465a121741c1efdf]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000979{
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300980 chtype ch1 = 0, ch2 = 0;
981 if (group_right_1) {
982 if (!PyCurses_ConvertToChtype(self, verch, &ch1)) {
Serhiy Storchaka4f469c02017-11-01 20:48:49 +0200983 return NULL;
984 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300985 if (!PyCurses_ConvertToChtype(self, horch, &ch2)) {
Serhiy Storchaka4f469c02017-11-01 20:48:49 +0200986 return NULL;
987 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000988 }
989 box(self->win,ch1,ch2);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200990 Py_RETURN_NONE;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000991}
992
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000993#if defined(HAVE_NCURSES_H) || defined(MVWDELCH_IS_EXPRESSION)
994#define py_mvwdelch mvwdelch
995#else
996int py_mvwdelch(WINDOW *w, int y, int x)
997{
Victor Stinner26486ea2010-05-15 22:23:53 +0000998 mvwdelch(w,y,x);
999 /* On HP/UX, mvwdelch already returns. On other systems,
1000 we may well run into this return statement. */
1001 return 0;
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001002}
1003#endif
1004
Masayuki Yamamoto8bc7d632017-11-01 21:05:26 +09001005#if defined(HAVE_CURSES_IS_PAD)
1006#define py_is_pad(win) is_pad(win)
1007#elif defined(WINDOW_HAS_FLAGS)
1008#define py_is_pad(win) ((win) ? ((win)->_flags & _ISPAD) != 0 : FALSE)
1009#endif
1010
Guido van Rossumd8faa362007-04-27 19:54:29 +00001011/* chgat, added by Fabian Kreutz <fabian.kreutz at gmx.net> */
Serhiy Storchaka894ebd02017-11-01 14:34:20 +02001012#ifdef HAVE_CURSES_WCHGAT
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001013/*[-clinic input]
1014_curses.window.chgat
1015
1016 [
1017 y: int
1018 Y-coordinate.
1019 x: int
1020 X-coordinate.
1021 ]
1022
1023 n: int = -1
1024 Number of characters.
1025
1026 attr: long
1027 Attributes for characters.
1028 /
1029
1030Set the attributes of characters.
1031
1032Set the attributes of num characters at the current cursor position, or at
1033position (y, x) if supplied. If no value of num is given or num = -1, the
1034attribute will be set on all the characters to the end of the line. This
1035function does not move the cursor. The changed line will be touched using
1036the touchline() method so that the contents will be redisplayed by the next
1037window refresh.
1038[-clinic start generated code]*/
Guido van Rossumd8faa362007-04-27 19:54:29 +00001039static PyObject *
1040PyCursesWindow_ChgAt(PyCursesWindowObject *self, PyObject *args)
1041{
Victor Stinner26486ea2010-05-15 22:23:53 +00001042 int rtn;
1043 int x, y;
1044 int num = -1;
1045 short color;
1046 attr_t attr = A_NORMAL;
1047 long lattr;
1048 int use_xy = FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001049
Victor Stinner26486ea2010-05-15 22:23:53 +00001050 switch (PyTuple_Size(args)) {
1051 case 1:
1052 if (!PyArg_ParseTuple(args,"l;attr", &lattr))
1053 return NULL;
1054 attr = lattr;
1055 break;
1056 case 2:
1057 if (!PyArg_ParseTuple(args,"il;n,attr", &num, &lattr))
1058 return NULL;
1059 attr = lattr;
1060 break;
1061 case 3:
1062 if (!PyArg_ParseTuple(args,"iil;int,int,attr", &y, &x, &lattr))
1063 return NULL;
1064 attr = lattr;
1065 use_xy = TRUE;
1066 break;
1067 case 4:
1068 if (!PyArg_ParseTuple(args,"iiil;int,int,n,attr", &y, &x, &num, &lattr))
1069 return NULL;
1070 attr = lattr;
1071 use_xy = TRUE;
1072 break;
1073 default:
1074 PyErr_SetString(PyExc_TypeError, "chgat requires 1 to 4 arguments");
1075 return NULL;
1076 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001077
Victor Stinner26486ea2010-05-15 22:23:53 +00001078 color = (short)((attr >> 8) & 0xff);
1079 attr = attr - (color << 8);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001080
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001081 if (use_xy) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001082 rtn = mvwchgat(self->win,y,x,num,attr,color,NULL);
1083 touchline(self->win,y,1);
1084 } else {
1085 getyx(self->win,y,x);
1086 rtn = wchgat(self->win,num,attr,color,NULL);
1087 touchline(self->win,y,1);
1088 }
1089 return PyCursesCheckERR(rtn, "chgat");
Guido van Rossumd8faa362007-04-27 19:54:29 +00001090}
Serhiy Storchaka894ebd02017-11-01 14:34:20 +02001091#endif
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001092
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001093/*[clinic input]
1094_curses.window.delch
Guido van Rossum85738471995-02-17 13:50:17 +00001095
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001096 [
1097 y: int
1098 Y-coordinate.
1099 x: int
1100 X-coordinate.
1101 ]
1102 /
1103
1104Delete any character at (y, x).
1105[clinic start generated code]*/
1106
1107static PyObject *
1108_curses_window_delch_impl(PyCursesWindowObject *self, int group_right_1,
1109 int y, int x)
1110/*[clinic end generated code: output=22e77bb9fa11b461 input=d2f79e630a4fc6d0]*/
1111{
1112 if (!group_right_1) {
1113 return PyCursesCheckERR(wdelch(self->win), "wdelch");
Victor Stinner26486ea2010-05-15 22:23:53 +00001114 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001115 else {
1116 return PyCursesCheckERR(py_mvwdelch(self->win, y, x), "mvwdelch");
1117 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001118}
1119
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001120/*[clinic input]
1121_curses.window.derwin
1122
1123 [
1124 nlines: int = 0
1125 Height.
1126 ncols: int = 0
1127 Width.
1128 ]
1129 begin_y: int
1130 Top side y-coordinate.
1131 begin_x: int
1132 Left side x-coordinate.
1133 /
1134
1135Create a sub-window (window-relative coordinates).
1136
1137derwin() is the same as calling subwin(), except that begin_y and begin_x
1138are relative to the origin of the window, rather than relative to the entire
1139screen.
1140[clinic start generated code]*/
1141
Guido van Rossumf6971e21994-08-30 12:25:20 +00001142static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001143_curses_window_derwin_impl(PyCursesWindowObject *self, int group_left_1,
1144 int nlines, int ncols, int begin_y, int begin_x)
1145/*[clinic end generated code: output=7924b112d9f70d6e input=966d9481f7f5022e]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001146{
Victor Stinner26486ea2010-05-15 22:23:53 +00001147 WINDOW *win;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001148
Victor Stinner26486ea2010-05-15 22:23:53 +00001149 win = derwin(self->win,nlines,ncols,begin_y,begin_x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001150
Victor Stinner26486ea2010-05-15 22:23:53 +00001151 if (win == NULL) {
1152 PyErr_SetString(PyCursesError, catchall_NULL);
1153 return NULL;
1154 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001155
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001156 return (PyObject *)PyCursesWindow_New(win, NULL);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001157}
1158
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001159/*[clinic input]
1160_curses.window.echochar
1161
1162 ch: object
1163 Character to add.
1164
1165 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
1166 Attributes for the character.
1167 /
1168
1169Add character ch with attribute attr, and refresh.
1170[clinic start generated code]*/
1171
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001172static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001173_curses_window_echochar_impl(PyCursesWindowObject *self, PyObject *ch,
1174 long attr)
1175/*[clinic end generated code: output=13e7dd875d4b9642 input=e7f34b964e92b156]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001176{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001177 chtype ch_;
Guido van Rossum85738471995-02-17 13:50:17 +00001178
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001179 if (!PyCurses_ConvertToChtype(self, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00001180 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00001181
Masayuki Yamamoto8bc7d632017-11-01 21:05:26 +09001182#ifdef py_is_pad
1183 if (py_is_pad(self->win)) {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001184 return PyCursesCheckERR(pechochar(self->win, ch_ | (attr_t)attr),
Victor Stinner26486ea2010-05-15 22:23:53 +00001185 "echochar");
Masayuki Yamamoto8bc7d632017-11-01 21:05:26 +09001186 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001187 else
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001188#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001189 return PyCursesCheckERR(wechochar(self->win, ch_ | (attr_t)attr),
Victor Stinner26486ea2010-05-15 22:23:53 +00001190 "echochar");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001191}
1192
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001193#ifdef NCURSES_MOUSE_VERSION
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001194/*[clinic input]
1195_curses.window.enclose -> long
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001196
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001197 y: int
1198 Y-coordinate.
1199 x: int
1200 X-coordinate.
1201 /
1202
1203Return True if the screen-relative coordinates are enclosed by the window.
1204[clinic start generated code]*/
1205
1206static long
1207_curses_window_enclose_impl(PyCursesWindowObject *self, int y, int x)
1208/*[clinic end generated code: output=5251c961cbe3df63 input=dfe1d9d4d05d8642]*/
1209{
1210 return wenclose(self->win, y, x);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001211}
1212#endif
1213
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001214/*[clinic input]
1215_curses.window.getbkgd -> long
1216
1217Return the window's current background character/attribute pair.
1218[clinic start generated code]*/
1219
1220static long
1221_curses_window_getbkgd_impl(PyCursesWindowObject *self)
1222/*[clinic end generated code: output=c52b25dc16b215c3 input=a69db882fa35426c]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001223{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001224 return (long) getbkgd(self->win);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001225}
1226
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001227/*[clinic input]
1228_curses.window.getch -> int
1229
1230 [
1231 y: int
1232 Y-coordinate.
1233 x: int
1234 X-coordinate.
1235 ]
1236 /
1237
1238Get a character code from terminal keyboard.
1239
1240The integer returned does not have to be in ASCII range: function keys,
1241keypad keys and so on return numbers higher than 256. In no-delay mode, -1
1242is returned if there is no input, else getch() waits until a key is pressed.
1243[clinic start generated code]*/
1244
1245static int
1246_curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
1247 int y, int x)
1248/*[clinic end generated code: output=980aa6af0c0ca387 input=bb24ebfb379f991f]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001249{
Victor Stinner26486ea2010-05-15 22:23:53 +00001250 int rtn;
Guido van Rossum85738471995-02-17 13:50:17 +00001251
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001252 Py_BEGIN_ALLOW_THREADS
1253 if (!group_right_1) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001254 rtn = wgetch(self->win);
Victor Stinner26486ea2010-05-15 22:23:53 +00001255 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001256 else {
1257 rtn = mvwgetch(self->win, y, x);
1258 }
1259 Py_END_ALLOW_THREADS
1260
1261 return rtn;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001262}
Guido van Rossum85738471995-02-17 13:50:17 +00001263
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001264/*[clinic input]
1265_curses.window.getkey
1266
1267 [
1268 y: int
1269 Y-coordinate.
1270 x: int
1271 X-coordinate.
1272 ]
1273 /
1274
1275Get a character (string) from terminal keyboard.
1276
1277Returning a string instead of an integer, as getch() does. Function keys,
1278keypad keys and other special keys return a multibyte string containing the
1279key name. In no-delay mode, an exception is raised if there is no input.
1280[clinic start generated code]*/
1281
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001282static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001283_curses_window_getkey_impl(PyCursesWindowObject *self, int group_right_1,
1284 int y, int x)
1285/*[clinic end generated code: output=8490a182db46b10f input=be2dee34f5cf57f8]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001286{
Victor Stinner26486ea2010-05-15 22:23:53 +00001287 int rtn;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001288
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001289 Py_BEGIN_ALLOW_THREADS
1290 if (!group_right_1) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001291 rtn = wgetch(self->win);
Victor Stinner26486ea2010-05-15 22:23:53 +00001292 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001293 else {
1294 rtn = mvwgetch(self->win, y, x);
1295 }
1296 Py_END_ALLOW_THREADS
1297
Victor Stinner26486ea2010-05-15 22:23:53 +00001298 if (rtn == ERR) {
1299 /* getch() returns ERR in nodelay mode */
R David Murrayf5d7cc22013-03-19 16:23:09 -04001300 PyErr_CheckSignals();
1301 if (!PyErr_Occurred())
1302 PyErr_SetString(PyCursesError, "no input");
Victor Stinner26486ea2010-05-15 22:23:53 +00001303 return NULL;
Serhiy Storchaka7e687902017-11-01 16:03:40 +02001304 } else if (rtn <= 255) {
1305#ifdef NCURSES_VERSION_MAJOR
1306#if NCURSES_VERSION_MAJOR*100+NCURSES_VERSION_MINOR <= 507
1307 /* Work around a bug in ncurses 5.7 and earlier */
1308 if (rtn < 0) {
1309 rtn += 256;
1310 }
1311#endif
1312#endif
1313 return PyUnicode_FromOrdinal(rtn);
Victor Stinner26486ea2010-05-15 22:23:53 +00001314 } else {
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02001315 const char *knp = keyname(rtn);
Victor Stinner26486ea2010-05-15 22:23:53 +00001316 return PyUnicode_FromString((knp == NULL) ? "" : knp);
1317 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001318}
1319
Nadeem Vawda9e2e9902011-07-31 15:01:11 +02001320#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001321/*[clinic input]
1322_curses.window.get_wch
1323
1324 [
1325 y: int
1326 Y-coordinate.
1327 x: int
1328 X-coordinate.
1329 ]
1330 /
1331
1332Get a wide character from terminal keyboard.
1333
1334Return a character for most keys, or an integer for function keys,
1335keypad keys, and other special keys.
1336[clinic start generated code]*/
1337
Guido van Rossumf6971e21994-08-30 12:25:20 +00001338static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001339_curses_window_get_wch_impl(PyCursesWindowObject *self, int group_right_1,
1340 int y, int x)
1341/*[clinic end generated code: output=9f4f86e91fe50ef3 input=dd7e5367fb49dc48]*/
Victor Stinnera7878b72011-07-14 23:07:44 +02001342{
Victor Stinnera7878b72011-07-14 23:07:44 +02001343 int ct;
1344 wint_t rtn;
1345
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001346 Py_BEGIN_ALLOW_THREADS
1347 if (!group_right_1) {
1348 ct = wget_wch(self->win ,&rtn);
Victor Stinnera7878b72011-07-14 23:07:44 +02001349 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001350 else {
1351 ct = mvwget_wch(self->win, y, x, &rtn);
1352 }
1353 Py_END_ALLOW_THREADS
1354
Victor Stinnera7878b72011-07-14 23:07:44 +02001355 if (ct == ERR) {
Victor Stinnerbd2d30c2013-03-21 12:21:06 +01001356 if (PyErr_CheckSignals())
1357 return NULL;
1358
Victor Stinnera7878b72011-07-14 23:07:44 +02001359 /* get_wch() returns ERR in nodelay mode */
1360 PyErr_SetString(PyCursesError, "no input");
1361 return NULL;
1362 }
Victor Stinner1d39cde2012-08-29 01:40:57 +02001363 if (ct == KEY_CODE_YES)
1364 return PyLong_FromLong(rtn);
1365 else
1366 return PyUnicode_FromOrdinal(rtn);
Victor Stinnera7878b72011-07-14 23:07:44 +02001367}
Nadeem Vawda9e2e9902011-07-31 15:01:11 +02001368#endif
Victor Stinnera7878b72011-07-14 23:07:44 +02001369
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001370/*[-clinic input]
1371_curses.window.getstr
1372
1373 [
1374 y: int
1375 Y-coordinate.
1376 x: int
1377 X-coordinate.
1378 ]
1379 n: int = 1023
1380 Maximal number of characters.
1381 /
1382
1383Read a string from the user, with primitive line editing capacity.
1384[-clinic start generated code]*/
1385
Victor Stinnera7878b72011-07-14 23:07:44 +02001386static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001387PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001388{
Victor Stinner26486ea2010-05-15 22:23:53 +00001389 int x, y, n;
1390 char rtn[1024]; /* This should be big enough.. I hope */
1391 int rtn2;
Guido van Rossum85738471995-02-17 13:50:17 +00001392
Victor Stinner26486ea2010-05-15 22:23:53 +00001393 switch (PyTuple_Size(args)) {
1394 case 0:
1395 Py_BEGIN_ALLOW_THREADS
1396 rtn2 = wgetnstr(self->win,rtn, 1023);
1397 Py_END_ALLOW_THREADS
1398 break;
1399 case 1:
1400 if (!PyArg_ParseTuple(args,"i;n", &n))
1401 return NULL;
Benjamin Peterson40a77c32016-08-13 18:15:28 -07001402 if (n < 0) {
1403 PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1404 return NULL;
1405 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001406 Py_BEGIN_ALLOW_THREADS
Victor Stinner640c35c2013-06-04 23:14:37 +02001407 rtn2 = wgetnstr(self->win, rtn, Py_MIN(n, 1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001408 Py_END_ALLOW_THREADS
1409 break;
1410 case 2:
1411 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1412 return NULL;
1413 Py_BEGIN_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001414#ifdef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00001415 rtn2 = wmove(self->win,y,x)==ERR ? ERR : wgetnstr(self->win, rtn, 1023);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001416#else
Victor Stinner26486ea2010-05-15 22:23:53 +00001417 rtn2 = mvwgetnstr(self->win,y,x,rtn, 1023);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001418#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001419 Py_END_ALLOW_THREADS
1420 break;
1421 case 3:
1422 if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
1423 return NULL;
Benjamin Peterson40a77c32016-08-13 18:15:28 -07001424 if (n < 0) {
1425 PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1426 return NULL;
1427 }
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +00001428#ifdef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00001429 Py_BEGIN_ALLOW_THREADS
1430 rtn2 = wmove(self->win,y,x)==ERR ? ERR :
Victor Stinner640c35c2013-06-04 23:14:37 +02001431 wgetnstr(self->win, rtn, Py_MIN(n, 1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001432 Py_END_ALLOW_THREADS
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001433#else
Victor Stinner26486ea2010-05-15 22:23:53 +00001434 Py_BEGIN_ALLOW_THREADS
Victor Stinner640c35c2013-06-04 23:14:37 +02001435 rtn2 = mvwgetnstr(self->win, y, x, rtn, Py_MIN(n, 1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001436 Py_END_ALLOW_THREADS
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001437#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001438 break;
1439 default:
1440 PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 3 arguments");
1441 return NULL;
1442 }
1443 if (rtn2 == ERR)
1444 rtn[0] = 0;
1445 return PyBytes_FromString(rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001446}
1447
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001448/*[clinic input]
1449_curses.window.hline
1450
1451 [
1452 y: int
1453 Starting Y-coordinate.
1454 x: int
1455 Starting X-coordinate.
1456 ]
1457
1458 ch: object
1459 Character to draw.
1460 n: int
1461 Line length.
1462
1463 [
1464 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
1465 Attributes for the characters.
1466 ]
1467 /
1468
1469Display a horizontal line.
1470[clinic start generated code]*/
1471
Guido van Rossumf6971e21994-08-30 12:25:20 +00001472static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001473_curses_window_hline_impl(PyCursesWindowObject *self, int group_left_1,
1474 int y, int x, PyObject *ch, int n,
1475 int group_right_1, long attr)
1476/*[clinic end generated code: output=c00d489d61fc9eef input=81a4dea47268163e]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001477{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001478 chtype ch_;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001479
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001480 if (!PyCurses_ConvertToChtype(self, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00001481 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001482 if (group_left_1) {
1483 if (wmove(self->win, y, x) == ERR) {
1484 return PyCursesCheckERR(ERR, "wmove");
1485 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001486 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001487 return PyCursesCheckERR(whline(self->win, ch_ | (attr_t)attr, n), "hline");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001488}
1489
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001490/*[clinic input]
1491_curses.window.insch
1492
1493 [
1494 y: int
1495 Y-coordinate.
1496 x: int
1497 X-coordinate.
1498 ]
1499
1500 ch: object
1501 Character to insert.
1502
1503 [
1504 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
1505 Attributes for the character.
1506 ]
1507 /
1508
1509Insert a character before the current or specified position.
1510
1511All characters to the right of the cursor are shifted one position right, with
1512the rightmost characters on the line being lost.
1513[clinic start generated code]*/
1514
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001515static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001516_curses_window_insch_impl(PyCursesWindowObject *self, int group_left_1,
1517 int y, int x, PyObject *ch, int group_right_1,
1518 long attr)
1519/*[clinic end generated code: output=ade8cfe3a3bf3e34 input=336342756ee19812]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001520{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001521 int rtn;
1522 chtype ch_ = 0;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001523
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001524 if (!PyCurses_ConvertToChtype(self, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00001525 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001526
1527 if (!group_left_1) {
1528 rtn = winsch(self->win, ch_ | (attr_t)attr);
Victor Stinner26486ea2010-05-15 22:23:53 +00001529 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001530 else {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001531 rtn = mvwinsch(self->win, y, x, ch_ | (attr_t)attr);
Victor Stinner26486ea2010-05-15 22:23:53 +00001532 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001533
Victor Stinner26486ea2010-05-15 22:23:53 +00001534 return PyCursesCheckERR(rtn, "insch");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001535}
1536
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001537/*[clinic input]
1538_curses.window.inch -> unsigned_long
1539
1540 [
1541 y: int
1542 Y-coordinate.
1543 x: int
1544 X-coordinate.
1545 ]
1546 /
1547
1548Return the character at the given position in the window.
1549
1550The bottom 8 bits are the character proper, and upper bits are the attributes.
1551[clinic start generated code]*/
1552
1553static unsigned long
1554_curses_window_inch_impl(PyCursesWindowObject *self, int group_right_1,
1555 int y, int x)
1556/*[clinic end generated code: output=6c4719fe978fe86a input=fac23ee11e3b3a66]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001557{
Christian Heimesabbc8ca2013-12-04 08:50:22 +01001558 unsigned long rtn;
Guido van Rossum85738471995-02-17 13:50:17 +00001559
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001560 if (!group_right_1) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001561 rtn = winch(self->win);
Victor Stinner26486ea2010-05-15 22:23:53 +00001562 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001563 else {
1564 rtn = mvwinch(self->win, y, x);
1565 }
1566
1567 return rtn;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001568}
1569
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001570/*[-clinic input]
1571_curses.window.instr
1572
1573 [
1574 y: int
1575 Y-coordinate.
1576 x: int
1577 X-coordinate.
1578 ]
1579 n: int = 1023
1580 Maximal number of characters.
1581 /
1582
1583Return a string of characters, extracted from the window.
1584
1585Return a string of characters, extracted from the window starting at the
1586current cursor position, or at y, x if specified. Attributes are stripped
1587from the characters. If n is specified, instr() returns a string at most
1588n characters long (exclusive of the trailing NUL).
1589[-clinic start generated code]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001590static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001591PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001592{
Victor Stinner26486ea2010-05-15 22:23:53 +00001593 int x, y, n;
1594 char rtn[1024]; /* This should be big enough.. I hope */
1595 int rtn2;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001596
Victor Stinner26486ea2010-05-15 22:23:53 +00001597 switch (PyTuple_Size(args)) {
1598 case 0:
1599 rtn2 = winnstr(self->win,rtn, 1023);
1600 break;
1601 case 1:
1602 if (!PyArg_ParseTuple(args,"i;n", &n))
1603 return NULL;
Benjamin Peterson432ea4f2016-08-15 21:40:14 -07001604 if (n < 0) {
1605 PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1606 return NULL;
1607 }
Victor Stinner640c35c2013-06-04 23:14:37 +02001608 rtn2 = winnstr(self->win, rtn, Py_MIN(n, 1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001609 break;
1610 case 2:
1611 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1612 return NULL;
1613 rtn2 = mvwinnstr(self->win,y,x,rtn,1023);
1614 break;
1615 case 3:
1616 if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
1617 return NULL;
Benjamin Peterson432ea4f2016-08-15 21:40:14 -07001618 if (n < 0) {
1619 PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1620 return NULL;
1621 }
Victor Stinner640c35c2013-06-04 23:14:37 +02001622 rtn2 = mvwinnstr(self->win, y, x, rtn, Py_MIN(n,1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001623 break;
1624 default:
1625 PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments");
1626 return NULL;
1627 }
1628 if (rtn2 == ERR)
1629 rtn[0] = 0;
1630 return PyBytes_FromString(rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001631}
1632
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001633/*[clinic input]
1634_curses.window.insstr
1635
1636 [
1637 y: int
1638 Y-coordinate.
1639 x: int
1640 X-coordinate.
1641 ]
1642
1643 str: object
1644 String to insert.
1645
1646 [
1647 attr: long
1648 Attributes for characters.
1649 ]
1650 /
1651
1652Insert the string before the current or specified position.
1653
1654Insert a character string (as many characters as will fit on the line)
1655before the character under the cursor. All characters to the right of
1656the cursor are shifted right, with the rightmost characters on the line
1657being lost. The cursor position does not change (after moving to y, x,
1658if specified).
1659[clinic start generated code]*/
1660
Guido van Rossumf6971e21994-08-30 12:25:20 +00001661static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001662_curses_window_insstr_impl(PyCursesWindowObject *self, int group_left_1,
1663 int y, int x, PyObject *str, int group_right_1,
1664 long attr)
1665/*[clinic end generated code: output=c259a5265ad0b777 input=6827cddc6340a7f3]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001666{
Victor Stinner26486ea2010-05-15 22:23:53 +00001667 int rtn;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001668 int strtype;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001669 PyObject *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001670#ifdef HAVE_NCURSESW
1671 wchar_t *wstr = NULL;
1672#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001673 attr_t attr_old = A_NORMAL;
1674 int use_xy = group_left_1, use_attr = group_right_1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001675 const char *funcname;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001676
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001677#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001678 strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001679#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001680 strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001681#endif
1682 if (strtype == 0)
1683 return NULL;
1684
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001685 if (use_attr) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001686 attr_old = getattrs(self->win);
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001687 (void)wattrset(self->win, (attr_t)attr);
Victor Stinner26486ea2010-05-15 22:23:53 +00001688 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001689#ifdef HAVE_NCURSESW
1690 if (strtype == 2) {
1691 funcname = "inswstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001692 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001693 rtn = mvwins_wstr(self->win,y,x,wstr);
1694 else
1695 rtn = wins_wstr(self->win,wstr);
1696 PyMem_Free(wstr);
1697 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001698 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001699#endif
1700 {
1701 char *str = PyBytes_AS_STRING(bytesobj);
1702 funcname = "insstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001703 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001704 rtn = mvwinsstr(self->win,y,x,str);
1705 else
1706 rtn = winsstr(self->win,str);
1707 Py_DECREF(bytesobj);
1708 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001709 if (use_attr)
Victor Stinner26486ea2010-05-15 22:23:53 +00001710 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001711 return PyCursesCheckERR(rtn, funcname);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001712}
1713
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001714/*[clinic input]
1715_curses.window.insnstr
1716
1717 [
1718 y: int
1719 Y-coordinate.
1720 x: int
1721 X-coordinate.
1722 ]
1723
1724 str: object
1725 String to insert.
1726
1727 n: int
1728 Maximal number of characters.
1729
1730 [
1731 attr: long
1732 Attributes for characters.
1733 ]
1734 /
1735
1736Insert at most n characters of the string.
1737
1738Insert a character string (as many characters as will fit on the line)
1739before the character under the cursor, up to n characters. If n is zero
1740or negative, the entire string is inserted. All characters to the right
1741of the cursor are shifted right, with the rightmost characters on the line
1742being lost. The cursor position does not change (after moving to y, x, if
1743specified).
1744[clinic start generated code]*/
1745
Guido van Rossumf6971e21994-08-30 12:25:20 +00001746static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001747_curses_window_insnstr_impl(PyCursesWindowObject *self, int group_left_1,
1748 int y, int x, PyObject *str, int n,
1749 int group_right_1, long attr)
1750/*[clinic end generated code: output=971a32ea6328ec8b input=70fa0cd543901a4c]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001751{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001752 int rtn;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001753 int strtype;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001754 PyObject *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001755#ifdef HAVE_NCURSESW
1756 wchar_t *wstr = NULL;
1757#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001758 attr_t attr_old = A_NORMAL;
1759 int use_xy = group_left_1, use_attr = group_right_1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001760 const char *funcname;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001761
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001762#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001763 strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001764#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001765 strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001766#endif
1767 if (strtype == 0)
1768 return NULL;
1769
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001770 if (use_attr) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001771 attr_old = getattrs(self->win);
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001772 (void)wattrset(self->win, (attr_t)attr);
Victor Stinner26486ea2010-05-15 22:23:53 +00001773 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001774#ifdef HAVE_NCURSESW
1775 if (strtype == 2) {
1776 funcname = "insn_wstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001777 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001778 rtn = mvwins_nwstr(self->win,y,x,wstr,n);
1779 else
1780 rtn = wins_nwstr(self->win,wstr,n);
1781 PyMem_Free(wstr);
1782 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001783 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001784#endif
1785 {
1786 char *str = PyBytes_AS_STRING(bytesobj);
1787 funcname = "insnstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001788 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001789 rtn = mvwinsnstr(self->win,y,x,str,n);
1790 else
1791 rtn = winsnstr(self->win,str,n);
1792 Py_DECREF(bytesobj);
1793 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001794 if (use_attr)
Victor Stinner26486ea2010-05-15 22:23:53 +00001795 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001796 return PyCursesCheckERR(rtn, funcname);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001797}
1798
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001799/*[clinic input]
1800_curses.window.is_linetouched
1801
1802 line: int
1803 Line number.
1804 /
1805
1806Return True if the specified line was modified, otherwise return False.
1807
1808Raise a curses.error exception if line is not valid for the given window.
1809[clinic start generated code]*/
1810
Guido van Rossumf6971e21994-08-30 12:25:20 +00001811static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001812_curses_window_is_linetouched_impl(PyCursesWindowObject *self, int line)
1813/*[clinic end generated code: output=ad4a4edfee2db08c input=a7be0c189f243914]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001814{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001815 int erg;
Victor Stinner26486ea2010-05-15 22:23:53 +00001816 erg = is_linetouched(self->win, line);
1817 if (erg == ERR) {
1818 PyErr_SetString(PyExc_TypeError,
1819 "is_linetouched: line number outside of boundaries");
1820 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001821 }
1822 return PyBool_FromLong(erg);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001823}
1824
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001825#ifdef py_is_pad
1826/*[clinic input]
1827_curses.window.noutrefresh
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001828
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001829 [
1830 pminrow: int
1831 pmincol: int
1832 sminrow: int
1833 smincol: int
1834 smaxrow: int
1835 smaxcol: int
1836 ]
1837 /
1838
1839Mark for refresh but wait.
1840
1841This function updates the data structure representing the desired state of the
1842window, but does not force an update of the physical screen. To accomplish
1843that, call doupdate().
1844[clinic start generated code]*/
1845
1846static PyObject *
1847_curses_window_noutrefresh_impl(PyCursesWindowObject *self,
1848 int group_right_1, int pminrow, int pmincol,
1849 int sminrow, int smincol, int smaxrow,
1850 int smaxcol)
1851/*[clinic end generated code: output=809a1f3c6a03e23e input=3e56898388cd739e]*/
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001852#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001853/*[clinic input]
1854_curses.window.noutrefresh
Andrew M. Kuchlingf4195722000-06-23 01:36:21 +00001855
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001856Mark for refresh but wait.
1857
1858This function updates the data structure representing the desired state of the
1859window, but does not force an update of the physical screen. To accomplish
1860that, call doupdate().
1861[clinic start generated code]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001862
1863static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001864_curses_window_noutrefresh_impl(PyCursesWindowObject *self)
1865/*[clinic end generated code: output=6ef6dec666643fee input=876902e3fa431dbd]*/
1866#endif
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001867{
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001868 int rtn;
Victor Stinner26486ea2010-05-15 22:23:53 +00001869
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001870#ifdef py_is_pad
1871 if (py_is_pad(self->win)) {
1872 if (!group_right_1) {
1873 PyErr_SetString(PyCursesError,
1874 "noutrefresh() called for a pad "
1875 "requires 6 arguments");
Victor Stinner26486ea2010-05-15 22:23:53 +00001876 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001877 }
1878 Py_BEGIN_ALLOW_THREADS
1879 rtn = pnoutrefresh(self->win, pminrow, pmincol,
1880 sminrow, smincol, smaxrow, smaxcol);
1881 Py_END_ALLOW_THREADS
1882 return PyCursesCheckERR(rtn, "pnoutrefresh");
1883 }
1884 if (group_right_1) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001885 PyErr_SetString(PyExc_TypeError,
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001886 "noutrefresh() takes no arguments (6 given)");
Victor Stinner26486ea2010-05-15 22:23:53 +00001887 return NULL;
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001888 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001889#endif
1890 Py_BEGIN_ALLOW_THREADS
1891 rtn = wnoutrefresh(self->win);
1892 Py_END_ALLOW_THREADS
1893 return PyCursesCheckERR(rtn, "wnoutrefresh");
1894}
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001895
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001896/*[clinic input]
1897_curses.window.overlay
1898
1899 destwin: object(type="PyCursesWindowObject *", subclass_of="&PyCursesWindow_Type")
1900
1901 [
1902 sminrow: int
1903 smincol: int
1904 dminrow: int
1905 dmincol: int
1906 dmaxrow: int
1907 dmaxcol: int
1908 ]
1909 /
1910
1911Overlay the window on top of destwin.
1912
1913The windows need not be the same size, only the overlapping region is copied.
1914This copy is non-destructive, which means that the current background
1915character does not overwrite the old contents of destwin.
1916
1917To get fine-grained control over the copied region, the second form of
1918overlay() can be used. sminrow and smincol are the upper-left coordinates
1919of the source window, and the other variables mark a rectangle in the
1920destination window.
1921[clinic start generated code]*/
1922
1923static PyObject *
1924_curses_window_overlay_impl(PyCursesWindowObject *self,
1925 PyCursesWindowObject *destwin, int group_right_1,
1926 int sminrow, int smincol, int dminrow,
1927 int dmincol, int dmaxrow, int dmaxcol)
1928/*[clinic end generated code: output=82bb2c4cb443ca58 input=7edd23ad22cc1984]*/
1929{
1930 int rtn;
1931
1932 if (group_right_1) {
1933 rtn = copywin(self->win, destwin->win, sminrow, smincol,
Victor Stinner26486ea2010-05-15 22:23:53 +00001934 dminrow, dmincol, dmaxrow, dmaxcol, TRUE);
1935 return PyCursesCheckERR(rtn, "copywin");
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001936 }
1937 else {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001938 rtn = overlay(self->win, destwin->win);
Victor Stinner26486ea2010-05-15 22:23:53 +00001939 return PyCursesCheckERR(rtn, "overlay");
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001940 }
1941}
1942
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001943/*[clinic input]
1944_curses.window.overwrite
1945
1946 destwin: object(type="PyCursesWindowObject *", subclass_of="&PyCursesWindow_Type")
1947
1948 [
1949 sminrow: int
1950 smincol: int
1951 dminrow: int
1952 dmincol: int
1953 dmaxrow: int
1954 dmaxcol: int
1955 ]
1956 /
1957
1958Overwrite the window on top of destwin.
1959
1960The windows need not be the same size, in which case only the overlapping
1961region is copied. This copy is destructive, which means that the current
1962background character overwrites the old contents of destwin.
1963
1964To get fine-grained control over the copied region, the second form of
1965overwrite() can be used. sminrow and smincol are the upper-left coordinates
1966of the source window, the other variables mark a rectangle in the destination
1967window.
1968[clinic start generated code]*/
1969
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001970static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001971_curses_window_overwrite_impl(PyCursesWindowObject *self,
1972 PyCursesWindowObject *destwin,
1973 int group_right_1, int sminrow, int smincol,
1974 int dminrow, int dmincol, int dmaxrow,
1975 int dmaxcol)
1976/*[clinic end generated code: output=12ae007d1681be28 input=ea5de1b35cd948e0]*/
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001977{
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001978 int rtn;
Victor Stinner26486ea2010-05-15 22:23:53 +00001979
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001980 if (group_right_1) {
1981 rtn = copywin(self->win, destwin->win, sminrow, smincol,
Victor Stinner26486ea2010-05-15 22:23:53 +00001982 dminrow, dmincol, dmaxrow, dmaxcol, FALSE);
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001983 return PyCursesCheckERR(rtn, "copywin");
1984 }
1985 else {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001986 rtn = overwrite(self->win, destwin->win);
Victor Stinner26486ea2010-05-15 22:23:53 +00001987 return PyCursesCheckERR(rtn, "overwrite");
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001988 }
1989}
1990
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001991/*[clinic input]
1992_curses.window.putwin
1993
1994 file: object
1995 /
1996
1997Write all data associated with the window into the provided file object.
1998
1999This information can be later retrieved using the getwin() function.
2000[clinic start generated code]*/
2001
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00002002static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002003_curses_window_putwin(PyCursesWindowObject *self, PyObject *file)
2004/*[clinic end generated code: output=3a25e2a5e7a040ac input=0608648e09c8ea0a]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002005{
Victor Stinner26486ea2010-05-15 22:23:53 +00002006 /* We have to simulate this by writing to a temporary FILE*,
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002007 then reading back, then writing to the argument file. */
Christian Heimes2b221b72017-03-02 11:09:01 +01002008 FILE *fp;
Victor Stinnerdaf45552013-08-28 00:53:59 +02002009 PyObject *res = NULL;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002010
Christian Heimes2b221b72017-03-02 11:09:01 +01002011 fp = tmpfile();
2012 if (fp == NULL)
2013 return PyErr_SetFromErrno(PyExc_OSError);
2014 if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02002015 goto exit;
Victor Stinner26486ea2010-05-15 22:23:53 +00002016 res = PyCursesCheckERR(putwin(self->win, fp), "putwin");
Victor Stinnerdaf45552013-08-28 00:53:59 +02002017 if (res == NULL)
2018 goto exit;
Victor Stinner26486ea2010-05-15 22:23:53 +00002019 fseek(fp, 0, 0);
2020 while (1) {
2021 char buf[BUFSIZ];
2022 Py_ssize_t n = fread(buf, 1, BUFSIZ, fp);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002023 _Py_IDENTIFIER(write);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002024
Victor Stinner26486ea2010-05-15 22:23:53 +00002025 if (n <= 0)
2026 break;
2027 Py_DECREF(res);
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002028 res = _PyObject_CallMethodId(file, &PyId_write, "y#", buf, n);
Victor Stinner26486ea2010-05-15 22:23:53 +00002029 if (res == NULL)
2030 break;
2031 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02002032
2033exit:
Christian Heimes2b221b72017-03-02 11:09:01 +01002034 fclose(fp);
Guido van Rossum6c95da32007-07-24 00:16:38 +00002035 return res;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002036}
2037
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002038/*[clinic input]
2039_curses.window.redrawln
2040
2041 beg: int
2042 Starting line number.
2043 num: int
2044 The number of lines.
2045 /
2046
2047Mark the specified lines corrupted.
2048
2049They should be completely redrawn on the next refresh() call.
2050[clinic start generated code]*/
2051
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002052static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002053_curses_window_redrawln_impl(PyCursesWindowObject *self, int beg, int num)
2054/*[clinic end generated code: output=ea216e334f9ce1b4 input=152155e258a77a7a]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002055{
Victor Stinner26486ea2010-05-15 22:23:53 +00002056 return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002057}
2058
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002059/*[clinic input]
2060_curses.window.refresh
2061
2062 [
2063 pminrow: int
2064 pmincol: int
2065 sminrow: int
2066 smincol: int
2067 smaxrow: int
2068 smaxcol: int
2069 ]
2070 /
2071
2072Update the display immediately.
2073
2074Synchronize actual screen with previous drawing/deleting methods.
2075The 6 optional arguments can only be specified when the window is a pad
2076created with newpad(). The additional parameters are needed to indicate
2077what part of the pad and screen are involved. pminrow and pmincol specify
2078the upper left-hand corner of the rectangle to be displayed in the pad.
2079sminrow, smincol, smaxrow, and smaxcol specify the edges of the rectangle to
2080be displayed on the screen. The lower right-hand corner of the rectangle to
2081be displayed in the pad is calculated from the screen coordinates, since the
2082rectangles must be the same size. Both rectangles must be entirely contained
2083within their respective structures. Negative values of pminrow, pmincol,
2084sminrow, or smincol are treated as if they were zero.
2085[clinic start generated code]*/
2086
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002087static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002088_curses_window_refresh_impl(PyCursesWindowObject *self, int group_right_1,
2089 int pminrow, int pmincol, int sminrow,
2090 int smincol, int smaxrow, int smaxcol)
2091/*[clinic end generated code: output=42199543115e6e63 input=95e01cb5ffc635d0]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002092{
Victor Stinner26486ea2010-05-15 22:23:53 +00002093 int rtn;
Andrew M. Kuchlingf4195722000-06-23 01:36:21 +00002094
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002095#ifdef py_is_pad
2096 if (py_is_pad(self->win)) {
2097 if (!group_right_1) {
2098 PyErr_SetString(PyCursesError,
2099 "refresh() for a pad requires 6 arguments");
2100 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00002101 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002102 Py_BEGIN_ALLOW_THREADS
2103 rtn = prefresh(self->win, pminrow, pmincol,
2104 sminrow, smincol, smaxrow, smaxcol);
2105 Py_END_ALLOW_THREADS
2106 return PyCursesCheckERR(rtn, "prefresh");
2107 }
2108#endif
2109 if (group_right_1) {
2110 PyErr_SetString(PyExc_TypeError,
2111 "refresh() takes no arguments (6 given)");
Victor Stinner26486ea2010-05-15 22:23:53 +00002112 return NULL;
2113 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002114 Py_BEGIN_ALLOW_THREADS
2115 rtn = wrefresh(self->win);
2116 Py_END_ALLOW_THREADS
2117 return PyCursesCheckERR(rtn, "prefresh");
2118}
2119
2120/*[clinic input]
2121_curses.window.setscrreg
2122
2123 top: int
2124 First line number.
2125 bottom: int
2126 Last line number.
2127 /
2128
2129Define a software scrolling region.
2130
2131All scrolling actions will take place in this region.
2132[clinic start generated code]*/
2133
2134static PyObject *
2135_curses_window_setscrreg_impl(PyCursesWindowObject *self, int top,
2136 int bottom)
2137/*[clinic end generated code: output=486ab5db218d2b1a input=1b517b986838bf0e]*/
2138{
2139 return PyCursesCheckERR(wsetscrreg(self->win, top, bottom), "wsetscrreg");
2140}
2141
2142/*[clinic input]
2143_curses.window.subwin
2144
2145 [
2146 nlines: int = 0
2147 Height.
2148 ncols: int = 0
2149 Width.
2150 ]
2151 begin_y: int
2152 Top side y-coordinate.
2153 begin_x: int
2154 Left side x-coordinate.
2155 /
2156
2157Create a sub-window (screen-relative coordinates).
2158
2159By default, the sub-window will extend from the specified position to the
2160lower right corner of the window.
2161[clinic start generated code]*/
2162
2163static PyObject *
2164_curses_window_subwin_impl(PyCursesWindowObject *self, int group_left_1,
2165 int nlines, int ncols, int begin_y, int begin_x)
2166/*[clinic end generated code: output=93e898afc348f59a input=2129fa47fd57721c]*/
2167{
2168 WINDOW *win;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002169
Victor Stinner26486ea2010-05-15 22:23:53 +00002170 /* printf("Subwin: %i %i %i %i \n", nlines, ncols, begin_y, begin_x); */
Masayuki Yamamoto8bc7d632017-11-01 21:05:26 +09002171#ifdef py_is_pad
2172 if (py_is_pad(self->win)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002173 win = subpad(self->win, nlines, ncols, begin_y, begin_x);
Masayuki Yamamoto8bc7d632017-11-01 21:05:26 +09002174 }
Victor Stinner26486ea2010-05-15 22:23:53 +00002175 else
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00002176#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00002177 win = subwin(self->win, nlines, ncols, begin_y, begin_x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002178
Victor Stinner26486ea2010-05-15 22:23:53 +00002179 if (win == NULL) {
2180 PyErr_SetString(PyCursesError, catchall_NULL);
2181 return NULL;
2182 }
2183
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002184 return (PyObject *)PyCursesWindow_New(win, self->encoding);
Guido van Rossumf6971e21994-08-30 12:25:20 +00002185}
2186
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002187/*[clinic input]
2188_curses.window.scroll
2189
2190 [
2191 lines: int = 1
2192 Number of lines to scroll.
2193 ]
2194 /
2195
2196Scroll the screen or scrolling region.
2197
2198Scroll upward if the argument is positive and downward if it is negative.
2199[clinic start generated code]*/
2200
Guido van Rossumf6971e21994-08-30 12:25:20 +00002201static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002202_curses_window_scroll_impl(PyCursesWindowObject *self, int group_right_1,
2203 int lines)
2204/*[clinic end generated code: output=4541a8a11852d360 input=c969ca0cfabbdbec]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00002205{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002206 if (!group_right_1) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002207 return PyCursesCheckERR(scroll(self->win), "scroll");
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002208 }
2209 else {
2210 return PyCursesCheckERR(wscrl(self->win, lines), "scroll");
Victor Stinner26486ea2010-05-15 22:23:53 +00002211 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00002212}
2213
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002214/*[clinic input]
2215_curses.window.touchline
2216
2217 start: int
2218 count: int
2219 [
2220 changed: bool(accept={int}) = True
2221 ]
2222 /
2223
2224Pretend count lines have been changed, starting with line start.
2225
2226If changed is supplied, it specifies whether the affected lines are marked
2227as having been changed (changed=True) or unchanged (changed=False).
2228[clinic start generated code]*/
2229
Guido van Rossumf6971e21994-08-30 12:25:20 +00002230static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002231_curses_window_touchline_impl(PyCursesWindowObject *self, int start,
2232 int count, int group_right_1, int changed)
2233/*[clinic end generated code: output=65d05b3f7438c61d input=918ad1cbdadf93ea]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00002234{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002235 if (!group_right_1) {
2236 return PyCursesCheckERR(touchline(self->win, start, count), "touchline");
2237 }
2238 else {
2239 return PyCursesCheckERR(wtouchln(self->win, start, count, changed), "touchline");
Victor Stinner26486ea2010-05-15 22:23:53 +00002240 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002241}
2242
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002243/*[clinic input]
2244_curses.window.vline
2245
2246 [
2247 y: int
2248 Starting Y-coordinate.
2249 x: int
2250 Starting X-coordinate.
2251 ]
2252
2253 ch: object
2254 Character to draw.
2255 n: int
2256 Line length.
2257
2258 [
2259 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
2260 Attributes for the character.
2261 ]
2262 /
2263
2264Display a vertical line.
2265[clinic start generated code]*/
2266
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002267static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002268_curses_window_vline_impl(PyCursesWindowObject *self, int group_left_1,
2269 int y, int x, PyObject *ch, int n,
2270 int group_right_1, long attr)
2271/*[clinic end generated code: output=287ad1cc8982217f input=a6f2dc86a4648b32]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002272{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002273 chtype ch_;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002274
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002275 if (!PyCurses_ConvertToChtype(self, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00002276 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002277 if (group_left_1) {
2278 if (wmove(self->win, y, x) == ERR)
2279 return PyCursesCheckERR(ERR, "wmove");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002280 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002281 return PyCursesCheckERR(wvline(self->win, ch_ | (attr_t)attr, n), "vline");
Guido van Rossumf6971e21994-08-30 12:25:20 +00002282}
2283
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002284static PyObject *
2285PyCursesWindow_get_encoding(PyCursesWindowObject *self, void *closure)
2286{
2287 return PyUnicode_FromString(self->encoding);
2288}
2289
2290static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02002291PyCursesWindow_set_encoding(PyCursesWindowObject *self, PyObject *value, void *Py_UNUSED(ignored))
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002292{
2293 PyObject *ascii;
2294 char *encoding;
2295
2296 /* It is illegal to del win.encoding */
2297 if (value == NULL) {
2298 PyErr_SetString(PyExc_TypeError,
2299 "encoding may not be deleted");
2300 return -1;
2301 }
2302
2303 if (!PyUnicode_Check(value)) {
2304 PyErr_SetString(PyExc_TypeError,
2305 "setting encoding to a non-string");
2306 return -1;
2307 }
2308 ascii = PyUnicode_AsASCIIString(value);
2309 if (ascii == NULL)
2310 return -1;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02002311 encoding = _PyMem_Strdup(PyBytes_AS_STRING(ascii));
Ross Lagerwall2dabaf62012-09-07 08:34:23 +02002312 Py_DECREF(ascii);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002313 if (encoding == NULL) {
2314 PyErr_NoMemory();
2315 return -1;
2316 }
Victor Stinner49fc8ec2013-07-07 23:30:24 +02002317 PyMem_Free(self->encoding);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002318 self->encoding = encoding;
2319 return 0;
2320}
2321
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002322#include "clinic/_cursesmodule.c.h"
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002323
Guido van Rossumf6971e21994-08-30 12:25:20 +00002324static PyMethodDef PyCursesWindow_Methods[] = {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002325 _CURSES_WINDOW_ADDCH_METHODDEF
2326 _CURSES_WINDOW_ADDNSTR_METHODDEF
2327 _CURSES_WINDOW_ADDSTR_METHODDEF
2328 _CURSES_WINDOW_ATTROFF_METHODDEF
2329 _CURSES_WINDOW_ATTRON_METHODDEF
2330 _CURSES_WINDOW_ATTRSET_METHODDEF
2331 _CURSES_WINDOW_BKGD_METHODDEF
Serhiy Storchaka894ebd02017-11-01 14:34:20 +02002332#ifdef HAVE_CURSES_WCHGAT
Victor Stinner26486ea2010-05-15 22:23:53 +00002333 {"chgat", (PyCFunction)PyCursesWindow_ChgAt, METH_VARARGS},
Serhiy Storchaka894ebd02017-11-01 14:34:20 +02002334#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002335 _CURSES_WINDOW_BKGDSET_METHODDEF
2336 _CURSES_WINDOW_BORDER_METHODDEF
2337 _CURSES_WINDOW_BOX_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002338 {"clear", (PyCFunction)PyCursesWindow_wclear, METH_NOARGS},
2339 {"clearok", (PyCFunction)PyCursesWindow_clearok, METH_VARARGS},
2340 {"clrtobot", (PyCFunction)PyCursesWindow_wclrtobot, METH_NOARGS},
2341 {"clrtoeol", (PyCFunction)PyCursesWindow_wclrtoeol, METH_NOARGS},
2342 {"cursyncup", (PyCFunction)PyCursesWindow_wcursyncup, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002343 _CURSES_WINDOW_DELCH_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002344 {"deleteln", (PyCFunction)PyCursesWindow_wdeleteln, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002345 _CURSES_WINDOW_DERWIN_METHODDEF
2346 _CURSES_WINDOW_ECHOCHAR_METHODDEF
2347 _CURSES_WINDOW_ENCLOSE_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002348 {"erase", (PyCFunction)PyCursesWindow_werase, METH_NOARGS},
2349 {"getbegyx", (PyCFunction)PyCursesWindow_getbegyx, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002350 _CURSES_WINDOW_GETBKGD_METHODDEF
2351 _CURSES_WINDOW_GETCH_METHODDEF
2352 _CURSES_WINDOW_GETKEY_METHODDEF
2353 _CURSES_WINDOW_GET_WCH_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002354 {"getmaxyx", (PyCFunction)PyCursesWindow_getmaxyx, METH_NOARGS},
2355 {"getparyx", (PyCFunction)PyCursesWindow_getparyx, METH_NOARGS},
2356 {"getstr", (PyCFunction)PyCursesWindow_GetStr, METH_VARARGS},
2357 {"getyx", (PyCFunction)PyCursesWindow_getyx, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002358 _CURSES_WINDOW_HLINE_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002359 {"idcok", (PyCFunction)PyCursesWindow_idcok, METH_VARARGS},
2360 {"idlok", (PyCFunction)PyCursesWindow_idlok, METH_VARARGS},
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002361#ifdef HAVE_CURSES_IMMEDOK
Victor Stinner26486ea2010-05-15 22:23:53 +00002362 {"immedok", (PyCFunction)PyCursesWindow_immedok, METH_VARARGS},
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002363#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002364 _CURSES_WINDOW_INCH_METHODDEF
2365 _CURSES_WINDOW_INSCH_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002366 {"insdelln", (PyCFunction)PyCursesWindow_winsdelln, METH_VARARGS},
2367 {"insertln", (PyCFunction)PyCursesWindow_winsertln, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002368 _CURSES_WINDOW_INSNSTR_METHODDEF
2369 _CURSES_WINDOW_INSSTR_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002370 {"instr", (PyCFunction)PyCursesWindow_InStr, METH_VARARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002371 _CURSES_WINDOW_IS_LINETOUCHED_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002372 {"is_wintouched", (PyCFunction)PyCursesWindow_is_wintouched, METH_NOARGS},
2373 {"keypad", (PyCFunction)PyCursesWindow_keypad, METH_VARARGS},
2374 {"leaveok", (PyCFunction)PyCursesWindow_leaveok, METH_VARARGS},
2375 {"move", (PyCFunction)PyCursesWindow_wmove, METH_VARARGS},
2376 {"mvderwin", (PyCFunction)PyCursesWindow_mvderwin, METH_VARARGS},
2377 {"mvwin", (PyCFunction)PyCursesWindow_mvwin, METH_VARARGS},
2378 {"nodelay", (PyCFunction)PyCursesWindow_nodelay, METH_VARARGS},
2379 {"notimeout", (PyCFunction)PyCursesWindow_notimeout, METH_VARARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002380 _CURSES_WINDOW_NOUTREFRESH_METHODDEF
2381 _CURSES_WINDOW_OVERLAY_METHODDEF
2382 _CURSES_WINDOW_OVERWRITE_METHODDEF
2383 _CURSES_WINDOW_PUTWIN_METHODDEF
2384 _CURSES_WINDOW_REDRAWLN_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002385 {"redrawwin", (PyCFunction)PyCursesWindow_redrawwin, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002386 _CURSES_WINDOW_REFRESH_METHODDEF
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +00002387#ifndef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00002388 {"resize", (PyCFunction)PyCursesWindow_wresize, METH_VARARGS},
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002389#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002390 _CURSES_WINDOW_SCROLL_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002391 {"scrollok", (PyCFunction)PyCursesWindow_scrollok, METH_VARARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002392 _CURSES_WINDOW_SETSCRREG_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002393 {"standend", (PyCFunction)PyCursesWindow_wstandend, METH_NOARGS},
2394 {"standout", (PyCFunction)PyCursesWindow_wstandout, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002395 {"subpad", (PyCFunction)_curses_window_subwin, METH_VARARGS, _curses_window_subwin__doc__},
2396 _CURSES_WINDOW_SUBWIN_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002397 {"syncdown", (PyCFunction)PyCursesWindow_wsyncdown, METH_NOARGS},
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002398#ifdef HAVE_CURSES_SYNCOK
Victor Stinner26486ea2010-05-15 22:23:53 +00002399 {"syncok", (PyCFunction)PyCursesWindow_syncok, METH_VARARGS},
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002400#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00002401 {"syncup", (PyCFunction)PyCursesWindow_wsyncup, METH_NOARGS},
2402 {"timeout", (PyCFunction)PyCursesWindow_wtimeout, METH_VARARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002403 _CURSES_WINDOW_TOUCHLINE_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002404 {"touchwin", (PyCFunction)PyCursesWindow_touchwin, METH_NOARGS},
2405 {"untouchwin", (PyCFunction)PyCursesWindow_untouchwin, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002406 _CURSES_WINDOW_VLINE_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002407 {NULL, NULL} /* sentinel */
Guido van Rossumf6971e21994-08-30 12:25:20 +00002408};
2409
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002410static PyGetSetDef PyCursesWindow_getsets[] = {
2411 {"encoding",
2412 (getter)PyCursesWindow_get_encoding,
2413 (setter)PyCursesWindow_set_encoding,
Ronald Oussoren2a7fe032012-01-17 16:53:20 +01002414 "the typecode character used to create the array"},
2415 {NULL, NULL, NULL, NULL } /* sentinel */
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002416};
2417
Guido van Rossumf6971e21994-08-30 12:25:20 +00002418/* -------------------------------------------------------*/
Guido van Rossum85738471995-02-17 13:50:17 +00002419
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002420PyTypeObject PyCursesWindow_Type = {
Victor Stinner26486ea2010-05-15 22:23:53 +00002421 PyVarObject_HEAD_INIT(NULL, 0)
Victor Stinner61e2bc72017-02-12 23:42:02 +01002422 "_curses.window", /*tp_name*/
Victor Stinner26486ea2010-05-15 22:23:53 +00002423 sizeof(PyCursesWindowObject), /*tp_basicsize*/
2424 0, /*tp_itemsize*/
2425 /* methods */
2426 (destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/
2427 0, /*tp_print*/
2428 (getattrfunc)0, /*tp_getattr*/
2429 (setattrfunc)0, /*tp_setattr*/
2430 0, /*tp_reserved*/
2431 0, /*tp_repr*/
2432 0, /*tp_as_number*/
2433 0, /*tp_as_sequence*/
2434 0, /*tp_as_mapping*/
2435 0, /*tp_hash*/
2436 0, /*tp_call*/
2437 0, /*tp_str*/
2438 0, /*tp_getattro*/
2439 0, /*tp_setattro*/
2440 0, /*tp_as_buffer*/
2441 Py_TPFLAGS_DEFAULT, /*tp_flags*/
2442 0, /*tp_doc*/
2443 0, /*tp_traverse*/
2444 0, /*tp_clear*/
2445 0, /*tp_richcompare*/
2446 0, /*tp_weaklistoffset*/
2447 0, /*tp_iter*/
2448 0, /*tp_iternext*/
2449 PyCursesWindow_Methods, /*tp_methods*/
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002450 0, /* tp_members */
2451 PyCursesWindow_getsets, /* tp_getset */
Guido van Rossumf6971e21994-08-30 12:25:20 +00002452};
2453
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002454/* Function Prototype Macros - They are ugly but very, very useful. ;-)
2455
2456 X - function name
2457 TYPE - parameter Type
2458 ERGSTR - format string for construction of the return value
2459 PARSESTR - format string for argument parsing
2460 */
2461
2462#define NoArgNoReturnFunctionBody(X) \
2463{ \
2464 PyCursesInitialised \
2465 return PyCursesCheckERR(X(), # X); }
2466
2467#define NoArgOrFlagNoReturnFunctionBody(X, flag) \
2468{ \
2469 PyCursesInitialised \
2470 if (flag) \
2471 return PyCursesCheckERR(X(), # X); \
2472 else \
2473 return PyCursesCheckERR(no ## X(), # X); \
2474}
2475
2476#define NoArgReturnIntFunctionBody(X) \
2477{ \
2478 PyCursesInitialised \
2479 return PyLong_FromLong((long) X()); }
2480
2481
2482#define NoArgReturnStringFunctionBody(X) \
2483{ \
2484 PyCursesInitialised \
2485 return PyBytes_FromString(X()); }
2486
2487#define NoArgTrueFalseFunctionBody(X) \
2488{ \
2489 PyCursesInitialised \
2490 return PyBool_FromLong(X()); }
2491
2492#define NoArgNoReturnVoidFunctionBody(X) \
2493{ \
2494 PyCursesInitialised \
2495 X(); \
2496 Py_RETURN_NONE; }
2497
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002498/*********************************************************************
2499 Global Functions
2500**********************************************************************/
Guido van Rossumf6971e21994-08-30 12:25:20 +00002501
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002502#ifdef HAVE_CURSES_FILTER
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002503/*[clinic input]
2504_curses.filter
2505
2506[clinic start generated code]*/
2507
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002508static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002509_curses_filter_impl(PyObject *module)
2510/*[clinic end generated code: output=fb5b8a3642eb70b5 input=668c75a6992d3624]*/
Christian Heimesaf98da12008-01-27 15:18:18 +00002511{
Victor Stinner26486ea2010-05-15 22:23:53 +00002512 /* not checking for PyCursesInitialised here since filter() must
2513 be called before initscr() */
2514 filter();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002515 Py_RETURN_NONE;
Christian Heimesaf98da12008-01-27 15:18:18 +00002516}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002517#endif
Christian Heimesaf98da12008-01-27 15:18:18 +00002518
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002519/*[clinic input]
2520_curses.baudrate
2521
2522Return the output speed of the terminal in bits per second.
2523[clinic start generated code]*/
2524
Christian Heimesaf98da12008-01-27 15:18:18 +00002525static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002526_curses_baudrate_impl(PyObject *module)
2527/*[clinic end generated code: output=3c63c6c401d7d9c0 input=921f022ed04a0fd9]*/
2528NoArgReturnIntFunctionBody(baudrate)
2529
2530/*[clinic input]
2531_curses.beep
2532
2533Emit a short attention sound.
2534[clinic start generated code]*/
2535
2536static PyObject *
2537_curses_beep_impl(PyObject *module)
2538/*[clinic end generated code: output=425274962abe49a2 input=a35698ca7d0162bc]*/
2539NoArgNoReturnFunctionBody(beep)
2540
2541/*[clinic input]
2542_curses.can_change_color
2543
2544Return True if the programmer can change the colors displayed by the terminal.
2545[clinic start generated code]*/
2546
2547static PyObject *
2548_curses_can_change_color_impl(PyObject *module)
2549/*[clinic end generated code: output=359df8c3c77d8bf1 input=d7718884de0092f2]*/
2550NoArgTrueFalseFunctionBody(can_change_color)
2551
2552/*[clinic input]
2553_curses.cbreak
2554
2555 flag: bool(accept={int}) = True
2556 If false, the effect is the same as calling nocbreak().
2557 /
2558
2559Enter cbreak mode.
2560
2561In cbreak mode (sometimes called "rare" mode) normal tty line buffering is
2562turned off and characters are available to be read one by one. However,
2563unlike raw mode, special characters (interrupt, quit, suspend, and flow
2564control) retain their effects on the tty driver and calling program.
2565Calling first raw() then cbreak() leaves the terminal in cbreak mode.
2566[clinic start generated code]*/
2567
2568static PyObject *
2569_curses_cbreak_impl(PyObject *module, int flag)
2570/*[clinic end generated code: output=9f9dee9664769751 input=150be619eb1f1458]*/
2571NoArgOrFlagNoReturnFunctionBody(cbreak, flag)
2572
2573/*[clinic input]
2574_curses.color_content
2575
2576 color_number: short
2577 The number of the color (0 - COLORS).
2578 /
2579
2580Return the red, green, and blue (RGB) components of the specified color.
2581
2582A 3-tuple is returned, containing the R, G, B values for the given color,
2583which will be between 0 (no component) and 1000 (maximum amount of component).
2584[clinic start generated code]*/
2585
2586static PyObject *
2587_curses_color_content_impl(PyObject *module, short color_number)
2588/*[clinic end generated code: output=cb15cf3120d4bfc1 input=5555abb1c11e11b7]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002589{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002590 short r,g,b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002591
Victor Stinner26486ea2010-05-15 22:23:53 +00002592 PyCursesInitialised;
2593 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002594
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002595 if (color_content(color_number, &r, &g, &b) != ERR)
Victor Stinner26486ea2010-05-15 22:23:53 +00002596 return Py_BuildValue("(iii)", r, g, b);
2597 else {
2598 PyErr_SetString(PyCursesError,
2599 "Argument 1 was out of range. Check value of COLORS.");
2600 return NULL;
2601 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002602}
2603
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002604/*[clinic input]
2605_curses.color_pair
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002606
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002607 color_number: short
2608 The number of the color (0 - COLORS).
2609 /
2610
2611Return the attribute value for displaying text in the specified color.
2612
2613This attribute value can be combined with A_STANDOUT, A_REVERSE, and the
2614other A_* attributes. pair_number() is the counterpart to this function.
2615[clinic start generated code]*/
2616
2617static PyObject *
2618_curses_color_pair_impl(PyObject *module, short color_number)
2619/*[clinic end generated code: output=6a84cb6b29ecaf9a input=a9d3eb6f50e4dc12]*/
2620{
Victor Stinner26486ea2010-05-15 22:23:53 +00002621 PyCursesInitialised;
2622 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002623
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002624 return PyLong_FromLong((long) (color_number << 8));
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002625}
2626
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002627/*[clinic input]
2628_curses.curs_set
2629
2630 visibility: int
2631 0 for invisible, 1 for normal visible, or 2 for very visible.
2632 /
2633
2634Set the cursor state.
2635
2636If the terminal supports the visibility requested, the previous cursor
2637state is returned; otherwise, an exception is raised. On many terminals,
2638the "visible" mode is an underline cursor and the "very visible" mode is
2639a block cursor.
2640[clinic start generated code]*/
2641
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002642static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002643_curses_curs_set_impl(PyObject *module, int visibility)
2644/*[clinic end generated code: output=ee8e62483b1d6cd4 input=81a7924a65d29504]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002645{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002646 int erg;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002647
Victor Stinner26486ea2010-05-15 22:23:53 +00002648 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002649
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002650 erg = curs_set(visibility);
Victor Stinner26486ea2010-05-15 22:23:53 +00002651 if (erg == ERR) return PyCursesCheckERR(erg, "curs_set");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002652
Victor Stinner26486ea2010-05-15 22:23:53 +00002653 return PyLong_FromLong((long) erg);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002654}
2655
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002656/*[clinic input]
2657_curses.def_prog_mode
2658
2659Save the current terminal mode as the "program" mode.
2660
2661The "program" mode is the mode when the running program is using curses.
2662
2663Subsequent calls to reset_prog_mode() will restore this mode.
2664[clinic start generated code]*/
2665
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002666static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002667_curses_def_prog_mode_impl(PyObject *module)
2668/*[clinic end generated code: output=05d5a351fff874aa input=768b9cace620dda5]*/
2669NoArgNoReturnFunctionBody(def_prog_mode)
2670
2671/*[clinic input]
2672_curses.def_shell_mode
2673
2674Save the current terminal mode as the "shell" mode.
2675
2676The "shell" mode is the mode when the running program is not using curses.
2677
2678Subsequent calls to reset_shell_mode() will restore this mode.
2679[clinic start generated code]*/
2680
2681static PyObject *
2682_curses_def_shell_mode_impl(PyObject *module)
2683/*[clinic end generated code: output=d6e42f5c768f860f input=5ead21f6f0baa894]*/
2684NoArgNoReturnFunctionBody(def_shell_mode)
2685
2686/*[clinic input]
2687_curses.delay_output
2688
2689 ms: int
2690 Duration in milliseconds.
2691 /
2692
2693Insert a pause in output.
2694[clinic start generated code]*/
2695
2696static PyObject *
2697_curses_delay_output_impl(PyObject *module, int ms)
2698/*[clinic end generated code: output=b6613a67f17fa4f4 input=5316457f5f59196c]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002699{
Victor Stinner26486ea2010-05-15 22:23:53 +00002700 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002701
Victor Stinner26486ea2010-05-15 22:23:53 +00002702 return PyCursesCheckERR(delay_output(ms), "delay_output");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002703}
2704
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002705/*[clinic input]
2706_curses.doupdate
2707
2708Update the physical screen to match the virtual screen.
2709[clinic start generated code]*/
2710
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002711static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002712_curses_doupdate_impl(PyObject *module)
2713/*[clinic end generated code: output=f34536975a75680c input=8da80914432a6489]*/
2714NoArgNoReturnFunctionBody(doupdate)
2715
2716/*[clinic input]
2717_curses.echo
2718
2719 flag: bool(accept={int}) = True
2720 If false, the effect is the same as calling noecho().
2721 /
2722
2723Enter echo mode.
2724
2725In echo mode, each character input is echoed to the screen as it is entered.
2726[clinic start generated code]*/
2727
2728static PyObject *
2729_curses_echo_impl(PyObject *module, int flag)
2730/*[clinic end generated code: output=03acb2ddfa6c8729 input=2e9e891d637eac5d]*/
2731NoArgOrFlagNoReturnFunctionBody(echo, flag)
2732
2733/*[clinic input]
2734_curses.endwin
2735
2736De-initialize the library, and return terminal to normal status.
2737[clinic start generated code]*/
2738
2739static PyObject *
2740_curses_endwin_impl(PyObject *module)
2741/*[clinic end generated code: output=c0150cd96d2f4128 input=e172cfa43062f3fa]*/
2742NoArgNoReturnFunctionBody(endwin)
2743
2744/*[clinic input]
2745_curses.erasechar
2746
2747Return the user's current erase character.
2748[clinic start generated code]*/
2749
2750static PyObject *
2751_curses_erasechar_impl(PyObject *module)
2752/*[clinic end generated code: output=3df305dc6b926b3f input=628c136c3c5758d3]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002753{
Victor Stinner26486ea2010-05-15 22:23:53 +00002754 char ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002755
Victor Stinner26486ea2010-05-15 22:23:53 +00002756 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002757
Victor Stinner26486ea2010-05-15 22:23:53 +00002758 ch = erasechar();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002759
Victor Stinner26486ea2010-05-15 22:23:53 +00002760 return PyBytes_FromStringAndSize(&ch, 1);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002761}
2762
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002763/*[clinic input]
2764_curses.flash
2765
2766Flash the screen.
2767
2768That is, change it to reverse-video and then change it back in a short interval.
2769[clinic start generated code]*/
2770
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002771static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002772_curses_flash_impl(PyObject *module)
2773/*[clinic end generated code: output=488b8a0ebd9ea9b8 input=02fdfb06c8fc3171]*/
2774NoArgNoReturnFunctionBody(flash)
2775
2776/*[clinic input]
2777_curses.flushinp
2778
2779Flush all input buffers.
2780
2781This throws away any typeahead that has been typed by the user and has not
2782yet been processed by the program.
2783[clinic start generated code]*/
2784
2785static PyObject *
2786_curses_flushinp_impl(PyObject *module)
2787/*[clinic end generated code: output=7e7a1fc1473960f5 input=59d042e705cef5ec]*/
2788NoArgNoReturnVoidFunctionBody(flushinp)
2789
2790#ifdef getsyx
2791/*[clinic input]
2792_curses.getsyx
2793
2794Return the current coordinates of the virtual screen cursor.
2795
2796Return a (y, x) tuple. If leaveok is currently true, return (-1, -1).
2797[clinic start generated code]*/
2798
2799static PyObject *
2800_curses_getsyx_impl(PyObject *module)
2801/*[clinic end generated code: output=c8e6c3f42349a038 input=9e1f862f3b4f7cba]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002802{
Victor Stinner26486ea2010-05-15 22:23:53 +00002803 int x = 0;
2804 int y = 0;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002805
Victor Stinner26486ea2010-05-15 22:23:53 +00002806 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002807
Victor Stinner26486ea2010-05-15 22:23:53 +00002808 getsyx(y, x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002809
Victor Stinner26486ea2010-05-15 22:23:53 +00002810 return Py_BuildValue("(ii)", y, x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002811}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002812#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002813
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002814#ifdef NCURSES_MOUSE_VERSION
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002815/*[clinic input]
2816_curses.getmouse
2817
2818Retrieve the queued mouse event.
2819
2820After getch() returns KEY_MOUSE to signal a mouse event, this function
2821returns a 5-tuple (id, x, y, z, bstate).
2822[clinic start generated code]*/
2823
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002824static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002825_curses_getmouse_impl(PyObject *module)
2826/*[clinic end generated code: output=ccf4242546b9cfa8 input=5b756ee6f5b481b1]*/
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002827{
Victor Stinner26486ea2010-05-15 22:23:53 +00002828 int rtn;
2829 MEVENT event;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002830
Victor Stinner26486ea2010-05-15 22:23:53 +00002831 PyCursesInitialised;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002832
Victor Stinner26486ea2010-05-15 22:23:53 +00002833 rtn = getmouse( &event );
2834 if (rtn == ERR) {
2835 PyErr_SetString(PyCursesError, "getmouse() returned ERR");
2836 return NULL;
2837 }
Serhiy Storchaka4f469c02017-11-01 20:48:49 +02002838 return Py_BuildValue("(hiiik)",
Victor Stinner26486ea2010-05-15 22:23:53 +00002839 (short)event.id,
Serhiy Storchaka4f469c02017-11-01 20:48:49 +02002840 (int)event.x, (int)event.y, (int)event.z,
2841 (unsigned long) event.bstate);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002842}
2843
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002844/*[clinic input]
2845_curses.ungetmouse
2846
2847 id: short
2848 x: int
2849 y: int
2850 z: int
2851 bstate: unsigned_long(bitwise=True)
2852 /
2853
2854Push a KEY_MOUSE event onto the input queue.
2855
2856The following getmouse() will return the given state data.
2857[clinic start generated code]*/
2858
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002859static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002860_curses_ungetmouse_impl(PyObject *module, short id, int x, int y, int z,
2861 unsigned long bstate)
2862/*[clinic end generated code: output=3430c9b0fc5c4341 input=fd650b2ca5a01e8f]*/
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002863{
Victor Stinner26486ea2010-05-15 22:23:53 +00002864 MEVENT event;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002865
Victor Stinner26486ea2010-05-15 22:23:53 +00002866 PyCursesInitialised;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002867
Serhiy Storchaka4f469c02017-11-01 20:48:49 +02002868 event.id = id;
2869 event.x = x;
2870 event.y = y;
2871 event.z = z;
2872 event.bstate = bstate;
Victor Stinner26486ea2010-05-15 22:23:53 +00002873 return PyCursesCheckERR(ungetmouse(&event), "ungetmouse");
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002874}
2875#endif
2876
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002877/*[clinic input]
2878_curses.getwin
2879
2880 file: object
2881 /
2882
2883Read window related data stored in the file by an earlier putwin() call.
2884
2885The routine then creates and initializes a new window using that data,
2886returning the new window object.
2887[clinic start generated code]*/
2888
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002889static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002890_curses_getwin(PyObject *module, PyObject *file)
2891/*[clinic end generated code: output=a79e0df3379af756 input=f713d2bba0e4c929]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002892{
Christian Heimes2b221b72017-03-02 11:09:01 +01002893 FILE *fp;
Victor Stinner26486ea2010-05-15 22:23:53 +00002894 PyObject *data;
2895 size_t datalen;
2896 WINDOW *win;
Benjamin Peterson18b71912013-05-16 15:29:44 -05002897 _Py_IDENTIFIER(read);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002898 PyObject *res = NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002899
Victor Stinner26486ea2010-05-15 22:23:53 +00002900 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002901
Christian Heimes2b221b72017-03-02 11:09:01 +01002902 fp = tmpfile();
2903 if (fp == NULL)
2904 return PyErr_SetFromErrno(PyExc_OSError);
2905
2906 if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02002907 goto error;
Christian Heimes2b221b72017-03-02 11:09:01 +01002908
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002909 data = _PyObject_CallMethodId(file, &PyId_read, NULL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002910 if (data == NULL)
2911 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00002912 if (!PyBytes_Check(data)) {
2913 PyErr_Format(PyExc_TypeError,
2914 "f.read() returned %.100s instead of bytes",
2915 data->ob_type->tp_name);
2916 Py_DECREF(data);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002917 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00002918 }
2919 datalen = PyBytes_GET_SIZE(data);
2920 if (fwrite(PyBytes_AS_STRING(data), 1, datalen, fp) != datalen) {
2921 Py_DECREF(data);
Christian Heimes2b221b72017-03-02 11:09:01 +01002922 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002923 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00002924 }
Guido van Rossum6c95da32007-07-24 00:16:38 +00002925 Py_DECREF(data);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002926
Victor Stinner26486ea2010-05-15 22:23:53 +00002927 fseek(fp, 0, 0);
2928 win = getwin(fp);
Victor Stinner26486ea2010-05-15 22:23:53 +00002929 if (win == NULL) {
2930 PyErr_SetString(PyCursesError, catchall_NULL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002931 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00002932 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02002933 res = PyCursesWindow_New(win, NULL);
2934
2935error:
Christian Heimes2b221b72017-03-02 11:09:01 +01002936 fclose(fp);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002937 return res;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002938}
2939
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002940/*[clinic input]
2941_curses.halfdelay
2942
2943 tenths: byte
2944 Maximal blocking delay in tenths of seconds (1 - 255).
2945 /
2946
2947Enter half-delay mode.
2948
2949Use nocbreak() to leave half-delay mode.
2950[clinic start generated code]*/
2951
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002952static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002953_curses_halfdelay_impl(PyObject *module, unsigned char tenths)
2954/*[clinic end generated code: output=e92cdf0ef33c0663 input=e42dce7259c15100]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002955{
Victor Stinner26486ea2010-05-15 22:23:53 +00002956 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002957
Victor Stinner26486ea2010-05-15 22:23:53 +00002958 return PyCursesCheckERR(halfdelay(tenths), "halfdelay");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002959}
2960
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002961/*[clinic input]
2962_curses.has_colors
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002963
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002964Return True if the terminal can display colors; otherwise, return False.
2965[clinic start generated code]*/
2966
2967static PyObject *
2968_curses_has_colors_impl(PyObject *module)
2969/*[clinic end generated code: output=db5667483139e3e2 input=b2ec41b739d896c6]*/
2970NoArgTrueFalseFunctionBody(has_colors)
2971
2972/*[clinic input]
2973_curses.has_ic
2974
2975Return True if the terminal has insert- and delete-character capabilities.
2976[clinic start generated code]*/
2977
2978static PyObject *
2979_curses_has_ic_impl(PyObject *module)
2980/*[clinic end generated code: output=6be24da9cb1268fe input=9bc2d3a797cc7324]*/
2981NoArgTrueFalseFunctionBody(has_ic)
2982
2983/*[clinic input]
2984_curses.has_il
2985
2986Return True if the terminal has insert- and delete-line capabilities.
2987[clinic start generated code]*/
2988
2989static PyObject *
2990_curses_has_il_impl(PyObject *module)
2991/*[clinic end generated code: output=d45bd7788ff9f5f4 input=cd939d5607ee5427]*/
2992NoArgTrueFalseFunctionBody(has_il)
2993
2994#ifdef HAVE_CURSES_HAS_KEY
2995/*[clinic input]
2996_curses.has_key
2997
2998 key: int
2999 Key number.
3000 /
3001
3002Return True if the current terminal type recognizes a key with that value.
3003[clinic start generated code]*/
3004
3005static PyObject *
3006_curses_has_key_impl(PyObject *module, int key)
3007/*[clinic end generated code: output=19ad48319414d0b1 input=78bd44acf1a4997c]*/
3008{
Victor Stinner26486ea2010-05-15 22:23:53 +00003009 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003010
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003011 return PyBool_FromLong(has_key(key));
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003012}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02003013#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003014
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003015/*[clinic input]
3016_curses.init_color
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003017
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003018 color_number: short
3019 The number of the color to be changed (0 - COLORS).
3020 r: short
3021 Red component (0 - 1000).
3022 g: short
3023 Green component (0 - 1000).
3024 b: short
3025 Blue component (0 - 1000).
3026 /
3027
3028Change the definition of a color.
3029
3030When init_color() is used, all occurrences of that color on the screen
3031immediately change to the new definition. This function is a no-op on
3032most terminals; it is active only if can_change_color() returns 1.
3033[clinic start generated code]*/
3034
3035static PyObject *
3036_curses_init_color_impl(PyObject *module, short color_number, short r,
3037 short g, short b)
3038/*[clinic end generated code: output=280236f5efe9776a input=f3a05bd38f619175]*/
3039{
Victor Stinner26486ea2010-05-15 22:23:53 +00003040 PyCursesInitialised;
3041 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003042
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003043 return PyCursesCheckERR(init_color(color_number, r, g, b), "init_color");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003044}
3045
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003046/*[clinic input]
3047_curses.init_pair
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003048
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003049 pair_number: short
3050 The number of the color-pair to be changed (1 - (COLOR_PAIRS-1)).
3051 fg: short
3052 Foreground color number (0 - COLORS).
3053 bg: short
3054 Background color number (0 - COLORS).
3055 /
3056
3057Change the definition of a color-pair.
3058
3059If the color-pair was previously initialized, the screen is refreshed and
3060all occurrences of that color-pair are changed to the new definition.
3061[clinic start generated code]*/
3062
3063static PyObject *
3064_curses_init_pair_impl(PyObject *module, short pair_number, short fg,
3065 short bg)
3066/*[clinic end generated code: output=9c2ce39c22f376b6 input=c9f0b11b17a2ac6d]*/
3067{
Victor Stinner26486ea2010-05-15 22:23:53 +00003068 PyCursesInitialised;
3069 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003070
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003071 return PyCursesCheckERR(init_pair(pair_number, fg, bg), "init_pair");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003072}
3073
Andrew M. Kuchling32552682000-12-22 21:52:27 +00003074static PyObject *ModDict;
3075
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003076/*[clinic input]
3077_curses.initscr
3078
3079Initialize the library.
3080
3081Return a WindowObject which represents the whole screen.
3082[clinic start generated code]*/
3083
Victor Stinner26486ea2010-05-15 22:23:53 +00003084static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003085_curses_initscr_impl(PyObject *module)
3086/*[clinic end generated code: output=619fb68443810b7b input=514f4bce1821f6b5]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00003087{
Victor Stinner26486ea2010-05-15 22:23:53 +00003088 WINDOW *win;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003089 PyCursesWindowObject *winobj;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003090
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003091 if (initialised) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003092 wrefresh(stdscr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003093 return (PyObject *)PyCursesWindow_New(stdscr, NULL);
Victor Stinner26486ea2010-05-15 22:23:53 +00003094 }
Guido van Rossum56bf2351994-08-31 22:06:24 +00003095
Victor Stinner26486ea2010-05-15 22:23:53 +00003096 win = initscr();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003097
Victor Stinner26486ea2010-05-15 22:23:53 +00003098 if (win == NULL) {
3099 PyErr_SetString(PyCursesError, catchall_NULL);
3100 return NULL;
3101 }
Guido van Rossum85738471995-02-17 13:50:17 +00003102
Victor Stinner26486ea2010-05-15 22:23:53 +00003103 initialised = initialised_setupterm = TRUE;
Guido van Rossum56bf2351994-08-31 22:06:24 +00003104
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00003105/* This was moved from initcurses() because it core dumped on SGI,
3106 where they're not defined until you've called initscr() */
Victor Stinner26486ea2010-05-15 22:23:53 +00003107#define SetDictInt(string,ch) \
3108 do { \
3109 PyObject *o = PyLong_FromLong((long) (ch)); \
3110 if (o && PyDict_SetItemString(ModDict, string, o) == 0) { \
3111 Py_DECREF(o); \
3112 } \
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003113 } while (0)
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00003114
Victor Stinner26486ea2010-05-15 22:23:53 +00003115 /* Here are some graphic symbols you can use */
3116 SetDictInt("ACS_ULCORNER", (ACS_ULCORNER));
3117 SetDictInt("ACS_LLCORNER", (ACS_LLCORNER));
3118 SetDictInt("ACS_URCORNER", (ACS_URCORNER));
3119 SetDictInt("ACS_LRCORNER", (ACS_LRCORNER));
3120 SetDictInt("ACS_LTEE", (ACS_LTEE));
3121 SetDictInt("ACS_RTEE", (ACS_RTEE));
3122 SetDictInt("ACS_BTEE", (ACS_BTEE));
3123 SetDictInt("ACS_TTEE", (ACS_TTEE));
3124 SetDictInt("ACS_HLINE", (ACS_HLINE));
3125 SetDictInt("ACS_VLINE", (ACS_VLINE));
3126 SetDictInt("ACS_PLUS", (ACS_PLUS));
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00003127#if !defined(__hpux) || defined(HAVE_NCURSES_H)
Victor Stinner26486ea2010-05-15 22:23:53 +00003128 /* On HP/UX 11, these are of type cchar_t, which is not an
3129 integral type. If this is a problem on more platforms, a
3130 configure test should be added to determine whether ACS_S1
3131 is of integral type. */
3132 SetDictInt("ACS_S1", (ACS_S1));
3133 SetDictInt("ACS_S9", (ACS_S9));
3134 SetDictInt("ACS_DIAMOND", (ACS_DIAMOND));
3135 SetDictInt("ACS_CKBOARD", (ACS_CKBOARD));
3136 SetDictInt("ACS_DEGREE", (ACS_DEGREE));
3137 SetDictInt("ACS_PLMINUS", (ACS_PLMINUS));
3138 SetDictInt("ACS_BULLET", (ACS_BULLET));
3139 SetDictInt("ACS_LARROW", (ACS_LARROW));
3140 SetDictInt("ACS_RARROW", (ACS_RARROW));
3141 SetDictInt("ACS_DARROW", (ACS_DARROW));
3142 SetDictInt("ACS_UARROW", (ACS_UARROW));
3143 SetDictInt("ACS_BOARD", (ACS_BOARD));
3144 SetDictInt("ACS_LANTERN", (ACS_LANTERN));
3145 SetDictInt("ACS_BLOCK", (ACS_BLOCK));
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00003146#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003147 SetDictInt("ACS_BSSB", (ACS_ULCORNER));
3148 SetDictInt("ACS_SSBB", (ACS_LLCORNER));
3149 SetDictInt("ACS_BBSS", (ACS_URCORNER));
3150 SetDictInt("ACS_SBBS", (ACS_LRCORNER));
3151 SetDictInt("ACS_SBSS", (ACS_RTEE));
3152 SetDictInt("ACS_SSSB", (ACS_LTEE));
3153 SetDictInt("ACS_SSBS", (ACS_BTEE));
3154 SetDictInt("ACS_BSSS", (ACS_TTEE));
3155 SetDictInt("ACS_BSBS", (ACS_HLINE));
3156 SetDictInt("ACS_SBSB", (ACS_VLINE));
3157 SetDictInt("ACS_SSSS", (ACS_PLUS));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003158
Victor Stinner26486ea2010-05-15 22:23:53 +00003159 /* The following are never available with strict SYSV curses */
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003160#ifdef ACS_S3
Victor Stinner26486ea2010-05-15 22:23:53 +00003161 SetDictInt("ACS_S3", (ACS_S3));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003162#endif
3163#ifdef ACS_S7
Victor Stinner26486ea2010-05-15 22:23:53 +00003164 SetDictInt("ACS_S7", (ACS_S7));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003165#endif
3166#ifdef ACS_LEQUAL
Victor Stinner26486ea2010-05-15 22:23:53 +00003167 SetDictInt("ACS_LEQUAL", (ACS_LEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003168#endif
3169#ifdef ACS_GEQUAL
Victor Stinner26486ea2010-05-15 22:23:53 +00003170 SetDictInt("ACS_GEQUAL", (ACS_GEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003171#endif
3172#ifdef ACS_PI
Victor Stinner26486ea2010-05-15 22:23:53 +00003173 SetDictInt("ACS_PI", (ACS_PI));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003174#endif
3175#ifdef ACS_NEQUAL
Victor Stinner26486ea2010-05-15 22:23:53 +00003176 SetDictInt("ACS_NEQUAL", (ACS_NEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003177#endif
3178#ifdef ACS_STERLING
Victor Stinner26486ea2010-05-15 22:23:53 +00003179 SetDictInt("ACS_STERLING", (ACS_STERLING));
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00003180#endif
3181
Victor Stinner26486ea2010-05-15 22:23:53 +00003182 SetDictInt("LINES", LINES);
3183 SetDictInt("COLS", COLS);
Guido van Rossum56bf2351994-08-31 22:06:24 +00003184
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003185 winobj = (PyCursesWindowObject *)PyCursesWindow_New(win, NULL);
3186 screen_encoding = winobj->encoding;
3187 return (PyObject *)winobj;
Guido van Rossumf6971e21994-08-30 12:25:20 +00003188}
3189
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003190/*[clinic input]
3191_curses.setupterm
3192
3193 term: str(accept={str, NoneType}) = NULL
3194 Terminal name.
3195 If omitted, the value of the TERM environment variable will be used.
3196 fd: int = -1
3197 File descriptor to which any initialization sequences will be sent.
3198 If not supplied, the file descriptor for sys.stdout will be used.
3199
3200Initialize the terminal.
3201[clinic start generated code]*/
3202
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003203static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003204_curses_setupterm_impl(PyObject *module, const char *term, int fd)
3205/*[clinic end generated code: output=4584e587350f2848 input=8ac5f78ec6268be3]*/
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003206{
Victor Stinner26486ea2010-05-15 22:23:53 +00003207 int err;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003208
Victor Stinner26486ea2010-05-15 22:23:53 +00003209 if (fd == -1) {
3210 PyObject* sys_stdout;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003211
Victor Stinnerbd303c12013-11-07 23:07:29 +01003212 sys_stdout = PySys_GetObject("stdout");
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003213
Victor Stinner26486ea2010-05-15 22:23:53 +00003214 if (sys_stdout == NULL || sys_stdout == Py_None) {
3215 PyErr_SetString(
3216 PyCursesError,
3217 "lost sys.stdout");
3218 return NULL;
3219 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003220
Victor Stinner26486ea2010-05-15 22:23:53 +00003221 fd = PyObject_AsFileDescriptor(sys_stdout);
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003222
Victor Stinner26486ea2010-05-15 22:23:53 +00003223 if (fd == -1) {
3224 return NULL;
3225 }
3226 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003227
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003228 if (!initialised_setupterm && setupterm((char *)term, fd, &err) == ERR) {
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02003229 const char* s = "setupterm: unknown error";
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003230
Victor Stinner26486ea2010-05-15 22:23:53 +00003231 if (err == 0) {
3232 s = "setupterm: could not find terminal";
3233 } else if (err == -1) {
3234 s = "setupterm: could not find terminfo database";
3235 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003236
Victor Stinner26486ea2010-05-15 22:23:53 +00003237 PyErr_SetString(PyCursesError,s);
3238 return NULL;
3239 }
3240
3241 initialised_setupterm = TRUE;
3242
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003243 Py_RETURN_NONE;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003244}
Guido van Rossumf6971e21994-08-30 12:25:20 +00003245
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003246/*[clinic input]
3247_curses.intrflush
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003248
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003249 flag: bool(accept={int})
3250 /
3251
3252[clinic start generated code]*/
3253
3254static PyObject *
3255_curses_intrflush_impl(PyObject *module, int flag)
3256/*[clinic end generated code: output=c1986df35e999a0f input=fcba57bb28dfd795]*/
3257{
Victor Stinner26486ea2010-05-15 22:23:53 +00003258 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003259
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003260 return PyCursesCheckERR(intrflush(NULL, flag), "intrflush");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003261}
3262
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003263/*[clinic input]
3264_curses.isendwin
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003265
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003266Return True if endwin() has been called.
3267[clinic start generated code]*/
3268
3269static PyObject *
3270_curses_isendwin_impl(PyObject *module)
3271/*[clinic end generated code: output=d73179e4a7e1eb8c input=6cdb01a7ebf71397]*/
3272NoArgTrueFalseFunctionBody(isendwin)
3273
3274#ifdef HAVE_CURSES_IS_TERM_RESIZED
3275/*[clinic input]
3276_curses.is_term_resized
3277
3278 nlines: int
3279 Height.
3280 ncols: int
3281 Width.
3282 /
3283
3284Return True if resize_term() would modify the window structure, False otherwise.
3285[clinic start generated code]*/
3286
3287static PyObject *
3288_curses_is_term_resized_impl(PyObject *module, int nlines, int ncols)
3289/*[clinic end generated code: output=aafe04afe50f1288 input=ca9c0bd0fb8ab444]*/
3290{
Victor Stinner26486ea2010-05-15 22:23:53 +00003291 PyCursesInitialised;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003292
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003293 return PyBool_FromLong(is_term_resized(nlines, ncols));
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003294}
3295#endif /* HAVE_CURSES_IS_TERM_RESIZED */
3296
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003297/*[clinic input]
3298_curses.keyname
3299
3300 key: int
3301 Key number.
3302 /
3303
3304Return the name of specified key.
3305[clinic start generated code]*/
3306
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003307static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003308_curses_keyname_impl(PyObject *module, int key)
3309/*[clinic end generated code: output=fa2675ab3f4e056b input=ee4b1d0f243a2a2b]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003310{
Victor Stinner26486ea2010-05-15 22:23:53 +00003311 const char *knp;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003312
Victor Stinner26486ea2010-05-15 22:23:53 +00003313 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003314
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003315 if (key < 0) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003316 PyErr_SetString(PyExc_ValueError, "invalid key number");
3317 return NULL;
3318 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003319 knp = keyname(key);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003320
Serhiy Storchaka20b39b22014-09-28 11:27:24 +03003321 return PyBytes_FromString((knp == NULL) ? "" : knp);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003322}
3323
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003324/*[clinic input]
3325_curses.killchar
3326
3327Return the user's current line kill character.
3328[clinic start generated code]*/
3329
Victor Stinner26486ea2010-05-15 22:23:53 +00003330static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003331_curses_killchar_impl(PyObject *module)
3332/*[clinic end generated code: output=31c3a45b2c528269 input=1ff171c38df5ccad]*/
Victor Stinner26486ea2010-05-15 22:23:53 +00003333{
3334 char ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003335
Victor Stinner26486ea2010-05-15 22:23:53 +00003336 ch = killchar();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003337
Victor Stinner26486ea2010-05-15 22:23:53 +00003338 return PyBytes_FromStringAndSize(&ch, 1);
3339}
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003340
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003341/*[clinic input]
3342_curses.longname
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003343
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003344Return the terminfo long name field describing the current terminal.
3345
3346The maximum length of a verbose description is 128 characters. It is defined
3347only after the call to initscr().
3348[clinic start generated code]*/
3349
3350static PyObject *
3351_curses_longname_impl(PyObject *module)
3352/*[clinic end generated code: output=fdf30433727ef568 input=84c3f20201b1098e]*/
3353NoArgReturnStringFunctionBody(longname)
3354
3355/*[clinic input]
3356_curses.meta
3357
3358 yes: bool(accept={int})
3359 /
3360
3361Enable/disable meta keys.
3362
3363If yes is True, allow 8-bit characters to be input. If yes is False,
3364allow only 7-bit characters.
3365[clinic start generated code]*/
3366
3367static PyObject *
3368_curses_meta_impl(PyObject *module, int yes)
3369/*[clinic end generated code: output=22f5abda46a605d8 input=af9892e3a74f35db]*/
3370{
Victor Stinner26486ea2010-05-15 22:23:53 +00003371 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003372
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003373 return PyCursesCheckERR(meta(stdscr, yes), "meta");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003374}
3375
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003376#ifdef NCURSES_MOUSE_VERSION
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003377/*[clinic input]
3378_curses.mouseinterval
3379
3380 interval: int
3381 Time in milliseconds.
3382 /
3383
3384Set and retrieve the maximum time between press and release in a click.
3385
3386Set the maximum time that can elapse between press and release events in
3387order for them to be recognized as a click, and return the previous interval
3388value.
3389[clinic start generated code]*/
3390
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003391static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003392_curses_mouseinterval_impl(PyObject *module, int interval)
3393/*[clinic end generated code: output=c4f5ff04354634c5 input=75aaa3f0db10ac4e]*/
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003394{
Victor Stinner26486ea2010-05-15 22:23:53 +00003395 PyCursesInitialised;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003396
Victor Stinner26486ea2010-05-15 22:23:53 +00003397 return PyCursesCheckERR(mouseinterval(interval), "mouseinterval");
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003398}
3399
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003400/*[clinic input]
3401_curses.mousemask
3402
3403 newmask: unsigned_long(bitwise=True)
3404 /
3405
3406Set the mouse events to be reported, and return a tuple (availmask, oldmask).
3407
3408Return a tuple (availmask, oldmask). availmask indicates which of the
3409specified mouse events can be reported; on complete failure it returns 0.
3410oldmask is the previous value of the given window's mouse event mask.
3411If this function is never called, no mouse events are ever reported.
3412[clinic start generated code]*/
3413
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003414static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003415_curses_mousemask_impl(PyObject *module, unsigned long newmask)
3416/*[clinic end generated code: output=9406cf1b8a36e485 input=bdf76b7568a3c541]*/
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003417{
Victor Stinner26486ea2010-05-15 22:23:53 +00003418 mmask_t oldmask, availmask;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003419
Victor Stinner26486ea2010-05-15 22:23:53 +00003420 PyCursesInitialised;
Serhiy Storchaka4f469c02017-11-01 20:48:49 +02003421 availmask = mousemask((mmask_t)newmask, &oldmask);
3422 return Py_BuildValue("(kk)",
3423 (unsigned long)availmask, (unsigned long)oldmask);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003424}
3425#endif
3426
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003427/*[clinic input]
3428_curses.napms
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +00003429
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003430 ms: int
3431 Duration in milliseconds.
3432 /
3433
3434Sleep for specified time.
3435[clinic start generated code]*/
3436
3437static PyObject *
3438_curses_napms_impl(PyObject *module, int ms)
3439/*[clinic end generated code: output=a40a1da2e39ea438 input=20cd3af2b6900f56]*/
3440{
Andrew M. Kuchling2b146762010-05-10 23:30:25 +00003441 PyCursesInitialised;
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +00003442
3443 return Py_BuildValue("i", napms(ms));
3444}
3445
3446
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003447/*[clinic input]
3448_curses.newpad
3449
3450 nlines: int
3451 Height.
3452 ncols: int
3453 Width.
3454 /
3455
3456Create and return a pointer to a new pad data structure.
3457[clinic start generated code]*/
3458
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +00003459static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003460_curses_newpad_impl(PyObject *module, int nlines, int ncols)
3461/*[clinic end generated code: output=de52a56eb1098ec9 input=93f1272f240d8894]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003462{
Victor Stinner26486ea2010-05-15 22:23:53 +00003463 WINDOW *win;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003464
Victor Stinner26486ea2010-05-15 22:23:53 +00003465 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003466
Victor Stinner26486ea2010-05-15 22:23:53 +00003467 win = newpad(nlines, ncols);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003468
Victor Stinner26486ea2010-05-15 22:23:53 +00003469 if (win == NULL) {
3470 PyErr_SetString(PyCursesError, catchall_NULL);
3471 return NULL;
3472 }
3473
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003474 return (PyObject *)PyCursesWindow_New(win, NULL);
Guido van Rossumf6971e21994-08-30 12:25:20 +00003475}
3476
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003477/*[clinic input]
3478_curses.newwin
3479
3480 nlines: int
3481 Height.
3482 ncols: int
3483 Width.
3484 [
3485 begin_y: int = 0
3486 Top side y-coordinate.
3487 begin_x: int = 0
3488 Left side x-coordinate.
3489 ]
3490 /
3491
3492Return a new window.
3493
3494By default, the window will extend from the specified position to the lower
3495right corner of the screen.
3496[clinic start generated code]*/
3497
Guido van Rossumf6971e21994-08-30 12:25:20 +00003498static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003499_curses_newwin_impl(PyObject *module, int nlines, int ncols,
3500 int group_right_1, int begin_y, int begin_x)
3501/*[clinic end generated code: output=c1e0a8dc8ac2826c input=29312c15a72a003d]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00003502{
Victor Stinner26486ea2010-05-15 22:23:53 +00003503 WINDOW *win;
Guido van Rossum85738471995-02-17 13:50:17 +00003504
Victor Stinner26486ea2010-05-15 22:23:53 +00003505 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003506
Victor Stinner26486ea2010-05-15 22:23:53 +00003507 win = newwin(nlines,ncols,begin_y,begin_x);
3508 if (win == NULL) {
3509 PyErr_SetString(PyCursesError, catchall_NULL);
3510 return NULL;
3511 }
Guido van Rossum85738471995-02-17 13:50:17 +00003512
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003513 return (PyObject *)PyCursesWindow_New(win, NULL);
Guido van Rossumf6971e21994-08-30 12:25:20 +00003514}
3515
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003516/*[clinic input]
3517_curses.nl
3518
3519 flag: bool(accept={int}) = True
3520 If false, the effect is the same as calling nonl().
3521 /
3522
3523Enter newline mode.
3524
3525This mode translates the return key into newline on input, and translates
3526newline into return and line-feed on output. Newline mode is initially on.
3527[clinic start generated code]*/
3528
Guido van Rossumf6971e21994-08-30 12:25:20 +00003529static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003530_curses_nl_impl(PyObject *module, int flag)
3531/*[clinic end generated code: output=b39cc0ffc9015003 input=cf36a63f7b86e28a]*/
3532NoArgOrFlagNoReturnFunctionBody(nl, flag)
3533
3534/*[clinic input]
3535_curses.nocbreak
3536
3537Leave cbreak mode.
3538
3539Return to normal "cooked" mode with line buffering.
3540[clinic start generated code]*/
3541
3542static PyObject *
3543_curses_nocbreak_impl(PyObject *module)
3544/*[clinic end generated code: output=eabf3833a4fbf620 input=e4b65f7d734af400]*/
3545NoArgNoReturnFunctionBody(nocbreak)
3546
3547/*[clinic input]
3548_curses.noecho
3549
3550Leave echo mode.
3551
3552Echoing of input characters is turned off.
3553[clinic start generated code]*/
3554
3555static PyObject *
3556_curses_noecho_impl(PyObject *module)
3557/*[clinic end generated code: output=cc95ab45bc98f41b input=76714df529e614c3]*/
3558NoArgNoReturnFunctionBody(noecho)
3559
3560/*[clinic input]
3561_curses.nonl
3562
3563Leave newline mode.
3564
3565Disable translation of return into newline on input, and disable low-level
3566translation of newline into newline/return on output.
3567[clinic start generated code]*/
3568
3569static PyObject *
3570_curses_nonl_impl(PyObject *module)
3571/*[clinic end generated code: output=99e917e9715770c6 input=9d37dd122d3022fc]*/
3572NoArgNoReturnFunctionBody(nonl)
3573
3574/*[clinic input]
3575_curses.noqiflush
3576
3577Disable queue flushing.
3578
3579When queue flushing is disabled, normal flush of input and output queues
3580associated with the INTR, QUIT and SUSP characters will not be done.
3581[clinic start generated code]*/
3582
3583static PyObject *
3584_curses_noqiflush_impl(PyObject *module)
3585/*[clinic end generated code: output=8b95a4229bbf0877 input=ba3e6b2e3e54c4df]*/
3586NoArgNoReturnVoidFunctionBody(noqiflush)
3587
3588/*[clinic input]
3589_curses.noraw
3590
3591Leave raw mode.
3592
3593Return to normal "cooked" mode with line buffering.
3594[clinic start generated code]*/
3595
3596static PyObject *
3597_curses_noraw_impl(PyObject *module)
3598/*[clinic end generated code: output=39894e5524c430cc input=6ec86692096dffb5]*/
3599NoArgNoReturnFunctionBody(noraw)
3600
3601/*[clinic input]
3602_curses.pair_content
3603
3604 pair_number: short
3605 The number of the color pair (1 - (COLOR_PAIRS-1)).
3606 /
3607
3608Return a tuple (fg, bg) containing the colors for the requested color pair.
3609[clinic start generated code]*/
3610
3611static PyObject *
3612_curses_pair_content_impl(PyObject *module, short pair_number)
3613/*[clinic end generated code: output=5a72aa1a28bbacf3 input=f4d7fec5643b976b]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00003614{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003615 short f, b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003616
Victor Stinner26486ea2010-05-15 22:23:53 +00003617 PyCursesInitialised;
3618 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003619
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003620 if (pair_content(pair_number, &f, &b)==ERR) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003621 PyErr_SetString(PyCursesError,
3622 "Argument 1 was out of range. (1..COLOR_PAIRS-1)");
3623 return NULL;
3624 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003625
Victor Stinner26486ea2010-05-15 22:23:53 +00003626 return Py_BuildValue("(ii)", f, b);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003627}
3628
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003629/*[clinic input]
3630_curses.pair_number
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003631
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003632 attr: int
3633 /
3634
3635Return the number of the color-pair set by the specified attribute value.
3636
3637color_pair() is the counterpart to this function.
3638[clinic start generated code]*/
3639
3640static PyObject *
3641_curses_pair_number_impl(PyObject *module, int attr)
3642/*[clinic end generated code: output=85bce7d65c0aa3f4 input=d478548e33f5e61a]*/
3643{
Victor Stinner26486ea2010-05-15 22:23:53 +00003644 PyCursesInitialised;
3645 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003646
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003647 return PyLong_FromLong((long) ((attr & A_COLOR) >> 8));
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003648}
3649
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003650/*[clinic input]
3651_curses.putp
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003652
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003653 string: str(accept={robuffer})
3654 /
3655
3656Emit the value of a specified terminfo capability for the current terminal.
3657
3658Note that the output of putp() always goes to standard output.
3659[clinic start generated code]*/
3660
3661static PyObject *
3662_curses_putp_impl(PyObject *module, const char *string)
3663/*[clinic end generated code: output=e98081d1b8eb5816 input=1601faa828b44cb3]*/
3664{
3665 return PyCursesCheckERR(putp(string), "putp");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003666}
3667
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003668/*[clinic input]
3669_curses.qiflush
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003670
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003671 flag: bool(accept={int}) = True
3672 If false, the effect is the same as calling noqiflush().
3673 /
3674
3675Enable queue flushing.
3676
3677If queue flushing is enabled, all output in the display driver queue
3678will be flushed when the INTR, QUIT and SUSP characters are read.
3679[clinic start generated code]*/
3680
3681static PyObject *
3682_curses_qiflush_impl(PyObject *module, int flag)
3683/*[clinic end generated code: output=9167e862f760ea30 input=e9e4a389946a0dbc]*/
3684{
Victor Stinner26486ea2010-05-15 22:23:53 +00003685 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003686
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003687 if (flag) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003688 qiflush();
Victor Stinner26486ea2010-05-15 22:23:53 +00003689 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003690 else {
3691 noqiflush();
3692 }
3693 Py_RETURN_NONE;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003694}
3695
Guido van Rossumd8faa362007-04-27 19:54:29 +00003696/* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES
3697 * and _curses.COLS */
Benjamin Petersonfea6a942008-07-02 16:11:42 +00003698#if defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM)
Guido van Rossumd8faa362007-04-27 19:54:29 +00003699static int
3700update_lines_cols(void)
3701{
Victor Stinner26486ea2010-05-15 22:23:53 +00003702 PyObject *o;
3703 PyObject *m = PyImport_ImportModuleNoBlock("curses");
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003704 _Py_IDENTIFIER(LINES);
3705 _Py_IDENTIFIER(COLS);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003706
Victor Stinner26486ea2010-05-15 22:23:53 +00003707 if (!m)
3708 return 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003709
Victor Stinner26486ea2010-05-15 22:23:53 +00003710 o = PyLong_FromLong(LINES);
3711 if (!o) {
3712 Py_DECREF(m);
3713 return 0;
3714 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003715 if (_PyObject_SetAttrId(m, &PyId_LINES, o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003716 Py_DECREF(m);
3717 Py_DECREF(o);
3718 return 0;
3719 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003720 /* PyId_LINES.object will be initialized here. */
3721 if (PyDict_SetItem(ModDict, PyId_LINES.object, o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003722 Py_DECREF(m);
3723 Py_DECREF(o);
3724 return 0;
3725 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003726 Py_DECREF(o);
Victor Stinner26486ea2010-05-15 22:23:53 +00003727 o = PyLong_FromLong(COLS);
3728 if (!o) {
3729 Py_DECREF(m);
3730 return 0;
3731 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003732 if (_PyObject_SetAttrId(m, &PyId_COLS, o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003733 Py_DECREF(m);
3734 Py_DECREF(o);
3735 return 0;
3736 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003737 if (PyDict_SetItem(ModDict, PyId_COLS.object, o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003738 Py_DECREF(m);
3739 Py_DECREF(o);
3740 return 0;
3741 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003742 Py_DECREF(o);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003743 Py_DECREF(m);
Victor Stinner26486ea2010-05-15 22:23:53 +00003744 return 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003745}
Steve Dowerd2bc3892015-04-15 18:06:05 -04003746
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003747/*[clinic input]
3748_curses.update_lines_cols -> int
3749
3750[clinic start generated code]*/
3751
3752static int
3753_curses_update_lines_cols_impl(PyObject *module)
3754/*[clinic end generated code: output=0345e7f072ea711a input=3a87760f7d5197f0]*/
Steve Dowerd2bc3892015-04-15 18:06:05 -04003755{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003756 return update_lines_cols();
Steve Dowerd2bc3892015-04-15 18:06:05 -04003757}
3758
Benjamin Petersonfea6a942008-07-02 16:11:42 +00003759#endif
Guido van Rossumd8faa362007-04-27 19:54:29 +00003760
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003761/*[clinic input]
3762_curses.raw
3763
3764 flag: bool(accept={int}) = True
3765 If false, the effect is the same as calling noraw().
3766 /
3767
3768Enter raw mode.
3769
3770In raw mode, normal line buffering and processing of interrupt, quit,
3771suspend, and flow control keys are turned off; characters are presented to
3772curses input functions one by one.
3773[clinic start generated code]*/
3774
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003775static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003776_curses_raw_impl(PyObject *module, int flag)
3777/*[clinic end generated code: output=a750e4b342be015b input=e36d8db27832b848]*/
3778NoArgOrFlagNoReturnFunctionBody(raw, flag)
3779
3780/*[clinic input]
3781_curses.reset_prog_mode
3782
3783Restore the terminal to "program" mode, as previously saved by def_prog_mode().
3784[clinic start generated code]*/
3785
3786static PyObject *
3787_curses_reset_prog_mode_impl(PyObject *module)
3788/*[clinic end generated code: output=15eb765abf0b6575 input=3d82bea2b3243471]*/
3789NoArgNoReturnFunctionBody(reset_prog_mode)
3790
3791/*[clinic input]
3792_curses.reset_shell_mode
3793
3794Restore the terminal to "shell" mode, as previously saved by def_shell_mode().
3795[clinic start generated code]*/
3796
3797static PyObject *
3798_curses_reset_shell_mode_impl(PyObject *module)
3799/*[clinic end generated code: output=0238de2962090d33 input=1c738fa64bd1a24f]*/
3800NoArgNoReturnFunctionBody(reset_shell_mode)
3801
3802/*[clinic input]
3803_curses.resetty
3804
3805Restore terminal mode.
3806[clinic start generated code]*/
3807
3808static PyObject *
3809_curses_resetty_impl(PyObject *module)
3810/*[clinic end generated code: output=ff4b448e80a7cd63 input=940493de03624bb0]*/
3811NoArgNoReturnFunctionBody(resetty)
3812
3813#ifdef HAVE_CURSES_RESIZETERM
3814/*[clinic input]
3815_curses.resizeterm
3816
3817 nlines: int
3818 Height.
3819 ncols: int
3820 Width.
3821 /
3822
3823Resize the standard and current windows to the specified dimensions.
3824
3825Adjusts other bookkeeping data used by the curses library that record the
3826window dimensions (in particular the SIGWINCH handler).
3827[clinic start generated code]*/
3828
3829static PyObject *
3830_curses_resizeterm_impl(PyObject *module, int nlines, int ncols)
3831/*[clinic end generated code: output=56d6bcc5194ad055 input=0fca02ebad5ffa82]*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003832{
Victor Stinner26486ea2010-05-15 22:23:53 +00003833 PyObject *result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003834
Victor Stinner26486ea2010-05-15 22:23:53 +00003835 PyCursesInitialised;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003836
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003837 result = PyCursesCheckERR(resizeterm(nlines, ncols), "resizeterm");
Victor Stinner26486ea2010-05-15 22:23:53 +00003838 if (!result)
3839 return NULL;
3840 if (!update_lines_cols())
3841 return NULL;
3842 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003843}
3844
3845#endif
3846
3847#ifdef HAVE_CURSES_RESIZE_TERM
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003848/*[clinic input]
3849_curses.resize_term
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003850
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003851 nlines: int
3852 Height.
3853 ncols: int
3854 Width.
3855 /
3856
3857Backend function used by resizeterm(), performing most of the work.
3858
3859When resizing the windows, resize_term() blank-fills the areas that are
3860extended. The calling application should fill in these areas with appropriate
3861data. The resize_term() function attempts to resize all windows. However,
3862due to the calling convention of pads, it is not possible to resize these
3863without additional interaction with the application.
3864[clinic start generated code]*/
3865
3866static PyObject *
3867_curses_resize_term_impl(PyObject *module, int nlines, int ncols)
3868/*[clinic end generated code: output=9e26d8b9ea311ed2 input=2197edd05b049ed4]*/
3869{
Victor Stinner26486ea2010-05-15 22:23:53 +00003870 PyObject *result;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003871
Victor Stinner26486ea2010-05-15 22:23:53 +00003872 PyCursesInitialised;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003873
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003874 result = PyCursesCheckERR(resize_term(nlines, ncols), "resize_term");
Victor Stinner26486ea2010-05-15 22:23:53 +00003875 if (!result)
3876 return NULL;
3877 if (!update_lines_cols())
3878 return NULL;
3879 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003880}
3881#endif /* HAVE_CURSES_RESIZE_TERM */
3882
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003883/*[clinic input]
3884_curses.savetty
3885
3886Save terminal mode.
3887[clinic start generated code]*/
3888
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003889static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003890_curses_savetty_impl(PyObject *module)
3891/*[clinic end generated code: output=6babc49f12b42199 input=fce6b2b7d2200102]*/
3892NoArgNoReturnFunctionBody(savetty)
3893
3894#ifdef getsyx
3895/*[clinic input]
3896_curses.setsyx
3897
3898 y: int
3899 Y-coordinate.
3900 x: int
3901 X-coordinate.
3902 /
3903
3904Set the virtual screen cursor.
3905
3906If y and x are both -1, then leaveok is set.
3907[clinic start generated code]*/
3908
3909static PyObject *
3910_curses_setsyx_impl(PyObject *module, int y, int x)
3911/*[clinic end generated code: output=23dcf753511a2464 input=fa7f2b208e10a557]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003912{
Victor Stinner26486ea2010-05-15 22:23:53 +00003913 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003914
Victor Stinner26486ea2010-05-15 22:23:53 +00003915 setsyx(y,x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003916
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003917 Py_RETURN_NONE;
Guido van Rossumf6971e21994-08-30 12:25:20 +00003918}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02003919#endif
Guido van Rossumf6971e21994-08-30 12:25:20 +00003920
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003921/*[clinic input]
3922_curses.start_color
3923
3924Initializes eight basic colors and global variables COLORS and COLOR_PAIRS.
3925
3926Must be called if the programmer wants to use colors, and before any other
3927color manipulation routine is called. It is good practice to call this
3928routine right after initscr().
3929
3930It also restores the colors on the terminal to the values they had when the
3931terminal was just turned on.
3932[clinic start generated code]*/
3933
Guido van Rossumf6971e21994-08-30 12:25:20 +00003934static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003935_curses_start_color_impl(PyObject *module)
3936/*[clinic end generated code: output=8b772b41d8090ede input=0ca0ecb2b77e1a12]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00003937{
Victor Stinner26486ea2010-05-15 22:23:53 +00003938 int code;
3939 PyObject *c, *cp;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003940
Victor Stinner26486ea2010-05-15 22:23:53 +00003941 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003942
Victor Stinner26486ea2010-05-15 22:23:53 +00003943 code = start_color();
3944 if (code != ERR) {
3945 initialisedcolors = TRUE;
3946 c = PyLong_FromLong((long) COLORS);
Christian Heimesa956e642013-07-26 14:45:37 +02003947 if (c == NULL)
3948 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00003949 PyDict_SetItemString(ModDict, "COLORS", c);
3950 Py_DECREF(c);
3951 cp = PyLong_FromLong((long) COLOR_PAIRS);
Christian Heimesa956e642013-07-26 14:45:37 +02003952 if (cp == NULL)
3953 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00003954 PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp);
3955 Py_DECREF(cp);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003956 Py_RETURN_NONE;
Victor Stinner26486ea2010-05-15 22:23:53 +00003957 } else {
3958 PyErr_SetString(PyCursesError, "start_color() returned ERR");
3959 return NULL;
3960 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003961}
3962
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003963/*[clinic input]
3964_curses.termattrs
3965
3966Return a logical OR of all video attributes supported by the terminal.
3967[clinic start generated code]*/
3968
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003969static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003970_curses_termattrs_impl(PyObject *module)
3971/*[clinic end generated code: output=b06f437fce1b6fc4 input=0559882a04f84d1d]*/
3972NoArgReturnIntFunctionBody(termattrs)
3973
3974/*[clinic input]
3975_curses.termname
3976
3977Return the value of the environment variable TERM, truncated to 14 characters.
3978[clinic start generated code]*/
3979
3980static PyObject *
3981_curses_termname_impl(PyObject *module)
3982/*[clinic end generated code: output=96375577ebbd67fd input=33c08d000944f33f]*/
3983NoArgReturnStringFunctionBody(termname)
3984
3985/*[clinic input]
3986_curses.tigetflag
3987
3988 capname: str
3989 The terminfo capability name.
3990 /
3991
3992Return the value of the Boolean capability.
3993
3994The value -1 is returned if capname is not a Boolean capability, or 0 if
3995it is canceled or absent from the terminal description.
3996[clinic start generated code]*/
3997
3998static PyObject *
3999_curses_tigetflag_impl(PyObject *module, const char *capname)
4000/*[clinic end generated code: output=8853c0e55542195b input=b0787af9e3e9a6ce]*/
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004001{
Victor Stinner26486ea2010-05-15 22:23:53 +00004002 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004003
Xiang Zhang44742e92018-06-23 12:29:30 +08004004 return PyLong_FromLong( (long) tigetflag( (char *)capname ) );
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004005}
4006
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004007/*[clinic input]
4008_curses.tigetnum
4009
4010 capname: str
4011 The terminfo capability name.
4012 /
4013
4014Return the value of the numeric capability.
4015
4016The value -2 is returned if capname is not a numeric capability, or -1 if
4017it is canceled or absent from the terminal description.
4018[clinic start generated code]*/
4019
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004020static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004021_curses_tigetnum_impl(PyObject *module, const char *capname)
4022/*[clinic end generated code: output=46f8b0a1b5dff42f input=5cdf2f410b109720]*/
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004023{
Victor Stinner26486ea2010-05-15 22:23:53 +00004024 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004025
Xiang Zhang44742e92018-06-23 12:29:30 +08004026 return PyLong_FromLong( (long) tigetnum( (char *)capname ) );
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004027}
4028
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004029/*[clinic input]
4030_curses.tigetstr
4031
4032 capname: str
4033 The terminfo capability name.
4034 /
4035
4036Return the value of the string capability.
4037
4038None is returned if capname is not a string capability, or is canceled or
4039absent from the terminal description.
4040[clinic start generated code]*/
4041
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004042static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004043_curses_tigetstr_impl(PyObject *module, const char *capname)
4044/*[clinic end generated code: output=f22b576ad60248f3 input=36644df25c73c0a7]*/
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004045{
Victor Stinner26486ea2010-05-15 22:23:53 +00004046 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004047
Xiang Zhang44742e92018-06-23 12:29:30 +08004048 capname = tigetstr( (char *)capname );
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +02004049 if (capname == NULL || capname == (char*) -1) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004050 Py_RETURN_NONE;
Victor Stinner26486ea2010-05-15 22:23:53 +00004051 }
4052 return PyBytes_FromString( capname );
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004053}
4054
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004055/*[clinic input]
4056_curses.tparm
4057
4058 str: str(accept={robuffer})
4059 Parameterized byte string obtained from the terminfo database.
4060 i1: int = 0
4061 i2: int = 0
4062 i3: int = 0
4063 i4: int = 0
4064 i5: int = 0
4065 i6: int = 0
4066 i7: int = 0
4067 i8: int = 0
4068 i9: int = 0
4069 /
4070
4071Instantiate the specified byte string with the supplied parameters.
4072[clinic start generated code]*/
4073
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004074static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004075_curses_tparm_impl(PyObject *module, const char *str, int i1, int i2, int i3,
4076 int i4, int i5, int i6, int i7, int i8, int i9)
4077/*[clinic end generated code: output=599f62b615c667ff input=5e30b15786f032aa]*/
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00004078{
Victor Stinner26486ea2010-05-15 22:23:53 +00004079 char* result = NULL;
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00004080
Victor Stinner26486ea2010-05-15 22:23:53 +00004081 PyCursesSetupTermCalled;
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00004082
Xiang Zhang44742e92018-06-23 12:29:30 +08004083 result = tparm((char *)str,i1,i2,i3,i4,i5,i6,i7,i8,i9);
Victor Stinner26486ea2010-05-15 22:23:53 +00004084 if (!result) {
4085 PyErr_SetString(PyCursesError, "tparm() returned NULL");
4086 return NULL;
4087 }
Michael W. Hudson21007982002-11-21 14:17:51 +00004088
Victor Stinner26486ea2010-05-15 22:23:53 +00004089 return PyBytes_FromString(result);
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00004090}
4091
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02004092#ifdef HAVE_CURSES_TYPEAHEAD
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004093/*[clinic input]
4094_curses.typeahead
4095
4096 fd: int
4097 File descriptor.
4098 /
4099
4100Specify that the file descriptor fd be used for typeahead checking.
4101
4102If fd is -1, then no typeahead checking is done.
4103[clinic start generated code]*/
4104
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00004105static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004106_curses_typeahead_impl(PyObject *module, int fd)
4107/*[clinic end generated code: output=084bb649d7066583 input=f2968d8e1805051b]*/
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00004108{
Victor Stinner26486ea2010-05-15 22:23:53 +00004109 PyCursesInitialised;
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00004110
Victor Stinner26486ea2010-05-15 22:23:53 +00004111 return PyCursesCheckERR(typeahead( fd ), "typeahead");
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00004112}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02004113#endif
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00004114
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004115/*[clinic input]
4116_curses.unctrl
4117
4118 ch: object
4119 /
4120
4121Return a string which is a printable representation of the character ch.
4122
4123Control characters are displayed as a caret followed by the character,
4124for example as ^C. Printing characters are left as they are.
4125[clinic start generated code]*/
4126
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00004127static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004128_curses_unctrl(PyObject *module, PyObject *ch)
4129/*[clinic end generated code: output=8e07fafc430c9434 input=cd1e35e16cd1ace4]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004130{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004131 chtype ch_;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004132
Victor Stinner26486ea2010-05-15 22:23:53 +00004133 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004134
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004135 if (!PyCurses_ConvertToChtype(NULL, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00004136 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004137
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004138 return PyBytes_FromString(unctrl(ch_));
Guido van Rossumf6971e21994-08-30 12:25:20 +00004139}
4140
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004141/*[clinic input]
4142_curses.ungetch
4143
4144 ch: object
4145 /
4146
4147Push ch so the next getch() will return it.
4148[clinic start generated code]*/
4149
Guido van Rossumf6971e21994-08-30 12:25:20 +00004150static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004151_curses_ungetch(PyObject *module, PyObject *ch)
4152/*[clinic end generated code: output=9b19d8268376d887 input=6681e6ae4c42e5eb]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00004153{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004154 chtype ch_;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004155
Victor Stinner26486ea2010-05-15 22:23:53 +00004156 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004157
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004158 if (!PyCurses_ConvertToChtype(NULL, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00004159 return NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01004160
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004161 return PyCursesCheckERR(ungetch(ch_), "ungetch");
Guido van Rossumf6971e21994-08-30 12:25:20 +00004162}
4163
Victor Stinner71e44cb2011-09-06 01:53:03 +02004164#ifdef HAVE_NCURSESW
4165/* Convert an object to a character (wchar_t):
4166
4167 - int
4168 - str of length 1
4169
4170 Return 1 on success, 0 on error. */
4171static int
4172PyCurses_ConvertToWchar_t(PyObject *obj,
4173 wchar_t *wch)
4174{
4175 if (PyUnicode_Check(obj)) {
4176 wchar_t buffer[2];
4177 if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) {
4178 PyErr_Format(PyExc_TypeError,
4179 "expect bytes or str of length 1, or int, "
4180 "got a str of length %zi",
Victor Stinnerc4f281e2011-10-11 22:11:42 +02004181 PyUnicode_GET_LENGTH(obj));
Victor Stinner71e44cb2011-09-06 01:53:03 +02004182 return 0;
4183 }
4184 *wch = buffer[0];
4185 return 2;
4186 }
4187 else if (PyLong_CheckExact(obj)) {
4188 long value;
4189 int overflow;
4190 value = PyLong_AsLongAndOverflow(obj, &overflow);
4191 if (overflow) {
4192 PyErr_SetString(PyExc_OverflowError,
4193 "int doesn't fit in long");
4194 return 0;
4195 }
4196 *wch = (wchar_t)value;
4197 if ((long)*wch != value) {
4198 PyErr_Format(PyExc_OverflowError,
4199 "character doesn't fit in wchar_t");
4200 return 0;
4201 }
4202 return 1;
4203 }
4204 else {
4205 PyErr_Format(PyExc_TypeError,
4206 "expect bytes or str of length 1, or int, got %s",
4207 Py_TYPE(obj)->tp_name);
4208 return 0;
4209 }
4210}
4211
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004212/*[clinic input]
4213_curses.unget_wch
4214
4215 ch: object
4216 /
4217
4218Push ch so the next get_wch() will return it.
4219[clinic start generated code]*/
4220
Victor Stinner71e44cb2011-09-06 01:53:03 +02004221static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004222_curses_unget_wch(PyObject *module, PyObject *ch)
4223/*[clinic end generated code: output=1974c9fb01d37863 input=0d56dc65a46feebb]*/
Victor Stinner71e44cb2011-09-06 01:53:03 +02004224{
Victor Stinner71e44cb2011-09-06 01:53:03 +02004225 wchar_t wch;
4226
4227 PyCursesInitialised;
4228
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004229 if (!PyCurses_ConvertToWchar_t(ch, &wch))
Victor Stinner71e44cb2011-09-06 01:53:03 +02004230 return NULL;
4231 return PyCursesCheckERR(unget_wch(wch), "unget_wch");
4232}
4233#endif
4234
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004235#ifdef HAVE_CURSES_USE_ENV
4236/*[clinic input]
4237_curses.use_env
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004238
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004239 flag: bool(accept={int})
4240 /
4241
4242Use environment variables LINES and COLUMNS.
4243
4244If used, this function should be called before initscr() or newterm() are
4245called.
4246
4247When flag is False, the values of lines and columns specified in the terminfo
4248database will be used, even if environment variables LINES and COLUMNS (used
4249by default) are set, or if curses is running in a window (in which case
4250default behavior would be to use the window size if LINES and COLUMNS are
4251not set).
4252[clinic start generated code]*/
4253
4254static PyObject *
4255_curses_use_env_impl(PyObject *module, int flag)
4256/*[clinic end generated code: output=b2c445e435c0b164 input=1778eb1e9151ea37]*/
4257{
Victor Stinner26486ea2010-05-15 22:23:53 +00004258 use_env(flag);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004259 Py_RETURN_NONE;
Guido van Rossumf6971e21994-08-30 12:25:20 +00004260}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02004261#endif
Guido van Rossumf6971e21994-08-30 12:25:20 +00004262
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004263#ifndef STRICT_SYSV_CURSES
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004264/*[clinic input]
4265_curses.use_default_colors
4266
4267Allow use of default values for colors on terminals supporting this feature.
4268
4269Use this to support transparency in your application. The default color
4270is assigned to the color number -1.
4271[clinic start generated code]*/
4272
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004273static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004274_curses_use_default_colors_impl(PyObject *module)
4275/*[clinic end generated code: output=a3b81ff71dd901be input=656844367470e8fc]*/
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004276{
Victor Stinner26486ea2010-05-15 22:23:53 +00004277 int code;
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004278
Victor Stinner26486ea2010-05-15 22:23:53 +00004279 PyCursesInitialised;
4280 PyCursesInitialisedColor;
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004281
Victor Stinner26486ea2010-05-15 22:23:53 +00004282 code = use_default_colors();
4283 if (code != ERR) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004284 Py_RETURN_NONE;
Victor Stinner26486ea2010-05-15 22:23:53 +00004285 } else {
4286 PyErr_SetString(PyCursesError, "use_default_colors() returned ERR");
4287 return NULL;
4288 }
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004289}
4290#endif /* STRICT_SYSV_CURSES */
4291
Serhiy Storchakab232df92018-10-30 13:22:42 +02004292
4293#ifdef NCURSES_VERSION
4294
4295PyDoc_STRVAR(ncurses_version__doc__,
4296"curses.ncurses_version\n\
4297\n\
4298Ncurses version information as a named tuple.");
4299
4300static PyTypeObject NcursesVersionType;
4301
4302static PyStructSequence_Field ncurses_version_fields[] = {
4303 {"major", "Major release number"},
4304 {"minor", "Minor release number"},
4305 {"patch", "Patch release number"},
4306 {0}
4307};
4308
4309static PyStructSequence_Desc ncurses_version_desc = {
4310 "curses.ncurses_version", /* name */
4311 ncurses_version__doc__, /* doc */
4312 ncurses_version_fields, /* fields */
4313 3
4314};
4315
4316static PyObject *
4317make_ncurses_version(void)
4318{
4319 PyObject *ncurses_version;
4320 int pos = 0;
4321
4322 ncurses_version = PyStructSequence_New(&NcursesVersionType);
4323 if (ncurses_version == NULL) {
4324 return NULL;
4325 }
4326
4327#define SetIntItem(flag) \
4328 PyStructSequence_SET_ITEM(ncurses_version, pos++, PyLong_FromLong(flag)); \
4329 if (PyErr_Occurred()) { \
4330 Py_CLEAR(ncurses_version); \
4331 return NULL; \
4332 }
4333
4334 SetIntItem(NCURSES_VERSION_MAJOR)
4335 SetIntItem(NCURSES_VERSION_MINOR)
4336 SetIntItem(NCURSES_VERSION_PATCH)
4337#undef SetIntItem
4338
4339 return ncurses_version;
4340}
4341
4342#endif /* NCURSES_VERSION */
4343
4344
Guido van Rossumf6971e21994-08-30 12:25:20 +00004345/* List of functions defined in the module */
4346
4347static PyMethodDef PyCurses_methods[] = {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004348 _CURSES_BAUDRATE_METHODDEF
4349 _CURSES_BEEP_METHODDEF
4350 _CURSES_CAN_CHANGE_COLOR_METHODDEF
4351 _CURSES_CBREAK_METHODDEF
4352 _CURSES_COLOR_CONTENT_METHODDEF
4353 _CURSES_COLOR_PAIR_METHODDEF
4354 _CURSES_CURS_SET_METHODDEF
4355 _CURSES_DEF_PROG_MODE_METHODDEF
4356 _CURSES_DEF_SHELL_MODE_METHODDEF
4357 _CURSES_DELAY_OUTPUT_METHODDEF
4358 _CURSES_DOUPDATE_METHODDEF
4359 _CURSES_ECHO_METHODDEF
4360 _CURSES_ENDWIN_METHODDEF
4361 _CURSES_ERASECHAR_METHODDEF
4362 _CURSES_FILTER_METHODDEF
4363 _CURSES_FLASH_METHODDEF
4364 _CURSES_FLUSHINP_METHODDEF
4365 _CURSES_GETMOUSE_METHODDEF
4366 _CURSES_UNGETMOUSE_METHODDEF
4367 _CURSES_GETSYX_METHODDEF
4368 _CURSES_GETWIN_METHODDEF
4369 _CURSES_HAS_COLORS_METHODDEF
4370 _CURSES_HAS_IC_METHODDEF
4371 _CURSES_HAS_IL_METHODDEF
4372 _CURSES_HAS_KEY_METHODDEF
4373 _CURSES_HALFDELAY_METHODDEF
4374 _CURSES_INIT_COLOR_METHODDEF
4375 _CURSES_INIT_PAIR_METHODDEF
4376 _CURSES_INITSCR_METHODDEF
4377 _CURSES_INTRFLUSH_METHODDEF
4378 _CURSES_ISENDWIN_METHODDEF
4379 _CURSES_IS_TERM_RESIZED_METHODDEF
4380 _CURSES_KEYNAME_METHODDEF
4381 _CURSES_KILLCHAR_METHODDEF
4382 _CURSES_LONGNAME_METHODDEF
4383 _CURSES_META_METHODDEF
4384 _CURSES_MOUSEINTERVAL_METHODDEF
4385 _CURSES_MOUSEMASK_METHODDEF
4386 _CURSES_NAPMS_METHODDEF
4387 _CURSES_NEWPAD_METHODDEF
4388 _CURSES_NEWWIN_METHODDEF
4389 _CURSES_NL_METHODDEF
4390 _CURSES_NOCBREAK_METHODDEF
4391 _CURSES_NOECHO_METHODDEF
4392 _CURSES_NONL_METHODDEF
4393 _CURSES_NOQIFLUSH_METHODDEF
4394 _CURSES_NORAW_METHODDEF
4395 _CURSES_PAIR_CONTENT_METHODDEF
4396 _CURSES_PAIR_NUMBER_METHODDEF
4397 _CURSES_PUTP_METHODDEF
4398 _CURSES_QIFLUSH_METHODDEF
4399 _CURSES_RAW_METHODDEF
4400 _CURSES_RESET_PROG_MODE_METHODDEF
4401 _CURSES_RESET_SHELL_MODE_METHODDEF
4402 _CURSES_RESETTY_METHODDEF
4403 _CURSES_RESIZETERM_METHODDEF
4404 _CURSES_RESIZE_TERM_METHODDEF
4405 _CURSES_SAVETTY_METHODDEF
4406 _CURSES_SETSYX_METHODDEF
4407 _CURSES_SETUPTERM_METHODDEF
4408 _CURSES_START_COLOR_METHODDEF
4409 _CURSES_TERMATTRS_METHODDEF
4410 _CURSES_TERMNAME_METHODDEF
4411 _CURSES_TIGETFLAG_METHODDEF
4412 _CURSES_TIGETNUM_METHODDEF
4413 _CURSES_TIGETSTR_METHODDEF
4414 _CURSES_TPARM_METHODDEF
4415 _CURSES_TYPEAHEAD_METHODDEF
4416 _CURSES_UNCTRL_METHODDEF
4417 _CURSES_UNGETCH_METHODDEF
4418 _CURSES_UPDATE_LINES_COLS_METHODDEF
4419 _CURSES_UNGET_WCH_METHODDEF
4420 _CURSES_USE_ENV_METHODDEF
4421 _CURSES_USE_DEFAULT_COLORS_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00004422 {NULL, NULL} /* sentinel */
Guido van Rossumf6971e21994-08-30 12:25:20 +00004423};
4424
4425/* Initialization function for the module */
4426
Martin v. Löwis1a214512008-06-11 05:26:20 +00004427
4428static struct PyModuleDef _cursesmodule = {
Victor Stinner26486ea2010-05-15 22:23:53 +00004429 PyModuleDef_HEAD_INIT,
4430 "_curses",
4431 NULL,
4432 -1,
4433 PyCurses_methods,
4434 NULL,
4435 NULL,
4436 NULL,
4437 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00004438};
4439
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004440PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00004441PyInit__curses(void)
Guido van Rossumf6971e21994-08-30 12:25:20 +00004442{
Victor Stinner26486ea2010-05-15 22:23:53 +00004443 PyObject *m, *d, *v, *c_api_object;
4444 static void *PyCurses_API[PyCurses_API_pointers];
Andrew M. Kuchling32552682000-12-22 21:52:27 +00004445
Victor Stinner26486ea2010-05-15 22:23:53 +00004446 /* Initialize object type */
4447 if (PyType_Ready(&PyCursesWindow_Type) < 0)
4448 return NULL;
Guido van Rossum6915c4d2001-01-19 00:28:08 +00004449
Victor Stinner26486ea2010-05-15 22:23:53 +00004450 /* Initialize the C API pointer array */
4451 PyCurses_API[0] = (void *)&PyCursesWindow_Type;
4452 PyCurses_API[1] = (void *)func_PyCursesSetupTermCalled;
4453 PyCurses_API[2] = (void *)func_PyCursesInitialised;
4454 PyCurses_API[3] = (void *)func_PyCursesInitialisedColor;
Guido van Rossumf6971e21994-08-30 12:25:20 +00004455
Victor Stinner26486ea2010-05-15 22:23:53 +00004456 /* Create the module and add the functions */
4457 m = PyModule_Create(&_cursesmodule);
4458 if (m == NULL)
4459 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +00004460
Victor Stinner26486ea2010-05-15 22:23:53 +00004461 /* Add some symbolic constants to the module */
4462 d = PyModule_GetDict(m);
4463 if (d == NULL)
4464 return NULL;
4465 ModDict = d; /* For PyCurses_InitScr to use later */
Andrew M. Kuchling32552682000-12-22 21:52:27 +00004466
Victor Stinner26486ea2010-05-15 22:23:53 +00004467 /* Add a capsule for the C API */
4468 c_api_object = PyCapsule_New(PyCurses_API, PyCurses_CAPSULE_NAME, NULL);
4469 PyDict_SetItemString(d, "_C_API", c_api_object);
4470 Py_DECREF(c_api_object);
Guido van Rossumfbea2f31994-08-31 22:05:27 +00004471
Victor Stinner26486ea2010-05-15 22:23:53 +00004472 /* For exception curses.error */
4473 PyCursesError = PyErr_NewException("_curses.error", NULL, NULL);
4474 PyDict_SetItemString(d, "error", PyCursesError);
Guido van Rossumfbea2f31994-08-31 22:05:27 +00004475
Victor Stinner26486ea2010-05-15 22:23:53 +00004476 /* Make the version available */
4477 v = PyBytes_FromString(PyCursesVersion);
4478 PyDict_SetItemString(d, "version", v);
4479 PyDict_SetItemString(d, "__version__", v);
4480 Py_DECREF(v);
Guido van Rossumf6971e21994-08-30 12:25:20 +00004481
Serhiy Storchakab232df92018-10-30 13:22:42 +02004482#ifdef NCURSES_VERSION
4483 /* ncurses_version */
4484 if (NcursesVersionType.tp_name == NULL) {
4485 if (PyStructSequence_InitType2(&NcursesVersionType,
4486 &ncurses_version_desc) < 0)
4487 return NULL;
4488 }
4489 v = make_ncurses_version();
4490 if (v == NULL) {
4491 return NULL;
4492 }
4493 PyDict_SetItemString(d, "ncurses_version", v);
4494 Py_DECREF(v);
4495
4496 /* prevent user from creating new instances */
4497 NcursesVersionType.tp_init = NULL;
4498 NcursesVersionType.tp_new = NULL;
4499 if (PyDict_DelItemString(NcursesVersionType.tp_dict, "__new__") < 0 &&
4500 PyErr_ExceptionMatches(PyExc_KeyError))
4501 {
4502 PyErr_Clear();
4503 }
4504#endif /* NCURSES_VERSION */
4505
Victor Stinner26486ea2010-05-15 22:23:53 +00004506 SetDictInt("ERR", ERR);
4507 SetDictInt("OK", OK);
Andrew M. Kuchlingcaefb372000-12-26 15:57:01 +00004508
Victor Stinner26486ea2010-05-15 22:23:53 +00004509 /* Here are some attributes you can add to chars to print */
4510
4511 SetDictInt("A_ATTRIBUTES", A_ATTRIBUTES);
4512 SetDictInt("A_NORMAL", A_NORMAL);
4513 SetDictInt("A_STANDOUT", A_STANDOUT);
4514 SetDictInt("A_UNDERLINE", A_UNDERLINE);
4515 SetDictInt("A_REVERSE", A_REVERSE);
4516 SetDictInt("A_BLINK", A_BLINK);
4517 SetDictInt("A_DIM", A_DIM);
4518 SetDictInt("A_BOLD", A_BOLD);
4519 SetDictInt("A_ALTCHARSET", A_ALTCHARSET);
Victor Stinner26486ea2010-05-15 22:23:53 +00004520 SetDictInt("A_INVIS", A_INVIS);
Victor Stinner26486ea2010-05-15 22:23:53 +00004521 SetDictInt("A_PROTECT", A_PROTECT);
4522 SetDictInt("A_CHARTEXT", A_CHARTEXT);
4523 SetDictInt("A_COLOR", A_COLOR);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004524
Victor Stinner26486ea2010-05-15 22:23:53 +00004525 /* The following are never available with strict SYSV curses */
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004526#ifdef A_HORIZONTAL
Victor Stinner26486ea2010-05-15 22:23:53 +00004527 SetDictInt("A_HORIZONTAL", A_HORIZONTAL);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004528#endif
4529#ifdef A_LEFT
Victor Stinner26486ea2010-05-15 22:23:53 +00004530 SetDictInt("A_LEFT", A_LEFT);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004531#endif
4532#ifdef A_LOW
Victor Stinner26486ea2010-05-15 22:23:53 +00004533 SetDictInt("A_LOW", A_LOW);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004534#endif
4535#ifdef A_RIGHT
Victor Stinner26486ea2010-05-15 22:23:53 +00004536 SetDictInt("A_RIGHT", A_RIGHT);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004537#endif
4538#ifdef A_TOP
Victor Stinner26486ea2010-05-15 22:23:53 +00004539 SetDictInt("A_TOP", A_TOP);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004540#endif
4541#ifdef A_VERTICAL
Victor Stinner26486ea2010-05-15 22:23:53 +00004542 SetDictInt("A_VERTICAL", A_VERTICAL);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004543#endif
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00004544
Xiang Zhang116dd5e2017-06-16 11:20:07 +08004545 /* ncurses extension */
4546#ifdef A_ITALIC
4547 SetDictInt("A_ITALIC", A_ITALIC);
4548#endif
4549
Victor Stinner26486ea2010-05-15 22:23:53 +00004550 SetDictInt("COLOR_BLACK", COLOR_BLACK);
4551 SetDictInt("COLOR_RED", COLOR_RED);
4552 SetDictInt("COLOR_GREEN", COLOR_GREEN);
4553 SetDictInt("COLOR_YELLOW", COLOR_YELLOW);
4554 SetDictInt("COLOR_BLUE", COLOR_BLUE);
4555 SetDictInt("COLOR_MAGENTA", COLOR_MAGENTA);
4556 SetDictInt("COLOR_CYAN", COLOR_CYAN);
4557 SetDictInt("COLOR_WHITE", COLOR_WHITE);
Guido van Rossumf6971e21994-08-30 12:25:20 +00004558
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004559#ifdef NCURSES_MOUSE_VERSION
Victor Stinner26486ea2010-05-15 22:23:53 +00004560 /* Mouse-related constants */
4561 SetDictInt("BUTTON1_PRESSED", BUTTON1_PRESSED);
4562 SetDictInt("BUTTON1_RELEASED", BUTTON1_RELEASED);
4563 SetDictInt("BUTTON1_CLICKED", BUTTON1_CLICKED);
4564 SetDictInt("BUTTON1_DOUBLE_CLICKED", BUTTON1_DOUBLE_CLICKED);
4565 SetDictInt("BUTTON1_TRIPLE_CLICKED", BUTTON1_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004566
Victor Stinner26486ea2010-05-15 22:23:53 +00004567 SetDictInt("BUTTON2_PRESSED", BUTTON2_PRESSED);
4568 SetDictInt("BUTTON2_RELEASED", BUTTON2_RELEASED);
4569 SetDictInt("BUTTON2_CLICKED", BUTTON2_CLICKED);
4570 SetDictInt("BUTTON2_DOUBLE_CLICKED", BUTTON2_DOUBLE_CLICKED);
4571 SetDictInt("BUTTON2_TRIPLE_CLICKED", BUTTON2_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004572
Victor Stinner26486ea2010-05-15 22:23:53 +00004573 SetDictInt("BUTTON3_PRESSED", BUTTON3_PRESSED);
4574 SetDictInt("BUTTON3_RELEASED", BUTTON3_RELEASED);
4575 SetDictInt("BUTTON3_CLICKED", BUTTON3_CLICKED);
4576 SetDictInt("BUTTON3_DOUBLE_CLICKED", BUTTON3_DOUBLE_CLICKED);
4577 SetDictInt("BUTTON3_TRIPLE_CLICKED", BUTTON3_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004578
Victor Stinner26486ea2010-05-15 22:23:53 +00004579 SetDictInt("BUTTON4_PRESSED", BUTTON4_PRESSED);
4580 SetDictInt("BUTTON4_RELEASED", BUTTON4_RELEASED);
4581 SetDictInt("BUTTON4_CLICKED", BUTTON4_CLICKED);
4582 SetDictInt("BUTTON4_DOUBLE_CLICKED", BUTTON4_DOUBLE_CLICKED);
4583 SetDictInt("BUTTON4_TRIPLE_CLICKED", BUTTON4_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004584
Victor Stinner26486ea2010-05-15 22:23:53 +00004585 SetDictInt("BUTTON_SHIFT", BUTTON_SHIFT);
4586 SetDictInt("BUTTON_CTRL", BUTTON_CTRL);
4587 SetDictInt("BUTTON_ALT", BUTTON_ALT);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004588
Victor Stinner26486ea2010-05-15 22:23:53 +00004589 SetDictInt("ALL_MOUSE_EVENTS", ALL_MOUSE_EVENTS);
4590 SetDictInt("REPORT_MOUSE_POSITION", REPORT_MOUSE_POSITION);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004591#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00004592 /* Now set everything up for KEY_ variables */
4593 {
4594 int key;
4595 char *key_n;
4596 char *key_n2;
Victor Stinner26486ea2010-05-15 22:23:53 +00004597 for (key=KEY_MIN;key < KEY_MAX; key++) {
4598 key_n = (char *)keyname(key);
4599 if (key_n == NULL || strcmp(key_n,"UNKNOWN KEY")==0)
4600 continue;
4601 if (strncmp(key_n,"KEY_F(",6)==0) {
4602 char *p1, *p2;
Victor Stinnerb6404912013-07-07 16:21:41 +02004603 key_n2 = PyMem_Malloc(strlen(key_n)+1);
Victor Stinner26486ea2010-05-15 22:23:53 +00004604 if (!key_n2) {
4605 PyErr_NoMemory();
4606 break;
4607 }
4608 p1 = key_n;
4609 p2 = key_n2;
4610 while (*p1) {
4611 if (*p1 != '(' && *p1 != ')') {
4612 *p2 = *p1;
4613 p2++;
4614 }
4615 p1++;
4616 }
4617 *p2 = (char)0;
4618 } else
4619 key_n2 = key_n;
4620 SetDictInt(key_n2,key);
4621 if (key_n2 != key_n)
Victor Stinnerb6404912013-07-07 16:21:41 +02004622 PyMem_Free(key_n2);
Victor Stinner26486ea2010-05-15 22:23:53 +00004623 }
Victor Stinner26486ea2010-05-15 22:23:53 +00004624 SetDictInt("KEY_MIN", KEY_MIN);
4625 SetDictInt("KEY_MAX", KEY_MAX);
4626 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004627
4628 Py_INCREF(&PyCursesWindow_Type);
4629 PyModule_AddObject(m, "window", (PyObject *)&PyCursesWindow_Type);
Victor Stinner26486ea2010-05-15 22:23:53 +00004630 return m;
Guido van Rossumf6971e21994-08-30 12:25:20 +00004631}