From b7b7c6210e74d34ca747b4ddadf0075a6b99964b Mon Sep 17 00:00:00 2001 From: Pierre Ferran Date: Tue, 7 May 2024 23:31:18 -0400 Subject: [PATCH] More work on replacement of ptt system --- backend/js/bindings.js | 1 + backend/src/InputHandler.cpp | 30 +++++++------ backend/src/main.cpp | 14 ++++++ backend/types/index.d.ts | 10 ++++- package-lock.json | 25 +++++------ package.json | 2 +- src/app/components/bootstrap.tsx | 13 +++--- .../settings-modal/settings-modal.tsx | 19 +++----- src/app/store/sessionStore.ts | 5 --- src/app/store/utilStore.ts | 6 +++ src/config.d.ts | 3 -- src/index.ts | 45 ++++--------------- 12 files changed, 81 insertions(+), 92 deletions(-) diff --git a/backend/js/bindings.js b/backend/js/bindings.js index 6e4890d..6e79655 100644 --- a/backend/js/bindings.js +++ b/backend/js/bindings.js @@ -12,4 +12,5 @@ exports.AfvEventTypes = { VuMeter: "VuMeter", NetworkConnected: "network-connected", NetworkDisconnected: "network-disconnected", + PttKeySet: "UpdatePttKeyName", }; \ No newline at end of file diff --git a/backend/src/InputHandler.cpp b/backend/src/InputHandler.cpp index 9e7424d..845bb15 100644 --- a/backend/src/InputHandler.cpp +++ b/backend/src/InputHandler.cpp @@ -34,14 +34,14 @@ void InputHandler::updatePttKey(int key, bool isJoystickButton, int joystickId) UserSettings::isJoystickButton = isJoystickButton; UserSettings::JoystickId = joystickId; - forwardPttKeyName(); + InputHandler::forwardPttKeyName(); } // NOLINTNEXTLINE void InputHandler::onTimer(Poco::Timer& /*timer*/) { sf::Joystick::update(); - + std::lock_guard lock(m); if (isPttSetupRunning) { @@ -52,7 +52,7 @@ void InputHandler::onTimer(Poco::Timer& /*timer*/) updatePttKey(i, false); isPttSetupRunning = false; - break; + return; } } @@ -65,13 +65,9 @@ void InputHandler::onTimer(Poco::Timer& /*timer*/) updatePttKey(j, true, i); isPttSetupRunning = false; - break; + return; } } - - if (!isPttSetupRunning) { - break; - } } } @@ -108,13 +104,19 @@ void InputHandler::onTimer(Poco::Timer& /*timer*/) void InputHandler::forwardPttKeyName() { - if (callbackAvailable) { - auto pttKeyName = getPttKeyName(); - callbackRef.NonBlockingCall([pttKeyName](Napi::Env env, Napi::Function jsCallback) { - jsCallback.Call({ Napi::String::New(env, "UpdatePttKeyName"), - Napi::String::New(env, pttKeyName), Napi::String::New(env, "") }); - }); + TRACK_LOG_INFO("Forwarding Ptt Key Nam 1e"); + + if (!callbackAvailable) { + return; } + + TRACK_LOG_INFO("Forwarding Ptt Key Name {}", getPttKeyName()); + + auto pttKeyName = getPttKeyName(); + callbackRef.NonBlockingCall([pttKeyName](Napi::Env env, Napi::Function jsCallback) { + jsCallback.Call({ Napi::String::New(env, "UpdatePttKeyName"), + Napi::String::New(env, pttKeyName), Napi::String::New(env, "") }); + }); } std::string InputHandler::getPttKeyName() diff --git a/backend/src/main.cpp b/backend/src/main.cpp index b8609b7..7df0cf2 100644 --- a/backend/src/main.cpp +++ b/backend/src/main.cpp @@ -393,6 +393,16 @@ void StopAudio(const Napi::CallbackInfo& /*info*/) mClient->StopAudio(); } +void SetupPttBegin(const Napi::CallbackInfo& /*info*/) +{ + mainStaticData::inputHandler->startPttSetup(); +} + +void SetupPttEnd(const Napi::CallbackInfo& /*info*/) +{ + mainStaticData::inputHandler->stopPttSetup(); +} + static void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, void* data2) { if (!callbackAvailable) { @@ -853,6 +863,10 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) exports.Set(Napi::String::New(env, "StopAudio"), Napi::Function::New(env, StopAudio)); + exports.Set(Napi::String::New(env, "SetupPttBegin"), Napi::Function::New(env, SetupPttBegin)); + + exports.Set(Napi::String::New(env, "SetupPttEnd"), Napi::Function::New(env, SetupPttEnd)); + exports.Set(Napi::String::New(env, "Exit"), Napi::Function::New(env, Exit)); return exports; diff --git a/backend/types/index.d.ts b/backend/types/index.d.ts index 9208a3a..5035fd7 100644 --- a/backend/types/index.d.ts +++ b/backend/types/index.d.ts @@ -22,6 +22,7 @@ export declare const AfvEventTypes: { NetworkConnected: string; NetworkDisconnected: string; VuMeter: string; + PttKeySet: string; }; declare namespace TrackAudioAfv { @@ -84,7 +85,14 @@ declare namespace TrackAudioAfv { func: (arg: string, arg2: string, arg3: string) => void ): void; + export function SetupPttBegin(): void; + export function SetupPttEnd(): void; + export function IsConnected(): boolean; - export function Bootstrap(resourcePath: string): {canRun: boolean, needUpdate: boolean, version: string}; + export function Bootstrap(resourcePath: string): { + canRun: boolean; + needUpdate: boolean; + version: string; + }; export function Exit(): void; } diff --git a/package-lock.json b/package-lock.json index 689cc45..0a282d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "@sentry/react": "7.113.0", "bootstrap": "^5.3.3", "bootstrap-scss": "^5.3.3", + "build-backend": "^1.0.2", "clsx": "^2.1.0", "electron-store": "^8.2.0", "i": "^0.3.7", @@ -24,7 +25,6 @@ "sass": "^1.74.1", "styled-components": "^5.3.11", "trackaudio-afv": "file:backend/trackaudio-afv-1.0.0.tgz", - "uiohook-napi": "1.5.2", "use-debounce": "^10.0.0", "zustand": "^4.5.2" }, @@ -4124,6 +4124,14 @@ "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", "dev": true }, + "node_modules/build-backend": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/build-backend/-/build-backend-1.0.2.tgz", + "integrity": "sha512-0aGZeY41Ig6zJc0ndArGKaUsyy6YynrgsEgSKVqL9Sl8RIBxMhSh/sTPwA+F5yf1xcJPBmkF6tk8Xm5P4NMfjg==", + "bin": { + "build": "app.js" + } + }, "node_modules/builder-util": { "version": "24.13.1", "resolved": "https://registry.npmjs.org/builder-util/-/builder-util-24.13.1.tgz", @@ -10544,6 +10552,7 @@ "version": "4.8.1", "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.1.tgz", "integrity": "sha512-OSs33Z9yWr148JZcbZd5WiAXhh/n9z8TxQcdMhIOlpN9AhWpLfvVFO73+m77bBABQMaY9XSvIa+qk0jlI7Gcaw==", + "dev": true, "bin": { "node-gyp-build": "bin.js", "node-gyp-build-optional": "optional.js", @@ -16128,7 +16137,7 @@ "node_modules/trackaudio-afv": { "version": "1.0.0", "resolved": "file:backend/trackaudio-afv-1.0.0.tgz", - "integrity": "sha512-Si7lCJ6VN4grYIdthd27rNvaS5RZTvkMM0N9+AedaJ63MByMwRb7vl2lebDCcubwXLefNtfSbGfgV5YD5Dqd1A==", + "integrity": "sha512-tU8Ws7AoNUStMt+Yddo42cvh9TwAPodLXgCk9GB+ZqDttmfgVqnjkUEYUpAQLQu4cKe3VyPyyu3ihO+U0WaFbw==", "dependencies": { "bindings": "^1.5.0", "node-addon-api": "^1.1.0" @@ -16426,18 +16435,6 @@ } } }, - "node_modules/uiohook-napi": { - "version": "1.5.2", - "resolved": "https://registry.npmjs.org/uiohook-napi/-/uiohook-napi-1.5.2.tgz", - "integrity": "sha512-XmxEQpgWZerYwrhRk5oBbHSO9pjeEKF7cMG6e+Ak1j5MW7xQUvl/P+qaPAYDfbJ1JsT/E6ontbClBBSy5Z3C0g==", - "hasInstallScript": true, - "dependencies": { - "node-gyp-build": "4.x.x" - }, - "engines": { - "node": ">= 16" - } - }, "node_modules/unbox-primitive": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.2.tgz", diff --git a/package.json b/package.json index 3398d23..68202f0 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "@sentry/react": "7.113.0", "bootstrap": "^5.3.3", "bootstrap-scss": "^5.3.3", + "build-backend": "^1.0.2", "clsx": "^2.1.0", "electron-store": "^8.2.0", "i": "^0.3.7", @@ -73,7 +74,6 @@ "sass": "^1.74.1", "styled-components": "^5.3.11", "trackaudio-afv": "file:backend/trackaudio-afv-1.0.0.tgz", - "uiohook-napi": "1.5.2", "use-debounce": "^10.0.0", "zustand": "^4.5.2" }, diff --git a/src/app/components/bootstrap.tsx b/src/app/components/bootstrap.tsx index d478ed3..82c57df 100644 --- a/src/app/components/bootstrap.tsx +++ b/src/app/components/bootstrap.tsx @@ -20,7 +20,7 @@ const Bootsrap: React.FC = () => { useRadioState .getState() .setTransceiverCountForStationCallsign(station, parseInt(count)); - }, + } ); window.api.on( @@ -39,16 +39,16 @@ const Bootsrap: React.FC = () => { .addRadio( freq, station, - useSessionStore.getState().getStationCallsign(), + useSessionStore.getState().getStationCallsign() ); void window.api.SetRadioGain( - useSessionStore.getState().radioGain / 100, + useSessionStore.getState().radioGain / 100 ); }) .catch((err: unknown) => { console.error(err); }); - }, + } ); window.api.on("FrequencyRxBegin", (frequency: string) => { @@ -98,7 +98,7 @@ const Bootsrap: React.FC = () => { const frequency = parseInt(dataArr[1]); useSessionStore.getState().setIsAtc(isAtc); useSessionStore.getState().setFrequency(frequency); - }, + } ); window.api.on("network-disconnected", () => { @@ -109,7 +109,8 @@ const Bootsrap: React.FC = () => { }); window.api.on("ptt-key-set", (key: string) => { - useSessionStore.getState().setPttKeyName(key); + console.log("Ptt key set to", key); + useUtilStore.getState().setPttKeyName(key); }); window.api diff --git a/src/app/components/settings-modal/settings-modal.tsx b/src/app/components/settings-modal/settings-modal.tsx index 064226c..5701519 100644 --- a/src/app/components/settings-modal/settings-modal.tsx +++ b/src/app/components/settings-modal/settings-modal.tsx @@ -4,7 +4,6 @@ import AudioInput from "./audio-input"; import AudioOutputs from "./audio-outputs"; import useUtilStore from "../../store/utilStore"; import clsx from "clsx"; -import useSessionStore from "../../store/sessionStore"; import { useDebouncedCallback } from "use-debounce"; import { Configuration } from "../../../config.d"; import { AudioApi, AudioDevice } from "trackaudio-afv"; @@ -23,28 +22,25 @@ const SettingsModal: React.FC = ({ closeModal }) => { const [changesSaved, setChangesSaved] = useState(SaveStatus.NoChanges); const [audioApis, setAudioApis] = useState(Array); const [audioOutputDevices, setAudioOutputDevices] = useState( - Array, + Array ); const [audioInputDevices, setAudioInputDevices] = useState( - Array, + Array ); const [hardwareType, setHardwareType] = useState(0); const [config, setConfig] = useState({} as Configuration); const [alwaysOnTop, setAlwaysOnTop] = useState(0); const [isSettingPtt, setIsSettingPtt] = useState(false); - const [pttKeyName, setPttKeyName] = useSessionStore((state) => [ - state.pttKeyName, - state.setPttKeyName, - ]); const [cid, setCid] = useState(""); const [password, setPassword] = useState(""); - const [vu, vuPeak, updateVu] = useUtilStore((state) => [ + const [vu, vuPeak, updateVu, pttKeyName] = useUtilStore((state) => [ state.vu, state.peakVu, state.updateVu, + state.pttKeyName, ]); const [isMicTesting, setIsMicTesting] = useState(false); @@ -56,7 +52,6 @@ const SettingsModal: React.FC = ({ closeModal }) => { setCid(config.cid || ""); setPassword(config.password || ""); setHardwareType(config.hardwareType || 0); - setPttKeyName(config.pttKeyName || "None"); setAlwaysOnTop(config.alwaysOnTop ? 1 : 0); }) .catch((err: unknown) => { @@ -89,7 +84,7 @@ const SettingsModal: React.FC = ({ closeModal }) => { .catch((err: unknown) => { console.error(err); }); - }, [config.audioApi, setPttKeyName]); + }, [config.audioApi]); const debouncedCid = useDebouncedCallback((cid: string) => { setChangesSaved(SaveStatus.Saving); @@ -183,7 +178,7 @@ const SettingsModal: React.FC = ({ closeModal }) => { }; const handleHardwareTypeChange = ( - e: React.ChangeEvent, + e: React.ChangeEvent ) => { setChangesSaved(SaveStatus.Saving); const hardwareType = parseInt(e.target.value); @@ -324,7 +319,7 @@ const SettingsModal: React.FC = ({ closeModal }) => { className={clsx( "btn mt-3 w-100", !isMicTesting && "btn-info", - isMicTesting && "btn-warning", + isMicTesting && "btn-warning" )} onClick={handleMicTest} disabled={ diff --git a/src/app/store/sessionStore.ts b/src/app/store/sessionStore.ts index 1f5da1d..bbcaf1f 100644 --- a/src/app/store/sessionStore.ts +++ b/src/app/store/sessionStore.ts @@ -8,7 +8,6 @@ interface sessionStore { isConnecting: boolean; version: string; frequency: number; - pttKeyName: string; radioGain: number; stationCallsign: string; setCallsign: (callsign: string) => void; @@ -18,7 +17,6 @@ interface sessionStore { setVersion: (version: string) => void; setNetworkConnected: (isConnected: boolean) => void; setFrequency: (frequency: number) => void; - setPttKeyName: (pttKeyName: string) => void; setRadioGain: (radioGain: number) => void; setStationCallsign: (stationCallsign: string) => void; getStationCallsign: () => string; @@ -57,9 +55,6 @@ const useSessionStore = create((set) => ({ setFrequency: (frequency) => { set({ frequency }); }, - setPttKeyName: (pttKeyName) => { - set({ pttKeyName }); - }, setRadioGain: (radioGain) => { set({ radioGain }); }, diff --git a/src/app/store/utilStore.ts b/src/app/store/utilStore.ts index aa64d29..3a9219c 100644 --- a/src/app/store/utilStore.ts +++ b/src/app/store/utilStore.ts @@ -4,6 +4,8 @@ interface UtilStore { vu: number; peakVu: number; platform: string; + pttKeyName: string; + setPttKeyName: (pttKeyName: string) => void; updateVu: (vu: number, peakVu: number) => void; updatePlatform: (platform: string) => void; } @@ -12,6 +14,10 @@ const useUtilStore = create((set) => ({ vu: 0, peakVu: 0, platform: "", + pttKeyName: "", + setPttKeyName: (pttKeyName: string) => { + set({ pttKeyName }); + }, updateVu: (vu: number, peakVu: number) => { set({ vu, peakVu }); }, diff --git a/src/config.d.ts b/src/config.d.ts index b193eeb..bc9320a 100644 --- a/src/config.d.ts +++ b/src/config.d.ts @@ -8,12 +8,9 @@ export interface Configuration { password: string; callsign: string; - pttKey: number; - pttKeyName: string; hardwareType: number; radioGain: number; alwaysOnTop: boolean; - consentedToTelemetry: boolean | undefined; } diff --git a/src/index.ts b/src/index.ts index 31395c4..954a8fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,6 @@ import { import { TrackAudioAfv, AfvEventTypes } from "trackaudio-afv"; import { Configuration } from "./config.d"; import Store from "electron-store"; -import { uIOhook, UiohookKey } from "uiohook-napi"; import * as Sentry from "@sentry/electron/main"; // This allows TypeScript to pick up the magic constants that's auto-generated by Forge's Webpack @@ -27,10 +26,6 @@ Sentry.init({ let version: string; let mainWindow: BrowserWindow; -let isSettingPtt = false; - -const keycodeMap = new Map(Object.entries(UiohookKey).map((_) => [_[1], _[0]])); - let currentConfiguration: Configuration = { audioApi: -1, audioInputDeviceId: "", @@ -39,8 +34,6 @@ let currentConfiguration: Configuration = { cid: "", password: "", callsign: "", - pttKey: 0, - pttKeyName: "None", hardwareType: 0, radioGain: 0, alwaysOnTop: false, @@ -62,31 +55,6 @@ const setAudioSettings = () => { TrackAudioAfv.SetHardwareType(currentConfiguration.hardwareType || 0); }; -const setupUiHook = () => { - uIOhook.on("keydown", (e) => { - if (isSettingPtt) { - currentConfiguration.pttKey = e.keycode; - currentConfiguration.pttKeyName = keycodeMap.get(e.keycode) ?? "Unknown"; - saveConfig(); - isSettingPtt = false; - - mainWindow.webContents.send("ptt-key-set", keycodeMap.get(e.keycode)); - } else { - if (e.keycode == currentConfiguration.pttKey && e.keycode != 0) { - TrackAudioAfv.SetPtt(true); - } - } - }); - - uIOhook.on("keyup", (e) => { - if (e.keycode == currentConfiguration.pttKey && e.keycode != 0) { - TrackAudioAfv.SetPtt(false); - } - }); - - uIOhook.start(); -}; - const createWindow = (): void => { // Set the store CID TrackAudioAfv.SetCid(currentConfiguration.cid || ""); @@ -111,7 +79,6 @@ const createWindow = (): void => { // and load the index.html of the app. void mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY); - setupUiHook(); // Open the DevTools only in development mode. if (process.env.NODE_ENV === "development") { mainWindow.webContents.openDevTools(); @@ -209,7 +176,7 @@ app.on("ready", () => { app.quit(); } - version = bootstrapOutput.version as string; + version = bootstrapOutput.version; createWindow(); }); @@ -219,7 +186,6 @@ app.on("ready", () => { // explicitly with Cmd + Q. app.on("window-all-closed", () => { TrackAudioAfv.Exit(); - uIOhook.stop(); app.quit(); }); @@ -352,7 +318,7 @@ ipcMain.handle("refresh-station", (_, callsign: string) => { }); ipcMain.handle("setup-ptt", () => { - isSettingPtt = true; + TrackAudioAfv.SetupPttBegin(); }); ipcMain.handle("set-radio-gain", (_, gain: number) => { @@ -428,6 +394,8 @@ TrackAudioAfv.RegisterCallback((arg: string, arg2: string, arg3: string) => { return; } + console.log(arg) + if (arg === AfvEventTypes.VuMeter) { mainWindow.webContents.send("VuMeter", arg2, arg3); } @@ -475,4 +443,9 @@ TrackAudioAfv.RegisterCallback((arg: string, arg2: string, arg3: string) => { if (arg == AfvEventTypes.NetworkDisconnected) { mainWindow.webContents.send("network-disconnected"); } + + if (arg == "UpdatePttKeyName") { + console.log("UpdatePttKeyName", arg2); + mainWindow.webContents.send("ptt-key-set", arg2); + } });