Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 1 | |
| 2 | #ifndef Py_CURSES_H |
| 3 | #define Py_CURSES_H |
| 4 | |
Jack Jansen | 26897bf | 2002-11-22 16:12:57 +0000 | [diff] [blame] | 5 | #ifdef __APPLE__ |
| 6 | /* |
| 7 | ** On Mac OS X 10.2 [n]curses.h and stdlib.h use different guards |
| 8 | ** against multiple definition of wchar_t. |
| 9 | */ |
| 10 | #ifdef _BSD_WCHAR_T_DEFINED_ |
| 11 | #define _WCHAR_T |
| 12 | #endif |
Mark Dickinson | 6b3f1ef | 2009-09-06 21:21:05 +0000 | [diff] [blame] | 13 | |
| 14 | /* the following define is necessary for OS X 10.6; without it, the |
| 15 | Apple-supplied ncurses.h sets NCURSES_OPAQUE to 1, and then Python |
| 16 | can't get at the WINDOW flags field. */ |
| 17 | #define NCURSES_OPAQUE 0 |
| 18 | #endif /* __APPLE__ */ |
Jack Jansen | d00c13a | 2003-02-28 12:51:18 +0000 | [diff] [blame] | 19 | |
Andrew MacIntyre | 1a90117 | 2003-06-11 12:26:08 +0000 | [diff] [blame] | 20 | #ifdef __FreeBSD__ |
| 21 | /* |
| 22 | ** On FreeBSD, [n]curses.h and stdlib.h/wchar.h use different guards |
| 23 | ** against multiple definition of wchar_t and wint_t. |
| 24 | */ |
| 25 | #ifdef _XOPEN_SOURCE_EXTENDED |
Andrew MacIntyre | e3454af | 2003-06-29 15:46:21 +0000 | [diff] [blame] | 26 | #ifndef __FreeBSD_version |
| 27 | #include <osreldate.h> |
| 28 | #endif |
| 29 | #if __FreeBSD_version >= 500000 |
| 30 | #ifndef __wchar_t |
| 31 | #define __wchar_t |
| 32 | #endif |
| 33 | #ifndef __wint_t |
| 34 | #define __wint_t |
| 35 | #endif |
| 36 | #else |
Andrew MacIntyre | 1a90117 | 2003-06-11 12:26:08 +0000 | [diff] [blame] | 37 | #ifndef _WCHAR_T |
| 38 | #define _WCHAR_T |
| 39 | #endif |
| 40 | #ifndef _WINT_T |
| 41 | #define _WINT_T |
| 42 | #endif |
| 43 | #endif |
| 44 | #endif |
Andrew MacIntyre | e3454af | 2003-06-29 15:46:21 +0000 | [diff] [blame] | 45 | #endif |
Andrew MacIntyre | 1a90117 | 2003-06-11 12:26:08 +0000 | [diff] [blame] | 46 | |
Jack Jansen | d00c13a | 2003-02-28 12:51:18 +0000 | [diff] [blame] | 47 | #ifdef HAVE_NCURSES_H |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 48 | #include <ncurses.h> |
| 49 | #else |
| 50 | #include <curses.h> |
Martin v. Löwis | 6953233 | 2001-10-13 09:12:41 +0000 | [diff] [blame] | 51 | #ifdef HAVE_TERM_H |
| 52 | /* for tigetstr, which is not declared in SysV curses */ |
| 53 | #include <term.h> |
| 54 | #endif |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 55 | #endif |
| 56 | |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 57 | #ifdef HAVE_NCURSES_H |
| 58 | /* configure was checking <curses.h>, but we will |
| 59 | use <ncurses.h>, which has all these features. */ |
| 60 | #ifndef WINDOW_HAS_FLAGS |
| 61 | #define WINDOW_HAS_FLAGS 1 |
| 62 | #endif |
| 63 | #ifndef MVWDELCH_IS_EXPRESSION |
| 64 | #define MVWDELCH_IS_EXPRESSION 1 |
| 65 | #endif |
| 66 | #endif |
| 67 | |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 68 | #ifdef __cplusplus |
| 69 | extern "C" { |
| 70 | #endif |
| 71 | |
| 72 | #define PyCurses_API_pointers 4 |
| 73 | |
| 74 | /* Type declarations */ |
| 75 | |
| 76 | typedef struct { |
| 77 | PyObject_HEAD |
| 78 | WINDOW *win; |
| 79 | } PyCursesWindowObject; |
| 80 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 81 | #define PyCursesWindow_Check(v) (Py_TYPE(v) == &PyCursesWindow_Type) |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 82 | |
| 83 | #ifdef CURSES_MODULE |
| 84 | /* This section is used when compiling _cursesmodule.c */ |
| 85 | |
| 86 | #else |
| 87 | /* This section is used in modules that use the _cursesmodule API */ |
| 88 | |
| 89 | static void **PyCurses_API; |
| 90 | |
| 91 | #define PyCursesWindow_Type (*(PyTypeObject *) PyCurses_API[0]) |
| 92 | #define PyCursesSetupTermCalled {if (! ((int (*)(void))PyCurses_API[1]) () ) return NULL;} |
| 93 | #define PyCursesInitialised {if (! ((int (*)(void))PyCurses_API[2]) () ) return NULL;} |
| 94 | #define PyCursesInitialisedColor {if (! ((int (*)(void))PyCurses_API[3]) () ) return NULL;} |
| 95 | |
| 96 | #define import_curses() \ |
| 97 | { \ |
Christian Heimes | 000a074 | 2008-01-03 22:16:32 +0000 | [diff] [blame] | 98 | PyObject *module = PyImport_ImportModuleNoBlock("_curses"); \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 99 | if (module != NULL) { \ |
| 100 | PyObject *module_dict = PyModule_GetDict(module); \ |
| 101 | PyObject *c_api_object = PyDict_GetItemString(module_dict, "_C_API"); \ |
| 102 | if (PyCObject_Check(c_api_object)) { \ |
| 103 | PyCurses_API = (void **)PyCObject_AsVoidPtr(c_api_object); \ |
| 104 | } \ |
| 105 | } \ |
| 106 | } |
| 107 | #endif |
| 108 | |
| 109 | /* general error messages */ |
| 110 | static char *catchall_ERR = "curses function returned ERR"; |
| 111 | static char *catchall_NULL = "curses function returned NULL"; |
| 112 | |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 113 | /* Function Prototype Macros - They are ugly but very, very useful. ;-) |
| 114 | |
| 115 | X - function name |
| 116 | TYPE - parameter Type |
| 117 | ERGSTR - format string for construction of the return value |
| 118 | PARSESTR - format string for argument parsing |
| 119 | */ |
| 120 | |
| 121 | #define NoArgNoReturnFunction(X) \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 122 | static PyObject *PyCurses_ ## X (PyObject *self) \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 123 | { \ |
| 124 | PyCursesInitialised \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 125 | return PyCursesCheckERR(X(), # X); } |
| 126 | |
| 127 | #define NoArgOrFlagNoReturnFunction(X) \ |
| 128 | static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \ |
| 129 | { \ |
| 130 | int flag = 0; \ |
| 131 | PyCursesInitialised \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 132 | switch(PyTuple_Size(args)) { \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 133 | case 0: \ |
| 134 | return PyCursesCheckERR(X(), # X); \ |
| 135 | case 1: \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 136 | if (!PyArg_ParseTuple(args, "i;True(1) or False(0)", &flag)) return NULL; \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 137 | if (flag) return PyCursesCheckERR(X(), # X); \ |
| 138 | else return PyCursesCheckERR(no ## X (), # X); \ |
| 139 | default: \ |
| 140 | PyErr_SetString(PyExc_TypeError, # X " requires 0 or 1 arguments"); \ |
| 141 | return NULL; } } |
| 142 | |
| 143 | #define NoArgReturnIntFunction(X) \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 144 | static PyObject *PyCurses_ ## X (PyObject *self) \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 145 | { \ |
| 146 | PyCursesInitialised \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 147 | return PyInt_FromLong((long) X()); } |
| 148 | |
| 149 | |
| 150 | #define NoArgReturnStringFunction(X) \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 151 | static PyObject *PyCurses_ ## X (PyObject *self) \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 152 | { \ |
| 153 | PyCursesInitialised \ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 154 | return PyString_FromString(X()); } |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 155 | |
| 156 | #define NoArgTrueFalseFunction(X) \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 157 | static PyObject *PyCurses_ ## X (PyObject *self) \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 158 | { \ |
| 159 | PyCursesInitialised \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 160 | if (X () == FALSE) { \ |
| 161 | Py_INCREF(Py_False); \ |
| 162 | return Py_False; \ |
| 163 | } \ |
| 164 | Py_INCREF(Py_True); \ |
| 165 | return Py_True; } |
| 166 | |
| 167 | #define NoArgNoReturnVoidFunction(X) \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 168 | static PyObject *PyCurses_ ## X (PyObject *self) \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 169 | { \ |
| 170 | PyCursesInitialised \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 171 | X(); \ |
| 172 | Py_INCREF(Py_None); \ |
| 173 | return Py_None; } |
| 174 | |
| 175 | #ifdef __cplusplus |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | #endif /* !defined(Py_CURSES_H) */ |
| 180 | |
| 181 | |