Commit | Line | Data |
---|---|---|
bd8895a8 | 1 | /* |
2 | * This file is part of DisOrder | |
a35bb7cc | 3 | * Copyright (C) 2004, 2007, 2008 Richard Kettlewell |
bd8895a8 | 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 | */ | |
b25aac59 | 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 | */ | |
bd8895a8 | 26 | |
27 | #include <config.h> | |
b25aac59 | 28 | |
29 | #if HAVE_SYS_SOUNDCARD_H | |
30 | ||
bd8895a8 | 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> | |
b25aac59 | 41 | #include <sys/soundcard.h> |
bd8895a8 | 42 | |
43 | #include "configuration.h" | |
44 | #include "mixer.h" | |
45 | #include "log.h" | |
46 | #include "syscalls.h" | |
a35bb7cc | 47 | #include "mem.h" |
bd8895a8 | 48 | |
bd8895a8 | 49 | /* documentation does not match implementation! */ |
50 | #ifndef SOUND_MIXER_READ | |
51 | # define SOUND_MIXER_READ(x) MIXER_READ(x) | |
52 | #endif | |
53 | #ifndef SOUND_MIXER_WRITE | |
54 | # define SOUND_MIXER_WRITE(x) MIXER_WRITE(x) | |
55 | #endif | |
56 | ||
b25aac59 | 57 | /** @brief Channel names */ |
bd8895a8 | 58 | static const char *channels[] = SOUND_DEVICE_NAMES; |
59 | ||
b25aac59 | 60 | /** @brief Convert channel name to number */ |
bd8895a8 | 61 | static int mixer_channel(const char *c) { |
62 | unsigned n; | |
63 | ||
64 | if(!c[strspn(c, "0123456789")]) | |
65 | return atoi(c); | |
66 | else { | |
67 | for(n = 0; n < sizeof channels / sizeof *channels; ++n) | |
68 | if(!strcmp(channels[n], c)) | |
69 | return n; | |
70 | return -1; | |
71 | } | |
72 | } | |
73 | ||
b25aac59 | 74 | /** @brief Open the OSS mixer device and return its fd */ |
bd8895a8 | 75 | static int oss_do_open(void) { |
76 | int fd; | |
77 | ||
a35bb7cc RK |
78 | if((fd = open(config->mixer, O_RDWR, 0)) < 0) { |
79 | static char *reported; | |
80 | ||
81 | if(!reported || strcmp(reported, config->mixer)) { | |
82 | if(reported) | |
83 | xfree(reported); | |
84 | reported = xstrdup(config->mixer); | |
85 | error(errno, "error opening %s", config->mixer); | |
86 | } | |
87 | } | |
bd8895a8 | 88 | return fd; |
89 | } | |
90 | ||
b25aac59 | 91 | /** @brief Get the OSS mixer setting */ |
bd8895a8 | 92 | static int oss_do_get(int *left, int *right, int fd, int ch) { |
93 | int r; | |
94 | ||
95 | if(ioctl(fd, SOUND_MIXER_READ(ch), &r) == -1) { | |
96 | error(errno, "error reading %s channel %s", | |
97 | config->mixer, config->channel); | |
98 | return -1; | |
99 | } | |
100 | *left = r & 0xff; | |
101 | *right = (r >> 8) & 0xff; | |
102 | return 0; | |
103 | } | |
104 | ||
b25aac59 | 105 | /** @brief Get OSS volume */ |
bd8895a8 | 106 | static int oss_get(int *left, int *right) { |
107 | int ch, fd; | |
108 | ||
109 | if(config->mixer | |
110 | && config->channel | |
111 | && (ch = mixer_channel(config->channel)) != -1) { | |
112 | if((fd = oss_do_open()) < 0) | |
113 | return -1; | |
114 | if(oss_do_get(left, right, fd, ch) < 0) { | |
115 | xclose(fd); | |
116 | return -1; | |
117 | } | |
118 | xclose(fd); | |
119 | return 0; | |
120 | } else | |
121 | return -1; | |
122 | } | |
123 | ||
b25aac59 | 124 | /** @brief Set OSS volume */ |
bd8895a8 | 125 | static int oss_set(int *left, int *right) { |
126 | int ch, fd, r; | |
127 | ||
128 | if(config->mixer | |
129 | && config->channel | |
130 | && (ch = mixer_channel(config->channel)) != -1) { | |
131 | if((fd = oss_do_open()) < 0) | |
132 | return -1; | |
133 | r = (*left & 0xff) + (*right & 0xff) * 256; | |
134 | if(ioctl(fd, SOUND_MIXER_WRITE(ch), &r) == -1) { | |
135 | error(errno, "error changing %s channel %s", | |
136 | config->mixer, config->channel); | |
137 | xclose(fd); | |
138 | return -1; | |
139 | } | |
140 | if(oss_do_get(left, right, fd, ch) < 0) { | |
141 | xclose(fd); | |
142 | return -1; | |
143 | } | |
144 | xclose(fd); | |
145 | return 0; | |
146 | } else | |
147 | return -1; | |
148 | } | |
149 | ||
b25aac59 | 150 | /** @brief OSS mixer vtable */ |
bd8895a8 | 151 | const struct mixer mixer_oss = { |
152 | BACKEND_OSS, | |
bd8895a8 | 153 | oss_get, |
154 | oss_set, | |
155 | "/dev/mixer", | |
156 | "pcm" | |
157 | }; | |
158 | #endif | |
159 | ||
160 | /* | |
161 | Local Variables: | |
162 | c-basic-offset:2 | |
163 | comment-column:40 | |
164 | End: | |
165 | */ |