| 1 | package Games::Rezrov::ZIO_Termcap; | ||
| 2 | # | ||
| 3 | # z-machine I/O for perls with Term::Cap | ||
| 4 | # | ||
| 5 | |||
| 6 | BEGIN { | ||
| 7 | # $ENV{"PERL_RL"} = 'Gnu'; | ||
| 8 | # $ENV{"PERL_RL"} = 'Perl'; | ||
| 9 | # don't set unless debugging: if requested readline isn't | ||
| 10 | # present, may default to useless "Stub" version | ||
| 11 | } | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use Term::Cap; | ||
| 15 | use POSIX; | ||
| 16 | |||
| 17 | use Games::Rezrov::GetKey; | ||
| 18 | use Games::Rezrov::GetSize; | ||
| 19 | use Games::Rezrov::ZConst; | ||
| 20 | use Games::Rezrov::ZIO_Tools; | ||
| 21 | use Games::Rezrov::ZIO_Generic; | ||
| 22 | |||
| 23 | @Games::Rezrov::ZIO_Termcap::ISA = qw(Games::Rezrov::ZIO_Generic); | ||
| 24 | |||
| 25 | # | ||
| 26 | # termcap(5) capabilities: | ||
| 27 | # | ||
| 28 | use constant ATTR_OFF => 'me'; | ||
| 29 | use constant ATTR_REVERSE => 'mr'; | ||
| 30 | use constant ATTR_BOLD => 'md'; | ||
| 31 | use constant ATTR_UNDERLINE => 'us'; | ||
| 32 | |||
| 33 | use constant CURSOR_MOVE => 'cm'; | ||
| 34 | use constant CURSOR_UP => 'up'; | ||
| 35 | |||
| 36 | use constant CLEAR_TO_EOL => 'ce'; | ||
| 37 | use constant CLEAR_SCREEN => 'cl'; | ||
| 38 | use constant DELETE_LINE => 'dl'; | ||
| 39 | use constant DELETE_CHAR => 'dc'; | ||
| 40 | |||
| 41 | use constant AUDIO_BELL => 'bl'; | ||
| 42 | use constant VISIBLE_BELL => 'vb'; | ||
| 43 | |||
| 44 | use constant SET_SCROLL_REGION => 'cs'; | ||
| 45 | use constant SCROLL_ONE_LINE => 'sf'; | ||
| 46 | |||
| 47 | use constant BACKSPACE_BOOLEAN => 'bs'; | ||
| 48 | use constant BACKSPACE_ALTERNATE => 'bc'; | ||
| 49 | # | ||
| 50 | # end termcap | ||
| 51 | # | ||
| 52 | |||
| 53 | use constant MANUAL_BACKSPACE => 0x08; | ||
| 54 | # ^H | ||
| 55 | |||
| 56 | use constant SCROLL_STYLE_CS => 1; | ||
| 57 | use constant SCROLL_STYLE_DEL => 2; | ||
| 58 | |||
| 59 | use Games::Rezrov::MethodMaker qw( | ||
| 60 | scrolling_style | ||
| 61 | backspace_sequence | ||
| 62 | read_own_lines | ||
| 63 | ); | ||
| 64 | |||
| 65 | #my $HAVE_STATUS_LINE; | ||
| 66 | |||
| 67 | # again, a lot of statics for speed... | ||
| 68 | my $UPPER_LINES; | ||
| 69 | my $terminal; | ||
| 70 | |||
| 71 | my ($abs_x, $abs_y) = (0,0); | ||
| 72 | # current cursor position | ||
| 73 | |||
| 74 | # in v4/v5, we have a "lower" and an "upper" window. | ||
| 75 | # in <= v3, we have a window and a status line; status line will be | ||
| 76 | # considered "upper". BROKEN: seastalker! | ||
| 77 | |||
| 78 | my ($rows, $columns); | ||
| 79 | |||
| 80 | sub new { | ||
| 81 | my ($type, %options) = @_; | ||
| 82 | my $self = new Games::Rezrov::ZIO_Generic(); | ||
| 83 | bless $self, $type; | ||
| 84 | |||
| 85 | $self->zio_options(\%options); | ||
| 86 | $self->readline_init(); | ||
| 87 | |||
| 88 | # set up Term::Cap | ||
| 89 | my $termios = new POSIX::Termios(); | ||
| 90 | $termios->getattr(); | ||
| 91 | my $ospeed = $termios->getospeed(); | ||
| 92 | $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed }; | ||
| 93 | |||
| 94 | $terminal->Trequire( | ||
| 95 | # CURSOR_X, | ||
| 96 | # CURSOR_LOWER_LEFT, | ||
| 97 | CLEAR_TO_EOL, | ||
| 98 | ); | ||
| 99 | |||
| 100 | $self->init_scrolling_style(); | ||
| 101 | |||
| 102 | # die join "---", $terminal->Tputs("cs"), $terminal->Tgoto("cs", 42, 1); | ||
| 103 | |||
| 104 | |||
| 105 | attr_off(); | ||
| 106 | |||
| 107 | if (0) { | ||
| 108 | # boolean testing code | ||
| 109 | my @booleans = ("5i","am","bs","bw","da","db","eo","es","gn","hc","HC","hs","hz","in","km","mi","ms","NP","NR","nx","os","ul","xb","xn","xo","xs","xt"); | ||
| 110 | foreach my $b (@booleans) { | ||
| 111 | my $v = $terminal->Tputs($b, 0); | ||
| 112 | printf "%s: '%s'\n", $b, defined $v ? "defined" : "undef"; | ||
| 113 | } | ||
| 114 | die; | ||
| 115 | } | ||
| 116 | |||
| 117 | # | ||
| 118 | # find backspace sequence | ||
| 119 | # | ||
| 120 | my $backspace; | ||
| 121 | if (defined $terminal->Tputs(BACKSPACE_BOOLEAN, 0)) { | ||
| 122 | # boolean -- only defined, not an actual value :/ | ||
| 123 | # if set, ctrl-H performs backspace | ||
| 124 | $backspace = pack "c", MANUAL_BACKSPACE; | ||
| 125 | } else { | ||
| 126 | my $alt = $terminal->Tputs(BACKSPACE_ALTERNATE, 0); | ||
| 127 | # "Backspace, if not ^H" | ||
| 128 | if ($alt) { | ||
| 129 | $backspace = $alt; | ||
| 130 | } else { | ||
| 131 | print STDERR "WARNING: can't get backspace sequence!\n"; | ||
| 132 | $backspace = pack "c", MANUAL_BACKSPACE; | ||
| 133 | # shouldn't happen | ||
| 134 | } | ||
| 135 | } | ||
| 136 | $self->backspace_sequence($backspace); | ||
| 137 | |||
| 138 | if ($options{"columns"} and $options{"rows"}) { | ||
| 139 | $columns = $options{"columns"}; | ||
| 140 | $rows = $options{"rows"}; | ||
| 141 | } else { | ||
| 142 | ($columns, $rows) = get_size(); | ||
| 143 | if ($columns and $rows) { | ||
| 144 | if ($options{"flaky"} and Games::Rezrov::GetKey::can_read_single()) { | ||
| 145 | $self->read_own_lines(1); | ||
| 146 | } else { | ||
| 147 | $rows-- if $self->scrolling_style() == SCROLL_STYLE_DEL; | ||
| 148 | # hack: steal the last line on the display. | ||
| 149 | # a newline on the last line causes the window to scroll | ||
| 150 | # automatically, adding an unrequested newline and erasing | ||
| 151 | # the status line. Fixable without "cs"? | ||
| 152 | } | ||
| 153 | } else { | ||
| 154 | print "I couldn't guess the number of rows and columns in your display,\n"; | ||
| 155 | print "so you must use -rows and -columns to specify them manually.\n"; | ||
| 156 | exit; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | $self->set_upper_lines(0); | ||
| 161 | |||
| 162 | return $self; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub update { | ||
| 166 | # force screen refresh | ||
| 167 | $|=1; | ||
| 168 | print ""; | ||
| 169 | $|=0; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub set_version { | ||
| 173 | # called by the game | ||
| 174 | my ($self, $status_needed, $callback) = @_; | ||
| 175 | # $HAVE_STATUS_LINE = $status_needed; | ||
| 176 | $self->set_upper_lines(); | ||
| 177 | # hack | ||
| 178 | Games::Rezrov::StoryFile::rows($rows); | ||
| 179 | Games::Rezrov::StoryFile::columns($columns); | ||
| 180 | return 0; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub write_string { | ||
| 184 | my ($self, $string, $x, $y) = @_; | ||
| 185 | $self->absolute_move($x, $y) if defined($x) and defined($y); | ||
| 186 | # printf STDERR "ws: %s at (%d,%d)\n", $string, $abs_x, $abs_y; | ||
| 187 | print $string; | ||
| 188 | $abs_x += length($string); | ||
| 189 | } | ||
| 190 | |||
| 191 | sub newline { | ||
| 192 | if (++$abs_y >= $rows) { | ||
| 193 | # scroll needed | ||
| 194 | if ($_[0]->scrolling_style() == SCROLL_STYLE_CS) { | ||
| 195 | $_[0]->absolute_move(0, $rows); | ||
| 196 | # saw a ref somewhere that said cursor had to be in | ||
| 197 | # the lower left corner for scroll cmd to work | ||
| 198 | # print $terminal->Tputs(SCROLL_ONE_LINE); | ||
| 199 | do_term(SCROLL_ONE_LINE); | ||
| 200 | $abs_y = $rows; | ||
| 201 | } else { | ||
| 202 | # print STDERR "scrolling!\n"; | ||
| 203 | my $restore = $_[0]->get_position(1); | ||
| 204 | $_[0]->absolute_move(0, $UPPER_LINES); | ||
| 205 | do_term(DELETE_LINE); | ||
| 206 | # scroll the lower window by deleting the first line | ||
| 207 | # FIX ME: ONLY IN LOWER WINDOW | ||
| 208 | # &$restore(); | ||
| 209 | $abs_y = $rows - 1; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | $_[0]->absolute_move(0, $abs_y); | ||
| 213 | |||
| 214 | Games::Rezrov::StoryFile::register_newline(); | ||
| 215 | } | ||
| 216 | |||
| 217 | sub write_zchar { | ||
| 218 | print chr($_[1]); | ||
| 219 | $abs_x++; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub absolute_move { | ||
| 223 | # col, row | ||
| 224 | my $self = shift; | ||
| 225 | ($abs_x, $abs_y) = @_; | ||
| 226 | print $terminal->Tgoto(CURSOR_MOVE, $abs_x, $abs_y); | ||
| 227 | } | ||
| 228 | |||
| 229 | sub get_position { | ||
| 230 | # with no arguments, return absolute X and Y coordinates. | ||
| 231 | # With an argument, return a sub that will restore the current cursor | ||
| 232 | # position. | ||
| 233 | my ($self, $sub) = @_; | ||
| 234 | my ($x, $y) = ($abs_x, $abs_y); | ||
| 235 | if ($sub) { | ||
| 236 | return sub { $self->absolute_move($x, $y); }; | ||
| 237 | } else { | ||
| 238 | return ($abs_x, $abs_y); | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | sub status_hook { | ||
| 243 | my ($self, $type) = @_; | ||
| 244 | # 0 = before | ||
| 245 | # 1 = after | ||
| 246 | if ($type == 0) { | ||
| 247 | # before printing status line | ||
| 248 | attr_reverse(); | ||
| 249 | } else { | ||
| 250 | # after printing status line | ||
| 251 | attr_off(); | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | sub get_input { | ||
| 256 | my ($self, $max, $single_char, %options) = @_; | ||
| 257 | my $result; | ||
| 258 | if ($single_char) { | ||
| 259 | $result = get_key(); | ||
| 260 | } else { | ||
| 261 | if ($self->read_own_lines()) { | ||
| 262 | # manual | ||
| 263 | $result = $self->manual_read_line($options{"-preloaded"}); | ||
| 264 | } elsif ($self->using_term_readline()) { | ||
| 265 | # Term::ReadLine enabled | ||
| 266 | $result = $self->readline($options{"-preloaded"}); | ||
| 267 | } else { | ||
| 268 | # system "stty -echo"; | ||
| 269 | $result = <STDIN>; | ||
| 270 | # this doesn't work with v5+ preloaded input | ||
| 271 | unless (defined $result) { | ||
| 272 | $result = ""; | ||
| 273 | print "\n"; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | chomp $result; | ||
| 277 | $result = "" unless defined($result); | ||
| 278 | |||
| 279 | $self->newline() if $self->scrolling_style() == SCROLL_STYLE_DEL; | ||
| 280 | # ugh, FIX ME | ||
| 281 | |||
| 282 | } | ||
| 283 | return $result; | ||
| 284 | } | ||
| 285 | |||
| 286 | sub clear_to_eol { | ||
| 287 | do_term(CLEAR_TO_EOL); | ||
| 288 | } | ||
| 289 | |||
| 290 | sub clear_screen { | ||
| 291 | do_term(CLEAR_SCREEN); | ||
| 292 | } | ||
| 293 | |||
| 294 | sub split_window { | ||
| 295 | # split upper window to specified number of lines | ||
| 296 | my ($self, $lines) = @_; | ||
| 297 | $self->set_upper_lines($lines); | ||
| 298 | # needed for scrolling the lower window | ||
| 299 | } | ||
| 300 | |||
| 301 | sub do_term { | ||
| 302 | print $terminal->Tputs($_[0], 0) if $terminal; | ||
| 303 | } | ||
| 304 | |||
| 305 | sub attr_off { | ||
| 306 | do_term(ATTR_OFF); | ||
| 307 | } | ||
| 308 | |||
| 309 | sub attr_reverse { | ||
| 310 | do_term(ATTR_REVERSE); | ||
| 311 | } | ||
| 312 | |||
| 313 | sub attr_bold { | ||
| 314 | do_term(ATTR_BOLD); | ||
| 315 | } | ||
| 316 | |||
| 317 | sub attr_underline { | ||
| 318 | do_term(ATTR_UNDERLINE); | ||
| 319 | } | ||
| 320 | |||
| 321 | sub set_text_style { | ||
| 322 | # sect15.html#set_text_style | ||
| 323 | my ($self, $text_style) = @_; | ||
| 324 | attr_off(); | ||
| 325 | attr_reverse() if ($text_style & Games::Rezrov::ZConst::STYLE_REVERSE); | ||
| 326 | attr_bold() if ($text_style & Games::Rezrov::ZConst::STYLE_BOLD); | ||
| 327 | attr_underline() if ($text_style & Games::Rezrov::ZConst::STYLE_ITALIC); | ||
| 328 | } | ||
| 329 | |||
| 330 | sub cleanup { | ||
| 331 | # don't just rely on DESTROY, doesn't work for interrupts | ||
| 332 | attr_off(); | ||
| 333 | $_[0]->set_upper_lines(0); | ||
| 334 | # reset scrolling if necessary | ||
| 335 | # $_[0]->clear_screen(); | ||
| 336 | $_[0]->absolute_move(0, $rows) if $terminal; | ||
| 337 | # move to last line | ||
| 338 | } | ||
| 339 | |||
| 340 | sub story { | ||
| 341 | return (defined $_[1] ? $_[0]->{"story"} = $_[1] : $_[0]->{"story"}); | ||
| 342 | } | ||
| 343 | |||
| 344 | sub manual_read_line { | ||
| 345 | # read a line manually | ||
| 346 | # incompatible with ReadLine, but compatible with preloaded input | ||
| 347 | my ($self, $buf) = @_; | ||
| 348 | $buf = "" unless defined $buf; | ||
| 349 | my ($ord, $char); | ||
| 350 | while (1) { | ||
| 351 | $char = get_key(); | ||
| 352 | $ord = ord($char); | ||
| 353 | if ($ord == Games::Rezrov::ZConst::ASCII_DEL or | ||
| 354 | $ord == Games::Rezrov::ZConst::ASCII_BS) { | ||
| 355 | if (my $len = length($buf)) { | ||
| 356 | print $self->backspace_sequence(); | ||
| 357 | do_term(DELETE_CHAR); | ||
| 358 | $buf = substr($buf, 0, $len - 1); | ||
| 359 | } | ||
| 360 | } elsif ($ord == Games::Rezrov::ZConst::ASCII_LF or | ||
| 361 | $ord == Games::Rezrov::ZConst::ASCII_CR) { | ||
| 362 | return $buf; | ||
| 363 | } elsif ($ord >= 32 and $ord <=127) { | ||
| 364 | $buf .= $char; | ||
| 365 | print $char; | ||
| 366 | } | ||
| 367 | } | ||
| 368 | } | ||
| 369 | |||
| 370 | sub set_upper_lines { | ||
| 371 | my ($self, $ul) = @_; | ||
| 372 | $UPPER_LINES = $ul if defined $ul; | ||
| 373 | if ($self->scrolling_style() == SCROLL_STYLE_CS) { | ||
| 374 | # set the scroll region (protect upper window) | ||
| 375 | |||
| 376 | # my $reserve = $UPPER_LINES + ($HAVE_STATUS_LINE ? 1 : 0); | ||
| 377 | my $reserve = $UPPER_LINES; | ||
| 378 | # I forget why we don't need to include the status line... | ||
| 379 | # ...or is this an ominous sign? :/ | ||
| 380 | |||
| 381 | print $terminal->Tgoto(SET_SCROLL_REGION, $rows, $reserve) if $terminal; | ||
| 382 | # HACK | ||
| 383 | # this "kind of" works, but args are reversed (1st=col 2nd=row) | ||
| 384 | # might this break on some systems? | ||
| 385 | # Seems to work under Linux but I don't think Tgoto is meant | ||
| 386 | # to be used this way. The "correct" way? | ||
| 387 | } | ||
| 388 | } | ||
| 389 | |||
| 390 | sub test_terminal_capabilities { | ||
| 391 | # return 1 if given list of capabilities available | ||
| 392 | eval { | ||
| 393 | local $SIG{__WARN__} = sub {}; | ||
| 394 | $terminal->Trequire(@_); | ||
| 395 | }; | ||
| 396 | return $@ ? 0 : 1; | ||
| 397 | } | ||
| 398 | |||
| 399 | sub init_scrolling_style { | ||
| 400 | # if terminal has "cs" attribute we can use all lines on the screen. | ||
| 401 | # the "del" style is still buggy. | ||
| 402 | |||
| 403 | $_[0]->scrolling_style(test_terminal_capabilities(SET_SCROLL_REGION) ? | ||
| 404 | SCROLL_STYLE_CS : SCROLL_STYLE_DEL); | ||
| 405 | # $_[0]->scrolling_style(SCROLL_STYLE_DEL); | ||
| 406 | } | ||
| 407 | |||
| 408 | 1; |