summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2012-06-26 16:50:14 +0000
committerGeorge Hazan <george.hazan@gmail.com>2012-06-26 16:50:14 +0000
commitc992cb2fdc11f1cac4bc5cbce26e8e2bb3b57da0 (patch)
tree697bdbf38a8a1f6b828a8bfbd08a478e19a82c6b /src/core
parentf616294363c642d138f9dc0ef6eceae639e2434c (diff)
- microkernel addded;
- version bumped to 0.92.2 git-svn-id: http://svn.miranda-ng.org/main/trunk@641 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'src/core')
-rw-r--r--src/core/commonheaders.h5
-rw-r--r--src/core/miranda.cpp396
-rw-r--r--src/core/miranda.h109
-rw-r--r--src/core/modules.cpp632
4 files changed, 37 insertions, 1105 deletions
diff --git a/src/core/commonheaders.h b/src/core/commonheaders.h
index e47ba25fb3..5e6c25da62 100644
--- a/src/core/commonheaders.h
+++ b/src/core/commonheaders.h
@@ -56,10 +56,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <win2k.h>
-#include "modules.h"
-
#include <m_system.h>
#include <m_system_cpp.h>
+#include <m_core.h>
#include <newpluginapi.h>
#include <m_database.h>
#include <m_clc.h>
@@ -94,8 +93,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <m_timezones.h>
#include "miranda.h"
-#include "forkthread.h"
-#include "../modules/database/dblists.h"
#include <m_ssl.h>
#include <m_netlib.h>
diff --git a/src/core/miranda.cpp b/src/core/miranda.cpp
index 8678524411..baf786a8d3 100644
--- a/src/core/miranda.cpp
+++ b/src/core/miranda.cpp
@@ -27,15 +27,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define MMI_SIZE_V1 (4*sizeof(void*))
#define MMI_SIZE_V2 (7*sizeof(void*))
-int InitPathUtils(void);
-int InitialiseModularEngine(void);
int LoadDefaultModules(void);
-void DestroyModularEngine(void);
void UnloadNewPluginsModule(void);
void UnloadDefaultModules(void);
-void RecalculateTime(void);
-
-HINSTANCE GetInstByAddress(void* codePtr);
pfnMyMonitorFromPoint MyMonitorFromPoint;
pfnMyMonitorFromRect MyMonitorFromRect;
@@ -84,106 +78,31 @@ LPFN_WSAADDRESSTOSTRINGA MyWSAAddressToString;
ITaskbarList3 * pTaskbarInterface;
-static DWORD MsgWaitForMultipleObjectsExWorkaround(DWORD nCount, const HANDLE *pHandles,
- DWORD dwMsecs, DWORD dwWakeMask, DWORD dwFlags);
-
HANDLE hOkToExitEvent, hModulesLoadedEvent;
HANDLE hShutdownEvent, hPreShutdownEvent;
static HANDLE hWaitObjects[MAXIMUM_WAIT_OBJECTS-1];
static char *pszWaitServices[MAXIMUM_WAIT_OBJECTS-1];
static int waitObjectCount = 0;
-HANDLE hStackMutex, hMirandaShutdown, hThreadQueueEmpty;
-HINSTANCE hMirandaInst;
+HANDLE hMirandaShutdown, hThreadQueueEmpty;
+HINSTANCE hInst;
int hLangpack = 0;
/////////////////////////////////////////////////////////////////////////////////////////
// exception handling
-static DWORD __cdecl sttDefaultFilter(DWORD, EXCEPTION_POINTERS*)
-{
- return EXCEPTION_EXECUTE_HANDLER;
-}
-
-pfnExceptionFilter pMirandaExceptFilter = sttDefaultFilter;
-
-static INT_PTR GetExceptionFilter(WPARAM, LPARAM)
+static INT_PTR srvGetExceptionFilter(WPARAM, LPARAM)
{
- return (INT_PTR)pMirandaExceptFilter;
+ return (INT_PTR)GetExceptionFilter();
}
-static INT_PTR SetExceptionFilter(WPARAM, LPARAM lParam)
+static INT_PTR srvSetExceptionFilter(WPARAM, LPARAM lParam)
{
- pfnExceptionFilter oldOne = pMirandaExceptFilter;
- if (lParam != 0)
- pMirandaExceptFilter = (pfnExceptionFilter)lParam;
- return (INT_PTR)oldOne;
+ return (INT_PTR)SetExceptionFilter((pfnExceptionFilter)lParam);
}
/////////////////////////////////////////////////////////////////////////////////////////
-// thread support functions
-
-typedef struct
-{
- DWORD dwThreadId; // valid if hThread isn't signalled
- HANDLE hThread;
- HINSTANCE hOwner;
- void* pObject;
- PVOID addr;
-}
-THREAD_WAIT_ENTRY;
-
-static LIST<THREAD_WAIT_ENTRY> threads(10, NumericKeySortT);
-
-struct FORK_ARG {
- HANDLE hEvent;
- pThreadFunc threadcode;
- pThreadFuncEx threadcodeex;
- void *arg, *owner;
-};
-
-/////////////////////////////////////////////////////////////////////////////////////////
// forkthread - starts a new thread
-void __cdecl forkthread_r(void * arg)
-{
- struct FORK_ARG * fa = (struct FORK_ARG *) arg;
- void (*callercode)(void*)=fa->threadcode;
- void * cookie=fa->arg;
- CallService(MS_SYSTEM_THREAD_PUSH, 0, (LPARAM)callercode);
- SetEvent(fa->hEvent);
- __try
- {
- callercode(cookie);
- }
- __except(pMirandaExceptFilter(GetExceptionCode(), GetExceptionInformation()))
- {
- Netlib_Logf(NULL, "Unhandled exception in thread %x", GetCurrentThreadId());
- }
-
- SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
- CallService(MS_SYSTEM_THREAD_POP, 0, 0);
- return;
-}
-
-UINT_PTR forkthread (
- void (__cdecl *threadcode)(void*),
- unsigned long stacksize,
- void *arg
- )
-{
- UINT_PTR rc;
- struct FORK_ARG fa;
- fa.hEvent=CreateEvent(NULL, FALSE, FALSE, NULL);
- fa.threadcode=threadcode;
- fa.arg=arg;
- rc=_beginthread(forkthread_r, stacksize, &fa);
- if ((UINT_PTR)-1L != rc)
- WaitForSingleObject(fa.hEvent, INFINITE);
-
- CloseHandle(fa.hEvent);
- return rc;
-}
-
static INT_PTR ForkThreadService(WPARAM wParam, LPARAM lParam)
{
return (INT_PTR)forkthread((pThreadFunc)wParam, 0, (void*)lParam);
@@ -192,56 +111,6 @@ static INT_PTR ForkThreadService(WPARAM wParam, LPARAM lParam)
/////////////////////////////////////////////////////////////////////////////////////////
// forkthreadex - starts a new thread with the extended info and returns the thread id
-unsigned __stdcall forkthreadex_r(void * arg)
-{
- struct FORK_ARG *fa = (struct FORK_ARG *)arg;
- pThreadFuncEx threadcode = fa->threadcodeex;
- pThreadFuncOwner threadcodeex = (pThreadFuncOwner)fa->threadcodeex;
- void *cookie = fa->arg;
- void *owner = fa->owner;
- unsigned long rc = 0;
-
- CallService(MS_SYSTEM_THREAD_PUSH, (WPARAM)fa->owner, (LPARAM)threadcode);
- SetEvent(fa->hEvent);
- __try
- {
- if (owner)
- rc = threadcodeex(owner, cookie);
- else
- rc = threadcode(cookie);
- }
- __except(pMirandaExceptFilter(GetExceptionCode(), GetExceptionInformation()))
- {
- Netlib_Logf(NULL, "Unhandled exception in thread %x", GetCurrentThreadId());
- }
-
- SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
- CallService(MS_SYSTEM_THREAD_POP, 0, 0);
- return rc;
-}
-
-UINT_PTR forkthreadex(
- void *sec,
- unsigned stacksize,
- unsigned (__stdcall *threadcode)(void*),
- void* owner,
- void *arg,
- unsigned *thraddr)
-{
- UINT_PTR rc;
- struct FORK_ARG fa = { 0 };
- fa.threadcodeex = threadcode;
- fa.arg = arg;
- fa.owner = owner;
- fa.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
- rc = _beginthreadex(sec, stacksize, forkthreadex_r, (void *)&fa, 0, thraddr);
- if (rc)
- WaitForSingleObject(fa.hEvent, INFINITE);
-
- CloseHandle(fa.hEvent);
- return rc;
-}
-
static INT_PTR ForkThreadServiceEx(WPARAM wParam, LPARAM lParam)
{
FORK_THREADEX_PARAMS* params = (FORK_THREADEX_PARAMS*)lParam;
@@ -253,108 +122,6 @@ static INT_PTR ForkThreadServiceEx(WPARAM wParam, LPARAM lParam)
}
/////////////////////////////////////////////////////////////////////////////////////////
-// APC and mutex functions
-
-static void __stdcall DummyAPCFunc(ULONG_PTR)
-{
- /* called in the context of thread that cleared it's APC queue */
- return;
-}
-
-static int MirandaWaitForMutex(HANDLE hEvent)
-{
- for (;;) {
- // will get WAIT_IO_COMPLETE for QueueUserAPC() which isnt a result
- DWORD rc = MsgWaitForMultipleObjectsExWorkaround(1, &hEvent, INFINITE, QS_ALLINPUT, MWMO_ALERTABLE);
- if (rc == WAIT_OBJECT_0 + 1) {
- MSG msg;
- while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
- if (IsDialogMessage(msg.hwnd, &msg)) continue;
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- } else if (rc == WAIT_OBJECT_0) {
- // got object
- return 1;
- } else if (rc == WAIT_ABANDONED_0 || rc == WAIT_FAILED) return 0;
- }
-}
-
-VOID CALLBACK KillAllThreads(HWND, UINT, UINT_PTR, DWORD)
-{
- if ( MirandaWaitForMutex(hStackMutex)) {
- for (int j=0; j < threads.getCount(); j++) {
- THREAD_WAIT_ENTRY* p = threads[j];
- char szModuleName[ MAX_PATH ];
- GetModuleFileNameA(p->hOwner, szModuleName, sizeof(szModuleName));
- Netlib_Logf(NULL, "Thread %p was abnormally terminated because module '%s' didn't release it. Entry point: %p",
- p->hThread, szModuleName, p->addr);
- TerminateThread(p->hThread, 9999);
- CloseHandle(p->hThread);
- mir_free(p);
- }
-
- threads.destroy();
-
- ReleaseMutex(hStackMutex);
- SetEvent(hThreadQueueEmpty);
- }
-}
-
-void KillObjectThreads(void* owner)
-{
- if (owner == NULL)
- return;
-
- WaitForSingleObject(hStackMutex, INFINITE);
-
- HANDLE* threadPool = (HANDLE*)alloca(threads.getCount()*sizeof(HANDLE));
- int threadCount = 0;
-
- for (int j = threads.getCount(); j--;) {
- THREAD_WAIT_ENTRY* p = threads[j];
- if (p->pObject == owner)
- threadPool[ threadCount++ ] = p->hThread;
- }
- ReleaseMutex(hStackMutex);
-
- // is there anything to kill?
- if (threadCount > 0) {
- if ( WaitForMultipleObjects(threadCount, threadPool, TRUE, 5000) == WAIT_TIMEOUT) {
- // forcibly kill all remaining threads after 5 secs
- WaitForSingleObject(hStackMutex, INFINITE);
- for (int j = threads.getCount()-1; j >= 0; j--) {
- THREAD_WAIT_ENTRY* p = threads[j];
- if (p->pObject == owner) {
- TerminateThread(p->hThread, 9999);
- CloseHandle(p->hThread);
- threads.remove(j);
- mir_free(p);
- }
- }
- ReleaseMutex(hStackMutex);
- }
- }
-}
-
-static void UnwindThreadWait(void)
-{
- // acquire the list and wake up any alertable threads
- if ( MirandaWaitForMutex(hStackMutex)) {
- int j;
- for (j=0; j < threads.getCount(); j++)
- QueueUserAPC(DummyAPCFunc, threads[j]->hThread, 0);
- ReleaseMutex(hStackMutex);
- }
-
- // give all unclosed threads 5 seconds to close
- SetTimer(NULL, 0, 5000, KillAllThreads);
-
- // wait til the thread list is empty
- MirandaWaitForMutex(hThreadQueueEmpty);
-}
-
-/////////////////////////////////////////////////////////////////////////////////////////
typedef LONG (WINAPI *pNtQIT)(HANDLE, LONG, PVOID, ULONG, PULONG);
#define ThreadQuerySetWin32StartAddress 9
@@ -380,55 +147,6 @@ void* GetCurrentThreadEntryPoint()
return (void*)dwStartAddress;
}
-INT_PTR UnwindThreadPush(WPARAM wParam, LPARAM lParam)
-{
- ResetEvent(hThreadQueueEmpty); // thread list is not empty
- if ( WaitForSingleObject(hStackMutex, INFINITE) == WAIT_OBJECT_0) {
- THREAD_WAIT_ENTRY* p = (THREAD_WAIT_ENTRY*)mir_calloc(sizeof(THREAD_WAIT_ENTRY));
-
- DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &p->hThread, 0, FALSE, DUPLICATE_SAME_ACCESS);
- p->dwThreadId = GetCurrentThreadId();
- p->pObject = (void*)wParam;
- p->hOwner = GetInstByAddress((void*)lParam);
- p->addr = (void*)lParam;
- threads.insert(p);
-
- //Netlib_Logf(NULL, "*** pushing thread %x[%x] (%d)", hThread, GetCurrentThreadId(), threads.count);
- ReleaseMutex(hStackMutex);
- } //if
- return 0;
-}
-
-INT_PTR UnwindThreadPop(WPARAM, LPARAM)
-{
- if ( WaitForSingleObject(hStackMutex, INFINITE) == WAIT_OBJECT_0) {
- DWORD dwThreadId=GetCurrentThreadId();
- int j;
- //Netlib_Logf(NULL, "*** popping thread %x, %d threads left", dwThreadId, threads.count);
- for (j=0; j < threads.getCount(); j++) {
- THREAD_WAIT_ENTRY* p = threads[j];
- if (p->dwThreadId == dwThreadId) {
- SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
- CloseHandle(p->hThread);
- threads.remove(j);
- mir_free(p);
-
- if ( !threads.getCount()) {
- threads.destroy();
- ReleaseMutex(hStackMutex);
- SetEvent(hThreadQueueEmpty); // thread list is empty now
- return 0;
- }
-
- ReleaseMutex(hStackMutex);
- return 0;
- } //if
- } //for
- ReleaseMutex(hStackMutex);
- } //if
- return 1;
-}
-
INT_PTR MirandaIsTerminated(WPARAM, LPARAM)
{
return WaitForSingleObject(hMirandaShutdown, 0) == WAIT_OBJECT_0;
@@ -449,14 +167,6 @@ static void __cdecl compactHeapsThread(void*)
} //while
}
-LRESULT CALLBACK APCWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
-{
- if (msg == WM_NULL) SleepEx(0, TRUE);
- if (msg == WM_TIMECHANGE) RecalculateTime();
- return DefWindowProc(hwnd, msg, wParam, lParam);
-}
-
-HWND hAPCWindow=NULL;
void (*SetIdleCallback) (void)=NULL;
static INT_PTR SystemSetIdleCallback(WPARAM, LPARAM lParam)
@@ -485,17 +195,6 @@ static INT_PTR SystemGetIdle(WPARAM, LPARAM lParam)
return 0;
}
-static DWORD MsgWaitForMultipleObjectsExWorkaround(DWORD nCount, const HANDLE *pHandles,
- DWORD dwMsecs, DWORD dwWakeMask, DWORD dwFlags)
-{
- DWORD rc;
- if (msgWaitForMultipleObjectsEx != NULL)
- return msgWaitForMultipleObjectsEx(nCount, pHandles, dwMsecs, dwWakeMask, dwFlags);
- rc=MsgWaitForMultipleObjects(nCount, pHandles, FALSE, 50, QS_ALLINPUT);
- if (rc == WAIT_TIMEOUT) rc=WaitForMultipleObjectsEx(nCount, pHandles, FALSE, 20, TRUE);
- return rc;
-}
-
static int SystemShutdownProc(WPARAM, LPARAM)
{
UnloadDefaultModules();
@@ -545,7 +244,7 @@ void ParseCommandLine()
if (p) {
HANDLE hProcess = OpenProcess(SYNCHRONIZE, FALSE, atol(p+9));
if (hProcess) {
- DialogBoxParam(hMirandaInst, MAKEINTRESOURCE(IDD_WAITRESTART), NULL, WaitForProcessDlgProc, (LPARAM)hProcess);
+ DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_WAITRESTART), NULL, WaitForProcessDlgProc, (LPARAM)hProcess);
CloseHandle(hProcess);
}
}
@@ -558,7 +257,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
HMODULE hUser32, hThemeAPI, hDwmApi, hShFolder = NULL;
int result = 0;
- hMirandaInst = hInstance;
+ hInst = hInstance;
setlocale(LC_ALL, "");
@@ -635,9 +334,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
if ( IsWinVer7Plus())
CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_ALL, IID_ITaskbarList3, (void**)&pTaskbarInterface);
- InitialiseModularEngine();
-// ParseCommandLine();
-
if ( LoadDefaultModules()) {
NotifyEventHooks(hShutdownEvent, 0, 0);
UnloadDefaultModules();
@@ -659,7 +355,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
MSG msg;
DWORD rc;
BOOL dying=FALSE;
- rc=MsgWaitForMultipleObjectsExWorkaround(waitObjectCount, hWaitObjects, INFINITE, QS_ALLINPUT, MWMO_ALERTABLE);
+ rc = MsgWaitForMultipleObjectsEx(waitObjectCount, hWaitObjects, INFINITE, QS_ALLINPUT, MWMO_ALERTABLE);
if (rc >= WAIT_OBJECT_0 && rc < WAIT_OBJECT_0 + waitObjectCount) {
rc -= WAIT_OBJECT_0;
CallService(pszWaitServices[rc], (WPARAM) hWaitObjects[rc], 0);
@@ -697,11 +393,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
exit:
UnloadNewPluginsModule();
- DestroyModularEngine();
- CloseHandle(hStackMutex);
CloseHandle(hMirandaShutdown);
CloseHandle(hThreadQueueEmpty);
- DestroyWindow(hAPCWindow);
if (pTaskbarInterface)
pTaskbarInterface->Release();
@@ -769,7 +462,7 @@ static INT_PTR GetMirandaVersionText(WPARAM wParam, LPARAM lParam)
INT_PTR WaitOnHandle(WPARAM wParam, LPARAM lParam)
{
- if (waitObjectCount>=MAXIMUM_WAIT_OBJECTS-1)
+ if (waitObjectCount >= MAXIMUM_WAIT_OBJECTS-1)
return 1;
hWaitObjects[waitObjectCount] = (HANDLE)wParam;
@@ -842,26 +535,18 @@ INT_PTR GetListInterface(WPARAM, LPARAM lParam)
if (li == NULL)
return 1;
- switch(li->cbSize) {
- case LIST_INTERFACE_V3_SIZE:
- li->List_Copy = List_Copy;
- li->List_ObjCopy = List_ObjCopy;
-
- case LIST_INTERFACE_V2_SIZE:
- li->List_InsertPtr = List_InsertPtr;
- li->List_RemovePtr = List_RemovePtr;
-
- case LIST_INTERFACE_V1_SIZE:
- li->List_Create = List_Create;
- li->List_Destroy = List_Destroy;
- li->List_Find = List_Find;
- li->List_GetIndex = List_GetIndex;
- li->List_Insert = List_Insert;
- li->List_Remove = List_Remove;
- li->List_IndexOf = List_IndexOf;
- return 0;
- }
- return 1;
+ li->List_Copy = List_Copy;
+ li->List_ObjCopy = List_ObjCopy;
+ li->List_InsertPtr = List_InsertPtr;
+ li->List_RemovePtr = List_RemovePtr;
+ li->List_Create = List_Create;
+ li->List_Destroy = List_Destroy;
+ li->List_Find = List_Find;
+ li->List_GetIndex = List_GetIndex;
+ li->List_Insert = List_Insert;
+ li->List_Remove = List_Remove;
+ li->List_IndexOf = List_IndexOf;
+ return 0;
}
INT_PTR GetUtfInterface(WPARAM, LPARAM lParam)
@@ -884,9 +569,9 @@ INT_PTR GetUtfInterface(WPARAM, LPARAM lParam)
utfi->utf8_decodecp = Utf8DecodeCP;
utfi->utf8_encode = Utf8Encode;
utfi->utf8_encodecp = Utf8EncodeCP;
- utfi->utf8_encodeW = Utf8EncodeUcs2;
+ utfi->utf8_encodeW = Utf8EncodeW;
if (utfi->cbSize > UTF8_INTERFACE_SIZEOF_V1)
- utfi->utf8_decodeW = Utf8DecodeUcs2;
+ utfi->utf8_decodeW = Utf8DecodeW;
if (utfi->cbSize > UTF8_INTERFACE_SIZEOF_V2)
utfi->utf8_lenW = Ucs2toUtf8Len;
@@ -895,28 +580,13 @@ INT_PTR GetUtfInterface(WPARAM, LPARAM lParam)
int LoadSystemModule(void)
{
- INITCOMMONCONTROLSEX icce = {0};
- icce.dwSize = sizeof(icce);
- icce.dwICC = ICC_WIN95_CLASSES | ICC_USEREX_CLASSES;
- InitCommonControlsEx(&icce);
+ hMirandaShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
+ hThreadQueueEmpty = CreateEvent(NULL, TRUE, TRUE, NULL);
- if ( IsWinVerXPPlus()) {
- hAPCWindow=CreateWindowEx(0, _T("ComboLBox"), NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
- SetClassLongPtr(hAPCWindow, GCL_STYLE, GetClassLongPtr(hAPCWindow, GCL_STYLE) | CS_DROPSHADOW);
- DestroyWindow(hAPCWindow);
- hAPCWindow = NULL;
- }
-
- hAPCWindow=CreateWindowEx(0, _T("STATIC"), NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL); // lame
- SetWindowLongPtr(hAPCWindow, GWLP_WNDPROC, (LONG_PTR)APCWndProc);
- hStackMutex=CreateMutex(NULL, FALSE, NULL);
- hMirandaShutdown=CreateEvent(NULL, TRUE, FALSE, NULL);
- hThreadQueueEmpty=CreateEvent(NULL, TRUE, TRUE, NULL);
-
- hShutdownEvent=CreateHookableEvent(ME_SYSTEM_SHUTDOWN);
- hPreShutdownEvent=CreateHookableEvent(ME_SYSTEM_PRESHUTDOWN);
- hModulesLoadedEvent=CreateHookableEvent(ME_SYSTEM_MODULESLOADED);
- hOkToExitEvent=CreateHookableEvent(ME_SYSTEM_OKTOEXIT);
+ hShutdownEvent = CreateHookableEvent(ME_SYSTEM_SHUTDOWN);
+ hPreShutdownEvent = CreateHookableEvent(ME_SYSTEM_PRESHUTDOWN);
+ hModulesLoadedEvent = CreateHookableEvent(ME_SYSTEM_MODULESLOADED);
+ hOkToExitEvent = CreateHookableEvent(ME_SYSTEM_OKTOEXIT);
CreateServiceFunction(MS_SYSTEM_FORK_THREAD, ForkThreadService);
CreateServiceFunction(MS_SYSTEM_FORK_THREAD_EX, ForkThreadServiceEx);
@@ -931,9 +601,7 @@ int LoadSystemModule(void)
CreateServiceFunction(MS_SYSTEM_GET_LI, GetListInterface);
CreateServiceFunction(MS_SYSTEM_GET_MMI, GetMemoryManagerInterface);
CreateServiceFunction(MS_SYSTEM_GET_UTFI, GetUtfInterface);
- CreateServiceFunction(MS_SYSTEM_GETEXCEPTFILTER, GetExceptionFilter);
- CreateServiceFunction(MS_SYSTEM_SETEXCEPTFILTER, SetExceptionFilter);
-
- InitPathUtils();
+ CreateServiceFunction(MS_SYSTEM_GETEXCEPTFILTER, srvGetExceptionFilter);
+ CreateServiceFunction(MS_SYSTEM_SETEXCEPTFILTER, srvSetExceptionFilter);
return 0;
}
diff --git a/src/core/miranda.h b/src/core/miranda.h
index 40329e5013..7dfba64a66 100644
--- a/src/core/miranda.h
+++ b/src/core/miranda.h
@@ -115,51 +115,13 @@ extern LPFN_WSAADDRESSTOSTRINGA MyWSAAddressToString;
void PushFileEvent(HANDLE hContact, HANDLE hdbe, LPARAM lParam);
-/**** memory.cpp ***********************************************************************/
-
-#ifdef _STATIC
-void* mir_alloc(size_t);
-void* mir_calloc(size_t);
-void* mir_realloc(void* ptr, size_t);
-void mir_free(void* ptr);
-char* mir_strdup(const char* str);
-WCHAR* mir_wstrdup(const WCHAR* str);
-char* mir_strndup(const char* str, size_t len);
-
-int mir_snprintf(char *buffer, size_t count, const char* fmt, ...);
-int mir_sntprintf(TCHAR *buffer, size_t count, const TCHAR* fmt, ...);
-int mir_vsnprintf(char *buffer, size_t count, const char* fmt, va_list va);
-int mir_vsntprintf(TCHAR *buffer, size_t count, const TCHAR* fmt, va_list va);
-
-WCHAR* mir_a2u_cp(const char* src, int codepage);
-WCHAR* mir_a2u(const char* src);
-char* mir_u2a_cp(const wchar_t* src, int codepage);
-char* mir_u2a(const wchar_t* src);
-#endif
-
/**** miranda.cpp **********************************************************************/
-extern HINSTANCE hMirandaInst;
+extern HINSTANCE hInst;
extern HANDLE hOkToExitEvent, hModulesLoadedEvent;
-extern pfnExceptionFilter pMirandaExceptFilter;
-
-/**** modules.cpp **********************************************************************/
-
-int CallPluginEventHook(HINSTANCE hInst, HANDLE hEvent, WPARAM wParam, LPARAM lParam);
-void KillModuleEventHooks(HINSTANCE);
-void KillModuleServices(HINSTANCE);
-
-void KillObjectEventHooks(void* pObject);
-void KillObjectServices(void* pObject);
-void KillObjectThreads(void* pObject);
/**** utf.cpp **************************************************************************/
-char* Utf8Decode(char* str, wchar_t** ucs2);
-char* Utf8DecodeCP(char* str, int codepage, wchar_t** ucs2);
-
-wchar_t* Utf8DecodeUcs2(const char* str);
-
__forceinline char* Utf8DecodeA(const char* src)
{
char* tmp = mir_strdup(src);
@@ -167,62 +129,12 @@ __forceinline char* Utf8DecodeA(const char* src)
return tmp;
}
-
-char* Utf8Encode(const char* str);
-char* Utf8EncodeCP(const char* src, int codepage);
-
-char* Utf8EncodeUcs2(const wchar_t* str);
-
-int Ucs2toUtf8Len(const wchar_t *src);
-
-#define Utf8DecodeT Utf8DecodeUcs2
-#define Utf8EncodeT Utf8EncodeUcs2
-
-/**** langpack.cpp *********************************************************************/
-
-int LangPackGetDefaultCodePage();
-int LangPackGetDefaultLocale();
-TCHAR* LangPackPcharToTchar(const char* pszStr);
-char* LangPackTranslateString(struct LangPackMuuid* pUuid, const char *szEnglish, const int W);
-TCHAR* LangPackTranslateStringT(int hLangpack, const TCHAR* tszEnglish);
-
-unsigned int __fastcall hash(const void * key, unsigned int len);
-
-#pragma optimize("gt", on)
-__inline unsigned int hashstr(const char * key)
-{
- if (key == NULL) return 0;
- const unsigned int len = (unsigned int)strlen((const char*)key);
- return hash(key, len);
-}
-
-__inline unsigned int hashstr(const wchar_t * key)
-{
- if (key == NULL) return 0;
- const unsigned int len = (unsigned int)wcslen((const wchar_t*)key);
- return hash(key, len * sizeof(wchar_t));
-}
#pragma optimize("", on)
/**** options.cpp **********************************************************************/
HTREEITEM FindNamedTreeItemAtRoot(HWND hwndTree, const TCHAR* name);
-/**** path.cpp *************************************************************************/
-
-void CreatePathToFile(char* wszFilePath);
-void CreatePathToFileW(WCHAR* wszFilePath);
-
-int CreateDirectoryTree(const char *szDir);
-int CreateDirectoryTreeW(const WCHAR *szDir);
-
-int pathToAbsolute(const char *pSrc, char *pOut, char* base);
-int pathToAbsoluteW(const TCHAR *pSrc, TCHAR *pOut, TCHAR* base);
-
-#define pathToAbsoluteT pathToAbsoluteW
-#define CreatePathToFileT CreatePathToFileW
-#define CreateDirectoryTreeT CreateDirectoryTreeW
-
/**** skin2icons.cpp *******************************************************************/
HANDLE IcoLib_AddNewIcon(int hLangpack, SKINICONDESC* sid);
@@ -294,25 +206,10 @@ void OpenAccountOptions(PROTOACCOUNT* pa);
void LoadDbAccounts(void);
void WriteDbAccounts(void);
-INT_PTR CallProtoServiceInt(HANDLE hContact, const char* szModule, const char* szService, WPARAM, LPARAM);
-INT_PTR CallContactService(HANDLE hContact, const char *szProtoService, WPARAM, LPARAM);
-
-__inline static INT_PTR CallProtoService(const char* szModule, const char* szService, WPARAM wParam, LPARAM lParam)
-{
- return CallProtoServiceInt(NULL, szModule, szService, wParam, lParam);
-}
+INT_PTR CallProtoServiceInt(HANDLE hContact, const char* szModule, const char* szService, WPARAM wParam, LPARAM lParam);
/**** utils.cpp ************************************************************************/
-char* __fastcall rtrim(char* str);
-TCHAR* __fastcall rtrim(TCHAR* str);
-char* __fastcall ltrim(char* str);
-char* __fastcall ltrimp(char* str);
-__inline char* lrtrim(char* str) { return ltrim(rtrim(str)); };
-__inline char* lrtrimp(char* str) { return ltrimp(rtrim(str)); };
-
-bool __fastcall wildcmp(char * name, char * mask);
-
void HotkeyToName(TCHAR *buf, int size, BYTE shift, BYTE key);
WORD GetHotkeyValue(INT_PTR idHotkey);
@@ -350,5 +247,3 @@ public:
#define StrConvTu(x) x
#define StrConvA(x) StrConvAT(x)
#define StrConvU(x) x
-
-
diff --git a/src/core/modules.cpp b/src/core/modules.cpp
index ba846d0fd4..11e93f1354 100644
--- a/src/core/modules.cpp
+++ b/src/core/modules.cpp
@@ -2,7 +2,7 @@
Miranda IM: the free IM client for Microsoft* Windows*
-Copyright 2000-2009 Miranda ICQ/IM project,
+Copyright 2000-2009 Miranda ICQ/IM project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.
@@ -11,7 +11,7 @@ modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
-This program is distributed in the hope that it will be useful,
+This program 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.
@@ -23,66 +23,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "commonheaders.h"
#include <m_plugins.h>
-// list of hooks
-
-static int compareHooks(const THook* p1, const THook* p2)
-{
- return strcmp(p1->name, p2->name);
-}
-
-static LIST<THook> hooks(50, compareHooks);
-
-struct THookToMainThreadItem
-{
- THook* hook;
- HANDLE hDoneEvent;
- WPARAM wParam;
- LPARAM lParam;
- int result;
-};
-
-// list of services
-
-struct TService
-{
- DWORD nameHash;
- HINSTANCE hOwner;
- union {
- MIRANDASERVICE pfnService;
- MIRANDASERVICEPARAM pfnServiceParam;
- MIRANDASERVICEOBJ pfnServiceObj;
- MIRANDASERVICEOBJPARAM pfnServiceObjParam;
- };
- int flags;
- LPARAM lParam;
- void* object;
- char name[1];
-};
-
-LIST<TService> services(100, NumericKeySortT);
-
-typedef struct
-{
- HANDLE hDoneEvent;
- WPARAM wParam;
- LPARAM lParam;
- int result;
- const char *name;
-}
- TServiceToMainThreadItem;
-
// other static variables
static BOOL bServiceMode = FALSE;
-static CRITICAL_SECTION csHooks, csServices;
static DWORD mainThreadId;
-static int hookId = 1;
static HANDLE hMainThread;
static HANDLE hMissingService;
-static THook *pLastHook = NULL;
-
-HINSTANCE GetInstByAddress(void* codePtr);
-
-void LangPackDropUnusedItems(void);
void ParseCommandLine(); // core: IDD_WAITRESTART
int LoadSystemModule(void); // core: m_system.h services
@@ -134,7 +79,6 @@ void UnloadClcModule(void);
void UnloadContactListModule(void);
void UnloadEventsModule(void);
void UnloadIdleModule(void);
-void UnloadLangPackModule(void);
void UnloadSslModule(void);
void UnloadNetlibModule(void);
void UnloadNewPlugins(void);
@@ -236,576 +180,4 @@ void UnloadDefaultModules(void)
UnloadUpdateNotifyModule();
UnloadNetlibModule();
UnloadSslModule();
- UnloadLangPackModule();
-}
-
-int InitialiseModularEngine(void)
-{
- InitializeCriticalSection(&csHooks);
- InitializeCriticalSection(&csServices);
-
- mainThreadId=GetCurrentThreadId();
- DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &hMainThread, 0, FALSE, DUPLICATE_SAME_ACCESS);
-
- hMissingService = CreateHookableEvent(ME_SYSTEM_MISSINGSERVICE);
- return 0;
-}
-
-void DestroyModularEngine(void)
-{
- int i;
- THook* p;
- EnterCriticalSection(&csHooks);
- for (i=0; i < hooks.getCount(); i++) {
- p = hooks[i];
- if (p->subscriberCount)
- mir_free(p->subscriber);
- DeleteCriticalSection(&p->csHook);
- mir_free(p);
- }
- hooks.destroy();
- LeaveCriticalSection(&csHooks);
- DeleteCriticalSection(&csHooks);
-
- EnterCriticalSection(&csServices);
- for (i=0; i < services.getCount(); i++)
- mir_free(services[i]);
-
- services.destroy();
- LeaveCriticalSection(&csServices);
- DeleteCriticalSection(&csServices);
- CloseHandle(hMainThread);
-}
-
-///////////////////////////////HOOKS
-
-HANDLE CreateHookableEvent(const char *name)
-{
- THook* ret;
- int idx;
-
- if (name == NULL)
- return NULL;
-
- EnterCriticalSection(&csHooks);
- if ((idx = hooks.getIndex((THook*)name)) != -1) {
- LeaveCriticalSection(&csHooks);
- return NULL;
- }
-
- ret = (THook*)mir_alloc(sizeof(THook));
- strncpy(ret->name, name, sizeof(ret->name)); ret->name[ MAXMODULELABELLENGTH-1 ] = 0;
- ret->id = hookId++;
- ret->subscriberCount = 0;
- ret->subscriber = NULL;
- ret->pfnHook = NULL;
- InitializeCriticalSection(&ret->csHook);
- hooks.insert(ret);
-
- LeaveCriticalSection(&csHooks);
- return (HANDLE)ret;
-}
-
-int DestroyHookableEvent(HANDLE hEvent)
-{
- int idx;
- THook* p;
-
- EnterCriticalSection(&csHooks);
- if (pLastHook == (THook*)hEvent)
- pLastHook = NULL;
-
- if ((idx = hooks.getIndex((THook*)hEvent)) == -1) {
- LeaveCriticalSection(&csHooks);
- return 1;
- }
- p = hooks[idx];
- if (p->subscriberCount) {
- mir_free(p->subscriber);
- p->subscriber = NULL;
- p->subscriberCount = 0;
- }
- hooks.remove(idx);
- DeleteCriticalSection(&p->csHook);
- mir_free(p);
-
- LeaveCriticalSection(&csHooks);
- return 0;
-}
-
-int SetHookDefaultForHookableEvent(HANDLE hEvent, MIRANDAHOOK pfnHook)
-{
- THook* p = (THook*)hEvent;
-
- EnterCriticalSection(&csHooks);
- if (hooks.getIndex(p) != -1)
- p->pfnHook = pfnHook;
- LeaveCriticalSection(&csHooks);
- return 0;
-}
-
-int CallPluginEventHook(HINSTANCE hInst, HANDLE hEvent, WPARAM wParam, LPARAM lParam)
-{
- int returnVal = 0;
- THook* p = (THook*)hEvent;
- if (p == NULL)
- return -1;
-
- EnterCriticalSection(&p->csHook);
- for (int i = 0; i < p->subscriberCount; i++) {
- THookSubscriber* s = &p->subscriber[i];
- if (s->hOwner != hInst)
- continue;
-
- switch (s->type) {
- case 1: returnVal = s->pfnHook(wParam, lParam); break;
- case 2: returnVal = s->pfnHookParam(wParam, lParam, s->lParam); break;
- case 3: returnVal = s->pfnHookObj(s->object, wParam, lParam); break;
- case 4: returnVal = s->pfnHookObjParam(s->object, wParam, lParam, s->lParam); break;
- case 5: returnVal = SendMessage(s->hwnd, s->message, wParam, lParam); break;
- default: continue;
- }
- if (returnVal)
- break;
- }
-
- if (p->subscriberCount == 0 && p->pfnHook != 0)
- returnVal = p->pfnHook(wParam, lParam);
-
- LeaveCriticalSection(&p->csHook);
- return returnVal;
-}
-
-int CallHookSubscribers(HANDLE hEvent, WPARAM wParam, LPARAM lParam)
-{
- int returnVal = 0;
- THook* p = (THook*)hEvent;
- if (p == NULL)
- return -1;
-
- EnterCriticalSection(&p->csHook);
-
- // NOTE: We've got the critical section while all this lot are called. That's mostly safe, though.
- for (int i = 0; i < p->subscriberCount; i++) {
- THookSubscriber* s = &p->subscriber[i];
- switch (s->type) {
- case 1: returnVal = s->pfnHook(wParam, lParam); break;
- case 2: returnVal = s->pfnHookParam(wParam, lParam, s->lParam); break;
- case 3: returnVal = s->pfnHookObj(s->object, wParam, lParam); break;
- case 4: returnVal = s->pfnHookObjParam(s->object, wParam, lParam, s->lParam); break;
- case 5: returnVal = SendMessage(s->hwnd, s->message, wParam, lParam); break;
- default: continue;
- }
- if (returnVal)
- break;
- }
-
- // check for no hooks and call the default hook if any
- if (p->subscriberCount == 0 && p->pfnHook != 0)
- returnVal = p->pfnHook(wParam, lParam);
-
- LeaveCriticalSection(&p->csHook);
- return returnVal;
-}
-
-static int checkHook(HANDLE hHook)
-{
- if (hHook == NULL)
- return -1;
-
- EnterCriticalSection(&csHooks);
- if (pLastHook != hHook || !pLastHook) {
- if (hooks.getIndex((THook*)hHook) == -1) {
- LeaveCriticalSection(&csHooks);
- return -1;
- }
- pLastHook = (THook*)hHook;
- }
- LeaveCriticalSection(&csHooks);
- return 0;
-}
-
-static void CALLBACK HookToMainAPCFunc(ULONG_PTR dwParam)
-{
- THookToMainThreadItem* item = (THookToMainThreadItem*)dwParam;
-
- if (checkHook(item->hook) == -1)
- item->result = -1;
- else
- item->result = CallHookSubscribers(item->hook, item->wParam, item->lParam);
- SetEvent(item->hDoneEvent);
-}
-
-int NotifyEventHooks(HANDLE hEvent, WPARAM wParam, LPARAM lParam)
-{
- extern HWND hAPCWindow;
-
- if ( GetCurrentThreadId() != mainThreadId) {
- THookToMainThreadItem item;
-
- item.hDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
- item.hook = (THook*)hEvent;
- item.wParam = wParam;
- item.lParam = lParam;
-
- QueueUserAPC(HookToMainAPCFunc, hMainThread, (ULONG_PTR)&item);
- PostMessage(hAPCWindow, WM_NULL, 0, 0); // let it process APC even if we're in a common dialog
- WaitForSingleObject(item.hDoneEvent, INFINITE);
- CloseHandle(item.hDoneEvent);
- return item.result;
- }
-
- return (checkHook(hEvent) == -1) ? -1 : CallHookSubscribers(hEvent, wParam, lParam);
-}
-
-static HANDLE HookEventInt(int type, const char* name, MIRANDAHOOK hookProc, void* object, LPARAM lParam)
-{
- int idx;
- THook* p;
- HANDLE ret;
-
- EnterCriticalSection(&csHooks);
- if ((idx = hooks.getIndex((THook*)name)) == -1) {
- #ifdef _DEBUG
- OutputDebugStringA("Attempt to hook: \t");
- OutputDebugStringA(name);
- OutputDebugStringA("\n");
- #endif
- LeaveCriticalSection(&csHooks);
- return NULL;
- }
-
- p = hooks[ idx ];
- p->subscriber = (THookSubscriber*)mir_realloc(p->subscriber, sizeof(THookSubscriber)*(p->subscriberCount+1));
- p->subscriber[ p->subscriberCount ].type = type;
- p->subscriber[ p->subscriberCount ].pfnHook = hookProc;
- p->subscriber[ p->subscriberCount ].object = object;
- p->subscriber[ p->subscriberCount ].lParam = lParam;
- p->subscriber[ p->subscriberCount ].hOwner = GetInstByAddress(hookProc);
- p->subscriberCount++;
-
- ret = (HANDLE)((p->id << 16) | p->subscriberCount);
- LeaveCriticalSection(&csHooks);
- return ret;
-}
-
-HANDLE HookEvent(const char* name, MIRANDAHOOK hookProc)
-{
- return HookEventInt(1, name, hookProc, 0, 0);
-}
-
-HANDLE HookEventParam(const char* name, MIRANDAHOOKPARAM hookProc, LPARAM lParam)
-{
- return HookEventInt(2, name, (MIRANDAHOOK)hookProc, 0, lParam);
-}
-
-HANDLE HookEventObj(const char* name, MIRANDAHOOKOBJ hookProc, void* object)
-{
- return HookEventInt(3, name, (MIRANDAHOOK)hookProc, object, 0);
-}
-
-HANDLE HookEventObjParam(const char* name, MIRANDAHOOKOBJPARAM hookProc, void* object, LPARAM lParam)
-{
- return HookEventInt(4, name, (MIRANDAHOOK)hookProc, object, lParam);
-}
-
-HANDLE HookEventMessage(const char* name, HWND hwnd, UINT message)
-{
- int idx;
- THook* p;
- HANDLE ret;
-
- EnterCriticalSection(&csHooks);
- if ((idx = hooks.getIndex((THook*)name)) == -1) {
- #ifdef _DEBUG
- MessageBoxA(NULL, "Attempt to hook non-existant event", name, MB_OK);
- #endif
- LeaveCriticalSection(&csHooks);
- return NULL;
- }
-
- p = hooks[ idx ];
- p->subscriber = (THookSubscriber*)mir_realloc(p->subscriber, sizeof(THookSubscriber)*(p->subscriberCount+1));
- p->subscriber[ p->subscriberCount ].type = 5;
- p->subscriber[ p->subscriberCount ].hwnd = hwnd;
- p->subscriber[ p->subscriberCount ].message = message;
- p->subscriberCount++;
-
- ret = (HANDLE)((p->id << 16) | p->subscriberCount);
- LeaveCriticalSection(&csHooks);
- return ret;
-}
-
-int UnhookEvent(HANDLE hHook)
-{
- int i;
- THook* p = NULL;
-
- int hookId = (int)hHook >> 16;
- int subscriberId = ((int)hHook & 0xFFFF) - 1;
-
- if (hHook == NULL) return 0;
-
- EnterCriticalSection(&csHooks);
- for (i = 0; i < hooks.getCount(); i++) {
- if (hooks[i]->id == hookId) {
- p = hooks[i];
- break;
- } }
-
- if (p == NULL) {
- LeaveCriticalSection(&csHooks);
- return 1;
- }
-
- if (subscriberId >= p->subscriberCount || subscriberId < 0) {
- LeaveCriticalSection(&csHooks);
- return 1;
- }
-
- p->subscriber[subscriberId].type = 0;
- p->subscriber[subscriberId].pfnHook = NULL;
- p->subscriber[subscriberId].hOwner = NULL;
- while (p->subscriberCount && p->subscriber[p->subscriberCount-1].type == 0)
- p->subscriberCount--;
- if (p->subscriberCount == 0) {
- if (p->subscriber) mir_free(p->subscriber);
- p->subscriber = NULL;
- }
- LeaveCriticalSection(&csHooks);
- return 0;
-}
-
-void KillModuleEventHooks(HINSTANCE hInst)
-{
- int i, j;
-
- EnterCriticalSection(&csHooks);
- for (i = hooks.getCount()-1; i >= 0; i--) {
- if (hooks[i]->subscriberCount == 0)
- continue;
-
- for (j = hooks[i]->subscriberCount-1; j >= 0; j--) {
- if (hooks[i]->subscriber[j].hOwner == hInst) {
- char szModuleName[ MAX_PATH ];
- GetModuleFileNameA(hooks[i]->subscriber[j].hOwner, szModuleName, sizeof(szModuleName));
- Netlib_Logf(NULL, "A hook %08x for event '%s' was abnormally deleted because module '%s' didn't released it",
- hooks[i]->subscriber[j].pfnHook, hooks[i]->name, szModuleName);
- UnhookEvent((HANDLE)((hooks[i]->id << 16) + j + 1));
- if (hooks[i]->subscriberCount == 0)
- break;
- } } }
-
- LeaveCriticalSection(&csHooks);
-}
-
-void KillObjectEventHooks(void* pObject)
-{
- int i, j;
-
- EnterCriticalSection(&csHooks);
- for (i = hooks.getCount()-1; i >= 0; i--) {
- if (hooks[i]->subscriberCount == 0)
- continue;
-
- for (j = hooks[i]->subscriberCount-1; j >= 0; j--) {
- if (hooks[i]->subscriber[j].object == pObject) {
- UnhookEvent((HANDLE)((hooks[i]->id << 16) + j + 1));
- if (hooks[i]->subscriberCount == 0)
- break;
- } } }
-
- LeaveCriticalSection(&csHooks);
-}
-
-/////////////////////SERVICES
-
-static __inline TService* FindServiceByName(const char *name)
-{
- unsigned hash = hashstr(name);
- return services.find((TService*)&hash);
-}
-
-static HANDLE CreateServiceInt(int type, const char *name, MIRANDASERVICE serviceProc, void* object, LPARAM lParam)
-{
- if (name == NULL)
- return NULL;
-
- TService tmp;
- tmp.nameHash = hashstr(name);
-
- EnterCriticalSection(&csServices);
-
- if (services.getIndex(&tmp) != -1) {
- LeaveCriticalSection(&csServices);
- return NULL;
- }
-
- TService* p = (TService*)mir_alloc(sizeof(*p) + strlen(name));
- strcpy(p->name, name);
- p->nameHash = tmp.nameHash;
- p->pfnService = serviceProc;
- p->hOwner = GetInstByAddress(serviceProc);
- p->flags = type;
- p->lParam = lParam;
- p->object = object;
- services.insert(p);
-
- LeaveCriticalSection(&csServices);
- return (HANDLE)tmp.nameHash;
-}
-
-HANDLE CreateServiceFunction(const char *name, MIRANDASERVICE serviceProc)
-{
- return CreateServiceInt(0, name, serviceProc, 0, 0);
-}
-
-HANDLE CreateServiceFunctionParam(const char *name, MIRANDASERVICEPARAM serviceProc, LPARAM lParam)
-{
- return CreateServiceInt(1, name, (MIRANDASERVICE)serviceProc, 0, lParam);
-}
-
-HANDLE CreateServiceFunctionObj(const char *name, MIRANDASERVICEOBJ serviceProc, void* object)
-{
- return CreateServiceInt(2, name, (MIRANDASERVICE)serviceProc, object, 0);
-}
-
-HANDLE CreateServiceFunctionObjParam(const char *name, MIRANDASERVICEOBJPARAM serviceProc, void* object, LPARAM lParam)
-{
- return CreateServiceInt(3, name, (MIRANDASERVICE)serviceProc, object, lParam);
-}
-
-int DestroyServiceFunction(HANDLE hService)
-{
- int idx;
-
- EnterCriticalSection(&csServices);
- if ((idx = services.getIndex((TService*)&hService)) != -1) {
- mir_free(services[idx]);
- services.remove(idx);
- }
-
- LeaveCriticalSection(&csServices);
- return 0;
-}
-
-int ServiceExists(const char *name)
-{
- if (name == NULL)
- return FALSE;
-
- EnterCriticalSection(&csServices);
- int ret = FindServiceByName(name) != NULL;
- LeaveCriticalSection(&csServices);
- return ret;
-}
-
-INT_PTR CallService(const char *name, WPARAM wParam, LPARAM lParam)
-{
- #ifdef _DEBUG
- if (name == NULL) {
- MessageBoxA(0, "Someone tried to CallService(NULL, ..) see stack trace for details", "", 0);
- DebugBreak();
- return CALLSERVICE_NOTFOUND;
- }
- #else
- if (name == NULL) return CALLSERVICE_NOTFOUND;
- #endif
-
- EnterCriticalSection(&csServices);
- TService *pService = FindServiceByName(name);
- if (pService == NULL) {
- LeaveCriticalSection(&csServices);
- #ifdef _DEBUG
- //MessageBoxA(NULL, "Attempt to call non-existant service", name, MB_OK);
- OutputDebugStringA("Missing service called: \t");
- OutputDebugStringA(name);
- OutputDebugStringA("\n");
- #endif
-/* { MISSING_SERVICE_PARAMS params = { name, wParam, lParam };
- int result = NotifyEventHooks(hMissingService, 0, (LPARAM)&params);
- if (result != 0)
- return params.lParam;
- } */
- return CALLSERVICE_NOTFOUND;
- }
-
- MIRANDASERVICE pfnService = pService->pfnService;
- int flags = pService->flags;
- LPARAM fnParam = pService->lParam;
- void* object = pService->object;
- LeaveCriticalSection(&csServices);
- switch(flags) {
- case 1: return ((MIRANDASERVICEPARAM)pfnService)(wParam, lParam, fnParam);
- case 2: return ((MIRANDASERVICEOBJ)pfnService)(object, wParam, lParam);
- case 3: return ((MIRANDASERVICEOBJPARAM)pfnService)(object, wParam, lParam, fnParam);
- default: return pfnService(wParam, lParam);
-} }
-
-static void CALLBACK CallServiceToMainAPCFunc(ULONG_PTR dwParam)
-{
- TServiceToMainThreadItem *item = (TServiceToMainThreadItem*) dwParam;
- item->result = CallService(item->name, item->wParam, item->lParam);
- SetEvent(item->hDoneEvent);
-}
-
-INT_PTR CallServiceSync(const char *name, WPARAM wParam, LPARAM lParam)
-{
- extern HWND hAPCWindow;
-
- if (name == NULL) return CALLSERVICE_NOTFOUND;
- // the service is looked up within the main thread, since the time it takes
- // for the APC queue to clear the service being called maybe removed.
- // even thou it may exists before the call, the critsec can't be locked between calls.
- if (GetCurrentThreadId() != mainThreadId) {
- TServiceToMainThreadItem item;
- item.wParam = wParam;
- item.lParam = lParam;
- item.name = name;
- item.hDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
- QueueUserAPC(CallServiceToMainAPCFunc, hMainThread, (ULONG_PTR) &item);
- PostMessage(hAPCWindow, WM_NULL, 0, 0); // let this get processed in its own time
- WaitForSingleObject(item.hDoneEvent, INFINITE);
- CloseHandle(item.hDoneEvent);
- return item.result;
- }
-
- return CallService(name, wParam, lParam);
-}
-
-int CallFunctionAsync(void (__stdcall *func)(void *), void *arg)
-{
- extern HWND hAPCWindow;
- int r = QueueUserAPC((void (__stdcall *)(ULONG_PTR))func, hMainThread, (ULONG_PTR)arg);
- PostMessage(hAPCWindow, WM_NULL, 0, 0);
- return r;
-}
-
-void KillModuleServices(HINSTANCE hInst)
-{
- int i;
-
- EnterCriticalSection(&csServices);
- for (i = services.getCount()-1; i >= 0; i--) {
- if (services[i]->hOwner == hInst) {
- char szModuleName[ MAX_PATH ];
- GetModuleFileNameA(services[i]->hOwner, szModuleName, sizeof(szModuleName));
- Netlib_Logf(NULL, "A service function '%s' was abnormally deleted because module '%s' didn't released it",
- services[i]->name, szModuleName);
- DestroyServiceFunction((HANDLE)services[i]->nameHash);
- } }
-
- LeaveCriticalSection(&csServices);
-}
-
-void KillObjectServices(void* pObject)
-{
- int i;
-
- EnterCriticalSection(&csServices);
- for (i = services.getCount()-1; i >= 0; i--)
- if (services[i]->object == pObject)
- DestroyServiceFunction((HANDLE)services[i]->nameHash);
-
- LeaveCriticalSection(&csServices);
}