2 * Copyright 2003-2014 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Jérôme Duval, jerome.duval@free.fr
7 * Axel Dörfler, axeld@pinc-software.de
8 * Ryan Leavengood, leavengood@gmail.com
10 * John Scipione, jscipione@gmail.com
11 * Puck Meerburg, puck@puckipedia.nl
15 #include "ScreenBlanker.h"
20 #include <FindDirectory.h>
23 #include <StorageDefs.h>
24 #include <SupportDefs.h>
31 #include "ScreenSaverShared.h"
33 const static uint32 kMsgTurnOffScreen
= 'tofs';
34 const static uint32 kMsgSuspendScreen
= 'suss';
35 const static uint32 kMsgStandByScreen
= 'stbs';
38 // #pragma mark - ScreenBlanker
41 ScreenBlanker::ScreenBlanker()
43 BApplication(SCREEN_BLANKER_SIG
),
46 fPasswordWindow(NULL
),
49 fStandByScreenRunner(NULL
),
50 fSuspendScreenRunner(NULL
),
51 fTurnOffScreenRunner(NULL
)
53 fBlankTime
= system_time();
57 ScreenBlanker::~ScreenBlanker()
65 ScreenBlanker::ReadyToRun()
67 if (!fSettings
.Load())
68 fprintf(stderr
, "could not load settings, using defaults\n");
70 // create a BDirectWindow and start the render thread.
71 // TODO: we need a window per screen...
72 BScreen
screen(B_MAIN_SCREEN_ID
);
73 fWindow
= new ScreenSaverWindow(screen
.Frame(), fTestSaver
);
74 fPasswordWindow
= new PasswordWindow();
76 BView
* view
= fWindow
->ChildAt(0);
77 fSaverRunner
= new ScreenSaverRunner(fWindow
, view
, fSettings
);
78 fWindow
->SetSaverRunner(fSaverRunner
);
80 BScreenSaver
* saver
= fSaverRunner
->ScreenSaver();
81 if (saver
!= NULL
&& saver
->StartSaver(view
, false) == B_OK
)
84 fprintf(stderr
, "could not load the screensaver addon\n");
85 view
->SetViewColor(0, 0, 0);
86 // needed for Blackness saver
89 fWindow
->SetFullScreen(true);
93 _QueueTurnOffScreen();
98 ScreenBlanker::_TurnOnScreen()
100 delete fStandByScreenRunner
;
101 delete fSuspendScreenRunner
;
102 delete fTurnOffScreenRunner
;
104 fStandByScreenRunner
= fSuspendScreenRunner
= fTurnOffScreenRunner
= NULL
;
107 screen
.SetDPMS(B_DPMS_ON
);
112 ScreenBlanker::_SetDPMSMode(uint32 mode
)
115 screen
.SetDPMS(mode
);
117 if (fWindow
->Lock()) {
118 fSaverRunner
->Suspend();
125 ScreenBlanker::_ShowPasswordWindow()
129 if (fWindow
->Lock()) {
130 fSaverRunner
->Suspend();
133 // TODO: is that needed?
135 if (fPasswordWindow
->IsHidden())
136 fPasswordWindow
->Show();
141 _QueueResumeScreenSaver();
146 ScreenBlanker::_QueueResumeScreenSaver()
148 delete fResumeRunner
;
150 BMessage
resume(kMsgResumeSaver
);
151 fResumeRunner
= new BMessageRunner(BMessenger(this), &resume
,
152 fSettings
.BlankTime(), 1);
153 if (fResumeRunner
->InitCheck() != B_OK
)
154 syslog(LOG_ERR
, "resume screen saver runner failed\n");
159 ScreenBlanker::_QueueTurnOffScreen()
161 // stop running notifiers
163 delete fStandByScreenRunner
;
164 delete fSuspendScreenRunner
;
165 delete fTurnOffScreenRunner
;
167 fStandByScreenRunner
= fSuspendScreenRunner
= fTurnOffScreenRunner
= NULL
;
169 // figure out which notifiers we need and which of them are supported
171 uint32 flags
= fSettings
.TimeFlags();
173 uint32 capabilities
= screen
.DPMSCapabilites();
174 if ((capabilities
& B_DPMS_OFF
) == 0)
175 flags
&= ~ENABLE_DPMS_OFF
;
176 if ((capabilities
& B_DPMS_SUSPEND
) == 0)
177 flags
&= ~ENABLE_DPMS_SUSPEND
;
178 if ((capabilities
& B_DPMS_STAND_BY
) == 0)
179 flags
&= ~ENABLE_DPMS_STAND_BY
;
181 if ((flags
& ENABLE_DPMS_MASK
) == 0)
184 if (fSettings
.OffTime() == fSettings
.SuspendTime()
185 && (flags
& (ENABLE_DPMS_OFF
| ENABLE_DPMS_SUSPEND
))
186 == (ENABLE_DPMS_OFF
| ENABLE_DPMS_SUSPEND
))
187 flags
&= ~ENABLE_DPMS_SUSPEND
;
188 if (fSettings
.SuspendTime() == fSettings
.StandByTime()
189 && (flags
& (ENABLE_DPMS_SUSPEND
| ENABLE_DPMS_STAND_BY
))
190 == (ENABLE_DPMS_SUSPEND
| ENABLE_DPMS_STAND_BY
))
191 flags
&= ~ENABLE_DPMS_STAND_BY
;
193 // start them off again
195 if (flags
& ENABLE_DPMS_STAND_BY
) {
196 BMessage
dpms(kMsgStandByScreen
);
197 fStandByScreenRunner
= new BMessageRunner(BMessenger(this), &dpms
,
198 fSettings
.StandByTime(), 1);
199 if (fStandByScreenRunner
->InitCheck() != B_OK
)
200 syslog(LOG_ERR
, "standby screen saver runner failed\n");
203 if (flags
& ENABLE_DPMS_SUSPEND
) {
204 BMessage
dpms(kMsgSuspendScreen
);
205 fSuspendScreenRunner
= new BMessageRunner(BMessenger(this), &dpms
,
206 fSettings
.SuspendTime(), 1);
207 if (fSuspendScreenRunner
->InitCheck() != B_OK
)
208 syslog(LOG_ERR
, "suspend screen saver runner failed\n");
211 if (flags
& ENABLE_DPMS_OFF
) {
212 BMessage
dpms(kMsgTurnOffScreen
);
213 fTurnOffScreenRunner
= new BMessageRunner(BMessenger(this), &dpms
,
214 fSettings
.OffTime(), 1);
215 if (fTurnOffScreenRunner
->InitCheck() != B_OK
)
216 syslog(LOG_ERR
, "turn off screen saver runner failed\n");
222 ScreenBlanker::MessageReceived(BMessage
* message
)
224 switch (message
->what
) {
227 if (strcmp(fSettings
.Password(), crypt(fPasswordWindow
->Password(),
228 fSettings
.Password())) != 0) {
230 fPasswordWindow
->SetPassword("");
231 _QueueResumeScreenSaver();
233 PRINT(("Quitting!\n"));
240 case kMsgResumeSaver
:
242 if (fWindow
->Lock()) {
244 fPasswordWindow
->SetPassword("");
245 fPasswordWindow
->Hide();
247 fSaverRunner
->Resume();
251 // Turn on the message filter again
252 BMessage
enable(kMsgEnableFilter
);
253 fWindow
->PostMessage(&enable
);
255 _QueueTurnOffScreen();
259 case kMsgTurnOffScreen
:
260 _SetDPMSMode(B_DPMS_OFF
);
263 case kMsgSuspendScreen
:
264 _SetDPMSMode(B_DPMS_SUSPEND
);
267 case kMsgStandByScreen
:
268 _SetDPMSMode(B_DPMS_STAND_BY
);
276 BApplication::MessageReceived(message
);
282 ScreenBlanker::QuitRequested()
284 if (fSettings
.LockEnable()) {
285 bigtime_t minTime
= fSettings
.PasswordTime() - fSettings
.BlankTime();
288 if (system_time() - fBlankTime
> minTime
) {
289 _ShowPasswordWindow();
300 ScreenBlanker::IsPasswordWindowShown() const
302 return fPasswordWindow
!= NULL
&& !fPasswordWindow
->IsHidden();
307 ScreenBlanker::_Shutdown()
309 if (fWindow
!= NULL
) {
321 // #pragma mark - main
325 main(int argc
, char** argv
)