1 /****************************************************************************
2 * This is the implementation file for conio.h - a conio.h for Linux. *
3 * It uses ncurses and some internal functions of Linux to simulate the *
5 * This is copyright (c) 1996,97 by Fractor / Mental eXPlosion (MXP) *
6 * Use and distribution is only allowed if you follow the terms of the *
7 * GNU Library Public License Version 2. *
8 * Since this work is based on ncurses please read its copyright notices as *
10 * Look into the readme to this file for further information. *
11 * Thanx to SubZero / MXP for his little tutorial on the curses library ! *
12 * Many thanks to Mark Hahn and Rich Cochran for solving the inpw and inpd *
14 * Watch out for other MXP releases, too ! *
15 * Send bugreports to: fractor@germanymail.com *
16 ****************************************************************************/
18 #define _ISOC99_SOURCE
28 static attr_t txtattr,oldattr;
35 /* Some internals... */
36 static int colortab(int a) /* convert LINUX Color code to DOS-standard */
39 case 0 : return COLOR_BLACK;
40 case 1 : return COLOR_BLUE;
41 case 2 : return COLOR_GREEN;
42 case 3 : return COLOR_CYAN;
43 case 4 : return COLOR_RED;
44 case 5 : return COLOR_MAGENTA;
45 case 6 : return COLOR_YELLOW;
46 case 7 : return COLOR_WHITE;
52 static void docolor (int color) /* Set DOS-like text mode colors */
54 wattrset(conio_scr,0); /* My curses-version needs this ... */
55 if ((color&128)==128) txtattr=A_BLINK; else txtattr=A_NORMAL;
56 if ((color&15)>7) txtattr|=A_STANDOUT; else txtattr|=A_NORMAL;
57 txtattr|=COLOR_PAIR((1+(color&7)+(color&112)) >> 1);
58 if (((color&127)==15) | ((color&127)==7)) txtattr=COLOR_PAIR(0);
59 if (((color&127)==127) | ((color&127)==119)) txtattr=COLOR_PAIR(8);
60 wattron(conio_scr,txtattr);
61 wattron(conio_scr,COLOR_PAIR(1+(color&7)+((color&112)>>1)));
65 /* Call this before any call to linux conio - except the port functions ! */
66 void __attribute__((constructor)) initconio (void) /* This is needed, because ncurses needs to be initialized */
70 if (atexit(doneconio)){
71 fprintf(stderr,"Unable to register doneconio(), exiting...\n");
76 // attr_get(&oldattr,&pair,NULL);
79 if (!has_colors() & (color_warning>-1))
80 fprintf(stderr,"Attention: A color terminal may be required to run this application !\n");
82 conio_scr=newwin(0,0,0,0);
83 keypad(conio_scr,TRUE);
85 idlok(conio_scr,TRUE);
86 scrollok(conio_scr,TRUE);
87 /* Color initialization */
90 init_pair((8*y)+x+1, colortab(x), colortab(y));
91 wattr_get(conio_scr,&txtattr,&pair,NULL);
98 /* Call this on exiting your program */
104 /* Here it starts... */
105 void _setcursortype (int cur_t)
111 char *cgets (char *str) /* ugly function :-( */
116 strncpy(strng,str,1);
117 wgetnstr(conio_scr,&strng[2],(int) strng[0]);
118 while (strng[i]!=0) i++;
121 memcpy(str,strng,i+3);
127 /* clear to end of line -- can be a no-op in tty mode */
130 wclrtoeol(conio_scr);
138 wmove(conio_scr,0,0);
142 int cprintf (char *format, ... )
145 char buffer[BUFSIZ]; /* Well, BUFSIZ is from ncurses... */
147 va_start(argp,format);
148 vsprintf(buffer,format,argp);
150 i=waddstr(conio_scr,buffer);
155 void cputs (char *str)
157 waddstr(conio_scr,str);
161 int cscanf (const char *format, ...)
164 char buffer[BUFSIZ]; /* See cprintf */
167 if (wgetstr(conio_scr,buffer)==ERR) return(-1);
168 va_start(argp,format);
169 i=vsscanf(buffer,format,argp);
177 wdeleteln(conio_scr);
191 void gettextinfo(struct text_info *inforec)
195 unsigned char x1,x2,y1,y2;
196 attr_t dattr,dnattr,a; /* The "d" stands for DOS */
197 getyx(conio_scr,yp,xp);
198 getbegyx(conio_scr,y1,x1);
199 getmaxyx(conio_scr,y2,x2);
201 wattr_get(conio_scr,&a,&pair,NULL);
202 if (a==(a & A_BLINK)) dattr=dattr+128;
203 dnattr=oldattr; /* Well, this cannot be right,
204 because we don't know the COLORPAIR values from before initconio() !*/
205 inforec->winleft=x1+1;
206 inforec->wintop=y1+1;
209 inforec->winright=x1+x2+1;
210 inforec->winbottom=y1+y2+1;
213 inforec->screenheight=y2+1;
214 inforec->screenwidth=x2+1;
215 inforec->currmode=3; /* This is C80 */
216 inforec->normattr=dnattr; /* Don't use this ! */
217 inforec->attribute=dattr;
220 void gotoxy (int x, int y)
221 /* address cusor -- OK for this to be a no-op in TTY mode */
226 wmove(conio_scr,y,x);
231 void highvideo (void)
234 textcolor(15); /* White */
235 textbackground(0); /* Black */
241 winsertln(conio_scr);
248 nodelay(conio_scr,TRUE);
250 nodelay(conio_scr,FALSE);
257 textbackground(0); /* Black */
258 textcolor(8); /* Should be darkgray */
261 void normvideo (void)
263 wattrset(conio_scr,oldattr);
268 if (waddch(conio_scr,c)!=ERR) {
275 void textattr (int attr)
280 void textbackground (int color)
288 void textcolor (int color)
297 void textmode (int mode)
306 getyx(conio_scr,y,x);
315 getyx(conio_scr,y,x);
320 void window (int left,int top,int right,int bottom)
330 if (top==0) nlines++;
331 if (left==0) ncols++;
332 if ((nlines<1) | (ncols<1)) return;
333 conio_scr=newwin(nlines,ncols,top,left);
334 keypad(conio_scr,TRUE);
335 meta(conio_scr,TRUE);
336 idlok(conio_scr,TRUE);
337 scrollok(conio_scr,TRUE);