"Fossies" - the Fresh Open Source Software Archive 
Member "seed7/src/con_con.c" (17 Dec 2019, 14099 Bytes) of package /linux/misc/seed7_05_20210223.tgz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "con_con.c" see the
Fossies "Dox" file reference documentation.
1 /********************************************************************/
2 /* */
3 /* con_con.c Driver for conio console access. */
4 /* Copyright (C) 1989 - 2005 Thomas Mertes */
5 /* */
6 /* This file is part of the Seed7 Runtime Library. */
7 /* */
8 /* The Seed7 Runtime Library is free software; you can */
9 /* redistribute it and/or modify it under the terms of the GNU */
10 /* Lesser General Public License as published by the Free Software */
11 /* Foundation; either version 2.1 of the License, or (at your */
12 /* option) any later version. */
13 /* */
14 /* The Seed7 Runtime Library is distributed in the hope that it */
15 /* will be useful, but WITHOUT ANY WARRANTY; without even the */
16 /* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR */
17 /* PURPOSE. See the GNU Lesser General Public License for more */
18 /* details. */
19 /* */
20 /* You should have received a copy of the GNU Lesser General */
21 /* Public License along with this program; if not, write to the */
22 /* Free Software Foundation, Inc., 51 Franklin Street, */
23 /* Fifth Floor, Boston, MA 02110-1301, USA. */
24 /* */
25 /* Module: Seed7 Runtime Library */
26 /* File: seed7/src/con_con.c */
27 /* Changes: 1994 Thomas Mertes */
28 /* Content: Driver for conio console access. */
29 /* */
30 /********************************************************************/
31
32 #define LOG_FUNCTIONS 0
33 #define VERBOSE_EXCEPTIONS 0
34
35 #include "version.h"
36
37 #include "stdlib.h"
38 #include "stdio.h"
39 #include "string.h"
40 #include "conio.h"
41
42 #include "common.h"
43 #include "con_drv.h"
44 #include "kbd_drv.h"
45
46
47 /* #define atexit(x) */
48
49 #define SCRHEIGHT 25
50 #define SCRWIDTH 80
51
52 #define black 0
53 #define green 2
54 #define lightgray 7
55 #define darkgray 8
56 #define yellow 14
57 #define white 15
58
59 static char currentattribute;
60 static char outbuffer[SCRWIDTH];
61 static boolType console_initialized = FALSE;
62 static boolType cursor_on = FALSE;
63
64
65 #ifdef OS_GETCH_READS_BYTES
66 static charType map_char[] = {
67 /* 0 */ 0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007, 0010, 0011,
68 /* 10 */ 0012, 0013, 0014, 0015, 0016, 0017, 0020, 0021, 0022, 0023,
69 /* 20 */ 0024, 0025, 0026, 0027, 0030, 0031, 0032, 0033, 0034, 0035,
70 /* 30 */ 0036, 0037, ' ', '!', '\"', '#', '$', '%', '&', '\'',
71 /* 40 */ '(', ')', '*', '+', ',', '-', '.', '/', '0', '1',
72 /* 50 */ '2', '3', '4', '5', '6', '7', '8', '9', ':', ';',
73 /* 60 */ '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E',
74 /* 70 */ 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
75 /* 80 */ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
76 /* 90 */ 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c',
77 /* 100 */ 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
78 /* 110 */ 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
79 /* 120 */ 'x', 'y', 'z', '{', '|', '}', '~', 0177, 0307, 0374,
80 /* 130 */ 0351, 0342, 0344, 0340, 0345, 0347, 0352, 0353, 0350, 0357,
81 /* 140 */ 0356, 0354, 0304, 0305, 0311, 0346, 0306, 0364, 0366, 0362,
82 /* 150 */ 0373, 0371, 0377, 0326, 0334, 0370, 0243, 0330, 0327, '?',
83 /* 160 */ 0341, 0355, 0363, 0372, 0361, 0321, 0252, 0272, 0277, 0256,
84 /* 170 */ 0254, 0275, 0274, 0241, 0253, 0273, '?', '?', '?', '?',
85 /* 180 */ '?', 0301, 0302, 0300, 0251, '?', '?', '?', '?', 0242,
86 /* 190 */ 0245, '?', '?', '?', '?', '?', '?', '?', 0343, 0303,
87 /* 200 */ '?', '?', '?', '?', '?', '?', '?', 0244, 0360, 0320,
88 /* 210 */ 0312, 0313, 0310, 0271, 0315, 0316, 0317, '?', '?', '?',
89 /* 220 */ '?', 0246, 0314, '?', 0363, 0337, 0324, 0322, 0365, 0325,
90 /* 230 */ 0265, 0376, 0336, 0332, 0333, 0331, 0375, 0335, 0257, 0264,
91 /* 240 */ 0255, 0261, '?', 0276, 0266, 0247, 0367, 0270, 0260, 0250,
92 /* 250 */ 0267, 0271, 0263, 0262, '?', 0240};
93 #endif
94
95
96 static charType map_key[] = {
97 /* 0 */ K_UNDEF, K_UNDEF, K_UNDEF, K_NULCHAR, K_UNDEF,
98 /* 5 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
99 /* 10 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
100 /* 15 */ K_BACKTAB, K_ALT_Q, K_ALT_W, K_ALT_E, K_ALT_R,
101 /* 20 */ K_ALT_T, K_ALT_Y, K_ALT_U, K_ALT_I, K_ALT_O,
102 /* 25 */ K_ALT_P, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
103 /* 30 */ K_ALT_A, K_ALT_S, K_ALT_D, K_ALT_F, K_ALT_G,
104 /* 35 */ K_ALT_H, K_ALT_J, K_ALT_K, K_ALT_L, K_UNDEF,
105 /* 40 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_ALT_Z,
106 /* 45 */ K_ALT_X, K_ALT_C, K_ALT_V, K_ALT_B, K_ALT_N,
107 /* 50 */ K_ALT_M, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
108 /* 55 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_F1,
109 /* 60 */ K_F2, K_F3, K_F4, K_F5, K_F6,
110 /* 65 */ K_F7, K_F8, K_F9, K_F10, K_UNDEF,
111 /* 70 */ K_UNDEF, K_HOME, K_UP, K_PGUP, K_UNDEF,
112 /* 75 */ K_LEFT, K_UNDEF, K_RIGHT, K_UNDEF, K_END,
113 /* 80 */ K_DOWN, K_PGDN, K_INS, K_DEL, K_SFT_F1,
114 /* 85 */ K_SFT_F2, K_SFT_F3, K_SFT_F4, K_SFT_F5, K_SFT_F6,
115 /* 90 */ K_SFT_F7, K_SFT_F8, K_SFT_F9, K_SFT_F10, K_CTL_F1,
116 /* 95 */ K_CTL_F2, K_CTL_F3, K_CTL_F4, K_CTL_F5, K_CTL_F6,
117 /* 100 */ K_CTL_F7, K_CTL_F8, K_CTL_F9, K_CTL_F10, K_ALT_F1,
118 /* 105 */ K_ALT_F2, K_ALT_F3, K_ALT_F4, K_ALT_F5, K_ALT_F6,
119 /* 110 */ K_ALT_F7, K_ALT_F8, K_ALT_F9, K_ALT_F10, K_UNDEF,
120 /* 115 */ K_CTL_LEFT, K_CTL_RIGHT, K_CTL_END, K_CTL_PGDN, K_CTL_HOME,
121 /* 120 */ K_ALT_1, K_ALT_2, K_ALT_3, K_ALT_4, K_ALT_5,
122 /* 125 */ K_ALT_6, K_ALT_7, K_ALT_8, K_ALT_9, K_ALT_0,
123 /* 130 */ K_UNDEF, K_UNDEF, K_CTL_PGUP, K_F11, K_F12,
124 /* 135 */ K_SFT_F11, K_SFT_F12, K_CTL_F11, K_CTL_F12, K_ALT_F11,
125 /* 140 */ K_ALT_F12, K_CTL_UP, K_UNDEF, K_UNDEF, K_UNDEF,
126 /* 145 */ K_CTL_DOWN, K_CTL_INS, K_CTL_DEL, K_UNDEF, K_UNDEF,
127 /* 150 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
128 /* 155 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
129 /* 160 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
130 /* 165 */ K_CTL_INS, K_CTL_DEL, K_UNDEF, K_UNDEF, K_UNDEF,
131 /* 170 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
132 /* 175 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
133 /* 180 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
134 /* 185 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
135 /* 190 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
136 /* 195 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
137 /* 200 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
138 /* 205 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
139 /* 210 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
140 /* 215 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
141 /* 220 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
142 /* 225 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
143 /* 230 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
144 /* 235 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
145 /* 240 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
146 /* 245 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
147 /* 250 */ K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF, K_UNDEF,
148 /* 255 */ K_UNDEF};
149
150
151
152 void kbdShut (void)
153
154 { /* kbdShut */
155 } /* kbdShut */
156
157
158
159 boolType kbdKeyPressed (void)
160
161 { /* kbdKeyPressed */
162 return kbhit();
163 } /* kbdKeyPressed */
164
165
166
167 charType kbdGetc (void)
168
169 {
170 intType key;
171 charType result;
172
173 /* kbdGetc */
174 logFunction(printf("kbdGetc\n"););
175 key = os_getch();
176 if (key == 0 || key == 224) {
177 /* printf("key [%ld, ", key); */
178 key = os_getch();
179 result = map_key[key];
180 /* printf("%ld] -> %lu ", key, result); */
181 } else {
182 #ifdef OS_GETCH_READS_BYTES
183 result = map_char[(uintType) key & 0xFF];
184 #else
185 result = (charType) key;
186 #endif
187 } /* if */
188 if (result == 13) {
189 result = 10;
190 } /* if */
191 logFunction(printf("kbdGetc --> %d\n", result););
192 return result;
193 } /* kbdGetc */
194
195
196
197 charType kbdRawGetc (void)
198
199 { /* kbdRawGetc */
200 return kbdGetc();
201 } /* kbdRawGetc */
202
203
204
205 static void con_setcolor (intType foreground, intType background)
206
207 { /* con_setcolor */
208 currentattribute = (char) (foreground + 16 * (background % 8));
209 } /* con_setcolor */
210
211
212
213 static void con_standardcolour (void)
214
215 { /* con_standardcolour */
216 con_setcolor(lightgray, black);
217 } /* con_standardcolour */
218
219
220
221 static void con_normalcolour (void)
222
223 { /* con_normalcolour */
224 con_setcolor(lightgray, black);
225 } /* con_normalcolour */
226
227
228
229 intType textheight (void)
230
231 { /* textheight */
232 return 1;
233 } /* textheight */
234
235
236
237 intType textwidth (striType stri,
238 intType startcol, intType stopcol)
239
240 { /* textwidth */
241 return stopcol + 1 - startcol;
242 } /* textwidth */
243
244
245
246 void textcolumns (striType stri, intType striwidth,
247 intType * cols, intType *rest)
248
249 { /* textcolumns */
250 *cols = striwidth;
251 *rest = 0;
252 } /* textcolumns */
253
254
255
256 int conHeight (void)
257
258 { /* conHeight */
259 return SCRHEIGHT;
260 } /* conHeight */
261
262
263
264 int conWidth (void)
265
266 { /* conWidth */
267 return SCRWIDTH;
268 } /* conWidth */
269
270
271
272 void conFlush (void)
273
274 { /* conFlush */
275 } /* conFlush */
276
277
278
279 void conCursor (boolType on)
280
281 { /* conCursor */
282 cursor_on = on;
283 if (on) {
284 _setcursortype(_SOLIDCURSOR);
285 } else {
286 _setcursortype(_NOCURSOR);
287 } /* if */
288 } /* conCursor */
289
290
291
292 /**
293 * Moves the system cursor to the given place of the console.
294 * If no system cursor exists this procedure can be replaced by
295 * a dummy procedure.
296 */
297 void conSetCursor (intType line, intType column)
298
299 { /* conSetCursor */
300 gotoxy(column, line);
301 } /* conSetCursor */
302
303
304
305 void conText (intType lin, intType col, ustriType stri,
306 memSizeType length)
307
308 /* This function writes the string stri to the console at the */
309 /* position (lin, col). The position (lin, col) must be a legal */
310 /* position of the console. The string stri is not allowed to go */
311 /* beyond the right border of the console. All console output */
312 /* must be done with this function. */
313
314 { /* conText */
315 memcpy(outbuffer, stri, length);
316 outbuffer[length] = '\0';
317 conSetCursor(lin, col);
318 cputs(outbuffer);
319 } /* conText */
320
321
322
323 /**
324 * Clears the area described by startlin, stoplin, startcol and stopcol.
325 */
326 void conClear (intType startlin, intType startcol,
327 intType stoplin, intType stopcol)
328
329 { /* conClear */
330 clrscr();
331 } /* conClear */
332
333
334
335 /**
336 * Scrolls the area inside startlin, startcol, stoplin and
337 * stopcol upward by count lines. The upper count lines of the
338 * area are overwritten. At the lower end of the area blank lines
339 * are inserted. Nothing is changed outside the area.
340 * The calling function assures that count is greater or equal 1.
341 */
342 void conUpScroll (intType startlin, intType startcol,
343 intType stoplin, intType stopcol, intType count)
344
345 { /* conUpScroll */
346 } /* conUpScroll */
347
348
349
350 /**
351 * Scrolls the area inside startlin, startcol, stoplin and
352 * stopcol downward by count lines. The lower count lines of the
353 * area are overwritten. At the upper end of the area blank lines
354 * are inserted. Nothing is changed outside the area.
355 * The calling function assures that count is greater or equal 1.
356 */
357 void conDownScroll (intType startlin, intType startcol,
358 intType stoplin, intType stopcol, intType count)
359
360 { /* conDownScroll */
361 } /* conDownScroll */
362
363
364
365 /**
366 * Scrolls the area inside startlin, startcol, stoplin and
367 * stopcol leftward by count columns. The left count columns of the
368 * area are overwritten. At the right end of the area blank columns
369 * are inserted. Nothing is changed outside the area.
370 * The calling function assures that count is greater or equal 1.
371 */
372 void conLeftScroll (intType startlin, intType startcol,
373 intType stoplin, intType stopcol, intType count)
374
375 { /* conLeftScroll */
376 } /* conLeftScroll */
377
378
379
380 /**
381 * Scrolls the area inside startlin, startcol, stoplin and
382 * stopcol rightward by count columns. The right count columns of the
383 * area are overwritten. At the left end of the area blank columns
384 * are inserted. Nothing is changed outside the area.
385 * The calling function assures that count is greater or equal 1.
386 */
387 void conRightScroll (intType startlin, intType startcol,
388 intType stoplin, intType stopcol, intType count)
389
390 { /* conRightScroll */
391 } /* conRightScroll */
392
393
394
395 void conShut (void)
396
397 { /* conShut */
398 logFunction(printf("conShut\n"););
399 if (console_initialized) {
400 con_standardcolour();
401 conCursor(TRUE);
402 conClear(1, 1, SCRHEIGHT, SCRWIDTH);
403 conSetCursor(1, 24);
404 console_initialized = FALSE;
405 } /* if */
406 logFunction(printf("conShut -->\n"););
407 } /* conShut */
408
409
410
411 /**
412 * Initializes and clears the console.
413 */
414 int conOpen (void)
415
416 { /* conOpen */
417 logFunction(printf("conOpen\n"););
418 con_normalcolour();
419 conClear(1, 1, SCRHEIGHT, SCRWIDTH);
420 conCursor(FALSE);
421 console_initialized = TRUE;
422 atexit(conShut);
423 logFunction(printf("conOpen -->\n"););
424 return 1;
425 } /* conOpen */