summaryrefslogtreecommitdiff
path: root/devel/tvision/files/patch-lib::system.cc
blob: 8cc8bd4d5eee4a27f5d9b7155c1c797620520e48 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
--- lib/system.cc.orig	Wed Nov 10 17:08:59 2004
+++ lib/system.cc	Wed Nov 10 17:34:47 2004
@@ -56,6 +56,7 @@
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <string.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
@@ -105,6 +106,8 @@
 
 extern "C"
 {
+#define _XOPEN_SOURCE
+
 #ifdef HAVE_NCURSES_H
 #include <ncurses.h>
 #else
@@ -131,7 +134,7 @@
 
 /*
  * This is the delay in ms between next evMouseAuto events.  Must be greater
- * than DELAY_SIGALRM (see below).
+ * than DELAY_CURSES (see below).
  */
 #define DELAY_AUTOCLICK_NEXT	100
 
@@ -150,10 +153,9 @@
 #define DELAY_ESCAPE		400
 
 /*
- * This is the delay in ms between consecutive SIGALRM signals.  This
- * signal is used to generate evMouseAuto and cmSysWakeup events.
+ * This is the delay in ms used when waiting for keyboard events.
  */
-#define DELAY_SIGALRM		100
+#define DELAY_CURSES		100
 
 /*
  * This broadcast event is used to update the StatusLine.
@@ -591,7 +593,6 @@
 static TPoint msWhere;		/* mouse coordinates */
 static char env[PATH_MAX];	/* value of the TVOPT environment variable */
 static int curX, curY;		/* current cursor coordinates */
-static int currentTime;		/* current timer value */
 static int doRepaint;		/* should redraw the screen ? */
 static int doResize;		/* resize screen ? */
 static int evLength;		/* number of events in the queue */
@@ -604,15 +605,30 @@
 
 class Timer
 {
-	int limit;
+	long limit;
+	static struct timeval offset_tv;
+	long currentTime()
+	{
+		struct timeval tv;
+		if(!offset_tv.tv_sec && !offset_tv.tv_usec)
+		{
+			gettimeofday(&offset_tv, NULL);
+			return 0;
+		}
+		gettimeofday(&tv, NULL);
+		return 1000L * (tv.tv_sec - offset_tv.tv_sec - 1) + (tv.tv_usec - offset_tv.tv_usec + 1000000L) / 1000L;
+	}
+
 public:
 	Timer() { limit = -1; }
-	int isExpired() { return limit != -1 && currentTime >= limit; }
+	int isExpired() { return limit != -1 && currentTime() >= limit; }
 	int isRunning() { return limit != -1; }
-	void start(int timeout) { limit = currentTime + timeout; }
+	void start(int timeout) { limit = currentTime() + timeout; }
 	void stop() { limit = -1; }
 };
 
+struct timeval Timer::offset_tv = {0,0};
+
 static Timer kbEscTimer;	/* time limit to detect Esc-key sequences */
 static Timer msAutoTimer;	/* time when generate next cmMouseAuto */
 static Timer wakeupTimer;	/* time when generate next cmWakeup */
@@ -703,22 +719,20 @@
  * Reads a key from the keyboard.
  */
 #ifdef NCURSES_MOUSE_VERSION
+static void msInit();
+static void msClose();
+static void msSuspend();
+static void msResume();
 static void msHandle();
 #endif
 
 static void kbHandle()
 {
 	int code, type = 0;
-	sigset_t alarmBlock, normalMask;
-
-	sigemptyset(&alarmBlock);
-	sigaddset(&alarmBlock, SIGALRM);
 
 	/* see if there is data available */
 
-	sigprocmask(SIG_BLOCK, &alarmBlock, &normalMask);
 	code = getch();
-	sigprocmask(SIG_SETMASK, &normalMask, NULL);
 
 #ifdef NCURSES_MOUSE_VERSION
 	if (code == KEY_MOUSE)	/* was it a mouse event ? */
@@ -1138,6 +1152,28 @@
  */
 
 #ifdef NCURSES_MOUSE_VERSION
+static mmask_t orig_mousemask = 0;
+
+static void msInit()
+{
+	mmask_t m = mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, &orig_mousemask);
+}
+
+static void msClose()
+{
+	mmask_t m = mousemask(orig_mousemask, NULL);
+}
+
+static void msSuspend()
+{
+	mmask_t m = mousemask(0, NULL);
+}
+
+static void msResume()
+{
+	mmask_t m = mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
+}
+
 static void msHandle()
 {
 	TEvent event;
@@ -1502,13 +1538,14 @@
 
 static void startcurses()
 {
-	initscr();		/* initialize the curses library */
-	keypad(stdscr, TRUE);	/* enable keyboard mapping */
-	cbreak();		/* do not wait for \n */
-	noecho();		/* do not echo input */
-	if (has_colors()) start_color();
-	timeout(0);		/* set getch() in non-blocking mode */
-	refresh();		/* fix for new ncurses packages */
+	::initscr();		/* initialize the curses library */
+	if (::has_colors()) ::start_color();
+	::cbreak();               /* do not wait for \n */
+	::noecho();               /* do not echo input */
+	::nodelay(stdscr, TRUE);	/* set getch() in non-blocking mode */
+	::timeout(DELAY_CURSES);	/* set getch() in non-blocking mode */
+	::keypad(stdscr, TRUE);	/* enable keyboard mapping */
+	::refresh();		/* fix for new ncurses packages */
 	selectPalette();	/* select the more appropiate palette */
 	TScreen::drawCursor(0);	/* hide the cursor */
 	TScreen::drawMouse(1);	/* draw the mouse pointer */
@@ -1736,6 +1773,10 @@
 	TScreen::drawMouse(0);
 #ifdef ENABLE_GPM
 	gpmClose();
+#else
+#ifdef NCURSES_MOUSE_VERSION
+	msClose();
+#endif
 #endif
 	stopcurses();
 #ifdef ENABLE_VCS
@@ -1765,12 +1806,6 @@
 		msFlag++;
 		break;
 #endif
-	case SIGALRM:
-		/*
-		 * called every DELAY_SIGALRM ms
-		 */
-		currentTime += DELAY_SIGALRM;
-		break;
 	case SIGCONT:
 		/*
 		 * called when the user restart the process after a ctrl-z
@@ -1881,7 +1916,7 @@
 	/* internal stuff */
 
 	curX = curY = 0;
-	currentTime = doRepaint = doResize = evLength = 0;
+	doRepaint = doResize = evLength = 0;
 	evIn = evOut = &evQueue[0];
 	kbEscTimer.stop();
 	msAutoTimer.stop();
@@ -1909,6 +1944,10 @@
 	startcurses();		/* curses stuff */
 #ifdef ENABLE_GPM
 	gpmInit();
+#else
+#ifdef NCURSES_MOUSE_VERSION
+	msInit();
+#endif
 #endif
 	/* catch useful signals */
 
@@ -1921,20 +1960,12 @@
 #ifdef ENABLE_FBSDM
 	sigaction(FBSDM_SIGNAL, &dfl_handler, NULL);
 #endif
-	sigaction(SIGALRM, &dfl_handler, NULL);
 	sigaction(SIGCONT, &dfl_handler, NULL);
 	sigaction(SIGINT, &dfl_handler, NULL);
 	sigaction(SIGQUIT, &dfl_handler, NULL);
 	sigaction(SIGTSTP, &dfl_handler, NULL);
 	sigaction(SIGWINCH, &dfl_handler, NULL);
 
-	/* generates a SIGALRM signal every DELAY_SIGALRM ms */
-
-	struct itimerval timer;
-	timer.it_interval.tv_usec = timer.it_value.tv_usec =
-		DELAY_SIGALRM * 1000;
-	timer.it_interval.tv_sec = timer.it_value.tv_sec = 0;
-	setitimer(ITIMER_REAL, &timer, NULL);
 }
 
 /*
@@ -1951,6 +1982,10 @@
 	startcurses();
 #ifdef ENABLE_GPM
 	gpmResume();
+#else
+#ifdef NCURSES_MOUSE_VERSION
+	msResume();
+#endif
 #endif
 	doRepaint++;
 }
@@ -1959,6 +1994,10 @@
 {
 #ifdef ENABLE_GPM
 	gpmSuspend();
+#else
+#ifdef NCURSES_MOUSE_VERSION
+	msSuspend();
+#endif
 #endif
 	stopcurses();
 }
@@ -1986,6 +2025,10 @@
 		 */
 #ifdef ENABLE_GPM
 		gpmSuspend();
+#else
+#ifdef NCURSES_MOUSE_VERSION
+		msSuspend();
+#endif
 #endif
 		clear();	/* blank the screen */
 		refresh();	/* this is necessary */
@@ -1993,6 +2036,10 @@
 		startcurses();
 #ifdef ENABLE_GPM
 		gpmResume();
+#else
+#ifdef NCURSES_MOUSE_VERSION
+		msResume();
+#endif
 #endif
 		doResize = 0;
 		winsize win;
@@ -2056,11 +2103,14 @@
 		}
 #endif
 		/*
-		 * suspend until there is a signal or some data in file
+		 * suspend until there is a timeout or some data in file
 		 * descriptors
 		 */
+		struct timeval tv;
+		tv.tv_sec = 0;
+		tv.tv_usec = 1000L * DELAY_CURSES;
 		if (select(FD_SETSIZE, &fdActualRead, &fdActualWrite,
-			&fdActualExcept, NULL) > 0)
+			&fdActualExcept, &tv) > 0)
 		{
 			kbReady = FD_ISSET(STDIN_FILENO, &fdActualRead);
 #ifdef ENABLE_GPM