int wxSocketEventDispatcher::FillSets(fd_set* readset, fd_set* writeset) { int max_fd = 0; wxFD_ZERO(readset); wxFD_ZERO(writeset); BeginFind(); wxHashTable::compatibility_iterator node = Next(); while (node) { wxSocketEventDispatcherEntry* entry = (wxSocketEventDispatcherEntry*) node->GetData(); if (entry->m_fdInput != -1) { wxFD_SET(entry->m_fdInput, readset); if (entry->m_fdInput > max_fd) max_fd = entry->m_fdInput; } if (entry->m_fdOutput != -1) { wxFD_SET(entry->m_fdOutput, writeset); if (entry->m_fdOutput > max_fd) max_fd = entry->m_fdOutput; } node = Next(); } return max_fd; }
bool wxSelectSets::SetFD(int fd, int flags) { wxCHECK_MSG( fd >= 0, false, wxT("invalid descriptor") ); #if defined(__INTEL_COMPILER) && 1 /* VDM auto patch */ # pragma ivdep # pragma swp # pragma unroll # pragma prefetch # if 0 # pragma simd noassert # endif #endif /* VDM auto patch */ for ( int n = 0; n < Max; n++ ) { if ( flags & ms_flags[n] ) { wxFD_SET(fd, &m_fds[n]); } else if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) ) { wxFD_CLR(fd, &m_fds[n]); } } return true; }
bool wxSelectSets::SetFD(int fd, int flags) { wxCHECK_MSG( fd >= 0, false, wxT("invalid descriptor") ); for ( int n = 0; n < Max; n++ ) { if ( flags & ms_flags[n] ) { wxFD_SET(fd, &m_fds[n]); } else if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) ) { wxFD_CLR(fd, &m_fds[n]); } } return true; }
bool wxPipeInputStream::CanRead() const { if ( m_lasterror == wxSTREAM_EOF ) return false; // check if there is any input available struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; const int fd = m_file->fd(); fd_set readfds; wxFD_ZERO(&readfds); wxFD_SET(fd, &readfds); switch ( select(fd + 1, &readfds, NULL, NULL, &tv) ) { case -1: wxLogSysError(_("Impossible to get child process input")); // fall through case 0: return false; default: wxFAIL_MSG(wxT("unexpected select() return value")); // still fall through case 1: // input available -- or maybe not, as select() returns 1 when a // read() will complete without delay, but it could still not read // anything return !Eof(); } }
void* wxJoystickThread::Entry() { struct js_event j_evt; fd_set read_fds; struct timeval time_out = {0, 0}; wxFD_ZERO(&read_fds); while (true) { if (TestDestroy()) break; // We use select when either polling or 'blocking' as even in the // blocking case we need to check TestDestroy periodically if (m_polling) time_out.tv_usec = m_polling * 1000; else time_out.tv_usec = 10 * 1000; // check at least every 10 msec in blocking case wxFD_SET(m_device, &read_fds); select(m_device+1, &read_fds, NULL, NULL, &time_out); if (wxFD_ISSET(m_device, &read_fds)) { memset(&j_evt, 0, sizeof(j_evt)); read(m_device, &j_evt, sizeof(j_evt)); //printf("time: %d\t value: %d\t type: %d\t number: %d\n", // j_evt.time, j_evt.value, j_evt.type, j_evt.number); if ((j_evt.type & JS_EVENT_AXIS) && (j_evt.number < wxJS_MAX_AXES)) { // Ignore invalid axis. if ( j_evt.number >= wxJS_MAX_AXES ) { wxLogDebug(wxS("Invalid axis index %d in joystick message."), j_evt.number); continue; } if ( (m_axe[j_evt.number] + m_threshold < j_evt.value) || (m_axe[j_evt.number] - m_threshold > j_evt.value) ) { m_axe[j_evt.number] = j_evt.value; switch (j_evt.number) { case wxJS_AXIS_X: m_lastposition.x = j_evt.value; SendEvent(wxEVT_JOY_MOVE, j_evt.time); break; case wxJS_AXIS_Y: m_lastposition.y = j_evt.value; SendEvent(wxEVT_JOY_MOVE, j_evt.time); break; case wxJS_AXIS_Z: SendEvent(wxEVT_JOY_ZMOVE, j_evt.time); break; default: SendEvent(wxEVT_JOY_MOVE, j_evt.time); // TODO: There should be a way to indicate that the event // is for some other axes. break; } } } if ( (j_evt.type & JS_EVENT_BUTTON) && (j_evt.number < wxJS_MAX_BUTTONS) ) { if (j_evt.value) { m_buttons |= (1 << j_evt.number); SendEvent(wxEVT_JOY_BUTTON_DOWN, j_evt.time, j_evt.number); } else { m_buttons &= ~(1 << j_evt.number); SendEvent(wxEVT_JOY_BUTTON_UP, j_evt.time, j_evt.number); } } } } close(m_device); return NULL; }
LRESULT CALLBACK wxSocket_Internal_WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if ( uMsg < WM_USER || uMsg > (WM_USER + MAXSOCKETS - 1)) return DefWindowProc(hWnd, uMsg, wParam, lParam); wxSocketImplMSW *socket; wxSocketNotify event = (wxSocketNotify)-1; { wxCRIT_SECT_LOCKER(lock, gs_critical); socket = socketList[(uMsg - WM_USER)]; if ( !socket ) return 0; // the socket may be already closed but we could still receive // notifications for it sent (asynchronously) before it got closed if ( socket->m_fd == INVALID_SOCKET ) return 0; wxASSERT_MSG( socket->m_fd == (SOCKET)wParam, "mismatch between message and socket?" ); switch ( WSAGETSELECTEVENT(lParam) ) { case FD_READ: // We may get a FD_READ notification even when there is no data // to read on the socket, in particular this happens on socket // creation when we seem to always get FD_CONNECT, FD_WRITE and // FD_READ notifications all at once (but it doesn't happen // only then). Ignore such dummy notifications. { fd_set fds; timeval tv = { 0, 0 }; wxFD_ZERO(&fds); wxFD_SET(socket->m_fd, &fds); if ( select(socket->m_fd + 1, &fds, NULL, NULL, &tv) != 1 ) return 0; } event = wxSOCKET_INPUT; break; case FD_WRITE: event = wxSOCKET_OUTPUT; break; case FD_ACCEPT: event = wxSOCKET_CONNECTION; break; case FD_CONNECT: event = WSAGETSELECTERROR(lParam) ? wxSOCKET_LOST : wxSOCKET_CONNECTION; break; case FD_CLOSE: event = wxSOCKET_LOST; break; default: wxFAIL_MSG( "unexpected socket notification" ); return 0; } } // unlock gs_critical socket->NotifyOnStateChange(event); return 0; }
void* wxJoystickThread::Entry() { struct js_event j_evt; fd_set read_fds; struct timeval time_out = {0, 0}; wxFD_ZERO(&read_fds); while (true) { if (TestDestroy()) break; // We use select when either polling or 'blocking' as even in the // blocking case we need to check TestDestroy periodically if (m_polling) time_out.tv_usec = m_polling * 1000; else time_out.tv_usec = 10 * 1000; // check at least every 10 msec in blocking case wxFD_SET(m_device, &read_fds); select(m_device+1, &read_fds, NULL, NULL, &time_out); if (wxFD_ISSET(m_device, &read_fds)) { memset(&j_evt, 0, sizeof(j_evt)); read(m_device, &j_evt, sizeof(j_evt)); //printf("time: %d\t value: %d\t type: %d\t number: %d\n", // j_evt.time, j_evt.value, j_evt.type, j_evt.number); wxJoystickEvent jwx_event; if (j_evt.type & JS_EVENT_AXIS) { m_axe[j_evt.number] = j_evt.value; switch (j_evt.number) { case wxJS_AXIS_X: m_lastposition.x = j_evt.value; jwx_event.SetEventType(wxEVT_JOY_MOVE); break; case wxJS_AXIS_Y: m_lastposition.y = j_evt.value; jwx_event.SetEventType(wxEVT_JOY_MOVE); break; case wxJS_AXIS_Z: jwx_event.SetEventType(wxEVT_JOY_ZMOVE); break; default: jwx_event.SetEventType(wxEVT_JOY_MOVE); // TODO: There should be a way to indicate that the event // is for some other axes. break; } } if (j_evt.type & JS_EVENT_BUTTON) { if (j_evt.value) { m_buttons |= (1 << j_evt.number); jwx_event.SetEventType(wxEVT_JOY_BUTTON_DOWN); } else { m_buttons &= ~(1 << j_evt.number); jwx_event.SetEventType(wxEVT_JOY_BUTTON_UP); } jwx_event.SetButtonChange(j_evt.number); jwx_event.SetTimestamp(j_evt.time); jwx_event.SetJoystick(m_joystick); jwx_event.SetButtonState(m_buttons); jwx_event.SetPosition(m_lastposition); jwx_event.SetZPosition(m_axe[3]); jwx_event.SetEventObject(m_catchwin); if (m_catchwin) m_catchwin->AddPendingEvent(jwx_event); } } } close(m_device); return NULL; }