xref: /openbsd/lib/libsndio/mio_rmidi.c (revision a6445c1d)
1 /*	$OpenBSD: mio_rmidi.c,v 1.14 2014/08/15 03:51:40 guenther 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 struct mio_rmidi_hdl {
34 	struct mio_hdl mio;
35 	int fd;
36 };
37 
38 static void mio_rmidi_close(struct mio_hdl *);
39 static size_t mio_rmidi_read(struct mio_hdl *, void *, size_t);
40 static size_t mio_rmidi_write(struct mio_hdl *, const void *, size_t);
41 static int mio_rmidi_nfds(struct mio_hdl *);
42 static int mio_rmidi_pollfd(struct mio_hdl *, struct pollfd *, int);
43 static int mio_rmidi_revents(struct mio_hdl *, struct pollfd *);
44 
45 static struct mio_ops mio_rmidi_ops = {
46 	mio_rmidi_close,
47 	mio_rmidi_write,
48 	mio_rmidi_read,
49 	mio_rmidi_nfds,
50 	mio_rmidi_pollfd,
51 	mio_rmidi_revents
52 };
53 
54 struct mio_hdl *
55 _mio_rmidi_open(const char *str, unsigned int mode, int nbio)
56 {
57 	int fd, flags;
58 	struct mio_rmidi_hdl *hdl;
59 	char path[PATH_MAX];
60 
61 	switch (*str) {
62 	case '/':
63 	case ':': /* XXX: for backward compat */
64 		str++;
65 		break;
66 	default:
67 		DPRINTF("_sio_sun_open: %s: '/<devnum>' expected\n", str);
68 		return NULL;
69 	}
70 	hdl = malloc(sizeof(struct mio_rmidi_hdl));
71 	if (hdl == NULL)
72 		return NULL;
73 	_mio_create(&hdl->mio, &mio_rmidi_ops, mode, nbio);
74 
75 	snprintf(path, sizeof(path), "/dev/rmidi%s", str);
76 	if (mode == (MIO_OUT | MIO_IN))
77 		flags = O_RDWR;
78 	else
79 		flags = (mode & MIO_OUT) ? O_WRONLY : O_RDONLY;
80 	while ((fd = open(path, flags | O_NONBLOCK | O_CLOEXEC)) < 0) {
81 		if (errno == EINTR)
82 			continue;
83 		DPERROR(path);
84 		goto bad_free;
85 	}
86 	hdl->fd = fd;
87 	return (struct mio_hdl *)hdl;
88  bad_free:
89 	free(hdl);
90 	return NULL;
91 }
92 
93 static void
94 mio_rmidi_close(struct mio_hdl *sh)
95 {
96 	struct mio_rmidi_hdl *hdl = (struct mio_rmidi_hdl *)sh;
97 	int rc;
98 
99 	do {
100 		rc = close(hdl->fd);
101 	} while (rc < 0 && errno == EINTR);
102 	free(hdl);
103 }
104 
105 static size_t
106 mio_rmidi_read(struct mio_hdl *sh, void *buf, size_t len)
107 {
108 	struct mio_rmidi_hdl *hdl = (struct mio_rmidi_hdl *)sh;
109 	ssize_t n;
110 
111 	while ((n = read(hdl->fd, buf, len)) < 0) {
112 		if (errno == EINTR)
113 			continue;
114 		if (errno != EAGAIN) {
115 			DPERROR("mio_rmidi_read: read");
116 			hdl->mio.eof = 1;
117 		}
118 		return 0;
119 	}
120 	if (n == 0) {
121 		DPRINTF("mio_rmidi_read: eof\n");
122 		hdl->mio.eof = 1;
123 		return 0;
124 	}
125 	return n;
126 }
127 
128 static size_t
129 mio_rmidi_write(struct mio_hdl *sh, const void *buf, size_t len)
130 {
131 	struct mio_rmidi_hdl *hdl = (struct mio_rmidi_hdl *)sh;
132 	ssize_t n;
133 
134 	while ((n = write(hdl->fd, buf, len)) < 0) {
135 		if (errno == EINTR)
136 			continue;
137 		if (errno != EAGAIN) {
138 			DPERROR("mio_rmidi_write: write");
139 			hdl->mio.eof = 1;
140 		}
141  		return 0;
142 	}
143 	return n;
144 }
145 
146 static int
147 mio_rmidi_nfds(struct mio_hdl *sh)
148 {
149 	return 1;
150 }
151 
152 static int
153 mio_rmidi_pollfd(struct mio_hdl *sh, struct pollfd *pfd, int events)
154 {
155 	struct mio_rmidi_hdl *hdl = (struct mio_rmidi_hdl *)sh;
156 
157 	pfd->fd = hdl->fd;
158 	pfd->events = events;
159 	return 1;
160 }
161 
162 static int
163 mio_rmidi_revents(struct mio_hdl *sh, struct pollfd *pfd)
164 {
165 	return pfd->revents;
166 }
167