os_uwp.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /*************************************************************************/
  2. /* os_uwp.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. // Must include Winsock before windows.h (included by os_uwp.h)
  31. #include "drivers/unix/net_socket_posix.h"
  32. #include "os_uwp.h"
  33. #include "core/config/project_settings.h"
  34. #include "core/io/marshalls.h"
  35. #include "drivers/unix/ip_unix.h"
  36. #include "drivers/windows/dir_access_windows.h"
  37. #include "drivers/windows/file_access_windows.h"
  38. #include "drivers/windows/mutex_windows.h"
  39. #include "drivers/windows/semaphore_windows.h"
  40. #include "main/main.h"
  41. #include "platform/windows/windows_terminal_logger.h"
  42. #include "servers/audio_server.h"
  43. #include "servers/rendering/rendering_server_default.h"
  44. #include <ppltasks.h>
  45. #include <wrl.h>
  46. using namespace Windows::ApplicationModel::Core;
  47. using namespace Windows::ApplicationModel::Activation;
  48. using namespace Windows::UI::Core;
  49. using namespace Windows::UI::Input;
  50. using namespace Windows::UI::Popups;
  51. using namespace Windows::Foundation;
  52. using namespace Windows::Graphics::Display;
  53. using namespace Microsoft::WRL;
  54. using namespace Windows::UI::ViewManagement;
  55. using namespace Windows::Devices::Input;
  56. using namespace Windows::Devices::Sensors;
  57. using namespace Windows::ApplicationModel::DataTransfer;
  58. using namespace concurrency;
  59. static const float earth_gravity = 9.80665;
  60. int OS_UWP::get_video_driver_count() const {
  61. return 2;
  62. }
  63. Size2 OS_UWP::get_window_size() const {
  64. Size2 size;
  65. size.width = video_mode.width;
  66. size.height = video_mode.height;
  67. return size;
  68. }
  69. int OS_UWP::get_current_video_driver() const {
  70. return video_driver_index;
  71. }
  72. void OS_UWP::set_window_size(const Size2 p_size) {
  73. Windows::Foundation::Size new_size;
  74. new_size.Width = p_size.width;
  75. new_size.Height = p_size.height;
  76. ApplicationView ^ view = ApplicationView::GetForCurrentView();
  77. if (view->TryResizeView(new_size)) {
  78. video_mode.width = p_size.width;
  79. video_mode.height = p_size.height;
  80. }
  81. }
  82. void OS_UWP::set_window_fullscreen(bool p_enabled) {
  83. ApplicationView ^ view = ApplicationView::GetForCurrentView();
  84. video_mode.fullscreen = view->IsFullScreenMode;
  85. if (video_mode.fullscreen == p_enabled)
  86. return;
  87. if (p_enabled) {
  88. video_mode.fullscreen = view->TryEnterFullScreenMode();
  89. } else {
  90. view->ExitFullScreenMode();
  91. video_mode.fullscreen = false;
  92. }
  93. }
  94. bool OS_UWP::is_window_fullscreen() const {
  95. return ApplicationView::GetForCurrentView()->IsFullScreenMode;
  96. }
  97. void OS_UWP::set_keep_screen_on(bool p_enabled) {
  98. if (is_keep_screen_on() == p_enabled)
  99. return;
  100. if (p_enabled)
  101. display_request->RequestActive();
  102. else
  103. display_request->RequestRelease();
  104. OS::set_keep_screen_on(p_enabled);
  105. }
  106. void OS_UWP::initialize_core() {
  107. last_button_state = 0;
  108. //RedirectIOToConsole();
  109. FileAccess::make_default<FileAccessWindows>(FileAccess::ACCESS_RESOURCES);
  110. FileAccess::make_default<FileAccessWindows>(FileAccess::ACCESS_USERDATA);
  111. FileAccess::make_default<FileAccessWindows>(FileAccess::ACCESS_FILESYSTEM);
  112. DirAccess::make_default<DirAccessWindows>(DirAccess::ACCESS_RESOURCES);
  113. DirAccess::make_default<DirAccessWindows>(DirAccess::ACCESS_USERDATA);
  114. DirAccess::make_default<DirAccessWindows>(DirAccess::ACCESS_FILESYSTEM);
  115. NetSocketPosix::make_default();
  116. // We need to know how often the clock is updated
  117. if (!QueryPerformanceFrequency((LARGE_INTEGER *)&ticks_per_second))
  118. ticks_per_second = 1000;
  119. // If timeAtGameStart is 0 then we get the time since
  120. // the start of the computer when we call GetGameTime()
  121. ticks_start = 0;
  122. ticks_start = get_ticks_usec();
  123. IP_Unix::make_default();
  124. cursor_shape = CURSOR_ARROW;
  125. }
  126. void OS_UWP::set_window(Windows::UI::Core::CoreWindow ^ p_window) {
  127. window = p_window;
  128. }
  129. void OS_UWP::screen_size_changed() {
  130. gl_context->reset();
  131. };
  132. Error OS_UWP::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
  133. main_loop = nullptr;
  134. outside = true;
  135. // FIXME: Hardcoded for now, add Vulkan support.
  136. p_video_driver = VIDEO_DRIVER_GLES2;
  137. ContextEGL_UWP::Driver opengl_api_type = ContextEGL_UWP::GLES_2_0;
  138. bool gl_initialization_error = false;
  139. gl_context = memnew(ContextEGL_UWP(window, opengl_api_type));
  140. if (gl_context->initialize() != OK) {
  141. memdelete(gl_context);
  142. gl_context = nullptr;
  143. gl_initialization_error = true;
  144. }
  145. if (opengl_api_type == ContextEGL_UWP::GLES_2_0) {
  146. if (RasterizerGLES2::is_viable() == OK) {
  147. RasterizerGLES2::register_config();
  148. RasterizerGLES2::make_current();
  149. } else {
  150. gl_initialization_error = true;
  151. }
  152. }
  153. if (gl_initialization_error) {
  154. OS::get_singleton()->alert("Your video card driver does not support any of the supported OpenGL versions.\n"
  155. "Please update your drivers or if you have a very old or integrated GPU upgrade it.",
  156. "Unable to initialize video driver");
  157. return ERR_UNAVAILABLE;
  158. }
  159. video_driver_index = p_video_driver;
  160. gl_context->make_current();
  161. gl_context->set_use_vsync(video_mode.use_vsync);
  162. VideoMode vm;
  163. vm.width = gl_context->get_window_width();
  164. vm.height = gl_context->get_window_height();
  165. vm.resizable = false;
  166. ApplicationView ^ view = ApplicationView::GetForCurrentView();
  167. vm.fullscreen = view->IsFullScreenMode;
  168. view->SetDesiredBoundsMode(ApplicationViewBoundsMode::UseVisible);
  169. view->PreferredLaunchWindowingMode = ApplicationViewWindowingMode::PreferredLaunchViewSize;
  170. if (p_desired.fullscreen != view->IsFullScreenMode) {
  171. if (p_desired.fullscreen) {
  172. vm.fullscreen = view->TryEnterFullScreenMode();
  173. } else {
  174. view->ExitFullScreenMode();
  175. vm.fullscreen = false;
  176. }
  177. }
  178. Windows::Foundation::Size desired;
  179. desired.Width = p_desired.width;
  180. desired.Height = p_desired.height;
  181. view->PreferredLaunchViewSize = desired;
  182. if (view->TryResizeView(desired)) {
  183. vm.width = view->VisibleBounds.Width;
  184. vm.height = view->VisibleBounds.Height;
  185. }
  186. set_video_mode(vm);
  187. rendering_server = memnew(RenderingServerDefault);
  188. // FIXME: Reimplement threaded rendering
  189. if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
  190. rendering_server = memnew(RenderingServerWrapMT(rendering_server, false));
  191. }
  192. rendering_server->init();
  193. input = memnew(InputDefault);
  194. joypad = ref new JoypadUWP(input);
  195. joypad->register_events();
  196. AudioDriverManager::initialize(p_audio_driver);
  197. managed_object->update_clipboard();
  198. Clipboard::ContentChanged += ref new EventHandler<Platform::Object ^>(managed_object, &ManagedType::on_clipboard_changed);
  199. accelerometer = Accelerometer::GetDefault();
  200. if (accelerometer != nullptr) {
  201. // 60 FPS
  202. accelerometer->ReportInterval = (1.0f / 60.0f) * 1000;
  203. accelerometer->ReadingChanged +=
  204. ref new TypedEventHandler<Accelerometer ^, AccelerometerReadingChangedEventArgs ^>(managed_object, &ManagedType::on_accelerometer_reading_changed);
  205. }
  206. magnetometer = Magnetometer::GetDefault();
  207. if (magnetometer != nullptr) {
  208. // 60 FPS
  209. magnetometer->ReportInterval = (1.0f / 60.0f) * 1000;
  210. magnetometer->ReadingChanged +=
  211. ref new TypedEventHandler<Magnetometer ^, MagnetometerReadingChangedEventArgs ^>(managed_object, &ManagedType::on_magnetometer_reading_changed);
  212. }
  213. gyrometer = Gyrometer::GetDefault();
  214. if (gyrometer != nullptr) {
  215. // 60 FPS
  216. gyrometer->ReportInterval = (1.0f / 60.0f) * 1000;
  217. gyrometer->ReadingChanged +=
  218. ref new TypedEventHandler<Gyrometer ^, GyrometerReadingChangedEventArgs ^>(managed_object, &ManagedType::on_gyroscope_reading_changed);
  219. }
  220. _ensure_user_data_dir();
  221. if (is_keep_screen_on())
  222. display_request->RequestActive();
  223. set_keep_screen_on(GLOBAL_DEF("display/window/energy_saving/keep_screen_on", true));
  224. return OK;
  225. }
  226. void OS_UWP::set_clipboard(const String &p_text) {
  227. DataPackage ^ clip = ref new DataPackage();
  228. clip->RequestedOperation = DataPackageOperation::Copy;
  229. clip->SetText(ref new Platform::String((LPCWSTR)(p_text.utf16().get_data())));
  230. Clipboard::SetContent(clip);
  231. };
  232. String OS_UWP::get_clipboard() const {
  233. if (managed_object->clipboard != nullptr)
  234. return managed_object->clipboard->Data();
  235. else
  236. return "";
  237. };
  238. void OS_UWP::input_event(const Ref<InputEvent> &p_event) {
  239. input->parse_input_event(p_event);
  240. };
  241. void OS_UWP::delete_main_loop() {
  242. if (main_loop)
  243. memdelete(main_loop);
  244. main_loop = nullptr;
  245. }
  246. void OS_UWP::set_main_loop(MainLoop *p_main_loop) {
  247. input->set_main_loop(p_main_loop);
  248. main_loop = p_main_loop;
  249. }
  250. void OS_UWP::finalize() {
  251. if (main_loop)
  252. memdelete(main_loop);
  253. main_loop = nullptr;
  254. rendering_server->finish();
  255. memdelete(rendering_server);
  256. #ifdef OPENGL_ENABLED
  257. if (gl_context)
  258. memdelete(gl_context);
  259. #endif
  260. memdelete(input);
  261. joypad = nullptr;
  262. }
  263. void OS_UWP::finalize_core() {
  264. NetSocketPosix::cleanup();
  265. }
  266. void OS_UWP::alert(const String &p_alert, const String &p_title) {
  267. Platform::String ^ alert = ref new Platform::String((LPCWSTR)(p_alert.utf16().get_data()));
  268. Platform::String ^ title = ref new Platform::String((LPCWSTR)(p_title.utf16().get_data()));
  269. MessageDialog ^ msg = ref new MessageDialog(alert, title);
  270. UICommand ^ close = ref new UICommand("Close", ref new UICommandInvokedHandler(managed_object, &OS_UWP::ManagedType::alert_close));
  271. msg->Commands->Append(close);
  272. msg->DefaultCommandIndex = 0;
  273. managed_object->alert_close_handle = true;
  274. msg->ShowAsync();
  275. }
  276. void OS_UWP::ManagedType::alert_close(IUICommand ^ command) {
  277. alert_close_handle = false;
  278. }
  279. void OS_UWP::ManagedType::on_clipboard_changed(Platform::Object ^ sender, Platform::Object ^ ev) {
  280. update_clipboard();
  281. }
  282. void OS_UWP::ManagedType::update_clipboard() {
  283. DataPackageView ^ data = Clipboard::GetContent();
  284. if (data->Contains(StandardDataFormats::Text)) {
  285. create_task(data->GetTextAsync()).then([this](Platform::String ^ clipboard_content) {
  286. this->clipboard = clipboard_content;
  287. });
  288. }
  289. }
  290. void OS_UWP::ManagedType::on_accelerometer_reading_changed(Accelerometer ^ sender, AccelerometerReadingChangedEventArgs ^ args) {
  291. AccelerometerReading ^ reading = args->Reading;
  292. os->input->set_accelerometer(Vector3(
  293. reading->AccelerationX * earth_gravity,
  294. reading->AccelerationY * earth_gravity,
  295. reading->AccelerationZ * earth_gravity));
  296. }
  297. void OS_UWP::ManagedType::on_magnetometer_reading_changed(Magnetometer ^ sender, MagnetometerReadingChangedEventArgs ^ args) {
  298. MagnetometerReading ^ reading = args->Reading;
  299. os->input->set_magnetometer(Vector3(
  300. reading->MagneticFieldX,
  301. reading->MagneticFieldY,
  302. reading->MagneticFieldZ));
  303. }
  304. void OS_UWP::ManagedType::on_gyroscope_reading_changed(Gyrometer ^ sender, GyrometerReadingChangedEventArgs ^ args) {
  305. GyrometerReading ^ reading = args->Reading;
  306. os->input->set_magnetometer(Vector3(
  307. reading->AngularVelocityX,
  308. reading->AngularVelocityY,
  309. reading->AngularVelocityZ));
  310. }
  311. void OS_UWP::set_mouse_mode(MouseMode p_mode) {
  312. if (p_mode == MouseMode::MOUSE_MODE_CAPTURED) {
  313. CoreWindow::GetForCurrentThread()->SetPointerCapture();
  314. } else {
  315. CoreWindow::GetForCurrentThread()->ReleasePointerCapture();
  316. }
  317. if (p_mode == MouseMode::MOUSE_MODE_CAPTURED || p_mode == MouseMode::MOUSE_MODE_HIDDEN) {
  318. CoreWindow::GetForCurrentThread()->PointerCursor = nullptr;
  319. } else {
  320. CoreWindow::GetForCurrentThread()->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0);
  321. }
  322. mouse_mode = p_mode;
  323. SetEvent(mouse_mode_changed);
  324. }
  325. OS_UWP::MouseMode OS_UWP::get_mouse_mode() const {
  326. return mouse_mode;
  327. }
  328. Point2 OS_UWP::get_mouse_position() const {
  329. return Point2(old_x, old_y);
  330. }
  331. int OS_UWP::get_mouse_button_state() const {
  332. return last_button_state;
  333. }
  334. void OS_UWP::set_window_title(const String &p_title) {
  335. }
  336. void OS_UWP::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
  337. video_mode = p_video_mode;
  338. }
  339. OS::VideoMode OS_UWP::get_video_mode(int p_screen) const {
  340. return video_mode;
  341. }
  342. void OS_UWP::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
  343. }
  344. String OS_UWP::get_name() const {
  345. return "UWP";
  346. }
  347. OS::Date OS_UWP::get_date(bool utc) const {
  348. SYSTEMTIME systemtime;
  349. if (utc)
  350. GetSystemTime(&systemtime);
  351. else
  352. GetLocalTime(&systemtime);
  353. Date date;
  354. date.day = systemtime.wDay;
  355. date.month = Month(systemtime.wMonth);
  356. date.weekday = Weekday(systemtime.wDayOfWeek);
  357. date.year = systemtime.wYear;
  358. date.dst = false;
  359. return date;
  360. }
  361. OS::Time OS_UWP::get_time(bool utc) const {
  362. SYSTEMTIME systemtime;
  363. if (utc)
  364. GetSystemTime(&systemtime);
  365. else
  366. GetLocalTime(&systemtime);
  367. Time time;
  368. time.hour = systemtime.wHour;
  369. time.min = systemtime.wMinute;
  370. time.sec = systemtime.wSecond;
  371. return time;
  372. }
  373. OS::TimeZoneInfo OS_UWP::get_time_zone_info() const {
  374. TIME_ZONE_INFORMATION info;
  375. bool daylight = false;
  376. if (GetTimeZoneInformation(&info) == TIME_ZONE_ID_DAYLIGHT)
  377. daylight = true;
  378. TimeZoneInfo ret;
  379. if (daylight) {
  380. ret.name = info.DaylightName;
  381. } else {
  382. ret.name = info.StandardName;
  383. }
  384. // Bias value returned by GetTimeZoneInformation is inverted of what we expect
  385. // For example on GMT-3 GetTimeZoneInformation return a Bias of 180, so invert the value to get -180
  386. ret.bias = -info.Bias;
  387. return ret;
  388. }
  389. uint64_t OS_UWP::get_unix_time() const {
  390. FILETIME ft;
  391. SYSTEMTIME st;
  392. GetSystemTime(&st);
  393. SystemTimeToFileTime(&st, &ft);
  394. SYSTEMTIME ep;
  395. ep.wYear = 1970;
  396. ep.wMonth = 1;
  397. ep.wDayOfWeek = 4;
  398. ep.wDay = 1;
  399. ep.wHour = 0;
  400. ep.wMinute = 0;
  401. ep.wSecond = 0;
  402. ep.wMilliseconds = 0;
  403. FILETIME fep;
  404. SystemTimeToFileTime(&ep, &fep);
  405. return (*(uint64_t *)&ft - *(uint64_t *)&fep) / 10000000;
  406. };
  407. void OS_UWP::delay_usec(uint32_t p_usec) const {
  408. int msec = p_usec < 1000 ? 1 : p_usec / 1000;
  409. // no Sleep()
  410. WaitForSingleObjectEx(GetCurrentThread(), msec, false);
  411. }
  412. uint64_t OS_UWP::get_ticks_usec() const {
  413. uint64_t ticks;
  414. // This is the number of clock ticks since start
  415. QueryPerformanceCounter((LARGE_INTEGER *)&ticks);
  416. // Divide by frequency to get the time in seconds
  417. // original calculation shown below is subject to overflow
  418. // with high ticks_per_second and a number of days since the last reboot.
  419. // time = ticks * 1000000L / ticks_per_second;
  420. // we can prevent this by either using 128 bit math
  421. // or separating into a calculation for seconds, and the fraction
  422. uint64_t seconds = ticks / ticks_per_second;
  423. // compiler will optimize these two into one divide
  424. uint64_t leftover = ticks % ticks_per_second;
  425. // remainder
  426. uint64_t time = (leftover * 1000000L) / ticks_per_second;
  427. // seconds
  428. time += seconds * 1000000L;
  429. // Subtract the time at game start to get
  430. // the time since the game started
  431. time -= ticks_start;
  432. return time;
  433. }
  434. void OS_UWP::process_events() {
  435. joypad->process_controllers();
  436. process_key_events();
  437. }
  438. void OS_UWP::process_key_events() {
  439. for (int i = 0; i < key_event_pos; i++) {
  440. KeyEvent &kev = key_event_buffer[i];
  441. Ref<InputEventKey> key_event;
  442. key_event.instance();
  443. key_event->set_alt(kev.alt);
  444. key_event->set_shift(kev.shift);
  445. key_event->set_control(kev.control);
  446. key_event->set_echo(kev.echo);
  447. key_event->set_keycode(kev.keycode);
  448. key_event->set_physical_keycode(kev.physical_keycode);
  449. key_event->set_unicode(kev.unicode);
  450. key_event->set_pressed(kev.pressed);
  451. input_event(key_event);
  452. }
  453. key_event_pos = 0;
  454. }
  455. void OS_UWP::queue_key_event(KeyEvent &p_event) {
  456. // This merges Char events with the previous Key event, so
  457. // the unicode can be retrieved without sending duplicate events.
  458. if (p_event.type == KeyEvent::MessageType::CHAR_EVENT_MESSAGE && key_event_pos > 0) {
  459. KeyEvent &old = key_event_buffer[key_event_pos - 1];
  460. ERR_FAIL_COND(old.type != KeyEvent::MessageType::KEY_EVENT_MESSAGE);
  461. key_event_buffer[key_event_pos - 1].unicode = p_event.unicode;
  462. return;
  463. }
  464. ERR_FAIL_COND(key_event_pos >= KEY_EVENT_BUFFER_SIZE);
  465. key_event_buffer[key_event_pos++] = p_event;
  466. }
  467. void OS_UWP::set_cursor_shape(CursorShape p_shape) {
  468. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  469. if (cursor_shape == p_shape)
  470. return;
  471. static const CoreCursorType uwp_cursors[CURSOR_MAX] = {
  472. CoreCursorType::Arrow,
  473. CoreCursorType::IBeam,
  474. CoreCursorType::Hand,
  475. CoreCursorType::Cross,
  476. CoreCursorType::Wait,
  477. CoreCursorType::Wait,
  478. CoreCursorType::Arrow,
  479. CoreCursorType::Arrow,
  480. CoreCursorType::UniversalNo,
  481. CoreCursorType::SizeNorthSouth,
  482. CoreCursorType::SizeWestEast,
  483. CoreCursorType::SizeNortheastSouthwest,
  484. CoreCursorType::SizeNorthwestSoutheast,
  485. CoreCursorType::SizeAll,
  486. CoreCursorType::SizeNorthSouth,
  487. CoreCursorType::SizeWestEast,
  488. CoreCursorType::Help
  489. };
  490. CoreWindow::GetForCurrentThread()->PointerCursor = ref new CoreCursor(uwp_cursors[p_shape], 0);
  491. cursor_shape = p_shape;
  492. }
  493. OS::CursorShape OS_UWP::get_cursor_shape() const {
  494. return cursor_shape;
  495. }
  496. void OS_UWP::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  497. // TODO
  498. }
  499. Error OS_UWP::execute(const String &p_path, const List<String> &p_arguments, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex) {
  500. return FAILED;
  501. };
  502. Error OS_UWP::create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id) {
  503. return FAILED;
  504. };
  505. Error OS_UWP::kill(const ProcessID &p_pid) {
  506. return FAILED;
  507. };
  508. Error OS_UWP::set_cwd(const String &p_cwd) {
  509. return FAILED;
  510. }
  511. String OS_UWP::get_executable_path() const {
  512. return "";
  513. }
  514. void OS_UWP::set_icon(const Ref<Image> &p_icon) {
  515. }
  516. bool OS_UWP::has_environment(const String &p_var) const {
  517. return false;
  518. };
  519. String OS_UWP::get_environment(const String &p_var) const {
  520. return "";
  521. };
  522. bool OS_UWP::set_environment(const String &p_var, const String &p_value) const {
  523. return false;
  524. }
  525. String OS_UWP::get_stdin_string(bool p_block) {
  526. return String();
  527. }
  528. void OS_UWP::move_window_to_foreground() {
  529. }
  530. Error OS_UWP::shell_open(String p_uri) {
  531. return FAILED;
  532. }
  533. String OS_UWP::get_locale() const {
  534. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // this should work on phone 8.1, but it doesn't
  535. return "en";
  536. #else
  537. Platform::String ^ language = Windows::Globalization::Language::CurrentInputMethodLanguageTag;
  538. return String(language->Data()).replace("-", "_");
  539. #endif
  540. }
  541. void OS_UWP::release_rendering_thread() {
  542. gl_context->release_current();
  543. }
  544. void OS_UWP::make_rendering_thread() {
  545. gl_context->make_current();
  546. }
  547. void OS_UWP::swap_buffers() {
  548. gl_context->swap_buffers();
  549. }
  550. bool OS_UWP::has_touchscreen_ui_hint() const {
  551. TouchCapabilities ^ tc = ref new TouchCapabilities();
  552. return tc->TouchPresent != 0 || UIViewSettings::GetForCurrentView()->UserInteractionMode == UserInteractionMode::Touch;
  553. }
  554. bool OS_UWP::has_virtual_keyboard() const {
  555. return UIViewSettings::GetForCurrentView()->UserInteractionMode == UserInteractionMode::Touch;
  556. }
  557. void OS_UWP::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect, bool p_multiline, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
  558. InputPane ^ pane = InputPane::GetForCurrentView();
  559. pane->TryShow();
  560. }
  561. void OS_UWP::hide_virtual_keyboard() {
  562. InputPane ^ pane = InputPane::GetForCurrentView();
  563. pane->TryHide();
  564. }
  565. static String format_error_message(DWORD id) {
  566. LPWSTR messageBuffer = nullptr;
  567. size_t size = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  568. nullptr, id, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&messageBuffer, 0, nullptr);
  569. String msg = "Error " + itos(id) + ": " + String(messageBuffer, size);
  570. LocalFree(messageBuffer);
  571. return msg;
  572. }
  573. Error OS_UWP::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path) {
  574. String full_path = "game/" + p_path;
  575. p_library_handle = (void *)LoadPackagedLibrary((LPCWSTR)(full_path.utf16().get_data()), 0);
  576. ERR_FAIL_COND_V_MSG(!p_library_handle, ERR_CANT_OPEN, "Can't open dynamic library: " + full_path + ", error: " + format_error_message(GetLastError()) + ".");
  577. return OK;
  578. }
  579. Error OS_UWP::close_dynamic_library(void *p_library_handle) {
  580. if (!FreeLibrary((HMODULE)p_library_handle)) {
  581. return FAILED;
  582. }
  583. return OK;
  584. }
  585. Error OS_UWP::get_dynamic_library_symbol_handle(void *p_library_handle, const String p_name, void *&p_symbol_handle, bool p_optional) {
  586. p_symbol_handle = (void *)GetProcAddress((HMODULE)p_library_handle, p_name.utf8().get_data());
  587. if (!p_symbol_handle) {
  588. if (!p_optional) {
  589. ERR_FAIL_V_MSG(ERR_CANT_RESOLVE, "Can't resolve symbol " + p_name + ", error: " + String::num(GetLastError()) + ".");
  590. } else {
  591. return ERR_CANT_RESOLVE;
  592. }
  593. }
  594. return OK;
  595. }
  596. void OS_UWP::run() {
  597. if (!main_loop)
  598. return;
  599. main_loop->init();
  600. uint64_t last_ticks = get_ticks_usec();
  601. int frames = 0;
  602. uint64_t frame = 0;
  603. while (!force_quit) {
  604. CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
  605. if (managed_object->alert_close_handle)
  606. continue;
  607. process_events(); // get rid of pending events
  608. if (Main::iteration())
  609. break;
  610. };
  611. main_loop->finish();
  612. }
  613. MainLoop *OS_UWP::get_main_loop() const {
  614. return main_loop;
  615. }
  616. String OS_UWP::get_user_data_dir() const {
  617. Windows::Storage::StorageFolder ^ data_folder = Windows::Storage::ApplicationData::Current->LocalFolder;
  618. return String(data_folder->Path->Data()).replace("\\", "/");
  619. }
  620. bool OS_UWP::_check_internal_feature_support(const String &p_feature) {
  621. return p_feature == "pc";
  622. }
  623. OS_UWP::OS_UWP() {
  624. key_event_pos = 0;
  625. force_quit = false;
  626. alt_mem = false;
  627. gr_mem = false;
  628. shift_mem = false;
  629. control_mem = false;
  630. meta_mem = false;
  631. minimized = false;
  632. pressrc = 0;
  633. old_invalid = true;
  634. mouse_mode = MOUSE_MODE_VISIBLE;
  635. #ifdef STDOUT_FILE
  636. stdo = fopen("stdout.txt", "wb");
  637. #endif
  638. gl_context = nullptr;
  639. display_request = ref new Windows::System::Display::DisplayRequest();
  640. managed_object = ref new ManagedType;
  641. managed_object->os = this;
  642. mouse_mode_changed = CreateEvent(nullptr, TRUE, FALSE, L"os_mouse_mode_changed");
  643. AudioDriverManager::add_driver(&audio_driver);
  644. Vector<Logger *> loggers;
  645. loggers.push_back(memnew(WindowsTerminalLogger));
  646. _set_logger(memnew(CompositeLogger(loggers)));
  647. }
  648. OS_UWP::~OS_UWP() {
  649. #ifdef STDOUT_FILE
  650. fclose(stdo);
  651. #endif
  652. }