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.

220 lines
7.0KB

  1. /*
  2. * DISTRHO Cardinal Plugin
  3. * Copyright (C) 2021-2025 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 3 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 LICENSE file.
  16. */
  17. #include "Application.hpp"
  18. #include "CardinalPluginContext.hpp"
  19. #include "widget/event.hpp"
  20. #include <GLFW/glfw3.h>
  21. typedef struct GLFWcursor {
  22. DGL_NAMESPACE::MouseCursor cursorId;
  23. } GLFWcursor;
  24. GLFWAPI int glfwGetKeyScancode(int) { return 0; }
  25. GLFWAPI const char* glfwGetClipboardString(GLFWwindow*)
  26. {
  27. CardinalPluginContext* const context = static_cast<CardinalPluginContext*>(APP);
  28. DISTRHO_SAFE_ASSERT_RETURN(context != nullptr, nullptr);
  29. DISTRHO_SAFE_ASSERT_RETURN(context->tlw != nullptr, nullptr);
  30. size_t dataSize;
  31. return static_cast<const char*>(context->tlw->getClipboard(dataSize));
  32. }
  33. GLFWAPI void glfwSetClipboardString(GLFWwindow*, const char* const text)
  34. {
  35. DISTRHO_SAFE_ASSERT_RETURN(text != nullptr,);
  36. CardinalPluginContext* const context = static_cast<CardinalPluginContext*>(APP);
  37. DISTRHO_SAFE_ASSERT_RETURN(context != nullptr,);
  38. DISTRHO_SAFE_ASSERT_RETURN(context->tlw != nullptr,);
  39. context->tlw->setClipboard(nullptr, text, std::strlen(text)+1);
  40. }
  41. GLFWAPI GLFWcursor* glfwCreateStandardCursor(const int shape)
  42. {
  43. static GLFWcursor cursors[] = {
  44. { kMouseCursorArrow }, // GLFW_ARROW_CURSOR
  45. { kMouseCursorCaret }, // GLFW_IBEAM_CURSOR
  46. { kMouseCursorCrosshair }, // GLFW_CROSSHAIR_CURSOR
  47. { kMouseCursorHand }, // GLFW_POINTING_HAND_CURSOR
  48. { kMouseCursorNotAllowed }, // GLFW_NOT_ALLOWED_CURSOR
  49. { kMouseCursorLeftRight }, // GLFW_RESIZE_EW_CURSOR
  50. { kMouseCursorUpDown }, // GLFW_RESIZE_NS_CURSOR
  51. { kMouseCursorUpLeftDownRight }, // GLFW_RESIZE_NWSE_CURSOR
  52. { kMouseCursorUpRightDownLeft }, // GLFW_RESIZE_NESW_CURSOR
  53. { kMouseCursorAllScroll }, // GLFW_RESIZE_ALL_CURSOR
  54. };
  55. switch (shape)
  56. {
  57. case GLFW_ARROW_CURSOR:
  58. return &cursors[kMouseCursorArrow];
  59. case GLFW_IBEAM_CURSOR:
  60. return &cursors[kMouseCursorCaret];
  61. case GLFW_CROSSHAIR_CURSOR:
  62. return &cursors[kMouseCursorCrosshair];
  63. case GLFW_POINTING_HAND_CURSOR:
  64. return &cursors[kMouseCursorHand];
  65. case GLFW_NOT_ALLOWED_CURSOR:
  66. return &cursors[kMouseCursorNotAllowed];
  67. case GLFW_RESIZE_EW_CURSOR:
  68. return &cursors[kMouseCursorLeftRight];
  69. case GLFW_RESIZE_NS_CURSOR:
  70. return &cursors[kMouseCursorUpDown];
  71. case GLFW_RESIZE_NWSE_CURSOR:
  72. return &cursors[kMouseCursorUpLeftDownRight];
  73. case GLFW_RESIZE_NESW_CURSOR:
  74. return &cursors[kMouseCursorUpRightDownLeft];
  75. case GLFW_RESIZE_ALL_CURSOR:
  76. return &cursors[kMouseCursorAllScroll];
  77. default:
  78. return nullptr;
  79. }
  80. }
  81. GLFWAPI void glfwSetCursor(GLFWwindow*, GLFWcursor* const cursor)
  82. {
  83. CardinalPluginContext* const context = static_cast<CardinalPluginContext*>(APP);
  84. DISTRHO_SAFE_ASSERT_RETURN(context != nullptr,);
  85. DISTRHO_SAFE_ASSERT_RETURN(context->tlw != nullptr,);
  86. context->tlw->setCursor(cursor != nullptr ? cursor->cursorId : kMouseCursorArrow);
  87. }
  88. GLFWAPI double glfwGetTime(void)
  89. {
  90. CardinalPluginContext* const context = static_cast<CardinalPluginContext*>(APP);
  91. DISTRHO_SAFE_ASSERT_RETURN(context != nullptr, 0.0);
  92. DISTRHO_SAFE_ASSERT_RETURN(context->tlw != nullptr, 0.0);
  93. return context->tlw->getApp().getTime();
  94. }
  95. GLFWAPI const char* glfwGetKeyName(const int key, int)
  96. {
  97. switch (key)
  98. {
  99. case '\"': return "\"";
  100. case '\'': return "\'";
  101. case '\\': return "\\";
  102. case ' ': return " ";
  103. case '!': return "!";
  104. case '#': return "#";
  105. case '$': return "$";
  106. case '%': return "%";
  107. case '&': return "&";
  108. case '(': return "(";
  109. case ')': return ")";
  110. case '*': return "*";
  111. case '+': return "+";
  112. case ',': return ",";
  113. case '-': return "-";
  114. case '.': return ".";
  115. case '/': return "/";
  116. case '0': return "0";
  117. case '1': return "1";
  118. case '2': return "2";
  119. case '3': return "3";
  120. case '4': return "4";
  121. case '5': return "5";
  122. case '6': return "6";
  123. case '7': return "7";
  124. case '8': return "8";
  125. case '9': return "9";
  126. case ':': return ":";
  127. case ';': return ";";
  128. case '<': return "<";
  129. case '=': return "=";
  130. case '>': return ">";
  131. case '?': return "?";
  132. case '@': return "@";
  133. /* Rack expects lowercase, forced below
  134. case 'A': return "A";
  135. case 'B': return "B";
  136. case 'C': return "C";
  137. case 'D': return "D";
  138. case 'E': return "E";
  139. case 'F': return "F";
  140. case 'G': return "G";
  141. case 'H': return "H";
  142. case 'I': return "I";
  143. case 'J': return "J";
  144. case 'K': return "K";
  145. case 'L': return "L";
  146. case 'M': return "M";
  147. case 'N': return "N";
  148. case 'O': return "O";
  149. case 'P': return "P";
  150. case 'Q': return "Q";
  151. case 'R': return "R";
  152. case 'S': return "S";
  153. case 'T': return "T";
  154. case 'U': return "U";
  155. case 'V': return "V";
  156. case 'W': return "W";
  157. case 'X': return "X";
  158. case 'Y': return "Y";
  159. case 'Z': return "Z";
  160. */
  161. case '[': return "[";
  162. case ']': return "]";
  163. case '^': return "^";
  164. case '_': return "_";
  165. case '`': return "`";
  166. case 'a': case 'A': return "a";
  167. case 'b': case 'B': return "b";
  168. case 'c': case 'C': return "c";
  169. case 'd': case 'D': return "d";
  170. case 'e': case 'E': return "e";
  171. case 'f': case 'F': return "f";
  172. case 'g': case 'G': return "g";
  173. case 'h': case 'H': return "h";
  174. case 'i': case 'I': return "i";
  175. case 'j': case 'J': return "j";
  176. case 'k': case 'K': return "k";
  177. case 'l': case 'L': return "l";
  178. case 'm': case 'M': return "m";
  179. case 'n': case 'N': return "n";
  180. case 'o': case 'O': return "o";
  181. case 'p': case 'P': return "p";
  182. case 'q': case 'Q': return "q";
  183. case 'r': case 'R': return "r";
  184. case 's': case 'S': return "s";
  185. case 't': case 'T': return "t";
  186. case 'u': case 'U': return "u";
  187. case 'v': case 'V': return "v";
  188. case 'w': case 'W': return "w";
  189. case 'x': case 'X': return "x";
  190. case 'y': case 'Y': return "y";
  191. case 'z': case 'Z': return "z";
  192. default: return nullptr;
  193. }
  194. }
  195. int glfwGetKey(GLFWwindow*, const int key)
  196. {
  197. CardinalPluginContext* const context = static_cast<CardinalPluginContext*>(APP);
  198. DISTRHO_SAFE_ASSERT_RETURN(context != nullptr, GLFW_RELEASE);
  199. DISTRHO_SAFE_ASSERT_RETURN(context->event != nullptr, GLFW_RELEASE);
  200. return context->event->heldKeys.find(key) != context->event->heldKeys.end() ? GLFW_PRESS : GLFW_RELEASE;
  201. }