Bug 500246 - Bug fixes for giowin32
[platform/upstream/glib.git] / glib / giowin32.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 /*
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/.
30  */
31
32 /*
33  * Bugs that are related to the code in this file:
34  *
35  * Bug 137968 - Sometimes a GIOFunc on Win32 is called with zero condition
36  * http://bugzilla.gnome.org/show_bug.cgi?id=137968
37  *
38  * Bug 324234 - Using g_io_add_watch_full() to wait for connect() to return on a non-blocking socket returns prematurely
39  * http://bugzilla.gnome.org/show_bug.cgi?id=324234
40  *
41  * Bug 331214 - g_io_channel async socket io stalls
42  * http://bugzilla.gnome.org/show_bug.cgi?id=331214
43  *
44  * Bug 338943 - Multiple watches on the same socket
45  * http://bugzilla.gnome.org/show_bug.cgi?id=338943
46  *
47  * Bug 357674 - 2 serious bugs in giowin32.c making glib iochannels useless
48  * http://bugzilla.gnome.org/show_bug.cgi?id=357674
49  *
50  * Bug 425156 - GIOChannel deadlocks on a win32 socket
51  * http://bugzilla.gnome.org/show_bug.cgi?id=425156
52  *
53  * Bug 468910 - giofunc condition=0
54  * http://bugzilla.gnome.org/show_bug.cgi?id=468910
55  *
56  * Bug 500246 - Bug fixes for giowin32
57  * http://bugzilla.gnome.org/show_bug.cgi?id=500246
58  *
59  * Bug 548278 - Async GETs connections are always terminated unexpectedly on windows
60  * http://bugzilla.gnome.org/show_bug.cgi?id=548278
61  *
62  * Bug 548536 - giowin32 problem when adding and removing watches
63  * http://bugzilla.gnome.org/show_bug.cgi?id=548536
64  *
65  * When fixing bugs related to the code in this file, either the above
66  * bugs or others, make sure that the test programs attached to the
67  * above bugs continue to work.
68  */
69
70 #include "config.h"
71
72 #include "glib.h"
73
74 #include <stdlib.h>
75 #include <winsock2.h>
76 #include <windows.h>
77 #include <conio.h>
78 #include <fcntl.h>
79 #include <io.h>
80 #include <process.h>
81 #include <errno.h>
82 #include <sys/stat.h>
83
84 #include "gstdio.h"
85 #include "glibintl.h"
86
87 #include "galias.h"
88
89 typedef struct _GIOWin32Channel GIOWin32Channel;
90 typedef struct _GIOWin32Watch GIOWin32Watch;
91
92 #define BUFFER_SIZE 4096
93
94 typedef enum {
95   G_IO_WIN32_WINDOWS_MESSAGES,  /* Windows messages */
96   G_IO_WIN32_FILE_DESC,         /* Unix-like file descriptors from
97                                  * _open() or _pipe(), except for console IO.
98                                  * Have to create separate thread to read.
99                                  */
100   G_IO_WIN32_CONSOLE,           /* Console IO (usually stdin, stdout, stderr) */
101   G_IO_WIN32_SOCKET             /* Sockets. No separate thread */
102 } GIOWin32ChannelType;
103
104 struct _GIOWin32Channel {
105   GIOChannel channel;
106   gint fd;                      /* Either a Unix-like file handle as provided
107                                  * by the Microsoft C runtime, or a SOCKET
108                                  * as provided by WinSock.
109                                  */
110   GIOWin32ChannelType type;
111   
112   gboolean debug;
113
114   /* This is used by G_IO_WIN32_WINDOWS_MESSAGES channels */
115   HWND hwnd;                    /* handle of window, or NULL */
116   
117   /* Following fields are used by fd channels. */
118   CRITICAL_SECTION mutex;
119
120   int direction;                /* 0 means we read from it,
121                                  * 1 means we write to it.
122                                  */
123
124   gboolean running;             /* Is reader thread running. FALSE if
125                                  * EOF has been reached.
126                                  */
127   gboolean needs_close;         /* If the channel has been closed while
128                                  * the reader thread was still running.
129                                  */
130   guint thread_id;              /* If non-NULL has a reader thread, or has
131                                  * had.*/
132   HANDLE data_avail_event;
133
134   gushort revents;
135
136   /* Following fields used by fd channels for input */
137   
138   /* Data is kept in a circular buffer. To be able to distinguish between
139    * empty and full buffer, we cannot fill it completely, but have to
140    * leave a one character gap.
141    *
142    * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
143    *
144    * Empty:    wrp == rdp
145    * Full:     (wrp + 1) % BUFFER_SIZE == rdp
146    * Partial:  otherwise
147    */
148   guchar *buffer;               /* (Circular) buffer */
149   gint wrp, rdp;                /* Buffer indices for writing and reading */
150   HANDLE space_avail_event;
151
152   /* Following fields used by socket channels */
153   int event_mask;
154   int last_events;
155   HANDLE event;
156   gboolean write_would_have_blocked;
157   gboolean ever_writable;
158 };
159
160 #define LOCK(mutex) EnterCriticalSection (&mutex)
161 #define UNLOCK(mutex) LeaveCriticalSection (&mutex)
162
163 struct _GIOWin32Watch {
164   GSource       source;
165   GPollFD       pollfd;
166   GIOChannel   *channel;
167   GIOCondition  condition;
168 };
169
170 static void
171 g_win32_print_access_mode (int flags)
172 {
173   g_print ("%s%s%s%s%s%s%s%s%s%s",
174            ((flags & 0x3) == _O_RDWR ? "O_RDWR" :
175             ((flags & 0x3) == _O_RDONLY ? "O_RDONLY" :
176              ((flags & 0x3) == _O_WRONLY ? "O_WRONLY" : "0"))),
177            (flags & _O_APPEND ? "|O_APPEND" : ""),
178            (flags & _O_RANDOM ? "|O_RANDOM" : ""),
179            (flags & _O_SEQUENTIAL ? "|O_SEQUENTIAL" : ""),
180            (flags & _O_TEMPORARY ? "|O_TEMPORARY" : ""),
181            (flags & _O_CREAT ? "|O_CREAT" : ""),
182            (flags & _O_TRUNC ? "|O_TRUNC" : ""),
183            (flags & _O_EXCL ? "|O_EXCL" : ""),
184            (flags & _O_TEXT ? "|O_TEXT" : ""),
185            (flags & _O_BINARY ? "|O_BINARY" : ""));
186 }
187
188 static void
189 g_win32_print_gioflags (GIOFlags flags)
190 {
191   char *bar = "";
192
193   if (flags & G_IO_FLAG_APPEND)
194     bar = "|", g_print ("APPEND");
195   if (flags & G_IO_FLAG_NONBLOCK)
196     g_print ("%sNONBLOCK", bar), bar = "|";
197   if (flags & G_IO_FLAG_IS_READABLE)
198     g_print ("%sREADABLE", bar), bar = "|";
199   if (flags & G_IO_FLAG_IS_WRITEABLE)
200     g_print ("%sWRITEABLE", bar), bar = "|";
201   if (flags & G_IO_FLAG_IS_SEEKABLE)
202     g_print ("%sSEEKABLE", bar), bar = "|";
203 }
204
205 static const char *
206 event_mask_to_string (int mask)
207 {
208   char buf[100];
209   int checked_bits = 0;
210   char *bufp = buf;
211
212   if (mask == 0)
213     return "";
214
215 #define BIT(n) checked_bits |= FD_##n; if (mask & FD_##n) bufp += sprintf (bufp, "%s" #n, (bufp>buf ? "|" : ""))
216
217   BIT (READ);
218   BIT (WRITE);
219   BIT (OOB);
220   BIT (ACCEPT);
221   BIT (CONNECT);
222   BIT (CLOSE);
223   BIT (QOS);
224   BIT (GROUP_QOS);
225   BIT (ROUTING_INTERFACE_CHANGE);
226   BIT (ADDRESS_LIST_CHANGE);
227   
228 #undef BIT
229
230   if ((mask & ~checked_bits) != 0)
231           bufp += sprintf (bufp, "|%#x", mask & ~checked_bits);
232   
233   return g_quark_to_string (g_quark_from_string (buf));
234 }
235
236 static const char *
237 condition_to_string (GIOCondition condition)
238 {
239   char buf[100];
240   int checked_bits = 0;
241   char *bufp = buf;
242
243   if (condition == 0)
244     return "";
245
246 #define BIT(n) checked_bits |= G_IO_##n; if (condition & G_IO_##n) bufp += sprintf (bufp, "%s" #n, (bufp>buf ? "|" : ""))
247
248   BIT (IN);
249   BIT (OUT);
250   BIT (PRI);
251   BIT (ERR);
252   BIT (HUP);
253   BIT (NVAL);
254   
255 #undef BIT
256
257   if ((condition & ~checked_bits) != 0)
258           bufp += sprintf (bufp, "|%#x", condition & ~checked_bits);
259   
260   return g_quark_to_string (g_quark_from_string (buf));
261 }
262
263 static gboolean
264 g_io_win32_get_debug_flag (void)
265 {
266   return (getenv ("G_IO_WIN32_DEBUG") != NULL);
267 }
268
269 static void
270 g_io_channel_win32_init (GIOWin32Channel *channel)
271 {
272   channel->debug = g_io_win32_get_debug_flag ();
273   channel->buffer = NULL;
274   channel->running = FALSE;
275   channel->needs_close = FALSE;
276   channel->thread_id = 0;
277   channel->data_avail_event = NULL;
278   channel->revents = 0;
279   channel->space_avail_event = NULL;
280   channel->event_mask = 0;
281   channel->last_events = 0;
282   channel->event = NULL;
283   channel->write_would_have_blocked = FALSE;
284   channel->ever_writable = FALSE;
285   InitializeCriticalSection (&channel->mutex);
286 }
287
288 static void
289 create_events (GIOWin32Channel *channel)
290 {
291   SECURITY_ATTRIBUTES sec_attrs;
292   
293   sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
294   sec_attrs.lpSecurityDescriptor = NULL;
295   sec_attrs.bInheritHandle = FALSE;
296
297   /* The data available event is manual reset, the space available event
298    * is automatic reset.
299    */
300   if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
301       || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
302     {
303       gchar *emsg = g_win32_error_message (GetLastError ());
304       g_error ("Error creating event: %s", emsg);
305       g_free (emsg);
306     }
307 }
308
309 static unsigned __stdcall
310 read_thread (void *parameter)
311 {
312   GIOWin32Channel *channel = parameter;
313   guchar *buffer;
314   gint nbytes;
315
316   g_io_channel_ref ((GIOChannel *)channel);
317
318   if (channel->debug)
319     g_print ("read_thread %#x: start fd=%d, data_avail=%p space_avail=%p\n",
320              channel->thread_id,
321              channel->fd,
322              channel->data_avail_event,
323              channel->space_avail_event);
324
325   channel->direction = 0;
326   channel->buffer = g_malloc (BUFFER_SIZE);
327   channel->rdp = channel->wrp = 0;
328   channel->running = TRUE;
329
330   SetEvent (channel->space_avail_event);
331   
332   LOCK (channel->mutex);
333   while (channel->running)
334     {
335       if (channel->debug)
336         g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
337                  channel->thread_id, channel->rdp, channel->wrp);
338       if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
339         {
340           /* Buffer is full */
341           if (channel->debug)
342             g_print ("read_thread %#x: resetting space_avail\n",
343                      channel->thread_id);
344           ResetEvent (channel->space_avail_event);
345           if (channel->debug)
346             g_print ("read_thread %#x: waiting for space\n",
347                      channel->thread_id);
348           UNLOCK (channel->mutex);
349           WaitForSingleObject (channel->space_avail_event, INFINITE);
350           LOCK (channel->mutex);
351           if (channel->debug)
352             g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
353                      channel->thread_id, channel->rdp, channel->wrp);
354         }
355       
356       buffer = channel->buffer + channel->wrp;
357       
358       /* Always leave at least one byte unused gap to be able to
359        * distinguish between the full and empty condition...
360        */
361       nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
362                     BUFFER_SIZE - channel->wrp);
363
364       if (channel->debug)
365         g_print ("read_thread %#x: calling read() for %d bytes\n",
366                  channel->thread_id, nbytes);
367
368       UNLOCK (channel->mutex);
369
370       nbytes = read (channel->fd, buffer, nbytes);
371       
372       LOCK (channel->mutex);
373
374       channel->revents = G_IO_IN;
375       if (nbytes == 0)
376         channel->revents |= G_IO_HUP;
377       else if (nbytes < 0)
378         channel->revents |= G_IO_ERR;
379
380       if (channel->debug)
381         g_print ("read_thread %#x: read() returned %d, rdp=%d, wrp=%d\n",
382                  channel->thread_id, nbytes, channel->rdp, channel->wrp);
383
384       if (nbytes <= 0)
385         break;
386
387       channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
388       if (channel->debug)
389         g_print ("read_thread %#x: rdp=%d, wrp=%d, setting data_avail\n",
390                  channel->thread_id, channel->rdp, channel->wrp);
391       SetEvent (channel->data_avail_event);
392     }
393   
394   channel->running = FALSE;
395   if (channel->needs_close)
396     {
397       if (channel->debug)
398         g_print ("read_thread %#x: channel fd %d needs closing\n",
399                  channel->thread_id, channel->fd);
400       close (channel->fd);
401       channel->fd = -1;
402     }
403
404   if (channel->debug)
405     g_print ("read_thread %#x: EOF, rdp=%d, wrp=%d, setting data_avail\n",
406              channel->thread_id, channel->rdp, channel->wrp);
407   SetEvent (channel->data_avail_event);
408   UNLOCK (channel->mutex);
409   
410   g_io_channel_unref ((GIOChannel *)channel);
411   
412   /* No need to call _endthreadex(), the actual thread starter routine
413    * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
414    * _endthreadex() for us.
415    */
416
417   return 0;
418 }
419
420 static unsigned __stdcall
421 write_thread (void *parameter)
422 {
423   GIOWin32Channel *channel = parameter;
424   guchar *buffer;
425   gint nbytes;
426
427   g_io_channel_ref ((GIOChannel *)channel);
428
429   if (channel->debug)
430     g_print ("write_thread %#x: start fd=%d, data_avail=%p space_avail=%p\n",
431              channel->thread_id,
432              channel->fd,
433              channel->data_avail_event,
434              channel->space_avail_event);
435   
436   channel->direction = 1;
437   channel->buffer = g_malloc (BUFFER_SIZE);
438   channel->rdp = channel->wrp = 0;
439   channel->running = TRUE;
440
441   SetEvent (channel->space_avail_event);
442
443   /* We use the same event objects as for a reader thread, but with
444    * reversed meaning. So, space_avail is used if data is available
445    * for writing, and data_avail is used if space is available in the
446    * write buffer.
447    */
448
449   LOCK (channel->mutex);
450   while (channel->running || channel->rdp != channel->wrp)
451     {
452       if (channel->debug)
453         g_print ("write_thread %#x: rdp=%d, wrp=%d\n",
454                  channel->thread_id, channel->rdp, channel->wrp);
455       if (channel->wrp == channel->rdp)
456         {
457           /* Buffer is empty. */
458           if (channel->debug)
459             g_print ("write_thread %#x: resetting space_avail\n",
460                      channel->thread_id);
461           ResetEvent (channel->space_avail_event);
462           if (channel->debug)
463             g_print ("write_thread %#x: waiting for data\n",
464                      channel->thread_id);
465           channel->revents = G_IO_OUT;
466           SetEvent (channel->data_avail_event);
467           UNLOCK (channel->mutex);
468           WaitForSingleObject (channel->space_avail_event, INFINITE);
469
470           LOCK (channel->mutex);
471           if (channel->rdp == channel->wrp)
472             break;
473
474           if (channel->debug)
475             g_print ("write_thread %#x: rdp=%d, wrp=%d\n",
476                      channel->thread_id, channel->rdp, channel->wrp);
477         }
478       
479       buffer = channel->buffer + channel->rdp;
480       if (channel->rdp < channel->wrp)
481         nbytes = channel->wrp - channel->rdp;
482       else
483         nbytes = BUFFER_SIZE - channel->rdp;
484
485       if (channel->debug)
486         g_print ("write_thread %#x: calling write() for %d bytes\n",
487                  channel->thread_id, nbytes);
488
489       UNLOCK (channel->mutex);
490       nbytes = write (channel->fd, buffer, nbytes);
491       LOCK (channel->mutex);
492
493       if (channel->debug)
494         g_print ("write_thread %#x: write(%i) returned %d, rdp=%d, wrp=%d\n",
495                  channel->thread_id, channel->fd, nbytes, channel->rdp, channel->wrp);
496
497       channel->revents = 0;
498       if (nbytes > 0)
499         channel->revents |= G_IO_OUT;
500       else if (nbytes <= 0)
501         channel->revents |= G_IO_ERR;
502
503       channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
504
505       if (nbytes <= 0)
506         break;
507
508       if (channel->debug)
509         g_print ("write_thread: setting data_avail for thread %#x\n",
510                  channel->thread_id);
511       SetEvent (channel->data_avail_event);
512     }
513   
514   channel->running = FALSE;
515   if (channel->needs_close)
516     {
517       if (channel->debug)
518         g_print ("write_thread %#x: channel fd %d needs closing\n",
519                  channel->thread_id, channel->fd);
520       close (channel->fd);
521       channel->fd = -1;
522     }
523
524   UNLOCK (channel->mutex);
525   
526   g_io_channel_unref ((GIOChannel *)channel);
527   
528   return 0;
529 }
530
531 static void
532 create_thread (GIOWin32Channel     *channel,
533                GIOCondition         condition,
534                unsigned (__stdcall *thread) (void *parameter))
535 {
536   HANDLE thread_handle;
537
538   thread_handle = (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0,
539                                            &channel->thread_id);
540   if (thread_handle == 0)
541     g_warning (G_STRLOC ": Error creating reader thread: %s",
542                g_strerror (errno));
543   else if (!CloseHandle (thread_handle))
544     g_warning (G_STRLOC ": Error closing thread handle: %s\n",
545                g_win32_error_message (GetLastError ()));
546
547   WaitForSingleObject (channel->space_avail_event, INFINITE);
548 }
549
550 static GIOStatus
551 buffer_read (GIOWin32Channel *channel,
552              gchar           *dest,
553              gsize            count,
554              gsize           *bytes_read,
555              GError         **err)
556 {
557   guint nbytes;
558   guint left = count;
559   
560   LOCK (channel->mutex);
561   if (channel->debug)
562     g_print ("reading from thread %#x %" G_GSIZE_FORMAT " bytes, rdp=%d, wrp=%d\n",
563              channel->thread_id, count, channel->rdp, channel->wrp);
564   
565   if (channel->wrp == channel->rdp)
566     {
567       UNLOCK (channel->mutex);
568       if (channel->debug)
569         g_print ("waiting for data from thread %#x\n", channel->thread_id);
570       WaitForSingleObject (channel->data_avail_event, INFINITE);
571       if (channel->debug)
572         g_print ("done waiting for data from thread %#x\n", channel->thread_id);
573       LOCK (channel->mutex);
574       if (channel->wrp == channel->rdp && !channel->running)
575         {
576           if (channel->debug)
577             g_print ("wrp==rdp, !running\n");
578           UNLOCK (channel->mutex);
579           *bytes_read = 0;
580           return G_IO_STATUS_EOF;
581         }
582     }
583   
584   if (channel->rdp < channel->wrp)
585     nbytes = channel->wrp - channel->rdp;
586   else
587     nbytes = BUFFER_SIZE - channel->rdp;
588   UNLOCK (channel->mutex);
589   nbytes = MIN (left, nbytes);
590   if (channel->debug)
591     g_print ("moving %d bytes from thread %#x\n",
592              nbytes, channel->thread_id);
593   memcpy (dest, channel->buffer + channel->rdp, nbytes);
594   dest += nbytes;
595   left -= nbytes;
596   LOCK (channel->mutex);
597   channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
598   if (channel->debug)
599     g_print ("setting space_avail for thread %#x\n", channel->thread_id);
600   SetEvent (channel->space_avail_event);
601   if (channel->debug)
602     g_print ("for thread %#x: rdp=%d, wrp=%d\n",
603              channel->thread_id, channel->rdp, channel->wrp);
604   if (channel->running && channel->wrp == channel->rdp)
605     {
606       if (channel->debug)
607         g_print ("resetting data_avail of thread %#x\n",
608                  channel->thread_id);
609       ResetEvent (channel->data_avail_event);
610     };
611   UNLOCK (channel->mutex);
612   
613   /* We have no way to indicate any errors form the actual
614    * read() or recv() call in the reader thread. Should we have?
615    */
616   *bytes_read = count - left;
617   return (*bytes_read > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
618 }
619
620
621 static GIOStatus
622 buffer_write (GIOWin32Channel *channel,
623               const gchar     *dest,
624               gsize            count,
625               gsize           *bytes_written,
626               GError         **err)
627 {
628   guint nbytes;
629   guint left = count;
630   
631   LOCK (channel->mutex);
632   if (channel->debug)
633     g_print ("buffer_write: writing to thread %#x %" G_GSIZE_FORMAT " bytes, rdp=%d, wrp=%d\n",
634              channel->thread_id, count, channel->rdp, channel->wrp);
635   
636   if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
637     {
638       /* Buffer is full */
639       if (channel->debug)
640         g_print ("buffer_write: tid %#x: resetting data_avail\n",
641                  channel->thread_id);
642       ResetEvent (channel->data_avail_event);
643       if (channel->debug)
644         g_print ("buffer_write: tid %#x: waiting for space\n",
645                  channel->thread_id);
646       UNLOCK (channel->mutex);
647       WaitForSingleObject (channel->data_avail_event, INFINITE);
648       LOCK (channel->mutex);
649       if (channel->debug)
650         g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d\n",
651                  channel->thread_id, channel->rdp, channel->wrp);
652     }
653    
654   nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
655                 BUFFER_SIZE - channel->wrp);
656
657   UNLOCK (channel->mutex);
658   nbytes = MIN (left, nbytes);
659   if (channel->debug)
660     g_print ("buffer_write: tid %#x: writing %d bytes\n",
661              channel->thread_id, nbytes);
662   memcpy (channel->buffer + channel->wrp, dest, nbytes);
663   dest += nbytes;
664   left -= nbytes;
665   LOCK (channel->mutex);
666
667   channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
668   if (channel->debug)
669     g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d, setting space_avail\n",
670              channel->thread_id, channel->rdp, channel->wrp);
671   SetEvent (channel->space_avail_event);
672
673   if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
674     {
675       /* Buffer is full */
676       if (channel->debug)
677         g_print ("buffer_write: tid %#x: resetting data_avail\n",
678                  channel->thread_id);
679       ResetEvent (channel->data_avail_event);
680     }
681
682   UNLOCK (channel->mutex);
683   
684   /* We have no way to indicate any errors form the actual
685    * write() call in the writer thread. Should we have?
686    */
687   *bytes_written = count - left;
688   return (*bytes_written > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
689 }
690
691
692 static gboolean
693 g_io_win32_prepare (GSource *source,
694                     gint    *timeout)
695 {
696   GIOWin32Watch *watch = (GIOWin32Watch *)source;
697   GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
698   GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
699   int event_mask;
700   
701   *timeout = -1;
702   
703   if (channel->debug)
704     g_print ("g_io_win32_prepare: source=%p channel=%p", source, channel);
705
706   switch (channel->type)
707     {
708     case G_IO_WIN32_WINDOWS_MESSAGES:
709       if (channel->debug)
710         g_print (" MSG");
711       break;
712
713     case G_IO_WIN32_CONSOLE:
714       if (channel->debug)
715         g_print (" CON");
716       break;
717
718     case G_IO_WIN32_FILE_DESC:
719       if (channel->debug)
720         g_print (" FD thread=%#x buffer_condition:{%s}"
721                  "\n  watch->pollfd.events:{%s} watch->pollfd.revents:{%s} channel->revents:{%s}",
722                  channel->thread_id, condition_to_string (buffer_condition),
723                  condition_to_string (watch->pollfd.events),
724                  condition_to_string (watch->pollfd.revents),
725                  condition_to_string (channel->revents));
726       
727       LOCK (channel->mutex);
728       if (channel->running)
729         {
730           if (channel->direction == 0 && channel->wrp == channel->rdp)
731             {
732               if (channel->debug)
733                 g_print ("\n  setting revents=0");
734               channel->revents = 0;
735             }
736         }
737       else
738         {
739           if (channel->direction == 1
740               && (channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
741             {
742               if (channel->debug)
743                 g_print ("\n setting revents=0");
744               channel->revents = 0;
745             }
746         }         
747       UNLOCK (channel->mutex);
748       break;
749
750     case G_IO_WIN32_SOCKET:
751       if (channel->debug)
752         g_print (" SOCK");
753       event_mask = 0;
754       if (watch->condition & G_IO_IN)
755         event_mask |= (FD_READ | FD_ACCEPT);
756       if (watch->condition & G_IO_OUT)
757         event_mask |= (FD_WRITE | FD_CONNECT);
758       event_mask |= FD_CLOSE;
759
760       if (channel->event_mask != event_mask /* || channel->event != watch->pollfd.fd*/)
761         {
762           if (channel->debug)
763             g_print ("\n  WSAEventSelect(%d,%p,{%s})",
764                      channel->fd, (HANDLE) watch->pollfd.fd,
765                      event_mask_to_string (event_mask));
766           if (WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd,
767                               event_mask) == SOCKET_ERROR)
768             ;                   /* What? */
769           channel->event_mask = event_mask;
770 #if 0
771           channel->event = watch->pollfd.fd;
772 #endif
773           if (channel->debug)
774             g_print ("\n  setting last_events=0");
775           channel->last_events = 0;
776
777           if ((event_mask & FD_WRITE) &&
778               channel->ever_writable &&
779               !channel->write_would_have_blocked)
780             {
781               if (channel->debug)
782                 g_print (" WSASetEvent(%p)", (WSAEVENT) watch->pollfd.fd);
783               WSASetEvent ((WSAEVENT) watch->pollfd.fd);
784             }
785         }
786       break;
787
788     default:
789       g_assert_not_reached ();
790       abort ();
791     }
792   if (channel->debug)
793     g_print ("\n");
794
795   return ((watch->condition & buffer_condition) == watch->condition);
796 }
797
798 static gboolean
799 g_io_win32_check (GSource *source)
800 {
801   MSG msg;
802   GIOWin32Watch *watch = (GIOWin32Watch *)source;
803   GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
804   GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
805   WSANETWORKEVENTS events;
806
807   if (channel->debug)
808     g_print ("g_io_win32_check: source=%p channel=%p", source, channel);
809
810   switch (channel->type)
811     {
812     case G_IO_WIN32_WINDOWS_MESSAGES:
813       if (channel->debug)
814         g_print (" MSG\n");
815       return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
816
817     case G_IO_WIN32_FILE_DESC:
818       if (channel->debug)
819         g_print (" FD thread=%#x buffer_condition=%s\n"
820                  "  watch->pollfd.events={%s} watch->pollfd.revents={%s} channel->revents={%s}\n",
821                  channel->thread_id, condition_to_string (buffer_condition),
822                  condition_to_string (watch->pollfd.events),
823                  condition_to_string (watch->pollfd.revents),
824                  condition_to_string (channel->revents));
825       
826       watch->pollfd.revents = (watch->pollfd.events & channel->revents);
827
828       return ((watch->pollfd.revents | buffer_condition) & watch->condition);
829
830     case G_IO_WIN32_CONSOLE:
831       if (channel->debug)
832         g_print (" CON\n");
833       if (watch->channel->is_writeable)
834         return TRUE;
835       else if (watch->channel->is_readable)
836         {
837           INPUT_RECORD buffer;
838           DWORD n;
839           if (PeekConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n) &&
840               n == 1)
841             {
842               /* _kbhit() does quite complex processing to find out
843                * whether at least one of the key events pending corresponds
844                * to a "real" character that can be read.
845                */
846               if (_kbhit ())
847                 return TRUE;
848               
849               /* Discard all other kinds of events */
850               ReadConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n);
851             }
852         }
853       return FALSE;
854
855     case G_IO_WIN32_SOCKET:
856       if (channel->debug)
857         g_print (" SOCK");
858       if (channel->last_events & FD_WRITE)
859         {
860           if (channel->debug)
861             g_print (" sock=%d event=%p last_events has FD_WRITE",
862                      channel->fd, (HANDLE) watch->pollfd.fd);
863         }
864       else
865         {
866           WSAEnumNetworkEvents (channel->fd, 0, &events);
867
868           if (channel->debug)
869             g_print ("\n  revents={%s} condition={%s}"
870                      "\n  WSAEnumNetworkEvents(%d,0) sets events={%s}",
871                      condition_to_string (watch->pollfd.revents),
872                      condition_to_string (watch->condition),
873                      channel->fd, 
874                      event_mask_to_string (events.lNetworkEvents));
875           
876           if (watch->pollfd.revents != 0 &&
877               events.lNetworkEvents == 0 &&
878               !(channel->event_mask & FD_WRITE))
879             {
880               channel->event_mask = 0;
881               if (channel->debug)
882                 g_print ("\n  WSAEventSelect(%d,%p,{})",
883                          channel->fd, (HANDLE) watch->pollfd.fd);
884               WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd, 0);
885               if (channel->debug)
886                 g_print ("  ResetEvent(%p)",
887                          (HANDLE) watch->pollfd.fd);
888               ResetEvent ((HANDLE) watch->pollfd.fd);
889             }
890           else if (events.lNetworkEvents & FD_WRITE)
891             channel->ever_writable = TRUE;
892           channel->last_events = events.lNetworkEvents;
893         }
894
895       watch->pollfd.revents = 0;
896       if (channel->last_events & (FD_READ | FD_ACCEPT))
897         watch->pollfd.revents |= G_IO_IN;
898
899       if (channel->last_events & FD_WRITE)
900         watch->pollfd.revents |= G_IO_OUT;
901       else
902         {
903           /* We have called WSAEnumNetworkEvents() above but it didn't
904            * set FD_WRITE.
905            */
906           if (events.lNetworkEvents & FD_CONNECT)
907             {
908               if (events.iErrorCode[FD_CONNECT_BIT] == 0)
909                 watch->pollfd.revents |= G_IO_OUT;
910               else
911                 watch->pollfd.revents |= (G_IO_HUP | G_IO_ERR);
912             }
913           if (watch->pollfd.revents == 0 && (channel->last_events & (FD_CLOSE)))
914             watch->pollfd.revents |= G_IO_HUP;
915         }
916
917       /* Regardless of WSAEnumNetworkEvents() result, if watching for
918        * writability, and if we have ever got a FD_WRITE event, and
919        * unless last write would have blocked, set G_IO_OUT. But never
920        * set both G_IO_OUT and G_IO_HUP.
921        */
922       if (!(watch->pollfd.revents & G_IO_HUP) &&
923           channel->ever_writable &&
924           !channel->write_would_have_blocked &&
925           (channel->event_mask & FD_WRITE))
926         watch->pollfd.revents |= G_IO_OUT;
927
928       if (channel->debug)
929         g_print ("\n  revents={%s} retval={%s}\n",
930                  condition_to_string (watch->pollfd.revents),
931                  condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
932
933       return ((watch->pollfd.revents | buffer_condition) & watch->condition);
934
935     default:
936       g_assert_not_reached ();
937       abort ();
938     }
939 }
940
941 static gboolean
942 g_io_win32_dispatch (GSource     *source,
943                      GSourceFunc  callback,
944                      gpointer     user_data)
945 {
946   GIOFunc func = (GIOFunc)callback;
947   GIOWin32Watch *watch = (GIOWin32Watch *)source;
948   GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
949   GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
950   
951   if (!func)
952     {
953       g_warning (G_STRLOC ": GIOWin32Watch dispatched without callback\n"
954                  "You must call g_source_connect().");
955       return FALSE;
956     }
957   
958   if (channel->debug)
959     g_print ("g_io_win32_dispatch: pollfd.revents=%s condition=%s result=%s\n",
960              condition_to_string (watch->pollfd.revents),
961              condition_to_string (watch->condition),
962              condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
963
964   return (*func) (watch->channel,
965                   (watch->pollfd.revents | buffer_condition) & watch->condition,
966                   user_data);
967 }
968
969 static void
970 g_io_win32_finalize (GSource *source)
971 {
972   GIOWin32Watch *watch = (GIOWin32Watch *)source;
973   GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
974   
975   if (channel->debug)
976     g_print ("g_io_win32_finalize: source=%p channel=%p", source, channel);
977
978   switch (channel->type)
979     {
980     case G_IO_WIN32_WINDOWS_MESSAGES:
981       if (channel->debug)
982         g_print (" MSG");
983       break;
984
985     case G_IO_WIN32_CONSOLE:
986       if (channel->debug)
987         g_print (" CON");
988       break;
989
990     case G_IO_WIN32_FILE_DESC:
991       if (channel->debug)
992         g_print (" FD thread=%#x", channel->thread_id);
993       break;
994
995     case G_IO_WIN32_SOCKET:
996       if (channel->debug)
997         g_print (" SOCK sock=%d", channel->fd);
998 #if 0
999       CloseHandle ((HANDLE) watch->pollfd.fd);
1000       channel->event = 0;
1001       channel->event_mask = 0;
1002 #endif
1003       break;
1004
1005     default:
1006       g_assert_not_reached ();
1007       abort ();
1008     }
1009   if (channel->debug)
1010     g_print ("\n");
1011   g_io_channel_unref (watch->channel);
1012 }
1013
1014 GSourceFuncs g_io_watch_funcs = {
1015   g_io_win32_prepare,
1016   g_io_win32_check,
1017   g_io_win32_dispatch,
1018   g_io_win32_finalize
1019 };
1020
1021 static GIOStatus
1022 g_io_win32_msg_read (GIOChannel *channel,
1023                      gchar      *buf,
1024                      gsize       count,
1025                      gsize      *bytes_read,
1026                      GError    **err)
1027 {
1028   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1029   MSG msg;               /* In case of alignment problems */
1030   
1031   if (count < sizeof (MSG))
1032     {
1033       g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
1034                            "Incorrect message size"); /* Informative enough error message? */
1035       return G_IO_STATUS_ERROR;
1036     }
1037   
1038   if (win32_channel->debug)
1039     g_print ("g_io_win32_msg_read: channel=%p hwnd=%p\n",
1040              channel, win32_channel->hwnd);
1041   if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
1042     return G_IO_STATUS_AGAIN;
1043
1044   memmove (buf, &msg, sizeof (MSG));
1045   *bytes_read = sizeof (MSG);
1046
1047   return G_IO_STATUS_NORMAL;
1048 }
1049
1050 static GIOStatus
1051 g_io_win32_msg_write (GIOChannel  *channel,
1052                       const gchar *buf,
1053                       gsize        count,
1054                       gsize       *bytes_written,
1055                       GError     **err)
1056 {
1057   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1058   MSG msg;
1059   
1060   if (count != sizeof (MSG))
1061     {
1062       g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
1063                            "Incorrect message size"); /* Informative enough error message? */
1064       return G_IO_STATUS_ERROR;
1065     }
1066   
1067   /* In case of alignment problems */
1068   memmove (&msg, buf, sizeof (MSG));
1069   if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
1070     {
1071       gchar *emsg = g_win32_error_message (GetLastError ());
1072       g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, emsg);
1073       g_free (emsg);
1074       return G_IO_STATUS_ERROR;
1075     }
1076
1077   *bytes_written = sizeof (MSG);
1078
1079   return G_IO_STATUS_NORMAL;
1080 }
1081
1082 static GIOStatus
1083 g_io_win32_msg_close (GIOChannel *channel,
1084                       GError    **err)
1085 {
1086   /* Nothing to be done. Or should we set hwnd to some invalid value? */
1087
1088   return G_IO_STATUS_NORMAL;
1089 }
1090
1091 static void
1092 g_io_win32_free (GIOChannel *channel)
1093 {
1094   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1095   
1096   if (win32_channel->debug)
1097     g_print ("g_io_win32_free channel=%p fd=%d\n", channel, win32_channel->fd);
1098
1099   if (win32_channel->data_avail_event)
1100     CloseHandle (win32_channel->data_avail_event);
1101   if (win32_channel->space_avail_event)
1102     CloseHandle (win32_channel->space_avail_event);
1103   if (win32_channel->type == G_IO_WIN32_SOCKET)
1104     WSAEventSelect (win32_channel->fd, NULL, 0);
1105   DeleteCriticalSection (&win32_channel->mutex);
1106
1107   g_free (win32_channel->buffer);
1108   g_free (win32_channel);
1109 }
1110
1111 static GSource *
1112 g_io_win32_msg_create_watch (GIOChannel   *channel,
1113                              GIOCondition  condition)
1114 {
1115   GIOWin32Watch *watch;
1116   GSource *source;
1117
1118   source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1119   watch = (GIOWin32Watch *)source;
1120   
1121   watch->channel = channel;
1122   g_io_channel_ref (channel);
1123   
1124   watch->condition = condition;
1125   
1126   watch->pollfd.fd = (gintptr) G_WIN32_MSG_HANDLE;
1127   watch->pollfd.events = condition;
1128   
1129   g_source_add_poll (source, &watch->pollfd);
1130   
1131   return source;
1132 }
1133
1134 static GIOStatus
1135 g_io_win32_fd_and_console_read (GIOChannel *channel,
1136                                 gchar      *buf,
1137                                 gsize       count,
1138                                 gsize      *bytes_read,
1139                                 GError    **err)
1140 {
1141   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1142   gint result;
1143   
1144   if (win32_channel->debug)
1145     g_print ("g_io_win32_fd_read: fd=%d count=%" G_GSIZE_FORMAT "\n",
1146              win32_channel->fd, count);
1147   
1148   if (win32_channel->thread_id)
1149     {
1150       return buffer_read (win32_channel, buf, count, bytes_read, err);
1151     }
1152
1153   result = read (win32_channel->fd, buf, count);
1154
1155   if (win32_channel->debug)
1156     g_print ("g_io_win32_fd_read: read() => %d\n", result);
1157
1158   if (result < 0)
1159     {
1160       *bytes_read = 0;
1161
1162       switch (errno)
1163         {
1164 #ifdef EAGAIN
1165         case EAGAIN:
1166           return G_IO_STATUS_AGAIN;
1167 #endif
1168         default:
1169           g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1170                                g_io_channel_error_from_errno (errno),
1171                                g_strerror (errno));
1172           return G_IO_STATUS_ERROR;
1173         }
1174     }
1175
1176   *bytes_read = result;
1177
1178   return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
1179 }
1180
1181 static GIOStatus
1182 g_io_win32_fd_and_console_write (GIOChannel  *channel,
1183                                  const gchar *buf,
1184                                  gsize        count,
1185                                  gsize       *bytes_written,
1186                                  GError     **err)
1187 {
1188   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1189   gint result;
1190
1191   if (win32_channel->thread_id)
1192     {
1193       return buffer_write (win32_channel, buf, count, bytes_written, err);
1194     }
1195   
1196   result = write (win32_channel->fd, buf, count);
1197   if (win32_channel->debug)
1198     g_print ("g_io_win32_fd_write: fd=%d count=%" G_GSIZE_FORMAT " => %d\n",
1199              win32_channel->fd, count, result);
1200
1201   if (result < 0)
1202     {
1203       *bytes_written = 0;
1204
1205       switch (errno)
1206         {
1207 #ifdef EAGAIN
1208         case EAGAIN:
1209           return G_IO_STATUS_AGAIN;
1210 #endif
1211         default:
1212           g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1213                                g_io_channel_error_from_errno (errno),
1214                                g_strerror (errno));
1215           return G_IO_STATUS_ERROR;
1216         }
1217     }
1218
1219   *bytes_written = result;
1220
1221   return G_IO_STATUS_NORMAL;
1222 }
1223
1224 static GIOStatus
1225 g_io_win32_fd_seek (GIOChannel *channel,
1226                     gint64      offset,
1227                     GSeekType   type,
1228                     GError    **err)
1229 {
1230   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1231   int whence;
1232   off_t tmp_offset;
1233   off_t result;
1234   
1235   switch (type)
1236     {
1237     case G_SEEK_SET:
1238       whence = SEEK_SET;
1239       break;
1240     case G_SEEK_CUR:
1241       whence = SEEK_CUR;
1242       break;
1243     case G_SEEK_END:
1244       whence = SEEK_END;
1245       break;
1246     default:
1247       whence = -1; /* Keep the compiler quiet */
1248       g_assert_not_reached ();
1249       abort ();
1250     }
1251
1252   tmp_offset = offset;
1253   if (tmp_offset != offset)
1254     {
1255       g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1256                            g_io_channel_error_from_errno (EINVAL),
1257                            g_strerror (EINVAL));
1258       return G_IO_STATUS_ERROR;
1259     }
1260   
1261   result = lseek (win32_channel->fd, tmp_offset, whence);
1262   
1263   if (result < 0)
1264     {
1265       g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1266                            g_io_channel_error_from_errno (errno),
1267                            g_strerror (errno));
1268       return G_IO_STATUS_ERROR;
1269     }
1270
1271   return G_IO_STATUS_NORMAL;
1272 }
1273
1274 static GIOStatus
1275 g_io_win32_fd_close (GIOChannel *channel,
1276                      GError    **err)
1277 {
1278   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1279   
1280   if (win32_channel->debug)
1281     g_print ("g_io_win32_fd_close: thread=%#x: fd=%d\n",
1282              win32_channel->thread_id,
1283              win32_channel->fd);
1284   LOCK (win32_channel->mutex);
1285   if (win32_channel->running)
1286     {
1287       if (win32_channel->debug)
1288         g_print ("thread %#x: running, marking fd %d for later close\n",
1289                  win32_channel->thread_id, win32_channel->fd);
1290       win32_channel->running = FALSE;
1291       win32_channel->needs_close = TRUE;
1292       if (win32_channel->direction == 0)
1293         SetEvent (win32_channel->data_avail_event);
1294       else
1295         SetEvent (win32_channel->space_avail_event);
1296     }
1297   else
1298     {
1299       if (win32_channel->debug)
1300         g_print ("closing fd %d\n", win32_channel->fd);
1301       close (win32_channel->fd);
1302       if (win32_channel->debug)
1303         g_print ("closed fd %d, setting to -1\n",
1304                  win32_channel->fd);
1305       win32_channel->fd = -1;
1306     }
1307   UNLOCK (win32_channel->mutex);
1308
1309   /* FIXME error detection? */
1310
1311   return G_IO_STATUS_NORMAL;
1312 }
1313
1314 static GSource *
1315 g_io_win32_fd_create_watch (GIOChannel    *channel,
1316                             GIOCondition   condition)
1317 {
1318   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1319   GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1320   GIOWin32Watch *watch = (GIOWin32Watch *)source;
1321
1322   watch->channel = channel;
1323   g_io_channel_ref (channel);
1324   
1325   watch->condition = condition;
1326   
1327   if (win32_channel->data_avail_event == NULL)
1328     create_events (win32_channel);
1329
1330   watch->pollfd.fd = (gintptr) win32_channel->data_avail_event;
1331   watch->pollfd.events = condition;
1332   
1333   if (win32_channel->debug)
1334     g_print ("g_io_win32_fd_create_watch: channel=%p fd=%d condition={%s} event=%p\n",
1335              channel, win32_channel->fd,
1336              condition_to_string (condition), (HANDLE) watch->pollfd.fd);
1337
1338   LOCK (win32_channel->mutex);
1339   if (win32_channel->thread_id == 0)
1340     {
1341       if (condition & G_IO_IN)
1342         create_thread (win32_channel, condition, read_thread);
1343       else if (condition & G_IO_OUT)
1344         create_thread (win32_channel, condition, write_thread);
1345     }
1346
1347   g_source_add_poll (source, &watch->pollfd);
1348   UNLOCK (win32_channel->mutex);
1349
1350   return source;
1351 }
1352
1353 static GIOStatus
1354 g_io_win32_console_close (GIOChannel *channel,
1355                           GError    **err)
1356 {
1357   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1358   
1359   if (close (win32_channel->fd) < 0)
1360     {
1361       g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1362                            g_io_channel_error_from_errno (errno),
1363                            g_strerror (errno));
1364       return G_IO_STATUS_ERROR;
1365     }
1366
1367   return G_IO_STATUS_NORMAL;
1368 }
1369
1370 static GSource *
1371 g_io_win32_console_create_watch (GIOChannel    *channel,
1372                                  GIOCondition   condition)
1373 {
1374   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1375   GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1376   GIOWin32Watch *watch = (GIOWin32Watch *)source;
1377
1378   watch->channel = channel;
1379   g_io_channel_ref (channel);
1380   
1381   watch->condition = condition;
1382   
1383   watch->pollfd.fd = _get_osfhandle (win32_channel->fd);
1384   watch->pollfd.events = condition;
1385   
1386   g_source_add_poll (source, &watch->pollfd);
1387
1388   return source;
1389 }
1390
1391 static GIOStatus
1392 g_io_win32_sock_read (GIOChannel *channel,
1393                       gchar      *buf,
1394                       gsize       count,
1395                       gsize      *bytes_read,
1396                       GError    **err)
1397 {
1398   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1399   gint result;
1400   GIOChannelError error;
1401   int winsock_error;
1402
1403   if (win32_channel->debug)
1404     g_print ("g_io_win32_sock_read: channel=%p sock=%d count=%" G_GSIZE_FORMAT,
1405              channel, win32_channel->fd, count);
1406
1407   result = recv (win32_channel->fd, buf, count, 0);
1408   if (result == SOCKET_ERROR)
1409     winsock_error = WSAGetLastError ();
1410
1411   if (win32_channel->debug)
1412     g_print (" recv=%d", result);
1413   
1414   if (result == SOCKET_ERROR)
1415     {
1416       gchar *emsg = g_win32_error_message (winsock_error);
1417
1418       if (win32_channel->debug)
1419         g_print (" %s\n", emsg);
1420
1421       *bytes_read = 0;
1422
1423       switch (winsock_error)
1424         {
1425         case WSAEINVAL:
1426           error = G_IO_CHANNEL_ERROR_INVAL;
1427           break;
1428         case WSAEWOULDBLOCK:
1429           g_free (emsg);
1430           return G_IO_STATUS_AGAIN;
1431         default:
1432           error = G_IO_CHANNEL_ERROR_FAILED;
1433           break;
1434         }
1435       g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
1436       g_free (emsg);
1437
1438       return G_IO_STATUS_ERROR;
1439     }
1440   else
1441     {
1442       if (win32_channel->debug)
1443         g_print ("\n");
1444       *bytes_read = result;
1445       if (result == 0)
1446         return G_IO_STATUS_EOF;
1447       else
1448         return G_IO_STATUS_NORMAL;
1449     }
1450 }
1451
1452 static GIOStatus
1453 g_io_win32_sock_write (GIOChannel  *channel,
1454                        const gchar *buf,
1455                        gsize        count,
1456                        gsize       *bytes_written,
1457                        GError     **err)
1458 {
1459   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1460   gint result;
1461   GIOChannelError error;
1462   int winsock_error;
1463   
1464   if (win32_channel->debug)
1465     g_print ("g_io_win32_sock_write: channel=%p sock=%d count=%" G_GSIZE_FORMAT,
1466              channel, win32_channel->fd, count);
1467
1468   result = send (win32_channel->fd, buf, count, 0);
1469   if (result == SOCKET_ERROR)
1470     winsock_error = WSAGetLastError ();
1471
1472   if (win32_channel->debug)
1473     g_print (" send=%d", result);
1474   
1475   if (result == SOCKET_ERROR)
1476     {
1477       gchar *emsg = g_win32_error_message (winsock_error);
1478
1479       if (win32_channel->debug)
1480         g_print (" %s\n", emsg);
1481
1482       *bytes_written = 0;
1483
1484       switch (winsock_error)
1485         {
1486         case WSAEINVAL:
1487           error = G_IO_CHANNEL_ERROR_INVAL;
1488           break;
1489         case WSAEWOULDBLOCK:
1490           win32_channel->write_would_have_blocked = TRUE;
1491           win32_channel->last_events = 0;
1492           g_free (emsg);
1493           return G_IO_STATUS_AGAIN;
1494         default:
1495           error = G_IO_CHANNEL_ERROR_FAILED;
1496           break;
1497         }
1498       g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
1499       g_free (emsg);
1500
1501       return G_IO_STATUS_ERROR;
1502     }
1503   else
1504     {
1505       if (win32_channel->debug)
1506         g_print ("\n");
1507       *bytes_written = result;
1508       win32_channel->write_would_have_blocked = FALSE;
1509
1510       return G_IO_STATUS_NORMAL;
1511     }
1512 }
1513
1514 static GIOStatus
1515 g_io_win32_sock_close (GIOChannel *channel,
1516                        GError    **err)
1517 {
1518   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1519
1520   if (win32_channel->fd != -1)
1521     {
1522       if (win32_channel->debug)
1523         g_print ("g_io_win32_sock_close: channel=%p sock=%d\n",
1524                  channel, win32_channel->fd);
1525       
1526       closesocket (win32_channel->fd);
1527       win32_channel->fd = -1;
1528     }
1529
1530   /* FIXME error detection? */
1531
1532   return G_IO_STATUS_NORMAL;
1533 }
1534
1535 static GSource *
1536 g_io_win32_sock_create_watch (GIOChannel    *channel,
1537                               GIOCondition   condition)
1538 {
1539   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1540   GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1541   GIOWin32Watch *watch = (GIOWin32Watch *)source;
1542   
1543   watch->channel = channel;
1544   g_io_channel_ref (channel);
1545   
1546   watch->condition = condition;
1547
1548   if (win32_channel->event == 0)
1549     win32_channel->event = WSACreateEvent ();
1550
1551   watch->pollfd.fd = (gintptr) win32_channel->event;
1552   watch->pollfd.events = condition;
1553   
1554   if (win32_channel->debug)
1555     g_print ("g_io_win32_sock_create_watch: channel=%p sock=%d event=%p condition={%s}\n",
1556              channel, win32_channel->fd, (HANDLE) watch->pollfd.fd,
1557              condition_to_string (watch->condition));
1558
1559   g_source_add_poll (source, &watch->pollfd);
1560
1561   return source;
1562 }
1563
1564 GIOChannel *
1565 g_io_channel_new_file (const gchar  *filename,
1566                        const gchar  *mode,
1567                        GError      **error)
1568 {
1569   int fid, flags, pmode;
1570   GIOChannel *channel;
1571
1572   enum { /* Cheesy hack */
1573     MODE_R = 1 << 0,
1574     MODE_W = 1 << 1,
1575     MODE_A = 1 << 2,
1576     MODE_PLUS = 1 << 3,
1577   } mode_num;
1578
1579   g_return_val_if_fail (filename != NULL, NULL);
1580   g_return_val_if_fail (mode != NULL, NULL);
1581   g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
1582
1583   switch (mode[0])
1584     {
1585       case 'r':
1586         mode_num = MODE_R;
1587         break;
1588       case 'w':
1589         mode_num = MODE_W;
1590         break;
1591       case 'a':
1592         mode_num = MODE_A;
1593         break;
1594       default:
1595         g_warning ("Invalid GIOFileMode %s.\n", mode);
1596         return NULL;
1597     }
1598
1599   switch (mode[1])
1600     {
1601       case '\0':
1602         break;
1603       case '+':
1604         if (mode[2] == '\0')
1605           {
1606             mode_num |= MODE_PLUS;
1607             break;
1608           }
1609         /* Fall through */
1610       default:
1611         g_warning ("Invalid GIOFileMode %s.\n", mode);
1612         return NULL;
1613     }
1614
1615   switch (mode_num)
1616     {
1617       case MODE_R:
1618         flags = O_RDONLY;
1619         pmode = _S_IREAD;
1620         break;
1621       case MODE_W:
1622         flags = O_WRONLY | O_TRUNC | O_CREAT;
1623         pmode = _S_IWRITE;
1624         break;
1625       case MODE_A:
1626         flags = O_WRONLY | O_APPEND | O_CREAT;
1627         pmode = _S_IWRITE;
1628         break;
1629       case MODE_R | MODE_PLUS:
1630         flags = O_RDWR;
1631         pmode = _S_IREAD | _S_IWRITE;
1632         break;
1633       case MODE_W | MODE_PLUS:
1634         flags = O_RDWR | O_TRUNC | O_CREAT;
1635         pmode = _S_IREAD | _S_IWRITE;
1636         break;
1637       case MODE_A | MODE_PLUS:
1638         flags = O_RDWR | O_APPEND | O_CREAT;
1639         pmode = _S_IREAD | _S_IWRITE;
1640         break;
1641       default:
1642         g_assert_not_reached ();
1643         abort ();
1644     }
1645
1646   /* always open 'untranslated' */
1647   fid = g_open (filename, flags | _O_BINARY, pmode);
1648
1649   if (g_io_win32_get_debug_flag ())
1650     {
1651       g_print ("g_io_channel_win32_new_file: open(\"%s\",", filename);
1652       g_win32_print_access_mode (flags|_O_BINARY);
1653       g_print (",%#o)=%d\n", pmode, fid);
1654     }
1655
1656   if (fid < 0)
1657     {
1658       g_set_error_literal (error, G_FILE_ERROR,
1659                            g_file_error_from_errno (errno),
1660                            g_strerror (errno));
1661       return (GIOChannel *)NULL;
1662     }
1663
1664   channel = g_io_channel_win32_new_fd (fid);
1665
1666   /* XXX: move this to g_io_channel_win32_new_fd () */
1667   channel->close_on_unref = TRUE;
1668   channel->is_seekable = TRUE;
1669
1670   /* g_io_channel_win32_new_fd sets is_readable and is_writeable to
1671    * correspond to actual readability/writeability. Set to FALSE those
1672    * that mode doesn't allow
1673    */
1674   switch (mode_num)
1675     {
1676       case MODE_R:
1677         channel->is_writeable = FALSE;
1678         break;
1679       case MODE_W:
1680       case MODE_A:
1681         channel->is_readable = FALSE;
1682         break;
1683       case MODE_R | MODE_PLUS:
1684       case MODE_W | MODE_PLUS:
1685       case MODE_A | MODE_PLUS:
1686         break;
1687       default:
1688         g_assert_not_reached ();
1689         abort ();
1690     }
1691
1692   return channel;
1693 }
1694
1695 #if !defined (_WIN64)
1696
1697 #undef g_io_channel_new_file
1698
1699 /* Binary compatibility version. Not for newly compiled code. */
1700
1701 GIOChannel *
1702 g_io_channel_new_file (const gchar  *filename,
1703                        const gchar  *mode,
1704                        GError      **error)
1705 {
1706   gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1707   GIOChannel *retval;
1708
1709   if (utf8_filename == NULL)
1710     return NULL;
1711
1712   retval = g_io_channel_new_file_utf8 (utf8_filename, mode, error);
1713
1714   g_free (utf8_filename);
1715
1716   return retval;
1717 }
1718
1719 #endif
1720
1721 static GIOStatus
1722 g_io_win32_unimpl_set_flags (GIOChannel *channel,
1723                              GIOFlags    flags,
1724                              GError    **err)
1725 {
1726   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1727
1728   if (win32_channel->debug)
1729     {
1730       g_print ("g_io_win32_unimpl_set_flags: ");
1731       g_win32_print_gioflags (flags);
1732       g_print ("\n");
1733     }
1734
1735   g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1736                        G_IO_CHANNEL_ERROR_FAILED,
1737                        "Not implemented on Win32");
1738
1739   return G_IO_STATUS_ERROR;
1740 }
1741
1742 static GIOFlags
1743 g_io_win32_fd_get_flags_internal (GIOChannel  *channel,
1744                                   struct stat *st)
1745 {
1746   GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1747   gchar c;
1748   DWORD count;
1749
1750   if (st->st_mode & _S_IFIFO)
1751     {
1752       channel->is_readable =
1753         (PeekNamedPipe ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL, NULL) != 0) || GetLastError () == ERROR_BROKEN_PIPE;
1754       channel->is_writeable =
1755         (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1756       channel->is_seekable  = FALSE;
1757     }
1758   else
1759     {
1760       channel->is_readable =
1761         (ReadFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1762       channel->is_writeable =
1763         (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1764       channel->is_seekable = TRUE;
1765     }
1766
1767   /* XXX: G_IO_FLAG_APPEND */
1768   /* XXX: G_IO_FLAG_NONBLOCK */
1769
1770   return 0;
1771 }
1772
1773 static GIOFlags
1774 g_io_win32_fd_get_flags (GIOChannel *channel)
1775 {
1776   struct stat st;
1777   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1778
1779   g_return_val_if_fail (win32_channel != NULL, 0);
1780   g_return_val_if_fail (win32_channel->type == G_IO_WIN32_FILE_DESC, 0);
1781
1782   if (0 == fstat (win32_channel->fd, &st))
1783     return g_io_win32_fd_get_flags_internal (channel, &st);
1784   else
1785     return 0;
1786 }
1787
1788 static GIOFlags
1789 g_io_win32_console_get_flags_internal (GIOChannel  *channel)
1790 {
1791   GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1792   HANDLE handle = (HANDLE) _get_osfhandle (win32_channel->fd);
1793   gchar c;
1794   DWORD count;
1795   INPUT_RECORD record;
1796
1797   channel->is_readable = PeekConsoleInput (handle, &record, 1, &count);
1798   channel->is_writeable = WriteFile (handle, &c, 0, &count, NULL);
1799   channel->is_seekable = FALSE;
1800
1801   return 0;
1802 }
1803
1804 static GIOFlags
1805 g_io_win32_console_get_flags (GIOChannel *channel)
1806 {
1807   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1808
1809   g_return_val_if_fail (win32_channel != NULL, 0);
1810   g_return_val_if_fail (win32_channel->type == G_IO_WIN32_CONSOLE, 0);
1811
1812   return g_io_win32_console_get_flags_internal (channel);
1813 }
1814
1815 static GIOFlags
1816 g_io_win32_msg_get_flags (GIOChannel *channel)
1817 {
1818   return 0;
1819 }
1820
1821 static GIOStatus
1822 g_io_win32_sock_set_flags (GIOChannel *channel,
1823                            GIOFlags    flags,
1824                            GError    **err)
1825 {
1826   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1827   u_long arg;
1828
1829   if (win32_channel->debug)
1830     {
1831       g_print ("g_io_win32_sock_set_flags: ");
1832       g_win32_print_gioflags (flags);
1833       g_print ("\n");
1834     }
1835
1836   if (flags & G_IO_FLAG_NONBLOCK)
1837     {
1838       arg = 1;
1839       if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1840         {
1841           gchar *emsg = g_win32_error_message (WSAGetLastError ());
1842
1843           g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1844                                G_IO_CHANNEL_ERROR_FAILED,
1845                                emsg);
1846           g_free (emsg);
1847
1848           return G_IO_STATUS_ERROR;
1849         }
1850     }
1851   else
1852     {
1853       arg = 0;
1854       if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1855         {
1856           gchar *emsg = g_win32_error_message (WSAGetLastError ());
1857
1858           g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1859                                G_IO_CHANNEL_ERROR_FAILED,
1860                                emsg);
1861           g_free (emsg);
1862
1863           return G_IO_STATUS_ERROR;
1864         }
1865     }
1866
1867   return G_IO_STATUS_NORMAL;
1868 }
1869
1870 static GIOFlags
1871 g_io_win32_sock_get_flags (GIOChannel *channel)
1872 {
1873   /* Could we do something here? */
1874   return 0;
1875 }
1876
1877 static GIOFuncs win32_channel_msg_funcs = {
1878   g_io_win32_msg_read,
1879   g_io_win32_msg_write,
1880   NULL,
1881   g_io_win32_msg_close,
1882   g_io_win32_msg_create_watch,
1883   g_io_win32_free,
1884   g_io_win32_unimpl_set_flags,
1885   g_io_win32_msg_get_flags,
1886 };
1887
1888 static GIOFuncs win32_channel_fd_funcs = {
1889   g_io_win32_fd_and_console_read,
1890   g_io_win32_fd_and_console_write,
1891   g_io_win32_fd_seek,
1892   g_io_win32_fd_close,
1893   g_io_win32_fd_create_watch,
1894   g_io_win32_free,
1895   g_io_win32_unimpl_set_flags,
1896   g_io_win32_fd_get_flags,
1897 };
1898
1899 static GIOFuncs win32_channel_console_funcs = {
1900   g_io_win32_fd_and_console_read,
1901   g_io_win32_fd_and_console_write,
1902   NULL,
1903   g_io_win32_console_close,
1904   g_io_win32_console_create_watch,
1905   g_io_win32_free,
1906   g_io_win32_unimpl_set_flags,
1907   g_io_win32_console_get_flags,
1908 };
1909
1910 static GIOFuncs win32_channel_sock_funcs = {
1911   g_io_win32_sock_read,
1912   g_io_win32_sock_write,
1913   NULL,
1914   g_io_win32_sock_close,
1915   g_io_win32_sock_create_watch,
1916   g_io_win32_free,
1917   g_io_win32_sock_set_flags,
1918   g_io_win32_sock_get_flags,
1919 };
1920
1921 GIOChannel *
1922 #if GLIB_SIZEOF_VOID_P == 8
1923 g_io_channel_win32_new_messages (gsize hwnd)
1924 #else
1925 g_io_channel_win32_new_messages (guint hwnd)
1926 #endif
1927 {
1928   GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1929   GIOChannel *channel = (GIOChannel *)win32_channel;
1930
1931   g_io_channel_init (channel);
1932   g_io_channel_win32_init (win32_channel);
1933   if (win32_channel->debug)
1934     g_print ("g_io_channel_win32_new_messages: channel=%p hwnd=%p\n",
1935              channel, (HWND) hwnd);
1936   channel->funcs = &win32_channel_msg_funcs;
1937   win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
1938   win32_channel->hwnd = (HWND) hwnd;
1939
1940   /* XXX: check this. */
1941   channel->is_readable = IsWindow (win32_channel->hwnd);
1942   channel->is_writeable = IsWindow (win32_channel->hwnd);
1943
1944   channel->is_seekable = FALSE;
1945
1946   return channel;
1947 }
1948
1949 static GIOChannel *
1950 g_io_channel_win32_new_fd_internal (gint         fd,
1951                                     struct stat *st)
1952 {
1953   GIOWin32Channel *win32_channel;
1954   GIOChannel *channel;
1955
1956   win32_channel = g_new (GIOWin32Channel, 1);
1957   channel = (GIOChannel *)win32_channel;
1958
1959   g_io_channel_init (channel);
1960   g_io_channel_win32_init (win32_channel);
1961
1962   win32_channel->fd = fd;
1963
1964   if (win32_channel->debug)
1965     g_print ("g_io_channel_win32_new_fd: channel=%p fd=%u\n",
1966              channel, fd);
1967
1968   if (st->st_mode & _S_IFCHR) /* console */
1969     {
1970       channel->funcs = &win32_channel_console_funcs;
1971       win32_channel->type = G_IO_WIN32_CONSOLE;
1972       g_io_win32_console_get_flags_internal (channel);
1973     }
1974   else
1975     {
1976       channel->funcs = &win32_channel_fd_funcs;
1977       win32_channel->type = G_IO_WIN32_FILE_DESC;
1978       g_io_win32_fd_get_flags_internal (channel, st);
1979     }
1980   
1981   return channel;
1982 }
1983
1984 GIOChannel *
1985 g_io_channel_win32_new_fd (gint fd)
1986 {
1987   struct stat st;
1988
1989   if (fstat (fd, &st) == -1)
1990     {
1991       g_warning (G_STRLOC ": %d isn't a C library file descriptor", fd);
1992       return NULL;
1993     }
1994
1995   return g_io_channel_win32_new_fd_internal (fd, &st);
1996 }
1997
1998 gint
1999 g_io_channel_win32_get_fd (GIOChannel *channel)
2000 {
2001   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2002
2003   return win32_channel->fd;
2004 }
2005
2006 GIOChannel *
2007 g_io_channel_win32_new_socket (int socket)
2008 {
2009   GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
2010   GIOChannel *channel = (GIOChannel *)win32_channel;
2011
2012   g_io_channel_init (channel);
2013   g_io_channel_win32_init (win32_channel);
2014   if (win32_channel->debug)
2015     g_print ("g_io_channel_win32_new_socket: channel=%p sock=%d\n",
2016              channel, socket);
2017   channel->funcs = &win32_channel_sock_funcs;
2018   win32_channel->type = G_IO_WIN32_SOCKET;
2019   win32_channel->fd = socket;
2020
2021   channel->is_readable = TRUE;
2022   channel->is_writeable = TRUE;
2023   channel->is_seekable = FALSE;
2024
2025   return channel;
2026 }
2027
2028 GIOChannel *
2029 g_io_channel_unix_new (gint fd)
2030 {
2031   gboolean is_fd, is_socket;
2032   struct stat st;
2033   int optval, optlen;
2034
2035   is_fd = (fstat (fd, &st) == 0);
2036
2037   optlen = sizeof (optval);
2038   is_socket = (getsockopt (fd, SOL_SOCKET, SO_TYPE, (char *) &optval, &optlen) != SOCKET_ERROR);
2039
2040   if (is_fd && is_socket)
2041     g_warning (G_STRLOC ": %d is both a file descriptor and a socket, file descriptor interpretation assumed.", fd);
2042
2043   if (is_fd)
2044     return g_io_channel_win32_new_fd_internal (fd, &st);
2045
2046   if (is_socket)
2047     return g_io_channel_win32_new_socket(fd);
2048
2049   g_warning (G_STRLOC ": %d is neither a file descriptor or a socket", fd);
2050
2051   return NULL;
2052 }
2053
2054 gint
2055 g_io_channel_unix_get_fd (GIOChannel *channel)
2056 {
2057   return g_io_channel_win32_get_fd (channel);
2058 }
2059
2060 void
2061 g_io_channel_win32_set_debug (GIOChannel *channel,
2062                               gboolean    flag)
2063 {
2064   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2065
2066   win32_channel->debug = flag;
2067 }
2068
2069 gint
2070 g_io_channel_win32_poll (GPollFD *fds,
2071                          gint     n_fds,
2072                          gint     timeout)
2073 {
2074   int result;
2075
2076   g_return_val_if_fail (n_fds >= 0, 0);
2077
2078   result = (*g_main_context_get_poll_func (NULL)) (fds, n_fds, timeout);
2079
2080   return result;
2081 }
2082
2083 void
2084 g_io_channel_win32_make_pollfd (GIOChannel   *channel,
2085                                 GIOCondition  condition,
2086                                 GPollFD      *fd)
2087 {
2088   GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2089
2090   switch (win32_channel->type)
2091     {
2092     case G_IO_WIN32_FILE_DESC:
2093       if (win32_channel->data_avail_event == NULL)
2094         create_events (win32_channel);
2095
2096       fd->fd = (gintptr) win32_channel->data_avail_event;
2097
2098       if (win32_channel->thread_id == 0)
2099         {
2100           /* Is it meaningful for a file descriptor to be polled for
2101            * both IN and OUT? For what kind of file descriptor would
2102            * that be? Doesn't seem to make sense, in practise the file
2103            * descriptors handled here are always read or write ends of
2104            * pipes surely, and thus unidirectional.
2105            */
2106           if (condition & G_IO_IN)
2107             create_thread (win32_channel, condition, read_thread);
2108           else if (condition & G_IO_OUT)
2109             create_thread (win32_channel, condition, write_thread);
2110         }
2111       break;
2112
2113     case G_IO_WIN32_CONSOLE:
2114       fd->fd = _get_osfhandle (win32_channel->fd);
2115       break;
2116
2117     case G_IO_WIN32_SOCKET:
2118       fd->fd = (gintptr) WSACreateEvent ();
2119       break;
2120       
2121     case G_IO_WIN32_WINDOWS_MESSAGES:
2122       fd->fd = G_WIN32_MSG_HANDLE;
2123       break;
2124
2125     default:
2126       g_assert_not_reached ();
2127       abort ();
2128     }
2129   
2130   fd->events = condition;
2131 }
2132
2133 /* Binary compatibility */
2134 GIOChannel *
2135 g_io_channel_win32_new_stream_socket (int socket)
2136 {
2137   return g_io_channel_win32_new_socket (socket);
2138 }
2139
2140 #define __G_IO_WIN32_C__
2141 #include "galiasdef.c"