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.

187 lines
4.8KB

  1. /*
  2. * Carla Plugin UI
  3. * Copyright (C) 2014 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 "CarlaPluginUi.hpp"
  18. #ifdef HAVE_X11
  19. # include <X11/Xatom.h>
  20. # include <X11/Xlib.h>
  21. # include <X11/Xutil.h>
  22. #endif
  23. #ifdef HAVE_X11
  24. // -----------------------------------------------------
  25. // X11
  26. class X11PluginUi : public CarlaPluginUi
  27. {
  28. public:
  29. X11PluginUi(CloseCallback* cb) noexcept
  30. : CarlaPluginUi(cb),
  31. fDisplay(nullptr),
  32. fWindow(0)
  33. {
  34. fDisplay = XOpenDisplay(0);
  35. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  36. const int screen = DefaultScreen(fDisplay);
  37. XSetWindowAttributes attr;
  38. carla_zeroStruct<XSetWindowAttributes>(attr);
  39. fWindow = XCreateWindow(fDisplay, RootWindow(fDisplay, screen),
  40. 0, 0, 300, 300, 0,
  41. DefaultDepth(fDisplay, screen),
  42. InputOutput,
  43. DefaultVisual(fDisplay, screen),
  44. CWBorderPixel | CWColormap | CWEventMask, &attr);
  45. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  46. Atom wmDelete = XInternAtom(fDisplay, "WM_DELETE_WINDOW", True);
  47. XSetWMProtocols(fDisplay, fWindow, &wmDelete, 1);
  48. }
  49. ~X11PluginUi() override
  50. {
  51. if (fWindow != 0)
  52. {
  53. XDestroyWindow(fDisplay, fWindow);
  54. fWindow = 0;
  55. }
  56. if (fDisplay != nullptr)
  57. {
  58. XCloseDisplay(fDisplay);
  59. fDisplay = nullptr;
  60. }
  61. }
  62. void show() override
  63. {
  64. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  65. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  66. XMapRaised(fDisplay, fWindow);
  67. XFlush(fDisplay);
  68. }
  69. void hide() override
  70. {
  71. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  72. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  73. XUnmapWindow(fDisplay, fWindow);
  74. XFlush(fDisplay);
  75. }
  76. void idle() override
  77. {
  78. for (XEvent event; XPending(fDisplay) > 0;)
  79. {
  80. XNextEvent(fDisplay, &event);
  81. switch (event.type)
  82. {
  83. case ClientMessage:
  84. if (std::strcmp(XGetAtomName(fDisplay, event.xclient.message_type), "WM_PROTOCOLS") == 0)
  85. {
  86. CARLA_SAFE_ASSERT_BREAK(fCallback != nullptr);
  87. fCallback->handlePluginUiClosed();
  88. }
  89. break;
  90. default:
  91. break;
  92. }
  93. }
  94. }
  95. void focus() override
  96. {
  97. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  98. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  99. XRaiseWindow(fDisplay, fWindow);
  100. XSetInputFocus(fDisplay, fWindow, RevertToPointerRoot, CurrentTime);
  101. XFlush(fDisplay);
  102. }
  103. void setSize(const uint width, const uint height, const bool forceUpdate) override
  104. {
  105. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  106. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  107. XResizeWindow(fDisplay, fWindow, width, height);
  108. XSizeHints sizeHints;
  109. carla_zeroStruct<XSizeHints>(sizeHints);
  110. sizeHints.flags = PMinSize|PMaxSize;
  111. sizeHints.min_width = width;
  112. sizeHints.min_height = height;
  113. sizeHints.max_width = width;
  114. sizeHints.max_height = height;
  115. XSetNormalHints(fDisplay, fWindow, &sizeHints);
  116. if (forceUpdate)
  117. XFlush(fDisplay);
  118. }
  119. void setTitle(const char* const title) override
  120. {
  121. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  122. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  123. XStoreName(fDisplay, fWindow, title);
  124. }
  125. void* getPtr() const noexcept
  126. {
  127. return (void*)fWindow;
  128. }
  129. private:
  130. Display* fDisplay;
  131. Window fWindow;
  132. };
  133. #endif
  134. // -----------------------------------------------------
  135. #ifdef CARLA_OS_MAC
  136. CarlaPluginUi* CarlaPluginUi::newCocoa(CloseCallback* cb)
  137. {
  138. return new CocoaPluginUi(cb);
  139. }
  140. #endif
  141. #ifdef CARLA_OS_WIN
  142. CarlaPluginUi* CarlaPluginUi::newWindows(CloseCallback* cb)
  143. {
  144. return new WindowsPluginUi(cb);
  145. }
  146. #endif
  147. #ifdef HAVE_X11
  148. CarlaPluginUi* CarlaPluginUi::newX11(CloseCallback* cb)
  149. {
  150. return new X11PluginUi(cb);
  151. }
  152. #endif
  153. // -----------------------------------------------------