py_curses.h 4.1 KB

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