1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
/*
* Copyright 2008-2013 Various Authors
* Copyright 2004-2005 Timo Hirvonen
*
* mixer_sun.c by alex <pukpuk@gmx.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/audioio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "../debug.h"
#include "../mixer.h"
#include "../op.h"
#include "../sf.h"
#include "../xmalloc.h"
static int sun_mixer_device_id = -1;
static int sun_mixer_channels = -1;
static int sun_mixer_volume_delta = -1;
static int mixer_fd = -1;
static char *sun_mixer_device = NULL;
static char *sun_mixer_channel = NULL;
static int mixer_open(const char *);
static int min_delta(int, int, int);
static int sun_device_exists(const char *);
static int sun_mixer_init(void);
static int sun_mixer_exit(void);
static int sun_mixer_open(int *);
static int sun_mixer_close(void);
static int sun_mixer_set_volume(int, int);
static int sun_mixer_get_volume(int *, int *);
static int mixer_open(const char *dev)
{
struct mixer_devinfo minf;
int output_class;
mixer_fd = open(dev, O_RDWR);
if (mixer_fd == -1)
return -1;
output_class = -1;
sun_mixer_device_id = -1;
/* determine output class */
minf.index = 0;
while (ioctl(mixer_fd, AUDIO_MIXER_DEVINFO, &minf) != -1) {
if (minf.type == AUDIO_MIXER_CLASS) {
if (strcmp(minf.label.name, AudioCoutputs) == 0)
output_class = minf.index;
}
++minf.index;
}
/* no output class found?? something must be wrong */
if (output_class == -1)
return -1;
minf.index = 0;
/* query all mixer devices and try choose the correct one */
while (ioctl(mixer_fd, AUDIO_MIXER_DEVINFO, &minf) != -1) {
/* only scan output channels */
if (minf.type == AUDIO_MIXER_VALUE && minf.prev == AUDIO_MIXER_LAST &&
minf.mixer_class == output_class) {
if (strcasecmp(minf.label.name, sun_mixer_channel) == 0) {
sun_mixer_volume_delta = minf.un.v.delta;
sun_mixer_device_id = minf.index;
sun_mixer_channels = minf.un.v.num_channels;
}
}
++minf.index;
}
if (sun_mixer_device_id == -1)
return -1;
d_print("sun: found mixer-channel: %s, devid: %d, delta: %d, channels: %d\n", sun_mixer_channel,
sun_mixer_device_id, sun_mixer_volume_delta, sun_mixer_channels);
if (sun_mixer_volume_delta == 0)
sun_mixer_volume_delta = 1;
return 0;
}
static int min_delta(int oval, int nval, int delta)
{
if (oval > nval && oval - nval < delta)
nval -= delta;
else if (oval < nval && nval - oval < delta)
nval += delta;
nval = (nval < 0) ? 0 : nval;
nval = (nval > AUDIO_MAX_GAIN) ? AUDIO_MAX_GAIN : nval;
return nval;
}
static int sun_device_exists(const char *dev)
{
struct stat s;
if (stat(dev, &s) == 0) {
d_print("device %s exists\n", dev);
return 1;
}
d_print("device %s does not exist\n", dev);
return 0;
}
static int sun_mixer_init(void)
{
const char *mixer_dev = "/dev/mixer";
if (sun_mixer_device != NULL) {
if (sun_device_exists(sun_mixer_device))
return 0;
free(sun_mixer_device);
sun_mixer_device = NULL;
return -1;
}
if (sun_device_exists(mixer_dev)) {
sun_mixer_device = xstrdup(mixer_dev);
return 0;
}
return -1;
}
static int sun_mixer_exit(void)
{
if (sun_mixer_device != NULL) {
free(sun_mixer_device);
sun_mixer_device = NULL;
}
if (sun_mixer_channel != NULL) {
free(sun_mixer_channel);
sun_mixer_channel = NULL;
}
return 0;
}
static int sun_mixer_open(int *vol_max)
{
const char *mixer_channel = "master";
/* set default mixer channel */
if (sun_mixer_channel == NULL)
sun_mixer_channel = xstrdup(mixer_channel);
if (mixer_open(sun_mixer_device) == 0) {
*vol_max = AUDIO_MAX_GAIN;
return 0;
}
return -1;
}
static int sun_mixer_close(void)
{
if (mixer_fd != -1) {
close(mixer_fd);
mixer_fd = -1;
}
return 0;
}
static int sun_mixer_set_volume(int l, int r)
{
struct mixer_ctrl minf;
int ovall, ovalr;
if (sun_mixer_get_volume(&ovall, &ovalr) == -1)
return -1;
/* OpenBSD mixer values are `discrete' */
l = min_delta(ovall, l, sun_mixer_volume_delta);
r = min_delta(ovalr, r, sun_mixer_volume_delta);
minf.type = AUDIO_MIXER_VALUE;
minf.dev = sun_mixer_device_id;
if (sun_mixer_channels == 1)
minf.un.value.level[AUDIO_MIXER_LEVEL_MONO] = l;
else {
minf.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
minf.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
}
minf.un.value.num_channels = sun_mixer_channels;
if (ioctl(mixer_fd, AUDIO_MIXER_WRITE, &minf) == -1)
return -1;
return 0;
}
static int sun_mixer_get_volume(int *l, int *r)
{
struct mixer_ctrl minf;
minf.dev = sun_mixer_device_id;
minf.type = AUDIO_MIXER_VALUE;
minf.un.value.num_channels = sun_mixer_channels;
if (ioctl(mixer_fd, AUDIO_MIXER_READ, &minf) == -1)
return -1;
if (sun_mixer_channels == 1) {
*l = minf.un.value.level[AUDIO_MIXER_LEVEL_MONO];
*r = *l;
} else {
*l = minf.un.value.level[AUDIO_MIXER_LEVEL_LEFT];
*r = minf.un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
}
return 0;
}
static int sun_mixer_set_channel(const char *val)
{
if (sun_mixer_channel != NULL)
free(sun_mixer_channel);
sun_mixer_channel = xstrdup(val);
return 0;
}
static int sun_mixer_get_channel(char **val)
{
if (sun_mixer_channel)
*val = xstrdup(sun_mixer_channel);
return 0;
}
static int sun_mixer_set_device(const char *val)
{
free(sun_mixer_device);
sun_mixer_device = xstrdup(val);
return 0;
}
static int sun_mixer_get_device(char **val)
{
if (sun_mixer_device)
*val = xstrdup(sun_mixer_device);
return 0;
}
const struct mixer_plugin_ops op_mixer_ops = {
.init = sun_mixer_init,
.exit = sun_mixer_exit,
.open = sun_mixer_open,
.close = sun_mixer_close,
.set_volume = sun_mixer_set_volume,
.get_volume = sun_mixer_get_volume,
};
const struct mixer_plugin_opt op_mixer_options[] = {
OPT(sun_mixer, channel),
OPT(sun_mixer, device),
{ NULL },
};
|