Audacity  2.3.1
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  OldStyleCommandPointer cmd = builder.GetCommand();
111  AppCommandEvent ev;
112  ev.SetCommand(cmd);
113  AudacityApp & App = wxGetApp();
114  App.OnReceiveCommand(ev);
115 
116  *pOut = wxEmptyString;
117  }
118  else
119  {
120  *pOut = wxT("Syntax error!\n");
121  *pOut += builder.GetErrorMessage() + wxT("\n");
122  }
123  }
124 
125  // Wait until all responses from the command have been received.
126  // The last response is signalled by an empty line.
128  while (msg != wxT("\n"))
129  {
130  //wxLogDebug( "Msg: %s", msg );
131  *pOut += msg + wxT("\n");
133  }
134 
135  return 0;
136 }
137 
138 
139 
140 // The void * return is actually a Lisp LVAL and will be cast to such as needed.
141 extern void * ExecForLisp( char * pIn );
142 extern void * nyq_make_opaque_string( int size, unsigned char *src );
143 extern void * nyq_reformat_aud_do_response(const wxString & Str);
144 
145 
146 void * ExecForLisp( char * pIn ){
147  wxString Str1( pIn );
148  wxString Str2;
149  ExecCommand2( &Str1, &Str2 );
150 
151  // wxString provides a const char *
152  //const char * pStr = static_cast<const char*>(Str2);
153 
154  // We'll be passing it as a non-const unsigned char *
155  // That 'unsafe' cast is actually safe. nyq_make_opaque_string is just copying the string.
156  void * pResult = nyq_reformat_aud_do_response( Str2 );
157  return pResult;
158 };
159 
160 
162 void ScriptCommandRelay::SendResponse(const wxString &response)
163 {
164  sResponseQueue.AddResponse(response);
165 }
166 
169 {
170  return ScriptCommandRelay::sResponseQueue.WaitAndGetResponse();
171 }
172 
175 std::shared_ptr<ResponseQueueTarget> ScriptCommandRelay::GetResponseTarget()
176 {
177  // This should be deleted by a Command destructor
178  return std::make_shared<ResponseQueueTarget>(sResponseQueue);
179 }
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:3172
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:175
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:312
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 * nyq_reformat_aud_do_response(const wxString &Str)
Definition: Nyquist.cpp:3186
void SetCommand(const OldStyleCommandPointer &cmd)
Store a pointer to a command object.
static void SetCommandHandler(CommandHandler &ch)
static ResponseQueue sResponseQueue
static tpRegScriptServerFunc sScriptFn