1 /* 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2025 Goran Mekić 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/event.h> 29 30 #include "oss.h" 31 32 int 33 main(int argc, char *argv[]) 34 { 35 struct config config = { 36 .device = "/dev/dsp", 37 .mode = O_RDWR, 38 .format = AFMT_S32_NE, 39 .sample_rate = 48000, 40 }; 41 struct kevent event = {}; 42 int rc, bytes, kq; 43 44 oss_init(&config); 45 bytes = config.buffer_info.bytes; 46 47 if ((kq = kqueue()) < 0) 48 err(1, "Failed to allocate kqueue"); 49 EV_SET(&event, config.fd, EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, 0); 50 if (kevent(kq, &event, 1, NULL, 0, NULL) < 0) 51 err(1, "Failed to register kevent"); 52 for (;;) { 53 if (kevent(kq, NULL, 0, &event, 1, NULL) < 0) { 54 warn("Event error"); 55 break; 56 } 57 if (event.flags & EV_ERROR) { 58 warn("Event error: %s", strerror(event.data)); 59 break; 60 } 61 if ((rc = read(config.fd, config.buf, bytes)) < bytes) { 62 warn("Requested %d bytes, but read %d!\n", bytes, rc); 63 break; 64 } 65 if ((rc = write(config.fd, config.buf, bytes)) < bytes) { 66 warn("Requested %d bytes, but wrote %d!\n", bytes, rc); 67 break; 68 } 69 } 70 EV_SET(&event, config.fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0); 71 if (kevent(kq, &event, 1, NULL, 0, NULL) < 0) 72 err(1, "Failed to unregister kevent"); 73 close(kq); 74 75 free(config.buf); 76 close(config.fd); 77 78 return (0); 79 } 80