1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/evtloop_cf.cpp
3 // Purpose: wxEventLoop implementation common to both Carbon and Cocoa
4 // Author: Vadim Zeitlin
6 // RCS-ID: $Id: wxhead.cpp,v 1.10 2009-06-29 10:23:04 zeitlin Exp $
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/evtloop.h"
28 #if wxUSE_EVENTLOOP_SOURCE
34 #include "wx/evtloopsrc.h"
36 #include "wx/scopedptr.h"
38 #include "wx/osx/private.h"
39 #include "wx/osx/core/cfref.h"
41 // ============================================================================
42 // wxCFEventLoopSource and wxCFEventLoop implementation
43 // ============================================================================
45 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
49 void EnableDescriptorCallBacks(CFFileDescriptorRef cffd
, int flags
)
51 if ( flags
& wxEVENT_SOURCE_INPUT
)
52 CFFileDescriptorEnableCallBacks(cffd
, kCFFileDescriptorReadCallBack
);
53 if ( flags
& wxEVENT_SOURCE_OUTPUT
)
54 CFFileDescriptorEnableCallBacks(cffd
, kCFFileDescriptorWriteCallBack
);
58 wx_cffiledescriptor_callback(CFFileDescriptorRef cffd
,
62 wxLogTrace(wxTRACE_EVT_SOURCE
,
63 "CFFileDescriptor callback, flags=%d", flags
);
65 wxCFEventLoopSource
* const
66 source
= static_cast<wxCFEventLoopSource
*>(ctxData
);
68 wxEventLoopSourceHandler
* const
69 handler
= source
->GetHandler();
70 if ( flags
& kCFFileDescriptorReadCallBack
)
71 handler
->OnReadWaiting();
72 if ( flags
& kCFFileDescriptorWriteCallBack
)
73 handler
->OnWriteWaiting();
75 // we need to re-enable callbacks to be called again
76 EnableDescriptorCallBacks(cffd
, source
->GetFlags());
79 } // anonymous namespace
82 wxCFEventLoop::AddSourceForFD(int fd
,
83 wxEventLoopSourceHandler
*handler
,
86 wxCHECK_MSG( fd
!= -1, NULL
, "can't monitor invalid fd" );
88 wxScopedPtr
<wxCFEventLoopSource
>
89 source(new wxCFEventLoopSource(handler
, flags
));
91 CFFileDescriptorContext ctx
= { 0, source
.get(), NULL
, NULL
, NULL
};
92 wxCFRef
<CFFileDescriptorRef
>
93 cffd(CFFileDescriptorCreate
97 true, // close on invalidate
98 wx_cffiledescriptor_callback
,
104 source
->SetFileDescriptor(cffd
.release());
106 wxCFRef
<CFRunLoopSourceRef
>
107 cfsrc(CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault
, cffd
, 0));
111 CFRunLoopRef cfloop
= CFGetCurrentRunLoop();
112 CFRunLoopAddSource(cfloop
, cfsrc
, kCFRunLoopDefaultMode
);
114 return source
.release();
117 void wxCFEventLoopSource::SetFileDescriptor(CFFileDescriptorRef cffd
)
119 wxASSERT_MSG( !m_cffd
, "shouldn't be called more than once" );
124 wxCFEventLoopSource::~wxCFEventLoopSource()
133 wxCFEventLoop::AddSourceForFD(int WXUNUSED(fd
),
134 wxEventLoopSourceHandler
* WXUNUSED(handler
),
140 #endif // MAC_OS_X_VERSION_MAX_ALLOWED
142 #endif // wxUSE_EVENTLOOP_SOURCE