| 1 | /* ------------------------------------------------------------------------- |
| 2 | * Project: GSocket (Generic Socket) for WX |
| 3 | * Name: gsockgtk.c |
| 4 | * Purpose: GSocket: GTK part |
| 5 | * CVSID: $Id$ |
| 6 | * ------------------------------------------------------------------------- |
| 7 | */ |
| 8 | #include "wx/setup.h" |
| 9 | |
| 10 | #if wxUSE_SOCKETS |
| 11 | |
| 12 | #include <stdlib.h> |
| 13 | #include <stdio.h> |
| 14 | #include "gdk/gdk.h" |
| 15 | #include "glib.h" |
| 16 | #include "wx/gsocket.h" |
| 17 | #include "wx/unix/gsockunx.h" |
| 18 | |
| 19 | |
| 20 | void _GSocket_GDK_Input(gpointer data, |
| 21 | gint source, |
| 22 | GdkInputCondition condition) |
| 23 | { |
| 24 | GSocket *socket = (GSocket *)data; |
| 25 | |
| 26 | if (condition & GDK_INPUT_READ) |
| 27 | _GSocket_Detected_Read(socket); |
| 28 | if (condition & GDK_INPUT_WRITE) |
| 29 | _GSocket_Detected_Write(socket); |
| 30 | } |
| 31 | |
| 32 | void _GSocket_GUI_Init(GSocket *socket) |
| 33 | { |
| 34 | int i; |
| 35 | gint *m_id; |
| 36 | |
| 37 | socket->m_gui_dependent = (char *)malloc(sizeof(gint)*2); |
| 38 | m_id = (gint *)(socket->m_gui_dependent); |
| 39 | |
| 40 | m_id[0] = -1; |
| 41 | m_id[1] = -1; |
| 42 | } |
| 43 | |
| 44 | void _GSocket_GUI_Destroy(GSocket *socket) |
| 45 | { |
| 46 | free(socket->m_gui_dependent); |
| 47 | } |
| 48 | |
| 49 | void _GSocket_Install_Callback(GSocket *socket, GSocketEvent event) |
| 50 | { |
| 51 | gint *m_id = (gint *)(socket->m_gui_dependent); |
| 52 | int c; |
| 53 | |
| 54 | if (socket->m_fd == -1) |
| 55 | return; |
| 56 | |
| 57 | switch (event) |
| 58 | { |
| 59 | case GSOCK_LOST: /* fall-through */ |
| 60 | case GSOCK_INPUT: c = 0; break; |
| 61 | case GSOCK_OUTPUT: c = 1; break; |
| 62 | case GSOCK_CONNECTION: c = ((socket->m_server) ? 0 : 1); break; |
| 63 | default: return; |
| 64 | } |
| 65 | |
| 66 | if (m_id[c] != -1) |
| 67 | gdk_input_remove(m_id[c]); |
| 68 | |
| 69 | m_id[c] = gdk_input_add(socket->m_fd, |
| 70 | (c ? GDK_INPUT_WRITE : GDK_INPUT_READ), |
| 71 | _GSocket_GDK_Input, |
| 72 | (gpointer)socket); |
| 73 | } |
| 74 | |
| 75 | void _GSocket_Uninstall_Callback(GSocket *socket, GSocketEvent event) |
| 76 | { |
| 77 | gint *m_id = (gint *)(socket->m_gui_dependent); |
| 78 | int c; |
| 79 | |
| 80 | switch (event) |
| 81 | { |
| 82 | case GSOCK_LOST: /* fall-through */ |
| 83 | case GSOCK_INPUT: c = 0; break; |
| 84 | case GSOCK_OUTPUT: c = 1; break; |
| 85 | case GSOCK_CONNECTION: c = ((socket->m_server) ? 0 : 1); break; |
| 86 | default: return; |
| 87 | } |
| 88 | |
| 89 | if (m_id[c] != -1) |
| 90 | gdk_input_remove(m_id[c]); |
| 91 | |
| 92 | m_id[c] = -1; |
| 93 | } |
| 94 | |
| 95 | void _GSocket_Enable_Events(GSocket *socket) |
| 96 | { |
| 97 | _GSocket_Install_Callback(socket, GSOCK_INPUT); |
| 98 | _GSocket_Install_Callback(socket, GSOCK_OUTPUT); |
| 99 | } |
| 100 | |
| 101 | void _GSocket_Disable_Events(GSocket *socket) |
| 102 | { |
| 103 | _GSocket_Uninstall_Callback(socket, GSOCK_INPUT); |
| 104 | _GSocket_Uninstall_Callback(socket, GSOCK_OUTPUT); |
| 105 | } |
| 106 | |
| 107 | #endif /* wxUSE_SOCKETS */ |