xref: /haiku/src/bin/screen_blanker/ScreenSaverWindow.cpp (revision c302a243e15e640fae0f689e32cdf0c18749afee)
1cbb7d503SAxel Dörfler /*
2*e4e2ce4cSPuck Meerburg  * Copyright 2003-2014, Haiku.
3cbb7d503SAxel Dörfler  * Distributed under the terms of the MIT License.
4cbb7d503SAxel Dörfler  *
5cbb7d503SAxel Dörfler  * Authors:
6cbb7d503SAxel Dörfler  *		Michael Phipps
7cbb7d503SAxel Dörfler  *		Jérôme Duval, jerome.duval@free.fr
8cb6a084fSRyan Leavengood  *		Ryan Leavengood, leavengood@gmail.com
9*e4e2ce4cSPuck Meerburg  *		Puck Meerburg, puck@puckipedia.nl
10cbb7d503SAxel Dörfler  */
11cbb7d503SAxel Dörfler 
12cbb7d503SAxel Dörfler 
130834f79cSRyan Leavengood #include "ScreenBlanker.h"
14cbb7d503SAxel Dörfler #include "ScreenSaverWindow.h"
15cbb7d503SAxel Dörfler 
16d0e9fa68SAxel Dörfler #include <Application.h>
17d0e9fa68SAxel Dörfler #include <View.h>
18d0e9fa68SAxel Dörfler 
194a1b9897SAxel Dörfler #include <WindowPrivate.h>
204a1b9897SAxel Dörfler 
21cb6a084fSRyan Leavengood #include <syslog.h>
22cb6a084fSRyan Leavengood 
23cb6a084fSRyan Leavengood 
24e22c01afSJohn Scipione //	#pragma mark - ScreenSaverFilter
25e22c01afSJohn Scipione 
26e22c01afSJohn Scipione 
27cb6a084fSRyan Leavengood /* This message filter is what will close the screensaver upon user activity. */
28cb6a084fSRyan Leavengood filter_result
Filter(BMessage * message,BHandler ** target)29cb6a084fSRyan Leavengood ScreenSaverFilter::Filter(BMessage* message, BHandler** target)
30cb6a084fSRyan Leavengood {
31cb6a084fSRyan Leavengood 	// This guard is used to avoid sending multiple B_QUIT_REQUESTED messages
32cb6a084fSRyan Leavengood 	if (fEnabled) {
33cb6a084fSRyan Leavengood 		switch (message->what) {
34*e4e2ce4cSPuck Meerburg 			case B_MOUSE_MOVED:
35*e4e2ce4cSPuck Meerburg 			{
36*e4e2ce4cSPuck Meerburg 				// ignore the initial B_MOUSE_MOVED sent by the app_server
37*e4e2ce4cSPuck Meerburg 				// in test mode, all mouse move events are ignored
38*e4e2ce4cSPuck Meerburg 				bool transitOnly = false;
39*e4e2ce4cSPuck Meerburg 				if (fTestMode
4069a973e2SJérôme Duval 					|| (message->FindBool("be:transit_only", &transitOnly)
4169a973e2SJérôme Duval 						== B_OK && transitOnly))
42*e4e2ce4cSPuck Meerburg 					return B_DISPATCH_MESSAGE;
43*e4e2ce4cSPuck Meerburg 
44*e4e2ce4cSPuck Meerburg 				// Fall through
45*e4e2ce4cSPuck Meerburg 			}
46cb6a084fSRyan Leavengood 			case B_KEY_DOWN:
47cb6a084fSRyan Leavengood 			{
48cb6a084fSRyan Leavengood 				// we ignore the Print-Screen key to make screen shots of
49cb6a084fSRyan Leavengood 				// screen savers possible
50cb6a084fSRyan Leavengood 				int32 key;
51cb6a084fSRyan Leavengood 				if (message->FindInt32("key", &key) == B_OK && key == 0xe)
52cb6a084fSRyan Leavengood 					return B_DISPATCH_MESSAGE;
53cb6a084fSRyan Leavengood 
54cb6a084fSRyan Leavengood 				// Fall through
55cb6a084fSRyan Leavengood 			}
560a4f8d8cSAxel Dörfler 			case B_MODIFIERS_CHANGED:
570a4f8d8cSAxel Dörfler 			case B_UNMAPPED_KEY_DOWN:
58cb6a084fSRyan Leavengood 			case B_MOUSE_DOWN:
59cb6a084fSRyan Leavengood 				fEnabled = false;
60cb6a084fSRyan Leavengood 				be_app->PostMessage(B_QUIT_REQUESTED);
61cb6a084fSRyan Leavengood 				break;
62cb6a084fSRyan Leavengood 		}
6322d74352SJohn Scipione 	} else if (message->what == B_KEY_DOWN) {
6422d74352SJohn Scipione 		ScreenBlanker* app = dynamic_cast<ScreenBlanker*>(be_app);
6522d74352SJohn Scipione 		if (app != NULL && app->IsPasswordWindowShown()) {
660834f79cSRyan Leavengood 			// Handle the escape key when the password window is showing
670834f79cSRyan Leavengood 			const char* string = NULL;
680f1162d2SJohn Scipione 			if (message->FindString("bytes", &string) == B_OK
690f1162d2SJohn Scipione 					&& string[0] == B_ESCAPE) {
700834f79cSRyan Leavengood 				be_app->PostMessage(kMsgResumeSaver);
71cb6a084fSRyan Leavengood 			}
720f1162d2SJohn Scipione 		}
7322d74352SJohn Scipione 	}
74cb6a084fSRyan Leavengood 
75cb6a084fSRyan Leavengood 	return B_DISPATCH_MESSAGE;
76cb6a084fSRyan Leavengood }
77cb6a084fSRyan Leavengood 
78cb6a084fSRyan Leavengood 
79d34a680cSJohn Scipione //	#pragma mark - ScreenSaverWindow
80cb6a084fSRyan Leavengood 
81cbb7d503SAxel Dörfler 
82cbb7d503SAxel Dörfler /*!
83cbb7d503SAxel Dörfler 	This is the BDirectWindow subclass that rendering occurs in.
84cbb7d503SAxel Dörfler 	A view is added to it so that BView based screensavers will work.
85cbb7d503SAxel Dörfler */
ScreenSaverWindow(BRect frame,bool test)86*e4e2ce4cSPuck Meerburg ScreenSaverWindow::ScreenSaverWindow(BRect frame, bool test)
87e22c01afSJohn Scipione 	:
88e22c01afSJohn Scipione 	BDirectWindow(frame, "ScreenSaver Window",
894a1b9897SAxel Dörfler 		B_NO_BORDER_WINDOW_LOOK, kWindowScreenFeel,
906189b5a1SSergei Reznikov 		B_NOT_RESIZABLE | B_NOT_MOVABLE | B_NOT_MINIMIZABLE
916189b5a1SSergei Reznikov 		| B_NOT_ZOOMABLE | B_NOT_CLOSABLE, B_ALL_WORKSPACES),
92d34a680cSJohn Scipione 	fTopView(NULL),
93d34a680cSJohn Scipione 	fSaverRunner(NULL),
94d34a680cSJohn Scipione 	fFilter(NULL)
95cbb7d503SAxel Dörfler {
96cbb7d503SAxel Dörfler 	frame.OffsetTo(0, 0);
971c776bfeSJohn Scipione 	fTopView = new BView(frame, "ScreenSaver View", B_FOLLOW_ALL,
981c776bfeSJohn Scipione 		B_WILL_DRAW);
997f45fe0bSPhilippe Houdoin 	fTopView->SetViewColor(B_TRANSPARENT_COLOR);
100cb6a084fSRyan Leavengood 
101*e4e2ce4cSPuck Meerburg 	fFilter = new ScreenSaverFilter(test);
102cb6a084fSRyan Leavengood 	fTopView->AddFilter(fFilter);
103cb6a084fSRyan Leavengood 
104cbb7d503SAxel Dörfler 	AddChild(fTopView);
105cb6a084fSRyan Leavengood 
106d34a680cSJohn Scipione 	// Ensure that this view receives keyboard and mouse input
107cb6a084fSRyan Leavengood 	fTopView->MakeFocus(true);
108d34a680cSJohn Scipione 	fTopView->SetEventMask(B_KEYBOARD_EVENTS | B_POINTER_EVENTS,
109d34a680cSJohn Scipione 		B_NO_POINTER_HISTORY);
110cbb7d503SAxel Dörfler }
111cbb7d503SAxel Dörfler 
112cbb7d503SAxel Dörfler 
~ScreenSaverWindow()113cbb7d503SAxel Dörfler ScreenSaverWindow::~ScreenSaverWindow()
114cbb7d503SAxel Dörfler {
115cbb7d503SAxel Dörfler 	Hide();
116cbb7d503SAxel Dörfler }
117cbb7d503SAxel Dörfler 
118cbb7d503SAxel Dörfler 
119cbb7d503SAxel Dörfler void
MessageReceived(BMessage * message)120cb6a084fSRyan Leavengood ScreenSaverWindow::MessageReceived(BMessage* message)
121cb6a084fSRyan Leavengood {
122cb6a084fSRyan Leavengood 	switch (message->what) {
123cb6a084fSRyan Leavengood 		case kMsgEnableFilter:
124cb6a084fSRyan Leavengood 			fFilter->SetEnabled(true);
125cb6a084fSRyan Leavengood 			break;
126cb6a084fSRyan Leavengood 
127cb6a084fSRyan Leavengood 		default:
128cb6a084fSRyan Leavengood 			BWindow::MessageReceived(message);
129cb6a084fSRyan Leavengood 	}
130cb6a084fSRyan Leavengood }
131cb6a084fSRyan Leavengood 
132cb6a084fSRyan Leavengood 
133cbb7d503SAxel Dörfler bool
QuitRequested()134cbb7d503SAxel Dörfler ScreenSaverWindow::QuitRequested()
135cbb7d503SAxel Dörfler {
136d0e9fa68SAxel Dörfler 	be_app->PostMessage(B_QUIT_REQUESTED);
137cbb7d503SAxel Dörfler 	return true;
138cbb7d503SAxel Dörfler }
139cbb7d503SAxel Dörfler 
140cbb7d503SAxel Dörfler 
141cbb7d503SAxel Dörfler void
DirectConnected(direct_buffer_info * info)142cbb7d503SAxel Dörfler ScreenSaverWindow::DirectConnected(direct_buffer_info* info)
143cbb7d503SAxel Dörfler {
144e22c01afSJohn Scipione 	BScreenSaver* saver = _ScreenSaver();
145e22c01afSJohn Scipione 	if (saver != NULL)
146e22c01afSJohn Scipione 		saver->DirectConnected(info);
147cbb7d503SAxel Dörfler }
148cbb7d503SAxel Dörfler 
149d34a680cSJohn Scipione 
150d34a680cSJohn Scipione void
SetSaverRunner(ScreenSaverRunner * runner)151d34a680cSJohn Scipione ScreenSaverWindow::SetSaverRunner(ScreenSaverRunner* runner)
152d34a680cSJohn Scipione {
153d34a680cSJohn Scipione 	fSaverRunner = runner;
154d34a680cSJohn Scipione }
155d34a680cSJohn Scipione 
156d34a680cSJohn Scipione 
157d34a680cSJohn Scipione BScreenSaver*
_ScreenSaver()158d34a680cSJohn Scipione ScreenSaverWindow::_ScreenSaver()
159d34a680cSJohn Scipione {
160d34a680cSJohn Scipione 	if (fSaverRunner != NULL)
161d34a680cSJohn Scipione 		return fSaverRunner->ScreenSaver();
162d34a680cSJohn Scipione 
163d34a680cSJohn Scipione 	return NULL;
164d34a680cSJohn Scipione }
165