• Main Page
  • Classes
  • Files
  • File List

CArchPluginWindows.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2012 Bolton Software Ltd.
00004  * Copyright (C) 2012 Nick Bolton
00005  * 
00006  * This package is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * found in the file COPYING that should have accompanied this file.
00009  * 
00010  * This package is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include "CArchPluginWindows.h"
00020 #include "XArchWindows.h"
00021 #include "CLog.h"
00022 #include "IEventQueue.h"
00023 #include "CEvent.h"
00024 #include "CScreen.h"
00025 #include "IPlatformScreen.h" // temp
00026 
00027 #define WIN32_LEAN_AND_MEAN
00028 #include <Windows.h>
00029 #include <iostream>
00030 
00031 typedef int (*initFunc)(void (*sendEvent)(const char*, void*), void (*log)(const char*));
00032 
00033 void* CArchPluginWindows::m_eventTarget = NULL;
00034 
00035 CArchPluginWindows::CArchPluginWindows()
00036 {
00037 }
00038 
00039 CArchPluginWindows::~CArchPluginWindows()
00040 {
00041 }
00042 
00043 void
00044 CArchPluginWindows::init(void* eventTarget)
00045 {
00046     m_eventTarget = eventTarget;
00047     
00048     CString dir = getPluginsDir();
00049     LOG((CLOG_DEBUG "plugins dir: %s", dir.c_str()));
00050 
00051     CString pattern = CString(dir).append("\\*.dll");
00052     std::vector<CString> plugins;
00053     getFilenames(pattern, plugins);
00054 
00055     std::vector<CString>::iterator it;
00056     for (it = plugins.begin(); it != plugins.end(); ++it)
00057         load(*it);
00058 }
00059 
00060 void
00061 CArchPluginWindows::load(const CString& dllFilename)
00062 {
00063     LOG((CLOG_DEBUG "loading plugin: %s", dllFilename.c_str()));
00064     CString path = CString(getPluginsDir()).append("\\").append(dllFilename);
00065     HINSTANCE library = LoadLibrary(path.c_str());
00066     if (library == NULL)
00067         throw XArch(new XArchEvalWindows);
00068 
00069     initFunc initPlugin = (initFunc)GetProcAddress(library, "init");
00070     initPlugin(&sendEvent, &log);
00071 }
00072 
00073 CString
00074 CArchPluginWindows::getModuleDir()
00075 {
00076     TCHAR c_modulePath[MAX_PATH];
00077     if (GetModuleFileName(NULL, c_modulePath, MAX_PATH) == 0) {
00078         throw XArch(new XArchEvalWindows);
00079     }
00080 
00081     CString modulePath(c_modulePath);
00082     size_t lastSlash = modulePath.find_last_of("\\");
00083 
00084     if (lastSlash != CString::npos) {
00085         return modulePath.substr(0, lastSlash);
00086     }
00087 
00088     throw XArch("could not get module path.");
00089 }
00090 
00091 void
00092 CArchPluginWindows::getFilenames(const CString& pattern, std::vector<CString>& filenames)
00093 {
00094     WIN32_FIND_DATA data;
00095     HANDLE find = FindFirstFile(pattern.c_str(), &data);
00096     if (find == INVALID_HANDLE_VALUE) {
00097         FindClose(find);
00098         LOG((CLOG_DEBUG "plugins dir is empty: %s", pattern.c_str()));
00099         return;
00100     }
00101 
00102     do {
00103         filenames.push_back(data.cFileName);
00104     } while (FindNextFile(find, &data));
00105 
00106     FindClose(find);
00107 }
00108 
00109 CString CArchPluginWindows::getPluginsDir()
00110 {
00111     return getModuleDir().append("\\").append(PLUGINS_DIR);
00112 }
00113 
00114 void
00115 sendEvent(const char* eventName, void* data)
00116 {
00117     LOG((CLOG_DEBUG5 "plugin sending event"));
00118     CEvent::Type type = EVENTQUEUE->getRegisteredType(eventName);
00119     EVENTQUEUE->addEvent(CEvent(type, CArchPluginWindows::m_eventTarget, data));
00120 }
00121 
00122 void
00123 log(const char* text)
00124 {
00125     LOG((CLOG_DEBUG "plugin: %s", text)); 
00126 }

Generated on Thu Jun 20 2013 00:00:04 for Synergy by  doxygen 1.7.1