os_uwp.cpp 23 KB

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