1 /* $OpenBSD: mio_rmidi.c,v 1.6 2010/04/24 06:15:54 ratchov Exp $ */ 2 /* 3 * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/types.h> 19 #include <sys/stat.h> 20 21 #include <errno.h> 22 #include <fcntl.h> 23 #include <limits.h> 24 #include <poll.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <unistd.h> 29 30 #include "mio_priv.h" 31 32 #define RMIDI_PATH "/dev/rmidi0" 33 34 struct rmidi_hdl { 35 struct mio_hdl mio; 36 int fd; 37 }; 38 39 static void rmidi_close(struct mio_hdl *); 40 static size_t rmidi_read(struct mio_hdl *, void *, size_t); 41 static size_t rmidi_write(struct mio_hdl *, const void *, size_t); 42 static int rmidi_pollfd(struct mio_hdl *, struct pollfd *, int); 43 static int rmidi_revents(struct mio_hdl *, struct pollfd *); 44 45 static struct mio_ops rmidi_ops = { 46 rmidi_close, 47 rmidi_write, 48 rmidi_read, 49 rmidi_pollfd, 50 rmidi_revents, 51 }; 52 53 struct mio_hdl * 54 mio_open_rmidi(const char *str, unsigned mode, int nbio) 55 { 56 int fd, flags; 57 struct rmidi_hdl *hdl; 58 char path[PATH_MAX]; 59 60 hdl = malloc(sizeof(struct rmidi_hdl)); 61 if (hdl == NULL) 62 return NULL; 63 mio_create(&hdl->mio, &rmidi_ops, mode, nbio); 64 65 snprintf(path, sizeof(path), "/dev/rmidi%s", str); 66 if (mode == (MIO_OUT | MIO_IN)) 67 flags = O_RDWR; 68 else 69 flags = (mode & MIO_OUT) ? O_WRONLY : O_RDONLY; 70 if (nbio) 71 flags |= O_NONBLOCK; 72 while ((fd = open(path, flags)) < 0) { 73 if (errno == EINTR) 74 continue; 75 DPERROR(path); 76 goto bad_free; 77 } 78 if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) { 79 DPERROR("FD_CLOEXEC"); 80 goto bad_close; 81 } 82 hdl->fd = fd; 83 return (struct mio_hdl *)hdl; 84 bad_close: 85 while (close(hdl->fd) < 0 && errno == EINTR) 86 ; /* retry */ 87 bad_free: 88 free(hdl); 89 return NULL; 90 } 91 92 static void 93 rmidi_close(struct mio_hdl *sh) 94 { 95 struct rmidi_hdl *hdl = (struct rmidi_hdl *)sh; 96 int rc; 97 98 do { 99 rc = close(hdl->fd); 100 } while (rc < 0 && errno == EINTR); 101 free(hdl); 102 } 103 104 static size_t 105 rmidi_read(struct mio_hdl *sh, void *buf, size_t len) 106 { 107 struct rmidi_hdl *hdl = (struct rmidi_hdl *)sh; 108 ssize_t n; 109 110 while ((n = read(hdl->fd, buf, len)) < 0) { 111 if (errno == EINTR) 112 continue; 113 if (errno != EAGAIN) { 114 DPERROR("rmidi_read: read"); 115 hdl->mio.eof = 1; 116 } 117 return 0; 118 } 119 if (n == 0) { 120 DPRINTF("rmidi_read: eof\n"); 121 hdl->mio.eof = 1; 122 return 0; 123 } 124 return n; 125 } 126 127 static size_t 128 rmidi_write(struct mio_hdl *sh, const void *buf, size_t len) 129 { 130 struct rmidi_hdl *hdl = (struct rmidi_hdl *)sh; 131 ssize_t n; 132 133 while ((n = write(hdl->fd, buf, len)) < 0) { 134 if (errno == EINTR) 135 continue; 136 if (errno != EAGAIN) { 137 DPERROR("rmidi_write: write"); 138 hdl->mio.eof = 1; 139 return 0; 140 } 141 return 0; 142 } 143 return n; 144 } 145 146 static int 147 rmidi_pollfd(struct mio_hdl *sh, struct pollfd *pfd, int events) 148 { 149 struct rmidi_hdl *hdl = (struct rmidi_hdl *)sh; 150 151 pfd->fd = hdl->fd; 152 pfd->events = events; 153 return 1; 154 } 155 156 static int 157 rmidi_revents(struct mio_hdl *sh, struct pollfd *pfd) 158 { 159 return pfd->revents; 160 } 161