Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1468 lines
37 KiB

  1. package Win32::Console;
  2. #######################################################################
  3. #
  4. # Win32::Console - Perl Module for Windows Clipboard Interaction
  5. # ^^^^^^^^^^^^^^
  6. # Version: 0.03 (07 Apr 1997)
  7. #
  8. #######################################################################
  9. require Exporter; # to export the constants to the main:: space
  10. require DynaLoader; # to dynuhlode the module.
  11. @ISA= qw( Exporter DynaLoader );
  12. @EXPORT = qw(
  13. BACKGROUND_BLUE
  14. BACKGROUND_GREEN
  15. BACKGROUND_INTENSITY
  16. BACKGROUND_RED
  17. CAPSLOCK_ON
  18. CONSOLE_TEXTMODE_BUFFER
  19. CTRL_BREAK_EVENT
  20. CTRL_C_EVENT
  21. ENABLE_ECHO_INPUT
  22. ENABLE_LINE_INPUT
  23. ENABLE_MOUSE_INPUT
  24. ENABLE_PROCESSED_INPUT
  25. ENABLE_PROCESSED_OUTPUT
  26. ENABLE_WINDOW_INPUT
  27. ENABLE_WRAP_AT_EOL_OUTPUT
  28. ENHANCED_KEY
  29. FILE_SHARE_READ
  30. FILE_SHARE_WRITE
  31. FOREGROUND_BLUE
  32. FOREGROUND_GREEN
  33. FOREGROUND_INTENSITY
  34. FOREGROUND_RED
  35. LEFT_ALT_PRESSED
  36. LEFT_CTRL_PRESSED
  37. NUMLOCK_ON
  38. GENERIC_READ
  39. GENERIC_WRITE
  40. RIGHT_ALT_PRESSED
  41. RIGHT_CTRL_PRESSED
  42. SCROLLLOCK_ON
  43. SHIFT_PRESSED
  44. STD_INPUT_HANDLE
  45. STD_OUTPUT_HANDLE
  46. STD_ERROR_HANDLE
  47. );
  48. #######################################################################
  49. # This AUTOLOAD is used to 'autoload' constants from the constant()
  50. # XS function. If a constant is not found then control is passed
  51. # to the AUTOLOAD in AutoLoader.
  52. #
  53. sub AUTOLOAD {
  54. my($constname);
  55. ($constname = $AUTOLOAD) =~ s/.*:://;
  56. #reset $! to zero to reset any current errors.
  57. $!=0;
  58. my $val = constant($constname, @_ ? $_[0] : 0);
  59. if ($! != 0) {
  60. # if ($! =~ /Invalid/) {
  61. # $AutoLoader::AUTOLOAD = $AUTOLOAD;
  62. # goto &AutoLoader::AUTOLOAD;
  63. # } else {
  64. ($pack, $file, $line) = caller; undef $pack;
  65. die "Symbol Win32::Console::$constname not defined, used at $file line $line.";
  66. # }
  67. }
  68. eval "sub $AUTOLOAD { $val }";
  69. goto &$AUTOLOAD;
  70. }
  71. #######################################################################
  72. # STATIC OBJECT PROPERTIES
  73. #
  74. $VERSION = "0.03";
  75. # %HandlerRoutineStack = ();
  76. # $HandlerRoutineRegistered = 0;
  77. #######################################################################
  78. # PUBLIC METHODS
  79. #
  80. #======== (MAIN CONSTRUCTOR)
  81. sub new {
  82. #========
  83. my($class, $param1, $param2) = @_;
  84. my $self = {};
  85. if(defined($param1)
  86. and ($param1 == constant("STD_INPUT_HANDLE", 0)
  87. or $param1 == constant("STD_OUTPUT_HANDLE", 0)
  88. or $param1 == constant("STD_ERROR_HANDLE", 0))) {
  89. $self->{'handle'} = _GetStdHandle($param1);
  90. } else {
  91. $param1 = constant("GENERIC_READ", 0) | constant("GENERIC_WRITE", 0) unless $param1;
  92. $param2 = constant("FILE_SHARE_READ", 0) | constant("FILE_SHARE_WRITE", 0) unless $param2;
  93. $self->{'handle'} = _CreateConsoleScreenBuffer($param1, $param2,
  94. constant("CONSOLE_TEXTMODE_BUFFER", 0));
  95. }
  96. bless $self, $class;
  97. return $self;
  98. }
  99. #============
  100. sub Display {
  101. #============
  102. my($self)=@_;
  103. return undef unless ref($self);
  104. return _SetConsoleActiveScreenBuffer($self->{'handle'});
  105. }
  106. #===========
  107. sub Select {
  108. #===========
  109. ($self, $type) = @_;
  110. return undef unless ref($self);
  111. return _SetStdHandle($type, $self->{'handle'});
  112. }
  113. #==========
  114. sub Title {
  115. #==========
  116. my($self, $title) = @_;
  117. $title = $self unless ref($self);
  118. if(defined($title)) {
  119. return _SetConsoleTitle($title);
  120. } else {
  121. return _GetConsoleTitle();
  122. }
  123. }
  124. #==============
  125. sub WriteChar {
  126. #==============
  127. my($self, $text, $col, $row) = @_;
  128. return undef unless ref($self);
  129. return _WriteConsoleOutputCharacter($self->{'handle'},$text,$col,$row);
  130. }
  131. #=============
  132. sub ReadChar {
  133. #=============
  134. my($self, $size, $col, $row) = @_;
  135. return undef unless ref($self);
  136. my $buffer = (" " x $size);
  137. if(_ReadConsoleOutputCharacter($self->{'handle'}, $buffer, $size, $col, $row)) {
  138. return $buffer;
  139. } else {
  140. return undef;
  141. }
  142. }
  143. #==============
  144. sub WriteAttr {
  145. #==============
  146. my($self, $attr, $col, $row) = @_;
  147. return undef unless ref($self);
  148. return _WriteConsoleOutputAttribute($self->{'handle'}, $attr, $col, $row);
  149. }
  150. #=============
  151. sub ReadAttr {
  152. #=============
  153. my($self, $size, $col, $row) = @_;
  154. return undef unless ref($self);
  155. return _ReadConsoleOutputAttribute($self->{'handle'}, $size, $col, $row);
  156. }
  157. #==========
  158. sub Write {
  159. #==========
  160. my($self,$string) = @_;
  161. return undef unless ref($self);
  162. return _WriteConsole($self->{'handle'}, $string);
  163. }
  164. #=============
  165. sub ReadRect {
  166. #=============
  167. my($self, $left, $top, $right, $bottom) = @_;
  168. return undef unless ref($self);
  169. my $col = $right - $left + 1;
  170. my $row = $bottom - $top + 1;
  171. my $buffer = (" " x ($col*$row*4));
  172. if(_ReadConsoleOutput($self->{'handle'}, $buffer,
  173. $col, $row, 0, 0,
  174. $left, $top, $right, $bottom)) {
  175. return $buffer;
  176. } else {
  177. return undef;
  178. }
  179. }
  180. #==============
  181. sub WriteRect {
  182. #==============
  183. my($self, $buffer, $left, $top, $right, $bottom) = @_;
  184. return undef unless ref($self);
  185. my $col = $right - $left + 1;
  186. my $row = $bottom - $top + 1;
  187. return _WriteConsoleOutput($self->{'handle'}, $buffer,
  188. $col, $row, 0, 0,
  189. $left, $top, $right, $bottom);
  190. }
  191. #===========
  192. sub Scroll {
  193. #===========
  194. my($self, $left1, $top1, $right1, $bottom1,
  195. $col, $row, $char, $attr,
  196. $left2, $top2, $right2, $bottom2) = @_;
  197. return undef unless ref($self);
  198. return _ScrollConsoleScreenBuffer($self->{'handle'},
  199. $left1, $top1, $right1, $bottom1,
  200. $col, $row, $char, $attr,
  201. $left2, $top2, $right2, $bottom2);
  202. }
  203. #==============
  204. sub MaxWindow {
  205. #==============
  206. my($self, $flag) = @_;
  207. return undef unless ref($self);
  208. if(not defined($flag)) {
  209. my @info = _GetConsoleScreenBufferInfo($self->{'handle'});
  210. return $info[9], $info[10];
  211. } else {
  212. return _GetLargestConsoleWindowSize($self->{'handle'});
  213. }
  214. }
  215. #=========
  216. sub Info {
  217. #=========
  218. my($self) = @_;
  219. return undef unless ref($self);
  220. return _GetConsoleScreenBufferInfo($self->{'handle'});
  221. }
  222. #===========
  223. sub Window {
  224. #===========
  225. my($self, $flag, $left, $top, $right, $bottom) = @_;
  226. return undef unless ref($self);
  227. if(not defined($flag)) {
  228. my @info = _GetConsoleScreenBufferInfo($self->{'handle'});
  229. return $info[5], $info[6], $info[7], $info[8];
  230. } else {
  231. return _SetConsoleWindowInfo($self->{'handle'}, $flag, $left, $top, $right, $bottom);
  232. }
  233. }
  234. #==============
  235. sub GetEvents {
  236. #==============
  237. my $self="";
  238. ($self)=@_;
  239. return undef unless ref($self);
  240. return _GetNumberOfConsoleInputEvents($self->{'handle'});
  241. }
  242. #==========
  243. sub Flush {
  244. #==========
  245. my($self) = @_;
  246. return undef unless ref($self);
  247. return _FlushConsoleInputBuffer($self->{'handle'});
  248. }
  249. #==============
  250. sub InputChar {
  251. #==============
  252. my($self, $number) = @_;
  253. return undef unless ref($self);
  254. $number = 1 unless defined($number);
  255. my $buffer = (" " x $number);
  256. if(_ReadConsole($self->{'handle'}, $buffer, $number) == $number) {
  257. return $buffer;
  258. } else {
  259. return undef;
  260. }
  261. }
  262. #==========
  263. sub Input {
  264. #==========
  265. my($self) = @_;
  266. return undef unless ref($self);
  267. return _ReadConsoleInput($self->{'handle'});
  268. }
  269. #==============
  270. sub PeekInput {
  271. #==============
  272. my($self) = @_;
  273. return undef unless ref($self);
  274. return _PeekConsoleInput($self->{'handle'});
  275. }
  276. #===============
  277. sub WriteInput {
  278. #===============
  279. my($self) = shift;
  280. return undef unless ref($self);
  281. return _WriteConsoleInput($self->{'handle'}, @_);
  282. }
  283. #=========
  284. sub Mode {
  285. #=========
  286. my($self, $mode) = @_;
  287. return undef unless ref($self);
  288. if(defined($mode)) {
  289. return _SetConsoleMode($self->{'handle'}, $mode);
  290. } else {
  291. return _GetConsoleMode($self->{'handle'});
  292. }
  293. }
  294. #========
  295. sub Cls {
  296. #========
  297. my($self, $attr) = @_;
  298. return undef unless ref($self);
  299. $attr = $main::ATTR_NORMAL unless defined($attr);
  300. my ($x, $y) = $self->Size();
  301. my($left, $top, $right ,$bottom) = $self->Window();
  302. my $vx = $right - $left;
  303. my $vy = $bottom - $top;
  304. $self->FillChar(" ", $x*$y, 0, 0);
  305. $self->FillAttr($attr, $x*$y, 0, 0);
  306. $self->Cursor(0, 0);
  307. $self->Window(1, 0, 0, $vx, $vy);
  308. }
  309. #=========
  310. sub Attr {
  311. #=========
  312. my($self, $attr) = @_;
  313. return undef unless ref($self);
  314. if(not defined($attr)) {
  315. return (_GetConsoleScreenBufferInfo($self->{'handle'}))[4];
  316. } else {
  317. return _SetConsoleTextAttribute($self->{'handle'}, $attr);
  318. }
  319. }
  320. #===========
  321. sub Cursor {
  322. #===========
  323. my($self, $col, $row, $size, $visi) = @_;
  324. return undef unless ref($self);
  325. my $curr_row = 0;
  326. my $curr_col = 0;
  327. my $curr_size = 0;
  328. my $curr_visi = 0;
  329. my $return = 0;
  330. my $discard = 0;
  331. if(defined($col)) {
  332. $row = -1 if not defined($row);
  333. if($col == -1 or $row == -1) {
  334. ($discard, $discard, $curr_col, $curr_row) = _GetConsoleScreenBufferInfo($self->{'handle'});
  335. $col=$curr_col if $col==-1;
  336. $row=$curr_row if $row==-1;
  337. }
  338. $return += _SetConsoleCursorPosition($self->{'handle'}, $col, $row);
  339. if(defined($size) and defined($visi)) {
  340. if($size == -1 or $visi == -1) {
  341. ($curr_size, $curr_visi) = _GetConsoleCursorInfo($self->{'handle'});
  342. $size = $curr_size if $size == -1;
  343. $visi = $curr_visi if $visi == -1;
  344. }
  345. $size = 1 if $size < 1;
  346. $size = 99 if $size > 99;
  347. $return += _SetConsoleCursorInfo($self->{'handle'}, $size, $visi);
  348. }
  349. return $return;
  350. } else {
  351. ($discard, $discard, $curr_col, $curr_row) = _GetConsoleScreenBufferInfo($self->{'handle'});
  352. ($curr_size, $curr_visi) = _GetConsoleCursorInfo($self->{'handle'});
  353. return ($curr_col, $curr_row, $curr_size, $curr_visi);
  354. }
  355. }
  356. #=========
  357. sub Size {
  358. #=========
  359. my($self, $col, $row) = @_;
  360. return undef unless ref($self);
  361. if(not defined($col)) {
  362. ($col, $row) = _GetConsoleScreenBufferInfo($self->{'handle'});
  363. return ($col, $row);
  364. } else {
  365. $row = -1 if not defined($row);
  366. if($col == -1 or $row == -1) {
  367. ($curr_col, $curr_row) = _GetConsoleScreenBufferInfo($self->{'handle'});
  368. $col=$curr_col if $col==-1;
  369. $row=$curr_row if $row==-1;
  370. }
  371. return _SetConsoleScreenBufferSize($self->{'handle'}, $col, $row);
  372. }
  373. }
  374. #=============
  375. sub FillAttr {
  376. #=============
  377. my($self, $attr, $number, $col, $row) = @_;
  378. return undef unless ref($self);
  379. $number = 1 unless $number;
  380. if(!defined($col) or !defined($row) or $col == -1 or $row == -1) {
  381. ($discard, $discard,
  382. $curr_col, $curr_row) = _GetConsoleScreenBufferInfo($self->{'handle'});
  383. $col = $curr_col if !defined($col) or $col == -1;
  384. $row = $curr_row if !defined($row) or $row == -1;
  385. }
  386. return _FillConsoleOutputAttribute($self->{'handle'}, $attr, $number, $col, $row);
  387. }
  388. #=============
  389. sub FillChar {
  390. #=============
  391. my($self, $char, $number, $col, $row) = @_;
  392. return undef unless ref($self);
  393. if(!defined($col) or !defined($row) or $col == -1 or $row == -1) {
  394. ($discard, $discard,
  395. $curr_col, $curr_row) = _GetConsoleScreenBufferInfo($self->{'handle'});
  396. $col = $curr_col if !defined($col) or $col == -1;
  397. $row = $curr_row if !defined($row) or $row == -1;
  398. }
  399. return _FillConsoleOutputCharacter($self->{'handle'}, $char, $number, $col, $row);
  400. }
  401. #============
  402. sub InputCP {
  403. #============
  404. my($self, $codepage) = @_;
  405. $codepage = $self if (defined($self) and ref($self) ne "Win32::Console");
  406. if(defined($codepage)) {
  407. return _SetConsoleCP($codepage);
  408. } else {
  409. return _GetConsoleCP();
  410. }
  411. }
  412. #=============
  413. sub OutputCP {
  414. #=============
  415. my($self, $codepage) = @_;
  416. $codepage = $self if (defined($self) and ref($self) ne "Win32::Console");
  417. if(defined($codepage)) {
  418. return _SetConsoleOutputCP($codepage);
  419. } else {
  420. return _GetConsoleOutputCP();
  421. }
  422. }
  423. #======================
  424. sub GenerateCtrlEvent {
  425. #======================
  426. my($self, $type, $pid) = @_;
  427. $type = constant("CTRL_C_EVENT", 0) unless defined($type);
  428. $pid = 0 unless defined($pid);
  429. return _GenerateCtrlEvent($type, $pid);
  430. }
  431. #===================
  432. #sub SetCtrlHandler {
  433. #===================
  434. # my($name, $add) = @_;
  435. # $add = 1 unless defined($add);
  436. # my @nor = keys(%HandlerRoutineStack);
  437. # if($add == 0) {
  438. # foreach $key (@nor) {
  439. # delete $HandlerRoutineStack{$key}, last if $HandlerRoutineStack{$key}==$name;
  440. # }
  441. # $HandlerRoutineRegistered--;
  442. # } else {
  443. # if($#nor == -1) {
  444. # my $r = _SetConsoleCtrlHandler();
  445. # if(!$r) {
  446. # print "WARNING: SetConsoleCtrlHandler failed...\n";
  447. # }
  448. # }
  449. # $HandlerRoutineRegistered++;
  450. # $HandlerRoutineStack{$HandlerRoutineRegistered} = $name;
  451. # }
  452. #}
  453. ########################################################################
  454. # PRIVATE METHODS
  455. #
  456. #================
  457. #sub CtrlHandler {
  458. #================
  459. # my($ctrltype) = @_;
  460. # my $routine;
  461. # my $result = 0;
  462. # CALLEM: foreach $routine (sort { $b <=> $a } keys %HandlerRoutineStack) {
  463. # #print "CtrlHandler: calling $HandlerRoutineStack{$routine}($ctrltype)\n";
  464. # $result = &{"main::".$HandlerRoutineStack{$routine}}($ctrltype);
  465. # last CALLEM if $result;
  466. # }
  467. # return $result;
  468. #}
  469. #============ (MAIN DESTRUCTOR)
  470. sub DESTROY {
  471. #============
  472. my($self) = @_;
  473. _CloseHandle($self->{'handle'});
  474. }
  475. #######################################################################
  476. # dynamically load in the Console.pll module.
  477. #
  478. bootstrap Win32::Console;
  479. #######################################################################
  480. # ADDITIONAL CONSTANTS EXPORTED IN THE MAIN NAMESPACE
  481. #
  482. $main::FG_BLACK = 0;
  483. $main::FG_BLUE = constant("FOREGROUND_BLUE",0);
  484. $main::FG_LIGHTBLUE = constant("FOREGROUND_BLUE",0)|
  485. constant("FOREGROUND_INTENSITY",0);
  486. $main::FG_RED = constant("FOREGROUND_RED",0);
  487. $main::FG_LIGHTRED = constant("FOREGROUND_RED",0)|
  488. constant("FOREGROUND_INTENSITY",0);
  489. $main::FG_GREEN = constant("FOREGROUND_GREEN",0);
  490. $main::FG_LIGHTGREEN = constant("FOREGROUND_GREEN",0)|
  491. constant("FOREGROUND_INTENSITY",0);
  492. $main::FG_MAGENTA = constant("FOREGROUND_RED",0)|
  493. constant("FOREGROUND_BLUE",0);
  494. $main::FG_LIGHTMAGENTA = constant("FOREGROUND_RED",0)|
  495. constant("FOREGROUND_BLUE",0)|
  496. constant("FOREGROUND_INTENSITY",0);
  497. $main::FG_CYAN = constant("FOREGROUND_GREEN",0)|
  498. constant("FOREGROUND_BLUE",0);
  499. $main::FG_LIGHTCYAN = constant("FOREGROUND_GREEN",0)|
  500. constant("FOREGROUND_BLUE",0)|
  501. constant("FOREGROUND_INTENSITY",0);
  502. $main::FG_BROWN = constant("FOREGROUND_RED",0)|
  503. constant("FOREGROUND_GREEN",0);
  504. $main::FG_YELLOW = constant("FOREGROUND_RED",0)|
  505. constant("FOREGROUND_GREEN",0)|
  506. constant("FOREGROUND_INTENSITY",0);
  507. $main::FG_GRAY = constant("FOREGROUND_RED",0)|
  508. constant("FOREGROUND_GREEN",0)|
  509. constant("FOREGROUND_BLUE",0);
  510. $main::FG_WHITE = constant("FOREGROUND_RED",0)|
  511. constant("FOREGROUND_GREEN",0)|
  512. constant("FOREGROUND_BLUE",0)|
  513. constant("FOREGROUND_INTENSITY",0);
  514. $main::BG_BLACK = 0;
  515. $main::BG_BLUE = constant("BACKGROUND_BLUE",0);
  516. $main::BG_LIGHTBLUE = constant("BACKGROUND_BLUE",0)|
  517. constant("BACKGROUND_INTENSITY",0);
  518. $main::BG_RED = constant("BACKGROUND_RED",0);
  519. $main::BG_LIGHTRED = constant("BACKGROUND_RED",0)|
  520. constant("BACKGROUND_INTENSITY",0);
  521. $main::BG_GREEN = constant("BACKGROUND_GREEN",0);
  522. $main::BG_LIGHTGREEN = constant("BACKGROUND_GREEN",0)|
  523. constant("BACKGROUND_INTENSITY",0);
  524. $main::BG_MAGENTA = constant("BACKGROUND_RED",0)|
  525. constant("BACKGROUND_BLUE",0);
  526. $main::BG_LIGHTMAGENTA = constant("BACKGROUND_RED",0)|
  527. constant("BACKGROUND_BLUE",0)|
  528. constant("BACKGROUND_INTENSITY",0);
  529. $main::BG_CYAN = constant("BACKGROUND_GREEN",0)|
  530. constant("BACKGROUND_BLUE",0);
  531. $main::BG_LIGHTCYAN = constant("BACKGROUND_GREEN",0)|
  532. constant("BACKGROUND_BLUE",0)|
  533. constant("BACKGROUND_INTENSITY",0);
  534. $main::BG_BROWN = constant("BACKGROUND_RED",0)|
  535. constant("BACKGROUND_GREEN",0);
  536. $main::BG_YELLOW = constant("BACKGROUND_RED",0)|
  537. constant("BACKGROUND_GREEN",0)|
  538. constant("BACKGROUND_INTENSITY",0);
  539. $main::BG_GRAY = constant("BACKGROUND_RED",0)|
  540. constant("BACKGROUND_GREEN",0)|
  541. constant("BACKGROUND_BLUE",0);
  542. $main::BG_WHITE = constant("BACKGROUND_RED",0)|
  543. constant("BACKGROUND_GREEN",0)|
  544. constant("BACKGROUND_BLUE",0)|
  545. constant("BACKGROUND_INTENSITY",0);
  546. $main::ATTR_NORMAL = $main::FG_GRAY|$main::BG_BLACK;
  547. $main::ATTR_INVERSE = $main::FG_BLACK|$main::BG_GRAY;
  548. undef unless $main::ATTR_NORMAL;
  549. undef unless $main::ATTR_INVERSE;
  550. undef unless $VERSION;
  551. @main::CONSOLE_COLORS = ();
  552. foreach $fg ($main::FG_BLACK, $main::FG_BLUE, $main::FG_GREEN, $main::FG_CYAN,
  553. $main::FG_RED, $main::FG_MAGENTA, $main::FG_BROWN, $main::FG_GRAY,
  554. $main::FG_LIGHTBLUE, $main::FG_LIGHTGREEN, $main::FG_LIGHTCYAN,
  555. $main::FG_LIGHTRED, $main::FG_LIGHTMAGENTA, $main::FG_YELLOW,
  556. $main::FG_WHITE) {
  557. foreach $bg ($main::BG_BLACK, $main::BG_BLUE, $main::BG_GREEN, $main::BG_CYAN,
  558. $main::BG_RED, $main::BG_MAGENTA, $main::BG_BROWN, $main::BG_GRAY,
  559. $main::BG_LIGHTBLUE, $main::BG_LIGHTGREEN, $main::BG_LIGHTCYAN,
  560. $main::BG_LIGHTRED, $main::BG_LIGHTMAGENTA, $main::BG_YELLOW,
  561. $main::BG_WHITE) {
  562. push(@main::CONSOLE_COLORS, $fg|$bg);
  563. }
  564. }
  565. undef $fg;
  566. undef $bg;
  567. # Preloaded methods go here.
  568. #Currently Autoloading is not implemented in Perl for win32
  569. # Autoload methods go after __END__, and are processed by the autosplit program.
  570. 1;
  571. __END__
  572. =head1 NAME
  573. Win32::Console - Win32 Console and Character Mode Functions
  574. =head1 DESCRIPTION
  575. This module implements the Win32 console and character mode
  576. functions. They give you full control on the console input and output,
  577. including: support of off-screen console buffers (eg. multiple screen
  578. pages)
  579. =over
  580. =item *
  581. reading and writing of characters, attributes and whole portions of
  582. the screen
  583. =item *
  584. complete processing of keyboard and mouse events
  585. =item *
  586. some very funny additional features :)
  587. =back
  588. Those functions should also make possible a port of the Unix's curses
  589. library; if there is anyone interested (and/or willing to contribute)
  590. to this project, e-mail me. Thank you.
  591. =head1 REFERENCE
  592. =head2 Methods
  593. =over
  594. =item Alloc
  595. Allocates a new console for the process. Returns C<undef> on errors, a
  596. nonzero value on success. A process cannot be associated with more
  597. than one console, so this method will fail if there is already an
  598. allocated console. Use Free to detach the process from the console,
  599. and then call Alloc to create a new console. See also: C<Free>
  600. Example:
  601. $CONSOLE->Alloc();
  602. =item Attr [attr]
  603. Gets or sets the current console attribute. This attribute is used by
  604. the Write method.
  605. Example:
  606. $attr = $CONSOLE->Attr();
  607. $CONSOLE->Attr($FG_YELLOW | $BG_BLUE);
  608. =item Close
  609. Closes a shortcut object. Note that it is not "strictly" required to
  610. close the objects you created, since the Win32::Shortcut objects are
  611. automatically closed when the program ends (or when you elsehow
  612. destroy such an object).
  613. Example:
  614. $LINK->Close();
  615. =item Cls [attr]
  616. Clear the console, with the specified I<attr> if given, or using
  617. ATTR_NORMAL otherwise.
  618. Example:
  619. $CONSOLE->Cls();
  620. $CONSOLE->Cls($FG_WHITE | $BG_GREEN);
  621. =item Cursor [x, y, size, visible]
  622. Gets or sets cursor position and appearance. Returns C<undef> on
  623. errors, or a 4-element list containing: I<x>, I<y>, I<size>,
  624. I<visible>. I<x> and I<y> are the current cursor position; ...
  625. Example:
  626. ($x, $y, $size, $visible) = $CONSOLE->Cursor();
  627. # Get position only
  628. ($x, $y) = $CONSOLE->Cursor();
  629. $CONSOLE->Cursor(40, 13, 50, 1);
  630. # Set position only
  631. $CONSOLE->Cursor(40, 13);
  632. # Set size and visibility without affecting position
  633. $CONSOLE->Cursor(-1, -1, 50, 1);
  634. =item Display
  635. Displays the specified console on the screen. Returns C<undef> on errors,
  636. a nonzero value on success.
  637. Example:
  638. $CONSOLE->Display();
  639. =item FillAttr [attribute, number, col, row]
  640. Fills the specified number of consecutive attributes, beginning at
  641. I<col>, I<row>, with the value specified in I<attribute>. Returns the
  642. number of attributes filled, or C<undef> on errors. See also:
  643. C<FillChar>.
  644. Example:
  645. $CONSOLE->FillAttr($FG_BLACK | $BG_BLACK, 80*25, 0, 0);
  646. =item FillChar char, number, col, row
  647. Fills the specified number of consecutive characters, beginning at
  648. I<col>, I<row>, with the character specified in I<char>. Returns the
  649. number of characters filled, or C<undef> on errors. See also:
  650. C<FillAttr>.
  651. Example:
  652. $CONSOLE->FillChar("X", 80*25, 0, 0);
  653. =item Flush
  654. Flushes the console input buffer. All the events in the buffer are
  655. discarded. Returns C<undef> on errors, a nonzero value on success.
  656. Example:
  657. $CONSOLE->Flush();
  658. =item Free
  659. Detaches the process from the console. Returns C<undef> on errors, a
  660. nonzero value on success. See also: C<Alloc>.
  661. Example:
  662. $CONSOLE->Free();
  663. =item GenerateCtrlEvent [type, processgroup]
  664. Sends a break signal of the specified I<type> to the specified
  665. I<processgroup>. I<type> can be one of the following constants:
  666. CTRL_BREAK_EVENT
  667. CTRL_C_EVENT
  668. they signal, respectively, the pressing of Control + Break and of
  669. Control + C; if not specified, it defaults to CTRL_C_EVENT.
  670. I<processgroup> is the pid of a process sharing the same console. If
  671. omitted, it defaults to 0 (the current process), which is also the
  672. only meaningful value that you can pass to this function. Returns
  673. C<undef> on errors, a nonzero value on success.
  674. Example:
  675. # break this script now
  676. $CONSOLE->GenerateCtrlEvent();
  677. =item GetEvents
  678. Returns the number of unread input events in the console's input
  679. buffer, or C<undef> on errors. See also: C<Input>, C<InputChar>,
  680. C<PeekInput>, C<WriteInput>.
  681. Example:
  682. $events = $CONSOLE->GetEvents();
  683. =item Info
  684. Returns an array of informations about the console (or C<undef> on
  685. errors), which contains:
  686. =over
  687. =item *
  688. columns (X size) of the console buffer.
  689. =item *
  690. rows (Y size) of the console buffer.
  691. =item *
  692. current column (X position) of the cursor.
  693. =item *
  694. current row (Y position) of the cursor.
  695. =item *
  696. current attribute used for C<Write>.
  697. =item *
  698. left column (X of the starting point) of the current console window.
  699. =item *
  700. top row (Y of the starting point) of the current console window.
  701. =item *
  702. right column (X of the final point) of the current console window.
  703. =item *
  704. bottom row (Y of the final point) of the current console window.
  705. =item *
  706. maximum number of columns for the console window, given the current
  707. buffer size, font and the screen size.
  708. =item *
  709. maximum number of rows for the console window, given the current
  710. buffer size, font and the screen size.
  711. =back
  712. See also: C<Attr>, C<Cursor>, C<Size>, C<Window>, C<MaxWindow>.
  713. Example:
  714. @info = $CONSOLE->Info();
  715. print "Cursor at $info[3], $info[4].\n";
  716. =item Input
  717. Reads an event from the input buffer. Returns a list of values, which
  718. depending on the event's nature are:
  719. =over
  720. =item keyboard event
  721. The list will contain:
  722. =over
  723. =item *
  724. event type: 1 for keyboard
  725. =item *
  726. key down: TRUE if the key is being pressed, FALSE if the key is being released
  727. =item *
  728. repeat count: the number of times the key is being held down
  729. =item *
  730. virtual keycode: the virtual key code of the key
  731. =item *
  732. virtual scancode: the virtual scan code of the key
  733. =item *
  734. char: the ASCII code of the character (if the key is a character key, 0 otherwise)
  735. =item *
  736. control key state: the state of the control keys (SHIFTs, CTRLs, ALTs, etc.)
  737. =back
  738. =item mouse event
  739. The list will contain:
  740. =over
  741. =item *
  742. event type: 2 for mouse
  743. =item *
  744. mouse pos. X: X coordinate (column) of the mouse location
  745. =item *
  746. mouse pos. Y: Y coordinate (row) of the mouse location
  747. =item *
  748. button state: the mouse button(s) which are pressed
  749. =item *
  750. control key state: the state of the control keys (SHIFTs, CTRLs, ALTs, etc.)
  751. =item *
  752. event flags: the type of the mouse event
  753. =back
  754. =back
  755. This method will return C<undef> on errors. Note that the events
  756. returned are depending on the input C<Mode> of the console; for example,
  757. mouse events are not intercepted unless ENABLE_MOUSE_INPUT is
  758. specified. See also: C<GetEvents>, C<InputChar>, C<Mode>,
  759. C<PeekInput>, C<WriteInput>.
  760. Example:
  761. @event = $CONSOLE->Input();
  762. =item InputChar number
  763. Reads and returns I<number> characters from the console input buffer,
  764. or C<undef> on errors. See also: C<Input>, C<Mode>.
  765. Example:
  766. $key = $CONSOLE->InputChar(1);
  767. =item InputCP [codepage]
  768. Gets or sets the input code page used by the console. Note that this
  769. doesn't apply to a console object, but to the standard input
  770. console. This attribute is used by the Write method. See also:
  771. C<OutputCP>.
  772. Example:
  773. $codepage = $CONSOLE->InputCP();
  774. $CONSOLE->InputCP(437);
  775. # you may want to use the non-instanciated form to avoid confuzion :)
  776. $codepage = Win32::Console::InputCP();
  777. Win32::Console::InputCP(437);
  778. =item MaxWindow
  779. Returns the size of the largest possible console window, based on the
  780. current font and the size of the display. The result is C<undef> on
  781. errors, otherwise a 2-element list containing col, row.
  782. Example:
  783. ($maxCol, $maxRow) = $CONSOLE->MaxWindow();
  784. =item Mode [flags]
  785. Gets or sets the input or output mode of a console. I<flags> can be a
  786. combination of the following constants:
  787. ENABLE_LINE_INPUT
  788. ENABLE_ECHO_INPUT
  789. ENABLE_PROCESSED_INPUT
  790. ENABLE_WINDOW_INPUT
  791. ENABLE_MOUSE_INPUT
  792. ENABLE_PROCESSED_OUTPUT
  793. ENABLE_WRAP_AT_EOL_OUTPUT
  794. For more informations on the meaning of those flags, please refer to
  795. the L<"Microsoft's Documentation">.
  796. Example:
  797. $mode = $CONSOLE->Mode();
  798. $CONSOLE->Mode(ENABLE_MOUSE_INPUT | ENABLE_PROCESSED_INPUT);
  799. =item MouseButtons
  800. Returns the number of the buttons on your mouse, or C<undef> on errors.
  801. Example:
  802. print "Your mouse has ", $CONSOLE->MouseButtons(), " buttons.\n";
  803. =item new Win32::Console standard_handle
  804. =item new Win32::Console [accessmode, sharemode]
  805. Creates a new console object. The first form creates a handle to a
  806. standard channel, I<standard_handle> can be one of the following:
  807. STD_OUTPUT_HANDLE
  808. STD_ERROR_HANDLE
  809. STD_INPUT_HANDLE
  810. The second form, instead, creates a console screen buffer in memory,
  811. which you can access for reading and writing as a normal console, and
  812. then redirect on the standard output (the screen) with C<Display>. In
  813. this case, you can specify one or both of the following values for
  814. I<accessmode>:
  815. GENERIC_READ
  816. GENERIC_WRITE
  817. which are the permissions you will have on the created buffer, and one
  818. or both of the following values for I<sharemode>:
  819. FILE_SHARE_READ
  820. FILE_SHARE_WRITE
  821. which affect the way the console can be shared. If you don't specify
  822. any of those parameters, all 4 flags will be used.
  823. Example:
  824. $STDOUT = new Win32::Console(STD_OUTPUT_HANDLE);
  825. $STDERR = new Win32::Console(STD_ERROR_HANDLE);
  826. $STDIN = new Win32::Console(STD_INPUT_HANDLE);
  827. $BUFFER = new Win32::Console();
  828. $BUFFER = new Win32::Console(GENERIC_READ | GENERIC_WRITE);
  829. =item OutputCP [codepage]
  830. Gets or sets the output code page used by the console. Note that this
  831. doesn't apply to a console object, but to the standard output console.
  832. See also: C<InputCP>.
  833. Example:
  834. $codepage = $CONSOLE->OutputCP();
  835. $CONSOLE->OutputCP(437);
  836. # you may want to use the non-instanciated form to avoid confuzion :)
  837. $codepage = Win32::Console::OutputCP();
  838. Win32::Console::OutputCP(437);
  839. =item PeekInput
  840. Does exactly the same as C<Input>, except that the event read is not
  841. removed from the input buffer. See also: C<GetEvents>, C<Input>,
  842. C<InputChar>, C<Mode>, C<WriteInput>.
  843. Example:
  844. @event = $CONSOLE->PeekInput();
  845. =item ReadAttr [number, col, row]
  846. Reads the specified I<number> of consecutive attributes, beginning at
  847. I<col>, I<row>, from the console. Returns the attributes read (a
  848. variable containing one character for each attribute), or C<undef> on
  849. errors. You can then pass the returned variable to C<WriteAttr> to
  850. restore the saved attributes on screen. See also: C<ReadChar>,
  851. C<ReadRect>.
  852. Example:
  853. $colors = $CONSOLE->ReadAttr(80*25, 0, 0);
  854. =item ReadChar [number, col, row]
  855. Reads the specified I<number> of consecutive characters, beginning at
  856. I<col>, I<row>, from the console. Returns a string containing the
  857. characters read, or C<undef> on errors. You can then pass the
  858. returned variable to C<WriteChar> to restore the saved characters on
  859. screen. See also: C<ReadAttr>, C<ReadRect>.
  860. Example:
  861. $chars = $CONSOLE->ReadChar(80*25, 0, 0);
  862. =item ReadRect left, top, right, bottom
  863. Reads the content (characters and attributes) of the rectangle
  864. specified by I<left>, I<top>, I<right>, I<bottom> from the console.
  865. Returns a string containing the rectangle read, or C<undef> on errors.
  866. You can then pass the returned variable to C<WriteRect> to restore the
  867. saved rectangle on screen (or on another console). See also:
  868. C<ReadAttr>, C<ReadChar>.
  869. Example:
  870. $rect = $CONSOLE->ReadRect(0, 0, 80, 25);
  871. =item Scroll left, top, right, bottom, col, row, char, attr,
  872. [cleft, ctop, cright, cbottom]
  873. Moves a block of data in a console buffer; the block is identified by
  874. I<left>, I<top>, I<right>, I<bottom>, while I<row>, I<col> identify
  875. the new location of the block. The cells left empty as a result of
  876. the move are filled with the character I<char> and attribute I<attr>.
  877. Optionally you can specify a clipping region with I<cleft>, I<ctop>,
  878. I<cright>, I<cbottom>, so that the content of the console outside this
  879. rectangle are unchanged. Returns C<undef> on errors, a nonzero value
  880. on success.
  881. Example:
  882. # scrolls the screen 10 lines down, filling with black spaces
  883. $CONSOLE->Scroll(0, 0, 80, 25, 0, 10, " ", $FG_BLACK | $BG_BLACK);
  884. =item Select standard_handle
  885. Redirects a standard handle to the specified console.
  886. I<standard_handle> can have one of the following values:
  887. STD_INPUT_HANDLE
  888. STD_OUTPUT_HANDLE
  889. STD_ERROR_HANDLE
  890. Returns C<undef> on errors, a nonzero value on success.
  891. Example:
  892. $CONSOLE->Select(STD_OUTPUT_HANDLE);
  893. =item Size [col, row]
  894. Gets or sets the console buffer size.
  895. Example:
  896. ($x, $y) = $CONSOLE->Size();
  897. $CONSOLE->Size(80, 25);
  898. =item Title [title]
  899. Gets or sets the title bar the string of the current console window.
  900. Example:
  901. $title = $CONSOLE->Title();
  902. $CONSOLE->Title("This is a title");
  903. =item Window [flag, left, top, right, bottom]
  904. Gets or sets the current console window size. If called without
  905. arguments, returns a 4-element list containing the current window
  906. coordinates in the form of I<left>, I<top>, I<right>, I<bottom>. To
  907. set the window size, you have to specify an additional I<flag>
  908. parameter: if it is 0 (zero), coordinates are considered relative to
  909. the current coordinates; if it is non-zero, coordinates are absolute.
  910. Example:
  911. ($left, $top, $right, $bottom) = $CONSOLE->Window();
  912. $CONSOLE->Window(1, 0, 0, 80, 50);
  913. =item Write string
  914. Writes I<string> on the console, using the current attribute, that you
  915. can set with C<Attr>, and advancing the cursor as needed. This isn't
  916. so different from Perl's "print" statement. Returns the number of
  917. characters written or C<undef> on errors. See also: C<WriteAttr>,
  918. C<WriteChar>, C<WriteRect>.
  919. Example:
  920. $CONSOLE->Write("Hello, world!");
  921. =item WriteAttr attrs, col, row
  922. Writes the attributes in the string I<attrs>, beginning at I<col>,
  923. I<row>, without affecting the characters that are on screen. The
  924. string attrs can be the result of a C<ReadAttr> function, or you can
  925. build your own attribute string; in this case, keep in mind that every
  926. attribute is treated as a character, not a number (see example).
  927. Returns the number of attributes written or C<undef> on errors. See
  928. also: C<Write>, C<WriteChar>, C<WriteRect>.
  929. Example:
  930. $CONSOLE->WriteAttr($attrs, 0, 0);
  931. # note the use of chr()...
  932. $attrs = chr($FG_BLACK | $BG_WHITE) x 80;
  933. $CONSOLE->WriteAttr($attrs, 0, 0);
  934. =item WriteChar chars, col, row
  935. Writes the characters in the string I<attr>, beginning at I<col>, I<row>,
  936. without affecting the attributes that are on screen. The string I<chars>
  937. can be the result of a C<ReadChar> function, or a normal string. Returns
  938. the number of characters written or C<undef> on errors. See also:
  939. C<Write>, C<WriteAttr>, C<WriteRect>.
  940. Example:
  941. $CONSOLE->WriteChar("Hello, worlds!", 0, 0);
  942. =item WriteInput (event)
  943. Pushes data in the console input buffer. I<(event)> is a list of values,
  944. for more information see C<Input>. The string chars can be the result of
  945. a C<ReadChar> function, or a normal string. Returns the number of
  946. characters written or C<undef> on errors. See also: C<Write>,
  947. C<WriteAttr>, C<WriteRect>.
  948. Example:
  949. $CONSOLE->WriteInput(@event);
  950. =item WriteRect rect, left, top, right, bottom
  951. Writes a rectangle of characters and attributes (contained in I<rect>)
  952. on the console at the coordinates specified by I<left>, I<top>,
  953. I<right>, I<bottom>. I<rect> can be the result of a C<ReadRect>
  954. function. Returns C<undef> on errors, otherwise a 4-element list
  955. containing the coordinates of the affected rectangle, in the format
  956. I<left>, I<top>, I<right>, I<bottom>. See also: C<Write>,
  957. C<WriteAttr>, C<WriteChar>.
  958. Example:
  959. $CONSOLE->WriteRect($rect, 0, 0, 80, 25);
  960. =back
  961. =head2 Constants
  962. The following constants are exported in the main namespace of your
  963. script using Win32::Console:
  964. BACKGROUND_BLUE
  965. BACKGROUND_GREEN
  966. BACKGROUND_INTENSITY
  967. BACKGROUND_RED
  968. CAPSLOCK_ON
  969. CONSOLE_TEXTMODE_BUFFER
  970. ENABLE_ECHO_INPUT
  971. ENABLE_LINE_INPUT
  972. ENABLE_MOUSE_INPUT
  973. ENABLE_PROCESSED_INPUT
  974. ENABLE_PROCESSED_OUTPUT
  975. ENABLE_WINDOW_INPUT
  976. ENABLE_WRAP_AT_EOL_OUTPUT
  977. ENHANCED_KEY
  978. FILE_SHARE_READ
  979. FILE_SHARE_WRITE
  980. FOREGROUND_BLUE
  981. FOREGROUND_GREEN
  982. FOREGROUND_INTENSITY
  983. FOREGROUND_RED
  984. LEFT_ALT_PRESSED
  985. LEFT_CTRL_PRESSED
  986. NUMLOCK_ON
  987. GENERIC_READ
  988. GENERIC_WRITE
  989. RIGHT_ALT_PRESSED
  990. RIGHT_CTRL_PRESSED
  991. SCROLLLOCK_ON
  992. SHIFT_PRESSED
  993. STD_INPUT_HANDLE
  994. STD_OUTPUT_HANDLE
  995. STD_ERROR_HANDLE
  996. Additionally, the following variables can be used:
  997. $FG_BLACK
  998. $FG_BLUE
  999. $FG_LIGHTBLUE
  1000. $FG_RED
  1001. $FG_LIGHTRED
  1002. $FG_GREEN
  1003. $FG_LIGHTGREEN
  1004. $FG_MAGENTA
  1005. $FG_LIGHTMAGENTA
  1006. $FG_CYAN
  1007. $FG_LIGHTCYAN
  1008. $FG_BROWN
  1009. $FG_YELLOW
  1010. $FG_GRAY
  1011. $FG_WHITE
  1012. $BG_BLACK
  1013. $BG_BLUE
  1014. $BG_LIGHTBLUE
  1015. $BG_RED
  1016. $BG_LIGHTRED
  1017. $BG_GREEN
  1018. $BG_LIGHTGREEN
  1019. $BG_MAGENTA
  1020. $BG_LIGHTMAGENTA
  1021. $BG_CYAN
  1022. $BG_LIGHTCYAN
  1023. $BG_BROWN
  1024. $BG_YELLOW
  1025. $BG_GRAY
  1026. $BG_WHITE
  1027. $ATTR_NORMAL
  1028. $ATTR_INVERSE
  1029. ATTR_NORMAL is set to gray foreground on black background (DOS's
  1030. standard colors).
  1031. =head2 Microsoft's Documentation
  1032. Documentation for the Win32 Console and Character mode Functions can
  1033. be found on Microsoft's site at this URL:
  1034. http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/sys/src/conchar.htm
  1035. A reference of the available functions is at:
  1036. http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/sys/src/conchar_34.htm
  1037. =head1 VERSION HISTORY
  1038. =over
  1039. =item * 0.03 (07 Apr 1997)
  1040. =over
  1041. =item *
  1042. Added "GenerateCtrlEvent" method.
  1043. =item *
  1044. The PLL file now comes in 2 versions, one for Perl version 5.001
  1045. (build 110) and one for Perl version 5.003 (build 300 and higher,
  1046. EXCEPT 304).
  1047. =item *
  1048. added an installation program that will automatically copy the right
  1049. version in the right place.
  1050. =back
  1051. =item * 0.01 (09 Feb 1997)
  1052. =over
  1053. =item *
  1054. First public release.
  1055. =back
  1056. =back
  1057. =head1 AUTHOR
  1058. Version 0.03 (07 Apr 1997) by Aldo Calpini <[email protected]>
  1059. =head1 CREDITS
  1060. Thanks to: Jesse Dougherty, Dave Roth, ActiveWare, and the
  1061. Perl-Win32-Users community.
  1062. =head1 DISCLAIMER
  1063. This program is FREE; you can redistribute, modify, disassemble, or
  1064. even reverse engineer this software at your will. Keep in mind,
  1065. however, that NOTHING IS GUARANTEED to work and everything you do is
  1066. AT YOUR OWN RISK - I will not take responsibility for any damage, loss
  1067. of money and/or health that may arise from the use of this program!
  1068. This is distributed under the terms of Larry Wall's Artistic License.