(*
 * Svgalib-Gpc - Pascal porting of Svgalib header files.
 * Copyright 1999, 2000 (C) Nicola Girardi <girardi@vicenza.linux.it>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *)

unit VgaNormalKeyboard;
interface

  uses GPC; (* Gnu Pascal unit, for AssignTFDD and such stuff *)

  var KeyboardFile: Text; (* this can be accessed by user programs
                           * to wait for keyboard events
                           *)

  procedure KeyboardInit;  (* initializing procedure *)
  procedure KeyboardFlush;
  procedure KeyboardClose; (* close keyboard files *)

  function AnyKeyPressed : Boolean; (* useful function like KeyPressed (CRT) *)
  function KeyPressed (Key: Integer) : Boolean; (* tells if Key is pressed *)

  function WaitKey : Integer; (*waits for a key to be pressed and return its ASCII value *)
  function GetKey : Integer; (*asmname 'vga_getkey';*)

  (*
   * Read a character from the keyboard without waiting;
   * returns zero if no key pressed and the ASCII value other
   * wise. It uses AnyKeyPressed (see below)
   *)

implementation

var
  KeyboardFD : asmname '__svgalib_tty_fd' Integer;
  buffered   : Integer;

function GetCh : Integer; asmname 'vga_getch'; (* only internally used *)

function KeyboardSelectFunc (var PrivateData; Writing: Boolean) : Integer;
begin
  if Writing then KeyboardSelectFunc:= -1
     else KeyboardSelectFunc:= KeyboardFD;
end;

(* actually does nothing, because the keyboard in normal mode is
 * already set up so this just clears the buffered
 *)
procedure KeyboardOpenProc (var PrivateData; Mode: TOpenMode);
begin buffered:= 0 end;

procedure KeyboardInit;
begin
  AssignTFDD(KeyboardFile, KeyboardOpenProc, KeyboardSelectFunc,
             TSelectProc(nil), TReadFunc(nil), TWriteFunc(nil),
             TFlushProc(nil), TCloseProc(nil), TDoneProc(nil),
             Pointer(nil));

  Reset(KeyboardFile);
end;

procedure KeyboardClose;
begin Close(KeyboardFile) end;

function AnyKeyPressed : Boolean; (* tells if any key is pressed *)
var
  SelectInput : array [ 1 .. 1 ] of IOSelectType;
  SelectRv    : Integer;
begin
  if (KeyboardFD = -1) (* keyboard uninitialized *)
     then AnyKeyPressed:= false;

  SelectInput[1].f := @KeyboardFile;
  SelectInput[1].Wanted:= [ SelectReadOrEOF ];

  SelectRv:= IOSelect(SelectInput, 0);

  if SelectRv = 1 then
     begin
       buffered:= GetCh;
       AnyKeyPressed:= true;
     end
  else AnyKeyPressed:= ( buffered <> 0 );
end;
   

function KeyPressed (Key: Integer) : Boolean; (* tells if Key is pressed *)
var
  Dummy: Boolean;
begin
  if (KeyboardFD = -1) (* keyboard uninitialized *)
     then KeyPressed:= false;

  Dummy:= AnyKeyPressed; (* treated as a procedure to manage a key in the buffer *)
  
  KeyPressed:= ( buffered = Key );
end;

function GetKey : Integer; (*asmname 'vga_getkey';*)
begin
   if not AnyKeyPressed
      then GetKey:= 0
   else
     begin
       GetKey:= buffered;
       buffered:= 0;
     end;
end;

function WaitKey : Integer;
begin
  WaitKey:= 0; (* initial value *)
  
  repeat until AnyKeyPressed;
  WaitKey:= buffered;
  buffered:= 0;
end;

end.


syntax highlighted by Code2HTML, v. 0.8.8b