ZIO_dumb.pm untested

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 237 CC 24 SCAR 27.7
CC
24
Cov
0%
get_input
56.0
new
30.0
set_window
20.0
CRAP 16.0
1 package Games::Rezrov::ZIO_dumb;
2 # z-machine i/o for dumb/semi-dumb terminals.
3  
4 # to do: get rid of absolute Y (untrackable)
5  
6 use strict;
7 use Carp qw(cluck);
8  
9 use Games::Rezrov::GetKey;
10 use Games::Rezrov::GetSize;
11 use Games::Rezrov::ZIO_Tools;
12 use Games::Rezrov::ZIO_Generic;
13  
14 @Games::Rezrov::ZIO_dumb::ISA = qw(Games::Rezrov::ZIO_Generic);
15  
16 my $have_term_readkey;
17 my ($rows, $columns);
18 my ($clear_prog);
19  
20 my $abs_x = 0;
21 my $abs_y = 0;
22  
23 $|=1;
24  
25 sub new {
26 my ($type, %options) = @_;
27 my $self = new Games::Rezrov::ZIO_Generic(%options);
28 bless $self, $type;
29  
30 $self->zio_options(\%options);
31 $self->readline_init();
32  
33 $self->io_setup();
34
35 $columns = $options{columns} if $options{columns};
36 $rows = $options{rows} if $options{rows};
37 ($columns, $rows) = get_size() unless $columns and $rows;
38 # don't attempt to detect terminal size if manually set;
39 # ("make test" crashes in Term::ReadKey if not run on a tty!)
40
41 unless ($columns and $rows) {
42 print "I couldn't guess the number of rows and columns in your display,\n";
43 print "so you must use -rows and -columns to specify them manually.\n";
44 exit;
45 }
46 return $self;
47 }
48  
49 sub io_setup {
50 my ($self) = @_;
51  
52 if (find_module('Term::ReadKey')) {
53 require Term::ReadKey;
54 import Term::ReadKey;
55 $have_term_readkey = 1;
56 # ReadMode(3);
57 # disable echoing
58 # ReadLine(-1);
59 # make sure we don't buffer any (invisible) characters
60 }
61  
62 $clear_prog = find_prog("clear");
63 }
64  
65 sub write_string {
66 my ($self, $string, $x, $y) = @_;
67 $self->absolute_move($x, $y) if defined($x) and defined($y);
68 print $string;
69 # print STDERR "ws: $string\n";
70 $abs_x += length($string);
71 }
72  
73 sub clear_to_eol {
74 # print STDERR "clear to eol; at $abs_x\n";
75 my $diff = $columns - $abs_x;
76 if ($diff > 0) {
77 print " " x $diff;
78 # erase
79 print pack("c", 0x08) x $diff;
80 # restore cursor
81 }
82 }
83  
84 sub update {
85 }
86  
87 sub find_prog {
88 # don't look
89 foreach ("/bin/", "/usr/bin/") {
90 my $fn = $_ . $_[0];
91 return $fn if -x $fn;
92 }
93 return undef;
94 }
95  
96 sub can_split {
97 # true or false: can this zio split the screen?
98 return 0;
99 }
100  
101 sub set_version {
102 my ($self, $status_needed, $callback) = @_;
103 Games::Rezrov::StoryFile::rows($rows);
104 Games::Rezrov::StoryFile::columns($columns);
105 # print STDERR "$columns\n";
106 $self->clear_screen();
107 return 0;
108 }
109  
110 sub absolute_move {
111 my ($nx, $ny) = @_[1,2];
112 # printf STDERR "move X to $nx from $abs_x\n";
113 if (0 and $nx < $abs_x) {
114 # DISABLED
115 # "this sidewalk's for regular walkin', not fancy walkin'..."
116 my $diff = $abs_x - $nx;
117 # printf STDERR "going back %d\n", $abs_x - $nx;
118 print pack("c", 0x08) x $diff;
119 # go back
120 print " " x $diff;
121 # erase
122 print pack("c", 0x08) x $diff;
123 # go back again
124 }
125 $abs_x = $nx;
126 $abs_y = $ny;
127 }
128  
129 sub newline {
130 # check to see if we need to pause
131 print "\n";
132 # cluck "nl\n";
133 $abs_x = 0;
134 Games::Rezrov::StoryFile::register_newline();
135 }
136  
137 sub write_zchar {
138 if ($_[0]->current_window() == Games::Rezrov::ZConst::LOWER_WIN) {
139 print chr($_[1]);
140 # printf STDERR "wc: %s\n", chr($_[1]);
141 $abs_x++;
142 } else {
143 # printf STDERR "ignoring char: %s\n", chr($_[1]);
144 }
145 }
146  
147 sub get_input {
148 my ($self, $max, $single_char, %options) = @_;
149 if ($single_char) {
150 return get_key();
151 } else {
152 if ($have_term_readkey) {
153 # re-enable terminal before prompt
154 ReadMode(0);
155 # ReadLine(0);
156 }
157 my $line;
158 if ($self->listening) {
159 # speech recognition
160 $line = $self->recognize_line();
161 print "$line\n";
162 } elsif ($self->using_term_readline()) {
163 # Term::ReadLine enabled
164 $line = $self->readline($options{"-preloaded"});
165 } else {
166 $line = <STDIN>;
167 # also doesn't work with v5+ preloaded input
168 }
169 unless (defined $line) {
170 $line = "";
171 print "\n";
172 }
173 chomp $line;
174 if ($have_term_readkey) {
175 ReadMode(3);
176 # ReadLine(-1);
177 }
178 return $line;
179 }
180 }
181  
182 sub get_position {
183 my ($self, $sub) = @_;
184 if ($sub) {
185 return sub { };
186 } else {
187 return ($abs_x, $abs_y);
188 }
189 }
190  
191 sub clear_screen {
192 system($clear_prog) if $clear_prog;
193 # cluck "clear: $rows";
194 for (my $i=0; $i < $rows; $i++) {
195 # move cursor to lower left
196 print "\n";
197 }
198 }
199  
200 sub set_window {
201 my ($self, $window) = @_;
202 $self->SUPER::set_window($window);
203 if ($window != Games::Rezrov::ZConst::LOWER_WIN) {
204 # ignore output except on lower window
205 unless ($self->warned()) {
206 $self->warned(1);
207 my $pb = Games::Rezrov::StoryFile::prompt_buffer();
208 $self->newline();
209 Games::Rezrov::StoryFile::set_window(Games::Rezrov::ZConst::LOWER_WIN);
210 my $message = "WARNING: this game is attempting to use multiple windows, which this interface can't handle. The game may be unplayable using this interface. You should probably use the Tk, Curses, Termcap, or Win32 interfaces if you can; see the documentation.";
211 $self->SUPER::buffer_zchunk(\$message);
212 Games::Rezrov::StoryFile::flush();
213 $self->newline();
214 Games::Rezrov::StoryFile::prompt_buffer($pb) if $pb;
215 Games::Rezrov::StoryFile::set_window($window);
216 }
217 }
218 }
219  
220 sub erase_chars {
221 my $count = shift;
222  
223 print pack 'c', 0x0d; # carriage return
224 print ' ' x $count; # erase
225 print pack 'c', 0x0d; # carriage return
226 # 2nd pass required in case of user input on same line as more prompt;
227 # example: start "enchanter" in 80x36 terminal.
228 # I'm not sure why just sending $count 0x08's (backspace) doesn't
229 # work in this case, but it doesn't.
230 }
231  
232 sub cleanup {
233 if ($have_term_readkey) {
234 ReadMode(0);
235 # ReadLine(0);
236 }
237 }
238  
239 sub warned {
240 return (defined $_[1] ? $_[0]->{"warned"} = $_[1] : $_[0]->{"warned"});
241 }
242  
243  
244 1;