py_curses.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #define PyCurses_CAPSULE_NAME "_curses._C_API"
  72. #ifdef CURSES_MODULE
  73. /* This section is used when compiling _cursesmodule.c */
  74. #else
  75. /* This section is used in modules that use the _cursesmodule API */
  76. static void **PyCurses_API;
  77. #define PyCursesWindow_Type (*(PyTypeObject *) PyCurses_API[0])
  78. #define PyCursesSetupTermCalled {if (! ((int (*)(void))PyCurses_API[1]) () ) return NULL;}
  79. #define PyCursesInitialised {if (! ((int (*)(void))PyCurses_API[2]) () ) return NULL;}
  80. #define PyCursesInitialisedColor {if (! ((int (*)(void))PyCurses_API[3]) () ) return NULL;}
  81. #define import_curses() \
  82. PyCurses_API = (void **)PyCapsule_Import(PyCurses_CAPSULE_NAME, 1);
  83. #endif
  84. /* general error messages */
  85. static char *catchall_ERR = "curses function returned ERR";
  86. static char *catchall_NULL = "curses function returned NULL";
  87. /* Function Prototype Macros - They are ugly but very, very useful. ;-)
  88. X - function name
  89. TYPE - parameter Type
  90. ERGSTR - format string for construction of the return value
  91. PARSESTR - format string for argument parsing
  92. */
  93. #define NoArgNoReturnFunction(X) \
  94. static PyObject *PyCurses_ ## X (PyObject *self) \
  95. { \
  96. PyCursesInitialised \
  97. return PyCursesCheckERR(X(), # X); }
  98. #define NoArgOrFlagNoReturnFunction(X) \
  99. static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \
  100. { \
  101. int flag = 0; \
  102. PyCursesInitialised \
  103. switch(PyTuple_Size(args)) { \
  104. case 0: \
  105. return PyCursesCheckERR(X(), # X); \
  106. case 1: \
  107. if (!PyArg_ParseTuple(args, "i;True(1) or False(0)", &flag)) return NULL; \
  108. if (flag) return PyCursesCheckERR(X(), # X); \
  109. else return PyCursesCheckERR(no ## X (), # X); \
  110. default: \
  111. PyErr_SetString(PyExc_TypeError, # X " requires 0 or 1 arguments"); \
  112. return NULL; } }
  113. #define NoArgReturnIntFunction(X) \
  114. static PyObject *PyCurses_ ## X (PyObject *self) \
  115. { \
  116. PyCursesInitialised \
  117. return PyInt_FromLong((long) X()); }
  118. #define NoArgReturnStringFunction(X) \
  119. static PyObject *PyCurses_ ## X (PyObject *self) \
  120. { \
  121. PyCursesInitialised \
  122. return PyString_FromString(X()); }
  123. #define NoArgTrueFalseFunction(X) \
  124. static PyObject *PyCurses_ ## X (PyObject *self) \
  125. { \
  126. PyCursesInitialised \
  127. if (X () == FALSE) { \
  128. Py_INCREF(Py_False); \
  129. return Py_False; \
  130. } \
  131. Py_INCREF(Py_True); \
  132. return Py_True; }
  133. #define NoArgNoReturnVoidFunction(X) \
  134. static PyObject *PyCurses_ ## X (PyObject *self) \
  135. { \
  136. PyCursesInitialised \
  137. X(); \
  138. Py_INCREF(Py_None); \
  139. return Py_None; }
  140. #ifdef __cplusplus
  141. }
  142. #endif
  143. #endif /* !defined(Py_CURSES_H) */