Collection of tools useful for audio production
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.

280 lines
6.5KB

  1. /*
  2. * Carla UI bridge code
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #include "carla_bridge_client.hpp"
  18. #include <QtCore/QSettings>
  19. #include <QtCore/QTimer>
  20. #include <QtCore/QTimerEvent>
  21. #ifdef BRIDGE_LV2_QT5
  22. # include <QtWidgets/QApplication>
  23. # include <QtWidgets/QMainWindow>
  24. # include <QtWidgets/QVBoxLayout>
  25. #else
  26. # include <QtGui/QApplication>
  27. # include <QtGui/QMainWindow>
  28. # include <QtGui/QVBoxLayout>
  29. #endif
  30. #ifdef Q_WS_X11
  31. # include <QtGui/QX11EmbedContainer>
  32. #endif
  33. CARLA_BRIDGE_START_NAMESPACE
  34. static int qargc = 0;
  35. static char* qargv[0] = {};
  36. class BridgeApplication : public QApplication
  37. {
  38. public:
  39. BridgeApplication()
  40. : QApplication(qargc, qargv, true)
  41. {
  42. msgTimer = 0;
  43. m_client = nullptr;
  44. }
  45. void exec(CarlaClient* const client)
  46. {
  47. m_client = client;
  48. msgTimer = startTimer(50);
  49. QApplication::exec();
  50. }
  51. protected:
  52. void timerEvent(QTimerEvent* const event)
  53. {
  54. if (event->timerId() == msgTimer)
  55. {
  56. if (m_client && ! m_client->oscIdle())
  57. killTimer(msgTimer);
  58. }
  59. QApplication::timerEvent(event);
  60. }
  61. private:
  62. int msgTimer;
  63. CarlaClient* m_client;
  64. };
  65. // -------------------------------------------------------------------------
  66. class CarlaToolkitQt: public CarlaToolkit
  67. {
  68. public:
  69. CarlaToolkitQt(const char* const title)
  70. : CarlaToolkit(title),
  71. #if defined(BRIDGE_LV2_QT4)
  72. settings("Cadence", "Carla-Qt4UIs")
  73. #elif defined(BRIDGE_LV2_QT5)
  74. settings("Cadence", "Carla-Qt5UIs")
  75. #elif defined(BRIDGE_LV2_X11) || defined(BRIDGE_VST_X11)
  76. settings("Cadence", "Carla-X11UIs")
  77. #elif defined(BRIDGE_LV2_HWND) || defined(BRIDGE_VST_HWND)
  78. settings("Cadence", "Carla-HWNDUIs")
  79. #else
  80. settings("Cadence", "Carla-UIs")
  81. #endif
  82. {
  83. qDebug("CarlaToolkitQt::CarlaToolkitQ4(%s)", title);
  84. app = nullptr;
  85. window = nullptr;
  86. #ifdef Q_WS_X11
  87. x11Container = nullptr;
  88. #endif
  89. }
  90. ~CarlaToolkitQt()
  91. {
  92. qDebug("CarlaToolkitQt::~CarlaToolkitQt()");
  93. CARLA_ASSERT(! app);
  94. if (window)
  95. {
  96. window->close();
  97. delete window;
  98. }
  99. }
  100. void init()
  101. {
  102. qDebug("CarlaToolkitQt::init()");
  103. CARLA_ASSERT(! app);
  104. CARLA_ASSERT(! window);
  105. app = new BridgeApplication;
  106. window = new QMainWindow(nullptr);
  107. window->resize(10, 10);
  108. window->hide();
  109. }
  110. void exec(CarlaClient* const client, const bool showGui)
  111. {
  112. qDebug("CarlaToolkitQt::exec(%p)", client);
  113. CARLA_ASSERT(app);
  114. CARLA_ASSERT(client);
  115. #ifndef BRIDGE_LV2_X11
  116. QWidget* const widget = (QWidget*)client->getWidget();
  117. window->setCentralWidget(widget);
  118. window->adjustSize();
  119. widget->setParent(window);
  120. widget->show();
  121. #endif
  122. if (! client->isResizable())
  123. window->setFixedSize(window->width(), window->height());
  124. window->setWindowTitle(m_title);
  125. if (settings.contains(QString("%1/pos_x").arg(m_title)))
  126. {
  127. int posX = settings.value(QString("%1/pos_x").arg(m_title), window->x()).toInt();
  128. int posY = settings.value(QString("%1/pos_y").arg(m_title), window->y()).toInt();
  129. window->move(posX, posY);
  130. if (client->isResizable())
  131. {
  132. int width = settings.value(QString("%1/width").arg(m_title), window->width()).toInt();
  133. int height = settings.value(QString("%1/height").arg(m_title), window->height()).toInt();
  134. window->resize(width, height);
  135. }
  136. }
  137. if (showGui)
  138. show();
  139. else
  140. m_client->sendOscUpdate();
  141. // Main loop
  142. app->exec(client);
  143. }
  144. void quit()
  145. {
  146. qDebug("CarlaToolkitQt::quit()");
  147. CARLA_ASSERT(app);
  148. if (window)
  149. {
  150. if (m_client)
  151. {
  152. settings.setValue(QString("%1/pos_x").arg(m_title), window->x());
  153. settings.setValue(QString("%1/pos_y").arg(m_title), window->y());
  154. settings.setValue(QString("%1/width").arg(m_title), window->width());
  155. settings.setValue(QString("%1/height").arg(m_title), window->height());
  156. settings.sync();
  157. }
  158. window->close();
  159. delete window;
  160. window = nullptr;
  161. }
  162. m_client = nullptr;
  163. if (app)
  164. {
  165. if (! app->closingDown())
  166. app->quit();
  167. delete app;
  168. app = nullptr;
  169. }
  170. }
  171. void show()
  172. {
  173. qDebug("CarlaToolkitQt::show()");
  174. CARLA_ASSERT(window);
  175. if (window)
  176. window->show();
  177. }
  178. void hide()
  179. {
  180. qDebug("CarlaToolkitQt::hide()");
  181. CARLA_ASSERT(window);
  182. if (window)
  183. window->hide();
  184. }
  185. void resize(int width, int height)
  186. {
  187. qDebug("CarlaToolkitQt::resize(%i, %i)", width, height);
  188. CARLA_ASSERT(window);
  189. if (window)
  190. window->setFixedSize(width, height);
  191. #ifdef BRIDGE_LV2_X11
  192. if (x11Container)
  193. x11Container->setFixedSize(width, height);
  194. #endif
  195. }
  196. void* getContainerId()
  197. {
  198. #ifdef Q_WS_X11
  199. if (! x11Container)
  200. {
  201. x11Container = new QX11EmbedContainer(window);
  202. window->setCentralWidget(x11Container);
  203. window->adjustSize();
  204. x11Container->setParent(window);
  205. x11Container->show();
  206. }
  207. return (void*)x11Container->winId();
  208. #else
  209. return nullptr;
  210. #endif
  211. }
  212. private:
  213. BridgeApplication* app;
  214. QMainWindow* window;
  215. QSettings settings;
  216. #ifdef Q_WS_X11
  217. QX11EmbedContainer* x11Container;
  218. #endif
  219. };
  220. // -------------------------------------------------------------------------
  221. CarlaToolkit* CarlaToolkit::createNew(const char* const title)
  222. {
  223. return new CarlaToolkitQt(title);
  224. }
  225. CARLA_BRIDGE_END_NAMESPACE