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 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
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 error(0, "error opening %s: %s", p
, dlerror());
60 pl
= xmalloc(sizeof *pl
);
62 pl
->name
= xstrdup(name
);
68 (flags
& PLUGIN_FATAL ? fatal
: error
)(0, "cannot find plugin '%s'", name
);
72 function_t
*get_plugin_function(const struct plugin
*pl
,
76 f
= (function_t
*)dlsym(pl
->dlhandle
, symbol
);
78 fatal(0, "error looking up function '%s' in '%s': %s",
79 symbol
, pl
->name
, dlerror());
83 const void *get_plugin_object(const struct plugin
*pl
,
87 o
= dlsym(pl
->dlhandle
, symbol
);
89 fatal(0, "error looking up object '%s' in '%s': %s",
90 symbol
, pl
->name
, dlerror());
94 /* specific plugin interfaces *************************************************/
96 typedef long tracklength_fn(const char *track
, const char *path
);
98 /** Compute the length of a track
99 * @param plugin plugin to use, as configured
100 * @param track UTF-8 name of track
101 * @param path file system path or 0
102 * @return length of track in seconds, 0 for unknown, -1 for error
104 long tracklength(const char *plugin
, const char *track
, const char *path
) {
105 tracklength_fn
*f
= 0;
107 f
= (tracklength_fn
*)get_plugin_function(open_plugin(plugin
,
109 "disorder_tracklength");
110 return (*f
)(track
, path
);
113 typedef void scan_fn(const char *root
);
115 void scan(const char *module
, const char *root
) {
116 ((scan_fn
*)get_plugin_function(open_plugin(module
, PLUGIN_FATAL
),
117 "disorder_scan"))(root
);
120 typedef int check_fn(const char *root
, const char *path
);
123 int check(const char *module
, const char *root
, const char *path
) {
124 return ((check_fn
*)get_plugin_function(open_plugin(module
, PLUGIN_FATAL
),
125 "disorder_check"))(root
, path
);
128 typedef void notify_play_fn(const char *track
, const char *submitter
);
130 void notify_play(const char *track
,
131 const char *submitter
) {
132 static notify_play_fn
*f
;
135 f
= (notify_play_fn
*)get_plugin_function(open_plugin("notify",
137 "disorder_notify_play");
138 (*f
)(track
, submitter
);
141 typedef void notify_scratch_fn(const char *track
,
142 const char *submitter
,
143 const char *scratcher
,
146 void notify_scratch(const char *track
,
147 const char *submitter
,
148 const char *scratcher
,
150 static notify_scratch_fn
*f
;
153 f
= (notify_scratch_fn
*)get_plugin_function(open_plugin("notify",
155 "disorder_notify_scratch");
156 (*f
)(track
, submitter
, scratcher
, seconds
);
159 typedef void notify_not_scratched_fn(const char *track
,
160 const char *submitter
);
162 void notify_not_scratched(const char *track
,
163 const char *submitter
) {
164 static notify_not_scratched_fn
*f
;
167 f
= (notify_not_scratched_fn
*)get_plugin_function
168 (open_plugin("notify",
170 "disorder_notify_not_scratched");
171 (*f
)(track
, submitter
);
174 typedef void notify_queue_fn(const char *track
,
175 const char *submitter
);
177 void notify_queue(const char *track
,
178 const char *submitter
) {
179 static notify_queue_fn
*f
;
182 f
= (notify_queue_fn
*)get_plugin_function(open_plugin("notify",
184 "disorder_notify_queue");
185 (*f
)(track
, submitter
);
188 void notify_queue_remove(const char *track
,
189 const char *remover
) {
190 static notify_queue_fn
*f
;
193 f
= (notify_queue_fn
*)get_plugin_function(open_plugin("notify",
195 "disorder_notify_queue_remove");
196 (*f
)(track
, remover
);
199 void notify_queue_move(const char *track
,
201 static notify_queue_fn
*f
;
204 f
= (notify_queue_fn
*)get_plugin_function(open_plugin("notify",
206 "disorder_notify_queue_move");
210 void notify_pause(const char *track
, const char *who
) {
211 static notify_queue_fn
*f
;
214 f
= (notify_queue_fn
*)get_plugin_function(open_plugin("notify",
216 "disorder_notify_pause");
220 void notify_resume(const char *track
, const char *who
) {
221 static notify_queue_fn
*f
;
224 f
= (notify_queue_fn
*)get_plugin_function(open_plugin("notify",
226 "disorder_notify_resume");
230 /* player plugin interfaces ***************************************************/
234 unsigned long play_get_type(const struct plugin
*pl
) {
235 return *(const unsigned long *)get_plugin_object(pl
, "disorder_player_type");
240 typedef void *prefork_fn(const char *track
);
242 void *play_prefork(const struct plugin
*pl
,
244 return ((prefork_fn
*)get_plugin_function(pl
,
245 "disorder_play_prefork"))(track
);
250 typedef void play_track_fn(const char *const *parameters
,
255 void play_track(const struct plugin
*pl
,
256 const char *const *parameters
,
260 ((play_track_fn
*)get_plugin_function(pl
,
261 "disorder_play_track"))(parameters
,
269 typedef void cleanup_fn(void *data
);
271 void play_cleanup(const struct plugin
*pl
, void *data
) {
272 ((cleanup_fn
*)get_plugin_function(pl
, "disorder_play_cleanup"))(data
);
277 typedef int pause_fn(long *playedp
, void *data
);
279 int play_pause(const struct plugin
*pl
, long *playedp
, void *data
) {
280 return (((pause_fn
*)get_plugin_function(pl
, "disorder_pause_track"))
286 typedef void resume_fn(void *data
);
288 void play_resume(const struct plugin
*pl
, void *data
) {
289 (((resume_fn
*)get_plugin_function(pl
, "disorder_resume_track"))