Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

carla_shared.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * Common Carla code
  3. * Copyright (C) 2011-2019 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "carla_shared.hpp"
  18. //---------------------------------------------------------------------------------------------------------------------
  19. // Imports (Global)
  20. #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  21. # pragma GCC diagnostic push
  22. # pragma GCC diagnostic ignored "-Wconversion"
  23. # pragma GCC diagnostic ignored "-Weffc++"
  24. # pragma GCC diagnostic ignored "-Wsign-conversion"
  25. #endif
  26. #include <QtGui/QFontMetrics>
  27. #include <QtWidgets/QFileDialog>
  28. #include <QtWidgets/QGridLayout>
  29. #include <QtWidgets/QLineEdit>
  30. #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  31. # pragma GCC diagnostic pop
  32. #endif
  33. //---------------------------------------------------------------------------------------------------------------------
  34. #ifdef CARLA_OS_UNIX
  35. # include <signal.h>
  36. #endif
  37. //---------------------------------------------------------------------------------------------------------------------
  38. // Imports (Custom)
  39. #include "carla_host.hpp"
  40. #include "CarlaUtils.h"
  41. #include "CarlaMathUtils.hpp"
  42. //---------------------------------------------------------------------------------------------------------------------
  43. // Global Carla object
  44. CarlaObject::CarlaObject() noexcept
  45. : host(nullptr),
  46. gui(nullptr),
  47. nogui(false),
  48. term(false) {}
  49. CarlaObject gCarla;
  50. //---------------------------------------------------------------------------------------------------------------------
  51. // Get Icon from user theme, using our own as backup (Oxygen)
  52. QIcon getIcon(const QString icon, const int size)
  53. {
  54. return QIcon::fromTheme(icon, QIcon(QString(":/%1x%1/%2.png").arg(size).arg(icon)));
  55. }
  56. //---------------------------------------------------------------------------------------------------------------------
  57. // Handle some basic command-line arguments shared between all carla variants
  58. QString handleInitialCommandLineArguments(const int argc, char* argv[])
  59. {
  60. static const QStringList listArgsNoGUI = { "-n", "--n", "-no-gui", "--no-gui", "-nogui", "--nogui" };
  61. static const QStringList listArgsHelp = { "-h", "--h", "-help", "--help" };
  62. static const QStringList listArgsVersion = { "-v", "--v", "-version", "--version" };
  63. QString initName(argv[0]); // = os.path.basename(file) if (file is not None and os.path.dirname(file) in PATH) else sys.argv[0]
  64. // libPrefix = None
  65. for (int i=1; i<argc; ++i)
  66. {
  67. const QString arg(argv[i]);
  68. if (arg.startsWith("--with-appname="))
  69. {
  70. // initName = os.path.basename(arg.replace("--with-appname=", ""));
  71. }
  72. else if (arg.startsWith("--with-libprefix=") || arg == "--gdb")
  73. {
  74. pass();
  75. }
  76. else if (listArgsNoGUI.contains(arg))
  77. {
  78. gCarla.nogui = true;
  79. }
  80. else if (listArgsHelp.contains(arg))
  81. {
  82. carla_stdout("Usage: %s [OPTION]... [FILE|URL]", initName);
  83. carla_stdout("");
  84. carla_stdout(" where FILE can be a Carla project or preset file to be loaded, or URL if using Carla-Control");
  85. carla_stdout("");
  86. carla_stdout(" and OPTION can be one or more of the following:");
  87. carla_stdout("");
  88. carla_stdout(" --gdb \t Run Carla inside gdb.");
  89. carla_stdout(" -n,--no-gui \t Run Carla headless, don't show UI.");
  90. carla_stdout("");
  91. carla_stdout(" -h,--help \t Print this help text and exit.");
  92. carla_stdout(" -v,--version\t Print version information and exit.");
  93. carla_stdout("");
  94. std::exit(0);
  95. }
  96. else if (listArgsVersion.contains(arg))
  97. {
  98. /*
  99. QString pathBinaries, pathResources = getPaths();
  100. */
  101. carla_stdout("Using Carla version %s", CARLA_VERSION_STRING);
  102. /*
  103. carla_stdout(" Qt version: %s", QT_VERSION_STR);
  104. carla_stdout(" Binary dir: %s", pathBinaries.toUtf8());
  105. carla_stdout(" Resources dir: %s", pathResources.toUtf8());
  106. */
  107. std::exit(0);
  108. }
  109. }
  110. return initName;
  111. }
  112. //---------------------------------------------------------------------------------------------------------------------
  113. // Get initial project file (as passed in the command-line parameters)
  114. QString getInitialProjectFile(bool)
  115. {
  116. // TODO
  117. return "";
  118. }
  119. //---------------------------------------------------------------------------------------------------------------------
  120. // Get paths (binaries, resources)
  121. bool getPaths(QString& pathBinaries, QString& pathResources)
  122. {
  123. const QString libFolder(carla_get_library_folder());
  124. QDir dir(libFolder);
  125. // FIXME need to completely rework this in C++ mode, so check if all cases are valid
  126. if (libFolder.endsWith("bin"))
  127. {
  128. CARLA_SAFE_ASSERT_RETURN(dir.cd("resources"), false);
  129. pathBinaries = libFolder;
  130. pathResources = dir.absolutePath();
  131. return true;
  132. }
  133. else if (libFolder.endsWith("carla"))
  134. {
  135. for (int i=2; --i>=0;)
  136. {
  137. CARLA_SAFE_ASSERT_INT_RETURN(dir.cdUp(), i, false);
  138. CARLA_SAFE_ASSERT_INT_RETURN(dir.cd("share"), i, false);
  139. if (dir.exists())
  140. {
  141. CARLA_SAFE_ASSERT_INT_RETURN(dir.cd("carla"), i, false);
  142. CARLA_SAFE_ASSERT_INT_RETURN(dir.cd("resources"), i, false);
  143. pathBinaries = libFolder;
  144. pathResources = dir.absolutePath();
  145. return true;
  146. }
  147. }
  148. }
  149. return false;
  150. }
  151. //---------------------------------------------------------------------------------------------------------------------
  152. // Signal handler
  153. static void signalHandler(const int sig)
  154. {
  155. switch (sig)
  156. {
  157. case SIGINT:
  158. case SIGTERM:
  159. gCarla.term = true;
  160. if (gCarla.host != nullptr)
  161. emit gCarla.host->SignalTerminate();
  162. break;
  163. case SIGUSR1:
  164. if (gCarla.host != nullptr)
  165. emit gCarla.host->SignalSave();
  166. break;
  167. }
  168. }
  169. #ifdef CARLA_OS_WIN
  170. static BOOL WINAPI winSignalHandler(DWORD dwCtrlType) noexcept
  171. {
  172. if (dwCtrlType == CTRL_C_EVENT)
  173. {
  174. signalHandler(SIGINT);
  175. return TRUE;
  176. }
  177. return FALSE;
  178. }
  179. #endif
  180. void setUpSignals()
  181. {
  182. #if defined(CARLA_OS_UNIX)
  183. struct sigaction sig;
  184. carla_zeroStruct(sig);
  185. sig.sa_handler = signalHandler;
  186. sig.sa_flags = SA_RESTART;
  187. sigemptyset(&sig.sa_mask);
  188. sigaction(SIGTERM, &sig, nullptr);
  189. sigaction(SIGINT, &sig, nullptr);
  190. sigaction(SIGUSR1, &sig, nullptr);
  191. #elif defined(CARLA_OS_WIN)
  192. SetConsoleCtrlHandler(winSignalHandler, TRUE);
  193. #endif
  194. }
  195. //---------------------------------------------------------------------------------------------------------------------
  196. // QLineEdit and QPushButton combo
  197. QString getAndSetPath(QWidget* const parent, QLineEdit* const lineEdit)
  198. {
  199. const QCarlaString newPath = QFileDialog::getExistingDirectory(parent, parent->tr("Set Path"), lineEdit->text(), QFileDialog::ShowDirsOnly);
  200. if (newPath.isNotEmpty())
  201. lineEdit->setText(newPath);
  202. return newPath;
  203. }
  204. //---------------------------------------------------------------------------------------------------------------------
  205. // fill up a qlists from a C arrays
  206. void fillQStringListFromStringArray(QStringList& list, const char* const* const stringArray)
  207. {
  208. int count = 0;
  209. // count number of strings first
  210. for (; stringArray[count] != nullptr; ++count) {}
  211. // allocate list
  212. list.reserve(count);
  213. // fill in strings
  214. for (count = 0; stringArray[count] != nullptr; ++count)
  215. list.append(stringArray[count]);
  216. }
  217. void fillQDoubleListFromDoubleArray(QList<double>& list, const double* const doubleArray)
  218. {
  219. int count = 0;
  220. // count number of strings first
  221. for (; carla_isNotZero(doubleArray[count]); ++count) {}
  222. // allocate list
  223. list.reserve(count);
  224. // fill in strings
  225. for (count = 0; carla_isNotZero(doubleArray[count]); ++count)
  226. list.append(doubleArray[count]);
  227. }
  228. void fillQUIntListFromUIntArray(QList<uint>& list, const uint* const uintArray)
  229. {
  230. int count = 0;
  231. // count number of strings first
  232. for (; uintArray[count] != 0; ++count) {}
  233. // allocate list
  234. list.reserve(count);
  235. // fill in strings
  236. for (count = 0; uintArray[count] != 0; ++count)
  237. list.append(uintArray[count]);
  238. }
  239. //---------------------------------------------------------------------------------------------------------------------
  240. // Backwards-compatible horizontalAdvance/width call, depending on Qt version
  241. int fontMetricsHorizontalAdvance(const QFontMetrics& fm, const QString& s)
  242. {
  243. #if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
  244. return fm.horizontalAdvance(s);
  245. #else
  246. return fm.width(s);
  247. #endif
  248. }
  249. //---------------------------------------------------------------------------------------------------------------------
  250. // Check if a string array contains a string
  251. bool stringArrayContainsString(const char* const* const stringArray, const char* const string) noexcept
  252. {
  253. for (uint i=0; stringArray[i] != nullptr; ++i)
  254. {
  255. if (std::strcmp(stringArray[i], string) == 0)
  256. return true;
  257. }
  258. return false;
  259. }
  260. //---------------------------------------------------------------------------------------------------------------------
  261. // Get index of a QList<double> value
  262. int getIndexOfQDoubleListValue(const QList<double>& list, const double value)
  263. {
  264. if (list.size() > 0)
  265. {
  266. for (QList<double>::const_iterator n = list.cbegin(), e = list.cend(); n != e; ++n)
  267. if (carla_isEqual(*n, value))
  268. return int(n - list.cbegin());
  269. }
  270. return -1;
  271. }
  272. //---------------------------------------------------------------------------------------------------------------------
  273. // Check if two QList<double> instances match
  274. bool isQDoubleListEqual(const QList<double>& list1, const QList<double>& list2)
  275. {
  276. if (list1.size() != list2.size())
  277. return false;
  278. if (list1.isEmpty())
  279. return true;
  280. for (QList<double>::const_iterator l1n = list1.cbegin(), l2n = list2.cbegin(), l1e = list1.cend(); l1n != l1e; ++l1n, ++l2n)
  281. if (carla_isNotEqual(*l1n, *l2n))
  282. return false;
  283. return true;
  284. }
  285. //---------------------------------------------------------------------------------------------------------------------
  286. // Custom QMessageBox which resizes itself to fit text
  287. void QMessageBoxWithBetterWidth::showEvent(QShowEvent* const event)
  288. {
  289. const QFontMetrics metrics(fontMetrics());
  290. const QStringList lines(text().trimmed().split("\n") + informativeText().trimmed().split("\n"));
  291. if (lines.size() > 0)
  292. {
  293. int width = 0;
  294. for (const QString& line : lines)
  295. width = std::max(fontMetricsHorizontalAdvance(metrics, line), width);
  296. if (QGridLayout* const layout_ = dynamic_cast<QGridLayout*>(layout()))
  297. layout_->setColumnMinimumWidth(2, width + 12);
  298. }
  299. QMessageBox::showEvent(event);
  300. }
  301. //---------------------------------------------------------------------------------------------------------------------
  302. // Safer QSettings class, which does not throw if type mismatches
  303. bool QSafeSettings::valueBool(const QString key, const bool defaultValue) const
  304. {
  305. QVariant var(value(key, defaultValue));
  306. if (var.isNull())
  307. return defaultValue;
  308. CARLA_SAFE_ASSERT_RETURN(var.convert(QVariant::Bool), defaultValue);
  309. return var.isValid() ? var.toBool() : defaultValue;
  310. }
  311. Qt::CheckState QSafeSettings::valueCheckState(const QString key, const Qt::CheckState defaultValue) const
  312. {
  313. QVariant var(value(key, defaultValue));
  314. if (var.isNull())
  315. return defaultValue;
  316. CARLA_SAFE_ASSERT_RETURN(var.convert(QVariant::UInt), defaultValue);
  317. if (! var.isValid())
  318. return defaultValue;
  319. const uint value = var.toUInt();
  320. switch (value)
  321. {
  322. case Qt::Unchecked:
  323. case Qt::PartiallyChecked:
  324. case Qt::Checked:
  325. return static_cast<Qt::CheckState>(value);
  326. default:
  327. return defaultValue;
  328. }
  329. }
  330. int QSafeSettings::valueIntPositive(const QString key, const int defaultValue) const
  331. {
  332. CARLA_SAFE_ASSERT_INT(defaultValue >= 0, defaultValue);
  333. QVariant var(value(key, defaultValue));
  334. if (var.isNull())
  335. return defaultValue;
  336. CARLA_SAFE_ASSERT_RETURN(var.convert(QVariant::Int), defaultValue);
  337. CARLA_SAFE_ASSERT_RETURN(var.isValid(), defaultValue);
  338. const int value = var.toInt();
  339. CARLA_SAFE_ASSERT_RETURN(value >= 0, defaultValue);
  340. return value;
  341. }
  342. uint QSafeSettings::valueUInt(const QString key, const uint defaultValue) const
  343. {
  344. QVariant var(value(key, defaultValue));
  345. if (var.isNull())
  346. return defaultValue;
  347. CARLA_SAFE_ASSERT_RETURN(var.convert(QVariant::UInt), defaultValue);
  348. return var.isValid() ? var.toUInt() : defaultValue;
  349. }
  350. double QSafeSettings::valueDouble(const QString key, const double defaultValue) const
  351. {
  352. QVariant var(value(key, defaultValue));
  353. if (var.isNull())
  354. return defaultValue;
  355. CARLA_SAFE_ASSERT_RETURN(var.convert(QVariant::Double), defaultValue);
  356. return var.isValid() ? var.toDouble() : defaultValue;
  357. }
  358. QString QSafeSettings::valueString(const QString key, const QString defaultValue) const
  359. {
  360. QVariant var(value(key, defaultValue));
  361. if (var.isNull())
  362. return defaultValue;
  363. CARLA_SAFE_ASSERT_RETURN(var.convert(QVariant::String), defaultValue);
  364. return var.isValid() ? var.toString() : defaultValue;
  365. }
  366. QByteArray QSafeSettings::valueByteArray(const QString key, const QByteArray defaultValue) const
  367. {
  368. QVariant var(value(key, defaultValue));
  369. if (var.isNull())
  370. return defaultValue;
  371. CARLA_SAFE_ASSERT_RETURN(var.convert(QVariant::ByteArray), defaultValue);
  372. return var.isValid() ? var.toByteArray() : defaultValue;
  373. }
  374. QStringList QSafeSettings::valueStringList(const QString key, const QStringList defaultValue) const
  375. {
  376. QVariant var(value(key, defaultValue));
  377. if (var.isNull())
  378. return defaultValue;
  379. CARLA_SAFE_ASSERT_RETURN(var.convert(QVariant::StringList), defaultValue);
  380. return var.isValid() ? var.toStringList() : defaultValue;
  381. }
  382. //---------------------------------------------------------------------------------------------------------------------
  383. // Custom MessageBox
  384. int CustomMessageBox(QWidget* const parent,
  385. const QMessageBox::Icon icon,
  386. const QString title,
  387. const QString text,
  388. const QString extraText,
  389. const QMessageBox::StandardButtons buttons,
  390. const QMessageBox::StandardButton defButton)
  391. {
  392. QMessageBoxWithBetterWidth msgBox(parent);
  393. msgBox.setIcon(icon);
  394. msgBox.setWindowTitle(title);
  395. msgBox.setText(text);
  396. msgBox.setInformativeText(extraText);
  397. msgBox.setStandardButtons(buttons);
  398. msgBox.setDefaultButton(defButton);
  399. return msgBox.exec();
  400. }
  401. //---------------------------------------------------------------------------------------------------------------------