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.

531 lines
15KB

  1. /*
  2. * Carla Bridge UI
  3. * Copyright (C) 2011-2021 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 "CarlaBridgeFormat.hpp"
  18. #include "CarlaBridgeToolkit.hpp"
  19. #include "CarlaLibUtils.hpp"
  20. #include <glib.h>
  21. #include <glib-object.h>
  22. #ifdef HAVE_X11
  23. # include <X11/Xlib.h>
  24. #endif
  25. // #include <gtk/gtk.h>
  26. // #ifdef HAVE_X11
  27. // # include <gdk/gdkx.h>
  28. // #endif
  29. // #undef GTK_TYPE_CONTAINER
  30. // #define GTK_TYPE_CONTAINER (gtk.container_get_type())
  31. //
  32. // #undef GTK_TYPE_WINDOW
  33. // #define GTK_TYPE_WINDOW (gtk.window_get_type())
  34. #define GTK_CONTAINER(G) ((GtkContainer*)G)
  35. #define GTK_WINDOW(G) ((GtkWindow*)G)
  36. struct GdkDisplay;
  37. struct GdkDrawable;
  38. struct GdkWindow;
  39. struct GtkContainer;
  40. struct GtkWidget;
  41. struct GtkWindow;
  42. enum GtkWindowType {
  43. GTK_WINDOW_TOPLEVEL
  44. };
  45. typedef void (*gtksym_init)(int* argc, char*** argv);
  46. typedef void (*gtksym_main)(void);
  47. typedef uint (*gtksym_main_level)(void);
  48. typedef void (*gtksym_main_quit)(void);
  49. typedef GType (*gtksym_container_get_type)(void) G_GNUC_CONST;
  50. typedef void (*gtksym_container_add)(GtkContainer* container, GtkWidget* widget);
  51. typedef void (*gtksym_widget_destroy)(GtkWidget* widget);
  52. typedef void (*gtksym_widget_hide)(GtkWidget* widget);
  53. typedef void (*gtksym_widget_show_all)(GtkWidget* widget);
  54. typedef GType (*gtksym_window_get_type)(void) G_GNUC_CONST;
  55. typedef GtkWidget* (*gtksym_window_new)(GtkWindowType type);
  56. typedef void (*gtksym_window_get_position)(GtkWindow* window, int* root_x, int* root_y);
  57. typedef void (*gtksym_window_get_size)(GtkWindow* window, int* width, int* height);
  58. typedef void (*gtksym_window_resize)(GtkWindow* window, int width, int height);
  59. typedef void (*gtksym_window_set_resizable)(GtkWindow* window, int resizable);
  60. typedef void (*gtksym_window_set_title)(GtkWindow* window, const char* title);
  61. #ifdef HAVE_X11
  62. typedef GdkWindow* (*gtksym_widget_get_window)(GtkWidget* widget);
  63. # ifdef BRIDGE_GTK3
  64. typedef GdkDisplay* (*gdksym_window_get_display)(GdkWindow* window);
  65. typedef Display* (*gdksym_x11_display_get_xdisplay)(GdkDisplay* display);
  66. typedef Window (*gdksym_x11_window_get_xid)(GdkWindow* window);
  67. # else
  68. typedef Display* (*gdksym_x11_drawable_get_xdisplay)(GdkDrawable* drawable);
  69. typedef XID (*gdksym_x11_drawable_get_xid)(GdkDrawable* drawable);
  70. # endif
  71. #endif
  72. CARLA_BRIDGE_UI_START_NAMESPACE
  73. // -------------------------------------------------------------------------
  74. struct GtkLoader {
  75. lib_t lib;
  76. gtksym_init init;
  77. gtksym_main main;
  78. gtksym_main_level main_level;
  79. gtksym_main_quit main_quit;
  80. gtksym_container_get_type container_get_type;
  81. gtksym_container_add container_add;
  82. gtksym_widget_destroy widget_destroy;
  83. gtksym_widget_hide widget_hide;
  84. gtksym_widget_show_all widget_show_all;
  85. gtksym_window_get_type window_get_type;
  86. gtksym_window_new window_new;
  87. gtksym_window_get_position window_get_position;
  88. gtksym_window_get_size window_get_size;
  89. gtksym_window_resize window_resize;
  90. gtksym_window_set_resizable window_set_resizable;
  91. gtksym_window_set_title window_set_title;
  92. bool ok;
  93. #ifdef HAVE_X11
  94. gtksym_widget_get_window widget_get_window;
  95. # ifdef BRIDGE_GTK3
  96. gdksym_window_get_display window_get_display;
  97. gdksym_x11_display_get_xdisplay x11_display_get_xdisplay;
  98. gdksym_x11_window_get_xid x11_window_get_xid;
  99. # else
  100. gdksym_x11_drawable_get_xdisplay x11_drawable_get_xdisplay;
  101. gdksym_x11_drawable_get_xid x11_drawable_get_xid;
  102. # endif
  103. #endif
  104. GtkLoader()
  105. : lib(nullptr),
  106. init(nullptr),
  107. main(nullptr),
  108. main_level(nullptr),
  109. main_quit(nullptr),
  110. container_get_type(nullptr),
  111. container_add(nullptr),
  112. widget_destroy(nullptr),
  113. widget_hide(nullptr),
  114. widget_show_all(nullptr),
  115. window_get_type(nullptr),
  116. window_new(nullptr),
  117. window_get_position(nullptr),
  118. window_get_size(nullptr),
  119. window_resize(nullptr),
  120. window_set_resizable(nullptr),
  121. window_set_title(nullptr),
  122. ok(false)
  123. #ifdef HAVE_X11
  124. , widget_get_window(nullptr),
  125. # ifdef BRIDGE_GTK3
  126. window_get_display(nullptr),
  127. x11_display_get_xdisplay(nullptr),
  128. x11_window_get_xid(nullptr)
  129. # else
  130. x11_drawable_get_xdisplay(nullptr),
  131. x11_drawable_get_xid(nullptr)
  132. # endif
  133. #endif
  134. {
  135. const char* filename;
  136. const char* const filenames[] = {
  137. #ifdef BRIDGE_GTK3
  138. # if defined(CARLA_OS_MAC)
  139. "libgtk-3.0.dylib",
  140. # else
  141. "libgtk-3.so.0",
  142. # endif
  143. #else
  144. # if defined(CARLA_OS_MAC)
  145. "libgtk-quartz-2.0.dylib",
  146. "libgtk-x11-2.0.dylib",
  147. "/opt/local/lib/libgtk-quartz-2.0.dylib",
  148. "/opt/local/lib/libgtk-x11-2.0.dylib",
  149. # else
  150. "libgtk-x11-2.0.so.0",
  151. # endif
  152. #endif
  153. };
  154. for (size_t i=0; i<sizeof(filenames)/sizeof(filenames[0]); ++i)
  155. {
  156. filename = filenames[i];
  157. if ((lib = lib_open(filename)) != nullptr)
  158. break;
  159. }
  160. if (lib == nullptr)
  161. {
  162. fprintf(stderr, "Failed to load Gtk, reason:\n%s\n", lib_error(filename));
  163. return;
  164. }
  165. else
  166. {
  167. fprintf(stdout, "%s loaded successfully!\n", filename);
  168. }
  169. #define GTK_LIB_SYMBOL(NAME) \
  170. NAME = lib_symbol<gtksym_##NAME>(lib, "gtk_" #NAME); \
  171. CARLA_SAFE_ASSERT_RETURN(NAME != nullptr,);
  172. #define GDK_LIB_SYMBOL(NAME) \
  173. NAME = lib_symbol<gdksym_##NAME>(lib, "gdk_" #NAME);
  174. GTK_LIB_SYMBOL(init)
  175. GTK_LIB_SYMBOL(main)
  176. GTK_LIB_SYMBOL(main_level)
  177. GTK_LIB_SYMBOL(main_quit)
  178. GTK_LIB_SYMBOL(container_get_type)
  179. GTK_LIB_SYMBOL(container_add)
  180. GTK_LIB_SYMBOL(widget_destroy)
  181. GTK_LIB_SYMBOL(widget_hide)
  182. GTK_LIB_SYMBOL(widget_show_all)
  183. GTK_LIB_SYMBOL(window_get_type)
  184. GTK_LIB_SYMBOL(window_new)
  185. GTK_LIB_SYMBOL(window_get_position)
  186. GTK_LIB_SYMBOL(window_get_size)
  187. GTK_LIB_SYMBOL(window_resize)
  188. GTK_LIB_SYMBOL(window_set_resizable)
  189. GTK_LIB_SYMBOL(window_set_title)
  190. ok = true;
  191. #ifdef HAVE_X11
  192. GTK_LIB_SYMBOL(widget_get_window)
  193. # ifdef BRIDGE_GTK3
  194. GDK_LIB_SYMBOL(window_get_display)
  195. GDK_LIB_SYMBOL(x11_display_get_xdisplay)
  196. GDK_LIB_SYMBOL(x11_window_get_xid)
  197. # else
  198. GDK_LIB_SYMBOL(x11_drawable_get_xdisplay)
  199. GDK_LIB_SYMBOL(x11_drawable_get_xid)
  200. # endif
  201. #endif
  202. #undef GDK_LIB_SYMBOL
  203. #undef GTK_LIB_SYMBOL
  204. }
  205. ~GtkLoader()
  206. {
  207. if (lib != nullptr)
  208. lib_close(lib);
  209. }
  210. void main_quit_if_needed()
  211. {
  212. if (main_level() != 0)
  213. main_quit();
  214. }
  215. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(GtkLoader)
  216. };
  217. // -------------------------------------------------------------------------
  218. static const bool gHideShowTesting = std::getenv("CARLA_UI_TESTING") != nullptr;
  219. // -------------------------------------------------------------------------
  220. class CarlaBridgeToolkitGtk : public CarlaBridgeToolkit
  221. {
  222. public:
  223. CarlaBridgeToolkitGtk(CarlaBridgeFormat* const format)
  224. : CarlaBridgeToolkit(format),
  225. gtk(),
  226. fNeedsShow(false),
  227. fWindow(nullptr),
  228. fLastX(0),
  229. fLastY(0),
  230. fLastWidth(0),
  231. fLastHeight(0)
  232. {
  233. carla_debug("CarlaBridgeToolkitGtk::CarlaBridgeToolkitGtk(%p)", format);
  234. }
  235. ~CarlaBridgeToolkitGtk() override
  236. {
  237. CARLA_SAFE_ASSERT(fWindow == nullptr);
  238. carla_debug("CarlaBridgeToolkitGtk::~CarlaBridgeToolkitGtk()");
  239. }
  240. bool init(const int /*argc*/, const char** /*argv[]*/) override
  241. {
  242. CARLA_SAFE_ASSERT_RETURN(fWindow == nullptr, false);
  243. carla_debug("CarlaBridgeToolkitGtk::init()");
  244. if (! gtk.ok)
  245. return false;
  246. static int gargc = 0;
  247. static char** gargv = nullptr;
  248. gtk.init(&gargc, &gargv);
  249. fWindow = gtk.window_new(GTK_WINDOW_TOPLEVEL);
  250. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr, false);
  251. gtk.window_resize(GTK_WINDOW(fWindow), 30, 30);
  252. gtk.widget_hide(fWindow);
  253. return true;
  254. }
  255. void exec(const bool showUI) override
  256. {
  257. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  258. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  259. carla_debug("CarlaBridgeToolkitGtk::exec(%s)", bool2str(showUI));
  260. const CarlaBridgeFormat::Options& options(fPlugin->getOptions());
  261. GtkWindow* const gtkWindow(GTK_WINDOW(fWindow));
  262. CARLA_SAFE_ASSERT_RETURN(gtkWindow != nullptr,);
  263. GtkWidget* const widget((GtkWidget*)fPlugin->getWidget());
  264. gtk.container_add(GTK_CONTAINER(fWindow), widget);
  265. gtk.window_set_resizable(gtkWindow, options.isResizable);
  266. gtk.window_set_title(gtkWindow, options.windowTitle.buffer());
  267. if (showUI || fNeedsShow)
  268. {
  269. show();
  270. fNeedsShow = false;
  271. }
  272. g_timeout_add(30, gtk_ui_timeout, this);
  273. g_signal_connect(fWindow, "destroy", G_CALLBACK(gtk_ui_destroy), this);
  274. g_signal_connect(fWindow, "realize", G_CALLBACK(gtk_ui_realize), this);
  275. // First idle
  276. handleTimeout();
  277. // Main loop
  278. gtk.main();
  279. }
  280. void quit() override
  281. {
  282. carla_debug("CarlaBridgeToolkitGtk::quit()");
  283. if (fWindow != nullptr)
  284. {
  285. gtk.widget_destroy(fWindow);
  286. fWindow = nullptr;
  287. gtk.main_quit_if_needed();
  288. }
  289. }
  290. void show() override
  291. {
  292. carla_debug("CarlaBridgeToolkitGtk::show()");
  293. fNeedsShow = true;
  294. if (fWindow != nullptr)
  295. gtk.widget_show_all(fWindow);
  296. }
  297. void focus() override
  298. {
  299. carla_debug("CarlaBridgeToolkitGtk::focus()");
  300. }
  301. void hide() override
  302. {
  303. carla_debug("CarlaBridgeToolkitGtk::hide()");
  304. fNeedsShow = false;
  305. if (fWindow != nullptr)
  306. gtk.widget_hide(fWindow);
  307. }
  308. void setChildWindow(void* const) override {}
  309. void setSize(const uint width, const uint height) override
  310. {
  311. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  312. carla_debug("CarlaBridgeToolkitGtk::resize(%i, %i)", width, height);
  313. gtk.window_resize(GTK_WINDOW(fWindow), static_cast<int>(width), static_cast<int>(height));
  314. }
  315. void setTitle(const char* const title) override
  316. {
  317. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  318. carla_debug("CarlaBridgeToolkitGtk::setTitle(\"%s\")", title);
  319. gtk.window_set_title(GTK_WINDOW(fWindow), title);
  320. }
  321. // ---------------------------------------------------------------------
  322. protected:
  323. GtkLoader gtk;
  324. bool fNeedsShow;
  325. GtkWidget* fWindow;
  326. int fLastX;
  327. int fLastY;
  328. int fLastWidth;
  329. int fLastHeight;
  330. void handleDestroy()
  331. {
  332. carla_debug("CarlaBridgeToolkitGtk::handleDestroy()");
  333. fWindow = nullptr;
  334. gtk.main_quit_if_needed();
  335. }
  336. void handleRealize()
  337. {
  338. carla_debug("CarlaBridgeToolkitGtk::handleRealize()");
  339. #ifdef HAVE_X11
  340. const CarlaBridgeFormat::Options& options(fPlugin->getOptions());
  341. if (options.transientWindowId != 0)
  342. setTransient(options.transientWindowId);
  343. #endif
  344. }
  345. int handleTimeout()
  346. {
  347. if (fWindow != nullptr)
  348. {
  349. gtk.window_get_position(GTK_WINDOW(fWindow), &fLastX, &fLastY);
  350. gtk.window_get_size(GTK_WINDOW(fWindow), &fLastWidth, &fLastHeight);
  351. }
  352. if (fPlugin->isPipeRunning())
  353. fPlugin->idlePipe();
  354. fPlugin->idleUI();
  355. if (gHideShowTesting)
  356. {
  357. static int counter = 0;
  358. ++counter;
  359. if (counter == 100)
  360. {
  361. hide();
  362. }
  363. else if (counter == 200)
  364. {
  365. show();
  366. counter = 0;
  367. }
  368. }
  369. return 1;
  370. }
  371. #ifdef HAVE_X11
  372. void setTransient(const uintptr_t winId)
  373. {
  374. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  375. carla_debug("CarlaBridgeToolkitGtk::setTransient(0x" P_UINTPTR ")", winId);
  376. if (gtk.widget_get_window == nullptr)
  377. return;
  378. # ifdef BRIDGE_GTK3
  379. if (gtk.window_get_display == nullptr)
  380. return;
  381. if (gtk.x11_display_get_xdisplay == nullptr)
  382. return;
  383. if (gtk.x11_window_get_xid == nullptr)
  384. return;
  385. # else
  386. if (gtk.x11_drawable_get_xdisplay == nullptr)
  387. return;
  388. if (gtk.x11_drawable_get_xid == nullptr)
  389. return;
  390. # endif
  391. GdkWindow* const gdkWindow = gtk.widget_get_window(fWindow);
  392. CARLA_SAFE_ASSERT_RETURN(gdkWindow != nullptr,);
  393. # ifdef BRIDGE_GTK3
  394. GdkDisplay* const gdkDisplay = gtk.window_get_display(gdkWindow);
  395. CARLA_SAFE_ASSERT_RETURN(gdkDisplay != nullptr,);
  396. ::Display* const display = gtk.x11_display_get_xdisplay(gdkDisplay);
  397. CARLA_SAFE_ASSERT_RETURN(display != nullptr,);
  398. const ::XID xid = gtk.x11_window_get_xid(gdkWindow);
  399. CARLA_SAFE_ASSERT_RETURN(xid != 0,);
  400. # else
  401. ::Display* const display = gtk.x11_drawable_get_xdisplay((GdkDrawable*)gdkWindow);
  402. CARLA_SAFE_ASSERT_RETURN(display != nullptr,);
  403. const ::XID xid = gtk.x11_drawable_get_xid((GdkDrawable*)gdkWindow);
  404. CARLA_SAFE_ASSERT_RETURN(xid != 0,);
  405. # endif
  406. XSetTransientForHint(display, xid, static_cast< ::Window>(winId));
  407. }
  408. #endif
  409. // ---------------------------------------------------------------------
  410. private:
  411. static void gtk_ui_destroy(GtkWidget*, void* data)
  412. {
  413. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  414. ((CarlaBridgeToolkitGtk*)data)->handleDestroy();
  415. }
  416. static void gtk_ui_realize(GtkWidget*, void* data)
  417. {
  418. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  419. ((CarlaBridgeToolkitGtk*)data)->handleRealize();
  420. }
  421. static int gtk_ui_timeout(void* data)
  422. {
  423. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  424. return ((CarlaBridgeToolkitGtk*)data)->handleTimeout();
  425. }
  426. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaBridgeToolkitGtk)
  427. };
  428. // -------------------------------------------------------------------------
  429. CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(CarlaBridgeFormat* const format)
  430. {
  431. return new CarlaBridgeToolkitGtk(format);
  432. }
  433. // -------------------------------------------------------------------------
  434. CARLA_BRIDGE_UI_END_NAMESPACE