]> git.saurik.com Git - wxWidgets.git/blob - src/common/selectdispatcher.cpp
Do not allow multiple selection when dragging in the 'value' column
[wxWidgets.git] / src / common / selectdispatcher.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/selectdispatcher.cpp
3 // Purpose: implements dispatcher for select() call
4 // Author: Lukasz Michalski and Vadim Zeitlin
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
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_SELECT_DISPATCHER
27
28 #include "wx/private/selectdispatcher.h"
29 #include "wx/unix/private.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/hash.h"
33 #include "wx/log.h"
34 #include "wx/intl.h"
35 #endif
36
37 #if defined(HAVE_SYS_SELECT_H) || defined(__WATCOMC__)
38 #include <sys/time.h>
39 #include <sys/select.h>
40 #endif
41
42 #include <errno.h>
43
44 #define wxSelectDispatcher_Trace wxT("selectdispatcher")
45
46 // ============================================================================
47 // implementation
48 // ============================================================================
49
50 // ----------------------------------------------------------------------------
51 // wxSelectSets
52 // ----------------------------------------------------------------------------
53
54 int wxSelectSets::ms_flags[wxSelectSets::Max] =
55 {
56 wxFDIO_INPUT,
57 wxFDIO_OUTPUT,
58 wxFDIO_EXCEPTION,
59 };
60
61 const char *wxSelectSets::ms_names[wxSelectSets::Max] =
62 {
63 "input",
64 "output",
65 "exceptional",
66 };
67
68 wxSelectSets::Callback wxSelectSets::ms_handlers[wxSelectSets::Max] =
69 {
70 &wxFDIOHandler::OnReadWaiting,
71 &wxFDIOHandler::OnWriteWaiting,
72 &wxFDIOHandler::OnExceptionWaiting,
73 };
74
75 wxSelectSets::wxSelectSets()
76 {
77 for ( int n = 0; n < Max; n++ )
78 {
79 wxFD_ZERO(&m_fds[n]);
80 }
81 }
82
83 bool wxSelectSets::HasFD(int fd) const
84 {
85 for ( int n = 0; n < Max; n++ )
86 {
87 if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
88 return true;
89 }
90
91 return false;
92 }
93
94 bool wxSelectSets::SetFD(int fd, int flags)
95 {
96 wxCHECK_MSG( fd >= 0, false, wxT("invalid descriptor") );
97
98 for ( int n = 0; n < Max; n++ )
99 {
100 if ( flags & ms_flags[n] )
101 {
102 wxFD_SET(fd, &m_fds[n]);
103 }
104 else if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
105 {
106 wxFD_CLR(fd, &m_fds[n]);
107 }
108 }
109
110 return true;
111 }
112
113 int wxSelectSets::Select(int nfds, struct timeval *tv)
114 {
115 return select(nfds, &m_fds[Read], &m_fds[Write], &m_fds[Except], tv);
116 }
117
118 bool wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const
119 {
120 for ( int n = 0; n < Max; n++ )
121 {
122 if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
123 {
124 wxLogTrace(wxSelectDispatcher_Trace,
125 wxT("Got %s event on fd %d"), ms_names[n], fd);
126 (handler.*ms_handlers[n])();
127 // callback can modify sets and destroy handler
128 // this forces that one event can be processed at one time
129 return true;
130 }
131 }
132
133 return false;
134 }
135
136 // ----------------------------------------------------------------------------
137 // wxSelectDispatcher
138 // ----------------------------------------------------------------------------
139
140 bool wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags)
141 {
142 if ( !wxMappedFDIODispatcher::RegisterFD(fd, handler, flags) )
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
151 wxLogTrace(wxSelectDispatcher_Trace,
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);
153 return true;
154 }
155
156 bool wxSelectDispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags)
157 {
158 if ( !wxMappedFDIODispatcher::ModifyFD(fd, handler, flags) )
159 return false;
160
161 wxASSERT_MSG( fd <= m_maxFD, wxT("logic error: registered fd > m_maxFD?") );
162
163 wxLogTrace(wxSelectDispatcher_Trace,
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);
165 return m_sets.SetFD(fd, flags);
166 }
167
168 bool wxSelectDispatcher::UnregisterFD(int fd)
169 {
170 m_sets.ClearFD(fd);
171
172 if ( !wxMappedFDIODispatcher::UnregisterFD(fd) )
173 return false;
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 )
179 {
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 )
185 {
186 if ( it->first > m_maxFD )
187 {
188 m_maxFD = it->first;
189 }
190 }
191 }
192 }
193
194 wxLogTrace(wxSelectDispatcher_Trace,
195 wxT("Removed fd %d, current max: %d"), fd, m_maxFD);
196 return true;
197 }
198
199 int wxSelectDispatcher::ProcessSets(const wxSelectSets& sets)
200 {
201 int numEvents = 0;
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 {
210 wxFAIL_MSG( wxT("NULL handler in wxSelectDispatcher?") );
211 continue;
212 }
213
214 if ( sets.Handle(fd, *handler) )
215 numEvents++;
216 }
217
218 return numEvents;
219 }
220
221 int wxSelectDispatcher::DoSelect(wxSelectSets& sets, int timeout) const
222 {
223 struct timeval tv,
224 *ptv;
225 if ( timeout != TIMEOUT_INFINITE )
226 {
227 ptv = &tv;
228 tv.tv_sec = 0;
229 tv.tv_usec = timeout*1000;
230 }
231 else // no timeout
232 {
233 ptv = NULL;
234 }
235
236 int ret = sets.Select(m_maxFD + 1, ptv);
237
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
246 bool wxSelectDispatcher::HasPending() const
247 {
248 wxSelectSets sets(m_sets);
249 return DoSelect(sets, 0) > 0;
250 }
251
252 int wxSelectDispatcher::Dispatch(int timeout)
253 {
254 wxSelectSets sets(m_sets);
255 switch ( DoSelect(sets, timeout) )
256 {
257 case -1:
258 wxLogSysError(_("Failed to monitor I/O channels"));
259 return -1;
260
261 case 0:
262 // timeout expired without anything happening
263 return 0;
264
265 default:
266 return ProcessSets(sets);
267 }
268 }
269
270 #endif // wxUSE_SELECT_DISPATCHER