| 1 | package Games::Rezrov::ZIO_Curses; | ||
| 2 | # | ||
| 3 | # z-machine i/o for perls with the Curses module installed. | ||
| 4 | # | ||
| 5 | use strict; | ||
| 6 | use Carp qw(cluck confess); | ||
| 7 | |||
| 8 | BEGIN { | ||
| 9 | # $ENV{"PERL_RL"} = 'Perl'; | ||
| 10 | } | ||
| 11 | |||
| 12 | @Games::Rezrov::ZIO_Curses::ISA = qw( | ||
| 13 | Games::Rezrov::ZIO_Generic | ||
| 14 | Games::Rezrov::ZIO_Color | ||
| 15 | ); | ||
| 16 | |||
| 17 | use Curses; | ||
| 18 | |||
| 19 | use Games::Rezrov::ZOptions; | ||
| 20 | use Games::Rezrov::ZIO_Generic; | ||
| 21 | use Games::Rezrov::ZIO_Color; | ||
| 22 | use Games::Rezrov::ZConst; | ||
| 23 | use Games::Rezrov::ZIO_Tools; | ||
| 24 | use Games::Rezrov::MethodMaker qw( | ||
| 25 | need_endwin | ||
| 26 | rows | ||
| 27 | columns | ||
| 28 | color_pairs | ||
| 29 | zfont | ||
| 30 | ); | ||
| 31 | |||
| 32 | my $w_main; | ||
| 33 | my %COLORMAP = (black => COLOR_BLACK, | ||
| 34 | red => COLOR_RED, | ||
| 35 | green => COLOR_GREEN, | ||
| 36 | yellow => COLOR_YELLOW, | ||
| 37 | blue => COLOR_BLUE, | ||
| 38 | magenta => COLOR_MAGENTA, | ||
| 39 | cyan => COLOR_CYAN, | ||
| 40 | white => COLOR_WHITE); | ||
| 41 | my $color_pair_counter = 0; | ||
| 42 | # HACKS | ||
| 43 | |||
| 44 | sub new { | ||
| 45 | my ($type, %options) = @_; | ||
| 46 | my $self = new Games::Rezrov::ZIO_Generic(%options); | ||
| 47 | # my $self = []; | ||
| 48 | bless $self, $type; | ||
| 49 | |||
| 50 | $self->zio_options(\%options); | ||
| 51 | $self->readline_init(); | ||
| 52 | # danger! danger! | ||
| 53 | |||
| 54 | # set up Curses: | ||
| 55 | initscr() || die; | ||
| 56 | $self->need_endwin(1); | ||
| 57 | # printf STDERR "ew: %s %s\n", $self, $self->need_endwin(); | ||
| 58 | # isendwin() is not present in all Curses implementations (eg dec_osf) | ||
| 59 | # ...actually it's more like Curses.pm doesn't autoconfigure correctly | ||
| 60 | # under dec_osf :( | ||
| 61 | |||
| 62 | my $columns = $options{"columns"} || $Curses::COLS || die "need columns!"; | ||
| 63 | my $rows = $options{"rows"} || $Curses::LINES || die "need rows!"; | ||
| 64 | |||
| 65 | $self->rows($rows); | ||
| 66 | $self->columns($columns); | ||
| 67 | |||
| 68 | $w_main = newwin($rows, $columns, 0, 0); | ||
| 69 | # create main window | ||
| 70 | $w_main->keypad(1); | ||
| 71 | |||
| 72 | $self->color_pairs({}); | ||
| 73 | $self->parse_color_options(\%options); | ||
| 74 | $self->init_colors(\%options); | ||
| 75 | |||
| 76 | $self->zfont(Games::Rezrov::ZConst::FONT_NORMAL); | ||
| 77 | |||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub groks_font_3 { | ||
| 82 | return 1; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub can_use_color { | ||
| 86 | return has_colors() ? 1 : 0; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub update { | ||
| 90 | # force screen refresh | ||
| 91 | $w_main->refresh(); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub set_version { | ||
| 95 | # called by the game | ||
| 96 | my ($self, $status_needed, $callback) = @_; | ||
| 97 | Games::Rezrov::StoryFile::rows($self->rows); | ||
| 98 | Games::Rezrov::StoryFile::columns($self->columns); | ||
| 99 | $self->clear_screen(); | ||
| 100 | scrollok($w_main, 1); | ||
| 101 | noecho(); | ||
| 102 | return 0; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub set_background_color { | ||
| 106 | if (has_colors() and | ||
| 107 | $_[0]->fg() and $_[0]->bg()) { | ||
| 108 | $_[0]->do_colors($_[0]->fg(), $_[0]->bg()); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | sub clear_screen { | ||
| 113 | $w_main->erase(); | ||
| 114 | # erase | ||
| 115 | } | ||
| 116 | |||
| 117 | sub get_position { | ||
| 118 | # with no arguments, return absolute X and Y coordinates. | ||
| 119 | # With an argument, return a sub that will restore the current cursor | ||
| 120 | # position. | ||
| 121 | my ($self, $sub) = @_; | ||
| 122 | my ($x, $y); | ||
| 123 | $w_main->getyx($y,$x); | ||
| 124 | # carp "pos is $x, $y\n"; | ||
| 125 | if ($sub) { | ||
| 126 | return sub { $w_main->move($y, $x); }; | ||
| 127 | } else { | ||
| 128 | return ($x, $y); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | sub newline { | ||
| 133 | # use autoscrolling | ||
| 134 | $w_main->addstr("\n"); | ||
| 135 | # broken: what if not lower window?? | ||
| 136 | Games::Rezrov::StoryFile::register_newline(); | ||
| 137 | update() if Games::Rezrov::ZOptions::MAXIMUM_SCROLLING; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub write_zchar { | ||
| 141 | # write an unbuffered z-char to the screen | ||
| 142 | if ($_[0]->zfont() == 3) { | ||
| 143 | if ($_[1] == 32 || $_[1] == 37) { | ||
| 144 | $w_main->addch(" "); | ||
| 145 | } elsif ($_[1] == 35 || $_[1] == 50 || $_[1] == 52 || $_[1] == 67 || $_[1] == 69) { | ||
| 146 | $w_main->addch("/"); | ||
| 147 | } elsif ($_[1] == 36 || $_[1] == 51 || $_[1] == 53 || $_[1] == 68 || $_[1] == 70) { | ||
| 148 | $w_main->addch('\\'); | ||
| 149 | } elsif ($_[1] == 46) { | ||
| 150 | $w_main->addch(ACS_LLCORNER); | ||
| 151 | } elsif ($_[1] == 47) { | ||
| 152 | $w_main->addch(ACS_ULCORNER); | ||
| 153 | } elsif ($_[1] == 48) { | ||
| 154 | $w_main->addch(ACS_URCORNER); | ||
| 155 | } elsif ($_[1] == 49) { | ||
| 156 | $w_main->addch(ACS_LRCORNER); | ||
| 157 | } elsif ($_[1] == 42) { | ||
| 158 | $w_main->addch(ACS_BTEE); | ||
| 159 | } elsif ($_[1] == 43) { | ||
| 160 | $w_main->addch(ACS_TTEE); | ||
| 161 | } elsif ($_[1] == 44) { | ||
| 162 | $w_main->addch(ACS_LTEE); | ||
| 163 | } elsif ($_[1] == 45) { | ||
| 164 | $w_main->addch(ACS_RTEE); | ||
| 165 | } elsif ($_[1] == 54) { | ||
| 166 | $w_main->addch(ACS_BLOCK); | ||
| 167 | } elsif ($_[1] == 38 || $_[1] == 39 || $_[1] == 61 || $_[1] == 62) { | ||
| 168 | $w_main->addch(ACS_HLINE); | ||
| 169 | } elsif ($_[1] == 40 || $_[1] == 41 || $_[1] == 59 || $_[1] == 60) { | ||
| 170 | $w_main->addch(ACS_VLINE); | ||
| 171 | } elsif ($_[1] == 90) { | ||
| 172 | $w_main->addch("X"); | ||
| 173 | } elsif ($_[1] == 96) { | ||
| 174 | $w_main->addch("?"); | ||
| 175 | } else { | ||
| 176 | $w_main->addch(ACS_BULLET); | ||
| 177 | } | ||
| 178 | } else { | ||
| 179 | $w_main->addch(chr($_[1])); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | sub status_hook { | ||
| 184 | my ($self, $type) = @_; | ||
| 185 | # 0 = before | ||
| 186 | # 1 = after | ||
| 187 | if ($type == 0) { | ||
| 188 | # before printing status line | ||
| 189 | if (has_colors()) { | ||
| 190 | $w_main->bkgd(0); | ||
| 191 | # $w_main->attrset(COLOR_PAIR(2)); | ||
| 192 | $self->do_colors($self->sfg(), $self->sbg()); | ||
| 193 | } else { | ||
| 194 | $w_main->attrset(A_REVERSE); | ||
| 195 | } | ||
| 196 | } else { | ||
| 197 | # after printing status line | ||
| 198 | if (has_colors()) { | ||
| 199 | $self->do_colors($self->fg(), $self->bg()); | ||
| 200 | } else { | ||
| 201 | $w_main->attrset(A_NORMAL); | ||
| 202 | } | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | sub write_string { | ||
| 207 | my ($self, $string, $x, $y) = @_; | ||
| 208 | $self->absolute_move($x, $y) if defined($x) and defined($y); | ||
| 209 | # printf STDERR "write string: ->%s<-\n", $string; | ||
| 210 | $w_main->addstr($string); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub get_input { | ||
| 214 | my ($self, $max, $single_char, %options) = @_; | ||
| 215 | my $result; | ||
| 216 | # printf STDERR "geom: %s %s\n", $Curses::COLS, $Curses::LINES; | ||
| 217 | # hmm, how to detect terminal geometry changes? | ||
| 218 | echo(); | ||
| 219 | if ($single_char) { | ||
| 220 | $result = get_char(); | ||
| 221 | # printf STDERR "get_char: %d\n", ord($result); | ||
| 222 | } else { | ||
| 223 | my ($x, $y) = $self->get_position(); | ||
| 224 | scrollok($w_main, 0); | ||
| 225 | # temporarily disable autoscrolling; some Curses seem to generate | ||
| 226 | # a newline/scroll with user input (ie DEC OSF) | ||
| 227 | |||
| 228 | if ($self->using_term_readline()) { | ||
| 229 | $result = $self->readline($options{"-preloaded"}); | ||
| 230 | # doesn't work, period! | ||
| 231 | } else { | ||
| 232 | # $w_main->getnstr($result, $max); | ||
| 233 | # this doesn't work with v5+ preloaded input | ||
| 234 | |||
| 235 | $result = ""; | ||
| 236 | if ($options{"-preloaded"}) { | ||
| 237 | $result = $options{"-preloaded"}; | ||
| 238 | $x -= length $result; | ||
| 239 | } | ||
| 240 | my ($char, $ord); | ||
| 241 | noecho(); | ||
| 242 | cbreak(); | ||
| 243 | while (1) { | ||
| 244 | $char = $w_main->getch(); | ||
| 245 | $ord = ord($char); | ||
| 246 | if ($char eq KEY_BACKSPACE or | ||
| 247 | $ord == Games::Rezrov::ZConst::ASCII_DEL or | ||
| 248 | $ord == Games::Rezrov::ZConst::ASCII_BS | ||
| 249 | ) { | ||
| 250 | my $len = length $result; | ||
| 251 | if ($len) { | ||
| 252 | $result = substr($result, 0, --$len); | ||
| 253 | $w_main->move($y, $x + $len); | ||
| 254 | $w_main->addch(" "); | ||
| 255 | $w_main->move($y, $x + $len); | ||
| 256 | } | ||
| 257 | } elsif ($ord == Games::Rezrov::ZConst::ASCII_LF or | ||
| 258 | $ord == Games::Rezrov::ZConst::ASCII_CR) { | ||
| 259 | # newline: hack | ||
| 260 | last; | ||
| 261 | } elsif (length $result < $max) { | ||
| 262 | $result .= $char; | ||
| 263 | $w_main->addch($char); | ||
| 264 | # $w_main->refresh(); | ||
| 265 | $w_main->touchline($y, 1); | ||
| 266 | # may be needed to update when user types a space char | ||
| 267 | # print STDERR "adding \"$char\"\n"; | ||
| 268 | } | ||
| 269 | } | ||
| 270 | nocbreak(); | ||
| 271 | } | ||
| 272 | scrollok($w_main, 1); | ||
| 273 | $w_main->move($y, $x + length($result)); | ||
| 274 | # make sure the cursor is now after the input | ||
| 275 | $self->newline(); | ||
| 276 | } | ||
| 277 | noecho(); | ||
| 278 | return $result; | ||
| 279 | } | ||
| 280 | |||
| 281 | sub get_char { | ||
| 282 | # read a single character (if possible) | ||
| 283 | my $char; | ||
| 284 | noecho(); | ||
| 285 | # raw(); | ||
| 286 | cbreak(); | ||
| 287 | $char = $w_main->getch(); | ||
| 288 | # noraw(); | ||
| 289 | nocbreak(); | ||
| 290 | echo(); | ||
| 291 | if ($char =~ /^\d+$/) { | ||
| 292 | # a numeric code was returned; see if we can translate it into a | ||
| 293 | # z-code meta key | ||
| 294 | if ($char == KEY_UP) { | ||
| 295 | $char = chr(Games::Rezrov::ZConst::Z_UP); | ||
| 296 | } elsif ($char == KEY_DOWN) { | ||
| 297 | $char = chr(Games::Rezrov::ZConst::Z_DOWN); | ||
| 298 | } elsif ($char == KEY_LEFT) { | ||
| 299 | $char = chr(Games::Rezrov::ZConst::Z_LEFT); | ||
| 300 | } elsif ($char == KEY_RIGHT) { | ||
| 301 | $char = chr(Games::Rezrov::ZConst::Z_RIGHT); | ||
| 302 | } | ||
| 303 | } | ||
| 304 | return $char; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub clear_to_eol { | ||
| 308 | $w_main->clrtoeol(); | ||
| 309 | } | ||
| 310 | |||
| 311 | sub split_window { | ||
| 312 | # split upper window to specified number of lines | ||
| 313 | my ($self, $lines) = @_; | ||
| 314 | $w_main->setscrreg($lines, $self->rows() - 1); | ||
| 315 | } | ||
| 316 | |||
| 317 | sub set_text_style { | ||
| 318 | # sect15.html#set_text_style | ||
| 319 | my ($self, $text_style) = @_; | ||
| 320 | if ($text_style == Games::Rezrov::ZConst::STYLE_ROMAN) { | ||
| 321 | # turn off all | ||
| 322 | $w_main->attrset(A_NORMAL); | ||
| 323 | } else { | ||
| 324 | my $mask = 0; | ||
| 325 | $mask |= A_REVERSE if ($text_style & Games::Rezrov::ZConst::STYLE_REVERSE); | ||
| 326 | $mask |= A_BOLD if ($text_style & Games::Rezrov::ZConst::STYLE_BOLD); | ||
| 327 | $mask |= A_UNDERLINE if ($text_style & Games::Rezrov::ZConst::STYLE_ITALIC); | ||
| 328 | $w_main->attrset($mask); | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | sub cleanup { | ||
| 333 | # don't just rely on DESTROY, doesn't work for interrupts | ||
| 334 | if ($_[0]->need_endwin()) { | ||
| 335 | endwin(); | ||
| 336 | $_[0]->need_endwin(0); | ||
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 340 | sub absolute_move { | ||
| 341 | # move to X, Y | ||
| 342 | # carp "move to x=$_[1] y=$_[2]\n"; | ||
| 343 | $w_main->move($_[2], $_[1]); | ||
| 344 | } | ||
| 345 | |||
| 346 | sub init_colors { | ||
| 347 | # initialize color support | ||
| 348 | my ($self, $options) = @_; | ||
| 349 | |||
| 350 | if (has_colors()) { | ||
| 351 | foreach (map {$self->$_()} qw (fg bg sfg sbg)) { | ||
| 352 | # validate colors | ||
| 353 | if ($_ and !exists $COLORMAP{$_}) { | ||
| 354 | $self->fatal_error(sprintf "Unknown color \"%s\"; available colors are %s.\n", $_, join ", ", sort keys %COLORMAP); | ||
| 355 | } | ||
| 356 | } | ||
| 357 | start_color(); | ||
| 358 | $self->do_colors($self->fg(), $self->bg()); | ||
| 359 | } elsif ($options->{fg} or | ||
| 360 | $options->{bg} or | ||
| 361 | $options->{sfg} or | ||
| 362 | $options->{sbg}) { | ||
| 363 | # we specified colors to use, but terminal can't handle color | ||
| 364 | my $message = "You specified colors, but your terminal does not seem to\nsupport color. Is your TERM variable set correctly?\n"; | ||
| 365 | if ($^O =~ /linux/i) { | ||
| 366 | $message .= "You seem to be using Linux; if you are using color_xterm,\nhave you tried setting TERM to \"xterm-color\"?"; | ||
| 367 | } | ||
| 368 | $self->fatal_error($message); | ||
| 369 | } | ||
| 370 | } | ||
| 371 | |||
| 372 | sub color_change_notify { | ||
| 373 | $_[0]->do_colors($_[0]->fg(), $_[0]->bg()); | ||
| 374 | } | ||
| 375 | |||
| 376 | sub do_colors { | ||
| 377 | my ($self, $fg, $bg) = @_; | ||
| 378 | if (has_colors()) { | ||
| 379 | my $color_pairs = $self->color_pairs(); | ||
| 380 | my $key = $fg . "," . $bg; | ||
| 381 | my $pair; | ||
| 382 | if (exists $color_pairs->{$key}) { | ||
| 383 | # pair already exists, use it | ||
| 384 | $pair = $color_pairs->{$key}; | ||
| 385 | } else { | ||
| 386 | # if pair is not in cache, create it | ||
| 387 | $pair = ++$color_pair_counter; | ||
| 388 | init_pair($pair, $COLORMAP{$fg}, $COLORMAP{$bg}); | ||
| 389 | # print STDERR "create new $fg $bg = $pair\n"; | ||
| 390 | $color_pairs->{$key} = $pair; | ||
| 391 | } | ||
| 392 | # $w_main->bkgdset(COLOR_PAIR($pair)) if $set_background; | ||
| 393 | # printf STDERR "want %s/%s = %d\n", $fg, $bg, $pair; | ||
| 394 | $w_main->bkgdset(COLOR_PAIR($pair)); | ||
| 395 | } | ||
| 396 | } | ||
| 397 | |||
| 398 | sub set_font { | ||
| 399 | return $_[0]->zfont($_[1]); | ||
| 400 | } | ||
| 401 | |||
| 402 | |||
| 403 | 1; |