]> git.saurik.com Git - wxWidgets.git/blame - src/common/selectdispatcher.cpp
compilation fix after HandleWindowEvent() changes
[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
a1873279
VZ
22#if wxUSE_SELECT_DISPATCHER
23
30c45bdd 24#include "wx/private/selectdispatcher.h"
30c45bdd 25#include "wx/unix/private.h"
30c45bdd
VZ
26
27#ifndef WX_PRECOMP
28 #include "wx/hash.h"
b5ef33b2
VS
29 #include "wx/log.h"
30 #include "wx/intl.h"
30c45bdd
VZ
31#endif
32
7f6b7a5a 33#if defined(HAVE_SYS_SELECT_H) || defined(__WATCOMC__)
47f63c5b 34 #include <sys/time.h>
b46b1d59 35 #include <sys/select.h>
30c45bdd
VZ
36#endif
37
b46b1d59
VZ
38#include <errno.h>
39
30c45bdd
VZ
40#define wxSelectDispatcher_Trace wxT("selectdispatcher")
41
42// ============================================================================
43// implementation
44// ============================================================================
45
46// ----------------------------------------------------------------------------
b46b1d59 47// wxSelectSets
30c45bdd
VZ
48// ----------------------------------------------------------------------------
49
b46b1d59
VZ
50int wxSelectSets::ms_flags[wxSelectSets::Max] =
51{
52 wxFDIO_INPUT,
53 wxFDIO_OUTPUT,
54 wxFDIO_EXCEPTION,
55};
30c45bdd 56
b46b1d59 57const char *wxSelectSets::ms_names[wxSelectSets::Max] =
30c45bdd 58{
b46b1d59
VZ
59 "input",
60 "output",
61 "exceptional",
62};
30c45bdd 63
b46b1d59 64wxSelectSets::Callback wxSelectSets::ms_handlers[wxSelectSets::Max] =
30c45bdd 65{
b46b1d59
VZ
66 &wxFDIOHandler::OnReadWaiting,
67 &wxFDIOHandler::OnWriteWaiting,
68 &wxFDIOHandler::OnExceptionWaiting,
69};
30c45bdd 70
b46b1d59
VZ
71wxSelectSets::wxSelectSets()
72{
73 for ( int n = 0; n < Max; n++ )
30c45bdd 74 {
b46b1d59 75 wxFD_ZERO(&m_fds[n]);
30c45bdd 76 }
b46b1d59 77}
30c45bdd 78
b46b1d59
VZ
79bool wxSelectSets::HasFD(int fd) const
80{
81 for ( int n = 0; n < Max; n++ )
30c45bdd 82 {
fa15eac7 83 if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
b46b1d59
VZ
84 return true;
85 }
30c45bdd 86
b46b1d59 87 return false;
30c45bdd
VZ
88}
89
b46b1d59 90bool wxSelectSets::SetFD(int fd, int flags)
30c45bdd 91{
b46b1d59 92 wxCHECK_MSG( fd >= 0, false, _T("invalid descriptor") );
30c45bdd 93
b46b1d59 94 for ( int n = 0; n < Max; n++ )
30c45bdd 95 {
b46b1d59
VZ
96 if ( flags & ms_flags[n] )
97 {
98 wxFD_SET(fd, &m_fds[n]);
985acf87
VZ
99 wxLogTrace(wxSelectDispatcher_Trace,
100 _T("Registered fd %d for %s events"), fd, ms_names[n]);
b46b1d59 101 }
fa15eac7 102 else if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
b46b1d59
VZ
103 {
104 wxFD_CLR(fd, &m_fds[n]);
985acf87
VZ
105 wxLogTrace(wxSelectDispatcher_Trace,
106 _T("Unregistered fd %d from %s events"), fd, ms_names[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
b46b1d59
VZ
118void wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const
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
VZ
124 wxLogTrace(wxSelectDispatcher_Trace,
125 _T("Got %s event on fd %d"), ms_names[n], fd);
126 (handler.*ms_handlers[n])();
127 }
128 }
30c45bdd
VZ
129}
130
b46b1d59
VZ
131// ----------------------------------------------------------------------------
132// wxSelectDispatcher
133// ----------------------------------------------------------------------------
134
b46b1d59 135/* static */
5e1eac14 136wxSelectDispatcher *wxSelectDispatcher::Create()
b46b1d59 137{
5e1eac14 138 return new wxSelectDispatcher;
b46b1d59 139}
30c45bdd 140
b46b1d59
VZ
141wxSelectDispatcher::wxSelectDispatcher()
142{
143 m_maxFD = -1;
144}
145
146bool wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags)
147{
ad8d42f8 148 if ( !wxMappedFDIODispatcher::RegisterFD(fd, handler, flags) )
b46b1d59
VZ
149 return false;
150
151 if ( !m_sets.SetFD(fd, flags) )
152 return false;
153
154 if ( fd > m_maxFD )
155 m_maxFD = fd;
156
157 return true;
158}
159
160bool wxSelectDispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags)
161{
ad8d42f8 162 if ( !wxMappedFDIODispatcher::ModifyFD(fd, handler, flags) )
b46b1d59
VZ
163 return false;
164
165 wxASSERT_MSG( fd <= m_maxFD, _T("logic error: registered fd > m_maxFD?") );
166
167 return m_sets.SetFD(fd, flags);
168}
169
af57c51a 170bool wxSelectDispatcher::UnregisterFD(int fd)
b46b1d59 171{
af57c51a
VZ
172 m_sets.ClearFD(fd);
173
174 if ( !wxMappedFDIODispatcher::UnregisterFD(fd) )
175 return false;
b46b1d59
VZ
176
177 // remove the handler if we don't need it any more
178 if ( !m_sets.HasFD(fd) )
179 {
180 if ( fd == m_maxFD )
30c45bdd 181 {
b46b1d59
VZ
182 // need to find new max fd
183 m_maxFD = -1;
184 for ( wxFDIOHandlerMap::const_iterator it = m_handlers.begin();
185 it != m_handlers.end();
186 ++it )
30c45bdd 187 {
b46b1d59
VZ
188 if ( it->first > m_maxFD )
189 m_maxFD = it->first;
190 }
191 }
192 }
193
ad8d42f8 194 return true;
30c45bdd
VZ
195}
196
b46b1d59 197void wxSelectDispatcher::ProcessSets(const wxSelectSets& sets)
30c45bdd 198{
b46b1d59
VZ
199 for ( int fd = 0; fd <= m_maxFD; fd++ )
200 {
201 if ( !sets.HasFD(fd) )
202 continue;
203
204 wxFDIOHandler * const handler = FindHandler(fd);
205 if ( !handler )
206 {
207 wxFAIL_MSG( _T("NULL handler in wxSelectDispatcher?") );
208 continue;
209 }
210
211 sets.Handle(fd, *handler);
212 }
213}
30c45bdd 214
7523de90 215void wxSelectDispatcher::Dispatch(int timeout)
30c45bdd 216{
b46b1d59 217 struct timeval tv,
7523de90 218 *ptv;
b46b1d59 219 if ( timeout != TIMEOUT_INFINITE )
30c45bdd
VZ
220 {
221 ptv = &tv;
222 tv.tv_sec = 0;
b46b1d59
VZ
223 tv.tv_usec = timeout*1000;
224 }
7523de90 225 else // no timeout
30c45bdd 226 {
7523de90
VZ
227 ptv = NULL;
228 }
b46b1d59 229
7523de90 230 wxSelectSets sets = m_sets;
b46b1d59 231
7523de90
VZ
232 const int ret = sets.Select(m_maxFD + 1, ptv);
233 switch ( ret )
234 {
235 case -1:
236 if ( errno != EINTR )
237 {
238 wxLogSysError(_("Failed to monitor I/O channels"));
239 }
240 break;
b46b1d59 241
7523de90
VZ
242 case 0:
243 // timeout expired without anything happening
244 break;
b46b1d59 245
7523de90
VZ
246 default:
247 ProcessSets(sets);
b46b1d59 248 }
30c45bdd
VZ
249}
250
a1873279 251#endif // wxUSE_SELECT_DISPATCHER