V6 SCR: Merge code for Mac and Windows screensavers, adding full functionality for Windows SS.

svn path=/trunk/boinc/; revision=13781
This commit is contained in:
Charlie Fenton 2007-10-05 13:28:11 +00:00
parent 932aec2158
commit 0a19f38786
7 changed files with 968 additions and 762 deletions

225
clientscr/Mac_Saver_Module.h Executable file
View File

@ -0,0 +1,225 @@
// Berkeley Open Infrastructure for Network Computing
// http://boinc.berkeley.edu
// Copyright (C) 2005 University of California
//
// This is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation;
// either version 2.1 of the License, or (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// To view the GNU Lesser General Public License visit
// http://www.gnu.org/copyleft/lesser.html
// or write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// Mac_Saver_Module.h
// BOINC_Saver_Module
//
#ifndef _SCREENSAVER_MAC_H
#define _SCREENSAVER_MAC_H
#include <Carbon/Carbon.h>
#ifdef __cplusplus
extern "C" {
#endif
int initBOINCSaver(Boolean ispreview);
int drawGraphics(GrafPtr aPort);
void drawPreview(GrafPtr aPort);
void closeBOINCSaver(void);
#ifdef __cplusplus
} // extern "C"
#endif
//-----------------------------------------------------------------------------
// Name: class CScreensaver
// Desc: Screensaver class
//-----------------------------------------------------------------------------
class CScreensaver
{
public:
CScreensaver();
#ifdef _WIN32
virtual HRESULT Create( HINSTANCE hInstance );
HRESULT DisplayErrorMsg( HRESULT hr );
#elif defined(__APPLE__)
int Create();
#endif
int Run();
//
// Infrastructure layer
//
protected:
#ifdef _WIN32
SaverMode ParseCommandLine( TCHAR* pstrCommandLine );
VOID EnumMonitors( VOID );
int UtilGetRegKey(LPCTSTR name, DWORD &keyval);
int UtilSetRegKey(LPCTSTR name, DWORD value);
int UtilGetRegStartupStr(LPCTSTR name, LPTSTR str);
BOOL IsConfigStartupBOINC();
BOOL CreateInfrastructureMutexes();
BOOL GetError( BOOL& bErrorMode, HRESULT& hrError, TCHAR* pszError, size_t iErrorSize );
BOOL SetError( BOOL bErrorMode, HRESULT hrError );
VOID UpdateErrorBoxText();
virtual BOOL GetTextForError( HRESULT hr, TCHAR* pszError, DWORD dwNumChars );
// Variables for non-fatal error management
HANDLE m_hErrorManagementMutex;
BOOL m_bErrorMode; // Whether to display an error
HRESULT m_hrError; // Error code to display
TCHAR m_szError[400]; // Error message text
BOOL m_bBOINCConfigChecked;
BOOL m_bBOINCStartupConfigured;
DWORD m_dwBlankScreen;
DWORD m_dwBlankTime;
#elif defined(__APPLE__)
OSStatus initBOINCApp(void);
int GetBrandID(void);
char* PersistentFGets(char *buf, size_t buflen, FILE *f);
pid_t FindProcessPID(char* name, pid_t thePID);
OSErr GetpathToBOINCManagerApp(char* path, int maxLen);
bool SetError( bool bErrorMode, int hrError );
void setBannerText(const char *msg, GrafPtr aPort);
void updateBannerText(char *msg, GrafPtr aPort);
void drawBanner(GrafPtr aPort);
void print_to_log_file(const char *format, ...);
void strip_cr(char *buf);
char m_gfx_Switcher_Path[MAXPATHLEN];
bool m_bErrorMode; // Whether to display an error
unsigned int m_hrError; // Error code to display
bool m_wasAlreadyRunning;
pid_t m_CoreClientPID;
int m_dwBlankScreen;
time_t m_dwBlankTime;
#endif
//
// Data management layer
//
protected:
bool CreateDataManagementThread();
bool DestoryDataManagementThread();
#ifdef _WIN32
DWORD WINAPI DataManagementProc();
static DWORD WINAPI DataManagementProcStub( LPVOID lpParam );
int launch_screensaver(RESULT* rp, HANDLE& graphics_application);
int terminate_screensaver(HANDLE& graphics_application, RESULT *worker_app);
HANDLE m_hDataManagementThread;
HANDLE m_hGraphicsApplication;
#elif defined(__APPLE__)
void* DataManagementProc();
static void* DataManagementProcStub( void* param );
int terminate_screensaver(int& graphics_application, RESULT *worker_app);
int launch_screensaver(RESULT* rp, int& graphics_application);
void HandleRPCError(void);
OSErr KillScreenSaver(void);
pthread_t m_hDataManagementThread;
pid_t m_hGraphicsApplication;
#endif
// Determine if two RESULT pointers refer to the same task
bool is_same_task(RESULT* taska, RESULT* taskb);
// Count the number of active graphics-capable apps
int count_active_graphic_apps(RESULTS& results, RESULT* exclude = NULL);
// Choose a ramdom graphics application from the vector that
// was passed in.
RESULT* get_random_graphics_app(RESULTS& results, RESULT* exclude = NULL);
RPC_CLIENT *rpc;
CC_STATE state;
RESULTS results;
RESULT m_running_result;
bool m_updating_results;
int m_iLastResultShown;
time_t m_tLastResultChangeTime;
bool m_bResetCoreState;
bool m_QuitDataManagementProc;
//
// Presentation layer
//
#ifdef _WIN32
protected:
HRESULT CreateSaverWindow();
VOID UpdateErrorBox();
VOID InterruptSaver();
VOID ChangePassword();
VOID DoConfig();
HRESULT DoSaver();
VOID DoPaint( HWND hwnd, HDC hdc, LPPAINTSTRUCT lpps );
void DrawTransparentBitmap(HDC hdc, HBITMAP hBitmap, LONG xStart, LONG yStart, COLORREF cTransparentColor);
LRESULT SaverProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
INT_PTR ConfigureDialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam );
static LRESULT CALLBACK SaverProcStub( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
static INT_PTR CALLBACK ConfigureDialogProcStub( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
void ShutdownSaver();
#elif defined(__APPLE__)
char m_MsgBuf[2048];
char m_BannerText[2048];
int m_BannerWidth;
StringPtr m_CurrentBannerMessage;
char* m_BrandText;
public:
int drawGraphics(GrafPtr aPort);
void drawPreview(GrafPtr aPort);
void ShutdownSaver();
#endif
protected:
#ifdef _WIN32
SaverMode m_SaverMode; // sm_config, sm_full, sm_preview, etc.
BOOL m_bAllScreensSame; // If TRUE, show same image on all screens
HWND m_hWnd; // Focus window and device window on primary
HWND m_hWndParent;
HINSTANCE m_hInstance;
BOOL m_bWaitForInputIdle; // Used to pause when preview starts
DWORD m_dwSaverMouseMoveCount;
BOOL m_bIs9x;
BOOL m_bCheckingSaverPassword;
BOOL m_bWindowed;
INTERNALMONITORINFO m_Monitors[MAX_DISPLAYS];
DWORD m_dwNumMonitors;
RECT m_rcRenderTotal; // Rect of entire area to be rendered
RECT m_rcRenderCurDevice; // Rect of render area of current device
BOOL m_bPaintingInitialized;
TCHAR m_strWindowTitle[200]; // Title for the app's window
DWORD m_dwLastInputTimeAtStartup;
#endif
};
#endif

File diff suppressed because it is too large Load Diff

View File

@ -20,22 +20,44 @@
#ifdef _WIN32
#include "boinc_win.h"
#include <windowsx.h>
#include <mmsystem.h>
#include <regstr.h>
#include <strsafe.h>
#include "boinc_ss.h"
#endif
#include "diagnostics.h"
#include "common_defs.h"
#include "util.h"
#ifdef _WIN32
#include "gui_rpc_client.h"
#include "screensaver_win.h"
#endif
#ifdef __APPLE__
#include <Carbon/Carbon.h>
#include "gui_rpc_client.h"
#include "Mac_Saver_Module.h"
#endif
#include "screensaver.h"
bool is_same_task(RESULT* taska, RESULT* taskb) {
// Flags for testing & debugging
#define SIMULATE_NO_GRAPHICS 0
bool CScreensaver::is_same_task(RESULT* taska, RESULT* taskb) {
if ((taska == NULL) || (taskb == NULL)) return false;
if (taska->name != taskb->name) return false;
if (taska->project_url != taskb->project_url) return false;
return true;
}
int count_active_graphic_apps(RESULTS& results, RESULT* exclude) {
int CScreensaver::count_active_graphic_apps(RESULTS& results, RESULT* exclude) {
unsigned int i = 0;
unsigned int graphics_app_count = 0;
@ -62,7 +84,7 @@ int count_active_graphic_apps(RESULTS& results, RESULT* exclude) {
// Exclude the specified result unless it is the only candidate.
// If exclude is NULL or an empty string, don't exclude any results.
//
RESULT* get_random_graphics_app(RESULTS& results, RESULT* exclude) {
RESULT* CScreensaver::get_random_graphics_app(RESULTS& results, RESULT* exclude) {
RESULT* rp = NULL;
unsigned int i = 0;
unsigned int graphics_app_count = 0;
@ -113,14 +135,39 @@ CLEANUP:
// Launch the graphics application
//
#ifdef _WIN32
int launch_screensaver(RESULT* rp, HANDLE& graphics_application, RPC_CLIENT* rpc)
int CScreensaver::launch_screensaver(RESULT* rp, HANDLE& graphics_application)
#else
int launch_screensaver(RESULT* rp, int& graphics_application, RPC_CLIENT* rpc)
int CScreensaver::launch_screensaver(RESULT* rp, int& graphics_application)
#endif
{
int retval = 0;
if (!rp->graphics_exec_path.empty()) {
// V6 Graphics
#ifdef __APPLE__
// For sandbox security, use gfx_switcher to launch gfx app
// as user boinc_project and group boinc_project.
//
// For unknown reasons, the graphics application exits with
// "RegisterProcess failed (error = -50)" unless we pass its
// full path twice in the argument list to execv.
char* argv[5];
argv[0] = "gfx_Switcher";
argv[1] = "-launch_gfx";
argv[2] = strrchr(rp->slot_path.c_str(), '/');
if (*argv[2]) argv[2]++; // Point to the slot number in ascii
argv[3] = "--fullscreen";
argv[4] = 0;
retval = run_program(
rp->slot_path.c_str(),
m_gfx_Switcher_Path,
4,
argv,
0,
graphics_application
);
#else
char* argv[3];
argv[0] = "app_graphics"; // not used
argv[1] = "--fullscreen";
@ -133,18 +180,19 @@ int launch_screensaver(RESULT* rp, int& graphics_application, RPC_CLIENT* rpc)
0,
graphics_application
);
#endif
} else {
// V5 and Older
DISPLAY_INFO di;
#ifdef __WXMSW__
#ifdef _WIN32
graphics_application = NULL;
memset(di.window_station, 0, sizeof(window_station));
memset(di.window_station, 0, sizeof(di.window_station));
memset(di.desktop, 0, sizeof(di.desktop));
memset(di.display, 0, sizeof(di.display));
if (wxWIN95 != wxGetOsVersion(NULL, NULL)) {
if (!m_bIs9x) {
// Retrieve the current window station and desktop names
GetUserObjectInformation(
GetProcessWindowStation(),
@ -181,15 +229,43 @@ int launch_screensaver(RESULT* rp, int& graphics_application, RPC_CLIENT* rpc)
// Terminate the graphics application
//
#ifdef _WIN32
int terminate_screensaver(HANDLE& graphics_application, RESULT *worker_app, RPC_CLIENT* rpc)
int CScreensaver::terminate_screensaver(HANDLE& graphics_application, RESULT *worker_app)
#else
int terminate_screensaver(int& graphics_application, RESULT *worker_app, RPC_CLIENT* rpc)
int CScreensaver::terminate_screensaver(int& graphics_application, RESULT *worker_app)
#endif
{
if (graphics_application) {
// V6 Graphics
kill_program(graphics_application);
#ifdef __APPLE
// For sandbox security, use gfx_switcher to launch gfx app
// as user boinc_project and group boinc_project.
int retval = 0;
char current_dir[MAXPATHLEN];
char gfx_pid[16];
pid_t dont_care;
sprintf(gfx_pid, "%d", graphics_application);
getcwd( current_dir, sizeof(current_dir));
char* argv[4];
argv[0] = "gfx_switcher";
argv[1] = "-kill_gfx";
argv[2] = gfx_pid;
argv[3] = 0;
retval = run_program(
current_dir,
m_gfx_Switcher_Path,
3,
argv,
0,
dont_care
);
return retval;
#endif
graphics_application = 0;
} else {
// V5 and Older
DISPLAY_INFO di;
@ -210,3 +286,243 @@ int terminate_screensaver(int& graphics_application, RESULT *worker_app, RPC_CLI
return 0;
}
#ifdef _WIN32
DWORD WINAPI CScreensaver::DataManagementProc() {
#else
void *CScreensaver::DataManagementProc() {
#endif
int retval = 0;
int suspend_reason = 0;
RESULT* theResult = NULL;
RESULT* graphics_app_result_ptr = NULL;
RESULT previous_result;
RESULT* previous_result_ptr = NULL;
int iResultCount = 0;
int iIndex = 0;
double launch_time = 0.0;
double last_change_time = 0.0;
double last_run_check_time = 0.0;
#ifdef _WIN32
time_t m_tThreadCreateTime = 0;
BOINCTRACE(_T("CScreensaver::DataManagementProc - Display screen saver loading message\n"));
SetError(TRUE, SCRAPPERR_BOINCSCREENSAVERLOADING);
m_tThreadCreateTime = time(0);
// Set the starting point for iterating through the results
m_iLastResultShown = 0;
m_tLastResultChangeTime = 0;
#endif
while (true) {
if ((m_dwBlankScreen) && (time(0) > m_dwBlankTime)) {
BOINCTRACE(_T("CScreensaver::DataManagementProc - Time to blank\n"));
SetError(FALSE, SCRAPPERR_SCREENSAVERBLANKED);
m_QuitDataManagementProc = true;
}
if (m_QuitDataManagementProc) { // If main thread has requested we exit
if (m_hGraphicsApplication || graphics_app_result_ptr) {
terminate_screensaver(m_hGraphicsApplication, graphics_app_result_ptr);
graphics_app_result_ptr = NULL;
m_hGraphicsApplication = 0;
graphics_app_result_ptr = NULL;
previous_result_ptr = NULL;
}
#ifdef _WIN32
TerminateThread(m_hDataManagementThread, 0);
#else
pthread_exit(0); // Exit the thread
#endif
}
boinc_sleep(0.5);
if (m_bResetCoreState) {
// Try and get the current state of the CC
retval = rpc->get_state(state);
if (retval) {
// CC may not yet be running
HandleRPCError();
continue;
}
m_bResetCoreState = false;
}
BOINCTRACE(_T("CScreensaver::DataManagementProc - ErrorMode = '%d', ErrorCode = '%x'\n"), m_bErrorMode, m_hrError);
if ((m_dwBlankScreen) && (time(0) > m_dwBlankTime)) {
SetError(FALSE, SCRAPPERR_SCREENSAVERBLANKED);
m_QuitDataManagementProc = true;
}
if (m_QuitDataManagementProc) { // If main thread has requested we exit
if (m_hGraphicsApplication || graphics_app_result_ptr) {
terminate_screensaver(m_hGraphicsApplication, graphics_app_result_ptr);
graphics_app_result_ptr = NULL;
m_hGraphicsApplication = 0;
graphics_app_result_ptr = NULL;
previous_result_ptr = NULL;
}
#ifdef _WIN32
TerminateThread(m_hDataManagementThread, 0);
#else
pthread_exit(0); // Exit the thread
#endif
}
boinc_sleep(0.25);
m_updating_results = true;
retval = rpc->get_screensaver_tasks(suspend_reason, results);
m_updating_results = false;
boinc_sleep(0.25);
if (retval) {
// rpc call returned error
HandleRPCError();
m_bResetCoreState = true;
continue;
}
if (suspend_reason != 0) {
SetError(TRUE, SCRAPPERR_BOINCSUSPENDED);
if (m_hGraphicsApplication || graphics_app_result_ptr) {
terminate_screensaver(m_hGraphicsApplication, graphics_app_result_ptr);
if (m_hGraphicsApplication == 0) {
graphics_app_result_ptr = NULL;
} else {
// waitpid test will clear m_hGraphicsApplication and graphics_app_result_ptr
}
previous_result_ptr = NULL;
}
continue;
}
#if SIMULATE_NO_GRAPHICS /* FOR TESTING */
SetError(TRUE, SCRAPPERR_BOINCNOGRAPHICSAPPSEXECUTING);
#else /* NORMAL OPERATION */
// Is the current graphics app's associated task still running?
if ((m_hGraphicsApplication) || (graphics_app_result_ptr)) {
iResultCount = results.results.size();
graphics_app_result_ptr = NULL;
// Find the current task in the new results vector (if it still exists)
for (iIndex = 0; iIndex < iResultCount; iIndex++) {
theResult = results.results.at(iIndex);
if (is_same_task(theResult, previous_result_ptr)) {
graphics_app_result_ptr = theResult;
previous_result = *theResult;
previous_result_ptr = &previous_result;
break;
}
}
// V6 graphics only: if worker application has stopped running, terminate_screensaver
if ((graphics_app_result_ptr == NULL) && (m_hGraphicsApplication != 0)) {
// if (previous_result_ptr) print_to_log_file("%s finished", previous_result.name.c_str());
terminate_screensaver(m_hGraphicsApplication, previous_result_ptr);
previous_result_ptr = NULL;
// waitpid test will clear m_hGraphicsApplication
}
#if 0
// Safety check that task is actually running
if (last_run_check_time && ((dtime() - last_run_check_time) > TASK_RUN_CHECK_PERIOD)) {
if (FindProcessPID(NULL, ? ? ?) == 0) {
terminate_screensaver(m_hGraphicsApplication, graphics_app_result_ptr);
if (m_hGraphicsApplication == 0) {
graphics_app_result_ptr = NULL;
// Save previous_result and previous_result_ptr for get_random_graphics_app() call
} else {
// waitpid test will clear m_hGraphicsApplication and graphics_app_result_ptr
}
previous_result_ptr = NULL;
}
}
#endif
if (last_change_time && ((dtime() - last_change_time) > GFX_CHANGE_PERIOD)) {
if (count_active_graphic_apps(results, previous_result_ptr) > 0) {
// if (previous_result_ptr) print_to_log_file("time to change: %s", previous_result.name.c_str());
terminate_screensaver(m_hGraphicsApplication, graphics_app_result_ptr);
if (m_hGraphicsApplication == 0) {
graphics_app_result_ptr = NULL;
// Save previous_result and previous_result_ptr for get_random_graphics_app() call
} else {
// waitpid test will clear m_hGraphicsApplication and graphics_app_result_ptr
}
}
last_change_time = dtime();
}
}
// If no current graphics app, pick an active task at random and launch its graphics app
if ((m_hGraphicsApplication == 0) && (graphics_app_result_ptr == NULL)) {
graphics_app_result_ptr = get_random_graphics_app(results, previous_result_ptr);
previous_result_ptr = NULL;
if (graphics_app_result_ptr) {
retval = launch_screensaver(graphics_app_result_ptr, m_hGraphicsApplication);
if (retval) {
m_hGraphicsApplication = 0;
previous_result_ptr = NULL;
graphics_app_result_ptr = NULL;
} else {
SetError(FALSE, SCRAPPERR_SCREENSAVERRUNNING);
launch_time = dtime();
last_change_time = launch_time;
last_run_check_time = launch_time;
// Make a local copy of current result, since original pointer
// may have been freed by the time we perform later tests
previous_result = *graphics_app_result_ptr;
previous_result_ptr = &previous_result;
// if (previous_result_ptr) print_to_log_file("launching %s", previous_result.name.c_str());
}
} else {
if (state.projects.size() == 0) {
// We are not attached to any projects
SetError(TRUE, SCRAPPERR_BOINCNOPROJECTSDETECTED);
} else if (results.results.size() == 0) {
// We currently do not have any applications to run
SetError(TRUE, SCRAPPERR_BOINCNOAPPSEXECUTING);
} else {
// We currently do not have any graphics capable application
SetError(TRUE, SCRAPPERR_BOINCNOGRAPHICSAPPSEXECUTING);
}
}
} else { // End if ((m_hGraphicsApplication == 0) && (graphics_app_result_ptr == NULL))
// Is the graphics app still running?
if (m_hGraphicsApplication) {
#ifdef _WIN32
DWORD dwStatus = STILL_ACTIVE;
BOOL bRetVal = FALSE;
bRetVal = GetExitCodeProcess(m_hGraphicsApplication, &dwStatus);
BOINCTRACE(_T("CScreensaver::DataManagementProc - GetExitCodeProcess RetVal = '%d', Status = '%d'\n"), bRetVal, dwStatus);
if (bRetVal && (dwStatus != STILL_ACTIVE)) {
// Something has happened to the previously selected screensaver
// application. Start a different one.
m_hGraphicsApplication = 0;
graphics_app_result_ptr = NULL;
continue;
} else {
CheckForegroundWindow();
}
#else
if (waitpid(m_hGraphicsApplication, 0, WNOHANG) == m_hGraphicsApplication) {
m_hGraphicsApplication = 0;
graphics_app_result_ptr = NULL;
continue;
}
#endif
}
}
#endif // ! SIMULATE_NO_GRAPHICS
} // end while(true)
// return noErr; // should never get here; it fixes compiler warning
}

View File

@ -21,28 +21,28 @@
#ifndef __SCREENSAVER_H__
#define __SCREENSAVER_H__
// Determine if two RESULT pointers refer to the same task
extern bool is_same_task(RESULT* taska, RESULT* taskb);
// Count the number of active graphics-capable apps
extern int count_active_graphic_apps(RESULTS& results, RESULT* exclude = NULL);
#define GFX_CHANGE_PERIOD 600 /* if > 1 CPUs, change screensaver every 600 secs */
// Choose a ramdom graphics application from the vector that
// was passed in.
extern RESULT* get_random_graphics_app(RESULTS& results, RESULT* exclude = NULL);
// Launch the screensaver
#ifdef _WIN32
extern int launch_screensaver(RESULT* rp, HANDLE& graphics_application, RPC_CLIENT* rpc);
#else
extern int launch_screensaver(RESULT* rp, int& graphics_application, RPC_CLIENT* rpc);
#endif
//-----------------------------------------------------------------------------
// Error / status codes
//-----------------------------------------------------------------------------
// Terminate the screensaver
#ifdef _WIN32
extern int terminate_screensaver(HANDLE& graphics_application, RESULT *worker_app, RPC_CLIENT* rpc);
#else
extern int terminate_screensaver(int& graphics_application, RESULT *worker_app, RPC_CLIENT* rpc);
#endif
#define SCRAPPERR_BOINCNOTDETECTED 0x82000001
#define SCRAPPERR_BOINCNOTDETECTEDSTARTUP 0x82000002
#define SCRAPPERR_BOINCSUSPENDED 0x82000003
#define SCRAPPERR_BOINCNOTGRAPHICSCAPABLE 0x82000004
#define SCRAPPERR_BOINCNOAPPSEXECUTING 0x82000005
#define SCRAPPERR_BOINCNOPROJECTSDETECTED 0x82000006
#define SCRAPPERR_BOINCNOGRAPHICSAPPSEXECUTING 0x82000007
#define SCRAPPERR_BOINCSCREENSAVERLOADING 0x82000008
#define SCRAPPERR_BOINCAPPFOUNDGRAPHICSLOADING 0x82000009
#define SCRAPPERR_BOINCSHUTDOWNEVENT 0x8200000a
#define SCRAPPERR_NOPREVIEW 0x8200000f
#define SCRAPPERR_DAEMONALLOWSNOGRAPHICS 0x82000010
#define SCRAPPERR_SCREENSAVERRUNNING 0x82000011
#define SCRAPPERR_SCREENSAVERBLANKED 0x82000012
#define SCRAPPERR_QUITSCREENSAVERREQUESTED 0x82000013
#endif

View File

@ -27,9 +27,9 @@
#include <windowsx.h>
#include <mmsystem.h>
#include <regstr.h>
#define COMPILE_MULTIMON_STUBS
#include <multimon.h>
#include <strsafe.h>
#include <mmsystem.h>
#define COMPILE_MULTIMON_STUBS
#include "boinc_ss.h"
#include "diagnostics.h"
@ -185,10 +185,11 @@ CScreensaver::CScreensaver() {
m_dwBlankScreen = 0;
m_dwBlankTime = 0;
rpc = 0;
m_hDataManagementThread = NULL;
m_hGraphicsApplication = NULL;
m_bScreensaverStarted = FALSE;
m_bResetCoreState = TRUE;
m_QuitDataManagementProc = FALSE;
m_bBOINCConfigChecked = FALSE;
m_bBOINCStartupConfigured = FALSE;
memset(&m_running_result, 0, sizeof(m_running_result));
@ -197,6 +198,7 @@ CScreensaver::CScreensaver() {
m_dwNumMonitors = 0;
m_dwLastInputTimeAtStartup = 0;
m_tThreadCreateTime = 0;
}
@ -261,7 +263,9 @@ HRESULT CScreensaver::Create(HINSTANCE hInstance) {
return E_FAIL;
}
// Create the screen saver window(s)
if (rpc == NULL) rpc = new RPC_CLIENT;
// Create the screen saver window(s)
if (m_SaverMode == sm_preview ||
m_SaverMode == sm_full
) {
@ -862,9 +866,7 @@ BOOL CScreensaver::SetError(BOOL bErrorMode, HRESULT hrError) {
// Update the error message
//
VOID CScreensaver::UpdateErrorBoxText() {
RESULTS results;
PROJECT* pProject;
TCHAR szBuffer[256];
bool bIsActive = false;
bool bIsExecuting = false;
bool bIsDownloaded = false;
@ -872,9 +874,9 @@ VOID CScreensaver::UpdateErrorBoxText() {
size_t iIndex = 0;
// Load error string
GetTextForError(m_hrError, m_szError, sizeof(m_szError) / sizeof(TCHAR));
if (SCRAPPERR_BOINCNOGRAPHICSAPPSEXECUTING == m_hrError) {
if (m_updating_results) return; // results vector is currently being updated by rpc
iResultCount = results.results.size();
int iModIndex;
for (iIndex = 0; iIndex < iResultCount; iIndex++) {
@ -911,6 +913,9 @@ VOID CScreensaver::UpdateErrorBoxText() {
}
}
m_szError[ sizeof(m_szError) -1 ] = '\0';
} else {
// Load error string
GetTextForError(m_hrError, m_szError, sizeof(m_szError) / sizeof(TCHAR));
}
BOINCTRACE(_T("CScreensaver::UpdateErrorBoxText - Updated Text '%s'\n"), m_szError);
}
@ -974,6 +979,8 @@ BOOL CScreensaver::GetTextForError(
//
BOOL CScreensaver::CreateDataManagementThread() {
DWORD dwThreadID = 0;
BOINCTRACE(_T("CScreensaver::CreateDataManagementThread Start\n"));
m_QuitDataManagementProc = FALSE;
m_hDataManagementThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
@ -995,9 +1002,18 @@ BOOL CScreensaver::CreateDataManagementThread() {
// Terminate the thread that is used to talk to the daemon.
//
BOOL CScreensaver::DestoryDataManagementThread() {
if (!TerminateThread(m_hDataManagementThread, 0)) {
BOINCTRACE(_T("CScreensaver::DestoryDataManagementThread: Failed to terminate data management thread '%d'\n"), GetLastError());
return FALSE;
m_QuitDataManagementProc = TRUE; // Tell RPC Thread to exit
// Wait up to 5 seconds for DataManagementThread to exit
for (int i=0; i< 20; i++) {
DWORD dwStatus = STILL_ACTIVE;
BOOL bRetVal = FALSE;
bRetVal = GetExitCodeThread(m_hDataManagementThread, &dwStatus);
BOINCTRACE(_T("CScreensaver::DestoryDataManagementThread - GetExitCodeThread RetVal = '%d', Status = '%d'\n"), bRetVal, dwStatus);
if (bRetVal && (dwStatus != STILL_ACTIVE)) {
break;
}
}
return TRUE;
}
@ -1005,203 +1021,6 @@ BOOL CScreensaver::DestoryDataManagementThread() {
// Do what needs to be done to update the text that is displayed
// to the user
//
DWORD WINAPI CScreensaver::DataManagementProc() {
BOOL bForegroundWindowIsScreensaver;
HWND hwndBOINCGraphicsWindow = NULL;
HWND hwndForeWindow = NULL;
HWND hwndForeParent = NULL;
DWORD iMonitor = 0;
int iReturnValue = 0;
int iSuspendReason = 0;
time_t tThreadCreateTime = 0;
bool bScreenSaverStarting = false;
INTERNALMONITORINFO* pMonitorInfo = NULL;
BOINCTRACE(_T("CScreensaver::DataManagementProc - Display screen saver loading message\n"));
SetError(TRUE, SCRAPPERR_BOINCSCREENSAVERLOADING);
tThreadCreateTime = time(0);
// Set the starting point for iterating through the results
m_iLastResultShown = 0;
m_tLastResultChangeTime = 0;
while(1) {
bScreenSaverStarting = (3 >= (time(0) - tThreadCreateTime));
BOINCTRACE(_T("CScreensaver::DataManagementProc - ErrorMode = '%d', ErrorCode = '%x'\n"), m_bErrorMode, m_hrError);
// Lets try and get the current state of the CC
if (m_bResetCoreState) {
iReturnValue = rpc.get_state(state);
if (0 == iReturnValue) {
m_bResetCoreState = FALSE;
}
BOINCTRACE(_T("CScreensaver::DataManagementProc - get_state iReturnValue = '%d'\n"), iReturnValue);
}
iReturnValue = rpc.get_screensaver_tasks(iSuspendReason, results);
BOINCTRACE(_T("CScreensaver::DataManagementProc - get_screensaver_tasks iReturnValue = '%d'\n"), iReturnValue);
if (0 != iReturnValue) {
// Attempt to reinitialize the RPC client and state
rpc.close();
rpc.init(NULL);
m_bResetCoreState = TRUE;
if (!m_bBOINCConfigChecked) {
m_bBOINCConfigChecked = TRUE;
m_bBOINCStartupConfigured = IsConfigStartupBOINC();
}
if (!bScreenSaverStarting) {
if (m_bBOINCStartupConfigured) {
SetError(TRUE, SCRAPPERR_BOINCNOTDETECTED);
} else {
SetError(TRUE, SCRAPPERR_BOINCNOTDETECTEDSTARTUP);
}
}
} else {
// Reset the error state.
SetError(FALSE, 0);
// Start the screensaver if it hasn't been started already.
if (!m_bScreensaverStarted) {
// Choose a random graphics application to start.
RESULT* rp = get_random_graphics_app(results);
if (rp) {
int retval = launch_screensaver(rp, m_hGraphicsApplication, &rpc);
BOINCTRACE(_T("CScreensaver::DataManagementProc - launch_screensaver RetVal = '%d', m_hGraphicsApplication = '%d'\n"), retval, m_hGraphicsApplication);
if (!retval) {
m_bScreensaverStarted = TRUE;
m_running_result = *rp;
}
} else {
if (state.projects.size() == 0) {
// We are not attached to any projects
SetError(TRUE, SCRAPPERR_BOINCNOPROJECTSDETECTED);
} else if (results.results.size() == 0) {
// We currently do not have any applications to run
SetError(TRUE, SCRAPPERR_BOINCNOAPPSEXECUTING);
} else {
// We currently do not have any graphics capable application
SetError(TRUE, SCRAPPERR_BOINCNOGRAPHICSAPPSEXECUTING);
}
}
} else {
// Is the graphics app still running?
DWORD dwStatus = STILL_ACTIVE;
BOOL bRetVal = FALSE;
bRetVal = GetExitCodeProcess(m_hGraphicsApplication, &dwStatus);
BOINCTRACE(_T("CScreensaver::DataManagementProc - GetExitCodeProcess RetVal = '%d', Status = '%d'\n"), bRetVal, dwStatus);
if (bRetVal && (dwStatus != STILL_ACTIVE)) {
// Something has happened to the previously selected screensaver
// application. Start a different one.
m_bScreensaverStarted = FALSE;
} else {
// When running in screensaver mode the only two valid conditions for z-order
// is that either the screensaver or graphics application is the foreground
// application. If this is not true, then blow out of the screensaver.
hwndBOINCGraphicsWindow = FindWindow(BOINC_WINDOW_CLASS_NAME, NULL);
if (hwndBOINCGraphicsWindow) {
// Graphics Application.
hwndForeWindow = GetForegroundWindow();
// If the graphics application is not the top most window try and force it
// to the top.
if (hwndForeWindow != hwndBOINCGraphicsWindow) {
BOINCTRACE(_T("CScreensaver::DataManagementProc - Graphics Window Detected but NOT the foreground window, bringing window to foreground.\n"));
SetForegroundWindow(hwndBOINCGraphicsWindow);
hwndForeWindow = GetForegroundWindow();
if (hwndForeWindow != hwndBOINCGraphicsWindow) {
BOINCTRACE(_T("CScreensaver::DataManagementProc - Graphics Window Detected but NOT the foreground window, bringing window to foreground. (Final Try)\n"));
// This may be needed on Windows 2000 or better machines
if (gspfnMyBroadcastSystemMessage) {
DWORD dwComponents = BSM_APPLICATIONS;
gspfnMyBroadcastSystemMessage(
BSF_ALLOWSFW,
&dwComponents,
WM_BOINCSFW,
NULL,
NULL
);
}
}
} else {
// Science application has focus, and is visible.
//
// Some science application take a really long time to display something on their
// window, during this time the window will appear to eat keyboard and mouse event
// messages and not respond to other system events. These windows are considered
// ghost windows, normally they have an outline and can be moved around and resized.
// In the science application case where the borders are hidden from view, the
// window just takes on the background of the previous window which happens to be
// the black screensaver window owned by this process.
//
// Verify that their hasn't been any keyboard or mouse activity. If there has
// we should hide the window from this process and exit out of the screensaver to
// return control back to the user as quickly as possible.
BOINCTRACE(_T("CScreensaver::DataManagementProc - Graphics Window Detected and is the foreground window.\n"));
if (gspfnMyGetLastInputInfo) {
BOINCTRACE(_T("CScreensaver::DataManagementProc - Checking idle actvity.\n"));
LASTINPUTINFO lii;
lii.cbSize = sizeof(LASTINPUTINFO);
gspfnMyGetLastInputInfo(&lii);
if (m_dwLastInputTimeAtStartup != lii.dwTime) {
BOINCTRACE(_T("CScreensaver::DataManagementProc - Activity Detected.\n"));
ShowWindow(hwndBOINCGraphicsWindow, SW_MINIMIZE);
ShowWindow(hwndBOINCGraphicsWindow, SW_FORCEMINIMIZE);
SetError(TRUE, SCRAPPERR_BOINCSHUTDOWNEVENT);
SendMessage(m_Monitors[iMonitor].hWnd, WM_INTERRUPTSAVER, NULL, NULL);
}
}
}
} else {
// Graphics application does not exist. So check that one of the windows
// assigned to each monitor is the foreground window.
bForegroundWindowIsScreensaver = FALSE;
hwndForeWindow = GetForegroundWindow();
hwndForeParent = GetParent(hwndForeWindow);
for(iMonitor = 0; iMonitor < m_dwNumMonitors; iMonitor++) {
pMonitorInfo = &m_Monitors[iMonitor];
if ((pMonitorInfo->hWnd == hwndForeWindow) ||
(pMonitorInfo->hWnd == hwndForeParent))
{
bForegroundWindowIsScreensaver = TRUE;
}
}
if (!bForegroundWindowIsScreensaver) {
// This can happen because of a personal firewall notifications or some
// funky IM client that thinks it has to notify the user even when in
// screensaver mode.
BOINCTRACE(_T("CScreensaver::DataManagementProc - Unknown foreground window detected, shutdown the screensaver.\n"));
SetError(TRUE, SCRAPPERR_BOINCSHUTDOWNEVENT);
SendMessage(m_Monitors[0].hWnd, WM_INTERRUPTSAVER, NULL, NULL);
}
}
}
}
}
BOINCTRACE(_T("CScreensaver::SaverProc - ErrorMode = '%d', ErrorCode = '%x'\n"), m_bErrorMode, m_hrError);
Sleep(1000);
}
}
// This function forwards to DataManagementProc, which has access to the
// "this" pointer.
//
@ -1212,6 +1031,126 @@ DWORD WINAPI CScreensaver::DataManagementProcStub(LPVOID UNUSED(lpParam)) {
void CScreensaver::HandleRPCError()
{
// Attempt to reinitialize the RPC client and state
rpc->close();
rpc->init(NULL);
m_bResetCoreState = TRUE;
if (!m_bBOINCConfigChecked) {
m_bBOINCConfigChecked = TRUE;
m_bBOINCStartupConfigured = IsConfigStartupBOINC();
}
if ((time(0) - m_tThreadCreateTime) > 3) {
if (m_bBOINCStartupConfigured) {
SetError(TRUE, SCRAPPERR_BOINCNOTDETECTED);
} else {
SetError(TRUE, SCRAPPERR_BOINCNOTDETECTEDSTARTUP);
}
}
}
void CScreensaver::CheckForegroundWindow()
{
BOOL bForegroundWindowIsScreensaver;
HWND hwndBOINCGraphicsWindow = NULL;
HWND hwndForeWindow = NULL;
HWND hwndForeParent = NULL;
DWORD iMonitor = 0;
INTERNALMONITORINFO* pMonitorInfo = NULL;
// When running in screensaver mode the only two valid conditions for z-order
// is that either the screensaver or graphics application is the foreground
// application. If this is not true, then blow out of the screensaver.
hwndBOINCGraphicsWindow = FindWindow(BOINC_WINDOW_CLASS_NAME, NULL);
if (hwndBOINCGraphicsWindow) {
// Graphics Application.
hwndForeWindow = GetForegroundWindow();
// If the graphics application is not the top most window try and force it
// to the top.
if (hwndForeWindow != hwndBOINCGraphicsWindow) {
BOINCTRACE(_T("CScreensaver::CheckForegroundWindow - Graphics Window Detected but NOT the foreground window, bringing window to foreground.\n"));
SetForegroundWindow(hwndBOINCGraphicsWindow);
hwndForeWindow = GetForegroundWindow();
if (hwndForeWindow != hwndBOINCGraphicsWindow) {
BOINCTRACE(_T("CScreensaver::CheckForegroundWindow - Graphics Window Detected but NOT the foreground window, bringing window to foreground. (Final Try)\n"));
// This may be needed on Windows 2000 or better machines
if (gspfnMyBroadcastSystemMessage) {
DWORD dwComponents = BSM_APPLICATIONS;
gspfnMyBroadcastSystemMessage(
BSF_ALLOWSFW,
&dwComponents,
WM_BOINCSFW,
NULL,
NULL
);
}
}
} else {
// Science application has focus, and is visible.
//
// Some science application take a really long time to display something on their
// window, during this time the window will appear to eat keyboard and mouse event
// messages and not respond to other system events. These windows are considered
// ghost windows, normally they have an outline and can be moved around and resized.
// In the science application case where the borders are hidden from view, the
// window just takes on the background of the previous window which happens to be
// the black screensaver window owned by this process.
//
// Verify that their hasn't been any keyboard or mouse activity. If there has
// we should hide the window from this process and exit out of the screensaver to
// return control back to the user as quickly as possible.
BOINCTRACE(_T("CScreensaver::CheckForegroundWindow - Graphics Window Detected and is the foreground window.\n"));
if (gspfnMyGetLastInputInfo) {
BOINCTRACE(_T("CScreensaver::CheckForegroundWindow - Checking idle actvity.\n"));
LASTINPUTINFO lii;
lii.cbSize = sizeof(LASTINPUTINFO);
gspfnMyGetLastInputInfo(&lii);
if (m_dwLastInputTimeAtStartup != lii.dwTime) {
BOINCTRACE(_T("CScreensaver::CheckForegroundWindow - Activity Detected.\n"));
ShowWindow(hwndBOINCGraphicsWindow, SW_MINIMIZE);
ShowWindow(hwndBOINCGraphicsWindow, SW_FORCEMINIMIZE);
SetError(TRUE, SCRAPPERR_BOINCSHUTDOWNEVENT);
SendMessage(m_Monitors[iMonitor].hWnd, WM_INTERRUPTSAVER, NULL, NULL);
}
}
}
} else {
// Graphics application does not exist. So check that one of the windows
// assigned to each monitor is the foreground window.
bForegroundWindowIsScreensaver = FALSE;
hwndForeWindow = GetForegroundWindow();
hwndForeParent = GetParent(hwndForeWindow);
for(iMonitor = 0; iMonitor < m_dwNumMonitors; iMonitor++) {
pMonitorInfo = &m_Monitors[iMonitor];
if ((pMonitorInfo->hWnd == hwndForeWindow) ||
(pMonitorInfo->hWnd == hwndForeParent))
{
bForegroundWindowIsScreensaver = TRUE;
}
}
if (!bForegroundWindowIsScreensaver) {
// This can happen because of a personal firewall notifications or some
// funky IM client that thinks it has to notify the user even when in
// screensaver mode.
BOINCTRACE(_T("CScreensaver::CheckForegroundWindow - Unknown foreground window detected, shutdown the screensaver.\n"));
SetError(TRUE, SCRAPPERR_BOINCSHUTDOWNEVENT);
SendMessage(m_Monitors[0].hWnd, WM_INTERRUPTSAVER, NULL, NULL);
}
}
}
// Register and create the appropriate window(s)
//
HRESULT CScreensaver::CreateSaverWindow() {
@ -1582,7 +1521,7 @@ VOID CScreensaver::ShutdownSaver() {
}
// Kill the currently executing graphics application
terminate_screensaver(m_hGraphicsApplication, &m_running_result, &rpc);
terminate_screensaver(m_hGraphicsApplication, &m_running_result);
// Post message to drop out of message loop
// This can be called from the data management thread, so specifically

View File

@ -7,23 +7,8 @@
#ifndef _SCREENSAVER_WIN_H
#define _SCREENSAVER_WIN_H
//-----------------------------------------------------------------------------
// Error codes
//-----------------------------------------------------------------------------
#define SCRAPPERR_BOINCNOTDETECTED 0x82000001
#define SCRAPPERR_BOINCNOTDETECTEDSTARTUP 0x82000002
#define SCRAPPERR_BOINCSUSPENDED 0x82000003
#define SCRAPPERR_BOINCNOTGRAPHICSCAPABLE 0x82000004
#define SCRAPPERR_BOINCNOAPPSEXECUTING 0x82000005
#define SCRAPPERR_BOINCNOPROJECTSDETECTED 0x82000006
#define SCRAPPERR_BOINCNOGRAPHICSAPPSEXECUTING 0x82000007
#define SCRAPPERR_BOINCSCREENSAVERLOADING 0x82000008
#define SCRAPPERR_BOINCAPPFOUNDGRAPHICSLOADING 0x82000009
#define SCRAPPERR_BOINCSHUTDOWNEVENT 0x8200000a
#define SCRAPPERR_NOPREVIEW 0x8200000f
#define SCRAPPERR_DAEMONALLOWSNOGRAPHICS 0x82000010
#include <multimon.h>
//-----------------------------------------------------------------------------
// Constants
@ -173,19 +158,35 @@ protected:
DWORD WINAPI DataManagementProc();
static DWORD WINAPI DataManagementProcStub( LPVOID lpParam );
void CheckForegroundWindow();
int terminate_screensaver(HANDLE& graphics_application, RESULT *worker_app);
int launch_screensaver(RESULT* rp, HANDLE& graphics_application);
void HandleRPCError(void);
RPC_CLIENT rpc;
// Determine if two RESULT pointers refer to the same task
bool is_same_task(RESULT* taska, RESULT* taskb);
// Count the number of active graphics-capable apps
int count_active_graphic_apps(RESULTS& results, RESULT* exclude = NULL);
// Choose a ramdom graphics application from the vector that
// was passed in.
RESULT* get_random_graphics_app(RESULTS& results, RESULT* exclude = NULL);
RPC_CLIENT* rpc;
CC_STATE state;
RESULTS results;
RESULT m_running_result;
bool m_updating_results;
HANDLE m_hDataManagementThread;
HANDLE m_hGraphicsApplication;
BOOL m_bScreensaverStarted;
BOOL m_bResetCoreState;
bool m_QuitDataManagementProc;
int m_iLastResultShown;
time_t m_tLastResultChangeTime;
time_t m_tThreadCreateTime;
//
// Presentation layer

View File

@ -1143,6 +1143,7 @@
DD9C94A30A5A3A4100AB0D10 /* BOINCBaseFrame.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = BOINCBaseFrame.h; path = ../clientgui/BOINCBaseFrame.h; sourceTree = SOURCE_ROOT; };
DDA12A910A369AB500FBDD12 /* SetupSecurity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SetupSecurity.h; path = ../clientgui/mac/SetupSecurity.h; sourceTree = SOURCE_ROOT; };
DDA12AAD0A369C5800FBDD12 /* SecurityUtility.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SecurityUtility.cpp; path = ../clientgui/mac/SecurityUtility.cpp; sourceTree = SOURCE_ROOT; };
DDA290360CB5D80E00512BD8 /* Mac_Saver_Module.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = Mac_Saver_Module.h; path = ../clientscr/Mac_Saver_Module.h; sourceTree = SOURCE_ROOT; };
DDA6BD030BD4551F008F7921 /* dyld_gdb.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = dyld_gdb.h; path = ../lib/mac/dyld_gdb.h; sourceTree = SOURCE_ROOT; };
DDA6BD040BD4551F008F7921 /* mac_backtrace.C */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = mac_backtrace.C; path = ../lib/mac/mac_backtrace.C; sourceTree = SOURCE_ROOT; };
DDA6BD050BD4551F008F7921 /* mac_backtrace.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = mac_backtrace.h; path = ../lib/mac/mac_backtrace.h; sourceTree = SOURCE_ROOT; };
@ -1704,8 +1705,9 @@
isa = PBXGroup;
children = (
DDB873960C85072500E0DE1F /* mac_saver_module.cpp */,
DDB873970C85072500E0DE1F /* Mac_Saver_ModuleView.h */,
DDA290360CB5D80E00512BD8 /* Mac_Saver_Module.h */,
DDB873980C85072500E0DE1F /* Mac_Saver_ModuleView.m */,
DDB873970C85072500E0DE1F /* Mac_Saver_ModuleView.h */,
DD7AEB490C87CE1300AC3B5C /* screensaver.cpp */,
DD7AEB680C87CE6500AC3B5C /* screensaver.h */,
DDFA60E30CB3396C0037B88C /* gfx_switcher.C */,