Main.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*\
  2. |*| Copyright 2015-2016 bill-auger <https://github.com/bill-auger/av-caster/issues>
  3. |*|
  4. |*| This file is part of the AvCaster program.
  5. |*|
  6. |*| AvCaster is free software: you can redistribute it and/or modify
  7. |*| it under the terms of the GNU General Public License version 3
  8. |*| as published by the Free Software Foundation.
  9. |*|
  10. |*| AvCaster is distributed in the hope that it will be useful,
  11. |*| but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. |*| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. |*| GNU General Public License for more details.
  14. |*|
  15. |*| You should have received a copy of the GNU General Public License
  16. |*| along with AvCaster. If not, see <http://www.gnu.org/licenses/>.
  17. \*/
  18. #include "Controllers/AvCaster.h"
  19. #include "Trace/TraceMain.h"
  20. class AvCasterApplication : public JUCEApplication , public MultiTimer
  21. {
  22. public:
  23. AvCasterApplication() {}
  24. void initialise(const String& command_line) override
  25. {
  26. DEBUG_TRACE_INIT_VERSION
  27. this->mainWindow = new MainWindow() ;
  28. if (AvCaster::Initialize(this , this->mainWindow->mainContent))
  29. {
  30. #ifdef JUCE_LINUX
  31. // create desktop launch file
  32. if (APP::desktopFile().loadFileAsString() != APP::desktopText())
  33. APP::desktopFile().replaceWithText(APP::desktopText()) ;
  34. // create desktop icon
  35. if (APP::iconFile().getSize() != APP::logoFile().getSize())
  36. {
  37. PNGImageFormat image_format = PNGImageFormat() ;
  38. Image icon_image = ImageCache::getFromMemory(BinaryData::avcasterlogo48_png ,
  39. BinaryData::avcasterlogo48_pngSize) ;
  40. FileOutputStream* icon_stream = new FileOutputStream(APP::iconFile()) ;
  41. if (!icon_stream->failedToOpen())
  42. image_format.writeImageToStream(icon_image , *icon_stream) ;
  43. delete icon_stream ;
  44. }
  45. #endif // JUCE_LINUX
  46. // start runtime timers
  47. startTimers() ;
  48. }
  49. else if (AvCaster::Alerts.size() > 0)
  50. { setApplicationReturnValue(255) ; startTimers() ; } // defer shutdown
  51. else { setApplicationReturnValue(255) ; shutdown() ; quit() ; }
  52. }
  53. void startTimers()
  54. {
  55. #ifndef DEBUG_QUIT_BEFORE_MAIN_LOOP
  56. for (int timer_n = 0 ; timer_n < APP::N_TIMERS ; ++timer_n)
  57. startTimer(APP::TIMER_IDS[timer_n] , APP::TIMER_IVLS[timer_n]) ;
  58. #else // DEBUG_QUIT_BEFORE_MAIN_LOOP
  59. Trace::TraceEvent("forced quit") ; shutdown() ; quit() ;
  60. #endif // DEBUG_QUIT_BEFORE_MAIN_LOOP
  61. }
  62. void stopTimers()
  63. {
  64. for (int timer_n = 0 ; timer_n > APP::N_TIMERS ; ++timer_n)
  65. stopTimer(APP::TIMER_IDS[timer_n]) ;
  66. }
  67. void shutdown() override
  68. {
  69. DEBUG_TRACE_SHUTDOWN_IN
  70. stopTimers() ;
  71. AvCaster::Shutdown() ;
  72. this->mainWindow = nullptr ;
  73. DEBUG_TRACE_SHUTDOWN_OUT
  74. }
  75. void anotherInstanceStarted(const String& command_line) override { }
  76. void systemRequestedQuit() override { this->quit() ; }
  77. const String getApplicationName() override { return ProjectInfo::projectName ; }
  78. const String getApplicationVersion() override { return ProjectInfo::versionString ; }
  79. bool moreThanOneInstanceAllowed() override { return false ; }
  80. /**
  81. MainWindow is the top-level ancestor class of all Components.
  82. It implements the desktop window that contains an instance of our MainContent class.
  83. */
  84. class MainWindow : public DocumentWindow
  85. {
  86. friend class AvCasterApplication ;
  87. friend class MainContent ;
  88. public:
  89. MainWindow() : DocumentWindow(APP::APP_NAME , Colour(0xff202020) , GUI::TITLEBAR_BTNS)
  90. {
  91. // main content
  92. this->mainContent = new MainContent() ;
  93. setContentOwned(this->mainContent , true) ;
  94. // this main desktop window
  95. Image icon_image = ImageCache::getFromMemory(BinaryData::avcasterlogo48_png ,
  96. BinaryData::avcasterlogo48_pngSize) ;
  97. setIcon(icon_image) ; getPeer()->setIcon(icon_image) ;
  98. #ifdef TRAY_ICON
  99. this->mainContent->trayIcon->setIconImage(icon_image) ;
  100. #endif // TRAY_ICON
  101. setUsingNativeTitleBar(false) ;
  102. #ifdef JUCE_MAC
  103. setTitleBarButtonsRequired(DocumentWindow::allButtons , true) ;
  104. #endif // JUCE_MAC
  105. setTitleBarHeight(GUI::TITLEBAR_H) ;
  106. centreWithSize(getWidth() , getHeight()) ;
  107. // setResizable(true , false) ; // TODO: resizeable preview ?
  108. setVisible(true) ;
  109. }
  110. ~MainWindow() { this->mainContent = nullptr ; }
  111. void closeButtonPressed() override
  112. {
  113. JUCEApplicationBase::getInstance()->systemRequestedQuit() ;
  114. }
  115. #ifdef TRAY_ICON
  116. void userTriedToCloseWindow()
  117. {
  118. JUCEApplicationBase::getInstance()->systemRequestedQuit() ;
  119. } // FIXME: this avoids assertion in juce_Component.cpp:737
  120. #endif // TRAY_ICON
  121. private:
  122. ScopedPointer<MainContent> mainContent ;
  123. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MainWindow)
  124. } ;
  125. private:
  126. #ifndef DEBUG_QUIT_AFTER_MAIN_LOOP
  127. void timerCallback(int timer_id) override { AvCaster::HandleTimer(timer_id) ; }
  128. #else // DEBUG_QUIT_AFTER_MAIN_LOOP
  129. void timerCallback(int timer_id) override
  130. {
  131. if (timer_id != APP::TIMER_LO_ID) AvCaster::HandleTimer(timer_id) ;
  132. else { Trace::TraceEvent("forced quit") ; quit() ; }
  133. }
  134. #endif // DEBUG_QUIT_AFTER_MAIN_LOOP
  135. ScopedPointer<MainWindow> mainWindow ;
  136. } ;
  137. // This macro generates the main() routine that launches the app.
  138. START_JUCE_APPLICATION(AvCasterApplication)