aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2022-12-27 10:46:06 -0500
committerDavid Robillard <d@drobilla.net>2022-12-27 10:46:06 -0500
commitd5efee77f8daf572602a9925e34c96698a1dcbdf (patch)
treebfad9228f631d7029ec8e0f5f7be54a8e7e3e48e
parentef2544be3f88b20401699519ff75c56b6f151700 (diff)
downloadpugl-d5efee77f8daf572602a9925e34c96698a1dcbdf.tar.gz
pugl-d5efee77f8daf572602a9925e34c96698a1dcbdf.tar.bz2
pugl-d5efee77f8daf572602a9925e34c96698a1dcbdf.zip
MacOS: Fix timestamps on systems with different clock rates
This seems to be a thing at least on MacOS 12 on M1.
-rw-r--r--src/mac.h7
-rw-r--r--src/mac.m12
2 files changed, 14 insertions, 5 deletions
diff --git a/src/mac.h b/src/mac.h
index 10e37c5..7473231 100644
--- a/src/mac.h
+++ b/src/mac.h
@@ -9,6 +9,8 @@
#import <Cocoa/Cocoa.h>
+#include <mach/mach_time.h>
+
#include <stdint.h>
@interface PuglWrapperView : NSView<NSTextInputClient>
@@ -25,8 +27,9 @@
@end
struct PuglWorldInternalsImpl {
- NSApplication* app;
- NSAutoreleasePool* autoreleasePool;
+ NSApplication* app;
+ NSAutoreleasePool* autoreleasePool;
+ struct mach_timebase_info timebaseInfo;
};
struct PuglInternalsImpl {
diff --git a/src/mac.m b/src/mac.m
index ab8ea68..72b3e5f 100644
--- a/src/mac.m
+++ b/src/mac.m
@@ -913,6 +913,10 @@ puglInitWorldInternals(PuglWorldType type, PuglWorldFlags PUGL_UNUSED(flags))
impl->app = [NSApplication sharedApplication];
+ if (mach_timebase_info(&impl->timebaseInfo)) {
+ return NULL;
+ }
+
if (type == PUGL_PROGRAM) {
impl->autoreleasePool = [NSAutoreleasePool new];
@@ -1400,7 +1404,9 @@ puglProcessEvents(PuglView* view)
double
puglGetTime(const PuglWorld* world)
{
- return (mach_absolute_time() / 1e9) - world->startTime;
+ return ((double)mach_absolute_time() * world->impl->timebaseInfo.denom /
+ world->impl->timebaseInfo.numer / 1e9) -
+ world->startTime;
}
PuglStatus
@@ -1575,7 +1581,7 @@ puglPaste(PuglView* const view)
const PuglDataOfferEvent offer = {
PUGL_DATA_OFFER,
0,
- mach_absolute_time() / 1e9,
+ puglGetTime(view->world),
};
PuglEvent offerEvent;
@@ -1633,7 +1639,7 @@ puglAcceptOffer(PuglView* const view,
wrapper->dragTypeIndex = typeIndex;
const PuglDataEvent data = {
- PUGL_DATA, 0U, mach_absolute_time() / 1e9, (uint32_t)typeIndex};
+ PUGL_DATA, 0U, puglGetTime(view->world), (uint32_t)typeIndex};
PuglEvent dataEvent;
dataEvent.data = data;