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 GIOFunc func = (GIOFunc)callback;
358 GIOWin32Watch *watch = (GIOWin32Watch *)source;
362 g_warning ("GIOWin32Watch dispatched without callback\n"
363 "You must call g_source_connect().");
367 return (*func) (watch->channel,
368 watch->pollfd.revents & watch->condition,
373 g_io_win32_destroy (GSource *source)
375 GIOWin32Watch *watch = (GIOWin32Watch *)source;
377 g_io_channel_unref (watch->channel);
380 static GSourceFuncs win32_watch_funcs = {
388 g_io_win32_create_watch (GIOChannel *channel,
389 GIOCondition condition,
390 int (*reader) (int, guchar *, int))
392 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
393 GIOWin32Watch *watch;
396 source = g_source_new (&win32_watch_funcs, sizeof (GIOWin32Watch));
397 watch = (GIOWin32Watch *)source;
399 watch->channel = channel;
400 g_io_channel_ref (channel);
402 watch->condition = condition;
404 if (win32_channel->data_avail_event == NULL)
405 create_events (win32_channel);
407 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
408 watch->pollfd.events = condition;
410 if (win32_channel->debug)
411 g_print ("g_io_win32_create_watch: fd:%d handle:%#x\n",
412 win32_channel->fd, watch->pollfd.fd);
414 if (win32_channel->thread_id == 0)
415 create_reader_thread (win32_channel, reader);
417 g_source_add_poll (source, &watch->pollfd);
423 g_io_win32_msg_read (GIOChannel *channel,
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;
443 g_io_win32_msg_write (GIOChannel *channel,
446 guint *bytes_written)
448 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
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;
464 g_io_win32_no_seek (GIOChannel *channel,
468 return G_IO_ERROR_UNKNOWN;
472 g_io_win32_msg_close (GIOChannel *channel)
474 /* Nothing to be done. Or should we set hwnd to some invalid value? */
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);
494 g_io_win32_msg_create_watch (GIOChannel *channel,
495 GIOCondition condition)
497 GIOWin32Watch *watch;
500 source = g_source_new (&win32_watch_funcs, sizeof (GIOWin32Watch));
501 watch = (GIOWin32Watch *)source;
503 watch->channel = channel;
504 g_io_channel_ref (channel);
506 watch->condition = condition;
508 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
509 watch->pollfd.events = condition;
511 g_source_add_poll (source, &watch->pollfd);
517 g_io_win32_fd_read (GIOChannel *channel,
522 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
526 if (win32_channel->debug)
527 g_print ("g_io_win32_fd_read: fd:%d count:%d\n",
528 win32_channel->fd, count);
530 if (win32_channel->thread_id)
532 result = buffer_read (win32_channel, buf, count, &error);
540 *bytes_read = result;
541 return G_IO_ERROR_NONE;
545 result = read (win32_channel->fd, buf, count);
551 return G_IO_ERROR_INVAL;
553 return G_IO_ERROR_UNKNOWN;
557 *bytes_read = result;
558 return G_IO_ERROR_NONE;
563 g_io_win32_fd_write (GIOChannel *channel,
566 guint *bytes_written)
568 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
571 result = write (win32_channel->fd, buf, count);
572 if (win32_channel->debug)
573 g_print ("g_io_win32_fd_write: fd:%d count:%d = %d\n",
574 win32_channel->fd, count, result);
582 return G_IO_ERROR_INVAL;
584 return G_IO_ERROR_AGAIN;
586 return G_IO_ERROR_UNKNOWN;
591 *bytes_written = result;
592 return G_IO_ERROR_NONE;
597 g_io_win32_fd_seek (GIOChannel *channel,
601 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
617 g_warning ("g_io_win32_fd_seek: unknown seek type");
618 return G_IO_ERROR_UNKNOWN;
621 result = lseek (win32_channel->fd, offset, whence);
628 return G_IO_ERROR_INVAL;
630 return G_IO_ERROR_UNKNOWN;
634 return G_IO_ERROR_NONE;
638 g_io_win32_fd_close (GIOChannel *channel)
640 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
642 close (win32_channel->fd);
651 return read (fd, buf, len);
655 g_io_win32_fd_create_watch (GIOChannel *channel,
656 GIOCondition condition)
658 return g_io_win32_create_watch (channel, condition, fd_reader);
662 g_io_win32_sock_read (GIOChannel *channel,
667 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
671 if (win32_channel->thread_id)
673 result = buffer_read (win32_channel, buf, count, &error);
681 *bytes_read = result;
682 return G_IO_ERROR_NONE;
686 result = recv (win32_channel->fd, buf, count, 0);
691 return G_IO_ERROR_UNKNOWN;
695 *bytes_read = result;
696 return G_IO_ERROR_NONE;
701 g_io_win32_sock_write (GIOChannel *channel,
704 guint *bytes_written)
706 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
709 result = send (win32_channel->fd, buf, count, 0);
711 if (result == SOCKET_ERROR)
714 switch (WSAGetLastError ())
717 return G_IO_ERROR_INVAL;
720 return G_IO_ERROR_AGAIN;
722 return G_IO_ERROR_UNKNOWN;
727 *bytes_written = result;
728 return G_IO_ERROR_NONE;
733 g_io_win32_sock_close (GIOChannel *channel)
735 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
737 closesocket (win32_channel->fd);
745 return recv (fd, buf, len, 0);
749 g_io_win32_sock_create_watch (GIOChannel *channel,
750 GIOCondition condition)
752 return g_io_win32_create_watch (channel, condition, sock_reader);
755 static GIOFuncs win32_channel_msg_funcs = {
757 g_io_win32_msg_write,
759 g_io_win32_msg_close,
760 g_io_win32_msg_create_watch,
764 static GIOFuncs win32_channel_fd_funcs = {
769 g_io_win32_fd_create_watch,
773 static GIOFuncs win32_channel_sock_funcs = {
774 g_io_win32_sock_read,
775 g_io_win32_sock_write,
777 g_io_win32_sock_close,
778 g_io_win32_sock_create_watch,
783 g_io_channel_win32_new_messages (guint hwnd)
785 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
786 GIOChannel *channel = (GIOChannel *) win32_channel;
788 g_io_channel_init (channel);
789 g_io_channel_win32_init (win32_channel);
790 channel->funcs = &win32_channel_msg_funcs;
791 win32_channel->type = G_IO_WINDOWS_MESSAGES;
792 win32_channel->hwnd = (HWND) hwnd;
798 g_io_channel_win32_new_fd (gint fd)
800 GIOWin32Channel *win32_channel;
804 if (fstat (fd, &st) == -1)
806 g_warning ("%d isn't a (emulated) file descriptor", fd);
810 win32_channel = g_new (GIOWin32Channel, 1);
811 channel = (GIOChannel *) win32_channel;
813 g_io_channel_init (channel);
814 g_io_channel_win32_init (win32_channel);
815 channel->funcs = &win32_channel_fd_funcs;
816 win32_channel->type = G_IO_FILE_DESC;
817 win32_channel->fd = fd;
823 g_io_channel_win32_get_fd (GIOChannel *channel)
825 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
827 return win32_channel->fd;
831 g_io_channel_win32_new_stream_socket (int socket)
833 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
834 GIOChannel *channel = (GIOChannel *) win32_channel;
836 g_io_channel_init (channel);
837 g_io_channel_win32_init (win32_channel);
838 channel->funcs = &win32_channel_sock_funcs;
839 win32_channel->type = G_IO_STREAM_SOCKET;
840 win32_channel->fd = socket;
846 g_io_channel_unix_new (gint fd)
850 if (fstat (fd, &st) == 0)
851 return g_io_channel_win32_new_fd (fd);
853 if (getsockopt (fd, SOL_SOCKET, SO_TYPE, NULL, NULL) != SO_ERROR)
854 return g_io_channel_win32_new_stream_socket(fd);
856 g_warning ("%d isn't a file descriptor or a socket", fd);
861 g_io_channel_unix_get_fd (GIOChannel *channel)
863 return g_io_channel_win32_get_fd (channel);
867 g_io_channel_win32_set_debug (GIOChannel *channel,
870 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
872 win32_channel->debug = flag;
876 g_io_channel_win32_poll (GPollFD *fds,
882 g_return_val_if_fail (n_fds >= 0, 0);
884 result = (*g_main_context_get_poll_func (NULL)) (fds, n_fds, timeout);
890 g_io_channel_win32_make_pollfd (GIOChannel *channel,
891 GIOCondition condition,
894 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
896 if (win32_channel->data_avail_event == NULL)
897 create_events (win32_channel);
899 fd->fd = (gint) win32_channel->data_avail_event;
900 fd->events = condition;
902 if (win32_channel->thread_id == 0)
903 if (win32_channel->type == G_IO_FILE_DESC)
904 create_reader_thread (win32_channel, fd_reader);
905 else if (win32_channel->type == G_IO_STREAM_SOCKET)
906 create_reader_thread (win32_channel, sock_reader);
909 /* This variable and the functions below are present just to be
910 * binary compatible with old clients... But note that in GIMP, the
911 * libgimp/gimp.c:gimp_extension_process() function will have to be modified
912 * anyhow for this new approach.
914 * These will be removed after some weeks.
916 guint g_pipe_readable_msg = 0;
919 g_io_channel_win32_new_pipe (int fd)
921 return g_io_channel_win32_new_fd (fd);
925 g_io_channel_win32_new_pipe_with_wakeups (int fd,
929 return g_io_channel_win32_new_fd (fd);
933 g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
937 /* Nothing needed now */
941 g_io_channel_win32_pipe_readable (gint fd,
944 /* Nothing needed now */