Audacity 3.2.0
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
24#include "CommandTargets.h"
25#include "CommandBuilder.h"
26#include "ActiveProject.h"
27#include "AppCommandEvent.h"
28#include "Project.h"
29#include <wx/app.h>
30#include <wx/string.h>
31#include <thread>
32
34static int ExecCommand(wxString *pIn, wxString *pOut, bool fromMain)
35{
36 if (auto pProject = ::GetActiveProject().lock()) {
37 CommandBuilder builder(*pProject, *pIn);
38 if (builder.WasValid())
39 {
40 OldStyleCommandPointer cmd = builder.GetCommand();
41
43 ev.SetCommand(cmd);
44
45 if (fromMain)
46 {
47 // Use SafelyProcessEvent, which stops exceptions, because this is
48 // expected to be reached from within the XLisp runtime
49 wxTheApp->SafelyProcessEvent(ev);
50 }
51 else
52 {
53 // Send the event to the main thread
54 wxTheApp->AddPendingEvent(ev);
55 }
56 }
57
58 // Wait for and retrieve the response
59 *pOut = builder.GetResponse();
60 }
61 else
62 *pOut = wxString{};
63
64 return 0;
65}
66
68static int ExecFromWorker(wxString *pIn, wxString *pOut)
69{
70 return ExecCommand(pIn, pOut, false);
71}
72
74static int ExecFromMain(wxString *pIn, wxString *pOut)
75{
76 return ExecCommand(pIn, pOut, true);
77}
78
81{
82 wxASSERT(scriptFn != NULL);
83
84 auto server = [](tpRegScriptServerFunc function)
85 {
86 while (true)
87 {
88 function(ExecFromWorker);
89 }
90 };
91
92 std::thread(server, scriptFn).detach();
93}
94
95void * ExecForLisp( char * pIn )
96{
97 wxString Str1(pIn);
98 wxString Str2;
99
100 ExecFromMain(&Str1, &Str2);
101
103}
AUDACITY_DLL_API std::weak_ptr< AudacityProject > GetActiveProject()
Handle changing of active project and keep global project pointer.
Headers and event table macros for AppCommandEvent.
Contains declaration of CommandBuilder class.
static int ExecCommand(wxString *pIn, wxString *pOut, bool fromMain)
This is the function which actually obeys one command.
void * ExecForLisp(char *pIn)
static int ExecFromWorker(wxString *pIn, wxString *pOut)
Executes a command in the worker (script) thread.
static int ExecFromMain(wxString *pIn, wxString *pOut)
Executes a command on the main (GUI) thread.
Contains declarations for ScriptCommandRelay.
void * nyq_reformat_aud_do_response(const wxString &Str)
Definition: Nyquist.cpp:3514
int(* tpRegScriptServerFunc)(tpExecScriptServerFunc pFn)
wxString Str2
An event 'envelope' for sending Command objects through the wxwidgets event loop.
void SetCommand(const OldStyleCommandPointer &cmd)
Store a pointer to a command object.
A type of factory for Commands of various sorts.
wxString GetResponse()
OldStyleCommandPointer GetCommand()
OldStyleCommandPointer is a unique_ptr to an OldStyleCommand.
static void StartScriptServer(tpRegScriptServerFunc scriptFn)
Starts the script server.