blob: c70b0e2a19fadc440797a950dd8268fc3446901a [file] [log] [blame]
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001/*
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00002 * This is a curses module for Python.
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003 *
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00004 * Based on prior work by Lance Ellinghaus and Oliver Andrich
5 * Version 1.2 of this module: Copyright 1994 by Lance Ellinghouse,
6 * Cathedral City, California Republic, United States of America.
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00007 *
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00008 * Version 1.5b1, heavily extended for ncurses by Oliver Andrich:
9 * Copyright 1996,1997 by Oliver Andrich, Koblenz, Germany.
10 *
Andrew M. Kuchling3adefcc2002-10-30 21:08:34 +000011 * Tidied for Python 1.6, and currently maintained by <amk@amk.ca>.
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +000012 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this source file to use, copy, modify, merge, or publish it
15 * subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or in any new file that contains a substantial portion of
19 * this file.
20 *
21 * THE AUTHOR MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF
22 * THE SOFTWARE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT
23 * EXPRESS OR IMPLIED WARRANTY. THE AUTHOR DISCLAIMS ALL WARRANTIES
24 * WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
25 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NON-INFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE
27 * AUTHOR BE LIABLE TO YOU OR ANY OTHER PARTY FOR ANY SPECIAL,
28 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
29 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE, STRICT LIABILITY OR
30 * ANY OTHER ACTION ARISING OUT OF OR IN CONNECTION WITH THE USE OR
31 * PERFORMANCE OF THIS SOFTWARE.
32 */
Guido van Rossumf6971e21994-08-30 12:25:20 +000033
Andrew M. Kuchlingfb6bbf52000-07-27 11:58:01 +000034/*
35
Victor Stinner26486ea2010-05-15 22:23:53 +000036 A number of SysV or ncurses functions don't have wrappers yet; if you
37 need a given function, add it and send a patch. See
38 http://www.python.org/dev/patches/ for instructions on how to submit
39 patches to Python.
Andrew M. Kuchlingfb6bbf52000-07-27 11:58:01 +000040
Victor Stinner26486ea2010-05-15 22:23:53 +000041 Here's a list of currently unsupported functions:
Guido van Rossumd8faa362007-04-27 19:54:29 +000042
Victor Stinner26486ea2010-05-15 22:23:53 +000043 addchnstr addchstr color_set define_key
44 del_curterm delscreen dupwin inchnstr inchstr innstr keyok
45 mcprint mvaddchnstr mvaddchstr mvcur mvinchnstr
46 mvinchstr mvinnstr mmvwaddchnstr mvwaddchstr
47 mvwinchnstr mvwinchstr mvwinnstr newterm
48 restartterm ripoffline scr_dump
49 scr_init scr_restore scr_set scrl set_curterm set_term setterm
50 tgetent tgetflag tgetnum tgetstr tgoto timeout tputs
51 vidattr vidputs waddchnstr waddchstr
52 wcolor_set winchnstr winchstr winnstr wmouse_trafo wscrl
Andrew M. Kuchlingfb6bbf52000-07-27 11:58:01 +000053
Victor Stinner26486ea2010-05-15 22:23:53 +000054 Low-priority:
55 slk_attr slk_attr_off slk_attr_on slk_attr_set slk_attroff
56 slk_attron slk_attrset slk_clear slk_color slk_init slk_label
57 slk_noutrefresh slk_refresh slk_restore slk_set slk_touch
Andrew M. Kuchlingfb6bbf52000-07-27 11:58:01 +000058
Victor Stinner26486ea2010-05-15 22:23:53 +000059 Menu extension (ncurses and probably SYSV):
60 current_item free_item free_menu item_count item_description
61 item_index item_init item_name item_opts item_opts_off
62 item_opts_on item_term item_userptr item_value item_visible
63 menu_back menu_driver menu_fore menu_format menu_grey
64 menu_init menu_items menu_mark menu_opts menu_opts_off
65 menu_opts_on menu_pad menu_pattern menu_request_by_name
66 menu_request_name menu_spacing menu_sub menu_term menu_userptr
67 menu_win new_item new_menu pos_menu_cursor post_menu
68 scale_menu set_current_item set_item_init set_item_opts
69 set_item_term set_item_userptr set_item_value set_menu_back
70 set_menu_fore set_menu_format set_menu_grey set_menu_init
71 set_menu_items set_menu_mark set_menu_opts set_menu_pad
72 set_menu_pattern set_menu_spacing set_menu_sub set_menu_term
73 set_menu_userptr set_menu_win set_top_row top_row unpost_menu
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +000074
Victor Stinner26486ea2010-05-15 22:23:53 +000075 Form extension (ncurses and probably SYSV):
76 current_field data_ahead data_behind dup_field
77 dynamic_fieldinfo field_arg field_back field_buffer
78 field_count field_fore field_index field_info field_init
79 field_just field_opts field_opts_off field_opts_on field_pad
80 field_status field_term field_type field_userptr form_driver
81 form_fields form_init form_opts form_opts_off form_opts_on
82 form_page form_request_by_name form_request_name form_sub
83 form_term form_userptr form_win free_field free_form
84 link_field link_fieldtype move_field new_field new_form
85 new_page pos_form_cursor post_form scale_form
86 set_current_field set_field_back set_field_buffer
87 set_field_fore set_field_init set_field_just set_field_opts
88 set_field_pad set_field_status set_field_term set_field_type
89 set_field_userptr set_fieldtype_arg set_fieldtype_choice
90 set_form_fields set_form_init set_form_opts set_form_page
91 set_form_sub set_form_term set_form_userptr set_form_win
92 set_max_field set_new_page unpost_form
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +000093
94
Victor Stinner26486ea2010-05-15 22:23:53 +000095*/
Andrew M. Kuchlingfb6bbf52000-07-27 11:58:01 +000096
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +000097/* Release Number */
Guido van Rossumf6971e21994-08-30 12:25:20 +000098
Serhiy Storchakae2f92de2017-11-11 13:06:26 +020099static const char PyCursesVersion[] = "2.2";
Guido van Rossumf6971e21994-08-30 12:25:20 +0000100
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000101/* Includes */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000102
Mark Dickinsone047cfa2010-05-11 17:57:09 +0000103#define PY_SSIZE_T_CLEAN
104
Guido van Rossum602099a1994-09-14 13:32:22 +0000105#include "Python.h"
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000106
Benjamin Petersonb173f782009-05-05 22:31:58 +0000107
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000108#ifdef __hpux
Martin v. Löwiseb9b1032001-10-24 17:10:49 +0000109#define STRICT_SYSV_CURSES
110#endif
111
Guido van Rossum858ca0f2001-04-10 19:53:37 +0000112#define CURSES_MODULE
113#include "py_curses.h"
114
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200115#if defined(HAVE_TERM_H) || defined(__sgi)
116/* For termname, longname, putp, tigetflag, tigetnum, tigetstr, tparm
117 which are not declared in SysV curses and for setupterm. */
Fred Drake0368bc42001-07-19 20:48:32 +0000118#include <term.h>
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200119/* Including <term.h> #defines many common symbols. */
120#undef lines
121#undef columns
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +0000122#endif
123
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100124#ifdef HAVE_LANGINFO_H
125#include <langinfo.h>
126#endif
127
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000128#if !defined(HAVE_NCURSES_H) && (defined(sgi) || defined(__sun) || defined(SCO5))
Andrew M. Kuchlingbf680262000-07-25 12:37:41 +0000129#define STRICT_SYSV_CURSES /* Don't use ncurses extensions */
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +0000130typedef chtype attr_t; /* No attr_t type is available */
Guido van Rossum1266a011996-02-25 04:50:31 +0000131#endif
Guido van Rossumf6971e21994-08-30 12:25:20 +0000132
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +0000133#if defined(_AIX)
134#define STRICT_SYSV_CURSES
135#endif
136
Larry Hastings61272b72014-01-07 12:41:53 -0800137/*[clinic input]
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300138module _curses
139class _curses.window "PyCursesWindowObject *" "&PyCursesWindow_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800140[clinic start generated code]*/
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300141/*[clinic end generated code: output=da39a3ee5e6b4b0d input=43265c372c2887d6]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300142
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000143/* Definition of exception curses.error */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000144
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000145static PyObject *PyCursesError;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000146
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +0000147/* Tells whether setupterm() has been called to initialise terminfo. */
148static int initialised_setupterm = FALSE;
149
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000150/* Tells whether initscr() has been called to initialise curses. */
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000151static int initialised = FALSE;
152
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000153/* Tells whether start_color() has been called to initialise color usage. */
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000154static int initialisedcolors = FALSE;
155
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100156static char *screen_encoding = NULL;
157
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000158/* Utility Macros */
Victor Stinner26486ea2010-05-15 22:23:53 +0000159#define PyCursesSetupTermCalled \
160 if (initialised_setupterm != TRUE) { \
161 PyErr_SetString(PyCursesError, \
162 "must call (at least) setupterm() first"); \
163 return 0; }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +0000164
Victor Stinner26486ea2010-05-15 22:23:53 +0000165#define PyCursesInitialised \
166 if (initialised != TRUE) { \
167 PyErr_SetString(PyCursesError, \
168 "must call initscr() first"); \
169 return 0; }
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000170
Victor Stinner26486ea2010-05-15 22:23:53 +0000171#define PyCursesInitialisedColor \
172 if (initialisedcolors != TRUE) { \
173 PyErr_SetString(PyCursesError, \
174 "must call start_color() first"); \
175 return 0; }
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000176
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000177/* Utility Functions */
Guido van Rossumfbea2f31994-08-31 22:05:27 +0000178
Victor Stinner077af8c2019-08-14 12:31:43 +0200179static inline int
180color_pair_to_attr(short color_number)
181{
182 return ((int)color_number << 8);
183}
184
185static inline short
186attr_to_color_pair(int attr)
187{
188 return (short)((attr & A_COLOR) >> 8);
189}
190
Guido van Rossum85738471995-02-17 13:50:17 +0000191/*
Victor Stinner26486ea2010-05-15 22:23:53 +0000192 * Check the return code from a curses function and return None
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000193 * or raise an exception as appropriate. These are exported using the
Victor Stinner26486ea2010-05-15 22:23:53 +0000194 * capsule API.
Guido van Rossum85738471995-02-17 13:50:17 +0000195 */
196
197static PyObject *
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100198PyCursesCheckERR(int code, const char *fname)
Guido van Rossum85738471995-02-17 13:50:17 +0000199{
Victor Stinner26486ea2010-05-15 22:23:53 +0000200 if (code != ERR) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200201 Py_RETURN_NONE;
Guido van Rossum85738471995-02-17 13:50:17 +0000202 } else {
Victor Stinner26486ea2010-05-15 22:23:53 +0000203 if (fname == NULL) {
204 PyErr_SetString(PyCursesError, catchall_ERR);
205 } else {
206 PyErr_Format(PyCursesError, "%s() returned ERR", fname);
207 }
208 return NULL;
Guido van Rossum85738471995-02-17 13:50:17 +0000209 }
Guido van Rossum85738471995-02-17 13:50:17 +0000210}
211
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100212/* Convert an object to a byte (an integer of type chtype):
213
214 - int
215 - bytes of length 1
216 - str of length 1
217
218 Return 1 on success, 0 on error (invalid type or integer overflow). */
Victor Stinner26486ea2010-05-15 22:23:53 +0000219static int
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100220PyCurses_ConvertToChtype(PyCursesWindowObject *win, PyObject *obj, chtype *ch)
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000221{
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100222 long value;
223 if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) {
224 value = (unsigned char)PyBytes_AsString(obj)[0];
225 }
226 else if (PyUnicode_Check(obj)) {
227 if (PyUnicode_GetLength(obj) != 1) {
228 PyErr_Format(PyExc_TypeError,
229 "expect bytes or str of length 1, or int, "
230 "got a str of length %zi",
231 PyUnicode_GET_LENGTH(obj));
Victor Stinner26486ea2010-05-15 22:23:53 +0000232 return 0;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100233 }
234 value = PyUnicode_READ_CHAR(obj, 0);
235 if (128 < value) {
236 PyObject *bytes;
237 const char *encoding;
238 if (win)
239 encoding = win->encoding;
240 else
241 encoding = screen_encoding;
Serhiy Storchakab29cee42016-10-27 19:31:49 +0300242 bytes = PyUnicode_AsEncodedString(obj, encoding, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100243 if (bytes == NULL)
244 return 0;
245 if (PyBytes_GET_SIZE(bytes) == 1)
246 value = (unsigned char)PyBytes_AS_STRING(bytes)[0];
247 else
248 value = -1;
249 Py_DECREF(bytes);
250 if (value < 0)
251 goto overflow;
252 }
253 }
254 else if (PyLong_CheckExact(obj)) {
255 int long_overflow;
256 value = PyLong_AsLongAndOverflow(obj, &long_overflow);
257 if (long_overflow)
258 goto overflow;
259 }
260 else {
261 PyErr_Format(PyExc_TypeError,
262 "expect bytes or str of length 1, or int, got %s",
263 Py_TYPE(obj)->tp_name);
Victor Stinner26486ea2010-05-15 22:23:53 +0000264 return 0;
265 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100266 *ch = (chtype)value;
267 if ((long)*ch != value)
268 goto overflow;
Victor Stinner26486ea2010-05-15 22:23:53 +0000269 return 1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100270
271overflow:
272 PyErr_SetString(PyExc_OverflowError,
273 "byte doesn't fit in chtype");
274 return 0;
275}
276
277/* Convert an object to a byte (chtype) or a character (cchar_t):
278
279 - int
280 - bytes of length 1
281 - str of length 1
282
283 Return:
284
285 - 2 if obj is a character (written into *wch)
286 - 1 if obj is a byte (written into *ch)
287 - 0 on error: raise an exception */
288static int
289PyCurses_ConvertToCchar_t(PyCursesWindowObject *win, PyObject *obj,
290 chtype *ch
291#ifdef HAVE_NCURSESW
Serhiy Storchaka0bcd89b2016-10-30 22:52:06 +0200292 , wchar_t *wch
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100293#endif
294 )
295{
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100296 long value;
297#ifdef HAVE_NCURSESW
298 wchar_t buffer[2];
299#endif
300
301 if (PyUnicode_Check(obj)) {
302#ifdef HAVE_NCURSESW
303 if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) {
304 PyErr_Format(PyExc_TypeError,
305 "expect bytes or str of length 1, or int, "
306 "got a str of length %zi",
307 PyUnicode_GET_LENGTH(obj));
308 return 0;
309 }
Serhiy Storchaka0bcd89b2016-10-30 22:52:06 +0200310 *wch = buffer[0];
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100311 return 2;
312#else
313 return PyCurses_ConvertToChtype(win, obj, ch);
314#endif
315 }
316 else if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) {
317 value = (unsigned char)PyBytes_AsString(obj)[0];
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100318 }
319 else if (PyLong_CheckExact(obj)) {
320 int overflow;
321 value = PyLong_AsLongAndOverflow(obj, &overflow);
322 if (overflow) {
323 PyErr_SetString(PyExc_OverflowError,
324 "int doesn't fit in long");
325 return 0;
326 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100327 }
328 else {
329 PyErr_Format(PyExc_TypeError,
330 "expect bytes or str of length 1, or int, got %s",
331 Py_TYPE(obj)->tp_name);
332 return 0;
333 }
Victor Stinner9f16e442012-09-01 15:00:34 +0200334
335 *ch = (chtype)value;
336 if ((long)*ch != value) {
337 PyErr_Format(PyExc_OverflowError,
338 "byte doesn't fit in chtype");
339 return 0;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100340 }
Victor Stinner9f16e442012-09-01 15:00:34 +0200341 return 1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100342}
343
344/* Convert an object to a byte string (char*) or a wide character string
345 (wchar_t*). Return:
346
347 - 2 if obj is a character string (written into *wch)
348 - 1 if obj is a byte string (written into *bytes)
349 - 0 on error: raise an exception */
350static int
351PyCurses_ConvertToString(PyCursesWindowObject *win, PyObject *obj,
352 PyObject **bytes, wchar_t **wstr)
353{
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300354 char *str;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100355 if (PyUnicode_Check(obj)) {
356#ifdef HAVE_NCURSESW
357 assert (wstr != NULL);
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300358
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100359 *wstr = PyUnicode_AsWideCharString(obj, NULL);
360 if (*wstr == NULL)
361 return 0;
362 return 2;
363#else
364 assert (wstr == NULL);
Serhiy Storchakab29cee42016-10-27 19:31:49 +0300365 *bytes = PyUnicode_AsEncodedString(obj, win->encoding, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100366 if (*bytes == NULL)
367 return 0;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300368 /* check for embedded null bytes */
369 if (PyBytes_AsStringAndSize(*bytes, &str, NULL) < 0) {
370 return 0;
371 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100372 return 1;
373#endif
374 }
375 else if (PyBytes_Check(obj)) {
376 Py_INCREF(obj);
377 *bytes = obj;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300378 /* check for embedded null bytes */
379 if (PyBytes_AsStringAndSize(*bytes, &str, NULL) < 0) {
380 return 0;
381 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100382 return 1;
383 }
384
385 PyErr_Format(PyExc_TypeError, "expect bytes or str, got %s",
386 Py_TYPE(obj)->tp_name);
387 return 0;
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000388}
389
Benjamin Peterson21896a32010-03-21 22:03:03 +0000390/* Function versions of the 3 functions for testing whether curses has been
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000391 initialised or not. */
Victor Stinner26486ea2010-05-15 22:23:53 +0000392
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000393static int func_PyCursesSetupTermCalled(void)
394{
395 PyCursesSetupTermCalled;
396 return 1;
397}
398
399static int func_PyCursesInitialised(void)
400{
401 PyCursesInitialised;
402 return 1;
403}
404
405static int func_PyCursesInitialisedColor(void)
406{
407 PyCursesInitialisedColor;
408 return 1;
409}
410
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000411/*****************************************************************************
412 The Window Object
413******************************************************************************/
Guido van Rossum585c6dc1995-02-24 13:45:43 +0000414
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000415/* Definition of the window type */
Guido van Rossum85738471995-02-17 13:50:17 +0000416
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000417PyTypeObject PyCursesWindow_Type;
418
Andrew M. Kuchling32552682000-12-22 21:52:27 +0000419/* Function prototype macros for Window object
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000420
421 X - function name
422 TYPE - parameter Type
423 ERGSTR - format string for construction of the return value
424 PARSESTR - format string for argument parsing
Victor Stinner26486ea2010-05-15 22:23:53 +0000425*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000426
Serhiy Storchaka81524022018-11-27 13:05:02 +0200427#define Window_NoArgNoReturnFunction(X) \
428 static PyObject *PyCursesWindow_ ## X \
429 (PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \
Victor Stinner26486ea2010-05-15 22:23:53 +0000430 { return PyCursesCheckERR(X(self->win), # X); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000431
Victor Stinner26486ea2010-05-15 22:23:53 +0000432#define Window_NoArgTrueFalseFunction(X) \
433 static PyObject * PyCursesWindow_ ## X \
Serhiy Storchaka81524022018-11-27 13:05:02 +0200434 (PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \
Victor Stinner26486ea2010-05-15 22:23:53 +0000435 { \
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300436 return PyBool_FromLong(X(self->win)); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000437
Victor Stinner26486ea2010-05-15 22:23:53 +0000438#define Window_NoArgNoReturnVoidFunction(X) \
439 static PyObject * PyCursesWindow_ ## X \
Serhiy Storchaka81524022018-11-27 13:05:02 +0200440 (PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \
Victor Stinner26486ea2010-05-15 22:23:53 +0000441 { \
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200442 X(self->win); Py_RETURN_NONE; }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000443
Victor Stinner26486ea2010-05-15 22:23:53 +0000444#define Window_NoArg2TupleReturnFunction(X, TYPE, ERGSTR) \
445 static PyObject * PyCursesWindow_ ## X \
Serhiy Storchaka81524022018-11-27 13:05:02 +0200446 (PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \
Victor Stinner26486ea2010-05-15 22:23:53 +0000447 { \
448 TYPE arg1, arg2; \
449 X(self->win,arg1,arg2); return Py_BuildValue(ERGSTR, arg1, arg2); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000450
Victor Stinner26486ea2010-05-15 22:23:53 +0000451#define Window_OneArgNoReturnVoidFunction(X, TYPE, PARSESTR) \
452 static PyObject * PyCursesWindow_ ## X \
453 (PyCursesWindowObject *self, PyObject *args) \
454 { \
455 TYPE arg1; \
456 if (!PyArg_ParseTuple(args, PARSESTR, &arg1)) return NULL; \
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200457 X(self->win,arg1); Py_RETURN_NONE; }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000458
Victor Stinner26486ea2010-05-15 22:23:53 +0000459#define Window_OneArgNoReturnFunction(X, TYPE, PARSESTR) \
460 static PyObject * PyCursesWindow_ ## X \
461 (PyCursesWindowObject *self, PyObject *args) \
462 { \
463 TYPE arg1; \
464 if (!PyArg_ParseTuple(args,PARSESTR, &arg1)) return NULL; \
465 return PyCursesCheckERR(X(self->win, arg1), # X); }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000466
Victor Stinner26486ea2010-05-15 22:23:53 +0000467#define Window_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \
468 static PyObject * PyCursesWindow_ ## X \
469 (PyCursesWindowObject *self, PyObject *args) \
470 { \
471 TYPE arg1, arg2; \
472 if (!PyArg_ParseTuple(args,PARSESTR, &arg1, &arg2)) return NULL; \
473 return PyCursesCheckERR(X(self->win, arg1, arg2), # X); }
Guido van Rossum85738471995-02-17 13:50:17 +0000474
Guido van Rossumf6971e21994-08-30 12:25:20 +0000475/* ------------- WINDOW routines --------------- */
Guido van Rossum85738471995-02-17 13:50:17 +0000476
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000477Window_NoArgNoReturnFunction(untouchwin)
478Window_NoArgNoReturnFunction(touchwin)
479Window_NoArgNoReturnFunction(redrawwin)
480Window_NoArgNoReturnFunction(winsertln)
481Window_NoArgNoReturnFunction(werase)
482Window_NoArgNoReturnFunction(wdeleteln)
483
484Window_NoArgTrueFalseFunction(is_wintouched)
485
486Window_NoArgNoReturnVoidFunction(wsyncup)
487Window_NoArgNoReturnVoidFunction(wsyncdown)
488Window_NoArgNoReturnVoidFunction(wstandend)
489Window_NoArgNoReturnVoidFunction(wstandout)
490Window_NoArgNoReturnVoidFunction(wcursyncup)
491Window_NoArgNoReturnVoidFunction(wclrtoeol)
492Window_NoArgNoReturnVoidFunction(wclrtobot)
493Window_NoArgNoReturnVoidFunction(wclear)
494
495Window_OneArgNoReturnVoidFunction(idcok, int, "i;True(1) or False(0)")
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200496#ifdef HAVE_CURSES_IMMEDOK
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000497Window_OneArgNoReturnVoidFunction(immedok, int, "i;True(1) or False(0)")
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200498#endif
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +0000499Window_OneArgNoReturnVoidFunction(wtimeout, int, "i;delay")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000500
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000501Window_NoArg2TupleReturnFunction(getyx, int, "ii")
502Window_NoArg2TupleReturnFunction(getbegyx, int, "ii")
503Window_NoArg2TupleReturnFunction(getmaxyx, int, "ii")
504Window_NoArg2TupleReturnFunction(getparyx, int, "ii")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000505
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000506Window_OneArgNoReturnFunction(clearok, int, "i;True(1) or False(0)")
507Window_OneArgNoReturnFunction(idlok, int, "i;True(1) or False(0)")
508Window_OneArgNoReturnFunction(keypad, int, "i;True(1) or False(0)")
509Window_OneArgNoReturnFunction(leaveok, int, "i;True(1) or False(0)")
510Window_OneArgNoReturnFunction(nodelay, int, "i;True(1) or False(0)")
511Window_OneArgNoReturnFunction(notimeout, int, "i;True(1) or False(0)")
512Window_OneArgNoReturnFunction(scrollok, int, "i;True(1) or False(0)")
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +0000513Window_OneArgNoReturnFunction(winsdelln, int, "i;nlines")
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200514#ifdef HAVE_CURSES_SYNCOK
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000515Window_OneArgNoReturnFunction(syncok, int, "i;True(1) or False(0)")
Serhiy Storchakabaac01e2017-10-31 13:56:44 +0200516#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000517
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000518Window_TwoArgNoReturnFunction(mvwin, int, "ii;y,x")
519Window_TwoArgNoReturnFunction(mvderwin, int, "ii;y,x")
520Window_TwoArgNoReturnFunction(wmove, int, "ii;y,x")
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +0000521#ifndef STRICT_SYSV_CURSES
Martin v. Löwisc0e16712002-01-17 23:08:27 +0000522Window_TwoArgNoReturnFunction(wresize, int, "ii;lines,columns")
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000523#endif
524
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +0000525/* Allocation and deallocation of Window Objects */
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000526
Guido van Rossumf6971e21994-08-30 12:25:20 +0000527static PyObject *
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100528PyCursesWindow_New(WINDOW *win, const char *encoding)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000529{
Victor Stinner26486ea2010-05-15 22:23:53 +0000530 PyCursesWindowObject *wo;
Guido van Rossum85738471995-02-17 13:50:17 +0000531
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100532 if (encoding == NULL) {
533#if defined(MS_WINDOWS)
534 char *buffer[100];
535 UINT cp;
536 cp = GetConsoleOutputCP();
537 if (cp != 0) {
538 PyOS_snprintf(buffer, sizeof(buffer), "cp%u", cp);
539 encoding = buffer;
540 }
541#elif defined(CODESET)
542 const char *codeset = nl_langinfo(CODESET);
543 if (codeset != NULL && codeset[0] != 0)
544 encoding = codeset;
545#endif
546 if (encoding == NULL)
547 encoding = "utf-8";
548 }
549
Victor Stinner92055202020-04-08 00:38:15 +0200550 wo = PyObject_New(PyCursesWindowObject, &PyCursesWindow_Type);
Victor Stinner26486ea2010-05-15 22:23:53 +0000551 if (wo == NULL) return NULL;
552 wo->win = win;
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200553 wo->encoding = _PyMem_Strdup(encoding);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100554 if (wo->encoding == NULL) {
555 Py_DECREF(wo);
556 PyErr_NoMemory();
557 return NULL;
558 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000559 return (PyObject *)wo;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000560}
561
562static void
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +0000563PyCursesWindow_Dealloc(PyCursesWindowObject *wo)
Guido van Rossumf6971e21994-08-30 12:25:20 +0000564{
Victor Stinner26486ea2010-05-15 22:23:53 +0000565 if (wo->win != stdscr) delwin(wo->win);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100566 if (wo->encoding != NULL)
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200567 PyMem_Free(wo->encoding);
Victor Stinner26486ea2010-05-15 22:23:53 +0000568 PyObject_DEL(wo);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000569}
570
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000571/* Addch, Addstr, Addnstr */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000572
Larry Hastings61272b72014-01-07 12:41:53 -0800573/*[clinic input]
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300574_curses.window.addch
Larry Hastings31826802013-10-19 00:09:25 -0700575
576 [
Larry Hastings31826802013-10-19 00:09:25 -0700577 y: int
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300578 Y-coordinate.
Larry Hastings9147a962014-05-04 04:41:18 -0700579 x: int
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300580 X-coordinate.
Larry Hastings31826802013-10-19 00:09:25 -0700581 ]
582
583 ch: object
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300584 Character to add.
Larry Hastings31826802013-10-19 00:09:25 -0700585
586 [
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300587 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
588 Attributes for the character.
Larry Hastings31826802013-10-19 00:09:25 -0700589 ]
590 /
591
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300592Paint the character.
Larry Hastings31826802013-10-19 00:09:25 -0700593
594Paint character ch at (y, x) with attributes attr,
595overwriting any character previously painted at that location.
596By default, the character position and attributes are the
597current settings for the window object.
Larry Hastings61272b72014-01-07 12:41:53 -0800598[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700599
Larry Hastings31826802013-10-19 00:09:25 -0700600static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300601_curses_window_addch_impl(PyCursesWindowObject *self, int group_left_1,
602 int y, int x, PyObject *ch, int group_right_1,
603 long attr)
604/*[clinic end generated code: output=00f4c37af3378f45 input=95ce131578458196]*/
Larry Hastings31826802013-10-19 00:09:25 -0700605{
Larry Hastings31826802013-10-19 00:09:25 -0700606 int coordinates_group = group_left_1;
Larry Hastings31826802013-10-19 00:09:25 -0700607 int rtn;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100608 int type;
Victor Stinnerb110dad2016-12-09 17:06:43 +0100609 chtype cch = 0;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100610#ifdef HAVE_NCURSESW
Serhiy Storchaka0bcd89b2016-10-30 22:52:06 +0200611 wchar_t wstr[2];
612 cchar_t wcval;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100613#endif
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100614 const char *funcname;
Guido van Rossum85738471995-02-17 13:50:17 +0000615
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100616#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300617 type = PyCurses_ConvertToCchar_t(self, ch, &cch, wstr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100618 if (type == 2) {
619 funcname = "add_wch";
Serhiy Storchaka0bcd89b2016-10-30 22:52:06 +0200620 wstr[1] = L'\0';
Victor Stinner077af8c2019-08-14 12:31:43 +0200621 setcchar(&wcval, wstr, attr, attr_to_color_pair(attr), NULL);
Larry Hastings31826802013-10-19 00:09:25 -0700622 if (coordinates_group)
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300623 rtn = mvwadd_wch(self->win,y,x, &wcval);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100624 else {
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300625 rtn = wadd_wch(self->win, &wcval);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100626 }
627 }
628 else
629#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300630 type = PyCurses_ConvertToCchar_t(self, ch, &cch);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100631#endif
632 if (type == 1) {
633 funcname = "addch";
Larry Hastings31826802013-10-19 00:09:25 -0700634 if (coordinates_group)
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300635 rtn = mvwaddch(self->win,y,x, cch | (attr_t) attr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100636 else {
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300637 rtn = waddch(self->win, cch | (attr_t) attr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100638 }
639 }
640 else {
Victor Stinner26486ea2010-05-15 22:23:53 +0000641 return NULL;
642 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100643 return PyCursesCheckERR(rtn, funcname);
Guido van Rossumf6971e21994-08-30 12:25:20 +0000644}
645
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300646/*[clinic input]
647_curses.window.addstr
648
649 [
650 y: int
651 Y-coordinate.
652 x: int
653 X-coordinate.
654 ]
655
656 str: object
657 String to add.
658
659 [
660 attr: long
661 Attributes for characters.
662 ]
663 /
664
665Paint the string.
666
667Paint the string str at (y, x) with attributes attr,
668overwriting anything previously on the display.
669By default, the character position and attributes are the
670current settings for the window object.
671[clinic start generated code]*/
672
Guido van Rossumf6971e21994-08-30 12:25:20 +0000673static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300674_curses_window_addstr_impl(PyCursesWindowObject *self, int group_left_1,
675 int y, int x, PyObject *str, int group_right_1,
676 long attr)
677/*[clinic end generated code: output=65a928ea85ff3115 input=ff6cbb91448a22a3]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +0000678{
Victor Stinner26486ea2010-05-15 22:23:53 +0000679 int rtn;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100680 int strtype;
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300681 PyObject *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100682#ifdef HAVE_NCURSESW
683 wchar_t *wstr = NULL;
684#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300685 attr_t attr_old = A_NORMAL;
686 int use_xy = group_left_1, use_attr = group_right_1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100687 const char *funcname;
Guido van Rossum85738471995-02-17 13:50:17 +0000688
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100689#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300690 strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100691#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300692 strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100693#endif
694 if (strtype == 0)
695 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300696 if (use_attr) {
Victor Stinner26486ea2010-05-15 22:23:53 +0000697 attr_old = getattrs(self->win);
698 (void)wattrset(self->win,attr);
699 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100700#ifdef HAVE_NCURSESW
701 if (strtype == 2) {
702 funcname = "addwstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300703 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100704 rtn = mvwaddwstr(self->win,y,x,wstr);
705 else
706 rtn = waddwstr(self->win,wstr);
707 PyMem_Free(wstr);
708 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000709 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100710#endif
711 {
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +0300712 const char *str = PyBytes_AS_STRING(bytesobj);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100713 funcname = "addstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300714 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100715 rtn = mvwaddstr(self->win,y,x,str);
716 else
717 rtn = waddstr(self->win,str);
718 Py_DECREF(bytesobj);
719 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300720 if (use_attr)
Victor Stinner26486ea2010-05-15 22:23:53 +0000721 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100722 return PyCursesCheckERR(rtn, funcname);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000723}
Guido van Rossum85738471995-02-17 13:50:17 +0000724
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300725/*[clinic input]
726_curses.window.addnstr
727
728 [
729 y: int
730 Y-coordinate.
731 x: int
732 X-coordinate.
733 ]
734
735 str: object
736 String to add.
737
738 n: int
739 Maximal number of characters.
740
741 [
742 attr: long
743 Attributes for characters.
744 ]
745 /
746
747Paint at most n characters of the string.
748
749Paint at most n characters of the string str at (y, x) with
750attributes attr, overwriting anything previously on the display.
751By default, the character position and attributes are the
752current settings for the window object.
753[clinic start generated code]*/
754
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000755static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300756_curses_window_addnstr_impl(PyCursesWindowObject *self, int group_left_1,
757 int y, int x, PyObject *str, int n,
758 int group_right_1, long attr)
759/*[clinic end generated code: output=6d21cee2ce6876d9 input=72718415c2744a2a]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000760{
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300761 int rtn;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100762 int strtype;
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300763 PyObject *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100764#ifdef HAVE_NCURSESW
765 wchar_t *wstr = NULL;
766#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300767 attr_t attr_old = A_NORMAL;
768 int use_xy = group_left_1, use_attr = group_right_1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100769 const char *funcname;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000770
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100771#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300772 strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100773#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300774 strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100775#endif
776 if (strtype == 0)
777 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000778
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300779 if (use_attr) {
Victor Stinner26486ea2010-05-15 22:23:53 +0000780 attr_old = getattrs(self->win);
781 (void)wattrset(self->win,attr);
782 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100783#ifdef HAVE_NCURSESW
784 if (strtype == 2) {
785 funcname = "addnwstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300786 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100787 rtn = mvwaddnwstr(self->win,y,x,wstr,n);
788 else
789 rtn = waddnwstr(self->win,wstr,n);
790 PyMem_Free(wstr);
791 }
Victor Stinner26486ea2010-05-15 22:23:53 +0000792 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100793#endif
794 {
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +0300795 const char *str = PyBytes_AS_STRING(bytesobj);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100796 funcname = "addnstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300797 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100798 rtn = mvwaddnstr(self->win,y,x,str,n);
799 else
800 rtn = waddnstr(self->win,str,n);
801 Py_DECREF(bytesobj);
802 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300803 if (use_attr)
Victor Stinner26486ea2010-05-15 22:23:53 +0000804 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100805 return PyCursesCheckERR(rtn, funcname);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000806}
807
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300808/*[clinic input]
809_curses.window.bkgd
810
811 ch: object
812 Background character.
813 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
814 Background attributes.
815 /
816
817Set the background property of the window.
818[clinic start generated code]*/
819
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000820static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300821_curses_window_bkgd_impl(PyCursesWindowObject *self, PyObject *ch, long attr)
822/*[clinic end generated code: output=058290afb2cf4034 input=634015bcb339283d]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000823{
Victor Stinner26486ea2010-05-15 22:23:53 +0000824 chtype bkgd;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000825
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300826 if (!PyCurses_ConvertToChtype(self, ch, &bkgd))
Victor Stinner26486ea2010-05-15 22:23:53 +0000827 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000828
Victor Stinner26486ea2010-05-15 22:23:53 +0000829 return PyCursesCheckERR(wbkgd(self->win, bkgd | attr), "bkgd");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000830}
831
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300832/*[clinic input]
833_curses.window.attroff
834
835 attr: long
836 /
837
838Remove attribute attr from the "background" set.
839[clinic start generated code]*/
Christian Heimes2380ac72008-01-09 00:17:24 +0000840
841static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300842_curses_window_attroff_impl(PyCursesWindowObject *self, long attr)
843/*[clinic end generated code: output=8a2fcd4df682fc64 input=786beedf06a7befe]*/
Christian Heimes2380ac72008-01-09 00:17:24 +0000844{
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300845 return PyCursesCheckERR(wattroff(self->win, (attr_t)attr), "attroff");
Christian Heimes2380ac72008-01-09 00:17:24 +0000846}
847
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300848/*[clinic input]
849_curses.window.attron
850
851 attr: long
852 /
853
854Add attribute attr from the "background" set.
855[clinic start generated code]*/
Christian Heimes2380ac72008-01-09 00:17:24 +0000856
857static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300858_curses_window_attron_impl(PyCursesWindowObject *self, long attr)
859/*[clinic end generated code: output=7afea43b237fa870 input=5a88fba7b1524f32]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000860{
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300861 return PyCursesCheckERR(wattron(self->win, (attr_t)attr), "attron");
862}
863
864/*[clinic input]
865_curses.window.attrset
866
867 attr: long
868 /
869
870Set the "background" set of attributes.
871[clinic start generated code]*/
872
873static PyObject *
874_curses_window_attrset_impl(PyCursesWindowObject *self, long attr)
875/*[clinic end generated code: output=84e379bff20c0433 input=42e400c0d0154ab5]*/
876{
877 return PyCursesCheckERR(wattrset(self->win, (attr_t)attr), "attrset");
878}
879
880/*[clinic input]
881_curses.window.bkgdset
882
883 ch: object
884 Background character.
885 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
886 Background attributes.
887 /
888
889Set the window's background.
890[clinic start generated code]*/
891
892static PyObject *
893_curses_window_bkgdset_impl(PyCursesWindowObject *self, PyObject *ch,
894 long attr)
895/*[clinic end generated code: output=8cb994fc4d7e2496 input=e09c682425c9e45b]*/
896{
Victor Stinner26486ea2010-05-15 22:23:53 +0000897 chtype bkgd;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000898
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300899 if (!PyCurses_ConvertToChtype(self, ch, &bkgd))
Victor Stinner26486ea2010-05-15 22:23:53 +0000900 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000901
Victor Stinner26486ea2010-05-15 22:23:53 +0000902 wbkgdset(self->win, bkgd | attr);
903 return PyCursesCheckERR(0, "bkgdset");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000904}
905
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300906/*[clinic input]
907_curses.window.border
908
909 ls: object(c_default="NULL") = _curses.ACS_VLINE
910 Left side.
911 rs: object(c_default="NULL") = _curses.ACS_VLINE
912 Right side.
913 ts: object(c_default="NULL") = _curses.ACS_HLINE
914 Top side.
915 bs: object(c_default="NULL") = _curses.ACS_HLINE
916 Bottom side.
917 tl: object(c_default="NULL") = _curses.ACS_ULCORNER
918 Upper-left corner.
919 tr: object(c_default="NULL") = _curses.ACS_URCORNER
920 Upper-right corner.
921 bl: object(c_default="NULL") = _curses.ACS_LLCORNER
922 Bottom-left corner.
923 br: object(c_default="NULL") = _curses.ACS_LRCORNER
924 Bottom-right corner.
925 /
926
927Draw a border around the edges of the window.
928
929Each parameter specifies the character to use for a specific part of the
930border. The characters can be specified as integers or as one-character
931strings. A 0 value for any parameter will cause the default character to be
932used for that parameter.
933[clinic start generated code]*/
934
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000935static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300936_curses_window_border_impl(PyCursesWindowObject *self, PyObject *ls,
937 PyObject *rs, PyObject *ts, PyObject *bs,
938 PyObject *tl, PyObject *tr, PyObject *bl,
939 PyObject *br)
940/*[clinic end generated code: output=670ef38d3d7c2aa3 input=e015f735d67a240b]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000941{
Victor Stinner26486ea2010-05-15 22:23:53 +0000942 chtype ch[8];
943 int i;
Andrew M. Kuchling16e65a02001-10-20 16:05:52 +0000944
Victor Stinner26486ea2010-05-15 22:23:53 +0000945 /* Clear the array of parameters */
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300946 for(i=0; i<8; i++)
Victor Stinner26486ea2010-05-15 22:23:53 +0000947 ch[i] = 0;
Andrew M. Kuchling16e65a02001-10-20 16:05:52 +0000948
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300949#define CONVERTTOCHTYPE(obj, i) \
950 if ((obj) != NULL && !PyCurses_ConvertToChtype(self, (obj), &ch[(i)])) \
Victor Stinner26486ea2010-05-15 22:23:53 +0000951 return NULL;
952
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300953 CONVERTTOCHTYPE(ls, 0);
954 CONVERTTOCHTYPE(rs, 1);
955 CONVERTTOCHTYPE(ts, 2);
956 CONVERTTOCHTYPE(bs, 3);
957 CONVERTTOCHTYPE(tl, 4);
958 CONVERTTOCHTYPE(tr, 5);
959 CONVERTTOCHTYPE(bl, 6);
960 CONVERTTOCHTYPE(br, 7);
961
962#undef CONVERTTOCHTYPE
Victor Stinner26486ea2010-05-15 22:23:53 +0000963
964 wborder(self->win,
965 ch[0], ch[1], ch[2], ch[3],
966 ch[4], ch[5], ch[6], ch[7]);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200967 Py_RETURN_NONE;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000968}
969
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300970/*[clinic input]
971_curses.window.box
972
973 [
974 verch: object(c_default="_PyLong_Zero") = 0
975 Left and right side.
976 horch: object(c_default="_PyLong_Zero") = 0
977 Top and bottom side.
978 ]
979 /
980
981Draw a border around the edges of the window.
982
983Similar to border(), but both ls and rs are verch and both ts and bs are
984horch. The default corner characters are always used by this function.
985[clinic start generated code]*/
986
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000987static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300988_curses_window_box_impl(PyCursesWindowObject *self, int group_right_1,
989 PyObject *verch, PyObject *horch)
990/*[clinic end generated code: output=f3fcb038bb287192 input=465a121741c1efdf]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +0000991{
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300992 chtype ch1 = 0, ch2 = 0;
993 if (group_right_1) {
994 if (!PyCurses_ConvertToChtype(self, verch, &ch1)) {
Serhiy Storchaka4f469c02017-11-01 20:48:49 +0200995 return NULL;
996 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +0300997 if (!PyCurses_ConvertToChtype(self, horch, &ch2)) {
Serhiy Storchaka4f469c02017-11-01 20:48:49 +0200998 return NULL;
999 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001000 }
1001 box(self->win,ch1,ch2);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001002 Py_RETURN_NONE;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001003}
1004
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001005#if defined(HAVE_NCURSES_H) || defined(MVWDELCH_IS_EXPRESSION)
1006#define py_mvwdelch mvwdelch
1007#else
1008int py_mvwdelch(WINDOW *w, int y, int x)
1009{
Victor Stinner26486ea2010-05-15 22:23:53 +00001010 mvwdelch(w,y,x);
1011 /* On HP/UX, mvwdelch already returns. On other systems,
1012 we may well run into this return statement. */
1013 return 0;
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001014}
1015#endif
1016
Masayuki Yamamoto8bc7d632017-11-01 21:05:26 +09001017#if defined(HAVE_CURSES_IS_PAD)
1018#define py_is_pad(win) is_pad(win)
1019#elif defined(WINDOW_HAS_FLAGS)
1020#define py_is_pad(win) ((win) ? ((win)->_flags & _ISPAD) != 0 : FALSE)
1021#endif
1022
Guido van Rossumd8faa362007-04-27 19:54:29 +00001023/* chgat, added by Fabian Kreutz <fabian.kreutz at gmx.net> */
Serhiy Storchaka894ebd02017-11-01 14:34:20 +02001024#ifdef HAVE_CURSES_WCHGAT
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001025/*[-clinic input]
1026_curses.window.chgat
1027
1028 [
1029 y: int
1030 Y-coordinate.
1031 x: int
1032 X-coordinate.
1033 ]
1034
1035 n: int = -1
1036 Number of characters.
1037
1038 attr: long
1039 Attributes for characters.
1040 /
1041
1042Set the attributes of characters.
1043
1044Set the attributes of num characters at the current cursor position, or at
1045position (y, x) if supplied. If no value of num is given or num = -1, the
1046attribute will be set on all the characters to the end of the line. This
1047function does not move the cursor. The changed line will be touched using
1048the touchline() method so that the contents will be redisplayed by the next
1049window refresh.
1050[-clinic start generated code]*/
Guido van Rossumd8faa362007-04-27 19:54:29 +00001051static PyObject *
1052PyCursesWindow_ChgAt(PyCursesWindowObject *self, PyObject *args)
1053{
Victor Stinner26486ea2010-05-15 22:23:53 +00001054 int rtn;
1055 int x, y;
1056 int num = -1;
1057 short color;
1058 attr_t attr = A_NORMAL;
1059 long lattr;
1060 int use_xy = FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001061
Victor Stinner26486ea2010-05-15 22:23:53 +00001062 switch (PyTuple_Size(args)) {
1063 case 1:
1064 if (!PyArg_ParseTuple(args,"l;attr", &lattr))
1065 return NULL;
1066 attr = lattr;
1067 break;
1068 case 2:
1069 if (!PyArg_ParseTuple(args,"il;n,attr", &num, &lattr))
1070 return NULL;
1071 attr = lattr;
1072 break;
1073 case 3:
1074 if (!PyArg_ParseTuple(args,"iil;int,int,attr", &y, &x, &lattr))
1075 return NULL;
1076 attr = lattr;
1077 use_xy = TRUE;
1078 break;
1079 case 4:
1080 if (!PyArg_ParseTuple(args,"iiil;int,int,n,attr", &y, &x, &num, &lattr))
1081 return NULL;
1082 attr = lattr;
1083 use_xy = TRUE;
1084 break;
1085 default:
1086 PyErr_SetString(PyExc_TypeError, "chgat requires 1 to 4 arguments");
1087 return NULL;
1088 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001089
Victor Stinner26486ea2010-05-15 22:23:53 +00001090 color = (short)((attr >> 8) & 0xff);
1091 attr = attr - (color << 8);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001092
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001093 if (use_xy) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001094 rtn = mvwchgat(self->win,y,x,num,attr,color,NULL);
1095 touchline(self->win,y,1);
1096 } else {
1097 getyx(self->win,y,x);
1098 rtn = wchgat(self->win,num,attr,color,NULL);
1099 touchline(self->win,y,1);
1100 }
1101 return PyCursesCheckERR(rtn, "chgat");
Guido van Rossumd8faa362007-04-27 19:54:29 +00001102}
Serhiy Storchaka894ebd02017-11-01 14:34:20 +02001103#endif
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00001104
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001105/*[clinic input]
1106_curses.window.delch
Guido van Rossum85738471995-02-17 13:50:17 +00001107
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001108 [
1109 y: int
1110 Y-coordinate.
1111 x: int
1112 X-coordinate.
1113 ]
1114 /
1115
1116Delete any character at (y, x).
1117[clinic start generated code]*/
1118
1119static PyObject *
1120_curses_window_delch_impl(PyCursesWindowObject *self, int group_right_1,
1121 int y, int x)
1122/*[clinic end generated code: output=22e77bb9fa11b461 input=d2f79e630a4fc6d0]*/
1123{
1124 if (!group_right_1) {
1125 return PyCursesCheckERR(wdelch(self->win), "wdelch");
Victor Stinner26486ea2010-05-15 22:23:53 +00001126 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001127 else {
1128 return PyCursesCheckERR(py_mvwdelch(self->win, y, x), "mvwdelch");
1129 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001130}
1131
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001132/*[clinic input]
1133_curses.window.derwin
1134
1135 [
1136 nlines: int = 0
1137 Height.
1138 ncols: int = 0
1139 Width.
1140 ]
1141 begin_y: int
1142 Top side y-coordinate.
1143 begin_x: int
1144 Left side x-coordinate.
1145 /
1146
1147Create a sub-window (window-relative coordinates).
1148
1149derwin() is the same as calling subwin(), except that begin_y and begin_x
1150are relative to the origin of the window, rather than relative to the entire
1151screen.
1152[clinic start generated code]*/
1153
Guido van Rossumf6971e21994-08-30 12:25:20 +00001154static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001155_curses_window_derwin_impl(PyCursesWindowObject *self, int group_left_1,
1156 int nlines, int ncols, int begin_y, int begin_x)
1157/*[clinic end generated code: output=7924b112d9f70d6e input=966d9481f7f5022e]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001158{
Victor Stinner26486ea2010-05-15 22:23:53 +00001159 WINDOW *win;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001160
Victor Stinner26486ea2010-05-15 22:23:53 +00001161 win = derwin(self->win,nlines,ncols,begin_y,begin_x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001162
Victor Stinner26486ea2010-05-15 22:23:53 +00001163 if (win == NULL) {
1164 PyErr_SetString(PyCursesError, catchall_NULL);
1165 return NULL;
1166 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001167
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001168 return (PyObject *)PyCursesWindow_New(win, NULL);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001169}
1170
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001171/*[clinic input]
1172_curses.window.echochar
1173
1174 ch: object
1175 Character to add.
1176
1177 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
1178 Attributes for the character.
1179 /
1180
1181Add character ch with attribute attr, and refresh.
1182[clinic start generated code]*/
1183
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001184static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001185_curses_window_echochar_impl(PyCursesWindowObject *self, PyObject *ch,
1186 long attr)
1187/*[clinic end generated code: output=13e7dd875d4b9642 input=e7f34b964e92b156]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001188{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001189 chtype ch_;
Guido van Rossum85738471995-02-17 13:50:17 +00001190
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001191 if (!PyCurses_ConvertToChtype(self, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00001192 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00001193
Masayuki Yamamoto8bc7d632017-11-01 21:05:26 +09001194#ifdef py_is_pad
1195 if (py_is_pad(self->win)) {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001196 return PyCursesCheckERR(pechochar(self->win, ch_ | (attr_t)attr),
Victor Stinner26486ea2010-05-15 22:23:53 +00001197 "echochar");
Masayuki Yamamoto8bc7d632017-11-01 21:05:26 +09001198 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001199 else
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001200#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001201 return PyCursesCheckERR(wechochar(self->win, ch_ | (attr_t)attr),
Victor Stinner26486ea2010-05-15 22:23:53 +00001202 "echochar");
Guido van Rossumf6971e21994-08-30 12:25:20 +00001203}
1204
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001205#ifdef NCURSES_MOUSE_VERSION
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001206/*[clinic input]
1207_curses.window.enclose -> long
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001208
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001209 y: int
1210 Y-coordinate.
1211 x: int
1212 X-coordinate.
1213 /
1214
1215Return True if the screen-relative coordinates are enclosed by the window.
1216[clinic start generated code]*/
1217
1218static long
1219_curses_window_enclose_impl(PyCursesWindowObject *self, int y, int x)
1220/*[clinic end generated code: output=5251c961cbe3df63 input=dfe1d9d4d05d8642]*/
1221{
1222 return wenclose(self->win, y, x);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00001223}
1224#endif
1225
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001226/*[clinic input]
1227_curses.window.getbkgd -> long
1228
1229Return the window's current background character/attribute pair.
1230[clinic start generated code]*/
1231
1232static long
1233_curses_window_getbkgd_impl(PyCursesWindowObject *self)
1234/*[clinic end generated code: output=c52b25dc16b215c3 input=a69db882fa35426c]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001235{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001236 return (long) getbkgd(self->win);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001237}
1238
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001239/*[clinic input]
1240_curses.window.getch -> int
1241
1242 [
1243 y: int
1244 Y-coordinate.
1245 x: int
1246 X-coordinate.
1247 ]
1248 /
1249
1250Get a character code from terminal keyboard.
1251
1252The integer returned does not have to be in ASCII range: function keys,
1253keypad keys and so on return numbers higher than 256. In no-delay mode, -1
1254is returned if there is no input, else getch() waits until a key is pressed.
1255[clinic start generated code]*/
1256
1257static int
1258_curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
1259 int y, int x)
1260/*[clinic end generated code: output=980aa6af0c0ca387 input=bb24ebfb379f991f]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001261{
Victor Stinner26486ea2010-05-15 22:23:53 +00001262 int rtn;
Guido van Rossum85738471995-02-17 13:50:17 +00001263
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001264 Py_BEGIN_ALLOW_THREADS
1265 if (!group_right_1) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001266 rtn = wgetch(self->win);
Victor Stinner26486ea2010-05-15 22:23:53 +00001267 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001268 else {
1269 rtn = mvwgetch(self->win, y, x);
1270 }
1271 Py_END_ALLOW_THREADS
1272
1273 return rtn;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001274}
Guido van Rossum85738471995-02-17 13:50:17 +00001275
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001276/*[clinic input]
1277_curses.window.getkey
1278
1279 [
1280 y: int
1281 Y-coordinate.
1282 x: int
1283 X-coordinate.
1284 ]
1285 /
1286
1287Get a character (string) from terminal keyboard.
1288
1289Returning a string instead of an integer, as getch() does. Function keys,
1290keypad keys and other special keys return a multibyte string containing the
1291key name. In no-delay mode, an exception is raised if there is no input.
1292[clinic start generated code]*/
1293
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001294static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001295_curses_window_getkey_impl(PyCursesWindowObject *self, int group_right_1,
1296 int y, int x)
1297/*[clinic end generated code: output=8490a182db46b10f input=be2dee34f5cf57f8]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001298{
Victor Stinner26486ea2010-05-15 22:23:53 +00001299 int rtn;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001300
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001301 Py_BEGIN_ALLOW_THREADS
1302 if (!group_right_1) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001303 rtn = wgetch(self->win);
Victor Stinner26486ea2010-05-15 22:23:53 +00001304 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001305 else {
1306 rtn = mvwgetch(self->win, y, x);
1307 }
1308 Py_END_ALLOW_THREADS
1309
Victor Stinner26486ea2010-05-15 22:23:53 +00001310 if (rtn == ERR) {
1311 /* getch() returns ERR in nodelay mode */
R David Murrayf5d7cc22013-03-19 16:23:09 -04001312 PyErr_CheckSignals();
1313 if (!PyErr_Occurred())
1314 PyErr_SetString(PyCursesError, "no input");
Victor Stinner26486ea2010-05-15 22:23:53 +00001315 return NULL;
Serhiy Storchaka7e687902017-11-01 16:03:40 +02001316 } else if (rtn <= 255) {
1317#ifdef NCURSES_VERSION_MAJOR
1318#if NCURSES_VERSION_MAJOR*100+NCURSES_VERSION_MINOR <= 507
1319 /* Work around a bug in ncurses 5.7 and earlier */
1320 if (rtn < 0) {
1321 rtn += 256;
1322 }
1323#endif
1324#endif
1325 return PyUnicode_FromOrdinal(rtn);
Victor Stinner26486ea2010-05-15 22:23:53 +00001326 } else {
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02001327 const char *knp = keyname(rtn);
Victor Stinner26486ea2010-05-15 22:23:53 +00001328 return PyUnicode_FromString((knp == NULL) ? "" : knp);
1329 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00001330}
1331
Nadeem Vawda9e2e9902011-07-31 15:01:11 +02001332#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001333/*[clinic input]
1334_curses.window.get_wch
1335
1336 [
1337 y: int
1338 Y-coordinate.
1339 x: int
1340 X-coordinate.
1341 ]
1342 /
1343
1344Get a wide character from terminal keyboard.
1345
1346Return a character for most keys, or an integer for function keys,
1347keypad keys, and other special keys.
1348[clinic start generated code]*/
1349
Guido van Rossumf6971e21994-08-30 12:25:20 +00001350static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001351_curses_window_get_wch_impl(PyCursesWindowObject *self, int group_right_1,
1352 int y, int x)
1353/*[clinic end generated code: output=9f4f86e91fe50ef3 input=dd7e5367fb49dc48]*/
Victor Stinnera7878b72011-07-14 23:07:44 +02001354{
Victor Stinnera7878b72011-07-14 23:07:44 +02001355 int ct;
1356 wint_t rtn;
1357
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001358 Py_BEGIN_ALLOW_THREADS
1359 if (!group_right_1) {
1360 ct = wget_wch(self->win ,&rtn);
Victor Stinnera7878b72011-07-14 23:07:44 +02001361 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001362 else {
1363 ct = mvwget_wch(self->win, y, x, &rtn);
1364 }
1365 Py_END_ALLOW_THREADS
1366
Victor Stinnera7878b72011-07-14 23:07:44 +02001367 if (ct == ERR) {
Victor Stinnerbd2d30c2013-03-21 12:21:06 +01001368 if (PyErr_CheckSignals())
1369 return NULL;
1370
Victor Stinnera7878b72011-07-14 23:07:44 +02001371 /* get_wch() returns ERR in nodelay mode */
1372 PyErr_SetString(PyCursesError, "no input");
1373 return NULL;
1374 }
Victor Stinner1d39cde2012-08-29 01:40:57 +02001375 if (ct == KEY_CODE_YES)
1376 return PyLong_FromLong(rtn);
1377 else
1378 return PyUnicode_FromOrdinal(rtn);
Victor Stinnera7878b72011-07-14 23:07:44 +02001379}
Nadeem Vawda9e2e9902011-07-31 15:01:11 +02001380#endif
Victor Stinnera7878b72011-07-14 23:07:44 +02001381
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001382/*[-clinic input]
1383_curses.window.getstr
1384
1385 [
1386 y: int
1387 Y-coordinate.
1388 x: int
1389 X-coordinate.
1390 ]
1391 n: int = 1023
1392 Maximal number of characters.
1393 /
1394
1395Read a string from the user, with primitive line editing capacity.
1396[-clinic start generated code]*/
1397
Victor Stinnera7878b72011-07-14 23:07:44 +02001398static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001399PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001400{
Victor Stinner26486ea2010-05-15 22:23:53 +00001401 int x, y, n;
1402 char rtn[1024]; /* This should be big enough.. I hope */
1403 int rtn2;
Guido van Rossum85738471995-02-17 13:50:17 +00001404
Victor Stinner26486ea2010-05-15 22:23:53 +00001405 switch (PyTuple_Size(args)) {
1406 case 0:
1407 Py_BEGIN_ALLOW_THREADS
1408 rtn2 = wgetnstr(self->win,rtn, 1023);
1409 Py_END_ALLOW_THREADS
1410 break;
1411 case 1:
1412 if (!PyArg_ParseTuple(args,"i;n", &n))
1413 return NULL;
Benjamin Peterson40a77c32016-08-13 18:15:28 -07001414 if (n < 0) {
1415 PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1416 return NULL;
1417 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001418 Py_BEGIN_ALLOW_THREADS
Victor Stinner640c35c2013-06-04 23:14:37 +02001419 rtn2 = wgetnstr(self->win, rtn, Py_MIN(n, 1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001420 Py_END_ALLOW_THREADS
1421 break;
1422 case 2:
1423 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1424 return NULL;
1425 Py_BEGIN_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001426#ifdef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00001427 rtn2 = wmove(self->win,y,x)==ERR ? ERR : wgetnstr(self->win, rtn, 1023);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001428#else
Victor Stinner26486ea2010-05-15 22:23:53 +00001429 rtn2 = mvwgetnstr(self->win,y,x,rtn, 1023);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001430#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001431 Py_END_ALLOW_THREADS
1432 break;
1433 case 3:
1434 if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
1435 return NULL;
Benjamin Peterson40a77c32016-08-13 18:15:28 -07001436 if (n < 0) {
1437 PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1438 return NULL;
1439 }
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +00001440#ifdef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00001441 Py_BEGIN_ALLOW_THREADS
1442 rtn2 = wmove(self->win,y,x)==ERR ? ERR :
Victor Stinner640c35c2013-06-04 23:14:37 +02001443 wgetnstr(self->win, rtn, Py_MIN(n, 1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001444 Py_END_ALLOW_THREADS
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001445#else
Victor Stinner26486ea2010-05-15 22:23:53 +00001446 Py_BEGIN_ALLOW_THREADS
Victor Stinner640c35c2013-06-04 23:14:37 +02001447 rtn2 = mvwgetnstr(self->win, y, x, rtn, Py_MIN(n, 1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001448 Py_END_ALLOW_THREADS
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001449#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00001450 break;
1451 default:
1452 PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 3 arguments");
1453 return NULL;
1454 }
1455 if (rtn2 == ERR)
1456 rtn[0] = 0;
1457 return PyBytes_FromString(rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001458}
1459
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001460/*[clinic input]
1461_curses.window.hline
1462
1463 [
1464 y: int
1465 Starting Y-coordinate.
1466 x: int
1467 Starting X-coordinate.
1468 ]
1469
1470 ch: object
1471 Character to draw.
1472 n: int
1473 Line length.
1474
1475 [
1476 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
1477 Attributes for the characters.
1478 ]
1479 /
1480
1481Display a horizontal line.
1482[clinic start generated code]*/
1483
Guido van Rossumf6971e21994-08-30 12:25:20 +00001484static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001485_curses_window_hline_impl(PyCursesWindowObject *self, int group_left_1,
1486 int y, int x, PyObject *ch, int n,
1487 int group_right_1, long attr)
1488/*[clinic end generated code: output=c00d489d61fc9eef input=81a4dea47268163e]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001489{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001490 chtype ch_;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001491
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001492 if (!PyCurses_ConvertToChtype(self, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00001493 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001494 if (group_left_1) {
1495 if (wmove(self->win, y, x) == ERR) {
1496 return PyCursesCheckERR(ERR, "wmove");
1497 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001498 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001499 return PyCursesCheckERR(whline(self->win, ch_ | (attr_t)attr, n), "hline");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001500}
1501
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001502/*[clinic input]
1503_curses.window.insch
1504
1505 [
1506 y: int
1507 Y-coordinate.
1508 x: int
1509 X-coordinate.
1510 ]
1511
1512 ch: object
1513 Character to insert.
1514
1515 [
1516 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
1517 Attributes for the character.
1518 ]
1519 /
1520
1521Insert a character before the current or specified position.
1522
1523All characters to the right of the cursor are shifted one position right, with
1524the rightmost characters on the line being lost.
1525[clinic start generated code]*/
1526
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001527static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001528_curses_window_insch_impl(PyCursesWindowObject *self, int group_left_1,
1529 int y, int x, PyObject *ch, int group_right_1,
1530 long attr)
1531/*[clinic end generated code: output=ade8cfe3a3bf3e34 input=336342756ee19812]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001532{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001533 int rtn;
1534 chtype ch_ = 0;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001535
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001536 if (!PyCurses_ConvertToChtype(self, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00001537 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001538
1539 if (!group_left_1) {
1540 rtn = winsch(self->win, ch_ | (attr_t)attr);
Victor Stinner26486ea2010-05-15 22:23:53 +00001541 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001542 else {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001543 rtn = mvwinsch(self->win, y, x, ch_ | (attr_t)attr);
Victor Stinner26486ea2010-05-15 22:23:53 +00001544 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001545
Victor Stinner26486ea2010-05-15 22:23:53 +00001546 return PyCursesCheckERR(rtn, "insch");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001547}
1548
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001549/*[clinic input]
1550_curses.window.inch -> unsigned_long
1551
1552 [
1553 y: int
1554 Y-coordinate.
1555 x: int
1556 X-coordinate.
1557 ]
1558 /
1559
1560Return the character at the given position in the window.
1561
1562The bottom 8 bits are the character proper, and upper bits are the attributes.
1563[clinic start generated code]*/
1564
1565static unsigned long
1566_curses_window_inch_impl(PyCursesWindowObject *self, int group_right_1,
1567 int y, int x)
1568/*[clinic end generated code: output=6c4719fe978fe86a input=fac23ee11e3b3a66]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001569{
Christian Heimesabbc8ca2013-12-04 08:50:22 +01001570 unsigned long rtn;
Guido van Rossum85738471995-02-17 13:50:17 +00001571
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001572 if (!group_right_1) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001573 rtn = winch(self->win);
Victor Stinner26486ea2010-05-15 22:23:53 +00001574 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001575 else {
1576 rtn = mvwinch(self->win, y, x);
1577 }
1578
1579 return rtn;
Guido van Rossumf6971e21994-08-30 12:25:20 +00001580}
1581
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001582/*[-clinic input]
1583_curses.window.instr
1584
1585 [
1586 y: int
1587 Y-coordinate.
1588 x: int
1589 X-coordinate.
1590 ]
1591 n: int = 1023
1592 Maximal number of characters.
1593 /
1594
1595Return a string of characters, extracted from the window.
1596
1597Return a string of characters, extracted from the window starting at the
1598current cursor position, or at y, x if specified. Attributes are stripped
1599from the characters. If n is specified, instr() returns a string at most
1600n characters long (exclusive of the trailing NUL).
1601[-clinic start generated code]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001602static PyObject *
Andrew M. Kuchlingad9b1f82000-07-09 14:35:00 +00001603PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
Guido van Rossumf6971e21994-08-30 12:25:20 +00001604{
Victor Stinner26486ea2010-05-15 22:23:53 +00001605 int x, y, n;
1606 char rtn[1024]; /* This should be big enough.. I hope */
1607 int rtn2;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001608
Victor Stinner26486ea2010-05-15 22:23:53 +00001609 switch (PyTuple_Size(args)) {
1610 case 0:
1611 rtn2 = winnstr(self->win,rtn, 1023);
1612 break;
1613 case 1:
1614 if (!PyArg_ParseTuple(args,"i;n", &n))
1615 return NULL;
Benjamin Peterson432ea4f2016-08-15 21:40:14 -07001616 if (n < 0) {
1617 PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1618 return NULL;
1619 }
Victor Stinner640c35c2013-06-04 23:14:37 +02001620 rtn2 = winnstr(self->win, rtn, Py_MIN(n, 1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001621 break;
1622 case 2:
1623 if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1624 return NULL;
1625 rtn2 = mvwinnstr(self->win,y,x,rtn,1023);
1626 break;
1627 case 3:
1628 if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
1629 return NULL;
Benjamin Peterson432ea4f2016-08-15 21:40:14 -07001630 if (n < 0) {
1631 PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1632 return NULL;
1633 }
Victor Stinner640c35c2013-06-04 23:14:37 +02001634 rtn2 = mvwinnstr(self->win, y, x, rtn, Py_MIN(n,1023));
Victor Stinner26486ea2010-05-15 22:23:53 +00001635 break;
1636 default:
1637 PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments");
1638 return NULL;
1639 }
1640 if (rtn2 == ERR)
1641 rtn[0] = 0;
1642 return PyBytes_FromString(rtn);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001643}
1644
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001645/*[clinic input]
1646_curses.window.insstr
1647
1648 [
1649 y: int
1650 Y-coordinate.
1651 x: int
1652 X-coordinate.
1653 ]
1654
1655 str: object
1656 String to insert.
1657
1658 [
1659 attr: long
1660 Attributes for characters.
1661 ]
1662 /
1663
1664Insert the string before the current or specified position.
1665
1666Insert a character string (as many characters as will fit on the line)
1667before the character under the cursor. All characters to the right of
1668the cursor are shifted right, with the rightmost characters on the line
1669being lost. The cursor position does not change (after moving to y, x,
1670if specified).
1671[clinic start generated code]*/
1672
Guido van Rossumf6971e21994-08-30 12:25:20 +00001673static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001674_curses_window_insstr_impl(PyCursesWindowObject *self, int group_left_1,
1675 int y, int x, PyObject *str, int group_right_1,
1676 long attr)
1677/*[clinic end generated code: output=c259a5265ad0b777 input=6827cddc6340a7f3]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001678{
Victor Stinner26486ea2010-05-15 22:23:53 +00001679 int rtn;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001680 int strtype;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001681 PyObject *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001682#ifdef HAVE_NCURSESW
1683 wchar_t *wstr = NULL;
1684#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001685 attr_t attr_old = A_NORMAL;
1686 int use_xy = group_left_1, use_attr = group_right_1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001687 const char *funcname;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001688
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001689#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001690 strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001691#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001692 strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001693#endif
1694 if (strtype == 0)
1695 return NULL;
1696
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001697 if (use_attr) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001698 attr_old = getattrs(self->win);
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001699 (void)wattrset(self->win, (attr_t)attr);
Victor Stinner26486ea2010-05-15 22:23:53 +00001700 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001701#ifdef HAVE_NCURSESW
1702 if (strtype == 2) {
1703 funcname = "inswstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001704 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001705 rtn = mvwins_wstr(self->win,y,x,wstr);
1706 else
1707 rtn = wins_wstr(self->win,wstr);
1708 PyMem_Free(wstr);
1709 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001710 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001711#endif
1712 {
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +03001713 const char *str = PyBytes_AS_STRING(bytesobj);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001714 funcname = "insstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001715 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001716 rtn = mvwinsstr(self->win,y,x,str);
1717 else
1718 rtn = winsstr(self->win,str);
1719 Py_DECREF(bytesobj);
1720 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001721 if (use_attr)
Victor Stinner26486ea2010-05-15 22:23:53 +00001722 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001723 return PyCursesCheckERR(rtn, funcname);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001724}
1725
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001726/*[clinic input]
1727_curses.window.insnstr
1728
1729 [
1730 y: int
1731 Y-coordinate.
1732 x: int
1733 X-coordinate.
1734 ]
1735
1736 str: object
1737 String to insert.
1738
1739 n: int
1740 Maximal number of characters.
1741
1742 [
1743 attr: long
1744 Attributes for characters.
1745 ]
1746 /
1747
1748Insert at most n characters of the string.
1749
1750Insert a character string (as many characters as will fit on the line)
1751before the character under the cursor, up to n characters. If n is zero
1752or negative, the entire string is inserted. All characters to the right
1753of the cursor are shifted right, with the rightmost characters on the line
1754being lost. The cursor position does not change (after moving to y, x, if
1755specified).
1756[clinic start generated code]*/
1757
Guido van Rossumf6971e21994-08-30 12:25:20 +00001758static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001759_curses_window_insnstr_impl(PyCursesWindowObject *self, int group_left_1,
1760 int y, int x, PyObject *str, int n,
1761 int group_right_1, long attr)
1762/*[clinic end generated code: output=971a32ea6328ec8b input=70fa0cd543901a4c]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00001763{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001764 int rtn;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001765 int strtype;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001766 PyObject *bytesobj = NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001767#ifdef HAVE_NCURSESW
1768 wchar_t *wstr = NULL;
1769#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001770 attr_t attr_old = A_NORMAL;
1771 int use_xy = group_left_1, use_attr = group_right_1;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001772 const char *funcname;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001773
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001774#ifdef HAVE_NCURSESW
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001775 strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001776#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001777 strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001778#endif
1779 if (strtype == 0)
1780 return NULL;
1781
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001782 if (use_attr) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001783 attr_old = getattrs(self->win);
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001784 (void)wattrset(self->win, (attr_t)attr);
Victor Stinner26486ea2010-05-15 22:23:53 +00001785 }
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001786#ifdef HAVE_NCURSESW
1787 if (strtype == 2) {
1788 funcname = "insn_wstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001789 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001790 rtn = mvwins_nwstr(self->win,y,x,wstr,n);
1791 else
1792 rtn = wins_nwstr(self->win,wstr,n);
1793 PyMem_Free(wstr);
1794 }
Victor Stinner26486ea2010-05-15 22:23:53 +00001795 else
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001796#endif
1797 {
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +03001798 const char *str = PyBytes_AS_STRING(bytesobj);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001799 funcname = "insnstr";
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001800 if (use_xy)
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001801 rtn = mvwinsnstr(self->win,y,x,str,n);
1802 else
1803 rtn = winsnstr(self->win,str,n);
1804 Py_DECREF(bytesobj);
1805 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001806 if (use_attr)
Victor Stinner26486ea2010-05-15 22:23:53 +00001807 (void)wattrset(self->win,attr_old);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01001808 return PyCursesCheckERR(rtn, funcname);
Guido van Rossumf6971e21994-08-30 12:25:20 +00001809}
1810
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001811/*[clinic input]
1812_curses.window.is_linetouched
1813
1814 line: int
1815 Line number.
1816 /
1817
1818Return True if the specified line was modified, otherwise return False.
1819
1820Raise a curses.error exception if line is not valid for the given window.
1821[clinic start generated code]*/
1822
Guido van Rossumf6971e21994-08-30 12:25:20 +00001823static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001824_curses_window_is_linetouched_impl(PyCursesWindowObject *self, int line)
1825/*[clinic end generated code: output=ad4a4edfee2db08c input=a7be0c189f243914]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001826{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001827 int erg;
Victor Stinner26486ea2010-05-15 22:23:53 +00001828 erg = is_linetouched(self->win, line);
1829 if (erg == ERR) {
1830 PyErr_SetString(PyExc_TypeError,
1831 "is_linetouched: line number outside of boundaries");
1832 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001833 }
1834 return PyBool_FromLong(erg);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001835}
1836
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001837#ifdef py_is_pad
1838/*[clinic input]
1839_curses.window.noutrefresh
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001840
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001841 [
1842 pminrow: int
1843 pmincol: int
1844 sminrow: int
1845 smincol: int
1846 smaxrow: int
1847 smaxcol: int
1848 ]
1849 /
1850
1851Mark for refresh but wait.
1852
1853This function updates the data structure representing the desired state of the
1854window, but does not force an update of the physical screen. To accomplish
1855that, call doupdate().
1856[clinic start generated code]*/
1857
1858static PyObject *
1859_curses_window_noutrefresh_impl(PyCursesWindowObject *self,
1860 int group_right_1, int pminrow, int pmincol,
1861 int sminrow, int smincol, int smaxrow,
1862 int smaxcol)
1863/*[clinic end generated code: output=809a1f3c6a03e23e input=3e56898388cd739e]*/
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00001864#else
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001865/*[clinic input]
1866_curses.window.noutrefresh
Andrew M. Kuchlingf4195722000-06-23 01:36:21 +00001867
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001868Mark for refresh but wait.
1869
1870This function updates the data structure representing the desired state of the
1871window, but does not force an update of the physical screen. To accomplish
1872that, call doupdate().
1873[clinic start generated code]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00001874
1875static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001876_curses_window_noutrefresh_impl(PyCursesWindowObject *self)
1877/*[clinic end generated code: output=6ef6dec666643fee input=876902e3fa431dbd]*/
1878#endif
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001879{
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001880 int rtn;
Victor Stinner26486ea2010-05-15 22:23:53 +00001881
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001882#ifdef py_is_pad
1883 if (py_is_pad(self->win)) {
1884 if (!group_right_1) {
1885 PyErr_SetString(PyCursesError,
1886 "noutrefresh() called for a pad "
1887 "requires 6 arguments");
Victor Stinner26486ea2010-05-15 22:23:53 +00001888 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001889 }
1890 Py_BEGIN_ALLOW_THREADS
1891 rtn = pnoutrefresh(self->win, pminrow, pmincol,
1892 sminrow, smincol, smaxrow, smaxcol);
1893 Py_END_ALLOW_THREADS
1894 return PyCursesCheckERR(rtn, "pnoutrefresh");
1895 }
1896 if (group_right_1) {
Victor Stinner26486ea2010-05-15 22:23:53 +00001897 PyErr_SetString(PyExc_TypeError,
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001898 "noutrefresh() takes no arguments (6 given)");
Victor Stinner26486ea2010-05-15 22:23:53 +00001899 return NULL;
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001900 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001901#endif
1902 Py_BEGIN_ALLOW_THREADS
1903 rtn = wnoutrefresh(self->win);
1904 Py_END_ALLOW_THREADS
1905 return PyCursesCheckERR(rtn, "wnoutrefresh");
1906}
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001907
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001908/*[clinic input]
1909_curses.window.overlay
1910
1911 destwin: object(type="PyCursesWindowObject *", subclass_of="&PyCursesWindow_Type")
1912
1913 [
1914 sminrow: int
1915 smincol: int
1916 dminrow: int
1917 dmincol: int
1918 dmaxrow: int
1919 dmaxcol: int
1920 ]
1921 /
1922
1923Overlay the window on top of destwin.
1924
1925The windows need not be the same size, only the overlapping region is copied.
1926This copy is non-destructive, which means that the current background
1927character does not overwrite the old contents of destwin.
1928
1929To get fine-grained control over the copied region, the second form of
1930overlay() can be used. sminrow and smincol are the upper-left coordinates
1931of the source window, and the other variables mark a rectangle in the
1932destination window.
1933[clinic start generated code]*/
1934
1935static PyObject *
1936_curses_window_overlay_impl(PyCursesWindowObject *self,
1937 PyCursesWindowObject *destwin, int group_right_1,
1938 int sminrow, int smincol, int dminrow,
1939 int dmincol, int dmaxrow, int dmaxcol)
1940/*[clinic end generated code: output=82bb2c4cb443ca58 input=7edd23ad22cc1984]*/
1941{
1942 int rtn;
1943
1944 if (group_right_1) {
1945 rtn = copywin(self->win, destwin->win, sminrow, smincol,
Victor Stinner26486ea2010-05-15 22:23:53 +00001946 dminrow, dmincol, dmaxrow, dmaxcol, TRUE);
1947 return PyCursesCheckERR(rtn, "copywin");
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001948 }
1949 else {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001950 rtn = overlay(self->win, destwin->win);
Victor Stinner26486ea2010-05-15 22:23:53 +00001951 return PyCursesCheckERR(rtn, "overlay");
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001952 }
1953}
1954
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001955/*[clinic input]
1956_curses.window.overwrite
1957
1958 destwin: object(type="PyCursesWindowObject *", subclass_of="&PyCursesWindow_Type")
1959
1960 [
1961 sminrow: int
1962 smincol: int
1963 dminrow: int
1964 dmincol: int
1965 dmaxrow: int
1966 dmaxcol: int
1967 ]
1968 /
1969
1970Overwrite the window on top of destwin.
1971
1972The windows need not be the same size, in which case only the overlapping
1973region is copied. This copy is destructive, which means that the current
1974background character overwrites the old contents of destwin.
1975
1976To get fine-grained control over the copied region, the second form of
1977overwrite() can be used. sminrow and smincol are the upper-left coordinates
1978of the source window, the other variables mark a rectangle in the destination
1979window.
1980[clinic start generated code]*/
1981
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001982static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001983_curses_window_overwrite_impl(PyCursesWindowObject *self,
1984 PyCursesWindowObject *destwin,
1985 int group_right_1, int sminrow, int smincol,
1986 int dminrow, int dmincol, int dmaxrow,
1987 int dmaxcol)
1988/*[clinic end generated code: output=12ae007d1681be28 input=ea5de1b35cd948e0]*/
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001989{
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001990 int rtn;
Victor Stinner26486ea2010-05-15 22:23:53 +00001991
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001992 if (group_right_1) {
1993 rtn = copywin(self->win, destwin->win, sminrow, smincol,
Victor Stinner26486ea2010-05-15 22:23:53 +00001994 dminrow, dmincol, dmaxrow, dmaxcol, FALSE);
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00001995 return PyCursesCheckERR(rtn, "copywin");
1996 }
1997 else {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03001998 rtn = overwrite(self->win, destwin->win);
Victor Stinner26486ea2010-05-15 22:23:53 +00001999 return PyCursesCheckERR(rtn, "overwrite");
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00002000 }
2001}
2002
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002003/*[clinic input]
2004_curses.window.putwin
2005
2006 file: object
2007 /
2008
2009Write all data associated with the window into the provided file object.
2010
2011This information can be later retrieved using the getwin() function.
2012[clinic start generated code]*/
2013
Andrew M. Kuchling9de6ffa2000-12-21 16:22:22 +00002014static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002015_curses_window_putwin(PyCursesWindowObject *self, PyObject *file)
2016/*[clinic end generated code: output=3a25e2a5e7a040ac input=0608648e09c8ea0a]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002017{
Victor Stinner26486ea2010-05-15 22:23:53 +00002018 /* We have to simulate this by writing to a temporary FILE*,
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002019 then reading back, then writing to the argument file. */
Christian Heimes2b221b72017-03-02 11:09:01 +01002020 FILE *fp;
Victor Stinnerdaf45552013-08-28 00:53:59 +02002021 PyObject *res = NULL;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002022
Christian Heimes2b221b72017-03-02 11:09:01 +01002023 fp = tmpfile();
2024 if (fp == NULL)
2025 return PyErr_SetFromErrno(PyExc_OSError);
2026 if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02002027 goto exit;
Victor Stinner26486ea2010-05-15 22:23:53 +00002028 res = PyCursesCheckERR(putwin(self->win, fp), "putwin");
Victor Stinnerdaf45552013-08-28 00:53:59 +02002029 if (res == NULL)
2030 goto exit;
Victor Stinner26486ea2010-05-15 22:23:53 +00002031 fseek(fp, 0, 0);
2032 while (1) {
2033 char buf[BUFSIZ];
2034 Py_ssize_t n = fread(buf, 1, BUFSIZ, fp);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002035 _Py_IDENTIFIER(write);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002036
Victor Stinner26486ea2010-05-15 22:23:53 +00002037 if (n <= 0)
2038 break;
2039 Py_DECREF(res);
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002040 res = _PyObject_CallMethodId(file, &PyId_write, "y#", buf, n);
Victor Stinner26486ea2010-05-15 22:23:53 +00002041 if (res == NULL)
2042 break;
2043 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02002044
2045exit:
Christian Heimes2b221b72017-03-02 11:09:01 +01002046 fclose(fp);
Guido van Rossum6c95da32007-07-24 00:16:38 +00002047 return res;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002048}
2049
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002050/*[clinic input]
2051_curses.window.redrawln
2052
2053 beg: int
2054 Starting line number.
2055 num: int
2056 The number of lines.
2057 /
2058
2059Mark the specified lines corrupted.
2060
2061They should be completely redrawn on the next refresh() call.
2062[clinic start generated code]*/
2063
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002064static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002065_curses_window_redrawln_impl(PyCursesWindowObject *self, int beg, int num)
2066/*[clinic end generated code: output=ea216e334f9ce1b4 input=152155e258a77a7a]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002067{
Victor Stinner26486ea2010-05-15 22:23:53 +00002068 return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002069}
2070
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002071/*[clinic input]
2072_curses.window.refresh
2073
2074 [
2075 pminrow: int
2076 pmincol: int
2077 sminrow: int
2078 smincol: int
2079 smaxrow: int
2080 smaxcol: int
2081 ]
2082 /
2083
2084Update the display immediately.
2085
2086Synchronize actual screen with previous drawing/deleting methods.
2087The 6 optional arguments can only be specified when the window is a pad
2088created with newpad(). The additional parameters are needed to indicate
2089what part of the pad and screen are involved. pminrow and pmincol specify
2090the upper left-hand corner of the rectangle to be displayed in the pad.
2091sminrow, smincol, smaxrow, and smaxcol specify the edges of the rectangle to
2092be displayed on the screen. The lower right-hand corner of the rectangle to
2093be displayed in the pad is calculated from the screen coordinates, since the
2094rectangles must be the same size. Both rectangles must be entirely contained
2095within their respective structures. Negative values of pminrow, pmincol,
2096sminrow, or smincol are treated as if they were zero.
2097[clinic start generated code]*/
2098
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002099static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002100_curses_window_refresh_impl(PyCursesWindowObject *self, int group_right_1,
2101 int pminrow, int pmincol, int sminrow,
2102 int smincol, int smaxrow, int smaxcol)
2103/*[clinic end generated code: output=42199543115e6e63 input=95e01cb5ffc635d0]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002104{
Victor Stinner26486ea2010-05-15 22:23:53 +00002105 int rtn;
Andrew M. Kuchlingf4195722000-06-23 01:36:21 +00002106
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002107#ifdef py_is_pad
2108 if (py_is_pad(self->win)) {
2109 if (!group_right_1) {
2110 PyErr_SetString(PyCursesError,
2111 "refresh() for a pad requires 6 arguments");
2112 return NULL;
Victor Stinner26486ea2010-05-15 22:23:53 +00002113 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002114 Py_BEGIN_ALLOW_THREADS
2115 rtn = prefresh(self->win, pminrow, pmincol,
2116 sminrow, smincol, smaxrow, smaxcol);
2117 Py_END_ALLOW_THREADS
2118 return PyCursesCheckERR(rtn, "prefresh");
2119 }
2120#endif
2121 if (group_right_1) {
2122 PyErr_SetString(PyExc_TypeError,
2123 "refresh() takes no arguments (6 given)");
Victor Stinner26486ea2010-05-15 22:23:53 +00002124 return NULL;
2125 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002126 Py_BEGIN_ALLOW_THREADS
2127 rtn = wrefresh(self->win);
2128 Py_END_ALLOW_THREADS
2129 return PyCursesCheckERR(rtn, "prefresh");
2130}
2131
2132/*[clinic input]
2133_curses.window.setscrreg
2134
2135 top: int
2136 First line number.
2137 bottom: int
2138 Last line number.
2139 /
2140
2141Define a software scrolling region.
2142
2143All scrolling actions will take place in this region.
2144[clinic start generated code]*/
2145
2146static PyObject *
2147_curses_window_setscrreg_impl(PyCursesWindowObject *self, int top,
2148 int bottom)
2149/*[clinic end generated code: output=486ab5db218d2b1a input=1b517b986838bf0e]*/
2150{
2151 return PyCursesCheckERR(wsetscrreg(self->win, top, bottom), "wsetscrreg");
2152}
2153
2154/*[clinic input]
2155_curses.window.subwin
2156
2157 [
2158 nlines: int = 0
2159 Height.
2160 ncols: int = 0
2161 Width.
2162 ]
2163 begin_y: int
2164 Top side y-coordinate.
2165 begin_x: int
2166 Left side x-coordinate.
2167 /
2168
2169Create a sub-window (screen-relative coordinates).
2170
2171By default, the sub-window will extend from the specified position to the
2172lower right corner of the window.
2173[clinic start generated code]*/
2174
2175static PyObject *
2176_curses_window_subwin_impl(PyCursesWindowObject *self, int group_left_1,
2177 int nlines, int ncols, int begin_y, int begin_x)
2178/*[clinic end generated code: output=93e898afc348f59a input=2129fa47fd57721c]*/
2179{
2180 WINDOW *win;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002181
Victor Stinner26486ea2010-05-15 22:23:53 +00002182 /* printf("Subwin: %i %i %i %i \n", nlines, ncols, begin_y, begin_x); */
Masayuki Yamamoto8bc7d632017-11-01 21:05:26 +09002183#ifdef py_is_pad
2184 if (py_is_pad(self->win)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002185 win = subpad(self->win, nlines, ncols, begin_y, begin_x);
Masayuki Yamamoto8bc7d632017-11-01 21:05:26 +09002186 }
Victor Stinner26486ea2010-05-15 22:23:53 +00002187 else
Andrew M. Kuchling2d339f92001-01-29 20:47:59 +00002188#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00002189 win = subwin(self->win, nlines, ncols, begin_y, begin_x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002190
Victor Stinner26486ea2010-05-15 22:23:53 +00002191 if (win == NULL) {
2192 PyErr_SetString(PyCursesError, catchall_NULL);
2193 return NULL;
2194 }
2195
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002196 return (PyObject *)PyCursesWindow_New(win, self->encoding);
Guido van Rossumf6971e21994-08-30 12:25:20 +00002197}
2198
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002199/*[clinic input]
2200_curses.window.scroll
2201
2202 [
2203 lines: int = 1
2204 Number of lines to scroll.
2205 ]
2206 /
2207
2208Scroll the screen or scrolling region.
2209
2210Scroll upward if the argument is positive and downward if it is negative.
2211[clinic start generated code]*/
2212
Guido van Rossumf6971e21994-08-30 12:25:20 +00002213static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002214_curses_window_scroll_impl(PyCursesWindowObject *self, int group_right_1,
2215 int lines)
2216/*[clinic end generated code: output=4541a8a11852d360 input=c969ca0cfabbdbec]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00002217{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002218 if (!group_right_1) {
Victor Stinner26486ea2010-05-15 22:23:53 +00002219 return PyCursesCheckERR(scroll(self->win), "scroll");
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002220 }
2221 else {
2222 return PyCursesCheckERR(wscrl(self->win, lines), "scroll");
Victor Stinner26486ea2010-05-15 22:23:53 +00002223 }
Guido van Rossumf6971e21994-08-30 12:25:20 +00002224}
2225
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002226/*[clinic input]
2227_curses.window.touchline
2228
2229 start: int
2230 count: int
2231 [
2232 changed: bool(accept={int}) = True
2233 ]
2234 /
2235
2236Pretend count lines have been changed, starting with line start.
2237
2238If changed is supplied, it specifies whether the affected lines are marked
2239as having been changed (changed=True) or unchanged (changed=False).
2240[clinic start generated code]*/
2241
Guido van Rossumf6971e21994-08-30 12:25:20 +00002242static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002243_curses_window_touchline_impl(PyCursesWindowObject *self, int start,
2244 int count, int group_right_1, int changed)
2245/*[clinic end generated code: output=65d05b3f7438c61d input=918ad1cbdadf93ea]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00002246{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002247 if (!group_right_1) {
2248 return PyCursesCheckERR(touchline(self->win, start, count), "touchline");
2249 }
2250 else {
2251 return PyCursesCheckERR(wtouchln(self->win, start, count, changed), "touchline");
Victor Stinner26486ea2010-05-15 22:23:53 +00002252 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002253}
2254
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002255/*[clinic input]
2256_curses.window.vline
2257
2258 [
2259 y: int
2260 Starting Y-coordinate.
2261 x: int
2262 Starting X-coordinate.
2263 ]
2264
2265 ch: object
2266 Character to draw.
2267 n: int
2268 Line length.
2269
2270 [
2271 attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
2272 Attributes for the character.
2273 ]
2274 /
2275
2276Display a vertical line.
2277[clinic start generated code]*/
2278
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002279static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002280_curses_window_vline_impl(PyCursesWindowObject *self, int group_left_1,
2281 int y, int x, PyObject *ch, int n,
2282 int group_right_1, long attr)
2283/*[clinic end generated code: output=287ad1cc8982217f input=a6f2dc86a4648b32]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002284{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002285 chtype ch_;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002286
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002287 if (!PyCurses_ConvertToChtype(self, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00002288 return NULL;
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002289 if (group_left_1) {
2290 if (wmove(self->win, y, x) == ERR)
2291 return PyCursesCheckERR(ERR, "wmove");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002292 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002293 return PyCursesCheckERR(wvline(self->win, ch_ | (attr_t)attr, n), "vline");
Guido van Rossumf6971e21994-08-30 12:25:20 +00002294}
2295
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002296static PyObject *
2297PyCursesWindow_get_encoding(PyCursesWindowObject *self, void *closure)
2298{
2299 return PyUnicode_FromString(self->encoding);
2300}
2301
2302static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02002303PyCursesWindow_set_encoding(PyCursesWindowObject *self, PyObject *value, void *Py_UNUSED(ignored))
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002304{
2305 PyObject *ascii;
2306 char *encoding;
2307
2308 /* It is illegal to del win.encoding */
2309 if (value == NULL) {
2310 PyErr_SetString(PyExc_TypeError,
2311 "encoding may not be deleted");
2312 return -1;
2313 }
2314
2315 if (!PyUnicode_Check(value)) {
2316 PyErr_SetString(PyExc_TypeError,
2317 "setting encoding to a non-string");
2318 return -1;
2319 }
2320 ascii = PyUnicode_AsASCIIString(value);
2321 if (ascii == NULL)
2322 return -1;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02002323 encoding = _PyMem_Strdup(PyBytes_AS_STRING(ascii));
Ross Lagerwall2dabaf62012-09-07 08:34:23 +02002324 Py_DECREF(ascii);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002325 if (encoding == NULL) {
2326 PyErr_NoMemory();
2327 return -1;
2328 }
Victor Stinner49fc8ec2013-07-07 23:30:24 +02002329 PyMem_Free(self->encoding);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002330 self->encoding = encoding;
2331 return 0;
2332}
2333
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002334#include "clinic/_cursesmodule.c.h"
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002335
Guido van Rossumf6971e21994-08-30 12:25:20 +00002336static PyMethodDef PyCursesWindow_Methods[] = {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002337 _CURSES_WINDOW_ADDCH_METHODDEF
2338 _CURSES_WINDOW_ADDNSTR_METHODDEF
2339 _CURSES_WINDOW_ADDSTR_METHODDEF
2340 _CURSES_WINDOW_ATTROFF_METHODDEF
2341 _CURSES_WINDOW_ATTRON_METHODDEF
2342 _CURSES_WINDOW_ATTRSET_METHODDEF
2343 _CURSES_WINDOW_BKGD_METHODDEF
Serhiy Storchaka894ebd02017-11-01 14:34:20 +02002344#ifdef HAVE_CURSES_WCHGAT
Victor Stinner26486ea2010-05-15 22:23:53 +00002345 {"chgat", (PyCFunction)PyCursesWindow_ChgAt, METH_VARARGS},
Serhiy Storchaka894ebd02017-11-01 14:34:20 +02002346#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002347 _CURSES_WINDOW_BKGDSET_METHODDEF
2348 _CURSES_WINDOW_BORDER_METHODDEF
2349 _CURSES_WINDOW_BOX_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002350 {"clear", (PyCFunction)PyCursesWindow_wclear, METH_NOARGS},
2351 {"clearok", (PyCFunction)PyCursesWindow_clearok, METH_VARARGS},
2352 {"clrtobot", (PyCFunction)PyCursesWindow_wclrtobot, METH_NOARGS},
2353 {"clrtoeol", (PyCFunction)PyCursesWindow_wclrtoeol, METH_NOARGS},
2354 {"cursyncup", (PyCFunction)PyCursesWindow_wcursyncup, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002355 _CURSES_WINDOW_DELCH_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002356 {"deleteln", (PyCFunction)PyCursesWindow_wdeleteln, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002357 _CURSES_WINDOW_DERWIN_METHODDEF
2358 _CURSES_WINDOW_ECHOCHAR_METHODDEF
2359 _CURSES_WINDOW_ENCLOSE_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002360 {"erase", (PyCFunction)PyCursesWindow_werase, METH_NOARGS},
2361 {"getbegyx", (PyCFunction)PyCursesWindow_getbegyx, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002362 _CURSES_WINDOW_GETBKGD_METHODDEF
2363 _CURSES_WINDOW_GETCH_METHODDEF
2364 _CURSES_WINDOW_GETKEY_METHODDEF
2365 _CURSES_WINDOW_GET_WCH_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002366 {"getmaxyx", (PyCFunction)PyCursesWindow_getmaxyx, METH_NOARGS},
2367 {"getparyx", (PyCFunction)PyCursesWindow_getparyx, METH_NOARGS},
2368 {"getstr", (PyCFunction)PyCursesWindow_GetStr, METH_VARARGS},
2369 {"getyx", (PyCFunction)PyCursesWindow_getyx, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002370 _CURSES_WINDOW_HLINE_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002371 {"idcok", (PyCFunction)PyCursesWindow_idcok, METH_VARARGS},
2372 {"idlok", (PyCFunction)PyCursesWindow_idlok, METH_VARARGS},
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002373#ifdef HAVE_CURSES_IMMEDOK
Victor Stinner26486ea2010-05-15 22:23:53 +00002374 {"immedok", (PyCFunction)PyCursesWindow_immedok, METH_VARARGS},
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002375#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002376 _CURSES_WINDOW_INCH_METHODDEF
2377 _CURSES_WINDOW_INSCH_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002378 {"insdelln", (PyCFunction)PyCursesWindow_winsdelln, METH_VARARGS},
2379 {"insertln", (PyCFunction)PyCursesWindow_winsertln, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002380 _CURSES_WINDOW_INSNSTR_METHODDEF
2381 _CURSES_WINDOW_INSSTR_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002382 {"instr", (PyCFunction)PyCursesWindow_InStr, METH_VARARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002383 _CURSES_WINDOW_IS_LINETOUCHED_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002384 {"is_wintouched", (PyCFunction)PyCursesWindow_is_wintouched, METH_NOARGS},
2385 {"keypad", (PyCFunction)PyCursesWindow_keypad, METH_VARARGS},
2386 {"leaveok", (PyCFunction)PyCursesWindow_leaveok, METH_VARARGS},
2387 {"move", (PyCFunction)PyCursesWindow_wmove, METH_VARARGS},
2388 {"mvderwin", (PyCFunction)PyCursesWindow_mvderwin, METH_VARARGS},
2389 {"mvwin", (PyCFunction)PyCursesWindow_mvwin, METH_VARARGS},
2390 {"nodelay", (PyCFunction)PyCursesWindow_nodelay, METH_VARARGS},
2391 {"notimeout", (PyCFunction)PyCursesWindow_notimeout, METH_VARARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002392 _CURSES_WINDOW_NOUTREFRESH_METHODDEF
2393 _CURSES_WINDOW_OVERLAY_METHODDEF
2394 _CURSES_WINDOW_OVERWRITE_METHODDEF
2395 _CURSES_WINDOW_PUTWIN_METHODDEF
2396 _CURSES_WINDOW_REDRAWLN_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002397 {"redrawwin", (PyCFunction)PyCursesWindow_redrawwin, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002398 _CURSES_WINDOW_REFRESH_METHODDEF
Andrew M. Kuchlinga1e4b052000-06-27 21:49:47 +00002399#ifndef STRICT_SYSV_CURSES
Victor Stinner26486ea2010-05-15 22:23:53 +00002400 {"resize", (PyCFunction)PyCursesWindow_wresize, METH_VARARGS},
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002401#endif
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002402 _CURSES_WINDOW_SCROLL_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002403 {"scrollok", (PyCFunction)PyCursesWindow_scrollok, METH_VARARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002404 _CURSES_WINDOW_SETSCRREG_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002405 {"standend", (PyCFunction)PyCursesWindow_wstandend, METH_NOARGS},
2406 {"standout", (PyCFunction)PyCursesWindow_wstandout, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002407 {"subpad", (PyCFunction)_curses_window_subwin, METH_VARARGS, _curses_window_subwin__doc__},
2408 _CURSES_WINDOW_SUBWIN_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002409 {"syncdown", (PyCFunction)PyCursesWindow_wsyncdown, METH_NOARGS},
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002410#ifdef HAVE_CURSES_SYNCOK
Victor Stinner26486ea2010-05-15 22:23:53 +00002411 {"syncok", (PyCFunction)PyCursesWindow_syncok, METH_VARARGS},
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002412#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00002413 {"syncup", (PyCFunction)PyCursesWindow_wsyncup, METH_NOARGS},
2414 {"timeout", (PyCFunction)PyCursesWindow_wtimeout, METH_VARARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002415 _CURSES_WINDOW_TOUCHLINE_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002416 {"touchwin", (PyCFunction)PyCursesWindow_touchwin, METH_NOARGS},
2417 {"untouchwin", (PyCFunction)PyCursesWindow_untouchwin, METH_NOARGS},
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002418 _CURSES_WINDOW_VLINE_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00002419 {NULL, NULL} /* sentinel */
Guido van Rossumf6971e21994-08-30 12:25:20 +00002420};
2421
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002422static PyGetSetDef PyCursesWindow_getsets[] = {
2423 {"encoding",
2424 (getter)PyCursesWindow_get_encoding,
2425 (setter)PyCursesWindow_set_encoding,
Ronald Oussoren2a7fe032012-01-17 16:53:20 +01002426 "the typecode character used to create the array"},
2427 {NULL, NULL, NULL, NULL } /* sentinel */
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002428};
2429
Guido van Rossumf6971e21994-08-30 12:25:20 +00002430/* -------------------------------------------------------*/
Guido van Rossum85738471995-02-17 13:50:17 +00002431
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002432PyTypeObject PyCursesWindow_Type = {
Victor Stinner26486ea2010-05-15 22:23:53 +00002433 PyVarObject_HEAD_INIT(NULL, 0)
Victor Stinner61e2bc72017-02-12 23:42:02 +01002434 "_curses.window", /*tp_name*/
Victor Stinner26486ea2010-05-15 22:23:53 +00002435 sizeof(PyCursesWindowObject), /*tp_basicsize*/
2436 0, /*tp_itemsize*/
2437 /* methods */
2438 (destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002439 0, /*tp_vectorcall_offset*/
Victor Stinner26486ea2010-05-15 22:23:53 +00002440 (getattrfunc)0, /*tp_getattr*/
2441 (setattrfunc)0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002442 0, /*tp_as_async*/
Victor Stinner26486ea2010-05-15 22:23:53 +00002443 0, /*tp_repr*/
2444 0, /*tp_as_number*/
2445 0, /*tp_as_sequence*/
2446 0, /*tp_as_mapping*/
2447 0, /*tp_hash*/
2448 0, /*tp_call*/
2449 0, /*tp_str*/
2450 0, /*tp_getattro*/
2451 0, /*tp_setattro*/
2452 0, /*tp_as_buffer*/
2453 Py_TPFLAGS_DEFAULT, /*tp_flags*/
2454 0, /*tp_doc*/
2455 0, /*tp_traverse*/
2456 0, /*tp_clear*/
2457 0, /*tp_richcompare*/
2458 0, /*tp_weaklistoffset*/
2459 0, /*tp_iter*/
2460 0, /*tp_iternext*/
2461 PyCursesWindow_Methods, /*tp_methods*/
Victor Stinner0fdfceb2011-11-25 22:10:02 +01002462 0, /* tp_members */
2463 PyCursesWindow_getsets, /* tp_getset */
Guido van Rossumf6971e21994-08-30 12:25:20 +00002464};
2465
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002466/* Function Prototype Macros - They are ugly but very, very useful. ;-)
2467
2468 X - function name
2469 TYPE - parameter Type
2470 ERGSTR - format string for construction of the return value
2471 PARSESTR - format string for argument parsing
2472 */
2473
2474#define NoArgNoReturnFunctionBody(X) \
2475{ \
2476 PyCursesInitialised \
2477 return PyCursesCheckERR(X(), # X); }
2478
2479#define NoArgOrFlagNoReturnFunctionBody(X, flag) \
2480{ \
2481 PyCursesInitialised \
2482 if (flag) \
2483 return PyCursesCheckERR(X(), # X); \
2484 else \
2485 return PyCursesCheckERR(no ## X(), # X); \
2486}
2487
2488#define NoArgReturnIntFunctionBody(X) \
2489{ \
2490 PyCursesInitialised \
2491 return PyLong_FromLong((long) X()); }
2492
2493
2494#define NoArgReturnStringFunctionBody(X) \
2495{ \
2496 PyCursesInitialised \
2497 return PyBytes_FromString(X()); }
2498
2499#define NoArgTrueFalseFunctionBody(X) \
2500{ \
2501 PyCursesInitialised \
2502 return PyBool_FromLong(X()); }
2503
2504#define NoArgNoReturnVoidFunctionBody(X) \
2505{ \
2506 PyCursesInitialised \
2507 X(); \
2508 Py_RETURN_NONE; }
2509
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002510/*********************************************************************
2511 Global Functions
2512**********************************************************************/
Guido van Rossumf6971e21994-08-30 12:25:20 +00002513
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002514#ifdef HAVE_CURSES_FILTER
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002515/*[clinic input]
2516_curses.filter
2517
2518[clinic start generated code]*/
2519
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002520static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002521_curses_filter_impl(PyObject *module)
2522/*[clinic end generated code: output=fb5b8a3642eb70b5 input=668c75a6992d3624]*/
Christian Heimesaf98da12008-01-27 15:18:18 +00002523{
Victor Stinner26486ea2010-05-15 22:23:53 +00002524 /* not checking for PyCursesInitialised here since filter() must
2525 be called before initscr() */
2526 filter();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002527 Py_RETURN_NONE;
Christian Heimesaf98da12008-01-27 15:18:18 +00002528}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002529#endif
Christian Heimesaf98da12008-01-27 15:18:18 +00002530
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002531/*[clinic input]
2532_curses.baudrate
2533
2534Return the output speed of the terminal in bits per second.
2535[clinic start generated code]*/
2536
Christian Heimesaf98da12008-01-27 15:18:18 +00002537static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002538_curses_baudrate_impl(PyObject *module)
2539/*[clinic end generated code: output=3c63c6c401d7d9c0 input=921f022ed04a0fd9]*/
2540NoArgReturnIntFunctionBody(baudrate)
2541
2542/*[clinic input]
2543_curses.beep
2544
2545Emit a short attention sound.
2546[clinic start generated code]*/
2547
2548static PyObject *
2549_curses_beep_impl(PyObject *module)
2550/*[clinic end generated code: output=425274962abe49a2 input=a35698ca7d0162bc]*/
2551NoArgNoReturnFunctionBody(beep)
2552
2553/*[clinic input]
2554_curses.can_change_color
2555
2556Return True if the programmer can change the colors displayed by the terminal.
2557[clinic start generated code]*/
2558
2559static PyObject *
2560_curses_can_change_color_impl(PyObject *module)
2561/*[clinic end generated code: output=359df8c3c77d8bf1 input=d7718884de0092f2]*/
2562NoArgTrueFalseFunctionBody(can_change_color)
2563
2564/*[clinic input]
2565_curses.cbreak
2566
2567 flag: bool(accept={int}) = True
2568 If false, the effect is the same as calling nocbreak().
2569 /
2570
2571Enter cbreak mode.
2572
2573In cbreak mode (sometimes called "rare" mode) normal tty line buffering is
2574turned off and characters are available to be read one by one. However,
2575unlike raw mode, special characters (interrupt, quit, suspend, and flow
2576control) retain their effects on the tty driver and calling program.
2577Calling first raw() then cbreak() leaves the terminal in cbreak mode.
2578[clinic start generated code]*/
2579
2580static PyObject *
2581_curses_cbreak_impl(PyObject *module, int flag)
2582/*[clinic end generated code: output=9f9dee9664769751 input=150be619eb1f1458]*/
2583NoArgOrFlagNoReturnFunctionBody(cbreak, flag)
2584
2585/*[clinic input]
2586_curses.color_content
2587
2588 color_number: short
2589 The number of the color (0 - COLORS).
2590 /
2591
2592Return the red, green, and blue (RGB) components of the specified color.
2593
2594A 3-tuple is returned, containing the R, G, B values for the given color,
2595which will be between 0 (no component) and 1000 (maximum amount of component).
2596[clinic start generated code]*/
2597
2598static PyObject *
2599_curses_color_content_impl(PyObject *module, short color_number)
2600/*[clinic end generated code: output=cb15cf3120d4bfc1 input=5555abb1c11e11b7]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002601{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002602 short r,g,b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002603
Victor Stinner26486ea2010-05-15 22:23:53 +00002604 PyCursesInitialised;
2605 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002606
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002607 if (color_content(color_number, &r, &g, &b) != ERR)
Victor Stinner26486ea2010-05-15 22:23:53 +00002608 return Py_BuildValue("(iii)", r, g, b);
2609 else {
2610 PyErr_SetString(PyCursesError,
2611 "Argument 1 was out of range. Check value of COLORS.");
2612 return NULL;
2613 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002614}
2615
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002616/*[clinic input]
2617_curses.color_pair
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002618
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002619 color_number: short
2620 The number of the color (0 - COLORS).
2621 /
2622
2623Return the attribute value for displaying text in the specified color.
2624
2625This attribute value can be combined with A_STANDOUT, A_REVERSE, and the
2626other A_* attributes. pair_number() is the counterpart to this function.
2627[clinic start generated code]*/
2628
2629static PyObject *
2630_curses_color_pair_impl(PyObject *module, short color_number)
2631/*[clinic end generated code: output=6a84cb6b29ecaf9a input=a9d3eb6f50e4dc12]*/
2632{
Victor Stinner26486ea2010-05-15 22:23:53 +00002633 PyCursesInitialised;
2634 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002635
Victor Stinner077af8c2019-08-14 12:31:43 +02002636 return PyLong_FromLong(color_pair_to_attr(color_number));
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002637}
2638
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002639/*[clinic input]
2640_curses.curs_set
2641
2642 visibility: int
2643 0 for invisible, 1 for normal visible, or 2 for very visible.
2644 /
2645
2646Set the cursor state.
2647
2648If the terminal supports the visibility requested, the previous cursor
2649state is returned; otherwise, an exception is raised. On many terminals,
2650the "visible" mode is an underline cursor and the "very visible" mode is
2651a block cursor.
2652[clinic start generated code]*/
2653
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002654static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002655_curses_curs_set_impl(PyObject *module, int visibility)
2656/*[clinic end generated code: output=ee8e62483b1d6cd4 input=81a7924a65d29504]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002657{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002658 int erg;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002659
Victor Stinner26486ea2010-05-15 22:23:53 +00002660 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002661
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002662 erg = curs_set(visibility);
Victor Stinner26486ea2010-05-15 22:23:53 +00002663 if (erg == ERR) return PyCursesCheckERR(erg, "curs_set");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002664
Victor Stinner26486ea2010-05-15 22:23:53 +00002665 return PyLong_FromLong((long) erg);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002666}
2667
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002668/*[clinic input]
2669_curses.def_prog_mode
2670
2671Save the current terminal mode as the "program" mode.
2672
2673The "program" mode is the mode when the running program is using curses.
2674
2675Subsequent calls to reset_prog_mode() will restore this mode.
2676[clinic start generated code]*/
2677
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002678static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002679_curses_def_prog_mode_impl(PyObject *module)
2680/*[clinic end generated code: output=05d5a351fff874aa input=768b9cace620dda5]*/
2681NoArgNoReturnFunctionBody(def_prog_mode)
2682
2683/*[clinic input]
2684_curses.def_shell_mode
2685
2686Save the current terminal mode as the "shell" mode.
2687
2688The "shell" mode is the mode when the running program is not using curses.
2689
2690Subsequent calls to reset_shell_mode() will restore this mode.
2691[clinic start generated code]*/
2692
2693static PyObject *
2694_curses_def_shell_mode_impl(PyObject *module)
2695/*[clinic end generated code: output=d6e42f5c768f860f input=5ead21f6f0baa894]*/
2696NoArgNoReturnFunctionBody(def_shell_mode)
2697
2698/*[clinic input]
2699_curses.delay_output
2700
2701 ms: int
2702 Duration in milliseconds.
2703 /
2704
2705Insert a pause in output.
2706[clinic start generated code]*/
2707
2708static PyObject *
2709_curses_delay_output_impl(PyObject *module, int ms)
2710/*[clinic end generated code: output=b6613a67f17fa4f4 input=5316457f5f59196c]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002711{
Victor Stinner26486ea2010-05-15 22:23:53 +00002712 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002713
Victor Stinner26486ea2010-05-15 22:23:53 +00002714 return PyCursesCheckERR(delay_output(ms), "delay_output");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002715}
2716
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002717/*[clinic input]
2718_curses.doupdate
2719
2720Update the physical screen to match the virtual screen.
2721[clinic start generated code]*/
2722
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002723static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002724_curses_doupdate_impl(PyObject *module)
2725/*[clinic end generated code: output=f34536975a75680c input=8da80914432a6489]*/
2726NoArgNoReturnFunctionBody(doupdate)
2727
2728/*[clinic input]
2729_curses.echo
2730
2731 flag: bool(accept={int}) = True
2732 If false, the effect is the same as calling noecho().
2733 /
2734
2735Enter echo mode.
2736
2737In echo mode, each character input is echoed to the screen as it is entered.
2738[clinic start generated code]*/
2739
2740static PyObject *
2741_curses_echo_impl(PyObject *module, int flag)
2742/*[clinic end generated code: output=03acb2ddfa6c8729 input=2e9e891d637eac5d]*/
2743NoArgOrFlagNoReturnFunctionBody(echo, flag)
2744
2745/*[clinic input]
2746_curses.endwin
2747
2748De-initialize the library, and return terminal to normal status.
2749[clinic start generated code]*/
2750
2751static PyObject *
2752_curses_endwin_impl(PyObject *module)
2753/*[clinic end generated code: output=c0150cd96d2f4128 input=e172cfa43062f3fa]*/
2754NoArgNoReturnFunctionBody(endwin)
2755
2756/*[clinic input]
2757_curses.erasechar
2758
2759Return the user's current erase character.
2760[clinic start generated code]*/
2761
2762static PyObject *
2763_curses_erasechar_impl(PyObject *module)
2764/*[clinic end generated code: output=3df305dc6b926b3f input=628c136c3c5758d3]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002765{
Victor Stinner26486ea2010-05-15 22:23:53 +00002766 char ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002767
Victor Stinner26486ea2010-05-15 22:23:53 +00002768 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002769
Victor Stinner26486ea2010-05-15 22:23:53 +00002770 ch = erasechar();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002771
Victor Stinner26486ea2010-05-15 22:23:53 +00002772 return PyBytes_FromStringAndSize(&ch, 1);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002773}
2774
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002775/*[clinic input]
2776_curses.flash
2777
2778Flash the screen.
2779
2780That is, change it to reverse-video and then change it back in a short interval.
2781[clinic start generated code]*/
2782
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002783static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002784_curses_flash_impl(PyObject *module)
2785/*[clinic end generated code: output=488b8a0ebd9ea9b8 input=02fdfb06c8fc3171]*/
2786NoArgNoReturnFunctionBody(flash)
2787
2788/*[clinic input]
2789_curses.flushinp
2790
2791Flush all input buffers.
2792
2793This throws away any typeahead that has been typed by the user and has not
2794yet been processed by the program.
2795[clinic start generated code]*/
2796
2797static PyObject *
2798_curses_flushinp_impl(PyObject *module)
2799/*[clinic end generated code: output=7e7a1fc1473960f5 input=59d042e705cef5ec]*/
2800NoArgNoReturnVoidFunctionBody(flushinp)
2801
2802#ifdef getsyx
2803/*[clinic input]
2804_curses.getsyx
2805
2806Return the current coordinates of the virtual screen cursor.
2807
2808Return a (y, x) tuple. If leaveok is currently true, return (-1, -1).
2809[clinic start generated code]*/
2810
2811static PyObject *
2812_curses_getsyx_impl(PyObject *module)
2813/*[clinic end generated code: output=c8e6c3f42349a038 input=9e1f862f3b4f7cba]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002814{
Victor Stinner26486ea2010-05-15 22:23:53 +00002815 int x = 0;
2816 int y = 0;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002817
Victor Stinner26486ea2010-05-15 22:23:53 +00002818 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002819
Victor Stinner26486ea2010-05-15 22:23:53 +00002820 getsyx(y, x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002821
Victor Stinner26486ea2010-05-15 22:23:53 +00002822 return Py_BuildValue("(ii)", y, x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002823}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02002824#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002825
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002826#ifdef NCURSES_MOUSE_VERSION
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002827/*[clinic input]
2828_curses.getmouse
2829
2830Retrieve the queued mouse event.
2831
2832After getch() returns KEY_MOUSE to signal a mouse event, this function
2833returns a 5-tuple (id, x, y, z, bstate).
2834[clinic start generated code]*/
2835
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002836static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002837_curses_getmouse_impl(PyObject *module)
2838/*[clinic end generated code: output=ccf4242546b9cfa8 input=5b756ee6f5b481b1]*/
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002839{
Victor Stinner26486ea2010-05-15 22:23:53 +00002840 int rtn;
2841 MEVENT event;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002842
Victor Stinner26486ea2010-05-15 22:23:53 +00002843 PyCursesInitialised;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002844
Victor Stinner26486ea2010-05-15 22:23:53 +00002845 rtn = getmouse( &event );
2846 if (rtn == ERR) {
2847 PyErr_SetString(PyCursesError, "getmouse() returned ERR");
2848 return NULL;
2849 }
Serhiy Storchaka4f469c02017-11-01 20:48:49 +02002850 return Py_BuildValue("(hiiik)",
Victor Stinner26486ea2010-05-15 22:23:53 +00002851 (short)event.id,
Serhiy Storchaka4f469c02017-11-01 20:48:49 +02002852 (int)event.x, (int)event.y, (int)event.z,
2853 (unsigned long) event.bstate);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002854}
2855
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002856/*[clinic input]
2857_curses.ungetmouse
2858
2859 id: short
2860 x: int
2861 y: int
2862 z: int
2863 bstate: unsigned_long(bitwise=True)
2864 /
2865
2866Push a KEY_MOUSE event onto the input queue.
2867
2868The following getmouse() will return the given state data.
2869[clinic start generated code]*/
2870
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002871static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002872_curses_ungetmouse_impl(PyObject *module, short id, int x, int y, int z,
2873 unsigned long bstate)
2874/*[clinic end generated code: output=3430c9b0fc5c4341 input=fd650b2ca5a01e8f]*/
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002875{
Victor Stinner26486ea2010-05-15 22:23:53 +00002876 MEVENT event;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002877
Victor Stinner26486ea2010-05-15 22:23:53 +00002878 PyCursesInitialised;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002879
Serhiy Storchaka4f469c02017-11-01 20:48:49 +02002880 event.id = id;
2881 event.x = x;
2882 event.y = y;
2883 event.z = z;
2884 event.bstate = bstate;
Victor Stinner26486ea2010-05-15 22:23:53 +00002885 return PyCursesCheckERR(ungetmouse(&event), "ungetmouse");
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00002886}
2887#endif
2888
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002889/*[clinic input]
2890_curses.getwin
2891
2892 file: object
2893 /
2894
2895Read window related data stored in the file by an earlier putwin() call.
2896
2897The routine then creates and initializes a new window using that data,
2898returning the new window object.
2899[clinic start generated code]*/
2900
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002901static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002902_curses_getwin(PyObject *module, PyObject *file)
2903/*[clinic end generated code: output=a79e0df3379af756 input=f713d2bba0e4c929]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002904{
Christian Heimes2b221b72017-03-02 11:09:01 +01002905 FILE *fp;
Victor Stinner26486ea2010-05-15 22:23:53 +00002906 PyObject *data;
2907 size_t datalen;
2908 WINDOW *win;
Benjamin Peterson18b71912013-05-16 15:29:44 -05002909 _Py_IDENTIFIER(read);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002910 PyObject *res = NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002911
Victor Stinner26486ea2010-05-15 22:23:53 +00002912 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002913
Christian Heimes2b221b72017-03-02 11:09:01 +01002914 fp = tmpfile();
2915 if (fp == NULL)
2916 return PyErr_SetFromErrno(PyExc_OSError);
2917
2918 if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02002919 goto error;
Christian Heimes2b221b72017-03-02 11:09:01 +01002920
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002921 data = _PyObject_CallMethodIdNoArgs(file, &PyId_read);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002922 if (data == NULL)
2923 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00002924 if (!PyBytes_Check(data)) {
2925 PyErr_Format(PyExc_TypeError,
2926 "f.read() returned %.100s instead of bytes",
Victor Stinnerdaa97562020-02-07 03:37:06 +01002927 Py_TYPE(data)->tp_name);
Victor Stinner26486ea2010-05-15 22:23:53 +00002928 Py_DECREF(data);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002929 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00002930 }
2931 datalen = PyBytes_GET_SIZE(data);
2932 if (fwrite(PyBytes_AS_STRING(data), 1, datalen, fp) != datalen) {
2933 Py_DECREF(data);
Christian Heimes2b221b72017-03-02 11:09:01 +01002934 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002935 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00002936 }
Guido van Rossum6c95da32007-07-24 00:16:38 +00002937 Py_DECREF(data);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002938
Victor Stinner26486ea2010-05-15 22:23:53 +00002939 fseek(fp, 0, 0);
2940 win = getwin(fp);
Victor Stinner26486ea2010-05-15 22:23:53 +00002941 if (win == NULL) {
2942 PyErr_SetString(PyCursesError, catchall_NULL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002943 goto error;
Victor Stinner26486ea2010-05-15 22:23:53 +00002944 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02002945 res = PyCursesWindow_New(win, NULL);
2946
2947error:
Christian Heimes2b221b72017-03-02 11:09:01 +01002948 fclose(fp);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002949 return res;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002950}
2951
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002952/*[clinic input]
2953_curses.halfdelay
2954
2955 tenths: byte
2956 Maximal blocking delay in tenths of seconds (1 - 255).
2957 /
2958
2959Enter half-delay mode.
2960
2961Use nocbreak() to leave half-delay mode.
2962[clinic start generated code]*/
2963
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002964static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002965_curses_halfdelay_impl(PyObject *module, unsigned char tenths)
2966/*[clinic end generated code: output=e92cdf0ef33c0663 input=e42dce7259c15100]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002967{
Victor Stinner26486ea2010-05-15 22:23:53 +00002968 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002969
Victor Stinner26486ea2010-05-15 22:23:53 +00002970 return PyCursesCheckERR(halfdelay(tenths), "halfdelay");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002971}
2972
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002973/*[clinic input]
2974_curses.has_colors
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00002975
Serhiy Storchakab00854c2018-05-10 11:27:23 +03002976Return True if the terminal can display colors; otherwise, return False.
2977[clinic start generated code]*/
2978
2979static PyObject *
2980_curses_has_colors_impl(PyObject *module)
2981/*[clinic end generated code: output=db5667483139e3e2 input=b2ec41b739d896c6]*/
2982NoArgTrueFalseFunctionBody(has_colors)
2983
2984/*[clinic input]
2985_curses.has_ic
2986
2987Return True if the terminal has insert- and delete-character capabilities.
2988[clinic start generated code]*/
2989
2990static PyObject *
2991_curses_has_ic_impl(PyObject *module)
2992/*[clinic end generated code: output=6be24da9cb1268fe input=9bc2d3a797cc7324]*/
2993NoArgTrueFalseFunctionBody(has_ic)
2994
2995/*[clinic input]
2996_curses.has_il
2997
2998Return True if the terminal has insert- and delete-line capabilities.
2999[clinic start generated code]*/
3000
3001static PyObject *
3002_curses_has_il_impl(PyObject *module)
3003/*[clinic end generated code: output=d45bd7788ff9f5f4 input=cd939d5607ee5427]*/
3004NoArgTrueFalseFunctionBody(has_il)
3005
3006#ifdef HAVE_CURSES_HAS_KEY
3007/*[clinic input]
3008_curses.has_key
3009
3010 key: int
3011 Key number.
3012 /
3013
3014Return True if the current terminal type recognizes a key with that value.
3015[clinic start generated code]*/
3016
3017static PyObject *
3018_curses_has_key_impl(PyObject *module, int key)
3019/*[clinic end generated code: output=19ad48319414d0b1 input=78bd44acf1a4997c]*/
3020{
Victor Stinner26486ea2010-05-15 22:23:53 +00003021 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003022
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003023 return PyBool_FromLong(has_key(key));
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003024}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02003025#endif
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003026
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003027/*[clinic input]
3028_curses.init_color
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003029
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003030 color_number: short
3031 The number of the color to be changed (0 - COLORS).
3032 r: short
3033 Red component (0 - 1000).
3034 g: short
3035 Green component (0 - 1000).
3036 b: short
3037 Blue component (0 - 1000).
3038 /
3039
3040Change the definition of a color.
3041
3042When init_color() is used, all occurrences of that color on the screen
3043immediately change to the new definition. This function is a no-op on
3044most terminals; it is active only if can_change_color() returns 1.
3045[clinic start generated code]*/
3046
3047static PyObject *
3048_curses_init_color_impl(PyObject *module, short color_number, short r,
3049 short g, short b)
3050/*[clinic end generated code: output=280236f5efe9776a input=f3a05bd38f619175]*/
3051{
Victor Stinner26486ea2010-05-15 22:23:53 +00003052 PyCursesInitialised;
3053 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003054
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003055 return PyCursesCheckERR(init_color(color_number, r, g, b), "init_color");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003056}
3057
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003058/*[clinic input]
3059_curses.init_pair
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003060
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003061 pair_number: short
3062 The number of the color-pair to be changed (1 - (COLOR_PAIRS-1)).
3063 fg: short
3064 Foreground color number (0 - COLORS).
3065 bg: short
3066 Background color number (0 - COLORS).
3067 /
3068
3069Change the definition of a color-pair.
3070
3071If the color-pair was previously initialized, the screen is refreshed and
3072all occurrences of that color-pair are changed to the new definition.
3073[clinic start generated code]*/
3074
3075static PyObject *
3076_curses_init_pair_impl(PyObject *module, short pair_number, short fg,
3077 short bg)
3078/*[clinic end generated code: output=9c2ce39c22f376b6 input=c9f0b11b17a2ac6d]*/
3079{
Victor Stinner26486ea2010-05-15 22:23:53 +00003080 PyCursesInitialised;
3081 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003082
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003083 return PyCursesCheckERR(init_pair(pair_number, fg, bg), "init_pair");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003084}
3085
Andrew M. Kuchling32552682000-12-22 21:52:27 +00003086static PyObject *ModDict;
3087
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003088/*[clinic input]
3089_curses.initscr
3090
3091Initialize the library.
3092
3093Return a WindowObject which represents the whole screen.
3094[clinic start generated code]*/
3095
Victor Stinner26486ea2010-05-15 22:23:53 +00003096static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003097_curses_initscr_impl(PyObject *module)
3098/*[clinic end generated code: output=619fb68443810b7b input=514f4bce1821f6b5]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00003099{
Victor Stinner26486ea2010-05-15 22:23:53 +00003100 WINDOW *win;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003101 PyCursesWindowObject *winobj;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003102
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003103 if (initialised) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003104 wrefresh(stdscr);
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003105 return (PyObject *)PyCursesWindow_New(stdscr, NULL);
Victor Stinner26486ea2010-05-15 22:23:53 +00003106 }
Guido van Rossum56bf2351994-08-31 22:06:24 +00003107
Victor Stinner26486ea2010-05-15 22:23:53 +00003108 win = initscr();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003109
Victor Stinner26486ea2010-05-15 22:23:53 +00003110 if (win == NULL) {
3111 PyErr_SetString(PyCursesError, catchall_NULL);
3112 return NULL;
3113 }
Guido van Rossum85738471995-02-17 13:50:17 +00003114
Victor Stinner26486ea2010-05-15 22:23:53 +00003115 initialised = initialised_setupterm = TRUE;
Guido van Rossum56bf2351994-08-31 22:06:24 +00003116
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00003117/* This was moved from initcurses() because it core dumped on SGI,
3118 where they're not defined until you've called initscr() */
Victor Stinner26486ea2010-05-15 22:23:53 +00003119#define SetDictInt(string,ch) \
3120 do { \
3121 PyObject *o = PyLong_FromLong((long) (ch)); \
3122 if (o && PyDict_SetItemString(ModDict, string, o) == 0) { \
3123 Py_DECREF(o); \
3124 } \
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003125 } while (0)
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00003126
Victor Stinner26486ea2010-05-15 22:23:53 +00003127 /* Here are some graphic symbols you can use */
3128 SetDictInt("ACS_ULCORNER", (ACS_ULCORNER));
3129 SetDictInt("ACS_LLCORNER", (ACS_LLCORNER));
3130 SetDictInt("ACS_URCORNER", (ACS_URCORNER));
3131 SetDictInt("ACS_LRCORNER", (ACS_LRCORNER));
3132 SetDictInt("ACS_LTEE", (ACS_LTEE));
3133 SetDictInt("ACS_RTEE", (ACS_RTEE));
3134 SetDictInt("ACS_BTEE", (ACS_BTEE));
3135 SetDictInt("ACS_TTEE", (ACS_TTEE));
3136 SetDictInt("ACS_HLINE", (ACS_HLINE));
3137 SetDictInt("ACS_VLINE", (ACS_VLINE));
3138 SetDictInt("ACS_PLUS", (ACS_PLUS));
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00003139#if !defined(__hpux) || defined(HAVE_NCURSES_H)
Victor Stinner26486ea2010-05-15 22:23:53 +00003140 /* On HP/UX 11, these are of type cchar_t, which is not an
3141 integral type. If this is a problem on more platforms, a
3142 configure test should be added to determine whether ACS_S1
3143 is of integral type. */
3144 SetDictInt("ACS_S1", (ACS_S1));
3145 SetDictInt("ACS_S9", (ACS_S9));
3146 SetDictInt("ACS_DIAMOND", (ACS_DIAMOND));
3147 SetDictInt("ACS_CKBOARD", (ACS_CKBOARD));
3148 SetDictInt("ACS_DEGREE", (ACS_DEGREE));
3149 SetDictInt("ACS_PLMINUS", (ACS_PLMINUS));
3150 SetDictInt("ACS_BULLET", (ACS_BULLET));
3151 SetDictInt("ACS_LARROW", (ACS_LARROW));
3152 SetDictInt("ACS_RARROW", (ACS_RARROW));
3153 SetDictInt("ACS_DARROW", (ACS_DARROW));
3154 SetDictInt("ACS_UARROW", (ACS_UARROW));
3155 SetDictInt("ACS_BOARD", (ACS_BOARD));
3156 SetDictInt("ACS_LANTERN", (ACS_LANTERN));
3157 SetDictInt("ACS_BLOCK", (ACS_BLOCK));
Martin v. Löwiseb9b1032001-10-24 17:10:49 +00003158#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00003159 SetDictInt("ACS_BSSB", (ACS_ULCORNER));
3160 SetDictInt("ACS_SSBB", (ACS_LLCORNER));
3161 SetDictInt("ACS_BBSS", (ACS_URCORNER));
3162 SetDictInt("ACS_SBBS", (ACS_LRCORNER));
3163 SetDictInt("ACS_SBSS", (ACS_RTEE));
3164 SetDictInt("ACS_SSSB", (ACS_LTEE));
3165 SetDictInt("ACS_SSBS", (ACS_BTEE));
3166 SetDictInt("ACS_BSSS", (ACS_TTEE));
3167 SetDictInt("ACS_BSBS", (ACS_HLINE));
3168 SetDictInt("ACS_SBSB", (ACS_VLINE));
3169 SetDictInt("ACS_SSSS", (ACS_PLUS));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003170
Victor Stinner26486ea2010-05-15 22:23:53 +00003171 /* The following are never available with strict SYSV curses */
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003172#ifdef ACS_S3
Victor Stinner26486ea2010-05-15 22:23:53 +00003173 SetDictInt("ACS_S3", (ACS_S3));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003174#endif
3175#ifdef ACS_S7
Victor Stinner26486ea2010-05-15 22:23:53 +00003176 SetDictInt("ACS_S7", (ACS_S7));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003177#endif
3178#ifdef ACS_LEQUAL
Victor Stinner26486ea2010-05-15 22:23:53 +00003179 SetDictInt("ACS_LEQUAL", (ACS_LEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003180#endif
3181#ifdef ACS_GEQUAL
Victor Stinner26486ea2010-05-15 22:23:53 +00003182 SetDictInt("ACS_GEQUAL", (ACS_GEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003183#endif
3184#ifdef ACS_PI
Victor Stinner26486ea2010-05-15 22:23:53 +00003185 SetDictInt("ACS_PI", (ACS_PI));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003186#endif
3187#ifdef ACS_NEQUAL
Victor Stinner26486ea2010-05-15 22:23:53 +00003188 SetDictInt("ACS_NEQUAL", (ACS_NEQUAL));
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00003189#endif
3190#ifdef ACS_STERLING
Victor Stinner26486ea2010-05-15 22:23:53 +00003191 SetDictInt("ACS_STERLING", (ACS_STERLING));
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00003192#endif
3193
Victor Stinner26486ea2010-05-15 22:23:53 +00003194 SetDictInt("LINES", LINES);
3195 SetDictInt("COLS", COLS);
Guido van Rossum56bf2351994-08-31 22:06:24 +00003196
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003197 winobj = (PyCursesWindowObject *)PyCursesWindow_New(win, NULL);
3198 screen_encoding = winobj->encoding;
3199 return (PyObject *)winobj;
Guido van Rossumf6971e21994-08-30 12:25:20 +00003200}
3201
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003202/*[clinic input]
3203_curses.setupterm
3204
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003205 term: str(accept={str, NoneType}) = None
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003206 Terminal name.
3207 If omitted, the value of the TERM environment variable will be used.
3208 fd: int = -1
3209 File descriptor to which any initialization sequences will be sent.
3210 If not supplied, the file descriptor for sys.stdout will be used.
3211
3212Initialize the terminal.
3213[clinic start generated code]*/
3214
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003215static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003216_curses_setupterm_impl(PyObject *module, const char *term, int fd)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003217/*[clinic end generated code: output=4584e587350f2848 input=4511472766af0c12]*/
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003218{
Victor Stinner26486ea2010-05-15 22:23:53 +00003219 int err;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003220
Victor Stinner26486ea2010-05-15 22:23:53 +00003221 if (fd == -1) {
3222 PyObject* sys_stdout;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003223
Victor Stinnerbd303c12013-11-07 23:07:29 +01003224 sys_stdout = PySys_GetObject("stdout");
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003225
Victor Stinner26486ea2010-05-15 22:23:53 +00003226 if (sys_stdout == NULL || sys_stdout == Py_None) {
3227 PyErr_SetString(
3228 PyCursesError,
3229 "lost sys.stdout");
3230 return NULL;
3231 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003232
Victor Stinner26486ea2010-05-15 22:23:53 +00003233 fd = PyObject_AsFileDescriptor(sys_stdout);
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003234
Victor Stinner26486ea2010-05-15 22:23:53 +00003235 if (fd == -1) {
3236 return NULL;
3237 }
3238 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003239
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003240 if (!initialised_setupterm && setupterm((char *)term, fd, &err) == ERR) {
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02003241 const char* s = "setupterm: unknown error";
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003242
Victor Stinner26486ea2010-05-15 22:23:53 +00003243 if (err == 0) {
3244 s = "setupterm: could not find terminal";
3245 } else if (err == -1) {
3246 s = "setupterm: could not find terminfo database";
3247 }
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003248
Victor Stinner26486ea2010-05-15 22:23:53 +00003249 PyErr_SetString(PyCursesError,s);
3250 return NULL;
3251 }
3252
3253 initialised_setupterm = TRUE;
3254
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003255 Py_RETURN_NONE;
Andrew M. Kuchling26cc66f2000-11-18 17:45:59 +00003256}
Guido van Rossumf6971e21994-08-30 12:25:20 +00003257
Batuhan Taşkaya4991cf42020-03-03 05:00:10 +03003258#if defined(NCURSES_EXT_FUNCS) && NCURSES_EXT_FUNCS >= 20081102
3259// https://invisible-island.net/ncurses/NEWS.html#index-t20080119
3260
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003261/*[clinic input]
Anthony Sottileb32cb972019-10-31 02:13:48 -07003262_curses.get_escdelay
3263
3264Gets the curses ESCDELAY setting.
3265
3266Gets the number of milliseconds to wait after reading an escape character,
3267to distinguish between an individual escape character entered on the
3268keyboard from escape sequences sent by cursor and function keys.
3269[clinic start generated code]*/
3270
3271static PyObject *
3272_curses_get_escdelay_impl(PyObject *module)
3273/*[clinic end generated code: output=222fa1a822555d60 input=be2d5b3dd974d0a4]*/
3274{
3275 return PyLong_FromLong(ESCDELAY);
3276}
3277/*[clinic input]
3278_curses.set_escdelay
3279 ms: int
3280 length of the delay in milliseconds.
3281 /
3282
3283Sets the curses ESCDELAY setting.
3284
3285Sets the number of milliseconds to wait after reading an escape character,
3286to distinguish between an individual escape character entered on the
3287keyboard from escape sequences sent by cursor and function keys.
3288[clinic start generated code]*/
3289
3290static PyObject *
3291_curses_set_escdelay_impl(PyObject *module, int ms)
3292/*[clinic end generated code: output=43818efbf7980ac4 input=7796fe19f111e250]*/
3293{
3294 if (ms <= 0) {
3295 PyErr_SetString(PyExc_ValueError, "ms must be > 0");
3296 return NULL;
3297 }
3298
3299 return PyCursesCheckERR(set_escdelay(ms), "set_escdelay");
3300}
3301
3302/*[clinic input]
3303_curses.get_tabsize
3304
3305Gets the curses TABSIZE setting.
3306
3307Gets the number of columns used by the curses library when converting a tab
3308character to spaces as it adds the tab to a window.
3309[clinic start generated code]*/
3310
3311static PyObject *
3312_curses_get_tabsize_impl(PyObject *module)
3313/*[clinic end generated code: output=7e9e51fb6126fbdf input=74af86bf6c9f5d7e]*/
3314{
3315 return PyLong_FromLong(TABSIZE);
3316}
3317/*[clinic input]
3318_curses.set_tabsize
3319 size: int
3320 rendered cell width of a tab character.
3321 /
3322
3323Sets the curses TABSIZE setting.
3324
3325Sets the number of columns used by the curses library when converting a tab
3326character to spaces as it adds the tab to a window.
3327[clinic start generated code]*/
3328
3329static PyObject *
3330_curses_set_tabsize_impl(PyObject *module, int size)
3331/*[clinic end generated code: output=c1de5a76c0daab1e input=78cba6a3021ad061]*/
3332{
3333 if (size <= 0) {
3334 PyErr_SetString(PyExc_ValueError, "size must be > 0");
3335 return NULL;
3336 }
3337
3338 return PyCursesCheckERR(set_tabsize(size), "set_tabsize");
3339}
Batuhan Taşkaya4991cf42020-03-03 05:00:10 +03003340#endif
Anthony Sottileb32cb972019-10-31 02:13:48 -07003341
3342/*[clinic input]
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003343_curses.intrflush
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003344
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003345 flag: bool(accept={int})
3346 /
3347
3348[clinic start generated code]*/
3349
3350static PyObject *
3351_curses_intrflush_impl(PyObject *module, int flag)
3352/*[clinic end generated code: output=c1986df35e999a0f input=fcba57bb28dfd795]*/
3353{
Victor Stinner26486ea2010-05-15 22:23:53 +00003354 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003355
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003356 return PyCursesCheckERR(intrflush(NULL, flag), "intrflush");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003357}
3358
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003359/*[clinic input]
3360_curses.isendwin
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003361
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003362Return True if endwin() has been called.
3363[clinic start generated code]*/
3364
3365static PyObject *
3366_curses_isendwin_impl(PyObject *module)
3367/*[clinic end generated code: output=d73179e4a7e1eb8c input=6cdb01a7ebf71397]*/
3368NoArgTrueFalseFunctionBody(isendwin)
3369
3370#ifdef HAVE_CURSES_IS_TERM_RESIZED
3371/*[clinic input]
3372_curses.is_term_resized
3373
3374 nlines: int
3375 Height.
3376 ncols: int
3377 Width.
3378 /
3379
3380Return True if resize_term() would modify the window structure, False otherwise.
3381[clinic start generated code]*/
3382
3383static PyObject *
3384_curses_is_term_resized_impl(PyObject *module, int nlines, int ncols)
3385/*[clinic end generated code: output=aafe04afe50f1288 input=ca9c0bd0fb8ab444]*/
3386{
Victor Stinner26486ea2010-05-15 22:23:53 +00003387 PyCursesInitialised;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003388
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003389 return PyBool_FromLong(is_term_resized(nlines, ncols));
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003390}
3391#endif /* HAVE_CURSES_IS_TERM_RESIZED */
3392
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003393/*[clinic input]
3394_curses.keyname
3395
3396 key: int
3397 Key number.
3398 /
3399
3400Return the name of specified key.
3401[clinic start generated code]*/
3402
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003403static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003404_curses_keyname_impl(PyObject *module, int key)
3405/*[clinic end generated code: output=fa2675ab3f4e056b input=ee4b1d0f243a2a2b]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003406{
Victor Stinner26486ea2010-05-15 22:23:53 +00003407 const char *knp;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003408
Victor Stinner26486ea2010-05-15 22:23:53 +00003409 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003410
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003411 if (key < 0) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003412 PyErr_SetString(PyExc_ValueError, "invalid key number");
3413 return NULL;
3414 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003415 knp = keyname(key);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003416
Serhiy Storchaka20b39b22014-09-28 11:27:24 +03003417 return PyBytes_FromString((knp == NULL) ? "" : knp);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003418}
3419
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003420/*[clinic input]
3421_curses.killchar
3422
3423Return the user's current line kill character.
3424[clinic start generated code]*/
3425
Victor Stinner26486ea2010-05-15 22:23:53 +00003426static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003427_curses_killchar_impl(PyObject *module)
3428/*[clinic end generated code: output=31c3a45b2c528269 input=1ff171c38df5ccad]*/
Victor Stinner26486ea2010-05-15 22:23:53 +00003429{
3430 char ch;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003431
Victor Stinner26486ea2010-05-15 22:23:53 +00003432 ch = killchar();
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003433
Victor Stinner26486ea2010-05-15 22:23:53 +00003434 return PyBytes_FromStringAndSize(&ch, 1);
3435}
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003436
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003437/*[clinic input]
3438_curses.longname
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003439
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003440Return the terminfo long name field describing the current terminal.
3441
3442The maximum length of a verbose description is 128 characters. It is defined
3443only after the call to initscr().
3444[clinic start generated code]*/
3445
3446static PyObject *
3447_curses_longname_impl(PyObject *module)
3448/*[clinic end generated code: output=fdf30433727ef568 input=84c3f20201b1098e]*/
3449NoArgReturnStringFunctionBody(longname)
3450
3451/*[clinic input]
3452_curses.meta
3453
3454 yes: bool(accept={int})
3455 /
3456
3457Enable/disable meta keys.
3458
3459If yes is True, allow 8-bit characters to be input. If yes is False,
3460allow only 7-bit characters.
3461[clinic start generated code]*/
3462
3463static PyObject *
3464_curses_meta_impl(PyObject *module, int yes)
3465/*[clinic end generated code: output=22f5abda46a605d8 input=af9892e3a74f35db]*/
3466{
Victor Stinner26486ea2010-05-15 22:23:53 +00003467 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003468
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003469 return PyCursesCheckERR(meta(stdscr, yes), "meta");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003470}
3471
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003472#ifdef NCURSES_MOUSE_VERSION
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003473/*[clinic input]
3474_curses.mouseinterval
3475
3476 interval: int
3477 Time in milliseconds.
3478 /
3479
3480Set and retrieve the maximum time between press and release in a click.
3481
3482Set the maximum time that can elapse between press and release events in
3483order for them to be recognized as a click, and return the previous interval
3484value.
3485[clinic start generated code]*/
3486
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003487static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003488_curses_mouseinterval_impl(PyObject *module, int interval)
3489/*[clinic end generated code: output=c4f5ff04354634c5 input=75aaa3f0db10ac4e]*/
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003490{
Victor Stinner26486ea2010-05-15 22:23:53 +00003491 PyCursesInitialised;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003492
Victor Stinner26486ea2010-05-15 22:23:53 +00003493 return PyCursesCheckERR(mouseinterval(interval), "mouseinterval");
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003494}
3495
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003496/*[clinic input]
3497_curses.mousemask
3498
3499 newmask: unsigned_long(bitwise=True)
3500 /
3501
3502Set the mouse events to be reported, and return a tuple (availmask, oldmask).
3503
3504Return a tuple (availmask, oldmask). availmask indicates which of the
3505specified mouse events can be reported; on complete failure it returns 0.
3506oldmask is the previous value of the given window's mouse event mask.
3507If this function is never called, no mouse events are ever reported.
3508[clinic start generated code]*/
3509
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003510static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003511_curses_mousemask_impl(PyObject *module, unsigned long newmask)
3512/*[clinic end generated code: output=9406cf1b8a36e485 input=bdf76b7568a3c541]*/
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003513{
Victor Stinner26486ea2010-05-15 22:23:53 +00003514 mmask_t oldmask, availmask;
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003515
Victor Stinner26486ea2010-05-15 22:23:53 +00003516 PyCursesInitialised;
Serhiy Storchaka4f469c02017-11-01 20:48:49 +02003517 availmask = mousemask((mmask_t)newmask, &oldmask);
3518 return Py_BuildValue("(kk)",
3519 (unsigned long)availmask, (unsigned long)oldmask);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00003520}
3521#endif
3522
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003523/*[clinic input]
3524_curses.napms
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +00003525
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003526 ms: int
3527 Duration in milliseconds.
3528 /
3529
3530Sleep for specified time.
3531[clinic start generated code]*/
3532
3533static PyObject *
3534_curses_napms_impl(PyObject *module, int ms)
3535/*[clinic end generated code: output=a40a1da2e39ea438 input=20cd3af2b6900f56]*/
3536{
Andrew M. Kuchling2b146762010-05-10 23:30:25 +00003537 PyCursesInitialised;
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +00003538
3539 return Py_BuildValue("i", napms(ms));
3540}
3541
3542
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003543/*[clinic input]
3544_curses.newpad
3545
3546 nlines: int
3547 Height.
3548 ncols: int
3549 Width.
3550 /
3551
3552Create and return a pointer to a new pad data structure.
3553[clinic start generated code]*/
3554
Andrew M. Kuchling2824d7f2000-12-15 00:44:02 +00003555static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003556_curses_newpad_impl(PyObject *module, int nlines, int ncols)
3557/*[clinic end generated code: output=de52a56eb1098ec9 input=93f1272f240d8894]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003558{
Victor Stinner26486ea2010-05-15 22:23:53 +00003559 WINDOW *win;
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
Victor Stinner26486ea2010-05-15 22:23:53 +00003563 win = newpad(nlines, ncols);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003564
Victor Stinner26486ea2010-05-15 22:23:53 +00003565 if (win == NULL) {
3566 PyErr_SetString(PyCursesError, catchall_NULL);
3567 return NULL;
3568 }
3569
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003570 return (PyObject *)PyCursesWindow_New(win, NULL);
Guido van Rossumf6971e21994-08-30 12:25:20 +00003571}
3572
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003573/*[clinic input]
3574_curses.newwin
3575
3576 nlines: int
3577 Height.
3578 ncols: int
3579 Width.
3580 [
3581 begin_y: int = 0
3582 Top side y-coordinate.
3583 begin_x: int = 0
3584 Left side x-coordinate.
3585 ]
3586 /
3587
3588Return a new window.
3589
3590By default, the window will extend from the specified position to the lower
3591right corner of the screen.
3592[clinic start generated code]*/
3593
Guido van Rossumf6971e21994-08-30 12:25:20 +00003594static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003595_curses_newwin_impl(PyObject *module, int nlines, int ncols,
3596 int group_right_1, int begin_y, int begin_x)
3597/*[clinic end generated code: output=c1e0a8dc8ac2826c input=29312c15a72a003d]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00003598{
Victor Stinner26486ea2010-05-15 22:23:53 +00003599 WINDOW *win;
Guido van Rossum85738471995-02-17 13:50:17 +00003600
Victor Stinner26486ea2010-05-15 22:23:53 +00003601 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003602
Victor Stinner26486ea2010-05-15 22:23:53 +00003603 win = newwin(nlines,ncols,begin_y,begin_x);
3604 if (win == NULL) {
3605 PyErr_SetString(PyCursesError, catchall_NULL);
3606 return NULL;
3607 }
Guido van Rossum85738471995-02-17 13:50:17 +00003608
Victor Stinner0fdfceb2011-11-25 22:10:02 +01003609 return (PyObject *)PyCursesWindow_New(win, NULL);
Guido van Rossumf6971e21994-08-30 12:25:20 +00003610}
3611
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003612/*[clinic input]
3613_curses.nl
3614
3615 flag: bool(accept={int}) = True
3616 If false, the effect is the same as calling nonl().
3617 /
3618
3619Enter newline mode.
3620
3621This mode translates the return key into newline on input, and translates
3622newline into return and line-feed on output. Newline mode is initially on.
3623[clinic start generated code]*/
3624
Guido van Rossumf6971e21994-08-30 12:25:20 +00003625static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003626_curses_nl_impl(PyObject *module, int flag)
3627/*[clinic end generated code: output=b39cc0ffc9015003 input=cf36a63f7b86e28a]*/
3628NoArgOrFlagNoReturnFunctionBody(nl, flag)
3629
3630/*[clinic input]
3631_curses.nocbreak
3632
3633Leave cbreak mode.
3634
3635Return to normal "cooked" mode with line buffering.
3636[clinic start generated code]*/
3637
3638static PyObject *
3639_curses_nocbreak_impl(PyObject *module)
3640/*[clinic end generated code: output=eabf3833a4fbf620 input=e4b65f7d734af400]*/
3641NoArgNoReturnFunctionBody(nocbreak)
3642
3643/*[clinic input]
3644_curses.noecho
3645
3646Leave echo mode.
3647
3648Echoing of input characters is turned off.
3649[clinic start generated code]*/
3650
3651static PyObject *
3652_curses_noecho_impl(PyObject *module)
3653/*[clinic end generated code: output=cc95ab45bc98f41b input=76714df529e614c3]*/
3654NoArgNoReturnFunctionBody(noecho)
3655
3656/*[clinic input]
3657_curses.nonl
3658
3659Leave newline mode.
3660
3661Disable translation of return into newline on input, and disable low-level
3662translation of newline into newline/return on output.
3663[clinic start generated code]*/
3664
3665static PyObject *
3666_curses_nonl_impl(PyObject *module)
3667/*[clinic end generated code: output=99e917e9715770c6 input=9d37dd122d3022fc]*/
3668NoArgNoReturnFunctionBody(nonl)
3669
3670/*[clinic input]
3671_curses.noqiflush
3672
3673Disable queue flushing.
3674
3675When queue flushing is disabled, normal flush of input and output queues
3676associated with the INTR, QUIT and SUSP characters will not be done.
3677[clinic start generated code]*/
3678
3679static PyObject *
3680_curses_noqiflush_impl(PyObject *module)
3681/*[clinic end generated code: output=8b95a4229bbf0877 input=ba3e6b2e3e54c4df]*/
3682NoArgNoReturnVoidFunctionBody(noqiflush)
3683
3684/*[clinic input]
3685_curses.noraw
3686
3687Leave raw mode.
3688
3689Return to normal "cooked" mode with line buffering.
3690[clinic start generated code]*/
3691
3692static PyObject *
3693_curses_noraw_impl(PyObject *module)
3694/*[clinic end generated code: output=39894e5524c430cc input=6ec86692096dffb5]*/
3695NoArgNoReturnFunctionBody(noraw)
3696
3697/*[clinic input]
3698_curses.pair_content
3699
3700 pair_number: short
3701 The number of the color pair (1 - (COLOR_PAIRS-1)).
3702 /
3703
3704Return a tuple (fg, bg) containing the colors for the requested color pair.
3705[clinic start generated code]*/
3706
3707static PyObject *
3708_curses_pair_content_impl(PyObject *module, short pair_number)
3709/*[clinic end generated code: output=5a72aa1a28bbacf3 input=f4d7fec5643b976b]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00003710{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003711 short f, b;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003712
Victor Stinner26486ea2010-05-15 22:23:53 +00003713 PyCursesInitialised;
3714 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003715
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003716 if (pair_content(pair_number, &f, &b)==ERR) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003717 PyErr_SetString(PyCursesError,
3718 "Argument 1 was out of range. (1..COLOR_PAIRS-1)");
3719 return NULL;
3720 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003721
Victor Stinner26486ea2010-05-15 22:23:53 +00003722 return Py_BuildValue("(ii)", f, b);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003723}
3724
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003725/*[clinic input]
3726_curses.pair_number
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003727
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003728 attr: int
3729 /
3730
3731Return the number of the color-pair set by the specified attribute value.
3732
3733color_pair() is the counterpart to this function.
3734[clinic start generated code]*/
3735
3736static PyObject *
3737_curses_pair_number_impl(PyObject *module, int attr)
3738/*[clinic end generated code: output=85bce7d65c0aa3f4 input=d478548e33f5e61a]*/
3739{
Victor Stinner26486ea2010-05-15 22:23:53 +00003740 PyCursesInitialised;
3741 PyCursesInitialisedColor;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003742
Victor Stinner077af8c2019-08-14 12:31:43 +02003743 return PyLong_FromLong(attr_to_color_pair(attr));
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003744}
3745
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003746/*[clinic input]
3747_curses.putp
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003748
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003749 string: str(accept={robuffer})
3750 /
3751
3752Emit the value of a specified terminfo capability for the current terminal.
3753
3754Note that the output of putp() always goes to standard output.
3755[clinic start generated code]*/
3756
3757static PyObject *
3758_curses_putp_impl(PyObject *module, const char *string)
3759/*[clinic end generated code: output=e98081d1b8eb5816 input=1601faa828b44cb3]*/
3760{
3761 return PyCursesCheckERR(putp(string), "putp");
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003762}
3763
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003764/*[clinic input]
3765_curses.qiflush
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003766
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003767 flag: bool(accept={int}) = True
3768 If false, the effect is the same as calling noqiflush().
3769 /
3770
3771Enable queue flushing.
3772
3773If queue flushing is enabled, all output in the display driver queue
3774will be flushed when the INTR, QUIT and SUSP characters are read.
3775[clinic start generated code]*/
3776
3777static PyObject *
3778_curses_qiflush_impl(PyObject *module, int flag)
3779/*[clinic end generated code: output=9167e862f760ea30 input=e9e4a389946a0dbc]*/
3780{
Victor Stinner26486ea2010-05-15 22:23:53 +00003781 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003782
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003783 if (flag) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003784 qiflush();
Victor Stinner26486ea2010-05-15 22:23:53 +00003785 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003786 else {
3787 noqiflush();
3788 }
3789 Py_RETURN_NONE;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003790}
3791
Guido van Rossumd8faa362007-04-27 19:54:29 +00003792/* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES
3793 * and _curses.COLS */
Benjamin Petersonfea6a942008-07-02 16:11:42 +00003794#if defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM)
Guido van Rossumd8faa362007-04-27 19:54:29 +00003795static int
3796update_lines_cols(void)
3797{
Victor Stinner26486ea2010-05-15 22:23:53 +00003798 PyObject *o;
3799 PyObject *m = PyImport_ImportModuleNoBlock("curses");
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003800 _Py_IDENTIFIER(LINES);
3801 _Py_IDENTIFIER(COLS);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003802
Victor Stinner26486ea2010-05-15 22:23:53 +00003803 if (!m)
3804 return 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003805
Victor Stinner26486ea2010-05-15 22:23:53 +00003806 o = PyLong_FromLong(LINES);
3807 if (!o) {
3808 Py_DECREF(m);
3809 return 0;
3810 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003811 if (_PyObject_SetAttrId(m, &PyId_LINES, o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003812 Py_DECREF(m);
3813 Py_DECREF(o);
3814 return 0;
3815 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003816 /* PyId_LINES.object will be initialized here. */
Victor Stinner4804b5b2020-05-12 01:43:38 +02003817 if (PyDict_SetItem(ModDict, _PyUnicode_FromId(&PyId_LINES), o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003818 Py_DECREF(m);
3819 Py_DECREF(o);
3820 return 0;
3821 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003822 Py_DECREF(o);
Victor Stinner26486ea2010-05-15 22:23:53 +00003823 o = PyLong_FromLong(COLS);
3824 if (!o) {
3825 Py_DECREF(m);
3826 return 0;
3827 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003828 if (_PyObject_SetAttrId(m, &PyId_COLS, o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003829 Py_DECREF(m);
3830 Py_DECREF(o);
3831 return 0;
3832 }
Victor Stinner4804b5b2020-05-12 01:43:38 +02003833 if (PyDict_SetItem(ModDict, _PyUnicode_FromId(&PyId_COLS), o)) {
Victor Stinner26486ea2010-05-15 22:23:53 +00003834 Py_DECREF(m);
3835 Py_DECREF(o);
3836 return 0;
3837 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003838 Py_DECREF(o);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003839 Py_DECREF(m);
Victor Stinner26486ea2010-05-15 22:23:53 +00003840 return 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003841}
Steve Dowerd2bc3892015-04-15 18:06:05 -04003842
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003843/*[clinic input]
Zackery Spytz2bc34342019-11-17 10:10:13 -07003844_curses.update_lines_cols
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003845
3846[clinic start generated code]*/
3847
Zackery Spytz2bc34342019-11-17 10:10:13 -07003848static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003849_curses_update_lines_cols_impl(PyObject *module)
Zackery Spytz2bc34342019-11-17 10:10:13 -07003850/*[clinic end generated code: output=423f2b1e63ed0f75 input=5f065ab7a28a5d90]*/
Steve Dowerd2bc3892015-04-15 18:06:05 -04003851{
Zackery Spytz2bc34342019-11-17 10:10:13 -07003852 if (!update_lines_cols()) {
3853 return NULL;
3854 }
3855 Py_RETURN_NONE;
Steve Dowerd2bc3892015-04-15 18:06:05 -04003856}
3857
Benjamin Petersonfea6a942008-07-02 16:11:42 +00003858#endif
Guido van Rossumd8faa362007-04-27 19:54:29 +00003859
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003860/*[clinic input]
3861_curses.raw
3862
3863 flag: bool(accept={int}) = True
3864 If false, the effect is the same as calling noraw().
3865 /
3866
3867Enter raw mode.
3868
3869In raw mode, normal line buffering and processing of interrupt, quit,
3870suspend, and flow control keys are turned off; characters are presented to
3871curses input functions one by one.
3872[clinic start generated code]*/
3873
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003874static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003875_curses_raw_impl(PyObject *module, int flag)
3876/*[clinic end generated code: output=a750e4b342be015b input=e36d8db27832b848]*/
3877NoArgOrFlagNoReturnFunctionBody(raw, flag)
3878
3879/*[clinic input]
3880_curses.reset_prog_mode
3881
3882Restore the terminal to "program" mode, as previously saved by def_prog_mode().
3883[clinic start generated code]*/
3884
3885static PyObject *
3886_curses_reset_prog_mode_impl(PyObject *module)
3887/*[clinic end generated code: output=15eb765abf0b6575 input=3d82bea2b3243471]*/
3888NoArgNoReturnFunctionBody(reset_prog_mode)
3889
3890/*[clinic input]
3891_curses.reset_shell_mode
3892
3893Restore the terminal to "shell" mode, as previously saved by def_shell_mode().
3894[clinic start generated code]*/
3895
3896static PyObject *
3897_curses_reset_shell_mode_impl(PyObject *module)
3898/*[clinic end generated code: output=0238de2962090d33 input=1c738fa64bd1a24f]*/
3899NoArgNoReturnFunctionBody(reset_shell_mode)
3900
3901/*[clinic input]
3902_curses.resetty
3903
3904Restore terminal mode.
3905[clinic start generated code]*/
3906
3907static PyObject *
3908_curses_resetty_impl(PyObject *module)
3909/*[clinic end generated code: output=ff4b448e80a7cd63 input=940493de03624bb0]*/
3910NoArgNoReturnFunctionBody(resetty)
3911
3912#ifdef HAVE_CURSES_RESIZETERM
3913/*[clinic input]
3914_curses.resizeterm
3915
3916 nlines: int
3917 Height.
3918 ncols: int
3919 Width.
3920 /
3921
3922Resize the standard and current windows to the specified dimensions.
3923
3924Adjusts other bookkeeping data used by the curses library that record the
3925window dimensions (in particular the SIGWINCH handler).
3926[clinic start generated code]*/
3927
3928static PyObject *
3929_curses_resizeterm_impl(PyObject *module, int nlines, int ncols)
3930/*[clinic end generated code: output=56d6bcc5194ad055 input=0fca02ebad5ffa82]*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003931{
Victor Stinner26486ea2010-05-15 22:23:53 +00003932 PyObject *result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003933
Victor Stinner26486ea2010-05-15 22:23:53 +00003934 PyCursesInitialised;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003935
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003936 result = PyCursesCheckERR(resizeterm(nlines, ncols), "resizeterm");
Victor Stinner26486ea2010-05-15 22:23:53 +00003937 if (!result)
3938 return NULL;
Zackery Spytz2bc34342019-11-17 10:10:13 -07003939 if (!update_lines_cols()) {
3940 Py_DECREF(result);
Victor Stinner26486ea2010-05-15 22:23:53 +00003941 return NULL;
Zackery Spytz2bc34342019-11-17 10:10:13 -07003942 }
Victor Stinner26486ea2010-05-15 22:23:53 +00003943 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003944}
3945
3946#endif
3947
3948#ifdef HAVE_CURSES_RESIZE_TERM
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003949/*[clinic input]
3950_curses.resize_term
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003951
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003952 nlines: int
3953 Height.
3954 ncols: int
3955 Width.
3956 /
3957
3958Backend function used by resizeterm(), performing most of the work.
3959
3960When resizing the windows, resize_term() blank-fills the areas that are
3961extended. The calling application should fill in these areas with appropriate
3962data. The resize_term() function attempts to resize all windows. However,
3963due to the calling convention of pads, it is not possible to resize these
3964without additional interaction with the application.
3965[clinic start generated code]*/
3966
3967static PyObject *
3968_curses_resize_term_impl(PyObject *module, int nlines, int ncols)
3969/*[clinic end generated code: output=9e26d8b9ea311ed2 input=2197edd05b049ed4]*/
3970{
Victor Stinner26486ea2010-05-15 22:23:53 +00003971 PyObject *result;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003972
Victor Stinner26486ea2010-05-15 22:23:53 +00003973 PyCursesInitialised;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003974
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003975 result = PyCursesCheckERR(resize_term(nlines, ncols), "resize_term");
Victor Stinner26486ea2010-05-15 22:23:53 +00003976 if (!result)
3977 return NULL;
Zackery Spytz2bc34342019-11-17 10:10:13 -07003978 if (!update_lines_cols()) {
3979 Py_DECREF(result);
Victor Stinner26486ea2010-05-15 22:23:53 +00003980 return NULL;
Zackery Spytz2bc34342019-11-17 10:10:13 -07003981 }
Victor Stinner26486ea2010-05-15 22:23:53 +00003982 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003983}
3984#endif /* HAVE_CURSES_RESIZE_TERM */
3985
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003986/*[clinic input]
3987_curses.savetty
3988
3989Save terminal mode.
3990[clinic start generated code]*/
3991
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00003992static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03003993_curses_savetty_impl(PyObject *module)
3994/*[clinic end generated code: output=6babc49f12b42199 input=fce6b2b7d2200102]*/
3995NoArgNoReturnFunctionBody(savetty)
3996
3997#ifdef getsyx
3998/*[clinic input]
3999_curses.setsyx
4000
4001 y: int
4002 Y-coordinate.
4003 x: int
4004 X-coordinate.
4005 /
4006
4007Set the virtual screen cursor.
4008
4009If y and x are both -1, then leaveok is set.
4010[clinic start generated code]*/
4011
4012static PyObject *
4013_curses_setsyx_impl(PyObject *module, int y, int x)
4014/*[clinic end generated code: output=23dcf753511a2464 input=fa7f2b208e10a557]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004015{
Victor Stinner26486ea2010-05-15 22:23:53 +00004016 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004017
Victor Stinner26486ea2010-05-15 22:23:53 +00004018 setsyx(y,x);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004019
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004020 Py_RETURN_NONE;
Guido van Rossumf6971e21994-08-30 12:25:20 +00004021}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02004022#endif
Guido van Rossumf6971e21994-08-30 12:25:20 +00004023
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004024/*[clinic input]
4025_curses.start_color
4026
4027Initializes eight basic colors and global variables COLORS and COLOR_PAIRS.
4028
4029Must be called if the programmer wants to use colors, and before any other
4030color manipulation routine is called. It is good practice to call this
4031routine right after initscr().
4032
4033It also restores the colors on the terminal to the values they had when the
4034terminal was just turned on.
4035[clinic start generated code]*/
4036
Guido van Rossumf6971e21994-08-30 12:25:20 +00004037static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004038_curses_start_color_impl(PyObject *module)
4039/*[clinic end generated code: output=8b772b41d8090ede input=0ca0ecb2b77e1a12]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00004040{
Victor Stinner26486ea2010-05-15 22:23:53 +00004041 int code;
4042 PyObject *c, *cp;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004043
Victor Stinner26486ea2010-05-15 22:23:53 +00004044 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004045
Victor Stinner26486ea2010-05-15 22:23:53 +00004046 code = start_color();
4047 if (code != ERR) {
4048 initialisedcolors = TRUE;
4049 c = PyLong_FromLong((long) COLORS);
Christian Heimesa956e642013-07-26 14:45:37 +02004050 if (c == NULL)
4051 return NULL;
Zackery Spytz2bc34342019-11-17 10:10:13 -07004052 if (PyDict_SetItemString(ModDict, "COLORS", c) < 0) {
4053 Py_DECREF(c);
4054 return NULL;
4055 }
Victor Stinner26486ea2010-05-15 22:23:53 +00004056 Py_DECREF(c);
4057 cp = PyLong_FromLong((long) COLOR_PAIRS);
Christian Heimesa956e642013-07-26 14:45:37 +02004058 if (cp == NULL)
4059 return NULL;
Zackery Spytz2bc34342019-11-17 10:10:13 -07004060 if (PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp) < 0) {
4061 Py_DECREF(cp);
4062 return NULL;
4063 }
Victor Stinner26486ea2010-05-15 22:23:53 +00004064 Py_DECREF(cp);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004065 Py_RETURN_NONE;
Victor Stinner26486ea2010-05-15 22:23:53 +00004066 } else {
4067 PyErr_SetString(PyCursesError, "start_color() returned ERR");
4068 return NULL;
4069 }
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004070}
4071
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004072/*[clinic input]
4073_curses.termattrs
4074
4075Return a logical OR of all video attributes supported by the terminal.
4076[clinic start generated code]*/
4077
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004078static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004079_curses_termattrs_impl(PyObject *module)
4080/*[clinic end generated code: output=b06f437fce1b6fc4 input=0559882a04f84d1d]*/
4081NoArgReturnIntFunctionBody(termattrs)
4082
4083/*[clinic input]
4084_curses.termname
4085
4086Return the value of the environment variable TERM, truncated to 14 characters.
4087[clinic start generated code]*/
4088
4089static PyObject *
4090_curses_termname_impl(PyObject *module)
4091/*[clinic end generated code: output=96375577ebbd67fd input=33c08d000944f33f]*/
4092NoArgReturnStringFunctionBody(termname)
4093
4094/*[clinic input]
4095_curses.tigetflag
4096
4097 capname: str
4098 The terminfo capability name.
4099 /
4100
4101Return the value of the Boolean capability.
4102
4103The value -1 is returned if capname is not a Boolean capability, or 0 if
4104it is canceled or absent from the terminal description.
4105[clinic start generated code]*/
4106
4107static PyObject *
4108_curses_tigetflag_impl(PyObject *module, const char *capname)
4109/*[clinic end generated code: output=8853c0e55542195b input=b0787af9e3e9a6ce]*/
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004110{
Victor Stinner26486ea2010-05-15 22:23:53 +00004111 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004112
Xiang Zhang44742e92018-06-23 12:29:30 +08004113 return PyLong_FromLong( (long) tigetflag( (char *)capname ) );
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004114}
4115
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004116/*[clinic input]
4117_curses.tigetnum
4118
4119 capname: str
4120 The terminfo capability name.
4121 /
4122
4123Return the value of the numeric capability.
4124
4125The value -2 is returned if capname is not a numeric capability, or -1 if
4126it is canceled or absent from the terminal description.
4127[clinic start generated code]*/
4128
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004129static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004130_curses_tigetnum_impl(PyObject *module, const char *capname)
4131/*[clinic end generated code: output=46f8b0a1b5dff42f input=5cdf2f410b109720]*/
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004132{
Victor Stinner26486ea2010-05-15 22:23:53 +00004133 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004134
Xiang Zhang44742e92018-06-23 12:29:30 +08004135 return PyLong_FromLong( (long) tigetnum( (char *)capname ) );
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004136}
4137
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004138/*[clinic input]
4139_curses.tigetstr
4140
4141 capname: str
4142 The terminfo capability name.
4143 /
4144
4145Return the value of the string capability.
4146
4147None is returned if capname is not a string capability, or is canceled or
4148absent from the terminal description.
4149[clinic start generated code]*/
4150
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004151static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004152_curses_tigetstr_impl(PyObject *module, const char *capname)
4153/*[clinic end generated code: output=f22b576ad60248f3 input=36644df25c73c0a7]*/
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004154{
Victor Stinner26486ea2010-05-15 22:23:53 +00004155 PyCursesSetupTermCalled;
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004156
Xiang Zhang44742e92018-06-23 12:29:30 +08004157 capname = tigetstr( (char *)capname );
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +02004158 if (capname == NULL || capname == (char*) -1) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004159 Py_RETURN_NONE;
Victor Stinner26486ea2010-05-15 22:23:53 +00004160 }
4161 return PyBytes_FromString( capname );
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004162}
4163
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004164/*[clinic input]
4165_curses.tparm
4166
4167 str: str(accept={robuffer})
4168 Parameterized byte string obtained from the terminfo database.
4169 i1: int = 0
4170 i2: int = 0
4171 i3: int = 0
4172 i4: int = 0
4173 i5: int = 0
4174 i6: int = 0
4175 i7: int = 0
4176 i8: int = 0
4177 i9: int = 0
4178 /
4179
4180Instantiate the specified byte string with the supplied parameters.
4181[clinic start generated code]*/
4182
Andrew M. Kuchling70e89c12000-07-26 02:57:20 +00004183static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004184_curses_tparm_impl(PyObject *module, const char *str, int i1, int i2, int i3,
4185 int i4, int i5, int i6, int i7, int i8, int i9)
4186/*[clinic end generated code: output=599f62b615c667ff input=5e30b15786f032aa]*/
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00004187{
Victor Stinner26486ea2010-05-15 22:23:53 +00004188 char* result = NULL;
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00004189
Victor Stinner26486ea2010-05-15 22:23:53 +00004190 PyCursesSetupTermCalled;
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00004191
Xiang Zhang44742e92018-06-23 12:29:30 +08004192 result = tparm((char *)str,i1,i2,i3,i4,i5,i6,i7,i8,i9);
Victor Stinner26486ea2010-05-15 22:23:53 +00004193 if (!result) {
4194 PyErr_SetString(PyCursesError, "tparm() returned NULL");
4195 return NULL;
4196 }
Michael W. Hudson21007982002-11-21 14:17:51 +00004197
Victor Stinner26486ea2010-05-15 22:23:53 +00004198 return PyBytes_FromString(result);
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00004199}
4200
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02004201#ifdef HAVE_CURSES_TYPEAHEAD
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004202/*[clinic input]
4203_curses.typeahead
4204
4205 fd: int
4206 File descriptor.
4207 /
4208
4209Specify that the file descriptor fd be used for typeahead checking.
4210
4211If fd is -1, then no typeahead checking is done.
4212[clinic start generated code]*/
4213
Andrew M. Kuchlingf16e0ed2000-11-07 03:35:24 +00004214static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004215_curses_typeahead_impl(PyObject *module, int fd)
4216/*[clinic end generated code: output=084bb649d7066583 input=f2968d8e1805051b]*/
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00004217{
Victor Stinner26486ea2010-05-15 22:23:53 +00004218 PyCursesInitialised;
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00004219
Victor Stinner26486ea2010-05-15 22:23:53 +00004220 return PyCursesCheckERR(typeahead( fd ), "typeahead");
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00004221}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02004222#endif
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00004223
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004224/*[clinic input]
4225_curses.unctrl
4226
4227 ch: object
4228 /
4229
4230Return a string which is a printable representation of the character ch.
4231
4232Control characters are displayed as a caret followed by the character,
4233for example as ^C. Printing characters are left as they are.
4234[clinic start generated code]*/
4235
Andrew M. Kuchling97311bb2000-06-21 01:41:48 +00004236static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004237_curses_unctrl(PyObject *module, PyObject *ch)
4238/*[clinic end generated code: output=8e07fafc430c9434 input=cd1e35e16cd1ace4]*/
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004239{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004240 chtype ch_;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004241
Victor Stinner26486ea2010-05-15 22:23:53 +00004242 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004243
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004244 if (!PyCurses_ConvertToChtype(NULL, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00004245 return NULL;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004246
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004247 return PyBytes_FromString(unctrl(ch_));
Guido van Rossumf6971e21994-08-30 12:25:20 +00004248}
4249
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004250/*[clinic input]
4251_curses.ungetch
4252
4253 ch: object
4254 /
4255
4256Push ch so the next getch() will return it.
4257[clinic start generated code]*/
4258
Guido van Rossumf6971e21994-08-30 12:25:20 +00004259static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004260_curses_ungetch(PyObject *module, PyObject *ch)
4261/*[clinic end generated code: output=9b19d8268376d887 input=6681e6ae4c42e5eb]*/
Guido van Rossumf6971e21994-08-30 12:25:20 +00004262{
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004263 chtype ch_;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004264
Victor Stinner26486ea2010-05-15 22:23:53 +00004265 PyCursesInitialised;
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004266
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004267 if (!PyCurses_ConvertToChtype(NULL, ch, &ch_))
Victor Stinner26486ea2010-05-15 22:23:53 +00004268 return NULL;
Victor Stinner0fdfceb2011-11-25 22:10:02 +01004269
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004270 return PyCursesCheckERR(ungetch(ch_), "ungetch");
Guido van Rossumf6971e21994-08-30 12:25:20 +00004271}
4272
Victor Stinner71e44cb2011-09-06 01:53:03 +02004273#ifdef HAVE_NCURSESW
4274/* Convert an object to a character (wchar_t):
4275
4276 - int
4277 - str of length 1
4278
4279 Return 1 on success, 0 on error. */
4280static int
4281PyCurses_ConvertToWchar_t(PyObject *obj,
4282 wchar_t *wch)
4283{
4284 if (PyUnicode_Check(obj)) {
4285 wchar_t buffer[2];
4286 if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) {
4287 PyErr_Format(PyExc_TypeError,
Anthony Sottilec9345e32019-07-31 05:11:24 -07004288 "expect str of length 1 or int, "
Victor Stinner71e44cb2011-09-06 01:53:03 +02004289 "got a str of length %zi",
Victor Stinnerc4f281e2011-10-11 22:11:42 +02004290 PyUnicode_GET_LENGTH(obj));
Victor Stinner71e44cb2011-09-06 01:53:03 +02004291 return 0;
4292 }
4293 *wch = buffer[0];
4294 return 2;
4295 }
4296 else if (PyLong_CheckExact(obj)) {
4297 long value;
4298 int overflow;
4299 value = PyLong_AsLongAndOverflow(obj, &overflow);
4300 if (overflow) {
4301 PyErr_SetString(PyExc_OverflowError,
4302 "int doesn't fit in long");
4303 return 0;
4304 }
4305 *wch = (wchar_t)value;
4306 if ((long)*wch != value) {
4307 PyErr_Format(PyExc_OverflowError,
4308 "character doesn't fit in wchar_t");
4309 return 0;
4310 }
4311 return 1;
4312 }
4313 else {
4314 PyErr_Format(PyExc_TypeError,
Anthony Sottilec9345e32019-07-31 05:11:24 -07004315 "expect str of length 1 or int, got %s",
Victor Stinner71e44cb2011-09-06 01:53:03 +02004316 Py_TYPE(obj)->tp_name);
4317 return 0;
4318 }
4319}
4320
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004321/*[clinic input]
4322_curses.unget_wch
4323
4324 ch: object
4325 /
4326
4327Push ch so the next get_wch() will return it.
4328[clinic start generated code]*/
4329
Victor Stinner71e44cb2011-09-06 01:53:03 +02004330static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004331_curses_unget_wch(PyObject *module, PyObject *ch)
4332/*[clinic end generated code: output=1974c9fb01d37863 input=0d56dc65a46feebb]*/
Victor Stinner71e44cb2011-09-06 01:53:03 +02004333{
Victor Stinner71e44cb2011-09-06 01:53:03 +02004334 wchar_t wch;
4335
4336 PyCursesInitialised;
4337
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004338 if (!PyCurses_ConvertToWchar_t(ch, &wch))
Victor Stinner71e44cb2011-09-06 01:53:03 +02004339 return NULL;
4340 return PyCursesCheckERR(unget_wch(wch), "unget_wch");
4341}
4342#endif
4343
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004344#ifdef HAVE_CURSES_USE_ENV
4345/*[clinic input]
4346_curses.use_env
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004347
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004348 flag: bool(accept={int})
4349 /
4350
4351Use environment variables LINES and COLUMNS.
4352
4353If used, this function should be called before initscr() or newterm() are
4354called.
4355
4356When flag is False, the values of lines and columns specified in the terminfo
4357database will be used, even if environment variables LINES and COLUMNS (used
4358by default) are set, or if curses is running in a window (in which case
4359default behavior would be to use the window size if LINES and COLUMNS are
4360not set).
4361[clinic start generated code]*/
4362
4363static PyObject *
4364_curses_use_env_impl(PyObject *module, int flag)
4365/*[clinic end generated code: output=b2c445e435c0b164 input=1778eb1e9151ea37]*/
4366{
Victor Stinner26486ea2010-05-15 22:23:53 +00004367 use_env(flag);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004368 Py_RETURN_NONE;
Guido van Rossumf6971e21994-08-30 12:25:20 +00004369}
Serhiy Storchakabaac01e2017-10-31 13:56:44 +02004370#endif
Guido van Rossumf6971e21994-08-30 12:25:20 +00004371
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004372#ifndef STRICT_SYSV_CURSES
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004373/*[clinic input]
4374_curses.use_default_colors
4375
4376Allow use of default values for colors on terminals supporting this feature.
4377
4378Use this to support transparency in your application. The default color
4379is assigned to the color number -1.
4380[clinic start generated code]*/
4381
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004382static PyObject *
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004383_curses_use_default_colors_impl(PyObject *module)
4384/*[clinic end generated code: output=a3b81ff71dd901be input=656844367470e8fc]*/
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004385{
Victor Stinner26486ea2010-05-15 22:23:53 +00004386 int code;
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004387
Victor Stinner26486ea2010-05-15 22:23:53 +00004388 PyCursesInitialised;
4389 PyCursesInitialisedColor;
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004390
Victor Stinner26486ea2010-05-15 22:23:53 +00004391 code = use_default_colors();
4392 if (code != ERR) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004393 Py_RETURN_NONE;
Victor Stinner26486ea2010-05-15 22:23:53 +00004394 } else {
4395 PyErr_SetString(PyCursesError, "use_default_colors() returned ERR");
4396 return NULL;
4397 }
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00004398}
4399#endif /* STRICT_SYSV_CURSES */
4400
Serhiy Storchakab232df92018-10-30 13:22:42 +02004401
4402#ifdef NCURSES_VERSION
4403
4404PyDoc_STRVAR(ncurses_version__doc__,
4405"curses.ncurses_version\n\
4406\n\
4407Ncurses version information as a named tuple.");
4408
4409static PyTypeObject NcursesVersionType;
4410
4411static PyStructSequence_Field ncurses_version_fields[] = {
4412 {"major", "Major release number"},
4413 {"minor", "Minor release number"},
4414 {"patch", "Patch release number"},
4415 {0}
4416};
4417
4418static PyStructSequence_Desc ncurses_version_desc = {
4419 "curses.ncurses_version", /* name */
4420 ncurses_version__doc__, /* doc */
4421 ncurses_version_fields, /* fields */
4422 3
4423};
4424
4425static PyObject *
4426make_ncurses_version(void)
4427{
4428 PyObject *ncurses_version;
4429 int pos = 0;
4430
4431 ncurses_version = PyStructSequence_New(&NcursesVersionType);
4432 if (ncurses_version == NULL) {
4433 return NULL;
4434 }
4435
4436#define SetIntItem(flag) \
4437 PyStructSequence_SET_ITEM(ncurses_version, pos++, PyLong_FromLong(flag)); \
4438 if (PyErr_Occurred()) { \
4439 Py_CLEAR(ncurses_version); \
4440 return NULL; \
4441 }
4442
4443 SetIntItem(NCURSES_VERSION_MAJOR)
4444 SetIntItem(NCURSES_VERSION_MINOR)
4445 SetIntItem(NCURSES_VERSION_PATCH)
4446#undef SetIntItem
4447
4448 return ncurses_version;
4449}
4450
4451#endif /* NCURSES_VERSION */
4452
4453
Guido van Rossumf6971e21994-08-30 12:25:20 +00004454/* List of functions defined in the module */
4455
4456static PyMethodDef PyCurses_methods[] = {
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004457 _CURSES_BAUDRATE_METHODDEF
4458 _CURSES_BEEP_METHODDEF
4459 _CURSES_CAN_CHANGE_COLOR_METHODDEF
4460 _CURSES_CBREAK_METHODDEF
4461 _CURSES_COLOR_CONTENT_METHODDEF
4462 _CURSES_COLOR_PAIR_METHODDEF
4463 _CURSES_CURS_SET_METHODDEF
4464 _CURSES_DEF_PROG_MODE_METHODDEF
4465 _CURSES_DEF_SHELL_MODE_METHODDEF
4466 _CURSES_DELAY_OUTPUT_METHODDEF
4467 _CURSES_DOUPDATE_METHODDEF
4468 _CURSES_ECHO_METHODDEF
4469 _CURSES_ENDWIN_METHODDEF
4470 _CURSES_ERASECHAR_METHODDEF
4471 _CURSES_FILTER_METHODDEF
4472 _CURSES_FLASH_METHODDEF
4473 _CURSES_FLUSHINP_METHODDEF
4474 _CURSES_GETMOUSE_METHODDEF
4475 _CURSES_UNGETMOUSE_METHODDEF
4476 _CURSES_GETSYX_METHODDEF
4477 _CURSES_GETWIN_METHODDEF
4478 _CURSES_HAS_COLORS_METHODDEF
4479 _CURSES_HAS_IC_METHODDEF
4480 _CURSES_HAS_IL_METHODDEF
4481 _CURSES_HAS_KEY_METHODDEF
4482 _CURSES_HALFDELAY_METHODDEF
4483 _CURSES_INIT_COLOR_METHODDEF
4484 _CURSES_INIT_PAIR_METHODDEF
4485 _CURSES_INITSCR_METHODDEF
4486 _CURSES_INTRFLUSH_METHODDEF
4487 _CURSES_ISENDWIN_METHODDEF
4488 _CURSES_IS_TERM_RESIZED_METHODDEF
4489 _CURSES_KEYNAME_METHODDEF
4490 _CURSES_KILLCHAR_METHODDEF
4491 _CURSES_LONGNAME_METHODDEF
4492 _CURSES_META_METHODDEF
4493 _CURSES_MOUSEINTERVAL_METHODDEF
4494 _CURSES_MOUSEMASK_METHODDEF
4495 _CURSES_NAPMS_METHODDEF
4496 _CURSES_NEWPAD_METHODDEF
4497 _CURSES_NEWWIN_METHODDEF
4498 _CURSES_NL_METHODDEF
4499 _CURSES_NOCBREAK_METHODDEF
4500 _CURSES_NOECHO_METHODDEF
4501 _CURSES_NONL_METHODDEF
4502 _CURSES_NOQIFLUSH_METHODDEF
4503 _CURSES_NORAW_METHODDEF
4504 _CURSES_PAIR_CONTENT_METHODDEF
4505 _CURSES_PAIR_NUMBER_METHODDEF
4506 _CURSES_PUTP_METHODDEF
4507 _CURSES_QIFLUSH_METHODDEF
4508 _CURSES_RAW_METHODDEF
4509 _CURSES_RESET_PROG_MODE_METHODDEF
4510 _CURSES_RESET_SHELL_MODE_METHODDEF
4511 _CURSES_RESETTY_METHODDEF
4512 _CURSES_RESIZETERM_METHODDEF
4513 _CURSES_RESIZE_TERM_METHODDEF
4514 _CURSES_SAVETTY_METHODDEF
Batuhan Taşkaya4991cf42020-03-03 05:00:10 +03004515#if defined(NCURSES_EXT_FUNCS) && NCURSES_EXT_FUNCS >= 20081102
Anthony Sottileb32cb972019-10-31 02:13:48 -07004516 _CURSES_GET_ESCDELAY_METHODDEF
4517 _CURSES_SET_ESCDELAY_METHODDEF
Batuhan Taşkaya4991cf42020-03-03 05:00:10 +03004518#endif
Anthony Sottileb32cb972019-10-31 02:13:48 -07004519 _CURSES_GET_TABSIZE_METHODDEF
4520 _CURSES_SET_TABSIZE_METHODDEF
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004521 _CURSES_SETSYX_METHODDEF
4522 _CURSES_SETUPTERM_METHODDEF
4523 _CURSES_START_COLOR_METHODDEF
4524 _CURSES_TERMATTRS_METHODDEF
4525 _CURSES_TERMNAME_METHODDEF
4526 _CURSES_TIGETFLAG_METHODDEF
4527 _CURSES_TIGETNUM_METHODDEF
4528 _CURSES_TIGETSTR_METHODDEF
4529 _CURSES_TPARM_METHODDEF
4530 _CURSES_TYPEAHEAD_METHODDEF
4531 _CURSES_UNCTRL_METHODDEF
4532 _CURSES_UNGETCH_METHODDEF
4533 _CURSES_UPDATE_LINES_COLS_METHODDEF
4534 _CURSES_UNGET_WCH_METHODDEF
4535 _CURSES_USE_ENV_METHODDEF
4536 _CURSES_USE_DEFAULT_COLORS_METHODDEF
Victor Stinner26486ea2010-05-15 22:23:53 +00004537 {NULL, NULL} /* sentinel */
Guido van Rossumf6971e21994-08-30 12:25:20 +00004538};
4539
4540/* Initialization function for the module */
4541
Martin v. Löwis1a214512008-06-11 05:26:20 +00004542
4543static struct PyModuleDef _cursesmodule = {
Victor Stinner26486ea2010-05-15 22:23:53 +00004544 PyModuleDef_HEAD_INIT,
4545 "_curses",
4546 NULL,
4547 -1,
4548 PyCurses_methods,
4549 NULL,
4550 NULL,
4551 NULL,
4552 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00004553};
4554
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004555PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00004556PyInit__curses(void)
Guido van Rossumf6971e21994-08-30 12:25:20 +00004557{
Victor Stinner26486ea2010-05-15 22:23:53 +00004558 PyObject *m, *d, *v, *c_api_object;
4559 static void *PyCurses_API[PyCurses_API_pointers];
Andrew M. Kuchling32552682000-12-22 21:52:27 +00004560
Victor Stinner26486ea2010-05-15 22:23:53 +00004561 /* Initialize object type */
4562 if (PyType_Ready(&PyCursesWindow_Type) < 0)
4563 return NULL;
Guido van Rossum6915c4d2001-01-19 00:28:08 +00004564
Victor Stinner26486ea2010-05-15 22:23:53 +00004565 /* Initialize the C API pointer array */
4566 PyCurses_API[0] = (void *)&PyCursesWindow_Type;
4567 PyCurses_API[1] = (void *)func_PyCursesSetupTermCalled;
4568 PyCurses_API[2] = (void *)func_PyCursesInitialised;
4569 PyCurses_API[3] = (void *)func_PyCursesInitialisedColor;
Guido van Rossumf6971e21994-08-30 12:25:20 +00004570
Victor Stinner26486ea2010-05-15 22:23:53 +00004571 /* Create the module and add the functions */
4572 m = PyModule_Create(&_cursesmodule);
4573 if (m == NULL)
4574 return NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +00004575
Victor Stinner26486ea2010-05-15 22:23:53 +00004576 /* Add some symbolic constants to the module */
4577 d = PyModule_GetDict(m);
4578 if (d == NULL)
4579 return NULL;
4580 ModDict = d; /* For PyCurses_InitScr to use later */
Andrew M. Kuchling32552682000-12-22 21:52:27 +00004581
Victor Stinner26486ea2010-05-15 22:23:53 +00004582 /* Add a capsule for the C API */
4583 c_api_object = PyCapsule_New(PyCurses_API, PyCurses_CAPSULE_NAME, NULL);
4584 PyDict_SetItemString(d, "_C_API", c_api_object);
4585 Py_DECREF(c_api_object);
Guido van Rossumfbea2f31994-08-31 22:05:27 +00004586
Victor Stinner26486ea2010-05-15 22:23:53 +00004587 /* For exception curses.error */
4588 PyCursesError = PyErr_NewException("_curses.error", NULL, NULL);
4589 PyDict_SetItemString(d, "error", PyCursesError);
Guido van Rossumfbea2f31994-08-31 22:05:27 +00004590
Victor Stinner26486ea2010-05-15 22:23:53 +00004591 /* Make the version available */
4592 v = PyBytes_FromString(PyCursesVersion);
4593 PyDict_SetItemString(d, "version", v);
4594 PyDict_SetItemString(d, "__version__", v);
4595 Py_DECREF(v);
Guido van Rossumf6971e21994-08-30 12:25:20 +00004596
Serhiy Storchakab232df92018-10-30 13:22:42 +02004597#ifdef NCURSES_VERSION
4598 /* ncurses_version */
4599 if (NcursesVersionType.tp_name == NULL) {
4600 if (PyStructSequence_InitType2(&NcursesVersionType,
4601 &ncurses_version_desc) < 0)
4602 return NULL;
4603 }
4604 v = make_ncurses_version();
4605 if (v == NULL) {
4606 return NULL;
4607 }
4608 PyDict_SetItemString(d, "ncurses_version", v);
4609 Py_DECREF(v);
4610
4611 /* prevent user from creating new instances */
4612 NcursesVersionType.tp_init = NULL;
4613 NcursesVersionType.tp_new = NULL;
4614 if (PyDict_DelItemString(NcursesVersionType.tp_dict, "__new__") < 0 &&
4615 PyErr_ExceptionMatches(PyExc_KeyError))
4616 {
4617 PyErr_Clear();
4618 }
4619#endif /* NCURSES_VERSION */
4620
Victor Stinner26486ea2010-05-15 22:23:53 +00004621 SetDictInt("ERR", ERR);
4622 SetDictInt("OK", OK);
Andrew M. Kuchlingcaefb372000-12-26 15:57:01 +00004623
Victor Stinner26486ea2010-05-15 22:23:53 +00004624 /* Here are some attributes you can add to chars to print */
4625
4626 SetDictInt("A_ATTRIBUTES", A_ATTRIBUTES);
4627 SetDictInt("A_NORMAL", A_NORMAL);
4628 SetDictInt("A_STANDOUT", A_STANDOUT);
4629 SetDictInt("A_UNDERLINE", A_UNDERLINE);
4630 SetDictInt("A_REVERSE", A_REVERSE);
4631 SetDictInt("A_BLINK", A_BLINK);
4632 SetDictInt("A_DIM", A_DIM);
4633 SetDictInt("A_BOLD", A_BOLD);
4634 SetDictInt("A_ALTCHARSET", A_ALTCHARSET);
Victor Stinner26486ea2010-05-15 22:23:53 +00004635 SetDictInt("A_INVIS", A_INVIS);
Victor Stinner26486ea2010-05-15 22:23:53 +00004636 SetDictInt("A_PROTECT", A_PROTECT);
4637 SetDictInt("A_CHARTEXT", A_CHARTEXT);
4638 SetDictInt("A_COLOR", A_COLOR);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004639
Victor Stinner26486ea2010-05-15 22:23:53 +00004640 /* The following are never available with strict SYSV curses */
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004641#ifdef A_HORIZONTAL
Victor Stinner26486ea2010-05-15 22:23:53 +00004642 SetDictInt("A_HORIZONTAL", A_HORIZONTAL);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004643#endif
4644#ifdef A_LEFT
Victor Stinner26486ea2010-05-15 22:23:53 +00004645 SetDictInt("A_LEFT", A_LEFT);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004646#endif
4647#ifdef A_LOW
Victor Stinner26486ea2010-05-15 22:23:53 +00004648 SetDictInt("A_LOW", A_LOW);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004649#endif
4650#ifdef A_RIGHT
Victor Stinner26486ea2010-05-15 22:23:53 +00004651 SetDictInt("A_RIGHT", A_RIGHT);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004652#endif
4653#ifdef A_TOP
Victor Stinner26486ea2010-05-15 22:23:53 +00004654 SetDictInt("A_TOP", A_TOP);
Andrew M. Kuchlinge7d36162000-11-01 19:59:12 +00004655#endif
4656#ifdef A_VERTICAL
Victor Stinner26486ea2010-05-15 22:23:53 +00004657 SetDictInt("A_VERTICAL", A_VERTICAL);
Andrew M. Kuchling22b88ce2000-05-23 16:18:03 +00004658#endif
Andrew M. Kuchling03e644b2000-05-23 16:24:54 +00004659
Xiang Zhang116dd5e2017-06-16 11:20:07 +08004660 /* ncurses extension */
4661#ifdef A_ITALIC
4662 SetDictInt("A_ITALIC", A_ITALIC);
4663#endif
4664
Victor Stinner26486ea2010-05-15 22:23:53 +00004665 SetDictInt("COLOR_BLACK", COLOR_BLACK);
4666 SetDictInt("COLOR_RED", COLOR_RED);
4667 SetDictInt("COLOR_GREEN", COLOR_GREEN);
4668 SetDictInt("COLOR_YELLOW", COLOR_YELLOW);
4669 SetDictInt("COLOR_BLUE", COLOR_BLUE);
4670 SetDictInt("COLOR_MAGENTA", COLOR_MAGENTA);
4671 SetDictInt("COLOR_CYAN", COLOR_CYAN);
4672 SetDictInt("COLOR_WHITE", COLOR_WHITE);
Guido van Rossumf6971e21994-08-30 12:25:20 +00004673
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004674#ifdef NCURSES_MOUSE_VERSION
Victor Stinner26486ea2010-05-15 22:23:53 +00004675 /* Mouse-related constants */
4676 SetDictInt("BUTTON1_PRESSED", BUTTON1_PRESSED);
4677 SetDictInt("BUTTON1_RELEASED", BUTTON1_RELEASED);
4678 SetDictInt("BUTTON1_CLICKED", BUTTON1_CLICKED);
4679 SetDictInt("BUTTON1_DOUBLE_CLICKED", BUTTON1_DOUBLE_CLICKED);
4680 SetDictInt("BUTTON1_TRIPLE_CLICKED", BUTTON1_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004681
Victor Stinner26486ea2010-05-15 22:23:53 +00004682 SetDictInt("BUTTON2_PRESSED", BUTTON2_PRESSED);
4683 SetDictInt("BUTTON2_RELEASED", BUTTON2_RELEASED);
4684 SetDictInt("BUTTON2_CLICKED", BUTTON2_CLICKED);
4685 SetDictInt("BUTTON2_DOUBLE_CLICKED", BUTTON2_DOUBLE_CLICKED);
4686 SetDictInt("BUTTON2_TRIPLE_CLICKED", BUTTON2_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004687
Victor Stinner26486ea2010-05-15 22:23:53 +00004688 SetDictInt("BUTTON3_PRESSED", BUTTON3_PRESSED);
4689 SetDictInt("BUTTON3_RELEASED", BUTTON3_RELEASED);
4690 SetDictInt("BUTTON3_CLICKED", BUTTON3_CLICKED);
4691 SetDictInt("BUTTON3_DOUBLE_CLICKED", BUTTON3_DOUBLE_CLICKED);
4692 SetDictInt("BUTTON3_TRIPLE_CLICKED", BUTTON3_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004693
Victor Stinner26486ea2010-05-15 22:23:53 +00004694 SetDictInt("BUTTON4_PRESSED", BUTTON4_PRESSED);
4695 SetDictInt("BUTTON4_RELEASED", BUTTON4_RELEASED);
4696 SetDictInt("BUTTON4_CLICKED", BUTTON4_CLICKED);
4697 SetDictInt("BUTTON4_DOUBLE_CLICKED", BUTTON4_DOUBLE_CLICKED);
4698 SetDictInt("BUTTON4_TRIPLE_CLICKED", BUTTON4_TRIPLE_CLICKED);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004699
Victor Stinner26486ea2010-05-15 22:23:53 +00004700 SetDictInt("BUTTON_SHIFT", BUTTON_SHIFT);
4701 SetDictInt("BUTTON_CTRL", BUTTON_CTRL);
4702 SetDictInt("BUTTON_ALT", BUTTON_ALT);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004703
Victor Stinner26486ea2010-05-15 22:23:53 +00004704 SetDictInt("ALL_MOUSE_EVENTS", ALL_MOUSE_EVENTS);
4705 SetDictInt("REPORT_MOUSE_POSITION", REPORT_MOUSE_POSITION);
Andrew M. Kuchling8e9d23b2000-06-27 03:10:38 +00004706#endif
Victor Stinner26486ea2010-05-15 22:23:53 +00004707 /* Now set everything up for KEY_ variables */
4708 {
4709 int key;
4710 char *key_n;
4711 char *key_n2;
Victor Stinner26486ea2010-05-15 22:23:53 +00004712 for (key=KEY_MIN;key < KEY_MAX; key++) {
4713 key_n = (char *)keyname(key);
4714 if (key_n == NULL || strcmp(key_n,"UNKNOWN KEY")==0)
4715 continue;
4716 if (strncmp(key_n,"KEY_F(",6)==0) {
4717 char *p1, *p2;
Victor Stinnerb6404912013-07-07 16:21:41 +02004718 key_n2 = PyMem_Malloc(strlen(key_n)+1);
Victor Stinner26486ea2010-05-15 22:23:53 +00004719 if (!key_n2) {
4720 PyErr_NoMemory();
4721 break;
4722 }
4723 p1 = key_n;
4724 p2 = key_n2;
4725 while (*p1) {
4726 if (*p1 != '(' && *p1 != ')') {
4727 *p2 = *p1;
4728 p2++;
4729 }
4730 p1++;
4731 }
4732 *p2 = (char)0;
4733 } else
4734 key_n2 = key_n;
4735 SetDictInt(key_n2,key);
4736 if (key_n2 != key_n)
Victor Stinnerb6404912013-07-07 16:21:41 +02004737 PyMem_Free(key_n2);
Victor Stinner26486ea2010-05-15 22:23:53 +00004738 }
Victor Stinner26486ea2010-05-15 22:23:53 +00004739 SetDictInt("KEY_MIN", KEY_MIN);
4740 SetDictInt("KEY_MAX", KEY_MAX);
4741 }
Serhiy Storchakab00854c2018-05-10 11:27:23 +03004742
Dong-hee Na37fcbb62020-03-25 07:08:51 +09004743 if (PyModule_AddType(m, &PyCursesWindow_Type) < 0) {
4744 return NULL;
4745 }
Victor Stinner26486ea2010-05-15 22:23:53 +00004746 return m;
Guido van Rossumf6971e21994-08-30 12:25:20 +00004747}