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;
34 /* Some internals... */
35 static int colortab(int a) /* convert LINUX Color code to DOS-standard */
38 case 0 : return COLOR_BLACK;
39 case 1 : return COLOR_BLUE;
40 case 2 : return COLOR_GREEN;
41 case 3 : return COLOR_CYAN;
42 case 4 : return COLOR_RED;
43 case 5 : return COLOR_MAGENTA;
44 case 6 : return COLOR_YELLOW;
45 case 7 : return COLOR_WHITE;
50 static void docolor (int color) /* Set DOS-like text mode colors */
52 wattrset(conio_scr,0); /* My curses-version needs this ... */
53 if ((color&128)==128) txtattr=A_BLINK; else txtattr=A_NORMAL;
54 if ((color&15)>7) txtattr|=A_STANDOUT; else txtattr|=A_NORMAL;
55 txtattr|=COLOR_PAIR((1+(color&7)+(color&112)) >> 1);
56 if (((color&127)==15) | ((color&127)==7)) txtattr=COLOR_PAIR(0);
57 if (((color&127)==127) | ((color&127)==119)) txtattr=COLOR_PAIR(8);
58 wattron(conio_scr,txtattr);
59 wattron(conio_scr,COLOR_PAIR(1+(color&7)+((color&112)>>1)));
63 /* Call this before any call to linux conio - except the port functions ! */
64 void __attribute__((constructor)) initconio (void) /* This is needed, because ncurses needs to be initialized */
68 if (atexit(doneconio)){
69 fprintf(stderr,"Unable to register doneconio(), exiting...\n");
74 // attr_get(&oldattr,&pair,NULL);
77 if (!has_colors() & (color_warning>-1))
78 fprintf(stderr,"Attention: A color terminal may be required to run this application !\n");
80 conio_scr=newwin(0,0,0,0);
81 keypad(conio_scr,TRUE);
83 idlok(conio_scr,TRUE);
84 scrollok(conio_scr,TRUE);
85 /* Color initialization */
88 init_pair((8*y)+x+1, colortab(x), colortab(y));
89 wattr_get(conio_scr,&txtattr,&pair,NULL);
96 /* Call this on exiting your program */
102 /* Here it starts... */
103 void _setcursortype (int cur_t)
109 char *cgets (char *str) /* ugly function :-( */
114 strncpy(strng,str,1);
115 wgetnstr(conio_scr,&strng[2],(int) strng[0]);
116 while (strng[i]!=0) i++;
119 memcpy(str,strng,i+3);
126 wclrtoeol(conio_scr);
133 wmove(conio_scr,0,0);
137 int cprintf (char *format, ... )
140 char buffer[BUFSIZ]; /* Well, BUFSIZ is from ncurses... */
142 va_start(argp,format);
143 vsprintf(buffer,format,argp);
145 i=waddstr(conio_scr,buffer);
150 void cputs (char *str)
152 waddstr(conio_scr,str);
156 int cscanf (const char *format, ...)
159 char buffer[BUFSIZ]; /* See cprintf */
162 if (wgetstr(conio_scr,buffer)==ERR) return(-1);
163 va_start(argp,format);
164 i=vsscanf(buffer,format,argp);
172 wdeleteln(conio_scr);
186 void gettextinfo(struct text_info *inforec)
190 unsigned char x1,x2,y1,y2;
191 attr_t dattr,dnattr,a; /* The "d" stands for DOS */
192 getyx(conio_scr,yp,xp);
193 getbegyx(conio_scr,y1,x1);
194 getmaxyx(conio_scr,y2,x2);
196 wattr_get(conio_scr,&a,&pair,NULL);
197 if (a==(a & A_BLINK)) dattr=dattr+128;
198 dnattr=oldattr; /* Well, this cannot be right,
199 because we don't know the COLORPAIR values from before initconio() !*/
200 inforec->winleft=x1+1;
201 inforec->wintop=y1+1;
204 inforec->winright=x1+x2+1;
205 inforec->winbottom=y1+y2+1;
208 inforec->screenheight=y2+1;
209 inforec->screenwidth=x2+1;
210 inforec->currmode=3; /* This is C80 */
211 inforec->normattr=dnattr; /* Don't use this ! */
212 inforec->attribute=dattr;
215 void gotoxy (int x, int y)
219 wmove(conio_scr,y,x);
223 void highvideo (void)
225 textcolor(15); /* White */
226 textbackground(0); /* Black */
231 winsertln(conio_scr);
238 nodelay(conio_scr,TRUE);
240 nodelay(conio_scr,FALSE);
247 textbackground(0); /* Black */
248 textcolor(8); /* Should be darkgray */
251 void normvideo (void)
253 wattrset(conio_scr,oldattr);
258 if (waddch(conio_scr,c)!=ERR) {
265 void textattr (int attr)
270 void textbackground (int color)
277 void textcolor (int color)
284 void textmode (int mode)
293 getyx(conio_scr,y,x);
302 getyx(conio_scr,y,x);
307 void window (int left,int top,int right,int bottom)
317 if (top==0) nlines++;
318 if (left==0) ncols++;
319 if ((nlines<1) | (ncols<1)) return;
320 conio_scr=newwin(nlines,ncols,top,left);
321 keypad(conio_scr,TRUE);
322 meta(conio_scr,TRUE);
323 idlok(conio_scr,TRUE);
324 scrollok(conio_scr,TRUE);