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 */
38 #include <winsock.h> /* Not everybody has winsock2 */
45 typedef struct _GIOWin32Channel GIOWin32Channel
;
46 typedef struct _GIOWin32Watch GIOWin32Watch
;
48 #define BUFFER_SIZE 4096
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
59 } GIOWin32ChannelType
;
61 struct _GIOWin32Channel
{
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
;
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).
83 * Full: (wrp + 1) % BUFFER_SIZE == rdp
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
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
{
107 GIOCondition condition
;
112 g_io_channel_win32_init (GIOWin32Channel
*channel
)
114 #ifdef G_IO_WIN32_DEBUG
115 channel
->debug
= TRUE
;
117 if (getenv ("G_IO_WIN32_DEBUG") != NULL
)
118 channel
->debug
= TRUE
;
120 channel
->debug
= FALSE
;
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
;
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
;
157 g_io_channel_ref ((GIOChannel
*) channel
);
160 g_print ("thread %#x: starting. pid:%#x, fd:%d, data_avail:%#x, space_avail:%#x\n",
162 (guint
) GetCurrentProcessId (),
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
);
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
)
183 g_print ("thread %#x: resetting space_available\n",
185 ResetEvent (channel
->space_avail_event
);
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
);
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 UNLOCK (channel
->mutex
);
206 nbytes
= (*channel
->reader
) (channel
->fd
, buffer
, nbytes
);
211 LOCK (channel
->mutex
);
213 g_print ("thread %#x: got %d bytes, rdp=%d, wrp=%d\n",
214 channel
->thread_id
, nbytes
, channel
->rdp
, channel
->wrp
);
215 channel
->wrp
= (channel
->wrp
+ nbytes
) % BUFFER_SIZE
;
217 g_print ("thread %#x: rdp=%d, wrp=%d, setting data available\n",
218 channel
->thread_id
, channel
->rdp
, channel
->wrp
);
219 SetEvent (channel
->data_avail_event
);
220 UNLOCK (channel
->mutex
);
223 LOCK (channel
->mutex
);
224 channel
->running
= FALSE
;
226 g_print ("thread %#x: got EOF, rdp=%d, wrp=%d, setting data available\n",
227 channel
->thread_id
, channel
->rdp
, channel
->wrp
);
228 SetEvent (channel
->data_avail_event
);
229 UNLOCK (channel
->mutex
);
231 g_io_channel_unref((GIOChannel
*) channel
);
233 /* All of the Microsoft docs say we should explicitly
242 create_reader_thread (GIOWin32Channel
*channel
,
245 channel
->reader
= reader
;
247 if (_beginthreadex (NULL
, 0, reader_thread
, channel
, 0,
248 &channel
->thread_id
) == 0)
249 g_warning ("Error creating reader thread: %s", strerror (errno
));
250 WaitForSingleObject (channel
->space_avail_event
, INFINITE
);
254 buffer_read (GIOWin32Channel
*channel
,
262 LOCK (channel
->mutex
);
264 g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
265 channel
->thread_id
, count
, channel
->rdp
, channel
->wrp
);
267 if (channel
->rdp
== channel
->wrp
)
269 UNLOCK (channel
->mutex
);
271 g_print ("waiting for data from thread %#x\n", channel
->thread_id
);
272 WaitForSingleObject (channel
->data_avail_event
, INFINITE
);
273 LOCK (channel
->mutex
);
274 if (channel
->rdp
== channel
->wrp
&& !channel
->running
)
276 UNLOCK (channel
->mutex
);
281 if (channel
->rdp
< channel
->wrp
)
282 nbytes
= channel
->wrp
- channel
->rdp
;
284 nbytes
= BUFFER_SIZE
- channel
->rdp
;
285 UNLOCK (channel
->mutex
);
286 nbytes
= MIN (left
, nbytes
);
288 g_print ("moving %d bytes from thread %#x\n",
289 nbytes
, channel
->thread_id
);
290 memcpy (dest
, channel
->buffer
+ channel
->rdp
, nbytes
);
293 LOCK (channel
->mutex
);
294 channel
->rdp
= (channel
->rdp
+ nbytes
) % BUFFER_SIZE
;
296 g_print ("setting space available for thread %#x\n", channel
->thread_id
);
297 SetEvent (channel
->space_avail_event
);
299 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
300 channel
->thread_id
, channel
->rdp
, channel
->wrp
);
301 if (channel
->running
&& channel
->rdp
== channel
->wrp
)
304 g_print ("resetting data_available of thread %#x\n",
306 ResetEvent (channel
->data_avail_event
);
308 UNLOCK (channel
->mutex
);
310 /* We have no way to indicate any errors form the actual
311 * read() or recv() call in the reader thread. Should we have?
313 *error
= G_IO_ERROR_NONE
;
318 g_io_win32_prepare (gpointer source_data
,
319 GTimeVal
*current_time
,
329 g_io_win32_check (gpointer source_data
,
330 GTimeVal
*current_time
,
333 GIOWin32Watch
*data
= source_data
;
334 GIOWin32Channel
*channel
= (GIOWin32Channel
*) data
->channel
;
336 /* If the thread has died, we have encountered EOF. If the buffer
337 * also is emtpty set the HUP bit.
339 if (!channel
->running
&& channel
->rdp
== channel
->wrp
)
342 g_print ("g_io_win32_check: setting G_IO_HUP thread %#x rdp=%d wrp=%d\n",
343 channel
->thread_id
, channel
->rdp
, channel
->wrp
);
344 data
->pollfd
.revents
|= G_IO_HUP
;
348 return (data
->pollfd
.revents
& data
->condition
);
352 g_io_win32_dispatch (gpointer source_data
,
353 GTimeVal
*current_time
,
357 GIOWin32Watch
*data
= source_data
;
359 return (*data
->callback
) (data
->channel
,
360 data
->pollfd
.revents
& data
->condition
,
365 g_io_win32_destroy (gpointer source_data
)
367 GIOWin32Watch
*data
= source_data
;
369 g_main_remove_poll (&data
->pollfd
);
370 g_io_channel_unref (data
->channel
);
374 static GSourceFuncs win32_watch_funcs
= {
382 g_io_win32_add_watch (GIOChannel
*channel
,
384 GIOCondition condition
,
387 GDestroyNotify notify
,
388 int (*reader
) (int, guchar
*, int))
390 GIOWin32Watch
*watch
= g_new (GIOWin32Watch
, 1);
391 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*) channel
;
393 watch
->channel
= channel
;
394 g_io_channel_ref (channel
);
396 watch
->callback
= func
;
397 watch
->condition
= condition
;
399 if (win32_channel
->data_avail_event
== NULL
)
400 create_events (win32_channel
);
402 watch
->pollfd
.fd
= (gint
) win32_channel
->data_avail_event
;
403 watch
->pollfd
.events
= condition
;
405 if (win32_channel
->debug
)
406 g_print ("g_io_win32_add_watch: fd:%d handle:%#x\n",
407 win32_channel
->fd
, watch
->pollfd
.fd
);
409 if (win32_channel
->thread_id
== 0)
410 create_reader_thread (win32_channel
, reader
);
412 g_main_add_poll (&watch
->pollfd
, priority
);
414 return g_source_add (priority
, TRUE
, &win32_watch_funcs
, watch
,
419 g_io_win32_msg_read (GIOChannel
*channel
,
424 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*) channel
;
425 MSG msg
; /* In case of alignment problems */
427 if (count
< sizeof (MSG
))
428 return G_IO_ERROR_INVAL
;
430 if (!PeekMessage (&msg
, win32_channel
->hwnd
, 0, 0, PM_REMOVE
))
431 return G_IO_ERROR_AGAIN
;
433 memmove (buf
, &msg
, sizeof (MSG
));
434 *bytes_read
= sizeof (MSG
);
435 return G_IO_ERROR_NONE
;
439 g_io_win32_msg_write (GIOChannel
*channel
,
442 guint
*bytes_written
)
444 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*) channel
;
447 if (count
!= sizeof (MSG
))
448 return G_IO_ERROR_INVAL
;
450 /* In case of alignment problems */
451 memmove (&msg
, buf
, sizeof (MSG
));
452 if (!PostMessage (win32_channel
->hwnd
, msg
.message
, msg
.wParam
, msg
.lParam
))
453 return G_IO_ERROR_UNKNOWN
;
455 *bytes_written
= sizeof (MSG
);
456 return G_IO_ERROR_NONE
;
460 g_io_win32_no_seek (GIOChannel
*channel
,
464 return G_IO_ERROR_UNKNOWN
;
468 g_io_win32_msg_close (GIOChannel
*channel
)
470 /* Nothing to be done. Or should we set hwnd to some invalid value? */
474 g_io_win32_free (GIOChannel
*channel
)
476 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*) channel
;
478 if (win32_channel
->buffer
)
480 CloseHandle (win32_channel
->data_avail_event
);
481 CloseHandle (win32_channel
->space_avail_event
);
482 DeleteCriticalSection (&win32_channel
->mutex
);
485 g_free (win32_channel
->buffer
);
486 g_free (win32_channel
);
490 g_io_win32_msg_add_watch (GIOChannel
*channel
,
492 GIOCondition condition
,
495 GDestroyNotify notify
)
497 GIOWin32Watch
*watch
= g_new (GIOWin32Watch
, 1);
499 watch
->channel
= channel
;
500 g_io_channel_ref (channel
);
502 watch
->callback
= func
;
503 watch
->condition
= condition
;
505 watch
->pollfd
.fd
= G_WIN32_MSG_HANDLE
;
506 watch
->pollfd
.events
= condition
;
508 g_main_add_poll (&watch
->pollfd
, priority
);
510 return g_source_add (priority
, TRUE
, &win32_watch_funcs
,
511 watch
, user_data
, notify
);
515 g_io_win32_fd_read (GIOChannel
*channel
,
520 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*) channel
;
524 if (win32_channel
->debug
)
525 g_print ("g_io_win32_fd_read: fd:%d count:%d\n",
526 win32_channel
->fd
, count
);
528 if (win32_channel
->thread_id
)
530 result
= buffer_read (win32_channel
, buf
, count
, &error
);
538 *bytes_read
= result
;
539 return G_IO_ERROR_NONE
;
543 result
= read (win32_channel
->fd
, buf
, count
);
549 return G_IO_ERROR_INVAL
;
551 return G_IO_ERROR_UNKNOWN
;
555 *bytes_read
= result
;
556 return G_IO_ERROR_NONE
;
561 g_io_win32_fd_write (GIOChannel
*channel
,
564 guint
*bytes_written
)
566 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*) channel
;
569 result
= write (win32_channel
->fd
, buf
, count
);
570 if (win32_channel
->debug
)
571 g_print ("g_io_win32_fd_write: fd:%d count:%d = %d\n",
572 win32_channel
->fd
, count
, result
);
580 return G_IO_ERROR_INVAL
;
582 return G_IO_ERROR_AGAIN
;
584 return G_IO_ERROR_UNKNOWN
;
589 *bytes_written
= result
;
590 return G_IO_ERROR_NONE
;
595 g_io_win32_fd_seek (GIOChannel
*channel
,
599 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*) channel
;
615 g_warning ("g_io_win32_fd_seek: unknown seek type");
616 return G_IO_ERROR_UNKNOWN
;
619 result
= lseek (win32_channel
->fd
, offset
, whence
);
626 return G_IO_ERROR_INVAL
;
628 return G_IO_ERROR_UNKNOWN
;
632 return G_IO_ERROR_NONE
;
636 g_io_win32_fd_close (GIOChannel
*channel
)
638 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*) channel
;
640 close (win32_channel
->fd
);
649 return read (fd
, buf
, len
);
653 g_io_win32_fd_add_watch (GIOChannel
*channel
,
655 GIOCondition condition
,
658 GDestroyNotify notify
)
660 return g_io_win32_add_watch (channel
, priority
, condition
,
661 func
, user_data
, notify
, fd_reader
);
665 g_io_win32_sock_read (GIOChannel
*channel
,
670 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*) channel
;
674 if (win32_channel
->thread_id
)
676 result
= buffer_read (win32_channel
, buf
, count
, &error
);
684 *bytes_read
= result
;
685 return G_IO_ERROR_NONE
;
689 result
= recv (win32_channel
->fd
, buf
, count
, 0);
694 return G_IO_ERROR_UNKNOWN
;
698 *bytes_read
= result
;
699 return G_IO_ERROR_NONE
;
704 g_io_win32_sock_write (GIOChannel
*channel
,
707 guint
*bytes_written
)
709 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*) channel
;
712 result
= send (win32_channel
->fd
, buf
, count
, 0);
714 if (result
== SOCKET_ERROR
)
717 switch (WSAGetLastError ())
720 return G_IO_ERROR_INVAL
;
723 return G_IO_ERROR_AGAIN
;
725 return G_IO_ERROR_UNKNOWN
;
730 *bytes_written
= result
;
731 return G_IO_ERROR_NONE
;
736 g_io_win32_sock_close (GIOChannel
*channel
)
738 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*) channel
;
740 closesocket (win32_channel
->fd
);
748 return recv (fd
, buf
, len
, 0);
752 g_io_win32_sock_add_watch (GIOChannel
*channel
,
754 GIOCondition condition
,
757 GDestroyNotify notify
)
759 return g_io_win32_add_watch (channel
, priority
, condition
,
760 func
, user_data
, notify
, sock_reader
);
763 static GIOFuncs win32_channel_msg_funcs
= {
765 g_io_win32_msg_write
,
767 g_io_win32_msg_close
,
768 g_io_win32_msg_add_watch
,
772 static GIOFuncs win32_channel_fd_funcs
= {
777 g_io_win32_fd_add_watch
,
781 static GIOFuncs win32_channel_sock_funcs
= {
782 g_io_win32_sock_read
,
783 g_io_win32_sock_write
,
785 g_io_win32_sock_close
,
786 g_io_win32_sock_add_watch
,
791 g_io_channel_win32_new_messages (guint hwnd
)
793 GIOWin32Channel
*win32_channel
= g_new (GIOWin32Channel
, 1);
794 GIOChannel
*channel
= (GIOChannel
*) win32_channel
;
796 g_io_channel_init (channel
);
797 g_io_channel_win32_init (win32_channel
);
798 channel
->funcs
= &win32_channel_msg_funcs
;
799 win32_channel
->type
= G_IO_WINDOWS_MESSAGES
;
800 win32_channel
->hwnd
= (HWND
) hwnd
;
806 g_io_channel_win32_new_fd (gint fd
)
808 GIOWin32Channel
*win32_channel
;
812 if (fstat (fd
, &st
) == -1)
814 g_warning ("%d isn't a (emulated) file descriptor", fd
);
818 win32_channel
= g_new (GIOWin32Channel
, 1);
819 channel
= (GIOChannel
*) win32_channel
;
821 g_io_channel_init (channel
);
822 g_io_channel_win32_init (win32_channel
);
823 channel
->funcs
= &win32_channel_fd_funcs
;
824 win32_channel
->type
= G_IO_FILE_DESC
;
825 win32_channel
->fd
= fd
;
831 g_io_channel_win32_get_fd (GIOChannel
*channel
)
833 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*) channel
;
835 return win32_channel
->fd
;
839 g_io_channel_win32_new_stream_socket (int socket
)
841 GIOWin32Channel
*win32_channel
= g_new (GIOWin32Channel
, 1);
842 GIOChannel
*channel
= (GIOChannel
*) win32_channel
;
844 g_io_channel_init (channel
);
845 g_io_channel_win32_init (win32_channel
);
846 channel
->funcs
= &win32_channel_sock_funcs
;
847 win32_channel
->type
= G_IO_STREAM_SOCKET
;
848 win32_channel
->fd
= socket
;
854 g_io_channel_unix_new (gint fd
)
856 return g_io_channel_win32_new_fd (fd
);
860 g_io_channel_unix_get_fd (GIOChannel
*channel
)
862 return g_io_channel_win32_get_fd (channel
);
866 g_io_channel_win32_set_debug (GIOChannel
*channel
,
869 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*) channel
;
871 win32_channel
->debug
= flag
;
875 g_io_channel_win32_poll (GPollFD
*fds
,
881 gboolean debug
= FALSE
;
883 g_return_val_if_fail (n_fds
>= 0, 0);
885 result
= (*g_main_win32_get_poll_func ()) (fds
, n_fds
, timeout
);
891 g_io_channel_win32_make_pollfd (GIOChannel
*channel
,
892 GIOCondition condition
,
895 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*) channel
;
897 if (win32_channel
->data_avail_event
== NULL
)
898 create_events (win32_channel
);
900 fd
->fd
= win32_channel
->data_avail_event
;
901 fd
->events
= condition
;
903 if (win32_channel
->thread_id
== 0)
904 if (win32_channel
->type
== G_IO_FILE_DESC
)
905 create_reader_thread (win32_channel
, fd_reader
);
906 else if (win32_channel
->type
== G_IO_STREAM_SOCKET
)
907 create_reader_thread (win32_channel
, sock_reader
);
911 g_io_channel_win32_wait_for_condition (GIOChannel
*channel
,
912 GIOCondition condition
,
916 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*) channel
;
918 g_io_channel_win32_make_pollfd (channel
, condition
, &pollfd
);
920 return g_io_channel_win32_poll (&pollfd
, 1, timeout
);
923 /* This variable and the functions below are present just to be
924 * binary compatible with old clients... But note that in GIMP, the
925 * libgimp/gimp.c:gimp_extension_process() function will have to be modified
926 * anyhow for this new approach.
928 * These will be removed after some weeks.
930 guint g_pipe_readable_msg
= 0;
933 g_io_channel_win32_new_pipe (int fd
)
935 return g_io_channel_win32_new_fd (fd
);
939 g_io_channel_win32_new_pipe_with_wakeups (int fd
,
943 return g_io_channel_win32_new_fd (fd
);
947 g_io_channel_win32_pipe_request_wakeups (GIOChannel
*channel
,
951 /* Nothing needed now */
955 g_io_channel_win32_pipe_readable (gint fd
,
958 /* Nothing needed now */