2009-02-21 02:42:50 +00:00
|
|
|
// This file is part of BOINC.
|
2005-04-23 15:14:52 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2009-02-21 02:42:50 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2005-04-23 15:14:52 +00:00
|
|
|
//
|
2009-02-21 02:42:50 +00:00
|
|
|
// BOINC 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 3 of the License, or (at your option) any later version.
|
2005-04-23 15:14:52 +00:00
|
|
|
//
|
2009-02-21 02:42:50 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-04-23 15:14:52 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2009-02-21 02:42:50 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2005-04-23 15:14:52 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Mac_Saver_ModuleView.m
|
|
|
|
// BOINC_Saver_Module
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "Mac_Saver_ModuleView.h"
|
2008-02-14 12:26:13 +00:00
|
|
|
#include <Carbon/Carbon.h>
|
2008-02-27 03:21:46 +00:00
|
|
|
#include <AppKit/AppKit.h>
|
2009-08-19 02:49:06 +00:00
|
|
|
#include <QTKit/QTKitDefines.h> // For NSInteger
|
2011-07-27 11:42:22 +00:00
|
|
|
#include <IOKit/hidsystem/IOHIDLib.h>
|
|
|
|
#include <IOKit/hidsystem/IOHIDParameter.h>
|
|
|
|
#include <IOKit/hidsystem/event_status_driver.h>
|
2009-08-19 02:49:06 +00:00
|
|
|
|
|
|
|
#ifndef NSInteger
|
|
|
|
#if __LP64__ || NS_BUILD_32_LIKE_64
|
|
|
|
typedef long NSInteger;
|
|
|
|
#else
|
|
|
|
typedef int NSInteger;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CGFLOAT_DEFINED
|
|
|
|
typedef float CGFloat;
|
|
|
|
#endif
|
2008-02-14 12:26:13 +00:00
|
|
|
|
2012-07-10 09:28:17 +00:00
|
|
|
#ifndef GrafPtr
|
|
|
|
// OS 10.7 SDK and later do not have QuickDraw headers
|
|
|
|
// Although the run-time libraries are still available,
|
|
|
|
// we use weak linking in case they are removed in
|
|
|
|
// future versions of OS X.
|
|
|
|
typedef struct OpaqueGrafPtr* GrafPtr;
|
|
|
|
void GetPort(GrafPtr * port) __attribute__((weak_import));
|
|
|
|
void SetPortTextFont(GrafPtr port,short txFont) __attribute__((weak_import));
|
|
|
|
void SetPortTextSize(CGrafPtr port, short txSize) __attribute__((weak_import));
|
|
|
|
#endif
|
|
|
|
#define kHelveticaFontID 21
|
|
|
|
|
2008-02-14 12:26:13 +00:00
|
|
|
void print_to_log_file(const char *format, ...);
|
|
|
|
void strip_cr(char *buf);
|
2005-04-23 15:14:52 +00:00
|
|
|
|
2011-07-27 11:42:22 +00:00
|
|
|
static SInt32 gSystemVersion = 0;
|
|
|
|
static double gSS_StartTime = 0.0;
|
|
|
|
mach_port_t gEventHandle = 0;
|
|
|
|
|
2005-04-23 15:14:52 +00:00
|
|
|
int gGoToBlank; // True if we are to blank the screen
|
|
|
|
int gBlankingTime; // Delay in minutes before blanking the screen
|
2008-10-31 13:44:52 +00:00
|
|
|
NSString *gPathToBundleResources = NULL;
|
|
|
|
NSString *mBundleID = NULL; // our bundle ID
|
|
|
|
NSImage *gBOINC_Logo = NULL;
|
2008-02-14 12:26:13 +00:00
|
|
|
|
2008-10-22 12:25:54 +00:00
|
|
|
int gTopWindowListIndex = -1;
|
|
|
|
|
2008-02-14 12:26:13 +00:00
|
|
|
NSRect gMovingRect;
|
|
|
|
float gImageXIndent;
|
|
|
|
float gTextBoxHeight;
|
2011-07-28 12:40:11 +00:00
|
|
|
CGFloat gActualTextBoxHeight;
|
2008-02-14 12:26:13 +00:00
|
|
|
NSPoint gCurrentPosition;
|
|
|
|
NSPoint gCurrentDelta;
|
|
|
|
|
|
|
|
CGContextRef myContext;
|
|
|
|
bool isErased;
|
|
|
|
|
|
|
|
#define TEXTBOXMINWIDTH 400.0
|
|
|
|
#define MINTEXTBOXHEIGHT 40.0
|
|
|
|
#define MAXTEXTBOXHEIGHT 300.0
|
2008-02-22 07:32:26 +00:00
|
|
|
#define TEXTBOXTOPBORDER 15
|
2008-02-14 12:26:13 +00:00
|
|
|
#define SAFETYBORDER 20.0
|
|
|
|
#define MINDELTA 8
|
2008-02-22 08:58:07 +00:00
|
|
|
#define MAXDELTA 16
|
2008-02-14 12:26:13 +00:00
|
|
|
|
|
|
|
int signof(float x) {
|
|
|
|
return (x > 0.0 ? 1 : -1);
|
|
|
|
}
|
2005-04-23 15:14:52 +00:00
|
|
|
|
|
|
|
@implementation BOINC_Saver_ModuleView
|
|
|
|
|
|
|
|
- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview {
|
2008-10-22 12:25:54 +00:00
|
|
|
self = [ super initWithFrame:frame isPreview:isPreview ];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2008-11-01 09:05:23 +00:00
|
|
|
// If there are multiple displays, this may get called
|
|
|
|
// multiple times (once for each display), so we need to guard
|
|
|
|
// against any problems that may cause.
|
2008-10-22 12:25:54 +00:00
|
|
|
- (void)startAnimation {
|
2007-09-19 09:41:23 +00:00
|
|
|
NSBundle * myBundle;
|
2008-10-22 12:25:54 +00:00
|
|
|
int newFrequency;
|
2009-12-14 13:10:49 +00:00
|
|
|
int period;
|
2011-07-27 11:42:22 +00:00
|
|
|
|
|
|
|
gEventHandle = NXOpenEventStatus();
|
|
|
|
|
|
|
|
OSStatus err = Gestalt(gestaltSystemVersion, &gSystemVersion);
|
|
|
|
if (err != noErr) {
|
|
|
|
gSystemVersion = 0;
|
|
|
|
}
|
2009-12-14 13:10:49 +00:00
|
|
|
|
|
|
|
initBOINCSaver();
|
2005-04-23 15:14:52 +00:00
|
|
|
|
2008-02-14 12:26:13 +00:00
|
|
|
if (gBOINC_Logo == NULL) {
|
|
|
|
if (self) {
|
|
|
|
myBundle = [ NSBundle bundleForClass:[self class]];
|
|
|
|
// grab the screensaver defaults
|
2008-10-31 13:44:52 +00:00
|
|
|
if (mBundleID == NULL) {
|
|
|
|
mBundleID = [ myBundle bundleIdentifier ];
|
|
|
|
}
|
2007-09-19 09:41:23 +00:00
|
|
|
|
2008-02-14 12:26:13 +00:00
|
|
|
// Path to our copy of switcher utility application in this screensaver bundle
|
2008-10-31 13:44:52 +00:00
|
|
|
if (gPathToBundleResources == NULL) {
|
|
|
|
gPathToBundleResources = [ myBundle resourcePath ];
|
|
|
|
}
|
2005-04-23 15:14:52 +00:00
|
|
|
|
2008-02-14 12:26:13 +00:00
|
|
|
ScreenSaverDefaults *defaults = [ ScreenSaverDefaults defaultsForModuleWithName:mBundleID ];
|
2005-04-23 15:14:52 +00:00
|
|
|
|
2008-02-14 12:26:13 +00:00
|
|
|
// try to load the version key, used to see if we have any saved settings
|
|
|
|
mVersion = [defaults floatForKey:@"version"];
|
|
|
|
if (!mVersion) {
|
|
|
|
// no previous settings so define our defaults
|
|
|
|
gGoToBlank = NO;
|
|
|
|
gBlankingTime = 1;
|
|
|
|
|
|
|
|
// write out the defaults
|
|
|
|
[ defaults setInteger:gGoToBlank forKey:@"GoToBlank" ];
|
|
|
|
[ defaults setInteger:gBlankingTime forKey:@"BlankingTime" ];
|
2009-12-14 13:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mVersion < 2) {
|
|
|
|
mVersion = 2;
|
|
|
|
|
|
|
|
[ defaults setInteger:mVersion forKey:@"version" ];
|
|
|
|
period = getGFXDefaultPeriod() / 60;
|
|
|
|
[ defaults setInteger:period forKey:@"DefaultPeriod" ];
|
|
|
|
period = getGFXSciencePeriod() / 60;
|
|
|
|
[ defaults setInteger:period forKey:@"SciencePeriod" ];
|
|
|
|
period = getGGFXChangePeriod() / 60;
|
|
|
|
[ defaults setInteger:period forKey:@"ChangePeriod" ];
|
2008-02-14 12:26:13 +00:00
|
|
|
|
|
|
|
// synchronize
|
|
|
|
[defaults synchronize];
|
|
|
|
}
|
2005-04-23 15:14:52 +00:00
|
|
|
|
2009-12-14 13:10:49 +00:00
|
|
|
// get defaults...
|
2008-02-14 12:26:13 +00:00
|
|
|
gGoToBlank = [ defaults integerForKey:@"GoToBlank" ];
|
|
|
|
gBlankingTime = [ defaults integerForKey:@"BlankingTime" ];
|
2009-12-14 13:10:49 +00:00
|
|
|
period = [ defaults integerForKey:@"DefaultPeriod" ];
|
|
|
|
setGFXDefaultPeriod((double)(period * 60));
|
|
|
|
period = [ defaults integerForKey:@"SciencePeriod" ];
|
|
|
|
setGFXSciencePeriod((double)(period * 60));
|
|
|
|
period = [ defaults integerForKey:@"ChangePeriod" ];
|
|
|
|
setGGFXChangePeriod((double)(period * 60));
|
2005-04-23 15:14:52 +00:00
|
|
|
|
2008-02-14 12:26:13 +00:00
|
|
|
[ self setAutoresizesSubviews:YES ]; // make sure the subview resizes.
|
|
|
|
|
|
|
|
NSString *fileName = [[ NSBundle bundleForClass:[ self class ]] pathForImageResource:@"boinc_ss_logo" ];
|
|
|
|
if (! fileName) {
|
|
|
|
// What should we do in this case?
|
2008-10-22 12:25:54 +00:00
|
|
|
return;
|
2008-02-14 12:26:13 +00:00
|
|
|
}
|
2005-04-23 15:14:52 +00:00
|
|
|
|
2008-02-14 12:26:13 +00:00
|
|
|
gBOINC_Logo = [[ NSImage alloc ] initWithContentsOfFile:fileName ];
|
|
|
|
gMovingRect.origin.x = 0.0;
|
|
|
|
gMovingRect.origin.y = 0.0;
|
|
|
|
gMovingRect.size = [gBOINC_Logo size];
|
|
|
|
|
|
|
|
if (gMovingRect.size.width < TEXTBOXMINWIDTH) {
|
|
|
|
gImageXIndent = (TEXTBOXMINWIDTH - gMovingRect.size.width) / 2;
|
|
|
|
gMovingRect.size.width = TEXTBOXMINWIDTH;
|
|
|
|
} else {
|
|
|
|
gImageXIndent = 0.0;
|
|
|
|
}
|
|
|
|
gTextBoxHeight = MINTEXTBOXHEIGHT;
|
|
|
|
gMovingRect.size.height += gTextBoxHeight;
|
|
|
|
gCurrentPosition.x = SAFETYBORDER + 1;
|
|
|
|
gCurrentPosition.y = SAFETYBORDER + 1 + gTextBoxHeight;
|
|
|
|
gCurrentDelta.x = 1.0;
|
|
|
|
gCurrentDelta.y = 1.0;
|
|
|
|
|
2011-07-28 12:40:11 +00:00
|
|
|
gActualTextBoxHeight = MINTEXTBOXHEIGHT;
|
|
|
|
|
2008-02-14 12:26:13 +00:00
|
|
|
[ self setAnimationTimeInterval:1/8.0 ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-23 15:14:52 +00:00
|
|
|
[ super startAnimation ];
|
|
|
|
|
2008-10-31 13:44:52 +00:00
|
|
|
if ( [ self isPreview ] ) {
|
|
|
|
[ self setAnimationTimeInterval:1.0/8.0 ];
|
2005-04-23 15:14:52 +00:00
|
|
|
return;
|
2008-10-31 13:44:52 +00:00
|
|
|
}
|
2008-10-22 12:25:54 +00:00
|
|
|
|
2009-12-14 13:10:49 +00:00
|
|
|
newFrequency = startBOINCSaver();
|
2005-04-23 15:14:52 +00:00
|
|
|
if (newFrequency)
|
|
|
|
[ self setAnimationTimeInterval:1.0/newFrequency ];
|
2011-07-27 11:42:22 +00:00
|
|
|
|
|
|
|
gSS_StartTime = getDTime();
|
2005-04-23 15:14:52 +00:00
|
|
|
}
|
|
|
|
|
2008-11-01 09:05:23 +00:00
|
|
|
// If there are multiple displays, this may get called
|
|
|
|
// multiple times (once for each display), so we need to guard
|
|
|
|
// against any problems that may cause.
|
2005-04-23 15:14:52 +00:00
|
|
|
- (void)stopAnimation {
|
|
|
|
[ super stopAnimation ];
|
|
|
|
|
2008-10-31 13:44:52 +00:00
|
|
|
if ( ! [ self isPreview ] ) {
|
|
|
|
closeBOINCSaver();
|
|
|
|
}
|
|
|
|
|
2008-10-22 12:25:54 +00:00
|
|
|
gTopWindowListIndex = -1;
|
|
|
|
|
2008-10-31 13:44:52 +00:00
|
|
|
if (gBOINC_Logo) {
|
|
|
|
[ gBOINC_Logo release ];
|
|
|
|
}
|
2008-10-22 12:25:54 +00:00
|
|
|
gBOINC_Logo = NULL;
|
|
|
|
|
2005-04-23 15:14:52 +00:00
|
|
|
}
|
|
|
|
|
2008-11-01 09:05:23 +00:00
|
|
|
// If there are multiple displays, this may get called
|
|
|
|
// multiple times (once for each display), so we need to guard
|
|
|
|
// against any problems that may cause.
|
2005-04-23 15:14:52 +00:00
|
|
|
- (void)drawRect:(NSRect)rect {
|
|
|
|
[ super drawRect:rect ];
|
|
|
|
|
|
|
|
// optionally draw here
|
|
|
|
}
|
|
|
|
|
2008-11-01 09:05:23 +00:00
|
|
|
// If there are multiple displays, this may get called
|
|
|
|
// multiple times (once for each display), so we need to guard
|
|
|
|
// against any problems that may cause.
|
2005-04-23 15:14:52 +00:00
|
|
|
- (void)animateOneFrame {
|
2008-02-14 12:26:13 +00:00
|
|
|
int newFrequency = 0;
|
2008-02-27 03:21:46 +00:00
|
|
|
int coveredFreq = 0;
|
2005-04-23 15:14:52 +00:00
|
|
|
NSRect theFrame = [ self frame ];
|
2009-08-19 02:49:06 +00:00
|
|
|
NSInteger myWindowNumber;
|
|
|
|
NSInteger windowList[20];
|
|
|
|
NSInteger i, n;
|
2008-02-14 12:26:13 +00:00
|
|
|
NSRect currentDrawingRect, eraseRect;
|
|
|
|
NSPoint imagePosition;
|
|
|
|
char *msg;
|
|
|
|
CFStringRef cf_msg;
|
2005-04-24 12:31:55 +00:00
|
|
|
AbsoluteTime timeToUnblock, frameStartTime = UpTime();
|
2011-07-27 11:42:22 +00:00
|
|
|
kern_return_t kernResult = kIOReturnError;
|
|
|
|
UInt64 params;
|
|
|
|
IOByteCount rcnt = sizeof(UInt64);
|
|
|
|
double idleTime = 0;
|
2012-07-10 09:28:17 +00:00
|
|
|
HIThemeTextInfo textInfo;
|
|
|
|
|
2008-02-14 12:26:13 +00:00
|
|
|
if ([ self isPreview ]) {
|
2005-04-23 15:14:52 +00:00
|
|
|
#if 1 // Currently drawRect just draws our logo in the preview window
|
|
|
|
NSString *fileName = [[ NSBundle bundleForClass:[ self class ]] pathForImageResource:@"boinc" ];
|
|
|
|
if (fileName) {
|
|
|
|
NSImage *myImage = [[ NSImage alloc ] initWithContentsOfFile:fileName ];
|
|
|
|
[ myImage setScalesWhenResized:YES ];
|
|
|
|
[ myImage setSize:theFrame.size ];
|
|
|
|
[ myImage compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver ];
|
|
|
|
[ myImage release ];
|
|
|
|
}
|
|
|
|
[ self setAnimationTimeInterval:1/1.0 ];
|
|
|
|
#else // Code for possible future use if we want to draw more in preview
|
2008-02-14 12:26:13 +00:00
|
|
|
myContext = [[NSGraphicsContext currentContext] graphicsPort];
|
|
|
|
drawPreview(myContext);
|
2005-04-23 15:14:52 +00:00
|
|
|
[ self setAnimationTimeInterval:1/30.0 ];
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2008-02-19 13:26:26 +00:00
|
|
|
|
2011-07-27 11:42:22 +00:00
|
|
|
// For unkown reasons, OS 10.7 Lion screensaver delays several seconds after
|
|
|
|
// user activity before calling stopAnimation, so we check user activity here
|
2012-07-10 12:18:21 +00:00
|
|
|
if ((gSystemVersion >= 0x1070) && ((getDTime() - gSS_StartTime) > 2.0)) {
|
2011-07-27 11:42:22 +00:00
|
|
|
kernResult = IOHIDGetParameter( gEventHandle, CFSTR(EVSIOIDLE), sizeof(UInt64), ¶ms, &rcnt );
|
|
|
|
if ( kernResult == kIOReturnSuccess ) {
|
|
|
|
idleTime = ((double)params) / 1000.0 / 1000.0 / 1000.0;
|
|
|
|
if (idleTime < 1.5) {
|
|
|
|
[ self stopAnimation ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-14 12:26:13 +00:00
|
|
|
myContext = [[NSGraphicsContext currentContext] graphicsPort];
|
|
|
|
// [myContext retain];
|
2009-08-19 02:49:06 +00:00
|
|
|
|
2008-02-14 12:26:13 +00:00
|
|
|
NSWindow *myWindow = [ self window ];
|
|
|
|
NSRect windowFrame = [ myWindow frame ];
|
|
|
|
if ( (windowFrame.origin.x != 0) || (windowFrame.origin.y != 0) ) {
|
2005-04-23 15:14:52 +00:00
|
|
|
// Hide window on second display to aid in debugging
|
2008-09-05 02:47:10 +00:00
|
|
|
#ifdef _DEBUG
|
2008-10-31 13:44:52 +00:00
|
|
|
[ myWindow setLevel:kCGMinimumWindowLevel ];
|
2010-04-07 11:37:56 +00:00
|
|
|
NSInteger alpha = 0;
|
|
|
|
[ myWindow setAlphaValue:alpha ]; // For OS 10.6
|
2008-09-05 02:47:10 +00:00
|
|
|
#endif
|
2005-04-23 15:14:52 +00:00
|
|
|
return; // We draw only to main screen
|
|
|
|
}
|
2008-02-14 12:26:13 +00:00
|
|
|
|
|
|
|
NSRect viewBounds = [self bounds];
|
|
|
|
|
2008-02-27 03:21:46 +00:00
|
|
|
newFrequency = getSSMessage(&msg, &coveredFreq);
|
|
|
|
|
2008-10-22 12:25:54 +00:00
|
|
|
// NOTE: My tests seem to confirm that the top window is always the first
|
|
|
|
// window returned by NSWindowList under OS 10.5 and the second window
|
|
|
|
// returned by NSWindowList under OS 10.3.9 and OS 10.4. However, Apple's
|
|
|
|
// documentation is unclear whether we can depend on this. So I have
|
|
|
|
// added some safety by doing two things:
|
2008-02-27 03:21:46 +00:00
|
|
|
// [1] Only use the NSWindowList test when we have started project graphics.
|
|
|
|
// [2] Assume that our window is covered 45 seconds after starting project
|
|
|
|
// graphics even if the NSWindowList test did not indicate that is so.
|
|
|
|
//
|
|
|
|
// getSSMessage() returns a non-zero value for coveredFreq only if we have started
|
|
|
|
// project graphics.
|
|
|
|
//
|
|
|
|
// If we should use a different frequency when our window is covered by another
|
|
|
|
// window, then check whether there is a window at a higher z-level than ours.
|
2008-10-22 12:25:54 +00:00
|
|
|
|
|
|
|
// Assuming our window(s) are initially the top window(s),
|
|
|
|
// determine our position in the window list when no graphics
|
|
|
|
// applications have covered us.
|
|
|
|
if (gTopWindowListIndex < 0) {
|
|
|
|
myWindowNumber = [ myWindow windowNumber ];
|
|
|
|
NSWindowList(20, windowList);
|
|
|
|
NSCountWindows(&n);
|
|
|
|
if (n > 20) n = 20;
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
if (windowList[i] == myWindowNumber) {
|
|
|
|
gTopWindowListIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-27 03:21:46 +00:00
|
|
|
if (coveredFreq) {
|
2009-02-27 11:20:08 +00:00
|
|
|
if ( (msg != NULL) && (msg[0] != '\0') ) {
|
|
|
|
myWindowNumber = [ myWindow windowNumber ];
|
|
|
|
|
|
|
|
windowList[0] = 0;
|
|
|
|
NSWindowList(20, windowList);
|
|
|
|
NSCountWindows(&n);
|
|
|
|
if (gTopWindowListIndex < n) {
|
|
|
|
if (windowList[gTopWindowListIndex] != myWindowNumber) {
|
|
|
|
// Project graphics application has a window open above ours
|
|
|
|
// Don't waste CPU cycles since our window is obscured by application graphics
|
|
|
|
newFrequency = coveredFreq;
|
|
|
|
msg = NULL;
|
|
|
|
windowIsCovered();
|
|
|
|
}
|
2008-10-22 12:25:54 +00:00
|
|
|
}
|
2009-02-27 11:20:08 +00:00
|
|
|
} else {
|
|
|
|
newFrequency = coveredFreq;
|
2008-02-27 03:21:46 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-14 12:26:13 +00:00
|
|
|
|
|
|
|
// Clear the previous drawing area
|
|
|
|
currentDrawingRect = gMovingRect;
|
|
|
|
currentDrawingRect.origin.x = (float) ((int)gCurrentPosition.x);
|
|
|
|
currentDrawingRect.origin.y += (float) ((int)gCurrentPosition.y - gTextBoxHeight);
|
|
|
|
|
|
|
|
if ( (msg != NULL) && (msg[0] != '\0') ) {
|
|
|
|
|
|
|
|
// Set direction of motion to "bounce" off edges of screen
|
|
|
|
if (currentDrawingRect.origin.x <= SAFETYBORDER) {
|
2008-02-22 08:58:07 +00:00
|
|
|
gCurrentDelta.x = (float)SSRandomIntBetween(MINDELTA, MAXDELTA) / 16.;
|
|
|
|
gCurrentDelta.y = (float)(SSRandomIntBetween(MINDELTA, MAXDELTA) * signof(gCurrentDelta.y)) / 16.;
|
2008-02-14 12:26:13 +00:00
|
|
|
}
|
|
|
|
if ( (currentDrawingRect.origin.x + currentDrawingRect.size.width) >=
|
|
|
|
(viewBounds.origin.x + viewBounds.size.width - SAFETYBORDER) ) {
|
2008-02-22 08:58:07 +00:00
|
|
|
gCurrentDelta.x = -(float)SSRandomIntBetween(MINDELTA, MAXDELTA) / 16.;
|
|
|
|
gCurrentDelta.y = (float)(SSRandomIntBetween(MINDELTA, MAXDELTA) * signof(gCurrentDelta.y)) / 16.;
|
2008-02-14 12:26:13 +00:00
|
|
|
}
|
2011-07-28 12:40:11 +00:00
|
|
|
if (currentDrawingRect.origin.y + gTextBoxHeight - gActualTextBoxHeight <= SAFETYBORDER) {
|
2008-02-22 08:58:07 +00:00
|
|
|
gCurrentDelta.y = (float)SSRandomIntBetween(MINDELTA, MAXDELTA) / 16.;
|
|
|
|
gCurrentDelta.x = (float)(SSRandomIntBetween(MINDELTA, MAXDELTA) * signof(gCurrentDelta.x)) / 16.;
|
2008-02-14 12:26:13 +00:00
|
|
|
}
|
|
|
|
if ( (currentDrawingRect.origin.y + currentDrawingRect.size.height) >=
|
|
|
|
(viewBounds.origin.y + viewBounds.size.height - SAFETYBORDER) ) {
|
2008-02-22 08:58:07 +00:00
|
|
|
gCurrentDelta.y = -(float)SSRandomIntBetween(MINDELTA, MAXDELTA) / 16.;
|
|
|
|
gCurrentDelta.x = (float)(SSRandomIntBetween(MINDELTA, MAXDELTA) * signof(gCurrentDelta.x)) / 16.;
|
2008-02-14 12:26:13 +00:00
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
// For testing
|
|
|
|
gCurrentDelta.x = 0;
|
|
|
|
gCurrentDelta.y = 0;
|
|
|
|
#endif
|
|
|
|
|
2009-08-19 02:49:06 +00:00
|
|
|
if (!isErased) {
|
|
|
|
[[NSColor blackColor] set];
|
|
|
|
|
|
|
|
// Erasing only 2 small rectangles reduces screensaver's CPU usage by about 25%
|
|
|
|
imagePosition.x = (float) ((int)gCurrentPosition.x + gImageXIndent);
|
|
|
|
imagePosition.y = (float) (int)gCurrentPosition.y;
|
2008-02-14 12:26:13 +00:00
|
|
|
eraseRect.origin.y = imagePosition.y;
|
2009-08-19 02:49:06 +00:00
|
|
|
eraseRect.size.height = currentDrawingRect.size.height - gTextBoxHeight;
|
|
|
|
|
|
|
|
if (gCurrentDelta.x > 0) {
|
|
|
|
eraseRect.origin.x = imagePosition.x - 1;
|
|
|
|
eraseRect.size.width = gCurrentDelta.x + 1;
|
|
|
|
} else {
|
|
|
|
eraseRect.origin.x = currentDrawingRect.origin.x + currentDrawingRect.size.width - gImageXIndent + gCurrentDelta.x - 1;
|
|
|
|
eraseRect.size.width = -gCurrentDelta.x + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
eraseRect = NSInsetRect(eraseRect, -1, -1);
|
|
|
|
NSRectFill(eraseRect);
|
|
|
|
|
|
|
|
eraseRect.origin.x = imagePosition.x;
|
|
|
|
eraseRect.size.width = currentDrawingRect.size.width - gImageXIndent - gImageXIndent;
|
2008-02-14 12:26:13 +00:00
|
|
|
|
2009-08-19 02:49:06 +00:00
|
|
|
if (gCurrentDelta.y > 0) {
|
|
|
|
eraseRect.origin.y = imagePosition.y;
|
|
|
|
eraseRect.size.height = gCurrentDelta.y + 1;
|
|
|
|
} else {
|
|
|
|
eraseRect.origin.y = imagePosition.y + currentDrawingRect.size.height - gTextBoxHeight - 1;
|
|
|
|
eraseRect.size.height = -gCurrentDelta.y + 1;
|
|
|
|
}
|
|
|
|
eraseRect = NSInsetRect(eraseRect, -1, -1);
|
|
|
|
NSRectFill(eraseRect);
|
|
|
|
|
|
|
|
eraseRect = currentDrawingRect;
|
|
|
|
eraseRect.size.height = gTextBoxHeight;
|
|
|
|
eraseRect = NSInsetRect(eraseRect, -1, -1);
|
|
|
|
NSRectFill(eraseRect);
|
|
|
|
|
|
|
|
isErased = true;
|
|
|
|
}
|
2008-02-14 12:26:13 +00:00
|
|
|
|
|
|
|
// Get the new drawing area
|
|
|
|
gCurrentPosition.x += gCurrentDelta.x;
|
|
|
|
gCurrentPosition.y += gCurrentDelta.y;
|
|
|
|
|
|
|
|
imagePosition.x = (float) ((int)gCurrentPosition.x + gImageXIndent);
|
|
|
|
imagePosition.y = (float) (int)gCurrentPosition.y;
|
|
|
|
|
2009-08-19 02:49:06 +00:00
|
|
|
[ gBOINC_Logo compositeToPoint:imagePosition operation:NSCompositeCopy ];
|
2008-02-14 12:26:13 +00:00
|
|
|
|
2009-08-19 02:49:06 +00:00
|
|
|
if ( (msg != NULL) && (msg[0] != '\0') ) {
|
|
|
|
cf_msg = CFStringCreateWithCString(NULL, msg, kCFStringEncodingMacRoman);
|
2008-02-14 12:26:13 +00:00
|
|
|
|
2009-08-19 02:49:06 +00:00
|
|
|
CGRect bounds = CGRectMake((float) ((int)gCurrentPosition.x),
|
|
|
|
viewBounds.size.height - imagePosition.y + TEXTBOXTOPBORDER,
|
2011-07-28 12:40:11 +00:00
|
|
|
gMovingRect.size.width,
|
|
|
|
MAXTEXTBOXHEIGHT
|
2009-08-19 02:49:06 +00:00
|
|
|
);
|
2008-02-14 12:26:13 +00:00
|
|
|
|
2009-08-19 02:49:06 +00:00
|
|
|
CGContextSaveGState (myContext);
|
|
|
|
CGContextTranslateCTM (myContext, 0, viewBounds.origin.y + viewBounds.size.height);
|
|
|
|
CGContextScaleCTM (myContext, 1.0f, -1.0f);
|
|
|
|
|
2012-07-10 12:18:21 +00:00
|
|
|
if (gSystemVersion >= 0x1050) {
|
2012-07-10 09:28:17 +00:00
|
|
|
CTFontRef myFont = CTFontCreateWithName(CFSTR("Helvetica"), 20, NULL);
|
2009-08-19 02:49:06 +00:00
|
|
|
|
2012-07-10 09:28:17 +00:00
|
|
|
HIThemeTextInfo theTextInfo = {kHIThemeTextInfoVersionOne, kThemeStateActive, kThemeSpecifiedFont,
|
|
|
|
kHIThemeTextHorizontalFlushLeft, kHIThemeTextVerticalFlushTop,
|
|
|
|
kHIThemeTextBoxOptionNone, kHIThemeTextTruncationNone, 0, false,
|
|
|
|
0, myFont
|
|
|
|
};
|
|
|
|
textInfo = theTextInfo;
|
|
|
|
} else {
|
|
|
|
#ifndef __x86_64__
|
|
|
|
GrafPtr port;
|
|
|
|
GetPort(&port);
|
|
|
|
SetPortTextFont(port, kHelveticaFontID);
|
|
|
|
SetPortTextSize(port, 20);
|
|
|
|
|
|
|
|
HIThemeTextInfo theTextInfo = {0, kThemeStateActive, kThemeCurrentPortFont, //kThemeMenuItemCmdKeyFont, //kThemePushButtonFont,
|
|
|
|
kHIThemeTextHorizontalFlushLeft, kHIThemeTextVerticalFlushTop,
|
|
|
|
kHIThemeTextBoxOptionNone, kHIThemeTextTruncationNone, 0, false
|
|
|
|
};
|
|
|
|
textInfo = theTextInfo;
|
2009-08-19 02:49:06 +00:00
|
|
|
#endif
|
2012-07-10 09:28:17 +00:00
|
|
|
}
|
2009-08-19 02:49:06 +00:00
|
|
|
|
2011-07-28 12:40:11 +00:00
|
|
|
HIThemeGetTextDimensions(cf_msg, (float)gMovingRect.size.width, &textInfo, NULL, &gActualTextBoxHeight, NULL);
|
|
|
|
gActualTextBoxHeight += TEXTBOXTOPBORDER;
|
|
|
|
|
2009-08-19 02:49:06 +00:00
|
|
|
// Use only APIs available in Mac OS 10.3.9
|
|
|
|
// HIThemeSetTextFill(kThemeTextColorWhite, NULL, myContext, kHIThemeOrientationNormal);
|
|
|
|
// SetThemeTextColor(kThemeTextColorWhite, 32, true);
|
|
|
|
|
|
|
|
CGFloat myWhiteComponents[] = {1.0, 1.0, 1.0, 1.0};
|
|
|
|
CGColorSpaceRef myColorSpace = CGColorSpaceCreateDeviceRGB ();
|
|
|
|
CGColorRef myTextColor = CGColorCreate(myColorSpace, myWhiteComponents);
|
|
|
|
|
|
|
|
CGContextSetFillColorWithColor(myContext, myTextColor);
|
|
|
|
|
|
|
|
HIThemeDrawTextBox(cf_msg, &bounds, &textInfo, myContext, kHIThemeOrientationNormal);
|
|
|
|
|
|
|
|
CGColorRelease(myTextColor);
|
|
|
|
CGColorSpaceRelease(myColorSpace);
|
|
|
|
CGContextRestoreGState (myContext);
|
|
|
|
CFRelease(cf_msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
gTextBoxHeight = MAXTEXTBOXHEIGHT + TEXTBOXTOPBORDER;
|
2008-02-14 12:26:13 +00:00
|
|
|
gMovingRect.size.height = [gBOINC_Logo size].height + gTextBoxHeight;
|
|
|
|
|
|
|
|
isErased = false;
|
|
|
|
|
|
|
|
} else { // Empty or NULL message
|
|
|
|
if (!isErased) {
|
|
|
|
eraseRect = NSInsetRect(currentDrawingRect, -1, -1);
|
|
|
|
[[NSColor blackColor] set];
|
|
|
|
isErased = true;
|
|
|
|
NSRectFill(eraseRect);
|
|
|
|
gTextBoxHeight = MAXTEXTBOXHEIGHT;
|
|
|
|
gMovingRect.size.height = [gBOINC_Logo size].height + gTextBoxHeight;
|
|
|
|
}
|
|
|
|
}
|
2005-04-23 15:14:52 +00:00
|
|
|
|
2011-07-28 12:40:11 +00:00
|
|
|
if (newFrequency) {
|
2005-04-23 15:14:52 +00:00
|
|
|
[ self setAnimationTimeInterval:(1.0/newFrequency) ];
|
2011-07-28 12:40:11 +00:00
|
|
|
// setAnimationTimeInterval does not seem to be working, so we
|
|
|
|
// throttle the screensaver directly here.
|
|
|
|
timeToUnblock = AddDurationToAbsolute(durationSecond/newFrequency, frameStartTime);
|
|
|
|
MPDelayUntil(&timeToUnblock);
|
|
|
|
}
|
2005-04-23 15:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)hasConfigureSheet {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display the configuration sheet for the user to choose their settings
|
|
|
|
- (NSWindow*)configureSheet
|
|
|
|
{
|
2009-12-14 13:10:49 +00:00
|
|
|
int period;
|
|
|
|
|
2005-04-23 15:14:52 +00:00
|
|
|
// if we haven't loaded our configure sheet, load the nib named MyScreenSaver.nib
|
|
|
|
if (!mConfigureSheet)
|
2009-12-14 13:10:49 +00:00
|
|
|
[ NSBundle loadNibNamed:@"BOINCSaver" owner:self ];
|
2005-04-23 15:14:52 +00:00
|
|
|
// set the UI state
|
|
|
|
[ mGoToBlankCheckbox setState:gGoToBlank ];
|
2009-12-14 13:10:49 +00:00
|
|
|
|
|
|
|
mBlankingTimeString = [[ NSString alloc ] initWithFormat:@"%d", gBlankingTime ];
|
2005-04-23 15:14:52 +00:00
|
|
|
[ mBlankingTimeTextField setStringValue:mBlankingTimeString ];
|
|
|
|
|
2009-12-14 13:10:49 +00:00
|
|
|
period = getGFXDefaultPeriod() / 60;
|
|
|
|
mDefaultPeriodString = [[ NSString alloc ] initWithFormat:@"%d", period ];
|
|
|
|
[ mDefaultPeriodTextField setStringValue:mDefaultPeriodString ];
|
|
|
|
|
|
|
|
period = getGFXSciencePeriod() / 60;
|
|
|
|
mSciencePeriodString = [[ NSString alloc ] initWithFormat:@"%d", period ];
|
|
|
|
[ mSciencePeriodTextField setStringValue:mSciencePeriodString ];
|
|
|
|
|
|
|
|
period = getGGFXChangePeriod() / 60;
|
|
|
|
mChangePeriodString = [[ NSString alloc ] initWithFormat:@"%d", period ];
|
|
|
|
[ mChangePeriodTextField setStringValue:mChangePeriodString ];
|
|
|
|
|
2005-04-23 15:14:52 +00:00
|
|
|
return mConfigureSheet;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the user clicked the SAVE button
|
|
|
|
- (IBAction) closeSheetSave:(id) sender
|
|
|
|
{
|
2009-12-14 13:10:49 +00:00
|
|
|
int period;
|
|
|
|
|
2005-04-23 15:14:52 +00:00
|
|
|
// get the defaults
|
|
|
|
ScreenSaverDefaults *defaults = [ ScreenSaverDefaults defaultsForModuleWithName:mBundleID ];
|
|
|
|
|
|
|
|
// save the UI state
|
|
|
|
gGoToBlank = [ mGoToBlankCheckbox state ];
|
|
|
|
mBlankingTimeString = [ mBlankingTimeTextField stringValue ];
|
2009-12-14 13:10:49 +00:00
|
|
|
gBlankingTime = [ mBlankingTimeString intValue ];
|
|
|
|
if ((gBlankingTime < 0) || (gBlankingTime > 999)) goto Bad;
|
|
|
|
|
|
|
|
mDefaultPeriodString = [ mDefaultPeriodTextField stringValue ];
|
|
|
|
period = [ mDefaultPeriodString intValue ];
|
|
|
|
if (!validateNumericString((CFStringRef)mDefaultPeriodString)) goto Bad;
|
|
|
|
if ((period < 0) || (period > 999)) goto Bad;
|
|
|
|
setGFXDefaultPeriod((double)(period * 60));
|
|
|
|
|
|
|
|
mSciencePeriodString = [ mSciencePeriodTextField stringValue ];
|
|
|
|
period = [ mSciencePeriodString intValue ];
|
|
|
|
if (!validateNumericString((CFStringRef)mSciencePeriodString)) goto Bad;
|
|
|
|
if ((period < 0) || (period > 999)) goto Bad;
|
|
|
|
setGFXSciencePeriod((double)(period * 60));
|
|
|
|
|
|
|
|
mChangePeriodString = [ mChangePeriodTextField stringValue ];
|
|
|
|
period = [ mChangePeriodString intValue ];
|
|
|
|
if (!validateNumericString((CFStringRef)mChangePeriodString)) goto Bad;
|
|
|
|
if ((period < 0) || (period > 999)) goto Bad;
|
|
|
|
setGGFXChangePeriod((double)(period * 60));
|
2005-04-23 15:14:52 +00:00
|
|
|
|
|
|
|
// write the defaults
|
|
|
|
[ defaults setInteger:gGoToBlank forKey:@"GoToBlank" ];
|
|
|
|
[ defaults setInteger:gBlankingTime forKey:@"BlankingTime" ];
|
2009-12-14 13:10:49 +00:00
|
|
|
period = getGFXDefaultPeriod() / 60;
|
|
|
|
[ defaults setInteger:period forKey:@"DefaultPeriod" ];
|
|
|
|
period = getGFXSciencePeriod() / 60;
|
|
|
|
[ defaults setInteger:period forKey:@"SciencePeriod" ];
|
|
|
|
period = getGGFXChangePeriod() / 60;
|
|
|
|
[ defaults setInteger:period forKey:@"ChangePeriod" ];
|
2005-04-23 15:14:52 +00:00
|
|
|
|
|
|
|
// synchronize
|
|
|
|
[ defaults synchronize ];
|
|
|
|
|
|
|
|
// end the sheet
|
|
|
|
[ NSApp endSheet:mConfigureSheet ];
|
2009-12-14 13:10:49 +00:00
|
|
|
return;
|
|
|
|
Bad:
|
|
|
|
; // Empty statement is needed to prevent compiler error
|
|
|
|
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
|
|
|
|
[alert addButtonWithTitle:@"OK"];
|
|
|
|
[alert setMessageText:@"Please enter a number between 0 and 999."];
|
|
|
|
[alert setAlertStyle:NSCriticalAlertStyle];
|
|
|
|
[alert beginSheetModalForWindow:mConfigureSheet modalDelegate:self didEndSelector:nil contextInfo:nil];
|
2005-04-23 15:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the user clicked the CANCEL button
|
|
|
|
- (IBAction) closeSheetCancel:(id) sender
|
|
|
|
{
|
|
|
|
// nothing to configure
|
|
|
|
[ NSApp endSheet:mConfigureSheet ];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|