summaryrefslogtreecommitdiff
path: root/plugins/ZeroSwitch/src/ZeroSwitch.cpp
blob: 1230b92a2eced80c881784910f5f8734f878aee8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// ZeroSwitch.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"

HINSTANCE hInst;
HHOOK hHook;
HWND hDummyWnd = nullptr, hHelperWnd = nullptr, hMirandaWnd = nullptr;
int hLangpack;
CLIST_INTERFACE *pcli;

PLUGININFOEX pluginInfo = {
	sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE,
	// {3F1657B1-69CB-4992-9CFC-226C808A5202}
	{ 0x3f1657b1, 0x69cb, 0x4992, { 0x9c, 0xfc, 0x22, 0x6c, 0x80, 0x8a, 0x52, 0x2 } }
};

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD, LPVOID)
{
	hInst = hinstDLL;
	return TRUE;
}

extern "C" __declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD)
{
	return &pluginInfo;
}

LRESULT CALLBACK HelperProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_ACTIVATE:
		if (LOWORD(wParam) != WA_INACTIVE) // Helper window got activated
			SetActiveWindow(hMirandaWnd);
		break;
	case WM_DESTROY:
		if (hWnd == hHelperWnd)
			hHelperWnd = nullptr;
		else
			hDummyWnd = nullptr;
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

void CreateHelperWnd()
{
	WNDCLASSEX wcex;
	// First we must check if Miranda's main window has the "Title bar" border style...
	if ((GetWindowLongPtr(hMirandaWnd, GWL_STYLE) & WS_CAPTION) && !(GetWindowLongPtr(hMirandaWnd, GWL_EXSTYLE) & WS_EX_TOOLWINDOW))
		return;

	if (hHelperWnd)
		return; // We need only one helper window
	// Widows taskbar workaround
	// We don't want our helper window to be displayed on Windows taskbar.
	// So first of all we have to create an invisible window and then set it as
	// parent to our window.

	// Register windows class
	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = HelperProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInst;
	wcex.hIcon = Skin_LoadIcon(SKINICON_OTHER_MIRANDA, true);
	wcex.hCursor = nullptr;
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wcex.lpszMenuName = nullptr;
	wcex.lpszClassName = L"ZeroSwitchHlp";
	wcex.hIconSm = Skin_LoadIcon(SKINICON_OTHER_MIRANDA);

	if (NULL == RegisterClassEx(&wcex))
		return; // wtf

	hDummyWnd = CreateWindow(L"ZeroSwitchHlp", L"", WS_POPUP, 0, 0, 0, 0, nullptr, nullptr, hInst, nullptr);
	if (!hDummyWnd)
		UnregisterClass(L"ZeroSwitchHlp", hInst);
	hHelperWnd = CreateWindow(L"ZeroSwitchHlp", L"Miranda NG", WS_OVERLAPPEDWINDOW | WS_VISIBLE, -100, -100, 90, 90, hDummyWnd, nullptr, hInst, nullptr);
	if (!hHelperWnd)
	{
		DestroyWindow(hDummyWnd);
		UnregisterClass(L"ZeroSwitchHlp", hInst);
	}
}

void DestroyHelperWnd()
{
	if (hHelperWnd)
	{
		DestroyWindow(hHelperWnd);
		DestroyWindow(hDummyWnd);
		UnregisterClass(L"ZeroSwitchHlp", hInst);
	}
}

LRESULT CALLBACK CallWndRetProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	PCWPRETSTRUCT pMes;

	if (nCode >= 0)
	{
		pMes = (PCWPRETSTRUCT)lParam; // Get message details
		if (!hMirandaWnd)
			hMirandaWnd = pcli->hwndContactList;

		if (pMes->hwnd == hMirandaWnd)
		{
			if (pMes->message == WM_SHOWWINDOW) // We are interested only in this message
			{
				if (pMes->wParam == TRUE) // The window is being shown
					CreateHelperWnd();
				else
					DestroyHelperWnd();
			}
		}
	}
	return CallNextHookEx(nullptr, nCode, wParam, lParam); // Pass the message to other hooks in chain
}

extern "C" int __declspec(dllexport) Load(void)
{
	mir_getLP(&pluginInfo);
 	pcli = Clist_GetInterface();

	if (IsWinVerVistaPlus()) {
		MessageBox(nullptr, TranslateT("Plugin works under Windows XP only"), TranslateT("ZeroSwitch plugin failed"), MB_ICONSTOP);
		return 1;
	}

	// Let's setup shop :)
	hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, CallWndRetProc, nullptr, GetCurrentThreadId());
	if (hHook == nullptr)
		MessageBox(nullptr, TranslateT("Oops, we've got a big hook error here :("), TranslateT("ZeroSwitch plugin failed"), MB_ICONSTOP);
	return 0;
}

extern "C" int __declspec(dllexport) Unload(void)
{
	if (hHook)
		UnhookWindowsHookEx(hHook);
	DestroyHelperWnd();
	return 0;
}