summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/src/s_midi_oss.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/s_midi_oss.c')
-rw-r--r--apps/plugins/pdbox/PDa/src/s_midi_oss.c358
1 files changed, 0 insertions, 358 deletions
diff --git a/apps/plugins/pdbox/PDa/src/s_midi_oss.c b/apps/plugins/pdbox/PDa/src/s_midi_oss.c
index 1adfcd0d0c..fab1d84b6f 100644
--- a/apps/plugins/pdbox/PDa/src/s_midi_oss.c
+++ b/apps/plugins/pdbox/PDa/src/s_midi_oss.c
@@ -357,362 +357,4 @@ void midi_getdevs(char *indevlist, int *nindevs,
357 sprintf(outdevlist + i * devdescsize, "OSS MIDI device #%d", i+1); 357 sprintf(outdevlist + i * devdescsize, "OSS MIDI device #%d", i+1);
358 *noutdevs = ndev; 358 *noutdevs = ndev;
359} 359}
360/* Copyright (c) 1997-1999 Guenter Geiger, Miller Puckette, Larry Troxler,
361* Winfried Ritsch, Karl MacMillan, and others.
362* For information on usage and redistribution, and for a DISCLAIMER OF ALL
363* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
364
365/* MIDI I/O for Linux using OSS */
366
367#include <stdio.h>
368#ifdef UNIX
369#include <unistd.h>
370#endif
371#include <stdlib.h>
372#include <sys/types.h>
373#include <sys/stat.h>
374#include <fcntl.h>
375#include <errno.h>
376#include "m_pd.h"
377#include "s_stuff.h"
378
379static int oss_nmidiin;
380static int oss_midiinfd[MAXMIDIINDEV];
381static int oss_nmidiout;
382static int oss_midioutfd[MAXMIDIOUTDEV];
383
384static void oss_midiout(int fd, int n)
385{
386 char b = n;
387 if ((write(fd, (char *) &b, 1)) != 1)
388 perror("midi write");
389}
390
391#define O_MIDIFLAG O_NDELAY
392
393void sys_do_open_midi(int nmidiin, int *midiinvec,
394 int nmidiout, int *midioutvec)
395{
396 int i;
397 for (i = 0; i < nmidiout; i++)
398 oss_midioutfd[i] = -1;
399 for (i = 0, oss_nmidiin = 0; i < nmidiin; i++)
400 {
401 int fd = -1, j, outdevindex = -1;
402 char namebuf[80];
403 int devno = midiinvec[i];
404
405 for (j = 0; j < nmidiout; j++)
406 if (midioutvec[j] == midiinvec[i])
407 outdevindex = j;
408
409 /* try to open the device for read/write. */
410 if (devno == 0 && fd < 0 && outdevindex >= 0)
411 {
412 sys_setalarm(1000000);
413 fd = open("/dev/midi", O_RDWR | O_MIDIFLAG);
414 if (sys_verbose)
415 fprintf(stderr,
416 "device 1: tried /dev/midi READ/WRITE; returned %d\n", fd);
417 if (outdevindex >= 0 && fd >= 0)
418 oss_midioutfd[outdevindex] = fd;
419 }
420 if (fd < 0 && outdevindex >= 0)
421 {
422 sys_setalarm(1000000);
423 sprintf(namebuf, "/dev/midi%2.2d", devno);
424 fd = open(namebuf, O_RDWR | O_MIDIFLAG);
425 if (sys_verbose)
426 fprintf(stderr,
427 "device %d: tried %s READ/WRITE; returned %d\n",
428 devno, namebuf, fd);
429 if (outdevindex >= 0 && fd >= 0)
430 oss_midioutfd[outdevindex] = fd;
431 }
432 if (fd < 0 && outdevindex >= 0)
433 {
434 sys_setalarm(1000000);
435 sprintf(namebuf, "/dev/midi%d", devno);
436 fd = open(namebuf, O_RDWR | O_MIDIFLAG);
437 if (sys_verbose)
438 fprintf(stderr, "device %d: tried %s READ/WRITE; returned %d\n",
439 devno, namebuf, fd);
440 if (outdevindex >= 0 && fd >= 0)
441 oss_midioutfd[outdevindex] = fd;
442 }
443 if (devno == 1 && fd < 0)
444 {
445 sys_setalarm(1000000);
446 fd = open("/dev/midi", O_RDONLY | O_MIDIFLAG);
447 if (sys_verbose)
448 fprintf(stderr,
449 "device 1: tried /dev/midi READONLY; returned %d\n", fd);
450 }
451 if (fd < 0)
452 {
453 sys_setalarm(1000000);
454 sprintf(namebuf, "/dev/midi%2.2d", devno);
455 fd = open(namebuf, O_RDONLY | O_MIDIFLAG);
456 if (sys_verbose)
457 fprintf(stderr, "device %d: tried %s READONLY; returned %d\n",
458 devno, namebuf, fd);
459 }
460 if (fd < 0)
461 {
462 sys_setalarm(1000000);
463 sprintf(namebuf, "/dev/midi%d", devno);
464 fd = open(namebuf, O_RDONLY | O_MIDIFLAG);
465 if (sys_verbose)
466 fprintf(stderr, "device %d: tried %s READONLY; returned %d\n",
467 devno, namebuf, fd);
468 }
469 if (fd >= 0)
470 oss_midiinfd[oss_nmidiin++] = fd;
471 else post("couldn't open MIDI input device %d", devno);
472 }
473 for (i = 0, oss_nmidiout = 0; i < nmidiout; i++)
474 {
475 int fd = oss_midioutfd[i];
476 char namebuf[80];
477 int devno = midioutvec[i];
478 if (devno == 1 && fd < 0)
479 {
480 sys_setalarm(1000000);
481 fd = open("/dev/midi", O_WRONLY | O_MIDIFLAG);
482 if (sys_verbose)
483 fprintf(stderr,
484 "device 1: tried /dev/midi WRITEONLY; returned %d\n", fd);
485 }
486 if (fd < 0)
487 {
488 sys_setalarm(1000000);
489 sprintf(namebuf, "/dev/midi%2.2d", devno);
490 fd = open(namebuf, O_WRONLY | O_MIDIFLAG);
491 if (sys_verbose)
492 fprintf(stderr, "device %d: tried %s WRITEONLY; returned %d\n",
493 devno, namebuf, fd);
494 }
495 if (fd < 0)
496 {
497 sys_setalarm(1000000);
498 sprintf(namebuf, "/dev/midi%d", devno);
499 fd = open(namebuf, O_WRONLY | O_MIDIFLAG);
500 if (sys_verbose)
501 fprintf(stderr, "device %d: tried %s WRITEONLY; returned %d\n",
502 devno, namebuf, fd);
503 }
504 if (fd >= 0)
505 oss_midioutfd[oss_nmidiout++] = fd;
506 else post("couldn't open MIDI output device %d", devno);
507 }
508
509 if (oss_nmidiin < nmidiin || oss_nmidiout < nmidiout || sys_verbose)
510 post("opened %d MIDI input device(s) and %d MIDI output device(s).",
511 oss_nmidiin, oss_nmidiout);
512
513 sys_setalarm(0);
514}
515
516#define md_msglen(x) (((x)<0xC0)?2:((x)<0xE0)?1:((x)<0xF0)?2:\
517 ((x)==0xF2)?2:((x)<0xF4)?1:0)
518
519void sys_putmidimess(int portno, int a, int b, int c)
520{
521 if (portno >= 0 && portno < oss_nmidiout)
522 {
523 switch (md_msglen(a))
524 {
525 case 2:
526 oss_midiout(oss_midioutfd[portno],a);
527 oss_midiout(oss_midioutfd[portno],b);
528 oss_midiout(oss_midioutfd[portno],c);
529 return;
530 case 1:
531 oss_midiout(oss_midioutfd[portno],a);
532 oss_midiout(oss_midioutfd[portno],b);
533 return;
534 case 0:
535 oss_midiout(oss_midioutfd[portno],a);
536 return;
537 };
538 }
539}
540
541void sys_putmidibyte(int portno, int byte)
542{
543 if (portno >= 0 && portno < oss_nmidiout)
544 oss_midiout(oss_midioutfd[portno], byte);
545}
546
547#if 0 /* this is the "select" version which doesn't work with OSS
548 driver for emu10k1 (it doesn't implement select.) */
549void sys_poll_midi(void)
550{
551 int i, throttle = 100;
552 struct timeval timout;
553 int did = 1, maxfd = 0;
554 while (did)
555 {
556 fd_set readset, writeset, exceptset;
557 did = 0;
558 if (throttle-- < 0)
559 break;
560 timout.tv_sec = 0;
561 timout.tv_usec = 0;
562
563 FD_ZERO(&writeset);
564 FD_ZERO(&readset);
565 FD_ZERO(&exceptset);
566 for (i = 0; i < oss_nmidiin; i++)
567 {
568 if (oss_midiinfd[i] > maxfd)
569 maxfd = oss_midiinfd[i];
570 FD_SET(oss_midiinfd[i], &readset);
571 }
572 select(maxfd+1, &readset, &writeset, &exceptset, &timout);
573 for (i = 0; i < oss_nmidiin; i++)
574 if (FD_ISSET(oss_midiinfd[i], &readset))
575 {
576 char c;
577 int ret = read(oss_midiinfd[i], &c, 1);
578 if (ret <= 0)
579 fprintf(stderr, "Midi read error\n");
580 else sys_midibytein(i, (c & 0xff));
581 did = 1;
582 }
583 }
584}
585#else
586 360
587 /* this version uses the asynchronous "read()" ... */
588void sys_poll_midi(void)
589{
590 int i, throttle = 100;
591 struct timeval timout;
592 int did = 1, maxfd = 0;
593 while (did)
594 {
595 fd_set readset, writeset, exceptset;
596 did = 0;
597 if (throttle-- < 0)
598 break;
599 for (i = 0; i < oss_nmidiin; i++)
600 {
601 char c;
602 int ret = read(oss_midiinfd[i], &c, 1);
603 if (ret < 0)
604 {
605 if (errno != EAGAIN)
606 perror("MIDI");
607 }
608 else if (ret != 0)
609 {
610 sys_midibytein(i, (c & 0xff));
611 did = 1;
612 }
613 }
614 }
615}
616#endif
617
618void sys_close_midi()
619{
620 int i;
621 for (i = 0; i < oss_nmidiin; i++)
622 close(oss_midiinfd[i]);
623 for (i = 0; i < oss_nmidiout; i++)
624 close(oss_midioutfd[i]);
625 oss_nmidiin = oss_nmidiout = 0;
626}
627
628#define NSEARCH 10
629static int oss_nmidiindevs, oss_nmidioutdevs, oss_initted;
630
631void midi_oss_init(void)
632{
633 int i;
634 if (oss_initted)
635 return;
636 oss_initted = 1;
637 for (i = 0; i < NSEARCH; i++)
638 {
639 int fd;
640 char namebuf[80];
641
642 oss_nmidiindevs = i;
643 /* try to open the device for reading */
644 if (i == 0)
645 {
646 fd = open("/dev/midi", O_RDONLY | O_NDELAY);
647 if (fd >= 0)
648 {
649 close(fd);
650 continue;
651 }
652 }
653 sprintf(namebuf, "/dev/midi%2.2d", i);
654 fd = open(namebuf, O_RDONLY | O_NDELAY);
655 if (fd >= 0)
656 {
657 close(fd);
658 continue;
659 }
660 sprintf(namebuf, "/dev/midi%d", i);
661 fd = open(namebuf, O_RDONLY | O_NDELAY);
662 if (fd >= 0)
663 {
664 close(fd);
665 continue;
666 }
667 break;
668 }
669 for (i = 0; i < NSEARCH; i++)
670 {
671 int fd;
672 char namebuf[80];
673
674 oss_nmidioutdevs = i;
675 /* try to open the device for writing */
676 if (i == 0)
677 {
678 fd = open("/dev/midi", O_WRONLY | O_NDELAY);
679 if (fd >= 0)
680 {
681 close(fd);
682 continue;
683 }
684 }
685 sprintf(namebuf, "/dev/midi%2.2d", i);
686 fd = open(namebuf, O_WRONLY | O_NDELAY);
687 if (fd >= 0)
688 {
689 close(fd);
690 continue;
691 }
692 sprintf(namebuf, "/dev/midi%d", i);
693 fd = open(namebuf, O_WRONLY | O_NDELAY);
694 if (fd >= 0)
695 {
696 close(fd);
697 continue;
698 }
699 break;
700 }
701}
702
703void midi_getdevs(char *indevlist, int *nindevs,
704 char *outdevlist, int *noutdevs, int maxndev, int devdescsize)
705{
706 int i, ndev;
707 if ((ndev = oss_nmidiindevs) > maxndev)
708 ndev = maxndev;
709 for (i = 0; i < ndev; i++)
710 sprintf(indevlist + i * devdescsize, "OSS MIDI device #%d", i+1);
711 *nindevs = ndev;
712
713 if ((ndev = oss_nmidioutdevs) > maxndev)
714 ndev = maxndev;
715 for (i = 0; i < ndev; i++)
716 sprintf(outdevlist + i * devdescsize, "OSS MIDI device #%d", i+1);
717 *noutdevs = ndev;
718}