blob: d221cf1a92520c4b8512be912599e79cd192d5c4 [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"
Victor Stinner37834132020-10-27 17:12:53 +0100106#include "pycore_long.h" // _PyLong_GetZero()
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000107
Benjamin Petersonb173f782009-05-05 22:31:58 +0000108
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000109#ifdef __hpux
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000110#define STRICT_SYSV_CURSES
111#endif
112
Guido van Rossum858ca0f2001-04-10 19:53:37 +0000113#define CURSES_MODULE
114#include "py_curses.h"
115
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200116#if defined(HAVE_TERM_H) || defined(__sgi)
117/* For termname, longname, putp, tigetflag, tigetnum, tigetstr, tparm
118 which are not declared in SysV curses and for setupterm. */
Fred Drake0368bc42001-07-19 20:48:32 +0000119#include <term.h>
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200120/* Including <term.h> #defines many common symbols. */
121#undef lines
122#undef columns
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +0000123#endif
124
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100125#ifdef HAVE_LANGINFO_H
126#include <langinfo.h>
127#endif
128
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000129#if !defined(HAVE_NCURSES_H) && (defined(sgi) || defined(__sun) || defined(SCO5))
Andrew M. Kuchlingbf680262000-07-25 12:37:41 +0000130#define STRICT_SYSV_CURSES /* Don't use ncurses extensions */
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +0000131typedef chtype attr_t; /* No attr_t type is available */
Guido van Rossum1266a011996-02-25 04:50:31 +0000132#endif
Guido van Rossumf6971e21994-08-30 12:25:20 +0000133
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000134#if defined(_AIX)
135#define STRICT_SYSV_CURSES
136#endif
137
Serhiy Storchaka59f9b4e2021-01-05 09:13:15 +0200138#if NCURSES_EXT_COLORS+0 && NCURSES_EXT_FUNCS+0
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -0500139#define _NCURSES_EXTENDED_COLOR_FUNCS 1
140#else
141#define _NCURSES_EXTENDED_COLOR_FUNCS 0
142#endif /* defined(NCURSES_EXT_COLORS) && defined(NCURSES_EXT_FUNCS) */
143
144#if _NCURSES_EXTENDED_COLOR_FUNCS
Serhiy Storchaka59f9b4e2021-01-05 09:13:15 +0200145#define _CURSES_COLOR_VAL_TYPE int
146#define _CURSES_COLOR_NUM_TYPE int
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -0500147#define _CURSES_INIT_COLOR_FUNC init_extended_color
148#define _CURSES_INIT_PAIR_FUNC init_extended_pair
149#define _COLOR_CONTENT_FUNC extended_color_content
Serhiy Storchaka59f9b4e2021-01-05 09:13:15 +0200150#define _CURSES_PAIR_CONTENT_FUNC extended_pair_content
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -0500151#else
Serhiy Storchaka59f9b4e2021-01-05 09:13:15 +0200152#define _CURSES_COLOR_VAL_TYPE short
153#define _CURSES_COLOR_NUM_TYPE short
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -0500154#define _CURSES_INIT_COLOR_FUNC init_color
155#define _CURSES_INIT_PAIR_FUNC init_pair
156#define _COLOR_CONTENT_FUNC color_content
Serhiy Storchaka59f9b4e2021-01-05 09:13:15 +0200157#define _CURSES_PAIR_CONTENT_FUNC pair_content
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -0500158#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */
159
Larry Hastings61272b72014-01-07 12:41:53 -0800160/*[clinic input]
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300161module _curses
162class _curses.window "PyCursesWindowObject *" "&PyCursesWindow_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800163[clinic start generated code]*/
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300164/*[clinic end generated code: output=da39a3ee5e6b4b0d input=43265c372c2887d6]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300165
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000166/* Definition of exception curses.error */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000167
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000168static PyObject *PyCursesError;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000169
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +0000170/* Tells whether setupterm() has been called to initialise terminfo. */
171static int initialised_setupterm = FALSE;
172
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000173/* Tells whether initscr() has been called to initialise curses. */
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000174static int initialised = FALSE;
175
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000176/* Tells whether start_color() has been called to initialise color usage. */
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000177static int initialisedcolors = FALSE;
178
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100179static char *screen_encoding = NULL;
180
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000181/* Utility Macros */
Victor Stinner26486ea2010-05-15 22:23:53 +0000182#define PyCursesSetupTermCalled \
183 if (initialised_setupterm != TRUE) { \
184 PyErr_SetString(PyCursesError, \
185 "must call (at least) setupterm() first"); \
186 return 0; }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +0000187
Victor Stinner26486ea2010-05-15 22:23:53 +0000188#define PyCursesInitialised \
189 if (initialised != TRUE) { \
190 PyErr_SetString(PyCursesError, \
191 "must call initscr() first"); \
192 return 0; }
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000193
Victor Stinner26486ea2010-05-15 22:23:53 +0000194#define PyCursesInitialisedColor \
195 if (initialisedcolors != TRUE) { \
196 PyErr_SetString(PyCursesError, \
197 "must call start_color() first"); \
198 return 0; }
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000199
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000200/* Utility Functions */
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000201
Guido van Rossum85738471995-02-17 13:50:17 +0000202/*
Victor Stinner26486ea2010-05-15 22:23:53 +0000203 * Check the return code from a curses function and return None
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000204 * or raise an exception as appropriate. These are exported using the
Victor Stinner26486ea2010-05-15 22:23:53 +0000205 * capsule API.
Guido van Rossum85738471995-02-17 13:50:17 +0000206 */
207
208static PyObject *
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100209PyCursesCheckERR(int code, const char *fname)
Guido van Rossum85738471995-02-17 13:50:17 +0000210{
Victor Stinner26486ea2010-05-15 22:23:53 +0000211 if (code != ERR) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200212 Py_RETURN_NONE;
Guido van Rossum85738471995-02-17 13:50:17 +0000213 } else {
Victor Stinner26486ea2010-05-15 22:23:53 +0000214 if (fname == NULL) {
215 PyErr_SetString(PyCursesError, catchall_ERR);
216 } else {
217 PyErr_Format(PyCursesError, "%s() returned ERR", fname);
218 }
219 return NULL;
Guido van Rossum85738471995-02-17 13:50:17 +0000220 }
Guido van Rossum85738471995-02-17 13:50:17 +0000221}
222
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100223/* Convert an object to a byte (an integer of type chtype):
224
225 - int
226 - bytes of length 1
227 - str of length 1
228
229 Return 1 on success, 0 on error (invalid type or integer overflow). */
Victor Stinner26486ea2010-05-15 22:23:53 +0000230static int
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100231PyCurses_ConvertToChtype(PyCursesWindowObject *win, PyObject *obj, chtype *ch)
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000232{
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100233 long value;
234 if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) {
235 value = (unsigned char)PyBytes_AsString(obj)[0];
236 }
237 else if (PyUnicode_Check(obj)) {
238 if (PyUnicode_GetLength(obj) != 1) {
239 PyErr_Format(PyExc_TypeError,
240 "expect bytes or str of length 1, or int, "
241 "got a str of length %zi",
242 PyUnicode_GET_LENGTH(obj));
Victor Stinner26486ea2010-05-15 22:23:53 +0000243 return 0;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100244 }
245 value = PyUnicode_READ_CHAR(obj, 0);
246 if (128 < value) {
247 PyObject *bytes;
248 const char *encoding;
249 if (win)
250 encoding = win->encoding;
251 else
252 encoding = screen_encoding;
Serhiy Storchakab29cee42016-10-27 19:31:49 +0300253 bytes = PyUnicode_AsEncodedString(obj, encoding, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100254 if (bytes == NULL)
255 return 0;
256 if (PyBytes_GET_SIZE(bytes) == 1)
257 value = (unsigned char)PyBytes_AS_STRING(bytes)[0];
258 else
259 value = -1;
260 Py_DECREF(bytes);
261 if (value < 0)
262 goto overflow;
263 }
264 }
265 else if (PyLong_CheckExact(obj)) {
266 int long_overflow;
267 value = PyLong_AsLongAndOverflow(obj, &long_overflow);
268 if (long_overflow)
269 goto overflow;
270 }
271 else {
272 PyErr_Format(PyExc_TypeError,
273 "expect bytes or str of length 1, or int, got %s",
274 Py_TYPE(obj)->tp_name);
Victor Stinner26486ea2010-05-15 22:23:53 +0000275 return 0;
276 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100277 *ch = (chtype)value;
278 if ((long)*ch != value)
279 goto overflow;
Victor Stinner26486ea2010-05-15 22:23:53 +0000280 return 1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100281
282overflow:
283 PyErr_SetString(PyExc_OverflowError,
284 "byte doesn't fit in chtype");
285 return 0;
286}
287
288/* Convert an object to a byte (chtype) or a character (cchar_t):
289
290 - int
291 - bytes of length 1
292 - str of length 1
293
294 Return:
295
296 - 2 if obj is a character (written into *wch)
297 - 1 if obj is a byte (written into *ch)
298 - 0 on error: raise an exception */
299static int
300PyCurses_ConvertToCchar_t(PyCursesWindowObject *win, PyObject *obj,
301 chtype *ch
302#ifdef HAVE_NCURSESW
Serhiy Storchaka0bcd89b2016-10-30 22:52:06 +0200303 , wchar_t *wch
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100304#endif
305 )
306{
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100307 long value;
308#ifdef HAVE_NCURSESW
309 wchar_t buffer[2];
310#endif
311
312 if (PyUnicode_Check(obj)) {
313#ifdef HAVE_NCURSESW
314 if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) {
315 PyErr_Format(PyExc_TypeError,
316 "expect bytes or str of length 1, or int, "
317 "got a str of length %zi",
318 PyUnicode_GET_LENGTH(obj));
319 return 0;
320 }
Serhiy Storchaka0bcd89b2016-10-30 22:52:06 +0200321 *wch = buffer[0];
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100322 return 2;
323#else
324 return PyCurses_ConvertToChtype(win, obj, ch);
325#endif
326 }
327 else if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) {
328 value = (unsigned char)PyBytes_AsString(obj)[0];
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100329 }
330 else if (PyLong_CheckExact(obj)) {
331 int overflow;
332 value = PyLong_AsLongAndOverflow(obj, &overflow);
333 if (overflow) {
334 PyErr_SetString(PyExc_OverflowError,
335 "int doesn't fit in long");
336 return 0;
337 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100338 }
339 else {
340 PyErr_Format(PyExc_TypeError,
341 "expect bytes or str of length 1, or int, got %s",
342 Py_TYPE(obj)->tp_name);
343 return 0;
344 }
Victor Stinner9f16e442012-09-01 15:00:34 +0200345
346 *ch = (chtype)value;
347 if ((long)*ch != value) {
348 PyErr_Format(PyExc_OverflowError,
349 "byte doesn't fit in chtype");
350 return 0;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100351 }
Victor Stinner9f16e442012-09-01 15:00:34 +0200352 return 1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100353}
354
355/* Convert an object to a byte string (char*) or a wide character string
356 (wchar_t*). Return:
357
358 - 2 if obj is a character string (written into *wch)
359 - 1 if obj is a byte string (written into *bytes)
360 - 0 on error: raise an exception */
361static int
362PyCurses_ConvertToString(PyCursesWindowObject *win, PyObject *obj,
363 PyObject **bytes, wchar_t **wstr)
364{
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300365 char *str;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100366 if (PyUnicode_Check(obj)) {
367#ifdef HAVE_NCURSESW
368 assert (wstr != NULL);
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300369
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100370 *wstr = PyUnicode_AsWideCharString(obj, NULL);
371 if (*wstr == NULL)
372 return 0;
373 return 2;
374#else
375 assert (wstr == NULL);
Serhiy Storchakab29cee42016-10-27 19:31:49 +0300376 *bytes = PyUnicode_AsEncodedString(obj, win->encoding, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100377 if (*bytes == NULL)
378 return 0;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300379 /* check for embedded null bytes */
380 if (PyBytes_AsStringAndSize(*bytes, &str, NULL) < 0) {
381 return 0;
382 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100383 return 1;
384#endif
385 }
386 else if (PyBytes_Check(obj)) {
387 Py_INCREF(obj);
388 *bytes = obj;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300389 /* check for embedded null bytes */
390 if (PyBytes_AsStringAndSize(*bytes, &str, NULL) < 0) {
Pablo Galindobb739ec2021-02-02 20:38:26 +0000391 Py_DECREF(obj);
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300392 return 0;
393 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100394 return 1;
395 }
396
397 PyErr_Format(PyExc_TypeError, "expect bytes or str, got %s",
398 Py_TYPE(obj)->tp_name);
399 return 0;
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000400}
401
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -0500402static int
Serhiy Storchaka1470edd2021-01-03 22:51:11 +0200403color_allow_default_converter(PyObject *arg, void *ptr)
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -0500404{
405 long color_number;
406 int overflow;
407
408 color_number = PyLong_AsLongAndOverflow(arg, &overflow);
409 if (color_number == -1 && PyErr_Occurred())
410 return 0;
411
Serhiy Storchaka1470edd2021-01-03 22:51:11 +0200412 if (overflow > 0 || color_number >= COLORS) {
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -0500413 PyErr_Format(PyExc_ValueError,
Serhiy Storchaka1470edd2021-01-03 22:51:11 +0200414 "Color number is greater than COLORS-1 (%d).",
415 COLORS - 1);
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -0500416 return 0;
417 }
418 else if (overflow < 0 || color_number < 0) {
Serhiy Storchaka1470edd2021-01-03 22:51:11 +0200419 color_number = -1;
420 }
421
422 *(int *)ptr = (int)color_number;
423 return 1;
424}
425
426static int
427color_converter(PyObject *arg, void *ptr)
428{
429 if (!color_allow_default_converter(arg, ptr)) {
430 return 0;
431 }
432 if (*(int *)ptr < 0) {
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -0500433 PyErr_SetString(PyExc_ValueError,
434 "Color number is less than 0.");
435 return 0;
436 }
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -0500437 return 1;
438}
439
440/*[python input]
441class color_converter(CConverter):
442 type = 'int'
443 converter = 'color_converter'
444[python start generated code]*/
445/*[python end generated code: output=da39a3ee5e6b4b0d input=4260d2b6e66b3709]*/
446
Serhiy Storchaka1470edd2021-01-03 22:51:11 +0200447/*[python input]
448class color_allow_default_converter(CConverter):
449 type = 'int'
450 converter = 'color_allow_default_converter'
451[python start generated code]*/
452/*[python end generated code: output=da39a3ee5e6b4b0d input=975602bc058a872d]*/
453
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -0500454static int
455pair_converter(PyObject *arg, void *ptr)
456{
457 long pair_number;
458 int overflow;
459
460 pair_number = PyLong_AsLongAndOverflow(arg, &overflow);
461 if (pair_number == -1 && PyErr_Occurred())
462 return 0;
463
Serhiy Storchaka1470edd2021-01-03 22:51:11 +0200464#if _NCURSES_EXTENDED_COLOR_FUNCS
465 if (overflow > 0 || pair_number > INT_MAX) {
466 PyErr_Format(PyExc_ValueError,
467 "Color pair is greater than maximum (%d).",
468 INT_MAX);
469 return 0;
470 }
471#else
472 if (overflow > 0 || pair_number >= COLOR_PAIRS) {
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -0500473 PyErr_Format(PyExc_ValueError,
474 "Color pair is greater than COLOR_PAIRS-1 (%d).",
475 COLOR_PAIRS - 1);
476 return 0;
477 }
Serhiy Storchaka1470edd2021-01-03 22:51:11 +0200478#endif
479 else if (overflow < 0 || pair_number < 0) {
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -0500480 PyErr_SetString(PyExc_ValueError,
Serhiy Storchaka1470edd2021-01-03 22:51:11 +0200481 "Color pair is less than 0.");
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -0500482 return 0;
483 }
484
485 *(int *)ptr = (int)pair_number;
486 return 1;
487}
488
489/*[python input]
490class pair_converter(CConverter):
491 type = 'int'
492 converter = 'pair_converter'
493[python start generated code]*/
494/*[python end generated code: output=da39a3ee5e6b4b0d input=1a918ae6a1b32af7]*/
495
496static int
497component_converter(PyObject *arg, void *ptr)
498{
499 long component;
500 int overflow;
501
502 component = PyLong_AsLongAndOverflow(arg, &overflow);
503 if (component == -1 && PyErr_Occurred())
504 return 0;
505
506 if (overflow > 0 || component > 1000) {
507 PyErr_SetString(PyExc_ValueError,
508 "Color component is greater than 1000");
509 return 0;
510 }
511 else if (overflow < 0 || component < 0) {
512 PyErr_SetString(PyExc_ValueError,
513 "Color component is less than 0");
514 return 0;
515 }
516
517 *(short *)ptr = (short)component;
518 return 1;
519}
520
521/*[python input]
522class component_converter(CConverter):
523 type = 'short'
524 converter = 'component_converter'
525[python start generated code]*/
526/*[python end generated code: output=da39a3ee5e6b4b0d input=38e9be01d33927fb]*/
527
Benjamin Peterson21896a32010-03-21 22:03:03 +0000528/* Function versions of the 3 functions for testing whether curses has been
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000529 initialised or not. */
Victor Stinner26486ea2010-05-15 22:23:53 +0000530
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000531static int func_PyCursesSetupTermCalled(void)
532{
533 PyCursesSetupTermCalled;
534 return 1;
535}
536
537static int func_PyCursesInitialised(void)
538{
539 PyCursesInitialised;
540 return 1;
541}
542
543static int func_PyCursesInitialisedColor(void)
544{
545 PyCursesInitialisedColor;
546 return 1;
547}
548
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000549/*****************************************************************************
550 The Window Object
551******************************************************************************/
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000552
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000553/* Definition of the window type */
Guido van Rossum85738471995-02-17 13:50:17 +0000554
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000555PyTypeObject PyCursesWindow_Type;
556
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000557/* Function prototype macros for Window object
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000558
559 X - function name
560 TYPE - parameter Type
561 ERGSTR - format string for construction of the return value
562 PARSESTR - format string for argument parsing
Victor Stinner26486ea2010-05-15 22:23:53 +0000563*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000564
Serhiy Storchaka81524022018-11-27 13:05:02 +0200565#define Window_NoArgNoReturnFunction(X) \
566 static PyObject *PyCursesWindow_ ## X \
567 (PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \
Victor Stinner26486ea2010-05-15 22:23:53 +0000568 { return PyCursesCheckERR(X(self->win), # X); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000569
Victor Stinner26486ea2010-05-15 22:23:53 +0000570#define Window_NoArgTrueFalseFunction(X) \
571 static PyObject * PyCursesWindow_ ## X \
Serhiy Storchaka81524022018-11-27 13:05:02 +0200572 (PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \
Victor Stinner26486ea2010-05-15 22:23:53 +0000573 { \
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300574 return PyBool_FromLong(X(self->win)); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000575
Victor Stinner26486ea2010-05-15 22:23:53 +0000576#define Window_NoArgNoReturnVoidFunction(X) \
577 static PyObject * PyCursesWindow_ ## X \
Serhiy Storchaka81524022018-11-27 13:05:02 +0200578 (PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \
Victor Stinner26486ea2010-05-15 22:23:53 +0000579 { \
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200580 X(self->win); Py_RETURN_NONE; }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000581
Victor Stinner26486ea2010-05-15 22:23:53 +0000582#define Window_NoArg2TupleReturnFunction(X, TYPE, ERGSTR) \
583 static PyObject * PyCursesWindow_ ## X \
Serhiy Storchaka81524022018-11-27 13:05:02 +0200584 (PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \
Victor Stinner26486ea2010-05-15 22:23:53 +0000585 { \
586 TYPE arg1, arg2; \
587 X(self->win,arg1,arg2); return Py_BuildValue(ERGSTR, arg1, arg2); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000588
Victor Stinner26486ea2010-05-15 22:23:53 +0000589#define Window_OneArgNoReturnVoidFunction(X, TYPE, PARSESTR) \
590 static PyObject * PyCursesWindow_ ## X \
591 (PyCursesWindowObject *self, PyObject *args) \
592 { \
593 TYPE arg1; \
594 if (!PyArg_ParseTuple(args, PARSESTR, &arg1)) return NULL; \
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200595 X(self->win,arg1); Py_RETURN_NONE; }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000596
Victor Stinner26486ea2010-05-15 22:23:53 +0000597#define Window_OneArgNoReturnFunction(X, TYPE, PARSESTR) \
598 static PyObject * PyCursesWindow_ ## X \
599 (PyCursesWindowObject *self, PyObject *args) \
600 { \
601 TYPE arg1; \
602 if (!PyArg_ParseTuple(args,PARSESTR, &arg1)) return NULL; \
603 return PyCursesCheckERR(X(self->win, arg1), # X); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000604
Victor Stinner26486ea2010-05-15 22:23:53 +0000605#define Window_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \
606 static PyObject * PyCursesWindow_ ## X \
607 (PyCursesWindowObject *self, PyObject *args) \
608 { \
609 TYPE arg1, arg2; \
610 if (!PyArg_ParseTuple(args,PARSESTR, &arg1, &arg2)) return NULL; \
611 return PyCursesCheckERR(X(self->win, arg1, arg2), # X); }
Guido van Rossum85738471995-02-17 13:50:17 +0000612
Guido van Rossumf6971e21994-08-30 12:25:20 +0000613/* ------------- WINDOW routines --------------- */
Guido van Rossum85738471995-02-17 13:50:17 +0000614
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000615Window_NoArgNoReturnFunction(untouchwin)
616Window_NoArgNoReturnFunction(touchwin)
617Window_NoArgNoReturnFunction(redrawwin)
618Window_NoArgNoReturnFunction(winsertln)
619Window_NoArgNoReturnFunction(werase)
620Window_NoArgNoReturnFunction(wdeleteln)
621
622Window_NoArgTrueFalseFunction(is_wintouched)
623
624Window_NoArgNoReturnVoidFunction(wsyncup)
625Window_NoArgNoReturnVoidFunction(wsyncdown)
626Window_NoArgNoReturnVoidFunction(wstandend)
627Window_NoArgNoReturnVoidFunction(wstandout)
628Window_NoArgNoReturnVoidFunction(wcursyncup)
629Window_NoArgNoReturnVoidFunction(wclrtoeol)
630Window_NoArgNoReturnVoidFunction(wclrtobot)
631Window_NoArgNoReturnVoidFunction(wclear)
632
633Window_OneArgNoReturnVoidFunction(idcok, int, "i;True(1) or False(0)")
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200634#ifdef HAVE_CURSES_IMMEDOK
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000635Window_OneArgNoReturnVoidFunction(immedok, int, "i;True(1) or False(0)")
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200636#endif
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +0000637Window_OneArgNoReturnVoidFunction(wtimeout, int, "i;delay")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000638
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000639Window_NoArg2TupleReturnFunction(getyx, int, "ii")
640Window_NoArg2TupleReturnFunction(getbegyx, int, "ii")
641Window_NoArg2TupleReturnFunction(getmaxyx, int, "ii")
642Window_NoArg2TupleReturnFunction(getparyx, int, "ii")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000643
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000644Window_OneArgNoReturnFunction(clearok, int, "i;True(1) or False(0)")
645Window_OneArgNoReturnFunction(idlok, int, "i;True(1) or False(0)")
646Window_OneArgNoReturnFunction(keypad, int, "i;True(1) or False(0)")
647Window_OneArgNoReturnFunction(leaveok, int, "i;True(1) or False(0)")
648Window_OneArgNoReturnFunction(nodelay, int, "i;True(1) or False(0)")
649Window_OneArgNoReturnFunction(notimeout, int, "i;True(1) or False(0)")
650Window_OneArgNoReturnFunction(scrollok, int, "i;True(1) or False(0)")
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +0000651Window_OneArgNoReturnFunction(winsdelln, int, "i;nlines")
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200652#ifdef HAVE_CURSES_SYNCOK
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000653Window_OneArgNoReturnFunction(syncok, int, "i;True(1) or False(0)")
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200654#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000655
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000656Window_TwoArgNoReturnFunction(mvwin, int, "ii;y,x")
657Window_TwoArgNoReturnFunction(mvderwin, int, "ii;y,x")
658Window_TwoArgNoReturnFunction(wmove, int, "ii;y,x")
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +0000659#ifndef STRICT_SYSV_CURSES
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000660Window_TwoArgNoReturnFunction(wresize, int, "ii;lines,columns")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000661#endif
662
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +0000663/* Allocation and deallocation of Window Objects */
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000664
Guido van Rossumf6971e21994-08-30 12:25:20 +0000665static PyObject *
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100666PyCursesWindow_New(WINDOW *win, const char *encoding)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000667{
Victor Stinner26486ea2010-05-15 22:23:53 +0000668 PyCursesWindowObject *wo;
Guido van Rossum85738471995-02-17 13:50:17 +0000669
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100670 if (encoding == NULL) {
671#if defined(MS_WINDOWS)
672 char *buffer[100];
673 UINT cp;
674 cp = GetConsoleOutputCP();
675 if (cp != 0) {
676 PyOS_snprintf(buffer, sizeof(buffer), "cp%u", cp);
677 encoding = buffer;
678 }
679#elif defined(CODESET)
680 const char *codeset = nl_langinfo(CODESET);
681 if (codeset != NULL && codeset[0] != 0)
682 encoding = codeset;
683#endif
684 if (encoding == NULL)
685 encoding = "utf-8";
686 }
687
Victor Stinner92055202020-04-08 00:38:15 +0200688 wo = PyObject_New(PyCursesWindowObject, &PyCursesWindow_Type);
Victor Stinner26486ea2010-05-15 22:23:53 +0000689 if (wo == NULL) return NULL;
690 wo->win = win;
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200691 wo->encoding = _PyMem_Strdup(encoding);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100692 if (wo->encoding == NULL) {
693 Py_DECREF(wo);
694 PyErr_NoMemory();
695 return NULL;
696 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000697 return (PyObject *)wo;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000698}
699
700static void
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000701PyCursesWindow_Dealloc(PyCursesWindowObject *wo)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000702{
Victor Stinner26486ea2010-05-15 22:23:53 +0000703 if (wo->win != stdscr) delwin(wo->win);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100704 if (wo->encoding != NULL)
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200705 PyMem_Free(wo->encoding);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100706 PyObject_Free(wo);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000707}
708
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000709/* Addch, Addstr, Addnstr */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000710
Larry Hastings61272b72014-01-07 12:41:53 -0800711/*[clinic input]
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300712_curses.window.addch
Larry Hastings31826802013-10-19 00:09:25 -0700713
714 [
Larry Hastings31826802013-10-19 00:09:25 -0700715 y: int
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300716 Y-coordinate.
Larry Hastings9147a962014-05-04 04:41:18 -0700717 x: int
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300718 X-coordinate.
Larry Hastings31826802013-10-19 00:09:25 -0700719 ]
720
721 ch: object
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300722 Character to add.
Larry Hastings31826802013-10-19 00:09:25 -0700723
724 [
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300725 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
726 Attributes for the character.
Larry Hastings31826802013-10-19 00:09:25 -0700727 ]
728 /
729
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300730Paint the character.
Larry Hastings31826802013-10-19 00:09:25 -0700731
732Paint character ch at (y, x) with attributes attr,
733overwriting any character previously painted at that location.
734By default, the character position and attributes are the
735current settings for the window object.
Larry Hastings61272b72014-01-07 12:41:53 -0800736[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700737
Larry Hastings31826802013-10-19 00:09:25 -0700738static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300739_curses_window_addch_impl(PyCursesWindowObject *self, int group_left_1,
740 int y, int x, PyObject *ch, int group_right_1,
741 long attr)
742/*[clinic end generated code: output=00f4c37af3378f45 input=95ce131578458196]*/
Larry Hastings31826802013-10-19 00:09:25 -0700743{
Larry Hastings31826802013-10-19 00:09:25 -0700744 int coordinates_group = group_left_1;
Larry Hastings31826802013-10-19 00:09:25 -0700745 int rtn;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100746 int type;
Victor Stinnerb110dad2016-12-09 17:06:43 +0100747 chtype cch = 0;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100748#ifdef HAVE_NCURSESW
Serhiy Storchaka0bcd89b2016-10-30 22:52:06 +0200749 wchar_t wstr[2];
750 cchar_t wcval;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100751#endif
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100752 const char *funcname;
Guido van Rossum85738471995-02-17 13:50:17 +0000753
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100754#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300755 type = PyCurses_ConvertToCchar_t(self, ch, &cch, wstr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100756 if (type == 2) {
757 funcname = "add_wch";
Serhiy Storchaka0bcd89b2016-10-30 22:52:06 +0200758 wstr[1] = L'\0';
Serhiy Storchaka1470edd2021-01-03 22:51:11 +0200759 setcchar(&wcval, wstr, attr, PAIR_NUMBER(attr), NULL);
Larry Hastings31826802013-10-19 00:09:25 -0700760 if (coordinates_group)
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300761 rtn = mvwadd_wch(self->win,y,x, &wcval);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100762 else {
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300763 rtn = wadd_wch(self->win, &wcval);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100764 }
765 }
766 else
767#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300768 type = PyCurses_ConvertToCchar_t(self, ch, &cch);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100769#endif
770 if (type == 1) {
771 funcname = "addch";
Larry Hastings31826802013-10-19 00:09:25 -0700772 if (coordinates_group)
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300773 rtn = mvwaddch(self->win,y,x, cch | (attr_t) attr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100774 else {
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300775 rtn = waddch(self->win, cch | (attr_t) attr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100776 }
777 }
778 else {
Victor Stinner26486ea2010-05-15 22:23:53 +0000779 return NULL;
780 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100781 return PyCursesCheckERR(rtn, funcname);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000782}
783
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300784/*[clinic input]
785_curses.window.addstr
786
787 [
788 y: int
789 Y-coordinate.
790 x: int
791 X-coordinate.
792 ]
793
794 str: object
795 String to add.
796
797 [
798 attr: long
799 Attributes for characters.
800 ]
801 /
802
803Paint the string.
804
805Paint the string str at (y, x) with attributes attr,
806overwriting anything previously on the display.
807By default, the character position and attributes are the
808current settings for the window object.
809[clinic start generated code]*/
810
Guido van Rossumf6971e21994-08-30 12:25:20 +0000811static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300812_curses_window_addstr_impl(PyCursesWindowObject *self, int group_left_1,
813 int y, int x, PyObject *str, int group_right_1,
814 long attr)
815/*[clinic end generated code: output=65a928ea85ff3115 input=ff6cbb91448a22a3]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +0000816{
Victor Stinner26486ea2010-05-15 22:23:53 +0000817 int rtn;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100818 int strtype;
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300819 PyObject *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100820#ifdef HAVE_NCURSESW
821 wchar_t *wstr = NULL;
822#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300823 attr_t attr_old = A_NORMAL;
824 int use_xy = group_left_1, use_attr = group_right_1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100825 const char *funcname;
Guido van Rossum85738471995-02-17 13:50:17 +0000826
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100827#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300828 strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100829#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300830 strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100831#endif
Pablo Galindobb739ec2021-02-02 20:38:26 +0000832 if (strtype == 0) {
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100833 return NULL;
Pablo Galindobb739ec2021-02-02 20:38:26 +0000834 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300835 if (use_attr) {
Victor Stinner26486ea2010-05-15 22:23:53 +0000836 attr_old = getattrs(self->win);
837 (void)wattrset(self->win,attr);
838 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100839#ifdef HAVE_NCURSESW
840 if (strtype == 2) {
841 funcname = "addwstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300842 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100843 rtn = mvwaddwstr(self->win,y,x,wstr);
844 else
845 rtn = waddwstr(self->win,wstr);
846 PyMem_Free(wstr);
847 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000848 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100849#endif
850 {
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +0300851 const char *str = PyBytes_AS_STRING(bytesobj);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100852 funcname = "addstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300853 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100854 rtn = mvwaddstr(self->win,y,x,str);
855 else
856 rtn = waddstr(self->win,str);
857 Py_DECREF(bytesobj);
858 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300859 if (use_attr)
Victor Stinner26486ea2010-05-15 22:23:53 +0000860 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100861 return PyCursesCheckERR(rtn, funcname);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000862}
Guido van Rossum85738471995-02-17 13:50:17 +0000863
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300864/*[clinic input]
865_curses.window.addnstr
866
867 [
868 y: int
869 Y-coordinate.
870 x: int
871 X-coordinate.
872 ]
873
874 str: object
875 String to add.
876
877 n: int
878 Maximal number of characters.
879
880 [
881 attr: long
882 Attributes for characters.
883 ]
884 /
885
886Paint at most n characters of the string.
887
888Paint at most n characters of the string str at (y, x) with
889attributes attr, overwriting anything previously on the display.
890By default, the character position and attributes are the
891current settings for the window object.
892[clinic start generated code]*/
893
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000894static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300895_curses_window_addnstr_impl(PyCursesWindowObject *self, int group_left_1,
896 int y, int x, PyObject *str, int n,
897 int group_right_1, long attr)
898/*[clinic end generated code: output=6d21cee2ce6876d9 input=72718415c2744a2a]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000899{
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300900 int rtn;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100901 int strtype;
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300902 PyObject *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100903#ifdef HAVE_NCURSESW
904 wchar_t *wstr = NULL;
905#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300906 attr_t attr_old = A_NORMAL;
907 int use_xy = group_left_1, use_attr = group_right_1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100908 const char *funcname;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000909
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100910#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300911 strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100912#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300913 strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100914#endif
915 if (strtype == 0)
916 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000917
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300918 if (use_attr) {
Victor Stinner26486ea2010-05-15 22:23:53 +0000919 attr_old = getattrs(self->win);
920 (void)wattrset(self->win,attr);
921 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100922#ifdef HAVE_NCURSESW
923 if (strtype == 2) {
924 funcname = "addnwstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300925 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100926 rtn = mvwaddnwstr(self->win,y,x,wstr,n);
927 else
928 rtn = waddnwstr(self->win,wstr,n);
929 PyMem_Free(wstr);
930 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000931 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100932#endif
933 {
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +0300934 const char *str = PyBytes_AS_STRING(bytesobj);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100935 funcname = "addnstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300936 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100937 rtn = mvwaddnstr(self->win,y,x,str,n);
938 else
939 rtn = waddnstr(self->win,str,n);
940 Py_DECREF(bytesobj);
941 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300942 if (use_attr)
Victor Stinner26486ea2010-05-15 22:23:53 +0000943 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100944 return PyCursesCheckERR(rtn, funcname);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000945}
946
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300947/*[clinic input]
948_curses.window.bkgd
949
950 ch: object
951 Background character.
952 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
953 Background attributes.
954 /
955
956Set the background property of the window.
957[clinic start generated code]*/
958
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000959static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300960_curses_window_bkgd_impl(PyCursesWindowObject *self, PyObject *ch, long attr)
961/*[clinic end generated code: output=058290afb2cf4034 input=634015bcb339283d]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000962{
Victor Stinner26486ea2010-05-15 22:23:53 +0000963 chtype bkgd;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000964
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300965 if (!PyCurses_ConvertToChtype(self, ch, &bkgd))
Victor Stinner26486ea2010-05-15 22:23:53 +0000966 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000967
Victor Stinner26486ea2010-05-15 22:23:53 +0000968 return PyCursesCheckERR(wbkgd(self->win, bkgd | attr), "bkgd");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000969}
970
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300971/*[clinic input]
972_curses.window.attroff
973
974 attr: long
975 /
976
977Remove attribute attr from the "background" set.
978[clinic start generated code]*/
Christian Heimes2380ac72008-01-09 00:17:24 +0000979
980static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300981_curses_window_attroff_impl(PyCursesWindowObject *self, long attr)
982/*[clinic end generated code: output=8a2fcd4df682fc64 input=786beedf06a7befe]*/
Christian Heimes2380ac72008-01-09 00:17:24 +0000983{
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300984 return PyCursesCheckERR(wattroff(self->win, (attr_t)attr), "attroff");
Christian Heimes2380ac72008-01-09 00:17:24 +0000985}
986
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300987/*[clinic input]
988_curses.window.attron
989
990 attr: long
991 /
992
993Add attribute attr from the "background" set.
994[clinic start generated code]*/
Christian Heimes2380ac72008-01-09 00:17:24 +0000995
996static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300997_curses_window_attron_impl(PyCursesWindowObject *self, long attr)
998/*[clinic end generated code: output=7afea43b237fa870 input=5a88fba7b1524f32]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000999{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001000 return PyCursesCheckERR(wattron(self->win, (attr_t)attr), "attron");
1001}
1002
1003/*[clinic input]
1004_curses.window.attrset
1005
1006 attr: long
1007 /
1008
1009Set the "background" set of attributes.
1010[clinic start generated code]*/
1011
1012static PyObject *
1013_curses_window_attrset_impl(PyCursesWindowObject *self, long attr)
1014/*[clinic end generated code: output=84e379bff20c0433 input=42e400c0d0154ab5]*/
1015{
1016 return PyCursesCheckERR(wattrset(self->win, (attr_t)attr), "attrset");
1017}
1018
1019/*[clinic input]
1020_curses.window.bkgdset
1021
1022 ch: object
1023 Background character.
1024 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
1025 Background attributes.
1026 /
1027
1028Set the window's background.
1029[clinic start generated code]*/
1030
1031static PyObject *
1032_curses_window_bkgdset_impl(PyCursesWindowObject *self, PyObject *ch,
1033 long attr)
1034/*[clinic end generated code: output=8cb994fc4d7e2496 input=e09c682425c9e45b]*/
1035{
Victor Stinner26486ea2010-05-15 22:23:53 +00001036 chtype bkgd;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001037
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001038 if (!PyCurses_ConvertToChtype(self, ch, &bkgd))
Victor Stinner26486ea2010-05-15 22:23:53 +00001039 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001040
Victor Stinner26486ea2010-05-15 22:23:53 +00001041 wbkgdset(self->win, bkgd | attr);
1042 return PyCursesCheckERR(0, "bkgdset");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001043}
1044
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001045/*[clinic input]
1046_curses.window.border
1047
1048 ls: object(c_default="NULL") = _curses.ACS_VLINE
1049 Left side.
1050 rs: object(c_default="NULL") = _curses.ACS_VLINE
1051 Right side.
1052 ts: object(c_default="NULL") = _curses.ACS_HLINE
1053 Top side.
1054 bs: object(c_default="NULL") = _curses.ACS_HLINE
1055 Bottom side.
1056 tl: object(c_default="NULL") = _curses.ACS_ULCORNER
1057 Upper-left corner.
1058 tr: object(c_default="NULL") = _curses.ACS_URCORNER
1059 Upper-right corner.
1060 bl: object(c_default="NULL") = _curses.ACS_LLCORNER
1061 Bottom-left corner.
1062 br: object(c_default="NULL") = _curses.ACS_LRCORNER
1063 Bottom-right corner.
1064 /
1065
1066Draw a border around the edges of the window.
1067
1068Each parameter specifies the character to use for a specific part of the
1069border. The characters can be specified as integers or as one-character
1070strings. A 0 value for any parameter will cause the default character to be
1071used for that parameter.
1072[clinic start generated code]*/
1073
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001074static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001075_curses_window_border_impl(PyCursesWindowObject *self, PyObject *ls,
1076 PyObject *rs, PyObject *ts, PyObject *bs,
1077 PyObject *tl, PyObject *tr, PyObject *bl,
1078 PyObject *br)
1079/*[clinic end generated code: output=670ef38d3d7c2aa3 input=e015f735d67a240b]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001080{
Victor Stinner26486ea2010-05-15 22:23:53 +00001081 chtype ch[8];
1082 int i;
Andrew M. Kuchling16e65a02001-10-20 16:05:52 +00001083
Victor Stinner26486ea2010-05-15 22:23:53 +00001084 /* Clear the array of parameters */
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001085 for(i=0; i<8; i++)
Victor Stinner26486ea2010-05-15 22:23:53 +00001086 ch[i] = 0;
Andrew M. Kuchling16e65a02001-10-20 16:05:52 +00001087
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001088#define CONVERTTOCHTYPE(obj, i) \
1089 if ((obj) != NULL && !PyCurses_ConvertToChtype(self, (obj), &ch[(i)])) \
Victor Stinner26486ea2010-05-15 22:23:53 +00001090 return NULL;
1091
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001092 CONVERTTOCHTYPE(ls, 0);
1093 CONVERTTOCHTYPE(rs, 1);
1094 CONVERTTOCHTYPE(ts, 2);
1095 CONVERTTOCHTYPE(bs, 3);
1096 CONVERTTOCHTYPE(tl, 4);
1097 CONVERTTOCHTYPE(tr, 5);
1098 CONVERTTOCHTYPE(bl, 6);
1099 CONVERTTOCHTYPE(br, 7);
1100
1101#undef CONVERTTOCHTYPE
Victor Stinner26486ea2010-05-15 22:23:53 +00001102
1103 wborder(self->win,
1104 ch[0], ch[1], ch[2], ch[3],
1105 ch[4], ch[5], ch[6], ch[7]);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001106 Py_RETURN_NONE;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001107}
1108
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001109/*[clinic input]
1110_curses.window.box
1111
1112 [
Victor Stinner37834132020-10-27 17:12:53 +01001113 verch: object(c_default="_PyLong_GetZero()") = 0
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001114 Left and right side.
Victor Stinner37834132020-10-27 17:12:53 +01001115 horch: object(c_default="_PyLong_GetZero()") = 0
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001116 Top and bottom side.
1117 ]
1118 /
1119
1120Draw a border around the edges of the window.
1121
1122Similar to border(), but both ls and rs are verch and both ts and bs are
1123horch. The default corner characters are always used by this function.
1124[clinic start generated code]*/
1125
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001126static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001127_curses_window_box_impl(PyCursesWindowObject *self, int group_right_1,
1128 PyObject *verch, PyObject *horch)
Victor Stinner37834132020-10-27 17:12:53 +01001129/*[clinic end generated code: output=f3fcb038bb287192 input=f00435f9c8c98f60]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001130{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001131 chtype ch1 = 0, ch2 = 0;
1132 if (group_right_1) {
1133 if (!PyCurses_ConvertToChtype(self, verch, &ch1)) {
Serhiy Storchaka4f469c02017-11-01 20:48:49 +02001134 return NULL;
1135 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001136 if (!PyCurses_ConvertToChtype(self, horch, &ch2)) {
Serhiy Storchaka4f469c02017-11-01 20:48:49 +02001137 return NULL;
1138 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001139 }
1140 box(self->win,ch1,ch2);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001141 Py_RETURN_NONE;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001142}
1143
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001144#if defined(HAVE_NCURSES_H) || defined(MVWDELCH_IS_EXPRESSION)
1145#define py_mvwdelch mvwdelch
1146#else
1147int py_mvwdelch(WINDOW *w, int y, int x)
1148{
Victor Stinner26486ea2010-05-15 22:23:53 +00001149 mvwdelch(w,y,x);
1150 /* On HP/UX, mvwdelch already returns. On other systems,
1151 we may well run into this return statement. */
1152 return 0;
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001153}
1154#endif
1155
Masayuki Yamamoto8bc7d632017-11-01 21:05:26 +09001156#if defined(HAVE_CURSES_IS_PAD)
1157#define py_is_pad(win) is_pad(win)
1158#elif defined(WINDOW_HAS_FLAGS)
1159#define py_is_pad(win) ((win) ? ((win)->_flags & _ISPAD) != 0 : FALSE)
1160#endif
1161
Guido van Rossumd8faa362007-04-27 19:54:29 +00001162/* chgat, added by Fabian Kreutz <fabian.kreutz at gmx.net> */
Serhiy Storchaka894ebd02017-11-01 14:34:20 +02001163#ifdef HAVE_CURSES_WCHGAT
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001164/*[-clinic input]
1165_curses.window.chgat
1166
1167 [
1168 y: int
1169 Y-coordinate.
1170 x: int
1171 X-coordinate.
1172 ]
1173
1174 n: int = -1
1175 Number of characters.
1176
1177 attr: long
1178 Attributes for characters.
1179 /
1180
1181Set the attributes of characters.
1182
1183Set the attributes of num characters at the current cursor position, or at
1184position (y, x) if supplied. If no value of num is given or num = -1, the
1185attribute will be set on all the characters to the end of the line. This
1186function does not move the cursor. The changed line will be touched using
1187the touchline() method so that the contents will be redisplayed by the next
1188window refresh.
1189[-clinic start generated code]*/
Guido van Rossumd8faa362007-04-27 19:54:29 +00001190static PyObject *
1191PyCursesWindow_ChgAt(PyCursesWindowObject *self, PyObject *args)
1192{
Victor Stinner26486ea2010-05-15 22:23:53 +00001193 int rtn;
1194 int x, y;
1195 int num = -1;
1196 short color;
1197 attr_t attr = A_NORMAL;
1198 long lattr;
1199 int use_xy = FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001200
Victor Stinner26486ea2010-05-15 22:23:53 +00001201 switch (PyTuple_Size(args)) {
1202 case 1:
1203 if (!PyArg_ParseTuple(args,"l;attr", &lattr))
1204 return NULL;
1205 attr = lattr;
1206 break;
1207 case 2:
1208 if (!PyArg_ParseTuple(args,"il;n,attr", &num, &lattr))
1209 return NULL;
1210 attr = lattr;
1211 break;
1212 case 3:
1213 if (!PyArg_ParseTuple(args,"iil;int,int,attr", &y, &x, &lattr))
1214 return NULL;
1215 attr = lattr;
1216 use_xy = TRUE;
1217 break;
1218 case 4:
1219 if (!PyArg_ParseTuple(args,"iiil;int,int,n,attr", &y, &x, &num, &lattr))
1220 return NULL;
1221 attr = lattr;
1222 use_xy = TRUE;
1223 break;
1224 default:
1225 PyErr_SetString(PyExc_TypeError, "chgat requires 1 to 4 arguments");
1226 return NULL;
1227 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001228
Victor Stinner26486ea2010-05-15 22:23:53 +00001229 color = (short)((attr >> 8) & 0xff);
1230 attr = attr - (color << 8);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001231
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001232 if (use_xy) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001233 rtn = mvwchgat(self->win,y,x,num,attr,color,NULL);
1234 touchline(self->win,y,1);
1235 } else {
1236 getyx(self->win,y,x);
1237 rtn = wchgat(self->win,num,attr,color,NULL);
1238 touchline(self->win,y,1);
1239 }
1240 return PyCursesCheckERR(rtn, "chgat");
Guido van Rossumd8faa362007-04-27 19:54:29 +00001241}
Serhiy Storchaka894ebd02017-11-01 14:34:20 +02001242#endif
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001243
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001244/*[clinic input]
1245_curses.window.delch
Guido van Rossum85738471995-02-17 13:50:17 +00001246
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001247 [
1248 y: int
1249 Y-coordinate.
1250 x: int
1251 X-coordinate.
1252 ]
1253 /
1254
1255Delete any character at (y, x).
1256[clinic start generated code]*/
1257
1258static PyObject *
1259_curses_window_delch_impl(PyCursesWindowObject *self, int group_right_1,
1260 int y, int x)
1261/*[clinic end generated code: output=22e77bb9fa11b461 input=d2f79e630a4fc6d0]*/
1262{
1263 if (!group_right_1) {
1264 return PyCursesCheckERR(wdelch(self->win), "wdelch");
Victor Stinner26486ea2010-05-15 22:23:53 +00001265 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001266 else {
1267 return PyCursesCheckERR(py_mvwdelch(self->win, y, x), "mvwdelch");
1268 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001269}
1270
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001271/*[clinic input]
1272_curses.window.derwin
1273
1274 [
1275 nlines: int = 0
1276 Height.
1277 ncols: int = 0
1278 Width.
1279 ]
1280 begin_y: int
1281 Top side y-coordinate.
1282 begin_x: int
1283 Left side x-coordinate.
1284 /
1285
1286Create a sub-window (window-relative coordinates).
1287
1288derwin() is the same as calling subwin(), except that begin_y and begin_x
1289are relative to the origin of the window, rather than relative to the entire
1290screen.
1291[clinic start generated code]*/
1292
Guido van Rossumf6971e21994-08-30 12:25:20 +00001293static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001294_curses_window_derwin_impl(PyCursesWindowObject *self, int group_left_1,
1295 int nlines, int ncols, int begin_y, int begin_x)
1296/*[clinic end generated code: output=7924b112d9f70d6e input=966d9481f7f5022e]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001297{
Victor Stinner26486ea2010-05-15 22:23:53 +00001298 WINDOW *win;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001299
Victor Stinner26486ea2010-05-15 22:23:53 +00001300 win = derwin(self->win,nlines,ncols,begin_y,begin_x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001301
Victor Stinner26486ea2010-05-15 22:23:53 +00001302 if (win == NULL) {
1303 PyErr_SetString(PyCursesError, catchall_NULL);
1304 return NULL;
1305 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001306
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001307 return (PyObject *)PyCursesWindow_New(win, NULL);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001308}
1309
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001310/*[clinic input]
1311_curses.window.echochar
1312
1313 ch: object
1314 Character to add.
1315
1316 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
1317 Attributes for the character.
1318 /
1319
1320Add character ch with attribute attr, and refresh.
1321[clinic start generated code]*/
1322
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001323static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001324_curses_window_echochar_impl(PyCursesWindowObject *self, PyObject *ch,
1325 long attr)
1326/*[clinic end generated code: output=13e7dd875d4b9642 input=e7f34b964e92b156]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001327{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001328 chtype ch_;
Guido van Rossum85738471995-02-17 13:50:17 +00001329
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001330 if (!PyCurses_ConvertToChtype(self, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00001331 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00001332
Masayuki Yamamoto8bc7d632017-11-01 21:05:26 +09001333#ifdef py_is_pad
1334 if (py_is_pad(self->win)) {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001335 return PyCursesCheckERR(pechochar(self->win, ch_ | (attr_t)attr),
Victor Stinner26486ea2010-05-15 22:23:53 +00001336 "echochar");
Masayuki Yamamoto8bc7d632017-11-01 21:05:26 +09001337 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001338 else
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001339#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001340 return PyCursesCheckERR(wechochar(self->win, ch_ | (attr_t)attr),
Victor Stinner26486ea2010-05-15 22:23:53 +00001341 "echochar");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001342}
1343
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001344#ifdef NCURSES_MOUSE_VERSION
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001345/*[clinic input]
Serhiy Storchakab1dc1aa2021-04-05 16:50:24 +03001346_curses.window.enclose
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001347
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001348 y: int
1349 Y-coordinate.
1350 x: int
1351 X-coordinate.
1352 /
1353
1354Return True if the screen-relative coordinates are enclosed by the window.
1355[clinic start generated code]*/
1356
Serhiy Storchakab1dc1aa2021-04-05 16:50:24 +03001357static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001358_curses_window_enclose_impl(PyCursesWindowObject *self, int y, int x)
Serhiy Storchakab1dc1aa2021-04-05 16:50:24 +03001359/*[clinic end generated code: output=8679beef50502648 input=4fd3355d723f7bc9]*/
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001360{
Serhiy Storchakab1dc1aa2021-04-05 16:50:24 +03001361 return PyBool_FromLong(wenclose(self->win, y, x));
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001362}
1363#endif
1364
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001365/*[clinic input]
1366_curses.window.getbkgd -> long
1367
1368Return the window's current background character/attribute pair.
1369[clinic start generated code]*/
1370
1371static long
1372_curses_window_getbkgd_impl(PyCursesWindowObject *self)
1373/*[clinic end generated code: output=c52b25dc16b215c3 input=a69db882fa35426c]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001374{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001375 return (long) getbkgd(self->win);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001376}
1377
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001378/*[clinic input]
1379_curses.window.getch -> int
1380
1381 [
1382 y: int
1383 Y-coordinate.
1384 x: int
1385 X-coordinate.
1386 ]
1387 /
1388
1389Get a character code from terminal keyboard.
1390
1391The integer returned does not have to be in ASCII range: function keys,
1392keypad keys and so on return numbers higher than 256. In no-delay mode, -1
1393is returned if there is no input, else getch() waits until a key is pressed.
1394[clinic start generated code]*/
1395
1396static int
1397_curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
1398 int y, int x)
1399/*[clinic end generated code: output=980aa6af0c0ca387 input=bb24ebfb379f991f]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001400{
Victor Stinner26486ea2010-05-15 22:23:53 +00001401 int rtn;
Guido van Rossum85738471995-02-17 13:50:17 +00001402
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001403 Py_BEGIN_ALLOW_THREADS
1404 if (!group_right_1) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001405 rtn = wgetch(self->win);
Victor Stinner26486ea2010-05-15 22:23:53 +00001406 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001407 else {
1408 rtn = mvwgetch(self->win, y, x);
1409 }
1410 Py_END_ALLOW_THREADS
1411
1412 return rtn;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001413}
Guido van Rossum85738471995-02-17 13:50:17 +00001414
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001415/*[clinic input]
1416_curses.window.getkey
1417
1418 [
1419 y: int
1420 Y-coordinate.
1421 x: int
1422 X-coordinate.
1423 ]
1424 /
1425
1426Get a character (string) from terminal keyboard.
1427
1428Returning a string instead of an integer, as getch() does. Function keys,
1429keypad keys and other special keys return a multibyte string containing the
1430key name. In no-delay mode, an exception is raised if there is no input.
1431[clinic start generated code]*/
1432
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001433static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001434_curses_window_getkey_impl(PyCursesWindowObject *self, int group_right_1,
1435 int y, int x)
1436/*[clinic end generated code: output=8490a182db46b10f input=be2dee34f5cf57f8]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001437{
Victor Stinner26486ea2010-05-15 22:23:53 +00001438 int rtn;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001439
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001440 Py_BEGIN_ALLOW_THREADS
1441 if (!group_right_1) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001442 rtn = wgetch(self->win);
Victor Stinner26486ea2010-05-15 22:23:53 +00001443 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001444 else {
1445 rtn = mvwgetch(self->win, y, x);
1446 }
1447 Py_END_ALLOW_THREADS
1448
Victor Stinner26486ea2010-05-15 22:23:53 +00001449 if (rtn == ERR) {
1450 /* getch() returns ERR in nodelay mode */
R David Murrayf5d7cc22013-03-19 16:23:09 -04001451 PyErr_CheckSignals();
1452 if (!PyErr_Occurred())
1453 PyErr_SetString(PyCursesError, "no input");
Victor Stinner26486ea2010-05-15 22:23:53 +00001454 return NULL;
Serhiy Storchaka7e687902017-11-01 16:03:40 +02001455 } else if (rtn <= 255) {
1456#ifdef NCURSES_VERSION_MAJOR
1457#if NCURSES_VERSION_MAJOR*100+NCURSES_VERSION_MINOR <= 507
1458 /* Work around a bug in ncurses 5.7 and earlier */
1459 if (rtn < 0) {
1460 rtn += 256;
1461 }
1462#endif
1463#endif
1464 return PyUnicode_FromOrdinal(rtn);
Victor Stinner26486ea2010-05-15 22:23:53 +00001465 } else {
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02001466 const char *knp = keyname(rtn);
Victor Stinner26486ea2010-05-15 22:23:53 +00001467 return PyUnicode_FromString((knp == NULL) ? "" : knp);
1468 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001469}
1470
Nadeem Vawda9e2e9902011-07-31 15:01:11 +02001471#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001472/*[clinic input]
1473_curses.window.get_wch
1474
1475 [
1476 y: int
1477 Y-coordinate.
1478 x: int
1479 X-coordinate.
1480 ]
1481 /
1482
1483Get a wide character from terminal keyboard.
1484
1485Return a character for most keys, or an integer for function keys,
1486keypad keys, and other special keys.
1487[clinic start generated code]*/
1488
Guido van Rossumf6971e21994-08-30 12:25:20 +00001489static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001490_curses_window_get_wch_impl(PyCursesWindowObject *self, int group_right_1,
1491 int y, int x)
1492/*[clinic end generated code: output=9f4f86e91fe50ef3 input=dd7e5367fb49dc48]*/
Victor Stinnera7878b72011-07-14 23:07:44 +02001493{
Victor Stinnera7878b72011-07-14 23:07:44 +02001494 int ct;
1495 wint_t rtn;
1496
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001497 Py_BEGIN_ALLOW_THREADS
1498 if (!group_right_1) {
1499 ct = wget_wch(self->win ,&rtn);
Victor Stinnera7878b72011-07-14 23:07:44 +02001500 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001501 else {
1502 ct = mvwget_wch(self->win, y, x, &rtn);
1503 }
1504 Py_END_ALLOW_THREADS
1505
Victor Stinnera7878b72011-07-14 23:07:44 +02001506 if (ct == ERR) {
Victor Stinnerbd2d30c2013-03-21 12:21:06 +01001507 if (PyErr_CheckSignals())
1508 return NULL;
1509
Victor Stinnera7878b72011-07-14 23:07:44 +02001510 /* get_wch() returns ERR in nodelay mode */
1511 PyErr_SetString(PyCursesError, "no input");
1512 return NULL;
1513 }
Victor Stinner1d39cde2012-08-29 01:40:57 +02001514 if (ct == KEY_CODE_YES)
1515 return PyLong_FromLong(rtn);
1516 else
1517 return PyUnicode_FromOrdinal(rtn);
Victor Stinnera7878b72011-07-14 23:07:44 +02001518}
Nadeem Vawda9e2e9902011-07-31 15:01:11 +02001519#endif
Victor Stinnera7878b72011-07-14 23:07:44 +02001520
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001521/*[-clinic input]
1522_curses.window.getstr
1523
1524 [
1525 y: int
1526 Y-coordinate.
1527 x: int
1528 X-coordinate.
1529 ]
1530 n: int = 1023
1531 Maximal number of characters.
1532 /
1533
1534Read a string from the user, with primitive line editing capacity.
1535[-clinic start generated code]*/
1536
Victor Stinnera7878b72011-07-14 23:07:44 +02001537static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001538PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001539{
Victor Stinner26486ea2010-05-15 22:23:53 +00001540 int x, y, n;
1541 char rtn[1024]; /* This should be big enough.. I hope */
1542 int rtn2;
Guido van Rossum85738471995-02-17 13:50:17 +00001543
Victor Stinner26486ea2010-05-15 22:23:53 +00001544 switch (PyTuple_Size(args)) {
1545 case 0:
1546 Py_BEGIN_ALLOW_THREADS
1547 rtn2 = wgetnstr(self->win,rtn, 1023);
1548 Py_END_ALLOW_THREADS
1549 break;
1550 case 1:
1551 if (!PyArg_ParseTuple(args,"i;n", &n))
1552 return NULL;
Benjamin Peterson40a77c32016-08-13 18:15:28 -07001553 if (n < 0) {
1554 PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1555 return NULL;
1556 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001557 Py_BEGIN_ALLOW_THREADS
Victor Stinner640c35c2013-06-04 23:14:37 +02001558 rtn2 = wgetnstr(self->win, rtn, Py_MIN(n, 1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001559 Py_END_ALLOW_THREADS
1560 break;
1561 case 2:
1562 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1563 return NULL;
1564 Py_BEGIN_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001565#ifdef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00001566 rtn2 = wmove(self->win,y,x)==ERR ? ERR : wgetnstr(self->win, rtn, 1023);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001567#else
Victor Stinner26486ea2010-05-15 22:23:53 +00001568 rtn2 = mvwgetnstr(self->win,y,x,rtn, 1023);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001569#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001570 Py_END_ALLOW_THREADS
1571 break;
1572 case 3:
1573 if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
1574 return NULL;
Benjamin Peterson40a77c32016-08-13 18:15:28 -07001575 if (n < 0) {
1576 PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1577 return NULL;
1578 }
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +00001579#ifdef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00001580 Py_BEGIN_ALLOW_THREADS
1581 rtn2 = wmove(self->win,y,x)==ERR ? ERR :
Victor Stinner640c35c2013-06-04 23:14:37 +02001582 wgetnstr(self->win, rtn, Py_MIN(n, 1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001583 Py_END_ALLOW_THREADS
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001584#else
Victor Stinner26486ea2010-05-15 22:23:53 +00001585 Py_BEGIN_ALLOW_THREADS
Victor Stinner640c35c2013-06-04 23:14:37 +02001586 rtn2 = mvwgetnstr(self->win, y, x, rtn, Py_MIN(n, 1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001587 Py_END_ALLOW_THREADS
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001588#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001589 break;
1590 default:
1591 PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 3 arguments");
1592 return NULL;
1593 }
1594 if (rtn2 == ERR)
1595 rtn[0] = 0;
1596 return PyBytes_FromString(rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001597}
1598
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001599/*[clinic input]
1600_curses.window.hline
1601
1602 [
1603 y: int
1604 Starting Y-coordinate.
1605 x: int
1606 Starting X-coordinate.
1607 ]
1608
1609 ch: object
1610 Character to draw.
1611 n: int
1612 Line length.
1613
1614 [
1615 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
1616 Attributes for the characters.
1617 ]
1618 /
1619
1620Display a horizontal line.
1621[clinic start generated code]*/
1622
Guido van Rossumf6971e21994-08-30 12:25:20 +00001623static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001624_curses_window_hline_impl(PyCursesWindowObject *self, int group_left_1,
1625 int y, int x, PyObject *ch, int n,
1626 int group_right_1, long attr)
1627/*[clinic end generated code: output=c00d489d61fc9eef input=81a4dea47268163e]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001628{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001629 chtype ch_;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001630
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001631 if (!PyCurses_ConvertToChtype(self, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00001632 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001633 if (group_left_1) {
1634 if (wmove(self->win, y, x) == ERR) {
1635 return PyCursesCheckERR(ERR, "wmove");
1636 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001637 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001638 return PyCursesCheckERR(whline(self->win, ch_ | (attr_t)attr, n), "hline");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001639}
1640
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001641/*[clinic input]
1642_curses.window.insch
1643
1644 [
1645 y: int
1646 Y-coordinate.
1647 x: int
1648 X-coordinate.
1649 ]
1650
1651 ch: object
1652 Character to insert.
1653
1654 [
1655 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
1656 Attributes for the character.
1657 ]
1658 /
1659
1660Insert a character before the current or specified position.
1661
1662All characters to the right of the cursor are shifted one position right, with
1663the rightmost characters on the line being lost.
1664[clinic start generated code]*/
1665
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001666static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001667_curses_window_insch_impl(PyCursesWindowObject *self, int group_left_1,
1668 int y, int x, PyObject *ch, int group_right_1,
1669 long attr)
1670/*[clinic end generated code: output=ade8cfe3a3bf3e34 input=336342756ee19812]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001671{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001672 int rtn;
1673 chtype ch_ = 0;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001674
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001675 if (!PyCurses_ConvertToChtype(self, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00001676 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001677
1678 if (!group_left_1) {
1679 rtn = winsch(self->win, ch_ | (attr_t)attr);
Victor Stinner26486ea2010-05-15 22:23:53 +00001680 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001681 else {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001682 rtn = mvwinsch(self->win, y, x, ch_ | (attr_t)attr);
Victor Stinner26486ea2010-05-15 22:23:53 +00001683 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001684
Victor Stinner26486ea2010-05-15 22:23:53 +00001685 return PyCursesCheckERR(rtn, "insch");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001686}
1687
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001688/*[clinic input]
1689_curses.window.inch -> unsigned_long
1690
1691 [
1692 y: int
1693 Y-coordinate.
1694 x: int
1695 X-coordinate.
1696 ]
1697 /
1698
1699Return the character at the given position in the window.
1700
1701The bottom 8 bits are the character proper, and upper bits are the attributes.
1702[clinic start generated code]*/
1703
1704static unsigned long
1705_curses_window_inch_impl(PyCursesWindowObject *self, int group_right_1,
1706 int y, int x)
1707/*[clinic end generated code: output=6c4719fe978fe86a input=fac23ee11e3b3a66]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001708{
Christian Heimesabbc8ca2013-12-04 08:50:22 +01001709 unsigned long rtn;
Guido van Rossum85738471995-02-17 13:50:17 +00001710
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001711 if (!group_right_1) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001712 rtn = winch(self->win);
Victor Stinner26486ea2010-05-15 22:23:53 +00001713 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001714 else {
1715 rtn = mvwinch(self->win, y, x);
1716 }
1717
1718 return rtn;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001719}
1720
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001721/*[-clinic input]
1722_curses.window.instr
1723
1724 [
1725 y: int
1726 Y-coordinate.
1727 x: int
1728 X-coordinate.
1729 ]
1730 n: int = 1023
1731 Maximal number of characters.
1732 /
1733
1734Return a string of characters, extracted from the window.
1735
1736Return a string of characters, extracted from the window starting at the
1737current cursor position, or at y, x if specified. Attributes are stripped
1738from the characters. If n is specified, instr() returns a string at most
1739n characters long (exclusive of the trailing NUL).
1740[-clinic start generated code]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001741static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001742PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001743{
Victor Stinner26486ea2010-05-15 22:23:53 +00001744 int x, y, n;
1745 char rtn[1024]; /* This should be big enough.. I hope */
1746 int rtn2;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001747
Victor Stinner26486ea2010-05-15 22:23:53 +00001748 switch (PyTuple_Size(args)) {
1749 case 0:
1750 rtn2 = winnstr(self->win,rtn, 1023);
1751 break;
1752 case 1:
1753 if (!PyArg_ParseTuple(args,"i;n", &n))
1754 return NULL;
Benjamin Peterson432ea4f2016-08-15 21:40:14 -07001755 if (n < 0) {
1756 PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1757 return NULL;
1758 }
Victor Stinner640c35c2013-06-04 23:14:37 +02001759 rtn2 = winnstr(self->win, rtn, Py_MIN(n, 1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001760 break;
1761 case 2:
1762 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1763 return NULL;
1764 rtn2 = mvwinnstr(self->win,y,x,rtn,1023);
1765 break;
1766 case 3:
1767 if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
1768 return NULL;
Benjamin Peterson432ea4f2016-08-15 21:40:14 -07001769 if (n < 0) {
1770 PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1771 return NULL;
1772 }
Victor Stinner640c35c2013-06-04 23:14:37 +02001773 rtn2 = mvwinnstr(self->win, y, x, rtn, Py_MIN(n,1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001774 break;
1775 default:
1776 PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments");
1777 return NULL;
1778 }
1779 if (rtn2 == ERR)
1780 rtn[0] = 0;
1781 return PyBytes_FromString(rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001782}
1783
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001784/*[clinic input]
1785_curses.window.insstr
1786
1787 [
1788 y: int
1789 Y-coordinate.
1790 x: int
1791 X-coordinate.
1792 ]
1793
1794 str: object
1795 String to insert.
1796
1797 [
1798 attr: long
1799 Attributes for characters.
1800 ]
1801 /
1802
1803Insert the string before the current or specified position.
1804
1805Insert a character string (as many characters as will fit on the line)
1806before the character under the cursor. All characters to the right of
1807the cursor are shifted right, with the rightmost characters on the line
1808being lost. The cursor position does not change (after moving to y, x,
1809if specified).
1810[clinic start generated code]*/
1811
Guido van Rossumf6971e21994-08-30 12:25:20 +00001812static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001813_curses_window_insstr_impl(PyCursesWindowObject *self, int group_left_1,
1814 int y, int x, PyObject *str, int group_right_1,
1815 long attr)
1816/*[clinic end generated code: output=c259a5265ad0b777 input=6827cddc6340a7f3]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001817{
Victor Stinner26486ea2010-05-15 22:23:53 +00001818 int rtn;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001819 int strtype;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001820 PyObject *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001821#ifdef HAVE_NCURSESW
1822 wchar_t *wstr = NULL;
1823#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001824 attr_t attr_old = A_NORMAL;
1825 int use_xy = group_left_1, use_attr = group_right_1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001826 const char *funcname;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001827
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001828#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001829 strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001830#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001831 strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001832#endif
1833 if (strtype == 0)
1834 return NULL;
1835
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001836 if (use_attr) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001837 attr_old = getattrs(self->win);
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001838 (void)wattrset(self->win, (attr_t)attr);
Victor Stinner26486ea2010-05-15 22:23:53 +00001839 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001840#ifdef HAVE_NCURSESW
1841 if (strtype == 2) {
1842 funcname = "inswstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001843 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001844 rtn = mvwins_wstr(self->win,y,x,wstr);
1845 else
1846 rtn = wins_wstr(self->win,wstr);
1847 PyMem_Free(wstr);
1848 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001849 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001850#endif
1851 {
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +03001852 const char *str = PyBytes_AS_STRING(bytesobj);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001853 funcname = "insstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001854 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001855 rtn = mvwinsstr(self->win,y,x,str);
1856 else
1857 rtn = winsstr(self->win,str);
1858 Py_DECREF(bytesobj);
1859 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001860 if (use_attr)
Victor Stinner26486ea2010-05-15 22:23:53 +00001861 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001862 return PyCursesCheckERR(rtn, funcname);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001863}
1864
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001865/*[clinic input]
1866_curses.window.insnstr
1867
1868 [
1869 y: int
1870 Y-coordinate.
1871 x: int
1872 X-coordinate.
1873 ]
1874
1875 str: object
1876 String to insert.
1877
1878 n: int
1879 Maximal number of characters.
1880
1881 [
1882 attr: long
1883 Attributes for characters.
1884 ]
1885 /
1886
1887Insert at most n characters of the string.
1888
1889Insert a character string (as many characters as will fit on the line)
1890before the character under the cursor, up to n characters. If n is zero
1891or negative, the entire string is inserted. All characters to the right
1892of the cursor are shifted right, with the rightmost characters on the line
1893being lost. The cursor position does not change (after moving to y, x, if
1894specified).
1895[clinic start generated code]*/
1896
Guido van Rossumf6971e21994-08-30 12:25:20 +00001897static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001898_curses_window_insnstr_impl(PyCursesWindowObject *self, int group_left_1,
1899 int y, int x, PyObject *str, int n,
1900 int group_right_1, long attr)
1901/*[clinic end generated code: output=971a32ea6328ec8b input=70fa0cd543901a4c]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001902{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001903 int rtn;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001904 int strtype;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001905 PyObject *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001906#ifdef HAVE_NCURSESW
1907 wchar_t *wstr = NULL;
1908#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001909 attr_t attr_old = A_NORMAL;
1910 int use_xy = group_left_1, use_attr = group_right_1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001911 const char *funcname;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001912
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001913#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001914 strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001915#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001916 strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001917#endif
1918 if (strtype == 0)
1919 return NULL;
1920
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001921 if (use_attr) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001922 attr_old = getattrs(self->win);
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001923 (void)wattrset(self->win, (attr_t)attr);
Victor Stinner26486ea2010-05-15 22:23:53 +00001924 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001925#ifdef HAVE_NCURSESW
1926 if (strtype == 2) {
1927 funcname = "insn_wstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001928 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001929 rtn = mvwins_nwstr(self->win,y,x,wstr,n);
1930 else
1931 rtn = wins_nwstr(self->win,wstr,n);
1932 PyMem_Free(wstr);
1933 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001934 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001935#endif
1936 {
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +03001937 const char *str = PyBytes_AS_STRING(bytesobj);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001938 funcname = "insnstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001939 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001940 rtn = mvwinsnstr(self->win,y,x,str,n);
1941 else
1942 rtn = winsnstr(self->win,str,n);
1943 Py_DECREF(bytesobj);
1944 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001945 if (use_attr)
Victor Stinner26486ea2010-05-15 22:23:53 +00001946 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001947 return PyCursesCheckERR(rtn, funcname);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001948}
1949
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001950/*[clinic input]
1951_curses.window.is_linetouched
1952
1953 line: int
1954 Line number.
1955 /
1956
1957Return True if the specified line was modified, otherwise return False.
1958
1959Raise a curses.error exception if line is not valid for the given window.
1960[clinic start generated code]*/
1961
Guido van Rossumf6971e21994-08-30 12:25:20 +00001962static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001963_curses_window_is_linetouched_impl(PyCursesWindowObject *self, int line)
1964/*[clinic end generated code: output=ad4a4edfee2db08c input=a7be0c189f243914]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001965{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001966 int erg;
Victor Stinner26486ea2010-05-15 22:23:53 +00001967 erg = is_linetouched(self->win, line);
1968 if (erg == ERR) {
1969 PyErr_SetString(PyExc_TypeError,
1970 "is_linetouched: line number outside of boundaries");
1971 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001972 }
1973 return PyBool_FromLong(erg);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001974}
1975
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001976#ifdef py_is_pad
1977/*[clinic input]
1978_curses.window.noutrefresh
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001979
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001980 [
1981 pminrow: int
1982 pmincol: int
1983 sminrow: int
1984 smincol: int
1985 smaxrow: int
1986 smaxcol: int
1987 ]
1988 /
1989
1990Mark for refresh but wait.
1991
1992This function updates the data structure representing the desired state of the
1993window, but does not force an update of the physical screen. To accomplish
1994that, call doupdate().
1995[clinic start generated code]*/
1996
1997static PyObject *
1998_curses_window_noutrefresh_impl(PyCursesWindowObject *self,
1999 int group_right_1, int pminrow, int pmincol,
2000 int sminrow, int smincol, int smaxrow,
2001 int smaxcol)
2002/*[clinic end generated code: output=809a1f3c6a03e23e input=3e56898388cd739e]*/
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00002003#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002004/*[clinic input]
2005_curses.window.noutrefresh
Andrew M. Kuchlingf4195722000-06-23 01:36:21 +00002006
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002007Mark for refresh but wait.
2008
2009This function updates the data structure representing the desired state of the
2010window, but does not force an update of the physical screen. To accomplish
2011that, call doupdate().
2012[clinic start generated code]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002013
2014static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002015_curses_window_noutrefresh_impl(PyCursesWindowObject *self)
2016/*[clinic end generated code: output=6ef6dec666643fee input=876902e3fa431dbd]*/
2017#endif
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00002018{
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00002019 int rtn;
Victor Stinner26486ea2010-05-15 22:23:53 +00002020
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002021#ifdef py_is_pad
2022 if (py_is_pad(self->win)) {
2023 if (!group_right_1) {
2024 PyErr_SetString(PyCursesError,
2025 "noutrefresh() called for a pad "
2026 "requires 6 arguments");
Victor Stinner26486ea2010-05-15 22:23:53 +00002027 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002028 }
2029 Py_BEGIN_ALLOW_THREADS
2030 rtn = pnoutrefresh(self->win, pminrow, pmincol,
2031 sminrow, smincol, smaxrow, smaxcol);
2032 Py_END_ALLOW_THREADS
2033 return PyCursesCheckERR(rtn, "pnoutrefresh");
2034 }
2035 if (group_right_1) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002036 PyErr_SetString(PyExc_TypeError,
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002037 "noutrefresh() takes no arguments (6 given)");
Victor Stinner26486ea2010-05-15 22:23:53 +00002038 return NULL;
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00002039 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002040#endif
2041 Py_BEGIN_ALLOW_THREADS
2042 rtn = wnoutrefresh(self->win);
2043 Py_END_ALLOW_THREADS
2044 return PyCursesCheckERR(rtn, "wnoutrefresh");
2045}
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00002046
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002047/*[clinic input]
2048_curses.window.overlay
2049
2050 destwin: object(type="PyCursesWindowObject *", subclass_of="&PyCursesWindow_Type")
2051
2052 [
2053 sminrow: int
2054 smincol: int
2055 dminrow: int
2056 dmincol: int
2057 dmaxrow: int
2058 dmaxcol: int
2059 ]
2060 /
2061
2062Overlay the window on top of destwin.
2063
2064The windows need not be the same size, only the overlapping region is copied.
2065This copy is non-destructive, which means that the current background
2066character does not overwrite the old contents of destwin.
2067
2068To get fine-grained control over the copied region, the second form of
2069overlay() can be used. sminrow and smincol are the upper-left coordinates
2070of the source window, and the other variables mark a rectangle in the
2071destination window.
2072[clinic start generated code]*/
2073
2074static PyObject *
2075_curses_window_overlay_impl(PyCursesWindowObject *self,
2076 PyCursesWindowObject *destwin, int group_right_1,
2077 int sminrow, int smincol, int dminrow,
2078 int dmincol, int dmaxrow, int dmaxcol)
2079/*[clinic end generated code: output=82bb2c4cb443ca58 input=7edd23ad22cc1984]*/
2080{
2081 int rtn;
2082
2083 if (group_right_1) {
2084 rtn = copywin(self->win, destwin->win, sminrow, smincol,
Victor Stinner26486ea2010-05-15 22:23:53 +00002085 dminrow, dmincol, dmaxrow, dmaxcol, TRUE);
2086 return PyCursesCheckERR(rtn, "copywin");
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00002087 }
2088 else {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002089 rtn = overlay(self->win, destwin->win);
Victor Stinner26486ea2010-05-15 22:23:53 +00002090 return PyCursesCheckERR(rtn, "overlay");
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00002091 }
2092}
2093
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002094/*[clinic input]
2095_curses.window.overwrite
2096
2097 destwin: object(type="PyCursesWindowObject *", subclass_of="&PyCursesWindow_Type")
2098
2099 [
2100 sminrow: int
2101 smincol: int
2102 dminrow: int
2103 dmincol: int
2104 dmaxrow: int
2105 dmaxcol: int
2106 ]
2107 /
2108
2109Overwrite the window on top of destwin.
2110
2111The windows need not be the same size, in which case only the overlapping
2112region is copied. This copy is destructive, which means that the current
2113background character overwrites the old contents of destwin.
2114
2115To get fine-grained control over the copied region, the second form of
2116overwrite() can be used. sminrow and smincol are the upper-left coordinates
2117of the source window, the other variables mark a rectangle in the destination
2118window.
2119[clinic start generated code]*/
2120
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00002121static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002122_curses_window_overwrite_impl(PyCursesWindowObject *self,
2123 PyCursesWindowObject *destwin,
2124 int group_right_1, int sminrow, int smincol,
2125 int dminrow, int dmincol, int dmaxrow,
2126 int dmaxcol)
2127/*[clinic end generated code: output=12ae007d1681be28 input=ea5de1b35cd948e0]*/
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00002128{
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00002129 int rtn;
Victor Stinner26486ea2010-05-15 22:23:53 +00002130
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002131 if (group_right_1) {
2132 rtn = copywin(self->win, destwin->win, sminrow, smincol,
Victor Stinner26486ea2010-05-15 22:23:53 +00002133 dminrow, dmincol, dmaxrow, dmaxcol, FALSE);
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00002134 return PyCursesCheckERR(rtn, "copywin");
2135 }
2136 else {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002137 rtn = overwrite(self->win, destwin->win);
Victor Stinner26486ea2010-05-15 22:23:53 +00002138 return PyCursesCheckERR(rtn, "overwrite");
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00002139 }
2140}
2141
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002142/*[clinic input]
2143_curses.window.putwin
2144
2145 file: object
2146 /
2147
2148Write all data associated with the window into the provided file object.
2149
2150This information can be later retrieved using the getwin() function.
2151[clinic start generated code]*/
2152
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00002153static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002154_curses_window_putwin(PyCursesWindowObject *self, PyObject *file)
2155/*[clinic end generated code: output=3a25e2a5e7a040ac input=0608648e09c8ea0a]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002156{
Victor Stinner26486ea2010-05-15 22:23:53 +00002157 /* We have to simulate this by writing to a temporary FILE*,
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002158 then reading back, then writing to the argument file. */
Christian Heimes2b221b72017-03-02 11:09:01 +01002159 FILE *fp;
Victor Stinnerdaf45552013-08-28 00:53:59 +02002160 PyObject *res = NULL;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002161
Christian Heimes2b221b72017-03-02 11:09:01 +01002162 fp = tmpfile();
2163 if (fp == NULL)
2164 return PyErr_SetFromErrno(PyExc_OSError);
2165 if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02002166 goto exit;
Victor Stinner26486ea2010-05-15 22:23:53 +00002167 res = PyCursesCheckERR(putwin(self->win, fp), "putwin");
Victor Stinnerdaf45552013-08-28 00:53:59 +02002168 if (res == NULL)
2169 goto exit;
Victor Stinner26486ea2010-05-15 22:23:53 +00002170 fseek(fp, 0, 0);
2171 while (1) {
2172 char buf[BUFSIZ];
2173 Py_ssize_t n = fread(buf, 1, BUFSIZ, fp);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002174 _Py_IDENTIFIER(write);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002175
Victor Stinner26486ea2010-05-15 22:23:53 +00002176 if (n <= 0)
2177 break;
2178 Py_DECREF(res);
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002179 res = _PyObject_CallMethodId(file, &PyId_write, "y#", buf, n);
Victor Stinner26486ea2010-05-15 22:23:53 +00002180 if (res == NULL)
2181 break;
2182 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02002183
2184exit:
Christian Heimes2b221b72017-03-02 11:09:01 +01002185 fclose(fp);
Guido van Rossum6c95da32007-07-24 00:16:38 +00002186 return res;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002187}
2188
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002189/*[clinic input]
2190_curses.window.redrawln
2191
2192 beg: int
2193 Starting line number.
2194 num: int
2195 The number of lines.
2196 /
2197
2198Mark the specified lines corrupted.
2199
2200They should be completely redrawn on the next refresh() call.
2201[clinic start generated code]*/
2202
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002203static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002204_curses_window_redrawln_impl(PyCursesWindowObject *self, int beg, int num)
2205/*[clinic end generated code: output=ea216e334f9ce1b4 input=152155e258a77a7a]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002206{
Victor Stinner26486ea2010-05-15 22:23:53 +00002207 return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002208}
2209
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002210/*[clinic input]
2211_curses.window.refresh
2212
2213 [
2214 pminrow: int
2215 pmincol: int
2216 sminrow: int
2217 smincol: int
2218 smaxrow: int
2219 smaxcol: int
2220 ]
2221 /
2222
2223Update the display immediately.
2224
2225Synchronize actual screen with previous drawing/deleting methods.
2226The 6 optional arguments can only be specified when the window is a pad
2227created with newpad(). The additional parameters are needed to indicate
2228what part of the pad and screen are involved. pminrow and pmincol specify
2229the upper left-hand corner of the rectangle to be displayed in the pad.
2230sminrow, smincol, smaxrow, and smaxcol specify the edges of the rectangle to
2231be displayed on the screen. The lower right-hand corner of the rectangle to
2232be displayed in the pad is calculated from the screen coordinates, since the
2233rectangles must be the same size. Both rectangles must be entirely contained
2234within their respective structures. Negative values of pminrow, pmincol,
2235sminrow, or smincol are treated as if they were zero.
2236[clinic start generated code]*/
2237
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002238static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002239_curses_window_refresh_impl(PyCursesWindowObject *self, int group_right_1,
2240 int pminrow, int pmincol, int sminrow,
2241 int smincol, int smaxrow, int smaxcol)
2242/*[clinic end generated code: output=42199543115e6e63 input=95e01cb5ffc635d0]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002243{
Victor Stinner26486ea2010-05-15 22:23:53 +00002244 int rtn;
Andrew M. Kuchlingf4195722000-06-23 01:36:21 +00002245
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002246#ifdef py_is_pad
2247 if (py_is_pad(self->win)) {
2248 if (!group_right_1) {
2249 PyErr_SetString(PyCursesError,
2250 "refresh() for a pad requires 6 arguments");
2251 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00002252 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002253 Py_BEGIN_ALLOW_THREADS
2254 rtn = prefresh(self->win, pminrow, pmincol,
2255 sminrow, smincol, smaxrow, smaxcol);
2256 Py_END_ALLOW_THREADS
2257 return PyCursesCheckERR(rtn, "prefresh");
2258 }
2259#endif
2260 if (group_right_1) {
2261 PyErr_SetString(PyExc_TypeError,
2262 "refresh() takes no arguments (6 given)");
Victor Stinner26486ea2010-05-15 22:23:53 +00002263 return NULL;
2264 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002265 Py_BEGIN_ALLOW_THREADS
2266 rtn = wrefresh(self->win);
2267 Py_END_ALLOW_THREADS
2268 return PyCursesCheckERR(rtn, "prefresh");
2269}
2270
2271/*[clinic input]
2272_curses.window.setscrreg
2273
2274 top: int
2275 First line number.
2276 bottom: int
2277 Last line number.
2278 /
2279
2280Define a software scrolling region.
2281
2282All scrolling actions will take place in this region.
2283[clinic start generated code]*/
2284
2285static PyObject *
2286_curses_window_setscrreg_impl(PyCursesWindowObject *self, int top,
2287 int bottom)
2288/*[clinic end generated code: output=486ab5db218d2b1a input=1b517b986838bf0e]*/
2289{
2290 return PyCursesCheckERR(wsetscrreg(self->win, top, bottom), "wsetscrreg");
2291}
2292
2293/*[clinic input]
2294_curses.window.subwin
2295
2296 [
2297 nlines: int = 0
2298 Height.
2299 ncols: int = 0
2300 Width.
2301 ]
2302 begin_y: int
2303 Top side y-coordinate.
2304 begin_x: int
2305 Left side x-coordinate.
2306 /
2307
2308Create a sub-window (screen-relative coordinates).
2309
2310By default, the sub-window will extend from the specified position to the
2311lower right corner of the window.
2312[clinic start generated code]*/
2313
2314static PyObject *
2315_curses_window_subwin_impl(PyCursesWindowObject *self, int group_left_1,
2316 int nlines, int ncols, int begin_y, int begin_x)
2317/*[clinic end generated code: output=93e898afc348f59a input=2129fa47fd57721c]*/
2318{
2319 WINDOW *win;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002320
Victor Stinner26486ea2010-05-15 22:23:53 +00002321 /* printf("Subwin: %i %i %i %i \n", nlines, ncols, begin_y, begin_x); */
Masayuki Yamamoto8bc7d632017-11-01 21:05:26 +09002322#ifdef py_is_pad
2323 if (py_is_pad(self->win)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002324 win = subpad(self->win, nlines, ncols, begin_y, begin_x);
Masayuki Yamamoto8bc7d632017-11-01 21:05:26 +09002325 }
Victor Stinner26486ea2010-05-15 22:23:53 +00002326 else
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00002327#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00002328 win = subwin(self->win, nlines, ncols, begin_y, begin_x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002329
Victor Stinner26486ea2010-05-15 22:23:53 +00002330 if (win == NULL) {
2331 PyErr_SetString(PyCursesError, catchall_NULL);
2332 return NULL;
2333 }
2334
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002335 return (PyObject *)PyCursesWindow_New(win, self->encoding);
Guido van Rossumf6971e21994-08-30 12:25:20 +00002336}
2337
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002338/*[clinic input]
2339_curses.window.scroll
2340
2341 [
2342 lines: int = 1
2343 Number of lines to scroll.
2344 ]
2345 /
2346
2347Scroll the screen or scrolling region.
2348
2349Scroll upward if the argument is positive and downward if it is negative.
2350[clinic start generated code]*/
2351
Guido van Rossumf6971e21994-08-30 12:25:20 +00002352static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002353_curses_window_scroll_impl(PyCursesWindowObject *self, int group_right_1,
2354 int lines)
2355/*[clinic end generated code: output=4541a8a11852d360 input=c969ca0cfabbdbec]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00002356{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002357 if (!group_right_1) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002358 return PyCursesCheckERR(scroll(self->win), "scroll");
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002359 }
2360 else {
2361 return PyCursesCheckERR(wscrl(self->win, lines), "scroll");
Victor Stinner26486ea2010-05-15 22:23:53 +00002362 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00002363}
2364
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002365/*[clinic input]
2366_curses.window.touchline
2367
2368 start: int
2369 count: int
2370 [
2371 changed: bool(accept={int}) = True
2372 ]
2373 /
2374
2375Pretend count lines have been changed, starting with line start.
2376
2377If changed is supplied, it specifies whether the affected lines are marked
2378as having been changed (changed=True) or unchanged (changed=False).
2379[clinic start generated code]*/
2380
Guido van Rossumf6971e21994-08-30 12:25:20 +00002381static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002382_curses_window_touchline_impl(PyCursesWindowObject *self, int start,
2383 int count, int group_right_1, int changed)
2384/*[clinic end generated code: output=65d05b3f7438c61d input=918ad1cbdadf93ea]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00002385{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002386 if (!group_right_1) {
2387 return PyCursesCheckERR(touchline(self->win, start, count), "touchline");
2388 }
2389 else {
2390 return PyCursesCheckERR(wtouchln(self->win, start, count, changed), "touchline");
Victor Stinner26486ea2010-05-15 22:23:53 +00002391 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002392}
2393
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002394/*[clinic input]
2395_curses.window.vline
2396
2397 [
2398 y: int
2399 Starting Y-coordinate.
2400 x: int
2401 Starting X-coordinate.
2402 ]
2403
2404 ch: object
2405 Character to draw.
2406 n: int
2407 Line length.
2408
2409 [
2410 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
2411 Attributes for the character.
2412 ]
2413 /
2414
2415Display a vertical line.
2416[clinic start generated code]*/
2417
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002418static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002419_curses_window_vline_impl(PyCursesWindowObject *self, int group_left_1,
2420 int y, int x, PyObject *ch, int n,
2421 int group_right_1, long attr)
2422/*[clinic end generated code: output=287ad1cc8982217f input=a6f2dc86a4648b32]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002423{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002424 chtype ch_;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002425
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002426 if (!PyCurses_ConvertToChtype(self, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00002427 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002428 if (group_left_1) {
2429 if (wmove(self->win, y, x) == ERR)
2430 return PyCursesCheckERR(ERR, "wmove");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002431 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002432 return PyCursesCheckERR(wvline(self->win, ch_ | (attr_t)attr, n), "vline");
Guido van Rossumf6971e21994-08-30 12:25:20 +00002433}
2434
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002435static PyObject *
2436PyCursesWindow_get_encoding(PyCursesWindowObject *self, void *closure)
2437{
2438 return PyUnicode_FromString(self->encoding);
2439}
2440
2441static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02002442PyCursesWindow_set_encoding(PyCursesWindowObject *self, PyObject *value, void *Py_UNUSED(ignored))
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002443{
2444 PyObject *ascii;
2445 char *encoding;
2446
2447 /* It is illegal to del win.encoding */
2448 if (value == NULL) {
2449 PyErr_SetString(PyExc_TypeError,
2450 "encoding may not be deleted");
2451 return -1;
2452 }
2453
2454 if (!PyUnicode_Check(value)) {
2455 PyErr_SetString(PyExc_TypeError,
2456 "setting encoding to a non-string");
2457 return -1;
2458 }
2459 ascii = PyUnicode_AsASCIIString(value);
2460 if (ascii == NULL)
2461 return -1;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02002462 encoding = _PyMem_Strdup(PyBytes_AS_STRING(ascii));
Ross Lagerwall2dabaf62012-09-07 08:34:23 +02002463 Py_DECREF(ascii);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002464 if (encoding == NULL) {
2465 PyErr_NoMemory();
2466 return -1;
2467 }
Victor Stinner49fc8ec2013-07-07 23:30:24 +02002468 PyMem_Free(self->encoding);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002469 self->encoding = encoding;
2470 return 0;
2471}
2472
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002473#include "clinic/_cursesmodule.c.h"
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002474
Guido van Rossumf6971e21994-08-30 12:25:20 +00002475static PyMethodDef PyCursesWindow_Methods[] = {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002476 _CURSES_WINDOW_ADDCH_METHODDEF
2477 _CURSES_WINDOW_ADDNSTR_METHODDEF
2478 _CURSES_WINDOW_ADDSTR_METHODDEF
2479 _CURSES_WINDOW_ATTROFF_METHODDEF
2480 _CURSES_WINDOW_ATTRON_METHODDEF
2481 _CURSES_WINDOW_ATTRSET_METHODDEF
2482 _CURSES_WINDOW_BKGD_METHODDEF
Serhiy Storchaka894ebd02017-11-01 14:34:20 +02002483#ifdef HAVE_CURSES_WCHGAT
Victor Stinner26486ea2010-05-15 22:23:53 +00002484 {"chgat", (PyCFunction)PyCursesWindow_ChgAt, METH_VARARGS},
Serhiy Storchaka894ebd02017-11-01 14:34:20 +02002485#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002486 _CURSES_WINDOW_BKGDSET_METHODDEF
2487 _CURSES_WINDOW_BORDER_METHODDEF
2488 _CURSES_WINDOW_BOX_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002489 {"clear", (PyCFunction)PyCursesWindow_wclear, METH_NOARGS},
2490 {"clearok", (PyCFunction)PyCursesWindow_clearok, METH_VARARGS},
2491 {"clrtobot", (PyCFunction)PyCursesWindow_wclrtobot, METH_NOARGS},
2492 {"clrtoeol", (PyCFunction)PyCursesWindow_wclrtoeol, METH_NOARGS},
2493 {"cursyncup", (PyCFunction)PyCursesWindow_wcursyncup, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002494 _CURSES_WINDOW_DELCH_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002495 {"deleteln", (PyCFunction)PyCursesWindow_wdeleteln, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002496 _CURSES_WINDOW_DERWIN_METHODDEF
2497 _CURSES_WINDOW_ECHOCHAR_METHODDEF
2498 _CURSES_WINDOW_ENCLOSE_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002499 {"erase", (PyCFunction)PyCursesWindow_werase, METH_NOARGS},
2500 {"getbegyx", (PyCFunction)PyCursesWindow_getbegyx, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002501 _CURSES_WINDOW_GETBKGD_METHODDEF
2502 _CURSES_WINDOW_GETCH_METHODDEF
2503 _CURSES_WINDOW_GETKEY_METHODDEF
2504 _CURSES_WINDOW_GET_WCH_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002505 {"getmaxyx", (PyCFunction)PyCursesWindow_getmaxyx, METH_NOARGS},
2506 {"getparyx", (PyCFunction)PyCursesWindow_getparyx, METH_NOARGS},
2507 {"getstr", (PyCFunction)PyCursesWindow_GetStr, METH_VARARGS},
2508 {"getyx", (PyCFunction)PyCursesWindow_getyx, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002509 _CURSES_WINDOW_HLINE_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002510 {"idcok", (PyCFunction)PyCursesWindow_idcok, METH_VARARGS},
2511 {"idlok", (PyCFunction)PyCursesWindow_idlok, METH_VARARGS},
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002512#ifdef HAVE_CURSES_IMMEDOK
Victor Stinner26486ea2010-05-15 22:23:53 +00002513 {"immedok", (PyCFunction)PyCursesWindow_immedok, METH_VARARGS},
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002514#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002515 _CURSES_WINDOW_INCH_METHODDEF
2516 _CURSES_WINDOW_INSCH_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002517 {"insdelln", (PyCFunction)PyCursesWindow_winsdelln, METH_VARARGS},
2518 {"insertln", (PyCFunction)PyCursesWindow_winsertln, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002519 _CURSES_WINDOW_INSNSTR_METHODDEF
2520 _CURSES_WINDOW_INSSTR_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002521 {"instr", (PyCFunction)PyCursesWindow_InStr, METH_VARARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002522 _CURSES_WINDOW_IS_LINETOUCHED_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002523 {"is_wintouched", (PyCFunction)PyCursesWindow_is_wintouched, METH_NOARGS},
2524 {"keypad", (PyCFunction)PyCursesWindow_keypad, METH_VARARGS},
2525 {"leaveok", (PyCFunction)PyCursesWindow_leaveok, METH_VARARGS},
2526 {"move", (PyCFunction)PyCursesWindow_wmove, METH_VARARGS},
2527 {"mvderwin", (PyCFunction)PyCursesWindow_mvderwin, METH_VARARGS},
2528 {"mvwin", (PyCFunction)PyCursesWindow_mvwin, METH_VARARGS},
2529 {"nodelay", (PyCFunction)PyCursesWindow_nodelay, METH_VARARGS},
2530 {"notimeout", (PyCFunction)PyCursesWindow_notimeout, METH_VARARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002531 _CURSES_WINDOW_NOUTREFRESH_METHODDEF
2532 _CURSES_WINDOW_OVERLAY_METHODDEF
2533 _CURSES_WINDOW_OVERWRITE_METHODDEF
2534 _CURSES_WINDOW_PUTWIN_METHODDEF
2535 _CURSES_WINDOW_REDRAWLN_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002536 {"redrawwin", (PyCFunction)PyCursesWindow_redrawwin, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002537 _CURSES_WINDOW_REFRESH_METHODDEF
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +00002538#ifndef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00002539 {"resize", (PyCFunction)PyCursesWindow_wresize, METH_VARARGS},
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002540#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002541 _CURSES_WINDOW_SCROLL_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002542 {"scrollok", (PyCFunction)PyCursesWindow_scrollok, METH_VARARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002543 _CURSES_WINDOW_SETSCRREG_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002544 {"standend", (PyCFunction)PyCursesWindow_wstandend, METH_NOARGS},
2545 {"standout", (PyCFunction)PyCursesWindow_wstandout, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002546 {"subpad", (PyCFunction)_curses_window_subwin, METH_VARARGS, _curses_window_subwin__doc__},
2547 _CURSES_WINDOW_SUBWIN_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002548 {"syncdown", (PyCFunction)PyCursesWindow_wsyncdown, METH_NOARGS},
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002549#ifdef HAVE_CURSES_SYNCOK
Victor Stinner26486ea2010-05-15 22:23:53 +00002550 {"syncok", (PyCFunction)PyCursesWindow_syncok, METH_VARARGS},
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002551#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00002552 {"syncup", (PyCFunction)PyCursesWindow_wsyncup, METH_NOARGS},
2553 {"timeout", (PyCFunction)PyCursesWindow_wtimeout, METH_VARARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002554 _CURSES_WINDOW_TOUCHLINE_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002555 {"touchwin", (PyCFunction)PyCursesWindow_touchwin, METH_NOARGS},
2556 {"untouchwin", (PyCFunction)PyCursesWindow_untouchwin, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002557 _CURSES_WINDOW_VLINE_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002558 {NULL, NULL} /* sentinel */
Guido van Rossumf6971e21994-08-30 12:25:20 +00002559};
2560
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002561static PyGetSetDef PyCursesWindow_getsets[] = {
2562 {"encoding",
2563 (getter)PyCursesWindow_get_encoding,
2564 (setter)PyCursesWindow_set_encoding,
Ronald Oussoren2a7fe032012-01-17 16:53:20 +01002565 "the typecode character used to create the array"},
2566 {NULL, NULL, NULL, NULL } /* sentinel */
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002567};
2568
Guido van Rossumf6971e21994-08-30 12:25:20 +00002569/* -------------------------------------------------------*/
Guido van Rossum85738471995-02-17 13:50:17 +00002570
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002571PyTypeObject PyCursesWindow_Type = {
Victor Stinner26486ea2010-05-15 22:23:53 +00002572 PyVarObject_HEAD_INIT(NULL, 0)
Victor Stinner61e2bc72017-02-12 23:42:02 +01002573 "_curses.window", /*tp_name*/
Victor Stinner26486ea2010-05-15 22:23:53 +00002574 sizeof(PyCursesWindowObject), /*tp_basicsize*/
2575 0, /*tp_itemsize*/
2576 /* methods */
2577 (destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002578 0, /*tp_vectorcall_offset*/
Victor Stinner26486ea2010-05-15 22:23:53 +00002579 (getattrfunc)0, /*tp_getattr*/
2580 (setattrfunc)0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002581 0, /*tp_as_async*/
Victor Stinner26486ea2010-05-15 22:23:53 +00002582 0, /*tp_repr*/
2583 0, /*tp_as_number*/
2584 0, /*tp_as_sequence*/
2585 0, /*tp_as_mapping*/
2586 0, /*tp_hash*/
2587 0, /*tp_call*/
2588 0, /*tp_str*/
2589 0, /*tp_getattro*/
2590 0, /*tp_setattro*/
2591 0, /*tp_as_buffer*/
2592 Py_TPFLAGS_DEFAULT, /*tp_flags*/
2593 0, /*tp_doc*/
2594 0, /*tp_traverse*/
2595 0, /*tp_clear*/
2596 0, /*tp_richcompare*/
2597 0, /*tp_weaklistoffset*/
2598 0, /*tp_iter*/
2599 0, /*tp_iternext*/
2600 PyCursesWindow_Methods, /*tp_methods*/
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002601 0, /* tp_members */
2602 PyCursesWindow_getsets, /* tp_getset */
Guido van Rossumf6971e21994-08-30 12:25:20 +00002603};
2604
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002605/* Function Prototype Macros - They are ugly but very, very useful. ;-)
2606
2607 X - function name
2608 TYPE - parameter Type
2609 ERGSTR - format string for construction of the return value
2610 PARSESTR - format string for argument parsing
2611 */
2612
2613#define NoArgNoReturnFunctionBody(X) \
2614{ \
2615 PyCursesInitialised \
2616 return PyCursesCheckERR(X(), # X); }
2617
2618#define NoArgOrFlagNoReturnFunctionBody(X, flag) \
2619{ \
2620 PyCursesInitialised \
2621 if (flag) \
2622 return PyCursesCheckERR(X(), # X); \
2623 else \
2624 return PyCursesCheckERR(no ## X(), # X); \
2625}
2626
2627#define NoArgReturnIntFunctionBody(X) \
2628{ \
2629 PyCursesInitialised \
2630 return PyLong_FromLong((long) X()); }
2631
2632
2633#define NoArgReturnStringFunctionBody(X) \
2634{ \
2635 PyCursesInitialised \
2636 return PyBytes_FromString(X()); }
2637
2638#define NoArgTrueFalseFunctionBody(X) \
2639{ \
2640 PyCursesInitialised \
2641 return PyBool_FromLong(X()); }
2642
2643#define NoArgNoReturnVoidFunctionBody(X) \
2644{ \
2645 PyCursesInitialised \
2646 X(); \
2647 Py_RETURN_NONE; }
2648
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002649/*********************************************************************
2650 Global Functions
2651**********************************************************************/
Guido van Rossumf6971e21994-08-30 12:25:20 +00002652
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002653#ifdef HAVE_CURSES_FILTER
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002654/*[clinic input]
2655_curses.filter
2656
2657[clinic start generated code]*/
2658
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002659static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002660_curses_filter_impl(PyObject *module)
2661/*[clinic end generated code: output=fb5b8a3642eb70b5 input=668c75a6992d3624]*/
Christian Heimesaf98da12008-01-27 15:18:18 +00002662{
Victor Stinner26486ea2010-05-15 22:23:53 +00002663 /* not checking for PyCursesInitialised here since filter() must
2664 be called before initscr() */
2665 filter();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002666 Py_RETURN_NONE;
Christian Heimesaf98da12008-01-27 15:18:18 +00002667}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002668#endif
Christian Heimesaf98da12008-01-27 15:18:18 +00002669
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002670/*[clinic input]
2671_curses.baudrate
2672
2673Return the output speed of the terminal in bits per second.
2674[clinic start generated code]*/
2675
Christian Heimesaf98da12008-01-27 15:18:18 +00002676static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002677_curses_baudrate_impl(PyObject *module)
2678/*[clinic end generated code: output=3c63c6c401d7d9c0 input=921f022ed04a0fd9]*/
2679NoArgReturnIntFunctionBody(baudrate)
2680
2681/*[clinic input]
2682_curses.beep
2683
2684Emit a short attention sound.
2685[clinic start generated code]*/
2686
2687static PyObject *
2688_curses_beep_impl(PyObject *module)
2689/*[clinic end generated code: output=425274962abe49a2 input=a35698ca7d0162bc]*/
2690NoArgNoReturnFunctionBody(beep)
2691
2692/*[clinic input]
2693_curses.can_change_color
2694
2695Return True if the programmer can change the colors displayed by the terminal.
2696[clinic start generated code]*/
2697
2698static PyObject *
2699_curses_can_change_color_impl(PyObject *module)
2700/*[clinic end generated code: output=359df8c3c77d8bf1 input=d7718884de0092f2]*/
2701NoArgTrueFalseFunctionBody(can_change_color)
2702
2703/*[clinic input]
2704_curses.cbreak
2705
2706 flag: bool(accept={int}) = True
2707 If false, the effect is the same as calling nocbreak().
2708 /
2709
2710Enter cbreak mode.
2711
2712In cbreak mode (sometimes called "rare" mode) normal tty line buffering is
2713turned off and characters are available to be read one by one. However,
2714unlike raw mode, special characters (interrupt, quit, suspend, and flow
2715control) retain their effects on the tty driver and calling program.
2716Calling first raw() then cbreak() leaves the terminal in cbreak mode.
2717[clinic start generated code]*/
2718
2719static PyObject *
2720_curses_cbreak_impl(PyObject *module, int flag)
2721/*[clinic end generated code: output=9f9dee9664769751 input=150be619eb1f1458]*/
2722NoArgOrFlagNoReturnFunctionBody(cbreak, flag)
2723
2724/*[clinic input]
2725_curses.color_content
2726
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -05002727 color_number: color
Serhiy Storchaka1470edd2021-01-03 22:51:11 +02002728 The number of the color (0 - (COLORS-1)).
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002729 /
2730
2731Return the red, green, and blue (RGB) components of the specified color.
2732
2733A 3-tuple is returned, containing the R, G, B values for the given color,
2734which will be between 0 (no component) and 1000 (maximum amount of component).
2735[clinic start generated code]*/
2736
2737static PyObject *
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -05002738_curses_color_content_impl(PyObject *module, int color_number)
Serhiy Storchaka1470edd2021-01-03 22:51:11 +02002739/*[clinic end generated code: output=17b466df7054e0de input=03b5ed0472662aea]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002740{
Serhiy Storchaka59f9b4e2021-01-05 09:13:15 +02002741 _CURSES_COLOR_VAL_TYPE r,g,b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002742
Victor Stinner26486ea2010-05-15 22:23:53 +00002743 PyCursesInitialised;
2744 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002745
Serhiy Storchaka59f9b4e2021-01-05 09:13:15 +02002746 if (_COLOR_CONTENT_FUNC(color_number, &r, &g, &b) == ERR) {
2747 PyErr_Format(PyCursesError, "%s() returned ERR",
2748 Py_STRINGIFY(_COLOR_CONTENT_FUNC));
Victor Stinner26486ea2010-05-15 22:23:53 +00002749 return NULL;
2750 }
Serhiy Storchaka59f9b4e2021-01-05 09:13:15 +02002751
2752 return Py_BuildValue("(iii)", r, g, b);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002753}
2754
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002755/*[clinic input]
2756_curses.color_pair
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002757
Serhiy Storchaka1470edd2021-01-03 22:51:11 +02002758 pair_number: int
2759 The number of the color pair.
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002760 /
2761
2762Return the attribute value for displaying text in the specified color.
2763
2764This attribute value can be combined with A_STANDOUT, A_REVERSE, and the
2765other A_* attributes. pair_number() is the counterpart to this function.
2766[clinic start generated code]*/
2767
2768static PyObject *
Serhiy Storchaka1470edd2021-01-03 22:51:11 +02002769_curses_color_pair_impl(PyObject *module, int pair_number)
2770/*[clinic end generated code: output=60718abb10ce9feb input=6034e9146f343802]*/
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002771{
Victor Stinner26486ea2010-05-15 22:23:53 +00002772 PyCursesInitialised;
2773 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002774
Serhiy Storchaka1470edd2021-01-03 22:51:11 +02002775 return PyLong_FromLong(COLOR_PAIR(pair_number));
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002776}
2777
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002778/*[clinic input]
2779_curses.curs_set
2780
2781 visibility: int
2782 0 for invisible, 1 for normal visible, or 2 for very visible.
2783 /
2784
2785Set the cursor state.
2786
2787If the terminal supports the visibility requested, the previous cursor
2788state is returned; otherwise, an exception is raised. On many terminals,
2789the "visible" mode is an underline cursor and the "very visible" mode is
2790a block cursor.
2791[clinic start generated code]*/
2792
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002793static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002794_curses_curs_set_impl(PyObject *module, int visibility)
2795/*[clinic end generated code: output=ee8e62483b1d6cd4 input=81a7924a65d29504]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002796{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002797 int erg;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002798
Victor Stinner26486ea2010-05-15 22:23:53 +00002799 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002800
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002801 erg = curs_set(visibility);
Victor Stinner26486ea2010-05-15 22:23:53 +00002802 if (erg == ERR) return PyCursesCheckERR(erg, "curs_set");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002803
Victor Stinner26486ea2010-05-15 22:23:53 +00002804 return PyLong_FromLong((long) erg);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002805}
2806
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002807/*[clinic input]
2808_curses.def_prog_mode
2809
2810Save the current terminal mode as the "program" mode.
2811
2812The "program" mode is the mode when the running program is using curses.
2813
2814Subsequent calls to reset_prog_mode() will restore this mode.
2815[clinic start generated code]*/
2816
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002817static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002818_curses_def_prog_mode_impl(PyObject *module)
2819/*[clinic end generated code: output=05d5a351fff874aa input=768b9cace620dda5]*/
2820NoArgNoReturnFunctionBody(def_prog_mode)
2821
2822/*[clinic input]
2823_curses.def_shell_mode
2824
2825Save the current terminal mode as the "shell" mode.
2826
2827The "shell" mode is the mode when the running program is not using curses.
2828
2829Subsequent calls to reset_shell_mode() will restore this mode.
2830[clinic start generated code]*/
2831
2832static PyObject *
2833_curses_def_shell_mode_impl(PyObject *module)
2834/*[clinic end generated code: output=d6e42f5c768f860f input=5ead21f6f0baa894]*/
2835NoArgNoReturnFunctionBody(def_shell_mode)
2836
2837/*[clinic input]
2838_curses.delay_output
2839
2840 ms: int
2841 Duration in milliseconds.
2842 /
2843
2844Insert a pause in output.
2845[clinic start generated code]*/
2846
2847static PyObject *
2848_curses_delay_output_impl(PyObject *module, int ms)
2849/*[clinic end generated code: output=b6613a67f17fa4f4 input=5316457f5f59196c]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002850{
Victor Stinner26486ea2010-05-15 22:23:53 +00002851 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002852
Victor Stinner26486ea2010-05-15 22:23:53 +00002853 return PyCursesCheckERR(delay_output(ms), "delay_output");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002854}
2855
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002856/*[clinic input]
2857_curses.doupdate
2858
2859Update the physical screen to match the virtual screen.
2860[clinic start generated code]*/
2861
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002862static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002863_curses_doupdate_impl(PyObject *module)
2864/*[clinic end generated code: output=f34536975a75680c input=8da80914432a6489]*/
2865NoArgNoReturnFunctionBody(doupdate)
2866
2867/*[clinic input]
2868_curses.echo
2869
2870 flag: bool(accept={int}) = True
2871 If false, the effect is the same as calling noecho().
2872 /
2873
2874Enter echo mode.
2875
2876In echo mode, each character input is echoed to the screen as it is entered.
2877[clinic start generated code]*/
2878
2879static PyObject *
2880_curses_echo_impl(PyObject *module, int flag)
2881/*[clinic end generated code: output=03acb2ddfa6c8729 input=2e9e891d637eac5d]*/
2882NoArgOrFlagNoReturnFunctionBody(echo, flag)
2883
2884/*[clinic input]
2885_curses.endwin
2886
2887De-initialize the library, and return terminal to normal status.
2888[clinic start generated code]*/
2889
2890static PyObject *
2891_curses_endwin_impl(PyObject *module)
2892/*[clinic end generated code: output=c0150cd96d2f4128 input=e172cfa43062f3fa]*/
2893NoArgNoReturnFunctionBody(endwin)
2894
2895/*[clinic input]
2896_curses.erasechar
2897
2898Return the user's current erase character.
2899[clinic start generated code]*/
2900
2901static PyObject *
2902_curses_erasechar_impl(PyObject *module)
2903/*[clinic end generated code: output=3df305dc6b926b3f input=628c136c3c5758d3]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002904{
Victor Stinner26486ea2010-05-15 22:23:53 +00002905 char ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002906
Victor Stinner26486ea2010-05-15 22:23:53 +00002907 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002908
Victor Stinner26486ea2010-05-15 22:23:53 +00002909 ch = erasechar();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002910
Victor Stinner26486ea2010-05-15 22:23:53 +00002911 return PyBytes_FromStringAndSize(&ch, 1);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002912}
2913
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002914/*[clinic input]
2915_curses.flash
2916
2917Flash the screen.
2918
2919That is, change it to reverse-video and then change it back in a short interval.
2920[clinic start generated code]*/
2921
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002922static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002923_curses_flash_impl(PyObject *module)
2924/*[clinic end generated code: output=488b8a0ebd9ea9b8 input=02fdfb06c8fc3171]*/
2925NoArgNoReturnFunctionBody(flash)
2926
2927/*[clinic input]
2928_curses.flushinp
2929
2930Flush all input buffers.
2931
2932This throws away any typeahead that has been typed by the user and has not
2933yet been processed by the program.
2934[clinic start generated code]*/
2935
2936static PyObject *
2937_curses_flushinp_impl(PyObject *module)
2938/*[clinic end generated code: output=7e7a1fc1473960f5 input=59d042e705cef5ec]*/
2939NoArgNoReturnVoidFunctionBody(flushinp)
2940
2941#ifdef getsyx
2942/*[clinic input]
2943_curses.getsyx
2944
2945Return the current coordinates of the virtual screen cursor.
2946
2947Return a (y, x) tuple. If leaveok is currently true, return (-1, -1).
2948[clinic start generated code]*/
2949
2950static PyObject *
2951_curses_getsyx_impl(PyObject *module)
2952/*[clinic end generated code: output=c8e6c3f42349a038 input=9e1f862f3b4f7cba]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002953{
Victor Stinner26486ea2010-05-15 22:23:53 +00002954 int x = 0;
2955 int y = 0;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002956
Victor Stinner26486ea2010-05-15 22:23:53 +00002957 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002958
Victor Stinner26486ea2010-05-15 22:23:53 +00002959 getsyx(y, x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002960
Victor Stinner26486ea2010-05-15 22:23:53 +00002961 return Py_BuildValue("(ii)", y, x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002962}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002963#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002964
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002965#ifdef NCURSES_MOUSE_VERSION
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002966/*[clinic input]
2967_curses.getmouse
2968
2969Retrieve the queued mouse event.
2970
2971After getch() returns KEY_MOUSE to signal a mouse event, this function
2972returns a 5-tuple (id, x, y, z, bstate).
2973[clinic start generated code]*/
2974
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002975static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002976_curses_getmouse_impl(PyObject *module)
2977/*[clinic end generated code: output=ccf4242546b9cfa8 input=5b756ee6f5b481b1]*/
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002978{
Victor Stinner26486ea2010-05-15 22:23:53 +00002979 int rtn;
2980 MEVENT event;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002981
Victor Stinner26486ea2010-05-15 22:23:53 +00002982 PyCursesInitialised;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002983
Victor Stinner26486ea2010-05-15 22:23:53 +00002984 rtn = getmouse( &event );
2985 if (rtn == ERR) {
2986 PyErr_SetString(PyCursesError, "getmouse() returned ERR");
2987 return NULL;
2988 }
Serhiy Storchaka4f469c02017-11-01 20:48:49 +02002989 return Py_BuildValue("(hiiik)",
Victor Stinner26486ea2010-05-15 22:23:53 +00002990 (short)event.id,
Serhiy Storchaka4f469c02017-11-01 20:48:49 +02002991 (int)event.x, (int)event.y, (int)event.z,
2992 (unsigned long) event.bstate);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002993}
2994
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002995/*[clinic input]
2996_curses.ungetmouse
2997
2998 id: short
2999 x: int
3000 y: int
3001 z: int
3002 bstate: unsigned_long(bitwise=True)
3003 /
3004
3005Push a KEY_MOUSE event onto the input queue.
3006
3007The following getmouse() will return the given state data.
3008[clinic start generated code]*/
3009
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003010static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003011_curses_ungetmouse_impl(PyObject *module, short id, int x, int y, int z,
3012 unsigned long bstate)
3013/*[clinic end generated code: output=3430c9b0fc5c4341 input=fd650b2ca5a01e8f]*/
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003014{
Victor Stinner26486ea2010-05-15 22:23:53 +00003015 MEVENT event;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003016
Victor Stinner26486ea2010-05-15 22:23:53 +00003017 PyCursesInitialised;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003018
Serhiy Storchaka4f469c02017-11-01 20:48:49 +02003019 event.id = id;
3020 event.x = x;
3021 event.y = y;
3022 event.z = z;
3023 event.bstate = bstate;
Victor Stinner26486ea2010-05-15 22:23:53 +00003024 return PyCursesCheckERR(ungetmouse(&event), "ungetmouse");
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003025}
3026#endif
3027
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003028/*[clinic input]
3029_curses.getwin
3030
3031 file: object
3032 /
3033
3034Read window related data stored in the file by an earlier putwin() call.
3035
3036The routine then creates and initializes a new window using that data,
3037returning the new window object.
3038[clinic start generated code]*/
3039
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003040static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003041_curses_getwin(PyObject *module, PyObject *file)
3042/*[clinic end generated code: output=a79e0df3379af756 input=f713d2bba0e4c929]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003043{
Christian Heimes2b221b72017-03-02 11:09:01 +01003044 FILE *fp;
Victor Stinner26486ea2010-05-15 22:23:53 +00003045 PyObject *data;
3046 size_t datalen;
3047 WINDOW *win;
Benjamin Peterson18b71912013-05-16 15:29:44 -05003048 _Py_IDENTIFIER(read);
Victor Stinnerdaf45552013-08-28 00:53:59 +02003049 PyObject *res = NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003050
Victor Stinner26486ea2010-05-15 22:23:53 +00003051 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003052
Christian Heimes2b221b72017-03-02 11:09:01 +01003053 fp = tmpfile();
3054 if (fp == NULL)
3055 return PyErr_SetFromErrno(PyExc_OSError);
3056
3057 if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02003058 goto error;
Christian Heimes2b221b72017-03-02 11:09:01 +01003059
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02003060 data = _PyObject_CallMethodIdNoArgs(file, &PyId_read);
Victor Stinnerdaf45552013-08-28 00:53:59 +02003061 if (data == NULL)
3062 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00003063 if (!PyBytes_Check(data)) {
3064 PyErr_Format(PyExc_TypeError,
3065 "f.read() returned %.100s instead of bytes",
Victor Stinnerdaa97562020-02-07 03:37:06 +01003066 Py_TYPE(data)->tp_name);
Victor Stinner26486ea2010-05-15 22:23:53 +00003067 Py_DECREF(data);
Victor Stinnerdaf45552013-08-28 00:53:59 +02003068 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00003069 }
3070 datalen = PyBytes_GET_SIZE(data);
3071 if (fwrite(PyBytes_AS_STRING(data), 1, datalen, fp) != datalen) {
3072 Py_DECREF(data);
Christian Heimes2b221b72017-03-02 11:09:01 +01003073 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02003074 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00003075 }
Guido van Rossum6c95da32007-07-24 00:16:38 +00003076 Py_DECREF(data);
Victor Stinnerdaf45552013-08-28 00:53:59 +02003077
Victor Stinner26486ea2010-05-15 22:23:53 +00003078 fseek(fp, 0, 0);
3079 win = getwin(fp);
Victor Stinner26486ea2010-05-15 22:23:53 +00003080 if (win == NULL) {
3081 PyErr_SetString(PyCursesError, catchall_NULL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02003082 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00003083 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02003084 res = PyCursesWindow_New(win, NULL);
3085
3086error:
Christian Heimes2b221b72017-03-02 11:09:01 +01003087 fclose(fp);
Victor Stinnerdaf45552013-08-28 00:53:59 +02003088 return res;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003089}
3090
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003091/*[clinic input]
3092_curses.halfdelay
3093
3094 tenths: byte
3095 Maximal blocking delay in tenths of seconds (1 - 255).
3096 /
3097
3098Enter half-delay mode.
3099
3100Use nocbreak() to leave half-delay mode.
3101[clinic start generated code]*/
3102
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003103static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003104_curses_halfdelay_impl(PyObject *module, unsigned char tenths)
3105/*[clinic end generated code: output=e92cdf0ef33c0663 input=e42dce7259c15100]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003106{
Victor Stinner26486ea2010-05-15 22:23:53 +00003107 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003108
Victor Stinner26486ea2010-05-15 22:23:53 +00003109 return PyCursesCheckERR(halfdelay(tenths), "halfdelay");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003110}
3111
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003112/*[clinic input]
3113_curses.has_colors
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003114
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003115Return True if the terminal can display colors; otherwise, return False.
3116[clinic start generated code]*/
3117
3118static PyObject *
3119_curses_has_colors_impl(PyObject *module)
3120/*[clinic end generated code: output=db5667483139e3e2 input=b2ec41b739d896c6]*/
3121NoArgTrueFalseFunctionBody(has_colors)
3122
3123/*[clinic input]
3124_curses.has_ic
3125
3126Return True if the terminal has insert- and delete-character capabilities.
3127[clinic start generated code]*/
3128
3129static PyObject *
3130_curses_has_ic_impl(PyObject *module)
3131/*[clinic end generated code: output=6be24da9cb1268fe input=9bc2d3a797cc7324]*/
3132NoArgTrueFalseFunctionBody(has_ic)
3133
3134/*[clinic input]
3135_curses.has_il
3136
3137Return True if the terminal has insert- and delete-line capabilities.
3138[clinic start generated code]*/
3139
3140static PyObject *
3141_curses_has_il_impl(PyObject *module)
3142/*[clinic end generated code: output=d45bd7788ff9f5f4 input=cd939d5607ee5427]*/
3143NoArgTrueFalseFunctionBody(has_il)
3144
3145#ifdef HAVE_CURSES_HAS_KEY
3146/*[clinic input]
3147_curses.has_key
3148
3149 key: int
3150 Key number.
3151 /
3152
3153Return True if the current terminal type recognizes a key with that value.
3154[clinic start generated code]*/
3155
3156static PyObject *
3157_curses_has_key_impl(PyObject *module, int key)
3158/*[clinic end generated code: output=19ad48319414d0b1 input=78bd44acf1a4997c]*/
3159{
Victor Stinner26486ea2010-05-15 22:23:53 +00003160 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003161
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003162 return PyBool_FromLong(has_key(key));
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003163}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02003164#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003165
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003166/*[clinic input]
3167_curses.init_color
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003168
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -05003169 color_number: color
Serhiy Storchaka1470edd2021-01-03 22:51:11 +02003170 The number of the color to be changed (0 - (COLORS-1)).
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -05003171 r: component
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003172 Red component (0 - 1000).
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -05003173 g: component
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003174 Green component (0 - 1000).
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -05003175 b: component
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003176 Blue component (0 - 1000).
3177 /
3178
3179Change the definition of a color.
3180
3181When init_color() is used, all occurrences of that color on the screen
3182immediately change to the new definition. This function is a no-op on
Serhiy Storchaka1470edd2021-01-03 22:51:11 +02003183most terminals; it is active only if can_change_color() returns true.
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003184[clinic start generated code]*/
3185
3186static PyObject *
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -05003187_curses_init_color_impl(PyObject *module, int color_number, short r, short g,
3188 short b)
Serhiy Storchaka1470edd2021-01-03 22:51:11 +02003189/*[clinic end generated code: output=d7ed71b2d818cdf2 input=ae2b8bea0f152c80]*/
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003190{
Victor Stinner26486ea2010-05-15 22:23:53 +00003191 PyCursesInitialised;
3192 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003193
Serhiy Storchaka59f9b4e2021-01-05 09:13:15 +02003194 return PyCursesCheckERR(_CURSES_INIT_COLOR_FUNC(color_number, r, g, b),
3195 Py_STRINGIFY(_CURSES_INIT_COLOR_FUNC));
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003196}
3197
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003198/*[clinic input]
3199_curses.init_pair
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003200
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -05003201 pair_number: pair
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003202 The number of the color-pair to be changed (1 - (COLOR_PAIRS-1)).
Serhiy Storchaka1470edd2021-01-03 22:51:11 +02003203 fg: color_allow_default
3204 Foreground color number (-1 - (COLORS-1)).
3205 bg: color_allow_default
3206 Background color number (-1 - (COLORS-1)).
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003207 /
3208
3209Change the definition of a color-pair.
3210
3211If the color-pair was previously initialized, the screen is refreshed and
3212all occurrences of that color-pair are changed to the new definition.
3213[clinic start generated code]*/
3214
3215static PyObject *
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -05003216_curses_init_pair_impl(PyObject *module, int pair_number, int fg, int bg)
Serhiy Storchaka1470edd2021-01-03 22:51:11 +02003217/*[clinic end generated code: output=a0bba03d2bbc3ee6 input=54b421b44c12c389]*/
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003218{
Victor Stinner26486ea2010-05-15 22:23:53 +00003219 PyCursesInitialised;
3220 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003221
Serhiy Storchaka59f9b4e2021-01-05 09:13:15 +02003222 if (_CURSES_INIT_PAIR_FUNC(pair_number, fg, bg) == ERR) {
3223 if (pair_number >= COLOR_PAIRS) {
3224 PyErr_Format(PyExc_ValueError,
3225 "Color pair is greater than COLOR_PAIRS-1 (%d).",
3226 COLOR_PAIRS - 1);
3227 }
3228 else {
3229 PyErr_Format(PyCursesError, "%s() returned ERR",
3230 Py_STRINGIFY(_CURSES_INIT_PAIR_FUNC));
3231 }
3232 return NULL;
3233 }
3234
3235 Py_RETURN_NONE;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003236}
3237
Andrew M. Kuchling32552682000-12-22 21:52:27 +00003238static PyObject *ModDict;
3239
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003240/*[clinic input]
3241_curses.initscr
3242
3243Initialize the library.
3244
3245Return a WindowObject which represents the whole screen.
3246[clinic start generated code]*/
3247
Victor Stinner26486ea2010-05-15 22:23:53 +00003248static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003249_curses_initscr_impl(PyObject *module)
3250/*[clinic end generated code: output=619fb68443810b7b input=514f4bce1821f6b5]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00003251{
Victor Stinner26486ea2010-05-15 22:23:53 +00003252 WINDOW *win;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003253 PyCursesWindowObject *winobj;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003254
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003255 if (initialised) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003256 wrefresh(stdscr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003257 return (PyObject *)PyCursesWindow_New(stdscr, NULL);
Victor Stinner26486ea2010-05-15 22:23:53 +00003258 }
Guido van Rossum56bf2351994-08-31 22:06:24 +00003259
Victor Stinner26486ea2010-05-15 22:23:53 +00003260 win = initscr();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003261
Victor Stinner26486ea2010-05-15 22:23:53 +00003262 if (win == NULL) {
3263 PyErr_SetString(PyCursesError, catchall_NULL);
3264 return NULL;
3265 }
Guido van Rossum85738471995-02-17 13:50:17 +00003266
Victor Stinner26486ea2010-05-15 22:23:53 +00003267 initialised = initialised_setupterm = TRUE;
Guido van Rossum56bf2351994-08-31 22:06:24 +00003268
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00003269/* This was moved from initcurses() because it core dumped on SGI,
3270 where they're not defined until you've called initscr() */
Victor Stinner26486ea2010-05-15 22:23:53 +00003271#define SetDictInt(string,ch) \
3272 do { \
3273 PyObject *o = PyLong_FromLong((long) (ch)); \
3274 if (o && PyDict_SetItemString(ModDict, string, o) == 0) { \
3275 Py_DECREF(o); \
3276 } \
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003277 } while (0)
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00003278
Victor Stinner26486ea2010-05-15 22:23:53 +00003279 /* Here are some graphic symbols you can use */
3280 SetDictInt("ACS_ULCORNER", (ACS_ULCORNER));
3281 SetDictInt("ACS_LLCORNER", (ACS_LLCORNER));
3282 SetDictInt("ACS_URCORNER", (ACS_URCORNER));
3283 SetDictInt("ACS_LRCORNER", (ACS_LRCORNER));
3284 SetDictInt("ACS_LTEE", (ACS_LTEE));
3285 SetDictInt("ACS_RTEE", (ACS_RTEE));
3286 SetDictInt("ACS_BTEE", (ACS_BTEE));
3287 SetDictInt("ACS_TTEE", (ACS_TTEE));
3288 SetDictInt("ACS_HLINE", (ACS_HLINE));
3289 SetDictInt("ACS_VLINE", (ACS_VLINE));
3290 SetDictInt("ACS_PLUS", (ACS_PLUS));
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00003291#if !defined(__hpux) || defined(HAVE_NCURSES_H)
Victor Stinner26486ea2010-05-15 22:23:53 +00003292 /* On HP/UX 11, these are of type cchar_t, which is not an
3293 integral type. If this is a problem on more platforms, a
3294 configure test should be added to determine whether ACS_S1
3295 is of integral type. */
3296 SetDictInt("ACS_S1", (ACS_S1));
3297 SetDictInt("ACS_S9", (ACS_S9));
3298 SetDictInt("ACS_DIAMOND", (ACS_DIAMOND));
3299 SetDictInt("ACS_CKBOARD", (ACS_CKBOARD));
3300 SetDictInt("ACS_DEGREE", (ACS_DEGREE));
3301 SetDictInt("ACS_PLMINUS", (ACS_PLMINUS));
3302 SetDictInt("ACS_BULLET", (ACS_BULLET));
3303 SetDictInt("ACS_LARROW", (ACS_LARROW));
3304 SetDictInt("ACS_RARROW", (ACS_RARROW));
3305 SetDictInt("ACS_DARROW", (ACS_DARROW));
3306 SetDictInt("ACS_UARROW", (ACS_UARROW));
3307 SetDictInt("ACS_BOARD", (ACS_BOARD));
3308 SetDictInt("ACS_LANTERN", (ACS_LANTERN));
3309 SetDictInt("ACS_BLOCK", (ACS_BLOCK));
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00003310#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003311 SetDictInt("ACS_BSSB", (ACS_ULCORNER));
3312 SetDictInt("ACS_SSBB", (ACS_LLCORNER));
3313 SetDictInt("ACS_BBSS", (ACS_URCORNER));
3314 SetDictInt("ACS_SBBS", (ACS_LRCORNER));
3315 SetDictInt("ACS_SBSS", (ACS_RTEE));
3316 SetDictInt("ACS_SSSB", (ACS_LTEE));
3317 SetDictInt("ACS_SSBS", (ACS_BTEE));
3318 SetDictInt("ACS_BSSS", (ACS_TTEE));
3319 SetDictInt("ACS_BSBS", (ACS_HLINE));
3320 SetDictInt("ACS_SBSB", (ACS_VLINE));
3321 SetDictInt("ACS_SSSS", (ACS_PLUS));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003322
Victor Stinner26486ea2010-05-15 22:23:53 +00003323 /* The following are never available with strict SYSV curses */
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003324#ifdef ACS_S3
Victor Stinner26486ea2010-05-15 22:23:53 +00003325 SetDictInt("ACS_S3", (ACS_S3));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003326#endif
3327#ifdef ACS_S7
Victor Stinner26486ea2010-05-15 22:23:53 +00003328 SetDictInt("ACS_S7", (ACS_S7));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003329#endif
3330#ifdef ACS_LEQUAL
Victor Stinner26486ea2010-05-15 22:23:53 +00003331 SetDictInt("ACS_LEQUAL", (ACS_LEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003332#endif
3333#ifdef ACS_GEQUAL
Victor Stinner26486ea2010-05-15 22:23:53 +00003334 SetDictInt("ACS_GEQUAL", (ACS_GEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003335#endif
3336#ifdef ACS_PI
Victor Stinner26486ea2010-05-15 22:23:53 +00003337 SetDictInt("ACS_PI", (ACS_PI));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003338#endif
3339#ifdef ACS_NEQUAL
Victor Stinner26486ea2010-05-15 22:23:53 +00003340 SetDictInt("ACS_NEQUAL", (ACS_NEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003341#endif
3342#ifdef ACS_STERLING
Victor Stinner26486ea2010-05-15 22:23:53 +00003343 SetDictInt("ACS_STERLING", (ACS_STERLING));
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00003344#endif
3345
Victor Stinner26486ea2010-05-15 22:23:53 +00003346 SetDictInt("LINES", LINES);
3347 SetDictInt("COLS", COLS);
Guido van Rossum56bf2351994-08-31 22:06:24 +00003348
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003349 winobj = (PyCursesWindowObject *)PyCursesWindow_New(win, NULL);
3350 screen_encoding = winobj->encoding;
3351 return (PyObject *)winobj;
Guido van Rossumf6971e21994-08-30 12:25:20 +00003352}
3353
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003354/*[clinic input]
3355_curses.setupterm
3356
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003357 term: str(accept={str, NoneType}) = None
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003358 Terminal name.
3359 If omitted, the value of the TERM environment variable will be used.
3360 fd: int = -1
3361 File descriptor to which any initialization sequences will be sent.
3362 If not supplied, the file descriptor for sys.stdout will be used.
3363
3364Initialize the terminal.
3365[clinic start generated code]*/
3366
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003367static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003368_curses_setupterm_impl(PyObject *module, const char *term, int fd)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003369/*[clinic end generated code: output=4584e587350f2848 input=4511472766af0c12]*/
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003370{
Victor Stinner26486ea2010-05-15 22:23:53 +00003371 int err;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003372
Victor Stinner26486ea2010-05-15 22:23:53 +00003373 if (fd == -1) {
3374 PyObject* sys_stdout;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003375
Victor Stinnerbd303c12013-11-07 23:07:29 +01003376 sys_stdout = PySys_GetObject("stdout");
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003377
Victor Stinner26486ea2010-05-15 22:23:53 +00003378 if (sys_stdout == NULL || sys_stdout == Py_None) {
3379 PyErr_SetString(
3380 PyCursesError,
3381 "lost sys.stdout");
3382 return NULL;
3383 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003384
Victor Stinner26486ea2010-05-15 22:23:53 +00003385 fd = PyObject_AsFileDescriptor(sys_stdout);
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003386
Victor Stinner26486ea2010-05-15 22:23:53 +00003387 if (fd == -1) {
3388 return NULL;
3389 }
3390 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003391
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003392 if (!initialised_setupterm && setupterm((char *)term, fd, &err) == ERR) {
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02003393 const char* s = "setupterm: unknown error";
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003394
Victor Stinner26486ea2010-05-15 22:23:53 +00003395 if (err == 0) {
3396 s = "setupterm: could not find terminal";
3397 } else if (err == -1) {
3398 s = "setupterm: could not find terminfo database";
3399 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003400
Victor Stinner26486ea2010-05-15 22:23:53 +00003401 PyErr_SetString(PyCursesError,s);
3402 return NULL;
3403 }
3404
3405 initialised_setupterm = TRUE;
3406
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003407 Py_RETURN_NONE;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003408}
Guido van Rossumf6971e21994-08-30 12:25:20 +00003409
Batuhan Taşkaya4991cf42020-03-03 05:00:10 +03003410#if defined(NCURSES_EXT_FUNCS) && NCURSES_EXT_FUNCS >= 20081102
3411// https://invisible-island.net/ncurses/NEWS.html#index-t20080119
3412
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003413/*[clinic input]
Anthony Sottileb32cb972019-10-31 02:13:48 -07003414_curses.get_escdelay
3415
3416Gets the curses ESCDELAY setting.
3417
3418Gets the number of milliseconds to wait after reading an escape character,
3419to distinguish between an individual escape character entered on the
3420keyboard from escape sequences sent by cursor and function keys.
3421[clinic start generated code]*/
3422
3423static PyObject *
3424_curses_get_escdelay_impl(PyObject *module)
3425/*[clinic end generated code: output=222fa1a822555d60 input=be2d5b3dd974d0a4]*/
3426{
3427 return PyLong_FromLong(ESCDELAY);
3428}
3429/*[clinic input]
3430_curses.set_escdelay
3431 ms: int
3432 length of the delay in milliseconds.
3433 /
3434
3435Sets the curses ESCDELAY setting.
3436
3437Sets the number of milliseconds to wait after reading an escape character,
3438to distinguish between an individual escape character entered on the
3439keyboard from escape sequences sent by cursor and function keys.
3440[clinic start generated code]*/
3441
3442static PyObject *
3443_curses_set_escdelay_impl(PyObject *module, int ms)
3444/*[clinic end generated code: output=43818efbf7980ac4 input=7796fe19f111e250]*/
3445{
3446 if (ms <= 0) {
3447 PyErr_SetString(PyExc_ValueError, "ms must be > 0");
3448 return NULL;
3449 }
3450
3451 return PyCursesCheckERR(set_escdelay(ms), "set_escdelay");
3452}
3453
3454/*[clinic input]
3455_curses.get_tabsize
3456
3457Gets the curses TABSIZE setting.
3458
3459Gets the number of columns used by the curses library when converting a tab
3460character to spaces as it adds the tab to a window.
3461[clinic start generated code]*/
3462
3463static PyObject *
3464_curses_get_tabsize_impl(PyObject *module)
3465/*[clinic end generated code: output=7e9e51fb6126fbdf input=74af86bf6c9f5d7e]*/
3466{
3467 return PyLong_FromLong(TABSIZE);
3468}
3469/*[clinic input]
3470_curses.set_tabsize
3471 size: int
3472 rendered cell width of a tab character.
3473 /
3474
3475Sets the curses TABSIZE setting.
3476
3477Sets the number of columns used by the curses library when converting a tab
3478character to spaces as it adds the tab to a window.
3479[clinic start generated code]*/
3480
3481static PyObject *
3482_curses_set_tabsize_impl(PyObject *module, int size)
3483/*[clinic end generated code: output=c1de5a76c0daab1e input=78cba6a3021ad061]*/
3484{
3485 if (size <= 0) {
3486 PyErr_SetString(PyExc_ValueError, "size must be > 0");
3487 return NULL;
3488 }
3489
3490 return PyCursesCheckERR(set_tabsize(size), "set_tabsize");
3491}
Batuhan Taşkaya4991cf42020-03-03 05:00:10 +03003492#endif
Anthony Sottileb32cb972019-10-31 02:13:48 -07003493
3494/*[clinic input]
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003495_curses.intrflush
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003496
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003497 flag: bool(accept={int})
3498 /
3499
3500[clinic start generated code]*/
3501
3502static PyObject *
3503_curses_intrflush_impl(PyObject *module, int flag)
3504/*[clinic end generated code: output=c1986df35e999a0f input=fcba57bb28dfd795]*/
3505{
Victor Stinner26486ea2010-05-15 22:23:53 +00003506 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003507
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003508 return PyCursesCheckERR(intrflush(NULL, flag), "intrflush");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003509}
3510
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003511/*[clinic input]
3512_curses.isendwin
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003513
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003514Return True if endwin() has been called.
3515[clinic start generated code]*/
3516
3517static PyObject *
3518_curses_isendwin_impl(PyObject *module)
3519/*[clinic end generated code: output=d73179e4a7e1eb8c input=6cdb01a7ebf71397]*/
3520NoArgTrueFalseFunctionBody(isendwin)
3521
3522#ifdef HAVE_CURSES_IS_TERM_RESIZED
3523/*[clinic input]
3524_curses.is_term_resized
3525
3526 nlines: int
3527 Height.
3528 ncols: int
3529 Width.
3530 /
3531
3532Return True if resize_term() would modify the window structure, False otherwise.
3533[clinic start generated code]*/
3534
3535static PyObject *
3536_curses_is_term_resized_impl(PyObject *module, int nlines, int ncols)
3537/*[clinic end generated code: output=aafe04afe50f1288 input=ca9c0bd0fb8ab444]*/
3538{
Victor Stinner26486ea2010-05-15 22:23:53 +00003539 PyCursesInitialised;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003540
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003541 return PyBool_FromLong(is_term_resized(nlines, ncols));
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003542}
3543#endif /* HAVE_CURSES_IS_TERM_RESIZED */
3544
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003545/*[clinic input]
3546_curses.keyname
3547
3548 key: int
3549 Key number.
3550 /
3551
3552Return the name of specified key.
3553[clinic start generated code]*/
3554
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003555static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003556_curses_keyname_impl(PyObject *module, int key)
3557/*[clinic end generated code: output=fa2675ab3f4e056b input=ee4b1d0f243a2a2b]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003558{
Victor Stinner26486ea2010-05-15 22:23:53 +00003559 const char *knp;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003560
Victor Stinner26486ea2010-05-15 22:23:53 +00003561 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003562
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003563 if (key < 0) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003564 PyErr_SetString(PyExc_ValueError, "invalid key number");
3565 return NULL;
3566 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003567 knp = keyname(key);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003568
Serhiy Storchaka20b39b22014-09-28 11:27:24 +03003569 return PyBytes_FromString((knp == NULL) ? "" : knp);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003570}
3571
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003572/*[clinic input]
3573_curses.killchar
3574
3575Return the user's current line kill character.
3576[clinic start generated code]*/
3577
Victor Stinner26486ea2010-05-15 22:23:53 +00003578static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003579_curses_killchar_impl(PyObject *module)
3580/*[clinic end generated code: output=31c3a45b2c528269 input=1ff171c38df5ccad]*/
Victor Stinner26486ea2010-05-15 22:23:53 +00003581{
3582 char ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003583
Victor Stinner26486ea2010-05-15 22:23:53 +00003584 ch = killchar();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003585
Victor Stinner26486ea2010-05-15 22:23:53 +00003586 return PyBytes_FromStringAndSize(&ch, 1);
3587}
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003588
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003589/*[clinic input]
3590_curses.longname
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003591
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003592Return the terminfo long name field describing the current terminal.
3593
3594The maximum length of a verbose description is 128 characters. It is defined
3595only after the call to initscr().
3596[clinic start generated code]*/
3597
3598static PyObject *
3599_curses_longname_impl(PyObject *module)
3600/*[clinic end generated code: output=fdf30433727ef568 input=84c3f20201b1098e]*/
3601NoArgReturnStringFunctionBody(longname)
3602
3603/*[clinic input]
3604_curses.meta
3605
3606 yes: bool(accept={int})
3607 /
3608
3609Enable/disable meta keys.
3610
3611If yes is True, allow 8-bit characters to be input. If yes is False,
3612allow only 7-bit characters.
3613[clinic start generated code]*/
3614
3615static PyObject *
3616_curses_meta_impl(PyObject *module, int yes)
3617/*[clinic end generated code: output=22f5abda46a605d8 input=af9892e3a74f35db]*/
3618{
Victor Stinner26486ea2010-05-15 22:23:53 +00003619 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003620
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003621 return PyCursesCheckERR(meta(stdscr, yes), "meta");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003622}
3623
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003624#ifdef NCURSES_MOUSE_VERSION
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003625/*[clinic input]
3626_curses.mouseinterval
3627
3628 interval: int
3629 Time in milliseconds.
3630 /
3631
3632Set and retrieve the maximum time between press and release in a click.
3633
3634Set the maximum time that can elapse between press and release events in
3635order for them to be recognized as a click, and return the previous interval
3636value.
3637[clinic start generated code]*/
3638
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003639static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003640_curses_mouseinterval_impl(PyObject *module, int interval)
3641/*[clinic end generated code: output=c4f5ff04354634c5 input=75aaa3f0db10ac4e]*/
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003642{
Victor Stinner26486ea2010-05-15 22:23:53 +00003643 PyCursesInitialised;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003644
Victor Stinner26486ea2010-05-15 22:23:53 +00003645 return PyCursesCheckERR(mouseinterval(interval), "mouseinterval");
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003646}
3647
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003648/*[clinic input]
3649_curses.mousemask
3650
3651 newmask: unsigned_long(bitwise=True)
3652 /
3653
3654Set the mouse events to be reported, and return a tuple (availmask, oldmask).
3655
3656Return a tuple (availmask, oldmask). availmask indicates which of the
3657specified mouse events can be reported; on complete failure it returns 0.
3658oldmask is the previous value of the given window's mouse event mask.
3659If this function is never called, no mouse events are ever reported.
3660[clinic start generated code]*/
3661
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003662static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003663_curses_mousemask_impl(PyObject *module, unsigned long newmask)
3664/*[clinic end generated code: output=9406cf1b8a36e485 input=bdf76b7568a3c541]*/
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003665{
Victor Stinner26486ea2010-05-15 22:23:53 +00003666 mmask_t oldmask, availmask;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003667
Victor Stinner26486ea2010-05-15 22:23:53 +00003668 PyCursesInitialised;
Serhiy Storchaka4f469c02017-11-01 20:48:49 +02003669 availmask = mousemask((mmask_t)newmask, &oldmask);
3670 return Py_BuildValue("(kk)",
3671 (unsigned long)availmask, (unsigned long)oldmask);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003672}
3673#endif
3674
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003675/*[clinic input]
3676_curses.napms
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +00003677
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003678 ms: int
3679 Duration in milliseconds.
3680 /
3681
3682Sleep for specified time.
3683[clinic start generated code]*/
3684
3685static PyObject *
3686_curses_napms_impl(PyObject *module, int ms)
3687/*[clinic end generated code: output=a40a1da2e39ea438 input=20cd3af2b6900f56]*/
3688{
Andrew M. Kuchling2b146762010-05-10 23:30:25 +00003689 PyCursesInitialised;
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +00003690
3691 return Py_BuildValue("i", napms(ms));
3692}
3693
3694
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003695/*[clinic input]
3696_curses.newpad
3697
3698 nlines: int
3699 Height.
3700 ncols: int
3701 Width.
3702 /
3703
3704Create and return a pointer to a new pad data structure.
3705[clinic start generated code]*/
3706
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +00003707static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003708_curses_newpad_impl(PyObject *module, int nlines, int ncols)
3709/*[clinic end generated code: output=de52a56eb1098ec9 input=93f1272f240d8894]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003710{
Victor Stinner26486ea2010-05-15 22:23:53 +00003711 WINDOW *win;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003712
Victor Stinner26486ea2010-05-15 22:23:53 +00003713 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003714
Victor Stinner26486ea2010-05-15 22:23:53 +00003715 win = newpad(nlines, ncols);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003716
Victor Stinner26486ea2010-05-15 22:23:53 +00003717 if (win == NULL) {
3718 PyErr_SetString(PyCursesError, catchall_NULL);
3719 return NULL;
3720 }
3721
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003722 return (PyObject *)PyCursesWindow_New(win, NULL);
Guido van Rossumf6971e21994-08-30 12:25:20 +00003723}
3724
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003725/*[clinic input]
3726_curses.newwin
3727
3728 nlines: int
3729 Height.
3730 ncols: int
3731 Width.
3732 [
3733 begin_y: int = 0
3734 Top side y-coordinate.
3735 begin_x: int = 0
3736 Left side x-coordinate.
3737 ]
3738 /
3739
3740Return a new window.
3741
3742By default, the window will extend from the specified position to the lower
3743right corner of the screen.
3744[clinic start generated code]*/
3745
Guido van Rossumf6971e21994-08-30 12:25:20 +00003746static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003747_curses_newwin_impl(PyObject *module, int nlines, int ncols,
3748 int group_right_1, int begin_y, int begin_x)
3749/*[clinic end generated code: output=c1e0a8dc8ac2826c input=29312c15a72a003d]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00003750{
Victor Stinner26486ea2010-05-15 22:23:53 +00003751 WINDOW *win;
Guido van Rossum85738471995-02-17 13:50:17 +00003752
Victor Stinner26486ea2010-05-15 22:23:53 +00003753 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003754
Victor Stinner26486ea2010-05-15 22:23:53 +00003755 win = newwin(nlines,ncols,begin_y,begin_x);
3756 if (win == NULL) {
3757 PyErr_SetString(PyCursesError, catchall_NULL);
3758 return NULL;
3759 }
Guido van Rossum85738471995-02-17 13:50:17 +00003760
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003761 return (PyObject *)PyCursesWindow_New(win, NULL);
Guido van Rossumf6971e21994-08-30 12:25:20 +00003762}
3763
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003764/*[clinic input]
3765_curses.nl
3766
3767 flag: bool(accept={int}) = True
3768 If false, the effect is the same as calling nonl().
3769 /
3770
3771Enter newline mode.
3772
3773This mode translates the return key into newline on input, and translates
3774newline into return and line-feed on output. Newline mode is initially on.
3775[clinic start generated code]*/
3776
Guido van Rossumf6971e21994-08-30 12:25:20 +00003777static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003778_curses_nl_impl(PyObject *module, int flag)
3779/*[clinic end generated code: output=b39cc0ffc9015003 input=cf36a63f7b86e28a]*/
3780NoArgOrFlagNoReturnFunctionBody(nl, flag)
3781
3782/*[clinic input]
3783_curses.nocbreak
3784
3785Leave cbreak mode.
3786
3787Return to normal "cooked" mode with line buffering.
3788[clinic start generated code]*/
3789
3790static PyObject *
3791_curses_nocbreak_impl(PyObject *module)
3792/*[clinic end generated code: output=eabf3833a4fbf620 input=e4b65f7d734af400]*/
3793NoArgNoReturnFunctionBody(nocbreak)
3794
3795/*[clinic input]
3796_curses.noecho
3797
3798Leave echo mode.
3799
3800Echoing of input characters is turned off.
3801[clinic start generated code]*/
3802
3803static PyObject *
3804_curses_noecho_impl(PyObject *module)
3805/*[clinic end generated code: output=cc95ab45bc98f41b input=76714df529e614c3]*/
3806NoArgNoReturnFunctionBody(noecho)
3807
3808/*[clinic input]
3809_curses.nonl
3810
3811Leave newline mode.
3812
3813Disable translation of return into newline on input, and disable low-level
3814translation of newline into newline/return on output.
3815[clinic start generated code]*/
3816
3817static PyObject *
3818_curses_nonl_impl(PyObject *module)
3819/*[clinic end generated code: output=99e917e9715770c6 input=9d37dd122d3022fc]*/
3820NoArgNoReturnFunctionBody(nonl)
3821
3822/*[clinic input]
3823_curses.noqiflush
3824
3825Disable queue flushing.
3826
3827When queue flushing is disabled, normal flush of input and output queues
3828associated with the INTR, QUIT and SUSP characters will not be done.
3829[clinic start generated code]*/
3830
3831static PyObject *
3832_curses_noqiflush_impl(PyObject *module)
3833/*[clinic end generated code: output=8b95a4229bbf0877 input=ba3e6b2e3e54c4df]*/
3834NoArgNoReturnVoidFunctionBody(noqiflush)
3835
3836/*[clinic input]
3837_curses.noraw
3838
3839Leave raw mode.
3840
3841Return to normal "cooked" mode with line buffering.
3842[clinic start generated code]*/
3843
3844static PyObject *
3845_curses_noraw_impl(PyObject *module)
3846/*[clinic end generated code: output=39894e5524c430cc input=6ec86692096dffb5]*/
3847NoArgNoReturnFunctionBody(noraw)
3848
3849/*[clinic input]
3850_curses.pair_content
3851
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -05003852 pair_number: pair
Serhiy Storchaka1470edd2021-01-03 22:51:11 +02003853 The number of the color pair (0 - (COLOR_PAIRS-1)).
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003854 /
3855
3856Return a tuple (fg, bg) containing the colors for the requested color pair.
3857[clinic start generated code]*/
3858
3859static PyObject *
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -05003860_curses_pair_content_impl(PyObject *module, int pair_number)
Serhiy Storchaka1470edd2021-01-03 22:51:11 +02003861/*[clinic end generated code: output=4a726dd0e6885f3f input=03970f840fc7b739]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00003862{
Serhiy Storchaka59f9b4e2021-01-05 09:13:15 +02003863 _CURSES_COLOR_NUM_TYPE f, b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003864
Victor Stinner26486ea2010-05-15 22:23:53 +00003865 PyCursesInitialised;
3866 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003867
Serhiy Storchaka59f9b4e2021-01-05 09:13:15 +02003868 if (_CURSES_PAIR_CONTENT_FUNC(pair_number, &f, &b) == ERR) {
3869 if (pair_number >= COLOR_PAIRS) {
3870 PyErr_Format(PyExc_ValueError,
3871 "Color pair is greater than COLOR_PAIRS-1 (%d).",
3872 COLOR_PAIRS - 1);
3873 }
3874 else {
3875 PyErr_Format(PyCursesError, "%s() returned ERR",
3876 Py_STRINGIFY(_CURSES_PAIR_CONTENT_FUNC));
3877 }
Victor Stinner26486ea2010-05-15 22:23:53 +00003878 return NULL;
3879 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003880
Victor Stinner26486ea2010-05-15 22:23:53 +00003881 return Py_BuildValue("(ii)", f, b);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003882}
3883
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003884/*[clinic input]
3885_curses.pair_number
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003886
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003887 attr: int
3888 /
3889
3890Return the number of the color-pair set by the specified attribute value.
3891
3892color_pair() is the counterpart to this function.
3893[clinic start generated code]*/
3894
3895static PyObject *
3896_curses_pair_number_impl(PyObject *module, int attr)
3897/*[clinic end generated code: output=85bce7d65c0aa3f4 input=d478548e33f5e61a]*/
3898{
Victor Stinner26486ea2010-05-15 22:23:53 +00003899 PyCursesInitialised;
3900 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003901
Serhiy Storchaka1470edd2021-01-03 22:51:11 +02003902 return PyLong_FromLong(PAIR_NUMBER(attr));
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003903}
3904
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003905/*[clinic input]
3906_curses.putp
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003907
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003908 string: str(accept={robuffer})
3909 /
3910
3911Emit the value of a specified terminfo capability for the current terminal.
3912
3913Note that the output of putp() always goes to standard output.
3914[clinic start generated code]*/
3915
3916static PyObject *
3917_curses_putp_impl(PyObject *module, const char *string)
3918/*[clinic end generated code: output=e98081d1b8eb5816 input=1601faa828b44cb3]*/
3919{
3920 return PyCursesCheckERR(putp(string), "putp");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003921}
3922
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003923/*[clinic input]
3924_curses.qiflush
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003925
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003926 flag: bool(accept={int}) = True
3927 If false, the effect is the same as calling noqiflush().
3928 /
3929
3930Enable queue flushing.
3931
3932If queue flushing is enabled, all output in the display driver queue
3933will be flushed when the INTR, QUIT and SUSP characters are read.
3934[clinic start generated code]*/
3935
3936static PyObject *
3937_curses_qiflush_impl(PyObject *module, int flag)
3938/*[clinic end generated code: output=9167e862f760ea30 input=e9e4a389946a0dbc]*/
3939{
Victor Stinner26486ea2010-05-15 22:23:53 +00003940 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003941
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003942 if (flag) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003943 qiflush();
Victor Stinner26486ea2010-05-15 22:23:53 +00003944 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003945 else {
3946 noqiflush();
3947 }
3948 Py_RETURN_NONE;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003949}
3950
Guido van Rossumd8faa362007-04-27 19:54:29 +00003951/* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES
3952 * and _curses.COLS */
Benjamin Petersonfea6a942008-07-02 16:11:42 +00003953#if defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM)
Guido van Rossumd8faa362007-04-27 19:54:29 +00003954static int
3955update_lines_cols(void)
3956{
Victor Stinner26486ea2010-05-15 22:23:53 +00003957 PyObject *o;
3958 PyObject *m = PyImport_ImportModuleNoBlock("curses");
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003959 _Py_IDENTIFIER(LINES);
3960 _Py_IDENTIFIER(COLS);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003961
Victor Stinner26486ea2010-05-15 22:23:53 +00003962 if (!m)
3963 return 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003964
Victor Stinner26486ea2010-05-15 22:23:53 +00003965 o = PyLong_FromLong(LINES);
3966 if (!o) {
3967 Py_DECREF(m);
3968 return 0;
3969 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003970 if (_PyObject_SetAttrId(m, &PyId_LINES, o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003971 Py_DECREF(m);
3972 Py_DECREF(o);
3973 return 0;
3974 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003975 /* PyId_LINES.object will be initialized here. */
Victor Stinner4804b5b2020-05-12 01:43:38 +02003976 if (PyDict_SetItem(ModDict, _PyUnicode_FromId(&PyId_LINES), o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003977 Py_DECREF(m);
3978 Py_DECREF(o);
3979 return 0;
3980 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003981 Py_DECREF(o);
Victor Stinner26486ea2010-05-15 22:23:53 +00003982 o = PyLong_FromLong(COLS);
3983 if (!o) {
3984 Py_DECREF(m);
3985 return 0;
3986 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003987 if (_PyObject_SetAttrId(m, &PyId_COLS, o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003988 Py_DECREF(m);
3989 Py_DECREF(o);
3990 return 0;
3991 }
Victor Stinner4804b5b2020-05-12 01:43:38 +02003992 if (PyDict_SetItem(ModDict, _PyUnicode_FromId(&PyId_COLS), o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003993 Py_DECREF(m);
3994 Py_DECREF(o);
3995 return 0;
3996 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003997 Py_DECREF(o);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003998 Py_DECREF(m);
Victor Stinner26486ea2010-05-15 22:23:53 +00003999 return 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004000}
Steve Dowerd2bc3892015-04-15 18:06:05 -04004001
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004002/*[clinic input]
Zackery Spytz2bc34342019-11-17 10:10:13 -07004003_curses.update_lines_cols
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004004
4005[clinic start generated code]*/
4006
Zackery Spytz2bc34342019-11-17 10:10:13 -07004007static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004008_curses_update_lines_cols_impl(PyObject *module)
Zackery Spytz2bc34342019-11-17 10:10:13 -07004009/*[clinic end generated code: output=423f2b1e63ed0f75 input=5f065ab7a28a5d90]*/
Steve Dowerd2bc3892015-04-15 18:06:05 -04004010{
Zackery Spytz2bc34342019-11-17 10:10:13 -07004011 if (!update_lines_cols()) {
4012 return NULL;
4013 }
4014 Py_RETURN_NONE;
Steve Dowerd2bc3892015-04-15 18:06:05 -04004015}
4016
Benjamin Petersonfea6a942008-07-02 16:11:42 +00004017#endif
Guido van Rossumd8faa362007-04-27 19:54:29 +00004018
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004019/*[clinic input]
4020_curses.raw
4021
4022 flag: bool(accept={int}) = True
4023 If false, the effect is the same as calling noraw().
4024 /
4025
4026Enter raw mode.
4027
4028In raw mode, normal line buffering and processing of interrupt, quit,
4029suspend, and flow control keys are turned off; characters are presented to
4030curses input functions one by one.
4031[clinic start generated code]*/
4032
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004033static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004034_curses_raw_impl(PyObject *module, int flag)
4035/*[clinic end generated code: output=a750e4b342be015b input=e36d8db27832b848]*/
4036NoArgOrFlagNoReturnFunctionBody(raw, flag)
4037
4038/*[clinic input]
4039_curses.reset_prog_mode
4040
4041Restore the terminal to "program" mode, as previously saved by def_prog_mode().
4042[clinic start generated code]*/
4043
4044static PyObject *
4045_curses_reset_prog_mode_impl(PyObject *module)
4046/*[clinic end generated code: output=15eb765abf0b6575 input=3d82bea2b3243471]*/
4047NoArgNoReturnFunctionBody(reset_prog_mode)
4048
4049/*[clinic input]
4050_curses.reset_shell_mode
4051
4052Restore the terminal to "shell" mode, as previously saved by def_shell_mode().
4053[clinic start generated code]*/
4054
4055static PyObject *
4056_curses_reset_shell_mode_impl(PyObject *module)
4057/*[clinic end generated code: output=0238de2962090d33 input=1c738fa64bd1a24f]*/
4058NoArgNoReturnFunctionBody(reset_shell_mode)
4059
4060/*[clinic input]
4061_curses.resetty
4062
4063Restore terminal mode.
4064[clinic start generated code]*/
4065
4066static PyObject *
4067_curses_resetty_impl(PyObject *module)
4068/*[clinic end generated code: output=ff4b448e80a7cd63 input=940493de03624bb0]*/
4069NoArgNoReturnFunctionBody(resetty)
4070
4071#ifdef HAVE_CURSES_RESIZETERM
4072/*[clinic input]
4073_curses.resizeterm
4074
4075 nlines: int
4076 Height.
4077 ncols: int
4078 Width.
4079 /
4080
4081Resize the standard and current windows to the specified dimensions.
4082
4083Adjusts other bookkeeping data used by the curses library that record the
4084window dimensions (in particular the SIGWINCH handler).
4085[clinic start generated code]*/
4086
4087static PyObject *
4088_curses_resizeterm_impl(PyObject *module, int nlines, int ncols)
4089/*[clinic end generated code: output=56d6bcc5194ad055 input=0fca02ebad5ffa82]*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004090{
Victor Stinner26486ea2010-05-15 22:23:53 +00004091 PyObject *result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004092
Victor Stinner26486ea2010-05-15 22:23:53 +00004093 PyCursesInitialised;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004094
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004095 result = PyCursesCheckERR(resizeterm(nlines, ncols), "resizeterm");
Victor Stinner26486ea2010-05-15 22:23:53 +00004096 if (!result)
4097 return NULL;
Zackery Spytz2bc34342019-11-17 10:10:13 -07004098 if (!update_lines_cols()) {
4099 Py_DECREF(result);
Victor Stinner26486ea2010-05-15 22:23:53 +00004100 return NULL;
Zackery Spytz2bc34342019-11-17 10:10:13 -07004101 }
Victor Stinner26486ea2010-05-15 22:23:53 +00004102 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004103}
4104
4105#endif
4106
4107#ifdef HAVE_CURSES_RESIZE_TERM
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004108/*[clinic input]
4109_curses.resize_term
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004110
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004111 nlines: int
4112 Height.
4113 ncols: int
4114 Width.
4115 /
4116
4117Backend function used by resizeterm(), performing most of the work.
4118
4119When resizing the windows, resize_term() blank-fills the areas that are
4120extended. The calling application should fill in these areas with appropriate
4121data. The resize_term() function attempts to resize all windows. However,
4122due to the calling convention of pads, it is not possible to resize these
4123without additional interaction with the application.
4124[clinic start generated code]*/
4125
4126static PyObject *
4127_curses_resize_term_impl(PyObject *module, int nlines, int ncols)
4128/*[clinic end generated code: output=9e26d8b9ea311ed2 input=2197edd05b049ed4]*/
4129{
Victor Stinner26486ea2010-05-15 22:23:53 +00004130 PyObject *result;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004131
Victor Stinner26486ea2010-05-15 22:23:53 +00004132 PyCursesInitialised;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004133
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004134 result = PyCursesCheckERR(resize_term(nlines, ncols), "resize_term");
Victor Stinner26486ea2010-05-15 22:23:53 +00004135 if (!result)
4136 return NULL;
Zackery Spytz2bc34342019-11-17 10:10:13 -07004137 if (!update_lines_cols()) {
4138 Py_DECREF(result);
Victor Stinner26486ea2010-05-15 22:23:53 +00004139 return NULL;
Zackery Spytz2bc34342019-11-17 10:10:13 -07004140 }
Victor Stinner26486ea2010-05-15 22:23:53 +00004141 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004142}
4143#endif /* HAVE_CURSES_RESIZE_TERM */
4144
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004145/*[clinic input]
4146_curses.savetty
4147
4148Save terminal mode.
4149[clinic start generated code]*/
4150
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004151static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004152_curses_savetty_impl(PyObject *module)
4153/*[clinic end generated code: output=6babc49f12b42199 input=fce6b2b7d2200102]*/
4154NoArgNoReturnFunctionBody(savetty)
4155
4156#ifdef getsyx
4157/*[clinic input]
4158_curses.setsyx
4159
4160 y: int
4161 Y-coordinate.
4162 x: int
4163 X-coordinate.
4164 /
4165
4166Set the virtual screen cursor.
4167
4168If y and x are both -1, then leaveok is set.
4169[clinic start generated code]*/
4170
4171static PyObject *
4172_curses_setsyx_impl(PyObject *module, int y, int x)
4173/*[clinic end generated code: output=23dcf753511a2464 input=fa7f2b208e10a557]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004174{
Victor Stinner26486ea2010-05-15 22:23:53 +00004175 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004176
Victor Stinner26486ea2010-05-15 22:23:53 +00004177 setsyx(y,x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004178
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004179 Py_RETURN_NONE;
Guido van Rossumf6971e21994-08-30 12:25:20 +00004180}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02004181#endif
Guido van Rossumf6971e21994-08-30 12:25:20 +00004182
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004183/*[clinic input]
4184_curses.start_color
4185
4186Initializes eight basic colors and global variables COLORS and COLOR_PAIRS.
4187
4188Must be called if the programmer wants to use colors, and before any other
4189color manipulation routine is called. It is good practice to call this
4190routine right after initscr().
4191
4192It also restores the colors on the terminal to the values they had when the
4193terminal was just turned on.
4194[clinic start generated code]*/
4195
Guido van Rossumf6971e21994-08-30 12:25:20 +00004196static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004197_curses_start_color_impl(PyObject *module)
4198/*[clinic end generated code: output=8b772b41d8090ede input=0ca0ecb2b77e1a12]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00004199{
Victor Stinner26486ea2010-05-15 22:23:53 +00004200 int code;
4201 PyObject *c, *cp;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004202
Victor Stinner26486ea2010-05-15 22:23:53 +00004203 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004204
Victor Stinner26486ea2010-05-15 22:23:53 +00004205 code = start_color();
4206 if (code != ERR) {
4207 initialisedcolors = TRUE;
4208 c = PyLong_FromLong((long) COLORS);
Christian Heimesa956e642013-07-26 14:45:37 +02004209 if (c == NULL)
4210 return NULL;
Zackery Spytz2bc34342019-11-17 10:10:13 -07004211 if (PyDict_SetItemString(ModDict, "COLORS", c) < 0) {
4212 Py_DECREF(c);
4213 return NULL;
4214 }
Victor Stinner26486ea2010-05-15 22:23:53 +00004215 Py_DECREF(c);
4216 cp = PyLong_FromLong((long) COLOR_PAIRS);
Christian Heimesa956e642013-07-26 14:45:37 +02004217 if (cp == NULL)
4218 return NULL;
Zackery Spytz2bc34342019-11-17 10:10:13 -07004219 if (PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp) < 0) {
4220 Py_DECREF(cp);
4221 return NULL;
4222 }
Victor Stinner26486ea2010-05-15 22:23:53 +00004223 Py_DECREF(cp);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004224 Py_RETURN_NONE;
Victor Stinner26486ea2010-05-15 22:23:53 +00004225 } else {
4226 PyErr_SetString(PyCursesError, "start_color() returned ERR");
4227 return NULL;
4228 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004229}
4230
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004231/*[clinic input]
4232_curses.termattrs
4233
4234Return a logical OR of all video attributes supported by the terminal.
4235[clinic start generated code]*/
4236
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004237static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004238_curses_termattrs_impl(PyObject *module)
4239/*[clinic end generated code: output=b06f437fce1b6fc4 input=0559882a04f84d1d]*/
4240NoArgReturnIntFunctionBody(termattrs)
4241
4242/*[clinic input]
4243_curses.termname
4244
4245Return the value of the environment variable TERM, truncated to 14 characters.
4246[clinic start generated code]*/
4247
4248static PyObject *
4249_curses_termname_impl(PyObject *module)
4250/*[clinic end generated code: output=96375577ebbd67fd input=33c08d000944f33f]*/
4251NoArgReturnStringFunctionBody(termname)
4252
4253/*[clinic input]
4254_curses.tigetflag
4255
4256 capname: str
4257 The terminfo capability name.
4258 /
4259
4260Return the value of the Boolean capability.
4261
4262The value -1 is returned if capname is not a Boolean capability, or 0 if
4263it is canceled or absent from the terminal description.
4264[clinic start generated code]*/
4265
4266static PyObject *
4267_curses_tigetflag_impl(PyObject *module, const char *capname)
4268/*[clinic end generated code: output=8853c0e55542195b input=b0787af9e3e9a6ce]*/
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004269{
Victor Stinner26486ea2010-05-15 22:23:53 +00004270 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004271
Xiang Zhang44742e92018-06-23 12:29:30 +08004272 return PyLong_FromLong( (long) tigetflag( (char *)capname ) );
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004273}
4274
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004275/*[clinic input]
4276_curses.tigetnum
4277
4278 capname: str
4279 The terminfo capability name.
4280 /
4281
4282Return the value of the numeric capability.
4283
4284The value -2 is returned if capname is not a numeric capability, or -1 if
4285it is canceled or absent from the terminal description.
4286[clinic start generated code]*/
4287
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004288static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004289_curses_tigetnum_impl(PyObject *module, const char *capname)
4290/*[clinic end generated code: output=46f8b0a1b5dff42f input=5cdf2f410b109720]*/
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004291{
Victor Stinner26486ea2010-05-15 22:23:53 +00004292 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004293
Xiang Zhang44742e92018-06-23 12:29:30 +08004294 return PyLong_FromLong( (long) tigetnum( (char *)capname ) );
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004295}
4296
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004297/*[clinic input]
4298_curses.tigetstr
4299
4300 capname: str
4301 The terminfo capability name.
4302 /
4303
4304Return the value of the string capability.
4305
4306None is returned if capname is not a string capability, or is canceled or
4307absent from the terminal description.
4308[clinic start generated code]*/
4309
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004310static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004311_curses_tigetstr_impl(PyObject *module, const char *capname)
4312/*[clinic end generated code: output=f22b576ad60248f3 input=36644df25c73c0a7]*/
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004313{
Victor Stinner26486ea2010-05-15 22:23:53 +00004314 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004315
Xiang Zhang44742e92018-06-23 12:29:30 +08004316 capname = tigetstr( (char *)capname );
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +02004317 if (capname == NULL || capname == (char*) -1) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004318 Py_RETURN_NONE;
Victor Stinner26486ea2010-05-15 22:23:53 +00004319 }
4320 return PyBytes_FromString( capname );
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004321}
4322
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004323/*[clinic input]
4324_curses.tparm
4325
4326 str: str(accept={robuffer})
4327 Parameterized byte string obtained from the terminfo database.
4328 i1: int = 0
4329 i2: int = 0
4330 i3: int = 0
4331 i4: int = 0
4332 i5: int = 0
4333 i6: int = 0
4334 i7: int = 0
4335 i8: int = 0
4336 i9: int = 0
4337 /
4338
4339Instantiate the specified byte string with the supplied parameters.
4340[clinic start generated code]*/
4341
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004342static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004343_curses_tparm_impl(PyObject *module, const char *str, int i1, int i2, int i3,
4344 int i4, int i5, int i6, int i7, int i8, int i9)
4345/*[clinic end generated code: output=599f62b615c667ff input=5e30b15786f032aa]*/
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00004346{
Victor Stinner26486ea2010-05-15 22:23:53 +00004347 char* result = NULL;
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00004348
Victor Stinner26486ea2010-05-15 22:23:53 +00004349 PyCursesSetupTermCalled;
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00004350
Xiang Zhang44742e92018-06-23 12:29:30 +08004351 result = tparm((char *)str,i1,i2,i3,i4,i5,i6,i7,i8,i9);
Victor Stinner26486ea2010-05-15 22:23:53 +00004352 if (!result) {
4353 PyErr_SetString(PyCursesError, "tparm() returned NULL");
4354 return NULL;
4355 }
Michael W. Hudson21007982002-11-21 14:17:51 +00004356
Victor Stinner26486ea2010-05-15 22:23:53 +00004357 return PyBytes_FromString(result);
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00004358}
4359
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02004360#ifdef HAVE_CURSES_TYPEAHEAD
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004361/*[clinic input]
4362_curses.typeahead
4363
4364 fd: int
4365 File descriptor.
4366 /
4367
4368Specify that the file descriptor fd be used for typeahead checking.
4369
4370If fd is -1, then no typeahead checking is done.
4371[clinic start generated code]*/
4372
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00004373static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004374_curses_typeahead_impl(PyObject *module, int fd)
4375/*[clinic end generated code: output=084bb649d7066583 input=f2968d8e1805051b]*/
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00004376{
Victor Stinner26486ea2010-05-15 22:23:53 +00004377 PyCursesInitialised;
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00004378
Victor Stinner26486ea2010-05-15 22:23:53 +00004379 return PyCursesCheckERR(typeahead( fd ), "typeahead");
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00004380}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02004381#endif
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00004382
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004383/*[clinic input]
4384_curses.unctrl
4385
4386 ch: object
4387 /
4388
4389Return a string which is a printable representation of the character ch.
4390
4391Control characters are displayed as a caret followed by the character,
4392for example as ^C. Printing characters are left as they are.
4393[clinic start generated code]*/
4394
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00004395static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004396_curses_unctrl(PyObject *module, PyObject *ch)
4397/*[clinic end generated code: output=8e07fafc430c9434 input=cd1e35e16cd1ace4]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004398{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004399 chtype ch_;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004400
Victor Stinner26486ea2010-05-15 22:23:53 +00004401 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004402
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004403 if (!PyCurses_ConvertToChtype(NULL, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00004404 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004405
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004406 return PyBytes_FromString(unctrl(ch_));
Guido van Rossumf6971e21994-08-30 12:25:20 +00004407}
4408
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004409/*[clinic input]
4410_curses.ungetch
4411
4412 ch: object
4413 /
4414
4415Push ch so the next getch() will return it.
4416[clinic start generated code]*/
4417
Guido van Rossumf6971e21994-08-30 12:25:20 +00004418static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004419_curses_ungetch(PyObject *module, PyObject *ch)
4420/*[clinic end generated code: output=9b19d8268376d887 input=6681e6ae4c42e5eb]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00004421{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004422 chtype ch_;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004423
Victor Stinner26486ea2010-05-15 22:23:53 +00004424 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004425
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004426 if (!PyCurses_ConvertToChtype(NULL, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00004427 return NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01004428
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004429 return PyCursesCheckERR(ungetch(ch_), "ungetch");
Guido van Rossumf6971e21994-08-30 12:25:20 +00004430}
4431
Victor Stinner71e44cb2011-09-06 01:53:03 +02004432#ifdef HAVE_NCURSESW
4433/* Convert an object to a character (wchar_t):
4434
4435 - int
4436 - str of length 1
4437
4438 Return 1 on success, 0 on error. */
4439static int
4440PyCurses_ConvertToWchar_t(PyObject *obj,
4441 wchar_t *wch)
4442{
4443 if (PyUnicode_Check(obj)) {
4444 wchar_t buffer[2];
4445 if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) {
4446 PyErr_Format(PyExc_TypeError,
Anthony Sottilec9345e32019-07-31 05:11:24 -07004447 "expect str of length 1 or int, "
Victor Stinner71e44cb2011-09-06 01:53:03 +02004448 "got a str of length %zi",
Victor Stinnerc4f281e2011-10-11 22:11:42 +02004449 PyUnicode_GET_LENGTH(obj));
Victor Stinner71e44cb2011-09-06 01:53:03 +02004450 return 0;
4451 }
4452 *wch = buffer[0];
4453 return 2;
4454 }
4455 else if (PyLong_CheckExact(obj)) {
4456 long value;
4457 int overflow;
4458 value = PyLong_AsLongAndOverflow(obj, &overflow);
4459 if (overflow) {
4460 PyErr_SetString(PyExc_OverflowError,
4461 "int doesn't fit in long");
4462 return 0;
4463 }
4464 *wch = (wchar_t)value;
4465 if ((long)*wch != value) {
4466 PyErr_Format(PyExc_OverflowError,
4467 "character doesn't fit in wchar_t");
4468 return 0;
4469 }
4470 return 1;
4471 }
4472 else {
4473 PyErr_Format(PyExc_TypeError,
Anthony Sottilec9345e32019-07-31 05:11:24 -07004474 "expect str of length 1 or int, got %s",
Victor Stinner71e44cb2011-09-06 01:53:03 +02004475 Py_TYPE(obj)->tp_name);
4476 return 0;
4477 }
4478}
4479
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004480/*[clinic input]
4481_curses.unget_wch
4482
4483 ch: object
4484 /
4485
4486Push ch so the next get_wch() will return it.
4487[clinic start generated code]*/
4488
Victor Stinner71e44cb2011-09-06 01:53:03 +02004489static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004490_curses_unget_wch(PyObject *module, PyObject *ch)
4491/*[clinic end generated code: output=1974c9fb01d37863 input=0d56dc65a46feebb]*/
Victor Stinner71e44cb2011-09-06 01:53:03 +02004492{
Victor Stinner71e44cb2011-09-06 01:53:03 +02004493 wchar_t wch;
4494
4495 PyCursesInitialised;
4496
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004497 if (!PyCurses_ConvertToWchar_t(ch, &wch))
Victor Stinner71e44cb2011-09-06 01:53:03 +02004498 return NULL;
4499 return PyCursesCheckERR(unget_wch(wch), "unget_wch");
4500}
4501#endif
4502
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004503#ifdef HAVE_CURSES_USE_ENV
4504/*[clinic input]
4505_curses.use_env
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004506
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004507 flag: bool(accept={int})
4508 /
4509
4510Use environment variables LINES and COLUMNS.
4511
4512If used, this function should be called before initscr() or newterm() are
4513called.
4514
4515When flag is False, the values of lines and columns specified in the terminfo
4516database will be used, even if environment variables LINES and COLUMNS (used
4517by default) are set, or if curses is running in a window (in which case
4518default behavior would be to use the window size if LINES and COLUMNS are
4519not set).
4520[clinic start generated code]*/
4521
4522static PyObject *
4523_curses_use_env_impl(PyObject *module, int flag)
4524/*[clinic end generated code: output=b2c445e435c0b164 input=1778eb1e9151ea37]*/
4525{
Victor Stinner26486ea2010-05-15 22:23:53 +00004526 use_env(flag);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004527 Py_RETURN_NONE;
Guido van Rossumf6971e21994-08-30 12:25:20 +00004528}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02004529#endif
Guido van Rossumf6971e21994-08-30 12:25:20 +00004530
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004531#ifndef STRICT_SYSV_CURSES
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004532/*[clinic input]
4533_curses.use_default_colors
4534
4535Allow use of default values for colors on terminals supporting this feature.
4536
4537Use this to support transparency in your application. The default color
4538is assigned to the color number -1.
4539[clinic start generated code]*/
4540
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004541static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004542_curses_use_default_colors_impl(PyObject *module)
4543/*[clinic end generated code: output=a3b81ff71dd901be input=656844367470e8fc]*/
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004544{
Victor Stinner26486ea2010-05-15 22:23:53 +00004545 int code;
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004546
Victor Stinner26486ea2010-05-15 22:23:53 +00004547 PyCursesInitialised;
4548 PyCursesInitialisedColor;
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004549
Victor Stinner26486ea2010-05-15 22:23:53 +00004550 code = use_default_colors();
4551 if (code != ERR) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004552 Py_RETURN_NONE;
Victor Stinner26486ea2010-05-15 22:23:53 +00004553 } else {
4554 PyErr_SetString(PyCursesError, "use_default_colors() returned ERR");
4555 return NULL;
4556 }
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004557}
4558#endif /* STRICT_SYSV_CURSES */
4559
Serhiy Storchakab232df92018-10-30 13:22:42 +02004560
4561#ifdef NCURSES_VERSION
4562
4563PyDoc_STRVAR(ncurses_version__doc__,
4564"curses.ncurses_version\n\
4565\n\
4566Ncurses version information as a named tuple.");
4567
4568static PyTypeObject NcursesVersionType;
4569
4570static PyStructSequence_Field ncurses_version_fields[] = {
4571 {"major", "Major release number"},
4572 {"minor", "Minor release number"},
4573 {"patch", "Patch release number"},
4574 {0}
4575};
4576
4577static PyStructSequence_Desc ncurses_version_desc = {
4578 "curses.ncurses_version", /* name */
4579 ncurses_version__doc__, /* doc */
4580 ncurses_version_fields, /* fields */
4581 3
4582};
4583
4584static PyObject *
4585make_ncurses_version(void)
4586{
4587 PyObject *ncurses_version;
4588 int pos = 0;
4589
4590 ncurses_version = PyStructSequence_New(&NcursesVersionType);
4591 if (ncurses_version == NULL) {
4592 return NULL;
4593 }
4594
4595#define SetIntItem(flag) \
4596 PyStructSequence_SET_ITEM(ncurses_version, pos++, PyLong_FromLong(flag)); \
4597 if (PyErr_Occurred()) { \
4598 Py_CLEAR(ncurses_version); \
4599 return NULL; \
4600 }
4601
4602 SetIntItem(NCURSES_VERSION_MAJOR)
4603 SetIntItem(NCURSES_VERSION_MINOR)
4604 SetIntItem(NCURSES_VERSION_PATCH)
4605#undef SetIntItem
4606
4607 return ncurses_version;
4608}
4609
4610#endif /* NCURSES_VERSION */
4611
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -05004612/*[clinic input]
4613_curses.has_extended_color_support
4614
4615Return True if the module supports extended colors; otherwise, return False.
4616
4617Extended color support allows more than 256 color-pairs for terminals
4618that support more than 16 colors (e.g. xterm-256color).
4619[clinic start generated code]*/
4620
4621static PyObject *
4622_curses_has_extended_color_support_impl(PyObject *module)
4623/*[clinic end generated code: output=68f1be2b57d92e22 input=4b905f046e35ee9f]*/
4624{
4625 return PyBool_FromLong(_NCURSES_EXTENDED_COLOR_FUNCS);
4626}
Serhiy Storchakab232df92018-10-30 13:22:42 +02004627
Guido van Rossumf6971e21994-08-30 12:25:20 +00004628/* List of functions defined in the module */
4629
4630static PyMethodDef PyCurses_methods[] = {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004631 _CURSES_BAUDRATE_METHODDEF
4632 _CURSES_BEEP_METHODDEF
4633 _CURSES_CAN_CHANGE_COLOR_METHODDEF
4634 _CURSES_CBREAK_METHODDEF
4635 _CURSES_COLOR_CONTENT_METHODDEF
4636 _CURSES_COLOR_PAIR_METHODDEF
4637 _CURSES_CURS_SET_METHODDEF
4638 _CURSES_DEF_PROG_MODE_METHODDEF
4639 _CURSES_DEF_SHELL_MODE_METHODDEF
4640 _CURSES_DELAY_OUTPUT_METHODDEF
4641 _CURSES_DOUPDATE_METHODDEF
4642 _CURSES_ECHO_METHODDEF
4643 _CURSES_ENDWIN_METHODDEF
4644 _CURSES_ERASECHAR_METHODDEF
4645 _CURSES_FILTER_METHODDEF
4646 _CURSES_FLASH_METHODDEF
4647 _CURSES_FLUSHINP_METHODDEF
4648 _CURSES_GETMOUSE_METHODDEF
4649 _CURSES_UNGETMOUSE_METHODDEF
4650 _CURSES_GETSYX_METHODDEF
4651 _CURSES_GETWIN_METHODDEF
4652 _CURSES_HAS_COLORS_METHODDEF
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -05004653 _CURSES_HAS_EXTENDED_COLOR_SUPPORT_METHODDEF
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004654 _CURSES_HAS_IC_METHODDEF
4655 _CURSES_HAS_IL_METHODDEF
4656 _CURSES_HAS_KEY_METHODDEF
4657 _CURSES_HALFDELAY_METHODDEF
4658 _CURSES_INIT_COLOR_METHODDEF
4659 _CURSES_INIT_PAIR_METHODDEF
4660 _CURSES_INITSCR_METHODDEF
4661 _CURSES_INTRFLUSH_METHODDEF
4662 _CURSES_ISENDWIN_METHODDEF
4663 _CURSES_IS_TERM_RESIZED_METHODDEF
4664 _CURSES_KEYNAME_METHODDEF
4665 _CURSES_KILLCHAR_METHODDEF
4666 _CURSES_LONGNAME_METHODDEF
4667 _CURSES_META_METHODDEF
4668 _CURSES_MOUSEINTERVAL_METHODDEF
4669 _CURSES_MOUSEMASK_METHODDEF
4670 _CURSES_NAPMS_METHODDEF
4671 _CURSES_NEWPAD_METHODDEF
4672 _CURSES_NEWWIN_METHODDEF
4673 _CURSES_NL_METHODDEF
4674 _CURSES_NOCBREAK_METHODDEF
4675 _CURSES_NOECHO_METHODDEF
4676 _CURSES_NONL_METHODDEF
4677 _CURSES_NOQIFLUSH_METHODDEF
4678 _CURSES_NORAW_METHODDEF
4679 _CURSES_PAIR_CONTENT_METHODDEF
4680 _CURSES_PAIR_NUMBER_METHODDEF
4681 _CURSES_PUTP_METHODDEF
4682 _CURSES_QIFLUSH_METHODDEF
4683 _CURSES_RAW_METHODDEF
4684 _CURSES_RESET_PROG_MODE_METHODDEF
4685 _CURSES_RESET_SHELL_MODE_METHODDEF
4686 _CURSES_RESETTY_METHODDEF
4687 _CURSES_RESIZETERM_METHODDEF
4688 _CURSES_RESIZE_TERM_METHODDEF
4689 _CURSES_SAVETTY_METHODDEF
Batuhan Taşkaya4991cf42020-03-03 05:00:10 +03004690#if defined(NCURSES_EXT_FUNCS) && NCURSES_EXT_FUNCS >= 20081102
Anthony Sottileb32cb972019-10-31 02:13:48 -07004691 _CURSES_GET_ESCDELAY_METHODDEF
4692 _CURSES_SET_ESCDELAY_METHODDEF
Batuhan Taşkaya4991cf42020-03-03 05:00:10 +03004693#endif
Anthony Sottileb32cb972019-10-31 02:13:48 -07004694 _CURSES_GET_TABSIZE_METHODDEF
4695 _CURSES_SET_TABSIZE_METHODDEF
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004696 _CURSES_SETSYX_METHODDEF
4697 _CURSES_SETUPTERM_METHODDEF
4698 _CURSES_START_COLOR_METHODDEF
4699 _CURSES_TERMATTRS_METHODDEF
4700 _CURSES_TERMNAME_METHODDEF
4701 _CURSES_TIGETFLAG_METHODDEF
4702 _CURSES_TIGETNUM_METHODDEF
4703 _CURSES_TIGETSTR_METHODDEF
4704 _CURSES_TPARM_METHODDEF
4705 _CURSES_TYPEAHEAD_METHODDEF
4706 _CURSES_UNCTRL_METHODDEF
4707 _CURSES_UNGETCH_METHODDEF
4708 _CURSES_UPDATE_LINES_COLS_METHODDEF
4709 _CURSES_UNGET_WCH_METHODDEF
4710 _CURSES_USE_ENV_METHODDEF
4711 _CURSES_USE_DEFAULT_COLORS_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00004712 {NULL, NULL} /* sentinel */
Guido van Rossumf6971e21994-08-30 12:25:20 +00004713};
4714
4715/* Initialization function for the module */
4716
Martin v. Löwis1a214512008-06-11 05:26:20 +00004717
4718static struct PyModuleDef _cursesmodule = {
Victor Stinner26486ea2010-05-15 22:23:53 +00004719 PyModuleDef_HEAD_INIT,
4720 "_curses",
4721 NULL,
4722 -1,
4723 PyCurses_methods,
4724 NULL,
4725 NULL,
4726 NULL,
4727 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00004728};
4729
Hai Shi2f12a1b2021-01-22 18:06:43 +08004730static void
4731curses_destructor(PyObject *op)
4732{
4733 void *ptr = PyCapsule_GetPointer(op, PyCurses_CAPSULE_NAME);
4734 Py_DECREF(*(void **)ptr);
4735 PyMem_Free(ptr);
4736}
4737
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004738PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00004739PyInit__curses(void)
Guido van Rossumf6971e21994-08-30 12:25:20 +00004740{
Victor Stinner26486ea2010-05-15 22:23:53 +00004741 PyObject *m, *d, *v, *c_api_object;
Andrew M. Kuchling32552682000-12-22 21:52:27 +00004742
Victor Stinner26486ea2010-05-15 22:23:53 +00004743 /* Initialize object type */
4744 if (PyType_Ready(&PyCursesWindow_Type) < 0)
4745 return NULL;
Guido van Rossum6915c4d2001-01-19 00:28:08 +00004746
Victor Stinner26486ea2010-05-15 22:23:53 +00004747 /* Create the module and add the functions */
4748 m = PyModule_Create(&_cursesmodule);
4749 if (m == NULL)
4750 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +00004751
Victor Stinner26486ea2010-05-15 22:23:53 +00004752 /* Add some symbolic constants to the module */
4753 d = PyModule_GetDict(m);
4754 if (d == NULL)
4755 return NULL;
4756 ModDict = d; /* For PyCurses_InitScr to use later */
Andrew M. Kuchling32552682000-12-22 21:52:27 +00004757
Hai Shi2f12a1b2021-01-22 18:06:43 +08004758 void **PyCurses_API = PyMem_Calloc(PyCurses_API_pointers, sizeof(void *));
4759 if (PyCurses_API == NULL) {
4760 PyErr_NoMemory();
4761 return NULL;
4762 }
4763 /* Initialize the C API pointer array */
4764 PyCurses_API[0] = (void *)Py_NewRef(&PyCursesWindow_Type);
4765 PyCurses_API[1] = (void *)func_PyCursesSetupTermCalled;
4766 PyCurses_API[2] = (void *)func_PyCursesInitialised;
4767 PyCurses_API[3] = (void *)func_PyCursesInitialisedColor;
4768
Victor Stinner26486ea2010-05-15 22:23:53 +00004769 /* Add a capsule for the C API */
Hai Shi2f12a1b2021-01-22 18:06:43 +08004770 c_api_object = PyCapsule_New(PyCurses_API, PyCurses_CAPSULE_NAME,
4771 curses_destructor);
4772 if (c_api_object == NULL) {
4773 Py_DECREF(PyCurses_API[0]);
4774 PyMem_Free(PyCurses_API);
4775 return NULL;
4776 }
4777 if (PyDict_SetItemString(d, "_C_API", c_api_object) < 0) {
4778 Py_DECREF(c_api_object);
4779 return NULL;
4780 }
Victor Stinner26486ea2010-05-15 22:23:53 +00004781 Py_DECREF(c_api_object);
Guido van Rossumfbea2f31994-08-31 22:05:27 +00004782
Victor Stinner26486ea2010-05-15 22:23:53 +00004783 /* For exception curses.error */
4784 PyCursesError = PyErr_NewException("_curses.error", NULL, NULL);
4785 PyDict_SetItemString(d, "error", PyCursesError);
Guido van Rossumfbea2f31994-08-31 22:05:27 +00004786
Victor Stinner26486ea2010-05-15 22:23:53 +00004787 /* Make the version available */
4788 v = PyBytes_FromString(PyCursesVersion);
4789 PyDict_SetItemString(d, "version", v);
4790 PyDict_SetItemString(d, "__version__", v);
4791 Py_DECREF(v);
Guido van Rossumf6971e21994-08-30 12:25:20 +00004792
Serhiy Storchakab232df92018-10-30 13:22:42 +02004793#ifdef NCURSES_VERSION
4794 /* ncurses_version */
4795 if (NcursesVersionType.tp_name == NULL) {
4796 if (PyStructSequence_InitType2(&NcursesVersionType,
4797 &ncurses_version_desc) < 0)
4798 return NULL;
4799 }
4800 v = make_ncurses_version();
4801 if (v == NULL) {
4802 return NULL;
4803 }
4804 PyDict_SetItemString(d, "ncurses_version", v);
4805 Py_DECREF(v);
4806
4807 /* prevent user from creating new instances */
4808 NcursesVersionType.tp_init = NULL;
4809 NcursesVersionType.tp_new = NULL;
4810 if (PyDict_DelItemString(NcursesVersionType.tp_dict, "__new__") < 0 &&
4811 PyErr_ExceptionMatches(PyExc_KeyError))
4812 {
4813 PyErr_Clear();
4814 }
4815#endif /* NCURSES_VERSION */
4816
Victor Stinner26486ea2010-05-15 22:23:53 +00004817 SetDictInt("ERR", ERR);
4818 SetDictInt("OK", OK);
Andrew M. Kuchlingcaefb372000-12-26 15:57:01 +00004819
Victor Stinner26486ea2010-05-15 22:23:53 +00004820 /* Here are some attributes you can add to chars to print */
4821
4822 SetDictInt("A_ATTRIBUTES", A_ATTRIBUTES);
4823 SetDictInt("A_NORMAL", A_NORMAL);
4824 SetDictInt("A_STANDOUT", A_STANDOUT);
4825 SetDictInt("A_UNDERLINE", A_UNDERLINE);
4826 SetDictInt("A_REVERSE", A_REVERSE);
4827 SetDictInt("A_BLINK", A_BLINK);
4828 SetDictInt("A_DIM", A_DIM);
4829 SetDictInt("A_BOLD", A_BOLD);
4830 SetDictInt("A_ALTCHARSET", A_ALTCHARSET);
Victor Stinner26486ea2010-05-15 22:23:53 +00004831 SetDictInt("A_INVIS", A_INVIS);
Victor Stinner26486ea2010-05-15 22:23:53 +00004832 SetDictInt("A_PROTECT", A_PROTECT);
4833 SetDictInt("A_CHARTEXT", A_CHARTEXT);
4834 SetDictInt("A_COLOR", A_COLOR);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004835
Victor Stinner26486ea2010-05-15 22:23:53 +00004836 /* The following are never available with strict SYSV curses */
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004837#ifdef A_HORIZONTAL
Victor Stinner26486ea2010-05-15 22:23:53 +00004838 SetDictInt("A_HORIZONTAL", A_HORIZONTAL);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004839#endif
4840#ifdef A_LEFT
Victor Stinner26486ea2010-05-15 22:23:53 +00004841 SetDictInt("A_LEFT", A_LEFT);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004842#endif
4843#ifdef A_LOW
Victor Stinner26486ea2010-05-15 22:23:53 +00004844 SetDictInt("A_LOW", A_LOW);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004845#endif
4846#ifdef A_RIGHT
Victor Stinner26486ea2010-05-15 22:23:53 +00004847 SetDictInt("A_RIGHT", A_RIGHT);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004848#endif
4849#ifdef A_TOP
Victor Stinner26486ea2010-05-15 22:23:53 +00004850 SetDictInt("A_TOP", A_TOP);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004851#endif
4852#ifdef A_VERTICAL
Victor Stinner26486ea2010-05-15 22:23:53 +00004853 SetDictInt("A_VERTICAL", A_VERTICAL);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004854#endif
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00004855
Xiang Zhang116dd5e2017-06-16 11:20:07 +08004856 /* ncurses extension */
4857#ifdef A_ITALIC
4858 SetDictInt("A_ITALIC", A_ITALIC);
4859#endif
4860
Victor Stinner26486ea2010-05-15 22:23:53 +00004861 SetDictInt("COLOR_BLACK", COLOR_BLACK);
4862 SetDictInt("COLOR_RED", COLOR_RED);
4863 SetDictInt("COLOR_GREEN", COLOR_GREEN);
4864 SetDictInt("COLOR_YELLOW", COLOR_YELLOW);
4865 SetDictInt("COLOR_BLUE", COLOR_BLUE);
4866 SetDictInt("COLOR_MAGENTA", COLOR_MAGENTA);
4867 SetDictInt("COLOR_CYAN", COLOR_CYAN);
4868 SetDictInt("COLOR_WHITE", COLOR_WHITE);
Guido van Rossumf6971e21994-08-30 12:25:20 +00004869
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004870#ifdef NCURSES_MOUSE_VERSION
Victor Stinner26486ea2010-05-15 22:23:53 +00004871 /* Mouse-related constants */
4872 SetDictInt("BUTTON1_PRESSED", BUTTON1_PRESSED);
4873 SetDictInt("BUTTON1_RELEASED", BUTTON1_RELEASED);
4874 SetDictInt("BUTTON1_CLICKED", BUTTON1_CLICKED);
4875 SetDictInt("BUTTON1_DOUBLE_CLICKED", BUTTON1_DOUBLE_CLICKED);
4876 SetDictInt("BUTTON1_TRIPLE_CLICKED", BUTTON1_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004877
Victor Stinner26486ea2010-05-15 22:23:53 +00004878 SetDictInt("BUTTON2_PRESSED", BUTTON2_PRESSED);
4879 SetDictInt("BUTTON2_RELEASED", BUTTON2_RELEASED);
4880 SetDictInt("BUTTON2_CLICKED", BUTTON2_CLICKED);
4881 SetDictInt("BUTTON2_DOUBLE_CLICKED", BUTTON2_DOUBLE_CLICKED);
4882 SetDictInt("BUTTON2_TRIPLE_CLICKED", BUTTON2_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004883
Victor Stinner26486ea2010-05-15 22:23:53 +00004884 SetDictInt("BUTTON3_PRESSED", BUTTON3_PRESSED);
4885 SetDictInt("BUTTON3_RELEASED", BUTTON3_RELEASED);
4886 SetDictInt("BUTTON3_CLICKED", BUTTON3_CLICKED);
4887 SetDictInt("BUTTON3_DOUBLE_CLICKED", BUTTON3_DOUBLE_CLICKED);
4888 SetDictInt("BUTTON3_TRIPLE_CLICKED", BUTTON3_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004889
Victor Stinner26486ea2010-05-15 22:23:53 +00004890 SetDictInt("BUTTON4_PRESSED", BUTTON4_PRESSED);
4891 SetDictInt("BUTTON4_RELEASED", BUTTON4_RELEASED);
4892 SetDictInt("BUTTON4_CLICKED", BUTTON4_CLICKED);
4893 SetDictInt("BUTTON4_DOUBLE_CLICKED", BUTTON4_DOUBLE_CLICKED);
4894 SetDictInt("BUTTON4_TRIPLE_CLICKED", BUTTON4_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004895
Zackery Spytz14cfa322021-01-14 02:40:09 -07004896#if NCURSES_MOUSE_VERSION > 1
4897 SetDictInt("BUTTON5_PRESSED", BUTTON5_PRESSED);
4898 SetDictInt("BUTTON5_RELEASED", BUTTON5_RELEASED);
4899 SetDictInt("BUTTON5_CLICKED", BUTTON5_CLICKED);
4900 SetDictInt("BUTTON5_DOUBLE_CLICKED", BUTTON5_DOUBLE_CLICKED);
4901 SetDictInt("BUTTON5_TRIPLE_CLICKED", BUTTON5_TRIPLE_CLICKED);
4902#endif
4903
Victor Stinner26486ea2010-05-15 22:23:53 +00004904 SetDictInt("BUTTON_SHIFT", BUTTON_SHIFT);
4905 SetDictInt("BUTTON_CTRL", BUTTON_CTRL);
4906 SetDictInt("BUTTON_ALT", BUTTON_ALT);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004907
Victor Stinner26486ea2010-05-15 22:23:53 +00004908 SetDictInt("ALL_MOUSE_EVENTS", ALL_MOUSE_EVENTS);
4909 SetDictInt("REPORT_MOUSE_POSITION", REPORT_MOUSE_POSITION);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004910#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00004911 /* Now set everything up for KEY_ variables */
4912 {
4913 int key;
4914 char *key_n;
4915 char *key_n2;
Victor Stinner26486ea2010-05-15 22:23:53 +00004916 for (key=KEY_MIN;key < KEY_MAX; key++) {
4917 key_n = (char *)keyname(key);
4918 if (key_n == NULL || strcmp(key_n,"UNKNOWN KEY")==0)
4919 continue;
4920 if (strncmp(key_n,"KEY_F(",6)==0) {
4921 char *p1, *p2;
Victor Stinnerb6404912013-07-07 16:21:41 +02004922 key_n2 = PyMem_Malloc(strlen(key_n)+1);
Victor Stinner26486ea2010-05-15 22:23:53 +00004923 if (!key_n2) {
4924 PyErr_NoMemory();
4925 break;
4926 }
4927 p1 = key_n;
4928 p2 = key_n2;
4929 while (*p1) {
4930 if (*p1 != '(' && *p1 != ')') {
4931 *p2 = *p1;
4932 p2++;
4933 }
4934 p1++;
4935 }
4936 *p2 = (char)0;
4937 } else
4938 key_n2 = key_n;
4939 SetDictInt(key_n2,key);
4940 if (key_n2 != key_n)
Victor Stinnerb6404912013-07-07 16:21:41 +02004941 PyMem_Free(key_n2);
Victor Stinner26486ea2010-05-15 22:23:53 +00004942 }
Victor Stinner26486ea2010-05-15 22:23:53 +00004943 SetDictInt("KEY_MIN", KEY_MIN);
4944 SetDictInt("KEY_MAX", KEY_MAX);
4945 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004946
Dong-hee Na37fcbb62020-03-25 07:08:51 +09004947 if (PyModule_AddType(m, &PyCursesWindow_Type) < 0) {
4948 return NULL;
4949 }
Victor Stinner26486ea2010-05-15 22:23:53 +00004950 return m;
Guido van Rossumf6971e21994-08-30 12:25:20 +00004951}