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.

511 lines
14KB

  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. void* 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. #ifdef BRIDGE_GTK3
  136. const char* const filename = "libgtk-3.so.0";
  137. #else
  138. const char* const filename = "libgtk-x11-2.0.so.0";
  139. #endif
  140. lib = lib_open(filename);
  141. if (lib == nullptr)
  142. {
  143. fprintf(stderr, "Failed to load Gtk, reason:\n%s\n", lib_error(filename));
  144. return;
  145. }
  146. else
  147. {
  148. fprintf(stdout, "%s loaded successfully!\n", filename);
  149. }
  150. #define GTK_LIB_SYMBOL(NAME) \
  151. NAME = lib_symbol<gtksym_##NAME>(lib, "gtk_" #NAME); \
  152. CARLA_SAFE_ASSERT_RETURN(NAME != nullptr,);
  153. #define GDK_LIB_SYMBOL(NAME) \
  154. NAME = lib_symbol<gdksym_##NAME>(lib, "gdk_" #NAME);
  155. GTK_LIB_SYMBOL(init)
  156. GTK_LIB_SYMBOL(main)
  157. GTK_LIB_SYMBOL(main_level)
  158. GTK_LIB_SYMBOL(main_quit)
  159. GTK_LIB_SYMBOL(container_get_type)
  160. GTK_LIB_SYMBOL(container_add)
  161. GTK_LIB_SYMBOL(widget_destroy)
  162. GTK_LIB_SYMBOL(widget_hide)
  163. GTK_LIB_SYMBOL(widget_show_all)
  164. GTK_LIB_SYMBOL(window_get_type)
  165. GTK_LIB_SYMBOL(window_new)
  166. GTK_LIB_SYMBOL(window_get_position)
  167. GTK_LIB_SYMBOL(window_get_size)
  168. GTK_LIB_SYMBOL(window_resize)
  169. GTK_LIB_SYMBOL(window_set_resizable)
  170. GTK_LIB_SYMBOL(window_set_title)
  171. ok = true;
  172. #ifdef HAVE_X11
  173. GTK_LIB_SYMBOL(widget_get_window)
  174. # ifdef BRIDGE_GTK3
  175. GDK_LIB_SYMBOL(window_get_display)
  176. GDK_LIB_SYMBOL(x11_display_get_xdisplay)
  177. GDK_LIB_SYMBOL(x11_window_get_xid)
  178. # else
  179. GDK_LIB_SYMBOL(x11_drawable_get_xdisplay)
  180. GDK_LIB_SYMBOL(x11_drawable_get_xid)
  181. # endif
  182. #endif
  183. #undef GDK_LIB_SYMBOL
  184. #undef GTK_LIB_SYMBOL
  185. }
  186. ~GtkLoader()
  187. {
  188. if (lib != nullptr)
  189. lib_close(lib);
  190. }
  191. void main_quit_if_needed()
  192. {
  193. if (main_level() != 0)
  194. main_quit();
  195. }
  196. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(GtkLoader)
  197. };
  198. // -------------------------------------------------------------------------
  199. static const bool gHideShowTesting = std::getenv("CARLA_UI_TESTING") != nullptr;
  200. // -------------------------------------------------------------------------
  201. class CarlaBridgeToolkitGtk : public CarlaBridgeToolkit
  202. {
  203. public:
  204. CarlaBridgeToolkitGtk(CarlaBridgeFormat* const format)
  205. : CarlaBridgeToolkit(format),
  206. gtk(),
  207. fNeedsShow(false),
  208. fWindow(nullptr),
  209. fLastX(0),
  210. fLastY(0),
  211. fLastWidth(0),
  212. fLastHeight(0)
  213. {
  214. carla_debug("CarlaBridgeToolkitGtk::CarlaBridgeToolkitGtk(%p)", format);
  215. }
  216. ~CarlaBridgeToolkitGtk() override
  217. {
  218. CARLA_SAFE_ASSERT(fWindow == nullptr);
  219. carla_debug("CarlaBridgeToolkitGtk::~CarlaBridgeToolkitGtk()");
  220. }
  221. bool init(const int /*argc*/, const char** /*argv[]*/) override
  222. {
  223. CARLA_SAFE_ASSERT_RETURN(fWindow == nullptr, false);
  224. carla_debug("CarlaBridgeToolkitGtk::init()");
  225. if (! gtk.ok)
  226. return false;
  227. static int gargc = 0;
  228. static char** gargv = nullptr;
  229. gtk.init(&gargc, &gargv);
  230. fWindow = gtk.window_new(GTK_WINDOW_TOPLEVEL);
  231. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr, false);
  232. gtk.window_resize(GTK_WINDOW(fWindow), 30, 30);
  233. gtk.widget_hide(fWindow);
  234. return true;
  235. }
  236. void exec(const bool showUI) override
  237. {
  238. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  239. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  240. carla_debug("CarlaBridgeToolkitGtk::exec(%s)", bool2str(showUI));
  241. const CarlaBridgeFormat::Options& options(fPlugin->getOptions());
  242. GtkWindow* const gtkWindow(GTK_WINDOW(fWindow));
  243. CARLA_SAFE_ASSERT_RETURN(gtkWindow != nullptr,);
  244. GtkWidget* const widget((GtkWidget*)fPlugin->getWidget());
  245. gtk.container_add(GTK_CONTAINER(fWindow), widget);
  246. gtk.window_set_resizable(gtkWindow, options.isResizable);
  247. gtk.window_set_title(gtkWindow, options.windowTitle.buffer());
  248. if (showUI || fNeedsShow)
  249. {
  250. show();
  251. fNeedsShow = false;
  252. }
  253. g_timeout_add(30, gtk_ui_timeout, this);
  254. g_signal_connect(fWindow, "destroy", G_CALLBACK(gtk_ui_destroy), this);
  255. g_signal_connect(fWindow, "realize", G_CALLBACK(gtk_ui_realize), this);
  256. // First idle
  257. handleTimeout();
  258. // Main loop
  259. gtk.main();
  260. }
  261. void quit() override
  262. {
  263. carla_debug("CarlaBridgeToolkitGtk::quit()");
  264. if (fWindow != nullptr)
  265. {
  266. gtk.widget_destroy(fWindow);
  267. fWindow = nullptr;
  268. gtk.main_quit_if_needed();
  269. }
  270. }
  271. void show() override
  272. {
  273. carla_debug("CarlaBridgeToolkitGtk::show()");
  274. fNeedsShow = true;
  275. if (fWindow != nullptr)
  276. gtk.widget_show_all(fWindow);
  277. }
  278. void focus() override
  279. {
  280. carla_debug("CarlaBridgeToolkitGtk::focus()");
  281. }
  282. void hide() override
  283. {
  284. carla_debug("CarlaBridgeToolkitGtk::hide()");
  285. fNeedsShow = false;
  286. if (fWindow != nullptr)
  287. gtk.widget_hide(fWindow);
  288. }
  289. void setChildWindow(void* const) override {}
  290. void setSize(const uint width, const uint height) override
  291. {
  292. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  293. carla_debug("CarlaBridgeToolkitGtk::resize(%i, %i)", width, height);
  294. gtk.window_resize(GTK_WINDOW(fWindow), static_cast<int>(width), static_cast<int>(height));
  295. }
  296. void setTitle(const char* const title) override
  297. {
  298. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  299. carla_debug("CarlaBridgeToolkitGtk::setTitle(\"%s\")", title);
  300. gtk.window_set_title(GTK_WINDOW(fWindow), title);
  301. }
  302. // ---------------------------------------------------------------------
  303. protected:
  304. GtkLoader gtk;
  305. bool fNeedsShow;
  306. GtkWidget* fWindow;
  307. int fLastX;
  308. int fLastY;
  309. int fLastWidth;
  310. int fLastHeight;
  311. void handleDestroy()
  312. {
  313. carla_debug("CarlaBridgeToolkitGtk::handleDestroy()");
  314. fWindow = nullptr;
  315. gtk.main_quit_if_needed();
  316. }
  317. void handleRealize()
  318. {
  319. carla_debug("CarlaBridgeToolkitGtk::handleRealize()");
  320. #ifdef HAVE_X11
  321. const CarlaBridgeFormat::Options& options(fPlugin->getOptions());
  322. if (options.transientWindowId != 0)
  323. setTransient(options.transientWindowId);
  324. #endif
  325. }
  326. int handleTimeout()
  327. {
  328. if (fWindow != nullptr)
  329. {
  330. gtk.window_get_position(GTK_WINDOW(fWindow), &fLastX, &fLastY);
  331. gtk.window_get_size(GTK_WINDOW(fWindow), &fLastWidth, &fLastHeight);
  332. }
  333. if (fPlugin->isPipeRunning())
  334. fPlugin->idlePipe();
  335. fPlugin->idleUI();
  336. if (gHideShowTesting)
  337. {
  338. static int counter = 0;
  339. ++counter;
  340. if (counter == 100)
  341. {
  342. hide();
  343. }
  344. else if (counter == 200)
  345. {
  346. show();
  347. counter = 0;
  348. }
  349. }
  350. return 1;
  351. }
  352. #ifdef HAVE_X11
  353. void setTransient(const uintptr_t winId)
  354. {
  355. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  356. carla_debug("CarlaBridgeToolkitGtk::setTransient(0x" P_UINTPTR ")", winId);
  357. if (gtk.widget_get_window == nullptr)
  358. return;
  359. # ifdef BRIDGE_GTK3
  360. if (gtk.window_get_display == nullptr)
  361. return;
  362. if (gtk.x11_display_get_xdisplay == nullptr)
  363. return;
  364. if (gtk.x11_window_get_xid == nullptr)
  365. return;
  366. # else
  367. if (gtk.x11_drawable_get_xdisplay == nullptr)
  368. return;
  369. if (gtk.x11_drawable_get_xid == nullptr)
  370. return;
  371. # endif
  372. GdkWindow* const gdkWindow = gtk.widget_get_window(fWindow);
  373. CARLA_SAFE_ASSERT_RETURN(gdkWindow != nullptr,);
  374. # ifdef BRIDGE_GTK3
  375. GdkDisplay* const gdkDisplay = gtk.window_get_display(gdkWindow);
  376. CARLA_SAFE_ASSERT_RETURN(gdkDisplay != nullptr,);
  377. ::Display* const display = gtk.x11_display_get_xdisplay(gdkDisplay);
  378. CARLA_SAFE_ASSERT_RETURN(display != nullptr,);
  379. const ::XID xid = gtk.x11_window_get_xid(gdkWindow);
  380. CARLA_SAFE_ASSERT_RETURN(xid != 0,);
  381. # else
  382. ::Display* const display = gtk.x11_drawable_get_xdisplay((GdkDrawable*)gdkWindow);
  383. CARLA_SAFE_ASSERT_RETURN(display != nullptr,);
  384. const ::XID xid = gtk.x11_drawable_get_xid((GdkDrawable*)gdkWindow);
  385. CARLA_SAFE_ASSERT_RETURN(xid != 0,);
  386. # endif
  387. XSetTransientForHint(display, xid, static_cast< ::Window>(winId));
  388. }
  389. #endif
  390. // ---------------------------------------------------------------------
  391. private:
  392. static void gtk_ui_destroy(GtkWidget*, void* data)
  393. {
  394. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  395. ((CarlaBridgeToolkitGtk*)data)->handleDestroy();
  396. }
  397. static void gtk_ui_realize(GtkWidget*, void* data)
  398. {
  399. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  400. ((CarlaBridgeToolkitGtk*)data)->handleRealize();
  401. }
  402. static int gtk_ui_timeout(void* data)
  403. {
  404. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  405. return ((CarlaBridgeToolkitGtk*)data)->handleTimeout();
  406. }
  407. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaBridgeToolkitGtk)
  408. };
  409. // -------------------------------------------------------------------------
  410. CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(CarlaBridgeFormat* const format)
  411. {
  412. return new CarlaBridgeToolkitGtk(format);
  413. }
  414. // -------------------------------------------------------------------------
  415. CARLA_BRIDGE_UI_END_NAMESPACE