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");
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);
310 namespace Private = ROOT::MacOSX::Detail;
316 : fPimpl(new Private::MacOSXSystem),
317 fCocoaInitialized(
false),
343 Bool_t pollOnce = pendingOnly;
369 if (pendingOnly && !pollOnce)
375 if (!pendingOnly)
return;
430 fPimpl->InitializeCocoa();
444 [stopper.Get() performSelector : @selector(stopRun) withObject : nil afterDelay : 0.05];
455 bool processed =
false;
456 while (NSEvent *event = [NSApp nextEventMatchingMask : NSAnyEventMask
457 untilDate : nil inMode : NSDefaultRunLoopMode dequeue : YES]) {
458 [NSApp sendEvent : event];
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)
492 [NSApp sendEvent : event];
495 while ((event = [NSApp nextEventMatchingMask : NSAnyEventMask
496 untilDate : nil inMode : NSDefaultRunLoopMode dequeue : YES]))
498 if (event.type == NSApplicationDefined)
501 [NSApp sendEvent : event];
504 fPimpl->CloseFileDescriptors();
514 if (fh->
GetFd() == -1)
515 Error(
"AddFileHandler",
"invalid file descriptor");
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;
549 descriptorFound =
true;
554 descriptorFound =
true;
557 if (!descriptorFound) {
558 Error(
"ProcessApplicationDefinedEvent",
"file descriptor %d was not found",
int(event.data1));
virtual Bool_t Notify()
Notify when event occurred on descriptor associated with this handler.
bool ProcessPendingEvents()
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
void Fatal(const char *location, const char *msgfmt,...)
Bool_t DispatchTimers(Bool_t mode)
Handle and dispatch timers.
TSeqCollection * fSignalHandler
TFileHandler * RemoveFileHandler(TFileHandler *fh)
Remove a file handler from the list of file handlers.
ClassImp(TIterator) Bool_t TIterator return false
Compare two iterator objects.
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.
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
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.
std::auto_ptr< ROOT::MacOSX::Detail::MacOSXSystem > fPimpl
Int_t fNfd
Signals that were trapped.
R__EXTERN TFileHandler * gXDisplay
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
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)...
Bool_t CheckDescriptors()
Check if there is activity on some file descriptors and call their Notify() member.
virtual Int_t GetSize() const
TFdSet * fReadready
Files that should be checked for write events.
TFdSet * fSignals
Files with writes waiting.
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)())
bool CocoaInitialized() const
ClassImp(TMacOSXSystem) TMacOSXSystem
void AddFileHandler(TFileHandler *fh)
Add a file handler to the list of system file handlers.
Bool_t CheckSignals(Bool_t sync)
Check if some signals were raised and call their Notify() member.
TFdSet * fWriteready
Files with reads waiting.
TFdSet * fWritemask
Files that should be checked for read events.