5#include <QtCore/qcoreapplication.h>
6#include <QtCore/qsocketnotifier.h>
7#include <QtCore/qthread.h>
19 int nativeSocket = CFSocketGetNative(
s);
28 if (callbackType == kCFSocketConnectCallBack) {
31 if (socketInfo->readNotifier)
33 if (socketInfo->writeNotifier)
36 }
else if (callbackType == kCFSocketReadCallBack) {
37 if (socketInfo->readNotifier && socketInfo->readEnabled) {
38 socketInfo->readEnabled =
false;
41 }
else if (callbackType == kCFSocketWriteCallBack) {
42 if (socketInfo->writeNotifier && socketInfo->writeEnabled) {
43 socketInfo->writeEnabled =
false;
48 if (cfSocketNotifier->maybeCancelWaitForMoreEvents)
49 cfSocketNotifier->maybeCancelWaitForMoreEvents(cfSocketNotifier->eventDispatcher);
57 CFRunLoopSourceRef loopSource = CFSocketCreateRunLoopSource(kCFAllocatorDefault,
socket, 0);
61 CFRunLoopAddSource(CFRunLoopGetCurrent(), loopSource, kCFRunLoopCommonModes);
71 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runloop, kCFRunLoopCommonModes);
72 CFSocketDisableCallBacks(
socket, kCFSocketReadCallBack);
73 CFSocketDisableCallBacks(
socket, kCFSocketWriteCallBack);
78 , maybeCancelWaitForMoreEvents(0)
79 , enableNotifiersObserver(0)
91 eventDispatcher = hostEventDispacher;
96 maybeCancelWaitForMoreEvents = callBack;
102 int nativeSocket =
notifier->socket();
105 if (nativeSocket < 0 || nativeSocket > FD_SETSIZE) {
106 qWarning(
"QSocketNotifier: Internal error");
110 qWarning(
"QSocketNotifier: socket notifiers cannot be enabled from another thread");
116 qWarning(
"QSocketNotifier::Exception is not supported on iOS");
127 const int callbackTypes = kCFSocketConnectCallBack | kCFSocketReadCallBack | kCFSocketWriteCallBack;
128 CFSocketContext
context = {0,
this, 0, 0, 0};
130 if (CFSocketIsValid(socketInfo->socket) ==
false) {
131 qWarning(
"QEventDispatcherMac::registerSocketNotifier: Failed to create CFSocket");
135 CFOptionFlags
flags = CFSocketGetSocketFlags(socketInfo->socket);
137 flags &= ~kCFSocketCloseOnInvalidate;
139 flags &= ~(kCFSocketAutomaticallyReenableWriteCallBack | kCFSocketAutomaticallyReenableReadCallBack);
140 CFSocketSetSocketFlags(socketInfo->socket,
flags);
142 macSockets.
insert(nativeSocket, socketInfo);
146 Q_ASSERT(socketInfo->readNotifier == 0);
147 socketInfo->readNotifier =
notifier;
148 socketInfo->readEnabled =
false;
150 Q_ASSERT(socketInfo->writeNotifier == 0);
151 socketInfo->writeNotifier =
notifier;
152 socketInfo->writeEnabled =
false;
155 if (!enableNotifiersObserver) {
158 CFRunLoopObserverContext
context = {};
160 enableNotifiersObserver = CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopBeforeSources,
161 true, 0, enableSocketNotifiers, &
context);
163 CFRunLoopAddObserver(CFRunLoopGetCurrent(), enableNotifiersObserver, kCFRunLoopCommonModes);
170 int nativeSocket =
notifier->socket();
173 if (nativeSocket < 0 || nativeSocket > FD_SETSIZE) {
174 qWarning(
"QSocketNotifier: Internal error");
177 qWarning(
"QSocketNotifier: socket notifiers cannot be disabled from another thread");
183 qWarning(
"QSocketNotifier::Exception is not supported on iOS");
188 qWarning(
"QEventDispatcherMac::unregisterSocketNotifier: Tried to unregister a not registered notifier");
195 socketInfo->readNotifier = 0;
196 socketInfo->readEnabled =
false;
197 CFSocketDisableCallBacks(socketInfo->socket, kCFSocketReadCallBack);
200 socketInfo->writeNotifier = 0;
201 socketInfo->writeEnabled =
false;
202 CFSocketDisableCallBacks(socketInfo->socket, kCFSocketWriteCallBack);
206 if (socketInfo->readNotifier == 0 && socketInfo->writeNotifier == 0) {
207 unregisterSocketInfo(socketInfo);
209 macSockets.
remove(nativeSocket);
216 for (
MacSocketInfo *socketInfo : std::as_const(macSockets)) {
217 unregisterSocketInfo(socketInfo);
223 destroyRunLoopObserver();
226void QCFSocketNotifier::destroyRunLoopObserver()
228 if (!enableNotifiersObserver)
231 CFRunLoopObserverInvalidate(enableNotifiersObserver);
232 CFRelease(enableNotifiersObserver);
233 enableNotifiersObserver = 0;
236void QCFSocketNotifier::unregisterSocketInfo(
MacSocketInfo *socketInfo)
238 if (socketInfo->runloop) {
239 if (CFSocketIsValid(socketInfo->socket))
241 CFRunLoopSourceInvalidate(socketInfo->runloop);
242 CFRelease(socketInfo->runloop);
244 CFSocketInvalidate(socketInfo->socket);
245 CFRelease(socketInfo->socket);
248void QCFSocketNotifier::enableSocketNotifiers(CFRunLoopObserverRef
ref, CFRunLoopActivity activity,
void *
info)
256 if (!CFSocketIsValid(socketInfo->socket))
259 if (!socketInfo->runloop) {
262 qWarning(
"QEventDispatcherMac::registerSocketNotifier: Failed to add CFSocket to runloop");
263 CFSocketInvalidate(socketInfo->socket);
273 socketInfo->readEnabled = (socketInfo->readNotifier !=
nullptr);
274 if (!socketInfo->readEnabled)
275 CFSocketDisableCallBacks(socketInfo->socket, kCFSocketReadCallBack);
276 socketInfo->writeEnabled = (socketInfo->writeNotifier !=
nullptr);
277 if (!socketInfo->writeEnabled)
278 CFSocketDisableCallBacks(socketInfo->socket, kCFSocketWriteCallBack);
282 if (socketInfo->readNotifier && !socketInfo->readEnabled) {
283 socketInfo->readEnabled =
true;
284 CFSocketEnableCallBacks(socketInfo->socket, kCFSocketReadCallBack);
286 if (socketInfo->writeNotifier && !socketInfo->writeEnabled) {
287 socketInfo->writeEnabled =
true;
288 CFSocketEnableCallBacks(socketInfo->socket, kCFSocketWriteCallBack);
DarwinBluetooth::LECBManagerNotifier * notifier
void unregisterSocketNotifier(QSocketNotifier *notifier)
void removeSocketNotifiers()
void setMaybeCancelWaitForMoreEventsCallback(MaybeCancelWaitForMoreEventsFn callBack)
void registerSocketNotifier(QSocketNotifier *notifier)
void setHostEventDispatcher(QAbstractEventDispatcher *hostEventDispacher)
friend void qt_mac_socket_callback(CFSocketRef, CFSocketCallBackType, CFDataRef, const void *, void *)
static bool sendEvent(QObject *receiver, QEvent *event)
Sends event event directly to receiver receiver, using the notify() function.
bool remove(const Key &key)
Removes the item that has the key from the hash.
T value(const Key &key) const noexcept
void clear() noexcept(std::is_nothrow_destructible< Node >::value)
Removes all items from the hash and frees up all memory used by it.
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
QThread * thread() const
Returns the thread in which the object lives.
static QThread * currentThread()
Combined button and popup list for selecting options.
CFRunLoopSourceRef qt_mac_add_socket_to_runloop(const CFSocketRef socket)
void qt_mac_remove_socket_from_runloop(const CFSocketRef socket, CFRunLoopSourceRef runloop)
QT_BEGIN_NAMESPACE void qt_mac_socket_callback(CFSocketRef s, CFSocketCallBackType callbackType, CFDataRef, const void *data, void *info)
void(* MaybeCancelWaitForMoreEventsFn)(QAbstractEventDispatcher *hostEventDispacher)
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data