aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/mptrack/IPCWindow.cpp
blob: 9ab159ac08c88bffce8989ce3207ae95a83a7395 (plain) (blame)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* IPCWindow.cpp
* -------------
* Purpose: Hidden window to receive file open commands from another OpenMPT instance
* Notes  : (currently none)
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/


#include "stdafx.h"
#include "IPCWindow.h"

#include "../common/version.h"
#include "Mptrack.h"


OPENMPT_NAMESPACE_BEGIN

namespace IPCWindow
{

	static constexpr TCHAR ClassName[] = _T("OpenMPT_IPC_Wnd");
	static HWND ipcWindow = nullptr;

	static LRESULT CALLBACK IPCWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
	{
		if(uMsg == WM_COPYDATA)
		{
			const auto &copyData = *reinterpret_cast<const COPYDATASTRUCT *>(lParam);
			LRESULT result = 0;
			switch(static_cast<Function>(copyData.dwData))
			{
			case Function::Open:
				{
					std::size_t count = copyData.cbData / sizeof(WCHAR);
					const WCHAR* data = static_cast<const WCHAR *>(copyData.lpData);
					const std::wstring name = std::wstring(data, data + count);
					result = theApp.OpenDocumentFile(mpt::PathString::FromWide(name).AsNative().c_str()) ? 1 : 2;
				}
				break;
			case Function::SetWindowForeground:
				{
					auto mainWnd = theApp.GetMainWnd();
					if(mainWnd)
					{
						if(mainWnd->IsIconic())
						{
							mainWnd->ShowWindow(SW_RESTORE);
						}
						mainWnd->SetForegroundWindow();
						result = 1;
					} else
					{
						result = 0;
					}
				}
				break;
			case Function::GetVersion:
				{
					result = Version::Current().GetRawVersion();
				}
				break;
			case Function::GetArchitecture:
				{
					#if MPT_OS_WINDOWS
						result = static_cast<int32>(mpt::OS::Windows::GetProcessArchitecture());
					#else
						result = -1;
					#endif
				}
				break;
			case Function::HasSameBinaryPath:
				{
					std::size_t count = copyData.cbData / sizeof(WCHAR);
					const WCHAR* data = static_cast<const WCHAR *>(copyData.lpData);
					const std::wstring path = std::wstring(data, data + count);
					result = (theApp.GetInstallBinArchPath().ToWide() == path) ? 1 : 0;
				}
				break;
			case Function::HasSameSettingsPath:
				{
					std::size_t count = copyData.cbData / sizeof(WCHAR);
					const WCHAR* data = static_cast<const WCHAR *>(copyData.lpData);
					const std::wstring path = std::wstring(data, data + count);
					result = (theApp.GetConfigPath().ToWide() == path) ? 1 : 0;
				}
				break;
			default:
				result = 0;
				break;
			}
			return result;
		}
		return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
	}

	void Open(HINSTANCE hInstance)
	{
		WNDCLASS ipcWindowClass =
		{
			0,
			IPCWindowProc,
			0,
			0,
			hInstance,
			nullptr,
			nullptr,
			nullptr,
			nullptr,
			ClassName
		};
		auto ipcAtom = RegisterClass(&ipcWindowClass);
		ipcWindow = CreateWindow(MAKEINTATOM(ipcAtom), _T("OpenMPT IPC Window"), 0, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, 0);
	}

	void Close()
	{
		::DestroyWindow(ipcWindow);
		ipcWindow = nullptr;
	}

	LRESULT SendIPC(HWND ipcWnd, Function function, mpt::const_byte_span data)
	{
		if(!ipcWnd)
		{
			return 0;
		}
		if(!mpt::in_range<DWORD>(data.size()))
		{
			return 0;
		}
		COPYDATASTRUCT copyData{};
		copyData.dwData = static_cast<ULONG>(function);
		copyData.cbData = mpt::saturate_cast<DWORD>(data.size());
		copyData.lpData = const_cast<void*>(mpt::void_cast<const void*>(data.data()));
		return ::SendMessage(ipcWnd, WM_COPYDATA, 0, reinterpret_cast<LPARAM>(&copyData));
	}

	HWND FindIPCWindow()
	{
		return ::FindWindow(ClassName, nullptr);
	}

	struct EnumWindowState
	{
		FlagSet<InstanceRequirements> require;
		HWND result = nullptr;
	};

	static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
	{
		EnumWindowState &state = *reinterpret_cast<EnumWindowState*>(lParam);
		if(hwnd)
		{
			TCHAR className[256];
			MemsetZero(className);
			if(::GetClassName(hwnd, className, 256) > 0)
			{
				if(!_tcscmp(className, IPCWindow::ClassName))
				{
					if(state.require[SameVersion])
					{
						if(Version(static_cast<uint32>(SendIPC(hwnd, Function::GetVersion))) != Version::Current())
						{
							return TRUE; // continue
						}
					}
					if(state.require[SameArchitecture])
					{
						if(SendIPC(hwnd, Function::GetArchitecture) != static_cast<int>(mpt::OS::Windows::GetProcessArchitecture()))
						{
							return TRUE; // continue
						}
					}
					if(state.require[SamePath])
					{
						if(SendIPC(hwnd, Function::HasSameBinaryPath, mpt::as_span(theApp.GetInstallBinArchPath().ToWide())) != 1)
						{
							return TRUE; // continue
						}
					}
					if(state.require[SameSettings])
					{
						if(SendIPC(hwnd, Function::HasSameSettingsPath, mpt::as_span(theApp.GetConfigPath().ToWide())) != 1)
						{
							return TRUE; // continue
						}
					}
					state.result = hwnd;
					return TRUE; // continue
					//return FALSE; // done
				}
			}
		}
		return TRUE; // continue
	}

	HWND FindIPCWindow(FlagSet<InstanceRequirements> require)
	{
		EnumWindowState state;
		state.require = require;
		if(::EnumWindows(&EnumWindowsProc, reinterpret_cast<LPARAM>(&state)) == 0)
		{
			return nullptr;
		}
		return state.result;
	}



	bool SendToIPC(const std::vector<mpt::PathString> &filenames)
	{
		HWND ipcWnd = FindIPCWindow();
		if(!ipcWnd)
		{
			return false;
		}
		DWORD processID = 0;
		GetWindowThreadProcessId(ipcWnd, &processID);
		AllowSetForegroundWindow(processID);
		SendIPC(ipcWnd, Function::SetWindowForeground);
		for(const auto &filename : filenames)
		{
			if(SendIPC(ipcWnd, Function::Open, mpt::as_span(filename.ToWide())) == 0)
			{
				return false;
			}
		}
		return true;
	}

}

OPENMPT_NAMESPACE_END