Audacity  2.2.2
ScriptCommandRelay.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity - A Digital Audio Editor
4  Copyright 1999-2018 Audacity Team
5  File License: wxWidgets
6 
7  Dan Horgan
8 
9 ******************************************************************//****************************************************************//*******************************************************************/
21 
22 #include "ScriptCommandRelay.h"
23 #include "CommandTargets.h"
24 #include "CommandBuilder.h"
25 #include "AppCommandEvent.h"
26 #include "ResponseQueue.h"
27 #include "../Project.h"
28 #include "../AudacityApp.h"
29 #include <wx/string.h>
30 
31 // Declare static class members
35 
37 {
39 }
40 
42 {
43  sCmdHandler = &ch;
44 }
45 
48 {
49  wxASSERT( sScriptFn != NULL );
50  while( true )
52 }
53 
56 {
57  wxASSERT(project != NULL);
58  wxASSERT(cmd != NULL);
59  AppCommandEvent ev;
60  ev.SetCommand(cmd);
61  project->GetEventHandler()->AddPendingEvent(ev);
62 }
63 
68 int ExecCommand(wxString *pIn, wxString *pOut)
69 {
70  {
71  CommandBuilder builder(*pIn);
72  if (builder.WasValid())
73  {
74  AudacityProject *project = GetActiveProject();
75  OldStyleCommandPointer cmd = builder.GetCommand();
76  ScriptCommandRelay::PostCommand(project, cmd);
77 
78  *pOut = wxEmptyString;
79  }
80  else
81  {
82  *pOut = wxT("Syntax error!\n");
83  *pOut += builder.GetErrorMessage() + wxT("\n");
84  }
85  }
86 
87  // Wait until all responses from the command have been received.
88  // The last response is signalled by an empty line.
90  while (msg != wxT("\n"))
91  {
92  //wxLogDebug( "Msg: %s", msg );
93  *pOut += msg + wxT("\n");
95  }
96 
97  return 0;
98 }
99 
104 int ExecCommand2(wxString *pIn, wxString *pOut)
105 {
106  {
107  CommandBuilder builder(*pIn);
108  if (builder.WasValid())
109  {
110  AudacityProject *project = GetActiveProject();
111  OldStyleCommandPointer cmd = builder.GetCommand();
112  AppCommandEvent ev;
113  ev.SetCommand(cmd);
114  AudacityApp & App = wxGetApp();
115  App.OnReceiveCommand(ev);
116 
117  *pOut = wxEmptyString;
118  }
119  else
120  {
121  *pOut = wxT("Syntax error!\n");
122  *pOut += builder.GetErrorMessage() + wxT("\n");
123  }
124  }
125 
126  // Wait until all responses from the command have been received.
127  // The last response is signalled by an empty line.
129  while (msg != wxT("\n"))
130  {
131  //wxLogDebug( "Msg: %s", msg );
132  *pOut += msg + wxT("\n");
134  }
135 
136  return 0;
137 }
138 
139 
140 
141 // The void * return is actually a Lisp LVAL and will be cast to such as needed.
142 extern void * ExecForLisp( char * pIn );
143 extern void * nyq_make_opaque_string( int size, unsigned char *src );
144 
145 void * ExecForLisp( char * pIn ){
146  wxString Str1( pIn );
147  wxString Str2;
148  ExecCommand2( &Str1, &Str2 );
149 
150  // wxString provides a const char *
151  const char * pStr = static_cast<const char*>(Str2);
152 
153  // We'll be passing it as a non-const unsigned char *
154  // That 'unsafe' cast is actually safe. nyq_make_opaque_string is just copying the string.
155  void * pResult = nyq_make_opaque_string( Str2.Length(), (unsigned char *)pStr );
156  return pResult;
157 };
158 
159 
161 void ScriptCommandRelay::SendResponse(const wxString &response)
162 {
163  sResponseQueue.AddResponse(response);
164 }
165 
168 {
169  return ScriptCommandRelay::sResponseQueue.WaitAndGetResponse();
170 }
171 
174 std::shared_ptr<ResponseQueueTarget> ScriptCommandRelay::GetResponseTarget()
175 {
176  // This should be deleted by a Command destructor
177  return std::make_shared<ResponseQueueTarget>(sResponseQueue);
178 }
void OnReceiveCommand(AppCommandEvent &event)
void AddResponse(Response response)
const wxString & GetErrorMessage()
static tpRegScriptServerFunc scriptFn
void * nyq_make_opaque_string(int size, unsigned char *src)
Definition: Nyquist.cpp:3191
static void SetRegScriptServerFunc(tpRegScriptServerFunc scriptFn)
Headers and event table macros for AppCommandEvent.
Contains declarations for ScriptCommandRelay.
int ExecCommand2(wxString *pIn, wxString *pOut)
static void Run()
Calls the script function, passing it the function for obeying commands.
AudacityProject provides the main window, with tools and tracks contained within it.
Definition: Project.h:176
static std::shared_ptr< ResponseQueueTarget > GetResponseTarget()
Response WaitAndGetResponse()
static CommandHandler * sCmdHandler
Contains methods for applying commands that are passed to it.
Allow messages to be sent from the main thread to the script thread.
Definition: ResponseQueue.h:60
static Response ReceiveResponse()
Gets a response from the queue (may block)
wxString GetMessage()
Definition: ResponseQueue.h:54
Contains declarations for Response and ResponseQueue classes.
static void PostCommand(AudacityProject *project, const OldStyleCommandPointer &cmd)
Send a command to a project, to be applied in that context.
AudacityApp is the 'main' class for Audacity.
Definition: AudacityApp.h:59
void * ExecForLisp(char *pIn)
An event 'envelope' for sending Command objects through the wxwidgets event loop. ...
AUDACITY_DLL_API AudacityProject * GetActiveProject()
Definition: Project.cpp:309
int ExecCommand(wxString *pIn, wxString *pOut)
int(* tpRegScriptServerFunc)(tpExecScriptServerFunc pFn)
Stores a command response string (and other response data if it becomes necessary) ...
Definition: ResponseQueue.h:46
static void SendResponse(const wxString &response)
Adds a response to the queue to be sent back to the script.
OldStyleCommandPointer is a unique_ptr to an OldStyleCommand.
AudacityApp & wxGetApp()
Contains declaration of CommandBuilder class.
OldStyleCommandPointer GetCommand()
A type of factory for Commands of various sorts.
void SetCommand(const OldStyleCommandPointer &cmd)
Store a pointer to a command object.
static void SetCommandHandler(CommandHandler &ch)
static ResponseQueue sResponseQueue
static tpRegScriptServerFunc sScriptFn