summaryrefslogtreecommitdiff
path: root/packages/amunits/src/utilunits/consoleio.pas
blob: 639ab4998d79fe5cbe962ade9ea8393eff7cac32 (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
{
    This file is part of the Free Pascal run time library.

    A file in Amiga system run time library.
    Copyright (c) 1998-2003 by Nils Sjoholm
    member of the Amiga RTL development team.

    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.

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

unit consoleio;

{
    History:
    First version of ConsoleIO.
    This is an translation of consoleio from PCQ Pascal.
    Just AttachConsole to a window and you have your
    own console.
    12 Sep 2000.

    Added the define use_amiga_smartlink.
    13 Jan 2003.

    Changed integer > smallint.
    10 Feb 2003.

    nils.sjoholm@mailbox.swipnet.se

}

interface

uses exec, intuition, console, amigalib, conunit;

TYPE
    tConsoleSet = record
                     WritePort,
                     ReadPort   : pMsgPort;
                     WriteRequest,
                     ReadRequest : pIOStdReq;
                     Window     : pWindow; { not yet used }
                     Buffer     : Char;
                 end;
    pConsoleSet = ^tConsoleSet;

{
        ConsoleIO.p

        This file implements all the normal console.device stuff for
dealing with windows.  They are pulled from the ROM Kernel Manual.
See ConsoleTest.p for an example of using these routines.
}

Procedure ConPutChar(Request : pIOStdReq; Character : Char);
Procedure ConWrite(Request : pIOStdReq; Str : pchar; length : longint);
Procedure ConPutStr(Request : pIOStdReq; Str : pchar);
Procedure QueueRead(Request : pIOStdReq; Where : pchar);
Function ConGetChar(consolePort : pMsgPort; Request : pIOStdReq;
                        WhereTo : pchar) : Char;
Procedure CleanSet(con : pConsoleSet);
Function AttachConsole(w : pWindow) : pConsoleSet;
Function ReadKey(con : pConsoleSet) : Char;
Function KeyPressed(con : pConsoleSet) : Boolean;
Procedure WriteString(con : pConsoleSet; Str : Pchar);
Procedure WriteString(con : pConsoleSet; Str : string);
Function MaxX(con : pConsoleSet) : smallint;
Function MaxY(con : pConsoleSet) : smallint;
Function WhereX(con : pConsoleSet) : smallint;
Function WhereY(con : pConsoleSet) : smallint;
Procedure TextColor(con : pConsoleSet; pen : Byte);
Procedure TextBackground(con : pConsoleSet; pen : Byte);
Procedure DetachConsole(con : pConsoleSet);
Procedure ClrEOL(con : pConsoleSet);
Procedure ClrScr(con : pConsoleSet);
Procedure CursOff(con : pConsoleSet);
Procedure CursOn(con : pConsoleSet);
Procedure DelLine(con : pConsoleSet);
Function LongToStr (I : smallint) : String;
Procedure GotoXY(con : pConsoleSet; x,y : smallint);
Procedure InsLine(con : pConsoleSet);
Procedure OpenConsoleDevice;
Procedure CloseConsoleDevice;

implementation

Procedure ConPutChar(Request : pIOStdReq; Character : Char);
var
    Error : longint;
begin
    Request^.io_Command := CMD_WRITE;
    Request^.io_Data := Addr(Character);
    Request^.io_Length := 1;
    Error := DoIO(pIORequest(Request));
end;

Procedure ConWrite(Request : pIOStdReq; Str : pchar; length : longint);
var
   Error : longint;
begin
    Request^.io_Command := CMD_WRITE;
    Request^.io_Data := Str;
    Request^.io_Length := Length;
    Error := DoIO(pIORequest(Request));
end;

Procedure ConPutStr(Request : pIOStdReq; Str : pchar);
var
    Error : longint;
begin
    Request^.io_Command := CMD_WRITE;
    Request^.io_Data := Str;
    Request^.io_Length := -1;
    Error := DoIO(pIORequest(Request));
end;

Procedure QueueRead(Request : pIOStdReq; Where : pchar);
begin
    Request^.io_Command := CMD_READ;
    Request^.io_Data := Where;
    Request^.io_Length := 1;
    SendIO(pIORequest(Request));
end;

Function ConGetChar(consolePort : pMsgPort; Request : pIOStdReq;
                        WhereTo : pchar) : Char;
var
    Temp : Char;
    TempMsg : pMessage;
begin
    if GetMsg(consolePort) = Nil then begin
        TempMsg := WaitPort(consolePort);
        TempMsg := GetMsg(consolePort);
    end;
    Temp := WhereTo^;
    QueueRead(Request, WhereTo);
    ConGetChar := Temp;
end;

Procedure CleanSet(con : pConsoleSet);
begin
    with con^ do begin
        if ReadRequest <> Nil then
            DeleteStdIO(ReadRequest);
        if WriteRequest <> Nil then
            DeleteStdIO(WriteRequest);
        if ReadPort <> Nil then
            DeletePort(ReadPort);
        if WritePort <> Nil then
            DeletePort(WritePort);
    end;
end;

Function AttachConsole(w : pWindow) : pConsoleSet;
var
    con : pConsoleSet;
    Error : Boolean;
begin
    New(con);
    if con = Nil then
        AttachConsole := Nil;
    with Con^ do begin
        WritePort := CreatePort(Nil, 0);
        Error := WritePort = Nil;
        ReadPort  := CreatePort(Nil, 0);
        Error := Error or (ReadPort = Nil);
        if not Error then begin
            WriteRequest := CreateStdIO(WritePort);
            Error := Error or (WriteRequest = Nil);
            ReadRequest := CreateStdIO(ReadPort);
            Error := Error or (ReadRequest = Nil);
        end;
        if Error then begin
            CleanSet(con);
            Dispose(con);
            AttachConsole := Nil;
        end;
        Window := w;
    end;
    with con^.WriteRequest^ do begin
        io_Data := pointer(w);
        io_Length := SizeOf(tWindow);
    end;
    Error := OpenDevice('console.device', 0,
                        pIORequest(con^.WriteRequest), 0) <> 0;
    if Error then begin
        CleanSet(con);
        Dispose(con);
        AttachConsole := Nil;
    end;
    with con^ do begin
        ReadRequest^.io_Device := WriteRequest^.io_Device;
        ReadRequest^.io_Unit := WriteRequest^.io_Unit;
    end;
    QueueRead(con^.ReadRequest, Addr(con^.Buffer));
    AttachConsole := Con;
end;

Function ReadKey(con : pConsoleSet) : Char;
begin
    with con^ do
        ReadKey := ConGetChar(ReadPort, ReadRequest, Addr(Buffer));
end;

Function KeyPressed(con : pConsoleSet) : Boolean;
begin
    with con^ do
        KeyPressed := CheckIO(pIORequest(ReadRequest)) <> Nil;
end;

Procedure WriteString(con : pConsoleSet; Str : Pchar);
begin
    ConPutStr(con^.WriteRequest, Str);
end;

Procedure WriteString(con : pConsoleSet; Str : string);
var
    temp : string;
begin
    temp := Str;
    temp := temp + #0;
    ConPutStr(con^.WriteRequest, @temp[1]);
end;

Function MaxX(con : pConsoleSet) : smallint;
var
    CU : pConUnit;
begin
    CU := pConUnit(con^.WriteRequest^.io_Unit);
    MaxX := CU^.cu_XMax;
end;

Function MaxY(con : pConsoleSet) : smallint;
var
    CU : pConUnit;
begin
    CU := pConUnit(con^.WriteRequest^.io_Unit);
    MaxY := CU^.cu_YMax;
end;

Function WhereX(con : pConsoleSet) : smallint;
var
    CU : pConUnit;
begin
    CU := pConUnit(con^.WriteRequest^.io_Unit);
    WhereX := CU^.cu_XCP;
end;

Function WhereY(con : pConsoleSet) : smallint;
var
    CU : pConUnit;
begin
    CU := pConUnit(con^.WriteRequest^.io_Unit);
    WhereY := CU^.cu_YCP;
end;

Procedure TextColor(con : pConsoleSet; pen : Byte);
var
    CU : pConUnit;
begin
    CU := pConUnit(con^.WriteRequest^.io_Unit);
    CU^.cu_FgPen := pen;
end;

Procedure TextBackground(con : pConsoleSet; pen : Byte);
var
    CU : pConUnit;
begin
    CU := pConUnit(con^.WriteRequest^.io_Unit);
    CU^.cu_BgPen := pen;
end;

Procedure DetachConsole(con : pConsoleSet);
var
    TempMsg : pMessage;
begin
    with con^ do begin
        Forbid;
        if CheckIO(pIORequest(ReadRequest)) = Nil then begin
            AbortIO(pIORequest(ReadRequest));
            Permit;
            TempMsg := WaitPort(ReadPort);
            TempMsg := GetMsg(ReadPort);
        end else
            Permit;
        CloseDevice(pIORequest(WriteRequest));
    end;
    CleanSet(con);
    Dispose(con);
end;

const
    CSI = #27 + '[';

Procedure ClrEOL(con : pConsoleSet);
{
    Clear to the end of the line
}
begin
    WriteString(con, CSI + 'K');
end;

Procedure ClrScr(con : pConsoleSet);
{
    Clear the text area of the window
}
begin
    WriteString(con, CSI + '1;1H\cJ');
end;

Procedure CursOff(con : pConsoleSet);
{
    Turn the console device's text cursor off
}
begin
    WriteString(con, CSI + '0 p');
end;

Procedure CursOn(con : pConsoleSet);
{
    Turn the text cursor on
}
begin
    WriteString(con, CSI + ' p');
end;


{ Delete the current line, moving all the lines below it  }
{ up one.  The bottom line is cleared.                    }

Procedure DelLine(con : pConsoleSet);
begin
    WriteString(con, CSI + 'M');
end;

Function LongToStr (I : smallint) : String;
Var
    S : String;
begin
    Str (I,S);
    LongToStr:=S;
end;

Procedure GotoXY(con : pConsoleSet; x,y : smallint);
{
    Move the text cursor to the x,y position.  This routine uses
    the ANSI CUP command.
}
var
    XRep : string[7];
    YRep : string[7];
begin
    XRep := LongToStr(x);
    YRep := LongToStr(y);
    WriteString(con,CSI);
    WriteString(con,(YRep));
    WriteString(con,string(';'));
    WriteString(con,(XRep));
    WriteString(con,string('H'));
end;


{  Insert a line at the current text position.  The current line and  }
{  all those below it are moved down one.                             }

Procedure InsLine(con : pConsoleSet);
begin
    WriteString(con, CSI + 'L');
end;



{
        These routines just open and close the Console device without
attaching it to any window.  They update ConsoleBase, and are thus required
for RawKeyConvert and DeadKeyConvert.
}



var

    ConsoleRequest : tIOStdReq;

Procedure OpenConsoleDevice;
{
        This procedure initializes ConsoleDevice, which is required for
    CDInputHandler and RawKeyConvert.
}
var
    Error : longint;
begin
    Error := OpenDevice('console.device', -1, Addr(ConsoleRequest), 0);
    ConsoleDevice := ConsoleRequest.io_Device;
end;

Procedure CloseConsoleDevice;
begin
    CloseDevice(Addr(ConsoleRequest));
end;

end.