1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <emx/syscalls.h>
4 #include <sys/video.h>
5 
6 #include "libemx.h"
7 
8 
9 static int cursor_start = 0, cursor_end = 0;
10 static int gx = 0, gy = 0, gxx = 79, gyy = 24;
11 static char buf[4096];
12 
13 
14 
15 
init_emx()16 void init_emx()
17 {
18     v_init();
19     v_getctype(&cursor_start, &cursor_end);
20 }
21 
22 
23 
24 
deinit_emx()25 void deinit_emx()
26 {
27     // nothing to do
28 }
29 
30 
31 
32 
_setcursortype(int curstype)33 void _setcursortype(int curstype)
34 {
35     if ( curstype == _NOCURSOR )
36       v_hidecursor();
37     else
38       v_ctype(cursor_start, cursor_end);
39 }
40 
41 
42 
43 
clrscr()44 void clrscr()
45 {
46     if ( (gx == 0) && (gy == 0) && (gxx == 79) && (gyy == 24) )
47       {
48         v_clear();
49         v_gotoxy(0, 0);
50       }
51     else
52       {
53         for (int i = gy; i <= gyy; ++i)
54           {
55             v_gotoxy(gx, i);
56             v_putn(' ', gxx - gx + 1);
57           }
58         v_gotoxy(gx, gy);
59       }
60 }
61 
62 
63 
64 
gotoxy(int x,int y)65 void gotoxy(int x, int y)
66 {
67     v_gotoxy(x - 1 + gx, y - 1 + gy);
68 }
69 
70 
71 
72 
textcolor(int c)73 void textcolor(int c)
74 {
75     v_attrib(c);
76 }
77 
78 
79 
80 
cprintf_aux(const char * s)81 static void cprintf_aux(const char *s)
82 {
83     char *ptr = buf;
84 
85     while (*s)
86     {
87         if ( *s == '\n' )
88           {
89             *ptr = 0;
90             v_puts(buf);
91             int x, y;
92 
93             v_getxy(&x, &y);
94             if ( y != 24 )
95               v_gotoxy(gx, y + 1);
96             else
97               v_putc('\n');
98 
99             ptr = buf;
100           }
101         else if ( *s != '\r' )
102           *ptr++ = *s;
103 
104         ++s;
105     }
106 
107     *ptr = 0;
108     v_puts(buf);
109 
110 }
111 
cprintf(const char * format,...)112 void cprintf(const char *format, ...)
113 {
114    va_list argp;
115    char buffer[4096]; // one could hope it's enough
116 
117    va_start( argp, format );
118 
119    vsprintf(buffer, format, argp);
120    cprintf_aux(buffer);
121 
122    va_end(argp);
123 }
124 
puttext(int x,int y,int lx,int ly,unsigned const char * buf)125 void puttext(int x, int y, int lx, int ly, unsigned const char *buf)
126 {
127     puttext(x, y, lx, ly, (const char *) buf);
128 }
129 
130 
131 
132 
puttext(int x,int y,int lx,int ly,const char * buf)133 void puttext(int x, int y, int lx, int ly, const char *buf)
134 {
135     int count = (lx - x + 1);
136 
137     for (int i = y - 1; i < ly; ++i)
138       {
139         v_putline(buf + 2 * count * i, x - 1, i, count);
140       }
141 }
142 
143 
144 
145 
gettext(int x,int y,int lx,int ly,unsigned char * buf)146 void gettext(int x, int y, int lx, int ly, unsigned char *buf)
147 {
148     gettext(x, y, lx, ly, (char *) buf);
149 }
150 
151 
152 
153 
gettext(int x,int y,int lx,int ly,char * buf)154 void gettext(int x, int y, int lx, int ly, char *buf)
155 {
156     int count = (lx - x + 1);
157 
158     for (int i = y - 1; i < ly; ++i)
159       {
160         v_getline(buf + 2 * count * i, x - 1, i, count);
161       }
162 }
163 
164 
165 
166 
window(int x,int y,int lx,int ly)167 void window(int x, int y, int lx, int ly)
168 {
169     gx = x - 1;
170     gy = y - 1;
171     gxx = lx - 1;
172     gyy = ly - 1;
173 }
174 
175 
176 
177 
wherex()178 int wherex()
179 {
180     int x, y;
181 
182     v_getxy(&x, &y);
183 
184     return x + 1 - gx;
185 }
186 
187 
188 
189 
wherey()190 int wherey()
191 {
192     int x, y;
193 
194     v_getxy(&x, &y);
195 
196     return y + 1 - gy;
197 }
198 
199 
200 
201 
putch(char c)202 void putch(char c)
203 {
204     v_putc(c);
205 }
206 
207 
208 
209 
kbhit()210 int kbhit()
211 {
212     return 0;
213 }
214 
215 
216 
217 
delay(int ms)218 void delay(int ms)
219 {
220     __sleep2(ms);
221 }
222 
223 
224 
225 
textbackground(int c)226 void textbackground(int c)
227 {
228     if ( c != 0 )
229       {
230         fprintf(stderr, "bad background=%d", c);
231         exit(1);
232       }
233 }
234