2
* idle_mac.cpp - detect desktop idle time
3
* Copyright (C) 2003 Tarkvara Design Inc.
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
#include <Carbon/Carbon.h>
25
// Why does Apple have to make this so complicated?
26
static OSStatus LoadFrameworkBundle(CFStringRef framework, CFBundleRef *bundlePtr) {
28
FSRef frameworksFolderRef;
32
if ( bundlePtr == nil ) return( -1 );
39
err = FSFindFolder(kOnAppropriateDisk, kFrameworksFolderType, true, &frameworksFolderRef);
41
baseURL = CFURLCreateFromFSRef(kCFAllocatorSystemDefault, &frameworksFolderRef);
43
err = coreFoundationUnknownErr;
47
bundleURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, baseURL, framework, false);
48
if (bundleURL == nil) {
49
err = coreFoundationUnknownErr;
53
*bundlePtr = CFBundleCreate(kCFAllocatorSystemDefault, bundleURL);
54
if (*bundlePtr == nil) {
55
err = coreFoundationUnknownErr;
59
if ( ! CFBundleLoadExecutable( *bundlePtr ) ) {
60
err = coreFoundationUnknownErr;
65
if (err != noErr && *bundlePtr != nil) {
66
CFRelease(*bundlePtr);
69
if (bundleURL != nil) {
80
class IdlePlatform::Private {
82
EventLoopTimerRef mTimerRef;
85
Private() : mTimerRef(0), mSecondsIdle(0) {}
87
static pascal void IdleTimerAction(EventLoopTimerRef, EventLoopIdleTimerMessage inState, void* inUserData);
92
pascal void IdlePlatform::Private::IdleTimerAction(EventLoopTimerRef, EventLoopIdleTimerMessage inState, void* inUserData) {
94
case kEventLoopIdleTimerStarted:
95
case kEventLoopIdleTimerStopped:
96
// Get invoked with this constant at the start of the idle period,
97
// or whenever user activity cancels the idle.
98
((IdlePlatform::Private*)inUserData)->mSecondsIdle = 0;
100
case kEventLoopIdleTimerIdling:
101
// Called every time the timer fires (i.e. every second).
102
((IdlePlatform::Private*)inUserData)->mSecondsIdle++;
108
IdlePlatform::IdlePlatform() {
112
IdlePlatform::~IdlePlatform() {
113
RemoveEventLoopTimer(d->mTimerRef);
118
// Typedef for the function we're getting back from CFBundleGetFunctionPointerForName.
119
typedef OSStatus (*InstallEventLoopIdleTimerPtr)(EventLoopRef inEventLoop,
120
EventTimerInterval inFireDelay,
121
EventTimerInterval inInterval,
122
EventLoopIdleTimerUPP inTimerProc,
124
EventLoopTimerRef * outTimer);
127
bool IdlePlatform::init() {
128
// May already be init'ed.
133
// According to the docs, InstallEventLoopIdleTimer is new in 10.2.
134
// According to the headers, it has been around since 10.0.
135
// One of them is lying. We'll play it safe and weak-link the function.
137
// Load the "Carbon.framework" bundle.
138
CFBundleRef carbonBundle;
139
if (LoadFrameworkBundle( CFSTR("Carbon.framework"), &carbonBundle ) != noErr) {
143
// Load the Mach-O function pointers for the routine we will be using.
144
InstallEventLoopIdleTimerPtr myInstallEventLoopIdleTimer = (InstallEventLoopIdleTimerPtr)CFBundleGetFunctionPointerForName(carbonBundle, CFSTR("InstallEventLoopIdleTimer"));
145
if (myInstallEventLoopIdleTimer == 0) {
149
EventLoopIdleTimerUPP timerUPP = NewEventLoopIdleTimerUPP(Private::IdleTimerAction);
150
if ((*myInstallEventLoopIdleTimer)(GetMainEventLoop(), kEventDurationSecond, kEventDurationSecond, timerUPP, 0, &d->mTimerRef)) {
158
int IdlePlatform::secondsIdle() {
159
return d->mSecondsIdle;