Remove CRs.
[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 initconio (void)
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 /* clear to end of line -- can be a no-op in tty mode */
124 {
125 #ifdef SERGEEV
126    wclrtoeol(conio_scr);
127    wrefresh(conio_scr);
128 #endif /* SERGEEV */
129 }
130
131 void clrscr (void)
132 /* clear screen -- can be a no-op in tty mode */
133 {
134 #ifdef SERGEEV
135    wclear(conio_scr);
136    wmove(conio_scr,0,0);
137    wrefresh(conio_scr);
138 #endif /* SERGEEV */
139 }
140
141 int cprintf (char *format, ... )
142 {
143    int i;
144    char buffer[BUFSIZ]; /* Well, BUFSIZ is from ncurses...  */
145    va_list argp;
146    va_start(argp,format);
147    vsprintf(buffer,format,argp);
148    va_end(argp);
149
150 #ifdef SERGEEV
151    i=waddstr(conio_scr,buffer);
152    wrefresh(conio_scr);
153 #else
154    i=printf(buffer);
155 #endif /* SERGEEV */
156    return(i);
157 }
158
159 void cputs (char *str)
160 {
161    waddstr(conio_scr,str);
162    wrefresh(conio_scr);
163 }
164
165 int cscanf (const char *format, ...)
166 {
167    int i;
168    char buffer[BUFSIZ]; /* See cprintf */
169    va_list argp;
170    echo();
171    if (wgetstr(conio_scr,buffer)==ERR) return(-1);                    
172    va_start(argp,format);
173    i=vsscanf(buffer,format,argp);                         
174    va_end(argp);
175    noecho();
176    return(i);
177 }
178
179 void delline (void)
180 {
181    wdeleteln(conio_scr);
182    wrefresh(conio_scr);
183 }
184
185 int getche (void)
186 {
187    int i;
188    echo();
189    i=wgetch(conio_scr);
190    if (i==-1) i=0;
191    noecho();
192    return(i);
193 }
194
195 void gettextinfo(struct text_info *inforec)
196 {
197    short pair;
198    unsigned char xp,yp;
199    unsigned char x1,x2,y1,y2;
200    attr_t dattr,dnattr,a; /* The "d" stands for DOS */
201    getyx(conio_scr,yp,xp);
202    getbegyx(conio_scr,y1,x1);
203    getmaxyx(conio_scr,y2,x2);
204    dattr=(bgc*16)+fgc;
205    wattr_get(conio_scr,&a,&pair,NULL);
206    if (a==(a & A_BLINK)) dattr=dattr+128;
207    dnattr=oldattr;  /* Well, this cannot be right, 
208                        because we don't know the COLORPAIR values from before initconio() !*/
209    inforec->winleft=x1+1;
210    inforec->wintop=y1+1;
211    if (x1==0) x2--;
212    if (y1==0) y2--;
213    inforec->winright=x1+x2+1;
214    inforec->winbottom=y1+y2+1;
215    inforec->curx=xp+1;
216    inforec->cury=yp+1;
217    inforec->screenheight=y2+1;
218    inforec->screenwidth=x2+1;
219    inforec->currmode=3; /* This is C80 */
220    inforec->normattr=dnattr; /* Don't use this ! */
221    inforec->attribute=dattr;
222
223
224 void gotoxy (int x, int y)
225 /* address cursor -- OK for this to be a no-op in TTY mode */
226 {
227 #ifdef SERGEEV
228    y--;
229    x--;
230    wmove(conio_scr,y,x);
231    wrefresh(conio_scr);
232 #endif /* SERGEEV */
233 }
234
235 void highvideo (void)
236 {
237 #ifdef SERGEEV
238    textcolor(15); /* White */
239    textbackground(0); /* Black */
240 #endif /* SERGEEV */
241 }
242
243 void insline (void)
244
245    winsertln(conio_scr);
246    wrefresh(conio_scr);
247 }
248
249 int kbhit (void)
250 {
251    int i;
252    nodelay(conio_scr,TRUE);
253    i=wgetch(conio_scr);
254    nodelay(conio_scr,FALSE);
255    if (i==-1) i=0;
256    return(i);
257 }
258
259 void lowvideo (void)
260 {
261    textbackground(0); /* Black */
262    textcolor(8); /* Should be darkgray */
263 }
264
265 void normvideo (void)
266 {
267    wattrset(conio_scr,oldattr);
268 }
269
270 int putch (int c)
271 {
272    if (waddch(conio_scr,c)!=ERR) {
273       wrefresh(conio_scr); 
274       return(c);
275    }
276    return(0);
277 }
278                                      
279 void textattr (int attr)
280 {
281    docolor(attr);
282 }
283
284 void textbackground (int color)
285 {
286    bgc=color;
287    color=(bgc*16)+fgc;
288    docolor(color);
289 }
290
291
292 void textcolor (int color)
293 {
294 #ifdef SERGEEV
295    fgc=color;
296    color=(bgc*16)+fgc;
297    docolor(color);
298 #endif /* SERGEEV */
299 }
300  
301 void textmode (int mode)
302 {
303    /* Ignored */
304 }
305
306 int wherex (void)
307 {
308    int y;
309    int x;
310    getyx(conio_scr,y,x);
311    x++;
312    return(x);
313 }
314
315 int wherey (void)
316 {
317    int y;
318    int x;
319    getyx(conio_scr,y,x);
320    y++;
321    return(y);
322 }
323
324 void window (int left,int top,int right,int bottom)
325 {
326    int nlines,ncols;
327    delwin(conio_scr);
328    top--;
329    left--;
330    right--;
331    bottom--;
332    nlines=bottom-top;
333    ncols=right-left;
334    if (top==0) nlines++;
335    if (left==0) ncols++;
336    if ((nlines<1) | (ncols<1)) return;
337    conio_scr=newwin(nlines,ncols,top,left);   
338    keypad(conio_scr,TRUE);
339    meta(conio_scr,TRUE);
340    idlok(conio_scr,TRUE);
341    scrollok(conio_scr,TRUE);
342    wrefresh(conio_scr);
343 }
344
345 /* Linux is cool */