display_server_windows.cpp 92 KB

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