2012-06-10 16:50:54 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
2014-11-02 12:12:05 +00:00
|
|
|
* Copyright (C) 2012 Synergy Si Ltd.
|
2012-06-10 16:50:54 +00:00
|
|
|
* Copyright (C) 2012 Nick Bolton
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "arch/win32/ArchPluginWindows.h"
|
|
|
|
#include "arch/win32/XArchWindows.h"
|
|
|
|
#include "base/Log.h"
|
|
|
|
#include "base/IEventQueue.h"
|
|
|
|
#include "base/Event.h"
|
|
|
|
#include "synergy/Screen.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
typedef int (*initFunc)(void (*sendEvent)(const char*, void*), void (*log)(const char*));
|
|
|
|
|
2013-06-29 14:17:49 +00:00
|
|
|
void* g_eventTarget = NULL;
|
|
|
|
IEventQueue* g_events = NULL;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
ArchPluginWindows::ArchPluginWindows()
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
ArchPluginWindows::~ArchPluginWindows()
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-11 13:51:47 +00:00
|
|
|
ArchPluginWindows::init(void* eventTarget, IEventQueue* events)
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
2013-06-29 14:17:49 +00:00
|
|
|
g_eventTarget = eventTarget;
|
|
|
|
g_events = events;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
String dir = getPluginsDir();
|
2012-06-10 16:50:54 +00:00
|
|
|
LOG((CLOG_DEBUG "plugins dir: %s", dir.c_str()));
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
String pattern = String(dir).append("\\*.dll");
|
|
|
|
std::vector<String> plugins;
|
2012-06-10 16:50:54 +00:00
|
|
|
getFilenames(pattern, plugins);
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
std::vector<String>::iterator it;
|
2012-06-10 16:50:54 +00:00
|
|
|
for (it = plugins.begin(); it != plugins.end(); ++it)
|
|
|
|
load(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-11 13:51:47 +00:00
|
|
|
ArchPluginWindows::load(const String& dllFilename)
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
LOG((CLOG_DEBUG "loading plugin: %s", dllFilename.c_str()));
|
2014-11-11 13:51:47 +00:00
|
|
|
String path = String(getPluginsDir()).append("\\").append(dllFilename);
|
2012-06-10 16:50:54 +00:00
|
|
|
HINSTANCE library = LoadLibrary(path.c_str());
|
|
|
|
if (library == NULL)
|
|
|
|
throw XArch(new XArchEvalWindows);
|
|
|
|
|
|
|
|
initFunc initPlugin = (initFunc)GetProcAddress(library, "init");
|
|
|
|
initPlugin(&sendEvent, &log);
|
|
|
|
}
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
String
|
|
|
|
ArchPluginWindows::getModuleDir()
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
TCHAR c_modulePath[MAX_PATH];
|
2012-07-14 00:15:07 +00:00
|
|
|
if (GetModuleFileName(NULL, c_modulePath, MAX_PATH) == 0) {
|
2012-06-10 16:50:54 +00:00
|
|
|
throw XArch(new XArchEvalWindows);
|
|
|
|
}
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
String modulePath(c_modulePath);
|
2012-06-10 16:50:54 +00:00
|
|
|
size_t lastSlash = modulePath.find_last_of("\\");
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
if (lastSlash != String::npos) {
|
2012-06-10 16:50:54 +00:00
|
|
|
return modulePath.substr(0, lastSlash);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw XArch("could not get module path.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-11 13:51:47 +00:00
|
|
|
ArchPluginWindows::getFilenames(const String& pattern, std::vector<String>& filenames)
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
WIN32_FIND_DATA data;
|
|
|
|
HANDLE find = FindFirstFile(pattern.c_str(), &data);
|
|
|
|
if (find == INVALID_HANDLE_VALUE) {
|
|
|
|
FindClose(find);
|
|
|
|
LOG((CLOG_DEBUG "plugins dir is empty: %s", pattern.c_str()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
filenames.push_back(data.cFileName);
|
|
|
|
} while (FindNextFile(find, &data));
|
|
|
|
|
|
|
|
FindClose(find);
|
|
|
|
}
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
String ArchPluginWindows::getPluginsDir()
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
return getModuleDir().append("\\").append(PLUGINS_DIR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sendEvent(const char* eventName, void* data)
|
|
|
|
{
|
|
|
|
LOG((CLOG_DEBUG5 "plugin sending event"));
|
2014-11-11 13:51:47 +00:00
|
|
|
Event::Type type = g_events->getRegisteredType(eventName);
|
|
|
|
g_events->addEvent(Event(type, g_eventTarget, data));
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
log(const char* text)
|
|
|
|
{
|
|
|
|
LOG((CLOG_DEBUG "plugin: %s", text));
|
|
|
|
}
|