Changed unportable __FUNCTION__ to the verbatim function name.
[glib.git] / giowin32.c
blob560d714ba285cf16c940f54c52e6e21b0a30e973
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * giowin32.c: IO Channels for Win32.
5 * Copyright 1998 Owen Taylor and Tor Lillqvist
6 * Copyright 1999-2000 Tor Lillqvist and Craig Setera
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
26 * file for a list of people on the GLib Team. See the ChangeLog
27 * files for a list of changes. These files are distributed with
28 * GLib at ftp://ftp.gtk.org/pub/gtk/.
31 /* Define this to get (very) verbose logging of all channels */
32 /* #define G_IO_WIN32_DEBUG */
34 #include "glib.h"
36 #include <stdlib.h>
37 #include <windows.h>
38 #include <winsock.h> /* Not everybody has winsock2 */
39 #include <fcntl.h>
40 #include <io.h>
41 #include <process.h>
42 #include <errno.h>
43 #include <sys/stat.h>
45 typedef struct _GIOWin32Channel GIOWin32Channel;
46 typedef struct _GIOWin32Watch GIOWin32Watch;
48 #define BUFFER_SIZE 4096
50 typedef enum {
51 G_IO_WINDOWS_MESSAGES, /* Windows messages */
52 G_IO_FILE_DESC, /* Unix-like file descriptors from
53 * _open() or _pipe(). Read with read().
54 * Have to create separate thread to read.
56 G_IO_STREAM_SOCKET /* Stream sockets. Similar as fds, but
57 * read with recv().
59 } GIOWin32ChannelType;
61 struct _GIOWin32Channel {
62 GIOChannel channel;
63 gint fd; /* Either a Unix-like file handle as provided
64 * by the Microsoft C runtime, or a SOCKET
65 * as provided by WinSock.
67 GIOWin32ChannelType type;
69 gboolean debug;
71 /* This is used by G_IO_WINDOWS_MESSAGES channels */
72 HWND hwnd; /* handle of window, or NULL */
74 /* Following fields used by fd and socket channels for input */
76 /* Data is kept in a circular buffer. To be able to distinguish between
77 * empty and full buffer, we cannot fill it completely, but have to
78 * leave a one character gap.
80 * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
82 * Empty: wrp == rdp
83 * Full: (wrp + 1) % BUFFER_SIZE == rdp
84 * Partial: otherwise
86 guchar *buffer; /* (Circular) buffer */
87 gint wrp, rdp; /* Buffer indices for writing and reading */
88 gboolean running; /* Is reader thread running. FALSE if
89 * EOF has been reached.
91 guint thread_id; /* If non-NULL has a reader thread, or has
92 * had.*/
93 HANDLE data_avail_event;
94 HANDLE space_avail_event;
95 CRITICAL_SECTION mutex;
97 /* Function that actually reads from fd */
98 int (*reader) (int fd, guchar *buf, int len);
101 #define LOCK(mutex) EnterCriticalSection (&mutex)
102 #define UNLOCK(mutex) LeaveCriticalSection (&mutex)
104 struct _GIOWin32Watch {
105 GPollFD pollfd;
106 GIOChannel *channel;
107 GIOCondition condition;
108 GIOFunc callback;
111 static void
112 g_io_channel_win32_init (GIOWin32Channel *channel)
114 #ifdef G_IO_WIN32_DEBUG
115 channel->debug = TRUE;
116 #else
117 if (getenv ("G_IO_WIN32_DEBUG") != NULL)
118 channel->debug = TRUE;
119 else
120 channel->debug = FALSE;
121 #endif
122 channel->buffer = NULL;
123 channel->running = FALSE;
124 channel->thread_id = 0;
125 channel->data_avail_event = NULL;
126 channel->space_avail_event = NULL;
129 static void
130 create_events (GIOWin32Channel *channel)
132 SECURITY_ATTRIBUTES sec_attrs;
134 sec_attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
135 sec_attrs.lpSecurityDescriptor = NULL;
136 sec_attrs.bInheritHandle = FALSE;
138 /* The data available event is manual reset, the space available event
139 * is automatic reset.
141 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
142 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
144 gchar *msg = g_win32_error_message (GetLastError ());
145 g_error ("Error creating event: %s", msg);
147 InitializeCriticalSection (&channel->mutex);
150 static unsigned __stdcall
151 reader_thread (void *parameter)
153 GIOWin32Channel *channel = parameter;
154 guchar *buffer;
155 guint nbytes;
157 g_io_channel_ref ((GIOChannel *) channel);
159 if (channel->debug)
160 g_print ("thread %#x: starting. pid:%#x, fd:%d, data_avail:%#x, space_avail:%#x\n",
161 channel->thread_id,
162 (guint) GetCurrentProcessId (),
163 channel->fd,
164 (guint) channel->data_avail_event,
165 (guint) channel->space_avail_event);
167 channel->buffer = g_malloc (BUFFER_SIZE);
168 channel->rdp = channel->wrp = 0;
169 channel->running = TRUE;
171 SetEvent (channel->space_avail_event);
173 while (channel->running)
175 LOCK (channel->mutex);
176 if (channel->debug)
177 g_print ("thread %#x: rdp=%d, wrp=%d\n",
178 channel->thread_id, channel->rdp, channel->wrp);
179 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
181 /* Buffer is full */
182 if (channel->debug)
183 g_print ("thread %#x: resetting space_available\n",
184 channel->thread_id);
185 ResetEvent (channel->space_avail_event);
186 if (channel->debug)
187 g_print ("thread %#x: waiting for space\n", channel->thread_id);
188 UNLOCK (channel->mutex);
189 WaitForSingleObject (channel->space_avail_event, INFINITE);
190 LOCK (channel->mutex);
191 if (channel->debug)
192 g_print ("thread %#x: rdp=%d, wrp=%d\n",
193 channel->thread_id, channel->rdp, channel->wrp);
196 buffer = channel->buffer + channel->wrp;
198 /* Always leave at least one byte unused gap to be able to
199 * distinguish between the full and empty condition...
201 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
202 BUFFER_SIZE - channel->wrp);
204 if (channel->debug)
205 g_print ("thread %#x: calling reader for %d bytes\n",
206 channel->thread_id, nbytes);
208 UNLOCK (channel->mutex);
210 nbytes = (*channel->reader) (channel->fd, buffer, nbytes);
212 if (nbytes <= 0)
213 break;
215 LOCK (channel->mutex);
216 if (channel->debug)
217 g_print ("thread %#x: got %d bytes, rdp=%d, wrp=%d\n",
218 channel->thread_id, nbytes, channel->rdp, channel->wrp);
219 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
220 if (channel->debug)
221 g_print ("thread %#x: rdp=%d, wrp=%d, setting data available\n",
222 channel->thread_id, channel->rdp, channel->wrp);
223 SetEvent (channel->data_avail_event);
224 UNLOCK (channel->mutex);
227 LOCK (channel->mutex);
228 channel->running = FALSE;
229 if (channel->debug)
230 g_print ("thread %#x: got EOF, rdp=%d, wrp=%d, setting data available\n",
231 channel->thread_id, channel->rdp, channel->wrp);
232 SetEvent (channel->data_avail_event);
233 UNLOCK (channel->mutex);
235 g_io_channel_unref((GIOChannel *) channel);
237 /* All of the Microsoft docs say we should explicitly
238 * end the thread...
240 _endthreadex(1);
242 return 0;
245 static void
246 create_reader_thread (GIOWin32Channel *channel,
247 gpointer reader)
249 channel->reader = reader;
251 if (_beginthreadex (NULL, 0, reader_thread, channel, 0,
252 &channel->thread_id) == 0)
253 g_warning ("Error creating reader thread: %s", strerror (errno));
254 WaitForSingleObject (channel->space_avail_event, INFINITE);
257 static int
258 buffer_read (GIOWin32Channel *channel,
259 guchar *dest,
260 guint count,
261 GIOError *error)
263 guint nbytes;
264 guint left = count;
266 LOCK (channel->mutex);
267 if (channel->debug)
268 g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
269 channel->thread_id, count, channel->rdp, channel->wrp);
271 if (channel->rdp == channel->wrp)
273 UNLOCK (channel->mutex);
274 if (channel->debug)
275 g_print ("waiting for data from thread %#x\n", channel->thread_id);
276 WaitForSingleObject (channel->data_avail_event, INFINITE);
277 LOCK (channel->mutex);
278 if (channel->rdp == channel->wrp && !channel->running)
280 UNLOCK (channel->mutex);
281 return 0;
285 if (channel->rdp < channel->wrp)
286 nbytes = channel->wrp - channel->rdp;
287 else
288 nbytes = BUFFER_SIZE - channel->rdp;
289 UNLOCK (channel->mutex);
290 nbytes = MIN (left, nbytes);
291 if (channel->debug)
292 g_print ("moving %d bytes from thread %#x\n",
293 nbytes, channel->thread_id);
294 memcpy (dest, channel->buffer + channel->rdp, nbytes);
295 dest += nbytes;
296 left -= nbytes;
297 LOCK (channel->mutex);
298 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
299 if (channel->debug)
300 g_print ("setting space available for thread %#x\n", channel->thread_id);
301 SetEvent (channel->space_avail_event);
302 if (channel->debug)
303 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
304 channel->thread_id, channel->rdp, channel->wrp);
305 if (channel->running && channel->rdp == channel->wrp)
307 if (channel->debug)
308 g_print ("resetting data_available of thread %#x\n",
309 channel->thread_id);
310 ResetEvent (channel->data_avail_event);
312 UNLOCK (channel->mutex);
314 /* We have no way to indicate any errors form the actual
315 * read() or recv() call in the reader thread. Should we have?
317 *error = G_IO_ERROR_NONE;
318 return count - left;
321 static gboolean
322 g_io_win32_prepare (gpointer source_data,
323 GTimeVal *current_time,
324 gint *timeout,
325 gpointer user_data)
327 *timeout = -1;
329 return FALSE;
332 static gboolean
333 g_io_win32_check (gpointer source_data,
334 GTimeVal *current_time,
335 gpointer user_data)
337 GIOWin32Watch *data = source_data;
338 GIOWin32Channel *channel = (GIOWin32Channel *) data->channel;
340 /* If the thread has died, we have encountered EOF. If the buffer
341 * also is emtpty set the HUP bit.
343 if (!channel->running && channel->rdp == channel->wrp)
345 if (channel->debug)
346 g_print ("g_io_win32_check: setting G_IO_HUP thread %#x rdp=%d wrp=%d\n",
347 channel->thread_id, channel->rdp, channel->wrp);
348 data->pollfd.revents |= G_IO_HUP;
349 return TRUE;
352 return (data->pollfd.revents & data->condition);
355 static gboolean
356 g_io_win32_dispatch (gpointer source_data,
357 GTimeVal *current_time,
358 gpointer user_data)
361 GIOWin32Watch *data = source_data;
363 return (*data->callback) (data->channel,
364 data->pollfd.revents & data->condition,
365 user_data);
368 static void
369 g_io_win32_destroy (gpointer source_data)
371 GIOWin32Watch *data = source_data;
373 g_main_remove_poll (&data->pollfd);
374 g_io_channel_unref (data->channel);
375 g_free (data);
378 static GSourceFuncs win32_watch_funcs = {
379 g_io_win32_prepare,
380 g_io_win32_check,
381 g_io_win32_dispatch,
382 g_io_win32_destroy
385 static guint
386 g_io_win32_add_watch (GIOChannel *channel,
387 gint priority,
388 GIOCondition condition,
389 GIOFunc func,
390 gpointer user_data,
391 GDestroyNotify notify,
392 int (*reader) (int, guchar *, int))
394 GIOWin32Watch *watch = g_new (GIOWin32Watch, 1);
395 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
397 watch->channel = channel;
398 g_io_channel_ref (channel);
400 watch->callback = func;
401 watch->condition = condition;
403 if (win32_channel->data_avail_event == NULL)
404 create_events (win32_channel);
406 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
407 watch->pollfd.events = condition;
409 if (win32_channel->debug)
410 g_print ("g_io_win32_add_watch: fd:%d handle:%#x\n",
411 win32_channel->fd, watch->pollfd.fd);
413 if (win32_channel->thread_id == 0)
414 create_reader_thread (win32_channel, reader);
416 g_main_add_poll (&watch->pollfd, priority);
418 return g_source_add (priority, TRUE, &win32_watch_funcs, watch,
419 user_data, notify);
422 static GIOError
423 g_io_win32_msg_read (GIOChannel *channel,
424 gchar *buf,
425 guint count,
426 guint *bytes_read)
428 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
429 MSG msg; /* In case of alignment problems */
431 if (count < sizeof (MSG))
432 return G_IO_ERROR_INVAL;
434 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
435 return G_IO_ERROR_AGAIN;
437 memmove (buf, &msg, sizeof (MSG));
438 *bytes_read = sizeof (MSG);
439 return G_IO_ERROR_NONE;
442 static GIOError
443 g_io_win32_msg_write (GIOChannel *channel,
444 gchar *buf,
445 guint count,
446 guint *bytes_written)
448 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
449 MSG msg;
451 if (count != sizeof (MSG))
452 return G_IO_ERROR_INVAL;
454 /* In case of alignment problems */
455 memmove (&msg, buf, sizeof (MSG));
456 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
457 return G_IO_ERROR_UNKNOWN;
459 *bytes_written = sizeof (MSG);
460 return G_IO_ERROR_NONE;
463 static GIOError
464 g_io_win32_no_seek (GIOChannel *channel,
465 gint offset,
466 GSeekType type)
468 return G_IO_ERROR_UNKNOWN;
471 static void
472 g_io_win32_msg_close (GIOChannel *channel)
474 /* Nothing to be done. Or should we set hwnd to some invalid value? */
477 static void
478 g_io_win32_free (GIOChannel *channel)
480 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
482 if (win32_channel->buffer)
484 CloseHandle (win32_channel->data_avail_event);
485 CloseHandle (win32_channel->space_avail_event);
486 DeleteCriticalSection (&win32_channel->mutex);
489 g_free (win32_channel->buffer);
490 g_free (win32_channel);
493 static guint
494 g_io_win32_msg_add_watch (GIOChannel *channel,
495 gint priority,
496 GIOCondition condition,
497 GIOFunc func,
498 gpointer user_data,
499 GDestroyNotify notify)
501 GIOWin32Watch *watch = g_new (GIOWin32Watch, 1);
503 watch->channel = channel;
504 g_io_channel_ref (channel);
506 watch->callback = func;
507 watch->condition = condition;
509 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
510 watch->pollfd.events = condition;
512 g_main_add_poll (&watch->pollfd, priority);
514 return g_source_add (priority, TRUE, &win32_watch_funcs,
515 watch, user_data, notify);
518 static GIOError
519 g_io_win32_fd_read (GIOChannel *channel,
520 gchar *buf,
521 guint count,
522 guint *bytes_read)
524 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
525 gint result;
526 GIOError error;
528 if (win32_channel->debug)
529 g_print ("g_io_win32_fd_read: fd:%d count:%d\n",
530 win32_channel->fd, count);
532 if (win32_channel->thread_id)
534 result = buffer_read (win32_channel, buf, count, &error);
535 if (result < 0)
537 *bytes_read = 0;
538 return error;
540 else
542 *bytes_read = result;
543 return G_IO_ERROR_NONE;
547 result = read (win32_channel->fd, buf, count);
549 if (result < 0)
551 *bytes_read = 0;
552 if (errno == EINVAL)
553 return G_IO_ERROR_INVAL;
554 else
555 return G_IO_ERROR_UNKNOWN;
557 else
559 *bytes_read = result;
560 return G_IO_ERROR_NONE;
564 static GIOError
565 g_io_win32_fd_write (GIOChannel *channel,
566 gchar *buf,
567 guint count,
568 guint *bytes_written)
570 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
571 gint result;
573 result = write (win32_channel->fd, buf, count);
574 if (win32_channel->debug)
575 g_print ("g_io_win32_fd_write: fd:%d count:%d = %d\n",
576 win32_channel->fd, count, result);
578 if (result < 0)
580 *bytes_written = 0;
581 switch (errno)
583 case EINVAL:
584 return G_IO_ERROR_INVAL;
585 case EAGAIN:
586 return G_IO_ERROR_AGAIN;
587 default:
588 return G_IO_ERROR_UNKNOWN;
591 else
593 *bytes_written = result;
594 return G_IO_ERROR_NONE;
598 static GIOError
599 g_io_win32_fd_seek (GIOChannel *channel,
600 gint offset,
601 GSeekType type)
603 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
604 int whence;
605 off_t result;
607 switch (type)
609 case G_SEEK_SET:
610 whence = SEEK_SET;
611 break;
612 case G_SEEK_CUR:
613 whence = SEEK_CUR;
614 break;
615 case G_SEEK_END:
616 whence = SEEK_END;
617 break;
618 default:
619 g_warning ("g_io_win32_fd_seek: unknown seek type");
620 return G_IO_ERROR_UNKNOWN;
623 result = lseek (win32_channel->fd, offset, whence);
625 if (result < 0)
627 switch (errno)
629 case EINVAL:
630 return G_IO_ERROR_INVAL;
631 default:
632 return G_IO_ERROR_UNKNOWN;
635 else
636 return G_IO_ERROR_NONE;
639 static void
640 g_io_win32_fd_close (GIOChannel *channel)
642 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
644 close (win32_channel->fd);
645 return;
648 static int
649 fd_reader (int fd,
650 guchar *buf,
651 int len)
653 return read (fd, buf, len);
656 static guint
657 g_io_win32_fd_add_watch (GIOChannel *channel,
658 gint priority,
659 GIOCondition condition,
660 GIOFunc func,
661 gpointer user_data,
662 GDestroyNotify notify)
664 return g_io_win32_add_watch (channel, priority, condition,
665 func, user_data, notify, fd_reader);
668 static GIOError
669 g_io_win32_sock_read (GIOChannel *channel,
670 gchar *buf,
671 guint count,
672 guint *bytes_read)
674 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
675 gint result;
676 GIOError error;
678 if (win32_channel->thread_id)
680 result = buffer_read (win32_channel, buf, count, &error);
681 if (result < 0)
683 *bytes_read = 0;
684 return error;
686 else
688 *bytes_read = result;
689 return G_IO_ERROR_NONE;
693 result = recv (win32_channel->fd, buf, count, 0);
695 if (result < 0)
697 *bytes_read = 0;
698 return G_IO_ERROR_UNKNOWN;
700 else
702 *bytes_read = result;
703 return G_IO_ERROR_NONE;
707 static GIOError
708 g_io_win32_sock_write (GIOChannel *channel,
709 gchar *buf,
710 guint count,
711 guint *bytes_written)
713 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
714 gint result;
716 result = send (win32_channel->fd, buf, count, 0);
718 if (result == SOCKET_ERROR)
720 *bytes_written = 0;
721 switch (WSAGetLastError ())
723 case WSAEINVAL:
724 return G_IO_ERROR_INVAL;
725 case WSAEWOULDBLOCK:
726 case WSAEINTR:
727 return G_IO_ERROR_AGAIN;
728 default:
729 return G_IO_ERROR_UNKNOWN;
732 else
734 *bytes_written = result;
735 return G_IO_ERROR_NONE;
739 static void
740 g_io_win32_sock_close (GIOChannel *channel)
742 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
744 closesocket (win32_channel->fd);
747 static int
748 sock_reader (int fd,
749 guchar *buf,
750 int len)
752 return recv (fd, buf, len, 0);
755 static guint
756 g_io_win32_sock_add_watch (GIOChannel *channel,
757 gint priority,
758 GIOCondition condition,
759 GIOFunc func,
760 gpointer user_data,
761 GDestroyNotify notify)
763 return g_io_win32_add_watch (channel, priority, condition,
764 func, user_data, notify, sock_reader);
767 static GIOFuncs win32_channel_msg_funcs = {
768 g_io_win32_msg_read,
769 g_io_win32_msg_write,
770 g_io_win32_no_seek,
771 g_io_win32_msg_close,
772 g_io_win32_msg_add_watch,
773 g_io_win32_free
776 static GIOFuncs win32_channel_fd_funcs = {
777 g_io_win32_fd_read,
778 g_io_win32_fd_write,
779 g_io_win32_fd_seek,
780 g_io_win32_fd_close,
781 g_io_win32_fd_add_watch,
782 g_io_win32_free
785 static GIOFuncs win32_channel_sock_funcs = {
786 g_io_win32_sock_read,
787 g_io_win32_sock_write,
788 g_io_win32_no_seek,
789 g_io_win32_sock_close,
790 g_io_win32_sock_add_watch,
791 g_io_win32_free
794 GIOChannel *
795 g_io_channel_win32_new_messages (guint hwnd)
797 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
798 GIOChannel *channel = (GIOChannel *) win32_channel;
800 g_io_channel_init (channel);
801 g_io_channel_win32_init (win32_channel);
802 channel->funcs = &win32_channel_msg_funcs;
803 win32_channel->type = G_IO_WINDOWS_MESSAGES;
804 win32_channel->hwnd = (HWND) hwnd;
806 return channel;
809 GIOChannel *
810 g_io_channel_win32_new_fd (gint fd)
812 GIOWin32Channel *win32_channel;
813 GIOChannel *channel;
814 struct stat st;
816 if (fstat (fd, &st) == -1)
818 g_warning ("%d isn't a (emulated) file descriptor", fd);
819 return NULL;
822 win32_channel = g_new (GIOWin32Channel, 1);
823 channel = (GIOChannel *) win32_channel;
825 g_io_channel_init (channel);
826 g_io_channel_win32_init (win32_channel);
827 channel->funcs = &win32_channel_fd_funcs;
828 win32_channel->type = G_IO_FILE_DESC;
829 win32_channel->fd = fd;
831 return channel;
834 gint
835 g_io_channel_win32_get_fd (GIOChannel *channel)
837 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
839 return win32_channel->fd;
842 GIOChannel *
843 g_io_channel_win32_new_stream_socket (int socket)
845 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
846 GIOChannel *channel = (GIOChannel *) win32_channel;
848 g_io_channel_init (channel);
849 g_io_channel_win32_init (win32_channel);
850 channel->funcs = &win32_channel_sock_funcs;
851 win32_channel->type = G_IO_STREAM_SOCKET;
852 win32_channel->fd = socket;
854 return channel;
857 GIOChannel *
858 g_io_channel_unix_new (gint fd)
860 return g_io_channel_win32_new_fd (fd);
863 gint
864 g_io_channel_unix_get_fd (GIOChannel *channel)
866 return g_io_channel_win32_get_fd (channel);
869 void
870 g_io_channel_win32_set_debug (GIOChannel *channel,
871 gboolean flag)
873 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
875 win32_channel->debug = flag;
878 gint
879 g_io_channel_win32_poll (GPollFD *fds,
880 gint n_fds,
881 gint timeout)
883 int result;
885 g_return_val_if_fail (n_fds >= 0, 0);
887 result = (*g_main_win32_get_poll_func ()) (fds, n_fds, timeout);
889 return result;
892 void
893 g_io_channel_win32_make_pollfd (GIOChannel *channel,
894 GIOCondition condition,
895 GPollFD *fd)
897 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
899 if (win32_channel->data_avail_event == NULL)
900 create_events (win32_channel);
902 fd->fd = (gint) win32_channel->data_avail_event;
903 fd->events = condition;
905 if (win32_channel->thread_id == 0)
906 if (win32_channel->type == G_IO_FILE_DESC)
907 create_reader_thread (win32_channel, fd_reader);
908 else if (win32_channel->type == G_IO_STREAM_SOCKET)
909 create_reader_thread (win32_channel, sock_reader);
912 /* This variable and the functions below are present just to be
913 * binary compatible with old clients... But note that in GIMP, the
914 * libgimp/gimp.c:gimp_extension_process() function will have to be modified
915 * anyhow for this new approach.
917 * These will be removed after some weeks.
919 guint g_pipe_readable_msg = 0;
921 GIOChannel *
922 g_io_channel_win32_new_pipe (int fd)
924 return g_io_channel_win32_new_fd (fd);
927 GIOChannel *
928 g_io_channel_win32_new_pipe_with_wakeups (int fd,
929 guint peer,
930 int peer_fd)
932 return g_io_channel_win32_new_fd (fd);
935 void
936 g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
937 guint peer,
938 int peer_fd)
940 /* Nothing needed now */
943 void
944 g_io_channel_win32_pipe_readable (gint fd,
945 guint offset)
947 /* Nothing needed now */