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
7 * Copyright 2001 Andrew Lanoix
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
27 * file for a list of people on the GLib Team. See the ChangeLog
28 * files for a list of changes. These files are distributed with
29 * GLib at ftp://ftp.gtk.org/pub/gtk/.
32 /* Define this to get (very) verbose logging of all channels */
33 /* #define G_IO_WIN32_DEBUG */
41 #include <winsock.h> /* Not everybody has winsock2 */
50 typedef struct _GIOWin32Channel GIOWin32Channel;
51 typedef struct _GIOWin32Watch GIOWin32Watch;
53 #define BUFFER_SIZE 4096
56 G_IO_WIN32_WINDOWS_MESSAGES, /* Windows messages */
57 G_IO_WIN32_FILE_DESC, /* Unix-like file descriptors from
58 * _open() or _pipe(). Read with read().
59 * Have to create separate thread to read.
61 G_IO_WIN32_SOCKET /* Sockets. A separate thread is blocked
62 * in select() most of the time.
64 } GIOWin32ChannelType;
66 struct _GIOWin32Channel {
68 gint fd; /* Either a Unix-like file handle as provided
69 * by the Microsoft C runtime, or a SOCKET
70 * as provided by WinSock.
72 GIOWin32ChannelType type;
76 CRITICAL_SECTION mutex;
78 /* This is used by G_IO_WIN32_WINDOWS_MESSAGES channels */
79 HWND hwnd; /* handle of window, or NULL */
81 /* Following fields are used by both fd and socket channels. */
82 gboolean running; /* Is reader thread running. FALSE if
83 * EOF has been reached.
85 gboolean needs_close; /* If the channel has been closed while
86 * the reader thread was still running.
88 guint thread_id; /* If non-NULL has a reader thread, or has
91 HANDLE data_avail_event;
95 /* Following fields used by fd channels for input */
97 /* Data is kept in a circular buffer. To be able to distinguish between
98 * empty and full buffer, we cannot fill it completely, but have to
99 * leave a one character gap.
101 * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
104 * Full: (wrp + 1) % BUFFER_SIZE == rdp
107 guchar *buffer; /* (Circular) buffer */
108 gint wrp, rdp; /* Buffer indices for writing and reading */
109 HANDLE space_avail_event;
111 /* Following fields used by socket channels */
113 HANDLE data_avail_noticed_event;
116 #define LOCK(mutex) EnterCriticalSection (&mutex)
117 #define UNLOCK(mutex) LeaveCriticalSection (&mutex)
119 struct _GIOWin32Watch {
123 GIOCondition condition;
127 g_win32_print_access_mode (int flags)
129 g_print ("%s%s%s%s%s%s%s%s%s%s",
130 ((flags & 0x3) == _O_RDWR ? "O_RDWR" :
131 ((flags & 0x3) == _O_RDONLY ? "O_RDONLY" :
132 ((flags & 0x3) == _O_WRONLY ? "O_WRONLY" : "0"))),
133 (flags & _O_APPEND ? "|O_APPEND" : ""),
134 (flags & _O_RANDOM ? "|O_RANDOM" : ""),
135 (flags & _O_SEQUENTIAL ? "|O_SEQUENTIAL" : ""),
136 (flags & _O_TEMPORARY ? "|O_TEMPORARY" : ""),
137 (flags & _O_CREAT ? "|O_CREAT" : ""),
138 (flags & _O_TRUNC ? "|O_TRUNC" : ""),
139 (flags & _O_EXCL ? "|O_EXCL" : ""),
140 (flags & _O_TEXT ? "|O_TEXT" : ""),
141 (flags & _O_BINARY ? "|O_BINARY" : ""));
145 g_win32_print_gioflags (GIOFlags flags)
149 if (flags & G_IO_FLAG_APPEND)
150 bar = "|", g_print ("APPEND");
151 if (flags & G_IO_FLAG_NONBLOCK)
152 g_print ("%sNONBLOCK", bar), bar = "|";
153 if (flags & G_IO_FLAG_IS_READABLE)
154 g_print ("%sREADABLE", bar), bar = "|";
155 if (flags & G_IO_FLAG_IS_WRITEABLE)
156 g_print ("%sWRITEABLE", bar), bar = "|";
157 if (flags & G_IO_FLAG_IS_WRITEABLE)
158 g_print ("%sWRITEABLE", bar), bar = "|";
159 if (flags & G_IO_FLAG_IS_SEEKABLE)
160 g_print ("%sSEEKABLE", bar), bar = "|";
164 g_io_win32_get_debug_flag (void)
166 #ifdef G_IO_WIN32_DEBUG
169 if (getenv ("G_IO_WIN32_DEBUG") != NULL)
177 g_io_channel_win32_init (GIOWin32Channel *channel)
179 channel->debug = g_io_win32_get_debug_flag ();
180 channel->buffer = NULL;
181 channel->running = FALSE;
182 channel->needs_close = FALSE;
183 channel->thread_id = 0;
184 channel->data_avail_event = NULL;
185 channel->revents = 0;
186 channel->space_avail_event = NULL;
187 channel->data_avail_noticed_event = NULL;
188 channel->watches = NULL;
189 InitializeCriticalSection (&channel->mutex);
193 create_events (GIOWin32Channel *channel)
195 SECURITY_ATTRIBUTES sec_attrs;
197 sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
198 sec_attrs.lpSecurityDescriptor = NULL;
199 sec_attrs.bInheritHandle = FALSE;
201 /* The data available event is manual reset, the space available event
202 * is automatic reset.
204 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
205 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL))
206 || !(channel->data_avail_noticed_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
208 gchar *emsg = g_win32_error_message (GetLastError ());
209 g_error ("Error creating event: %s", emsg);
214 static unsigned __stdcall
215 read_thread (void *parameter)
217 GIOWin32Channel *channel = parameter;
221 g_io_channel_ref ((GIOChannel *)channel);
224 g_print ("read_thread %#x: start fd:%d, data_avail:%#x space_avail:%#x\n",
227 (guint) channel->data_avail_event,
228 (guint) channel->space_avail_event);
230 channel->buffer = g_malloc (BUFFER_SIZE);
231 channel->rdp = channel->wrp = 0;
232 channel->running = TRUE;
234 SetEvent (channel->space_avail_event);
236 while (channel->running)
238 LOCK (channel->mutex);
240 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
241 channel->thread_id, channel->rdp, channel->wrp);
242 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
246 g_print ("read_thread %#x: resetting space_avail\n",
248 ResetEvent (channel->space_avail_event);
250 g_print ("read_thread %#x: waiting for space\n",
252 UNLOCK (channel->mutex);
253 WaitForSingleObject (channel->space_avail_event, INFINITE);
254 LOCK (channel->mutex);
256 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
257 channel->thread_id, channel->rdp, channel->wrp);
260 buffer = channel->buffer + channel->wrp;
262 /* Always leave at least one byte unused gap to be able to
263 * distinguish between the full and empty condition...
265 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
266 BUFFER_SIZE - channel->wrp);
269 g_print ("read_thread %#x: calling read() for %d bytes\n",
270 channel->thread_id, nbytes);
272 UNLOCK (channel->mutex);
274 nbytes = read (channel->fd, buffer, nbytes);
276 LOCK (channel->mutex);
278 channel->revents = G_IO_IN;
280 channel->revents |= G_IO_HUP;
282 channel->revents |= G_IO_ERR;
285 g_print ("read_thread %#x: read() returned %d, rdp=%d, wrp=%d\n",
286 channel->thread_id, nbytes, channel->rdp, channel->wrp);
291 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
293 g_print ("read_thread %#x: rdp=%d, wrp=%d, setting data_avail\n",
294 channel->thread_id, channel->rdp, channel->wrp);
295 SetEvent (channel->data_avail_event);
296 UNLOCK (channel->mutex);
299 channel->running = FALSE;
300 if (channel->needs_close)
303 g_print ("read_thread %#x: channel fd %d needs closing\n",
304 channel->thread_id, channel->fd);
310 g_print ("read_thread %#x: EOF, rdp=%d, wrp=%d, setting data_avail\n",
311 channel->thread_id, channel->rdp, channel->wrp);
312 SetEvent (channel->data_avail_event);
313 UNLOCK (channel->mutex);
315 g_io_channel_unref((GIOChannel *)channel);
317 /* No need to call _endthreadex(), the actual thread starter routine
318 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
319 * _endthreadex() for us.
322 CloseHandle (channel->thread_handle);
328 create_thread (GIOWin32Channel *channel,
329 GIOCondition condition,
330 unsigned (__stdcall *thread) (void *parameter))
332 channel->thread_handle =
333 (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0,
334 &channel->thread_id);
335 if (channel->thread_handle == 0)
336 g_warning (G_STRLOC ": Error creating reader thread: %s",
338 WaitForSingleObject (channel->space_avail_event, INFINITE);
342 buffer_read (GIOWin32Channel *channel,
351 LOCK (channel->mutex);
353 g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
354 channel->thread_id, count, channel->rdp, channel->wrp);
356 if (channel->wrp == channel->rdp)
358 UNLOCK (channel->mutex);
360 g_print ("waiting for data from thread %#x\n", channel->thread_id);
361 WaitForSingleObject (channel->data_avail_event, INFINITE);
363 g_print ("done waiting for data from thread %#x\n", channel->thread_id);
364 LOCK (channel->mutex);
365 if (channel->wrp == channel->rdp && !channel->running)
368 g_print ("wrp==rdp, !running\n");
369 UNLOCK (channel->mutex);
371 return G_IO_STATUS_EOF;
375 if (channel->rdp < channel->wrp)
376 nbytes = channel->wrp - channel->rdp;
378 nbytes = BUFFER_SIZE - channel->rdp;
379 UNLOCK (channel->mutex);
380 nbytes = MIN (left, nbytes);
382 g_print ("moving %d bytes from thread %#x\n",
383 nbytes, channel->thread_id);
384 memcpy (dest, channel->buffer + channel->rdp, nbytes);
387 LOCK (channel->mutex);
388 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
390 g_print ("setting space_avail for thread %#x\n", channel->thread_id);
391 SetEvent (channel->space_avail_event);
393 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
394 channel->thread_id, channel->rdp, channel->wrp);
395 if (channel->running && channel->wrp == channel->rdp)
398 g_print ("resetting data_avail of thread %#x\n",
400 ResetEvent (channel->data_avail_event);
402 UNLOCK (channel->mutex);
404 /* We have no way to indicate any errors form the actual
405 * read() or recv() call in the reader thread. Should we have?
407 *bytes_read = count - left;
408 return (*bytes_read > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
411 static unsigned __stdcall
412 select_thread (void *parameter)
414 GIOWin32Channel *channel = parameter;
415 fd_set read_fds, write_fds, except_fds;
419 g_io_channel_ref ((GIOChannel *)channel);
422 g_print ("select_thread %#x: start fd:%d data_avail:%#x data_avail_noticed:%#x\n",
425 (guint) channel->data_avail_event,
426 (guint) channel->data_avail_noticed_event);
428 channel->rdp = channel->wrp = 0;
429 channel->running = TRUE;
431 SetEvent (channel->space_avail_event);
433 while (channel->running)
436 FD_ZERO (&write_fds);
437 FD_ZERO (&except_fds);
439 tmp = channel->watches;
442 GIOWin32Watch *watch = (GIOWin32Watch *)tmp->data;
444 if (watch->condition & (G_IO_IN | G_IO_HUP))
445 FD_SET (channel->fd, &read_fds);
446 if (watch->condition & G_IO_OUT)
447 FD_SET (channel->fd, &write_fds);
448 if (watch->condition & G_IO_ERR)
449 FD_SET (channel->fd, &except_fds);
454 g_print ("select_thread %#x: calling select() for%s%s%s\n",
456 (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
457 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
458 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
460 n = select (1, &read_fds, &write_fds, &except_fds, NULL);
462 if (n == SOCKET_ERROR)
465 g_print ("select_thread %#x: select returned SOCKET_ERROR\n",
471 g_print ("select_thread %#x: got%s%s%s\n",
473 (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
474 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
475 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
477 if (FD_ISSET (channel->fd, &read_fds))
478 channel->revents |= G_IO_IN;
479 if (FD_ISSET (channel->fd, &write_fds))
480 channel->revents |= G_IO_OUT;
481 if (FD_ISSET (channel->fd, &except_fds))
482 channel->revents |= G_IO_ERR;
485 g_print ("select_thread %#x: resetting data_avail_noticed, setting data_avail\n",
487 ResetEvent (channel->data_avail_noticed_event);
488 SetEvent (channel->data_avail_event);
490 LOCK (channel->mutex);
491 if (channel->needs_close)
493 UNLOCK (channel->mutex);
496 UNLOCK (channel->mutex);
499 g_print ("select_thread %#x: waiting for data_avail_noticed\n",
502 WaitForSingleObject (channel->data_avail_noticed_event, INFINITE);
504 g_print ("select_thread %#x: got data_avail_noticed\n",
508 channel->running = FALSE;
509 LOCK (channel->mutex);
510 if (channel->fd != -1)
512 /* DO NOT close the fd here */
517 g_print ("select_thread %#x: got error, setting data_avail\n",
519 SetEvent (channel->data_avail_event);
520 UNLOCK (channel->mutex);
522 g_io_channel_unref((GIOChannel *)channel);
524 /* No need to call _endthreadex(), the actual thread starter routine
525 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
526 * _endthreadex() for us.
529 CloseHandle (channel->thread_handle);
535 g_io_win32_prepare (GSource *source,
538 GIOWin32Watch *watch = (GIOWin32Watch *)source;
539 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
540 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
545 g_print ("g_io_win32_prepare: for thread %#x buffer_condition:%#x\n"
546 " watch->pollfd.events:%#x watch->pollfd.revents:%#x channel->revents:%#x\n",
547 channel->thread_id, buffer_condition,
548 watch->pollfd.events, watch->pollfd.revents, channel->revents);
550 if (channel->type == G_IO_WIN32_FILE_DESC)
552 LOCK (channel->mutex);
553 if (channel->running && channel->wrp == channel->rdp)
556 g_print ("g_io_win32_prepare: for thread %#x, setting channel->revents = 0\n",
558 channel->revents = 0;
560 UNLOCK (channel->mutex);
562 else if (channel->type == G_IO_WIN32_SOCKET)
564 channel->revents = 0;
567 g_print ("g_io_win32_prepare: for thread %#x, setting data_avail_noticed\n",
569 SetEvent (channel->data_avail_noticed_event);
571 g_print ("g_io_win32_prepare: thread %#x, there.\n",
575 return ((watch->condition & buffer_condition) == watch->condition);
579 g_io_win32_check (GSource *source)
582 GIOWin32Watch *watch = (GIOWin32Watch *)source;
583 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
584 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
587 g_print ("g_io_win32_check: for thread %#x buffer_condition:%#x\n"
588 " watch->pollfd.events:%#x watch->pollfd.revents:%#x channel->revents:%#x\n",
589 channel->thread_id, buffer_condition,
590 watch->pollfd.events, watch->pollfd.revents, channel->revents);
592 if (channel->type != G_IO_WIN32_WINDOWS_MESSAGES)
594 watch->pollfd.revents = (watch->pollfd.events & channel->revents);
598 return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
601 if (channel->type == G_IO_WIN32_SOCKET)
604 g_print ("g_io_win32_check: thread %#x, resetting data_avail\n",
606 ResetEvent (channel->data_avail_event);
608 g_print ("g_io_win32_check: thread %#x, there.\n",
612 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
616 g_io_win32_dispatch (GSource *source,
617 GSourceFunc callback,
620 GIOFunc func = (GIOFunc)callback;
621 GIOWin32Watch *watch = (GIOWin32Watch *)source;
622 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
626 g_warning (G_STRLOC ": GIOWin32Watch dispatched without callback\n"
627 "You must call g_source_connect().");
631 return (*func) (watch->channel,
632 (watch->pollfd.revents | buffer_condition) & watch->condition,
637 g_io_win32_finalize (GSource *source)
639 GIOWin32Watch *watch = (GIOWin32Watch *)source;
640 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
643 g_print ("g_io_win32_finalize: channel with thread %#x\n",
646 channel->watches = g_slist_remove (channel->watches, watch);
648 SetEvent (channel->data_avail_noticed_event);
649 g_io_channel_unref (watch->channel);
652 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
653 __declspec(dllexport)
655 GSourceFuncs g_io_watch_funcs = {
663 g_io_win32_create_watch (GIOChannel *channel,
664 GIOCondition condition,
665 unsigned (__stdcall *thread) (void *parameter))
667 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
668 GIOWin32Watch *watch;
671 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
672 watch = (GIOWin32Watch *)source;
674 watch->channel = channel;
675 g_io_channel_ref (channel);
677 watch->condition = condition;
679 if (win32_channel->data_avail_event == NULL)
680 create_events (win32_channel);
682 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
683 watch->pollfd.events = condition;
685 if (win32_channel->debug)
686 g_print ("g_io_win32_create_watch: fd:%d condition:%#x handle:%#x\n",
687 win32_channel->fd, condition, watch->pollfd.fd);
689 win32_channel->watches = g_slist_append (win32_channel->watches, watch);
691 if (win32_channel->thread_id == 0)
692 create_thread (win32_channel, condition, thread);
694 g_source_add_poll (source, &watch->pollfd);
700 g_io_win32_msg_read (GIOChannel *channel,
706 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
707 MSG msg; /* In case of alignment problems */
709 if (count < sizeof (MSG))
711 g_set_error(err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
712 _("Incorrect message size")); /* Informative enough error message? */
713 return G_IO_STATUS_ERROR;
716 if (win32_channel->debug)
717 g_print ("g_io_win32_msg_read: for %#x\n",
718 (guint) win32_channel->hwnd);
719 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
720 return G_IO_STATUS_AGAIN;
722 memmove (buf, &msg, sizeof (MSG));
723 *bytes_read = sizeof (MSG);
725 return G_IO_STATUS_NORMAL;
729 g_io_win32_msg_write (GIOChannel *channel,
732 gsize *bytes_written,
735 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
738 if (count != sizeof (MSG))
740 g_set_error(err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
741 _("Incorrect message size")); /* Informative enough error message? */
742 return G_IO_STATUS_ERROR;
745 /* In case of alignment problems */
746 memmove (&msg, buf, sizeof (MSG));
747 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
749 gchar *emsg = g_win32_error_message (GetLastError ());
750 g_set_error(err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, emsg);
752 return G_IO_STATUS_ERROR;
755 *bytes_written = sizeof (MSG);
757 return G_IO_STATUS_NORMAL;
761 g_io_win32_no_seek (GIOChannel *channel,
766 g_assert_not_reached ();
768 return G_IO_STATUS_ERROR;
772 g_io_win32_msg_close (GIOChannel *channel,
775 /* Nothing to be done. Or should we set hwnd to some invalid value? */
777 return G_IO_STATUS_NORMAL;
781 g_io_win32_free (GIOChannel *channel)
783 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
785 if (win32_channel->debug)
786 g_print ("thread %#x: freeing channel, fd: %d\n",
787 win32_channel->thread_id,
790 if (win32_channel->data_avail_event)
791 CloseHandle (win32_channel->data_avail_event);
792 if (win32_channel->space_avail_event)
793 CloseHandle (win32_channel->space_avail_event);
794 if (win32_channel->data_avail_noticed_event)
795 CloseHandle (win32_channel->data_avail_noticed_event);
796 DeleteCriticalSection (&win32_channel->mutex);
798 g_free (win32_channel->buffer);
799 g_slist_free (win32_channel->watches);
800 g_free (win32_channel);
804 g_io_win32_msg_create_watch (GIOChannel *channel,
805 GIOCondition condition)
807 GIOWin32Watch *watch;
810 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
811 watch = (GIOWin32Watch *)source;
813 watch->channel = channel;
814 g_io_channel_ref (channel);
816 watch->condition = condition;
818 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
819 watch->pollfd.events = condition;
821 g_source_add_poll (source, &watch->pollfd);
827 g_io_win32_fd_read (GIOChannel *channel,
833 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
836 if (win32_channel->debug)
837 g_print ("g_io_win32_fd_read: fd:%d count:%d\n",
838 win32_channel->fd, count);
840 if (win32_channel->thread_id)
842 return buffer_read (win32_channel, buf, count, bytes_read, err);
845 result = read (win32_channel->fd, buf, count);
847 if (win32_channel->debug)
848 g_print ("g_io_win32_fd_read: read() = %d\n", result);
858 return G_IO_STATUS_AGAIN;
861 g_set_error (err, G_IO_CHANNEL_ERROR,
862 g_io_channel_error_from_errno (errno),
864 return G_IO_STATUS_ERROR;
868 *bytes_read = result;
870 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
874 g_io_win32_fd_write (GIOChannel *channel,
877 gsize *bytes_written,
880 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
883 result = write (win32_channel->fd, buf, count);
884 if (win32_channel->debug)
885 g_print ("g_io_win32_fd_write: fd:%d count:%d = %d\n",
886 win32_channel->fd, count, result);
896 return G_IO_STATUS_AGAIN;
899 g_set_error (err, G_IO_CHANNEL_ERROR,
900 g_io_channel_error_from_errno (errno),
902 return G_IO_STATUS_ERROR;
906 *bytes_written = result;
908 return G_IO_STATUS_NORMAL;
912 g_io_win32_fd_seek (GIOChannel *channel,
917 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
934 whence = -1; /* Keep the compiler quiet */
935 g_assert_not_reached();
939 if (tmp_offset != offset)
941 g_set_error (err, G_IO_CHANNEL_ERROR,
942 g_io_channel_error_from_errno (EINVAL),
943 g_strerror (EINVAL));
944 return G_IO_STATUS_ERROR;
947 result = lseek (win32_channel->fd, tmp_offset, whence);
951 g_set_error (err, G_IO_CHANNEL_ERROR,
952 g_io_channel_error_from_errno (errno),
954 return G_IO_STATUS_ERROR;
957 return G_IO_STATUS_NORMAL;
961 g_io_win32_fd_close (GIOChannel *channel,
964 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
966 if (win32_channel->debug)
967 g_print ("thread %#x: closing fd %d\n",
968 win32_channel->thread_id,
970 LOCK (win32_channel->mutex);
971 if (win32_channel->running)
973 if (win32_channel->debug)
974 g_print ("thread %#x: running, marking fd %d for later close\n",
975 win32_channel->thread_id, win32_channel->fd);
976 win32_channel->running = FALSE;
977 win32_channel->needs_close = TRUE;
978 SetEvent (win32_channel->data_avail_event);
982 if (win32_channel->debug)
983 g_print ("closing fd %d\n", win32_channel->fd);
984 close (win32_channel->fd);
985 if (win32_channel->debug)
986 g_print ("closed fd %d, setting to -1\n",
988 win32_channel->fd = -1;
990 UNLOCK (win32_channel->mutex);
992 /* FIXME error detection? */
994 return G_IO_STATUS_NORMAL;
998 g_io_win32_fd_create_watch (GIOChannel *channel,
999 GIOCondition condition)
1001 return g_io_win32_create_watch (channel, condition, read_thread);
1005 g_io_win32_sock_read (GIOChannel *channel,
1011 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1013 GIOChannelError error;
1015 if (win32_channel->debug)
1016 g_print ("g_io_win32_sock_read: sockfd:%d count:%d\n",
1017 win32_channel->fd, count);
1018 #ifdef WE_NEED_TO_HANDLE_WSAEINTR
1021 result = recv (win32_channel->fd, buf, count, 0);
1023 if (win32_channel->debug)
1024 g_print ("g_io_win32_sock_read: recv:%d\n", result);
1026 if (result == SOCKET_ERROR)
1030 switch (WSAGetLastError ())
1033 error = G_IO_CHANNEL_ERROR_INVAL;
1035 case WSAEWOULDBLOCK:
1036 return G_IO_STATUS_AGAIN;
1037 #ifdef WE_NEED_TO_HANDLE_WSAEINTR /* not anymore with wsock2 ? */
1042 error = G_IO_CHANNEL_ERROR_FAILED;
1045 g_set_error(err, G_IO_CHANNEL_ERROR, error, _("Socket error"));
1046 return G_IO_STATUS_ERROR;
1047 /* FIXME get all errors, better error messages */
1051 *bytes_read = result;
1053 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
1058 g_io_win32_sock_write (GIOChannel *channel,
1061 gsize *bytes_written,
1064 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1066 GIOChannelError error;
1068 if (win32_channel->debug)
1069 g_print ("g_io_win32_sock_write: sockfd:%d count:%d\n",
1070 win32_channel->fd, count);
1071 #ifdef WE_NEED_TO_HANDLE_WSAEINTR
1074 result = send (win32_channel->fd, buf, count, 0);
1076 if (win32_channel->debug)
1077 g_print ("g_io_win32_sock_write: send:%d\n", result);
1079 if (result == SOCKET_ERROR)
1083 switch (WSAGetLastError ())
1086 error = G_IO_CHANNEL_ERROR_INVAL;
1088 case WSAEWOULDBLOCK:
1089 return G_IO_STATUS_AGAIN;
1090 #ifdef WE_NEED_TO_HANDLE_WSAEINTR /* not anymore with wsock2 ? */
1095 error = G_IO_CHANNEL_ERROR_FAILED;
1098 g_set_error(err, G_IO_CHANNEL_ERROR, error, _("Socket error"));
1099 return G_IO_STATUS_ERROR;
1100 /* FIXME get all errors, better error messages */
1104 *bytes_written = result;
1106 return G_IO_STATUS_NORMAL;
1111 g_io_win32_sock_close (GIOChannel *channel,
1114 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1116 LOCK(win32_channel->mutex);
1117 if (win32_channel->running)
1119 if (win32_channel->debug)
1120 g_print ("thread %#x: running, marking for later close\n",
1121 win32_channel->thread_id);
1122 win32_channel->running = FALSE;
1123 win32_channel->needs_close = TRUE;
1124 SetEvent(win32_channel->data_avail_noticed_event);
1126 if (win32_channel->fd != -1)
1128 if (win32_channel->debug)
1129 g_print ("thread %#x: closing socket %d\n",
1130 win32_channel->thread_id,
1133 closesocket (win32_channel->fd);
1134 win32_channel->fd = -1;
1136 UNLOCK(win32_channel->mutex);
1138 /* FIXME error detection? */
1140 return G_IO_STATUS_NORMAL;
1144 g_io_win32_sock_create_watch (GIOChannel *channel,
1145 GIOCondition condition)
1147 return g_io_win32_create_watch (channel, condition, select_thread);
1151 g_io_channel_new_file (const gchar *filename,
1155 int fid, flags, pmode;
1156 GIOChannel *channel;
1158 enum { /* Cheesy hack */
1165 g_return_val_if_fail (filename != NULL, NULL);
1166 g_return_val_if_fail (mode != NULL, NULL);
1167 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
1181 g_warning (G_STRLOC ": Invalid GIOFileMode %s.\n", mode);
1190 if (mode[2] == '\0')
1192 mode_num |= MODE_PLUS;
1197 g_warning (G_STRLOC ": Invalid GIOFileMode %s.\n", mode);
1208 flags = O_WRONLY | O_TRUNC | O_CREAT;
1212 flags = O_WRONLY | O_APPEND | O_CREAT;
1215 case MODE_R | MODE_PLUS:
1217 pmode = _S_IREAD | _S_IWRITE;
1219 case MODE_W | MODE_PLUS:
1220 flags = O_RDWR | O_TRUNC | O_CREAT;
1221 pmode = _S_IREAD | _S_IWRITE;
1223 case MODE_A | MODE_PLUS:
1224 flags = O_RDWR | O_APPEND | O_CREAT;
1225 pmode = _S_IREAD | _S_IWRITE;
1228 g_assert_not_reached ();
1233 /* always open 'untranslated' */
1234 fid = open (filename, flags | _O_BINARY, pmode);
1236 if (g_io_win32_get_debug_flag ())
1238 g_print ("g_io_channel_win32_new_file: open(\"%s\", ", filename);
1239 g_win32_print_access_mode (flags|_O_BINARY);
1240 g_print (",%#o)=%d\n", pmode, fid);
1245 g_set_error (error, G_FILE_ERROR,
1246 g_file_error_from_errno (errno),
1247 g_strerror (errno));
1248 return (GIOChannel *)NULL;
1251 channel = g_io_channel_win32_new_fd (fid);
1253 /* XXX: move this to g_io_channel_win32_new_fd () */
1254 channel->close_on_unref = TRUE;
1255 channel->is_seekable = TRUE;
1260 channel->is_readable = TRUE;
1261 channel->is_writeable = FALSE;
1265 channel->is_readable = FALSE;
1266 channel->is_writeable = TRUE;
1268 case MODE_R | MODE_PLUS:
1269 case MODE_W | MODE_PLUS:
1270 case MODE_A | MODE_PLUS:
1271 channel->is_readable = TRUE;
1272 channel->is_writeable = TRUE;
1275 g_assert_not_reached ();
1282 g_io_win32_set_flags (GIOChannel *channel,
1286 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1288 if (win32_channel->debug)
1290 g_print ("g_io_win32_set_flags: ");
1291 g_win32_print_gioflags (flags);
1297 g_file_error_from_errno (EACCES),
1298 _("Channel set flags unsupported"));
1299 return G_IO_STATUS_ERROR;
1303 g_io_win32_fd_get_flags (GIOChannel *channel)
1307 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1309 g_return_val_if_fail (win32_channel != NULL, 0);
1310 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_FILE_DESC, 0);
1312 if (0 == _fstat (win32_channel->fd, &st))
1314 /* XXX: G_IO_FLAG_APPEND */
1315 /* XXX: G_IO_FLAG_NONBLOCK */
1316 if (st.st_mode & _S_IREAD) flags |= G_IO_FLAG_IS_READABLE;
1317 if (st.st_mode & _S_IWRITE) flags |= G_IO_FLAG_IS_WRITEABLE;
1319 if (!(st.st_mode & _S_IFIFO)) flags |= G_IO_FLAG_IS_SEEKABLE;
1326 * Generic implementation, just translating createion flags
1329 g_io_win32_get_flags (GIOChannel *channel)
1333 flags = (channel->is_readable ? G_IO_FLAG_IS_READABLE : 0)
1334 | (channel->is_writeable ? G_IO_FLAG_IS_READABLE : 0)
1335 | (channel->is_seekable ? G_IO_FLAG_IS_SEEKABLE : 0);
1340 static GIOFuncs win32_channel_msg_funcs = {
1341 g_io_win32_msg_read,
1342 g_io_win32_msg_write,
1344 g_io_win32_msg_close,
1345 g_io_win32_msg_create_watch,
1347 g_io_win32_set_flags,
1348 g_io_win32_get_flags,
1351 static GIOFuncs win32_channel_fd_funcs = {
1353 g_io_win32_fd_write,
1355 g_io_win32_fd_close,
1356 g_io_win32_fd_create_watch,
1358 g_io_win32_set_flags,
1359 g_io_win32_fd_get_flags,
1362 static GIOFuncs win32_channel_sock_funcs = {
1363 g_io_win32_sock_read,
1364 g_io_win32_sock_write,
1366 g_io_win32_sock_close,
1367 g_io_win32_sock_create_watch,
1369 g_io_win32_set_flags,
1370 g_io_win32_get_flags,
1374 g_io_channel_win32_new_messages (guint hwnd)
1376 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1377 GIOChannel *channel = (GIOChannel *)win32_channel;
1379 g_io_channel_init (channel);
1380 g_io_channel_win32_init (win32_channel);
1381 if (win32_channel->debug)
1382 g_print ("g_io_channel_win32_new_messages: hwnd = %ud\n", hwnd);
1383 channel->funcs = &win32_channel_msg_funcs;
1384 win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
1385 win32_channel->hwnd = (HWND) hwnd;
1387 /* XXX: check this. */
1388 channel->is_readable = IsWindow (win32_channel->hwnd);
1389 channel->is_writeable = IsWindow (win32_channel->hwnd);
1391 channel->is_seekable = FALSE;
1397 g_io_channel_win32_new_fd (gint fd)
1399 GIOWin32Channel *win32_channel;
1400 GIOChannel *channel;
1403 if (fstat (fd, &st) == -1)
1405 g_warning (G_STRLOC ": %d isn't a (emulated) file descriptor", fd);
1409 win32_channel = g_new (GIOWin32Channel, 1);
1410 channel = (GIOChannel *)win32_channel;
1412 g_io_channel_init (channel);
1413 g_io_channel_win32_init (win32_channel);
1414 if (win32_channel->debug)
1415 g_print ("g_io_channel_win32_new_fd: %u\n", fd);
1416 channel->funcs = &win32_channel_fd_funcs;
1417 win32_channel->type = G_IO_WIN32_FILE_DESC;
1418 win32_channel->fd = fd;
1421 /* fstat doesn't deliver senseful values, but
1422 * fcntl isn't available, so guess ...
1424 if (st.st_mode & _S_IFIFO)
1426 channel->is_readable = TRUE;
1427 channel->is_writeable = TRUE;
1428 channel->is_seekable = FALSE;
1432 channel->is_readable = !!(st.st_mode & _S_IREAD);
1433 channel->is_writeable = !!(st.st_mode & _S_IWRITE);
1434 /* XXX What about "device files" (COM1: and the like) */
1435 channel->is_seekable = TRUE;
1442 g_io_channel_win32_get_fd (GIOChannel *channel)
1444 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1446 return win32_channel->fd;
1450 g_io_channel_win32_new_socket (int socket)
1452 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1453 GIOChannel *channel = (GIOChannel *)win32_channel;
1455 g_io_channel_init (channel);
1456 g_io_channel_win32_init (win32_channel);
1457 if (win32_channel->debug)
1458 g_print ("g_io_channel_win32_new_socket: sockfd:%d\n", socket);
1459 channel->funcs = &win32_channel_sock_funcs;
1460 win32_channel->type = G_IO_WIN32_SOCKET;
1461 win32_channel->fd = socket;
1463 /* XXX: check this */
1464 channel->is_readable = TRUE;
1465 channel->is_writeable = TRUE;
1466 channel->is_seekable = FALSE;
1472 g_io_channel_unix_new (gint fd)
1476 if (fstat (fd, &st) == 0)
1477 return g_io_channel_win32_new_fd (fd);
1479 if (getsockopt (fd, SOL_SOCKET, SO_TYPE, NULL, NULL) != SO_ERROR)
1480 return g_io_channel_win32_new_socket(fd);
1482 g_warning (G_STRLOC ": %d is neither a file descriptor or a socket", fd);
1487 g_io_channel_unix_get_fd (GIOChannel *channel)
1489 return g_io_channel_win32_get_fd (channel);
1493 g_io_channel_win32_set_debug (GIOChannel *channel,
1496 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1498 win32_channel->debug = flag;
1502 g_io_channel_win32_poll (GPollFD *fds,
1508 g_return_val_if_fail (n_fds >= 0, 0);
1510 result = (*g_main_context_get_poll_func (NULL)) (fds, n_fds, timeout);
1516 g_io_channel_win32_make_pollfd (GIOChannel *channel,
1517 GIOCondition condition,
1520 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1522 if (win32_channel->data_avail_event == NULL)
1523 create_events (win32_channel);
1525 fd->fd = (gint) win32_channel->data_avail_event;
1526 fd->events = condition;
1528 if (win32_channel->thread_id == 0)
1530 if ((condition & G_IO_IN) && win32_channel->type == G_IO_WIN32_FILE_DESC)
1531 create_thread (win32_channel, condition, read_thread);
1532 else if (win32_channel->type == G_IO_WIN32_SOCKET)
1533 create_thread (win32_channel, condition, select_thread);
1537 /* Binary compatibility */
1539 g_io_channel_win32_new_stream_socket (int socket)
1541 return g_io_channel_win32_new_socket (socket);