| 1 | =pod | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Win32::Console::DotNet - Win32::Console .NET interface | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | Simply integrate this module into your package or script. | ||
| 10 | |||
| 11 | use Win32::Console::DotNet; | ||
| 12 | # | ||
| 13 | System::Console->WriteLine("The current console title is: \"%s\"", | ||
| 14 | System::Console->Title); | ||
| 15 | System::Console->WriteLine("\t(Press any key to change the console title.)"); | ||
| 16 | System::Console->ReadKey(1); | ||
| 17 | System::Console->Title( "The title has changed!" ); | ||
| 18 | System::Console->WriteLine("Note that the new console title is \"%s\"\n". | ||
| 19 | "\t(Press any key to quit.)", System::Console->Title); | ||
| 20 | System::Console->ReadKey(1); | ||
| 21 | # | ||
| 22 | # This example produces the following results: | ||
| 23 | # The current console title is: "Command Prompt - perl samples\Title.pl" | ||
| 24 | # (Press any key to change the console title.) | ||
| 25 | # Note that the new console title is "The title has changed!" | ||
| 26 | # (Press any key to quit.) | ||
| 27 | # | ||
| 28 | |||
| 29 | Alternatively, using the System namespace. | ||
| 30 | |||
| 31 | use Time::Piece; | ||
| 32 | use Win32::Console::DotNet; | ||
| 33 | use System; | ||
| 34 | # | ||
| 35 | Console->Clear(); | ||
| 36 | my $dat = localtime; | ||
| 37 | # | ||
| 38 | Console->Write("\nToday is %s at %s.", $dat->mdy, $dat->hms); | ||
| 39 | Console->Write("\nPress Enter key to continue... "); | ||
| 40 | Console->ReadLine(); | ||
| 41 | # | ||
| 42 | # The example displays output like the following: | ||
| 43 | # Today is 07/30/2024 at 08:32:54. | ||
| 44 | # Press Enter key to continue... | ||
| 45 | # | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | package Win32::Console::DotNet; | ||
| 50 | |||
| 51 | # ------------------------------------------------------------------------ | ||
| 52 | # Boilerplate ------------------------------------------------------------ | ||
| 53 | # ------------------------------------------------------------------------ | ||
| 54 | |||
| 55 | 12 | ▶ | use 5.014; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 56 | 12 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 57 | |||
| 58 | # version '...' | ||
| 59 | our $version = 'v4.6.0'; | ||
| 60 | our $VERSION = 'v0.5.7'; | ||
| 61 | $VERSION = eval $VERSION; | ||
| 62 | |||
| 63 | # authority '...' | ||
| 64 | our $authority = 'github:microsoft'; | ||
| 65 | our $AUTHORITY = 'github:brickpool'; | ||
| 66 | |||
| 67 | # ------------------------------------------------------------------------ | ||
| 68 | # Used Modules ----------------------------------------------------------- | ||
| 69 | # ------------------------------------------------------------------------ | ||
| 70 | |||
| 71 | 12 | ▶ | use Carp qw( confess ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 72 | 12 | ▶ | use Config; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 73 | 12 | ▶ | use Devel::StrictMode; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 74 | 12 | ▶ | use Devel::Assert STRICT ? 'on' : ''; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 75 | 12 | ▶ | use Encode (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 76 | 12 | ▶ | use Encode::Alias (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 77 | 12 | ▶ | use English qw( -no_match_vars ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 78 | 12 | ▶ | use IO::File; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 79 | 12 | ▶ | use IO::Handle; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 80 | 12 | ▶ | use IO::Null; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 81 | 12 | ▶ | use List::Util qw( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 82 | first | ||
| 83 | max | ||
| 84 | 12 | ); | |
| 85 | 12 | ▶ | use Scalar::Util qw( blessed ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 86 | 0 | ▶ | use threads; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 87 | use threads::shared; | ||
| 88 | use Win32; | ||
| 89 | use Win32::Console; | ||
| 90 | use Win32API::File; | ||
| 91 | |||
| 92 | use namespace::clean; | ||
| 93 | |||
| 94 | # ------------------------------------------------------------------------ | ||
| 95 | # Encode::Encoding ------------------------------------------------------- | ||
| 96 | # ------------------------------------------------------------------------ | ||
| 97 | |||
| 98 | # Gets the code page identifier of the current Encoding. | ||
| 99 | my $CodePage = sub { | ||
| 100 | assert ( @_ == 1 ); | ||
| 101 | my $self = shift; | ||
| 102 | assert ( is_Object($self) && $self->isa('Encode::Encoding') ); | ||
| 103 | |||
| 104 | my $regex = qr/^cp(\d+)$/; | ||
| 105 | my @aliases = grep { | ||
| 106 | /$regex/ && $Encode::Alias::Alias{$_} eq $self; | ||
| 107 | } keys(%Encode::Alias::Alias); | ||
| 108 | my $element = first { /$regex/ } ( $self->name, @aliases ); | ||
| 109 | return $element && $element =~ $regex ? 0+ $1 : 0; | ||
| 110 | }; | ||
| 111 | |||
| 112 | # Allows additional 'cpXXXX' to be used as an alias for encoding. Encoding may | ||
| 113 | # be either the name of an encoding or an encoding object (as described in | ||
| 114 | # Encode). | ||
| 115 | BEGIN { | ||
| 116 | # require Encode; | ||
| 117 | Encode::Alias::define_alias( cp20127 => 'ascii' ); | ||
| 118 | Encode::Alias::define_alias( cp20866 => 'koi8-r' ); | ||
| 119 | Encode::Alias::define_alias( cp21866 => 'koi8-u' ); | ||
| 120 | |||
| 121 | require Encode::Unicode; | ||
| 122 | Encode::Alias::define_alias( cp1200 => 'UTF-16LE' ); | ||
| 123 | Encode::Alias::define_alias( cp1201 => 'UTF-16BE' ); | ||
| 124 | Encode::Alias::define_alias( cp12000 => 'UTF-32LE' ); | ||
| 125 | Encode::Alias::define_alias( cp12001 => 'UTF-32BE' ); | ||
| 126 | |||
| 127 | require Encode::Byte; | ||
| 128 | Encode::Alias::define_alias( qr/^cp2859(\d)$/i => '"iso-8859-$1"' ); | ||
| 129 | Encode::Alias::define_alias( qr/^cp2860(3|5)$/i => '"iso-8859-1$1"' ); | ||
| 130 | |||
| 131 | # require Encode::CN; | ||
| 132 | Encode::Alias::define_alias( cp51936 => 'euc-cn' ); | ||
| 133 | |||
| 134 | # require Encode::JN; | ||
| 135 | Encode::Alias::define_alias( cp20932 => 'euc-jp' ); | ||
| 136 | Encode::Alias::define_alias( cp50221 => 'iso-2022-jp' ); | ||
| 137 | |||
| 138 | # require Encode::KR; | ||
| 139 | Encode::Alias::define_alias( cp50225 => 'iso-2022-kr' ); | ||
| 140 | Encode::Alias::define_alias( cp51949 => 'euc-kr' ); | ||
| 141 | } | ||
| 142 | |||
| 143 | # Allows the use of additional MIME names (stored in Encode::MIME::Name). | ||
| 144 | BEGIN { | ||
| 145 | require Encode::MIME::Name; | ||
| 146 | $Encode::MIME::Name::MIME_NAME_OF{ cp932 } ||= 'Windows-31J'; | ||
| 147 | $Encode::MIME::Name::MIME_NAME_OF{ cp949 } ||= 'Windows-949'; | ||
| 148 | $Encode::MIME::Name::MIME_NAME_OF{ cp950 } ||= 'Big5'; | ||
| 149 | } | ||
| 150 | |||
| 151 | # ------------------------------------------------------------------------ | ||
| 152 | # Class Definition ------------------------------------------------------- | ||
| 153 | # ------------------------------------------------------------------------ | ||
| 154 | |||
| 155 | =head1 DESCRIPTION | ||
| 156 | |||
| 157 | The I<Win32::Console::DotNet> class offers fundamental support for Windows | ||
| 158 | applications that read from and write to a console. | ||
| 159 | |||
| 160 | The I<Win32::Console::DotNet> class is based on the L<Win32::Console> module | ||
| 161 | and provides an I<.NET> compatible API. | ||
| 162 | |||
| 163 | The I<.NET> API provides a number of benefits that allow developers to | ||
| 164 | effectively create console applications and efficiently manage Windows console | ||
| 165 | applications. Here are some of the key features: | ||
| 166 | |||
| 167 | =over | ||
| 168 | |||
| 169 | =item * B<Windows Console Functions>: | ||
| 170 | The I<.NET> API offers a wide range of functions for handling input and | ||
| 171 | output, such as reading keyboard inputs and writing text to the screen. These | ||
| 172 | low-level functions such as I<ReadConsoleInput>, I<WriteConsoleOutput>, and | ||
| 173 | I<SetConsoleCursorPosition> are suitably abstracted by the I<.NET> API. | ||
| 174 | |||
| 175 | =item * B<Windows Console Structures>: | ||
| 176 | Access to low-level data structures such as I<CONSOLE_SCREEN_BUFFER_INFO> or | ||
| 177 | I<INPUT_RECORD> is simplified by using the I<.NET> API. These low-level console | ||
| 178 | structures are effectively managed by this module. | ||
| 179 | |||
| 180 | =item * B<Windows Console Extensions>: | ||
| 181 | It provides an extended compatibility layer to the classic Windows Console API. | ||
| 182 | The I<.NET> API provided in this module is well-known and thoroughly | ||
| 183 | documented, transforming the otherwise complex Windows Console API into a | ||
| 184 | powerful tool for developing Windows console applications. | ||
| 185 | |||
| 186 | =back | ||
| 187 | |||
| 188 | B<To summarize>: The I<.Net> API provides easy direct access to console | ||
| 189 | windows and allows detailed control over display and interaction, making your | ||
| 190 | applications based on the classic Windows console API more robust and | ||
| 191 | therefore better. | ||
| 192 | |||
| 193 | B<Note>: I<System::Console> is the singleton class that represents the | ||
| 194 | I<Win32::Console::DotNet> console applications. You can take a closer look at | ||
| 195 | the use of I<System::Console> in the examples provided. | ||
| 196 | |||
| 197 | =head2 Class | ||
| 198 | |||
| 199 | public class I<< Win32::Console::DotNet >> | ||
| 200 | |||
| 201 | Object Hierarchy | ||
| 202 | |||
| 203 | UNIVERSAL | ||
| 204 | Win32::Console::DotNet | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | package Win32::Console::DotNet { | ||
| 209 | |||
| 210 | # ------------------------------------------------------------------------ | ||
| 211 | # Type Constraints ------------------------------------------------------- | ||
| 212 | # ------------------------------------------------------------------------ | ||
| 213 | |||
| 214 | no namespace::clean; | ||
| 215 | |||
| 216 | =begin private | ||
| 217 | |||
| 218 | =head2 Type Checks | ||
| 219 | |||
| 220 | Basic type checks | ||
| 221 | |||
| 222 | This module use the following type checks: | ||
| 223 | |||
| 224 | is_Bool | ||
| 225 | is_ClassName | ||
| 226 | is_FileHandle | ||
| 227 | is_Object | ||
| 228 | is_Str | ||
| 229 | |||
| 230 | I<Note>: if L<Type::Tiny> is present, the L<Type::Standard> functions are used. | ||
| 231 | |||
| 232 | This module has the following assert type checks: | ||
| 233 | |||
| 234 | assert_Bool | ||
| 235 | assert_ArrayRef | ||
| 236 | assert_CodeRef | ||
| 237 | assert_FileHandle | ||
| 238 | assert_Int | ||
| 239 | assert_Object | ||
| 240 | assert_Str | ||
| 241 | |||
| 242 | =over | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | BEGIN { eval { require Types::Standard } } | ||
| 247 | |||
| 248 | =item I<is_Bool> | ||
| 249 | |||
| 250 | sub is_Bool($value) : Bool | ||
| 251 | |||
| 252 | Check for a reasonable boolean value. Accepts C<1>, C<0>, the empty string and | ||
| 253 | C<undef>. | ||
| 254 | |||
| 255 | I<Param>: C<$value> to be checked. | ||
| 256 | |||
| 257 | I<Returns>: I<true> if operand is boolean. | ||
| 258 | |||
| 259 | =cut | ||
| 260 | |||
| 261 | sub is_Bool($) { | ||
| 262 | # Taken from Types::Nano | ||
| 263 | return !defined($_[0]) | ||
| 264 | || !ref($_[0]) && { 1 => 1, 0 => 1, '' => 1 }->{$_[0]}; | ||
| 265 | }; | ||
| 266 | if ( exists &Types::Standard::is_Bool ) { | ||
| 267 | no warnings qw( redefine prototype ); | ||
| 268 | *is_Bool = \&Types::Standard::is_Bool; | ||
| 269 | } | ||
| 270 | |||
| 271 | =item I<is_ClassName> | ||
| 272 | |||
| 273 | sub is_ClassName ($value) : Bool | ||
| 274 | |||
| 275 | Check for a name of a loaded package. The package must have C<@ISA> or | ||
| 276 | C<$VERSION> defined. | ||
| 277 | |||
| 278 | I<Param>: C<$value> to be checked. | ||
| 279 | |||
| 280 | I<Returns>: I<true> if I<$value> is name of a valid package. | ||
| 281 | |||
| 282 | =cut | ||
| 283 | |||
| 284 | sub is_ClassName($) { | ||
| 285 | # Taken from Types::Standard | ||
| 286 | assert ( @_ == 1 ); | ||
| 287 | my $stash = do { | ||
| 288 | no strict 'refs'; | ||
| 289 | \%{ $_[0] . '::' }; | ||
| 290 | }; | ||
| 291 | return !!1 | ||
| 292 | if exists $stash->{ISA} | ||
| 293 | || exists $stash->{VERSION}; | ||
| 294 | foreach my $globref ( values %{$stash} ) { | ||
| 295 | return !!1 | ||
| 296 | if ref(\$globref) eq 'GLOB' | ||
| 297 | ? *{$globref}{CODE} | ||
| 298 | : ref($globref); | ||
| 299 | } | ||
| 300 | return !!0; | ||
| 301 | }; | ||
| 302 | if ( exists &Types::Standard::is_ClassName ) { | ||
| 303 | no warnings qw( redefine prototype ); | ||
| 304 | *is_ClassName = \&Types::Standard::is_ClassName; | ||
| 305 | } | ||
| 306 | |||
| 307 | =item I<is_FileHandle> | ||
| 308 | |||
| 309 | sub is_FileHandle($value) : Bool | ||
| 310 | |||
| 311 | Check for a file handle. | ||
| 312 | |||
| 313 | I<Param>: C<$value> to be checked. | ||
| 314 | |||
| 315 | I<Returns>: I<true> if I<$value> is a file handle. | ||
| 316 | |||
| 317 | =cut | ||
| 318 | |||
| 319 | sub is_FileHandle($) { | ||
| 320 | # Taken from Params::Util::PP | ||
| 321 | return | ||
| 322 | (ref($_[0]) eq 'GLOB') | ||
| 323 | || | ||
| 324 | (tied($_[0]) && tied($_[0])->can('TIEHANDLE')) | ||
| 325 | || | ||
| 326 | (blessed($_[0]) && $_[0]->isa('IO::Handle')) | ||
| 327 | || | ||
| 328 | (blessed($_[0]) && $_[0]->isa('Tie::Handle')) | ||
| 329 | } | ||
| 330 | if ( exists &Types::Standard::is_FileHandle ) { | ||
| 331 | no warnings qw( redefine prototype ); | ||
| 332 | *is_FileHandle = \&Types::Standard::is_FileHandle; | ||
| 333 | } | ||
| 334 | |||
| 335 | =item I<is_Object> | ||
| 336 | |||
| 337 | sub is_Object($value) : Bool | ||
| 338 | |||
| 339 | Check for a blessed object. | ||
| 340 | |||
| 341 | I<Param>: C<$value> to be checked. | ||
| 342 | |||
| 343 | I<Returns>: I<true> if I<$value> is blessed. | ||
| 344 | |||
| 345 | =cut | ||
| 346 | |||
| 347 | sub is_Object($) { | ||
| 348 | goto &Scalar::Util::blessed; | ||
| 349 | } | ||
| 350 | if ( exists &Types::Standard::is_Object ) { | ||
| 351 | no warnings qw( redefine prototype ); | ||
| 352 | *is_Object = \&Types::Standard::is_Object; | ||
| 353 | } | ||
| 354 | |||
| 355 | =item I<is_Str> | ||
| 356 | |||
| 357 | sub is_Str($value) : Bool | ||
| 358 | |||
| 359 | Check for a string. | ||
| 360 | |||
| 361 | I<Param>: C<$value> to be checked. | ||
| 362 | |||
| 363 | I<Returns>: I<true> if I<$value> is a string. | ||
| 364 | |||
| 365 | =cut | ||
| 366 | |||
| 367 | sub is_Str($) { | ||
| 368 | return defined($_[0]) && !ref($_[0]); | ||
| 369 | } | ||
| 370 | if ( exists &Types::Standard::is_Str ) { | ||
| 371 | no warnings qw( redefine prototype ); | ||
| 372 | *is_Str = \&Types::Standard::is_Str; | ||
| 373 | } | ||
| 374 | |||
| 375 | =item I<get_message> | ||
| 376 | |||
| 377 | classmethod $get_message(Any $value) : Str; | ||
| 378 | |||
| 379 | Generates an error message for an C<assert_*> exception. | ||
| 380 | |||
| 381 | I<Param>: C<$value> to be checked. | ||
| 382 | |||
| 383 | I<Returns>: an error message string. | ||
| 384 | |||
| 385 | =cut | ||
| 386 | |||
| 387 | # code snippet from Type::Nano | ||
| 388 | my $get_message = sub { | ||
| 389 | my ($name, $value) = (shift, @_); | ||
| 390 | |||
| 391 | require B; | ||
| 392 | !defined($value) | ||
| 393 | ? sprintf("Undef did not pass type constraint %s", $name) | ||
| 394 | : ref($value) | ||
| 395 | ? sprintf("Reference %s did not pass type constraint %s", $value, $name) | ||
| 396 | : sprintf("Value %s did not pass type constraint %s", | ||
| 397 | B::perlstring($value), $name); | ||
| 398 | }; | ||
| 399 | |||
| 400 | =item I<assert_Bool> | ||
| 401 | |||
| 402 | sub assert_Bool($value) : Bool | ||
| 403 | |||
| 404 | Check the boolean value. Accepts C<1>, C<0>, the empty string (C<''> or C<"">) | ||
| 405 | and C<undef>. | ||
| 406 | |||
| 407 | I<Param>: C<$value> to be checked. | ||
| 408 | |||
| 409 | I<Returns>: C<$value> if the C<$value> is boolean. | ||
| 410 | |||
| 411 | I<Throws>: I<IllegalArgumentException> if the check fails. | ||
| 412 | |||
| 413 | =cut | ||
| 414 | |||
| 415 | sub assert_Bool($) { | ||
| 416 | confess(sprintf("IllegalArgumentException: %s\n", | ||
| 417 | 'Bool'->$get_message($_[0]))) | ||
| 418 | if STRICT and !is_Bool($_[0]); | ||
| 419 | return $_[0]; | ||
| 420 | } | ||
| 421 | |||
| 422 | =item I<assert_ArrayRef> | ||
| 423 | |||
| 424 | sub assert_ArrayRef($ref) : ArrayRef | ||
| 425 | |||
| 426 | Check the array reference. | ||
| 427 | |||
| 428 | I<Param>: C<$ref> to be checked. | ||
| 429 | |||
| 430 | I<Returns>: C<$ref> if operand is an array reference. | ||
| 431 | |||
| 432 | I<Throws>: I<IllegalArgumentException> if the check fails. | ||
| 433 | |||
| 434 | =cut | ||
| 435 | |||
| 436 | sub assert_ArrayRef($) { | ||
| 437 | confess(sprintf("IllegalArgumentException: %s\n", | ||
| 438 | 'ArrayRef'->$get_message($_[0]))) | ||
| 439 | if STRICT and !(ref($_[0]) eq 'ARRAY'); | ||
| 440 | return $_[0]; | ||
| 441 | } | ||
| 442 | |||
| 443 | =item I<assert_CodeRef> | ||
| 444 | |||
| 445 | sub assert_CodeRef($ref) : CodeRef | ||
| 446 | |||
| 447 | Check the code reference. | ||
| 448 | |||
| 449 | I<Param>: C<$ref> to be checked. | ||
| 450 | |||
| 451 | I<Returns>: C<$ref> if operand is a code reference. | ||
| 452 | |||
| 453 | I<Throws>: I<IllegalArgumentException> if the check fails. | ||
| 454 | |||
| 455 | =cut | ||
| 456 | |||
| 457 | sub assert_CodeRef($) { | ||
| 458 | confess(sprintf("IllegalArgumentException: %s\n", | ||
| 459 | 'CodeRef'->$get_message($_[0]))) | ||
| 460 | if STRICT and !(ref($_[0]) eq 'CODE'); | ||
| 461 | return $_[0]; | ||
| 462 | } | ||
| 463 | |||
| 464 | =item I<assert_FileHandle> | ||
| 465 | |||
| 466 | sub assert_FileHandle($value) : FileHandle | ||
| 467 | |||
| 468 | Check for a file handle. | ||
| 469 | |||
| 470 | I<Param>: C<$value> to be checked. | ||
| 471 | |||
| 472 | I<Returns>: C<$value> if C<$value> is a file handle. | ||
| 473 | |||
| 474 | I<Throws>: I<IllegalArgumentException> if the check fails. | ||
| 475 | |||
| 476 | =cut | ||
| 477 | |||
| 478 | sub assert_FileHandle($) { | ||
| 479 | confess(sprintf("IllegalArgumentException: %s\n", | ||
| 480 | 'FileHandle'->$get_message($_[0]))) | ||
| 481 | if STRICT and !is_FileHandle($_[0]); | ||
| 482 | return $_[0]; | ||
| 483 | } | ||
| 484 | |||
| 485 | =item I<assert_Int> | ||
| 486 | |||
| 487 | sub assert_Int($value) : Int | ||
| 488 | |||
| 489 | Check for on integer; strict constaint. | ||
| 490 | |||
| 491 | I<Param>: C<$value> to be checked. | ||
| 492 | |||
| 493 | I<Returns>: C<$value> if the C<$value> is an integer. | ||
| 494 | |||
| 495 | I<Throws>: I<IllegalArgumentException> if the check fails. | ||
| 496 | |||
| 497 | =cut | ||
| 498 | |||
| 499 | sub assert_Int($) { | ||
| 500 | confess(sprintf("IllegalArgumentException: %s\n", | ||
| 501 | 'Int'->get_message($_[0]))) | ||
| 502 | if STRICT | ||
| 503 | and !(defined($_[0]) && !ref($_[0]) && $_[0] =~ /\A[+-]?\d+\z/); | ||
| 504 | return $_[0]; | ||
| 505 | } | ||
| 506 | |||
| 507 | =item I<assert_Object> | ||
| 508 | |||
| 509 | sub assert_Object($value) : Object | ||
| 510 | |||
| 511 | Check for a blessed object. | ||
| 512 | |||
| 513 | I<Param>: C<$value> to be checked. | ||
| 514 | |||
| 515 | I<Returns>: C<$value> if C<$value> is blessed. | ||
| 516 | |||
| 517 | I<Throws>: I<IllegalArgumentException> if the check fails. | ||
| 518 | |||
| 519 | =cut | ||
| 520 | |||
| 521 | sub assert_Object($) { | ||
| 522 | confess(sprintf("IllegalArgumentException: %s\n", | ||
| 523 | 'Object'->$get_message($_[0]))) | ||
| 524 | if STRICT and !is_Object($_[0]); | ||
| 525 | return $_[0]; | ||
| 526 | } | ||
| 527 | |||
| 528 | =item I<assert_Str> | ||
| 529 | |||
| 530 | sub assert_Str($value) : Str | ||
| 531 | |||
| 532 | Check the string that cannot be stringified. | ||
| 533 | |||
| 534 | I<Param>: C<$value> to be checked. | ||
| 535 | |||
| 536 | I<Returns>: C<$value> if the C<$value> is a string. | ||
| 537 | |||
| 538 | I<Throws>: I<IllegalArgumentException> if the check fails. | ||
| 539 | |||
| 540 | =cut | ||
| 541 | |||
| 542 | sub assert_Str($) { | ||
| 543 | confess(sprintf("IllegalArgumentException: %s\n", | ||
| 544 | 'Str'->get_message($_[0]))) | ||
| 545 | if STRICT and !is_Str($_[0]); | ||
| 546 | return $_[0]; | ||
| 547 | } | ||
| 548 | |||
| 549 | =back | ||
| 550 | |||
| 551 | =end private | ||
| 552 | |||
| 553 | =cut | ||
| 554 | |||
| 555 | # ------------------------------------------------------------------------ | ||
| 556 | # Constants -------------------------------------------------------------- | ||
| 557 | # ------------------------------------------------------------------------ | ||
| 558 | |||
| 559 | =head2 Constants | ||
| 560 | |||
| 561 | =begin private | ||
| 562 | |||
| 563 | =over | ||
| 564 | |||
| 565 | =item I<_DEBUG> | ||
| 566 | |||
| 567 | use constant _DEBUG => 1|undef; | ||
| 568 | |||
| 569 | C<_DEBUG> is defined as 1 if the environment variable C<NDEBUG> or | ||
| 570 | C<PERL_NDEBUG> is not defined as I<true> and the environment variable | ||
| 571 | C<EXTENDED_TESTING> has been set to I<true>, otherwise undefined. | ||
| 572 | |||
| 573 | =cut | ||
| 574 | |||
| 575 | use constant _DEBUG => ( $ENV{EXTENDED_TESTING} | ||
| 576 | && !$ENV{NDEBUG} | ||
| 577 | && !$ENV{PERL_NDEBUG} | ||
| 578 | ) ? 1 : undef; | ||
| 579 | |||
| 580 | =item I<TRUE> | ||
| 581 | |||
| 582 | =item I<FALSE> | ||
| 583 | |||
| 584 | use constant { | ||
| 585 | TRUE => !! 1, | ||
| 586 | FALSE => !! '', | ||
| 587 | }; | ||
| 588 | |||
| 589 | Defines C<TRUE> and C<FALSE> constants. | ||
| 590 | |||
| 591 | I<See also>: L<constant::boolean> | ||
| 592 | |||
| 593 | =cut | ||
| 594 | |||
| 595 | use constant { | ||
| 596 | TRUE => !! 1, | ||
| 597 | FALSE => !! '', | ||
| 598 | }; | ||
| 599 | |||
| 600 | =item I<DefaultConsoleBufferSize> | ||
| 601 | |||
| 602 | use constant DefaultConsoleBufferSize => 256; | ||
| 603 | |||
| 604 | Defines the standard console buffer size. | ||
| 605 | |||
| 606 | =cut | ||
| 607 | |||
| 608 | use constant DefaultConsoleBufferSize => 256; | ||
| 609 | |||
| 610 | =item I<MinBeepFrequency> | ||
| 611 | |||
| 612 | =item I<MaxBeepFrequency> | ||
| 613 | |||
| 614 | use constant { | ||
| 615 | MinBeepFrequency => 0x25, | ||
| 616 | MaxBeepFrequency => 0x7fff, | ||
| 617 | }; | ||
| 618 | |||
| 619 | Beep range - see MSDN. | ||
| 620 | |||
| 621 | =cut | ||
| 622 | |||
| 623 | use constant { | ||
| 624 | MinBeepFrequency => 0x25, | ||
| 625 | MaxBeepFrequency => 0x7fff, | ||
| 626 | }; | ||
| 627 | |||
| 628 | =item I<MaxConsoleTitleLength> | ||
| 629 | |||
| 630 | use constant MaxConsoleTitleLength => 24500; | ||
| 631 | |||
| 632 | MSDN says console titles can be up to 64 KB in length. | ||
| 633 | But I get an exception if I use buffer lengths longer than | ||
| 634 | ~24500 Unicode characters. Oh well. | ||
| 635 | |||
| 636 | =cut | ||
| 637 | |||
| 638 | use constant MaxConsoleTitleLength => 24500; | ||
| 639 | |||
| 640 | =item I<StdConUnicodeEncoding> | ||
| 641 | |||
| 642 | use constant StdConUnicodeEncoding => Encode::find_encoding('UTF-16'); | ||
| 643 | |||
| 644 | The value corresponds to the Windows code pages 1200 (little endian byte | ||
| 645 | order) or 1201 (big endian byte order). | ||
| 646 | |||
| 647 | =cut | ||
| 648 | |||
| 649 | use constant StdConUnicodeEncoding => Encode::find_encoding('cp120' . | ||
| 650 | ($Config{byteorder} & 0b1)); | ||
| 651 | |||
| 652 | =back | ||
| 653 | |||
| 654 | =end private | ||
| 655 | |||
| 656 | =over | ||
| 657 | |||
| 658 | =item I<WinError.h> | ||
| 659 | |||
| 660 | use constant Win32Native::ERROR_INVALID_HANDLE => 0x6; | ||
| 661 | |||
| 662 | C<ERROR_INVALID_HANDLE> is a predefined constant that is used to represent a | ||
| 663 | value that is passed to or returned by one or more built-in functions. | ||
| 664 | |||
| 665 | =cut | ||
| 666 | |||
| 667 | sub Win32Native::ERROR_INVALID_HANDLE() { 0x6 }; | ||
| 668 | |||
| 669 | =begin private | ||
| 670 | |||
| 671 | =item I<Winuser.h> | ||
| 672 | |||
| 673 | use constant { | ||
| 674 | AltVKCode => 0x12, | ||
| 675 | NumberLockVKCode => 0x90, | ||
| 676 | CapsLockVKCode => 0x14, | ||
| 677 | }; | ||
| 678 | |||
| 679 | use constant { | ||
| 680 | VK_CLEAR => 0x0c, | ||
| 681 | VK_SHIFT => 0x10, | ||
| 682 | VK_PRIOR => 0x21, | ||
| 683 | VK_NEXT => 0x22, | ||
| 684 | VK_INSERT => 0x2d, | ||
| 685 | VK_NUMPAD0 => 0x60, | ||
| 686 | VK_NUMPAD9 => 0x69, | ||
| 687 | VK_SCROLL => 0x91, | ||
| 688 | }; | ||
| 689 | |||
| 690 | Virtual-Key Codes from I<Winuser.h> | ||
| 691 | |||
| 692 | =cut | ||
| 693 | |||
| 694 | use constant { | ||
| 695 | AltVKCode => 0x12, | ||
| 696 | NumberLockVKCode => 0x90, # virtual key code | ||
| 697 | CapsLockVKCode => 0x14, | ||
| 698 | }; | ||
| 699 | |||
| 700 | use constant { | ||
| 701 | VK_CLEAR => 0x0c, | ||
| 702 | VK_SHIFT => 0x10, | ||
| 703 | VK_PRIOR => 0x21, | ||
| 704 | VK_NEXT => 0x22, | ||
| 705 | VK_INSERT => 0x2d, | ||
| 706 | VK_NUMPAD0 => 0x60, | ||
| 707 | VK_NUMPAD9 => 0x69, | ||
| 708 | VK_SCROLL => 0x91, | ||
| 709 | }; | ||
| 710 | |||
| 711 | =end private | ||
| 712 | |||
| 713 | =item I<WinCon.h> | ||
| 714 | |||
| 715 | use constant Win32Native::KEY_EVENT => 0x0001; | ||
| 716 | |||
| 717 | The Event member contains a I<KEY_EVENT_RECORD> structure with information | ||
| 718 | about a keyboard event. | ||
| 719 | |||
| 720 | =cut | ||
| 721 | |||
| 722 | sub Win32Native::KEY_EVENT() { 0x0001 }; | ||
| 723 | |||
| 724 | =begin private | ||
| 725 | |||
| 726 | use constant { | ||
| 727 | eventType => 0, | ||
| 728 | keyDown => 1, | ||
| 729 | repeatCount => 2, | ||
| 730 | virtualKeyCode => 3, | ||
| 731 | virtualScanCode => 4, | ||
| 732 | uChar => 5, | ||
| 733 | controlKeyState => 6, | ||
| 734 | }; | ||
| 735 | |||
| 736 | Constants for accessing the input event array which is used for the console | ||
| 737 | input buffer API calls. | ||
| 738 | |||
| 739 | I<See also>: I<KEY_EVENT_RECORD> structure. | ||
| 740 | |||
| 741 | =cut | ||
| 742 | |||
| 743 | use constant { | ||
| 744 | eventType => 0, | ||
| 745 | keyDown => 1, | ||
| 746 | repeatCount => 2, | ||
| 747 | virtualKeyCode => 3, | ||
| 748 | virtualScanCode => 4, | ||
| 749 | uChar => 5, | ||
| 750 | controlKeyState => 6, | ||
| 751 | }; | ||
| 752 | |||
| 753 | =pod | ||
| 754 | |||
| 755 | use constant { | ||
| 756 | dwSizeX => 0, | ||
| 757 | dwSizeY => 1, | ||
| 758 | dwCursorPositionX => 2, | ||
| 759 | dwCursorPositionY => 3, | ||
| 760 | wAttributes => 4, | ||
| 761 | srWindowLeft => 5, | ||
| 762 | srWindowTop => 6, | ||
| 763 | srWindowRight => 7, | ||
| 764 | srWindowBottom => 8, | ||
| 765 | dwMaximumWindowSizeX => 9, | ||
| 766 | dwMaximumWindowSizeY => 10, | ||
| 767 | }; | ||
| 768 | |||
| 769 | Constants for accessing the console screen buffer array which contains | ||
| 770 | information about a console screen buffer. | ||
| 771 | |||
| 772 | I<See also>: I<CONSOLE_SCREEN_BUFFER_INFO> structure. | ||
| 773 | |||
| 774 | =cut | ||
| 775 | |||
| 776 | use constant { | ||
| 777 | dwSizeX => 0, | ||
| 778 | dwSizeY => 1, | ||
| 779 | dwCursorPositionX => 2, | ||
| 780 | dwCursorPositionY => 3, | ||
| 781 | wAttributes => 4, | ||
| 782 | srWindowLeft => 5, | ||
| 783 | srWindowTop => 6, | ||
| 784 | srWindowRight => 7, | ||
| 785 | srWindowBottom => 8, | ||
| 786 | dwMaximumWindowSizeX => 9, | ||
| 787 | dwMaximumWindowSizeY => 10, | ||
| 788 | }; | ||
| 789 | |||
| 790 | =pod | ||
| 791 | |||
| 792 | use constant { | ||
| 793 | dwSize => 0, | ||
| 794 | bVisible => 1, | ||
| 795 | }; | ||
| 796 | |||
| 797 | Constants for accessing the console cursor info array which contains information | ||
| 798 | about the console cursor. | ||
| 799 | |||
| 800 | I<See also>: I<CONSOLE_CURSOR_INFO> structure. | ||
| 801 | |||
| 802 | =cut | ||
| 803 | |||
| 804 | use constant { | ||
| 805 | dwSize => 0, | ||
| 806 | bVisible => 1, | ||
| 807 | }; | ||
| 808 | |||
| 809 | =end private | ||
| 810 | |||
| 811 | =back | ||
| 812 | |||
| 813 | =cut | ||
| 814 | |||
| 815 | use namespace::clean; | ||
| 816 | |||
| 817 | # ------------------------------------------------------------------------ | ||
| 818 | # Variables -------------------------------------------------------------- | ||
| 819 | # ------------------------------------------------------------------------ | ||
| 820 | |||
| 821 | =begin private | ||
| 822 | |||
| 823 | =head2 Variables | ||
| 824 | |||
| 825 | =over | ||
| 826 | |||
| 827 | =item <_instance> | ||
| 828 | |||
| 829 | my $_instance ( is => private, type => Object ); | ||
| 830 | |||
| 831 | The instance reference is stored in the C<$_instance> scalar. | ||
| 832 | |||
| 833 | =cut | ||
| 834 | |||
| 835 | my $_instance = undef; | ||
| 836 | |||
| 837 | |||
| 838 | =item I<_in> | ||
| 839 | |||
| 840 | my $_in ( is => private, type => FileHandle ); | ||
| 841 | |||
| 842 | For L</In> | ||
| 843 | |||
| 844 | =item I<_out> | ||
| 845 | |||
| 846 | my $_out ( is => private, type => FileHandle ); | ||
| 847 | |||
| 848 | For L</Out> | ||
| 849 | |||
| 850 | =item I<_error> | ||
| 851 | |||
| 852 | my $_error ( is => private, type => FileHandle ): | ||
| 853 | |||
| 854 | For L</Error> | ||
| 855 | |||
| 856 | =cut | ||
| 857 | |||
| 858 | my $_in; | ||
| 859 | my $_out; | ||
| 860 | my $_error; | ||
| 861 | |||
| 862 | =item I<_cachedInputRecord> | ||
| 863 | |||
| 864 | my $_cachedInputRecord ( is => private, type => ArrayRef ) = [(-1)]; | ||
| 865 | |||
| 866 | For L</ReadKey> | ||
| 867 | |||
| 868 | =cut | ||
| 869 | |||
| 870 | # ReadLine & Read can't use this because they need to use ReadFile | ||
| 871 | # to be able to handle redirected input. We have to accept that | ||
| 872 | # we will lose repeated keystrokes when someone switches from | ||
| 873 | # calling ReadKey to calling Read or ReadLine. Those methods should | ||
| 874 | # ideally flush this cache as well. | ||
| 875 | my $_cachedInputRecord = [(-1)]; | ||
| 876 | |||
| 877 | =item I<_haveReadDefaultColors> | ||
| 878 | |||
| 879 | my $_haveReadDefaultColors ( is => private, type => Bool ); | ||
| 880 | |||
| 881 | For L</ResetColor> | ||
| 882 | |||
| 883 | =cut | ||
| 884 | |||
| 885 | my $_haveReadDefaultColors; | ||
| 886 | |||
| 887 | =item I<_defaultColors> | ||
| 888 | |||
| 889 | my $_defaultColors ( is => private, type => Ref[Int] ); | ||
| 890 | |||
| 891 | Reference value of L<$ATTR_NORMAL|Win32::Console>, used for L</ResetColor>. | ||
| 892 | |||
| 893 | =cut | ||
| 894 | |||
| 895 | my $_defaultColors = \$ATTR_NORMAL; | ||
| 896 | |||
| 897 | =item I<_isOutTextWriterRedirected> | ||
| 898 | |||
| 899 | my $_isOutTextWriterRedirected ( is => private, type => Bool ) = FALSE; | ||
| 900 | |||
| 901 | For L</OutputEncoding> | ||
| 902 | |||
| 903 | =item I<_isErrorTextWriterRedirected> | ||
| 904 | |||
| 905 | my $_isErrorTextWriterRedirected ( is => private, type => Bool ) = FALSE; | ||
| 906 | |||
| 907 | For L</OutputEncoding> | ||
| 908 | |||
| 909 | =cut | ||
| 910 | |||
| 911 | my $_isOutTextWriterRedirected = FALSE; | ||
| 912 | my $_isErrorTextWriterRedirected = FALSE; | ||
| 913 | |||
| 914 | =item I<_inputEncoding> | ||
| 915 | |||
| 916 | my $_inputEncoding ( is => private, type => Int ); | ||
| 917 | |||
| 918 | For L</InputEncoding> | ||
| 919 | |||
| 920 | =item I<_outputEncoding> | ||
| 921 | |||
| 922 | my $_outputEncoding ( is => private, type => Int ); | ||
| 923 | |||
| 924 | For L</OutputEncoding> | ||
| 925 | |||
| 926 | =cut | ||
| 927 | |||
| 928 | my $_inputEncoding; | ||
| 929 | my $_outputEncoding; | ||
| 930 | |||
| 931 | =item I<_stdInRedirectQueried> | ||
| 932 | |||
| 933 | my $_stdInRedirectQueried ( is => private, type => Bool ) = FALSE; | ||
| 934 | |||
| 935 | For L</IsInputRedirected> | ||
| 936 | |||
| 937 | =item I<_stdOutRedirectQueried> | ||
| 938 | |||
| 939 | my $_stdOutRedirectQueried ( is => private, type => Bool ) = FALSE; | ||
| 940 | |||
| 941 | For L</IsOutputRedirected> | ||
| 942 | |||
| 943 | =item I<_stdErrRedirectQueried> | ||
| 944 | |||
| 945 | my $_stdErrRedirectQueried ( is => private, type => Bool ) = FALSE; | ||
| 946 | |||
| 947 | For L</IsErrorRedirected> | ||
| 948 | |||
| 949 | =cut | ||
| 950 | |||
| 951 | my $_stdInRedirectQueried = FALSE; | ||
| 952 | my $_stdOutRedirectQueried = FALSE; | ||
| 953 | my $_stdErrRedirectQueried = FALSE; | ||
| 954 | |||
| 955 | =item I<_isStdInRedirected> | ||
| 956 | |||
| 957 | my $_isStdInRedirected ( is => private, type => Bool ); | ||
| 958 | |||
| 959 | For L</IsInputRedirected> | ||
| 960 | |||
| 961 | =item I<_isStdOutRedirected> | ||
| 962 | |||
| 963 | my $_isStdOutRedirected ( is => private, type => Bool ); | ||
| 964 | |||
| 965 | For L</IsOutputRedirected> | ||
| 966 | |||
| 967 | =item I<_isStdErrRedirected> | ||
| 968 | |||
| 969 | my $_isStdErrRedirected ( is => private, type => Bool ); | ||
| 970 | |||
| 971 | For L</IsErrorRedirected> | ||
| 972 | |||
| 973 | =cut | ||
| 974 | |||
| 975 | my $_isStdInRedirected; | ||
| 976 | my $_isStdOutRedirected; | ||
| 977 | my $_isStdErrRedirected; | ||
| 978 | |||
| 979 | =item I<InternalSyncObject> | ||
| 980 | |||
| 981 | my $InternalSyncObject ( is => private, type => Any ); | ||
| 982 | |||
| 983 | Private variable for locking instead of locking on a public type for SQL | ||
| 984 | reliability work. | ||
| 985 | |||
| 986 | Use this for internal synchronization during initialization, wiring up events, | ||
| 987 | or for short, non-blocking OS calls. | ||
| 988 | |||
| 989 | =item I<ReadKeySyncObject> | ||
| 990 | |||
| 991 | my $ReadKeySyncObject ( is => private, type => Any ); | ||
| 992 | |||
| 993 | Use this for blocking in Console->ReadKey, which needs to protect itself in | ||
| 994 | case multiple threads call it simultaneously. | ||
| 995 | |||
| 996 | Use a L</ReadKey>-specific lock though, to allow other fields to be | ||
| 997 | initialized on this type. | ||
| 998 | |||
| 999 | =cut | ||
| 1000 | |||
| 1001 | my $InternalSyncObject :shared; | ||
| 1002 | my $ReadKeySyncObject :shared; | ||
| 1003 | |||
| 1004 | =item I<_consoleInputHandle> | ||
| 1005 | |||
| 1006 | my $_consoleInputHandle ( is => private, type => Int ); | ||
| 1007 | |||
| 1008 | Holds the output handle of the console. | ||
| 1009 | |||
| 1010 | =item I<_consoleOutputHandle> | ||
| 1011 | |||
| 1012 | my $_consoleOutputHandle ( is => private, type => Int ); | ||
| 1013 | |||
| 1014 | Holds the input handle of the console. | ||
| 1015 | |||
| 1016 | =cut | ||
| 1017 | |||
| 1018 | # About reliability: I'm not using SafeHandle here. We don't | ||
| 1019 | # need to close these handles, and we don't allow the user to close | ||
| 1020 | # them so we don't have many of the security problems inherent in | ||
| 1021 | # something like file handles. Additionally, in a host like SQL | ||
| 1022 | # Server, we won't have a console. | ||
| 1023 | my $_consoleInputHandle; | ||
| 1024 | my $_consoleOutputHandle; | ||
| 1025 | |||
| 1026 | =item I<_leaveOpen> | ||
| 1027 | |||
| 1028 | my $_leaveOpen ( is => private, type => HashRef ) = {}; | ||
| 1029 | |||
| 1030 | If a file handle needs to be protected against automatic closing (when leaving | ||
| 1031 | the scope), the associated parameter C<$ownsHandle> is set to I<false> when | ||
| 1032 | L</SafeFileHandle> is called. | ||
| 1033 | |||
| 1034 | To leave the file handle open, we save the L<IO::Handle> object in this hash | ||
| 1035 | so that the C<REFCNT> is C<< > 0 >>. | ||
| 1036 | |||
| 1037 | =cut | ||
| 1038 | |||
| 1039 | my $_leaveOpen = {}; | ||
| 1040 | |||
| 1041 | =item I<ResourceString> | ||
| 1042 | |||
| 1043 | my %ResourceString ( is => private, type => Hash ) = (...); | ||
| 1044 | |||
| 1045 | This hash variable contains all resource strings that are used here in this | ||
| 1046 | package. | ||
| 1047 | |||
| 1048 | =cut | ||
| 1049 | |||
| 1050 | my %ResourceString = ( | ||
| 1051 | ArgumentNullException => | ||
| 1052 | "Value cannot be null. Parameter name: %s", | ||
| 1053 | Arg_InvalidConsoleColor => | ||
| 1054 | "The ConsoleColor enum value was not defined on that enum. Please ". | ||
| 1055 | "use a defined color from the enum.", | ||
| 1056 | ArgumentOutOfRange_BeepFrequency => | ||
| 1057 | "Console->Beep's frequency must be between between %d and %d.", | ||
| 1058 | ArgumentOutOfRange_ConsoleBufferBoundaries => | ||
| 1059 | "The value must be greater than or equal to zero and less than the ". | ||
| 1060 | "console's buffer size in that dimension.", | ||
| 1061 | ArgumentOutOfRange_ConsoleBufferLessThanWindowSize => | ||
| 1062 | "The console buffer size must not be less than the current size and ". | ||
| 1063 | "position of the console window, nor greater than or equal to 32767.", | ||
| 1064 | ArgumentOutOfRange_CursorSize => | ||
| 1065 | "The cursor size is invalid. It must be a percentage between 1 and 100.", | ||
| 1066 | ArgumentOutOfRange_ConsoleTitleTooLong | ||
| 1067 | => "The console title is too long.", | ||
| 1068 | ArgumentOutOfRange_ConsoleWindowBufferSize => | ||
| 1069 | "The new console window size would force the console buffer size to be ". | ||
| 1070 | "too large.", | ||
| 1071 | ArgumentOutOfRange_ConsoleWindowPos => | ||
| 1072 | "The window position must be set such that the current window size fits ". | ||
| 1073 | "within the console's buffer, and the numbers must not be negative.", | ||
| 1074 | ArgumentOutOfRange_ConsoleWindowSize_Size => | ||
| 1075 | "The value must be less than the console's current maximum window size ". | ||
| 1076 | "of %d in that dimension. Note that this value depends on screen ". | ||
| 1077 | "resolution and the console font.", | ||
| 1078 | ArgumentOutOfRange_NeedPosNum => | ||
| 1079 | "Positive number required.", | ||
| 1080 | ArgumentOutOfRange_NeedNonNegNum => | ||
| 1081 | "Non-negative number required.", | ||
| 1082 | InvalidOperation_ConsoleKeyAvailableOnFile => | ||
| 1083 | "Cannot see if a key has been pressed when either application does not ". | ||
| 1084 | "have a console or when console input has been redirected from a file.", | ||
| 1085 | InvalidOperation_ConsoleReadKeyOnFile => | ||
| 1086 | "Cannot read keys when either application does not have a console or ". | ||
| 1087 | "when console input has been redirected. Try Console->Read.", | ||
| 1088 | IO_NoConsole => | ||
| 1089 | "There is no console.", | ||
| 1090 | ); | ||
| 1091 | |||
| 1092 | =back | ||
| 1093 | |||
| 1094 | =end private | ||
| 1095 | |||
| 1096 | =cut | ||
| 1097 | |||
| 1098 | # ------------------------------------------------------------------------ | ||
| 1099 | # Attributes ------------------------------------------------------------- | ||
| 1100 | # ------------------------------------------------------------------------ | ||
| 1101 | |||
| 1102 | =head2 Attributes | ||
| 1103 | |||
| 1104 | =over | ||
| 1105 | |||
| 1106 | =item I<BackgroundColor> | ||
| 1107 | |||
| 1108 | field BackgroundColor ( is => rw, type => Int, | ||
| 1109 | default => $Win32::Console::BG_BLACK >> 4 ); | ||
| 1110 | |||
| 1111 | A Color that specifies the background color of the console; that is, the color | ||
| 1112 | that appears behind each character. The default is black. | ||
| 1113 | |||
| 1114 | I<Throws>: I<ArgumentException> if the color specified in a set operation is not | ||
| 1115 | valid. | ||
| 1116 | |||
| 1117 | =cut | ||
| 1118 | |||
| 1119 | sub BackgroundColor { | ||
| 1120 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 1121 | my $self = assert_Object shift; | ||
| 1122 | goto SET if @_; | ||
| 1123 | GET: { | ||
| 1124 | my $succeeded; | ||
| 1125 | my $csbi = GetBufferInfo(FALSE, \$succeeded); | ||
| 1126 | |||
| 1127 | # For code that may be used from Windows app w/ no console | ||
| 1128 | if ( !$succeeded ) { | ||
| 1129 | my $BLACK = ($BG_BLACK & 0xf0) >> 4; | ||
| 1130 | return $BLACK; | ||
| 1131 | } | ||
| 1132 | |||
| 1133 | my $c = $csbi->{wAttributes} & 0xf0; | ||
| 1134 | my $value = ColorAttributeToConsoleColor($c); | ||
| 1135 | return assert_Int $value; | ||
| 1136 | } | ||
| 1137 | SET: { | ||
| 1138 | my $value = assert_Int shift; | ||
| 1139 | if ( $value < 0 || $value > 15 ) { | ||
| 1140 | confess("ArgumentException:\n". | ||
| 1141 | "$ResourceString{Arg_InvalidConsoleColor}\n"); | ||
| 1142 | } | ||
| 1143 | my $c = ConsoleColorToColorAttribute($value, TRUE); | ||
| 1144 | |||
| 1145 | my $succeeded; | ||
| 1146 | my $csbi = GetBufferInfo(FALSE, \$succeeded); | ||
| 1147 | # For code that may be used from Windows app w/ no console | ||
| 1148 | return if !$succeeded; | ||
| 1149 | |||
| 1150 | assert ( $_haveReadDefaultColors | ||
| 1151 | or ~- warn "Setting the background color before we've read the ". | ||
| 1152 | "default background color!" | ||
| 1153 | ); | ||
| 1154 | |||
| 1155 | my $attr = $csbi->{wAttributes}; | ||
| 1156 | $attr &= ~0xf0; | ||
| 1157 | # Perl's bitwise-or. | ||
| 1158 | $attr = $attr | $c; | ||
| 1159 | # Ignore errors here - there are some scenarios for running code that | ||
| 1160 | # wants to print in colors to the console in a Windows application. | ||
| 1161 | Win32::Console::_SetConsoleTextAttribute(ConsoleOutputHandle(), $attr); | ||
| 1162 | return; | ||
| 1163 | } | ||
| 1164 | }; | ||
| 1165 | |||
| 1166 | =item I<BufferHeight> | ||
| 1167 | |||
| 1168 | field BufferHeight ( is => rw, type => Int ); | ||
| 1169 | |||
| 1170 | The current height, in rows, of the buffer area. | ||
| 1171 | |||
| 1172 | I<Throws>: I<ArgumentOutOfRangeException> if the value in a set operation is | ||
| 1173 | less than or equal to zero or greater than or equal to C<0x7fff> or less than | ||
| 1174 | L</WindowTop> + L</WindowHeight>. | ||
| 1175 | |||
| 1176 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 1177 | |||
| 1178 | =cut | ||
| 1179 | |||
| 1180 | sub BufferHeight { | ||
| 1181 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 1182 | my $self = assert_Object shift; | ||
| 1183 | goto SET if @_; | ||
| 1184 | GET: { | ||
| 1185 | my $csbi = GetBufferInfo(); | ||
| 1186 | my $value = $csbi->{dwSize}->{Y}; | ||
| 1187 | return assert_Int $value; | ||
| 1188 | } | ||
| 1189 | SET: { | ||
| 1190 | my $value = assert_Int shift; | ||
| 1191 | $self->SetBufferSize($self->BufferWidth, $value); | ||
| 1192 | return; | ||
| 1193 | } | ||
| 1194 | }; | ||
| 1195 | |||
| 1196 | =item I<BufferWidth> | ||
| 1197 | |||
| 1198 | field BufferWidth ( is => rw, type => Int ); | ||
| 1199 | |||
| 1200 | The current width, in columns, of the buffer area. | ||
| 1201 | |||
| 1202 | I<Throws>: I<ArgumentOutOfRangeException> if the value in a set operation is | ||
| 1203 | less than or equal to zero or greater than or equal to C<0x7fff> or less than | ||
| 1204 | L</WindowLeft> + L</WindowWidth>. | ||
| 1205 | |||
| 1206 | =cut | ||
| 1207 | |||
| 1208 | sub BufferWidth { | ||
| 1209 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 1210 | my $self = assert_Object shift; | ||
| 1211 | goto SET if @_; | ||
| 1212 | GET: { | ||
| 1213 | my $csbi = GetBufferInfo(); | ||
| 1214 | my $value = $csbi->{dwSize}->{X}; | ||
| 1215 | return assert_Int $value; | ||
| 1216 | } | ||
| 1217 | SET: { | ||
| 1218 | my $value = assert_Int shift; | ||
| 1219 | $self->SetBufferSize($value, $self->BufferHeight); | ||
| 1220 | return; | ||
| 1221 | } | ||
| 1222 | }; | ||
| 1223 | |||
| 1224 | =item I<CapsLock> | ||
| 1225 | |||
| 1226 | field CapsLock ( is => ro, type => Bool ); | ||
| 1227 | |||
| 1228 | Gets a value indicating whether the C<CAPS LOCK> keyboard toggle is turned on | ||
| 1229 | or turned off. | ||
| 1230 | |||
| 1231 | =cut | ||
| 1232 | |||
| 1233 | sub CapsLock { | ||
| 1234 | assert ( @_ == 1 ); | ||
| 1235 | my $self = assert_Object shift; | ||
| 1236 | GET: { | ||
| 1237 | require Win32Native; | ||
| 1238 | my $value = (Win32Native::GetKeyState(CapsLockVKCode) & 1) == 1; | ||
| 1239 | return assert_Bool $value; | ||
| 1240 | } | ||
| 1241 | }; | ||
| 1242 | |||
| 1243 | =item I<CursorLeft> | ||
| 1244 | |||
| 1245 | field CursorLeft ( is => rw, type => Int ); | ||
| 1246 | |||
| 1247 | The column position of the cursor within the buffer area. | ||
| 1248 | |||
| 1249 | I<Throws>: I<ArgumentOutOfRangeException> if the value in a set operation is | ||
| 1250 | less than zero or greater than or equal to L</BufferWidth>. | ||
| 1251 | |||
| 1252 | =cut | ||
| 1253 | |||
| 1254 | sub CursorLeft { | ||
| 1255 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 1256 | my $self = assert_Object shift; | ||
| 1257 | goto SET if @_; | ||
| 1258 | GET: { | ||
| 1259 | my $csbi = GetBufferInfo(); | ||
| 1260 | my $value = $csbi->{dwCursorPosition}->{X}; | ||
| 1261 | return assert_Int $value; | ||
| 1262 | } | ||
| 1263 | SET: { | ||
| 1264 | my $value = assert_Int shift; | ||
| 1265 | $self->SetCursorPosition($value, $self->CursorTop); | ||
| 1266 | return; | ||
| 1267 | } | ||
| 1268 | }; | ||
| 1269 | |||
| 1270 | =item I<CursorSize> | ||
| 1271 | |||
| 1272 | field CursorSize ( is => rw, type => Int ); | ||
| 1273 | |||
| 1274 | The height of the cursor within a character cell. | ||
| 1275 | |||
| 1276 | The size of the cursor expressed as a percentage of the height of a character | ||
| 1277 | cell. The property value ranges from C<1> to C<100>. | ||
| 1278 | |||
| 1279 | I<Throws>: I<ArgumentOutOfRangeException> if the value specified in a set | ||
| 1280 | operation is less than C<1> or greater than C<100>. | ||
| 1281 | |||
| 1282 | =cut | ||
| 1283 | |||
| 1284 | sub CursorSize { | ||
| 1285 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 1286 | my $self = assert_Object shift; | ||
| 1287 | goto SET if @_; | ||
| 1288 | GET: { | ||
| 1289 | my @cci; | ||
| 1290 | my $hConsole = ConsoleOutputHandle(); | ||
| 1291 | my $r = do { | ||
| 1292 | @cci = Win32::Console::_GetConsoleCursorInfo($hConsole); | ||
| 1293 | @cci > 1; | ||
| 1294 | }; | ||
| 1295 | if ( !$r ) { | ||
| 1296 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n") | ||
| 1297 | } | ||
| 1298 | my $value = $cci[dwSize]; | ||
| 1299 | return assert_Int $value; | ||
| 1300 | } | ||
| 1301 | SET: { | ||
| 1302 | my $value = assert_Int shift; | ||
| 1303 | if ( $value < 1 || $value > 100 ) { | ||
| 1304 | confess("ArgumentOutOfRangeException: value $value\n". | ||
| 1305 | "$ResourceString{ArgumentOutOfRange_CursorSize}\n"); | ||
| 1306 | } | ||
| 1307 | |||
| 1308 | my @cci; | ||
| 1309 | my $hConsole = ConsoleOutputHandle(); | ||
| 1310 | my $r = do { | ||
| 1311 | @cci = Win32::Console::_GetConsoleCursorInfo($hConsole); | ||
| 1312 | @cci > 1; | ||
| 1313 | }; | ||
| 1314 | if ( !$r ) { | ||
| 1315 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n") | ||
| 1316 | } | ||
| 1317 | |||
| 1318 | $cci[dwSize] = $value; | ||
| 1319 | $r = Win32::Console::_SetConsoleCursorInfo($hConsole, @cci); | ||
| 1320 | if ( !$r ) { | ||
| 1321 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n") | ||
| 1322 | } | ||
| 1323 | return; | ||
| 1324 | } | ||
| 1325 | }; | ||
| 1326 | |||
| 1327 | =item I<CursorTop> | ||
| 1328 | |||
| 1329 | field CursorTop ( is => rw, type => Int ); | ||
| 1330 | |||
| 1331 | The row position of the cursor within the buffer area. | ||
| 1332 | |||
| 1333 | I<Throws>: I<ArgumentOutOfRangeException> if the value in a set operation is | ||
| 1334 | less than zero or greater than or equal to L</BufferHeight>. | ||
| 1335 | |||
| 1336 | =cut | ||
| 1337 | |||
| 1338 | sub CursorTop { | ||
| 1339 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 1340 | my $self = assert_Object shift; | ||
| 1341 | goto SET if @_; | ||
| 1342 | GET: { | ||
| 1343 | my $csbi = GetBufferInfo(); | ||
| 1344 | my $value = $csbi->{dwCursorPosition}->{Y}; | ||
| 1345 | return assert_Int $value; | ||
| 1346 | } | ||
| 1347 | SET: { | ||
| 1348 | my $value = assert_Int shift; | ||
| 1349 | $self->SetCursorPosition($self->CursorLeft, $value); | ||
| 1350 | return; | ||
| 1351 | } | ||
| 1352 | }; | ||
| 1353 | |||
| 1354 | =item I<CursorVisible> | ||
| 1355 | |||
| 1356 | field CursorVisible ( is => rw, type => Bool ); | ||
| 1357 | |||
| 1358 | The attribute indicating whether the cursor is visible. | ||
| 1359 | |||
| 1360 | I<True> if the cursor is visible; otherwise, I<false>. | ||
| 1361 | |||
| 1362 | =cut | ||
| 1363 | |||
| 1364 | sub CursorVisible { | ||
| 1365 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 1366 | my $self = assert_Object shift; | ||
| 1367 | goto SET if @_; | ||
| 1368 | GET: { | ||
| 1369 | my @cci; | ||
| 1370 | my $hConsole = ConsoleOutputHandle(); | ||
| 1371 | my $r = do { | ||
| 1372 | @cci = Win32::Console::_GetConsoleCursorInfo($hConsole); | ||
| 1373 | @cci > 1; | ||
| 1374 | }; | ||
| 1375 | if ( !$r ) { | ||
| 1376 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n") | ||
| 1377 | } | ||
| 1378 | my $value = $cci[bVisible]; | ||
| 1379 | return assert_Bool $value; | ||
| 1380 | } | ||
| 1381 | SET: { | ||
| 1382 | my $value = assert_Bool shift; | ||
| 1383 | |||
| 1384 | my @cci; | ||
| 1385 | my $hConsole = ConsoleOutputHandle(); | ||
| 1386 | my $r = do { | ||
| 1387 | @cci = Win32::Console::_GetConsoleCursorInfo($hConsole); | ||
| 1388 | @cci > 1; | ||
| 1389 | }; | ||
| 1390 | if ( !$r ) { | ||
| 1391 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n") | ||
| 1392 | } | ||
| 1393 | |||
| 1394 | $cci[bVisible] = $value; | ||
| 1395 | $r = Win32::Console::_SetConsoleCursorInfo($hConsole, @cci); | ||
| 1396 | if ( !$r ) { | ||
| 1397 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n") | ||
| 1398 | } | ||
| 1399 | return; | ||
| 1400 | } | ||
| 1401 | }; | ||
| 1402 | |||
| 1403 | =item I<Error> | ||
| 1404 | |||
| 1405 | field Error ( is => ro, type => FileHandle ); | ||
| 1406 | |||
| 1407 | A I<FileHandle> that represents the standard error stream. | ||
| 1408 | |||
| 1409 | =cut | ||
| 1410 | |||
| 1411 | sub Error { | ||
| 1412 | assert ( @_ == 1 ); | ||
| 1413 | my $self = assert_Object shift; | ||
| 1414 | GET: { | ||
| 1415 | if ( !defined $_error ) { | ||
| 1416 | InitializeStdOutError(FALSE); | ||
| 1417 | } | ||
| 1418 | return $_error; | ||
| 1419 | } | ||
| 1420 | }; | ||
| 1421 | |||
| 1422 | =item I<ForegroundColor> | ||
| 1423 | |||
| 1424 | field ForegroundColor ( is => rw, type => Int, | ||
| 1425 | default => $Win32::Console::FG_LIGHTGRAY); | ||
| 1426 | |||
| 1427 | Color that specifies the foreground color of the console; that is, the color | ||
| 1428 | of each character that is displayed. The default is gray. | ||
| 1429 | |||
| 1430 | I<Throws>: I<ArgumentException> if the color specified in a set operation is not | ||
| 1431 | valid. | ||
| 1432 | |||
| 1433 | =cut | ||
| 1434 | |||
| 1435 | sub ForegroundColor { | ||
| 1436 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 1437 | my $self = assert_Object shift; | ||
| 1438 | goto SET if @_; | ||
| 1439 | GET: { | ||
| 1440 | my $succeeded; | ||
| 1441 | my $csbi = GetBufferInfo(FALSE, \$succeeded); | ||
| 1442 | |||
| 1443 | # For code that may be used from Windows app w/ no console | ||
| 1444 | if ( !$succeeded ) { | ||
| 1445 | return $FG_LIGHTGRAY; | ||
| 1446 | } | ||
| 1447 | |||
| 1448 | my $c = $csbi->{wAttributes} & 0x0f; | ||
| 1449 | my $value = ColorAttributeToConsoleColor($c); | ||
| 1450 | return assert_Int $value; | ||
| 1451 | } | ||
| 1452 | SET: { | ||
| 1453 | my $value = assert_Int shift; | ||
| 1454 | if ( $value < 0 || $value > 15 ) { | ||
| 1455 | confess("ArgumentException:\n". | ||
| 1456 | "$ResourceString{Arg_InvalidConsoleColor}\n"); | ||
| 1457 | } | ||
| 1458 | my $c = ConsoleColorToColorAttribute($value, FALSE); | ||
| 1459 | |||
| 1460 | my $succeeded; | ||
| 1461 | my $csbi = GetBufferInfo(FALSE, \$succeeded); | ||
| 1462 | # For code that may be used from Windows app w/ no console | ||
| 1463 | return if !$succeeded; | ||
| 1464 | |||
| 1465 | assert ( $_haveReadDefaultColors | ||
| 1466 | or ~- warn "Setting the foreground color before we've read the ". | ||
| 1467 | "default foreground color!" | ||
| 1468 | ); | ||
| 1469 | |||
| 1470 | my $attr = $csbi->{wAttributes}; | ||
| 1471 | $attr &= ~0x0f; | ||
| 1472 | # Perl's bitwise-or. | ||
| 1473 | $attr = $attr | $c; | ||
| 1474 | # Ignore errors here - there are some scenarios for running code that | ||
| 1475 | # wants to print in colors to the console in a Windows application. | ||
| 1476 | Win32::Console::_SetConsoleTextAttribute(ConsoleOutputHandle(), $attr); | ||
| 1477 | return; | ||
| 1478 | } | ||
| 1479 | }; | ||
| 1480 | |||
| 1481 | =item I<In> | ||
| 1482 | |||
| 1483 | field In ( is => ro, type => FileHandle ); | ||
| 1484 | |||
| 1485 | A I<FileHandle> that represents the standard input stream. | ||
| 1486 | |||
| 1487 | =cut | ||
| 1488 | |||
| 1489 | sub In { | ||
| 1490 | assert ( @_ == 1 ); | ||
| 1491 | my $self = assert_Object shift; | ||
| 1492 | GET: { | ||
| 1493 | # Because most applications don't use stdin, we can delay | ||
| 1494 | # initialize it slightly better startup performance. | ||
| 1495 | if ( !defined $_in ) { | ||
| 1496 | lock($InternalSyncObject); | ||
| 1497 | # Set up Console->In | ||
| 1498 | my $s = __PACKAGE__->OpenStandardInput(); | ||
| 1499 | my $reader; | ||
| 1500 | if ( !$s ) { | ||
| 1501 | $reader = IO::Null->new(); | ||
| 1502 | } else { | ||
| 1503 | my $enc = __PACKAGE__->InputEncoding; | ||
| 1504 | my $cpi = $enc->$CodePage() || Win32::GetConsoleCP(); | ||
| 1505 | $reader = IO::File->new_from_fd(fileno($s), 'r'); | ||
| 1506 | $reader->binmode(":encoding(cp$cpi)"); | ||
| 1507 | } | ||
| 1508 | $_in = assert_FileHandle $reader; | ||
| 1509 | } | ||
| 1510 | return $_in; | ||
| 1511 | } | ||
| 1512 | }; | ||
| 1513 | |||
| 1514 | =item I<InputEncoding> | ||
| 1515 | |||
| 1516 | field InputEncoding ( is => rw, type => Object ); | ||
| 1517 | class_has InputEncoding ( is => rw, isa => Object, init_arg => undef ); | ||
| 1518 | |||
| 1519 | Gets or sets the encoding the console uses to write input. | ||
| 1520 | |||
| 1521 | I<Remarks>: A get operation may return a cached value instead of the console's | ||
| 1522 | current input encoding. | ||
| 1523 | |||
| 1524 | =cut | ||
| 1525 | |||
| 1526 | sub InputEncoding { | ||
| 1527 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 1528 | my $caller = shift; | ||
| 1529 | |||
| 1530 | assert ( $caller ); | ||
| 1531 | assert ( is_Object($caller) || is_ClassName($caller) ); | ||
| 1532 | |||
| 1533 | goto SET if @_; | ||
| 1534 | GET: { | ||
| 1535 | return $_inputEncoding | ||
| 1536 | if $_inputEncoding; | ||
| 1537 | |||
| 1538 | { | ||
| 1539 | lock($InternalSyncObject); | ||
| 1540 | |||
| 1541 | return $_inputEncoding | ||
| 1542 | if $_inputEncoding; | ||
| 1543 | |||
| 1544 | my $cp = Win32::GetConsoleCP() || Win32::GetACP(); | ||
| 1545 | $_inputEncoding = Encode::find_encoding("cp$cp"); | ||
| 1546 | return $_inputEncoding; | ||
| 1547 | } | ||
| 1548 | } | ||
| 1549 | SET: { | ||
| 1550 | if ( !defined $_[0] ) { | ||
| 1551 | confess("ArgumentNullException:\n". | ||
| 1552 | sprintf("$ResourceString{ArgumentNullException}\n", "value")); | ||
| 1553 | } | ||
| 1554 | my $value = assert_Object shift; | ||
| 1555 | |||
| 1556 | { | ||
| 1557 | lock($InternalSyncObject); | ||
| 1558 | |||
| 1559 | if ( !IsStandardConsoleUnicodeEncoding($value) ) { | ||
| 1560 | my $cp = $value->$CodePage(); | ||
| 1561 | my $r = Win32::SetConsoleCP($cp); | ||
| 1562 | if ( !$r ) { | ||
| 1563 | warn("WinIOError:\n$EXTENDED_OS_ERROR\n"); | ||
| 1564 | } | ||
| 1565 | } | ||
| 1566 | |||
| 1567 | $_inputEncoding = $value; | ||
| 1568 | |||
| 1569 | # We need to reinitialize Console->In in the next call to _in | ||
| 1570 | # This will discard the current FileHandle, potentially | ||
| 1571 | # losing buffered data | ||
| 1572 | $_in = undef; | ||
| 1573 | return; | ||
| 1574 | } | ||
| 1575 | } | ||
| 1576 | }; | ||
| 1577 | |||
| 1578 | =item I<IsErrorRedirected> | ||
| 1579 | |||
| 1580 | field IsErrorRedirected ( is => ro, type => Bool ); | ||
| 1581 | class_has IsErrorRedirected ( is => ro, type => Bool, init_arg => undef ); | ||
| 1582 | |||
| 1583 | Gets a value that indicates whether error has been redirected from the | ||
| 1584 | standard error stream. I<True> if error is redirected; otherwise, I<false>. | ||
| 1585 | |||
| 1586 | =cut | ||
| 1587 | |||
| 1588 | sub IsErrorRedirected { | ||
| 1589 | assert ( @_ == 1 ); | ||
| 1590 | my $caller = shift; | ||
| 1591 | |||
| 1592 | assert ( $caller ); | ||
| 1593 | assert ( is_Object($caller) || is_ClassName($caller) ); | ||
| 1594 | |||
| 1595 | GET: { | ||
| 1596 | return $_isStdErrRedirected | ||
| 1597 | if $_stdErrRedirectQueried; | ||
| 1598 | |||
| 1599 | { | ||
| 1600 | lock($InternalSyncObject); | ||
| 1601 | |||
| 1602 | return $_isStdErrRedirected | ||
| 1603 | if $_stdErrRedirectQueried; | ||
| 1604 | |||
| 1605 | my $errHndle = Win32::Console::_GetStdHandle(STD_ERROR_HANDLE); | ||
| 1606 | $_isStdErrRedirected = IsHandleRedirected($errHndle); | ||
| 1607 | $_stdErrRedirectQueried = TRUE; | ||
| 1608 | |||
| 1609 | return $_isStdErrRedirected; | ||
| 1610 | } | ||
| 1611 | } | ||
| 1612 | }; | ||
| 1613 | |||
| 1614 | =item I<IsInputRedirected> | ||
| 1615 | |||
| 1616 | field IsInputRedirected ( is => ro, type => Bool ); | ||
| 1617 | class_has IsInputRedirected ( is => ro, type => Bool, init_arg => undef ); | ||
| 1618 | |||
| 1619 | Gets a value that indicates whether input has been redirected from the | ||
| 1620 | standard input stream. I<True> if input is redirected; otherwise, I<false>. | ||
| 1621 | |||
| 1622 | =cut | ||
| 1623 | |||
| 1624 | sub IsInputRedirected { | ||
| 1625 | assert ( @_ == 1 ); | ||
| 1626 | my $caller = shift; | ||
| 1627 | |||
| 1628 | assert ( $caller ); | ||
| 1629 | assert ( is_Object($caller) || is_ClassName($caller) ); | ||
| 1630 | |||
| 1631 | GET: { | ||
| 1632 | return $_isStdInRedirected | ||
| 1633 | if $_stdInRedirectQueried; | ||
| 1634 | |||
| 1635 | { | ||
| 1636 | lock($InternalSyncObject); | ||
| 1637 | |||
| 1638 | return $_isStdInRedirected | ||
| 1639 | if $_stdInRedirectQueried; | ||
| 1640 | |||
| 1641 | $_isStdInRedirected = IsHandleRedirected(ConsoleInputHandle()); | ||
| 1642 | $_stdInRedirectQueried = TRUE; | ||
| 1643 | |||
| 1644 | return $_isStdInRedirected; | ||
| 1645 | } | ||
| 1646 | } | ||
| 1647 | }; | ||
| 1648 | |||
| 1649 | =item I<IsOutputRedirected> | ||
| 1650 | |||
| 1651 | field IsOutputRedirected ( is => ro, type => Bool ); | ||
| 1652 | class_has IsOutputRedirected ( is => ro, type => Bool, init_arg => undef ); | ||
| 1653 | |||
| 1654 | Gets a value that indicates whether output has been redirected from the | ||
| 1655 | standard output stream. I<True> if output is redirected; otherwise, I<false>. | ||
| 1656 | |||
| 1657 | =cut | ||
| 1658 | |||
| 1659 | sub IsOutputRedirected { | ||
| 1660 | assert ( @_ == 1 ); | ||
| 1661 | my $caller = shift; | ||
| 1662 | |||
| 1663 | assert ( $caller ); | ||
| 1664 | assert ( is_Object($caller) || is_ClassName($caller) ); | ||
| 1665 | |||
| 1666 | GET: { | ||
| 1667 | return $_isStdOutRedirected | ||
| 1668 | if $_stdOutRedirectQueried; | ||
| 1669 | |||
| 1670 | { | ||
| 1671 | lock($InternalSyncObject); | ||
| 1672 | |||
| 1673 | return $_isStdOutRedirected | ||
| 1674 | if $_stdOutRedirectQueried; | ||
| 1675 | |||
| 1676 | $_isStdOutRedirected = IsHandleRedirected(ConsoleOutputHandle()); | ||
| 1677 | $_stdOutRedirectQueried = TRUE; | ||
| 1678 | |||
| 1679 | return $_isStdOutRedirected; | ||
| 1680 | } | ||
| 1681 | } | ||
| 1682 | }; | ||
| 1683 | |||
| 1684 | =item I<KeyAvailable> | ||
| 1685 | |||
| 1686 | field KeyAvailable ( is => ro, type => Bool ); | ||
| 1687 | |||
| 1688 | Gets a value indicating whether a key press is available in the input stream. | ||
| 1689 | |||
| 1690 | =cut | ||
| 1691 | |||
| 1692 | sub KeyAvailable { | ||
| 1693 | assert ( @_ == 1 ); | ||
| 1694 | my $self = assert_Object shift; | ||
| 1695 | GET: { | ||
| 1696 | if ( $_cachedInputRecord->[eventType] == Win32Native::KEY_EVENT ) { | ||
| 1697 | return TRUE; | ||
| 1698 | } | ||
| 1699 | |||
| 1700 | my @ir; | ||
| 1701 | my $numEventsRead = 0; | ||
| 1702 | while (TRUE) { | ||
| 1703 | my $r = do { | ||
| 1704 | @ir = Win32::Console::_PeekConsoleInput(ConsoleInputHandle()); | ||
| 1705 | my $r = @ir != 1; | ||
| 1706 | $numEventsRead = @ir > 1 ? 1 : 0; | ||
| 1707 | @ir = (0) x 6 unless $ir[0]; | ||
| 1708 | $r; | ||
| 1709 | }; | ||
| 1710 | if ( !$r ) { | ||
| 1711 | my $errorCode = Win32::GetLastError(); | ||
| 1712 | if ( $errorCode == Win32Native::ERROR_INVALID_HANDLE ) { | ||
| 1713 | confess("InvalidOperationException:\n". | ||
| 1714 | "$ResourceString{InvalidOperation_ConsoleKeyAvailableOnFile}". | ||
| 1715 | "\n"); | ||
| 1716 | } | ||
| 1717 | confess("WinIOError: stdin\n$EXTENDED_OS_ERROR\n"); | ||
| 1718 | } | ||
| 1719 | |||
| 1720 | if ( $numEventsRead == 0 ) { | ||
| 1721 | return FALSE; | ||
| 1722 | } | ||
| 1723 | |||
| 1724 | # Skip non key-down && mod key events. | ||
| 1725 | if ( !IsKeyDownEvent(\@ir) || IsModKey(\@ir) ) { | ||
| 1726 | # | ||
| 1727 | |||
| 1728 | $r = do { | ||
| 1729 | @ir = Win32::Console::_ReadConsoleInput(ConsoleInputHandle()); | ||
| 1730 | my $r = @ir > 1; | ||
| 1731 | $numEventsRead = @ir > 1 ? 1 : 0; | ||
| 1732 | @ir = (0) x 6 unless $ir[0]; | ||
| 1733 | $r; | ||
| 1734 | }; | ||
| 1735 | |||
| 1736 | if ( !$r ) { | ||
| 1737 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n"); | ||
| 1738 | } | ||
| 1739 | } | ||
| 1740 | else { | ||
| 1741 | return TRUE; | ||
| 1742 | } | ||
| 1743 | } | ||
| 1744 | } | ||
| 1745 | }; | ||
| 1746 | |||
| 1747 | =item I<LargestWindowHeight> | ||
| 1748 | |||
| 1749 | field LargestWindowHeight ( is => ro, type => Int ); | ||
| 1750 | |||
| 1751 | Gets the largest possible number of console window rows, based on the current | ||
| 1752 | font and screen resolution. | ||
| 1753 | |||
| 1754 | =cut | ||
| 1755 | |||
| 1756 | sub LargestWindowHeight { | ||
| 1757 | assert ( @_ == 1 ); | ||
| 1758 | my $self = assert_Object shift; | ||
| 1759 | GET: { | ||
| 1760 | # Note this varies based on current screen resolution and | ||
| 1761 | # current console font. Do not cache this value. | ||
| 1762 | my (undef, $bounds_Y) = Win32::Console::_GetLargestConsoleWindowSize( | ||
| 1763 | ConsoleOutputHandle()); | ||
| 1764 | return assert_Int $bounds_Y; | ||
| 1765 | } | ||
| 1766 | }; | ||
| 1767 | |||
| 1768 | =item I<LargestWindowWidth> | ||
| 1769 | |||
| 1770 | field LargestWindowWidth ( is => ro, type => Int ); | ||
| 1771 | |||
| 1772 | Gets the largest possible number of console window columns, based on the | ||
| 1773 | current font and screen resolution. | ||
| 1774 | |||
| 1775 | =cut | ||
| 1776 | |||
| 1777 | sub LargestWindowWidth { | ||
| 1778 | assert ( @_ == 1 ); | ||
| 1779 | my $self = assert_Object shift; | ||
| 1780 | GET: { | ||
| 1781 | # Note this varies based on current screen resolution and | ||
| 1782 | # current console font. Do not cache this value. | ||
| 1783 | my ($bounds_X) = Win32::Console::_GetLargestConsoleWindowSize( | ||
| 1784 | ConsoleOutputHandle()); | ||
| 1785 | return assert_Int $bounds_X; | ||
| 1786 | } | ||
| 1787 | }; | ||
| 1788 | |||
| 1789 | =item I<NumberLock> | ||
| 1790 | |||
| 1791 | field NumberLock ( is => ro, type => Bool ); | ||
| 1792 | |||
| 1793 | Gets a value indicating whether the C<NUM LOCK> keyboard toggle is turned on | ||
| 1794 | or turned off. | ||
| 1795 | |||
| 1796 | =cut | ||
| 1797 | |||
| 1798 | sub NumberLock { | ||
| 1799 | assert ( @_ == 1 ); | ||
| 1800 | my $self = assert_Object shift; | ||
| 1801 | GET: { | ||
| 1802 | require Win32Native; | ||
| 1803 | my $value = (Win32Native::GetKeyState(NumberLockVKCode) & 1) == 1; | ||
| 1804 | return assert_Bool $value; | ||
| 1805 | } | ||
| 1806 | }; | ||
| 1807 | |||
| 1808 | =item I<Out> | ||
| 1809 | |||
| 1810 | field Out ( is => ro, type => FileHandle ); | ||
| 1811 | |||
| 1812 | A I<FileHandle> that represents the standard output stream. | ||
| 1813 | |||
| 1814 | =cut | ||
| 1815 | |||
| 1816 | sub Out { | ||
| 1817 | assert ( @_ == 1 ); | ||
| 1818 | my $self = assert_Object shift; | ||
| 1819 | GET: { | ||
| 1820 | if ( !defined $_out ) { | ||
| 1821 | InitializeStdOutError(TRUE); | ||
| 1822 | } | ||
| 1823 | return $_out; | ||
| 1824 | } | ||
| 1825 | }; | ||
| 1826 | |||
| 1827 | =item I<OutputEncoding> | ||
| 1828 | |||
| 1829 | field OutputEncoding ( is => rw, type => Object ); | ||
| 1830 | class_has OutputEncoding ( is => rw, isa => Object, init_arg => undef ); | ||
| 1831 | |||
| 1832 | Gets or sets the encoding the console uses to write output. | ||
| 1833 | |||
| 1834 | I<Remarks>: A get operation may return a cached value instead of the console's | ||
| 1835 | current output encoding. | ||
| 1836 | |||
| 1837 | =cut | ||
| 1838 | |||
| 1839 | sub OutputEncoding { | ||
| 1840 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 1841 | my $caller = shift; | ||
| 1842 | |||
| 1843 | assert ( $caller ); | ||
| 1844 | assert ( is_Object($caller) || is_ClassName($caller) ); | ||
| 1845 | |||
| 1846 | goto SET if @_; | ||
| 1847 | GET: { | ||
| 1848 | return $_outputEncoding | ||
| 1849 | if $_outputEncoding; | ||
| 1850 | |||
| 1851 | { | ||
| 1852 | lock($InternalSyncObject); | ||
| 1853 | |||
| 1854 | return $_outputEncoding | ||
| 1855 | if $_outputEncoding; | ||
| 1856 | |||
| 1857 | my $cp = Win32::GetConsoleOutputCP() || Win32::GetACP(); | ||
| 1858 | $_outputEncoding = Encode::find_encoding("cp$cp"); | ||
| 1859 | return assert_Object $_outputEncoding; | ||
| 1860 | } | ||
| 1861 | } | ||
| 1862 | SET: { | ||
| 1863 | if ( !defined $_[0] ) { | ||
| 1864 | confess("ArgumentNullException:\n". | ||
| 1865 | sprintf("$ResourceString{ArgumentNullException}\n", "value")); | ||
| 1866 | } | ||
| 1867 | my $value = assert_Object shift; | ||
| 1868 | |||
| 1869 | { | ||
| 1870 | lock($InternalSyncObject); | ||
| 1871 | # Before changing the code page we need to flush the data | ||
| 1872 | # if Out hasn't been redirected. Also, have the next call to | ||
| 1873 | # $_out reinitialize the console code page. | ||
| 1874 | |||
| 1875 | if ( defined($_out) && !$_isOutTextWriterRedirected ) { | ||
| 1876 | $_out->flush(); | ||
| 1877 | $_out = undef; | ||
| 1878 | } | ||
| 1879 | if ( defined($_error) && !$_isErrorTextWriterRedirected ) { | ||
| 1880 | $_error->flush(); | ||
| 1881 | $_error = undef; | ||
| 1882 | } | ||
| 1883 | |||
| 1884 | if ( !IsStandardConsoleUnicodeEncoding($value) ) { | ||
| 1885 | my $cp = $value->$CodePage(); | ||
| 1886 | my $r = Win32::SetConsoleOutputCP($cp); | ||
| 1887 | if ( !$r ) { | ||
| 1888 | warn("WinIOError:\n$EXTENDED_OS_ERROR\n"); | ||
| 1889 | } | ||
| 1890 | } | ||
| 1891 | |||
| 1892 | $_outputEncoding = $value; | ||
| 1893 | return; | ||
| 1894 | } # set | ||
| 1895 | } | ||
| 1896 | }; | ||
| 1897 | |||
| 1898 | =item I<Title> | ||
| 1899 | |||
| 1900 | field Title ( is => rw, type => Str ) = ''; | ||
| 1901 | |||
| 1902 | The string to be displayed in the title bar of the console. The maximum length | ||
| 1903 | of the title string is C<24500> characters for set and C<1024> for get. | ||
| 1904 | |||
| 1905 | I<Throws>: I<InvalidOperationException> in a get operation, if the specified | ||
| 1906 | title is longer than C<24500> characters. | ||
| 1907 | |||
| 1908 | I<Throws>: I<ArgumentOutOfRangeException> in a set operation, if the specified | ||
| 1909 | title is longer than C<24500> characters. | ||
| 1910 | |||
| 1911 | I<Throws>: I<ArgumentNullException> in a set operation, if the specified title | ||
| 1912 | is C<undef>. | ||
| 1913 | |||
| 1914 | I<Throws>: I<Exception> in a set operation, if the specified title is not a | ||
| 1915 | string. | ||
| 1916 | |||
| 1917 | I<Remarks>: The return value is a empty string if the specific length is greater | ||
| 1918 | than C<1024>. | ||
| 1919 | |||
| 1920 | =cut | ||
| 1921 | |||
| 1922 | sub Title { | ||
| 1923 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 1924 | my $self = assert_Object shift; | ||
| 1925 | goto SET if @_; | ||
| 1926 | GET: { | ||
| 1927 | my $title; | ||
| 1928 | my $titleLength = 0xffff; | ||
| 1929 | my $r = do { # GetTitleNative | ||
| 1930 | Win32::SetLastError(0); | ||
| 1931 | $title = Win32::Console::_GetConsoleTitle(); | ||
| 1932 | $titleLength = length($title); | ||
| 1933 | # Win32::Console::_GetConsoleTitle() only supports 1024 characters | ||
| 1934 | $title = substr($title, 0, 1024) if $titleLength > 1024; | ||
| 1935 | Win32::GetLastError() == 0; | ||
| 1936 | }; | ||
| 1937 | |||
| 1938 | if ( !$r ) { | ||
| 1939 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n") | ||
| 1940 | } | ||
| 1941 | |||
| 1942 | if ( $titleLength > MaxConsoleTitleLength ) { | ||
| 1943 | confess("InvalidOperationException:\n". | ||
| 1944 | "$ResourceString{ArgumentOutOfRange_ConsoleTitleTooLong}\n"); | ||
| 1945 | } | ||
| 1946 | |||
| 1947 | return assert_Str $title; | ||
| 1948 | } | ||
| 1949 | SET: { | ||
| 1950 | my $value = shift; | ||
| 1951 | if ( !defined $value ) { | ||
| 1952 | confess("ArgumentNullException:\n". | ||
| 1953 | sprintf("$ResourceString{ArgumentNullException}\n", "value")); | ||
| 1954 | } | ||
| 1955 | assert_Str $value; | ||
| 1956 | if ( length($value) > MaxConsoleTitleLength ) { | ||
| 1957 | confess("ArgumentOutOfRangeException:\n". | ||
| 1958 | "$ResourceString{ArgumentOutOfRange_ConsoleTitleTooLong}\n"); | ||
| 1959 | } | ||
| 1960 | |||
| 1961 | Win32::Console::_SetConsoleTitle($value) | ||
| 1962 | or confess("WinIOError:\n$EXTENDED_OS_ERROR\n"); | ||
| 1963 | return; | ||
| 1964 | } | ||
| 1965 | }; | ||
| 1966 | |||
| 1967 | =item I<TreatControlCAsInput> | ||
| 1968 | |||
| 1969 | field TreatControlCAsInput ( is => rw, type => Bool ) = FALSE; | ||
| 1970 | |||
| 1971 | Indicating whether the combination of the C<Control> modifier key and C<C> | ||
| 1972 | console key (C<Ctrl+C>) is treated as ordinary input or as an interruption | ||
| 1973 | that is handled by the operating system. | ||
| 1974 | |||
| 1975 | The attribute is I<true> if C<Ctrl+C> is treated as ordinary input; otherwise, | ||
| 1976 | I<false>. | ||
| 1977 | |||
| 1978 | I<Throws>: I<IOException> if unable to get or set the input mode of the console | ||
| 1979 | input buffer. | ||
| 1980 | |||
| 1981 | =cut | ||
| 1982 | |||
| 1983 | sub TreatControlCAsInput { | ||
| 1984 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 1985 | my $self = assert_Object shift; | ||
| 1986 | goto SET if @_; | ||
| 1987 | GET: { | ||
| 1988 | my $handle = ConsoleInputHandle(); | ||
| 1989 | if ( $handle == Win32API::File::INVALID_HANDLE_VALUE ) { | ||
| 1990 | confess("IOException:\n$ResourceString{IO_NoConsole}\n"); | ||
| 1991 | } | ||
| 1992 | my $mode = 0; | ||
| 1993 | my $r = do { | ||
| 1994 | Win32::SetLastError(0); | ||
| 1995 | $mode = Win32::Console::_GetConsoleMode($handle) || 0; | ||
| 1996 | Win32::GetLastError() == 0; | ||
| 1997 | }; | ||
| 1998 | if ( !$r ) { | ||
| 1999 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n"); | ||
| 2000 | } | ||
| 2001 | my $value = ($mode & ENABLE_PROCESSED_INPUT) == 0; | ||
| 2002 | return assert_Bool $value; | ||
| 2003 | } | ||
| 2004 | SET: { | ||
| 2005 | my $value = assert_Bool shift; | ||
| 2006 | my $handle = ConsoleInputHandle(); | ||
| 2007 | if ( $handle == Win32API::File::INVALID_HANDLE_VALUE ) { | ||
| 2008 | confess("IOException:\n$ResourceString{IO_NoConsole}\n"); | ||
| 2009 | } | ||
| 2010 | my $mode = 0; | ||
| 2011 | my $r = do { | ||
| 2012 | Win32::SetLastError(0); | ||
| 2013 | $mode = Win32::Console::_GetConsoleMode($handle) || 0; | ||
| 2014 | Win32::GetLastError() == 0; | ||
| 2015 | }; | ||
| 2016 | if ( $value ) { | ||
| 2017 | $mode &= ~ENABLE_PROCESSED_INPUT; | ||
| 2018 | } else { | ||
| 2019 | $mode |= ENABLE_PROCESSED_INPUT; | ||
| 2020 | } | ||
| 2021 | $r = Win32::Console::_SetConsoleMode($handle, $mode); | ||
| 2022 | if ( !$r ) { | ||
| 2023 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n"); | ||
| 2024 | } | ||
| 2025 | return; | ||
| 2026 | } | ||
| 2027 | }; | ||
| 2028 | |||
| 2029 | =item I<WindowHeight> | ||
| 2030 | |||
| 2031 | field WindowHeight ( is => rw, type => Int ); | ||
| 2032 | |||
| 2033 | The height of the console window measured in rows. | ||
| 2034 | |||
| 2035 | I<Throws>: I<ArgumentOutOfRangeException> if the value is less than or equal to | ||
| 2036 | C<0> or the value plus L</WindowTop> is greater than or equal to C<0x7fff> or | ||
| 2037 | the value greater than the largest possible window height for the current | ||
| 2038 | screen resolution and console font. | ||
| 2039 | |||
| 2040 | I<Throws>: I<Exception> if an error occurs when reading or writing information. | ||
| 2041 | |||
| 2042 | =cut | ||
| 2043 | |||
| 2044 | sub WindowHeight { | ||
| 2045 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 2046 | my $self = assert_Object shift; | ||
| 2047 | goto SET if @_; | ||
| 2048 | GET: { | ||
| 2049 | my $csbi = GetBufferInfo(); | ||
| 2050 | my $value = $csbi->{srWindow}->{Bottom} - $csbi->{srWindow}->{Top} + 1; | ||
| 2051 | return assert_Int $value; | ||
| 2052 | } | ||
| 2053 | SET: { | ||
| 2054 | my $value = assert_Int shift; | ||
| 2055 | $self->SetWindowSize($self->WindowWidth, $value); | ||
| 2056 | return; | ||
| 2057 | } | ||
| 2058 | }; | ||
| 2059 | |||
| 2060 | =item I<WindowLeft> | ||
| 2061 | |||
| 2062 | field WindowLeft ( is => rw, type => Int ); | ||
| 2063 | |||
| 2064 | The leftmost console window position measured in columns. | ||
| 2065 | |||
| 2066 | I<Throws>: I<ArgumentOutOfRangeException> if the value is less than C<0> or | ||
| 2067 | as a result of the assignment, L</WindowLeft> plus L</WindowWidth> would exceed | ||
| 2068 | L</BufferWidth>. | ||
| 2069 | |||
| 2070 | I<Throws>: I<Exception> if an error occurs when reading or writing information. | ||
| 2071 | |||
| 2072 | =cut | ||
| 2073 | |||
| 2074 | sub WindowLeft { | ||
| 2075 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 2076 | my $self = assert_Object shift; | ||
| 2077 | goto SET if @_; | ||
| 2078 | GET: { | ||
| 2079 | my $csbi = GetBufferInfo(); | ||
| 2080 | my $value = $csbi->{srWindow}->{Left}; | ||
| 2081 | return assert_Int $value; | ||
| 2082 | } | ||
| 2083 | SET: { | ||
| 2084 | my $value = assert_Int shift; | ||
| 2085 | $self->SetWindowPosition($value, $self->WindowTop); | ||
| 2086 | return; | ||
| 2087 | } | ||
| 2088 | }; | ||
| 2089 | |||
| 2090 | =item I<WindowTop> | ||
| 2091 | |||
| 2092 | field WindowTop ( is => rw, type => Int ); | ||
| 2093 | |||
| 2094 | The uppermost console window position measured in rows. | ||
| 2095 | |||
| 2096 | I<Throws>: I<ArgumentOutOfRangeException> if the value is less than C<0> or | ||
| 2097 | as a result of the assignment, L</WindowTop> plus L</WindowHeight> would exceed | ||
| 2098 | L</BufferHeight>. | ||
| 2099 | |||
| 2100 | I<Throws>: I<Exception> if an error occurs when reading or writing information. | ||
| 2101 | |||
| 2102 | =cut | ||
| 2103 | |||
| 2104 | sub WindowTop { | ||
| 2105 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 2106 | my $self = assert_Object shift; | ||
| 2107 | goto SET if @_; | ||
| 2108 | GET: { | ||
| 2109 | my $csbi = GetBufferInfo(); | ||
| 2110 | my $value = $csbi->{srWindow}->{Top}; | ||
| 2111 | return assert_Int $value; | ||
| 2112 | } | ||
| 2113 | SET: { | ||
| 2114 | my $value = assert_Int shift; | ||
| 2115 | $self->SetWindowPosition($self->WindowLeft, $value); | ||
| 2116 | return; | ||
| 2117 | } | ||
| 2118 | }; | ||
| 2119 | |||
| 2120 | =item I<WindowWidth> | ||
| 2121 | |||
| 2122 | field WindowWidth ( is => rw, type => Int ); | ||
| 2123 | |||
| 2124 | The width of the console window measured in columns. | ||
| 2125 | |||
| 2126 | I<Throws>: I<ArgumentOutOfRangeException> if the value is less than or equal to | ||
| 2127 | C<0> or the value plus L</WindowLeft> is greater than or equal to C<0x7fff> or | ||
| 2128 | the value greater than the largest possible window width for the current screen | ||
| 2129 | resolution and console font. | ||
| 2130 | |||
| 2131 | I<Throws>: I<Exception> if an error occurs when reading or writing information. | ||
| 2132 | |||
| 2133 | =cut | ||
| 2134 | |||
| 2135 | sub WindowWidth { | ||
| 2136 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 2137 | my $self = assert_Object shift; | ||
| 2138 | goto SET if @_; | ||
| 2139 | GET: { | ||
| 2140 | my $csbi = GetBufferInfo(); | ||
| 2141 | my $value = $csbi->{srWindow}->{Right} - $csbi->{srWindow}->{Left} + 1; | ||
| 2142 | return assert_Int $value; | ||
| 2143 | } | ||
| 2144 | SET: { | ||
| 2145 | my $value = assert_Int shift; | ||
| 2146 | $self->SetWindowSize($value, $self->WindowHeight); | ||
| 2147 | return; | ||
| 2148 | } | ||
| 2149 | }; | ||
| 2150 | |||
| 2151 | =back | ||
| 2152 | |||
| 2153 | =cut | ||
| 2154 | |||
| 2155 | # ------------------------------------------------------------------------ | ||
| 2156 | # Constructors ----------------------------------------------------------- | ||
| 2157 | # ------------------------------------------------------------------------ | ||
| 2158 | |||
| 2159 | use constant FACTORY => __PACKAGE__; | ||
| 2160 | |||
| 2161 | =head2 Constructors | ||
| 2162 | |||
| 2163 | =over | ||
| 2164 | |||
| 2165 | =item I<new> | ||
| 2166 | |||
| 2167 | factory new() : Win32::Console::DotNet | ||
| 2168 | |||
| 2169 | Public constructor. | ||
| 2170 | |||
| 2171 | =cut | ||
| 2172 | |||
| 2173 | sub new { | ||
| 2174 | assert ( @_ == 1 ); | ||
| 2175 | my $class = shift; | ||
| 2176 | assert ( is_ClassName $class ); | ||
| 2177 | return bless {}, $class; | ||
| 2178 | }; | ||
| 2179 | |||
| 2180 | =item I<instance> | ||
| 2181 | |||
| 2182 | factory instance() : Win32::Console::DotNet | ||
| 2183 | |||
| 2184 | This constructor instantiates an object instance if none exists, otherwise it | ||
| 2185 | returns an existing instance. | ||
| 2186 | |||
| 2187 | It is used to initialize the default I/O console. | ||
| 2188 | |||
| 2189 | =cut | ||
| 2190 | |||
| 2191 | sub instance { | ||
| 2192 | assert ( @_ == 1 ); | ||
| 2193 | my $class = shift; | ||
| 2194 | # already got an object | ||
| 2195 | return $class if ref($class); | ||
| 2196 | # create a instance and store it in $_instances if not already defined | ||
| 2197 | $_instance = $class->new() unless $_instance; | ||
| 2198 | return $_instance; | ||
| 2199 | } | ||
| 2200 | |||
| 2201 | =back | ||
| 2202 | |||
| 2203 | =cut | ||
| 2204 | |||
| 2205 | # ------------------------------------------------------------------------ | ||
| 2206 | # Destructors ------------------------------------------------------------ | ||
| 2207 | # ------------------------------------------------------------------------ | ||
| 2208 | |||
| 2209 | =head2 Destructors | ||
| 2210 | |||
| 2211 | =over | ||
| 2212 | |||
| 2213 | =item I<DESTROY> | ||
| 2214 | |||
| 2215 | method DESTROY() | ||
| 2216 | |||
| 2217 | Restore the console before destroying the instance/object. | ||
| 2218 | |||
| 2219 | =cut | ||
| 2220 | |||
| 2221 | # | ||
| 2222 | # END block to explicitly destroy the Singleton object since | ||
| 2223 | # destruction order at program exit is not predictable. | ||
| 2224 | # see CPAN RT #23568 and #68526 for examples | ||
| 2225 | # | ||
| 2226 | END { | ||
| 2227 | # dereferences and causes orderly destruction of the instance | ||
| 2228 | undef($_instance); | ||
| 2229 | } | ||
| 2230 | |||
| 2231 | =back | ||
| 2232 | |||
| 2233 | =cut | ||
| 2234 | |||
| 2235 | # ------------------------------------------------------------------------ | ||
| 2236 | # Methods ---------------------------------------------------------------- | ||
| 2237 | # ------------------------------------------------------------------------ | ||
| 2238 | |||
| 2239 | =head2 Methods | ||
| 2240 | |||
| 2241 | =over | ||
| 2242 | |||
| 2243 | =item I<Beep> | ||
| 2244 | |||
| 2245 | method Beep() | ||
| 2246 | |||
| 2247 | Plays the sound of a beep through the console speaker. | ||
| 2248 | |||
| 2249 | method Beep(Int $frequency, Int $duration) | ||
| 2250 | |||
| 2251 | Plays the sound of a beep of a specified frequency and duration through the | ||
| 2252 | console speaker. | ||
| 2253 | |||
| 2254 | I<Param>: C<$frequency> of the beep, ranging from C<37> to C<32767> hertz. | ||
| 2255 | |||
| 2256 | I<Param>: C<$duration> of the beep measured in milliseconds. | ||
| 2257 | |||
| 2258 | I<Throws>: I<ArgumentOutOfRangeException> if C<$frequency> is less than C<37> | ||
| 2259 | or more than C<32767> hertz or C<$duration> is less than or equal to zero. | ||
| 2260 | |||
| 2261 | =cut | ||
| 2262 | |||
| 2263 | sub Beep { | ||
| 2264 | assert ( @_ == 1 || @_ == 3 ); | ||
| 2265 | my $self = assert_Object shift; | ||
| 2266 | my $frequency = @_ > 1 ? assert_Int(shift) : 800; | ||
| 2267 | my $duration = @_ > 0 ? assert_Int(shift) : 200; | ||
| 2268 | |||
| 2269 | if ( $frequency < MinBeepFrequency || $frequency > MaxBeepFrequency ) { | ||
| 2270 | confess("ArgumentOutOfRangeException: frequency $frequency\n". | ||
| 2271 | sprintf("$ResourceString{ArgumentOutOfRange_BeepFrequency}\n", | ||
| 2272 | MinBeepFrequency, MaxBeepFrequency)); | ||
| 2273 | } | ||
| 2274 | if ( $duration <= 0 ) { | ||
| 2275 | confess("ArgumentOutOfRangeException: duration $duration\n". | ||
| 2276 | "$ResourceString{ArgumentOutOfRange_NeedPosNum}\n"); | ||
| 2277 | } | ||
| 2278 | |||
| 2279 | # Note that Beep over Remote Desktop connections does not currently | ||
| 2280 | # work. Ignore any failures here. | ||
| 2281 | require Win32Native; | ||
| 2282 | Win32Native::Beep($frequency, $duration); | ||
| 2283 | return; | ||
| 2284 | } | ||
| 2285 | |||
| 2286 | =item I<Clear> | ||
| 2287 | |||
| 2288 | method Clear() | ||
| 2289 | |||
| 2290 | Clears the console buffer and corresponding console window of display | ||
| 2291 | information. | ||
| 2292 | |||
| 2293 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 2294 | |||
| 2295 | =cut | ||
| 2296 | |||
| 2297 | sub Clear { | ||
| 2298 | assert ( @_ == 1 ); | ||
| 2299 | my $self = assert_Object shift; | ||
| 2300 | |||
| 2301 | my $coordScreen = { X => 0, Y => 0 }; | ||
| 2302 | my $csbi; | ||
| 2303 | my $conSize; | ||
| 2304 | my $success; | ||
| 2305 | |||
| 2306 | my $hConsole = ConsoleOutputHandle(); | ||
| 2307 | if ( $hConsole == Win32API::File::INVALID_HANDLE_VALUE ) { | ||
| 2308 | confess("IOException:\n$ResourceString{IO_NoConsole}\n"); | ||
| 2309 | } | ||
| 2310 | |||
| 2311 | # get the number of character cells in the current buffer | ||
| 2312 | # Go through my helper method for fetching a screen buffer info | ||
| 2313 | # to correctly handle default console colors. | ||
| 2314 | $csbi = GetBufferInfo(); | ||
| 2315 | $conSize = $csbi->{dwSize}->{X} * $csbi->{dwSize}->{Y}; | ||
| 2316 | |||
| 2317 | # fill the entire screen with blanks | ||
| 2318 | |||
| 2319 | my $numCellsWritten = 0; | ||
| 2320 | $success = do { | ||
| 2321 | Win32::SetLastError(0); | ||
| 2322 | $numCellsWritten = Win32::Console::_FillConsoleOutputCharacter($hConsole, | ||
| 2323 | ' ', $conSize, $coordScreen->{X}, $coordScreen->{Y} | ||
| 2324 | ) || 0; | ||
| 2325 | Win32::GetLastError() == 0; | ||
| 2326 | }; | ||
| 2327 | if ( !$success ) { | ||
| 2328 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n"); | ||
| 2329 | } | ||
| 2330 | |||
| 2331 | # now set the buffer's attributes accordingly | ||
| 2332 | |||
| 2333 | $numCellsWritten = 0; | ||
| 2334 | $success = do { | ||
| 2335 | Win32::SetLastError(0); | ||
| 2336 | $numCellsWritten = Win32::Console::_FillConsoleOutputAttribute($hConsole, | ||
| 2337 | $csbi->{wAttributes}, $conSize, $coordScreen->{X}, $coordScreen->{Y} | ||
| 2338 | ) || 0; | ||
| 2339 | Win32::GetLastError() == 0; | ||
| 2340 | }; | ||
| 2341 | if ( !$success ) { | ||
| 2342 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n"); | ||
| 2343 | } | ||
| 2344 | |||
| 2345 | # put the cursor at (0, 0) | ||
| 2346 | |||
| 2347 | $success = Win32::Console::_SetConsoleCursorPosition($hConsole, | ||
| 2348 | $coordScreen->{X}, $coordScreen->{Y}); | ||
| 2349 | if ( !$success ) { | ||
| 2350 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n"); | ||
| 2351 | } | ||
| 2352 | return; | ||
| 2353 | } | ||
| 2354 | |||
| 2355 | =item I<GetCursorPosition> | ||
| 2356 | |||
| 2357 | method GetCursorPosition() : ArrayRef[Int] | ||
| 2358 | |||
| 2359 | Gets the position of the cursor. | ||
| 2360 | |||
| 2361 | I<Returns>: the column and row position of the cursor as array reference. | ||
| 2362 | |||
| 2363 | =cut | ||
| 2364 | |||
| 2365 | sub GetCursorPosition { | ||
| 2366 | assert ( @_ == 1 ); | ||
| 2367 | my $self = assert_Object shift; | ||
| 2368 | |||
| 2369 | return [ | ||
| 2370 | $self->CursorLeft, | ||
| 2371 | $self->CursorTop, | ||
| 2372 | ] | ||
| 2373 | } | ||
| 2374 | |||
| 2375 | =item I<MoveBufferArea> | ||
| 2376 | |||
| 2377 | method MoveBufferArea(Int $sourceLeft, Int $sourceTop, Int $sourceWidth, | ||
| 2378 | Int $sourceHeight, Int $targetLeft, Int $targetTop); | ||
| 2379 | |||
| 2380 | Copies a specified source area of the screen buffer to a specified destination | ||
| 2381 | area. | ||
| 2382 | |||
| 2383 | I<Param>: C<$sourceLeft> is the leftmost column of the source area. | ||
| 2384 | |||
| 2385 | I<Param>: C<$sourceTop> is the topmost row of the source area. | ||
| 2386 | |||
| 2387 | I<Param>: C<$sourceWidth> is the number of columns in the source area. | ||
| 2388 | |||
| 2389 | I<Param>: C<$sourceHeight> is the number of rows in the source area. | ||
| 2390 | |||
| 2391 | I<Param>: C<$targetLeft> is the leftmost column of the destination area. | ||
| 2392 | |||
| 2393 | I<Param>: C<$targetTop> is the topmost row of the destination area. | ||
| 2394 | |||
| 2395 | I<Throws>: I<ArgumentOutOfRangeException> if one or more of the parameters is | ||
| 2396 | less than zero or C<$sourceLeft> or C<$targetLeft> is greater than or equal to | ||
| 2397 | L</BufferWidth> or C<$sourceTop> or C<$targetTop> is greater than or equal to | ||
| 2398 | L</BufferHeight> or C<$sourceTop> + C<$sourceHeight> is greater than or equal | ||
| 2399 | to L</BufferHeight> or C<$sourceLeft> + C<$sourceWidth> is greater than or | ||
| 2400 | equal to L</BufferWidth>. | ||
| 2401 | |||
| 2402 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 2403 | |||
| 2404 | I<Remarks>: If the destination and source parameters specify a position located | ||
| 2405 | outside the boundaries of the current screen buffer, only the portion of the | ||
| 2406 | source area that fits within the destination area is copied. That is, the | ||
| 2407 | source area is clipped to fit the current screen buffer. | ||
| 2408 | |||
| 2409 | The L</MoveBufferArea> method copies the source area to the destination area. If | ||
| 2410 | the destination area does not intersect the source area, the source area is | ||
| 2411 | filled with blanks using the current foreground and background colors. | ||
| 2412 | Otherwise, the intersected portion of the source area is not filled. | ||
| 2413 | |||
| 2414 | method MoveBufferArea(Int $sourceLeft, Int $sourceTop, Int $sourceWidth, | ||
| 2415 | Int $sourceHeight, Int $targetLeft, Int $targetTop, Str $sourceChar, | ||
| 2416 | Int $sourceForeColor, Int $sourceBackColor); | ||
| 2417 | |||
| 2418 | Copies a specified source area of the screen buffer to a specified destination | ||
| 2419 | area. | ||
| 2420 | |||
| 2421 | I<Param>: C<$sourceLeft> is the leftmost column of the source area. | ||
| 2422 | |||
| 2423 | I<Param>: C<$sourceTop> is the topmost row of the source area. | ||
| 2424 | |||
| 2425 | I<Param>: C<$sourceWidth> is the number of columns in the source area. | ||
| 2426 | |||
| 2427 | I<Param>: C<$sourceHeight> is the number of rows in the source area. | ||
| 2428 | |||
| 2429 | I<Param>: C<$targetLeft> is the leftmost column of the destination area. | ||
| 2430 | |||
| 2431 | I<Param>: C<$targetTop> is the topmost row of the destination area. | ||
| 2432 | |||
| 2433 | I<Param>: C<$sourceChar> is the character used to fill the source area. | ||
| 2434 | |||
| 2435 | I<Param>: C<$sourceForeColor> is the foreground color used to fill the source | ||
| 2436 | area. | ||
| 2437 | |||
| 2438 | I<Param>: C<$sourceBackColor> is the background color used to fill the source | ||
| 2439 | area. | ||
| 2440 | |||
| 2441 | I<Throws>: I<ArgumentOutOfRangeException> if one or more of the parameters is | ||
| 2442 | less than zero or C<$sourceLeft> or C<$targetLeft> is greater than or equal to | ||
| 2443 | L</BufferWidth> or C<$sourceTop> or C<$targetTop> is greater than or equal to | ||
| 2444 | L</BufferHeight> or C<$sourceTop> + C<$sourceHeight> is greater than or equal | ||
| 2445 | to L</BufferHeight> or C<$sourceLeft> + C<$sourceWidth> is greater than or | ||
| 2446 | equal to L</BufferWidth>. | ||
| 2447 | |||
| 2448 | I<Throws>: I<ArgumentException> if one or both of the color parameters is not | ||
| 2449 | valid. | ||
| 2450 | |||
| 2451 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 2452 | |||
| 2453 | I<Remarks>: If the destination and source parameters specify a position located | ||
| 2454 | outside the boundaries of the current screen buffer, only the portion of the | ||
| 2455 | source area that fits within the destination area is copied. That is, the | ||
| 2456 | source area is clipped to fit the current screen buffer. | ||
| 2457 | |||
| 2458 | The L</MoveBufferArea> method copies the source area to the destination area. | ||
| 2459 | If the destination area does not intersect the source area, the source area is | ||
| 2460 | filled with the character specified by C<$sourceChar>, using the colors | ||
| 2461 | specified by C<$sourceForeColor> and C<$sourceBackColor>. Otherwise, the | ||
| 2462 | intersected portion of the source area is not filled. | ||
| 2463 | |||
| 2464 | The L</MoveBufferArea> method performs no operation if C<$sourceWidth> or | ||
| 2465 | C<$sourceHeight> is zero. | ||
| 2466 | |||
| 2467 | =cut | ||
| 2468 | |||
| 2469 | sub MoveBufferArea { | ||
| 2470 | assert ( @_ == 7 || @_ == 10 ); | ||
| 2471 | my $self = assert_Object shift; | ||
| 2472 | my $sourceLeft = assert_Int shift; | ||
| 2473 | my $sourceTop = assert_Int shift; | ||
| 2474 | my $sourceWidth = assert_Int shift; | ||
| 2475 | my $sourceHeight = assert_Int shift; | ||
| 2476 | my $targetLeft = assert_Int shift; | ||
| 2477 | my $targetTop = assert_Int shift; | ||
| 2478 | my $sourceChar = @_ ? assert_Str(shift) : ' '; | ||
| 2479 | my $sourceForeColor = @_ ? assert_Int(shift) : $FG_BLACK; | ||
| 2480 | my $sourceBackColor = @_ ? assert_Int(shift) : $self->BackgroundColor; | ||
| 2481 | |||
| 2482 | if ( $sourceForeColor < 0 || $sourceForeColor > 15 ) { | ||
| 2483 | confess("ArgumentException: sourceForeColor\n". | ||
| 2484 | "$ResourceString{Arg_InvalidConsoleColor}\n"); | ||
| 2485 | } | ||
| 2486 | if ( $sourceBackColor < 0 || $sourceBackColor > 15 ) { | ||
| 2487 | confess("ArgumentException: sourceBackColor\n". | ||
| 2488 | "$ResourceString{Arg_InvalidConsoleColor}\n"); | ||
| 2489 | } | ||
| 2490 | |||
| 2491 | my $csbi = GetBufferInfo(); | ||
| 2492 | my $bufferSize = $csbi->{dwSize}; | ||
| 2493 | if ( $sourceLeft < 0 || $sourceLeft > $bufferSize->{X} ) { | ||
| 2494 | confess("ArgumentOutOfRangeException: sourceLeft $sourceLeft\n". | ||
| 2495 | "$ResourceString{ArgumentOutOfRange_ConsoleBufferBoundaries}\n"); | ||
| 2496 | } | ||
| 2497 | if ( $sourceTop < 0 || $sourceTop > $bufferSize->{Y} ) { | ||
| 2498 | confess("ArgumentOutOfRangeException: sourceTop $sourceTop\n". | ||
| 2499 | "$ResourceString{ArgumentOutOfRange_ConsoleBufferBoundaries}\n"); | ||
| 2500 | } | ||
| 2501 | if ( $sourceWidth < 0 || $sourceWidth > $bufferSize->{X} - $sourceLeft ) { | ||
| 2502 | confess("ArgumentOutOfRangeException: sourceWidth $sourceWidth\n". | ||
| 2503 | "$ResourceString{ArgumentOutOfRange_ConsoleBufferBoundaries}\n"); | ||
| 2504 | } | ||
| 2505 | if ( $sourceHeight < 0 || $sourceTop > $bufferSize->{Y} - $sourceHeight ) { | ||
| 2506 | confess("ArgumentOutOfRangeException: sourceHeight $sourceHeight\n". | ||
| 2507 | "$ResourceString{ArgumentOutOfRange_ConsoleBufferBoundaries}\n"); | ||
| 2508 | } | ||
| 2509 | |||
| 2510 | # Note: if the target range is partially in and partially out | ||
| 2511 | # of the buffer, then we let the OS clip it for us. | ||
| 2512 | if ( $targetLeft < 0 || $targetLeft > $bufferSize->{X} ) { | ||
| 2513 | confess("ArgumentOutOfRangeException: targetLeft $targetLeft\n". | ||
| 2514 | "$ResourceString{ArgumentOutOfRange_ConsoleBufferBoundaries}\n"); | ||
| 2515 | } | ||
| 2516 | if ( $targetTop < 0 || $targetTop > $bufferSize->{Y} ) { | ||
| 2517 | confess("ArgumentOutOfRangeException: targetTop $targetTop\n". | ||
| 2518 | "$ResourceString{ArgumentOutOfRange_ConsoleBufferBoundaries}\n"); | ||
| 2519 | } | ||
| 2520 | |||
| 2521 | # If we're not doing any work, bail out now (Windows will return | ||
| 2522 | # an error otherwise) | ||
| 2523 | return if $sourceWidth == 0 || $sourceHeight == 0; | ||
| 2524 | |||
| 2525 | # Read data from the original location, blank it out, then write | ||
| 2526 | # it to the new location. This will handle overlapping source and | ||
| 2527 | # destination regions correctly. | ||
| 2528 | |||
| 2529 | # See the "Reading and Writing Blocks of Characters and Attributes" | ||
| 2530 | # sample for help | ||
| 2531 | |||
| 2532 | # Read the old data | ||
| 2533 | my $data = (" " x ($sourceWidth * $sourceHeight * 4)); | ||
| 2534 | $bufferSize->{X} = $sourceWidth; | ||
| 2535 | $bufferSize->{Y} = $sourceHeight; | ||
| 2536 | my $bufferCoord = { X => 0, Y => 0 }; | ||
| 2537 | my $readRegion = {}; | ||
| 2538 | $readRegion->{Left} = $sourceLeft; | ||
| 2539 | $readRegion->{Right} = $sourceLeft + $sourceWidth - 1; | ||
| 2540 | $readRegion->{Top} = $sourceTop; | ||
| 2541 | $readRegion->{Bottom} = $sourceTop + $sourceHeight - 1; | ||
| 2542 | |||
| 2543 | my $r; | ||
| 2544 | $r = do { | ||
| 2545 | my @rect = Win32::Console::_ReadConsoleOutput(ConsoleOutputHandle(), | ||
| 2546 | $data, | ||
| 2547 | $bufferSize->{X}, $bufferSize->{Y}, | ||
| 2548 | $bufferCoord->{X}, $bufferCoord->{Y}, | ||
| 2549 | $readRegion->{Left}, $readRegion->{Top}, | ||
| 2550 | $readRegion->{Right}, $readRegion->{Bottom} | ||
| 2551 | ); | ||
| 2552 | @rect > 1; | ||
| 2553 | }; | ||
| 2554 | if ( !$r ) { | ||
| 2555 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n"); | ||
| 2556 | } | ||
| 2557 | |||
| 2558 | # Overwrite old section | ||
| 2559 | # I don't have a good function to blank out a rectangle. | ||
| 2560 | my $writeCoord = { X => 0, Y => 0 }; | ||
| 2561 | $writeCoord->{X} = $sourceLeft; | ||
| 2562 | my $c = ConsoleColorToColorAttribute($sourceBackColor, TRUE); | ||
| 2563 | $c |= ConsoleColorToColorAttribute($sourceForeColor, FALSE); | ||
| 2564 | my $attr = $c; | ||
| 2565 | my $numWritten; | ||
| 2566 | for (my $i = $sourceTop; $i < $sourceTop + $sourceHeight; $i++) { | ||
| 2567 | $writeCoord->{Y} = $i; | ||
| 2568 | $r = do { | ||
| 2569 | Win32::SetLastError(0); | ||
| 2570 | $numWritten = Win32::Console::_FillConsoleOutputCharacter( | ||
| 2571 | ConsoleOutputHandle(), $sourceChar, $sourceWidth, | ||
| 2572 | $writeCoord->{X}, $writeCoord->{Y} | ||
| 2573 | ) || 0; | ||
| 2574 | Win32::GetLastError() == 0; | ||
| 2575 | }; | ||
| 2576 | assert ( $numWritten == $sourceWidth | ||
| 2577 | or ~- warn "FillConsoleOutputCharacter wrote the wrong number of chars!" | ||
| 2578 | ); | ||
| 2579 | if ( !$r ) { | ||
| 2580 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n"); | ||
| 2581 | } | ||
| 2582 | $r = do { | ||
| 2583 | Win32::SetLastError(0); | ||
| 2584 | $numWritten = Win32::Console::_FillConsoleOutputAttribute( | ||
| 2585 | ConsoleOutputHandle(), $attr, $sourceWidth, | ||
| 2586 | $writeCoord->{X}, $writeCoord->{Y} | ||
| 2587 | ) || 0; | ||
| 2588 | Win32::GetLastError() == 0; | ||
| 2589 | }; | ||
| 2590 | if ( !$r ) { | ||
| 2591 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n"); | ||
| 2592 | } | ||
| 2593 | } | ||
| 2594 | |||
| 2595 | # Write text to new location | ||
| 2596 | my $writeRegion = {}; | ||
| 2597 | $writeRegion->{Left} = $targetLeft; | ||
| 2598 | $writeRegion->{Right} = $targetLeft + $sourceWidth; | ||
| 2599 | $writeRegion->{Top} = $targetTop; | ||
| 2600 | $writeRegion->{Bottom} = $targetTop + $sourceHeight; | ||
| 2601 | |||
| 2602 | $r = do { | ||
| 2603 | my @rect = Win32::Console::_WriteConsoleOutput( | ||
| 2604 | ConsoleOutputHandle(), $data, | ||
| 2605 | $bufferSize->{X}, $bufferSize->{Y}, | ||
| 2606 | $bufferCoord->{X}, $bufferCoord->{Y}, | ||
| 2607 | $writeRegion->{Left}, $writeRegion->{Top}, | ||
| 2608 | $writeRegion->{Right}, $writeRegion->{Bottom} | ||
| 2609 | ); | ||
| 2610 | @rect > 1; | ||
| 2611 | }; | ||
| 2612 | return; | ||
| 2613 | } | ||
| 2614 | |||
| 2615 | =item I<OpenStandardError> | ||
| 2616 | |||
| 2617 | method OpenStandardError() : FileHandle | ||
| 2618 | method OpenStandardError(Int $bufferSize) : FileHandle | ||
| 2619 | classmethod OpenStandardError() : FileHandle | ||
| 2620 | classmethod OpenStandardError(Int $bufferSize) : FileHandle | ||
| 2621 | |||
| 2622 | Acquires the standard error object. | ||
| 2623 | |||
| 2624 | I<Returns>: the standard error object. | ||
| 2625 | |||
| 2626 | =cut | ||
| 2627 | |||
| 2628 | sub OpenStandardError { | ||
| 2629 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 2630 | my $caller = shift; | ||
| 2631 | my $bufferSize = @_ ? assert_Int(shift) : DefaultConsoleBufferSize; | ||
| 2632 | |||
| 2633 | assert ( $caller ); | ||
| 2634 | assert ( is_Object($caller) || is_ClassName($caller) ); | ||
| 2635 | |||
| 2636 | if ( $bufferSize < 0 ) { | ||
| 2637 | confess("ArgumentOutOfRangeException: bufferSize\n". | ||
| 2638 | "$ResourceString{ArgumentOutOfRange_NeedNonNegNum}\n"); | ||
| 2639 | } | ||
| 2640 | return GetStandardFile(STD_ERROR_HANDLE, 'w', $bufferSize); | ||
| 2641 | } | ||
| 2642 | |||
| 2643 | =item I<OpenStandardInput> | ||
| 2644 | |||
| 2645 | method OpenStandardInput() : FileHandle | ||
| 2646 | method OpenStandardInput(Int $bufferSize) : FileHandle | ||
| 2647 | classmethod OpenStandardInput() : FileHandle | ||
| 2648 | classmethod OpenStandardInput(Int $bufferSize) : FileHandle | ||
| 2649 | |||
| 2650 | Acquires the standard input object. | ||
| 2651 | |||
| 2652 | I<Returns>: the standard input object. | ||
| 2653 | |||
| 2654 | =cut | ||
| 2655 | |||
| 2656 | sub OpenStandardInput { | ||
| 2657 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 2658 | my $caller = shift; | ||
| 2659 | my $bufferSize = @_ ? assert_Int(shift) : DefaultConsoleBufferSize; | ||
| 2660 | |||
| 2661 | assert ( $caller ); | ||
| 2662 | assert ( is_Object($caller) || is_ClassName($caller) ); | ||
| 2663 | |||
| 2664 | if ( $bufferSize < 0 ) { | ||
| 2665 | confess("ArgumentOutOfRangeException: bufferSize\n". | ||
| 2666 | "$ResourceString{ArgumentOutOfRange_NeedNonNegNum}\n"); | ||
| 2667 | } | ||
| 2668 | return GetStandardFile(STD_INPUT_HANDLE, 'r', $bufferSize); | ||
| 2669 | } | ||
| 2670 | |||
| 2671 | =item I<OpenStandardOutput> | ||
| 2672 | |||
| 2673 | method OpenStandardOutput() : FileHandle | ||
| 2674 | method OpenStandardOutput(Int $bufferSize) : FileHandle | ||
| 2675 | classmethod OpenStandardOutput() : FileHandle | ||
| 2676 | classmethod OpenStandardOutput(Int $bufferSize) : FileHandle | ||
| 2677 | |||
| 2678 | Acquires the standard output object. | ||
| 2679 | |||
| 2680 | I<Returns>: the standard output object. | ||
| 2681 | |||
| 2682 | =cut | ||
| 2683 | |||
| 2684 | sub OpenStandardOutput { | ||
| 2685 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 2686 | my $caller = shift; | ||
| 2687 | my $bufferSize = @_ ? assert_Int(shift) : DefaultConsoleBufferSize; | ||
| 2688 | |||
| 2689 | assert ( $caller ); | ||
| 2690 | assert ( is_Object($caller) || is_ClassName($caller) ); | ||
| 2691 | |||
| 2692 | if ( $bufferSize < 0 ) { | ||
| 2693 | confess("ArgumentOutOfRangeException: bufferSize\n". | ||
| 2694 | "$ResourceString{ArgumentOutOfRange_NeedNonNegNum}\n"); | ||
| 2695 | } | ||
| 2696 | return GetStandardFile(STD_OUTPUT_HANDLE, 'w', $bufferSize); | ||
| 2697 | } | ||
| 2698 | |||
| 2699 | =item I<Read> | ||
| 2700 | |||
| 2701 | method Read() : Int | ||
| 2702 | |||
| 2703 | Reads the next character from the standard input stream. | ||
| 2704 | |||
| 2705 | I<Returns>: the next character from the input stream, or negative one (C<-1>) | ||
| 2706 | if there are currently no more characters to be read. | ||
| 2707 | |||
| 2708 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 2709 | |||
| 2710 | =cut | ||
| 2711 | |||
| 2712 | sub Read { | ||
| 2713 | assert ( @_ == 1 ); | ||
| 2714 | my $self = assert_Object shift; | ||
| 2715 | |||
| 2716 | assert_FileHandle $self->In; | ||
| 2717 | my $r = $self->In->read(my $ch, 1); | ||
| 2718 | if ( !$r ) { | ||
| 2719 | confess("IOException:\n$OS_ERROR\n") unless defined $r; | ||
| 2720 | # flush on stdin is not provided, so a loop is used | ||
| 2721 | 1 while defined $self->In->getline(); | ||
| 2722 | return -1; | ||
| 2723 | } | ||
| 2724 | return ord($ch); | ||
| 2725 | } | ||
| 2726 | |||
| 2727 | =item I<ReadKey> | ||
| 2728 | |||
| 2729 | method ReadKey() : ConsoleKeyInfo | ||
| 2730 | method ReadKey(Bool $intercept) : ConsoleKeyInfo | ||
| 2731 | |||
| 2732 | Obtains the next character or function key pressed by the user. | ||
| 2733 | The pressed key is optionally displayed in the console window. | ||
| 2734 | |||
| 2735 | I<Param>: C<$intercept> determines whether to display the pressed key in the | ||
| 2736 | console window. I<True> to not display the pressed key; otherwise, I<false>. | ||
| 2737 | |||
| 2738 | I<Returns>: an I<ConsoleKeyInfo> (blessed HashRef) that describes the console | ||
| 2739 | key and unicode character, if any, that correspond to the pressed console key. | ||
| 2740 | The I<ConsoleKeyInfo> also describes, in a bitwise combination of values, | ||
| 2741 | whether one or more C<Shift>, C<Alt>, or C<Ctrl> modifier keys was pressed | ||
| 2742 | simultaneously with the console key. | ||
| 2743 | |||
| 2744 | =cut | ||
| 2745 | |||
| 2746 | sub ReadKey { | ||
| 2747 | assert ( @_ >= 1 && @_ <= 2 ); | ||
| 2748 | my $self = assert_Object shift; | ||
| 2749 | my $intercept = @_ ? assert_Bool(shift) : FALSE; | ||
| 2750 | |||
| 2751 | my @ir; | ||
| 2752 | my $numEventsRead = -1; | ||
| 2753 | my $r; | ||
| 2754 | |||
| 2755 | { | ||
| 2756 | lock($ReadKeySyncObject); | ||
| 2757 | |||
| 2758 | if ( $_cachedInputRecord->[eventType] == Win32Native::KEY_EVENT ) { | ||
| 2759 | # We had a previous keystroke with repeated characters. | ||
| 2760 | @ir = @$_cachedInputRecord; | ||
| 2761 | if ( $_cachedInputRecord->[repeatCount] == 0 ) { | ||
| 2762 | $_cachedInputRecord->[eventType] = -1; | ||
| 2763 | } else { | ||
| 2764 | $_cachedInputRecord->[repeatCount]--; | ||
| 2765 | } | ||
| 2766 | # We will return one key from this method, so we decrement the | ||
| 2767 | # repeatCount here, leaving the cachedInputRecord in the "queue". | ||
| 2768 | |||
| 2769 | } else { # We did NOT have a previous keystroke with repeated characters: | ||
| 2770 | |||
| 2771 | while (TRUE) { | ||
| 2772 | $r = do { | ||
| 2773 | @ir = Win32::Console::_ReadConsoleInput(ConsoleInputHandle()); | ||
| 2774 | my $r = @ir > 1; | ||
| 2775 | $numEventsRead = @ir > 1 ? 1 : 0; | ||
| 2776 | @ir = (0) x 6 unless $ir[0]; | ||
| 2777 | $r; | ||
| 2778 | }; | ||
| 2779 | if ( !$r || $numEventsRead == 0 ) { | ||
| 2780 | # This will fail when stdin is redirected from a file or pipe. | ||
| 2781 | # We could theoretically call Console->Read here, but I | ||
| 2782 | # think we might do some things incorrectly then. | ||
| 2783 | confess("InvalidOperationException:\n". | ||
| 2784 | "$ResourceString{InvalidOperation_ConsoleReadKeyOnFile}\n"); | ||
| 2785 | } | ||
| 2786 | |||
| 2787 | my $keyCode = $ir[virtualKeyCode]; | ||
| 2788 | |||
| 2789 | # First check for non-keyboard events & discard them. Generally we tap | ||
| 2790 | # into only KeyDown events and ignore the KeyUp events but it is | ||
| 2791 | # possible that we are dealing with a Alt+NumPad unicode key sequence, | ||
| 2792 | # the final unicode char is revealed only when the Alt key is | ||
| 2793 | # released (i.e when the sequence is complete). To avoid noise, when | ||
| 2794 | # the Alt key is down, we should eat up any intermediate key strokes | ||
| 2795 | # (from NumPad) that collectively forms the Unicode character. | ||
| 2796 | |||
| 2797 | if ( !IsKeyDownEvent(\@ir) ) { | ||
| 2798 | # | ||
| 2799 | next if $keyCode != AltVKCode; | ||
| 2800 | } | ||
| 2801 | |||
| 2802 | my $ch = $ir[uChar]; | ||
| 2803 | |||
| 2804 | # In a Alt+NumPad unicode sequence, when the alt key is released uChar | ||
| 2805 | # will represent the final unicode character, we need to surface this. | ||
| 2806 | # VirtualKeyCode for this event will be Alt from the Alt-Up key event. | ||
| 2807 | # This is probably not the right code, especially when we don't expose | ||
| 2808 | # ConsoleKey.Alt, so this will end up being the hex value (0x12). | ||
| 2809 | # VK_PACKET comes very close to being useful and something that we | ||
| 2810 | # could look into using for this purpose... | ||
| 2811 | |||
| 2812 | if ( $ch == 0 ) { | ||
| 2813 | # Skip mod keys. | ||
| 2814 | next if IsModKey(\@ir); | ||
| 2815 | } | ||
| 2816 | |||
| 2817 | # When Alt is down, it is possible that we are in the middle of a | ||
| 2818 | # Alt+NumPad unicode sequence. Escape any intermediate NumPad keys | ||
| 2819 | # whether NumLock is on or not (notepad behavior) | ||
| 2820 | my $key = $keyCode; | ||
| 2821 | if (IsAltKeyDown(\@ir) && (($key >= VK_NUMPAD0 && $key <= VK_NUMPAD9) | ||
| 2822 | || ($key == VK_CLEAR) || ($key == VK_INSERT) | ||
| 2823 | || ($key >= VK_PRIOR && $key <= VK_NEXT)) | ||
| 2824 | ) { | ||
| 2825 | next; | ||
| 2826 | } | ||
| 2827 | |||
| 2828 | if ( $ir[repeatCount] > 1 ) { | ||
| 2829 | $ir[repeatCount]--; | ||
| 2830 | $_cachedInputRecord = \@ir; | ||
| 2831 | } | ||
| 2832 | last; | ||
| 2833 | } | ||
| 2834 | } # we did NOT have a previous keystroke with repeated characters. | ||
| 2835 | } # lock($ReadKeySyncObject) | ||
| 2836 | |||
| 2837 | my $state = $ir[controlKeyState]; | ||
| 2838 | my $shift = ($state & SHIFT_PRESSED) != 0; | ||
| 2839 | my $alt = ($state & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) != 0; | ||
| 2840 | my $control = ($state & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) != 0; | ||
| 2841 | |||
| 2842 | my $info = { | ||
| 2843 | KeyChar => chr($ir[uChar]), | ||
| 2844 | Key => $ir[virtualKeyCode], | ||
| 2845 | Modifiers => ($shift ? 2 : 0) + ($alt ? 1 : 0) + ($control ? 4 : 0), | ||
| 2846 | }; | ||
| 2847 | |||
| 2848 | if ( !$intercept ) { | ||
| 2849 | $self->Write(chr($ir[uChar])); | ||
| 2850 | } | ||
| 2851 | return bless $info, 'ConsoleKeyInfo'; | ||
| 2852 | } | ||
| 2853 | |||
| 2854 | =item I<ReadLine> | ||
| 2855 | |||
| 2856 | method ReadLine() : Str | ||
| 2857 | |||
| 2858 | Reads the next line of characters from the standard input stream. | ||
| 2859 | |||
| 2860 | I<Returns>: the next line of characters from the input stream, or C<undef> if no | ||
| 2861 | more lines are available. | ||
| 2862 | |||
| 2863 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 2864 | |||
| 2865 | =cut | ||
| 2866 | |||
| 2867 | sub ReadLine { | ||
| 2868 | assert ( @_ == 1 ); | ||
| 2869 | my $self = assert_Object shift; | ||
| 2870 | |||
| 2871 | assert_FileHandle $self->In; | ||
| 2872 | $! = undef; | ||
| 2873 | my $str = $self->In->getline(); | ||
| 2874 | confess("IOException:\n$OS_ERROR\n") if $!; | ||
| 2875 | chomp $str if defined $str; | ||
| 2876 | return $str; | ||
| 2877 | } | ||
| 2878 | |||
| 2879 | =item I<ResetColor> | ||
| 2880 | |||
| 2881 | method ResetColor() | ||
| 2882 | |||
| 2883 | Sets the foreground and background console colors to their defaults. | ||
| 2884 | |||
| 2885 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 2886 | |||
| 2887 | =cut | ||
| 2888 | |||
| 2889 | sub ResetColor { | ||
| 2890 | assert ( @_ == 1 ); | ||
| 2891 | my $self = assert_Object shift; | ||
| 2892 | |||
| 2893 | my $succeeded; | ||
| 2894 | my $csbi = GetBufferInfo(FALSE, \$succeeded); | ||
| 2895 | return if !$succeeded; | ||
| 2896 | |||
| 2897 | assert ( $_haveReadDefaultColors | ||
| 2898 | or ~- warn "Setting the color attributes before we've read the default ". | ||
| 2899 | "color attributes!" | ||
| 2900 | ); | ||
| 2901 | |||
| 2902 | my $defaultAttrs = $$_defaultColors & 0xff; | ||
| 2903 | # Ignore errors here - there are some scenarios for running code that wants | ||
| 2904 | # to print in colors to the console in a Windows application. | ||
| 2905 | Win32::Console::_SetConsoleTextAttribute(ConsoleOutputHandle(), | ||
| 2906 | $defaultAttrs); | ||
| 2907 | return; | ||
| 2908 | } | ||
| 2909 | |||
| 2910 | =item I<SetBufferSize> | ||
| 2911 | |||
| 2912 | method SetBufferSize(Int $width, Int $height) | ||
| 2913 | |||
| 2914 | Sets the height and width of the screen buffer area to the specified values. | ||
| 2915 | |||
| 2916 | I<Param>: C<$width> of the buffer area measured in columns. | ||
| 2917 | |||
| 2918 | I<Param>: C<$height> of the buffer area measured in rows. | ||
| 2919 | |||
| 2920 | =cut | ||
| 2921 | |||
| 2922 | sub SetBufferSize { | ||
| 2923 | assert ( @_ == 3 ); | ||
| 2924 | my $self = assert_Object shift; | ||
| 2925 | my $width = assert_Int shift; | ||
| 2926 | my $height = assert_Int shift; | ||
| 2927 | |||
| 2928 | my $csbi = GetBufferInfo(); | ||
| 2929 | my $srWindow = $csbi->{srWindow}; | ||
| 2930 | if ( $width < $srWindow->{Right} + 1 || $width >= 0x7fff ) { | ||
| 2931 | confess("ArgumentOutOfRangeException: width $width\n". | ||
| 2932 | "$ResourceString{ArgumentOutOfRange_ConsoleBufferLessThanWindowSize}", | ||
| 2933 | "\n"); | ||
| 2934 | } | ||
| 2935 | if ( $height < $srWindow->{Bottom} + 1 || $height >= 0x7fff ) { | ||
| 2936 | confess("ArgumentOutOfRangeException: height $height\n". | ||
| 2937 | "$ResourceString{ArgumentOutOfRange_ConsoleBufferLessThanWindowSize}", | ||
| 2938 | "\n"); | ||
| 2939 | } | ||
| 2940 | Win32::Console::_SetConsoleScreenBufferSize(ConsoleOutputHandle(), | ||
| 2941 | $width, $height) or confess("WinIOError:\n$EXTENDED_OS_ERROR\n"); | ||
| 2942 | |||
| 2943 | return; | ||
| 2944 | } | ||
| 2945 | |||
| 2946 | =item I<SetCursorPosition> | ||
| 2947 | |||
| 2948 | method SetCursorPosition(Int $left, Int $top) | ||
| 2949 | |||
| 2950 | Sets the position of the cursor. | ||
| 2951 | |||
| 2952 | I<Param>: C<$left> column position of the cursor. Columns are numbered from left | ||
| 2953 | to right starting at C<0>. | ||
| 2954 | |||
| 2955 | I<Param>: C<$top> row position of the cursor. Rows are numbered from top to | ||
| 2956 | bottom starting at C<0>. | ||
| 2957 | |||
| 2958 | =cut | ||
| 2959 | |||
| 2960 | sub SetCursorPosition { | ||
| 2961 | assert ( @_ == 3 ); | ||
| 2962 | my $self = assert_Object shift; | ||
| 2963 | my $left = assert_Int shift; | ||
| 2964 | my $top = assert_Int shift; | ||
| 2965 | |||
| 2966 | # Note on argument checking - the upper bounds are NOT correct | ||
| 2967 | # here! But it looks slightly expensive to compute them. Let | ||
| 2968 | # Windows calculate them, then we'll give a nice error message. | ||
| 2969 | if ( $left < 0 || $left >= 0x7fff ) { | ||
| 2970 | confess("ArgumentOutOfRangeException: left $left\n". | ||
| 2971 | "$ResourceString{ArgumentOutOfRange_ConsoleBufferBoundaries}\n"); | ||
| 2972 | } | ||
| 2973 | if ( $top < 0 || $top >= 0x7fff ) { | ||
| 2974 | confess("ArgumentOutOfRangeException: top $top\n". | ||
| 2975 | "$ResourceString{ArgumentOutOfRange_ConsoleBufferBoundaries}\n"); | ||
| 2976 | } | ||
| 2977 | |||
| 2978 | my $hConsole = ConsoleOutputHandle(); | ||
| 2979 | my $r = Win32::Console::_SetConsoleCursorPosition($hConsole, $left, $top); | ||
| 2980 | if ( !$r ) { | ||
| 2981 | # Give a nice error message for out of range sizes | ||
| 2982 | my $errorCode = Win32::GetLastError(); | ||
| 2983 | my $csbi = GetBufferInfo(); | ||
| 2984 | if ( $left < 0 || $left >= $csbi->{dwSize}->{X} ) { | ||
| 2985 | confess("ArgumentOutOfRangeException: left $left\n". | ||
| 2986 | "$ResourceString{ArgumentOutOfRange_ConsoleBufferBoundaries}\n"); | ||
| 2987 | } | ||
| 2988 | if ( $top < 0 || $top >= $csbi->{dwSize}->{Y} ) { | ||
| 2989 | confess("ArgumentOutOfRangeException: top $top\n". | ||
| 2990 | "$ResourceString{ArgumentOutOfRange_ConsoleBufferBoundaries}\n"); | ||
| 2991 | } | ||
| 2992 | |||
| 2993 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n"); | ||
| 2994 | } | ||
| 2995 | |||
| 2996 | return; | ||
| 2997 | } | ||
| 2998 | |||
| 2999 | =item I<SetError> | ||
| 3000 | |||
| 3001 | method SetError(FileHandle $newError) | ||
| 3002 | |||
| 3003 | Sets the L</Error> attribute to the specified error I<FileHandle>. | ||
| 3004 | |||
| 3005 | I<Param>: C<$newError> represents a io handle that is the new standard error. | ||
| 3006 | |||
| 3007 | =cut | ||
| 3008 | |||
| 3009 | sub SetError { | ||
| 3010 | assert ( @_ == 2 ); | ||
| 3011 | my $self = assert_Object shift; | ||
| 3012 | my $newError = shift; | ||
| 3013 | |||
| 3014 | if ( !defined $newError ) { | ||
| 3015 | confess("ArgumentNullException:\n". | ||
| 3016 | sprintf("$ResourceString{ArgumentNullException}\n", "newError")); | ||
| 3017 | } | ||
| 3018 | $_isErrorTextWriterRedirected = TRUE; | ||
| 3019 | { | ||
| 3020 | lock($InternalSyncObject); | ||
| 3021 | $_error = assert_FileHandle $newError; | ||
| 3022 | } | ||
| 3023 | return; | ||
| 3024 | } | ||
| 3025 | |||
| 3026 | =item I<SetIn> | ||
| 3027 | |||
| 3028 | method SetIn(FileHandle $newIn) | ||
| 3029 | |||
| 3030 | Sets the L</In> attribute to the specified input I<FileHandle>. | ||
| 3031 | |||
| 3032 | I<Param>: C<$newIn> represents a io handle that is the new standard input. | ||
| 3033 | |||
| 3034 | =cut | ||
| 3035 | |||
| 3036 | sub SetIn { | ||
| 3037 | assert ( @_ == 2 ); | ||
| 3038 | my $self = assert_Object shift; | ||
| 3039 | my $newIn = shift; | ||
| 3040 | |||
| 3041 | if ( !defined $newIn ) { | ||
| 3042 | confess("ArgumentNullException:\n". | ||
| 3043 | sprintf("$ResourceString{ArgumentNullException}\n", "newIn")); | ||
| 3044 | } | ||
| 3045 | { | ||
| 3046 | lock($InternalSyncObject); | ||
| 3047 | $_in = assert_FileHandle $newIn; | ||
| 3048 | } | ||
| 3049 | return; | ||
| 3050 | } | ||
| 3051 | |||
| 3052 | =item I<SetOut> | ||
| 3053 | |||
| 3054 | method SetOut(FileHandle $newOut) | ||
| 3055 | |||
| 3056 | Sets the L</Out> attribute to the specified output I<FileHandle>. | ||
| 3057 | |||
| 3058 | I<Param>: C<$newOut> represents a io handle that is the new standard output. | ||
| 3059 | |||
| 3060 | =cut | ||
| 3061 | |||
| 3062 | sub SetOut { | ||
| 3063 | assert ( @_ == 2 ); | ||
| 3064 | my $self = assert_Object shift; | ||
| 3065 | my $newOut = shift; | ||
| 3066 | |||
| 3067 | if ( !defined $newOut ) { | ||
| 3068 | confess("ArgumentNullException:\n". | ||
| 3069 | sprintf("$ResourceString{ArgumentNullException}\n", "newOut")); | ||
| 3070 | } | ||
| 3071 | $_isOutTextWriterRedirected = TRUE; | ||
| 3072 | { | ||
| 3073 | lock($InternalSyncObject); | ||
| 3074 | $_out = assert_FileHandle $newOut; | ||
| 3075 | } | ||
| 3076 | return; | ||
| 3077 | } | ||
| 3078 | |||
| 3079 | =item I<SetWindowSize> | ||
| 3080 | |||
| 3081 | method SetWindowSize(Int $width, Int $height) | ||
| 3082 | |||
| 3083 | Sets the height and width of the console window to the specified values. | ||
| 3084 | |||
| 3085 | I<Param>: C<$width> of the console window measured in columns. | ||
| 3086 | |||
| 3087 | I<Param>: C<$height> of the console window measured in rows. | ||
| 3088 | |||
| 3089 | =cut | ||
| 3090 | |||
| 3091 | sub SetWindowSize { | ||
| 3092 | assert ( @_ == 3 ); | ||
| 3093 | my $self = assert_Object shift; | ||
| 3094 | my $width = assert_Int shift; | ||
| 3095 | my $height = assert_Int shift; | ||
| 3096 | |||
| 3097 | if ( $width <= 0 ) { | ||
| 3098 | confess("ArgumentOutOfRangeException: width $width\n". | ||
| 3099 | "$ResourceString{ArgumentOutOfRange_NeedPosNum}\n"); | ||
| 3100 | } | ||
| 3101 | if ( $height <= 0 ) { | ||
| 3102 | confess("ArgumentOutOfRangeException: height $height\n". | ||
| 3103 | "$ResourceString{ArgumentOutOfRange_NeedPosNum}\n"); | ||
| 3104 | } | ||
| 3105 | |||
| 3106 | # Get the position of the current console window | ||
| 3107 | my $csbi = GetBufferInfo(); | ||
| 3108 | my $r; | ||
| 3109 | |||
| 3110 | # If the buffer is smaller than this new window size, resize the | ||
| 3111 | # buffer to be large enough. Include window position. | ||
| 3112 | my $resizeBuffer = FALSE; | ||
| 3113 | my $size = { | ||
| 3114 | X => $csbi->{dwSize}->{X}, | ||
| 3115 | Y => $csbi->{dwSize}->{Y}, | ||
| 3116 | }; | ||
| 3117 | if ( $csbi->{dwSize}->{X} < $csbi->{srWindow}->{Left} + $width ) { | ||
| 3118 | if ( $csbi->{srWindow}->{Left} >= 0x7fff - $width ) { | ||
| 3119 | confess("ArgumentOutOfRangeException: width $width\n". | ||
| 3120 | "$ResourceString{ArgumentOutOfRange_ConsoleWindowBufferSize}\n"); | ||
| 3121 | } | ||
| 3122 | $size->{X} = $csbi->{srWindow}->{Left} + $width; | ||
| 3123 | $resizeBuffer = TRUE; | ||
| 3124 | } | ||
| 3125 | if ( $csbi->{dwSize}->{Y} < $csbi->{srWindow}->{Top} + $height ) { | ||
| 3126 | if ( $csbi->{srWindow}->{Top} >= 0x7fff - $height ) { | ||
| 3127 | confess("ArgumentOutOfRangeException: height $height\n". | ||
| 3128 | "$ResourceString{ArgumentOutOfRange_ConsoleWindowBufferSize}\n"); | ||
| 3129 | } | ||
| 3130 | $size->{Y} = $csbi->{srWindow}->{Top} + $height; | ||
| 3131 | $resizeBuffer = TRUE; | ||
| 3132 | } | ||
| 3133 | if ( $resizeBuffer ) { | ||
| 3134 | $r = Win32::Console::_SetConsoleScreenBufferSize(ConsoleOutputHandle(), | ||
| 3135 | $size->{X}, $size->{Y}); | ||
| 3136 | if ( !$r ) { | ||
| 3137 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n"); | ||
| 3138 | } | ||
| 3139 | } | ||
| 3140 | |||
| 3141 | my $srWindow = $csbi->{srWindow}; | ||
| 3142 | # Preserve the position, but change the size. | ||
| 3143 | $srWindow->{Bottom} = $srWindow->{Top} + $height - 1; | ||
| 3144 | $srWindow->{Right} = $srWindow->{Left} + $width - 1; | ||
| 3145 | |||
| 3146 | $r = Win32::Console::_SetConsoleWindowInfo(ConsoleOutputHandle(), TRUE, | ||
| 3147 | $srWindow->{Left}, $srWindow->{Top}, | ||
| 3148 | $srWindow->{Right}, $srWindow->{Bottom} | ||
| 3149 | ); | ||
| 3150 | if ( !$r ) { | ||
| 3151 | my $errorCode = Win32::GetLastError(); | ||
| 3152 | |||
| 3153 | # If we resized the buffer, un-resize it. | ||
| 3154 | if ( $resizeBuffer ) { | ||
| 3155 | Win32::Console::_SetConsoleScreenBufferSize(ConsoleOutputHandle(), | ||
| 3156 | $csbi->{dwSize}->{X}, $csbi->{dwSize}->{Y}); | ||
| 3157 | } | ||
| 3158 | |||
| 3159 | # Try to give a better error message here | ||
| 3160 | my $bounds = { X => 0, Y => 0 }; | ||
| 3161 | ($bounds->{X}, $bounds->{Y}) = | ||
| 3162 | Win32::Console::_GetLargestConsoleWindowSize(ConsoleOutputHandle()); | ||
| 3163 | if ( $width > $bounds->{X} ) { | ||
| 3164 | confess("ArgumentOutOfRangeException: width $width\n". | ||
| 3165 | sprintf("$ResourceString{ArgumentOutOfRange_ConsoleWindowSize_Size}". | ||
| 3166 | "\n", $bounds->{X})); | ||
| 3167 | } | ||
| 3168 | if ( $height > $bounds->{Y} ) { | ||
| 3169 | confess("ArgumentOutOfRangeException: height $height\n". | ||
| 3170 | sprintf("$ResourceString{ArgumentOutOfRange_ConsoleWindowSize_Size}". | ||
| 3171 | "\n", $bounds->{Y})); | ||
| 3172 | } | ||
| 3173 | |||
| 3174 | confess(sprintf("WinIOError:\n%s\n", Win32::FormatMessage($errorCode))); | ||
| 3175 | } | ||
| 3176 | |||
| 3177 | return; | ||
| 3178 | } | ||
| 3179 | |||
| 3180 | =item I<SetWindowPosition> | ||
| 3181 | |||
| 3182 | method SetWindowPosition(Int $left, Int $top) | ||
| 3183 | |||
| 3184 | Sets the position of the console window relative to the screen buffer. | ||
| 3185 | |||
| 3186 | I<Param>: C<$left> corner of the console window. | ||
| 3187 | |||
| 3188 | I<Param>: C<$top> corner of the console window. | ||
| 3189 | |||
| 3190 | =cut | ||
| 3191 | |||
| 3192 | sub SetWindowPosition { | ||
| 3193 | assert ( @_ == 3 ); | ||
| 3194 | my $self = assert_Object shift; | ||
| 3195 | my $left = assert_Int shift; | ||
| 3196 | my $top = assert_Int shift; | ||
| 3197 | |||
| 3198 | # Get the size of the current console window | ||
| 3199 | my $csbi = GetBufferInfo(); | ||
| 3200 | |||
| 3201 | my $srWindow = $csbi->{srWindow}; | ||
| 3202 | |||
| 3203 | # Check for arithmetic underflows & overflows. | ||
| 3204 | my $newRight = $left + $srWindow->{Right} - $srWindow->{Left} + 1; | ||
| 3205 | if ( $left < 0 || $newRight > $csbi->{dwSize}->{X} || $newRight < 0 ) { | ||
| 3206 | confess("ArgumentOutOfRangeException: left $left\n". | ||
| 3207 | "$ResourceString{ArgumentOutOfRange_ConsoleWindowPos}\n"); | ||
| 3208 | } | ||
| 3209 | my $newBottom = $top + $srWindow->{Bottom} - $srWindow->{Top} + 1; | ||
| 3210 | if ( $top < 0 || $newBottom > $csbi->{dwSize}->{Y} || $newBottom < 0 ) { | ||
| 3211 | confess("ArgumentOutOfRangeException: top $top\n". | ||
| 3212 | "$ResourceString{ArgumentOutOfRange_ConsoleWindowPos}\n"); | ||
| 3213 | } | ||
| 3214 | |||
| 3215 | # Preserve the size, but move the position. | ||
| 3216 | $srWindow->{Bottom} -= $srWindow->{Top} - $top; | ||
| 3217 | $srWindow->{Right} -= $srWindow->{Left} - $left; | ||
| 3218 | $srWindow->{Left} = $left; | ||
| 3219 | $srWindow->{Top} = $top; | ||
| 3220 | |||
| 3221 | my $r = Win32::Console::_SetConsoleWindowInfo(ConsoleOutputHandle(), TRUE, | ||
| 3222 | $srWindow->{Left}, $srWindow->{Top}, | ||
| 3223 | $srWindow->{Right}, $srWindow->{Bottom} | ||
| 3224 | ); | ||
| 3225 | if ( !$r ) { | ||
| 3226 | confess("WinIOError:\n$EXTENDED_OS_ERROR\n"); | ||
| 3227 | } | ||
| 3228 | |||
| 3229 | return; | ||
| 3230 | } | ||
| 3231 | |||
| 3232 | =item I<Write> | ||
| 3233 | |||
| 3234 | method Write(Str $format, Item $arg0, Item $arg1, ...) | ||
| 3235 | |||
| 3236 | Writes the text representation of the specified arguments to the standard | ||
| 3237 | output stream using the specified format information. | ||
| 3238 | |||
| 3239 | I<Param>: C<$format> is a composite format string. | ||
| 3240 | |||
| 3241 | I<Param>: C<$arg0> is the first item to write using format. | ||
| 3242 | |||
| 3243 | I<Param>: C<$arg1> is the second item to write using format. | ||
| 3244 | |||
| 3245 | I<Param>: ... | ||
| 3246 | |||
| 3247 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 3248 | |||
| 3249 | I<Throws>: I<ArgumentNullException> if C<$format> is C<undef>. | ||
| 3250 | |||
| 3251 | I<Remarks>: this method does not perform any formatting of its own: It uses the | ||
| 3252 | Perl's subroutine I<sprintf>. | ||
| 3253 | |||
| 3254 | method Write(Int $value) | ||
| 3255 | |||
| 3256 | Writes the text representation of the specified integer value to the standard | ||
| 3257 | output stream. | ||
| 3258 | |||
| 3259 | I<Param>: C<$value> is the value to write. | ||
| 3260 | |||
| 3261 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 3262 | |||
| 3263 | method Write(String $value) | ||
| 3264 | |||
| 3265 | Writes the specified string value to the standard output stream. | ||
| 3266 | |||
| 3267 | I<Param>: C<$value> is the value to write. | ||
| 3268 | |||
| 3269 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 3270 | |||
| 3271 | method Write(Object $value) | ||
| 3272 | |||
| 3273 | Writes the text representation of the specified object to the standard output | ||
| 3274 | stream. | ||
| 3275 | |||
| 3276 | I<Param>: C<$value> is the value to write or C<undef>. | ||
| 3277 | |||
| 3278 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 3279 | |||
| 3280 | I<Remarks>: If C<$value> is C<undef>, nothing is written and no exception is | ||
| 3281 | thrown. Otherwise, the stringification method of C<$value> is called to produce | ||
| 3282 | its string representation, and the resulting string is written to the standard | ||
| 3283 | output stream. | ||
| 3284 | |||
| 3285 | method Write(Num $value) | ||
| 3286 | |||
| 3287 | Writes the text representation of the specified floating-point value to the | ||
| 3288 | standard output stream. | ||
| 3289 | |||
| 3290 | I<Param>: C<$value> is the value to write. | ||
| 3291 | |||
| 3292 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 3293 | |||
| 3294 | method Write(Bool $value) | ||
| 3295 | |||
| 3296 | Writes the text representation of the specified boolean value to the standard | ||
| 3297 | output stream. | ||
| 3298 | |||
| 3299 | I<Param>: C<$value> is the value to write. | ||
| 3300 | |||
| 3301 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 3302 | |||
| 3303 | =cut | ||
| 3304 | |||
| 3305 | sub Write { | ||
| 3306 | assert ( @_ > 1 ); | ||
| 3307 | my $self = assert_Object shift; | ||
| 3308 | |||
| 3309 | assert_FileHandle $self->Out; | ||
| 3310 | $! = undef; | ||
| 3311 | if ( @_ > 1 ) { | ||
| 3312 | my $format = shift; | ||
| 3313 | if ( !defined $format ) { | ||
| 3314 | confess("ArgumentNullException:\n". | ||
| 3315 | sprintf("$ResourceString{ArgumentNullException}\n", "format")); | ||
| 3316 | } | ||
| 3317 | no warnings 'uninitialized'; | ||
| 3318 | $self->Out->printf($format, @_); | ||
| 3319 | } elsif ( @_ > 0 ) { | ||
| 3320 | $self->Out->print(shift); | ||
| 3321 | } | ||
| 3322 | confess("IOException:\n$OS_ERROR\n") if $!; | ||
| 3323 | return; | ||
| 3324 | } | ||
| 3325 | |||
| 3326 | =item I<WriteLine> | ||
| 3327 | |||
| 3328 | method WriteLine(Str $format, Item $arg0, Item $arg1, ...) | ||
| 3329 | |||
| 3330 | Writes the text representation of the specified objects, followed by the | ||
| 3331 | current line terminator, to the standard output stream using the specified | ||
| 3332 | format information. | ||
| 3333 | |||
| 3334 | I<Param>: C<$format> is a composite format string. | ||
| 3335 | |||
| 3336 | I<Param>: C<$arg0> is the first item to write using format. | ||
| 3337 | |||
| 3338 | I<Param>: C<$arg1> is the second item to write using format. | ||
| 3339 | |||
| 3340 | I<Param>: ... | ||
| 3341 | |||
| 3342 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 3343 | |||
| 3344 | I<Throws>: I<ArgumentNullException> if C<$format> is C<undef>. | ||
| 3345 | |||
| 3346 | I<Remarks>: this method does not perform any formatting of its own: It uses the | ||
| 3347 | Perl's subroutine I<sprintf>. | ||
| 3348 | |||
| 3349 | method WriteLine(String $value) | ||
| 3350 | |||
| 3351 | Writes the specified string value, followed by the current line terminator, to | ||
| 3352 | the standard output stream. | ||
| 3353 | |||
| 3354 | I<Param>: C<$value> is the value to write. | ||
| 3355 | |||
| 3356 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 3357 | |||
| 3358 | method WriteLine(Int $value) | ||
| 3359 | |||
| 3360 | Writes the text representation of the specified integer value, followed by the | ||
| 3361 | current line terminator, to the standard output stream. | ||
| 3362 | |||
| 3363 | I<Param>: C<$value> is the value to write. | ||
| 3364 | |||
| 3365 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 3366 | |||
| 3367 | method WriteLine(Num $value) | ||
| 3368 | |||
| 3369 | Writes the text representation of the specified floating-point value, followed | ||
| 3370 | by the current line terminator, to the standard output stream. | ||
| 3371 | |||
| 3372 | I<Param>: C<$value> is the value to write. | ||
| 3373 | |||
| 3374 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 3375 | |||
| 3376 | method WriteLine(Bool $value) | ||
| 3377 | |||
| 3378 | Writes the text representation of the specified boolean value, followed by the | ||
| 3379 | current line terminator, to the standard output stream. | ||
| 3380 | |||
| 3381 | I<Param>: C<$value> is the value to write. | ||
| 3382 | |||
| 3383 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 3384 | |||
| 3385 | method WriteLine() | ||
| 3386 | |||
| 3387 | Writes the current line terminator to the standard output stream. | ||
| 3388 | |||
| 3389 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 3390 | |||
| 3391 | method WriteLine(Object $value) | ||
| 3392 | |||
| 3393 | Writes the text representation of the specified object, followed by the current | ||
| 3394 | line terminator, to the standard output stream. | ||
| 3395 | |||
| 3396 | I<Param>: C<$value> is the value to write or C<undef>. | ||
| 3397 | |||
| 3398 | I<Throws>: I<IOException> if an I/O error occurred. | ||
| 3399 | |||
| 3400 | I<Remarks>: If C<$value> is C<undef> only the line terminator is written. | ||
| 3401 | Otherwise, the stringification method of C<$value> is called to produce its | ||
| 3402 | string representation, and the resulting string is written to the standard | ||
| 3403 | output stream. | ||
| 3404 | |||
| 3405 | =cut | ||
| 3406 | |||
| 3407 | sub WriteLine { | ||
| 3408 | assert ( @_ > 0 ); | ||
| 3409 | my $self = assert_Object shift; | ||
| 3410 | |||
| 3411 | assert_FileHandle $self->Out; | ||
| 3412 | $! = undef; | ||
| 3413 | if ( @_ > 1 ) { | ||
| 3414 | # Intercept redundant warnings in Perl 5.22 and higher | ||
| 3415 | local $SIG{__WARN__} = sub { | ||
| 3416 | if ( $] >= 5.022 && warnings::enabled('redundant') ) { | ||
| 3417 | $_ = shift; | ||
| 3418 | s/\sat .+?\R$//; | ||
| 3419 | s/sprintf/WriteLine/g; | ||
| 3420 | Carp::carp($_); | ||
| 3421 | } | ||
| 3422 | }; | ||
| 3423 | my $format = shift; | ||
| 3424 | if ( !defined $format ) { | ||
| 3425 | confess("ArgumentNullException:\n". | ||
| 3426 | sprintf("$ResourceString{ArgumentNullException}\n", "format")); | ||
| 3427 | } | ||
| 3428 | no warnings 'uninitialized'; | ||
| 3429 | $self->Out->say(sprintf($format, @_)); | ||
| 3430 | } elsif ( @_ > 0 ) { | ||
| 3431 | $self->Out->say(shift); | ||
| 3432 | } else { | ||
| 3433 | $self->Out->say(); | ||
| 3434 | } | ||
| 3435 | confess("IOException:\n$OS_ERROR\n") if $!; | ||
| 3436 | return; | ||
| 3437 | } | ||
| 3438 | |||
| 3439 | # ------------------------------------------------------------------------ | ||
| 3440 | # Subroutines ------------------------------------------------------------ | ||
| 3441 | # ------------------------------------------------------------------------ | ||
| 3442 | |||
| 3443 | no namespace::clean; | ||
| 3444 | |||
| 3445 | =begin private | ||
| 3446 | |||
| 3447 | =item I<CheckOutputDebug> | ||
| 3448 | |||
| 3449 | sub CheckOutputDebug() : Bool | ||
| 3450 | |||
| 3451 | Checks whether the developer mode is currently activated | ||
| 3452 | |||
| 3453 | I<Returns>: I<true> if the developer mode is currently enabled. | ||
| 3454 | |||
| 3455 | I<Remarks>: It always returns I<false> on Windows versions older than | ||
| 3456 | Windows 10. | ||
| 3457 | |||
| 3458 | =cut | ||
| 3459 | |||
| 3460 | # This is ONLY used in debug builds. If you have a registry key set, | ||
| 3461 | # it will redirect Console->Out & Error on console-less applications to | ||
| 3462 | # your debugger's output window. | ||
| 3463 | sub CheckOutputDebug { | ||
| 3464 | return exists(&Win32::IsDeveloperModeEnabled) | ||
| 3465 | && Win32::IsDeveloperModeEnabled(); | ||
| 3466 | } | ||
| 3467 | |||
| 3468 | =item I<ColorAttributeToConsoleColor> | ||
| 3469 | |||
| 3470 | sub ColorAttributeToConsoleColor(Int $c) : Int | ||
| 3471 | |||
| 3472 | Converts the color attribute of the Windows console into a color constant. | ||
| 3473 | |||
| 3474 | I<Param>: C<$c> is a color attribute of the Windows Console. | ||
| 3475 | |||
| 3476 | I<Returns>: a console color constant. | ||
| 3477 | |||
| 3478 | =cut | ||
| 3479 | |||
| 3480 | sub ColorAttributeToConsoleColor { | ||
| 3481 | assert ( @_ == 1 ); | ||
| 3482 | my $c = assert_Int shift; | ||
| 3483 | |||
| 3484 | # Turn background colors into foreground colors. | ||
| 3485 | if ( ($c & 0xf0) != 0 ) { | ||
| 3486 | $c = $c >> 4; | ||
| 3487 | } | ||
| 3488 | |||
| 3489 | return $c; | ||
| 3490 | } | ||
| 3491 | |||
| 3492 | =item I<ConsoleColorToColorAttribute> | ||
| 3493 | |||
| 3494 | sub ConsoleColorToColorAttribute(Int $color, Bool $isBackground) : Int | ||
| 3495 | |||
| 3496 | Converts a color constant into the color attribute of the Windows Console. | ||
| 3497 | |||
| 3498 | I<Param>: C<$color> specifies a color constant that defines the foreground or | ||
| 3499 | background color. | ||
| 3500 | |||
| 3501 | I<Param>: C<$isBackground> specifies whether the specified color constant is a | ||
| 3502 | foreground or background color. | ||
| 3503 | |||
| 3504 | I<Returns>: a color attribute of the Windows Console. | ||
| 3505 | |||
| 3506 | =cut | ||
| 3507 | |||
| 3508 | sub ConsoleColorToColorAttribute { | ||
| 3509 | assert ( @_ == 2 ); | ||
| 3510 | my $color = assert_Int shift; | ||
| 3511 | my $isBackground = assert_Bool shift; | ||
| 3512 | |||
| 3513 | if ( ($color & ~0xf) != 0 ) { | ||
| 3514 | confess("ArgumentException:\n". | ||
| 3515 | "$ResourceString{Arg_InvalidConsoleColor}\n"); | ||
| 3516 | } | ||
| 3517 | |||
| 3518 | my $c = $color; | ||
| 3519 | |||
| 3520 | # Make these background colors instead of foreground | ||
| 3521 | if ( $isBackground ) { | ||
| 3522 | $c *= 16; | ||
| 3523 | } | ||
| 3524 | return $c; | ||
| 3525 | } | ||
| 3526 | |||
| 3527 | =item I<ConsoleHandleIsWritable> | ||
| 3528 | |||
| 3529 | sub ConsoleHandleIsWritable(Int $outErrHandle) : Bool | ||
| 3530 | |||
| 3531 | Checks whether stdout or stderr are writable. Do NOT pass | ||
| 3532 | stdin here. | ||
| 3533 | |||
| 3534 | I<Param>: C<$outErrHandle> is a handle to a file or I/O device (for example | ||
| 3535 | file, console buffer or pipe). The parameter should be created with write | ||
| 3536 | access. | ||
| 3537 | |||
| 3538 | I<Returns>: I<true> if the specified handle is writable, otherwise I<false>. | ||
| 3539 | |||
| 3540 | =cut | ||
| 3541 | |||
| 3542 | sub ConsoleHandleIsWritable { | ||
| 3543 | assert ( @_ == 1 ); | ||
| 3544 | my $outErrHandle = assert_Int shift; | ||
| 3545 | |||
| 3546 | # Do NOT call this method on stdin! | ||
| 3547 | |||
| 3548 | # Windows apps may have non-null valid looking handle values for | ||
| 3549 | # stdin, stdout and stderr, but they may not be readable or | ||
| 3550 | # writable. Verify this by calling WriteFile in the | ||
| 3551 | # appropriate modes. | ||
| 3552 | # This must handle console-less Windows apps. | ||
| 3553 | |||
| 3554 | my $bytesWritten; | ||
| 3555 | my $junkByte = chr 0x41; | ||
| 3556 | # We use our own Windows API call for WriteFile because the Win32API::File | ||
| 3557 | # version provides a different implementation for the use of the third | ||
| 3558 | # parameter (nNumberOfBytesToWrite). | ||
| 3559 | # According to the Windows API, it is intended that the value 0 performs a | ||
| 3560 | # NULL write! | ||
| 3561 | require Win32Native; | ||
| 3562 | my $r = Win32Native::WriteFile($outErrHandle, $junkByte, 0, $bytesWritten, | ||
| 3563 | undef); | ||
| 3564 | # In Win32 apps w/ no console, bResult should be false for failure. | ||
| 3565 | return !!$r; | ||
| 3566 | } | ||
| 3567 | |||
| 3568 | =item I<ConsoleInputHandle> | ||
| 3569 | |||
| 3570 | sub ConsoleInputHandle() : Int | ||
| 3571 | |||
| 3572 | Simplifies the use of I<GetStdHandle(STD_INPUT_HANDLE)>. | ||
| 3573 | |||
| 3574 | I<Returns>: the standard input handle to the standard input device. | ||
| 3575 | |||
| 3576 | =cut | ||
| 3577 | |||
| 3578 | sub ConsoleInputHandle { | ||
| 3579 | assert ( @_ == 0 ); | ||
| 3580 | $_consoleInputHandle //= Win32::Console::_GetStdHandle(STD_INPUT_HANDLE); | ||
| 3581 | return $_consoleInputHandle; | ||
| 3582 | } | ||
| 3583 | |||
| 3584 | =item I<ConsoleOutputHandle> | ||
| 3585 | |||
| 3586 | sub ConsoleOutputHandle() : Int | ||
| 3587 | |||
| 3588 | Simplifies the use of I<GetStdHandle(STD_OUTPUT_HANDLE)>. | ||
| 3589 | |||
| 3590 | I<Returns>: the standard output handle to the standard output device. | ||
| 3591 | |||
| 3592 | =cut | ||
| 3593 | |||
| 3594 | sub ConsoleOutputHandle { | ||
| 3595 | assert ( @_ == 0 ); | ||
| 3596 | $_consoleOutputHandle //= Win32::Console::_GetStdHandle(STD_OUTPUT_HANDLE); | ||
| 3597 | return $_consoleOutputHandle; | ||
| 3598 | } | ||
| 3599 | |||
| 3600 | =item I<GetBufferInfo> | ||
| 3601 | |||
| 3602 | sub GetBufferInfo() : HashRef | ||
| 3603 | sub GetBufferInfo(Bool $throwOnNoConsole, Ref[Bool] $succeeded) : HashRef | ||
| 3604 | |||
| 3605 | Simplifies the use of I<GetConsoleScreenBufferInfo()>. | ||
| 3606 | |||
| 3607 | I<Param>: C<$throwOnNoConsole> must be set to I<true> if an exception is to be | ||
| 3608 | generated in the event of an error and I<false> if an empty input record is to | ||
| 3609 | be returned instead. | ||
| 3610 | |||
| 3611 | I<Param>: C<$succeeded> [out] is I<true> if no error occurred and I<false> if an | ||
| 3612 | error occurred. | ||
| 3613 | |||
| 3614 | I<Returns>: an hash reference with information's about the console. | ||
| 3615 | |||
| 3616 | =cut | ||
| 3617 | |||
| 3618 | sub GetBufferInfo { | ||
| 3619 | state $CONSOLE_SCREEN_BUFFER_INFO = { | ||
| 3620 | dwSize => { | ||
| 3621 | X => 0, | ||
| 3622 | Y => 0, | ||
| 3623 | }, | ||
| 3624 | dwCursorPosition => { | ||
| 3625 | X => 0, | ||
| 3626 | Y => 0, | ||
| 3627 | }, | ||
| 3628 | wAttributes => 0, | ||
| 3629 | srWindow => { | ||
| 3630 | Left => 0, | ||
| 3631 | Top => 0, | ||
| 3632 | Right => 0, | ||
| 3633 | Bottom => 0, | ||
| 3634 | }, | ||
| 3635 | dwMaximumWindowSize => { | ||
| 3636 | X => 0, | ||
| 3637 | Y => 0, | ||
| 3638 | }, | ||
| 3639 | }; | ||
| 3640 | |||
| 3641 | assert ( @_ == 0 || @_ == 2 ); | ||
| 3642 | my $throwOnNoConsole = @_ ? assert_Bool(shift) : TRUE; | ||
| 3643 | my $succeeded = @_ ? do { assert_Bool(${$_[0]}); shift } | ||
| 3644 | : do { my $junk; \$junk }; | ||
| 3645 | |||
| 3646 | $$succeeded = FALSE; | ||
| 3647 | my @csbi; | ||
| 3648 | my $success; | ||
| 3649 | |||
| 3650 | my $hConsole = ConsoleOutputHandle(); | ||
| 3651 | if ( $hConsole == Win32API::File::INVALID_HANDLE_VALUE ) { | ||
| 3652 | if ( !$throwOnNoConsole ) { | ||
| 3653 | return { %$CONSOLE_SCREEN_BUFFER_INFO }; | ||
| 3654 | } | ||
| 3655 | else { | ||
| 3656 | confess("IOException:\n$ResourceString{IO_NoConsole}\n"); | ||
| 3657 | } | ||
| 3658 | } | ||
| 3659 | |||
| 3660 | # Note that if stdout is redirected to a file, the console handle | ||
| 3661 | # may be a file. If this fails, try stderr and stdin. | ||
| 3662 | $success = do { | ||
| 3663 | @csbi = Win32::Console::_GetConsoleScreenBufferInfo($hConsole); | ||
| 3664 | @csbi > 1; | ||
| 3665 | }; | ||
| 3666 | if ( !$success ) { | ||
| 3667 | $success = do { | ||
| 3668 | @csbi = Win32::Console::_GetConsoleScreenBufferInfo( | ||
| 3669 | Win32::Console::_GetStdHandle(STD_ERROR_HANDLE) | ||
| 3670 | ); | ||
| 3671 | @csbi > 1; | ||
| 3672 | }; | ||
| 3673 | if ( !$success ) { | ||
| 3674 | $success = do { | ||
| 3675 | @csbi = Win32::Console::_GetConsoleScreenBufferInfo( | ||
| 3676 | Win32::Console::_GetStdHandle(STD_INPUT_HANDLE) | ||
| 3677 | ); | ||
| 3678 | @csbi > 1; | ||
| 3679 | }; | ||
| 3680 | } | ||
| 3681 | |||
| 3682 | if ( !$success ) { | ||
| 3683 | my $errorCode = Win32::GetLastError(); | ||
| 3684 | if ( $errorCode == Win32Native::ERROR_INVALID_HANDLE | ||
| 3685 | && !$throwOnNoConsole | ||
| 3686 | ) { | ||
| 3687 | return { %$CONSOLE_SCREEN_BUFFER_INFO }; | ||
| 3688 | } | ||
| 3689 | confess(sprintf("WinIOError:\n%s\n", | ||
| 3690 | Win32::FormatMessage($errorCode))); | ||
| 3691 | } | ||
| 3692 | } | ||
| 3693 | |||
| 3694 | if ( !$_haveReadDefaultColors ) { | ||
| 3695 | # Fetch the default foreground and background color for the | ||
| 3696 | # ResetColor method. | ||
| 3697 | $$_defaultColors = $csbi[wAttributes] & 0xff; | ||
| 3698 | $_haveReadDefaultColors = TRUE; | ||
| 3699 | } | ||
| 3700 | |||
| 3701 | $$succeeded = TRUE; | ||
| 3702 | return { | ||
| 3703 | dwSize => { | ||
| 3704 | X => $csbi[dwSizeX], | ||
| 3705 | Y => $csbi[dwSizeY], | ||
| 3706 | }, | ||
| 3707 | dwCursorPosition => { | ||
| 3708 | X => $csbi[dwCursorPositionX], | ||
| 3709 | Y => $csbi[dwCursorPositionY], | ||
| 3710 | }, | ||
| 3711 | wAttributes => $csbi[wAttributes], | ||
| 3712 | srWindow => { | ||
| 3713 | Left => $csbi[srWindowLeft], | ||
| 3714 | Top => $csbi[srWindowTop], | ||
| 3715 | Right => $csbi[srWindowRight], | ||
| 3716 | Bottom => $csbi[srWindowBottom], | ||
| 3717 | }, | ||
| 3718 | dwMaximumWindowSize => { | ||
| 3719 | X => $csbi[dwMaximumWindowSizeX], | ||
| 3720 | Y => $csbi[dwMaximumWindowSizeY], | ||
| 3721 | }, | ||
| 3722 | } | ||
| 3723 | } | ||
| 3724 | |||
| 3725 | =item I<GetStandardFile> | ||
| 3726 | |||
| 3727 | sub GetStandardFile(Int $stdHandleName, Str $access, | ||
| 3728 | Int $bufferSize) : FileHandle | ||
| 3729 | |||
| 3730 | This subroutine is only exposed via methods to get at the console. | ||
| 3731 | We won't use any security checks here. | ||
| 3732 | |||
| 3733 | I<Param>: C<$stdHandleName> specified the standard device (C<STD_INPUT_HANDLE>, | ||
| 3734 | C<STD_OUTPUT_HANDLE> or C<STD_ERROR_HANDLE>). | ||
| 3735 | |||
| 3736 | I<Param>: C<$access> - the possible values of the C<$access> parameter are | ||
| 3737 | system-dependent. See the documentation of L<Win32API::File/"OsFHandleOpen"> | ||
| 3738 | to see which values are available. | ||
| 3739 | |||
| 3740 | I<Param>: C<$bufferSize> buffer size. | ||
| 3741 | |||
| 3742 | I<Returns>: a I<FileHandle> of the specified standard device | ||
| 3743 | (C<STD_INPUT_HANDLE>, C<STD_OUTPUT_HANDLE> or C<STD_ERROR_HANDLE>) or | ||
| 3744 | L<IO::Null> in the event of an error. | ||
| 3745 | |||
| 3746 | =cut | ||
| 3747 | |||
| 3748 | sub GetStandardFile { | ||
| 3749 | assert ( @_ == 3 ); | ||
| 3750 | my $stdHandleName = assert_Int shift; | ||
| 3751 | my $access = assert_Str shift; | ||
| 3752 | my $bufferSize = assert_Int shift; | ||
| 3753 | |||
| 3754 | # We shouldn't close the handle for stdout, etc, or we'll break | ||
| 3755 | # unmanaged code in the process that will print to console. | ||
| 3756 | # We should have a better way of marking this on SafeHandle. | ||
| 3757 | my $handle = Win32::Console::_GetStdHandle($stdHandleName); | ||
| 3758 | |||
| 3759 | # If someone launches a managed process via CreateProcess, stdout | ||
| 3760 | # stderr, & stdin could independently be set to INVALID_HANDLE_VALUE. | ||
| 3761 | # Additionally they might use 0 as an invalid handle. | ||
| 3762 | if ( !$handle || $handle == Win32API::File::INVALID_HANDLE_VALUE ) { | ||
| 3763 | return IO::Null->new(); | ||
| 3764 | } | ||
| 3765 | |||
| 3766 | # Check whether we can read or write to this handle. | ||
| 3767 | if ( $stdHandleName != STD_INPUT_HANDLE | ||
| 3768 | && !ConsoleHandleIsWritable($handle) | ||
| 3769 | ) { | ||
| 3770 | # Win32::OutputDebugString(sprintf("Console::ConsoleHandleIsValid for ". | ||
| 3771 | # "std handle %ld failed, setting it to a null stream", | ||
| 3772 | # $stdHandleName)) if _DEBUG; | ||
| 3773 | return IO::Null->new(); | ||
| 3774 | } | ||
| 3775 | |||
| 3776 | my $useFileAPIs = GetUseFileAPIs($stdHandleName); | ||
| 3777 | |||
| 3778 | # Win32::OutputDebugString(sprintf("Console::GetStandardFile for std ". | ||
| 3779 | # "handle %ld succeeded, returning handle number %d", | ||
| 3780 | # $stdHandleName, $handle)) if _DEBUG; | ||
| 3781 | my $console = IO::Handle->new(); | ||
| 3782 | my $sh = SafeFileHandle($console, FALSE); | ||
| 3783 | if ( !Win32API::File::OsFHandleOpen($sh, $handle, $access) ) { | ||
| 3784 | return IO::Null->new(); | ||
| 3785 | } | ||
| 3786 | # Do not buffer console streams, or we can get into situations where | ||
| 3787 | # we end up blocking waiting for you to hit enter twice. It was | ||
| 3788 | # redundant. | ||
| 3789 | return $console; | ||
| 3790 | } | ||
| 3791 | |||
| 3792 | =item I<GetUseFileAPIs> | ||
| 3793 | |||
| 3794 | sub GetUseFileAPIs(Int $handleType) : Bool | ||
| 3795 | |||
| 3796 | This subroutine checks whether the file API should be used. | ||
| 3797 | |||
| 3798 | I<Param>: C<$handleType> specified the standard device (C<STD_INPUT_HANDLE>, | ||
| 3799 | C<STD_OUTPUT_HANDLE> or C<STD_ERROR_HANDLE>). | ||
| 3800 | |||
| 3801 | I<Returns>: I<true> if the specified handle should use the Window File API for | ||
| 3802 | console access, or I<false> if the Windows Console API should rather be used. | ||
| 3803 | |||
| 3804 | =cut | ||
| 3805 | |||
| 3806 | sub GetUseFileAPIs { | ||
| 3807 | assert ( @_ == 1 ); | ||
| 3808 | my $handleType = assert_Int shift; | ||
| 3809 | |||
| 3810 | switch: for ($handleType) { | ||
| 3811 | |||
| 3812 | case: $_ == STD_INPUT_HANDLE and | ||
| 3813 | return !IsStandardConsoleUnicodeEncoding(__PACKAGE__->InputEncoding) | ||
| 3814 | || __PACKAGE__->IsInputRedirected; | ||
| 3815 | |||
| 3816 | case: $_ == STD_OUTPUT_HANDLE and | ||
| 3817 | return !IsStandardConsoleUnicodeEncoding(__PACKAGE__->OutputEncoding) | ||
| 3818 | || __PACKAGE__->IsOutputRedirected; | ||
| 3819 | |||
| 3820 | case: $_ == STD_ERROR_HANDLE and | ||
| 3821 | return !IsStandardConsoleUnicodeEncoding(__PACKAGE__->OutputEncoding) | ||
| 3822 | || __PACKAGE__->IsErrorRedirected; | ||
| 3823 | |||
| 3824 | default: { | ||
| 3825 | # This can never happen. | ||
| 3826 | confess("Unexpected handleType value ($handleType)") if STRICT; | ||
| 3827 | return TRUE; | ||
| 3828 | } | ||
| 3829 | } | ||
| 3830 | } | ||
| 3831 | |||
| 3832 | =item I<InitializeStdOutError> | ||
| 3833 | |||
| 3834 | sub InitializeStdOutError(Bool $stdout) | ||
| 3835 | |||
| 3836 | Initialization of standard output or standard error handle. | ||
| 3837 | |||
| 3838 | I<Param>: C<$stdout> is I<true> if a standard output handle is to be | ||
| 3839 | initialized and I<false> if a standard error handle is to be initialized. | ||
| 3840 | |||
| 3841 | =cut | ||
| 3842 | |||
| 3843 | # For console apps, the console handles are set to values like 3, 7, | ||
| 3844 | # and 11 OR if you've been created via CreateProcess, possibly -1 | ||
| 3845 | # or 0. -1 is definitely invalid, while 0 is probably invalid. | ||
| 3846 | # Also note each handle can independently be invalid or good. | ||
| 3847 | # For Windows apps, the console handles are set to values like 3, 7, | ||
| 3848 | # and 11 but are invalid handles - you may not write to them. However, | ||
| 3849 | # you can still spawn a Windows app via CreateProcess and read stdout | ||
| 3850 | # and stderr. | ||
| 3851 | # So, we always need to check each handle independently for validity | ||
| 3852 | # by trying to write or read to it, unless it is -1. | ||
| 3853 | |||
| 3854 | # We do not do a security check here, under the assumption that this | ||
| 3855 | # cannot create a security hole, but only waste a user's time or | ||
| 3856 | # cause a possible denial of service attack. | ||
| 3857 | sub InitializeStdOutError { | ||
| 3858 | assert ( @_ == 1 ); | ||
| 3859 | my $stdout = assert_Bool shift; | ||
| 3860 | |||
| 3861 | # Set up Console->Out or Console->Error. | ||
| 3862 | { | ||
| 3863 | lock($InternalSyncObject); | ||
| 3864 | if ( $stdout && $_out ) { | ||
| 3865 | return; | ||
| 3866 | } elsif ( !$stdout && $_error ) { | ||
| 3867 | return; | ||
| 3868 | } | ||
| 3869 | |||
| 3870 | my $writer; | ||
| 3871 | my $s; | ||
| 3872 | if ( $stdout ) { | ||
| 3873 | $s = __PACKAGE__->OpenStandardOutput(DefaultConsoleBufferSize); | ||
| 3874 | } else { | ||
| 3875 | $s = __PACKAGE__->OpenStandardError(DefaultConsoleBufferSize); | ||
| 3876 | } | ||
| 3877 | |||
| 3878 | if ( !$s ) { | ||
| 3879 | if ( _DEBUG && CheckOutputDebug() ) { | ||
| 3880 | $writer = MakeDebugOutputTextWriter($stdout | ||
| 3881 | ? "Console->Out: " | ||
| 3882 | : "Console->Error: " | ||
| 3883 | ); | ||
| 3884 | } else { | ||
| 3885 | $writer = IO::Null->new(); | ||
| 3886 | } | ||
| 3887 | } | ||
| 3888 | else { | ||
| 3889 | my $encoding = __PACKAGE__->OutputEncoding; | ||
| 3890 | my $cpi = $encoding->$CodePage(); | ||
| 3891 | $cpi ||= $stdout ? Win32::GetConsoleOutputCP() : Win32::GetACP(); | ||
| 3892 | my $stdxxx = IO::File->new_from_fd(fileno($s), 'w'); | ||
| 3893 | $stdxxx->binmode(":encoding(cp$cpi)"); | ||
| 3894 | $stdxxx->autoflush(TRUE); | ||
| 3895 | $writer = $stdxxx; | ||
| 3896 | } | ||
| 3897 | if ( $stdout ) { | ||
| 3898 | $_out = $writer; | ||
| 3899 | } else { | ||
| 3900 | $_error = $writer; | ||
| 3901 | } | ||
| 3902 | assert ( $stdout && $_out || !$stdout && $_error | ||
| 3903 | or ~- warn "Didn't set Console::_out or _error appropriately!" | ||
| 3904 | ); | ||
| 3905 | } | ||
| 3906 | return; | ||
| 3907 | } | ||
| 3908 | |||
| 3909 | =item I<IsAltKeyDown> | ||
| 3910 | |||
| 3911 | sub IsAltKeyDown(ArrayRef $ir) : Bool | ||
| 3912 | |||
| 3913 | For tracking Alt+NumPad unicode key sequence. | ||
| 3914 | |||
| 3915 | I<Param>: C<$ir> is an array reference to a KeyEvent input record. | ||
| 3916 | |||
| 3917 | I<Returns>: I<true> if C<Alt> key is pressed, otherwise I<false>. | ||
| 3918 | |||
| 3919 | =cut | ||
| 3920 | |||
| 3921 | # For tracking Alt+NumPad unicode key sequence. When you press Alt key down | ||
| 3922 | # and press a numpad unicode decimal sequence and then release Alt key, the | ||
| 3923 | # desired effect is to translate the sequence into one Unicode KeyPress. | ||
| 3924 | # We need to keep track of the Alt+NumPad sequence and surface the final | ||
| 3925 | # unicode char alone when the Alt key is released. | ||
| 3926 | sub IsAltKeyDown { | ||
| 3927 | assert ( @_ == 1 ); | ||
| 3928 | my $ir = assert_ArrayRef shift; | ||
| 3929 | |||
| 3930 | return ($ir->[controlKeyState] | ||
| 3931 | & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) != 0; | ||
| 3932 | } | ||
| 3933 | |||
| 3934 | =item I<IsHandleRedirected> | ||
| 3935 | |||
| 3936 | sub IsHandleRedirected(Int $ioHandle) : Bool | ||
| 3937 | |||
| 3938 | Detects if a console handle has been redirected. | ||
| 3939 | |||
| 3940 | I<Param>: C<$ioHandle> is a Windows IO handle (for example a handle of a file, | ||
| 3941 | a console or a pipe). | ||
| 3942 | |||
| 3943 | I<Returns>: I<true> if the specified handle is redirected, otherwise I<false>. | ||
| 3944 | |||
| 3945 | =cut | ||
| 3946 | |||
| 3947 | sub IsHandleRedirected { | ||
| 3948 | assert ( @_ == 1 ); | ||
| 3949 | my $ioHandle = assert_Int shift; | ||
| 3950 | |||
| 3951 | assert ( $ioHandle ); | ||
| 3952 | assert ( $ioHandle != Win32API::File::INVALID_HANDLE_VALUE ); | ||
| 3953 | |||
| 3954 | # If handle is not to a character device, we must be redirected: | ||
| 3955 | my $fileType = Win32API::File::GetFileType($ioHandle) // 0; | ||
| 3956 | if ( ($fileType & Win32API::File::FILE_TYPE_CHAR) | ||
| 3957 | != Win32API::File::FILE_TYPE_CHAR | ||
| 3958 | ) { | ||
| 3959 | return TRUE; | ||
| 3960 | } | ||
| 3961 | |||
| 3962 | # We are on a char device. | ||
| 3963 | # If GetConsoleMode succeeds, we are NOT redirected. | ||
| 3964 | my $mode; | ||
| 3965 | my $success = do { | ||
| 3966 | Win32::SetLastError(0); | ||
| 3967 | $mode = Win32::Console::_GetConsoleMode($ioHandle) || 0; | ||
| 3968 | Win32::GetLastError() == 0; | ||
| 3969 | }; | ||
| 3970 | return !$success; | ||
| 3971 | }; | ||
| 3972 | |||
| 3973 | =item I<IsKeyDownEvent> | ||
| 3974 | |||
| 3975 | sub IsKeyDownEvent(ArrayRef $ir) : Bool | ||
| 3976 | |||
| 3977 | To detect pure KeyDown events. | ||
| 3978 | |||
| 3979 | I<Param>: C<$ir> is an array reference to a KeyEvent input record. | ||
| 3980 | |||
| 3981 | I<Returns>: I<true> on a KeyDown event, otherwise I<false>. | ||
| 3982 | |||
| 3983 | =cut | ||
| 3984 | |||
| 3985 | # Skip non key events. Generally we want to surface only KeyDown event | ||
| 3986 | # and suppress KeyUp event from the same Key press but there are cases | ||
| 3987 | # where the assumption of KeyDown-KeyUp pairing for a given key press | ||
| 3988 | # is invalid. For example in IME Unicode keyboard input, we often see | ||
| 3989 | # only KeyUp until the key is released. | ||
| 3990 | sub IsKeyDownEvent { | ||
| 3991 | assert ( @_ == 1 ); | ||
| 3992 | my $ir = assert_ArrayRef shift; | ||
| 3993 | |||
| 3994 | return $ir->[eventType] == Win32Native::KEY_EVENT && $ir->[keyDown]; | ||
| 3995 | } | ||
| 3996 | |||
| 3997 | =item I<IsModKey> | ||
| 3998 | |||
| 3999 | sub IsModKey(ArrayRef $ir) : Bool | ||
| 4000 | |||
| 4001 | Detects if the KeyEvent uses a mod key. | ||
| 4002 | |||
| 4003 | I<Param>: C<$ir> is an array reference to a KeyEvent input record. | ||
| 4004 | |||
| 4005 | I<Returns>: I<true> if the KeyEvent uses a mod key, otherwise I<false>. | ||
| 4006 | |||
| 4007 | =cut | ||
| 4008 | |||
| 4009 | sub IsModKey { | ||
| 4010 | assert ( @_ == 1 ); | ||
| 4011 | my $ir = assert_ArrayRef shift; | ||
| 4012 | |||
| 4013 | # We should also skip over Shift, Control, and Alt, as well as caps lock. | ||
| 4014 | # Apparently we don't need to check for 0xA0 through 0xA5, which are keys | ||
| 4015 | # like Left Control & Right Control. See the Microsoft 'ConsoleKey' for | ||
| 4016 | # these values. | ||
| 4017 | my $keyCode = $ir->[virtualKeyCode]; | ||
| 4018 | return ($keyCode >= VK_SHIFT && $keyCode <= AltVKCode) | ||
| 4019 | || $keyCode == CapsLockVKCode | ||
| 4020 | || $keyCode == NumberLockVKCode | ||
| 4021 | || $keyCode == VK_SCROLL | ||
| 4022 | } | ||
| 4023 | |||
| 4024 | =item I<IsStandardConsoleUnicodeEncoding> | ||
| 4025 | |||
| 4026 | sub IsStandardConsoleUnicodeEncoding(Object $encoding) : Bool | ||
| 4027 | |||
| 4028 | Test if standard console Unicode encoding is activated. | ||
| 4029 | |||
| 4030 | I<Param>: C<$encoding> contains a L<Encode::Encoding> object. | ||
| 4031 | |||
| 4032 | I<Returns>: I<true> if the encoding uses a Windows Unicode encoding or I<false> | ||
| 4033 | if not. | ||
| 4034 | |||
| 4035 | =cut | ||
| 4036 | |||
| 4037 | # We cannot simply compare the encoding to Encode::Unicode because it | ||
| 4038 | # incorporates BOM and we do not care about BOM. Instead, we compare by | ||
| 4039 | # class, codepage and little-endianess only: | ||
| 4040 | sub IsStandardConsoleUnicodeEncoding { | ||
| 4041 | assert ( @_ == 1 ); | ||
| 4042 | my $encoding = assert_Object shift; | ||
| 4043 | |||
| 4044 | my $enc = $encoding->isa('Encode::Unicode') ? $encoding : undef; | ||
| 4045 | return FALSE if !$enc; | ||
| 4046 | |||
| 4047 | return StdConUnicodeEncoding->name eq $enc->name | ||
| 4048 | && StdConUnicodeEncoding->{endian} eq $enc->{endian}; | ||
| 4049 | } | ||
| 4050 | |||
| 4051 | =item I<MakeDebugOutputTextWriter> | ||
| 4052 | |||
| 4053 | sub MakeDebugOutputTextWriter(Str $streamLabel) : IO::Handle | ||
| 4054 | |||
| 4055 | Creates an I<IO::DebugOutputTextWriter> object (derived from L<IO::Handle>) | ||
| 4056 | and returns it. | ||
| 4057 | |||
| 4058 | I<Param>: C<$streamLabel> contains a string which is prefixed to each output. | ||
| 4059 | |||
| 4060 | I<Returns>: of an L<IO::Handle> of type I<IO::DebugOutputTextWriter>. | ||
| 4061 | |||
| 4062 | =cut | ||
| 4063 | |||
| 4064 | sub MakeDebugOutputTextWriter { | ||
| 4065 | require IO::DebugOutputTextWriter; | ||
| 4066 | assert ( @_ == 1 ); | ||
| 4067 | my $streamLabel = assert_Str shift; | ||
| 4068 | my $output = IO::DebugOutputTextWriter->new($streamLabel); | ||
| 4069 | $output->print("Output redirected to debugger from a bit bucket."); | ||
| 4070 | return $output; | ||
| 4071 | } | ||
| 4072 | |||
| 4073 | =item I<SafeFileHandle> | ||
| 4074 | |||
| 4075 | sub SafeFileHandle(FileHandle $preexistingHandle, | ||
| 4076 | Bool $ownsHandle) : FileHandle; | ||
| 4077 | |||
| 4078 | Create a reference to safe an existing file handle. | ||
| 4079 | |||
| 4080 | I<Param>: C<$preexistingHandle> is an I<FileHandle> that represents the | ||
| 4081 | pre-existing file handle to use. | ||
| 4082 | |||
| 4083 | I<Param>: C<$ownsHandle> should be set to I<true> to reliably release the file | ||
| 4084 | handle during the closing phase; I<false> to prevent release. | ||
| 4085 | |||
| 4086 | I<Returns>: the specified I<FileHandle>. | ||
| 4087 | |||
| 4088 | =cut | ||
| 4089 | |||
| 4090 | sub SafeFileHandle { | ||
| 4091 | assert ( @_ == 2 ); | ||
| 4092 | my $preexistingHandle = assert_FileHandle shift; | ||
| 4093 | my $ownsHandle = assert_Bool shift; | ||
| 4094 | |||
| 4095 | my $hNativeHandle = Win32API::File::GetOsFHandle($preexistingHandle); | ||
| 4096 | if ( $hNativeHandle | ||
| 4097 | && $hNativeHandle != Win32API::File::INVALID_HANDLE_VALUE | ||
| 4098 | ) { | ||
| 4099 | my $ouFlags = 0; | ||
| 4100 | if ( Win32API::File::GetHandleInformation($hNativeHandle, $ouFlags) | ||
| 4101 | && $ouFlags & Win32API::File::HANDLE_FLAG_PROTECT_FROM_CLOSE | ||
| 4102 | ) { | ||
| 4103 | $ownsHandle = FALSE; | ||
| 4104 | } | ||
| 4105 | } | ||
| 4106 | if ( !$ownsHandle ) { | ||
| 4107 | $_leaveOpen->{$preexistingHandle} = $preexistingHandle; | ||
| 4108 | } else { | ||
| 4109 | delete $_leaveOpen->{$preexistingHandle}; | ||
| 4110 | } | ||
| 4111 | return $preexistingHandle; | ||
| 4112 | } | ||
| 4113 | |||
| 4114 | =end private | ||
| 4115 | |||
| 4116 | =back | ||
| 4117 | |||
| 4118 | =cut | ||
| 4119 | |||
| 4120 | use namespace::clean; | ||
| 4121 | |||
| 4122 | =head2 Inheritance | ||
| 4123 | |||
| 4124 | Methods inherited from class L<UNIVERSAL> | ||
| 4125 | |||
| 4126 | can, DOES, isa, VERSION | ||
| 4127 | |||
| 4128 | =cut | ||
| 4129 | |||
| 4130 | } | ||
| 4131 | |||
| 4132 | 1; | ||
| 4133 | |||
| 4134 | # ------------------------------------------------------------------------ | ||
| 4135 | # Additional Packages ---------------------------------------------------- | ||
| 4136 | # ------------------------------------------------------------------------ | ||
| 4137 | |||
| 4138 | # see SYNOPSIS using this code | ||
| 4139 | #------- | ||
| 4140 | package # hidden from CPAN | ||
| 4141 | System { | ||
| 4142 | #------- | ||
| 4143 | use strict; | ||
| 4144 | use warnings; | ||
| 4145 | use Exporter qw( import ); | ||
| 4146 | our @EXPORT = qw( Console ); | ||
| 4147 | sub Console() { | ||
| 4148 | require Win32::Console::DotNet; | ||
| 4149 | state $instance = Win32::Console::DotNet->instance(); | ||
| 4150 | } | ||
| 4151 | $INC{'System.pm'} = 1; | ||
| 4152 | } | ||
| 4153 | |||
| 4154 | # see Utilapiset.h and Winuser.h documentation for Beep() and GetKeyState() | ||
| 4155 | #------------ | ||
| 4156 | package # hidden from CPAN | ||
| 4157 | Win32Native { | ||
| 4158 | #------------ | ||
| 4159 | use strict; | ||
| 4160 | use warnings; | ||
| 4161 | use English qw( -no_match_vars ); | ||
| 4162 | use Win32::API; | ||
| 4163 | use constant { | ||
| 4164 | KERNEL32 => 'kernel32', | ||
| 4165 | USER32 => 'user32', | ||
| 4166 | }; | ||
| 4167 | BEGIN { | ||
| 4168 | Win32::API::More->Import(KERNEL32, | ||
| 4169 | 'BOOL Beep(DWORD dwFreq, DWORD dwDuration)' | ||
| 4170 | ) or die "Import Beep: $EXTENDED_OS_ERROR"; | ||
| 4171 | Win32::API::More->Import(USER32, | ||
| 4172 | 'int GetKeyState(int nVirtKey)' | ||
| 4173 | ) or die "Import GetKeyState: $EXTENDED_OS_ERROR"; | ||
| 4174 | Win32::API::More->Import(KERNEL32, | ||
| 4175 | 'BOOL WriteFile( | ||
| 4176 | HANDLE hFile, | ||
| 4177 | LPCSTR lpBuffer, | ||
| 4178 | DWORD nNumberOfBytesToWrite, | ||
| 4179 | LPDWORD lpNumberOfBytesWritten, | ||
| 4180 | LPVOID lpOverlapped | ||
| 4181 | )' | ||
| 4182 | ) or die "Import WriteFile: $EXTENDED_OS_ERROR"; | ||
| 4183 | } | ||
| 4184 | $INC{'Win32Native.pm'} = 1; | ||
| 4185 | } | ||
| 4186 | |||
| 4187 | # Most of the content was taken from L</IO::Null>, L</IO::String> and | ||
| 4188 | # I<system.io.__debugoutputtextwriter.cs> | ||
| 4189 | #-------------------------- | ||
| 4190 | package # hidden from CPAN | ||
| 4191 | IO::DebugOutputTextWriter { | ||
| 4192 | #-------------------------- | ||
| 4193 | use strict; | ||
| 4194 | use warnings; | ||
| 4195 | use Symbol (); | ||
| 4196 | use IO::Handle (); | ||
| 4197 | use Win32; | ||
| 4198 | our @ISA = qw(IO::Handle); | ||
| 4199 | |||
| 4200 | *CLOSE = *WRITE = | ||
| 4201 | *close = *write = | ||
| 4202 | *opened = *eof = *syswrite = *ungetc = *clearerr = *flush = | ||
| 4203 | *binmode = sub { 1 }; | ||
| 4204 | |||
| 4205 | *TELL = *FILENO = | ||
| 4206 | *tell = *fileno = sub { -1 }; | ||
| 4207 | |||
| 4208 | *GETC = *READ = | ||
| 4209 | *getc = *read = *sysread = *error = *getline = sub { '' }; | ||
| 4210 | |||
| 4211 | sub readline { | ||
| 4212 | return () if wantarray; | ||
| 4213 | return ''; | ||
| 4214 | } | ||
| 4215 | *READLINE = \&readline; | ||
| 4216 | |||
| 4217 | sub getlines { return () } | ||
| 4218 | sub DESTROY { 1 } | ||
| 4219 | |||
| 4220 | sub new { # $handle ($class, | $consoleType) | ||
| 4221 | my $class = shift; | ||
| 4222 | my $self = bless Symbol::gensym(), ref($class) || $class; | ||
| 4223 | tie *$self, $self; | ||
| 4224 | $self->open(@_); | ||
| 4225 | return $self; | ||
| 4226 | } | ||
| 4227 | *new_from_fd = *fdopen = \&new; | ||
| 4228 | |||
| 4229 | sub open { # $handle ($handle, | $consoleType) | ||
| 4230 | my $self = shift; | ||
| 4231 | return $self->new(@_) unless ref($self); | ||
| 4232 | my $consoleType = shift // ''; | ||
| 4233 | *$self->{_consoleType} = "$consoleType"; | ||
| 4234 | return $self; | ||
| 4235 | } | ||
| 4236 | *OPEN = \&open; | ||
| 4237 | |||
| 4238 | sub print { # $success ($handle, @list) | ||
| 4239 | return undef unless ref(shift); | ||
| 4240 | Win32::OutputDebugString(join $,//'', @_); | ||
| 4241 | return 1; | ||
| 4242 | } | ||
| 4243 | *PRINT = \&print; | ||
| 4244 | |||
| 4245 | sub printf { # $success ($handle, $format, @list) | ||
| 4246 | return undef unless ref(shift); | ||
| 4247 | Win32::OutputDebugString(sprintf(shift, @_)); | ||
| 4248 | return 1; | ||
| 4249 | } | ||
| 4250 | *PRINTF = \&printf; | ||
| 4251 | |||
| 4252 | sub say { # $success ($handle, @list) | ||
| 4253 | my $self = shift; | ||
| 4254 | return undef unless ref($self); | ||
| 4255 | if ( defined $_[0] ) { | ||
| 4256 | my $consoleType = *$self->{_consoleType} // ''; | ||
| 4257 | Win32::OutputDebugString(join $,//'', $consoleType, @_); | ||
| 4258 | } else { | ||
| 4259 | Win32::OutputDebugString('<null>'); | ||
| 4260 | } | ||
| 4261 | Win32::OutputDebugString("\n"); | ||
| 4262 | return 1; | ||
| 4263 | } | ||
| 4264 | |||
| 4265 | sub TIEHANDLE { | ||
| 4266 | return $_[0] if ref($_[0]); | ||
| 4267 | my $class = shift; | ||
| 4268 | my $self = bless Symbol::gensym(), $class; | ||
| 4269 | $self->open(@_); | ||
| 4270 | return $self; | ||
| 4271 | } | ||
| 4272 | |||
| 4273 | $INC{'IO/DebugOutputTextWriter.pm'} = 1; | ||
| 4274 | } | ||
| 4275 | |||
| 4276 | # Specifies constants that define foreground and background colors for the | ||
| 4277 | # console. | ||
| 4278 | #--------------------- | ||
| 4279 | package ConsoleColor { | ||
| 4280 | #--------------------- | ||
| 4281 | use strict; | ||
| 4282 | use warnings; | ||
| 4283 | use constant _enum => qw( | ||
| 4284 | Black | ||
| 4285 | DarkBlue | ||
| 4286 | DarkGreen | ||
| 4287 | DarkCyan | ||
| 4288 | DarkRed | ||
| 4289 | DarkMagenta | ||
| 4290 | DarkYellow | ||
| 4291 | Gray | ||
| 4292 | DarkGray | ||
| 4293 | Blue | ||
| 4294 | Green | ||
| 4295 | Cyan | ||
| 4296 | Red | ||
| 4297 | Magenta | ||
| 4298 | Yellow | ||
| 4299 | White | ||
| 4300 | ); | ||
| 4301 | BEGIN { | ||
| 4302 | eval "use constant (_enum)[$_] => $_;" foreach 0..(_enum)-1; | ||
| 4303 | } | ||
| 4304 | # Returns all valid names (Str) in the enumeration as a list. | ||
| 4305 | use constant elements => grep {defined} _enum; | ||
| 4306 | # Returns a list of all values (Int) of the enumeration. | ||
| 4307 | use constant values => grep {defined((_enum)[$_])} 0..(_enum)-1; | ||
| 4308 | # Returns the number of elements (Int) in the array. | ||
| 4309 | use constant count => +grep {defined} _enum; | ||
| 4310 | # Returns an element (Str) of the array by it's index (Int). You can also | ||
| 4311 | # use negative index numbers, just as with Perl's core array handling. | ||
| 4312 | # If the specified element does not exist, this will return undef. | ||
| 4313 | sub get { # $name (|$class, $value) | ||
| 4314 | shift if @_ > 1 && defined($_[0]) && $_[0] eq __PACKAGE__; | ||
| 4315 | my $key = shift // return; | ||
| 4316 | (_enum)[$key]; | ||
| 4317 | }; | ||
| 4318 | $INC{'ConsoleColor.pm'} = 1; | ||
| 4319 | } | ||
| 4320 | |||
| 4321 | # Specifies the standard keys on a console. | ||
| 4322 | #------------------- | ||
| 4323 | package ConsoleKey { | ||
| 4324 | #------------------- | ||
| 4325 | use strict; | ||
| 4326 | use warnings; | ||
| 4327 | use constant _enum => qw( | ||
| 4328 | None | ||
| 4329 | ), (undef) x 7, qw( | ||
| 4330 | Backspace | ||
| 4331 | Tab | ||
| 4332 | ), (undef) x 2, qw( | ||
| 4333 | Clear | ||
| 4334 | Enter | ||
| 4335 | ), (undef) x 5, qw( | ||
| 4336 | Pause | ||
| 4337 | ), (undef) x 7, qw( | ||
| 4338 | Escape | ||
| 4339 | ), (undef) x 4, qw( | ||
| 4340 | Spacebar | ||
| 4341 | PageUp | ||
| 4342 | PageDown | ||
| 4343 | End | ||
| 4344 | Home | ||
| 4345 | LeftArrow | ||
| 4346 | UpArrow | ||
| 4347 | RightArrow | ||
| 4348 | DownArrow | ||
| 4349 | Select | ||
| 4350 | |||
| 4351 | Execute | ||
| 4352 | PrintScreen | ||
| 4353 | Insert | ||
| 4354 | Delete | ||
| 4355 | Help | ||
| 4356 | D0 | ||
| 4357 | D1 | ||
| 4358 | D2 | ||
| 4359 | D3 | ||
| 4360 | D4 | ||
| 4361 | D5 | ||
| 4362 | D6 | ||
| 4363 | D7 | ||
| 4364 | D8 | ||
| 4365 | D9 | ||
| 4366 | ), (undef) x 7, qw( | ||
| 4367 | A | ||
| 4368 | B | ||
| 4369 | C | ||
| 4370 | D | ||
| 4371 | E | ||
| 4372 | F | ||
| 4373 | G | ||
| 4374 | H | ||
| 4375 | I | ||
| 4376 | J | ||
| 4377 | K | ||
| 4378 | L | ||
| 4379 | M | ||
| 4380 | N | ||
| 4381 | O | ||
| 4382 | P | ||
| 4383 | Q | ||
| 4384 | R | ||
| 4385 | S | ||
| 4386 | T | ||
| 4387 | U | ||
| 4388 | V | ||
| 4389 | W | ||
| 4390 | X | ||
| 4391 | Y | ||
| 4392 | Z | ||
| 4393 | LeftWindows | ||
| 4394 | RightWindows | ||
| 4395 | Applications | ||
| 4396 | ), (undef) x 1, qw( | ||
| 4397 | Sleep | ||
| 4398 | NumPad0 | ||
| 4399 | NumPad1 | ||
| 4400 | NumPad2 | ||
| 4401 | NumPad3 | ||
| 4402 | NumPad4 | ||
| 4403 | NumPad5 | ||
| 4404 | NumPad6 | ||
| 4405 | NumPad7 | ||
| 4406 | NumPad8 | ||
| 4407 | NumPad9 | ||
| 4408 | Multiply | ||
| 4409 | Add | ||
| 4410 | Separator | ||
| 4411 | Subtract | ||
| 4412 | Decimal | ||
| 4413 | Divide | ||
| 4414 | F1 | ||
| 4415 | F2 | ||
| 4416 | F3 | ||
| 4417 | F4 | ||
| 4418 | F5 | ||
| 4419 | F6 | ||
| 4420 | F7 | ||
| 4421 | F8 | ||
| 4422 | F9 | ||
| 4423 | F10 | ||
| 4424 | F11 | ||
| 4425 | F12 | ||
| 4426 | F13 | ||
| 4427 | F14 | ||
| 4428 | F15 | ||
| 4429 | F16 | ||
| 4430 | F17 | ||
| 4431 | F18 | ||
| 4432 | F19 | ||
| 4433 | F20 | ||
| 4434 | F21 | ||
| 4435 | F22 | ||
| 4436 | F23 | ||
| 4437 | F24 | ||
| 4438 | ), (undef) x 30, qw( | ||
| 4439 | BrowserBack | ||
| 4440 | BrowserForward | ||
| 4441 | BrowserRefresh | ||
| 4442 | BrowserStop | ||
| 4443 | BrowserSearch | ||
| 4444 | BrowserFavorites | ||
| 4445 | BrowserHome | ||
| 4446 | VolumeMute | ||
| 4447 | VolumeDown | ||
| 4448 | VolumeUp | ||
| 4449 | MediaNext | ||
| 4450 | MediaPrevious | ||
| 4451 | MediaStop | ||
| 4452 | MediaPlay | ||
| 4453 | LaunchMail | ||
| 4454 | LaunchMediaSelect | ||
| 4455 | LaunchApp1 | ||
| 4456 | LaunchApp2 | ||
| 4457 | ), (undef) x 2, qw( | ||
| 4458 | Oem1 | ||
| 4459 | OemPlus | ||
| 4460 | OemComma | ||
| 4461 | OemMinus | ||
| 4462 | OemPeriod | ||
| 4463 | Oem2 | ||
| 4464 | Oem3 | ||
| 4465 | ), (undef) x 26, qw( | ||
| 4466 | Oem4 | ||
| 4467 | Oem5 | ||
| 4468 | Oem6 | ||
| 4469 | Oem7 | ||
| 4470 | Oem8 | ||
| 4471 | ), (undef) x 2, qw( | ||
| 4472 | Oem102 | ||
| 4473 | ), (undef) x 2, qw( | ||
| 4474 | Process | ||
| 4475 | ), (undef) x 1, qw( | ||
| 4476 | Packet | ||
| 4477 | ), (undef) x 14, qw( | ||
| 4478 | Attention | ||
| 4479 | CrSel | ||
| 4480 | ExSel | ||
| 4481 | EraseEndOfFile | ||
| 4482 | Play | ||
| 4483 | Zoom | ||
| 4484 | NoName | ||
| 4485 | Pa1 | ||
| 4486 | OemClear | ||
| 4487 | ); | ||
| 4488 | BEGIN { | ||
| 4489 | eval "use constant (_enum)[$_] => $_;" foreach 0..(_enum)-1; | ||
| 4490 | } | ||
| 4491 | use constant elements => grep {defined} _enum; | ||
| 4492 | use constant values => grep {defined((_enum)[$_])} 0..(_enum)-1; | ||
| 4493 | use constant count => +grep {defined} _enum; | ||
| 4494 | sub get { # $name (|$class, $value) | ||
| 4495 | shift if @_ > 1 && defined($_[0]) && $_[0] eq __PACKAGE__; | ||
| 4496 | my $key = shift // return; | ||
| 4497 | (_enum)[$key]; | ||
| 4498 | }; | ||
| 4499 | $INC{'ConsoleKey.pm'} = 1; | ||
| 4500 | } | ||
| 4501 | |||
| 4502 | # Describes the console key that was pressed, including the character | ||
| 4503 | # represented by the console key and the state of the SHIFT, ALT, and CTRL | ||
| 4504 | # modifier keys. | ||
| 4505 | #----------------------- | ||
| 4506 | package ConsoleKeyInfo { | ||
| 4507 | #----------------------- | ||
| 4508 | use strict; | ||
| 4509 | use warnings; | ||
| 4510 | use Carp qw( confess ); | ||
| 4511 | use Data::Dumper; | ||
| 4512 | use Scalar::Util qw( looks_like_number ); | ||
| 4513 | |||
| 4514 | sub new { # $object ($class, \%arg | @args) | ||
| 4515 | my $class = shift; | ||
| 4516 | return unless $class && (@_ == 0 || @_ == 1 || @_ == 5); | ||
| 4517 | my $self; | ||
| 4518 | if ( @_ == 0 ) { | ||
| 4519 | $self = { | ||
| 4520 | KeyChar => "\0", | ||
| 4521 | Key => 0, | ||
| 4522 | Modifiers => 0, | ||
| 4523 | } | ||
| 4524 | } elsif ( @_ == 1 ) { | ||
| 4525 | return unless ref($_[0]) | ||
| 4526 | && length($_[0]->{KeyChar}) | ||
| 4527 | && looks_like_number($_[0]->{Key}) | ||
| 4528 | && looks_like_number($_[0]->{Modifiers}); | ||
| 4529 | $self = $_[0]; | ||
| 4530 | } else { | ||
| 4531 | return unless length($_[0]) | ||
| 4532 | && looks_like_number($_[1]); | ||
| 4533 | $self = { | ||
| 4534 | KeyChar => $_[0], | ||
| 4535 | Key => $_[1], | ||
| 4536 | Modifiers => ($_[2] ? 2 : 0) | ($_[3] ? 1 : 0) | ($_[4] ? 4 : 0), | ||
| 4537 | } | ||
| 4538 | } | ||
| 4539 | confess 'ArgumentOutOfRangeException' unless | ||
| 4540 | $self->{Key} >= 0 && $self->{Key} <= 255; | ||
| 4541 | return bless $self, $class; | ||
| 4542 | } | ||
| 4543 | |||
| 4544 | sub Key { # $key ($self) | ||
| 4545 | my $self = shift; | ||
| 4546 | return if !ref($self) || @_; | ||
| 4547 | return $self->{Key}; | ||
| 4548 | } | ||
| 4549 | |||
| 4550 | sub KeyChar { # $keyChar ($self) | ||
| 4551 | my $self = shift; | ||
| 4552 | return if !ref($self) || @_; | ||
| 4553 | return $self->{KeyChar}; | ||
| 4554 | } | ||
| 4555 | |||
| 4556 | sub Modifiers { # $modifiers ($self) | ||
| 4557 | my $self = shift; | ||
| 4558 | return if !ref($self) || @_; | ||
| 4559 | return $self->{Modifiers}; | ||
| 4560 | } | ||
| 4561 | |||
| 4562 | sub Equals { # $bool ($lhs, $rhs) | ||
| 4563 | my ($lhs, $rhs) = @_; | ||
| 4564 | return unless ref($lhs) && ref($rhs) && @_ == 2; | ||
| 4565 | return ref($lhs) eq ref($rhs) | ||
| 4566 | && $lhs->{Key} == $rhs->{Key} | ||
| 4567 | && $lhs->{KeyChar} eq $rhs->{KeyChar} | ||
| 4568 | && $lhs->{Modifiers} eq $rhs->{Modifiers}; | ||
| 4569 | } | ||
| 4570 | |||
| 4571 | sub ToString { | ||
| 4572 | my $self = shift; | ||
| 4573 | return if !ref($self) || @_; | ||
| 4574 | local $Data::Dumper::Indent = 0; | ||
| 4575 | local $Data::Dumper::Terse = 1; | ||
| 4576 | local $Data::Dumper::Sortkeys = 1; | ||
| 4577 | return Dumper({ %$self }); | ||
| 4578 | } | ||
| 4579 | |||
| 4580 | use overload ( | ||
| 4581 | '""' => sub { ToString(shift) }, | ||
| 4582 | 'eq' => sub { Equals(shift, shift) }, | ||
| 4583 | 'ne' => sub { !Equals(shift, shift) }, | ||
| 4584 | ); | ||
| 4585 | |||
| 4586 | $INC{'ConsoleKeyInfo.pm'} = 1; | ||
| 4587 | } | ||
| 4588 | |||
| 4589 | # Represents the SHIFT, ALT, and CTRL modifier keys on a keyboard. | ||
| 4590 | #------------------------- | ||
| 4591 | package ConsoleModifiers { | ||
| 4592 | #------------------------- | ||
| 4593 | use strict; | ||
| 4594 | use warnings; | ||
| 4595 | use constant _enum => qw( | ||
| 4596 | None | ||
| 4597 | Alt | ||
| 4598 | Shift | ||
| 4599 | ), (undef) x 1, qw( | ||
| 4600 | Control | ||
| 4601 | ); | ||
| 4602 | BEGIN { | ||
| 4603 | eval "use constant (_enum)[$_] => $_;" foreach 0..(_enum)-1; | ||
| 4604 | } | ||
| 4605 | use constant elements => grep {defined} _enum; | ||
| 4606 | use constant values => grep {defined((_enum)[$_])} 0..(_enum)-1; | ||
| 4607 | use constant count => +grep {defined} _enum; | ||
| 4608 | sub get { | ||
| 4609 | shift if @_ > 1 && defined($_[0]) && $_[0] eq __PACKAGE__; | ||
| 4610 | my $key = shift // return; | ||
| 4611 | (_enum)[$key]; | ||
| 4612 | }; | ||
| 4613 | $INC{'ConsoleModifiers.pm'} = 1; | ||
| 4614 | } | ||
| 4615 | |||
| 4616 | __END__ | ||