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;
737 if ((event_mask & FD_WRITE) && !channel->write_would_have_blocked)
740 g_print (" WSASetEvent(%#x)", watch->pollfd.fd);
741 WSASetEvent ((WSAEVENT) watch->pollfd.fd);
747 g_assert_not_reached ();
753 return ((watch->condition & buffer_condition) == watch->condition);
757 g_io_win32_check (GSource *source)
760 GIOWin32Watch *watch = (GIOWin32Watch *)source;
761 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
762 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
763 WSANETWORKEVENTS events;
766 g_print ("g_io_win32_check: source=%p channel=%p", source, channel);
768 switch (channel->type)
770 case G_IO_WIN32_WINDOWS_MESSAGES:
773 return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
775 case G_IO_WIN32_FILE_DESC:
777 g_print (" FD thread=%#x buffer_condition=%s\n"
778 " watch->pollfd.events={%s} watch->pollfd.revents={%s} channel->revents={%s}\n",
779 channel->thread_id, condition_to_string (buffer_condition),
780 condition_to_string (watch->pollfd.events),
781 condition_to_string (watch->pollfd.revents),
782 condition_to_string (channel->revents));
784 watch->pollfd.revents = (watch->pollfd.events & channel->revents);
786 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
788 case G_IO_WIN32_CONSOLE:
791 if (watch->channel->is_writeable)
793 else if (watch->channel->is_readable)
797 if (PeekConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n) &&
800 /* _kbhit() does quite complex processing to find out
801 * whether at least one of the key events pending corresponds
802 * to a "real" character that can be read.
807 /* Discard all other kinds of events */
808 ReadConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n);
813 case G_IO_WIN32_SOCKET:
816 if (channel->last_events & FD_WRITE)
819 g_print (" sock=%d event=%#x last_events has FD_WRITE",
820 channel->fd, watch->pollfd.fd);
824 WSAEnumNetworkEvents (channel->fd, 0, &events);
827 g_print ("\n revents={%s} condition={%s}"
828 "\n WSAEnumNetworkEvents(%d,0) sets events={%s}",
829 condition_to_string (watch->pollfd.revents),
830 condition_to_string (watch->condition),
832 event_mask_to_string (events.lNetworkEvents));
834 if (watch->pollfd.revents != 0 &&
835 events.lNetworkEvents == 0 &&
836 !(channel->event_mask & FD_WRITE))
838 channel->event_mask = 0;
840 g_print ("\n WSAEventSelect(%d,%#x,{})",
841 channel->fd, watch->pollfd.fd);
842 WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd, 0);
844 g_print (" ResetEvent(%#x)",
846 ResetEvent ((HANDLE) watch->pollfd.fd);
848 channel->last_events = events.lNetworkEvents;
850 watch->pollfd.revents = 0;
851 if (channel->last_events & (FD_READ | FD_ACCEPT))
852 watch->pollfd.revents |= G_IO_IN;
853 if (channel->last_events & FD_WRITE)
854 watch->pollfd.revents |= G_IO_OUT;
857 /* We have called WSAEnumNetworkEvents() above but it didn't
860 if (events.lNetworkEvents & FD_CONNECT)
862 if (events.iErrorCode[FD_CONNECT_BIT] == 0)
863 watch->pollfd.revents |= G_IO_OUT;
865 watch->pollfd.revents |= (G_IO_HUP | G_IO_ERR);
867 if (watch->pollfd.revents == 0 && (channel->last_events & (FD_CLOSE)))
868 watch->pollfd.revents |= G_IO_HUP;
871 /* Regardless of WSAEnumNetworkEvents() result, if watching for
872 * writability, unless last write would have blocked set
873 * G_IO_OUT. But never set both G_IO_OUT and G_IO_HUP.
875 if (!(watch->pollfd.revents & G_IO_HUP) &&
876 !channel->write_would_have_blocked &&
877 (channel->event_mask & FD_WRITE))
878 watch->pollfd.revents |= G_IO_OUT;
881 g_print ("\n revents={%s} retval={%s}\n",
882 condition_to_string (watch->pollfd.revents),
883 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
885 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
888 g_assert_not_reached ();
894 g_io_win32_dispatch (GSource *source,
895 GSourceFunc callback,
898 GIOFunc func = (GIOFunc)callback;
899 GIOWin32Watch *watch = (GIOWin32Watch *)source;
900 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
901 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
905 g_warning (G_STRLOC ": GIOWin32Watch dispatched without callback\n"
906 "You must call g_source_connect().");
911 g_print ("g_io_win32_dispatch: pollfd.revents=%s condition=%s result=%s\n",
912 condition_to_string (watch->pollfd.revents),
913 condition_to_string (watch->condition),
914 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
916 return (*func) (watch->channel,
917 (watch->pollfd.revents | buffer_condition) & watch->condition,
922 g_io_win32_finalize (GSource *source)
924 GIOWin32Watch *watch = (GIOWin32Watch *)source;
925 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
928 g_print ("g_io_win32_finalize: source=%p channel=%p", source, channel);
930 switch (channel->type)
932 case G_IO_WIN32_WINDOWS_MESSAGES:
937 case G_IO_WIN32_CONSOLE:
942 case G_IO_WIN32_FILE_DESC:
944 g_print (" FD thread=%#x", channel->thread_id);
947 case G_IO_WIN32_SOCKET:
949 g_print (" SOCK sock=%d", channel->fd);
951 CloseHandle ((HANDLE) watch->pollfd.fd);
953 channel->event_mask = 0;
958 g_assert_not_reached ();
963 g_io_channel_unref (watch->channel);
966 GSourceFuncs g_io_watch_funcs = {
974 g_io_win32_msg_read (GIOChannel *channel,
980 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
981 MSG msg; /* In case of alignment problems */
983 if (count < sizeof (MSG))
985 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
986 "Incorrect message size"); /* Informative enough error message? */
987 return G_IO_STATUS_ERROR;
990 if (win32_channel->debug)
991 g_print ("g_io_win32_msg_read: channel=%p hwnd=%#x\n",
992 channel, (guint) win32_channel->hwnd);
993 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
994 return G_IO_STATUS_AGAIN;
996 memmove (buf, &msg, sizeof (MSG));
997 *bytes_read = sizeof (MSG);
999 return G_IO_STATUS_NORMAL;
1003 g_io_win32_msg_write (GIOChannel *channel,
1006 gsize *bytes_written,
1009 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1012 if (count != sizeof (MSG))
1014 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
1015 "Incorrect message size"); /* Informative enough error message? */
1016 return G_IO_STATUS_ERROR;
1019 /* In case of alignment problems */
1020 memmove (&msg, buf, sizeof (MSG));
1021 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
1023 gchar *emsg = g_win32_error_message (GetLastError ());
1024 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, emsg);
1026 return G_IO_STATUS_ERROR;
1029 *bytes_written = sizeof (MSG);
1031 return G_IO_STATUS_NORMAL;
1035 g_io_win32_msg_close (GIOChannel *channel,
1038 /* Nothing to be done. Or should we set hwnd to some invalid value? */
1040 return G_IO_STATUS_NORMAL;
1044 g_io_win32_free (GIOChannel *channel)
1046 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1048 if (win32_channel->debug)
1049 g_print ("g_io_win32_free channel=%p fd=%d\n", channel, win32_channel->fd);
1051 if (win32_channel->data_avail_event)
1052 CloseHandle (win32_channel->data_avail_event);
1053 if (win32_channel->space_avail_event)
1054 CloseHandle (win32_channel->space_avail_event);
1055 if (win32_channel->type == G_IO_WIN32_SOCKET)
1056 WSAEventSelect (win32_channel->fd, NULL, 0);
1057 DeleteCriticalSection (&win32_channel->mutex);
1059 g_free (win32_channel->buffer);
1060 g_free (win32_channel);
1064 g_io_win32_msg_create_watch (GIOChannel *channel,
1065 GIOCondition condition)
1067 GIOWin32Watch *watch;
1070 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1071 watch = (GIOWin32Watch *)source;
1073 watch->channel = channel;
1074 g_io_channel_ref (channel);
1076 watch->condition = condition;
1078 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
1079 watch->pollfd.events = condition;
1081 g_source_add_poll (source, &watch->pollfd);
1087 g_io_win32_fd_and_console_read (GIOChannel *channel,
1093 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1096 if (win32_channel->debug)
1097 g_print ("g_io_win32_fd_read: fd=%d count=%d\n",
1098 win32_channel->fd, count);
1100 if (win32_channel->thread_id)
1102 return buffer_read (win32_channel, buf, count, bytes_read, err);
1105 result = read (win32_channel->fd, buf, count);
1107 if (win32_channel->debug)
1108 g_print ("g_io_win32_fd_read: read() => %d\n", result);
1118 return G_IO_STATUS_AGAIN;
1121 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1122 g_io_channel_error_from_errno (errno),
1123 g_strerror (errno));
1124 return G_IO_STATUS_ERROR;
1128 *bytes_read = result;
1130 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
1134 g_io_win32_fd_and_console_write (GIOChannel *channel,
1137 gsize *bytes_written,
1140 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1143 if (win32_channel->thread_id)
1145 return buffer_write (win32_channel, buf, count, bytes_written, err);
1148 result = write (win32_channel->fd, buf, count);
1149 if (win32_channel->debug)
1150 g_print ("g_io_win32_fd_write: fd=%d count=%d => %d\n",
1151 win32_channel->fd, count, result);
1161 return G_IO_STATUS_AGAIN;
1164 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1165 g_io_channel_error_from_errno (errno),
1166 g_strerror (errno));
1167 return G_IO_STATUS_ERROR;
1171 *bytes_written = result;
1173 return G_IO_STATUS_NORMAL;
1177 g_io_win32_fd_seek (GIOChannel *channel,
1182 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1199 whence = -1; /* Keep the compiler quiet */
1200 g_assert_not_reached ();
1204 tmp_offset = offset;
1205 if (tmp_offset != offset)
1207 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1208 g_io_channel_error_from_errno (EINVAL),
1209 g_strerror (EINVAL));
1210 return G_IO_STATUS_ERROR;
1213 result = lseek (win32_channel->fd, tmp_offset, whence);
1217 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1218 g_io_channel_error_from_errno (errno),
1219 g_strerror (errno));
1220 return G_IO_STATUS_ERROR;
1223 return G_IO_STATUS_NORMAL;
1227 g_io_win32_fd_close (GIOChannel *channel,
1230 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1232 if (win32_channel->debug)
1233 g_print ("g_io_win32_fd_close: thread=%#x: fd=%d\n",
1234 win32_channel->thread_id,
1236 LOCK (win32_channel->mutex);
1237 if (win32_channel->running)
1239 if (win32_channel->debug)
1240 g_print ("thread %#x: running, marking fd %d for later close\n",
1241 win32_channel->thread_id, win32_channel->fd);
1242 win32_channel->running = FALSE;
1243 win32_channel->needs_close = TRUE;
1244 if (win32_channel->direction == 0)
1245 SetEvent (win32_channel->data_avail_event);
1247 SetEvent (win32_channel->space_avail_event);
1251 if (win32_channel->debug)
1252 g_print ("closing fd %d\n", win32_channel->fd);
1253 close (win32_channel->fd);
1254 if (win32_channel->debug)
1255 g_print ("closed fd %d, setting to -1\n",
1257 win32_channel->fd = -1;
1259 UNLOCK (win32_channel->mutex);
1261 /* FIXME error detection? */
1263 return G_IO_STATUS_NORMAL;
1267 g_io_win32_fd_create_watch (GIOChannel *channel,
1268 GIOCondition condition)
1270 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1271 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1272 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1274 watch->channel = channel;
1275 g_io_channel_ref (channel);
1277 watch->condition = condition;
1279 if (win32_channel->data_avail_event == NULL)
1280 create_events (win32_channel);
1282 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
1283 watch->pollfd.events = condition;
1285 if (win32_channel->debug)
1286 g_print ("g_io_win32_fd_create_watch: channel=%p fd=%d condition={%s} event=%#x\n",
1287 channel, win32_channel->fd,
1288 condition_to_string (condition), watch->pollfd.fd);
1290 LOCK (win32_channel->mutex);
1291 if (win32_channel->thread_id == 0)
1293 if (condition & G_IO_IN)
1294 create_thread (win32_channel, condition, read_thread);
1295 else if (condition & G_IO_OUT)
1296 create_thread (win32_channel, condition, write_thread);
1299 g_source_add_poll (source, &watch->pollfd);
1300 UNLOCK (win32_channel->mutex);
1306 g_io_win32_console_close (GIOChannel *channel,
1309 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1311 if (close (win32_channel->fd) < 0)
1313 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1314 g_io_channel_error_from_errno (errno),
1315 g_strerror (errno));
1316 return G_IO_STATUS_ERROR;
1319 return G_IO_STATUS_NORMAL;
1323 g_io_win32_console_create_watch (GIOChannel *channel,
1324 GIOCondition condition)
1326 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1327 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1328 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1330 watch->channel = channel;
1331 g_io_channel_ref (channel);
1333 watch->condition = condition;
1335 watch->pollfd.fd = (gint) _get_osfhandle (win32_channel->fd);
1336 watch->pollfd.events = condition;
1338 g_source_add_poll (source, &watch->pollfd);
1344 g_io_win32_sock_read (GIOChannel *channel,
1350 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1352 GIOChannelError error;
1355 if (win32_channel->debug)
1356 g_print ("g_io_win32_sock_read: channel=%p sock=%d count=%d",
1357 channel, win32_channel->fd, count);
1359 result = recv (win32_channel->fd, buf, count, 0);
1360 if (result == SOCKET_ERROR)
1361 winsock_error = WSAGetLastError ();
1363 if (win32_channel->debug)
1364 g_print (" recv=%d", result);
1366 if (result == SOCKET_ERROR)
1368 gchar *emsg = g_win32_error_message (winsock_error);
1370 if (win32_channel->debug)
1371 g_print (" %s\n", emsg);
1375 switch (winsock_error)
1378 error = G_IO_CHANNEL_ERROR_INVAL;
1380 case WSAEWOULDBLOCK:
1382 return G_IO_STATUS_AGAIN;
1384 error = G_IO_CHANNEL_ERROR_FAILED;
1387 g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
1390 return G_IO_STATUS_ERROR;
1394 if (win32_channel->debug)
1396 *bytes_read = result;
1398 return G_IO_STATUS_EOF;
1400 return G_IO_STATUS_NORMAL;
1405 g_io_win32_sock_write (GIOChannel *channel,
1408 gsize *bytes_written,
1411 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1413 GIOChannelError error;
1416 if (win32_channel->debug)
1417 g_print ("g_io_win32_sock_write: channel=%p sock=%d count=%d",
1418 channel, win32_channel->fd, count);
1420 result = send (win32_channel->fd, buf, count, 0);
1421 if (result == SOCKET_ERROR)
1422 winsock_error = WSAGetLastError ();
1424 if (win32_channel->debug)
1425 g_print (" send=%d", result);
1427 if (result == SOCKET_ERROR)
1429 gchar *emsg = g_win32_error_message (winsock_error);
1431 if (win32_channel->debug)
1432 g_print (" %s\n", emsg);
1436 switch (winsock_error)
1439 error = G_IO_CHANNEL_ERROR_INVAL;
1441 case WSAEWOULDBLOCK:
1442 win32_channel->write_would_have_blocked = TRUE;
1443 win32_channel->last_events = 0;
1445 return G_IO_STATUS_AGAIN;
1447 error = G_IO_CHANNEL_ERROR_FAILED;
1450 g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
1453 return G_IO_STATUS_ERROR;
1457 if (win32_channel->debug)
1459 *bytes_written = result;
1460 win32_channel->write_would_have_blocked = FALSE;
1462 return G_IO_STATUS_NORMAL;
1467 g_io_win32_sock_close (GIOChannel *channel,
1470 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1472 if (win32_channel->fd != -1)
1474 if (win32_channel->debug)
1475 g_print ("g_io_win32_sock_close: channel=%p sock=%d\n",
1476 channel, win32_channel->fd);
1478 closesocket (win32_channel->fd);
1479 win32_channel->fd = -1;
1482 /* FIXME error detection? */
1484 return G_IO_STATUS_NORMAL;
1488 g_io_win32_sock_create_watch (GIOChannel *channel,
1489 GIOCondition condition)
1491 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1492 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1493 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1495 watch->channel = channel;
1496 g_io_channel_ref (channel);
1498 watch->condition = condition;
1500 if (win32_channel->event == 0)
1501 win32_channel->event = (int) WSACreateEvent ();
1503 watch->pollfd.fd = win32_channel->event;
1504 watch->pollfd.events = condition;
1506 if (win32_channel->debug)
1507 g_print ("g_io_win32_sock_create_watch: channel=%p sock=%d event=%#x condition={%s}\n",
1508 channel, win32_channel->fd, watch->pollfd.fd,
1509 condition_to_string (watch->condition));
1511 g_source_add_poll (source, &watch->pollfd);
1517 g_io_channel_new_file (const gchar *filename,
1521 int fid, flags, pmode;
1522 GIOChannel *channel;
1524 enum { /* Cheesy hack */
1531 g_return_val_if_fail (filename != NULL, NULL);
1532 g_return_val_if_fail (mode != NULL, NULL);
1533 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
1547 g_warning ("Invalid GIOFileMode %s.\n", mode);
1556 if (mode[2] == '\0')
1558 mode_num |= MODE_PLUS;
1563 g_warning ("Invalid GIOFileMode %s.\n", mode);
1574 flags = O_WRONLY | O_TRUNC | O_CREAT;
1578 flags = O_WRONLY | O_APPEND | O_CREAT;
1581 case MODE_R | MODE_PLUS:
1583 pmode = _S_IREAD | _S_IWRITE;
1585 case MODE_W | MODE_PLUS:
1586 flags = O_RDWR | O_TRUNC | O_CREAT;
1587 pmode = _S_IREAD | _S_IWRITE;
1589 case MODE_A | MODE_PLUS:
1590 flags = O_RDWR | O_APPEND | O_CREAT;
1591 pmode = _S_IREAD | _S_IWRITE;
1594 g_assert_not_reached ();
1598 /* always open 'untranslated' */
1599 fid = g_open (filename, flags | _O_BINARY, pmode);
1601 if (g_io_win32_get_debug_flag ())
1603 g_print ("g_io_channel_win32_new_file: open(\"%s\",", filename);
1604 g_win32_print_access_mode (flags|_O_BINARY);
1605 g_print (",%#o)=%d\n", pmode, fid);
1610 g_set_error_literal (error, G_FILE_ERROR,
1611 g_file_error_from_errno (errno),
1612 g_strerror (errno));
1613 return (GIOChannel *)NULL;
1616 channel = g_io_channel_win32_new_fd (fid);
1618 /* XXX: move this to g_io_channel_win32_new_fd () */
1619 channel->close_on_unref = TRUE;
1620 channel->is_seekable = TRUE;
1622 /* g_io_channel_win32_new_fd sets is_readable and is_writeable to
1623 * correspond to actual readability/writeability. Set to FALSE those
1624 * that mode doesn't allow
1629 channel->is_writeable = FALSE;
1633 channel->is_readable = FALSE;
1635 case MODE_R | MODE_PLUS:
1636 case MODE_W | MODE_PLUS:
1637 case MODE_A | MODE_PLUS:
1640 g_assert_not_reached ();
1647 #if !defined (_WIN64)
1649 #undef g_io_channel_new_file
1651 /* Binary compatibility version. Not for newly compiled code. */
1654 g_io_channel_new_file (const gchar *filename,
1658 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1661 if (utf8_filename == NULL)
1664 retval = g_io_channel_new_file_utf8 (utf8_filename, mode, error);
1666 g_free (utf8_filename);
1674 g_io_win32_unimpl_set_flags (GIOChannel *channel,
1678 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1680 if (win32_channel->debug)
1682 g_print ("g_io_win32_unimpl_set_flags: ");
1683 g_win32_print_gioflags (flags);
1687 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1688 G_IO_CHANNEL_ERROR_FAILED,
1689 "Not implemented on Win32");
1691 return G_IO_STATUS_ERROR;
1695 g_io_win32_fd_get_flags_internal (GIOChannel *channel,
1698 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1702 if (st->st_mode & _S_IFIFO)
1704 channel->is_readable =
1705 (PeekNamedPipe ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL, NULL) != 0) || GetLastError () == ERROR_BROKEN_PIPE;
1706 channel->is_writeable =
1707 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1708 channel->is_seekable = FALSE;
1712 channel->is_readable =
1713 (ReadFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1714 channel->is_writeable =
1715 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1716 channel->is_seekable = TRUE;
1719 /* XXX: G_IO_FLAG_APPEND */
1720 /* XXX: G_IO_FLAG_NONBLOCK */
1726 g_io_win32_fd_get_flags (GIOChannel *channel)
1729 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1731 g_return_val_if_fail (win32_channel != NULL, 0);
1732 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_FILE_DESC, 0);
1734 if (0 == fstat (win32_channel->fd, &st))
1735 return g_io_win32_fd_get_flags_internal (channel, &st);
1741 g_io_win32_console_get_flags_internal (GIOChannel *channel)
1743 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1744 HANDLE handle = (HANDLE) _get_osfhandle (win32_channel->fd);
1747 INPUT_RECORD record;
1749 channel->is_readable = PeekConsoleInput (handle, &record, 1, &count);
1750 channel->is_writeable = WriteFile (handle, &c, 0, &count, NULL);
1751 channel->is_seekable = FALSE;
1757 g_io_win32_console_get_flags (GIOChannel *channel)
1759 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1761 g_return_val_if_fail (win32_channel != NULL, 0);
1762 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_CONSOLE, 0);
1764 return g_io_win32_console_get_flags_internal (channel);
1768 g_io_win32_msg_get_flags (GIOChannel *channel)
1774 g_io_win32_sock_set_flags (GIOChannel *channel,
1778 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1781 if (win32_channel->debug)
1783 g_print ("g_io_win32_sock_set_flags: ");
1784 g_win32_print_gioflags (flags);
1788 if (flags & G_IO_FLAG_NONBLOCK)
1791 if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1793 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1795 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1796 G_IO_CHANNEL_ERROR_FAILED,
1800 return G_IO_STATUS_ERROR;
1806 if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1808 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1810 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1811 G_IO_CHANNEL_ERROR_FAILED,
1815 return G_IO_STATUS_ERROR;
1819 return G_IO_STATUS_NORMAL;
1823 g_io_win32_sock_get_flags (GIOChannel *channel)
1825 /* Could we do something here? */
1829 static GIOFuncs win32_channel_msg_funcs = {
1830 g_io_win32_msg_read,
1831 g_io_win32_msg_write,
1833 g_io_win32_msg_close,
1834 g_io_win32_msg_create_watch,
1836 g_io_win32_unimpl_set_flags,
1837 g_io_win32_msg_get_flags,
1840 static GIOFuncs win32_channel_fd_funcs = {
1841 g_io_win32_fd_and_console_read,
1842 g_io_win32_fd_and_console_write,
1844 g_io_win32_fd_close,
1845 g_io_win32_fd_create_watch,
1847 g_io_win32_unimpl_set_flags,
1848 g_io_win32_fd_get_flags,
1851 static GIOFuncs win32_channel_console_funcs = {
1852 g_io_win32_fd_and_console_read,
1853 g_io_win32_fd_and_console_write,
1855 g_io_win32_console_close,
1856 g_io_win32_console_create_watch,
1858 g_io_win32_unimpl_set_flags,
1859 g_io_win32_console_get_flags,
1862 static GIOFuncs win32_channel_sock_funcs = {
1863 g_io_win32_sock_read,
1864 g_io_win32_sock_write,
1866 g_io_win32_sock_close,
1867 g_io_win32_sock_create_watch,
1869 g_io_win32_sock_set_flags,
1870 g_io_win32_sock_get_flags,
1874 g_io_channel_win32_new_messages (guint hwnd)
1876 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1877 GIOChannel *channel = (GIOChannel *)win32_channel;
1879 g_io_channel_init (channel);
1880 g_io_channel_win32_init (win32_channel);
1881 if (win32_channel->debug)
1882 g_print ("g_io_channel_win32_new_messages: channel=%p hwnd=%#x\n",
1884 channel->funcs = &win32_channel_msg_funcs;
1885 win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
1886 win32_channel->hwnd = (HWND) hwnd;
1888 /* XXX: check this. */
1889 channel->is_readable = IsWindow (win32_channel->hwnd);
1890 channel->is_writeable = IsWindow (win32_channel->hwnd);
1892 channel->is_seekable = FALSE;
1898 g_io_channel_win32_new_fd_internal (gint fd,
1901 GIOWin32Channel *win32_channel;
1902 GIOChannel *channel;
1904 win32_channel = g_new (GIOWin32Channel, 1);
1905 channel = (GIOChannel *)win32_channel;
1907 g_io_channel_init (channel);
1908 g_io_channel_win32_init (win32_channel);
1910 win32_channel->fd = fd;
1912 if (win32_channel->debug)
1913 g_print ("g_io_channel_win32_new_fd: channel=%p fd=%u\n",
1916 if (st->st_mode & _S_IFCHR) /* console */
1918 channel->funcs = &win32_channel_console_funcs;
1919 win32_channel->type = G_IO_WIN32_CONSOLE;
1920 g_io_win32_console_get_flags_internal (channel);
1924 channel->funcs = &win32_channel_fd_funcs;
1925 win32_channel->type = G_IO_WIN32_FILE_DESC;
1926 g_io_win32_fd_get_flags_internal (channel, st);
1933 g_io_channel_win32_new_fd (gint fd)
1937 if (fstat (fd, &st) == -1)
1939 g_warning (G_STRLOC ": %d isn't a C library file descriptor", fd);
1943 return g_io_channel_win32_new_fd_internal (fd, &st);
1947 g_io_channel_win32_get_fd (GIOChannel *channel)
1949 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1951 return win32_channel->fd;
1955 g_io_channel_win32_new_socket (int socket)
1957 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1958 GIOChannel *channel = (GIOChannel *)win32_channel;
1960 g_io_channel_init (channel);
1961 g_io_channel_win32_init (win32_channel);
1962 if (win32_channel->debug)
1963 g_print ("g_io_channel_win32_new_socket: channel=%p sock=%d\n",
1965 channel->funcs = &win32_channel_sock_funcs;
1966 win32_channel->type = G_IO_WIN32_SOCKET;
1967 win32_channel->fd = socket;
1969 channel->is_readable = TRUE;
1970 channel->is_writeable = TRUE;
1971 channel->is_seekable = FALSE;
1977 g_io_channel_unix_new (gint fd)
1979 gboolean is_fd, is_socket;
1983 is_fd = (fstat (fd, &st) == 0);
1985 optlen = sizeof (optval);
1986 is_socket = (getsockopt (fd, SOL_SOCKET, SO_TYPE, (char *) &optval, &optlen) != SOCKET_ERROR);
1988 if (is_fd && is_socket)
1989 g_warning (G_STRLOC ": %d is both a file descriptor and a socket, file descriptor interpretation assumed.", fd);
1992 return g_io_channel_win32_new_fd_internal (fd, &st);
1995 return g_io_channel_win32_new_socket(fd);
1997 g_warning (G_STRLOC ": %d is neither a file descriptor or a socket", fd);
2003 g_io_channel_unix_get_fd (GIOChannel *channel)
2005 return g_io_channel_win32_get_fd (channel);
2009 g_io_channel_win32_set_debug (GIOChannel *channel,
2012 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2014 win32_channel->debug = flag;
2018 g_io_channel_win32_poll (GPollFD *fds,
2024 g_return_val_if_fail (n_fds >= 0, 0);
2026 result = (*g_main_context_get_poll_func (NULL)) (fds, n_fds, timeout);
2032 g_io_channel_win32_make_pollfd (GIOChannel *channel,
2033 GIOCondition condition,
2036 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2038 switch (win32_channel->type)
2040 case G_IO_WIN32_FILE_DESC:
2041 if (win32_channel->data_avail_event == NULL)
2042 create_events (win32_channel);
2044 fd->fd = (gint) win32_channel->data_avail_event;
2046 if (win32_channel->thread_id == 0 && (condition & G_IO_IN))
2048 if (condition & G_IO_IN)
2049 create_thread (win32_channel, condition, read_thread);
2050 else if (condition & G_IO_OUT)
2051 create_thread (win32_channel, condition, write_thread);
2055 case G_IO_WIN32_CONSOLE:
2056 fd->fd = (gint) _get_osfhandle (win32_channel->fd);
2059 case G_IO_WIN32_SOCKET:
2060 fd->fd = (int) WSACreateEvent ();
2063 case G_IO_WIN32_WINDOWS_MESSAGES:
2064 fd->fd = G_WIN32_MSG_HANDLE;
2068 g_assert_not_reached ();
2072 fd->events = condition;
2075 /* Binary compatibility */
2077 g_io_channel_win32_new_stream_socket (int socket)
2079 return g_io_channel_win32_new_socket (socket);
2082 #define __G_IO_WIN32_C__
2083 #include "galiasdef.c"