File: console.c

package info (click to toggle)
udpcast 20120424-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, stretch, trixie
  • size: 876 kB
  • ctags: 1,011
  • sloc: ansic: 7,713; sh: 2,838; perl: 227; makefile: 114
file content (233 lines) | stat: -rw-r--r-- 5,370 bytes parent folder | download | duplicates (2)
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
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#include "console.h"
#include "util.h"
#include "udpcast.h"

#ifndef __MINGW32__

#include <termios.h>

struct console_t {
    int fd; /* Filedescriptor for console, or -1 if disabled */
    struct termios oldtio; /* old settings, for restore */
    int needClose; /* Does file descriptor need to be closed on reset? */
    int needRestore; /* Is the file descriptor indeed a terminal, and needs
		      * to be restored? */
};

console_t *prepareConsole(int fd) {
    struct termios newtio;
    int needClose=0;
    console_t *c;

    if(fd < 0) {
	fd = open("/dev/tty", O_RDWR);
	if(fd < 0) {
	    fprintf(stderr, "Could not open keyboard: %s\n", strerror(errno));
	    return NULL;
	}
	needClose=1;
    }

    c = MALLOC(console_t);
    if(c == NULL)
	return c;

    c->fd = fd;
    c->needClose = needClose;
    c->needRestore = 0;

    if(tcgetattr(c->fd, &c->oldtio) >= 0) {
	newtio = c->oldtio;
	newtio.c_lflag &= ~ECHO;
	newtio.c_lflag &= ~ICANON;
	newtio.c_cc[VMIN] = 1;
	newtio.c_cc[VTIME] = 0;
	if(tcsetattr(c->fd, TCSAFLUSH, &newtio) < 0)
	    perror("Set terminal to raw");
	else
	    c->needRestore = 1;
    }

    return c;
}

int selectWithConsole(console_t *con, int maxFd, 
		      fd_set *read_set, struct timeval *tv,
		      int *keyPressed) {
    int ret;

    if(con) {
	int fd = con->fd;
	FD_SET(fd, read_set);
	if(fd >= maxFd)
	    maxFd = fd+1;
    }
    ret = select(maxFd, read_set, NULL, NULL, tv);
    if(ret < 0)
	return -1;
    if(con && FD_ISSET(con->fd, read_set)) {
	*keyPressed = 1;
    }
    return ret;
}

void restoreConsole(console_t **cp, int doConsume) {
    console_t *c=*cp;
    int ch='\0';
    int r UNUSED;

    if(c == NULL)
      return;

    /* If key pressed, consume it. If letter is q, quit */
    if(doConsume) {
      r = read(c->fd, &ch, 1);
    }

    if(c->needRestore && tcsetattr(c->fd, TCSAFLUSH, &c->oldtio) < 0) {
	perror("Restore terminal settings");
    }
    *cp = NULL;
    if(c->needClose)
	close(c->fd);
    free(c);
    if(ch == 'q')
	exit(1);
}

#else /* __MINGW32__ */

#include "log.h"

struct console_t {
    HANDLE thread[2]; /* 0: console, 1: select */
    int fd;
    int maxFd;
    fd_set read_set;
    struct timeval tv;
    struct timeval *tvp;
    int select_return;
    int keyPressed;
    char ch;
};

static DWORD WINAPI waitForKeyPress(LPVOID lpParameter) {
    console_t *con = lpParameter;
    int n=read(con->fd, &con->ch, 1);
    if(n == 1)
	con->keyPressed=1;
    return 0;
}

static DWORD WINAPI waitForSelect(LPVOID lpParameter) {
    console_t *con = lpParameter;
    con->select_return = select(con->maxFd, &con->read_set, 
				NULL, NULL, con->tvp);
    return 0;
}


console_t *prepareConsole(int fd) { 
    console_t *con;
    if(fd < 0) {
	fd = open("CON", O_RDONLY);
	if(fd < 0)
	    return NULL;
    } else {
	fd = dup(fd);  /* dup console filedescriptor in order to avoid 
			*  race with pipe spawner... */      
    }    
    con = MALLOC(console_t);
    if(con == NULL)
	return con;
    con->thread[0] = con->thread[1] = NULL;
    con->keyPressed=0;
    con->fd = fd;
    return con;
}

static HANDLE startThread(console_t *con,
			  LPTHREAD_START_ROUTINE lpStartAddress) {
    /* Start thread ... 
     * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createthread.asp
     */	       
    return CreateThread(NULL,	/* lpThreadAttributes */
			0,	/* dwStackSize */
			lpStartAddress,
			con,	/* lpParameter */
			0,	/* dwCreationFlags */
			NULL    /* lpThreadId */);
}

int selectWithConsole(console_t *con, int maxFd, 
		      fd_set *read_set, struct timeval *tv,
		      int *keyPressed) {
    int r;
    if(con == NULL)
	return select(maxFd, read_set, NULL, NULL, tv);
    if(!con->thread[0]) {
	con->thread[0]=startThread(con, waitForKeyPress);
	if(!con->thread[0])
	    udpc_fatal(1, "Could not start console listen thread");
    }

    if(con->thread[1])
	udpc_fatal(1, "Two select threads started at once!");

    con->maxFd = maxFd;
    memcpy(&con->read_set, read_set, sizeof(*read_set));
    if(tv) {
	memcpy(&con->tv, tv, sizeof(*tv));
	con->tvp = &con->tv;
    } else {
	con->tvp=NULL;
    }

    /* Start select thread */
    con->thread[1]=startThread(con, waitForSelect);
    if(!con->thread[1])
	udpc_fatal(1, "Could not start select thread");

    /* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/waitformultipleobjects.asp
     */    
    switch( (r=WaitForMultipleObjects(2, con->thread, FALSE, INFINITE)) ) {
    case WAIT_OBJECT_0:
	*keyPressed=1;
	CloseHandle(con->thread[0]);
	CloseHandle(con->thread[1]);
	FD_ZERO(read_set);
	return 1;
    case WAIT_OBJECT_0+1:
	*keyPressed=0;
	CloseHandle(con->thread[1]);
	con->thread[1]=NULL;
	memcpy(read_set, &con->read_set, sizeof(*read_set));
	return con->select_return;
    default:
	udpc_fatal(1, "Unexpected result %d for waitForMultipleObjects", r);
	return -1;
    }
}

void restoreConsole(console_t **cp, int doConsume) { 
    console_t *c=*cp;

    /* If key pressed, consume it. If letter is q, quit */
    if(doConsume) {
	if (c->keyPressed && c->ch == 'q') {
	    exit(1);
	}
    }

    /* We do not free the console, because the select thread might
     * still be active... */
    *cp = NULL;
}

#endif /* __MINGW32__ */