First cut at socket support in wxX11
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16583 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -3964,11 +3964,6 @@ dnl ------------------------------------------------------------------------
|
|||||||
|
|
||||||
dnl VZ: the GUI hooks wxSocket needs are not implemented yet in wxX11
|
dnl VZ: the GUI hooks wxSocket needs are not implemented yet in wxX11
|
||||||
if test "$wxUSE_SOCKETS" = "yes"; then
|
if test "$wxUSE_SOCKETS" = "yes"; then
|
||||||
if test "$wxUSE_X11" = "1"; then
|
|
||||||
AC_MSG_WARN([wxSocket not yet supported under X11... disabled])
|
|
||||||
wxUSE_SOCKETS="no"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if test "$wxUSE_MGL" = "1"; then
|
if test "$wxUSE_MGL" = "1"; then
|
||||||
AC_MSG_WARN([wxSocket not yet supported under MGL... disabled])
|
AC_MSG_WARN([wxSocket not yet supported under MGL... disabled])
|
||||||
wxUSE_SOCKETS="no"
|
wxUSE_SOCKETS="no"
|
||||||
|
@@ -29,12 +29,224 @@
|
|||||||
#include "wx/thread.h"
|
#include "wx/thread.h"
|
||||||
#endif
|
#endif
|
||||||
#include "wx/timer.h"
|
#include "wx/timer.h"
|
||||||
|
#include "wx/hash.h"
|
||||||
|
#include "wx/module.h"
|
||||||
#include "wx/x11/private.h"
|
#include "wx/x11/private.h"
|
||||||
#include "X11/Xlib.h"
|
#include "X11/Xlib.h"
|
||||||
|
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// 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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
~wxSocketTable()
|
||||||
|
{
|
||||||
|
DeleteContents(TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
wxNode* node = Next();
|
||||||
|
while (node)
|
||||||
|
{
|
||||||
|
wxSocketTableEntry* entry = (wxSocketTableEntry*) node->Data();
|
||||||
|
|
||||||
|
if (entry->m_fdInput != -1)
|
||||||
|
{
|
||||||
|
FD_SET(entry->m_fdInput, readset);
|
||||||
|
if (entry->m_fdInput > *highest)
|
||||||
|
* highest = entry->m_fdInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entry->m_fdOutput != -1)
|
||||||
|
{
|
||||||
|
FD_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();
|
||||||
|
wxNode* node = Next();
|
||||||
|
while (node)
|
||||||
|
{
|
||||||
|
wxSocketTableEntry* entry = (wxSocketTableEntry*) node->Data();
|
||||||
|
|
||||||
|
if (entry->m_fdInput != -1 && FD_ISSET(entry->m_fdInput, readset))
|
||||||
|
{
|
||||||
|
(entry->m_callbackInput) (entry->m_fdInput, entry->m_dataInput);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entry->m_fdOutput != -1 && FD_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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// wxEventLoopImpl
|
// wxEventLoopImpl
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
@@ -226,10 +438,6 @@ bool wxEventLoop::Dispatch()
|
|||||||
|
|
||||||
// TODO allowing for threads, as per e.g. wxMSW
|
// TODO allowing for threads, as per e.g. wxMSW
|
||||||
|
|
||||||
#if 0
|
|
||||||
XNextEvent((Display*) wxGetDisplay(), & event);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// This now waits until either an X event is received,
|
// This now waits until either an X event is received,
|
||||||
// or the select times out. So we should now process
|
// or the select times out. So we should now process
|
||||||
// wxTimers in a reasonably timely fashion. However it
|
// wxTimers in a reasonably timely fashion. However it
|
||||||
@@ -254,17 +462,31 @@ bool wxEventLoop::Dispatch()
|
|||||||
tv.tv_usec=10000; // TODO make this configurable
|
tv.tv_usec=10000; // TODO make this configurable
|
||||||
int fd = ConnectionNumber((Display*) wxGetDisplay());
|
int fd = ConnectionNumber((Display*) wxGetDisplay());
|
||||||
fd_set readset;
|
fd_set readset;
|
||||||
|
fd_set writeset;
|
||||||
|
int highest = fd;
|
||||||
FD_ZERO(&readset);
|
FD_ZERO(&readset);
|
||||||
|
FD_ZERO(&writeset);
|
||||||
|
|
||||||
FD_SET(fd, &readset);
|
FD_SET(fd, &readset);
|
||||||
if (select(fd+1, &readset, NULL, NULL, & tv) == 0)
|
|
||||||
|
if (wxTheSocketTable)
|
||||||
|
wxTheSocketTable->FillSets(& readset, & writeset, & highest);
|
||||||
|
|
||||||
|
if (select(highest+1, &readset, &writeset, NULL, & tv) == 0)
|
||||||
{
|
{
|
||||||
// Timed out, so no event to process
|
// Timed out, so no event to process
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// An event was pending, so get it
|
// An X11 event was pending, so get it
|
||||||
XNextEvent((Display*) wxGetDisplay(), & event);
|
if (FD_ISSET(fd, & readset))
|
||||||
|
XNextEvent((Display*) wxGetDisplay(), & event);
|
||||||
|
|
||||||
|
// Check if any socket events were pending,
|
||||||
|
// and if so, call their callbacks
|
||||||
|
if (wxTheSocketTable)
|
||||||
|
wxTheSocketTable->ProcessEvents(& readset, & writeset);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
} else
|
} else
|
||||||
|
@@ -10,25 +10,31 @@
|
|||||||
#if wxUSE_SOCKETS
|
#if wxUSE_SOCKETS
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
// #include <X11/Intrinsic.h>
|
|
||||||
#include "wx/gsocket.h"
|
#include "wx/gsocket.h"
|
||||||
#include "wx/unix/gsockunx.h"
|
#include "wx/unix/gsockunx.h"
|
||||||
|
|
||||||
// TODO: Raw X11 version
|
/*
|
||||||
#if 0
|
* TODO: have these in a common header instead of being repeated
|
||||||
|
* in evtloop.cpp and gsockx11.c
|
||||||
|
*/
|
||||||
|
|
||||||
extern XtAppContext wxGetAppContext();
|
typedef void (*wxSocketCallback) (int fd, void* data);
|
||||||
|
|
||||||
static void _GSocket_Motif_Input(XtPointer data, int *fid,
|
typedef enum
|
||||||
XtInputId *id)
|
{ wxSocketTableInput, wxSocketTableOutput } wxSocketTableType ;
|
||||||
|
|
||||||
|
void wxRegisterSocketCallback(int fd, wxSocketTableType socketType, wxSocketCallback cback, void* data);
|
||||||
|
void wxUnregisterSocketCallback(int fd, wxSocketTableType socketType);
|
||||||
|
|
||||||
|
|
||||||
|
static void _GSocket_X11_Input(int *fid, void* data)
|
||||||
{
|
{
|
||||||
GSocket *socket = (GSocket *)data;
|
GSocket *socket = (GSocket *)data;
|
||||||
|
|
||||||
_GSocket_Detected_Read(socket);
|
_GSocket_Detected_Read(socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _GSocket_Motif_Output(XtPointer data, int *fid,
|
static void _GSocket_X11_Output(int *fid, void* data)
|
||||||
XtInputId *id)
|
|
||||||
{
|
{
|
||||||
GSocket *socket = (GSocket *)data;
|
GSocket *socket = (GSocket *)data;
|
||||||
|
|
||||||
@@ -70,22 +76,22 @@ void _GSocket_Install_Callback(GSocket *socket, GSocketEvent event)
|
|||||||
default: return;
|
default: return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_id[c] != -1)
|
// if (m_id[c] != -1)
|
||||||
XtRemoveInput(m_id[c]);
|
// XtRemoveInput(m_id[c]);
|
||||||
|
|
||||||
if (c == 0)
|
if (c == 0)
|
||||||
{
|
{
|
||||||
m_id[0] = XtAppAddInput(wxGetAppContext(), socket->m_fd,
|
m_id[0] = socket->m_fd;
|
||||||
(XtPointer *)XtInputReadMask,
|
|
||||||
(XtInputCallbackProc) _GSocket_Motif_Input,
|
wxRegisterSocketCallback(socket->m_fd, wxSocketTableInput,
|
||||||
(XtPointer) socket);
|
(wxSocketCallback) _GSocket_X11_Input, (void*) socket);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
m_id[1] = XtAppAddInput(wxGetAppContext(), socket->m_fd,
|
m_id[1] = socket->m_fd;
|
||||||
(XtPointer *)XtInputWriteMask,
|
|
||||||
(XtInputCallbackProc) _GSocket_Motif_Output,
|
wxRegisterSocketCallback(socket->m_fd, wxSocketTableOutput,
|
||||||
(XtPointer) socket);
|
(wxSocketCallback) _GSocket_X11_Output, (void*) socket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +110,12 @@ void _GSocket_Uninstall_Callback(GSocket *socket, GSocketEvent event)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (m_id[c] != -1)
|
if (m_id[c] != -1)
|
||||||
XtRemoveInput(m_id[c]);
|
{
|
||||||
|
if (c == 0)
|
||||||
|
wxUnregisterSocketCallback(m_id[c], wxSocketTableInput);
|
||||||
|
else
|
||||||
|
wxUnregisterSocketCallback(m_id[c], wxSocketTableOutput);
|
||||||
|
}
|
||||||
|
|
||||||
m_id[c] = -1;
|
m_id[c] = -1;
|
||||||
}
|
}
|
||||||
@@ -126,7 +137,4 @@ void _GSocket_Disable_Events(GSocket *socket)
|
|||||||
/* some compilers don't like having empty source files */
|
/* some compilers don't like having empty source files */
|
||||||
static int wxDummyGsockVar = 0;
|
static int wxDummyGsockVar = 0;
|
||||||
|
|
||||||
#endif
|
|
||||||
// 0
|
|
||||||
|
|
||||||
#endif /* wxUSE_SOCKETS/!wxUSE_SOCKETS */
|
#endif /* wxUSE_SOCKETS/!wxUSE_SOCKETS */
|
||||||
|
Reference in New Issue
Block a user