Collection of DPF-based plugins for packaging
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.

172 lines
3.8KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2016 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DISTRHO_EXTERNAL_WINDOW_HPP_INCLUDED
  17. #define DISTRHO_EXTERNAL_WINDOW_HPP_INCLUDED
  18. #include "String.hpp"
  19. #ifdef DISTRHO_OS_UNIX
  20. # include <cerrno>
  21. # include <sys/wait.h>
  22. # include <unistd.h>
  23. #else
  24. # error Unsupported platform!
  25. #endif
  26. START_NAMESPACE_DISTRHO
  27. // -----------------------------------------------------------------------
  28. // ExternalWindow class
  29. class ExternalWindow
  30. {
  31. public:
  32. ExternalWindow(const uint w = 1, const uint h = 1, const char* const t = "")
  33. : width(w),
  34. height(h),
  35. title(t),
  36. pid(0) {}
  37. virtual ~ExternalWindow()
  38. {
  39. terminateAndWaitForProcess();
  40. }
  41. uint getWidth() const noexcept
  42. {
  43. return width;
  44. }
  45. uint getHeight() const noexcept
  46. {
  47. return height;
  48. }
  49. const char* getTitle() const noexcept
  50. {
  51. return title;
  52. }
  53. void setTitle(const char* const t) noexcept
  54. {
  55. title = t;
  56. }
  57. bool isRunning() noexcept
  58. {
  59. if (pid <= 0)
  60. return false;
  61. const pid_t p = ::waitpid(pid, nullptr, WNOHANG);
  62. if (p == pid || (p == -1 && errno == ECHILD))
  63. {
  64. printf("NOTICE: Child process exited while idle\n");
  65. pid = 0;
  66. return false;
  67. }
  68. return true;
  69. }
  70. protected:
  71. bool startExternalProcess(const char* args[])
  72. {
  73. terminateAndWaitForProcess();
  74. pid = vfork();
  75. switch (pid)
  76. {
  77. case 0:
  78. execvp(args[0], (char**)args);
  79. _exit(1);
  80. return false;
  81. case -1:
  82. printf("Could not start external ui\n");
  83. return false;
  84. default:
  85. return true;
  86. }
  87. }
  88. private:
  89. uint width;
  90. uint height;
  91. String title;
  92. pid_t pid;
  93. friend class UIExporter;
  94. void terminateAndWaitForProcess()
  95. {
  96. if (pid <= 0)
  97. return;
  98. printf("Waiting for previous process to stop,,,\n");
  99. bool sendTerm = true;
  100. for (pid_t p;;)
  101. {
  102. p = ::waitpid(pid, nullptr, WNOHANG);
  103. switch (p)
  104. {
  105. case 0:
  106. if (sendTerm)
  107. {
  108. sendTerm = false;
  109. ::kill(pid, SIGTERM);
  110. }
  111. break;
  112. case -1:
  113. if (errno == ECHILD)
  114. {
  115. printf("Done! (no such process)\n");
  116. pid = 0;
  117. return;
  118. }
  119. break;
  120. default:
  121. if (p == pid)
  122. {
  123. printf("Done! (clean wait)\n");
  124. pid = 0;
  125. return;
  126. }
  127. break;
  128. }
  129. // 5 msec
  130. usleep(5*1000);
  131. }
  132. }
  133. DISTRHO_DECLARE_NON_COPY_CLASS(ExternalWindow)
  134. };
  135. // -----------------------------------------------------------------------
  136. END_NAMESPACE_DISTRHO
  137. #endif // DISTRHO_EXTERNAL_WINDOW_HPP_INCLUDED