1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_vcl.hxx" 26 27 #include "aqua/saltimer.h" 28 #include "aqua/salnstimer.h" 29 #include "aqua/saldata.hxx" 30 #include "aqua/salframe.h" 31 #include "aqua/salinst.h" 32 33 // ======================================================================= 34 35 NSTimer* AquaSalTimer::pRunningTimer = nil; 36 bool AquaSalTimer::bDispatchTimer = false; 37 38 39 void ImplSalStartTimer( sal_uLong nMS ) 40 { 41 SalData* pSalData = GetSalData(); 42 if( pSalData->mpFirstInstance->isNSAppThread() ) 43 { 44 AquaSalTimer::bDispatchTimer = true; 45 NSTimeInterval aTI = double(nMS)/1000.0; 46 if( AquaSalTimer::pRunningTimer != nil ) 47 { 48 if( [AquaSalTimer::pRunningTimer timeInterval] == aTI ) 49 // set new fire date 50 [AquaSalTimer::pRunningTimer setFireDate: [NSDate dateWithTimeIntervalSinceNow: aTI]]; 51 else 52 { 53 [AquaSalTimer::pRunningTimer invalidate]; 54 AquaSalTimer::pRunningTimer = nil; 55 } 56 } 57 if( AquaSalTimer::pRunningTimer == nil ) 58 { 59 AquaSalTimer::pRunningTimer = [NSTimer scheduledTimerWithTimeInterval: aTI 60 target: [[[TimerCallbackCaller alloc] init] autorelease] 61 selector: @selector(timerElapsed:) 62 userInfo: nil 63 repeats: YES]; 64 /* #i84055# add timer to tracking run loop mode, 65 so they also elapse while e.g. life resize 66 */ 67 [[NSRunLoop currentRunLoop] addTimer: AquaSalTimer::pRunningTimer forMode: NSEventTrackingRunLoopMode]; 68 } 69 } 70 else 71 { 72 SalData::ensureThreadAutoreleasePool(); 73 // post an event so we can get into the main thread 74 NSPoint aPt = { 0, 0 }; 75 NSEvent* pEvent = [NSEvent otherEventWithType: NSApplicationDefined 76 location: aPt 77 modifierFlags: 0 78 timestamp: [NSDate timeIntervalSinceReferenceDate] 79 windowNumber: 0 80 context: nil 81 subtype: AquaSalInstance::AppStartTimerEvent 82 data1: (int)nMS 83 data2: 0 ]; 84 if( pEvent ) 85 [NSApp postEvent: pEvent atStart: YES]; 86 } 87 } 88 89 void ImplSalStopTimer() 90 { 91 AquaSalTimer::bDispatchTimer = false; 92 } 93 94 void AquaSalTimer::handleStartTimerEvent( NSEvent* pEvent ) 95 { 96 ImplSVData* pSVData = ImplGetSVData(); 97 if( pSVData->mpSalTimer ) 98 { 99 NSTimeInterval posted = [pEvent timestamp] + NSTimeInterval([pEvent data1])/1000.0; 100 NSTimeInterval current = [NSDate timeIntervalSinceReferenceDate]; 101 if( (posted - current) <= 0.0 ) 102 { 103 YIELD_GUARD; 104 // timer already elapsed since event posted 105 pSVData->mpSalTimer->CallCallback(); 106 } 107 ImplSalStartTimer( sal_uLong( [pEvent data1] ) ); 108 } 109 110 } 111 112 AquaSalTimer::AquaSalTimer( ) 113 { 114 } 115 116 AquaSalTimer::~AquaSalTimer() 117 { 118 ImplSalStopTimer(); 119 } 120 121 void AquaSalTimer::Start( sal_uLong nMS ) 122 { 123 ImplSalStartTimer( nMS ); 124 } 125 126 void AquaSalTimer::Stop() 127 { 128 ImplSalStopTimer(); 129 } 130 131 132