| 1 |
/* AbiSource Application Framework |
| 2 |
* Copyright (C) 2001 Hubert Figuiere |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU General Public License |
| 6 |
* as published by the Free Software Foundation; either version 2 |
| 7 |
* of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 |
* 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#import <Cocoa/Cocoa.h> |
| 21 |
|
| 22 |
#import "xap_CocoaTimer.h" |
| 23 |
|
| 24 |
#include "ut_assert.h" |
| 25 |
#include "ut_mutex.h" |
| 26 |
#include "ut_debugmsg.h" |
| 27 |
|
| 28 |
#include "ut_unixTimer.h" |
| 29 |
|
| 30 |
NSLock* UT_UNIXTimer::s_timerMutex; |
| 31 |
NSMutableDictionary* UT_UNIXTimer::s_timerIds = nil;; |
| 32 |
int UT_UNIXTimer::s_lastTimerId = 1; |
| 33 |
|
| 34 |
@interface XAP_TimerInvocation: NSInvocation { |
| 35 |
int (*proc)(void *); |
| 36 |
void * param; |
| 37 |
} |
| 38 |
+(XAP_TimerInvocation *)createWithProc:(int (*)(void *))pr param:(void*)p; |
| 39 |
-(void)invoke; |
| 40 |
-(void)retainArguments; |
| 41 |
@end |
| 42 |
|
| 43 |
@implementation XAP_TimerInvocation |
| 44 |
+(XAP_TimerInvocation *)createWithProc:(int (*)(void *))pr param:(void*)p |
| 45 |
{ |
| 46 |
XAP_TimerInvocation * obj = [XAP_TimerInvocation alloc]; |
| 47 |
obj->proc = pr; |
| 48 |
obj->param = p; |
| 49 |
return [obj autorelease]; |
| 50 |
} |
| 51 |
|
| 52 |
-(void)invoke |
| 53 |
{ |
| 54 |
proc(param); |
| 55 |
} |
| 56 |
|
| 57 |
-(void)retainArguments |
| 58 |
{ |
| 59 |
// don't need. |
| 60 |
} |
| 61 |
@end |
| 62 |
|
| 63 |
/*! |
| 64 |
Check that the NSLock exist, and eventually lock it. |
| 65 |
*/ |
| 66 |
void _checkLock(void) |
| 67 |
{ |
| 68 |
if (!UT_UNIXTimer::s_timerMutex) { |
| 69 |
UT_UNIXTimer::s_timerMutex = [[NSLock alloc] init]; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
UT_uint32 XAP_newCocoaTimer (UT_uint32 time, int (*proc)(void *), void *p) |
| 75 |
{ |
| 76 |
NSTimeInterval dTime = (double)time / 1000.; |
| 77 |
|
| 78 |
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:dTime |
| 79 |
invocation:[XAP_TimerInvocation createWithProc:proc param:p] |
| 80 |
repeats:TRUE]; |
| 81 |
|
| 82 |
int tid = -1; |
| 83 |
{ |
| 84 |
_checkLock(); |
| 85 |
[UT_UNIXTimer::s_timerMutex lock]; |
| 86 |
tid = UT_UNIXTimer::s_lastTimerId; |
| 87 |
if (UT_UNIXTimer::s_timerIds == nil) { |
| 88 |
UT_UNIXTimer::s_timerIds = [[NSMutableDictionary alloc] init]; |
| 89 |
} |
| 90 |
[UT_UNIXTimer::s_timerIds setObject:timer forKey:[NSNumber numberWithInt:UT_UNIXTimer::s_lastTimerId++]]; |
| 91 |
[UT_UNIXTimer::s_timerMutex unlock]; |
| 92 |
} |
| 93 |
|
| 94 |
return tid; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
void XAP_stopCocoaTimer (UT_uint32 timerId) |
| 100 |
{ |
| 101 |
NSTimer *pTimer; |
| 102 |
{ |
| 103 |
_checkLock(); |
| 104 |
[UT_UNIXTimer::s_timerMutex lock]; |
| 105 |
NSNumber* key = [NSNumber numberWithInt:timerId]; |
| 106 |
pTimer = [[UT_UNIXTimer::s_timerIds objectForKey:key] retain]; |
| 107 |
[UT_UNIXTimer::s_timerIds removeObjectForKey:key]; |
| 108 |
[UT_UNIXTimer::s_timerMutex unlock]; |
| 109 |
} |
| 110 |
|
| 111 |
[pTimer invalidate]; |
| 112 |
[pTimer release]; |
| 113 |
} |
| 114 |
|
| 115 |
|