File: console.c

package info (click to toggle)
dosemu-freedos 1%3A0.0.b9r5a%2Betch.1-0etch1
  • links: PTS
  • area: contrib
  • in suites: etch
  • size: 19,744 kB
  • ctags: 23,279
  • sloc: ansic: 143,864; asm: 20,397; makefile: 3,868; perl: 1,106; yacc: 690; sh: 553; pascal: 297; xml: 150; cpp: 67
file content (402 lines) | stat: -rw-r--r-- 8,864 bytes parent folder | download | duplicates (2)
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
/* ----------- console.c ---------- */

#include "dflat.h"

/* ----- table of alt keys for finding shortcut keys ----- */
static int altconvert[] = {
    ALT_A,ALT_B,ALT_C,ALT_D,ALT_E,ALT_F,ALT_G,ALT_H,
    ALT_I,ALT_J,ALT_K,ALT_L,ALT_M,ALT_N,ALT_O,ALT_P,
    ALT_Q,ALT_R,ALT_S,ALT_T,ALT_U,ALT_V,ALT_W,ALT_X,
    ALT_Y,ALT_Z,ALT_0,ALT_1,ALT_2,ALT_3,ALT_4,ALT_5,
    ALT_6,ALT_7,ALT_8,ALT_9
};

unsigned video_mode;
unsigned video_page;
static int near cursorpos[MAXSAVES];
static int near cursorshape[MAXSAVES];
static union REGS regs;
static int cs;
static int far *clk = MK_FP(0x40,0x6c);

/* ------- macro to wait one clock tick -------- */
#define wait()           \
{                        \
    int now = *clk;      \
    while (now == *clk); \
}

/* ------------- clear the screen -------------- */
void clearscreen(void)
{
    int ht=SCREENHEIGHT;
    int wd=SCREENWIDTH;

    cursor(0, 0);
    regs.h.al=' ';
    regs.h.ah=9;
    regs.x.bx=7;
    regs.x.cx=ht * wd;
    int86(VIDEO, &regs, &regs);

}

void SwapCursorStack(void)
{
    if (cs > 1)
        {
        swap(cursorpos[cs-2], cursorpos[cs-1]);
        swap(cursorshape[cs-2], cursorshape[cs-1]);
	}

}

/* ---- BIOS keyboard routines with 84 and 102 key keyboard support ---- */
int Xbioskey(int cmd)
{
    static int keybase=-1;
    union REGS kregs;

    if (keybase < 0)
        {
        volatile char far *kbtype = MK_FP(0x40,0x96); /* BIOS data flag */
        keybase=(((*kbtype) & 0x10) != 0) ? 0x10 : 0;
	/* 0 for 84 key XT mode, 0x10 for 102 key AT mode. */
	/* (0x20 for 122 key mode, which is not used here) */
        }

    kregs.h.ah = (char) (keybase + cmd);
    kregs.h.al = 0;
    int86(0x16, &kregs, &kregs);
    if ((cmd == 1) && (kregs.x.flags & 0x40 /* zero flag */))
        return 0;

    return kregs.x.ax;

}

/* ---- Test for keystroke ---- */
BOOL keyhit(void)
{
#if MSC | WATCOM
    return (kbhit() ? TRUE : FALSE);
#else
    return (Xbioskey(1) != 0) ? TRUE : FALSE;
#endif
}

/* ---- Read a keystroke ---- */
int getkey(void)
{
    unsigned int c;
    unsigned int theShift;
    unsigned int theScan;

    while (keyhit() == FALSE);          /* Wait for a key */
    c=Xbioskey(0);                      /* Fetch key */
    theShift=getshift();
    theScan=c >> 8;                     /* Scan code */
    c=c & 0xff;                         /* ASCII code or 0 of 0xe0 */

    if (theShift & (LEFTSHIFT|RIGHTSHIFT))
        {
        /* BIOS normally calls shift-ins "ins" and shift-del "del" */
        if (theScan == 0x52)            /* INS */
            return CTRL_V;              /* SHIFT_INS */

        if (theScan == 0x53)            /* DEL */
            return CTRL_X;              /* SHIFT_DEL */

        }

    if ((theShift & ALTKEY) && (theScan == 0x0e)) /* Alt-BS  */
        return CTRL_Z;                  /* ALT_BS */

    if (theShift & CTRLKEY)
        {
        if (theScan == 0x92)            /* ^ins */
            {
            return CTRL_C;              /* CTRL_INS */
            }

        }

    if ((c != 0) && (c != 0xe0))        /* Nonzero / nonnumpad ASCII part? */
        return c;                       /* Then return only the ASCII part */

    /* Watch out: Special case for Russian non-numpad "0xe0 ASCII" */
    if ((c == 0xe0) && (theScan == 0))
        return 0xe0;

    return (FKEY | theScan);

}

/* ---------- read the keyboard shift status --------- */
int getshift(void)
{
    static int enhkeyb=-1;              /* 1 for an enhanced keyboard */
    static char far *kbtype = MK_FP(0x40,0x96);

    if (enhkeyb == -1)                  /* If we do not yet know... */
        {
        enhkeyb=(((*kbtype) & 0x10) != 0) ? 1 : 0; /* read BIOS data flag! */
        }                               /* Now enhkeyb is either 0 or 1 - Eric */

    if (!enhkeyb)                       /* Old/new by Eric */
        {
        regs.h.ah = 2;
        int86(KEYBRD, &regs, &regs);
        return regs.h.al;
        }
    else
        {
        regs.h.ah=0x12;                 /* Extended shift: AL as above... */
        int86(KEYBRD, &regs, &regs);

        /* Ignore SysRQ (Alt-PrtScr) and shift lock presses */
        regs.x.ax &= 0x0fff;
        if (regs.x.ax & RALTKEY)        /* Treat RALT as NO ALT (but as AltGr */
            regs.x.ax &= ~ALTKEY;

        return regs.x.ax;
        }

}

/* -------- sound a buzz tone, using hardware directly ---------- */
void beep(void)
{
    wait();
    outp(0x43, 0xb6);                   /* program the frequency */
    outp(0x42, (int) (COUNT % 256));
    outp(0x42, (int) (COUNT / 256));
    outp(0x61, inp(0x61) | 3);          /* start the sound */
    wait();
    outp(0x61, inp(0x61) & ~3);         /* stop the sound  */

}

/* -------- get the video mode and page from BIOS -------- */
void videomode(void)
{
    regs.h.ah = 15;
    int86(VIDEO, &regs, &regs);
    video_mode = regs.h.al;
    video_page = regs.x.bx;
    video_page &= 0xff00;
    video_mode &= 0x7f;

}

/* ------ position the cursor ------ */
void cursor(int x, int y)
{
    videomode();
    regs.x.dx=((y << 8) & 0xff00)+x;
    regs.h.ah=SETCURSOR;
    regs.x.bx=video_page;
    int86(VIDEO, &regs, &regs);

}

/* ------ get cursor shape and position ------ */
static void near getcursor(void)
{
    videomode();
    regs.h.ah=READCURSOR;
    regs.x.bx=video_page;
    int86(VIDEO, &regs, &regs);

}

/* ------- get the current cursor position ------- */
void curr_cursor(int *x, int *y)
{
    getcursor();
    *x=regs.h.dl;
    *y=regs.h.dh;

}

/* ------ save the current cursor configuration ------ */
void savecursor(void)
{
    if (cs < MAXSAVES)
        {
        getcursor();
        cursorshape[cs]=regs.x.cx;
        cursorpos[cs]=regs.x.dx;
        cs++;
        }

}

/* ---- restore the saved cursor configuration ---- */
void restorecursor(void)
{
    if (cs)
        {
        --cs;
        videomode();
        regs.x.dx=cursorpos[cs];
        regs.h.ah=SETCURSOR;
        regs.x.bx=video_page;
        int86(VIDEO, &regs, &regs);
        set_cursor_type(cursorshape[cs]);
        }

}

/* ------ make a normal cursor ------ */
void normalcursor(void)
{
    set_cursor_type(0x0106);

}

/* ------ hide the cursor ------ */
void hidecursor(void)
{
    getcursor();
    regs.h.ch |= HIDECURSOR;
    regs.h.ah = SETCURSORTYPE;
    int86(VIDEO, &regs, &regs);

}

/* ------ unhide the cursor ------ */
void unhidecursor(void)
{
    getcursor();
    regs.h.ch &= ~HIDECURSOR;
    regs.h.ah = SETCURSORTYPE;
    int86(VIDEO, &regs, &regs);

}

/* ---- use BIOS to set the cursor type ---- */
void set_cursor_type(unsigned t)
{
    videomode();
    regs.h.ah=SETCURSORTYPE;
    regs.x.bx=video_page;
    regs.x.cx=t;
    int86(VIDEO, &regs, &regs);

}

/* ---- test for EGA -------- */
BOOL isEGA(void)
{
    if (isVGA())
        return FALSE;

    regs.h.ah=0x12;
    regs.h.bl=0x10;
    int86(VIDEO, &regs, &regs);
    return regs.h.bl != 0x10;

}

/* ---- test for VGA -------- */
BOOL isVGA(void)
{
    regs.x.ax=0x1a00;
    int86(VIDEO, &regs, &regs);
    return regs.h.al == 0x1a && regs.h.bl > 6;

}

static void Scan350(void)
{
    regs.x.ax=0x1201;
    regs.h.bl=0x30;
    int86(VIDEO, &regs, &regs);
    regs.h.ah=0x0f;
    int86(VIDEO, &regs, &regs);
    regs.h.ah=0x00;
    int86(VIDEO, &regs, &regs);

}

static void Scan400(void)
{
    regs.x.ax=0x1202;
    regs.h.bl=0x30;
    int86(VIDEO, &regs, &regs);
    regs.h.ah=0x0f;
    int86(VIDEO, &regs, &regs);
    regs.h.ah=0x00;
    int86(VIDEO, &regs, &regs);

}

/* ---------- set 25 line mode ------- */
void Set25(void)
{
    if (isVGA())
        {
        Scan400();
        regs.x.ax=0x1114;
	}
    else
        regs.x.ax=0x1111;

    regs.h.bl=0;
    int86(VIDEO, &regs, &regs);

}

/* ---------- set 43 line mode ------- */
void Set43(void)
{
    if (isVGA())
        Scan350();

    regs.x.ax=0x1112;
    regs.h.bl=0;
    int86(VIDEO, &regs, &regs);

}

/* ---------- set 50 line mode ------- */
void Set50(void)
{
    if (isVGA())
        Scan400();

    regs.x.ax=0x1112;
    regs.h.bl=0;
    int86(VIDEO, &regs, &regs);

}

/* ------ convert an Alt+ key to its letter equivalent ----- */
int AltConvert(int c)
{
    int i,a=0;

    for (i=0;i<36;i++)
        if (c == altconvert[i])
            break;

    if (i < 26)
        a='a'+i;
    else if (i < 36)
        a='0'+i-26;

    return a;

}

#if MSC | WATCOM
int getdisk(void)
{
    unsigned int cd;

    _dos_getdrive(&cd);
    cd -= 1;
    return cd;
}
#endif