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 */
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;
69 /* This is used by G_IO_WINDOWS_MESSAGES channels */
70 HWND hwnd; /* handle of window, or NULL */
72 /* Following fields used by fd and socket channels for input */
74 /* Data is kept in a circular buffer. To be able to distinguish between
75 * empty and full buffer, we cannot fill it completely, but have to
76 * leave a one character gap.
78 * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
81 * Full: (wrp + 1) % BUFFER_SIZE == rdp
84 guchar *buffer; /* (Circular) buffer */
85 gint wrp, rdp; /* Buffer indices for writing and reading */
86 gboolean running; /* Is reader thread running. FALSE if
87 * EOF has been reached.
89 guint thread_id; /* If non-NULL has a reader thread, or has
91 HANDLE data_avail_event;
92 HANDLE space_avail_event;
93 CRITICAL_SECTION mutex;
95 /* Function that actually reads from fd */
96 int (*reader) (int fd, guchar *buf, int len);
99 #define LOCK(mutex) EnterCriticalSection (&mutex)
100 #define UNLOCK(mutex) LeaveCriticalSection (&mutex)
102 /* Temporarilyu change a PRINT to PRINT_ to get just *that* message */
103 #define PRINT_(x) g_print x
105 #ifdef G_IO_WIN32_DEBUG
106 #define PRINT(x) PRINT_(x)
111 struct _GIOWin32Watch {
114 GIOCondition condition;
119 create_events (GIOWin32Channel *channel)
121 SECURITY_ATTRIBUTES sec_attrs;
123 sec_attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
124 sec_attrs.lpSecurityDescriptor = NULL;
125 sec_attrs.bInheritHandle = FALSE;
127 channel->data_avail_event = NULL;
128 channel->space_avail_event = NULL;
130 /* The data available event is manual reset, the space available event
131 * is automatic reset.
133 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
134 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
136 gchar *msg = g_win32_error_message (GetLastError ());
137 g_error ("Error creating event: %s", msg);
139 InitializeCriticalSection (&channel->mutex);
142 static unsigned __stdcall
143 reader_thread (void *parameter)
145 GIOWin32Channel *channel = parameter;
149 g_io_channel_ref ((GIOChannel *) channel);
151 PRINT (("thread %#x: starting. pid:%#x, fd:%d, data_avail:%#x, space_avail:%#x\n",
153 (guint) GetCurrentProcessId (),
155 (guint) channel->data_avail_event,
156 (guint) channel->space_avail_event));
158 channel->buffer = g_malloc (BUFFER_SIZE);
159 channel->rdp = channel->wrp = 0;
160 channel->running = TRUE;
162 SetEvent (channel->space_avail_event);
164 while (channel->running)
166 LOCK (channel->mutex);
167 PRINT (("thread %#x: rdp=%d, wrp=%d\n",
168 channel->thread_id, channel->rdp, channel->wrp));
169 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
172 PRINT (("thread %#x: resetting space_available\n",
173 channel->thread_id));
174 ResetEvent (channel->space_avail_event);
175 PRINT (("thread %#x: waiting for space\n", channel->thread_id));
176 UNLOCK (channel->mutex);
177 WaitForSingleObject (channel->space_avail_event, INFINITE);
178 LOCK (channel->mutex);
179 PRINT (("thread %#x: rdp=%d, wrp=%d\n",
180 channel->thread_id, channel->rdp, channel->wrp));
183 buffer = channel->buffer + channel->wrp;
185 /* Always leave at least one byte unused gap to be able to
186 * distinguish between the full and empty condition...
188 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
189 BUFFER_SIZE - channel->wrp);
191 UNLOCK (channel->mutex);
193 nbytes = (*channel->reader) (channel->fd, buffer, nbytes);
198 LOCK (channel->mutex);
199 PRINT (("thread %#x: got %d bytes, rdp=%d, wrp=%d\n",
200 channel->thread_id, nbytes, channel->rdp, channel->wrp));
201 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
202 PRINT (("thread %#x: rdp=%d, wrp=%d, setting data available\n",
203 channel->thread_id, channel->rdp, channel->wrp));
204 SetEvent (channel->data_avail_event);
205 UNLOCK (channel->mutex);
208 LOCK (channel->mutex);
209 channel->running = FALSE;
210 PRINT (("thread %#x: got EOF, rdp=%d, wrp=%d, setting data available\n",
211 channel->thread_id, channel->rdp, channel->wrp));
212 SetEvent (channel->data_avail_event);
213 UNLOCK (channel->mutex);
215 g_io_channel_unref((GIOChannel *) channel);
217 /* All of the Microsoft docs say we should explicitly
226 create_reader_thread (GIOWin32Channel *channel,
229 channel->reader = reader;
231 if (_beginthreadex (NULL, 0, reader_thread, channel, 0,
232 &channel->thread_id) == 0)
233 g_warning ("Error creating reader thread: %s", strerror (errno));
234 WaitForSingleObject (channel->space_avail_event, INFINITE);
238 buffer_read (GIOWin32Channel *channel,
246 LOCK (channel->mutex);
247 PRINT (("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
248 channel->thread_id, count, channel->rdp, channel->wrp));
252 if (channel->rdp == channel->wrp)
254 UNLOCK (channel->mutex);
255 PRINT (("waiting for data from thread %#x\n", channel->thread_id));
256 WaitForSingleObject (channel->data_avail_event, INFINITE);
257 LOCK (channel->mutex);
258 if (channel->rdp == channel->wrp && !channel->running)
262 if (channel->rdp < channel->wrp)
263 nbytes = channel->wrp - channel->rdp;
265 nbytes = BUFFER_SIZE - channel->rdp;
266 UNLOCK (channel->mutex);
267 nbytes = MIN (left, nbytes);
268 PRINT (("moving %d bytes from thread %#x\n",
269 nbytes, channel->thread_id));
270 memcpy (dest, channel->buffer + channel->rdp, nbytes);
273 LOCK (channel->mutex);
274 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
275 PRINT (("setting space available for thread %#x\n", channel->thread_id));
276 SetEvent (channel->space_avail_event);
277 PRINT (("for thread %#x: rdp=%d, wrp=%d\n",
278 channel->thread_id, channel->rdp, channel->wrp));
279 if (channel->running && channel->rdp == channel->wrp)
281 PRINT (("resetting data_available of thread %#x\n",
282 channel->thread_id));
283 ResetEvent (channel->data_avail_event);
286 UNLOCK (channel->mutex);
292 g_io_win32_prepare (gpointer source_data,
293 GTimeVal *current_time,
303 g_io_win32_check (gpointer source_data,
304 GTimeVal *current_time,
307 GIOWin32Watch *data = source_data;
308 GIOWin32Channel *channel = (GIOWin32Channel *) data->channel;
310 /* If the thread has died, we have encountered EOF. If the buffer
311 * also is emtpty set the HUP bit.
313 if (!channel->running && channel->rdp == channel->wrp)
315 PRINT (("g_io_win32_check: setting G_IO_HUP thread %#x rdp=%d wrp=%d\n", channel->thread_id, channel->rdp, channel->wrp));
316 data->pollfd.revents |= G_IO_HUP;
320 return (data->pollfd.revents & data->condition);
324 g_io_win32_dispatch (gpointer source_data,
325 GTimeVal *current_time,
329 GIOWin32Watch *data = source_data;
331 return (*data->callback) (data->channel,
332 data->pollfd.revents & data->condition,
337 g_io_win32_destroy (gpointer source_data)
339 GIOWin32Watch *data = source_data;
341 g_main_remove_poll (&data->pollfd);
342 g_io_channel_unref (data->channel);
346 static GSourceFuncs win32_watch_funcs = {
354 g_io_win32_msg_read (GIOChannel *channel,
359 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
360 MSG msg; /* In case of alignment problems */
362 if (count < sizeof (MSG))
363 return G_IO_ERROR_INVAL;
365 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
366 return G_IO_ERROR_AGAIN;
368 memmove (buf, &msg, sizeof (MSG));
369 *bytes_read = sizeof (MSG);
370 return G_IO_ERROR_NONE;
374 g_io_win32_msg_write (GIOChannel *channel,
377 guint *bytes_written)
379 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
382 if (count != sizeof (MSG))
383 return G_IO_ERROR_INVAL;
385 /* In case of alignment problems */
386 memmove (&msg, buf, sizeof (MSG));
387 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
388 return G_IO_ERROR_UNKNOWN;
390 *bytes_written = sizeof (MSG);
391 return G_IO_ERROR_NONE;
395 g_io_win32_no_seek (GIOChannel *channel,
399 return G_IO_ERROR_UNKNOWN;
404 g_io_win32_msg_close (GIOChannel *channel)
406 /* Nothing to be done. Or should we set hwnd to some invalid value? */
410 g_io_win32_free (GIOChannel *channel)
412 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
414 if (win32_channel->buffer)
416 CloseHandle (win32_channel->data_avail_event);
417 CloseHandle (win32_channel->space_avail_event);
418 DeleteCriticalSection (&win32_channel->mutex);
421 g_free (win32_channel->buffer);
422 g_free (win32_channel);
426 g_io_win32_msg_add_watch (GIOChannel *channel,
428 GIOCondition condition,
431 GDestroyNotify notify)
433 GIOWin32Watch *watch = g_new (GIOWin32Watch, 1);
435 watch->channel = channel;
436 g_io_channel_ref (channel);
438 watch->callback = func;
439 watch->condition = condition;
441 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
442 watch->pollfd.events = condition;
444 g_main_add_poll (&watch->pollfd, priority);
446 return g_source_add (priority, TRUE, &win32_watch_funcs,
447 watch, user_data, notify);
451 g_io_win32_fd_read (GIOChannel *channel,
456 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
460 if (win32_channel->thread_id)
462 result = buffer_read (win32_channel, buf, count, &error);
470 *bytes_read = result;
471 return G_IO_ERROR_NONE;
475 result = read (win32_channel->fd, buf, count);
481 return G_IO_ERROR_INVAL;
483 return G_IO_ERROR_UNKNOWN;
487 *bytes_read = result;
488 return G_IO_ERROR_NONE;
493 g_io_win32_fd_write(GIOChannel *channel,
496 guint *bytes_written)
498 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
501 result = write (win32_channel->fd, buf, count);
502 PRINT (("g_io_win32_fd_write: fd:%d count:%d = %d\n", win32_channel->fd, count, result));
510 return G_IO_ERROR_INVAL;
512 return G_IO_ERROR_AGAIN;
514 return G_IO_ERROR_UNKNOWN;
519 *bytes_written = result;
520 return G_IO_ERROR_NONE;
525 g_io_win32_fd_seek (GIOChannel *channel,
529 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
545 g_warning ("g_io_win32_fd_seek: unknown seek type");
546 return G_IO_ERROR_UNKNOWN;
549 result = lseek (win32_channel->fd, offset, whence);
556 return G_IO_ERROR_INVAL;
558 return G_IO_ERROR_UNKNOWN;
562 return G_IO_ERROR_NONE;
566 g_io_win32_fd_close (GIOChannel *channel)
568 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
570 close (win32_channel->fd);
581 value = read (fd, buf, len);
583 PRINT (("fd_reader (%d,%p,%d) = %d\n", fd, buf, len, value));
589 g_io_win32_fd_add_watch (GIOChannel *channel,
591 GIOCondition condition,
594 GDestroyNotify notify)
596 GIOWin32Watch *watch = g_new (GIOWin32Watch, 1);
597 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
601 watch->channel = channel;
602 g_io_channel_ref (channel);
604 watch->callback = func;
605 watch->condition = condition;
607 create_events (win32_channel);
609 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
610 watch->pollfd.events = condition;
612 PRINT (("g_io_win32_fd_add_watch: fd:%d handle:%#x\n",
613 win32_channel->fd, watch->pollfd.fd));
615 /* Is it readable? (Would be strange to watch it otherwise, but... */
616 if (ReadFile ((HANDLE) _get_osfhandle (win32_channel->fd),
617 dummy, 0, &nbytes, NULL))
618 create_reader_thread (win32_channel, fd_reader);
620 g_main_add_poll (&watch->pollfd, priority);
622 return g_source_add (priority, TRUE, &win32_watch_funcs,
623 watch, user_data, notify);
627 g_io_win32_sock_read (GIOChannel *channel,
632 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
636 if (win32_channel->thread_id)
638 result = buffer_read (win32_channel, buf, count, &error);
646 *bytes_read = result;
647 return G_IO_ERROR_NONE;
651 result = recv (win32_channel->fd, buf, count, 0);
656 return G_IO_ERROR_UNKNOWN;
660 *bytes_read = result;
661 return G_IO_ERROR_NONE;
666 g_io_win32_sock_write(GIOChannel *channel,
669 guint *bytes_written)
671 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
674 result = send (win32_channel->fd, buf, count, 0);
676 if (result == SOCKET_ERROR)
679 switch (WSAGetLastError ())
682 return G_IO_ERROR_INVAL;
685 return G_IO_ERROR_AGAIN;
687 return G_IO_ERROR_UNKNOWN;
692 *bytes_written = result;
693 return G_IO_ERROR_NONE;
698 g_io_win32_sock_close (GIOChannel *channel)
700 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
702 closesocket (win32_channel->fd);
710 return recv (fd, buf, len, 0);
714 g_io_win32_sock_add_watch (GIOChannel *channel,
716 GIOCondition condition,
719 GDestroyNotify notify)
721 GIOWin32Watch *watch = g_new (GIOWin32Watch, 1);
722 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
724 watch->channel = channel;
725 g_io_channel_ref (channel);
727 watch->callback = func;
728 watch->condition = condition;
730 create_events (win32_channel);
732 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
733 watch->pollfd.events = condition;
735 /* Sockets are always readable, aren't they? */
736 create_reader_thread (win32_channel, sock_reader);
738 g_main_add_poll (&watch->pollfd, priority);
740 return g_source_add (priority, TRUE, &win32_watch_funcs, watch,
744 static GIOFuncs win32_channel_msg_funcs = {
746 g_io_win32_msg_write,
748 g_io_win32_msg_close,
749 g_io_win32_msg_add_watch,
753 static GIOFuncs win32_channel_fd_funcs = {
758 g_io_win32_fd_add_watch,
762 static GIOFuncs win32_channel_sock_funcs = {
763 g_io_win32_sock_read,
764 g_io_win32_sock_write,
766 g_io_win32_sock_close,
767 g_io_win32_sock_add_watch,
772 g_io_channel_win32_new_messages (guint hwnd)
774 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
775 GIOChannel *channel = (GIOChannel *) win32_channel;
777 g_io_channel_init (channel);
778 channel->funcs = &win32_channel_msg_funcs;
779 win32_channel->type = G_IO_WINDOWS_MESSAGES;
780 win32_channel->hwnd = (HWND) hwnd;
786 g_io_channel_win32_new_fd (gint fd)
788 GIOWin32Channel *win32_channel;
792 if (fstat (fd, &st) == -1)
794 g_warning ("%d isn't a (emulated) file descriptor", fd);
798 PRINT (("g_io_channel_win32_new_fd: %d\n", fd));
800 win32_channel = g_new (GIOWin32Channel, 1);
801 channel = (GIOChannel *) win32_channel;
803 g_io_channel_init (channel);
805 channel->funcs = &win32_channel_fd_funcs;
806 win32_channel->fd = fd;
807 win32_channel->type = G_IO_FILE_DESC;
808 win32_channel->buffer = NULL;
809 win32_channel->running = FALSE;
810 win32_channel->thread_id = 0;
816 g_io_channel_win32_get_fd (GIOChannel *channel)
818 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
820 return win32_channel->fd;
824 g_io_channel_win32_new_stream_socket (int socket)
826 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
827 GIOChannel *channel = (GIOChannel *) win32_channel;
829 g_io_channel_init (channel);
830 channel->funcs = &win32_channel_sock_funcs;
831 win32_channel->fd = socket;
832 win32_channel->type = G_IO_STREAM_SOCKET;
833 win32_channel->buffer = NULL;
834 win32_channel->running = FALSE;
835 win32_channel->thread_id = 0;
841 g_io_channel_unix_new (gint fd)
843 return g_io_channel_win32_new_fd (fd);
847 g_io_channel_unix_get_fd (GIOChannel *channel)
849 return g_io_channel_win32_get_fd (channel);
853 g_io_channel_win32_wait_for_condition (GIOChannel *channel,
854 GIOCondition condition,
858 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
861 pollfd.fd = (gint) win32_channel->data_avail_event;
862 pollfd.events = condition;
864 PRINT (("g_io_channel_win32_wait_for_condition: fd:%d event:%#x timeout:%d\n",
865 win32_channel->fd, pollfd.fd, timeout));
867 result = (*g_main_win32_get_poll_func ()) (&pollfd, 1, timeout);
869 PRINT (("g_io_channel_win32_wait_for_condition: done:%d\n", result));
875 /* This variable and the functions below are present just to be
876 * binary compatible with old clients... But note that in GIMP, the
877 * libgimp/gimp.c:gimp_extension_process() function will have to be modified
878 * anyhow for this new approach.
880 * These will be removed after some weeks.
882 guint g_pipe_readable_msg = 0;
885 g_io_channel_win32_new_pipe (int fd)
887 return g_io_channel_win32_new_fd (fd);
891 g_io_channel_win32_new_pipe_with_wakeups (int fd,
895 return g_io_channel_win32_new_fd (fd);
899 g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
903 /* Nothing needed now */
907 g_io_channel_win32_pipe_readable (gint fd,
910 /* Nothing needed now */