Debian Codegraph @

src/dos/dev_con.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
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 /* $Id: dev_con.h,v 1.35 2009-05-27 09:15:41 qbix79 Exp $ */
20
21 #include "dos_inc.h"
22 #include "../ints/int10.h"
23 #include <string.h>
24
25 #define NUMBER_ANSI_DATA 10
26
27 class device_CON : public DOS_Device {
28 public:
29 device_CON();
30 bool Read(Bit8u * data,Bit16u * size);
31 bool Write(Bit8u * data,Bit16u * size);
32 bool Seek(Bit32u * pos,Bit32u type);
33 bool Close();
34 void ClearAnsi(void);
35 Bit16u GetInformation(void);
36 bool ReadFromControlChannel(PhysPt bufptr,Bit16u size,Bit16u * retcode){return false;}
37 bool WriteToControlChannel(PhysPt bufptr,Bit16u size,Bit16u * retcode){return false;}
38 private:
39 Bit8u readcache;
40 Bit8u lastwrite;
41 struct ansi { /* should create a constructor which fills them with the appriorate values */
42 bool esc;
43 bool sci;
44 bool enabled;
45 Bit8u attr;
46 Bit8u data[NUMBER_ANSI_DATA];
47 Bit8u numberofarg;
48 Bit16u nrows;
49 Bit16u ncols;
50 Bit8s savecol;
51 Bit8s saverow;
52 bool warned;
53 } ansi;
54 };
55
56 bool device_CON::Read(Bit8u * data,Bit16u * size) {
57 Bit16u oldax=reg_ax;
58 Bit16u count=0;
59 if ((readcache) && (*size)) {
60 data[count++]=readcache;
61 if(dos.echo) INT10_TeletypeOutput(readcache,7);
62 readcache=0;
63 }
64 while (*size>count) {
65 reg_ah=(IS_EGAVGA_ARCH)?0x10:0x0;
66 CALLBACK_RunRealInt(0x16);
67 switch(reg_al) {
68 case 13:
69 data[count++]=0x0D;
70 if (*size>count) data[count++]=0x0A; // it's only expanded if there is room for it. (NO cache)
71 *size=count;
72 reg_ax=oldax;
73 if(dos.echo) {
74 INT10_TeletypeOutput(13,7); //maybe don't do this ( no need for it actually ) (but it's compatible)
75 INT10_TeletypeOutput(10,7);
76 }
77 return true;
78 break;
79 case 8:
80 if(*size==1) data[count++]=reg_al; //one char at the time so give back that BS
81 else if(count) { //Remove data if it exists (extended keys don't go right)
82 data[count--]=0;
83 INT10_TeletypeOutput(8,7);
84 INT10_TeletypeOutput(' ',7);
85 } else {
86 continue; //no data read yet so restart whileloop.
87 }
88 break;
89 case 0xe0: /* Extended keys in the int 16 0x10 case */
90 if(!reg_ah) { /*extended key if reg_ah isn't 0 */
91 data[count++] = reg_al;
92 } else {
93 data[count++] = 0;
94 if (*size>count) data[count++] = reg_ah;
95 else readcache = reg_ah;
96 }
97 break;
98 case 0: /* Extended keys in the int 16 0x0 case */
99 data[count++]=reg_al;
100 if (*size>count) data[count++]=reg_ah;
101 else readcache=reg_ah;
102 break;
103 default:
104 data[count++]=reg_al;
105 break;
106 }
107 if(dos.echo) { //what to do if *size==1 and character is BS ?????
108 INT10_TeletypeOutput(reg_al,7);
109 }
110 }
111 *size=count;
112 reg_ax=oldax;
113 return true;
114 }
115
116
117 bool device_CON::Write(Bit8u * data,Bit16u * size) {
118 Bit16u count=0;
119 Bitu i;
120 Bit8u col,row;
121 Bit8u tempdata;
122 while (*size>count) {
123 if (!ansi.esc){
124 if(data[count]=='\033') {
125 /*clear the datastructure */
126 ClearAnsi();
127 /* start the sequence */
128 ansi.esc=true;
129 count++;
130 continue;
131 } else {
132 /* Some sort of "hack" now that \n doesn't set col to 0 (int10_char.cpp old chessgame) */
133 if((data[count] == '\n') && (lastwrite != '\r')) INT10_TeletypeOutputAttr('\r',ansi.attr,ansi.enabled);
134 /* pass attribute only if ansi is enabled */
135 INT10_TeletypeOutputAttr(data[count],ansi.attr,ansi.enabled);
136 lastwrite = data[count++];
137 continue;
138 }
139 }
140
141 if(!ansi.sci){
142
143 switch(data[count]){
144 case '[':
145 ansi.sci=true;
146 break;
147 case '7': /* save cursor pos + attr */
148 case '8': /* restore this (Wonder if this is actually used) */
149 case 'D':/* scrolling DOWN*/
150 case 'M':/* scrolling UP*/
151 default:
152 LOG(LOG_IOCTL,LOG_NORMAL)("ANSI: unknown char %c after a esc",data[count]); /*prob () */
153 ClearAnsi();
154 break;
155 }
156 count++;
157 continue;
158 }
159 /*ansi.esc and ansi.sci are true */
160 Bit8u page = real_readb(BIOSMEM_SEG,BIOSMEM_CURRENT_PAGE);
161 switch(data[count]){
162 case '0':
163 case '1':
164 case '2':
165 case '3':
166 case '4':
167 case '5':
168 case '6':
169 case '7':
170 case '8':
171 case '9':
172 ansi.data[ansi.numberofarg]=10*ansi.data[ansi.numberofarg]+(data[count]-'0');
173 break;
174 case ';': /* till a max of NUMBER_ANSI_DATA */
175 ansi.numberofarg++;
176 break;
177 case 'm': /* SGR */
178 for(i=0;i<=ansi.numberofarg;i++){
179 ansi.enabled=true;
180 switch(ansi.data[i]){
181 case 0: /* normal */
182 ansi.attr=0x07;//Real ansi does this as well. (should do current defaults)
183 ansi.enabled=false;
184 break;
185 case 1: /* bold mode on*/
186 ansi.attr|=0x08;
187 break;
188 case 4: /* underline */
189 LOG(LOG_IOCTL,LOG_NORMAL)("ANSI:no support for underline yet");
190 break;
191 case 5: /* blinking */
192 ansi.attr|=0x80;
193 break;
194 case 7: /* reverse */
195 ansi.attr=0x70;//Just like real ansi. (should do use current colors reversed)
196 break;
197 case 30: /* fg color black */
198 ansi.attr&=0xf8;
199 ansi.attr|=0x0;
200 break;
201 case 31: /* fg color red */
202 ansi.attr&=0xf8;
203 ansi.attr|=0x4;
204 break;
205 case 32: /* fg color green */
206 ansi.attr&=0xf8;
207 ansi.attr|=0x2;
208 break;
209 case 33: /* fg color yellow */
210 ansi.attr&=0xf8;
211 ansi.attr|=0x6;
212 break;
213 case 34: /* fg color blue */
214 ansi.attr&=0xf8;
215 ansi.attr|=0x1;
216 break;
217 case 35: /* fg color magenta */
218 ansi.attr&=0xf8;
219 ansi.attr|=0x5;
220 break;
221 case 36: /* fg color cyan */
222 ansi.attr&=0xf8;
223 ansi.attr|=0x3;
224 break;
225 case 37: /* fg color white */
226 ansi.attr&=0xf8;
227 ansi.attr|=0x7;
228 break;
229 case 40:
230 ansi.attr&=0x8f;
231 ansi.attr|=0x0;
232 break;
233 case 41:
234 ansi.attr&=0x8f;
235 ansi.attr|=0x40;
236 break;
237 case 42:
238 ansi.attr&=0x8f;
239 ansi.attr|=0x20;
240 break;
241 case 43:
242 ansi.attr&=0x8f;
243 ansi.attr|=0x60;
244 break;
245 case 44:
246 ansi.attr&=0x8f;
247 ansi.attr|=0x10;
248 break;
249 case 45:
250 ansi.attr&=0x8f;
251 ansi.attr|=0x50;
252 break;
253 case 46:
254 ansi.attr&=0x8f;
255 ansi.attr|=0x30;
256 break;
257 case 47:
258 ansi.attr&=0x8f;
259 ansi.attr|=0x70;
260 break;
261 default:
262 break;
263 }
264 }
265 ClearAnsi();
266 break;
267 case 'f':
268 case 'H':/* Cursor Pos*/
269 if(!ansi.warned) { //Inform the debugger that ansi is used.
270 ansi.warned = true;
271 LOG(LOG_IOCTL,LOG_WARN)("ANSI SEQUENCES USED");
272 }
273 /* Turn them into positions that are on the screen */
274 if(ansi.data[0] == 0) ansi.data[0] = 1;
275 if(ansi.data[1] == 0) ansi.data[1] = 1;
276 if(ansi.data[0] > ansi.nrows) ansi.data[0] = (Bit8u)ansi.nrows;
277 if(ansi.data[1] > ansi.ncols) ansi.data[1] = (Bit8u)ansi.ncols;
278 INT10_SetCursorPos(--(ansi.data[0]),--(ansi.data[1]),page); /*ansi=1 based, int10 is 0 based */
279 ClearAnsi();
280 break;
281 /* cursor up down and forward and backward only change the row or the col not both */
282 case 'A': /* cursor up*/
283 col=CURSOR_POS_COL(page) ;
284 row=CURSOR_POS_ROW(page) ;
285 tempdata = (ansi.data[0]? ansi.data[0] : 1);
286 if(tempdata > row) { row=0; }
287 else { row-=tempdata;}
288 INT10_SetCursorPos(row,col,page);
289 ClearAnsi();
290 break;
291 case 'B': /*cursor Down */
292 col=CURSOR_POS_COL(page) ;
293 row=CURSOR_POS_ROW(page) ;
294 tempdata = (ansi.data[0]? ansi.data[0] : 1);
295 if(tempdata + static_cast<Bitu>(row) >= ansi.nrows)
296 { row = ansi.nrows - 1;}
297 else { row += tempdata; }
298 INT10_SetCursorPos(row,col,page);
299 ClearAnsi();
300 break;
301 case 'C': /*cursor forward */
302 col=CURSOR_POS_COL(page);
303 row=CURSOR_POS_ROW(page);
304 tempdata=(ansi.data[0]? ansi.data[0] : 1);
305 if(tempdata + static_cast<Bitu>(col) >= ansi.ncols)
306 { col = ansi.ncols - 1;}
307 else { col += tempdata;}
308 INT10_SetCursorPos(row,col,page);
309 ClearAnsi();
310 break;
311 case 'D': /*Cursor Backward */
312 col=CURSOR_POS_COL(page);
313 row=CURSOR_POS_ROW(page);
314 tempdata=(ansi.data[0]? ansi.data[0] : 1);
315 if(tempdata > col) {col = 0;}
316 else { col -= tempdata;}
317 INT10_SetCursorPos(row,col,page);
318 ClearAnsi();
319 break;
320 case 'J': /*erase screen and move cursor home*/
321 if(ansi.data[0]==0) ansi.data[0]=2;
322 if(ansi.data[0]!=2) {/* every version behaves like type 2 */
323 LOG(LOG_IOCTL,LOG_NORMAL)("ANSI: esc[%dJ called : not supported handling as 2",ansi.data[0]);
324 }
325 INT10_ScrollWindow(0,0,255,255,0,ansi.attr,page);
326 ClearAnsi();
327 INT10_SetCursorPos(0,0,page);
328 break;
329 case 'h': /* SET MODE (if code =7 enable linewrap) */
330 case 'I': /* RESET MODE */
331 LOG(LOG_IOCTL,LOG_NORMAL)("ANSI: set/reset mode called(not supported)");
332 ClearAnsi();
333 break;
334 case 'u': /* Restore Cursor Pos */
335 INT10_SetCursorPos(ansi.saverow,ansi.savecol,page);
336 ClearAnsi();
337 break;
338 case 's': /* SAVE CURSOR POS */
339 ansi.savecol=CURSOR_POS_COL(page);
340 ansi.saverow=CURSOR_POS_ROW(page);
341 ClearAnsi();
342 break;
343 case 'K': /* erase till end of line (don't touch cursor) */
344 col = CURSOR_POS_COL(page);
345 row = CURSOR_POS_ROW(page);
346 INT10_WriteChar(' ',ansi.attr,page,ansi.ncols-col,true); //Use this one to prevent scrolling when end of screen is reached
347 //for(i = col;i<(Bitu) ansi.ncols; i++) INT10_TeletypeOutputAttr(' ',ansi.attr,true);
348 INT10_SetCursorPos(row,col,page);
349 ClearAnsi();
350 break;
351 case 'M': /* delete line (NANSI) */
352 col = CURSOR_POS_COL(page);
353 row = CURSOR_POS_ROW(page);
354 INT10_ScrollWindow(row,0,ansi.nrows-1,ansi.ncols-1,ansi.data[0]? -ansi.data[0] : -1,ansi.attr,0xFF);
355 ClearAnsi();
356 break;
357 case 'l':/* (if code =7) disable linewrap */
358 case 'p':/* reassign keys (needs strings) */
359 case 'i':/* printer stuff */
360 default:
361 LOG(LOG_IOCTL,LOG_NORMAL)("ANSI: unhandled char %c in esc[",data[count]);
362 ClearAnsi();
363 break;
364 }
365 count++;
366 }
367 *size=count;
368 return true;
369 }
370
371 bool device_CON::Seek(Bit32u * pos,Bit32u type) {
372 // seek is valid
373 *pos = 0;
374 return true;
375 }
376
377 bool device_CON::Close() {
378 return true;
379 }
380
381 Bit16u device_CON::GetInformation(void) {
382 Bit16u head=mem_readw(BIOS_KEYBOARD_BUFFER_HEAD);
383 Bit16u tail=mem_readw(BIOS_KEYBOARD_BUFFER_TAIL);
384
385 if ((head==tail) && !readcache) return 0x80D3; /* No Key Available */
386 if (readcache || real_readw(0x40,head)) return 0x8093; /* Key Available */
387
388 /* remove the zero from keyboard buffer */
389 Bit16u start=mem_readw(BIOS_KEYBOARD_BUFFER_START);
390 Bit16u end =mem_readw(BIOS_KEYBOARD_BUFFER_END);
391 head+=2;
392 if (head>=end) head=start;
393 mem_writew(BIOS_KEYBOARD_BUFFER_HEAD,head);
394 return 0x80D3; /* No Key Available */
395 }
396
397 device_CON::device_CON() {
398 SetName("CON");
399 readcache=0;
400 lastwrite=0;
401 ansi.enabled=false;
402 ansi.attr=0x7;
403 ansi.ncols=real_readw(BIOSMEM_SEG,BIOSMEM_NB_COLS); //should be updated once set/reset mode is implemented
404 ansi.nrows=real_readb(BIOSMEM_SEG,BIOSMEM_NB_ROWS) + 1;
405 ansi.saverow=0;
406 ansi.savecol=0;
407 ansi.warned=false;
408 ClearAnsi();
409 }
410
411 void device_CON::ClearAnsi(void){
412 for(Bit8u i=0; i<NUMBER_ANSI_DATA;i++) ansi.data[i]=0;
413 ansi.esc=false;
414 ansi.sci=false;
415 ansi.numberofarg=0;
416 }