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/.
51 typedef struct _GIOWin32Channel GIOWin32Channel;
52 typedef struct _GIOWin32Watch GIOWin32Watch;
54 #define BUFFER_SIZE 4096
57 G_IO_WIN32_WINDOWS_MESSAGES, /* Windows messages */
58 G_IO_WIN32_FILE_DESC, /* Unix-like file descriptors from
59 * _open() or _pipe(), except for console IO.
60 * Have to create separate thread to read.
62 G_IO_WIN32_CONSOLE, /* Console IO (usually stdin, stdout, stderr) */
63 G_IO_WIN32_SOCKET /* Sockets. No separate thread */
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 /* This is used by G_IO_WIN32_WINDOWS_MESSAGES channels */
77 HWND hwnd; /* handle of window, or NULL */
79 /* Following fields are used by fd channels. */
80 CRITICAL_SECTION mutex;
82 int direction; /* 0 means we read from it,
83 * 1 means we write to it.
86 gboolean running; /* Is reader thread running. FALSE if
87 * EOF has been reached.
89 gboolean needs_close; /* If the channel has been closed while
90 * the reader thread was still running.
92 guint thread_id; /* If non-NULL has a reader thread, or has
94 HANDLE data_avail_event;
98 /* Following fields used by fd channels for input */
100 /* Data is kept in a circular buffer. To be able to distinguish between
101 * empty and full buffer, we cannot fill it completely, but have to
102 * leave a one character gap.
104 * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
107 * Full: (wrp + 1) % BUFFER_SIZE == rdp
110 guchar *buffer; /* (Circular) buffer */
111 gint wrp, rdp; /* Buffer indices for writing and reading */
112 HANDLE space_avail_event;
114 /* Following fields used by socket channels */
118 gboolean write_would_have_blocked;
121 #define LOCK(mutex) EnterCriticalSection (&mutex)
122 #define UNLOCK(mutex) LeaveCriticalSection (&mutex)
124 struct _GIOWin32Watch {
128 GIOCondition condition;
132 g_win32_print_access_mode (int flags)
134 g_print ("%s%s%s%s%s%s%s%s%s%s",
135 ((flags & 0x3) == _O_RDWR ? "O_RDWR" :
136 ((flags & 0x3) == _O_RDONLY ? "O_RDONLY" :
137 ((flags & 0x3) == _O_WRONLY ? "O_WRONLY" : "0"))),
138 (flags & _O_APPEND ? "|O_APPEND" : ""),
139 (flags & _O_RANDOM ? "|O_RANDOM" : ""),
140 (flags & _O_SEQUENTIAL ? "|O_SEQUENTIAL" : ""),
141 (flags & _O_TEMPORARY ? "|O_TEMPORARY" : ""),
142 (flags & _O_CREAT ? "|O_CREAT" : ""),
143 (flags & _O_TRUNC ? "|O_TRUNC" : ""),
144 (flags & _O_EXCL ? "|O_EXCL" : ""),
145 (flags & _O_TEXT ? "|O_TEXT" : ""),
146 (flags & _O_BINARY ? "|O_BINARY" : ""));
150 g_win32_print_gioflags (GIOFlags flags)
154 if (flags & G_IO_FLAG_APPEND)
155 bar = "|", g_print ("APPEND");
156 if (flags & G_IO_FLAG_NONBLOCK)
157 g_print ("%sNONBLOCK", bar), bar = "|";
158 if (flags & G_IO_FLAG_IS_READABLE)
159 g_print ("%sREADABLE", bar), bar = "|";
160 if (flags & G_IO_FLAG_IS_WRITEABLE)
161 g_print ("%sWRITEABLE", bar), bar = "|";
162 if (flags & G_IO_FLAG_IS_SEEKABLE)
163 g_print ("%sSEEKABLE", bar), bar = "|";
167 event_mask_to_string (int mask)
170 int checked_bits = 0;
176 #define BIT(n) checked_bits |= FD_##n; if (mask & FD_##n) bufp += sprintf (bufp, "%s" #n, (bufp>buf ? "|" : ""))
186 BIT (ROUTING_INTERFACE_CHANGE);
187 BIT (ADDRESS_LIST_CHANGE);
191 if ((mask & ~checked_bits) != 0)
192 bufp += sprintf (bufp, "|%#x", mask & ~checked_bits);
194 return g_quark_to_string (g_quark_from_string (buf));
198 condition_to_string (GIOCondition condition)
201 int checked_bits = 0;
207 #define BIT(n) checked_bits |= G_IO_##n; if (condition & G_IO_##n) bufp += sprintf (bufp, "%s" #n, (bufp>buf ? "|" : ""))
218 if ((condition & ~checked_bits) != 0)
219 bufp += sprintf (bufp, "|%#x", condition & ~checked_bits);
221 return g_quark_to_string (g_quark_from_string (buf));
225 g_io_win32_get_debug_flag (void)
227 return (getenv ("G_IO_WIN32_DEBUG") != NULL);
231 g_io_channel_win32_init (GIOWin32Channel *channel)
233 channel->debug = g_io_win32_get_debug_flag ();
234 channel->buffer = NULL;
235 channel->running = FALSE;
236 channel->needs_close = FALSE;
237 channel->thread_id = 0;
238 channel->data_avail_event = NULL;
239 channel->revents = 0;
240 channel->space_avail_event = NULL;
241 channel->event_mask = 0;
242 channel->last_events = 0;
244 channel->write_would_have_blocked = FALSE;
245 InitializeCriticalSection (&channel->mutex);
249 create_events (GIOWin32Channel *channel)
251 SECURITY_ATTRIBUTES sec_attrs;
253 sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
254 sec_attrs.lpSecurityDescriptor = NULL;
255 sec_attrs.bInheritHandle = FALSE;
257 /* The data available event is manual reset, the space available event
258 * is automatic reset.
260 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
261 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
263 gchar *emsg = g_win32_error_message (GetLastError ());
264 g_error ("Error creating event: %s", emsg);
269 static unsigned __stdcall
270 read_thread (void *parameter)
272 GIOWin32Channel *channel = parameter;
276 g_io_channel_ref ((GIOChannel *)channel);
279 g_print ("read_thread %#x: start fd=%d, data_avail=%#x space_avail=%#x\n",
282 (guint) channel->data_avail_event,
283 (guint) channel->space_avail_event);
285 channel->direction = 0;
286 channel->buffer = g_malloc (BUFFER_SIZE);
287 channel->rdp = channel->wrp = 0;
288 channel->running = TRUE;
290 SetEvent (channel->space_avail_event);
292 LOCK (channel->mutex);
293 while (channel->running)
296 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
297 channel->thread_id, channel->rdp, channel->wrp);
298 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
302 g_print ("read_thread %#x: resetting space_avail\n",
304 ResetEvent (channel->space_avail_event);
306 g_print ("read_thread %#x: waiting for space\n",
308 UNLOCK (channel->mutex);
309 WaitForSingleObject (channel->space_avail_event, INFINITE);
310 LOCK (channel->mutex);
312 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
313 channel->thread_id, channel->rdp, channel->wrp);
316 buffer = channel->buffer + channel->wrp;
318 /* Always leave at least one byte unused gap to be able to
319 * distinguish between the full and empty condition...
321 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
322 BUFFER_SIZE - channel->wrp);
325 g_print ("read_thread %#x: calling read() for %d bytes\n",
326 channel->thread_id, nbytes);
328 UNLOCK (channel->mutex);
330 nbytes = read (channel->fd, buffer, nbytes);
332 LOCK (channel->mutex);
334 channel->revents = G_IO_IN;
336 channel->revents |= G_IO_HUP;
338 channel->revents |= G_IO_ERR;
341 g_print ("read_thread %#x: read() returned %d, rdp=%d, wrp=%d\n",
342 channel->thread_id, nbytes, channel->rdp, channel->wrp);
347 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
349 g_print ("read_thread %#x: rdp=%d, wrp=%d, setting data_avail\n",
350 channel->thread_id, channel->rdp, channel->wrp);
351 SetEvent (channel->data_avail_event);
354 channel->running = FALSE;
355 if (channel->needs_close)
358 g_print ("read_thread %#x: channel fd %d needs closing\n",
359 channel->thread_id, channel->fd);
365 g_print ("read_thread %#x: EOF, rdp=%d, wrp=%d, setting data_avail\n",
366 channel->thread_id, channel->rdp, channel->wrp);
367 SetEvent (channel->data_avail_event);
368 UNLOCK (channel->mutex);
370 g_io_channel_unref ((GIOChannel *)channel);
372 /* No need to call _endthreadex(), the actual thread starter routine
373 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
374 * _endthreadex() for us.
380 static unsigned __stdcall
381 write_thread (void *parameter)
383 GIOWin32Channel *channel = parameter;
387 g_io_channel_ref ((GIOChannel *)channel);
390 g_print ("write_thread %#x: start fd=%d, data_avail=%#x space_avail=%#x\n",
393 (guint) channel->data_avail_event,
394 (guint) channel->space_avail_event);
396 channel->direction = 1;
397 channel->buffer = g_malloc (BUFFER_SIZE);
398 channel->rdp = channel->wrp = 0;
399 channel->running = TRUE;
401 SetEvent (channel->space_avail_event);
403 /* We use the same event objects as for a reader thread, but with
404 * reversed meaning. So, space_avail is used if data is available
405 * for writing, and data_avail is used if space is available in the
409 LOCK (channel->mutex);
410 while (channel->running || channel->rdp != channel->wrp)
413 g_print ("write_thread %#x: rdp=%d, wrp=%d\n",
414 channel->thread_id, channel->rdp, channel->wrp);
415 if (channel->wrp == channel->rdp)
417 /* Buffer is empty. */
419 g_print ("write_thread %#x: resetting space_avail\n",
421 ResetEvent (channel->space_avail_event);
423 g_print ("write_thread %#x: waiting for data\n",
425 channel->revents = G_IO_OUT;
426 SetEvent (channel->data_avail_event);
427 UNLOCK (channel->mutex);
428 WaitForSingleObject (channel->space_avail_event, INFINITE);
430 LOCK (channel->mutex);
431 if (channel->rdp == channel->wrp)
435 g_print ("write_thread %#x: rdp=%d, wrp=%d\n",
436 channel->thread_id, channel->rdp, channel->wrp);
439 buffer = channel->buffer + channel->rdp;
440 if (channel->rdp < channel->wrp)
441 nbytes = channel->wrp - channel->rdp;
443 nbytes = BUFFER_SIZE - channel->rdp;
446 g_print ("write_thread %#x: calling write() for %d bytes\n",
447 channel->thread_id, nbytes);
449 UNLOCK (channel->mutex);
450 nbytes = write (channel->fd, buffer, nbytes);
451 LOCK (channel->mutex);
454 g_print ("write_thread %#x: write(%i) returned %d, rdp=%d, wrp=%d\n",
455 channel->thread_id, channel->fd, nbytes, channel->rdp, channel->wrp);
457 channel->revents = 0;
459 channel->revents |= G_IO_OUT;
460 else if (nbytes <= 0)
461 channel->revents |= G_IO_ERR;
463 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
469 g_print ("write_thread: setting data_avail for thread %#x\n",
471 SetEvent (channel->data_avail_event);
474 channel->running = FALSE;
475 if (channel->needs_close)
478 g_print ("write_thread %#x: channel fd %d needs closing\n",
479 channel->thread_id, channel->fd);
484 UNLOCK (channel->mutex);
486 g_io_channel_unref ((GIOChannel *)channel);
492 create_thread (GIOWin32Channel *channel,
493 GIOCondition condition,
494 unsigned (__stdcall *thread) (void *parameter))
496 HANDLE thread_handle;
498 thread_handle = (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0,
499 &channel->thread_id);
500 if (thread_handle == 0)
501 g_warning (G_STRLOC ": Error creating reader thread: %s",
503 else if (!CloseHandle (thread_handle))
504 g_warning (G_STRLOC ": Error closing thread handle: %s\n",
505 g_win32_error_message (GetLastError ()));
507 WaitForSingleObject (channel->space_avail_event, INFINITE);
511 buffer_read (GIOWin32Channel *channel,
520 LOCK (channel->mutex);
522 g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
523 channel->thread_id, count, channel->rdp, channel->wrp);
525 if (channel->wrp == channel->rdp)
527 UNLOCK (channel->mutex);
529 g_print ("waiting for data from thread %#x\n", channel->thread_id);
530 WaitForSingleObject (channel->data_avail_event, INFINITE);
532 g_print ("done waiting for data from thread %#x\n", channel->thread_id);
533 LOCK (channel->mutex);
534 if (channel->wrp == channel->rdp && !channel->running)
537 g_print ("wrp==rdp, !running\n");
538 UNLOCK (channel->mutex);
540 return G_IO_STATUS_EOF;
544 if (channel->rdp < channel->wrp)
545 nbytes = channel->wrp - channel->rdp;
547 nbytes = BUFFER_SIZE - channel->rdp;
548 UNLOCK (channel->mutex);
549 nbytes = MIN (left, nbytes);
551 g_print ("moving %d bytes from thread %#x\n",
552 nbytes, channel->thread_id);
553 memcpy (dest, channel->buffer + channel->rdp, nbytes);
556 LOCK (channel->mutex);
557 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
559 g_print ("setting space_avail for thread %#x\n", channel->thread_id);
560 SetEvent (channel->space_avail_event);
562 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
563 channel->thread_id, channel->rdp, channel->wrp);
564 if (channel->running && channel->wrp == channel->rdp)
567 g_print ("resetting data_avail of thread %#x\n",
569 ResetEvent (channel->data_avail_event);
571 UNLOCK (channel->mutex);
573 /* We have no way to indicate any errors form the actual
574 * read() or recv() call in the reader thread. Should we have?
576 *bytes_read = count - left;
577 return (*bytes_read > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
582 buffer_write (GIOWin32Channel *channel,
585 gsize *bytes_written,
591 LOCK (channel->mutex);
593 g_print ("buffer_write: writing to thread %#x %d bytes, rdp=%d, wrp=%d\n",
594 channel->thread_id, count, channel->rdp, channel->wrp);
596 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
600 g_print ("buffer_write: tid %#x: resetting data_avail\n",
602 ResetEvent (channel->data_avail_event);
604 g_print ("buffer_write: tid %#x: waiting for space\n",
606 UNLOCK (channel->mutex);
607 WaitForSingleObject (channel->data_avail_event, INFINITE);
608 LOCK (channel->mutex);
610 g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d\n",
611 channel->thread_id, channel->rdp, channel->wrp);
614 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
615 BUFFER_SIZE - channel->wrp);
617 UNLOCK (channel->mutex);
618 nbytes = MIN (left, nbytes);
620 g_print ("buffer_write: tid %#x: writing %d bytes\n",
621 channel->thread_id, nbytes);
622 memcpy (channel->buffer + channel->wrp, dest, nbytes);
625 LOCK (channel->mutex);
627 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
629 g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d, setting space_avail\n",
630 channel->thread_id, channel->rdp, channel->wrp);
631 SetEvent (channel->space_avail_event);
633 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
637 g_print ("buffer_write: tid %#x: resetting data_avail\n",
639 ResetEvent (channel->data_avail_event);
642 UNLOCK (channel->mutex);
644 /* We have no way to indicate any errors form the actual
645 * write() call in the writer thread. Should we have?
647 *bytes_written = count - left;
648 return (*bytes_written > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
653 g_io_win32_prepare (GSource *source,
656 GIOWin32Watch *watch = (GIOWin32Watch *)source;
657 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
658 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
664 g_print ("g_io_win32_prepare: source=%p channel=%p", source, channel);
666 switch (channel->type)
668 case G_IO_WIN32_WINDOWS_MESSAGES:
673 case G_IO_WIN32_CONSOLE:
678 case G_IO_WIN32_FILE_DESC:
680 g_print (" FD thread=%#x buffer_condition:{%s}"
681 "\n watch->pollfd.events:{%s} watch->pollfd.revents:{%s} channel->revents:{%s}",
682 channel->thread_id, condition_to_string (buffer_condition),
683 condition_to_string (watch->pollfd.events),
684 condition_to_string (watch->pollfd.revents),
685 condition_to_string (channel->revents));
687 LOCK (channel->mutex);
688 if (channel->running)
690 if (channel->direction == 0 && channel->wrp == channel->rdp)
693 g_print ("\n setting revents=0");
694 channel->revents = 0;
699 if (channel->direction == 1
700 && (channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
703 g_print ("\n setting revents=0");
704 channel->revents = 0;
707 UNLOCK (channel->mutex);
710 case G_IO_WIN32_SOCKET:
714 if (watch->condition & G_IO_IN)
715 event_mask |= (FD_READ | FD_ACCEPT);
716 if (watch->condition & G_IO_OUT)
717 event_mask |= (FD_WRITE | FD_CONNECT);
718 event_mask |= FD_CLOSE;
720 if (channel->event_mask != event_mask /* || channel->event != watch->pollfd.fd*/)
723 g_print ("\n WSAEventSelect(%d,%#x,{%s})",
724 channel->fd, watch->pollfd.fd,
725 event_mask_to_string (event_mask));
726 if (WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd,
727 event_mask) == SOCKET_ERROR)
729 channel->event_mask = event_mask;
731 channel->event = watch->pollfd.fd;
734 g_print ("\n setting last_events=0");
735 channel->last_events = 0;
740 g_assert_not_reached ();
746 return ((watch->condition & buffer_condition) == watch->condition);
750 g_io_win32_check (GSource *source)
753 GIOWin32Watch *watch = (GIOWin32Watch *)source;
754 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
755 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
756 WSANETWORKEVENTS events;
759 g_print ("g_io_win32_check: source=%p channel=%p", source, channel);
761 switch (channel->type)
763 case G_IO_WIN32_WINDOWS_MESSAGES:
766 return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
768 case G_IO_WIN32_FILE_DESC:
770 g_print (" FD thread=%#x buffer_condition=%s\n"
771 " watch->pollfd.events={%s} watch->pollfd.revents={%s} channel->revents={%s}\n",
772 channel->thread_id, condition_to_string (buffer_condition),
773 condition_to_string (watch->pollfd.events),
774 condition_to_string (watch->pollfd.revents),
775 condition_to_string (channel->revents));
777 watch->pollfd.revents = (watch->pollfd.events & channel->revents);
779 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
781 case G_IO_WIN32_CONSOLE:
784 if (watch->channel->is_writeable)
786 else if (watch->channel->is_readable)
790 if (PeekConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n) &&
793 /* _kbhit() does quite complex processing to find out
794 * whether at least one of the key events pending corresponds
795 * to a "real" character that can be read.
800 /* Discard all other kinds of events */
801 ReadConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n);
806 case G_IO_WIN32_SOCKET:
809 if (channel->last_events & FD_WRITE)
812 g_print (" sock=%d event=%#x last_events has FD_WRITE",
813 channel->fd, watch->pollfd.fd);
817 WSAEnumNetworkEvents (channel->fd, 0, &events);
820 g_print ("\n revents={%s} condition={%s}"
821 "\n WSAEnumNetworkEvents(%d,0) sets events={%s}",
822 condition_to_string (watch->pollfd.revents),
823 condition_to_string (watch->condition),
825 event_mask_to_string (events.lNetworkEvents));
827 if (watch->pollfd.revents != 0 &&
828 events.lNetworkEvents == 0 &&
829 !(channel->event_mask & FD_WRITE))
831 channel->event_mask = 0;
833 g_print ("\n WSAEventSelect(%d,%#x,{})",
834 channel->fd, watch->pollfd.fd);
835 WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd, 0);
837 g_print (" ResetEvent(%#x)",
839 ResetEvent ((HANDLE) watch->pollfd.fd);
841 channel->last_events = events.lNetworkEvents;
843 watch->pollfd.revents = 0;
844 if (channel->last_events & (FD_READ | FD_ACCEPT))
845 watch->pollfd.revents |= G_IO_IN;
846 if (channel->last_events & FD_WRITE)
847 watch->pollfd.revents |= G_IO_OUT;
850 /* We have called WSAEnumNetworkEvents() above but it didn't
853 if (events.lNetworkEvents & FD_CONNECT)
855 if (events.iErrorCode[FD_CONNECT_BIT] == 0)
856 watch->pollfd.revents |= G_IO_OUT;
858 watch->pollfd.revents |= (G_IO_HUP | G_IO_ERR);
860 if (watch->pollfd.revents == 0 && (channel->last_events & (FD_CLOSE)))
861 watch->pollfd.revents |= G_IO_HUP;
864 /* Regardless of WSAEnumNetworkEvents() result, if watching for
865 * writability, unless last write would have blocked set
866 * G_IO_OUT. But never set both G_IO_OUT and G_IO_HUP.
868 if (!(watch->pollfd.revents & G_IO_HUP) &&
869 !channel->write_would_have_blocked &&
870 (channel->event_mask & FD_WRITE))
871 watch->pollfd.revents |= G_IO_OUT;
874 g_print ("\n revents={%s} retval={%s}\n",
875 condition_to_string (watch->pollfd.revents),
876 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
878 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
881 g_assert_not_reached ();
887 g_io_win32_dispatch (GSource *source,
888 GSourceFunc callback,
891 GIOFunc func = (GIOFunc)callback;
892 GIOWin32Watch *watch = (GIOWin32Watch *)source;
893 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
894 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
898 g_warning (G_STRLOC ": GIOWin32Watch dispatched without callback\n"
899 "You must call g_source_connect().");
904 g_print ("g_io_win32_dispatch: pollfd.revents=%s condition=%s result=%s\n",
905 condition_to_string (watch->pollfd.revents),
906 condition_to_string (watch->condition),
907 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
909 return (*func) (watch->channel,
910 (watch->pollfd.revents | buffer_condition) & watch->condition,
915 g_io_win32_finalize (GSource *source)
917 GIOWin32Watch *watch = (GIOWin32Watch *)source;
918 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
921 g_print ("g_io_win32_finalize: source=%p channel=%p", source, channel);
923 switch (channel->type)
925 case G_IO_WIN32_WINDOWS_MESSAGES:
930 case G_IO_WIN32_CONSOLE:
935 case G_IO_WIN32_FILE_DESC:
937 g_print (" FD thread=%#x", channel->thread_id);
940 case G_IO_WIN32_SOCKET:
942 g_print (" SOCK sock=%d", channel->fd);
944 CloseHandle ((HANDLE) watch->pollfd.fd);
946 channel->event_mask = 0;
951 g_assert_not_reached ();
956 g_io_channel_unref (watch->channel);
959 GSourceFuncs g_io_watch_funcs = {
967 g_io_win32_msg_read (GIOChannel *channel,
973 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
974 MSG msg; /* In case of alignment problems */
976 if (count < sizeof (MSG))
978 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
979 "Incorrect message size"); /* Informative enough error message? */
980 return G_IO_STATUS_ERROR;
983 if (win32_channel->debug)
984 g_print ("g_io_win32_msg_read: channel=%p hwnd=%#x\n",
985 channel, (guint) win32_channel->hwnd);
986 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
987 return G_IO_STATUS_AGAIN;
989 memmove (buf, &msg, sizeof (MSG));
990 *bytes_read = sizeof (MSG);
992 return G_IO_STATUS_NORMAL;
996 g_io_win32_msg_write (GIOChannel *channel,
999 gsize *bytes_written,
1002 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1005 if (count != sizeof (MSG))
1007 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
1008 "Incorrect message size"); /* Informative enough error message? */
1009 return G_IO_STATUS_ERROR;
1012 /* In case of alignment problems */
1013 memmove (&msg, buf, sizeof (MSG));
1014 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
1016 gchar *emsg = g_win32_error_message (GetLastError ());
1017 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, emsg);
1019 return G_IO_STATUS_ERROR;
1022 *bytes_written = sizeof (MSG);
1024 return G_IO_STATUS_NORMAL;
1028 g_io_win32_msg_close (GIOChannel *channel,
1031 /* Nothing to be done. Or should we set hwnd to some invalid value? */
1033 return G_IO_STATUS_NORMAL;
1037 g_io_win32_free (GIOChannel *channel)
1039 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1041 if (win32_channel->debug)
1042 g_print ("g_io_win32_free channel=%p fd=%d\n", channel, win32_channel->fd);
1044 if (win32_channel->data_avail_event)
1045 CloseHandle (win32_channel->data_avail_event);
1046 if (win32_channel->space_avail_event)
1047 CloseHandle (win32_channel->space_avail_event);
1048 if (win32_channel->type == G_IO_WIN32_SOCKET)
1049 WSAEventSelect (win32_channel->fd, NULL, 0);
1050 DeleteCriticalSection (&win32_channel->mutex);
1052 g_free (win32_channel->buffer);
1053 g_free (win32_channel);
1057 g_io_win32_msg_create_watch (GIOChannel *channel,
1058 GIOCondition condition)
1060 GIOWin32Watch *watch;
1063 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1064 watch = (GIOWin32Watch *)source;
1066 watch->channel = channel;
1067 g_io_channel_ref (channel);
1069 watch->condition = condition;
1071 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
1072 watch->pollfd.events = condition;
1074 g_source_add_poll (source, &watch->pollfd);
1080 g_io_win32_fd_and_console_read (GIOChannel *channel,
1086 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1089 if (win32_channel->debug)
1090 g_print ("g_io_win32_fd_read: fd=%d count=%d\n",
1091 win32_channel->fd, count);
1093 if (win32_channel->thread_id)
1095 return buffer_read (win32_channel, buf, count, bytes_read, err);
1098 result = read (win32_channel->fd, buf, count);
1100 if (win32_channel->debug)
1101 g_print ("g_io_win32_fd_read: read() => %d\n", result);
1111 return G_IO_STATUS_AGAIN;
1114 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1115 g_io_channel_error_from_errno (errno),
1116 g_strerror (errno));
1117 return G_IO_STATUS_ERROR;
1121 *bytes_read = result;
1123 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
1127 g_io_win32_fd_and_console_write (GIOChannel *channel,
1130 gsize *bytes_written,
1133 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1136 if (win32_channel->thread_id)
1138 return buffer_write (win32_channel, buf, count, bytes_written, err);
1141 result = write (win32_channel->fd, buf, count);
1142 if (win32_channel->debug)
1143 g_print ("g_io_win32_fd_write: fd=%d count=%d => %d\n",
1144 win32_channel->fd, count, result);
1154 return G_IO_STATUS_AGAIN;
1157 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1158 g_io_channel_error_from_errno (errno),
1159 g_strerror (errno));
1160 return G_IO_STATUS_ERROR;
1164 *bytes_written = result;
1166 return G_IO_STATUS_NORMAL;
1170 g_io_win32_fd_seek (GIOChannel *channel,
1175 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1192 whence = -1; /* Keep the compiler quiet */
1193 g_assert_not_reached ();
1197 tmp_offset = offset;
1198 if (tmp_offset != offset)
1200 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1201 g_io_channel_error_from_errno (EINVAL),
1202 g_strerror (EINVAL));
1203 return G_IO_STATUS_ERROR;
1206 result = lseek (win32_channel->fd, tmp_offset, whence);
1210 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1211 g_io_channel_error_from_errno (errno),
1212 g_strerror (errno));
1213 return G_IO_STATUS_ERROR;
1216 return G_IO_STATUS_NORMAL;
1220 g_io_win32_fd_close (GIOChannel *channel,
1223 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1225 if (win32_channel->debug)
1226 g_print ("g_io_win32_fd_close: thread=%#x: fd=%d\n",
1227 win32_channel->thread_id,
1229 LOCK (win32_channel->mutex);
1230 if (win32_channel->running)
1232 if (win32_channel->debug)
1233 g_print ("thread %#x: running, marking fd %d for later close\n",
1234 win32_channel->thread_id, win32_channel->fd);
1235 win32_channel->running = FALSE;
1236 win32_channel->needs_close = TRUE;
1237 if (win32_channel->direction == 0)
1238 SetEvent (win32_channel->data_avail_event);
1240 SetEvent (win32_channel->space_avail_event);
1244 if (win32_channel->debug)
1245 g_print ("closing fd %d\n", win32_channel->fd);
1246 close (win32_channel->fd);
1247 if (win32_channel->debug)
1248 g_print ("closed fd %d, setting to -1\n",
1250 win32_channel->fd = -1;
1252 UNLOCK (win32_channel->mutex);
1254 /* FIXME error detection? */
1256 return G_IO_STATUS_NORMAL;
1260 g_io_win32_fd_create_watch (GIOChannel *channel,
1261 GIOCondition condition)
1263 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1264 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1265 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1267 watch->channel = channel;
1268 g_io_channel_ref (channel);
1270 watch->condition = condition;
1272 if (win32_channel->data_avail_event == NULL)
1273 create_events (win32_channel);
1275 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
1276 watch->pollfd.events = condition;
1278 if (win32_channel->debug)
1279 g_print ("g_io_win32_fd_create_watch: channel=%p fd=%d condition={%s} event=%#x\n",
1280 channel, win32_channel->fd,
1281 condition_to_string (condition), watch->pollfd.fd);
1283 LOCK (win32_channel->mutex);
1284 if (win32_channel->thread_id == 0)
1286 if (condition & G_IO_IN)
1287 create_thread (win32_channel, condition, read_thread);
1288 else if (condition & G_IO_OUT)
1289 create_thread (win32_channel, condition, write_thread);
1292 g_source_add_poll (source, &watch->pollfd);
1293 UNLOCK (win32_channel->mutex);
1299 g_io_win32_console_close (GIOChannel *channel,
1302 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1304 if (close (win32_channel->fd) < 0)
1306 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1307 g_io_channel_error_from_errno (errno),
1308 g_strerror (errno));
1309 return G_IO_STATUS_ERROR;
1312 return G_IO_STATUS_NORMAL;
1316 g_io_win32_console_create_watch (GIOChannel *channel,
1317 GIOCondition condition)
1319 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1320 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1321 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1323 watch->channel = channel;
1324 g_io_channel_ref (channel);
1326 watch->condition = condition;
1328 watch->pollfd.fd = (gint) _get_osfhandle (win32_channel->fd);
1329 watch->pollfd.events = condition;
1331 g_source_add_poll (source, &watch->pollfd);
1337 g_io_win32_sock_read (GIOChannel *channel,
1343 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1345 GIOChannelError error;
1348 if (win32_channel->debug)
1349 g_print ("g_io_win32_sock_read: channel=%p sock=%d count=%d",
1350 channel, win32_channel->fd, count);
1352 result = recv (win32_channel->fd, buf, count, 0);
1353 if (result == SOCKET_ERROR)
1354 winsock_error = WSAGetLastError ();
1356 if (win32_channel->debug)
1357 g_print (" recv=%d", result);
1359 if (result == SOCKET_ERROR)
1361 gchar *emsg = g_win32_error_message (winsock_error);
1363 if (win32_channel->debug)
1364 g_print (" %s\n", emsg);
1368 switch (winsock_error)
1371 error = G_IO_CHANNEL_ERROR_INVAL;
1373 case WSAEWOULDBLOCK:
1375 return G_IO_STATUS_AGAIN;
1377 error = G_IO_CHANNEL_ERROR_FAILED;
1380 g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
1383 return G_IO_STATUS_ERROR;
1387 if (win32_channel->debug)
1389 *bytes_read = result;
1391 return G_IO_STATUS_EOF;
1393 return G_IO_STATUS_NORMAL;
1398 g_io_win32_sock_write (GIOChannel *channel,
1401 gsize *bytes_written,
1404 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1406 GIOChannelError error;
1409 if (win32_channel->debug)
1410 g_print ("g_io_win32_sock_write: channel=%p sock=%d count=%d",
1411 channel, win32_channel->fd, count);
1413 result = send (win32_channel->fd, buf, count, 0);
1414 if (result == SOCKET_ERROR)
1415 winsock_error = WSAGetLastError ();
1417 if (win32_channel->debug)
1418 g_print (" send=%d", result);
1420 if (result == SOCKET_ERROR)
1422 gchar *emsg = g_win32_error_message (winsock_error);
1424 if (win32_channel->debug)
1425 g_print (" %s\n", emsg);
1429 switch (winsock_error)
1432 error = G_IO_CHANNEL_ERROR_INVAL;
1434 case WSAEWOULDBLOCK:
1435 win32_channel->write_would_have_blocked = TRUE;
1436 win32_channel->last_events = 0;
1438 return G_IO_STATUS_AGAIN;
1440 error = G_IO_CHANNEL_ERROR_FAILED;
1443 g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
1446 return G_IO_STATUS_ERROR;
1450 if (win32_channel->debug)
1452 *bytes_written = result;
1453 win32_channel->write_would_have_blocked = FALSE;
1455 return G_IO_STATUS_NORMAL;
1460 g_io_win32_sock_close (GIOChannel *channel,
1463 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1465 if (win32_channel->fd != -1)
1467 if (win32_channel->debug)
1468 g_print ("g_io_win32_sock_close: channel=%p sock=%d\n",
1469 channel, win32_channel->fd);
1471 closesocket (win32_channel->fd);
1472 win32_channel->fd = -1;
1475 /* FIXME error detection? */
1477 return G_IO_STATUS_NORMAL;
1481 g_io_win32_sock_create_watch (GIOChannel *channel,
1482 GIOCondition condition)
1484 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1485 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1486 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1488 watch->channel = channel;
1489 g_io_channel_ref (channel);
1491 watch->condition = condition;
1493 if (win32_channel->event == 0)
1494 win32_channel->event = (int) WSACreateEvent ();
1496 watch->pollfd.fd = win32_channel->event;
1497 watch->pollfd.events = condition;
1499 if (win32_channel->debug)
1500 g_print ("g_io_win32_sock_create_watch: channel=%p sock=%d event=%#x condition={%s}\n",
1501 channel, win32_channel->fd, watch->pollfd.fd,
1502 condition_to_string (watch->condition));
1504 g_source_add_poll (source, &watch->pollfd);
1510 g_io_channel_new_file (const gchar *filename,
1514 int fid, flags, pmode;
1515 GIOChannel *channel;
1517 enum { /* Cheesy hack */
1524 g_return_val_if_fail (filename != NULL, NULL);
1525 g_return_val_if_fail (mode != NULL, NULL);
1526 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
1540 g_warning ("Invalid GIOFileMode %s.\n", mode);
1549 if (mode[2] == '\0')
1551 mode_num |= MODE_PLUS;
1556 g_warning ("Invalid GIOFileMode %s.\n", mode);
1567 flags = O_WRONLY | O_TRUNC | O_CREAT;
1571 flags = O_WRONLY | O_APPEND | O_CREAT;
1574 case MODE_R | MODE_PLUS:
1576 pmode = _S_IREAD | _S_IWRITE;
1578 case MODE_W | MODE_PLUS:
1579 flags = O_RDWR | O_TRUNC | O_CREAT;
1580 pmode = _S_IREAD | _S_IWRITE;
1582 case MODE_A | MODE_PLUS:
1583 flags = O_RDWR | O_APPEND | O_CREAT;
1584 pmode = _S_IREAD | _S_IWRITE;
1587 g_assert_not_reached ();
1591 /* always open 'untranslated' */
1592 fid = g_open (filename, flags | _O_BINARY, pmode);
1594 if (g_io_win32_get_debug_flag ())
1596 g_print ("g_io_channel_win32_new_file: open(\"%s\",", filename);
1597 g_win32_print_access_mode (flags|_O_BINARY);
1598 g_print (",%#o)=%d\n", pmode, fid);
1603 g_set_error_literal (error, G_FILE_ERROR,
1604 g_file_error_from_errno (errno),
1605 g_strerror (errno));
1606 return (GIOChannel *)NULL;
1609 channel = g_io_channel_win32_new_fd (fid);
1611 /* XXX: move this to g_io_channel_win32_new_fd () */
1612 channel->close_on_unref = TRUE;
1613 channel->is_seekable = TRUE;
1615 /* g_io_channel_win32_new_fd sets is_readable and is_writeable to
1616 * correspond to actual readability/writeability. Set to FALSE those
1617 * that mode doesn't allow
1622 channel->is_writeable = FALSE;
1626 channel->is_readable = FALSE;
1628 case MODE_R | MODE_PLUS:
1629 case MODE_W | MODE_PLUS:
1630 case MODE_A | MODE_PLUS:
1633 g_assert_not_reached ();
1642 #undef g_io_channel_new_file
1644 /* Binary compatibility version. Not for newly compiled code. */
1647 g_io_channel_new_file (const gchar *filename,
1651 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1654 if (utf8_filename == NULL)
1657 retval = g_io_channel_new_file_utf8 (utf8_filename, mode, error);
1659 g_free (utf8_filename);
1667 g_io_win32_unimpl_set_flags (GIOChannel *channel,
1671 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1673 if (win32_channel->debug)
1675 g_print ("g_io_win32_unimpl_set_flags: ");
1676 g_win32_print_gioflags (flags);
1680 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1681 G_IO_CHANNEL_ERROR_FAILED,
1682 "Not implemented on Win32");
1684 return G_IO_STATUS_ERROR;
1688 g_io_win32_fd_get_flags_internal (GIOChannel *channel,
1691 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1695 if (st->st_mode & _S_IFIFO)
1697 channel->is_readable =
1698 (PeekNamedPipe ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL, NULL) != 0) || GetLastError () == ERROR_BROKEN_PIPE;
1699 channel->is_writeable =
1700 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1701 channel->is_seekable = FALSE;
1705 channel->is_readable =
1706 (ReadFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1707 channel->is_writeable =
1708 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1709 channel->is_seekable = TRUE;
1712 /* XXX: G_IO_FLAG_APPEND */
1713 /* XXX: G_IO_FLAG_NONBLOCK */
1719 g_io_win32_fd_get_flags (GIOChannel *channel)
1722 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1724 g_return_val_if_fail (win32_channel != NULL, 0);
1725 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_FILE_DESC, 0);
1727 if (0 == fstat (win32_channel->fd, &st))
1728 return g_io_win32_fd_get_flags_internal (channel, &st);
1734 g_io_win32_console_get_flags_internal (GIOChannel *channel)
1736 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1737 HANDLE handle = (HANDLE) _get_osfhandle (win32_channel->fd);
1740 INPUT_RECORD record;
1742 channel->is_readable = PeekConsoleInput (handle, &record, 1, &count);
1743 channel->is_writeable = WriteFile (handle, &c, 0, &count, NULL);
1744 channel->is_seekable = FALSE;
1750 g_io_win32_console_get_flags (GIOChannel *channel)
1752 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1754 g_return_val_if_fail (win32_channel != NULL, 0);
1755 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_CONSOLE, 0);
1757 return g_io_win32_console_get_flags_internal (channel);
1761 g_io_win32_msg_get_flags (GIOChannel *channel)
1767 g_io_win32_sock_set_flags (GIOChannel *channel,
1771 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1774 if (win32_channel->debug)
1776 g_print ("g_io_win32_sock_set_flags: ");
1777 g_win32_print_gioflags (flags);
1781 if (flags & G_IO_FLAG_NONBLOCK)
1784 if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1786 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1788 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1789 G_IO_CHANNEL_ERROR_FAILED,
1793 return G_IO_STATUS_ERROR;
1799 if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1801 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1803 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1804 G_IO_CHANNEL_ERROR_FAILED,
1808 return G_IO_STATUS_ERROR;
1812 return G_IO_STATUS_NORMAL;
1816 g_io_win32_sock_get_flags (GIOChannel *channel)
1818 /* Could we do something here? */
1822 static GIOFuncs win32_channel_msg_funcs = {
1823 g_io_win32_msg_read,
1824 g_io_win32_msg_write,
1826 g_io_win32_msg_close,
1827 g_io_win32_msg_create_watch,
1829 g_io_win32_unimpl_set_flags,
1830 g_io_win32_msg_get_flags,
1833 static GIOFuncs win32_channel_fd_funcs = {
1834 g_io_win32_fd_and_console_read,
1835 g_io_win32_fd_and_console_write,
1837 g_io_win32_fd_close,
1838 g_io_win32_fd_create_watch,
1840 g_io_win32_unimpl_set_flags,
1841 g_io_win32_fd_get_flags,
1844 static GIOFuncs win32_channel_console_funcs = {
1845 g_io_win32_fd_and_console_read,
1846 g_io_win32_fd_and_console_write,
1848 g_io_win32_console_close,
1849 g_io_win32_console_create_watch,
1851 g_io_win32_unimpl_set_flags,
1852 g_io_win32_console_get_flags,
1855 static GIOFuncs win32_channel_sock_funcs = {
1856 g_io_win32_sock_read,
1857 g_io_win32_sock_write,
1859 g_io_win32_sock_close,
1860 g_io_win32_sock_create_watch,
1862 g_io_win32_sock_set_flags,
1863 g_io_win32_sock_get_flags,
1867 g_io_channel_win32_new_messages (guint hwnd)
1869 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1870 GIOChannel *channel = (GIOChannel *)win32_channel;
1872 g_io_channel_init (channel);
1873 g_io_channel_win32_init (win32_channel);
1874 if (win32_channel->debug)
1875 g_print ("g_io_channel_win32_new_messages: channel=%p hwnd=%#x\n",
1877 channel->funcs = &win32_channel_msg_funcs;
1878 win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
1879 win32_channel->hwnd = (HWND) hwnd;
1881 /* XXX: check this. */
1882 channel->is_readable = IsWindow (win32_channel->hwnd);
1883 channel->is_writeable = IsWindow (win32_channel->hwnd);
1885 channel->is_seekable = FALSE;
1891 g_io_channel_win32_new_fd_internal (gint fd,
1894 GIOWin32Channel *win32_channel;
1895 GIOChannel *channel;
1897 win32_channel = g_new (GIOWin32Channel, 1);
1898 channel = (GIOChannel *)win32_channel;
1900 g_io_channel_init (channel);
1901 g_io_channel_win32_init (win32_channel);
1903 win32_channel->fd = fd;
1905 if (win32_channel->debug)
1906 g_print ("g_io_channel_win32_new_fd: channel=%p fd=%u\n",
1909 if (st->st_mode & _S_IFCHR) /* console */
1911 channel->funcs = &win32_channel_console_funcs;
1912 win32_channel->type = G_IO_WIN32_CONSOLE;
1913 g_io_win32_console_get_flags_internal (channel);
1917 channel->funcs = &win32_channel_fd_funcs;
1918 win32_channel->type = G_IO_WIN32_FILE_DESC;
1919 g_io_win32_fd_get_flags_internal (channel, st);
1926 g_io_channel_win32_new_fd (gint fd)
1930 if (fstat (fd, &st) == -1)
1932 g_warning (G_STRLOC ": %d isn't a C library file descriptor", fd);
1936 return g_io_channel_win32_new_fd_internal (fd, &st);
1940 g_io_channel_win32_get_fd (GIOChannel *channel)
1942 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1944 return win32_channel->fd;
1948 g_io_channel_win32_new_socket (int socket)
1950 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1951 GIOChannel *channel = (GIOChannel *)win32_channel;
1953 g_io_channel_init (channel);
1954 g_io_channel_win32_init (win32_channel);
1955 if (win32_channel->debug)
1956 g_print ("g_io_channel_win32_new_socket: channel=%p sock=%d\n",
1958 channel->funcs = &win32_channel_sock_funcs;
1959 win32_channel->type = G_IO_WIN32_SOCKET;
1960 win32_channel->fd = socket;
1962 channel->is_readable = TRUE;
1963 channel->is_writeable = TRUE;
1964 channel->is_seekable = FALSE;
1970 g_io_channel_unix_new (gint fd)
1972 gboolean is_fd, is_socket;
1976 is_fd = (fstat (fd, &st) == 0);
1978 optlen = sizeof (optval);
1979 is_socket = (getsockopt (fd, SOL_SOCKET, SO_TYPE, (char *) &optval, &optlen) != SOCKET_ERROR);
1981 if (is_fd && is_socket)
1982 g_warning (G_STRLOC ": %d is both a file descriptor and a socket, file descriptor interpretation assumed.", fd);
1985 return g_io_channel_win32_new_fd_internal (fd, &st);
1988 return g_io_channel_win32_new_socket(fd);
1990 g_warning (G_STRLOC ": %d is neither a file descriptor or a socket", fd);
1996 g_io_channel_unix_get_fd (GIOChannel *channel)
1998 return g_io_channel_win32_get_fd (channel);
2002 g_io_channel_win32_set_debug (GIOChannel *channel,
2005 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2007 win32_channel->debug = flag;
2011 g_io_channel_win32_poll (GPollFD *fds,
2017 g_return_val_if_fail (n_fds >= 0, 0);
2019 result = (*g_main_context_get_poll_func (NULL)) (fds, n_fds, timeout);
2025 g_io_channel_win32_make_pollfd (GIOChannel *channel,
2026 GIOCondition condition,
2029 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2031 switch (win32_channel->type)
2033 case G_IO_WIN32_FILE_DESC:
2034 if (win32_channel->data_avail_event == NULL)
2035 create_events (win32_channel);
2037 fd->fd = (gint) win32_channel->data_avail_event;
2039 if (win32_channel->thread_id == 0 && (condition & G_IO_IN))
2041 if (condition & G_IO_IN)
2042 create_thread (win32_channel, condition, read_thread);
2043 else if (condition & G_IO_OUT)
2044 create_thread (win32_channel, condition, write_thread);
2048 case G_IO_WIN32_CONSOLE:
2049 fd->fd = (gint) _get_osfhandle (win32_channel->fd);
2052 case G_IO_WIN32_SOCKET:
2053 fd->fd = (int) WSACreateEvent ();
2056 case G_IO_WIN32_WINDOWS_MESSAGES:
2057 fd->fd = G_WIN32_MSG_HANDLE;
2061 g_assert_not_reached ();
2065 fd->events = condition;
2068 /* Binary compatibility */
2070 g_io_channel_win32_new_stream_socket (int socket)
2072 return g_io_channel_win32_new_socket (socket);
2075 #define __G_IO_WIN32_C__
2076 #include "galiasdef.c"