1 | n/a | /* |
---|
2 | n/a | * Interface to the ncurses panel library |
---|
3 | n/a | * |
---|
4 | n/a | * Original version by Thomas Gellekum |
---|
5 | n/a | */ |
---|
6 | n/a | |
---|
7 | n/a | /* Release Number */ |
---|
8 | n/a | |
---|
9 | n/a | static const char PyCursesVersion[] = "2.1"; |
---|
10 | n/a | |
---|
11 | n/a | /* Includes */ |
---|
12 | n/a | |
---|
13 | n/a | #include "Python.h" |
---|
14 | n/a | |
---|
15 | n/a | #include "py_curses.h" |
---|
16 | n/a | |
---|
17 | n/a | #include <panel.h> |
---|
18 | n/a | |
---|
19 | n/a | typedef struct { |
---|
20 | n/a | PyObject *PyCursesError; |
---|
21 | n/a | PyObject *PyCursesPanel_Type; |
---|
22 | n/a | } _curses_panelstate; |
---|
23 | n/a | |
---|
24 | n/a | #define _curses_panelstate(o) ((_curses_panelstate *)PyModule_GetState(o)) |
---|
25 | n/a | |
---|
26 | n/a | static int |
---|
27 | n/a | _curses_panel_clear(PyObject *m) |
---|
28 | n/a | { |
---|
29 | n/a | Py_CLEAR(_curses_panelstate(m)->PyCursesError); |
---|
30 | n/a | return 0; |
---|
31 | n/a | } |
---|
32 | n/a | |
---|
33 | n/a | static int |
---|
34 | n/a | _curses_panel_traverse(PyObject *m, visitproc visit, void *arg) |
---|
35 | n/a | { |
---|
36 | n/a | Py_VISIT(_curses_panelstate(m)->PyCursesError); |
---|
37 | n/a | return 0; |
---|
38 | n/a | } |
---|
39 | n/a | |
---|
40 | n/a | static void |
---|
41 | n/a | _curses_panel_free(void *m) |
---|
42 | n/a | { |
---|
43 | n/a | _curses_panel_clear((PyObject *) m); |
---|
44 | n/a | } |
---|
45 | n/a | |
---|
46 | n/a | static struct PyModuleDef _curses_panelmodule; |
---|
47 | n/a | |
---|
48 | n/a | #define _curses_panelstate_global \ |
---|
49 | n/a | ((_curses_panelstate *) PyModule_GetState(PyState_FindModule(&_curses_panelmodule))) |
---|
50 | n/a | |
---|
51 | n/a | /* Utility Functions */ |
---|
52 | n/a | |
---|
53 | n/a | /* |
---|
54 | n/a | * Check the return code from a curses function and return None |
---|
55 | n/a | * or raise an exception as appropriate. |
---|
56 | n/a | */ |
---|
57 | n/a | |
---|
58 | n/a | static PyObject * |
---|
59 | n/a | PyCursesCheckERR(int code, const char *fname) |
---|
60 | n/a | { |
---|
61 | n/a | if (code != ERR) { |
---|
62 | n/a | Py_RETURN_NONE; |
---|
63 | n/a | } else { |
---|
64 | n/a | if (fname == NULL) { |
---|
65 | n/a | PyErr_SetString(_curses_panelstate_global->PyCursesError, catchall_ERR); |
---|
66 | n/a | } else { |
---|
67 | n/a | PyErr_Format(_curses_panelstate_global->PyCursesError, "%s() returned ERR", fname); |
---|
68 | n/a | } |
---|
69 | n/a | return NULL; |
---|
70 | n/a | } |
---|
71 | n/a | } |
---|
72 | n/a | |
---|
73 | n/a | /***************************************************************************** |
---|
74 | n/a | The Panel Object |
---|
75 | n/a | ******************************************************************************/ |
---|
76 | n/a | |
---|
77 | n/a | /* Definition of the panel object and panel type */ |
---|
78 | n/a | |
---|
79 | n/a | typedef struct { |
---|
80 | n/a | PyObject_HEAD |
---|
81 | n/a | PANEL *pan; |
---|
82 | n/a | PyCursesWindowObject *wo; /* for reference counts */ |
---|
83 | n/a | } PyCursesPanelObject; |
---|
84 | n/a | |
---|
85 | n/a | #define PyCursesPanel_Check(v) \ |
---|
86 | n/a | (Py_TYPE(v) == _curses_panelstate_global->PyCursesPanel_Type) |
---|
87 | n/a | |
---|
88 | n/a | /* Some helper functions. The problem is that there's always a window |
---|
89 | n/a | associated with a panel. To ensure that Python's GC doesn't pull |
---|
90 | n/a | this window from under our feet we need to keep track of references |
---|
91 | n/a | to the corresponding window object within Python. We can't use |
---|
92 | n/a | dupwin(oldwin) to keep a copy of the curses WINDOW because the |
---|
93 | n/a | contents of oldwin is copied only once; code like |
---|
94 | n/a | |
---|
95 | n/a | win = newwin(...) |
---|
96 | n/a | pan = win.panel() |
---|
97 | n/a | win.addstr(some_string) |
---|
98 | n/a | pan.window().addstr(other_string) |
---|
99 | n/a | |
---|
100 | n/a | will fail. */ |
---|
101 | n/a | |
---|
102 | n/a | /* We keep a linked list of PyCursesPanelObjects, lop. A list should |
---|
103 | n/a | suffice, I don't expect more than a handful or at most a few |
---|
104 | n/a | dozens of panel objects within a typical program. */ |
---|
105 | n/a | typedef struct _list_of_panels { |
---|
106 | n/a | PyCursesPanelObject *po; |
---|
107 | n/a | struct _list_of_panels *next; |
---|
108 | n/a | } list_of_panels; |
---|
109 | n/a | |
---|
110 | n/a | /* list anchor */ |
---|
111 | n/a | static list_of_panels *lop; |
---|
112 | n/a | |
---|
113 | n/a | /* Insert a new panel object into lop */ |
---|
114 | n/a | static int |
---|
115 | n/a | insert_lop(PyCursesPanelObject *po) |
---|
116 | n/a | { |
---|
117 | n/a | list_of_panels *new; |
---|
118 | n/a | |
---|
119 | n/a | if ((new = (list_of_panels *)PyMem_Malloc(sizeof(list_of_panels))) == NULL) { |
---|
120 | n/a | PyErr_NoMemory(); |
---|
121 | n/a | return -1; |
---|
122 | n/a | } |
---|
123 | n/a | new->po = po; |
---|
124 | n/a | new->next = lop; |
---|
125 | n/a | lop = new; |
---|
126 | n/a | return 0; |
---|
127 | n/a | } |
---|
128 | n/a | |
---|
129 | n/a | /* Remove the panel object from lop */ |
---|
130 | n/a | static void |
---|
131 | n/a | remove_lop(PyCursesPanelObject *po) |
---|
132 | n/a | { |
---|
133 | n/a | list_of_panels *temp, *n; |
---|
134 | n/a | |
---|
135 | n/a | temp = lop; |
---|
136 | n/a | if (temp->po == po) { |
---|
137 | n/a | lop = temp->next; |
---|
138 | n/a | PyMem_Free(temp); |
---|
139 | n/a | return; |
---|
140 | n/a | } |
---|
141 | n/a | while (temp->next == NULL || temp->next->po != po) { |
---|
142 | n/a | if (temp->next == NULL) { |
---|
143 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
144 | n/a | "remove_lop: can't find Panel Object"); |
---|
145 | n/a | return; |
---|
146 | n/a | } |
---|
147 | n/a | temp = temp->next; |
---|
148 | n/a | } |
---|
149 | n/a | n = temp->next->next; |
---|
150 | n/a | PyMem_Free(temp->next); |
---|
151 | n/a | temp->next = n; |
---|
152 | n/a | return; |
---|
153 | n/a | } |
---|
154 | n/a | |
---|
155 | n/a | /* Return the panel object that corresponds to pan */ |
---|
156 | n/a | static PyCursesPanelObject * |
---|
157 | n/a | find_po(PANEL *pan) |
---|
158 | n/a | { |
---|
159 | n/a | list_of_panels *temp; |
---|
160 | n/a | for (temp = lop; temp->po->pan != pan; temp = temp->next) |
---|
161 | n/a | if (temp->next == NULL) return NULL; /* not found!? */ |
---|
162 | n/a | return temp->po; |
---|
163 | n/a | } |
---|
164 | n/a | |
---|
165 | n/a | /* Function Prototype Macros - They are ugly but very, very useful. ;-) |
---|
166 | n/a | |
---|
167 | n/a | X - function name |
---|
168 | n/a | TYPE - parameter Type |
---|
169 | n/a | ERGSTR - format string for construction of the return value |
---|
170 | n/a | PARSESTR - format string for argument parsing */ |
---|
171 | n/a | |
---|
172 | n/a | #define Panel_NoArgNoReturnFunction(X) \ |
---|
173 | n/a | static PyObject *PyCursesPanel_##X(PyCursesPanelObject *self) \ |
---|
174 | n/a | { return PyCursesCheckERR(X(self->pan), # X); } |
---|
175 | n/a | |
---|
176 | n/a | #define Panel_NoArgTrueFalseFunction(X) \ |
---|
177 | n/a | static PyObject *PyCursesPanel_##X(PyCursesPanelObject *self) \ |
---|
178 | n/a | { \ |
---|
179 | n/a | if (X (self->pan) == FALSE) { Py_RETURN_FALSE; } \ |
---|
180 | n/a | else { Py_RETURN_TRUE; } } |
---|
181 | n/a | |
---|
182 | n/a | #define Panel_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \ |
---|
183 | n/a | static PyObject *PyCursesPanel_##X(PyCursesPanelObject *self, PyObject *args) \ |
---|
184 | n/a | { \ |
---|
185 | n/a | TYPE arg1, arg2; \ |
---|
186 | n/a | if (!PyArg_ParseTuple(args, PARSESTR, &arg1, &arg2)) return NULL; \ |
---|
187 | n/a | return PyCursesCheckERR(X(self->pan, arg1, arg2), # X); } |
---|
188 | n/a | |
---|
189 | n/a | /* ------------- PANEL routines --------------- */ |
---|
190 | n/a | |
---|
191 | n/a | Panel_NoArgNoReturnFunction(bottom_panel) |
---|
192 | n/a | Panel_NoArgNoReturnFunction(hide_panel) |
---|
193 | n/a | Panel_NoArgNoReturnFunction(show_panel) |
---|
194 | n/a | Panel_NoArgNoReturnFunction(top_panel) |
---|
195 | n/a | Panel_NoArgTrueFalseFunction(panel_hidden) |
---|
196 | n/a | Panel_TwoArgNoReturnFunction(move_panel, int, "ii;y,x") |
---|
197 | n/a | |
---|
198 | n/a | /* Allocation and deallocation of Panel Objects */ |
---|
199 | n/a | |
---|
200 | n/a | static PyObject * |
---|
201 | n/a | PyCursesPanel_New(PANEL *pan, PyCursesWindowObject *wo) |
---|
202 | n/a | { |
---|
203 | n/a | PyCursesPanelObject *po; |
---|
204 | n/a | |
---|
205 | n/a | po = PyObject_NEW(PyCursesPanelObject, |
---|
206 | n/a | (PyTypeObject *)(_curses_panelstate_global)->PyCursesPanel_Type); |
---|
207 | n/a | if (po == NULL) return NULL; |
---|
208 | n/a | po->pan = pan; |
---|
209 | n/a | if (insert_lop(po) < 0) { |
---|
210 | n/a | po->wo = NULL; |
---|
211 | n/a | Py_DECREF(po); |
---|
212 | n/a | return NULL; |
---|
213 | n/a | } |
---|
214 | n/a | po->wo = wo; |
---|
215 | n/a | Py_INCREF(wo); |
---|
216 | n/a | return (PyObject *)po; |
---|
217 | n/a | } |
---|
218 | n/a | |
---|
219 | n/a | static void |
---|
220 | n/a | PyCursesPanel_Dealloc(PyCursesPanelObject *po) |
---|
221 | n/a | { |
---|
222 | n/a | PyObject *obj = (PyObject *) panel_userptr(po->pan); |
---|
223 | n/a | if (obj) { |
---|
224 | n/a | (void)set_panel_userptr(po->pan, NULL); |
---|
225 | n/a | Py_DECREF(obj); |
---|
226 | n/a | } |
---|
227 | n/a | (void)del_panel(po->pan); |
---|
228 | n/a | if (po->wo != NULL) { |
---|
229 | n/a | Py_DECREF(po->wo); |
---|
230 | n/a | remove_lop(po); |
---|
231 | n/a | } |
---|
232 | n/a | PyObject_DEL(po); |
---|
233 | n/a | } |
---|
234 | n/a | |
---|
235 | n/a | /* panel_above(NULL) returns the bottom panel in the stack. To get |
---|
236 | n/a | this behaviour we use curses.panel.bottom_panel(). */ |
---|
237 | n/a | static PyObject * |
---|
238 | n/a | PyCursesPanel_above(PyCursesPanelObject *self) |
---|
239 | n/a | { |
---|
240 | n/a | PANEL *pan; |
---|
241 | n/a | PyCursesPanelObject *po; |
---|
242 | n/a | |
---|
243 | n/a | pan = panel_above(self->pan); |
---|
244 | n/a | |
---|
245 | n/a | if (pan == NULL) { /* valid output, it means the calling panel |
---|
246 | n/a | is on top of the stack */ |
---|
247 | n/a | Py_RETURN_NONE; |
---|
248 | n/a | } |
---|
249 | n/a | po = find_po(pan); |
---|
250 | n/a | if (po == NULL) { |
---|
251 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
252 | n/a | "panel_above: can't find Panel Object"); |
---|
253 | n/a | return NULL; |
---|
254 | n/a | } |
---|
255 | n/a | Py_INCREF(po); |
---|
256 | n/a | return (PyObject *)po; |
---|
257 | n/a | } |
---|
258 | n/a | |
---|
259 | n/a | /* panel_below(NULL) returns the top panel in the stack. To get |
---|
260 | n/a | this behaviour we use curses.panel.top_panel(). */ |
---|
261 | n/a | static PyObject * |
---|
262 | n/a | PyCursesPanel_below(PyCursesPanelObject *self) |
---|
263 | n/a | { |
---|
264 | n/a | PANEL *pan; |
---|
265 | n/a | PyCursesPanelObject *po; |
---|
266 | n/a | |
---|
267 | n/a | pan = panel_below(self->pan); |
---|
268 | n/a | |
---|
269 | n/a | if (pan == NULL) { /* valid output, it means the calling panel |
---|
270 | n/a | is on the bottom of the stack */ |
---|
271 | n/a | Py_RETURN_NONE; |
---|
272 | n/a | } |
---|
273 | n/a | po = find_po(pan); |
---|
274 | n/a | if (po == NULL) { |
---|
275 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
276 | n/a | "panel_below: can't find Panel Object"); |
---|
277 | n/a | return NULL; |
---|
278 | n/a | } |
---|
279 | n/a | Py_INCREF(po); |
---|
280 | n/a | return (PyObject *)po; |
---|
281 | n/a | } |
---|
282 | n/a | |
---|
283 | n/a | static PyObject * |
---|
284 | n/a | PyCursesPanel_window(PyCursesPanelObject *self) |
---|
285 | n/a | { |
---|
286 | n/a | Py_INCREF(self->wo); |
---|
287 | n/a | return (PyObject *)self->wo; |
---|
288 | n/a | } |
---|
289 | n/a | |
---|
290 | n/a | static PyObject * |
---|
291 | n/a | PyCursesPanel_replace_panel(PyCursesPanelObject *self, PyObject *args) |
---|
292 | n/a | { |
---|
293 | n/a | PyCursesPanelObject *po; |
---|
294 | n/a | PyCursesWindowObject *temp; |
---|
295 | n/a | int rtn; |
---|
296 | n/a | |
---|
297 | n/a | if (PyTuple_Size(args) != 1) { |
---|
298 | n/a | PyErr_SetString(PyExc_TypeError, "replace requires one argument"); |
---|
299 | n/a | return NULL; |
---|
300 | n/a | } |
---|
301 | n/a | if (!PyArg_ParseTuple(args, "O!;window object", |
---|
302 | n/a | &PyCursesWindow_Type, &temp)) |
---|
303 | n/a | return NULL; |
---|
304 | n/a | |
---|
305 | n/a | po = find_po(self->pan); |
---|
306 | n/a | if (po == NULL) { |
---|
307 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
308 | n/a | "replace_panel: can't find Panel Object"); |
---|
309 | n/a | return NULL; |
---|
310 | n/a | } |
---|
311 | n/a | |
---|
312 | n/a | rtn = replace_panel(self->pan, temp->win); |
---|
313 | n/a | if (rtn == ERR) { |
---|
314 | n/a | PyErr_SetString(_curses_panelstate_global->PyCursesError, "replace_panel() returned ERR"); |
---|
315 | n/a | return NULL; |
---|
316 | n/a | } |
---|
317 | n/a | Py_INCREF(temp); |
---|
318 | n/a | Py_SETREF(po->wo, temp); |
---|
319 | n/a | Py_RETURN_NONE; |
---|
320 | n/a | } |
---|
321 | n/a | |
---|
322 | n/a | static PyObject * |
---|
323 | n/a | PyCursesPanel_set_panel_userptr(PyCursesPanelObject *self, PyObject *obj) |
---|
324 | n/a | { |
---|
325 | n/a | PyObject *oldobj; |
---|
326 | n/a | int rc; |
---|
327 | n/a | PyCursesInitialised; |
---|
328 | n/a | Py_INCREF(obj); |
---|
329 | n/a | oldobj = (PyObject *) panel_userptr(self->pan); |
---|
330 | n/a | rc = set_panel_userptr(self->pan, (void*)obj); |
---|
331 | n/a | if (rc == ERR) { |
---|
332 | n/a | /* In case of an ncurses error, decref the new object again */ |
---|
333 | n/a | Py_DECREF(obj); |
---|
334 | n/a | } |
---|
335 | n/a | Py_XDECREF(oldobj); |
---|
336 | n/a | return PyCursesCheckERR(rc, "set_panel_userptr"); |
---|
337 | n/a | } |
---|
338 | n/a | |
---|
339 | n/a | static PyObject * |
---|
340 | n/a | PyCursesPanel_userptr(PyCursesPanelObject *self) |
---|
341 | n/a | { |
---|
342 | n/a | PyObject *obj; |
---|
343 | n/a | PyCursesInitialised; |
---|
344 | n/a | obj = (PyObject *) panel_userptr(self->pan); |
---|
345 | n/a | if (obj == NULL) { |
---|
346 | n/a | PyErr_SetString(_curses_panelstate_global->PyCursesError, "no userptr set"); |
---|
347 | n/a | return NULL; |
---|
348 | n/a | } |
---|
349 | n/a | |
---|
350 | n/a | Py_INCREF(obj); |
---|
351 | n/a | return obj; |
---|
352 | n/a | } |
---|
353 | n/a | |
---|
354 | n/a | |
---|
355 | n/a | /* Module interface */ |
---|
356 | n/a | |
---|
357 | n/a | static PyMethodDef PyCursesPanel_Methods[] = { |
---|
358 | n/a | {"above", (PyCFunction)PyCursesPanel_above, METH_NOARGS}, |
---|
359 | n/a | {"below", (PyCFunction)PyCursesPanel_below, METH_NOARGS}, |
---|
360 | n/a | {"bottom", (PyCFunction)PyCursesPanel_bottom_panel, METH_NOARGS}, |
---|
361 | n/a | {"hidden", (PyCFunction)PyCursesPanel_panel_hidden, METH_NOARGS}, |
---|
362 | n/a | {"hide", (PyCFunction)PyCursesPanel_hide_panel, METH_NOARGS}, |
---|
363 | n/a | {"move", (PyCFunction)PyCursesPanel_move_panel, METH_VARARGS}, |
---|
364 | n/a | {"replace", (PyCFunction)PyCursesPanel_replace_panel, METH_VARARGS}, |
---|
365 | n/a | {"set_userptr", (PyCFunction)PyCursesPanel_set_panel_userptr, METH_O}, |
---|
366 | n/a | {"show", (PyCFunction)PyCursesPanel_show_panel, METH_NOARGS}, |
---|
367 | n/a | {"top", (PyCFunction)PyCursesPanel_top_panel, METH_NOARGS}, |
---|
368 | n/a | {"userptr", (PyCFunction)PyCursesPanel_userptr, METH_NOARGS}, |
---|
369 | n/a | {"window", (PyCFunction)PyCursesPanel_window, METH_NOARGS}, |
---|
370 | n/a | {NULL, NULL} /* sentinel */ |
---|
371 | n/a | }; |
---|
372 | n/a | |
---|
373 | n/a | /* -------------------------------------------------------*/ |
---|
374 | n/a | |
---|
375 | n/a | static PyType_Slot PyCursesPanel_Type_slots[] = { |
---|
376 | n/a | {Py_tp_dealloc, PyCursesPanel_Dealloc}, |
---|
377 | n/a | {Py_tp_methods, PyCursesPanel_Methods}, |
---|
378 | n/a | {0, 0}, |
---|
379 | n/a | }; |
---|
380 | n/a | |
---|
381 | n/a | static PyType_Spec PyCursesPanel_Type_spec = { |
---|
382 | n/a | "_curses_panel.curses panel", |
---|
383 | n/a | sizeof(PyCursesPanelObject), |
---|
384 | n/a | 0, |
---|
385 | n/a | Py_TPFLAGS_DEFAULT, |
---|
386 | n/a | PyCursesPanel_Type_slots |
---|
387 | n/a | }; |
---|
388 | n/a | |
---|
389 | n/a | /* Wrapper for panel_above(NULL). This function returns the bottom |
---|
390 | n/a | panel of the stack, so it's renamed to bottom_panel(). |
---|
391 | n/a | panel.above() *requires* a panel object in the first place which |
---|
392 | n/a | may be undesirable. */ |
---|
393 | n/a | static PyObject * |
---|
394 | n/a | PyCurses_bottom_panel(PyObject *self) |
---|
395 | n/a | { |
---|
396 | n/a | PANEL *pan; |
---|
397 | n/a | PyCursesPanelObject *po; |
---|
398 | n/a | |
---|
399 | n/a | PyCursesInitialised; |
---|
400 | n/a | |
---|
401 | n/a | pan = panel_above(NULL); |
---|
402 | n/a | |
---|
403 | n/a | if (pan == NULL) { /* valid output, it means |
---|
404 | n/a | there's no panel at all */ |
---|
405 | n/a | Py_RETURN_NONE; |
---|
406 | n/a | } |
---|
407 | n/a | po = find_po(pan); |
---|
408 | n/a | if (po == NULL) { |
---|
409 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
410 | n/a | "panel_above: can't find Panel Object"); |
---|
411 | n/a | return NULL; |
---|
412 | n/a | } |
---|
413 | n/a | Py_INCREF(po); |
---|
414 | n/a | return (PyObject *)po; |
---|
415 | n/a | } |
---|
416 | n/a | |
---|
417 | n/a | static PyObject * |
---|
418 | n/a | PyCurses_new_panel(PyObject *self, PyObject *args) |
---|
419 | n/a | { |
---|
420 | n/a | PyCursesWindowObject *win; |
---|
421 | n/a | PANEL *pan; |
---|
422 | n/a | |
---|
423 | n/a | if (!PyArg_ParseTuple(args, "O!", &PyCursesWindow_Type, &win)) |
---|
424 | n/a | return NULL; |
---|
425 | n/a | pan = new_panel(win->win); |
---|
426 | n/a | if (pan == NULL) { |
---|
427 | n/a | PyErr_SetString(_curses_panelstate_global->PyCursesError, catchall_NULL); |
---|
428 | n/a | return NULL; |
---|
429 | n/a | } |
---|
430 | n/a | return (PyObject *)PyCursesPanel_New(pan, win); |
---|
431 | n/a | } |
---|
432 | n/a | |
---|
433 | n/a | |
---|
434 | n/a | /* Wrapper for panel_below(NULL). This function returns the top panel |
---|
435 | n/a | of the stack, so it's renamed to top_panel(). panel.below() |
---|
436 | n/a | *requires* a panel object in the first place which may be |
---|
437 | n/a | undesirable. */ |
---|
438 | n/a | static PyObject * |
---|
439 | n/a | PyCurses_top_panel(PyObject *self) |
---|
440 | n/a | { |
---|
441 | n/a | PANEL *pan; |
---|
442 | n/a | PyCursesPanelObject *po; |
---|
443 | n/a | |
---|
444 | n/a | PyCursesInitialised; |
---|
445 | n/a | |
---|
446 | n/a | pan = panel_below(NULL); |
---|
447 | n/a | |
---|
448 | n/a | if (pan == NULL) { /* valid output, it means |
---|
449 | n/a | there's no panel at all */ |
---|
450 | n/a | Py_RETURN_NONE; |
---|
451 | n/a | } |
---|
452 | n/a | po = find_po(pan); |
---|
453 | n/a | if (po == NULL) { |
---|
454 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
455 | n/a | "panel_below: can't find Panel Object"); |
---|
456 | n/a | return NULL; |
---|
457 | n/a | } |
---|
458 | n/a | Py_INCREF(po); |
---|
459 | n/a | return (PyObject *)po; |
---|
460 | n/a | } |
---|
461 | n/a | |
---|
462 | n/a | static PyObject *PyCurses_update_panels(PyObject *self) |
---|
463 | n/a | { |
---|
464 | n/a | PyCursesInitialised; |
---|
465 | n/a | update_panels(); |
---|
466 | n/a | Py_RETURN_NONE; |
---|
467 | n/a | } |
---|
468 | n/a | |
---|
469 | n/a | |
---|
470 | n/a | /* List of functions defined in the module */ |
---|
471 | n/a | |
---|
472 | n/a | static PyMethodDef PyCurses_methods[] = { |
---|
473 | n/a | {"bottom_panel", (PyCFunction)PyCurses_bottom_panel, METH_NOARGS}, |
---|
474 | n/a | {"new_panel", (PyCFunction)PyCurses_new_panel, METH_VARARGS}, |
---|
475 | n/a | {"top_panel", (PyCFunction)PyCurses_top_panel, METH_NOARGS}, |
---|
476 | n/a | {"update_panels", (PyCFunction)PyCurses_update_panels, METH_NOARGS}, |
---|
477 | n/a | {NULL, NULL} /* sentinel */ |
---|
478 | n/a | }; |
---|
479 | n/a | |
---|
480 | n/a | /* Initialization function for the module */ |
---|
481 | n/a | |
---|
482 | n/a | |
---|
483 | n/a | static struct PyModuleDef _curses_panelmodule = { |
---|
484 | n/a | PyModuleDef_HEAD_INIT, |
---|
485 | n/a | "_curses_panel", |
---|
486 | n/a | NULL, |
---|
487 | n/a | sizeof(_curses_panelstate), |
---|
488 | n/a | PyCurses_methods, |
---|
489 | n/a | NULL, |
---|
490 | n/a | _curses_panel_traverse, |
---|
491 | n/a | _curses_panel_clear, |
---|
492 | n/a | _curses_panel_free |
---|
493 | n/a | }; |
---|
494 | n/a | |
---|
495 | n/a | PyMODINIT_FUNC |
---|
496 | n/a | PyInit__curses_panel(void) |
---|
497 | n/a | { |
---|
498 | n/a | PyObject *m, *d, *v; |
---|
499 | n/a | |
---|
500 | n/a | /* Create the module and add the functions */ |
---|
501 | n/a | m = PyModule_Create(&_curses_panelmodule); |
---|
502 | n/a | if (m == NULL) |
---|
503 | n/a | goto fail; |
---|
504 | n/a | d = PyModule_GetDict(m); |
---|
505 | n/a | |
---|
506 | n/a | /* Initialize object type */ |
---|
507 | n/a | v = PyType_FromSpec(&PyCursesPanel_Type_spec); |
---|
508 | n/a | if (v == NULL) |
---|
509 | n/a | goto fail; |
---|
510 | n/a | ((PyTypeObject *)v)->tp_new = NULL; |
---|
511 | n/a | _curses_panelstate(m)->PyCursesPanel_Type = v; |
---|
512 | n/a | |
---|
513 | n/a | import_curses(); |
---|
514 | n/a | if (PyErr_Occurred()) |
---|
515 | n/a | goto fail; |
---|
516 | n/a | |
---|
517 | n/a | /* For exception _curses_panel.error */ |
---|
518 | n/a | _curses_panelstate(m)->PyCursesError = PyErr_NewException("_curses_panel.error", NULL, NULL); |
---|
519 | n/a | PyDict_SetItemString(d, "error", _curses_panelstate(m)->PyCursesError); |
---|
520 | n/a | |
---|
521 | n/a | /* Make the version available */ |
---|
522 | n/a | v = PyUnicode_FromString(PyCursesVersion); |
---|
523 | n/a | PyDict_SetItemString(d, "version", v); |
---|
524 | n/a | PyDict_SetItemString(d, "__version__", v); |
---|
525 | n/a | Py_DECREF(v); |
---|
526 | n/a | return m; |
---|
527 | n/a | fail: |
---|
528 | n/a | Py_XDECREF(m); |
---|
529 | n/a | return NULL; |
---|
530 | n/a | } |
---|