2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file server/plugin.c
19 * @brief Server plugin interface
21 #include "disorder-server.h"
25 /* generic plugin support *****************************************************/
28 # define SOSUFFIX ".so"
37 static struct plugin *plugins;
39 const struct plugin *open_plugin(const char *name,
46 for(pl = plugins; pl && strcmp(pl->name, name); pl = pl->next)
49 /* Search the plugin path */
50 for(n = 0; n <= config->plugins.n; ++n) {
51 byte_xasprintf(&p, "%s/%s" SOSUFFIX,
52 n == config->plugins.n ? pkglibdir : config->plugins.s[n],
54 if(access(p, R_OK) == 0) {
55 h = dlopen(p, RTLD_NOW);
57 disorder_error(0, "error opening %s: %s", p, dlerror());
60 pl = xmalloc(sizeof *pl);
62 pl->name = xstrdup(name);
68 (flags & PLUGIN_FATAL ? disorder_fatal : disorder_error)
69 (0, "cannot find plugin '%s'", name);
73 function_t *get_plugin_function(const struct plugin *pl,
77 f = (function_t *)dlsym(pl->dlhandle, symbol);
79 disorder_fatal(0, "error looking up function '%s' in '%s': %s",
80 symbol, pl->name, dlerror());
84 const void *get_plugin_object(const struct plugin *pl,
88 o = dlsym(pl->dlhandle, symbol);
90 disorder_fatal(0, "error looking up object '%s' in '%s': %s",
91 symbol, pl->name, dlerror());
95 /* specific plugin interfaces *************************************************/
97 typedef long tracklength_fn(const char *track, const char *path);
99 /** Compute the length of a track
100 * @param plugin plugin to use, as configured
101 * @param track UTF-8 name of track
102 * @param path file system path or 0
103 * @return length of track in seconds, 0 for unknown, -1 for error
105 long tracklength(const char *plugin, const char *track, const char *path) {
106 tracklength_fn *f = 0;
108 f = (tracklength_fn *)get_plugin_function(open_plugin(plugin,
110 "disorder_tracklength");
111 return (*f)(track, path);
114 typedef void scan_fn(const char *root);
116 void scan(const char *module, const char *root) {
117 ((scan_fn *)get_plugin_function(open_plugin(module, PLUGIN_FATAL),
118 "disorder_scan"))(root);
121 typedef int check_fn(const char *root, const char *path);
124 int check(const char *module, const char *root, const char *path) {
125 return ((check_fn *)get_plugin_function(open_plugin(module, PLUGIN_FATAL),
126 "disorder_check"))(root, path);
129 typedef void notify_play_fn(const char *track, const char *submitter);
131 void notify_play(const char *track,
132 const char *submitter) {
133 static notify_play_fn *f;
136 f = (notify_play_fn *)get_plugin_function(open_plugin("notify",
138 "disorder_notify_play");
139 (*f)(track, submitter);
142 typedef void notify_scratch_fn(const char *track,
143 const char *submitter,
144 const char *scratcher,
147 void notify_scratch(const char *track,
148 const char *submitter,
149 const char *scratcher,
151 static notify_scratch_fn *f;
154 f = (notify_scratch_fn *)get_plugin_function(open_plugin("notify",
156 "disorder_notify_scratch");
157 (*f)(track, submitter, scratcher, seconds);
160 typedef void notify_not_scratched_fn(const char *track,
161 const char *submitter);
163 void notify_not_scratched(const char *track,
164 const char *submitter) {
165 static notify_not_scratched_fn *f;
168 f = (notify_not_scratched_fn *)get_plugin_function
169 (open_plugin("notify",
171 "disorder_notify_not_scratched");
172 (*f)(track, submitter);
175 typedef void notify_queue_fn(const char *track,
176 const char *submitter);
178 void notify_queue(const char *track,
179 const char *submitter) {
180 static notify_queue_fn *f;
183 f = (notify_queue_fn *)get_plugin_function(open_plugin("notify",
185 "disorder_notify_queue");
186 (*f)(track, submitter);
189 void notify_queue_remove(const char *track,
190 const char *remover) {
191 static notify_queue_fn *f;
194 f = (notify_queue_fn *)get_plugin_function(open_plugin("notify",
196 "disorder_notify_queue_remove");
197 (*f)(track, remover);
200 void notify_queue_move(const char *track,
202 static notify_queue_fn *f;
205 f = (notify_queue_fn *)get_plugin_function(open_plugin("notify",
207 "disorder_notify_queue_move");
211 void notify_pause(const char *track, const char *who) {
212 static notify_queue_fn *f;
215 f = (notify_queue_fn *)get_plugin_function(open_plugin("notify",
217 "disorder_notify_pause");
221 void notify_resume(const char *track, const char *who) {
222 static notify_queue_fn *f;
225 f = (notify_queue_fn *)get_plugin_function(open_plugin("notify",
227 "disorder_notify_resume");
231 /* player plugin interfaces ***************************************************/
235 unsigned long play_get_type(const struct plugin *pl) {
236 return *(const unsigned long *)get_plugin_object(pl, "disorder_player_type");
241 typedef void *prefork_fn(const char *track);
243 void *play_prefork(const struct plugin *pl,
245 return ((prefork_fn *)get_plugin_function(pl,
246 "disorder_play_prefork"))(track);
251 typedef void play_track_fn(const char *const *parameters,
256 void play_track(const struct plugin *pl,
257 const char *const *parameters,
261 ((play_track_fn *)get_plugin_function(pl,
262 "disorder_play_track"))(parameters,
270 typedef void cleanup_fn(void *data);
272 void play_cleanup(const struct plugin *pl, void *data) {
273 ((cleanup_fn *)get_plugin_function(pl, "disorder_play_cleanup"))(data);
278 typedef int pause_fn(long *playedp, void *data);
280 int play_pause(const struct plugin *pl, long *playedp, void *data) {
281 return (((pause_fn *)get_plugin_function(pl, "disorder_pause_track"))
287 typedef void resume_fn(void *data);
289 void play_resume(const struct plugin *pl, void *data) {
290 (((resume_fn *)get_plugin_function(pl, "disorder_resume_track"))