include/cinder/app/AppScreenSaver.h
Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2012, The Cinder Project, All rights reserved.
00003 
00004  This code is intended for use with the Cinder C++ library: http://libcinder.org
00005 
00006  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
00007  the following conditions are met:
00008 
00009     * Redistributions of source code must retain the above copyright notice, this list of conditions and
00010     the following disclaimer.
00011     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
00012     the following disclaimer in the documentation and/or other materials provided with the distribution.
00013 
00014  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00015  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00016  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00017  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00018  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00019  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00020  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00021  POSSIBILITY OF SUCH DAMAGE.
00022 */
00023 
00024 #pragma once
00025 
00026 #include "cinder/Cinder.h"
00027 #include "cinder/gl/gl.h"
00028 #include "cinder/app/App.h"
00029 
00030 #if defined( CINDER_MAC )
00031     #ifdef __OBJC__
00032         @class AppImplCocoaScreenSaver;
00033         @class NSWindow;
00034     #else
00035         class AppImplCocoaScreenSaver;
00036         class NSWindow;
00037     #endif
00038 #elif defined( CINDER_MSW )
00039     #include "cinder/app/AppImplMswScreenSaver.h"
00040     #include <windows.h>
00041     #undef min
00042     #undef max
00043     #include <ScrnSave.h>
00044 #endif
00045 
00046 
00047 namespace cinder { namespace app {
00048 
00049 class AppScreenSaver : public App {
00050  public:
00051     class Settings : public App::Settings {
00052       public:
00053         Settings()
00054             : App::Settings(),
00055 #if defined( CINDER_MAC )
00056             mEnableSecondaryDisplayBlanking( false ),
00057             mProvidesMacConfigDialog( false ),
00058 #elif defined( CINDER_MSW )
00059             mEnableSecondaryDisplayBlanking( true ),
00060 #endif
00061             mEnableDebug( false )
00062         {}
00063 
00065         void    enableSecondaryDisplayBlanking( bool enable = true ) { mEnableSecondaryDisplayBlanking = enable; }
00067         bool    isSecondaryDisplayBlankingEnabled() const { return mEnableSecondaryDisplayBlanking; }   
00068 
00069 #if defined( CINDER_MAC )
00070 
00071         void    setProvidesMacConfigDialog( bool provides = true ) { mProvidesMacConfigDialog = provides; }
00073         bool    getProvidesMacConfigDialog() const { return mProvidesMacConfigDialog; }
00074 #endif
00075 
00077         void    enableDebug( bool enable = true ) { mEnableDebug = true; }
00078         bool    isDebugEnabled() const { return mEnableDebug; }
00079 
00080       protected:
00081         bool    mEnableSecondaryDisplayBlanking;
00082 #if defined( CINDER_MAC )
00083         bool    mProvidesMacConfigDialog;
00084 #endif
00085         bool    mEnableDebug;
00086     };
00087 
00088     void    launch( const char *title, int argc, char * const argv[] ) { /* do nothing - this gets handled a weirder way for screensavers */ }
00089 
00090     virtual void        prepareSettings( Settings *settings ) {}
00091     const Settings&     getSettings() const { return mSettings; }
00092 
00094     virtual float       getFrameRate() const;
00096     virtual void        setFrameRate( float frameRate );
00097 
00099     bool                isPreview() const;
00100 
00102     virtual void            quit() {}
00103 
00104 #if defined( CINDER_MAC )
00105 
00106     virtual NSWindow*       createMacConfigDialog() { return NULL; }
00107 #elif defined( CINDER_MSW )
00108 
00109     static BOOL doConfigureDialog( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam ) {
00110         return FALSE;
00111     }
00112 #endif
00113 
00114     virtual fs::path        getAppPath() const;
00115 #if defined( CINDER_COCOA )
00116     virtual NSBundle*       getBundle() const;
00117 #endif
00118 
00119     virtual void    launch( RendererRef defaultRenderer, const char *title, int argc, char * const argv[] ) {}
00120 
00121     virtual size_t      getNumWindows() const override;
00122     virtual WindowRef   getWindow() const override;
00123     virtual WindowRef   getWindowIndex( size_t index ) const override;
00124 
00125 #if defined( CINDER_MAC )
00126     static void executeLaunch( AppScreenSaver *app, RendererRef renderer, const char *title );
00127 #elif defined( CINDER_MSW )
00128     static void executeLaunch( AppScreenSaver *app, RendererRef renderer, const char *title, ::HWND hwnd );
00129 #endif
00130 
00131 #if defined( CINDER_MAC )
00132     void            privateSetImpl__( void *impl ) { mImpl = reinterpret_cast<AppImplCocoaScreenSaver*>( impl ); }
00133 #elif defined( CINDER_MSW )
00134     void                            launch( ::HWND hwnd );
00135     virtual bool                    getsWindowsPaintEvents() { return false; }
00136     class AppImplMswScreenSaver*    getImpl() { return mImpl; }
00137 #endif
00138 
00139     virtual bool        receivesEvents() const { return false; }
00140 
00141  private:
00142     static AppScreenSaver       *sInstance;
00143 #if defined( CINDER_MAC )
00144     AppImplCocoaScreenSaver     *mImpl;
00145 #elif defined( CINDER_MSW )
00146     class AppImplMswScreenSaver *mImpl;
00147 #endif
00148     Settings            mSettings;
00149 };
00150 
00151 } } // namespace cinder::app
00152 
00153 #if defined( CINDER_MAC )
00154     extern "C" cinder::app::AppScreenSaver* ScreenSaverFactoryMethod( void *impl );
00155     #define CINDER_APP_SCREENSAVER( APP, RENDERER ) \
00156         extern "C" { \
00157         cinder::app::AppScreenSaver* ScreenSaverFactoryMethod( void *impl ) { \
00158             cinder::app::AppScreenSaver *app = new APP; \
00159             app->privateSetImpl__( impl ); \
00160             cinder::app::AppScreenSaver::executeLaunch( app, RendererRef( new RENDERER ), #APP );  \
00161             return app; \
00162             } \
00163         }
00164 #elif defined( CINDER_MSW )
00165     #define CINDER_APP_SCREENSAVER( APP, RENDERER ) \
00166         cinder::app::AppScreenSaver *sScreenSaverMswInstance = 0; \
00167         LRESULT CALLBACK ScreenSaverProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ) { \
00168             switch( message ) { \
00169                 case WM_CREATE: \
00170                     sScreenSaverMswInstance = new APP; \
00171                     cinder::app::AppScreenSaver::executeLaunch( sScreenSaverMswInstance, RendererRef( new RENDERER ), #APP, hWnd ); return 0; break; \
00172                 default: if( sScreenSaverMswInstance && sScreenSaverMswInstance->getImpl() ) return sScreenSaverMswInstance->getImpl()->eventHandler( hWnd, message, wParam, lParam ); \
00173                 else return ::DefScreenSaverProc( hWnd, message, wParam, lParam ); \
00174             } \
00175         } \
00176         extern "C" BOOL CALLBACK ScreenSaverConfigureDialog( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam ) { return APP::doConfigureDialog( hDlg, message, wParam, lParam ); } \
00177         extern "C" BOOL CALLBACK RegisterDialogClasses(HANDLE hInst) { return TRUE; }
00178 #endif