1 /*
2  *	@(#) system.c -- lowlevel system functions for TROJKA
3  *	created:	4.iii.1992 for XENIX/68000
4  *	modified: 	16.iv.1992 Maarten added SunOS + HPUX support
5  *			20.x.1995  Maarten added Linux support
6  */
7 
8 #ifdef HPUX
9 #include <codelibs/nap.h>
10 #endif
11 
12 #ifdef LINUX
13 #include <termios.h>
14 #endif
15 
16 #ifdef XENIX68
17 #include <sys/types.h>
18 #include <sys/timeb.h>
19 #endif
20 #include <signal.h>
21 #include <curses.h>
22 
23 #include <stdio.h>
24 #include <time.h>
25 #include <fcntl.h>
26 #ifdef __FreeBSD__
27 #include <sys/ioctl.h>
28 #endif
29 
30 #include "trojka.h"
31 
32 extern flag curses_installed;
33 
34 int 	killkeys(),
35 	quit_prog();
36 
37 char	getkey();
38 
39 int initcurses();
40 int catch();
41 
42 #if XENIX68
43 struct timeb *tb;		/* for delay */
44 #endif
45 
getkey()46 char getkey()			/* get key from keyboard	*/
47 {
48 #if SUNOS | HPUX | LINUX | __FreeBSD__
49 	long count;
50 	char ch;
51 
52 	ioctl(0,FIONREAD,&count);	/* check if there's something to read */
53 	if(count > 0)
54 		return(ch = getchar());
55 	return(0);
56 #endif
57 #if XENIX68
58 	static char buf[1];
59 
60 	if(rdchk(0)>0) {
61 		read(0,buf,1);
62 		return(buf[0]);
63 	}
64 #endif
65 }
66 
67 
68 
killkeys()69 int killkeys()				/* flush keyboard buffer	*/
70 {
71 	fflush(stdin);
72 }
73 
74 
initcurses()75 int initcurses()
76 {
77 #ifdef SUNOS
78 	struct sgttyb terminfo;
79 
80 	ioctl(0,TIOCGETP,&terminfo);
81 #endif
82 	initscr();
83 	crmode();
84 	nonl();
85 	noecho();
86 	signal(SIGTERM, catch);
87 	signal(SIGINT, catch);
88 	curses_installed = TRUE;
89 
90 #ifdef SUNOS
91 	if (LINES < 24 || COLS < 80)
92 		quit_prog("Sorry. The screensize must be at least 80 x 24");
93 #endif
94 }
95 
96 
catch(sig)97 int catch(sig)
98 int sig;
99 {
100 	signal(SIGTERM, SIG_IGN);
101 	signal(SIGINT,  SIG_IGN);
102 
103 	quit_prog("See you around in the Trojka-zone...");
104 }
105 
quit_prog(s)106 int quit_prog(s)
107 char *s;
108 {
109 	if(curses_installed) {
110 		clear();
111 		refresh();
112 		echo();
113 		nl();
114 		nocrmode();
115 		endwin();
116 	}
117 	fprintf(stderr,"%s\n",s);
118 	exit(0);
119 }
120 
121 
delay(millisecs)122 int delay(millisecs)
123 int millisecs;
124 {
125 #if SUNOS | LINUX | __FreeBSD__
126 	usleep(millisecs * 1200);	/* usleep does microsecs; need millisecs */
127 #endif
128 #ifdef HPUX
129 	nap(millisecs);
130 #endif
131 #if XENIX68
132 /* believe me, this works */
133 	long goal;
134 
135 	ftime(tb);
136  	goal = (long)(tb->time*(long)1000)+(tb->millitm/40)+(long)(millisec/50);
137 		 while((long)(tb->time*(long)1000)+(tb->millitm/40)<goal)
138 			ftime(tb);
139 #endif
140 }
141 
142