|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- /*
- * Carla Bridge UI
- * Copyright (C) 2011-2019 Filipe Coelho <falktx@falktx.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * For a full copy of the GNU General Public License see the doc/GPL.txt file.
- */
-
- #include "CarlaBridgeFormat.hpp"
- #include "CarlaBridgeToolkit.hpp"
-
- #include "CarlaBase64Utils.hpp"
- #include "CarlaProcessUtils.hpp"
-
- #include "CarlaMIDI.h"
-
- // needed for atom-util
- #ifndef nullptr
- # undef NULL
- # define NULL nullptr
- #endif
-
- #include "lv2/atom-util.h"
-
- CARLA_BRIDGE_UI_START_NAMESPACE
-
- // ---------------------------------------------------------------------
-
- CarlaBridgeFormat::CarlaBridgeFormat() noexcept
- : CarlaPipeClient(),
- fQuitReceived(false),
- fGotOptions(false),
- fLastMsgTimer(-1),
- fToolkit(nullptr),
- fLib(nullptr),
- fLibFilename()
- {
- carla_debug("CarlaBridgeFormat::CarlaBridgeFormat()");
-
- try {
- fToolkit = CarlaBridgeToolkit::createNew(this);
- } CARLA_SAFE_EXCEPTION_RETURN("CarlaBridgeToolkit::createNew",);
- }
-
- CarlaBridgeFormat::~CarlaBridgeFormat() /*noexcept*/
- {
- carla_debug("CarlaBridgeFormat::~CarlaBridgeFormat()");
-
- if (isPipeRunning() && ! fQuitReceived)
- writeExitingMessageAndWait();
-
- if (fLib != nullptr)
- {
- lib_close(fLib);
- fLib = nullptr;
- }
-
- if (fToolkit != nullptr)
- {
- fToolkit->quit();
- delete fToolkit;
- fToolkit = nullptr;
- }
-
- closePipeClient();
- }
-
- // ---------------------------------------------------------------------
-
- bool CarlaBridgeFormat::libOpen(const char* const filename) noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(fLib == nullptr, false);
-
- fLib = lib_open(filename);
-
- if (fLib != nullptr)
- {
- fLibFilename = filename;
- return true;
- }
-
- return false;
- }
-
- void* CarlaBridgeFormat::libSymbol(const char* const symbol) const noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(fLib != nullptr, nullptr);
-
- return lib_symbol<void*>(fLib, symbol);
- }
-
- const char* CarlaBridgeFormat::libError() const noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(fLibFilename.isNotEmpty(), nullptr);
-
- return lib_error(fLibFilename);
- }
-
- // ---------------------------------------------------------------------
-
- bool CarlaBridgeFormat::msgReceived(const char* const msg) noexcept
- {
- carla_debug("CarlaBridgeFormat::msgReceived(\"%s\")", msg);
-
- if (! fGotOptions && std::strcmp(msg, "urid") != 0 && std::strcmp(msg, "uiOptions") != 0)
- {
- carla_stderr2("CarlaBridgeFormat::msgReceived(\"%s\") - invalid message while waiting for options", msg);
- return true;
- }
-
- if (fLastMsgTimer > 0)
- --fLastMsgTimer;
-
- if (std::strcmp(msg, "control") == 0)
- {
- uint32_t index;
- float value;
-
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
-
- dspParameterChanged(index, value);
- return true;
- }
-
- if (std::strcmp(msg, "program") == 0)
- {
- uint32_t index;
-
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
-
- dspProgramChanged(index);
- return true;
- }
-
- if (std::strcmp(msg, "midiprogram") == 0)
- {
- uint32_t bank, program;
-
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(bank), true);
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(program), true);
-
- dspMidiProgramChanged(bank, program);
- return true;
- }
-
- if (std::strcmp(msg, "configure") == 0)
- {
- const char* key;
- const char* value;
-
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key), true);
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value), true);
-
- dspStateChanged(key, value);
-
- delete[] key;
- delete[] value;
- return true;
- }
-
- if (std::strcmp(msg, "note") == 0)
- {
- bool onOff;
- uint8_t channel, note, velocity;
-
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(onOff), true);
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(channel), true);
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(note), true);
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(velocity), true);
-
- dspNoteReceived(onOff, channel, note, velocity);
- return true;
- }
-
- if (std::strcmp(msg, "atom") == 0)
- {
- uint32_t index, atomTotalSize;
- const char* base64atom;
-
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(atomTotalSize), true);
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(base64atom), true);
-
- std::vector<uint8_t> chunk(carla_getChunkFromBase64String(base64atom));
- delete[] base64atom;
- CARLA_SAFE_ASSERT_RETURN(chunk.size() >= sizeof(LV2_Atom), true);
-
- #ifdef CARLA_PROPER_CPP11_SUPPORT
- const LV2_Atom* const atom((const LV2_Atom*)chunk.data());
- #else
- const LV2_Atom* const atom((const LV2_Atom*)&chunk.front());
- #endif
- const uint32_t atomTotalSizeCheck(lv2_atom_total_size(atom));
-
- CARLA_SAFE_ASSERT_RETURN(atomTotalSizeCheck == atomTotalSize, true);
- CARLA_SAFE_ASSERT_RETURN(atomTotalSizeCheck == chunk.size(), true);
-
- dspAtomReceived(index, atom);
- return true;
- }
-
- if (std::strcmp(msg, "urid") == 0)
- {
- uint32_t urid;
- const char* uri;
-
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(urid), true);
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(uri), true);
-
- if (urid != 0)
- dspURIDReceived(urid, uri);
-
- delete[] uri;
- return true;
- }
-
- if (std::strcmp(msg, "uiOptions") == 0)
- {
- double sampleRate;
- bool useTheme, useThemeColors;
- const char* windowTitle;
- uint64_t transientWindowId;
-
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsDouble(sampleRate), true);
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useTheme), true);
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useThemeColors), true);
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(windowTitle), true);
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(transientWindowId), true);
-
- fGotOptions = true;
- uiOptionsChanged(sampleRate, useTheme, useThemeColors, windowTitle, static_cast<uintptr_t>(transientWindowId));
-
- delete[] windowTitle;
- return true;
- }
-
- CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, true);
-
- if (std::strcmp(msg, "show") == 0)
- {
- fToolkit->show();
- return true;
- }
-
- if (std::strcmp(msg, "focus") == 0)
- {
- fToolkit->focus();
- return true;
- }
-
- if (std::strcmp(msg, "hide") == 0)
- {
- fToolkit->hide();
- return true;
- }
-
- if (std::strcmp(msg, "quit") == 0)
- {
- fQuitReceived = true;
- fToolkit->quit();
- delete fToolkit;
- fToolkit = nullptr;
- return true;
- }
-
- if (std::strcmp(msg, "uiTitle") == 0)
- {
- const char* title;
-
- CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(title), true);
-
- fToolkit->setTitle(title);
-
- delete[] title;
- return true;
- }
-
- carla_stderr("CarlaBridgeFormat::msgReceived : %s", msg);
- return false;
- }
-
- // ---------------------------------------------------------------------
-
- bool CarlaBridgeFormat::init(const int argc, const char* argv[])
- {
- CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, false);
-
- if (argc == 7)
- {
- if (! initPipeClient(argv))
- return false;
-
- fLastMsgTimer = 0;
-
- // wait for ui options
- for (; ++fLastMsgTimer < 50 && ! fGotOptions;)
- {
- idlePipe(true);
- carla_msleep(20);
- }
-
- if (! fGotOptions)
- {
- carla_stderr2("CarlaBridgeFormat::init() - did not get options on time, quitting...");
- writeExitingMessageAndWait();
- closePipeClient();
- return false;
- }
- }
-
- if (! fToolkit->init(argc, argv))
- {
- if (argc == 7)
- closePipeClient();
- return false;
- }
-
- return true;
- }
-
- void CarlaBridgeFormat::exec(const bool showUI)
- {
- CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr,);
-
- carla_terminateProcessOnParentExit(true);
- fToolkit->exec(showUI);
- }
-
- // ---------------------------------------------------------------------
-
- CARLA_BRIDGE_UI_END_NAMESPACE
-
- #include "CarlaPipeUtils.cpp"
-
- // ---------------------------------------------------------------------
|