btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / bin / screen_blanker / ScreenBlanker.cpp
blob87d387a9ec991d817a11f2c388306c635350348f
1 /*
2 * Copyright 2003-2014 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Jérôme Duval, jerome.duval@free.fr
7 * Axel Dörfler, axeld@pinc-software.de
8 * Ryan Leavengood, leavengood@gmail.com
9 * Michael Phipps
10 * John Scipione, jscipione@gmail.com
11 * Puck Meerburg, puck@puckipedia.nl
15 #include "ScreenBlanker.h"
17 #include <Beep.h>
18 #include <Debug.h>
19 #include <File.h>
20 #include <FindDirectory.h>
21 #include <Path.h>
22 #include <Screen.h>
23 #include <StorageDefs.h>
24 #include <SupportDefs.h>
25 #include <image.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <syslog.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),
44 fWindow(NULL),
45 fSaverRunner(NULL),
46 fPasswordWindow(NULL),
47 fTestSaver(false),
48 fResumeRunner(NULL),
49 fStandByScreenRunner(NULL),
50 fSuspendScreenRunner(NULL),
51 fTurnOffScreenRunner(NULL)
53 fBlankTime = system_time();
57 ScreenBlanker::~ScreenBlanker()
59 delete fResumeRunner;
60 _TurnOnScreen();
64 void
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)
82 fSaverRunner->Run();
83 else {
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);
90 fWindow->Show();
91 HideCursor();
93 _QueueTurnOffScreen();
97 void
98 ScreenBlanker::_TurnOnScreen()
100 delete fStandByScreenRunner;
101 delete fSuspendScreenRunner;
102 delete fTurnOffScreenRunner;
104 fStandByScreenRunner = fSuspendScreenRunner = fTurnOffScreenRunner = NULL;
106 BScreen screen;
107 screen.SetDPMS(B_DPMS_ON);
111 void
112 ScreenBlanker::_SetDPMSMode(uint32 mode)
114 BScreen screen;
115 screen.SetDPMS(mode);
117 if (fWindow->Lock()) {
118 fSaverRunner->Suspend();
119 fWindow->Unlock();
124 void
125 ScreenBlanker::_ShowPasswordWindow()
127 _TurnOnScreen();
129 if (fWindow->Lock()) {
130 fSaverRunner->Suspend();
132 fWindow->Sync();
133 // TODO: is that needed?
134 ShowCursor();
135 if (fPasswordWindow->IsHidden())
136 fPasswordWindow->Show();
138 fWindow->Unlock();
141 _QueueResumeScreenSaver();
145 void
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");
158 void
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();
172 BScreen screen;
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)
182 return;
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");
221 void
222 ScreenBlanker::MessageReceived(BMessage* message)
224 switch (message->what) {
225 case kMsgUnlock:
227 if (strcmp(fSettings.Password(), crypt(fPasswordWindow->Password(),
228 fSettings.Password())) != 0) {
229 beep();
230 fPasswordWindow->SetPassword("");
231 _QueueResumeScreenSaver();
232 } else {
233 PRINT(("Quitting!\n"));
234 _Shutdown();
235 Quit();
237 break;
240 case kMsgResumeSaver:
242 if (fWindow->Lock()) {
243 HideCursor();
244 fPasswordWindow->SetPassword("");
245 fPasswordWindow->Hide();
247 fSaverRunner->Resume();
248 fWindow->Unlock();
251 // Turn on the message filter again
252 BMessage enable(kMsgEnableFilter);
253 fWindow->PostMessage(&enable);
255 _QueueTurnOffScreen();
256 break;
259 case kMsgTurnOffScreen:
260 _SetDPMSMode(B_DPMS_OFF);
261 break;
263 case kMsgSuspendScreen:
264 _SetDPMSMode(B_DPMS_SUSPEND);
265 break;
267 case kMsgStandByScreen:
268 _SetDPMSMode(B_DPMS_STAND_BY);
269 break;
271 case kMsgTestSaver:
272 fTestSaver = true;
273 break;
275 default:
276 BApplication::MessageReceived(message);
281 bool
282 ScreenBlanker::QuitRequested()
284 if (fSettings.LockEnable()) {
285 bigtime_t minTime = fSettings.PasswordTime() - fSettings.BlankTime();
286 if (minTime == 0)
287 minTime = 5000000;
288 if (system_time() - fBlankTime > minTime) {
289 _ShowPasswordWindow();
290 return false;
294 _Shutdown();
295 return true;
299 bool
300 ScreenBlanker::IsPasswordWindowShown() const
302 return fPasswordWindow != NULL && !fPasswordWindow->IsHidden();
306 void
307 ScreenBlanker::_Shutdown()
309 if (fWindow != NULL) {
310 fWindow->Hide();
312 if (fWindow->Lock())
313 fWindow->Quit();
316 delete fSaverRunner;
317 fSaverRunner = NULL;
321 // #pragma mark - main
325 main(int argc, char** argv)
327 ScreenBlanker app;
328 app.Run();
330 return 0;