[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / addons / ScreenSaver.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program 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 XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20 #include "ScreenSaver.h"
21 #include "settings/Settings.h"
22 #include "windowing/WindowingFactory.h"
23
24 #ifdef HAS_PYTHON
25 #include "interfaces/python/XBPython.h"
26 #include "utils/AlarmClock.h"
27
28 // What sound does a python screensaver make?
29 #define PYTHON_ALARM "sssssscreensaver"
30
31 #define PYTHON_SCRIPT_TIMEOUT 5 // seconds
32 #endif
33
34 namespace ADDON
35 {
36
37   CScreenSaver::CScreenSaver(const char *addonID)
38    : ADDON::CAddonDll<DllScreenSaver, ScreenSaver, SCR_PROPS>(AddonProps(addonID, ADDON_UNKNOWN, "", ""))
39   {
40   }
41
42 bool CScreenSaver::CreateScreenSaver()
43 {
44 #ifdef HAS_PYTHON
45   if (URIUtils::GetExtension(LibPath()).Equals(".py", false))
46   {
47     // Don't allow a previously-scheduled alarm to kill our new screensaver
48     g_alarmClock.Stop(PYTHON_ALARM, true);
49
50     if (!g_pythonParser.StopScript(LibPath()))
51       g_pythonParser.evalFile(LibPath(), AddonPtr(new CScreenSaver(Props())));
52     return true;
53   }
54 #endif
55  // pass it the screen width,height
56  // and the name of the screensaver
57   int iWidth = g_graphicsContext.GetWidth();
58   int iHeight = g_graphicsContext.GetHeight();
59
60   m_pInfo = new SCR_PROPS;
61 #ifdef HAS_DX
62   m_pInfo->device     = g_Windowing.Get3DDevice();
63 #else
64   m_pInfo->device     = NULL;
65 #endif
66   m_pInfo->x          = 0;
67   m_pInfo->y          = 0;
68   m_pInfo->width      = iWidth;
69   m_pInfo->height     = iHeight;
70   m_pInfo->pixelRatio = g_settings.m_ResInfo[g_graphicsContext.GetVideoResolution()].fPixelRatio;
71   m_pInfo->name       = strdup(Name().c_str());
72   m_pInfo->presets    = strdup(CSpecialProtocol::TranslatePath(Path()).c_str());
73   m_pInfo->profile    = strdup(CSpecialProtocol::TranslatePath(Profile()).c_str());
74
75   if (CAddonDll<DllScreenSaver, ScreenSaver, SCR_PROPS>::Create() == ADDON_STATUS_OK)
76     return true;
77
78   return false;
79 }
80
81 void CScreenSaver::Start()
82 {
83   // notify screen saver that they should start
84   if (Initialized()) m_pStruct->Start();
85 }
86
87 void CScreenSaver::Render()
88 {
89   // ask screensaver to render itself
90   if (Initialized()) m_pStruct->Render();
91 }
92
93 void CScreenSaver::GetInfo(SCR_INFO *info)
94 {
95   // get info from screensaver
96   if (Initialized()) m_pStruct->GetInfo(info);
97 }
98
99 void CScreenSaver::Destroy()
100 {
101 #ifdef HAS_PYTHON
102   if (URIUtils::GetExtension(LibPath()).Equals(".py", false))
103   {
104     g_alarmClock.Start(PYTHON_ALARM, PYTHON_SCRIPT_TIMEOUT, "StopScript(" + LibPath() + ")", true, false);
105     return;
106   }
107 #endif
108   // Release what was allocated in method CScreenSaver::CreateScreenSaver.
109   if (m_pInfo)
110   {
111     free((void *) m_pInfo->name);
112     free((void *) m_pInfo->presets);
113     free((void *) m_pInfo->profile);
114
115     delete m_pInfo;
116     m_pInfo = NULL;
117   }
118
119   CAddonDll<DllScreenSaver, ScreenSaver, SCR_PROPS>::Destroy();
120 }
121
122 } /*namespace ADDON*/
123