First round of changes from Stas Sergeev.
[super-star-trek.git] / conio.c
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     *
4  * I/O-functions.                                                           *
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 *
9  * well !                                                                   *
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  *
13  * mega-bug !!!                                                             *
14  * Watch out for other MXP releases, too !                                  *
15  * Send bugreports to: fractor@germanymail.com                              *
16  ****************************************************************************/
17
18 #define _ISOC99_SOURCE
19 #include <stdio.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/io.h>
25 #include <ncurses.h>
26 #include "conio.h" 
27
28 static attr_t txtattr,oldattr;
29 static int fgc,bgc;
30 char color_warning=1;
31 int directvideo;
32 WINDOW *conio_scr;
33
34 /* Some internals... */
35 static int colortab(int a) /* convert LINUX Color code to DOS-standard */
36 {
37    switch(a) {
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;
46    }
47    return COLOR_BLACK;
48
49
50 static void docolor (int color) /* Set DOS-like text mode colors */
51 {
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))); 
60 }
61
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 */
64 {
65    int x,y;
66    short pair;
67    if (atexit(doneconio)){
68       fprintf(stderr,"Unable to register doneconio(), exiting...\n");
69       exit(1);
70    }
71    initscr();
72    start_color();
73 //   attr_get(&oldattr,&pair,NULL);
74    nonl();
75    raw();
76    if (!has_colors() & (color_warning>-1))
77       fprintf(stderr,"Attention: A color terminal may be required to run this application !\n");   
78    noecho();
79    conio_scr=newwin(0,0,0,0);
80    keypad(conio_scr,TRUE);
81    meta(conio_scr,TRUE);
82    idlok(conio_scr,TRUE);
83    scrollok(conio_scr,TRUE);
84    /* Color initialization */
85    for (y=0;y<=7;y++)
86       for (x=0;x<=7;x++)
87          init_pair((8*y)+x+1, colortab(x), colortab(y));              
88    wattr_get(conio_scr,&txtattr,&pair,NULL);
89    bgc=0;
90    textcolor(7);
91    textbackground(0);
92 }
93
94 /* Call this on exiting your program */
95 void doneconio (void)
96 {
97    endwin();
98 }
99
100 /* Here it starts... */
101 void _setcursortype (int cur_t)
102 {
103    curs_set(cur_t);
104    wrefresh(conio_scr);
105 }
106
107 char *cgets (char *str) /* ugly function :-( */
108 {
109    char strng[257];
110    unsigned char i=2;
111    echo();
112    strncpy(strng,str,1);
113    wgetnstr(conio_scr,&strng[2],(int) strng[0]);
114    while (strng[i]!=0) i++;
115    i=i-2;
116    strng[1]=i;
117    memcpy(str,strng,i+3);
118    noecho();
119    return(&str[2]);
120 }
121
122 void clreol (void)
123 {
124    wclrtoeol(conio_scr);
125    wrefresh(conio_scr);
126 }
127
128 void clrscr (void)
129 {
130    wclear(conio_scr);
131    wmove(conio_scr,0,0);
132    wrefresh(conio_scr);
133 }
134
135 int cprintf (char *format, ... )
136 {
137    int i;
138    char buffer[BUFSIZ]; /* Well, BUFSIZ is from ncurses...  */
139    va_list argp;
140    va_start(argp,format);
141    vsprintf(buffer,format,argp);
142    va_end(argp);
143    i=waddstr(conio_scr,buffer);
144    wrefresh(conio_scr);
145    return(i);
146 }
147
148 void cputs (char *str)
149 {
150    waddstr(conio_scr,str);
151    wrefresh(conio_scr);
152 }
153
154 int cscanf (const char *format, ...)
155 {
156    int i;
157    char buffer[BUFSIZ]; /* See cprintf */
158    va_list argp;
159    echo();
160    if (wgetstr(conio_scr,buffer)==ERR) return(-1);                    
161    va_start(argp,format);
162    i=vsscanf(buffer,format,argp);                         
163    va_end(argp);
164    noecho();
165    return(i);
166 }
167
168 void delline (void)
169 {
170    wdeleteln(conio_scr);
171    wrefresh(conio_scr);
172 }
173
174 int getche (void)
175 {
176    int i;
177    echo();
178    i=wgetch(conio_scr);
179    if (i==-1) i=0;
180    noecho();
181    return(i);
182 }
183
184 void gettextinfo(struct text_info *inforec)
185 {
186    short pair;
187    unsigned char xp,yp;
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);
193    dattr=(bgc*16)+fgc;
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;
200    if (x1==0) x2--;
201    if (y1==0) y2--;
202    inforec->winright=x1+x2+1;
203    inforec->winbottom=y1+y2+1;
204    inforec->curx=xp+1;
205    inforec->cury=yp+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;
211
212
213 void gotoxy (int x, int y)
214 {
215    y--;
216    x--;
217    wmove(conio_scr,y,x);
218    wrefresh(conio_scr);
219 }
220
221 void highvideo (void)
222 {
223    textcolor(15); /* White */
224    textbackground(0); /* Black */
225 }
226
227 void insline (void)
228
229    winsertln(conio_scr);
230    wrefresh(conio_scr);
231 }
232
233 int kbhit (void)
234 {
235    int i;
236    nodelay(conio_scr,TRUE);
237    i=wgetch(conio_scr);
238    nodelay(conio_scr,FALSE);
239    if (i==-1) i=0;
240    return(i);
241 }
242
243 void lowvideo (void)
244 {
245    textbackground(0); /* Black */
246    textcolor(8); /* Should be darkgray */
247 }
248
249 void normvideo (void)
250 {
251    wattrset(conio_scr,oldattr);
252 }
253
254 int putch (int c)
255 {
256    if (waddch(conio_scr,c)!=ERR) {
257       wrefresh(conio_scr); 
258       return(c);
259    }
260    return(0);
261 }
262                                      
263 void textattr (int attr)
264 {
265    docolor(attr);
266 }
267
268 void textbackground (int color)
269 {
270    bgc=color;
271    color=(bgc*16)+fgc;
272    docolor(color);
273 }
274
275 void textcolor (int color)
276 {
277    fgc=color;
278    color=(bgc*16)+fgc;
279    docolor(color);
280 }
281  
282 void textmode (int mode)
283 {
284    /* Ignored */
285 }
286
287 int wherex (void)
288 {
289    int y;
290    int x;
291    getyx(conio_scr,y,x);
292    x++;
293    return(x);
294 }
295
296 int wherey (void)
297 {
298    int y;
299    int x;
300    getyx(conio_scr,y,x);
301    y++;
302    return(y);
303 }
304
305 void window (int left,int top,int right,int bottom)
306 {
307    int nlines,ncols;
308    delwin(conio_scr);
309    top--;
310    left--;
311    right--;
312    bottom--;
313    nlines=bottom-top;
314    ncols=right-left;
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);
323    wrefresh(conio_scr);
324 }
325
326 /* Linux is cool */