| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2004, 2007 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 18 | * USA |
| 19 | */ |
| 20 | /** @file lib/mixer-oss.c |
| 21 | * @brief OSS mixer support |
| 22 | * |
| 23 | * Mono output devices aren't explicitly supported (but may work |
| 24 | * nonetheless). |
| 25 | */ |
| 26 | |
| 27 | #include <config.h> |
| 28 | |
| 29 | #if HAVE_SYS_SOUNDCARD_H |
| 30 | |
| 31 | #include "types.h" |
| 32 | |
| 33 | #include <stdio.h> |
| 34 | #include <string.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <unistd.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <errno.h> |
| 39 | #include <stddef.h> |
| 40 | #include <sys/ioctl.h> |
| 41 | #include <sys/soundcard.h> |
| 42 | |
| 43 | #include "configuration.h" |
| 44 | #include "mixer.h" |
| 45 | #include "log.h" |
| 46 | #include "syscalls.h" |
| 47 | |
| 48 | /* documentation does not match implementation! */ |
| 49 | #ifndef SOUND_MIXER_READ |
| 50 | # define SOUND_MIXER_READ(x) MIXER_READ(x) |
| 51 | #endif |
| 52 | #ifndef SOUND_MIXER_WRITE |
| 53 | # define SOUND_MIXER_WRITE(x) MIXER_WRITE(x) |
| 54 | #endif |
| 55 | |
| 56 | /** @brief Channel names */ |
| 57 | static const char *channels[] = SOUND_DEVICE_NAMES; |
| 58 | |
| 59 | /** @brief Convert channel name to number */ |
| 60 | static int mixer_channel(const char *c) { |
| 61 | unsigned n; |
| 62 | |
| 63 | if(!c[strspn(c, "0123456789")]) |
| 64 | return atoi(c); |
| 65 | else { |
| 66 | for(n = 0; n < sizeof channels / sizeof *channels; ++n) |
| 67 | if(!strcmp(channels[n], c)) |
| 68 | return n; |
| 69 | return -1; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** @brief Open the OSS mixer device and return its fd */ |
| 74 | static int oss_do_open(void) { |
| 75 | int fd; |
| 76 | |
| 77 | if((fd = open(config->mixer, O_RDWR, 0)) < 0) |
| 78 | error(errno, "error opening %s", config->mixer); |
| 79 | return fd; |
| 80 | } |
| 81 | |
| 82 | /** @brief Get the OSS mixer setting */ |
| 83 | static int oss_do_get(int *left, int *right, int fd, int ch) { |
| 84 | int r; |
| 85 | |
| 86 | if(ioctl(fd, SOUND_MIXER_READ(ch), &r) == -1) { |
| 87 | error(errno, "error reading %s channel %s", |
| 88 | config->mixer, config->channel); |
| 89 | return -1; |
| 90 | } |
| 91 | *left = r & 0xff; |
| 92 | *right = (r >> 8) & 0xff; |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | /** @brief Get OSS volume */ |
| 97 | static int oss_get(int *left, int *right) { |
| 98 | int ch, fd; |
| 99 | |
| 100 | if(config->mixer |
| 101 | && config->channel |
| 102 | && (ch = mixer_channel(config->channel)) != -1) { |
| 103 | if((fd = oss_do_open()) < 0) |
| 104 | return -1; |
| 105 | if(oss_do_get(left, right, fd, ch) < 0) { |
| 106 | xclose(fd); |
| 107 | return -1; |
| 108 | } |
| 109 | xclose(fd); |
| 110 | return 0; |
| 111 | } else |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | /** @brief Set OSS volume */ |
| 116 | static int oss_set(int *left, int *right) { |
| 117 | int ch, fd, r; |
| 118 | |
| 119 | if(config->mixer |
| 120 | && config->channel |
| 121 | && (ch = mixer_channel(config->channel)) != -1) { |
| 122 | if((fd = oss_do_open()) < 0) |
| 123 | return -1; |
| 124 | r = (*left & 0xff) + (*right & 0xff) * 256; |
| 125 | if(ioctl(fd, SOUND_MIXER_WRITE(ch), &r) == -1) { |
| 126 | error(errno, "error changing %s channel %s", |
| 127 | config->mixer, config->channel); |
| 128 | xclose(fd); |
| 129 | return -1; |
| 130 | } |
| 131 | if(oss_do_get(left, right, fd, ch) < 0) { |
| 132 | xclose(fd); |
| 133 | return -1; |
| 134 | } |
| 135 | xclose(fd); |
| 136 | return 0; |
| 137 | } else |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | /** @brief OSS mixer vtable */ |
| 142 | const struct mixer mixer_oss = { |
| 143 | BACKEND_OSS, |
| 144 | oss_get, |
| 145 | oss_set, |
| 146 | "/dev/mixer", |
| 147 | "pcm" |
| 148 | }; |
| 149 | #endif |
| 150 | |
| 151 | /* |
| 152 | Local Variables: |
| 153 | c-basic-offset:2 |
| 154 | comment-column:40 |
| 155 | End: |
| 156 | */ |