os_winrt.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. /*************************************************************************/
  2. /* os_winrt.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 "drivers/gles2/rasterizer_gles2.h"
  31. #include "drivers/unix/memory_pool_static_malloc.h"
  32. #include "drivers/windows/dir_access_windows.h"
  33. #include "drivers/windows/file_access_windows.h"
  34. #include "drivers/windows/mutex_windows.h"
  35. #include "drivers/windows/semaphore_windows.h"
  36. #include "main/main.h"
  37. #include "os/memory_pool_dynamic_static.h"
  38. #include "os_winrt.h"
  39. #include "thread_winrt.h"
  40. #include "servers/audio/audio_server_sw.h"
  41. #include "servers/visual/visual_server_raster.h"
  42. #include "servers/visual/visual_server_wrap_mt.h"
  43. #include "globals.h"
  44. #include "io/marshalls.h"
  45. #include "os/memory_pool_dynamic_prealloc.h"
  46. #include "drivers/unix/ip_unix.h"
  47. #include "platform/windows/packet_peer_udp_winsock.h"
  48. #include "platform/windows/stream_peer_winsock.h"
  49. #include "platform/windows/tcp_server_winsock.h"
  50. #include <ppltasks.h>
  51. #include <wrl.h>
  52. using namespace Windows::ApplicationModel::Core;
  53. using namespace Windows::ApplicationModel::Activation;
  54. using namespace Windows::UI::Core;
  55. using namespace Windows::UI::Input;
  56. using namespace Windows::UI::Popups;
  57. using namespace Windows::Foundation;
  58. using namespace Windows::Graphics::Display;
  59. using namespace Microsoft::WRL;
  60. using namespace Windows::UI::ViewManagement;
  61. using namespace Windows::Devices::Input;
  62. using namespace Windows::Devices::Sensors;
  63. using namespace Windows::ApplicationModel::DataTransfer;
  64. using namespace concurrency;
  65. int OSWinrt::get_video_driver_count() const {
  66. return 1;
  67. }
  68. const char *OSWinrt::get_video_driver_name(int p_driver) const {
  69. return "GLES2";
  70. }
  71. OS::VideoMode OSWinrt::get_default_video_mode() const {
  72. return video_mode;
  73. }
  74. Size2 OSWinrt::get_window_size() const {
  75. Size2 size;
  76. size.width = video_mode.width;
  77. size.height = video_mode.height;
  78. return size;
  79. }
  80. void OSWinrt::set_window_size(const Size2 p_size) {
  81. Windows::Foundation::Size new_size;
  82. new_size.Width = p_size.width;
  83. new_size.Height = p_size.height;
  84. ApplicationView ^ view = ApplicationView::GetForCurrentView();
  85. if (view->TryResizeView(new_size)) {
  86. video_mode.width = p_size.width;
  87. video_mode.height = p_size.height;
  88. }
  89. }
  90. void OSWinrt::set_window_fullscreen(bool p_enabled) {
  91. ApplicationView ^ view = ApplicationView::GetForCurrentView();
  92. video_mode.fullscreen = view->IsFullScreenMode;
  93. if (video_mode.fullscreen == p_enabled)
  94. return;
  95. if (p_enabled) {
  96. video_mode.fullscreen = view->TryEnterFullScreenMode();
  97. } else {
  98. view->ExitFullScreenMode();
  99. video_mode.fullscreen = false;
  100. }
  101. }
  102. bool OSWinrt::is_window_fullscreen() const {
  103. return ApplicationView::GetForCurrentView()->IsFullScreenMode;
  104. }
  105. void OSWinrt::set_keep_screen_on(bool p_enabled) {
  106. if (is_keep_screen_on() == p_enabled) return;
  107. if (p_enabled)
  108. display_request->RequestActive();
  109. else
  110. display_request->RequestRelease();
  111. OS::set_keep_screen_on(p_enabled);
  112. }
  113. int OSWinrt::get_audio_driver_count() const {
  114. return AudioDriverManagerSW::get_driver_count();
  115. }
  116. const char *OSWinrt::get_audio_driver_name(int p_driver) const {
  117. AudioDriverSW *driver = AudioDriverManagerSW::get_driver(p_driver);
  118. ERR_FAIL_COND_V(!driver, "");
  119. return AudioDriverManagerSW::get_driver(p_driver)->get_name();
  120. }
  121. static MemoryPoolStatic *mempool_static = NULL;
  122. static MemoryPoolDynamic *mempool_dynamic = NULL;
  123. void OSWinrt::initialize_core() {
  124. last_button_state = 0;
  125. //RedirectIOToConsole();
  126. ThreadWinrt::make_default();
  127. SemaphoreWindows::make_default();
  128. MutexWindows::make_default();
  129. FileAccess::make_default<FileAccessWindows>(FileAccess::ACCESS_RESOURCES);
  130. FileAccess::make_default<FileAccessWindows>(FileAccess::ACCESS_USERDATA);
  131. FileAccess::make_default<FileAccessWindows>(FileAccess::ACCESS_FILESYSTEM);
  132. //FileAccessBufferedFA<FileAccessWindows>::make_default();
  133. DirAccess::make_default<DirAccessWindows>(DirAccess::ACCESS_RESOURCES);
  134. DirAccess::make_default<DirAccessWindows>(DirAccess::ACCESS_USERDATA);
  135. DirAccess::make_default<DirAccessWindows>(DirAccess::ACCESS_FILESYSTEM);
  136. //TCPServerWinsock::make_default();
  137. //StreamPeerWinsock::make_default();
  138. TCPServerWinsock::make_default();
  139. StreamPeerWinsock::make_default();
  140. PacketPeerUDPWinsock::make_default();
  141. mempool_static = new MemoryPoolStaticMalloc;
  142. #if 1
  143. mempool_dynamic = memnew(MemoryPoolDynamicStatic);
  144. #else
  145. #define DYNPOOL_SIZE 4 * 1024 * 1024
  146. void *buffer = malloc(DYNPOOL_SIZE);
  147. mempool_dynamic = memnew(MemoryPoolDynamicPrealloc(buffer, DYNPOOL_SIZE));
  148. #endif
  149. // We need to know how often the clock is updated
  150. if (!QueryPerformanceFrequency((LARGE_INTEGER *)&ticks_per_second))
  151. ticks_per_second = 1000;
  152. // If timeAtGameStart is 0 then we get the time since
  153. // the start of the computer when we call GetGameTime()
  154. ticks_start = 0;
  155. ticks_start = get_ticks_usec();
  156. IP_Unix::make_default();
  157. cursor_shape = CURSOR_ARROW;
  158. }
  159. bool OSWinrt::can_draw() const {
  160. return !minimized;
  161. };
  162. void OSWinrt::set_gl_context(ContextEGL *p_context) {
  163. gl_context = p_context;
  164. };
  165. void OSWinrt::screen_size_changed() {
  166. gl_context->reset();
  167. };
  168. void OSWinrt::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
  169. main_loop = NULL;
  170. outside = true;
  171. gl_context->initialize();
  172. VideoMode vm;
  173. vm.width = gl_context->get_window_width();
  174. vm.height = gl_context->get_window_height();
  175. vm.resizable = false;
  176. ApplicationView ^ view = ApplicationView::GetForCurrentView();
  177. vm.fullscreen = view->IsFullScreenMode;
  178. view->SetDesiredBoundsMode(ApplicationViewBoundsMode::UseVisible);
  179. view->PreferredLaunchWindowingMode = ApplicationViewWindowingMode::PreferredLaunchViewSize;
  180. if (p_desired.fullscreen != view->IsFullScreenMode) {
  181. if (p_desired.fullscreen) {
  182. vm.fullscreen = view->TryEnterFullScreenMode();
  183. } else {
  184. view->ExitFullScreenMode();
  185. vm.fullscreen = false;
  186. }
  187. }
  188. Windows::Foundation::Size desired;
  189. desired.Width = p_desired.width;
  190. desired.Height = p_desired.height;
  191. view->PreferredLaunchViewSize = desired;
  192. if (view->TryResizeView(desired)) {
  193. vm.width = view->VisibleBounds.Width;
  194. vm.height = view->VisibleBounds.Height;
  195. }
  196. set_video_mode(vm);
  197. gl_context->make_current();
  198. rasterizer = memnew(RasterizerGLES2);
  199. visual_server = memnew(VisualServerRaster(rasterizer));
  200. if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
  201. visual_server = memnew(VisualServerWrapMT(visual_server, get_render_thread_mode() == RENDER_SEPARATE_THREAD));
  202. }
  203. //
  204. physics_server = memnew(PhysicsServerSW);
  205. physics_server->init();
  206. physics_2d_server = memnew(Physics2DServerSW);
  207. physics_2d_server->init();
  208. visual_server->init();
  209. input = memnew(InputDefault);
  210. joystick = ref new JoystickWinrt(input);
  211. joystick->register_events();
  212. AudioDriverManagerSW::initialize(p_audio_driver);
  213. sample_manager = memnew(SampleManagerMallocSW);
  214. audio_server = memnew(AudioServerSW(sample_manager));
  215. audio_server->init();
  216. spatial_sound_server = memnew(SpatialSoundServerSW);
  217. spatial_sound_server->init();
  218. spatial_sound_2d_server = memnew(SpatialSound2DServerSW);
  219. spatial_sound_2d_server->init();
  220. managed_object->update_clipboard();
  221. Clipboard::ContentChanged += ref new EventHandler<Platform::Object ^>(managed_object, &ManagedType::on_clipboard_changed);
  222. accelerometer = Accelerometer::GetDefault();
  223. if (accelerometer != nullptr) {
  224. // 60 FPS
  225. accelerometer->ReportInterval = (1.0f / 60.0f) * 1000;
  226. accelerometer->ReadingChanged +=
  227. ref new TypedEventHandler<Accelerometer ^, AccelerometerReadingChangedEventArgs ^>(managed_object, &ManagedType::on_accelerometer_reading_changed);
  228. }
  229. magnetometer = Magnetometer::GetDefault();
  230. if (magnetometer != nullptr) {
  231. // 60 FPS
  232. magnetometer->ReportInterval = (1.0f / 60.0f) * 1000;
  233. magnetometer->ReadingChanged +=
  234. ref new TypedEventHandler<Magnetometer ^, MagnetometerReadingChangedEventArgs ^>(managed_object, &ManagedType::on_magnetometer_reading_changed);
  235. }
  236. gyrometer = Gyrometer::GetDefault();
  237. if (gyrometer != nullptr) {
  238. // 60 FPS
  239. gyrometer->ReportInterval = (1.0f / 60.0f) * 1000;
  240. gyrometer->ReadingChanged +=
  241. ref new TypedEventHandler<Gyrometer ^, GyrometerReadingChangedEventArgs ^>(managed_object, &ManagedType::on_gyroscope_reading_changed);
  242. }
  243. _ensure_data_dir();
  244. if (is_keep_screen_on())
  245. display_request->RequestActive();
  246. set_keep_screen_on(GLOBAL_DEF("display/keep_screen_on", true));
  247. }
  248. void OSWinrt::set_clipboard(const String &p_text) {
  249. DataPackage ^ clip = ref new DataPackage();
  250. clip->RequestedOperation = DataPackageOperation::Copy;
  251. clip->SetText(ref new Platform::String((const wchar_t *)p_text.c_str()));
  252. Clipboard::SetContent(clip);
  253. };
  254. String OSWinrt::get_clipboard() const {
  255. if (managed_object->clipboard != nullptr)
  256. return managed_object->clipboard->Data();
  257. else
  258. return "";
  259. };
  260. void OSWinrt::input_event(InputEvent &p_event) {
  261. p_event.ID = ++last_id;
  262. input->parse_input_event(p_event);
  263. if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed && p_event.mouse_button.button_index > 3) {
  264. //send release for mouse wheel
  265. p_event.mouse_button.pressed = false;
  266. p_event.ID = ++last_id;
  267. input->parse_input_event(p_event);
  268. }
  269. };
  270. void OSWinrt::delete_main_loop() {
  271. if (main_loop)
  272. memdelete(main_loop);
  273. main_loop = NULL;
  274. }
  275. void OSWinrt::set_main_loop(MainLoop *p_main_loop) {
  276. input->set_main_loop(p_main_loop);
  277. main_loop = p_main_loop;
  278. }
  279. void OSWinrt::finalize() {
  280. if (main_loop)
  281. memdelete(main_loop);
  282. main_loop = NULL;
  283. visual_server->finish();
  284. memdelete(visual_server);
  285. #ifdef OPENGL_ENABLED
  286. if (gl_context)
  287. memdelete(gl_context);
  288. #endif
  289. if (rasterizer)
  290. memdelete(rasterizer);
  291. spatial_sound_server->finish();
  292. memdelete(spatial_sound_server);
  293. spatial_sound_2d_server->finish();
  294. memdelete(spatial_sound_2d_server);
  295. //if (debugger_connection_console) {
  296. // memdelete(debugger_connection_console);
  297. //}
  298. memdelete(sample_manager);
  299. audio_server->finish();
  300. memdelete(audio_server);
  301. memdelete(input);
  302. physics_server->finish();
  303. memdelete(physics_server);
  304. physics_2d_server->finish();
  305. memdelete(physics_2d_server);
  306. joystick = nullptr;
  307. }
  308. void OSWinrt::finalize_core() {
  309. if (mempool_dynamic)
  310. memdelete(mempool_dynamic);
  311. delete mempool_static;
  312. }
  313. void OSWinrt::vprint(const char *p_format, va_list p_list, bool p_stderr) {
  314. char buf[16384 + 1];
  315. int len = vsnprintf(buf, 16384, p_format, p_list);
  316. if (len <= 0)
  317. return;
  318. buf[len] = 0;
  319. int wlen = MultiByteToWideChar(CP_UTF8, 0, buf, len, NULL, 0);
  320. if (wlen < 0)
  321. return;
  322. wchar_t *wbuf = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
  323. MultiByteToWideChar(CP_UTF8, 0, buf, len, wbuf, wlen);
  324. wbuf[wlen] = 0;
  325. if (p_stderr)
  326. fwprintf(stderr, L"%s", wbuf);
  327. else
  328. wprintf(L"%s", wbuf);
  329. free(wbuf);
  330. fflush(stdout);
  331. };
  332. void OSWinrt::alert(const String &p_alert, const String &p_title) {
  333. Platform::String ^ alert = ref new Platform::String(p_alert.c_str());
  334. Platform::String ^ title = ref new Platform::String(p_title.c_str());
  335. MessageDialog ^ msg = ref new MessageDialog(alert, title);
  336. UICommand ^ close = ref new UICommand("Close", ref new UICommandInvokedHandler(managed_object, &OSWinrt::ManagedType::alert_close));
  337. msg->Commands->Append(close);
  338. msg->DefaultCommandIndex = 0;
  339. managed_object->alert_close_handle = true;
  340. msg->ShowAsync();
  341. }
  342. void OSWinrt::ManagedType::alert_close(IUICommand ^ command) {
  343. alert_close_handle = false;
  344. }
  345. void OSWinrt::ManagedType::on_clipboard_changed(Platform::Object ^ sender, Platform::Object ^ ev) {
  346. update_clipboard();
  347. }
  348. void OSWinrt::ManagedType::update_clipboard() {
  349. DataPackageView ^ data = Clipboard::GetContent();
  350. if (data->Contains(StandardDataFormats::Text)) {
  351. create_task(data->GetTextAsync()).then([this](Platform::String ^ clipboard_content) {
  352. this->clipboard = clipboard_content;
  353. });
  354. }
  355. }
  356. void OSWinrt::ManagedType::on_accelerometer_reading_changed(Accelerometer ^ sender, AccelerometerReadingChangedEventArgs ^ args) {
  357. AccelerometerReading ^ reading = args->Reading;
  358. os->input->set_accelerometer(Vector3(
  359. reading->AccelerationX,
  360. reading->AccelerationY,
  361. reading->AccelerationZ));
  362. }
  363. void OSWinrt::ManagedType::on_magnetometer_reading_changed(Magnetometer ^ sender, MagnetometerReadingChangedEventArgs ^ args) {
  364. MagnetometerReading ^ reading = args->Reading;
  365. os->input->set_magnetometer(Vector3(
  366. reading->MagneticFieldX,
  367. reading->MagneticFieldY,
  368. reading->MagneticFieldZ));
  369. }
  370. void OSWinrt::ManagedType::on_gyroscope_reading_changed(Gyrometer ^ sender, GyrometerReadingChangedEventArgs ^ args) {
  371. GyrometerReading ^ reading = args->Reading;
  372. os->input->set_magnetometer(Vector3(
  373. reading->AngularVelocityX,
  374. reading->AngularVelocityY,
  375. reading->AngularVelocityZ));
  376. }
  377. void OSWinrt::set_mouse_mode(MouseMode p_mode) {
  378. if (p_mode == MouseMode::MOUSE_MODE_CAPTURED) {
  379. CoreWindow::GetForCurrentThread()->SetPointerCapture();
  380. } else {
  381. CoreWindow::GetForCurrentThread()->ReleasePointerCapture();
  382. }
  383. if (p_mode == MouseMode::MOUSE_MODE_CAPTURED || p_mode == MouseMode::MOUSE_MODE_HIDDEN) {
  384. CoreWindow::GetForCurrentThread()->PointerCursor = nullptr;
  385. } else {
  386. CoreWindow::GetForCurrentThread()->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0);
  387. }
  388. mouse_mode = p_mode;
  389. SetEvent(mouse_mode_changed);
  390. }
  391. OSWinrt::MouseMode OSWinrt::get_mouse_mode() const {
  392. return mouse_mode;
  393. }
  394. Point2 OSWinrt::get_mouse_pos() const {
  395. return Point2(old_x, old_y);
  396. }
  397. int OSWinrt::get_mouse_button_state() const {
  398. return last_button_state;
  399. }
  400. void OSWinrt::set_window_title(const String &p_title) {
  401. }
  402. void OSWinrt::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
  403. video_mode = p_video_mode;
  404. }
  405. OS::VideoMode OSWinrt::get_video_mode(int p_screen) const {
  406. return video_mode;
  407. }
  408. void OSWinrt::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
  409. }
  410. void OSWinrt::print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) {
  411. const char *err_details;
  412. if (p_rationale && p_rationale[0])
  413. err_details = p_rationale;
  414. else
  415. err_details = p_code;
  416. switch (p_type) {
  417. case ERR_ERROR:
  418. print("ERROR: %s: %s\n", p_function, err_details);
  419. print(" At: %s:%i\n", p_file, p_line);
  420. break;
  421. case ERR_WARNING:
  422. print("WARNING: %s: %s\n", p_function, err_details);
  423. print(" At: %s:%i\n", p_file, p_line);
  424. break;
  425. case ERR_SCRIPT:
  426. print("SCRIPT ERROR: %s: %s\n", p_function, err_details);
  427. print(" At: %s:%i\n", p_file, p_line);
  428. break;
  429. }
  430. }
  431. String OSWinrt::get_name() {
  432. return "WinRT";
  433. }
  434. OS::Date OSWinrt::get_date(bool utc) const {
  435. SYSTEMTIME systemtime;
  436. if (utc)
  437. GetSystemTime(&systemtime);
  438. else
  439. GetLocalTime(&systemtime);
  440. Date date;
  441. date.day = systemtime.wDay;
  442. date.month = Month(systemtime.wMonth);
  443. date.weekday = Weekday(systemtime.wDayOfWeek);
  444. date.year = systemtime.wYear;
  445. date.dst = false;
  446. return date;
  447. }
  448. OS::Time OSWinrt::get_time(bool utc) const {
  449. SYSTEMTIME systemtime;
  450. if (utc)
  451. GetSystemTime(&systemtime);
  452. else
  453. GetLocalTime(&systemtime);
  454. Time time;
  455. time.hour = systemtime.wHour;
  456. time.min = systemtime.wMinute;
  457. time.sec = systemtime.wSecond;
  458. return time;
  459. }
  460. OS::TimeZoneInfo OSWinrt::get_time_zone_info() const {
  461. TIME_ZONE_INFORMATION info;
  462. bool daylight = false;
  463. if (GetTimeZoneInformation(&info) == TIME_ZONE_ID_DAYLIGHT)
  464. daylight = true;
  465. TimeZoneInfo ret;
  466. if (daylight) {
  467. ret.name = info.DaylightName;
  468. } else {
  469. ret.name = info.StandardName;
  470. }
  471. ret.bias = info.Bias;
  472. return ret;
  473. }
  474. uint64_t OSWinrt::get_unix_time() const {
  475. FILETIME ft;
  476. SYSTEMTIME st;
  477. GetSystemTime(&st);
  478. SystemTimeToFileTime(&st, &ft);
  479. SYSTEMTIME ep;
  480. ep.wYear = 1970;
  481. ep.wMonth = 1;
  482. ep.wDayOfWeek = 4;
  483. ep.wDay = 1;
  484. ep.wHour = 0;
  485. ep.wMinute = 0;
  486. ep.wSecond = 0;
  487. ep.wMilliseconds = 0;
  488. FILETIME fep;
  489. SystemTimeToFileTime(&ep, &fep);
  490. return (*(uint64_t *)&ft - *(uint64_t *)&fep) / 10000000;
  491. };
  492. void OSWinrt::delay_usec(uint32_t p_usec) const {
  493. int msec = p_usec < 1000 ? 1 : p_usec / 1000;
  494. // no Sleep()
  495. WaitForSingleObjectEx(GetCurrentThread(), msec, false);
  496. }
  497. uint64_t OSWinrt::get_ticks_usec() const {
  498. uint64_t ticks;
  499. uint64_t time;
  500. // This is the number of clock ticks since start
  501. QueryPerformanceCounter((LARGE_INTEGER *)&ticks);
  502. // Divide by frequency to get the time in seconds
  503. time = ticks * 1000000L / ticks_per_second;
  504. // Subtract the time at game start to get
  505. // the time since the game started
  506. time -= ticks_start;
  507. return time;
  508. }
  509. void OSWinrt::process_events() {
  510. last_id = joystick->process_controllers(last_id);
  511. process_key_events();
  512. }
  513. void OSWinrt::process_key_events() {
  514. for (int i = 0; i < key_event_pos; i++) {
  515. KeyEvent &kev = key_event_buffer[i];
  516. InputEvent iev;
  517. iev.type = InputEvent::KEY;
  518. iev.key.mod = kev.mod_state;
  519. iev.key.echo = kev.echo;
  520. iev.key.scancode = kev.scancode;
  521. iev.key.unicode = kev.unicode;
  522. iev.key.pressed = kev.pressed;
  523. input_event(iev);
  524. }
  525. key_event_pos = 0;
  526. }
  527. void OSWinrt::queue_key_event(KeyEvent &p_event) {
  528. // This merges Char events with the previous Key event, so
  529. // the unicode can be retrieved without sending duplicate events.
  530. if (p_event.type == KeyEvent::MessageType::CHAR_EVENT_MESSAGE && key_event_pos > 0) {
  531. KeyEvent &old = key_event_buffer[key_event_pos - 1];
  532. ERR_FAIL_COND(old.type != KeyEvent::MessageType::KEY_EVENT_MESSAGE);
  533. key_event_buffer[key_event_pos - 1].unicode = p_event.unicode;
  534. return;
  535. }
  536. ERR_FAIL_COND(key_event_pos >= KEY_EVENT_BUFFER_SIZE);
  537. key_event_buffer[key_event_pos++] = p_event;
  538. }
  539. void OSWinrt::set_cursor_shape(CursorShape p_shape) {
  540. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  541. if (cursor_shape == p_shape)
  542. return;
  543. static const CoreCursorType uwp_cursors[CURSOR_MAX] = {
  544. CoreCursorType::Arrow,
  545. CoreCursorType::IBeam,
  546. CoreCursorType::Hand,
  547. CoreCursorType::Cross,
  548. CoreCursorType::Wait,
  549. CoreCursorType::Wait,
  550. CoreCursorType::Arrow,
  551. CoreCursorType::Arrow,
  552. CoreCursorType::UniversalNo,
  553. CoreCursorType::SizeNorthSouth,
  554. CoreCursorType::SizeWestEast,
  555. CoreCursorType::SizeNortheastSouthwest,
  556. CoreCursorType::SizeNorthwestSoutheast,
  557. CoreCursorType::SizeAll,
  558. CoreCursorType::SizeNorthSouth,
  559. CoreCursorType::SizeWestEast,
  560. CoreCursorType::Help
  561. };
  562. CoreWindow::GetForCurrentThread()->PointerCursor = ref new CoreCursor(uwp_cursors[p_shape], 0);
  563. cursor_shape = p_shape;
  564. }
  565. void OSWinrt::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  566. // FIXME:
  567. // Not implemented, some work needs to be done for this one
  568. // Might be a good source: https://stackoverflow.com/questions/43793745/set-custom-mouse-cursor-on-windows-10-universal-app-uwp
  569. }
  570. Error OSWinrt::execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id, String *r_pipe, int *r_exitcode, bool read_stderr) {
  571. return FAILED;
  572. };
  573. Error OSWinrt::kill(const ProcessID &p_pid) {
  574. return FAILED;
  575. };
  576. Error OSWinrt::set_cwd(const String &p_cwd) {
  577. return FAILED;
  578. }
  579. String OSWinrt::get_executable_path() const {
  580. return "";
  581. }
  582. void OSWinrt::set_icon(const Image &p_icon) {
  583. }
  584. bool OSWinrt::has_environment(const String &p_var) const {
  585. return false;
  586. };
  587. String OSWinrt::get_environment(const String &p_var) const {
  588. return "";
  589. };
  590. String OSWinrt::get_stdin_string(bool p_block) {
  591. return String();
  592. }
  593. void OSWinrt::move_window_to_foreground() {
  594. }
  595. Error OSWinrt::shell_open(String p_uri) {
  596. return FAILED;
  597. }
  598. String OSWinrt::get_locale() const {
  599. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // this should work on phone 8.1, but it doesn't
  600. return "en";
  601. #else
  602. Platform::String ^ language = Windows::Globalization::Language::CurrentInputMethodLanguageTag;
  603. return String(language->Data()).replace("-", "_");
  604. #endif
  605. }
  606. void OSWinrt::release_rendering_thread() {
  607. gl_context->release_current();
  608. }
  609. void OSWinrt::make_rendering_thread() {
  610. gl_context->make_current();
  611. }
  612. void OSWinrt::swap_buffers() {
  613. gl_context->swap_buffers();
  614. }
  615. bool OSWinrt::has_touchscreen_ui_hint() const {
  616. TouchCapabilities ^ tc = ref new TouchCapabilities();
  617. return tc->TouchPresent != 0 || UIViewSettings::GetForCurrentView()->UserInteractionMode == UserInteractionMode::Touch;
  618. }
  619. bool OSWinrt::has_virtual_keyboard() const {
  620. return UIViewSettings::GetForCurrentView()->UserInteractionMode == UserInteractionMode::Touch;
  621. }
  622. void OSWinrt::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect) {
  623. InputPane ^ pane = InputPane::GetForCurrentView();
  624. pane->TryShow();
  625. }
  626. void OSWinrt::hide_virtual_keyboard() {
  627. InputPane ^ pane = InputPane::GetForCurrentView();
  628. pane->TryHide();
  629. }
  630. void OSWinrt::run() {
  631. if (!main_loop)
  632. return;
  633. main_loop->init();
  634. uint64_t last_ticks = get_ticks_usec();
  635. int frames = 0;
  636. uint64_t frame = 0;
  637. while (!force_quit) {
  638. CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
  639. if (managed_object->alert_close_handle) continue;
  640. process_events(); // get rid of pending events
  641. if (Main::iteration() == true)
  642. break;
  643. };
  644. main_loop->finish();
  645. }
  646. MainLoop *OSWinrt::get_main_loop() const {
  647. return main_loop;
  648. }
  649. String OSWinrt::get_data_dir() const {
  650. Windows::Storage::StorageFolder ^ data_folder = Windows::Storage::ApplicationData::Current->LocalFolder;
  651. return String(data_folder->Path->Data()).replace("\\", "/");
  652. }
  653. OSWinrt::OSWinrt() {
  654. key_event_pos = 0;
  655. force_quit = false;
  656. alt_mem = false;
  657. gr_mem = false;
  658. shift_mem = false;
  659. control_mem = false;
  660. meta_mem = false;
  661. minimized = false;
  662. pressrc = 0;
  663. old_invalid = true;
  664. last_id = 0;
  665. mouse_mode = MOUSE_MODE_VISIBLE;
  666. #ifdef STDOUT_FILE
  667. stdo = fopen("stdout.txt", "wb");
  668. #endif
  669. gl_context = NULL;
  670. display_request = ref new Windows::System::Display::DisplayRequest();
  671. managed_object = ref new ManagedType;
  672. managed_object->os = this;
  673. mouse_mode_changed = CreateEvent(NULL, TRUE, FALSE, L"os_mouse_mode_changed");
  674. AudioDriverManagerSW::add_driver(&audio_driver);
  675. }
  676. OSWinrt::~OSWinrt() {
  677. #ifdef STDOUT_FILE
  678. fclose(stdo);
  679. #endif
  680. }