1
0

IPCWindow.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * IPCWindow.cpp
  3. * -------------
  4. * Purpose: Hidden window to receive file open commands from another OpenMPT instance
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "IPCWindow.h"
  11. #include "../common/version.h"
  12. #include "Mptrack.h"
  13. OPENMPT_NAMESPACE_BEGIN
  14. namespace IPCWindow
  15. {
  16. static constexpr TCHAR ClassName[] = _T("OpenMPT_IPC_Wnd");
  17. static HWND ipcWindow = nullptr;
  18. static LRESULT CALLBACK IPCWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  19. {
  20. if(uMsg == WM_COPYDATA)
  21. {
  22. const auto &copyData = *reinterpret_cast<const COPYDATASTRUCT *>(lParam);
  23. LRESULT result = 0;
  24. switch(static_cast<Function>(copyData.dwData))
  25. {
  26. case Function::Open:
  27. {
  28. std::size_t count = copyData.cbData / sizeof(WCHAR);
  29. const WCHAR* data = static_cast<const WCHAR *>(copyData.lpData);
  30. const std::wstring name = std::wstring(data, data + count);
  31. result = theApp.OpenDocumentFile(mpt::PathString::FromWide(name).AsNative().c_str()) ? 1 : 2;
  32. }
  33. break;
  34. case Function::SetWindowForeground:
  35. {
  36. auto mainWnd = theApp.GetMainWnd();
  37. if(mainWnd)
  38. {
  39. if(mainWnd->IsIconic())
  40. {
  41. mainWnd->ShowWindow(SW_RESTORE);
  42. }
  43. mainWnd->SetForegroundWindow();
  44. result = 1;
  45. } else
  46. {
  47. result = 0;
  48. }
  49. }
  50. break;
  51. case Function::GetVersion:
  52. {
  53. result = Version::Current().GetRawVersion();
  54. }
  55. break;
  56. case Function::GetArchitecture:
  57. {
  58. #if MPT_OS_WINDOWS
  59. result = static_cast<int32>(mpt::OS::Windows::GetProcessArchitecture());
  60. #else
  61. result = -1;
  62. #endif
  63. }
  64. break;
  65. case Function::HasSameBinaryPath:
  66. {
  67. std::size_t count = copyData.cbData / sizeof(WCHAR);
  68. const WCHAR* data = static_cast<const WCHAR *>(copyData.lpData);
  69. const std::wstring path = std::wstring(data, data + count);
  70. result = (theApp.GetInstallBinArchPath().ToWide() == path) ? 1 : 0;
  71. }
  72. break;
  73. case Function::HasSameSettingsPath:
  74. {
  75. std::size_t count = copyData.cbData / sizeof(WCHAR);
  76. const WCHAR* data = static_cast<const WCHAR *>(copyData.lpData);
  77. const std::wstring path = std::wstring(data, data + count);
  78. result = (theApp.GetConfigPath().ToWide() == path) ? 1 : 0;
  79. }
  80. break;
  81. default:
  82. result = 0;
  83. break;
  84. }
  85. return result;
  86. }
  87. return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
  88. }
  89. void Open(HINSTANCE hInstance)
  90. {
  91. WNDCLASS ipcWindowClass =
  92. {
  93. 0,
  94. IPCWindowProc,
  95. 0,
  96. 0,
  97. hInstance,
  98. nullptr,
  99. nullptr,
  100. nullptr,
  101. nullptr,
  102. ClassName
  103. };
  104. auto ipcAtom = RegisterClass(&ipcWindowClass);
  105. ipcWindow = CreateWindow(MAKEINTATOM(ipcAtom), _T("OpenMPT IPC Window"), 0, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, 0);
  106. }
  107. void Close()
  108. {
  109. ::DestroyWindow(ipcWindow);
  110. ipcWindow = nullptr;
  111. }
  112. LRESULT SendIPC(HWND ipcWnd, Function function, mpt::const_byte_span data)
  113. {
  114. if(!ipcWnd)
  115. {
  116. return 0;
  117. }
  118. if(!mpt::in_range<DWORD>(data.size()))
  119. {
  120. return 0;
  121. }
  122. COPYDATASTRUCT copyData{};
  123. copyData.dwData = static_cast<ULONG>(function);
  124. copyData.cbData = mpt::saturate_cast<DWORD>(data.size());
  125. copyData.lpData = const_cast<void*>(mpt::void_cast<const void*>(data.data()));
  126. return ::SendMessage(ipcWnd, WM_COPYDATA, 0, reinterpret_cast<LPARAM>(&copyData));
  127. }
  128. HWND FindIPCWindow()
  129. {
  130. return ::FindWindow(ClassName, nullptr);
  131. }
  132. struct EnumWindowState
  133. {
  134. FlagSet<InstanceRequirements> require;
  135. HWND result = nullptr;
  136. };
  137. static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
  138. {
  139. EnumWindowState &state = *reinterpret_cast<EnumWindowState*>(lParam);
  140. if(hwnd)
  141. {
  142. TCHAR className[256];
  143. MemsetZero(className);
  144. if(::GetClassName(hwnd, className, 256) > 0)
  145. {
  146. if(!_tcscmp(className, IPCWindow::ClassName))
  147. {
  148. if(state.require[SameVersion])
  149. {
  150. if(Version(static_cast<uint32>(SendIPC(hwnd, Function::GetVersion))) != Version::Current())
  151. {
  152. return TRUE; // continue
  153. }
  154. }
  155. if(state.require[SameArchitecture])
  156. {
  157. if(SendIPC(hwnd, Function::GetArchitecture) != static_cast<int>(mpt::OS::Windows::GetProcessArchitecture()))
  158. {
  159. return TRUE; // continue
  160. }
  161. }
  162. if(state.require[SamePath])
  163. {
  164. if(SendIPC(hwnd, Function::HasSameBinaryPath, mpt::as_span(theApp.GetInstallBinArchPath().ToWide())) != 1)
  165. {
  166. return TRUE; // continue
  167. }
  168. }
  169. if(state.require[SameSettings])
  170. {
  171. if(SendIPC(hwnd, Function::HasSameSettingsPath, mpt::as_span(theApp.GetConfigPath().ToWide())) != 1)
  172. {
  173. return TRUE; // continue
  174. }
  175. }
  176. state.result = hwnd;
  177. return TRUE; // continue
  178. //return FALSE; // done
  179. }
  180. }
  181. }
  182. return TRUE; // continue
  183. }
  184. HWND FindIPCWindow(FlagSet<InstanceRequirements> require)
  185. {
  186. EnumWindowState state;
  187. state.require = require;
  188. if(::EnumWindows(&EnumWindowsProc, reinterpret_cast<LPARAM>(&state)) == 0)
  189. {
  190. return nullptr;
  191. }
  192. return state.result;
  193. }
  194. bool SendToIPC(const std::vector<mpt::PathString> &filenames)
  195. {
  196. HWND ipcWnd = FindIPCWindow();
  197. if(!ipcWnd)
  198. {
  199. return false;
  200. }
  201. DWORD processID = 0;
  202. GetWindowThreadProcessId(ipcWnd, &processID);
  203. AllowSetForegroundWindow(processID);
  204. SendIPC(ipcWnd, Function::SetWindowForeground);
  205. for(const auto &filename : filenames)
  206. {
  207. if(SendIPC(ipcWnd, Function::Open, mpt::as_span(filename.ToWide())) == 0)
  208. {
  209. return false;
  210. }
  211. }
  212. return true;
  213. }
  214. }
  215. OPENMPT_NAMESPACE_END