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