bd8895a8 |
1 | /* |
2 | * This file is part of DisOrder |
3 | * Copyright (C) 2004, 2007 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 | #include "types.h" |
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 | |
49 | static const char *channels[] = SOUND_DEVICE_NAMES; |
50 | |
51 | static int 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 | |
64 | static int oss_validate_channel(const char *c) { |
65 | if(mixer_channel(c) != -1) |
66 | return 1; |
67 | else |
68 | return 0; |
69 | } |
70 | |
71 | static int oss_validate_device(const char *d) { |
72 | struct stat sb; |
73 | |
74 | if(stat(d, &sb) < 0) { |
75 | error(errno, "%s", d); |
76 | return 0; |
77 | } |
78 | if(!S_ISCHR(sb.st_mode)) { |
79 | error(0, "%s: not a character device", d); |
80 | return 0; |
81 | } |
82 | return 1; |
83 | } |
84 | |
85 | static int oss_do_open(void) { |
86 | int fd; |
87 | |
88 | if((fd = open(config->mixer, O_RDWR, 0)) < 0) |
89 | error(errno, "error opening %s", config->mixer); |
90 | return fd; |
91 | } |
92 | |
93 | static int oss_do_get(int *left, int *right, int fd, int ch) { |
94 | int r; |
95 | |
96 | if(ioctl(fd, SOUND_MIXER_READ(ch), &r) == -1) { |
97 | error(errno, "error reading %s channel %s", |
98 | config->mixer, config->channel); |
99 | return -1; |
100 | } |
101 | *left = r & 0xff; |
102 | *right = (r >> 8) & 0xff; |
103 | return 0; |
104 | } |
105 | |
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 | |
124 | static int oss_set(int *left, int *right) { |
125 | int ch, fd, r; |
126 | |
127 | if(config->mixer |
128 | && config->channel |
129 | && (ch = mixer_channel(config->channel)) != -1) { |
130 | if((fd = oss_do_open()) < 0) |
131 | return -1; |
132 | r = (*left & 0xff) + (*right & 0xff) * 256; |
133 | if(ioctl(fd, SOUND_MIXER_WRITE(ch), &r) == -1) { |
134 | error(errno, "error changing %s channel %s", |
135 | config->mixer, config->channel); |
136 | xclose(fd); |
137 | return -1; |
138 | } |
139 | if(oss_do_get(left, right, fd, ch) < 0) { |
140 | xclose(fd); |
141 | return -1; |
142 | } |
143 | xclose(fd); |
144 | return 0; |
145 | } else |
146 | return -1; |
147 | } |
148 | |
149 | const struct mixer mixer_oss = { |
150 | BACKEND_OSS, |
151 | oss_validate_device, |
152 | oss_validate_channel, |
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 | */ |