read_mixer.cxx 2.12 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21 22
#include "mixer/MixerControl.hxx"
#include "mixer/MixerList.hxx"
23
#include "filter/FilterRegistry.hxx"
24
#include "pcm/Volume.hxx"
25 26
#include "Main.hxx"
#include "event/Loop.hxx"
27
#include "config/Block.hxx"
28
#include "util/Error.hxx"
29
#include "Log.hxx"
30 31 32 33

#include <assert.h>
#include <string.h>
#include <unistd.h>
34
#include <stdlib.h>
35

36
const struct filter_plugin *
37
filter_plugin_by_name(gcc_unused const char *name)
38 39 40 41 42
{
	assert(false);
	return NULL;
}

43
int main(int argc, gcc_unused char **argv)
44 45 46 47
{
	int volume;

	if (argc != 2) {
48 49
		fprintf(stderr, "Usage: read_mixer PLUGIN\n");
		return EXIT_FAILURE;
50 51
	}

52
	EventLoop event_loop;
53

54
	Error error;
55 56
	Mixer *mixer = mixer_new(event_loop, alsa_mixer_plugin,
				 *(AudioOutput *)nullptr,
57
				 *(MixerListener *)nullptr,
58
				 ConfigBlock(), error);
59
	if (mixer == NULL) {
60 61
		LogError(error, "mixer_new() failed");
		return EXIT_FAILURE;
62 63
	}

64
	if (!mixer_open(mixer, error)) {
65
		mixer_free(mixer);
66 67
		LogError(error, "failed to open the mixer");
		return EXIT_FAILURE;
68 69
	}

70
	volume = mixer_get_volume(mixer, error);
71 72 73 74 75 76
	mixer_close(mixer);
	mixer_free(mixer);

	assert(volume >= -1 && volume <= 100);

	if (volume < 0) {
77
		if (error.IsDefined()) {
78
			LogError(error, "failed to read volume");
79
		} else
80 81
			fprintf(stderr, "failed to read volume\n");
		return EXIT_FAILURE;
82 83
	}

84
	printf("%d\n", volume);
85 86
	return 0;
}