summaryrefslogtreecommitdiff
path: root/fpcsrc/rtl/inc/crt.inc
blob: 967452a466b7ca928f27940edddf8d0b46822c04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 1998 - 2005 by the Free Pascal development team.

    This file implements platform independent routines for Crt.
    It should be modified later to use routines from Keyboard and
    Video instead of code in platform-specific crt.pas.

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program 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.

 **********************************************************************}

var
  ScanCode: byte;
  SpecialKey: boolean;

procedure GotoXY (X: tcrtcoord; Y: tcrtcoord);
begin
 GotoXY32 (X, Y);
end;

procedure Window (X1, Y1, X2, Y2: byte);
begin
 Window32 (X1, Y1, X2, Y2);
end;

function WhereX: tcrtcoord;
var
 X1: dword;
begin
 X1 := WhereX32;
 if X1 > 255 then
  WhereX := 255
 else
  WhereX := X1;
end;

function WhereY: tcrtcoord;
var
 Y1: dword;
begin
 Y1 := WhereY32;
 if Y1 > 255 then
  WhereY := 255
 else
  WhereY := Y1;
end;


procedure ClrScr;
{Clears the current window.}
begin
 RemoveLines (0, Succ (WindMaxY - WindMinY));
 GotoXY32 (1, 1);
end;


procedure GotoXY32 (X, Y: dword);
(* Positions cursor on (X, Y) (1-based) relative to window origin; for TP/BP
   compatibility call completely ignored in case of incorrect parameters.    *)
begin
 if (X > 0) and (Y > 0) then
  begin
   Dec (X);
   Dec (Y);
   if (X <= WindMaxX - WindMinX) and (Y <= WindMaxY - WindMinY) then
    SetScreenCursor (X + WindMinX, Y + WindMinY);
  end;
end;


function WhereX32: dword;
(* Returns the X position of the cursor (1-based). *)
var
 X, Y: dword;
begin
 GetScreenCursor (X, Y);
 WhereX32 := Succ (X - WindMinX);
end;


function WhereY32: dword;
(* Returns the Y position of the cursor (1-based). *)
var
 X, Y: dword;
begin
 GetScreenCursor (X, Y);
 WhereY32 := Succ (Y - WindMinY);
end;


procedure ClrEol;
(* Clears the line where cursor is located from current position up to end. *)
var
 X, Y: dword;
begin
 GetScreenCursor (X, Y);
 ClearCells (X, Y, Succ (WindMaxX - X));
end;


procedure DelLine;
(* Deletes the line at cursor. *)
begin
 RemoveLines (Pred (WhereY32), 1);
end;


procedure TextMode (Mode: word);
{ Use this procedure to set-up a specific text-mode.}
begin
 TextAttr := $07;
 LastMode := Mode;
 SetScreenMode (Mode);
 WindMin := 0;
 WindMaxX := Pred (ScreenWidth);
 WindMaxY := Pred (ScreenHeight);
 if WindMaxX >= 255 then
  WindMax := 255
 else
  WindMax := WindMaxX;
 if WindMaxY >= 255 then
  WindMax := WindMax or $FF00
 else
  WindMax := WindMax or (WindMaxY shl 8);
 ClrScr;
end;


procedure TextColor (Color: byte);
{All text written after calling this will have Color as foreground colour.}
begin
 TextAttr := (TextAttr and $70) or (Color and $f);
 if Color > 15 then
  TextAttr := TextAttr or 128;
end;


procedure TextBackground (Color: byte);
{All text written after calling this will have Color as background colour.}
begin
 TextAttr := (TextAttr and $8F) or ((Color and $7) shl 4);
end;


procedure NormVideo;
{Changes the text-background to black and the foreground to white.}
begin
 TextAttr := $7;
end;


procedure LowVideo;
{All text written after this will have low intensity.}
begin
 TextAttr := TextAttr and $F7;
end;


procedure HighVideo;
{All text written after this will have high intensity.}
begin
 TextAttr := TextAttr or $8;
end;


procedure Window32 (X1, Y1, X2, Y2: dword);
{Change the write window to the given coordinates.}
begin
 if (X1 > 0) and (Y1 > 0) and (X2 <= ScreenWidth) and (Y2 <= ScreenHeight)
                                             and (X1 <= X2) and (Y1 <= Y2) then
  begin
   WindMinX := Pred (X1);
   WindMinY := Pred (Y1);
   if WindMinX >= 255 then
    WindMin := 255
   else
    WindMin := WindMinX;
   if WindMinY >= 255 then
    WindMin := WindMin or $FF00
   else
    WindMin := WindMin or (WindMinY shl 8);
   WindMaxX := Pred (X2);
   WindMaxY := Pred (Y2);
   if WindMaxX >= 255 then
    WindMax := 255
   else
    WindMax := WindMaxX;
   if WindMaxY >= 255 then
    WindMax := WindMax or $FF00
   else
    WindMax := WindMaxX or (WindMaxY shl 8);
   GotoXY32 (1, 1);
  end;
end;


threadvar
 CurrX, CurrY: dword;


procedure WriteChar (C: char);
begin
 case C of
  #7: WriteBell;
  #8: if CurrX >= WindMinX then
       Dec (CurrX);
{      #9: x:=(x-lo(windmin)) and $fff8+8+lo(windmin);}
  #10: Inc (CurrY);
  #13: CurrX := WindMinX;
  else
   begin
    WriteNormal (C, CurrX, CurrY);
    Inc (CurrX);
   end;
 end;
 if CurrX > WindMaxX then
  begin
   CurrX := WindMinX;
   Inc (CurrY);
  end;
 if CurrY > WindMaxY then
  begin
   RemoveLines (0, 1);
   CurrY := WindMaxY;
  end;
end;


function CrtWrite (var F: TextRec): integer;
var
 I: dword;
{Write a series of characters to the console.}
begin
 if F.BufPos > 0 then
  begin
   GetScreenCursor (CurrX, CurrY);
   for I := 0 to Pred (F.BufPos) do
    WriteChar ((PChar (F.BufPtr) + I)^);
   SetScreenCursor (CurrX, CurrY);
   F.BufPos := 0;
  end;
 CrtWrite := 0;
end;


function CrtRead (var F: TextRec): integer;
{Read a series of characters from the console.}
var
 C: char;
begin
 GetScreenCursor (CurrX, CurrY);
 F.BufPos := 0;
 F.BufEnd := 0;
 repeat
  if F.BufPos > F.BufEnd then
   F.BufEnd := F.BufPos;
  SetScreenCursor (CurrX, CurrY);
  C := ReadKey;
  case C of
   #0: ReadKey;
(* The following code to support input editing is incomplete anyway
   - no handling of line breaks, no possibility to insert characters
   or delete characters inside the string, etc.

   #0 : case readkey of
          #71 : while f.bufpos>0 do
                 begin
                   dec(f.bufpos);
                   WriteChar(#8);
                 end;
          #75 : if f.bufpos>0 then
                 begin
                   dec(f.bufpos);
                   WriteChar(#8);
                 end;
          #77 : if f.bufpos<f.bufend then
                 begin
                   WriteChar(f.bufptr^[f.bufpos]);
                   inc(f.bufpos);
                 end;
          #79 : while f.bufpos<f.bufend do
                 begin
                   WriteChar(f.bufptr^[f.bufpos]);
                   inc(f.bufpos);
                 end;
         end;
*)

   #8: if (F.BufPos > 0) and (F.BufPos = F.BufEnd) then
        begin
{$WARNING CrtRead doesn't handle line breaks correctly (same bug as TP/BP)!}
         WriteChar (#8);
         WriteChar (' ');
         WriteChar (#8);
         Dec (F.BufPos);
         Dec (F.BufEnd);
        end;
   #13: begin
         WriteChar(#13);
         WriteChar(#10);
         F.BufPtr^ [F.BufEnd] := #13;
         Inc (F.BufEnd);
         F.BufPtr^ [F.BufEnd] := #10;
         Inc (F.BufEnd);
         break;
        end;
   #26: if CheckEOF then
         begin
          F.BufPtr^ [F.BufEnd] := #26;
          Inc (F.BufEnd);
          break;
         end;
   #32..#255: if F.BufPos < F.BufSize - 2 then
               begin
                F.BufPtr^ [F.BufPos] := C;
                Inc (F.BufPos);
                WriteChar (C);
               end;
  end
 until false;
 CrtRead := 0;
end;


function CrtReturn (var F: TextRec): integer;
begin
 CrtReturn:=0;
end;


function CrtClose (var F: TextRec): integer;
begin
 F.Mode := fmClosed;
 CrtClose := 0;
end;


function CrtOpen (var F: TextRec): integer;
begin
 if F.Mode = fmOutput then
  begin
   TextRec(F).InOutFunc := @CrtWrite;
   TextRec(F).FlushFunc := @CrtWrite;
  end
 else
  begin
   F.Mode := fmInput;
   TextRec(F).InOutFunc := @CrtRead;
   TextRec(F).FlushFunc := @CrtReturn;
  end;
 TextRec(F).CloseFunc := @CrtClose;
 CrtOpen := 0;
end;


procedure AssignCrt (var F: text);
{Assigns a file to the crt console.}
begin
 Assign (F, '');
 TextRec (F).OpenFunc := @CrtOpen;
end;


{$IFNDEF HAS_SOUND}
procedure Sound (Hz: word);
(* Dummy Sound implementation - for platforms requiring both frequence
   and duration at the beginning instead of start and stop procedures. *)
begin
end;
{$ENDIF HAS_SOUND}


{$IFNDEF HAS_NOSOUND}
procedure NoSound;
(* Dummy NoSound implementation - for platforms requiring both frequence
   and duration at the beginning instead of start and stop procedures.   *)
begin
end;
{$ENDIF HAS_NOSOUND}


var
  PrevCtrlBreakHandler: TCtrlBreakHandler;


function CrtCtrlBreakHandler (CtrlBreak: boolean): boolean;
begin
(* Earlier registered handlers (e.g. FreeVision) have priority. *)
  if Assigned (PrevCtrlBreakHandler) then
    if PrevCtrlBreakHandler (CtrlBreak) then
      begin
        CrtCtrlBreakHandler := true;
        Exit;
      end;
(* If Ctrl-Break was pressed, either ignore it or allow default processing. *)
  if CtrlBreak then
    CrtCtrlBreakHandler := not (CheckBreak)
  else (* Ctrl-C pressed *)
    begin
      if not (SpecialKey) and (ScanCode = 0) then
        ScanCode := 3;
      CrtCtrlBreakHandler := true;
    end;
end;


procedure CrtInit;
(* Common part of unit initialization. *)
begin
 TextAttr := LightGray;
 WindMin := 0;
 WindMaxX := Pred (ScreenWidth);
 WindMaxY := Pred (ScreenHeight);
 if WindMaxX >= 255 then
  WindMax := 255
 else
  WindMax := WindMaxX;
 if WindMaxY >= 255 then
  WindMax := WindMax or $FF00
 else
  WindMax := WindMax or (WindMaxY shl 8);
 ScanCode := 0;
 SpecialKey := false;
 AssignCrt (Input);
 Reset (Input);
 AssignCrt (Output);
 Rewrite (Output);
 PrevCtrlBreakHandler := SysSetCtrlBreakHandler (@CrtCtrlBreakHandler);
 if PrevCtrlBreakHandler = TCtrlBreakHandler (pointer (-1)) then
   PrevCtrlBreakHandler := nil;
 CheckBreak := true;
end;