drop_target_windows.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. #include <shellapi.h>
  36. // Helpers
  37. static String create_temp_dir() {
  38. Char16String buf;
  39. int bufsize = GetTempPathW(0, nullptr) + 1;
  40. buf.resize_uninitialized(bufsize);
  41. if (GetTempPathW(bufsize, (LPWSTR)buf.ptrw()) == 0) {
  42. return "";
  43. }
  44. String tmp_dir = String::utf16((const char16_t *)buf.ptr());
  45. RandomPCG gen(Time::get_singleton()->get_ticks_usec());
  46. const int attempts = 4;
  47. for (int i = 0; i < attempts; ++i) {
  48. uint32_t rnd = gen.rand();
  49. String dirname = "godot_tmp_" + String::num_uint64(rnd);
  50. String res_dir = tmp_dir.path_join(dirname);
  51. Char16String res_dir16 = res_dir.utf16();
  52. if (CreateDirectoryW((LPCWSTR)res_dir16.ptr(), nullptr)) {
  53. return res_dir;
  54. }
  55. }
  56. return "";
  57. }
  58. static bool remove_dir_recursive(const String &p_dir) {
  59. Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  60. if (dir_access->change_dir(p_dir) != OK) {
  61. return false;
  62. }
  63. return dir_access->erase_contents_recursive() == OK;
  64. }
  65. static bool stream2file(IStream *p_stream, FILEDESCRIPTORW *p_desc, const String &p_path) {
  66. if (DirAccess::make_dir_recursive_absolute(p_path.get_base_dir()) != OK) {
  67. return false;
  68. }
  69. Char16String path16 = p_path.utf16();
  70. DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
  71. if (p_desc->dwFlags & FD_ATTRIBUTES) {
  72. dwFlagsAndAttributes = p_desc->dwFileAttributes;
  73. }
  74. HANDLE file = CreateFileW(
  75. (LPCWSTR)path16.ptr(),
  76. GENERIC_WRITE,
  77. 0,
  78. nullptr,
  79. CREATE_NEW,
  80. dwFlagsAndAttributes,
  81. nullptr);
  82. if (!file) {
  83. return false;
  84. }
  85. const int bufsize = 4096;
  86. char buf[bufsize];
  87. ULONG nread = 0;
  88. DWORD nwritten = 0;
  89. HRESULT read_result = S_OK;
  90. bool result = true;
  91. while (true) {
  92. read_result = p_stream->Read(buf, bufsize, &nread);
  93. if (read_result != S_OK && read_result != S_FALSE) {
  94. result = false;
  95. goto cleanup;
  96. }
  97. if (!nread) {
  98. break;
  99. }
  100. while (nread > 0) {
  101. if (!WriteFile(file, buf, nread, &nwritten, nullptr) || !nwritten) {
  102. result = false;
  103. goto cleanup;
  104. }
  105. nread -= nwritten;
  106. }
  107. }
  108. cleanup:
  109. CloseHandle(file);
  110. return result;
  111. }
  112. // DropTargetWindows
  113. bool DropTargetWindows::is_valid_filedescriptor() {
  114. return cf_filedescriptor != 0 && cf_filecontents != 0;
  115. }
  116. HRESULT DropTargetWindows::handle_hdrop_format(Vector<String> *p_files, IDataObject *pDataObj) {
  117. FORMATETC fmt = { CF_HDROP, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  118. STGMEDIUM stg;
  119. HRESULT res = S_OK;
  120. if (pDataObj->GetData(&fmt, &stg) != S_OK) {
  121. return E_UNEXPECTED;
  122. }
  123. HDROP hDropInfo = (HDROP)GlobalLock(stg.hGlobal);
  124. Char16String buf;
  125. if (hDropInfo == nullptr) {
  126. ReleaseStgMedium(&stg);
  127. return E_UNEXPECTED;
  128. }
  129. int fcount = DragQueryFileW(hDropInfo, 0xFFFFFFFF, nullptr, 0);
  130. for (int i = 0; i < fcount; i++) {
  131. int buffsize = DragQueryFileW(hDropInfo, i, nullptr, 0);
  132. buf.resize_uninitialized(buffsize + 1);
  133. if (DragQueryFileW(hDropInfo, i, (LPWSTR)buf.ptrw(), buffsize + 1) == 0) {
  134. res = E_UNEXPECTED;
  135. goto cleanup;
  136. }
  137. String file = String::utf16((const char16_t *)buf.ptr());
  138. p_files->push_back(file);
  139. }
  140. cleanup:
  141. GlobalUnlock(stg.hGlobal);
  142. ReleaseStgMedium(&stg);
  143. return res;
  144. }
  145. HRESULT DropTargetWindows::handle_filedescriptor_format(Vector<String> *p_files, IDataObject *pDataObj) {
  146. FORMATETC fmt = { cf_filedescriptor, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  147. STGMEDIUM stg;
  148. HRESULT res = S_OK;
  149. if (pDataObj->GetData(&fmt, &stg) != S_OK) {
  150. return E_UNEXPECTED;
  151. }
  152. FILEGROUPDESCRIPTORW *filegroup_desc = (FILEGROUPDESCRIPTORW *)GlobalLock(stg.hGlobal);
  153. if (!filegroup_desc) {
  154. ReleaseStgMedium(&stg);
  155. return E_UNEXPECTED;
  156. }
  157. tmp_path = create_temp_dir();
  158. Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  159. PackedStringArray copied;
  160. if (dir_access->change_dir(tmp_path) != OK) {
  161. res = E_UNEXPECTED;
  162. goto cleanup;
  163. }
  164. for (int i = 0; i < (int)filegroup_desc->cItems; ++i) {
  165. res = save_as_file(tmp_path, filegroup_desc->fgd + i, pDataObj, i);
  166. if (res != S_OK) {
  167. res = E_UNEXPECTED;
  168. goto cleanup;
  169. }
  170. }
  171. copied = dir_access->get_files();
  172. for (const String &file : copied) {
  173. p_files->push_back(tmp_path.path_join(file));
  174. }
  175. copied = dir_access->get_directories();
  176. for (const String &dir : copied) {
  177. p_files->push_back(tmp_path.path_join(dir));
  178. }
  179. cleanup:
  180. GlobalUnlock(filegroup_desc);
  181. ReleaseStgMedium(&stg);
  182. if (res != S_OK) {
  183. remove_dir_recursive(tmp_path);
  184. tmp_path.clear();
  185. }
  186. return res;
  187. }
  188. HRESULT DropTargetWindows::save_as_file(const String &p_out_dir, FILEDESCRIPTORW *p_file_desc, IDataObject *pDataObj, int p_file_idx) {
  189. String relpath = String::utf16((const char16_t *)p_file_desc->cFileName);
  190. String fullpath = p_out_dir.path_join(relpath);
  191. if (p_file_desc->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  192. if (DirAccess::make_dir_recursive_absolute(fullpath) != OK) {
  193. return E_UNEXPECTED;
  194. }
  195. return S_OK;
  196. }
  197. FORMATETC fmt = { cf_filecontents, nullptr, DVASPECT_CONTENT, p_file_idx, TYMED_ISTREAM };
  198. STGMEDIUM stg;
  199. HRESULT res = S_OK;
  200. if (pDataObj->GetData(&fmt, &stg) != S_OK) {
  201. return E_UNEXPECTED;
  202. }
  203. IStream *stream = stg.pstm;
  204. if (stream == nullptr) {
  205. res = E_UNEXPECTED;
  206. goto cleanup;
  207. }
  208. if (!stream2file(stream, p_file_desc, fullpath)) {
  209. res = E_UNEXPECTED;
  210. goto cleanup;
  211. }
  212. cleanup:
  213. ReleaseStgMedium(&stg);
  214. return res;
  215. }
  216. DropTargetWindows::DropTargetWindows(DisplayServerWindows::WindowData *p_window_data) :
  217. ref_count(1), window_data(p_window_data) {
  218. cf_filedescriptor = RegisterClipboardFormat(CFSTR_FILEDESCRIPTORW);
  219. cf_filecontents = RegisterClipboardFormat(CFSTR_FILECONTENTS);
  220. }
  221. HRESULT STDMETHODCALLTYPE DropTargetWindows::QueryInterface(REFIID riid, void **ppvObject) {
  222. if (riid == IID_IUnknown || riid == IID_IDropTarget) {
  223. *ppvObject = static_cast<IDropTarget *>(this);
  224. AddRef();
  225. return S_OK;
  226. }
  227. *ppvObject = nullptr;
  228. return E_NOINTERFACE;
  229. }
  230. ULONG STDMETHODCALLTYPE DropTargetWindows::AddRef() {
  231. return InterlockedIncrement(&ref_count);
  232. }
  233. ULONG STDMETHODCALLTYPE DropTargetWindows::Release() {
  234. ULONG count = InterlockedDecrement(&ref_count);
  235. if (count == 0) {
  236. memfree(this);
  237. }
  238. return count;
  239. }
  240. HRESULT STDMETHODCALLTYPE DropTargetWindows::DragEnter(IDataObject *pDataObj, DWORD grfKeyState, POINTL pt, DWORD *pdwEffect) {
  241. (void)grfKeyState;
  242. (void)pt;
  243. FORMATETC hdrop_fmt = { CF_HDROP, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  244. FORMATETC filedesc_fmt = { cf_filedescriptor, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  245. if (!window_data->drop_files_callback.is_valid()) {
  246. *pdwEffect = DROPEFFECT_NONE;
  247. } else if (pDataObj->QueryGetData(&hdrop_fmt) == S_OK) {
  248. *pdwEffect = DROPEFFECT_COPY;
  249. } else if (is_valid_filedescriptor() && pDataObj->QueryGetData(&filedesc_fmt) == S_OK) {
  250. *pdwEffect = DROPEFFECT_COPY;
  251. } else {
  252. *pdwEffect = DROPEFFECT_NONE;
  253. }
  254. return S_OK;
  255. }
  256. HRESULT STDMETHODCALLTYPE DropTargetWindows::DragOver(DWORD grfKeyState, POINTL pt, DWORD *pdwEffect) {
  257. (void)grfKeyState;
  258. (void)pt;
  259. *pdwEffect = DROPEFFECT_COPY;
  260. return S_OK;
  261. }
  262. HRESULT STDMETHODCALLTYPE DropTargetWindows::DragLeave() {
  263. return S_OK;
  264. }
  265. HRESULT STDMETHODCALLTYPE DropTargetWindows::Drop(IDataObject *pDataObj, DWORD grfKeyState, POINTL pt, DWORD *pdwEffect) {
  266. (void)grfKeyState;
  267. (void)pt;
  268. *pdwEffect = DROPEFFECT_NONE;
  269. if (!window_data->drop_files_callback.is_valid()) {
  270. return S_OK;
  271. }
  272. FORMATETC hdrop_fmt = { CF_HDROP, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  273. FORMATETC filedesc_fmt = { cf_filedescriptor, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  274. Vector<String> files;
  275. if (pDataObj->QueryGetData(&hdrop_fmt) == S_OK) {
  276. HRESULT res = handle_hdrop_format(&files, pDataObj);
  277. if (res != S_OK) {
  278. return res;
  279. }
  280. } else if (pDataObj->QueryGetData(&filedesc_fmt) == S_OK && is_valid_filedescriptor()) {
  281. HRESULT res = handle_filedescriptor_format(&files, pDataObj);
  282. if (res != S_OK) {
  283. return res;
  284. }
  285. } else {
  286. return E_UNEXPECTED;
  287. }
  288. if (!files.size()) {
  289. return S_OK;
  290. }
  291. Variant v_files = files;
  292. const Variant *v_args[1] = { &v_files };
  293. Variant ret;
  294. Callable::CallError ce;
  295. window_data->drop_files_callback.callp((const Variant **)&v_args, 1, ret, ce);
  296. if (!tmp_path.is_empty()) {
  297. remove_dir_recursive(tmp_path);
  298. tmp_path.clear();
  299. }
  300. if (ce.error != Callable::CallError::CALL_OK) {
  301. ERR_PRINT(vformat("Failed to execute drop files callback: %s.", Variant::get_callable_error_text(window_data->drop_files_callback, v_args, 1, ce)));
  302. return E_UNEXPECTED;
  303. }
  304. *pdwEffect = DROPEFFECT_COPY;
  305. return S_OK;
  306. }