display_server_windows.cpp 95 KB

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