aboutsummaryrefslogtreecommitdiff
path: root/pd/src/s_midi_oss.c
blob: 5c11bae328d078b36ecf82bc9192be227f791b3d (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* Copyright (c) 1997-1999 Guenter Geiger, Miller Puckette, Larry Troxler,
* Winfried Ritsch, Karl MacMillan, and others.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* MIDI I/O for Linux using OSS */

#include <stdio.h>
#ifdef UNISTD
#include <unistd.h>
#endif
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "m_pd.h"
#include "s_stuff.h"

#define NSEARCH 10
static int oss_nmidiindevs, oss_nmidioutdevs, oss_initted, oss_onebased;
static int oss_nmidiin;
static int oss_midiinfd[MAXMIDIINDEV];
static int oss_nmidiout;
static int oss_midioutfd[MAXMIDIOUTDEV];

static void oss_midiout(int fd, int n)
{
    char b = n;
    if ((write(fd, (char *) &b, 1)) != 1)
        perror("midi write");
}

#define O_MIDIFLAG O_NDELAY

void sys_do_open_midi(int nmidiin, int *midiinvec,
    int nmidiout, int *midioutvec)
{
    int i;
    for (i = 0; i < nmidiout; i++)
        oss_midioutfd[i] = -1;
    for (i = 0, oss_nmidiin = 0; i < nmidiin; i++)
    {
        int fd = -1, j, outdevindex = -1;
        char namebuf[80];
        int devno = midiinvec[i] + oss_onebased;

        for (j = 0; j < nmidiout; j++)
            if (midioutvec[j] == midiinvec[i])
                outdevindex = j;
        
            /* try to open the device for read/write. */
        if (devno == 0 && fd < 0 && outdevindex >= 0)
        {
            sys_setalarm(1000000);
            fd = open("/dev/midi", O_RDWR | O_MIDIFLAG);
            if (sys_verbose)
                fprintf(stderr,
                    "device 1: tried /dev/midi READ/WRITE; returned %d\n", fd);
            if (outdevindex >= 0 && fd >= 0)
                oss_midioutfd[outdevindex] = fd;
        }
        if (fd < 0 && outdevindex >= 0)
        {
            sys_setalarm(1000000);
            sprintf(namebuf, "/dev/midi%2.2d", devno);
            fd = open(namebuf, O_RDWR | O_MIDIFLAG);
            if (sys_verbose)
                fprintf(stderr,
                    "device %d: tried %s READ/WRITE; returned %d\n",
                        devno, namebuf, fd);
            if (outdevindex >= 0 && fd >= 0)
                oss_midioutfd[outdevindex] = fd;
        }
        if (fd < 0 && outdevindex >= 0)
        {
            sys_setalarm(1000000);
            sprintf(namebuf, "/dev/midi%d", devno);
            fd = open(namebuf, O_RDWR | O_MIDIFLAG);
            if (sys_verbose)
                fprintf(stderr, "device %d: tried %s READ/WRITE; returned %d\n",
                    devno, namebuf, fd);
            if (outdevindex >= 0 && fd >= 0)
                oss_midioutfd[outdevindex] = fd;
        }
        if (devno == 0 && fd < 0)
        {
            sys_setalarm(1000000);
            fd = open("/dev/midi", O_RDONLY | O_MIDIFLAG);
            if (sys_verbose)
                fprintf(stderr,
                    "device 1: tried /dev/midi READONLY; returned %d\n", fd);
        }
        if (fd < 0)
        {
            sys_setalarm(1000000);
            sprintf(namebuf, "/dev/midi%2.2d", devno);
            fd = open(namebuf, O_RDONLY | O_MIDIFLAG);
            if (sys_verbose)
                fprintf(stderr, "device %d: tried %s READONLY; returned %d\n",
                    devno, namebuf, fd);
        }
        if (fd < 0)
        {
            sys_setalarm(1000000);
            sprintf(namebuf, "/dev/midi%d", devno);
            fd = open(namebuf, O_RDONLY | O_MIDIFLAG);
            if (sys_verbose)
                fprintf(stderr, "device %d: tried %s READONLY; returned %d\n",
                    devno, namebuf, fd);
        }
        if (fd >= 0)
            oss_midiinfd[oss_nmidiin++] = fd;       
        else post("couldn't open MIDI input device %d", devno);
    }
    for (i = 0, oss_nmidiout = 0; i < nmidiout; i++)
    {
        int fd = oss_midioutfd[i];
        char namebuf[80];
        int devno = midioutvec[i] + oss_onebased;
        if (devno == 0 && fd < 0)
        {
            sys_setalarm(1000000);
            fd = open("/dev/midi", O_WRONLY | O_MIDIFLAG);
            if (sys_verbose)
                fprintf(stderr,
                    "device 1: tried /dev/midi WRITEONLY; returned %d\n", fd);
        }
        if (fd < 0)
        {
            sys_setalarm(1000000);
            sprintf(namebuf, "/dev/midi%2.2d", devno);
            fd = open(namebuf, O_WRONLY | O_MIDIFLAG);
            if (sys_verbose)
                fprintf(stderr, "device %d: tried %s WRITEONLY; returned %d\n",
                    devno, namebuf, fd);
        }
        if (fd < 0)
        {
            sys_setalarm(1000000);
            sprintf(namebuf, "/dev/midi%d", devno);
            fd = open(namebuf, O_WRONLY | O_MIDIFLAG);
            if (sys_verbose)
                fprintf(stderr, "device %d: tried %s WRITEONLY; returned %d\n",
                    devno, namebuf, fd);
        }
        if (fd >= 0)
            oss_midioutfd[oss_nmidiout++] = fd;     
        else post("couldn't open MIDI output device %d", devno);
    }

    if (oss_nmidiin < nmidiin || oss_nmidiout < nmidiout || sys_verbose)
        post("opened %d MIDI input device(s) and %d MIDI output device(s).",
            oss_nmidiin, oss_nmidiout);

    sys_setalarm(0);
}

#define md_msglen(x) (((x)<0xC0)?2:((x)<0xE0)?1:((x)<0xF0)?2:\
    ((x)==0xF2)?2:((x)<0xF4)?1:0)

void sys_putmidimess(int portno, int a, int b, int c)
{
    if (portno >= 0 && portno < oss_nmidiout)
    {
       switch (md_msglen(a))
       {
       case 2:
            oss_midiout(oss_midioutfd[portno],a);        
            oss_midiout(oss_midioutfd[portno],b);        
            oss_midiout(oss_midioutfd[portno],c);
            return;
       case 1:
            oss_midiout(oss_midioutfd[portno],a);        
            oss_midiout(oss_midioutfd[portno],b);        
            return;
       case 0:
            oss_midiout(oss_midioutfd[portno],a);        
            return;
       };
    }
}

void sys_putmidibyte(int portno, int byte)
{
    if (portno >= 0 && portno < oss_nmidiout)
        oss_midiout(oss_midioutfd[portno], byte);       
}

#if 0   /* this is the "select" version which doesn't work with OSS
        driver for emu10k1 (it doesn't implement select.) */
void sys_poll_midi(void)
{
    int i, throttle = 100;
    struct timeval timout;
    int did = 1, maxfd = 0;
    while (did)
    {
        fd_set readset, writeset, exceptset;
        did = 0;
        if (throttle-- < 0)
            break;
        timout.tv_sec = 0;
        timout.tv_usec = 0;

        FD_ZERO(&writeset);
        FD_ZERO(&readset);
        FD_ZERO(&exceptset);
        for (i = 0; i < oss_nmidiin; i++)
        {
            if (oss_midiinfd[i] > maxfd)
                maxfd = oss_midiinfd[i];
            FD_SET(oss_midiinfd[i], &readset);
        }
        select(maxfd+1, &readset, &writeset, &exceptset, &timout);
        for (i = 0; i < oss_nmidiin; i++)
            if (FD_ISSET(oss_midiinfd[i], &readset))
        {
            char c;
            int ret = read(oss_midiinfd[i], &c, 1);
            if (ret <= 0)
                fprintf(stderr, "Midi read error\n");
            else sys_midibytein(i, (c & 0xff));
            did = 1;
        }
    }
}
#else 

    /* this version uses the asynchronous "read()" ... */
void sys_poll_midi(void)
{
    int i, throttle = 100;
    struct timeval timout;
    int did = 1, maxfd = 0;
    while (did)
    {
        fd_set readset, writeset, exceptset;
        did = 0;
        if (throttle-- < 0)
            break;
        for (i = 0; i < oss_nmidiin; i++)
        {
            char c;
            int ret = read(oss_midiinfd[i], &c, 1);
            if (ret < 0)
            {
                if (errno != EAGAIN)
                    perror("MIDI");
            }
            else if (ret != 0)
            {
                sys_midibytein(i, (c & 0xff));
                did = 1;
            }
        }
    }
}
#endif

void sys_close_midi()
{
    int i;
    for (i = 0; i < oss_nmidiin; i++)
        close(oss_midiinfd[i]);
    for (i = 0; i < oss_nmidiout; i++)
        close(oss_midioutfd[i]);
    oss_nmidiin = oss_nmidiout = 0;
}

void midi_oss_init(void)     
{
    int i, fd, devno;
    struct stat statbuf;
    char namebuf[80];
        
    if (oss_initted)
        return;
    oss_initted = 1;
        /* at some point, Fedora started counting MIDI devices from one -
        catch that here: */
    oss_onebased = (stat("/dev/midi1", &statbuf) >= 0 &&
        stat("/dev/midi0", &statbuf) < 0);
            
    for (i = 0; i < NSEARCH; i++)
    {
        oss_nmidiindevs = i;
        devno = i + oss_onebased;
            /* try to open the device for reading */
        if (devno == 0)
        {
            fd = open("/dev/midi", O_RDONLY | O_NDELAY);
            if (fd >= 0)
            {
                close(fd);
                continue;
            }
        }
        sprintf(namebuf, "/dev/midi%2.2d", devno);
        fd = open(namebuf, O_RDONLY | O_NDELAY);
        if (fd >= 0)
        {
            close(fd);
            continue;
        }
        sprintf(namebuf, "/dev/midi%d", devno);
        fd = open(namebuf, O_RDONLY | O_NDELAY);
        if (fd >= 0)
        {
            close(fd);
            continue;
        }
        break;
    }
    for (i = 0; i < NSEARCH; i++)
    {
        oss_nmidioutdevs = i;
        devno = i + oss_onebased;
            /* try to open the device for writing */
        if (devno == 0)
        {
            fd = open("/dev/midi", O_WRONLY | O_NDELAY);
            if (fd >= 0)
            {
                close(fd);
                continue;
            }
        }
        sprintf(namebuf, "/dev/midi%2.2d", devno);
        fd = open(namebuf, O_WRONLY | O_NDELAY);
        if (fd >= 0)
        {
            close(fd);
            continue;
        }
        sprintf(namebuf, "/dev/midi%d", devno);
        fd = open(namebuf, O_WRONLY | O_NDELAY);
        if (fd >= 0)
        {
            close(fd);
            continue;
        }
        break;
    }
}

void midi_getdevs(char *indevlist, int *nindevs,
    char *outdevlist, int *noutdevs, int maxndev, int devdescsize)
{
    int i, ndev;
    if ((ndev = oss_nmidiindevs) > maxndev)
        ndev = maxndev;
    for (i = 0; i < ndev; i++)
        sprintf(indevlist + i * devdescsize, "OSS MIDI device #%d", i+1);
    *nindevs = ndev;

    if ((ndev = oss_nmidioutdevs) > maxndev)
        ndev = maxndev;
    for (i = 0; i < ndev; i++)
        sprintf(outdevlist + i * devdescsize, "OSS MIDI device #%d", i+1);
    *noutdevs = ndev;
}