1 |
|
|
/* $OpenBSD: mio_rmidi.c,v 1.24 2016/01/09 08:27:24 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 "debug.h" |
31 |
|
|
#include "mio_priv.h" |
32 |
|
|
|
33 |
|
|
#define DEVPATH_PREFIX "/dev/rmidi" |
34 |
|
|
#define DEVPATH_MAX (1 + \ |
35 |
|
|
sizeof(DEVPATH_PREFIX) - 1 + \ |
36 |
|
|
sizeof(int) * 3) |
37 |
|
|
|
38 |
|
|
struct mio_rmidi_hdl { |
39 |
|
|
struct mio_hdl mio; |
40 |
|
|
int fd; |
41 |
|
|
}; |
42 |
|
|
|
43 |
|
|
static void mio_rmidi_close(struct mio_hdl *); |
44 |
|
|
static size_t mio_rmidi_read(struct mio_hdl *, void *, size_t); |
45 |
|
|
static size_t mio_rmidi_write(struct mio_hdl *, const void *, size_t); |
46 |
|
|
static int mio_rmidi_nfds(struct mio_hdl *); |
47 |
|
|
static int mio_rmidi_pollfd(struct mio_hdl *, struct pollfd *, int); |
48 |
|
|
static int mio_rmidi_revents(struct mio_hdl *, struct pollfd *); |
49 |
|
|
|
50 |
|
|
static struct mio_ops mio_rmidi_ops = { |
51 |
|
|
mio_rmidi_close, |
52 |
|
|
mio_rmidi_write, |
53 |
|
|
mio_rmidi_read, |
54 |
|
|
mio_rmidi_nfds, |
55 |
|
|
mio_rmidi_pollfd, |
56 |
|
|
mio_rmidi_revents |
57 |
|
|
}; |
58 |
|
|
|
59 |
|
|
int |
60 |
|
|
mio_rmidi_getfd(const char *str, unsigned int mode, int nbio) |
61 |
|
|
{ |
62 |
|
|
const char *p; |
63 |
|
|
char path[DEVPATH_MAX]; |
64 |
|
|
unsigned int devnum; |
65 |
|
|
int fd, flags; |
66 |
|
|
|
67 |
|
|
#ifdef DEBUG |
68 |
|
|
_sndio_debug_init(); |
69 |
|
|
#endif |
70 |
|
|
p = _sndio_parsetype(str, "rmidi"); |
71 |
|
|
if (p == NULL) { |
72 |
|
|
DPRINTF("mio_rmidi_getfd: %s: \"rsnd\" expected\n", str); |
73 |
|
|
return -1; |
74 |
|
|
} |
75 |
|
|
switch (*p) { |
76 |
|
|
case '/': |
77 |
|
|
p++; |
78 |
|
|
break; |
79 |
|
|
default: |
80 |
|
|
DPRINTF("mio_rmidi_getfd: %s: '/' expected\n", str); |
81 |
|
|
return -1; |
82 |
|
|
} |
83 |
|
|
p = _sndio_parsenum(p, &devnum, 255); |
84 |
|
|
if (p == NULL || *p != '\0') { |
85 |
|
|
DPRINTF("mio_rmidi_getfd: %s: number expected after '/'\n", str); |
86 |
|
|
return -1; |
87 |
|
|
} |
88 |
|
|
snprintf(path, sizeof(path), DEVPATH_PREFIX "%u", devnum); |
89 |
|
|
if (mode == (MIO_IN | MIO_OUT)) |
90 |
|
|
flags = O_RDWR; |
91 |
|
|
else |
92 |
|
|
flags = (mode & MIO_OUT) ? O_WRONLY : O_RDONLY; |
93 |
|
|
while ((fd = open(path, flags | O_NONBLOCK | O_CLOEXEC)) < 0) { |
94 |
|
|
if (errno == EINTR) |
95 |
|
|
continue; |
96 |
|
|
DPERROR(path); |
97 |
|
|
return -1; |
98 |
|
|
} |
99 |
|
|
return fd; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
struct mio_hdl * |
103 |
|
|
mio_rmidi_fdopen(int fd, unsigned int mode, int nbio) |
104 |
|
|
{ |
105 |
|
|
struct mio_rmidi_hdl *hdl; |
106 |
|
|
|
107 |
|
|
#ifdef DEBUG |
108 |
|
|
_sndio_debug_init(); |
109 |
|
|
#endif |
110 |
|
|
hdl = malloc(sizeof(struct mio_rmidi_hdl)); |
111 |
|
|
if (hdl == NULL) |
112 |
|
|
return NULL; |
113 |
|
|
_mio_create(&hdl->mio, &mio_rmidi_ops, mode, nbio); |
114 |
|
|
hdl->fd = fd; |
115 |
|
|
return (struct mio_hdl *)hdl; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
struct mio_hdl * |
119 |
|
|
_mio_rmidi_open(const char *str, unsigned int mode, int nbio) |
120 |
|
|
{ |
121 |
|
|
struct mio_hdl *hdl; |
122 |
|
|
int fd; |
123 |
|
|
|
124 |
|
|
fd = mio_rmidi_getfd(str, mode, nbio); |
125 |
|
|
if (fd < 0) |
126 |
|
|
return NULL; |
127 |
|
|
hdl = mio_rmidi_fdopen(fd, mode, nbio); |
128 |
|
|
if (hdl != NULL) |
129 |
|
|
return hdl; |
130 |
|
|
while (close(fd) < 0 && errno == EINTR) |
131 |
|
|
; /* retry */ |
132 |
|
|
return NULL; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
static void |
136 |
|
|
mio_rmidi_close(struct mio_hdl *sh) |
137 |
|
|
{ |
138 |
|
|
struct mio_rmidi_hdl *hdl = (struct mio_rmidi_hdl *)sh; |
139 |
|
|
int rc; |
140 |
|
|
|
141 |
|
|
do { |
142 |
|
|
rc = close(hdl->fd); |
143 |
|
|
} while (rc < 0 && errno == EINTR); |
144 |
|
|
free(hdl); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
static size_t |
148 |
|
|
mio_rmidi_read(struct mio_hdl *sh, void *buf, size_t len) |
149 |
|
|
{ |
150 |
|
|
struct mio_rmidi_hdl *hdl = (struct mio_rmidi_hdl *)sh; |
151 |
|
|
ssize_t n; |
152 |
|
|
|
153 |
|
|
while ((n = read(hdl->fd, buf, len)) < 0) { |
154 |
|
|
if (errno == EINTR) |
155 |
|
|
continue; |
156 |
|
|
if (errno != EAGAIN) { |
157 |
|
|
DPERROR("mio_rmidi_read: read"); |
158 |
|
|
hdl->mio.eof = 1; |
159 |
|
|
} |
160 |
|
|
return 0; |
161 |
|
|
} |
162 |
|
|
if (n == 0) { |
163 |
|
|
DPRINTF("mio_rmidi_read: eof\n"); |
164 |
|
|
hdl->mio.eof = 1; |
165 |
|
|
return 0; |
166 |
|
|
} |
167 |
|
|
return n; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
static size_t |
171 |
|
|
mio_rmidi_write(struct mio_hdl *sh, const void *buf, size_t len) |
172 |
|
|
{ |
173 |
|
|
struct mio_rmidi_hdl *hdl = (struct mio_rmidi_hdl *)sh; |
174 |
|
|
ssize_t n; |
175 |
|
|
|
176 |
|
|
while ((n = write(hdl->fd, buf, len)) < 0) { |
177 |
|
|
if (errno == EINTR) |
178 |
|
|
continue; |
179 |
|
|
if (errno != EAGAIN) { |
180 |
|
|
DPERROR("mio_rmidi_write: write"); |
181 |
|
|
hdl->mio.eof = 1; |
182 |
|
|
} |
183 |
|
|
return 0; |
184 |
|
|
} |
185 |
|
|
return n; |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
static int |
189 |
|
|
mio_rmidi_nfds(struct mio_hdl *sh) |
190 |
|
|
{ |
191 |
|
|
return 1; |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
static int |
195 |
|
|
mio_rmidi_pollfd(struct mio_hdl *sh, struct pollfd *pfd, int events) |
196 |
|
|
{ |
197 |
|
|
struct mio_rmidi_hdl *hdl = (struct mio_rmidi_hdl *)sh; |
198 |
|
|
|
199 |
|
|
pfd->fd = hdl->fd; |
200 |
|
|
pfd->events = events; |
201 |
|
|
return 1; |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
static int |
205 |
|
|
mio_rmidi_revents(struct mio_hdl *sh, struct pollfd *pfd) |
206 |
|
|
{ |
207 |
|
|
return pfd->revents; |
208 |
|
|
} |