|
| 1 | +#pragma once |
| 2 | + |
| 3 | +/* <editor-fold desc="MIT License"> |
| 4 | +
|
| 5 | +Copyright(c) 2025 Robert Osfield |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | +
|
| 9 | +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 10 | +
|
| 11 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 12 | +
|
| 13 | +</editor-fold> */ |
| 14 | + |
| 15 | +#define VK_USE_PLATFORM_OHOS |
| 16 | + |
| 17 | +#include <vsg/app/Window.h> |
| 18 | +#include <vsg/ui/KeyEvent.h> |
| 19 | + |
| 20 | +// Forward declaration for OHOS native window type |
| 21 | +struct NativeWindow; |
| 22 | +typedef struct NativeWindow OHNativeWindow; |
| 23 | + |
| 24 | +// Forward declaration for OHOS ArkUI event type |
| 25 | +struct ArkUI_NodeEvent; |
| 26 | + |
| 27 | +namespace vsgHarmony |
| 28 | +{ |
| 29 | + typedef struct { |
| 30 | + bool capsLock; |
| 31 | + bool numLock; |
| 32 | + bool shiftPressed; |
| 33 | + bool ctrlPressed; |
| 34 | + bool altPressed; |
| 35 | + } KeyMetaState; |
| 36 | + |
| 37 | + /// KeyboardMap maps OpenHarmony keyboard events to vsg::KeySymbol |
| 38 | + class KeyboardMap : public vsg::Object |
| 39 | + { |
| 40 | + public: |
| 41 | + KeyboardMap(); |
| 42 | + |
| 43 | + using KeyCodeToKeySymbolMap = std::map<uint32_t, vsg::KeySymbol>; |
| 44 | + |
| 45 | + bool getKeySymbol(uint32_t keycode, KeyMetaState* KeyMetaState, vsg::KeySymbol& keySymbol, vsg::KeySymbol& modifiedKeySymbol, vsg::KeyModifier& keyModifier); |
| 46 | + |
| 47 | + protected: |
| 48 | + KeyCodeToKeySymbolMap _keycodeMap; |
| 49 | + }; |
| 50 | + |
| 51 | + /// Harmony_Window implements OpenHarmony specific window creation, event handling and vulkan Surface setup. |
| 52 | + /// |
| 53 | + /// In order to initialise the window, an OHNativeWindow* handle must be provided through WindowTraits, |
| 54 | + /// provided via the value "nativeWindow". |
| 55 | + /// |
| 56 | + /// ``` |
| 57 | + /// // void OH_main(struct OH_NativeActivity* activity) |
| 58 | + /// auto traits = vsg::WindowTraits::create(); |
| 59 | + /// traits->setValue("nativeWindow", activity->window); |
| 60 | + /// auto window = vsg::Window::create(traits); |
| 61 | + /// ``` |
| 62 | + /// |
| 63 | + /// This window handle may also be provided through WindowTraits::nativeWindow however due to |
| 64 | + /// the way OpenHarmony loads shared libraries this is likely to encounter duplicate typeinfo for |
| 65 | + /// OHNativeWindow, and as a result can throw a std::bad_any_cast on later SDK versions and some |
| 66 | + /// system architectures. |
| 67 | + class Harmony_Window : public vsg::Inherit<vsg::Window, Harmony_Window> |
| 68 | + { |
| 69 | + public: |
| 70 | + Harmony_Window(vsg::ref_ptr<vsg::WindowTraits> traits); |
| 71 | + Harmony_Window() = delete; |
| 72 | + Harmony_Window(const Harmony_Window&) = delete; |
| 73 | + Harmony_Window operator=(const Harmony_Window&) = delete; |
| 74 | + |
| 75 | + const char* instanceExtensionSurfaceName() const override { return "VK_OHOS_surface"; } |
| 76 | + |
| 77 | + bool valid() const override { return _window; } |
| 78 | + |
| 79 | + bool pollEvents(vsg::UIEvents& events) override; |
| 80 | + |
| 81 | + void resize() override; |
| 82 | + |
| 83 | + bool handleOHOSInputEvent(ArkUI_NodeEvent* event); |
| 84 | + |
| 85 | + protected: |
| 86 | + virtual ~Harmony_Window(); |
| 87 | + |
| 88 | + void _initSurface() override; |
| 89 | + |
| 90 | + OHNativeWindow* _window = nullptr; |
| 91 | + |
| 92 | + int64_t _first_ohos_timestamp = 0; |
| 93 | + vsg::clock::time_point _first_ohos_time_point; |
| 94 | + |
| 95 | + vsg::ref_ptr<KeyboardMap> _keyboard; |
| 96 | + KeyMetaState _keyMetaState; |
| 97 | + }; |
| 98 | + |
| 99 | +} // namespace vsgHarmony |
| 100 | + |
| 101 | +EVSG_type_name(vsgHarmony::Harmony_Window); |
0 commit comments