lib/TUI/Drivers/HardwareInfo/Win32.pm untested

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