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
94 HANDLE data_avail_event;
95 HANDLE space_avail_event;
96 CRITICAL_SECTION mutex;
98 /* Function that actually reads from fd */
99 int (*reader) (int fd, guchar *buf, int len);
102 #define LOCK(mutex) EnterCriticalSection (&mutex)
103 #define UNLOCK(mutex) LeaveCriticalSection (&mutex)
105 struct _GIOWin32Watch {
109 GIOCondition condition;
114 g_io_channel_win32_init (GIOWin32Channel *channel)
116 #ifdef G_IO_WIN32_DEBUG
117 channel->debug = TRUE;
119 if (getenv ("G_IO_WIN32_DEBUG") != NULL)
120 channel->debug = TRUE;
122 channel->debug = FALSE;
124 channel->buffer = NULL;
125 channel->running = FALSE;
126 channel->thread_id = 0;
127 channel->data_avail_event = NULL;
128 channel->space_avail_event = NULL;
129 InitializeCriticalSection (&channel->mutex);
133 create_events (GIOWin32Channel *channel)
135 SECURITY_ATTRIBUTES sec_attrs;
137 sec_attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
138 sec_attrs.lpSecurityDescriptor = NULL;
139 sec_attrs.bInheritHandle = FALSE;
141 /* The data available event is manual reset, the space available event
142 * is automatic reset.
144 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
145 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
147 gchar *msg = g_win32_error_message (GetLastError ());
148 g_error ("Error creating event: %s", msg);
152 static unsigned __stdcall
153 reader_thread (void *parameter)
155 GIOWin32Channel *channel = parameter;
159 g_io_channel_ref ((GIOChannel *) channel);
162 g_print ("thread %#x: starting. pid:%#x, fd:%d, data_avail:%#x, space_avail:%#x\n",
164 (guint) GetCurrentProcessId (),
166 (guint) channel->data_avail_event,
167 (guint) channel->space_avail_event);
169 channel->buffer = g_malloc (BUFFER_SIZE);
170 channel->rdp = channel->wrp = 0;
171 channel->running = TRUE;
173 SetEvent (channel->space_avail_event);
175 while (channel->running)
177 LOCK (channel->mutex);
179 g_print ("thread %#x: rdp=%d, wrp=%d\n",
180 channel->thread_id, channel->rdp, channel->wrp);
181 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
185 g_print ("thread %#x: resetting space_available\n",
187 ResetEvent (channel->space_avail_event);
189 g_print ("thread %#x: waiting for space\n", channel->thread_id);
190 UNLOCK (channel->mutex);
191 WaitForSingleObject (channel->space_avail_event, INFINITE);
192 LOCK (channel->mutex);
194 g_print ("thread %#x: rdp=%d, wrp=%d\n",
195 channel->thread_id, channel->rdp, channel->wrp);
198 buffer = channel->buffer + channel->wrp;
200 /* Always leave at least one byte unused gap to be able to
201 * distinguish between the full and empty condition...
203 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
204 BUFFER_SIZE - channel->wrp);
207 g_print ("thread %#x: calling reader for %d bytes\n",
208 channel->thread_id, nbytes);
210 UNLOCK (channel->mutex);
212 nbytes = (*channel->reader) (channel->fd, buffer, nbytes);
214 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);
223 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
225 g_print ("thread %#x: rdp=%d, wrp=%d, setting data available\n",
226 channel->thread_id, channel->rdp, channel->wrp);
227 SetEvent (channel->data_avail_event);
228 UNLOCK (channel->mutex);
231 channel->running = FALSE;
233 g_print ("thread %#x: got EOF, rdp=%d, wrp=%d, setting data available\n",
234 channel->thread_id, channel->rdp, channel->wrp);
235 SetEvent (channel->data_avail_event);
236 UNLOCK (channel->mutex);
238 g_io_channel_unref((GIOChannel *) channel);
241 /* All of the Microsoft docs say we should explicitly
247 CloseHandle (channel->thread_handle);
253 create_reader_thread (GIOWin32Channel *channel,
256 channel->reader = reader;
259 if ((channel->thread_handle =
260 _beginthreadex (NULL, 0, reader_thread, channel, 0,
261 &channel->thread_id)) == 0)
262 g_warning ("Error creating reader thread: %s", strerror (errno));
264 if ((channel->thread_handle =
265 CreateThread (NULL, 0, reader_thread, channel, 0,
266 &channel->thread_id)) == 0)
268 gchar *msg = g_win32_error_message (GetLastError ());
269 g_warning ("Error creating reader thread: %s", msg);
273 WaitForSingleObject (channel->space_avail_event, INFINITE);
277 buffer_read (GIOWin32Channel *channel,
285 LOCK (channel->mutex);
287 g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
288 channel->thread_id, count, channel->rdp, channel->wrp);
290 if (channel->rdp == channel->wrp)
292 UNLOCK (channel->mutex);
294 g_print ("waiting for data from thread %#x\n", channel->thread_id);
295 WaitForSingleObject (channel->data_avail_event, INFINITE);
297 g_print ("done waiting for data from thread %#x\n", channel->thread_id);
298 LOCK (channel->mutex);
299 if (channel->rdp == channel->wrp && !channel->running)
301 UNLOCK (channel->mutex);
306 if (channel->rdp < channel->wrp)
307 nbytes = channel->wrp - channel->rdp;
309 nbytes = BUFFER_SIZE - channel->rdp;
310 UNLOCK (channel->mutex);
311 nbytes = MIN (left, nbytes);
313 g_print ("moving %d bytes from thread %#x\n",
314 nbytes, channel->thread_id);
315 memcpy (dest, channel->buffer + channel->rdp, nbytes);
318 LOCK (channel->mutex);
319 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
321 g_print ("setting space available for thread %#x\n", channel->thread_id);
322 SetEvent (channel->space_avail_event);
324 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
325 channel->thread_id, channel->rdp, channel->wrp);
326 if (channel->running && channel->rdp == channel->wrp)
329 g_print ("resetting data_available of thread %#x\n",
331 ResetEvent (channel->data_avail_event);
333 UNLOCK (channel->mutex);
335 /* We have no way to indicate any errors form the actual
336 * read() or recv() call in the reader thread. Should we have?
338 *error = G_IO_ERROR_NONE;
343 g_io_win32_prepare (GSource *source,
352 g_io_win32_check (GSource *source)
354 GIOWin32Watch *watch = (GIOWin32Watch *)source;
355 GIOWin32Channel *channel = (GIOWin32Channel *) watch->channel;
357 /* If the thread has died, we have encountered EOF. If the buffer
358 * also is emtpty set the HUP bit.
360 if (!channel->running && channel->rdp == channel->wrp)
363 g_print ("g_io_win32_check: setting G_IO_HUP thread %#x rdp=%d wrp=%d\n",
364 channel->thread_id, channel->rdp, channel->wrp);
365 watch->pollfd.revents |= G_IO_HUP;
369 return (watch->pollfd.revents & watch->condition);
373 g_io_win32_dispatch (GSource *source,
374 GSourceFunc callback,
377 GIOFunc func = (GIOFunc)callback;
378 GIOWin32Watch *watch = (GIOWin32Watch *)source;
382 g_warning ("GIOWin32Watch dispatched without callback\n"
383 "You must call g_source_connect().");
387 return (*func) (watch->channel,
388 watch->pollfd.revents & watch->condition,
393 g_io_win32_destroy (GSource *source)
395 GIOWin32Watch *watch = (GIOWin32Watch *)source;
397 g_io_channel_unref (watch->channel);
400 static GSourceFuncs win32_watch_funcs = {
408 g_io_win32_create_watch (GIOChannel *channel,
409 GIOCondition condition,
410 int (*reader) (int, guchar *, int))
412 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
413 GIOWin32Watch *watch;
416 source = g_source_new (&win32_watch_funcs, sizeof (GIOWin32Watch));
417 watch = (GIOWin32Watch *)source;
419 watch->channel = channel;
420 g_io_channel_ref (channel);
422 watch->condition = condition;
424 if (win32_channel->data_avail_event == NULL)
425 create_events (win32_channel);
427 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
428 watch->pollfd.events = condition;
430 if (win32_channel->debug)
431 g_print ("g_io_win32_create_watch: fd:%d handle:%#x\n",
432 win32_channel->fd, watch->pollfd.fd);
434 if (win32_channel->thread_id == 0)
435 create_reader_thread (win32_channel, reader);
437 g_source_add_poll (source, &watch->pollfd);
443 g_io_win32_msg_read (GIOChannel *channel,
448 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
449 MSG msg; /* In case of alignment problems */
451 if (count < sizeof (MSG))
452 return G_IO_ERROR_INVAL;
454 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
455 return G_IO_ERROR_AGAIN;
457 memmove (buf, &msg, sizeof (MSG));
458 *bytes_read = sizeof (MSG);
459 return G_IO_ERROR_NONE;
463 g_io_win32_msg_write (GIOChannel *channel,
466 guint *bytes_written)
468 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
471 if (count != sizeof (MSG))
472 return G_IO_ERROR_INVAL;
474 /* In case of alignment problems */
475 memmove (&msg, buf, sizeof (MSG));
476 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
477 return G_IO_ERROR_UNKNOWN;
479 *bytes_written = sizeof (MSG);
480 return G_IO_ERROR_NONE;
484 g_io_win32_no_seek (GIOChannel *channel,
488 return G_IO_ERROR_UNKNOWN;
492 g_io_win32_msg_close (GIOChannel *channel)
494 /* Nothing to be done. Or should we set hwnd to some invalid value? */
498 g_io_win32_free (GIOChannel *channel)
500 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
502 if (win32_channel->debug)
503 g_print ("thread %#x: freeing channel, fd: %d\n",
504 win32_channel->thread_id,
507 if (win32_channel->buffer)
509 CloseHandle (win32_channel->data_avail_event);
510 CloseHandle (win32_channel->space_avail_event);
511 DeleteCriticalSection (&win32_channel->mutex);
514 g_free (win32_channel->buffer);
515 g_free (win32_channel);
519 g_io_win32_msg_create_watch (GIOChannel *channel,
520 GIOCondition condition)
522 GIOWin32Watch *watch;
525 source = g_source_new (&win32_watch_funcs, sizeof (GIOWin32Watch));
526 watch = (GIOWin32Watch *)source;
528 watch->channel = channel;
529 g_io_channel_ref (channel);
531 watch->condition = condition;
533 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
534 watch->pollfd.events = condition;
536 g_source_add_poll (source, &watch->pollfd);
542 g_io_win32_fd_read (GIOChannel *channel,
547 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
551 if (win32_channel->debug)
552 g_print ("g_io_win32_fd_read: fd:%d count:%d\n",
553 win32_channel->fd, count);
555 if (win32_channel->thread_id)
557 result = buffer_read (win32_channel, buf, count, &error);
565 *bytes_read = result;
566 return G_IO_ERROR_NONE;
570 result = read (win32_channel->fd, buf, count);
576 return G_IO_ERROR_INVAL;
578 return G_IO_ERROR_UNKNOWN;
582 *bytes_read = result;
583 return G_IO_ERROR_NONE;
588 g_io_win32_fd_write (GIOChannel *channel,
591 guint *bytes_written)
593 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
596 result = write (win32_channel->fd, buf, count);
597 if (win32_channel->debug)
598 g_print ("g_io_win32_fd_write: fd:%d count:%d = %d\n",
599 win32_channel->fd, count, result);
607 return G_IO_ERROR_INVAL;
609 return G_IO_ERROR_AGAIN;
611 return G_IO_ERROR_UNKNOWN;
616 *bytes_written = result;
617 return G_IO_ERROR_NONE;
622 g_io_win32_fd_seek (GIOChannel *channel,
626 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
642 g_warning ("g_io_win32_fd_seek: unknown seek type");
643 return G_IO_ERROR_UNKNOWN;
646 result = lseek (win32_channel->fd, offset, whence);
653 return G_IO_ERROR_INVAL;
655 return G_IO_ERROR_UNKNOWN;
659 return G_IO_ERROR_NONE;
663 g_io_win32_fd_close (GIOChannel *channel)
665 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
667 if (win32_channel->debug)
668 g_print ("thread %#x: closing fd %d\n",
669 win32_channel->thread_id,
671 LOCK (win32_channel->mutex);
672 if (win32_channel->running)
674 if (win32_channel->debug)
675 g_print ("thread %#x: running, terminating it\n",
676 win32_channel->thread_id);
677 TerminateThread (win32_channel->thread_handle, 0);
678 if (win32_channel->debug)
679 g_print ("thread %#x: terminated, setting id to 0, closing handle\n",
680 win32_channel->thread_id);
681 win32_channel->thread_id = 0;
682 CloseHandle (win32_channel->thread_handle);
683 win32_channel->running = FALSE;
684 SetEvent (win32_channel->data_avail_event);
686 UNLOCK (win32_channel->mutex);
687 if (win32_channel->debug)
688 g_print ("closing fd %d\n", win32_channel->fd);
689 close (win32_channel->fd);
690 if (win32_channel->debug)
691 g_print ("closed fd %d, setting to -1\n",
693 win32_channel->fd = -1;
701 return read (fd, buf, len);
705 g_io_win32_fd_create_watch (GIOChannel *channel,
706 GIOCondition condition)
708 return g_io_win32_create_watch (channel, condition, fd_reader);
712 g_io_win32_sock_read (GIOChannel *channel,
717 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
721 if (win32_channel->thread_id)
723 result = buffer_read (win32_channel, buf, count, &error);
731 *bytes_read = result;
732 return G_IO_ERROR_NONE;
736 result = recv (win32_channel->fd, buf, count, 0);
741 return G_IO_ERROR_UNKNOWN;
745 *bytes_read = result;
746 return G_IO_ERROR_NONE;
751 g_io_win32_sock_write (GIOChannel *channel,
754 guint *bytes_written)
756 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
759 result = send (win32_channel->fd, buf, count, 0);
761 if (result == SOCKET_ERROR)
764 switch (WSAGetLastError ())
767 return G_IO_ERROR_INVAL;
770 return G_IO_ERROR_AGAIN;
772 return G_IO_ERROR_UNKNOWN;
777 *bytes_written = result;
778 return G_IO_ERROR_NONE;
783 g_io_win32_sock_close (GIOChannel *channel)
785 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
787 if (win32_channel->debug)
788 g_print ("thread %#x: closing socket %d\n",
789 win32_channel->thread_id,
791 closesocket (win32_channel->fd);
792 win32_channel->fd = -1;
800 return recv (fd, buf, len, 0);
804 g_io_win32_sock_create_watch (GIOChannel *channel,
805 GIOCondition condition)
807 return g_io_win32_create_watch (channel, condition, sock_reader);
810 static GIOFuncs win32_channel_msg_funcs = {
812 g_io_win32_msg_write,
814 g_io_win32_msg_close,
815 g_io_win32_msg_create_watch,
819 static GIOFuncs win32_channel_fd_funcs = {
824 g_io_win32_fd_create_watch,
828 static GIOFuncs win32_channel_sock_funcs = {
829 g_io_win32_sock_read,
830 g_io_win32_sock_write,
832 g_io_win32_sock_close,
833 g_io_win32_sock_create_watch,
838 g_io_channel_win32_new_messages (guint hwnd)
840 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
841 GIOChannel *channel = (GIOChannel *) win32_channel;
843 g_io_channel_init (channel);
844 g_io_channel_win32_init (win32_channel);
845 channel->funcs = &win32_channel_msg_funcs;
846 win32_channel->type = G_IO_WINDOWS_MESSAGES;
847 win32_channel->hwnd = (HWND) hwnd;
853 g_io_channel_win32_new_fd (gint fd)
855 GIOWin32Channel *win32_channel;
859 if (fstat (fd, &st) == -1)
861 g_warning ("%d isn't a (emulated) file descriptor", fd);
865 win32_channel = g_new (GIOWin32Channel, 1);
866 channel = (GIOChannel *) win32_channel;
868 g_io_channel_init (channel);
869 g_io_channel_win32_init (win32_channel);
870 channel->funcs = &win32_channel_fd_funcs;
871 win32_channel->type = G_IO_FILE_DESC;
872 win32_channel->fd = fd;
878 g_io_channel_win32_get_fd (GIOChannel *channel)
880 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
882 return win32_channel->fd;
886 g_io_channel_win32_new_stream_socket (int socket)
888 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
889 GIOChannel *channel = (GIOChannel *) win32_channel;
891 g_io_channel_init (channel);
892 g_io_channel_win32_init (win32_channel);
893 channel->funcs = &win32_channel_sock_funcs;
894 win32_channel->type = G_IO_STREAM_SOCKET;
895 win32_channel->fd = socket;
901 g_io_channel_unix_new (gint fd)
905 if (fstat (fd, &st) == 0)
906 return g_io_channel_win32_new_fd (fd);
908 if (getsockopt (fd, SOL_SOCKET, SO_TYPE, NULL, NULL) != SO_ERROR)
909 return g_io_channel_win32_new_stream_socket(fd);
911 g_warning ("%d isn't a file descriptor or a socket", fd);
916 g_io_channel_unix_get_fd (GIOChannel *channel)
918 return g_io_channel_win32_get_fd (channel);
922 g_io_channel_win32_set_debug (GIOChannel *channel,
925 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
927 win32_channel->debug = flag;
931 g_io_channel_win32_poll (GPollFD *fds,
937 g_return_val_if_fail (n_fds >= 0, 0);
939 result = (*g_main_context_get_poll_func (NULL)) (fds, n_fds, timeout);
945 g_io_channel_win32_make_pollfd (GIOChannel *channel,
946 GIOCondition condition,
949 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
951 if (win32_channel->data_avail_event == NULL)
952 create_events (win32_channel);
954 fd->fd = (gint) win32_channel->data_avail_event;
955 fd->events = condition;
957 if (win32_channel->thread_id == 0)
958 if (win32_channel->type == G_IO_FILE_DESC)
959 create_reader_thread (win32_channel, fd_reader);
960 else if (win32_channel->type == G_IO_STREAM_SOCKET)
961 create_reader_thread (win32_channel, sock_reader);
964 /* This variable and the functions below are present just to be
965 * binary compatible with old clients... But note that in GIMP, the
966 * libgimp/gimp.c:gimp_extension_process() function will have to be modified
967 * anyhow for this new approach.
969 * These will be removed after some weeks.
971 guint g_pipe_readable_msg = 0;
974 g_io_channel_win32_new_pipe (int fd)
976 return g_io_channel_win32_new_fd (fd);
980 g_io_channel_win32_new_pipe_with_wakeups (int fd,
984 return g_io_channel_win32_new_fd (fd);
988 g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
992 /* Nothing needed now */
996 g_io_channel_win32_pipe_readable (gint fd,
999 /* Nothing needed now */