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 
101 void ScriptCommandRelay::SendResponse(const wxString &response)
102 {
103  sResponseQueue.AddResponse(response);
104 }
105 
108 {
109  return ScriptCommandRelay::sResponseQueue.WaitAndGetResponse();
110 }
111 
114 std::shared_ptr<ResponseQueueTarget> ScriptCommandRelay::GetResponseTarget()
115 {
116  // This should be deleted by a Command destructor
117  return std::make_shared<ResponseQueueTarget>(sResponseQueue);
118 }
void AddResponse(Response response)
const wxString & GetErrorMessage()
static tpRegScriptServerFunc scriptFn
static void SetRegScriptServerFunc(tpRegScriptServerFunc scriptFn)
Headers and event table macros for AppCommandEvent.
Contains declarations for ScriptCommandRelay.
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.
An event 'envelope' for sending Command objects through the wxwidgets event loop. ...
AUDACITY_DLL_API AudacityProject * GetActiveProject()
Definition: Project.cpp:308
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.
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