Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / vcl / osx / saltimer.cxx
blob2a3158102517e16350b716423c0c46619e14d854
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <rtl/math.hxx>
24 #include "osx/saltimer.h"
25 #include "osx/salnstimer.h"
26 #include "osx/saldata.hxx"
27 #include "osx/salframe.h"
28 #include "osx/salinst.h"
30 NSTimer* AquaSalTimer::pRunningTimer = nil;
31 bool AquaSalTimer::bDispatchTimer = false;
33 void ImplSalStartTimer( sal_uLong nMS )
35 SalData* pSalData = GetSalData();
36 if( pSalData->mpFirstInstance->isNSAppThread() )
38 AquaSalTimer::bDispatchTimer = true;
39 NSTimeInterval aTI = double(nMS)/1000.0;
40 if( AquaSalTimer::pRunningTimer != nil )
42 if (rtl::math::approxEqual(
43 [AquaSalTimer::pRunningTimer timeInterval], aTI))
45 // set new fire date
46 [AquaSalTimer::pRunningTimer setFireDate: [NSDate dateWithTimeIntervalSinceNow: aTI]];
48 else
50 [AquaSalTimer::pRunningTimer invalidate];
51 AquaSalTimer::pRunningTimer = nil;
54 if( AquaSalTimer::pRunningTimer == nil )
56 AquaSalTimer::pRunningTimer = [NSTimer scheduledTimerWithTimeInterval: aTI
57 target: [[[TimerCallbackCaller alloc] init] autorelease]
58 selector: @selector(timerElapsed:)
59 userInfo: nil
60 repeats: YES];
61 /* #i84055# add timer to tracking run loop mode,
62 so they also elapse while e.g. life resize
64 [[NSRunLoop currentRunLoop] addTimer: AquaSalTimer::pRunningTimer forMode: NSEventTrackingRunLoopMode];
67 else
69 SalData::ensureThreadAutoreleasePool();
70 // post an event so we can get into the main thread
71 SAL_WNODEPRECATED_DECLARATIONS_PUSH
72 // 'NSApplicationDefined' is deprecated: first deprecated in macOS 10.12
73 NSEvent* pEvent = [NSEvent otherEventWithType: NSApplicationDefined
74 location: NSZeroPoint
75 modifierFlags: 0
76 timestamp: [NSDate timeIntervalSinceReferenceDate]
77 windowNumber: 0
78 context: nil
79 subtype: AquaSalInstance::AppStartTimerEvent
80 data1: (int)nMS
81 data2: 0 ];
82 SAL_WNODEPRECATED_DECLARATIONS_POP
83 if( pEvent )
84 [NSApp postEvent: pEvent atStart: YES];
88 void ImplSalStopTimer()
90 AquaSalTimer::bDispatchTimer = false;
93 void AquaSalTimer::handleStartTimerEvent( NSEvent* pEvent )
95 ImplSVData* pSVData = ImplGetSVData();
96 if( pSVData->mpSalTimer )
98 NSTimeInterval posted = [pEvent timestamp] + NSTimeInterval([pEvent data1])/1000.0;
99 NSTimeInterval current = [NSDate timeIntervalSinceReferenceDate];
100 if( (posted - current) <= 0.0 )
102 SolarMutexGuard aGuard;
103 if( pSVData->mpSalTimer )
105 // timer already elapsed since event posted
106 bool idle = true; // TODO
107 pSVData->mpSalTimer->CallCallback( idle );
110 ImplSalStartTimer( sal_uLong( [pEvent data1] ) );
115 AquaSalTimer::AquaSalTimer( )
119 AquaSalTimer::~AquaSalTimer()
121 ImplSalStopTimer();
124 void AquaSalTimer::Start( sal_uLong nMS )
126 ImplSalStartTimer( nMS );
129 void AquaSalTimer::Stop()
131 ImplSalStopTimer();
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */