Annotation of src/usr.bin/aucat/miofile.c, Revision 1.6
1.6 ! ratchov 1: /* $OpenBSD: miofile.c,v 1.5 2010/06/04 06:15:28 ratchov Exp $ */
1.1 ratchov 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/time.h>
20:
21: #include <poll.h>
22: #include <stdio.h>
23: #include <stdlib.h>
24: #include <string.h>
25: #include <sndio.h>
26:
27: #include "conf.h"
28: #include "file.h"
29: #include "miofile.h"
1.4 ratchov 30: #ifdef DEBUG
31: #include "dbg.h"
32: #endif
1.1 ratchov 33:
34: struct miofile {
35: struct file file;
36: struct mio_hdl *hdl;
37: };
38:
39: void miofile_close(struct file *);
40: unsigned miofile_read(struct file *, unsigned char *, unsigned);
41: unsigned miofile_write(struct file *, unsigned char *, unsigned);
42: void miofile_start(struct file *);
43: void miofile_stop(struct file *);
44: int miofile_nfds(struct file *);
45: int miofile_pollfd(struct file *, struct pollfd *, int);
46: int miofile_revents(struct file *, struct pollfd *);
47:
48: struct fileops miofile_ops = {
49: "mio",
50: sizeof(struct miofile),
51: miofile_close,
52: miofile_read,
53: miofile_write,
54: NULL, /* start */
55: NULL, /* stop */
56: miofile_nfds,
57: miofile_pollfd,
58: miofile_revents
59: };
60:
61: /*
62: * open the device
63: */
64: struct miofile *
1.6 ! ratchov 65: miofile_new(struct fileops *ops, char *path, unsigned mode)
1.1 ratchov 66: {
1.5 ratchov 67: char *siopath;
1.1 ratchov 68: struct mio_hdl *hdl;
69: struct miofile *f;
70:
1.5 ratchov 71: siopath = (strcmp(path, "default") == 0) ? NULL : path;
72: hdl = mio_open(siopath, mode, 1);
1.1 ratchov 73: if (hdl == NULL)
74: return NULL;
1.3 ratchov 75: f = (struct miofile *)file_new(ops, path, mio_nfds(hdl));
1.1 ratchov 76: if (f == NULL)
77: goto bad_close;
78: f->hdl = hdl;
79: return f;
80: bad_close:
81: mio_close(hdl);
82: return NULL;
83: }
84:
85: unsigned
86: miofile_read(struct file *file, unsigned char *data, unsigned count)
87: {
88: struct miofile *f = (struct miofile *)file;
89: unsigned n;
90:
91: n = mio_read(f->hdl, data, count);
92: if (n == 0) {
93: f->file.state &= ~FILE_ROK;
94: if (mio_eof(f->hdl)) {
1.4 ratchov 95: #ifdef DEBUG
96: dbg_puts(f->file.name);
97: dbg_puts(": failed to read from device\n");
98: #endif
1.1 ratchov 99: file_eof(&f->file);
100: } else {
1.4 ratchov 101: #ifdef DEBUG
102: if (debug_level >= 4) {
103: file_dbg(&f->file);
104: dbg_puts(": reading blocked\n");
105: }
106: #endif
1.1 ratchov 107: }
108: return 0;
109: }
110: return n;
111:
112: }
113:
114: unsigned
115: miofile_write(struct file *file, unsigned char *data, unsigned count)
116: {
117: struct miofile *f = (struct miofile *)file;
118: unsigned n;
119:
120: n = mio_write(f->hdl, data, count);
121: if (n == 0) {
122: f->file.state &= ~FILE_WOK;
123: if (mio_eof(f->hdl)) {
1.4 ratchov 124: #ifdef DEBUG
125: dbg_puts(f->file.name);
126: dbg_puts(": failed to write on device\n");
127: #endif
1.1 ratchov 128: file_hup(&f->file);
129: } else {
1.4 ratchov 130: #ifdef DEBUG
131: if (debug_level >= 4) {
132: file_dbg(&f->file);
133: dbg_puts(": writing blocked\n");
134: }
135: #endif
1.1 ratchov 136: }
137: return 0;
138: }
139: return n;
140: }
141:
142: int
143: miofile_nfds(struct file *file)
144: {
145: return mio_nfds(((struct miofile *)file)->hdl);
146: }
147:
148: int
149: miofile_pollfd(struct file *file, struct pollfd *pfd, int events)
150: {
151: return mio_pollfd(((struct miofile *)file)->hdl, pfd, events);
152: }
153:
154: int
155: miofile_revents(struct file *file, struct pollfd *pfd)
156: {
157: return mio_revents(((struct miofile *)file)->hdl, pfd);
158: }
159:
160: void
161: miofile_close(struct file *file)
162: {
163: return mio_close(((struct miofile *)file)->hdl);
164: }