• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KUtils

  • kutils
  • kidletime
macpoller.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2009 Dario Freddi <drf at kde.org>
3 Copyright (C) 2003 Tarkvara Design Inc. (from KVIrc source code)
4 Copyright (c) 2008 Roman Jarosz <kedgedev at centrum.cz>
5 Copyright (c) 2008 the Kopete developers <kopete-devel at kde.org>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License version 2 as published by the Free Software Foundation.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22#include "macpoller.h"
23
24// Why does Apple have to make this so complicated?
25static OSStatus LoadFrameworkBundle(CFStringRef framework, CFBundleRef *bundlePtr)
26{
27 OSStatus err;
28 FSRef frameworksFolderRef;
29 CFURLRef baseURL;
30 CFURLRef bundleURL;
31
32 if (bundlePtr == nil)
33 return(-1);
34
35 *bundlePtr = nil;
36
37 baseURL = nil;
38 bundleURL = nil;
39
40 err = FSFindFolder(kOnAppropriateDisk, kFrameworksFolderType, true, &frameworksFolderRef);
41 if (err == noErr) {
42 baseURL = CFURLCreateFromFSRef(kCFAllocatorSystemDefault, &frameworksFolderRef);
43 if (baseURL == nil)
44 err = coreFoundationUnknownErr;
45 }
46
47 if (err == noErr) {
48 bundleURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, baseURL, framework, false);
49 if (bundleURL == nil)
50 err = coreFoundationUnknownErr;
51 }
52
53 if (err == noErr) {
54 *bundlePtr = CFBundleCreate(kCFAllocatorSystemDefault, bundleURL);
55 if (*bundlePtr == nil)
56 err = coreFoundationUnknownErr;
57 }
58
59 if (err == noErr) {
60 if (!CFBundleLoadExecutable(*bundlePtr))
61 err = coreFoundationUnknownErr;
62 }
63
64 // Clean up.
65 if (err != noErr && *bundlePtr != nil) {
66 CFRelease(*bundlePtr);
67 *bundlePtr = nil;
68 }
69
70 if (bundleURL != nil)
71 CFRelease(bundleURL);
72
73 if (baseURL != nil)
74 CFRelease(baseURL);
75
76 return err;
77}
78
79pascal void MacPoller::IdleTimerAction(EventLoopTimerRef, EventLoopIdleTimerMessage inState, void* inUserData)
80{
81 Q_ASSERT(inUserData);
82 switch (inState) {
83 case kEventLoopIdleTimerStarted:
84 case kEventLoopIdleTimerStopped:
85 // Get invoked with this constant at the start of the idle period,
86 // or whenever user activity cancels the idle.
87 ((MacPoller*)inUserData)->m_secondsIdle = 0;
88 ((MacPoller*)inUserData)->triggerResume();
89 break;
90 case kEventLoopIdleTimerIdling:
91 // Called every time the timer fires (i.e. every second).
92 ((MacPoller*)inUserData)->m_secondsIdle++;
93 ((MacPoller*)inUserData)->poll();
94 break;
95 }
96}
97
98// Typedef for the function we're getting back from CFBundleGetFunctionPointerForName.
99typedef OSStatus(*InstallEventLoopIdleTimerPtr)(EventLoopRef inEventLoop,
100 EventTimerInterval inFireDelay,
101 EventTimerInterval inInterval,
102 EventLoopIdleTimerUPP inTimerProc,
103 void * inTimerData,
104 EventLoopTimerRef * outTimer);
105
106MacPoller::MacPoller(QWidget *parent)
107 : AbstractSystemPoller(parent)
108 , m_timerRef(0)
109 , m_secondsIdle(0)
110 , m_catch(false)
111{
112}
113
114MacPoller::~MacPoller()
115{
116}
117
118void MacPoller::unloadPoller()
119{
120 RemoveEventLoopTimer(m_timerRef);
121}
122
123bool MacPoller::isAvailable()
124{
125 return true;
126}
127
128bool MacPoller::setUpPoller()
129{
130 // May already be init'ed.
131 if (m_timerRef) {
132 return true;
133 }
134
135 // According to the docs, InstallEventLoopIdleTimer is new in 10.2.
136 // According to the headers, it has been around since 10.0.
137 // One of them is lying. We'll play it safe and weak-link the function.
138
139 // Load the "Carbon.framework" bundle.
140 CFBundleRef carbonBundle;
141
142 if (LoadFrameworkBundle(CFSTR("Carbon.framework"), &carbonBundle) != noErr) {
143 return false;
144 }
145
146 // Load the Mach-O function pointers for the routine we will be using.
147 InstallEventLoopIdleTimerPtr myInstallEventLoopIdleTimer =
148 (InstallEventLoopIdleTimerPtr)CFBundleGetFunctionPointerForName(carbonBundle, CFSTR("InstallEventLoopIdleTimer"));
149
150 if (myInstallEventLoopIdleTimer == 0) {
151 return false;
152 }
153
154 EventLoopIdleTimerUPP timerUPP = NewEventLoopIdleTimerUPP(IdleTimerAction);
155 if ((*myInstallEventLoopIdleTimer)(GetMainEventLoop(), kEventDurationSecond, kEventDurationSecond, timerUPP, this, &m_timerRef)) {
156 return true;
157 }
158
159 return false;
160}
161
162QList<int> MacPoller::timeouts() const
163{
164 return m_timeouts;
165}
166
167void MacPoller::addTimeout(int nextTimeout)
168{
169 m_timeouts.append(nextTimeout);
170 poll();
171}
172
173int MacPoller::poll()
174{
175 int idle = m_secondsIdle * 1000;
176
177 // Check if we reached a timeout..
178 foreach(int i, m_timeouts) {
179 if ((i - idle < 1000 && i > idle) || (idle - i < 1000 && idle > i)) {
180 // Bingo!
181 emit timeoutReached(i);
182 }
183 }
184
185 return idle;
186}
187
188int MacPoller::forcePollRequest()
189{
190 return poll();
191}
192
193void MacPoller::removeTimeout(int timeout)
194{
195 m_timeouts.removeOne(timeout);
196 poll();
197}
198
199void MacPoller::catchIdleEvent()
200{
201 m_catch = true;
202}
203
204void MacPoller::stopCatchingIdleEvents()
205{
206 m_catch = false;
207}
208
209void MacPoller::triggerResume()
210{
211 if (m_catch) {
212 emit resumingFromIdle();
213 stopCatchingIdleEvents();
214 }
215}
216
217void MacPoller::simulateUserActivity()
218{
219 // TODO
220}
221
222#include "macpoller.moc"
AbstractSystemPoller
Definition: abstractsystempoller.h:25
AbstractSystemPoller::resumingFromIdle
void resumingFromIdle()
AbstractSystemPoller::timeoutReached
void timeoutReached(int msec)
MacPoller
Definition: macpoller.h:27
MacPoller::IdleTimerAction
static pascal void IdleTimerAction(EventLoopTimerRef, EventLoopIdleTimerMessage inState, void *inUserData)
Definition: macpoller.cpp:79
MacPoller::stopCatchingIdleEvents
void stopCatchingIdleEvents()
Definition: macpoller.cpp:204
MacPoller::~MacPoller
virtual ~MacPoller()
Definition: macpoller.cpp:114
MacPoller::MacPoller
MacPoller(QWidget *parent=0)
Definition: macpoller.cpp:106
MacPoller::forcePollRequest
int forcePollRequest()
Definition: macpoller.cpp:188
MacPoller::isAvailable
bool isAvailable()
Definition: macpoller.cpp:123
MacPoller::addTimeout
void addTimeout(int nextTimeout)
Definition: macpoller.cpp:167
MacPoller::unloadPoller
void unloadPoller()
Definition: macpoller.cpp:118
MacPoller::triggerResume
void triggerResume()
Definition: macpoller.cpp:209
MacPoller::removeTimeout
void removeTimeout(int nextTimeout)
Definition: macpoller.cpp:193
MacPoller::setUpPoller
bool setUpPoller()
Definition: macpoller.cpp:128
MacPoller::timeouts
QList< int > timeouts() const
Definition: macpoller.cpp:162
MacPoller::catchIdleEvent
void catchIdleEvent()
Definition: macpoller.cpp:199
MacPoller::simulateUserActivity
void simulateUserActivity()
Definition: macpoller.cpp:217
QList
QWidget
timeout
int timeout
LoadFrameworkBundle
static OSStatus LoadFrameworkBundle(CFStringRef framework, CFBundleRef *bundlePtr)
Definition: macpoller.cpp:25
InstallEventLoopIdleTimerPtr
OSStatus(* InstallEventLoopIdleTimerPtr)(EventLoopRef inEventLoop, EventTimerInterval inFireDelay, EventTimerInterval inInterval, EventLoopIdleTimerUPP inTimerProc, void *inTimerData, EventLoopTimerRef *outTimer)
Definition: macpoller.cpp:99
macpoller.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KUtils

Skip menu "KUtils"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal