Leaked source code of windows server 2003
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.

1483 lines
39 KiB

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