File: example.c

package info (click to toggle)
mpdcron 0.3%2Bgit20161228-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 1,304 kB
  • sloc: ansic: 12,825; ruby: 587; makefile: 178; sh: 95
file content (141 lines) | stat: -rw-r--r-- 4,215 bytes parent folder | download | duplicates (4)
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
/* vim: set cino= fo=croql sw=8 ts=8 sts=0 noet ai cin fdm=syntax : */

/*
 * Copyright (c) 2009 Ali Polatel <alip@exherbo.org>
 *
 * This file is part of the mpdcron mpd client. mpdcron is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * mpdcron 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., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* mpdcron example module to print volume on stdout.
 * Compile this file like:
 * gcc -fPIC -shared \
 *     $(pkg-config --cflags --libs glib-2.0) \
 *     $(pkg-config --cflags --libs libmpdclient) \
 *     example.c -o example.so
 * Put it under MPDCRON_DIR/modules where MPDCRON_DIR is ~/.mpdcron by default.
 * Load it from your configuration file like:
 * [main]
 * modules = example
 */

/* Define MPDCRON_MODULE before including the file.
 */
#define MPDCRON_MODULE "example"
#include <mpdcron/gmodule.h>

#include <stdio.h>

#include <glib.h>
#include <mpd/client.h>

static int count;
static int last_volume;

/* Prototypes */
int init(const struct mpdcron_config *, GKeyFile *);
void destroy(void);
int run(const struct mpd_connection *, const struct mpd_status *);

/* Every module should define module which is of type struct mpdcron_module.
 * This is the initial symbol mpdcron looks for in the module.
 */
struct mpdcron_module module = {
	.name = "Example",
	.init = init,
	.destroy = destroy,
	.event_mixer = run,
};

/**
 * Initialization function.
 * @param conf mpdcron's global configuration.
 * @param fd This file descriptor points to mpdcron's configuration file.
 *           Never call g_key_file_free() on this!
 * @return On success this function should return MPDCRON_INIT_SUCCESS.
 *         On failure this function should return MPDCRON_INIT_FAILURE.
 */
int init(G_GNUC_UNUSED const struct mpdcron_config *conf, GKeyFile *fd)
{
	GError *parse_error;

	/* You may use GLib logging functions to do the logging.
	 */
	g_message("Hello from example module!");

	/* Parse configuration here. */
	parse_error = NULL;
	count = g_key_file_get_integer(fd, "example", "count", &parse_error);
	if (parse_error != NULL) {
		switch (parse_error->code) {
			case G_KEY_FILE_ERROR_GROUP_NOT_FOUND:
			case G_KEY_FILE_ERROR_KEY_NOT_FOUND:
				/* ignore */
				g_error_free(parse_error);
				break;
			default:
				g_critical("Parse error: %s", parse_error->message);
				g_error_free(parse_error);
				return MPDCRON_INIT_FAILURE;
		}
	}
	if (count <= 0)
		count = 5;

	last_volume = -1;
	return MPDCRON_INIT_SUCCESS;
}

/**
 * Destroy function.
 * This function should be used to free any allocated data and do other
 * cleaning up that the module has to make.
 */
void destroy(void)
{
	g_message("Bye from example module!");
	/* Do the cleaning up. */
}

/**
 * Mixer event function.
 * @param conn: mpd connection
 * @param status: mpd status
 * @return This function should return MPDCRON_RUN_SUCCESS on success.
 *         This function should return MPDCRON_RUN_RECONNECT to schedule
 *           a reconnection to mpd server.
 *         This function should return MPDCRON_RUN_RECONNECT_NOW to
 *           schedule a reconnection by cancelling to run any of the next
 *           modules in the queue.
 *         This function should return MPDCRON_RUN_UNLOAD when the
 *           module needs to be unloaded.
 */
int run(G_GNUC_UNUSED const struct mpd_connection *conn, const struct mpd_status *status)
{
	int volume;

	if (count-- <= 0)
		return MPDCRON_EVENT_UNLOAD;

	volume = mpd_status_get_volume(status);

	if (last_volume < 0)
		g_message("Volume set to: %d%%", volume);
	else
		g_message("Volume %s from: %d to: %d%%",
				(last_volume < volume) ? "increased" : "decreased",
				last_volume, volume);

	last_volume = volume;
	return MPDCRON_EVENT_SUCCESS;
}