aboutsummaryrefslogtreecommitdiffstats
path: root/src/js_raw.c
blob: 9cdc168558b866899eddd0a7ea2ce2101b145f8c (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
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
/*
 * Copyright (c) 2017-2019 TOYOTA MOTOR CORPORATION
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <net/if.h>
#include <string.h>
#include <sys/ioctl.h>
#include <alloca.h>

#include <linux/input.h>
#include <linux/joystick.h>

#include "wheel-service.h"
#include "js_raw.h"
#include "js_signal_event.h"

#define JSNAMELEN 128

static int *axis;
static char *button;
static timer_t tid;
static struct sigaction oldact;

int js_signal_read(int fd)
{
	ssize_t size;
	struct js_event jsEvent;
	int rc = 0;

	size = read(fd, &jsEvent, sizeof(struct js_event));
	if(size != 0)
	{
		switch (jsEvent.type) //& ~JS_EVENT_INIT don't deal with init state.
		{
			case JS_EVENT_BUTTON | JS_EVENT_INIT:
				DBG_NOTICE("This is JS_EVENT_BUTTON init state [%d] : %d.", jsEvent.number, button[jsEvent.number]);
				break;
			case JS_EVENT_AXIS | JS_EVENT_INIT:
				DBG_NOTICE("This is JS_EVENT_AXIS init state [%d] : %d.", jsEvent.number, axis[jsEvent.number]);
				break;
			case JS_EVENT_BUTTON:
				button[jsEvent.number] = (char)jsEvent.value;
				DBG_NOTICE("JS_EVENT_BUTTON [%d] : %d.", jsEvent.number, button[jsEvent.number]);
				newButtonValue(jsEvent.number, jsEvent.value);
				break;
			case JS_EVENT_AXIS:
				axis[jsEvent.number] = jsEvent.value;
				DBG_NOTICE("JS_EVENT_AXIS [%d] : %d.", jsEvent.number, axis[jsEvent.number]);
				newAxisValue(jsEvent.number, jsEvent.value);
				break;
			default:
				break;
		}

		rc = 0;
	}
	else
	{
		rc = -1;
	}

	return rc;
}

int on_event(sd_event_source *s, int fd, uint32_t revents, void *userdata)
{
	if ((revents & EPOLLIN) != 0)
	{
		//NOTICEMSG("on_event!\n");
		int rc = 0;

		rc = js_signal_read(fd);
		if(rc == -1)
		{
			DBG_ERROR("JS Frame Read failed");

			return -1;
		}
	}
	if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0)
	{
		/* T.B.D
		 * if error or hungup */
		DBG_ERROR("Error or Hunup: rvent=%08x", revents);
	}

	return 0;
}

int js_open(const char *devname)
{
	unsigned char numAxes = 0;
	unsigned char numButtons = 0;
	int version = 0;
	int fd;
	char name[JSNAMELEN] = "Unknown";
	char steering_name[JSNAMELEN] = "Logitech G29 Driving Force Racing Wheel";

	struct js_corr cal[6];
	int i, j;
	unsigned int calData[36] =
	{
		1, 0, 8191, 8192, 65542, 65534,
		1, 0, 127, 128, 4227201, 4194176,
		1, 0, 127, 128, 4227201, 4194176,
		1, 0, 127, 128, 4227201, 4194176,
		1, 0, 0, 0, 536854528, 536854528,
		1, 0, 0, 0, 536854528, 536854528
	};

	if ((fd = open(devname, O_RDONLY)) < 0)
	{
		return -1;
	}

	ioctl(fd, JSIOCGVERSION, &version);
	ioctl(fd, JSIOCGAXES, &numAxes);
	ioctl(fd, JSIOCGBUTTONS, &numButtons);
	ioctl(fd, JSIOCGNAME(JSNAMELEN), name);

	if (strcmp(name, steering_name) == 0)
	{
		setJsType(JS_TYPE_STEERING);
		DBG_NOTICE("JS_TYPE_STEERING type!");
	}
	else
	{
		setJsType(JS_TYPE_GAME_CTL);
		DBG_NOTICE("JS_TYPE_GAME_CTL type!");
	}

	for (i = 0; i < 6; i++)
	{
		int k = 0;
		cal[i].type = (__u16)calData[(i*6)+k];
		k++;
		cal[i].prec = (__s16)calData[(i*6)+k];
		k++;

		for(j = 0; j < 4; j++)
		{
			cal[i].coef[j] = (__s32)calData[(i*6)+k];
			k++;
		}
	}

	if (ioctl(fd, JSIOCSCORR, &cal) < 0)
	{
		return -1;
	}

	axis = (int *)calloc(numAxes, sizeof(int));
	button = (char *)calloc(numButtons, sizeof(char));
#if 0
	gis = g_unix_input_stream_new(fd, TRUE);
	if(gis == NULL)
	{
		DBG_ERROR("g_unix_input_stream_new() failed!");
	}
	else
	{
		NOTICEMSG("g_unix_input_stream_new() succeed!");
	}
	g_input_stream_read_async(gis, &jsEvent, sizeof(struct js_event), G_PRIORITY_DEFAULT, NULL, &readCallback, NULL);
#endif

	return fd;
}

void js_close(int js)
{
	if (js < 0)
	{
		return;
	}

	close(js);
}

int init_timer()
{
    struct sigaction act;
    struct itimerspec itval;

    memset(&act, 0, sizeof(struct sigaction));
    memset(&oldact, 0, sizeof(struct sigaction));

    act.sa_handler = updateTimerHandler;
    act.sa_flags = SA_RESTART;
    if(sigaction(SIGALRM, &act, &oldact) < 0) {
        DBG_ERROR("sigaction failed.");
        return -1;
    }

    itval.it_value.tv_sec = 0;
    itval.it_value.tv_nsec = 200000000;
    itval.it_interval.tv_sec = 0;
    itval.it_interval.tv_nsec = 200000000;

    if(timer_create(CLOCK_REALTIME, NULL, &tid) < 0) {
        DBG_ERROR("timer_create failed.");
        return -1;
    }

    if(timer_settime(tid, 0, &itval, NULL) < 0) {
        DBG_ERROR("timer_settime failed.");
        return -1;
    }

    return 0;
}

void deinit_timer()
{
    timer_delete(tid);
    sigaction(SIGALRM, &oldact, NULL);
}