| 1 | package Games::Rezrov::GetKey; | ||
| 2 | # Try as hard as we can to read a single key from the keyboard. | ||
| 3 | # Use a "nice" approach if available, wallow if we must. | ||
| 4 | # Michael Edmonson 9/29/98 | ||
| 5 | # | ||
| 6 | # POSIX code taken from Tom Christiansen's "HotKey.pm", see | ||
| 7 | # perlfaq8, or <6k403m$r1l$9@csnews.cs.colorado.edu> | ||
| 8 | # | ||
| 9 | # TO DO: add DOS and other OS-specific code if Term::ReadKey not available | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use Exporter; | ||
| 13 | |||
| 14 | @Games::Rezrov::GetKey::ISA = qw(Exporter); | ||
| 15 | @Games::Rezrov::GetKey::EXPORT = qw(get_key); | ||
| 16 | |||
| 17 | use constant DEBUG => 0; | ||
| 18 | |||
| 19 | $Games::Rezrov::GetKey::STTY = ""; | ||
| 20 | |||
| 21 | my $CAN_READ_SINGLE = 1; | ||
| 22 | |||
| 23 | sub can_read_single { | ||
| 24 | return $CAN_READ_SINGLE; | ||
| 25 | } | ||
| 26 | |||
| 27 | eval 'use Term::ReadKey'; | ||
| 28 | if (!$@) { | ||
| 29 | # | ||
| 30 | # use Term::ReadKey | ||
| 31 | # | ||
| 32 | print STDERR "term::readkey\n" if DEBUG; | ||
| 33 | eval << 'DONE' | ||
| 34 | sub get_key { | ||
| 35 | ReadMode(3); | ||
| 36 | my $z; | ||
| 37 | read(STDIN, $z, 1); | ||
| 38 | ReadMode(0); | ||
| 39 | return $z; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub END { | ||
| 43 | ReadMode(0); | ||
| 44 | } | ||
| 45 | DONE | ||
| 46 | |||
| 47 | } else { | ||
| 48 | my $posix_ok = 0; | ||
| 49 | eval 'use POSIX qw(:termios_h)'; | ||
| 50 | if (!$@) { | ||
| 51 | eval 'my $term = POSIX::Termios->new();'; | ||
| 52 | if ($@) { | ||
| 53 | # we have the POSIX module but Termios doesn't work! | ||
| 54 | # die "aha"; | ||
| 55 | } else { | ||
| 56 | $posix_ok = 1; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | if ($posix_ok) { | ||
| 61 | # | ||
| 62 | # use POSIX termios | ||
| 63 | # | ||
| 64 | print STDERR "posix\n" if DEBUG; | ||
| 65 | |||
| 66 | eval << 'DONE' | ||
| 67 | |||
| 68 | my $fd_stdin = fileno(STDIN); | ||
| 69 | my $term = POSIX::Termios->new(); | ||
| 70 | $term->getattr($fd_stdin); | ||
| 71 | my $oterm = $term->getlflag(); | ||
| 72 | my $echo = ECHO | ECHOK | ICANON; | ||
| 73 | my $noecho = $oterm & ~$echo; | ||
| 74 | |||
| 75 | sub cbreak { | ||
| 76 | $term->setlflag($noecho); | ||
| 77 | $term->setcc(VTIME, 1); | ||
| 78 | $term->setattr($fd_stdin, TCSANOW); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub cooked { | ||
| 82 | $term->setlflag($oterm); | ||
| 83 | $term->setcc(VTIME, 0); | ||
| 84 | $term->setattr($fd_stdin, TCSANOW); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub get_key { | ||
| 88 | my $key = ''; | ||
| 89 | cbreak(); | ||
| 90 | sysread(STDIN, $key, 1); | ||
| 91 | cooked(); | ||
| 92 | return $key; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub END { | ||
| 96 | cooked(); | ||
| 97 | } | ||
| 98 | DONE | ||
| 99 | } else { | ||
| 100 | # | ||
| 101 | # Ugh, hopefully it won't come to this :) | ||
| 102 | # | ||
| 103 | my $prog; | ||
| 104 | foreach ("/bin/", "/usr/bin/") { | ||
| 105 | my $fn = $_ . "stty"; | ||
| 106 | $Games::Rezrov::GetKey::STTY = $fn, last if -x $fn; | ||
| 107 | } | ||
| 108 | |||
| 109 | if ($Games::Rezrov::GetKey::STTY) { | ||
| 110 | # use stty program | ||
| 111 | print STDERR "stty\n" if DEBUG; | ||
| 112 | |||
| 113 | eval << 'DONE' | ||
| 114 | sub get_key { | ||
| 115 | my $z; | ||
| 116 | system "$Games::Rezrov::GetKey::STTY -icanon -echo"; | ||
| 117 | read(STDIN, $z, 1); | ||
| 118 | system "$Games::Rezrov::GetKey::STTY icanon echo"; | ||
| 119 | return $z; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub END { | ||
| 123 | system "$Games::Rezrov::GetKey::STTY icanon echo"; | ||
| 124 | } | ||
| 125 | DONE | ||
| 126 | } else { | ||
| 127 | $CAN_READ_SINGLE = 0; | ||
| 128 | print STDERR "giving up" if DEBUG; | ||
| 129 | eval << 'DONE' | ||
| 130 | sub get_key { | ||
| 131 | my $z; | ||
| 132 | read(STDIN, $z, 1); | ||
| 133 | return $z; | ||
| 134 | } | ||
| 135 | DONE | ||
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | 1; | ||
| 142 | |||
| 143 |