| 1 | =head1 NAME | ||
| 2 | |||
| 3 | AnyEvent::Impl::Tk - AnyEvent adaptor for Tk | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use AnyEvent; | ||
| 8 | use Tk; | ||
| 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 Tk work with AnyEvent except by loading Tk before | ||
| 16 | creating the first AnyEvent watcher. | ||
| 17 | |||
| 18 | Tk is buggy. Tk is extremely buggy. Tk is so unbelievably buggy that | ||
| 19 | for each bug reported and fixed, you get one new bug followed by | ||
| 20 | reintroduction of the old bug in a later revision. It is also basically | ||
| 21 | unmaintained: the maintainers are not even interested in improving | ||
| 22 | the situation - reporting bugs is considered rude, and fixing bugs is | ||
| 23 | considered changing holy code, so it's apparently better to leave it | ||
| 24 | broken. | ||
| 25 | |||
| 26 | I regularly run out of words to describe how bad it really is. | ||
| 27 | |||
| 28 | To work around the many, many bugs in Tk that don't get fixed, this | ||
| 29 | adaptor dup()'s all filehandles that get passed into its I/O watchers, | ||
| 30 | so if you register a read and a write watcher for one fh, AnyEvent will | ||
| 31 | create two additional file descriptors (and handles). | ||
| 32 | |||
| 33 | This creates a high overhead and is slow, but seems to work around most | ||
| 34 | known bugs in L<Tk::fileevent> on 32 bit architectures (Tk seems to be | ||
| 35 | terminally broken on 64 bit, do not expect more than 10 or so watchers to | ||
| 36 | work on 64 bit machines). | ||
| 37 | |||
| 38 | Note also that Tk event ids wrap around after 2**32 or so events, which on | ||
| 39 | my machine can happen within less than 12 hours, after which Tk will stomp | ||
| 40 | on random other events and kill them. So don't run Tk programs for more | ||
| 41 | than an hour or so. | ||
| 42 | |||
| 43 | To be able to access the Tk event loop, this module creates a main | ||
| 44 | window and withdraws it immediately. This might cause flickering on some | ||
| 45 | platforms, but Tk perversely requires a window to be able to wait for file | ||
| 46 | handle readyness notifications. This window is always created (in this | ||
| 47 | version of AnyEvent) and can be accessed as C<$AnyEvent::Impl::Tk::mw>. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | package AnyEvent::Impl::Tk; | ||
| 52 | |||
| 53 | use AnyEvent (); BEGIN { AnyEvent::common_sense } | ||
| 54 | use Tk (); | ||
| 55 | |||
| 56 | our $mw = new MainWindow; | ||
| 57 | $mw->withdraw; | ||
| 58 | |||
| 59 | sub io { | ||
| 60 | my (undef, %arg) = @_; | ||
| 61 | |||
| 62 | # work around these bugs in Tk: | ||
| 63 | # - removing a callback will destroy other callbacks | ||
| 64 | # - removing a callback might crash | ||
| 65 | # - adding a callback might destroy other callbacks | ||
| 66 | # - only one callback per fh | ||
| 67 | # - only one callback per fh/poll combination | ||
| 68 | my ($fh, $tk) = AnyEvent::_dupfh $arg{poll}, $arg{fh}, "readable", "writable"; | ||
| 69 | |||
| 70 | $mw->fileevent ($fh, $tk => $arg{cb}); | ||
| 71 | |||
| 72 | bless [$fh, $tk], "AnyEvent::Impl::Tk::io" | ||
| 73 | } | ||
| 74 | |||
| 75 | sub AnyEvent::Impl::Tk::io::DESTROY { | ||
| 76 | my ($fh, $tk) = @{$_[0]}; | ||
| 77 | |||
| 78 | # work around another bug: watchers don't get removed when | ||
| 79 | # the fh is closed contrary to documentation. also, trying | ||
| 80 | # to unregister a read callback will make it impossible | ||
| 81 | # to remove the write callback. | ||
| 82 | $mw->fileevent ($fh, $tk => ""); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub timer { | ||
| 86 | my (undef, %arg) = @_; | ||
| 87 | |||
| 88 | my $after = $arg{after} < 0 ? 0 : $arg{after} * 1000; | ||
| 89 | my $cb = $arg{cb}; | ||
| 90 | my $id; | ||
| 91 | |||
| 92 | if ($arg{interval}) { | ||
| 93 | my $ival = $arg{interval} * 1000; | ||
| 94 | my $rcb = sub { | ||
| 95 | $id = Tk::after $mw, $ival, [$_[0], $_[0]]; | ||
| 96 | &$cb; | ||
| 97 | }; | ||
| 98 | $id = Tk::after $mw, $after, [$rcb, $rcb]; | ||
| 99 | } else { | ||
| 100 | # tk blesses $cb, thus the extra indirection | ||
| 101 | $id = Tk::after $mw, $after, sub { &$cb }; | ||
| 102 | } | ||
| 103 | |||
| 104 | bless \\$id, "AnyEvent::Impl::Tk::after" | ||
| 105 | } | ||
| 106 | |||
| 107 | sub idle { | ||
| 108 | my (undef, %arg) = @_; | ||
| 109 | |||
| 110 | my $cb = $arg{cb}; | ||
| 111 | my $id; | ||
| 112 | my $rcb = sub { | ||
| 113 | # in their endless stupidity, they decided to give repeating idle watchers | ||
| 114 | # strictly higher priority than timers :/ | ||
| 115 | $id = Tk::after $mw, 0 => [sub { | ||
| 116 | $id = Tk::after $mw, idle => [$_[0], $_[0]]; | ||
| 117 | }, $_[0]]; | ||
| 118 | &$cb; | ||
| 119 | }; | ||
| 120 | |||
| 121 | $id = Tk::after $mw, idle => [$rcb, $rcb]; | ||
| 122 | bless \\$id, "AnyEvent::Impl::Tk::after" | ||
| 123 | } | ||
| 124 | |||
| 125 | sub AnyEvent::Impl::Tk::after::DESTROY { | ||
| 126 | Tk::after $mw, cancel => $${$_[0]}; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub one_event { | ||
| 130 | Tk::DoOneEvent (0); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub loop { | ||
| 134 | Tk::MainLoop; | ||
| 135 | } | ||
| 136 | |||
| 137 | 1; | ||
| 138 | |||
| 139 | =head1 SEE ALSO | ||
| 140 | |||
| 141 | L<AnyEvent>, L<Tk>. | ||
| 142 | |||
| 143 | =head1 AUTHOR | ||
| 144 | |||
| 145 | Marc Lehmann <schmorp@schmorp.de> | ||
| 146 | http://home.schmorp.de/ | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 |