From 6b44a3355390abef75580416efa5cd3b02fe2ca7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Thu, 14 Dec 2006 14:13:57 +0000 Subject: [PATCH] moved wxX11's wxSocketTable to private wxSocketEventDispatcher class reusable in other ports (backport from SOC2006_SOCKETS branch); use it in wxDFB git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@43976 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- Makefile.in | 52 ++-- build/bakefiles/files.bkl | 3 +- include/wx/private/socketevtdispatch.h | 67 +++++ src/common/socketevtdispatch.cpp | 339 +++++++++++++++++++++++++ src/dfb/evtloop.cpp | 12 +- src/x11/evtloop.cpp | 243 +----------------- src/x11/gsockx11.cpp | 160 ------------ 7 files changed, 462 insertions(+), 414 deletions(-) create mode 100644 include/wx/private/socketevtdispatch.h create mode 100644 src/common/socketevtdispatch.cpp delete mode 100644 src/x11/gsockx11.cpp diff --git a/Makefile.in b/Makefile.in index ca2fb43f47..5ae4ec548c 100644 --- a/Makefile.in +++ b/Makefile.in @@ -3728,6 +3728,7 @@ COND_USE_GUI_1_WXUNIV_1___CORE_SRC_OBJECTS = \ @COND_TOOLKIT_COCOA@ monodll_gsockosx.o monodll_hid.o monodll_utilsexc_cf.o COND_TOOLKIT_DFB___LOWLEVEL_SRC_OBJECTS = \ monodll_fontmgrcmn.o \ + monodll_socketevtdispatch.o \ monodll_caret.o \ monodll_colour.o \ monodll_icon.o \ @@ -3992,6 +3993,7 @@ COND_TOOLKIT_X11___LOWLEVEL_SRC_OBJECTS = \ monodll_fontutil.o \ monodll_utilsx11.o \ monodll_displayx11.o \ + monodll_socketevtdispatch.o \ monodll_icon.o \ monodll_timer.o \ monodll_app.o \ @@ -4008,7 +4010,6 @@ COND_TOOLKIT_X11___LOWLEVEL_SRC_OBJECTS = \ monodll_dcscreen.o \ monodll_evtloop.o \ monodll_font.o \ - monodll_gsockx11.o \ monodll_minifram.o \ monodll_nanox.o \ monodll_palette.o \ @@ -4560,6 +4561,7 @@ COND_TOOLKIT_WINCE___GUI_SRC_OBJECTS = \ @COND_TOOLKIT_COCOA@ monodll_gsockosx.o monodll_hid.o monodll_utilsexc_cf.o COND_TOOLKIT_DFB___LOWLEVEL_SRC_OBJECTS_1 = \ monodll_fontmgrcmn.o \ + monodll_socketevtdispatch.o \ monodll_caret.o \ monodll_colour.o \ monodll_icon.o \ @@ -4824,6 +4826,7 @@ COND_TOOLKIT_X11___LOWLEVEL_SRC_OBJECTS_1 = \ monodll_fontutil.o \ monodll_utilsx11.o \ monodll_displayx11.o \ + monodll_socketevtdispatch.o \ monodll_icon.o \ monodll_timer.o \ monodll_app.o \ @@ -4840,7 +4843,6 @@ COND_TOOLKIT_X11___LOWLEVEL_SRC_OBJECTS_1 = \ monodll_dcscreen.o \ monodll_evtloop.o \ monodll_font.o \ - monodll_gsockx11.o \ monodll_minifram.o \ monodll_nanox.o \ monodll_palette.o \ @@ -5507,6 +5509,7 @@ COND_USE_GUI_1_WXUNIV_1___CORE_SRC_OBJECTS_1 = \ @COND_TOOLKIT_COCOA@ monolib_gsockosx.o monolib_hid.o monolib_utilsexc_cf.o COND_TOOLKIT_DFB___LOWLEVEL_SRC_OBJECTS_2 = \ monolib_fontmgrcmn.o \ + monolib_socketevtdispatch.o \ monolib_caret.o \ monolib_colour.o \ monolib_icon.o \ @@ -5772,6 +5775,7 @@ COND_TOOLKIT_X11___LOWLEVEL_SRC_OBJECTS_2 = \ monolib_fontutil.o \ monolib_utilsx11.o \ monolib_displayx11.o \ + monolib_socketevtdispatch.o \ monolib_icon.o \ monolib_timer.o \ monolib_app.o \ @@ -5788,7 +5792,6 @@ COND_TOOLKIT_X11___LOWLEVEL_SRC_OBJECTS_2 = \ monolib_dcscreen.o \ monolib_evtloop.o \ monolib_font.o \ - monolib_gsockx11.o \ monolib_minifram.o \ monolib_nanox.o \ monolib_palette.o \ @@ -6340,6 +6343,7 @@ COND_TOOLKIT_WINCE___GUI_SRC_OBJECTS_1 = \ @COND_TOOLKIT_COCOA@ monolib_gsockosx.o monolib_hid.o monolib_utilsexc_cf.o COND_TOOLKIT_DFB___LOWLEVEL_SRC_OBJECTS_3 = \ monolib_fontmgrcmn.o \ + monolib_socketevtdispatch.o \ monolib_caret.o \ monolib_colour.o \ monolib_icon.o \ @@ -6605,6 +6609,7 @@ COND_TOOLKIT_X11___LOWLEVEL_SRC_OBJECTS_3 = \ monolib_fontutil.o \ monolib_utilsx11.o \ monolib_displayx11.o \ + monolib_socketevtdispatch.o \ monolib_icon.o \ monolib_timer.o \ monolib_app.o \ @@ -6621,7 +6626,6 @@ COND_TOOLKIT_X11___LOWLEVEL_SRC_OBJECTS_3 = \ monolib_dcscreen.o \ monolib_evtloop.o \ monolib_font.o \ - monolib_gsockx11.o \ monolib_minifram.o \ monolib_nanox.o \ monolib_palette.o \ @@ -7516,6 +7520,7 @@ COND_USE_GUI_1_WXUNIV_1___CORE_SRC_OBJECTS_2 = \ @COND_TOOLKIT_COCOA@ coredll_gsockosx.o coredll_hid.o coredll_utilsexc_cf.o COND_TOOLKIT_DFB___LOWLEVEL_SRC_OBJECTS_4 = \ coredll_fontmgrcmn.o \ + coredll_socketevtdispatch.o \ coredll_caret.o \ coredll_colour.o \ coredll_icon.o \ @@ -7781,6 +7786,7 @@ COND_TOOLKIT_X11___LOWLEVEL_SRC_OBJECTS_4 = \ coredll_fontutil.o \ coredll_utilsx11.o \ coredll_displayx11.o \ + coredll_socketevtdispatch.o \ coredll_icon.o \ coredll_timer.o \ coredll_app.o \ @@ -7797,7 +7803,6 @@ COND_TOOLKIT_X11___LOWLEVEL_SRC_OBJECTS_4 = \ coredll_dcscreen.o \ coredll_evtloop.o \ coredll_font.o \ - coredll_gsockx11.o \ coredll_minifram.o \ coredll_nanox.o \ coredll_palette.o \ @@ -8349,6 +8354,7 @@ COND_TOOLKIT_WINCE___GUI_SRC_OBJECTS_2 = \ @COND_TOOLKIT_COCOA@ coredll_gsockosx.o coredll_hid.o coredll_utilsexc_cf.o COND_TOOLKIT_DFB___LOWLEVEL_SRC_OBJECTS_5 = \ coredll_fontmgrcmn.o \ + coredll_socketevtdispatch.o \ coredll_caret.o \ coredll_colour.o \ coredll_icon.o \ @@ -8614,6 +8620,7 @@ COND_TOOLKIT_X11___LOWLEVEL_SRC_OBJECTS_5 = \ coredll_fontutil.o \ coredll_utilsx11.o \ coredll_displayx11.o \ + coredll_socketevtdispatch.o \ coredll_icon.o \ coredll_timer.o \ coredll_app.o \ @@ -8630,7 +8637,6 @@ COND_TOOLKIT_X11___LOWLEVEL_SRC_OBJECTS_5 = \ coredll_dcscreen.o \ coredll_evtloop.o \ coredll_font.o \ - coredll_gsockx11.o \ coredll_minifram.o \ coredll_nanox.o \ coredll_palette.o \ @@ -8969,6 +8975,7 @@ COND_USE_GUI_1_WXUNIV_1___CORE_SRC_OBJECTS_3 = \ @COND_TOOLKIT_COCOA@ corelib_gsockosx.o corelib_hid.o corelib_utilsexc_cf.o COND_TOOLKIT_DFB___LOWLEVEL_SRC_OBJECTS_6 = \ corelib_fontmgrcmn.o \ + corelib_socketevtdispatch.o \ corelib_caret.o \ corelib_colour.o \ corelib_icon.o \ @@ -9234,6 +9241,7 @@ COND_TOOLKIT_X11___LOWLEVEL_SRC_OBJECTS_6 = \ corelib_fontutil.o \ corelib_utilsx11.o \ corelib_displayx11.o \ + corelib_socketevtdispatch.o \ corelib_icon.o \ corelib_timer.o \ corelib_app.o \ @@ -9250,7 +9258,6 @@ COND_TOOLKIT_X11___LOWLEVEL_SRC_OBJECTS_6 = \ corelib_dcscreen.o \ corelib_evtloop.o \ corelib_font.o \ - corelib_gsockx11.o \ corelib_minifram.o \ corelib_nanox.o \ corelib_palette.o \ @@ -9802,6 +9809,7 @@ COND_TOOLKIT_WINCE___GUI_SRC_OBJECTS_3 = \ @COND_TOOLKIT_COCOA@ corelib_gsockosx.o corelib_hid.o corelib_utilsexc_cf.o COND_TOOLKIT_DFB___LOWLEVEL_SRC_OBJECTS_7 = \ corelib_fontmgrcmn.o \ + corelib_socketevtdispatch.o \ corelib_caret.o \ corelib_colour.o \ corelib_icon.o \ @@ -10067,6 +10075,7 @@ COND_TOOLKIT_X11___LOWLEVEL_SRC_OBJECTS_7 = \ corelib_fontutil.o \ corelib_utilsx11.o \ corelib_displayx11.o \ + corelib_socketevtdispatch.o \ corelib_icon.o \ corelib_timer.o \ corelib_app.o \ @@ -10083,7 +10092,6 @@ COND_TOOLKIT_X11___LOWLEVEL_SRC_OBJECTS_7 = \ corelib_dcscreen.o \ corelib_evtloop.o \ corelib_font.o \ - corelib_gsockx11.o \ corelib_minifram.o \ corelib_nanox.o \ corelib_palette.o \ @@ -14811,8 +14819,11 @@ monodll_sound_sdl.o: $(srcdir)/src/unix/sound_sdl.cpp $(MONODLL_ODEP) @COND_TOOLKIT_COCOA_USE_GUI_1@monodll_utilsexc_cf.o: $(srcdir)/src/mac/corefoundation/utilsexc_cf.cpp $(MONODLL_ODEP) @COND_TOOLKIT_COCOA_USE_GUI_1@ $(CXXC) -c -o $@ $(MONODLL_CXXFLAGS) $(srcdir)/src/mac/corefoundation/utilsexc_cf.cpp -@COND_TOOLKIT_X11_USE_GUI_1@monodll_gsockx11.o: $(srcdir)/src/x11/gsockx11.cpp $(MONODLL_ODEP) -@COND_TOOLKIT_X11_USE_GUI_1@ $(CXXC) -c -o $@ $(MONODLL_CXXFLAGS) $(srcdir)/src/x11/gsockx11.cpp +@COND_TOOLKIT_X11_USE_GUI_1@monodll_socketevtdispatch.o: $(srcdir)/src/common/socketevtdispatch.cpp $(MONODLL_ODEP) +@COND_TOOLKIT_X11_USE_GUI_1@ $(CXXC) -c -o $@ $(MONODLL_CXXFLAGS) $(srcdir)/src/common/socketevtdispatch.cpp + +@COND_TOOLKIT_DFB_USE_GUI_1@monodll_socketevtdispatch.o: $(srcdir)/src/common/socketevtdispatch.cpp $(MONODLL_ODEP) +@COND_TOOLKIT_DFB_USE_GUI_1@ $(CXXC) -c -o $@ $(MONODLL_CXXFLAGS) $(srcdir)/src/common/socketevtdispatch.cpp @COND_TOOLKIT_X11_USE_GUI_1@monodll_nanox.o: $(srcdir)/src/x11/nanox.c $(MONODLL_ODEP) @COND_TOOLKIT_X11_USE_GUI_1@ $(CCC) -c -o $@ $(MONODLL_CFLAGS) $(srcdir)/src/x11/nanox.c @@ -18927,8 +18938,11 @@ monolib_sound_sdl.o: $(srcdir)/src/unix/sound_sdl.cpp $(MONOLIB_ODEP) @COND_TOOLKIT_COCOA_USE_GUI_1@monolib_utilsexc_cf.o: $(srcdir)/src/mac/corefoundation/utilsexc_cf.cpp $(MONOLIB_ODEP) @COND_TOOLKIT_COCOA_USE_GUI_1@ $(CXXC) -c -o $@ $(MONOLIB_CXXFLAGS) $(srcdir)/src/mac/corefoundation/utilsexc_cf.cpp -@COND_TOOLKIT_X11_USE_GUI_1@monolib_gsockx11.o: $(srcdir)/src/x11/gsockx11.cpp $(MONOLIB_ODEP) -@COND_TOOLKIT_X11_USE_GUI_1@ $(CXXC) -c -o $@ $(MONOLIB_CXXFLAGS) $(srcdir)/src/x11/gsockx11.cpp +@COND_TOOLKIT_X11_USE_GUI_1@monolib_socketevtdispatch.o: $(srcdir)/src/common/socketevtdispatch.cpp $(MONOLIB_ODEP) +@COND_TOOLKIT_X11_USE_GUI_1@ $(CXXC) -c -o $@ $(MONOLIB_CXXFLAGS) $(srcdir)/src/common/socketevtdispatch.cpp + +@COND_TOOLKIT_DFB_USE_GUI_1@monolib_socketevtdispatch.o: $(srcdir)/src/common/socketevtdispatch.cpp $(MONOLIB_ODEP) +@COND_TOOLKIT_DFB_USE_GUI_1@ $(CXXC) -c -o $@ $(MONOLIB_CXXFLAGS) $(srcdir)/src/common/socketevtdispatch.cpp @COND_TOOLKIT_X11_USE_GUI_1@monolib_nanox.o: $(srcdir)/src/x11/nanox.c $(MONOLIB_ODEP) @COND_TOOLKIT_X11_USE_GUI_1@ $(CCC) -c -o $@ $(MONOLIB_CFLAGS) $(srcdir)/src/x11/nanox.c @@ -23424,8 +23438,11 @@ coredll_win32.o: $(srcdir)/src/univ/themes/win32.cpp $(COREDLL_ODEP) @COND_TOOLKIT_COCOA_USE_GUI_1@coredll_utilsexc_cf.o: $(srcdir)/src/mac/corefoundation/utilsexc_cf.cpp $(COREDLL_ODEP) @COND_TOOLKIT_COCOA_USE_GUI_1@ $(CXXC) -c -o $@ $(COREDLL_CXXFLAGS) $(srcdir)/src/mac/corefoundation/utilsexc_cf.cpp -@COND_TOOLKIT_X11_USE_GUI_1@coredll_gsockx11.o: $(srcdir)/src/x11/gsockx11.cpp $(COREDLL_ODEP) -@COND_TOOLKIT_X11_USE_GUI_1@ $(CXXC) -c -o $@ $(COREDLL_CXXFLAGS) $(srcdir)/src/x11/gsockx11.cpp +@COND_TOOLKIT_X11_USE_GUI_1@coredll_socketevtdispatch.o: $(srcdir)/src/common/socketevtdispatch.cpp $(COREDLL_ODEP) +@COND_TOOLKIT_X11_USE_GUI_1@ $(CXXC) -c -o $@ $(COREDLL_CXXFLAGS) $(srcdir)/src/common/socketevtdispatch.cpp + +@COND_TOOLKIT_DFB_USE_GUI_1@coredll_socketevtdispatch.o: $(srcdir)/src/common/socketevtdispatch.cpp $(COREDLL_ODEP) +@COND_TOOLKIT_DFB_USE_GUI_1@ $(CXXC) -c -o $@ $(COREDLL_CXXFLAGS) $(srcdir)/src/common/socketevtdispatch.cpp @COND_TOOLKIT_X11_USE_GUI_1@coredll_nanox.o: $(srcdir)/src/x11/nanox.c $(COREDLL_ODEP) @COND_TOOLKIT_X11_USE_GUI_1@ $(CCC) -c -o $@ $(COREDLL_CFLAGS) $(srcdir)/src/x11/nanox.c @@ -26376,8 +26393,11 @@ corelib_win32.o: $(srcdir)/src/univ/themes/win32.cpp $(CORELIB_ODEP) @COND_TOOLKIT_COCOA_USE_GUI_1@corelib_utilsexc_cf.o: $(srcdir)/src/mac/corefoundation/utilsexc_cf.cpp $(CORELIB_ODEP) @COND_TOOLKIT_COCOA_USE_GUI_1@ $(CXXC) -c -o $@ $(CORELIB_CXXFLAGS) $(srcdir)/src/mac/corefoundation/utilsexc_cf.cpp -@COND_TOOLKIT_X11_USE_GUI_1@corelib_gsockx11.o: $(srcdir)/src/x11/gsockx11.cpp $(CORELIB_ODEP) -@COND_TOOLKIT_X11_USE_GUI_1@ $(CXXC) -c -o $@ $(CORELIB_CXXFLAGS) $(srcdir)/src/x11/gsockx11.cpp +@COND_TOOLKIT_X11_USE_GUI_1@corelib_socketevtdispatch.o: $(srcdir)/src/common/socketevtdispatch.cpp $(CORELIB_ODEP) +@COND_TOOLKIT_X11_USE_GUI_1@ $(CXXC) -c -o $@ $(CORELIB_CXXFLAGS) $(srcdir)/src/common/socketevtdispatch.cpp + +@COND_TOOLKIT_DFB_USE_GUI_1@corelib_socketevtdispatch.o: $(srcdir)/src/common/socketevtdispatch.cpp $(CORELIB_ODEP) +@COND_TOOLKIT_DFB_USE_GUI_1@ $(CXXC) -c -o $@ $(CORELIB_CXXFLAGS) $(srcdir)/src/common/socketevtdispatch.cpp @COND_TOOLKIT_X11_USE_GUI_1@corelib_nanox.o: $(srcdir)/src/x11/nanox.c $(CORELIB_ODEP) @COND_TOOLKIT_X11_USE_GUI_1@ $(CCC) -c -o $@ $(CORELIB_CFLAGS) $(srcdir)/src/x11/nanox.c diff --git a/build/bakefiles/files.bkl b/build/bakefiles/files.bkl index 3452667ab1..3614fe12e2 100644 --- a/build/bakefiles/files.bkl +++ b/build/bakefiles/files.bkl @@ -1399,6 +1399,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file! $(XWIN_LOWLEVEL_SRC) + src/common/socketevtdispatch.cpp src/generic/icon.cpp src/generic/timer.cpp src/x11/app.cpp @@ -1415,7 +1416,6 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file! src/x11/dcscreen.cpp src/x11/evtloop.cpp src/x11/font.cpp - src/x11/gsockx11.cpp src/x11/minifram.cpp src/x11/nanox.c src/x11/palette.cpp @@ -1818,6 +1818,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file! src/common/fontmgrcmn.cpp + src/common/socketevtdispatch.cpp src/generic/caret.cpp src/generic/colour.cpp src/generic/icon.cpp diff --git a/include/wx/private/socketevtdispatch.h b/include/wx/private/socketevtdispatch.h new file mode 100644 index 0000000000..a7f39bce8e --- /dev/null +++ b/include/wx/private/socketevtdispatch.h @@ -0,0 +1,67 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: wx/private/socketevtdispatch.h +// Purpose: wxSocketEventDispatcher class +// Authors: Angel Vidal +// Modified by: +// Created: August 2006 +// Copyright: (c) Angel Vidal +// RCS-ID: $Id$ +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +#ifndef _WX_PRIVATE_SOCKETEVTDISPATCH_H_ +#define _WX_PRIVATE_SOCKETEVTDISPATCH_H_ + +#include "wx/defs.h" + +#if wxUSE_SOCKETS + +#include "wx/hash.h" + +// forward declarations +class wxSocketEventDispatcherEntry; +class GSocket; + +enum wxSocketEventDispatcherType +{ + wxSocketEventDispatcherInput, + wxSocketEventDispatcherOutput +}; + +class WXDLLIMPEXP_CORE wxSocketEventDispatcher : public wxHashTable +{ +protected: + wxSocketEventDispatcher() : wxHashTable(wxKEY_INTEGER) {} + +public: + // returns instance of the table + static wxSocketEventDispatcher& Get(); + + virtual ~wxSocketEventDispatcher() + { + WX_CLEAR_HASH_TABLE(*this) + } + + void RegisterCallback(int fd, wxSocketEventDispatcherType socketType, + GSocket* socket); + + void UnregisterCallback(int fd, wxSocketEventDispatcherType socketType); + + void RunLoop(int timeout = 0); + +private: + void AddEvents(fd_set* readset, fd_set* writeset); + + int FillSets(fd_set* readset, fd_set* writeset); + + wxSocketEventDispatcherEntry* FindEntry(int fd); + +private: + static wxSocketEventDispatcher *ms_instance; + + friend class wxSocketEventDispatcherModule; +}; + +#endif // wxUSE_SOCKETS + +#endif // _WX_PRIVATE_SOCKETEVTDISPATCH_H_ diff --git a/src/common/socketevtdispatch.cpp b/src/common/socketevtdispatch.cpp new file mode 100644 index 0000000000..dabd69c644 --- /dev/null +++ b/src/common/socketevtdispatch.cpp @@ -0,0 +1,339 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: src/common/socketevtdispatch.cpp +// Purpose: implements wxSocketEventDispatcher for platforms with no +// socket events notification +// Author: Angel Vidal +// Modified by: +// Created: 08.24.06 +// RCS-ID: $Id$ +// Copyright: (c) 2006 Angel vidal +// License: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +// for compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" + +#if wxUSE_SOCKETS + +#include "wx/private/socketevtdispatch.h" +#include "wx/module.h" +#include "wx/unix/private.h" +#include "wx/gsocket.h" +#include "wx/unix/gsockunx.h" + +#ifndef WX_PRECOMP + #include "wx/hash.h" +#endif + +#include +#include + +#ifdef HAVE_SYS_SELECT_H +# include +#endif + +// ============================================================================ +// implementation +// ============================================================================ + +// ---------------------------------------------------------------------------- +// wxSocketEventDispatcherEntry +// ---------------------------------------------------------------------------- + +class wxSocketEventDispatcherEntry: public wxObject +{ + public: + wxSocketEventDispatcherEntry() + { + m_fdInput = -1; m_fdOutput = -1; + m_socket = NULL; + } + + int m_fdInput; + int m_fdOutput; + GSocket* m_socket; +}; + +// ---------------------------------------------------------------------------- +// wxSocketEventDispatcher +// ---------------------------------------------------------------------------- + +wxSocketEventDispatcher* wxSocketEventDispatcher::ms_instance = NULL; + +/* static */ +wxSocketEventDispatcher& wxSocketEventDispatcher::Get() +{ + if ( !ms_instance ) + ms_instance = new wxSocketEventDispatcher; + return *ms_instance; +} + +wxSocketEventDispatcherEntry* wxSocketEventDispatcher::FindEntry(int fd) +{ + wxSocketEventDispatcherEntry* entry = + (wxSocketEventDispatcherEntry*) wxHashTable::Get(fd); + return entry; +} + +void +wxSocketEventDispatcher::RegisterCallback(int fd, + wxSocketEventDispatcherType socketType, + GSocket* socket) +{ + wxSocketEventDispatcherEntry* entry = FindEntry(fd); + if (!entry) + { + entry = new wxSocketEventDispatcherEntry(); + Put(fd, entry); + } + + if (socketType == wxSocketEventDispatcherInput) + entry->m_fdInput = fd; + else + entry->m_fdOutput = fd; + + entry->m_socket = socket; +} + +void +wxSocketEventDispatcher::UnregisterCallback(int fd, + wxSocketEventDispatcherType socketType) +{ + wxSocketEventDispatcherEntry* entry = FindEntry(fd); + if (entry) + { + if (socketType == wxSocketEventDispatcherInput) + entry->m_fdInput = -1; + else + entry->m_fdOutput = -1; + + if (entry->m_fdInput == -1 && entry->m_fdOutput == -1) + { + entry->m_socket = NULL; + Delete(fd); + delete entry; + } + } +} + +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; +} + +void wxSocketEventDispatcher::AddEvents(fd_set* readset, fd_set* writeset) +{ + BeginFind(); + wxHashTable::compatibility_iterator node = Next(); + while (node) + { + // We have to store the next node here, because the event processing can + // destroy the object before we call Next() + + wxHashTable::compatibility_iterator next_node = Next(); + + wxSocketEventDispatcherEntry* entry = + (wxSocketEventDispatcherEntry*) node->GetData(); + + wxCHECK_RET(entry->m_socket, wxT("Critical: Processing a NULL socket in wxSocketEventDispatcher")); + + if (entry->m_fdInput != -1 && wxFD_ISSET(entry->m_fdInput, readset)) + entry->m_socket->Detected_Read(); + + if (entry->m_fdOutput != -1 && wxFD_ISSET(entry->m_fdOutput, writeset)) + entry->m_socket->Detected_Write();; + + node = next_node; + } +} + +void wxSocketEventDispatcher::RunLoop(int timeout) +{ + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = timeout; + fd_set readset; + fd_set writeset; + + int max_fd = FillSets( &readset, &writeset); + if (select( max_fd+1, &readset, &writeset, NULL, &tv ) == 0) + { + // No socket input/output. Don't add events. + return; + } + else + { + AddEvents(&readset, &writeset); + } +} + +// ---------------------------------------------------------------------------- +// wxSocketEventDispatcherModule +// ---------------------------------------------------------------------------- + +class wxSocketEventDispatcherModule: public wxModule +{ +public: + bool OnInit() { return true; } + void OnExit() { wxDELETE(wxSocketEventDispatcher::ms_instance); } + +private: + DECLARE_DYNAMIC_CLASS(wxSocketEventDispatcherModule) +}; + +IMPLEMENT_DYNAMIC_CLASS(wxSocketEventDispatcherModule, wxModule) + + +// ---------------------------------------------------------------------------- +// GSocket interface +// ---------------------------------------------------------------------------- + +bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop() +{ + return true; +} + +bool GSocketGUIFunctionsTableConcrete::OnInit(void) +{ + return 1; +} + +void GSocketGUIFunctionsTableConcrete::OnExit(void) +{ +} + +bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket *socket) +{ + int *m_id; + + socket->m_gui_dependent = (char *)malloc(sizeof(int)*2); + m_id = (int *)(socket->m_gui_dependent); + + m_id[0] = -1; + m_id[1] = -1; + + return true; +} + +void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket *socket) +{ + free(socket->m_gui_dependent); +} + +void GSocketGUIFunctionsTableConcrete::Install_Callback(GSocket *socket, + GSocketEvent event) +{ + int *m_id = (int *)(socket->m_gui_dependent); + int c; + + if (socket->m_fd == -1) + return; + + switch (event) + { + case GSOCK_LOST: /* fall-through */ + case GSOCK_INPUT: c = 0; break; + case GSOCK_OUTPUT: c = 1; break; + case GSOCK_CONNECTION: c = ((socket->m_server) ? 0 : 1); break; + default: return; + } + +#if 0 + if (m_id[c] != -1) + XtRemoveInput(m_id[c]); +#endif /* 0 */ + + if (c == 0) + { + m_id[0] = socket->m_fd; + + wxSocketEventDispatcher::Get().RegisterCallback( + socket->m_fd, wxSocketEventDispatcherInput, socket); + } + else + { + m_id[1] = socket->m_fd; + + wxSocketEventDispatcher::Get().RegisterCallback( + socket->m_fd, wxSocketEventDispatcherOutput, socket); + } +} + +void GSocketGUIFunctionsTableConcrete::Uninstall_Callback(GSocket *socket, + GSocketEvent event) +{ + int *m_id = (int *)(socket->m_gui_dependent); + int c; + + switch (event) + { + case GSOCK_LOST: /* fall-through */ + case GSOCK_INPUT: c = 0; break; + case GSOCK_OUTPUT: c = 1; break; + case GSOCK_CONNECTION: c = ((socket->m_server) ? 0 : 1); break; + default: return; + } + + if (m_id[c] != -1) + { + if (c == 0) + wxSocketEventDispatcher::Get().UnregisterCallback( + m_id[c], wxSocketEventDispatcherInput); + else + wxSocketEventDispatcher::Get().UnregisterCallback( + m_id[c], wxSocketEventDispatcherOutput); + } + + m_id[c] = -1; +} + +void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket *socket) +{ + Install_Callback(socket, GSOCK_INPUT); + Install_Callback(socket, GSOCK_OUTPUT); +} + +void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket *socket) +{ + Uninstall_Callback(socket, GSOCK_INPUT); + Uninstall_Callback(socket, GSOCK_OUTPUT); +} + +#endif // wxUSE_SOCKETS diff --git a/src/dfb/evtloop.cpp b/src/dfb/evtloop.cpp index 52b940c9f4..30ed0b7259 100644 --- a/src/dfb/evtloop.cpp +++ b/src/dfb/evtloop.cpp @@ -26,6 +26,7 @@ #endif #include "wx/timer.h" +#include "wx/private/socketevtdispatch.h" #include "wx/dfb/private.h" #define TRACE_EVENTS _T("events") @@ -86,7 +87,8 @@ bool wxEventLoop::Dispatch() // time out after a brief period in order to make sure that // OnNextIteration() will be called frequently enough // - // FIXME: call NotifyTimers() from here (and loop) instead? + // FIXME: call NotifyTimers() and wxSocketEventDispatcher::RunLoop() from here + // (and loop) instead? const int TIMEOUT = 100; if ( ms_buffer->WaitForEventWithTimeout(0, TIMEOUT) ) @@ -125,10 +127,16 @@ void wxEventLoop::WakeUp() void wxEventLoop::OnNextIteration() { -#if wxUSE_TIMER // see the comment in Dispatch + +#if wxUSE_TIMER wxTimer::NotifyTimers(); #endif + +#if wxUSE_SOCKETS + // handle any pending socket events: + wxSocketEventDispatcher::Get().RunLoop(); +#endif } diff --git a/src/x11/evtloop.cpp b/src/x11/evtloop.cpp index ce563c8499..ed86512596 100644 --- a/src/x11/evtloop.cpp +++ b/src/x11/evtloop.cpp @@ -30,7 +30,7 @@ #include "wx/module.h" #endif -#include "wx/tooltip.h" +#include "wx/private/socketevtdispatch.h" #include "wx/unix/private.h" #include "wx/x11/private.h" #include "X11/Xlib.h" @@ -46,218 +46,6 @@ # include #endif -#if wxUSE_SOCKETS -// ---------------------------------------------------------------------------- -// wxSocketTable -// ---------------------------------------------------------------------------- - -typedef void (*wxSocketCallback) (int fd, void* data); - -class wxSocketTableEntry: public wxObject -{ - public: - wxSocketTableEntry() - { - m_fdInput = -1; m_fdOutput = -1; - m_callbackInput = NULL; m_callbackOutput = NULL; - m_dataInput = NULL; m_dataOutput = NULL; - } - - int m_fdInput; - int m_fdOutput; - wxSocketCallback m_callbackInput; - wxSocketCallback m_callbackOutput; - void* m_dataInput; - void* m_dataOutput; -}; - -typedef enum -{ wxSocketTableInput, wxSocketTableOutput } wxSocketTableType ; - -class wxSocketTable: public wxHashTable -{ - public: - wxSocketTable(): wxHashTable(wxKEY_INTEGER) - { - } - virtual ~wxSocketTable() - { - WX_CLEAR_HASH_TABLE(*this) - } - - wxSocketTableEntry* FindEntry(int fd); - - void RegisterCallback(int fd, wxSocketTableType socketType, wxSocketCallback callback, void* data); - - void UnregisterCallback(int fd, wxSocketTableType socketType); - - bool CallCallback(int fd, wxSocketTableType socketType); - - void FillSets(fd_set* readset, fd_set* writeset, int* highest); - - void ProcessEvents(fd_set* readset, fd_set* writeset); -}; - -wxSocketTableEntry* wxSocketTable::FindEntry(int fd) -{ - wxSocketTableEntry* entry = (wxSocketTableEntry*) Get(fd); - return entry; -} - -void wxSocketTable::RegisterCallback(int fd, wxSocketTableType socketType, wxSocketCallback callback, void* data) -{ - wxSocketTableEntry* entry = FindEntry(fd); - if (!entry) - { - entry = new wxSocketTableEntry(); - Put(fd, entry); - } - - if (socketType == wxSocketTableInput) - { - entry->m_fdInput = fd; - entry->m_dataInput = data; - entry->m_callbackInput = callback; - } - else - { - entry->m_fdOutput = fd; - entry->m_dataOutput = data; - entry->m_callbackOutput = callback; - } -} - -void wxSocketTable::UnregisterCallback(int fd, wxSocketTableType socketType) -{ - wxSocketTableEntry* entry = FindEntry(fd); - if (entry) - { - if (socketType == wxSocketTableInput) - { - entry->m_fdInput = -1; - entry->m_dataInput = NULL; - entry->m_callbackInput = NULL; - } - else - { - entry->m_fdOutput = -1; - entry->m_dataOutput = NULL; - entry->m_callbackOutput = NULL; - } - if (entry->m_fdInput == -1 && entry->m_fdOutput == -1) - { - Delete(fd); - delete entry; - } - } -} - -bool wxSocketTable::CallCallback(int fd, wxSocketTableType socketType) -{ - wxSocketTableEntry* entry = FindEntry(fd); - if (entry) - { - if (socketType == wxSocketTableInput) - { - if (entry->m_fdInput != -1 && entry->m_callbackInput) - { - (entry->m_callbackInput) (entry->m_fdInput, entry->m_dataInput); - } - } - else - { - if (entry->m_fdOutput != -1 && entry->m_callbackOutput) - { - (entry->m_callbackOutput) (entry->m_fdOutput, entry->m_dataOutput); - } - } - return true; - } - else - return false; -} - -void wxSocketTable::FillSets(fd_set* readset, fd_set* writeset, int* highest) -{ - BeginFind(); - wxHashTable::compatibility_iterator node = Next(); - while (node) - { - wxSocketTableEntry* entry = (wxSocketTableEntry*) node->GetData(); - - if (entry->m_fdInput != -1) - { - wxFD_SET(entry->m_fdInput, readset); - if (entry->m_fdInput > *highest) - * highest = entry->m_fdInput; - } - - if (entry->m_fdOutput != -1) - { - wxFD_SET(entry->m_fdOutput, writeset); - if (entry->m_fdOutput > *highest) - * highest = entry->m_fdOutput; - } - - node = Next(); - } -} - -void wxSocketTable::ProcessEvents(fd_set* readset, fd_set* writeset) -{ - BeginFind(); - wxHashTable::compatibility_iterator node = Next(); - while (node) - { - wxSocketTableEntry* entry = (wxSocketTableEntry*) node->GetData(); - - if (entry->m_fdInput != -1 && wxFD_ISSET(entry->m_fdInput, readset)) - { - (entry->m_callbackInput) (entry->m_fdInput, entry->m_dataInput); - } - - if (entry->m_fdOutput != -1 && wxFD_ISSET(entry->m_fdOutput, writeset)) - { - (entry->m_callbackOutput) (entry->m_fdOutput, entry->m_dataOutput); - } - - node = Next(); - } -} - -wxSocketTable* wxTheSocketTable = NULL; - -class wxSocketTableModule: public wxModule -{ -DECLARE_DYNAMIC_CLASS(wxSocketTableModule) -public: - wxSocketTableModule() {} - bool OnInit() { wxTheSocketTable = new wxSocketTable; return true; }; - void OnExit() { delete wxTheSocketTable; wxTheSocketTable = NULL; }; -}; - -IMPLEMENT_DYNAMIC_CLASS(wxSocketTableModule, wxModule) - -// Implement registration functions as C functions so they -// can be called from gsock11.c - -extern "C" void wxRegisterSocketCallback(int fd, wxSocketTableType socketType, wxSocketCallback callback, void* data) -{ - if (wxTheSocketTable) - { - wxTheSocketTable->RegisterCallback(fd, socketType, callback, data); - } -} - -extern "C" void wxUnregisterSocketCallback(int fd, wxSocketTableType socketType) -{ - if (wxTheSocketTable) - { - wxTheSocketTable->UnregisterCallback(fd, socketType); - } -} -#endif - // ---------------------------------------------------------------------------- // wxEventLoopImpl // ---------------------------------------------------------------------------- @@ -456,35 +244,16 @@ bool wxEventLoop::Dispatch() fd_set readset; fd_set writeset; - int highest = fd; wxFD_ZERO(&readset); wxFD_ZERO(&writeset); - wxFD_SET(fd, &readset); -#if wxUSE_SOCKETS - if (wxTheSocketTable) - wxTheSocketTable->FillSets( &readset, &writeset, &highest ); -#endif - - if (select( highest+1, &readset, &writeset, NULL, &tv ) == 0) + if (select( fd+1, &readset, &writeset, NULL, &tv ) != 0) { - // Timed out, so no event to process - return true; - } - else - { - // An X11 event was pending, so get it + // An X11 event was pending, get it if (wxFD_ISSET( fd, &readset )) XNextEvent( wxGlobalDisplay(), &event ); - -#if wxUSE_SOCKETS - // Check if any socket events were pending, - // and if so, call their callbacks - if (wxTheSocketTable) - wxTheSocketTable->ProcessEvents( &readset, &writeset ); -#endif - } + } #endif } else @@ -492,6 +261,10 @@ bool wxEventLoop::Dispatch() XNextEvent( wxGlobalDisplay(), &event ); } +#if wxUSE_SOCKETS + // handle any pending socket events: + wxSocketEventDispatcher::Get().RunLoop(); +#endif (void) m_impl->ProcessEvent( &event ); return true; diff --git a/src/x11/gsockx11.cpp b/src/x11/gsockx11.cpp deleted file mode 100644 index faf91d0e21..0000000000 --- a/src/x11/gsockx11.cpp +++ /dev/null @@ -1,160 +0,0 @@ -/* ------------------------------------------------------------------------- - * Project: GSocket (Generic Socket) for WX - * Name: src/x11/gsockx11.cpp - * Purpose: GSocket: X11 part - * Licence: The wxWindows licence - * CVSID: $Id$ - * ------------------------------------------------------------------------- */ - -// for compilers that support precompilation, includes "wx.h". -#include "wx/wxprec.h" - -#if defined(__BORLANDC__) -#pragma hdrstop -#endif - -#if wxUSE_SOCKETS - -#include -#include "wx/gsocket.h" -#include "wx/unix/gsockunx.h" - -/* - * FIXME: have these in a common header instead of being repeated - * in evtloop.cpp and gsockx11.c - */ - -typedef void (*wxSocketCallback) (int fd, void* data); - -typedef enum -{ wxSocketTableInput, wxSocketTableOutput } wxSocketTableType ; - -extern "C" void wxRegisterSocketCallback(int fd, wxSocketTableType socketType, wxSocketCallback cback, void* data); -extern "C" void wxUnregisterSocketCallback(int fd, wxSocketTableType socketType); - - -static void _GSocket_X11_Input(int *fid, void* data) -{ - GSocket *socket = (GSocket *)data; - - socket->Detected_Read(); -} - -static void _GSocket_X11_Output(int *fid, void* data) -{ - GSocket *socket = (GSocket *)data; - - socket->Detected_Write(); -} - -bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop() -{ return true; } - -bool GSocketGUIFunctionsTableConcrete::OnInit(void) -{ - return 1; -} - -void GSocketGUIFunctionsTableConcrete::OnExit(void) -{ -} - -bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket *socket) -{ - int *m_id; - - socket->m_gui_dependent = (char *)malloc(sizeof(int)*2); - m_id = (int *)(socket->m_gui_dependent); - - m_id[0] = -1; - m_id[1] = -1; - - return true; -} - -void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket *socket) -{ - free(socket->m_gui_dependent); -} - -void GSocketGUIFunctionsTableConcrete::Install_Callback(GSocket *socket, GSocketEvent event) -{ - int *m_id = (int *)(socket->m_gui_dependent); - int c; - - if (socket->m_fd == -1) - return; - - switch (event) - { - case GSOCK_LOST: /* fall-through */ - case GSOCK_INPUT: c = 0; break; - case GSOCK_OUTPUT: c = 1; break; - case GSOCK_CONNECTION: c = ((socket->m_server) ? 0 : 1); break; - default: return; - } - -#if 0 - if (m_id[c] != -1) - XtRemoveInput(m_id[c]); -#endif /* 0 */ - - if (c == 0) - { - m_id[0] = socket->m_fd; - - wxRegisterSocketCallback(socket->m_fd, wxSocketTableInput, - (wxSocketCallback) _GSocket_X11_Input, (void*) socket); - } - else - { - m_id[1] = socket->m_fd; - - wxRegisterSocketCallback(socket->m_fd, wxSocketTableOutput, - (wxSocketCallback) _GSocket_X11_Output, (void*) socket); - } -} - -void GSocketGUIFunctionsTableConcrete::Uninstall_Callback(GSocket *socket, GSocketEvent event) -{ - int *m_id = (int *)(socket->m_gui_dependent); - int c; - - switch (event) - { - case GSOCK_LOST: /* fall-through */ - case GSOCK_INPUT: c = 0; break; - case GSOCK_OUTPUT: c = 1; break; - case GSOCK_CONNECTION: c = ((socket->m_server) ? 0 : 1); break; - default: return; - } - - if (m_id[c] != -1) - { - if (c == 0) - wxUnregisterSocketCallback(m_id[c], wxSocketTableInput); - else - wxUnregisterSocketCallback(m_id[c], wxSocketTableOutput); - } - - m_id[c] = -1; -} - -void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket *socket) -{ - Install_Callback(socket, GSOCK_INPUT); - Install_Callback(socket, GSOCK_OUTPUT); -} - -void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket *socket) -{ - Uninstall_Callback(socket, GSOCK_INPUT); - Uninstall_Callback(socket, GSOCK_OUTPUT); -} - -#else /* !wxUSE_SOCKETS */ - -/* some compilers don't like having empty source files */ -static int wxDummyGsockVar = 0; - -#endif /* wxUSE_SOCKETS/!wxUSE_SOCKETS */ -- 2.45.2