The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

218 lines
7.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. /**
  14. This component runs in a client process, draws the part of the canvas that this
  15. particular client covers, and updates itself when messages arrive from the master
  16. containing new canvas states.
  17. */
  18. class ClientCanvasComponent : public Component,
  19. private OSCSender,
  20. private OSCReceiver,
  21. private OSCReceiver::Listener<OSCReceiver::RealtimeCallback>,
  22. private AsyncUpdater,
  23. private Timer
  24. {
  25. public:
  26. ClientCanvasComponent (PropertiesFile& p, int windowIndex) : properties (p)
  27. {
  28. {
  29. String uuidPropName ("UUID" + String (windowIndex));
  30. clientName = properties.getValue (uuidPropName);
  31. if (clientName.isEmpty())
  32. {
  33. clientName = "CLIENT_" + String (Random().nextInt (10000)).toUpperCase();
  34. properties.setValue (uuidPropName, clientName);
  35. }
  36. }
  37. setOpaque (true);
  38. setSize (1500, 900);
  39. if (! OSCSender::connect (getBroadcastIPAddress(), clientPortNumber))
  40. error = "Client app OSC sender: network connection error.";
  41. if (! OSCReceiver::connect (masterPortNumber))
  42. error = "Client app OSC receiver: network connection error.";
  43. OSCReceiver::addListener (this);
  44. timerCallback();
  45. startTimer (2000);
  46. }
  47. ~ClientCanvasComponent() override
  48. {
  49. OSCReceiver::removeListener (this);
  50. }
  51. private:
  52. void mouseDrag (const MouseEvent& e) override
  53. {
  54. auto clientArea = getAreaInGlobalSpace();
  55. if (! clientArea.isEmpty())
  56. {
  57. OSCMessage message (userInputOSCAddress);
  58. message.addString (clientName);
  59. message.addFloat32 (e.position.x * clientArea.getWidth() / (float) getWidth() + clientArea.getX());
  60. message.addFloat32 (e.position.y * clientArea.getHeight() / (float) getHeight() + clientArea.getY());
  61. send (message);
  62. }
  63. }
  64. //==============================================================================
  65. void oscMessageReceived (const OSCMessage& message) override
  66. {
  67. auto address = message.getAddressPattern();
  68. if (address.matches (canvasStateOSCAddress))
  69. canvasStateOSCMessageReceived (message);
  70. }
  71. struct NewStateMessage : public Message
  72. {
  73. NewStateMessage (const MemoryBlock& d) : data (d) {}
  74. MemoryBlock data;
  75. };
  76. void canvasStateOSCMessageReceived (const OSCMessage& message)
  77. {
  78. if (message.isEmpty() || ! message[0].isBlob())
  79. return;
  80. if (packetiser.appendIncomingBlock (message[0].getBlob()))
  81. {
  82. const ScopedLock sl (canvasLock);
  83. MemoryBlock newCanvasData;
  84. if (packetiser.reassemble (newCanvasData))
  85. {
  86. MemoryInputStream i (newCanvasData.getData(), newCanvasData.getSize(), false);
  87. canvas2.load (i);
  88. triggerAsyncUpdate();
  89. }
  90. }
  91. }
  92. //==============================================================================
  93. String getMachineInfoToDisplay() const
  94. {
  95. auto* display = Desktop::getInstance().getDisplays().getDisplayForPoint (getScreenBounds().getCentre());
  96. return getOSName() + " " + String (display->dpi) + " " + String (display->scale);
  97. }
  98. static String getOSName()
  99. {
  100. #if JUCE_MAC
  101. return "Mac OSX";
  102. #elif JUCE_ANDROID
  103. return "Android";
  104. #elif JUCE_IOS
  105. return "iOS";
  106. #elif JUCE_WINDOWS
  107. return "Windows";
  108. #elif JUCE_LINUX
  109. return "Linux";
  110. #elif JUCE_BSD
  111. return "BSD";
  112. #endif
  113. }
  114. void paint (Graphics& g) override
  115. {
  116. g.fillAll (canvas.backgroundColour);
  117. auto clientArea = getAreaInGlobalSpace();
  118. if (clientArea.isEmpty())
  119. {
  120. g.setColour (Colours::red.withAlpha (0.5f));
  121. g.setFont (20.0f);
  122. g.drawText ("Not Connected", getLocalBounds(), Justification::centred, false);
  123. return;
  124. }
  125. canvas.draw (g, getLocalBounds().toFloat(), clientArea);
  126. g.setFont (Font (34.0f));
  127. g.setColour (Colours::white.withAlpha (0.6f));
  128. g.drawText (getMachineInfoToDisplay(),
  129. getLocalBounds().reduced (10).removeFromBottom (20),
  130. Justification::centredRight, true);
  131. if (error.isNotEmpty())
  132. {
  133. g.setColour (Colours::red);
  134. g.drawText (error, getLocalBounds().reduced (10).removeFromBottom (80),
  135. Justification::centredRight, true);
  136. }
  137. }
  138. Rectangle<float> getAreaInGlobalSpace() const
  139. {
  140. if (auto client = canvas.findClient (clientName))
  141. {
  142. auto screenBounds = getScreenBounds();
  143. auto* display = Desktop::getInstance().getDisplays().getDisplayForPoint (screenBounds.getCentre());
  144. return ((screenBounds - display->userArea.getCentre()).toFloat() / (client->scaleFactor * display->dpi / display->scale)) + client->centre;
  145. }
  146. return {};
  147. }
  148. Rectangle<float> getScreenAreaInGlobalSpace() const
  149. {
  150. if (auto client = canvas.findClient (clientName))
  151. {
  152. auto* display = Desktop::getInstance().getDisplays().getDisplayForPoint (getScreenBounds().getCentre());
  153. return (display->userArea.toFloat() / (client->scaleFactor * display->dpi / display->scale)).withCentre (client->centre);
  154. }
  155. return {};
  156. }
  157. void timerCallback() override
  158. {
  159. send (newClientOSCAddress, clientName + ":" + IPAddress::getLocalAddress().toString()
  160. + ":" + getScreenAreaInGlobalSpace().toString());
  161. }
  162. void handleAsyncUpdate() override
  163. {
  164. const ScopedLock sl (canvasLock);
  165. canvas.swapWith (canvas2);
  166. repaint();
  167. }
  168. SharedCanvasDescription canvas, canvas2;
  169. PropertiesFile& properties;
  170. String clientName, error;
  171. CriticalSection canvasLock;
  172. BlockPacketiser packetiser;
  173. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ClientCanvasComponent)
  174. };