drop_target_windows.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**************************************************************************/
  2. /* drop_target_windows.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "drop_target_windows.h"
  31. #include "core/io/dir_access.h"
  32. #include "core/math/random_pcg.h"
  33. #include "core/os/time.h"
  34. #include <fileapi.h>
  35. // Helpers
  36. static String create_temp_dir() {
  37. Char16String buf;
  38. int bufsize = GetTempPathW(0, nullptr) + 1;
  39. buf.resize(bufsize);
  40. if (GetTempPathW(bufsize, (LPWSTR)buf.ptrw()) == 0) {
  41. return "";
  42. }
  43. String tmp_dir = String::utf16((const char16_t *)buf.ptr());
  44. RandomPCG gen(Time::get_singleton()->get_ticks_usec());
  45. const int attempts = 4;
  46. for (int i = 0; i < attempts; ++i) {
  47. uint32_t rnd = gen.rand();
  48. String dirname = "godot_tmp_" + String::num_uint64(rnd);
  49. String res_dir = tmp_dir.path_join(dirname);
  50. Char16String res_dir16 = res_dir.utf16();
  51. if (CreateDirectoryW((LPCWSTR)res_dir16.ptr(), nullptr)) {
  52. return res_dir;
  53. }
  54. }
  55. return "";
  56. }
  57. static bool remove_dir_recursive(const String &p_dir) {
  58. Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  59. if (dir_access->change_dir(p_dir) != OK) {
  60. return false;
  61. }
  62. return dir_access->erase_contents_recursive() == OK;
  63. }
  64. static bool stream2file(IStream *p_stream, FILEDESCRIPTORW *p_desc, const String &p_path) {
  65. if (DirAccess::make_dir_recursive_absolute(p_path.get_base_dir()) != OK) {
  66. return false;
  67. }
  68. Char16String path16 = p_path.utf16();
  69. DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
  70. if (p_desc->dwFlags & FD_ATTRIBUTES) {
  71. dwFlagsAndAttributes = p_desc->dwFileAttributes;
  72. }
  73. HANDLE file = CreateFileW(
  74. (LPCWSTR)path16.ptr(),
  75. GENERIC_WRITE,
  76. 0,
  77. nullptr,
  78. CREATE_NEW,
  79. dwFlagsAndAttributes,
  80. nullptr);
  81. if (!file) {
  82. return false;
  83. }
  84. const int bufsize = 4096;
  85. char buf[bufsize];
  86. ULONG nread = 0;
  87. DWORD nwritten = 0;
  88. HRESULT read_result = S_OK;
  89. bool result = true;
  90. while (true) {
  91. read_result = p_stream->Read(buf, bufsize, &nread);
  92. if (read_result != S_OK && read_result != S_FALSE) {
  93. result = false;
  94. goto cleanup;
  95. }
  96. if (!nread) {
  97. break;
  98. }
  99. while (nread > 0) {
  100. if (!WriteFile(file, buf, nread, &nwritten, nullptr) || !nwritten) {
  101. result = false;
  102. goto cleanup;
  103. }
  104. nread -= nwritten;
  105. }
  106. }
  107. cleanup:
  108. CloseHandle(file);
  109. return result;
  110. }
  111. // DropTargetWindows
  112. bool DropTargetWindows::is_valid_filedescriptor() {
  113. return cf_filedescriptor != 0 && cf_filecontents != 0;
  114. }
  115. HRESULT DropTargetWindows::handle_hdrop_format(Vector<String> *p_files, IDataObject *pDataObj) {
  116. FORMATETC fmt = { CF_HDROP, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  117. STGMEDIUM stg;
  118. HRESULT res = S_OK;
  119. if (pDataObj->GetData(&fmt, &stg) != S_OK) {
  120. return E_UNEXPECTED;
  121. }
  122. HDROP hDropInfo = (HDROP)GlobalLock(stg.hGlobal);
  123. Char16String buf;
  124. if (hDropInfo == nullptr) {
  125. ReleaseStgMedium(&stg);
  126. return E_UNEXPECTED;
  127. }
  128. int fcount = DragQueryFileW(hDropInfo, 0xFFFFFFFF, nullptr, 0);
  129. for (int i = 0; i < fcount; i++) {
  130. int buffsize = DragQueryFileW(hDropInfo, i, nullptr, 0);
  131. buf.resize(buffsize + 1);
  132. if (DragQueryFileW(hDropInfo, i, (LPWSTR)buf.ptrw(), buffsize + 1) == 0) {
  133. res = E_UNEXPECTED;
  134. goto cleanup;
  135. }
  136. String file = String::utf16((const char16_t *)buf.ptr());
  137. p_files->push_back(file);
  138. }
  139. cleanup:
  140. GlobalUnlock(stg.hGlobal);
  141. ReleaseStgMedium(&stg);
  142. return res;
  143. }
  144. HRESULT DropTargetWindows::handle_filedescriptor_format(Vector<String> *p_files, IDataObject *pDataObj) {
  145. FORMATETC fmt = { cf_filedescriptor, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  146. STGMEDIUM stg;
  147. HRESULT res = S_OK;
  148. if (pDataObj->GetData(&fmt, &stg) != S_OK) {
  149. return E_UNEXPECTED;
  150. }
  151. FILEGROUPDESCRIPTORW *filegroup_desc = (FILEGROUPDESCRIPTORW *)GlobalLock(stg.hGlobal);
  152. if (!filegroup_desc) {
  153. ReleaseStgMedium(&stg);
  154. return E_UNEXPECTED;
  155. }
  156. tmp_path = create_temp_dir();
  157. Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  158. PackedStringArray copied;
  159. if (dir_access->change_dir(tmp_path) != OK) {
  160. res = E_UNEXPECTED;
  161. goto cleanup;
  162. }
  163. for (int i = 0; i < (int)filegroup_desc->cItems; ++i) {
  164. res = save_as_file(tmp_path, filegroup_desc->fgd + i, pDataObj, i);
  165. if (res != S_OK) {
  166. res = E_UNEXPECTED;
  167. goto cleanup;
  168. }
  169. }
  170. copied = dir_access->get_files();
  171. for (const String &file : copied) {
  172. p_files->push_back(tmp_path.path_join(file));
  173. }
  174. copied = dir_access->get_directories();
  175. for (const String &dir : copied) {
  176. p_files->push_back(tmp_path.path_join(dir));
  177. }
  178. cleanup:
  179. GlobalUnlock(filegroup_desc);
  180. ReleaseStgMedium(&stg);
  181. if (res != S_OK) {
  182. remove_dir_recursive(tmp_path);
  183. tmp_path.clear();
  184. }
  185. return res;
  186. }
  187. HRESULT DropTargetWindows::save_as_file(const String &p_out_dir, FILEDESCRIPTORW *p_file_desc, IDataObject *pDataObj, int p_file_idx) {
  188. String relpath = String::utf16((const char16_t *)p_file_desc->cFileName);
  189. String fullpath = p_out_dir.path_join(relpath);
  190. if (p_file_desc->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  191. if (DirAccess::make_dir_recursive_absolute(fullpath) != OK) {
  192. return E_UNEXPECTED;
  193. }
  194. return S_OK;
  195. }
  196. FORMATETC fmt = { cf_filecontents, nullptr, DVASPECT_CONTENT, p_file_idx, TYMED_ISTREAM };
  197. STGMEDIUM stg;
  198. HRESULT res = S_OK;
  199. if (pDataObj->GetData(&fmt, &stg) != S_OK) {
  200. return E_UNEXPECTED;
  201. }
  202. IStream *stream = stg.pstm;
  203. if (stream == nullptr) {
  204. res = E_UNEXPECTED;
  205. goto cleanup;
  206. }
  207. if (!stream2file(stream, p_file_desc, fullpath)) {
  208. res = E_UNEXPECTED;
  209. goto cleanup;
  210. }
  211. cleanup:
  212. ReleaseStgMedium(&stg);
  213. return res;
  214. }
  215. DropTargetWindows::DropTargetWindows(DisplayServerWindows::WindowData *p_window_data) :
  216. ref_count(1), window_data(p_window_data) {
  217. cf_filedescriptor = RegisterClipboardFormat(CFSTR_FILEDESCRIPTORW);
  218. cf_filecontents = RegisterClipboardFormat(CFSTR_FILECONTENTS);
  219. }
  220. HRESULT STDMETHODCALLTYPE DropTargetWindows::QueryInterface(REFIID riid, void **ppvObject) {
  221. if (riid == IID_IUnknown || riid == IID_IDropTarget) {
  222. *ppvObject = static_cast<IDropTarget *>(this);
  223. AddRef();
  224. return S_OK;
  225. }
  226. *ppvObject = nullptr;
  227. return E_NOINTERFACE;
  228. }
  229. ULONG STDMETHODCALLTYPE DropTargetWindows::AddRef() {
  230. return InterlockedIncrement(&ref_count);
  231. }
  232. ULONG STDMETHODCALLTYPE DropTargetWindows::Release() {
  233. ULONG count = InterlockedDecrement(&ref_count);
  234. if (count == 0) {
  235. memfree(this);
  236. }
  237. return count;
  238. }
  239. HRESULT STDMETHODCALLTYPE DropTargetWindows::DragEnter(IDataObject *pDataObj, DWORD grfKeyState, POINTL pt, DWORD *pdwEffect) {
  240. (void)grfKeyState;
  241. (void)pt;
  242. FORMATETC hdrop_fmt = { CF_HDROP, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  243. FORMATETC filedesc_fmt = { cf_filedescriptor, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  244. if (!window_data->drop_files_callback.is_valid()) {
  245. *pdwEffect = DROPEFFECT_NONE;
  246. } else if (pDataObj->QueryGetData(&hdrop_fmt) == S_OK) {
  247. *pdwEffect = DROPEFFECT_COPY;
  248. } else if (is_valid_filedescriptor() && pDataObj->QueryGetData(&filedesc_fmt) == S_OK) {
  249. *pdwEffect = DROPEFFECT_COPY;
  250. } else {
  251. *pdwEffect = DROPEFFECT_NONE;
  252. }
  253. return S_OK;
  254. }
  255. HRESULT STDMETHODCALLTYPE DropTargetWindows::DragOver(DWORD grfKeyState, POINTL pt, DWORD *pdwEffect) {
  256. (void)grfKeyState;
  257. (void)pt;
  258. *pdwEffect = DROPEFFECT_COPY;
  259. return S_OK;
  260. }
  261. HRESULT STDMETHODCALLTYPE DropTargetWindows::DragLeave() {
  262. return S_OK;
  263. }
  264. HRESULT STDMETHODCALLTYPE DropTargetWindows::Drop(IDataObject *pDataObj, DWORD grfKeyState, POINTL pt, DWORD *pdwEffect) {
  265. (void)grfKeyState;
  266. (void)pt;
  267. *pdwEffect = DROPEFFECT_NONE;
  268. if (!window_data->drop_files_callback.is_valid()) {
  269. return S_OK;
  270. }
  271. FORMATETC hdrop_fmt = { CF_HDROP, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  272. FORMATETC filedesc_fmt = { cf_filedescriptor, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  273. Vector<String> files;
  274. if (pDataObj->QueryGetData(&hdrop_fmt) == S_OK) {
  275. HRESULT res = handle_hdrop_format(&files, pDataObj);
  276. if (res != S_OK) {
  277. return res;
  278. }
  279. } else if (pDataObj->QueryGetData(&filedesc_fmt) == S_OK && is_valid_filedescriptor()) {
  280. HRESULT res = handle_filedescriptor_format(&files, pDataObj);
  281. if (res != S_OK) {
  282. return res;
  283. }
  284. } else {
  285. return E_UNEXPECTED;
  286. }
  287. if (!files.size()) {
  288. return S_OK;
  289. }
  290. Variant v_files = files;
  291. const Variant *v_args[1] = { &v_files };
  292. Variant ret;
  293. Callable::CallError ce;
  294. window_data->drop_files_callback.callp((const Variant **)&v_args, 1, ret, ce);
  295. if (!tmp_path.is_empty()) {
  296. remove_dir_recursive(tmp_path);
  297. tmp_path.clear();
  298. }
  299. if (ce.error != Callable::CallError::CALL_OK) {
  300. ERR_PRINT(vformat("Failed to execute drop files callback: %s.", Variant::get_callable_error_text(window_data->drop_files_callback, v_args, 1, ce)));
  301. return E_UNEXPECTED;
  302. }
  303. *pdwEffect = DROPEFFECT_COPY;
  304. return S_OK;
  305. }