File: input-recv.c

package info (click to toggle)
input-utils 1.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 380 kB
  • ctags: 1,091
  • sloc: ansic: 1,900; sh: 25; makefile: 6
file content (87 lines) | stat: -rw-r--r-- 1,617 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include "input.h"
#include "tcp.h"

/* ------------------------------------------------------------------ */

static int usage(char *prog, int error)
{
	fprintf(error ? stderr : stdout,
		"usage: %s"
		"\n",
		prog);
	exit(error);
}

int main(int argc, char *argv[])
{
	char *addr   = NULL;
	char *port   = NULL;
	char *host   = "localhost";
	char *serv   = "1234";
	struct addrinfo ask;
	int c,sock;

	memset(&ask,0,sizeof(ask));
	ask.ai_family = PF_UNSPEC;
	ask.ai_socktype = SOCK_STREAM;

	for (;;) {
		if (-1 == (c = getopt(argc, argv, "h")))
			break;
		switch (c) {
		case 'h':
			usage(argv[0],0);
		default:
			usage(argv[0],1);
		}
	}

	tcp_verbose = 1;
	sock = tcp_connect(&ask,addr,port,host,serv);
	if (-1 == sock)
		exit(1);

	for (;;) {
		struct input_event ev;
		fd_set set;
		int rc;

		FD_ZERO(&set);
		FD_SET(sock,&set);
		rc = select(sock+1,&set,NULL,NULL,NULL);
		if (1 != rc) {
			perror("select");
			exit(1);
		}
		
		rc = read(sock,&ev,sizeof(ev));
		if (rc != sizeof(ev)) {
			printf("read ret=%d (expected %d), errno=%s\n",
				rc,(int)sizeof(ev),strerror(errno));
			exit(1);
		}
		
		/* convert from network byte order ... */
		ev.time.tv_sec  = ntohl(ev.time.tv_sec);
		ev.time.tv_usec = ntohl(ev.time.tv_usec);
		ev.type         = ntohs(ev.type);
		ev.code         = ntohs(ev.code);
		ev.value        = ntohl(ev.value);

		print_event(&ev);
	}
		
	return 0;
}

/* ---------------------------------------------------------------------
 * Local variables:
 * c-basic-offset: 8
 * End:
 */