os_uwp.cpp 23 KB

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