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