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-2003 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
90 HANDLE data_avail_event;
94 /* Following fields used by fd channels for input */
96 /* Data is kept in a circular buffer. To be able to distinguish between
97 * empty and full buffer, we cannot fill it completely, but have to
98 * leave a one character gap.
100 * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
103 * Full: (wrp + 1) % BUFFER_SIZE == rdp
106 guchar *buffer; /* (Circular) buffer */
107 gint wrp, rdp; /* Buffer indices for writing and reading */
108 HANDLE space_avail_event;
110 /* Following fields used by socket channels */
112 HANDLE data_avail_noticed_event;
113 gint reset_send; /* socket used to send data so select_thread() can reset/re-loop */
114 gint reset_recv; /* socket used to recv data so select_thread() can reset/re-loop */
117 #define LOCK(mutex) EnterCriticalSection (&mutex)
118 #define UNLOCK(mutex) LeaveCriticalSection (&mutex)
120 struct _GIOWin32Watch {
124 GIOCondition condition;
128 g_win32_print_access_mode (int flags)
130 g_print ("%s%s%s%s%s%s%s%s%s%s",
131 ((flags & 0x3) == _O_RDWR ? "O_RDWR" :
132 ((flags & 0x3) == _O_RDONLY ? "O_RDONLY" :
133 ((flags & 0x3) == _O_WRONLY ? "O_WRONLY" : "0"))),
134 (flags & _O_APPEND ? "|O_APPEND" : ""),
135 (flags & _O_RANDOM ? "|O_RANDOM" : ""),
136 (flags & _O_SEQUENTIAL ? "|O_SEQUENTIAL" : ""),
137 (flags & _O_TEMPORARY ? "|O_TEMPORARY" : ""),
138 (flags & _O_CREAT ? "|O_CREAT" : ""),
139 (flags & _O_TRUNC ? "|O_TRUNC" : ""),
140 (flags & _O_EXCL ? "|O_EXCL" : ""),
141 (flags & _O_TEXT ? "|O_TEXT" : ""),
142 (flags & _O_BINARY ? "|O_BINARY" : ""));
146 g_win32_print_gioflags (GIOFlags flags)
150 if (flags & G_IO_FLAG_APPEND)
151 bar = "|", g_print ("APPEND");
152 if (flags & G_IO_FLAG_NONBLOCK)
153 g_print ("%sNONBLOCK", bar), bar = "|";
154 if (flags & G_IO_FLAG_IS_READABLE)
155 g_print ("%sREADABLE", bar), bar = "|";
156 if (flags & G_IO_FLAG_IS_WRITEABLE)
157 g_print ("%sWRITEABLE", bar), bar = "|";
158 if (flags & G_IO_FLAG_IS_WRITEABLE)
159 g_print ("%sWRITEABLE", bar), bar = "|";
160 if (flags & G_IO_FLAG_IS_SEEKABLE)
161 g_print ("%sSEEKABLE", bar), bar = "|";
165 g_io_win32_get_debug_flag (void)
167 #ifdef G_IO_WIN32_DEBUG
170 if (getenv ("G_IO_WIN32_DEBUG") != NULL)
178 g_io_channel_win32_init (GIOWin32Channel *channel)
180 channel->debug = g_io_win32_get_debug_flag ();
181 channel->buffer = NULL;
182 channel->running = FALSE;
183 channel->needs_close = FALSE;
184 channel->thread_id = 0;
185 channel->data_avail_event = NULL;
186 channel->revents = 0;
187 channel->space_avail_event = NULL;
188 channel->data_avail_noticed_event = NULL;
189 channel->watches = NULL;
190 InitializeCriticalSection (&channel->mutex);
194 create_events (GIOWin32Channel *channel)
196 SECURITY_ATTRIBUTES sec_attrs;
198 sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
199 sec_attrs.lpSecurityDescriptor = NULL;
200 sec_attrs.bInheritHandle = FALSE;
202 /* The data available event is manual reset, the space available event
203 * is automatic reset.
205 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
206 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL))
207 || !(channel->data_avail_noticed_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
209 gchar *emsg = g_win32_error_message (GetLastError ());
210 g_error ("Error creating event: %s", emsg);
215 static unsigned __stdcall
216 read_thread (void *parameter)
218 GIOWin32Channel *channel = parameter;
222 g_io_channel_ref ((GIOChannel *)channel);
225 g_print ("read_thread %#x: start fd:%d, data_avail:%#x space_avail:%#x\n",
228 (guint) channel->data_avail_event,
229 (guint) channel->space_avail_event);
231 channel->buffer = g_malloc (BUFFER_SIZE);
232 channel->rdp = channel->wrp = 0;
233 channel->running = TRUE;
235 SetEvent (channel->space_avail_event);
237 LOCK (channel->mutex);
238 while (channel->running)
241 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
242 channel->thread_id, channel->rdp, channel->wrp);
243 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
247 g_print ("read_thread %#x: resetting space_avail\n",
249 ResetEvent (channel->space_avail_event);
251 g_print ("read_thread %#x: waiting for space\n",
253 UNLOCK (channel->mutex);
254 WaitForSingleObject (channel->space_avail_event, INFINITE);
255 LOCK (channel->mutex);
257 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
258 channel->thread_id, channel->rdp, channel->wrp);
261 buffer = channel->buffer + channel->wrp;
263 /* Always leave at least one byte unused gap to be able to
264 * distinguish between the full and empty condition...
266 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
267 BUFFER_SIZE - channel->wrp);
270 g_print ("read_thread %#x: calling read() for %d bytes\n",
271 channel->thread_id, nbytes);
273 UNLOCK (channel->mutex);
275 nbytes = read (channel->fd, buffer, nbytes);
277 LOCK (channel->mutex);
279 channel->revents = G_IO_IN;
281 channel->revents |= G_IO_HUP;
283 channel->revents |= G_IO_ERR;
286 g_print ("read_thread %#x: read() returned %d, rdp=%d, wrp=%d\n",
287 channel->thread_id, nbytes, channel->rdp, channel->wrp);
292 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
294 g_print ("read_thread %#x: rdp=%d, wrp=%d, setting data_avail\n",
295 channel->thread_id, channel->rdp, channel->wrp);
296 SetEvent (channel->data_avail_event);
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.
326 create_thread (GIOWin32Channel *channel,
327 GIOCondition condition,
328 unsigned (__stdcall *thread) (void *parameter))
330 HANDLE thread_handle;
332 thread_handle = (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0,
333 &channel->thread_id);
334 if (thread_handle == 0)
335 g_warning (G_STRLOC ": Error creating reader thread: %s",
337 else if (!CloseHandle (thread_handle))
338 g_warning (G_STRLOC ": Error closing thread handle: %s\n",
339 g_win32_error_message (GetLastError ()));
341 WaitForSingleObject (channel->space_avail_event, INFINITE);
345 init_reset_sockets (GIOWin32Channel *channel)
347 struct sockaddr_in local, local2, server;
350 channel->reset_send = (gint) socket (AF_INET, SOCK_DGRAM, 0);
351 if (channel->reset_send == INVALID_SOCKET)
353 g_warning (G_STRLOC ": Error creating reset_send socket: %s\n",
354 g_win32_error_message (WSAGetLastError ()));
357 local.sin_family = AF_INET;
359 local.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
361 if (bind (channel->reset_send, (struct sockaddr *)&local, sizeof (local)) == SOCKET_ERROR)
363 g_warning (G_STRLOC ": Error binding to reset_send socket: %s\n",
364 g_win32_error_message (WSAGetLastError ()));
367 local2.sin_family = AF_INET;
369 local2.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
371 channel->reset_recv = (gint) socket (AF_INET, SOCK_DGRAM, 0);
372 if (channel->reset_recv == INVALID_SOCKET)
374 g_warning (G_STRLOC ": Error creating reset_recv socket: %s\n",
375 g_win32_error_message (WSAGetLastError ()));
378 if (bind (channel->reset_recv, (struct sockaddr *)&local2, sizeof (local)) == SOCKET_ERROR)
380 g_warning (G_STRLOC ": Error binding to reset_recv socket: %s\n",
381 g_win32_error_message (WSAGetLastError ()));
384 len = sizeof (local2);
385 if (getsockname (channel->reset_recv, (struct sockaddr *)&local2, &len) == SOCKET_ERROR)
387 g_warning (G_STRLOC ": Error getsockname with reset_recv socket: %s\n",
388 g_win32_error_message (WSAGetLastError ()));
391 memset (&server, 0, sizeof (server));
392 server.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
393 server.sin_family = AF_INET;
394 server.sin_port = local2.sin_port;
396 if (connect (channel->reset_send, (struct sockaddr *)&server, sizeof (server)) == SOCKET_ERROR)
398 g_warning (G_STRLOC ": connect to reset_recv socket: %s\n",
399 g_win32_error_message (WSAGetLastError ()));
405 buffer_read (GIOWin32Channel *channel,
414 LOCK (channel->mutex);
416 g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
417 channel->thread_id, count, channel->rdp, channel->wrp);
419 if (channel->wrp == channel->rdp)
421 UNLOCK (channel->mutex);
423 g_print ("waiting for data from thread %#x\n", channel->thread_id);
424 WaitForSingleObject (channel->data_avail_event, INFINITE);
426 g_print ("done waiting for data from thread %#x\n", channel->thread_id);
427 LOCK (channel->mutex);
428 if (channel->wrp == channel->rdp && !channel->running)
431 g_print ("wrp==rdp, !running\n");
432 UNLOCK (channel->mutex);
434 return G_IO_STATUS_EOF;
438 if (channel->rdp < channel->wrp)
439 nbytes = channel->wrp - channel->rdp;
441 nbytes = BUFFER_SIZE - channel->rdp;
442 UNLOCK (channel->mutex);
443 nbytes = MIN (left, nbytes);
445 g_print ("moving %d bytes from thread %#x\n",
446 nbytes, channel->thread_id);
447 memcpy (dest, channel->buffer + channel->rdp, nbytes);
450 LOCK (channel->mutex);
451 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
453 g_print ("setting space_avail for thread %#x\n", channel->thread_id);
454 SetEvent (channel->space_avail_event);
456 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
457 channel->thread_id, channel->rdp, channel->wrp);
458 if (channel->running && channel->wrp == channel->rdp)
461 g_print ("resetting data_avail of thread %#x\n",
463 ResetEvent (channel->data_avail_event);
465 UNLOCK (channel->mutex);
467 /* We have no way to indicate any errors form the actual
468 * read() or recv() call in the reader thread. Should we have?
470 *bytes_read = count - left;
471 return (*bytes_read > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
474 static unsigned __stdcall
475 select_thread (void *parameter)
477 GIOWin32Channel *channel = parameter;
478 fd_set read_fds, write_fds, except_fds;
483 g_io_channel_ref ((GIOChannel *)channel);
486 g_print ("select_thread %#x: start fd:%d data_avail:%#x data_avail_noticed:%#x\n",
489 (guint) channel->data_avail_event,
490 (guint) channel->data_avail_noticed_event);
492 channel->rdp = channel->wrp = 0;
493 channel->running = TRUE;
495 SetEvent (channel->space_avail_event);
497 while (channel->running)
500 FD_ZERO (&write_fds);
501 FD_ZERO (&except_fds);
502 FD_SET (channel->reset_recv, &read_fds);
504 LOCK (channel->mutex);
505 tmp = channel->watches;
508 GIOWin32Watch *watch = (GIOWin32Watch *)tmp->data;
510 if (watch->condition & (G_IO_IN | G_IO_HUP))
511 FD_SET (channel->fd, &read_fds);
512 if (watch->condition & G_IO_OUT)
513 FD_SET (channel->fd, &write_fds);
514 if (watch->condition & G_IO_ERR)
515 FD_SET (channel->fd, &except_fds);
519 UNLOCK (channel->mutex);
522 g_print ("select_thread %#x: calling select() for%s%s%s\n",
524 (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
525 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
526 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
528 n = select (1, &read_fds, &write_fds, &except_fds, NULL);
530 LOCK (channel->mutex);
531 if (channel->needs_close)
533 UNLOCK (channel->mutex);
536 UNLOCK (channel->mutex);
538 if (n == SOCKET_ERROR)
541 g_print ("select_thread %#x: select returned SOCKET_ERROR\n",
546 if (FD_ISSET (channel->reset_recv, &read_fds))
549 g_print ("select_thread %#x: re-looping\n",
551 recv (channel->reset_recv, (char *)&buffer, (int) sizeof (buffer), 0);
556 g_print ("select_thread %#x: got%s%s%s\n",
558 (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
559 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
560 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
562 if (FD_ISSET (channel->fd, &read_fds))
563 channel->revents |= G_IO_IN;
564 if (FD_ISSET (channel->fd, &write_fds))
565 channel->revents |= G_IO_OUT;
566 if (FD_ISSET (channel->fd, &except_fds))
567 channel->revents |= G_IO_ERR;
570 g_print ("select_thread %#x: resetting data_avail_noticed, setting data_avail\n",
573 LOCK (channel->mutex);
574 ResetEvent (channel->data_avail_noticed_event);
575 SetEvent (channel->data_avail_event);
576 if (channel->needs_close)
578 UNLOCK (channel->mutex);
581 UNLOCK (channel->mutex);
584 g_print ("select_thread %#x: waiting for data_avail_noticed\n",
587 WaitForSingleObject (channel->data_avail_noticed_event, INFINITE);
589 g_print ("select_thread %#x: got data_avail_noticed\n",
593 LOCK (channel->mutex);
594 channel->running = FALSE;
596 g_print ("select_thread %#x: got error, setting data_avail\n",
598 SetEvent (channel->data_avail_event);
599 g_io_channel_unref ((GIOChannel *)channel);
600 UNLOCK (channel->mutex);
602 /* No need to call _endthreadex(), the actual thread starter routine
603 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
604 * _endthreadex() for us.
611 g_io_win32_prepare (GSource *source,
614 GIOWin32Watch *watch = (GIOWin32Watch *)source;
615 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
616 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
621 g_print ("g_io_win32_prepare: for thread %#x buffer_condition:%#x\n"
622 " watch->pollfd.events:%#x watch->pollfd.revents:%#x channel->revents:%#x\n",
623 channel->thread_id, buffer_condition,
624 watch->pollfd.events, watch->pollfd.revents, channel->revents);
626 if (channel->type == G_IO_WIN32_FILE_DESC)
628 LOCK (channel->mutex);
629 if (channel->running && channel->wrp == channel->rdp)
632 g_print ("g_io_win32_prepare: for thread %#x, setting channel->revents = 0\n",
634 channel->revents = 0;
636 UNLOCK (channel->mutex);
638 else if (channel->type == G_IO_WIN32_SOCKET)
640 LOCK (channel->mutex);
641 channel->revents = 0;
643 g_print ("g_io_win32_prepare: for thread %#x, setting data_avail_noticed\n",
645 SetEvent (channel->data_avail_noticed_event);
647 g_print ("g_io_win32_prepare: thread %#x, there.\n",
649 UNLOCK (channel->mutex);
652 return ((watch->condition & buffer_condition) == watch->condition);
656 g_io_win32_check (GSource *source)
659 GIOWin32Watch *watch = (GIOWin32Watch *)source;
660 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
661 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
664 g_print ("g_io_win32_check: for thread %#x buffer_condition:%#x\n"
665 " watch->pollfd.events:%#x watch->pollfd.revents:%#x channel->revents:%#x\n",
666 channel->thread_id, buffer_condition,
667 watch->pollfd.events, watch->pollfd.revents, channel->revents);
669 if (channel->type != G_IO_WIN32_WINDOWS_MESSAGES)
671 watch->pollfd.revents = (watch->pollfd.events & channel->revents);
675 return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
678 if (channel->type == G_IO_WIN32_SOCKET)
680 LOCK (channel->mutex);
682 g_print ("g_io_win32_check: thread %#x, resetting data_avail\n",
684 ResetEvent (channel->data_avail_event);
686 g_print ("g_io_win32_check: thread %#x, there.\n",
688 UNLOCK (channel->mutex);
691 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
695 g_io_win32_dispatch (GSource *source,
696 GSourceFunc callback,
699 GIOFunc func = (GIOFunc)callback;
700 GIOWin32Watch *watch = (GIOWin32Watch *)source;
701 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
705 g_warning (G_STRLOC ": GIOWin32Watch dispatched without callback\n"
706 "You must call g_source_connect().");
710 return (*func) (watch->channel,
711 (watch->pollfd.revents | buffer_condition) & watch->condition,
716 g_io_win32_finalize (GSource *source)
718 GIOWin32Watch *watch = (GIOWin32Watch *)source;
719 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
720 char send_buffer[] = "f";
722 LOCK (channel->mutex);
724 g_print ("g_io_win32_finalize: channel with thread %#x\n",
727 channel->watches = g_slist_remove (channel->watches, watch);
729 SetEvent (channel->data_avail_noticed_event);
730 if (channel->type == G_IO_WIN32_SOCKET)
731 send (channel->reset_send, send_buffer, sizeof (send_buffer), 0);
733 g_io_channel_unref (watch->channel);
734 UNLOCK (channel->mutex);
737 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
738 __declspec(dllexport)
740 GSourceFuncs g_io_watch_funcs = {
748 g_io_win32_create_watch (GIOChannel *channel,
749 GIOCondition condition,
750 unsigned (__stdcall *thread) (void *parameter))
752 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
753 GIOWin32Watch *watch;
755 char send_buffer[] = "c";
757 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
758 watch = (GIOWin32Watch *)source;
760 watch->channel = channel;
761 g_io_channel_ref (channel);
763 watch->condition = condition;
765 if (win32_channel->data_avail_event == NULL)
766 create_events (win32_channel);
768 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
769 watch->pollfd.events = condition;
771 if (win32_channel->debug)
772 g_print ("g_io_win32_create_watch: fd:%d condition:%#x handle:%#x\n",
773 win32_channel->fd, condition, watch->pollfd.fd);
775 LOCK (win32_channel->mutex);
776 win32_channel->watches = g_slist_append (win32_channel->watches, watch);
778 if (win32_channel->thread_id == 0)
779 create_thread (win32_channel, condition, thread);
781 send (win32_channel->reset_send, send_buffer, sizeof (send_buffer), 0);
783 g_source_add_poll (source, &watch->pollfd);
784 UNLOCK (win32_channel->mutex);
790 g_io_win32_msg_read (GIOChannel *channel,
796 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
797 MSG msg; /* In case of alignment problems */
799 if (count < sizeof (MSG))
801 g_set_error (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
802 "Incorrect message size"); /* Informative enough error message? */
803 return G_IO_STATUS_ERROR;
806 if (win32_channel->debug)
807 g_print ("g_io_win32_msg_read: for %#x\n",
808 (guint) win32_channel->hwnd);
809 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
810 return G_IO_STATUS_AGAIN;
812 memmove (buf, &msg, sizeof (MSG));
813 *bytes_read = sizeof (MSG);
815 return G_IO_STATUS_NORMAL;
819 g_io_win32_msg_write (GIOChannel *channel,
822 gsize *bytes_written,
825 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
828 if (count != sizeof (MSG))
830 g_set_error (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
831 "Incorrect message size"); /* Informative enough error message? */
832 return G_IO_STATUS_ERROR;
835 /* In case of alignment problems */
836 memmove (&msg, buf, sizeof (MSG));
837 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
839 gchar *emsg = g_win32_error_message (GetLastError ());
840 g_set_error (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, emsg);
842 return G_IO_STATUS_ERROR;
845 *bytes_written = sizeof (MSG);
847 return G_IO_STATUS_NORMAL;
851 g_io_win32_msg_close (GIOChannel *channel,
854 /* Nothing to be done. Or should we set hwnd to some invalid value? */
856 return G_IO_STATUS_NORMAL;
860 g_io_win32_free (GIOChannel *channel)
862 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
864 if (win32_channel->debug)
865 g_print ("thread %#x: freeing channel, fd: %d\n",
866 win32_channel->thread_id,
869 if (win32_channel->reset_send)
870 closesocket (win32_channel->reset_send);
871 if (win32_channel->reset_recv)
872 closesocket (win32_channel->reset_recv);
873 if (win32_channel->data_avail_event)
874 CloseHandle (win32_channel->data_avail_event);
875 if (win32_channel->space_avail_event)
876 CloseHandle (win32_channel->space_avail_event);
877 if (win32_channel->data_avail_noticed_event)
878 CloseHandle (win32_channel->data_avail_noticed_event);
879 DeleteCriticalSection (&win32_channel->mutex);
881 g_free (win32_channel->buffer);
882 g_slist_free (win32_channel->watches);
883 g_free (win32_channel);
887 g_io_win32_msg_create_watch (GIOChannel *channel,
888 GIOCondition condition)
890 GIOWin32Watch *watch;
893 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
894 watch = (GIOWin32Watch *)source;
896 watch->channel = channel;
897 g_io_channel_ref (channel);
899 watch->condition = condition;
901 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
902 watch->pollfd.events = condition;
904 g_source_add_poll (source, &watch->pollfd);
910 g_io_win32_fd_read (GIOChannel *channel,
916 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
919 if (win32_channel->debug)
920 g_print ("g_io_win32_fd_read: fd:%d count:%d\n",
921 win32_channel->fd, count);
923 if (win32_channel->thread_id)
925 return buffer_read (win32_channel, buf, count, bytes_read, err);
928 result = read (win32_channel->fd, buf, count);
930 if (win32_channel->debug)
931 g_print ("g_io_win32_fd_read: read() = %d\n", result);
941 return G_IO_STATUS_AGAIN;
944 g_set_error (err, G_IO_CHANNEL_ERROR,
945 g_io_channel_error_from_errno (errno),
947 return G_IO_STATUS_ERROR;
951 *bytes_read = result;
953 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
957 g_io_win32_fd_write (GIOChannel *channel,
960 gsize *bytes_written,
963 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
966 result = write (win32_channel->fd, buf, count);
967 if (win32_channel->debug)
968 g_print ("g_io_win32_fd_write: fd:%d count:%d = %d\n",
969 win32_channel->fd, count, result);
979 return G_IO_STATUS_AGAIN;
982 g_set_error (err, G_IO_CHANNEL_ERROR,
983 g_io_channel_error_from_errno (errno),
985 return G_IO_STATUS_ERROR;
989 *bytes_written = result;
991 return G_IO_STATUS_NORMAL;
995 g_io_win32_fd_seek (GIOChannel *channel,
1000 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1017 whence = -1; /* Keep the compiler quiet */
1018 g_assert_not_reached ();
1021 tmp_offset = offset;
1022 if (tmp_offset != offset)
1024 g_set_error (err, G_IO_CHANNEL_ERROR,
1025 g_io_channel_error_from_errno (EINVAL),
1026 g_strerror (EINVAL));
1027 return G_IO_STATUS_ERROR;
1030 result = lseek (win32_channel->fd, tmp_offset, whence);
1034 g_set_error (err, G_IO_CHANNEL_ERROR,
1035 g_io_channel_error_from_errno (errno),
1036 g_strerror (errno));
1037 return G_IO_STATUS_ERROR;
1040 return G_IO_STATUS_NORMAL;
1044 g_io_win32_fd_close (GIOChannel *channel,
1047 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1049 if (win32_channel->debug)
1050 g_print ("thread %#x: closing fd %d\n",
1051 win32_channel->thread_id,
1053 LOCK (win32_channel->mutex);
1054 if (win32_channel->running)
1056 if (win32_channel->debug)
1057 g_print ("thread %#x: running, marking fd %d for later close\n",
1058 win32_channel->thread_id, win32_channel->fd);
1059 win32_channel->running = FALSE;
1060 win32_channel->needs_close = TRUE;
1061 SetEvent (win32_channel->data_avail_event);
1065 if (win32_channel->debug)
1066 g_print ("closing fd %d\n", win32_channel->fd);
1067 close (win32_channel->fd);
1068 if (win32_channel->debug)
1069 g_print ("closed fd %d, setting to -1\n",
1071 win32_channel->fd = -1;
1073 UNLOCK (win32_channel->mutex);
1075 /* FIXME error detection? */
1077 return G_IO_STATUS_NORMAL;
1081 g_io_win32_fd_create_watch (GIOChannel *channel,
1082 GIOCondition condition)
1084 return g_io_win32_create_watch (channel, condition, read_thread);
1088 g_io_win32_sock_read (GIOChannel *channel,
1094 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1096 GIOChannelError error = G_IO_STATUS_NORMAL;
1097 GIOStatus internal_status = G_IO_STATUS_NORMAL;
1098 char send_buffer[] = "sr";
1100 if (win32_channel->debug)
1101 g_print ("g_io_win32_sock_read: sockfd:%d count:%d\n",
1102 win32_channel->fd, count);
1103 #ifdef WE_NEED_TO_HANDLE_WSAEINTR
1106 result = recv (win32_channel->fd, buf, count, 0);
1108 if (win32_channel->debug)
1109 g_print ("g_io_win32_sock_read: recv:%d\n", result);
1111 if (result == SOCKET_ERROR)
1115 switch (WSAGetLastError ())
1118 error = G_IO_CHANNEL_ERROR_INVAL;
1120 case WSAEWOULDBLOCK:
1121 return G_IO_STATUS_AGAIN;
1122 #ifdef WE_NEED_TO_HANDLE_WSAEINTR /* not anymore with wsock2 ? */
1127 error = G_IO_CHANNEL_ERROR_FAILED;
1130 g_set_error (err, G_IO_CHANNEL_ERROR, error, "Socket read error");
1131 internal_status = G_IO_STATUS_ERROR;
1132 /* FIXME get all errors, better error messages */
1136 *bytes_read = result;
1138 internal_status = G_IO_STATUS_EOF;
1141 if ((internal_status == G_IO_STATUS_EOF) ||
1142 (internal_status == G_IO_STATUS_ERROR))
1144 LOCK (win32_channel->mutex);
1145 SetEvent (win32_channel->data_avail_noticed_event);
1146 win32_channel->needs_close = 1;
1147 send (win32_channel->reset_send, send_buffer, sizeof (send_buffer), 0);
1148 UNLOCK (win32_channel->mutex);
1150 return internal_status;
1154 g_io_win32_sock_write (GIOChannel *channel,
1157 gsize *bytes_written,
1160 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1162 GIOChannelError error = G_IO_STATUS_NORMAL;
1163 char send_buffer[] = "sw";
1165 if (win32_channel->debug)
1166 g_print ("g_io_win32_sock_write: sockfd:%d count:%d\n",
1167 win32_channel->fd, count);
1168 #ifdef WE_NEED_TO_HANDLE_WSAEINTR
1171 result = send (win32_channel->fd, buf, count, 0);
1173 if (win32_channel->debug)
1174 g_print ("g_io_win32_sock_write: send:%d\n", result);
1176 if (result == SOCKET_ERROR)
1180 switch (WSAGetLastError ())
1183 error = G_IO_CHANNEL_ERROR_INVAL;
1185 case WSAEWOULDBLOCK:
1186 return G_IO_STATUS_AGAIN;
1187 #ifdef WE_NEED_TO_HANDLE_WSAEINTR /* not anymore with wsock2 ? */
1192 error = G_IO_CHANNEL_ERROR_FAILED;
1195 g_set_error (err, G_IO_CHANNEL_ERROR, error, "Socket write error");
1196 LOCK (win32_channel->mutex);
1197 SetEvent (win32_channel->data_avail_noticed_event);
1198 win32_channel->needs_close = 1;
1199 send (win32_channel->reset_send, send_buffer, sizeof (send_buffer), 0);
1200 UNLOCK (win32_channel->mutex);
1201 return G_IO_STATUS_ERROR;
1202 /* FIXME get all errors, better error messages */
1206 *bytes_written = result;
1208 return G_IO_STATUS_NORMAL;
1213 g_io_win32_sock_close (GIOChannel *channel,
1216 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1218 LOCK (win32_channel->mutex);
1219 if (win32_channel->running)
1221 if (win32_channel->debug)
1222 g_print ("thread %#x: running, marking for later close\n",
1223 win32_channel->thread_id);
1224 win32_channel->running = FALSE;
1225 win32_channel->needs_close = TRUE;
1226 SetEvent(win32_channel->data_avail_noticed_event);
1228 if (win32_channel->fd != -1)
1230 if (win32_channel->debug)
1231 g_print ("thread %#x: closing socket %d\n",
1232 win32_channel->thread_id,
1235 closesocket (win32_channel->fd);
1236 win32_channel->fd = -1;
1238 UNLOCK (win32_channel->mutex);
1240 /* FIXME error detection? */
1242 return G_IO_STATUS_NORMAL;
1246 g_io_win32_sock_create_watch (GIOChannel *channel,
1247 GIOCondition condition)
1249 return g_io_win32_create_watch (channel, condition, select_thread);
1253 g_io_channel_new_file (const gchar *filename,
1257 int fid, flags, pmode;
1258 GIOChannel *channel;
1260 enum { /* Cheesy hack */
1267 g_return_val_if_fail (filename != NULL, NULL);
1268 g_return_val_if_fail (mode != NULL, NULL);
1269 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
1283 g_warning ("Invalid GIOFileMode %s.\n", mode);
1292 if (mode[2] == '\0')
1294 mode_num |= MODE_PLUS;
1299 g_warning ("Invalid GIOFileMode %s.\n", mode);
1310 flags = O_WRONLY | O_TRUNC | O_CREAT;
1314 flags = O_WRONLY | O_APPEND | O_CREAT;
1317 case MODE_R | MODE_PLUS:
1319 pmode = _S_IREAD | _S_IWRITE;
1321 case MODE_W | MODE_PLUS:
1322 flags = O_RDWR | O_TRUNC | O_CREAT;
1323 pmode = _S_IREAD | _S_IWRITE;
1325 case MODE_A | MODE_PLUS:
1326 flags = O_RDWR | O_APPEND | O_CREAT;
1327 pmode = _S_IREAD | _S_IWRITE;
1330 g_assert_not_reached ();
1335 /* always open 'untranslated' */
1336 fid = open (filename, flags | _O_BINARY, pmode);
1338 if (g_io_win32_get_debug_flag ())
1340 g_print ("g_io_channel_win32_new_file: open(\"%s\", ", filename);
1341 g_win32_print_access_mode (flags|_O_BINARY);
1342 g_print (",%#o)=%d\n", pmode, fid);
1347 g_set_error (error, G_FILE_ERROR,
1348 g_file_error_from_errno (errno),
1349 g_strerror (errno));
1350 return (GIOChannel *)NULL;
1353 channel = g_io_channel_win32_new_fd (fid);
1355 /* XXX: move this to g_io_channel_win32_new_fd () */
1356 channel->close_on_unref = TRUE;
1357 channel->is_seekable = TRUE;
1359 /* g_io_channel_win32_new_fd sets is_readable and is_writeable to
1360 * correspond to actual readability/writeability. Set to FALSE those
1361 * that mode doesn't allow
1366 channel->is_writeable = FALSE;
1370 channel->is_readable = FALSE;
1372 case MODE_R | MODE_PLUS:
1373 case MODE_W | MODE_PLUS:
1374 case MODE_A | MODE_PLUS:
1377 g_assert_not_reached ();
1384 g_io_win32_set_flags (GIOChannel *channel,
1388 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1390 if (win32_channel->debug)
1392 g_print ("g_io_win32_set_flags: ");
1393 g_win32_print_gioflags (flags);
1397 g_warning ("g_io_win32_set_flags () not implemented.\n");
1399 return G_IO_STATUS_NORMAL;
1403 g_io_win32_fd_get_flags_internal (GIOChannel *channel,
1406 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1410 if (st->st_mode & _S_IFIFO)
1412 channel->is_readable =
1413 (PeekNamedPipe ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL, NULL) != 0) || GetLastError () == ERROR_BROKEN_PIPE;
1414 channel->is_writeable =
1415 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1416 channel->is_seekable = FALSE;
1418 else if (st->st_mode & _S_IFCHR)
1420 /* XXX Seems there is no way to find out the readability of file
1421 * handles to device files (consoles, mostly) without doing a
1422 * blocking read. So punt, use st->st_mode.
1424 channel->is_readable = !!(st->st_mode & _S_IREAD);
1426 channel->is_writeable =
1427 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1429 /* XXX What about devices that actually *are* seekable? But
1430 * those would probably not be handled using the C runtime
1431 * anyway, but using Windows-specific code.
1433 channel->is_seekable = FALSE;
1437 channel->is_readable =
1438 (ReadFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1439 channel->is_writeable =
1440 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1441 channel->is_seekable = TRUE;
1444 /* XXX: G_IO_FLAG_APPEND */
1445 /* XXX: G_IO_FLAG_NONBLOCK */
1451 g_io_win32_fd_get_flags (GIOChannel *channel)
1454 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1456 g_return_val_if_fail (win32_channel != NULL, 0);
1457 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_FILE_DESC, 0);
1459 if (0 == fstat (win32_channel->fd, &st))
1460 return g_io_win32_fd_get_flags_internal (channel, &st);
1466 g_io_win32_msg_get_flags (GIOChannel *channel)
1472 g_io_win32_sock_get_flags (GIOChannel *channel)
1474 /* XXX Could do something here. */
1478 static GIOFuncs win32_channel_msg_funcs = {
1479 g_io_win32_msg_read,
1480 g_io_win32_msg_write,
1482 g_io_win32_msg_close,
1483 g_io_win32_msg_create_watch,
1485 g_io_win32_set_flags,
1486 g_io_win32_msg_get_flags,
1489 static GIOFuncs win32_channel_fd_funcs = {
1491 g_io_win32_fd_write,
1493 g_io_win32_fd_close,
1494 g_io_win32_fd_create_watch,
1496 g_io_win32_set_flags,
1497 g_io_win32_fd_get_flags,
1500 static GIOFuncs win32_channel_sock_funcs = {
1501 g_io_win32_sock_read,
1502 g_io_win32_sock_write,
1504 g_io_win32_sock_close,
1505 g_io_win32_sock_create_watch,
1507 g_io_win32_set_flags,
1508 g_io_win32_sock_get_flags,
1512 g_io_channel_win32_new_messages (guint hwnd)
1514 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1515 GIOChannel *channel = (GIOChannel *)win32_channel;
1517 g_io_channel_init (channel);
1518 g_io_channel_win32_init (win32_channel);
1519 if (win32_channel->debug)
1520 g_print ("g_io_channel_win32_new_messages: hwnd = %ud\n", hwnd);
1521 channel->funcs = &win32_channel_msg_funcs;
1522 win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
1523 win32_channel->hwnd = (HWND) hwnd;
1525 /* XXX: check this. */
1526 channel->is_readable = IsWindow (win32_channel->hwnd);
1527 channel->is_writeable = IsWindow (win32_channel->hwnd);
1529 channel->is_seekable = FALSE;
1535 g_io_channel_win32_new_fd_internal (gint fd,
1538 GIOWin32Channel *win32_channel;
1539 GIOChannel *channel;
1541 win32_channel = g_new (GIOWin32Channel, 1);
1542 channel = (GIOChannel *)win32_channel;
1544 g_io_channel_init (channel);
1545 g_io_channel_win32_init (win32_channel);
1546 if (win32_channel->debug)
1547 g_print ("g_io_channel_win32_new_fd: %u\n", fd);
1548 channel->funcs = &win32_channel_fd_funcs;
1549 win32_channel->type = G_IO_WIN32_FILE_DESC;
1550 win32_channel->fd = fd;
1552 g_io_win32_fd_get_flags_internal (channel, st);
1558 g_io_channel_win32_new_fd (gint fd)
1562 if (fstat (fd, &st) == -1)
1564 g_warning (G_STRLOC ": %d isn't a C library file descriptor", fd);
1568 return g_io_channel_win32_new_fd_internal (fd, &st);
1572 g_io_channel_win32_get_fd (GIOChannel *channel)
1574 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1576 return win32_channel->fd;
1580 g_io_channel_win32_new_socket (int socket)
1582 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1583 GIOChannel *channel = (GIOChannel *)win32_channel;
1585 g_io_channel_init (channel);
1586 g_io_channel_win32_init (win32_channel);
1587 init_reset_sockets (win32_channel);
1588 if (win32_channel->debug)
1589 g_print ("g_io_channel_win32_new_socket: sockfd:%d\n", socket);
1590 channel->funcs = &win32_channel_sock_funcs;
1591 win32_channel->type = G_IO_WIN32_SOCKET;
1592 win32_channel->fd = socket;
1594 /* XXX: check this */
1595 channel->is_readable = TRUE;
1596 channel->is_writeable = TRUE;
1598 channel->is_seekable = FALSE;
1604 g_io_channel_unix_new (gint fd)
1606 gboolean is_fd, is_socket;
1610 is_fd = (fstat (fd, &st) == 0);
1612 optlen = sizeof (optval);
1613 is_socket = (getsockopt (fd, SOL_SOCKET, SO_TYPE, (char *) &optval, &optlen) != SOCKET_ERROR);
1615 if (is_fd && is_socket)
1616 g_warning (G_STRLOC ": %d is both a file descriptor and a socket, file descriptor interpretation assumed.", fd);
1619 return g_io_channel_win32_new_fd_internal (fd, &st);
1622 return g_io_channel_win32_new_socket(fd);
1624 g_warning (G_STRLOC ": %d is neither a file descriptor or a socket", fd);
1630 g_io_channel_unix_get_fd (GIOChannel *channel)
1632 return g_io_channel_win32_get_fd (channel);
1636 g_io_channel_win32_set_debug (GIOChannel *channel,
1639 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1641 win32_channel->debug = flag;
1645 g_io_channel_win32_poll (GPollFD *fds,
1651 g_return_val_if_fail (n_fds >= 0, 0);
1653 result = (*g_main_context_get_poll_func (NULL)) (fds, n_fds, timeout);
1659 g_io_channel_win32_make_pollfd (GIOChannel *channel,
1660 GIOCondition condition,
1663 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1665 if (win32_channel->data_avail_event == NULL)
1666 create_events (win32_channel);
1668 fd->fd = (gint) win32_channel->data_avail_event;
1669 fd->events = condition;
1671 if (win32_channel->thread_id == 0)
1673 if ((condition & G_IO_IN) && win32_channel->type == G_IO_WIN32_FILE_DESC)
1674 create_thread (win32_channel, condition, read_thread);
1675 else if (win32_channel->type == G_IO_WIN32_SOCKET)
1676 create_thread (win32_channel, condition, select_thread);
1680 /* Binary compatibility */
1682 g_io_channel_win32_new_stream_socket (int socket)
1684 return g_io_channel_win32_new_socket (socket);