read_mixer.cxx 2.19 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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/ConfigData.hxx"
28
#include "util/Error.hxx"
29
#include "Log.hxx"
30 31 32 33 34 35 36

#include <glib.h>

#include <assert.h>
#include <string.h>
#include <unistd.h>

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

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

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

53
#if !GLIB_CHECK_VERSION(2,32,0)
54
	g_thread_init(NULL);
55
#endif
56

57
	EventLoop event_loop;
58

59
	Error error;
60 61
	Mixer *mixer = mixer_new(event_loop, alsa_mixer_plugin,
				 *(AudioOutput *)nullptr,
62
				 *(MixerListener *)nullptr,
63
				 config_param(), error);
64
	if (mixer == NULL) {
65 66
		LogError(error, "mixer_new() failed");
		return EXIT_FAILURE;
67 68
	}

69
	if (!mixer_open(mixer, error)) {
70
		mixer_free(mixer);
71 72
		LogError(error, "failed to open the mixer");
		return EXIT_FAILURE;
73 74
	}

75
	volume = mixer_get_volume(mixer, error);
76 77 78 79 80 81
	mixer_close(mixer);
	mixer_free(mixer);

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

	if (volume < 0) {
82
		if (error.IsDefined()) {
83
			LogError(error, "failed to read volume");
84
		} else
85 86
			fprintf(stderr, "failed to read volume\n");
		return EXIT_FAILURE;
87 88
	}

89
	printf("%d\n", volume);
90 91
	return 0;
}