/Include/py_curses.h

http://unladen-swallow.googlecode.com/ · C++ Header · 181 lines · 127 code · 29 blank · 25 comment · 10 complexity · 6307925eea22ad560984cfa21fef1eda MD5 · raw file

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