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.

172 lines
4.7KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2022 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 "CarlaUtils.h"
  18. #include "CarlaMathUtils.hpp"
  19. #if defined(CARLA_OS_MAC) && !defined(CARLA_PLUGIN_BUILD)
  20. # import <Cocoa/Cocoa.h>
  21. #endif
  22. #ifdef HAVE_X11
  23. # include <X11/Xlib.h>
  24. # include <X11/Xresource.h>
  25. #endif
  26. namespace CB = CARLA_BACKEND_NAMESPACE;
  27. // -------------------------------------------------------------------------------------------------------------------
  28. double carla_get_desktop_scale_factor()
  29. {
  30. // allow custom scale for testing
  31. if (const char* const scale = getenv("DPF_SCALE_FACTOR"))
  32. return std::max(1.0, std::atof(scale));
  33. // Qt env var for the same
  34. if (const char* const scale = getenv("QT_SCALE_FACTOR"))
  35. return std::max(1.0, std::atof(scale));
  36. #if defined(CARLA_OS_MAC) && !defined(CARLA_PLUGIN_BUILD)
  37. return [NSScreen mainScreen].backingScaleFactor;
  38. #endif
  39. #ifdef HAVE_X11
  40. if (::Display* const display = XOpenDisplay(nullptr))
  41. {
  42. XrmInitialize();
  43. if (char* const rms = XResourceManagerString(display))
  44. {
  45. if (const XrmDatabase sdb = XrmGetStringDatabase(rms))
  46. {
  47. char* type = nullptr;
  48. XrmValue ret;
  49. if (XrmGetResource(sdb, "Xft.dpi", "String", &type, &ret)
  50. && ret.addr != nullptr
  51. && type != nullptr
  52. && std::strncmp("String", type, 6) == 0)
  53. {
  54. const double dpi = std::atof(ret.addr);
  55. if (carla_isNotZero(dpi))
  56. return dpi / 96;
  57. }
  58. }
  59. }
  60. XCloseDisplay(display);
  61. }
  62. #endif
  63. return 1.0;
  64. }
  65. // -------------------------------------------------------------------------------------------------------------------
  66. int carla_cocoa_get_window(void* nsViewPtr)
  67. {
  68. CARLA_SAFE_ASSERT_RETURN(nsViewPtr != nullptr, 0);
  69. #if defined(CARLA_OS_MAC) && !defined(CARLA_PLUGIN_BUILD)
  70. NSView* const nsView = (NSView*)nsViewPtr;
  71. return [[nsView window] windowNumber];
  72. #else
  73. return 0;
  74. #endif
  75. }
  76. void carla_cocoa_set_transient_window_for(void* nsViewChildPtr, void* nsViewParentPtr)
  77. {
  78. CARLA_SAFE_ASSERT_RETURN(nsViewChildPtr != nullptr,);
  79. CARLA_SAFE_ASSERT_RETURN(nsViewParentPtr != nullptr,);
  80. #if defined(CARLA_OS_MAC) && !defined(CARLA_PLUGIN_BUILD)
  81. NSView* const nsViewChild = (NSView*)nsViewChildPtr;
  82. NSView* const nsViewParent = (NSView*)nsViewParentPtr;
  83. [[nsViewParent window] addChildWindow:[nsViewChild window]
  84. ordered:NSWindowAbove];
  85. #endif
  86. }
  87. void carla_x11_reparent_window(uintptr_t winId1, uintptr_t winId2)
  88. {
  89. CARLA_SAFE_ASSERT_RETURN(winId1 != 0,);
  90. CARLA_SAFE_ASSERT_RETURN(winId2 != 0,);
  91. #ifdef HAVE_X11
  92. if (::Display* const disp = XOpenDisplay(nullptr))
  93. {
  94. XReparentWindow(disp, winId1, winId2, 0, 0);
  95. XMapWindow(disp, winId1);
  96. XCloseDisplay(disp);
  97. }
  98. #endif
  99. }
  100. void carla_x11_move_window(uintptr_t winId, int x, int y)
  101. {
  102. CARLA_SAFE_ASSERT_RETURN(winId != 0,);
  103. #ifdef HAVE_X11
  104. if (::Display* const disp = XOpenDisplay(nullptr))
  105. {
  106. XMoveWindow(disp, winId, x, y);
  107. XCloseDisplay(disp);
  108. }
  109. #else
  110. // unused
  111. return; (void)x; (void)y;
  112. #endif
  113. }
  114. int* carla_x11_get_window_pos(uintptr_t winId)
  115. {
  116. static int pos[4];
  117. if (winId == 0)
  118. {
  119. pos[0] = 0;
  120. pos[1] = 0;
  121. pos[2] = 0;
  122. pos[3] = 0;
  123. }
  124. #ifdef HAVE_X11
  125. else if (::Display* const disp = XOpenDisplay(nullptr))
  126. {
  127. int x, y;
  128. Window child;
  129. XWindowAttributes xwa;
  130. XTranslateCoordinates(disp, winId, XRootWindow(disp, 0), 0, 0, &x, &y, &child);
  131. XGetWindowAttributes(disp, winId, &xwa);
  132. XCloseDisplay(disp);
  133. pos[0] = x - xwa.x;
  134. pos[1] = y - xwa.y;
  135. pos[2] = xwa.x;
  136. pos[3] = xwa.y;
  137. }
  138. #endif
  139. else
  140. {
  141. pos[0] = 0;
  142. pos[1] = 0;
  143. pos[2] = 0;
  144. pos[3] = 0;
  145. }
  146. return pos;
  147. }
  148. // -------------------------------------------------------------------------------------------------------------------