]> git.saurik.com Git - wxWidgets.git/blame - src/common/selectdispatcher.cpp
storing modal returncode correctly for carbon
[wxWidgets.git] / src / common / selectdispatcher.cpp
CommitLineData
30c45bdd 1///////////////////////////////////////////////////////////////////////////////
b46b1d59 2// Name: src/common/selectdispatcher.cpp
30c45bdd 3// Purpose: implements dispatcher for select() call
b46b1d59 4// Author: Lukasz Michalski and Vadim Zeitlin
30c45bdd
VZ
5// Created: December 2006
6// RCS-ID: $Id$
7// Copyright: (c) 2006 Lukasz Michalski
8// License: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// for compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
b4715d08
VZ
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
a1873279
VZ
26#if wxUSE_SELECT_DISPATCHER
27
30c45bdd 28#include "wx/private/selectdispatcher.h"
30c45bdd 29#include "wx/unix/private.h"
30c45bdd
VZ
30
31#ifndef WX_PRECOMP
32 #include "wx/hash.h"
b5ef33b2
VS
33 #include "wx/log.h"
34 #include "wx/intl.h"
30c45bdd
VZ
35#endif
36
7f6b7a5a 37#if defined(HAVE_SYS_SELECT_H) || defined(__WATCOMC__)
47f63c5b 38 #include <sys/time.h>
b46b1d59 39 #include <sys/select.h>
30c45bdd
VZ
40#endif
41
b46b1d59
VZ
42#include <errno.h>
43
30c45bdd
VZ
44#define wxSelectDispatcher_Trace wxT("selectdispatcher")
45
46// ============================================================================
47// implementation
48// ============================================================================
49
50// ----------------------------------------------------------------------------
b46b1d59 51// wxSelectSets
30c45bdd
VZ
52// ----------------------------------------------------------------------------
53
b46b1d59
VZ
54int wxSelectSets::ms_flags[wxSelectSets::Max] =
55{
56 wxFDIO_INPUT,
57 wxFDIO_OUTPUT,
58 wxFDIO_EXCEPTION,
59};
30c45bdd 60
b46b1d59 61const char *wxSelectSets::ms_names[wxSelectSets::Max] =
30c45bdd 62{
b46b1d59
VZ
63 "input",
64 "output",
65 "exceptional",
66};
30c45bdd 67
b46b1d59 68wxSelectSets::Callback wxSelectSets::ms_handlers[wxSelectSets::Max] =
30c45bdd 69{
b46b1d59
VZ
70 &wxFDIOHandler::OnReadWaiting,
71 &wxFDIOHandler::OnWriteWaiting,
72 &wxFDIOHandler::OnExceptionWaiting,
73};
30c45bdd 74
b46b1d59
VZ
75wxSelectSets::wxSelectSets()
76{
77 for ( int n = 0; n < Max; n++ )
30c45bdd 78 {
b46b1d59 79 wxFD_ZERO(&m_fds[n]);
30c45bdd 80 }
b46b1d59 81}
30c45bdd 82
b46b1d59
VZ
83bool wxSelectSets::HasFD(int fd) const
84{
85 for ( int n = 0; n < Max; n++ )
30c45bdd 86 {
fa15eac7 87 if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
b46b1d59
VZ
88 return true;
89 }
30c45bdd 90
b46b1d59 91 return false;
30c45bdd
VZ
92}
93
b46b1d59 94bool wxSelectSets::SetFD(int fd, int flags)
30c45bdd 95{
9a83f860 96 wxCHECK_MSG( fd >= 0, false, wxT("invalid descriptor") );
30c45bdd 97
b46b1d59 98 for ( int n = 0; n < Max; n++ )
30c45bdd 99 {
b46b1d59
VZ
100 if ( flags & ms_flags[n] )
101 {
102 wxFD_SET(fd, &m_fds[n]);
b46b1d59 103 }
fa15eac7 104 else if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
b46b1d59
VZ
105 {
106 wxFD_CLR(fd, &m_fds[n]);
b46b1d59 107 }
30c45bdd
VZ
108 }
109
b46b1d59
VZ
110 return true;
111}
30c45bdd 112
b46b1d59
VZ
113int wxSelectSets::Select(int nfds, struct timeval *tv)
114{
115 return select(nfds, &m_fds[Read], &m_fds[Write], &m_fds[Except], tv);
116}
30c45bdd 117
a12698ab 118bool wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const
b46b1d59
VZ
119{
120 for ( int n = 0; n < Max; n++ )
30c45bdd 121 {
fa15eac7 122 if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
30c45bdd 123 {
b46b1d59 124 wxLogTrace(wxSelectDispatcher_Trace,
9a83f860 125 wxT("Got %s event on fd %d"), ms_names[n], fd);
b46b1d59 126 (handler.*ms_handlers[n])();
2804f77d
VZ
127 // callback can modify sets and destroy handler
128 // this forces that one event can be processed at one time
a12698ab 129 return true;
b46b1d59
VZ
130 }
131 }
a12698ab
VZ
132
133 return false;
30c45bdd
VZ
134}
135
b46b1d59
VZ
136// ----------------------------------------------------------------------------
137// wxSelectDispatcher
138// ----------------------------------------------------------------------------
139
b46b1d59
VZ
140bool wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags)
141{
ad8d42f8 142 if ( !wxMappedFDIODispatcher::RegisterFD(fd, handler, flags) )
b46b1d59
VZ
143 return false;
144
145 if ( !m_sets.SetFD(fd, flags) )
146 return false;
147
148 if ( fd > m_maxFD )
149 m_maxFD = fd;
150
2804f77d 151 wxLogTrace(wxSelectDispatcher_Trace,
9a83f860 152 wxT("Registered fd %d: input:%d, output:%d, exceptional:%d"), fd, (flags & wxFDIO_INPUT) == wxFDIO_INPUT, (flags & wxFDIO_OUTPUT), (flags & wxFDIO_EXCEPTION) == wxFDIO_EXCEPTION);
b46b1d59
VZ
153 return true;
154}
155
156bool wxSelectDispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags)
157{
ad8d42f8 158 if ( !wxMappedFDIODispatcher::ModifyFD(fd, handler, flags) )
b46b1d59
VZ
159 return false;
160
9a83f860 161 wxASSERT_MSG( fd <= m_maxFD, wxT("logic error: registered fd > m_maxFD?") );
b46b1d59 162
2804f77d 163 wxLogTrace(wxSelectDispatcher_Trace,
9a83f860 164 wxT("Modified fd %d: input:%d, output:%d, exceptional:%d"), fd, (flags & wxFDIO_INPUT) == wxFDIO_INPUT, (flags & wxFDIO_OUTPUT) == wxFDIO_OUTPUT, (flags & wxFDIO_EXCEPTION) == wxFDIO_EXCEPTION);
b46b1d59
VZ
165 return m_sets.SetFD(fd, flags);
166}
167
af57c51a 168bool wxSelectDispatcher::UnregisterFD(int fd)
b46b1d59 169{
af57c51a
VZ
170 m_sets.ClearFD(fd);
171
172 if ( !wxMappedFDIODispatcher::UnregisterFD(fd) )
173 return false;
b46b1d59
VZ
174
175 // remove the handler if we don't need it any more
176 if ( !m_sets.HasFD(fd) )
177 {
178 if ( fd == m_maxFD )
30c45bdd 179 {
b46b1d59
VZ
180 // need to find new max fd
181 m_maxFD = -1;
182 for ( wxFDIOHandlerMap::const_iterator it = m_handlers.begin();
183 it != m_handlers.end();
184 ++it )
30c45bdd 185 {
b46b1d59 186 if ( it->first > m_maxFD )
2804f77d 187 {
b46b1d59 188 m_maxFD = it->first;
2804f77d 189 }
b46b1d59
VZ
190 }
191 }
192 }
193
2804f77d 194 wxLogTrace(wxSelectDispatcher_Trace,
9a83f860 195 wxT("Removed fd %d, current max: %d"), fd, m_maxFD);
ad8d42f8 196 return true;
30c45bdd
VZ
197}
198
a12698ab 199int wxSelectDispatcher::ProcessSets(const wxSelectSets& sets)
30c45bdd 200{
a12698ab 201 int numEvents = 0;
b46b1d59
VZ
202 for ( int fd = 0; fd <= m_maxFD; fd++ )
203 {
204 if ( !sets.HasFD(fd) )
205 continue;
206
207 wxFDIOHandler * const handler = FindHandler(fd);
208 if ( !handler )
209 {
9a83f860 210 wxFAIL_MSG( wxT("NULL handler in wxSelectDispatcher?") );
b46b1d59
VZ
211 continue;
212 }
213
a12698ab
VZ
214 if ( sets.Handle(fd, *handler) )
215 numEvents++;
b46b1d59 216 }
5a557d1e 217
a12698ab 218 return numEvents;
b46b1d59 219}
30c45bdd 220
a12698ab 221int wxSelectDispatcher::DoSelect(wxSelectSets& sets, int timeout) const
30c45bdd 222{
b46b1d59 223 struct timeval tv,
7523de90 224 *ptv;
b46b1d59 225 if ( timeout != TIMEOUT_INFINITE )
30c45bdd
VZ
226 {
227 ptv = &tv;
228 tv.tv_sec = 0;
b46b1d59
VZ
229 tv.tv_usec = timeout*1000;
230 }
7523de90 231 else // no timeout
30c45bdd 232 {
7523de90
VZ
233 ptv = NULL;
234 }
b46b1d59 235
a12698ab 236 int ret = sets.Select(m_maxFD + 1, ptv);
b46b1d59 237
a12698ab
VZ
238 // TODO: we need to restart select() in this case but for now just return
239 // as if timeout expired
240 if ( ret == -1 && errno == EINTR )
241 ret = 0;
242
243 return ret;
244}
245
246bool wxSelectDispatcher::HasPending() const
247{
248 wxSelectSets sets(m_sets);
249 return DoSelect(sets, 0) > 0;
250}
251
252int wxSelectDispatcher::Dispatch(int timeout)
253{
254 wxSelectSets sets(m_sets);
255 switch ( DoSelect(sets, timeout) )
7523de90
VZ
256 {
257 case -1:
a12698ab
VZ
258 wxLogSysError(_("Failed to monitor I/O channels"));
259 return -1;
b46b1d59 260
7523de90
VZ
261 case 0:
262 // timeout expired without anything happening
a12698ab 263 return 0;
b46b1d59 264
7523de90 265 default:
a12698ab 266 return ProcessSets(sets);
b46b1d59 267 }
30c45bdd
VZ
268}
269
a1873279 270#endif // wxUSE_SELECT_DISPATCHER