summaryrefslogtreecommitdiff
path: root/src/mir_core/subclass.cpp
blob: ca5dc88d2b2e3be5b969855993b59d731b559929 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
Copyright (C) 2012-14 Miranda NG team (http://miranda-ng.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.

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.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "commonheaders.h"

struct MSubclassData
{
	HWND     m_hWnd;

	int      m_iHooks;
	WNDPROC *m_hooks;
	WNDPROC  m_origWndProc;

	~MSubclassData()
	{
		free(m_hooks);
	}
};

static LIST<MSubclassData> arSubclass(10, LIST<MSubclassData>::FTSortFunc(HandleKeySortT));

/////////////////////////////////////////////////////////////////////////////////////////

static LRESULT CALLBACK MSubclassWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	MSubclassData *p = arSubclass.find((MSubclassData*)&hwnd);
	if (p != NULL) {
		if (p->m_iHooks)
			return p->m_hooks[p->m_iHooks-1](hwnd, uMsg, wParam, lParam);

		return p->m_origWndProc(hwnd, uMsg, wParam, lParam);
	}

	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

MIR_CORE_DLL(void) mir_subclassWindow(HWND hWnd, WNDPROC wndProc)
{
	MSubclassData *p = arSubclass.find((MSubclassData*)&hWnd);
	if (p == NULL) {
		p = new MSubclassData;
		p->m_hWnd = hWnd;
		p->m_origWndProc = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)MSubclassWndProc);
		p->m_iHooks = 0;
		p->m_hooks = (WNDPROC*)malloc( sizeof(WNDPROC));
		arSubclass.insert(p);
	}
	else {
		for (int i=0; i < p->m_iHooks; i++)
			if (p->m_hooks[i] == wndProc)
				return;

		p->m_hooks = (WNDPROC*)realloc(p->m_hooks, (p->m_iHooks+1)*sizeof(WNDPROC));
	}

	p->m_hooks[p->m_iHooks++] = wndProc;
}

MIR_CORE_DLL(void) mir_subclassWindowFull(HWND hWnd, WNDPROC wndProc, WNDPROC oldWndProc)
{
	MSubclassData *p = arSubclass.find((MSubclassData*)&hWnd);
	if (p == NULL) {
		p = new MSubclassData;
		p->m_hWnd = hWnd;
		p->m_origWndProc = oldWndProc;
		p->m_iHooks = 0;
		p->m_hooks = (WNDPROC*)malloc( sizeof(WNDPROC));
		arSubclass.insert(p);

		SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)MSubclassWndProc);
	}
	else {
		for (int i=0; i < p->m_iHooks; i++)
			if (p->m_hooks[i] == wndProc)
				return;

		p->m_hooks = (WNDPROC*)realloc(p->m_hooks, (p->m_iHooks+1)*sizeof(WNDPROC));
	}

	p->m_hooks[p->m_iHooks++] = wndProc;
}

/////////////////////////////////////////////////////////////////////////////////////////

static void removeHook(MSubclassData *p, int idx)
{
	WNDPROC saveProc = p->m_hooks[idx];

	// untie hook from a window to prevent calling mir_callNextSubclass from saveProc
	for (int i=idx+1; i < p->m_iHooks; i++)
		p->m_hooks[i-1] = p->m_hooks[i];
	p->m_iHooks--;
}

MIR_CORE_DLL(void) mir_unsubclassWindow(HWND hWnd, WNDPROC wndProc)
{
	MSubclassData *p = arSubclass.find((MSubclassData*)&hWnd);
	if (p == NULL)
		return;

	for (int i=0; i < p->m_iHooks; i++) {
		if (p->m_hooks[i] == wndProc) {
			removeHook(p, i);
			i--;
		}
	}

	if (p->m_iHooks == 0) {
		arSubclass.remove(p);
		delete p;
	}
}

/////////////////////////////////////////////////////////////////////////////////////////

MIR_CORE_DLL(LRESULT) mir_callNextSubclass(HWND hWnd, WNDPROC wndProc, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	MSubclassData *p = arSubclass.find((MSubclassData*)&hWnd);
	if (p == NULL)
		return DefWindowProc(hWnd, uMsg, wParam, lParam);

	for (int i=p->m_iHooks-1; i >= 0; i--) {
		if (p->m_hooks[i] != wndProc)
			continue;

		// next hook exists, call it
		if (i != 0)
			return p->m_hooks[i-1](hWnd, uMsg, wParam, lParam);

		// last hook called, ping the default window procedure
		if (uMsg != WM_DESTROY)
			return p->m_origWndProc(hWnd, uMsg, wParam, lParam);

		WNDPROC saveProc = p->m_origWndProc;
		arSubclass.remove(p);
		delete p;

		SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)saveProc);
		return saveProc(hWnd, uMsg, wParam, lParam);
	}

	// invalid / closed hook
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

MIR_CORE_DLL(void) KillModuleSubclassing(HMODULE hInst)
{
	for (int i=arSubclass.getCount()-1; i >= 0; i--) {
		MSubclassData *p = arSubclass[i];
		for (int j=0; j < p->m_iHooks; j++) {
			if ( GetInstByAddress(p->m_hooks[j]) == hInst) {
				removeHook(p, j);
				j--;
			}
		}

		if (p->m_iHooks == 0) {
			arSubclass.remove(p);
			delete p;
		}
	}
}