Console interface made close to original one

This commit is contained in:
2020-06-24 16:38:03 +03:00
parent a6dcaf154c
commit 700e45b364
2 changed files with 57 additions and 58 deletions

View File

@@ -15,7 +15,7 @@ type
end; end;
TKey = record TKey = record
CharCode, ScanCode: Byte; CharCode, ScanCode: KolibriChar;
end; end;
const const
@@ -91,46 +91,39 @@ uses
SysUtils; SysUtils;
var var
hConsole: Pointer;
ConsoleInterface: TConsoleInterface; ConsoleInterface: TConsoleInterface;
ConsoleExit: procedure(CloseWindow: Boolean); stdcall;
CloseWindow: Boolean; CloseWindow: Boolean;
procedure InitConsole(Title: PKolibriChar; CloseWindowOnExit: Boolean; procedure InitConsole(Title: PKolibriChar; CloseWindowOnExit: Boolean;
WndWidth, WndHeight, ScrWidth, ScrHeight: LongWord); WndWidth, WndHeight, ScrWidth, ScrHeight: LongWord);
var var
ConsoleInit: procedure(WndWidth, WndHeight, ScrWidth, ScrHeight: LongWord; Title: PKolibriChar); stdcall; hConsole: Pointer;
begin begin
if hConsole = nil then hConsole := LoadLibrary('/sys/lib/console.obj');
with ConsoleInterface do
begin begin
hConsole := LoadLibrary('/sys/lib/console.obj'); Cls := GetProcAddress(hConsole, 'con_cls');
with ConsoleInterface do
begin
ClrScr := GetProcAddress(hConsole, 'con_cls');
GetCh := GetProcAddress(hConsole, 'con_getch');
GetCh2 := GetProcAddress(hConsole, 'con_getch2');
GetS := GetProcAddress(hConsole, 'con_gets');
GetCursorHeight := GetProcAddress(hConsole, 'con_get_cursor_height');
GetFlags := GetProcAddress(hConsole, 'con_get_flags');
GetFontHeight := GetProcAddress(hConsole, 'con_get_font_height');
GotoXY := GetProcAddress(hConsole, 'con_set_cursor_pos');
KeyPressed := GetProcAddress(hConsole, 'con_kbhit');
PrintF := GetProcAddress(hConsole, 'con_printf');
ReadKey := GetProcAddress(hConsole, 'con_getch');
SetCursorHeight := GetProcAddress(hConsole, 'con_set_cursor_height');
SetFlags := GetProcAddress(hConsole, 'con_set_flags');
SetTitle := GetProcAddress(hConsole, 'con_set_title');
WhereXY := GetProcAddress(hConsole, 'con_get_cursor_pos');
WritePChar := GetProcAddress(hConsole, 'con_write_asciiz');
WritePCharLen := GetProcAddress(hConsole, 'con_write_string');
end;
ConsoleInit := GetProcAddress(hConsole, 'con_init');
ConsoleInit(WndWidth, WndHeight, ScrWidth, ScrHeight, Title);
ConsoleExit := GetProcAddress(hConsole, 'con_exit'); ConsoleExit := GetProcAddress(hConsole, 'con_exit');
CloseWindow := CloseWindowOnExit; ConsoleInit := GetProcAddress(hConsole, 'con_init');
GetCh := GetProcAddress(hConsole, 'con_getch');
GetCh2 := GetProcAddress(hConsole, 'con_getch2');
GetCursorPos := GetProcAddress(hConsole, 'con_get_cursor_pos');
GetCursorHeight := GetProcAddress(hConsole, 'con_get_cursor_height');
GetFlags := GetProcAddress(hConsole, 'con_get_flags');
GetFontHeight := GetProcAddress(hConsole, 'con_get_font_height');
GetS := GetProcAddress(hConsole, 'con_gets');
KbdHit := GetProcAddress(hConsole, 'con_kbhit');
PrintF := GetProcAddress(hConsole, 'con_printf');
SetCursorHeight := GetProcAddress(hConsole, 'con_set_cursor_height');
SetCursorPos := GetProcAddress(hConsole, 'con_set_cursor_pos');
SetFlags := GetProcAddress(hConsole, 'con_set_flags');
SetTitle := GetProcAddress(hConsole, 'con_set_title');
WriteASCIIZ := GetProcAddress(hConsole, 'con_write_asciiz');
WriteString := GetProcAddress(hConsole, 'con_write_string');
ConsoleInit(WndWidth, WndHeight, ScrWidth, ScrHeight, Title);
end; end;
CloseWindow := CloseWindowOnExit;
end; end;
procedure SetTitle(Title: PKolibriChar); procedure SetTitle(Title: PKolibriChar);
@@ -185,28 +178,32 @@ end;
procedure GotoXY(X, Y: Integer); procedure GotoXY(X, Y: Integer);
begin begin
ConsoleInterface.GotoXY(X, Y); ConsoleInterface.SetCursorPos(X, Y);
end; end;
procedure GotoXY(const Point: TCursorXY); procedure GotoXY(const Point: TCursorXY);
begin begin
with Point do with Point do
ConsoleInterface.GotoXY(X, Y); ConsoleInterface.SetCursorPos(X, Y);
end; end;
function WhereX: Integer; function WhereX: Integer;
var
Y: Integer;
begin begin
Result := WhereXY.X; ConsoleInterface.GetCursorPos(Result, Y);
end; end;
function WhereY: Integer; function WhereY: Integer;
var
X: Integer;
begin begin
Result := WhereXY.Y; ConsoleInterface.GetCursorPos(X, Result);
end; end;
function WhereXY: TCursorXY; function WhereXY: TCursorXY;
begin begin
ConsoleInterface.WhereXY(Result.X, Result.Y); ConsoleInterface.GetCursorPos(Result.X, Result.Y);
end; end;
function CursorBig: Integer; function CursorBig: Integer;
@@ -236,22 +233,22 @@ end;
procedure ClrScr; procedure ClrScr;
begin begin
ConsoleInterface.ClrScr; ConsoleInterface.Cls;
end; end;
procedure Write(Str: PKolibriChar); procedure Write(Str: PKolibriChar);
begin begin
ConsoleInterface.WritePChar(Str); ConsoleInterface.WriteASCIIZ(Str);
end; end;
procedure Write(Str: PKolibriChar; Length: LongWord); procedure Write(Str: PKolibriChar; Length: LongWord);
begin begin
ConsoleInterface.WritePCharLen(Str, Length); ConsoleInterface.WriteString(Str, Length);
end; end;
procedure Write(const Str: ShortString); procedure Write(const Str: ShortString);
begin begin
ConsoleInterface.WritePCharLen(@Str[1], Length(Str)); ConsoleInterface.WriteString(@Str[1], Length(Str));
end; end;
function Write(Format: PKolibriChar; const Args: array of const): Integer; function Write(Format: PKolibriChar; const Args: array of const): Integer;
@@ -279,24 +276,24 @@ var
I: Integer; I: Integer;
begin begin
for I := 0 to LineBreaks - 1 do for I := 0 to LineBreaks - 1 do
ConsoleInterface.WritePCharLen(#10, 1); ConsoleInterface.WriteString(#10, 1);
end; end;
procedure WriteLn(Str: PKolibriChar; LineBreaks: Integer); procedure WriteLn(Str: PKolibriChar; LineBreaks: Integer);
begin begin
ConsoleInterface.WritePChar(Str); ConsoleInterface.WriteASCIIZ(Str);
WriteLn(LineBreaks); WriteLn(LineBreaks);
end; end;
procedure WriteLn(Str: PKolibriChar; Length: LongWord; LineBreaks: Integer); procedure WriteLn(Str: PKolibriChar; Length: LongWord; LineBreaks: Integer);
begin begin
ConsoleInterface.WritePCharLen(Str, Length); ConsoleInterface.WriteString(Str, Length);
WriteLn(LineBreaks); WriteLn(LineBreaks);
end; end;
procedure WriteLn(const Str: ShortString; LineBreaks: Integer); procedure WriteLn(const Str: ShortString; LineBreaks: Integer);
begin begin
ConsoleInterface.WritePCharLen(@Str[1], Length(Str)); ConsoleInterface.WriteString(@Str[1], Length(Str));
WriteLn(LineBreaks); WriteLn(LineBreaks);
end; end;
@@ -318,8 +315,8 @@ begin
K := ConsoleInterface.GetCh2; K := ConsoleInterface.GetCh2;
with WordRec(K), Result do with WordRec(K), Result do
begin begin
CharCode := Lo; CharCode := Chr(Lo);
ScanCode := Hi; ScanCode := Chr(Hi);
end; end;
end; end;
@@ -337,12 +334,12 @@ end;
function KeyPressed: Boolean; function KeyPressed: Boolean;
begin begin
Result := ConsoleInterface.KeyPressed; Result := ConsoleInterface.KbdHit;
end; end;
function ReadKey: KolibriChar; function ReadKey: KolibriChar;
begin begin
Result := ConsoleInterface.ReadKey; Result := Chr(ConsoleInterface.GetCh);
end; end;
function FontHeight: Integer; function FontHeight: Integer;
@@ -358,7 +355,8 @@ end;
initialization initialization
finalization finalization
if hConsole <> nil then with ConsoleInterface do
ConsoleExit(CloseWindow); if Assigned(ConsoleExit) then
ConsoleExit(CloseWindow);
end. end.

View File

@@ -126,23 +126,24 @@ type
PConsoleInterface = ^TConsoleInterface; PConsoleInterface = ^TConsoleInterface;
TConsoleInterface = record TConsoleInterface = record
ClrScr: procedure; stdcall; Cls: procedure; stdcall;
ConsoleExit: procedure(CloseWindow: Boolean); stdcall;
ConsoleInit: procedure(WndWidth, WndHeight, ScrWidth, ScrHeight: LongWord; Title: PKolibriChar); stdcall;
GetCh: function: Integer; stdcall; GetCh: function: Integer; stdcall;
GetCh2: function: Word; stdcall; GetCh2: function: Word; stdcall;
GetS: function(Str: PKolibriChar; Length: Integer): PKolibriChar; stdcall; GetCursorPos: procedure(var X, Y: Integer); stdcall;
GetCursorHeight: function: Integer; stdcall; GetCursorHeight: function: Integer; stdcall;
GetFlags: function: LongWord; stdcall; GetFlags: function: LongWord; stdcall;
GetFontHeight: function: Integer; stdcall; GetFontHeight: function: Integer; stdcall;
GotoXY: procedure(X, Y: Integer); stdcall; GetS: function(Str: PKolibriChar; Length: Integer): PKolibriChar; stdcall;
KeyPressed: function: Boolean; stdcall; KbdHit: function: Boolean; stdcall;
PrintF: function(Str: PKolibriChar): Integer; cdecl varargs; PrintF: function(Str: PKolibriChar): Integer; cdecl varargs;
ReadKey: function: KolibriChar; stdcall;
SetFlags: function(Flags: LongWord): LongWord; stdcall; SetFlags: function(Flags: LongWord): LongWord; stdcall;
SetCursorHeight: function(Height: Integer): Integer; stdcall; SetCursorHeight: function(Height: Integer): Integer; stdcall;
SetCursorPos: procedure(X, Y: Integer); stdcall;
SetTitle: procedure(Title: PKolibriChar); stdcall; SetTitle: procedure(Title: PKolibriChar); stdcall;
WhereXY: procedure(var X, Y: Integer); stdcall; WriteASCIIZ: procedure(Str: PKolibriChar); stdcall;
WritePChar: procedure(Str: PKolibriChar); stdcall; WriteString: procedure(Str: PKolibriChar; Length: LongWord); stdcall;
WritePCharLen: procedure(Str: PKolibriChar; Length: LongWord); stdcall;
end; end;
procedure _Halt0; procedure _Halt0;