download, progress, improved screen updates...
[oftp] / src / dos / tgfx.c
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <dos.h>
4 #include <conio.h>
5 #include "tgfx.h"
6
7 static unsigned short attr = 0x0700;
8 static int bgcol = 0, fgcol = 7;
9 static int bgchar = ' ';
10 static unsigned short *framebuf = (unsigned short*)0xb8000;
11
12 #define UPD_ATTR        attr = (fgcol << 8) | (bgcol << 12)
13
14 void tg_init(void)
15 {
16 }
17
18 void tg_cleanup(void)
19 {
20 }
21
22 void tg_redraw(void)
23 {
24 }
25
26 void tg_clear(void)
27 {
28         tg_rect(0, 0, 0, 80, 25, 0);
29 }
30
31 void tg_fgcolor(int col)
32 {
33         fgcol = col & 0xf;
34         UPD_ATTR;
35 }
36
37 void tg_bgcolor(int col)
38 {
39         bgcol = col & 0xf;
40         UPD_ATTR;
41 }
42
43 void tg_color(int col)
44 {
45         fgcol = col & 0xf;
46         bgcol = (col >> 4) & 0xf;
47         attr = col << 8;
48 }
49
50 void tg_bgchar(int c)
51 {
52         bgchar = c;
53 }
54
55 #define CRTC_ADDR_PORT  0x3d4
56 #define CRTC_DATA_PORT  0x3d5
57 #define REG_CRTC_CURH   0xe
58 #define REG_CRTC_CURL   0xf
59
60 void tg_setcursor(int x, int y)
61 {
62         unsigned int addr = y * 80 + x;
63
64         outpw(CRTC_ADDR_PORT, (addr & 0xff00) | REG_CRTC_CURH);
65         outpw(CRTC_ADDR_PORT, (addr << 8) | REG_CRTC_CURL);
66 }
67
68 void tg_text(int x, int y, const char *fmt, ...)
69 {
70         va_list ap;
71
72         va_start(ap, fmt);
73         tg_vtext(x, y, fmt, ap);
74         va_end(ap);
75 }
76
77 void tg_vtext(int x, int y, const char *fmt, va_list ap)
78 {
79         char buf[256], *ptr;
80         unsigned short *fbptr = framebuf + y * 80 + x;
81
82         vsprintf(buf, fmt, ap);
83
84         ptr = buf;
85         while(*ptr) {
86                 *fbptr++ = *ptr++ | attr;
87         }
88 }
89
90 void tg_rect(const char *label, int x, int y, int xsz, int ysz, unsigned int flags)
91 {
92         int i, j;
93         unsigned short *fbptr = framebuf + y * 80 + x;
94
95         for(i=0; i<ysz; i++) {
96                 for(j=0; j<xsz; j++) {
97                         fbptr[j] = attr | bgchar;
98                 }
99                 fbptr += 80;
100         }
101
102         if(flags & TGFX_FRAME) {
103                 fbptr = framebuf + y * 80 + x;
104                 for(i=0; i<xsz-2; i++) {
105                         fbptr[i + 1] = attr | 0xcd;
106                         fbptr[(ysz-1) * 80 + i + 1] = attr | 0xcd;
107                 }
108                 for(i=0; i<ysz-2; i++) {
109                         fbptr[(i + 1) * 80] = attr | 0xba;
110                         fbptr[(xsz-1) + (i + 1) * 80] = attr | 0xba;
111                 }
112                 fbptr[0] = attr | 0xc9;
113                 fbptr[xsz-1] = attr | 0xbb;
114                 fbptr += (ysz - 1) * 80;
115                 fbptr[0] = attr | 0xc8;
116                 fbptr[xsz-1] = attr | 0xbc;
117         }
118
119         if(label) {
120                 tg_text(x + 2, y, "%s", label);
121         }
122 }
123
124 int tg_gchar(int gchar)
125 {
126         switch(gchar) {
127         case TGFX_LARROW:
128                 return 0x1b;
129         case TGFX_RARROW:
130                 return 0x1a;
131         default:
132                 break;
133         }
134         return '@';
135 }