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