-
Notifications
You must be signed in to change notification settings - Fork 11
/
volume.c
184 lines (159 loc) · 4.02 KB
/
volume.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
/* See LICENSE file for copyright and license details. */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/audioio.h>
#include <xcb/xcb.h>
#include "bspwmbar.h"
#include "util.h"
#define check_device(dinfo) \
(((dinfo).type == AUDIO_MIXER_VALUE && \
!strncmp((dinfo).label.name, AudioNmaster, MAX_AUDIO_DEV_LEN)) || \
((dinfo).type == AUDIO_MIXER_ENUM && \
!strncmp((dinfo).label.name, AudioNmute, MAX_AUDIO_DEV_LEN)))
/* functions */
static int get_volume(int);
static int is_muted(int);
static void init_devinfo(int);
static void toggle_mute(int);
static void set_volume(int, int);
static char *file = NULL;
static mixer_ctrl_t vctrl = { 0 };
static mixer_ctrl_t mctrl = { 0 };
static int initialized = 0;
int
get_volume(int fd)
{
if (ioctl(fd, AUDIO_MIXER_READ, &vctrl) < 0) {
return 0;
}
return vctrl.un.value.num_channels == 1 ?
vctrl.un.value.level[AUDIO_MIXER_LEVEL_MONO] :
(vctrl.un.value.level[AUDIO_MIXER_LEVEL_LEFT] >
vctrl.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] ?
vctrl.un.value.level[AUDIO_MIXER_LEVEL_LEFT] :
vctrl.un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
}
int
is_muted(int fd)
{
if (ioctl(fd, AUDIO_MIXER_READ, &mctrl) < 0) {
return 0;
}
return mctrl.un.ord;
}
void
init_devinfo(int fd)
{
mixer_devinfo_t dinfo;
mixer_ctrl_t ctrl;
int cls = -1;
for (dinfo.index = 0; cls == -1; dinfo.index++) {
if (ioctl(fd, AUDIO_MIXER_DEVINFO, &dinfo)) {
close(fd);
die("volume: failed to get device info\n");
}
if (dinfo.type == AUDIO_MIXER_CLASS &&
!strncmp(dinfo.label.name, AudioCoutputs, MAX_AUDIO_DEV_LEN))
cls = dinfo.index;
}
int vol = 1, mute = 1;
for (dinfo.index = 0; vol || mute; dinfo.index++) {
if (ioctl(fd, AUDIO_MIXER_DEVINFO, &dinfo) < 0) {
close(fd);
die("volume: failed to get device info\n");
}
if (dinfo.mixer_class == cls && check_device(dinfo)) {
ctrl.dev = dinfo.index;
ctrl.type = dinfo.type;
ctrl.un.value.num_channels = 2;
if (ioctl(fd, AUDIO_MIXER_READ, &ctrl) < 0) {
ctrl.un.value.num_channels = 1;
if (ioctl(fd, AUDIO_MIXER_READ, &ctrl) < 0) {
close(fd);
die("volume: failed to get mixer info\n");
}
}
if (ctrl.type == AUDIO_MIXER_VALUE) {
vctrl = ctrl;
vol = 0;
} else if (ctrl.type == AUDIO_MIXER_ENUM) {
mctrl = ctrl;
mute = 0;
}
}
}
initialized = 1;
}
void
volume(draw_context_t *dc, module_option_t *opts)
{
if (!file) {
if ((file = getenv("MIXERDEVICE")) == 0 || *file == '\0')
file = "/dev/mixer";
}
int fd;
if ((fd = open(file, O_RDONLY)) < 0)
die("volume: failed to open %s\n", file);
if (!initialized)
init_devinfo(fd);
if (!opts->vol.prefix)
opts->vol.prefix = "";
if (!opts->vol.suffix)
opts->vol.suffix = "";
const char *mark = is_muted(fd) ? opts->vol.muted : opts->vol.unmuted;
sprintf(buf, "%s%s %d%s", opts->vol.prefix, mark, get_volume(fd) * 100 / 255,
opts->vol.suffix);
draw_text(dc, buf);
close(fd);
}
void
toggle_mute(int fd)
{
if (is_muted(fd))
mctrl.un.ord = 0;
else
mctrl.un.ord = 1;
ioctl(fd, AUDIO_MIXER_WRITE, &mctrl);
}
void
set_volume(int fd, int vol)
{
if (vctrl.un.value.num_channels == 1) {
vctrl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = vol;
} else {
vctrl.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = vol;
vctrl.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = vol;
}
ioctl(fd, AUDIO_MIXER_WRITE, &vctrl);
}
void
volume_ev(xcb_generic_event_t *ev, module_option_t *unused)
{
xcb_button_press_event_t *button;
int fd, vol;
(void)unused;
if ((fd = open(file, O_RDWR)) < 0)
die("volume: failed to open %s\n", file);
vol = get_volume(fd);
switch (ev->response_type & ~0x80) {
case XCB_BUTTON_PRESS:
button = (xcb_button_press_event_t *)ev;
switch (button->detail) {
case XCB_BUTTON_INDEX_1:
toggle_mute(fd);
break;
case XCB_BUTTON_INDEX_4:
set_volume(fd, SMALLER(vol + 12, 255));
break;
case XCB_BUTTON_INDEX_5:
set_volume(fd, BIGGER(vol - 12, 0));
break;
}
break;
}
close(fd);
}