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 <thread>
31
33static int ExecCommand(wxString *pIn, wxString *pOut, bool fromMain)
34{
35 if (auto pProject = ::GetActiveProject().lock()) {
36 CommandBuilder builder(*pProject, *pIn);
37 if (builder.WasValid())
38 {
39 OldStyleCommandPointer cmd = builder.GetCommand();
40
42 ev.SetCommand(cmd);
43
44 if (fromMain)
45 {
46 // Use SafelyProcessEvent, which stops exceptions, because this is
47 // expected to be reached from within the XLisp runtime
48 wxTheApp->SafelyProcessEvent(ev);
49 }
50 else
51 {
52 // Send the event to the main thread
53 wxTheApp->AddPendingEvent(ev);
54 }
55 }
56
57 // Wait for and retrieve the response
58 *pOut = builder.GetResponse();
59 }
60 else
61 *pOut = wxString{};
62
63 return 0;
64}
65
67static int ExecFromWorker(wxString *pIn, wxString *pOut)
68{
69 return ExecCommand(pIn, pOut, false);
70}
71
73static int ExecFromMain(wxString *pIn, wxString *pOut)
74{
75 return ExecCommand(pIn, pOut, true);
76}
77
80{
81 wxASSERT(scriptFn != NULL);
82
83 auto server = [](tpRegScriptServerFunc function)
84 {
85 while (true)
86 {
87 function(ExecFromWorker);
88 }
89 };
90
91 std::thread(server, scriptFn).detach();
92}
93
94void * ExecForLisp( char * pIn )
95{
96 wxString Str1(pIn);
97 wxString Str2;
98
99 ExecFromMain(&Str1, &Str2);
100
102}
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:3533
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.