-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathy50-subwoofer.c
317 lines (237 loc) · 7.41 KB
/
y50-subwoofer.c
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Utility for managing Lenovo Y50 subwoofer (based on hda-verb)
* Version 0.1.4
*
* Copyright (c) 2016 Pavel Artsishevsky <[email protected]>
* Copyright (c) 2008 Takashi Iwai <[email protected]>
*
* Licensed under GPL v2 or later.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <poll.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <alsa/asoundlib.h>
#include <alsa/control.h>
#include "hda_hwdep.h"
// Verb types used to setup LFE output
#define AC_VERB_SET_AMP_GAIN_MUTE 0x300
#define AC_VERB_SET_POWER_STATE 0x705
#define AC_VERB_SET_PIN_WIDGET_CONTROL 0x707
// Y50-specific defines
#define Y50_CTL_NAME "hw:1"
#define Y50_HWDEP_DEVICE "/dev/snd/hwC1D0"
#define Y50_LFE_VOLUME_MAX 85
// ALSA control names
#define MASTER_VOLUME_NAME "Master Playback Volume"
#define HEADPHONE_SWITCH_NAME "Headphone Playback Switch"
// Global plugged flag
static int headphones_plugged = 0;
// Done flag
static volatile sig_atomic_t doneflag = 0;
static int open_ctl(const char *name, snd_ctl_t **ctlp)
{
snd_ctl_t *ctl;
if (snd_ctl_open(&ctl, name, SND_CTL_READONLY) < 0) {
fprintf(stderr, "Cannot open ctl %s\n", name);
return 1;
}
if (snd_ctl_subscribe_events(ctl, 1) < 0) {
fprintf(stderr, "Cannot open subscribe events to ctl %s\n", name);
snd_ctl_close(ctl);
return 1;
}
*ctlp = ctl;
return 0;
}
static int open_hwdep(const char* name, int* pfd) {
int fd = open(name, O_RDWR);
if (fd < 0) {
perror("open");
fprintf(stderr, "Cannot open hwdep device %s\n", name);
return 1;
}
int version = 0;
if (ioctl(fd, HDA_IOCTL_PVERSION, &version) < 0) {
perror("ioctl(PVERSION)");
fprintf(stderr, "Looks like an invalid hwdep device...\n");
close(fd);
return 1;
}
if (version < HDA_HWDEP_VERSION) {
fprintf(stderr, "Invalid version number 0x%x\n", version);
fprintf(stderr, "Looks like an invalid hwdep device...\n");
close(fd);
return 1;
}
*pfd = fd;
return 0;
}
static void set_headphones_plugged(int* plugged)
{
snd_mixer_t *handle;
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle, Y50_CTL_NAME);
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
snd_mixer_selem_id_t *headphone_sid;
const char *headphone_selem_name = "Headphone";
snd_mixer_selem_id_alloca(&headphone_sid);
snd_mixer_selem_id_set_index(headphone_sid, 0);
snd_mixer_selem_id_set_name(headphone_sid, headphone_selem_name);
snd_mixer_elem_t *elem = snd_mixer_find_selem(handle, headphone_sid);
snd_mixer_selem_get_playback_switch(elem, SND_MIXER_SCHN_MONO, plugged);
snd_mixer_close(handle);
}
static long get_master_volume()
{
snd_mixer_t *handle;
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle, Y50_CTL_NAME);
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
snd_mixer_selem_id_t *master_sid;
const char *master_selem_name = "Master";
snd_mixer_selem_id_alloca(&master_sid);
snd_mixer_selem_id_set_index(master_sid, 0);
snd_mixer_selem_id_set_name(master_sid, master_selem_name);
snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, master_sid);
long min, max;
snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
long volume;
snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_MONO, &volume);
snd_mixer_close(handle);
return volume * 100 / max;
}
static void set_lfe_volume(int fd, long volume)
{
char lfe_volume = Y50_LFE_VOLUME_MAX * volume / 100;
struct hda_verb_ioctl val = { 0 };
const int verbs[] = {
HDA_VERB(0x03, AC_VERB_SET_AMP_GAIN_MUTE,
(lfe_volume > 0) ? (0xa0 << 8) | lfe_volume : 0),
HDA_VERB(0x03, AC_VERB_SET_AMP_GAIN_MUTE,
(lfe_volume > 0) ? (0x90 << 8) | lfe_volume : 0),
HDA_VERB(0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL,
(lfe_volume > 0) ? 0x25 : 0),
0
};
for (int i = 0; verbs[i] > 0; ++i) {
val.verb = verbs[i];
if (ioctl(fd, HDA_IOCTL_VERB_WRITE, &val) < 0) {
perror("ioctl");
return;
}
}
}
static void lfe_initialize(int fd)
{
struct hda_verb_ioctl val = { 0 };
const int verbs_init[] = {
HDA_VERB(0x17, AC_VERB_SET_POWER_STATE, 0),
HDA_VERB(0x1a, AC_VERB_SET_POWER_STATE, 0),
HDA_VERB(0x17, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000),
HDA_VERB(0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40),
0
};
for (int i = 0; verbs_init[i] > 0; ++i) {
val.verb = verbs_init[i];
if (ioctl(fd, HDA_IOCTL_VERB_WRITE, &val) < 0) {
perror("ioctl");
return;
}
}
}
static int handle_event(int hwdep_fd, snd_ctl_t *ctl)
{
snd_ctl_event_t *event;
unsigned int mask;
int err;
snd_ctl_event_alloca(&event);
err = snd_ctl_read(ctl, event);
if (err < 0) {
fprintf(stderr, "Cannot read event from ctl %s\n", snd_ctl_name(ctl));
return err;
}
if (snd_ctl_event_get_type(event) != SND_CTL_EVENT_ELEM) {
return 0;
}
// Filter events
mask = snd_ctl_event_elem_get_mask(event);
if (mask & SND_CTL_EVENT_MASK_VALUE) {
const char *event_name = snd_ctl_event_elem_get_name(event);
if (!strcmp(event_name, HEADPHONE_SWITCH_NAME)) {
// If headphones were plugged, mute LFE
set_headphones_plugged(&headphones_plugged);
if (headphones_plugged) {
set_lfe_volume(hwdep_fd, 0);
}
} else if (!headphones_plugged && !strcmp(event_name, MASTER_VOLUME_NAME)) {
// If headphones are unplugged and master volume changed, adjust LFE volume
set_lfe_volume(hwdep_fd, get_master_volume());
}
}
return 0;
}
void handle_signal(int signo)
{
switch (signo) {
case SIGINT:
case SIGHUP:
case SIGTERM:
doneflag = 1;
default:
break;
}
}
int main(int argc, char **argv)
{
int hwdep_fd;
snd_ctl_t *ctl;
(void) argc;
(void) argv;
// Setup the signal handler
struct sigaction sa = { 0 };
sa.sa_handler = &handle_signal;
// Handle standard signals to quit properly
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
// Open control and hwdep devices
if (open_hwdep(Y50_HWDEP_DEVICE, &hwdep_fd) != 0
|| open_ctl(Y50_CTL_NAME, &ctl) != 0) {
return 1;
}
// Initialize subwoofer
lfe_initialize(hwdep_fd);
// Set initial values
set_headphones_plugged(&headphones_plugged);
set_lfe_volume(hwdep_fd, headphones_plugged ? 0 : get_master_volume());
// Poll events
while (!doneflag) {
struct pollfd fds;
snd_ctl_poll_descriptors(ctl, &fds, 1);
if (poll(&fds, 1, -1) <= 0) {
if (errno == EINTR) {
continue;
} else {
perror("poll");
break;
}
}
unsigned short revents;
snd_ctl_poll_descriptors_revents(ctl, &fds, 1, &revents);
if (revents & POLLIN) {
handle_event(hwdep_fd, ctl);
}
}
// Close resources
close(hwdep_fd);
snd_ctl_close(ctl);
return EXIT_SUCCESS;
}