1 /* pMARS -- a portable Memory Array Redcode Simulator
2 * Copyright (C) 1993-1995 Albert Ma, Na'ndor Sieben, Stefan Strack and Mintardjo Wangsawidjaja
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /*
20 * xgraphio.c: input/output functions for DOS DJGPP/GRX display
21 *
22 * $Id: xgraphio.c,v 1.1.1.1 2000/08/20 13:29:48 iltzu Exp $
23 */
24
25 #include <bios.h>
26 /* #include <alloc.h> */
27 #include <string.h>
28 #include <conio.h>
29 #include <dos.h>
30
31 #define ESC 27
32 #define BS 8
33 #define LF 10
34 #define CR 13
35 #define CLEAR 219
36 #define MAXLENGTH 160
37 #define HORIZSPACE 8
38
39 int posx, posy; /* for current panel */
40 int posx1, posy1; /* for panel 1 */
41 int posx2, posy2; /* for panel 2 */
42 int scrollx1, scrollx2;
43 char outstr[MAXLENGTH];
44 int point;
45 int grwindx0;
46 int grwindy0;
47 int grwindx1;
48 int grwindy1;
49 int verspace = 8;
50 int scrollPixel; /* how many pixels in a scrolled line */
51 int scrollLines; /* hom many lines to scroll */
52 int lastLine; /* last scrolled line */
53 int bgiTextLines; /* how many lines has cdb */
54 char str[2] = " ";
55 int graphioColor;
56 extern int curPanel;
57
58 extern char *ckey2macro(int ccode, char *buf);
59 extern char *ukey2macro(int, char *);
60 extern char *xkey2macro(int, char *);
61
62 void grputs(char *);
63
64 void
grupdate(int newcurPanel)65 grupdate(int newcurPanel)
66 {
67 if (curPanel == newcurPanel)
68 return;
69 if (curPanel == 0 && newcurPanel != 0) {
70 posx1 = posx;
71 posy1 = posy;
72 grwindx0 = GrMaxX() / 2;
73 grwindx1 = GrMaxX() - HORIZSPACE;
74 grclear();
75 posx2 = posx;
76 posy2 = posy;
77 curPanel = 2;
78 }
79 switch (newcurPanel) {
80 case 0: /* only one panel */
81 if (curPanel == 1) {
82 posx1 = posx;
83 posy1 = posy;
84 }
85 grwindx0 = GrMaxX() / 2;
86 grwindx1 = GrMaxX() - HORIZSPACE;
87 grclear();
88 grwindx0 = 1;
89 grwindx1 = GrMaxX() - HORIZSPACE;
90 posx = posx1;
91 posy = posy1;
92 scrollx1 = 1;
93 scrollx2 = 79;
94 break;
95 case 1: /* first panel */
96 grwindx0 = 1;
97 grwindx1 = GrMaxX() / 2 - HORIZSPACE;
98 if (curPanel == 2) {
99 posx2 = posx;
100 posy2 = posy;
101 posx = posx1;
102 posy = posy1;
103 }
104 scrollx1 = 1;
105 scrollx2 = 39;
106 break;
107 case 2: /* second panel */
108 grwindx0 = GrMaxX() / 2;
109 grwindx1 = GrMaxX() - 10;
110 switch (curPanel) {
111 case 1:
112 posx1 = posx;
113 posy1 = posy;
114 posx = posx2;
115 posy = posy2;
116 break;
117 case 0:
118 posx1 = posx;
119 posy1 = posy;
120 grclear();
121 }
122 scrollx1 = 40;
123 scrollx2 = 79;
124 break;
125 }
126 curPanel = newcurPanel;
127 }
128
129 void
graphio_init(void)130 graphio_init(void)
131 {
132 switch (graphdriver) {
133 default:scrollPixel = 16;
134 lastLine = 29;
135 verspace = 10; /* 8 */
136 break;
137 /* case EGA : scrollPixel= 14;
138 lastLine = 24;
139 verspace = 8;
140 break;
141 case CGA : scrollPixel= 8;
142 lastLine = 24;
143 verspace = 8;
144 */
145 }
146 scrollLines = (GrMaxY() - grwindy0) / scrollPixel;
147 grwindy0 = 1 + GrMaxY() - scrollPixel * (scrollLines);
148 bgiTextLines = (GrMaxY() - grwindy0) / verspace; /* used for pausing in
149 * cdb */
150 }
151
152
153 void
grclear()154 grclear()
155 {
156 posx = grwindx0;
157 posy = grwindy0;
158 /* setfillstyle(EMPTY_FILL, WHITE); */
159 GrFilledBox(grwindx0, grwindy0, grwindx1 + HORIZSPACE - 1, GrMaxY(), GrBlack());
160 }
161
162 void
newline(void)163 newline(void)
164 {
165 union REGS regs;
166 posx = grwindx0;
167 posy += verspace;
168 if (posy > grwindy1) {
169 posy -= verspace;
170 switch (graphdriver) {
171 default:
172 GrBitBlt(NULL, grwindx0, grwindy0, NULL, grwindx0, grwindy0 + verspace,
173 grwindx1 + HORIZSPACE - 1, GrMaxX(), GrWRITE);
174 GrFilledBox(grwindx0, posy, grwindx1 + HORIZSPACE - 1, GrMaxY(), GrBlack());
175 /* case VGA :
176 case EGA :
177 case CGA :
178
179 regs.x.ax=0x0600+ 1 ;
180 regs.x.bx= 0;
181 regs.x.cx=( (lastLine-scrollLines+1) <<8)+ scrollx1 ;
182 regs.x.dx=( lastLine <<8)+ scrollx2 ;
183 int86(0x10,®s,®s);
184 posy -= (scrollPixel-verspace) ;
185 break;
186 default:
187 grclear();
188 */
189 }
190 }
191 }
192
193 void
newchar(void)194 newchar(void)
195 {
196 posx += HORIZSPACE;
197 if (posx > grwindx1 - HORIZSPACE)
198 newline();
199 }
200
201 #define cursoron() outtextxy(posx, posy, "_")
202
203 #define cursoroff() setcolor(BLACK); \
204 str[0]=CLEAR; \
205 outtextxy(posx, posy, str); \
206 setcolor(graphioColor)
207
208 void
delchar(void)209 delchar(void)
210 {
211 if (point) {
212 cursoroff();
213 posx -= HORIZSPACE;
214 if (posx < grwindx0) {
215 posx = grwindx0;
216 cursoron();
217 } else {
218 --point;
219 cursoroff();
220 cursoron();
221 }
222 }
223 }
224
225 char *
grgets(char * result,int maxchar)226 grgets(char *result, int maxchar)
227 {
228 if (inputRedirection) {
229 return fgets(result, maxchar, stdin);
230 } else {
231 int lo;
232 result[0] = 0;
233 point = 0;
234 cursoron();
235
236 do {
237 if (mouse_or_key(result))
238 return result;
239 lo = getch();
240 if (lo) {
241 switch (lo) {
242 case BS:
243 delchar();
244 break;
245 case ESC:
246 while (point)
247 delchar();
248 break;
249 case CR:
250 cursoroff();
251 newline();
252 break;
253 /* case '\t':
254 cursoroff();
255 grputs(" ");
256 lo=' '; I'd like tab to be a macro for switch, Nandor */
257 default:
258 if (lo >= 32 && lo < 128) {
259 result[point++] = lo;
260 if (point > maxchar - 1)
261 lo = CR;
262 cursoroff();
263 str[0] = lo;
264 outtextxy(posx, posy, str);
265 newchar();
266 cursoron();
267 } else if (lo > 127) {
268 ukey2macro(lo, result);
269 cursoroff();
270 grputs(result);
271 return result;
272 } else {
273 ckey2macro(lo, result);
274 cursoroff();
275 grputs(result);
276 return result;
277 }
278 }
279
280 } else {
281 xkey2macro(getch(), result);
282 cursoroff();
283 grputs(result); /* Stefan you might want to change this,
284 * Nandor */
285 return result;
286 }
287 } while (lo != CR);
288 result[point++] = '\n';
289 result[point] = 0;
290 if (point)
291 return result;
292 else
293 return NULL;
294 } /* !inputRedirection */
295 }
296
297 void
grputs(char * sss)298 grputs(char *sss)
299 {
300 if (printAttr) {
301 graphioColor = colors[printAttr - 1];
302 setcolor(graphioColor);
303 } else {
304 graphioColor = WHITE;
305 setcolor(graphioColor);
306 }
307 while (*sss) {
308 switch (*sss) {
309 case '\n':
310 newline();
311 break;
312 default:
313 str[0] = *sss;
314 outtextxy(posx, posy, str);
315 newchar();
316 }
317 ++sss;
318 }
319 }
320
321
322
323 /*
324 * void main(void) { char result[160]; int graphdriver =
325 * DETECT; int graphmode;
326 *
327 * initgraph(&graphdriver, &graphmode, ""); grclear();
328 *
329 * grgets(result,10); grputs("first\nsecond"); grclear(); }
330 *
331 */
332