From 69a563e04ebc3481631b9ce9c6e85736076814a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludwig=20Fr=C3=BChsch=C3=BCtz?= Date: Sat, 27 Mar 2021 00:09:22 +0100 Subject: [PATCH] Added OSC String support by creating a payload buffer and getter functions. closes #4 --- OSC2AHK/dllmain.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++- OSC2AHK/dllmain.h | 3 +++ msgtest.ahk | 27 ++++++++++++++++++++++-- 3 files changed, 87 insertions(+), 3 deletions(-) diff --git a/OSC2AHK/dllmain.cpp b/OSC2AHK/dllmain.cpp index 275b896..97f254e 100644 --- a/OSC2AHK/dllmain.cpp +++ b/OSC2AHK/dllmain.cpp @@ -37,6 +37,15 @@ struct Listener { unsigned int message; }; std::vector listeners; +/* Definition and declaration of the array buffer for received OSC Strings. +* All received OSC strings that match a listener are stored here until they are +* fetched by the calling script. */ +struct StoredString { + std::string string; + int id; +}; +std::vector storedStrings; +const int storedStringsMaxSize = 100; /* Handle to the calling window, also indicates if this OSC receiver is "opened" by being != NULL*/ HWND hwnd = NULL; /* OSC receiving objects and thread. @@ -109,6 +118,7 @@ DLLEXPORT int close(unsigned int clearListeners) oscThread = NULL; hwnd = NULL; if (clearListeners) listeners.clear(); + storedStrings.clear(); return 0; } @@ -140,6 +150,52 @@ DLLEXPORT int removeListener(LPCSTR address_) return result; } +DLLEXPORT char* getStringData(char* targetString, unsigned int targetSize, unsigned int stringId) +{ + for (UINT i = 0; i < storedStrings.size(); i++) + { + if (storedStrings[i].id == stringId) + { + strcpy_s(targetString, targetSize, storedStrings[i].string.c_str()); + storedStrings.erase(storedStrings.begin() + i); + return targetString; + } + } + //Return empty string if nothing was found. + strcpy_s(targetString, targetSize, "\0"); + return targetString; +} + +void removeStoredString(int stringId) +{ + for (UINT i = 0; i < storedStrings.size(); i++) + { + if (storedStrings[i].id == stringId) + { + storedStrings.erase(storedStrings.begin() + i); + return; //Every ID is unique in the array, no need to keep searching. + } + } +} + +int addStoredString(std::string theString) +{ + static int storedStringsNextId = 0; + + int storedStringsThisId = storedStringsNextId; + if (storedStringsNextId == INT_MAX) storedStringsNextId = 0; + else storedStringsNextId++; + + //Make sure every ID is unique in the array. + removeStoredString(storedStringsThisId); + //Limit array size to fixed value + if (storedStrings.size() >= storedStringsMaxSize) storedStrings.erase(storedStrings.begin()); + + storedStrings.push_back(StoredString{ theString, storedStringsThisId }); + + return storedStringsThisId; +} + unsigned int getOscType(osc::ReceivedMessage::const_iterator arg) { if (arg->IsBlob()) return OSC_TYPE_BLOB; @@ -237,7 +293,9 @@ int handleOscMsg(const osc::ReceivedMessage& m) ret++; } else if (msgType == OSC_TYPE_STRING) { - OutputDebugString(L"handleOScMsg: String not implemented yet"); //TODO + PostMessage(hwnd, listeners[i].message, + OSC_TYPE_STRING, + addStoredString( std::string(m.ArgumentsBegin()->AsString()) ) ) ; } else if (msgType == OSC_TYPE_BLOB) { OutputDebugString(L"handleOScMsg: Blob not implemented yet"); //TODO diff --git a/OSC2AHK/dllmain.h b/OSC2AHK/dllmain.h index 1e48512..c8e853d 100644 --- a/OSC2AHK/dllmain.h +++ b/OSC2AHK/dllmain.h @@ -23,7 +23,10 @@ extern "C" DLLEXPORT int open(HWND targetWindowHandle, unsigned int port); extern "C" DLLEXPORT int close(unsigned int clearListeners = 1); extern "C" DLLEXPORT int addListener(LPCSTR address, unsigned int messageID, unsigned int dataType = OSC_TYPE_ALL); extern "C" DLLEXPORT int removeListener(LPCSTR address); +extern "C" DLLEXPORT char* getStringData(char* targetString, unsigned int targetSize, unsigned int StringID); extern "C" DLLEXPORT int testMsg(HWND windowHandle, unsigned int messageID); int handleOscMsg(const osc::ReceivedMessage& m); bool isMatchingOscType(unsigned int msgType, unsigned int listenerTypeField); unsigned int getOscType(osc::ReceivedMessage::const_iterator arg); +void removeStoredString(int stringId); +int addStoredString(std::string theString); diff --git a/msgtest.ahk b/msgtest.ahk index 7c30573..bc640b1 100644 --- a/msgtest.ahk +++ b/msgtest.ahk @@ -9,6 +9,7 @@ hWnd := WinExist() global oscTypeNone := 1 global oscTypeInt := 2 global oscTypeFloat := 4 +global oscTypeString := 8 global oscTypeAll := 0xffffffff stdout := FileOpen("tmp.log", "w") @@ -31,9 +32,28 @@ OnMessage(0x1002, "msghandlerFloat") DllCall("OSC2AHK.dll\addListener", AStr, "/test3", UInt, 0x1003, UInt, oscTypeInt+oscTypeFloat) OnMessage(0x1003, "msghandlerTest3") +DllCall("OSC2AHK.dll\addListener", AStr, "/test4", UInt, 0x1004, UInt, oscTypeString) +OnMessage(0x1004, "msghandlerString") + stdout.Close() return +msghandlerString(wParam, lParam, msg, hwnd) { + stdout := FileOpen("tmp.log", "a") + if (wParam = oscTypeInt) + { + stdout.WriteLine("No String!") + return + } + stdout.Write("msghandlerString: ") + + VarSetCapacity(theStr, 20) + theStr := DllCall("OSC2AHK.dll\getStringData", AStr, theStr, UInt, 20, UInt, lParam, "Cdecl AStr") + + stdout.WriteLine(theStr) + stdout.Close() +} + msghandlerTest3(wParam, lParam, msg, hwnd) { stdout := FileOpen("tmp.log", "a") stdout.Write("Got /test3 with ") @@ -103,6 +123,9 @@ Esc:: ExitApp ^a:: -DllCall("OSC2AHK.dll\close", UInt, 0) +VarSetCapacity(theStr, 10) +theStr := DllCall("OSC2AHK.dll\getStringData", AStr, theStr, UInt, 10, UInt, 0, "Cdecl AStr") +msgbox,%theStr% +; DllCall("OSC2AHK.dll\close", UInt, 0) ;msgbox,esc -DllCall("OSC2AHK.dll\open", UInt, hWnd, UInt, 7001) \ No newline at end of file +; DllCall("OSC2AHK.dll\open", UInt, hWnd, UInt, 7001) \ No newline at end of file