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