summaryrefslogtreecommitdiffstats
path: root/tsutils/tsrecorder.c
blob: 63accac5ad2f2c156edb524f1e3ebe80fb853174 (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
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
#include <fcntl.h>
#include <linux/input.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>

static int eventFd = -1;
static FILE* pfile;
struct input_event ev0[64];
static int screenWidth = 0;
static int screenHeight = 0;

extern char* getTouchScreenInfo();

static int handle_event()
{
	int i, rd;
	char str[256];
	float x = 0, y = 0;

	rd = read(eventFd, ev0, sizeof(struct input_event)* 64);

	if(rd < sizeof(struct input_event))
		return 0;

	for(i=0;i<rd/sizeof(struct input_event); i++)
	{
		memset(str, 0, sizeof(str));

		if(ev0[i].type == 3)
		{
			if(ev0[i].code == 53 || ev0[i].code == 0)
			{//screen widthに対して変換
				x = (float)ev0[i].value / screenWidth;
				sprintf(str, "%2d\t%ld\t%ld\t%3hu\t%3hu\t%f\r\n",
						i, ev0[i].time.tv_sec, ev0[i].time.tv_usec, ev0[i].type, ev0[i].code, x);
			}
			else if(ev0[i].code == 54 || ev0[i].code == 1)
			{//screen heightに対して変換
				y = (float)ev0[i].value / screenHeight;
				sprintf(str, "%2d\t%ld\t%ld\t%3hu\t%3hu\t%f\r\n",
						i, ev0[i].time.tv_sec, ev0[i].time.tv_usec, ev0[i].type, ev0[i].code, y);
			}
			else
			{
				sprintf(str, "%2d\t%ld\t%ld\t%3hu\t%3hu\t%d\r\n", i, ev0[i].time.tv_sec, ev0[i].time.tv_usec, ev0[i].type, ev0[i].code, ev0[i].value);
			}
		}
		else
		{
			sprintf(str, "%2d\t%ld\t%ld\t%3hu\t%3hu\t%d\r\n", i, ev0[i].time.tv_sec, ev0[i].time.tv_usec, ev0[i].type, ev0[i].code, ev0[i].value);
		}

		fputs(str, pfile);
	}
	fflush(pfile);
	return 1;
}

void printUsage(char* myName)
{
	printf("usage:\n \
	%s -f <string>\n \
	-f : file name for recording event\n", myName);
}

int main(int argc,char *argv[])
{
	int done = 1;
	int index = 0;
	char* fName = NULL;
	char eventNode[50];
	const char* tsInfo;

	if (argc < 3)
	{
		printUsage(argv[0]);
		return -1;
	}

	for (index = 1; index < argc; index++)
	{
		if (strcmp(argv[index], "-f") == 0)
		{
			fName = argv[++index];
		}
		else
		{
			printUsage(argv[0]);
			return -1;
		}
	}

	tsInfo = getTouchScreenInfo();
	if (NULL == tsInfo) {
		printf("Failed to get touch screen info.\n");
		return -1;
	}

	memset(eventNode, 0, sizeof(eventNode));
	sscanf(tsInfo, "%s %d %d", eventNode, &screenWidth, &screenHeight);

	eventFd = open(eventNode, 02);
	if(eventFd <0) {
		printf("open input device error\n");
		return -1;
	}

	pfile = fopen(fName, "wb+");
	if(pfile == NULL)
	{
		printf("Failed to create file!\n");
		return -1;
	}

	while (done)
	{
		printf("begin %s...\n", eventNode);
		done = handle_event();
		printf("end %s...\n", eventNode);
	}

	fclose(pfile);

	if(eventFd > 0)
	{
		close(eventFd);
		eventFd = -1;
	}

	return 0;
}