Skip to content

Commit

Permalink
Add NVDAControl impl files (Use the named pipe).
Browse files Browse the repository at this point in the history
  • Loading branch information
m1maker committed Jan 26, 2025
1 parent 29a703b commit 066f774
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
79 changes: 79 additions & 0 deletions Dep/nvda_control.c
Original file line number Diff line number Diff line change
@@ -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");
}
72 changes: 72 additions & 0 deletions Dep/nvda_control.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#ifndef NVDA_CONTROL_H
#define NVDA_CONTROL_H

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 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

0 comments on commit 066f774

Please sign in to comment.