os_uwp.cpp 21 KB

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