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 |
| 13 | #endif |
Jack Jansen | d00c13a | 2003-02-28 12:51:18 +0000 | [diff] [blame] | 14 | |
| 15 | #ifdef HAVE_NCURSES_H |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 16 | #include <ncurses.h> |
| 17 | #else |
| 18 | #include <curses.h> |
Martin v. Löwis | 6953233 | 2001-10-13 09:12:41 +0000 | [diff] [blame] | 19 | #ifdef HAVE_TERM_H |
| 20 | /* for tigetstr, which is not declared in SysV curses */ |
| 21 | #include <term.h> |
| 22 | #endif |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
Martin v. Löwis | eb9b103 | 2001-10-24 17:10:49 +0000 | [diff] [blame] | 25 | #ifdef HAVE_NCURSES_H |
| 26 | /* configure was checking <curses.h>, but we will |
| 27 | use <ncurses.h>, which has all these features. */ |
| 28 | #ifndef WINDOW_HAS_FLAGS |
| 29 | #define WINDOW_HAS_FLAGS 1 |
| 30 | #endif |
| 31 | #ifndef MVWDELCH_IS_EXPRESSION |
| 32 | #define MVWDELCH_IS_EXPRESSION 1 |
| 33 | #endif |
| 34 | #endif |
| 35 | |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 36 | #ifdef __cplusplus |
| 37 | extern "C" { |
| 38 | #endif |
| 39 | |
| 40 | #define PyCurses_API_pointers 4 |
| 41 | |
| 42 | /* Type declarations */ |
| 43 | |
| 44 | typedef struct { |
| 45 | PyObject_HEAD |
| 46 | WINDOW *win; |
| 47 | } PyCursesWindowObject; |
| 48 | |
| 49 | #define PyCursesWindow_Check(v) ((v)->ob_type == &PyCursesWindow_Type) |
| 50 | |
| 51 | #ifdef CURSES_MODULE |
| 52 | /* This section is used when compiling _cursesmodule.c */ |
| 53 | |
| 54 | #else |
| 55 | /* This section is used in modules that use the _cursesmodule API */ |
| 56 | |
| 57 | static void **PyCurses_API; |
| 58 | |
| 59 | #define PyCursesWindow_Type (*(PyTypeObject *) PyCurses_API[0]) |
| 60 | #define PyCursesSetupTermCalled {if (! ((int (*)(void))PyCurses_API[1]) () ) return NULL;} |
| 61 | #define PyCursesInitialised {if (! ((int (*)(void))PyCurses_API[2]) () ) return NULL;} |
| 62 | #define PyCursesInitialisedColor {if (! ((int (*)(void))PyCurses_API[3]) () ) return NULL;} |
| 63 | |
| 64 | #define import_curses() \ |
| 65 | { \ |
| 66 | PyObject *module = PyImport_ImportModule("_curses"); \ |
| 67 | if (module != NULL) { \ |
| 68 | PyObject *module_dict = PyModule_GetDict(module); \ |
| 69 | PyObject *c_api_object = PyDict_GetItemString(module_dict, "_C_API"); \ |
| 70 | if (PyCObject_Check(c_api_object)) { \ |
| 71 | PyCurses_API = (void **)PyCObject_AsVoidPtr(c_api_object); \ |
| 72 | } \ |
| 73 | } \ |
| 74 | } |
| 75 | #endif |
| 76 | |
| 77 | /* general error messages */ |
| 78 | static char *catchall_ERR = "curses function returned ERR"; |
| 79 | static char *catchall_NULL = "curses function returned NULL"; |
| 80 | |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 81 | /* Function Prototype Macros - They are ugly but very, very useful. ;-) |
| 82 | |
| 83 | X - function name |
| 84 | TYPE - parameter Type |
| 85 | ERGSTR - format string for construction of the return value |
| 86 | PARSESTR - format string for argument parsing |
| 87 | */ |
| 88 | |
| 89 | #define NoArgNoReturnFunction(X) \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 90 | static PyObject *PyCurses_ ## X (PyObject *self) \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 91 | { \ |
| 92 | PyCursesInitialised \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 93 | return PyCursesCheckERR(X(), # X); } |
| 94 | |
| 95 | #define NoArgOrFlagNoReturnFunction(X) \ |
| 96 | static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \ |
| 97 | { \ |
| 98 | int flag = 0; \ |
| 99 | PyCursesInitialised \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 100 | switch(PyTuple_Size(args)) { \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 101 | case 0: \ |
| 102 | return PyCursesCheckERR(X(), # X); \ |
| 103 | case 1: \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 104 | 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] | 105 | if (flag) return PyCursesCheckERR(X(), # X); \ |
| 106 | else return PyCursesCheckERR(no ## X (), # X); \ |
| 107 | default: \ |
| 108 | PyErr_SetString(PyExc_TypeError, # X " requires 0 or 1 arguments"); \ |
| 109 | return NULL; } } |
| 110 | |
| 111 | #define NoArgReturnIntFunction(X) \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 112 | static PyObject *PyCurses_ ## X (PyObject *self) \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 113 | { \ |
| 114 | PyCursesInitialised \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 115 | return PyInt_FromLong((long) X()); } |
| 116 | |
| 117 | |
| 118 | #define NoArgReturnStringFunction(X) \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 119 | static PyObject *PyCurses_ ## X (PyObject *self) \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 120 | { \ |
| 121 | PyCursesInitialised \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 122 | return PyString_FromString(X()); } |
| 123 | |
| 124 | #define NoArgTrueFalseFunction(X) \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 125 | static PyObject *PyCurses_ ## X (PyObject *self) \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 126 | { \ |
| 127 | PyCursesInitialised \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 128 | if (X () == FALSE) { \ |
| 129 | Py_INCREF(Py_False); \ |
| 130 | return Py_False; \ |
| 131 | } \ |
| 132 | Py_INCREF(Py_True); \ |
| 133 | return Py_True; } |
| 134 | |
| 135 | #define NoArgNoReturnVoidFunction(X) \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 136 | static PyObject *PyCurses_ ## X (PyObject *self) \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 137 | { \ |
| 138 | PyCursesInitialised \ |
Andrew M. Kuchling | 0c7a0bd | 2000-12-22 21:51:10 +0000 | [diff] [blame] | 139 | X(); \ |
| 140 | Py_INCREF(Py_None); \ |
| 141 | return Py_None; } |
| 142 | |
| 143 | #ifdef __cplusplus |
| 144 | } |
| 145 | #endif |
| 146 | |
| 147 | #endif /* !defined(Py_CURSES_H) */ |
| 148 | |
| 149 | |