| 1 | package TUI::Drivers::HardwareInfo::Win32; | ||
| 2 | # ABSTRACT: Win32 driver for TUI::Drivers::HardwareInfo | ||
| 3 | |||
| 4 | use 5.010; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | our $VERSION = '2.000002'; | ||
| 9 | $VERSION =~ tr/_//d; | ||
| 10 | our $AUTHORITY = 'cpan:BRICKPOOL'; | ||
| 11 | |||
| 12 | use PerlX::Assert::PP; | ||
| 13 | use English qw( -no_match_vars ); | ||
| 14 | use Scalar::Util qw( | ||
| 15 | blessed | ||
| 16 | looks_like_number | ||
| 17 | readonly | ||
| 18 | ); | ||
| 19 | use TUI::toolkit::boolean; | ||
| 20 | use Win32; | ||
| 21 | use Win32::API; | ||
| 22 | use Win32::Console; | ||
| 23 | use Win32::Console::PatchForRT33513; | ||
| 24 | use Win32API::File; | ||
| 25 | |||
| 26 | use TUI::Drivers::Const qw( | ||
| 27 | evCommand | ||
| 28 | evKeyDown | ||
| 29 | :smXXXX | ||
| 30 | kbAltShift | ||
| 31 | kbCtrlShift | ||
| 32 | kbShift | ||
| 33 | kbIns | ||
| 34 | kbInsState | ||
| 35 | kbCtrlA | ||
| 36 | kbCtrlC | ||
| 37 | kbCtrlZ | ||
| 38 | ); | ||
| 39 | use TUI::Drivers::Util qw( getAltCode ); | ||
| 40 | use TUI::Views::Const qw( cmScreenChanged ); | ||
| 41 | |||
| 42 | # We use variables to avoid polluting the namespace when importing Win32 API | ||
| 43 | # functions. | ||
| 44 | my ( | ||
| 45 | $GetNumberOfConsoleMouseButtons, | ||
| 46 | ); | ||
| 47 | |||
| 48 | # Load required Windows API functions | ||
| 49 | BEGIN { | ||
| 50 | $GetNumberOfConsoleMouseButtons = Win32::API::More->new('kernel32', | ||
| 51 | 'BOOL GetNumberOfConsoleMouseButtons( | ||
| 52 | LPDWORD lpNumberOfMouseButtons | ||
| 53 | )' | ||
| 54 | ) or die "Import GetNumberOfConsoleMouseButtons: $^E"; | ||
| 55 | } | ||
| 56 | |||
| 57 | PRIVATE: { | ||
| 58 | namespace::sweep->import( -also => [qw( | ||
| 59 | cnInput | ||
| 60 | cnOutput | ||
| 61 | dwSize | ||
| 62 | bVisible | ||
| 63 | dwSizeX | ||
| 64 | dwSizeY | ||
| 65 | srWindowLeft | ||
| 66 | srWindowTop | ||
| 67 | srWindowRight | ||
| 68 | srWindowBottom | ||
| 69 | EventType | ||
| 70 | KEY_EVENT | ||
| 71 | MOUSE_EVENT | ||
| 72 | bKeyDown | ||
| 73 | wRepeatCount | ||
| 74 | wVirtualKeyCode | ||
| 75 | wVirtualScanCode | ||
| 76 | uChar | ||
| 77 | dwControlKeyState1 | ||
| 78 | dwMousePositionX | ||
| 79 | dwMousePositionY | ||
| 80 | dwButtonState | ||
| 81 | dwControlKeyState2 | ||
| 82 | dwEventFlags | ||
| 83 | ENABLE_QUICK_EDIT_MODE | ||
| 84 | ENABLE_EXTENDED_FLAGS | ||
| 85 | )] ) if eval { require namespace::sweep }; | ||
| 86 | |||
| 87 | # ConsoleType | ||
| 88 | sub cnInput (){ 0 } | ||
| 89 | sub cnStartup (){ 1 } | ||
| 90 | sub cnOutput (){ 2 } | ||
| 91 | |||
| 92 | # CONSOLE_CURSOR_INFO | ||
| 93 | sub dwSize (){ 2 } | ||
| 94 | sub bVisible (){ 3 } | ||
| 95 | |||
| 96 | # CONSOLE_SCREEN_BUFFER_INFO | ||
| 97 | sub dwSizeX (){ 0 } | ||
| 98 | sub dwSizeY (){ 1 } | ||
| 99 | sub srWindowLeft (){ 5 } | ||
| 100 | sub srWindowTop (){ 6 } | ||
| 101 | sub srWindowRight (){ 7 } | ||
| 102 | sub srWindowBottom (){ 8 } | ||
| 103 | |||
| 104 | # INPUT_RECORD | ||
| 105 | sub EventType (){ 0 } | ||
| 106 | |||
| 107 | # EventType | ||
| 108 | sub KEY_EVENT (){ 0x0001 } | ||
| 109 | sub MOUSE_EVENT (){ 0x0002 } | ||
| 110 | |||
| 111 | # KEY_EVENT_RECORD | ||
| 112 | sub bKeyDown (){ 1 } | ||
| 113 | sub wRepeatCount (){ 2 } | ||
| 114 | sub wVirtualKeyCode (){ 3 } | ||
| 115 | sub wVirtualScanCode (){ 4 } | ||
| 116 | sub uChar (){ 5 } | ||
| 117 | sub dwControlKeyState1 (){ 6 } | ||
| 118 | |||
| 119 | # MOUSE_EVENT_RECORD | ||
| 120 | sub dwMousePositionX (){ 1 } | ||
| 121 | sub dwMousePositionY (){ 2 } | ||
| 122 | sub dwButtonState (){ 3 } | ||
| 123 | sub dwControlKeyState2 (){ 4 } | ||
| 124 | sub dwEventFlags (){ 5 } | ||
| 125 | |||
| 126 | # Additional constants which are missing in the Win32::Console module. | ||
| 127 | sub ENABLE_QUICK_EDIT_MODE (){ 0x0040 } | ||
| 128 | sub ENABLE_EXTENDED_FLAGS (){ 0x0080 } | ||
| 129 | } | ||
| 130 | |||
| 131 | # declare global variables | ||
| 132 | our $insertState = true; | ||
| 133 | our $platform = ''; | ||
| 134 | our @consoleHandle = (); | ||
| 135 | our $ownsConsole = false; | ||
| 136 | our $consoleMode = 0; | ||
| 137 | our $pendingEvent = 0; | ||
| 138 | our @irBuffer = (); | ||
| 139 | our @crInfo = (); | ||
| 140 | our @sbInfo = (); | ||
| 141 | |||
| 142 | # import global variables | ||
| 143 | use vars qw( | ||
| 144 | $ctrlBreakHit | ||
| 145 | ); | ||
| 146 | { | ||
| 147 | *ctrlBreakHit = \$TUI::Drivers::SystemError::ctrlBreakHit; | ||
| 148 | } | ||
| 149 | |||
| 150 | # declare local variables | ||
| 151 | |||
| 152 | my @ShiftCvt = ( | ||
| 153 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 154 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 155 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 156 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 157 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 158 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 159 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 160 | 0, 0, 0, 0x5400, 0x5500, 0x5600, 0x5700, 0x5800, | ||
| 161 | 0x5900, 0x5a00, 0x5b00, 0x5c00, 0x5d00, 0, 0, 0, | ||
| 162 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 163 | 0, 0, 0x0500, 0x0700, 0, 0, 0, 0x8700, | ||
| 164 | 0x8800 | ||
| 165 | ); | ||
| 166 | |||
| 167 | my @CtrlCvt = ( | ||
| 168 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 169 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 170 | 0x0011, 0x0017, 0x0005, 0x0012, 0x0014, 0x0019, 0x0015, 0x0009, | ||
| 171 | 0x000f, 0x0010, 0, 0, 0, 0, 0x0001, 0x0013, | ||
| 172 | 0x0004, 0x0006, 0x0007, 0x0008, 0x000a, 0x000b, 0x000c, 0, | ||
| 173 | 0, 0, 0, 0, 0x001a, 0x0018, 0x0003, 0x0016, | ||
| 174 | 0x0002, 0x000e, 0x000d, 0, 0, 0, 0, 0, | ||
| 175 | 0, 0, 0, 0x5e00, 0x5f00, 0x6000, 0x6100, 0x6200, | ||
| 176 | 0x6300, 0x6400, 0x6500, 0x6600, 0x6700, 0, 0, 0x7700, | ||
| 177 | 0, 0x8400, 0, 0x7300, 0, 0x7400, 0, 0x7500, | ||
| 178 | 0, 0x7600, 0x0400, 0x0600, 0, 0, 0, 0x8900, | ||
| 179 | 0x8a00 | ||
| 180 | ); | ||
| 181 | |||
| 182 | my @AltCvt = ( | ||
| 183 | 0, 0, 0x7800, 0x7900, 0x7a00, 0x7b00, 0x7c00, 0x7d00, | ||
| 184 | 0x7e00, 0x7f00, 0x8000, 0x8100, 0x8200, 0x8300, 0x0800, 0, | ||
| 185 | 0x1000, 0x1100, 0x1200, 0x1300, 0x1400, 0x1500, 0x1600, 0x1700, | ||
| 186 | 0x1800, 0x1900, 0, 0, 0, 0, 0x1e00, 0x1f00, | ||
| 187 | 0x2000, 0x2100, 0x2200, 0x2300, 0x2400, 0x2500, 0x2600, 0, | ||
| 188 | 0, 0, 0, 0, 0x2c00, 0x2d00, 0x2e00, 0x2f00, | ||
| 189 | 0x3000, 0x3100, 0x3200, 0, 0, 0, 0, 0, | ||
| 190 | 0, 0x0200, 0, 0x6800, 0x6900, 0x6a00, 0x6b00, 0x6c00, | ||
| 191 | 0x6d00, 0x6e00, 0x6f00, 0x7000, 0x7100, 0, 0, 0, | ||
| 192 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 193 | 0, 0, 0, 0, 0, 0, 0, 0x8b00, | ||
| 194 | 0x8c00 | ||
| 195 | ); | ||
| 196 | |||
| 197 | my @lastSize = ( 0, 0 ); | ||
| 198 | |||
| 199 | # private functions | ||
| 200 | |||
| 201 | my $isValid = sub { # $bool ($self) | ||
| 202 | my ( $self ) = @_; | ||
| 203 | return undef unless ref( $self ); | ||
| 204 | return !!$self->Mode(); | ||
| 205 | }; | ||
| 206 | |||
| 207 | INIT { | ||
| 208 | my $mod; | ||
| 209 | |||
| 210 | if ( ( $mod = $^O ) && ( $mod ne 'MSWin32' ) ) { | ||
| 211 | ...; | ||
| 212 | } | ||
| 213 | else { | ||
| 214 | $platform = 'Windows'; | ||
| 215 | } | ||
| 216 | |||
| 217 | # The following content was ported from the framework | ||
| 218 | # "A modern port of Turbo Vision 2.0", which is licensed under MIT licence. | ||
| 219 | # | ||
| 220 | # Copyright 2019-2021 by magiblot <magiblot@hotmail.com> | ||
| 221 | # | ||
| 222 | # I<conctl.cpp> | ||
| 223 | { | ||
| 224 | my $console; | ||
| 225 | my $have_console = false; | ||
| 226 | |||
| 227 | $console = Win32::Console->new( STD_INPUT_HANDLE ); | ||
| 228 | if ( $console && $console->$isValid() ) { | ||
| 229 | $have_console = true; | ||
| 230 | $consoleHandle[cnInput] = $console; | ||
| 231 | } | ||
| 232 | $console = Win32::Console->new( STD_OUTPUT_HANDLE ); | ||
| 233 | if ( $console && $console->$isValid() ) { | ||
| 234 | $have_console = true; | ||
| 235 | $consoleHandle[cnStartup] = $console; | ||
| 236 | } | ||
| 237 | $console = Win32::Console->new( STD_ERROR_HANDLE ); | ||
| 238 | if ( $console && $console->$isValid() ) { | ||
| 239 | $have_console = true; | ||
| 240 | $consoleHandle[cnStartup] = $console; | ||
| 241 | } | ||
| 242 | |||
| 243 | unless ( $have_console ) { | ||
| 244 | Win32::Console::Free(); | ||
| 245 | Win32::Console::Alloc(); | ||
| 246 | $ownsConsole = true; | ||
| 247 | } | ||
| 248 | |||
| 249 | unless ( $consoleHandle[cnInput] ) { | ||
| 250 | # Create a new generic object | ||
| 251 | $console = Win32::Console->new(); | ||
| 252 | if ( $console && $console->$isValid() ) { | ||
| 253 | # Assign a handle created by CreateFile() to the object | ||
| 254 | $console->{handle} = Win32API::File::createFile( | ||
| 255 | 'CONIN$', | ||
| 256 | { | ||
| 257 | Access => GENERIC_READ | GENERIC_WRITE, | ||
| 258 | Share => FILE_SHARE_READ, | ||
| 259 | Create => Win32API::File::OPEN_EXISTING, | ||
| 260 | } | ||
| 261 | ); | ||
| 262 | $consoleHandle[cnInput] = $console; | ||
| 263 | } | ||
| 264 | } | ||
| 265 | unless ( $consoleHandle[cnStartup] ) { | ||
| 266 | $console = Win32::Console->new(); | ||
| 267 | if ( $console && $console->$isValid() ) { | ||
| 268 | $console->{handle} = Win32API::File::createFile( | ||
| 269 | 'CONOUT$', | ||
| 270 | { | ||
| 271 | Access => GENERIC_READ | GENERIC_WRITE, | ||
| 272 | Share => FILE_SHARE_WRITE, | ||
| 273 | Create => Win32API::File::OPEN_EXISTING, | ||
| 274 | } | ||
| 275 | ); | ||
| 276 | $consoleHandle[cnStartup] = $console; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | $console = Win32::Console->new( GENERIC_READ | GENERIC_WRITE, 0 ); | ||
| 280 | if ( $console && $console->$isValid() ) { | ||
| 281 | $consoleHandle[cnOutput] = $console; | ||
| 282 | if ( ref $consoleHandle[cnStartup] ) { | ||
| 283 | @sbInfo = $consoleHandle[cnStartup]->Info(); | ||
| 284 | # Force the screen buffer size to match the window size. | ||
| 285 | # The Console API guarantees this, but some implementations | ||
| 286 | # are not compliant (e.g. Wine). | ||
| 287 | $sbInfo[dwSizeX] = $sbInfo[srWindowRight] - $sbInfo[srWindowLeft] + 1; | ||
| 288 | $sbInfo[dwSizeY] = $sbInfo[srWindowBottom] - $sbInfo[srWindowTop] + 1; | ||
| 289 | $consoleHandle[cnOutput]->Size( @sbInfo[dwSizeX, dwSizeY] ); | ||
| 290 | } | ||
| 291 | $consoleHandle[cnOutput]->Display(); | ||
| 292 | } | ||
| 293 | |||
| 294 | die "Error: cannot get a console.\n" | ||
| 295 | unless grep { $_ && $_->$isValid() } @consoleHandle; | ||
| 296 | } | ||
| 297 | |||
| 298 | $consoleMode = $consoleHandle[cnInput]->Mode(); | ||
| 299 | @crInfo = $consoleHandle[cnOutput]->Cursor(); | ||
| 300 | @sbInfo = $consoleHandle[cnOutput]->Info(); | ||
| 301 | @lastSize = @sbInfo[ dwSizeX, dwSizeY ]; | ||
| 302 | } | ||
| 303 | |||
| 304 | END { | ||
| 305 | $consoleHandle[cnStartup]->Display() | ||
| 306 | if ref $consoleHandle[cnStartup]; | ||
| 307 | Win32::Console::Free() | ||
| 308 | if $ownsConsole; | ||
| 309 | } | ||
| 310 | |||
| 311 | sub getTickCount { # $ticks ($class) | ||
| 312 | assert ( $_[0] and !ref $_[0] ); | ||
| 313 | # To change units from ms to clock ticks. | ||
| 314 | # X ms * 1s/1000ms * 18.2ticks/s = X/55 ticks, roughly. | ||
| 315 | return int( Win32::GetTickCount() / 55 ); | ||
| 316 | } | ||
| 317 | |||
| 318 | sub getPlatform { # $osname ($class) | ||
| 319 | assert ( $_[0] and !ref $_[0] ); | ||
| 320 | return $platform; | ||
| 321 | } | ||
| 322 | |||
| 323 | # Caret functions. | ||
| 324 | |||
| 325 | sub setCaretSize { # void ($class, $size) | ||
| 326 | my ( $class, $size ) = @_; | ||
| 327 | assert ( $class and !ref $class ); | ||
| 328 | assert ( looks_like_number $size ); | ||
| 329 | return unless ref $consoleHandle[cnOutput]; | ||
| 330 | @crInfo = $consoleHandle[cnOutput]->Cursor(); | ||
| 331 | if ( $size == 0 ) { | ||
| 332 | $crInfo[bVisible] = 0; | ||
| 333 | $crInfo[dwSize] = 1; | ||
| 334 | } | ||
| 335 | else { | ||
| 336 | $crInfo[bVisible] = 1; | ||
| 337 | $crInfo[dwSize] = $size; | ||
| 338 | } | ||
| 339 | $consoleHandle[cnOutput]->Cursor(@crInfo); | ||
| 340 | return; | ||
| 341 | } #/ sub setCaretSize | ||
| 342 | |||
| 343 | sub getCaretSize { # $size ($class) | ||
| 344 | assert ( $_[0] and !ref $_[0] ); | ||
| 345 | return $crInfo[dwSize]; | ||
| 346 | } | ||
| 347 | |||
| 348 | sub setCaretPosition { # void ($class, $x, $y) | ||
| 349 | my ( $class, $x, $y ) = @_; | ||
| 350 | assert ( $class and !ref $class ); | ||
| 351 | assert ( looks_like_number $x ); | ||
| 352 | assert ( looks_like_number $y ); | ||
| 353 | return unless ref $consoleHandle[cnOutput]; | ||
| 354 | $consoleHandle[cnOutput]->Cursor($x, $y); | ||
| 355 | return; | ||
| 356 | } | ||
| 357 | |||
| 358 | sub isCaretVisible { # $visible ($class) | ||
| 359 | assert ( $_[0] and !ref $_[0] ); | ||
| 360 | return $crInfo[bVisible]; | ||
| 361 | } | ||
| 362 | |||
| 363 | # Screen functions. | ||
| 364 | |||
| 365 | sub getScreenRows { # $rows ($class) | ||
| 366 | assert ( $_[0] and !ref $_[0] ); | ||
| 367 | return $sbInfo[dwSizeY]; | ||
| 368 | } | ||
| 369 | |||
| 370 | sub getScreenCols { # $cols ($class) | ||
| 371 | assert ( $_[0] and !ref $_[0] ); | ||
| 372 | return $sbInfo[dwSizeX]; | ||
| 373 | } | ||
| 374 | |||
| 375 | sub getScreenMode { # $mode ($class) | ||
| 376 | my $class = shift; | ||
| 377 | assert ( $class and !ref $class ); | ||
| 378 | my $mode = 0; | ||
| 379 | if ( $platform eq 'Windows' ) { | ||
| 380 | $mode = smCO80; # B/W, mono not supported if running on Windows | ||
| 381 | } | ||
| 382 | if ( $class->getScreenRows() > 25 ) { | ||
| 383 | $mode |= smFont8x8; | ||
| 384 | } | ||
| 385 | return $mode; | ||
| 386 | } #/ sub getScreenMode | ||
| 387 | |||
| 388 | sub setScreenMode { # void ($class, $mode) | ||
| 389 | my ( $class, $mode ) = @_; | ||
| 390 | assert ( $class and !ref $class ); | ||
| 391 | assert ( looks_like_number $mode ); | ||
| 392 | return unless ref $consoleHandle[cnOutput]; | ||
| 393 | my %newSize = ( X => 80, Y => 25 ); | ||
| 394 | my %rect = ( Left => 0, Top => 0, Right => 79, Bottom => 24 ); | ||
| 395 | |||
| 396 | if ( $mode & smFont8x8 ) { | ||
| 397 | $newSize{Y} = 50; | ||
| 398 | $rect{Bottom} = 49; | ||
| 399 | } | ||
| 400 | |||
| 401 | if ( $platform eq 'Windows' ) { | ||
| 402 | my ( $maxSizeX, $maxSizeY ) = $consoleHandle[cnOutput]->MaxWindow(); | ||
| 403 | if ( $newSize{Y} > $maxSizeY ) { | ||
| 404 | $newSize{Y} = $maxSizeY; | ||
| 405 | $rect{Bottom} = $newSize{Y} - 1; | ||
| 406 | } | ||
| 407 | } | ||
| 408 | |||
| 409 | if ( $mode & smFont8x8 ) { | ||
| 410 | $consoleHandle[cnOutput]->Size( @newSize{qw(X Y)} ); | ||
| 411 | $consoleHandle[cnOutput]->Window( @rect{qw(Left Top Right Bottom)} ); | ||
| 412 | } | ||
| 413 | else { | ||
| 414 | $consoleHandle[cnOutput]->Window( @rect{qw(Left Top Right Bottom)} ); | ||
| 415 | $consoleHandle[cnOutput]->Size( @newSize{qw(X Y)} ); | ||
| 416 | } | ||
| 417 | |||
| 418 | @sbInfo = $consoleHandle[cnOutput]->Size(); | ||
| 419 | return; | ||
| 420 | } #/ sub setScreenMode | ||
| 421 | |||
| 422 | sub clearScreen { # void ($class, $w, $h); | ||
| 423 | my ( $class, $w, $h ) = @_; | ||
| 424 | assert ( $class and !ref $class ); | ||
| 425 | assert ( looks_like_number $w ); | ||
| 426 | assert ( looks_like_number $h ); | ||
| 427 | return unless ref $consoleHandle[cnOutput]; | ||
| 428 | $consoleHandle[cnOutput]->FillAttr( 0x07, $w * $h, 0, 0 ); | ||
| 429 | $consoleHandle[cnOutput]->FillChar( ' ', $w * $h, 0, 0 ); | ||
| 430 | return; | ||
| 431 | } | ||
| 432 | |||
| 433 | sub screenWrite { # void ($class, $x, $y, $buf, $len) | ||
| 434 | my ( $class, $x, $y, $buf, $len ) = @_; | ||
| 435 | assert ( $class and !ref $class ); | ||
| 436 | assert ( looks_like_number $x ); | ||
| 437 | assert ( looks_like_number $y ); | ||
| 438 | assert ( ref $buf ); | ||
| 439 | assert ( looks_like_number $len ); | ||
| 440 | return unless ref $consoleHandle[cnOutput]; | ||
| 441 | my %to = ( Left => $x, Top => $y, Right => $x + $len - 1, Bottom => $y ); | ||
| 442 | |||
| 443 | $consoleHandle[cnOutput]->WriteRect( | ||
| 444 | pack( 'S*', @$buf ), @to{qw(Left Top Right Bottom)} | ||
| 445 | ); | ||
| 446 | return; | ||
| 447 | } | ||
| 448 | |||
| 449 | sub allocateScreenBuffer { # \@buffer ($class) | ||
| 450 | my ( $class ) = @_; | ||
| 451 | assert ( $class and !ref $class ); | ||
| 452 | $class->reloadScreenInfo(); | ||
| 453 | |||
| 454 | my $x = $sbInfo[dwSizeX]; | ||
| 455 | my $y = $sbInfo[dwSizeY]; | ||
| 456 | |||
| 457 | # Make sure we allocate at least enough for a 80x50 screen. | ||
| 458 | $x = 80 if $x < 80; | ||
| 459 | $y = 50 if $y < 50; | ||
| 460 | |||
| 461 | return [ ( 0 ) x ( $x * $y * 2 ) ]; | ||
| 462 | } | ||
| 463 | |||
| 464 | sub freeScreenBuffer { # void ($class, \@buffer) | ||
| 465 | assert ( $_[0] and !ref $_[0] ); | ||
| 466 | assert ( ref $_[1] and !readonly @{$_[1]} ); | ||
| 467 | $_[1] = []; | ||
| 468 | return; | ||
| 469 | } | ||
| 470 | |||
| 471 | # Mouse functions. | ||
| 472 | |||
| 473 | sub getButtonCount { # $num ($class) | ||
| 474 | assert ( $_[0] and !ref $_[0] ); | ||
| 475 | my $num = 0; | ||
| 476 | $GetNumberOfConsoleMouseButtons->Call( $num ); | ||
| 477 | return $num; | ||
| 478 | } | ||
| 479 | |||
| 480 | sub cursorOn { # void ($class) | ||
| 481 | assert ( $_[0] and !ref $_[0] ); | ||
| 482 | return unless ref $consoleHandle[cnInput]; | ||
| 483 | # Disable the Quick Edit mode, which inhibits the mouse. | ||
| 484 | local $consoleMode = $consoleMode; | ||
| 485 | $consoleMode |= ENABLE_EXTENDED_FLAGS; | ||
| 486 | $consoleMode &= ~ENABLE_QUICK_EDIT_MODE; | ||
| 487 | $consoleHandle[cnInput]->Mode( $consoleMode | ENABLE_MOUSE_INPUT ); | ||
| 488 | return; | ||
| 489 | } | ||
| 490 | |||
| 491 | sub cursorOff { # void ($class) | ||
| 492 | assert ( $_[0] and !ref $_[0] ); | ||
| 493 | return unless ref $consoleHandle[cnInput]; | ||
| 494 | $consoleHandle[cnInput]->Mode( $consoleMode & ~ENABLE_MOUSE_INPUT ); | ||
| 495 | return; | ||
| 496 | } | ||
| 497 | |||
| 498 | # Event functions. | ||
| 499 | |||
| 500 | sub clearPendingEvent { # void ($class) | ||
| 501 | assert ( $_[0] and !ref $_[0] ); | ||
| 502 | $pendingEvent = 0; | ||
| 503 | return; | ||
| 504 | } | ||
| 505 | |||
| 506 | sub getMouseEvent { # $bool ($class, $event) | ||
| 507 | my ( $class, $event ) = @_; | ||
| 508 | assert ( $class and !ref $class ); | ||
| 509 | assert ( blessed $event ); | ||
| 510 | return unless ref $consoleHandle[cnInput]; | ||
| 511 | if ( !$pendingEvent ) { | ||
| 512 | $pendingEvent = $consoleHandle[cnInput]->GetEvents(); | ||
| 513 | if ( $pendingEvent ) { | ||
| 514 | @irBuffer = $consoleHandle[cnInput]->Input(); | ||
| 515 | $irBuffer[EventType] ||= 0; | ||
| 516 | } | ||
| 517 | } | ||
| 518 | |||
| 519 | if ( $pendingEvent && $irBuffer[EventType] == MOUSE_EVENT ) { | ||
| 520 | $event->{where}{x} = $irBuffer[dwMousePositionX]; | ||
| 521 | $event->{where}{y} = $irBuffer[dwMousePositionY]; | ||
| 522 | $event->{buttons} = $irBuffer[dwButtonState]; | ||
| 523 | $event->{eventFlags} = $irBuffer[dwEventFlags]; | ||
| 524 | $event->{controlKeyState} = $irBuffer[dwControlKeyState2]; | ||
| 525 | |||
| 526 | $pendingEvent = 0; | ||
| 527 | return true; | ||
| 528 | } #/ if ( $pendingEvent && ...) | ||
| 529 | return false; | ||
| 530 | } #/ sub getMouseEvent | ||
| 531 | |||
| 532 | sub getKeyEvent { # $bool ($class, $event) | ||
| 533 | my ( $class, $event ) = @_; | ||
| 534 | assert ( $class and !ref $class ); | ||
| 535 | assert ( blessed $event ); | ||
| 536 | return unless ref $consoleHandle[cnInput]; | ||
| 537 | if ( !$pendingEvent ) { | ||
| 538 | $pendingEvent = $consoleHandle[cnInput]->GetEvents(); | ||
| 539 | if ( $pendingEvent ) { | ||
| 540 | @irBuffer = $consoleHandle[cnInput]->Input(); | ||
| 541 | $irBuffer[EventType] ||= 0; | ||
| 542 | } | ||
| 543 | } | ||
| 544 | |||
| 545 | if ( $pendingEvent ) { | ||
| 546 | if ( $irBuffer[EventType] == KEY_EVENT && $irBuffer[bKeyDown] ) { | ||
| 547 | $event->{what} = evKeyDown; | ||
| 548 | $event->{keyDown}{charScan}{scanCode} = $irBuffer[wVirtualScanCode]; | ||
| 549 | $event->{keyDown}{charScan}{charCode} = $irBuffer[uChar]; | ||
| 550 | $event->{keyDown}{controlKeyState} = $irBuffer[dwControlKeyState1]; | ||
| 551 | |||
| 552 | # Win32::OutputDebugString(sprintf( | ||
| 553 | # "scan=%d char=%d ctrl=%#x", | ||
| 554 | # $irBuffer[wVirtualScanCode], | ||
| 555 | # $irBuffer[uChar], | ||
| 556 | # $irBuffer[dwControlKeyState1], | ||
| 557 | # )); | ||
| 558 | |||
| 559 | # Convert Windows style virtual scan codes to PC BIOS codes. | ||
| 560 | if ( $event->{keyDown}{controlKeyState} & | ||
| 561 | ( kbShift | kbAltShift | kbCtrlShift ) | ||
| 562 | ) { | ||
| 563 | my $index = $irBuffer[wVirtualScanCode]; | ||
| 564 | if ( ( $event->{keyDown}{controlKeyState} & kbAltShift ) | ||
| 565 | && ( $event->{keyDown}{controlKeyState} & kbCtrlShift ) | ||
| 566 | ) { | ||
| 567 | # AltGr is usually reported as Ctrl+Alt on Win32 consoles. | ||
| 568 | # Do not translate through AltCvt/CtrlCvt in that case. | ||
| 569 | } | ||
| 570 | elsif ( ( $event->{keyDown}{controlKeyState} & kbShift ) | ||
| 571 | && $ShiftCvt[$index] | ||
| 572 | ) { | ||
| 573 | $event->{keyDown}{keyCode} = $ShiftCvt[$index]; | ||
| 574 | } | ||
| 575 | elsif ( ( $event->{keyDown}{controlKeyState} & kbCtrlShift ) | ||
| 576 | && $CtrlCvt[$index] | ||
| 577 | ) { | ||
| 578 | my $charCode = $event->{keyDown}{charScan}{charCode}; | ||
| 579 | if ( $charCode >= kbCtrlA && $charCode <= kbCtrlZ ) { | ||
| 580 | # Do not translate through CtrlCvt if the charCode is already a | ||
| 581 | # control character. | ||
| 582 | $event->{keyDown}{keyCode} = $charCode; | ||
| 583 | } else { | ||
| 584 | # Borland default behavior is to translate the charCode through | ||
| 585 | # CtrlCvt if the Ctrl key is pressed. | ||
| 586 | $event->{keyDown}{keyCode} = $CtrlCvt[$index]; | ||
| 587 | } | ||
| 588 | } | ||
| 589 | elsif ( ( $event->{keyDown}{controlKeyState} & kbAltShift ) | ||
| 590 | && $AltCvt[$index] | ||
| 591 | ) { | ||
| 592 | my $ch = chr $event->{keyDown}{charScan}{charCode}; | ||
| 593 | if ( $ch =~ /^[A-Za-z]$/ ) { | ||
| 594 | # Do not translate through AltCvt if the charCode is already a | ||
| 595 | # letter. Translate to kbAltA..kbAltZ instead. | ||
| 596 | $event->{keyDown}{keyCode} = getAltCode( $ch ); | ||
| 597 | } else { | ||
| 598 | # Borland default behavior is to translate the charCode through | ||
| 599 | # AltCvt if the Alt key is pressed. | ||
| 600 | $event->{keyDown}{keyCode} = $AltCvt[$index]; | ||
| 601 | } | ||
| 602 | } | ||
| 603 | } #/ if ( $event->{keyDown}...) | ||
| 604 | |||
| 605 | # Set/Reset insert flag. | ||
| 606 | if ( $event->{keyDown}{keyCode} == kbIns ) { | ||
| 607 | $insertState = !$insertState; | ||
| 608 | } | ||
| 609 | |||
| 610 | if ( $insertState ) { | ||
| 611 | $event->{keyDown}{controlKeyState} |= kbInsState; | ||
| 612 | } | ||
| 613 | |||
| 614 | if ( $event->{keyDown}{keyCode} == kbCtrlC ) { | ||
| 615 | $ctrlBreakHit = true; | ||
| 616 | } | ||
| 617 | |||
| 618 | $pendingEvent = 0; | ||
| 619 | return true; | ||
| 620 | } #/ if ( $irBuffer[EventType...]) | ||
| 621 | elsif ( $irBuffer[EventType] == MOUSE_EVENT ) { | ||
| 622 | # Pending mouse events will be read on the next polling loop. | ||
| 623 | return false; | ||
| 624 | } #/ if ( $pendingEvent ) | ||
| 625 | |||
| 626 | # Other events are not handled by Win32::Console, but we can check the | ||
| 627 | # screen buffer information when no other events are occurring. This | ||
| 628 | # allows us to detect changes in screen size. | ||
| 629 | $pendingEvent = 0; | ||
| 630 | if ( $class->screenChanged() ) { | ||
| 631 | $event->{what} = evCommand; | ||
| 632 | $event->{message}{command} = cmScreenChanged; | ||
| 633 | $event->{message}{infoPtr} = undef; | ||
| 634 | return true; | ||
| 635 | } | ||
| 636 | return false; | ||
| 637 | } | ||
| 638 | |||
| 639 | return false; | ||
| 640 | } #/ sub getKeyEvent | ||
| 641 | |||
| 642 | # System functions. | ||
| 643 | |||
| 644 | sub setCtrlBrkHandler { # $success ($class, $install) | ||
| 645 | my ( $class, $install ) = @_; | ||
| 646 | assert ( @_ == 2 ); | ||
| 647 | assert ( $class and !ref $class ); | ||
| 648 | assert ( !defined $install or !ref $install ); | ||
| 649 | return unless ref $consoleHandle[cnInput]; | ||
| 650 | my $consoleMode = $consoleHandle[cnInput]->Mode() || return; | ||
| 651 | return $consoleHandle[cnInput]->Mode( | ||
| 652 | $install | ||
| 653 | ? $consoleMode & ~ENABLE_PROCESSED_INPUT | ||
| 654 | : $consoleMode | ENABLE_PROCESSED_INPUT | ||
| 655 | ); | ||
| 656 | } | ||
| 657 | |||
| 658 | sub setCritErrorHandler { # $bool ($class, $install) | ||
| 659 | assert ( @_ == 2 ); | ||
| 660 | assert ( $_[0] and !ref $_[0] ); | ||
| 661 | assert ( !defined $_[1] or !ref $_[1] ); | ||
| 662 | # Handled by Windows | ||
| 663 | return true; | ||
| 664 | } | ||
| 665 | |||
| 666 | my $ctrlBreakHandler = sub { ... }; | ||
| 667 | |||
| 668 | # Additional functions (not part of the original Borland interface). | ||
| 669 | |||
| 670 | sub getColorCount { # $count ($class) | ||
| 671 | assert ( $_[0] and !ref $_[0] ); | ||
| 672 | # Windows console supports 16 colors in the default color palette. | ||
| 673 | return 16; | ||
| 674 | } | ||
| 675 | |||
| 676 | sub reloadScreenInfo { # void ($class) | ||
| 677 | my ( $class ) = @_; | ||
| 678 | assert ( $class and !ref $class ); | ||
| 679 | return unless ref $consoleHandle[cnOutput]; | ||
| 680 | @sbInfo = $consoleHandle[cnOutput]->Info(); | ||
| 681 | |||
| 682 | my @curPos = $consoleHandle[cnOutput]->Cursor(); | ||
| 683 | # Set the cursor temporally to (0, 0) to prevent the console from crashing | ||
| 684 | # due to https://github.com/microsoft/terminal/issues/7511. | ||
| 685 | $consoleHandle[cnOutput]->Cursor( 0, 0 ); | ||
| 686 | # Make sure the buffer size matches the viewport size so that the | ||
| 687 | # scrollbars are not shown. | ||
| 688 | $sbInfo[dwSizeX] = $sbInfo[srWindowRight] - $sbInfo[srWindowLeft] + 1; | ||
| 689 | $sbInfo[dwSizeY] = $sbInfo[srWindowBottom] - $sbInfo[srWindowTop] + 1; | ||
| 690 | $consoleHandle[cnOutput]->Size( $sbInfo[dwSizeX], $sbInfo[dwSizeY] ); | ||
| 691 | # Restore the cursor position (it does not matter if it is out of bounds). | ||
| 692 | $consoleHandle[cnOutput]->Cursor( @curPos ); | ||
| 693 | |||
| 694 | return; | ||
| 695 | } | ||
| 696 | |||
| 697 | sub screenChanged { # $bool ($class) | ||
| 698 | assert ( $_[0] and !ref $_[0] ); | ||
| 699 | my @size = do { | ||
| 700 | my ( $left, $top, $right, $bottom ) = $consoleHandle[cnOutput]->Window(); | ||
| 701 | ( $right - $left + 1, $bottom - $top + 1 ); | ||
| 702 | }; | ||
| 703 | if ( $size[dwSizeX] != $lastSize[dwSizeX] | ||
| 704 | || $size[dwSizeY] != $lastSize[dwSizeY] | ||
| 705 | ) { | ||
| 706 | @lastSize = @size; | ||
| 707 | return true; | ||
| 708 | } | ||
| 709 | return false; | ||
| 710 | } | ||
| 711 | |||
| 712 | 1 | ||
| 713 | |||
| 714 | __END__ |