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.

124 lines
3.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. ChildProcess::ChildProcess() {}
  20. ChildProcess::~ChildProcess() {}
  21. bool ChildProcess::isRunning() const
  22. {
  23. return activeProcess != nullptr && activeProcess->isRunning();
  24. }
  25. int ChildProcess::readProcessOutput (void* dest, int numBytes)
  26. {
  27. return activeProcess != nullptr ? activeProcess->read (dest, numBytes) : 0;
  28. }
  29. bool ChildProcess::kill()
  30. {
  31. return activeProcess == nullptr || activeProcess->killProcess();
  32. }
  33. uint32 ChildProcess::getExitCode() const
  34. {
  35. return activeProcess != nullptr ? activeProcess->getExitCode() : 0;
  36. }
  37. bool ChildProcess::waitForProcessToFinish (const int timeoutMs) const
  38. {
  39. auto timeoutTime = Time::getMillisecondCounter() + (uint32) timeoutMs;
  40. do
  41. {
  42. if (! isRunning())
  43. return true;
  44. Thread::sleep (2);
  45. }
  46. while (timeoutMs < 0 || Time::getMillisecondCounter() < timeoutTime);
  47. return false;
  48. }
  49. String ChildProcess::readAllProcessOutput()
  50. {
  51. MemoryOutputStream result;
  52. for (;;)
  53. {
  54. char buffer[512];
  55. auto num = readProcessOutput (buffer, sizeof (buffer));
  56. if (num <= 0)
  57. break;
  58. result.write (buffer, (size_t) num);
  59. }
  60. return result.toString();
  61. }
  62. uint32 ChildProcess::getPID() const noexcept
  63. {
  64. return activeProcess != nullptr ? activeProcess->getPID() : 0;
  65. }
  66. //==============================================================================
  67. //==============================================================================
  68. #if JUCE_UNIT_TESTS
  69. class ChildProcessTests : public UnitTest
  70. {
  71. public:
  72. ChildProcessTests()
  73. : UnitTest ("ChildProcess", UnitTestCategories::threads)
  74. {}
  75. void runTest() override
  76. {
  77. beginTest ("Child Processes");
  78. #if JUCE_WINDOWS || JUCE_MAC || JUCE_LINUX
  79. ChildProcess p;
  80. #if JUCE_WINDOWS
  81. expect (p.start ("tasklist"));
  82. #else
  83. expect (p.start ("ls /"));
  84. #endif
  85. auto output = p.readAllProcessOutput();
  86. expect (output.isNotEmpty());
  87. #endif
  88. }
  89. };
  90. static ChildProcessTests childProcessUnitTests;
  91. #endif
  92. } // namespace juce