17 #include <Cocoa/Cocoa.h> 27 @interface RunStopper : NSObject
30 @implementation RunStopper
46 NSEvent* stopEvent = [NSEvent otherEventWithType : NSApplicationDefined
47 location : NSMakePoint(0., 0.) modifierFlags : 0 timestamp : 0.
48 windowNumber : 0 context : nil subtype: 0 data1 : 0 data2 : 0];
49 [NSApp postEvent : stopEvent atStart : true];
62 # define HOWMANY(x, y) (((x)+((y)-1))/(y)) 75 ULong_t fds_bits[HOWMANY(kFDSETSIZE, kNFDBITS)];
77 TFdSet() { memset(fds_bits, 0,
sizeof(fds_bits)); }
78 TFdSet(
const TFdSet &
org) { memcpy(fds_bits, org.fds_bits,
sizeof(org.fds_bits)); }
82 memcpy(fds_bits, rhs.fds_bits,
sizeof(rhs.fds_bits));
86 void Zero() { memset(fds_bits, 0,
sizeof(fds_bits)); }
90 fds_bits[n/kNFDBITS] |= (1UL << (n %
kNFDBITS));
98 fds_bits[n/kNFDBITS] &= ~(1UL << (n %
kNFDBITS));
124 void InitializeCocoa();
127 void UnregisterFileDescriptor(CFFileDescriptorRef fd);
128 void CloseFileDescriptors();
130 enum DescriptorType {
136 void SetFileDescriptor(
int fd, DescriptorType fdType);
138 std::set<CFFileDescriptorRef> fCFFileDescriptors;
141 bool fCocoaInitialized;
143 static MacOSXSystem *fgInstance;
146 MacOSXSystem *MacOSXSystem::fgInstance = 0;
154 const int nativeFD = CFFileDescriptorGetNativeDescriptor(fdref);
157 assert(MacOSXSystem::fgInstance != 0 &&
"TMacOSXSystem_ReadCallback, MacOSXSystem's singleton is null");
158 MacOSXSystem::fgInstance->UnregisterFileDescriptor(fdref);
160 CFFileDescriptorInvalidate(fdref);
163 NSEvent *fdEvent = [NSEvent otherEventWithType : NSApplicationDefined
164 location : NSMakePoint(0, 0) modifierFlags : 0
165 timestamp: 0. windowNumber : 0 context : nil
166 subtype : 0 data1 : nativeFD data2 : 0];
167 [NSApp postEvent : fdEvent atStart : NO];
174 const int nativeFD = CFFileDescriptorGetNativeDescriptor(fdref);
177 assert(MacOSXSystem::fgInstance != 0 &&
"TMacOSXSystem_WriteCallback, MacOSXSystem's singleton is null");
178 MacOSXSystem::fgInstance->UnregisterFileDescriptor(fdref);
180 CFFileDescriptorInvalidate(fdref);
183 NSEvent *fdEvent = [NSEvent otherEventWithType : NSApplicationDefined
184 location : NSMakePoint(0, 0) modifierFlags : 0
185 timestamp: 0. windowNumber : 0 context : nil
186 subtype : 0 data1 : nativeFD data2 : 0];
187 [NSApp postEvent : fdEvent atStart : NO];
193 MacOSXSystem::MacOSXSystem()
195 fCocoaInitialized(
false)
197 assert(fgInstance == 0 &&
"MacOSXSystem, fgInstance was initialized already");
202 MacOSXSystem::~MacOSXSystem()
204 if (fCocoaInitialized)
205 CloseFileDescriptors();
209 void MacOSXSystem::InitializeCocoa()
211 assert(fCocoaInitialized ==
false &&
"InitializeCocoa, Cocoa was initialized already");
213 [NSApplication sharedApplication];
215 fCocoaInitialized =
true;
219 bool MacOSXSystem::SetFileDescriptors(
const TSeqCollection *fileHandlers)
226 assert(fileHandlers != 0 &&
"SetFileDescriptors, parameter 'fileHandlers' is null");
232 TIter next(fileHandlers);
233 while (
TFileHandler *
const handler = static_cast<TFileHandler *>(next())) {
234 assert(handler->GetFd() != -1 &&
"SetFileDescriptors, invalid file descriptor");
236 if (handler->HasReadInterest())
237 SetFileDescriptor(handler->GetFd(), kDTRead);
239 if (handler->HasWriteInterest())
240 SetFileDescriptor(handler->GetFd(), kDTWrite);
242 }
catch (
const std::exception &) {
243 CloseFileDescriptors();
251 void MacOSXSystem::UnregisterFileDescriptor(CFFileDescriptorRef fd)
256 std::set<CFFileDescriptorRef>::iterator fdIter = fCFFileDescriptors.find(fd);
257 assert(fdIter != fCFFileDescriptors.end() &&
"InvalidateFileDescriptor, file descriptor was not found");
258 fCFFileDescriptors.erase(fdIter);
262 void MacOSXSystem::CloseFileDescriptors()
265 assert(fCocoaInitialized ==
true &&
"CloseFileDescriptors, Cocoa was not initialized");
267 std::set<CFFileDescriptorRef>::iterator fdIter = fCFFileDescriptors.begin(), end = fCFFileDescriptors.end();
269 for (; fdIter != end; ++fdIter) {
270 CFFileDescriptorInvalidate(*fdIter);
274 fCFFileDescriptors.clear();
278 void MacOSXSystem::SetFileDescriptor(
int fd, DescriptorType fdType)
281 assert(fCocoaInitialized ==
true &&
"SetFileDescriptors, Cocoa was not initialized");
283 assert(fd != -1 &&
"SetFileDescriptor, invalid file descriptor");
285 const bool read = fdType == kDTRead;
286 CFFileDescriptorRef fdref = CFFileDescriptorCreate(kCFAllocatorDefault, fd,
false,
290 throw std::runtime_error(
"MacOSXSystem::SetFileDescriptors: CFFileDescriptorCreate failed");
292 CFFileDescriptorEnableCallBacks(fdref, read ? kCFFileDescriptorReadCallBack : kCFFileDescriptorWriteCallBack);
293 CFRunLoopSourceRef runLoopSource = CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, fdref, 0);
295 if (!runLoopSource) {
297 throw std::runtime_error(
"MacOSXSystem::SetFileDescriptors: CFFileDescriptorCreateRunLoopSource failed");
300 CFRunLoopAddSource(CFRunLoopGetMain(), runLoopSource, kCFRunLoopDefaultMode);
301 CFRelease(runLoopSource);
303 fCFFileDescriptors.insert(fdref);
316 : fPimpl(new
Private::MacOSXSystem),
317 fCocoaInitialized(false),
333 if (fFirstDispatch) {
334 if (!fCocoaInitialized && !
gROOT->IsBatch())
337 fFirstDispatch =
false;
340 if (!fCocoaInitialized)
343 Bool_t pollOnce = pendingOnly;
348 if (ProcessPendingEvents()) {
360 if (fNfd > 0 && fFileHandler && fFileHandler->GetSize() > 0)
361 if (CheckDescriptors())
369 if (pendingOnly && !pollOnce)
373 if (fSigcnt > 0 && fSignalHandler->GetSize() > 0)
374 if (CheckSignals(
kTRUE))
375 if (!pendingOnly)
return;
382 if (fTimers && fTimers->GetSize() > 0) {
383 if (DispatchTimers(
kTRUE)) {
385 nextto = NextTimeOut(
kTRUE);
392 nextto = NextTimeOut(
kTRUE);
419 return fCocoaInitialized;
425 if (fCocoaInitialized)
430 fPimpl->InitializeCocoa();
444 [stopper.Get() performSelector : @selector(stopRun) withObject : nil afterDelay : 0.05];
447 fCocoaInitialized =
true;
453 assert(fCocoaInitialized ==
true &&
"ProcessPendingEvents, called while Cocoa was not initialized");
455 bool processed =
false;
456 while (NSEvent *event = [NSApp nextEventMatchingMask : NSAnyEventMask
457 untilDate : nil inMode : NSDefaultRunLoopMode dequeue : YES]) {
458 [NSApp sendEvent : event];
469 assert(fCocoaInitialized ==
true &&
"WaitEvents, called while Cocoa was not initialized");
471 if (fFileHandler && !fPimpl->SetFileDescriptors(fFileHandler)) {
473 Fatal(
"WaitForAllEvents",
"SetFileDesciptors failed");
476 NSDate *untilDate = nil;
478 untilDate = [NSDate dateWithTimeIntervalSinceNow : nextto / 1000.];
480 untilDate = [NSDate distantFuture];
486 NSEvent *
event = [NSApp nextEventMatchingMask : NSAnyEventMask
487 untilDate : untilDate inMode : NSDefaultRunLoopMode dequeue : YES];
489 if (event.type == NSApplicationDefined)
490 ProcessApplicationDefinedEvent(event);
492 [NSApp sendEvent : event];
495 while ((event = [NSApp nextEventMatchingMask : NSAnyEventMask
496 untilDate : nil inMode : NSDefaultRunLoopMode dequeue : YES]))
498 if (event.type == NSApplicationDefined)
499 ProcessApplicationDefinedEvent(event);
501 [NSApp sendEvent : event];
504 fPimpl->CloseFileDescriptors();
514 if (fh->
GetFd() == -1)
515 Error(
"AddFileHandler",
"invalid file descriptor");
536 assert(fCocoaInitialized ==
true &&
537 "ProcessApplicationDefinedEvent, called while Cocoa was not initialized");
539 NSEvent *
event = (NSEvent *)e;
541 "ProcessApplicationDefinedEvent, event parameter is nil");
542 assert(event.type == NSApplicationDefined &&
543 "ProcessApplicationDefinedEvent, event parameter has wrong type");
545 bool descriptorFound =
false;
547 if (fReadmask->IsSet(event.data1)) {
548 fReadready->Set(event.data1);
549 descriptorFound =
true;
552 if (fWritemask->IsSet(event.data1)) {
553 fWriteready->Set(event.data1);
554 descriptorFound =
true;
557 if (!descriptorFound) {
558 Error(
"ProcessApplicationDefinedEvent",
"file descriptor %d was not found",
int(event.data1));
double read(const std::string &file_name)
reading
virtual Bool_t Notify()
Notify when event occurred on descriptor associated with this handler.
bool ProcessPendingEvents()
Namespace for new ROOT classes and functions.
void Fatal(const char *location, const char *msgfmt,...)
TFileHandler * RemoveFileHandler(TFileHandler *fh)
Remove a file handler from the list of file handlers.
bool CocoaInitialized() const
void TMacOSXSystem_WriteCallback(CFFileDescriptorRef fdref, CFOptionFlags, void *)
void TMacOSXSystem_ReadCallback(CFFileDescriptorRef fdref, CFOptionFlags, void *)
void ProcessApplicationDefinedEvent(void *event)
void DispatchOneEvent(Bool_t pendingOnly)
Dispatch a single event.
Sequenceable collection abstract base class.
void DispatchOneEvent(Bool_t pendingOnly=kFALSE)
Dispatch a single event.
TFileHandler * RemoveFileHandler(TFileHandler *fh)
Remove a file handler from the list of file handlers.
R__EXTERN TFileHandler * gXDisplay
void Error(const char *location, const char *msgfmt,...)
void WaitEvents(Long_t nextto)
Binding & operator=(OUT(*fun)(void))
void AddFileHandler(TFileHandler *fh)
Add a file handler to the list of system file handlers.
#define org(otri, vertexptr)
typedef void((*Func_t)())
void AddFileHandler(TFileHandler *fh)
Add a file handler to the list of system file handlers.