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.

216 lines
6.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  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 slave 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 SlaveCanvasComponent : public Component,
  19. private OSCSender,
  20. private OSCReceiver,
  21. private OSCReceiver::Listener<OSCReceiver::RealtimeCallback>,
  22. private AsyncUpdater,
  23. private Timer
  24. {
  25. public:
  26. SlaveCanvasComponent (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. ~SlaveCanvasComponent()
  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() / getWidth() + clientArea.getX());
  60. message.addFloat32 (e.position.y * clientArea.getHeight() / 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().findDisplayForPoint (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. #endif
  111. }
  112. void paint (Graphics& g) override
  113. {
  114. g.fillAll (canvas.backgroundColour);
  115. auto clientArea = getAreaInGlobalSpace();
  116. if (clientArea.isEmpty())
  117. {
  118. g.setColour (Colours::red.withAlpha (0.5f));
  119. g.setFont (20.0f);
  120. g.drawText ("Not Connected", getLocalBounds(), Justification::centred, false);
  121. return;
  122. }
  123. canvas.draw (g, getLocalBounds().toFloat(), clientArea);
  124. g.setFont (Font (34.0f));
  125. g.setColour (Colours::white.withAlpha (0.6f));
  126. g.drawText (getMachineInfoToDisplay(),
  127. getLocalBounds().reduced (10).removeFromBottom (20),
  128. Justification::centredRight, true);
  129. if (error.isNotEmpty())
  130. {
  131. g.setColour (Colours::red);
  132. g.drawText (error, getLocalBounds().reduced (10).removeFromBottom (80),
  133. Justification::centredRight, true);
  134. }
  135. }
  136. Rectangle<float> getAreaInGlobalSpace() const
  137. {
  138. if (auto client = canvas.findClient (clientName))
  139. {
  140. auto screenBounds = getScreenBounds();
  141. auto display = Desktop::getInstance().getDisplays().findDisplayForPoint (screenBounds.getCentre());
  142. return ((screenBounds - display.userArea.getCentre()).toFloat() / (client->scaleFactor * display.dpi / display.scale)) + client->centre;
  143. }
  144. return {};
  145. }
  146. Rectangle<float> getScreenAreaInGlobalSpace() const
  147. {
  148. if (auto client = canvas.findClient (clientName))
  149. {
  150. auto display = Desktop::getInstance().getDisplays().findDisplayForPoint (getScreenBounds().getCentre());
  151. return (display.userArea.toFloat() / (client->scaleFactor * display.dpi / display.scale)).withCentre (client->centre);
  152. }
  153. return {};
  154. }
  155. void timerCallback() override
  156. {
  157. send (newClientOSCAddress, clientName + ":" + IPAddress::getLocalAddress().toString()
  158. + ":" + getScreenAreaInGlobalSpace().toString());
  159. }
  160. void handleAsyncUpdate() override
  161. {
  162. const ScopedLock sl (canvasLock);
  163. canvas.swapWith (canvas2);
  164. repaint();
  165. }
  166. SharedCanvasDescription canvas, canvas2;
  167. PropertiesFile& properties;
  168. String clientName, error;
  169. CriticalSection canvasLock;
  170. BlockPacketiser packetiser;
  171. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SlaveCanvasComponent)
  172. };