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
23 #include <sys/ioctl.h>
25 #include <sys/types.h>
34 #include <linux/joystick.h>
35 #include <alsa/asoundlib.h>
37 #define NAME_LENGTH 128
39 #define TOOL_NAME "aseqjoy"
52 snd_seq_t *seq_handle;
63 /* Create the sequencer port. */
65 sprintf(client_name, "Joystick%i", joystick_no);
66 sprintf(port_name , "%s Output", client_name);
68 if (snd_seq_open(&seq_handle, "default", SND_SEQ_OPEN_OUTPUT, 0) < 0) {
69 puts("Error: Failed to access the ALSA sequencer.");
73 snd_seq_set_client_name(seq_handle, client_name);
74 src.client = snd_seq_client_id(seq_handle);
75 src.port = snd_seq_create_simple_port(seq_handle, "Joystick Output",
76 SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ, SND_SEQ_PORT_TYPE_APPLICATION);
78 /* Init the event structure */
80 snd_seq_ev_clear(&ev);
81 snd_seq_ev_set_source(&ev, src.port);
82 snd_seq_ev_set_subs(&ev);
83 snd_seq_ev_set_direct(&ev);
95 char name[NAME_LENGTH] = "Unknown";
97 sprintf(device, "/dev/js%i", joystick_no);
99 if ((joy_fd = open(device, O_RDONLY)) < 0) {
100 fprintf(stderr, "%s: ", TOOL_NAME); perror(device);
101 sprintf(device, "/dev/input/js%i", joystick_no);
103 if ((joy_fd = open(device, O_RDONLY)) < 0) {
104 fprintf(stderr, "%s: ", TOOL_NAME); perror(device);
109 ioctl(joy_fd, JSIOCGAXES, &axes);
110 ioctl(joy_fd, JSIOCGBUTTONS, &buttons);
111 ioctl(joy_fd, JSIOCGNAME(NAME_LENGTH), name);
113 printf("Using Joystick (%s) through device %s with %i axes and %i buttons.\n", name, device, axes, buttons);
121 int current_channel=1;
127 values = calloc(axes, sizeof(val));
129 puts("Axis -> MIDI controller mapping:");
131 for (i=0; i<axes; i++) {
133 values[i].controller=controllers[i];
135 values[i].controller=10+i;
137 printf(" %2i -> %3i\n", i, values[i].controller);
138 values[i].last_value=0;
143 puts("Ready, entering loop - use Ctrl-C to exit.");
146 if (read(joy_fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
147 perror(TOOL_NAME ": error reading from joystick device");
151 switch(js.type & ~JS_EVENT_INIT) {
152 case JS_EVENT_BUTTON:
154 current_channel=js.number+1;
157 printf("Switched to MIDI channel %i.\n", current_channel);
163 val_d=(double) js.value;
165 val_d=val_d/((double) USHRT_MAX);
170 if (values[js.number].last_value!=val_i) {
171 values[js.number].last_value!=val_i;
172 snd_seq_ev_set_controller(&ev, current_channel, values[js.number].controller, val_i);
173 snd_seq_event_output_direct(seq_handle, &ev);
176 printf("Sent controller %i with value: %i.\n", values[js.number].controller, val_i);
184 int main (int argc, char **argv)
187 fprintf(stderr, "%s Version %s - Copyright (C) 2003 by Alexander König\n", TOOL_NAME, VERSION);
188 fprintf(stderr, "%s comes with ABSOLUTELY NO WARRANTY - for details read the license.\n", TOOL_NAME);
190 for (i=0; i<4; i++) {
195 int i=getopt(argc, argv, "vhd:1:2:3:4:");
201 printf("usage: %s [-d joystick_no] [-v] [-0 ctrl0] [-1 ctrl1] [-2 ctrl2] [-3 ctrl3]\n\n", TOOL_NAME);
202 puts("\t-d Select the Joystick to use: 0..3");
203 puts("\t-0 Select the Controller for Axis 0 (1-127).");
204 puts("\t-1 Select the Controller for Axis 1 (1-127). Etc.");
205 puts("\t-v Verbose mode.");
210 controllers[0]=atoi(optarg);
214 controllers[1]=atoi(optarg);
218 controllers[2]=atoi(optarg);
222 controllers[3]=atoi(optarg);
230 joystick_no=atoi(optarg);