zynaddsubfx

ZynAddSubFX open source synthesizer
Log | Files | Refs | Submodules | LICENSE

commit 23aa791488381a4958afebdefa9cdd367b2f00ea
parent 24777fd9d0c94e823020c9ec62a537f04ff406f3
Author: Johannes Lorenz <johannes89@ist-einmalig.de>
Date:   Sat, 18 Jul 2015 05:44:21 +0200

Moved TmpFileMgr out of MiddleWare.

Diffstat:
MTODO.txt | 2+-
Msrc/Misc/CMakeLists.txt | 1+
Msrc/Misc/Config.h | 3++-
Msrc/Misc/MiddleWare.cpp | 112+++----------------------------------------------------------------------------
Asrc/Misc/TmpFileMgr.cpp | 114+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/Misc/TmpFileMgr.h | 18++++++++++++++++++
6 files changed, 139 insertions(+), 111 deletions(-)

diff --git a/TODO.txt b/TODO.txt @@ -11,7 +11,7 @@ in order to avoid denormalisation*/ (8) src/Params/PresetsStore.h:extern PresetsStore presetsstore; Status: -(1) +(1) will be fixed by fundamental (2) will be fixed with fftw3 (?), until then maybe use separate mutex? (3) will be fixed by fundamental (4) diff --git a/src/Misc/CMakeLists.txt b/src/Misc/CMakeLists.txt @@ -14,6 +14,7 @@ set(zynaddsubfx_misc_SRCS Misc/MiddleWare.cpp Misc/PresetExtractor.cpp Misc/Allocator.cpp + Misc/TmpFileMgr.cpp ) diff --git a/src/Misc/Config.h b/src/Misc/Config.h @@ -32,8 +32,9 @@ namespace rtosc struct Ports; } -struct oss_devs_t +class oss_devs_t { +public: char *linux_wave_out, *linux_seq_in; }; diff --git a/src/Misc/MiddleWare.cpp b/src/Misc/MiddleWare.cpp @@ -11,7 +11,6 @@ #include <lo/lo.h> #include <unistd.h> -#include <dirent.h> #include "../UI/Connection.h" #include "../UI/Fl_Osc_Interface.h" @@ -19,6 +18,7 @@ #include <map> #include "Util.h" +#include "TmpFileMgr.h" #include "Master.h" #include "Part.h" #include "PresetExtractor.h" @@ -428,13 +428,12 @@ struct ParamStore }; /* Implementation */ -class MiddleWareImpl +class MiddleWareImpl : TmpFileMgr { - static constexpr const char* tmp_nam_prefix = "/tmp/zynaddsubfx_"; MiddleWare *parent; //Detect if the name of the process is 'zynaddsubfx' - bool isPlugin() + bool isPlugin() const { std::string proc_file = "/proc/" + to_s(getpid()) + "/comm"; std::ifstream ifs(proc_file); @@ -446,111 +445,6 @@ class MiddleWareImpl return true; } - //! returns file name to where UDP port is saved - std::string get_tmp_nam() const - { - return tmp_nam_prefix + to_s(getpid()); - } - - void create_tmp_file(unsigned server_port) - { - std::string tmp_nam = get_tmp_nam(); - if(0 == access(tmp_nam.c_str(), F_OK)) { - fprintf(stderr, "Error: Cannot overwrite file %s. " - "You should probably remove it.", tmp_nam.c_str()); - exit(EXIT_FAILURE); - } - FILE* tmp_fp = fopen(tmp_nam.c_str(), "w"); - if(!tmp_fp) - fprintf(stderr, "Warning: could not create new file %s.\n", - tmp_nam.c_str()); - else - fprintf(tmp_fp, "%u", server_port); - fclose(tmp_fp); - } - - void clean_up_tmp_nams() const - { - DIR *dir; - struct dirent *entry; - if ((dir = opendir ("/tmp/")) != nullptr) - { - while ((entry = readdir (dir)) != nullptr) - { - std::string name = std::string("/tmp/") + entry->d_name; - if(!name.compare(0, strlen(tmp_nam_prefix),tmp_nam_prefix)) - { - std::string pid = name.substr(strlen(tmp_nam_prefix)); - std::string proc_file = "/proc/" + std::move(pid) + - "/comm"; - - std::ifstream ifs(proc_file); - bool remove = false; - - if(!ifs.good()) - { - fprintf(stderr, "Note: trying to remove %s - the " - "process does not exist anymore.\n", - name.c_str()); - remove = true; - } - else - { - std::string comm_name; - ifs >> comm_name; - if(comm_name == "zynaddsubfx") - fprintf(stderr, "Note: detected running " - "zynaddsubfx with PID %s.\n", - name.c_str() + strlen(tmp_nam_prefix)); - else { - fprintf(stderr, "Note: trying to remove %s - the " - "PID is owned by\n another " - "process: %s\n", - name.c_str(), comm_name.c_str()); - remove = true; - } - } - - - if(remove) - { - // make sure this file contains only one unsigned - unsigned udp_port; - std::ifstream ifs2(name); - if(!ifs2.good()) - fprintf(stderr, "Warning: could not open %s.\n", - name.c_str()); - else - { - ifs2 >> udp_port; - if(ifs.good()) - fprintf(stderr, "Warning: could not remove " - "%s, \n it has not been " - "written by zynaddsubfx\n", - name.c_str()); - else - { - if(std::remove(name.c_str()) != 0) - fprintf(stderr, "Warning: can not remove " - "%s.\n", name.c_str()); - } - } - } - - /* one might want to connect to zyn here, - but it is not necessary: - lo_address la = lo_address_new(nullptr, udp_port.c_str()); - if(lo_send(la, "/echo", nullptr) <= 0) - fputs("Note: found crashed file %s\n", stderr); - lo_address_free(la);*/ - } - } - closedir (dir); - } else { - fputs("Warning: can not read /tmp.\n", stderr); - } - } - Config* const config; public: diff --git a/src/Misc/TmpFileMgr.cpp b/src/Misc/TmpFileMgr.cpp @@ -0,0 +1,114 @@ +#include <cstdlib> +#include <cstring> +#include <string> +#include <cstdio> +#include <fstream> +#include <unistd.h> +#include <dirent.h> + +#include "Util.h" +#include "TmpFileMgr.h" + +std::string TmpFileMgr::get_tmp_nam() const +{ + return tmp_nam_prefix + to_s(getpid()); +} + +void TmpFileMgr::create_tmp_file(unsigned server_port) const +{ + std::string tmp_nam = get_tmp_nam(); + if(0 == access(tmp_nam.c_str(), F_OK)) { + fprintf(stderr, "Error: Cannot overwrite file %s. " + "You should probably remove it.", tmp_nam.c_str()); + exit(EXIT_FAILURE); + } + FILE* tmp_fp = fopen(tmp_nam.c_str(), "w"); + if(!tmp_fp) + fprintf(stderr, "Warning: could not create new file %s.\n", + tmp_nam.c_str()); + else + fprintf(tmp_fp, "%u", server_port); + fclose(tmp_fp); +} + +void TmpFileMgr::clean_up_tmp_nams() const +{ + DIR *dir; + struct dirent *entry; + if ((dir = opendir ("/tmp/")) != nullptr) + { + while ((entry = readdir (dir)) != nullptr) + { + std::string name = std::string("/tmp/") + entry->d_name; + if(!name.compare(0, strlen(tmp_nam_prefix),tmp_nam_prefix)) + { + std::string pid = name.substr(strlen(tmp_nam_prefix)); + std::string proc_file = "/proc/" + std::move(pid) + + "/comm"; + + std::ifstream ifs(proc_file); + bool remove = false; + + if(!ifs.good()) + { + fprintf(stderr, "Note: trying to remove %s - the " + "process does not exist anymore.\n", + name.c_str()); + remove = true; + } + else + { + std::string comm_name; + ifs >> comm_name; + if(comm_name == "zynaddsubfx") + fprintf(stderr, "Note: detected running " + "zynaddsubfx with PID %s.\n", + name.c_str() + strlen(tmp_nam_prefix)); + else { + fprintf(stderr, "Note: trying to remove %s - the " + "PID is owned by\n another " + "process: %s\n", + name.c_str(), comm_name.c_str()); + remove = true; + } + } + + + if(remove) + { + // make sure this file contains only one unsigned + unsigned udp_port; + std::ifstream ifs2(name); + if(!ifs2.good()) + fprintf(stderr, "Warning: could not open %s.\n", + name.c_str()); + else + { + ifs2 >> udp_port; + if(ifs.good()) + fprintf(stderr, "Warning: could not remove " + "%s, \n it has not been " + "written by zynaddsubfx\n", + name.c_str()); + else + { + if(std::remove(name.c_str()) != 0) + fprintf(stderr, "Warning: can not remove " + "%s.\n", name.c_str()); + } + } + } + + /* one might want to connect to zyn here, + but it is not necessary: + lo_address la = lo_address_new(nullptr, udp_port.c_str()); + if(lo_send(la, "/echo", nullptr) <= 0) + fputs("Note: found crashed file %s\n", stderr); + lo_address_free(la);*/ + } + } + closedir (dir); + } else { + fputs("Warning: can not read /tmp.\n", stderr); + } +} diff --git a/src/Misc/TmpFileMgr.h b/src/Misc/TmpFileMgr.h @@ -0,0 +1,18 @@ + +/** + This file provides routines for using zyn's tmp files. +*/ +class TmpFileMgr +{ + static constexpr const char* tmp_nam_prefix = "/tmp/zynaddsubfx_"; + +public: + //! returns file name to where UDP port is saved + std::string get_tmp_nam() const; + + //! creates a tmp file with given UDP port information + void create_tmp_file(unsigned server_port) const; + + //! cleans up as many tmp files as possible + void clean_up_tmp_nams() const; +};