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 {
108 GIOCondition condition;
113 g_io_channel_win32_init (GIOWin32Channel *channel)
115 #ifdef G_IO_WIN32_DEBUG
116 channel->debug = TRUE;
118 if (getenv ("G_IO_WIN32_DEBUG") != NULL)
119 channel->debug = TRUE;
121 channel->debug = FALSE;
123 channel->buffer = NULL;
124 channel->running = FALSE;
125 channel->thread_id = 0;
126 channel->data_avail_event = NULL;
127 channel->space_avail_event = NULL;
131 create_events (GIOWin32Channel *channel)
133 SECURITY_ATTRIBUTES sec_attrs;
135 sec_attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
136 sec_attrs.lpSecurityDescriptor = NULL;
137 sec_attrs.bInheritHandle = FALSE;
139 /* The data available event is manual reset, the space available event
140 * is automatic reset.
142 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
143 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
145 gchar *msg = g_win32_error_message (GetLastError ());
146 g_error ("Error creating event: %s", msg);
148 InitializeCriticalSection (&channel->mutex);
151 static unsigned __stdcall
152 reader_thread (void *parameter)
154 GIOWin32Channel *channel = parameter;
158 g_io_channel_ref ((GIOChannel *) channel);
161 g_print ("thread %#x: starting. pid:%#x, fd:%d, data_avail:%#x, space_avail:%#x\n",
163 (guint) GetCurrentProcessId (),
165 (guint) channel->data_avail_event,
166 (guint) channel->space_avail_event);
168 channel->buffer = g_malloc (BUFFER_SIZE);
169 channel->rdp = channel->wrp = 0;
170 channel->running = TRUE;
172 SetEvent (channel->space_avail_event);
174 while (channel->running)
176 LOCK (channel->mutex);
178 g_print ("thread %#x: rdp=%d, wrp=%d\n",
179 channel->thread_id, channel->rdp, channel->wrp);
180 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
184 g_print ("thread %#x: resetting space_available\n",
186 ResetEvent (channel->space_avail_event);
188 g_print ("thread %#x: waiting for space\n", channel->thread_id);
189 UNLOCK (channel->mutex);
190 WaitForSingleObject (channel->space_avail_event, INFINITE);
191 LOCK (channel->mutex);
193 g_print ("thread %#x: rdp=%d, wrp=%d\n",
194 channel->thread_id, channel->rdp, channel->wrp);
197 buffer = channel->buffer + channel->wrp;
199 /* Always leave at least one byte unused gap to be able to
200 * distinguish between the full and empty condition...
202 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
203 BUFFER_SIZE - channel->wrp);
206 g_print ("thread %#x: calling reader for %d bytes\n",
207 channel->thread_id, nbytes);
209 UNLOCK (channel->mutex);
211 nbytes = (*channel->reader) (channel->fd, buffer, nbytes);
216 LOCK (channel->mutex);
218 g_print ("thread %#x: got %d bytes, rdp=%d, wrp=%d\n",
219 channel->thread_id, nbytes, channel->rdp, channel->wrp);
220 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
222 g_print ("thread %#x: rdp=%d, wrp=%d, setting data available\n",
223 channel->thread_id, channel->rdp, channel->wrp);
224 SetEvent (channel->data_avail_event);
225 UNLOCK (channel->mutex);
228 LOCK (channel->mutex);
229 channel->running = FALSE;
231 g_print ("thread %#x: got EOF, rdp=%d, wrp=%d, setting data available\n",
232 channel->thread_id, channel->rdp, channel->wrp);
233 SetEvent (channel->data_avail_event);
234 UNLOCK (channel->mutex);
236 g_io_channel_unref((GIOChannel *) channel);
238 /* All of the Microsoft docs say we should explicitly
247 create_reader_thread (GIOWin32Channel *channel,
250 channel->reader = reader;
252 if (_beginthreadex (NULL, 0, reader_thread, channel, 0,
253 &channel->thread_id) == 0)
254 g_warning ("Error creating reader thread: %s", strerror (errno));
255 WaitForSingleObject (channel->space_avail_event, INFINITE);
259 buffer_read (GIOWin32Channel *channel,
267 LOCK (channel->mutex);
269 g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
270 channel->thread_id, count, channel->rdp, channel->wrp);
272 if (channel->rdp == channel->wrp)
274 UNLOCK (channel->mutex);
276 g_print ("waiting for data from thread %#x\n", channel->thread_id);
277 WaitForSingleObject (channel->data_avail_event, INFINITE);
278 LOCK (channel->mutex);
279 if (channel->rdp == channel->wrp && !channel->running)
281 UNLOCK (channel->mutex);
286 if (channel->rdp < channel->wrp)
287 nbytes = channel->wrp - channel->rdp;
289 nbytes = BUFFER_SIZE - channel->rdp;
290 UNLOCK (channel->mutex);
291 nbytes = MIN (left, nbytes);
293 g_print ("moving %d bytes from thread %#x\n",
294 nbytes, channel->thread_id);
295 memcpy (dest, channel->buffer + channel->rdp, nbytes);
298 LOCK (channel->mutex);
299 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
301 g_print ("setting space available for thread %#x\n", channel->thread_id);
302 SetEvent (channel->space_avail_event);
304 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
305 channel->thread_id, channel->rdp, channel->wrp);
306 if (channel->running && channel->rdp == channel->wrp)
309 g_print ("resetting data_available of thread %#x\n",
311 ResetEvent (channel->data_avail_event);
313 UNLOCK (channel->mutex);
315 /* We have no way to indicate any errors form the actual
316 * read() or recv() call in the reader thread. Should we have?
318 *error = G_IO_ERROR_NONE;
323 g_io_win32_prepare (GSource *source,
332 g_io_win32_check (GSource *source)
334 GIOWin32Watch *watch = (GIOWin32Watch *)source;
335 GIOWin32Channel *channel = (GIOWin32Channel *) watch->channel;
337 /* If the thread has died, we have encountered EOF. If the buffer
338 * also is emtpty set the HUP bit.
340 if (!channel->running && channel->rdp == channel->wrp)
343 g_print ("g_io_win32_check: setting G_IO_HUP thread %#x rdp=%d wrp=%d\n",
344 channel->thread_id, channel->rdp, channel->wrp);
345 watch->pollfd.revents |= G_IO_HUP;
349 return (watch->pollfd.revents & watch->condition);
353 g_io_win32_dispatch (GSource *source,
354 GSourceFunc callback,
357 GIOWin32Watch *watch = (GIOWin32Watch *)source;
359 return (*callback) (watch->channel,
360 watch->pollfd.revents & watch->condition,
365 g_io_win32_destroy (GSource *source)
367 GIOWin32Watch *watch = (GIOWin32Watch *)source;
369 g_io_channel_unref (watch->channel);
372 static GSourceFuncs win32_watch_funcs = {
380 g_io_win32_create_watch (GIOChannel *channel,
381 GIOCondition condition,
382 int (*reader) (int, guchar *, int)))
384 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
385 GIOWin32Watch *watch;
388 source = g_source_new (&win32_watch_funcs, sizeof (GIOWin32Watch));
389 watch = (GIOWin32Watch *)source;
391 watch->channel = channel;
392 g_io_channel_ref (channel);
394 watch->condition = condition;
396 if (win32_channel->data_avail_event == NULL)
397 create_events (win32_channel);
399 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
400 watch->pollfd.events = condition;
402 if (win32_channel->debug)
403 g_print ("g_io_win32_create_watch: fd:%d handle:%#x\n",
404 win32_channel->fd, watch->pollfd.fd);
406 if (win32_channel->thread_id == 0)
407 create_reader_thread (win32_channel, reader);
409 g_source_add_poll (source, &watch->pollfd);
415 g_io_win32_msg_read (GIOChannel *channel,
420 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
421 MSG msg; /* In case of alignment problems */
423 if (count < sizeof (MSG))
424 return G_IO_ERROR_INVAL;
426 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
427 return G_IO_ERROR_AGAIN;
429 memmove (buf, &msg, sizeof (MSG));
430 *bytes_read = sizeof (MSG);
431 return G_IO_ERROR_NONE;
435 g_io_win32_msg_write (GIOChannel *channel,
438 guint *bytes_written)
440 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
443 if (count != sizeof (MSG))
444 return G_IO_ERROR_INVAL;
446 /* In case of alignment problems */
447 memmove (&msg, buf, sizeof (MSG));
448 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
449 return G_IO_ERROR_UNKNOWN;
451 *bytes_written = sizeof (MSG);
452 return G_IO_ERROR_NONE;
456 g_io_win32_no_seek (GIOChannel *channel,
460 return G_IO_ERROR_UNKNOWN;
464 g_io_win32_msg_close (GIOChannel *channel)
466 /* Nothing to be done. Or should we set hwnd to some invalid value? */
470 g_io_win32_free (GIOChannel *channel)
472 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
474 if (win32_channel->buffer)
476 CloseHandle (win32_channel->data_avail_event);
477 CloseHandle (win32_channel->space_avail_event);
478 DeleteCriticalSection (&win32_channel->mutex);
481 g_free (win32_channel->buffer);
482 g_free (win32_channel);
486 g_io_win32_msg_create_watch (GIOChannel *channel,
487 GIOCondition condition)
489 GIOWin32Watch *watch;
492 source = g_source_new (&win32_watch_funcs, sizeof (GIOWin32Watch));
493 watch = (GIOWin32Watch *)source;
495 watch->channel = channel;
496 g_io_channel_ref (channel);
498 watch->condition = condition;
500 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
501 watch->pollfd.events = condition;
503 g_source_add_poll (source, &watch->pollfd);
509 g_io_win32_fd_read (GIOChannel *channel,
514 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
518 if (win32_channel->debug)
519 g_print ("g_io_win32_fd_read: fd:%d count:%d\n",
520 win32_channel->fd, count);
522 if (win32_channel->thread_id)
524 result = buffer_read (win32_channel, buf, count, &error);
532 *bytes_read = result;
533 return G_IO_ERROR_NONE;
537 result = read (win32_channel->fd, buf, count);
543 return G_IO_ERROR_INVAL;
545 return G_IO_ERROR_UNKNOWN;
549 *bytes_read = result;
550 return G_IO_ERROR_NONE;
555 g_io_win32_fd_write (GIOChannel *channel,
558 guint *bytes_written)
560 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
563 result = write (win32_channel->fd, buf, count);
564 if (win32_channel->debug)
565 g_print ("g_io_win32_fd_write: fd:%d count:%d = %d\n",
566 win32_channel->fd, count, result);
574 return G_IO_ERROR_INVAL;
576 return G_IO_ERROR_AGAIN;
578 return G_IO_ERROR_UNKNOWN;
583 *bytes_written = result;
584 return G_IO_ERROR_NONE;
589 g_io_win32_fd_seek (GIOChannel *channel,
593 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
609 g_warning ("g_io_win32_fd_seek: unknown seek type");
610 return G_IO_ERROR_UNKNOWN;
613 result = lseek (win32_channel->fd, offset, whence);
620 return G_IO_ERROR_INVAL;
622 return G_IO_ERROR_UNKNOWN;
626 return G_IO_ERROR_NONE;
630 g_io_win32_fd_close (GIOChannel *channel)
632 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
634 close (win32_channel->fd);
643 return read (fd, buf, len);
647 g_io_win32_fd_create_watch (GIOChannel *channel,
648 GIOCondition condition)
650 return g_io_win32_create_watch (channel, condition, fd_reader);
654 g_io_win32_sock_read (GIOChannel *channel,
659 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
663 if (win32_channel->thread_id)
665 result = buffer_read (win32_channel, buf, count, &error);
673 *bytes_read = result;
674 return G_IO_ERROR_NONE;
678 result = recv (win32_channel->fd, buf, count, 0);
683 return G_IO_ERROR_UNKNOWN;
687 *bytes_read = result;
688 return G_IO_ERROR_NONE;
693 g_io_win32_sock_write (GIOChannel *channel,
696 guint *bytes_written)
698 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
701 result = send (win32_channel->fd, buf, count, 0);
703 if (result == SOCKET_ERROR)
706 switch (WSAGetLastError ())
709 return G_IO_ERROR_INVAL;
712 return G_IO_ERROR_AGAIN;
714 return G_IO_ERROR_UNKNOWN;
719 *bytes_written = result;
720 return G_IO_ERROR_NONE;
725 g_io_win32_sock_close (GIOChannel *channel)
727 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
729 closesocket (win32_channel->fd);
737 return recv (fd, buf, len, 0);
741 g_io_win32_sock_create_watch (GIOChannel *channel,
742 GIOCondition condition)
744 return g_io_win32_add_watch (channel, condition, sock_reader);
747 static GIOFuncs win32_channel_msg_funcs = {
749 g_io_win32_msg_write,
751 g_io_win32_msg_close,
752 g_io_win32_msg_create_watch,
756 static GIOFuncs win32_channel_fd_funcs = {
761 g_io_win32_fd_create_watch,
765 static GIOFuncs win32_channel_sock_funcs = {
766 g_io_win32_sock_read,
767 g_io_win32_sock_write,
769 g_io_win32_sock_close,
770 g_io_win32_sock_create_watch,
775 g_io_channel_win32_new_messages (guint hwnd)
777 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
778 GIOChannel *channel = (GIOChannel *) win32_channel;
780 g_io_channel_init (channel);
781 g_io_channel_win32_init (win32_channel);
782 channel->funcs = &win32_channel_msg_funcs;
783 win32_channel->type = G_IO_WINDOWS_MESSAGES;
784 win32_channel->hwnd = (HWND) hwnd;
790 g_io_channel_win32_new_fd (gint fd)
792 GIOWin32Channel *win32_channel;
796 if (fstat (fd, &st) == -1)
798 g_warning ("%d isn't a (emulated) file descriptor", fd);
802 win32_channel = g_new (GIOWin32Channel, 1);
803 channel = (GIOChannel *) win32_channel;
805 g_io_channel_init (channel);
806 g_io_channel_win32_init (win32_channel);
807 channel->funcs = &win32_channel_fd_funcs;
808 win32_channel->type = G_IO_FILE_DESC;
809 win32_channel->fd = fd;
815 g_io_channel_win32_get_fd (GIOChannel *channel)
817 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
819 return win32_channel->fd;
823 g_io_channel_win32_new_stream_socket (int socket)
825 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
826 GIOChannel *channel = (GIOChannel *) win32_channel;
828 g_io_channel_init (channel);
829 g_io_channel_win32_init (win32_channel);
830 channel->funcs = &win32_channel_sock_funcs;
831 win32_channel->type = G_IO_STREAM_SOCKET;
832 win32_channel->fd = socket;
838 g_io_channel_unix_new (gint fd)
840 return g_io_channel_win32_new_fd (fd);
844 g_io_channel_unix_get_fd (GIOChannel *channel)
846 return g_io_channel_win32_get_fd (channel);
850 g_io_channel_win32_set_debug (GIOChannel *channel,
853 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
855 win32_channel->debug = flag;
859 g_io_channel_win32_poll (GPollFD *fds,
865 g_return_val_if_fail (n_fds >= 0, 0);
867 result = (*g_main_win32_get_poll_func ()) (fds, n_fds, timeout);
873 g_io_channel_win32_make_pollfd (GIOChannel *channel,
874 GIOCondition condition,
877 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
879 if (win32_channel->data_avail_event == NULL)
880 create_events (win32_channel);
882 fd->fd = (gint) win32_channel->data_avail_event;
883 fd->events = condition;
885 if (win32_channel->thread_id == 0)
886 if (win32_channel->type == G_IO_FILE_DESC)
887 create_reader_thread (win32_channel, fd_reader);
888 else if (win32_channel->type == G_IO_STREAM_SOCKET)
889 create_reader_thread (win32_channel, sock_reader);
892 /* This variable and the functions below are present just to be
893 * binary compatible with old clients... But note that in GIMP, the
894 * libgimp/gimp.c:gimp_extension_process() function will have to be modified
895 * anyhow for this new approach.
897 * These will be removed after some weeks.
899 guint g_pipe_readable_msg = 0;
902 g_io_channel_win32_new_pipe (int fd)
904 return g_io_channel_win32_new_fd (fd);
908 g_io_channel_win32_new_pipe_with_wakeups (int fd,
912 return g_io_channel_win32_new_fd (fd);
916 g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
920 /* Nothing needed now */
924 g_io_channel_win32_pipe_readable (gint fd,
927 /* Nothing needed now */