py_curses.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #endif /* __APPLE__ */
  12. /* On FreeBSD, [n]curses.h and stdlib.h/wchar.h use different guards
  13. against multiple definition of wchar_t and wint_t. */
  14. #if defined(__FreeBSD__) && defined(_XOPEN_SOURCE_EXTENDED)
  15. # ifndef __wchar_t
  16. # define __wchar_t
  17. # endif
  18. # ifndef __wint_t
  19. # define __wint_t
  20. # endif
  21. #endif
  22. #if !defined(HAVE_CURSES_IS_PAD) && defined(WINDOW_HAS_FLAGS)
  23. /* The following definition is necessary for ncurses 5.7; without it,
  24. some of [n]curses.h set NCURSES_OPAQUE to 1, and then Python
  25. can't get at the WINDOW flags field. */
  26. #define NCURSES_OPAQUE 0
  27. #endif
  28. #ifdef HAVE_NCURSES_H
  29. #include <ncurses.h>
  30. #else
  31. #include <curses.h>
  32. #endif
  33. #ifdef HAVE_NCURSES_H
  34. /* configure was checking <curses.h>, but we will
  35. use <ncurses.h>, which has some or all these features. */
  36. #if !defined(WINDOW_HAS_FLAGS) && !(NCURSES_OPAQUE+0)
  37. #define WINDOW_HAS_FLAGS 1
  38. #endif
  39. #if !defined(HAVE_CURSES_IS_PAD) && NCURSES_VERSION_PATCH+0 >= 20090906
  40. #define HAVE_CURSES_IS_PAD 1
  41. #endif
  42. #ifndef MVWDELCH_IS_EXPRESSION
  43. #define MVWDELCH_IS_EXPRESSION 1
  44. #endif
  45. #endif
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. #define PyCurses_API_pointers 4
  50. /* Type declarations */
  51. typedef struct {
  52. PyObject_HEAD
  53. WINDOW *win;
  54. char *encoding;
  55. } PyCursesWindowObject;
  56. #define PyCursesWindow_Check(v) (Py_TYPE(v) == &PyCursesWindow_Type)
  57. #define PyCurses_CAPSULE_NAME "_curses._C_API"
  58. #ifdef CURSES_MODULE
  59. /* This section is used when compiling _cursesmodule.c */
  60. #else
  61. /* This section is used in modules that use the _cursesmodule API */
  62. static void **PyCurses_API;
  63. #define PyCursesWindow_Type (*(PyTypeObject *) PyCurses_API[0])
  64. #define PyCursesSetupTermCalled {if (! ((int (*)(void))PyCurses_API[1]) () ) return NULL;}
  65. #define PyCursesInitialised {if (! ((int (*)(void))PyCurses_API[2]) () ) return NULL;}
  66. #define PyCursesInitialisedColor {if (! ((int (*)(void))PyCurses_API[3]) () ) return NULL;}
  67. #define import_curses() \
  68. PyCurses_API = (void **)PyCapsule_Import(PyCurses_CAPSULE_NAME, 1);
  69. #endif
  70. /* general error messages */
  71. static const char catchall_ERR[] = "curses function returned ERR";
  72. static const char catchall_NULL[] = "curses function returned NULL";
  73. /* Function Prototype Macros - They are ugly but very, very useful. ;-)
  74. X - function name
  75. TYPE - parameter Type
  76. ERGSTR - format string for construction of the return value
  77. PARSESTR - format string for argument parsing
  78. */
  79. #define NoArgNoReturnFunction(X) \
  80. static PyObject *PyCurses_ ## X (PyObject *self) \
  81. { \
  82. PyCursesInitialised \
  83. return PyCursesCheckERR(X(), # X); }
  84. #define NoArgOrFlagNoReturnFunction(X) \
  85. static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \
  86. { \
  87. int flag = 0; \
  88. PyCursesInitialised \
  89. switch(PyTuple_Size(args)) { \
  90. case 0: \
  91. return PyCursesCheckERR(X(), # X); \
  92. case 1: \
  93. if (!PyArg_ParseTuple(args, "i;True(1) or False(0)", &flag)) return NULL; \
  94. if (flag) return PyCursesCheckERR(X(), # X); \
  95. else return PyCursesCheckERR(no ## X (), # X); \
  96. default: \
  97. PyErr_SetString(PyExc_TypeError, # X " requires 0 or 1 arguments"); \
  98. return NULL; } }
  99. #define NoArgReturnIntFunction(X) \
  100. static PyObject *PyCurses_ ## X (PyObject *self) \
  101. { \
  102. PyCursesInitialised \
  103. return PyLong_FromLong((long) X()); }
  104. #define NoArgReturnStringFunction(X) \
  105. static PyObject *PyCurses_ ## X (PyObject *self) \
  106. { \
  107. PyCursesInitialised \
  108. return PyBytes_FromString(X()); }
  109. #define NoArgTrueFalseFunction(X) \
  110. static PyObject *PyCurses_ ## X (PyObject *self) \
  111. { \
  112. PyCursesInitialised \
  113. if (X () == FALSE) { \
  114. Py_RETURN_FALSE; \
  115. } \
  116. Py_RETURN_TRUE; }
  117. #define NoArgNoReturnVoidFunction(X) \
  118. static PyObject *PyCurses_ ## X (PyObject *self) \
  119. { \
  120. PyCursesInitialised \
  121. X(); \
  122. Py_RETURN_NONE; }
  123. #ifdef __cplusplus
  124. }
  125. #endif
  126. #endif /* !defined(Py_CURSES_H) */