Some more debugging output. (g_io_channel_win32_poll): Remove unused vars.
[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  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /*
25  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
26  * file for a list of people on the GLib Team.  See the ChangeLog
27  * files for a list of changes.  These files are distributed with
28  * GLib at ftp://ftp.gtk.org/pub/gtk/.
29  */
30
31 /* Define this to get (very) verbose logging of all channels */
32 /* #define G_IO_WIN32_DEBUG */
33
34 #include "glib.h"
35
36 #include <stdlib.h>
37 #include <windows.h>
38 #include <winsock.h>          /* Not everybody has winsock2 */
39 #include <fcntl.h>
40 #include <io.h>
41 #include <process.h>
42 #include <errno.h>
43 #include <sys/stat.h>
44
45 typedef struct _GIOWin32Channel GIOWin32Channel;
46 typedef struct _GIOWin32Watch GIOWin32Watch;
47
48 #define BUFFER_SIZE 4096
49
50 typedef enum {
51   G_IO_WINDOWS_MESSAGES,        /* Windows messages */
52   G_IO_FILE_DESC,               /* Unix-like file descriptors from
53                                  * _open() or _pipe(). Read with read().
54                                  * Have to create separate thread to read.
55                                  */
56   G_IO_STREAM_SOCKET            /* Stream sockets. Similar as fds, but
57                                  * read with recv().
58                                  */
59 } GIOWin32ChannelType;
60
61 struct _GIOWin32Channel {
62   GIOChannel channel;
63   gint fd;                      /* Either a Unix-like file handle as provided
64                                  * by the Microsoft C runtime, or a SOCKET
65                                  * as provided by WinSock.
66                                  */
67   GIOWin32ChannelType type;
68   
69   gboolean debug;
70
71   /* This is used by G_IO_WINDOWS_MESSAGES channels */
72   HWND hwnd;                    /* handle of window, or NULL */
73   
74   /* Following fields used by fd and socket channels for input */
75   
76   /* Data is kept in a circular buffer. To be able to distinguish between
77    * empty and full buffer, we cannot fill it completely, but have to
78    * leave a one character gap.
79    *
80    * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
81    *
82    * Empty:    wrp == rdp
83    * Full:     (wrp + 1) % BUFFER_SIZE == rdp
84    * Partial:  otherwise
85    */
86   guchar *buffer;               /* (Circular) buffer */
87   gint wrp, rdp;                /* Buffer indices for writing and reading */
88   gboolean running;             /* Is reader thread running. FALSE if
89                                  * EOF has been reached.
90                                  */
91   guint thread_id;              /* If non-NULL has a reader thread, or has
92                                  * had.*/
93   HANDLE data_avail_event;
94   HANDLE space_avail_event;
95   CRITICAL_SECTION mutex;
96   
97   /* Function that actually reads from fd */
98   int (*reader) (int fd, guchar *buf, int len);
99 };
100
101 #define LOCK(mutex) EnterCriticalSection (&mutex)
102 #define UNLOCK(mutex) LeaveCriticalSection (&mutex)
103
104 struct _GIOWin32Watch {
105   GPollFD       pollfd;
106   GIOChannel   *channel;
107   GIOCondition  condition;
108   GIOFunc       callback;
109 };
110
111 static void
112 g_io_channel_win32_init (GIOWin32Channel *channel)
113 {
114 #ifdef G_IO_WIN32_DEBUG
115   channel->debug = TRUE;
116 #else
117   if (getenv ("G_IO_WIN32_DEBUG") != NULL)
118     channel->debug = TRUE;
119   else
120     channel->debug = FALSE;
121 #endif
122   channel->buffer = NULL;
123   channel->running = FALSE;
124   channel->thread_id = 0;
125   channel->data_avail_event = NULL;
126   channel->space_avail_event = NULL;
127 }
128
129 static void
130 create_events (GIOWin32Channel *channel)
131 {
132   SECURITY_ATTRIBUTES sec_attrs;
133   
134   sec_attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
135   sec_attrs.lpSecurityDescriptor = NULL;
136   sec_attrs.bInheritHandle = FALSE;
137
138   /* The data available event is manual reset, the space available event
139    * is automatic reset.
140    */
141   if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
142       || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
143     {
144       gchar *msg = g_win32_error_message (GetLastError ());
145       g_error ("Error creating event: %s", msg);
146     }
147   InitializeCriticalSection (&channel->mutex);
148 }
149
150 static unsigned __stdcall
151 reader_thread (void *parameter)
152 {
153   GIOWin32Channel *channel = parameter;
154   guchar *buffer;
155   guint nbytes;
156
157   g_io_channel_ref ((GIOChannel *) channel);
158
159   if (channel->debug)
160     g_print ("thread %#x: starting. pid:%#x, fd:%d, data_avail:%#x, space_avail:%#x\n",
161              channel->thread_id,
162              (guint) GetCurrentProcessId (),
163              channel->fd,
164              (guint) channel->data_avail_event,
165              (guint) channel->space_avail_event);
166   
167   channel->buffer = g_malloc (BUFFER_SIZE);
168   channel->rdp = channel->wrp = 0;
169   channel->running = TRUE;
170
171   SetEvent (channel->space_avail_event);
172   
173   while (channel->running)
174     {
175       LOCK (channel->mutex);
176       if (channel->debug)
177         g_print ("thread %#x: rdp=%d, wrp=%d\n",
178                  channel->thread_id, channel->rdp, channel->wrp);
179       if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
180         {
181           /* Buffer is full */
182           if (channel->debug)
183             g_print ("thread %#x: resetting space_available\n",
184                      channel->thread_id);
185           ResetEvent (channel->space_avail_event);
186           if (channel->debug)
187             g_print ("thread %#x: waiting for space\n", channel->thread_id);
188           UNLOCK (channel->mutex);
189           WaitForSingleObject (channel->space_avail_event, INFINITE);
190           LOCK (channel->mutex);
191           if (channel->debug)
192             g_print ("thread %#x: rdp=%d, wrp=%d\n",
193                      channel->thread_id, channel->rdp, channel->wrp);
194         }
195       
196       buffer = channel->buffer + channel->wrp;
197       
198       /* Always leave at least one byte unused gap to be able to
199        * distinguish between the full and empty condition...
200        */
201       nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
202                     BUFFER_SIZE - channel->wrp);
203
204       if (channel->debug)
205         g_print ("thread %#x: calling reader for %d bytes\n",
206                  channel->thread_id, nbytes);
207
208       UNLOCK (channel->mutex);
209
210       nbytes = (*channel->reader) (channel->fd, buffer, nbytes);
211       
212       if (nbytes <= 0)
213         break;
214
215       LOCK (channel->mutex);
216       if (channel->debug)
217         g_print ("thread %#x: got %d bytes, rdp=%d, wrp=%d\n",
218                  channel->thread_id, nbytes, channel->rdp, channel->wrp);
219       channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
220       if (channel->debug)
221         g_print ("thread %#x: rdp=%d, wrp=%d, setting data available\n",
222                  channel->thread_id, channel->rdp, channel->wrp);
223       SetEvent (channel->data_avail_event);
224       UNLOCK (channel->mutex);
225     }
226   
227   LOCK (channel->mutex);
228   channel->running = FALSE;
229   if (channel->debug)
230     g_print ("thread %#x: got EOF, rdp=%d, wrp=%d, setting data available\n",
231              channel->thread_id, channel->rdp, channel->wrp);
232   SetEvent (channel->data_avail_event);
233   UNLOCK (channel->mutex);
234   
235   g_io_channel_unref((GIOChannel *) channel);
236   
237   /* All of the Microsoft docs say we should explicitly
238    * end the thread...
239    */
240   _endthreadex(1);
241   
242   return 0;
243 }
244
245 static void
246 create_reader_thread (GIOWin32Channel *channel,
247                       gpointer         reader)
248 {
249   channel->reader = reader;
250
251   if (_beginthreadex (NULL, 0, reader_thread, channel, 0,
252                       &channel->thread_id) == 0)
253     g_warning ("Error creating reader thread: %s", strerror (errno));
254   WaitForSingleObject (channel->space_avail_event, INFINITE);
255 }
256
257 static int
258 buffer_read (GIOWin32Channel *channel,
259              guchar          *dest,
260              guint            count,
261              GIOError        *error)
262 {
263   guint nbytes;
264   guint left = count;
265   
266   LOCK (channel->mutex);
267   if (channel->debug)
268     g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
269              channel->thread_id, count, channel->rdp, channel->wrp);
270   
271   if (channel->rdp == channel->wrp)
272     {
273       UNLOCK (channel->mutex);
274       if (channel->debug)
275         g_print ("waiting for data from thread %#x\n", channel->thread_id);
276       WaitForSingleObject (channel->data_avail_event, INFINITE);
277       LOCK (channel->mutex);
278       if (channel->rdp == channel->wrp && !channel->running)
279         {
280           UNLOCK (channel->mutex);
281           return 0;
282         }
283     }
284   
285   if (channel->rdp < channel->wrp)
286     nbytes = channel->wrp - channel->rdp;
287   else
288     nbytes = BUFFER_SIZE - channel->rdp;
289   UNLOCK (channel->mutex);
290   nbytes = MIN (left, nbytes);
291   if (channel->debug)
292     g_print ("moving %d bytes from thread %#x\n",
293              nbytes, channel->thread_id);
294   memcpy (dest, channel->buffer + channel->rdp, nbytes);
295   dest += nbytes;
296   left -= nbytes;
297   LOCK (channel->mutex);
298   channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
299   if (channel->debug)
300     g_print ("setting space available for thread %#x\n", channel->thread_id);
301   SetEvent (channel->space_avail_event);
302   if (channel->debug)
303     g_print ("for thread %#x: rdp=%d, wrp=%d\n",
304              channel->thread_id, channel->rdp, channel->wrp);
305   if (channel->running && channel->rdp == channel->wrp)
306     {
307       if (channel->debug)
308         g_print ("resetting data_available of thread %#x\n",
309                  channel->thread_id);
310       ResetEvent (channel->data_avail_event);
311     };
312   UNLOCK (channel->mutex);
313   
314   /* We have no way to indicate any errors form the actual
315    * read() or recv() call in the reader thread. Should we have?
316    */
317   *error = G_IO_ERROR_NONE;
318   return count - left;
319 }
320
321 static gboolean
322 g_io_win32_prepare (gpointer  source_data,
323                     GTimeVal *current_time,
324                     gint     *timeout,
325                     gpointer  user_data)
326 {
327   *timeout = -1;
328   
329   return FALSE;
330 }
331
332 static gboolean
333 g_io_win32_check (gpointer  source_data,
334                   GTimeVal *current_time,
335                   gpointer  user_data)
336 {
337   GIOWin32Watch *data = source_data;
338   GIOWin32Channel *channel = (GIOWin32Channel *) data->channel;
339   
340   /* If the thread has died, we have encountered EOF. If the buffer
341    * also is emtpty set the HUP bit.
342    */
343   if (!channel->running && channel->rdp == channel->wrp)
344     {
345       if (channel->debug)
346         g_print ("g_io_win32_check: setting G_IO_HUP thread %#x rdp=%d wrp=%d\n",
347                  channel->thread_id, channel->rdp, channel->wrp);
348       data->pollfd.revents |= G_IO_HUP;
349       return TRUE;
350     }
351   
352   return (data->pollfd.revents & data->condition);
353 }
354
355 static gboolean
356 g_io_win32_dispatch (gpointer  source_data,
357                      GTimeVal *current_time,
358                      gpointer  user_data)
359      
360 {
361   GIOWin32Watch *data = source_data;
362   
363   return (*data->callback) (data->channel,
364                             data->pollfd.revents & data->condition,
365                             user_data);
366 }
367
368 static void
369 g_io_win32_destroy (gpointer source_data)
370 {
371   GIOWin32Watch *data = source_data;
372   
373   g_main_remove_poll (&data->pollfd);
374   g_io_channel_unref (data->channel);
375   g_free (data);
376 }
377
378 static GSourceFuncs win32_watch_funcs = {
379   g_io_win32_prepare,
380   g_io_win32_check,
381   g_io_win32_dispatch,
382   g_io_win32_destroy
383 };
384
385 static guint
386 g_io_win32_add_watch (GIOChannel    *channel,
387                       gint           priority,
388                       GIOCondition   condition,
389                       GIOFunc        func,
390                       gpointer       user_data,
391                       GDestroyNotify notify,
392                       int (*reader) (int, guchar *, int))
393 {
394   GIOWin32Watch *watch = g_new (GIOWin32Watch, 1);
395   GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
396   
397   watch->channel = channel;
398   g_io_channel_ref (channel);
399   
400   watch->callback = func;
401   watch->condition = condition;
402   
403   if (win32_channel->data_avail_event == NULL)
404     create_events (win32_channel);
405
406   watch->pollfd.fd = (gint) win32_channel->data_avail_event;
407   watch->pollfd.events = condition;
408   
409   if (win32_channel->debug)
410     g_print ("g_io_win32_add_watch: fd:%d handle:%#x\n",
411              win32_channel->fd, watch->pollfd.fd);
412   
413   if (win32_channel->thread_id == 0)
414     create_reader_thread (win32_channel, reader);
415
416   g_main_add_poll (&watch->pollfd, priority);
417   
418   return g_source_add (priority, TRUE, &win32_watch_funcs, watch,
419                        user_data, notify);
420 }
421
422 static GIOError
423 g_io_win32_msg_read (GIOChannel *channel,
424                      gchar      *buf,
425                      guint       count,
426                      guint      *bytes_read)
427 {
428   GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
429   MSG msg;               /* In case of alignment problems */
430   
431   if (count < sizeof (MSG))
432     return G_IO_ERROR_INVAL;
433   
434   if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
435     return G_IO_ERROR_AGAIN;
436   
437   memmove (buf, &msg, sizeof (MSG));
438   *bytes_read = sizeof (MSG);
439   return G_IO_ERROR_NONE;
440 }
441
442 static GIOError
443 g_io_win32_msg_write (GIOChannel *channel,
444                       gchar      *buf,
445                       guint       count,
446                       guint      *bytes_written)
447 {
448   GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
449   MSG msg;
450   
451   if (count != sizeof (MSG))
452     return G_IO_ERROR_INVAL;
453   
454   /* In case of alignment problems */
455   memmove (&msg, buf, sizeof (MSG));
456   if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
457     return G_IO_ERROR_UNKNOWN;
458   
459   *bytes_written = sizeof (MSG);
460   return G_IO_ERROR_NONE;
461 }
462
463 static GIOError
464 g_io_win32_no_seek (GIOChannel *channel,
465                     gint        offset,
466                     GSeekType   type)
467 {
468   return G_IO_ERROR_UNKNOWN;
469 }
470
471 static void
472 g_io_win32_msg_close (GIOChannel *channel)
473 {
474   /* Nothing to be done. Or should we set hwnd to some invalid value? */
475 }
476
477 static void
478 g_io_win32_free (GIOChannel *channel)
479 {
480   GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
481   
482   if (win32_channel->buffer)
483     {
484       CloseHandle (win32_channel->data_avail_event);
485       CloseHandle (win32_channel->space_avail_event);
486       DeleteCriticalSection (&win32_channel->mutex);
487     }
488
489   g_free (win32_channel->buffer);
490   g_free (win32_channel);
491 }
492
493 static guint
494 g_io_win32_msg_add_watch (GIOChannel    *channel,
495                           gint           priority,
496                           GIOCondition   condition,
497                           GIOFunc        func,
498                           gpointer       user_data,
499                           GDestroyNotify notify)
500 {
501   GIOWin32Watch *watch = g_new (GIOWin32Watch, 1);
502   
503   watch->channel = channel;
504   g_io_channel_ref (channel);
505   
506   watch->callback = func;
507   watch->condition = condition;
508   
509   watch->pollfd.fd = G_WIN32_MSG_HANDLE;
510   watch->pollfd.events = condition;
511   
512   g_main_add_poll (&watch->pollfd, priority);
513   
514   return g_source_add (priority, TRUE, &win32_watch_funcs,
515                        watch, user_data, notify);
516 }
517
518 static GIOError
519 g_io_win32_fd_read (GIOChannel *channel,
520                     gchar      *buf,
521                     guint       count,
522                     guint      *bytes_read)
523 {
524   GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
525   gint result;
526   GIOError error;
527   
528   if (win32_channel->debug)
529     g_print ("g_io_win32_fd_read: fd:%d count:%d\n",
530              win32_channel->fd, count);
531   
532   if (win32_channel->thread_id)
533     {
534       result = buffer_read (win32_channel, buf, count, &error);
535       if (result < 0)
536         {
537           *bytes_read = 0;
538           return error;
539         }
540       else
541         {
542           *bytes_read = result;
543           return G_IO_ERROR_NONE;
544         }
545     }
546
547   result = read (win32_channel->fd, buf, count);
548
549   if (result < 0)
550     {
551       *bytes_read = 0;
552       if (errno == EINVAL)
553         return G_IO_ERROR_INVAL;
554       else
555         return G_IO_ERROR_UNKNOWN;
556     }
557   else
558     {
559       *bytes_read = result;
560       return G_IO_ERROR_NONE;
561     }
562 }
563
564 static GIOError
565 g_io_win32_fd_write (GIOChannel *channel,
566                      gchar      *buf,
567                      guint       count,
568                      guint      *bytes_written)
569 {
570   GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
571   gint result;
572   
573   result = write (win32_channel->fd, buf, count);
574   if (win32_channel->debug)
575     g_print ("g_io_win32_fd_write: fd:%d count:%d = %d\n",
576              win32_channel->fd, count, result);
577   
578   if (result < 0)
579     {
580       *bytes_written = 0;
581       switch (errno)
582         {
583         case EINVAL:
584           return G_IO_ERROR_INVAL;
585         case EAGAIN:
586           return G_IO_ERROR_AGAIN;
587         default:
588           return G_IO_ERROR_UNKNOWN;
589         }
590     }
591   else
592     {
593       *bytes_written = result;
594       return G_IO_ERROR_NONE;
595     }
596 }
597
598 static GIOError
599 g_io_win32_fd_seek (GIOChannel *channel,
600                     gint        offset,
601                     GSeekType   type)
602 {
603   GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
604   int whence;
605   off_t result;
606   
607   switch (type)
608     {
609     case G_SEEK_SET:
610       whence = SEEK_SET;
611       break;
612     case G_SEEK_CUR:
613       whence = SEEK_CUR;
614       break;
615     case G_SEEK_END:
616       whence = SEEK_END;
617       break;
618     default:
619       g_warning ("g_io_win32_fd_seek: unknown seek type");
620       return G_IO_ERROR_UNKNOWN;
621     }
622   
623   result = lseek (win32_channel->fd, offset, whence);
624   
625   if (result < 0)
626     {
627       switch (errno)
628         {
629         case EINVAL:
630           return G_IO_ERROR_INVAL;
631         default:
632           return G_IO_ERROR_UNKNOWN;
633         }
634     }
635   else
636     return G_IO_ERROR_NONE;
637 }
638
639 static void
640 g_io_win32_fd_close (GIOChannel *channel)
641 {
642   GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
643   
644   close (win32_channel->fd);
645   return;
646 }
647
648 static int
649 fd_reader (int     fd,
650            guchar *buf,
651            int     len)
652 {
653   return read (fd, buf, len);
654 }
655
656 static guint
657 g_io_win32_fd_add_watch (GIOChannel    *channel,
658                          gint           priority,
659                          GIOCondition   condition,
660                          GIOFunc        func,
661                          gpointer       user_data,
662                          GDestroyNotify notify)
663 {
664   return g_io_win32_add_watch (channel, priority, condition,
665                                func, user_data, notify, fd_reader);
666 }
667
668 static GIOError
669 g_io_win32_sock_read (GIOChannel *channel,
670                       gchar      *buf,
671                       guint       count,
672                       guint      *bytes_read)
673 {
674   GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
675   gint result;
676   GIOError error;
677   
678   if (win32_channel->thread_id)
679     {
680       result = buffer_read (win32_channel, buf, count, &error);
681       if (result < 0)
682         {
683           *bytes_read = 0;
684           return error;
685         }
686       else
687         {
688           *bytes_read = result;
689           return G_IO_ERROR_NONE;
690         }
691     }
692
693   result = recv (win32_channel->fd, buf, count, 0);
694
695   if (result < 0)
696     {
697       *bytes_read = 0;
698       return G_IO_ERROR_UNKNOWN;
699     }
700   else
701     {
702       *bytes_read = result;
703       return G_IO_ERROR_NONE;
704     }
705 }
706
707 static GIOError
708 g_io_win32_sock_write (GIOChannel *channel,
709                        gchar      *buf,
710                        guint       count,
711                        guint      *bytes_written)
712 {
713   GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
714   gint result;
715   
716   result = send (win32_channel->fd, buf, count, 0);
717   
718   if (result == SOCKET_ERROR)
719     {
720       *bytes_written = 0;
721       switch (WSAGetLastError ())
722         {
723         case WSAEINVAL:
724           return G_IO_ERROR_INVAL;
725         case WSAEWOULDBLOCK:
726         case WSAEINTR:
727           return G_IO_ERROR_AGAIN;
728         default:
729           return G_IO_ERROR_UNKNOWN;
730         }
731     }
732   else
733     {
734       *bytes_written = result;
735       return G_IO_ERROR_NONE;
736     }
737 }
738
739 static void
740 g_io_win32_sock_close (GIOChannel *channel)
741 {
742   GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
743
744   closesocket (win32_channel->fd);
745 }
746
747 static int
748 sock_reader (int     fd,
749              guchar *buf,
750              int     len)
751 {
752   return recv (fd, buf, len, 0);
753 }
754
755 static guint
756 g_io_win32_sock_add_watch (GIOChannel    *channel,
757                            gint           priority,
758                            GIOCondition   condition,
759                            GIOFunc        func,
760                            gpointer       user_data,
761                            GDestroyNotify notify)
762 {
763   return g_io_win32_add_watch (channel, priority, condition,
764                                func, user_data, notify, sock_reader);
765 }
766
767 static GIOFuncs win32_channel_msg_funcs = {
768   g_io_win32_msg_read,
769   g_io_win32_msg_write,
770   g_io_win32_no_seek,
771   g_io_win32_msg_close,
772   g_io_win32_msg_add_watch,
773   g_io_win32_free
774 };
775
776 static GIOFuncs win32_channel_fd_funcs = {
777   g_io_win32_fd_read,
778   g_io_win32_fd_write,
779   g_io_win32_fd_seek,
780   g_io_win32_fd_close,
781   g_io_win32_fd_add_watch,
782   g_io_win32_free
783 };
784
785 static GIOFuncs win32_channel_sock_funcs = {
786   g_io_win32_sock_read,
787   g_io_win32_sock_write,
788   g_io_win32_no_seek,
789   g_io_win32_sock_close,
790   g_io_win32_sock_add_watch,
791   g_io_win32_free
792 };
793
794 GIOChannel *
795 g_io_channel_win32_new_messages (guint hwnd)
796 {
797   GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
798   GIOChannel *channel = (GIOChannel *) win32_channel;
799
800   g_io_channel_init (channel);
801   g_io_channel_win32_init (win32_channel);
802   channel->funcs = &win32_channel_msg_funcs;
803   win32_channel->type = G_IO_WINDOWS_MESSAGES;
804   win32_channel->hwnd = (HWND) hwnd;
805
806   return channel;
807 }
808
809 GIOChannel *
810 g_io_channel_win32_new_fd (gint fd)
811 {
812   GIOWin32Channel *win32_channel;
813   GIOChannel *channel;
814   struct stat st;
815
816   if (fstat (fd, &st) == -1)
817     {
818       g_warning ("%d isn't a (emulated) file descriptor", fd);
819       return NULL;
820     }
821
822   win32_channel = g_new (GIOWin32Channel, 1);
823   channel = (GIOChannel *) win32_channel;
824
825   g_io_channel_init (channel);
826   g_io_channel_win32_init (win32_channel);
827   channel->funcs = &win32_channel_fd_funcs;
828   win32_channel->type = G_IO_FILE_DESC;
829   win32_channel->fd = fd;
830
831   return channel;
832 }
833
834 gint
835 g_io_channel_win32_get_fd (GIOChannel *channel)
836 {
837   GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
838
839   return win32_channel->fd;
840 }
841
842 GIOChannel *
843 g_io_channel_win32_new_stream_socket (int socket)
844 {
845   GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
846   GIOChannel *channel = (GIOChannel *) win32_channel;
847
848   g_io_channel_init (channel);
849   g_io_channel_win32_init (win32_channel);
850   channel->funcs = &win32_channel_sock_funcs;
851   win32_channel->type = G_IO_STREAM_SOCKET;
852   win32_channel->fd = socket;
853
854   return channel;
855 }
856
857 GIOChannel *
858 g_io_channel_unix_new (gint fd)
859 {
860   return g_io_channel_win32_new_fd (fd);
861 }
862
863 gint
864 g_io_channel_unix_get_fd (GIOChannel *channel)
865 {
866   return g_io_channel_win32_get_fd (channel);
867 }
868
869 void
870 g_io_channel_win32_set_debug (GIOChannel *channel,
871                               gboolean    flag)
872 {
873   GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
874
875   win32_channel->debug = flag;
876 }
877
878 gint
879 g_io_channel_win32_poll (GPollFD *fds,
880                          gint     n_fds,
881                          gint     timeout)
882 {
883   int result;
884
885   g_return_val_if_fail (n_fds >= 0, 0);
886
887   result = (*g_main_win32_get_poll_func ()) (fds, n_fds, timeout);
888
889   return result;
890 }
891
892 void
893 g_io_channel_win32_make_pollfd (GIOChannel   *channel,
894                                 GIOCondition  condition,
895                                 GPollFD      *fd)
896 {
897   GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
898
899   if (win32_channel->data_avail_event == NULL)
900     create_events (win32_channel);
901   
902   fd->fd = (gint) win32_channel->data_avail_event;
903   fd->events = condition;
904
905   if (win32_channel->thread_id == 0)
906     if (win32_channel->type == G_IO_FILE_DESC)
907       create_reader_thread (win32_channel, fd_reader);
908     else if (win32_channel->type == G_IO_STREAM_SOCKET)
909       create_reader_thread (win32_channel, sock_reader);
910 }
911
912 /* This variable and the functions below are present just to be 
913  * binary compatible with old clients... But note that in GIMP, the
914  * libgimp/gimp.c:gimp_extension_process() function will have to be modified
915  * anyhow for this new approach.
916  *
917  * These will be removed after some weeks.
918  */
919 guint g_pipe_readable_msg = 0;
920
921 GIOChannel *
922 g_io_channel_win32_new_pipe (int fd)
923 {
924   return g_io_channel_win32_new_fd (fd);
925 }
926
927 GIOChannel *
928 g_io_channel_win32_new_pipe_with_wakeups (int   fd,
929                                           guint peer,
930                                           int   peer_fd)
931 {
932   return g_io_channel_win32_new_fd (fd);
933 }
934
935 void
936 g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
937                                          guint       peer,
938                                          int         peer_fd)
939 {
940   /* Nothing needed now */
941 }
942
943 void
944 g_io_channel_win32_pipe_readable (gint  fd,
945                                   guint offset)
946 {
947   /* Nothing needed now */
948 }
949