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;
128 create_events (GIOWin32Channel *channel)
130 SECURITY_ATTRIBUTES sec_attrs;
132 sec_attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
133 sec_attrs.lpSecurityDescriptor = NULL;
134 sec_attrs.bInheritHandle = FALSE;
136 channel->data_avail_event = NULL;
137 channel->space_avail_event = NULL;
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);
205 UNLOCK (channel->mutex);
207 nbytes = (*channel->reader) (channel->fd, buffer, nbytes);
212 LOCK (channel->mutex);
214 g_print ("thread %#x: got %d bytes, rdp=%d, wrp=%d\n",
215 channel->thread_id, nbytes, channel->rdp, channel->wrp);
216 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
218 g_print ("thread %#x: rdp=%d, wrp=%d, setting data available\n",
219 channel->thread_id, channel->rdp, channel->wrp);
220 SetEvent (channel->data_avail_event);
221 UNLOCK (channel->mutex);
224 LOCK (channel->mutex);
225 channel->running = FALSE;
227 g_print ("thread %#x: got EOF, rdp=%d, wrp=%d, setting data available\n",
228 channel->thread_id, channel->rdp, channel->wrp);
229 SetEvent (channel->data_avail_event);
230 UNLOCK (channel->mutex);
232 g_io_channel_unref((GIOChannel *) channel);
234 /* All of the Microsoft docs say we should explicitly
243 create_reader_thread (GIOWin32Channel *channel,
246 channel->reader = reader;
248 if (_beginthreadex (NULL, 0, reader_thread, channel, 0,
249 &channel->thread_id) == 0)
250 g_warning ("Error creating reader thread: %s", strerror (errno));
251 WaitForSingleObject (channel->space_avail_event, INFINITE);
255 buffer_read (GIOWin32Channel *channel,
263 LOCK (channel->mutex);
265 g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
266 channel->thread_id, count, channel->rdp, channel->wrp);
268 if (channel->rdp == channel->wrp)
270 UNLOCK (channel->mutex);
272 g_print ("waiting for data from thread %#x\n", channel->thread_id);
273 WaitForSingleObject (channel->data_avail_event, INFINITE);
274 LOCK (channel->mutex);
275 if (channel->rdp == channel->wrp && !channel->running)
279 if (channel->rdp < channel->wrp)
280 nbytes = channel->wrp - channel->rdp;
282 nbytes = BUFFER_SIZE - channel->rdp;
283 UNLOCK (channel->mutex);
284 nbytes = MIN (left, nbytes);
286 g_print ("moving %d bytes from thread %#x\n",
287 nbytes, channel->thread_id);
288 memcpy (dest, channel->buffer + channel->rdp, nbytes);
291 LOCK (channel->mutex);
292 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
294 g_print ("setting space available for thread %#x\n", channel->thread_id);
295 SetEvent (channel->space_avail_event);
297 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
298 channel->thread_id, channel->rdp, channel->wrp);
299 if (channel->running && channel->rdp == channel->wrp)
302 g_print ("resetting data_available of thread %#x\n",
304 ResetEvent (channel->data_avail_event);
306 UNLOCK (channel->mutex);
308 /* We have no way to indicate any errors form the actual
309 * read() or recv() call in the reader thread. Should we have?
311 *error = G_IO_ERROR_NONE;
316 g_io_win32_prepare (gpointer source_data,
317 GTimeVal *current_time,
327 g_io_win32_check (gpointer source_data,
328 GTimeVal *current_time,
331 GIOWin32Watch *data = source_data;
332 GIOWin32Channel *channel = (GIOWin32Channel *) data->channel;
334 /* If the thread has died, we have encountered EOF. If the buffer
335 * also is emtpty set the HUP bit.
337 if (!channel->running && channel->rdp == channel->wrp)
340 g_print ("g_io_win32_check: setting G_IO_HUP thread %#x rdp=%d wrp=%d\n",
341 channel->thread_id, channel->rdp, channel->wrp);
342 data->pollfd.revents |= G_IO_HUP;
346 return (data->pollfd.revents & data->condition);
350 g_io_win32_dispatch (gpointer source_data,
351 GTimeVal *current_time,
355 GIOWin32Watch *data = source_data;
357 return (*data->callback) (data->channel,
358 data->pollfd.revents & data->condition,
363 g_io_win32_destroy (gpointer source_data)
365 GIOWin32Watch *data = source_data;
367 g_main_remove_poll (&data->pollfd);
368 g_io_channel_unref (data->channel);
372 static GSourceFuncs win32_watch_funcs = {
380 g_io_win32_msg_read (GIOChannel *channel,
385 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
386 MSG msg; /* In case of alignment problems */
388 if (count < sizeof (MSG))
389 return G_IO_ERROR_INVAL;
391 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
392 return G_IO_ERROR_AGAIN;
394 memmove (buf, &msg, sizeof (MSG));
395 *bytes_read = sizeof (MSG);
396 return G_IO_ERROR_NONE;
400 g_io_win32_msg_write (GIOChannel *channel,
403 guint *bytes_written)
405 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
408 if (count != sizeof (MSG))
409 return G_IO_ERROR_INVAL;
411 /* In case of alignment problems */
412 memmove (&msg, buf, sizeof (MSG));
413 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
414 return G_IO_ERROR_UNKNOWN;
416 *bytes_written = sizeof (MSG);
417 return G_IO_ERROR_NONE;
421 g_io_win32_no_seek (GIOChannel *channel,
425 return G_IO_ERROR_UNKNOWN;
430 g_io_win32_msg_close (GIOChannel *channel)
432 /* Nothing to be done. Or should we set hwnd to some invalid value? */
436 g_io_win32_free (GIOChannel *channel)
438 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
440 if (win32_channel->buffer)
442 CloseHandle (win32_channel->data_avail_event);
443 CloseHandle (win32_channel->space_avail_event);
444 DeleteCriticalSection (&win32_channel->mutex);
447 g_free (win32_channel->buffer);
448 g_free (win32_channel);
452 g_io_win32_msg_add_watch (GIOChannel *channel,
454 GIOCondition condition,
457 GDestroyNotify notify)
459 GIOWin32Watch *watch = g_new (GIOWin32Watch, 1);
461 watch->channel = channel;
462 g_io_channel_ref (channel);
464 watch->callback = func;
465 watch->condition = condition;
467 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
468 watch->pollfd.events = condition;
470 g_main_add_poll (&watch->pollfd, priority);
472 return g_source_add (priority, TRUE, &win32_watch_funcs,
473 watch, user_data, notify);
477 g_io_win32_fd_read (GIOChannel *channel,
482 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
486 if (win32_channel->thread_id)
488 result = buffer_read (win32_channel, buf, count, &error);
496 *bytes_read = result;
497 return G_IO_ERROR_NONE;
501 result = read (win32_channel->fd, buf, count);
507 return G_IO_ERROR_INVAL;
509 return G_IO_ERROR_UNKNOWN;
513 *bytes_read = result;
514 return G_IO_ERROR_NONE;
519 g_io_win32_fd_write(GIOChannel *channel,
522 guint *bytes_written)
524 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
527 result = write (win32_channel->fd, buf, count);
528 if (win32_channel->debug)
529 g_print ("g_io_win32_fd_write: fd:%d count:%d = %d\n",
530 win32_channel->fd, count, result);
538 return G_IO_ERROR_INVAL;
540 return G_IO_ERROR_AGAIN;
542 return G_IO_ERROR_UNKNOWN;
547 *bytes_written = result;
548 return G_IO_ERROR_NONE;
553 g_io_win32_fd_seek (GIOChannel *channel,
557 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
573 g_warning ("g_io_win32_fd_seek: unknown seek type");
574 return G_IO_ERROR_UNKNOWN;
577 result = lseek (win32_channel->fd, offset, whence);
584 return G_IO_ERROR_INVAL;
586 return G_IO_ERROR_UNKNOWN;
590 return G_IO_ERROR_NONE;
594 g_io_win32_fd_close (GIOChannel *channel)
596 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
598 close (win32_channel->fd);
607 return read (fd, buf, len);
611 g_io_win32_fd_add_watch (GIOChannel *channel,
613 GIOCondition condition,
616 GDestroyNotify notify)
618 GIOWin32Watch *watch = g_new (GIOWin32Watch, 1);
619 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
623 watch->channel = channel;
624 g_io_channel_ref (channel);
626 watch->callback = func;
627 watch->condition = condition;
629 create_events (win32_channel);
631 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
632 watch->pollfd.events = condition;
634 if (win32_channel->debug)
635 g_print ("g_io_win32_fd_add_watch: fd:%d handle:%#x\n",
636 win32_channel->fd, watch->pollfd.fd);
638 /* Is it readable? (Would be strange to watch it otherwise, but... */
639 if (ReadFile ((HANDLE) _get_osfhandle (win32_channel->fd),
640 dummy, 0, &nbytes, NULL))
641 create_reader_thread (win32_channel, fd_reader);
643 g_main_add_poll (&watch->pollfd, priority);
645 return g_source_add (priority, TRUE, &win32_watch_funcs,
646 watch, user_data, notify);
650 g_io_win32_sock_read (GIOChannel *channel,
655 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
659 if (win32_channel->thread_id)
661 result = buffer_read (win32_channel, buf, count, &error);
669 *bytes_read = result;
670 return G_IO_ERROR_NONE;
674 result = recv (win32_channel->fd, buf, count, 0);
679 return G_IO_ERROR_UNKNOWN;
683 *bytes_read = result;
684 return G_IO_ERROR_NONE;
689 g_io_win32_sock_write(GIOChannel *channel,
692 guint *bytes_written)
694 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
697 result = send (win32_channel->fd, buf, count, 0);
699 if (result == SOCKET_ERROR)
702 switch (WSAGetLastError ())
705 return G_IO_ERROR_INVAL;
708 return G_IO_ERROR_AGAIN;
710 return G_IO_ERROR_UNKNOWN;
715 *bytes_written = result;
716 return G_IO_ERROR_NONE;
721 g_io_win32_sock_close (GIOChannel *channel)
723 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
725 closesocket (win32_channel->fd);
733 return recv (fd, buf, len, 0);
737 g_io_win32_sock_add_watch (GIOChannel *channel,
739 GIOCondition condition,
742 GDestroyNotify notify)
744 GIOWin32Watch *watch = g_new (GIOWin32Watch, 1);
745 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
747 watch->channel = channel;
748 g_io_channel_ref (channel);
750 watch->callback = func;
751 watch->condition = condition;
753 create_events (win32_channel);
755 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
756 watch->pollfd.events = condition;
758 /* Sockets are always readable, aren't they? */
759 create_reader_thread (win32_channel, sock_reader);
761 g_main_add_poll (&watch->pollfd, priority);
763 return g_source_add (priority, TRUE, &win32_watch_funcs, watch,
767 static GIOFuncs win32_channel_msg_funcs = {
769 g_io_win32_msg_write,
771 g_io_win32_msg_close,
772 g_io_win32_msg_add_watch,
776 static GIOFuncs win32_channel_fd_funcs = {
781 g_io_win32_fd_add_watch,
785 static GIOFuncs win32_channel_sock_funcs = {
786 g_io_win32_sock_read,
787 g_io_win32_sock_write,
789 g_io_win32_sock_close,
790 g_io_win32_sock_add_watch,
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;
810 g_io_channel_win32_new_fd (gint fd)
812 GIOWin32Channel *win32_channel;
816 if (fstat (fd, &st) == -1)
818 g_warning ("%d isn't a (emulated) file descriptor", fd);
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;
835 g_io_channel_win32_get_fd (GIOChannel *channel)
837 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
839 return win32_channel->fd;
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;
858 g_io_channel_unix_new (gint fd)
860 return g_io_channel_win32_new_fd (fd);
864 g_io_channel_unix_get_fd (GIOChannel *channel)
866 return g_io_channel_win32_get_fd (channel);
870 g_io_channel_win32_set_debug (GIOChannel *channel,
873 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
875 win32_channel->debug = flag;
879 g_io_channel_win32_wait_for_condition (GIOChannel *channel,
880 GIOCondition condition,
884 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
887 pollfd.fd = (gint) win32_channel->data_avail_event;
888 pollfd.events = condition;
890 if (win32_channel->debug)
891 g_print ("g_io_channel_win32_wait_for_condition: fd:%d event:%#x timeout:%d\n",
892 win32_channel->fd, pollfd.fd, timeout);
894 result = (*g_main_win32_get_poll_func ()) (&pollfd, 1, timeout);
896 if (win32_channel->debug)
897 g_print ("g_io_channel_win32_wait_for_condition: done:%d\n", result);
903 /* This variable and the functions below are present just to be
904 * binary compatible with old clients... But note that in GIMP, the
905 * libgimp/gimp.c:gimp_extension_process() function will have to be modified
906 * anyhow for this new approach.
908 * These will be removed after some weeks.
910 guint g_pipe_readable_msg = 0;
913 g_io_channel_win32_new_pipe (int fd)
915 return g_io_channel_win32_new_fd (fd);
919 g_io_channel_win32_new_pipe_with_wakeups (int fd,
923 return g_io_channel_win32_new_fd (fd);
927 g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
931 /* Nothing needed now */
935 g_io_channel_win32_pipe_readable (gint fd,
938 /* Nothing needed now */