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.

146 lines
3.8KB

  1. /*
  2. * Carla Plugin Host
  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 "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. #ifdef CARLA_OS_MAC
  36. return [NSScreen mainScreen].backingScaleFactor;
  37. #endif
  38. return 1.0;
  39. }
  40. // -------------------------------------------------------------------------------------------------------------------
  41. int carla_cocoa_get_window(void* nsViewPtr)
  42. {
  43. CARLA_SAFE_ASSERT_RETURN(nsViewPtr != nullptr, 0);
  44. #if defined(CARLA_OS_MAC) && !defined(CARLA_PLUGIN_EXPORT)
  45. NSView* const nsView = (NSView*)nsViewPtr;
  46. return [[nsView window] windowNumber];
  47. #else
  48. return 0;
  49. #endif
  50. }
  51. void carla_cocoa_set_transient_window_for(void* nsViewChildPtr, void* nsViewParentPtr)
  52. {
  53. CARLA_SAFE_ASSERT_RETURN(nsViewChildPtr != nullptr,);
  54. CARLA_SAFE_ASSERT_RETURN(nsViewParentPtr != nullptr,);
  55. #if defined(CARLA_OS_MAC) && !defined(CARLA_PLUGIN_EXPORT)
  56. NSView* const nsViewChild = (NSView*)nsViewChildPtr;
  57. NSView* const nsViewParent = (NSView*)nsViewParentPtr;
  58. [[nsViewParent window] addChildWindow:[nsViewChild window]
  59. ordered:NSWindowAbove];
  60. #endif
  61. }
  62. void carla_x11_reparent_window(uintptr_t winId1, uintptr_t winId2)
  63. {
  64. CARLA_SAFE_ASSERT_RETURN(winId1 != 0,);
  65. CARLA_SAFE_ASSERT_RETURN(winId2 != 0,);
  66. #ifdef HAVE_X11
  67. if (::Display* const disp = XOpenDisplay(nullptr))
  68. {
  69. XReparentWindow(disp, winId1, winId2, 0, 0);
  70. XMapWindow(disp, winId1);
  71. XCloseDisplay(disp);
  72. }
  73. #endif
  74. }
  75. void carla_x11_move_window(uintptr_t winId, int x, int y)
  76. {
  77. CARLA_SAFE_ASSERT_RETURN(winId != 0,);
  78. #ifdef HAVE_X11
  79. if (::Display* const disp = XOpenDisplay(nullptr))
  80. {
  81. XMoveWindow(disp, winId, x, y);
  82. XCloseDisplay(disp);
  83. }
  84. #else
  85. // unused
  86. return; (void)x; (void)y;
  87. #endif
  88. }
  89. int* carla_x11_get_window_pos(uintptr_t winId)
  90. {
  91. static int pos[4];
  92. if (winId == 0)
  93. {
  94. pos[0] = 0;
  95. pos[1] = 0;
  96. pos[2] = 0;
  97. pos[3] = 0;
  98. }
  99. #ifdef HAVE_X11
  100. else if (::Display* const disp = XOpenDisplay(nullptr))
  101. {
  102. int x, y;
  103. Window child;
  104. XWindowAttributes xwa;
  105. XTranslateCoordinates(disp, winId, XRootWindow(disp, 0), 0, 0, &x, &y, &child);
  106. XGetWindowAttributes(disp, winId, &xwa);
  107. XCloseDisplay(disp);
  108. pos[0] = x - xwa.x;
  109. pos[1] = y - xwa.y;
  110. pos[2] = xwa.x;
  111. pos[3] = xwa.y;
  112. }
  113. #endif
  114. else
  115. {
  116. pos[0] = 0;
  117. pos[1] = 0;
  118. pos[2] = 0;
  119. pos[3] = 0;
  120. }
  121. return pos;
  122. }
  123. // -------------------------------------------------------------------------------------------------------------------