stereokit-rust 0.4.0-alpha.22

High-Level Rust bindings around the StereoKitC library for XR
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/* SPDX-License-Identifier: MIT */
/* The authors below grant copyright rights under the MIT license:
 * Copyright (c) 2019-2023 Nick Klingensmith
 * Copyright (c) 2023 Qualcomm Technologies, Inc.
 */

#include "uwp.h"
#if defined(SK_OS_WINDOWS_UWP)

#include <dxgi1_2.h>
#include <process.h>

#include "../stereokit.h"
#include "../_stereokit.h"
#include "../sk_math.h"
#include "../asset_types/texture.h"
#include "../libraries/sokol_time.h"
#include "../libraries/stref.h"
#include "../libraries/ferr_thread.h"
#include "../systems/system.h"
#include "../systems/render.h"
#include "../systems/input.h"
#include "../systems/input_keyboard.h"

// The WinRT/UWP mess comes from:
// https://github.com/walbourn/directx-vs-templates/tree/master/d3d11game_uwp_cppwinrt

#include <wrl/client.h>

#include <winrt/Windows.ApplicationModel.h>
#include <winrt/Windows.ApplicationModel.Core.h>
#include <winrt/Windows.ApplicationModel.Activation.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Graphics.Display.h>
#include <winrt/Windows.System.h>
#include <winrt/Windows.UI.Core.h>
#include <winrt/Windows.UI.Input.h>
#include <winrt/Windows.UI.ViewManagement.h>
#include <winrt/Windows.UI.Popups.h>
#include <winrt/Windows.Foundation.Collections.h>

using namespace winrt::Windows::ApplicationModel;
using namespace winrt::Windows::ApplicationModel::Core;
using namespace winrt::Windows::ApplicationModel::Activation;
using namespace winrt::Windows::UI::Core;
using namespace winrt::Windows::UI::ViewManagement;
using namespace winrt::Windows::System;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Graphics::Display;

namespace sk {

///////////////////////////////////////////

struct window_event_t {
	platform_evt_           type;
	platform_evt_data_t     data;
};

struct window_t {
	wchar_t*                title;
	HWND                    handle;
	bool                    run_thread;
	bool                    thread_running;
	bool                    initialized;
	bool                    valid;
	array_t<window_event_t> events;
	ft_mutex_t              event_mtx;

	skg_swapchain_t         swapchain;
	bool                    has_swapchain;

	vec2                    mouse_point;
	bool                    resize_pending;
	int32_t                 resize_x, resize_y;
	RECT                    save_rect;
};

///////////////////////////////////////////

array_t<window_t> uwp_windows            = {};
bool              uwp_keyboard_showing   = false;
bool              uwp_keyboard_show_evts = false;
bool              uwp_mouse_set;
vec2              uwp_mouse_set_delta;
vec2              uwp_mouse_frame_get;
float             uwp_mouse_scroll = 0;

///////////////////////////////////////////

void uwp_on_corewindow_character(CoreWindow     const&, CharacterReceivedEventArgs const& args);
void uwp_on_corewindow_keypress (CoreDispatcher const&, AcceleratorKeyEventArgs    const& args);

bool platform_win_resize        (platform_win_t window_id, int32_t width, int32_t height);
void platform_win_add_event     (platform_win_t win_id, window_event_t evt);

///////////////////////////////////////////

bool platform_impl_init() {
	// This only works with WMR, and will crash elsewhere
	/*try {
		CoreApplication::MainView().CoreWindow().Dispatcher().AcceleratorKeyActivated(uwp_on_corewindow_keypress);
		CoreApplication::MainView().CoreWindow().Dispatcher().RunAsync(CoreDispatcherPriority::Normal, []() {
			CoreApplication::MainView().CoreWindow().CharacterReceived(uwp_on_corewindow_character);
			});
	}
	catch (...) {
		log_warn("Keyboard input not available through this runtime.");
	}*/
	return true;
}

///////////////////////////////////////////

void platform_impl_shutdown() {
	winrt::Windows::ApplicationModel::Core::CoreApplication::Exit();

	for (int32_t i = 0; i < uwp_windows.count; i++) {
		platform_win_destroy(i);
	}
	uwp_windows.free();
}

///////////////////////////////////////////

void platform_impl_step() {
	for (int32_t i = 0; i < uwp_windows.count; i++) {
		if (!uwp_windows[i].resize_pending) continue;
		uwp_windows[i].resize_pending = false;
		if (platform_win_resize((platform_win_t)i, uwp_windows[i].resize_x, uwp_windows[i].resize_y)) {
			window_event_t e = { platform_evt_resize };
			e.data.resize = { uwp_windows[i].resize_x, uwp_windows[i].resize_y };
			platform_win_add_event((platform_win_t)i, e);
		}
	}
	if (uwp_windows.count > 0)
		uwp_mouse_frame_get = uwp_windows[0].mouse_point;
}

///////////////////////////////////////////

void uwp_on_corewindow_character(CoreWindow const &, CharacterReceivedEventArgs const &args) {
	input_text_inject_char((uint32_t)args.KeyCode());
}

///////////////////////////////////////////

void uwp_on_corewindow_keypress(CoreDispatcher const &, AcceleratorKeyEventArgs const & args) {
	// https://github.com/microsoft/DirectXTK/blob/master/Src/Keyboard.cpp#L466
	CorePhysicalKeyStatus status = args.KeyStatus();
	int32_t               vk     = (int32_t)args.VirtualKey();

	bool down;
	switch (args.EventType()) {
	case CoreAcceleratorKeyEventType::KeyDown:
	case CoreAcceleratorKeyEventType::SystemKeyDown: down = true; break;
	case CoreAcceleratorKeyEventType::KeyUp:
	case CoreAcceleratorKeyEventType::SystemKeyUp:   down = false; break;
	default:                                         return;
	}

	if (down) input_key_inject_press  ((key_)vk);
	else      input_key_inject_release((key_)vk);
}

///////////////////////////////////////////

inline float dips_to_pixels(float dips, float DPI) {
	return dips * DPI / 96.f + 0.5f;
}

///////////////////////////////////////////

inline float pixels_to_dips(float pixels, float DPI) {
	return (float(pixels) * 96.f / DPI);
}

///////////////////////////////////////////

bool platform_get_cursor(vec2 *out_pos) {
	*out_pos = uwp_mouse_frame_get;
	return true;
}

///////////////////////////////////////////

void platform_set_cursor(vec2 window_pos) {
	uwp_mouse_set_delta = window_pos - uwp_mouse_frame_get;
	uwp_mouse_frame_get = window_pos;
	uwp_mouse_set       = true;
}

///////////////////////////////////////////

float platform_get_scroll() {
	return uwp_mouse_scroll;
}

///////////////////////////////////////////

bool platform_xr_keyboard_present() {
	return true;
}

///////////////////////////////////////////

void platform_xr_keyboard_show(bool show) {
	// For future improvements, see here:
	// https://github.com/microsoft/Windows-universal-samples/blob/main/Samples/CustomEditControl/cs/CustomEditControl.xaml.cs
	CoreApplication::MainView().CoreWindow().Dispatcher().RunAsync(CoreDispatcherPriority::Normal, [show]() {
		auto inputPane = InputPane::GetForCurrentView();

		// Registering these callbacks up in the class doesn't work, and I
		// don't know why. Likely not worth the pain of figuring it out either.
		if (!uwp_keyboard_show_evts) {
			inputPane.Showing([](auto &&, auto &&) { uwp_keyboard_showing = true;  });
			inputPane.Hiding ([](auto &&, auto &&) { uwp_keyboard_showing = false; });
			uwp_keyboard_show_evts = true;
		}

		if (show) {
			uwp_keyboard_showing = inputPane.TryShow();
		} else {
			if (inputPane.TryHide()) {
				uwp_keyboard_showing = false;
			}
		}
	});
}

///////////////////////////////////////////

bool platform_xr_keyboard_visible() {
	return uwp_keyboard_showing;
}

///////////////////////////////////////////
// Window code                           //
///////////////////////////////////////////

platform_win_type_ platform_win_type() { return platform_win_type_creatable; }

///////////////////////////////////////////

platform_win_t platform_win_get_existing(platform_surface_ ) { return -1; }

///////////////////////////////////////////

class UWPWindow : public winrt::implements<UWPWindow, IFrameworkView> {
public:
	UWPWindow(platform_win_t win_id) :
		m_visible           (true),
		m_in_sizemove       (false),
		m_DPI               (96.f),
		m_logicalWidth      (800.f),
		m_logicalHeight     (600.f),
		m_nativeOrientation (DisplayOrientations::None),
		m_currentOrientation(DisplayOrientations::None),
		win_id              (win_id) { }
	~UWPWindow() { }

	// IFrameworkView methods
	void Initialize(const CoreApplicationView &applicationView) {
		applicationView.Activated  ({ this, &UWPWindow::OnActivated });
		CoreApplication::Suspending({ this, &UWPWindow::OnSuspending });
		CoreApplication::Resuming  ({ this, &UWPWindow::OnResuming   });
	}

	///////////////////////////////////////////

	void Uninitialize() { }

	///////////////////////////////////////////

	void Load(winrt::hstring const&) { }

	///////////////////////////////////////////

	void SetWindow(CoreWindow const & window) {
		auto dispatcher                = CoreWindow             ::GetForCurrentThread().Dispatcher();
		auto navigation                = SystemNavigationManager::GetForCurrentView();
		auto currentDisplayInformation = DisplayInformation     ::GetForCurrentView();

#if defined(NTDDI_WIN10_RS2) && (NTDDI_VERSION >= NTDDI_WIN10_RS2)
		try {
			window.ResizeStarted  ([this](auto&&, auto&&) { m_in_sizemove = true; });
			window.ResizeCompleted([this](auto&&, auto&&) { m_in_sizemove = false; HandleWindowSizeChanged(); });
		} catch (...) { } // Requires Windows 10 Creators Update (10.0.15063) or later
#endif
		window.SizeChanged                            ({ this, &UWPWindow::OnWindowSizeChanged  });
		window.PointerPressed                         ({ this, &UWPWindow::OnMouseButtonDown    });
		window.PointerReleased                        ({ this, &UWPWindow::OnMouseButtonUp      });
		window.PointerWheelChanged                    ({ this, &UWPWindow::OnWheelChanged       });
		window.VisibilityChanged                      ({ this, &UWPWindow::OnVisibilityChanged  });
		window.Activated                              ({ this, &UWPWindow::OnActivation         });
		window.CharacterReceived                      (uwp_on_corewindow_character);
		dispatcher.AcceleratorKeyActivated            (uwp_on_corewindow_keypress);
		currentDisplayInformation.DpiChanged          ({ this, &UWPWindow::OnDpiChanged         });
		currentDisplayInformation.OrientationChanged  ({ this, &UWPWindow::OnOrientationChanged });

		window.Closed([this](auto&&, auto&&) { window_event_t e = { platform_evt_close }; platform_win_add_event(win_id, e); uwp_windows[win_id].run_thread = false; });

		// UWP on Xbox One triggers a back request whenever the B button is pressed
		// which can result in the app being suspended if unhandled
		navigation.BackRequested([](const winrt::Windows::Foundation::IInspectable&, const BackRequestedEventArgs& args)
			{ args.Handled(true); });

		m_DPI                = currentDisplayInformation.LogicalDpi();
		m_logicalWidth       = window.Bounds().Width;
		m_logicalHeight      = window.Bounds().Height;
		m_nativeOrientation  = currentDisplayInformation.NativeOrientation ();
		m_currentOrientation = currentDisplayInformation.CurrentOrientation();
		HandleWindowSizeChanged();

		// Get the HWND of the UWP window
		// https://kennykerr.ca/2012/11/09/windows-8-whered-you-put-my-hwnd/
		struct 
		__declspec(uuid("45D64A29-A63E-4CB6-B498-5781D298CB4F")) 
		__declspec(novtable)
		ICoreWindowInterop : ::IUnknown
		{
			virtual HRESULT __stdcall get_WindowHandle  (HWND* hwnd) = 0;
			virtual HRESULT __stdcall put_MessageHandled(unsigned char) = 0;
		};
		winrt::com_ptr<ICoreWindowInterop> interop {};
		winrt::check_hresult(winrt::get_unknown(window)->QueryInterface(interop.put()));
		winrt::check_hresult(interop->get_WindowHandle(&uwp_windows[win_id].handle));

		uwp_windows[win_id].initialized = true;
		uwp_windows[win_id].valid       = true;
	}

	///////////////////////////////////////////

	void Run() {
		uwp_windows[win_id].run_thread     = true;
		uwp_windows[win_id].thread_running = true;
		while (uwp_windows[win_id].run_thread == true) {
			auto core_window = CoreWindow::GetForCurrentThread();
			core_window.Dispatcher().ProcessEvents(m_visible
				? CoreProcessEventsOption::ProcessAllIfPresent
				: CoreProcessEventsOption::ProcessOneAndAllPending);

			if (uwp_mouse_set) {
				Point pos = core_window.PointerPosition();
				Rect  win = core_window.Bounds();

				vec2 new_point = uwp_mouse_set_delta + vec2{ 
					dips_to_pixels(pos.X, m_DPI) - dips_to_pixels(win.X, m_DPI),
					dips_to_pixels(pos.Y, m_DPI) - dips_to_pixels(win.Y, m_DPI)};

				core_window.PointerPosition(Point(
					pixels_to_dips(new_point.x, m_DPI) + win.X,
					pixels_to_dips(new_point.y, m_DPI) + win.Y));

				uwp_windows[win_id].mouse_point = new_point;
				uwp_mouse_set = false;
			} else {
				Point pos = core_window.PointerPosition();
				Rect  win = core_window.Bounds();
				uwp_windows[win_id].mouse_point = {
					dips_to_pixels(pos.X, m_DPI) - dips_to_pixels(win.X, m_DPI),
					dips_to_pixels(pos.Y, m_DPI) - dips_to_pixels(win.Y, m_DPI)};
			}

			Sleep(1);
		}
		uwp_windows[win_id].thread_running = false;
	}

protected:

	///////////////////////////////////////////
	
	// Event handlers
	void OnActivated(CoreApplicationView const & /*applicationView*/, IActivatedEventArgs const & args) {
		if (args.Kind() == ActivationKind::Launch) {
			auto launchArgs = (const LaunchActivatedEventArgs*)(&args);
			if (launchArgs->PrelaunchActivated()) {
				// Opt-out of Prelaunch
				CoreApplication::Exit();
				return;
			}
		}

		m_DPI = DisplayInformation::GetForCurrentView().LogicalDpi();

		ApplicationView::PreferredLaunchWindowingMode(ApplicationViewWindowingMode::PreferredLaunchViewSize);
		// Change to ApplicationViewWindowingMode::FullScreen to default to full screen

		auto desiredSize = Size(pixels_to_dips(1280, m_DPI), pixels_to_dips(720, m_DPI));

		ApplicationView::PreferredLaunchViewSize(desiredSize);

		auto view    = ApplicationView::GetForCurrentView();
		auto minSize = Size(pixels_to_dips(320, m_DPI), pixels_to_dips(200, m_DPI));

		view.SetPreferredMinSize(minSize);

		CoreWindow::GetForCurrentThread().Activate();

		view.FullScreenSystemOverlayMode(FullScreenSystemOverlayMode::Minimal);
		view.TryResizeView(desiredSize);
	}

	///////////////////////////////////////////

	void OnSuspending(IInspectable const & /*sender*/, SuspendingEventArgs const &) { }

	///////////////////////////////////////////

	void OnResuming(IInspectable const & /*sender*/, IInspectable const & /*args*/) { }

	///////////////////////////////////////////

	void OnWindowSizeChanged(CoreWindow const & sender, WindowSizeChangedEventArgs const & /*args*/)
	{
		m_logicalWidth  = sender.Bounds().Width;
		m_logicalHeight = sender.Bounds().Height;

		if (m_in_sizemove)
			return;

		HandleWindowSizeChanged();
	}

	///////////////////////////////////////////

	void OnMouseButtonDown(CoreWindow const & /*sender*/, PointerEventArgs const &args) {
		window_event_t e = { platform_evt_mouse_press };
		auto           p = args.CurrentPoint().Properties();
		if (p.IsLeftButtonPressed  () && input_key(key_mouse_left)    == button_state_inactive) { e.data.press_release = key_mouse_left;    platform_win_add_event(win_id, e); }
		if (p.IsRightButtonPressed () && input_key(key_mouse_right)   == button_state_inactive) { e.data.press_release = key_mouse_right;   platform_win_add_event(win_id, e); }
		if (p.IsMiddleButtonPressed() && input_key(key_mouse_center)  == button_state_inactive) { e.data.press_release = key_mouse_center;  platform_win_add_event(win_id, e); }
		if (p.IsXButton1Pressed    () && input_key(key_mouse_back)    == button_state_inactive) { e.data.press_release = key_mouse_back;    platform_win_add_event(win_id, e); }
		if (p.IsXButton2Pressed    () && input_key(key_mouse_forward) == button_state_inactive) { e.data.press_release = key_mouse_forward; platform_win_add_event(win_id, e); }
	}

	///////////////////////////////////////////

	void OnMouseButtonUp(CoreWindow const & /*sender*/, PointerEventArgs const &args) {
		window_event_t e = { platform_evt_mouse_release };
		auto           p = args.CurrentPoint().Properties();
		if (!p.IsLeftButtonPressed  () && input_key(key_mouse_left)    == button_state_active) { e.data.press_release = key_mouse_left;    platform_win_add_event(win_id, e); }
		if (!p.IsRightButtonPressed () && input_key(key_mouse_right)   == button_state_active) { e.data.press_release = key_mouse_right;   platform_win_add_event(win_id, e); }
		if (!p.IsMiddleButtonPressed() && input_key(key_mouse_center)  == button_state_active) { e.data.press_release = key_mouse_center;  platform_win_add_event(win_id, e); }
		if (!p.IsXButton1Pressed    () && input_key(key_mouse_back)    == button_state_active) { e.data.press_release = key_mouse_back;    platform_win_add_event(win_id, e); }
		if (!p.IsXButton2Pressed    () && input_key(key_mouse_forward) == button_state_active) { e.data.press_release = key_mouse_forward; platform_win_add_event(win_id, e); }
	}

	///////////////////////////////////////////

	void OnWheelChanged(CoreWindow const & /*sender*/, PointerEventArgs const &args) {
		window_event_t e = { platform_evt_scroll };
		e.data.scroll = (float)args.CurrentPoint().Properties().MouseWheelDelta();
		uwp_mouse_scroll += e.data.scroll;
		platform_win_add_event(win_id, e);
	}

	///////////////////////////////////////////

	void OnVisibilityChanged(CoreWindow const & /*sender*/, VisibilityChangedEventArgs const & args) {
		m_visible = args.Visible();

		window_event_t e = { platform_evt_app_focus };
		e.data.app_focus = m_visible
			? app_focus_active
			: app_focus_background;
		platform_win_add_event(win_id, e);
	}

	///////////////////////////////////////////

	void OnActivation(CoreWindow const & /*sender*/, WindowActivatedEventArgs const & args) {
		window_event_t e = { platform_evt_app_focus };
		e.data.app_focus = args.WindowActivationState() != CoreWindowActivationState::Deactivated
			? app_focus_active
			: app_focus_background;
		platform_win_add_event(win_id, e);
	}

	///////////////////////////////////////////

	void OnAcceleratorKeyActivated(CoreDispatcher const &, AcceleratorKeyEventArgs const & args)
	{
		if (args.EventType() == CoreAcceleratorKeyEventType::SystemKeyDown
			&&  args.VirtualKey() == VirtualKey::Enter
			&&  args.KeyStatus().IsMenuKeyDown
			&& !args.KeyStatus().WasKeyDown) {
			// Implements the classic ALT+ENTER fullscreen toggle
			auto view = ApplicationView::GetForCurrentView();

			if (view.IsFullScreenMode())
				view.ExitFullScreenMode();
			else
				view.TryEnterFullScreenMode();

			args.Handled(true);
		}
	}

	///////////////////////////////////////////

	void OnDpiChanged(DisplayInformation const & sender, IInspectable const & /*args*/) {
		m_DPI = sender.LogicalDpi();
		HandleWindowSizeChanged();
	}

	///////////////////////////////////////////

	void OnOrientationChanged(DisplayInformation const & sender, IInspectable const & /*args*/) {
		auto resizeManager = CoreWindowResizeManager::GetForCurrentView();
		resizeManager.ShouldWaitForLayoutCompletion(true);

		m_currentOrientation = sender.CurrentOrientation();

		HandleWindowSizeChanged();

		resizeManager.NotifyLayoutCompleted();
	}

	///////////////////////////////////////////

private:
	bool  m_visible;
	bool  m_in_sizemove;
	float m_logicalWidth;
	float m_logicalHeight;
	float m_DPI;
	platform_win_t win_id;

	winrt::Windows::Graphics::Display::DisplayOrientations	m_nativeOrientation;
	winrt::Windows::Graphics::Display::DisplayOrientations	m_currentOrientation;

	///////////////////////////////////////////

	DXGI_MODE_ROTATION ComputeDisplayRotation() const {
		DXGI_MODE_ROTATION rotation = DXGI_MODE_ROTATION_UNSPECIFIED;

		switch (m_nativeOrientation) {
		case DisplayOrientations::Landscape:
			switch (m_currentOrientation) {
			case DisplayOrientations::Landscape:        rotation = DXGI_MODE_ROTATION_IDENTITY;  break;
			case DisplayOrientations::Portrait:         rotation = DXGI_MODE_ROTATION_ROTATE270; break;
			case DisplayOrientations::LandscapeFlipped: rotation = DXGI_MODE_ROTATION_ROTATE180; break;
			case DisplayOrientations::PortraitFlipped:  rotation = DXGI_MODE_ROTATION_ROTATE90;  break;
			} break;

		case DisplayOrientations::Portrait:
			switch (m_currentOrientation) {
			case DisplayOrientations::Landscape:        rotation = DXGI_MODE_ROTATION_ROTATE90;  break;
			case DisplayOrientations::Portrait:         rotation = DXGI_MODE_ROTATION_IDENTITY;  break;
			case DisplayOrientations::LandscapeFlipped: rotation = DXGI_MODE_ROTATION_ROTATE270; break;
			case DisplayOrientations::PortraitFlipped:  rotation = DXGI_MODE_ROTATION_ROTATE180; break;
			} break;
		}
		return rotation;
	}

	///////////////////////////////////////////

	void HandleWindowSizeChanged() {
		int32_t output_width  = maxi(1,(int32_t)dips_to_pixels(m_logicalWidth,  m_DPI));
		int32_t output_height = maxi(1,(int32_t)dips_to_pixels(m_logicalHeight, m_DPI));

		DXGI_MODE_ROTATION rotation = ComputeDisplayRotation();

		if (rotation == DXGI_MODE_ROTATION_ROTATE90 || rotation == DXGI_MODE_ROTATION_ROTATE270) {
			int32_t swap_tmp = output_width;
			output_width  = output_height;
			output_height = swap_tmp;
		}

		if (output_width  == uwp_windows[win_id].resize_x &&
			output_height == uwp_windows[win_id].resize_y)
			return;

		uwp_windows[win_id].resize_pending = true;
		uwp_windows[win_id].resize_x = output_width;
		uwp_windows[win_id].resize_y = output_height;
	}
};

///////////////////////////////////////////

class UWPWindowFactory : public winrt::implements<UWPWindowFactory, IFrameworkViewSource> {
public:
	platform_win_t win_id;
	UWPWindowFactory(platform_win_t win_id) : win_id(win_id) {}
	IFrameworkView CreateView() {
		return winrt::make<UWPWindow>(win_id);
	}
};

///////////////////////////////////////////

void uwp_window_thread(void *win_id_ptr) {
	platform_win_t       win_id        = *(platform_win_t*)win_id_ptr;
	IFrameworkViewSource windowFactory = winrt::make<UWPWindowFactory>(win_id);
	CoreApplication::Run(windowFactory);
}

///////////////////////////////////////////

platform_win_t platform_win_make(const char* title, recti_t, platform_surface_ surface_type) {
	uwp_windows.add({});
	platform_win_t win_id = uwp_windows.count - 1;
	window_t      *win    = &uwp_windows[win_id];
	win->title     = platform_to_wchar(title);
	win->event_mtx = ft_mutex_create();

	_beginthread(uwp_window_thread, 0, &win_id);

	// Wait until the window thread is ready
	while (!win->initialized) { Sleep(1); }

	// Not all windows need a swapchain, but here's where we make 'em for those
	// that do.
	if (surface_type == platform_surface_swapchain) {
		skg_tex_fmt_ color_fmt = skg_tex_fmt_rgba32_linear;
		skg_tex_fmt_ depth_fmt = (skg_tex_fmt_)render_preferred_depth_fmt();
		win->swapchain     = skg_swapchain_create(win->handle, color_fmt, skg_tex_fmt_none, win->resize_x, win->resize_y);
		win->has_swapchain = true;

		log_diagf("Created swapchain: %dx%d color:%s depth:%s", win->swapchain.width, win->swapchain.height, render_fmt_name((tex_format_)color_fmt), render_fmt_name((tex_format_)depth_fmt));
	}

	if (!win->valid) {
		uwp_windows.remove(win_id);
		return -1;
	} else {
		return win_id;
	}
}

///////////////////////////////////////////

void platform_win_destroy(platform_win_t window) {
	window_t* win = &uwp_windows[window];

	// Message the window thread to stop, and then wait for it.
	win->run_thread = false;
	while (win->thread_running == true) {
		Sleep(1);
	}

	if (win->has_swapchain) {
		skg_swapchain_destroy(&win->swapchain);
	}

	ft_mutex_destroy(&win->event_mtx);

	win->events.free();
	sk_free(win->title);
	*win = {};
}

///////////////////////////////////////////

bool platform_win_resize(platform_win_t window_id, int32_t width, int32_t height) {
	window_t* win = &uwp_windows[window_id];

	width  = maxi(1, width);
	height = maxi(1, height);

	if (win->has_swapchain == false || (width == win->swapchain.width && height == win->swapchain.height))
		return false;

	skg_swapchain_resize(&win->swapchain, width, height);
	return true;
}

///////////////////////////////////////////

bool platform_win_next_event(platform_win_t window_id, platform_evt_* out_event, platform_evt_data_t* out_event_data) {
	window_t *window = &uwp_windows[window_id];
	if (window->events.count == 0) return false;

	ft_mutex_lock(window->event_mtx);
	*out_event      = window->events[0].type;
	*out_event_data = window->events[0].data;
	window->events.remove(0);
	ft_mutex_unlock(window->event_mtx);
	return true;
}

///////////////////////////////////////////

skg_swapchain_t* platform_win_get_swapchain(platform_win_t window) { return uwp_windows[window].has_swapchain ? &uwp_windows[window].swapchain : nullptr; }

///////////////////////////////////////////

recti_t platform_win_rect(platform_win_t window_id) {
	window_t* win = &uwp_windows[window_id];

	// See if we can update it real quick
	//RECT r = {};
	//if (GetWindowRect(win->handle, &r))
	//	win->save_rect = r;

	return recti_t{
		win->save_rect.left,
		win->save_rect.top,
		win->save_rect.right  - win->save_rect.left,
		win->save_rect.bottom - win->save_rect.top };
}

///////////////////////////////////////////

void platform_win_add_event(platform_win_t win_id, window_event_t evt) {
	if (uwp_windows[win_id].run_thread == false) return;
	ft_mutex_lock(uwp_windows[win_id].event_mtx);
	uwp_windows[win_id].events.add(evt);
	ft_mutex_unlock(uwp_windows[win_id].event_mtx);
}

///////////////////////////////////////////

bool in_messagebox = false;
void platform_msgbox_err(const char *text, const char *header) {
	wchar_t* text_w   = platform_to_wchar(text);
	wchar_t* header_w = platform_to_wchar(header);
	in_messagebox = true;
	winrt::Windows::ApplicationModel::Core::CoreApplication::MainView().CoreWindow().Dispatcher().RunAsync(
		winrt::Windows::UI::Core::CoreDispatcherPriority::Normal, [text_w, header_w]() {

		winrt::Windows::UI::Popups::MessageDialog dialog  = winrt::Windows::UI::Popups::MessageDialog(text_w, header_w);
		winrt::Windows::UI::Popups::UICommand     command = winrt::Windows::UI::Popups::UICommand{
			L"OK",
			winrt::Windows::UI::Popups::UICommandInvokedHandler{
				[](winrt::Windows::UI::Popups::IUICommand const &) {
					in_messagebox = false;
					return;
				}
			}
		};
		dialog.Commands().Append(command);
		dialog.ShowAsync();
	});
	while (in_messagebox) {
		platform_sleep(100);
	}
	sk_free(text_w);
	sk_free(header_w);
}


///////////////////////////////////////////

void platform_iterate_dir(const char*, void*, void (*)(void* callback_data, const char* name, const platform_file_attr_t file_attr)) {
}

}

#endif // defined(SK_OS_WINDOWS_UWP)