os_uwp.cpp 21 KB

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