@ng-native/desktop 0.0.0 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +52 -0
- package/app.plugin.cjs +17 -0
- package/bin/app-config.mjs +168 -0
- package/bin/autolink.mjs +224 -0
- package/bin/cli.mjs +158 -0
- package/bin/host.mjs +427 -0
- package/bin/ng-native-desktop.mjs +4 -0
- package/bin/package.mjs +971 -0
- package/bin/plist.mjs +68 -0
- package/bin/prebuilt.mjs +328 -0
- package/bin/toolchain.mjs +192 -0
- package/native/CMakeLists.txt +277 -0
- package/native/appkit/CMakeLists.txt +130 -0
- package/native/appkit/address-search.h +26 -0
- package/native/appkit/address-search.mm +277 -0
- package/native/appkit/animation-frames.h +14 -0
- package/native/appkit/animation-frames.mm +106 -0
- package/native/appkit/app-info.h +12 -0
- package/native/appkit/app-info.mm +98 -0
- package/native/appkit/app-updater.mm +128 -0
- package/native/appkit/app-windows.h +81 -0
- package/native/appkit/app-windows.mm +318 -0
- package/native/appkit/appkit-backend.h +73 -0
- package/native/appkit/appkit-backend.mm +276 -0
- package/native/appkit/apple-authentication.h +19 -0
- package/native/appkit/apple-authentication.mm +248 -0
- package/native/appkit/apple-button-view.h +10 -0
- package/native/appkit/apple-button-view.mm +37 -0
- package/native/appkit/apple-id-button.h +16 -0
- package/native/appkit/apple-id-button.mm +50 -0
- package/native/appkit/audio.h +16 -0
- package/native/appkit/audio.mm +275 -0
- package/native/appkit/battery.h +13 -0
- package/native/appkit/battery.mm +92 -0
- package/native/appkit/bookmarks.h +17 -0
- package/native/appkit/bookmarks.mm +145 -0
- package/native/appkit/calendar.h +25 -0
- package/native/appkit/calendar.mm +660 -0
- package/native/appkit/camera-sheet.h +59 -0
- package/native/appkit/camera-sheet.mm +344 -0
- package/native/appkit/camera-view.h +15 -0
- package/native/appkit/camera-view.mm +134 -0
- package/native/appkit/camera.h +61 -0
- package/native/appkit/camera.mm +496 -0
- package/native/appkit/cg-canvas.h +24 -0
- package/native/appkit/cg-canvas.mm +663 -0
- package/native/appkit/component-view.h +17 -0
- package/native/appkit/crypto.h +19 -0
- package/native/appkit/crypto.mm +78 -0
- package/native/appkit/desktop-expo.h +29 -0
- package/native/appkit/desktop-expo.mm +385 -0
- package/native/appkit/expo-image-drawing.h +46 -0
- package/native/appkit/expo-image-drawing.mm +131 -0
- package/native/appkit/expo-image-view.h +15 -0
- package/native/appkit/expo-image-view.mm +182 -0
- package/native/appkit/expo-ui-renderer.h +68 -0
- package/native/appkit/expo-ui-renderer.mm +101 -0
- package/native/appkit/expo-ui-view.h +26 -0
- package/native/appkit/expo-ui-view.mm +134 -0
- package/native/appkit/expo-ui.swift +1284 -0
- package/native/appkit/file-system.h +16 -0
- package/native/appkit/file-system.mm +140 -0
- package/native/appkit/fonts.h +12 -0
- package/native/appkit/fonts.mm +48 -0
- package/native/appkit/geocoder.h +19 -0
- package/native/appkit/geocoder.mm +101 -0
- package/native/appkit/glass-effect.h +22 -0
- package/native/appkit/glass-effect.mm +63 -0
- package/native/appkit/glass-view.h +8 -0
- package/native/appkit/glass-view.mm +65 -0
- package/native/appkit/header-toolbar.h +60 -0
- package/native/appkit/header-toolbar.mm +632 -0
- package/native/appkit/host-view.h +54 -0
- package/native/appkit/host-view.mm +1228 -0
- package/native/appkit/image-manipulator.h +14 -0
- package/native/appkit/image-manipulator.mm +191 -0
- package/native/appkit/image-request.h +22 -0
- package/native/appkit/image-request.mm +62 -0
- package/native/appkit/image-view.h +8 -0
- package/native/appkit/image-view.mm +142 -0
- package/native/appkit/input-replay.h +25 -0
- package/native/appkit/input-replay.mm +418 -0
- package/native/appkit/language-model.h +14 -0
- package/native/appkit/language-model.mm +99 -0
- package/native/appkit/language-model.swift +472 -0
- package/native/appkit/local-authentication.h +15 -0
- package/native/appkit/local-authentication.mm +133 -0
- package/native/appkit/location.h +16 -0
- package/native/appkit/location.mm +214 -0
- package/native/appkit/main.mm +1475 -0
- package/native/appkit/map-view.h +15 -0
- package/native/appkit/map-view.mm +109 -0
- package/native/appkit/maps.h +35 -0
- package/native/appkit/maps.mm +343 -0
- package/native/appkit/menu-bar.h +22 -0
- package/native/appkit/menu-bar.mm +208 -0
- package/native/appkit/modal-focus.h +16 -0
- package/native/appkit/modal-focus.mm +26 -0
- package/native/appkit/modal-host-view.h +79 -0
- package/native/appkit/modal-host-view.mm +442 -0
- package/native/appkit/native-controls.h +22 -0
- package/native/appkit/native-controls.mm +431 -0
- package/native/appkit/network-state.h +12 -0
- package/native/appkit/network-state.mm +64 -0
- package/native/appkit/network.h +14 -0
- package/native/appkit/network.mm +396 -0
- package/native/appkit/notifications.h +19 -0
- package/native/appkit/notifications.mm +309 -0
- package/native/appkit/photo-library.h +33 -0
- package/native/appkit/photo-library.mm +548 -0
- package/native/appkit/pointer-buttons.h +32 -0
- package/native/appkit/presentation-label.h +15 -0
- package/native/appkit/presentation-label.mm +12 -0
- package/native/appkit/presented-screen.h +19 -0
- package/native/appkit/presented-screen.mm +181 -0
- package/native/appkit/printing.h +42 -0
- package/native/appkit/printing.mm +393 -0
- package/native/appkit/push.h +19 -0
- package/native/appkit/push.mm +81 -0
- package/native/appkit/red-box.h +26 -0
- package/native/appkit/red-box.mm +387 -0
- package/native/appkit/screen-capture.h +21 -0
- package/native/appkit/screen-capture.mm +92 -0
- package/native/appkit/scroll-view.h +13 -0
- package/native/appkit/scroll-view.mm +251 -0
- package/native/appkit/scrollers.h +25 -0
- package/native/appkit/scrollers.mm +43 -0
- package/native/appkit/search-field.h +34 -0
- package/native/appkit/search-field.mm +291 -0
- package/native/appkit/search-toolbar-item.h +19 -0
- package/native/appkit/search-toolbar-item.mm +269 -0
- package/native/appkit/share-picker.h +34 -0
- package/native/appkit/share-picker.mm +121 -0
- package/native/appkit/split-view.h +77 -0
- package/native/appkit/split-view.mm +1206 -0
- package/native/appkit/store-review.h +13 -0
- package/native/appkit/store-review.mm +28 -0
- package/native/appkit/surface-view.h +45 -0
- package/native/appkit/surface-view.mm +562 -0
- package/native/appkit/svg-view.h +18 -0
- package/native/appkit/svg-view.mm +103 -0
- package/native/appkit/symbol-drawing.h +24 -0
- package/native/appkit/symbol-drawing.mm +117 -0
- package/native/appkit/symbol-view.h +8 -0
- package/native/appkit/symbol-view.mm +33 -0
- package/native/appkit/system-access.h +34 -0
- package/native/appkit/system-access.mm +210 -0
- package/native/appkit/system.h +24 -0
- package/native/appkit/system.mm +403 -0
- package/native/appkit/tab-walk.h +31 -0
- package/native/appkit/tab-walk.mm +64 -0
- package/native/appkit/test-launch.h +36 -0
- package/native/appkit/test-launch.mm +115 -0
- package/native/appkit/text-input-field.h +81 -0
- package/native/appkit/text-input-field.mm +525 -0
- package/native/appkit/text-layout.h +58 -0
- package/native/appkit/text-layout.mm +485 -0
- package/native/appkit/text-recognition.h +13 -0
- package/native/appkit/text-recognition.mm +102 -0
- package/native/appkit/tracking.h +13 -0
- package/native/appkit/tracking.mm +51 -0
- package/native/appkit/tray-and-dock.h +45 -0
- package/native/appkit/tray-and-dock.mm +130 -0
- package/native/appkit/undo.h +23 -0
- package/native/appkit/undo.mm +125 -0
- package/native/appkit/updates.h +17 -0
- package/native/appkit/updates.mm +71 -0
- package/native/appkit/video-view.h +10 -0
- package/native/appkit/video-view.mm +42 -0
- package/native/appkit/video.h +38 -0
- package/native/appkit/video.mm +404 -0
- package/native/appkit/web-browser.h +12 -0
- package/native/appkit/web-browser.mm +97 -0
- package/native/appkit/web-view-component.h +10 -0
- package/native/appkit/web-view-component.mm +62 -0
- package/native/appkit/web-view.h +31 -0
- package/native/appkit/web-view.mm +401 -0
- package/native/cmake/libraries/react-native-reanimated.cmake +45 -0
- package/native/cmake/libraries/react-native-worklets.cmake +46 -0
- package/native/cmake/libraries.cmake +109 -0
- package/native/cmake/linked-modules.cmake +112 -0
- package/native/cmake/react-native.cmake +175 -0
- package/native/cmake/runtime-dependencies.cmake +34 -0
- package/native/cmake/swift.cmake +43 -0
- package/native/cmake/third-party.cmake +74 -0
- package/native/core/include/ngn/absent-views.h +27 -0
- package/native/core/include/ngn/accessibility.h +140 -0
- package/native/core/include/ngn/address-search.h +69 -0
- package/native/core/include/ngn/animation-frames.h +39 -0
- package/native/core/include/ngn/app-info.h +52 -0
- package/native/core/include/ngn/app-menus.h +151 -0
- package/native/core/include/ngn/app-windows.h +181 -0
- package/native/core/include/ngn/apple-authentication.h +116 -0
- package/native/core/include/ngn/asset.h +28 -0
- package/native/core/include/ngn/audio.h +125 -0
- package/native/core/include/ngn/back-swipe.h +121 -0
- package/native/core/include/ngn/battery.h +48 -0
- package/native/core/include/ngn/bytes.h +59 -0
- package/native/core/include/ngn/calendar.h +180 -0
- package/native/core/include/ngn/camera.h +222 -0
- package/native/core/include/ngn/control-sizes.h +111 -0
- package/native/core/include/ngn/core-modules.h +108 -0
- package/native/core/include/ngn/crypto.h +38 -0
- package/native/core/include/ngn/default-menu.h +91 -0
- package/native/core/include/ngn/desktop-expo.h +193 -0
- package/native/core/include/ngn/desktop-modal.h +109 -0
- package/native/core/include/ngn/desktop-props.h +75 -0
- package/native/core/include/ngn/desktop-view-props.h +110 -0
- package/native/core/include/ngn/dev-server.h +33 -0
- package/native/core/include/ngn/dom-web-view.h +53 -0
- package/native/core/include/ngn/download.h +24 -0
- package/native/core/include/ngn/expo-image.h +192 -0
- package/native/core/include/ngn/expo-ui.h +133 -0
- package/native/core/include/ngn/file-system.h +274 -0
- package/native/core/include/ngn/focus-order.h +63 -0
- package/native/core/include/ngn/fonts.h +64 -0
- package/native/core/include/ngn/gestures.h +305 -0
- package/native/core/include/ngn/glass.h +67 -0
- package/native/core/include/ngn/header-bar.h +112 -0
- package/native/core/include/ngn/host-state.h +243 -0
- package/native/core/include/ngn/image-manipulator.h +70 -0
- package/native/core/include/ngn/input-dispatcher.h +211 -0
- package/native/core/include/ngn/input-script.h +56 -0
- package/native/core/include/ngn/input.h +112 -0
- package/native/core/include/ngn/key-value-store.h +41 -0
- package/native/core/include/ngn/language-model.h +69 -0
- package/native/core/include/ngn/launch-frame.h +47 -0
- package/native/core/include/ngn/launch-settings.h +98 -0
- package/native/core/include/ngn/layout-animations.h +24 -0
- package/native/core/include/ngn/library-components.h +13 -0
- package/native/core/include/ngn/library-glue.h +53 -0
- package/native/core/include/ngn/live-regions.h +54 -0
- package/native/core/include/ngn/local-authentication.h +74 -0
- package/native/core/include/ngn/location.h +89 -0
- package/native/core/include/ngn/maps.h +226 -0
- package/native/core/include/ngn/module-registry.h +51 -0
- package/native/core/include/ngn/mounting-backend.h +96 -0
- package/native/core/include/ngn/mounting-manager.h +155 -0
- package/native/core/include/ngn/native-controls.h +161 -0
- package/native/core/include/ngn/native-module.h +109 -0
- package/native/core/include/ngn/network-state.h +44 -0
- package/native/core/include/ngn/network.h +100 -0
- package/native/core/include/ngn/notifications.h +185 -0
- package/native/core/include/ngn/photo-library.h +134 -0
- package/native/core/include/ngn/printing.h +69 -0
- package/native/core/include/ngn/processes.h +114 -0
- package/native/core/include/ngn/red-box.h +119 -0
- package/native/core/include/ngn/reload-thread.h +258 -0
- package/native/core/include/ngn/runtime.h +163 -0
- package/native/core/include/ngn/safe-areas.h +82 -0
- package/native/core/include/ngn/screen-capture.h +35 -0
- package/native/core/include/ngn/screen-presentation.h +85 -0
- package/native/core/include/ngn/screen-stacks.h +80 -0
- package/native/core/include/ngn/scrollers.h +106 -0
- package/native/core/include/ngn/sharing.h +39 -0
- package/native/core/include/ngn/spawn-rules.h +78 -0
- package/native/core/include/ngn/splash-screen.h +41 -0
- package/native/core/include/ngn/split-view.h +270 -0
- package/native/core/include/ngn/sqlite.h +38 -0
- package/native/core/include/ngn/store-review.h +26 -0
- package/native/core/include/ngn/surface-book.h +101 -0
- package/native/core/include/ngn/surface-metrics.h +51 -0
- package/native/core/include/ngn/svg.h +434 -0
- package/native/core/include/ngn/symbol.h +90 -0
- package/native/core/include/ngn/system.h +102 -0
- package/native/core/include/ngn/tabs-host.h +171 -0
- package/native/core/include/ngn/text-input.h +134 -0
- package/native/core/include/ngn/text-measurer.h +38 -0
- package/native/core/include/ngn/text-recognition.h +55 -0
- package/native/core/include/ngn/tracking.h +34 -0
- package/native/core/include/ngn/tray-and-dock.h +107 -0
- package/native/core/include/ngn/undo.h +37 -0
- package/native/core/include/ngn/updates.h +253 -0
- package/native/core/include/ngn/video.h +126 -0
- package/native/core/include/ngn/view-box.h +126 -0
- package/native/core/include/ngn/view-transform.h +26 -0
- package/native/core/include/ngn/view-tree.h +85 -0
- package/native/core/include/ngn/web-browser.h +45 -0
- package/native/core/include/ngn/web-view.h +137 -0
- package/native/core/include/ngn/worklet-host.h +98 -0
- package/native/core/src/absent-views.cpp +41 -0
- package/native/core/src/accessibility.cpp +374 -0
- package/native/core/src/animation-frames.cpp +49 -0
- package/native/core/src/app-menus.cpp +359 -0
- package/native/core/src/app-windows.cpp +444 -0
- package/native/core/src/back-swipe.cpp +98 -0
- package/native/core/src/bytes.cpp +158 -0
- package/native/core/src/control-sizes.cpp +232 -0
- package/native/core/src/default-menu.cpp +96 -0
- package/native/core/src/desktop-modal.cpp +162 -0
- package/native/core/src/desktop-view-props.cpp +80 -0
- package/native/core/src/dev-server-helper.cpp +152 -0
- package/native/core/src/dev-server.cpp +103 -0
- package/native/core/src/dom-web-view.cpp +45 -0
- package/native/core/src/download.cpp +53 -0
- package/native/core/src/expo-image.cpp +548 -0
- package/native/core/src/expo-ui.cpp +203 -0
- package/native/core/src/focus-order.cpp +441 -0
- package/native/core/src/gestures.cpp +1806 -0
- package/native/core/src/glass.cpp +49 -0
- package/native/core/src/header-bar.cpp +104 -0
- package/native/core/src/host-services.cpp +50 -0
- package/native/core/src/host-state.cpp +61 -0
- package/native/core/src/input-dispatcher.cpp +727 -0
- package/native/core/src/input-script.cpp +123 -0
- package/native/core/src/key-value-store.cpp +169 -0
- package/native/core/src/launch-settings.cpp +148 -0
- package/native/core/src/layout-animations.cpp +62 -0
- package/native/core/src/libraries/RNCWebViewSpec.cpp +80 -0
- package/native/core/src/libraries/react-native-reanimated.cpp +216 -0
- package/native/core/src/libraries/react-native-worklets.cpp +192 -0
- package/native/core/src/libraries/rnscreens.cpp +125 -0
- package/native/core/src/libraries/rnsvg.cpp +358 -0
- package/native/core/src/libraries/safeareacontext.cpp +70 -0
- package/native/core/src/libraries/worklets-idle-frames.js +70 -0
- package/native/core/src/library-glue.cpp +35 -0
- package/native/core/src/live-regions.cpp +122 -0
- package/native/core/src/module-registry.cpp +50 -0
- package/native/core/src/modules/accessibility-manager.cpp +98 -0
- package/native/core/src/modules/address-search.cpp +219 -0
- package/native/core/src/modules/alert-manager.cpp +140 -0
- package/native/core/src/modules/app-info.cpp +74 -0
- package/native/core/src/modules/app-modules.cpp +412 -0
- package/native/core/src/modules/apple-authentication.cpp +188 -0
- package/native/core/src/modules/asset.cpp +111 -0
- package/native/core/src/modules/async-storage.cpp +192 -0
- package/native/core/src/modules/audio.cpp +541 -0
- package/native/core/src/modules/battery.cpp +88 -0
- package/native/core/src/modules/calendar.cpp +457 -0
- package/native/core/src/modules/camera.cpp +401 -0
- package/native/core/src/modules/core-modules.cpp +62 -0
- package/native/core/src/modules/crypto.cpp +117 -0
- package/native/core/src/modules/desktop-expo.cpp +527 -0
- package/native/core/src/modules/file-system.cpp +941 -0
- package/native/core/src/modules/fonts.cpp +163 -0
- package/native/core/src/modules/image-loader.cpp +105 -0
- package/native/core/src/modules/image-manipulator.cpp +305 -0
- package/native/core/src/modules/language-model.cpp +195 -0
- package/native/core/src/modules/local-authentication.cpp +140 -0
- package/native/core/src/modules/location.cpp +186 -0
- package/native/core/src/modules/maps.cpp +500 -0
- package/native/core/src/modules/network-state.cpp +72 -0
- package/native/core/src/modules/networking.cpp +387 -0
- package/native/core/src/modules/notifications.cpp +549 -0
- package/native/core/src/modules/photo-library.cpp +385 -0
- package/native/core/src/modules/printing.cpp +201 -0
- package/native/core/src/modules/screen-capture.cpp +71 -0
- package/native/core/src/modules/sharing.cpp +59 -0
- package/native/core/src/modules/splash-screen.cpp +70 -0
- package/native/core/src/modules/sqlite.cpp +421 -0
- package/native/core/src/modules/store-review.cpp +23 -0
- package/native/core/src/modules/text-recognition.cpp +107 -0
- package/native/core/src/modules/tracking.cpp +46 -0
- package/native/core/src/modules/undo.cpp +59 -0
- package/native/core/src/modules/video.cpp +366 -0
- package/native/core/src/modules/web-browser.cpp +100 -0
- package/native/core/src/modules/web-socket.cpp +266 -0
- package/native/core/src/mounting-manager.cpp +384 -0
- package/native/core/src/native-controls.cpp +129 -0
- package/native/core/src/native-module.cpp +250 -0
- package/native/core/src/network.cpp +219 -0
- package/native/core/src/posix-launcher.cpp +366 -0
- package/native/core/src/processes.cpp +324 -0
- package/native/core/src/red-box-errors.js +83 -0
- package/native/core/src/red-box.cpp +306 -0
- package/native/core/src/runtime.cpp +420 -0
- package/native/core/src/safe-areas.cpp +137 -0
- package/native/core/src/screen-presentation.cpp +61 -0
- package/native/core/src/screen-stacks.cpp +254 -0
- package/native/core/src/scrollers.cpp +181 -0
- package/native/core/src/spawn-rules.cpp +292 -0
- package/native/core/src/split-view.cpp +343 -0
- package/native/core/src/svg.cpp +2190 -0
- package/native/core/src/symbol.cpp +94 -0
- package/native/core/src/tabs-host.cpp +234 -0
- package/native/core/src/text-input.cpp +139 -0
- package/native/core/src/text-layout-manager.cpp +63 -0
- package/native/core/src/tray-and-dock.cpp +154 -0
- package/native/core/src/updates.cpp +965 -0
- package/native/core/src/view-box.cpp +788 -0
- package/native/core/src/view-transform.cpp +45 -0
- package/native/core/src/view-tree.cpp +234 -0
- package/native/core/src/web-view.cpp +143 -0
- package/native/core/src/worklet-host.cpp +10 -0
- package/native/scripts/build-hermes.sh +44 -0
- package/native/scripts/build.sh +175 -0
- package/native/scripts/cache-dir.sh +18 -0
- package/native/scripts/codegen-library.sh +193 -0
- package/native/scripts/compiler-launcher.sh +17 -0
- package/native/scripts/fetch.sh +174 -0
- package/native/scripts/hermes-built.sh +10 -0
- package/native/scripts/host-dir.sh +16 -0
- package/native/scripts/launch-probe.sh +60 -0
- package/native/scripts/macos-target.sh +8 -0
- package/native/scripts/make-prebuilt.mjs +360 -0
- package/native/scripts/prebuilt-host-cache.mjs +158 -0
- package/native/shim/openssl/sha.h +22 -0
- package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +37 -0
- package/package.json +73 -1
- package/prebuilt.json +33 -0
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
#include "input-replay.h"
|
|
2
|
+
|
|
3
|
+
#include <memory>
|
|
4
|
+
|
|
5
|
+
namespace ngn::appkit {
|
|
6
|
+
|
|
7
|
+
namespace {
|
|
8
|
+
|
|
9
|
+
std::function<NSEvent *(NSEvent *, NSWindow *)> gScrollMonitor;
|
|
10
|
+
|
|
11
|
+
// IOKit's gesture phases, which a gesture CGEvent carries (field 132).
|
|
12
|
+
int64_t hidPhase(std::optional<GesturePhase> phase) {
|
|
13
|
+
if (!phase) {
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
16
|
+
switch (*phase) {
|
|
17
|
+
case GesturePhase::Began:
|
|
18
|
+
return 1;
|
|
19
|
+
case GesturePhase::Changed:
|
|
20
|
+
return 2;
|
|
21
|
+
case GesturePhase::Ended:
|
|
22
|
+
return 4;
|
|
23
|
+
case GesturePhase::Cancelled:
|
|
24
|
+
return 8;
|
|
25
|
+
}
|
|
26
|
+
return 0;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// The point in the window's coordinates (bottom-left origin) and on the screens (CGEvent's top-left
|
|
30
|
+
// global coordinates).
|
|
31
|
+
NSPoint inWindow(NSWindow *window, double x, double y) {
|
|
32
|
+
return NSMakePoint(x, window.contentView.frame.size.height - y);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
CGPoint onScreen(NSWindow *window, double x, double y) {
|
|
36
|
+
NSPoint screen = [window convertPointToScreen:inWindow(window, x, y)];
|
|
37
|
+
CGFloat top = NSScreen.screens.firstObject.frame.size.height;
|
|
38
|
+
return CGPointMake(screen.x, top - screen.y);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
void sendCGEvent(NSWindow *window, CGEventRef cg, double x, double y) {
|
|
42
|
+
CGEventSetLocation(cg, onScreen(window, x, y));
|
|
43
|
+
// The window under the pointer, which NSEvent takes its window and its location in it from.
|
|
44
|
+
CGEventSetIntegerValueField(cg, kCGMouseEventWindowUnderMousePointer, window.windowNumber);
|
|
45
|
+
CGEventSetIntegerValueField(cg, kCGMouseEventWindowUnderMousePointerThatCanHandleThisEvent, window.windowNumber);
|
|
46
|
+
NSEvent *event = [NSEvent eventWithCGEvent:cg];
|
|
47
|
+
CFRelease(cg);
|
|
48
|
+
if (event == nil) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
// A made-up scroll or gesture gets a window only where the window server would send it, under
|
|
52
|
+
// the real pointer: elsewhere NSApp drops it, so the window takes it and sends it to the view
|
|
53
|
+
// under its point, as it does a real one.
|
|
54
|
+
if (event.window == window) {
|
|
55
|
+
[NSApp sendEvent:event];
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (event.type == NSEventTypeScrollWheel && gScrollMonitor) {
|
|
59
|
+
event = gScrollMonitor(event, window);
|
|
60
|
+
}
|
|
61
|
+
if (event != nil) {
|
|
62
|
+
[window sendEvent:event];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// The view the replayed press went to. NSWindow sends a real drag to the view its press hit; it
|
|
67
|
+
// drops a made-up drag when no button is down in hardware (NSEvent.pressedMouseButtons), so the
|
|
68
|
+
// replay hands the drag and the release to that view itself, as the window would.
|
|
69
|
+
NSView *__weak gPressedView;
|
|
70
|
+
// Whether the press has dragged: its release then goes to the pressed view too.
|
|
71
|
+
bool gDragged = false;
|
|
72
|
+
|
|
73
|
+
// Whether a replayed press is still being handled: a view tracking it in a loop of its own reads
|
|
74
|
+
// the release from the event queue, where it is posted.
|
|
75
|
+
bool gInPress = false;
|
|
76
|
+
// Whether that press's release is posted and not yet read: what the release does (a field it
|
|
77
|
+
// focuses) has not happened yet.
|
|
78
|
+
bool gReleasePosted = false;
|
|
79
|
+
// The modal window running when the press began, if any: one running after it is one the press's
|
|
80
|
+
// release opened (an alert a button's action runs), inside the press's handling.
|
|
81
|
+
NSWindow *__weak gModalAtPress;
|
|
82
|
+
|
|
83
|
+
void sendMouse(NSWindow *window, NSEventType type, double x, double y) {
|
|
84
|
+
NSEvent *event = [NSEvent mouseEventWithType:type
|
|
85
|
+
location:inWindow(window, x, y)
|
|
86
|
+
modifierFlags:0
|
|
87
|
+
timestamp:NSProcessInfo.processInfo.systemUptime
|
|
88
|
+
windowNumber:window.windowNumber
|
|
89
|
+
context:nil
|
|
90
|
+
eventNumber:0
|
|
91
|
+
clickCount:1
|
|
92
|
+
pressure:type == NSEventTypeLeftMouseUp || type == NSEventTypeRightMouseUp || type == NSEventTypeMouseMoved ? 0 : 1];
|
|
93
|
+
if (type == NSEventTypeLeftMouseDown) {
|
|
94
|
+
gDragged = false;
|
|
95
|
+
NSView *content = window.contentView;
|
|
96
|
+
gPressedView = [content hitTest:[content.superview convertPoint:event.locationInWindow fromView:nil]];
|
|
97
|
+
}
|
|
98
|
+
NSView *pressed = gPressedView;
|
|
99
|
+
// A view tracking the press in a loop of its own reads its drags and its release from the event
|
|
100
|
+
// queue: they are posted there, dragged or not.
|
|
101
|
+
if (gInPress && (type == NSEventTypeLeftMouseDragged || type == NSEventTypeLeftMouseUp)) {
|
|
102
|
+
if (type == NSEventTypeLeftMouseUp) {
|
|
103
|
+
gPressedView = nil;
|
|
104
|
+
gDragged = false;
|
|
105
|
+
gReleasePosted = true;
|
|
106
|
+
}
|
|
107
|
+
[NSApp postEvent:event atStart:NO];
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (type == NSEventTypeLeftMouseDragged && pressed != nil) {
|
|
111
|
+
gDragged = true;
|
|
112
|
+
[pressed mouseDragged:event];
|
|
113
|
+
} else if (type == NSEventTypeLeftMouseUp && pressed != nil && gDragged) {
|
|
114
|
+
gPressedView = nil;
|
|
115
|
+
gDragged = false;
|
|
116
|
+
[pressed mouseUp:event];
|
|
117
|
+
} else {
|
|
118
|
+
// A click's release goes through the window as a real one does, so a view that tracks the
|
|
119
|
+
// press through the window (SwiftUI's controls, AppKit's own) sees it end.
|
|
120
|
+
if (type == NSEventTypeLeftMouseUp) {
|
|
121
|
+
gPressedView = nil;
|
|
122
|
+
}
|
|
123
|
+
bool press = type == NSEventTypeLeftMouseDown;
|
|
124
|
+
gInPress = gInPress || press;
|
|
125
|
+
if (press) {
|
|
126
|
+
gModalAtPress = NSApp.modalWindow;
|
|
127
|
+
}
|
|
128
|
+
[NSApp sendEvent:event];
|
|
129
|
+
if (press) {
|
|
130
|
+
gInPress = false;
|
|
131
|
+
gReleasePosted = false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// The text a key types with `flags` held, by the keyboard layout: the key that types `base` alone,
|
|
137
|
+
// with the modifiers applied. `base` itself when no key types it.
|
|
138
|
+
NSString *typedWith(NSString *base, NSEventModifierFlags flags) {
|
|
139
|
+
if (flags == 0) {
|
|
140
|
+
return base;
|
|
141
|
+
}
|
|
142
|
+
for (unsigned short code = 0; code < 128; code++) {
|
|
143
|
+
NSEvent *key = [NSEvent keyEventWithType:NSEventTypeKeyDown
|
|
144
|
+
location:NSZeroPoint
|
|
145
|
+
modifierFlags:0
|
|
146
|
+
timestamp:0
|
|
147
|
+
windowNumber:0
|
|
148
|
+
context:nil
|
|
149
|
+
characters:base
|
|
150
|
+
charactersIgnoringModifiers:base
|
|
151
|
+
isARepeat:NO
|
|
152
|
+
keyCode:code];
|
|
153
|
+
if ([[key charactersByApplyingModifiers:0] isEqualToString:base]) {
|
|
154
|
+
NSString *typed = [key charactersByApplyingModifiers:flags];
|
|
155
|
+
return typed.length > 0 ? typed : base;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return base;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// The window the keyboard's steps go to: the modal window while one runs, else the key window (a
|
|
162
|
+
// red box or a sheet, not only the app's), else the script's window.
|
|
163
|
+
NSWindow *keyboardWindow(NSWindow *scriptWindow) {
|
|
164
|
+
return NSApp.modalWindow != nil ? NSApp.modalWindow
|
|
165
|
+
: NSApp.keyWindow != nil ? NSApp.keyWindow
|
|
166
|
+
: scriptWindow;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// A key press and its release, to the keyboard's window.
|
|
170
|
+
void sendKey(NSWindow *scriptWindow, const InputStep &step) {
|
|
171
|
+
NSWindow *window = keyboardWindow(scriptWindow);
|
|
172
|
+
NSString *characters = step.key == "escape" ? @"\x1b"
|
|
173
|
+
: step.key == "return" ? @"\r"
|
|
174
|
+
: step.key == "space" ? @" "
|
|
175
|
+
: step.key == "tab" ? @"\t"
|
|
176
|
+
: step.key == "delete" ? @"\x7f"
|
|
177
|
+
: [NSString stringWithUTF8String:step.key.c_str()];
|
|
178
|
+
unsigned short code = step.key == "escape" ? 53
|
|
179
|
+
: step.key == "return" ? 36
|
|
180
|
+
: step.key == "space" ? 49
|
|
181
|
+
: step.key == "tab" ? 48
|
|
182
|
+
: step.key == "delete" ? 51
|
|
183
|
+
: 0;
|
|
184
|
+
// A character typed into text goes in as the input method puts it, through NSTextInputClient: a
|
|
185
|
+
// made-up key event reaches the field editor's keyDown, but the text system inserts nothing for
|
|
186
|
+
// one no keyboard sent.
|
|
187
|
+
bool typed = (step.key.size() == 1 || step.key == "space") &&
|
|
188
|
+
(step.modifiers & (InputStep::Command | InputStep::Control)) == 0;
|
|
189
|
+
NSEventModifierFlags flags = 0;
|
|
190
|
+
if (step.modifiers & InputStep::Command) flags |= NSEventModifierFlagCommand;
|
|
191
|
+
if (step.modifiers & InputStep::Shift) flags |= NSEventModifierFlagShift;
|
|
192
|
+
if (step.modifiers & InputStep::Option) flags |= NSEventModifierFlagOption;
|
|
193
|
+
if (step.modifiers & InputStep::Control) flags |= NSEventModifierFlagControl;
|
|
194
|
+
// Shift and Option change the character typed, as the keyboard layout has them.
|
|
195
|
+
id responder = window.firstResponder;
|
|
196
|
+
if (typed && [responder conformsToProtocol:@protocol(NSTextInputClient)]) {
|
|
197
|
+
NSString *text = typedWith(characters, flags & (NSEventModifierFlagShift | NSEventModifierFlagOption));
|
|
198
|
+
[(id<NSTextInputClient>)responder insertText:text replacementRange:NSMakeRange(NSNotFound, 0)];
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
for (NSEventType type : {NSEventTypeKeyDown, NSEventTypeKeyUp}) {
|
|
202
|
+
NSEvent *event = [NSEvent keyEventWithType:type
|
|
203
|
+
location:NSZeroPoint
|
|
204
|
+
modifierFlags:flags
|
|
205
|
+
timestamp:NSProcessInfo.processInfo.systemUptime
|
|
206
|
+
windowNumber:window.windowNumber
|
|
207
|
+
context:nil
|
|
208
|
+
characters:characters
|
|
209
|
+
charactersIgnoringModifiers:characters
|
|
210
|
+
isARepeat:NO
|
|
211
|
+
keyCode:code];
|
|
212
|
+
[NSApp sendEvent:event];
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Types `text` into the focused field of the keyboard's window (the modal window, else the key
|
|
217
|
+
// window, else the script's window) a character at a time, each as the input method puts a
|
|
218
|
+
// keystroke's character, so the text system's substitutions and capitals follow each one as they
|
|
219
|
+
// follow typing.
|
|
220
|
+
void typeText(NSWindow *window, NSString *text) {
|
|
221
|
+
id responder = keyboardWindow(window).firstResponder;
|
|
222
|
+
if (![responder conformsToProtocol:@protocol(NSTextInputClient)]) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
[text enumerateSubstringsInRange:NSMakeRange(0, text.length)
|
|
226
|
+
options:NSStringEnumerationByComposedCharacterSequences
|
|
227
|
+
usingBlock:^(NSString *character, NSRange, NSRange, BOOL *) {
|
|
228
|
+
[(id<NSTextInputClient>)responder insertText:character
|
|
229
|
+
replacementRange:NSMakeRange(NSNotFound, 0)];
|
|
230
|
+
}];
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// The title a choose step left for the next menu to open, and the observer waiting for it.
|
|
234
|
+
NSString *gChoice;
|
|
235
|
+
id gMenuObserver;
|
|
236
|
+
|
|
237
|
+
// Picks the item titled `title` in `menu`, or in a submenu of it, as a click on it does, and
|
|
238
|
+
// closes the menu. Whether one was found.
|
|
239
|
+
bool pick(NSMenu *menu, NSString *title) {
|
|
240
|
+
for (NSInteger i = 0; i < menu.numberOfItems; i++) {
|
|
241
|
+
NSMenuItem *item = [menu itemAtIndex:i];
|
|
242
|
+
if ([item.title isEqualToString:title] && item.enabled && !item.hidden) {
|
|
243
|
+
[menu cancelTrackingWithoutAnimation];
|
|
244
|
+
[menu performActionForItemAtIndex:i];
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
if (item.submenu != nil && pick(item.submenu, title)) {
|
|
248
|
+
[menu cancelTrackingWithoutAnimation];
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
void chooseInNextMenu(NSString *title) {
|
|
256
|
+
gChoice = title;
|
|
257
|
+
if (gMenuObserver != nil) {
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
gMenuObserver = [NSNotificationCenter.defaultCenter
|
|
261
|
+
addObserverForName:NSMenuDidBeginTrackingNotification
|
|
262
|
+
object:nil
|
|
263
|
+
queue:nil
|
|
264
|
+
usingBlock:^(NSNotification *note) {
|
|
265
|
+
NSMenu *menu = note.object;
|
|
266
|
+
NSString *choice = gChoice;
|
|
267
|
+
// A submenu opening within the menu being chosen from is not the next menu.
|
|
268
|
+
if (choice == nil || menu.supermenu != nil) {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
gChoice = nil;
|
|
272
|
+
// A second open first, so the menu can be seen; the menu's tracking runs the
|
|
273
|
+
// main queue.
|
|
274
|
+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), dispatch_get_main_queue(), ^{
|
|
275
|
+
if (!pick(menu, choice)) {
|
|
276
|
+
fprintf(stderr, "[ngn] input script: the menu has no item %s\n", choice.UTF8String);
|
|
277
|
+
[menu cancelTracking];
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
}];
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
void play(NSWindow *window, const InputStep &step) {
|
|
284
|
+
switch (step.kind) {
|
|
285
|
+
case InputStep::Kind::Choose:
|
|
286
|
+
chooseInNextMenu([NSString stringWithUTF8String:step.key.c_str()]);
|
|
287
|
+
break;
|
|
288
|
+
case InputStep::Kind::Key:
|
|
289
|
+
sendKey(window, step);
|
|
290
|
+
break;
|
|
291
|
+
case InputStep::Kind::Type:
|
|
292
|
+
typeText(window, [NSString stringWithUTF8String:step.key.c_str()]);
|
|
293
|
+
break;
|
|
294
|
+
case InputStep::Kind::Wait:
|
|
295
|
+
break;
|
|
296
|
+
case InputStep::Kind::Move:
|
|
297
|
+
sendMouse(window, NSEventTypeMouseMoved, step.x, step.y);
|
|
298
|
+
break;
|
|
299
|
+
case InputStep::Kind::Down:
|
|
300
|
+
sendMouse(window, NSEventTypeLeftMouseDown, step.x, step.y);
|
|
301
|
+
break;
|
|
302
|
+
case InputStep::Kind::Drag:
|
|
303
|
+
sendMouse(window, NSEventTypeLeftMouseDragged, step.x, step.y);
|
|
304
|
+
break;
|
|
305
|
+
case InputStep::Kind::Up:
|
|
306
|
+
sendMouse(window, NSEventTypeLeftMouseUp, step.x, step.y);
|
|
307
|
+
break;
|
|
308
|
+
case InputStep::Kind::Secondary:
|
|
309
|
+
sendMouse(window, NSEventTypeRightMouseDown, step.x, step.y);
|
|
310
|
+
sendMouse(window, NSEventTypeRightMouseUp, step.x, step.y);
|
|
311
|
+
break;
|
|
312
|
+
case InputStep::Kind::Scroll: {
|
|
313
|
+
// AppKit's deltas move the content: the fingers' movement, as with natural scrolling off.
|
|
314
|
+
CGEventRef cg = CGEventCreateScrollWheelEvent2(
|
|
315
|
+
nullptr, kCGScrollEventUnitPixel, 2, static_cast<int32_t>(-step.dy), static_cast<int32_t>(-step.amount), 0);
|
|
316
|
+
CGEventSetIntegerValueField(cg, kCGScrollWheelEventIsContinuous, 1);
|
|
317
|
+
int64_t phase = step.phase == GesturePhase::Began ? kCGScrollPhaseBegan
|
|
318
|
+
: step.phase == GesturePhase::Changed ? kCGScrollPhaseChanged
|
|
319
|
+
: step.phase == GesturePhase::Ended ? kCGScrollPhaseEnded
|
|
320
|
+
: 0;
|
|
321
|
+
CGEventSetIntegerValueField(cg, kCGScrollWheelEventScrollPhase, phase);
|
|
322
|
+
sendCGEvent(window, cg, step.x, step.y);
|
|
323
|
+
break;
|
|
324
|
+
}
|
|
325
|
+
case InputStep::Kind::Magnify:
|
|
326
|
+
case InputStep::Kind::Rotate: {
|
|
327
|
+
// A gesture event (29), its HID type zoom (8) or rotation (5) in field 110, its amount in 113
|
|
328
|
+
// or 114 and its phase in 132: how the trackpad's own reach the app.
|
|
329
|
+
bool magnify = step.kind == InputStep::Kind::Magnify;
|
|
330
|
+
CGEventRef cg = CGEventCreate(nullptr);
|
|
331
|
+
CGEventSetType(cg, static_cast<CGEventType>(29));
|
|
332
|
+
CGEventSetIntegerValueField(cg, static_cast<CGEventField>(110), magnify ? 8 : 5);
|
|
333
|
+
CGEventSetDoubleValueField(cg, static_cast<CGEventField>(magnify ? 113 : 114), step.amount);
|
|
334
|
+
CGEventSetIntegerValueField(cg, static_cast<CGEventField>(132), hidPhase(step.phase));
|
|
335
|
+
sendCGEvent(window, cg, step.x, step.y);
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
void playFrom(NSWindow *window, std::shared_ptr<InputScript> script, size_t at);
|
|
342
|
+
|
|
343
|
+
// Plays the steps from `at` after `ms`, from a run loop timer in the common modes rather than a
|
|
344
|
+
// main-queue block: a press a view tracks in a loop of its own (a table's row, a text field)
|
|
345
|
+
// holds up the main queue's blocks until it returns, while its loop still fires these timers.
|
|
346
|
+
void playLater(NSWindow *window, std::shared_ptr<InputScript> script, size_t at, double ms) {
|
|
347
|
+
NSTimer *timer = [NSTimer timerWithTimeInterval:ms / 1000
|
|
348
|
+
repeats:NO
|
|
349
|
+
block:^(NSTimer *) {
|
|
350
|
+
playFrom(window, script, at);
|
|
351
|
+
}];
|
|
352
|
+
[NSRunLoop.mainRunLoop addTimer:timer forMode:NSRunLoopCommonModes];
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// Whether a modal session the press's release opened is running: the release has been read, and
|
|
356
|
+
// the press's handling returns only once the session ends.
|
|
357
|
+
bool modalInPress() {
|
|
358
|
+
NSWindow *modal = NSApp.modalWindow;
|
|
359
|
+
return gInPress && gReleasePosted && modal != nil && modal != gModalAtPress;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
void playFrom(NSWindow *window, std::shared_ptr<InputScript> script, size_t at) {
|
|
363
|
+
while (at < script->steps.size()) {
|
|
364
|
+
const auto &step = script->steps[at++];
|
|
365
|
+
// In a modal session the release opened, the keyboard's steps (and waits and menu choices) go
|
|
366
|
+
// on, so a key can end the session and with it the press; a pointer step still waits.
|
|
367
|
+
bool modal = modalInPress();
|
|
368
|
+
bool keyboard = step.kind == InputStep::Kind::Key || step.kind == InputStep::Kind::Type ||
|
|
369
|
+
step.kind == InputStep::Kind::Wait || step.kind == InputStep::Kind::Choose;
|
|
370
|
+
// A press still tracking in its loop has not read the release posted for it: the next press
|
|
371
|
+
// waits for it, so that loop does not take this one's release, and once the release is
|
|
372
|
+
// posted every step waits, so it follows what the release does.
|
|
373
|
+
if (gInPress && (step.kind == InputStep::Kind::Down || gReleasePosted) && !(modal && keyboard)) {
|
|
374
|
+
playLater(window, script, at - 1, 10);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
if (step.kind == InputStep::Kind::Wait) {
|
|
378
|
+
playLater(window, script, at, step.amount);
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
if (step.kind == InputStep::Kind::Down) {
|
|
382
|
+
// The steps after a press go on from a timer, which fires inside the press's tracking loop
|
|
383
|
+
// where a view runs one: its release is then posted for the loop to read (sendMouse).
|
|
384
|
+
playLater(window, script, at, 0);
|
|
385
|
+
play(window, step);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
play(window, step);
|
|
389
|
+
// A step played into the session can end it, which happens once this returns: the next step
|
|
390
|
+
// looks again.
|
|
391
|
+
if (modal) {
|
|
392
|
+
playLater(window, script, at, 10);
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
fprintf(stderr, "[ngn] input script done\n");
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
} // namespace
|
|
400
|
+
|
|
401
|
+
void playInputStep(NSWindow *window, const InputStep &step) {
|
|
402
|
+
if (step.kind != InputStep::Kind::Wait) {
|
|
403
|
+
play(window, step);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
void setReplayScrollMonitor(std::function<NSEvent *(NSEvent *, NSWindow *)> monitor) {
|
|
408
|
+
gScrollMonitor = std::move(monitor);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
void replayInputScript(NSWindow *window, InputScript script) {
|
|
412
|
+
// A click in a window that is not key only brings it forward, as a person's first click does.
|
|
413
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
414
|
+
[window makeKeyAndOrderFront:nil];
|
|
415
|
+
playFrom(window, std::make_shared<InputScript>(std::move(script)), 0);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
} // namespace ngn::appkit
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// The OS half of NgNativeLocalLlm (the core's language-model.h): Apple's Foundation Models on macOS
|
|
2
|
+
// 26 and later, through the C interface language-model.swift exports. On an older macOS the
|
|
3
|
+
// availability is notEligible and no session is made.
|
|
4
|
+
#pragma once
|
|
5
|
+
|
|
6
|
+
#include "ngn/language-model.h"
|
|
7
|
+
|
|
8
|
+
#include <memory>
|
|
9
|
+
|
|
10
|
+
namespace ngn::appkit {
|
|
11
|
+
|
|
12
|
+
std::shared_ptr<LanguageModelEngine> makeLanguageModelEngine();
|
|
13
|
+
|
|
14
|
+
} // namespace ngn::appkit
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
#include "language-model.h"
|
|
2
|
+
|
|
3
|
+
#include <cstdint>
|
|
4
|
+
|
|
5
|
+
// language-model.swift's C interface.
|
|
6
|
+
extern "C" {
|
|
7
|
+
void ngn_lm_availability(char *out, int32_t size);
|
|
8
|
+
void *ngn_lm_session_create(const char *config, void *context, void (*onTool)(void *, const char *, const char *, const char *),
|
|
9
|
+
void (*onFree)(void *), char *error, int32_t size);
|
|
10
|
+
void ngn_lm_session_respond(void *session, const char *prompt, bool stream, void *context, void (*onText)(void *, const char *),
|
|
11
|
+
void (*onDone)(void *, const char *, const char *, const char *));
|
|
12
|
+
void ngn_lm_session_cancel(void *session);
|
|
13
|
+
bool ngn_lm_tool_answer(void *session, const char *callId, const char *output, const char *error);
|
|
14
|
+
void ngn_lm_session_release(void *session);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
namespace ngn::appkit {
|
|
18
|
+
|
|
19
|
+
namespace {
|
|
20
|
+
|
|
21
|
+
using ToolCalls = std::function<void(LanguageModelEngine::ToolCall)>;
|
|
22
|
+
|
|
23
|
+
struct TurnContext {
|
|
24
|
+
std::function<void(const std::string &)> onText;
|
|
25
|
+
std::function<void(LanguageModelEngine::Outcome)> done;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
class SwiftSession final : public LanguageModelEngine::Session {
|
|
29
|
+
public:
|
|
30
|
+
explicit SwiftSession(void *handle) : handle_(handle) {}
|
|
31
|
+
~SwiftSession() override {
|
|
32
|
+
ngn_lm_session_release(handle_);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
void respond(const std::string &prompt, bool stream, std::function<void(const std::string &)> onText,
|
|
36
|
+
std::function<void(LanguageModelEngine::Outcome)> done) override {
|
|
37
|
+
// Freed by the one call to done.
|
|
38
|
+
auto *turn = new TurnContext{std::move(onText), std::move(done)};
|
|
39
|
+
ngn_lm_session_respond(
|
|
40
|
+
handle_, prompt.c_str(), stream, turn,
|
|
41
|
+
[](void *context, const char *text) { static_cast<TurnContext *>(context)->onText(text); },
|
|
42
|
+
[](void *context, const char *text, const char *code, const char *message) {
|
|
43
|
+
std::unique_ptr<TurnContext> turn(static_cast<TurnContext *>(context));
|
|
44
|
+
LanguageModelEngine::Outcome outcome;
|
|
45
|
+
if (text != nullptr) {
|
|
46
|
+
outcome.text = text;
|
|
47
|
+
} else {
|
|
48
|
+
outcome.code = code != nullptr ? code : "ERR_GENERATION_FAILED";
|
|
49
|
+
outcome.message = message != nullptr ? message : "";
|
|
50
|
+
}
|
|
51
|
+
turn->done(std::move(outcome));
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
void cancel() override {
|
|
56
|
+
ngn_lm_session_cancel(handle_);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
void answerTool(const std::string &callId, const std::optional<std::string> &output, const std::string &error) override {
|
|
60
|
+
ngn_lm_tool_answer(handle_, callId.c_str(), output ? output->c_str() : nullptr, error.c_str());
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private:
|
|
64
|
+
void *handle_;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
class SwiftEngine final : public LanguageModelEngine {
|
|
68
|
+
public:
|
|
69
|
+
std::string availability() override {
|
|
70
|
+
char out[32];
|
|
71
|
+
ngn_lm_availability(out, sizeof out);
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
std::unique_ptr<Session> session(const std::string &config, std::function<void(ToolCall)> onToolCall, std::string &error) override {
|
|
76
|
+
// Freed by Swift once nothing there can call it.
|
|
77
|
+
auto *calls = new ToolCalls(std::move(onToolCall));
|
|
78
|
+
char reason[512];
|
|
79
|
+
void *handle = ngn_lm_session_create(
|
|
80
|
+
config.c_str(), calls,
|
|
81
|
+
[](void *context, const char *id, const char *tool, const char *arguments) {
|
|
82
|
+
(*static_cast<ToolCalls *>(context))({.id = id, .tool = tool, .arguments = arguments});
|
|
83
|
+
},
|
|
84
|
+
[](void *context) { delete static_cast<ToolCalls *>(context); }, reason, sizeof reason);
|
|
85
|
+
if (handle == nullptr) {
|
|
86
|
+
error = reason;
|
|
87
|
+
return nullptr;
|
|
88
|
+
}
|
|
89
|
+
return std::make_unique<SwiftSession>(handle);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
} // namespace
|
|
94
|
+
|
|
95
|
+
std::shared_ptr<LanguageModelEngine> makeLanguageModelEngine() {
|
|
96
|
+
return std::make_shared<SwiftEngine>();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
} // namespace ngn::appkit
|