forked from KDAB/KDUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacos_platform_event_loop.mm
More file actions
87 lines (73 loc) · 2.55 KB
/
macos_platform_event_loop.mm
File metadata and controls
87 lines (73 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
This file is part of KDUtils.
SPDX-FileCopyrightText: 2018 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Paul Lemire <paul.lemire@kdab.com>
SPDX-License-Identifier: MIT
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#include "macos_platform_event_loop.h"
#include "macos_platform_timer.h"
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#include <limits>
#include <memory>
constexpr auto KDFoundationCocoaEventSubTypeWakeup = std::numeric_limits<short>::max();
namespace KDFoundation {
MacOSPlatformEventLoop::MacOSPlatformEventLoop()
{
@autoreleasepool {
// make sure there's a NSApp
[NSApplication sharedApplication];
}
}
MacOSPlatformEventLoop::~MacOSPlatformEventLoop() = default;
void MacOSPlatformEventLoop::waitForEvents(int timeout)
{
@autoreleasepool {
NSDate *expiration = [timeout] {
if (timeout == -1)
return [NSDate distantFuture];
if (timeout == 0)
return [NSDate distantPast];
return [NSDate dateWithTimeIntervalSinceNow:static_cast<double>(timeout) / 1000.0];
}();
NSEvent *event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:expiration inMode:NSDefaultRunLoopMode dequeue:YES];
if (event)
[NSApp sendEvent:event];
}
}
void MacOSPlatformEventLoop::wakeUp()
{
// post a dummy event to wake up the event loop
postEmptyEvent();
}
void MacOSPlatformEventLoop::postEmptyEvent()
{
@autoreleasepool {
[NSApp postEvent:[NSEvent otherEventWithType:NSEventTypeApplicationDefined
location:NSZeroPoint
modifierFlags:0
timestamp:0
windowNumber:0
context:nil
subtype:KDFoundationCocoaEventSubTypeWakeup
data1:0
data2:0]
atStart:NO];
}
}
bool MacOSPlatformEventLoop::registerNotifier(FileDescriptorNotifier * /* notifier */)
{
// TODO
return false;
}
bool MacOSPlatformEventLoop::unregisterNotifier(FileDescriptorNotifier * /* notifier */)
{
// TODO
return false;
}
std::unique_ptr<AbstractPlatformTimer> MacOSPlatformEventLoop::createPlatformTimerImpl(Timer *timer)
{
return std::make_unique<MacOSPlatformTimer>(timer);
}
} // namespace KDFoundation