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.

144 lines
3.7KB

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