block_joystick.c 4.81 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2011

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <controller/controller_block.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
29
30
#include <controller/controller_bus.h>
#include <log/log.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
31
32

#include <linux/joystick.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
33
#include <errno.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
34
35
36
37
38

struct controller_block_private {
	float	*axes;
	bool	*buttons;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
39
	char *device;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
40
	int fd;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
41
42
	
	struct controller_bus *bus;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
43
44
45
46
47
48
49
50
};

static void calculate(struct controller_block *block)
{
	struct controller_block_private *priv = block->private;
	struct js_event event;
	int ret;
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
51
52
53
	if (priv->fd < 0)
		return;
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
54
55
56
57
58
59
60
61
62
63
64
	do {
		ret = read (priv->fd, &event, sizeof(struct js_event));
		if (ret == sizeof(struct js_event)) {
			if (event.type & JS_EVENT_BUTTON) {
				priv->buttons[event.number] = event.value;
			}
			if (event.type & JS_EVENT_AXIS) {
				priv->axes[event.number] = event.value / 32768.0;
			}
		}
	} while (ret == sizeof(struct js_event));
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
	
	if (ret <= 0 && errno != EAGAIN) {
		close(priv->fd);
		priv->fd = -1;
		log_send(LOG_T_ERROR, "%s: Error reading device", block->name);
	}
}

static int joystick_open(char *device)
{
	int fd;
	int ret;
	
	fd = open(device, O_RDONLY);
	if (fd < 0)
		return -1;
	
	ret = ioctl(fd, FIONBIO, &(int){1});
	if (ret)
		goto err_ioctl;

	return fd;

err_ioctl:
	close(fd);
	return -1;
}


static enum controller_bus_state joystick_poll(struct controller_bus *bus)
{
	struct controller_block *block = bus->owner;
	int fd = block->private->fd;
	
	if (fd >= 0)
		return CONTROLLER_BUS_STATE_OK;
	
	fd = joystick_open(block->private->device);
	if (fd < 0)
		return CONTROLLER_BUS_STATE_ERROR;
	
	block->private->fd = fd;
	log_send(LOG_T_WARNING, "%s: re-opened %s", block->name,
	    block->private->device);
	
	return CONTROLLER_BUS_STATE_OK;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
111
112
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
113

Jeroen Vreeken's avatar
Jeroen Vreeken committed
114
115
116
117
118
119
120
struct controller_block * block_joystick_create(char *name, va_list ap)
{
	struct controller_block *block;
	int fd;
	char *device;
	int nr_axes = 0, nr_buttons = 0;
	int i, ret;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
121

Jeroen Vreeken's avatar
Jeroen Vreeken committed
122
	device = va_arg(ap, char *);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
123
124

	fd = joystick_open(device);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
125
	if (fd < 0) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
126
127
		log_send(LOG_T_ERROR, "Could not open device %s", device);
		return NULL;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
128
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
129
130
131
132
133
134
135
136
137
	ret = ioctl(fd, JSIOCGAXES, &nr_axes);
	if (ret)
		goto err_ioctl;
	ret = ioctl(fd, JSIOCGBUTTONS, &nr_buttons);
	if (ret)
		goto err_ioctl;

	log_send(LOG_T_DEBUG, "%s: %d axes, %d buttons", 
	    device, nr_axes, nr_buttons);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
138
139
	

Jeroen Vreeken's avatar
Jeroen Vreeken committed
140
141
	if (!(block = controller_block_alloc("joystick", name, sizeof(struct controller_block_private))))
		goto err_block;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
142
143
144
145
146
147
148
149

	block->private->axes = calloc(sizeof(float), nr_axes);
	if (!block->private->axes)
		goto err_axes;
	block->private->buttons = calloc(sizeof(bool), nr_buttons);
	if (!block->private->buttons)
		goto err_buttons;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
150
	block->private->device = strdup(device);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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

	block->inputs = 0;
	block->input = NULL;
	
	/* Inputs on the hardware are outputs on this software block */
	block->outputs = nr_axes + nr_buttons;
	block->output = malloc(sizeof(struct controller_block_outterm) * 
	    (nr_axes + nr_buttons));
	if (!block->output)
		goto err_output;
	for (i = 0; i < nr_axes; i++) {
		block->output[i].type = CONTROLLER_BLOCK_TERM_FLOAT;
		block->output[i].value.f = &block->private->axes[i];
		block->output[i].source = block;
		block->output[i].name = malloc(16);
		sprintf(block->output[i].name, "axis%d", i);
	}
	for (i = 0; i < nr_buttons; i++) {
		int outnr = i + nr_axes;
		block->output[outnr].type = CONTROLLER_BLOCK_TERM_BOOL;
		block->output[outnr].value.b = &block->private->buttons[i];
		block->output[outnr].source = block;
		block->output[outnr].name = malloc(16);
		sprintf(block->output[outnr].name, "button%d", i);
	}
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
177
178
179
180
181
182
183
184
	block->private->bus = controller_bus_create("joystick", name, block, 0);
	if (!block->private->bus)
		goto err_bus;

	controller_bus_poll_set(block->private->bus, joystick_poll, NULL);

	block->calculate = calculate;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
185
186
187
188
189
190
191
192
193
194
	block->params = 0;
	block->param = NULL;
	block->param_get = NULL;
	block->param_set = NULL;
	
	block->private->fd = fd;

	controller_block_add(block);
	return block;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
195
err_bus:
Jeroen Vreeken's avatar
Jeroen Vreeken committed
196
197
err_output:
	free(block->private->buttons);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
198
	free(block->private->device);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
199
200
201
202
203
err_buttons:
	free(block->private->axes);
err_axes:
	free(block->private);
err_block:
Jeroen Vreeken's avatar
Jeroen Vreeken committed
204
	controller_block_free(block);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
205
206
207
208
err_ioctl:
	close(fd);
	return NULL;
}