Audacity 3.2.0
ScripterCallback.cpp
Go to the documentation of this file.
1// ScripterCallback.cpp :
2//
3// A loadable module that connects a windows named pipe
4// to a registered service function that is able to
5// process a single command at a time.
6//
7// The service function is provided by the application
8// and not by libscript. mod_script_pipe was developed for
9// Audacity. Because it forwards commands
10// rather than handling them itself it can be used in
11// other projects too.
12//
13// Enabling other programs to connect to Audacity via a pipe is a potential
14// security risk. Use at your own risk.
15
16#include <wx/wx.h>
17#include "ScripterCallback.h"
19
20/*
21//#define ModuleDispatchName "ModuleDispatch"
22See the example in this file. It has several cases/options in it.
23*/
24
25#include "ModuleConstants.h"
26
27extern void PipeServer();
28typedef DLL_IMPORT int (*tpExecScriptServerFunc)( wxString * pIn, wxString * pOut);
30
31
32extern "C" {
33
34// And here is our special registration function.
36{
37 if( pFn )
38 {
39 pScriptServerFn = pFn;
40 PipeServer();
41 }
42
43 return 4;
44}
45
48{
49 switch (type) {
52 break;
53 default:
54 break;
55 }
56 return 1;
57}
58
59wxString Str2;
60wxArrayString aStr;
61unsigned int currentLine;
63
64// Send the received command to Audacity and build an array of response lines.
65// The response lines can be retrieved by calling DoSrvMore repeatedly.
66int DoSrv(char *pIn)
67{
68 // Interpret string as unicode.
69 // wxWidgets (now) uses unicode internally.
70 // Scripts must send unicode strings (if going beyond 7-bit ASCII).
71 // Important for filenames in commands.
72 wxString Str1(pIn, wxConvUTF8);
73 Str1.Replace( wxT("\r"), wxT(""));
74 Str1.Replace( wxT("\n"), wxT(""));
75 Str2 = wxEmptyString;
76 (*pScriptServerFn)( &Str1 , &Str2);
77
78 Str2 += wxT('\n');
79 size_t outputLength = Str2.Length();
80 aStr.Clear();
81 size_t iStart = 0;
82 size_t i;
83 for(i = 0; i < outputLength; ++i)
84 {
85 if( Str2[i] == wxT('\n') )
86 {
87 aStr.Add( Str2.Mid( iStart, i-iStart) + wxT("\n") );
88 iStart = i+1;
89 }
90 }
91
92 currentLine = 0;
94
95 return 1;
96}
97
98size_t smin(size_t a, size_t b) { return a < b ? a : b; }
99
100// Write up to nMax characters of the prepared (by DoSrv) response lines.
101// Returns the number of characters sent, including null.
102// Zero returned if and only if there's nothing else to send.
103int DoSrvMore(char *pOut, size_t nMax)
104{
105 wxASSERT(currentLine >= 0);
106 wxASSERT(currentPosition >= 0);
107
108 size_t totalLines = aStr.GetCount();
109 while (currentLine < totalLines)
110 {
111 wxCharBuffer lineString = aStr[currentLine].ToUTF8();
112 size_t lineLength = lineString.length();
113 size_t charsLeftInLine = lineLength - currentPosition;
114
115 wxASSERT(charsLeftInLine >= 0);
116
117 if (charsLeftInLine == 0)
118 {
119 // Move to next line
120 ++currentLine;
121 currentPosition = 0;
122 }
123 else
124 {
125 // Write as much of the rest of the line as will fit in the buffer
126 size_t charsToWrite = smin(charsLeftInLine, nMax - 1);
127 memcpy(pOut,
128 &(lineString.data()[currentPosition]),
129 charsToWrite);
130 pOut[charsToWrite] = '\0';
131 currentPosition += charsToWrite;
132 // Need to cast to prevent compiler warnings
133 int charsWritten = static_cast<int>(charsToWrite + 1);
134 // (Check cast was safe)
135 wxASSERT(static_cast<size_t>(charsWritten) == charsToWrite + 1);
136 return charsWritten;
137 }
138 }
139 return 0;
140}
141
142} // End extern "C"
wxT("CloseDown"))
#define DLL_IMPORT
#define DLL_API
ModuleDispatchTypes
@ ModuleInitialize
Contains declarations for ScriptCommandRelay.
size_t currentPosition
unsigned int currentLine
DEFINE_VERSION_CHECK DLL_API int ModuleDispatch(ModuleDispatchTypes type)
void PipeServer()
Definition: PipeServer.cpp:124
size_t smin(size_t a, size_t b)
int DLL_API RegScriptServerFunc(tpExecScriptServerFunc pFn)
int DoSrv(char *pIn)
int DoSrvMore(char *pOut, size_t nMax)
DLL_IMPORT int(* tpExecScriptServerFunc)(wxString *pIn, wxString *pOut)
static tpExecScriptServerFunc pScriptServerFn
wxString Str2
wxArrayString aStr
static void StartScriptServer(tpRegScriptServerFunc scriptFn)
Starts the script server.