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.

908 lines
27KB

  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. #include "../Application/jucer_Headers.h"
  20. #include "../Application/jucer_Application.h"
  21. #include "../ProjectSaving/jucer_ProjectExporter.h"
  22. #include "jucer_MessageIDs.h"
  23. #include "jucer_CppHelpers.h"
  24. #include "jucer_SourceCodeRange.h"
  25. #include "jucer_ClassDatabase.h"
  26. #include "jucer_DiagnosticMessage.h"
  27. #include "jucer_ProjectBuildInfo.h"
  28. #include "jucer_ClientServerMessages.h"
  29. #include "jucer_CompileEngineClient.h"
  30. #include "jucer_CompileEngineServer.h"
  31. #include "jucer_CompileEngineSettings.h"
  32. #ifndef RUN_CLANG_IN_CHILD_PROCESS
  33. #error
  34. #endif
  35. //==============================================================================
  36. static File getProjucerTempFolder() noexcept
  37. {
  38. #if JUCE_MAC
  39. return { "~/Library/Caches/com.juce.projucer" };
  40. #else
  41. return File::getSpecialLocation (File::tempDirectory).getChildFile ("com.juce.projucer");
  42. #endif
  43. }
  44. static File getCacheLocationForProject (Project& project) noexcept
  45. {
  46. auto cacheFolderName = project.getProjectFilenameRootString() + "_" + project.getProjectUIDString();
  47. #if JUCE_DEBUG
  48. cacheFolderName += "_debug";
  49. #endif
  50. return getProjucerTempFolder().getChildFile ("Intermediate Files").getChildFile (cacheFolderName);
  51. }
  52. //==============================================================================
  53. class ClientIPC : public MessageHandler,
  54. private InterprocessConnection,
  55. private Timer
  56. {
  57. public:
  58. ClientIPC (CompileEngineChildProcess& cp)
  59. : InterprocessConnection (true), owner (cp)
  60. {
  61. launchServer();
  62. }
  63. ~ClientIPC()
  64. {
  65. #if RUN_CLANG_IN_CHILD_PROCESS
  66. if (childProcess.isRunning())
  67. {
  68. #if JUCE_DEBUG
  69. killServerPolitely();
  70. #else
  71. // in release builds we don't want to wait
  72. // for the server to clean up and shut down
  73. killServerWithoutMercy();
  74. #endif
  75. }
  76. #endif
  77. }
  78. void launchServer()
  79. {
  80. DBG ("Client: Launching Server...");
  81. auto pipeName = "ipc_" + String::toHexString (Random().nextInt64());
  82. auto command = createCommandLineForLaunchingServer (pipeName, owner.project.getProjectUIDString(),
  83. getCacheLocationForProject (owner.project));
  84. #if RUN_CLANG_IN_CHILD_PROCESS
  85. if (! childProcess.start (command))
  86. jassertfalse;
  87. #else
  88. server = createClangServer (command);
  89. #endif
  90. for (int i = 0; i < 20; ++i)
  91. {
  92. if (connectToPipe (pipeName, 10000))
  93. {
  94. MessageTypes::sendPing (*this);
  95. break;
  96. }
  97. Thread::sleep (50);
  98. }
  99. jassert (isConnected());
  100. startTimer (serverKeepAliveTimeout);
  101. }
  102. void killServerPolitely()
  103. {
  104. DBG ("Client: Killing Server...");
  105. MessageTypes::sendQuit (*this);
  106. disconnect();
  107. stopTimer();
  108. #if RUN_CLANG_IN_CHILD_PROCESS
  109. childProcess.waitForProcessToFinish (5000);
  110. #endif
  111. killServerWithoutMercy();
  112. }
  113. void killServerWithoutMercy()
  114. {
  115. disconnect();
  116. stopTimer();
  117. #if RUN_CLANG_IN_CHILD_PROCESS
  118. childProcess.kill();
  119. #else
  120. destroyClangServer (server);
  121. server = nullptr;
  122. #endif
  123. }
  124. void connectionMade()
  125. {
  126. DBG ("Client: connected");
  127. stopTimer();
  128. }
  129. void connectionLost()
  130. {
  131. DBG ("Client: disconnected");
  132. startTimer (100);
  133. }
  134. bool sendMessage (const ValueTree& m)
  135. {
  136. return InterprocessConnection::sendMessage (MessageHandler::convertMessage (m));
  137. }
  138. void messageReceived (const MemoryBlock& message)
  139. {
  140. #if RUN_CLANG_IN_CHILD_PROCESS
  141. startTimer (serverKeepAliveTimeout);
  142. #else
  143. stopTimer();
  144. #endif
  145. MessageTypes::dispatchToClient (owner, MessageHandler::convertMessage (message));
  146. }
  147. enum { serverKeepAliveTimeout = 10000 };
  148. private:
  149. CompileEngineChildProcess& owner;
  150. #if RUN_CLANG_IN_CHILD_PROCESS
  151. ChildProcess childProcess;
  152. #else
  153. void* server;
  154. #endif
  155. void timerCallback()
  156. {
  157. stopTimer();
  158. owner.handleCrash (String());
  159. }
  160. };
  161. //==============================================================================
  162. class CompileEngineChildProcess::ChildProcess : private ValueTree::Listener,
  163. private Timer
  164. {
  165. public:
  166. ChildProcess (CompileEngineChildProcess& proc, Project& p)
  167. : owner (proc), project (p)
  168. {
  169. projectRoot = project.getProjectRoot();
  170. restartServer();
  171. projectRoot.addListener (this);
  172. openedOk = true;
  173. }
  174. ~ChildProcess() override
  175. {
  176. projectRoot.removeListener (this);
  177. if (isRunningApp && server != nullptr)
  178. server->killServerWithoutMercy();
  179. server.reset();
  180. }
  181. void restartServer()
  182. {
  183. server.reset (new ClientIPC (owner));
  184. sendRebuild();
  185. }
  186. void sendRebuild()
  187. {
  188. stopTimer();
  189. ProjectBuildInfo build;
  190. if (! doesProjectMatchSavedHeaderState (project))
  191. {
  192. MessageTypes::sendNewBuild (*server, build);
  193. owner.errorList.resetToError ("Project structure does not match the saved headers! "
  194. "Please re-save your project to enable compilation");
  195. return;
  196. }
  197. if (areAnyModulesMissing (project))
  198. {
  199. MessageTypes::sendNewBuild (*server, build);
  200. owner.errorList.resetToError ("Some of your JUCE modules can't be found! "
  201. "Please check that all the module paths are correct");
  202. return;
  203. }
  204. build.setSystemIncludes (getSystemIncludePaths());
  205. build.setUserIncludes (getUserIncludes());
  206. build.setGlobalDefs (getGlobalDefs());
  207. build.setCompileFlags (project.getCompileEngineSettings().getExtraCompilerFlagsString());
  208. build.setExtraDLLs (getExtraDLLs());
  209. build.setJuceModulesFolder (project.getEnabledModules().getDefaultModulesFolder().getFullPathName());
  210. build.setUtilsCppInclude (project.getAppIncludeFile().getFullPathName());
  211. build.setWindowsTargetPlatformVersion (project.getCompileEngineSettings().getWindowsTargetPlatformVersionString());
  212. scanForProjectFiles (project, build);
  213. owner.updateAllEditors();
  214. MessageTypes::sendNewBuild (*server, build);
  215. }
  216. void cleanAll()
  217. {
  218. MessageTypes::sendCleanAll (*server);
  219. sendRebuild();
  220. }
  221. void reinstantiatePreviews()
  222. {
  223. MessageTypes::sendReinstantiate (*server);
  224. }
  225. bool launchApp()
  226. {
  227. MessageTypes::sendLaunchApp (*server);
  228. return true;
  229. }
  230. std::unique_ptr<ClientIPC> server;
  231. bool openedOk = false;
  232. bool isRunningApp = false;
  233. private:
  234. CompileEngineChildProcess& owner;
  235. Project& project;
  236. ValueTree projectRoot;
  237. void projectStructureChanged()
  238. {
  239. startTimer (100);
  240. }
  241. void timerCallback() override
  242. {
  243. sendRebuild();
  244. }
  245. void valueTreePropertyChanged (ValueTree&, const Identifier&) override { projectStructureChanged(); }
  246. void valueTreeChildAdded (ValueTree&, ValueTree&) override { projectStructureChanged(); }
  247. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override { projectStructureChanged(); }
  248. void valueTreeParentChanged (ValueTree&) override { projectStructureChanged(); }
  249. String getGlobalDefs()
  250. {
  251. StringArray defs;
  252. defs.add (project.getCompileEngineSettings().getExtraPreprocessorDefsString());
  253. {
  254. auto projectDefines = project.getPreprocessorDefs();
  255. for (int i = 0; i < projectDefines.size(); ++i)
  256. {
  257. auto def = projectDefines.getAllKeys()[i];
  258. auto value = projectDefines.getAllValues()[i];
  259. if (value.isNotEmpty())
  260. def << "=" << value;
  261. defs.add (def);
  262. }
  263. }
  264. for (Project::ExporterIterator exporter (project); exporter.next();)
  265. if (exporter->canLaunchProject())
  266. defs.add (exporter->getExporterIdentifierMacro() + "=1");
  267. defs.removeEmptyStrings();
  268. return defs.joinIntoString (" ");
  269. }
  270. static void scanProjectItem (const Project::Item& projectItem, Array<File>& compileUnits, Array<File>& userFiles)
  271. {
  272. if (projectItem.isGroup())
  273. {
  274. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  275. scanProjectItem (projectItem.getChild(i), compileUnits, userFiles);
  276. return;
  277. }
  278. if (projectItem.shouldBeCompiled())
  279. {
  280. auto f = projectItem.getFile();
  281. if (f.exists())
  282. compileUnits.add (f);
  283. }
  284. if (projectItem.shouldBeAddedToTargetProject() && ! projectItem.shouldBeAddedToBinaryResources())
  285. {
  286. auto f = projectItem.getFile();
  287. if (f.exists())
  288. userFiles.add (f);
  289. }
  290. }
  291. void scanForProjectFiles (Project& proj, ProjectBuildInfo& build)
  292. {
  293. Array<File> compileUnits, userFiles;
  294. scanProjectItem (proj.getMainGroup(), compileUnits, userFiles);
  295. {
  296. auto isVSTHost = project.getEnabledModules().isModuleEnabled ("juce_audio_processors")
  297. && (project.isConfigFlagEnabled ("JUCE_PLUGINHOST_VST3") || project.isConfigFlagEnabled ("JUCE_PLUGINHOST_VST"));
  298. auto isPluginProject = proj.isAudioPluginProject();
  299. OwnedArray<LibraryModule> modules;
  300. proj.getEnabledModules().createRequiredModules (modules);
  301. for (Project::ExporterIterator exporter (proj); exporter.next();)
  302. {
  303. if (exporter->canLaunchProject())
  304. {
  305. for (auto* m : modules)
  306. {
  307. auto copyLocally = proj.getEnabledModules().shouldCopyModuleFilesLocally (m->moduleInfo.getID());
  308. auto localModuleFolder = copyLocally ? proj.getLocalModuleFolder (m->moduleInfo.getID())
  309. : m->moduleInfo.getFolder();
  310. m->findAndAddCompiledUnits (*exporter, nullptr, compileUnits,
  311. isPluginProject || isVSTHost ? ProjectType::Target::SharedCodeTarget
  312. : ProjectType::Target::unspecified);
  313. if (isPluginProject || isVSTHost)
  314. m->findAndAddCompiledUnits (*exporter, nullptr, compileUnits, ProjectType::Target::StandalonePlugIn);
  315. }
  316. break;
  317. }
  318. }
  319. }
  320. for (int i = 0; ; ++i)
  321. {
  322. auto binaryDataCpp = proj.getBinaryDataCppFile (i);
  323. if (! binaryDataCpp.exists())
  324. break;
  325. compileUnits.add (binaryDataCpp);
  326. }
  327. for (auto i = compileUnits.size(); --i >= 0;)
  328. if (compileUnits.getReference(i).hasFileExtension (".r"))
  329. compileUnits.remove (i);
  330. build.setFiles (compileUnits, userFiles);
  331. }
  332. static bool doesProjectMatchSavedHeaderState (Project& project)
  333. {
  334. auto liveModules = project.getProjectRoot().getChildWithName (Ids::MODULES);
  335. if (auto xml = parseXMLIfTagMatches (project.getFile(), Ids::JUCERPROJECT.toString()))
  336. {
  337. auto diskModules = ValueTree::fromXml (*xml).getChildWithName (Ids::MODULES);
  338. return liveModules.isEquivalentTo (diskModules);
  339. }
  340. return false;
  341. }
  342. static bool areAnyModulesMissing (Project& project)
  343. {
  344. OwnedArray<LibraryModule> modules;
  345. project.getEnabledModules().createRequiredModules (modules);
  346. for (auto* module : modules)
  347. if (! module->getFolder().isDirectory())
  348. return true;
  349. return false;
  350. }
  351. StringArray getUserIncludes()
  352. {
  353. StringArray paths;
  354. paths.add (project.getGeneratedCodeFolder().getFullPathName());
  355. paths.addArray (getSearchPathsFromString (project.getCompileEngineSettings().getUserHeaderPathString()));
  356. return convertSearchPathsToAbsolute (paths);
  357. }
  358. StringArray getSystemIncludePaths()
  359. {
  360. StringArray paths;
  361. paths.add (project.getGeneratedCodeFolder().getFullPathName());
  362. paths.addArray (getSearchPathsFromString (project.getCompileEngineSettings().getSystemHeaderPathString()));
  363. auto isVSTHost = project.getEnabledModules().isModuleEnabled ("juce_audio_processors")
  364. && (project.isConfigFlagEnabled ("JUCE_PLUGINHOST_VST3")
  365. || project.isConfigFlagEnabled ("JUCE_PLUGINHOST_VST"));
  366. auto customVst3Path = getAppSettings().getStoredPath (Ids::vst3Path, TargetOS::getThisOS()).get().toString();
  367. if (customVst3Path.isNotEmpty() && (project.isAudioPluginProject() || isVSTHost))
  368. paths.add (customVst3Path);
  369. OwnedArray<LibraryModule> modules;
  370. project.getEnabledModules().createRequiredModules (modules);
  371. for (auto* module : modules)
  372. {
  373. paths.addIfNotAlreadyThere (module->getFolder().getParentDirectory().getFullPathName());
  374. if (customVst3Path.isEmpty() && (project.isAudioPluginProject() || isVSTHost))
  375. if (module->getID() == "juce_audio_processors")
  376. paths.addIfNotAlreadyThere (module->getFolder().getChildFile ("format_types").getChildFile ("VST3_SDK").getFullPathName());
  377. }
  378. return convertSearchPathsToAbsolute (paths);
  379. }
  380. StringArray convertSearchPathsToAbsolute (const StringArray& paths) const
  381. {
  382. StringArray s;
  383. const File root (project.getProjectFolder());
  384. for (String p : paths)
  385. s.add (root.getChildFile (p).getFullPathName());
  386. return s;
  387. }
  388. StringArray getExtraDLLs()
  389. {
  390. auto dlls = StringArray::fromTokens (project.getCompileEngineSettings().getExtraDLLsString(), "\n\r,", {});
  391. dlls.trim();
  392. dlls.removeEmptyStrings();
  393. return dlls;
  394. }
  395. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildProcess)
  396. };
  397. //==============================================================================
  398. CompileEngineChildProcess::CompileEngineChildProcess (Project& p)
  399. : project (p)
  400. {
  401. ProjucerApplication::getApp().openDocumentManager.addListener (this);
  402. createProcess();
  403. errorList.setWarningsEnabled (project.getCompileEngineSettings().areWarningsEnabled());
  404. }
  405. CompileEngineChildProcess::~CompileEngineChildProcess()
  406. {
  407. ProjucerApplication::getApp().openDocumentManager.removeListener (this);
  408. process.reset();
  409. lastComponentList.clear();
  410. }
  411. void CompileEngineChildProcess::createProcess()
  412. {
  413. jassert (process == nullptr);
  414. process.reset (new ChildProcess (*this, project));
  415. if (! process->openedOk)
  416. process.reset();
  417. updateAllEditors();
  418. }
  419. void CompileEngineChildProcess::cleanAll()
  420. {
  421. if (process != nullptr)
  422. process->cleanAll();
  423. }
  424. void CompileEngineChildProcess::openPreview (const ClassDatabase::Class& comp)
  425. {
  426. if (process != nullptr)
  427. {
  428. MainWindow* projectWindow = nullptr;
  429. OwnedArray<MainWindow>& windows = ProjucerApplication::getApp().mainWindowList.windows;
  430. for (int i = 0; i < windows.size(); ++i)
  431. {
  432. if (MainWindow* w = windows[i])
  433. {
  434. if (w->getProject() == &project)
  435. {
  436. projectWindow = w;
  437. break;
  438. }
  439. }
  440. }
  441. Rectangle<int> mainWindowRect;
  442. if (projectWindow != nullptr)
  443. mainWindowRect = projectWindow->getBounds();
  444. MessageTypes::sendOpenPreview (*process->server, comp, mainWindowRect);
  445. }
  446. }
  447. void CompileEngineChildProcess::reinstantiatePreviews()
  448. {
  449. if (process != nullptr)
  450. process->reinstantiatePreviews();
  451. }
  452. void CompileEngineChildProcess::processActivationChanged (bool isForeground)
  453. {
  454. if (process != nullptr)
  455. MessageTypes::sendProcessActivationState (*process->server, isForeground);
  456. }
  457. //==============================================================================
  458. bool CompileEngineChildProcess::canLaunchApp() const
  459. {
  460. return process != nullptr
  461. && runningAppProcess == nullptr
  462. && activityList.getNumActivities() == 0
  463. && errorList.getNumErrors() == 0
  464. && project.getProjectType().isGUIApplication();
  465. }
  466. void CompileEngineChildProcess::launchApp()
  467. {
  468. if (process != nullptr)
  469. process->launchApp();
  470. }
  471. bool CompileEngineChildProcess::canKillApp() const
  472. {
  473. return runningAppProcess != nullptr;
  474. }
  475. void CompileEngineChildProcess::killApp()
  476. {
  477. runningAppProcess.reset();
  478. }
  479. void CompileEngineChildProcess::handleAppLaunched()
  480. {
  481. runningAppProcess.reset (process.release());
  482. runningAppProcess->isRunningApp = true;
  483. createProcess();
  484. }
  485. void CompileEngineChildProcess::handleAppQuit()
  486. {
  487. DBG ("handleAppQuit");
  488. runningAppProcess.reset();
  489. }
  490. bool CompileEngineChildProcess::isAppRunning() const noexcept
  491. {
  492. return runningAppProcess != nullptr && runningAppProcess->isRunningApp;
  493. }
  494. //==============================================================================
  495. struct CompileEngineChildProcess::Editor : private CodeDocument::Listener,
  496. private Timer
  497. {
  498. Editor (CompileEngineChildProcess& ccp, const File& f, CodeDocument& doc)
  499. : owner (ccp), file (f), document (doc), transactionTimer (doc)
  500. {
  501. sendFullUpdate();
  502. document.addListener (this);
  503. }
  504. ~Editor() override
  505. {
  506. document.removeListener (this);
  507. }
  508. void codeDocumentTextInserted (const String& newText, int insertIndex) override
  509. {
  510. CodeChange (Range<int> (insertIndex, insertIndex), newText).addToList (pendingChanges);
  511. startEditorChangeTimer();
  512. transactionTimer.stopTimer();
  513. owner.lastComponentList.globalNamespace
  514. .nudgeAllCodeRanges (file.getFullPathName(), insertIndex, newText.length());
  515. }
  516. void codeDocumentTextDeleted (int start, int end) override
  517. {
  518. CodeChange (Range<int> (start, end), String()).addToList (pendingChanges);
  519. startEditorChangeTimer();
  520. transactionTimer.stopTimer();
  521. owner.lastComponentList.globalNamespace
  522. .nudgeAllCodeRanges (file.getFullPathName(), start, start - end);
  523. }
  524. void sendFullUpdate()
  525. {
  526. reset();
  527. if (owner.process != nullptr)
  528. MessageTypes::sendFileContentFullUpdate (*owner.process->server, file, document.getAllContent());
  529. }
  530. bool flushEditorChanges()
  531. {
  532. if (pendingChanges.size() > 0)
  533. {
  534. if (owner.process != nullptr && owner.process->server != nullptr)
  535. MessageTypes::sendFileChanges (*owner.process->server, pendingChanges, file);
  536. reset();
  537. return true;
  538. }
  539. stopTimer();
  540. return false;
  541. }
  542. void reset()
  543. {
  544. stopTimer();
  545. pendingChanges.clear();
  546. }
  547. void startTransactionTimer()
  548. {
  549. transactionTimer.startTimer (1000);
  550. }
  551. void startEditorChangeTimer()
  552. {
  553. startTimer (200);
  554. }
  555. CompileEngineChildProcess& owner;
  556. File file;
  557. CodeDocument& document;
  558. private:
  559. Array<CodeChange> pendingChanges;
  560. void timerCallback() override
  561. {
  562. if (owner.project.getCompileEngineSettings().isContinuousRebuildEnabled())
  563. flushEditorChanges();
  564. else
  565. stopTimer();
  566. }
  567. struct TransactionTimer : public Timer
  568. {
  569. TransactionTimer (CodeDocument& doc) : document (doc) {}
  570. void timerCallback() override
  571. {
  572. stopTimer();
  573. document.newTransaction();
  574. }
  575. CodeDocument& document;
  576. };
  577. TransactionTimer transactionTimer;
  578. };
  579. void CompileEngineChildProcess::editorOpened (const File& file, CodeDocument& document)
  580. {
  581. editors.add (new Editor (*this, file, document));
  582. }
  583. bool CompileEngineChildProcess::documentAboutToClose (OpenDocumentManager::Document* document)
  584. {
  585. for (int i = editors.size(); --i >= 0;)
  586. {
  587. if (document->getFile() == editors.getUnchecked(i)->file)
  588. {
  589. const File f (editors.getUnchecked(i)->file);
  590. editors.remove (i);
  591. if (process != nullptr)
  592. MessageTypes::sendHandleFileReset (*process->server, f);
  593. }
  594. }
  595. return true;
  596. }
  597. void CompileEngineChildProcess::updateAllEditors()
  598. {
  599. for (int i = editors.size(); --i >= 0;)
  600. editors.getUnchecked(i)->sendFullUpdate();
  601. }
  602. //==============================================================================
  603. void CompileEngineChildProcess::handleCrash (const String& message)
  604. {
  605. Logger::writeToLog ("*** Child process crashed: " + message);
  606. if (crashHandler != nullptr)
  607. crashHandler (message);
  608. }
  609. void CompileEngineChildProcess::handleNewDiagnosticList (const ValueTree& l) { errorList.setList (l); }
  610. void CompileEngineChildProcess::handleActivityListChanged (const StringArray& l) { activityList.setList (l); }
  611. void CompileEngineChildProcess::handleCloseIDE()
  612. {
  613. if (JUCEApplication* app = JUCEApplication::getInstance())
  614. app->systemRequestedQuit();
  615. }
  616. void CompileEngineChildProcess::handleMissingSystemHeaders()
  617. {
  618. if (ProjectContentComponent* p = findProjectContentComponent())
  619. p->handleMissingSystemHeaders();
  620. }
  621. void CompileEngineChildProcess::handleKeyPress (const String& className, const KeyPress& key)
  622. {
  623. ApplicationCommandManager& commandManager = ProjucerApplication::getCommandManager();
  624. CommandID command = commandManager.getKeyMappings()->findCommandForKeyPress (key);
  625. if (command == StandardApplicationCommandIDs::undo)
  626. {
  627. handleUndoInEditor (className);
  628. }
  629. else if (command == StandardApplicationCommandIDs::redo)
  630. {
  631. handleRedoInEditor (className);
  632. }
  633. else if (ApplicationCommandTarget* const target = ApplicationCommandManager::findTargetForComponent (findProjectContentComponent()))
  634. {
  635. commandManager.setFirstCommandTarget (target);
  636. commandManager.getKeyMappings()->keyPressed (key, findProjectContentComponent());
  637. commandManager.setFirstCommandTarget (nullptr);
  638. }
  639. }
  640. void CompileEngineChildProcess::handleUndoInEditor (const String& /*className*/)
  641. {
  642. }
  643. void CompileEngineChildProcess::handleRedoInEditor (const String& /*className*/)
  644. {
  645. }
  646. void CompileEngineChildProcess::handleClassListChanged (const ValueTree& newList)
  647. {
  648. lastComponentList = ClassDatabase::ClassList::fromValueTree (newList);
  649. activityList.sendClassListChangedMessage (lastComponentList);
  650. }
  651. void CompileEngineChildProcess::handleBuildFailed()
  652. {
  653. ProjucerApplication::getCommandManager().commandStatusChanged();
  654. }
  655. void CompileEngineChildProcess::handleChangeCode (const SourceCodeRange& location, const String& newText)
  656. {
  657. if (Editor* ed = getOrOpenEditorFor (location.file))
  658. {
  659. if (ed->flushEditorChanges())
  660. return; // client-side editor changes were pending, so deal with them first, and discard
  661. // the incoming change, whose position may now be wrong.
  662. ed->document.deleteSection (location.range.getStart(), location.range.getEnd());
  663. ed->document.insertText (location.range.getStart(), newText);
  664. // deliberately clear the messages that we just added, to avoid these changes being
  665. // sent to the server (which will already have processed the same ones locally)
  666. ed->reset();
  667. ed->startTransactionTimer();
  668. }
  669. }
  670. void CompileEngineChildProcess::handlePing()
  671. {
  672. }
  673. //==============================================================================
  674. void CompileEngineChildProcess::flushEditorChanges()
  675. {
  676. for (Editor* ed : editors)
  677. ed->flushEditorChanges();
  678. }
  679. ProjectContentComponent* CompileEngineChildProcess::findProjectContentComponent() const
  680. {
  681. for (MainWindow* mw : ProjucerApplication::getApp().mainWindowList.windows)
  682. if (mw->getProject() == &project)
  683. return mw->getProjectContentComponent();
  684. return nullptr;
  685. }
  686. CompileEngineChildProcess::Editor* CompileEngineChildProcess::getOrOpenEditorFor (const File& file)
  687. {
  688. for (Editor* ed : editors)
  689. if (ed->file == file)
  690. return ed;
  691. if (ProjectContentComponent* pcc = findProjectContentComponent())
  692. if (pcc->showEditorForFile (file, false))
  693. return getOrOpenEditorFor (file);
  694. return nullptr;
  695. }
  696. void CompileEngineChildProcess::handleHighlightCode (const SourceCodeRange& location)
  697. {
  698. ProjectContentComponent* pcc = findProjectContentComponent();
  699. if (pcc != nullptr && pcc->showEditorForFile (location.file, false))
  700. {
  701. SourceCodeEditor* sce = dynamic_cast <SourceCodeEditor*> (pcc->getEditorComponent());
  702. if (sce != nullptr && sce->editor != nullptr)
  703. {
  704. sce->highlight (location.range, true);
  705. Process::makeForegroundProcess();
  706. CodeEditorComponent& ed = *sce->editor;
  707. ed.getTopLevelComponent()->toFront (false);
  708. ed.grabKeyboardFocus();
  709. }
  710. }
  711. }
  712. void CompileEngineChildProcess::cleanAllCachedFilesForProject (Project& p)
  713. {
  714. File cacheFolder (getCacheLocationForProject (p));
  715. if (cacheFolder.isDirectory())
  716. cacheFolder.deleteRecursively();
  717. }