Skip to content
Newer
Older
100644 125 lines (99 sloc) 2.97 KB
dc5be1c Initial check-in.
uli authored Jul 4, 2009
1 //
2 // UKIdleTimer.m
3 // CocoaMoose
4 //
5 // Created by Uli Kusterer on Tue Apr 06 2004.
720b0e7 @uliwitness Added licensing information to the source files.
authored Mar 4, 2010
6 // Copyright (c) 2004 Uli Kusterer.
dc5be1c Initial check-in.
uli authored Jul 4, 2009
7 //
720b0e7 @uliwitness Added licensing information to the source files.
authored Mar 4, 2010
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
26 //
27
dc5be1c Initial check-in.
uli authored Jul 4, 2009
28
29 #import "UKIdleTimer.h"
30 #include <Carbon/Carbon.h>
31
32 pascal void UKIdleTimerProc( EventLoopTimerRef inTimer,
33 EventLoopIdleTimerMessage inMessage, void *refCon );
34
35
36 @implementation UKIdleTimer
37
38 -(id) initWithTimeInterval: (NSTimeInterval)interval
39 {
40 self = [super init];
41 if( !self )
42 return nil;
43
44 static EventLoopIdleTimerUPP eventLoopIdleTimerUPP = NULL;
45 if( !eventLoopIdleTimerUPP )
46 eventLoopIdleTimerUPP = NewEventLoopIdleTimerUPP(UKIdleTimerProc);
47
48 if( InstallEventLoopIdleTimer( GetCurrentEventLoop(),
49 kEventDurationSecond * interval,
50 kEventDurationSecond * interval,
51 eventLoopIdleTimerUPP,
52 self, (EventLoopTimerRef*) &carbonTimerRef) != noErr )
53 {
54 [self release];
55 return nil;
56 }
57
58 return self;
59 }
60
61 -(void) dealloc
62 {
63 RemoveEventLoopTimer( (EventLoopTimerRef) carbonTimerRef );
64
65 [super dealloc];
66 }
67
68 -(void) setFireTime: (NSTimeInterval)foo
69 {
70 SetEventLoopTimerNextFireTime( (EventLoopTimerRef) carbonTimerRef, kEventDurationSecond * foo );
71 }
72
73
74 -(id) delegate
75 {
76 return delegate;
77 }
78
79 -(void) setDelegate: (id)newDelegate
80 {
81 delegate = newDelegate;
82 }
83
84
85 -(void) timerBeginsIdling: (id)sender
86 {
87 if( [delegate respondsToSelector: @selector(timerBeginsIdling:)] )
88 [delegate timerBeginsIdling: self];
89 }
90
91
92 -(void) timerContinuesIdling: (id)sender
93 {
94 if( [delegate respondsToSelector: @selector(timerContinuesIdling:)] )
95 [delegate timerContinuesIdling: self];
96 }
97
98
99 -(void) timerFinishedIdling: (id)sender
100 {
101 if( [delegate respondsToSelector: @selector(timerFinishedIdling:)] )
102 [delegate timerFinishedIdling: self];
103 }
104
105 @end
106
107 pascal void UKIdleTimerProc( EventLoopTimerRef inTimer,
108 EventLoopIdleTimerMessage inMessage, void *refCon )
109 {
110 switch( inMessage )
111 {
112 case kEventLoopIdleTimerStarted:
113 [((UKIdleTimer*)refCon) timerBeginsIdling: nil];
114 break;
115
116 case kEventLoopIdleTimerIdling:
117 [((UKIdleTimer*)refCon) timerContinuesIdling: nil];
118 break;
119
120 case kEventLoopIdleTimerStopped:
121 [((UKIdleTimer*)refCon) timerFinishedIdling: nil];
122 break;
123 }
124 }
Something went wrong with that request. Please try again.