fpPS4/sys/sys_crt.pas

201 lines
3.6 KiB
ObjectPascal
Raw Normal View History

2022-07-13 14:01:22 +00:00
unit sys_crt;
{$mode ObjFPC}{$H+}
interface
uses
windows,
2022-09-05 13:09:58 +00:00
sys_pthread,
2022-07-13 14:01:22 +00:00
spinlock;
Procedure sys_crt_init;
implementation
2022-07-13 20:27:56 +00:00
uses
2022-10-22 21:09:28 +00:00
sys_kernel,
2022-07-13 20:27:56 +00:00
sys_signal;
2022-07-13 14:01:22 +00:00
var
2022-10-24 11:17:28 +00:00
StdOutLock:TRTLCriticalSection;
2022-09-05 13:09:58 +00:00
StdOutColor:Word;
const
StdErrColor=FOREGROUND_RED;
2022-07-13 14:01:22 +00:00
function GetConsoleTextAttribute(hConsoleOutput:HANDLE;var wAttributes:WORD):WINBOOL;
var
info:CONSOLE_SCREEN_BUFFER_INFO;
begin
Result:=GetConsoleScreenBufferInfo(hConsoleOutput,@info);
if Result then
begin
wAttributes:=info.wAttributes
end;
end;
2022-10-22 21:09:28 +00:00
function GetConsoleCursorPosition(hConsoleOutput:HANDLE;var dwCursorPosition:COORD):WINBOOL;
var
info:CONSOLE_SCREEN_BUFFER_INFO;
begin
Result:=GetConsoleScreenBufferInfo(hConsoleOutput,@info);
if Result then
begin
dwCursorPosition:=info.dwCursorPosition;
end;
end;
2022-07-13 14:01:22 +00:00
Procedure CrtOutWrite(var t:TextRec);
var
n:DWORD;
Begin
if (t.BufPos=0) then exit;
n:=0;
2022-07-14 20:16:23 +00:00
_sig_lock(SL_NOINTRRUP);
2022-10-24 11:17:28 +00:00
EnterCriticalSection(StdOutLock);
2022-07-13 14:01:22 +00:00
2022-10-22 21:09:28 +00:00
WriteConsole(t.Handle,
t.Bufptr,
t.BufPos,
@n,
nil);
2022-07-13 14:01:22 +00:00
2022-10-24 11:17:28 +00:00
LeaveCriticalSection(StdOutLock);
2022-07-14 20:16:23 +00:00
_sig_unlock(SL_NOINTRRUP);
2022-07-17 17:53:17 +00:00
if (n<>t.BufPos) then InOutRes:=101;
t.BufPos:=0;
2022-07-13 14:01:22 +00:00
end;
Procedure CrtErrWrite(var t:TextRec);
var
n:DWORD;
2022-10-22 21:09:28 +00:00
dwCursorPosition:COORD;
2022-07-13 14:01:22 +00:00
Begin
if (t.BufPos=0) then exit;
n:=0;
2022-07-14 20:16:23 +00:00
_sig_lock(SL_NOINTRRUP);
2022-10-24 11:17:28 +00:00
EnterCriticalSection(StdOutLock);
2022-07-13 14:01:22 +00:00
2022-10-22 21:09:28 +00:00
dwCursorPosition:=Default(COORD);
GetConsoleCursorPosition(t.Handle,
dwCursorPosition);
WriteConsole(t.Handle,
t.Bufptr,
t.BufPos,
@n,
nil);
FillConsoleOutputAttribute(t.Handle,
StdErrColor,
t.BufPos,
dwCursorPosition,
n);
2022-10-24 11:17:28 +00:00
LeaveCriticalSection(StdOutLock);
2022-10-22 21:09:28 +00:00
_sig_unlock(SL_NOINTRRUP);
if (n<>t.BufPos) then InOutRes:=101;
t.BufPos:=0;
end;
Procedure CrtFileWrite(var t:TextRec);
var
n:DWORD;
Begin
if (t.BufPos=0) then exit;
n:=0;
_sig_lock(SL_NOINTRRUP);
2022-10-24 11:17:28 +00:00
EnterCriticalSection(StdOutLock);
2022-10-22 21:09:28 +00:00
WriteFile(t.Handle,
t.Bufptr^,
t.BufPos,
n,
nil);
2022-07-13 14:01:22 +00:00
2022-10-24 11:17:28 +00:00
LeaveCriticalSection(StdOutLock);
2022-07-14 20:16:23 +00:00
_sig_unlock(SL_NOINTRRUP);
2022-07-17 17:53:17 +00:00
if (n<>t.BufPos) then InOutRes:=101;
t.BufPos:=0;
2022-07-13 14:01:22 +00:00
end;
Procedure CrtClose(Var F:TextRec);
Begin
F.Mode:=fmClosed;
end;
Procedure CrtOpenOut(Var F:TextRec);
Begin
TextRec(F).Handle:=GetStdHandle(STD_OUTPUT_HANDLE);
2022-10-22 21:09:28 +00:00
if (SwGetFileType(TextRec(F).Handle)=FILE_TYPE_CHAR) then
begin
TextRec(F).InOutFunc:=@CrtOutWrite;
TextRec(F).FlushFunc:=@CrtOutWrite;
TextRec(F).CloseFunc:=@CrtClose;
end else
begin
TextRec(F).InOutFunc:=@CrtFileWrite;
TextRec(F).FlushFunc:=@CrtFileWrite;
TextRec(F).CloseFunc:=@CrtClose;
end;
2022-07-13 14:01:22 +00:00
end;
Procedure CrtOpenErr(Var F:TextRec);
Begin
TextRec(F).Handle:=GetStdHandle(STD_ERROR_HANDLE);
2022-10-22 21:09:28 +00:00
if (SwGetFileType(TextRec(F).Handle)=FILE_TYPE_CHAR) then
begin
TextRec(F).InOutFunc:=@CrtErrWrite;
TextRec(F).FlushFunc:=@CrtErrWrite;
TextRec(F).CloseFunc:=@CrtClose;
end else
begin
TextRec(F).InOutFunc:=@CrtFileWrite;
TextRec(F).FlushFunc:=@CrtFileWrite;
TextRec(F).CloseFunc:=@CrtClose;
end;
2022-07-13 14:01:22 +00:00
end;
procedure AssignCrt(var F:Text;cb:codepointer);
begin
Assign(F,'');
TextRec(F).OpenFunc:=cb;
end;
Procedure sys_crt_init;
begin
2022-09-05 13:09:58 +00:00
tcb_thread:=nil; //need zero tcb
2022-07-13 14:01:22 +00:00
AssignCrt(Output,@CrtOpenOut);
Rewrite(Output);
AssignCrt(StdOut,@CrtOpenOut);
Rewrite(StdOut);
AssignCrt(ErrOutput,@CrtOpenErr);
Rewrite(ErrOutput);
AssignCrt(StdErr,@CrtOpenErr);
Rewrite(StdErr);
end;
2022-09-05 13:09:58 +00:00
initialization
2022-10-24 11:17:28 +00:00
InitCriticalSection(StdOutLock);
2022-09-05 13:09:58 +00:00
StdOutColor:=7;
GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),StdOutColor);
2022-07-13 14:01:22 +00:00
end.