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.

272 lines
6.3KB

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