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