From 066f774c95f2a4ca14e12b4d178441e8527bf256 Mon Sep 17 00:00:00 2001 From: m1maker Date: Sun, 26 Jan 2025 13:55:38 +0300 Subject: [PATCH] Add NVDAControl impl files (Use the named pipe). --- Dep/nvda_control.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++ Dep/nvda_control.h | 72 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 Dep/nvda_control.c create mode 100644 Dep/nvda_control.h diff --git a/Dep/nvda_control.c b/Dep/nvda_control.c new file mode 100644 index 0000000..8e29202 --- /dev/null +++ b/Dep/nvda_control.c @@ -0,0 +1,79 @@ +#include "nvda_control.h" + +// Connects to the NVDA named pipe +HANDLE nvda_connect() { + HANDLE hPipe = CreateFile( + NVDA_PIPE_NAME, // Pipe name + GENERIC_READ | GENERIC_WRITE, // Read and write access + 0, // No sharing + NULL, // Default security attributes + OPEN_EXISTING, // Opens existing pipe + 0, // Default attributes + NULL // No template file + ); + + return hPipe; +} + +// Disconnects from the NVDA named pipe +void nvda_disconnect(HANDLE hPipe) { + if (hPipe != INVALID_HANDLE_VALUE) { + CloseHandle(hPipe); + } +} + +// Sends a command to the NVDA named pipe +int nvda_send_command(HANDLE hPipe, const char* command) { + if (hPipe == INVALID_HANDLE_VALUE) { + return -1; + } + + DWORD bytesWritten; + BOOL result = WriteFile( + hPipe, // Pipe handle + command, // Command to send + (DWORD)strlen(command), // Length of command + &bytesWritten, // Bytes written + NULL // Not overlapped + ); + + if (!result) { + fprintf(stderr, "Failed to send command. Error: %lu\n", GetLastError()); + return -1; + } + + return 0; +} + +// Sends a "speak" command to NVDA +int nvda_speak(HANDLE hPipe, const char* text) { + char command[64000]; + snprintf(command, sizeof(command), "speak \"%s\"", text); + return nvda_send_command(hPipe, command); +} + +// Sends a "speakSpelling" command to NVDA +int nvda_speak_spelling(HANDLE hPipe, const char* text) { + char command[64000]; + snprintf(command, sizeof(command), "speakSpelling \"%s\"", text); + return nvda_send_command(hPipe, command); +} + +// Sends a "speakSsml" command to NVDA +int nvda_speak_ssml(HANDLE hPipe, const char* ssml) { + char command[64000]; + snprintf(command, sizeof(command), "speakSsml \"%s\"", ssml); + return nvda_send_command(hPipe, command); +} + +// Sends a "pauseSpeech" command to NVDA +int nvda_pause_speech(HANDLE hPipe, int pause) { + char command[256]; + snprintf(command, sizeof(command), "pauseSpeech %d", pause); + return nvda_send_command(hPipe, command); +} + +// Sends a "cancelSpeech" command to NVDA +int nvda_cancel_speech(HANDLE hPipe) { + return nvda_send_command(hPipe, "cancelSpeech"); +} diff --git a/Dep/nvda_control.h b/Dep/nvda_control.h new file mode 100644 index 0000000..72046be --- /dev/null +++ b/Dep/nvda_control.h @@ -0,0 +1,72 @@ +#ifndef NVDA_CONTROL_H +#define NVDA_CONTROL_H + +#include +#include +#include +#include + +// Named pipe name +#define NVDA_PIPE_NAME L"\\\\.\\pipe\\NVDAControlPipe" + + +/** + * Connects to the NVDA named pipe. + * Returns a handle to the pipe if successful, or INVALID_HANDLE_VALUE on failure. + */ +HANDLE nvda_connect(); + +/** + * Disconnects from the NVDA named pipe. + * @param hPipe Handle to the pipe. + */ +void nvda_disconnect(HANDLE hPipe); + +/** + * Sends a command to the NVDA named pipe. + * @param hPipe Handle to the pipe. + * @param command The command to send. + * @return 0 on success, -1 on failure. + */ +int nvda_send_command(HANDLE hPipe, const char* command); + +/** + * Sends a "speak" command to NVDA. + * @param hPipe Handle to the pipe. + * @param text The text to speak. + * @return 0 on success, -1 on failure. + */ +int nvda_speak(HANDLE hPipe, const char* text); + +/** + * Sends a "speakSpelling" command to NVDA. + * @param hPipe Handle to the pipe. + * @param text The text to spell. + * @return 0 on success, -1 on failure. + */ +int nvda_speak_spelling(HANDLE hPipe, const char* text); + +/** + * Sends a "speakSsml" command to NVDA. + * @param hPipe Handle to the pipe. + * @param ssml The SSML text to speak. + * @return 0 on success, -1 on failure. + */ +int nvda_speak_ssml(HANDLE hPipe, const char* ssml); + +/** + * Sends a "pauseSpeech" command to NVDA. + * @param hPipe Handle to the pipe. + * @param pause 1 to pause, 0 to unpause. + * @return 0 on success, -1 on failure. + */ +int nvda_pause_speech(HANDLE hPipe, int pause); + +/** + * Sends a "cancelSpeech" command to NVDA. + * @param hPipe Handle to the pipe. + * @return 0 on success, -1 on failure. + */ +int nvda_cancel_speech(HANDLE hPipe); + +#endif // NVDA_CONTROL_H