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 | |
Andrew M. Kuchling | 16e65a0 | 2001-10-20 16:05:52 +0000 | [diff] [blame] | 99 | 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" |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 106 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 107 | |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 108 | #ifdef __hpux |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 109 | #define STRICT_SYSV_CURSES |
| 110 | #endif |
| 111 | |
Guido van Rossum | 858ca0f | 2001-04-10 19:53:37 +0000 | [diff] [blame] | 112 | #define CURSES_MODULE |
| 113 | #include "py_curses.h" |
| 114 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 115 | /* These prototypes are in <term.h>, but including this header |
| 116 | #defines many common symbols (such as "lines") which breaks the |
| 117 | curses module in other ways. So the code will just specify |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 118 | explicit prototypes here. */ |
| 119 | extern int setupterm(char *,int,int *); |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 120 | #ifdef __sgi |
| 121 | #include <term.h> |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 122 | #endif |
| 123 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 124 | #ifdef HAVE_LANGINFO_H |
| 125 | #include <langinfo.h> |
| 126 | #endif |
| 127 | |
Martin v. Löwis | 21ee409 | 2002-09-30 16:19:48 +0000 | [diff] [blame] | 128 | #if !defined(HAVE_NCURSES_H) && (defined(sgi) || defined(__sun) || defined(SCO5)) |
Andrew M. Kuchling | bf68026 | 2000-07-25 12:37:41 +0000 | [diff] [blame] | 129 | #define STRICT_SYSV_CURSES /* Don't use ncurses extensions */ |
Andrew M. Kuchling | a1e4b05 | 2000-06-27 21:49:47 +0000 | [diff] [blame] | 130 | typedef chtype attr_t; /* No attr_t type is available */ |
Guido van Rossum | 1266a01 | 1996-02-25 04:50:31 +0000 | [diff] [blame] | 131 | #endif |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 132 | |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 133 | #if defined(_AIX) |
| 134 | #define STRICT_SYSV_CURSES |
| 135 | #endif |
| 136 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 137 | /*[clinic input] |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 138 | module curses |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 139 | class curses.window "PyCursesWindowObject *" "&PyCursesWindow_Type" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 140 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 141 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=88c860abdbb50e0c]*/ |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 142 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 143 | #include "clinic/_cursesmodule.c.h" |
| 144 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 145 | /* Definition of exception curses.error */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 146 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 147 | static PyObject *PyCursesError; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 148 | |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 149 | /* Tells whether setupterm() has been called to initialise terminfo. */ |
| 150 | static int initialised_setupterm = FALSE; |
| 151 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 152 | /* Tells whether initscr() has been called to initialise curses. */ |
Guido van Rossum | 585c6dc | 1995-02-24 13:45:43 +0000 | [diff] [blame] | 153 | static int initialised = FALSE; |
| 154 | |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 155 | /* Tells whether start_color() has been called to initialise color usage. */ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 156 | static int initialisedcolors = FALSE; |
| 157 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 158 | static char *screen_encoding = NULL; |
| 159 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 160 | /* Utility Macros */ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 161 | #define PyCursesSetupTermCalled \ |
| 162 | if (initialised_setupterm != TRUE) { \ |
| 163 | PyErr_SetString(PyCursesError, \ |
| 164 | "must call (at least) setupterm() first"); \ |
| 165 | return 0; } |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 166 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 167 | #define PyCursesInitialised \ |
| 168 | if (initialised != TRUE) { \ |
| 169 | PyErr_SetString(PyCursesError, \ |
| 170 | "must call initscr() first"); \ |
| 171 | return 0; } |
Guido van Rossum | fbea2f3 | 1994-08-31 22:05:27 +0000 | [diff] [blame] | 172 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 173 | #define PyCursesInitialisedColor \ |
| 174 | if (initialisedcolors != TRUE) { \ |
| 175 | PyErr_SetString(PyCursesError, \ |
| 176 | "must call start_color() first"); \ |
| 177 | return 0; } |
Guido van Rossum | fbea2f3 | 1994-08-31 22:05:27 +0000 | [diff] [blame] | 178 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 179 | /* Utility Functions */ |
Guido van Rossum | fbea2f3 | 1994-08-31 22:05:27 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 181 | /* |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 182 | * Check the return code from a curses function and return None |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 183 | * or raise an exception as appropriate. These are exported using the |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 184 | * capsule API. |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 185 | */ |
| 186 | |
| 187 | static PyObject * |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 188 | PyCursesCheckERR(int code, const char *fname) |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 189 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 190 | if (code != ERR) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 191 | Py_RETURN_NONE; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 192 | } else { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 193 | if (fname == NULL) { |
| 194 | PyErr_SetString(PyCursesError, catchall_ERR); |
| 195 | } else { |
| 196 | PyErr_Format(PyCursesError, "%s() returned ERR", fname); |
| 197 | } |
| 198 | return NULL; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 199 | } |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 202 | /* Convert an object to a byte (an integer of type chtype): |
| 203 | |
| 204 | - int |
| 205 | - bytes of length 1 |
| 206 | - str of length 1 |
| 207 | |
| 208 | Return 1 on success, 0 on error (invalid type or integer overflow). */ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 209 | static int |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 210 | PyCurses_ConvertToChtype(PyCursesWindowObject *win, PyObject *obj, chtype *ch) |
Guido van Rossum | 585c6dc | 1995-02-24 13:45:43 +0000 | [diff] [blame] | 211 | { |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 212 | long value; |
| 213 | if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) { |
| 214 | value = (unsigned char)PyBytes_AsString(obj)[0]; |
| 215 | } |
| 216 | else if (PyUnicode_Check(obj)) { |
| 217 | if (PyUnicode_GetLength(obj) != 1) { |
| 218 | PyErr_Format(PyExc_TypeError, |
| 219 | "expect bytes or str of length 1, or int, " |
| 220 | "got a str of length %zi", |
| 221 | PyUnicode_GET_LENGTH(obj)); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 222 | return 0; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 223 | } |
| 224 | value = PyUnicode_READ_CHAR(obj, 0); |
| 225 | if (128 < value) { |
| 226 | PyObject *bytes; |
| 227 | const char *encoding; |
| 228 | if (win) |
| 229 | encoding = win->encoding; |
| 230 | else |
| 231 | encoding = screen_encoding; |
Serhiy Storchaka | b29cee4 | 2016-10-27 19:31:49 +0300 | [diff] [blame] | 232 | bytes = PyUnicode_AsEncodedString(obj, encoding, NULL); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 233 | if (bytes == NULL) |
| 234 | return 0; |
| 235 | if (PyBytes_GET_SIZE(bytes) == 1) |
| 236 | value = (unsigned char)PyBytes_AS_STRING(bytes)[0]; |
| 237 | else |
| 238 | value = -1; |
| 239 | Py_DECREF(bytes); |
| 240 | if (value < 0) |
| 241 | goto overflow; |
| 242 | } |
| 243 | } |
| 244 | else if (PyLong_CheckExact(obj)) { |
| 245 | int long_overflow; |
| 246 | value = PyLong_AsLongAndOverflow(obj, &long_overflow); |
| 247 | if (long_overflow) |
| 248 | goto overflow; |
| 249 | } |
| 250 | else { |
| 251 | PyErr_Format(PyExc_TypeError, |
| 252 | "expect bytes or str of length 1, or int, got %s", |
| 253 | Py_TYPE(obj)->tp_name); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 254 | return 0; |
| 255 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 256 | *ch = (chtype)value; |
| 257 | if ((long)*ch != value) |
| 258 | goto overflow; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 259 | return 1; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 260 | |
| 261 | overflow: |
| 262 | PyErr_SetString(PyExc_OverflowError, |
| 263 | "byte doesn't fit in chtype"); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | /* Convert an object to a byte (chtype) or a character (cchar_t): |
| 268 | |
| 269 | - int |
| 270 | - bytes of length 1 |
| 271 | - str of length 1 |
| 272 | |
| 273 | Return: |
| 274 | |
| 275 | - 2 if obj is a character (written into *wch) |
| 276 | - 1 if obj is a byte (written into *ch) |
| 277 | - 0 on error: raise an exception */ |
| 278 | static int |
| 279 | PyCurses_ConvertToCchar_t(PyCursesWindowObject *win, PyObject *obj, |
| 280 | chtype *ch |
| 281 | #ifdef HAVE_NCURSESW |
Serhiy Storchaka | 0bcd89b | 2016-10-30 22:52:06 +0200 | [diff] [blame] | 282 | , wchar_t *wch |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 283 | #endif |
| 284 | ) |
| 285 | { |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 286 | long value; |
| 287 | #ifdef HAVE_NCURSESW |
| 288 | wchar_t buffer[2]; |
| 289 | #endif |
| 290 | |
| 291 | if (PyUnicode_Check(obj)) { |
| 292 | #ifdef HAVE_NCURSESW |
| 293 | if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) { |
| 294 | PyErr_Format(PyExc_TypeError, |
| 295 | "expect bytes or str of length 1, or int, " |
| 296 | "got a str of length %zi", |
| 297 | PyUnicode_GET_LENGTH(obj)); |
| 298 | return 0; |
| 299 | } |
Serhiy Storchaka | 0bcd89b | 2016-10-30 22:52:06 +0200 | [diff] [blame] | 300 | *wch = buffer[0]; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 301 | return 2; |
| 302 | #else |
| 303 | return PyCurses_ConvertToChtype(win, obj, ch); |
| 304 | #endif |
| 305 | } |
| 306 | else if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) { |
| 307 | value = (unsigned char)PyBytes_AsString(obj)[0]; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 308 | } |
| 309 | else if (PyLong_CheckExact(obj)) { |
| 310 | int overflow; |
| 311 | value = PyLong_AsLongAndOverflow(obj, &overflow); |
| 312 | if (overflow) { |
| 313 | PyErr_SetString(PyExc_OverflowError, |
| 314 | "int doesn't fit in long"); |
| 315 | return 0; |
| 316 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 317 | } |
| 318 | else { |
| 319 | PyErr_Format(PyExc_TypeError, |
| 320 | "expect bytes or str of length 1, or int, got %s", |
| 321 | Py_TYPE(obj)->tp_name); |
| 322 | return 0; |
| 323 | } |
Victor Stinner | 9f16e44 | 2012-09-01 15:00:34 +0200 | [diff] [blame] | 324 | |
| 325 | *ch = (chtype)value; |
| 326 | if ((long)*ch != value) { |
| 327 | PyErr_Format(PyExc_OverflowError, |
| 328 | "byte doesn't fit in chtype"); |
| 329 | return 0; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 330 | } |
Victor Stinner | 9f16e44 | 2012-09-01 15:00:34 +0200 | [diff] [blame] | 331 | return 1; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /* Convert an object to a byte string (char*) or a wide character string |
| 335 | (wchar_t*). Return: |
| 336 | |
| 337 | - 2 if obj is a character string (written into *wch) |
| 338 | - 1 if obj is a byte string (written into *bytes) |
| 339 | - 0 on error: raise an exception */ |
| 340 | static int |
| 341 | PyCurses_ConvertToString(PyCursesWindowObject *win, PyObject *obj, |
| 342 | PyObject **bytes, wchar_t **wstr) |
| 343 | { |
| 344 | if (PyUnicode_Check(obj)) { |
| 345 | #ifdef HAVE_NCURSESW |
| 346 | assert (wstr != NULL); |
| 347 | *wstr = PyUnicode_AsWideCharString(obj, NULL); |
| 348 | if (*wstr == NULL) |
| 349 | return 0; |
| 350 | return 2; |
| 351 | #else |
| 352 | assert (wstr == NULL); |
Serhiy Storchaka | b29cee4 | 2016-10-27 19:31:49 +0300 | [diff] [blame] | 353 | *bytes = PyUnicode_AsEncodedString(obj, win->encoding, NULL); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 354 | if (*bytes == NULL) |
| 355 | return 0; |
| 356 | return 1; |
| 357 | #endif |
| 358 | } |
| 359 | else if (PyBytes_Check(obj)) { |
| 360 | Py_INCREF(obj); |
| 361 | *bytes = obj; |
| 362 | return 1; |
| 363 | } |
| 364 | |
| 365 | PyErr_Format(PyExc_TypeError, "expect bytes or str, got %s", |
| 366 | Py_TYPE(obj)->tp_name); |
| 367 | return 0; |
Guido van Rossum | 585c6dc | 1995-02-24 13:45:43 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Benjamin Peterson | 21896a3 | 2010-03-21 22:03:03 +0000 | [diff] [blame] | 370 | /* 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] | 371 | initialised or not. */ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 372 | |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 373 | static int func_PyCursesSetupTermCalled(void) |
| 374 | { |
| 375 | PyCursesSetupTermCalled; |
| 376 | return 1; |
| 377 | } |
| 378 | |
| 379 | static int func_PyCursesInitialised(void) |
| 380 | { |
| 381 | PyCursesInitialised; |
| 382 | return 1; |
| 383 | } |
| 384 | |
| 385 | static int func_PyCursesInitialisedColor(void) |
| 386 | { |
| 387 | PyCursesInitialisedColor; |
| 388 | return 1; |
| 389 | } |
| 390 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 391 | /***************************************************************************** |
| 392 | The Window Object |
| 393 | ******************************************************************************/ |
Guido van Rossum | 585c6dc | 1995-02-24 13:45:43 +0000 | [diff] [blame] | 394 | |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 395 | /* Definition of the window type */ |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 396 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 397 | PyTypeObject PyCursesWindow_Type; |
| 398 | |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 399 | /* Function prototype macros for Window object |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 400 | |
| 401 | X - function name |
| 402 | TYPE - parameter Type |
| 403 | ERGSTR - format string for construction of the return value |
| 404 | PARSESTR - format string for argument parsing |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 405 | */ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 406 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 407 | #define Window_NoArgNoReturnFunction(X) \ |
| 408 | static PyObject *PyCursesWindow_ ## X \ |
| 409 | (PyCursesWindowObject *self, PyObject *args) \ |
| 410 | { return PyCursesCheckERR(X(self->win), # X); } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 411 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 412 | #define Window_NoArgTrueFalseFunction(X) \ |
| 413 | static PyObject * PyCursesWindow_ ## X \ |
| 414 | (PyCursesWindowObject *self) \ |
| 415 | { \ |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 416 | if (X (self->win) == FALSE) { Py_RETURN_FALSE; } \ |
| 417 | else { Py_RETURN_TRUE; } } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 418 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 419 | #define Window_NoArgNoReturnVoidFunction(X) \ |
| 420 | static PyObject * PyCursesWindow_ ## X \ |
| 421 | (PyCursesWindowObject *self) \ |
| 422 | { \ |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 423 | X(self->win); Py_RETURN_NONE; } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 424 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 425 | #define Window_NoArg2TupleReturnFunction(X, TYPE, ERGSTR) \ |
| 426 | static PyObject * PyCursesWindow_ ## X \ |
| 427 | (PyCursesWindowObject *self) \ |
| 428 | { \ |
| 429 | TYPE arg1, arg2; \ |
| 430 | X(self->win,arg1,arg2); return Py_BuildValue(ERGSTR, arg1, arg2); } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 431 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 432 | #define Window_OneArgNoReturnVoidFunction(X, TYPE, PARSESTR) \ |
| 433 | static PyObject * PyCursesWindow_ ## X \ |
| 434 | (PyCursesWindowObject *self, PyObject *args) \ |
| 435 | { \ |
| 436 | TYPE arg1; \ |
| 437 | if (!PyArg_ParseTuple(args, PARSESTR, &arg1)) return NULL; \ |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 438 | X(self->win,arg1); Py_RETURN_NONE; } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 439 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 440 | #define Window_OneArgNoReturnFunction(X, TYPE, PARSESTR) \ |
| 441 | static PyObject * PyCursesWindow_ ## X \ |
| 442 | (PyCursesWindowObject *self, PyObject *args) \ |
| 443 | { \ |
| 444 | TYPE arg1; \ |
| 445 | if (!PyArg_ParseTuple(args,PARSESTR, &arg1)) return NULL; \ |
| 446 | return PyCursesCheckERR(X(self->win, arg1), # X); } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 447 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 448 | #define Window_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \ |
| 449 | static PyObject * PyCursesWindow_ ## X \ |
| 450 | (PyCursesWindowObject *self, PyObject *args) \ |
| 451 | { \ |
| 452 | TYPE arg1, arg2; \ |
| 453 | if (!PyArg_ParseTuple(args,PARSESTR, &arg1, &arg2)) return NULL; \ |
| 454 | return PyCursesCheckERR(X(self->win, arg1, arg2), # X); } |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 455 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 456 | /* ------------- WINDOW routines --------------- */ |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 457 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 458 | Window_NoArgNoReturnFunction(untouchwin) |
| 459 | Window_NoArgNoReturnFunction(touchwin) |
| 460 | Window_NoArgNoReturnFunction(redrawwin) |
| 461 | Window_NoArgNoReturnFunction(winsertln) |
| 462 | Window_NoArgNoReturnFunction(werase) |
| 463 | Window_NoArgNoReturnFunction(wdeleteln) |
| 464 | |
| 465 | Window_NoArgTrueFalseFunction(is_wintouched) |
| 466 | |
| 467 | Window_NoArgNoReturnVoidFunction(wsyncup) |
| 468 | Window_NoArgNoReturnVoidFunction(wsyncdown) |
| 469 | Window_NoArgNoReturnVoidFunction(wstandend) |
| 470 | Window_NoArgNoReturnVoidFunction(wstandout) |
| 471 | Window_NoArgNoReturnVoidFunction(wcursyncup) |
| 472 | Window_NoArgNoReturnVoidFunction(wclrtoeol) |
| 473 | Window_NoArgNoReturnVoidFunction(wclrtobot) |
| 474 | Window_NoArgNoReturnVoidFunction(wclear) |
| 475 | |
| 476 | Window_OneArgNoReturnVoidFunction(idcok, int, "i;True(1) or False(0)") |
| 477 | Window_OneArgNoReturnVoidFunction(immedok, int, "i;True(1) or False(0)") |
Andrew M. Kuchling | 97311bb | 2000-06-21 01:41:48 +0000 | [diff] [blame] | 478 | Window_OneArgNoReturnVoidFunction(wtimeout, int, "i;delay") |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 479 | |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 480 | Window_NoArg2TupleReturnFunction(getyx, int, "ii") |
| 481 | Window_NoArg2TupleReturnFunction(getbegyx, int, "ii") |
| 482 | Window_NoArg2TupleReturnFunction(getmaxyx, int, "ii") |
| 483 | Window_NoArg2TupleReturnFunction(getparyx, int, "ii") |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 484 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 485 | Window_OneArgNoReturnFunction(clearok, int, "i;True(1) or False(0)") |
| 486 | Window_OneArgNoReturnFunction(idlok, int, "i;True(1) or False(0)") |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 487 | #if defined(__NetBSD__) |
| 488 | Window_OneArgNoReturnVoidFunction(keypad, int, "i;True(1) or False(0)") |
| 489 | #else |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 490 | Window_OneArgNoReturnFunction(keypad, int, "i;True(1) or False(0)") |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 491 | #endif |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 492 | Window_OneArgNoReturnFunction(leaveok, int, "i;True(1) or False(0)") |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 493 | #if defined(__NetBSD__) |
| 494 | Window_OneArgNoReturnVoidFunction(nodelay, int, "i;True(1) or False(0)") |
| 495 | #else |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 496 | Window_OneArgNoReturnFunction(nodelay, int, "i;True(1) or False(0)") |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 497 | #endif |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 498 | Window_OneArgNoReturnFunction(notimeout, int, "i;True(1) or False(0)") |
| 499 | Window_OneArgNoReturnFunction(scrollok, int, "i;True(1) or False(0)") |
Andrew M. Kuchling | 03e644b | 2000-05-23 16:24:54 +0000 | [diff] [blame] | 500 | Window_OneArgNoReturnFunction(winsdelln, int, "i;nlines") |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 501 | Window_OneArgNoReturnFunction(syncok, int, "i;True(1) or False(0)") |
| 502 | |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 503 | Window_TwoArgNoReturnFunction(mvwin, int, "ii;y,x") |
| 504 | Window_TwoArgNoReturnFunction(mvderwin, int, "ii;y,x") |
| 505 | Window_TwoArgNoReturnFunction(wmove, int, "ii;y,x") |
Andrew M. Kuchling | a1e4b05 | 2000-06-27 21:49:47 +0000 | [diff] [blame] | 506 | #ifndef STRICT_SYSV_CURSES |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 507 | Window_TwoArgNoReturnFunction(wresize, int, "ii;lines,columns") |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 508 | #endif |
| 509 | |
Andrew M. Kuchling | 03e644b | 2000-05-23 16:24:54 +0000 | [diff] [blame] | 510 | /* Allocation and deallocation of Window Objects */ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 511 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 512 | static PyObject * |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 513 | PyCursesWindow_New(WINDOW *win, const char *encoding) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 514 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 515 | PyCursesWindowObject *wo; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 516 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 517 | if (encoding == NULL) { |
| 518 | #if defined(MS_WINDOWS) |
| 519 | char *buffer[100]; |
| 520 | UINT cp; |
| 521 | cp = GetConsoleOutputCP(); |
| 522 | if (cp != 0) { |
| 523 | PyOS_snprintf(buffer, sizeof(buffer), "cp%u", cp); |
| 524 | encoding = buffer; |
| 525 | } |
| 526 | #elif defined(CODESET) |
| 527 | const char *codeset = nl_langinfo(CODESET); |
| 528 | if (codeset != NULL && codeset[0] != 0) |
| 529 | encoding = codeset; |
| 530 | #endif |
| 531 | if (encoding == NULL) |
| 532 | encoding = "utf-8"; |
| 533 | } |
| 534 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 535 | wo = PyObject_NEW(PyCursesWindowObject, &PyCursesWindow_Type); |
| 536 | if (wo == NULL) return NULL; |
| 537 | wo->win = win; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 538 | wo->encoding = _PyMem_Strdup(encoding); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 539 | if (wo->encoding == NULL) { |
| 540 | Py_DECREF(wo); |
| 541 | PyErr_NoMemory(); |
| 542 | return NULL; |
| 543 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 544 | return (PyObject *)wo; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static void |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 548 | PyCursesWindow_Dealloc(PyCursesWindowObject *wo) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 549 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 550 | if (wo->win != stdscr) delwin(wo->win); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 551 | if (wo->encoding != NULL) |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 552 | PyMem_Free(wo->encoding); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 553 | PyObject_DEL(wo); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 556 | /* Addch, Addstr, Addnstr */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 557 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 558 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 559 | |
| 560 | curses.window.addch |
| 561 | |
| 562 | [ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 563 | y: int |
| 564 | Y-coordinate. |
Larry Hastings | 9147a96 | 2014-05-04 04:41:18 -0700 | [diff] [blame] | 565 | x: int |
| 566 | X-coordinate. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 567 | ] |
| 568 | |
| 569 | ch: object |
| 570 | Character to add. |
| 571 | |
| 572 | [ |
| 573 | attr: long |
| 574 | Attributes for the character. |
| 575 | ] |
| 576 | / |
| 577 | |
| 578 | Paint character ch at (y, x) with attributes attr. |
| 579 | |
| 580 | Paint character ch at (y, x) with attributes attr, |
| 581 | overwriting any character previously painted at that location. |
| 582 | By default, the character position and attributes are the |
| 583 | current settings for the window object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 584 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 585 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 586 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 587 | curses_window_addch_impl(PyCursesWindowObject *self, int group_left_1, int y, |
| 588 | int x, PyObject *ch, int group_right_1, long attr) |
| 589 | /*[clinic end generated code: output=99f7f85078ec06c3 input=5a41efb34a2de338]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 590 | { |
| 591 | PyCursesWindowObject *cwself = (PyCursesWindowObject *)self; |
| 592 | int coordinates_group = group_left_1; |
| 593 | int attr_group = group_right_1; |
| 594 | int rtn; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 595 | int type; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 596 | chtype cch = 0; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 597 | #ifdef HAVE_NCURSESW |
Serhiy Storchaka | 0bcd89b | 2016-10-30 22:52:06 +0200 | [diff] [blame] | 598 | wchar_t wstr[2]; |
| 599 | cchar_t wcval; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 600 | #endif |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 601 | const char *funcname; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 602 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 603 | if (!attr_group) |
| 604 | attr = A_NORMAL; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 605 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 606 | #ifdef HAVE_NCURSESW |
Serhiy Storchaka | 0bcd89b | 2016-10-30 22:52:06 +0200 | [diff] [blame] | 607 | type = PyCurses_ConvertToCchar_t(cwself, ch, &cch, wstr); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 608 | if (type == 2) { |
| 609 | funcname = "add_wch"; |
Serhiy Storchaka | 0bcd89b | 2016-10-30 22:52:06 +0200 | [diff] [blame] | 610 | wstr[1] = L'\0'; |
| 611 | setcchar(&wcval, wstr, attr, 0, NULL); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 612 | if (coordinates_group) |
Serhiy Storchaka | 0bcd89b | 2016-10-30 22:52:06 +0200 | [diff] [blame] | 613 | rtn = mvwadd_wch(cwself->win,y,x, &wcval); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 614 | else { |
Serhiy Storchaka | 0bcd89b | 2016-10-30 22:52:06 +0200 | [diff] [blame] | 615 | rtn = wadd_wch(cwself->win, &wcval); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | else |
| 619 | #else |
Serhiy Storchaka | a412f76 | 2013-10-19 10:45:48 +0300 | [diff] [blame] | 620 | type = PyCurses_ConvertToCchar_t(cwself, ch, &cch); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 621 | #endif |
| 622 | if (type == 1) { |
| 623 | funcname = "addch"; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 624 | if (coordinates_group) |
| 625 | rtn = mvwaddch(cwself->win,y,x, cch | attr); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 626 | else { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 627 | rtn = waddch(cwself->win, cch | attr); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | else { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 631 | return NULL; |
| 632 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 633 | return PyCursesCheckERR(rtn, funcname); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 637 | PyCursesWindow_AddStr(PyCursesWindowObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 638 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 639 | int rtn; |
| 640 | int x, y; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 641 | int strtype; |
Benjamin Peterson | 5e54519 | 2012-03-14 18:21:35 -0500 | [diff] [blame] | 642 | PyObject *strobj, *bytesobj = NULL; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 643 | #ifdef HAVE_NCURSESW |
| 644 | wchar_t *wstr = NULL; |
| 645 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 646 | attr_t attr = A_NORMAL , attr_old = A_NORMAL; |
| 647 | long lattr; |
| 648 | int use_xy = FALSE, use_attr = FALSE; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 649 | const char *funcname; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 650 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 651 | switch (PyTuple_Size(args)) { |
| 652 | case 1: |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 653 | if (!PyArg_ParseTuple(args,"O;str", &strobj)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 654 | return NULL; |
| 655 | break; |
| 656 | case 2: |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 657 | if (!PyArg_ParseTuple(args,"Ol;str,attr", &strobj, &lattr)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 658 | return NULL; |
| 659 | attr = lattr; |
| 660 | use_attr = TRUE; |
| 661 | break; |
| 662 | case 3: |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 663 | if (!PyArg_ParseTuple(args,"iiO;int,int,str", &y, &x, &strobj)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 664 | return NULL; |
| 665 | use_xy = TRUE; |
| 666 | break; |
| 667 | case 4: |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 668 | if (!PyArg_ParseTuple(args,"iiOl;int,int,str,attr", &y, &x, &strobj, &lattr)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 669 | return NULL; |
| 670 | attr = lattr; |
| 671 | use_xy = use_attr = TRUE; |
| 672 | break; |
| 673 | default: |
| 674 | PyErr_SetString(PyExc_TypeError, "addstr requires 1 to 4 arguments"); |
| 675 | return NULL; |
| 676 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 677 | #ifdef HAVE_NCURSESW |
| 678 | strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, &wstr); |
| 679 | #else |
| 680 | strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, NULL); |
| 681 | #endif |
| 682 | if (strtype == 0) |
| 683 | return NULL; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 684 | if (use_attr == TRUE) { |
| 685 | attr_old = getattrs(self->win); |
| 686 | (void)wattrset(self->win,attr); |
| 687 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 688 | #ifdef HAVE_NCURSESW |
| 689 | if (strtype == 2) { |
| 690 | funcname = "addwstr"; |
| 691 | if (use_xy == TRUE) |
| 692 | rtn = mvwaddwstr(self->win,y,x,wstr); |
| 693 | else |
| 694 | rtn = waddwstr(self->win,wstr); |
| 695 | PyMem_Free(wstr); |
| 696 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 697 | else |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 698 | #endif |
| 699 | { |
| 700 | char *str = PyBytes_AS_STRING(bytesobj); |
| 701 | funcname = "addstr"; |
| 702 | if (use_xy == TRUE) |
| 703 | rtn = mvwaddstr(self->win,y,x,str); |
| 704 | else |
| 705 | rtn = waddstr(self->win,str); |
| 706 | Py_DECREF(bytesobj); |
| 707 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 708 | if (use_attr == TRUE) |
| 709 | (void)wattrset(self->win,attr_old); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 710 | return PyCursesCheckERR(rtn, funcname); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 711 | } |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 712 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 713 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 714 | PyCursesWindow_AddNStr(PyCursesWindowObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 715 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 716 | int rtn, x, y, n; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 717 | int strtype; |
Benjamin Peterson | 5e54519 | 2012-03-14 18:21:35 -0500 | [diff] [blame] | 718 | PyObject *strobj, *bytesobj = NULL; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 719 | #ifdef HAVE_NCURSESW |
| 720 | wchar_t *wstr = NULL; |
| 721 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 722 | attr_t attr = A_NORMAL , attr_old = A_NORMAL; |
| 723 | long lattr; |
| 724 | int use_xy = FALSE, use_attr = FALSE; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 725 | const char *funcname; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 726 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 727 | switch (PyTuple_Size(args)) { |
| 728 | case 2: |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 729 | if (!PyArg_ParseTuple(args,"Oi;str,n", &strobj, &n)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 730 | return NULL; |
| 731 | break; |
| 732 | case 3: |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 733 | if (!PyArg_ParseTuple(args,"Oil;str,n,attr", &strobj, &n, &lattr)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 734 | return NULL; |
| 735 | attr = lattr; |
| 736 | use_attr = TRUE; |
| 737 | break; |
| 738 | case 4: |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 739 | if (!PyArg_ParseTuple(args,"iiOi;y,x,str,n", &y, &x, &strobj, &n)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 740 | return NULL; |
| 741 | use_xy = TRUE; |
| 742 | break; |
| 743 | case 5: |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 744 | if (!PyArg_ParseTuple(args,"iiOil;y,x,str,n,attr", &y, &x, &strobj, &n, &lattr)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 745 | return NULL; |
| 746 | attr = lattr; |
| 747 | use_xy = use_attr = TRUE; |
| 748 | break; |
| 749 | default: |
| 750 | PyErr_SetString(PyExc_TypeError, "addnstr requires 2 to 5 arguments"); |
| 751 | return NULL; |
| 752 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 753 | #ifdef HAVE_NCURSESW |
| 754 | strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, &wstr); |
| 755 | #else |
| 756 | strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, NULL); |
| 757 | #endif |
| 758 | if (strtype == 0) |
| 759 | return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 760 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 761 | if (use_attr == TRUE) { |
| 762 | attr_old = getattrs(self->win); |
| 763 | (void)wattrset(self->win,attr); |
| 764 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 765 | #ifdef HAVE_NCURSESW |
| 766 | if (strtype == 2) { |
| 767 | funcname = "addnwstr"; |
| 768 | if (use_xy == TRUE) |
| 769 | rtn = mvwaddnwstr(self->win,y,x,wstr,n); |
| 770 | else |
| 771 | rtn = waddnwstr(self->win,wstr,n); |
| 772 | PyMem_Free(wstr); |
| 773 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 774 | else |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 775 | #endif |
| 776 | { |
| 777 | char *str = PyBytes_AS_STRING(bytesobj); |
| 778 | funcname = "addnstr"; |
| 779 | if (use_xy == TRUE) |
| 780 | rtn = mvwaddnstr(self->win,y,x,str,n); |
| 781 | else |
| 782 | rtn = waddnstr(self->win,str,n); |
| 783 | Py_DECREF(bytesobj); |
| 784 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 785 | if (use_attr == TRUE) |
| 786 | (void)wattrset(self->win,attr_old); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 787 | return PyCursesCheckERR(rtn, funcname); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 791 | PyCursesWindow_Bkgd(PyCursesWindowObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 792 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 793 | PyObject *temp; |
| 794 | chtype bkgd; |
| 795 | attr_t attr = A_NORMAL; |
| 796 | long lattr; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 797 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 798 | switch (PyTuple_Size(args)) { |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 799 | case 1: |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 800 | if (!PyArg_ParseTuple(args, "O;ch or int", &temp)) |
| 801 | return NULL; |
| 802 | break; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 803 | case 2: |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 804 | if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr)) |
| 805 | return NULL; |
| 806 | attr = lattr; |
| 807 | break; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 808 | default: |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 809 | PyErr_SetString(PyExc_TypeError, "bkgd requires 1 or 2 arguments"); |
| 810 | return NULL; |
| 811 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 812 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 813 | if (!PyCurses_ConvertToChtype(self, temp, &bkgd)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 814 | return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 815 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 816 | return PyCursesCheckERR(wbkgd(self->win, bkgd | attr), "bkgd"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | static PyObject * |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 820 | PyCursesWindow_AttrOff(PyCursesWindowObject *self, PyObject *args) |
| 821 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 822 | long lattr; |
| 823 | if (!PyArg_ParseTuple(args,"l;attr", &lattr)) |
| 824 | return NULL; |
| 825 | return PyCursesCheckERR(wattroff(self->win, (attr_t)lattr), "attroff"); |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | static PyObject * |
| 829 | PyCursesWindow_AttrOn(PyCursesWindowObject *self, PyObject *args) |
| 830 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 831 | long lattr; |
| 832 | if (!PyArg_ParseTuple(args,"l;attr", &lattr)) |
| 833 | return NULL; |
| 834 | return PyCursesCheckERR(wattron(self->win, (attr_t)lattr), "attron"); |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | static PyObject * |
| 838 | PyCursesWindow_AttrSet(PyCursesWindowObject *self, PyObject *args) |
| 839 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 840 | long lattr; |
| 841 | if (!PyArg_ParseTuple(args,"l;attr", &lattr)) |
| 842 | return NULL; |
| 843 | return PyCursesCheckERR(wattrset(self->win, (attr_t)lattr), "attrset"); |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 847 | PyCursesWindow_BkgdSet(PyCursesWindowObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 848 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 849 | PyObject *temp; |
| 850 | chtype bkgd; |
| 851 | attr_t attr = A_NORMAL; |
| 852 | long lattr; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 853 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 854 | switch (PyTuple_Size(args)) { |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 855 | case 1: |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 856 | if (!PyArg_ParseTuple(args, "O;ch or int", &temp)) |
| 857 | return NULL; |
| 858 | break; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 859 | case 2: |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 860 | if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr)) |
| 861 | return NULL; |
| 862 | attr = lattr; |
| 863 | break; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 864 | default: |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 865 | PyErr_SetString(PyExc_TypeError, "bkgdset requires 1 or 2 arguments"); |
| 866 | return NULL; |
| 867 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 868 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 869 | if (!PyCurses_ConvertToChtype(self, temp, &bkgd)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 870 | return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 871 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 872 | wbkgdset(self->win, bkgd | attr); |
| 873 | return PyCursesCheckERR(0, "bkgdset"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 877 | PyCursesWindow_Border(PyCursesWindowObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 878 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 879 | PyObject *temp[8]; |
| 880 | chtype ch[8]; |
| 881 | int i; |
Andrew M. Kuchling | 16e65a0 | 2001-10-20 16:05:52 +0000 | [diff] [blame] | 882 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 883 | /* Clear the array of parameters */ |
| 884 | for(i=0; i<8; i++) { |
| 885 | temp[i] = NULL; |
| 886 | ch[i] = 0; |
| 887 | } |
Andrew M. Kuchling | 16e65a0 | 2001-10-20 16:05:52 +0000 | [diff] [blame] | 888 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 889 | if (!PyArg_ParseTuple(args,"|OOOOOOOO;ls,rs,ts,bs,tl,tr,bl,br", |
| 890 | &temp[0], &temp[1], &temp[2], &temp[3], |
| 891 | &temp[4], &temp[5], &temp[6], &temp[7])) |
| 892 | return NULL; |
| 893 | |
| 894 | for(i=0; i<8; i++) { |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 895 | if (temp[i] != NULL && !PyCurses_ConvertToChtype(self, temp[i], &ch[i])) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 896 | return NULL; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | wborder(self->win, |
| 900 | ch[0], ch[1], ch[2], ch[3], |
| 901 | ch[4], ch[5], ch[6], ch[7]); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 902 | Py_RETURN_NONE; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 906 | PyCursesWindow_Box(PyCursesWindowObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 907 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 908 | chtype ch1=0,ch2=0; |
| 909 | switch(PyTuple_Size(args)){ |
| 910 | case 0: break; |
| 911 | default: |
| 912 | if (!PyArg_ParseTuple(args,"ll;vertint,horint", &ch1, &ch2)) |
| 913 | return NULL; |
| 914 | } |
| 915 | box(self->win,ch1,ch2); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 916 | Py_RETURN_NONE; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 919 | #if defined(HAVE_NCURSES_H) || defined(MVWDELCH_IS_EXPRESSION) |
| 920 | #define py_mvwdelch mvwdelch |
| 921 | #else |
| 922 | int py_mvwdelch(WINDOW *w, int y, int x) |
| 923 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 924 | mvwdelch(w,y,x); |
| 925 | /* On HP/UX, mvwdelch already returns. On other systems, |
| 926 | we may well run into this return statement. */ |
| 927 | return 0; |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 928 | } |
| 929 | #endif |
| 930 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 931 | /* chgat, added by Fabian Kreutz <fabian.kreutz at gmx.net> */ |
| 932 | |
| 933 | static PyObject * |
| 934 | PyCursesWindow_ChgAt(PyCursesWindowObject *self, PyObject *args) |
| 935 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 936 | int rtn; |
| 937 | int x, y; |
| 938 | int num = -1; |
| 939 | short color; |
| 940 | attr_t attr = A_NORMAL; |
| 941 | long lattr; |
| 942 | int use_xy = FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 943 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 944 | switch (PyTuple_Size(args)) { |
| 945 | case 1: |
| 946 | if (!PyArg_ParseTuple(args,"l;attr", &lattr)) |
| 947 | return NULL; |
| 948 | attr = lattr; |
| 949 | break; |
| 950 | case 2: |
| 951 | if (!PyArg_ParseTuple(args,"il;n,attr", &num, &lattr)) |
| 952 | return NULL; |
| 953 | attr = lattr; |
| 954 | break; |
| 955 | case 3: |
| 956 | if (!PyArg_ParseTuple(args,"iil;int,int,attr", &y, &x, &lattr)) |
| 957 | return NULL; |
| 958 | attr = lattr; |
| 959 | use_xy = TRUE; |
| 960 | break; |
| 961 | case 4: |
| 962 | if (!PyArg_ParseTuple(args,"iiil;int,int,n,attr", &y, &x, &num, &lattr)) |
| 963 | return NULL; |
| 964 | attr = lattr; |
| 965 | use_xy = TRUE; |
| 966 | break; |
| 967 | default: |
| 968 | PyErr_SetString(PyExc_TypeError, "chgat requires 1 to 4 arguments"); |
| 969 | return NULL; |
| 970 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 971 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 972 | color = (short)((attr >> 8) & 0xff); |
| 973 | attr = attr - (color << 8); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 974 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 975 | if (use_xy == TRUE) { |
| 976 | rtn = mvwchgat(self->win,y,x,num,attr,color,NULL); |
| 977 | touchline(self->win,y,1); |
| 978 | } else { |
| 979 | getyx(self->win,y,x); |
| 980 | rtn = wchgat(self->win,num,attr,color,NULL); |
| 981 | touchline(self->win,y,1); |
| 982 | } |
| 983 | return PyCursesCheckERR(rtn, "chgat"); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 986 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 987 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 988 | PyCursesWindow_DelCh(PyCursesWindowObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 989 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 990 | int rtn; |
| 991 | int x, y; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 992 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 993 | switch (PyTuple_Size(args)) { |
| 994 | case 0: |
| 995 | rtn = wdelch(self->win); |
| 996 | break; |
| 997 | case 2: |
| 998 | if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x)) |
| 999 | return NULL; |
| 1000 | rtn = py_mvwdelch(self->win,y,x); |
| 1001 | break; |
| 1002 | default: |
| 1003 | PyErr_SetString(PyExc_TypeError, "delch requires 0 or 2 arguments"); |
| 1004 | return NULL; |
| 1005 | } |
| 1006 | return PyCursesCheckERR(rtn, "[mv]wdelch"); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1010 | PyCursesWindow_DerWin(PyCursesWindowObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1011 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1012 | WINDOW *win; |
| 1013 | int nlines, ncols, begin_y, begin_x; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1014 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1015 | nlines = 0; |
| 1016 | ncols = 0; |
| 1017 | switch (PyTuple_Size(args)) { |
| 1018 | case 2: |
| 1019 | if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x)) |
| 1020 | return NULL; |
| 1021 | break; |
| 1022 | case 4: |
| 1023 | if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x", |
| 1024 | &nlines,&ncols,&begin_y,&begin_x)) |
| 1025 | return NULL; |
| 1026 | break; |
| 1027 | default: |
| 1028 | PyErr_SetString(PyExc_TypeError, "derwin requires 2 or 4 arguments"); |
| 1029 | return NULL; |
| 1030 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1031 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1032 | win = derwin(self->win,nlines,ncols,begin_y,begin_x); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1033 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1034 | if (win == NULL) { |
| 1035 | PyErr_SetString(PyCursesError, catchall_NULL); |
| 1036 | return NULL; |
| 1037 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1038 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1039 | return (PyObject *)PyCursesWindow_New(win, NULL); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1043 | PyCursesWindow_EchoChar(PyCursesWindowObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1044 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1045 | PyObject *temp; |
| 1046 | chtype ch; |
| 1047 | attr_t attr = A_NORMAL; |
| 1048 | long lattr; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 1049 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1050 | switch (PyTuple_Size(args)) { |
| 1051 | case 1: |
| 1052 | if (!PyArg_ParseTuple(args,"O;ch or int", &temp)) |
| 1053 | return NULL; |
| 1054 | break; |
| 1055 | case 2: |
| 1056 | if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr)) |
| 1057 | return NULL; |
| 1058 | attr = lattr; |
| 1059 | break; |
| 1060 | default: |
| 1061 | PyErr_SetString(PyExc_TypeError, "echochar requires 1 or 2 arguments"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1062 | |
| 1063 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1064 | return NULL; |
| 1065 | } |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 1066 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1067 | if (!PyCurses_ConvertToChtype(self, temp, &ch)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1068 | return NULL; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1069 | |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 1070 | #ifdef WINDOW_HAS_FLAGS |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1071 | if (self->win->_flags & _ISPAD) |
| 1072 | return PyCursesCheckERR(pechochar(self->win, ch | attr), |
| 1073 | "echochar"); |
| 1074 | else |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 1075 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1076 | return PyCursesCheckERR(wechochar(self->win, ch | attr), |
| 1077 | "echochar"); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 1080 | #ifdef NCURSES_MOUSE_VERSION |
| 1081 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1082 | PyCursesWindow_Enclose(PyCursesWindowObject *self, PyObject *args) |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 1083 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1084 | int x, y; |
| 1085 | if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x)) |
| 1086 | return NULL; |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 1087 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1088 | return PyLong_FromLong( wenclose(self->win,y,x) ); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 1089 | } |
| 1090 | #endif |
| 1091 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1092 | static PyObject * |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 1093 | PyCursesWindow_GetBkgd(PyCursesWindowObject *self) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1094 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1095 | return PyLong_FromLong((long) getbkgd(self->win)); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1099 | PyCursesWindow_GetCh(PyCursesWindowObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1100 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1101 | int x, y; |
| 1102 | int rtn; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 1103 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1104 | switch (PyTuple_Size(args)) { |
| 1105 | case 0: |
| 1106 | Py_BEGIN_ALLOW_THREADS |
| 1107 | rtn = wgetch(self->win); |
| 1108 | Py_END_ALLOW_THREADS |
| 1109 | break; |
| 1110 | case 2: |
| 1111 | if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x)) |
| 1112 | return NULL; |
| 1113 | Py_BEGIN_ALLOW_THREADS |
| 1114 | rtn = mvwgetch(self->win,y,x); |
| 1115 | Py_END_ALLOW_THREADS |
| 1116 | break; |
| 1117 | default: |
| 1118 | PyErr_SetString(PyExc_TypeError, "getch requires 0 or 2 arguments"); |
| 1119 | return NULL; |
| 1120 | } |
| 1121 | return PyLong_FromLong((long)rtn); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1122 | } |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 1123 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1124 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1125 | PyCursesWindow_GetKey(PyCursesWindowObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1126 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1127 | int x, y; |
| 1128 | int rtn; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1129 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1130 | switch (PyTuple_Size(args)) { |
| 1131 | case 0: |
| 1132 | Py_BEGIN_ALLOW_THREADS |
| 1133 | rtn = wgetch(self->win); |
| 1134 | Py_END_ALLOW_THREADS |
| 1135 | break; |
| 1136 | case 2: |
| 1137 | if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x)) |
| 1138 | return NULL; |
| 1139 | Py_BEGIN_ALLOW_THREADS |
| 1140 | rtn = mvwgetch(self->win,y,x); |
| 1141 | Py_END_ALLOW_THREADS |
| 1142 | break; |
| 1143 | default: |
| 1144 | PyErr_SetString(PyExc_TypeError, "getkey requires 0 or 2 arguments"); |
| 1145 | return NULL; |
| 1146 | } |
| 1147 | if (rtn == ERR) { |
| 1148 | /* getch() returns ERR in nodelay mode */ |
R David Murray | f5d7cc2 | 2013-03-19 16:23:09 -0400 | [diff] [blame] | 1149 | PyErr_CheckSignals(); |
| 1150 | if (!PyErr_Occurred()) |
| 1151 | PyErr_SetString(PyCursesError, "no input"); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1152 | return NULL; |
| 1153 | } else if (rtn<=255) { |
| 1154 | return Py_BuildValue("C", rtn); |
| 1155 | } else { |
| 1156 | const char *knp; |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 1157 | #if defined(__NetBSD__) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1158 | knp = unctrl(rtn); |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 1159 | #else |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1160 | knp = keyname(rtn); |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 1161 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1162 | return PyUnicode_FromString((knp == NULL) ? "" : knp); |
| 1163 | } |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Nadeem Vawda | 9e2e990 | 2011-07-31 15:01:11 +0200 | [diff] [blame] | 1166 | #ifdef HAVE_NCURSESW |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1167 | static PyObject * |
Victor Stinner | a7878b7 | 2011-07-14 23:07:44 +0200 | [diff] [blame] | 1168 | PyCursesWindow_Get_WCh(PyCursesWindowObject *self, PyObject *args) |
| 1169 | { |
| 1170 | int x, y; |
| 1171 | int ct; |
| 1172 | wint_t rtn; |
| 1173 | |
| 1174 | switch (PyTuple_Size(args)) { |
| 1175 | case 0: |
| 1176 | Py_BEGIN_ALLOW_THREADS |
| 1177 | ct = wget_wch(self->win,&rtn); |
| 1178 | Py_END_ALLOW_THREADS |
| 1179 | break; |
| 1180 | case 2: |
| 1181 | if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x)) |
| 1182 | return NULL; |
| 1183 | Py_BEGIN_ALLOW_THREADS |
| 1184 | ct = mvwget_wch(self->win,y,x,&rtn); |
| 1185 | Py_END_ALLOW_THREADS |
| 1186 | break; |
| 1187 | default: |
| 1188 | PyErr_SetString(PyExc_TypeError, "get_wch requires 0 or 2 arguments"); |
| 1189 | return NULL; |
| 1190 | } |
| 1191 | if (ct == ERR) { |
Victor Stinner | bd2d30c | 2013-03-21 12:21:06 +0100 | [diff] [blame] | 1192 | if (PyErr_CheckSignals()) |
| 1193 | return NULL; |
| 1194 | |
Victor Stinner | a7878b7 | 2011-07-14 23:07:44 +0200 | [diff] [blame] | 1195 | /* get_wch() returns ERR in nodelay mode */ |
| 1196 | PyErr_SetString(PyCursesError, "no input"); |
| 1197 | return NULL; |
| 1198 | } |
Victor Stinner | 1d39cde | 2012-08-29 01:40:57 +0200 | [diff] [blame] | 1199 | if (ct == KEY_CODE_YES) |
| 1200 | return PyLong_FromLong(rtn); |
| 1201 | else |
| 1202 | return PyUnicode_FromOrdinal(rtn); |
Victor Stinner | a7878b7 | 2011-07-14 23:07:44 +0200 | [diff] [blame] | 1203 | } |
Nadeem Vawda | 9e2e990 | 2011-07-31 15:01:11 +0200 | [diff] [blame] | 1204 | #endif |
Victor Stinner | a7878b7 | 2011-07-14 23:07:44 +0200 | [diff] [blame] | 1205 | |
| 1206 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1207 | PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1208 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1209 | int x, y, n; |
| 1210 | char rtn[1024]; /* This should be big enough.. I hope */ |
| 1211 | int rtn2; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 1212 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1213 | switch (PyTuple_Size(args)) { |
| 1214 | case 0: |
| 1215 | Py_BEGIN_ALLOW_THREADS |
| 1216 | rtn2 = wgetnstr(self->win,rtn, 1023); |
| 1217 | Py_END_ALLOW_THREADS |
| 1218 | break; |
| 1219 | case 1: |
| 1220 | if (!PyArg_ParseTuple(args,"i;n", &n)) |
| 1221 | return NULL; |
Benjamin Peterson | 40a77c3 | 2016-08-13 18:15:28 -0700 | [diff] [blame] | 1222 | if (n < 0) { |
| 1223 | PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative"); |
| 1224 | return NULL; |
| 1225 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1226 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 1227 | rtn2 = wgetnstr(self->win, rtn, Py_MIN(n, 1023)); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1228 | Py_END_ALLOW_THREADS |
| 1229 | break; |
| 1230 | case 2: |
| 1231 | if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x)) |
| 1232 | return NULL; |
| 1233 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1234 | #ifdef STRICT_SYSV_CURSES |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1235 | 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] | 1236 | #else |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1237 | rtn2 = mvwgetnstr(self->win,y,x,rtn, 1023); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1238 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1239 | Py_END_ALLOW_THREADS |
| 1240 | break; |
| 1241 | case 3: |
| 1242 | if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n)) |
| 1243 | return NULL; |
Benjamin Peterson | 40a77c3 | 2016-08-13 18:15:28 -0700 | [diff] [blame] | 1244 | if (n < 0) { |
| 1245 | PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative"); |
| 1246 | return NULL; |
| 1247 | } |
Andrew M. Kuchling | a1e4b05 | 2000-06-27 21:49:47 +0000 | [diff] [blame] | 1248 | #ifdef STRICT_SYSV_CURSES |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1249 | Py_BEGIN_ALLOW_THREADS |
| 1250 | rtn2 = wmove(self->win,y,x)==ERR ? ERR : |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 1251 | wgetnstr(self->win, rtn, Py_MIN(n, 1023)); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1252 | Py_END_ALLOW_THREADS |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1253 | #else |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1254 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 1255 | rtn2 = mvwgetnstr(self->win, y, x, rtn, Py_MIN(n, 1023)); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1256 | Py_END_ALLOW_THREADS |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1257 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1258 | break; |
| 1259 | default: |
| 1260 | PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 3 arguments"); |
| 1261 | return NULL; |
| 1262 | } |
| 1263 | if (rtn2 == ERR) |
| 1264 | rtn[0] = 0; |
| 1265 | return PyBytes_FromString(rtn); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1269 | PyCursesWindow_Hline(PyCursesWindowObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1270 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1271 | PyObject *temp; |
| 1272 | chtype ch; |
| 1273 | int n, x, y, code = OK; |
| 1274 | attr_t attr = A_NORMAL; |
| 1275 | long lattr; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1276 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1277 | switch (PyTuple_Size(args)) { |
| 1278 | case 2: |
| 1279 | if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n)) |
| 1280 | return NULL; |
| 1281 | break; |
| 1282 | case 3: |
| 1283 | if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &lattr)) |
| 1284 | return NULL; |
| 1285 | attr = lattr; |
| 1286 | break; |
| 1287 | case 4: |
| 1288 | if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n)) |
| 1289 | return NULL; |
| 1290 | code = wmove(self->win, y, x); |
| 1291 | break; |
| 1292 | case 5: |
| 1293 | if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr", |
| 1294 | &y, &x, &temp, &n, &lattr)) |
| 1295 | return NULL; |
| 1296 | attr = lattr; |
| 1297 | code = wmove(self->win, y, x); |
| 1298 | break; |
| 1299 | default: |
| 1300 | PyErr_SetString(PyExc_TypeError, "hline requires 2 to 5 arguments"); |
| 1301 | return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1302 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1303 | |
| 1304 | if (code != ERR) { |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1305 | if (!PyCurses_ConvertToChtype(self, temp, &ch)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1306 | return NULL; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1307 | return PyCursesCheckERR(whline(self->win, ch | attr, n), "hline"); |
| 1308 | } else |
| 1309 | return PyCursesCheckERR(code, "wmove"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1313 | PyCursesWindow_InsCh(PyCursesWindowObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1314 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1315 | int rtn, x, y, use_xy = FALSE; |
| 1316 | PyObject *temp; |
| 1317 | chtype ch = 0; |
| 1318 | attr_t attr = A_NORMAL; |
| 1319 | long lattr; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1320 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1321 | switch (PyTuple_Size(args)) { |
| 1322 | case 1: |
| 1323 | if (!PyArg_ParseTuple(args, "O;ch or int", &temp)) |
| 1324 | return NULL; |
| 1325 | break; |
| 1326 | case 2: |
| 1327 | if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &temp, &lattr)) |
| 1328 | return NULL; |
| 1329 | attr = lattr; |
| 1330 | break; |
| 1331 | case 3: |
| 1332 | if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &temp)) |
| 1333 | return NULL; |
| 1334 | use_xy = TRUE; |
| 1335 | break; |
| 1336 | case 4: |
| 1337 | if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr", &y, &x, &temp, &lattr)) |
| 1338 | return NULL; |
| 1339 | attr = lattr; |
| 1340 | use_xy = TRUE; |
| 1341 | break; |
| 1342 | default: |
| 1343 | PyErr_SetString(PyExc_TypeError, "insch requires 1 or 4 arguments"); |
| 1344 | return NULL; |
| 1345 | } |
| 1346 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1347 | if (!PyCurses_ConvertToChtype(self, temp, &ch)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1348 | return NULL; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1349 | |
| 1350 | if (use_xy == TRUE) |
| 1351 | rtn = mvwinsch(self->win,y,x, ch | attr); |
| 1352 | else { |
| 1353 | rtn = winsch(self->win, ch | attr); |
| 1354 | } |
| 1355 | return PyCursesCheckERR(rtn, "insch"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1359 | PyCursesWindow_InCh(PyCursesWindowObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1360 | { |
Christian Heimes | abbc8ca | 2013-12-04 08:50:22 +0100 | [diff] [blame] | 1361 | int x, y; |
| 1362 | unsigned long rtn; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 1363 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1364 | switch (PyTuple_Size(args)) { |
| 1365 | case 0: |
| 1366 | rtn = winch(self->win); |
| 1367 | break; |
| 1368 | case 2: |
| 1369 | if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x)) |
| 1370 | return NULL; |
| 1371 | rtn = mvwinch(self->win,y,x); |
| 1372 | break; |
| 1373 | default: |
| 1374 | PyErr_SetString(PyExc_TypeError, "inch requires 0 or 2 arguments"); |
| 1375 | return NULL; |
| 1376 | } |
Christian Heimes | abbc8ca | 2013-12-04 08:50:22 +0100 | [diff] [blame] | 1377 | return PyLong_FromUnsignedLong(rtn); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1381 | PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1382 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1383 | int x, y, n; |
| 1384 | char rtn[1024]; /* This should be big enough.. I hope */ |
| 1385 | int rtn2; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1386 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1387 | switch (PyTuple_Size(args)) { |
| 1388 | case 0: |
| 1389 | rtn2 = winnstr(self->win,rtn, 1023); |
| 1390 | break; |
| 1391 | case 1: |
| 1392 | if (!PyArg_ParseTuple(args,"i;n", &n)) |
| 1393 | return NULL; |
Benjamin Peterson | 432ea4f | 2016-08-15 21:40:14 -0700 | [diff] [blame] | 1394 | if (n < 0) { |
| 1395 | PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative"); |
| 1396 | return NULL; |
| 1397 | } |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 1398 | rtn2 = winnstr(self->win, rtn, Py_MIN(n, 1023)); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1399 | break; |
| 1400 | case 2: |
| 1401 | if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x)) |
| 1402 | return NULL; |
| 1403 | rtn2 = mvwinnstr(self->win,y,x,rtn,1023); |
| 1404 | break; |
| 1405 | case 3: |
| 1406 | if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n)) |
| 1407 | return NULL; |
Benjamin Peterson | 432ea4f | 2016-08-15 21:40:14 -0700 | [diff] [blame] | 1408 | if (n < 0) { |
| 1409 | PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative"); |
| 1410 | return NULL; |
| 1411 | } |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 1412 | rtn2 = mvwinnstr(self->win, y, x, rtn, Py_MIN(n,1023)); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1413 | break; |
| 1414 | default: |
| 1415 | PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments"); |
| 1416 | return NULL; |
| 1417 | } |
| 1418 | if (rtn2 == ERR) |
| 1419 | rtn[0] = 0; |
| 1420 | return PyBytes_FromString(rtn); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1424 | PyCursesWindow_InsStr(PyCursesWindowObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1425 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1426 | int rtn; |
| 1427 | int x, y; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1428 | int strtype; |
Benjamin Peterson | 5e54519 | 2012-03-14 18:21:35 -0500 | [diff] [blame] | 1429 | PyObject *strobj, *bytesobj = NULL; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1430 | #ifdef HAVE_NCURSESW |
| 1431 | wchar_t *wstr = NULL; |
| 1432 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1433 | attr_t attr = A_NORMAL , attr_old = A_NORMAL; |
| 1434 | long lattr; |
| 1435 | int use_xy = FALSE, use_attr = FALSE; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1436 | const char *funcname; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1437 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1438 | switch (PyTuple_Size(args)) { |
| 1439 | case 1: |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1440 | if (!PyArg_ParseTuple(args,"O;str", &strobj)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1441 | return NULL; |
| 1442 | break; |
| 1443 | case 2: |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1444 | if (!PyArg_ParseTuple(args,"Ol;str,attr", &strobj, &lattr)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1445 | return NULL; |
| 1446 | attr = lattr; |
| 1447 | use_attr = TRUE; |
| 1448 | break; |
| 1449 | case 3: |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1450 | if (!PyArg_ParseTuple(args,"iiO;y,x,str", &y, &x, &strobj)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1451 | return NULL; |
| 1452 | use_xy = TRUE; |
| 1453 | break; |
| 1454 | case 4: |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1455 | if (!PyArg_ParseTuple(args,"iiOl;y,x,str,attr", &y, &x, &strobj, &lattr)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1456 | return NULL; |
| 1457 | attr = lattr; |
| 1458 | use_xy = use_attr = TRUE; |
| 1459 | break; |
| 1460 | default: |
| 1461 | PyErr_SetString(PyExc_TypeError, "insstr requires 1 to 4 arguments"); |
| 1462 | return NULL; |
| 1463 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1464 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1465 | #ifdef HAVE_NCURSESW |
| 1466 | strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, &wstr); |
| 1467 | #else |
| 1468 | strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, NULL); |
| 1469 | #endif |
| 1470 | if (strtype == 0) |
| 1471 | return NULL; |
| 1472 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1473 | if (use_attr == TRUE) { |
| 1474 | attr_old = getattrs(self->win); |
| 1475 | (void)wattrset(self->win,attr); |
| 1476 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1477 | #ifdef HAVE_NCURSESW |
| 1478 | if (strtype == 2) { |
| 1479 | funcname = "inswstr"; |
| 1480 | if (use_xy == TRUE) |
| 1481 | rtn = mvwins_wstr(self->win,y,x,wstr); |
| 1482 | else |
| 1483 | rtn = wins_wstr(self->win,wstr); |
| 1484 | PyMem_Free(wstr); |
| 1485 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1486 | else |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1487 | #endif |
| 1488 | { |
| 1489 | char *str = PyBytes_AS_STRING(bytesobj); |
| 1490 | funcname = "insstr"; |
| 1491 | if (use_xy == TRUE) |
| 1492 | rtn = mvwinsstr(self->win,y,x,str); |
| 1493 | else |
| 1494 | rtn = winsstr(self->win,str); |
| 1495 | Py_DECREF(bytesobj); |
| 1496 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1497 | if (use_attr == TRUE) |
| 1498 | (void)wattrset(self->win,attr_old); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1499 | return PyCursesCheckERR(rtn, funcname); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1503 | PyCursesWindow_InsNStr(PyCursesWindowObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1504 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1505 | int rtn, x, y, n; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1506 | int strtype; |
Benjamin Peterson | 5e54519 | 2012-03-14 18:21:35 -0500 | [diff] [blame] | 1507 | PyObject *strobj, *bytesobj = NULL; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1508 | #ifdef HAVE_NCURSESW |
| 1509 | wchar_t *wstr = NULL; |
| 1510 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1511 | attr_t attr = A_NORMAL , attr_old = A_NORMAL; |
| 1512 | long lattr; |
| 1513 | int use_xy = FALSE, use_attr = FALSE; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1514 | const char *funcname; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1515 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1516 | switch (PyTuple_Size(args)) { |
| 1517 | case 2: |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1518 | if (!PyArg_ParseTuple(args,"Oi;str,n", &strobj, &n)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1519 | return NULL; |
| 1520 | break; |
| 1521 | case 3: |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1522 | if (!PyArg_ParseTuple(args,"Oil;str,n,attr", &strobj, &n, &lattr)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1523 | return NULL; |
| 1524 | attr = lattr; |
| 1525 | use_attr = TRUE; |
| 1526 | break; |
| 1527 | case 4: |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1528 | if (!PyArg_ParseTuple(args,"iiOi;y,x,str,n", &y, &x, &strobj, &n)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1529 | return NULL; |
| 1530 | use_xy = TRUE; |
| 1531 | break; |
| 1532 | case 5: |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1533 | if (!PyArg_ParseTuple(args,"iiOil;y,x,str,n,attr", &y, &x, &strobj, &n, &lattr)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1534 | return NULL; |
| 1535 | attr = lattr; |
| 1536 | use_xy = use_attr = TRUE; |
| 1537 | break; |
| 1538 | default: |
| 1539 | PyErr_SetString(PyExc_TypeError, "insnstr requires 2 to 5 arguments"); |
| 1540 | return NULL; |
| 1541 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1542 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1543 | #ifdef HAVE_NCURSESW |
| 1544 | strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, &wstr); |
| 1545 | #else |
| 1546 | strtype = PyCurses_ConvertToString(self, strobj, &bytesobj, NULL); |
| 1547 | #endif |
| 1548 | if (strtype == 0) |
| 1549 | return NULL; |
| 1550 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1551 | if (use_attr == TRUE) { |
| 1552 | attr_old = getattrs(self->win); |
| 1553 | (void)wattrset(self->win,attr); |
| 1554 | } |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1555 | #ifdef HAVE_NCURSESW |
| 1556 | if (strtype == 2) { |
| 1557 | funcname = "insn_wstr"; |
| 1558 | if (use_xy == TRUE) |
| 1559 | rtn = mvwins_nwstr(self->win,y,x,wstr,n); |
| 1560 | else |
| 1561 | rtn = wins_nwstr(self->win,wstr,n); |
| 1562 | PyMem_Free(wstr); |
| 1563 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1564 | else |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1565 | #endif |
| 1566 | { |
| 1567 | char *str = PyBytes_AS_STRING(bytesobj); |
| 1568 | funcname = "insnstr"; |
| 1569 | if (use_xy == TRUE) |
| 1570 | rtn = mvwinsnstr(self->win,y,x,str,n); |
| 1571 | else |
| 1572 | rtn = winsnstr(self->win,str,n); |
| 1573 | Py_DECREF(bytesobj); |
| 1574 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1575 | if (use_attr == TRUE) |
| 1576 | (void)wattrset(self->win,attr_old); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1577 | return PyCursesCheckERR(rtn, funcname); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1581 | PyCursesWindow_Is_LineTouched(PyCursesWindowObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1582 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1583 | int line, erg; |
| 1584 | if (!PyArg_ParseTuple(args,"i;line", &line)) |
| 1585 | return NULL; |
| 1586 | erg = is_linetouched(self->win, line); |
| 1587 | if (erg == ERR) { |
| 1588 | PyErr_SetString(PyExc_TypeError, |
| 1589 | "is_linetouched: line number outside of boundaries"); |
| 1590 | return NULL; |
| 1591 | } else |
| 1592 | if (erg == FALSE) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1593 | Py_RETURN_FALSE; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1594 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1595 | Py_RETURN_TRUE; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1596 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1600 | PyCursesWindow_NoOutRefresh(PyCursesWindowObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1601 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1602 | int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol; |
| 1603 | int rtn; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1604 | |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 1605 | #ifndef WINDOW_HAS_FLAGS |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1606 | if (0) |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 1607 | #else |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1608 | if (self->win->_flags & _ISPAD) |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 1609 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1610 | { |
| 1611 | switch(PyTuple_Size(args)) { |
| 1612 | case 6: |
| 1613 | if (!PyArg_ParseTuple(args, |
| 1614 | "iiiiii;" \ |
| 1615 | "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol", |
| 1616 | &pminrow, &pmincol, &sminrow, |
| 1617 | &smincol, &smaxrow, &smaxcol)) |
| 1618 | return NULL; |
| 1619 | Py_BEGIN_ALLOW_THREADS |
| 1620 | rtn = pnoutrefresh(self->win, |
| 1621 | pminrow, pmincol, sminrow, |
| 1622 | smincol, smaxrow, smaxcol); |
| 1623 | Py_END_ALLOW_THREADS |
| 1624 | return PyCursesCheckERR(rtn, "pnoutrefresh"); |
| 1625 | default: |
| 1626 | PyErr_SetString(PyCursesError, |
| 1627 | "noutrefresh() called for a pad " |
| 1628 | "requires 6 arguments"); |
| 1629 | return NULL; |
| 1630 | } |
| 1631 | } else { |
| 1632 | if (!PyArg_ParseTuple(args, ":noutrefresh")) |
| 1633 | return NULL; |
Andrew M. Kuchling | f419572 | 2000-06-23 01:36:21 +0000 | [diff] [blame] | 1634 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1635 | Py_BEGIN_ALLOW_THREADS |
| 1636 | rtn = wnoutrefresh(self->win); |
| 1637 | Py_END_ALLOW_THREADS |
| 1638 | return PyCursesCheckERR(rtn, "wnoutrefresh"); |
| 1639 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | static PyObject * |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 1643 | PyCursesWindow_Overlay(PyCursesWindowObject *self, PyObject *args) |
| 1644 | { |
| 1645 | PyCursesWindowObject *temp; |
| 1646 | int use_copywin = FALSE; |
| 1647 | int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol; |
| 1648 | int rtn; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1649 | |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 1650 | switch (PyTuple_Size(args)) { |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 1651 | case 1: |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1652 | if (!PyArg_ParseTuple(args, "O!;window object", |
| 1653 | &PyCursesWindow_Type, &temp)) |
| 1654 | return NULL; |
| 1655 | break; |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 1656 | case 7: |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1657 | if (!PyArg_ParseTuple(args, "O!iiiiii;window object, int, int, int, int, int, int", |
| 1658 | &PyCursesWindow_Type, &temp, &sminrow, &smincol, |
| 1659 | &dminrow, &dmincol, &dmaxrow, &dmaxcol)) |
| 1660 | return NULL; |
| 1661 | use_copywin = TRUE; |
| 1662 | break; |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 1663 | default: |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1664 | PyErr_SetString(PyExc_TypeError, |
| 1665 | "overlay requires one or seven arguments"); |
| 1666 | return NULL; |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | if (use_copywin == TRUE) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1670 | rtn = copywin(self->win, temp->win, sminrow, smincol, |
| 1671 | dminrow, dmincol, dmaxrow, dmaxcol, TRUE); |
| 1672 | return PyCursesCheckERR(rtn, "copywin"); |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 1673 | } |
| 1674 | else { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1675 | rtn = overlay(self->win, temp->win); |
| 1676 | return PyCursesCheckERR(rtn, "overlay"); |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | static PyObject * |
| 1681 | PyCursesWindow_Overwrite(PyCursesWindowObject *self, PyObject *args) |
| 1682 | { |
| 1683 | PyCursesWindowObject *temp; |
| 1684 | int use_copywin = FALSE; |
| 1685 | int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol; |
| 1686 | int rtn; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1687 | |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 1688 | switch (PyTuple_Size(args)) { |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 1689 | case 1: |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1690 | if (!PyArg_ParseTuple(args, "O!;window object", |
| 1691 | &PyCursesWindow_Type, &temp)) |
| 1692 | return NULL; |
| 1693 | break; |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 1694 | case 7: |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1695 | if (!PyArg_ParseTuple(args, "O!iiiiii;window object, int, int, int, int, int, int", |
| 1696 | &PyCursesWindow_Type, &temp, &sminrow, &smincol, |
| 1697 | &dminrow, &dmincol, &dmaxrow, &dmaxcol)) |
| 1698 | return NULL; |
| 1699 | use_copywin = TRUE; |
| 1700 | break; |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 1701 | default: |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1702 | PyErr_SetString(PyExc_TypeError, |
| 1703 | "overwrite requires one or seven arguments"); |
| 1704 | return NULL; |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | if (use_copywin == TRUE) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1708 | rtn = copywin(self->win, temp->win, sminrow, smincol, |
| 1709 | dminrow, dmincol, dmaxrow, dmaxcol, FALSE); |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 1710 | return PyCursesCheckERR(rtn, "copywin"); |
| 1711 | } |
| 1712 | else { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1713 | rtn = overwrite(self->win, temp->win); |
| 1714 | return PyCursesCheckERR(rtn, "overwrite"); |
Andrew M. Kuchling | 9de6ffa | 2000-12-21 16:22:22 +0000 | [diff] [blame] | 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | static PyObject * |
Guido van Rossum | 6c95da3 | 2007-07-24 00:16:38 +0000 | [diff] [blame] | 1719 | PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *stream) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1720 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1721 | /* We have to simulate this by writing to a temporary FILE*, |
| 1722 | then reading back, then writing to the argument stream. */ |
Christian Heimes | 2b221b7 | 2017-03-02 11:09:01 +0100 | [diff] [blame^] | 1723 | FILE *fp; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1724 | PyObject *res = NULL; |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 1725 | |
Christian Heimes | 2b221b7 | 2017-03-02 11:09:01 +0100 | [diff] [blame^] | 1726 | fp = tmpfile(); |
| 1727 | if (fp == NULL) |
| 1728 | return PyErr_SetFromErrno(PyExc_OSError); |
| 1729 | if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1730 | goto exit; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1731 | res = PyCursesCheckERR(putwin(self->win, fp), "putwin"); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1732 | if (res == NULL) |
| 1733 | goto exit; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1734 | fseek(fp, 0, 0); |
| 1735 | while (1) { |
| 1736 | char buf[BUFSIZ]; |
| 1737 | Py_ssize_t n = fread(buf, 1, BUFSIZ, fp); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1738 | _Py_IDENTIFIER(write); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1739 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1740 | if (n <= 0) |
| 1741 | break; |
| 1742 | Py_DECREF(res); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1743 | res = _PyObject_CallMethodId(stream, &PyId_write, "y#", buf, n); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1744 | if (res == NULL) |
| 1745 | break; |
| 1746 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1747 | |
| 1748 | exit: |
Christian Heimes | 2b221b7 | 2017-03-02 11:09:01 +0100 | [diff] [blame^] | 1749 | fclose(fp); |
Guido van Rossum | 6c95da3 | 2007-07-24 00:16:38 +0000 | [diff] [blame] | 1750 | return res; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1754 | PyCursesWindow_RedrawLine(PyCursesWindowObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1755 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1756 | int beg, num; |
| 1757 | if (!PyArg_ParseTuple(args, "ii;beg,num", &beg, &num)) |
| 1758 | return NULL; |
| 1759 | return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1763 | PyCursesWindow_Refresh(PyCursesWindowObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1764 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1765 | int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol; |
| 1766 | int rtn; |
Andrew M. Kuchling | f419572 | 2000-06-23 01:36:21 +0000 | [diff] [blame] | 1767 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1768 | #ifndef WINDOW_HAS_FLAGS |
| 1769 | if (0) |
| 1770 | #else |
| 1771 | if (self->win->_flags & _ISPAD) |
| 1772 | #endif |
| 1773 | { |
| 1774 | switch(PyTuple_Size(args)) { |
| 1775 | case 6: |
| 1776 | if (!PyArg_ParseTuple(args, |
| 1777 | "iiiiii;" \ |
| 1778 | "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol", |
| 1779 | &pminrow, &pmincol, &sminrow, |
| 1780 | &smincol, &smaxrow, &smaxcol)) |
| 1781 | return NULL; |
| 1782 | |
| 1783 | Py_BEGIN_ALLOW_THREADS |
| 1784 | rtn = prefresh(self->win, |
| 1785 | pminrow, pmincol, sminrow, |
| 1786 | smincol, smaxrow, smaxcol); |
| 1787 | Py_END_ALLOW_THREADS |
| 1788 | return PyCursesCheckERR(rtn, "prefresh"); |
| 1789 | default: |
| 1790 | PyErr_SetString(PyCursesError, |
| 1791 | "refresh() for a pad requires 6 arguments"); |
| 1792 | return NULL; |
| 1793 | } |
| 1794 | } else { |
| 1795 | if (!PyArg_ParseTuple(args, ":refresh")) |
| 1796 | return NULL; |
| 1797 | Py_BEGIN_ALLOW_THREADS |
| 1798 | rtn = wrefresh(self->win); |
| 1799 | Py_END_ALLOW_THREADS |
| 1800 | return PyCursesCheckERR(rtn, "prefresh"); |
| 1801 | } |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1805 | PyCursesWindow_SetScrollRegion(PyCursesWindowObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1806 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1807 | int x, y; |
| 1808 | if (!PyArg_ParseTuple(args,"ii;top, bottom",&y,&x)) |
| 1809 | return NULL; |
| 1810 | return PyCursesCheckERR(wsetscrreg(self->win,y,x), "wsetscrreg"); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1814 | PyCursesWindow_SubWin(PyCursesWindowObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1815 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1816 | WINDOW *win; |
| 1817 | int nlines, ncols, begin_y, begin_x; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1818 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1819 | nlines = 0; |
| 1820 | ncols = 0; |
| 1821 | switch (PyTuple_Size(args)) { |
| 1822 | case 2: |
| 1823 | if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x)) |
| 1824 | return NULL; |
| 1825 | break; |
| 1826 | case 4: |
| 1827 | if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x", |
| 1828 | &nlines,&ncols,&begin_y,&begin_x)) |
| 1829 | return NULL; |
| 1830 | break; |
| 1831 | default: |
| 1832 | PyErr_SetString(PyExc_TypeError, "subwin requires 2 or 4 arguments"); |
| 1833 | return NULL; |
| 1834 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1835 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1836 | /* printf("Subwin: %i %i %i %i \n", nlines, ncols, begin_y, begin_x); */ |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 1837 | #ifdef WINDOW_HAS_FLAGS |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1838 | if (self->win->_flags & _ISPAD) |
| 1839 | win = subpad(self->win, nlines, ncols, begin_y, begin_x); |
| 1840 | else |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 1841 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1842 | win = subwin(self->win, nlines, ncols, begin_y, begin_x); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1843 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1844 | if (win == NULL) { |
| 1845 | PyErr_SetString(PyCursesError, catchall_NULL); |
| 1846 | return NULL; |
| 1847 | } |
| 1848 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1849 | return (PyObject *)PyCursesWindow_New(win, self->encoding); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1853 | PyCursesWindow_Scroll(PyCursesWindowObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1854 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1855 | int nlines; |
| 1856 | switch(PyTuple_Size(args)) { |
| 1857 | case 0: |
| 1858 | return PyCursesCheckERR(scroll(self->win), "scroll"); |
| 1859 | case 1: |
| 1860 | if (!PyArg_ParseTuple(args, "i;nlines", &nlines)) |
| 1861 | return NULL; |
| 1862 | return PyCursesCheckERR(wscrl(self->win, nlines), "scroll"); |
| 1863 | default: |
| 1864 | PyErr_SetString(PyExc_TypeError, "scroll requires 0 or 1 arguments"); |
| 1865 | return NULL; |
| 1866 | } |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1870 | PyCursesWindow_TouchLine(PyCursesWindowObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1871 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1872 | int st, cnt, val; |
| 1873 | switch (PyTuple_Size(args)) { |
| 1874 | case 2: |
| 1875 | if (!PyArg_ParseTuple(args,"ii;start,count",&st,&cnt)) |
| 1876 | return NULL; |
| 1877 | return PyCursesCheckERR(touchline(self->win,st,cnt), "touchline"); |
| 1878 | case 3: |
| 1879 | if (!PyArg_ParseTuple(args, "iii;start,count,val", &st, &cnt, &val)) |
| 1880 | return NULL; |
| 1881 | return PyCursesCheckERR(wtouchln(self->win, st, cnt, val), "touchline"); |
| 1882 | default: |
| 1883 | PyErr_SetString(PyExc_TypeError, "touchline requires 2 or 3 arguments"); |
| 1884 | return NULL; |
| 1885 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 1889 | PyCursesWindow_Vline(PyCursesWindowObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1890 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1891 | PyObject *temp; |
| 1892 | chtype ch; |
| 1893 | int n, x, y, code = OK; |
| 1894 | attr_t attr = A_NORMAL; |
| 1895 | long lattr; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1896 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1897 | switch (PyTuple_Size(args)) { |
| 1898 | case 2: |
| 1899 | if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n)) |
| 1900 | return NULL; |
| 1901 | break; |
| 1902 | case 3: |
| 1903 | if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &lattr)) |
| 1904 | return NULL; |
| 1905 | attr = lattr; |
| 1906 | break; |
| 1907 | case 4: |
| 1908 | if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n)) |
| 1909 | return NULL; |
| 1910 | code = wmove(self->win, y, x); |
| 1911 | break; |
| 1912 | case 5: |
| 1913 | if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr", |
| 1914 | &y, &x, &temp, &n, &lattr)) |
| 1915 | return NULL; |
| 1916 | attr = lattr; |
| 1917 | code = wmove(self->win, y, x); |
| 1918 | break; |
| 1919 | default: |
| 1920 | PyErr_SetString(PyExc_TypeError, "vline requires 2 to 5 arguments"); |
| 1921 | return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 1922 | } |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1923 | |
| 1924 | if (code != ERR) { |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1925 | if (!PyCurses_ConvertToChtype(self, temp, &ch)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1926 | return NULL; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1927 | return PyCursesCheckERR(wvline(self->win, ch | attr, n), "vline"); |
| 1928 | } else |
| 1929 | return PyCursesCheckERR(code, "wmove"); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1932 | static PyObject * |
| 1933 | PyCursesWindow_get_encoding(PyCursesWindowObject *self, void *closure) |
| 1934 | { |
| 1935 | return PyUnicode_FromString(self->encoding); |
| 1936 | } |
| 1937 | |
| 1938 | static int |
| 1939 | PyCursesWindow_set_encoding(PyCursesWindowObject *self, PyObject *value) |
| 1940 | { |
| 1941 | PyObject *ascii; |
| 1942 | char *encoding; |
| 1943 | |
| 1944 | /* It is illegal to del win.encoding */ |
| 1945 | if (value == NULL) { |
| 1946 | PyErr_SetString(PyExc_TypeError, |
| 1947 | "encoding may not be deleted"); |
| 1948 | return -1; |
| 1949 | } |
| 1950 | |
| 1951 | if (!PyUnicode_Check(value)) { |
| 1952 | PyErr_SetString(PyExc_TypeError, |
| 1953 | "setting encoding to a non-string"); |
| 1954 | return -1; |
| 1955 | } |
| 1956 | ascii = PyUnicode_AsASCIIString(value); |
| 1957 | if (ascii == NULL) |
| 1958 | return -1; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1959 | encoding = _PyMem_Strdup(PyBytes_AS_STRING(ascii)); |
Ross Lagerwall | 2dabaf6 | 2012-09-07 08:34:23 +0200 | [diff] [blame] | 1960 | Py_DECREF(ascii); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1961 | if (encoding == NULL) { |
| 1962 | PyErr_NoMemory(); |
| 1963 | return -1; |
| 1964 | } |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1965 | PyMem_Free(self->encoding); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 1966 | self->encoding = encoding; |
| 1967 | return 0; |
| 1968 | } |
| 1969 | |
| 1970 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 1971 | static PyMethodDef PyCursesWindow_Methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1972 | CURSES_WINDOW_ADDCH_METHODDEF |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1973 | {"addnstr", (PyCFunction)PyCursesWindow_AddNStr, METH_VARARGS}, |
| 1974 | {"addstr", (PyCFunction)PyCursesWindow_AddStr, METH_VARARGS}, |
| 1975 | {"attroff", (PyCFunction)PyCursesWindow_AttrOff, METH_VARARGS}, |
| 1976 | {"attron", (PyCFunction)PyCursesWindow_AttrOn, METH_VARARGS}, |
| 1977 | {"attrset", (PyCFunction)PyCursesWindow_AttrSet, METH_VARARGS}, |
| 1978 | {"bkgd", (PyCFunction)PyCursesWindow_Bkgd, METH_VARARGS}, |
| 1979 | {"chgat", (PyCFunction)PyCursesWindow_ChgAt, METH_VARARGS}, |
| 1980 | {"bkgdset", (PyCFunction)PyCursesWindow_BkgdSet, METH_VARARGS}, |
| 1981 | {"border", (PyCFunction)PyCursesWindow_Border, METH_VARARGS}, |
| 1982 | {"box", (PyCFunction)PyCursesWindow_Box, METH_VARARGS}, |
| 1983 | {"clear", (PyCFunction)PyCursesWindow_wclear, METH_NOARGS}, |
| 1984 | {"clearok", (PyCFunction)PyCursesWindow_clearok, METH_VARARGS}, |
| 1985 | {"clrtobot", (PyCFunction)PyCursesWindow_wclrtobot, METH_NOARGS}, |
| 1986 | {"clrtoeol", (PyCFunction)PyCursesWindow_wclrtoeol, METH_NOARGS}, |
| 1987 | {"cursyncup", (PyCFunction)PyCursesWindow_wcursyncup, METH_NOARGS}, |
| 1988 | {"delch", (PyCFunction)PyCursesWindow_DelCh, METH_VARARGS}, |
| 1989 | {"deleteln", (PyCFunction)PyCursesWindow_wdeleteln, METH_NOARGS}, |
| 1990 | {"derwin", (PyCFunction)PyCursesWindow_DerWin, METH_VARARGS}, |
| 1991 | {"echochar", (PyCFunction)PyCursesWindow_EchoChar, METH_VARARGS}, |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 1992 | #ifdef NCURSES_MOUSE_VERSION |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1993 | {"enclose", (PyCFunction)PyCursesWindow_Enclose, METH_VARARGS}, |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 1994 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 1995 | {"erase", (PyCFunction)PyCursesWindow_werase, METH_NOARGS}, |
| 1996 | {"getbegyx", (PyCFunction)PyCursesWindow_getbegyx, METH_NOARGS}, |
| 1997 | {"getbkgd", (PyCFunction)PyCursesWindow_GetBkgd, METH_NOARGS}, |
| 1998 | {"getch", (PyCFunction)PyCursesWindow_GetCh, METH_VARARGS}, |
| 1999 | {"getkey", (PyCFunction)PyCursesWindow_GetKey, METH_VARARGS}, |
Nadeem Vawda | 9e2e990 | 2011-07-31 15:01:11 +0200 | [diff] [blame] | 2000 | #ifdef HAVE_NCURSESW |
Victor Stinner | a7878b7 | 2011-07-14 23:07:44 +0200 | [diff] [blame] | 2001 | {"get_wch", (PyCFunction)PyCursesWindow_Get_WCh, METH_VARARGS}, |
Nadeem Vawda | 9e2e990 | 2011-07-31 15:01:11 +0200 | [diff] [blame] | 2002 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2003 | {"getmaxyx", (PyCFunction)PyCursesWindow_getmaxyx, METH_NOARGS}, |
| 2004 | {"getparyx", (PyCFunction)PyCursesWindow_getparyx, METH_NOARGS}, |
| 2005 | {"getstr", (PyCFunction)PyCursesWindow_GetStr, METH_VARARGS}, |
| 2006 | {"getyx", (PyCFunction)PyCursesWindow_getyx, METH_NOARGS}, |
| 2007 | {"hline", (PyCFunction)PyCursesWindow_Hline, METH_VARARGS}, |
| 2008 | {"idcok", (PyCFunction)PyCursesWindow_idcok, METH_VARARGS}, |
| 2009 | {"idlok", (PyCFunction)PyCursesWindow_idlok, METH_VARARGS}, |
| 2010 | {"immedok", (PyCFunction)PyCursesWindow_immedok, METH_VARARGS}, |
| 2011 | {"inch", (PyCFunction)PyCursesWindow_InCh, METH_VARARGS}, |
| 2012 | {"insch", (PyCFunction)PyCursesWindow_InsCh, METH_VARARGS}, |
| 2013 | {"insdelln", (PyCFunction)PyCursesWindow_winsdelln, METH_VARARGS}, |
| 2014 | {"insertln", (PyCFunction)PyCursesWindow_winsertln, METH_NOARGS}, |
| 2015 | {"insnstr", (PyCFunction)PyCursesWindow_InsNStr, METH_VARARGS}, |
| 2016 | {"insstr", (PyCFunction)PyCursesWindow_InsStr, METH_VARARGS}, |
| 2017 | {"instr", (PyCFunction)PyCursesWindow_InStr, METH_VARARGS}, |
| 2018 | {"is_linetouched", (PyCFunction)PyCursesWindow_Is_LineTouched, METH_VARARGS}, |
| 2019 | {"is_wintouched", (PyCFunction)PyCursesWindow_is_wintouched, METH_NOARGS}, |
| 2020 | {"keypad", (PyCFunction)PyCursesWindow_keypad, METH_VARARGS}, |
| 2021 | {"leaveok", (PyCFunction)PyCursesWindow_leaveok, METH_VARARGS}, |
| 2022 | {"move", (PyCFunction)PyCursesWindow_wmove, METH_VARARGS}, |
| 2023 | {"mvderwin", (PyCFunction)PyCursesWindow_mvderwin, METH_VARARGS}, |
| 2024 | {"mvwin", (PyCFunction)PyCursesWindow_mvwin, METH_VARARGS}, |
| 2025 | {"nodelay", (PyCFunction)PyCursesWindow_nodelay, METH_VARARGS}, |
| 2026 | {"notimeout", (PyCFunction)PyCursesWindow_notimeout, METH_VARARGS}, |
| 2027 | {"noutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS}, |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2028 | {"overlay", (PyCFunction)PyCursesWindow_Overlay, METH_VARARGS}, |
| 2029 | {"overwrite", (PyCFunction)PyCursesWindow_Overwrite, |
| 2030 | METH_VARARGS}, |
| 2031 | {"putwin", (PyCFunction)PyCursesWindow_PutWin, METH_O}, |
| 2032 | {"redrawln", (PyCFunction)PyCursesWindow_RedrawLine, METH_VARARGS}, |
| 2033 | {"redrawwin", (PyCFunction)PyCursesWindow_redrawwin, METH_NOARGS}, |
| 2034 | {"refresh", (PyCFunction)PyCursesWindow_Refresh, METH_VARARGS}, |
Andrew M. Kuchling | a1e4b05 | 2000-06-27 21:49:47 +0000 | [diff] [blame] | 2035 | #ifndef STRICT_SYSV_CURSES |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2036 | {"resize", (PyCFunction)PyCursesWindow_wresize, METH_VARARGS}, |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2037 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2038 | {"scroll", (PyCFunction)PyCursesWindow_Scroll, METH_VARARGS}, |
| 2039 | {"scrollok", (PyCFunction)PyCursesWindow_scrollok, METH_VARARGS}, |
| 2040 | {"setscrreg", (PyCFunction)PyCursesWindow_SetScrollRegion, METH_VARARGS}, |
| 2041 | {"standend", (PyCFunction)PyCursesWindow_wstandend, METH_NOARGS}, |
| 2042 | {"standout", (PyCFunction)PyCursesWindow_wstandout, METH_NOARGS}, |
| 2043 | {"subpad", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS}, |
| 2044 | {"subwin", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS}, |
| 2045 | {"syncdown", (PyCFunction)PyCursesWindow_wsyncdown, METH_NOARGS}, |
| 2046 | {"syncok", (PyCFunction)PyCursesWindow_syncok, METH_VARARGS}, |
| 2047 | {"syncup", (PyCFunction)PyCursesWindow_wsyncup, METH_NOARGS}, |
| 2048 | {"timeout", (PyCFunction)PyCursesWindow_wtimeout, METH_VARARGS}, |
| 2049 | {"touchline", (PyCFunction)PyCursesWindow_TouchLine, METH_VARARGS}, |
| 2050 | {"touchwin", (PyCFunction)PyCursesWindow_touchwin, METH_NOARGS}, |
| 2051 | {"untouchwin", (PyCFunction)PyCursesWindow_untouchwin, METH_NOARGS}, |
| 2052 | {"vline", (PyCFunction)PyCursesWindow_Vline, METH_VARARGS}, |
| 2053 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2054 | }; |
| 2055 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2056 | static PyGetSetDef PyCursesWindow_getsets[] = { |
| 2057 | {"encoding", |
| 2058 | (getter)PyCursesWindow_get_encoding, |
| 2059 | (setter)PyCursesWindow_set_encoding, |
Ronald Oussoren | 2a7fe03 | 2012-01-17 16:53:20 +0100 | [diff] [blame] | 2060 | "the typecode character used to create the array"}, |
| 2061 | {NULL, NULL, NULL, NULL } /* sentinel */ |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2062 | }; |
| 2063 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2064 | /* -------------------------------------------------------*/ |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 2065 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2066 | PyTypeObject PyCursesWindow_Type = { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2067 | PyVarObject_HEAD_INIT(NULL, 0) |
Victor Stinner | 61e2bc7 | 2017-02-12 23:42:02 +0100 | [diff] [blame] | 2068 | "_curses.window", /*tp_name*/ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2069 | sizeof(PyCursesWindowObject), /*tp_basicsize*/ |
| 2070 | 0, /*tp_itemsize*/ |
| 2071 | /* methods */ |
| 2072 | (destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/ |
| 2073 | 0, /*tp_print*/ |
| 2074 | (getattrfunc)0, /*tp_getattr*/ |
| 2075 | (setattrfunc)0, /*tp_setattr*/ |
| 2076 | 0, /*tp_reserved*/ |
| 2077 | 0, /*tp_repr*/ |
| 2078 | 0, /*tp_as_number*/ |
| 2079 | 0, /*tp_as_sequence*/ |
| 2080 | 0, /*tp_as_mapping*/ |
| 2081 | 0, /*tp_hash*/ |
| 2082 | 0, /*tp_call*/ |
| 2083 | 0, /*tp_str*/ |
| 2084 | 0, /*tp_getattro*/ |
| 2085 | 0, /*tp_setattro*/ |
| 2086 | 0, /*tp_as_buffer*/ |
| 2087 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 2088 | 0, /*tp_doc*/ |
| 2089 | 0, /*tp_traverse*/ |
| 2090 | 0, /*tp_clear*/ |
| 2091 | 0, /*tp_richcompare*/ |
| 2092 | 0, /*tp_weaklistoffset*/ |
| 2093 | 0, /*tp_iter*/ |
| 2094 | 0, /*tp_iternext*/ |
| 2095 | PyCursesWindow_Methods, /*tp_methods*/ |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2096 | 0, /* tp_members */ |
| 2097 | PyCursesWindow_getsets, /* tp_getset */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2098 | }; |
| 2099 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2100 | /********************************************************************* |
| 2101 | Global Functions |
| 2102 | **********************************************************************/ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2103 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2104 | NoArgNoReturnFunction(beep) |
| 2105 | NoArgNoReturnFunction(def_prog_mode) |
| 2106 | NoArgNoReturnFunction(def_shell_mode) |
| 2107 | NoArgNoReturnFunction(doupdate) |
| 2108 | NoArgNoReturnFunction(endwin) |
| 2109 | NoArgNoReturnFunction(flash) |
| 2110 | NoArgNoReturnFunction(nocbreak) |
| 2111 | NoArgNoReturnFunction(noecho) |
| 2112 | NoArgNoReturnFunction(nonl) |
| 2113 | NoArgNoReturnFunction(noraw) |
| 2114 | NoArgNoReturnFunction(reset_prog_mode) |
| 2115 | NoArgNoReturnFunction(reset_shell_mode) |
| 2116 | NoArgNoReturnFunction(resetty) |
| 2117 | NoArgNoReturnFunction(savetty) |
| 2118 | |
| 2119 | NoArgOrFlagNoReturnFunction(cbreak) |
| 2120 | NoArgOrFlagNoReturnFunction(echo) |
| 2121 | NoArgOrFlagNoReturnFunction(nl) |
| 2122 | NoArgOrFlagNoReturnFunction(raw) |
| 2123 | |
| 2124 | NoArgReturnIntFunction(baudrate) |
| 2125 | NoArgReturnIntFunction(termattrs) |
| 2126 | |
| 2127 | NoArgReturnStringFunction(termname) |
| 2128 | NoArgReturnStringFunction(longname) |
| 2129 | |
| 2130 | NoArgTrueFalseFunction(can_change_color) |
| 2131 | NoArgTrueFalseFunction(has_colors) |
| 2132 | NoArgTrueFalseFunction(has_ic) |
| 2133 | NoArgTrueFalseFunction(has_il) |
| 2134 | NoArgTrueFalseFunction(isendwin) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2135 | NoArgNoReturnVoidFunction(flushinp) |
| 2136 | NoArgNoReturnVoidFunction(noqiflush) |
| 2137 | |
| 2138 | static PyObject * |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 2139 | PyCurses_filter(PyObject *self) |
| 2140 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2141 | /* not checking for PyCursesInitialised here since filter() must |
| 2142 | be called before initscr() */ |
| 2143 | filter(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2144 | Py_RETURN_NONE; |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2148 | PyCurses_Color_Content(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2149 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2150 | short color,r,g,b; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2151 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2152 | PyCursesInitialised; |
| 2153 | PyCursesInitialisedColor; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2154 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2155 | if (!PyArg_ParseTuple(args, "h:color_content", &color)) return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2156 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2157 | if (color_content(color, &r, &g, &b) != ERR) |
| 2158 | return Py_BuildValue("(iii)", r, g, b); |
| 2159 | else { |
| 2160 | PyErr_SetString(PyCursesError, |
| 2161 | "Argument 1 was out of range. Check value of COLORS."); |
| 2162 | return NULL; |
| 2163 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2167 | PyCurses_color_pair(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2168 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2169 | int n; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2170 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2171 | PyCursesInitialised; |
| 2172 | PyCursesInitialisedColor; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2173 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2174 | if (!PyArg_ParseTuple(args, "i:color_pair", &n)) return NULL; |
| 2175 | return PyLong_FromLong((long) (n << 8)); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2179 | PyCurses_Curs_Set(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2180 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2181 | int vis,erg; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2182 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2183 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2184 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2185 | if (!PyArg_ParseTuple(args, "i:curs_set", &vis)) return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2186 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2187 | erg = curs_set(vis); |
| 2188 | if (erg == ERR) return PyCursesCheckERR(erg, "curs_set"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2189 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2190 | return PyLong_FromLong((long) erg); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2194 | PyCurses_Delay_Output(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2195 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2196 | int ms; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2197 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2198 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2199 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2200 | if (!PyArg_ParseTuple(args, "i:delay_output", &ms)) return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2201 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2202 | return PyCursesCheckERR(delay_output(ms), "delay_output"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
| 2205 | static PyObject * |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 2206 | PyCurses_EraseChar(PyObject *self) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2207 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2208 | char ch; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2209 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2210 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2211 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2212 | ch = erasechar(); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2213 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2214 | return PyBytes_FromStringAndSize(&ch, 1); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | static PyObject * |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 2218 | PyCurses_getsyx(PyObject *self) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2219 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2220 | int x = 0; |
| 2221 | int y = 0; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2222 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2223 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2224 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2225 | getsyx(y, x); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2226 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2227 | return Py_BuildValue("(ii)", y, x); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2230 | #ifdef NCURSES_MOUSE_VERSION |
| 2231 | static PyObject * |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 2232 | PyCurses_GetMouse(PyObject *self) |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2233 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2234 | int rtn; |
| 2235 | MEVENT event; |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2236 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2237 | PyCursesInitialised; |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2238 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2239 | rtn = getmouse( &event ); |
| 2240 | if (rtn == ERR) { |
| 2241 | PyErr_SetString(PyCursesError, "getmouse() returned ERR"); |
| 2242 | return NULL; |
| 2243 | } |
| 2244 | return Py_BuildValue("(hiiil)", |
| 2245 | (short)event.id, |
| 2246 | event.x, event.y, event.z, |
| 2247 | (long) event.bstate); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2251 | PyCurses_UngetMouse(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2252 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2253 | MEVENT event; |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2254 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2255 | PyCursesInitialised; |
| 2256 | if (!PyArg_ParseTuple(args, "hiiil", |
| 2257 | &event.id, |
| 2258 | &event.x, &event.y, &event.z, |
| 2259 | (int *) &event.bstate)) |
| 2260 | return NULL; |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2261 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2262 | return PyCursesCheckERR(ungetmouse(&event), "ungetmouse"); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2263 | } |
| 2264 | #endif |
| 2265 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2266 | static PyObject * |
Guido van Rossum | 6c95da3 | 2007-07-24 00:16:38 +0000 | [diff] [blame] | 2267 | PyCurses_GetWin(PyCursesWindowObject *self, PyObject *stream) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2268 | { |
Christian Heimes | 2b221b7 | 2017-03-02 11:09:01 +0100 | [diff] [blame^] | 2269 | FILE *fp; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2270 | PyObject *data; |
| 2271 | size_t datalen; |
| 2272 | WINDOW *win; |
Benjamin Peterson | 18b7191 | 2013-05-16 15:29:44 -0500 | [diff] [blame] | 2273 | _Py_IDENTIFIER(read); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2274 | PyObject *res = NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2275 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2276 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2277 | |
Christian Heimes | 2b221b7 | 2017-03-02 11:09:01 +0100 | [diff] [blame^] | 2278 | fp = tmpfile(); |
| 2279 | if (fp == NULL) |
| 2280 | return PyErr_SetFromErrno(PyExc_OSError); |
| 2281 | |
| 2282 | if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2283 | goto error; |
Christian Heimes | 2b221b7 | 2017-03-02 11:09:01 +0100 | [diff] [blame^] | 2284 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2285 | |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 2286 | data = _PyObject_CallMethodId(stream, &PyId_read, NULL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2287 | if (data == NULL) |
| 2288 | goto error; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2289 | if (!PyBytes_Check(data)) { |
| 2290 | PyErr_Format(PyExc_TypeError, |
| 2291 | "f.read() returned %.100s instead of bytes", |
| 2292 | data->ob_type->tp_name); |
| 2293 | Py_DECREF(data); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2294 | goto error; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2295 | } |
| 2296 | datalen = PyBytes_GET_SIZE(data); |
| 2297 | if (fwrite(PyBytes_AS_STRING(data), 1, datalen, fp) != datalen) { |
| 2298 | Py_DECREF(data); |
Christian Heimes | 2b221b7 | 2017-03-02 11:09:01 +0100 | [diff] [blame^] | 2299 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2300 | goto error; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2301 | } |
Guido van Rossum | 6c95da3 | 2007-07-24 00:16:38 +0000 | [diff] [blame] | 2302 | Py_DECREF(data); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2303 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2304 | fseek(fp, 0, 0); |
| 2305 | win = getwin(fp); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2306 | if (win == NULL) { |
| 2307 | PyErr_SetString(PyCursesError, catchall_NULL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2308 | goto error; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2309 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2310 | res = PyCursesWindow_New(win, NULL); |
| 2311 | |
| 2312 | error: |
Christian Heimes | 2b221b7 | 2017-03-02 11:09:01 +0100 | [diff] [blame^] | 2313 | fclose(fp); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2314 | return res; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
| 2317 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2318 | PyCurses_HalfDelay(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2319 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2320 | unsigned char tenths; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2321 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2322 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2323 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2324 | if (!PyArg_ParseTuple(args, "b:halfdelay", &tenths)) return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2325 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2326 | return PyCursesCheckERR(halfdelay(tenths), "halfdelay"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
Andrew M. Kuchling | bf68026 | 2000-07-25 12:37:41 +0000 | [diff] [blame] | 2329 | #ifndef STRICT_SYSV_CURSES |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2330 | /* No has_key! */ |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2331 | static PyObject * PyCurses_has_key(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2332 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2333 | int ch; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2334 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2335 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2336 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2337 | if (!PyArg_ParseTuple(args,"i",&ch)) return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2338 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2339 | if (has_key(ch) == FALSE) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2340 | Py_RETURN_FALSE; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2341 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2342 | Py_RETURN_TRUE; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2343 | } |
Andrew M. Kuchling | bf68026 | 2000-07-25 12:37:41 +0000 | [diff] [blame] | 2344 | #endif /* STRICT_SYSV_CURSES */ |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2345 | |
| 2346 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2347 | PyCurses_Init_Color(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2348 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2349 | short color, r, g, b; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2350 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2351 | PyCursesInitialised; |
| 2352 | PyCursesInitialisedColor; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2353 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2354 | switch(PyTuple_Size(args)) { |
| 2355 | case 4: |
| 2356 | if (!PyArg_ParseTuple(args, "hhhh;color,r,g,b", &color, &r, &g, &b)) return NULL; |
| 2357 | break; |
| 2358 | default: |
| 2359 | PyErr_SetString(PyExc_TypeError, "init_color requires 4 arguments"); |
| 2360 | return NULL; |
| 2361 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2362 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2363 | return PyCursesCheckERR(init_color(color, r, g, b), "init_color"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2367 | PyCurses_Init_Pair(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2368 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2369 | short pair, f, b; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2370 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2371 | PyCursesInitialised; |
| 2372 | PyCursesInitialisedColor; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2373 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2374 | if (PyTuple_Size(args) != 3) { |
| 2375 | PyErr_SetString(PyExc_TypeError, "init_pair requires 3 arguments"); |
| 2376 | return NULL; |
| 2377 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2378 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2379 | if (!PyArg_ParseTuple(args, "hhh;pair, f, b", &pair, &f, &b)) return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2380 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2381 | return PyCursesCheckERR(init_pair(pair, f, b), "init_pair"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 2384 | static PyObject *ModDict; |
| 2385 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2386 | static PyObject * |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 2387 | PyCurses_InitScr(PyObject *self) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2388 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2389 | WINDOW *win; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2390 | PyCursesWindowObject *winobj; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2391 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2392 | if (initialised == TRUE) { |
| 2393 | wrefresh(stdscr); |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2394 | return (PyObject *)PyCursesWindow_New(stdscr, NULL); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2395 | } |
Guido van Rossum | 56bf235 | 1994-08-31 22:06:24 +0000 | [diff] [blame] | 2396 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2397 | win = initscr(); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2398 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2399 | if (win == NULL) { |
| 2400 | PyErr_SetString(PyCursesError, catchall_NULL); |
| 2401 | return NULL; |
| 2402 | } |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 2403 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2404 | initialised = initialised_setupterm = TRUE; |
Guido van Rossum | 56bf235 | 1994-08-31 22:06:24 +0000 | [diff] [blame] | 2405 | |
Andrew M. Kuchling | 03e644b | 2000-05-23 16:24:54 +0000 | [diff] [blame] | 2406 | /* This was moved from initcurses() because it core dumped on SGI, |
| 2407 | where they're not defined until you've called initscr() */ |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2408 | #define SetDictInt(string,ch) \ |
| 2409 | do { \ |
| 2410 | PyObject *o = PyLong_FromLong((long) (ch)); \ |
| 2411 | if (o && PyDict_SetItemString(ModDict, string, o) == 0) { \ |
| 2412 | Py_DECREF(o); \ |
| 2413 | } \ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2414 | } while (0) |
Andrew M. Kuchling | 03e644b | 2000-05-23 16:24:54 +0000 | [diff] [blame] | 2415 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2416 | /* Here are some graphic symbols you can use */ |
| 2417 | SetDictInt("ACS_ULCORNER", (ACS_ULCORNER)); |
| 2418 | SetDictInt("ACS_LLCORNER", (ACS_LLCORNER)); |
| 2419 | SetDictInt("ACS_URCORNER", (ACS_URCORNER)); |
| 2420 | SetDictInt("ACS_LRCORNER", (ACS_LRCORNER)); |
| 2421 | SetDictInt("ACS_LTEE", (ACS_LTEE)); |
| 2422 | SetDictInt("ACS_RTEE", (ACS_RTEE)); |
| 2423 | SetDictInt("ACS_BTEE", (ACS_BTEE)); |
| 2424 | SetDictInt("ACS_TTEE", (ACS_TTEE)); |
| 2425 | SetDictInt("ACS_HLINE", (ACS_HLINE)); |
| 2426 | SetDictInt("ACS_VLINE", (ACS_VLINE)); |
| 2427 | SetDictInt("ACS_PLUS", (ACS_PLUS)); |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 2428 | #if !defined(__hpux) || defined(HAVE_NCURSES_H) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2429 | /* On HP/UX 11, these are of type cchar_t, which is not an |
| 2430 | integral type. If this is a problem on more platforms, a |
| 2431 | configure test should be added to determine whether ACS_S1 |
| 2432 | is of integral type. */ |
| 2433 | SetDictInt("ACS_S1", (ACS_S1)); |
| 2434 | SetDictInt("ACS_S9", (ACS_S9)); |
| 2435 | SetDictInt("ACS_DIAMOND", (ACS_DIAMOND)); |
| 2436 | SetDictInt("ACS_CKBOARD", (ACS_CKBOARD)); |
| 2437 | SetDictInt("ACS_DEGREE", (ACS_DEGREE)); |
| 2438 | SetDictInt("ACS_PLMINUS", (ACS_PLMINUS)); |
| 2439 | SetDictInt("ACS_BULLET", (ACS_BULLET)); |
| 2440 | SetDictInt("ACS_LARROW", (ACS_LARROW)); |
| 2441 | SetDictInt("ACS_RARROW", (ACS_RARROW)); |
| 2442 | SetDictInt("ACS_DARROW", (ACS_DARROW)); |
| 2443 | SetDictInt("ACS_UARROW", (ACS_UARROW)); |
| 2444 | SetDictInt("ACS_BOARD", (ACS_BOARD)); |
| 2445 | SetDictInt("ACS_LANTERN", (ACS_LANTERN)); |
| 2446 | SetDictInt("ACS_BLOCK", (ACS_BLOCK)); |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 2447 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2448 | SetDictInt("ACS_BSSB", (ACS_ULCORNER)); |
| 2449 | SetDictInt("ACS_SSBB", (ACS_LLCORNER)); |
| 2450 | SetDictInt("ACS_BBSS", (ACS_URCORNER)); |
| 2451 | SetDictInt("ACS_SBBS", (ACS_LRCORNER)); |
| 2452 | SetDictInt("ACS_SBSS", (ACS_RTEE)); |
| 2453 | SetDictInt("ACS_SSSB", (ACS_LTEE)); |
| 2454 | SetDictInt("ACS_SSBS", (ACS_BTEE)); |
| 2455 | SetDictInt("ACS_BSSS", (ACS_TTEE)); |
| 2456 | SetDictInt("ACS_BSBS", (ACS_HLINE)); |
| 2457 | SetDictInt("ACS_SBSB", (ACS_VLINE)); |
| 2458 | SetDictInt("ACS_SSSS", (ACS_PLUS)); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 2459 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2460 | /* The following are never available with strict SYSV curses */ |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 2461 | #ifdef ACS_S3 |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2462 | SetDictInt("ACS_S3", (ACS_S3)); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 2463 | #endif |
| 2464 | #ifdef ACS_S7 |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2465 | SetDictInt("ACS_S7", (ACS_S7)); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 2466 | #endif |
| 2467 | #ifdef ACS_LEQUAL |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2468 | SetDictInt("ACS_LEQUAL", (ACS_LEQUAL)); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 2469 | #endif |
| 2470 | #ifdef ACS_GEQUAL |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2471 | SetDictInt("ACS_GEQUAL", (ACS_GEQUAL)); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 2472 | #endif |
| 2473 | #ifdef ACS_PI |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2474 | SetDictInt("ACS_PI", (ACS_PI)); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 2475 | #endif |
| 2476 | #ifdef ACS_NEQUAL |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2477 | SetDictInt("ACS_NEQUAL", (ACS_NEQUAL)); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 2478 | #endif |
| 2479 | #ifdef ACS_STERLING |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2480 | SetDictInt("ACS_STERLING", (ACS_STERLING)); |
Andrew M. Kuchling | 03e644b | 2000-05-23 16:24:54 +0000 | [diff] [blame] | 2481 | #endif |
| 2482 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2483 | SetDictInt("LINES", LINES); |
| 2484 | SetDictInt("COLS", COLS); |
Guido van Rossum | 56bf235 | 1994-08-31 22:06:24 +0000 | [diff] [blame] | 2485 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2486 | winobj = (PyCursesWindowObject *)PyCursesWindow_New(win, NULL); |
| 2487 | screen_encoding = winobj->encoding; |
| 2488 | return (PyObject *)winobj; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 2491 | static PyObject * |
| 2492 | PyCurses_setupterm(PyObject* self, PyObject *args, PyObject* keywds) |
| 2493 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2494 | int fd = -1; |
| 2495 | int err; |
| 2496 | char* termstr = NULL; |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 2497 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2498 | static char *kwlist[] = {"term", "fd", NULL}; |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 2499 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2500 | if (!PyArg_ParseTupleAndKeywords( |
| 2501 | args, keywds, "|zi:setupterm", kwlist, &termstr, &fd)) { |
| 2502 | return NULL; |
| 2503 | } |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 2504 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2505 | if (fd == -1) { |
| 2506 | PyObject* sys_stdout; |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 2507 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 2508 | sys_stdout = PySys_GetObject("stdout"); |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 2509 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2510 | if (sys_stdout == NULL || sys_stdout == Py_None) { |
| 2511 | PyErr_SetString( |
| 2512 | PyCursesError, |
| 2513 | "lost sys.stdout"); |
| 2514 | return NULL; |
| 2515 | } |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 2516 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2517 | fd = PyObject_AsFileDescriptor(sys_stdout); |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 2518 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2519 | if (fd == -1) { |
| 2520 | return NULL; |
| 2521 | } |
| 2522 | } |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 2523 | |
Matthias Klose | 635edd1 | 2010-07-30 21:40:57 +0000 | [diff] [blame] | 2524 | if (!initialised_setupterm && setupterm(termstr,fd,&err) == ERR) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2525 | char* s = "setupterm: unknown error"; |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 2526 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2527 | if (err == 0) { |
| 2528 | s = "setupterm: could not find terminal"; |
| 2529 | } else if (err == -1) { |
| 2530 | s = "setupterm: could not find terminfo database"; |
| 2531 | } |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 2532 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2533 | PyErr_SetString(PyCursesError,s); |
| 2534 | return NULL; |
| 2535 | } |
| 2536 | |
| 2537 | initialised_setupterm = TRUE; |
| 2538 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2539 | Py_RETURN_NONE; |
Andrew M. Kuchling | 26cc66f | 2000-11-18 17:45:59 +0000 | [diff] [blame] | 2540 | } |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2541 | |
| 2542 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2543 | PyCurses_IntrFlush(PyObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2544 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2545 | int ch; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2546 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2547 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2548 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2549 | switch(PyTuple_Size(args)) { |
| 2550 | case 1: |
| 2551 | if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL; |
| 2552 | break; |
| 2553 | default: |
| 2554 | PyErr_SetString(PyExc_TypeError, "intrflush requires 1 argument"); |
| 2555 | return NULL; |
| 2556 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2557 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2558 | return PyCursesCheckERR(intrflush(NULL,ch), "intrflush"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2561 | #ifdef HAVE_CURSES_IS_TERM_RESIZED |
| 2562 | static PyObject * |
| 2563 | PyCurses_Is_Term_Resized(PyObject *self, PyObject *args) |
| 2564 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2565 | int lines; |
| 2566 | int columns; |
| 2567 | int result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2568 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2569 | PyCursesInitialised; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2570 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2571 | if (!PyArg_ParseTuple(args,"ii:is_term_resized", &lines, &columns)) |
| 2572 | return NULL; |
| 2573 | result = is_term_resized(lines, columns); |
| 2574 | if (result == TRUE) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2575 | Py_RETURN_TRUE; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2576 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2577 | Py_RETURN_FALSE; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2578 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2579 | } |
| 2580 | #endif /* HAVE_CURSES_IS_TERM_RESIZED */ |
| 2581 | |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 2582 | #if !defined(__NetBSD__) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2583 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2584 | PyCurses_KeyName(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2585 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2586 | const char *knp; |
| 2587 | int ch; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2588 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2589 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2590 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2591 | if (!PyArg_ParseTuple(args,"i",&ch)) return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2592 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2593 | if (ch < 0) { |
| 2594 | PyErr_SetString(PyExc_ValueError, "invalid key number"); |
| 2595 | return NULL; |
| 2596 | } |
| 2597 | knp = keyname(ch); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2598 | |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 2599 | return PyBytes_FromString((knp == NULL) ? "" : knp); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2600 | } |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 2601 | #endif |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2602 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2603 | static PyObject * |
| 2604 | PyCurses_KillChar(PyObject *self) |
| 2605 | { |
| 2606 | char ch; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2607 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2608 | ch = killchar(); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2609 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2610 | return PyBytes_FromStringAndSize(&ch, 1); |
| 2611 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2612 | |
| 2613 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2614 | PyCurses_Meta(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2615 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2616 | int ch; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2617 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2618 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2619 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2620 | switch(PyTuple_Size(args)) { |
| 2621 | case 1: |
| 2622 | if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&ch)) return NULL; |
| 2623 | break; |
| 2624 | default: |
| 2625 | PyErr_SetString(PyExc_TypeError, "meta requires 1 argument"); |
| 2626 | return NULL; |
| 2627 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2628 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2629 | return PyCursesCheckERR(meta(stdscr, ch), "meta"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2630 | } |
| 2631 | |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2632 | #ifdef NCURSES_MOUSE_VERSION |
| 2633 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2634 | PyCurses_MouseInterval(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2635 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2636 | int interval; |
| 2637 | PyCursesInitialised; |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2638 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2639 | if (!PyArg_ParseTuple(args,"i;interval",&interval)) |
| 2640 | return NULL; |
| 2641 | return PyCursesCheckERR(mouseinterval(interval), "mouseinterval"); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2645 | PyCurses_MouseMask(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2646 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2647 | int newmask; |
| 2648 | mmask_t oldmask, availmask; |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2649 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2650 | PyCursesInitialised; |
| 2651 | if (!PyArg_ParseTuple(args,"i;mousemask",&newmask)) |
| 2652 | return NULL; |
| 2653 | availmask = mousemask(newmask, &oldmask); |
| 2654 | return Py_BuildValue("(ll)", (long)availmask, (long)oldmask); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 2655 | } |
| 2656 | #endif |
| 2657 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2658 | static PyObject * |
Andrew M. Kuchling | 2824d7f | 2000-12-15 00:44:02 +0000 | [diff] [blame] | 2659 | PyCurses_Napms(PyObject *self, PyObject *args) |
| 2660 | { |
| 2661 | int ms; |
| 2662 | |
Andrew M. Kuchling | 2b14676 | 2010-05-10 23:30:25 +0000 | [diff] [blame] | 2663 | PyCursesInitialised; |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 2664 | if (!PyArg_ParseTuple(args, "i;ms", &ms)) return NULL; |
Andrew M. Kuchling | 2824d7f | 2000-12-15 00:44:02 +0000 | [diff] [blame] | 2665 | |
| 2666 | return Py_BuildValue("i", napms(ms)); |
| 2667 | } |
| 2668 | |
| 2669 | |
| 2670 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2671 | PyCurses_NewPad(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2672 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2673 | WINDOW *win; |
| 2674 | int nlines, ncols; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2675 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2676 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2677 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2678 | if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols)) return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2679 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2680 | win = newpad(nlines, ncols); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2681 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2682 | if (win == NULL) { |
| 2683 | PyErr_SetString(PyCursesError, catchall_NULL); |
| 2684 | return NULL; |
| 2685 | } |
| 2686 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2687 | return (PyObject *)PyCursesWindow_New(win, NULL); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
| 2690 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2691 | PyCurses_NewWindow(PyObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2692 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2693 | WINDOW *win; |
| 2694 | int nlines, ncols, begin_y=0, begin_x=0; |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 2695 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2696 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2697 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2698 | switch (PyTuple_Size(args)) { |
| 2699 | case 2: |
| 2700 | if (!PyArg_ParseTuple(args,"ii;nlines,ncols",&nlines,&ncols)) |
| 2701 | return NULL; |
| 2702 | break; |
| 2703 | case 4: |
| 2704 | if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x", |
| 2705 | &nlines,&ncols,&begin_y,&begin_x)) |
| 2706 | return NULL; |
| 2707 | break; |
| 2708 | default: |
| 2709 | PyErr_SetString(PyExc_TypeError, "newwin requires 2 or 4 arguments"); |
| 2710 | return NULL; |
| 2711 | } |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 2712 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2713 | win = newwin(nlines,ncols,begin_y,begin_x); |
| 2714 | if (win == NULL) { |
| 2715 | PyErr_SetString(PyCursesError, catchall_NULL); |
| 2716 | return NULL; |
| 2717 | } |
Guido van Rossum | 8573847 | 1995-02-17 13:50:17 +0000 | [diff] [blame] | 2718 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 2719 | return (PyObject *)PyCursesWindow_New(win, NULL); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2723 | PyCurses_Pair_Content(PyObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2724 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2725 | short pair,f,b; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2726 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2727 | PyCursesInitialised; |
| 2728 | PyCursesInitialisedColor; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2729 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2730 | switch(PyTuple_Size(args)) { |
| 2731 | case 1: |
| 2732 | if (!PyArg_ParseTuple(args, "h;pair", &pair)) return NULL; |
| 2733 | break; |
| 2734 | default: |
| 2735 | PyErr_SetString(PyExc_TypeError, "pair_content requires 1 argument"); |
| 2736 | return NULL; |
| 2737 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2738 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2739 | if (pair_content(pair, &f, &b)==ERR) { |
| 2740 | PyErr_SetString(PyCursesError, |
| 2741 | "Argument 1 was out of range. (1..COLOR_PAIRS-1)"); |
| 2742 | return NULL; |
| 2743 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2744 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2745 | return Py_BuildValue("(ii)", f, b); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2749 | PyCurses_pair_number(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2750 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2751 | int n; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2752 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2753 | PyCursesInitialised; |
| 2754 | PyCursesInitialisedColor; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2755 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2756 | switch(PyTuple_Size(args)) { |
| 2757 | case 1: |
| 2758 | if (!PyArg_ParseTuple(args, "i;pairvalue", &n)) return NULL; |
| 2759 | break; |
| 2760 | default: |
| 2761 | PyErr_SetString(PyExc_TypeError, |
| 2762 | "pair_number requires 1 argument"); |
| 2763 | return NULL; |
| 2764 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2765 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2766 | return PyLong_FromLong((long) ((n & A_COLOR) >> 8)); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2770 | PyCurses_Putp(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2771 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2772 | char *str; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2773 | |
Victor Stinner | b3bc7e7 | 2011-11-03 20:35:40 +0100 | [diff] [blame] | 2774 | if (!PyArg_ParseTuple(args,"y;str", &str)) |
| 2775 | return NULL; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2776 | return PyCursesCheckERR(putp(str), "putp"); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2780 | PyCurses_QiFlush(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2781 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2782 | int flag = 0; |
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 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2786 | switch(PyTuple_Size(args)) { |
| 2787 | case 0: |
| 2788 | qiflush(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2789 | Py_RETURN_NONE; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2790 | case 1: |
| 2791 | if (!PyArg_ParseTuple(args, "i;True(1) or False(0)", &flag)) return NULL; |
| 2792 | if (flag) qiflush(); |
| 2793 | else noqiflush(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2794 | Py_RETURN_NONE; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2795 | default: |
| 2796 | PyErr_SetString(PyExc_TypeError, "qiflush requires 0 or 1 arguments"); |
| 2797 | return NULL; |
| 2798 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2801 | /* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES |
| 2802 | * and _curses.COLS */ |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 2803 | #if defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2804 | static int |
| 2805 | update_lines_cols(void) |
| 2806 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2807 | PyObject *o; |
| 2808 | PyObject *m = PyImport_ImportModuleNoBlock("curses"); |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 2809 | _Py_IDENTIFIER(LINES); |
| 2810 | _Py_IDENTIFIER(COLS); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2811 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2812 | if (!m) |
| 2813 | return 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2814 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2815 | o = PyLong_FromLong(LINES); |
| 2816 | if (!o) { |
| 2817 | Py_DECREF(m); |
| 2818 | return 0; |
| 2819 | } |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 2820 | if (_PyObject_SetAttrId(m, &PyId_LINES, o)) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2821 | Py_DECREF(m); |
| 2822 | Py_DECREF(o); |
| 2823 | return 0; |
| 2824 | } |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 2825 | /* PyId_LINES.object will be initialized here. */ |
| 2826 | if (PyDict_SetItem(ModDict, PyId_LINES.object, o)) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2827 | Py_DECREF(m); |
| 2828 | Py_DECREF(o); |
| 2829 | return 0; |
| 2830 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2831 | Py_DECREF(o); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2832 | o = PyLong_FromLong(COLS); |
| 2833 | if (!o) { |
| 2834 | Py_DECREF(m); |
| 2835 | return 0; |
| 2836 | } |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 2837 | if (_PyObject_SetAttrId(m, &PyId_COLS, o)) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2838 | Py_DECREF(m); |
| 2839 | Py_DECREF(o); |
| 2840 | return 0; |
| 2841 | } |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 2842 | if (PyDict_SetItem(ModDict, PyId_COLS.object, o)) { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2843 | Py_DECREF(m); |
| 2844 | Py_DECREF(o); |
| 2845 | return 0; |
| 2846 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2847 | Py_DECREF(o); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2848 | Py_DECREF(m); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2849 | return 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2850 | } |
Steve Dower | d2bc389 | 2015-04-15 18:06:05 -0400 | [diff] [blame] | 2851 | |
| 2852 | static PyObject * |
| 2853 | PyCurses_update_lines_cols(PyObject *self) |
| 2854 | { |
| 2855 | return PyLong_FromLong((long) update_lines_cols()); |
| 2856 | } |
| 2857 | |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 2858 | #endif |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2859 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2860 | #ifdef HAVE_CURSES_RESIZETERM |
| 2861 | static PyObject * |
| 2862 | PyCurses_ResizeTerm(PyObject *self, PyObject *args) |
| 2863 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2864 | int lines; |
| 2865 | int columns; |
| 2866 | PyObject *result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2867 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2868 | PyCursesInitialised; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2869 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2870 | if (!PyArg_ParseTuple(args,"ii:resizeterm", &lines, &columns)) |
| 2871 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2872 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2873 | result = PyCursesCheckERR(resizeterm(lines, columns), "resizeterm"); |
| 2874 | if (!result) |
| 2875 | return NULL; |
| 2876 | if (!update_lines_cols()) |
| 2877 | return NULL; |
| 2878 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | #endif |
| 2882 | |
| 2883 | #ifdef HAVE_CURSES_RESIZE_TERM |
| 2884 | static PyObject * |
| 2885 | PyCurses_Resize_Term(PyObject *self, PyObject *args) |
| 2886 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2887 | int lines; |
| 2888 | int columns; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2889 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2890 | PyObject *result; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2891 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2892 | PyCursesInitialised; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2893 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2894 | if (!PyArg_ParseTuple(args,"ii:resize_term", &lines, &columns)) |
| 2895 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2896 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2897 | result = PyCursesCheckERR(resize_term(lines, columns), "resize_term"); |
| 2898 | if (!result) |
| 2899 | return NULL; |
| 2900 | if (!update_lines_cols()) |
| 2901 | return NULL; |
| 2902 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2903 | } |
| 2904 | #endif /* HAVE_CURSES_RESIZE_TERM */ |
| 2905 | |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2906 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 2907 | PyCurses_setsyx(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2908 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2909 | int y,x; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2910 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2911 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2912 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2913 | if (PyTuple_Size(args)!=2) { |
| 2914 | PyErr_SetString(PyExc_TypeError, "setsyx requires 2 arguments"); |
| 2915 | return NULL; |
| 2916 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2917 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2918 | if (!PyArg_ParseTuple(args, "ii;y, x", &y, &x)) return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2919 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2920 | setsyx(y,x); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2921 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2922 | Py_RETURN_NONE; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
| 2925 | static PyObject * |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 2926 | PyCurses_Start_Color(PyObject *self) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2927 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2928 | int code; |
| 2929 | PyObject *c, *cp; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2930 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2931 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2932 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2933 | code = start_color(); |
| 2934 | if (code != ERR) { |
| 2935 | initialisedcolors = TRUE; |
| 2936 | c = PyLong_FromLong((long) COLORS); |
Christian Heimes | a956e64 | 2013-07-26 14:45:37 +0200 | [diff] [blame] | 2937 | if (c == NULL) |
| 2938 | return NULL; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2939 | PyDict_SetItemString(ModDict, "COLORS", c); |
| 2940 | Py_DECREF(c); |
| 2941 | cp = PyLong_FromLong((long) COLOR_PAIRS); |
Christian Heimes | a956e64 | 2013-07-26 14:45:37 +0200 | [diff] [blame] | 2942 | if (cp == NULL) |
| 2943 | return NULL; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2944 | PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp); |
| 2945 | Py_DECREF(cp); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2946 | Py_RETURN_NONE; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2947 | } else { |
| 2948 | PyErr_SetString(PyCursesError, "start_color() returned ERR"); |
| 2949 | return NULL; |
| 2950 | } |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 2951 | } |
| 2952 | |
| 2953 | static PyObject * |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 2954 | PyCurses_tigetflag(PyObject *self, PyObject *args) |
| 2955 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2956 | char *capname; |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 2957 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2958 | PyCursesSetupTermCalled; |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 2959 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2960 | if (!PyArg_ParseTuple(args, "s", &capname)) |
| 2961 | return NULL; |
| 2962 | |
| 2963 | return PyLong_FromLong( (long) tigetflag( capname ) ); |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | static PyObject * |
| 2967 | PyCurses_tigetnum(PyObject *self, PyObject *args) |
| 2968 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2969 | char *capname; |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 2970 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2971 | PyCursesSetupTermCalled; |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 2972 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2973 | if (!PyArg_ParseTuple(args, "s", &capname)) |
| 2974 | return NULL; |
| 2975 | |
| 2976 | return PyLong_FromLong( (long) tigetnum( capname ) ); |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | static PyObject * |
| 2980 | PyCurses_tigetstr(PyObject *self, PyObject *args) |
| 2981 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2982 | char *capname; |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 2983 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2984 | PyCursesSetupTermCalled; |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 2985 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2986 | if (!PyArg_ParseTuple(args, "s", &capname)) |
| 2987 | return NULL; |
| 2988 | |
| 2989 | capname = tigetstr( capname ); |
| 2990 | if (capname == 0 || capname == (char*) -1) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2991 | Py_RETURN_NONE; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2992 | } |
| 2993 | return PyBytes_FromString( capname ); |
Andrew M. Kuchling | 70e89c1 | 2000-07-26 02:57:20 +0000 | [diff] [blame] | 2994 | } |
| 2995 | |
| 2996 | static PyObject * |
Andrew M. Kuchling | f16e0ed | 2000-11-07 03:35:24 +0000 | [diff] [blame] | 2997 | PyCurses_tparm(PyObject *self, PyObject *args) |
| 2998 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 2999 | char* fmt; |
| 3000 | char* result = NULL; |
| 3001 | int i1=0,i2=0,i3=0,i4=0,i5=0,i6=0,i7=0,i8=0,i9=0; |
Andrew M. Kuchling | f16e0ed | 2000-11-07 03:35:24 +0000 | [diff] [blame] | 3002 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3003 | PyCursesSetupTermCalled; |
Andrew M. Kuchling | f16e0ed | 2000-11-07 03:35:24 +0000 | [diff] [blame] | 3004 | |
Victor Stinner | 2662133 | 2011-11-02 23:45:29 +0100 | [diff] [blame] | 3005 | if (!PyArg_ParseTuple(args, "y|iiiiiiiii:tparm", |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3006 | &fmt, &i1, &i2, &i3, &i4, |
| 3007 | &i5, &i6, &i7, &i8, &i9)) { |
| 3008 | return NULL; |
| 3009 | } |
Michael W. Hudson | 2100798 | 2002-11-21 14:17:51 +0000 | [diff] [blame] | 3010 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3011 | result = tparm(fmt,i1,i2,i3,i4,i5,i6,i7,i8,i9); |
| 3012 | if (!result) { |
| 3013 | PyErr_SetString(PyCursesError, "tparm() returned NULL"); |
| 3014 | return NULL; |
| 3015 | } |
Michael W. Hudson | 2100798 | 2002-11-21 14:17:51 +0000 | [diff] [blame] | 3016 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3017 | return PyBytes_FromString(result); |
Andrew M. Kuchling | f16e0ed | 2000-11-07 03:35:24 +0000 | [diff] [blame] | 3018 | } |
| 3019 | |
| 3020 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 3021 | PyCurses_TypeAhead(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 97311bb | 2000-06-21 01:41:48 +0000 | [diff] [blame] | 3022 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3023 | int fd; |
Andrew M. Kuchling | 97311bb | 2000-06-21 01:41:48 +0000 | [diff] [blame] | 3024 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3025 | PyCursesInitialised; |
Andrew M. Kuchling | 97311bb | 2000-06-21 01:41:48 +0000 | [diff] [blame] | 3026 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3027 | if (!PyArg_ParseTuple(args,"i;fd",&fd)) return NULL; |
Andrew M. Kuchling | 97311bb | 2000-06-21 01:41:48 +0000 | [diff] [blame] | 3028 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3029 | return PyCursesCheckERR(typeahead( fd ), "typeahead"); |
Andrew M. Kuchling | 97311bb | 2000-06-21 01:41:48 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 3033 | PyCurses_UnCtrl(PyObject *self, PyObject *args) |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3034 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3035 | PyObject *temp; |
| 3036 | chtype ch; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3037 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3038 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3039 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3040 | if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3041 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 3042 | if (!PyCurses_ConvertToChtype(NULL, temp, &ch)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3043 | return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3044 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3045 | return PyBytes_FromString(unctrl(ch)); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3046 | } |
| 3047 | |
| 3048 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 3049 | PyCurses_UngetCh(PyObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3050 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3051 | PyObject *temp; |
| 3052 | chtype ch; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3053 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3054 | PyCursesInitialised; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3055 | |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 3056 | if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3057 | return NULL; |
Victor Stinner | 0fdfceb | 2011-11-25 22:10:02 +0100 | [diff] [blame] | 3058 | |
| 3059 | if (!PyCurses_ConvertToChtype(NULL, temp, &ch)) |
| 3060 | return NULL; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3061 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3062 | return PyCursesCheckERR(ungetch(ch), "ungetch"); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3063 | } |
| 3064 | |
Victor Stinner | 71e44cb | 2011-09-06 01:53:03 +0200 | [diff] [blame] | 3065 | #ifdef HAVE_NCURSESW |
| 3066 | /* Convert an object to a character (wchar_t): |
| 3067 | |
| 3068 | - int |
| 3069 | - str of length 1 |
| 3070 | |
| 3071 | Return 1 on success, 0 on error. */ |
| 3072 | static int |
| 3073 | PyCurses_ConvertToWchar_t(PyObject *obj, |
| 3074 | wchar_t *wch) |
| 3075 | { |
| 3076 | if (PyUnicode_Check(obj)) { |
| 3077 | wchar_t buffer[2]; |
| 3078 | if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) { |
| 3079 | PyErr_Format(PyExc_TypeError, |
| 3080 | "expect bytes or str of length 1, or int, " |
| 3081 | "got a str of length %zi", |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 3082 | PyUnicode_GET_LENGTH(obj)); |
Victor Stinner | 71e44cb | 2011-09-06 01:53:03 +0200 | [diff] [blame] | 3083 | return 0; |
| 3084 | } |
| 3085 | *wch = buffer[0]; |
| 3086 | return 2; |
| 3087 | } |
| 3088 | else if (PyLong_CheckExact(obj)) { |
| 3089 | long value; |
| 3090 | int overflow; |
| 3091 | value = PyLong_AsLongAndOverflow(obj, &overflow); |
| 3092 | if (overflow) { |
| 3093 | PyErr_SetString(PyExc_OverflowError, |
| 3094 | "int doesn't fit in long"); |
| 3095 | return 0; |
| 3096 | } |
| 3097 | *wch = (wchar_t)value; |
| 3098 | if ((long)*wch != value) { |
| 3099 | PyErr_Format(PyExc_OverflowError, |
| 3100 | "character doesn't fit in wchar_t"); |
| 3101 | return 0; |
| 3102 | } |
| 3103 | return 1; |
| 3104 | } |
| 3105 | else { |
| 3106 | PyErr_Format(PyExc_TypeError, |
| 3107 | "expect bytes or str of length 1, or int, got %s", |
| 3108 | Py_TYPE(obj)->tp_name); |
| 3109 | return 0; |
| 3110 | } |
| 3111 | } |
| 3112 | |
| 3113 | static PyObject * |
| 3114 | PyCurses_Unget_Wch(PyObject *self, PyObject *args) |
| 3115 | { |
| 3116 | PyObject *obj; |
| 3117 | wchar_t wch; |
| 3118 | |
| 3119 | PyCursesInitialised; |
| 3120 | |
| 3121 | if (!PyArg_ParseTuple(args,"O", &obj)) |
| 3122 | return NULL; |
| 3123 | |
| 3124 | if (!PyCurses_ConvertToWchar_t(obj, &wch)) |
| 3125 | return NULL; |
| 3126 | return PyCursesCheckERR(unget_wch(wch), "unget_wch"); |
| 3127 | } |
| 3128 | #endif |
| 3129 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3130 | static PyObject * |
Andrew M. Kuchling | ad9b1f8 | 2000-07-09 14:35:00 +0000 | [diff] [blame] | 3131 | PyCurses_Use_Env(PyObject *self, PyObject *args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3132 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3133 | int flag; |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3134 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3135 | switch(PyTuple_Size(args)) { |
| 3136 | case 1: |
| 3137 | if (!PyArg_ParseTuple(args,"i;True(1), False(0)",&flag)) |
| 3138 | return NULL; |
| 3139 | break; |
| 3140 | default: |
| 3141 | PyErr_SetString(PyExc_TypeError, "use_env requires 1 argument"); |
| 3142 | return NULL; |
| 3143 | } |
| 3144 | use_env(flag); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 3145 | Py_RETURN_NONE; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 3148 | #ifndef STRICT_SYSV_CURSES |
| 3149 | static PyObject * |
| 3150 | PyCurses_Use_Default_Colors(PyObject *self) |
| 3151 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3152 | int code; |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 3153 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3154 | PyCursesInitialised; |
| 3155 | PyCursesInitialisedColor; |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 3156 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3157 | code = use_default_colors(); |
| 3158 | if (code != ERR) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 3159 | Py_RETURN_NONE; |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3160 | } else { |
| 3161 | PyErr_SetString(PyCursesError, "use_default_colors() returned ERR"); |
| 3162 | return NULL; |
| 3163 | } |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 3164 | } |
| 3165 | #endif /* STRICT_SYSV_CURSES */ |
| 3166 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3167 | /* List of functions defined in the module */ |
| 3168 | |
| 3169 | static PyMethodDef PyCurses_methods[] = { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3170 | {"baudrate", (PyCFunction)PyCurses_baudrate, METH_NOARGS}, |
| 3171 | {"beep", (PyCFunction)PyCurses_beep, METH_NOARGS}, |
| 3172 | {"can_change_color", (PyCFunction)PyCurses_can_change_color, METH_NOARGS}, |
| 3173 | {"cbreak", (PyCFunction)PyCurses_cbreak, METH_VARARGS}, |
| 3174 | {"color_content", (PyCFunction)PyCurses_Color_Content, METH_VARARGS}, |
| 3175 | {"color_pair", (PyCFunction)PyCurses_color_pair, METH_VARARGS}, |
| 3176 | {"curs_set", (PyCFunction)PyCurses_Curs_Set, METH_VARARGS}, |
| 3177 | {"def_prog_mode", (PyCFunction)PyCurses_def_prog_mode, METH_NOARGS}, |
| 3178 | {"def_shell_mode", (PyCFunction)PyCurses_def_shell_mode, METH_NOARGS}, |
| 3179 | {"delay_output", (PyCFunction)PyCurses_Delay_Output, METH_VARARGS}, |
| 3180 | {"doupdate", (PyCFunction)PyCurses_doupdate, METH_NOARGS}, |
| 3181 | {"echo", (PyCFunction)PyCurses_echo, METH_VARARGS}, |
| 3182 | {"endwin", (PyCFunction)PyCurses_endwin, METH_NOARGS}, |
| 3183 | {"erasechar", (PyCFunction)PyCurses_EraseChar, METH_NOARGS}, |
| 3184 | {"filter", (PyCFunction)PyCurses_filter, METH_NOARGS}, |
| 3185 | {"flash", (PyCFunction)PyCurses_flash, METH_NOARGS}, |
| 3186 | {"flushinp", (PyCFunction)PyCurses_flushinp, METH_NOARGS}, |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3187 | #ifdef NCURSES_MOUSE_VERSION |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3188 | {"getmouse", (PyCFunction)PyCurses_GetMouse, METH_NOARGS}, |
| 3189 | {"ungetmouse", (PyCFunction)PyCurses_UngetMouse, METH_VARARGS}, |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3190 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3191 | {"getsyx", (PyCFunction)PyCurses_getsyx, METH_NOARGS}, |
| 3192 | {"getwin", (PyCFunction)PyCurses_GetWin, METH_O}, |
| 3193 | {"has_colors", (PyCFunction)PyCurses_has_colors, METH_NOARGS}, |
| 3194 | {"has_ic", (PyCFunction)PyCurses_has_ic, METH_NOARGS}, |
| 3195 | {"has_il", (PyCFunction)PyCurses_has_il, METH_NOARGS}, |
Andrew M. Kuchling | bf68026 | 2000-07-25 12:37:41 +0000 | [diff] [blame] | 3196 | #ifndef STRICT_SYSV_CURSES |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3197 | {"has_key", (PyCFunction)PyCurses_has_key, METH_VARARGS}, |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3198 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3199 | {"halfdelay", (PyCFunction)PyCurses_HalfDelay, METH_VARARGS}, |
| 3200 | {"init_color", (PyCFunction)PyCurses_Init_Color, METH_VARARGS}, |
| 3201 | {"init_pair", (PyCFunction)PyCurses_Init_Pair, METH_VARARGS}, |
| 3202 | {"initscr", (PyCFunction)PyCurses_InitScr, METH_NOARGS}, |
| 3203 | {"intrflush", (PyCFunction)PyCurses_IntrFlush, METH_VARARGS}, |
| 3204 | {"isendwin", (PyCFunction)PyCurses_isendwin, METH_NOARGS}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3205 | #ifdef HAVE_CURSES_IS_TERM_RESIZED |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3206 | {"is_term_resized", (PyCFunction)PyCurses_Is_Term_Resized, METH_VARARGS}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3207 | #endif |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 3208 | #if !defined(__NetBSD__) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3209 | {"keyname", (PyCFunction)PyCurses_KeyName, METH_VARARGS}, |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 3210 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3211 | {"killchar", (PyCFunction)PyCurses_KillChar, METH_NOARGS}, |
| 3212 | {"longname", (PyCFunction)PyCurses_longname, METH_NOARGS}, |
| 3213 | {"meta", (PyCFunction)PyCurses_Meta, METH_VARARGS}, |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3214 | #ifdef NCURSES_MOUSE_VERSION |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3215 | {"mouseinterval", (PyCFunction)PyCurses_MouseInterval, METH_VARARGS}, |
| 3216 | {"mousemask", (PyCFunction)PyCurses_MouseMask, METH_VARARGS}, |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3217 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3218 | {"napms", (PyCFunction)PyCurses_Napms, METH_VARARGS}, |
| 3219 | {"newpad", (PyCFunction)PyCurses_NewPad, METH_VARARGS}, |
| 3220 | {"newwin", (PyCFunction)PyCurses_NewWindow, METH_VARARGS}, |
| 3221 | {"nl", (PyCFunction)PyCurses_nl, METH_VARARGS}, |
| 3222 | {"nocbreak", (PyCFunction)PyCurses_nocbreak, METH_NOARGS}, |
| 3223 | {"noecho", (PyCFunction)PyCurses_noecho, METH_NOARGS}, |
| 3224 | {"nonl", (PyCFunction)PyCurses_nonl, METH_NOARGS}, |
| 3225 | {"noqiflush", (PyCFunction)PyCurses_noqiflush, METH_NOARGS}, |
| 3226 | {"noraw", (PyCFunction)PyCurses_noraw, METH_NOARGS}, |
| 3227 | {"pair_content", (PyCFunction)PyCurses_Pair_Content, METH_VARARGS}, |
| 3228 | {"pair_number", (PyCFunction)PyCurses_pair_number, METH_VARARGS}, |
| 3229 | {"putp", (PyCFunction)PyCurses_Putp, METH_VARARGS}, |
| 3230 | {"qiflush", (PyCFunction)PyCurses_QiFlush, METH_VARARGS}, |
| 3231 | {"raw", (PyCFunction)PyCurses_raw, METH_VARARGS}, |
| 3232 | {"reset_prog_mode", (PyCFunction)PyCurses_reset_prog_mode, METH_NOARGS}, |
| 3233 | {"reset_shell_mode", (PyCFunction)PyCurses_reset_shell_mode, METH_NOARGS}, |
| 3234 | {"resetty", (PyCFunction)PyCurses_resetty, METH_NOARGS}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3235 | #ifdef HAVE_CURSES_RESIZETERM |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3236 | {"resizeterm", (PyCFunction)PyCurses_ResizeTerm, METH_VARARGS}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3237 | #endif |
| 3238 | #ifdef HAVE_CURSES_RESIZE_TERM |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3239 | {"resize_term", (PyCFunction)PyCurses_Resize_Term, METH_VARARGS}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3240 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3241 | {"savetty", (PyCFunction)PyCurses_savetty, METH_NOARGS}, |
| 3242 | {"setsyx", (PyCFunction)PyCurses_setsyx, METH_VARARGS}, |
| 3243 | {"setupterm", (PyCFunction)PyCurses_setupterm, |
| 3244 | METH_VARARGS|METH_KEYWORDS}, |
| 3245 | {"start_color", (PyCFunction)PyCurses_Start_Color, METH_NOARGS}, |
| 3246 | {"termattrs", (PyCFunction)PyCurses_termattrs, METH_NOARGS}, |
| 3247 | {"termname", (PyCFunction)PyCurses_termname, METH_NOARGS}, |
| 3248 | {"tigetflag", (PyCFunction)PyCurses_tigetflag, METH_VARARGS}, |
| 3249 | {"tigetnum", (PyCFunction)PyCurses_tigetnum, METH_VARARGS}, |
| 3250 | {"tigetstr", (PyCFunction)PyCurses_tigetstr, METH_VARARGS}, |
| 3251 | {"tparm", (PyCFunction)PyCurses_tparm, METH_VARARGS}, |
| 3252 | {"typeahead", (PyCFunction)PyCurses_TypeAhead, METH_VARARGS}, |
| 3253 | {"unctrl", (PyCFunction)PyCurses_UnCtrl, METH_VARARGS}, |
| 3254 | {"ungetch", (PyCFunction)PyCurses_UngetCh, METH_VARARGS}, |
Steve Dower | d2bc389 | 2015-04-15 18:06:05 -0400 | [diff] [blame] | 3255 | #if defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM) |
| 3256 | {"update_lines_cols", (PyCFunction)PyCurses_update_lines_cols, METH_NOARGS}, |
| 3257 | #endif |
Victor Stinner | 71e44cb | 2011-09-06 01:53:03 +0200 | [diff] [blame] | 3258 | #ifdef HAVE_NCURSESW |
| 3259 | {"unget_wch", (PyCFunction)PyCurses_Unget_Wch, METH_VARARGS}, |
| 3260 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3261 | {"use_env", (PyCFunction)PyCurses_Use_Env, METH_VARARGS}, |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 3262 | #ifndef STRICT_SYSV_CURSES |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3263 | {"use_default_colors", (PyCFunction)PyCurses_Use_Default_Colors, METH_NOARGS}, |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 3264 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3265 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3266 | }; |
| 3267 | |
| 3268 | /* Initialization function for the module */ |
| 3269 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3270 | |
| 3271 | static struct PyModuleDef _cursesmodule = { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3272 | PyModuleDef_HEAD_INIT, |
| 3273 | "_curses", |
| 3274 | NULL, |
| 3275 | -1, |
| 3276 | PyCurses_methods, |
| 3277 | NULL, |
| 3278 | NULL, |
| 3279 | NULL, |
| 3280 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3281 | }; |
| 3282 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3283 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3284 | PyInit__curses(void) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3285 | { |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3286 | PyObject *m, *d, *v, *c_api_object; |
| 3287 | static void *PyCurses_API[PyCurses_API_pointers]; |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 3288 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3289 | /* Initialize object type */ |
| 3290 | if (PyType_Ready(&PyCursesWindow_Type) < 0) |
| 3291 | return NULL; |
Guido van Rossum | 6915c4d | 2001-01-19 00:28:08 +0000 | [diff] [blame] | 3292 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3293 | /* Initialize the C API pointer array */ |
| 3294 | PyCurses_API[0] = (void *)&PyCursesWindow_Type; |
| 3295 | PyCurses_API[1] = (void *)func_PyCursesSetupTermCalled; |
| 3296 | PyCurses_API[2] = (void *)func_PyCursesInitialised; |
| 3297 | PyCurses_API[3] = (void *)func_PyCursesInitialisedColor; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3298 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3299 | /* Create the module and add the functions */ |
| 3300 | m = PyModule_Create(&_cursesmodule); |
| 3301 | if (m == NULL) |
| 3302 | return NULL; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3303 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3304 | /* Add some symbolic constants to the module */ |
| 3305 | d = PyModule_GetDict(m); |
| 3306 | if (d == NULL) |
| 3307 | return NULL; |
| 3308 | ModDict = d; /* For PyCurses_InitScr to use later */ |
Andrew M. Kuchling | 3255268 | 2000-12-22 21:52:27 +0000 | [diff] [blame] | 3309 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3310 | /* Add a capsule for the C API */ |
| 3311 | c_api_object = PyCapsule_New(PyCurses_API, PyCurses_CAPSULE_NAME, NULL); |
| 3312 | PyDict_SetItemString(d, "_C_API", c_api_object); |
| 3313 | Py_DECREF(c_api_object); |
Guido van Rossum | fbea2f3 | 1994-08-31 22:05:27 +0000 | [diff] [blame] | 3314 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3315 | /* For exception curses.error */ |
| 3316 | PyCursesError = PyErr_NewException("_curses.error", NULL, NULL); |
| 3317 | PyDict_SetItemString(d, "error", PyCursesError); |
Guido van Rossum | fbea2f3 | 1994-08-31 22:05:27 +0000 | [diff] [blame] | 3318 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3319 | /* Make the version available */ |
| 3320 | v = PyBytes_FromString(PyCursesVersion); |
| 3321 | PyDict_SetItemString(d, "version", v); |
| 3322 | PyDict_SetItemString(d, "__version__", v); |
| 3323 | Py_DECREF(v); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3324 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3325 | SetDictInt("ERR", ERR); |
| 3326 | SetDictInt("OK", OK); |
Andrew M. Kuchling | caefb37 | 2000-12-26 15:57:01 +0000 | [diff] [blame] | 3327 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3328 | /* Here are some attributes you can add to chars to print */ |
| 3329 | |
| 3330 | SetDictInt("A_ATTRIBUTES", A_ATTRIBUTES); |
| 3331 | SetDictInt("A_NORMAL", A_NORMAL); |
| 3332 | SetDictInt("A_STANDOUT", A_STANDOUT); |
| 3333 | SetDictInt("A_UNDERLINE", A_UNDERLINE); |
| 3334 | SetDictInt("A_REVERSE", A_REVERSE); |
| 3335 | SetDictInt("A_BLINK", A_BLINK); |
| 3336 | SetDictInt("A_DIM", A_DIM); |
| 3337 | SetDictInt("A_BOLD", A_BOLD); |
| 3338 | SetDictInt("A_ALTCHARSET", A_ALTCHARSET); |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 3339 | #if !defined(__NetBSD__) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3340 | SetDictInt("A_INVIS", A_INVIS); |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 3341 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3342 | SetDictInt("A_PROTECT", A_PROTECT); |
| 3343 | SetDictInt("A_CHARTEXT", A_CHARTEXT); |
| 3344 | SetDictInt("A_COLOR", A_COLOR); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 3345 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3346 | /* The following are never available with strict SYSV curses */ |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 3347 | #ifdef A_HORIZONTAL |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3348 | SetDictInt("A_HORIZONTAL", A_HORIZONTAL); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 3349 | #endif |
| 3350 | #ifdef A_LEFT |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3351 | SetDictInt("A_LEFT", A_LEFT); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 3352 | #endif |
| 3353 | #ifdef A_LOW |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3354 | SetDictInt("A_LOW", A_LOW); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 3355 | #endif |
| 3356 | #ifdef A_RIGHT |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3357 | SetDictInt("A_RIGHT", A_RIGHT); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 3358 | #endif |
| 3359 | #ifdef A_TOP |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3360 | SetDictInt("A_TOP", A_TOP); |
Andrew M. Kuchling | e7d3616 | 2000-11-01 19:59:12 +0000 | [diff] [blame] | 3361 | #endif |
| 3362 | #ifdef A_VERTICAL |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3363 | SetDictInt("A_VERTICAL", A_VERTICAL); |
Andrew M. Kuchling | 22b88ce | 2000-05-23 16:18:03 +0000 | [diff] [blame] | 3364 | #endif |
Andrew M. Kuchling | 03e644b | 2000-05-23 16:24:54 +0000 | [diff] [blame] | 3365 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3366 | SetDictInt("COLOR_BLACK", COLOR_BLACK); |
| 3367 | SetDictInt("COLOR_RED", COLOR_RED); |
| 3368 | SetDictInt("COLOR_GREEN", COLOR_GREEN); |
| 3369 | SetDictInt("COLOR_YELLOW", COLOR_YELLOW); |
| 3370 | SetDictInt("COLOR_BLUE", COLOR_BLUE); |
| 3371 | SetDictInt("COLOR_MAGENTA", COLOR_MAGENTA); |
| 3372 | SetDictInt("COLOR_CYAN", COLOR_CYAN); |
| 3373 | SetDictInt("COLOR_WHITE", COLOR_WHITE); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3374 | |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3375 | #ifdef NCURSES_MOUSE_VERSION |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3376 | /* Mouse-related constants */ |
| 3377 | SetDictInt("BUTTON1_PRESSED", BUTTON1_PRESSED); |
| 3378 | SetDictInt("BUTTON1_RELEASED", BUTTON1_RELEASED); |
| 3379 | SetDictInt("BUTTON1_CLICKED", BUTTON1_CLICKED); |
| 3380 | SetDictInt("BUTTON1_DOUBLE_CLICKED", BUTTON1_DOUBLE_CLICKED); |
| 3381 | SetDictInt("BUTTON1_TRIPLE_CLICKED", BUTTON1_TRIPLE_CLICKED); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3382 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3383 | SetDictInt("BUTTON2_PRESSED", BUTTON2_PRESSED); |
| 3384 | SetDictInt("BUTTON2_RELEASED", BUTTON2_RELEASED); |
| 3385 | SetDictInt("BUTTON2_CLICKED", BUTTON2_CLICKED); |
| 3386 | SetDictInt("BUTTON2_DOUBLE_CLICKED", BUTTON2_DOUBLE_CLICKED); |
| 3387 | SetDictInt("BUTTON2_TRIPLE_CLICKED", BUTTON2_TRIPLE_CLICKED); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3388 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3389 | SetDictInt("BUTTON3_PRESSED", BUTTON3_PRESSED); |
| 3390 | SetDictInt("BUTTON3_RELEASED", BUTTON3_RELEASED); |
| 3391 | SetDictInt("BUTTON3_CLICKED", BUTTON3_CLICKED); |
| 3392 | SetDictInt("BUTTON3_DOUBLE_CLICKED", BUTTON3_DOUBLE_CLICKED); |
| 3393 | SetDictInt("BUTTON3_TRIPLE_CLICKED", BUTTON3_TRIPLE_CLICKED); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3394 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3395 | SetDictInt("BUTTON4_PRESSED", BUTTON4_PRESSED); |
| 3396 | SetDictInt("BUTTON4_RELEASED", BUTTON4_RELEASED); |
| 3397 | SetDictInt("BUTTON4_CLICKED", BUTTON4_CLICKED); |
| 3398 | SetDictInt("BUTTON4_DOUBLE_CLICKED", BUTTON4_DOUBLE_CLICKED); |
| 3399 | SetDictInt("BUTTON4_TRIPLE_CLICKED", BUTTON4_TRIPLE_CLICKED); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3400 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3401 | SetDictInt("BUTTON_SHIFT", BUTTON_SHIFT); |
| 3402 | SetDictInt("BUTTON_CTRL", BUTTON_CTRL); |
| 3403 | SetDictInt("BUTTON_ALT", BUTTON_ALT); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3404 | |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3405 | SetDictInt("ALL_MOUSE_EVENTS", ALL_MOUSE_EVENTS); |
| 3406 | SetDictInt("REPORT_MOUSE_POSITION", REPORT_MOUSE_POSITION); |
Andrew M. Kuchling | 8e9d23b | 2000-06-27 03:10:38 +0000 | [diff] [blame] | 3407 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3408 | /* Now set everything up for KEY_ variables */ |
| 3409 | { |
| 3410 | int key; |
| 3411 | char *key_n; |
| 3412 | char *key_n2; |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 3413 | #if !defined(__NetBSD__) |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3414 | for (key=KEY_MIN;key < KEY_MAX; key++) { |
| 3415 | key_n = (char *)keyname(key); |
| 3416 | if (key_n == NULL || strcmp(key_n,"UNKNOWN KEY")==0) |
| 3417 | continue; |
| 3418 | if (strncmp(key_n,"KEY_F(",6)==0) { |
| 3419 | char *p1, *p2; |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3420 | key_n2 = PyMem_Malloc(strlen(key_n)+1); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3421 | if (!key_n2) { |
| 3422 | PyErr_NoMemory(); |
| 3423 | break; |
| 3424 | } |
| 3425 | p1 = key_n; |
| 3426 | p2 = key_n2; |
| 3427 | while (*p1) { |
| 3428 | if (*p1 != '(' && *p1 != ')') { |
| 3429 | *p2 = *p1; |
| 3430 | p2++; |
| 3431 | } |
| 3432 | p1++; |
| 3433 | } |
| 3434 | *p2 = (char)0; |
| 3435 | } else |
| 3436 | key_n2 = key_n; |
| 3437 | SetDictInt(key_n2,key); |
| 3438 | if (key_n2 != key_n) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3439 | PyMem_Free(key_n2); |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3440 | } |
Andrew M. Kuchling | 2d339f9 | 2001-01-29 20:47:59 +0000 | [diff] [blame] | 3441 | #endif |
Victor Stinner | 26486ea | 2010-05-15 22:23:53 +0000 | [diff] [blame] | 3442 | SetDictInt("KEY_MIN", KEY_MIN); |
| 3443 | SetDictInt("KEY_MAX", KEY_MAX); |
| 3444 | } |
| 3445 | return m; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 3446 | } |