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