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 winsock_error_message (int number)
233 static char unk[100];
237 return "Interrupted function call";
239 return "Permission denied";
241 return "Bad address";
243 return "Invalid argument";
245 return "Too many open sockets";
247 return "Resource temporarily unavailable";
249 return "Operation now in progress";
251 return "Operation already in progress";
253 return "Socket operation on nonsocket";
254 case WSAEDESTADDRREQ:
255 return "Destination address required";
257 return "Message too long";
259 return "Protocol wrong type for socket";
261 return "Bad protocol option";
262 case WSAEPROTONOSUPPORT:
263 return "Protocol not supported";
264 case WSAESOCKTNOSUPPORT:
265 return "Socket type not supported";
267 return "Operation not supported on transport endpoint";
268 case WSAEPFNOSUPPORT:
269 return "Protocol family not supported";
270 case WSAEAFNOSUPPORT:
271 return "Address family not supported by protocol family";
273 return "Address already in use";
274 case WSAEADDRNOTAVAIL:
275 return "Address not available";
277 return "Network interface is not configured";
279 return "Network is unreachable";
281 return "Network dropped connection on reset";
282 case WSAECONNABORTED:
283 return "Software caused connection abort";
285 return "Connection reset by peer";
287 return "No buffer space available";
289 return "Socket is already connected";
291 return "Socket is not connected";
293 return "Can't send after socket shutdown";
295 return "Connection timed out";
296 case WSAECONNREFUSED:
297 return "Connection refused";
299 return "Host is down";
300 case WSAEHOSTUNREACH:
301 return "Host is unreachable";
303 return "Too many processes";
305 return "Network subsystem is unavailable";
306 case WSAVERNOTSUPPORTED:
307 return "Winsock.dll version out of range";
308 case WSANOTINITIALISED:
309 return "Successful WSAStartup not yet performed";
311 return "Graceful shutdown in progress";
312 case WSATYPE_NOT_FOUND:
313 return "Class type not found";
314 case WSAHOST_NOT_FOUND:
315 return "Host not found";
317 return "Nonauthoritative host not found";
319 return "This is a nonrecoverable error";
321 return "Valid name, no data record of requested type";
322 case WSA_INVALID_HANDLE:
323 return "Specified event object handle is invalid";
324 case WSA_INVALID_PARAMETER:
325 return "One or more parameters are invalid";
326 case WSA_IO_INCOMPLETE:
327 return "Overlapped I/O event object not in signaled state";
328 case WSA_NOT_ENOUGH_MEMORY:
329 return "Insufficient memory available";
330 case WSA_OPERATION_ABORTED:
331 return "Overlapped operation aborted";
332 case WSAEINVALIDPROCTABLE:
333 return "Invalid procedure table from service provider";
334 case WSAEINVALIDPROVIDER:
335 return "Invalid service provider version number";
336 case WSAEPROVIDERFAILEDINIT:
337 return "Unable to initialize a service provider";
338 case WSASYSCALLFAILURE:
339 return "System call failure";
341 sprintf (unk, "Unknown WinSock error %d", number);
347 g_io_channel_win32_init (GIOWin32Channel *channel)
349 channel->debug = g_io_win32_get_debug_flag ();
350 channel->buffer = NULL;
351 channel->running = FALSE;
352 channel->needs_close = FALSE;
353 channel->thread_id = 0;
354 channel->data_avail_event = NULL;
355 channel->revents = 0;
356 channel->space_avail_event = NULL;
357 channel->event_mask = 0;
358 channel->last_events = 0;
360 channel->write_would_have_blocked = FALSE;
361 InitializeCriticalSection (&channel->mutex);
365 create_events (GIOWin32Channel *channel)
367 SECURITY_ATTRIBUTES sec_attrs;
369 sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
370 sec_attrs.lpSecurityDescriptor = NULL;
371 sec_attrs.bInheritHandle = FALSE;
373 /* The data available event is manual reset, the space available event
374 * is automatic reset.
376 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
377 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
379 gchar *emsg = g_win32_error_message (GetLastError ());
380 g_error ("Error creating event: %s", emsg);
385 static unsigned __stdcall
386 read_thread (void *parameter)
388 GIOWin32Channel *channel = parameter;
392 g_io_channel_ref ((GIOChannel *)channel);
395 g_print ("read_thread %#x: start fd=%d, data_avail=%#x space_avail=%#x\n",
398 (guint) channel->data_avail_event,
399 (guint) channel->space_avail_event);
401 channel->direction = 0;
402 channel->buffer = g_malloc (BUFFER_SIZE);
403 channel->rdp = channel->wrp = 0;
404 channel->running = TRUE;
406 SetEvent (channel->space_avail_event);
408 LOCK (channel->mutex);
409 while (channel->running)
412 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
413 channel->thread_id, channel->rdp, channel->wrp);
414 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
418 g_print ("read_thread %#x: resetting space_avail\n",
420 ResetEvent (channel->space_avail_event);
422 g_print ("read_thread %#x: waiting for space\n",
424 UNLOCK (channel->mutex);
425 WaitForSingleObject (channel->space_avail_event, INFINITE);
426 LOCK (channel->mutex);
428 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
429 channel->thread_id, channel->rdp, channel->wrp);
432 buffer = channel->buffer + channel->wrp;
434 /* Always leave at least one byte unused gap to be able to
435 * distinguish between the full and empty condition...
437 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
438 BUFFER_SIZE - channel->wrp);
441 g_print ("read_thread %#x: calling read() for %d bytes\n",
442 channel->thread_id, nbytes);
444 UNLOCK (channel->mutex);
446 nbytes = read (channel->fd, buffer, nbytes);
448 LOCK (channel->mutex);
450 channel->revents = G_IO_IN;
452 channel->revents |= G_IO_HUP;
454 channel->revents |= G_IO_ERR;
457 g_print ("read_thread %#x: read() returned %d, rdp=%d, wrp=%d\n",
458 channel->thread_id, nbytes, channel->rdp, channel->wrp);
463 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
465 g_print ("read_thread %#x: rdp=%d, wrp=%d, setting data_avail\n",
466 channel->thread_id, channel->rdp, channel->wrp);
467 SetEvent (channel->data_avail_event);
470 channel->running = FALSE;
471 if (channel->needs_close)
474 g_print ("read_thread %#x: channel fd %d needs closing\n",
475 channel->thread_id, channel->fd);
481 g_print ("read_thread %#x: EOF, rdp=%d, wrp=%d, setting data_avail\n",
482 channel->thread_id, channel->rdp, channel->wrp);
483 SetEvent (channel->data_avail_event);
484 UNLOCK (channel->mutex);
486 g_io_channel_unref ((GIOChannel *)channel);
488 /* No need to call _endthreadex(), the actual thread starter routine
489 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
490 * _endthreadex() for us.
496 static unsigned __stdcall
497 write_thread (void *parameter)
499 GIOWin32Channel *channel = parameter;
503 g_io_channel_ref ((GIOChannel *)channel);
506 g_print ("write_thread %#x: start fd=%d, data_avail=%#x space_avail=%#x\n",
509 (guint) channel->data_avail_event,
510 (guint) channel->space_avail_event);
512 channel->direction = 1;
513 channel->buffer = g_malloc (BUFFER_SIZE);
514 channel->rdp = channel->wrp = 0;
515 channel->running = TRUE;
517 SetEvent (channel->space_avail_event);
519 /* We use the same event objects as for a reader thread, but with
520 * reversed meaning. So, space_avail is used if data is available
521 * for writing, and data_avail is used if space is available in the
525 LOCK (channel->mutex);
526 while (channel->running || channel->rdp != channel->wrp)
529 g_print ("write_thread %#x: rdp=%d, wrp=%d\n",
530 channel->thread_id, channel->rdp, channel->wrp);
531 if (channel->wrp == channel->rdp)
533 /* Buffer is empty. */
535 g_print ("write_thread %#x: resetting space_avail\n",
537 ResetEvent (channel->space_avail_event);
539 g_print ("write_thread %#x: waiting for data\n",
541 channel->revents = G_IO_OUT;
542 SetEvent (channel->data_avail_event);
543 UNLOCK (channel->mutex);
544 WaitForSingleObject (channel->space_avail_event, INFINITE);
546 LOCK (channel->mutex);
547 if (channel->rdp == channel->wrp)
551 g_print ("write_thread %#x: rdp=%d, wrp=%d\n",
552 channel->thread_id, channel->rdp, channel->wrp);
555 buffer = channel->buffer + channel->rdp;
556 if (channel->rdp < channel->wrp)
557 nbytes = channel->wrp - channel->rdp;
559 nbytes = BUFFER_SIZE - channel->rdp;
562 g_print ("write_thread %#x: calling write() for %d bytes\n",
563 channel->thread_id, nbytes);
565 UNLOCK (channel->mutex);
566 nbytes = write (channel->fd, buffer, nbytes);
567 LOCK (channel->mutex);
570 g_print ("write_thread %#x: write(%i) returned %d, rdp=%d, wrp=%d\n",
571 channel->thread_id, channel->fd, nbytes, channel->rdp, channel->wrp);
573 channel->revents = 0;
575 channel->revents |= G_IO_OUT;
576 else if (nbytes <= 0)
577 channel->revents |= G_IO_ERR;
579 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
585 g_print ("write_thread: setting data_avail for thread %#x\n",
587 SetEvent (channel->data_avail_event);
590 channel->running = FALSE;
591 if (channel->needs_close)
594 g_print ("write_thread %#x: channel fd %d needs closing\n",
595 channel->thread_id, channel->fd);
600 UNLOCK (channel->mutex);
602 g_io_channel_unref ((GIOChannel *)channel);
608 create_thread (GIOWin32Channel *channel,
609 GIOCondition condition,
610 unsigned (__stdcall *thread) (void *parameter))
612 HANDLE thread_handle;
614 thread_handle = (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0,
615 &channel->thread_id);
616 if (thread_handle == 0)
617 g_warning (G_STRLOC ": Error creating reader thread: %s",
619 else if (!CloseHandle (thread_handle))
620 g_warning (G_STRLOC ": Error closing thread handle: %s\n",
621 g_win32_error_message (GetLastError ()));
623 WaitForSingleObject (channel->space_avail_event, INFINITE);
627 buffer_read (GIOWin32Channel *channel,
636 LOCK (channel->mutex);
638 g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
639 channel->thread_id, count, channel->rdp, channel->wrp);
641 if (channel->wrp == channel->rdp)
643 UNLOCK (channel->mutex);
645 g_print ("waiting for data from thread %#x\n", channel->thread_id);
646 WaitForSingleObject (channel->data_avail_event, INFINITE);
648 g_print ("done waiting for data from thread %#x\n", channel->thread_id);
649 LOCK (channel->mutex);
650 if (channel->wrp == channel->rdp && !channel->running)
653 g_print ("wrp==rdp, !running\n");
654 UNLOCK (channel->mutex);
656 return G_IO_STATUS_EOF;
660 if (channel->rdp < channel->wrp)
661 nbytes = channel->wrp - channel->rdp;
663 nbytes = BUFFER_SIZE - channel->rdp;
664 UNLOCK (channel->mutex);
665 nbytes = MIN (left, nbytes);
667 g_print ("moving %d bytes from thread %#x\n",
668 nbytes, channel->thread_id);
669 memcpy (dest, channel->buffer + channel->rdp, nbytes);
672 LOCK (channel->mutex);
673 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
675 g_print ("setting space_avail for thread %#x\n", channel->thread_id);
676 SetEvent (channel->space_avail_event);
678 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
679 channel->thread_id, channel->rdp, channel->wrp);
680 if (channel->running && channel->wrp == channel->rdp)
683 g_print ("resetting data_avail of thread %#x\n",
685 ResetEvent (channel->data_avail_event);
687 UNLOCK (channel->mutex);
689 /* We have no way to indicate any errors form the actual
690 * read() or recv() call in the reader thread. Should we have?
692 *bytes_read = count - left;
693 return (*bytes_read > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
698 buffer_write (GIOWin32Channel *channel,
701 gsize *bytes_written,
707 LOCK (channel->mutex);
709 g_print ("buffer_write: writing to thread %#x %d bytes, rdp=%d, wrp=%d\n",
710 channel->thread_id, count, channel->rdp, channel->wrp);
712 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
716 g_print ("buffer_write: tid %#x: resetting data_avail\n",
718 ResetEvent (channel->data_avail_event);
720 g_print ("buffer_write: tid %#x: waiting for space\n",
722 UNLOCK (channel->mutex);
723 WaitForSingleObject (channel->data_avail_event, INFINITE);
724 LOCK (channel->mutex);
726 g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d\n",
727 channel->thread_id, channel->rdp, channel->wrp);
730 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
731 BUFFER_SIZE - channel->wrp);
733 UNLOCK (channel->mutex);
734 nbytes = MIN (left, nbytes);
736 g_print ("buffer_write: tid %#x: writing %d bytes\n",
737 channel->thread_id, nbytes);
738 memcpy (channel->buffer + channel->wrp, dest, nbytes);
741 LOCK (channel->mutex);
743 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
745 g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d, setting space_avail\n",
746 channel->thread_id, channel->rdp, channel->wrp);
747 SetEvent (channel->space_avail_event);
749 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
753 g_print ("buffer_write: tid %#x: resetting data_avail\n",
755 ResetEvent (channel->data_avail_event);
758 UNLOCK (channel->mutex);
760 /* We have no way to indicate any errors form the actual
761 * write() call in the writer thread. Should we have?
763 *bytes_written = count - left;
764 return (*bytes_written > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
769 g_io_win32_prepare (GSource *source,
772 GIOWin32Watch *watch = (GIOWin32Watch *)source;
773 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
774 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
780 g_print ("g_io_win32_prepare: source=%p channel=%p", source, channel);
782 switch (channel->type)
784 case G_IO_WIN32_WINDOWS_MESSAGES:
789 case G_IO_WIN32_CONSOLE:
794 case G_IO_WIN32_FILE_DESC:
796 g_print (" FD thread=%#x buffer_condition:{%s}"
797 "\n watch->pollfd.events:{%s} watch->pollfd.revents:{%s} channel->revents:{%s}",
798 channel->thread_id, condition_to_string (buffer_condition),
799 condition_to_string (watch->pollfd.events),
800 condition_to_string (watch->pollfd.revents),
801 condition_to_string (channel->revents));
803 LOCK (channel->mutex);
804 if (channel->running)
806 if (channel->direction == 0 && channel->wrp == channel->rdp)
809 g_print ("\n setting revents=0");
810 channel->revents = 0;
815 if (channel->direction == 1
816 && (channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
819 g_print ("\n setting revents=0");
820 channel->revents = 0;
823 UNLOCK (channel->mutex);
826 case G_IO_WIN32_SOCKET:
830 if (watch->condition & G_IO_IN)
831 event_mask |= (FD_READ | FD_ACCEPT);
832 if (watch->condition & G_IO_OUT)
833 event_mask |= (FD_WRITE | FD_CONNECT);
834 event_mask |= FD_CLOSE;
836 if (channel->event_mask != event_mask /* || channel->event != watch->pollfd.fd*/)
839 g_print ("\n WSAEventSelect(%d,%#x,{%s})",
840 channel->fd, watch->pollfd.fd,
841 event_mask_to_string (event_mask));
842 if (WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd,
843 event_mask) == SOCKET_ERROR)
845 channel->event_mask = event_mask;
847 channel->event = watch->pollfd.fd;
850 g_print ("\n setting last_events=0");
851 channel->last_events = 0;
856 g_assert_not_reached ();
862 return ((watch->condition & buffer_condition) == watch->condition);
866 g_io_win32_check (GSource *source)
869 GIOWin32Watch *watch = (GIOWin32Watch *)source;
870 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
871 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
872 WSANETWORKEVENTS events;
875 g_print ("g_io_win32_check: source=%p channel=%p", source, channel);
877 switch (channel->type)
879 case G_IO_WIN32_WINDOWS_MESSAGES:
882 return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
884 case G_IO_WIN32_FILE_DESC:
886 g_print (" FD thread=%#x buffer_condition=%s\n"
887 " watch->pollfd.events={%s} watch->pollfd.revents={%s} channel->revents={%s}\n",
888 channel->thread_id, condition_to_string (buffer_condition),
889 condition_to_string (watch->pollfd.events),
890 condition_to_string (watch->pollfd.revents),
891 condition_to_string (channel->revents));
893 watch->pollfd.revents = (watch->pollfd.events & channel->revents);
895 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
897 case G_IO_WIN32_CONSOLE:
900 if (watch->channel->is_writeable)
902 else if (watch->channel->is_readable)
906 if (PeekConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n) &&
909 /* _kbhit() does quite complex processing to find out
910 * whether at least one of the key events pending corresponds
911 * to a "real" character that can be read.
916 /* Discard all other kinds of events */
917 ReadConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n);
922 case G_IO_WIN32_SOCKET:
925 if (channel->last_events & FD_WRITE)
928 g_print (" sock=%d event=%#x last_events has FD_WRITE",
929 channel->fd, watch->pollfd.fd);
933 WSAEnumNetworkEvents (channel->fd, 0, &events);
936 g_print ("\n revents={%s} condition={%s}"
937 "\n WSAEnumNetworkEvents(%d,0) sets events={%s}",
938 condition_to_string (watch->pollfd.revents),
939 condition_to_string (watch->condition),
941 event_mask_to_string (events.lNetworkEvents));
943 if (watch->pollfd.revents != 0 &&
944 events.lNetworkEvents == 0 &&
945 !(channel->event_mask & FD_WRITE))
947 channel->event_mask = 0;
949 g_print ("\n WSAEventSelect(%d,%#x,{})",
950 channel->fd, watch->pollfd.fd);
951 WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd, 0);
953 g_print (" ResetEvent(%#x)",
955 ResetEvent ((HANDLE) watch->pollfd.fd);
957 channel->last_events = events.lNetworkEvents;
959 watch->pollfd.revents = 0;
960 if (channel->last_events & (FD_READ | FD_ACCEPT))
961 watch->pollfd.revents |= G_IO_IN;
962 if (channel->last_events & FD_WRITE)
963 watch->pollfd.revents |= G_IO_OUT;
966 /* We have called WSAEnumNetworkEvents() above but it didn't
969 if (events.lNetworkEvents & FD_CONNECT)
971 if (events.iErrorCode[FD_CONNECT_BIT] == 0)
972 watch->pollfd.revents |= G_IO_OUT;
974 watch->pollfd.revents |= (G_IO_HUP | G_IO_ERR);
976 if (watch->pollfd.revents == 0 && (channel->last_events & (FD_CLOSE)))
977 watch->pollfd.revents |= G_IO_HUP;
980 /* Regardless of WSAEnumNetworkEvents() result, if watching for
981 * writability, unless last write would have blocked set
982 * G_IO_OUT. But never set both G_IO_OUT and G_IO_HUP.
984 if (!(watch->pollfd.revents & G_IO_HUP) &&
985 !channel->write_would_have_blocked &&
986 (channel->event_mask & FD_WRITE))
987 watch->pollfd.revents |= G_IO_OUT;
990 g_print ("\n revents={%s} retval={%s}\n",
991 condition_to_string (watch->pollfd.revents),
992 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
994 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
997 g_assert_not_reached ();
1003 g_io_win32_dispatch (GSource *source,
1004 GSourceFunc callback,
1007 GIOFunc func = (GIOFunc)callback;
1008 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1009 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
1010 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
1014 g_warning (G_STRLOC ": GIOWin32Watch dispatched without callback\n"
1015 "You must call g_source_connect().");
1020 g_print ("g_io_win32_dispatch: pollfd.revents=%s condition=%s result=%s\n",
1021 condition_to_string (watch->pollfd.revents),
1022 condition_to_string (watch->condition),
1023 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
1025 return (*func) (watch->channel,
1026 (watch->pollfd.revents | buffer_condition) & watch->condition,
1031 g_io_win32_finalize (GSource *source)
1033 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1034 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
1037 g_print ("g_io_win32_finalize: source=%p channel=%p", source, channel);
1039 switch (channel->type)
1041 case G_IO_WIN32_WINDOWS_MESSAGES:
1046 case G_IO_WIN32_CONSOLE:
1051 case G_IO_WIN32_FILE_DESC:
1053 g_print (" FD thread=%#x", channel->thread_id);
1056 case G_IO_WIN32_SOCKET:
1058 g_print (" SOCK sock=%d", channel->fd);
1060 CloseHandle ((HANDLE) watch->pollfd.fd);
1062 channel->event_mask = 0;
1067 g_assert_not_reached ();
1072 g_io_channel_unref (watch->channel);
1075 GSourceFuncs g_io_watch_funcs = {
1078 g_io_win32_dispatch,
1083 g_io_win32_msg_read (GIOChannel *channel,
1089 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1090 MSG msg; /* In case of alignment problems */
1092 if (count < sizeof (MSG))
1094 g_set_error (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
1095 "Incorrect message size"); /* Informative enough error message? */
1096 return G_IO_STATUS_ERROR;
1099 if (win32_channel->debug)
1100 g_print ("g_io_win32_msg_read: channel=%p hwnd=%#x\n",
1101 channel, (guint) win32_channel->hwnd);
1102 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
1103 return G_IO_STATUS_AGAIN;
1105 memmove (buf, &msg, sizeof (MSG));
1106 *bytes_read = sizeof (MSG);
1108 return G_IO_STATUS_NORMAL;
1112 g_io_win32_msg_write (GIOChannel *channel,
1115 gsize *bytes_written,
1118 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1121 if (count != sizeof (MSG))
1123 g_set_error (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
1124 "Incorrect message size"); /* Informative enough error message? */
1125 return G_IO_STATUS_ERROR;
1128 /* In case of alignment problems */
1129 memmove (&msg, buf, sizeof (MSG));
1130 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
1132 gchar *emsg = g_win32_error_message (GetLastError ());
1133 g_set_error (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, emsg);
1135 return G_IO_STATUS_ERROR;
1138 *bytes_written = sizeof (MSG);
1140 return G_IO_STATUS_NORMAL;
1144 g_io_win32_msg_close (GIOChannel *channel,
1147 /* Nothing to be done. Or should we set hwnd to some invalid value? */
1149 return G_IO_STATUS_NORMAL;
1153 g_io_win32_free (GIOChannel *channel)
1155 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1157 if (win32_channel->debug)
1158 g_print ("g_io_win32_free channel=%p fd=%d\n", channel, win32_channel->fd);
1160 if (win32_channel->data_avail_event)
1161 CloseHandle (win32_channel->data_avail_event);
1162 if (win32_channel->space_avail_event)
1163 CloseHandle (win32_channel->space_avail_event);
1164 if (win32_channel->type == G_IO_WIN32_SOCKET)
1165 WSAEventSelect (win32_channel->fd, NULL, 0);
1166 DeleteCriticalSection (&win32_channel->mutex);
1168 g_free (win32_channel->buffer);
1169 g_free (win32_channel);
1173 g_io_win32_msg_create_watch (GIOChannel *channel,
1174 GIOCondition condition)
1176 GIOWin32Watch *watch;
1179 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1180 watch = (GIOWin32Watch *)source;
1182 watch->channel = channel;
1183 g_io_channel_ref (channel);
1185 watch->condition = condition;
1187 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
1188 watch->pollfd.events = condition;
1190 g_source_add_poll (source, &watch->pollfd);
1196 g_io_win32_fd_and_console_read (GIOChannel *channel,
1202 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1205 if (win32_channel->debug)
1206 g_print ("g_io_win32_fd_read: fd=%d count=%d\n",
1207 win32_channel->fd, count);
1209 if (win32_channel->thread_id)
1211 return buffer_read (win32_channel, buf, count, bytes_read, err);
1214 result = read (win32_channel->fd, buf, count);
1216 if (win32_channel->debug)
1217 g_print ("g_io_win32_fd_read: read() => %d\n", result);
1227 return G_IO_STATUS_AGAIN;
1230 g_set_error (err, G_IO_CHANNEL_ERROR,
1231 g_io_channel_error_from_errno (errno),
1232 g_strerror (errno));
1233 return G_IO_STATUS_ERROR;
1237 *bytes_read = result;
1239 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
1243 g_io_win32_fd_and_console_write (GIOChannel *channel,
1246 gsize *bytes_written,
1249 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1252 if (win32_channel->thread_id)
1254 return buffer_write (win32_channel, buf, count, bytes_written, err);
1257 result = write (win32_channel->fd, buf, count);
1258 if (win32_channel->debug)
1259 g_print ("g_io_win32_fd_write: fd=%d count=%d => %d\n",
1260 win32_channel->fd, count, result);
1270 return G_IO_STATUS_AGAIN;
1273 g_set_error (err, G_IO_CHANNEL_ERROR,
1274 g_io_channel_error_from_errno (errno),
1275 g_strerror (errno));
1276 return G_IO_STATUS_ERROR;
1280 *bytes_written = result;
1282 return G_IO_STATUS_NORMAL;
1286 g_io_win32_fd_seek (GIOChannel *channel,
1291 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1308 whence = -1; /* Keep the compiler quiet */
1309 g_assert_not_reached ();
1313 tmp_offset = offset;
1314 if (tmp_offset != offset)
1316 g_set_error (err, G_IO_CHANNEL_ERROR,
1317 g_io_channel_error_from_errno (EINVAL),
1318 g_strerror (EINVAL));
1319 return G_IO_STATUS_ERROR;
1322 result = lseek (win32_channel->fd, tmp_offset, whence);
1326 g_set_error (err, G_IO_CHANNEL_ERROR,
1327 g_io_channel_error_from_errno (errno),
1328 g_strerror (errno));
1329 return G_IO_STATUS_ERROR;
1332 return G_IO_STATUS_NORMAL;
1336 g_io_win32_fd_close (GIOChannel *channel,
1339 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1341 if (win32_channel->debug)
1342 g_print ("g_io_win32_fd_close: thread=%#x: fd=%d\n",
1343 win32_channel->thread_id,
1345 LOCK (win32_channel->mutex);
1346 if (win32_channel->running)
1348 if (win32_channel->debug)
1349 g_print ("thread %#x: running, marking fd %d for later close\n",
1350 win32_channel->thread_id, win32_channel->fd);
1351 win32_channel->running = FALSE;
1352 win32_channel->needs_close = TRUE;
1353 if (win32_channel->direction == 0)
1354 SetEvent (win32_channel->data_avail_event);
1356 SetEvent (win32_channel->space_avail_event);
1360 if (win32_channel->debug)
1361 g_print ("closing fd %d\n", win32_channel->fd);
1362 close (win32_channel->fd);
1363 if (win32_channel->debug)
1364 g_print ("closed fd %d, setting to -1\n",
1366 win32_channel->fd = -1;
1368 UNLOCK (win32_channel->mutex);
1370 /* FIXME error detection? */
1372 return G_IO_STATUS_NORMAL;
1376 g_io_win32_fd_create_watch (GIOChannel *channel,
1377 GIOCondition condition)
1379 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1380 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1381 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1383 watch->channel = channel;
1384 g_io_channel_ref (channel);
1386 watch->condition = condition;
1388 if (win32_channel->data_avail_event == NULL)
1389 create_events (win32_channel);
1391 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
1392 watch->pollfd.events = condition;
1394 if (win32_channel->debug)
1395 g_print ("g_io_win32_fd_create_watch: channel=%p fd=%d condition={%s} event=%#x\n",
1396 channel, win32_channel->fd,
1397 condition_to_string (condition), watch->pollfd.fd);
1399 LOCK (win32_channel->mutex);
1400 if (win32_channel->thread_id == 0)
1402 if (condition & G_IO_IN)
1403 create_thread (win32_channel, condition, read_thread);
1404 else if (condition & G_IO_OUT)
1405 create_thread (win32_channel, condition, write_thread);
1408 g_source_add_poll (source, &watch->pollfd);
1409 UNLOCK (win32_channel->mutex);
1415 g_io_win32_console_close (GIOChannel *channel,
1418 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1420 if (close (win32_channel->fd) < 0)
1422 g_set_error (err, G_IO_CHANNEL_ERROR,
1423 g_io_channel_error_from_errno (errno),
1424 g_strerror (errno));
1425 return G_IO_STATUS_ERROR;
1428 return G_IO_STATUS_NORMAL;
1432 g_io_win32_console_create_watch (GIOChannel *channel,
1433 GIOCondition condition)
1435 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1436 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1437 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1439 watch->channel = channel;
1440 g_io_channel_ref (channel);
1442 watch->condition = condition;
1444 watch->pollfd.fd = (gint) _get_osfhandle (win32_channel->fd);
1445 watch->pollfd.events = condition;
1447 g_source_add_poll (source, &watch->pollfd);
1453 g_io_win32_sock_read (GIOChannel *channel,
1459 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1461 GIOChannelError error;
1464 if (win32_channel->debug)
1465 g_print ("g_io_win32_sock_read: channel=%p sock=%d count=%d",
1466 channel, win32_channel->fd, count);
1468 result = recv (win32_channel->fd, buf, count, 0);
1469 if (result == SOCKET_ERROR)
1470 winsock_error = WSAGetLastError ();
1472 if (win32_channel->debug)
1473 g_print (" recv=%d %s\n",
1475 (result == SOCKET_ERROR ? winsock_error_message (winsock_error) : ""));
1477 if (result == SOCKET_ERROR)
1481 switch (winsock_error)
1484 error = G_IO_CHANNEL_ERROR_INVAL;
1486 case WSAEWOULDBLOCK:
1487 return G_IO_STATUS_AGAIN;
1489 error = G_IO_CHANNEL_ERROR_FAILED;
1492 g_set_error (err, G_IO_CHANNEL_ERROR, error,
1493 winsock_error_message (winsock_error));
1494 return G_IO_STATUS_ERROR;
1498 *bytes_read = result;
1500 return G_IO_STATUS_EOF;
1502 return G_IO_STATUS_NORMAL;
1507 g_io_win32_sock_write (GIOChannel *channel,
1510 gsize *bytes_written,
1513 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1515 GIOChannelError error;
1518 if (win32_channel->debug)
1519 g_print ("g_io_win32_sock_write: channel=%p sock=%d count=%d",
1520 channel, win32_channel->fd, count);
1522 result = send (win32_channel->fd, buf, count, 0);
1523 if (result == SOCKET_ERROR)
1524 winsock_error = WSAGetLastError ();
1526 if (win32_channel->debug)
1527 g_print (" send=%d %s\n",
1529 (result == SOCKET_ERROR ? winsock_error_message (winsock_error) : ""));
1531 if (result == SOCKET_ERROR)
1535 switch (winsock_error)
1538 error = G_IO_CHANNEL_ERROR_INVAL;
1540 case WSAEWOULDBLOCK:
1541 win32_channel->write_would_have_blocked = TRUE;
1542 win32_channel->last_events = 0;
1543 return G_IO_STATUS_AGAIN;
1545 error = G_IO_CHANNEL_ERROR_FAILED;
1548 g_set_error (err, G_IO_CHANNEL_ERROR, error,
1549 winsock_error_message (winsock_error));
1551 return G_IO_STATUS_ERROR;
1555 *bytes_written = result;
1556 win32_channel->write_would_have_blocked = FALSE;
1558 return G_IO_STATUS_NORMAL;
1563 g_io_win32_sock_close (GIOChannel *channel,
1566 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1568 if (win32_channel->fd != -1)
1570 if (win32_channel->debug)
1571 g_print ("g_io_win32_sock_close: channel=%p sock=%d\n",
1572 channel, win32_channel->fd);
1574 closesocket (win32_channel->fd);
1575 win32_channel->fd = -1;
1578 /* FIXME error detection? */
1580 return G_IO_STATUS_NORMAL;
1584 g_io_win32_sock_create_watch (GIOChannel *channel,
1585 GIOCondition condition)
1587 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1588 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1589 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1591 watch->channel = channel;
1592 g_io_channel_ref (channel);
1594 watch->condition = condition;
1596 if (win32_channel->event == 0)
1597 win32_channel->event = (int) WSACreateEvent ();
1599 watch->pollfd.fd = win32_channel->event;
1600 watch->pollfd.events = condition;
1602 if (win32_channel->debug)
1603 g_print ("g_io_win32_sock_create_watch: channel=%p sock=%d event=%#x condition={%s}\n",
1604 channel, win32_channel->fd, watch->pollfd.fd,
1605 condition_to_string (watch->condition));
1607 g_source_add_poll (source, &watch->pollfd);
1613 g_io_channel_new_file (const gchar *filename,
1617 int fid, flags, pmode;
1618 GIOChannel *channel;
1620 enum { /* Cheesy hack */
1627 g_return_val_if_fail (filename != NULL, NULL);
1628 g_return_val_if_fail (mode != NULL, NULL);
1629 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
1643 g_warning ("Invalid GIOFileMode %s.\n", mode);
1652 if (mode[2] == '\0')
1654 mode_num |= MODE_PLUS;
1659 g_warning ("Invalid GIOFileMode %s.\n", mode);
1670 flags = O_WRONLY | O_TRUNC | O_CREAT;
1674 flags = O_WRONLY | O_APPEND | O_CREAT;
1677 case MODE_R | MODE_PLUS:
1679 pmode = _S_IREAD | _S_IWRITE;
1681 case MODE_W | MODE_PLUS:
1682 flags = O_RDWR | O_TRUNC | O_CREAT;
1683 pmode = _S_IREAD | _S_IWRITE;
1685 case MODE_A | MODE_PLUS:
1686 flags = O_RDWR | O_APPEND | O_CREAT;
1687 pmode = _S_IREAD | _S_IWRITE;
1690 g_assert_not_reached ();
1694 /* always open 'untranslated' */
1695 fid = g_open (filename, flags | _O_BINARY, pmode);
1697 if (g_io_win32_get_debug_flag ())
1699 g_print ("g_io_channel_win32_new_file: open(\"%s\",", filename);
1700 g_win32_print_access_mode (flags|_O_BINARY);
1701 g_print (",%#o)=%d\n", pmode, fid);
1706 g_set_error (error, G_FILE_ERROR,
1707 g_file_error_from_errno (errno),
1708 g_strerror (errno));
1709 return (GIOChannel *)NULL;
1712 channel = g_io_channel_win32_new_fd (fid);
1714 /* XXX: move this to g_io_channel_win32_new_fd () */
1715 channel->close_on_unref = TRUE;
1716 channel->is_seekable = TRUE;
1718 /* g_io_channel_win32_new_fd sets is_readable and is_writeable to
1719 * correspond to actual readability/writeability. Set to FALSE those
1720 * that mode doesn't allow
1725 channel->is_writeable = FALSE;
1729 channel->is_readable = FALSE;
1731 case MODE_R | MODE_PLUS:
1732 case MODE_W | MODE_PLUS:
1733 case MODE_A | MODE_PLUS:
1736 g_assert_not_reached ();
1745 #undef g_io_channel_new_file
1747 /* Binary compatibility version. Not for newly compiled code. */
1750 g_io_channel_new_file (const gchar *filename,
1754 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1757 if (utf8_filename == NULL)
1760 retval = g_io_channel_new_file_utf8 (utf8_filename, mode, error);
1762 g_free (utf8_filename);
1770 g_io_win32_unimpl_set_flags (GIOChannel *channel,
1774 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1776 if (win32_channel->debug)
1778 g_print ("g_io_win32_unimpl_set_flags: ");
1779 g_win32_print_gioflags (flags);
1783 g_set_error (err, G_IO_CHANNEL_ERROR,
1784 G_IO_CHANNEL_ERROR_FAILED,
1785 "Not implemented on Win32");
1787 return G_IO_STATUS_ERROR;
1791 g_io_win32_fd_get_flags_internal (GIOChannel *channel,
1794 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1798 if (st->st_mode & _S_IFIFO)
1800 channel->is_readable =
1801 (PeekNamedPipe ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL, NULL) != 0) || GetLastError () == ERROR_BROKEN_PIPE;
1802 channel->is_writeable =
1803 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1804 channel->is_seekable = FALSE;
1808 channel->is_readable =
1809 (ReadFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1810 channel->is_writeable =
1811 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1812 channel->is_seekable = TRUE;
1815 /* XXX: G_IO_FLAG_APPEND */
1816 /* XXX: G_IO_FLAG_NONBLOCK */
1822 g_io_win32_fd_get_flags (GIOChannel *channel)
1825 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1827 g_return_val_if_fail (win32_channel != NULL, 0);
1828 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_FILE_DESC, 0);
1830 if (0 == fstat (win32_channel->fd, &st))
1831 return g_io_win32_fd_get_flags_internal (channel, &st);
1837 g_io_win32_console_get_flags_internal (GIOChannel *channel)
1839 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1840 HANDLE handle = (HANDLE) _get_osfhandle (win32_channel->fd);
1843 INPUT_RECORD record;
1845 channel->is_readable = PeekConsoleInput (handle, &record, 1, &count);
1846 channel->is_writeable = WriteFile (handle, &c, 0, &count, NULL);
1847 channel->is_seekable = FALSE;
1853 g_io_win32_console_get_flags (GIOChannel *channel)
1855 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1857 g_return_val_if_fail (win32_channel != NULL, 0);
1858 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_CONSOLE, 0);
1860 return g_io_win32_console_get_flags_internal (channel);
1864 g_io_win32_msg_get_flags (GIOChannel *channel)
1870 g_io_win32_sock_set_flags (GIOChannel *channel,
1874 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1877 if (win32_channel->debug)
1879 g_print ("g_io_win32_sock_set_flags: ");
1880 g_win32_print_gioflags (flags);
1884 if (flags & G_IO_FLAG_NONBLOCK)
1887 if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1889 g_set_error (err, G_IO_CHANNEL_ERROR,
1890 G_IO_CHANNEL_ERROR_FAILED,
1891 winsock_error_message (WSAGetLastError ()));
1892 return G_IO_STATUS_ERROR;
1898 if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1900 g_set_error (err, G_IO_CHANNEL_ERROR,
1901 G_IO_CHANNEL_ERROR_FAILED,
1902 winsock_error_message (WSAGetLastError ()));
1903 return G_IO_STATUS_ERROR;
1907 return G_IO_STATUS_NORMAL;
1911 g_io_win32_sock_get_flags (GIOChannel *channel)
1913 /* Could we do something here? */
1917 static GIOFuncs win32_channel_msg_funcs = {
1918 g_io_win32_msg_read,
1919 g_io_win32_msg_write,
1921 g_io_win32_msg_close,
1922 g_io_win32_msg_create_watch,
1924 g_io_win32_unimpl_set_flags,
1925 g_io_win32_msg_get_flags,
1928 static GIOFuncs win32_channel_fd_funcs = {
1929 g_io_win32_fd_and_console_read,
1930 g_io_win32_fd_and_console_write,
1932 g_io_win32_fd_close,
1933 g_io_win32_fd_create_watch,
1935 g_io_win32_unimpl_set_flags,
1936 g_io_win32_fd_get_flags,
1939 static GIOFuncs win32_channel_console_funcs = {
1940 g_io_win32_fd_and_console_read,
1941 g_io_win32_fd_and_console_write,
1943 g_io_win32_console_close,
1944 g_io_win32_console_create_watch,
1946 g_io_win32_unimpl_set_flags,
1947 g_io_win32_console_get_flags,
1950 static GIOFuncs win32_channel_sock_funcs = {
1951 g_io_win32_sock_read,
1952 g_io_win32_sock_write,
1954 g_io_win32_sock_close,
1955 g_io_win32_sock_create_watch,
1957 g_io_win32_sock_set_flags,
1958 g_io_win32_sock_get_flags,
1962 g_io_channel_win32_new_messages (guint hwnd)
1964 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1965 GIOChannel *channel = (GIOChannel *)win32_channel;
1967 g_io_channel_init (channel);
1968 g_io_channel_win32_init (win32_channel);
1969 if (win32_channel->debug)
1970 g_print ("g_io_channel_win32_new_messages: channel=%p hwnd=%#x\n",
1972 channel->funcs = &win32_channel_msg_funcs;
1973 win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
1974 win32_channel->hwnd = (HWND) hwnd;
1976 /* XXX: check this. */
1977 channel->is_readable = IsWindow (win32_channel->hwnd);
1978 channel->is_writeable = IsWindow (win32_channel->hwnd);
1980 channel->is_seekable = FALSE;
1986 g_io_channel_win32_new_fd_internal (gint fd,
1989 GIOWin32Channel *win32_channel;
1990 GIOChannel *channel;
1992 win32_channel = g_new (GIOWin32Channel, 1);
1993 channel = (GIOChannel *)win32_channel;
1995 g_io_channel_init (channel);
1996 g_io_channel_win32_init (win32_channel);
1998 win32_channel->fd = fd;
2000 if (win32_channel->debug)
2001 g_print ("g_io_channel_win32_new_fd: channel=%p fd=%u\n",
2004 if (st->st_mode & _S_IFCHR) /* console */
2006 channel->funcs = &win32_channel_console_funcs;
2007 win32_channel->type = G_IO_WIN32_CONSOLE;
2008 g_io_win32_console_get_flags_internal (channel);
2012 channel->funcs = &win32_channel_fd_funcs;
2013 win32_channel->type = G_IO_WIN32_FILE_DESC;
2014 g_io_win32_fd_get_flags_internal (channel, st);
2021 g_io_channel_win32_new_fd (gint fd)
2025 if (fstat (fd, &st) == -1)
2027 g_warning (G_STRLOC ": %d isn't a C library file descriptor", fd);
2031 return g_io_channel_win32_new_fd_internal (fd, &st);
2035 g_io_channel_win32_get_fd (GIOChannel *channel)
2037 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2039 return win32_channel->fd;
2043 g_io_channel_win32_new_socket (int socket)
2045 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
2046 GIOChannel *channel = (GIOChannel *)win32_channel;
2048 g_io_channel_init (channel);
2049 g_io_channel_win32_init (win32_channel);
2050 if (win32_channel->debug)
2051 g_print ("g_io_channel_win32_new_socket: channel=%p sock=%d\n",
2053 channel->funcs = &win32_channel_sock_funcs;
2054 win32_channel->type = G_IO_WIN32_SOCKET;
2055 win32_channel->fd = socket;
2057 channel->is_readable = TRUE;
2058 channel->is_writeable = TRUE;
2059 channel->is_seekable = FALSE;
2065 g_io_channel_unix_new (gint fd)
2067 gboolean is_fd, is_socket;
2071 is_fd = (fstat (fd, &st) == 0);
2073 optlen = sizeof (optval);
2074 is_socket = (getsockopt (fd, SOL_SOCKET, SO_TYPE, (char *) &optval, &optlen) != SOCKET_ERROR);
2076 if (is_fd && is_socket)
2077 g_warning (G_STRLOC ": %d is both a file descriptor and a socket, file descriptor interpretation assumed.", fd);
2080 return g_io_channel_win32_new_fd_internal (fd, &st);
2083 return g_io_channel_win32_new_socket(fd);
2085 g_warning (G_STRLOC ": %d is neither a file descriptor or a socket", fd);
2091 g_io_channel_unix_get_fd (GIOChannel *channel)
2093 return g_io_channel_win32_get_fd (channel);
2097 g_io_channel_win32_set_debug (GIOChannel *channel,
2100 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2102 win32_channel->debug = flag;
2106 g_io_channel_win32_poll (GPollFD *fds,
2112 g_return_val_if_fail (n_fds >= 0, 0);
2114 result = (*g_main_context_get_poll_func (NULL)) (fds, n_fds, timeout);
2120 g_io_channel_win32_make_pollfd (GIOChannel *channel,
2121 GIOCondition condition,
2124 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2126 switch (win32_channel->type)
2128 case G_IO_WIN32_FILE_DESC:
2129 if (win32_channel->data_avail_event == NULL)
2130 create_events (win32_channel);
2132 fd->fd = (gint) win32_channel->data_avail_event;
2134 if (win32_channel->thread_id == 0 && (condition & G_IO_IN))
2136 if (condition & G_IO_IN)
2137 create_thread (win32_channel, condition, read_thread);
2138 else if (condition & G_IO_OUT)
2139 create_thread (win32_channel, condition, write_thread);
2143 case G_IO_WIN32_CONSOLE:
2144 fd->fd = (gint) _get_osfhandle (win32_channel->fd);
2147 case G_IO_WIN32_SOCKET:
2148 fd->fd = (int) WSACreateEvent ();
2151 case G_IO_WIN32_WINDOWS_MESSAGES:
2152 fd->fd = G_WIN32_MSG_HANDLE;
2156 g_assert_not_reached ();
2160 fd->events = condition;
2163 /* Binary compatibility */
2165 g_io_channel_win32_new_stream_socket (int socket)
2167 return g_io_channel_win32_new_socket (socket);
2170 #define __G_IO_WIN32_C__
2171 #include "galiasdef.c"