@ng-native/desktop 0.0.0 → 0.23.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.
Files changed (393) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +52 -0
  3. package/app.plugin.cjs +17 -0
  4. package/bin/app-config.mjs +168 -0
  5. package/bin/autolink.mjs +224 -0
  6. package/bin/cli.mjs +158 -0
  7. package/bin/host.mjs +427 -0
  8. package/bin/ng-native-desktop.mjs +4 -0
  9. package/bin/package.mjs +971 -0
  10. package/bin/plist.mjs +68 -0
  11. package/bin/prebuilt.mjs +328 -0
  12. package/bin/toolchain.mjs +192 -0
  13. package/native/CMakeLists.txt +276 -0
  14. package/native/appkit/CMakeLists.txt +109 -0
  15. package/native/appkit/address-search.h +26 -0
  16. package/native/appkit/address-search.mm +277 -0
  17. package/native/appkit/animation-frames.h +14 -0
  18. package/native/appkit/animation-frames.mm +106 -0
  19. package/native/appkit/app-info.h +12 -0
  20. package/native/appkit/app-info.mm +98 -0
  21. package/native/appkit/app-updater.mm +128 -0
  22. package/native/appkit/app-windows.h +81 -0
  23. package/native/appkit/app-windows.mm +318 -0
  24. package/native/appkit/appkit-backend.h +73 -0
  25. package/native/appkit/appkit-backend.mm +276 -0
  26. package/native/appkit/apple-authentication.h +19 -0
  27. package/native/appkit/apple-authentication.mm +248 -0
  28. package/native/appkit/apple-button-view.h +10 -0
  29. package/native/appkit/apple-button-view.mm +37 -0
  30. package/native/appkit/apple-id-button.h +16 -0
  31. package/native/appkit/apple-id-button.mm +50 -0
  32. package/native/appkit/audio.h +16 -0
  33. package/native/appkit/audio.mm +275 -0
  34. package/native/appkit/battery.h +13 -0
  35. package/native/appkit/battery.mm +92 -0
  36. package/native/appkit/bookmarks.h +17 -0
  37. package/native/appkit/bookmarks.mm +145 -0
  38. package/native/appkit/calendar.h +25 -0
  39. package/native/appkit/calendar.mm +660 -0
  40. package/native/appkit/camera-view.h +15 -0
  41. package/native/appkit/camera-view.mm +130 -0
  42. package/native/appkit/camera.h +39 -0
  43. package/native/appkit/camera.mm +362 -0
  44. package/native/appkit/cg-canvas.h +17 -0
  45. package/native/appkit/cg-canvas.mm +518 -0
  46. package/native/appkit/component-view.h +17 -0
  47. package/native/appkit/crypto.h +19 -0
  48. package/native/appkit/crypto.mm +78 -0
  49. package/native/appkit/desktop-expo.h +29 -0
  50. package/native/appkit/desktop-expo.mm +385 -0
  51. package/native/appkit/expo-image-drawing.h +46 -0
  52. package/native/appkit/expo-image-drawing.mm +131 -0
  53. package/native/appkit/expo-image-view.h +15 -0
  54. package/native/appkit/expo-image-view.mm +182 -0
  55. package/native/appkit/expo-ui-renderer.h +68 -0
  56. package/native/appkit/expo-ui-renderer.mm +101 -0
  57. package/native/appkit/expo-ui-view.h +26 -0
  58. package/native/appkit/expo-ui-view.mm +134 -0
  59. package/native/appkit/expo-ui.swift +1284 -0
  60. package/native/appkit/file-system.h +16 -0
  61. package/native/appkit/file-system.mm +140 -0
  62. package/native/appkit/fonts.h +12 -0
  63. package/native/appkit/fonts.mm +48 -0
  64. package/native/appkit/geocoder.h +19 -0
  65. package/native/appkit/geocoder.mm +101 -0
  66. package/native/appkit/glass-effect.h +22 -0
  67. package/native/appkit/glass-effect.mm +63 -0
  68. package/native/appkit/glass-view.h +8 -0
  69. package/native/appkit/glass-view.mm +65 -0
  70. package/native/appkit/header-toolbar.h +60 -0
  71. package/native/appkit/header-toolbar.mm +629 -0
  72. package/native/appkit/host-view.h +54 -0
  73. package/native/appkit/host-view.mm +1200 -0
  74. package/native/appkit/image-manipulator.h +14 -0
  75. package/native/appkit/image-manipulator.mm +191 -0
  76. package/native/appkit/image-request.h +22 -0
  77. package/native/appkit/image-request.mm +62 -0
  78. package/native/appkit/image-view.h +8 -0
  79. package/native/appkit/image-view.mm +142 -0
  80. package/native/appkit/input-replay.h +25 -0
  81. package/native/appkit/input-replay.mm +418 -0
  82. package/native/appkit/language-model.h +14 -0
  83. package/native/appkit/language-model.mm +99 -0
  84. package/native/appkit/language-model.swift +472 -0
  85. package/native/appkit/local-authentication.h +15 -0
  86. package/native/appkit/local-authentication.mm +133 -0
  87. package/native/appkit/location.h +16 -0
  88. package/native/appkit/location.mm +214 -0
  89. package/native/appkit/main.mm +1515 -0
  90. package/native/appkit/map-view.h +15 -0
  91. package/native/appkit/map-view.mm +109 -0
  92. package/native/appkit/maps.h +35 -0
  93. package/native/appkit/maps.mm +343 -0
  94. package/native/appkit/menu-bar.h +22 -0
  95. package/native/appkit/menu-bar.mm +208 -0
  96. package/native/appkit/modal-focus.h +16 -0
  97. package/native/appkit/modal-focus.mm +26 -0
  98. package/native/appkit/modal-host-view.h +79 -0
  99. package/native/appkit/modal-host-view.mm +442 -0
  100. package/native/appkit/native-controls.h +22 -0
  101. package/native/appkit/native-controls.mm +431 -0
  102. package/native/appkit/network-state.h +12 -0
  103. package/native/appkit/network-state.mm +64 -0
  104. package/native/appkit/network.h +14 -0
  105. package/native/appkit/network.mm +396 -0
  106. package/native/appkit/notifications.h +19 -0
  107. package/native/appkit/notifications.mm +309 -0
  108. package/native/appkit/photo-library.h +33 -0
  109. package/native/appkit/photo-library.mm +548 -0
  110. package/native/appkit/pointer-buttons.h +32 -0
  111. package/native/appkit/presentation-label.h +15 -0
  112. package/native/appkit/presentation-label.mm +12 -0
  113. package/native/appkit/presented-screen.h +19 -0
  114. package/native/appkit/presented-screen.mm +181 -0
  115. package/native/appkit/printing.h +42 -0
  116. package/native/appkit/printing.mm +393 -0
  117. package/native/appkit/push.h +19 -0
  118. package/native/appkit/push.mm +81 -0
  119. package/native/appkit/red-box.h +26 -0
  120. package/native/appkit/red-box.mm +387 -0
  121. package/native/appkit/screen-capture.h +21 -0
  122. package/native/appkit/screen-capture.mm +92 -0
  123. package/native/appkit/scroll-view.h +13 -0
  124. package/native/appkit/scroll-view.mm +251 -0
  125. package/native/appkit/scrollers.h +25 -0
  126. package/native/appkit/scrollers.mm +43 -0
  127. package/native/appkit/search-field.h +34 -0
  128. package/native/appkit/search-field.mm +291 -0
  129. package/native/appkit/search-toolbar-item.h +19 -0
  130. package/native/appkit/search-toolbar-item.mm +269 -0
  131. package/native/appkit/share-picker.h +34 -0
  132. package/native/appkit/share-picker.mm +121 -0
  133. package/native/appkit/split-view.h +77 -0
  134. package/native/appkit/split-view.mm +1206 -0
  135. package/native/appkit/store-review.h +13 -0
  136. package/native/appkit/store-review.mm +28 -0
  137. package/native/appkit/surface-view.h +45 -0
  138. package/native/appkit/surface-view.mm +558 -0
  139. package/native/appkit/svg-view.h +17 -0
  140. package/native/appkit/svg-view.mm +57 -0
  141. package/native/appkit/symbol-drawing.h +24 -0
  142. package/native/appkit/symbol-drawing.mm +117 -0
  143. package/native/appkit/symbol-view.h +8 -0
  144. package/native/appkit/symbol-view.mm +33 -0
  145. package/native/appkit/system-access.h +34 -0
  146. package/native/appkit/system-access.mm +210 -0
  147. package/native/appkit/system.h +24 -0
  148. package/native/appkit/system.mm +403 -0
  149. package/native/appkit/test-launch.h +36 -0
  150. package/native/appkit/test-launch.mm +115 -0
  151. package/native/appkit/text-input-field.h +76 -0
  152. package/native/appkit/text-input-field.mm +444 -0
  153. package/native/appkit/text-layout.h +58 -0
  154. package/native/appkit/text-layout.mm +477 -0
  155. package/native/appkit/text-recognition.h +13 -0
  156. package/native/appkit/text-recognition.mm +102 -0
  157. package/native/appkit/tracking.h +13 -0
  158. package/native/appkit/tracking.mm +51 -0
  159. package/native/appkit/tray-and-dock.h +45 -0
  160. package/native/appkit/tray-and-dock.mm +130 -0
  161. package/native/appkit/undo.h +23 -0
  162. package/native/appkit/undo.mm +125 -0
  163. package/native/appkit/updates.h +17 -0
  164. package/native/appkit/updates.mm +71 -0
  165. package/native/appkit/video-view.h +10 -0
  166. package/native/appkit/video-view.mm +42 -0
  167. package/native/appkit/video.h +38 -0
  168. package/native/appkit/video.mm +404 -0
  169. package/native/appkit/web-browser.h +12 -0
  170. package/native/appkit/web-browser.mm +97 -0
  171. package/native/appkit/web-view-component.h +9 -0
  172. package/native/appkit/web-view-component.mm +56 -0
  173. package/native/appkit/web-view.h +31 -0
  174. package/native/appkit/web-view.mm +394 -0
  175. package/native/cmake/libraries/react-native-reanimated.cmake +45 -0
  176. package/native/cmake/libraries/react-native-worklets.cmake +46 -0
  177. package/native/cmake/libraries.cmake +109 -0
  178. package/native/cmake/linked-modules.cmake +112 -0
  179. package/native/cmake/react-native.cmake +175 -0
  180. package/native/cmake/runtime-dependencies.cmake +34 -0
  181. package/native/cmake/swift.cmake +43 -0
  182. package/native/cmake/third-party.cmake +74 -0
  183. package/native/core/include/ngn/absent-views.h +27 -0
  184. package/native/core/include/ngn/accessibility.h +140 -0
  185. package/native/core/include/ngn/address-search.h +69 -0
  186. package/native/core/include/ngn/animation-frames.h +39 -0
  187. package/native/core/include/ngn/app-info.h +52 -0
  188. package/native/core/include/ngn/app-menus.h +146 -0
  189. package/native/core/include/ngn/app-windows.h +181 -0
  190. package/native/core/include/ngn/apple-authentication.h +116 -0
  191. package/native/core/include/ngn/asset.h +28 -0
  192. package/native/core/include/ngn/audio.h +125 -0
  193. package/native/core/include/ngn/back-swipe.h +112 -0
  194. package/native/core/include/ngn/battery.h +48 -0
  195. package/native/core/include/ngn/bytes.h +59 -0
  196. package/native/core/include/ngn/calendar.h +180 -0
  197. package/native/core/include/ngn/camera.h +128 -0
  198. package/native/core/include/ngn/control-sizes.h +111 -0
  199. package/native/core/include/ngn/core-modules.h +105 -0
  200. package/native/core/include/ngn/crypto.h +38 -0
  201. package/native/core/include/ngn/default-menu.h +91 -0
  202. package/native/core/include/ngn/desktop-expo.h +193 -0
  203. package/native/core/include/ngn/desktop-modal.h +109 -0
  204. package/native/core/include/ngn/desktop-props.h +75 -0
  205. package/native/core/include/ngn/desktop-view-props.h +110 -0
  206. package/native/core/include/ngn/dev-server.h +33 -0
  207. package/native/core/include/ngn/download.h +24 -0
  208. package/native/core/include/ngn/expo-image.h +192 -0
  209. package/native/core/include/ngn/expo-ui.h +133 -0
  210. package/native/core/include/ngn/file-system.h +274 -0
  211. package/native/core/include/ngn/focus-order.h +63 -0
  212. package/native/core/include/ngn/fonts.h +64 -0
  213. package/native/core/include/ngn/gestures.h +305 -0
  214. package/native/core/include/ngn/glass.h +67 -0
  215. package/native/core/include/ngn/header-bar.h +112 -0
  216. package/native/core/include/ngn/host-state.h +243 -0
  217. package/native/core/include/ngn/image-manipulator.h +70 -0
  218. package/native/core/include/ngn/input-dispatcher.h +211 -0
  219. package/native/core/include/ngn/input-script.h +56 -0
  220. package/native/core/include/ngn/input.h +112 -0
  221. package/native/core/include/ngn/key-value-store.h +41 -0
  222. package/native/core/include/ngn/language-model.h +69 -0
  223. package/native/core/include/ngn/launch-frame.h +47 -0
  224. package/native/core/include/ngn/launch-settings.h +98 -0
  225. package/native/core/include/ngn/layout-animations.h +24 -0
  226. package/native/core/include/ngn/library-components.h +13 -0
  227. package/native/core/include/ngn/library-glue.h +53 -0
  228. package/native/core/include/ngn/live-regions.h +54 -0
  229. package/native/core/include/ngn/local-authentication.h +74 -0
  230. package/native/core/include/ngn/location.h +89 -0
  231. package/native/core/include/ngn/maps.h +226 -0
  232. package/native/core/include/ngn/module-registry.h +51 -0
  233. package/native/core/include/ngn/mounting-backend.h +96 -0
  234. package/native/core/include/ngn/mounting-manager.h +155 -0
  235. package/native/core/include/ngn/native-controls.h +161 -0
  236. package/native/core/include/ngn/native-module.h +109 -0
  237. package/native/core/include/ngn/network-state.h +44 -0
  238. package/native/core/include/ngn/network.h +100 -0
  239. package/native/core/include/ngn/notifications.h +185 -0
  240. package/native/core/include/ngn/photo-library.h +130 -0
  241. package/native/core/include/ngn/printing.h +69 -0
  242. package/native/core/include/ngn/processes.h +114 -0
  243. package/native/core/include/ngn/red-box.h +119 -0
  244. package/native/core/include/ngn/reload-thread.h +258 -0
  245. package/native/core/include/ngn/runtime.h +161 -0
  246. package/native/core/include/ngn/safe-areas.h +77 -0
  247. package/native/core/include/ngn/screen-capture.h +35 -0
  248. package/native/core/include/ngn/screen-presentation.h +85 -0
  249. package/native/core/include/ngn/screen-stacks.h +80 -0
  250. package/native/core/include/ngn/scrollers.h +106 -0
  251. package/native/core/include/ngn/sharing.h +39 -0
  252. package/native/core/include/ngn/spawn-rules.h +78 -0
  253. package/native/core/include/ngn/splash-screen.h +41 -0
  254. package/native/core/include/ngn/split-view.h +270 -0
  255. package/native/core/include/ngn/sqlite.h +38 -0
  256. package/native/core/include/ngn/store-review.h +26 -0
  257. package/native/core/include/ngn/surface-book.h +101 -0
  258. package/native/core/include/ngn/svg.h +393 -0
  259. package/native/core/include/ngn/symbol.h +90 -0
  260. package/native/core/include/ngn/system.h +102 -0
  261. package/native/core/include/ngn/tabs-host.h +171 -0
  262. package/native/core/include/ngn/text-input.h +125 -0
  263. package/native/core/include/ngn/text-measurer.h +38 -0
  264. package/native/core/include/ngn/text-recognition.h +55 -0
  265. package/native/core/include/ngn/tracking.h +34 -0
  266. package/native/core/include/ngn/tray-and-dock.h +107 -0
  267. package/native/core/include/ngn/undo.h +37 -0
  268. package/native/core/include/ngn/updates.h +253 -0
  269. package/native/core/include/ngn/video.h +126 -0
  270. package/native/core/include/ngn/view-box.h +126 -0
  271. package/native/core/include/ngn/view-transform.h +26 -0
  272. package/native/core/include/ngn/view-tree.h +85 -0
  273. package/native/core/include/ngn/web-browser.h +45 -0
  274. package/native/core/include/ngn/web-view.h +133 -0
  275. package/native/core/include/ngn/worklet-host.h +98 -0
  276. package/native/core/src/absent-views.cpp +41 -0
  277. package/native/core/src/accessibility.cpp +374 -0
  278. package/native/core/src/animation-frames.cpp +49 -0
  279. package/native/core/src/app-menus.cpp +352 -0
  280. package/native/core/src/app-windows.cpp +444 -0
  281. package/native/core/src/back-swipe.cpp +91 -0
  282. package/native/core/src/bytes.cpp +158 -0
  283. package/native/core/src/control-sizes.cpp +232 -0
  284. package/native/core/src/default-menu.cpp +96 -0
  285. package/native/core/src/desktop-modal.cpp +162 -0
  286. package/native/core/src/desktop-view-props.cpp +80 -0
  287. package/native/core/src/dev-server-helper.cpp +152 -0
  288. package/native/core/src/dev-server.cpp +103 -0
  289. package/native/core/src/download.cpp +53 -0
  290. package/native/core/src/expo-image.cpp +548 -0
  291. package/native/core/src/expo-ui.cpp +203 -0
  292. package/native/core/src/focus-order.cpp +441 -0
  293. package/native/core/src/gestures.cpp +1806 -0
  294. package/native/core/src/glass.cpp +49 -0
  295. package/native/core/src/header-bar.cpp +104 -0
  296. package/native/core/src/host-services.cpp +50 -0
  297. package/native/core/src/host-state.cpp +61 -0
  298. package/native/core/src/input-dispatcher.cpp +727 -0
  299. package/native/core/src/input-script.cpp +123 -0
  300. package/native/core/src/key-value-store.cpp +169 -0
  301. package/native/core/src/launch-settings.cpp +148 -0
  302. package/native/core/src/layout-animations.cpp +62 -0
  303. package/native/core/src/libraries/RNCWebViewSpec.cpp +80 -0
  304. package/native/core/src/libraries/react-native-reanimated.cpp +216 -0
  305. package/native/core/src/libraries/react-native-worklets.cpp +192 -0
  306. package/native/core/src/libraries/rnscreens.cpp +125 -0
  307. package/native/core/src/libraries/rnsvg.cpp +345 -0
  308. package/native/core/src/libraries/safeareacontext.cpp +70 -0
  309. package/native/core/src/libraries/worklets-idle-frames.js +70 -0
  310. package/native/core/src/library-glue.cpp +35 -0
  311. package/native/core/src/live-regions.cpp +122 -0
  312. package/native/core/src/module-registry.cpp +50 -0
  313. package/native/core/src/modules/accessibility-manager.cpp +98 -0
  314. package/native/core/src/modules/address-search.cpp +219 -0
  315. package/native/core/src/modules/alert-manager.cpp +140 -0
  316. package/native/core/src/modules/app-info.cpp +74 -0
  317. package/native/core/src/modules/app-modules.cpp +405 -0
  318. package/native/core/src/modules/apple-authentication.cpp +188 -0
  319. package/native/core/src/modules/asset.cpp +111 -0
  320. package/native/core/src/modules/async-storage.cpp +192 -0
  321. package/native/core/src/modules/audio.cpp +541 -0
  322. package/native/core/src/modules/battery.cpp +88 -0
  323. package/native/core/src/modules/calendar.cpp +457 -0
  324. package/native/core/src/modules/camera.cpp +221 -0
  325. package/native/core/src/modules/core-modules.cpp +62 -0
  326. package/native/core/src/modules/crypto.cpp +117 -0
  327. package/native/core/src/modules/desktop-expo.cpp +527 -0
  328. package/native/core/src/modules/file-system.cpp +941 -0
  329. package/native/core/src/modules/fonts.cpp +163 -0
  330. package/native/core/src/modules/image-loader.cpp +105 -0
  331. package/native/core/src/modules/image-manipulator.cpp +305 -0
  332. package/native/core/src/modules/language-model.cpp +195 -0
  333. package/native/core/src/modules/local-authentication.cpp +140 -0
  334. package/native/core/src/modules/location.cpp +186 -0
  335. package/native/core/src/modules/maps.cpp +500 -0
  336. package/native/core/src/modules/network-state.cpp +72 -0
  337. package/native/core/src/modules/networking.cpp +387 -0
  338. package/native/core/src/modules/notifications.cpp +549 -0
  339. package/native/core/src/modules/photo-library.cpp +371 -0
  340. package/native/core/src/modules/printing.cpp +201 -0
  341. package/native/core/src/modules/screen-capture.cpp +71 -0
  342. package/native/core/src/modules/sharing.cpp +59 -0
  343. package/native/core/src/modules/splash-screen.cpp +70 -0
  344. package/native/core/src/modules/sqlite.cpp +421 -0
  345. package/native/core/src/modules/store-review.cpp +23 -0
  346. package/native/core/src/modules/text-recognition.cpp +107 -0
  347. package/native/core/src/modules/tracking.cpp +46 -0
  348. package/native/core/src/modules/undo.cpp +59 -0
  349. package/native/core/src/modules/video.cpp +366 -0
  350. package/native/core/src/modules/web-browser.cpp +100 -0
  351. package/native/core/src/modules/web-socket.cpp +266 -0
  352. package/native/core/src/mounting-manager.cpp +380 -0
  353. package/native/core/src/native-controls.cpp +129 -0
  354. package/native/core/src/native-module.cpp +250 -0
  355. package/native/core/src/network.cpp +219 -0
  356. package/native/core/src/posix-launcher.cpp +366 -0
  357. package/native/core/src/processes.cpp +324 -0
  358. package/native/core/src/red-box-errors.js +83 -0
  359. package/native/core/src/red-box.cpp +306 -0
  360. package/native/core/src/runtime.cpp +422 -0
  361. package/native/core/src/safe-areas.cpp +130 -0
  362. package/native/core/src/screen-presentation.cpp +61 -0
  363. package/native/core/src/screen-stacks.cpp +254 -0
  364. package/native/core/src/scrollers.cpp +181 -0
  365. package/native/core/src/spawn-rules.cpp +292 -0
  366. package/native/core/src/split-view.cpp +343 -0
  367. package/native/core/src/svg.cpp +1883 -0
  368. package/native/core/src/symbol.cpp +94 -0
  369. package/native/core/src/tabs-host.cpp +234 -0
  370. package/native/core/src/text-input.cpp +126 -0
  371. package/native/core/src/text-layout-manager.cpp +63 -0
  372. package/native/core/src/tray-and-dock.cpp +154 -0
  373. package/native/core/src/updates.cpp +965 -0
  374. package/native/core/src/view-box.cpp +788 -0
  375. package/native/core/src/view-transform.cpp +45 -0
  376. package/native/core/src/view-tree.cpp +234 -0
  377. package/native/core/src/web-view.cpp +143 -0
  378. package/native/core/src/worklet-host.cpp +10 -0
  379. package/native/scripts/build-hermes.sh +44 -0
  380. package/native/scripts/build.sh +171 -0
  381. package/native/scripts/cache-dir.sh +18 -0
  382. package/native/scripts/codegen-library.sh +193 -0
  383. package/native/scripts/compiler-launcher.sh +17 -0
  384. package/native/scripts/fetch.sh +174 -0
  385. package/native/scripts/hermes-built.sh +10 -0
  386. package/native/scripts/host-dir.sh +16 -0
  387. package/native/scripts/macos-target.sh +8 -0
  388. package/native/scripts/make-prebuilt.mjs +360 -0
  389. package/native/scripts/prebuilt-host-cache.mjs +158 -0
  390. package/native/shim/openssl/sha.h +22 -0
  391. package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +37 -0
  392. package/package.json +73 -1
  393. package/prebuilt.json +33 -0
@@ -0,0 +1,541 @@
1
+ #include "ngn/audio.h"
2
+
3
+ #include <algorithm>
4
+ #include <cstdio>
5
+ #include <map>
6
+ #include <mutex>
7
+ #include <random>
8
+ #include <sys/stat.h>
9
+
10
+ namespace ngn {
11
+
12
+ namespace {
13
+
14
+ using Access = AudioInput::Access;
15
+
16
+ double clampTo(double value, double low, double high) {
17
+ return std::clamp(value, low, high);
18
+ }
19
+
20
+ std::optional<std::string> stringArg(const folly::dynamic &args, size_t at) {
21
+ return args.size() > at && args[at].isString() ? std::optional(args[at].asString()) : std::nullopt;
22
+ }
23
+
24
+ std::optional<double> numberArg(const folly::dynamic &args, size_t at) {
25
+ return args.size() > at && args[at].isNumber() ? std::optional(args[at].asDouble()) : std::nullopt;
26
+ }
27
+
28
+ bool flagArg(const folly::dynamic &args, size_t at) {
29
+ return args.size() > at && args[at].isBool() && args[at].asBool();
30
+ }
31
+
32
+ std::string randomName() {
33
+ static std::mutex mutex;
34
+ static std::mt19937_64 engine{std::random_device{}()};
35
+ std::lock_guard lock(mutex);
36
+ char out[40];
37
+ auto a = engine();
38
+ auto b = engine();
39
+ std::snprintf(out, sizeof out, "%08llX-%04llX-%04llX-%04llX-%012llX", static_cast<unsigned long long>(a >> 32),
40
+ static_cast<unsigned long long>((a >> 16) & 0xffff), static_cast<unsigned long long>(a & 0xffff),
41
+ static_cast<unsigned long long>(b >> 48), static_cast<unsigned long long>(b & 0xffffffffffffULL));
42
+ return out;
43
+ }
44
+
45
+ folly::dynamic permissionOf(Access access) {
46
+ bool granted = access == Access::Granted;
47
+ bool askable = access == Access::NotDetermined;
48
+ return folly::dynamic::object("status", granted ? "granted" : askable ? "undetermined" : "denied")("granted", granted)(
49
+ "canAskAgain", askable)("expires", "never");
50
+ }
51
+
52
+ struct Player {
53
+ std::unique_ptr<AudioTrack> track;
54
+ // Each source opened counts, so a change a replaced track sends late is dropped.
55
+ int generation{0};
56
+ double interval{0.5};
57
+ double volume{1};
58
+ bool muted{false};
59
+ bool loop{false};
60
+ double rate{1};
61
+ bool finished{false};
62
+ bool justFinished{false};
63
+ };
64
+
65
+ struct Recorder {
66
+ RecordingSettings settings;
67
+ std::unique_ptr<AudioRecording> recording;
68
+ std::string path;
69
+ };
70
+
71
+ struct Shared {
72
+ std::recursive_mutex mutex;
73
+ std::map<std::string, Player> players;
74
+ std::map<std::string, Recorder> recorders;
75
+ int next{0};
76
+ std::function<void(folly::dynamic)> send;
77
+ };
78
+
79
+ folly::dynamic statusOf(const std::string &id, Player &player) {
80
+ TrackState state = player.track ? player.track->state() : TrackState{};
81
+ bool loaded = player.track && state.load == TrackState::Load::Ready;
82
+ bool failed = player.track && state.load == TrackState::Load::Failed;
83
+ const char *playbackState = !player.track ? "idle" : failed ? "failed" : loaded ? "readyToPlay" : "loading";
84
+ // A source that failed is not playing, whatever the output last said about being asked to.
85
+ bool playing = state.playing && !failed;
86
+ const char *timeControl = playing ? (state.buffering ? "waitingToPlayAtSpecifiedRate" : "playing") : "paused";
87
+ return folly::dynamic::object("id", id)("currentTime", state.currentTime)("playbackState", playbackState)(
88
+ "timeControlStatus", timeControl)("reasonForWaitingToPlay", "")("mute", player.muted)("duration", state.duration)(
89
+ "playing", playing)("loop", player.loop)("didJustFinish", player.justFinished)("isBuffering", state.buffering)(
90
+ "isLoaded", loaded)("playbackRate", player.rate)("shouldCorrectPitch", true)("mediaServicesDidReset", false)(
91
+ "isLive", false)("currentOffsetFromLive", nullptr)("error", failed ? folly::dynamic(state.error) : folly::dynamic(nullptr));
92
+ }
93
+
94
+ void sendStatus(Shared &shared, const std::string &id, Player &player) {
95
+ if (shared.send) {
96
+ shared.send(folly::dynamic::object("type", "playbackStatusUpdate")("id", id)("status", statusOf(id, player)));
97
+ }
98
+ }
99
+
100
+ void applyVolume(Player &player) {
101
+ if (player.track) {
102
+ player.track->setVolume(player.muted ? 0 : player.volume);
103
+ }
104
+ }
105
+
106
+ void open(const std::shared_ptr<Shared> &shared, AudioOutput &output, const std::string &id, Player &player,
107
+ const std::optional<std::string> &uri) {
108
+ player.track.reset();
109
+ player.finished = false;
110
+ player.justFinished = false;
111
+ int generation = ++player.generation;
112
+ if (!uri) {
113
+ return;
114
+ }
115
+ std::weak_ptr<Shared> weak = shared;
116
+ player.track = output.open(*uri, player.interval, [weak, id, generation](AudioOutput::Change change) {
117
+ auto alive = weak.lock();
118
+ if (!alive) {
119
+ return;
120
+ }
121
+ std::lock_guard lock(alive->mutex);
122
+ auto found = alive->players.find(id);
123
+ if (found == alive->players.end() || found->second.generation != generation || !found->second.track) {
124
+ return;
125
+ }
126
+ auto &player = found->second;
127
+ if (change == AudioOutput::Change::Ended) {
128
+ player.justFinished = true;
129
+ if (player.loop) {
130
+ player.track->seek(0, [] {});
131
+ player.track->play(player.rate);
132
+ } else {
133
+ // Stopped now, so the status sent says so: the OS's own pause at the end comes later.
134
+ player.track->pause();
135
+ player.finished = true;
136
+ }
137
+ sendStatus(*alive, id, player);
138
+ return;
139
+ }
140
+ player.justFinished = false;
141
+ sendStatus(*alive, id, player);
142
+ });
143
+ applyVolume(player);
144
+ }
145
+
146
+ double fileSize(const std::string &path) {
147
+ struct stat info{};
148
+ return ::stat(path.c_str(), &info) == 0 ? static_cast<double>(info.st_size) : 0;
149
+ }
150
+
151
+ folly::dynamic recorderStatusOf(Recorder &recorder) {
152
+ auto &recording = recorder.recording;
153
+ folly::dynamic status = folly::dynamic::object("canRecord", recording != nullptr)("isRecording", recording && recording->recording())(
154
+ "durationMillis", recording ? recording->currentTime() * 1000 : 0.0)("fileSize", recording ? fileSize(recorder.path) : 0.0)(
155
+ "mediaServicesDidReset", false)("url", recording ? folly::dynamic(fileUri(recorder.path)) : folly::dynamic(nullptr));
156
+ if (recording && recorder.settings.metering) {
157
+ if (auto level = recording->metering()) {
158
+ status["metering"] = *level;
159
+ }
160
+ }
161
+ return status;
162
+ }
163
+
164
+ } // namespace
165
+
166
+ RecordingSettings recordingSettingsOf(const folly::dynamic &options) {
167
+ RecordingSettings settings;
168
+ if (!options.isObject()) {
169
+ return settings;
170
+ }
171
+ auto text = [&](const char *key) {
172
+ auto found = options.find(key);
173
+ return found != options.items().end() && found->second.isString() ? std::optional(found->second.asString()) : std::nullopt;
174
+ };
175
+ auto number = [&](const char *key) {
176
+ auto found = options.find(key);
177
+ return found != options.items().end() && found->second.isNumber() ? std::optional(found->second.asDouble()) : std::nullopt;
178
+ };
179
+ if (auto extension = text("extension")) {
180
+ settings.extension = extension->empty() || (*extension)[0] == '.' ? *extension : "." + *extension;
181
+ }
182
+ const std::string &ext = settings.extension;
183
+ if (ext == ".wav" || ext == ".caf" || ext == ".aif" || ext == ".aiff") {
184
+ settings.format = "pcm";
185
+ }
186
+ // expo-audio's IOSOutputFormat is the CoreAudio format's four characters, or its number.
187
+ std::string format;
188
+ if (auto code = text("outputFormat")) {
189
+ format = *code;
190
+ } else if (auto id = number("outputFormat")) {
191
+ auto value = static_cast<uint32_t>(*id);
192
+ format = {static_cast<char>(value >> 24), static_cast<char>((value >> 16) & 0xff), static_cast<char>((value >> 8) & 0xff),
193
+ static_cast<char>(value & 0xff)};
194
+ }
195
+ while (!format.empty() && format.back() == ' ') {
196
+ format.pop_back();
197
+ }
198
+ if (format == "lpcm") {
199
+ settings.format = "pcm";
200
+ } else if (format == "aac" || format == "aach" || format == "aacl") {
201
+ settings.format = "aac";
202
+ } else if (format == "alac") {
203
+ settings.format = "alac";
204
+ }
205
+ if (auto rate = number("sampleRate")) {
206
+ settings.sampleRate = *rate;
207
+ }
208
+ if (auto channels = number("numberOfChannels")) {
209
+ settings.channels = static_cast<int>(*channels);
210
+ }
211
+ if (auto bitRate = number("bitRate")) {
212
+ settings.bitRate = static_cast<int>(*bitRate);
213
+ }
214
+ if (auto quality = number("audioQuality")) {
215
+ settings.quality = static_cast<int>(clampTo(*quality, 0, 127));
216
+ }
217
+ if (auto depth = number("linearPCMBitDepth")) {
218
+ settings.bitDepth = static_cast<int>(*depth);
219
+ }
220
+ auto metering = options.find("isMeteringEnabled");
221
+ settings.metering = metering != options.items().end() && metering->second.isBool() && metering->second.asBool();
222
+ return settings;
223
+ }
224
+
225
+ namespace {
226
+
227
+ // A source outside the file boundary: failed with the refusal, and never opened.
228
+ class RefusedTrack final : public AudioTrack {
229
+ public:
230
+ explicit RefusedTrack(std::string error) : error_(std::move(error)) {}
231
+ TrackState state() override {
232
+ return {.load = TrackState::Load::Failed, .error = error_};
233
+ }
234
+ void play(double) override {}
235
+ void pause() override {}
236
+ void seek(double, std::function<void()> done) override {
237
+ done();
238
+ }
239
+ void setRate(double) override {}
240
+ void setVolume(double) override {}
241
+
242
+ private:
243
+ std::string error_;
244
+ };
245
+
246
+ // A track, and the pick holding its file open for as long as it plays.
247
+ class HeldTrack final : public AudioTrack {
248
+ public:
249
+ HeldTrack(std::unique_ptr<AudioTrack> track, std::shared_ptr<FileAccess::Use> use) : use_(std::move(use)), track_(std::move(track)) {}
250
+ TrackState state() override {
251
+ return track_->state();
252
+ }
253
+ void play(double rate) override {
254
+ track_->play(rate);
255
+ }
256
+ void pause() override {
257
+ track_->pause();
258
+ }
259
+ void seek(double seconds, std::function<void()> done) override {
260
+ track_->seek(seconds, std::move(done));
261
+ }
262
+ void setRate(double rate) override {
263
+ track_->setRate(rate);
264
+ }
265
+ void setVolume(double volume) override {
266
+ track_->setVolume(volume);
267
+ }
268
+
269
+ private:
270
+ // Declared first, so it goes last: the track closes its file before the pick lets it go.
271
+ std::shared_ptr<FileAccess::Use> use_;
272
+ std::unique_ptr<AudioTrack> track_;
273
+ };
274
+
275
+ // The output, opening a file source only inside the file boundary, exactly the file checked.
276
+ class BoundedOutput final : public AudioOutput {
277
+ public:
278
+ BoundedOutput(std::shared_ptr<AudioOutput> output, std::shared_ptr<FileAccess> files)
279
+ : output_(std::move(output)), files_(std::move(files)) {}
280
+ std::unique_ptr<AudioTrack> open(const std::string &uri, double interval, std::function<void(Change)> changed) override {
281
+ ReadableSource source;
282
+ try {
283
+ source = readableSource(files_.get(), uri);
284
+ } catch (const FileRefused &refused) {
285
+ return std::make_unique<RefusedTrack>(refused.what());
286
+ }
287
+ auto track = output_->open(source.uri, interval, std::move(changed));
288
+ if (!source.use || !track) {
289
+ return track;
290
+ }
291
+ return std::make_unique<HeldTrack>(std::move(track), std::move(source.use));
292
+ }
293
+
294
+ private:
295
+ std::shared_ptr<AudioOutput> output_;
296
+ std::shared_ptr<FileAccess> files_;
297
+ };
298
+
299
+ } // namespace
300
+
301
+ std::shared_ptr<NativeModule> makeAudioModule(std::shared_ptr<AudioOutput> output, std::shared_ptr<AudioInput> input,
302
+ std::string recordings, bool noPrompts, std::shared_ptr<FileAccess> files) {
303
+ struct Audio : NativeModule {
304
+ std::shared_ptr<Shared> shared = std::make_shared<Shared>();
305
+
306
+ Audio(std::shared_ptr<AudioOutput> output, std::shared_ptr<AudioInput> input, std::string recordings, bool noPrompts)
307
+ : NativeModule("NgNativeAudio") {
308
+ listeners();
309
+ shared->send = [this](folly::dynamic body) { emit("ngNativeAudio", std::move(body)); };
310
+ auto state = shared;
311
+ // Each method on a player: the player, under the lock, or nothing for an id not open.
312
+ auto withPlayer = [state](const folly::dynamic &args, auto fn) {
313
+ auto id = stringArg(args, 0);
314
+ std::lock_guard lock(state->mutex);
315
+ auto found = id ? state->players.find(*id) : state->players.end();
316
+ if (found != state->players.end()) {
317
+ fn(found->first, found->second);
318
+ }
319
+ };
320
+
321
+ sync("createPlayer", [state, output](const folly::dynamic &args) {
322
+ std::lock_guard lock(state->mutex);
323
+ auto id = std::to_string(++state->next);
324
+ auto &player = state->players[id];
325
+ if (auto ms = numberArg(args, 1); ms && *ms > 0) {
326
+ player.interval = *ms / 1000;
327
+ }
328
+ open(state, *output, id, player, stringArg(args, 0));
329
+ return folly::dynamic(id);
330
+ });
331
+ sync("replace", [state, output, withPlayer](const folly::dynamic &args) {
332
+ withPlayer(args, [&](const std::string &id, Player &player) { open(state, *output, id, player, stringArg(args, 1)); });
333
+ return folly::dynamic(nullptr);
334
+ });
335
+ sync("release", [state](const folly::dynamic &args) {
336
+ auto id = stringArg(args, 0);
337
+ std::lock_guard lock(state->mutex);
338
+ if (id) {
339
+ state->players.erase(*id);
340
+ }
341
+ return folly::dynamic(nullptr);
342
+ });
343
+ sync("play", [state, withPlayer](const folly::dynamic &args) {
344
+ withPlayer(args, [&](const std::string &id, Player &player) {
345
+ if (!player.track) {
346
+ return;
347
+ }
348
+ // A player at its end starts again from the beginning, as AVPlayer would not.
349
+ if (player.finished) {
350
+ player.track->seek(0, [] {});
351
+ player.finished = false;
352
+ }
353
+ player.justFinished = false;
354
+ player.track->play(player.rate);
355
+ sendStatus(*state, id, player);
356
+ });
357
+ return folly::dynamic(nullptr);
358
+ });
359
+ sync("pause", [state, withPlayer](const folly::dynamic &args) {
360
+ withPlayer(args, [&](const std::string &id, Player &player) {
361
+ if (player.track) {
362
+ player.track->pause();
363
+ sendStatus(*state, id, player);
364
+ }
365
+ });
366
+ return folly::dynamic(nullptr);
367
+ });
368
+ promise("seekTo", [state, withPlayer](const folly::dynamic &args, JsPromise promise) {
369
+ bool started = false;
370
+ withPlayer(args, [&](const std::string &id, Player &player) {
371
+ if (!player.track) {
372
+ return;
373
+ }
374
+ started = true;
375
+ player.finished = false;
376
+ std::weak_ptr<Shared> weak = state;
377
+ player.track->seek(std::max(0.0, numberArg(args, 1).value_or(0)), [weak, id, promise] {
378
+ if (auto alive = weak.lock()) {
379
+ std::lock_guard lock(alive->mutex);
380
+ if (auto found = alive->players.find(id); found != alive->players.end()) {
381
+ sendStatus(*alive, id, found->second);
382
+ }
383
+ }
384
+ promise.resolve(nullptr);
385
+ });
386
+ });
387
+ if (!started) {
388
+ promise.resolve(nullptr);
389
+ }
390
+ });
391
+ sync("setPlaybackRate", [withPlayer](const folly::dynamic &args) {
392
+ withPlayer(args, [&](const std::string &, Player &player) {
393
+ player.rate = clampTo(numberArg(args, 1).value_or(1), 0, 2);
394
+ if (player.track && player.track->state().playing) {
395
+ player.track->setRate(player.rate);
396
+ }
397
+ });
398
+ return folly::dynamic(nullptr);
399
+ });
400
+ sync("setVolume", [withPlayer](const folly::dynamic &args) {
401
+ withPlayer(args, [&](const std::string &, Player &player) {
402
+ player.volume = clampTo(numberArg(args, 1).value_or(1), 0, 1);
403
+ applyVolume(player);
404
+ });
405
+ return folly::dynamic(nullptr);
406
+ });
407
+ sync("setMuted", [withPlayer](const folly::dynamic &args) {
408
+ withPlayer(args, [&](const std::string &, Player &player) {
409
+ player.muted = flagArg(args, 1);
410
+ applyVolume(player);
411
+ });
412
+ return folly::dynamic(nullptr);
413
+ });
414
+ sync("setLoop", [withPlayer](const folly::dynamic &args) {
415
+ withPlayer(args, [&](const std::string &, Player &player) { player.loop = flagArg(args, 1); });
416
+ return folly::dynamic(nullptr);
417
+ });
418
+ sync("status", [withPlayer](const folly::dynamic &args) {
419
+ folly::dynamic out = nullptr;
420
+ withPlayer(args, [&](const std::string &id, Player &player) { out = statusOf(id, player); });
421
+ return out;
422
+ });
423
+
424
+ promise("getRecordingPermissionsAsync", [input](const folly::dynamic &, JsPromise promise) {
425
+ promise.resolve(permissionOf(input->access()));
426
+ });
427
+ promise("requestRecordingPermissionsAsync", [input, noPrompts](const folly::dynamic &, JsPromise promise) {
428
+ auto access = input->access();
429
+ if (noPrompts || access != Access::NotDetermined) {
430
+ promise.resolve(permissionOf(access));
431
+ return;
432
+ }
433
+ input->requestAccess([promise](Access decided) { promise.resolve(permissionOf(decided)); });
434
+ });
435
+ // macOS has no audio session: no category, no silent switch, nothing to activate.
436
+ promise("setAudioModeAsync", [](const folly::dynamic &, JsPromise promise) { promise.resolve(nullptr); });
437
+ promise("setIsAudioActiveAsync", [](const folly::dynamic &, JsPromise promise) { promise.resolve(nullptr); });
438
+
439
+ auto withRecorder = [state](const folly::dynamic &args, auto fn) {
440
+ auto id = stringArg(args, 0);
441
+ std::lock_guard lock(state->mutex);
442
+ auto found = id ? state->recorders.find(*id) : state->recorders.end();
443
+ if (found != state->recorders.end()) {
444
+ fn(found->first, found->second);
445
+ }
446
+ };
447
+ sync("createRecorder", [state](const folly::dynamic &args) {
448
+ std::lock_guard lock(state->mutex);
449
+ auto id = std::to_string(++state->next);
450
+ state->recorders[id].settings = recordingSettingsOf(args.empty() ? folly::dynamic(nullptr) : args[0]);
451
+ return folly::dynamic(id);
452
+ });
453
+ promise("prepareToRecordAsync", [withRecorder, input, recordings](const folly::dynamic &args, JsPromise promise) {
454
+ std::optional<std::string> failure = "AudioRecorder.prepareToRecordAsync: this recorder was released";
455
+ withRecorder(args, [&](const std::string &, Recorder &recorder) {
456
+ if (input->access() != Access::Granted) {
457
+ failure = "AudioRecorder.prepareToRecordAsync: recording permission has not been granted";
458
+ return;
459
+ }
460
+ if (!input->hasMicrophone()) {
461
+ failure = "AudioRecorder.prepareToRecordAsync: this Mac has no microphone";
462
+ return;
463
+ }
464
+ if (args.size() > 1 && args[1].isObject()) {
465
+ // The prepare's own options over expo-audio's defaults, as on iOS: not over the recorder's.
466
+ recorder.settings = recordingSettingsOf(args[1]);
467
+ }
468
+ recorder.recording.reset();
469
+ auto path = recordings + "/recording-" + randomName() + recorder.settings.extension;
470
+ std::string error;
471
+ recorder.recording = input->prepare(path, recorder.settings, error);
472
+ if (!recorder.recording) {
473
+ failure = "AudioRecorder.prepareToRecordAsync: " + (error.empty() ? std::string("the recorder did not start") : error);
474
+ return;
475
+ }
476
+ recorder.path = path;
477
+ failure.reset();
478
+ });
479
+ if (failure) {
480
+ promise.reject(*failure);
481
+ } else {
482
+ promise.resolve(nullptr);
483
+ }
484
+ });
485
+ sync("record", [withRecorder](const folly::dynamic &args) {
486
+ withRecorder(args, [](const std::string &, Recorder &recorder) {
487
+ if (recorder.recording) {
488
+ recorder.recording->record();
489
+ }
490
+ });
491
+ return folly::dynamic(nullptr);
492
+ });
493
+ sync("pauseRecording", [withRecorder](const folly::dynamic &args) {
494
+ withRecorder(args, [](const std::string &, Recorder &recorder) {
495
+ if (recorder.recording) {
496
+ recorder.recording->pause();
497
+ }
498
+ });
499
+ return folly::dynamic(nullptr);
500
+ });
501
+ promise("stopRecording", [state, withRecorder](const folly::dynamic &args, JsPromise promise) {
502
+ withRecorder(args, [&](const std::string &id, Recorder &recorder) {
503
+ if (!recorder.recording) {
504
+ return;
505
+ }
506
+ recorder.recording->stop();
507
+ if (state->send) {
508
+ state->send(folly::dynamic::object("type", "recordingStatusUpdate")("id", id)(
509
+ "status", folly::dynamic::object("id", id)("isFinished", true)("hasError", false)("error", nullptr)(
510
+ "url", fileUri(recorder.path))));
511
+ }
512
+ });
513
+ promise.resolve(nullptr);
514
+ });
515
+ sync("recorderStatus", [withRecorder](const folly::dynamic &args) {
516
+ folly::dynamic out = nullptr;
517
+ withRecorder(args, [&](const std::string &, Recorder &recorder) { out = recorderStatusOf(recorder); });
518
+ return out;
519
+ });
520
+ sync("releaseRecorder", [state](const folly::dynamic &args) {
521
+ auto id = stringArg(args, 0);
522
+ std::lock_guard lock(state->mutex);
523
+ if (id) {
524
+ state->recorders.erase(*id);
525
+ }
526
+ return folly::dynamic(nullptr);
527
+ });
528
+ }
529
+
530
+ ~Audio() override {
531
+ std::lock_guard lock(shared->mutex);
532
+ shared->send = nullptr;
533
+ shared->players.clear();
534
+ shared->recorders.clear();
535
+ }
536
+ };
537
+ return std::make_shared<Audio>(std::make_shared<BoundedOutput>(std::move(output), std::move(files)), std::move(input),
538
+ std::move(recordings), noPrompts);
539
+ }
540
+
541
+ } // namespace ngn
@@ -0,0 +1,88 @@
1
+ #include "ngn/battery.h"
2
+
3
+ #include <algorithm>
4
+ #include <cmath>
5
+ #include <mutex>
6
+
7
+ namespace ngn {
8
+
9
+ namespace {
10
+
11
+ // expo-battery's BatteryState.
12
+ enum BatteryState { Unknown = 0, Unplugged = 1, Charging = 2, Full = 3, NotCharging = 4 };
13
+
14
+ } // namespace
15
+
16
+ double batteryLevelOf(const PowerFacts &facts) {
17
+ if (!facts.hasBattery) {
18
+ return -1;
19
+ }
20
+ return std::clamp(facts.level, 0.0, 1.0);
21
+ }
22
+
23
+ int batteryStateOf(const PowerFacts &facts) {
24
+ if (!facts.hasBattery) {
25
+ return Unknown;
26
+ }
27
+ if (facts.charging) {
28
+ return Charging;
29
+ }
30
+ if (!facts.externalPower) {
31
+ return Unplugged;
32
+ }
33
+ return facts.charged || facts.level >= 1 ? Full : NotCharging;
34
+ }
35
+
36
+ std::shared_ptr<NativeModule> makeBatteryModule(std::shared_ptr<PowerSource> source) {
37
+ struct Battery : NativeModule {
38
+ std::shared_ptr<PowerSource> source;
39
+ std::mutex mutex;
40
+ PowerFacts last;
41
+ std::function<void()> stop;
42
+
43
+ explicit Battery(std::shared_ptr<PowerSource> from) : NativeModule("NgNativeBattery"), source(std::move(from)) {
44
+ listeners();
45
+ auto power = source;
46
+ promise("getBatteryLevelAsync", [power](const folly::dynamic &, JsPromise promise) {
47
+ promise.resolve(batteryLevelOf(power->facts()));
48
+ });
49
+ promise("getBatteryStateAsync", [power](const folly::dynamic &, JsPromise promise) {
50
+ promise.resolve(batteryStateOf(power->facts()));
51
+ });
52
+ promise("isLowPowerModeEnabledAsync", [power](const folly::dynamic &, JsPromise promise) {
53
+ promise.resolve(power->facts().lowPowerMode);
54
+ });
55
+ promise("isAvailableAsync", [power](const folly::dynamic &, JsPromise promise) {
56
+ promise.resolve(power->facts().hasBattery);
57
+ });
58
+ last = source->facts();
59
+ stop = source->watch([this] { changed(); });
60
+ }
61
+
62
+ ~Battery() override {
63
+ if (stop) {
64
+ stop();
65
+ }
66
+ }
67
+
68
+ // One change at a time, read and sent in order: the source may call from any thread.
69
+ void changed() {
70
+ std::lock_guard lock(mutex);
71
+ auto now = source->facts();
72
+ PowerFacts before = last;
73
+ last = now;
74
+ if (batteryLevelOf(now) != batteryLevelOf(before)) {
75
+ emit("ngNativeBattery", folly::dynamic::object("type", "level")("batteryLevel", batteryLevelOf(now)));
76
+ }
77
+ if (batteryStateOf(now) != batteryStateOf(before)) {
78
+ emit("ngNativeBattery", folly::dynamic::object("type", "state")("batteryState", batteryStateOf(now)));
79
+ }
80
+ if (now.lowPowerMode != before.lowPowerMode) {
81
+ emit("ngNativeBattery", folly::dynamic::object("type", "lowPowerMode")("lowPowerMode", now.lowPowerMode));
82
+ }
83
+ }
84
+ };
85
+ return std::make_shared<Battery>(std::move(source));
86
+ }
87
+
88
+ } // namespace ngn