2 * aseqjoy - Tiny Jostick -> MIDI Controller Tool
3 * Copyright 2003 by Alexander Koenig - alex@lisas.de
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Note: that these sources contain a few lines of Vojtech Pavlik's jstest.c
20 * example, which is GPL'd, too and available from:
21 * http://atrey.karlin.mff.cuni.cz/~vojtech/joystick/
24 #include <sys/ioctl.h>
26 #include <sys/types.h>
35 #include <linux/joystick.h>
36 #include <alsa/asoundlib.h>
38 #define NAME_LENGTH 128
40 #define TOOL_NAME "aseqjoy"
53 snd_seq_t *seq_handle;
64 /* Create the sequencer port. */
66 sprintf(client_name, "Joystick%i", joystick_no);
67 sprintf(port_name , "%s Output", client_name);
69 if (snd_seq_open(&seq_handle, "default", SND_SEQ_OPEN_OUTPUT, 0) < 0) {
70 puts("Error: Failed to access the ALSA sequencer.");
74 snd_seq_set_client_name(seq_handle, client_name);
75 src.client = snd_seq_client_id(seq_handle);
76 src.port = snd_seq_create_simple_port(seq_handle, "Joystick Output",
77 SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ, SND_SEQ_PORT_TYPE_APPLICATION);
79 /* Init the event structure */
81 snd_seq_ev_clear(&ev);
82 snd_seq_ev_set_source(&ev, src.port);
83 snd_seq_ev_set_subs(&ev);
84 snd_seq_ev_set_direct(&ev);
96 char name[NAME_LENGTH] = "Unknown";
98 sprintf(device, "/dev/js%i", joystick_no);
100 if ((joy_fd = open(device, O_RDONLY)) < 0) {
101 fprintf(stderr, "%s: ", TOOL_NAME); perror(device);
102 sprintf(device, "/dev/input/js%i", joystick_no);
104 if ((joy_fd = open(device, O_RDONLY)) < 0) {
105 fprintf(stderr, "%s: ", TOOL_NAME); perror(device);
110 ioctl(joy_fd, JSIOCGAXES, &axes);
111 ioctl(joy_fd, JSIOCGBUTTONS, &buttons);
112 ioctl(joy_fd, JSIOCGNAME(NAME_LENGTH), name);
114 printf("Using Joystick (%s) through device %s with %i axes and %i buttons.\n", name, device, axes, buttons);
122 int current_channel=1;
128 values = calloc(axes, sizeof(val));
130 puts("Axis -> MIDI controller mapping:");
132 for (i=0; i<axes; i++) {
134 values[i].controller=controllers[i];
136 values[i].controller=10+i;
138 printf(" %2i -> %3i\n", i, values[i].controller);
139 values[i].last_value=0;
142 puts("Ready, entering loop - use Ctrl-C to exit.");
145 if (read(joy_fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
146 perror(TOOL_NAME ": error reading from joystick device");
150 switch(js.type & ~JS_EVENT_INIT) {
151 case JS_EVENT_BUTTON:
153 current_channel=js.number+1;
156 printf("Switched to MIDI channel %i.\n", current_channel);
162 val_d=(double) js.value;
164 val_d=val_d/((double) USHRT_MAX);
169 if (values[js.number].last_value!=val_i) {
170 values[js.number].last_value!=val_i;
171 snd_seq_ev_set_controller(&ev, current_channel, values[js.number].controller, val_i);
172 snd_seq_event_output_direct(seq_handle, &ev);
175 printf("Sent controller %i with value: %i.\n", values[js.number].controller, val_i);
183 int main (int argc, char **argv)
186 fprintf(stderr, "%s Version %s - Copyright (C) 2003 by Alexander König\n", TOOL_NAME, VERSION);
187 fprintf(stderr, "%s comes with ABSOLUTELY NO WARRANTY - for details read the license.\n", TOOL_NAME);
189 for (i=0; i<4; i++) {
194 int i=getopt(argc, argv, "vhd:1:2:3:4:");
200 printf("usage: %s [-d joystick_no] [-v] [-0 ctrl0] [-1 ctrl1] [-2 ctrl2] [-3 ctrl3]\n\n", TOOL_NAME);
201 puts("\t-d Select the Joystick to use: 0..3");
202 puts("\t-0 Select the Controller for Axis 0 (1-127).");
203 puts("\t-1 Select the Controller for Axis 1 (1-127). Etc.");
204 puts("\t-v Verbose mode.");
209 controllers[0]=atoi(optarg);
213 controllers[1]=atoi(optarg);
217 controllers[2]=atoi(optarg);
221 controllers[3]=atoi(optarg);
229 joystick_no=atoi(optarg);