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