block_joystick.c 3.95 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
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
/*
	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>

#include <linux/joystick.h>


struct controller_block_private {
	float	*axes;
	bool	*buttons;

	int fd;
};

static void calculate(struct controller_block *block)
{
	struct controller_block_private *priv = block->private;
	struct js_event event;
	int ret;
	
	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));
}

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;
	bool sim = false;
		
	device = va_arg(ap, char *);
	
	fd = open(device, O_RDONLY);
	if (fd < 0) {
		printf("Could not open device %s\n", device);
		printf("Going to simulate a joystick\n");
		sim = true;
		device = "simulated joystick";
		nr_axes = 6;
		nr_buttons = 8;
	} else {
		ret = ioctl(fd, FIONBIO, &(int){1});
		if (ret)
			goto err_ioctl;
		ret = ioctl(fd, JSIOCGAXES, &nr_axes);
		if (ret)
			goto err_ioctl;
		ret = ioctl(fd, JSIOCGBUTTONS, &nr_buttons);
		if (ret)
			goto err_ioctl;
	}
	printf("%s: %d axes, %d buttons\n", device, nr_axes, nr_buttons);
	
	block = malloc(sizeof(struct controller_block));
	if (!block)
		goto err_block;

	block->private = malloc(sizeof(struct controller_block_private));
	if (!block->private)
		goto err_private;

	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;

	block->type = sim ? "joystick_sim" : "joystick";
	block->name = strdup(name);

	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);
	}
	
	if (sim) {
		block->calculate = NULL;
	} else {
		block->calculate = calculate;
	}
	block->params = 0;
	block->param = NULL;
	block->param_get = NULL;
	block->param_set = NULL;
	
	block->private->fd = fd;

	controller_block_add(block);
	return block;

err_output:
	free(block->private->buttons);
err_buttons:
	free(block->private->axes);
err_axes:
	free(block->private);
err_private:
	free(block);
err_block:
err_ioctl:
	close(fd);
	return NULL;
}