| 1 | =head1 NAME | ||
| 2 | |||
| 3 | AnyEvent::Impl::FLTK - AnyEvent adaptor for FLTK (Fast Light Toolkit version two) | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use AnyEvent; | ||
| 8 | use FLTK; | ||
| 9 | |||
| 10 | # this module gets loaded automatically as required | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | This module provides transparent support for AnyEvent. You don't have to | ||
| 15 | do anything to make FLTK work with AnyEvent except by loading FLTK before | ||
| 16 | creating the first AnyEvent watcher. | ||
| 17 | |||
| 18 | This implementation is not to be confused with AnyEvent::Impl::FLTK by | ||
| 19 | Sanko Robinson. That implementation is completely broken, and the author | ||
| 20 | is apparently unreachable. | ||
| 21 | |||
| 22 | In any case, FLTK suffers from typical GUI-ToolKit diseases, such as O(n) | ||
| 23 | or worse for every operation (adding a timer, destroying a timer etc.), | ||
| 24 | the typical Not-Well-Tested Perl Interface disases such as non-random | ||
| 25 | memory corruption and the typical Event-Loop-as-an-Afterthrough issues, | ||
| 26 | such as multiple watchers on the same fd silently overwriting the others. | ||
| 27 | |||
| 28 | It doesn't have native idle, signal or child watchers, so all of these are | ||
| 29 | emulated. | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | package AnyEvent::Impl::FLTK; | ||
| 34 | |||
| 35 | use AnyEvent (); BEGIN { AnyEvent::common_sense } | ||
| 36 | use FLTK 0.532 (); | ||
| 37 | use Scalar::Util (); | ||
| 38 | |||
| 39 | #*AE::timer = \&EV::timer; | ||
| 40 | #*AE::signal = \&EV::signal; | ||
| 41 | #*AE::idle = \&EV::idle; | ||
| 42 | |||
| 43 | # FLTK::get_time_secs returns a glob :/ | ||
| 44 | # on unix, fltk uses gettimeofday, so we are likely compatible | ||
| 45 | # on windows, fltk uses GetTickCount, to which we are unlikely to be compatible with. | ||
| 46 | #sub time { FLTK::get_time_secs } | ||
| 47 | #*now = \&time; | ||
| 48 | |||
| 49 | sub timer_interval_cb { | ||
| 50 | my $id = shift; # add_timeout kills @_, so we have to make a copy :( | ||
| 51 | $id->[0] = FLTK::add_timeout $id->[1], \&timer_interval_cb, $id; | ||
| 52 | &{ $id->[2] } | ||
| 53 | } | ||
| 54 | |||
| 55 | sub timer { | ||
| 56 | my ($class, %arg) = @_; | ||
| 57 | |||
| 58 | my $cb = $arg{cb}; | ||
| 59 | |||
| 60 | if ($arg{interval}) { | ||
| 61 | my $id = [undef, $arg{interval}, $cb]; | ||
| 62 | |||
| 63 | $id->[0] = FLTK::add_timeout $arg{after}, \&timer_interval_cb, $id; | ||
| 64 | |||
| 65 | return bless $id, "AnyEvent::Impl::FLTK::timer" | ||
| 66 | } else { | ||
| 67 | # non-repeating timers can be done very efficiently | ||
| 68 | # also, FLTK doesn't like callable objects | ||
| 69 | return FLTK::add_timeout $arg{after}, sub { &$cb } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | sub AnyEvent::Impl::FLTK::timer::DESTROY { | ||
| 74 | undef $_[0][0]; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub io { | ||
| 78 | my ($class, %arg) = @_; | ||
| 79 | |||
| 80 | # only one watcher/fd :( | ||
| 81 | |||
| 82 | my $cb = $arg{cb}; | ||
| 83 | my ($fh, $ev) = AnyEvent::_dupfh $arg{poll}, $arg{fh}, | ||
| 84 | FLTK::READ, | ||
| 85 | FLTK::WRITE | (AnyEvent::WIN32 ? FLTK::EXCEPT : 0); | ||
| 86 | |||
| 87 | # fltk hardcodes poll constants and aliases EXCEPT with POLLERR, | ||
| 88 | # which is grossly wrong, but likely it doesn't use poll on windows. | ||
| 89 | FLTK::add_fd $fh, $ev, sub { &$cb } | ||
| 90 | } | ||
| 91 | |||
| 92 | # use signal and child emulation - fltk has no facilities for that | ||
| 93 | |||
| 94 | # fltk idle watchers are like EV::check watchers, and fltk check watchers | ||
| 95 | # are like EV::prepare watchers. both are called when the loop is busy, | ||
| 96 | # so we have to use idle watcher emulation. | ||
| 97 | |||
| 98 | sub _poll { | ||
| 99 | FLTK::wait; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub AnyEvent::CondVar::Base::_wait { | ||
| 103 | FLTK::wait until exists $_[0]{_ae_sent}; | ||
| 104 | } | ||
| 105 | |||
| 106 | #sub loop { | ||
| 107 | # FLTK::run; | ||
| 108 | #} | ||
| 109 | |||
| 110 | =head1 SEE ALSO | ||
| 111 | |||
| 112 | L<AnyEvent>, L<FLTK>. | ||
| 113 | |||
| 114 | =head1 AUTHOR | ||
| 115 | |||
| 116 | Marc Lehmann <schmorp@schmorp.de> | ||
| 117 | http://anyevent.schmorp.de | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | 1 | ||
| 122 |