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/>.
19 #include "disorder-server.h"
23 /* generic plugin support *****************************************************/
26 # define SOSUFFIX ".so"
35 static struct plugin *plugins;
37 const struct plugin *open_plugin(const char *name,
44 for(pl = plugins; pl && strcmp(pl->name, name); pl = pl->next)
47 /* Search the plugin path */
48 for(n = 0; n <= config->plugins.n; ++n) {
49 byte_xasprintf(&p, "%s/%s" SOSUFFIX,
50 n == config->plugins.n ? pkglibdir : config->plugins.s[n],
52 if(access(p, R_OK) == 0) {
53 h = dlopen(p, RTLD_NOW);
55 error(0, "error opening %s: %s", p, dlerror());
58 pl = xmalloc(sizeof *pl);
60 pl->name = xstrdup(name);
66 (flags & PLUGIN_FATAL ? fatal : error)(0, "cannot find plugin '%s'", name);
70 function_t *get_plugin_function(const struct plugin *pl,
74 f = (function_t *)dlsym(pl->dlhandle, symbol);
76 fatal(0, "error looking up function '%s' in '%s': %s",
77 symbol, pl->name, dlerror());
81 const void *get_plugin_object(const struct plugin *pl,
85 o = dlsym(pl->dlhandle, symbol);
87 fatal(0, "error looking up object '%s' in '%s': %s",
88 symbol, pl->name, dlerror());
92 /* specific plugin interfaces *************************************************/
94 typedef long tracklength_fn(const char *track, const char *path);
96 /** Compute the length of a track
97 * @param plugin plugin to use, as configured
98 * @param track UTF-8 name of track
99 * @param path file system path or 0
100 * @return length of track in seconds, 0 for unknown, -1 for error
102 long tracklength(const char *plugin, const char *track, const char *path) {
103 tracklength_fn *f = 0;
105 f = (tracklength_fn *)get_plugin_function(open_plugin(plugin,
107 "disorder_tracklength");
108 return (*f)(track, path);
111 typedef void scan_fn(const char *root);
113 void scan(const char *module, const char *root) {
114 ((scan_fn *)get_plugin_function(open_plugin(module, PLUGIN_FATAL),
115 "disorder_scan"))(root);
118 typedef int check_fn(const char *root, const char *path);
121 int check(const char *module, const char *root, const char *path) {
122 return ((check_fn *)get_plugin_function(open_plugin(module, PLUGIN_FATAL),
123 "disorder_check"))(root, path);
126 typedef void notify_play_fn(const char *track, const char *submitter);
128 void notify_play(const char *track,
129 const char *submitter) {
130 static notify_play_fn *f;
133 f = (notify_play_fn *)get_plugin_function(open_plugin("notify",
135 "disorder_notify_play");
136 (*f)(track, submitter);
139 typedef void notify_scratch_fn(const char *track,
140 const char *submitter,
141 const char *scratcher,
144 void notify_scratch(const char *track,
145 const char *submitter,
146 const char *scratcher,
148 static notify_scratch_fn *f;
151 f = (notify_scratch_fn *)get_plugin_function(open_plugin("notify",
153 "disorder_notify_scratch");
154 (*f)(track, submitter, scratcher, seconds);
157 typedef void notify_not_scratched_fn(const char *track,
158 const char *submitter);
160 void notify_not_scratched(const char *track,
161 const char *submitter) {
162 static notify_not_scratched_fn *f;
165 f = (notify_not_scratched_fn *)get_plugin_function
166 (open_plugin("notify",
168 "disorder_notify_not_scratched");
169 (*f)(track, submitter);
172 typedef void notify_queue_fn(const char *track,
173 const char *submitter);
175 void notify_queue(const char *track,
176 const char *submitter) {
177 static notify_queue_fn *f;
180 f = (notify_queue_fn *)get_plugin_function(open_plugin("notify",
182 "disorder_notify_queue");
183 (*f)(track, submitter);
186 void notify_queue_remove(const char *track,
187 const char *remover) {
188 static notify_queue_fn *f;
191 f = (notify_queue_fn *)get_plugin_function(open_plugin("notify",
193 "disorder_notify_queue_remove");
194 (*f)(track, remover);
197 void notify_queue_move(const char *track,
199 static notify_queue_fn *f;
202 f = (notify_queue_fn *)get_plugin_function(open_plugin("notify",
204 "disorder_notify_queue_move");
208 void notify_pause(const char *track, const char *who) {
209 static notify_queue_fn *f;
212 f = (notify_queue_fn *)get_plugin_function(open_plugin("notify",
214 "disorder_notify_pause");
218 void notify_resume(const char *track, const char *who) {
219 static notify_queue_fn *f;
222 f = (notify_queue_fn *)get_plugin_function(open_plugin("notify",
224 "disorder_notify_resume");
228 /* player plugin interfaces ***************************************************/
232 unsigned long play_get_type(const struct plugin *pl) {
233 return *(const unsigned long *)get_plugin_object(pl, "disorder_player_type");
238 typedef void *prefork_fn(const char *track);
240 void *play_prefork(const struct plugin *pl,
242 return ((prefork_fn *)get_plugin_function(pl,
243 "disorder_play_prefork"))(track);
248 typedef void play_track_fn(const char *const *parameters,
253 void play_track(const struct plugin *pl,
254 const char *const *parameters,
258 ((play_track_fn *)get_plugin_function(pl,
259 "disorder_play_track"))(parameters,
267 typedef void cleanup_fn(void *data);
269 void play_cleanup(const struct plugin *pl, void *data) {
270 ((cleanup_fn *)get_plugin_function(pl, "disorder_play_cleanup"))(data);
275 typedef int pause_fn(long *playedp, void *data);
277 int play_pause(const struct plugin *pl, long *playedp, void *data) {
278 return (((pause_fn *)get_plugin_function(pl, "disorder_pause_track"))
284 typedef void resume_fn(void *data);
286 void play_resume(const struct plugin *pl, void *data) {
287 (((resume_fn *)get_plugin_function(pl, "disorder_resume_track"))