Browse Source

Added OSC String support by creating a payload buffer and getter functions. closes #4

tags/0.1
Ludwig Frühschütz 3 years ago
parent
commit
69a563e04e
3 changed files with 87 additions and 3 deletions
  1. +59
    -1
      OSC2AHK/dllmain.cpp
  2. +3
    -0
      OSC2AHK/dllmain.h
  3. +25
    -2
      msgtest.ahk

+ 59
- 1
OSC2AHK/dllmain.cpp View File

@ -37,6 +37,15 @@ struct Listener {
unsigned int message;
};
std::vector<Listener> 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<StoredString> 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


+ 3
- 0
OSC2AHK/dllmain.h View File

@ -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);

+ 25
- 2
msgtest.ahk View File

@ -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)
; DllCall("OSC2AHK.dll\open", UInt, hWnd, UInt, 7001)

Loading…
Cancel
Save