| 1 | /* ------------------------------------------------------------------------- |
| 2 | * Project: GSocket (Generic Socket) for WX |
| 3 | * Name: gsockpm.c |
| 4 | * Purpose: GSocket: PM part |
| 5 | * CVSID: $Id$ |
| 6 | * ------------------------------------------------------------------------- */ |
| 7 | |
| 8 | #include "wx/setup.h" |
| 9 | |
| 10 | #if wxUSE_SOCKETS |
| 11 | |
| 12 | #include <stdlib.h> |
| 13 | #include "wx/unix/gsockunx.h" |
| 14 | #include "wx/gsocket.h" |
| 15 | |
| 16 | extern int wxAppAddSocketHandler(int handle, int mask, |
| 17 | void (*callback)(void*), void * gsock); |
| 18 | |
| 19 | extern void wxAppRemoveSocketHandler(int handle); |
| 20 | |
| 21 | #define wxSockReadMask 0x01 |
| 22 | #define wxSockWriteMask 0x02 |
| 23 | |
| 24 | static void _GSocket_PM_Input(void *data) |
| 25 | { |
| 26 | GSocket *socket = (GSocket *) data; |
| 27 | _GSocket_Detected_Read(socket); |
| 28 | } |
| 29 | |
| 30 | static void _GSocket_PM_Output(void *data) |
| 31 | { |
| 32 | GSocket *socket = (GSocket *) data; |
| 33 | _GSocket_Detected_Write(socket); |
| 34 | } |
| 35 | |
| 36 | int _GSocket_GUI_Init(GSocket *socket) |
| 37 | { |
| 38 | int *m_id; |
| 39 | socket->m_gui_dependent = (char *)malloc(sizeof(int)*2); |
| 40 | m_id = (int *)(socket->m_gui_dependent); |
| 41 | |
| 42 | m_id[0] = -1; |
| 43 | m_id[1] = -1; |
| 44 | return TRUE; |
| 45 | } |
| 46 | |
| 47 | void _GSocket_GUI_Destroy(GSocket *socket) |
| 48 | { |
| 49 | free(socket->m_gui_dependent); |
| 50 | } |
| 51 | |
| 52 | void _GSocket_Install_Callback(GSocket *socket, GSocketEvent event) |
| 53 | { |
| 54 | int *m_id = (int *)(socket->m_gui_dependent); |
| 55 | int c; |
| 56 | |
| 57 | if (socket->m_fd == -1) |
| 58 | return; |
| 59 | |
| 60 | switch (event) |
| 61 | { |
| 62 | case GSOCK_LOST: /* fall-through */ |
| 63 | case GSOCK_INPUT: c = 0; break; |
| 64 | case GSOCK_OUTPUT: c = 1; break; |
| 65 | case GSOCK_CONNECTION: c = ((socket->m_server) ? 0 : 1); break; |
| 66 | default: return; |
| 67 | } |
| 68 | |
| 69 | if (m_id[c] != -1) |
| 70 | wxAppRemoveSocketHandler(m_id[c]); |
| 71 | |
| 72 | if (c == 0) |
| 73 | { |
| 74 | m_id[0] = wxAppAddSocketHandler(socket->m_fd, wxSockReadMask, |
| 75 | _GSocket_PM_Input, (void *)socket); |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | m_id[1] = wxAppAddSocketHandler(socket->m_fd, wxSockWriteMask, |
| 80 | _GSocket_PM_Output, (void *)socket); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void _GSocket_Uninstall_Callback(GSocket *socket, GSocketEvent event) |
| 85 | { |
| 86 | int *m_id = (int *)(socket->m_gui_dependent); |
| 87 | int c; |
| 88 | switch (event) |
| 89 | { |
| 90 | case GSOCK_LOST: /* fall-through */ |
| 91 | case GSOCK_INPUT: c = 0; break; |
| 92 | case GSOCK_OUTPUT: c = 1; break; |
| 93 | case GSOCK_CONNECTION: c = ((socket->m_server) ? 0 : 1); break; |
| 94 | default: return; |
| 95 | } |
| 96 | if (m_id[c] != -1) |
| 97 | wxAppRemoveSocketHandler(m_id[c]); |
| 98 | |
| 99 | m_id[c] = -1; |
| 100 | } |
| 101 | |
| 102 | void _GSocket_Enable_Events(GSocket *socket) |
| 103 | { |
| 104 | _GSocket_Install_Callback(socket, GSOCK_INPUT); |
| 105 | _GSocket_Install_Callback(socket, GSOCK_OUTPUT); |
| 106 | } |
| 107 | |
| 108 | void _GSocket_Disable_Events(GSocket *socket) |
| 109 | { |
| 110 | _GSocket_Uninstall_Callback(socket, GSOCK_INPUT); |
| 111 | _GSocket_Uninstall_Callback(socket, GSOCK_OUTPUT); |
| 112 | } |
| 113 | |
| 114 | #else /* !wxUSE_SOCKETS */ |
| 115 | |
| 116 | /* some compilers don't like having empty source files */ |
| 117 | static int wxDummyGsockVar = 0; |
| 118 | |
| 119 | #endif /* wxUSE_SOCKETS/!wxUSE_SOCKETS */ |