-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsetvolume.c
71 lines (58 loc) · 1.77 KB
/
setvolume.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
// set-volume is a small utility for ALSA mixer volume
// It's based on get-volume, written for being used in Conky.
//
// This code is in Public Domain
//
// Reference:
// http://www.alsa-project.org/alsa-doc/alsa-lib/index.html
//
// Author:
// 2009 Yu-Jie Lin
// 2014 Maarten ter Huurne
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
static void error_close_exit(char *errmsg, int err, snd_mixer_t *h_mixer)
{
if (err == 0)
fprintf(stderr, errmsg);
else
fprintf(stderr, errmsg, snd_strerror(err));
if (h_mixer != NULL)
snd_mixer_close(h_mixer);
exit(EXIT_FAILURE);
}
int main(int argc, char** argv)
{
int err;
long vol;
snd_mixer_t *h_mixer;
snd_mixer_selem_id_t *sid;
snd_mixer_elem_t *elem ;
char *device;
char *selem_name;
if (argc < 4) {
printf("Usage:\n\t%s device control volume\n", argv[0]);
return 0;
}
device = argv[1];
selem_name = argv[2];
vol = atol(argv[3]);
if ((err = snd_mixer_open(&h_mixer, 1)) < 0)
error_close_exit("Mixer open error: %s\n", err, NULL);
if ((err = snd_mixer_attach(h_mixer, device)) < 0)
error_close_exit("Mixer attach error: %s\n", err, h_mixer);
if ((err = snd_mixer_selem_register(h_mixer, NULL, NULL)) < 0)
error_close_exit("Mixer simple element register error: %s\n", err, h_mixer);
if ((err = snd_mixer_load(h_mixer)) < 0)
error_close_exit("Mixer load error: %s\n", err, h_mixer);
snd_mixer_selem_id_alloca(&sid);
snd_mixer_selem_id_set_index(sid, 0);
snd_mixer_selem_id_set_name(sid, selem_name);
if ((elem = snd_mixer_find_selem(h_mixer, sid)) == NULL)
error_close_exit("Cannot find simple element\n", 0, h_mixer);
if ((err = snd_mixer_selem_set_playback_volume_all(elem, vol)) < 0)
error_close_exit("Volume set error: %s\n", err, h_mixer);
snd_mixer_close(h_mixer);
return 0;
}