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