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)));
62 /* Call this before any call to linux conio - except the port functions ! */
63 void __attribute__((constructor)) initconio (void) /* This is needed, because ncurses needs to be initialized */
67 if (atexit(doneconio)){
68 fprintf(stderr,"Unable to register doneconio(), exiting...\n");
73 // attr_get(&oldattr,&pair,NULL);
76 if (!has_colors() & (color_warning>-1))
77 fprintf(stderr,"Attention: A color terminal may be required to run this application !\n");
79 conio_scr=newwin(0,0,0,0);
80 keypad(conio_scr,TRUE);
82 idlok(conio_scr,TRUE);
83 scrollok(conio_scr,TRUE);
84 /* Color initialization */
87 init_pair((8*y)+x+1, colortab(x), colortab(y));
88 wattr_get(conio_scr,&txtattr,&pair,NULL);
94 /* Call this on exiting your program */
100 /* Here it starts... */
101 void _setcursortype (int cur_t)
107 char *cgets (char *str) /* ugly function :-( */
112 strncpy(strng,str,1);
113 wgetnstr(conio_scr,&strng[2],(int) strng[0]);
114 while (strng[i]!=0) i++;
117 memcpy(str,strng,i+3);
124 wclrtoeol(conio_scr);
131 wmove(conio_scr,0,0);
135 int cprintf (char *format, ... )
138 char buffer[BUFSIZ]; /* Well, BUFSIZ is from ncurses... */
140 va_start(argp,format);
141 vsprintf(buffer,format,argp);
143 i=waddstr(conio_scr,buffer);
148 void cputs (char *str)
150 waddstr(conio_scr,str);
154 int cscanf (const char *format, ...)
157 char buffer[BUFSIZ]; /* See cprintf */
160 if (wgetstr(conio_scr,buffer)==ERR) return(-1);
161 va_start(argp,format);
162 i=vsscanf(buffer,format,argp);
170 wdeleteln(conio_scr);
184 void gettextinfo(struct text_info *inforec)
188 unsigned char x1,x2,y1,y2;
189 attr_t dattr,dnattr,a; /* The "d" stands for DOS */
190 getyx(conio_scr,yp,xp);
191 getbegyx(conio_scr,y1,x1);
192 getmaxyx(conio_scr,y2,x2);
194 wattr_get(conio_scr,&a,&pair,NULL);
195 if (a==(a & A_BLINK)) dattr=dattr+128;
196 dnattr=oldattr; /* Well, this cannot be right,
197 because we don't know the COLORPAIR values from before initconio() !*/
198 inforec->winleft=x1+1;
199 inforec->wintop=y1+1;
202 inforec->winright=x1+x2+1;
203 inforec->winbottom=y1+y2+1;
206 inforec->screenheight=y2+1;
207 inforec->screenwidth=x2+1;
208 inforec->currmode=3; /* This is C80 */
209 inforec->normattr=dnattr; /* Don't use this ! */
210 inforec->attribute=dattr;
213 void gotoxy (int x, int y)
217 wmove(conio_scr,y,x);
221 void highvideo (void)
223 textcolor(15); /* White */
224 textbackground(0); /* Black */
229 winsertln(conio_scr);
236 nodelay(conio_scr,TRUE);
238 nodelay(conio_scr,FALSE);
245 textbackground(0); /* Black */
246 textcolor(8); /* Should be darkgray */
249 void normvideo (void)
251 wattrset(conio_scr,oldattr);
256 if (waddch(conio_scr,c)!=ERR) {
263 void textattr (int attr)
268 void textbackground (int color)
275 void textcolor (int color)
282 void textmode (int mode)
291 getyx(conio_scr,y,x);
300 getyx(conio_scr,y,x);
305 void window (int left,int top,int right,int bottom)
315 if (top==0) nlines++;
316 if (left==0) ncols++;
317 if ((nlines<1) | (ncols<1)) return;
318 conio_scr=newwin(nlines,ncols,top,left);
319 keypad(conio_scr,TRUE);
320 meta(conio_scr,TRUE);
321 idlok(conio_scr,TRUE);
322 scrollok(conio_scr,TRUE);