-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslowputc.h
55 lines (41 loc) · 1 KB
/
slowputc.h
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
#include <sys/time.h>
#include <unistd.h>
/*
* Time as a floating point double, in seconds.
*/
static double
slowdown_dtime()
{
struct timeval t;
gettimeofday(&t, NULL);
return (double) t.tv_sec + (double) t.tv_usec * 1e-6;
}
INT slowspeed = -1; // Speed in baud, with 10 bits per byte, negative off
/*
* Potentially wait and flush to simulate output speed.
*/
static void
slowdown(INT c)
{
static INT count=0;
static double timepoint = 0.0;
INT buf;
double bufwait, now, delayperchar;
buf = slowspeed/1000; // 1000 = 10 bits per byte * 100 Hz
if (buf < 1) buf = 1;
if (++count >= buf && ((c & 0xc0) != 0x80 || !termisutf8)) {
delayperchar = 10.0/(double) slowspeed;
timepoint = timepoint + count*delayperchar;
now = slowdown_dtime();
bufwait = timepoint - now;
if (bufwait < -0.2) {
/* There is an external wait of more than */
/* 200 msec, reset */
timepoint = now;
} else if (bufwait > 0.0) {
usleep((useconds_t)(1.0e6*bufwait));
}
ttflush();
count = 0;
}
}