Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1 | /* |
Andrew M. Kuchling | 03e644b | 2000-05-23 16:24:54 +0000 | [diff] [blame] | 2 | * This is a curses module for Python. |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3 | * |
Andrew M. Kuchling | 03e644b | 2000-05-23 16:24:54 +0000 | [diff] [blame] | 4 | * 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. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 7 | * |
Andrew M. Kuchling | 03e644b | 2000-05-23 16:24:54 +0000 | [diff] [blame] | 8 | * Version 1.5b1, heavily extended for ncurses by Oliver Andrich: |
| 9 | * Copyright 1996,1997 by Oliver Andrich, Koblenz, Germany. |
| 10 | * |
Andrew M. Kuchling | 3adefcc | 2002-10-30 21:08:34 +0000 | [diff] [blame] | 11 | * Tidied for Python 1.6, and currently maintained by <amk@amk.ca>. |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 12 | * |
| 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 Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 33 | |
Andrew M. Kuchling | fb6bbf5 | 2000-07-27 11:58:01 +0000 | [diff] [blame] | 34 | /* |
| 35 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 36 | 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. Kuchling | fb6bbf5 | 2000-07-27 11:58:01 +0000 | [diff] [blame] | 40 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 41 | Here's a list of currently unsupported functions: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 42 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 43 | 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. Kuchling | fb6bbf5 | 2000-07-27 11:58:01 +0000 | [diff] [blame] | 53 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 54 | 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. Kuchling | fb6bbf5 | 2000-07-27 11:58:01 +0000 | [diff] [blame] | 58 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 59 | 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. Kuchling | 2824d7f | 2000-12-15 00:44:02 +0000 | [diff] [blame] | 74 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 75 | 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. Kuchling | 2824d7f | 2000-12-15 00:44:02 +0000 | [diff] [blame] | 93 | |
| 94 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 95 | */ |
Andrew M. Kuchling | fb6bbf5 | 2000-07-27 11:58:01 +0000 | [diff] [blame] | 96 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 97 | /* Release Number */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 98 | |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 99 | static const char PyCursesVersion[] = "2.2"; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 100 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 101 | /* Includes */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 102 | |
Mark Dickinson | e047cfa | 2010-05-11 17:57:09 +0000 | [diff] [blame] | 103 | #define PY_SSIZE_T_CLEAN |
| 104 | |
Guido van Rossum | 602099a | 1994-09-14 13:32:22 +0000 | [diff] [blame] | 105 | #include "Python.h" |
Victor Stinner | 3783413 | 2020-10-27 17:12:53 +0100 | [diff] [blame] | 106 | #include "pycore_long.h" // _PyLong_GetZero() |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 107 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 108 | |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 109 | #ifdef __hpux |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 110 | #define STRICT_SYSV_CURSES |
| 111 | #endif |
| 112 | |
Guido van Rossum | 858ca0f | 2001-04-10 19:53:37 +0000 | [diff] [blame] | 113 | #define CURSES_MODULE |
| 114 | #include "py_curses.h" |
| 115 | |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 116 | #if defined(HAVE_TERM_H) || defined(__sgi) |
| 117 | /* For termname, longname, putp, tigetflag, tigetnum, tigetstr, tparm |
| 118 | which are not declared in SysV curses and for setupterm. */ |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 119 | #include <term.h> |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 120 | /* Including <term.h> #defines many common symbols. */ |
| 121 | #undef lines |
| 122 | #undef columns |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 123 | #endif |
| 124 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 125 | #ifdef HAVE_LANGINFO_H |
| 126 | #include <langinfo.h> |
| 127 | #endif |
| 128 | |
Martin v. Löwis | 21ee409 | 2002-09-30 16:19:48 +0000 | [diff] [blame] | 129 | #if !defined(HAVE_NCURSES_H) && (defined(sgi) || defined(__sun) || defined(SCO5)) |
Andrew M. Kuchling | bf68026 | 2000-07-25 12:37:41 +0000 | [diff] [blame] | 130 | #define STRICT_SYSV_CURSES /* Don't use ncurses extensions */ |
Andrew M. Kuchling | a1e4b05 | 2000-06-27 21:49:47 +0000 | [diff] [blame] | 131 | typedef chtype attr_t; /* No attr_t type is available */ |
Guido van Rossum | 1266a01 | 1996-02-25 04:50:31 +0000 | [diff] [blame] | 132 | #endif |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 133 | |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 134 | #if defined(_AIX) |
| 135 | #define STRICT_SYSV_CURSES |
| 136 | #endif |
| 137 | |
Serhiy Storchaka | 59f9b4e | 2021-01-05 09:13:15 +0200 | [diff] [blame] | 138 | #if NCURSES_EXT_COLORS+0 && NCURSES_EXT_FUNCS+0 |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 139 | #define _NCURSES_EXTENDED_COLOR_FUNCS 1 |
| 140 | #else |
| 141 | #define _NCURSES_EXTENDED_COLOR_FUNCS 0 |
| 142 | #endif /* defined(NCURSES_EXT_COLORS) && defined(NCURSES_EXT_FUNCS) */ |
| 143 | |
| 144 | #if _NCURSES_EXTENDED_COLOR_FUNCS |
Serhiy Storchaka | 59f9b4e | 2021-01-05 09:13:15 +0200 | [diff] [blame] | 145 | #define _CURSES_COLOR_VAL_TYPE int |
| 146 | #define _CURSES_COLOR_NUM_TYPE int |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 147 | #define _CURSES_INIT_COLOR_FUNC init_extended_color |
| 148 | #define _CURSES_INIT_PAIR_FUNC init_extended_pair |
| 149 | #define _COLOR_CONTENT_FUNC extended_color_content |
Serhiy Storchaka | 59f9b4e | 2021-01-05 09:13:15 +0200 | [diff] [blame] | 150 | #define _CURSES_PAIR_CONTENT_FUNC extended_pair_content |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 151 | #else |
Serhiy Storchaka | 59f9b4e | 2021-01-05 09:13:15 +0200 | [diff] [blame] | 152 | #define _CURSES_COLOR_VAL_TYPE short |
| 153 | #define _CURSES_COLOR_NUM_TYPE short |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 154 | #define _CURSES_INIT_COLOR_FUNC init_color |
| 155 | #define _CURSES_INIT_PAIR_FUNC init_pair |
| 156 | #define _COLOR_CONTENT_FUNC color_content |
Serhiy Storchaka | 59f9b4e | 2021-01-05 09:13:15 +0200 | [diff] [blame] | 157 | #define _CURSES_PAIR_CONTENT_FUNC pair_content |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 158 | #endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ |
| 159 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 160 | /*[clinic input] |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 161 | module _curses |
| 162 | class _curses.window "PyCursesWindowObject *" "&PyCursesWindow_Type" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 163 | [clinic start generated code]*/ |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 164 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=43265c372c2887d6]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 165 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 166 | /* Definition of exception curses.error */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 167 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 168 | static PyObject *PyCursesError; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 169 | |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 170 | /* Tells whether setupterm() has been called to initialise terminfo. */ |
| 171 | static int initialised_setupterm = FALSE; |
| 172 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 173 | /* Tells whether initscr() has been called to initialise curses. */ |
Guido van Rossum | 585c6dc | 1995-02-24 13:45:43 +0000 | [diff] [blame] | 174 | static int initialised = FALSE; |
| 175 | |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 176 | /* Tells whether start_color() has been called to initialise color usage. */ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 177 | static int initialisedcolors = FALSE; |
| 178 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 179 | static char *screen_encoding = NULL; |
| 180 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 181 | /* Utility Macros */ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 182 | #define PyCursesSetupTermCalled \ |
| 183 | if (initialised_setupterm != TRUE) { \ |
| 184 | PyErr_SetString(PyCursesError, \ |
| 185 | "must call (at least) setupterm() first"); \ |
| 186 | return 0; } |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 187 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 188 | #define PyCursesInitialised \ |
| 189 | if (initialised != TRUE) { \ |
| 190 | PyErr_SetString(PyCursesError, \ |
| 191 | "must call initscr() first"); \ |
| 192 | return 0; } |
Guido van Rossum | fbea2f3 | 1994-08-31 22:05:27 +0000 | [diff] [blame] | 193 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 194 | #define PyCursesInitialisedColor \ |
| 195 | if (initialisedcolors != TRUE) { \ |
| 196 | PyErr_SetString(PyCursesError, \ |
| 197 | "must call start_color() first"); \ |
| 198 | return 0; } |
Guido van Rossum | fbea2f3 | 1994-08-31 22:05:27 +0000 | [diff] [blame] | 199 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 200 | /* Utility Functions */ |
Guido van Rossum | fbea2f3 | 1994-08-31 22:05:27 +0000 | [diff] [blame] | 201 | |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 202 | /* |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 203 | * Check the return code from a curses function and return None |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 204 | * or raise an exception as appropriate. These are exported using the |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 205 | * capsule API. |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 206 | */ |
| 207 | |
| 208 | static PyObject * |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 209 | PyCursesCheckERR(int code, const char *fname) |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 210 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 211 | if (code != ERR) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 212 | Py_RETURN_NONE; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 213 | } else { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 214 | if (fname == NULL) { |
| 215 | PyErr_SetString(PyCursesError, catchall_ERR); |
| 216 | } else { |
| 217 | PyErr_Format(PyCursesError, "%s() returned ERR", fname); |
| 218 | } |
| 219 | return NULL; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 220 | } |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 223 | /* Convert an object to a byte (an integer of type chtype): |
| 224 | |
| 225 | - int |
| 226 | - bytes of length 1 |
| 227 | - str of length 1 |
| 228 | |
| 229 | Return 1 on success, 0 on error (invalid type or integer overflow). */ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 230 | static int |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 231 | PyCurses_ConvertToChtype(PyCursesWindowObject *win, PyObject *obj, chtype *ch) |
Guido van Rossum | 585c6dc | 1995-02-24 13:45:43 +0000 | [diff] [blame] | 232 | { |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 233 | long value; |
| 234 | if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) { |
| 235 | value = (unsigned char)PyBytes_AsString(obj)[0]; |
| 236 | } |
| 237 | else if (PyUnicode_Check(obj)) { |
| 238 | if (PyUnicode_GetLength(obj) != 1) { |
| 239 | PyErr_Format(PyExc_TypeError, |
| 240 | "expect bytes or str of length 1, or int, " |
| 241 | "got a str of length %zi", |
| 242 | PyUnicode_GET_LENGTH(obj)); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 243 | return 0; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 244 | } |
| 245 | value = PyUnicode_READ_CHAR(obj, 0); |
| 246 | if (128 < value) { |
| 247 | PyObject *bytes; |
| 248 | const char *encoding; |
| 249 | if (win) |
| 250 | encoding = win->encoding; |
| 251 | else |
| 252 | encoding = screen_encoding; |
Serhiy Storchaka | b29cee4 | 2016-10-27 19:31:49 +0300 | [diff] [blame] | 253 | bytes = PyUnicode_AsEncodedString(obj, encoding, NULL); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 254 | if (bytes == NULL) |
| 255 | return 0; |
| 256 | if (PyBytes_GET_SIZE(bytes) == 1) |
| 257 | value = (unsigned char)PyBytes_AS_STRING(bytes)[0]; |
| 258 | else |
| 259 | value = -1; |
| 260 | Py_DECREF(bytes); |
| 261 | if (value < 0) |
| 262 | goto overflow; |
| 263 | } |
| 264 | } |
| 265 | else if (PyLong_CheckExact(obj)) { |
| 266 | int long_overflow; |
| 267 | value = PyLong_AsLongAndOverflow(obj, &long_overflow); |
| 268 | if (long_overflow) |
| 269 | goto overflow; |
| 270 | } |
| 271 | else { |
| 272 | PyErr_Format(PyExc_TypeError, |
| 273 | "expect bytes or str of length 1, or int, got %s", |
| 274 | Py_TYPE(obj)->tp_name); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 275 | return 0; |
| 276 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 277 | *ch = (chtype)value; |
| 278 | if ((long)*ch != value) |
| 279 | goto overflow; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 280 | return 1; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 281 | |
| 282 | overflow: |
| 283 | PyErr_SetString(PyExc_OverflowError, |
| 284 | "byte doesn't fit in chtype"); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | /* Convert an object to a byte (chtype) or a character (cchar_t): |
| 289 | |
| 290 | - int |
| 291 | - bytes of length 1 |
| 292 | - str of length 1 |
| 293 | |
| 294 | Return: |
| 295 | |
| 296 | - 2 if obj is a character (written into *wch) |
| 297 | - 1 if obj is a byte (written into *ch) |
| 298 | - 0 on error: raise an exception */ |
| 299 | static int |
| 300 | PyCurses_ConvertToCchar_t(PyCursesWindowObject *win, PyObject *obj, |
| 301 | chtype *ch |
| 302 | #ifdef HAVE_NCURSESW |
Serhiy Storchaka | 0bcd89b | 2016-10-30 22:52:06 +0200 | [diff] [blame] | 303 | , wchar_t *wch |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 304 | #endif |
| 305 | ) |
| 306 | { |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 307 | long value; |
| 308 | #ifdef HAVE_NCURSESW |
| 309 | wchar_t buffer[2]; |
| 310 | #endif |
| 311 | |
| 312 | if (PyUnicode_Check(obj)) { |
| 313 | #ifdef HAVE_NCURSESW |
| 314 | if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) { |
| 315 | PyErr_Format(PyExc_TypeError, |
| 316 | "expect bytes or str of length 1, or int, " |
| 317 | "got a str of length %zi", |
| 318 | PyUnicode_GET_LENGTH(obj)); |
| 319 | return 0; |
| 320 | } |
Serhiy Storchaka | 0bcd89b | 2016-10-30 22:52:06 +0200 | [diff] [blame] | 321 | *wch = buffer[0]; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 322 | return 2; |
| 323 | #else |
| 324 | return PyCurses_ConvertToChtype(win, obj, ch); |
| 325 | #endif |
| 326 | } |
| 327 | else if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) { |
| 328 | value = (unsigned char)PyBytes_AsString(obj)[0]; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 329 | } |
| 330 | else if (PyLong_CheckExact(obj)) { |
| 331 | int overflow; |
| 332 | value = PyLong_AsLongAndOverflow(obj, &overflow); |
| 333 | if (overflow) { |
| 334 | PyErr_SetString(PyExc_OverflowError, |
| 335 | "int doesn't fit in long"); |
| 336 | return 0; |
| 337 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 338 | } |
| 339 | else { |
| 340 | PyErr_Format(PyExc_TypeError, |
| 341 | "expect bytes or str of length 1, or int, got %s", |
| 342 | Py_TYPE(obj)->tp_name); |
| 343 | return 0; |
| 344 | } |
Victor Stinner | 9f16e44 | 2012-09-01 15:00:34 +0200 | [diff] [blame] | 345 | |
| 346 | *ch = (chtype)value; |
| 347 | if ((long)*ch != value) { |
| 348 | PyErr_Format(PyExc_OverflowError, |
| 349 | "byte doesn't fit in chtype"); |
| 350 | return 0; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 351 | } |
Victor Stinner | 9f16e44 | 2012-09-01 15:00:34 +0200 | [diff] [blame] | 352 | return 1; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /* Convert an object to a byte string (char*) or a wide character string |
| 356 | (wchar_t*). Return: |
| 357 | |
| 358 | - 2 if obj is a character string (written into *wch) |
| 359 | - 1 if obj is a byte string (written into *bytes) |
| 360 | - 0 on error: raise an exception */ |
| 361 | static int |
| 362 | PyCurses_ConvertToString(PyCursesWindowObject *win, PyObject *obj, |
| 363 | PyObject **bytes, wchar_t **wstr) |
| 364 | { |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 365 | char *str; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 366 | if (PyUnicode_Check(obj)) { |
| 367 | #ifdef HAVE_NCURSESW |
| 368 | assert (wstr != NULL); |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 369 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 370 | *wstr = PyUnicode_AsWideCharString(obj, NULL); |
| 371 | if (*wstr == NULL) |
| 372 | return 0; |
| 373 | return 2; |
| 374 | #else |
| 375 | assert (wstr == NULL); |
Serhiy Storchaka | b29cee4 | 2016-10-27 19:31:49 +0300 | [diff] [blame] | 376 | *bytes = PyUnicode_AsEncodedString(obj, win->encoding, NULL); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 377 | if (*bytes == NULL) |
| 378 | return 0; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 379 | /* check for embedded null bytes */ |
| 380 | if (PyBytes_AsStringAndSize(*bytes, &str, NULL) < 0) { |
| 381 | return 0; |
| 382 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 383 | return 1; |
| 384 | #endif |
| 385 | } |
| 386 | else if (PyBytes_Check(obj)) { |
| 387 | Py_INCREF(obj); |
| 388 | *bytes = obj; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 389 | /* check for embedded null bytes */ |
| 390 | if (PyBytes_AsStringAndSize(*bytes, &str, NULL) < 0) { |
| 391 | return 0; |
| 392 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 393 | return 1; |
| 394 | } |
| 395 | |
| 396 | PyErr_Format(PyExc_TypeError, "expect bytes or str, got %s", |
| 397 | Py_TYPE(obj)->tp_name); |
| 398 | return 0; |
Guido van Rossum | 585c6dc | 1995-02-24 13:45:43 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 401 | static int |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 402 | color_allow_default_converter(PyObject *arg, void *ptr) |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 403 | { |
| 404 | long color_number; |
| 405 | int overflow; |
| 406 | |
| 407 | color_number = PyLong_AsLongAndOverflow(arg, &overflow); |
| 408 | if (color_number == -1 && PyErr_Occurred()) |
| 409 | return 0; |
| 410 | |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 411 | if (overflow > 0 || color_number >= COLORS) { |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 412 | PyErr_Format(PyExc_ValueError, |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 413 | "Color number is greater than COLORS-1 (%d).", |
| 414 | COLORS - 1); |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | else if (overflow < 0 || color_number < 0) { |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 418 | color_number = -1; |
| 419 | } |
| 420 | |
| 421 | *(int *)ptr = (int)color_number; |
| 422 | return 1; |
| 423 | } |
| 424 | |
| 425 | static int |
| 426 | color_converter(PyObject *arg, void *ptr) |
| 427 | { |
| 428 | if (!color_allow_default_converter(arg, ptr)) { |
| 429 | return 0; |
| 430 | } |
| 431 | if (*(int *)ptr < 0) { |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 432 | PyErr_SetString(PyExc_ValueError, |
| 433 | "Color number is less than 0."); |
| 434 | return 0; |
| 435 | } |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 436 | return 1; |
| 437 | } |
| 438 | |
| 439 | /*[python input] |
| 440 | class color_converter(CConverter): |
| 441 | type = 'int' |
| 442 | converter = 'color_converter' |
| 443 | [python start generated code]*/ |
| 444 | /*[python end generated code: output=da39a3ee5e6b4b0d input=4260d2b6e66b3709]*/ |
| 445 | |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 446 | /*[python input] |
| 447 | class color_allow_default_converter(CConverter): |
| 448 | type = 'int' |
| 449 | converter = 'color_allow_default_converter' |
| 450 | [python start generated code]*/ |
| 451 | /*[python end generated code: output=da39a3ee5e6b4b0d input=975602bc058a872d]*/ |
| 452 | |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 453 | static int |
| 454 | pair_converter(PyObject *arg, void *ptr) |
| 455 | { |
| 456 | long pair_number; |
| 457 | int overflow; |
| 458 | |
| 459 | pair_number = PyLong_AsLongAndOverflow(arg, &overflow); |
| 460 | if (pair_number == -1 && PyErr_Occurred()) |
| 461 | return 0; |
| 462 | |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 463 | #if _NCURSES_EXTENDED_COLOR_FUNCS |
| 464 | if (overflow > 0 || pair_number > INT_MAX) { |
| 465 | PyErr_Format(PyExc_ValueError, |
| 466 | "Color pair is greater than maximum (%d).", |
| 467 | INT_MAX); |
| 468 | return 0; |
| 469 | } |
| 470 | #else |
| 471 | if (overflow > 0 || pair_number >= COLOR_PAIRS) { |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 472 | PyErr_Format(PyExc_ValueError, |
| 473 | "Color pair is greater than COLOR_PAIRS-1 (%d).", |
| 474 | COLOR_PAIRS - 1); |
| 475 | return 0; |
| 476 | } |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 477 | #endif |
| 478 | else if (overflow < 0 || pair_number < 0) { |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 479 | PyErr_SetString(PyExc_ValueError, |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 480 | "Color pair is less than 0."); |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | *(int *)ptr = (int)pair_number; |
| 485 | return 1; |
| 486 | } |
| 487 | |
| 488 | /*[python input] |
| 489 | class pair_converter(CConverter): |
| 490 | type = 'int' |
| 491 | converter = 'pair_converter' |
| 492 | [python start generated code]*/ |
| 493 | /*[python end generated code: output=da39a3ee5e6b4b0d input=1a918ae6a1b32af7]*/ |
| 494 | |
| 495 | static int |
| 496 | component_converter(PyObject *arg, void *ptr) |
| 497 | { |
| 498 | long component; |
| 499 | int overflow; |
| 500 | |
| 501 | component = PyLong_AsLongAndOverflow(arg, &overflow); |
| 502 | if (component == -1 && PyErr_Occurred()) |
| 503 | return 0; |
| 504 | |
| 505 | if (overflow > 0 || component > 1000) { |
| 506 | PyErr_SetString(PyExc_ValueError, |
| 507 | "Color component is greater than 1000"); |
| 508 | return 0; |
| 509 | } |
| 510 | else if (overflow < 0 || component < 0) { |
| 511 | PyErr_SetString(PyExc_ValueError, |
| 512 | "Color component is less than 0"); |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | *(short *)ptr = (short)component; |
| 517 | return 1; |
| 518 | } |
| 519 | |
| 520 | /*[python input] |
| 521 | class component_converter(CConverter): |
| 522 | type = 'short' |
| 523 | converter = 'component_converter' |
| 524 | [python start generated code]*/ |
| 525 | /*[python end generated code: output=da39a3ee5e6b4b0d input=38e9be01d33927fb]*/ |
| 526 | |
Benjamin Peterson | 21896a3 | 2010-03-21 22:03:03 +0000 | [diff] [blame] | 527 | /* Function versions of the 3 functions for testing whether curses has been |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 528 | initialised or not. */ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 529 | |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 530 | static int func_PyCursesSetupTermCalled(void) |
| 531 | { |
| 532 | PyCursesSetupTermCalled; |
| 533 | return 1; |
| 534 | } |
| 535 | |
| 536 | static int func_PyCursesInitialised(void) |
| 537 | { |
| 538 | PyCursesInitialised; |
| 539 | return 1; |
| 540 | } |
| 541 | |
| 542 | static int func_PyCursesInitialisedColor(void) |
| 543 | { |
| 544 | PyCursesInitialisedColor; |
| 545 | return 1; |
| 546 | } |
| 547 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 548 | /***************************************************************************** |
| 549 | The Window Object |
| 550 | ******************************************************************************/ |
Guido van Rossum | 585c6dc | 1995-02-24 13:45:43 +0000 | [diff] [blame] | 551 | |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 552 | /* Definition of the window type */ |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 553 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 554 | PyTypeObject PyCursesWindow_Type; |
| 555 | |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 556 | /* Function prototype macros for Window object |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 557 | |
| 558 | X - function name |
| 559 | TYPE - parameter Type |
| 560 | ERGSTR - format string for construction of the return value |
| 561 | PARSESTR - format string for argument parsing |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 562 | */ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 563 | |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 564 | #define Window_NoArgNoReturnFunction(X) \ |
| 565 | static PyObject *PyCursesWindow_ ## X \ |
| 566 | (PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 567 | { return PyCursesCheckERR(X(self->win), # X); } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 568 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 569 | #define Window_NoArgTrueFalseFunction(X) \ |
| 570 | static PyObject * PyCursesWindow_ ## X \ |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 571 | (PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 572 | { \ |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 573 | return PyBool_FromLong(X(self->win)); } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 574 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 575 | #define Window_NoArgNoReturnVoidFunction(X) \ |
| 576 | static PyObject * PyCursesWindow_ ## X \ |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 577 | (PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 578 | { \ |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 579 | X(self->win); Py_RETURN_NONE; } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 580 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 581 | #define Window_NoArg2TupleReturnFunction(X, TYPE, ERGSTR) \ |
| 582 | static PyObject * PyCursesWindow_ ## X \ |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 583 | (PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 584 | { \ |
| 585 | TYPE arg1, arg2; \ |
| 586 | X(self->win,arg1,arg2); return Py_BuildValue(ERGSTR, arg1, arg2); } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 587 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 588 | #define Window_OneArgNoReturnVoidFunction(X, TYPE, PARSESTR) \ |
| 589 | static PyObject * PyCursesWindow_ ## X \ |
| 590 | (PyCursesWindowObject *self, PyObject *args) \ |
| 591 | { \ |
| 592 | TYPE arg1; \ |
| 593 | if (!PyArg_ParseTuple(args, PARSESTR, &arg1)) return NULL; \ |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 594 | X(self->win,arg1); Py_RETURN_NONE; } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 595 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 596 | #define Window_OneArgNoReturnFunction(X, TYPE, PARSESTR) \ |
| 597 | static PyObject * PyCursesWindow_ ## X \ |
| 598 | (PyCursesWindowObject *self, PyObject *args) \ |
| 599 | { \ |
| 600 | TYPE arg1; \ |
| 601 | if (!PyArg_ParseTuple(args,PARSESTR, &arg1)) return NULL; \ |
| 602 | return PyCursesCheckERR(X(self->win, arg1), # X); } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 603 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 604 | #define Window_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \ |
| 605 | static PyObject * PyCursesWindow_ ## X \ |
| 606 | (PyCursesWindowObject *self, PyObject *args) \ |
| 607 | { \ |
| 608 | TYPE arg1, arg2; \ |
| 609 | if (!PyArg_ParseTuple(args,PARSESTR, &arg1, &arg2)) return NULL; \ |
| 610 | return PyCursesCheckERR(X(self->win, arg1, arg2), # X); } |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 611 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 612 | /* ------------- WINDOW routines --------------- */ |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 613 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 614 | Window_NoArgNoReturnFunction(untouchwin) |
| 615 | Window_NoArgNoReturnFunction(touchwin) |
| 616 | Window_NoArgNoReturnFunction(redrawwin) |
| 617 | Window_NoArgNoReturnFunction(winsertln) |
| 618 | Window_NoArgNoReturnFunction(werase) |
| 619 | Window_NoArgNoReturnFunction(wdeleteln) |
| 620 | |
| 621 | Window_NoArgTrueFalseFunction(is_wintouched) |
| 622 | |
| 623 | Window_NoArgNoReturnVoidFunction(wsyncup) |
| 624 | Window_NoArgNoReturnVoidFunction(wsyncdown) |
| 625 | Window_NoArgNoReturnVoidFunction(wstandend) |
| 626 | Window_NoArgNoReturnVoidFunction(wstandout) |
| 627 | Window_NoArgNoReturnVoidFunction(wcursyncup) |
| 628 | Window_NoArgNoReturnVoidFunction(wclrtoeol) |
| 629 | Window_NoArgNoReturnVoidFunction(wclrtobot) |
| 630 | Window_NoArgNoReturnVoidFunction(wclear) |
| 631 | |
| 632 | Window_OneArgNoReturnVoidFunction(idcok, int, "i;True(1) or False(0)") |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 633 | #ifdef HAVE_CURSES_IMMEDOK |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 634 | Window_OneArgNoReturnVoidFunction(immedok, int, "i;True(1) or False(0)") |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 635 | #endif |
Andrew M. Kuchling | 97311bb | 2000-06-21 01:41:48 +0000 | [diff] [blame] | 636 | Window_OneArgNoReturnVoidFunction(wtimeout, int, "i;delay") |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 637 | |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 638 | Window_NoArg2TupleReturnFunction(getyx, int, "ii") |
| 639 | Window_NoArg2TupleReturnFunction(getbegyx, int, "ii") |
| 640 | Window_NoArg2TupleReturnFunction(getmaxyx, int, "ii") |
| 641 | Window_NoArg2TupleReturnFunction(getparyx, int, "ii") |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 642 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 643 | Window_OneArgNoReturnFunction(clearok, int, "i;True(1) or False(0)") |
| 644 | Window_OneArgNoReturnFunction(idlok, int, "i;True(1) or False(0)") |
| 645 | Window_OneArgNoReturnFunction(keypad, int, "i;True(1) or False(0)") |
| 646 | Window_OneArgNoReturnFunction(leaveok, int, "i;True(1) or False(0)") |
| 647 | Window_OneArgNoReturnFunction(nodelay, int, "i;True(1) or False(0)") |
| 648 | Window_OneArgNoReturnFunction(notimeout, int, "i;True(1) or False(0)") |
| 649 | Window_OneArgNoReturnFunction(scrollok, int, "i;True(1) or False(0)") |
Andrew M. Kuchling | 03e644b | 2000-05-23 16:24:54 +0000 | [diff] [blame] | 650 | Window_OneArgNoReturnFunction(winsdelln, int, "i;nlines") |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 651 | #ifdef HAVE_CURSES_SYNCOK |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 652 | Window_OneArgNoReturnFunction(syncok, int, "i;True(1) or False(0)") |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 653 | #endif |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 654 | |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 655 | Window_TwoArgNoReturnFunction(mvwin, int, "ii;y,x") |
| 656 | Window_TwoArgNoReturnFunction(mvderwin, int, "ii;y,x") |
| 657 | Window_TwoArgNoReturnFunction(wmove, int, "ii;y,x") |
Andrew M. Kuchling | a1e4b05 | 2000-06-27 21:49:47 +0000 | [diff] [blame] | 658 | #ifndef STRICT_SYSV_CURSES |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 659 | Window_TwoArgNoReturnFunction(wresize, int, "ii;lines,columns") |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 660 | #endif |
| 661 | |
Andrew M. Kuchling | 03e644b | 2000-05-23 16:24:54 +0000 | [diff] [blame] | 662 | /* Allocation and deallocation of Window Objects */ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 663 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 664 | static PyObject * |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 665 | PyCursesWindow_New(WINDOW *win, const char *encoding) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 666 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 667 | PyCursesWindowObject *wo; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 668 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 669 | if (encoding == NULL) { |
| 670 | #if defined(MS_WINDOWS) |
| 671 | char *buffer[100]; |
| 672 | UINT cp; |
| 673 | cp = GetConsoleOutputCP(); |
| 674 | if (cp != 0) { |
| 675 | PyOS_snprintf(buffer, sizeof(buffer), "cp%u", cp); |
| 676 | encoding = buffer; |
| 677 | } |
| 678 | #elif defined(CODESET) |
| 679 | const char *codeset = nl_langinfo(CODESET); |
| 680 | if (codeset != NULL && codeset[0] != 0) |
| 681 | encoding = codeset; |
| 682 | #endif |
| 683 | if (encoding == NULL) |
| 684 | encoding = "utf-8"; |
| 685 | } |
| 686 | |
Victor Stinner | 9205520 | 2020-04-08 00:38:15 +0200 | [diff] [blame] | 687 | wo = PyObject_New(PyCursesWindowObject, &PyCursesWindow_Type); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 688 | if (wo == NULL) return NULL; |
| 689 | wo->win = win; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 690 | wo->encoding = _PyMem_Strdup(encoding); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 691 | if (wo->encoding == NULL) { |
| 692 | Py_DECREF(wo); |
| 693 | PyErr_NoMemory(); |
| 694 | return NULL; |
| 695 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 696 | return (PyObject *)wo; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | static void |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 700 | PyCursesWindow_Dealloc(PyCursesWindowObject *wo) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 701 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 702 | if (wo->win != stdscr) delwin(wo->win); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 703 | if (wo->encoding != NULL) |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 704 | PyMem_Free(wo->encoding); |
Victor Stinner | 32bd68c | 2020-12-01 10:37:39 +0100 | [diff] [blame] | 705 | PyObject_Free(wo); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 708 | /* Addch, Addstr, Addnstr */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 709 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 710 | /*[clinic input] |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 711 | _curses.window.addch |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 712 | |
| 713 | [ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 714 | y: int |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 715 | Y-coordinate. |
Larry Hastings | 9147a96 | 2014-05-04 04:41:18 -0700 | [diff] [blame] | 716 | x: int |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 717 | X-coordinate. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 718 | ] |
| 719 | |
| 720 | ch: object |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 721 | Character to add. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 722 | |
| 723 | [ |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 724 | attr: long(c_default="A_NORMAL") = _curses.A_NORMAL |
| 725 | Attributes for the character. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 726 | ] |
| 727 | / |
| 728 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 729 | Paint the character. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 730 | |
| 731 | Paint character ch at (y, x) with attributes attr, |
| 732 | overwriting any character previously painted at that location. |
| 733 | By default, the character position and attributes are the |
| 734 | current settings for the window object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 735 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 736 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 737 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 738 | _curses_window_addch_impl(PyCursesWindowObject *self, int group_left_1, |
| 739 | int y, int x, PyObject *ch, int group_right_1, |
| 740 | long attr) |
| 741 | /*[clinic end generated code: output=00f4c37af3378f45 input=95ce131578458196]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 742 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 743 | int coordinates_group = group_left_1; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 744 | int rtn; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 745 | int type; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 746 | chtype cch = 0; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 747 | #ifdef HAVE_NCURSESW |
Serhiy Storchaka | 0bcd89b | 2016-10-30 22:52:06 +0200 | [diff] [blame] | 748 | wchar_t wstr[2]; |
| 749 | cchar_t wcval; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 750 | #endif |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 751 | const char *funcname; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 752 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 753 | #ifdef HAVE_NCURSESW |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 754 | type = PyCurses_ConvertToCchar_t(self, ch, &cch, wstr); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 755 | if (type == 2) { |
| 756 | funcname = "add_wch"; |
Serhiy Storchaka | 0bcd89b | 2016-10-30 22:52:06 +0200 | [diff] [blame] | 757 | wstr[1] = L'\0'; |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 758 | setcchar(&wcval, wstr, attr, PAIR_NUMBER(attr), NULL); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 759 | if (coordinates_group) |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 760 | rtn = mvwadd_wch(self->win,y,x, &wcval); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 761 | else { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 762 | rtn = wadd_wch(self->win, &wcval); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | else |
| 766 | #else |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 767 | type = PyCurses_ConvertToCchar_t(self, ch, &cch); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 768 | #endif |
| 769 | if (type == 1) { |
| 770 | funcname = "addch"; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 771 | if (coordinates_group) |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 772 | rtn = mvwaddch(self->win,y,x, cch | (attr_t) attr); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 773 | else { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 774 | rtn = waddch(self->win, cch | (attr_t) attr); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | else { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 778 | return NULL; |
| 779 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 780 | return PyCursesCheckERR(rtn, funcname); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 783 | /*[clinic input] |
| 784 | _curses.window.addstr |
| 785 | |
| 786 | [ |
| 787 | y: int |
| 788 | Y-coordinate. |
| 789 | x: int |
| 790 | X-coordinate. |
| 791 | ] |
| 792 | |
| 793 | str: object |
| 794 | String to add. |
| 795 | |
| 796 | [ |
| 797 | attr: long |
| 798 | Attributes for characters. |
| 799 | ] |
| 800 | / |
| 801 | |
| 802 | Paint the string. |
| 803 | |
| 804 | Paint the string str at (y, x) with attributes attr, |
| 805 | overwriting anything previously on the display. |
| 806 | By default, the character position and attributes are the |
| 807 | current settings for the window object. |
| 808 | [clinic start generated code]*/ |
| 809 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 810 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 811 | _curses_window_addstr_impl(PyCursesWindowObject *self, int group_left_1, |
| 812 | int y, int x, PyObject *str, int group_right_1, |
| 813 | long attr) |
| 814 | /*[clinic end generated code: output=65a928ea85ff3115 input=ff6cbb91448a22a3]*/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 815 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 816 | int rtn; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 817 | int strtype; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 818 | PyObject *bytesobj = NULL; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 819 | #ifdef HAVE_NCURSESW |
| 820 | wchar_t *wstr = NULL; |
| 821 | #endif |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 822 | attr_t attr_old = A_NORMAL; |
| 823 | int use_xy = group_left_1, use_attr = group_right_1; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 824 | const char *funcname; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 825 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 826 | #ifdef HAVE_NCURSESW |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 827 | strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 828 | #else |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 829 | strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 830 | #endif |
| 831 | if (strtype == 0) |
| 832 | return NULL; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 833 | if (use_attr) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 834 | attr_old = getattrs(self->win); |
| 835 | (void)wattrset(self->win,attr); |
| 836 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 837 | #ifdef HAVE_NCURSESW |
| 838 | if (strtype == 2) { |
| 839 | funcname = "addwstr"; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 840 | if (use_xy) |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 841 | rtn = mvwaddwstr(self->win,y,x,wstr); |
| 842 | else |
| 843 | rtn = waddwstr(self->win,wstr); |
| 844 | PyMem_Free(wstr); |
| 845 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 846 | else |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 847 | #endif |
| 848 | { |
Serhiy Storchaka | 8f87eef | 2020-04-12 14:58:27 +0300 | [diff] [blame] | 849 | const char *str = PyBytes_AS_STRING(bytesobj); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 850 | funcname = "addstr"; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 851 | if (use_xy) |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 852 | rtn = mvwaddstr(self->win,y,x,str); |
| 853 | else |
| 854 | rtn = waddstr(self->win,str); |
| 855 | Py_DECREF(bytesobj); |
| 856 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 857 | if (use_attr) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 858 | (void)wattrset(self->win,attr_old); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 859 | return PyCursesCheckERR(rtn, funcname); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 860 | } |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 861 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 862 | /*[clinic input] |
| 863 | _curses.window.addnstr |
| 864 | |
| 865 | [ |
| 866 | y: int |
| 867 | Y-coordinate. |
| 868 | x: int |
| 869 | X-coordinate. |
| 870 | ] |
| 871 | |
| 872 | str: object |
| 873 | String to add. |
| 874 | |
| 875 | n: int |
| 876 | Maximal number of characters. |
| 877 | |
| 878 | [ |
| 879 | attr: long |
| 880 | Attributes for characters. |
| 881 | ] |
| 882 | / |
| 883 | |
| 884 | Paint at most n characters of the string. |
| 885 | |
| 886 | Paint at most n characters of the string str at (y, x) with |
| 887 | attributes attr, overwriting anything previously on the display. |
| 888 | By default, the character position and attributes are the |
| 889 | current settings for the window object. |
| 890 | [clinic start generated code]*/ |
| 891 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 892 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 893 | _curses_window_addnstr_impl(PyCursesWindowObject *self, int group_left_1, |
| 894 | int y, int x, PyObject *str, int n, |
| 895 | int group_right_1, long attr) |
| 896 | /*[clinic end generated code: output=6d21cee2ce6876d9 input=72718415c2744a2a]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 897 | { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 898 | int rtn; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 899 | int strtype; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 900 | PyObject *bytesobj = NULL; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 901 | #ifdef HAVE_NCURSESW |
| 902 | wchar_t *wstr = NULL; |
| 903 | #endif |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 904 | attr_t attr_old = A_NORMAL; |
| 905 | int use_xy = group_left_1, use_attr = group_right_1; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 906 | const char *funcname; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 907 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 908 | #ifdef HAVE_NCURSESW |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 909 | strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 910 | #else |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 911 | strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 912 | #endif |
| 913 | if (strtype == 0) |
| 914 | return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 915 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 916 | if (use_attr) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 917 | attr_old = getattrs(self->win); |
| 918 | (void)wattrset(self->win,attr); |
| 919 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 920 | #ifdef HAVE_NCURSESW |
| 921 | if (strtype == 2) { |
| 922 | funcname = "addnwstr"; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 923 | if (use_xy) |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 924 | rtn = mvwaddnwstr(self->win,y,x,wstr,n); |
| 925 | else |
| 926 | rtn = waddnwstr(self->win,wstr,n); |
| 927 | PyMem_Free(wstr); |
| 928 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 929 | else |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 930 | #endif |
| 931 | { |
Serhiy Storchaka | 8f87eef | 2020-04-12 14:58:27 +0300 | [diff] [blame] | 932 | const char *str = PyBytes_AS_STRING(bytesobj); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 933 | funcname = "addnstr"; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 934 | if (use_xy) |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 935 | rtn = mvwaddnstr(self->win,y,x,str,n); |
| 936 | else |
| 937 | rtn = waddnstr(self->win,str,n); |
| 938 | Py_DECREF(bytesobj); |
| 939 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 940 | if (use_attr) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 941 | (void)wattrset(self->win,attr_old); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 942 | return PyCursesCheckERR(rtn, funcname); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 945 | /*[clinic input] |
| 946 | _curses.window.bkgd |
| 947 | |
| 948 | ch: object |
| 949 | Background character. |
| 950 | attr: long(c_default="A_NORMAL") = _curses.A_NORMAL |
| 951 | Background attributes. |
| 952 | / |
| 953 | |
| 954 | Set the background property of the window. |
| 955 | [clinic start generated code]*/ |
| 956 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 957 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 958 | _curses_window_bkgd_impl(PyCursesWindowObject *self, PyObject *ch, long attr) |
| 959 | /*[clinic end generated code: output=058290afb2cf4034 input=634015bcb339283d]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 960 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 961 | chtype bkgd; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 962 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 963 | if (!PyCurses_ConvertToChtype(self, ch, &bkgd)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 964 | return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 965 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 966 | return PyCursesCheckERR(wbkgd(self->win, bkgd | attr), "bkgd"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 969 | /*[clinic input] |
| 970 | _curses.window.attroff |
| 971 | |
| 972 | attr: long |
| 973 | / |
| 974 | |
| 975 | Remove attribute attr from the "background" set. |
| 976 | [clinic start generated code]*/ |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 977 | |
| 978 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 979 | _curses_window_attroff_impl(PyCursesWindowObject *self, long attr) |
| 980 | /*[clinic end generated code: output=8a2fcd4df682fc64 input=786beedf06a7befe]*/ |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 981 | { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 982 | return PyCursesCheckERR(wattroff(self->win, (attr_t)attr), "attroff"); |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 985 | /*[clinic input] |
| 986 | _curses.window.attron |
| 987 | |
| 988 | attr: long |
| 989 | / |
| 990 | |
| 991 | Add attribute attr from the "background" set. |
| 992 | [clinic start generated code]*/ |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 993 | |
| 994 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 995 | _curses_window_attron_impl(PyCursesWindowObject *self, long attr) |
| 996 | /*[clinic end generated code: output=7afea43b237fa870 input=5a88fba7b1524f32]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 997 | { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 998 | return PyCursesCheckERR(wattron(self->win, (attr_t)attr), "attron"); |
| 999 | } |
| 1000 | |
| 1001 | /*[clinic input] |
| 1002 | _curses.window.attrset |
| 1003 | |
| 1004 | attr: long |
| 1005 | / |
| 1006 | |
| 1007 | Set the "background" set of attributes. |
| 1008 | [clinic start generated code]*/ |
| 1009 | |
| 1010 | static PyObject * |
| 1011 | _curses_window_attrset_impl(PyCursesWindowObject *self, long attr) |
| 1012 | /*[clinic end generated code: output=84e379bff20c0433 input=42e400c0d0154ab5]*/ |
| 1013 | { |
| 1014 | return PyCursesCheckERR(wattrset(self->win, (attr_t)attr), "attrset"); |
| 1015 | } |
| 1016 | |
| 1017 | /*[clinic input] |
| 1018 | _curses.window.bkgdset |
| 1019 | |
| 1020 | ch: object |
| 1021 | Background character. |
| 1022 | attr: long(c_default="A_NORMAL") = _curses.A_NORMAL |
| 1023 | Background attributes. |
| 1024 | / |
| 1025 | |
| 1026 | Set the window's background. |
| 1027 | [clinic start generated code]*/ |
| 1028 | |
| 1029 | static PyObject * |
| 1030 | _curses_window_bkgdset_impl(PyCursesWindowObject *self, PyObject *ch, |
| 1031 | long attr) |
| 1032 | /*[clinic end generated code: output=8cb994fc4d7e2496 input=e09c682425c9e45b]*/ |
| 1033 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1034 | chtype bkgd; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1035 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1036 | if (!PyCurses_ConvertToChtype(self, ch, &bkgd)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1037 | return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1038 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1039 | wbkgdset(self->win, bkgd | attr); |
| 1040 | return PyCursesCheckERR(0, "bkgdset"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1043 | /*[clinic input] |
| 1044 | _curses.window.border |
| 1045 | |
| 1046 | ls: object(c_default="NULL") = _curses.ACS_VLINE |
| 1047 | Left side. |
| 1048 | rs: object(c_default="NULL") = _curses.ACS_VLINE |
| 1049 | Right side. |
| 1050 | ts: object(c_default="NULL") = _curses.ACS_HLINE |
| 1051 | Top side. |
| 1052 | bs: object(c_default="NULL") = _curses.ACS_HLINE |
| 1053 | Bottom side. |
| 1054 | tl: object(c_default="NULL") = _curses.ACS_ULCORNER |
| 1055 | Upper-left corner. |
| 1056 | tr: object(c_default="NULL") = _curses.ACS_URCORNER |
| 1057 | Upper-right corner. |
| 1058 | bl: object(c_default="NULL") = _curses.ACS_LLCORNER |
| 1059 | Bottom-left corner. |
| 1060 | br: object(c_default="NULL") = _curses.ACS_LRCORNER |
| 1061 | Bottom-right corner. |
| 1062 | / |
| 1063 | |
| 1064 | Draw a border around the edges of the window. |
| 1065 | |
| 1066 | Each parameter specifies the character to use for a specific part of the |
| 1067 | border. The characters can be specified as integers or as one-character |
| 1068 | strings. A 0 value for any parameter will cause the default character to be |
| 1069 | used for that parameter. |
| 1070 | [clinic start generated code]*/ |
| 1071 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1072 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1073 | _curses_window_border_impl(PyCursesWindowObject *self, PyObject *ls, |
| 1074 | PyObject *rs, PyObject *ts, PyObject *bs, |
| 1075 | PyObject *tl, PyObject *tr, PyObject *bl, |
| 1076 | PyObject *br) |
| 1077 | /*[clinic end generated code: output=670ef38d3d7c2aa3 input=e015f735d67a240b]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1078 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1079 | chtype ch[8]; |
| 1080 | int i; |
Andrew M. Kuchling | 16e65a0 | 2001-10-20 16:05:52 +0000 | [diff] [blame] | 1081 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1082 | /* Clear the array of parameters */ |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1083 | for(i=0; i<8; i++) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1084 | ch[i] = 0; |
Andrew M. Kuchling | 16e65a0 | 2001-10-20 16:05:52 +0000 | [diff] [blame] | 1085 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1086 | #define CONVERTTOCHTYPE(obj, i) \ |
| 1087 | if ((obj) != NULL && !PyCurses_ConvertToChtype(self, (obj), &ch[(i)])) \ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1088 | return NULL; |
| 1089 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1090 | CONVERTTOCHTYPE(ls, 0); |
| 1091 | CONVERTTOCHTYPE(rs, 1); |
| 1092 | CONVERTTOCHTYPE(ts, 2); |
| 1093 | CONVERTTOCHTYPE(bs, 3); |
| 1094 | CONVERTTOCHTYPE(tl, 4); |
| 1095 | CONVERTTOCHTYPE(tr, 5); |
| 1096 | CONVERTTOCHTYPE(bl, 6); |
| 1097 | CONVERTTOCHTYPE(br, 7); |
| 1098 | |
| 1099 | #undef CONVERTTOCHTYPE |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1100 | |
| 1101 | wborder(self->win, |
| 1102 | ch[0], ch[1], ch[2], ch[3], |
| 1103 | ch[4], ch[5], ch[6], ch[7]); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1104 | Py_RETURN_NONE; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1107 | /*[clinic input] |
| 1108 | _curses.window.box |
| 1109 | |
| 1110 | [ |
Victor Stinner | 3783413 | 2020-10-27 17:12:53 +0100 | [diff] [blame] | 1111 | verch: object(c_default="_PyLong_GetZero()") = 0 |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1112 | Left and right side. |
Victor Stinner | 3783413 | 2020-10-27 17:12:53 +0100 | [diff] [blame] | 1113 | horch: object(c_default="_PyLong_GetZero()") = 0 |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1114 | Top and bottom side. |
| 1115 | ] |
| 1116 | / |
| 1117 | |
| 1118 | Draw a border around the edges of the window. |
| 1119 | |
| 1120 | Similar to border(), but both ls and rs are verch and both ts and bs are |
| 1121 | horch. The default corner characters are always used by this function. |
| 1122 | [clinic start generated code]*/ |
| 1123 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1124 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1125 | _curses_window_box_impl(PyCursesWindowObject *self, int group_right_1, |
| 1126 | PyObject *verch, PyObject *horch) |
Victor Stinner | 3783413 | 2020-10-27 17:12:53 +0100 | [diff] [blame] | 1127 | /*[clinic end generated code: output=f3fcb038bb287192 input=f00435f9c8c98f60]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1128 | { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1129 | chtype ch1 = 0, ch2 = 0; |
| 1130 | if (group_right_1) { |
| 1131 | if (!PyCurses_ConvertToChtype(self, verch, &ch1)) { |
Serhiy Storchaka | 4f469c0 | 2017-11-01 20:48:49 +0200 | [diff] [blame] | 1132 | return NULL; |
| 1133 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1134 | if (!PyCurses_ConvertToChtype(self, horch, &ch2)) { |
Serhiy Storchaka | 4f469c0 | 2017-11-01 20:48:49 +0200 | [diff] [blame] | 1135 | return NULL; |
| 1136 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1137 | } |
| 1138 | box(self->win,ch1,ch2); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1139 | Py_RETURN_NONE; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 1142 | #if defined(HAVE_NCURSES_H) || defined(MVWDELCH_IS_EXPRESSION) |
| 1143 | #define py_mvwdelch mvwdelch |
| 1144 | #else |
| 1145 | int py_mvwdelch(WINDOW *w, int y, int x) |
| 1146 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1147 | mvwdelch(w,y,x); |
| 1148 | /* On HP/UX, mvwdelch already returns. On other systems, |
| 1149 | we may well run into this return statement. */ |
| 1150 | return 0; |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 1151 | } |
| 1152 | #endif |
| 1153 | |
Masayuki Yamamoto | 8bc7d63 | 2017-11-01 21:05:26 +0900 | [diff] [blame] | 1154 | #if defined(HAVE_CURSES_IS_PAD) |
| 1155 | #define py_is_pad(win) is_pad(win) |
| 1156 | #elif defined(WINDOW_HAS_FLAGS) |
| 1157 | #define py_is_pad(win) ((win) ? ((win)->_flags & _ISPAD) != 0 : FALSE) |
| 1158 | #endif |
| 1159 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1160 | /* chgat, added by Fabian Kreutz <fabian.kreutz at gmx.net> */ |
Serhiy Storchaka | 894ebd0 | 2017-11-01 14:34:20 +0200 | [diff] [blame] | 1161 | #ifdef HAVE_CURSES_WCHGAT |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1162 | /*[-clinic input] |
| 1163 | _curses.window.chgat |
| 1164 | |
| 1165 | [ |
| 1166 | y: int |
| 1167 | Y-coordinate. |
| 1168 | x: int |
| 1169 | X-coordinate. |
| 1170 | ] |
| 1171 | |
| 1172 | n: int = -1 |
| 1173 | Number of characters. |
| 1174 | |
| 1175 | attr: long |
| 1176 | Attributes for characters. |
| 1177 | / |
| 1178 | |
| 1179 | Set the attributes of characters. |
| 1180 | |
| 1181 | Set the attributes of num characters at the current cursor position, or at |
| 1182 | position (y, x) if supplied. If no value of num is given or num = -1, the |
| 1183 | attribute will be set on all the characters to the end of the line. This |
| 1184 | function does not move the cursor. The changed line will be touched using |
| 1185 | the touchline() method so that the contents will be redisplayed by the next |
| 1186 | window refresh. |
| 1187 | [-clinic start generated code]*/ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1188 | static PyObject * |
| 1189 | PyCursesWindow_ChgAt(PyCursesWindowObject *self, PyObject *args) |
| 1190 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1191 | int rtn; |
| 1192 | int x, y; |
| 1193 | int num = -1; |
| 1194 | short color; |
| 1195 | attr_t attr = A_NORMAL; |
| 1196 | long lattr; |
| 1197 | int use_xy = FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1198 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1199 | switch (PyTuple_Size(args)) { |
| 1200 | case 1: |
| 1201 | if (!PyArg_ParseTuple(args,"l;attr", &lattr)) |
| 1202 | return NULL; |
| 1203 | attr = lattr; |
| 1204 | break; |
| 1205 | case 2: |
| 1206 | if (!PyArg_ParseTuple(args,"il;n,attr", &num, &lattr)) |
| 1207 | return NULL; |
| 1208 | attr = lattr; |
| 1209 | break; |
| 1210 | case 3: |
| 1211 | if (!PyArg_ParseTuple(args,"iil;int,int,attr", &y, &x, &lattr)) |
| 1212 | return NULL; |
| 1213 | attr = lattr; |
| 1214 | use_xy = TRUE; |
| 1215 | break; |
| 1216 | case 4: |
| 1217 | if (!PyArg_ParseTuple(args,"iiil;int,int,n,attr", &y, &x, &num, &lattr)) |
| 1218 | return NULL; |
| 1219 | attr = lattr; |
| 1220 | use_xy = TRUE; |
| 1221 | break; |
| 1222 | default: |
| 1223 | PyErr_SetString(PyExc_TypeError, "chgat requires 1 to 4 arguments"); |
| 1224 | return NULL; |
| 1225 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1226 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1227 | color = (short)((attr >> 8) & 0xff); |
| 1228 | attr = attr - (color << 8); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1229 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1230 | if (use_xy) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1231 | rtn = mvwchgat(self->win,y,x,num,attr,color,NULL); |
| 1232 | touchline(self->win,y,1); |
| 1233 | } else { |
| 1234 | getyx(self->win,y,x); |
| 1235 | rtn = wchgat(self->win,num,attr,color,NULL); |
| 1236 | touchline(self->win,y,1); |
| 1237 | } |
| 1238 | return PyCursesCheckERR(rtn, "chgat"); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1239 | } |
Serhiy Storchaka | 894ebd0 | 2017-11-01 14:34:20 +0200 | [diff] [blame] | 1240 | #endif |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 1241 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1242 | /*[clinic input] |
| 1243 | _curses.window.delch |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 1244 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1245 | [ |
| 1246 | y: int |
| 1247 | Y-coordinate. |
| 1248 | x: int |
| 1249 | X-coordinate. |
| 1250 | ] |
| 1251 | / |
| 1252 | |
| 1253 | Delete any character at (y, x). |
| 1254 | [clinic start generated code]*/ |
| 1255 | |
| 1256 | static PyObject * |
| 1257 | _curses_window_delch_impl(PyCursesWindowObject *self, int group_right_1, |
| 1258 | int y, int x) |
| 1259 | /*[clinic end generated code: output=22e77bb9fa11b461 input=d2f79e630a4fc6d0]*/ |
| 1260 | { |
| 1261 | if (!group_right_1) { |
| 1262 | return PyCursesCheckERR(wdelch(self->win), "wdelch"); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1263 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1264 | else { |
| 1265 | return PyCursesCheckERR(py_mvwdelch(self->win, y, x), "mvwdelch"); |
| 1266 | } |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1269 | /*[clinic input] |
| 1270 | _curses.window.derwin |
| 1271 | |
| 1272 | [ |
| 1273 | nlines: int = 0 |
| 1274 | Height. |
| 1275 | ncols: int = 0 |
| 1276 | Width. |
| 1277 | ] |
| 1278 | begin_y: int |
| 1279 | Top side y-coordinate. |
| 1280 | begin_x: int |
| 1281 | Left side x-coordinate. |
| 1282 | / |
| 1283 | |
| 1284 | Create a sub-window (window-relative coordinates). |
| 1285 | |
| 1286 | derwin() is the same as calling subwin(), except that begin_y and begin_x |
| 1287 | are relative to the origin of the window, rather than relative to the entire |
| 1288 | screen. |
| 1289 | [clinic start generated code]*/ |
| 1290 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1291 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1292 | _curses_window_derwin_impl(PyCursesWindowObject *self, int group_left_1, |
| 1293 | int nlines, int ncols, int begin_y, int begin_x) |
| 1294 | /*[clinic end generated code: output=7924b112d9f70d6e input=966d9481f7f5022e]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1295 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1296 | WINDOW *win; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1297 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1298 | win = derwin(self->win,nlines,ncols,begin_y,begin_x); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1299 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1300 | if (win == NULL) { |
| 1301 | PyErr_SetString(PyCursesError, catchall_NULL); |
| 1302 | return NULL; |
| 1303 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1304 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1305 | return (PyObject *)PyCursesWindow_New(win, NULL); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1308 | /*[clinic input] |
| 1309 | _curses.window.echochar |
| 1310 | |
| 1311 | ch: object |
| 1312 | Character to add. |
| 1313 | |
| 1314 | attr: long(c_default="A_NORMAL") = _curses.A_NORMAL |
| 1315 | Attributes for the character. |
| 1316 | / |
| 1317 | |
| 1318 | Add character ch with attribute attr, and refresh. |
| 1319 | [clinic start generated code]*/ |
| 1320 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1321 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1322 | _curses_window_echochar_impl(PyCursesWindowObject *self, PyObject *ch, |
| 1323 | long attr) |
| 1324 | /*[clinic end generated code: output=13e7dd875d4b9642 input=e7f34b964e92b156]*/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1325 | { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1326 | chtype ch_; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 1327 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1328 | if (!PyCurses_ConvertToChtype(self, ch, &ch_)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1329 | return NULL; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1330 | |
Masayuki Yamamoto | 8bc7d63 | 2017-11-01 21:05:26 +0900 | [diff] [blame] | 1331 | #ifdef py_is_pad |
| 1332 | if (py_is_pad(self->win)) { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1333 | return PyCursesCheckERR(pechochar(self->win, ch_ | (attr_t)attr), |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1334 | "echochar"); |
Masayuki Yamamoto | 8bc7d63 | 2017-11-01 21:05:26 +0900 | [diff] [blame] | 1335 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1336 | else |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 1337 | #endif |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1338 | return PyCursesCheckERR(wechochar(self->win, ch_ | (attr_t)attr), |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1339 | "echochar"); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 1342 | #ifdef NCURSES_MOUSE_VERSION |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1343 | /*[clinic input] |
| 1344 | _curses.window.enclose -> long |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 1345 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1346 | y: int |
| 1347 | Y-coordinate. |
| 1348 | x: int |
| 1349 | X-coordinate. |
| 1350 | / |
| 1351 | |
| 1352 | Return True if the screen-relative coordinates are enclosed by the window. |
| 1353 | [clinic start generated code]*/ |
| 1354 | |
| 1355 | static long |
| 1356 | _curses_window_enclose_impl(PyCursesWindowObject *self, int y, int x) |
| 1357 | /*[clinic end generated code: output=5251c961cbe3df63 input=dfe1d9d4d05d8642]*/ |
| 1358 | { |
| 1359 | return wenclose(self->win, y, x); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 1360 | } |
| 1361 | #endif |
| 1362 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1363 | /*[clinic input] |
| 1364 | _curses.window.getbkgd -> long |
| 1365 | |
| 1366 | Return the window's current background character/attribute pair. |
| 1367 | [clinic start generated code]*/ |
| 1368 | |
| 1369 | static long |
| 1370 | _curses_window_getbkgd_impl(PyCursesWindowObject *self) |
| 1371 | /*[clinic end generated code: output=c52b25dc16b215c3 input=a69db882fa35426c]*/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1372 | { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1373 | return (long) getbkgd(self->win); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1376 | /*[clinic input] |
| 1377 | _curses.window.getch -> int |
| 1378 | |
| 1379 | [ |
| 1380 | y: int |
| 1381 | Y-coordinate. |
| 1382 | x: int |
| 1383 | X-coordinate. |
| 1384 | ] |
| 1385 | / |
| 1386 | |
| 1387 | Get a character code from terminal keyboard. |
| 1388 | |
| 1389 | The integer returned does not have to be in ASCII range: function keys, |
| 1390 | keypad keys and so on return numbers higher than 256. In no-delay mode, -1 |
| 1391 | is returned if there is no input, else getch() waits until a key is pressed. |
| 1392 | [clinic start generated code]*/ |
| 1393 | |
| 1394 | static int |
| 1395 | _curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1, |
| 1396 | int y, int x) |
| 1397 | /*[clinic end generated code: output=980aa6af0c0ca387 input=bb24ebfb379f991f]*/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1398 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1399 | int rtn; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 1400 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1401 | Py_BEGIN_ALLOW_THREADS |
| 1402 | if (!group_right_1) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1403 | rtn = wgetch(self->win); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1404 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1405 | else { |
| 1406 | rtn = mvwgetch(self->win, y, x); |
| 1407 | } |
| 1408 | Py_END_ALLOW_THREADS |
| 1409 | |
| 1410 | return rtn; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1411 | } |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 1412 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1413 | /*[clinic input] |
| 1414 | _curses.window.getkey |
| 1415 | |
| 1416 | [ |
| 1417 | y: int |
| 1418 | Y-coordinate. |
| 1419 | x: int |
| 1420 | X-coordinate. |
| 1421 | ] |
| 1422 | / |
| 1423 | |
| 1424 | Get a character (string) from terminal keyboard. |
| 1425 | |
| 1426 | Returning a string instead of an integer, as getch() does. Function keys, |
| 1427 | keypad keys and other special keys return a multibyte string containing the |
| 1428 | key name. In no-delay mode, an exception is raised if there is no input. |
| 1429 | [clinic start generated code]*/ |
| 1430 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1431 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1432 | _curses_window_getkey_impl(PyCursesWindowObject *self, int group_right_1, |
| 1433 | int y, int x) |
| 1434 | /*[clinic end generated code: output=8490a182db46b10f input=be2dee34f5cf57f8]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1435 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1436 | int rtn; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1437 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1438 | Py_BEGIN_ALLOW_THREADS |
| 1439 | if (!group_right_1) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1440 | rtn = wgetch(self->win); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1441 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1442 | else { |
| 1443 | rtn = mvwgetch(self->win, y, x); |
| 1444 | } |
| 1445 | Py_END_ALLOW_THREADS |
| 1446 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1447 | if (rtn == ERR) { |
| 1448 | /* getch() returns ERR in nodelay mode */ |
R David Murray | f5d7cc2 | 2013-03-19 16:23:09 -0400 | [diff] [blame] | 1449 | PyErr_CheckSignals(); |
| 1450 | if (!PyErr_Occurred()) |
| 1451 | PyErr_SetString(PyCursesError, "no input"); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1452 | return NULL; |
Serhiy Storchaka | 7e68790 | 2017-11-01 16:03:40 +0200 | [diff] [blame] | 1453 | } else if (rtn <= 255) { |
| 1454 | #ifdef NCURSES_VERSION_MAJOR |
| 1455 | #if NCURSES_VERSION_MAJOR*100+NCURSES_VERSION_MINOR <= 507 |
| 1456 | /* Work around a bug in ncurses 5.7 and earlier */ |
| 1457 | if (rtn < 0) { |
| 1458 | rtn += 256; |
| 1459 | } |
| 1460 | #endif |
| 1461 | #endif |
| 1462 | return PyUnicode_FromOrdinal(rtn); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1463 | } else { |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 1464 | const char *knp = keyname(rtn); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1465 | return PyUnicode_FromString((knp == NULL) ? "" : knp); |
| 1466 | } |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Nadeem Vawda | 9e2e990 | 2011-07-31 15:01:11 +0200 | [diff] [blame] | 1469 | #ifdef HAVE_NCURSESW |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1470 | /*[clinic input] |
| 1471 | _curses.window.get_wch |
| 1472 | |
| 1473 | [ |
| 1474 | y: int |
| 1475 | Y-coordinate. |
| 1476 | x: int |
| 1477 | X-coordinate. |
| 1478 | ] |
| 1479 | / |
| 1480 | |
| 1481 | Get a wide character from terminal keyboard. |
| 1482 | |
| 1483 | Return a character for most keys, or an integer for function keys, |
| 1484 | keypad keys, and other special keys. |
| 1485 | [clinic start generated code]*/ |
| 1486 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1487 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1488 | _curses_window_get_wch_impl(PyCursesWindowObject *self, int group_right_1, |
| 1489 | int y, int x) |
| 1490 | /*[clinic end generated code: output=9f4f86e91fe50ef3 input=dd7e5367fb49dc48]*/ |
Victor Stinner | a7878b7 | 2011-07-14 23:07:44 +0200 | [diff] [blame] | 1491 | { |
Victor Stinner | a7878b7 | 2011-07-14 23:07:44 +0200 | [diff] [blame] | 1492 | int ct; |
| 1493 | wint_t rtn; |
| 1494 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1495 | Py_BEGIN_ALLOW_THREADS |
| 1496 | if (!group_right_1) { |
| 1497 | ct = wget_wch(self->win ,&rtn); |
Victor Stinner | a7878b7 | 2011-07-14 23:07:44 +0200 | [diff] [blame] | 1498 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1499 | else { |
| 1500 | ct = mvwget_wch(self->win, y, x, &rtn); |
| 1501 | } |
| 1502 | Py_END_ALLOW_THREADS |
| 1503 | |
Victor Stinner | a7878b7 | 2011-07-14 23:07:44 +0200 | [diff] [blame] | 1504 | if (ct == ERR) { |
Victor Stinner | bd2d30c | 2013-03-21 12:21:06 +0100 | [diff] [blame] | 1505 | if (PyErr_CheckSignals()) |
| 1506 | return NULL; |
| 1507 | |
Victor Stinner | a7878b7 | 2011-07-14 23:07:44 +0200 | [diff] [blame] | 1508 | /* get_wch() returns ERR in nodelay mode */ |
| 1509 | PyErr_SetString(PyCursesError, "no input"); |
| 1510 | return NULL; |
| 1511 | } |
Victor Stinner | 1d39cde | 2012-08-29 01:40:57 +0200 | [diff] [blame] | 1512 | if (ct == KEY_CODE_YES) |
| 1513 | return PyLong_FromLong(rtn); |
| 1514 | else |
| 1515 | return PyUnicode_FromOrdinal(rtn); |
Victor Stinner | a7878b7 | 2011-07-14 23:07:44 +0200 | [diff] [blame] | 1516 | } |
Nadeem Vawda | 9e2e990 | 2011-07-31 15:01:11 +0200 | [diff] [blame] | 1517 | #endif |
Victor Stinner | a7878b7 | 2011-07-14 23:07:44 +0200 | [diff] [blame] | 1518 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1519 | /*[-clinic input] |
| 1520 | _curses.window.getstr |
| 1521 | |
| 1522 | [ |
| 1523 | y: int |
| 1524 | Y-coordinate. |
| 1525 | x: int |
| 1526 | X-coordinate. |
| 1527 | ] |
| 1528 | n: int = 1023 |
| 1529 | Maximal number of characters. |
| 1530 | / |
| 1531 | |
| 1532 | Read a string from the user, with primitive line editing capacity. |
| 1533 | [-clinic start generated code]*/ |
| 1534 | |
Victor Stinner | a7878b7 | 2011-07-14 23:07:44 +0200 | [diff] [blame] | 1535 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1536 | PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1537 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1538 | int x, y, n; |
| 1539 | char rtn[1024]; /* This should be big enough.. I hope */ |
| 1540 | int rtn2; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 1541 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1542 | switch (PyTuple_Size(args)) { |
| 1543 | case 0: |
| 1544 | Py_BEGIN_ALLOW_THREADS |
| 1545 | rtn2 = wgetnstr(self->win,rtn, 1023); |
| 1546 | Py_END_ALLOW_THREADS |
| 1547 | break; |
| 1548 | case 1: |
| 1549 | if (!PyArg_ParseTuple(args,"i;n", &n)) |
| 1550 | return NULL; |
Benjamin Peterson | 40a77c3 | 2016-08-13 18:15:28 -0700 | [diff] [blame] | 1551 | if (n < 0) { |
| 1552 | PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative"); |
| 1553 | return NULL; |
| 1554 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1555 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 1556 | rtn2 = wgetnstr(self->win, rtn, Py_MIN(n, 1023)); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1557 | Py_END_ALLOW_THREADS |
| 1558 | break; |
| 1559 | case 2: |
| 1560 | if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x)) |
| 1561 | return NULL; |
| 1562 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1563 | #ifdef STRICT_SYSV_CURSES |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1564 | rtn2 = wmove(self->win,y,x)==ERR ? ERR : wgetnstr(self->win, rtn, 1023); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1565 | #else |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1566 | rtn2 = mvwgetnstr(self->win,y,x,rtn, 1023); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1567 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1568 | Py_END_ALLOW_THREADS |
| 1569 | break; |
| 1570 | case 3: |
| 1571 | if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n)) |
| 1572 | return NULL; |
Benjamin Peterson | 40a77c3 | 2016-08-13 18:15:28 -0700 | [diff] [blame] | 1573 | if (n < 0) { |
| 1574 | PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative"); |
| 1575 | return NULL; |
| 1576 | } |
Andrew M. Kuchling | a1e4b05 | 2000-06-27 21:49:47 +0000 | [diff] [blame] | 1577 | #ifdef STRICT_SYSV_CURSES |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1578 | Py_BEGIN_ALLOW_THREADS |
| 1579 | rtn2 = wmove(self->win,y,x)==ERR ? ERR : |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 1580 | wgetnstr(self->win, rtn, Py_MIN(n, 1023)); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1581 | Py_END_ALLOW_THREADS |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1582 | #else |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1583 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 1584 | rtn2 = mvwgetnstr(self->win, y, x, rtn, Py_MIN(n, 1023)); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1585 | Py_END_ALLOW_THREADS |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1586 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1587 | break; |
| 1588 | default: |
| 1589 | PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 3 arguments"); |
| 1590 | return NULL; |
| 1591 | } |
| 1592 | if (rtn2 == ERR) |
| 1593 | rtn[0] = 0; |
| 1594 | return PyBytes_FromString(rtn); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1597 | /*[clinic input] |
| 1598 | _curses.window.hline |
| 1599 | |
| 1600 | [ |
| 1601 | y: int |
| 1602 | Starting Y-coordinate. |
| 1603 | x: int |
| 1604 | Starting X-coordinate. |
| 1605 | ] |
| 1606 | |
| 1607 | ch: object |
| 1608 | Character to draw. |
| 1609 | n: int |
| 1610 | Line length. |
| 1611 | |
| 1612 | [ |
| 1613 | attr: long(c_default="A_NORMAL") = _curses.A_NORMAL |
| 1614 | Attributes for the characters. |
| 1615 | ] |
| 1616 | / |
| 1617 | |
| 1618 | Display a horizontal line. |
| 1619 | [clinic start generated code]*/ |
| 1620 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1621 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1622 | _curses_window_hline_impl(PyCursesWindowObject *self, int group_left_1, |
| 1623 | int y, int x, PyObject *ch, int n, |
| 1624 | int group_right_1, long attr) |
| 1625 | /*[clinic end generated code: output=c00d489d61fc9eef input=81a4dea47268163e]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1626 | { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1627 | chtype ch_; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1628 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1629 | if (!PyCurses_ConvertToChtype(self, ch, &ch_)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1630 | return NULL; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1631 | if (group_left_1) { |
| 1632 | if (wmove(self->win, y, x) == ERR) { |
| 1633 | return PyCursesCheckERR(ERR, "wmove"); |
| 1634 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1635 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1636 | return PyCursesCheckERR(whline(self->win, ch_ | (attr_t)attr, n), "hline"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1639 | /*[clinic input] |
| 1640 | _curses.window.insch |
| 1641 | |
| 1642 | [ |
| 1643 | y: int |
| 1644 | Y-coordinate. |
| 1645 | x: int |
| 1646 | X-coordinate. |
| 1647 | ] |
| 1648 | |
| 1649 | ch: object |
| 1650 | Character to insert. |
| 1651 | |
| 1652 | [ |
| 1653 | attr: long(c_default="A_NORMAL") = _curses.A_NORMAL |
| 1654 | Attributes for the character. |
| 1655 | ] |
| 1656 | / |
| 1657 | |
| 1658 | Insert a character before the current or specified position. |
| 1659 | |
| 1660 | All characters to the right of the cursor are shifted one position right, with |
| 1661 | the rightmost characters on the line being lost. |
| 1662 | [clinic start generated code]*/ |
| 1663 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1664 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1665 | _curses_window_insch_impl(PyCursesWindowObject *self, int group_left_1, |
| 1666 | int y, int x, PyObject *ch, int group_right_1, |
| 1667 | long attr) |
| 1668 | /*[clinic end generated code: output=ade8cfe3a3bf3e34 input=336342756ee19812]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1669 | { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1670 | int rtn; |
| 1671 | chtype ch_ = 0; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1672 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1673 | if (!PyCurses_ConvertToChtype(self, ch, &ch_)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1674 | return NULL; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1675 | |
| 1676 | if (!group_left_1) { |
| 1677 | rtn = winsch(self->win, ch_ | (attr_t)attr); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1678 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1679 | else { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1680 | rtn = mvwinsch(self->win, y, x, ch_ | (attr_t)attr); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1681 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1682 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1683 | return PyCursesCheckERR(rtn, "insch"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1686 | /*[clinic input] |
| 1687 | _curses.window.inch -> unsigned_long |
| 1688 | |
| 1689 | [ |
| 1690 | y: int |
| 1691 | Y-coordinate. |
| 1692 | x: int |
| 1693 | X-coordinate. |
| 1694 | ] |
| 1695 | / |
| 1696 | |
| 1697 | Return the character at the given position in the window. |
| 1698 | |
| 1699 | The bottom 8 bits are the character proper, and upper bits are the attributes. |
| 1700 | [clinic start generated code]*/ |
| 1701 | |
| 1702 | static unsigned long |
| 1703 | _curses_window_inch_impl(PyCursesWindowObject *self, int group_right_1, |
| 1704 | int y, int x) |
| 1705 | /*[clinic end generated code: output=6c4719fe978fe86a input=fac23ee11e3b3a66]*/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1706 | { |
Christian Heimes | abbc8ca | 2013-12-04 08:50:22 +0100 | [diff] [blame] | 1707 | unsigned long rtn; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 1708 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1709 | if (!group_right_1) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1710 | rtn = winch(self->win); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1711 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1712 | else { |
| 1713 | rtn = mvwinch(self->win, y, x); |
| 1714 | } |
| 1715 | |
| 1716 | return rtn; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1719 | /*[-clinic input] |
| 1720 | _curses.window.instr |
| 1721 | |
| 1722 | [ |
| 1723 | y: int |
| 1724 | Y-coordinate. |
| 1725 | x: int |
| 1726 | X-coordinate. |
| 1727 | ] |
| 1728 | n: int = 1023 |
| 1729 | Maximal number of characters. |
| 1730 | / |
| 1731 | |
| 1732 | Return a string of characters, extracted from the window. |
| 1733 | |
| 1734 | Return a string of characters, extracted from the window starting at the |
| 1735 | current cursor position, or at y, x if specified. Attributes are stripped |
| 1736 | from the characters. If n is specified, instr() returns a string at most |
| 1737 | n characters long (exclusive of the trailing NUL). |
| 1738 | [-clinic start generated code]*/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1739 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1740 | PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1741 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1742 | int x, y, n; |
| 1743 | char rtn[1024]; /* This should be big enough.. I hope */ |
| 1744 | int rtn2; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1745 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1746 | switch (PyTuple_Size(args)) { |
| 1747 | case 0: |
| 1748 | rtn2 = winnstr(self->win,rtn, 1023); |
| 1749 | break; |
| 1750 | case 1: |
| 1751 | if (!PyArg_ParseTuple(args,"i;n", &n)) |
| 1752 | return NULL; |
Benjamin Peterson | 432ea4f | 2016-08-15 21:40:14 -0700 | [diff] [blame] | 1753 | if (n < 0) { |
| 1754 | PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative"); |
| 1755 | return NULL; |
| 1756 | } |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 1757 | rtn2 = winnstr(self->win, rtn, Py_MIN(n, 1023)); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1758 | break; |
| 1759 | case 2: |
| 1760 | if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x)) |
| 1761 | return NULL; |
| 1762 | rtn2 = mvwinnstr(self->win,y,x,rtn,1023); |
| 1763 | break; |
| 1764 | case 3: |
| 1765 | if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n)) |
| 1766 | return NULL; |
Benjamin Peterson | 432ea4f | 2016-08-15 21:40:14 -0700 | [diff] [blame] | 1767 | if (n < 0) { |
| 1768 | PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative"); |
| 1769 | return NULL; |
| 1770 | } |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 1771 | rtn2 = mvwinnstr(self->win, y, x, rtn, Py_MIN(n,1023)); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1772 | break; |
| 1773 | default: |
| 1774 | PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments"); |
| 1775 | return NULL; |
| 1776 | } |
| 1777 | if (rtn2 == ERR) |
| 1778 | rtn[0] = 0; |
| 1779 | return PyBytes_FromString(rtn); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1782 | /*[clinic input] |
| 1783 | _curses.window.insstr |
| 1784 | |
| 1785 | [ |
| 1786 | y: int |
| 1787 | Y-coordinate. |
| 1788 | x: int |
| 1789 | X-coordinate. |
| 1790 | ] |
| 1791 | |
| 1792 | str: object |
| 1793 | String to insert. |
| 1794 | |
| 1795 | [ |
| 1796 | attr: long |
| 1797 | Attributes for characters. |
| 1798 | ] |
| 1799 | / |
| 1800 | |
| 1801 | Insert the string before the current or specified position. |
| 1802 | |
| 1803 | Insert a character string (as many characters as will fit on the line) |
| 1804 | before the character under the cursor. All characters to the right of |
| 1805 | the cursor are shifted right, with the rightmost characters on the line |
| 1806 | being lost. The cursor position does not change (after moving to y, x, |
| 1807 | if specified). |
| 1808 | [clinic start generated code]*/ |
| 1809 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1810 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1811 | _curses_window_insstr_impl(PyCursesWindowObject *self, int group_left_1, |
| 1812 | int y, int x, PyObject *str, int group_right_1, |
| 1813 | long attr) |
| 1814 | /*[clinic end generated code: output=c259a5265ad0b777 input=6827cddc6340a7f3]*/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1815 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1816 | int rtn; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1817 | int strtype; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1818 | PyObject *bytesobj = NULL; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1819 | #ifdef HAVE_NCURSESW |
| 1820 | wchar_t *wstr = NULL; |
| 1821 | #endif |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1822 | attr_t attr_old = A_NORMAL; |
| 1823 | int use_xy = group_left_1, use_attr = group_right_1; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1824 | const char *funcname; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1825 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1826 | #ifdef HAVE_NCURSESW |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1827 | strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1828 | #else |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1829 | strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1830 | #endif |
| 1831 | if (strtype == 0) |
| 1832 | return NULL; |
| 1833 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1834 | if (use_attr) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1835 | attr_old = getattrs(self->win); |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1836 | (void)wattrset(self->win, (attr_t)attr); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1837 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1838 | #ifdef HAVE_NCURSESW |
| 1839 | if (strtype == 2) { |
| 1840 | funcname = "inswstr"; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1841 | if (use_xy) |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1842 | rtn = mvwins_wstr(self->win,y,x,wstr); |
| 1843 | else |
| 1844 | rtn = wins_wstr(self->win,wstr); |
| 1845 | PyMem_Free(wstr); |
| 1846 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1847 | else |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1848 | #endif |
| 1849 | { |
Serhiy Storchaka | 8f87eef | 2020-04-12 14:58:27 +0300 | [diff] [blame] | 1850 | const char *str = PyBytes_AS_STRING(bytesobj); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1851 | funcname = "insstr"; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1852 | if (use_xy) |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1853 | rtn = mvwinsstr(self->win,y,x,str); |
| 1854 | else |
| 1855 | rtn = winsstr(self->win,str); |
| 1856 | Py_DECREF(bytesobj); |
| 1857 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1858 | if (use_attr) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1859 | (void)wattrset(self->win,attr_old); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1860 | return PyCursesCheckERR(rtn, funcname); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1863 | /*[clinic input] |
| 1864 | _curses.window.insnstr |
| 1865 | |
| 1866 | [ |
| 1867 | y: int |
| 1868 | Y-coordinate. |
| 1869 | x: int |
| 1870 | X-coordinate. |
| 1871 | ] |
| 1872 | |
| 1873 | str: object |
| 1874 | String to insert. |
| 1875 | |
| 1876 | n: int |
| 1877 | Maximal number of characters. |
| 1878 | |
| 1879 | [ |
| 1880 | attr: long |
| 1881 | Attributes for characters. |
| 1882 | ] |
| 1883 | / |
| 1884 | |
| 1885 | Insert at most n characters of the string. |
| 1886 | |
| 1887 | Insert a character string (as many characters as will fit on the line) |
| 1888 | before the character under the cursor, up to n characters. If n is zero |
| 1889 | or negative, the entire string is inserted. All characters to the right |
| 1890 | of the cursor are shifted right, with the rightmost characters on the line |
| 1891 | being lost. The cursor position does not change (after moving to y, x, if |
| 1892 | specified). |
| 1893 | [clinic start generated code]*/ |
| 1894 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1895 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1896 | _curses_window_insnstr_impl(PyCursesWindowObject *self, int group_left_1, |
| 1897 | int y, int x, PyObject *str, int n, |
| 1898 | int group_right_1, long attr) |
| 1899 | /*[clinic end generated code: output=971a32ea6328ec8b input=70fa0cd543901a4c]*/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1900 | { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1901 | int rtn; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1902 | int strtype; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1903 | PyObject *bytesobj = NULL; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1904 | #ifdef HAVE_NCURSESW |
| 1905 | wchar_t *wstr = NULL; |
| 1906 | #endif |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1907 | attr_t attr_old = A_NORMAL; |
| 1908 | int use_xy = group_left_1, use_attr = group_right_1; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1909 | const char *funcname; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1910 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1911 | #ifdef HAVE_NCURSESW |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1912 | strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1913 | #else |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1914 | strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1915 | #endif |
| 1916 | if (strtype == 0) |
| 1917 | return NULL; |
| 1918 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1919 | if (use_attr) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1920 | attr_old = getattrs(self->win); |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1921 | (void)wattrset(self->win, (attr_t)attr); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1922 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1923 | #ifdef HAVE_NCURSESW |
| 1924 | if (strtype == 2) { |
| 1925 | funcname = "insn_wstr"; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1926 | if (use_xy) |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1927 | rtn = mvwins_nwstr(self->win,y,x,wstr,n); |
| 1928 | else |
| 1929 | rtn = wins_nwstr(self->win,wstr,n); |
| 1930 | PyMem_Free(wstr); |
| 1931 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1932 | else |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1933 | #endif |
| 1934 | { |
Serhiy Storchaka | 8f87eef | 2020-04-12 14:58:27 +0300 | [diff] [blame] | 1935 | const char *str = PyBytes_AS_STRING(bytesobj); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1936 | funcname = "insnstr"; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1937 | if (use_xy) |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1938 | rtn = mvwinsnstr(self->win,y,x,str,n); |
| 1939 | else |
| 1940 | rtn = winsnstr(self->win,str,n); |
| 1941 | Py_DECREF(bytesobj); |
| 1942 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1943 | if (use_attr) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1944 | (void)wattrset(self->win,attr_old); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1945 | return PyCursesCheckERR(rtn, funcname); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1948 | /*[clinic input] |
| 1949 | _curses.window.is_linetouched |
| 1950 | |
| 1951 | line: int |
| 1952 | Line number. |
| 1953 | / |
| 1954 | |
| 1955 | Return True if the specified line was modified, otherwise return False. |
| 1956 | |
| 1957 | Raise a curses.error exception if line is not valid for the given window. |
| 1958 | [clinic start generated code]*/ |
| 1959 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1960 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1961 | _curses_window_is_linetouched_impl(PyCursesWindowObject *self, int line) |
| 1962 | /*[clinic end generated code: output=ad4a4edfee2db08c input=a7be0c189f243914]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1963 | { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1964 | int erg; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1965 | erg = is_linetouched(self->win, line); |
| 1966 | if (erg == ERR) { |
| 1967 | PyErr_SetString(PyExc_TypeError, |
| 1968 | "is_linetouched: line number outside of boundaries"); |
| 1969 | return NULL; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1970 | } |
| 1971 | return PyBool_FromLong(erg); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1974 | #ifdef py_is_pad |
| 1975 | /*[clinic input] |
| 1976 | _curses.window.noutrefresh |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1977 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 1978 | [ |
| 1979 | pminrow: int |
| 1980 | pmincol: int |
| 1981 | sminrow: int |
| 1982 | smincol: int |
| 1983 | smaxrow: int |
| 1984 | smaxcol: int |
| 1985 | ] |
| 1986 | / |
| 1987 | |
| 1988 | Mark for refresh but wait. |
| 1989 | |
| 1990 | This function updates the data structure representing the desired state of the |
| 1991 | window, but does not force an update of the physical screen. To accomplish |
| 1992 | that, call doupdate(). |
| 1993 | [clinic start generated code]*/ |
| 1994 | |
| 1995 | static PyObject * |
| 1996 | _curses_window_noutrefresh_impl(PyCursesWindowObject *self, |
| 1997 | int group_right_1, int pminrow, int pmincol, |
| 1998 | int sminrow, int smincol, int smaxrow, |
| 1999 | int smaxcol) |
| 2000 | /*[clinic end generated code: output=809a1f3c6a03e23e input=3e56898388cd739e]*/ |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 2001 | #else |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2002 | /*[clinic input] |
| 2003 | _curses.window.noutrefresh |
Andrew M. Kuchling | f419572 | 2000-06-23 01:36:21 +0000 | [diff] [blame] | 2004 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2005 | Mark for refresh but wait. |
| 2006 | |
| 2007 | This function updates the data structure representing the desired state of the |
| 2008 | window, but does not force an update of the physical screen. To accomplish |
| 2009 | that, call doupdate(). |
| 2010 | [clinic start generated code]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2011 | |
| 2012 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2013 | _curses_window_noutrefresh_impl(PyCursesWindowObject *self) |
| 2014 | /*[clinic end generated code: output=6ef6dec666643fee input=876902e3fa431dbd]*/ |
| 2015 | #endif |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 2016 | { |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 2017 | int rtn; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2018 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2019 | #ifdef py_is_pad |
| 2020 | if (py_is_pad(self->win)) { |
| 2021 | if (!group_right_1) { |
| 2022 | PyErr_SetString(PyCursesError, |
| 2023 | "noutrefresh() called for a pad " |
| 2024 | "requires 6 arguments"); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2025 | return NULL; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2026 | } |
| 2027 | Py_BEGIN_ALLOW_THREADS |
| 2028 | rtn = pnoutrefresh(self->win, pminrow, pmincol, |
| 2029 | sminrow, smincol, smaxrow, smaxcol); |
| 2030 | Py_END_ALLOW_THREADS |
| 2031 | return PyCursesCheckERR(rtn, "pnoutrefresh"); |
| 2032 | } |
| 2033 | if (group_right_1) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2034 | PyErr_SetString(PyExc_TypeError, |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2035 | "noutrefresh() takes no arguments (6 given)"); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2036 | return NULL; |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 2037 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2038 | #endif |
| 2039 | Py_BEGIN_ALLOW_THREADS |
| 2040 | rtn = wnoutrefresh(self->win); |
| 2041 | Py_END_ALLOW_THREADS |
| 2042 | return PyCursesCheckERR(rtn, "wnoutrefresh"); |
| 2043 | } |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 2044 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2045 | /*[clinic input] |
| 2046 | _curses.window.overlay |
| 2047 | |
| 2048 | destwin: object(type="PyCursesWindowObject *", subclass_of="&PyCursesWindow_Type") |
| 2049 | |
| 2050 | [ |
| 2051 | sminrow: int |
| 2052 | smincol: int |
| 2053 | dminrow: int |
| 2054 | dmincol: int |
| 2055 | dmaxrow: int |
| 2056 | dmaxcol: int |
| 2057 | ] |
| 2058 | / |
| 2059 | |
| 2060 | Overlay the window on top of destwin. |
| 2061 | |
| 2062 | The windows need not be the same size, only the overlapping region is copied. |
| 2063 | This copy is non-destructive, which means that the current background |
| 2064 | character does not overwrite the old contents of destwin. |
| 2065 | |
| 2066 | To get fine-grained control over the copied region, the second form of |
| 2067 | overlay() can be used. sminrow and smincol are the upper-left coordinates |
| 2068 | of the source window, and the other variables mark a rectangle in the |
| 2069 | destination window. |
| 2070 | [clinic start generated code]*/ |
| 2071 | |
| 2072 | static PyObject * |
| 2073 | _curses_window_overlay_impl(PyCursesWindowObject *self, |
| 2074 | PyCursesWindowObject *destwin, int group_right_1, |
| 2075 | int sminrow, int smincol, int dminrow, |
| 2076 | int dmincol, int dmaxrow, int dmaxcol) |
| 2077 | /*[clinic end generated code: output=82bb2c4cb443ca58 input=7edd23ad22cc1984]*/ |
| 2078 | { |
| 2079 | int rtn; |
| 2080 | |
| 2081 | if (group_right_1) { |
| 2082 | rtn = copywin(self->win, destwin->win, sminrow, smincol, |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2083 | dminrow, dmincol, dmaxrow, dmaxcol, TRUE); |
| 2084 | return PyCursesCheckERR(rtn, "copywin"); |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 2085 | } |
| 2086 | else { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2087 | rtn = overlay(self->win, destwin->win); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2088 | return PyCursesCheckERR(rtn, "overlay"); |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 2089 | } |
| 2090 | } |
| 2091 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2092 | /*[clinic input] |
| 2093 | _curses.window.overwrite |
| 2094 | |
| 2095 | destwin: object(type="PyCursesWindowObject *", subclass_of="&PyCursesWindow_Type") |
| 2096 | |
| 2097 | [ |
| 2098 | sminrow: int |
| 2099 | smincol: int |
| 2100 | dminrow: int |
| 2101 | dmincol: int |
| 2102 | dmaxrow: int |
| 2103 | dmaxcol: int |
| 2104 | ] |
| 2105 | / |
| 2106 | |
| 2107 | Overwrite the window on top of destwin. |
| 2108 | |
| 2109 | The windows need not be the same size, in which case only the overlapping |
| 2110 | region is copied. This copy is destructive, which means that the current |
| 2111 | background character overwrites the old contents of destwin. |
| 2112 | |
| 2113 | To get fine-grained control over the copied region, the second form of |
| 2114 | overwrite() can be used. sminrow and smincol are the upper-left coordinates |
| 2115 | of the source window, the other variables mark a rectangle in the destination |
| 2116 | window. |
| 2117 | [clinic start generated code]*/ |
| 2118 | |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 2119 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2120 | _curses_window_overwrite_impl(PyCursesWindowObject *self, |
| 2121 | PyCursesWindowObject *destwin, |
| 2122 | int group_right_1, int sminrow, int smincol, |
| 2123 | int dminrow, int dmincol, int dmaxrow, |
| 2124 | int dmaxcol) |
| 2125 | /*[clinic end generated code: output=12ae007d1681be28 input=ea5de1b35cd948e0]*/ |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 2126 | { |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 2127 | int rtn; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2128 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2129 | if (group_right_1) { |
| 2130 | rtn = copywin(self->win, destwin->win, sminrow, smincol, |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2131 | dminrow, dmincol, dmaxrow, dmaxcol, FALSE); |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 2132 | return PyCursesCheckERR(rtn, "copywin"); |
| 2133 | } |
| 2134 | else { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2135 | rtn = overwrite(self->win, destwin->win); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2136 | return PyCursesCheckERR(rtn, "overwrite"); |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 2137 | } |
| 2138 | } |
| 2139 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2140 | /*[clinic input] |
| 2141 | _curses.window.putwin |
| 2142 | |
| 2143 | file: object |
| 2144 | / |
| 2145 | |
| 2146 | Write all data associated with the window into the provided file object. |
| 2147 | |
| 2148 | This information can be later retrieved using the getwin() function. |
| 2149 | [clinic start generated code]*/ |
| 2150 | |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 2151 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2152 | _curses_window_putwin(PyCursesWindowObject *self, PyObject *file) |
| 2153 | /*[clinic end generated code: output=3a25e2a5e7a040ac input=0608648e09c8ea0a]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2154 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2155 | /* We have to simulate this by writing to a temporary FILE*, |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2156 | then reading back, then writing to the argument file. */ |
Christian Heimes | 2b221b7 | 2017-03-02 11:09:01 +0100 | [diff] [blame] | 2157 | FILE *fp; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2158 | PyObject *res = NULL; |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 2159 | |
Christian Heimes | 2b221b7 | 2017-03-02 11:09:01 +0100 | [diff] [blame] | 2160 | fp = tmpfile(); |
| 2161 | if (fp == NULL) |
| 2162 | return PyErr_SetFromErrno(PyExc_OSError); |
| 2163 | if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2164 | goto exit; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2165 | res = PyCursesCheckERR(putwin(self->win, fp), "putwin"); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2166 | if (res == NULL) |
| 2167 | goto exit; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2168 | fseek(fp, 0, 0); |
| 2169 | while (1) { |
| 2170 | char buf[BUFSIZ]; |
| 2171 | Py_ssize_t n = fread(buf, 1, BUFSIZ, fp); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2172 | _Py_IDENTIFIER(write); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2173 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2174 | if (n <= 0) |
| 2175 | break; |
| 2176 | Py_DECREF(res); |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2177 | res = _PyObject_CallMethodId(file, &PyId_write, "y#", buf, n); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2178 | if (res == NULL) |
| 2179 | break; |
| 2180 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2181 | |
| 2182 | exit: |
Christian Heimes | 2b221b7 | 2017-03-02 11:09:01 +0100 | [diff] [blame] | 2183 | fclose(fp); |
Guido van Rossum | 6c95da3 | 2007-07-24 00:16:38 +0000 | [diff] [blame] | 2184 | return res; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2187 | /*[clinic input] |
| 2188 | _curses.window.redrawln |
| 2189 | |
| 2190 | beg: int |
| 2191 | Starting line number. |
| 2192 | num: int |
| 2193 | The number of lines. |
| 2194 | / |
| 2195 | |
| 2196 | Mark the specified lines corrupted. |
| 2197 | |
| 2198 | They should be completely redrawn on the next refresh() call. |
| 2199 | [clinic start generated code]*/ |
| 2200 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2201 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2202 | _curses_window_redrawln_impl(PyCursesWindowObject *self, int beg, int num) |
| 2203 | /*[clinic end generated code: output=ea216e334f9ce1b4 input=152155e258a77a7a]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2204 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2205 | return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2208 | /*[clinic input] |
| 2209 | _curses.window.refresh |
| 2210 | |
| 2211 | [ |
| 2212 | pminrow: int |
| 2213 | pmincol: int |
| 2214 | sminrow: int |
| 2215 | smincol: int |
| 2216 | smaxrow: int |
| 2217 | smaxcol: int |
| 2218 | ] |
| 2219 | / |
| 2220 | |
| 2221 | Update the display immediately. |
| 2222 | |
| 2223 | Synchronize actual screen with previous drawing/deleting methods. |
| 2224 | The 6 optional arguments can only be specified when the window is a pad |
| 2225 | created with newpad(). The additional parameters are needed to indicate |
| 2226 | what part of the pad and screen are involved. pminrow and pmincol specify |
| 2227 | the upper left-hand corner of the rectangle to be displayed in the pad. |
| 2228 | sminrow, smincol, smaxrow, and smaxcol specify the edges of the rectangle to |
| 2229 | be displayed on the screen. The lower right-hand corner of the rectangle to |
| 2230 | be displayed in the pad is calculated from the screen coordinates, since the |
| 2231 | rectangles must be the same size. Both rectangles must be entirely contained |
| 2232 | within their respective structures. Negative values of pminrow, pmincol, |
| 2233 | sminrow, or smincol are treated as if they were zero. |
| 2234 | [clinic start generated code]*/ |
| 2235 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2236 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2237 | _curses_window_refresh_impl(PyCursesWindowObject *self, int group_right_1, |
| 2238 | int pminrow, int pmincol, int sminrow, |
| 2239 | int smincol, int smaxrow, int smaxcol) |
| 2240 | /*[clinic end generated code: output=42199543115e6e63 input=95e01cb5ffc635d0]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2241 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2242 | int rtn; |
Andrew M. Kuchling | f419572 | 2000-06-23 01:36:21 +0000 | [diff] [blame] | 2243 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2244 | #ifdef py_is_pad |
| 2245 | if (py_is_pad(self->win)) { |
| 2246 | if (!group_right_1) { |
| 2247 | PyErr_SetString(PyCursesError, |
| 2248 | "refresh() for a pad requires 6 arguments"); |
| 2249 | return NULL; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2250 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2251 | Py_BEGIN_ALLOW_THREADS |
| 2252 | rtn = prefresh(self->win, pminrow, pmincol, |
| 2253 | sminrow, smincol, smaxrow, smaxcol); |
| 2254 | Py_END_ALLOW_THREADS |
| 2255 | return PyCursesCheckERR(rtn, "prefresh"); |
| 2256 | } |
| 2257 | #endif |
| 2258 | if (group_right_1) { |
| 2259 | PyErr_SetString(PyExc_TypeError, |
| 2260 | "refresh() takes no arguments (6 given)"); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2261 | return NULL; |
| 2262 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2263 | Py_BEGIN_ALLOW_THREADS |
| 2264 | rtn = wrefresh(self->win); |
| 2265 | Py_END_ALLOW_THREADS |
| 2266 | return PyCursesCheckERR(rtn, "prefresh"); |
| 2267 | } |
| 2268 | |
| 2269 | /*[clinic input] |
| 2270 | _curses.window.setscrreg |
| 2271 | |
| 2272 | top: int |
| 2273 | First line number. |
| 2274 | bottom: int |
| 2275 | Last line number. |
| 2276 | / |
| 2277 | |
| 2278 | Define a software scrolling region. |
| 2279 | |
| 2280 | All scrolling actions will take place in this region. |
| 2281 | [clinic start generated code]*/ |
| 2282 | |
| 2283 | static PyObject * |
| 2284 | _curses_window_setscrreg_impl(PyCursesWindowObject *self, int top, |
| 2285 | int bottom) |
| 2286 | /*[clinic end generated code: output=486ab5db218d2b1a input=1b517b986838bf0e]*/ |
| 2287 | { |
| 2288 | return PyCursesCheckERR(wsetscrreg(self->win, top, bottom), "wsetscrreg"); |
| 2289 | } |
| 2290 | |
| 2291 | /*[clinic input] |
| 2292 | _curses.window.subwin |
| 2293 | |
| 2294 | [ |
| 2295 | nlines: int = 0 |
| 2296 | Height. |
| 2297 | ncols: int = 0 |
| 2298 | Width. |
| 2299 | ] |
| 2300 | begin_y: int |
| 2301 | Top side y-coordinate. |
| 2302 | begin_x: int |
| 2303 | Left side x-coordinate. |
| 2304 | / |
| 2305 | |
| 2306 | Create a sub-window (screen-relative coordinates). |
| 2307 | |
| 2308 | By default, the sub-window will extend from the specified position to the |
| 2309 | lower right corner of the window. |
| 2310 | [clinic start generated code]*/ |
| 2311 | |
| 2312 | static PyObject * |
| 2313 | _curses_window_subwin_impl(PyCursesWindowObject *self, int group_left_1, |
| 2314 | int nlines, int ncols, int begin_y, int begin_x) |
| 2315 | /*[clinic end generated code: output=93e898afc348f59a input=2129fa47fd57721c]*/ |
| 2316 | { |
| 2317 | WINDOW *win; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2318 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2319 | /* printf("Subwin: %i %i %i %i \n", nlines, ncols, begin_y, begin_x); */ |
Masayuki Yamamoto | 8bc7d63 | 2017-11-01 21:05:26 +0900 | [diff] [blame] | 2320 | #ifdef py_is_pad |
| 2321 | if (py_is_pad(self->win)) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2322 | win = subpad(self->win, nlines, ncols, begin_y, begin_x); |
Masayuki Yamamoto | 8bc7d63 | 2017-11-01 21:05:26 +0900 | [diff] [blame] | 2323 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2324 | else |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 2325 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2326 | win = subwin(self->win, nlines, ncols, begin_y, begin_x); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2327 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2328 | if (win == NULL) { |
| 2329 | PyErr_SetString(PyCursesError, catchall_NULL); |
| 2330 | return NULL; |
| 2331 | } |
| 2332 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2333 | return (PyObject *)PyCursesWindow_New(win, self->encoding); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2336 | /*[clinic input] |
| 2337 | _curses.window.scroll |
| 2338 | |
| 2339 | [ |
| 2340 | lines: int = 1 |
| 2341 | Number of lines to scroll. |
| 2342 | ] |
| 2343 | / |
| 2344 | |
| 2345 | Scroll the screen or scrolling region. |
| 2346 | |
| 2347 | Scroll upward if the argument is positive and downward if it is negative. |
| 2348 | [clinic start generated code]*/ |
| 2349 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2350 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2351 | _curses_window_scroll_impl(PyCursesWindowObject *self, int group_right_1, |
| 2352 | int lines) |
| 2353 | /*[clinic end generated code: output=4541a8a11852d360 input=c969ca0cfabbdbec]*/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2354 | { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2355 | if (!group_right_1) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2356 | return PyCursesCheckERR(scroll(self->win), "scroll"); |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2357 | } |
| 2358 | else { |
| 2359 | return PyCursesCheckERR(wscrl(self->win, lines), "scroll"); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2360 | } |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2363 | /*[clinic input] |
| 2364 | _curses.window.touchline |
| 2365 | |
| 2366 | start: int |
| 2367 | count: int |
| 2368 | [ |
| 2369 | changed: bool(accept={int}) = True |
| 2370 | ] |
| 2371 | / |
| 2372 | |
| 2373 | Pretend count lines have been changed, starting with line start. |
| 2374 | |
| 2375 | If changed is supplied, it specifies whether the affected lines are marked |
| 2376 | as having been changed (changed=True) or unchanged (changed=False). |
| 2377 | [clinic start generated code]*/ |
| 2378 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2379 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2380 | _curses_window_touchline_impl(PyCursesWindowObject *self, int start, |
| 2381 | int count, int group_right_1, int changed) |
| 2382 | /*[clinic end generated code: output=65d05b3f7438c61d input=918ad1cbdadf93ea]*/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2383 | { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2384 | if (!group_right_1) { |
| 2385 | return PyCursesCheckERR(touchline(self->win, start, count), "touchline"); |
| 2386 | } |
| 2387 | else { |
| 2388 | return PyCursesCheckERR(wtouchln(self->win, start, count, changed), "touchline"); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2389 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2390 | } |
| 2391 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2392 | /*[clinic input] |
| 2393 | _curses.window.vline |
| 2394 | |
| 2395 | [ |
| 2396 | y: int |
| 2397 | Starting Y-coordinate. |
| 2398 | x: int |
| 2399 | Starting X-coordinate. |
| 2400 | ] |
| 2401 | |
| 2402 | ch: object |
| 2403 | Character to draw. |
| 2404 | n: int |
| 2405 | Line length. |
| 2406 | |
| 2407 | [ |
| 2408 | attr: long(c_default="A_NORMAL") = _curses.A_NORMAL |
| 2409 | Attributes for the character. |
| 2410 | ] |
| 2411 | / |
| 2412 | |
| 2413 | Display a vertical line. |
| 2414 | [clinic start generated code]*/ |
| 2415 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2416 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2417 | _curses_window_vline_impl(PyCursesWindowObject *self, int group_left_1, |
| 2418 | int y, int x, PyObject *ch, int n, |
| 2419 | int group_right_1, long attr) |
| 2420 | /*[clinic end generated code: output=287ad1cc8982217f input=a6f2dc86a4648b32]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2421 | { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2422 | chtype ch_; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2423 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2424 | if (!PyCurses_ConvertToChtype(self, ch, &ch_)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2425 | return NULL; |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2426 | if (group_left_1) { |
| 2427 | if (wmove(self->win, y, x) == ERR) |
| 2428 | return PyCursesCheckERR(ERR, "wmove"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2429 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2430 | return PyCursesCheckERR(wvline(self->win, ch_ | (attr_t)attr, n), "vline"); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2433 | static PyObject * |
| 2434 | PyCursesWindow_get_encoding(PyCursesWindowObject *self, void *closure) |
| 2435 | { |
| 2436 | return PyUnicode_FromString(self->encoding); |
| 2437 | } |
| 2438 | |
| 2439 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 2440 | PyCursesWindow_set_encoding(PyCursesWindowObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2441 | { |
| 2442 | PyObject *ascii; |
| 2443 | char *encoding; |
| 2444 | |
| 2445 | /* It is illegal to del win.encoding */ |
| 2446 | if (value == NULL) { |
| 2447 | PyErr_SetString(PyExc_TypeError, |
| 2448 | "encoding may not be deleted"); |
| 2449 | return -1; |
| 2450 | } |
| 2451 | |
| 2452 | if (!PyUnicode_Check(value)) { |
| 2453 | PyErr_SetString(PyExc_TypeError, |
| 2454 | "setting encoding to a non-string"); |
| 2455 | return -1; |
| 2456 | } |
| 2457 | ascii = PyUnicode_AsASCIIString(value); |
| 2458 | if (ascii == NULL) |
| 2459 | return -1; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 2460 | encoding = _PyMem_Strdup(PyBytes_AS_STRING(ascii)); |
Ross Lagerwall | 2dabaf6 | 2012-09-07 08:34:23 +0200 | [diff] [blame] | 2461 | Py_DECREF(ascii); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2462 | if (encoding == NULL) { |
| 2463 | PyErr_NoMemory(); |
| 2464 | return -1; |
| 2465 | } |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 2466 | PyMem_Free(self->encoding); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2467 | self->encoding = encoding; |
| 2468 | return 0; |
| 2469 | } |
| 2470 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2471 | #include "clinic/_cursesmodule.c.h" |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2472 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2473 | static PyMethodDef PyCursesWindow_Methods[] = { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2474 | _CURSES_WINDOW_ADDCH_METHODDEF |
| 2475 | _CURSES_WINDOW_ADDNSTR_METHODDEF |
| 2476 | _CURSES_WINDOW_ADDSTR_METHODDEF |
| 2477 | _CURSES_WINDOW_ATTROFF_METHODDEF |
| 2478 | _CURSES_WINDOW_ATTRON_METHODDEF |
| 2479 | _CURSES_WINDOW_ATTRSET_METHODDEF |
| 2480 | _CURSES_WINDOW_BKGD_METHODDEF |
Serhiy Storchaka | 894ebd0 | 2017-11-01 14:34:20 +0200 | [diff] [blame] | 2481 | #ifdef HAVE_CURSES_WCHGAT |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2482 | {"chgat", (PyCFunction)PyCursesWindow_ChgAt, METH_VARARGS}, |
Serhiy Storchaka | 894ebd0 | 2017-11-01 14:34:20 +0200 | [diff] [blame] | 2483 | #endif |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2484 | _CURSES_WINDOW_BKGDSET_METHODDEF |
| 2485 | _CURSES_WINDOW_BORDER_METHODDEF |
| 2486 | _CURSES_WINDOW_BOX_METHODDEF |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2487 | {"clear", (PyCFunction)PyCursesWindow_wclear, METH_NOARGS}, |
| 2488 | {"clearok", (PyCFunction)PyCursesWindow_clearok, METH_VARARGS}, |
| 2489 | {"clrtobot", (PyCFunction)PyCursesWindow_wclrtobot, METH_NOARGS}, |
| 2490 | {"clrtoeol", (PyCFunction)PyCursesWindow_wclrtoeol, METH_NOARGS}, |
| 2491 | {"cursyncup", (PyCFunction)PyCursesWindow_wcursyncup, METH_NOARGS}, |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2492 | _CURSES_WINDOW_DELCH_METHODDEF |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2493 | {"deleteln", (PyCFunction)PyCursesWindow_wdeleteln, METH_NOARGS}, |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2494 | _CURSES_WINDOW_DERWIN_METHODDEF |
| 2495 | _CURSES_WINDOW_ECHOCHAR_METHODDEF |
| 2496 | _CURSES_WINDOW_ENCLOSE_METHODDEF |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2497 | {"erase", (PyCFunction)PyCursesWindow_werase, METH_NOARGS}, |
| 2498 | {"getbegyx", (PyCFunction)PyCursesWindow_getbegyx, METH_NOARGS}, |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2499 | _CURSES_WINDOW_GETBKGD_METHODDEF |
| 2500 | _CURSES_WINDOW_GETCH_METHODDEF |
| 2501 | _CURSES_WINDOW_GETKEY_METHODDEF |
| 2502 | _CURSES_WINDOW_GET_WCH_METHODDEF |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2503 | {"getmaxyx", (PyCFunction)PyCursesWindow_getmaxyx, METH_NOARGS}, |
| 2504 | {"getparyx", (PyCFunction)PyCursesWindow_getparyx, METH_NOARGS}, |
| 2505 | {"getstr", (PyCFunction)PyCursesWindow_GetStr, METH_VARARGS}, |
| 2506 | {"getyx", (PyCFunction)PyCursesWindow_getyx, METH_NOARGS}, |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2507 | _CURSES_WINDOW_HLINE_METHODDEF |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2508 | {"idcok", (PyCFunction)PyCursesWindow_idcok, METH_VARARGS}, |
| 2509 | {"idlok", (PyCFunction)PyCursesWindow_idlok, METH_VARARGS}, |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 2510 | #ifdef HAVE_CURSES_IMMEDOK |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2511 | {"immedok", (PyCFunction)PyCursesWindow_immedok, METH_VARARGS}, |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 2512 | #endif |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2513 | _CURSES_WINDOW_INCH_METHODDEF |
| 2514 | _CURSES_WINDOW_INSCH_METHODDEF |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2515 | {"insdelln", (PyCFunction)PyCursesWindow_winsdelln, METH_VARARGS}, |
| 2516 | {"insertln", (PyCFunction)PyCursesWindow_winsertln, METH_NOARGS}, |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2517 | _CURSES_WINDOW_INSNSTR_METHODDEF |
| 2518 | _CURSES_WINDOW_INSSTR_METHODDEF |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2519 | {"instr", (PyCFunction)PyCursesWindow_InStr, METH_VARARGS}, |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2520 | _CURSES_WINDOW_IS_LINETOUCHED_METHODDEF |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2521 | {"is_wintouched", (PyCFunction)PyCursesWindow_is_wintouched, METH_NOARGS}, |
| 2522 | {"keypad", (PyCFunction)PyCursesWindow_keypad, METH_VARARGS}, |
| 2523 | {"leaveok", (PyCFunction)PyCursesWindow_leaveok, METH_VARARGS}, |
| 2524 | {"move", (PyCFunction)PyCursesWindow_wmove, METH_VARARGS}, |
| 2525 | {"mvderwin", (PyCFunction)PyCursesWindow_mvderwin, METH_VARARGS}, |
| 2526 | {"mvwin", (PyCFunction)PyCursesWindow_mvwin, METH_VARARGS}, |
| 2527 | {"nodelay", (PyCFunction)PyCursesWindow_nodelay, METH_VARARGS}, |
| 2528 | {"notimeout", (PyCFunction)PyCursesWindow_notimeout, METH_VARARGS}, |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2529 | _CURSES_WINDOW_NOUTREFRESH_METHODDEF |
| 2530 | _CURSES_WINDOW_OVERLAY_METHODDEF |
| 2531 | _CURSES_WINDOW_OVERWRITE_METHODDEF |
| 2532 | _CURSES_WINDOW_PUTWIN_METHODDEF |
| 2533 | _CURSES_WINDOW_REDRAWLN_METHODDEF |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2534 | {"redrawwin", (PyCFunction)PyCursesWindow_redrawwin, METH_NOARGS}, |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2535 | _CURSES_WINDOW_REFRESH_METHODDEF |
Andrew M. Kuchling | a1e4b05 | 2000-06-27 21:49:47 +0000 | [diff] [blame] | 2536 | #ifndef STRICT_SYSV_CURSES |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2537 | {"resize", (PyCFunction)PyCursesWindow_wresize, METH_VARARGS}, |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2538 | #endif |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2539 | _CURSES_WINDOW_SCROLL_METHODDEF |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2540 | {"scrollok", (PyCFunction)PyCursesWindow_scrollok, METH_VARARGS}, |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2541 | _CURSES_WINDOW_SETSCRREG_METHODDEF |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2542 | {"standend", (PyCFunction)PyCursesWindow_wstandend, METH_NOARGS}, |
| 2543 | {"standout", (PyCFunction)PyCursesWindow_wstandout, METH_NOARGS}, |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2544 | {"subpad", (PyCFunction)_curses_window_subwin, METH_VARARGS, _curses_window_subwin__doc__}, |
| 2545 | _CURSES_WINDOW_SUBWIN_METHODDEF |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2546 | {"syncdown", (PyCFunction)PyCursesWindow_wsyncdown, METH_NOARGS}, |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 2547 | #ifdef HAVE_CURSES_SYNCOK |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2548 | {"syncok", (PyCFunction)PyCursesWindow_syncok, METH_VARARGS}, |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 2549 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2550 | {"syncup", (PyCFunction)PyCursesWindow_wsyncup, METH_NOARGS}, |
| 2551 | {"timeout", (PyCFunction)PyCursesWindow_wtimeout, METH_VARARGS}, |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2552 | _CURSES_WINDOW_TOUCHLINE_METHODDEF |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2553 | {"touchwin", (PyCFunction)PyCursesWindow_touchwin, METH_NOARGS}, |
| 2554 | {"untouchwin", (PyCFunction)PyCursesWindow_untouchwin, METH_NOARGS}, |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2555 | _CURSES_WINDOW_VLINE_METHODDEF |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2556 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2557 | }; |
| 2558 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2559 | static PyGetSetDef PyCursesWindow_getsets[] = { |
| 2560 | {"encoding", |
| 2561 | (getter)PyCursesWindow_get_encoding, |
| 2562 | (setter)PyCursesWindow_set_encoding, |
Ronald Oussoren | 2a7fe03 | 2012-01-17 16:53:20 +0100 | [diff] [blame] | 2563 | "the typecode character used to create the array"}, |
| 2564 | {NULL, NULL, NULL, NULL } /* sentinel */ |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2565 | }; |
| 2566 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2567 | /* -------------------------------------------------------*/ |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 2568 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2569 | PyTypeObject PyCursesWindow_Type = { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2570 | PyVarObject_HEAD_INIT(NULL, 0) |
Victor Stinner | 61e2bc7 | 2017-02-12 23:42:02 +0100 | [diff] [blame] | 2571 | "_curses.window", /*tp_name*/ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2572 | sizeof(PyCursesWindowObject), /*tp_basicsize*/ |
| 2573 | 0, /*tp_itemsize*/ |
| 2574 | /* methods */ |
| 2575 | (destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2576 | 0, /*tp_vectorcall_offset*/ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2577 | (getattrfunc)0, /*tp_getattr*/ |
| 2578 | (setattrfunc)0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2579 | 0, /*tp_as_async*/ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2580 | 0, /*tp_repr*/ |
| 2581 | 0, /*tp_as_number*/ |
| 2582 | 0, /*tp_as_sequence*/ |
| 2583 | 0, /*tp_as_mapping*/ |
| 2584 | 0, /*tp_hash*/ |
| 2585 | 0, /*tp_call*/ |
| 2586 | 0, /*tp_str*/ |
| 2587 | 0, /*tp_getattro*/ |
| 2588 | 0, /*tp_setattro*/ |
| 2589 | 0, /*tp_as_buffer*/ |
| 2590 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 2591 | 0, /*tp_doc*/ |
| 2592 | 0, /*tp_traverse*/ |
| 2593 | 0, /*tp_clear*/ |
| 2594 | 0, /*tp_richcompare*/ |
| 2595 | 0, /*tp_weaklistoffset*/ |
| 2596 | 0, /*tp_iter*/ |
| 2597 | 0, /*tp_iternext*/ |
| 2598 | PyCursesWindow_Methods, /*tp_methods*/ |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2599 | 0, /* tp_members */ |
| 2600 | PyCursesWindow_getsets, /* tp_getset */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2601 | }; |
| 2602 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2603 | /* Function Prototype Macros - They are ugly but very, very useful. ;-) |
| 2604 | |
| 2605 | X - function name |
| 2606 | TYPE - parameter Type |
| 2607 | ERGSTR - format string for construction of the return value |
| 2608 | PARSESTR - format string for argument parsing |
| 2609 | */ |
| 2610 | |
| 2611 | #define NoArgNoReturnFunctionBody(X) \ |
| 2612 | { \ |
| 2613 | PyCursesInitialised \ |
| 2614 | return PyCursesCheckERR(X(), # X); } |
| 2615 | |
| 2616 | #define NoArgOrFlagNoReturnFunctionBody(X, flag) \ |
| 2617 | { \ |
| 2618 | PyCursesInitialised \ |
| 2619 | if (flag) \ |
| 2620 | return PyCursesCheckERR(X(), # X); \ |
| 2621 | else \ |
| 2622 | return PyCursesCheckERR(no ## X(), # X); \ |
| 2623 | } |
| 2624 | |
| 2625 | #define NoArgReturnIntFunctionBody(X) \ |
| 2626 | { \ |
| 2627 | PyCursesInitialised \ |
| 2628 | return PyLong_FromLong((long) X()); } |
| 2629 | |
| 2630 | |
| 2631 | #define NoArgReturnStringFunctionBody(X) \ |
| 2632 | { \ |
| 2633 | PyCursesInitialised \ |
| 2634 | return PyBytes_FromString(X()); } |
| 2635 | |
| 2636 | #define NoArgTrueFalseFunctionBody(X) \ |
| 2637 | { \ |
| 2638 | PyCursesInitialised \ |
| 2639 | return PyBool_FromLong(X()); } |
| 2640 | |
| 2641 | #define NoArgNoReturnVoidFunctionBody(X) \ |
| 2642 | { \ |
| 2643 | PyCursesInitialised \ |
| 2644 | X(); \ |
| 2645 | Py_RETURN_NONE; } |
| 2646 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2647 | /********************************************************************* |
| 2648 | Global Functions |
| 2649 | **********************************************************************/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2650 | |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 2651 | #ifdef HAVE_CURSES_FILTER |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2652 | /*[clinic input] |
| 2653 | _curses.filter |
| 2654 | |
| 2655 | [clinic start generated code]*/ |
| 2656 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2657 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2658 | _curses_filter_impl(PyObject *module) |
| 2659 | /*[clinic end generated code: output=fb5b8a3642eb70b5 input=668c75a6992d3624]*/ |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 2660 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2661 | /* not checking for PyCursesInitialised here since filter() must |
| 2662 | be called before initscr() */ |
| 2663 | filter(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2664 | Py_RETURN_NONE; |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 2665 | } |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 2666 | #endif |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 2667 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2668 | /*[clinic input] |
| 2669 | _curses.baudrate |
| 2670 | |
| 2671 | Return the output speed of the terminal in bits per second. |
| 2672 | [clinic start generated code]*/ |
| 2673 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 2674 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2675 | _curses_baudrate_impl(PyObject *module) |
| 2676 | /*[clinic end generated code: output=3c63c6c401d7d9c0 input=921f022ed04a0fd9]*/ |
| 2677 | NoArgReturnIntFunctionBody(baudrate) |
| 2678 | |
| 2679 | /*[clinic input] |
| 2680 | _curses.beep |
| 2681 | |
| 2682 | Emit a short attention sound. |
| 2683 | [clinic start generated code]*/ |
| 2684 | |
| 2685 | static PyObject * |
| 2686 | _curses_beep_impl(PyObject *module) |
| 2687 | /*[clinic end generated code: output=425274962abe49a2 input=a35698ca7d0162bc]*/ |
| 2688 | NoArgNoReturnFunctionBody(beep) |
| 2689 | |
| 2690 | /*[clinic input] |
| 2691 | _curses.can_change_color |
| 2692 | |
| 2693 | Return True if the programmer can change the colors displayed by the terminal. |
| 2694 | [clinic start generated code]*/ |
| 2695 | |
| 2696 | static PyObject * |
| 2697 | _curses_can_change_color_impl(PyObject *module) |
| 2698 | /*[clinic end generated code: output=359df8c3c77d8bf1 input=d7718884de0092f2]*/ |
| 2699 | NoArgTrueFalseFunctionBody(can_change_color) |
| 2700 | |
| 2701 | /*[clinic input] |
| 2702 | _curses.cbreak |
| 2703 | |
| 2704 | flag: bool(accept={int}) = True |
| 2705 | If false, the effect is the same as calling nocbreak(). |
| 2706 | / |
| 2707 | |
| 2708 | Enter cbreak mode. |
| 2709 | |
| 2710 | In cbreak mode (sometimes called "rare" mode) normal tty line buffering is |
| 2711 | turned off and characters are available to be read one by one. However, |
| 2712 | unlike raw mode, special characters (interrupt, quit, suspend, and flow |
| 2713 | control) retain their effects on the tty driver and calling program. |
| 2714 | Calling first raw() then cbreak() leaves the terminal in cbreak mode. |
| 2715 | [clinic start generated code]*/ |
| 2716 | |
| 2717 | static PyObject * |
| 2718 | _curses_cbreak_impl(PyObject *module, int flag) |
| 2719 | /*[clinic end generated code: output=9f9dee9664769751 input=150be619eb1f1458]*/ |
| 2720 | NoArgOrFlagNoReturnFunctionBody(cbreak, flag) |
| 2721 | |
| 2722 | /*[clinic input] |
| 2723 | _curses.color_content |
| 2724 | |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 2725 | color_number: color |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 2726 | The number of the color (0 - (COLORS-1)). |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2727 | / |
| 2728 | |
| 2729 | Return the red, green, and blue (RGB) components of the specified color. |
| 2730 | |
| 2731 | A 3-tuple is returned, containing the R, G, B values for the given color, |
| 2732 | which will be between 0 (no component) and 1000 (maximum amount of component). |
| 2733 | [clinic start generated code]*/ |
| 2734 | |
| 2735 | static PyObject * |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 2736 | _curses_color_content_impl(PyObject *module, int color_number) |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 2737 | /*[clinic end generated code: output=17b466df7054e0de input=03b5ed0472662aea]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2738 | { |
Serhiy Storchaka | 59f9b4e | 2021-01-05 09:13:15 +0200 | [diff] [blame] | 2739 | _CURSES_COLOR_VAL_TYPE r,g,b; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2740 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2741 | PyCursesInitialised; |
| 2742 | PyCursesInitialisedColor; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2743 | |
Serhiy Storchaka | 59f9b4e | 2021-01-05 09:13:15 +0200 | [diff] [blame] | 2744 | if (_COLOR_CONTENT_FUNC(color_number, &r, &g, &b) == ERR) { |
| 2745 | PyErr_Format(PyCursesError, "%s() returned ERR", |
| 2746 | Py_STRINGIFY(_COLOR_CONTENT_FUNC)); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2747 | return NULL; |
| 2748 | } |
Serhiy Storchaka | 59f9b4e | 2021-01-05 09:13:15 +0200 | [diff] [blame] | 2749 | |
| 2750 | return Py_BuildValue("(iii)", r, g, b); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2753 | /*[clinic input] |
| 2754 | _curses.color_pair |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2755 | |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 2756 | pair_number: int |
| 2757 | The number of the color pair. |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2758 | / |
| 2759 | |
| 2760 | Return the attribute value for displaying text in the specified color. |
| 2761 | |
| 2762 | This attribute value can be combined with A_STANDOUT, A_REVERSE, and the |
| 2763 | other A_* attributes. pair_number() is the counterpart to this function. |
| 2764 | [clinic start generated code]*/ |
| 2765 | |
| 2766 | static PyObject * |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 2767 | _curses_color_pair_impl(PyObject *module, int pair_number) |
| 2768 | /*[clinic end generated code: output=60718abb10ce9feb input=6034e9146f343802]*/ |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2769 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2770 | PyCursesInitialised; |
| 2771 | PyCursesInitialisedColor; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2772 | |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 2773 | return PyLong_FromLong(COLOR_PAIR(pair_number)); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2776 | /*[clinic input] |
| 2777 | _curses.curs_set |
| 2778 | |
| 2779 | visibility: int |
| 2780 | 0 for invisible, 1 for normal visible, or 2 for very visible. |
| 2781 | / |
| 2782 | |
| 2783 | Set the cursor state. |
| 2784 | |
| 2785 | If the terminal supports the visibility requested, the previous cursor |
| 2786 | state is returned; otherwise, an exception is raised. On many terminals, |
| 2787 | the "visible" mode is an underline cursor and the "very visible" mode is |
| 2788 | a block cursor. |
| 2789 | [clinic start generated code]*/ |
| 2790 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2791 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2792 | _curses_curs_set_impl(PyObject *module, int visibility) |
| 2793 | /*[clinic end generated code: output=ee8e62483b1d6cd4 input=81a7924a65d29504]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2794 | { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2795 | int erg; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2796 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2797 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2798 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2799 | erg = curs_set(visibility); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2800 | if (erg == ERR) return PyCursesCheckERR(erg, "curs_set"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2801 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2802 | return PyLong_FromLong((long) erg); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2805 | /*[clinic input] |
| 2806 | _curses.def_prog_mode |
| 2807 | |
| 2808 | Save the current terminal mode as the "program" mode. |
| 2809 | |
| 2810 | The "program" mode is the mode when the running program is using curses. |
| 2811 | |
| 2812 | Subsequent calls to reset_prog_mode() will restore this mode. |
| 2813 | [clinic start generated code]*/ |
| 2814 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2815 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2816 | _curses_def_prog_mode_impl(PyObject *module) |
| 2817 | /*[clinic end generated code: output=05d5a351fff874aa input=768b9cace620dda5]*/ |
| 2818 | NoArgNoReturnFunctionBody(def_prog_mode) |
| 2819 | |
| 2820 | /*[clinic input] |
| 2821 | _curses.def_shell_mode |
| 2822 | |
| 2823 | Save the current terminal mode as the "shell" mode. |
| 2824 | |
| 2825 | The "shell" mode is the mode when the running program is not using curses. |
| 2826 | |
| 2827 | Subsequent calls to reset_shell_mode() will restore this mode. |
| 2828 | [clinic start generated code]*/ |
| 2829 | |
| 2830 | static PyObject * |
| 2831 | _curses_def_shell_mode_impl(PyObject *module) |
| 2832 | /*[clinic end generated code: output=d6e42f5c768f860f input=5ead21f6f0baa894]*/ |
| 2833 | NoArgNoReturnFunctionBody(def_shell_mode) |
| 2834 | |
| 2835 | /*[clinic input] |
| 2836 | _curses.delay_output |
| 2837 | |
| 2838 | ms: int |
| 2839 | Duration in milliseconds. |
| 2840 | / |
| 2841 | |
| 2842 | Insert a pause in output. |
| 2843 | [clinic start generated code]*/ |
| 2844 | |
| 2845 | static PyObject * |
| 2846 | _curses_delay_output_impl(PyObject *module, int ms) |
| 2847 | /*[clinic end generated code: output=b6613a67f17fa4f4 input=5316457f5f59196c]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2848 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2849 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2850 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2851 | return PyCursesCheckERR(delay_output(ms), "delay_output"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2854 | /*[clinic input] |
| 2855 | _curses.doupdate |
| 2856 | |
| 2857 | Update the physical screen to match the virtual screen. |
| 2858 | [clinic start generated code]*/ |
| 2859 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2860 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2861 | _curses_doupdate_impl(PyObject *module) |
| 2862 | /*[clinic end generated code: output=f34536975a75680c input=8da80914432a6489]*/ |
| 2863 | NoArgNoReturnFunctionBody(doupdate) |
| 2864 | |
| 2865 | /*[clinic input] |
| 2866 | _curses.echo |
| 2867 | |
| 2868 | flag: bool(accept={int}) = True |
| 2869 | If false, the effect is the same as calling noecho(). |
| 2870 | / |
| 2871 | |
| 2872 | Enter echo mode. |
| 2873 | |
| 2874 | In echo mode, each character input is echoed to the screen as it is entered. |
| 2875 | [clinic start generated code]*/ |
| 2876 | |
| 2877 | static PyObject * |
| 2878 | _curses_echo_impl(PyObject *module, int flag) |
| 2879 | /*[clinic end generated code: output=03acb2ddfa6c8729 input=2e9e891d637eac5d]*/ |
| 2880 | NoArgOrFlagNoReturnFunctionBody(echo, flag) |
| 2881 | |
| 2882 | /*[clinic input] |
| 2883 | _curses.endwin |
| 2884 | |
| 2885 | De-initialize the library, and return terminal to normal status. |
| 2886 | [clinic start generated code]*/ |
| 2887 | |
| 2888 | static PyObject * |
| 2889 | _curses_endwin_impl(PyObject *module) |
| 2890 | /*[clinic end generated code: output=c0150cd96d2f4128 input=e172cfa43062f3fa]*/ |
| 2891 | NoArgNoReturnFunctionBody(endwin) |
| 2892 | |
| 2893 | /*[clinic input] |
| 2894 | _curses.erasechar |
| 2895 | |
| 2896 | Return the user's current erase character. |
| 2897 | [clinic start generated code]*/ |
| 2898 | |
| 2899 | static PyObject * |
| 2900 | _curses_erasechar_impl(PyObject *module) |
| 2901 | /*[clinic end generated code: output=3df305dc6b926b3f input=628c136c3c5758d3]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2902 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2903 | char ch; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2904 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2905 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2906 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2907 | ch = erasechar(); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2908 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2909 | return PyBytes_FromStringAndSize(&ch, 1); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2912 | /*[clinic input] |
| 2913 | _curses.flash |
| 2914 | |
| 2915 | Flash the screen. |
| 2916 | |
| 2917 | That is, change it to reverse-video and then change it back in a short interval. |
| 2918 | [clinic start generated code]*/ |
| 2919 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2920 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2921 | _curses_flash_impl(PyObject *module) |
| 2922 | /*[clinic end generated code: output=488b8a0ebd9ea9b8 input=02fdfb06c8fc3171]*/ |
| 2923 | NoArgNoReturnFunctionBody(flash) |
| 2924 | |
| 2925 | /*[clinic input] |
| 2926 | _curses.flushinp |
| 2927 | |
| 2928 | Flush all input buffers. |
| 2929 | |
| 2930 | This throws away any typeahead that has been typed by the user and has not |
| 2931 | yet been processed by the program. |
| 2932 | [clinic start generated code]*/ |
| 2933 | |
| 2934 | static PyObject * |
| 2935 | _curses_flushinp_impl(PyObject *module) |
| 2936 | /*[clinic end generated code: output=7e7a1fc1473960f5 input=59d042e705cef5ec]*/ |
| 2937 | NoArgNoReturnVoidFunctionBody(flushinp) |
| 2938 | |
| 2939 | #ifdef getsyx |
| 2940 | /*[clinic input] |
| 2941 | _curses.getsyx |
| 2942 | |
| 2943 | Return the current coordinates of the virtual screen cursor. |
| 2944 | |
| 2945 | Return a (y, x) tuple. If leaveok is currently true, return (-1, -1). |
| 2946 | [clinic start generated code]*/ |
| 2947 | |
| 2948 | static PyObject * |
| 2949 | _curses_getsyx_impl(PyObject *module) |
| 2950 | /*[clinic end generated code: output=c8e6c3f42349a038 input=9e1f862f3b4f7cba]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2951 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2952 | int x = 0; |
| 2953 | int y = 0; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2954 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2955 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2956 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2957 | getsyx(y, x); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2958 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2959 | return Py_BuildValue("(ii)", y, x); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2960 | } |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 2961 | #endif |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2962 | |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2963 | #ifdef NCURSES_MOUSE_VERSION |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2964 | /*[clinic input] |
| 2965 | _curses.getmouse |
| 2966 | |
| 2967 | Retrieve the queued mouse event. |
| 2968 | |
| 2969 | After getch() returns KEY_MOUSE to signal a mouse event, this function |
| 2970 | returns a 5-tuple (id, x, y, z, bstate). |
| 2971 | [clinic start generated code]*/ |
| 2972 | |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2973 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2974 | _curses_getmouse_impl(PyObject *module) |
| 2975 | /*[clinic end generated code: output=ccf4242546b9cfa8 input=5b756ee6f5b481b1]*/ |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2976 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2977 | int rtn; |
| 2978 | MEVENT event; |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2979 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2980 | PyCursesInitialised; |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2981 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2982 | rtn = getmouse( &event ); |
| 2983 | if (rtn == ERR) { |
| 2984 | PyErr_SetString(PyCursesError, "getmouse() returned ERR"); |
| 2985 | return NULL; |
| 2986 | } |
Serhiy Storchaka | 4f469c0 | 2017-11-01 20:48:49 +0200 | [diff] [blame] | 2987 | return Py_BuildValue("(hiiik)", |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2988 | (short)event.id, |
Serhiy Storchaka | 4f469c0 | 2017-11-01 20:48:49 +0200 | [diff] [blame] | 2989 | (int)event.x, (int)event.y, (int)event.z, |
| 2990 | (unsigned long) event.bstate); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 2993 | /*[clinic input] |
| 2994 | _curses.ungetmouse |
| 2995 | |
| 2996 | id: short |
| 2997 | x: int |
| 2998 | y: int |
| 2999 | z: int |
| 3000 | bstate: unsigned_long(bitwise=True) |
| 3001 | / |
| 3002 | |
| 3003 | Push a KEY_MOUSE event onto the input queue. |
| 3004 | |
| 3005 | The following getmouse() will return the given state data. |
| 3006 | [clinic start generated code]*/ |
| 3007 | |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3008 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3009 | _curses_ungetmouse_impl(PyObject *module, short id, int x, int y, int z, |
| 3010 | unsigned long bstate) |
| 3011 | /*[clinic end generated code: output=3430c9b0fc5c4341 input=fd650b2ca5a01e8f]*/ |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3012 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3013 | MEVENT event; |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3014 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3015 | PyCursesInitialised; |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3016 | |
Serhiy Storchaka | 4f469c0 | 2017-11-01 20:48:49 +0200 | [diff] [blame] | 3017 | event.id = id; |
| 3018 | event.x = x; |
| 3019 | event.y = y; |
| 3020 | event.z = z; |
| 3021 | event.bstate = bstate; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3022 | return PyCursesCheckERR(ungetmouse(&event), "ungetmouse"); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3023 | } |
| 3024 | #endif |
| 3025 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3026 | /*[clinic input] |
| 3027 | _curses.getwin |
| 3028 | |
| 3029 | file: object |
| 3030 | / |
| 3031 | |
| 3032 | Read window related data stored in the file by an earlier putwin() call. |
| 3033 | |
| 3034 | The routine then creates and initializes a new window using that data, |
| 3035 | returning the new window object. |
| 3036 | [clinic start generated code]*/ |
| 3037 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3038 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3039 | _curses_getwin(PyObject *module, PyObject *file) |
| 3040 | /*[clinic end generated code: output=a79e0df3379af756 input=f713d2bba0e4c929]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3041 | { |
Christian Heimes | 2b221b7 | 2017-03-02 11:09:01 +0100 | [diff] [blame] | 3042 | FILE *fp; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3043 | PyObject *data; |
| 3044 | size_t datalen; |
| 3045 | WINDOW *win; |
Benjamin Peterson | 18b7191 | 2013-05-16 15:29:44 -0500 | [diff] [blame] | 3046 | _Py_IDENTIFIER(read); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3047 | PyObject *res = NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3048 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3049 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3050 | |
Christian Heimes | 2b221b7 | 2017-03-02 11:09:01 +0100 | [diff] [blame] | 3051 | fp = tmpfile(); |
| 3052 | if (fp == NULL) |
| 3053 | return PyErr_SetFromErrno(PyExc_OSError); |
| 3054 | |
| 3055 | if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3056 | goto error; |
Christian Heimes | 2b221b7 | 2017-03-02 11:09:01 +0100 | [diff] [blame] | 3057 | |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 3058 | data = _PyObject_CallMethodIdNoArgs(file, &PyId_read); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3059 | if (data == NULL) |
| 3060 | goto error; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3061 | if (!PyBytes_Check(data)) { |
| 3062 | PyErr_Format(PyExc_TypeError, |
| 3063 | "f.read() returned %.100s instead of bytes", |
Victor Stinner | daa9756 | 2020-02-07 03:37:06 +0100 | [diff] [blame] | 3064 | Py_TYPE(data)->tp_name); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3065 | Py_DECREF(data); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3066 | goto error; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3067 | } |
| 3068 | datalen = PyBytes_GET_SIZE(data); |
| 3069 | if (fwrite(PyBytes_AS_STRING(data), 1, datalen, fp) != datalen) { |
| 3070 | Py_DECREF(data); |
Christian Heimes | 2b221b7 | 2017-03-02 11:09:01 +0100 | [diff] [blame] | 3071 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3072 | goto error; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3073 | } |
Guido van Rossum | 6c95da3 | 2007-07-24 00:16:38 +0000 | [diff] [blame] | 3074 | Py_DECREF(data); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3075 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3076 | fseek(fp, 0, 0); |
| 3077 | win = getwin(fp); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3078 | if (win == NULL) { |
| 3079 | PyErr_SetString(PyCursesError, catchall_NULL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3080 | goto error; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3081 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3082 | res = PyCursesWindow_New(win, NULL); |
| 3083 | |
| 3084 | error: |
Christian Heimes | 2b221b7 | 2017-03-02 11:09:01 +0100 | [diff] [blame] | 3085 | fclose(fp); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3086 | return res; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3089 | /*[clinic input] |
| 3090 | _curses.halfdelay |
| 3091 | |
| 3092 | tenths: byte |
| 3093 | Maximal blocking delay in tenths of seconds (1 - 255). |
| 3094 | / |
| 3095 | |
| 3096 | Enter half-delay mode. |
| 3097 | |
| 3098 | Use nocbreak() to leave half-delay mode. |
| 3099 | [clinic start generated code]*/ |
| 3100 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3101 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3102 | _curses_halfdelay_impl(PyObject *module, unsigned char tenths) |
| 3103 | /*[clinic end generated code: output=e92cdf0ef33c0663 input=e42dce7259c15100]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3104 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3105 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3106 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3107 | return PyCursesCheckERR(halfdelay(tenths), "halfdelay"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3108 | } |
| 3109 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3110 | /*[clinic input] |
| 3111 | _curses.has_colors |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3112 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3113 | Return True if the terminal can display colors; otherwise, return False. |
| 3114 | [clinic start generated code]*/ |
| 3115 | |
| 3116 | static PyObject * |
| 3117 | _curses_has_colors_impl(PyObject *module) |
| 3118 | /*[clinic end generated code: output=db5667483139e3e2 input=b2ec41b739d896c6]*/ |
| 3119 | NoArgTrueFalseFunctionBody(has_colors) |
| 3120 | |
| 3121 | /*[clinic input] |
| 3122 | _curses.has_ic |
| 3123 | |
| 3124 | Return True if the terminal has insert- and delete-character capabilities. |
| 3125 | [clinic start generated code]*/ |
| 3126 | |
| 3127 | static PyObject * |
| 3128 | _curses_has_ic_impl(PyObject *module) |
| 3129 | /*[clinic end generated code: output=6be24da9cb1268fe input=9bc2d3a797cc7324]*/ |
| 3130 | NoArgTrueFalseFunctionBody(has_ic) |
| 3131 | |
| 3132 | /*[clinic input] |
| 3133 | _curses.has_il |
| 3134 | |
| 3135 | Return True if the terminal has insert- and delete-line capabilities. |
| 3136 | [clinic start generated code]*/ |
| 3137 | |
| 3138 | static PyObject * |
| 3139 | _curses_has_il_impl(PyObject *module) |
| 3140 | /*[clinic end generated code: output=d45bd7788ff9f5f4 input=cd939d5607ee5427]*/ |
| 3141 | NoArgTrueFalseFunctionBody(has_il) |
| 3142 | |
| 3143 | #ifdef HAVE_CURSES_HAS_KEY |
| 3144 | /*[clinic input] |
| 3145 | _curses.has_key |
| 3146 | |
| 3147 | key: int |
| 3148 | Key number. |
| 3149 | / |
| 3150 | |
| 3151 | Return True if the current terminal type recognizes a key with that value. |
| 3152 | [clinic start generated code]*/ |
| 3153 | |
| 3154 | static PyObject * |
| 3155 | _curses_has_key_impl(PyObject *module, int key) |
| 3156 | /*[clinic end generated code: output=19ad48319414d0b1 input=78bd44acf1a4997c]*/ |
| 3157 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3158 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3159 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3160 | return PyBool_FromLong(has_key(key)); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3161 | } |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 3162 | #endif |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3163 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3164 | /*[clinic input] |
| 3165 | _curses.init_color |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3166 | |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 3167 | color_number: color |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 3168 | The number of the color to be changed (0 - (COLORS-1)). |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 3169 | r: component |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3170 | Red component (0 - 1000). |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 3171 | g: component |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3172 | Green component (0 - 1000). |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 3173 | b: component |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3174 | Blue component (0 - 1000). |
| 3175 | / |
| 3176 | |
| 3177 | Change the definition of a color. |
| 3178 | |
| 3179 | When init_color() is used, all occurrences of that color on the screen |
| 3180 | immediately change to the new definition. This function is a no-op on |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 3181 | most terminals; it is active only if can_change_color() returns true. |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3182 | [clinic start generated code]*/ |
| 3183 | |
| 3184 | static PyObject * |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 3185 | _curses_init_color_impl(PyObject *module, int color_number, short r, short g, |
| 3186 | short b) |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 3187 | /*[clinic end generated code: output=d7ed71b2d818cdf2 input=ae2b8bea0f152c80]*/ |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3188 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3189 | PyCursesInitialised; |
| 3190 | PyCursesInitialisedColor; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3191 | |
Serhiy Storchaka | 59f9b4e | 2021-01-05 09:13:15 +0200 | [diff] [blame] | 3192 | return PyCursesCheckERR(_CURSES_INIT_COLOR_FUNC(color_number, r, g, b), |
| 3193 | Py_STRINGIFY(_CURSES_INIT_COLOR_FUNC)); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3196 | /*[clinic input] |
| 3197 | _curses.init_pair |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3198 | |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 3199 | pair_number: pair |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3200 | The number of the color-pair to be changed (1 - (COLOR_PAIRS-1)). |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 3201 | fg: color_allow_default |
| 3202 | Foreground color number (-1 - (COLORS-1)). |
| 3203 | bg: color_allow_default |
| 3204 | Background color number (-1 - (COLORS-1)). |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3205 | / |
| 3206 | |
| 3207 | Change the definition of a color-pair. |
| 3208 | |
| 3209 | If the color-pair was previously initialized, the screen is refreshed and |
| 3210 | all occurrences of that color-pair are changed to the new definition. |
| 3211 | [clinic start generated code]*/ |
| 3212 | |
| 3213 | static PyObject * |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 3214 | _curses_init_pair_impl(PyObject *module, int pair_number, int fg, int bg) |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 3215 | /*[clinic end generated code: output=a0bba03d2bbc3ee6 input=54b421b44c12c389]*/ |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3216 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3217 | PyCursesInitialised; |
| 3218 | PyCursesInitialisedColor; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3219 | |
Serhiy Storchaka | 59f9b4e | 2021-01-05 09:13:15 +0200 | [diff] [blame] | 3220 | if (_CURSES_INIT_PAIR_FUNC(pair_number, fg, bg) == ERR) { |
| 3221 | if (pair_number >= COLOR_PAIRS) { |
| 3222 | PyErr_Format(PyExc_ValueError, |
| 3223 | "Color pair is greater than COLOR_PAIRS-1 (%d).", |
| 3224 | COLOR_PAIRS - 1); |
| 3225 | } |
| 3226 | else { |
| 3227 | PyErr_Format(PyCursesError, "%s() returned ERR", |
| 3228 | Py_STRINGIFY(_CURSES_INIT_PAIR_FUNC)); |
| 3229 | } |
| 3230 | return NULL; |
| 3231 | } |
| 3232 | |
| 3233 | Py_RETURN_NONE; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3234 | } |
| 3235 | |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 3236 | static PyObject *ModDict; |
| 3237 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3238 | /*[clinic input] |
| 3239 | _curses.initscr |
| 3240 | |
| 3241 | Initialize the library. |
| 3242 | |
| 3243 | Return a WindowObject which represents the whole screen. |
| 3244 | [clinic start generated code]*/ |
| 3245 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3246 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3247 | _curses_initscr_impl(PyObject *module) |
| 3248 | /*[clinic end generated code: output=619fb68443810b7b input=514f4bce1821f6b5]*/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3249 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3250 | WINDOW *win; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 3251 | PyCursesWindowObject *winobj; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3252 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3253 | if (initialised) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3254 | wrefresh(stdscr); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 3255 | return (PyObject *)PyCursesWindow_New(stdscr, NULL); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3256 | } |
Guido van Rossum | 56bf235 | 1994-08-31 22:06:24 +0000 | [diff] [blame] | 3257 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3258 | win = initscr(); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3259 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3260 | if (win == NULL) { |
| 3261 | PyErr_SetString(PyCursesError, catchall_NULL); |
| 3262 | return NULL; |
| 3263 | } |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 3264 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3265 | initialised = initialised_setupterm = TRUE; |
Guido van Rossum | 56bf235 | 1994-08-31 22:06:24 +0000 | [diff] [blame] | 3266 | |
Andrew M. Kuchling | 03e644b | 2000-05-23 16:24:54 +0000 | [diff] [blame] | 3267 | /* This was moved from initcurses() because it core dumped on SGI, |
| 3268 | where they're not defined until you've called initscr() */ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3269 | #define SetDictInt(string,ch) \ |
| 3270 | do { \ |
| 3271 | PyObject *o = PyLong_FromLong((long) (ch)); \ |
| 3272 | if (o && PyDict_SetItemString(ModDict, string, o) == 0) { \ |
| 3273 | Py_DECREF(o); \ |
| 3274 | } \ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3275 | } while (0) |
Andrew M. Kuchling | 03e644b | 2000-05-23 16:24:54 +0000 | [diff] [blame] | 3276 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3277 | /* Here are some graphic symbols you can use */ |
| 3278 | SetDictInt("ACS_ULCORNER", (ACS_ULCORNER)); |
| 3279 | SetDictInt("ACS_LLCORNER", (ACS_LLCORNER)); |
| 3280 | SetDictInt("ACS_URCORNER", (ACS_URCORNER)); |
| 3281 | SetDictInt("ACS_LRCORNER", (ACS_LRCORNER)); |
| 3282 | SetDictInt("ACS_LTEE", (ACS_LTEE)); |
| 3283 | SetDictInt("ACS_RTEE", (ACS_RTEE)); |
| 3284 | SetDictInt("ACS_BTEE", (ACS_BTEE)); |
| 3285 | SetDictInt("ACS_TTEE", (ACS_TTEE)); |
| 3286 | SetDictInt("ACS_HLINE", (ACS_HLINE)); |
| 3287 | SetDictInt("ACS_VLINE", (ACS_VLINE)); |
| 3288 | SetDictInt("ACS_PLUS", (ACS_PLUS)); |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 3289 | #if !defined(__hpux) || defined(HAVE_NCURSES_H) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3290 | /* On HP/UX 11, these are of type cchar_t, which is not an |
| 3291 | integral type. If this is a problem on more platforms, a |
| 3292 | configure test should be added to determine whether ACS_S1 |
| 3293 | is of integral type. */ |
| 3294 | SetDictInt("ACS_S1", (ACS_S1)); |
| 3295 | SetDictInt("ACS_S9", (ACS_S9)); |
| 3296 | SetDictInt("ACS_DIAMOND", (ACS_DIAMOND)); |
| 3297 | SetDictInt("ACS_CKBOARD", (ACS_CKBOARD)); |
| 3298 | SetDictInt("ACS_DEGREE", (ACS_DEGREE)); |
| 3299 | SetDictInt("ACS_PLMINUS", (ACS_PLMINUS)); |
| 3300 | SetDictInt("ACS_BULLET", (ACS_BULLET)); |
| 3301 | SetDictInt("ACS_LARROW", (ACS_LARROW)); |
| 3302 | SetDictInt("ACS_RARROW", (ACS_RARROW)); |
| 3303 | SetDictInt("ACS_DARROW", (ACS_DARROW)); |
| 3304 | SetDictInt("ACS_UARROW", (ACS_UARROW)); |
| 3305 | SetDictInt("ACS_BOARD", (ACS_BOARD)); |
| 3306 | SetDictInt("ACS_LANTERN", (ACS_LANTERN)); |
| 3307 | SetDictInt("ACS_BLOCK", (ACS_BLOCK)); |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 3308 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3309 | SetDictInt("ACS_BSSB", (ACS_ULCORNER)); |
| 3310 | SetDictInt("ACS_SSBB", (ACS_LLCORNER)); |
| 3311 | SetDictInt("ACS_BBSS", (ACS_URCORNER)); |
| 3312 | SetDictInt("ACS_SBBS", (ACS_LRCORNER)); |
| 3313 | SetDictInt("ACS_SBSS", (ACS_RTEE)); |
| 3314 | SetDictInt("ACS_SSSB", (ACS_LTEE)); |
| 3315 | SetDictInt("ACS_SSBS", (ACS_BTEE)); |
| 3316 | SetDictInt("ACS_BSSS", (ACS_TTEE)); |
| 3317 | SetDictInt("ACS_BSBS", (ACS_HLINE)); |
| 3318 | SetDictInt("ACS_SBSB", (ACS_VLINE)); |
| 3319 | SetDictInt("ACS_SSSS", (ACS_PLUS)); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 3320 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3321 | /* The following are never available with strict SYSV curses */ |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 3322 | #ifdef ACS_S3 |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3323 | SetDictInt("ACS_S3", (ACS_S3)); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 3324 | #endif |
| 3325 | #ifdef ACS_S7 |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3326 | SetDictInt("ACS_S7", (ACS_S7)); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 3327 | #endif |
| 3328 | #ifdef ACS_LEQUAL |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3329 | SetDictInt("ACS_LEQUAL", (ACS_LEQUAL)); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 3330 | #endif |
| 3331 | #ifdef ACS_GEQUAL |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3332 | SetDictInt("ACS_GEQUAL", (ACS_GEQUAL)); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 3333 | #endif |
| 3334 | #ifdef ACS_PI |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3335 | SetDictInt("ACS_PI", (ACS_PI)); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 3336 | #endif |
| 3337 | #ifdef ACS_NEQUAL |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3338 | SetDictInt("ACS_NEQUAL", (ACS_NEQUAL)); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 3339 | #endif |
| 3340 | #ifdef ACS_STERLING |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3341 | SetDictInt("ACS_STERLING", (ACS_STERLING)); |
Andrew M. Kuchling | 03e644b | 2000-05-23 16:24:54 +0000 | [diff] [blame] | 3342 | #endif |
| 3343 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3344 | SetDictInt("LINES", LINES); |
| 3345 | SetDictInt("COLS", COLS); |
Guido van Rossum | 56bf235 | 1994-08-31 22:06:24 +0000 | [diff] [blame] | 3346 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 3347 | winobj = (PyCursesWindowObject *)PyCursesWindow_New(win, NULL); |
| 3348 | screen_encoding = winobj->encoding; |
| 3349 | return (PyObject *)winobj; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3352 | /*[clinic input] |
| 3353 | _curses.setupterm |
| 3354 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3355 | term: str(accept={str, NoneType}) = None |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3356 | Terminal name. |
| 3357 | If omitted, the value of the TERM environment variable will be used. |
| 3358 | fd: int = -1 |
| 3359 | File descriptor to which any initialization sequences will be sent. |
| 3360 | If not supplied, the file descriptor for sys.stdout will be used. |
| 3361 | |
| 3362 | Initialize the terminal. |
| 3363 | [clinic start generated code]*/ |
| 3364 | |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 3365 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3366 | _curses_setupterm_impl(PyObject *module, const char *term, int fd) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3367 | /*[clinic end generated code: output=4584e587350f2848 input=4511472766af0c12]*/ |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 3368 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3369 | int err; |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 3370 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3371 | if (fd == -1) { |
| 3372 | PyObject* sys_stdout; |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 3373 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 3374 | sys_stdout = PySys_GetObject("stdout"); |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 3375 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3376 | if (sys_stdout == NULL || sys_stdout == Py_None) { |
| 3377 | PyErr_SetString( |
| 3378 | PyCursesError, |
| 3379 | "lost sys.stdout"); |
| 3380 | return NULL; |
| 3381 | } |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 3382 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3383 | fd = PyObject_AsFileDescriptor(sys_stdout); |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 3384 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3385 | if (fd == -1) { |
| 3386 | return NULL; |
| 3387 | } |
| 3388 | } |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 3389 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3390 | if (!initialised_setupterm && setupterm((char *)term, fd, &err) == ERR) { |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 3391 | const char* s = "setupterm: unknown error"; |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 3392 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3393 | if (err == 0) { |
| 3394 | s = "setupterm: could not find terminal"; |
| 3395 | } else if (err == -1) { |
| 3396 | s = "setupterm: could not find terminfo database"; |
| 3397 | } |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 3398 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3399 | PyErr_SetString(PyCursesError,s); |
| 3400 | return NULL; |
| 3401 | } |
| 3402 | |
| 3403 | initialised_setupterm = TRUE; |
| 3404 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 3405 | Py_RETURN_NONE; |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 3406 | } |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3407 | |
Batuhan Taşkaya | 4991cf4 | 2020-03-03 05:00:10 +0300 | [diff] [blame] | 3408 | #if defined(NCURSES_EXT_FUNCS) && NCURSES_EXT_FUNCS >= 20081102 |
| 3409 | // https://invisible-island.net/ncurses/NEWS.html#index-t20080119 |
| 3410 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3411 | /*[clinic input] |
Anthony Sottile | b32cb97 | 2019-10-31 02:13:48 -0700 | [diff] [blame] | 3412 | _curses.get_escdelay |
| 3413 | |
| 3414 | Gets the curses ESCDELAY setting. |
| 3415 | |
| 3416 | Gets the number of milliseconds to wait after reading an escape character, |
| 3417 | to distinguish between an individual escape character entered on the |
| 3418 | keyboard from escape sequences sent by cursor and function keys. |
| 3419 | [clinic start generated code]*/ |
| 3420 | |
| 3421 | static PyObject * |
| 3422 | _curses_get_escdelay_impl(PyObject *module) |
| 3423 | /*[clinic end generated code: output=222fa1a822555d60 input=be2d5b3dd974d0a4]*/ |
| 3424 | { |
| 3425 | return PyLong_FromLong(ESCDELAY); |
| 3426 | } |
| 3427 | /*[clinic input] |
| 3428 | _curses.set_escdelay |
| 3429 | ms: int |
| 3430 | length of the delay in milliseconds. |
| 3431 | / |
| 3432 | |
| 3433 | Sets the curses ESCDELAY setting. |
| 3434 | |
| 3435 | Sets the number of milliseconds to wait after reading an escape character, |
| 3436 | to distinguish between an individual escape character entered on the |
| 3437 | keyboard from escape sequences sent by cursor and function keys. |
| 3438 | [clinic start generated code]*/ |
| 3439 | |
| 3440 | static PyObject * |
| 3441 | _curses_set_escdelay_impl(PyObject *module, int ms) |
| 3442 | /*[clinic end generated code: output=43818efbf7980ac4 input=7796fe19f111e250]*/ |
| 3443 | { |
| 3444 | if (ms <= 0) { |
| 3445 | PyErr_SetString(PyExc_ValueError, "ms must be > 0"); |
| 3446 | return NULL; |
| 3447 | } |
| 3448 | |
| 3449 | return PyCursesCheckERR(set_escdelay(ms), "set_escdelay"); |
| 3450 | } |
| 3451 | |
| 3452 | /*[clinic input] |
| 3453 | _curses.get_tabsize |
| 3454 | |
| 3455 | Gets the curses TABSIZE setting. |
| 3456 | |
| 3457 | Gets the number of columns used by the curses library when converting a tab |
| 3458 | character to spaces as it adds the tab to a window. |
| 3459 | [clinic start generated code]*/ |
| 3460 | |
| 3461 | static PyObject * |
| 3462 | _curses_get_tabsize_impl(PyObject *module) |
| 3463 | /*[clinic end generated code: output=7e9e51fb6126fbdf input=74af86bf6c9f5d7e]*/ |
| 3464 | { |
| 3465 | return PyLong_FromLong(TABSIZE); |
| 3466 | } |
| 3467 | /*[clinic input] |
| 3468 | _curses.set_tabsize |
| 3469 | size: int |
| 3470 | rendered cell width of a tab character. |
| 3471 | / |
| 3472 | |
| 3473 | Sets the curses TABSIZE setting. |
| 3474 | |
| 3475 | Sets the number of columns used by the curses library when converting a tab |
| 3476 | character to spaces as it adds the tab to a window. |
| 3477 | [clinic start generated code]*/ |
| 3478 | |
| 3479 | static PyObject * |
| 3480 | _curses_set_tabsize_impl(PyObject *module, int size) |
| 3481 | /*[clinic end generated code: output=c1de5a76c0daab1e input=78cba6a3021ad061]*/ |
| 3482 | { |
| 3483 | if (size <= 0) { |
| 3484 | PyErr_SetString(PyExc_ValueError, "size must be > 0"); |
| 3485 | return NULL; |
| 3486 | } |
| 3487 | |
| 3488 | return PyCursesCheckERR(set_tabsize(size), "set_tabsize"); |
| 3489 | } |
Batuhan Taşkaya | 4991cf4 | 2020-03-03 05:00:10 +0300 | [diff] [blame] | 3490 | #endif |
Anthony Sottile | b32cb97 | 2019-10-31 02:13:48 -0700 | [diff] [blame] | 3491 | |
| 3492 | /*[clinic input] |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3493 | _curses.intrflush |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3494 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3495 | flag: bool(accept={int}) |
| 3496 | / |
| 3497 | |
| 3498 | [clinic start generated code]*/ |
| 3499 | |
| 3500 | static PyObject * |
| 3501 | _curses_intrflush_impl(PyObject *module, int flag) |
| 3502 | /*[clinic end generated code: output=c1986df35e999a0f input=fcba57bb28dfd795]*/ |
| 3503 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3504 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3505 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3506 | return PyCursesCheckERR(intrflush(NULL, flag), "intrflush"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3509 | /*[clinic input] |
| 3510 | _curses.isendwin |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3511 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3512 | Return True if endwin() has been called. |
| 3513 | [clinic start generated code]*/ |
| 3514 | |
| 3515 | static PyObject * |
| 3516 | _curses_isendwin_impl(PyObject *module) |
| 3517 | /*[clinic end generated code: output=d73179e4a7e1eb8c input=6cdb01a7ebf71397]*/ |
| 3518 | NoArgTrueFalseFunctionBody(isendwin) |
| 3519 | |
| 3520 | #ifdef HAVE_CURSES_IS_TERM_RESIZED |
| 3521 | /*[clinic input] |
| 3522 | _curses.is_term_resized |
| 3523 | |
| 3524 | nlines: int |
| 3525 | Height. |
| 3526 | ncols: int |
| 3527 | Width. |
| 3528 | / |
| 3529 | |
| 3530 | Return True if resize_term() would modify the window structure, False otherwise. |
| 3531 | [clinic start generated code]*/ |
| 3532 | |
| 3533 | static PyObject * |
| 3534 | _curses_is_term_resized_impl(PyObject *module, int nlines, int ncols) |
| 3535 | /*[clinic end generated code: output=aafe04afe50f1288 input=ca9c0bd0fb8ab444]*/ |
| 3536 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3537 | PyCursesInitialised; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3538 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3539 | return PyBool_FromLong(is_term_resized(nlines, ncols)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3540 | } |
| 3541 | #endif /* HAVE_CURSES_IS_TERM_RESIZED */ |
| 3542 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3543 | /*[clinic input] |
| 3544 | _curses.keyname |
| 3545 | |
| 3546 | key: int |
| 3547 | Key number. |
| 3548 | / |
| 3549 | |
| 3550 | Return the name of specified key. |
| 3551 | [clinic start generated code]*/ |
| 3552 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3553 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3554 | _curses_keyname_impl(PyObject *module, int key) |
| 3555 | /*[clinic end generated code: output=fa2675ab3f4e056b input=ee4b1d0f243a2a2b]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3556 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3557 | const char *knp; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3558 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3559 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3560 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3561 | if (key < 0) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3562 | PyErr_SetString(PyExc_ValueError, "invalid key number"); |
| 3563 | return NULL; |
| 3564 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3565 | knp = keyname(key); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3566 | |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 3567 | return PyBytes_FromString((knp == NULL) ? "" : knp); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3570 | /*[clinic input] |
| 3571 | _curses.killchar |
| 3572 | |
| 3573 | Return the user's current line kill character. |
| 3574 | [clinic start generated code]*/ |
| 3575 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3576 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3577 | _curses_killchar_impl(PyObject *module) |
| 3578 | /*[clinic end generated code: output=31c3a45b2c528269 input=1ff171c38df5ccad]*/ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3579 | { |
| 3580 | char ch; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3581 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3582 | ch = killchar(); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3583 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3584 | return PyBytes_FromStringAndSize(&ch, 1); |
| 3585 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3586 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3587 | /*[clinic input] |
| 3588 | _curses.longname |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3589 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3590 | Return the terminfo long name field describing the current terminal. |
| 3591 | |
| 3592 | The maximum length of a verbose description is 128 characters. It is defined |
| 3593 | only after the call to initscr(). |
| 3594 | [clinic start generated code]*/ |
| 3595 | |
| 3596 | static PyObject * |
| 3597 | _curses_longname_impl(PyObject *module) |
| 3598 | /*[clinic end generated code: output=fdf30433727ef568 input=84c3f20201b1098e]*/ |
| 3599 | NoArgReturnStringFunctionBody(longname) |
| 3600 | |
| 3601 | /*[clinic input] |
| 3602 | _curses.meta |
| 3603 | |
| 3604 | yes: bool(accept={int}) |
| 3605 | / |
| 3606 | |
| 3607 | Enable/disable meta keys. |
| 3608 | |
| 3609 | If yes is True, allow 8-bit characters to be input. If yes is False, |
| 3610 | allow only 7-bit characters. |
| 3611 | [clinic start generated code]*/ |
| 3612 | |
| 3613 | static PyObject * |
| 3614 | _curses_meta_impl(PyObject *module, int yes) |
| 3615 | /*[clinic end generated code: output=22f5abda46a605d8 input=af9892e3a74f35db]*/ |
| 3616 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3617 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3618 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3619 | return PyCursesCheckERR(meta(stdscr, yes), "meta"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3620 | } |
| 3621 | |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3622 | #ifdef NCURSES_MOUSE_VERSION |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3623 | /*[clinic input] |
| 3624 | _curses.mouseinterval |
| 3625 | |
| 3626 | interval: int |
| 3627 | Time in milliseconds. |
| 3628 | / |
| 3629 | |
| 3630 | Set and retrieve the maximum time between press and release in a click. |
| 3631 | |
| 3632 | Set the maximum time that can elapse between press and release events in |
| 3633 | order for them to be recognized as a click, and return the previous interval |
| 3634 | value. |
| 3635 | [clinic start generated code]*/ |
| 3636 | |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3637 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3638 | _curses_mouseinterval_impl(PyObject *module, int interval) |
| 3639 | /*[clinic end generated code: output=c4f5ff04354634c5 input=75aaa3f0db10ac4e]*/ |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3640 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3641 | PyCursesInitialised; |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3642 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3643 | return PyCursesCheckERR(mouseinterval(interval), "mouseinterval"); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3646 | /*[clinic input] |
| 3647 | _curses.mousemask |
| 3648 | |
| 3649 | newmask: unsigned_long(bitwise=True) |
| 3650 | / |
| 3651 | |
| 3652 | Set the mouse events to be reported, and return a tuple (availmask, oldmask). |
| 3653 | |
| 3654 | Return a tuple (availmask, oldmask). availmask indicates which of the |
| 3655 | specified mouse events can be reported; on complete failure it returns 0. |
| 3656 | oldmask is the previous value of the given window's mouse event mask. |
| 3657 | If this function is never called, no mouse events are ever reported. |
| 3658 | [clinic start generated code]*/ |
| 3659 | |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3660 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3661 | _curses_mousemask_impl(PyObject *module, unsigned long newmask) |
| 3662 | /*[clinic end generated code: output=9406cf1b8a36e485 input=bdf76b7568a3c541]*/ |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3663 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3664 | mmask_t oldmask, availmask; |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3665 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3666 | PyCursesInitialised; |
Serhiy Storchaka | 4f469c0 | 2017-11-01 20:48:49 +0200 | [diff] [blame] | 3667 | availmask = mousemask((mmask_t)newmask, &oldmask); |
| 3668 | return Py_BuildValue("(kk)", |
| 3669 | (unsigned long)availmask, (unsigned long)oldmask); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3670 | } |
| 3671 | #endif |
| 3672 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3673 | /*[clinic input] |
| 3674 | _curses.napms |
Andrew M. Kuchling | 2824d7f | 2000-12-15 00:44:02 +0000 | [diff] [blame] | 3675 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3676 | ms: int |
| 3677 | Duration in milliseconds. |
| 3678 | / |
| 3679 | |
| 3680 | Sleep for specified time. |
| 3681 | [clinic start generated code]*/ |
| 3682 | |
| 3683 | static PyObject * |
| 3684 | _curses_napms_impl(PyObject *module, int ms) |
| 3685 | /*[clinic end generated code: output=a40a1da2e39ea438 input=20cd3af2b6900f56]*/ |
| 3686 | { |
Andrew M. Kuchling | 2b14676 | 2010-05-10 23:30:25 +0000 | [diff] [blame] | 3687 | PyCursesInitialised; |
Andrew M. Kuchling | 2824d7f | 2000-12-15 00:44:02 +0000 | [diff] [blame] | 3688 | |
| 3689 | return Py_BuildValue("i", napms(ms)); |
| 3690 | } |
| 3691 | |
| 3692 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3693 | /*[clinic input] |
| 3694 | _curses.newpad |
| 3695 | |
| 3696 | nlines: int |
| 3697 | Height. |
| 3698 | ncols: int |
| 3699 | Width. |
| 3700 | / |
| 3701 | |
| 3702 | Create and return a pointer to a new pad data structure. |
| 3703 | [clinic start generated code]*/ |
| 3704 | |
Andrew M. Kuchling | 2824d7f | 2000-12-15 00:44:02 +0000 | [diff] [blame] | 3705 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3706 | _curses_newpad_impl(PyObject *module, int nlines, int ncols) |
| 3707 | /*[clinic end generated code: output=de52a56eb1098ec9 input=93f1272f240d8894]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3708 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3709 | WINDOW *win; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3710 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3711 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3712 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3713 | win = newpad(nlines, ncols); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3714 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3715 | if (win == NULL) { |
| 3716 | PyErr_SetString(PyCursesError, catchall_NULL); |
| 3717 | return NULL; |
| 3718 | } |
| 3719 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 3720 | return (PyObject *)PyCursesWindow_New(win, NULL); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3723 | /*[clinic input] |
| 3724 | _curses.newwin |
| 3725 | |
| 3726 | nlines: int |
| 3727 | Height. |
| 3728 | ncols: int |
| 3729 | Width. |
| 3730 | [ |
| 3731 | begin_y: int = 0 |
| 3732 | Top side y-coordinate. |
| 3733 | begin_x: int = 0 |
| 3734 | Left side x-coordinate. |
| 3735 | ] |
| 3736 | / |
| 3737 | |
| 3738 | Return a new window. |
| 3739 | |
| 3740 | By default, the window will extend from the specified position to the lower |
| 3741 | right corner of the screen. |
| 3742 | [clinic start generated code]*/ |
| 3743 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3744 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3745 | _curses_newwin_impl(PyObject *module, int nlines, int ncols, |
| 3746 | int group_right_1, int begin_y, int begin_x) |
| 3747 | /*[clinic end generated code: output=c1e0a8dc8ac2826c input=29312c15a72a003d]*/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3748 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3749 | WINDOW *win; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 3750 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3751 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3752 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3753 | win = newwin(nlines,ncols,begin_y,begin_x); |
| 3754 | if (win == NULL) { |
| 3755 | PyErr_SetString(PyCursesError, catchall_NULL); |
| 3756 | return NULL; |
| 3757 | } |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 3758 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 3759 | return (PyObject *)PyCursesWindow_New(win, NULL); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3760 | } |
| 3761 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3762 | /*[clinic input] |
| 3763 | _curses.nl |
| 3764 | |
| 3765 | flag: bool(accept={int}) = True |
| 3766 | If false, the effect is the same as calling nonl(). |
| 3767 | / |
| 3768 | |
| 3769 | Enter newline mode. |
| 3770 | |
| 3771 | This mode translates the return key into newline on input, and translates |
| 3772 | newline into return and line-feed on output. Newline mode is initially on. |
| 3773 | [clinic start generated code]*/ |
| 3774 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3775 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3776 | _curses_nl_impl(PyObject *module, int flag) |
| 3777 | /*[clinic end generated code: output=b39cc0ffc9015003 input=cf36a63f7b86e28a]*/ |
| 3778 | NoArgOrFlagNoReturnFunctionBody(nl, flag) |
| 3779 | |
| 3780 | /*[clinic input] |
| 3781 | _curses.nocbreak |
| 3782 | |
| 3783 | Leave cbreak mode. |
| 3784 | |
| 3785 | Return to normal "cooked" mode with line buffering. |
| 3786 | [clinic start generated code]*/ |
| 3787 | |
| 3788 | static PyObject * |
| 3789 | _curses_nocbreak_impl(PyObject *module) |
| 3790 | /*[clinic end generated code: output=eabf3833a4fbf620 input=e4b65f7d734af400]*/ |
| 3791 | NoArgNoReturnFunctionBody(nocbreak) |
| 3792 | |
| 3793 | /*[clinic input] |
| 3794 | _curses.noecho |
| 3795 | |
| 3796 | Leave echo mode. |
| 3797 | |
| 3798 | Echoing of input characters is turned off. |
| 3799 | [clinic start generated code]*/ |
| 3800 | |
| 3801 | static PyObject * |
| 3802 | _curses_noecho_impl(PyObject *module) |
| 3803 | /*[clinic end generated code: output=cc95ab45bc98f41b input=76714df529e614c3]*/ |
| 3804 | NoArgNoReturnFunctionBody(noecho) |
| 3805 | |
| 3806 | /*[clinic input] |
| 3807 | _curses.nonl |
| 3808 | |
| 3809 | Leave newline mode. |
| 3810 | |
| 3811 | Disable translation of return into newline on input, and disable low-level |
| 3812 | translation of newline into newline/return on output. |
| 3813 | [clinic start generated code]*/ |
| 3814 | |
| 3815 | static PyObject * |
| 3816 | _curses_nonl_impl(PyObject *module) |
| 3817 | /*[clinic end generated code: output=99e917e9715770c6 input=9d37dd122d3022fc]*/ |
| 3818 | NoArgNoReturnFunctionBody(nonl) |
| 3819 | |
| 3820 | /*[clinic input] |
| 3821 | _curses.noqiflush |
| 3822 | |
| 3823 | Disable queue flushing. |
| 3824 | |
| 3825 | When queue flushing is disabled, normal flush of input and output queues |
| 3826 | associated with the INTR, QUIT and SUSP characters will not be done. |
| 3827 | [clinic start generated code]*/ |
| 3828 | |
| 3829 | static PyObject * |
| 3830 | _curses_noqiflush_impl(PyObject *module) |
| 3831 | /*[clinic end generated code: output=8b95a4229bbf0877 input=ba3e6b2e3e54c4df]*/ |
| 3832 | NoArgNoReturnVoidFunctionBody(noqiflush) |
| 3833 | |
| 3834 | /*[clinic input] |
| 3835 | _curses.noraw |
| 3836 | |
| 3837 | Leave raw mode. |
| 3838 | |
| 3839 | Return to normal "cooked" mode with line buffering. |
| 3840 | [clinic start generated code]*/ |
| 3841 | |
| 3842 | static PyObject * |
| 3843 | _curses_noraw_impl(PyObject *module) |
| 3844 | /*[clinic end generated code: output=39894e5524c430cc input=6ec86692096dffb5]*/ |
| 3845 | NoArgNoReturnFunctionBody(noraw) |
| 3846 | |
| 3847 | /*[clinic input] |
| 3848 | _curses.pair_content |
| 3849 | |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 3850 | pair_number: pair |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 3851 | The number of the color pair (0 - (COLOR_PAIRS-1)). |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3852 | / |
| 3853 | |
| 3854 | Return a tuple (fg, bg) containing the colors for the requested color pair. |
| 3855 | [clinic start generated code]*/ |
| 3856 | |
| 3857 | static PyObject * |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 3858 | _curses_pair_content_impl(PyObject *module, int pair_number) |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 3859 | /*[clinic end generated code: output=4a726dd0e6885f3f input=03970f840fc7b739]*/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3860 | { |
Serhiy Storchaka | 59f9b4e | 2021-01-05 09:13:15 +0200 | [diff] [blame] | 3861 | _CURSES_COLOR_NUM_TYPE f, b; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3862 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3863 | PyCursesInitialised; |
| 3864 | PyCursesInitialisedColor; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3865 | |
Serhiy Storchaka | 59f9b4e | 2021-01-05 09:13:15 +0200 | [diff] [blame] | 3866 | if (_CURSES_PAIR_CONTENT_FUNC(pair_number, &f, &b) == ERR) { |
| 3867 | if (pair_number >= COLOR_PAIRS) { |
| 3868 | PyErr_Format(PyExc_ValueError, |
| 3869 | "Color pair is greater than COLOR_PAIRS-1 (%d).", |
| 3870 | COLOR_PAIRS - 1); |
| 3871 | } |
| 3872 | else { |
| 3873 | PyErr_Format(PyCursesError, "%s() returned ERR", |
| 3874 | Py_STRINGIFY(_CURSES_PAIR_CONTENT_FUNC)); |
| 3875 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3876 | return NULL; |
| 3877 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3878 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3879 | return Py_BuildValue("(ii)", f, b); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3880 | } |
| 3881 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3882 | /*[clinic input] |
| 3883 | _curses.pair_number |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3884 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3885 | attr: int |
| 3886 | / |
| 3887 | |
| 3888 | Return the number of the color-pair set by the specified attribute value. |
| 3889 | |
| 3890 | color_pair() is the counterpart to this function. |
| 3891 | [clinic start generated code]*/ |
| 3892 | |
| 3893 | static PyObject * |
| 3894 | _curses_pair_number_impl(PyObject *module, int attr) |
| 3895 | /*[clinic end generated code: output=85bce7d65c0aa3f4 input=d478548e33f5e61a]*/ |
| 3896 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3897 | PyCursesInitialised; |
| 3898 | PyCursesInitialisedColor; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3899 | |
Serhiy Storchaka | 1470edd | 2021-01-03 22:51:11 +0200 | [diff] [blame] | 3900 | return PyLong_FromLong(PAIR_NUMBER(attr)); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3901 | } |
| 3902 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3903 | /*[clinic input] |
| 3904 | _curses.putp |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3905 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3906 | string: str(accept={robuffer}) |
| 3907 | / |
| 3908 | |
| 3909 | Emit the value of a specified terminfo capability for the current terminal. |
| 3910 | |
| 3911 | Note that the output of putp() always goes to standard output. |
| 3912 | [clinic start generated code]*/ |
| 3913 | |
| 3914 | static PyObject * |
| 3915 | _curses_putp_impl(PyObject *module, const char *string) |
| 3916 | /*[clinic end generated code: output=e98081d1b8eb5816 input=1601faa828b44cb3]*/ |
| 3917 | { |
| 3918 | return PyCursesCheckERR(putp(string), "putp"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3919 | } |
| 3920 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3921 | /*[clinic input] |
| 3922 | _curses.qiflush |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3923 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3924 | flag: bool(accept={int}) = True |
| 3925 | If false, the effect is the same as calling noqiflush(). |
| 3926 | / |
| 3927 | |
| 3928 | Enable queue flushing. |
| 3929 | |
| 3930 | If queue flushing is enabled, all output in the display driver queue |
| 3931 | will be flushed when the INTR, QUIT and SUSP characters are read. |
| 3932 | [clinic start generated code]*/ |
| 3933 | |
| 3934 | static PyObject * |
| 3935 | _curses_qiflush_impl(PyObject *module, int flag) |
| 3936 | /*[clinic end generated code: output=9167e862f760ea30 input=e9e4a389946a0dbc]*/ |
| 3937 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3938 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3939 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3940 | if (flag) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3941 | qiflush(); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3942 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 3943 | else { |
| 3944 | noqiflush(); |
| 3945 | } |
| 3946 | Py_RETURN_NONE; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3947 | } |
| 3948 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3949 | /* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES |
| 3950 | * and _curses.COLS */ |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 3951 | #if defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3952 | static int |
| 3953 | update_lines_cols(void) |
| 3954 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3955 | PyObject *o; |
| 3956 | PyObject *m = PyImport_ImportModuleNoBlock("curses"); |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 3957 | _Py_IDENTIFIER(LINES); |
| 3958 | _Py_IDENTIFIER(COLS); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3959 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3960 | if (!m) |
| 3961 | return 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3962 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3963 | o = PyLong_FromLong(LINES); |
| 3964 | if (!o) { |
| 3965 | Py_DECREF(m); |
| 3966 | return 0; |
| 3967 | } |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 3968 | if (_PyObject_SetAttrId(m, &PyId_LINES, o)) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3969 | Py_DECREF(m); |
| 3970 | Py_DECREF(o); |
| 3971 | return 0; |
| 3972 | } |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 3973 | /* PyId_LINES.object will be initialized here. */ |
Victor Stinner | 4804b5b | 2020-05-12 01:43:38 +0200 | [diff] [blame] | 3974 | if (PyDict_SetItem(ModDict, _PyUnicode_FromId(&PyId_LINES), o)) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3975 | Py_DECREF(m); |
| 3976 | Py_DECREF(o); |
| 3977 | return 0; |
| 3978 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3979 | Py_DECREF(o); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3980 | o = PyLong_FromLong(COLS); |
| 3981 | if (!o) { |
| 3982 | Py_DECREF(m); |
| 3983 | return 0; |
| 3984 | } |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 3985 | if (_PyObject_SetAttrId(m, &PyId_COLS, o)) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3986 | Py_DECREF(m); |
| 3987 | Py_DECREF(o); |
| 3988 | return 0; |
| 3989 | } |
Victor Stinner | 4804b5b | 2020-05-12 01:43:38 +0200 | [diff] [blame] | 3990 | if (PyDict_SetItem(ModDict, _PyUnicode_FromId(&PyId_COLS), o)) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3991 | Py_DECREF(m); |
| 3992 | Py_DECREF(o); |
| 3993 | return 0; |
| 3994 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3995 | Py_DECREF(o); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3996 | Py_DECREF(m); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3997 | return 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3998 | } |
Steve Dower | d2bc389 | 2015-04-15 18:06:05 -0400 | [diff] [blame] | 3999 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4000 | /*[clinic input] |
Zackery Spytz | 2bc3434 | 2019-11-17 10:10:13 -0700 | [diff] [blame] | 4001 | _curses.update_lines_cols |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4002 | |
| 4003 | [clinic start generated code]*/ |
| 4004 | |
Zackery Spytz | 2bc3434 | 2019-11-17 10:10:13 -0700 | [diff] [blame] | 4005 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4006 | _curses_update_lines_cols_impl(PyObject *module) |
Zackery Spytz | 2bc3434 | 2019-11-17 10:10:13 -0700 | [diff] [blame] | 4007 | /*[clinic end generated code: output=423f2b1e63ed0f75 input=5f065ab7a28a5d90]*/ |
Steve Dower | d2bc389 | 2015-04-15 18:06:05 -0400 | [diff] [blame] | 4008 | { |
Zackery Spytz | 2bc3434 | 2019-11-17 10:10:13 -0700 | [diff] [blame] | 4009 | if (!update_lines_cols()) { |
| 4010 | return NULL; |
| 4011 | } |
| 4012 | Py_RETURN_NONE; |
Steve Dower | d2bc389 | 2015-04-15 18:06:05 -0400 | [diff] [blame] | 4013 | } |
| 4014 | |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 4015 | #endif |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4016 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4017 | /*[clinic input] |
| 4018 | _curses.raw |
| 4019 | |
| 4020 | flag: bool(accept={int}) = True |
| 4021 | If false, the effect is the same as calling noraw(). |
| 4022 | / |
| 4023 | |
| 4024 | Enter raw mode. |
| 4025 | |
| 4026 | In raw mode, normal line buffering and processing of interrupt, quit, |
| 4027 | suspend, and flow control keys are turned off; characters are presented to |
| 4028 | curses input functions one by one. |
| 4029 | [clinic start generated code]*/ |
| 4030 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4031 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4032 | _curses_raw_impl(PyObject *module, int flag) |
| 4033 | /*[clinic end generated code: output=a750e4b342be015b input=e36d8db27832b848]*/ |
| 4034 | NoArgOrFlagNoReturnFunctionBody(raw, flag) |
| 4035 | |
| 4036 | /*[clinic input] |
| 4037 | _curses.reset_prog_mode |
| 4038 | |
| 4039 | Restore the terminal to "program" mode, as previously saved by def_prog_mode(). |
| 4040 | [clinic start generated code]*/ |
| 4041 | |
| 4042 | static PyObject * |
| 4043 | _curses_reset_prog_mode_impl(PyObject *module) |
| 4044 | /*[clinic end generated code: output=15eb765abf0b6575 input=3d82bea2b3243471]*/ |
| 4045 | NoArgNoReturnFunctionBody(reset_prog_mode) |
| 4046 | |
| 4047 | /*[clinic input] |
| 4048 | _curses.reset_shell_mode |
| 4049 | |
| 4050 | Restore the terminal to "shell" mode, as previously saved by def_shell_mode(). |
| 4051 | [clinic start generated code]*/ |
| 4052 | |
| 4053 | static PyObject * |
| 4054 | _curses_reset_shell_mode_impl(PyObject *module) |
| 4055 | /*[clinic end generated code: output=0238de2962090d33 input=1c738fa64bd1a24f]*/ |
| 4056 | NoArgNoReturnFunctionBody(reset_shell_mode) |
| 4057 | |
| 4058 | /*[clinic input] |
| 4059 | _curses.resetty |
| 4060 | |
| 4061 | Restore terminal mode. |
| 4062 | [clinic start generated code]*/ |
| 4063 | |
| 4064 | static PyObject * |
| 4065 | _curses_resetty_impl(PyObject *module) |
| 4066 | /*[clinic end generated code: output=ff4b448e80a7cd63 input=940493de03624bb0]*/ |
| 4067 | NoArgNoReturnFunctionBody(resetty) |
| 4068 | |
| 4069 | #ifdef HAVE_CURSES_RESIZETERM |
| 4070 | /*[clinic input] |
| 4071 | _curses.resizeterm |
| 4072 | |
| 4073 | nlines: int |
| 4074 | Height. |
| 4075 | ncols: int |
| 4076 | Width. |
| 4077 | / |
| 4078 | |
| 4079 | Resize the standard and current windows to the specified dimensions. |
| 4080 | |
| 4081 | Adjusts other bookkeeping data used by the curses library that record the |
| 4082 | window dimensions (in particular the SIGWINCH handler). |
| 4083 | [clinic start generated code]*/ |
| 4084 | |
| 4085 | static PyObject * |
| 4086 | _curses_resizeterm_impl(PyObject *module, int nlines, int ncols) |
| 4087 | /*[clinic end generated code: output=56d6bcc5194ad055 input=0fca02ebad5ffa82]*/ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4088 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4089 | PyObject *result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4090 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4091 | PyCursesInitialised; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4092 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4093 | result = PyCursesCheckERR(resizeterm(nlines, ncols), "resizeterm"); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4094 | if (!result) |
| 4095 | return NULL; |
Zackery Spytz | 2bc3434 | 2019-11-17 10:10:13 -0700 | [diff] [blame] | 4096 | if (!update_lines_cols()) { |
| 4097 | Py_DECREF(result); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4098 | return NULL; |
Zackery Spytz | 2bc3434 | 2019-11-17 10:10:13 -0700 | [diff] [blame] | 4099 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4100 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4101 | } |
| 4102 | |
| 4103 | #endif |
| 4104 | |
| 4105 | #ifdef HAVE_CURSES_RESIZE_TERM |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4106 | /*[clinic input] |
| 4107 | _curses.resize_term |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4108 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4109 | nlines: int |
| 4110 | Height. |
| 4111 | ncols: int |
| 4112 | Width. |
| 4113 | / |
| 4114 | |
| 4115 | Backend function used by resizeterm(), performing most of the work. |
| 4116 | |
| 4117 | When resizing the windows, resize_term() blank-fills the areas that are |
| 4118 | extended. The calling application should fill in these areas with appropriate |
| 4119 | data. The resize_term() function attempts to resize all windows. However, |
| 4120 | due to the calling convention of pads, it is not possible to resize these |
| 4121 | without additional interaction with the application. |
| 4122 | [clinic start generated code]*/ |
| 4123 | |
| 4124 | static PyObject * |
| 4125 | _curses_resize_term_impl(PyObject *module, int nlines, int ncols) |
| 4126 | /*[clinic end generated code: output=9e26d8b9ea311ed2 input=2197edd05b049ed4]*/ |
| 4127 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4128 | PyObject *result; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4129 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4130 | PyCursesInitialised; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4131 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4132 | result = PyCursesCheckERR(resize_term(nlines, ncols), "resize_term"); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4133 | if (!result) |
| 4134 | return NULL; |
Zackery Spytz | 2bc3434 | 2019-11-17 10:10:13 -0700 | [diff] [blame] | 4135 | if (!update_lines_cols()) { |
| 4136 | Py_DECREF(result); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4137 | return NULL; |
Zackery Spytz | 2bc3434 | 2019-11-17 10:10:13 -0700 | [diff] [blame] | 4138 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4139 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4140 | } |
| 4141 | #endif /* HAVE_CURSES_RESIZE_TERM */ |
| 4142 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4143 | /*[clinic input] |
| 4144 | _curses.savetty |
| 4145 | |
| 4146 | Save terminal mode. |
| 4147 | [clinic start generated code]*/ |
| 4148 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 4149 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4150 | _curses_savetty_impl(PyObject *module) |
| 4151 | /*[clinic end generated code: output=6babc49f12b42199 input=fce6b2b7d2200102]*/ |
| 4152 | NoArgNoReturnFunctionBody(savetty) |
| 4153 | |
| 4154 | #ifdef getsyx |
| 4155 | /*[clinic input] |
| 4156 | _curses.setsyx |
| 4157 | |
| 4158 | y: int |
| 4159 | Y-coordinate. |
| 4160 | x: int |
| 4161 | X-coordinate. |
| 4162 | / |
| 4163 | |
| 4164 | Set the virtual screen cursor. |
| 4165 | |
| 4166 | If y and x are both -1, then leaveok is set. |
| 4167 | [clinic start generated code]*/ |
| 4168 | |
| 4169 | static PyObject * |
| 4170 | _curses_setsyx_impl(PyObject *module, int y, int x) |
| 4171 | /*[clinic end generated code: output=23dcf753511a2464 input=fa7f2b208e10a557]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 4172 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4173 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 4174 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4175 | setsyx(y,x); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 4176 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 4177 | Py_RETURN_NONE; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4178 | } |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 4179 | #endif |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4180 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4181 | /*[clinic input] |
| 4182 | _curses.start_color |
| 4183 | |
| 4184 | Initializes eight basic colors and global variables COLORS and COLOR_PAIRS. |
| 4185 | |
| 4186 | Must be called if the programmer wants to use colors, and before any other |
| 4187 | color manipulation routine is called. It is good practice to call this |
| 4188 | routine right after initscr(). |
| 4189 | |
| 4190 | It also restores the colors on the terminal to the values they had when the |
| 4191 | terminal was just turned on. |
| 4192 | [clinic start generated code]*/ |
| 4193 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4194 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4195 | _curses_start_color_impl(PyObject *module) |
| 4196 | /*[clinic end generated code: output=8b772b41d8090ede input=0ca0ecb2b77e1a12]*/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4197 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4198 | int code; |
| 4199 | PyObject *c, *cp; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 4200 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4201 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 4202 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4203 | code = start_color(); |
| 4204 | if (code != ERR) { |
| 4205 | initialisedcolors = TRUE; |
| 4206 | c = PyLong_FromLong((long) COLORS); |
Christian Heimes | a956e64 | 2013-07-26 14:45:37 +0200 | [diff] [blame] | 4207 | if (c == NULL) |
| 4208 | return NULL; |
Zackery Spytz | 2bc3434 | 2019-11-17 10:10:13 -0700 | [diff] [blame] | 4209 | if (PyDict_SetItemString(ModDict, "COLORS", c) < 0) { |
| 4210 | Py_DECREF(c); |
| 4211 | return NULL; |
| 4212 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4213 | Py_DECREF(c); |
| 4214 | cp = PyLong_FromLong((long) COLOR_PAIRS); |
Christian Heimes | a956e64 | 2013-07-26 14:45:37 +0200 | [diff] [blame] | 4215 | if (cp == NULL) |
| 4216 | return NULL; |
Zackery Spytz | 2bc3434 | 2019-11-17 10:10:13 -0700 | [diff] [blame] | 4217 | if (PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp) < 0) { |
| 4218 | Py_DECREF(cp); |
| 4219 | return NULL; |
| 4220 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4221 | Py_DECREF(cp); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 4222 | Py_RETURN_NONE; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4223 | } else { |
| 4224 | PyErr_SetString(PyCursesError, "start_color() returned ERR"); |
| 4225 | return NULL; |
| 4226 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 4227 | } |
| 4228 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4229 | /*[clinic input] |
| 4230 | _curses.termattrs |
| 4231 | |
| 4232 | Return a logical OR of all video attributes supported by the terminal. |
| 4233 | [clinic start generated code]*/ |
| 4234 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 4235 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4236 | _curses_termattrs_impl(PyObject *module) |
| 4237 | /*[clinic end generated code: output=b06f437fce1b6fc4 input=0559882a04f84d1d]*/ |
| 4238 | NoArgReturnIntFunctionBody(termattrs) |
| 4239 | |
| 4240 | /*[clinic input] |
| 4241 | _curses.termname |
| 4242 | |
| 4243 | Return the value of the environment variable TERM, truncated to 14 characters. |
| 4244 | [clinic start generated code]*/ |
| 4245 | |
| 4246 | static PyObject * |
| 4247 | _curses_termname_impl(PyObject *module) |
| 4248 | /*[clinic end generated code: output=96375577ebbd67fd input=33c08d000944f33f]*/ |
| 4249 | NoArgReturnStringFunctionBody(termname) |
| 4250 | |
| 4251 | /*[clinic input] |
| 4252 | _curses.tigetflag |
| 4253 | |
| 4254 | capname: str |
| 4255 | The terminfo capability name. |
| 4256 | / |
| 4257 | |
| 4258 | Return the value of the Boolean capability. |
| 4259 | |
| 4260 | The value -1 is returned if capname is not a Boolean capability, or 0 if |
| 4261 | it is canceled or absent from the terminal description. |
| 4262 | [clinic start generated code]*/ |
| 4263 | |
| 4264 | static PyObject * |
| 4265 | _curses_tigetflag_impl(PyObject *module, const char *capname) |
| 4266 | /*[clinic end generated code: output=8853c0e55542195b input=b0787af9e3e9a6ce]*/ |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 4267 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4268 | PyCursesSetupTermCalled; |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 4269 | |
Xiang Zhang | 44742e9 | 2018-06-23 12:29:30 +0800 | [diff] [blame] | 4270 | return PyLong_FromLong( (long) tigetflag( (char *)capname ) ); |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4273 | /*[clinic input] |
| 4274 | _curses.tigetnum |
| 4275 | |
| 4276 | capname: str |
| 4277 | The terminfo capability name. |
| 4278 | / |
| 4279 | |
| 4280 | Return the value of the numeric capability. |
| 4281 | |
| 4282 | The value -2 is returned if capname is not a numeric capability, or -1 if |
| 4283 | it is canceled or absent from the terminal description. |
| 4284 | [clinic start generated code]*/ |
| 4285 | |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 4286 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4287 | _curses_tigetnum_impl(PyObject *module, const char *capname) |
| 4288 | /*[clinic end generated code: output=46f8b0a1b5dff42f input=5cdf2f410b109720]*/ |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 4289 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4290 | PyCursesSetupTermCalled; |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 4291 | |
Xiang Zhang | 44742e9 | 2018-06-23 12:29:30 +0800 | [diff] [blame] | 4292 | return PyLong_FromLong( (long) tigetnum( (char *)capname ) ); |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4295 | /*[clinic input] |
| 4296 | _curses.tigetstr |
| 4297 | |
| 4298 | capname: str |
| 4299 | The terminfo capability name. |
| 4300 | / |
| 4301 | |
| 4302 | Return the value of the string capability. |
| 4303 | |
| 4304 | None is returned if capname is not a string capability, or is canceled or |
| 4305 | absent from the terminal description. |
| 4306 | [clinic start generated code]*/ |
| 4307 | |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 4308 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4309 | _curses_tigetstr_impl(PyObject *module, const char *capname) |
| 4310 | /*[clinic end generated code: output=f22b576ad60248f3 input=36644df25c73c0a7]*/ |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 4311 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4312 | PyCursesSetupTermCalled; |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 4313 | |
Xiang Zhang | 44742e9 | 2018-06-23 12:29:30 +0800 | [diff] [blame] | 4314 | capname = tigetstr( (char *)capname ); |
Serhiy Storchaka | 0b3ec19 | 2017-03-23 17:53:47 +0200 | [diff] [blame] | 4315 | if (capname == NULL || capname == (char*) -1) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 4316 | Py_RETURN_NONE; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4317 | } |
| 4318 | return PyBytes_FromString( capname ); |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 4319 | } |
| 4320 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4321 | /*[clinic input] |
| 4322 | _curses.tparm |
| 4323 | |
| 4324 | str: str(accept={robuffer}) |
| 4325 | Parameterized byte string obtained from the terminfo database. |
| 4326 | i1: int = 0 |
| 4327 | i2: int = 0 |
| 4328 | i3: int = 0 |
| 4329 | i4: int = 0 |
| 4330 | i5: int = 0 |
| 4331 | i6: int = 0 |
| 4332 | i7: int = 0 |
| 4333 | i8: int = 0 |
| 4334 | i9: int = 0 |
| 4335 | / |
| 4336 | |
| 4337 | Instantiate the specified byte string with the supplied parameters. |
| 4338 | [clinic start generated code]*/ |
| 4339 | |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 4340 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4341 | _curses_tparm_impl(PyObject *module, const char *str, int i1, int i2, int i3, |
| 4342 | int i4, int i5, int i6, int i7, int i8, int i9) |
| 4343 | /*[clinic end generated code: output=599f62b615c667ff input=5e30b15786f032aa]*/ |
Andrew M. Kuchling | f16e0ed | 2000-11-07 03:35:24 +0000 | [diff] [blame] | 4344 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4345 | char* result = NULL; |
Andrew M. Kuchling | f16e0ed | 2000-11-07 03:35:24 +0000 | [diff] [blame] | 4346 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4347 | PyCursesSetupTermCalled; |
Andrew M. Kuchling | f16e0ed | 2000-11-07 03:35:24 +0000 | [diff] [blame] | 4348 | |
Xiang Zhang | 44742e9 | 2018-06-23 12:29:30 +0800 | [diff] [blame] | 4349 | result = tparm((char *)str,i1,i2,i3,i4,i5,i6,i7,i8,i9); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4350 | if (!result) { |
| 4351 | PyErr_SetString(PyCursesError, "tparm() returned NULL"); |
| 4352 | return NULL; |
| 4353 | } |
Michael W. Hudson | 2100798 | 2002-11-21 14:17:51 +0000 | [diff] [blame] | 4354 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4355 | return PyBytes_FromString(result); |
Andrew M. Kuchling | f16e0ed | 2000-11-07 03:35:24 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 4358 | #ifdef HAVE_CURSES_TYPEAHEAD |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4359 | /*[clinic input] |
| 4360 | _curses.typeahead |
| 4361 | |
| 4362 | fd: int |
| 4363 | File descriptor. |
| 4364 | / |
| 4365 | |
| 4366 | Specify that the file descriptor fd be used for typeahead checking. |
| 4367 | |
| 4368 | If fd is -1, then no typeahead checking is done. |
| 4369 | [clinic start generated code]*/ |
| 4370 | |
Andrew M. Kuchling | f16e0ed | 2000-11-07 03:35:24 +0000 | [diff] [blame] | 4371 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4372 | _curses_typeahead_impl(PyObject *module, int fd) |
| 4373 | /*[clinic end generated code: output=084bb649d7066583 input=f2968d8e1805051b]*/ |
Andrew M. Kuchling | 97311bb | 2000-06-21 01:41:48 +0000 | [diff] [blame] | 4374 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4375 | PyCursesInitialised; |
Andrew M. Kuchling | 97311bb | 2000-06-21 01:41:48 +0000 | [diff] [blame] | 4376 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4377 | return PyCursesCheckERR(typeahead( fd ), "typeahead"); |
Andrew M. Kuchling | 97311bb | 2000-06-21 01:41:48 +0000 | [diff] [blame] | 4378 | } |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 4379 | #endif |
Andrew M. Kuchling | 97311bb | 2000-06-21 01:41:48 +0000 | [diff] [blame] | 4380 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4381 | /*[clinic input] |
| 4382 | _curses.unctrl |
| 4383 | |
| 4384 | ch: object |
| 4385 | / |
| 4386 | |
| 4387 | Return a string which is a printable representation of the character ch. |
| 4388 | |
| 4389 | Control characters are displayed as a caret followed by the character, |
| 4390 | for example as ^C. Printing characters are left as they are. |
| 4391 | [clinic start generated code]*/ |
| 4392 | |
Andrew M. Kuchling | 97311bb | 2000-06-21 01:41:48 +0000 | [diff] [blame] | 4393 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4394 | _curses_unctrl(PyObject *module, PyObject *ch) |
| 4395 | /*[clinic end generated code: output=8e07fafc430c9434 input=cd1e35e16cd1ace4]*/ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 4396 | { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4397 | chtype ch_; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 4398 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4399 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 4400 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4401 | if (!PyCurses_ConvertToChtype(NULL, ch, &ch_)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4402 | return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 4403 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4404 | return PyBytes_FromString(unctrl(ch_)); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4405 | } |
| 4406 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4407 | /*[clinic input] |
| 4408 | _curses.ungetch |
| 4409 | |
| 4410 | ch: object |
| 4411 | / |
| 4412 | |
| 4413 | Push ch so the next getch() will return it. |
| 4414 | [clinic start generated code]*/ |
| 4415 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4416 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4417 | _curses_ungetch(PyObject *module, PyObject *ch) |
| 4418 | /*[clinic end generated code: output=9b19d8268376d887 input=6681e6ae4c42e5eb]*/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4419 | { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4420 | chtype ch_; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 4421 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4422 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 4423 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4424 | if (!PyCurses_ConvertToChtype(NULL, ch, &ch_)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4425 | return NULL; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 4426 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4427 | return PyCursesCheckERR(ungetch(ch_), "ungetch"); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4428 | } |
| 4429 | |
Victor Stinner | 71e44cb | 2011-09-06 01:53:03 +0200 | [diff] [blame] | 4430 | #ifdef HAVE_NCURSESW |
| 4431 | /* Convert an object to a character (wchar_t): |
| 4432 | |
| 4433 | - int |
| 4434 | - str of length 1 |
| 4435 | |
| 4436 | Return 1 on success, 0 on error. */ |
| 4437 | static int |
| 4438 | PyCurses_ConvertToWchar_t(PyObject *obj, |
| 4439 | wchar_t *wch) |
| 4440 | { |
| 4441 | if (PyUnicode_Check(obj)) { |
| 4442 | wchar_t buffer[2]; |
| 4443 | if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) { |
| 4444 | PyErr_Format(PyExc_TypeError, |
Anthony Sottile | c9345e3 | 2019-07-31 05:11:24 -0700 | [diff] [blame] | 4445 | "expect str of length 1 or int, " |
Victor Stinner | 71e44cb | 2011-09-06 01:53:03 +0200 | [diff] [blame] | 4446 | "got a str of length %zi", |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 4447 | PyUnicode_GET_LENGTH(obj)); |
Victor Stinner | 71e44cb | 2011-09-06 01:53:03 +0200 | [diff] [blame] | 4448 | return 0; |
| 4449 | } |
| 4450 | *wch = buffer[0]; |
| 4451 | return 2; |
| 4452 | } |
| 4453 | else if (PyLong_CheckExact(obj)) { |
| 4454 | long value; |
| 4455 | int overflow; |
| 4456 | value = PyLong_AsLongAndOverflow(obj, &overflow); |
| 4457 | if (overflow) { |
| 4458 | PyErr_SetString(PyExc_OverflowError, |
| 4459 | "int doesn't fit in long"); |
| 4460 | return 0; |
| 4461 | } |
| 4462 | *wch = (wchar_t)value; |
| 4463 | if ((long)*wch != value) { |
| 4464 | PyErr_Format(PyExc_OverflowError, |
| 4465 | "character doesn't fit in wchar_t"); |
| 4466 | return 0; |
| 4467 | } |
| 4468 | return 1; |
| 4469 | } |
| 4470 | else { |
| 4471 | PyErr_Format(PyExc_TypeError, |
Anthony Sottile | c9345e3 | 2019-07-31 05:11:24 -0700 | [diff] [blame] | 4472 | "expect str of length 1 or int, got %s", |
Victor Stinner | 71e44cb | 2011-09-06 01:53:03 +0200 | [diff] [blame] | 4473 | Py_TYPE(obj)->tp_name); |
| 4474 | return 0; |
| 4475 | } |
| 4476 | } |
| 4477 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4478 | /*[clinic input] |
| 4479 | _curses.unget_wch |
| 4480 | |
| 4481 | ch: object |
| 4482 | / |
| 4483 | |
| 4484 | Push ch so the next get_wch() will return it. |
| 4485 | [clinic start generated code]*/ |
| 4486 | |
Victor Stinner | 71e44cb | 2011-09-06 01:53:03 +0200 | [diff] [blame] | 4487 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4488 | _curses_unget_wch(PyObject *module, PyObject *ch) |
| 4489 | /*[clinic end generated code: output=1974c9fb01d37863 input=0d56dc65a46feebb]*/ |
Victor Stinner | 71e44cb | 2011-09-06 01:53:03 +0200 | [diff] [blame] | 4490 | { |
Victor Stinner | 71e44cb | 2011-09-06 01:53:03 +0200 | [diff] [blame] | 4491 | wchar_t wch; |
| 4492 | |
| 4493 | PyCursesInitialised; |
| 4494 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4495 | if (!PyCurses_ConvertToWchar_t(ch, &wch)) |
Victor Stinner | 71e44cb | 2011-09-06 01:53:03 +0200 | [diff] [blame] | 4496 | return NULL; |
| 4497 | return PyCursesCheckERR(unget_wch(wch), "unget_wch"); |
| 4498 | } |
| 4499 | #endif |
| 4500 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4501 | #ifdef HAVE_CURSES_USE_ENV |
| 4502 | /*[clinic input] |
| 4503 | _curses.use_env |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 4504 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4505 | flag: bool(accept={int}) |
| 4506 | / |
| 4507 | |
| 4508 | Use environment variables LINES and COLUMNS. |
| 4509 | |
| 4510 | If used, this function should be called before initscr() or newterm() are |
| 4511 | called. |
| 4512 | |
| 4513 | When flag is False, the values of lines and columns specified in the terminfo |
| 4514 | database will be used, even if environment variables LINES and COLUMNS (used |
| 4515 | by default) are set, or if curses is running in a window (in which case |
| 4516 | default behavior would be to use the window size if LINES and COLUMNS are |
| 4517 | not set). |
| 4518 | [clinic start generated code]*/ |
| 4519 | |
| 4520 | static PyObject * |
| 4521 | _curses_use_env_impl(PyObject *module, int flag) |
| 4522 | /*[clinic end generated code: output=b2c445e435c0b164 input=1778eb1e9151ea37]*/ |
| 4523 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4524 | use_env(flag); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 4525 | Py_RETURN_NONE; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4526 | } |
Serhiy Storchaka | baac01e | 2017-10-31 13:56:44 +0200 | [diff] [blame] | 4527 | #endif |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4528 | |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 4529 | #ifndef STRICT_SYSV_CURSES |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4530 | /*[clinic input] |
| 4531 | _curses.use_default_colors |
| 4532 | |
| 4533 | Allow use of default values for colors on terminals supporting this feature. |
| 4534 | |
| 4535 | Use this to support transparency in your application. The default color |
| 4536 | is assigned to the color number -1. |
| 4537 | [clinic start generated code]*/ |
| 4538 | |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 4539 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4540 | _curses_use_default_colors_impl(PyObject *module) |
| 4541 | /*[clinic end generated code: output=a3b81ff71dd901be input=656844367470e8fc]*/ |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 4542 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4543 | int code; |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 4544 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4545 | PyCursesInitialised; |
| 4546 | PyCursesInitialisedColor; |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 4547 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4548 | code = use_default_colors(); |
| 4549 | if (code != ERR) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 4550 | Py_RETURN_NONE; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4551 | } else { |
| 4552 | PyErr_SetString(PyCursesError, "use_default_colors() returned ERR"); |
| 4553 | return NULL; |
| 4554 | } |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 4555 | } |
| 4556 | #endif /* STRICT_SYSV_CURSES */ |
| 4557 | |
Serhiy Storchaka | b232df9 | 2018-10-30 13:22:42 +0200 | [diff] [blame] | 4558 | |
| 4559 | #ifdef NCURSES_VERSION |
| 4560 | |
| 4561 | PyDoc_STRVAR(ncurses_version__doc__, |
| 4562 | "curses.ncurses_version\n\ |
| 4563 | \n\ |
| 4564 | Ncurses version information as a named tuple."); |
| 4565 | |
| 4566 | static PyTypeObject NcursesVersionType; |
| 4567 | |
| 4568 | static PyStructSequence_Field ncurses_version_fields[] = { |
| 4569 | {"major", "Major release number"}, |
| 4570 | {"minor", "Minor release number"}, |
| 4571 | {"patch", "Patch release number"}, |
| 4572 | {0} |
| 4573 | }; |
| 4574 | |
| 4575 | static PyStructSequence_Desc ncurses_version_desc = { |
| 4576 | "curses.ncurses_version", /* name */ |
| 4577 | ncurses_version__doc__, /* doc */ |
| 4578 | ncurses_version_fields, /* fields */ |
| 4579 | 3 |
| 4580 | }; |
| 4581 | |
| 4582 | static PyObject * |
| 4583 | make_ncurses_version(void) |
| 4584 | { |
| 4585 | PyObject *ncurses_version; |
| 4586 | int pos = 0; |
| 4587 | |
| 4588 | ncurses_version = PyStructSequence_New(&NcursesVersionType); |
| 4589 | if (ncurses_version == NULL) { |
| 4590 | return NULL; |
| 4591 | } |
| 4592 | |
| 4593 | #define SetIntItem(flag) \ |
| 4594 | PyStructSequence_SET_ITEM(ncurses_version, pos++, PyLong_FromLong(flag)); \ |
| 4595 | if (PyErr_Occurred()) { \ |
| 4596 | Py_CLEAR(ncurses_version); \ |
| 4597 | return NULL; \ |
| 4598 | } |
| 4599 | |
| 4600 | SetIntItem(NCURSES_VERSION_MAJOR) |
| 4601 | SetIntItem(NCURSES_VERSION_MINOR) |
| 4602 | SetIntItem(NCURSES_VERSION_PATCH) |
| 4603 | #undef SetIntItem |
| 4604 | |
| 4605 | return ncurses_version; |
| 4606 | } |
| 4607 | |
| 4608 | #endif /* NCURSES_VERSION */ |
| 4609 | |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 4610 | /*[clinic input] |
| 4611 | _curses.has_extended_color_support |
| 4612 | |
| 4613 | Return True if the module supports extended colors; otherwise, return False. |
| 4614 | |
| 4615 | Extended color support allows more than 256 color-pairs for terminals |
| 4616 | that support more than 16 colors (e.g. xterm-256color). |
| 4617 | [clinic start generated code]*/ |
| 4618 | |
| 4619 | static PyObject * |
| 4620 | _curses_has_extended_color_support_impl(PyObject *module) |
| 4621 | /*[clinic end generated code: output=68f1be2b57d92e22 input=4b905f046e35ee9f]*/ |
| 4622 | { |
| 4623 | return PyBool_FromLong(_NCURSES_EXTENDED_COLOR_FUNCS); |
| 4624 | } |
Serhiy Storchaka | b232df9 | 2018-10-30 13:22:42 +0200 | [diff] [blame] | 4625 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4626 | /* List of functions defined in the module */ |
| 4627 | |
| 4628 | static PyMethodDef PyCurses_methods[] = { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4629 | _CURSES_BAUDRATE_METHODDEF |
| 4630 | _CURSES_BEEP_METHODDEF |
| 4631 | _CURSES_CAN_CHANGE_COLOR_METHODDEF |
| 4632 | _CURSES_CBREAK_METHODDEF |
| 4633 | _CURSES_COLOR_CONTENT_METHODDEF |
| 4634 | _CURSES_COLOR_PAIR_METHODDEF |
| 4635 | _CURSES_CURS_SET_METHODDEF |
| 4636 | _CURSES_DEF_PROG_MODE_METHODDEF |
| 4637 | _CURSES_DEF_SHELL_MODE_METHODDEF |
| 4638 | _CURSES_DELAY_OUTPUT_METHODDEF |
| 4639 | _CURSES_DOUPDATE_METHODDEF |
| 4640 | _CURSES_ECHO_METHODDEF |
| 4641 | _CURSES_ENDWIN_METHODDEF |
| 4642 | _CURSES_ERASECHAR_METHODDEF |
| 4643 | _CURSES_FILTER_METHODDEF |
| 4644 | _CURSES_FLASH_METHODDEF |
| 4645 | _CURSES_FLUSHINP_METHODDEF |
| 4646 | _CURSES_GETMOUSE_METHODDEF |
| 4647 | _CURSES_UNGETMOUSE_METHODDEF |
| 4648 | _CURSES_GETSYX_METHODDEF |
| 4649 | _CURSES_GETWIN_METHODDEF |
| 4650 | _CURSES_HAS_COLORS_METHODDEF |
Hans Petter Jansson | da4e09f | 2020-08-03 22:51:33 -0500 | [diff] [blame] | 4651 | _CURSES_HAS_EXTENDED_COLOR_SUPPORT_METHODDEF |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4652 | _CURSES_HAS_IC_METHODDEF |
| 4653 | _CURSES_HAS_IL_METHODDEF |
| 4654 | _CURSES_HAS_KEY_METHODDEF |
| 4655 | _CURSES_HALFDELAY_METHODDEF |
| 4656 | _CURSES_INIT_COLOR_METHODDEF |
| 4657 | _CURSES_INIT_PAIR_METHODDEF |
| 4658 | _CURSES_INITSCR_METHODDEF |
| 4659 | _CURSES_INTRFLUSH_METHODDEF |
| 4660 | _CURSES_ISENDWIN_METHODDEF |
| 4661 | _CURSES_IS_TERM_RESIZED_METHODDEF |
| 4662 | _CURSES_KEYNAME_METHODDEF |
| 4663 | _CURSES_KILLCHAR_METHODDEF |
| 4664 | _CURSES_LONGNAME_METHODDEF |
| 4665 | _CURSES_META_METHODDEF |
| 4666 | _CURSES_MOUSEINTERVAL_METHODDEF |
| 4667 | _CURSES_MOUSEMASK_METHODDEF |
| 4668 | _CURSES_NAPMS_METHODDEF |
| 4669 | _CURSES_NEWPAD_METHODDEF |
| 4670 | _CURSES_NEWWIN_METHODDEF |
| 4671 | _CURSES_NL_METHODDEF |
| 4672 | _CURSES_NOCBREAK_METHODDEF |
| 4673 | _CURSES_NOECHO_METHODDEF |
| 4674 | _CURSES_NONL_METHODDEF |
| 4675 | _CURSES_NOQIFLUSH_METHODDEF |
| 4676 | _CURSES_NORAW_METHODDEF |
| 4677 | _CURSES_PAIR_CONTENT_METHODDEF |
| 4678 | _CURSES_PAIR_NUMBER_METHODDEF |
| 4679 | _CURSES_PUTP_METHODDEF |
| 4680 | _CURSES_QIFLUSH_METHODDEF |
| 4681 | _CURSES_RAW_METHODDEF |
| 4682 | _CURSES_RESET_PROG_MODE_METHODDEF |
| 4683 | _CURSES_RESET_SHELL_MODE_METHODDEF |
| 4684 | _CURSES_RESETTY_METHODDEF |
| 4685 | _CURSES_RESIZETERM_METHODDEF |
| 4686 | _CURSES_RESIZE_TERM_METHODDEF |
| 4687 | _CURSES_SAVETTY_METHODDEF |
Batuhan Taşkaya | 4991cf4 | 2020-03-03 05:00:10 +0300 | [diff] [blame] | 4688 | #if defined(NCURSES_EXT_FUNCS) && NCURSES_EXT_FUNCS >= 20081102 |
Anthony Sottile | b32cb97 | 2019-10-31 02:13:48 -0700 | [diff] [blame] | 4689 | _CURSES_GET_ESCDELAY_METHODDEF |
| 4690 | _CURSES_SET_ESCDELAY_METHODDEF |
Batuhan Taşkaya | 4991cf4 | 2020-03-03 05:00:10 +0300 | [diff] [blame] | 4691 | #endif |
Anthony Sottile | b32cb97 | 2019-10-31 02:13:48 -0700 | [diff] [blame] | 4692 | _CURSES_GET_TABSIZE_METHODDEF |
| 4693 | _CURSES_SET_TABSIZE_METHODDEF |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4694 | _CURSES_SETSYX_METHODDEF |
| 4695 | _CURSES_SETUPTERM_METHODDEF |
| 4696 | _CURSES_START_COLOR_METHODDEF |
| 4697 | _CURSES_TERMATTRS_METHODDEF |
| 4698 | _CURSES_TERMNAME_METHODDEF |
| 4699 | _CURSES_TIGETFLAG_METHODDEF |
| 4700 | _CURSES_TIGETNUM_METHODDEF |
| 4701 | _CURSES_TIGETSTR_METHODDEF |
| 4702 | _CURSES_TPARM_METHODDEF |
| 4703 | _CURSES_TYPEAHEAD_METHODDEF |
| 4704 | _CURSES_UNCTRL_METHODDEF |
| 4705 | _CURSES_UNGETCH_METHODDEF |
| 4706 | _CURSES_UPDATE_LINES_COLS_METHODDEF |
| 4707 | _CURSES_UNGET_WCH_METHODDEF |
| 4708 | _CURSES_USE_ENV_METHODDEF |
| 4709 | _CURSES_USE_DEFAULT_COLORS_METHODDEF |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4710 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4711 | }; |
| 4712 | |
| 4713 | /* Initialization function for the module */ |
| 4714 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4715 | |
| 4716 | static struct PyModuleDef _cursesmodule = { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4717 | PyModuleDef_HEAD_INIT, |
| 4718 | "_curses", |
| 4719 | NULL, |
| 4720 | -1, |
| 4721 | PyCurses_methods, |
| 4722 | NULL, |
| 4723 | NULL, |
| 4724 | NULL, |
| 4725 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4726 | }; |
| 4727 | |
Hai Shi | 2f12a1b | 2021-01-22 18:06:43 +0800 | [diff] [blame^] | 4728 | static void |
| 4729 | curses_destructor(PyObject *op) |
| 4730 | { |
| 4731 | void *ptr = PyCapsule_GetPointer(op, PyCurses_CAPSULE_NAME); |
| 4732 | Py_DECREF(*(void **)ptr); |
| 4733 | PyMem_Free(ptr); |
| 4734 | } |
| 4735 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4736 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4737 | PyInit__curses(void) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4738 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4739 | PyObject *m, *d, *v, *c_api_object; |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 4740 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4741 | /* Initialize object type */ |
| 4742 | if (PyType_Ready(&PyCursesWindow_Type) < 0) |
| 4743 | return NULL; |
Guido van Rossum | 6915c4d | 2001-01-19 00:28:08 +0000 | [diff] [blame] | 4744 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4745 | /* Create the module and add the functions */ |
| 4746 | m = PyModule_Create(&_cursesmodule); |
| 4747 | if (m == NULL) |
| 4748 | return NULL; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4749 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4750 | /* Add some symbolic constants to the module */ |
| 4751 | d = PyModule_GetDict(m); |
| 4752 | if (d == NULL) |
| 4753 | return NULL; |
| 4754 | ModDict = d; /* For PyCurses_InitScr to use later */ |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 4755 | |
Hai Shi | 2f12a1b | 2021-01-22 18:06:43 +0800 | [diff] [blame^] | 4756 | void **PyCurses_API = PyMem_Calloc(PyCurses_API_pointers, sizeof(void *)); |
| 4757 | if (PyCurses_API == NULL) { |
| 4758 | PyErr_NoMemory(); |
| 4759 | return NULL; |
| 4760 | } |
| 4761 | /* Initialize the C API pointer array */ |
| 4762 | PyCurses_API[0] = (void *)Py_NewRef(&PyCursesWindow_Type); |
| 4763 | PyCurses_API[1] = (void *)func_PyCursesSetupTermCalled; |
| 4764 | PyCurses_API[2] = (void *)func_PyCursesInitialised; |
| 4765 | PyCurses_API[3] = (void *)func_PyCursesInitialisedColor; |
| 4766 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4767 | /* Add a capsule for the C API */ |
Hai Shi | 2f12a1b | 2021-01-22 18:06:43 +0800 | [diff] [blame^] | 4768 | c_api_object = PyCapsule_New(PyCurses_API, PyCurses_CAPSULE_NAME, |
| 4769 | curses_destructor); |
| 4770 | if (c_api_object == NULL) { |
| 4771 | Py_DECREF(PyCurses_API[0]); |
| 4772 | PyMem_Free(PyCurses_API); |
| 4773 | return NULL; |
| 4774 | } |
| 4775 | if (PyDict_SetItemString(d, "_C_API", c_api_object) < 0) { |
| 4776 | Py_DECREF(c_api_object); |
| 4777 | return NULL; |
| 4778 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4779 | Py_DECREF(c_api_object); |
Guido van Rossum | fbea2f3 | 1994-08-31 22:05:27 +0000 | [diff] [blame] | 4780 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4781 | /* For exception curses.error */ |
| 4782 | PyCursesError = PyErr_NewException("_curses.error", NULL, NULL); |
| 4783 | PyDict_SetItemString(d, "error", PyCursesError); |
Guido van Rossum | fbea2f3 | 1994-08-31 22:05:27 +0000 | [diff] [blame] | 4784 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4785 | /* Make the version available */ |
| 4786 | v = PyBytes_FromString(PyCursesVersion); |
| 4787 | PyDict_SetItemString(d, "version", v); |
| 4788 | PyDict_SetItemString(d, "__version__", v); |
| 4789 | Py_DECREF(v); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4790 | |
Serhiy Storchaka | b232df9 | 2018-10-30 13:22:42 +0200 | [diff] [blame] | 4791 | #ifdef NCURSES_VERSION |
| 4792 | /* ncurses_version */ |
| 4793 | if (NcursesVersionType.tp_name == NULL) { |
| 4794 | if (PyStructSequence_InitType2(&NcursesVersionType, |
| 4795 | &ncurses_version_desc) < 0) |
| 4796 | return NULL; |
| 4797 | } |
| 4798 | v = make_ncurses_version(); |
| 4799 | if (v == NULL) { |
| 4800 | return NULL; |
| 4801 | } |
| 4802 | PyDict_SetItemString(d, "ncurses_version", v); |
| 4803 | Py_DECREF(v); |
| 4804 | |
| 4805 | /* prevent user from creating new instances */ |
| 4806 | NcursesVersionType.tp_init = NULL; |
| 4807 | NcursesVersionType.tp_new = NULL; |
| 4808 | if (PyDict_DelItemString(NcursesVersionType.tp_dict, "__new__") < 0 && |
| 4809 | PyErr_ExceptionMatches(PyExc_KeyError)) |
| 4810 | { |
| 4811 | PyErr_Clear(); |
| 4812 | } |
| 4813 | #endif /* NCURSES_VERSION */ |
| 4814 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4815 | SetDictInt("ERR", ERR); |
| 4816 | SetDictInt("OK", OK); |
Andrew M. Kuchling | caefb37 | 2000-12-26 15:57:01 +0000 | [diff] [blame] | 4817 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4818 | /* Here are some attributes you can add to chars to print */ |
| 4819 | |
| 4820 | SetDictInt("A_ATTRIBUTES", A_ATTRIBUTES); |
| 4821 | SetDictInt("A_NORMAL", A_NORMAL); |
| 4822 | SetDictInt("A_STANDOUT", A_STANDOUT); |
| 4823 | SetDictInt("A_UNDERLINE", A_UNDERLINE); |
| 4824 | SetDictInt("A_REVERSE", A_REVERSE); |
| 4825 | SetDictInt("A_BLINK", A_BLINK); |
| 4826 | SetDictInt("A_DIM", A_DIM); |
| 4827 | SetDictInt("A_BOLD", A_BOLD); |
| 4828 | SetDictInt("A_ALTCHARSET", A_ALTCHARSET); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4829 | SetDictInt("A_INVIS", A_INVIS); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4830 | SetDictInt("A_PROTECT", A_PROTECT); |
| 4831 | SetDictInt("A_CHARTEXT", A_CHARTEXT); |
| 4832 | SetDictInt("A_COLOR", A_COLOR); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 4833 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4834 | /* The following are never available with strict SYSV curses */ |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 4835 | #ifdef A_HORIZONTAL |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4836 | SetDictInt("A_HORIZONTAL", A_HORIZONTAL); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 4837 | #endif |
| 4838 | #ifdef A_LEFT |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4839 | SetDictInt("A_LEFT", A_LEFT); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 4840 | #endif |
| 4841 | #ifdef A_LOW |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4842 | SetDictInt("A_LOW", A_LOW); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 4843 | #endif |
| 4844 | #ifdef A_RIGHT |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4845 | SetDictInt("A_RIGHT", A_RIGHT); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 4846 | #endif |
| 4847 | #ifdef A_TOP |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4848 | SetDictInt("A_TOP", A_TOP); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 4849 | #endif |
| 4850 | #ifdef A_VERTICAL |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4851 | SetDictInt("A_VERTICAL", A_VERTICAL); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 4852 | #endif |
Andrew M. Kuchling | 03e644b | 2000-05-23 16:24:54 +0000 | [diff] [blame] | 4853 | |
Xiang Zhang | 116dd5e | 2017-06-16 11:20:07 +0800 | [diff] [blame] | 4854 | /* ncurses extension */ |
| 4855 | #ifdef A_ITALIC |
| 4856 | SetDictInt("A_ITALIC", A_ITALIC); |
| 4857 | #endif |
| 4858 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4859 | SetDictInt("COLOR_BLACK", COLOR_BLACK); |
| 4860 | SetDictInt("COLOR_RED", COLOR_RED); |
| 4861 | SetDictInt("COLOR_GREEN", COLOR_GREEN); |
| 4862 | SetDictInt("COLOR_YELLOW", COLOR_YELLOW); |
| 4863 | SetDictInt("COLOR_BLUE", COLOR_BLUE); |
| 4864 | SetDictInt("COLOR_MAGENTA", COLOR_MAGENTA); |
| 4865 | SetDictInt("COLOR_CYAN", COLOR_CYAN); |
| 4866 | SetDictInt("COLOR_WHITE", COLOR_WHITE); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4867 | |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 4868 | #ifdef NCURSES_MOUSE_VERSION |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4869 | /* Mouse-related constants */ |
| 4870 | SetDictInt("BUTTON1_PRESSED", BUTTON1_PRESSED); |
| 4871 | SetDictInt("BUTTON1_RELEASED", BUTTON1_RELEASED); |
| 4872 | SetDictInt("BUTTON1_CLICKED", BUTTON1_CLICKED); |
| 4873 | SetDictInt("BUTTON1_DOUBLE_CLICKED", BUTTON1_DOUBLE_CLICKED); |
| 4874 | SetDictInt("BUTTON1_TRIPLE_CLICKED", BUTTON1_TRIPLE_CLICKED); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 4875 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4876 | SetDictInt("BUTTON2_PRESSED", BUTTON2_PRESSED); |
| 4877 | SetDictInt("BUTTON2_RELEASED", BUTTON2_RELEASED); |
| 4878 | SetDictInt("BUTTON2_CLICKED", BUTTON2_CLICKED); |
| 4879 | SetDictInt("BUTTON2_DOUBLE_CLICKED", BUTTON2_DOUBLE_CLICKED); |
| 4880 | SetDictInt("BUTTON2_TRIPLE_CLICKED", BUTTON2_TRIPLE_CLICKED); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 4881 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4882 | SetDictInt("BUTTON3_PRESSED", BUTTON3_PRESSED); |
| 4883 | SetDictInt("BUTTON3_RELEASED", BUTTON3_RELEASED); |
| 4884 | SetDictInt("BUTTON3_CLICKED", BUTTON3_CLICKED); |
| 4885 | SetDictInt("BUTTON3_DOUBLE_CLICKED", BUTTON3_DOUBLE_CLICKED); |
| 4886 | SetDictInt("BUTTON3_TRIPLE_CLICKED", BUTTON3_TRIPLE_CLICKED); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 4887 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4888 | SetDictInt("BUTTON4_PRESSED", BUTTON4_PRESSED); |
| 4889 | SetDictInt("BUTTON4_RELEASED", BUTTON4_RELEASED); |
| 4890 | SetDictInt("BUTTON4_CLICKED", BUTTON4_CLICKED); |
| 4891 | SetDictInt("BUTTON4_DOUBLE_CLICKED", BUTTON4_DOUBLE_CLICKED); |
| 4892 | SetDictInt("BUTTON4_TRIPLE_CLICKED", BUTTON4_TRIPLE_CLICKED); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 4893 | |
Zackery Spytz | 14cfa32 | 2021-01-14 02:40:09 -0700 | [diff] [blame] | 4894 | #if NCURSES_MOUSE_VERSION > 1 |
| 4895 | SetDictInt("BUTTON5_PRESSED", BUTTON5_PRESSED); |
| 4896 | SetDictInt("BUTTON5_RELEASED", BUTTON5_RELEASED); |
| 4897 | SetDictInt("BUTTON5_CLICKED", BUTTON5_CLICKED); |
| 4898 | SetDictInt("BUTTON5_DOUBLE_CLICKED", BUTTON5_DOUBLE_CLICKED); |
| 4899 | SetDictInt("BUTTON5_TRIPLE_CLICKED", BUTTON5_TRIPLE_CLICKED); |
| 4900 | #endif |
| 4901 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4902 | SetDictInt("BUTTON_SHIFT", BUTTON_SHIFT); |
| 4903 | SetDictInt("BUTTON_CTRL", BUTTON_CTRL); |
| 4904 | SetDictInt("BUTTON_ALT", BUTTON_ALT); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 4905 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4906 | SetDictInt("ALL_MOUSE_EVENTS", ALL_MOUSE_EVENTS); |
| 4907 | SetDictInt("REPORT_MOUSE_POSITION", REPORT_MOUSE_POSITION); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 4908 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4909 | /* Now set everything up for KEY_ variables */ |
| 4910 | { |
| 4911 | int key; |
| 4912 | char *key_n; |
| 4913 | char *key_n2; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4914 | for (key=KEY_MIN;key < KEY_MAX; key++) { |
| 4915 | key_n = (char *)keyname(key); |
| 4916 | if (key_n == NULL || strcmp(key_n,"UNKNOWN KEY")==0) |
| 4917 | continue; |
| 4918 | if (strncmp(key_n,"KEY_F(",6)==0) { |
| 4919 | char *p1, *p2; |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 4920 | key_n2 = PyMem_Malloc(strlen(key_n)+1); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4921 | if (!key_n2) { |
| 4922 | PyErr_NoMemory(); |
| 4923 | break; |
| 4924 | } |
| 4925 | p1 = key_n; |
| 4926 | p2 = key_n2; |
| 4927 | while (*p1) { |
| 4928 | if (*p1 != '(' && *p1 != ')') { |
| 4929 | *p2 = *p1; |
| 4930 | p2++; |
| 4931 | } |
| 4932 | p1++; |
| 4933 | } |
| 4934 | *p2 = (char)0; |
| 4935 | } else |
| 4936 | key_n2 = key_n; |
| 4937 | SetDictInt(key_n2,key); |
| 4938 | if (key_n2 != key_n) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 4939 | PyMem_Free(key_n2); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4940 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4941 | SetDictInt("KEY_MIN", KEY_MIN); |
| 4942 | SetDictInt("KEY_MAX", KEY_MAX); |
| 4943 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 4944 | |
Dong-hee Na | 37fcbb6 | 2020-03-25 07:08:51 +0900 | [diff] [blame] | 4945 | if (PyModule_AddType(m, &PyCursesWindow_Type) < 0) { |
| 4946 | return NULL; |
| 4947 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 4948 | return m; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 4949 | } |