diff --git a/MusicToSoundpad.vcxproj b/MusicToSoundpad.vcxproj
index 52233de..c7517e6 100644
--- a/MusicToSoundpad.vcxproj
+++ b/MusicToSoundpad.vcxproj
@@ -21,9 +21,11 @@
+
+
17.0
diff --git a/MusicToSoundpad.vcxproj.filters b/MusicToSoundpad.vcxproj.filters
index 672ec92..ccfb2d1 100644
--- a/MusicToSoundpad.vcxproj.filters
+++ b/MusicToSoundpad.vcxproj.filters
@@ -21,10 +21,16 @@
Source Files
+
+ Source Files
+
Source Files
+
+ Source Files
+
\ No newline at end of file
diff --git a/src/main.c b/src/main.c
index 6767adb..eeb10d5 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,6 +4,7 @@
#include
#include
#include "md5.h"
+#include "pipes.h"
#pragma comment(lib, "urlmon.lib")
@@ -97,8 +98,51 @@ BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
STARTUPINFOW si = { sizeof(si) };
PROCESS_INFORMATION pi;
wchar_t commandLine[MAX_PATH];
- swprintf_s(commandLine, MAX_PATH, L"\"utils\\yt-dlp.exe\" -f ba -x ytsearch:\"%s\"", windowTitle);
+ swprintf_s(commandLine, MAX_PATH, L"\"utils\\yt-dlp.exe\" -f ba -x --audio-format mp3 -o \"%%USERPROFILE%%\\Music\\%s.%%(ext)s\" ytsearch:\"%s\"", windowTitle, windowTitle);
if (CreateProcessW(NULL, commandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
+ // Wait for the process to finish before continuing
+ WaitForSingleObject(pi.hProcess, INFINITE);
+
+ // FIXME: We need to convert windowTitle from a wide to narrow
+
+ // Determine the size of the required buffer
+ size_t bufferSize = 0;
+ if (wcstombs_s(&bufferSize, NULL, 0, windowTitle, 0) != 0) {
+ perror("wcstombs_s");
+ return 1;
+ }
+
+ // Allocate memory for the narrow character buffer
+ char* narrowWindowTitle = (char*)malloc(bufferSize + 8); // +1 for null terminator
+ if (narrowWindowTitle == NULL) {
+ perror("malloc");
+ return 1;
+ }
+
+ // Perform the conversion
+ if (wcstombs_s(NULL, narrowWindowTitle, bufferSize + 1, windowTitle, bufferSize) != 0) {
+ perror("wcstombs");
+ free(narrowWindowTitle);
+ return 1;
+ }
+
+ // We now have to construct a path to the .mp3 file for SoundPad
+ char userProfile[512];
+ char path[512];
+
+ // Get the value of the USERPROFILE environment variable
+ char* userProfileEnv = getenv("USERPROFILE");
+ if (userProfileEnv == NULL) {
+ fprintf(stderr, "USERPROFILE environment variable not found\n");
+ return 1;
+ }
+
+ // Construct the path using USERPROFILE
+ snprintf(path, sizeof(path), "%s\\Music\\%s.mp3", userProfileEnv, narrowWindowTitle);
+
+ // Add to SoundPad library and start playing
+ checkForEntry(getSoundList(), path);
+
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
diff --git a/src/pipes.c b/src/pipes.c
new file mode 100644
index 0000000..ea3ff9d
--- /dev/null
+++ b/src/pipes.c
@@ -0,0 +1,138 @@
+#include
+#include
+#include
+#include
+#include
+#include "pipes.h"
+
+#define PIPE_NAME "\\\\.\\pipe\\sp_remote_control"
+#define BUFFER_SIZE 4096
+#define RESPONSE_SIZE 4096
+
+HANDLE pipeHandle = NULL;
+
+void init() {
+ if (pipeHandle == NULL || pipeHandle == INVALID_HANDLE_VALUE) {
+ pipeHandle = CreateFileA(
+ PIPE_NAME,
+ GENERIC_READ | GENERIC_WRITE,
+ 0,
+ NULL,
+ OPEN_EXISTING,
+ 0,
+ NULL
+ );
+
+ if (pipeHandle == INVALID_HANDLE_VALUE) {
+ printf("Error opening pipe\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+}
+
+void uninit() {
+ if (pipeHandle != NULL && pipeHandle != INVALID_HANDLE_VALUE) {
+ CloseHandle(pipeHandle);
+ pipeHandle = NULL;
+ }
+}
+
+char* sendRequest(const char* request) {
+ init();
+
+ DWORD bytesWritten;
+ if (!WriteFile(pipeHandle, request, strlen(request), &bytesWritten, NULL)) {
+ printf("Error writing to pipe\n");
+ exit(EXIT_FAILURE);
+ }
+
+ char response[RESPONSE_SIZE];
+ DWORD bytesRead;
+ if (!ReadFile(pipeHandle, response, RESPONSE_SIZE - 1, &bytesRead, NULL)) {
+ printf("Error reading from pipe\n");
+ exit(EXIT_FAILURE);
+ }
+ response[bytesRead] = '\0'; // Null-terminate the string
+
+ return _strdup(response);
+}
+
+int isSuccessPrintResponse(const char* response) {
+ if (strcmp(response, "R-200") != 0) {
+ printf("Error: %s\n", response);
+ return 0;
+ }
+ return 1;
+}
+
+char* getSoundList() {
+ char request[BUFFER_SIZE];
+ snprintf(request, BUFFER_SIZE, "GetSoundlist()");
+
+ // Call sendRequest function and store response
+ char* response = sendRequest(request);
+ if (response == NULL) {
+ fprintf(stderr, "Failed to get sound list\n");
+ return NULL; // Return NULL if sendRequest fails
+ }
+
+ return response;
+}
+
+int addSound(const char* url) {
+ char request[BUFFER_SIZE];
+ snprintf(request, BUFFER_SIZE, "DoAddSound(\"%s\")", url);
+ char* response = sendRequest(request);
+ int success = isSuccessPrintResponse(response);
+ free(response);
+ return success;
+}
+
+int playSound(const int index) {
+ char request[BUFFER_SIZE];
+ snprintf(request, BUFFER_SIZE, "DoPlaySound(%d)", index);
+ char* response = sendRequest(request);
+ int success = isSuccessPrintResponse(response);
+ free(response);
+ return success;
+}
+
+int getIndex(const char* inputCopy, const char* searchTerm) {
+ int songIndex = 0;
+ char* context = NULL;
+ char* line = strtok_s(inputCopy, "\n", &context);
+ while (line != NULL) {
+ if (strstr(line, searchTerm) != NULL) {
+ printf("%s\n", line);
+ if (sscanf_s(line, "%*[^0-9]%d", &songIndex) == 1) {
+ printf("Extracted index value: %d\n", songIndex);
+ }
+ else {
+ printf("Failed to extract index value.\n");
+ return 0;
+ }
+ break; // Exit loop after finding the line
+ }
+ line = strtok_s(NULL, "\n", &context);
+ }
+ return songIndex;
+}
+
+int checkForEntry(const char* input, const char* searchTerm) {
+ int songIndex = getIndex(input, searchTerm);
+
+ if (!songIndex) { // Item is not present in the Soundpad library. We need to add it.
+ if (!addSound(searchTerm)) {
+ printf("Failed to add sound: %s", searchTerm);
+ return 1;
+ }
+ printf("Sound added to SoundPad library.\n");
+ Sleep(1000);
+ songIndex = getIndex(getSoundList(), searchTerm);
+ }
+ if (!playSound(songIndex)) {
+ printf("Failed to play sound\n");
+ }
+ uninit(); // Clean up resources
+ return 0;
+}
diff --git a/src/pipes.h b/src/pipes.h
new file mode 100644
index 0000000..b11b00c
--- /dev/null
+++ b/src/pipes.h
@@ -0,0 +1,11 @@
+#pragma once
+
+void init();
+void uninit();
+char* sendRequest(const char* request);
+int isSuccessPrintResponse(const char* response);
+char* getSoundList();
+int addSound(const char* url);
+int playSound(const int index);
+int getIndex(const char* inputCopy, const char* searchTerm);
+int checkForEntry(const char* input, const char* searchTerm);