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);
205 g_print ("thread %#x: calling reader for %d bytes\n",
206 channel->thread_id, nbytes);
208 UNLOCK (channel->mutex);
210 nbytes = (*channel->reader) (channel->fd, buffer, nbytes);
215 LOCK (channel->mutex);
217 g_print ("thread %#x: got %d bytes, rdp=%d, wrp=%d\n",
218 channel->thread_id, nbytes, channel->rdp, channel->wrp);
219 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
221 g_print ("thread %#x: rdp=%d, wrp=%d, setting data available\n",
222 channel->thread_id, channel->rdp, channel->wrp);
223 SetEvent (channel->data_avail_event);
224 UNLOCK (channel->mutex);
227 LOCK (channel->mutex);
228 channel->running = FALSE;
230 g_print ("thread %#x: got EOF, rdp=%d, wrp=%d, setting data available\n",
231 channel->thread_id, channel->rdp, channel->wrp);
232 SetEvent (channel->data_avail_event);
233 UNLOCK (channel->mutex);
235 g_io_channel_unref((GIOChannel *) channel);
237 /* All of the Microsoft docs say we should explicitly
246 create_reader_thread (GIOWin32Channel *channel,
249 channel->reader = reader;
251 if (_beginthreadex (NULL, 0, reader_thread, channel, 0,
252 &channel->thread_id) == 0)
253 g_warning ("Error creating reader thread: %s", strerror (errno));
254 WaitForSingleObject (channel->space_avail_event, INFINITE);
258 buffer_read (GIOWin32Channel *channel,
266 LOCK (channel->mutex);
268 g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
269 channel->thread_id, count, channel->rdp, channel->wrp);
271 if (channel->rdp == channel->wrp)
273 UNLOCK (channel->mutex);
275 g_print ("waiting for data from thread %#x\n", channel->thread_id);
276 WaitForSingleObject (channel->data_avail_event, INFINITE);
277 LOCK (channel->mutex);
278 if (channel->rdp == channel->wrp && !channel->running)
280 UNLOCK (channel->mutex);
285 if (channel->rdp < channel->wrp)
286 nbytes = channel->wrp - channel->rdp;
288 nbytes = BUFFER_SIZE - channel->rdp;
289 UNLOCK (channel->mutex);
290 nbytes = MIN (left, nbytes);
292 g_print ("moving %d bytes from thread %#x\n",
293 nbytes, channel->thread_id);
294 memcpy (dest, channel->buffer + channel->rdp, nbytes);
297 LOCK (channel->mutex);
298 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
300 g_print ("setting space available for thread %#x\n", channel->thread_id);
301 SetEvent (channel->space_avail_event);
303 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
304 channel->thread_id, channel->rdp, channel->wrp);
305 if (channel->running && channel->rdp == channel->wrp)
308 g_print ("resetting data_available of thread %#x\n",
310 ResetEvent (channel->data_avail_event);
312 UNLOCK (channel->mutex);
314 /* We have no way to indicate any errors form the actual
315 * read() or recv() call in the reader thread. Should we have?
317 *error = G_IO_ERROR_NONE;
322 g_io_win32_prepare (gpointer source_data,
323 GTimeVal *current_time,
333 g_io_win32_check (gpointer source_data,
334 GTimeVal *current_time,
337 GIOWin32Watch *data = source_data;
338 GIOWin32Channel *channel = (GIOWin32Channel *) data->channel;
340 /* If the thread has died, we have encountered EOF. If the buffer
341 * also is emtpty set the HUP bit.
343 if (!channel->running && channel->rdp == channel->wrp)
346 g_print ("g_io_win32_check: setting G_IO_HUP thread %#x rdp=%d wrp=%d\n",
347 channel->thread_id, channel->rdp, channel->wrp);
348 data->pollfd.revents |= G_IO_HUP;
352 return (data->pollfd.revents & data->condition);
356 g_io_win32_dispatch (gpointer source_data,
357 GTimeVal *current_time,
361 GIOWin32Watch *data = source_data;
363 return (*data->callback) (data->channel,
364 data->pollfd.revents & data->condition,
369 g_io_win32_destroy (gpointer source_data)
371 GIOWin32Watch *data = source_data;
373 g_main_remove_poll (&data->pollfd);
374 g_io_channel_unref (data->channel);
378 static GSourceFuncs win32_watch_funcs = {
386 g_io_win32_add_watch (GIOChannel *channel,
388 GIOCondition condition,
391 GDestroyNotify notify,
392 int (*reader) (int, guchar *, int))
394 GIOWin32Watch *watch = g_new (GIOWin32Watch, 1);
395 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
397 watch->channel = channel;
398 g_io_channel_ref (channel);
400 watch->callback = func;
401 watch->condition = condition;
403 if (win32_channel->data_avail_event == NULL)
404 create_events (win32_channel);
406 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
407 watch->pollfd.events = condition;
409 if (win32_channel->debug)
410 g_print ("g_io_win32_add_watch: fd:%d handle:%#x\n",
411 win32_channel->fd, watch->pollfd.fd);
413 if (win32_channel->thread_id == 0)
414 create_reader_thread (win32_channel, reader);
416 g_main_add_poll (&watch->pollfd, priority);
418 return g_source_add (priority, TRUE, &win32_watch_funcs, watch,
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_add_watch (GIOChannel *channel,
496 GIOCondition condition,
499 GDestroyNotify notify)
501 GIOWin32Watch *watch = g_new (GIOWin32Watch, 1);
503 watch->channel = channel;
504 g_io_channel_ref (channel);
506 watch->callback = func;
507 watch->condition = condition;
509 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
510 watch->pollfd.events = condition;
512 g_main_add_poll (&watch->pollfd, priority);
514 return g_source_add (priority, TRUE, &win32_watch_funcs,
515 watch, user_data, notify);
519 g_io_win32_fd_read (GIOChannel *channel,
524 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
528 if (win32_channel->debug)
529 g_print ("g_io_win32_fd_read: fd:%d count:%d\n",
530 win32_channel->fd, count);
532 if (win32_channel->thread_id)
534 result = buffer_read (win32_channel, buf, count, &error);
542 *bytes_read = result;
543 return G_IO_ERROR_NONE;
547 result = read (win32_channel->fd, buf, count);
553 return G_IO_ERROR_INVAL;
555 return G_IO_ERROR_UNKNOWN;
559 *bytes_read = result;
560 return G_IO_ERROR_NONE;
565 g_io_win32_fd_write (GIOChannel *channel,
568 guint *bytes_written)
570 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
573 result = write (win32_channel->fd, buf, count);
574 if (win32_channel->debug)
575 g_print ("g_io_win32_fd_write: fd:%d count:%d = %d\n",
576 win32_channel->fd, count, result);
584 return G_IO_ERROR_INVAL;
586 return G_IO_ERROR_AGAIN;
588 return G_IO_ERROR_UNKNOWN;
593 *bytes_written = result;
594 return G_IO_ERROR_NONE;
599 g_io_win32_fd_seek (GIOChannel *channel,
603 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
619 g_warning ("g_io_win32_fd_seek: unknown seek type");
620 return G_IO_ERROR_UNKNOWN;
623 result = lseek (win32_channel->fd, offset, whence);
630 return G_IO_ERROR_INVAL;
632 return G_IO_ERROR_UNKNOWN;
636 return G_IO_ERROR_NONE;
640 g_io_win32_fd_close (GIOChannel *channel)
642 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
644 close (win32_channel->fd);
653 return read (fd, buf, len);
657 g_io_win32_fd_add_watch (GIOChannel *channel,
659 GIOCondition condition,
662 GDestroyNotify notify)
664 return g_io_win32_add_watch (channel, priority, condition,
665 func, user_data, notify, fd_reader);
669 g_io_win32_sock_read (GIOChannel *channel,
674 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
678 if (win32_channel->thread_id)
680 result = buffer_read (win32_channel, buf, count, &error);
688 *bytes_read = result;
689 return G_IO_ERROR_NONE;
693 result = recv (win32_channel->fd, buf, count, 0);
698 return G_IO_ERROR_UNKNOWN;
702 *bytes_read = result;
703 return G_IO_ERROR_NONE;
708 g_io_win32_sock_write (GIOChannel *channel,
711 guint *bytes_written)
713 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
716 result = send (win32_channel->fd, buf, count, 0);
718 if (result == SOCKET_ERROR)
721 switch (WSAGetLastError ())
724 return G_IO_ERROR_INVAL;
727 return G_IO_ERROR_AGAIN;
729 return G_IO_ERROR_UNKNOWN;
734 *bytes_written = result;
735 return G_IO_ERROR_NONE;
740 g_io_win32_sock_close (GIOChannel *channel)
742 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
744 closesocket (win32_channel->fd);
752 return recv (fd, buf, len, 0);
756 g_io_win32_sock_add_watch (GIOChannel *channel,
758 GIOCondition condition,
761 GDestroyNotify notify)
763 return g_io_win32_add_watch (channel, priority, condition,
764 func, user_data, notify, sock_reader);
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_poll (GPollFD *fds,
885 g_return_val_if_fail (n_fds >= 0, 0);
887 result = (*g_main_win32_get_poll_func ()) (fds, n_fds, timeout);
893 g_io_channel_win32_make_pollfd (GIOChannel *channel,
894 GIOCondition condition,
897 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
899 if (win32_channel->data_avail_event == NULL)
900 create_events (win32_channel);
902 fd->fd = (gint) win32_channel->data_avail_event;
903 fd->events = condition;
905 if (win32_channel->thread_id == 0)
906 if (win32_channel->type == G_IO_FILE_DESC)
907 create_reader_thread (win32_channel, fd_reader);
908 else if (win32_channel->type == G_IO_STREAM_SOCKET)
909 create_reader_thread (win32_channel, sock_reader);
912 /* This variable and the functions below are present just to be
913 * binary compatible with old clients... But note that in GIMP, the
914 * libgimp/gimp.c:gimp_extension_process() function will have to be modified
915 * anyhow for this new approach.
917 * These will be removed after some weeks.
919 guint g_pipe_readable_msg = 0;
922 g_io_channel_win32_new_pipe (int fd)
924 return g_io_channel_win32_new_fd (fd);
928 g_io_channel_win32_new_pipe_with_wakeups (int fd,
932 return g_io_channel_win32_new_fd (fd);
936 g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
940 /* Nothing needed now */
944 g_io_channel_win32_pipe_readable (gint fd,
947 /* Nothing needed now */