aboutsummaryrefslogtreecommitdiffstats
path: root/sdk/dev/inkey.c
blob: 10039058d5cf06c15eee2e51705dbcd5d537dc92 (plain)
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
/*
 * Wait and read single byte input from device.
 * $Id$
 * Copyright (c) 1997 by Tycho Softworks.
 * For conditions of distribution and reuse see product license.
 */
 
#include <dev/tty.h>
#include <std/select.h>
#include <std/poll.h>

#ifndef	SYS_POLL_H_MISSING
#define	USE_POLL
#endif

#ifndef	POLL_H_MISSING
#define	USE_POLL
#endif

#ifdef	USE_POLL

int	inkey(fd_t fd, int timeout)
{
	struct	pollfd	poller;
	uchar	buf;
	
	poller.fd = fd;
	poller.revents = 0;
	poller.events = POLLIN ;
	
	if(!poll(&poller, 1, timeout))
		return -1;
		
	if(poller.revents & (POLLERR | POLLHUP))
		return -1;
		
	if(read(fd, &buf, 1) != 1)
		return -1;
		
	return buf;
}
				
#else

int	inkey(fd_t fd, int timeout)
{
	fd_set	inp,out,exc;
	struct	timeval timer;
	uchar	buf;
	
	FD_ZERO(&inp);
	FD_ZERO(&out);
	FD_ZERO(&exc);
	
	FD_SET(fd, &inp);
	FD_SET(fd, &exc);
	
	timer.tv_sec = timeout / 1000;
	timer.tv_usec = timeout % 1000;
	
	if(!select(fd + 1, &inp, &out, &exc, &timer))
		return -1;
		
	if(FD_ISSET(fd, &exc))
			return -1;
			
	if(FD_ISSET(fd, &inp))
	{
		if(read(fd, &buf, 1) != 1)
			return -1;
			return buf;
	}
	return -1;
}

#endif