1
0
mirror of https://github.com/VDR4Arch/vdr.git synced 2023-10-10 13:36:52 +02:00
vdr/plugin.c

433 lines
10 KiB
C
Raw Normal View History

2002-05-09 16:26:56 +02:00
/*
* plugin.c: The VDR plugin interface
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
* $Id: plugin.c 1.17 2006/02/28 14:16:54 kls Exp $
2002-05-09 16:26:56 +02:00
*/
#include "plugin.h"
#include <ctype.h>
#include <dirent.h>
#include <dlfcn.h>
2002-08-11 10:47:11 +02:00
#include <stdlib.h>
#include <time.h>
2002-05-09 16:26:56 +02:00
#include "config.h"
#define LIBVDR_PREFIX "libvdr-"
#define SO_INDICATOR ".so."
#define MAXPLUGINARGS 1024
#define HOUSEKEEPINGDELTA 10 // seconds
2002-05-09 16:26:56 +02:00
// --- cPlugin ---------------------------------------------------------------
char *cPlugin::configDirectory = NULL;
2006-01-08 11:44:37 +01:00
cPlugin::cPlugin(void)
2002-05-09 16:26:56 +02:00
{
name = NULL;
2005-01-30 14:23:01 +01:00
started = false;
2002-05-09 16:26:56 +02:00
}
cPlugin::~cPlugin()
{
I18nRegister(NULL, Name());
}
void cPlugin::SetName(const char *s)
{
name = s;
}
const char *cPlugin::CommandLineHelp(void)
{
return NULL;
}
bool cPlugin::ProcessArgs(int argc, char *argv[])
{
return true;
}
2003-05-09 15:27:46 +02:00
bool cPlugin::Initialize(void)
{
return true;
}
bool cPlugin::Start(void)
2002-05-09 16:26:56 +02:00
{
return true;
2002-05-09 16:26:56 +02:00
}
2005-01-30 14:23:01 +01:00
void cPlugin::Stop(void)
{
}
void cPlugin::Housekeeping(void)
{
}
2002-05-09 16:26:56 +02:00
const char *cPlugin::MainMenuEntry(void)
{
return NULL;
}
2002-11-24 10:45:39 +01:00
cOsdObject *cPlugin::MainMenuAction(void)
2002-05-09 16:26:56 +02:00
{
return NULL;
}
cMenuSetupPage *cPlugin::SetupMenu(void)
{
return NULL;
}
bool cPlugin::SetupParse(const char *Name, const char *Value)
{
return false;
}
void cPlugin::SetupStore(const char *Name, const char *Value)
{
Setup.Store(Name, Value, this->Name());
}
void cPlugin::SetupStore(const char *Name, int Value)
{
Setup.Store(Name, Value, this->Name());
}
bool cPlugin::Service(const char *Id, void *Data)
{
return false;
}
2005-08-27 16:42:28 +02:00
const char **cPlugin::SVDRPHelpPages(void)
{
return NULL;
}
cString cPlugin::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
{
return NULL;
}
2002-05-09 16:26:56 +02:00
void cPlugin::RegisterI18n(const tI18nPhrase * const Phrases)
{
I18nRegister(Phrases, Name());
}
void cPlugin::SetConfigDirectory(const char *Dir)
{
configDirectory = strdup(Dir);
}
const char *cPlugin::ConfigDirectory(const char *PluginName)
{
static char *buffer = NULL;
free(buffer);
asprintf(&buffer, "%s/plugins%s%s", configDirectory, PluginName ? "/" : "", PluginName ? PluginName : "");
return MakeDirs(buffer, true) ? buffer : NULL;
}
2002-05-09 16:26:56 +02:00
// --- cDll ------------------------------------------------------------------
cDll::cDll(const char *FileName, const char *Args)
{
fileName = strdup(FileName);
args = Args ? strdup(Args) : NULL;
handle = NULL;
plugin = NULL;
}
cDll::~cDll()
{
delete plugin;
if (handle)
dlclose(handle);
free(args);
free(fileName);
2002-05-09 16:26:56 +02:00
}
static char *SkipQuote(char *s)
{
char c = *s;
strcpy(s, s + 1);
while (*s && *s != c) {
if (*s == '\\')
strcpy(s, s + 1);
if (*s)
s++;
}
if (*s) {
strcpy(s, s + 1);
return s;
}
2002-05-13 16:35:49 +02:00
esyslog("ERROR: missing closing %c", c);
2002-05-09 16:26:56 +02:00
fprintf(stderr, "vdr: missing closing %c\n", c);
return NULL;
}
bool cDll::Load(bool Log)
{
if (Log)
2002-05-13 16:35:49 +02:00
isyslog("loading plugin: %s", fileName);
2002-05-09 16:26:56 +02:00
if (handle) {
2002-05-13 16:35:49 +02:00
esyslog("attempt to load plugin '%s' twice!", fileName);
2002-05-09 16:26:56 +02:00
return false;
}
handle = dlopen(fileName, RTLD_NOW);
const char *error = dlerror();
if (!error) {
void *(*creator)(void);
2004-05-22 11:29:52 +02:00
creator = (void *(*)(void))dlsym(handle, "VDRPluginCreator");
2002-05-09 16:26:56 +02:00
if (!(error = dlerror()))
plugin = (cPlugin *)creator();
}
if (!error) {
if (plugin && args) {
int argc = 0;
char *argv[MAXPLUGINARGS];
char *p = skipspace(stripspace(args));
2002-05-09 16:26:56 +02:00
char *q = NULL;
bool done = false;
while (!done) {
if (!q)
q = p;
switch (*p) {
case '\\': strcpy(p, p + 1);
if (*p)
p++;
else {
2002-05-13 16:35:49 +02:00
esyslog("ERROR: missing character after \\");
2002-05-09 16:26:56 +02:00
fprintf(stderr, "vdr: missing character after \\\n");
return false;
}
break;
case '"':
case '\'': if ((p = SkipQuote(p)) == NULL)
return false;
break;
default: if (!*p || isspace(*p)) {
done = !*p;
*p = 0;
if (q) {
if (argc < MAXPLUGINARGS - 1)
argv[argc++] = q;
else {
2002-05-13 16:35:49 +02:00
esyslog("ERROR: plugin argument list too long");
2002-05-09 16:26:56 +02:00
fprintf(stderr, "vdr: plugin argument list too long\n");
return false;
}
q = NULL;
}
}
if (!done)
p = *p ? p + 1 : skipspace(p + 1);
2002-05-09 16:26:56 +02:00
}
}
argv[argc] = NULL;
if (argc)
plugin->SetName(argv[0]);
optind = 0; // to reset the getopt() data
return !argc || plugin->ProcessArgs(argc, argv);
}
}
else {
2002-05-13 16:35:49 +02:00
esyslog("ERROR: %s", error);
2002-05-09 16:26:56 +02:00
fprintf(stderr, "vdr: %s\n", error);
}
return !error && plugin;
}
// --- cPluginManager --------------------------------------------------------
cPluginManager *cPluginManager::pluginManager = NULL;
cPluginManager::cPluginManager(const char *Directory)
{
directory = NULL;
lastHousekeeping = time(NULL);
nextHousekeeping = -1;
2002-05-09 16:26:56 +02:00
if (pluginManager) {
fprintf(stderr, "vdr: attempt to create more than one plugin manager - exiting!\n");
exit(2);
}
SetDirectory(Directory);
pluginManager = this;
}
cPluginManager::~cPluginManager()
{
Shutdown();
free(directory);
2002-05-09 16:26:56 +02:00
if (pluginManager == this)
pluginManager = NULL;
}
void cPluginManager::SetDirectory(const char *Directory)
{
free(directory);
2002-05-09 16:26:56 +02:00
directory = Directory ? strdup(Directory) : NULL;
}
void cPluginManager::AddPlugin(const char *Args)
{
if (strcmp(Args, "*") == 0) {
2004-12-19 16:33:34 +01:00
cReadDir d(directory);
struct dirent *e;
while ((e = d.Next()) != NULL) {
if (strstr(e->d_name, LIBVDR_PREFIX) == e->d_name) {
char *p = strstr(e->d_name, SO_INDICATOR);
if (p) {
*p = 0;
p += strlen(SO_INDICATOR);
if (strcmp(p, VDRVERSION) == 0) {
char *name = e->d_name + strlen(LIBVDR_PREFIX);
if (strcmp(name, "*") != 0) { // let's not get into a loop!
AddPlugin(e->d_name + strlen(LIBVDR_PREFIX));
2002-05-09 16:26:56 +02:00
}
}
}
}
2004-12-19 16:33:34 +01:00
}
2002-05-09 16:26:56 +02:00
return;
}
char *s = strdup(skipspace(Args));
2002-05-09 16:26:56 +02:00
char *p = strchr(s, ' ');
if (p)
*p = 0;
char *buffer = NULL;
asprintf(&buffer, "%s/%s%s%s%s", directory, LIBVDR_PREFIX, s, SO_INDICATOR, VDRVERSION);
dlls.Add(new cDll(buffer, Args));
free(buffer);
free(s);
2002-05-09 16:26:56 +02:00
}
bool cPluginManager::LoadPlugins(bool Log)
{
for (cDll *dll = dlls.First(); dll; dll = dlls.Next(dll)) {
if (!dll->Load(Log))
return false;
}
return true;
}
2003-05-09 15:27:46 +02:00
bool cPluginManager::InitializePlugins(void)
{
for (cDll *dll = dlls.First(); dll; dll = dlls.Next(dll)) {
cPlugin *p = dll->Plugin();
if (p) {
int Language = Setup.OSDLanguage;
Setup.OSDLanguage = 0; // the i18n texts are only available _after_ Start()
isyslog("initializing plugin: %s (%s): %s", p->Name(), p->Version(), p->Description());
Setup.OSDLanguage = Language;
if (!p->Initialize())
return false;
}
}
return true;
}
bool cPluginManager::StartPlugins(void)
2002-05-09 16:26:56 +02:00
{
for (cDll *dll = dlls.First(); dll; dll = dlls.Next(dll)) {
cPlugin *p = dll->Plugin();
if (p) {
int Language = Setup.OSDLanguage;
Setup.OSDLanguage = 0; // the i18n texts are only available _after_ Start()
2003-05-09 15:27:46 +02:00
isyslog("starting plugin: %s", p->Name());
2002-05-09 16:26:56 +02:00
Setup.OSDLanguage = Language;
if (!p->Start())
return false;
2005-01-30 14:23:01 +01:00
p->started = true;
2002-05-09 16:26:56 +02:00
}
}
return true;
2002-05-09 16:26:56 +02:00
}
void cPluginManager::Housekeeping(void)
{
if (time(NULL) - lastHousekeeping > HOUSEKEEPINGDELTA) {
if (++nextHousekeeping >= dlls.Count())
nextHousekeeping = 0;
cDll *dll = dlls.Get(nextHousekeeping);
if (dll) {
cPlugin *p = dll->Plugin();
if (p) {
p->Housekeeping();
}
}
lastHousekeeping = time(NULL);
}
}
2002-05-09 16:26:56 +02:00
bool cPluginManager::HasPlugins(void)
{
return pluginManager && pluginManager->dlls.Count();
}
cPlugin *cPluginManager::GetPlugin(int Index)
{
cDll *dll = pluginManager ? pluginManager->dlls.Get(Index) : NULL;
return dll ? dll->Plugin() : NULL;
}
cPlugin *cPluginManager::GetPlugin(const char *Name)
{
if (pluginManager) {
for (cDll *dll = pluginManager->dlls.First(); dll; dll = pluginManager->dlls.Next(dll)) {
cPlugin *p = dll->Plugin();
if (p && strcmp(p->Name(), Name) == 0)
return p;
}
}
return NULL;
}
cPlugin *cPluginManager::CallFirstService(const char *Id, void *Data)
{
if (pluginManager) {
for (cDll *dll = pluginManager->dlls.First(); dll; dll = pluginManager->dlls.Next(dll)) {
cPlugin *p = dll->Plugin();
if (p && p->Service(Id, Data))
return p;
}
}
return NULL;
}
bool cPluginManager::CallAllServices(const char *Id, void *Data)
{
bool found=false;
if (pluginManager) {
for (cDll *dll = pluginManager->dlls.First(); dll; dll = pluginManager->dlls.Next(dll)) {
cPlugin *p = dll->Plugin();
if (p && p->Service(Id, Data))
found = true;
}
}
return found;
}
2005-01-30 14:23:01 +01:00
void cPluginManager::StopPlugins(void)
{
for (cDll *dll = dlls.Last(); dll; dll = dlls.Prev(dll)) {
cPlugin *p = dll->Plugin();
if (p && p->started) {
isyslog("stopping plugin: %s", p->Name());
p->Stop();
p->started = false;
}
}
}
void cPluginManager::Shutdown(void)
2002-05-09 16:26:56 +02:00
{
cDll *dll;
while ((dll = dlls.Last()) != NULL)
2002-05-09 16:26:56 +02:00
dlls.Del(dll);
}