vdr/plugin.c
Klaus Schmidinger 19b952728e Version 1.7.27
Original announce message:
VDR developer version 1.7.27 is now available at

       ftp://ftp.tvdr.de/vdr/Developer/vdr-1.7.27.tar.bz2

A 'diff' against the previous version is available at

       ftp://ftp.tvdr.de/vdr/Developer/vdr-1.7.26-1.7.27.diff

MD5 checksums:

bfeaa79a9e55144bca2b69139c45f1bb  vdr-1.7.27.tar.bz2
b23344be51d3e2c2d96cc2dd4e8e564e  vdr-1.7.26-1.7.27.diff

WARNING:
========

This is a developer version. Even though I use it in my productive
environment. I strongly recommend that you only use it under controlled
conditions and for testing and debugging.

From the HISTORY file:
- Updated the Finnish OSD texts (thanks to Rolf Ahrenberg).
- Changed the Green button in the "Edit timer" menu from "Once" to "Single"
  (suggested by Rolf Ahrenberg).
- Fixed some typos in HISTORY and CONTRIBUTORS (thanks to Ville Skyttä).
- The channel name column in the "What's on now/next" menu now adjusts its width
  to display the full short name of each channel (suggested by Dominic Evans).
- Dropped the meanwhile obsolete script 'i18n-to-gettext'.
- Removed the obsolete function cPlugin::RegisterI18n().
- Removed the obsolete typedef tI18nPhrase.
- Adapted menu column widths of 'skincurses' to the wider HD OSD sizes.
- Deactivated definition of __RECORDING_H_DEPRECATED_DIRECT_MEMBER_ACCESS (recording.h)
  and LEGACY_CRECEIVER (receiver.h) to trigger an error for any plugin that still
  uses the respective code. You can reactivate these to quickly make your plugin
  compile again, but beware that these code parts will be removed in one of the next
  versions.
- Made the "overloaded-virtual" warning an error to detect hidden overloaded
  virtual functions (thanks to Anssi Hannula for pointing out -Werror=...).
  Plugin authors may want to change -Woverloaded-virtual to -Werror=overloaded-virtual
  in their Makefiles.
- Updated the Estonian OSD texts (thanks to Arthur Konovalov).
- Improved fast forwarding to the end of a timeshift recording.
- The new function cDevice::DeviceName() returns a string identifying the name of
  the given device.
- When toggling a timer between "Single" and "Repeating", the previous setting is now
  retained in case the user toggles back to the original value.
- When estimating the remaining disk space (in hours), the average data rate of all
  existing recordings is now taken into account. If this value can't be determined,
  the previous value of 25.75 MB/min is taken.
- No longer using GetFont() (which is not thread safe) in the 'osddemo' plugin.
- No longer using GetFont() (which is not thread safe) in cSubtitleRegion::UpdateTextData().
- Fixed a memory leak in cSubtitleRegion::UpdateTextData().
- Moved setting LC_NUMERIC further up to make sure any floating point numbers use a
  decimal point (suggested by Tobias Grimm).
- Added missing channel locking to cEIT.
- Fixed reduced bpp support for DVB subtitles (thanks to Rolf Ahrenberg).
- Updated the Italian OSD texts (thanks to Diego Pierotto).
- Reverted some improvements to Make.config.template (thanks to Christian Ruppert).
- Fixed handling IDLEPRIORITY in cDvbDevice::ProvidesChannel() (thanks to Frank
  Schmirler).
2012-03-25 15:43:37 +02:00

487 lines
11 KiB
C

/*
* 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 2.3 2012/03/11 13:56:02 kls Exp $
*/
#include "plugin.h"
#include <ctype.h>
#include <dirent.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <time.h>
#include "config.h"
#include "interface.h"
#include "thread.h"
#define LIBVDR_PREFIX "libvdr-"
#define SO_INDICATOR ".so."
#define MAXPLUGINARGS 1024
#define HOUSEKEEPINGDELTA 10 // seconds
// --- cPlugin ---------------------------------------------------------------
char *cPlugin::configDirectory = NULL;
cPlugin::cPlugin(void)
{
name = NULL;
started = false;
}
cPlugin::~cPlugin()
{
}
void cPlugin::SetName(const char *s)
{
name = s;
I18nRegister(name);
}
const char *cPlugin::CommandLineHelp(void)
{
return NULL;
}
bool cPlugin::ProcessArgs(int argc, char *argv[])
{
return true;
}
bool cPlugin::Initialize(void)
{
return true;
}
bool cPlugin::Start(void)
{
return true;
}
void cPlugin::Stop(void)
{
}
void cPlugin::Housekeeping(void)
{
}
void cPlugin::MainThreadHook(void)
{
}
cString cPlugin::Active(void)
{
return NULL;
}
time_t cPlugin::WakeupTime(void)
{
return 0;
}
const char *cPlugin::MainMenuEntry(void)
{
return NULL;
}
cOsdObject *cPlugin::MainMenuAction(void)
{
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;
}
const char **cPlugin::SVDRPHelpPages(void)
{
return NULL;
}
cString cPlugin::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
{
return NULL;
}
void cPlugin::SetConfigDirectory(const char *Dir)
{
free(configDirectory);
configDirectory = strdup(Dir);
}
const char *cPlugin::ConfigDirectory(const char *PluginName)
{
static cString buffer;
if (!cThread::IsMainThread())
esyslog("ERROR: plugin '%s' called cPlugin::ConfigDirectory(), which is not thread safe!", PluginName ? PluginName : "<no name given>");
buffer = cString::sprintf("%s/plugins%s%s", configDirectory, PluginName ? "/" : "", PluginName ? PluginName : "");
return MakeDirs(buffer, true) ? *buffer : NULL;
}
// --- 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);
}
static char *SkipQuote(char *s)
{
char c = *s;
memmove(s, s + 1, strlen(s));
while (*s && *s != c) {
if (*s == '\\')
memmove(s, s + 1, strlen(s));
if (*s)
s++;
}
if (*s) {
memmove(s, s + 1, strlen(s));
return s;
}
esyslog("ERROR: missing closing %c", c);
fprintf(stderr, "vdr: missing closing %c\n", c);
return NULL;
}
bool cDll::Load(bool Log)
{
if (Log)
isyslog("loading plugin: %s", fileName);
if (handle) {
esyslog("attempt to load plugin '%s' twice!", fileName);
return false;
}
handle = dlopen(fileName, RTLD_NOW);
const char *error = dlerror();
if (!error) {
void *(*creator)(void);
creator = (void *(*)(void))dlsym(handle, "VDRPluginCreator");
if (!(error = dlerror()))
plugin = (cPlugin *)creator();
}
if (!error) {
if (plugin && args) {
int argc = 0;
char *argv[MAXPLUGINARGS];
char *p = skipspace(stripspace(args));
char *q = NULL;
bool done = false;
while (!done) {
if (!q)
q = p;
switch (*p) {
case '\\': memmove(p, p + 1, strlen(p));
if (*p)
p++;
else {
esyslog("ERROR: missing character after \\");
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 {
esyslog("ERROR: plugin argument list too long");
fprintf(stderr, "vdr: plugin argument list too long\n");
return false;
}
q = NULL;
}
}
if (!done)
p = *p ? p + 1 : skipspace(p + 1);
}
}
argv[argc] = NULL;
if (argc)
plugin->SetName(argv[0]);
optind = 0; // to reset the getopt() data
return !Log || !argc || plugin->ProcessArgs(argc, argv);
}
}
else {
esyslog("ERROR: %s", error);
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;
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);
if (pluginManager == this)
pluginManager = NULL;
}
void cPluginManager::SetDirectory(const char *Directory)
{
free(directory);
directory = Directory ? strdup(Directory) : NULL;
}
void cPluginManager::AddPlugin(const char *Args)
{
if (strcmp(Args, "*") == 0) {
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, APIVERSION) == 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));
}
}
}
}
}
return;
}
char *s = strdup(skipspace(Args));
char *p = strchr(s, ' ');
if (p)
*p = 0;
dlls.Add(new cDll(cString::sprintf("%s/%s%s%s%s", directory, LIBVDR_PREFIX, s, SO_INDICATOR, APIVERSION), Args));
free(s);
}
bool cPluginManager::LoadPlugins(bool Log)
{
for (cDll *dll = dlls.First(); dll; dll = dlls.Next(dll)) {
if (!dll->Load(Log))
return false;
}
return true;
}
bool cPluginManager::InitializePlugins(void)
{
for (cDll *dll = dlls.First(); dll; dll = dlls.Next(dll)) {
cPlugin *p = dll->Plugin();
if (p) {
isyslog("initializing plugin: %s (%s): %s", p->Name(), p->Version(), p->Description());
if (!p->Initialize())
return false;
}
}
return true;
}
bool cPluginManager::StartPlugins(void)
{
for (cDll *dll = dlls.First(); dll; dll = dlls.Next(dll)) {
cPlugin *p = dll->Plugin();
if (p) {
isyslog("starting plugin: %s", p->Name());
if (!p->Start())
return false;
p->started = true;
}
}
return true;
}
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);
}
}
void cPluginManager::MainThreadHook(void)
{
for (cDll *dll = pluginManager->dlls.First(); dll; dll = pluginManager->dlls.Next(dll)) {
cPlugin *p = dll->Plugin();
if (p)
p->MainThreadHook();
}
}
bool cPluginManager::Active(const char *Prompt)
{
if (pluginManager) {
for (cDll *dll = pluginManager->dlls.First(); dll; dll = pluginManager->dlls.Next(dll)) {
cPlugin *p = dll->Plugin();
if (p) {
cString s = p->Active();
if (!isempty(*s)) {
if (!Prompt || !Interface->Confirm(cString::sprintf("%s - %s", *s, Prompt)))
return true;
}
}
}
}
return false;
}
cPlugin *cPluginManager::GetNextWakeupPlugin(void)
{
cPlugin *NextPlugin = NULL;
if (pluginManager) {
time_t Now = time(NULL);
time_t Next = 0;
for (cDll *dll = pluginManager->dlls.First(); dll; dll = pluginManager->dlls.Next(dll)) {
cPlugin *p = dll->Plugin();
if (p) {
time_t t = p->WakeupTime();
if (t > Now && (!Next || t < Next)) {
Next = t;
NextPlugin = p;
}
}
}
}
return NextPlugin;
}
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 && Name) {
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;
}
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(bool Log)
{
cDll *dll;
while ((dll = dlls.Last()) != NULL) {
cPlugin *p = dll->Plugin();
if (p && Log)
isyslog("deleting plugin: %s", p->Name());
dlls.Del(dll);
}
}