summaryrefslogtreecommitdiff
path: root/mixnotify.c
blob: 734ffc08644aa37f849429ee01f3d19fd471ca62 (plain)
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
/*
 * Copyright (C) 2016 Anton Khirnov <anton@khirnov.net>
 *
 * mixnotify is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * mixnotify is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with mixnotify.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <alsa/asoundlib.h>

#include <stdio.h>
#include <string.h>

typedef struct MixerPrivate {
    snd_mixer_elem_t *elem;
} MixerPrivate;

static int elem_callback(snd_mixer_elem_t *elem, unsigned int mask)
{
    MixerPrivate *s = snd_mixer_elem_get_callback_private(elem);
    long val, min, max;
    int ret;

    if (!(mask & SND_CTL_EVENT_MASK_VALUE))
        return 0;

    ret = snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_UNKNOWN, &val);
    if (ret < 0) {
        fprintf(stderr, "Error getting element volume: %d (%s)\n");
        return ret;
    }

    ret = snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
    if (ret < 0) {
        fprintf(stderr, "Error getting element volume range: %d (%s)\n",
                ret, snd_strerror(ret));
        return ret;
    }

    fprintf(stdout, "%ld %ld %ld\n", val, min, max);
    fflush(stdout);

    return 0;
}

int main(int argc, const char **argv)
{
    static const char  *ctl_name = "default";
    static const char *elem_name = "Master";

    MixerPrivate priv = { NULL };

    snd_mixer_t *mixer = NULL;
    snd_mixer_elem_t *elem;
    int ret;

    if (argc > 1)
        ctl_name = argv[1];
    if (argc > 2)
        elem_name = argv[2];

    fprintf(stderr, "Using CTL: %s\n", ctl_name);
    fprintf(stderr, "Using mixer element: %s\n", elem_name);

    ret = snd_mixer_open(&mixer, 0);
    if (ret < 0) {
        fprintf(stderr, "Error opening a new mixer: %d (%s)\n",
                ret, snd_strerror(ret));
        return -1;
    }

    ret = snd_mixer_attach(mixer, ctl_name);
    if (ret < 0) {
        fprintf(stderr, "Error attachng a HCTL '%s' to the mixer: %d (%s)\n",
                ctl_name, ret, snd_strerror(ret));
        ret = -1;
        goto fail;
    }

    ret = snd_mixer_selem_register(mixer, NULL, NULL);
    if (ret < 0) {
        fprintf(stderr, "Error registering simple element class: %d (%s)\n",
                ctl_name, ret, snd_strerror(ret));
        ret = -1;
        goto fail;
    }

    ret = snd_mixer_load(mixer);
    if (ret < 0) {
        fprintf(stderr, "Error loading mixer elements: %d (%s)\n",
                ret, snd_strerror(ret));
        ret = -1;
        goto fail;
    }

    for (elem = snd_mixer_first_elem(mixer); elem; elem = snd_mixer_elem_next(elem)) {
        snd_mixer_elem_type_t type = snd_mixer_elem_get_type(elem);

        if (type == SND_MIXER_ELEM_SIMPLE &&
            !strcmp(snd_mixer_selem_get_name(elem), elem_name))
            break;
    }
    if (!elem) {
        fprintf(stderr, "Mixer element '%s' not found\n", elem_name);
        ret = -1;
        goto fail;
    }

    snd_mixer_elem_set_callback_private(elem, &priv);
    snd_mixer_elem_set_callback(elem, elem_callback);

    while (!(ret = snd_mixer_wait(mixer, -1))) {
        ret = snd_mixer_handle_events(mixer);
        if (ret < 0) {
            fprintf(stderr, "Error handling events: %d (%s)\n",
                    ret, snd_strerror(ret));
            ret = -1;
            goto fail;
        }
    }
    if (ret < 0) {
        fprintf(stderr, "Error waiting for mixer events: %d (%s)\n",
                ret, snd_strerror(ret));
        ret = -1;
        goto fail;
    }

fail:
    snd_mixer_close(mixer);

    return ret;
}