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.

296 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. struct MessageHandler
  20. {
  21. virtual ~MessageHandler() {}
  22. virtual bool sendMessage (const ValueTree&) = 0;
  23. static MemoryBlock convertMessage (const ValueTree& tree)
  24. {
  25. MemoryOutputStream out;
  26. tree.writeToStream (out);
  27. return out.getMemoryBlock();
  28. }
  29. static ValueTree convertMessage (const MemoryBlock& rawData)
  30. {
  31. return ValueTree::readFromData (rawData.getData(), rawData.getSize());
  32. }
  33. };
  34. //==============================================================================
  35. static inline Rectangle<int> varToRect (const var& v)
  36. {
  37. if (const Array<var>* obj = v.getArray())
  38. {
  39. if (obj->size() == 4)
  40. {
  41. int intArray[4];
  42. for (int i = 0; i < 4; ++i)
  43. {
  44. const var& p = obj->getReference (i);
  45. if (p.isInt() || p.isDouble() || p.isInt64())
  46. intArray[i] = static_cast<int> (p);
  47. else
  48. return Rectangle<int>();
  49. }
  50. return Rectangle<int> (intArray[0], intArray[1], intArray[2], intArray[3]);
  51. }
  52. }
  53. return Rectangle<int>();
  54. }
  55. static inline var rectToVar (const Rectangle<int>& rect)
  56. {
  57. Array<var> retval;
  58. retval.add (var (rect.getX()));
  59. retval.add (var (rect.getY()));
  60. retval.add (var (rect.getWidth()));
  61. retval.add (var (rect.getHeight()));
  62. return var (retval);
  63. }
  64. //==============================================================================
  65. namespace MessageTypes
  66. {
  67. inline bool send (MessageHandler& target, const ValueTree& v)
  68. {
  69. //DBG ("Send: " << v.getType().toString());
  70. bool result = target.sendMessage (v);
  71. if (! result)
  72. {
  73. DBG ("*** Message failed: " << v.getType().toString());
  74. }
  75. return result;
  76. }
  77. inline bool sendPing (MessageHandler& target)
  78. {
  79. return send (target, ValueTree (PING));
  80. }
  81. //==============================================================================
  82. // client -> server
  83. inline void sendOpenPreview (MessageHandler& target, const ClassDatabase::Class& comp, Rectangle<int> mainWindowRect)
  84. {
  85. ValueTree v (OPEN_PREVIEW);
  86. v.setProperty (Ids::name, comp.getName(), nullptr);
  87. v.setProperty (Ids::bounds, rectToVar (mainWindowRect), nullptr);
  88. send (target, v);
  89. }
  90. inline void sendReinstantiate (MessageHandler& target)
  91. {
  92. send (target, ValueTree (RELOAD));
  93. }
  94. inline void sendFileChanges (MessageHandler& target, const Array<CodeChange>& changes, const File& file)
  95. {
  96. ValueTree changesMessage (MessageTypes::LIVE_FILE_CHANGES);
  97. changesMessage.setProperty (Ids::file, file.getFullPathName(), nullptr);
  98. for (const CodeChange& change : changes)
  99. {
  100. ValueTree v (CHANGE);
  101. v.setProperty (Ids::start, change.range.getStart(), nullptr);
  102. v.setProperty (Ids::end, change.range.getEnd(), nullptr);
  103. v.setProperty (Ids::text, change.text, nullptr);
  104. changesMessage.addChild (v, -1, nullptr);
  105. }
  106. send (target, changesMessage);
  107. }
  108. inline Array<CodeChange> getChangeArray (const ValueTree& changes)
  109. {
  110. Array<CodeChange> result;
  111. for (int i = 0; i < changes.getNumChildren(); ++i)
  112. {
  113. const ValueTree& v = changes.getChild (i);
  114. result.add (CodeChange (Range<int> (v[Ids::start], v[Ids::end]), v[Ids::text]));
  115. }
  116. return result;
  117. }
  118. inline void sendFileContentFullUpdate (MessageHandler& target, const File& file, const String& text)
  119. {
  120. ValueTree v (LIVE_FILE_UPDATE);
  121. v.setProperty (Ids::file, file.getFullPathName(), nullptr);
  122. v.setProperty (Ids::text, text, nullptr);
  123. send (target, v);
  124. }
  125. inline void sendHandleFileReset (MessageHandler& target, const File& file)
  126. {
  127. ValueTree v (LIVE_FILE_RESET);
  128. v.setProperty (Ids::file, file.getFullPathName(), nullptr);
  129. send (target, v);
  130. }
  131. inline void sendNewBuild (MessageHandler& target, const ProjectBuildInfo& build)
  132. {
  133. send (target, build.tree);
  134. }
  135. inline void sendCleanAll (MessageHandler& target)
  136. {
  137. send (target, ValueTree (CLEAN_ALL));
  138. }
  139. inline void sendNewDiagnosticList (MessageHandler& target, const ValueTree& list)
  140. {
  141. send (target, list);
  142. }
  143. inline void sendEmptyDiagnosticList (MessageHandler& target)
  144. {
  145. send (target, ValueTree (MessageTypes::DIAGNOSTIC_LIST));
  146. }
  147. inline void sendProcessActivationState (MessageHandler& target, bool isNowActive)
  148. {
  149. ValueTree v (FOREGROUND);
  150. v.setProperty (Ids::parentActive, isNowActive, nullptr);
  151. send (target, v);
  152. }
  153. inline void sendLaunchApp (MessageHandler& target) { send (target, ValueTree (LAUNCH_APP)); }
  154. inline void sendQuit (MessageHandler& target) { send (target, ValueTree (QUIT_SERVER)); }
  155. inline void sendShouldCloseIDE (MessageHandler& target) { send (target, ValueTree (QUIT_IDE)); }
  156. //==============================================================================
  157. // server -> client
  158. inline void sendNewClassList (MessageHandler& target, const ClassDatabase::ClassList& classes)
  159. {
  160. send (target, classes.toValueTree());
  161. }
  162. inline void sendCrash (MessageHandler& target, const String& message)
  163. {
  164. ValueTree v (CRASH);
  165. v.setProperty (Ids::message, message, nullptr);
  166. send (target, v);
  167. }
  168. inline void sendSystemHeadersMissing (MessageHandler& target)
  169. {
  170. send (target, ValueTree (MISSING_SYSTEM_HEADERS));
  171. }
  172. inline void sendBuildFailed (MessageHandler& target)
  173. {
  174. send (target, ValueTree (BUILD_FAILED));
  175. }
  176. inline void sendNewActivityList (MessageHandler& target, const StringArray& list)
  177. {
  178. ValueTree v (ACTIVITY_LIST);
  179. v.setProperty (Ids::list, concatenateListOfStrings (list), nullptr);
  180. send (target, v);
  181. }
  182. inline void sendChangeCode (MessageHandler& target, const String& location, const String& newText)
  183. {
  184. if (location.isNotEmpty())
  185. {
  186. ValueTree v (CHANGE_CODE);
  187. v.setProperty (Ids::position, location, nullptr);
  188. v.setProperty (Ids::text, newText, nullptr);
  189. send (target, v);
  190. }
  191. }
  192. inline void sendHighlightCode (MessageHandler& target, const String& location)
  193. {
  194. if (location.isNotEmpty())
  195. {
  196. ValueTree v (HIGHLIGHT_CODE);
  197. v.setProperty (Ids::position, location, nullptr);
  198. send (target, v);
  199. }
  200. }
  201. inline void sendAppLaunched (MessageHandler& target) { send (target, ValueTree (LAUNCHED)); }
  202. inline void sendAppQuit (MessageHandler& target) { send (target, ValueTree (APPQUIT)); }
  203. inline void sendKeyPress (MessageHandler& target, const String& className, const String& keyDesc)
  204. {
  205. ValueTree v (KEY);
  206. v.setProperty (Ids::class_, className, nullptr);
  207. v.setProperty (Ids::key, keyDesc, nullptr);
  208. send (target, v);
  209. }
  210. //==============================================================================
  211. template <class MessageHandlerType>
  212. static void dispatchToClient (MessageHandlerType& target, const ValueTree& v)
  213. {
  214. if (v.hasType (DIAGNOSTIC_LIST)) target.handleNewDiagnosticList (v);
  215. else if (v.hasType (ACTIVITY_LIST)) target.handleActivityListChanged (separateJoinedStrings (v [Ids::list]));
  216. else if (v.hasType (Ids::CLASSLIST)) target.handleClassListChanged (v);
  217. else if (v.hasType (BUILD_FAILED)) target.handleBuildFailed();
  218. else if (v.hasType (CHANGE_CODE)) target.handleChangeCode (v [Ids::position].toString(), v [Ids::text]);
  219. else if (v.hasType (HIGHLIGHT_CODE)) target.handleHighlightCode (v [Ids::position].toString());
  220. else if (v.hasType (LAUNCHED)) target.handleAppLaunched();
  221. else if (v.hasType (APPQUIT)) target.handleAppQuit();
  222. else if (v.hasType (PING)) target.handlePing();
  223. else if (v.hasType (CRASH)) target.handleCrash (v [Ids::message]);
  224. else if (v.hasType (KEY)) target.handleKeyPress (v[Ids::class_], KeyPress::createFromDescription (v[Ids::key]));
  225. else if (v.hasType (QUIT_IDE)) target.handleCloseIDE();
  226. else if (v.hasType (MISSING_SYSTEM_HEADERS)) target.handleMissingSystemHeaders();
  227. else jassertfalse;
  228. }
  229. template <class MessageHandlerType>
  230. static void dispatchToServer (MessageHandlerType& target, const ValueTree& v)
  231. {
  232. if (v.hasType (CLEAN_ALL)) target.handleCleanAll();
  233. else if (v.hasType (BUILDINFO)) target.handleNewBuildSettings (ProjectBuildInfo (v));
  234. else if (v.hasType (OPEN_PREVIEW)) target.handleOpenPreview (v[Ids::name], varToRect (v[Ids::bounds]));
  235. else if (v.hasType (RELOAD)) target.handleReinstantiatePreviews();
  236. else if (v.hasType (LAUNCH_APP)) target.handleLaunchApp();
  237. else if (v.hasType (LIVE_FILE_CHANGES)) target.handleLiveFileChanges (v[Ids::file].toString(), getChangeArray (v));
  238. else if (v.hasType (LIVE_FILE_UPDATE)) target.handleLiveFileFullUpdate (v[Ids::file].toString(), v[Ids::text]);
  239. else if (v.hasType (LIVE_FILE_RESET)) target.handleResetLiveFileContent (v[Ids::file].toString());
  240. else if (v.hasType (FOREGROUND)) target.handleProcessActivationState (v[Ids::parentActive]);
  241. else if (v.hasType (PING)) target.handlePing();
  242. else jassertfalse;
  243. }
  244. }