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