blib/lib/AnyEvent/Impl/Tk.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 72 CC 3 SCAR 14.6
CC
3
Cov
0%
timer
12.0
BEGIN
2.0
END
2.0
CRAP 4.3
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 some of the many, many bugs in Tk that don't get fixed,
29 this adaptor dup()'s all filehandles that get passed into its I/O
30 watchers, so if you register a read and a write watcher for one fh,
31 AnyEvent will 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 Do not expect these workarounds to avoid segfaults and crashes inside Tk.
39  
40 Note also that Tk event ids wrap around after 2**32 or so events, which on
41 my machine can happen within less than 12 hours, after which Tk will stomp
42 on random other events and kill them. So don't run Tk programs for more
43 than an hour or so.
44  
45 To be able to access the Tk event loop, this module creates a main
46 window and withdraws it immediately. This might cause flickering on some
47 platforms, but Tk perversely requires a window to be able to wait for file
48 handle readyness notifications. This window is always created (in this
49 version of AnyEvent) and can be accessed as C<$AnyEvent::Impl::Tk::mw>.
50  
51 =cut
52  
53 package AnyEvent::Impl::Tk;
54  
55 use AnyEvent (); BEGIN { AnyEvent::common_sense }
56 use Tk ();
57  
58 our $mw = new MainWindow -title => "AnyEvent Dummy Window";
59 $mw->withdraw;
60  
61 END { undef $mw }
62  
63 sub io {
64 my (undef, %arg) = @_;
65  
66 # work around these bugs in Tk:
67 # - removing a callback will destroy other callbacks
68 # - removing a callback might crash
69 # - adding a callback might destroy other callbacks
70 # - only one callback per fh
71 # - only one callback per fh/poll combination
72 my ($fh, $tk) = AnyEvent::_dupfh $arg{poll}, $arg{fh}, "readable", "writable";
73  
74 $mw->fileevent ($fh, $tk => $arg{cb});
75  
76 bless [$fh, $tk], "AnyEvent::Impl::Tk::io"
77 }
78  
79 sub AnyEvent::Impl::Tk::io::DESTROY {
80 my ($fh, $tk) = @{$_[0]};
81  
82 # work around another bug: watchers don't get removed when
83 # the fh is closed, contrary to documentation. also, trying
84 # to unregister a read callback will make it impossible
85 # to remove the write callback.
86 # if your program segfaults here then you need to destroy
87 # your watchers before program exit. sorry, no way around
88 # that.
89 $mw->fileevent ($fh, $tk => "");
90 }
91  
92 sub timer {
93 my (undef, %arg) = @_;
94
95 my $after = $arg{after} < 0 ? 0 : $arg{after} * 1000;
96 my $cb = $arg{cb};
97 my $id;
98  
99 if ($arg{interval}) {
100 my $ival = $arg{interval} * 1000;
101 my $rcb = sub {
102 $id = Tk::after $mw, $ival, [$_[0], $_[0]];
103 &$cb;
104 };
105 $id = Tk::after $mw, $after, [$rcb, $rcb];
106 } else {
107 # tk blesses $cb, thus the extra indirection
108 $id = Tk::after $mw, $after, sub { &$cb };
109 }
110  
111 bless \\$id, "AnyEvent::Impl::Tk::after"
112 }
113  
114 sub idle {
115 my (undef, %arg) = @_;
116  
117 my $cb = $arg{cb};
118 my $id;
119 my $rcb = sub {
120 # in their endless stupidity, they decided to give repeating idle watchers
121 # strictly higher priority than timers :/
122 $id = Tk::after $mw, 0 => [sub {
123 $id = Tk::after $mw, idle => [$_[0], $_[0]];
124 }, $_[0]];
125 &$cb;
126 };
127  
128 $id = Tk::after $mw, idle => [$rcb, $rcb];
129 bless \\$id, "AnyEvent::Impl::Tk::after"
130 }
131  
132 sub AnyEvent::Impl::Tk::after::DESTROY {
133 Tk::after $mw, cancel => $${$_[0]};
134 }
135  
136 #sub loop {
137 # Tk::MainLoop;
138 #}
139  
140 sub _poll {
141 Tk::DoOneEvent (0);
142 }
143  
144 sub AnyEvent::CondVar::Base::_wait {
145 Tk::DoOneEvent (0) until exists $_[0]{_ae_sent};
146 }
147  
148 =head1 SEE ALSO
149  
150 L<AnyEvent>, L<Tk>.
151  
152 =head1 AUTHOR
153  
154 Marc Lehmann <schmorp@schmorp.de>
155 http://anyevent.schmorp.de
156  
157 =cut
158  
159 1
160