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.

176 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. XrmDestroyDatabase(sdb);
  59. }
  60. }
  61. XCloseDisplay(display);
  62. }
  63. #endif
  64. return 1.0;
  65. }
  66. // -------------------------------------------------------------------------------------------------------------------
  67. int carla_cocoa_get_window(void* nsViewPtr)
  68. {
  69. CARLA_SAFE_ASSERT_RETURN(nsViewPtr != nullptr, 0);
  70. #if defined(CARLA_OS_MAC) && !defined(CARLA_PLUGIN_BUILD)
  71. NSView* const nsView = (NSView*)nsViewPtr;
  72. return [[nsView window] windowNumber];
  73. #else
  74. return 0;
  75. #endif
  76. }
  77. void carla_cocoa_set_transient_window_for(void* nsViewChildPtr, void* nsViewParentPtr)
  78. {
  79. CARLA_SAFE_ASSERT_RETURN(nsViewChildPtr != nullptr,);
  80. CARLA_SAFE_ASSERT_RETURN(nsViewParentPtr != nullptr,);
  81. #if defined(CARLA_OS_MAC) && !defined(CARLA_PLUGIN_BUILD)
  82. NSView* const nsViewChild = (NSView*)nsViewChildPtr;
  83. NSView* const nsViewParent = (NSView*)nsViewParentPtr;
  84. [[nsViewParent window] addChildWindow:[nsViewChild window]
  85. ordered:NSWindowAbove];
  86. #endif
  87. }
  88. void carla_x11_reparent_window(uintptr_t winId1, uintptr_t winId2)
  89. {
  90. CARLA_SAFE_ASSERT_RETURN(winId1 != 0,);
  91. CARLA_SAFE_ASSERT_RETURN(winId2 != 0,);
  92. #ifdef HAVE_X11
  93. if (::Display* const disp = XOpenDisplay(nullptr))
  94. {
  95. XReparentWindow(disp, winId1, winId2, 0, 0);
  96. XMapWindow(disp, winId1);
  97. XCloseDisplay(disp);
  98. }
  99. #endif
  100. }
  101. void carla_x11_move_window(uintptr_t winId, int x, int y)
  102. {
  103. CARLA_SAFE_ASSERT_RETURN(winId != 0,);
  104. #ifdef HAVE_X11
  105. if (::Display* const disp = XOpenDisplay(nullptr))
  106. {
  107. XMoveWindow(disp, winId, x, y);
  108. XCloseDisplay(disp);
  109. }
  110. #else
  111. // unused
  112. return; (void)x; (void)y;
  113. #endif
  114. }
  115. int* carla_x11_get_window_pos(uintptr_t winId)
  116. {
  117. static int pos[4];
  118. if (winId == 0)
  119. {
  120. pos[0] = 0;
  121. pos[1] = 0;
  122. pos[2] = 0;
  123. pos[3] = 0;
  124. }
  125. #ifdef HAVE_X11
  126. else if (::Display* const disp = XOpenDisplay(nullptr))
  127. {
  128. int x, y;
  129. Window child;
  130. XWindowAttributes xwa;
  131. XTranslateCoordinates(disp, winId, XRootWindow(disp, 0), 0, 0, &x, &y, &child);
  132. XGetWindowAttributes(disp, winId, &xwa);
  133. XCloseDisplay(disp);
  134. pos[0] = x - xwa.x;
  135. pos[1] = y - xwa.y;
  136. pos[2] = xwa.x;
  137. pos[3] = xwa.y;
  138. }
  139. #endif
  140. else
  141. {
  142. pos[0] = 0;
  143. pos[1] = 0;
  144. pos[2] = 0;
  145. pos[3] = 0;
  146. }
  147. return pos;
  148. }
  149. // -------------------------------------------------------------------------------------------------------------------