chiark / gitweb /
disobedience more robust against server restart
[disorder] / lib / mixer.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2004 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
21#include <config.h>
22
23#include <stdio.h>
24#include <string.h>
25#include <fcntl.h>
26#include <unistd.h>
27#include <stdlib.h>
28#include <errno.h>
29#include <stddef.h>
30#include <sys/ioctl.h>
31
32#include "configuration.h"
33#include "mixer.h"
34#include "log.h"
35#include "syscalls.h"
36
37#if HAVE_SYS_SOUNDCARD_H
38#include <sys/soundcard.h>
39
40/* documentation does not match implementation! */
41#ifndef SOUND_MIXER_READ
42# define SOUND_MIXER_READ(x) MIXER_READ(x)
43#endif
44#ifndef SOUND_MIXER_WRITE
45# define SOUND_MIXER_WRITE(x) MIXER_WRITE(x)
46#endif
47
48static const char *channels[] = SOUND_DEVICE_NAMES;
49
50int mixer_channel(const char *c) {
51 unsigned n;
52
53 if(!c[strspn(c, "0123456789")])
54 return atoi(c);
55 else {
56 for(n = 0; n < sizeof channels / sizeof *channels; ++n)
57 if(!strcmp(channels[n], c))
58 return n;
59 return -1;
60 }
61}
62
63int mixer_control(int *left, int *right, int set) {
64 int fd, ch, r;
65
66 if(config->mixer
67 && config->channel
68 && (ch = mixer_channel(config->channel)) != -1) {
69 if((fd = open(config->mixer, O_RDWR, 0)) < 0) {
70 error(errno, "error opening %s", config->mixer);
71 return -1;
72 }
73 if(set) {
74 r = (*left & 0xff) + (*right & 0xff) * 256;
75 if(ioctl(fd, SOUND_MIXER_WRITE(ch), &r) == -1) {
76 error(errno, "error changing %s channel %s",
77 config->mixer, config->channel);
78 xclose(fd);
79 return -1;
80 }
81 }
82 if(ioctl(fd, SOUND_MIXER_READ(ch), &r) == -1) {
83 error(errno, "error reading %s channel %s",
84 config->mixer, config->channel);
85 xclose(fd);
86 return -1;
87 }
88 *left = r & 0xff;
89 *right = (r >> 8) & 0xff;
90 xclose(fd);
91 return 0;
92 } else
93 return -1;
94}
95#else
96int mixer_channel(const char attribute((unused)) *c) {
97 return 0;
98}
99
100int mixer_control(int attribute((unused)) *left,
101 int attribute((unused)) *right,
102 int attribute((unused)) set) {
103 error(0, "don't know how to set volume on this platform");
104 return -1;
105}
106#endif
107
108/*
109Local Variables:
110c-basic-offset:2
111comment-column:40
112End:
113*/