os_uwp.cpp 22 KB

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