display_server_windows.cpp 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190
  1. /*************************************************************************/
  2. /* display_server_windows.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 "display_server_windows.h"
  31. #include "core/io/marshalls.h"
  32. #include "main/main.h"
  33. #include "os_windows.h"
  34. #include "scene/resources/texture.h"
  35. #include <avrt.h>
  36. #ifdef DEBUG_ENABLED
  37. static String format_error_message(DWORD id) {
  38. LPWSTR messageBuffer = nullptr;
  39. size_t size = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  40. nullptr, id, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&messageBuffer, 0, nullptr);
  41. String msg = "Error " + itos(id) + ": " + String(messageBuffer, size);
  42. LocalFree(messageBuffer);
  43. return msg;
  44. }
  45. #endif // DEBUG_ENABLED
  46. bool DisplayServerWindows::has_feature(Feature p_feature) const {
  47. switch (p_feature) {
  48. case FEATURE_SUBWINDOWS:
  49. case FEATURE_TOUCHSCREEN:
  50. case FEATURE_MOUSE:
  51. case FEATURE_MOUSE_WARP:
  52. case FEATURE_CLIPBOARD:
  53. case FEATURE_CURSOR_SHAPE:
  54. case FEATURE_CUSTOM_CURSOR_SHAPE:
  55. case FEATURE_CONSOLE_WINDOW:
  56. case FEATURE_IME:
  57. case FEATURE_WINDOW_TRANSPARENCY:
  58. case FEATURE_HIDPI:
  59. case FEATURE_ICON:
  60. case FEATURE_NATIVE_ICON:
  61. case FEATURE_SWAP_BUFFERS:
  62. case FEATURE_KEEP_SCREEN_ON:
  63. return true;
  64. default:
  65. return false;
  66. }
  67. }
  68. String DisplayServerWindows::get_name() const {
  69. return "Windows";
  70. }
  71. void DisplayServerWindows::alert(const String &p_alert, const String &p_title) {
  72. MessageBoxW(nullptr, p_alert.c_str(), p_title.c_str(), MB_OK | MB_ICONEXCLAMATION | MB_TASKMODAL);
  73. }
  74. void DisplayServerWindows::_set_mouse_mode_impl(MouseMode p_mode) {
  75. if (p_mode == MOUSE_MODE_CAPTURED || p_mode == MOUSE_MODE_CONFINED) {
  76. WindowData &wd = windows[MAIN_WINDOW_ID];
  77. RECT clipRect;
  78. GetClientRect(wd.hWnd, &clipRect);
  79. ClientToScreen(wd.hWnd, (POINT *)&clipRect.left);
  80. ClientToScreen(wd.hWnd, (POINT *)&clipRect.right);
  81. ClipCursor(&clipRect);
  82. if (p_mode == MOUSE_MODE_CAPTURED) {
  83. center = window_get_size() / 2;
  84. POINT pos = { (int)center.x, (int)center.y };
  85. ClientToScreen(wd.hWnd, &pos);
  86. SetCursorPos(pos.x, pos.y);
  87. SetCapture(wd.hWnd);
  88. }
  89. } else {
  90. ReleaseCapture();
  91. ClipCursor(nullptr);
  92. }
  93. if (p_mode == MOUSE_MODE_CAPTURED || p_mode == MOUSE_MODE_HIDDEN) {
  94. hCursor = SetCursor(nullptr);
  95. } else {
  96. CursorShape c = cursor_shape;
  97. cursor_shape = CURSOR_MAX;
  98. cursor_set_shape(c);
  99. }
  100. }
  101. void DisplayServerWindows::mouse_set_mode(MouseMode p_mode) {
  102. _THREAD_SAFE_METHOD_
  103. if (mouse_mode == p_mode)
  104. return;
  105. _set_mouse_mode_impl(p_mode);
  106. mouse_mode = p_mode;
  107. }
  108. DisplayServer::MouseMode DisplayServerWindows::mouse_get_mode() const {
  109. return mouse_mode;
  110. }
  111. void DisplayServerWindows::mouse_warp_to_position(const Point2i &p_to) {
  112. _THREAD_SAFE_METHOD_
  113. if (!windows.has(last_focused_window)) {
  114. return; //no window focused?
  115. }
  116. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  117. old_x = p_to.x;
  118. old_y = p_to.y;
  119. } else {
  120. POINT p;
  121. p.x = p_to.x;
  122. p.y = p_to.y;
  123. ClientToScreen(windows[last_focused_window].hWnd, &p);
  124. SetCursorPos(p.x, p.y);
  125. }
  126. }
  127. Point2i DisplayServerWindows::mouse_get_position() const {
  128. POINT p;
  129. GetCursorPos(&p);
  130. return Point2i(p.x, p.y);
  131. //return Point2(old_x, old_y);
  132. }
  133. int DisplayServerWindows::mouse_get_button_state() const {
  134. return last_button_state;
  135. }
  136. void DisplayServerWindows::clipboard_set(const String &p_text) {
  137. _THREAD_SAFE_METHOD_
  138. if (!windows.has(last_focused_window)) {
  139. return; //no window focused?
  140. }
  141. // Convert LF line endings to CRLF in clipboard content
  142. // Otherwise, line endings won't be visible when pasted in other software
  143. String text = p_text.replace("\n", "\r\n");
  144. if (!OpenClipboard(windows[last_focused_window].hWnd)) {
  145. ERR_FAIL_MSG("Unable to open clipboard.");
  146. }
  147. EmptyClipboard();
  148. HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE, (text.length() + 1) * sizeof(CharType));
  149. ERR_FAIL_COND_MSG(mem == nullptr, "Unable to allocate memory for clipboard contents.");
  150. LPWSTR lptstrCopy = (LPWSTR)GlobalLock(mem);
  151. memcpy(lptstrCopy, text.c_str(), (text.length() + 1) * sizeof(CharType));
  152. GlobalUnlock(mem);
  153. SetClipboardData(CF_UNICODETEXT, mem);
  154. // set the CF_TEXT version (not needed?)
  155. CharString utf8 = text.utf8();
  156. mem = GlobalAlloc(GMEM_MOVEABLE, utf8.length() + 1);
  157. ERR_FAIL_COND_MSG(mem == nullptr, "Unable to allocate memory for clipboard contents.");
  158. LPTSTR ptr = (LPTSTR)GlobalLock(mem);
  159. memcpy(ptr, utf8.get_data(), utf8.length());
  160. ptr[utf8.length()] = 0;
  161. GlobalUnlock(mem);
  162. SetClipboardData(CF_TEXT, mem);
  163. CloseClipboard();
  164. }
  165. String DisplayServerWindows::clipboard_get() const {
  166. _THREAD_SAFE_METHOD_
  167. if (!windows.has(last_focused_window)) {
  168. return String(); //no window focused?
  169. }
  170. String ret;
  171. if (!OpenClipboard(windows[last_focused_window].hWnd)) {
  172. ERR_FAIL_V_MSG("", "Unable to open clipboard.");
  173. };
  174. if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
  175. HGLOBAL mem = GetClipboardData(CF_UNICODETEXT);
  176. if (mem != nullptr) {
  177. LPWSTR ptr = (LPWSTR)GlobalLock(mem);
  178. if (ptr != nullptr) {
  179. ret = String((CharType *)ptr);
  180. GlobalUnlock(mem);
  181. };
  182. };
  183. } else if (IsClipboardFormatAvailable(CF_TEXT)) {
  184. HGLOBAL mem = GetClipboardData(CF_UNICODETEXT);
  185. if (mem != nullptr) {
  186. LPTSTR ptr = (LPTSTR)GlobalLock(mem);
  187. if (ptr != nullptr) {
  188. ret.parse_utf8((const char *)ptr);
  189. GlobalUnlock(mem);
  190. };
  191. };
  192. };
  193. CloseClipboard();
  194. return ret;
  195. }
  196. typedef struct {
  197. int count;
  198. int screen;
  199. HMONITOR monitor;
  200. } EnumScreenData;
  201. static BOOL CALLBACK _MonitorEnumProcScreen(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
  202. EnumScreenData *data = (EnumScreenData *)dwData;
  203. if (data->monitor == hMonitor) {
  204. data->screen = data->count;
  205. }
  206. data->count++;
  207. return TRUE;
  208. }
  209. static BOOL CALLBACK _MonitorEnumProcCount(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
  210. int *data = (int *)dwData;
  211. (*data)++;
  212. return TRUE;
  213. }
  214. int DisplayServerWindows::get_screen_count() const {
  215. _THREAD_SAFE_METHOD_
  216. int data = 0;
  217. EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcCount, (LPARAM)&data);
  218. return data;
  219. }
  220. typedef struct {
  221. int count;
  222. int screen;
  223. Point2 pos;
  224. } EnumPosData;
  225. static BOOL CALLBACK _MonitorEnumProcPos(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
  226. EnumPosData *data = (EnumPosData *)dwData;
  227. if (data->count == data->screen) {
  228. data->pos.x = lprcMonitor->left;
  229. data->pos.y = lprcMonitor->top;
  230. }
  231. data->count++;
  232. return TRUE;
  233. }
  234. Point2i DisplayServerWindows::screen_get_position(int p_screen) const {
  235. _THREAD_SAFE_METHOD_
  236. EnumPosData data = { 0, p_screen == SCREEN_OF_MAIN_WINDOW ? window_get_current_screen() : p_screen, Point2() };
  237. EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcPos, (LPARAM)&data);
  238. return data.pos;
  239. }
  240. typedef struct {
  241. int count;
  242. int screen;
  243. Size2 size;
  244. } EnumSizeData;
  245. typedef struct {
  246. int count;
  247. int screen;
  248. Rect2i rect;
  249. } EnumRectData;
  250. static BOOL CALLBACK _MonitorEnumProcSize(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
  251. EnumSizeData *data = (EnumSizeData *)dwData;
  252. if (data->count == data->screen) {
  253. data->size.x = lprcMonitor->right - lprcMonitor->left;
  254. data->size.y = lprcMonitor->bottom - lprcMonitor->top;
  255. }
  256. data->count++;
  257. return TRUE;
  258. }
  259. Size2i DisplayServerWindows::screen_get_size(int p_screen) const {
  260. _THREAD_SAFE_METHOD_
  261. EnumSizeData data = { 0, p_screen == SCREEN_OF_MAIN_WINDOW ? window_get_current_screen() : p_screen, Size2() };
  262. EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcSize, (LPARAM)&data);
  263. return data.size;
  264. }
  265. static BOOL CALLBACK _MonitorEnumProcUsableSize(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
  266. EnumRectData *data = (EnumRectData *)dwData;
  267. if (data->count == data->screen) {
  268. MONITORINFO minfo;
  269. zeromem(&minfo, sizeof(MONITORINFO));
  270. minfo.cbSize = sizeof(MONITORINFO);
  271. GetMonitorInfoA(hMonitor, &minfo);
  272. data->rect.position.x = minfo.rcWork.left;
  273. data->rect.position.y = minfo.rcWork.top;
  274. data->rect.size.x = minfo.rcWork.right - minfo.rcWork.left;
  275. data->rect.size.y = minfo.rcWork.bottom - minfo.rcWork.top;
  276. }
  277. data->count++;
  278. return TRUE;
  279. }
  280. Rect2i DisplayServerWindows::screen_get_usable_rect(int p_screen) const {
  281. _THREAD_SAFE_METHOD_
  282. EnumRectData data = { 0, p_screen == SCREEN_OF_MAIN_WINDOW ? window_get_current_screen() : p_screen, Rect2i() };
  283. EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcUsableSize, (LPARAM)&data);
  284. return data.rect;
  285. }
  286. typedef struct {
  287. int count;
  288. int screen;
  289. int dpi;
  290. } EnumDpiData;
  291. enum _MonitorDpiType {
  292. MDT_Effective_DPI = 0,
  293. MDT_Angular_DPI = 1,
  294. MDT_Raw_DPI = 2,
  295. MDT_Default = MDT_Effective_DPI
  296. };
  297. static int QueryDpiForMonitor(HMONITOR hmon, _MonitorDpiType dpiType = MDT_Default) {
  298. int dpiX = 96, dpiY = 96;
  299. static HMODULE Shcore = nullptr;
  300. typedef HRESULT(WINAPI * GetDPIForMonitor_t)(HMONITOR hmonitor, _MonitorDpiType dpiType, UINT * dpiX, UINT * dpiY);
  301. static GetDPIForMonitor_t getDPIForMonitor = nullptr;
  302. if (Shcore == nullptr) {
  303. Shcore = LoadLibraryW(L"Shcore.dll");
  304. getDPIForMonitor = Shcore ? (GetDPIForMonitor_t)GetProcAddress(Shcore, "GetDpiForMonitor") : nullptr;
  305. if ((Shcore == nullptr) || (getDPIForMonitor == nullptr)) {
  306. if (Shcore)
  307. FreeLibrary(Shcore);
  308. Shcore = (HMODULE)INVALID_HANDLE_VALUE;
  309. }
  310. }
  311. UINT x = 0, y = 0;
  312. HRESULT hr = E_FAIL;
  313. if (hmon && (Shcore != (HMODULE)INVALID_HANDLE_VALUE)) {
  314. hr = getDPIForMonitor(hmon, dpiType /*MDT_Effective_DPI*/, &x, &y);
  315. if (SUCCEEDED(hr) && (x > 0) && (y > 0)) {
  316. dpiX = (int)x;
  317. dpiY = (int)y;
  318. }
  319. } else {
  320. static int overallX = 0, overallY = 0;
  321. if (overallX <= 0 || overallY <= 0) {
  322. HDC hdc = GetDC(nullptr);
  323. if (hdc) {
  324. overallX = GetDeviceCaps(hdc, LOGPIXELSX);
  325. overallY = GetDeviceCaps(hdc, LOGPIXELSY);
  326. ReleaseDC(nullptr, hdc);
  327. }
  328. }
  329. if (overallX > 0 && overallY > 0) {
  330. dpiX = overallX;
  331. dpiY = overallY;
  332. }
  333. }
  334. return (dpiX + dpiY) / 2;
  335. }
  336. static BOOL CALLBACK _MonitorEnumProcDpi(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
  337. EnumDpiData *data = (EnumDpiData *)dwData;
  338. if (data->count == data->screen) {
  339. data->dpi = QueryDpiForMonitor(hMonitor);
  340. }
  341. data->count++;
  342. return TRUE;
  343. }
  344. int DisplayServerWindows::screen_get_dpi(int p_screen) const {
  345. _THREAD_SAFE_METHOD_
  346. EnumDpiData data = { 0, p_screen == SCREEN_OF_MAIN_WINDOW ? window_get_current_screen() : p_screen, 72 };
  347. EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcDpi, (LPARAM)&data);
  348. return data.dpi;
  349. }
  350. bool DisplayServerWindows::screen_is_touchscreen(int p_screen) const {
  351. #ifndef _MSC_VER
  352. #warning touchscreen not working
  353. #endif
  354. return false;
  355. }
  356. void DisplayServerWindows::screen_set_orientation(ScreenOrientation p_orientation, int p_screen) {
  357. }
  358. DisplayServer::ScreenOrientation DisplayServerWindows::screen_get_orientation(int p_screen) const {
  359. return SCREEN_LANDSCAPE;
  360. }
  361. void DisplayServerWindows::screen_set_keep_on(bool p_enable) {
  362. }
  363. bool DisplayServerWindows::screen_is_kept_on() const {
  364. return false;
  365. }
  366. Vector<DisplayServer::WindowID> DisplayServerWindows::get_window_list() const {
  367. _THREAD_SAFE_METHOD_
  368. Vector<DisplayServer::WindowID> ret;
  369. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  370. ret.push_back(E->key());
  371. }
  372. return ret;
  373. }
  374. DisplayServer::WindowID DisplayServerWindows::get_window_at_screen_position(const Point2i &p_position) const {
  375. POINT p;
  376. p.x = p_position.x;
  377. p.y = p_position.y;
  378. HWND hwnd = WindowFromPoint(p);
  379. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  380. if (E->get().hWnd == hwnd) {
  381. return E->key();
  382. }
  383. }
  384. return INVALID_WINDOW_ID;
  385. }
  386. DisplayServer::WindowID DisplayServerWindows::create_sub_window(WindowMode p_mode, uint32_t p_flags, const Rect2i &p_rect) {
  387. _THREAD_SAFE_METHOD_
  388. WindowID window_id = _create_window(p_mode, p_flags, p_rect);
  389. WindowData &wd = windows[window_id];
  390. if (p_flags & WINDOW_FLAG_RESIZE_DISABLED_BIT) {
  391. wd.resizable = false;
  392. }
  393. if (p_flags & WINDOW_FLAG_BORDERLESS_BIT) {
  394. wd.borderless = true;
  395. }
  396. if (p_flags & WINDOW_FLAG_ALWAYS_ON_TOP_BIT && p_mode != WINDOW_MODE_FULLSCREEN) {
  397. wd.always_on_top = true;
  398. }
  399. if (p_flags & WINDOW_FLAG_NO_FOCUS_BIT) {
  400. wd.no_focus = true;
  401. }
  402. _update_window_style(window_id);
  403. ShowWindow(wd.hWnd, (p_flags & WINDOW_FLAG_NO_FOCUS_BIT) ? SW_SHOWNOACTIVATE : SW_SHOW); // Show The Window
  404. if (!(p_flags & WINDOW_FLAG_NO_FOCUS_BIT)) {
  405. SetForegroundWindow(wd.hWnd); // Slightly Higher Priority
  406. SetFocus(wd.hWnd); // Sets Keyboard Focus To
  407. }
  408. return window_id;
  409. }
  410. void DisplayServerWindows::delete_sub_window(WindowID p_window) {
  411. _THREAD_SAFE_METHOD_
  412. ERR_FAIL_COND(!windows.has(p_window));
  413. ERR_FAIL_COND_MSG(p_window == MAIN_WINDOW_ID, "Main window cannot be deleted.");
  414. WindowData &wd = windows[p_window];
  415. while (wd.transient_children.size()) {
  416. window_set_transient(wd.transient_children.front()->get(), INVALID_WINDOW_ID);
  417. }
  418. if (wd.transient_parent != INVALID_WINDOW_ID) {
  419. window_set_transient(p_window, INVALID_WINDOW_ID);
  420. }
  421. #ifdef VULKAN_ENABLED
  422. if (rendering_driver == "vulkan") {
  423. context_vulkan->window_destroy(p_window);
  424. }
  425. #endif
  426. if (!OS::get_singleton()->is_wintab_disabled() && wintab_available && windows[p_window].wtctx) {
  427. wintab_WTClose(windows[p_window].wtctx);
  428. windows[p_window].wtctx = 0;
  429. }
  430. DestroyWindow(windows[p_window].hWnd);
  431. windows.erase(p_window);
  432. }
  433. void DisplayServerWindows::window_attach_instance_id(ObjectID p_instance, WindowID p_window) {
  434. _THREAD_SAFE_METHOD_
  435. ERR_FAIL_COND(!windows.has(p_window));
  436. windows[p_window].instance_id = p_instance;
  437. }
  438. ObjectID DisplayServerWindows::window_get_attached_instance_id(WindowID p_window) const {
  439. _THREAD_SAFE_METHOD_
  440. ERR_FAIL_COND_V(!windows.has(p_window), ObjectID());
  441. return windows[p_window].instance_id;
  442. }
  443. void DisplayServerWindows::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) {
  444. _THREAD_SAFE_METHOD_
  445. ERR_FAIL_COND(!windows.has(p_window));
  446. windows[p_window].rect_changed_callback = p_callable;
  447. }
  448. void DisplayServerWindows::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) {
  449. _THREAD_SAFE_METHOD_
  450. ERR_FAIL_COND(!windows.has(p_window));
  451. windows[p_window].event_callback = p_callable;
  452. }
  453. void DisplayServerWindows::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) {
  454. _THREAD_SAFE_METHOD_
  455. ERR_FAIL_COND(!windows.has(p_window));
  456. windows[p_window].input_event_callback = p_callable;
  457. }
  458. void DisplayServerWindows::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) {
  459. _THREAD_SAFE_METHOD_
  460. ERR_FAIL_COND(!windows.has(p_window));
  461. windows[p_window].input_text_callback = p_callable;
  462. }
  463. void DisplayServerWindows::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) {
  464. _THREAD_SAFE_METHOD_
  465. ERR_FAIL_COND(!windows.has(p_window));
  466. windows[p_window].drop_files_callback = p_callable;
  467. }
  468. void DisplayServerWindows::window_set_title(const String &p_title, WindowID p_window) {
  469. _THREAD_SAFE_METHOD_
  470. ERR_FAIL_COND(!windows.has(p_window));
  471. SetWindowTextW(windows[p_window].hWnd, p_title.c_str());
  472. }
  473. int DisplayServerWindows::window_get_current_screen(WindowID p_window) const {
  474. _THREAD_SAFE_METHOD_
  475. ERR_FAIL_COND_V(!windows.has(p_window), -1);
  476. EnumScreenData data = { 0, 0, MonitorFromWindow(windows[p_window].hWnd, MONITOR_DEFAULTTONEAREST) };
  477. EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcScreen, (LPARAM)&data);
  478. return data.screen;
  479. }
  480. void DisplayServerWindows::window_set_current_screen(int p_screen, WindowID p_window) {
  481. _THREAD_SAFE_METHOD_
  482. ERR_FAIL_COND(!windows.has(p_window));
  483. ERR_FAIL_INDEX(p_screen, get_screen_count());
  484. Vector2 ofs = window_get_position(p_window) - screen_get_position(window_get_current_screen(p_window));
  485. window_set_position(ofs + screen_get_position(p_screen), p_window);
  486. }
  487. Point2i DisplayServerWindows::window_get_position(WindowID p_window) const {
  488. _THREAD_SAFE_METHOD_
  489. ERR_FAIL_COND_V(!windows.has(p_window), Point2i());
  490. const WindowData &wd = windows[p_window];
  491. if (wd.minimized) {
  492. return wd.last_pos;
  493. }
  494. POINT point;
  495. point.x = 0;
  496. point.y = 0;
  497. ClientToScreen(wd.hWnd, &point);
  498. return Point2i(point.x, point.y);
  499. #if 0
  500. //do not use this method, as it includes windows decorations
  501. RECT r;
  502. GetWindowRect(wd.hWnd, &r);
  503. return Point2(r.left, r.top);
  504. #endif
  505. }
  506. void DisplayServerWindows::_update_real_mouse_position(WindowID p_window) {
  507. POINT mouse_pos;
  508. if (GetCursorPos(&mouse_pos) && ScreenToClient(windows[p_window].hWnd, &mouse_pos)) {
  509. if (mouse_pos.x > 0 && mouse_pos.y > 0 && mouse_pos.x <= windows[p_window].width && mouse_pos.y <= windows[p_window].height) {
  510. old_x = mouse_pos.x;
  511. old_y = mouse_pos.y;
  512. old_invalid = false;
  513. Input::get_singleton()->set_mouse_position(Point2i(mouse_pos.x, mouse_pos.y));
  514. }
  515. }
  516. }
  517. void DisplayServerWindows::window_set_position(const Point2i &p_position, WindowID p_window) {
  518. _THREAD_SAFE_METHOD_
  519. ERR_FAIL_COND(!windows.has(p_window));
  520. WindowData &wd = windows[p_window];
  521. if (wd.fullscreen)
  522. return;
  523. #if 0
  524. //wrong needs to account properly for decorations
  525. RECT r;
  526. GetWindowRect(wd.hWnd, &r);
  527. MoveWindow(wd.hWnd, p_position.x, p_position.y, r.right - r.left, r.bottom - r.top, TRUE);
  528. #else
  529. RECT rc;
  530. rc.left = p_position.x;
  531. rc.right = p_position.x + wd.width;
  532. rc.bottom = p_position.y + wd.height;
  533. rc.top = p_position.y;
  534. const DWORD style = GetWindowLongPtr(wd.hWnd, GWL_STYLE);
  535. const DWORD exStyle = GetWindowLongPtr(wd.hWnd, GWL_EXSTYLE);
  536. AdjustWindowRectEx(&rc, style, false, exStyle);
  537. MoveWindow(wd.hWnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, TRUE);
  538. #endif
  539. // Don't let the mouse leave the window when moved
  540. if (mouse_mode == MOUSE_MODE_CONFINED) {
  541. RECT rect;
  542. GetClientRect(wd.hWnd, &rect);
  543. ClientToScreen(wd.hWnd, (POINT *)&rect.left);
  544. ClientToScreen(wd.hWnd, (POINT *)&rect.right);
  545. ClipCursor(&rect);
  546. }
  547. wd.last_pos = p_position;
  548. _update_real_mouse_position(p_window);
  549. }
  550. void DisplayServerWindows::window_set_transient(WindowID p_window, WindowID p_parent) {
  551. _THREAD_SAFE_METHOD_
  552. ERR_FAIL_COND(p_window == p_parent);
  553. ERR_FAIL_COND(!windows.has(p_window));
  554. WindowData &wd_window = windows[p_window];
  555. ERR_FAIL_COND(wd_window.transient_parent == p_parent);
  556. ERR_FAIL_COND_MSG(wd_window.always_on_top, "Windows with the 'on top' can't become transient.");
  557. if (p_parent == INVALID_WINDOW_ID) {
  558. //remove transient
  559. ERR_FAIL_COND(wd_window.transient_parent == INVALID_WINDOW_ID);
  560. ERR_FAIL_COND(!windows.has(wd_window.transient_parent));
  561. WindowData &wd_parent = windows[wd_window.transient_parent];
  562. wd_window.transient_parent = INVALID_WINDOW_ID;
  563. wd_parent.transient_children.erase(p_window);
  564. SetWindowLongPtr(wd_window.hWnd, GWLP_HWNDPARENT, (LONG_PTR) nullptr);
  565. } else {
  566. ERR_FAIL_COND(!windows.has(p_parent));
  567. ERR_FAIL_COND_MSG(wd_window.transient_parent != INVALID_WINDOW_ID, "Window already has a transient parent");
  568. WindowData &wd_parent = windows[p_parent];
  569. wd_window.transient_parent = p_parent;
  570. wd_parent.transient_children.insert(p_window);
  571. SetWindowLongPtr(wd_window.hWnd, GWLP_HWNDPARENT, (LONG_PTR)wd_parent.hWnd);
  572. }
  573. }
  574. void DisplayServerWindows::window_set_max_size(const Size2i p_size, WindowID p_window) {
  575. _THREAD_SAFE_METHOD_
  576. ERR_FAIL_COND(!windows.has(p_window));
  577. WindowData &wd = windows[p_window];
  578. if ((p_size != Size2()) && ((p_size.x < wd.min_size.x) || (p_size.y < wd.min_size.y))) {
  579. ERR_PRINT("Maximum window size can't be smaller than minimum window size!");
  580. return;
  581. }
  582. wd.max_size = p_size;
  583. }
  584. Size2i DisplayServerWindows::window_get_max_size(WindowID p_window) const {
  585. _THREAD_SAFE_METHOD_
  586. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  587. const WindowData &wd = windows[p_window];
  588. return wd.max_size;
  589. }
  590. void DisplayServerWindows::window_set_min_size(const Size2i p_size, WindowID p_window) {
  591. _THREAD_SAFE_METHOD_
  592. ERR_FAIL_COND(!windows.has(p_window));
  593. WindowData &wd = windows[p_window];
  594. if ((p_size != Size2()) && (wd.max_size != Size2()) && ((p_size.x > wd.max_size.x) || (p_size.y > wd.max_size.y))) {
  595. ERR_PRINT("Minimum window size can't be larger than maximum window size!");
  596. return;
  597. }
  598. wd.min_size = p_size;
  599. }
  600. Size2i DisplayServerWindows::window_get_min_size(WindowID p_window) const {
  601. _THREAD_SAFE_METHOD_
  602. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  603. const WindowData &wd = windows[p_window];
  604. return wd.min_size;
  605. }
  606. void DisplayServerWindows::window_set_size(const Size2i p_size, WindowID p_window) {
  607. _THREAD_SAFE_METHOD_
  608. ERR_FAIL_COND(!windows.has(p_window));
  609. WindowData &wd = windows[p_window];
  610. int w = p_size.width;
  611. int h = p_size.height;
  612. wd.width = w;
  613. wd.height = h;
  614. #if defined(VULKAN_ENABLED)
  615. if (rendering_driver == "vulkan") {
  616. context_vulkan->window_resize(p_window, w, h);
  617. }
  618. #endif
  619. if (wd.fullscreen) {
  620. return;
  621. }
  622. RECT rect;
  623. GetWindowRect(wd.hWnd, &rect);
  624. if (!wd.borderless) {
  625. RECT crect;
  626. GetClientRect(wd.hWnd, &crect);
  627. w += (rect.right - rect.left) - (crect.right - crect.left);
  628. h += (rect.bottom - rect.top) - (crect.bottom - crect.top);
  629. }
  630. MoveWindow(wd.hWnd, rect.left, rect.top, w, h, TRUE);
  631. // Don't let the mouse leave the window when resizing to a smaller resolution
  632. if (mouse_mode == MOUSE_MODE_CONFINED) {
  633. RECT crect;
  634. GetClientRect(wd.hWnd, &crect);
  635. ClientToScreen(wd.hWnd, (POINT *)&crect.left);
  636. ClientToScreen(wd.hWnd, (POINT *)&crect.right);
  637. ClipCursor(&crect);
  638. }
  639. }
  640. Size2i DisplayServerWindows::window_get_size(WindowID p_window) const {
  641. _THREAD_SAFE_METHOD_
  642. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  643. const WindowData &wd = windows[p_window];
  644. if (wd.minimized) {
  645. return Size2(wd.width, wd.height);
  646. }
  647. RECT r;
  648. if (GetClientRect(wd.hWnd, &r)) { // Only area inside of window border
  649. return Size2(r.right - r.left, r.bottom - r.top);
  650. }
  651. return Size2();
  652. }
  653. Size2i DisplayServerWindows::window_get_real_size(WindowID p_window) const {
  654. _THREAD_SAFE_METHOD_
  655. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  656. const WindowData &wd = windows[p_window];
  657. RECT r;
  658. if (GetWindowRect(wd.hWnd, &r)) { // Includes area of the window border
  659. return Size2(r.right - r.left, r.bottom - r.top);
  660. }
  661. return Size2();
  662. }
  663. void DisplayServerWindows::_get_window_style(bool p_main_window, bool p_fullscreen, bool p_borderless, bool p_resizable, bool p_maximized, bool p_no_activate_focus, DWORD &r_style, DWORD &r_style_ex) {
  664. r_style = 0;
  665. r_style_ex = WS_EX_WINDOWEDGE;
  666. if (p_main_window) {
  667. r_style_ex |= WS_EX_APPWINDOW;
  668. }
  669. if (p_fullscreen || p_borderless) {
  670. r_style |= WS_POPUP;
  671. //if (p_borderless) {
  672. // r_style_ex |= WS_EX_TOOLWINDOW;
  673. //}
  674. } else {
  675. if (p_resizable) {
  676. if (p_maximized) {
  677. r_style = WS_OVERLAPPEDWINDOW | WS_MAXIMIZE;
  678. } else {
  679. r_style = WS_OVERLAPPEDWINDOW;
  680. }
  681. } else {
  682. r_style = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU;
  683. }
  684. }
  685. if (p_no_activate_focus) {
  686. r_style_ex |= WS_EX_TOPMOST | WS_EX_NOACTIVATE;
  687. }
  688. r_style |= WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  689. }
  690. void DisplayServerWindows::_update_window_style(WindowID p_window, bool p_repaint, bool p_maximized) {
  691. _THREAD_SAFE_METHOD_
  692. ERR_FAIL_COND(!windows.has(p_window));
  693. WindowData &wd = windows[p_window];
  694. DWORD style = 0;
  695. DWORD style_ex = 0;
  696. _get_window_style(p_window == MAIN_WINDOW_ID, wd.fullscreen, wd.borderless, wd.resizable, wd.maximized, wd.no_focus, style, style_ex);
  697. SetWindowLongPtr(wd.hWnd, GWL_STYLE, style);
  698. SetWindowLongPtr(wd.hWnd, GWL_EXSTYLE, style_ex);
  699. SetWindowPos(wd.hWnd, wd.always_on_top ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | (wd.no_focus ? SWP_NOACTIVATE : 0));
  700. if (p_repaint) {
  701. RECT rect;
  702. GetWindowRect(wd.hWnd, &rect);
  703. MoveWindow(wd.hWnd, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, TRUE);
  704. }
  705. }
  706. void DisplayServerWindows::window_set_mode(WindowMode p_mode, WindowID p_window) {
  707. _THREAD_SAFE_METHOD_
  708. ERR_FAIL_COND(!windows.has(p_window));
  709. WindowData &wd = windows[p_window];
  710. if (wd.fullscreen && p_mode != WINDOW_MODE_FULLSCREEN) {
  711. RECT rect;
  712. wd.fullscreen = false;
  713. if (wd.pre_fs_valid) {
  714. rect = wd.pre_fs_rect;
  715. } else {
  716. rect.left = 0;
  717. rect.right = wd.width;
  718. rect.top = 0;
  719. rect.bottom = wd.height;
  720. }
  721. _update_window_style(p_window, false, wd.was_maximized);
  722. MoveWindow(wd.hWnd, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, TRUE);
  723. wd.pre_fs_valid = true;
  724. }
  725. if (p_mode == WINDOW_MODE_MAXIMIZED) {
  726. ShowWindow(wd.hWnd, SW_MAXIMIZE);
  727. wd.maximized = true;
  728. wd.minimized = false;
  729. }
  730. if (p_mode == WINDOW_MODE_WINDOWED) {
  731. ShowWindow(wd.hWnd, SW_RESTORE);
  732. wd.maximized = false;
  733. wd.minimized = false;
  734. }
  735. if (p_mode == WINDOW_MODE_MINIMIZED) {
  736. ShowWindow(wd.hWnd, SW_MINIMIZE);
  737. wd.maximized = false;
  738. wd.minimized = true;
  739. }
  740. if (p_mode == WINDOW_MODE_FULLSCREEN && !wd.fullscreen) {
  741. if (wd.minimized) {
  742. ShowWindow(wd.hWnd, SW_RESTORE);
  743. }
  744. wd.was_maximized = wd.maximized;
  745. if (wd.pre_fs_valid) {
  746. GetWindowRect(wd.hWnd, &wd.pre_fs_rect);
  747. }
  748. int cs = window_get_current_screen(p_window);
  749. Point2 pos = screen_get_position(cs);
  750. Size2 size = screen_get_size(cs);
  751. wd.fullscreen = true;
  752. wd.maximized = false;
  753. wd.minimized = false;
  754. _update_window_style(false);
  755. MoveWindow(wd.hWnd, pos.x, pos.y, size.width, size.height, TRUE);
  756. }
  757. }
  758. DisplayServer::WindowMode DisplayServerWindows::window_get_mode(WindowID p_window) const {
  759. _THREAD_SAFE_METHOD_
  760. ERR_FAIL_COND_V(!windows.has(p_window), WINDOW_MODE_WINDOWED);
  761. const WindowData &wd = windows[p_window];
  762. if (wd.fullscreen) {
  763. return WINDOW_MODE_FULLSCREEN;
  764. } else if (wd.minimized) {
  765. return WINDOW_MODE_MINIMIZED;
  766. } else if (wd.maximized) {
  767. return WINDOW_MODE_MAXIMIZED;
  768. } else {
  769. return WINDOW_MODE_WINDOWED;
  770. }
  771. }
  772. bool DisplayServerWindows::window_is_maximize_allowed(WindowID p_window) const {
  773. _THREAD_SAFE_METHOD_
  774. ERR_FAIL_COND_V(!windows.has(p_window), false);
  775. // FIXME: Implement this, or confirm that it should always be true.
  776. return true; //no idea
  777. }
  778. void DisplayServerWindows::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
  779. _THREAD_SAFE_METHOD_
  780. ERR_FAIL_COND(!windows.has(p_window));
  781. WindowData &wd = windows[p_window];
  782. switch (p_flag) {
  783. case WINDOW_FLAG_RESIZE_DISABLED: {
  784. wd.resizable = !p_enabled;
  785. _update_window_style(p_window);
  786. } break;
  787. case WINDOW_FLAG_BORDERLESS: {
  788. wd.borderless = p_enabled;
  789. _update_window_style(p_window);
  790. } break;
  791. case WINDOW_FLAG_ALWAYS_ON_TOP: {
  792. ERR_FAIL_COND_MSG(wd.transient_parent != INVALID_WINDOW_ID && p_enabled, "Transient windows can't become on top");
  793. wd.always_on_top = p_enabled;
  794. _update_window_style(p_window);
  795. } break;
  796. case WINDOW_FLAG_TRANSPARENT: {
  797. // FIXME: Implement.
  798. } break;
  799. case WINDOW_FLAG_NO_FOCUS: {
  800. wd.no_focus = p_enabled;
  801. _update_window_style(p_window);
  802. } break;
  803. case WINDOW_FLAG_MAX: break;
  804. }
  805. }
  806. bool DisplayServerWindows::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
  807. _THREAD_SAFE_METHOD_
  808. ERR_FAIL_COND_V(!windows.has(p_window), false);
  809. const WindowData &wd = windows[p_window];
  810. switch (p_flag) {
  811. case WINDOW_FLAG_RESIZE_DISABLED: {
  812. return !wd.resizable;
  813. } break;
  814. case WINDOW_FLAG_BORDERLESS: {
  815. return wd.borderless;
  816. } break;
  817. case WINDOW_FLAG_ALWAYS_ON_TOP: {
  818. return wd.always_on_top;
  819. } break;
  820. case WINDOW_FLAG_TRANSPARENT: {
  821. // FIXME: Implement.
  822. } break;
  823. case WINDOW_FLAG_NO_FOCUS: {
  824. return wd.no_focus;
  825. } break;
  826. case WINDOW_FLAG_MAX: break;
  827. }
  828. return false;
  829. }
  830. void DisplayServerWindows::window_request_attention(WindowID p_window) {
  831. _THREAD_SAFE_METHOD_
  832. ERR_FAIL_COND(!windows.has(p_window));
  833. WindowData &wd = windows[p_window];
  834. FLASHWINFO info;
  835. info.cbSize = sizeof(FLASHWINFO);
  836. info.hwnd = wd.hWnd;
  837. info.dwFlags = FLASHW_TRAY;
  838. info.dwTimeout = 0;
  839. info.uCount = 2;
  840. FlashWindowEx(&info);
  841. }
  842. void DisplayServerWindows::window_move_to_foreground(WindowID p_window) {
  843. _THREAD_SAFE_METHOD_
  844. ERR_FAIL_COND(!windows.has(p_window));
  845. WindowData &wd = windows[p_window];
  846. SetForegroundWindow(wd.hWnd);
  847. }
  848. bool DisplayServerWindows::window_can_draw(WindowID p_window) const {
  849. _THREAD_SAFE_METHOD_
  850. ERR_FAIL_COND_V(!windows.has(p_window), false);
  851. const WindowData &wd = windows[p_window];
  852. return wd.minimized;
  853. }
  854. bool DisplayServerWindows::can_any_window_draw() const {
  855. _THREAD_SAFE_METHOD_
  856. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  857. if (!E->get().minimized) {
  858. return true;
  859. }
  860. }
  861. return false;
  862. }
  863. void DisplayServerWindows::window_set_ime_active(const bool p_active, WindowID p_window) {
  864. _THREAD_SAFE_METHOD_
  865. ERR_FAIL_COND(!windows.has(p_window));
  866. WindowData &wd = windows[p_window];
  867. if (p_active) {
  868. ImmAssociateContext(wd.hWnd, wd.im_himc);
  869. window_set_ime_position(wd.im_position, p_window);
  870. } else {
  871. ImmAssociateContext(wd.hWnd, (HIMC)0);
  872. }
  873. }
  874. void DisplayServerWindows::window_set_ime_position(const Point2i &p_pos, WindowID p_window) {
  875. _THREAD_SAFE_METHOD_
  876. ERR_FAIL_COND(!windows.has(p_window));
  877. WindowData &wd = windows[p_window];
  878. wd.im_position = p_pos;
  879. HIMC himc = ImmGetContext(wd.hWnd);
  880. if (himc == (HIMC)0)
  881. return;
  882. COMPOSITIONFORM cps;
  883. cps.dwStyle = CFS_FORCE_POSITION;
  884. cps.ptCurrentPos.x = wd.im_position.x;
  885. cps.ptCurrentPos.y = wd.im_position.y;
  886. ImmSetCompositionWindow(himc, &cps);
  887. ImmReleaseContext(wd.hWnd, himc);
  888. }
  889. void DisplayServerWindows::console_set_visible(bool p_enabled) {
  890. _THREAD_SAFE_METHOD_
  891. if (console_visible == p_enabled)
  892. return;
  893. ShowWindow(GetConsoleWindow(), p_enabled ? SW_SHOW : SW_HIDE);
  894. console_visible = p_enabled;
  895. }
  896. bool DisplayServerWindows::is_console_visible() const {
  897. return console_visible;
  898. }
  899. void DisplayServerWindows::cursor_set_shape(CursorShape p_shape) {
  900. _THREAD_SAFE_METHOD_
  901. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  902. if (cursor_shape == p_shape)
  903. return;
  904. if (mouse_mode != MOUSE_MODE_VISIBLE && mouse_mode != MOUSE_MODE_CONFINED) {
  905. cursor_shape = p_shape;
  906. return;
  907. }
  908. static const LPCTSTR win_cursors[CURSOR_MAX] = {
  909. IDC_ARROW,
  910. IDC_IBEAM,
  911. IDC_HAND, //finger
  912. IDC_CROSS,
  913. IDC_WAIT,
  914. IDC_APPSTARTING,
  915. IDC_ARROW,
  916. IDC_ARROW,
  917. IDC_NO,
  918. IDC_SIZENS,
  919. IDC_SIZEWE,
  920. IDC_SIZENESW,
  921. IDC_SIZENWSE,
  922. IDC_SIZEALL,
  923. IDC_SIZENS,
  924. IDC_SIZEWE,
  925. IDC_HELP
  926. };
  927. if (cursors[p_shape] != nullptr) {
  928. SetCursor(cursors[p_shape]);
  929. } else {
  930. SetCursor(LoadCursor(hInstance, win_cursors[p_shape]));
  931. }
  932. cursor_shape = p_shape;
  933. }
  934. DisplayServer::CursorShape DisplayServerWindows::cursor_get_shape() const {
  935. return cursor_shape;
  936. }
  937. void DisplayServerWindows::GetMaskBitmaps(HBITMAP hSourceBitmap, COLORREF clrTransparent, OUT HBITMAP &hAndMaskBitmap, OUT HBITMAP &hXorMaskBitmap) {
  938. // Get the system display DC
  939. HDC hDC = GetDC(nullptr);
  940. // Create helper DC
  941. HDC hMainDC = CreateCompatibleDC(hDC);
  942. HDC hAndMaskDC = CreateCompatibleDC(hDC);
  943. HDC hXorMaskDC = CreateCompatibleDC(hDC);
  944. // Get the dimensions of the source bitmap
  945. BITMAP bm;
  946. GetObject(hSourceBitmap, sizeof(BITMAP), &bm);
  947. // Create the mask bitmaps
  948. hAndMaskBitmap = CreateCompatibleBitmap(hDC, bm.bmWidth, bm.bmHeight); // color
  949. hXorMaskBitmap = CreateCompatibleBitmap(hDC, bm.bmWidth, bm.bmHeight); // color
  950. // Release the system display DC
  951. ReleaseDC(nullptr, hDC);
  952. // Select the bitmaps to helper DC
  953. HBITMAP hOldMainBitmap = (HBITMAP)SelectObject(hMainDC, hSourceBitmap);
  954. HBITMAP hOldAndMaskBitmap = (HBITMAP)SelectObject(hAndMaskDC, hAndMaskBitmap);
  955. HBITMAP hOldXorMaskBitmap = (HBITMAP)SelectObject(hXorMaskDC, hXorMaskBitmap);
  956. // Assign the monochrome AND mask bitmap pixels so that a pixels of the source bitmap
  957. // with 'clrTransparent' will be white pixels of the monochrome bitmap
  958. SetBkColor(hMainDC, clrTransparent);
  959. BitBlt(hAndMaskDC, 0, 0, bm.bmWidth, bm.bmHeight, hMainDC, 0, 0, SRCCOPY);
  960. // Assign the color XOR mask bitmap pixels so that a pixels of the source bitmap
  961. // with 'clrTransparent' will be black and rest the pixels same as corresponding
  962. // pixels of the source bitmap
  963. SetBkColor(hXorMaskDC, RGB(0, 0, 0));
  964. SetTextColor(hXorMaskDC, RGB(255, 255, 255));
  965. BitBlt(hXorMaskDC, 0, 0, bm.bmWidth, bm.bmHeight, hAndMaskDC, 0, 0, SRCCOPY);
  966. BitBlt(hXorMaskDC, 0, 0, bm.bmWidth, bm.bmHeight, hMainDC, 0, 0, SRCAND);
  967. // Deselect bitmaps from the helper DC
  968. SelectObject(hMainDC, hOldMainBitmap);
  969. SelectObject(hAndMaskDC, hOldAndMaskBitmap);
  970. SelectObject(hXorMaskDC, hOldXorMaskBitmap);
  971. // Delete the helper DC
  972. DeleteDC(hXorMaskDC);
  973. DeleteDC(hAndMaskDC);
  974. DeleteDC(hMainDC);
  975. }
  976. void DisplayServerWindows::cursor_set_custom_image(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  977. _THREAD_SAFE_METHOD_
  978. if (p_cursor.is_valid()) {
  979. Map<CursorShape, Vector<Variant>>::Element *cursor_c = cursors_cache.find(p_shape);
  980. if (cursor_c) {
  981. if (cursor_c->get()[0] == p_cursor && cursor_c->get()[1] == p_hotspot) {
  982. cursor_set_shape(p_shape);
  983. return;
  984. }
  985. cursors_cache.erase(p_shape);
  986. }
  987. Ref<Texture2D> texture = p_cursor;
  988. Ref<AtlasTexture> atlas_texture = p_cursor;
  989. Ref<Image> image;
  990. Size2 texture_size;
  991. Rect2 atlas_rect;
  992. if (texture.is_valid()) {
  993. image = texture->get_data();
  994. }
  995. if (!image.is_valid() && atlas_texture.is_valid()) {
  996. texture = atlas_texture->get_atlas();
  997. atlas_rect.size.width = texture->get_width();
  998. atlas_rect.size.height = texture->get_height();
  999. atlas_rect.position.x = atlas_texture->get_region().position.x;
  1000. atlas_rect.position.y = atlas_texture->get_region().position.y;
  1001. texture_size.width = atlas_texture->get_region().size.x;
  1002. texture_size.height = atlas_texture->get_region().size.y;
  1003. } else if (image.is_valid()) {
  1004. texture_size.width = texture->get_width();
  1005. texture_size.height = texture->get_height();
  1006. }
  1007. ERR_FAIL_COND(!texture.is_valid());
  1008. ERR_FAIL_COND(p_hotspot.x < 0 || p_hotspot.y < 0);
  1009. ERR_FAIL_COND(texture_size.width > 256 || texture_size.height > 256);
  1010. ERR_FAIL_COND(p_hotspot.x > texture_size.width || p_hotspot.y > texture_size.height);
  1011. image = texture->get_data();
  1012. ERR_FAIL_COND(!image.is_valid());
  1013. UINT image_size = texture_size.width * texture_size.height;
  1014. // Create the BITMAP with alpha channel
  1015. COLORREF *buffer = (COLORREF *)memalloc(sizeof(COLORREF) * image_size);
  1016. for (UINT index = 0; index < image_size; index++) {
  1017. int row_index = floor(index / texture_size.width) + atlas_rect.position.y;
  1018. int column_index = (index % int(texture_size.width)) + atlas_rect.position.x;
  1019. if (atlas_texture.is_valid()) {
  1020. column_index = MIN(column_index, atlas_rect.size.width - 1);
  1021. row_index = MIN(row_index, atlas_rect.size.height - 1);
  1022. }
  1023. *(buffer + index) = image->get_pixel(column_index, row_index).to_argb32();
  1024. }
  1025. // Using 4 channels, so 4 * 8 bits
  1026. HBITMAP bitmap = CreateBitmap(texture_size.width, texture_size.height, 1, 4 * 8, buffer);
  1027. COLORREF clrTransparent = -1;
  1028. // Create the AND and XOR masks for the bitmap
  1029. HBITMAP hAndMask = nullptr;
  1030. HBITMAP hXorMask = nullptr;
  1031. GetMaskBitmaps(bitmap, clrTransparent, hAndMask, hXorMask);
  1032. if (nullptr == hAndMask || nullptr == hXorMask) {
  1033. memfree(buffer);
  1034. DeleteObject(bitmap);
  1035. return;
  1036. }
  1037. // Finally, create the icon
  1038. ICONINFO iconinfo;
  1039. iconinfo.fIcon = FALSE;
  1040. iconinfo.xHotspot = p_hotspot.x;
  1041. iconinfo.yHotspot = p_hotspot.y;
  1042. iconinfo.hbmMask = hAndMask;
  1043. iconinfo.hbmColor = hXorMask;
  1044. if (cursors[p_shape])
  1045. DestroyIcon(cursors[p_shape]);
  1046. cursors[p_shape] = CreateIconIndirect(&iconinfo);
  1047. Vector<Variant> params;
  1048. params.push_back(p_cursor);
  1049. params.push_back(p_hotspot);
  1050. cursors_cache.insert(p_shape, params);
  1051. if (p_shape == cursor_shape) {
  1052. if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) {
  1053. SetCursor(cursors[p_shape]);
  1054. }
  1055. }
  1056. if (hAndMask != nullptr) {
  1057. DeleteObject(hAndMask);
  1058. }
  1059. if (hXorMask != nullptr) {
  1060. DeleteObject(hXorMask);
  1061. }
  1062. memfree(buffer);
  1063. DeleteObject(bitmap);
  1064. } else {
  1065. // Reset to default system cursor
  1066. if (cursors[p_shape]) {
  1067. DestroyIcon(cursors[p_shape]);
  1068. cursors[p_shape] = nullptr;
  1069. }
  1070. CursorShape c = cursor_shape;
  1071. cursor_shape = CURSOR_MAX;
  1072. cursor_set_shape(c);
  1073. cursors_cache.erase(p_shape);
  1074. }
  1075. }
  1076. bool DisplayServerWindows::get_swap_ok_cancel() {
  1077. return true;
  1078. }
  1079. void DisplayServerWindows::enable_for_stealing_focus(OS::ProcessID pid) {
  1080. _THREAD_SAFE_METHOD_
  1081. AllowSetForegroundWindow(pid);
  1082. }
  1083. DisplayServer::LatinKeyboardVariant DisplayServerWindows::get_latin_keyboard_variant() const {
  1084. _THREAD_SAFE_METHOD_
  1085. unsigned long azerty[] = {
  1086. 0x00020401, // Arabic (102) AZERTY
  1087. 0x0001080c, // Belgian (Comma)
  1088. 0x0000080c, // Belgian French
  1089. 0x0000040c, // French
  1090. 0 // <--- STOP MARK
  1091. };
  1092. unsigned long qwertz[] = {
  1093. 0x0000041a, // Croation
  1094. 0x00000405, // Czech
  1095. 0x00000407, // German
  1096. 0x00010407, // German (IBM)
  1097. 0x0000040e, // Hungarian
  1098. 0x0000046e, // Luxembourgish
  1099. 0x00010415, // Polish (214)
  1100. 0x00000418, // Romanian (Legacy)
  1101. 0x0000081a, // Serbian (Latin)
  1102. 0x0000041b, // Slovak
  1103. 0x00000424, // Slovenian
  1104. 0x0001042e, // Sorbian Extended
  1105. 0x0002042e, // Sorbian Standard
  1106. 0x0000042e, // Sorbian Standard (Legacy)
  1107. 0x0000100c, // Swiss French
  1108. 0x00000807, // Swiss German
  1109. 0 // <--- STOP MARK
  1110. };
  1111. unsigned long dvorak[] = {
  1112. 0x00010409, // US-Dvorak
  1113. 0x00030409, // US-Dvorak for left hand
  1114. 0x00040409, // US-Dvorak for right hand
  1115. 0 // <--- STOP MARK
  1116. };
  1117. char name[KL_NAMELENGTH + 1];
  1118. name[0] = 0;
  1119. GetKeyboardLayoutNameA(name);
  1120. unsigned long hex = strtoul(name, nullptr, 16);
  1121. int i = 0;
  1122. while (azerty[i] != 0) {
  1123. if (azerty[i] == hex)
  1124. return LATIN_KEYBOARD_AZERTY;
  1125. i++;
  1126. }
  1127. i = 0;
  1128. while (qwertz[i] != 0) {
  1129. if (qwertz[i] == hex)
  1130. return LATIN_KEYBOARD_QWERTZ;
  1131. i++;
  1132. }
  1133. i = 0;
  1134. while (dvorak[i] != 0) {
  1135. if (dvorak[i] == hex)
  1136. return LATIN_KEYBOARD_DVORAK;
  1137. i++;
  1138. }
  1139. return LATIN_KEYBOARD_QWERTY;
  1140. }
  1141. void DisplayServerWindows::process_events() {
  1142. _THREAD_SAFE_METHOD_
  1143. MSG msg;
  1144. if (!drop_events) {
  1145. joypad->process_joypads();
  1146. }
  1147. while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) {
  1148. TranslateMessage(&msg);
  1149. DispatchMessageW(&msg);
  1150. }
  1151. if (!drop_events) {
  1152. _process_key_events();
  1153. Input::get_singleton()->flush_accumulated_events();
  1154. }
  1155. }
  1156. void DisplayServerWindows::force_process_and_drop_events() {
  1157. _THREAD_SAFE_METHOD_
  1158. drop_events = true;
  1159. process_events();
  1160. drop_events = false;
  1161. }
  1162. void DisplayServerWindows::release_rendering_thread() {
  1163. }
  1164. void DisplayServerWindows::make_rendering_thread() {
  1165. }
  1166. void DisplayServerWindows::swap_buffers() {
  1167. }
  1168. void DisplayServerWindows::set_native_icon(const String &p_filename) {
  1169. _THREAD_SAFE_METHOD_
  1170. FileAccess *f = FileAccess::open(p_filename, FileAccess::READ);
  1171. ERR_FAIL_COND_MSG(!f, "Cannot open file with icon '" + p_filename + "'.");
  1172. ICONDIR *icon_dir = (ICONDIR *)memalloc(sizeof(ICONDIR));
  1173. int pos = 0;
  1174. icon_dir->idReserved = f->get_32();
  1175. pos += sizeof(WORD);
  1176. f->seek(pos);
  1177. icon_dir->idType = f->get_32();
  1178. pos += sizeof(WORD);
  1179. f->seek(pos);
  1180. ERR_FAIL_COND_MSG(icon_dir->idType != 1, "Invalid icon file format!");
  1181. icon_dir->idCount = f->get_32();
  1182. pos += sizeof(WORD);
  1183. f->seek(pos);
  1184. icon_dir = (ICONDIR *)memrealloc(icon_dir, 3 * sizeof(WORD) + icon_dir->idCount * sizeof(ICONDIRENTRY));
  1185. f->get_buffer((uint8_t *)&icon_dir->idEntries[0], icon_dir->idCount * sizeof(ICONDIRENTRY));
  1186. int small_icon_index = -1; // Select 16x16 with largest color count
  1187. int small_icon_cc = 0;
  1188. int big_icon_index = -1; // Select largest
  1189. int big_icon_width = 16;
  1190. int big_icon_cc = 0;
  1191. for (int i = 0; i < icon_dir->idCount; i++) {
  1192. int colors = (icon_dir->idEntries[i].bColorCount == 0) ? 32768 : icon_dir->idEntries[i].bColorCount;
  1193. int width = (icon_dir->idEntries[i].bWidth == 0) ? 256 : icon_dir->idEntries[i].bWidth;
  1194. if (width == 16) {
  1195. if (colors >= small_icon_cc) {
  1196. small_icon_index = i;
  1197. small_icon_cc = colors;
  1198. }
  1199. }
  1200. if (width >= big_icon_width) {
  1201. if (colors >= big_icon_cc) {
  1202. big_icon_index = i;
  1203. big_icon_width = width;
  1204. big_icon_cc = colors;
  1205. }
  1206. }
  1207. }
  1208. ERR_FAIL_COND_MSG(big_icon_index == -1, "No valid icons found!");
  1209. if (small_icon_index == -1) {
  1210. WARN_PRINT("No small icon found, reusing " + itos(big_icon_width) + "x" + itos(big_icon_width) + " @" + itos(big_icon_cc) + " icon!");
  1211. small_icon_index = big_icon_index;
  1212. small_icon_cc = big_icon_cc;
  1213. }
  1214. // Read the big icon
  1215. DWORD bytecount_big = icon_dir->idEntries[big_icon_index].dwBytesInRes;
  1216. Vector<uint8_t> data_big;
  1217. data_big.resize(bytecount_big);
  1218. pos = icon_dir->idEntries[big_icon_index].dwImageOffset;
  1219. f->seek(pos);
  1220. f->get_buffer((uint8_t *)&data_big.write[0], bytecount_big);
  1221. HICON icon_big = CreateIconFromResource((PBYTE)&data_big.write[0], bytecount_big, TRUE, 0x00030000);
  1222. ERR_FAIL_COND_MSG(!icon_big, "Could not create " + itos(big_icon_width) + "x" + itos(big_icon_width) + " @" + itos(big_icon_cc) + " icon, error: " + format_error_message(GetLastError()) + ".");
  1223. // Read the small icon
  1224. DWORD bytecount_small = icon_dir->idEntries[small_icon_index].dwBytesInRes;
  1225. Vector<uint8_t> data_small;
  1226. data_small.resize(bytecount_small);
  1227. pos = icon_dir->idEntries[small_icon_index].dwImageOffset;
  1228. f->seek(pos);
  1229. f->get_buffer((uint8_t *)&data_small.write[0], bytecount_small);
  1230. HICON icon_small = CreateIconFromResource((PBYTE)&data_small.write[0], bytecount_small, TRUE, 0x00030000);
  1231. ERR_FAIL_COND_MSG(!icon_small, "Could not create 16x16 @" + itos(small_icon_cc) + " icon, error: " + format_error_message(GetLastError()) + ".");
  1232. // Online tradition says to be sure last error is cleared and set the small icon first
  1233. int err = 0;
  1234. SetLastError(err);
  1235. SendMessage(windows[MAIN_WINDOW_ID].hWnd, WM_SETICON, ICON_SMALL, (LPARAM)icon_small);
  1236. err = GetLastError();
  1237. ERR_FAIL_COND_MSG(err, "Error setting ICON_SMALL: " + format_error_message(err) + ".");
  1238. SendMessage(windows[MAIN_WINDOW_ID].hWnd, WM_SETICON, ICON_BIG, (LPARAM)icon_big);
  1239. err = GetLastError();
  1240. ERR_FAIL_COND_MSG(err, "Error setting ICON_BIG: " + format_error_message(err) + ".");
  1241. memdelete(f);
  1242. memdelete(icon_dir);
  1243. }
  1244. void DisplayServerWindows::set_icon(const Ref<Image> &p_icon) {
  1245. _THREAD_SAFE_METHOD_
  1246. ERR_FAIL_COND(!p_icon.is_valid());
  1247. Ref<Image> icon = p_icon->duplicate();
  1248. if (icon->get_format() != Image::FORMAT_RGBA8)
  1249. icon->convert(Image::FORMAT_RGBA8);
  1250. int w = icon->get_width();
  1251. int h = icon->get_height();
  1252. /* Create temporary bitmap buffer */
  1253. int icon_len = 40 + h * w * 4;
  1254. Vector<BYTE> v;
  1255. v.resize(icon_len);
  1256. BYTE *icon_bmp = v.ptrw();
  1257. encode_uint32(40, &icon_bmp[0]);
  1258. encode_uint32(w, &icon_bmp[4]);
  1259. encode_uint32(h * 2, &icon_bmp[8]);
  1260. encode_uint16(1, &icon_bmp[12]);
  1261. encode_uint16(32, &icon_bmp[14]);
  1262. encode_uint32(BI_RGB, &icon_bmp[16]);
  1263. encode_uint32(w * h * 4, &icon_bmp[20]);
  1264. encode_uint32(0, &icon_bmp[24]);
  1265. encode_uint32(0, &icon_bmp[28]);
  1266. encode_uint32(0, &icon_bmp[32]);
  1267. encode_uint32(0, &icon_bmp[36]);
  1268. uint8_t *wr = &icon_bmp[40];
  1269. const uint8_t *r = icon->get_data().ptr();
  1270. for (int i = 0; i < h; i++) {
  1271. for (int j = 0; j < w; j++) {
  1272. const uint8_t *rpx = &r[((h - i - 1) * w + j) * 4];
  1273. uint8_t *wpx = &wr[(i * w + j) * 4];
  1274. wpx[0] = rpx[2];
  1275. wpx[1] = rpx[1];
  1276. wpx[2] = rpx[0];
  1277. wpx[3] = rpx[3];
  1278. }
  1279. }
  1280. HICON hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
  1281. /* Set the icon for the window */
  1282. SendMessage(windows[MAIN_WINDOW_ID].hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hicon);
  1283. /* Set the icon in the task manager (should we do this?) */
  1284. SendMessage(windows[MAIN_WINDOW_ID].hWnd, WM_SETICON, ICON_BIG, (LPARAM)hicon);
  1285. }
  1286. void DisplayServerWindows::vsync_set_use_via_compositor(bool p_enable) {
  1287. }
  1288. bool DisplayServerWindows::vsync_is_using_via_compositor() const {
  1289. return false;
  1290. }
  1291. void DisplayServerWindows::set_context(Context p_context) {
  1292. }
  1293. #define MI_WP_SIGNATURE 0xFF515700
  1294. #define SIGNATURE_MASK 0xFFFFFF00
  1295. // Keeping the name suggested by Microsoft, but this macro really answers:
  1296. // Is this mouse event emulated from touch or pen input?
  1297. #define IsPenEvent(dw) (((dw)&SIGNATURE_MASK) == MI_WP_SIGNATURE)
  1298. // This one tells whether the event comes from touchscreen (and not from pen)
  1299. #define IsTouchEvent(dw) (IsPenEvent(dw) && ((dw)&0x80))
  1300. void DisplayServerWindows::_touch_event(WindowID p_window, bool p_pressed, float p_x, float p_y, int idx) {
  1301. // Defensive
  1302. if (touch_state.has(idx) == p_pressed)
  1303. return;
  1304. if (p_pressed) {
  1305. touch_state.insert(idx, Vector2(p_x, p_y));
  1306. } else {
  1307. touch_state.erase(idx);
  1308. }
  1309. Ref<InputEventScreenTouch> event;
  1310. event.instance();
  1311. event->set_index(idx);
  1312. event->set_window_id(p_window);
  1313. event->set_pressed(p_pressed);
  1314. event->set_position(Vector2(p_x, p_y));
  1315. Input::get_singleton()->accumulate_input_event(event);
  1316. }
  1317. void DisplayServerWindows::_drag_event(WindowID p_window, float p_x, float p_y, int idx) {
  1318. Map<int, Vector2>::Element *curr = touch_state.find(idx);
  1319. // Defensive
  1320. if (!curr)
  1321. return;
  1322. if (curr->get() == Vector2(p_x, p_y))
  1323. return;
  1324. Ref<InputEventScreenDrag> event;
  1325. event.instance();
  1326. event->set_window_id(p_window);
  1327. event->set_index(idx);
  1328. event->set_position(Vector2(p_x, p_y));
  1329. event->set_relative(Vector2(p_x, p_y) - curr->get());
  1330. Input::get_singleton()->accumulate_input_event(event);
  1331. curr->get() = Vector2(p_x, p_y);
  1332. }
  1333. void DisplayServerWindows::_send_window_event(const WindowData &wd, WindowEvent p_event) {
  1334. if (!wd.event_callback.is_null()) {
  1335. Variant event = int(p_event);
  1336. Variant *eventp = &event;
  1337. Variant ret;
  1338. Callable::CallError ce;
  1339. wd.event_callback.call((const Variant **)&eventp, 1, ret, ce);
  1340. }
  1341. }
  1342. void DisplayServerWindows::_dispatch_input_events(const Ref<InputEvent> &p_event) {
  1343. ((DisplayServerWindows *)(get_singleton()))->_dispatch_input_event(p_event);
  1344. }
  1345. void DisplayServerWindows::_dispatch_input_event(const Ref<InputEvent> &p_event) {
  1346. Variant ev = p_event;
  1347. Variant *evp = &ev;
  1348. Variant ret;
  1349. Callable::CallError ce;
  1350. Ref<InputEventFromWindow> event_from_window = p_event;
  1351. if (event_from_window.is_valid() && event_from_window->get_window_id() != INVALID_WINDOW_ID) {
  1352. //send to a window
  1353. ERR_FAIL_COND(!windows.has(event_from_window->get_window_id()));
  1354. Callable callable = windows[event_from_window->get_window_id()].input_event_callback;
  1355. if (callable.is_null()) {
  1356. return;
  1357. }
  1358. callable.call((const Variant **)&evp, 1, ret, ce);
  1359. } else {
  1360. //send to all windows
  1361. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  1362. Callable callable = E->get().input_event_callback;
  1363. if (callable.is_null()) {
  1364. continue;
  1365. }
  1366. callable.call((const Variant **)&evp, 1, ret, ce);
  1367. }
  1368. }
  1369. }
  1370. LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  1371. if (drop_events) {
  1372. if (user_proc) {
  1373. return CallWindowProcW(user_proc, hWnd, uMsg, wParam, lParam);
  1374. } else {
  1375. return DefWindowProcW(hWnd, uMsg, wParam, lParam);
  1376. }
  1377. };
  1378. WindowID window_id = INVALID_WINDOW_ID;
  1379. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  1380. if (E->get().hWnd == hWnd) {
  1381. window_id = E->key();
  1382. break;
  1383. }
  1384. }
  1385. switch (uMsg) // Check For Windows Messages
  1386. {
  1387. case WM_SETFOCUS: {
  1388. windows[window_id].window_has_focus = true;
  1389. last_focused_window = window_id;
  1390. // Restore mouse mode
  1391. _set_mouse_mode_impl(mouse_mode);
  1392. break;
  1393. }
  1394. case WM_KILLFOCUS: {
  1395. windows[window_id].window_has_focus = false;
  1396. last_focused_window = window_id;
  1397. // Release capture unconditionally because it can be set due to dragging, in addition to captured mode
  1398. ReleaseCapture();
  1399. // Release every touch to avoid sticky points
  1400. for (Map<int, Vector2>::Element *E = touch_state.front(); E; E = E->next()) {
  1401. _touch_event(window_id, false, E->get().x, E->get().y, E->key());
  1402. }
  1403. touch_state.clear();
  1404. break;
  1405. }
  1406. case WM_ACTIVATE: // Watch For Window Activate Message
  1407. {
  1408. windows[window_id].minimized = HIWORD(wParam) != 0;
  1409. if (LOWORD(wParam) == WA_ACTIVE || LOWORD(wParam) == WA_CLICKACTIVE) {
  1410. _send_window_event(windows[window_id], WINDOW_EVENT_FOCUS_IN);
  1411. windows[window_id].window_focused = true;
  1412. alt_mem = false;
  1413. control_mem = false;
  1414. shift_mem = false;
  1415. } else { // WM_INACTIVE
  1416. Input::get_singleton()->release_pressed_events();
  1417. _send_window_event(windows[window_id], WINDOW_EVENT_FOCUS_OUT);
  1418. windows[window_id].window_focused = false;
  1419. alt_mem = false;
  1420. };
  1421. if (!OS::get_singleton()->is_wintab_disabled() && wintab_available && windows[window_id].wtctx) {
  1422. wintab_WTEnable(windows[window_id].wtctx, GET_WM_ACTIVATE_STATE(wParam, lParam));
  1423. }
  1424. return 0; // Return To The Message Loop
  1425. }
  1426. case WM_GETMINMAXINFO: {
  1427. if (windows[window_id].resizable && !windows[window_id].fullscreen) {
  1428. Size2 decor = window_get_size(window_id) - window_get_real_size(window_id); // Size of window decorations
  1429. MINMAXINFO *min_max_info = (MINMAXINFO *)lParam;
  1430. if (windows[window_id].min_size != Size2()) {
  1431. min_max_info->ptMinTrackSize.x = windows[window_id].min_size.x + decor.x;
  1432. min_max_info->ptMinTrackSize.y = windows[window_id].min_size.y + decor.y;
  1433. }
  1434. if (windows[window_id].max_size != Size2()) {
  1435. min_max_info->ptMaxTrackSize.x = windows[window_id].max_size.x + decor.x;
  1436. min_max_info->ptMaxTrackSize.y = windows[window_id].max_size.y + decor.y;
  1437. }
  1438. return 0;
  1439. } else {
  1440. break;
  1441. }
  1442. }
  1443. case WM_PAINT:
  1444. Main::force_redraw();
  1445. break;
  1446. case WM_SYSCOMMAND: // Intercept System Commands
  1447. {
  1448. switch (wParam) // Check System Calls
  1449. {
  1450. case SC_SCREENSAVE: // Screensaver Trying To Start?
  1451. case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
  1452. return 0; // Prevent From Happening
  1453. case SC_KEYMENU:
  1454. if ((lParam >> 16) <= 0)
  1455. return 0;
  1456. }
  1457. break; // Exit
  1458. }
  1459. case WM_CLOSE: // Did We Receive A Close Message?
  1460. {
  1461. _send_window_event(windows[window_id], WINDOW_EVENT_CLOSE_REQUEST);
  1462. return 0; // Jump Back
  1463. }
  1464. case WM_MOUSELEAVE: {
  1465. old_invalid = true;
  1466. outside = true;
  1467. _send_window_event(windows[window_id], WINDOW_EVENT_MOUSE_EXIT);
  1468. } break;
  1469. case WM_INPUT: {
  1470. if (mouse_mode != MOUSE_MODE_CAPTURED || !use_raw_input) {
  1471. break;
  1472. }
  1473. UINT dwSize;
  1474. GetRawInputData((HRAWINPUT)lParam, RID_INPUT, nullptr, &dwSize, sizeof(RAWINPUTHEADER));
  1475. LPBYTE lpb = new BYTE[dwSize];
  1476. if (lpb == nullptr) {
  1477. return 0;
  1478. }
  1479. if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)) != dwSize)
  1480. OutputDebugString(TEXT("GetRawInputData does not return correct size !\n"));
  1481. RAWINPUT *raw = (RAWINPUT *)lpb;
  1482. if (raw->header.dwType == RIM_TYPEMOUSE) {
  1483. Ref<InputEventMouseMotion> mm;
  1484. mm.instance();
  1485. mm->set_window_id(window_id);
  1486. mm->set_control(control_mem);
  1487. mm->set_shift(shift_mem);
  1488. mm->set_alt(alt_mem);
  1489. mm->set_pressure((raw->data.mouse.ulButtons & RI_MOUSE_LEFT_BUTTON_DOWN) ? 1.0f : 0.0f);
  1490. mm->set_button_mask(last_button_state);
  1491. Point2i c(windows[window_id].width / 2, windows[window_id].height / 2);
  1492. // centering just so it works as before
  1493. POINT pos = { (int)c.x, (int)c.y };
  1494. ClientToScreen(windows[window_id].hWnd, &pos);
  1495. SetCursorPos(pos.x, pos.y);
  1496. mm->set_position(c);
  1497. mm->set_global_position(c);
  1498. Input::get_singleton()->set_mouse_position(c);
  1499. mm->set_speed(Vector2(0, 0));
  1500. if (raw->data.mouse.usFlags == MOUSE_MOVE_RELATIVE) {
  1501. mm->set_relative(Vector2(raw->data.mouse.lLastX, raw->data.mouse.lLastY));
  1502. } else if (raw->data.mouse.usFlags == MOUSE_MOVE_ABSOLUTE) {
  1503. int nScreenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN);
  1504. int nScreenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);
  1505. int nScreenLeft = GetSystemMetrics(SM_XVIRTUALSCREEN);
  1506. int nScreenTop = GetSystemMetrics(SM_YVIRTUALSCREEN);
  1507. Vector2 abs_pos(
  1508. (double(raw->data.mouse.lLastX) - 65536.0 / (nScreenWidth)) * nScreenWidth / 65536.0 + nScreenLeft,
  1509. (double(raw->data.mouse.lLastY) - 65536.0 / (nScreenHeight)) * nScreenHeight / 65536.0 + nScreenTop);
  1510. POINT coords; //client coords
  1511. coords.x = abs_pos.x;
  1512. coords.y = abs_pos.y;
  1513. ScreenToClient(hWnd, &coords);
  1514. mm->set_relative(Vector2(coords.x - old_x, coords.y - old_y));
  1515. old_x = coords.x;
  1516. old_y = coords.y;
  1517. /*Input.mi.dx = (int)((((double)(pos.x)-nScreenLeft) * 65536) / nScreenWidth + 65536 / (nScreenWidth));
  1518. Input.mi.dy = (int)((((double)(pos.y)-nScreenTop) * 65536) / nScreenHeight + 65536 / (nScreenHeight));
  1519. */
  1520. }
  1521. if (windows[window_id].window_has_focus && mm->get_relative() != Vector2())
  1522. Input::get_singleton()->accumulate_input_event(mm);
  1523. }
  1524. delete[] lpb;
  1525. } break;
  1526. case WT_CSRCHANGE:
  1527. case WT_PROXIMITY: {
  1528. if (!OS::get_singleton()->is_wintab_disabled() && wintab_available && windows[window_id].wtctx) {
  1529. AXIS pressure;
  1530. if (wintab_WTInfo(WTI_DEVICES + windows[window_id].wtlc.lcDevice, DVC_NPRESSURE, &pressure)) {
  1531. windows[window_id].min_pressure = int(pressure.axMin);
  1532. windows[window_id].max_pressure = int(pressure.axMax);
  1533. }
  1534. AXIS orientation[3];
  1535. if (wintab_WTInfo(WTI_DEVICES + windows[window_id].wtlc.lcDevice, DVC_ORIENTATION, &orientation)) {
  1536. windows[window_id].tilt_supported = orientation[0].axResolution && orientation[1].axResolution;
  1537. }
  1538. return 0;
  1539. }
  1540. } break;
  1541. case WT_PACKET: {
  1542. if (!OS::get_singleton()->is_wintab_disabled() && wintab_available && windows[window_id].wtctx) {
  1543. PACKET packet;
  1544. if (wintab_WTPacket(windows[window_id].wtctx, wParam, &packet)) {
  1545. float pressure = float(packet.pkNormalPressure - windows[window_id].min_pressure) / float(windows[window_id].max_pressure - windows[window_id].min_pressure);
  1546. windows[window_id].last_pressure = pressure;
  1547. windows[window_id].last_pressure_update = 0;
  1548. double azim = (packet.pkOrientation.orAzimuth / 10.0f) * (Math_PI / 180);
  1549. double alt = Math::tan((Math::abs(packet.pkOrientation.orAltitude / 10.0f)) * (Math_PI / 180));
  1550. if (windows[window_id].tilt_supported) {
  1551. windows[window_id].last_tilt = Vector2(Math::atan(Math::sin(azim) / alt), Math::atan(Math::cos(azim) / alt));
  1552. } else {
  1553. windows[window_id].last_tilt = Vector2();
  1554. }
  1555. POINT coords;
  1556. GetCursorPos(&coords);
  1557. ScreenToClient(windows[window_id].hWnd, &coords);
  1558. // Don't calculate relative mouse movement if we don't have focus in CAPTURED mode.
  1559. if (!windows[window_id].window_has_focus && mouse_mode == MOUSE_MODE_CAPTURED)
  1560. break;
  1561. Ref<InputEventMouseMotion> mm;
  1562. mm.instance();
  1563. mm->set_window_id(window_id);
  1564. mm->set_control(GetKeyState(VK_CONTROL) != 0);
  1565. mm->set_shift(GetKeyState(VK_SHIFT) != 0);
  1566. mm->set_alt(alt_mem);
  1567. mm->set_pressure(windows[window_id].last_pressure);
  1568. mm->set_tilt(windows[window_id].last_tilt);
  1569. mm->set_button_mask(last_button_state);
  1570. mm->set_position(Vector2(coords.x, coords.y));
  1571. mm->set_global_position(Vector2(coords.x, coords.y));
  1572. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  1573. Point2i c(windows[window_id].width / 2, windows[window_id].height / 2);
  1574. old_x = c.x;
  1575. old_y = c.y;
  1576. if (mm->get_position() == c) {
  1577. center = c;
  1578. return 0;
  1579. }
  1580. Point2i ncenter = mm->get_position();
  1581. center = ncenter;
  1582. POINT pos = { (int)c.x, (int)c.y };
  1583. ClientToScreen(windows[window_id].hWnd, &pos);
  1584. SetCursorPos(pos.x, pos.y);
  1585. }
  1586. Input::get_singleton()->set_mouse_position(mm->get_position());
  1587. mm->set_speed(Input::get_singleton()->get_last_mouse_speed());
  1588. if (old_invalid) {
  1589. old_x = mm->get_position().x;
  1590. old_y = mm->get_position().y;
  1591. old_invalid = false;
  1592. }
  1593. mm->set_relative(Vector2(mm->get_position() - Vector2(old_x, old_y)));
  1594. old_x = mm->get_position().x;
  1595. old_y = mm->get_position().y;
  1596. if (windows[window_id].window_has_focus)
  1597. Input::get_singleton()->parse_input_event(mm);
  1598. }
  1599. return 0;
  1600. }
  1601. } break;
  1602. case WM_POINTERUPDATE: {
  1603. if (mouse_mode == MOUSE_MODE_CAPTURED && use_raw_input) {
  1604. break;
  1605. }
  1606. if (!win8p_GetPointerType || !win8p_GetPointerPenInfo) {
  1607. break;
  1608. }
  1609. uint32_t pointer_id = LOWORD(wParam);
  1610. POINTER_INPUT_TYPE pointer_type = PT_POINTER;
  1611. if (!win8p_GetPointerType(pointer_id, &pointer_type)) {
  1612. break;
  1613. }
  1614. if (pointer_type != PT_PEN) {
  1615. break;
  1616. }
  1617. POINTER_PEN_INFO pen_info;
  1618. if (!win8p_GetPointerPenInfo(pointer_id, &pen_info)) {
  1619. break;
  1620. }
  1621. if (Input::get_singleton()->is_emulating_mouse_from_touch()) {
  1622. // Universal translation enabled; ignore OS translation
  1623. LPARAM extra = GetMessageExtraInfo();
  1624. if (IsTouchEvent(extra)) {
  1625. break;
  1626. }
  1627. }
  1628. if (outside) {
  1629. //mouse enter
  1630. if (mouse_mode != MOUSE_MODE_CAPTURED) {
  1631. _send_window_event(windows[window_id], WINDOW_EVENT_MOUSE_ENTER);
  1632. }
  1633. CursorShape c = cursor_shape;
  1634. cursor_shape = CURSOR_MAX;
  1635. cursor_set_shape(c);
  1636. outside = false;
  1637. //Once-Off notification, must call again....
  1638. TRACKMOUSEEVENT tme;
  1639. tme.cbSize = sizeof(TRACKMOUSEEVENT);
  1640. tme.dwFlags = TME_LEAVE;
  1641. tme.hwndTrack = hWnd;
  1642. tme.dwHoverTime = HOVER_DEFAULT;
  1643. TrackMouseEvent(&tme);
  1644. }
  1645. // Don't calculate relative mouse movement if we don't have focus in CAPTURED mode.
  1646. if (!windows[window_id].window_has_focus && mouse_mode == MOUSE_MODE_CAPTURED)
  1647. break;
  1648. Ref<InputEventMouseMotion> mm;
  1649. mm.instance();
  1650. mm->set_window_id(window_id);
  1651. if (pen_info.penMask & PEN_MASK_PRESSURE) {
  1652. mm->set_pressure((float)pen_info.pressure / 1024);
  1653. } else {
  1654. mm->set_pressure((HIWORD(wParam) & POINTER_MESSAGE_FLAG_FIRSTBUTTON) ? 1.0f : 0.0f);
  1655. }
  1656. if ((pen_info.penMask & PEN_MASK_TILT_X) && (pen_info.penMask & PEN_MASK_TILT_Y)) {
  1657. mm->set_tilt(Vector2((float)pen_info.tiltX / 90, (float)pen_info.tiltY / 90));
  1658. }
  1659. mm->set_control((wParam & MK_CONTROL) != 0);
  1660. mm->set_shift((wParam & MK_SHIFT) != 0);
  1661. mm->set_alt(alt_mem);
  1662. mm->set_button_mask(last_button_state);
  1663. POINT coords; //client coords
  1664. coords.x = GET_X_LPARAM(lParam);
  1665. coords.y = GET_Y_LPARAM(lParam);
  1666. ScreenToClient(windows[window_id].hWnd, &coords);
  1667. mm->set_position(Vector2(coords.x, coords.y));
  1668. mm->set_global_position(Vector2(coords.x, coords.y));
  1669. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  1670. Point2i c(windows[window_id].width / 2, windows[window_id].height / 2);
  1671. old_x = c.x;
  1672. old_y = c.y;
  1673. if (mm->get_position() == c) {
  1674. center = c;
  1675. return 0;
  1676. }
  1677. Point2i ncenter = mm->get_position();
  1678. center = ncenter;
  1679. POINT pos = { (int)c.x, (int)c.y };
  1680. ClientToScreen(hWnd, &pos);
  1681. SetCursorPos(pos.x, pos.y);
  1682. }
  1683. Input::get_singleton()->set_mouse_position(mm->get_position());
  1684. mm->set_speed(Input::get_singleton()->get_last_mouse_speed());
  1685. if (old_invalid) {
  1686. old_x = mm->get_position().x;
  1687. old_y = mm->get_position().y;
  1688. old_invalid = false;
  1689. }
  1690. mm->set_relative(Vector2(mm->get_position() - Vector2(old_x, old_y)));
  1691. old_x = mm->get_position().x;
  1692. old_y = mm->get_position().y;
  1693. if (windows[window_id].window_has_focus) {
  1694. Input::get_singleton()->parse_input_event(mm);
  1695. }
  1696. return 0; //Pointer event handled return 0 to avoid duplicate WM_MOUSEMOVE event
  1697. } break;
  1698. case WM_MOUSEMOVE: {
  1699. if (mouse_mode == MOUSE_MODE_CAPTURED && use_raw_input) {
  1700. break;
  1701. }
  1702. if (Input::get_singleton()->is_emulating_mouse_from_touch()) {
  1703. // Universal translation enabled; ignore OS translation
  1704. LPARAM extra = GetMessageExtraInfo();
  1705. if (IsTouchEvent(extra)) {
  1706. break;
  1707. }
  1708. }
  1709. if (outside) {
  1710. //mouse enter
  1711. if (mouse_mode != MOUSE_MODE_CAPTURED) {
  1712. _send_window_event(windows[window_id], WINDOW_EVENT_MOUSE_ENTER);
  1713. }
  1714. CursorShape c = cursor_shape;
  1715. cursor_shape = CURSOR_MAX;
  1716. cursor_set_shape(c);
  1717. outside = false;
  1718. //Once-Off notification, must call again....
  1719. TRACKMOUSEEVENT tme;
  1720. tme.cbSize = sizeof(TRACKMOUSEEVENT);
  1721. tme.dwFlags = TME_LEAVE;
  1722. tme.hwndTrack = hWnd;
  1723. tme.dwHoverTime = HOVER_DEFAULT;
  1724. TrackMouseEvent(&tme);
  1725. }
  1726. // Don't calculate relative mouse movement if we don't have focus in CAPTURED mode.
  1727. if (!windows[window_id].window_has_focus && mouse_mode == MOUSE_MODE_CAPTURED)
  1728. break;
  1729. Ref<InputEventMouseMotion> mm;
  1730. mm.instance();
  1731. mm->set_window_id(window_id);
  1732. mm->set_control((wParam & MK_CONTROL) != 0);
  1733. mm->set_shift((wParam & MK_SHIFT) != 0);
  1734. mm->set_alt(alt_mem);
  1735. if (!OS::get_singleton()->is_wintab_disabled() && wintab_available && windows[window_id].wtctx) {
  1736. // Note: WinTab sends both WT_PACKET and WM_xBUTTONDOWN/UP/MOUSEMOVE events, use mouse 1/0 pressure only when last_pressure was not update recently.
  1737. if (windows[window_id].last_pressure_update < 10) {
  1738. windows[window_id].last_pressure_update++;
  1739. } else {
  1740. windows[window_id].last_tilt = Vector2();
  1741. windows[window_id].last_pressure = (wParam & MK_LBUTTON) ? 1.0f : 0.0f;
  1742. }
  1743. } else {
  1744. windows[window_id].last_tilt = Vector2();
  1745. windows[window_id].last_pressure = (wParam & MK_LBUTTON) ? 1.0f : 0.0f;
  1746. }
  1747. mm->set_pressure(windows[window_id].last_pressure);
  1748. mm->set_tilt(windows[window_id].last_tilt);
  1749. mm->set_button_mask(last_button_state);
  1750. mm->set_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)));
  1751. mm->set_global_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)));
  1752. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  1753. Point2i c(windows[window_id].width / 2, windows[window_id].height / 2);
  1754. old_x = c.x;
  1755. old_y = c.y;
  1756. if (mm->get_position() == c) {
  1757. center = c;
  1758. return 0;
  1759. }
  1760. Point2i ncenter = mm->get_position();
  1761. center = ncenter;
  1762. POINT pos = { (int)c.x, (int)c.y };
  1763. ClientToScreen(windows[window_id].hWnd, &pos);
  1764. SetCursorPos(pos.x, pos.y);
  1765. }
  1766. Input::get_singleton()->set_mouse_position(mm->get_position());
  1767. mm->set_speed(Input::get_singleton()->get_last_mouse_speed());
  1768. if (old_invalid) {
  1769. old_x = mm->get_position().x;
  1770. old_y = mm->get_position().y;
  1771. old_invalid = false;
  1772. }
  1773. mm->set_relative(Vector2(mm->get_position() - Vector2(old_x, old_y)));
  1774. old_x = mm->get_position().x;
  1775. old_y = mm->get_position().y;
  1776. if (windows[window_id].window_has_focus)
  1777. Input::get_singleton()->accumulate_input_event(mm);
  1778. } break;
  1779. case WM_LBUTTONDOWN:
  1780. case WM_LBUTTONUP:
  1781. if (Input::get_singleton()->is_emulating_mouse_from_touch()) {
  1782. // Universal translation enabled; ignore OS translations for left button
  1783. LPARAM extra = GetMessageExtraInfo();
  1784. if (IsTouchEvent(extra)) {
  1785. break;
  1786. }
  1787. }
  1788. [[fallthrough]];
  1789. case WM_MBUTTONDOWN:
  1790. case WM_MBUTTONUP:
  1791. case WM_RBUTTONDOWN:
  1792. case WM_RBUTTONUP:
  1793. case WM_MOUSEWHEEL:
  1794. case WM_MOUSEHWHEEL:
  1795. case WM_LBUTTONDBLCLK:
  1796. case WM_MBUTTONDBLCLK:
  1797. case WM_RBUTTONDBLCLK:
  1798. case WM_XBUTTONDBLCLK:
  1799. case WM_XBUTTONDOWN:
  1800. case WM_XBUTTONUP: {
  1801. Ref<InputEventMouseButton> mb;
  1802. mb.instance();
  1803. mb->set_window_id(window_id);
  1804. switch (uMsg) {
  1805. case WM_LBUTTONDOWN: {
  1806. mb->set_pressed(true);
  1807. mb->set_button_index(1);
  1808. } break;
  1809. case WM_LBUTTONUP: {
  1810. mb->set_pressed(false);
  1811. mb->set_button_index(1);
  1812. } break;
  1813. case WM_MBUTTONDOWN: {
  1814. mb->set_pressed(true);
  1815. mb->set_button_index(3);
  1816. } break;
  1817. case WM_MBUTTONUP: {
  1818. mb->set_pressed(false);
  1819. mb->set_button_index(3);
  1820. } break;
  1821. case WM_RBUTTONDOWN: {
  1822. mb->set_pressed(true);
  1823. mb->set_button_index(2);
  1824. } break;
  1825. case WM_RBUTTONUP: {
  1826. mb->set_pressed(false);
  1827. mb->set_button_index(2);
  1828. } break;
  1829. case WM_LBUTTONDBLCLK: {
  1830. mb->set_pressed(true);
  1831. mb->set_button_index(1);
  1832. mb->set_doubleclick(true);
  1833. } break;
  1834. case WM_RBUTTONDBLCLK: {
  1835. mb->set_pressed(true);
  1836. mb->set_button_index(2);
  1837. mb->set_doubleclick(true);
  1838. } break;
  1839. case WM_MBUTTONDBLCLK: {
  1840. mb->set_pressed(true);
  1841. mb->set_button_index(3);
  1842. mb->set_doubleclick(true);
  1843. } break;
  1844. case WM_MOUSEWHEEL: {
  1845. mb->set_pressed(true);
  1846. int motion = (short)HIWORD(wParam);
  1847. if (!motion)
  1848. return 0;
  1849. if (motion > 0)
  1850. mb->set_button_index(BUTTON_WHEEL_UP);
  1851. else
  1852. mb->set_button_index(BUTTON_WHEEL_DOWN);
  1853. } break;
  1854. case WM_MOUSEHWHEEL: {
  1855. mb->set_pressed(true);
  1856. int motion = (short)HIWORD(wParam);
  1857. if (!motion)
  1858. return 0;
  1859. if (motion < 0) {
  1860. mb->set_button_index(BUTTON_WHEEL_LEFT);
  1861. mb->set_factor(fabs((double)motion / (double)WHEEL_DELTA));
  1862. } else {
  1863. mb->set_button_index(BUTTON_WHEEL_RIGHT);
  1864. mb->set_factor(fabs((double)motion / (double)WHEEL_DELTA));
  1865. }
  1866. } break;
  1867. case WM_XBUTTONDOWN: {
  1868. mb->set_pressed(true);
  1869. if (HIWORD(wParam) == XBUTTON1)
  1870. mb->set_button_index(BUTTON_XBUTTON1);
  1871. else
  1872. mb->set_button_index(BUTTON_XBUTTON2);
  1873. } break;
  1874. case WM_XBUTTONUP: {
  1875. mb->set_pressed(false);
  1876. if (HIWORD(wParam) == XBUTTON1)
  1877. mb->set_button_index(BUTTON_XBUTTON1);
  1878. else
  1879. mb->set_button_index(BUTTON_XBUTTON2);
  1880. } break;
  1881. case WM_XBUTTONDBLCLK: {
  1882. mb->set_pressed(true);
  1883. if (HIWORD(wParam) == XBUTTON1)
  1884. mb->set_button_index(BUTTON_XBUTTON1);
  1885. else
  1886. mb->set_button_index(BUTTON_XBUTTON2);
  1887. mb->set_doubleclick(true);
  1888. } break;
  1889. default: {
  1890. return 0;
  1891. }
  1892. }
  1893. mb->set_control((wParam & MK_CONTROL) != 0);
  1894. mb->set_shift((wParam & MK_SHIFT) != 0);
  1895. mb->set_alt(alt_mem);
  1896. //mb->get_alt()=(wParam&MK_MENU)!=0;
  1897. if (mb->is_pressed())
  1898. last_button_state |= (1 << (mb->get_button_index() - 1));
  1899. else
  1900. last_button_state &= ~(1 << (mb->get_button_index() - 1));
  1901. mb->set_button_mask(last_button_state);
  1902. mb->set_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)));
  1903. if (mouse_mode == MOUSE_MODE_CAPTURED && !use_raw_input) {
  1904. mb->set_position(Vector2(old_x, old_y));
  1905. }
  1906. if (uMsg != WM_MOUSEWHEEL && uMsg != WM_MOUSEHWHEEL) {
  1907. if (mb->is_pressed()) {
  1908. if (++pressrc > 0 && mouse_mode != MOUSE_MODE_CAPTURED)
  1909. SetCapture(hWnd);
  1910. } else {
  1911. if (--pressrc <= 0) {
  1912. if (mouse_mode != MOUSE_MODE_CAPTURED) {
  1913. ReleaseCapture();
  1914. }
  1915. pressrc = 0;
  1916. }
  1917. }
  1918. } else {
  1919. // for reasons unknown to mankind, wheel comes in screen coordinates
  1920. POINT coords;
  1921. coords.x = mb->get_position().x;
  1922. coords.y = mb->get_position().y;
  1923. ScreenToClient(hWnd, &coords);
  1924. mb->set_position(Vector2(coords.x, coords.y));
  1925. }
  1926. mb->set_global_position(mb->get_position());
  1927. Input::get_singleton()->accumulate_input_event(mb);
  1928. if (mb->is_pressed() && mb->get_button_index() > 3 && mb->get_button_index() < 8) {
  1929. //send release for mouse wheel
  1930. Ref<InputEventMouseButton> mbd = mb->duplicate();
  1931. mbd->set_window_id(window_id);
  1932. last_button_state &= ~(1 << (mbd->get_button_index() - 1));
  1933. mbd->set_button_mask(last_button_state);
  1934. mbd->set_pressed(false);
  1935. Input::get_singleton()->accumulate_input_event(mbd);
  1936. }
  1937. } break;
  1938. case WM_MOVE: {
  1939. if (!IsIconic(windows[window_id].hWnd)) {
  1940. int x = int16_t(LOWORD(lParam));
  1941. int y = int16_t(HIWORD(lParam));
  1942. windows[window_id].last_pos = Point2(x, y);
  1943. if (!windows[window_id].rect_changed_callback.is_null()) {
  1944. Variant size = Rect2i(windows[window_id].last_pos.x, windows[window_id].last_pos.y, windows[window_id].width, windows[window_id].height);
  1945. Variant *sizep = &size;
  1946. Variant ret;
  1947. Callable::CallError ce;
  1948. windows[window_id].rect_changed_callback.call((const Variant **)&sizep, 1, ret, ce);
  1949. }
  1950. }
  1951. } break;
  1952. case WM_SIZE: {
  1953. // Ignore size when a SIZE_MINIMIZED event is triggered
  1954. if (wParam != SIZE_MINIMIZED) {
  1955. int window_w = LOWORD(lParam);
  1956. int window_h = HIWORD(lParam);
  1957. if (window_w > 0 && window_h > 0 && !windows[window_id].preserve_window_size) {
  1958. windows[window_id].width = window_w;
  1959. windows[window_id].height = window_h;
  1960. #if defined(VULKAN_ENABLED)
  1961. if (rendering_driver == "vulkan") {
  1962. context_vulkan->window_resize(window_id, windows[window_id].width, windows[window_id].height);
  1963. }
  1964. #endif
  1965. } else {
  1966. windows[window_id].preserve_window_size = false;
  1967. window_set_size(Size2(windows[window_id].width, windows[window_id].height), window_id);
  1968. }
  1969. }
  1970. if (!windows[window_id].rect_changed_callback.is_null()) {
  1971. Variant size = Rect2i(windows[window_id].last_pos.x, windows[window_id].last_pos.y, windows[window_id].width, windows[window_id].height);
  1972. Variant *sizep = &size;
  1973. Variant ret;
  1974. Callable::CallError ce;
  1975. windows[window_id].rect_changed_callback.call((const Variant **)&sizep, 1, ret, ce);
  1976. }
  1977. if (wParam == SIZE_MAXIMIZED) {
  1978. windows[window_id].maximized = true;
  1979. windows[window_id].minimized = false;
  1980. } else if (wParam == SIZE_MINIMIZED) {
  1981. windows[window_id].maximized = false;
  1982. windows[window_id].minimized = true;
  1983. } else if (wParam == SIZE_RESTORED) {
  1984. windows[window_id].maximized = false;
  1985. windows[window_id].minimized = false;
  1986. }
  1987. #if 0
  1988. if (is_layered_allowed() && layered_window) {
  1989. DeleteObject(hBitmap);
  1990. RECT r;
  1991. GetWindowRect(hWnd, &r);
  1992. dib_size = Size2i(r.right - r.left, r.bottom - r.top);
  1993. BITMAPINFO bmi;
  1994. ZeroMemory(&bmi, sizeof(BITMAPINFO));
  1995. bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  1996. bmi.bmiHeader.biWidth = dib_size.x;
  1997. bmi.bmiHeader.biHeight = dib_size.y;
  1998. bmi.bmiHeader.biPlanes = 1;
  1999. bmi.bmiHeader.biBitCount = 32;
  2000. bmi.bmiHeader.biCompression = BI_RGB;
  2001. bmi.bmiHeader.biSizeImage = dib_size.x * dib_size.y * 4;
  2002. hBitmap = CreateDIBSection(hDC_dib, &bmi, DIB_RGB_COLORS, (void **)&dib_data, nullptr, 0x0);
  2003. SelectObject(hDC_dib, hBitmap);
  2004. ZeroMemory(dib_data, dib_size.x * dib_size.y * 4);
  2005. }
  2006. #endif
  2007. //return 0; // Jump Back
  2008. } break;
  2009. case WM_ENTERSIZEMOVE: {
  2010. Input::get_singleton()->release_pressed_events();
  2011. move_timer_id = SetTimer(windows[window_id].hWnd, 1, USER_TIMER_MINIMUM, (TIMERPROC) nullptr);
  2012. } break;
  2013. case WM_EXITSIZEMOVE: {
  2014. KillTimer(windows[window_id].hWnd, move_timer_id);
  2015. } break;
  2016. case WM_TIMER: {
  2017. if (wParam == move_timer_id) {
  2018. _process_key_events();
  2019. if (!Main::is_iterating()) {
  2020. Main::iteration();
  2021. }
  2022. }
  2023. } break;
  2024. case WM_SYSKEYDOWN:
  2025. case WM_SYSKEYUP:
  2026. case WM_KEYUP:
  2027. case WM_KEYDOWN: {
  2028. if (wParam == VK_SHIFT)
  2029. shift_mem = uMsg == WM_KEYDOWN;
  2030. if (wParam == VK_CONTROL)
  2031. control_mem = uMsg == WM_KEYDOWN;
  2032. if (wParam == VK_MENU) {
  2033. alt_mem = (uMsg == WM_KEYDOWN || uMsg == WM_SYSKEYDOWN);
  2034. if (lParam & (1 << 24))
  2035. gr_mem = alt_mem;
  2036. }
  2037. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  2038. // When SetCapture is used, ALT+F4 hotkey is ignored by Windows, so handle it ourselves
  2039. if (wParam == VK_F4 && alt_mem && (uMsg == WM_KEYDOWN || uMsg == WM_SYSKEYDOWN)) {
  2040. _send_window_event(windows[window_id], WINDOW_EVENT_CLOSE_REQUEST);
  2041. }
  2042. }
  2043. /*
  2044. if (wParam==VK_WIN) TODO wtf is this?
  2045. meta_mem=uMsg==WM_KEYDOWN;
  2046. */
  2047. [[fallthrough]];
  2048. }
  2049. case WM_CHAR: {
  2050. ERR_BREAK(key_event_pos >= KEY_EVENT_BUFFER_SIZE);
  2051. // Make sure we don't include modifiers for the modifier key itself.
  2052. KeyEvent ke;
  2053. ke.shift = (wParam != VK_SHIFT) ? shift_mem : false;
  2054. ke.alt = (!(wParam == VK_MENU && (uMsg == WM_KEYDOWN || uMsg == WM_SYSKEYDOWN))) ? alt_mem : false;
  2055. ke.control = (wParam != VK_CONTROL) ? control_mem : false;
  2056. ke.meta = meta_mem;
  2057. ke.uMsg = uMsg;
  2058. ke.window_id = window_id;
  2059. if (ke.uMsg == WM_SYSKEYDOWN)
  2060. ke.uMsg = WM_KEYDOWN;
  2061. if (ke.uMsg == WM_SYSKEYUP)
  2062. ke.uMsg = WM_KEYUP;
  2063. ke.wParam = wParam;
  2064. ke.lParam = lParam;
  2065. key_event_buffer[key_event_pos++] = ke;
  2066. } break;
  2067. case WM_INPUTLANGCHANGEREQUEST: {
  2068. // FIXME: Do something?
  2069. } break;
  2070. case WM_TOUCH: {
  2071. BOOL bHandled = FALSE;
  2072. UINT cInputs = LOWORD(wParam);
  2073. PTOUCHINPUT pInputs = memnew_arr(TOUCHINPUT, cInputs);
  2074. if (pInputs) {
  2075. if (GetTouchInputInfo((HTOUCHINPUT)lParam, cInputs, pInputs, sizeof(TOUCHINPUT))) {
  2076. for (UINT i = 0; i < cInputs; i++) {
  2077. TOUCHINPUT ti = pInputs[i];
  2078. POINT touch_pos = {
  2079. TOUCH_COORD_TO_PIXEL(ti.x),
  2080. TOUCH_COORD_TO_PIXEL(ti.y),
  2081. };
  2082. ScreenToClient(hWnd, &touch_pos);
  2083. //do something with each touch input entry
  2084. if (ti.dwFlags & TOUCHEVENTF_MOVE) {
  2085. _drag_event(window_id, touch_pos.x, touch_pos.y, ti.dwID);
  2086. } else if (ti.dwFlags & (TOUCHEVENTF_UP | TOUCHEVENTF_DOWN)) {
  2087. _touch_event(window_id, ti.dwFlags & TOUCHEVENTF_DOWN, touch_pos.x, touch_pos.y, ti.dwID);
  2088. };
  2089. }
  2090. bHandled = TRUE;
  2091. } else {
  2092. /* handle the error here */
  2093. }
  2094. memdelete_arr(pInputs);
  2095. } else {
  2096. /* handle the error here, probably out of memory */
  2097. }
  2098. if (bHandled) {
  2099. CloseTouchInputHandle((HTOUCHINPUT)lParam);
  2100. return 0;
  2101. };
  2102. } break;
  2103. case WM_DEVICECHANGE: {
  2104. joypad->probe_joypads();
  2105. } break;
  2106. case WM_SETCURSOR: {
  2107. if (LOWORD(lParam) == HTCLIENT) {
  2108. if (windows[window_id].window_has_focus && (mouse_mode == MOUSE_MODE_HIDDEN || mouse_mode == MOUSE_MODE_CAPTURED)) {
  2109. //Hide the cursor
  2110. if (hCursor == nullptr)
  2111. hCursor = SetCursor(nullptr);
  2112. else
  2113. SetCursor(nullptr);
  2114. } else {
  2115. if (hCursor != nullptr) {
  2116. CursorShape c = cursor_shape;
  2117. cursor_shape = CURSOR_MAX;
  2118. cursor_set_shape(c);
  2119. hCursor = nullptr;
  2120. }
  2121. }
  2122. }
  2123. } break;
  2124. case WM_DROPFILES: {
  2125. HDROP hDropInfo = (HDROP)wParam;
  2126. const int buffsize = 4096;
  2127. wchar_t buf[buffsize];
  2128. int fcount = DragQueryFileW(hDropInfo, 0xFFFFFFFF, nullptr, 0);
  2129. Vector<String> files;
  2130. for (int i = 0; i < fcount; i++) {
  2131. DragQueryFileW(hDropInfo, i, buf, buffsize);
  2132. String file = buf;
  2133. files.push_back(file);
  2134. }
  2135. if (files.size() && !windows[window_id].drop_files_callback.is_null()) {
  2136. Variant v = files;
  2137. Variant *vp = &v;
  2138. Variant ret;
  2139. Callable::CallError ce;
  2140. windows[window_id].drop_files_callback.call((const Variant **)&vp, 1, ret, ce);
  2141. }
  2142. } break;
  2143. default: {
  2144. if (user_proc) {
  2145. return CallWindowProcW(user_proc, hWnd, uMsg, wParam, lParam);
  2146. };
  2147. };
  2148. }
  2149. return DefWindowProcW(hWnd, uMsg, wParam, lParam);
  2150. }
  2151. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  2152. DisplayServerWindows *ds_win = static_cast<DisplayServerWindows *>(DisplayServer::get_singleton());
  2153. if (ds_win)
  2154. return ds_win->WndProc(hWnd, uMsg, wParam, lParam);
  2155. else
  2156. return DefWindowProcW(hWnd, uMsg, wParam, lParam);
  2157. }
  2158. void DisplayServerWindows::_process_key_events() {
  2159. for (int i = 0; i < key_event_pos; i++) {
  2160. KeyEvent &ke = key_event_buffer[i];
  2161. switch (ke.uMsg) {
  2162. case WM_CHAR: {
  2163. if ((i == 0 && ke.uMsg == WM_CHAR) || (i > 0 && key_event_buffer[i - 1].uMsg == WM_CHAR)) {
  2164. Ref<InputEventKey> k;
  2165. k.instance();
  2166. k->set_window_id(ke.window_id);
  2167. k->set_shift(ke.shift);
  2168. k->set_alt(ke.alt);
  2169. k->set_control(ke.control);
  2170. k->set_metakey(ke.meta);
  2171. k->set_pressed(true);
  2172. k->set_keycode(KeyMappingWindows::get_keysym(ke.wParam));
  2173. k->set_physical_keycode(KeyMappingWindows::get_scansym((ke.lParam >> 16) & 0xFF, ke.lParam & (1 << 24)));
  2174. k->set_unicode(ke.wParam);
  2175. if (k->get_unicode() && gr_mem) {
  2176. k->set_alt(false);
  2177. k->set_control(false);
  2178. }
  2179. if (k->get_unicode() < 32)
  2180. k->set_unicode(0);
  2181. Input::get_singleton()->accumulate_input_event(k);
  2182. }
  2183. //do nothing
  2184. } break;
  2185. case WM_KEYUP:
  2186. case WM_KEYDOWN: {
  2187. Ref<InputEventKey> k;
  2188. k.instance();
  2189. k->set_window_id(ke.window_id);
  2190. k->set_shift(ke.shift);
  2191. k->set_alt(ke.alt);
  2192. k->set_control(ke.control);
  2193. k->set_metakey(ke.meta);
  2194. k->set_pressed(ke.uMsg == WM_KEYDOWN);
  2195. if ((ke.lParam & (1 << 24)) && (ke.wParam == VK_RETURN)) {
  2196. // Special case for Numpad Enter key
  2197. k->set_keycode(KEY_KP_ENTER);
  2198. } else {
  2199. k->set_keycode(KeyMappingWindows::get_keysym(ke.wParam));
  2200. }
  2201. k->set_physical_keycode(KeyMappingWindows::get_scansym((ke.lParam >> 16) & 0xFF, ke.lParam & (1 << 24)));
  2202. if (i + 1 < key_event_pos && key_event_buffer[i + 1].uMsg == WM_CHAR) {
  2203. k->set_unicode(key_event_buffer[i + 1].wParam);
  2204. }
  2205. if (k->get_unicode() && gr_mem) {
  2206. k->set_alt(false);
  2207. k->set_control(false);
  2208. }
  2209. if (k->get_unicode() < 32)
  2210. k->set_unicode(0);
  2211. k->set_echo((ke.uMsg == WM_KEYDOWN && (ke.lParam & (1 << 30))));
  2212. Input::get_singleton()->accumulate_input_event(k);
  2213. } break;
  2214. }
  2215. }
  2216. key_event_pos = 0;
  2217. }
  2218. DisplayServer::WindowID DisplayServerWindows::_create_window(WindowMode p_mode, uint32_t p_flags, const Rect2i &p_rect) {
  2219. DWORD dwExStyle;
  2220. DWORD dwStyle;
  2221. _get_window_style(window_id_counter == MAIN_WINDOW_ID, p_mode == WINDOW_MODE_FULLSCREEN, p_flags & WINDOW_FLAG_BORDERLESS_BIT, !(p_flags & WINDOW_FLAG_RESIZE_DISABLED_BIT), p_mode == WINDOW_MODE_MAXIMIZED, (p_flags & WINDOW_FLAG_NO_FOCUS_BIT), dwStyle, dwExStyle);
  2222. RECT WindowRect;
  2223. WindowRect.left = p_rect.position.x;
  2224. WindowRect.right = p_rect.position.x + p_rect.size.x;
  2225. WindowRect.top = p_rect.position.y;
  2226. WindowRect.bottom = p_rect.position.y + p_rect.size.y;
  2227. AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);
  2228. WindowID id = window_id_counter;
  2229. {
  2230. WindowData wd;
  2231. wd.hWnd = CreateWindowExW(
  2232. dwExStyle,
  2233. L"Engine", L"",
  2234. dwStyle,
  2235. // (GetSystemMetrics(SM_CXSCREEN) - WindowRect.right) / 2,
  2236. // (GetSystemMetrics(SM_CYSCREEN) - WindowRect.bottom) / 2,
  2237. WindowRect.left,
  2238. WindowRect.top,
  2239. WindowRect.right - WindowRect.left,
  2240. WindowRect.bottom - WindowRect.top,
  2241. nullptr, nullptr, hInstance, nullptr);
  2242. if (!wd.hWnd) {
  2243. MessageBoxW(nullptr, L"Window Creation Error.", L"ERROR", MB_OK | MB_ICONEXCLAMATION);
  2244. return INVALID_WINDOW_ID;
  2245. }
  2246. #ifdef VULKAN_ENABLED
  2247. if (rendering_driver == "vulkan") {
  2248. if (context_vulkan->window_create(id, wd.hWnd, hInstance, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top) == -1) {
  2249. memdelete(context_vulkan);
  2250. context_vulkan = nullptr;
  2251. ERR_FAIL_V(INVALID_WINDOW_ID);
  2252. }
  2253. }
  2254. #endif
  2255. RegisterTouchWindow(wd.hWnd, 0);
  2256. TRACKMOUSEEVENT tme;
  2257. tme.cbSize = sizeof(TRACKMOUSEEVENT);
  2258. tme.dwFlags = TME_LEAVE;
  2259. tme.hwndTrack = wd.hWnd;
  2260. tme.dwHoverTime = HOVER_DEFAULT;
  2261. TrackMouseEvent(&tme);
  2262. DragAcceptFiles(wd.hWnd, true);
  2263. if (!OS::get_singleton()->is_wintab_disabled() && wintab_available) {
  2264. wintab_WTInfo(WTI_DEFSYSCTX, 0, &wd.wtlc);
  2265. wd.wtlc.lcOptions |= CXO_MESSAGES;
  2266. wd.wtlc.lcPktData = PK_NORMAL_PRESSURE | PK_TANGENT_PRESSURE | PK_ORIENTATION;
  2267. wd.wtlc.lcMoveMask = PK_NORMAL_PRESSURE | PK_TANGENT_PRESSURE;
  2268. wd.wtlc.lcPktMode = 0;
  2269. wd.wtlc.lcOutOrgX = 0;
  2270. wd.wtlc.lcOutExtX = wd.wtlc.lcInExtX;
  2271. wd.wtlc.lcOutOrgY = 0;
  2272. wd.wtlc.lcOutExtY = -wd.wtlc.lcInExtY;
  2273. wd.wtctx = wintab_WTOpen(wd.hWnd, &wd.wtlc, false);
  2274. if (wd.wtctx) {
  2275. wintab_WTEnable(wd.wtctx, true);
  2276. AXIS pressure;
  2277. if (wintab_WTInfo(WTI_DEVICES + wd.wtlc.lcDevice, DVC_NPRESSURE, &pressure)) {
  2278. wd.min_pressure = int(pressure.axMin);
  2279. wd.max_pressure = int(pressure.axMax);
  2280. }
  2281. AXIS orientation[3];
  2282. if (wintab_WTInfo(WTI_DEVICES + wd.wtlc.lcDevice, DVC_ORIENTATION, &orientation)) {
  2283. wd.tilt_supported = orientation[0].axResolution && orientation[1].axResolution;
  2284. }
  2285. } else {
  2286. print_verbose("WinTab context creation failed.");
  2287. }
  2288. } else {
  2289. wd.wtctx = 0;
  2290. }
  2291. wd.last_pressure = 0;
  2292. wd.last_pressure_update = 0;
  2293. wd.last_tilt = Vector2();
  2294. // IME
  2295. wd.im_himc = ImmGetContext(wd.hWnd);
  2296. ImmReleaseContext(wd.hWnd, wd.im_himc);
  2297. wd.im_position = Vector2();
  2298. wd.last_pos = p_rect.position;
  2299. wd.width = p_rect.size.width;
  2300. wd.height = p_rect.size.height;
  2301. windows[id] = wd;
  2302. window_id_counter++;
  2303. }
  2304. return id;
  2305. }
  2306. // WinTab API
  2307. bool DisplayServerWindows::wintab_available = false;
  2308. WTOpenPtr DisplayServerWindows::wintab_WTOpen = nullptr;
  2309. WTClosePtr DisplayServerWindows::wintab_WTClose = nullptr;
  2310. WTInfoPtr DisplayServerWindows::wintab_WTInfo = nullptr;
  2311. WTPacketPtr DisplayServerWindows::wintab_WTPacket = nullptr;
  2312. WTEnablePtr DisplayServerWindows::wintab_WTEnable = nullptr;
  2313. // Windows Ink API
  2314. GetPointerTypePtr DisplayServerWindows::win8p_GetPointerType = nullptr;
  2315. GetPointerPenInfoPtr DisplayServerWindows::win8p_GetPointerPenInfo = nullptr;
  2316. typedef enum _SHC_PROCESS_DPI_AWARENESS {
  2317. SHC_PROCESS_DPI_UNAWARE = 0,
  2318. SHC_PROCESS_SYSTEM_DPI_AWARE = 1,
  2319. SHC_PROCESS_PER_MONITOR_DPI_AWARE = 2
  2320. } SHC_PROCESS_DPI_AWARENESS;
  2321. DisplayServerWindows::DisplayServerWindows(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) {
  2322. //Note: Wacom WinTab driver API for pen input, for devices incompatible with Windows Ink.
  2323. HMODULE wintab_lib = LoadLibraryW(L"wintab32.dll");
  2324. if (wintab_lib) {
  2325. wintab_WTOpen = (WTOpenPtr)GetProcAddress(wintab_lib, "WTOpenW");
  2326. wintab_WTClose = (WTClosePtr)GetProcAddress(wintab_lib, "WTClose");
  2327. wintab_WTInfo = (WTInfoPtr)GetProcAddress(wintab_lib, "WTInfoW");
  2328. wintab_WTPacket = (WTPacketPtr)GetProcAddress(wintab_lib, "WTPacket");
  2329. wintab_WTEnable = (WTEnablePtr)GetProcAddress(wintab_lib, "WTEnable");
  2330. wintab_available = wintab_WTOpen && wintab_WTClose && wintab_WTInfo && wintab_WTPacket && wintab_WTEnable;
  2331. }
  2332. //Note: Windows Ink API for pen input, available on Windows 8+ only.
  2333. HMODULE user32_lib = LoadLibraryW(L"user32.dll");
  2334. if (user32_lib) {
  2335. win8p_GetPointerType = (GetPointerTypePtr)GetProcAddress(user32_lib, "GetPointerType");
  2336. win8p_GetPointerPenInfo = (GetPointerPenInfoPtr)GetProcAddress(user32_lib, "GetPointerPenInfo");
  2337. }
  2338. drop_events = false;
  2339. key_event_pos = 0;
  2340. alt_mem = false;
  2341. gr_mem = false;
  2342. shift_mem = false;
  2343. control_mem = false;
  2344. meta_mem = false;
  2345. console_visible = IsWindowVisible(GetConsoleWindow());
  2346. hInstance = ((OS_Windows *)OS::get_singleton())->get_hinstance();
  2347. pressrc = 0;
  2348. old_invalid = true;
  2349. mouse_mode = MOUSE_MODE_VISIBLE;
  2350. outside = true;
  2351. if (OS::get_singleton()->is_hidpi_allowed()) {
  2352. HMODULE Shcore = LoadLibraryW(L"Shcore.dll");
  2353. if (Shcore != nullptr) {
  2354. typedef HRESULT(WINAPI * SetProcessDpiAwareness_t)(SHC_PROCESS_DPI_AWARENESS);
  2355. SetProcessDpiAwareness_t SetProcessDpiAwareness = (SetProcessDpiAwareness_t)GetProcAddress(Shcore, "SetProcessDpiAwareness");
  2356. if (SetProcessDpiAwareness) {
  2357. SetProcessDpiAwareness(SHC_PROCESS_SYSTEM_DPI_AWARE);
  2358. }
  2359. }
  2360. }
  2361. memset(&wc, 0, sizeof(WNDCLASSEXW));
  2362. wc.cbSize = sizeof(WNDCLASSEXW);
  2363. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
  2364. wc.lpfnWndProc = (WNDPROC)::WndProc;
  2365. wc.cbClsExtra = 0;
  2366. wc.cbWndExtra = 0;
  2367. //wc.hInstance = hInstance;
  2368. wc.hInstance = hInstance ? hInstance : GetModuleHandle(nullptr);
  2369. wc.hIcon = LoadIcon(nullptr, IDI_WINLOGO);
  2370. wc.hCursor = nullptr; //LoadCursor(nullptr, IDC_ARROW);
  2371. wc.hbrBackground = nullptr;
  2372. wc.lpszMenuName = nullptr;
  2373. wc.lpszClassName = L"Engine";
  2374. if (!RegisterClassExW(&wc)) {
  2375. MessageBox(nullptr, "Failed To Register The Window Class.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  2376. r_error = ERR_UNAVAILABLE;
  2377. return;
  2378. }
  2379. use_raw_input = true;
  2380. RAWINPUTDEVICE Rid[1];
  2381. Rid[0].usUsagePage = 0x01;
  2382. Rid[0].usUsage = 0x02;
  2383. Rid[0].dwFlags = 0;
  2384. Rid[0].hwndTarget = 0;
  2385. if (RegisterRawInputDevices(Rid, 1, sizeof(Rid[0])) == FALSE) {
  2386. //registration failed.
  2387. use_raw_input = false;
  2388. }
  2389. rendering_driver = "vulkan";
  2390. #if defined(VULKAN_ENABLED)
  2391. if (rendering_driver == "vulkan") {
  2392. context_vulkan = memnew(VulkanContextWindows);
  2393. if (context_vulkan->initialize() != OK) {
  2394. memdelete(context_vulkan);
  2395. context_vulkan = nullptr;
  2396. r_error = ERR_UNAVAILABLE;
  2397. return;
  2398. }
  2399. }
  2400. #endif
  2401. #if defined(OPENGL_ENABLED)
  2402. if (rendering_driver_index == VIDEO_DRIVER_GLES2) {
  2403. context_gles2 = memnew(ContextGL_Windows(hWnd, false));
  2404. if (context_gles2->initialize() != OK) {
  2405. memdelete(context_gles2);
  2406. context_gles2 = nullptr;
  2407. ERR_FAIL_V(ERR_UNAVAILABLE);
  2408. }
  2409. context_gles2->set_use_vsync(video_mode.use_vsync);
  2410. set_vsync_via_compositor(video_mode.vsync_via_compositor);
  2411. if (RasterizerGLES2::is_viable() == OK) {
  2412. RasterizerGLES2::register_config();
  2413. RasterizerGLES2::make_current();
  2414. } else {
  2415. memdelete(context_gles2);
  2416. context_gles2 = nullptr;
  2417. ERR_FAIL_V(ERR_UNAVAILABLE);
  2418. }
  2419. }
  2420. #endif
  2421. Point2i window_position(
  2422. (screen_get_size(0).width - p_resolution.width) / 2,
  2423. (screen_get_size(0).height - p_resolution.height) / 2);
  2424. WindowID main_window = _create_window(p_mode, 0, Rect2i(window_position, p_resolution));
  2425. for (int i = 0; i < WINDOW_FLAG_MAX; i++) {
  2426. if (p_flags & (1 << i)) {
  2427. window_set_flag(WindowFlags(i), true, main_window);
  2428. }
  2429. }
  2430. ShowWindow(windows[MAIN_WINDOW_ID].hWnd, SW_SHOW); // Show The Window
  2431. SetForegroundWindow(windows[MAIN_WINDOW_ID].hWnd); // Slightly Higher Priority
  2432. SetFocus(windows[MAIN_WINDOW_ID].hWnd); // Sets Keyboard Focus To
  2433. #if defined(VULKAN_ENABLED)
  2434. if (rendering_driver == "vulkan") {
  2435. rendering_device_vulkan = memnew(RenderingDeviceVulkan);
  2436. rendering_device_vulkan->initialize(context_vulkan);
  2437. RasterizerRD::make_current();
  2438. }
  2439. #endif
  2440. move_timer_id = 1;
  2441. //set_ime_active(false);
  2442. if (!OS::get_singleton()->is_in_low_processor_usage_mode()) {
  2443. //SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
  2444. SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
  2445. DWORD index = 0;
  2446. HANDLE handle = AvSetMmThreadCharacteristics("Games", &index);
  2447. if (handle)
  2448. AvSetMmThreadPriority(handle, AVRT_PRIORITY_CRITICAL);
  2449. // This is needed to make sure that background work does not starve the main thread.
  2450. // This is only setting priority of this thread, not the whole process.
  2451. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
  2452. }
  2453. cursor_shape = CURSOR_ARROW;
  2454. _update_real_mouse_position(MAIN_WINDOW_ID);
  2455. joypad = new JoypadWindows(&windows[MAIN_WINDOW_ID].hWnd);
  2456. r_error = OK;
  2457. ((OS_Windows *)OS::get_singleton())->set_main_window(windows[MAIN_WINDOW_ID].hWnd);
  2458. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events);
  2459. }
  2460. Vector<String> DisplayServerWindows::get_rendering_drivers_func() {
  2461. Vector<String> drivers;
  2462. #ifdef VULKAN_ENABLED
  2463. drivers.push_back("vulkan");
  2464. #endif
  2465. #ifdef OPENGL_ENABLED
  2466. drivers.push_back("opengl");
  2467. #endif
  2468. return drivers;
  2469. }
  2470. DisplayServer *DisplayServerWindows::create_func(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) {
  2471. return memnew(DisplayServerWindows(p_rendering_driver, p_mode, p_flags, p_resolution, r_error));
  2472. }
  2473. void DisplayServerWindows::register_windows_driver() {
  2474. register_create_function("windows", create_func, get_rendering_drivers_func);
  2475. }
  2476. DisplayServerWindows::~DisplayServerWindows() {
  2477. delete joypad;
  2478. touch_state.clear();
  2479. cursors_cache.clear();
  2480. #if defined(VULKAN_ENABLED)
  2481. if (rendering_driver == "vulkan") {
  2482. if (rendering_device_vulkan) {
  2483. rendering_device_vulkan->finalize();
  2484. memdelete(rendering_device_vulkan);
  2485. }
  2486. if (context_vulkan)
  2487. memdelete(context_vulkan);
  2488. }
  2489. #endif
  2490. if (user_proc) {
  2491. SetWindowLongPtr(windows[MAIN_WINDOW_ID].hWnd, GWLP_WNDPROC, (LONG_PTR)user_proc);
  2492. };
  2493. if (windows.has(MAIN_WINDOW_ID)) {
  2494. #ifdef VULKAN_ENABLED
  2495. if (rendering_driver == "vulkan") {
  2496. context_vulkan->window_destroy(MAIN_WINDOW_ID);
  2497. }
  2498. #endif
  2499. if (wintab_available && windows[MAIN_WINDOW_ID].wtctx) {
  2500. wintab_WTClose(windows[MAIN_WINDOW_ID].wtctx);
  2501. windows[MAIN_WINDOW_ID].wtctx = 0;
  2502. }
  2503. DestroyWindow(windows[MAIN_WINDOW_ID].hWnd);
  2504. }
  2505. }