Debian Codegraph @

swig/Fl.i

raw
1 /* File : Fl.i */
2 //%module Fl
3
4 %include "typemaps.i"
5
6 // hack to convince SWIG that Fl_Color is something different than it really is!
7 %apply unsigned int { enum Fl_Color};
8 %apply const unsigned int& { const enum Fl_Color&};
9 // end hack
10
11 // used for get_color
12 %apply uchar* OUTPUT { uchar& };
13
14 %feature("docstring") ::Fl
15 """The Fl class is the FLTK global (static) class containing state
16 information and global methods for the current application.""" ;
17
18 // The following lines will create getter and setter for the named variables
19 %rename("e_number", %$isvariable) "e_number";
20 %rename("e_x", %$isvariable) "e_x";
21 %rename("e_y", %$isvariable) "e_y";
22 %rename("e_x_root", %$isvariable) "e_x_root";
23 %rename("e_y_root", %$isvariable) "e_y_root";
24 %rename("e_dx", %$isvariable) "e_dx";
25 %rename("e_dy", %$isvariable) "e_dy";
26 %rename("e_state", %$isvariable) "e_state";
27 %rename("e_clicks", %$isvariable) "e_click";
28 %rename("e_is_click", %$isvariable) "e_is_click";
29 %rename("e_keysym", %$isvariable) "e_keysym";
30
31
32 %{
33 #include "FL/Fl.H"
34 #include <signal.h>
35 %}
36
37
38 %ignore Fl::grab(Fl_Window &);
39
40 %ignore Fl::has_check(Fl_Timeout_Handler, void* = 0);
41 %ignore Fl::set_labeltype(Fl_Labeltype, Fl_Labeltype from);
42 %ignore Fl::add_timeout(double t, Fl_Timeout_Handler,void* = 0);
43 %ignore Fl::repeat_timeout(double t, Fl_Timeout_Handler,void* = 0);
44 %ignore Fl::remove_timeout(Fl_Timeout_Handler,void* = 0);
45 %ignore Fl::add_check(Fl_Timeout_Handler, void* = 0);
46 %ignore Fl::add_idle(void (*cb)(void*), void* = 0);
47 %ignore Fl::set_idle(void (*cb)());
48 %ignore Fl::remove_idle(void (*cb)(void*), void* = 0);
49 %ignore Fl::get_color(Fl_Color);
50 %ignore Fl::add_fd;
51 %ignore Fl::remove_fd;
52 %ignore Fl::add_handler;
53 %ignore Fl::remove_handler;
54 %ignore Fl::remove_check;
55 %ignore Fl::gl_visual;
56 %ignore Fl::get_awake_handler_;
57 %ignore Fl::copy_image;
58
59 //%wrapper %{
60 //#ifdef __APPLE__
61 // // empty implementation
62 // void Fl::free_color(Fl_Color i, int overlay)
63 // {
64 // }
65 //#endif
66 // %}
67
68 /* ignore multithreading, but see bottom for actual support */
69 //%ignore Fl::lock();
70 //%ignore Fl::unlock();
71 //%ignore Fl::awake(void* message = 0);
72 //%ignore Fl::thread_message();
73
74 /* missing wrappings */
75 //%ignore Fl::add_fd;
76 //%ignore Fl::remove_fd;
77 /* end missing wrappings */
78
79 // font related stuff
80 //%apply int* OUTPUT { int*& sizep };
81 %apply int* OUTPUT { int* attributes };
82 %apply int& OUTPUT { int& X };
83 %apply int& OUTPUT { int& Y };
84 %apply int& OUTPUT { int& W };
85 %apply int& OUTPUT { int& H };
86
87 %ignore Fl::get_font_sizes;
88
89 %include "FL/Fl.H"
90
91 void pyFLTK_controlIdleCallbacks(int enable);
92
93 %{
94 // called by FLTK when idle
95 PyObject *registeredDoIDle = 0;
96 void pyFLTK_idleCallback(void *data)
97 {
98 PyObject *arglist;
99 PyObject *result;
100 arglist = Py_BuildValue("()"); // Build argument list
101 result = PyObject_CallObject(registeredDoIDle, arglist); // Call Python
102 Py_DECREF(arglist); // Trash arglist
103 //result = PyObject_CallObject(func,(PyObject *)0); // Call Python
104 Py_XDECREF(result);
105 if (PyErr_Occurred())
106 {
107 PyErr_Print();
108 }
109 }
110
111
112 // turn on/off idle callback into the Python interpreter
113 void pyFLTK_controlIdleCallbacks(int enable)
114 {
115 if (enable)
116 {
117 Fl::add_idle(pyFLTK_idleCallback, 0);
118 }
119 else
120 {
121 Fl::remove_idle(pyFLTK_idleCallback);
122 }
123 }
124 %}
125
126 %native(pyFLTK_registerDoIdle) PyObject * registerDoIdle(PyObject *self,
127 PyObject *args);
128
129 %{
130 // called by the initialization code in the module -
131 // not meant for user consumption
132 PyObject *registerDoIdle(PyObject *self, PyObject *args)
133 {
134 PyArg_ParseTuple( args, "O", &registeredDoIDle);
135 if (!PyCallable_Check(registeredDoIDle))
136 {
137 PyErr_SetString(PyExc_TypeError, "Need a callable object!");
138 }
139 else
140 {
141 Py_INCREF(registeredDoIDle);
142 }
143 Py_INCREF(Py_None);
144 return Py_None;
145 }
146
147 %}
148
149 //implement time out processing
150
151 %native(Fl_add_timeout) PyObject * Fl_add_timeout(PyObject *self,
152 PyObject *args);
153 %native(Fl_repeat_timeout) PyObject * Fl_repeat_timeout(PyObject *self,
154 PyObject *args);
155 %native(Fl_remove_timeout) PyObject * Fl_remove_timeout(PyObject *self,
156 PyObject *args);
157
158
159 %{
160
161
162
163 #include "CallbackStruct.h"
164
165 struct timeout_link {
166 CallbackStruct *handle;
167 timeout_link *next;
168 };
169
170 static timeout_link *py_timeout_funcs = NULL;
171
172
173 void pyFLTK_timeoutCallback(void *data)
174 {
175 CallbackStruct *cb = (CallbackStruct *)data;
176 PyObject *result;
177
178 if (py_timeout_funcs != NULL) {
179 timeout_link *l, *p;
180 for (l = py_timeout_funcs, p = 0; l && l->handle != cb; p = l, l = l->next);
181
182 if (l) {
183 // Found it, so remove it from the list...
184 if (p)
185 p->next = l->next;
186 else
187 py_timeout_funcs = l->next;
188
189 // And free the record...
190 delete l;
191 }
192 }
193
194 // check for NULL arguments
195 PyObject *args = NULL;
196 if (cb->data) {
197 args = Py_BuildValue("(O)", cb->data);
198 if (PyErr_Occurred())
199 {
200 PyErr_Print();
201 }
202 }
203 result = PyObject_CallObject(cb->func, args); // Call Python
204 if (PyErr_Occurred())
205 {
206 PyErr_Print();
207 }
208 Py_DECREF(cb->func);
209 Py_XDECREF(cb->data);
210 Py_XDECREF(args);
211 Py_XDECREF(result);
212 delete cb;
213 if (PyErr_Occurred())
214 {
215 PyErr_Print();
216 }
217 }
218
219 PyObject *Fl_add_timeout(PyObject *self, PyObject *args)
220 {
221 float numSeconds;
222 PyObject *func = 0;
223 PyObject *data = 0;
224
225 PyArg_ParseTuple( args, "fO|O", &numSeconds, &func, &data);
226 if (!PyCallable_Check(func))
227 {
228 PyErr_SetString(PyExc_TypeError, "Need a callable object!");
229 }
230 else
231 {
232 CallbackStruct *cb = new CallbackStruct( func, data, (PyObject*)0 );
233 // add reference to list
234 timeout_link *t = new timeout_link;
235 t->handle = cb;
236 t->next = py_timeout_funcs;
237 py_timeout_funcs = t;
238 // function call
239 Py_INCREF(func);
240 Py_XINCREF(data);
241 Fl::add_timeout( numSeconds, pyFLTK_timeoutCallback, cb);
242 }
243
244 Py_INCREF(Py_None);
245 return Py_None;
246 }
247
248 PyObject *Fl_repeat_timeout(PyObject *self, PyObject *args)
249 {
250 float numSeconds;
251 PyObject *func = 0;
252 PyObject *data = 0;
253
254 PyArg_ParseTuple( args, "fO|O", &numSeconds, &func, &data);
255 if (!PyCallable_Check(func))
256 {
257 PyErr_SetString(PyExc_TypeError, "Need a callable object!");
258 }
259 else
260 {
261 CallbackStruct *cb = new CallbackStruct( func, data, (PyObject*)0 );
262 // add reference to list
263 timeout_link *t = new timeout_link;
264 t->handle = cb;
265 t->next = py_timeout_funcs;
266 py_timeout_funcs = t;
267 // function call
268 Py_INCREF(func);
269 Py_XINCREF(data);
270 Fl::repeat_timeout( numSeconds, pyFLTK_timeoutCallback, cb);
271 }
272
273 Py_INCREF(Py_None);
274 return Py_None;
275 }
276
277 PyObject *Fl_remove_timeout(PyObject *self, PyObject *args)
278 {
279 PyObject *func = 0;
280 PyObject *data = 0;
281 PyArg_ParseTuple( args, "O|O", &func, &data);
282 if (!PyCallable_Check(func))
283 {
284 PyErr_SetString(PyExc_TypeError, "Need a callable object!");
285 }
286 else
287 {
288 //printf("Fl_Remove_timeout callable found\n");
289 //CallbackStruct *cb = new CallbackStruct( func, data, (PyObject*)0 );
290 //Fl::remove_timeout( pyFLTK_timeoutCallback, cb);
291 //Py_DECREF(cb->func);
292 //Py_XDECREF(cb->data);
293
294 if (py_timeout_funcs != NULL)
295 {
296 timeout_link *l, *p;
297
298 l = py_timeout_funcs;
299 p = 0;
300 while (l != NULL)
301 {
302 // compare the functions
303 if( PyObject_RichCompareBool( l->handle->func, func, Py_EQ ) == 1 )
304 {
305 // if needed, also compare data
306 if (!(data == NULL || ( PyObject_RichCompareBool( l->handle->data, data, Py_EQ ) == 1)))
307 {
308 //printf("Fl_Remove_timeout incomplete match\n");
309 p = l;
310 l = l->next;
311 continue;
312 }
313 //printf("Removing: l = %xd, handle = %xd; func = %xd; data = %xd\n", l, l->handle, l->handle->func, l->handle->data);
314 // Found it, so remove it from the list...
315 if (p)
316 p->next = l->next;
317 else {
318 py_timeout_funcs = l->next;
319 }
320 Fl::remove_timeout( pyFLTK_timeoutCallback, l->handle);
321 Py_DECREF(l->handle->func);
322 Py_XDECREF(l->handle->data);
323
324 // And free the record...
325 l->handle = NULL;
326 l->next = NULL;
327 delete l;
328 l = NULL;
329 }
330 if (l) {
331 p = l;
332 l = l->next;
333 }
334 else {
335 l = py_timeout_funcs;
336 p = 0;
337 }
338 }
339 }
340 }
341 Py_INCREF(Py_None);
342 return Py_None;
343 }
344
345 %}
346
347 // implement file descriptors
348 %native(Fl_add_fd) PyObject * Fl_add_fd(PyObject *self, PyObject *args);
349 %native(Fl_remove_fd) PyObject * Fl_remove_fd(PyObject *self, PyObject *args);
350
351 %{
352
353 struct fd_link {
354 int fd;
355 PyObject *func;
356 PyObject *data;
357 fd_link *next;
358 };
359
360 static fd_link *py_fd_funcs = NULL;
361
362 void pyFLTK_fdCallback(int fd, void* data) {
363 // check for NULL arguments
364 PyObject *args = NULL;
365
366 // loop through Python handler
367 for (const fd_link *hl = py_fd_funcs; hl; hl = hl->next) {
368 if (hl->fd == fd) {
369 if (hl->data == NULL)
370 args = Py_BuildValue("(i)", fd);
371 else
372 args = Py_BuildValue("(iO)", fd, hl->data);
373 PyObject_CallObject(hl->func, args); // Call Python
374 if (PyErr_Occurred())
375 {
376 PyErr_Print();
377 }
378 break;
379 }
380 }
381
382 Py_XDECREF(args);
383 }
384
385 PyObject *Fl_add_fd(PyObject *self, PyObject *args)
386 {
387 int fd;
388 int when;
389 bool long_signature = true;
390 PyObject *func = 0;
391 PyObject *data = 0;
392
393 PyArg_ParseTuple( args, "iiO|O", &fd, &when, &func, &data);
394 if (PyErr_Occurred())
395 {
396 PyErr_Clear();
397 PyArg_ParseTuple( args, "iO|O", &fd, &func, &data);
398 long_signature = false;
399 if (PyErr_Occurred())
400 {
401 PyErr_Print();
402 }
403 }
404 if (!PyCallable_Check(func))
405 {
406 PyErr_SetString(PyExc_TypeError, "Need a callable object!");
407 }
408 else
409 {
410 fd_link *cb = py_fd_funcs;
411 while (cb != NULL) {
412 if (cb->fd == fd) {
413 Py_INCREF(cb->func);
414 Py_XINCREF(cb->data);
415 cb->func = func;
416 cb->data = data;
417 break;
418 }
419 cb = cb->next;
420 }
421 if (cb == NULL) {
422 // only add one callback, actual callback list is kept in Python
423 if (long_signature)
424 Fl::add_fd(fd, when, (Fl_FD_Handler)pyFLTK_fdCallback);
425 else
426 Fl::add_fd(fd, (Fl_FD_Handler)pyFLTK_fdCallback);
427
428 // add the python callback
429 fd_link *cb = new fd_link;
430 cb->next = py_fd_funcs;
431 cb->fd = fd;
432 cb->func = func;
433 cb->data = data;
434 py_fd_funcs = cb;
435 Py_INCREF(func);
436 Py_XINCREF(data);
437 }
438 }
439
440 Py_INCREF(Py_None);
441 return Py_None;
442 }
443
444 PyObject * Fl_remove_fd(PyObject *self, PyObject *args)
445 {
446 int fd;
447 PyArg_ParseTuple( args, "i", &fd);
448 if (PyErr_Occurred())
449 {
450 PyErr_Print();
451 }
452
453 if (py_fd_funcs != NULL) {
454 fd_link *l, *p;
455
456 // Search for the handler in the list...
457 for (l = py_fd_funcs, p = 0; l && l->fd != fd; p = l, l = l->next);
458
459 if (l) {
460 // Found it, so remove it from the list...
461 if (p)
462 p->next = l->next;
463 else
464 py_fd_funcs = l->next;
465
466 // reference count
467 Py_DECREF(l->func);
468 Py_XDECREF(l->data);
469
470 // And free the record...
471 delete l;
472
473 // remove the fltk handler to avoid memory leaks
474 Fl::remove_fd(fd);
475 }
476 }
477 Py_INCREF(Py_None);
478 return Py_None;
479 }
480 %}
481
482 // implement global handler
483 %native(Fl_add_handler) PyObject * Fl_add_handler(PyObject *self,
484 PyObject *args);
485
486 %native(Fl_remove_handler) PyObject * Fl_remove_handler(PyObject *self,
487 PyObject *args);
488
489 %{
490
491 struct handler_link {
492 //int (*handle)(int);
493 PyObject *handle;
494 handler_link *next;
495 };
496
497 static handler_link *py_handler_funcs = NULL;
498
499 int pyFLTK_handlerCallback(int data)
500 {
501 PyObject *result = NULL;
502
503 // check for NULL arguments
504 PyObject *args = NULL;
505 args = Py_BuildValue("(i)", data);
506 if (PyErr_Occurred())
507 {
508 PyErr_Print();
509 }
510
511 // loop through Python handler
512 int c_result = 0;
513 for (const handler_link *hl = py_handler_funcs; hl; hl = hl->next) {
514 result = PyObject_CallObject(hl->handle, args); // Call Python
515 if (PyErr_Occurred())
516 {
517 PyErr_Print();
518 }
519
520
521 if (result){
522 SWIG_AsVal_int(result, &c_result);
523 }
524 if (c_result) break;
525 }
526
527 Py_XDECREF(args);
528 Py_XDECREF(result);
529
530 return c_result;
531 }
532
533 PyObject *Fl_add_handler(PyObject *self, PyObject *args)
534 {
535 PyObject *func = 0;
536
537 PyArg_ParseTuple( args, "O", &func);
538 if (!PyCallable_Check(func))
539 {
540 PyErr_SetString(PyExc_TypeError, "Need a callable object!");
541 }
542 else
543 {
544 if (py_handler_funcs == NULL) {
545 // only add one callback, actual callback list is kept in Python
546 Fl::add_handler(pyFLTK_handlerCallback);
547 }
548 // add the python callback
549 handler_link *cb = new handler_link;
550 cb->next = py_handler_funcs;
551 cb->handle = func;
552 py_handler_funcs = cb;
553 Py_INCREF(func);
554 }
555
556 Py_INCREF(Py_None);
557 return Py_None;
558 }
559
560 PyObject * Fl_remove_handler(PyObject *self, PyObject *args)
561 {
562 PyObject *func = 0;
563 PyArg_ParseTuple( args, "O", &func);
564
565 if (py_handler_funcs != NULL) {
566 handler_link *l, *p;
567
568 // Search for the handler in the list...
569 for (l = py_handler_funcs, p = 0; l && l->handle != func; p = l, l = l->next);
570
571 if (l) {
572 // Found it, so remove it from the list...
573 if (p)
574 p->next = l->next;
575 else
576 py_handler_funcs = l->next;
577
578 // reference count
579 Py_DECREF(l->handle);
580
581 // And free the record...
582 delete l;
583 }
584 // was this the last one?
585 if (py_handler_funcs == NULL) {
586 // remove the fltk handler to avoid memory leaks
587 Fl::remove_handler(pyFLTK_handlerCallback);
588 }
589 }
590 Py_INCREF(Py_None);
591 return Py_None;
592 }
593
594
595
596
597 %}
598
599 // implement global check
600 %native(Fl_add_check) PyObject * Fl_add_check(PyObject *self,
601 PyObject *args);
602
603 %native(Fl_remove_check) PyObject * Fl_remove_check(PyObject *self,
604 PyObject *args);
605
606 %{
607
608 struct check_link {
609 //int (*handle)(int);
610 PyObject *handle;
611 PyObject *args;
612 check_link *next;
613 };
614
615 static check_link *py_check_funcs = NULL;
616
617 void pyFLTK_checkCallback(void *data)
618 {
619 PyObject *result = NULL;
620
621
622
623 // loop through Python handler
624 for (const check_link *hl = py_check_funcs; hl; hl = hl->next) {
625 // check for NULL arguments
626 PyObject *args = NULL;
627 if (hl->args) {
628 args = Py_BuildValue("(O)", hl->args);
629 }
630 else {
631 args = Py_BuildValue("()");
632 }
633 if (PyErr_Occurred()) {
634 PyErr_Print();
635 }
636 result = PyObject_CallObject(hl->handle, args); // Call Python
637 if (PyErr_Occurred())
638 {
639 PyErr_Print();
640 }
641 Py_XDECREF(args);
642 Py_XDECREF(result);
643 }
644 }
645
646 PyObject *Fl_add_check(PyObject *self, PyObject *args)
647 {
648 PyObject *func = 0;
649 PyObject *data = 0;
650
651 PyArg_ParseTuple( args, "O|O", &func, &data);
652 if (!PyCallable_Check(func))
653 {
654 PyErr_SetString(PyExc_TypeError, "Fl.add_check: need a callable object!");
655 }
656 else
657 {
658 if (py_check_funcs == NULL) {
659 // only add one callback, actual callback list is kept in Python
660 Fl::add_check(pyFLTK_checkCallback);
661 }
662 // add tye python callback
663 check_link *cb = new check_link;
664 cb->next = py_check_funcs;
665 cb->handle = func;
666 cb->args = data;
667 py_check_funcs = cb;
668 Py_INCREF(func);
669 Py_XINCREF(data);
670 }
671
672 Py_INCREF(Py_None);
673 return Py_None;
674 }
675
676 PyObject * Fl_remove_check(PyObject *self, PyObject *args)
677 {
678 PyObject *func = 0;
679 PyObject *data = 0;
680 PyArg_ParseTuple( args, "O|0", &func, &data);
681
682 if (py_check_funcs != NULL) {
683 check_link *l, *p;
684
685 // Search for the handler in the list...
686 for (l = py_check_funcs, p = 0; l && l->handle != func; p = l, l = l->next);
687
688 if (l) {
689 // Found it, so remove it from the list...
690 if (p)
691 p->next = l->next;
692 else
693 py_check_funcs = l->next;
694
695 // reference count
696 Py_DECREF(l->handle);
697 Py_XDECREF(l->args);
698
699 // And free the record...
700 delete l;
701 }
702 // was this the last one?
703 if (py_check_funcs == NULL) {
704 // remove the fltk handler to avoid memory leaks
705 Fl::remove_check(pyFLTK_checkCallback);
706 }
707 }
708 Py_INCREF(Py_None);
709 return Py_None;
710 }
711
712
713
714
715 %}
716
717 // clean up typemaps
718 %clear uchar&;
719 %clear int& X;
720 %clear int& Y;
721 %clear int& W;
722 %clear int& H;
723
724 %{
725
726
727 %}
728
729 %extend Fl {
730 #include <signal.h>
731 #define BARF(fmt, ...) PyErr_Format(PyExc_RuntimeError, "%s:%d %s(): " fmt, __FILE__, __LINE__, __func__)
732 // Python is silly. There's some nuance about signal handling where it sets a
733 // SIGINT (ctrl-c) handler to just set a flag, and the python layer then reads
734 // this flag and does the thing. Here I'm running C code, so SIGINT would set a
735 // flag, but not quit, so I can't interrupt the capture. Thus I reset the SIGINT
736 // handler to the default, and put it back to the python-specific version when
737 // I'm done
738 #define SET_SIGINT() \
739 struct sigaction sigaction_new = {}; \
740 sigaction_new.sa_handler = SIG_DFL; \
741 if( 0 != sigaction(SIGINT, \
742 &sigaction_new, \
743 &sigaction_old) ) \
744 { \
745 BARF("sigaction() failed"); \
746 }
747
748 #define RESET_SIGINT() \
749 if( 0 != sigaction(SIGINT, \
750 &sigaction_old, NULL )) \
751 BARF("sigaction-restore failed");
752
753 static int run_with_default_sigint_handler()
754 {
755 int result = 1;
756 #if defined(LINUX)
757 struct sigaction sigaction_old = {};
758 SET_SIGINT();
759 #endif
760 result = Fl::run();
761 #if defined(LINUX)
762 RESET_SIGINT();
763 #endif
764 return result;
765 }
766
767 static int wait_with_default_sigint_handler(double time)
768 {
769 int result = 1;
770 #if defined(LINUX)
771 struct sigaction sigaction_old = {};
772 SET_SIGINT();
773 #endif
774 result = Fl::wait(time);
775 #if defined(LINUX)
776 RESET_SIGINT();
777 #endif
778 return result;
779 }
780
781 static void fltk_exit()
782 {
783 while(Fl::first_window())
784 {
785 Fl::first_window()->hide();
786 Fl::check();
787 }
788 }
789 }
790
791 %pythoncode %{
792 def Fl_mt_run(window):
793 import time
794 while window.visible():
795 time.sleep(0.1)
796 Fl.check()
797
798 Fl.mt_run = staticmethod(Fl_mt_run)
799 %}
800
801 %native(Fl_get_font_sizes_tmp) PyObject * Fl_get_font_sizes_tmp(PyObject *self,
802 PyObject *args);
803
804 %{
805 PyObject *Fl_get_font_sizes_tmp(PyObject *self, PyObject *args)
806 {
807 int fontName;
808
809 PyArg_ParseTuple( args, "i", &fontName);
810
811 int *fontSizes = NULL;
812 int size = Fl::get_font_sizes(fontName, fontSizes);
813
814 PyObject *result = PyList_New(size);
815 for (int i = 0; i < size; i++) {
816 PyObject *o = PyLong_FromLong(fontSizes[i]);
817 PyList_SetItem(result,i,o);
818 }
819
820 Py_INCREF(result);
821 return result;
822 }
823 %}
824
825