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