14#include <Cocoa/Cocoa.h>
31#ifdef MAC_OS_X_VERSION_10_12
44@interface RunStopper : NSObject
47@implementation RunStopper
63 NSEvent* stopEvent = [NSEvent otherEventWithType : ROOT::MacOSX::Details::kApplicationDefined
64 location : NSMakePoint(0., 0.) modifierFlags : 0 timestamp : 0.
65 windowNumber : 0 context : nil subtype: 0 data1 : 0 data2 : 0];
66 [NSApp postEvent : stopEvent atStart : true];
79# define HOWMANY(x, y) (((x)+((y)-1))/(y))
92 ULong_t fds_bits[HOWMANY(kFDSETSIZE, kNFDBITS)];
94 TFdSet() { memset(fds_bits, 0,
sizeof(fds_bits)); }
95 TFdSet(
const TFdSet &
org) { memcpy(fds_bits,
org.fds_bits,
sizeof(
org.fds_bits)); }
99 memcpy(fds_bits, rhs.fds_bits,
sizeof(rhs.fds_bits));
103 void Zero() { memset(fds_bits, 0,
sizeof(fds_bits)); }
107 fds_bits[n/kNFDBITS] |= (1UL << (
n %
kNFDBITS));
115 fds_bits[n/kNFDBITS] &= ~(1UL << (
n %
kNFDBITS));
141 void InitializeCocoa();
144 void UnregisterFileDescriptor(CFFileDescriptorRef fd);
145 void CloseFileDescriptors();
147 enum DescriptorType {
153 void SetFileDescriptor(
int fd, DescriptorType fdType);
155 std::set<CFFileDescriptorRef> fCFFileDescriptors;
158 bool fCocoaInitialized;
160 static MacOSXSystem *fgInstance;
163MacOSXSystem *MacOSXSystem::fgInstance = 0;
171 const int nativeFD = CFFileDescriptorGetNativeDescriptor(fdref);
174 assert(MacOSXSystem::fgInstance != 0 &&
"TMacOSXSystem_ReadCallback, MacOSXSystem's singleton is null");
175 MacOSXSystem::fgInstance->UnregisterFileDescriptor(fdref);
177 CFFileDescriptorInvalidate(fdref);
180 NSEvent *fdEvent = [NSEvent otherEventWithType : kApplicationDefined
181 location : NSMakePoint(0, 0) modifierFlags : 0
182 timestamp: 0. windowNumber : 0 context : nil
183 subtype : 0 data1 : nativeFD data2 : 0];
184 [NSApp postEvent : fdEvent atStart : NO];
191 const int nativeFD = CFFileDescriptorGetNativeDescriptor(fdref);
194 assert(MacOSXSystem::fgInstance != 0 &&
"TMacOSXSystem_WriteCallback, MacOSXSystem's singleton is null");
195 MacOSXSystem::fgInstance->UnregisterFileDescriptor(fdref);
197 CFFileDescriptorInvalidate(fdref);
200 NSEvent *fdEvent = [NSEvent otherEventWithType : kApplicationDefined
201 location : NSMakePoint(0, 0) modifierFlags : 0
202 timestamp: 0. windowNumber : 0 context : nil
203 subtype : 0 data1 : nativeFD data2 : 0];
204 [NSApp postEvent : fdEvent atStart : NO];
210MacOSXSystem::MacOSXSystem()
212 fCocoaInitialized(false)
214 assert(fgInstance == 0 &&
"MacOSXSystem, fgInstance was initialized already");
219MacOSXSystem::~MacOSXSystem()
221 if (fCocoaInitialized)
222 CloseFileDescriptors();
226void MacOSXSystem::InitializeCocoa()
228 assert(fCocoaInitialized ==
false &&
"InitializeCocoa, Cocoa was initialized already");
230 [NSApplication sharedApplication];
232 fCocoaInitialized =
true;
236bool MacOSXSystem::SetFileDescriptors(
const TSeqCollection *fileHandlers)
243 assert(fileHandlers != 0 &&
"SetFileDescriptors, parameter 'fileHandlers' is null");
249 TIter next(fileHandlers);
251 assert(handler->GetFd() != -1 &&
"SetFileDescriptors, invalid file descriptor");
253 if (handler->HasReadInterest())
254 SetFileDescriptor(handler->GetFd(), kDTRead);
256 if (handler->HasWriteInterest())
257 SetFileDescriptor(handler->GetFd(), kDTWrite);
259 }
catch (
const std::exception &) {
260 CloseFileDescriptors();
268void MacOSXSystem::UnregisterFileDescriptor(CFFileDescriptorRef fd)
273 std::set<CFFileDescriptorRef>::iterator fdIter = fCFFileDescriptors.find(fd);
274 assert(fdIter != fCFFileDescriptors.end() &&
"InvalidateFileDescriptor, file descriptor was not found");
275 fCFFileDescriptors.erase(fdIter);
279void MacOSXSystem::CloseFileDescriptors()
282 assert(fCocoaInitialized ==
true &&
"CloseFileDescriptors, Cocoa was not initialized");
284 std::set<CFFileDescriptorRef>::iterator fdIter = fCFFileDescriptors.begin(), end = fCFFileDescriptors.end();
286 for (; fdIter != end; ++fdIter) {
287 CFFileDescriptorInvalidate(*fdIter);
291 fCFFileDescriptors.clear();
295void MacOSXSystem::SetFileDescriptor(
int fd, DescriptorType fdType)
298 assert(fCocoaInitialized ==
true &&
"SetFileDescriptors, Cocoa was not initialized");
300 assert(fd != -1 &&
"SetFileDescriptor, invalid file descriptor");
302 const bool read = fdType == kDTRead;
303 CFFileDescriptorRef fdref = CFFileDescriptorCreate(kCFAllocatorDefault, fd,
false,
307 throw std::runtime_error(
"MacOSXSystem::SetFileDescriptors: CFFileDescriptorCreate failed");
309 CFFileDescriptorEnableCallBacks(fdref, read ? kCFFileDescriptorReadCallBack : kCFFileDescriptorWriteCallBack);
310 CFRunLoopSourceRef runLoopSource = CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, fdref, 0);
312 if (!runLoopSource) {
314 throw std::runtime_error(
"MacOSXSystem::SetFileDescriptors: CFFileDescriptorCreateRunLoopSource failed");
317 CFRunLoopAddSource(CFRunLoopGetMain(), runLoopSource, kCFRunLoopDefaultMode);
318 CFRelease(runLoopSource);
320 fCFFileDescriptors.insert(fdref);
333 : fPimpl(new
Private::MacOSXSystem),
334 fCocoaInitialized(false),
360 Bool_t pollOnce = pendingOnly;
386 if (pendingOnly && !pollOnce)
392 if (!pendingOnly)
return;
447 fPimpl->InitializeCocoa();
461 [stopper.Get() performSelector : @selector(stopRun) withObject : nil afterDelay : 0.05];
470 assert(
fCocoaInitialized ==
true &&
"ProcessPendingEvents, called while Cocoa was not initialized");
472 bool processed =
false;
474 untilDate : nil inMode : NSDefaultRunLoopMode dequeue : YES]) {
475 [NSApp sendEvent : event];
486 assert(
fCocoaInitialized ==
true &&
"WaitEvents, called while Cocoa was not initialized");
490 Fatal(
"WaitForAllEvents",
"SetFileDesciptors failed");
493 NSDate *untilDate = nil;
495 untilDate = [NSDate dateWithTimeIntervalSinceNow : nextto / 1000.];
497 untilDate = [NSDate distantFuture];
503 NSEvent *
event = [NSApp nextEventMatchingMask : Private::kEventMaskAny
504 untilDate : untilDate inMode : NSDefaultRunLoopMode dequeue : YES];
509 [NSApp sendEvent : event];
513 untilDate : nil inMode : NSDefaultRunLoopMode dequeue : YES]))
518 [NSApp sendEvent : event];
521 fPimpl->CloseFileDescriptors();
531 if (fh->
GetFd() == -1)
532 Error(
"AddFileHandler",
"invalid file descriptor");
554 "ProcessApplicationDefinedEvent, called while Cocoa was not initialized");
556 NSEvent *
event = (NSEvent *)
e;
557 assert(event != nil &&
558 "ProcessApplicationDefinedEvent, event parameter is nil");
560 "ProcessApplicationDefinedEvent, event parameter has wrong type");
562 bool descriptorFound =
false;
566 descriptorFound =
true;
571 descriptorFound =
true;
574 if (!descriptorFound) {
575 Error(
"ProcessApplicationDefinedEvent",
"file descriptor %d was not found",
int(event.data1));
void Fatal(const char *location, const char *msgfmt,...)
Binding & operator=(OUT(*fun)(void))
typedef void((*Func_t)())
R__EXTERN TFileHandler * gXDisplay
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
virtual Bool_t Notify()
Notify when event occurred on descriptor associated with this handler.
bool ProcessPendingEvents()
void AddFileHandler(TFileHandler *fh)
Add a file handler to the list of system file handlers.
void WaitEvents(Long_t nextto)
bool CocoaInitialized() const
void DispatchOneEvent(Bool_t pendingOnly)
Dispatch a single event.
TFileHandler * RemoveFileHandler(TFileHandler *fh)
Remove a file handler from the list of file handlers.
void ProcessApplicationDefinedEvent(void *event)
std::unique_ptr< ROOT::MacOSX::Details::MacOSXSystem > fPimpl
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Sequenceable collection abstract base class.
TSeqCollection * fFileHandler
virtual Long_t NextTimeOut(Bool_t mode)
Time when next timer of mode (synchronous=kTRUE or asynchronous=kFALSE) will time-out (in ms).
TFdSet * fWritemask
Files that should be checked for read events.
TFdSet * fSignals
Files with writes waiting.
TFdSet * fWriteready
Files with reads waiting.
TSeqCollection * fSignalHandler
TFdSet * fReadready
Files that should be checked for write events.
Int_t fNfd
Signals that were trapped.
Bool_t CheckSignals(Bool_t sync)
Check if some signals were raised and call their Notify() member.
Bool_t DispatchTimers(Bool_t mode)
Handle and dispatch timers.
void AddFileHandler(TFileHandler *fh)
Add a file handler to the list of system file handlers.
void DispatchOneEvent(Bool_t pendingOnly=kFALSE)
Dispatch a single event.
Bool_t CheckDescriptors()
Check if there is activity on some file descriptors and call their Notify() member.
TFileHandler * RemoveFileHandler(TFileHandler *fh)
Remove a file handler from the list of file handlers.
const NSEventType kApplicationDefined
void TMacOSXSystem_ReadCallback(CFFileDescriptorRef fdref, CFOptionFlags, void *)
const NSUInteger kEventMaskAny
void TMacOSXSystem_WriteCallback(CFFileDescriptorRef fdref, CFOptionFlags, void *)
Namespace for new ROOT classes and functions.
#define org(otri, vertexptr)