📄 pluginmgr.cxx
字号:
return desc->CreateInstance(userData);
}
for (PINDEX i = 0; i < services.GetSize(); i++) {
const PPluginService & service = services[i];
if (service.serviceType *= serviceType) {
PDevicePluginServiceDescriptor * descriptor = (PDevicePluginServiceDescriptor *)service.descriptor;
if (PAssertNULL(descriptor) != NULL && descriptor->ValidateDeviceName(deviceName, userData))
return descriptor->CreateInstance(userData);
}
}
return NULL;
}
bool PDevicePluginServiceDescriptor::ValidateDeviceName(const PString & deviceName, int userData) const
{
PStringArray devices = GetDeviceNames(userData);
if (
(deviceName.GetLength() == 2) &&
(deviceName[0] == '#') &&
isdigit(deviceName[1]) &&
((deviceName[1]-'0') < devices.GetSize())
)
return true;
for (PINDEX j = 0; j < devices.GetSize(); j++) {
if (devices[j] *= deviceName)
return true;
}
return false;
}
bool PDevicePluginServiceDescriptor::GetDeviceCapabilities(const PString & /*deviceName*/,
void * /*capabilities*/) const
{
return false;
}
PStringArray PPluginManager::GetPluginsDeviceNames(const PString & serviceName,
const PString & serviceType,
int userData) const
{
PStringArray allDevices;
if (serviceName.IsEmpty() || serviceName == "*") {
PWaitAndSignal n(servicesMutex);
PINDEX i;
PStringToString deviceToPluginMap;
// First we run through all of the drivers and their lists of devices and
// use the dictionary to assure all names are unique
for (i = 0; i < services.GetSize(); i++) {
const PPluginService & service = services[i];
if (service.serviceType *= serviceType) {
PStringArray devices = ((PDevicePluginServiceDescriptor *)service.descriptor)->GetDeviceNames(userData);
for (PINDEX j = 0; j < devices.GetSize(); j++) {
PCaselessString device = devices[j];
if (deviceToPluginMap.Contains(device)) {
PString oldPlugin = deviceToPluginMap[device];
if (!oldPlugin.IsEmpty()) {
// Make name unique by prepending driver name and a tab character
deviceToPluginMap.SetAt(oldPlugin+PDevicePluginServiceDescriptor::SeparatorChar+device, "");
// Reset the original to empty string so we dont add it multiple times
deviceToPluginMap.SetAt(device, "");
}
// Now add the new one
deviceToPluginMap.SetAt(service.serviceName+PDevicePluginServiceDescriptor::SeparatorChar+device, "");
}
else
deviceToPluginMap.SetAt(device, service.serviceName);
}
}
}
for (i = 0; i < deviceToPluginMap.GetSize(); i++)
allDevices.AppendString(deviceToPluginMap.GetKeyAt(i));
}
else {
PDevicePluginServiceDescriptor * descr =
(PDevicePluginServiceDescriptor *)GetServiceDescriptor(serviceName, serviceType);
if (descr != NULL)
allDevices = descr->GetDeviceNames(userData);
}
return allDevices;
}
PBoolean PPluginManager::GetPluginsDeviceCapabilities(const PString & serviceType,
const PString & serviceName,
const PString & deviceName,
void * capabilities) const
{
if (serviceType.IsEmpty() || deviceName.IsEmpty())
return PFalse;
if (serviceName.IsEmpty() || serviceName == "*") {
for (PINDEX i = 0; i < services.GetSize(); i++) {
const PPluginService & service = services[i];
if (service.serviceType *= serviceType) {
PDevicePluginServiceDescriptor * desc = (PDevicePluginServiceDescriptor *)service.descriptor;
if (desc != NULL && desc->ValidateDeviceName(deviceName, 0))
return desc->GetDeviceCapabilities(deviceName,capabilities);
}
}
} else {
PDevicePluginServiceDescriptor * desc = (PDevicePluginServiceDescriptor *)GetServiceDescriptor(serviceName, serviceType);
if (desc != NULL && desc->ValidateDeviceName(deviceName, 0))
return desc->GetDeviceCapabilities(deviceName,capabilities);
}
return PFalse;
}
PBoolean PPluginManager::RegisterService(const PString & serviceName,
const PString & serviceType,
PPluginServiceDescriptor * descriptor)
{
PWaitAndSignal m(servicesMutex);
// first, check if it something didn't already register that name and type
for (PINDEX i = 0; i < services.GetSize(); i++) {
if (services[i].serviceName == serviceName &&
services[i].serviceType == serviceType)
return PFalse;
}
PPluginService * service = new PPluginService(serviceName, serviceType, descriptor);
services.Append(service);
PDevicePluginAdapterBase * adapter = PFactory<PDevicePluginAdapterBase>::CreateInstance(serviceType);
if (adapter != NULL)
adapter->CreateFactory(serviceName);
return PTrue;
}
void PPluginManager::AddNotifier(const PNotifier & notifyFunction, PBoolean existing)
{
PWaitAndSignal m(notifiersMutex);
notifiers.Append(new PNotifier(notifyFunction));
if (existing)
for (PINDEX i = 0; i < plugins.GetSize(); i++)
CallNotifier(plugins[i], 0);
}
void PPluginManager::RemoveNotifier(const PNotifier & notifyFunction)
{
PWaitAndSignal m(notifiersMutex);
for (PList<PNotifier>::iterator i = notifiers.begin(); i != notifiers.end(); i++) {
if (*i == notifyFunction) {
notifiers.Remove(&*i);
i = notifiers.begin();
}
}
}
void PPluginManager::CallNotifier(PDynaLink & dll, INT code)
{
PWaitAndSignal m(notifiersMutex);
for (PList<PNotifier>::iterator i = notifiers.begin(); i != notifiers.end(); i++)
(*i)(dll, code);
}
////////////////////////////////////////////////////////////////////////////////////
PPluginModuleManager::PPluginModuleManager(const char * _signatureFunctionName, PPluginManager * _pluginMgr)
: signatureFunctionName(_signatureFunctionName)
{
pluginDLLs.DisallowDeleteObjects();
pluginMgr = _pluginMgr;;
if (pluginMgr == NULL)
pluginMgr = &PPluginManager::GetPluginManager();
}
void PPluginModuleManager::OnLoadModule(PDynaLink & dll, INT code)
{
PDynaLink::Function dummyFunction;
if (!dll.GetFunction(signatureFunctionName, dummyFunction))
return;
switch (code) {
case 0:
pluginDLLs.SetAt(dll.GetName(), &dll);
break;
case 1:
pluginDLLs.RemoveAt(dll.GetName());
break;
default:
break;
}
OnLoadPlugin(dll, code);
}
////////////////////////////////////////////////////////////////////////////////////
void PluginLoaderStartup::OnStartup()
{
// load the actual DLLs, which will also load the system plugins
PStringArray dirs = PPluginManager::GetPluginDirs();
PPluginManager & mgr = PPluginManager::GetPluginManager();
PINDEX i;
for (i = 0; i < dirs.GetSize(); i++)
mgr.LoadPluginDirectory(dirs[i]);
// load the plugin module managers, and construct the list of suffixes
PFactory<PPluginModuleManager>::KeyList_T keyList = PFactory<PPluginModuleManager>::GetKeyList();
PFactory<PPluginModuleManager>::KeyList_T::const_iterator r;
for (r = keyList.begin(); r != keyList.end(); ++r) {
PPluginModuleManager * mgr = PFactory<PPluginModuleManager>::CreateInstance(*r);
if (mgr == NULL) {
PTRACE(1, "PLUGIN\tCannot create manager for plugins of type " << *r);
} else {
PTRACE(3, "PLUGIN\tCreated manager for plugins of type " << *r);
managers.push_back(mgr);
}
}
}
void PluginLoaderStartup::OnShutdown()
{
while (managers.begin() != managers.end()) {
std::vector<PPluginModuleManager *>::iterator r = managers.begin();
PPluginModuleManager * mgr = *r;
managers.erase(r);
mgr->OnShutdown();
delete mgr;
}
}
static PFactory<PProcessStartup>::Worker<PluginLoaderStartup> pluginLoaderStartupFactory("PluginLoader", true);
PINSTANTIATE_FACTORY(PluginLoaderStartup, PString)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -