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.

110 lines
4.0KB

  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. 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. #pragma once
  18. //==============================================================================
  19. /**
  20. Launches and monitors a child process.
  21. This class lets you launch an executable, and read its output. You can also
  22. use it to check whether the child process has finished.
  23. */
  24. class JUCE_API ChildProcess
  25. {
  26. public:
  27. //==============================================================================
  28. /** Creates a process object.
  29. To actually launch the process, use start().
  30. */
  31. ChildProcess();
  32. /** Destructor.
  33. Note that deleting this object won't terminate the child process.
  34. */
  35. ~ChildProcess();
  36. /** These flags are used by the start() methods. */
  37. enum StreamFlags
  38. {
  39. wantStdOut = 1,
  40. wantStdErr = 2
  41. };
  42. /** Attempts to launch a child process command.
  43. The command should be the name of the executable file, followed by any arguments
  44. that are required.
  45. If the process has already been launched, this will launch it again. If a problem
  46. occurs, the method will return false.
  47. The streamFlags is a combinations of values to indicate which of the child's output
  48. streams should be read and returned by readProcessOutput().
  49. */
  50. bool start (const String& command, int streamFlags = wantStdOut | wantStdErr);
  51. /** Attempts to launch a child process command.
  52. The first argument should be the name of the executable file, followed by any other
  53. arguments that are needed.
  54. If the process has already been launched, this will launch it again. If a problem
  55. occurs, the method will return false.
  56. The streamFlags is a combinations of values to indicate which of the child's output
  57. streams should be read and returned by readProcessOutput().
  58. */
  59. bool start (const StringArray& arguments, int streamFlags = wantStdOut | wantStdErr);
  60. /** Returns true if the child process is alive. */
  61. bool isRunning() const;
  62. /** Attempts to read some output from the child process.
  63. This will attempt to read up to the given number of bytes of data from the
  64. process. It returns the number of bytes that were actually read.
  65. */
  66. int readProcessOutput (void* destBuffer, int numBytesToRead);
  67. /** Blocks until the process has finished, and then returns its complete output
  68. as a string.
  69. */
  70. String readAllProcessOutput();
  71. /** Blocks until the process is no longer running. */
  72. bool waitForProcessToFinish (int timeoutMs) const;
  73. /** If the process has finished, this returns its exit code. */
  74. uint32 getExitCode() const;
  75. /** Attempts to kill the child process.
  76. Returns true if it succeeded. Trying to read from the process after calling this may
  77. result in undefined behaviour.
  78. */
  79. bool kill();
  80. private:
  81. //==============================================================================
  82. class ActiveProcess;
  83. friend struct ContainerDeletePolicy<ActiveProcess>;
  84. ScopedPointer<ActiveProcess> activeProcess;
  85. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildProcess)
  86. };