1
0
mirror of https://github.com/cookiengineer/audacity synced 2026-01-19 15:06:07 +01:00

Cleaning up lib-src

FileDialog goes into audacity/src/widgets and the mod-* directories go into
audacity/modules.

This leaves nothing in lib-src that isn't a 3rd-party libs or supporting
files.
This commit is contained in:
Leland Lucius
2020-05-24 16:21:26 -05:00
parent bf8387327e
commit 30dbdf40a9
73 changed files with 12788 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
set( TARGET mod-script-pipe )
set( TARGET_ROOT ${CMAKE_CURRENT_SOURCE_DIR} )
message( STATUS "========== Configuring ${TARGET} ==========" )
def_vars()
add_library( ${TARGET} MODULE )
list( APPEND SOURCES
PRIVATE
${TARGET_ROOT}/PipeServer.cpp
${TARGET_ROOT}/ScripterCallback.cpp
)
get_target_property( INCLUDES wxWidgets INTERFACE_INCLUDE_DIRECTORIES )
list( APPEND INCLUDES
PUBLIC
${TARGET_ROOT}
)
get_target_property( DEFINES wxWidgets INTERFACE_COMPILE_DEFINITIONS )
list( APPEND DEFINES
PRIVATE
BUILDING_SCRIPT_PIPE
# This is needed until the transition to cmake is complete and
# the Windows pragmas are removed from ScripterCallback.cpp.
# Without it, the wxWidgets "debug.h" will define __WXDEBUG__
# which then causes this module to emit library pragmas for the
# debug versions of wxWidgets...even if the build is for Release.
wxDEBUG_LEVEL=0
)
list( APPEND LOPTS
PRIVATE
$<$<PLATFORM_ID:Darwin>:-undefined dynamic_lookup>
)
list( APPEND LIBRARIES
PRIVATE
Audacity
$<$<PLATFORM_ID:Windows>:wxWidgets>
)
set_target_property_all( ${TARGET} LIBRARY_OUTPUT_DIRECTORY "${_DEST}/modules" )
set_target_properties( ${TARGET}
PROPERTIES
PREFIX ""
FOLDER "lib-src"
)
organize_source( "${TARGET_ROOT}" "" "${SOURCES}" )
target_sources( ${TARGET} PRIVATE ${SOURCES} )
target_compile_definitions( ${TARGET} PRIVATE ${DEFINES} )
target_include_directories( ${TARGET} PRIVATE ${INCLUDES} )
target_link_options( ${TARGET} PRIVATE ${LOPTS} )
target_link_libraries( ${TARGET} PRIVATE ${LIBRARIES} )

View File

@@ -0,0 +1,206 @@
#if defined(WIN32)
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
const int nBuff = 1024;
extern "C" int DoSrv( char * pIn );
extern "C" int DoSrvMore( char * pOut, int nMax );
void PipeServer()
{
HANDLE hPipeToSrv;
HANDLE hPipeFromSrv;
static const TCHAR pipeNameToSrv[] = _T("\\\\.\\pipe\\ToSrvPipe");
hPipeToSrv = CreateNamedPipe(
pipeNameToSrv ,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS,
PIPE_UNLIMITED_INSTANCES,
nBuff,
nBuff,
50,//Timeout - always send straight away.
NULL);
if( hPipeToSrv == INVALID_HANDLE_VALUE)
return;
static const TCHAR pipeNameFromSrv[] = __T("\\\\.\\pipe\\FromSrvPipe");
hPipeFromSrv = CreateNamedPipe(
pipeNameFromSrv ,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS,
PIPE_UNLIMITED_INSTANCES,
nBuff,
nBuff,
50,//Timeout - always send straight away.
NULL);
if( hPipeFromSrv == INVALID_HANDLE_VALUE)
return;
BOOL bConnected;
BOOL bSuccess;
DWORD cbBytesRead;
DWORD cbBytesWritten;
CHAR chRequest[ nBuff ];
CHAR chResponse[ nBuff ];
int jj=0;
for(;;)
{
// open to (incoming) pipe first.
printf( "Obtaining pipe\n" );
bConnected = ConnectNamedPipe(hPipeToSrv, NULL) ?
TRUE : (GetLastError()==ERROR_PIPE_CONNECTED );
printf( "Obtained to-srv %i\n", bConnected );
// open from (outgoing) pipe second. This could block if there is no reader.
bConnected = ConnectNamedPipe(hPipeFromSrv, NULL) ?
TRUE : (GetLastError()==ERROR_PIPE_CONNECTED );
printf( "Obtained from-srv %i\n", bConnected );
if( bConnected )
{
for(;;)
{
printf( "About to read\n" );
bSuccess = ReadFile( hPipeToSrv, chRequest, nBuff, &cbBytesRead, NULL);
chRequest[ cbBytesRead] = '\0';
if( !bSuccess || cbBytesRead==0 )
break;
printf( "Rxd %s\n", chRequest );
DoSrv( chRequest );
jj++;
while( true )
{
int nWritten = DoSrvMore( chResponse, nBuff );
if( nWritten <= 1 )
break;
WriteFile( hPipeFromSrv, chResponse, nWritten-1, &cbBytesWritten, NULL);
}
//FlushFileBuffers( hPipeFromSrv );
}
FlushFileBuffers( hPipeToSrv );
DisconnectNamedPipe( hPipeToSrv );
FlushFileBuffers( hPipeFromSrv );
DisconnectNamedPipe( hPipeFromSrv );
break;
}
else
{
CloseHandle( hPipeToSrv );
CloseHandle( hPipeFromSrv );
}
}
CloseHandle( hPipeToSrv );
CloseHandle( hPipeFromSrv );
}
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
const char fifotmpl[] = "/tmp/audacity_script_pipe.%s.%d";
const int nBuff = 1024;
extern "C" int DoSrv( char * pIn );
extern "C" int DoSrvMore( char * pOut, int nMax );
void PipeServer()
{
FILE *fromFifo = NULL;
FILE *toFifo = NULL;
int rc;
char buf[nBuff];
char toFifoName[nBuff];
char fromFifoName[nBuff];
sprintf(toFifoName, fifotmpl, "to", getuid());
sprintf(fromFifoName, fifotmpl, "from", getuid());
unlink(toFifoName);
unlink(fromFifoName);
// TODO avoid symlink security issues?
rc = mkfifo(fromFifoName, S_IRWXU) & mkfifo(toFifoName, S_IRWXU);
if (rc < 0)
{
perror("Unable to create fifos");
printf("Ignoring...");
// return;
}
// open to (incoming) pipe first.
toFifo = fopen(toFifoName, "r");
if (toFifo == NULL)
{
perror("Unable to open fifo to server from script");
if (fromFifo != NULL)
fclose(fromFifo);
return;
}
// open from (outgoing) pipe second. This could block if there is no reader.
fromFifo = fopen(fromFifoName, "w");
if (fromFifo == NULL)
{
perror("Unable to open fifo from server to script");
return;
}
while (fgets(buf, sizeof(buf), toFifo) != NULL)
{
int len = strlen(buf);
if (len <= 1)
{
continue;
}
buf[len - 1] = '\0';
printf("Server received %s\n", buf);
DoSrv(buf);
while (true)
{
len = DoSrvMore(buf, nBuff);
if (len <= 1)
{
break;
}
printf("Server sending %s",buf);
// len - 1 because we do not send the null character
fwrite(buf, 1, len - 1, fromFifo);
}
fflush(fromFifo);
}
printf("Read failed on fifo, quitting\n");
if (toFifo != NULL)
fclose(toFifo);
if (fromFifo != NULL)
fclose(fromFifo);
unlink(toFifoName);
unlink(fromFifoName);
}
#endif

View File

@@ -0,0 +1,197 @@
// ScripterCallback.cpp :
//
// A loadable module that connects a windows named pipe
// to a registered service function that is able to
// process a single command at a time.
//
// The service function is provided by the application
// and not by libscript. mod_script_pipe was developed for
// Audacity. Because it forwards commands
// rather than handling them itself it can be used in
// other projects too.
//
// Enabling other programs to connect to Audacity via a pipe is a potential
// security risk. Use at your own risk.
#include <wx/wx.h>
#include "ScripterCallback.h"
#include "../../src/Audacity.h"
/*
There are several functions that can be used in a GUI module.
//#define versionFnName "GetVersionString"
If the version is wrong, the module will be rejected.
That is it will be loaded and then unloaded.
//#define ModuleDispatchName "ModuleDispatch"
The most useful function. See the example in this
file. It has several cases/options in it.
//#define scriptFnName "RegScriptServerFunc"
This function is run from a non gui thread. It was originally
created for the benefit of mod-script-pipe.
//#define mainPanelFnName "MainPanelFunc"
This function is the hijacking function, to take over Audacity
and replace the main project window with our own wxFrame.
*/
#ifdef _MSC_VER
#define DLL_API _declspec(dllexport)
#define DLL_IMPORT _declspec(dllimport)
#else
#define DLL_API __attribute__ ((visibility("default")))
#define DLL_IMPORT
#endif
typedef enum
{
ModuleInitialize,
ModuleTerminate,
AppInitialized,
AppQuiting,
ProjectInitialized,
ProjectClosing
} ModuleDispatchTypes;
extern void PipeServer();
typedef DLL_IMPORT int (*tpExecScriptServerFunc)( wxString * pIn, wxString * pOut);
static tpExecScriptServerFunc pScriptServerFn=NULL;
extern "C" {
DLL_API const wxChar * GetVersionString()
{
// Make sure that this version of the module requires the version
// of Audacity it is built with.
// For now the versions must match exactly for Audacity to
// agree to load the module.
return AUDACITY_VERSION_STRING;
}
extern int DLL_API ModuleDispatch(ModuleDispatchTypes type);
// ModuleDispatch
// is called by Audacity to initialize/terminate the module
// We don't (yet) do anything in this, since we have a special function for the scripter
// all we need to do is return 1.
int ModuleDispatch(ModuleDispatchTypes type){
switch (type){
case AppInitialized:{
}
break;
case AppQuiting: {
}
break;
case ProjectInitialized: {
}
break;
default:
break;
}
return 1;
}
// And here is our special registration function.
int DLL_API RegScriptServerFunc( tpExecScriptServerFunc pFn )
{
if( pFn )
{
pScriptServerFn = pFn;
PipeServer();
}
return 4;
}
wxString Str2;
wxArrayString aStr;
unsigned int currentLine;
size_t currentPosition;
// Send the received command to Audacity and build an array of response lines.
// The response lines can be retrieved by calling DoSrvMore repeatedly.
int DoSrv(char *pIn)
{
// Interpret string as unicode.
// wxWidgets (now) uses unicode internally.
// Scripts must send unicode strings (if going beyond 7-bit ASCII).
// Important for filenames in commands.
wxString Str1(pIn, wxConvUTF8);
Str1.Replace( wxT("\r"), wxT(""));
Str1.Replace( wxT("\n"), wxT(""));
Str2 = wxEmptyString;
(*pScriptServerFn)( &Str1 , &Str2);
Str2 += wxT('\n');
size_t outputLength = Str2.Length();
aStr.Clear();
size_t iStart = 0;
size_t i;
for(i = 0; i < outputLength; ++i)
{
if( Str2[i] == wxT('\n') )
{
aStr.Add( Str2.Mid( iStart, i-iStart) + wxT("\n") );
iStart = i+1;
}
}
currentLine = 0;
currentPosition = 0;
return 1;
}
size_t smin(size_t a, size_t b) { return a < b ? a : b; }
// Write up to nMax characters of the prepared (by DoSrv) response lines.
// Returns the number of characters sent, including null.
// Zero returned if and only if there's nothing else to send.
int DoSrvMore(char *pOut, size_t nMax)
{
wxASSERT(currentLine >= 0);
wxASSERT(currentPosition >= 0);
size_t totalLines = aStr.GetCount();
while (currentLine < totalLines)
{
wxString lineString = aStr[currentLine];
size_t lineLength = lineString.Length();
size_t charsLeftInLine = lineLength - currentPosition;
wxASSERT(charsLeftInLine >= 0);
if (charsLeftInLine == 0)
{
// Move to next line
++currentLine;
currentPosition = 0;
}
else
{
// Write as much of the rest of the line as will fit in the buffer
size_t charsToWrite = smin(charsLeftInLine, nMax - 1);
memcpy(pOut,
lineString.Mid(currentPosition,
currentPosition + charsToWrite).mb_str(),
charsToWrite);
pOut[charsToWrite] = '\0';
currentPosition += charsToWrite;
// Need to cast to prevent compiler warnings
int charsWritten = static_cast<int>(charsToWrite + 1);
// (Check cast was safe)
wxASSERT(static_cast<size_t>(charsWritten) == charsToWrite + 1);
return charsWritten;
}
}
return 0;
}
} // End extern "C"

View File

@@ -0,0 +1,39 @@
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the LIBSCRIPT_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// SCRIPT_PIPE_DLL_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
/* Magic for dynamic library import and export. This is unfortunately
* compiler-specific because there isn't a standard way to do it. Currently it
* works with the Visual Studio compiler for windows, and for GCC 4+. Anything
* else gets all symbols made public, which gets messy */
/* The Visual Studio implementation */
#ifdef _MSC_VER
#define SCRIPT_PIPE_DLL_IMPORT _declspec(dllimport)
#ifdef BUILDING_SCRIPT_PIPE
#define SCRIPT_PIPE_DLL_API _declspec(dllexport)
#elif _DLL
#define SCRIPT_PIPE_DLL_API _declspec(dllimport)
#else
#define AUDACITY_DLL_API
#endif
#endif //_MSC_VER
/* The GCC implementation */
#ifdef CC_HASVISIBILITY // this is provided by the configure script, is only
// enabled for suitable GCC versions
/* The incantation is a bit weird here because it uses ELF symbol stuff. If we
* make a symbol "default" it makes it visible (for import or export). Making it
* "hidden" means it is invisible outside the shared object. */
#define SCRIPT_PIPE_DLL_IMPORT __attribute__((visibility("default")))
#ifdef BUILDING_SCRIPT_PIPE
#define SCRIPT_PIPE_DLL_API __attribute__((visibility("default")))
#else
#define SCRIPT_PIPE_DLL_API __attribute__((visibility("default")))
#endif
#endif