Blame glib/giowin32.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * giowin32.c: IO Channels for Win32.
Packit ae235b
 * Copyright 1998 Owen Taylor and Tor Lillqvist
Packit ae235b
 * Copyright 1999-2000 Tor Lillqvist and Craig Setera
Packit ae235b
 * Copyright 2001-2003 Andrew Lanoix
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Bugs that are related to the code in this file:
Packit ae235b
 *
Packit ae235b
 * Bug 137968 - Sometimes a GIOFunc on Win32 is called with zero condition
Packit ae235b
 * http://bugzilla.gnome.org/show_bug.cgi?id=137968
Packit ae235b
 *
Packit ae235b
 * Bug 324234 - Using g_io_add_watch_full() to wait for connect() to return on a non-blocking socket returns prematurely
Packit ae235b
 * http://bugzilla.gnome.org/show_bug.cgi?id=324234
Packit ae235b
 *
Packit ae235b
 * Bug 331214 - g_io_channel async socket io stalls
Packit ae235b
 * http://bugzilla.gnome.org/show_bug.cgi?id=331214
Packit ae235b
 *
Packit ae235b
 * Bug 338943 - Multiple watches on the same socket
Packit ae235b
 * http://bugzilla.gnome.org/show_bug.cgi?id=338943
Packit ae235b
 *
Packit ae235b
 * Bug 357674 - 2 serious bugs in giowin32.c making glib iochannels useless
Packit ae235b
 * http://bugzilla.gnome.org/show_bug.cgi?id=357674
Packit ae235b
 *
Packit ae235b
 * Bug 425156 - GIOChannel deadlocks on a win32 socket
Packit ae235b
 * http://bugzilla.gnome.org/show_bug.cgi?id=425156
Packit ae235b
 *
Packit ae235b
 * Bug 468910 - giofunc condition=0
Packit ae235b
 * http://bugzilla.gnome.org/show_bug.cgi?id=468910
Packit ae235b
 *
Packit ae235b
 * Bug 500246 - Bug fixes for giowin32
Packit ae235b
 * http://bugzilla.gnome.org/show_bug.cgi?id=500246
Packit ae235b
 *
Packit ae235b
 * Bug 548278 - Async GETs connections are always terminated unexpectedly on windows
Packit ae235b
 * http://bugzilla.gnome.org/show_bug.cgi?id=548278
Packit ae235b
 *
Packit ae235b
 * Bug 548536 - giowin32 problem when adding and removing watches
Packit ae235b
 * http://bugzilla.gnome.org/show_bug.cgi?id=548536
Packit ae235b
 *
Packit ae235b
 * When fixing bugs related to the code in this file, either the above
Packit ae235b
 * bugs or others, make sure that the test programs attached to the
Packit ae235b
 * above bugs continue to work.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "glib.h"
Packit ae235b
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <winsock2.h>
Packit ae235b
#include <windows.h>
Packit ae235b
#include <conio.h>
Packit ae235b
#include <fcntl.h>
Packit ae235b
#include <io.h>
Packit ae235b
#include <process.h>
Packit ae235b
#include <errno.h>
Packit ae235b
#include <sys/stat.h>
Packit ae235b
Packit ae235b
#include "gstdio.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
typedef struct _GIOWin32Channel GIOWin32Channel;
Packit ae235b
typedef struct _GIOWin32Watch GIOWin32Watch;
Packit ae235b
Packit ae235b
#define BUFFER_SIZE 4096
Packit ae235b
Packit ae235b
typedef enum {
Packit ae235b
  G_IO_WIN32_WINDOWS_MESSAGES,	/* Windows messages */
Packit ae235b
Packit ae235b
  G_IO_WIN32_FILE_DESC,		/* Unix-like file descriptors from
Packit ae235b
				 * _open() or _pipe(), except for
Packit ae235b
				 * console IO. Separate thread to read
Packit ae235b
				 * or write.
Packit ae235b
				 */
Packit ae235b
Packit ae235b
  G_IO_WIN32_CONSOLE,		/* Console IO (usually stdin, stdout, stderr) */
Packit ae235b
Packit ae235b
  G_IO_WIN32_SOCKET		/* Sockets. No separate thread. */
Packit ae235b
} GIOWin32ChannelType;
Packit ae235b
Packit ae235b
struct _GIOWin32Channel {
Packit ae235b
  GIOChannel channel;
Packit ae235b
  gint fd;			/* Either a Unix-like file handle as provided
Packit ae235b
				 * by the Microsoft C runtime, or a SOCKET
Packit ae235b
				 * as provided by WinSock.
Packit ae235b
				 */
Packit ae235b
  GIOWin32ChannelType type;
Packit ae235b
  
Packit ae235b
  gboolean debug;
Packit ae235b
Packit ae235b
  /* Field used by G_IO_WIN32_WINDOWS_MESSAGES channels */
Packit ae235b
  HWND hwnd;			/* Handle of window, or NULL */
Packit ae235b
  
Packit ae235b
  /* Fields used by G_IO_WIN32_FILE_DESC channels. */
Packit ae235b
  CRITICAL_SECTION mutex;
Packit ae235b
Packit ae235b
  int direction;		/* 0 means we read from it,
Packit ae235b
				 * 1 means we write to it.
Packit ae235b
				 */
Packit ae235b
Packit ae235b
  gboolean running;		/* Is reader or writer thread
Packit ae235b
				 * running. FALSE if EOF has been
Packit ae235b
				 * reached by the reader thread.
Packit ae235b
				 */
Packit ae235b
Packit ae235b
  gboolean needs_close;		/* If the channel has been closed while
Packit ae235b
				 * the reader thread was still running.
Packit ae235b
				 */
Packit ae235b
Packit ae235b
  guint thread_id;		/* If non-NULL the channel has or has
Packit ae235b
				 * had a reader or writer thread.
Packit ae235b
				 */
Packit ae235b
  HANDLE data_avail_event;
Packit ae235b
Packit ae235b
  gushort revents;
Packit ae235b
Packit ae235b
  /* Data is kept in a circular buffer. To be able to distinguish between
Packit ae235b
   * empty and full buffers, we cannot fill it completely, but have to
Packit ae235b
   * leave a one character gap.
Packit ae235b
   *
Packit ae235b
   * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
Packit ae235b
   *
Packit ae235b
   * Empty:    wrp == rdp
Packit ae235b
   * Full:     (wrp + 1) % BUFFER_SIZE == rdp
Packit ae235b
   * Partial:  otherwise
Packit ae235b
   */
Packit ae235b
  guchar *buffer;		/* (Circular) buffer */
Packit ae235b
  gint wrp, rdp;		/* Buffer indices for writing and reading */
Packit ae235b
  HANDLE space_avail_event;
Packit ae235b
Packit ae235b
  /* Fields used by G_IO_WIN32_SOCKET channels */
Packit ae235b
  int event_mask;
Packit ae235b
  int last_events;
Packit ae235b
  HANDLE event;
Packit ae235b
  gboolean write_would_have_blocked;
Packit ae235b
  gboolean ever_writable;
Packit ae235b
};
Packit ae235b
Packit ae235b
struct _GIOWin32Watch {
Packit ae235b
  GSource       source;
Packit ae235b
  GPollFD       pollfd;
Packit ae235b
  GIOChannel   *channel;
Packit ae235b
  GIOCondition  condition;
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_win32_print_access_mode (int flags)
Packit ae235b
{
Packit ae235b
  g_print ("%s%s%s%s%s%s%s%s%s%s",
Packit ae235b
	   ((flags & 0x3) == _O_RDWR ? "O_RDWR" :
Packit ae235b
	    ((flags & 0x3) == _O_RDONLY ? "O_RDONLY" :
Packit ae235b
	     ((flags & 0x3) == _O_WRONLY ? "O_WRONLY" : "0"))),
Packit ae235b
	   (flags & _O_APPEND ? "|O_APPEND" : ""),
Packit ae235b
	   (flags & _O_RANDOM ? "|O_RANDOM" : ""),
Packit ae235b
	   (flags & _O_SEQUENTIAL ? "|O_SEQUENTIAL" : ""),
Packit ae235b
	   (flags & _O_TEMPORARY ? "|O_TEMPORARY" : ""),
Packit ae235b
	   (flags & _O_CREAT ? "|O_CREAT" : ""),
Packit ae235b
	   (flags & _O_TRUNC ? "|O_TRUNC" : ""),
Packit ae235b
	   (flags & _O_EXCL ? "|O_EXCL" : ""),
Packit ae235b
	   (flags & _O_TEXT ? "|O_TEXT" : ""),
Packit ae235b
	   (flags & _O_BINARY ? "|O_BINARY" : ""));
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_win32_print_gioflags (GIOFlags flags)
Packit ae235b
{
Packit ae235b
  char *bar = "";
Packit ae235b
Packit ae235b
  if (flags & G_IO_FLAG_APPEND)
Packit ae235b
    bar = "|", g_print ("APPEND");
Packit ae235b
  if (flags & G_IO_FLAG_NONBLOCK)
Packit ae235b
    g_print ("%sNONBLOCK", bar), bar = "|";
Packit ae235b
  if (flags & G_IO_FLAG_IS_READABLE)
Packit ae235b
    g_print ("%sREADABLE", bar), bar = "|";
Packit ae235b
  if (flags & G_IO_FLAG_IS_WRITABLE)
Packit ae235b
    g_print ("%sWRITABLE", bar), bar = "|";
Packit ae235b
  if (flags & G_IO_FLAG_IS_SEEKABLE)
Packit ae235b
    g_print ("%sSEEKABLE", bar), bar = "|";
Packit ae235b
}
Packit ae235b
Packit ae235b
static const char *
Packit ae235b
event_mask_to_string (int mask)
Packit ae235b
{
Packit ae235b
  char buf[100];
Packit ae235b
  int checked_bits = 0;
Packit ae235b
  char *bufp = buf;
Packit ae235b
Packit ae235b
  if (mask == 0)
Packit ae235b
    return "";
Packit ae235b
Packit ae235b
#define BIT(n) checked_bits |= FD_##n; if (mask & FD_##n) bufp += sprintf (bufp, "%s" #n, (bufp>buf ? "|" : ""))
Packit ae235b
Packit ae235b
  BIT (READ);
Packit ae235b
  BIT (WRITE);
Packit ae235b
  BIT (OOB);
Packit ae235b
  BIT (ACCEPT);
Packit ae235b
  BIT (CONNECT);
Packit ae235b
  BIT (CLOSE);
Packit ae235b
  BIT (QOS);
Packit ae235b
  BIT (GROUP_QOS);
Packit ae235b
  BIT (ROUTING_INTERFACE_CHANGE);
Packit ae235b
  BIT (ADDRESS_LIST_CHANGE);
Packit ae235b
  
Packit ae235b
#undef BIT
Packit ae235b
Packit ae235b
  if ((mask & ~checked_bits) != 0)
Packit ae235b
	  bufp += sprintf (bufp, "|%#x", mask & ~checked_bits);
Packit ae235b
  
Packit ae235b
  return g_quark_to_string (g_quark_from_string (buf));
Packit ae235b
}
Packit ae235b
Packit ae235b
static const char *
Packit ae235b
condition_to_string (GIOCondition condition)
Packit ae235b
{
Packit ae235b
  char buf[100];
Packit ae235b
  int checked_bits = 0;
Packit ae235b
  char *bufp = buf;
Packit ae235b
Packit ae235b
  if (condition == 0)
Packit ae235b
    return "";
Packit ae235b
Packit ae235b
#define BIT(n) checked_bits |= G_IO_##n; if (condition & G_IO_##n) bufp += sprintf (bufp, "%s" #n, (bufp>buf ? "|" : ""))
Packit ae235b
Packit ae235b
  BIT (IN);
Packit ae235b
  BIT (OUT);
Packit ae235b
  BIT (PRI);
Packit ae235b
  BIT (ERR);
Packit ae235b
  BIT (HUP);
Packit ae235b
  BIT (NVAL);
Packit ae235b
  
Packit ae235b
#undef BIT
Packit ae235b
Packit ae235b
  if ((condition & ~checked_bits) != 0)
Packit ae235b
	  bufp += sprintf (bufp, "|%#x", condition & ~checked_bits);
Packit ae235b
  
Packit ae235b
  return g_quark_to_string (g_quark_from_string (buf));
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_io_win32_get_debug_flag (void)
Packit ae235b
{
Packit ae235b
  return (getenv ("G_IO_WIN32_DEBUG") != NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_io_channel_win32_init (GIOWin32Channel *channel)
Packit ae235b
{
Packit ae235b
  channel->debug = g_io_win32_get_debug_flag ();
Packit ae235b
Packit ae235b
  InitializeCriticalSection (&channel->mutex);
Packit ae235b
  channel->running = FALSE;
Packit ae235b
  channel->needs_close = FALSE;
Packit ae235b
  channel->thread_id = 0;
Packit ae235b
  channel->data_avail_event = NULL;
Packit ae235b
  channel->revents = 0;
Packit ae235b
  channel->buffer = NULL;
Packit ae235b
  channel->space_avail_event = NULL;
Packit ae235b
Packit ae235b
  channel->event_mask = 0;
Packit ae235b
  channel->last_events = 0;
Packit ae235b
  channel->event = NULL;
Packit ae235b
  channel->write_would_have_blocked = FALSE;
Packit ae235b
  channel->ever_writable = FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
create_events (GIOWin32Channel *channel)
Packit ae235b
{
Packit ae235b
  SECURITY_ATTRIBUTES sec_attrs;
Packit ae235b
  
Packit ae235b
  sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
Packit ae235b
  sec_attrs.lpSecurityDescriptor = NULL;
Packit ae235b
  sec_attrs.bInheritHandle = FALSE;
Packit ae235b
Packit ae235b
  /* The data available event is manual reset, the space available event
Packit ae235b
   * is automatic reset.
Packit ae235b
   */
Packit ae235b
  if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
Packit ae235b
      || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
Packit ae235b
    {
Packit ae235b
      gchar *emsg = g_win32_error_message (GetLastError ());
Packit ae235b
Packit ae235b
      g_error ("Error creating event: %s", emsg);
Packit ae235b
      g_free (emsg);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static unsigned __stdcall
Packit ae235b
read_thread (void *parameter)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *channel = parameter;
Packit ae235b
  guchar *buffer;
Packit ae235b
  gint nbytes;
Packit ae235b
Packit ae235b
  g_io_channel_ref ((GIOChannel *)channel);
Packit ae235b
Packit ae235b
  if (channel->debug)
Packit ae235b
    g_print ("read_thread %#x: start fd=%d, data_avail=%p space_avail=%p\n",
Packit ae235b
	     channel->thread_id,
Packit ae235b
	     channel->fd,
Packit ae235b
	     channel->data_avail_event,
Packit ae235b
	     channel->space_avail_event);
Packit ae235b
Packit ae235b
  channel->direction = 0;
Packit ae235b
  channel->buffer = g_malloc (BUFFER_SIZE);
Packit ae235b
  channel->rdp = channel->wrp = 0;
Packit ae235b
  channel->running = TRUE;
Packit ae235b
Packit ae235b
  SetEvent (channel->space_avail_event);
Packit ae235b
  
Packit ae235b
  EnterCriticalSection (&channel->mutex);
Packit ae235b
  while (channel->running)
Packit ae235b
    {
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
Packit ae235b
		 channel->thread_id, channel->rdp, channel->wrp);
Packit ae235b
      if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
Packit ae235b
	{
Packit ae235b
	  /* Buffer is full */
Packit ae235b
	  if (channel->debug)
Packit ae235b
	    g_print ("read_thread %#x: resetting space_avail\n",
Packit ae235b
		     channel->thread_id);
Packit ae235b
	  ResetEvent (channel->space_avail_event);
Packit ae235b
	  if (channel->debug)
Packit ae235b
	    g_print ("read_thread %#x: waiting for space\n",
Packit ae235b
		     channel->thread_id);
Packit ae235b
	  LeaveCriticalSection (&channel->mutex);
Packit ae235b
	  WaitForSingleObject (channel->space_avail_event, INFINITE);
Packit ae235b
	  EnterCriticalSection (&channel->mutex);
Packit ae235b
	  if (channel->debug)
Packit ae235b
	    g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
Packit ae235b
		     channel->thread_id, channel->rdp, channel->wrp);
Packit ae235b
	}
Packit ae235b
      
Packit ae235b
      buffer = channel->buffer + channel->wrp;
Packit ae235b
      
Packit ae235b
      /* Always leave at least one byte unused gap to be able to
Packit ae235b
       * distinguish between the full and empty condition...
Packit ae235b
       */
Packit ae235b
      nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
Packit ae235b
		    BUFFER_SIZE - channel->wrp);
Packit ae235b
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("read_thread %#x: calling read() for %d bytes\n",
Packit ae235b
		 channel->thread_id, nbytes);
Packit ae235b
Packit ae235b
      LeaveCriticalSection (&channel->mutex);
Packit ae235b
Packit ae235b
      nbytes = read (channel->fd, buffer, nbytes);
Packit ae235b
      
Packit ae235b
      EnterCriticalSection (&channel->mutex);
Packit ae235b
Packit ae235b
      channel->revents = G_IO_IN;
Packit ae235b
      if (nbytes == 0)
Packit ae235b
	channel->revents |= G_IO_HUP;
Packit ae235b
      else if (nbytes < 0)
Packit ae235b
	channel->revents |= G_IO_ERR;
Packit ae235b
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("read_thread %#x: read() returned %d, rdp=%d, wrp=%d\n",
Packit ae235b
		 channel->thread_id, nbytes, channel->rdp, channel->wrp);
Packit ae235b
Packit ae235b
      if (nbytes <= 0)
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("read_thread %#x: rdp=%d, wrp=%d, setting data_avail\n",
Packit ae235b
		 channel->thread_id, channel->rdp, channel->wrp);
Packit ae235b
      SetEvent (channel->data_avail_event);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  channel->running = FALSE;
Packit ae235b
  if (channel->needs_close)
Packit ae235b
    {
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("read_thread %#x: channel fd %d needs closing\n",
Packit ae235b
		 channel->thread_id, channel->fd);
Packit ae235b
      close (channel->fd);
Packit ae235b
      channel->fd = -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (channel->debug)
Packit ae235b
    g_print ("read_thread %#x: EOF, rdp=%d, wrp=%d, setting data_avail\n",
Packit ae235b
	     channel->thread_id, channel->rdp, channel->wrp);
Packit ae235b
  SetEvent (channel->data_avail_event);
Packit ae235b
  LeaveCriticalSection (&channel->mutex);
Packit ae235b
  
Packit ae235b
  g_io_channel_unref ((GIOChannel *)channel);
Packit ae235b
  
Packit ae235b
  /* No need to call _endthreadex(), the actual thread starter routine
Packit ae235b
   * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
Packit ae235b
   * _endthreadex() for us.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static unsigned __stdcall
Packit ae235b
write_thread (void *parameter)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *channel = parameter;
Packit ae235b
  guchar *buffer;
Packit ae235b
  gint nbytes;
Packit ae235b
Packit ae235b
  g_io_channel_ref ((GIOChannel *)channel);
Packit ae235b
Packit ae235b
  if (channel->debug)
Packit ae235b
    g_print ("write_thread %#x: start fd=%d, data_avail=%p space_avail=%p\n",
Packit ae235b
	     channel->thread_id,
Packit ae235b
	     channel->fd,
Packit ae235b
	     channel->data_avail_event,
Packit ae235b
	     channel->space_avail_event);
Packit ae235b
  
Packit ae235b
  channel->direction = 1;
Packit ae235b
  channel->buffer = g_malloc (BUFFER_SIZE);
Packit ae235b
  channel->rdp = channel->wrp = 0;
Packit ae235b
  channel->running = TRUE;
Packit ae235b
Packit ae235b
  SetEvent (channel->space_avail_event);
Packit ae235b
Packit ae235b
  /* We use the same event objects as for a reader thread, but with
Packit ae235b
   * reversed meaning. So, space_avail is used if data is available
Packit ae235b
   * for writing, and data_avail is used if space is available in the
Packit ae235b
   * write buffer.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  EnterCriticalSection (&channel->mutex);
Packit ae235b
  while (channel->running || channel->rdp != channel->wrp)
Packit ae235b
    {
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("write_thread %#x: rdp=%d, wrp=%d\n",
Packit ae235b
		 channel->thread_id, channel->rdp, channel->wrp);
Packit ae235b
      if (channel->wrp == channel->rdp)
Packit ae235b
	{
Packit ae235b
	  /* Buffer is empty. */
Packit ae235b
	  if (channel->debug)
Packit ae235b
	    g_print ("write_thread %#x: resetting space_avail\n",
Packit ae235b
		     channel->thread_id);
Packit ae235b
	  ResetEvent (channel->space_avail_event);
Packit ae235b
	  if (channel->debug)
Packit ae235b
	    g_print ("write_thread %#x: waiting for data\n",
Packit ae235b
		     channel->thread_id);
Packit ae235b
	  channel->revents = G_IO_OUT;
Packit ae235b
	  SetEvent (channel->data_avail_event);
Packit ae235b
	  LeaveCriticalSection (&channel->mutex);
Packit ae235b
	  WaitForSingleObject (channel->space_avail_event, INFINITE);
Packit ae235b
Packit ae235b
	  EnterCriticalSection (&channel->mutex);
Packit ae235b
	  if (channel->rdp == channel->wrp)
Packit ae235b
	    break;
Packit ae235b
Packit ae235b
	  if (channel->debug)
Packit ae235b
	    g_print ("write_thread %#x: rdp=%d, wrp=%d\n",
Packit ae235b
		     channel->thread_id, channel->rdp, channel->wrp);
Packit ae235b
	}
Packit ae235b
      
Packit ae235b
      buffer = channel->buffer + channel->rdp;
Packit ae235b
      if (channel->rdp < channel->wrp)
Packit ae235b
	nbytes = channel->wrp - channel->rdp;
Packit ae235b
      else
Packit ae235b
	nbytes = BUFFER_SIZE - channel->rdp;
Packit ae235b
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("write_thread %#x: calling write() for %d bytes\n",
Packit ae235b
		 channel->thread_id, nbytes);
Packit ae235b
Packit ae235b
      LeaveCriticalSection (&channel->mutex);
Packit ae235b
      nbytes = write (channel->fd, buffer, nbytes);
Packit ae235b
      EnterCriticalSection (&channel->mutex);
Packit ae235b
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("write_thread %#x: write(%i) returned %d, rdp=%d, wrp=%d\n",
Packit ae235b
		 channel->thread_id, channel->fd, nbytes, channel->rdp, channel->wrp);
Packit ae235b
Packit ae235b
      channel->revents = 0;
Packit ae235b
      if (nbytes > 0)
Packit ae235b
	channel->revents |= G_IO_OUT;
Packit ae235b
      else if (nbytes <= 0)
Packit ae235b
	channel->revents |= G_IO_ERR;
Packit ae235b
Packit ae235b
      channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
Packit ae235b
Packit ae235b
      if (nbytes <= 0)
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("write_thread: setting data_avail for thread %#x\n",
Packit ae235b
		 channel->thread_id);
Packit ae235b
      SetEvent (channel->data_avail_event);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  channel->running = FALSE;
Packit ae235b
  if (channel->needs_close)
Packit ae235b
    {
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("write_thread %#x: channel fd %d needs closing\n",
Packit ae235b
		 channel->thread_id, channel->fd);
Packit ae235b
      close (channel->fd);
Packit ae235b
      channel->fd = -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  LeaveCriticalSection (&channel->mutex);
Packit ae235b
  
Packit ae235b
  g_io_channel_unref ((GIOChannel *)channel);
Packit ae235b
  
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
create_thread (GIOWin32Channel     *channel,
Packit ae235b
	       GIOCondition         condition,
Packit ae235b
	       unsigned (__stdcall *thread) (void *parameter))
Packit ae235b
{
Packit ae235b
  HANDLE thread_handle;
Packit ae235b
  int errsv;
Packit ae235b
Packit ae235b
  thread_handle = (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0,
Packit ae235b
					   &channel->thread_id);
Packit ae235b
  errsv = errno;
Packit ae235b
  if (thread_handle == 0)
Packit ae235b
    g_warning ("Error creating thread: %s.",
Packit ae235b
	       g_strerror (errsv));
Packit ae235b
  else if (!CloseHandle (thread_handle))
Packit ae235b
    {
Packit ae235b
      gchar *emsg = g_win32_error_message (GetLastError ());
Packit ae235b
Packit ae235b
      g_warning ("Error closing thread handle: %s.", emsg);
Packit ae235b
      g_free (emsg);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  WaitForSingleObject (channel->space_avail_event, INFINITE);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
buffer_read (GIOWin32Channel *channel,
Packit ae235b
	     gchar           *dest,
Packit ae235b
	     gsize            count,
Packit ae235b
	     gsize           *bytes_read,
Packit ae235b
	     GError         **err)
Packit ae235b
{
Packit ae235b
  guint nbytes;
Packit ae235b
  guint left = count;
Packit ae235b
  
Packit ae235b
  EnterCriticalSection (&channel->mutex);
Packit ae235b
  if (channel->debug)
Packit ae235b
    g_print ("reading from thread %#x %" G_GSIZE_FORMAT " bytes, rdp=%d, wrp=%d\n",
Packit ae235b
	     channel->thread_id, count, channel->rdp, channel->wrp);
Packit ae235b
  
Packit ae235b
  if (channel->wrp == channel->rdp)
Packit ae235b
    {
Packit ae235b
      LeaveCriticalSection (&channel->mutex);
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("waiting for data from thread %#x\n", channel->thread_id);
Packit ae235b
      WaitForSingleObject (channel->data_avail_event, INFINITE);
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("done waiting for data from thread %#x\n", channel->thread_id);
Packit ae235b
      EnterCriticalSection (&channel->mutex);
Packit ae235b
      if (channel->wrp == channel->rdp && !channel->running)
Packit ae235b
	{
Packit ae235b
	  if (channel->debug)
Packit ae235b
	    g_print ("wrp==rdp, !running\n");
Packit ae235b
	  LeaveCriticalSection (&channel->mutex);
Packit ae235b
          *bytes_read = 0;
Packit ae235b
	  return G_IO_STATUS_EOF;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (channel->rdp < channel->wrp)
Packit ae235b
    nbytes = channel->wrp - channel->rdp;
Packit ae235b
  else
Packit ae235b
    nbytes = BUFFER_SIZE - channel->rdp;
Packit ae235b
  LeaveCriticalSection (&channel->mutex);
Packit ae235b
  nbytes = MIN (left, nbytes);
Packit ae235b
  if (channel->debug)
Packit ae235b
    g_print ("moving %d bytes from thread %#x\n",
Packit ae235b
	     nbytes, channel->thread_id);
Packit ae235b
  memcpy (dest, channel->buffer + channel->rdp, nbytes);
Packit ae235b
  dest += nbytes;
Packit ae235b
  left -= nbytes;
Packit ae235b
  EnterCriticalSection (&channel->mutex);
Packit ae235b
  channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
Packit ae235b
  if (channel->debug)
Packit ae235b
    g_print ("setting space_avail for thread %#x\n", channel->thread_id);
Packit ae235b
  SetEvent (channel->space_avail_event);
Packit ae235b
  if (channel->debug)
Packit ae235b
    g_print ("for thread %#x: rdp=%d, wrp=%d\n",
Packit ae235b
	     channel->thread_id, channel->rdp, channel->wrp);
Packit ae235b
  if (channel->running && channel->wrp == channel->rdp)
Packit ae235b
    {
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("resetting data_avail of thread %#x\n",
Packit ae235b
		 channel->thread_id);
Packit ae235b
      ResetEvent (channel->data_avail_event);
Packit ae235b
    };
Packit ae235b
  LeaveCriticalSection (&channel->mutex);
Packit ae235b
  
Packit ae235b
  /* We have no way to indicate any errors form the actual
Packit ae235b
   * read() or recv() call in the reader thread. Should we have?
Packit ae235b
   */
Packit ae235b
  *bytes_read = count - left;
Packit ae235b
  return (*bytes_read > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
buffer_write (GIOWin32Channel *channel,
Packit ae235b
	      const gchar     *dest,
Packit ae235b
	      gsize            count,
Packit ae235b
	      gsize           *bytes_written,
Packit ae235b
	      GError         **err)
Packit ae235b
{
Packit ae235b
  guint nbytes;
Packit ae235b
  guint left = count;
Packit ae235b
  
Packit ae235b
  EnterCriticalSection (&channel->mutex);
Packit ae235b
  if (channel->debug)
Packit ae235b
    g_print ("buffer_write: writing to thread %#x %" G_GSIZE_FORMAT " bytes, rdp=%d, wrp=%d\n",
Packit ae235b
	     channel->thread_id, count, channel->rdp, channel->wrp);
Packit ae235b
  
Packit ae235b
  if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
Packit ae235b
    {
Packit ae235b
      /* Buffer is full */
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("buffer_write: tid %#x: resetting data_avail\n",
Packit ae235b
		 channel->thread_id);
Packit ae235b
      ResetEvent (channel->data_avail_event);
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("buffer_write: tid %#x: waiting for space\n",
Packit ae235b
		 channel->thread_id);
Packit ae235b
      LeaveCriticalSection (&channel->mutex);
Packit ae235b
      WaitForSingleObject (channel->data_avail_event, INFINITE);
Packit ae235b
      EnterCriticalSection (&channel->mutex);
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d\n",
Packit ae235b
		 channel->thread_id, channel->rdp, channel->wrp);
Packit ae235b
    }
Packit ae235b
   
Packit ae235b
  nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
Packit ae235b
		BUFFER_SIZE - channel->wrp);
Packit ae235b
Packit ae235b
  LeaveCriticalSection (&channel->mutex);
Packit ae235b
  nbytes = MIN (left, nbytes);
Packit ae235b
  if (channel->debug)
Packit ae235b
    g_print ("buffer_write: tid %#x: writing %d bytes\n",
Packit ae235b
	     channel->thread_id, nbytes);
Packit ae235b
  memcpy (channel->buffer + channel->wrp, dest, nbytes);
Packit ae235b
  dest += nbytes;
Packit ae235b
  left -= nbytes;
Packit ae235b
  EnterCriticalSection (&channel->mutex);
Packit ae235b
Packit ae235b
  channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
Packit ae235b
  if (channel->debug)
Packit ae235b
    g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d, setting space_avail\n",
Packit ae235b
	     channel->thread_id, channel->rdp, channel->wrp);
Packit ae235b
  SetEvent (channel->space_avail_event);
Packit ae235b
Packit ae235b
  if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
Packit ae235b
    {
Packit ae235b
      /* Buffer is full */
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("buffer_write: tid %#x: resetting data_avail\n",
Packit ae235b
		 channel->thread_id);
Packit ae235b
      ResetEvent (channel->data_avail_event);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  LeaveCriticalSection (&channel->mutex);
Packit ae235b
  
Packit ae235b
  /* We have no way to indicate any errors form the actual
Packit ae235b
   * write() call in the writer thread. Should we have?
Packit ae235b
   */
Packit ae235b
  *bytes_written = count - left;
Packit ae235b
  return (*bytes_written > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_io_win32_prepare (GSource *source,
Packit ae235b
		    gint    *timeout)
Packit ae235b
{
Packit ae235b
  GIOWin32Watch *watch = (GIOWin32Watch *)source;
Packit ae235b
  GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
Packit ae235b
  GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
Packit ae235b
  int event_mask;
Packit ae235b
  
Packit ae235b
  *timeout = -1;
Packit ae235b
  
Packit ae235b
  if (channel->debug)
Packit ae235b
    g_print ("g_io_win32_prepare: source=%p channel=%p", source, channel);
Packit ae235b
Packit ae235b
  switch (channel->type)
Packit ae235b
    {
Packit ae235b
    case G_IO_WIN32_WINDOWS_MESSAGES:
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print (" MSG");
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case G_IO_WIN32_CONSOLE:
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print (" CON");
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case G_IO_WIN32_FILE_DESC:
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print (" FD thread=%#x buffer_condition:{%s}"
Packit ae235b
		 "\n  watch->pollfd.events:{%s} watch->pollfd.revents:{%s} channel->revents:{%s}",
Packit ae235b
		 channel->thread_id, condition_to_string (buffer_condition),
Packit ae235b
		 condition_to_string (watch->pollfd.events),
Packit ae235b
		 condition_to_string (watch->pollfd.revents),
Packit ae235b
		 condition_to_string (channel->revents));
Packit ae235b
      
Packit ae235b
      EnterCriticalSection (&channel->mutex);
Packit ae235b
      if (channel->running)
Packit ae235b
	{
Packit ae235b
	  if (channel->direction == 0 && channel->wrp == channel->rdp)
Packit ae235b
	    {
Packit ae235b
	      if (channel->debug)
Packit ae235b
		g_print ("\n  setting revents=0");
Packit ae235b
	      channel->revents = 0;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  if (channel->direction == 1
Packit ae235b
	      && (channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
Packit ae235b
	    {
Packit ae235b
	      if (channel->debug)
Packit ae235b
		g_print ("\n setting revents=0");
Packit ae235b
	      channel->revents = 0;
Packit ae235b
	    }
Packit ae235b
	}	  
Packit ae235b
      LeaveCriticalSection (&channel->mutex);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case G_IO_WIN32_SOCKET:
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print (" SOCK");
Packit ae235b
      event_mask = 0;
Packit ae235b
      if (watch->condition & G_IO_IN)
Packit ae235b
	event_mask |= (FD_READ | FD_ACCEPT);
Packit ae235b
      if (watch->condition & G_IO_OUT)
Packit ae235b
	event_mask |= (FD_WRITE | FD_CONNECT);
Packit ae235b
      event_mask |= FD_CLOSE;
Packit ae235b
Packit ae235b
      if (channel->event_mask != event_mask)
Packit ae235b
	{
Packit ae235b
	  if (channel->debug)
Packit ae235b
	    g_print ("\n  WSAEventSelect(%d,%p,{%s})",
Packit ae235b
		     channel->fd, (HANDLE) watch->pollfd.fd,
Packit ae235b
		     event_mask_to_string (event_mask));
Packit ae235b
	  if (WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd,
Packit ae235b
			      event_mask) == SOCKET_ERROR)
Packit ae235b
	    if (channel->debug)
Packit ae235b
	      {
Packit ae235b
		gchar *emsg = g_win32_error_message (WSAGetLastError ());
Packit ae235b
Packit ae235b
		g_print (" failed: %s", emsg);
Packit ae235b
		g_free (emsg);
Packit ae235b
	      }
Packit ae235b
	  channel->event_mask = event_mask;
Packit ae235b
Packit ae235b
	  if (channel->debug)
Packit ae235b
	    g_print ("\n  setting last_events=0");
Packit ae235b
	  channel->last_events = 0;
Packit ae235b
Packit ae235b
	  if ((event_mask & FD_WRITE) &&
Packit ae235b
	      channel->ever_writable &&
Packit ae235b
	      !channel->write_would_have_blocked)
Packit ae235b
	    {
Packit ae235b
	      if (channel->debug)
Packit ae235b
		g_print (" WSASetEvent(%p)", (WSAEVENT) watch->pollfd.fd);
Packit ae235b
	      WSASetEvent ((WSAEVENT) watch->pollfd.fd);
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
      g_abort ();
Packit ae235b
    }
Packit ae235b
  if (channel->debug)
Packit ae235b
    g_print ("\n");
Packit ae235b
Packit ae235b
  return ((watch->condition & buffer_condition) == watch->condition);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_io_win32_check (GSource *source)
Packit ae235b
{
Packit ae235b
  MSG msg;
Packit ae235b
  GIOWin32Watch *watch = (GIOWin32Watch *)source;
Packit ae235b
  GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
Packit ae235b
  GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
Packit ae235b
  WSANETWORKEVENTS events;
Packit ae235b
Packit ae235b
  if (channel->debug)
Packit ae235b
    g_print ("g_io_win32_check: source=%p channel=%p", source, channel);
Packit ae235b
Packit ae235b
  switch (channel->type)
Packit ae235b
    {
Packit ae235b
    case G_IO_WIN32_WINDOWS_MESSAGES:
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print (" MSG\n");
Packit ae235b
      return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
Packit ae235b
Packit ae235b
    case G_IO_WIN32_FILE_DESC:
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print (" FD thread=%#x buffer_condition=%s\n"
Packit ae235b
		 "  watch->pollfd.events={%s} watch->pollfd.revents={%s} channel->revents={%s}\n",
Packit ae235b
		 channel->thread_id, condition_to_string (buffer_condition),
Packit ae235b
		 condition_to_string (watch->pollfd.events),
Packit ae235b
		 condition_to_string (watch->pollfd.revents),
Packit ae235b
		 condition_to_string (channel->revents));
Packit ae235b
      
Packit ae235b
      watch->pollfd.revents = (watch->pollfd.events & channel->revents);
Packit ae235b
Packit ae235b
      return ((watch->pollfd.revents | buffer_condition) & watch->condition);
Packit ae235b
Packit ae235b
    case G_IO_WIN32_CONSOLE:
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print (" CON\n");
Packit ae235b
      if (watch->channel->is_writeable)
Packit ae235b
	return TRUE;
Packit ae235b
      else if (watch->channel->is_readable)
Packit ae235b
        {
Packit ae235b
	  INPUT_RECORD buffer;
Packit ae235b
	  DWORD n;
Packit ae235b
	  if (PeekConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n) &&
Packit ae235b
	      n == 1)
Packit ae235b
	    {
Packit ae235b
	      /* _kbhit() does quite complex processing to find out
Packit ae235b
	       * whether at least one of the key events pending corresponds
Packit ae235b
	       * to a "real" character that can be read.
Packit ae235b
	       */
Packit ae235b
	      if (_kbhit ())
Packit ae235b
		return TRUE;
Packit ae235b
	      
Packit ae235b
	      /* Discard all other kinds of events */
Packit ae235b
	      ReadConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n);
Packit ae235b
	    }
Packit ae235b
        }
Packit ae235b
      return FALSE;
Packit ae235b
Packit ae235b
    case G_IO_WIN32_SOCKET:
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print (" SOCK");
Packit ae235b
      if (channel->last_events & FD_WRITE)
Packit ae235b
	{
Packit ae235b
	  if (channel->debug)
Packit ae235b
	    g_print (" sock=%d event=%p last_events has FD_WRITE",
Packit ae235b
		     channel->fd, (HANDLE) watch->pollfd.fd);
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  WSAEnumNetworkEvents (channel->fd, 0, &events);
Packit ae235b
Packit ae235b
	  if (channel->debug)
Packit ae235b
	    g_print ("\n  revents={%s} condition={%s}"
Packit ae235b
		     "\n  WSAEnumNetworkEvents(%d,0) sets events={%s}",
Packit ae235b
		     condition_to_string (watch->pollfd.revents),
Packit ae235b
		     condition_to_string (watch->condition),
Packit ae235b
		     channel->fd, 
Packit ae235b
		     event_mask_to_string (events.lNetworkEvents));
Packit ae235b
	  
Packit ae235b
	  if (watch->pollfd.revents != 0 &&
Packit ae235b
	      events.lNetworkEvents == 0 &&
Packit ae235b
	      !(channel->event_mask & FD_WRITE))
Packit ae235b
	    {
Packit ae235b
	      channel->event_mask = 0;
Packit ae235b
	      if (channel->debug)
Packit ae235b
		g_print ("\n  WSAEventSelect(%d,%p,{})",
Packit ae235b
			 channel->fd, (HANDLE) watch->pollfd.fd);
Packit ae235b
	      WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd, 0);
Packit ae235b
	      if (channel->debug)
Packit ae235b
		g_print ("  ResetEvent(%p)",
Packit ae235b
			 (HANDLE) watch->pollfd.fd);
Packit ae235b
	      ResetEvent ((HANDLE) watch->pollfd.fd);
Packit ae235b
	    }
Packit ae235b
	  else if (events.lNetworkEvents & FD_WRITE)
Packit ae235b
	    channel->ever_writable = TRUE;
Packit ae235b
	  channel->last_events = events.lNetworkEvents;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      watch->pollfd.revents = 0;
Packit ae235b
      if (channel->last_events & (FD_READ | FD_ACCEPT))
Packit ae235b
	watch->pollfd.revents |= G_IO_IN;
Packit ae235b
Packit ae235b
      if (channel->last_events & FD_WRITE)
Packit ae235b
	watch->pollfd.revents |= G_IO_OUT;
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  /* We have called WSAEnumNetworkEvents() above but it didn't
Packit ae235b
	   * set FD_WRITE.
Packit ae235b
	   */
Packit ae235b
	  if (events.lNetworkEvents & FD_CONNECT)
Packit ae235b
	    {
Packit ae235b
	      if (events.iErrorCode[FD_CONNECT_BIT] == 0)
Packit ae235b
		watch->pollfd.revents |= G_IO_OUT;
Packit ae235b
	      else
Packit ae235b
		watch->pollfd.revents |= (G_IO_HUP | G_IO_ERR);
Packit ae235b
	    }
Packit ae235b
	  if (watch->pollfd.revents == 0 && (channel->last_events & (FD_CLOSE)))
Packit ae235b
	    watch->pollfd.revents |= G_IO_HUP;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      /* Regardless of WSAEnumNetworkEvents() result, if watching for
Packit ae235b
       * writability, and if we have ever got a FD_WRITE event, and
Packit ae235b
       * unless last write would have blocked, set G_IO_OUT. But never
Packit ae235b
       * set both G_IO_OUT and G_IO_HUP.
Packit ae235b
       */
Packit ae235b
      if (!(watch->pollfd.revents & G_IO_HUP) &&
Packit ae235b
	  channel->ever_writable &&
Packit ae235b
	  !channel->write_would_have_blocked &&
Packit ae235b
	  (channel->event_mask & FD_WRITE))
Packit ae235b
	watch->pollfd.revents |= G_IO_OUT;
Packit ae235b
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print ("\n  revents={%s} retval={%s}\n",
Packit ae235b
		 condition_to_string (watch->pollfd.revents),
Packit ae235b
		 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
Packit ae235b
Packit ae235b
      return ((watch->pollfd.revents | buffer_condition) & watch->condition);
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
      g_abort ();
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_io_win32_dispatch (GSource     *source,
Packit ae235b
		     GSourceFunc  callback,
Packit ae235b
		     gpointer     user_data)
Packit ae235b
{
Packit ae235b
  GIOFunc func = (GIOFunc)callback;
Packit ae235b
  GIOWin32Watch *watch = (GIOWin32Watch *)source;
Packit ae235b
  GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
Packit ae235b
  GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
Packit ae235b
  
Packit ae235b
  if (!func)
Packit ae235b
    {
Packit ae235b
      g_warning ("IO Watch dispatched without callback\n"
Packit ae235b
		 "You must call g_source_connect().");
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (channel->debug)
Packit ae235b
    g_print ("g_io_win32_dispatch: pollfd.revents=%s condition=%s result=%s\n",
Packit ae235b
	     condition_to_string (watch->pollfd.revents),
Packit ae235b
	     condition_to_string (watch->condition),
Packit ae235b
	     condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
Packit ae235b
Packit ae235b
  return (*func) (watch->channel,
Packit ae235b
		  (watch->pollfd.revents | buffer_condition) & watch->condition,
Packit ae235b
		  user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_io_win32_finalize (GSource *source)
Packit ae235b
{
Packit ae235b
  GIOWin32Watch *watch = (GIOWin32Watch *)source;
Packit ae235b
  GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
Packit ae235b
  
Packit ae235b
  if (channel->debug)
Packit ae235b
    g_print ("g_io_win32_finalize: source=%p channel=%p", source, channel);
Packit ae235b
Packit ae235b
  switch (channel->type)
Packit ae235b
    {
Packit ae235b
    case G_IO_WIN32_WINDOWS_MESSAGES:
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print (" MSG");
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case G_IO_WIN32_CONSOLE:
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print (" CON");
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case G_IO_WIN32_FILE_DESC:
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print (" FD thread=%#x", channel->thread_id);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case G_IO_WIN32_SOCKET:
Packit ae235b
      if (channel->debug)
Packit ae235b
	g_print (" SOCK sock=%d", channel->fd);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
      g_abort ();
Packit ae235b
    }
Packit ae235b
  if (channel->debug)
Packit ae235b
    g_print ("\n");
Packit ae235b
  g_io_channel_unref (watch->channel);
Packit ae235b
}
Packit ae235b
Packit ae235b
GSourceFuncs g_io_watch_funcs = {
Packit ae235b
  g_io_win32_prepare,
Packit ae235b
  g_io_win32_check,
Packit ae235b
  g_io_win32_dispatch,
Packit ae235b
  g_io_win32_finalize
Packit ae235b
};
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_win32_msg_read (GIOChannel *channel,
Packit ae235b
		     gchar      *buf,
Packit ae235b
		     gsize       count,
Packit ae235b
		     gsize      *bytes_read,
Packit ae235b
		     GError    **err)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
  MSG msg;               /* In case of alignment problems */
Packit ae235b
Packit ae235b
  *bytes_read = 0;
Packit ae235b
Packit ae235b
  if (count < sizeof (MSG))
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
Packit ae235b
                           "Incorrect message size"); /* Informative enough error message? */
Packit ae235b
      return G_IO_STATUS_ERROR;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    g_print ("g_io_win32_msg_read: channel=%p hwnd=%p\n",
Packit ae235b
	     channel, win32_channel->hwnd);
Packit ae235b
  if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
Packit ae235b
    return G_IO_STATUS_AGAIN;
Packit ae235b
Packit ae235b
  memmove (buf, &msg, sizeof (MSG));
Packit ae235b
  *bytes_read = sizeof (MSG);
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_win32_msg_write (GIOChannel  *channel,
Packit ae235b
		      const gchar *buf,
Packit ae235b
		      gsize        count,
Packit ae235b
		      gsize       *bytes_written,
Packit ae235b
		      GError     **err)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
  MSG msg;
Packit ae235b
Packit ae235b
  *bytes_written = 0;
Packit ae235b
Packit ae235b
  if (count != sizeof (MSG))
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
Packit ae235b
                           "Incorrect message size"); /* Informative enough error message? */
Packit ae235b
      return G_IO_STATUS_ERROR;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  /* In case of alignment problems */
Packit ae235b
  memmove (&msg, buf, sizeof (MSG));
Packit ae235b
  if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
Packit ae235b
    {
Packit ae235b
      gchar *emsg = g_win32_error_message (GetLastError ());
Packit ae235b
Packit ae235b
      g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, emsg);
Packit ae235b
      g_free (emsg);
Packit ae235b
Packit ae235b
      return G_IO_STATUS_ERROR;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  *bytes_written = sizeof (MSG);
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_win32_msg_close (GIOChannel *channel,
Packit ae235b
		      GError    **err)
Packit ae235b
{
Packit ae235b
  /* Nothing to be done. Or should we set hwnd to some invalid value? */
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_io_win32_free (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
  
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    g_print ("g_io_win32_free channel=%p fd=%d\n", channel, win32_channel->fd);
Packit ae235b
Packit ae235b
  DeleteCriticalSection (&win32_channel->mutex);
Packit ae235b
Packit ae235b
  if (win32_channel->data_avail_event)
Packit ae235b
    if (!CloseHandle (win32_channel->data_avail_event))
Packit ae235b
      if (win32_channel->debug)
Packit ae235b
	{
Packit ae235b
	  gchar *emsg = g_win32_error_message (GetLastError ());
Packit ae235b
Packit ae235b
	  g_print ("  CloseHandle(%p) failed: %s\n",
Packit ae235b
		   win32_channel->data_avail_event, emsg);
Packit ae235b
	  g_free (emsg);
Packit ae235b
	}
Packit ae235b
Packit ae235b
  g_free (win32_channel->buffer);
Packit ae235b
Packit ae235b
  if (win32_channel->space_avail_event)
Packit ae235b
    if (!CloseHandle (win32_channel->space_avail_event))
Packit ae235b
      if (win32_channel->debug)
Packit ae235b
	{
Packit ae235b
	  gchar *emsg = g_win32_error_message (GetLastError ());
Packit ae235b
Packit ae235b
	  g_print ("  CloseHandle(%p) failed: %s\n",
Packit ae235b
		   win32_channel->space_avail_event, emsg);
Packit ae235b
	  g_free (emsg);
Packit ae235b
	}
Packit ae235b
Packit ae235b
  if (win32_channel->type == G_IO_WIN32_SOCKET &&
Packit ae235b
      win32_channel->fd != -1)
Packit ae235b
    if (WSAEventSelect (win32_channel->fd, NULL, 0) == SOCKET_ERROR)
Packit ae235b
      if (win32_channel->debug)
Packit ae235b
	{
Packit ae235b
	  gchar *emsg = g_win32_error_message (WSAGetLastError ());
Packit ae235b
Packit ae235b
	  g_print ("  WSAEventSelect(%d,NULL,{}) failed: %s\n",
Packit ae235b
		   win32_channel->fd, emsg);
Packit ae235b
	  g_free (emsg);
Packit ae235b
	}
Packit ae235b
Packit ae235b
  if (win32_channel->event)
Packit ae235b
    if (!WSACloseEvent (win32_channel->event))
Packit ae235b
      if (win32_channel->debug)
Packit ae235b
	{
Packit ae235b
	  gchar *emsg = g_win32_error_message (WSAGetLastError ());
Packit ae235b
Packit ae235b
	  g_print ("  WSACloseEvent(%p) failed: %s\n",
Packit ae235b
		   win32_channel->event, emsg);
Packit ae235b
	  g_free (emsg);
Packit ae235b
	}
Packit ae235b
Packit ae235b
  g_free (win32_channel);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GSource *
Packit ae235b
g_io_win32_msg_create_watch (GIOChannel   *channel,
Packit ae235b
			     GIOCondition  condition)
Packit ae235b
{
Packit ae235b
  GIOWin32Watch *watch;
Packit ae235b
  GSource *source;
Packit ae235b
Packit ae235b
  source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
Packit ae235b
  g_source_set_name (source, "GIOChannel (Win32)");
Packit ae235b
  watch = (GIOWin32Watch *)source;
Packit ae235b
  
Packit ae235b
  watch->channel = channel;
Packit ae235b
  g_io_channel_ref (channel);
Packit ae235b
  
Packit ae235b
  watch->condition = condition;
Packit ae235b
  
Packit ae235b
  watch->pollfd.fd = (gintptr) G_WIN32_MSG_HANDLE;
Packit ae235b
  watch->pollfd.events = condition;
Packit ae235b
  
Packit ae235b
  g_source_add_poll (source, &watch->pollfd);
Packit ae235b
  
Packit ae235b
  return source;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_win32_fd_and_console_read (GIOChannel *channel,
Packit ae235b
				gchar      *buf,
Packit ae235b
				gsize       count,
Packit ae235b
				gsize      *bytes_read,
Packit ae235b
				GError    **err)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
  gint result;
Packit ae235b
  int errsv;
Packit ae235b
  
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    g_print ("g_io_win32_fd_read: fd=%d count=%" G_GSIZE_FORMAT "\n",
Packit ae235b
	     win32_channel->fd, count);
Packit ae235b
  
Packit ae235b
  if (win32_channel->thread_id)
Packit ae235b
    {
Packit ae235b
      return buffer_read (win32_channel, buf, count, bytes_read, err);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  result = read (win32_channel->fd, buf, count);
Packit ae235b
  errsv = errno;
Packit ae235b
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    g_print ("g_io_win32_fd_read: read() => %d\n", result);
Packit ae235b
Packit ae235b
  if (result < 0)
Packit ae235b
    {
Packit ae235b
      *bytes_read = 0;
Packit ae235b
Packit ae235b
      switch (errsv)
Packit ae235b
        {
Packit ae235b
#ifdef EAGAIN
Packit ae235b
	case EAGAIN:
Packit ae235b
	  return G_IO_STATUS_AGAIN;
Packit ae235b
#endif
Packit ae235b
	default:
Packit ae235b
	  g_set_error_literal (err, G_IO_CHANNEL_ERROR,
Packit ae235b
                               g_io_channel_error_from_errno (errsv),
Packit ae235b
                               g_strerror (errsv));
Packit ae235b
	  return G_IO_STATUS_ERROR;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  *bytes_read = result;
Packit ae235b
Packit ae235b
  return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_win32_fd_and_console_write (GIOChannel  *channel,
Packit ae235b
				 const gchar *buf,
Packit ae235b
				 gsize        count,
Packit ae235b
				 gsize       *bytes_written,
Packit ae235b
				 GError     **err)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
  gint result;
Packit ae235b
  int errsv;
Packit ae235b
Packit ae235b
  if (win32_channel->thread_id)
Packit ae235b
    {
Packit ae235b
      return buffer_write (win32_channel, buf, count, bytes_written, err);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  result = write (win32_channel->fd, buf, count);
Packit ae235b
  errsv = errno;
Packit ae235b
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    g_print ("g_io_win32_fd_write: fd=%d count=%" G_GSIZE_FORMAT " => %d\n",
Packit ae235b
	     win32_channel->fd, count, result);
Packit ae235b
Packit ae235b
  if (result < 0)
Packit ae235b
    {
Packit ae235b
      *bytes_written = 0;
Packit ae235b
Packit ae235b
      switch (errsv)
Packit ae235b
        {
Packit ae235b
#ifdef EAGAIN
Packit ae235b
	case EAGAIN:
Packit ae235b
	  return G_IO_STATUS_AGAIN;
Packit ae235b
#endif
Packit ae235b
	default:
Packit ae235b
	  g_set_error_literal (err, G_IO_CHANNEL_ERROR,
Packit ae235b
                               g_io_channel_error_from_errno (errsv),
Packit ae235b
                               g_strerror (errsv));
Packit ae235b
	  return G_IO_STATUS_ERROR;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  *bytes_written = result;
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_win32_fd_seek (GIOChannel *channel,
Packit ae235b
		    gint64      offset,
Packit ae235b
		    GSeekType   type,
Packit ae235b
		    GError    **err)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
  int whence, errsv;
Packit ae235b
  off_t tmp_offset;
Packit ae235b
  off_t result;
Packit ae235b
  
Packit ae235b
  switch (type)
Packit ae235b
    {
Packit ae235b
    case G_SEEK_SET:
Packit ae235b
      whence = SEEK_SET;
Packit ae235b
      break;
Packit ae235b
    case G_SEEK_CUR:
Packit ae235b
      whence = SEEK_CUR;
Packit ae235b
      break;
Packit ae235b
    case G_SEEK_END:
Packit ae235b
      whence = SEEK_END;
Packit ae235b
      break;
Packit ae235b
    default:
Packit ae235b
      whence = -1; /* Keep the compiler quiet */
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
      g_abort ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  tmp_offset = offset;
Packit ae235b
  if (tmp_offset != offset)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (err, G_IO_CHANNEL_ERROR,
Packit ae235b
                           g_io_channel_error_from_errno (EINVAL),
Packit ae235b
                           g_strerror (EINVAL));
Packit ae235b
      return G_IO_STATUS_ERROR;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  result = lseek (win32_channel->fd, tmp_offset, whence);
Packit ae235b
  errsv = errno;
Packit ae235b
  
Packit ae235b
  if (result < 0)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (err, G_IO_CHANNEL_ERROR,
Packit ae235b
                           g_io_channel_error_from_errno (errsv),
Packit ae235b
                           g_strerror (errsv));
Packit ae235b
      return G_IO_STATUS_ERROR;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_win32_fd_close (GIOChannel *channel,
Packit ae235b
	             GError    **err)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
  
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    g_print ("g_io_win32_fd_close: thread=%#x: fd=%d\n",
Packit ae235b
	     win32_channel->thread_id,
Packit ae235b
	     win32_channel->fd);
Packit ae235b
  EnterCriticalSection (&win32_channel->mutex);
Packit ae235b
  if (win32_channel->running)
Packit ae235b
    {
Packit ae235b
      if (win32_channel->debug)
Packit ae235b
	g_print ("thread %#x: running, marking fd %d for later close\n",
Packit ae235b
		 win32_channel->thread_id, win32_channel->fd);
Packit ae235b
      win32_channel->running = FALSE;
Packit ae235b
      win32_channel->needs_close = TRUE;
Packit ae235b
      if (win32_channel->direction == 0)
Packit ae235b
	SetEvent (win32_channel->data_avail_event);
Packit ae235b
      else
Packit ae235b
	SetEvent (win32_channel->space_avail_event);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (win32_channel->debug)
Packit ae235b
	g_print ("closing fd %d\n", win32_channel->fd);
Packit ae235b
      close (win32_channel->fd);
Packit ae235b
      if (win32_channel->debug)
Packit ae235b
	g_print ("closed fd %d, setting to -1\n",
Packit ae235b
		 win32_channel->fd);
Packit ae235b
      win32_channel->fd = -1;
Packit ae235b
    }
Packit ae235b
  LeaveCriticalSection (&win32_channel->mutex);
Packit ae235b
Packit ae235b
  /* FIXME error detection? */
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GSource *
Packit ae235b
g_io_win32_fd_create_watch (GIOChannel    *channel,
Packit ae235b
			    GIOCondition   condition)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
  GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
Packit ae235b
  GIOWin32Watch *watch = (GIOWin32Watch *)source;
Packit ae235b
Packit ae235b
  watch->channel = channel;
Packit ae235b
  g_io_channel_ref (channel);
Packit ae235b
  
Packit ae235b
  watch->condition = condition;
Packit ae235b
  
Packit ae235b
  if (win32_channel->data_avail_event == NULL)
Packit ae235b
    create_events (win32_channel);
Packit ae235b
Packit ae235b
  watch->pollfd.fd = (gintptr) win32_channel->data_avail_event;
Packit ae235b
  watch->pollfd.events = condition;
Packit ae235b
  
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    g_print ("g_io_win32_fd_create_watch: channel=%p fd=%d condition={%s} event=%p\n",
Packit ae235b
	     channel, win32_channel->fd,
Packit ae235b
	     condition_to_string (condition), (HANDLE) watch->pollfd.fd);
Packit ae235b
Packit ae235b
  EnterCriticalSection (&win32_channel->mutex);
Packit ae235b
  if (win32_channel->thread_id == 0)
Packit ae235b
    {
Packit ae235b
      if (condition & G_IO_IN)
Packit ae235b
	create_thread (win32_channel, condition, read_thread);
Packit ae235b
      else if (condition & G_IO_OUT)
Packit ae235b
	create_thread (win32_channel, condition, write_thread);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_source_add_poll (source, &watch->pollfd);
Packit ae235b
  LeaveCriticalSection (&win32_channel->mutex);
Packit ae235b
Packit ae235b
  return source;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_win32_console_close (GIOChannel *channel,
Packit ae235b
		          GError    **err)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
  
Packit ae235b
  if (close (win32_channel->fd) < 0)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
      g_set_error_literal (err, G_IO_CHANNEL_ERROR,
Packit ae235b
                           g_io_channel_error_from_errno (errsv),
Packit ae235b
                           g_strerror (errsv));
Packit ae235b
      return G_IO_STATUS_ERROR;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GSource *
Packit ae235b
g_io_win32_console_create_watch (GIOChannel    *channel,
Packit ae235b
				 GIOCondition   condition)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
  GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
Packit ae235b
  GIOWin32Watch *watch = (GIOWin32Watch *)source;
Packit ae235b
Packit ae235b
  watch->channel = channel;
Packit ae235b
  g_io_channel_ref (channel);
Packit ae235b
  
Packit ae235b
  watch->condition = condition;
Packit ae235b
  
Packit ae235b
  watch->pollfd.fd = _get_osfhandle (win32_channel->fd);
Packit ae235b
  watch->pollfd.events = condition;
Packit ae235b
  
Packit ae235b
  g_source_add_poll (source, &watch->pollfd);
Packit ae235b
Packit ae235b
  return source;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_win32_sock_read (GIOChannel *channel,
Packit ae235b
		      gchar      *buf,
Packit ae235b
		      gsize       count,
Packit ae235b
		      gsize      *bytes_read,
Packit ae235b
		      GError    **err)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
  gint result;
Packit ae235b
  GIOChannelError error;
Packit ae235b
  int winsock_error;
Packit ae235b
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    g_print ("g_io_win32_sock_read: channel=%p sock=%d count=%" G_GSIZE_FORMAT,
Packit ae235b
	     channel, win32_channel->fd, count);
Packit ae235b
Packit ae235b
  result = recv (win32_channel->fd, buf, count, 0);
Packit ae235b
  if (result == SOCKET_ERROR)
Packit ae235b
    winsock_error = WSAGetLastError ();
Packit ae235b
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    g_print (" recv=%d", result);
Packit ae235b
  
Packit ae235b
  if (result == SOCKET_ERROR)
Packit ae235b
    {
Packit ae235b
      gchar *emsg = g_win32_error_message (winsock_error);
Packit ae235b
Packit ae235b
      if (win32_channel->debug)
Packit ae235b
	g_print (" %s\n", emsg);
Packit ae235b
Packit ae235b
      *bytes_read = 0;
Packit ae235b
Packit ae235b
      switch (winsock_error)
Packit ae235b
	{
Packit ae235b
	case WSAEINVAL:
Packit ae235b
          error = G_IO_CHANNEL_ERROR_INVAL;
Packit ae235b
          break;
Packit ae235b
	case WSAEWOULDBLOCK:
Packit ae235b
	  g_free (emsg);
Packit ae235b
          return G_IO_STATUS_AGAIN;
Packit ae235b
	default:
Packit ae235b
	  error = G_IO_CHANNEL_ERROR_FAILED;
Packit ae235b
          break;
Packit ae235b
	}
Packit ae235b
      g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
Packit ae235b
      g_free (emsg);
Packit ae235b
Packit ae235b
      return G_IO_STATUS_ERROR;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (win32_channel->debug)
Packit ae235b
	g_print ("\n");
Packit ae235b
      *bytes_read = result;
Packit ae235b
      if (result == 0)
Packit ae235b
	return G_IO_STATUS_EOF;
Packit ae235b
      else
Packit ae235b
	return G_IO_STATUS_NORMAL;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_win32_sock_write (GIOChannel  *channel,
Packit ae235b
		       const gchar *buf,
Packit ae235b
		       gsize        count,
Packit ae235b
		       gsize       *bytes_written,
Packit ae235b
		       GError     **err)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
  gint result;
Packit ae235b
  GIOChannelError error;
Packit ae235b
  int winsock_error;
Packit ae235b
  
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    g_print ("g_io_win32_sock_write: channel=%p sock=%d count=%" G_GSIZE_FORMAT,
Packit ae235b
	     channel, win32_channel->fd, count);
Packit ae235b
Packit ae235b
  result = send (win32_channel->fd, buf, count, 0);
Packit ae235b
  if (result == SOCKET_ERROR)
Packit ae235b
    winsock_error = WSAGetLastError ();
Packit ae235b
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    g_print (" send=%d", result);
Packit ae235b
  
Packit ae235b
  if (result == SOCKET_ERROR)
Packit ae235b
    {
Packit ae235b
      gchar *emsg = g_win32_error_message (winsock_error);
Packit ae235b
Packit ae235b
      if (win32_channel->debug)
Packit ae235b
	g_print (" %s\n", emsg);
Packit ae235b
Packit ae235b
      *bytes_written = 0;
Packit ae235b
Packit ae235b
      switch (winsock_error)
Packit ae235b
	{
Packit ae235b
	case WSAEINVAL:
Packit ae235b
	  error = G_IO_CHANNEL_ERROR_INVAL;
Packit ae235b
          break;
Packit ae235b
	case WSAEWOULDBLOCK:
Packit ae235b
	  win32_channel->write_would_have_blocked = TRUE;
Packit ae235b
	  win32_channel->last_events = 0;
Packit ae235b
	  g_free (emsg);
Packit ae235b
          return G_IO_STATUS_AGAIN;
Packit ae235b
	default:
Packit ae235b
	  error = G_IO_CHANNEL_ERROR_FAILED;
Packit ae235b
          break;
Packit ae235b
	}
Packit ae235b
      g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
Packit ae235b
      g_free (emsg);
Packit ae235b
Packit ae235b
      return G_IO_STATUS_ERROR;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (win32_channel->debug)
Packit ae235b
	g_print ("\n");
Packit ae235b
      *bytes_written = result;
Packit ae235b
      win32_channel->write_would_have_blocked = FALSE;
Packit ae235b
Packit ae235b
      return G_IO_STATUS_NORMAL;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_win32_sock_close (GIOChannel *channel,
Packit ae235b
		       GError    **err)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
Packit ae235b
  if (win32_channel->fd != -1)
Packit ae235b
    {
Packit ae235b
      if (win32_channel->debug)
Packit ae235b
	g_print ("g_io_win32_sock_close: channel=%p sock=%d\n",
Packit ae235b
		 channel, win32_channel->fd);
Packit ae235b
      
Packit ae235b
      closesocket (win32_channel->fd);
Packit ae235b
      win32_channel->fd = -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* FIXME error detection? */
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GSource *
Packit ae235b
g_io_win32_sock_create_watch (GIOChannel    *channel,
Packit ae235b
			      GIOCondition   condition)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
  GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
Packit ae235b
  GIOWin32Watch *watch = (GIOWin32Watch *)source;
Packit ae235b
  
Packit ae235b
  watch->channel = channel;
Packit ae235b
  g_io_channel_ref (channel);
Packit ae235b
  
Packit ae235b
  watch->condition = condition;
Packit ae235b
Packit ae235b
  if (win32_channel->event == 0)
Packit ae235b
    win32_channel->event = WSACreateEvent ();
Packit ae235b
Packit ae235b
  watch->pollfd.fd = (gintptr) win32_channel->event;
Packit ae235b
  watch->pollfd.events = condition;
Packit ae235b
  
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    g_print ("g_io_win32_sock_create_watch: channel=%p sock=%d event=%p condition={%s}\n",
Packit ae235b
	     channel, win32_channel->fd, (HANDLE) watch->pollfd.fd,
Packit ae235b
	     condition_to_string (watch->condition));
Packit ae235b
Packit ae235b
  g_source_add_poll (source, &watch->pollfd);
Packit ae235b
Packit ae235b
  return source;
Packit ae235b
}
Packit ae235b
Packit ae235b
GIOChannel *
Packit ae235b
g_io_channel_new_file (const gchar  *filename,
Packit ae235b
                       const gchar  *mode,
Packit ae235b
                       GError      **error)
Packit ae235b
{
Packit ae235b
  int fid, flags, pmode;
Packit ae235b
  GIOChannel *channel;
Packit ae235b
Packit ae235b
  enum { /* Cheesy hack */
Packit ae235b
    MODE_R = 1 << 0,
Packit ae235b
    MODE_W = 1 << 1,
Packit ae235b
    MODE_A = 1 << 2,
Packit ae235b
    MODE_PLUS = 1 << 3,
Packit ae235b
  };
Packit ae235b
  int mode_num, errsv;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (filename != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (mode != NULL, NULL);
Packit ae235b
  g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
Packit ae235b
Packit ae235b
  switch (mode[0])
Packit ae235b
    {
Packit ae235b
      case 'r':
Packit ae235b
        mode_num = MODE_R;
Packit ae235b
        break;
Packit ae235b
      case 'w':
Packit ae235b
        mode_num = MODE_W;
Packit ae235b
        break;
Packit ae235b
      case 'a':
Packit ae235b
        mode_num = MODE_A;
Packit ae235b
        break;
Packit ae235b
      default:
Packit ae235b
        g_warning ("Invalid GIOFileMode %s.", mode);
Packit ae235b
        return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  switch (mode[1])
Packit ae235b
    {
Packit ae235b
      case '\0':
Packit ae235b
        break;
Packit ae235b
      case '+':
Packit ae235b
        if (mode[2] == '\0')
Packit ae235b
          {
Packit ae235b
            mode_num |= MODE_PLUS;
Packit ae235b
            break;
Packit ae235b
          }
Packit ae235b
        /* Fall through */
Packit ae235b
      default:
Packit ae235b
        g_warning ("Invalid GIOFileMode %s.", mode);
Packit ae235b
        return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  switch (mode_num)
Packit ae235b
    {
Packit ae235b
      case MODE_R:
Packit ae235b
        flags = O_RDONLY;
Packit ae235b
        pmode = _S_IREAD;
Packit ae235b
        break;
Packit ae235b
      case MODE_W:
Packit ae235b
        flags = O_WRONLY | O_TRUNC | O_CREAT;
Packit ae235b
        pmode = _S_IWRITE;
Packit ae235b
        break;
Packit ae235b
      case MODE_A:
Packit ae235b
        flags = O_WRONLY | O_APPEND | O_CREAT;
Packit ae235b
        pmode = _S_IWRITE;
Packit ae235b
        break;
Packit ae235b
      case MODE_R | MODE_PLUS:
Packit ae235b
        flags = O_RDWR;
Packit ae235b
        pmode = _S_IREAD | _S_IWRITE;
Packit ae235b
        break;
Packit ae235b
      case MODE_W | MODE_PLUS:
Packit ae235b
        flags = O_RDWR | O_TRUNC | O_CREAT;
Packit ae235b
        pmode = _S_IREAD | _S_IWRITE;
Packit ae235b
        break;
Packit ae235b
      case MODE_A | MODE_PLUS:
Packit ae235b
        flags = O_RDWR | O_APPEND | O_CREAT;
Packit ae235b
        pmode = _S_IREAD | _S_IWRITE;
Packit ae235b
        break;
Packit ae235b
      default:
Packit ae235b
        g_assert_not_reached ();
Packit ae235b
        g_abort ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* always open 'untranslated' */
Packit ae235b
  fid = g_open (filename, flags | _O_BINARY, pmode);
Packit ae235b
  errsv = errno;
Packit ae235b
Packit ae235b
  if (g_io_win32_get_debug_flag ())
Packit ae235b
    {
Packit ae235b
      g_print ("g_io_channel_win32_new_file: open(\"%s\",", filename);
Packit ae235b
      g_win32_print_access_mode (flags|_O_BINARY);
Packit ae235b
      g_print (",%#o)=%d\n", pmode, fid);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (fid < 0)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_FILE_ERROR,
Packit ae235b
                           g_file_error_from_errno (errsv),
Packit ae235b
                           g_strerror (errsv));
Packit ae235b
      return (GIOChannel *)NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  channel = g_io_channel_win32_new_fd (fid);
Packit ae235b
Packit ae235b
  /* XXX: move this to g_io_channel_win32_new_fd () */
Packit ae235b
  channel->close_on_unref = TRUE;
Packit ae235b
  channel->is_seekable = TRUE;
Packit ae235b
Packit ae235b
  /* g_io_channel_win32_new_fd sets is_readable and is_writeable to
Packit ae235b
   * correspond to actual readability/writeability. Set to FALSE those
Packit ae235b
   * that mode doesn't allow
Packit ae235b
   */
Packit ae235b
  switch (mode_num)
Packit ae235b
    {
Packit ae235b
      case MODE_R:
Packit ae235b
        channel->is_writeable = FALSE;
Packit ae235b
        break;
Packit ae235b
      case MODE_W:
Packit ae235b
      case MODE_A:
Packit ae235b
        channel->is_readable = FALSE;
Packit ae235b
        break;
Packit ae235b
      case MODE_R | MODE_PLUS:
Packit ae235b
      case MODE_W | MODE_PLUS:
Packit ae235b
      case MODE_A | MODE_PLUS:
Packit ae235b
        break;
Packit ae235b
      default:
Packit ae235b
        g_assert_not_reached ();
Packit ae235b
        g_abort ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return channel;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_win32_unimpl_set_flags (GIOChannel *channel,
Packit ae235b
			     GIOFlags    flags,
Packit ae235b
			     GError    **err)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    {
Packit ae235b
      g_print ("g_io_win32_unimpl_set_flags: ");
Packit ae235b
      g_win32_print_gioflags (flags);
Packit ae235b
      g_print ("\n");
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_set_error_literal (err, G_IO_CHANNEL_ERROR,
Packit ae235b
                       G_IO_CHANNEL_ERROR_FAILED,
Packit ae235b
                       "Not implemented on Win32");
Packit ae235b
Packit ae235b
  return G_IO_STATUS_ERROR;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOFlags
Packit ae235b
g_io_win32_fd_get_flags_internal (GIOChannel      *channel,
Packit ae235b
				  struct _stati64 *st)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
Packit ae235b
  gchar c;
Packit ae235b
  DWORD count;
Packit ae235b
Packit ae235b
  if (st->st_mode & _S_IFIFO)
Packit ae235b
    {
Packit ae235b
      channel->is_readable =
Packit ae235b
	(PeekNamedPipe ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL, NULL) != 0) || GetLastError () == ERROR_BROKEN_PIPE;
Packit ae235b
      channel->is_writeable =
Packit ae235b
	(WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
Packit ae235b
      channel->is_seekable  = FALSE;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      channel->is_readable =
Packit ae235b
	(ReadFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
Packit ae235b
      channel->is_writeable =
Packit ae235b
	(WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
Packit ae235b
      channel->is_seekable = TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* XXX: G_IO_FLAG_APPEND */
Packit ae235b
  /* XXX: G_IO_FLAG_NONBLOCK */
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOFlags
Packit ae235b
g_io_win32_fd_get_flags (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  struct _stati64 st;
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (win32_channel != NULL, 0);
Packit ae235b
  g_return_val_if_fail (win32_channel->type == G_IO_WIN32_FILE_DESC, 0);
Packit ae235b
Packit ae235b
  if (0 == _fstati64 (win32_channel->fd, &st))
Packit ae235b
    return g_io_win32_fd_get_flags_internal (channel, &st);
Packit ae235b
  else
Packit ae235b
    return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOFlags
Packit ae235b
g_io_win32_console_get_flags_internal (GIOChannel  *channel)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
Packit ae235b
  HANDLE handle = (HANDLE) _get_osfhandle (win32_channel->fd);
Packit ae235b
  gchar c;
Packit ae235b
  DWORD count;
Packit ae235b
  INPUT_RECORD record;
Packit ae235b
Packit ae235b
  channel->is_readable = PeekConsoleInput (handle, &record, 1, &count);
Packit ae235b
  channel->is_writeable = WriteFile (handle, &c, 0, &count, NULL);
Packit ae235b
  channel->is_seekable = FALSE;
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOFlags
Packit ae235b
g_io_win32_console_get_flags (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (win32_channel != NULL, 0);
Packit ae235b
  g_return_val_if_fail (win32_channel->type == G_IO_WIN32_CONSOLE, 0);
Packit ae235b
Packit ae235b
  return g_io_win32_console_get_flags_internal (channel);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOFlags
Packit ae235b
g_io_win32_msg_get_flags (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_win32_sock_set_flags (GIOChannel *channel,
Packit ae235b
			   GIOFlags    flags,
Packit ae235b
			   GError    **err)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
  u_long arg;
Packit ae235b
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    {
Packit ae235b
      g_print ("g_io_win32_sock_set_flags: ");
Packit ae235b
      g_win32_print_gioflags (flags);
Packit ae235b
      g_print ("\n");
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (flags & G_IO_FLAG_NONBLOCK)
Packit ae235b
    {
Packit ae235b
      arg = 1;
Packit ae235b
      if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
Packit ae235b
	{
Packit ae235b
	  gchar *emsg = g_win32_error_message (WSAGetLastError ());
Packit ae235b
Packit ae235b
	  g_set_error_literal (err, G_IO_CHANNEL_ERROR,
Packit ae235b
			       G_IO_CHANNEL_ERROR_FAILED,
Packit ae235b
			       emsg);
Packit ae235b
	  g_free (emsg);
Packit ae235b
Packit ae235b
	  return G_IO_STATUS_ERROR;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      arg = 0;
Packit ae235b
      if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
Packit ae235b
	{
Packit ae235b
	  gchar *emsg = g_win32_error_message (WSAGetLastError ());
Packit ae235b
Packit ae235b
	  g_set_error_literal (err, G_IO_CHANNEL_ERROR,
Packit ae235b
			       G_IO_CHANNEL_ERROR_FAILED,
Packit ae235b
			       emsg);
Packit ae235b
	  g_free (emsg);
Packit ae235b
Packit ae235b
	  return G_IO_STATUS_ERROR;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOFlags
Packit ae235b
g_io_win32_sock_get_flags (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  /* Could we do something here? */
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOFuncs win32_channel_msg_funcs = {
Packit ae235b
  g_io_win32_msg_read,
Packit ae235b
  g_io_win32_msg_write,
Packit ae235b
  NULL,
Packit ae235b
  g_io_win32_msg_close,
Packit ae235b
  g_io_win32_msg_create_watch,
Packit ae235b
  g_io_win32_free,
Packit ae235b
  g_io_win32_unimpl_set_flags,
Packit ae235b
  g_io_win32_msg_get_flags,
Packit ae235b
};
Packit ae235b
Packit ae235b
static GIOFuncs win32_channel_fd_funcs = {
Packit ae235b
  g_io_win32_fd_and_console_read,
Packit ae235b
  g_io_win32_fd_and_console_write,
Packit ae235b
  g_io_win32_fd_seek,
Packit ae235b
  g_io_win32_fd_close,
Packit ae235b
  g_io_win32_fd_create_watch,
Packit ae235b
  g_io_win32_free,
Packit ae235b
  g_io_win32_unimpl_set_flags,
Packit ae235b
  g_io_win32_fd_get_flags,
Packit ae235b
};
Packit ae235b
Packit ae235b
static GIOFuncs win32_channel_console_funcs = {
Packit ae235b
  g_io_win32_fd_and_console_read,
Packit ae235b
  g_io_win32_fd_and_console_write,
Packit ae235b
  NULL,
Packit ae235b
  g_io_win32_console_close,
Packit ae235b
  g_io_win32_console_create_watch,
Packit ae235b
  g_io_win32_free,
Packit ae235b
  g_io_win32_unimpl_set_flags,
Packit ae235b
  g_io_win32_console_get_flags,
Packit ae235b
};
Packit ae235b
Packit ae235b
static GIOFuncs win32_channel_sock_funcs = {
Packit ae235b
  g_io_win32_sock_read,
Packit ae235b
  g_io_win32_sock_write,
Packit ae235b
  NULL,
Packit ae235b
  g_io_win32_sock_close,
Packit ae235b
  g_io_win32_sock_create_watch,
Packit ae235b
  g_io_win32_free,
Packit ae235b
  g_io_win32_sock_set_flags,
Packit ae235b
  g_io_win32_sock_get_flags,
Packit ae235b
};
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_win32_new_messages:
Packit ae235b
 * @hwnd: a window handle.
Packit ae235b
 *
Packit ae235b
 * Creates a new #GIOChannel given a window handle on Windows.
Packit ae235b
 *
Packit ae235b
 * This function creates a #GIOChannel that can be used to poll for
Packit ae235b
 * Windows messages for the window in question.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GIOChannel.
Packit ae235b
 **/
Packit ae235b
GIOChannel *
Packit ae235b
#if GLIB_SIZEOF_VOID_P == 8
Packit ae235b
g_io_channel_win32_new_messages (gsize hwnd)
Packit ae235b
#else
Packit ae235b
g_io_channel_win32_new_messages (guint hwnd)
Packit ae235b
#endif
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
Packit ae235b
  GIOChannel *channel = (GIOChannel *)win32_channel;
Packit ae235b
Packit ae235b
  g_io_channel_init (channel);
Packit ae235b
  g_io_channel_win32_init (win32_channel);
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    g_print ("g_io_channel_win32_new_messages: channel=%p hwnd=%p\n",
Packit ae235b
	     channel, (HWND) hwnd);
Packit ae235b
  channel->funcs = &win32_channel_msg_funcs;
Packit ae235b
  win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
Packit ae235b
  win32_channel->hwnd = (HWND) hwnd;
Packit ae235b
Packit ae235b
  /* XXX: check this. */
Packit ae235b
  channel->is_readable = IsWindow (win32_channel->hwnd);
Packit ae235b
  channel->is_writeable = IsWindow (win32_channel->hwnd);
Packit ae235b
Packit ae235b
  channel->is_seekable = FALSE;
Packit ae235b
Packit ae235b
  return channel;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOChannel *
Packit ae235b
g_io_channel_win32_new_fd_internal (gint             fd,
Packit ae235b
				    struct _stati64 *st)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel;
Packit ae235b
  GIOChannel *channel;
Packit ae235b
Packit ae235b
  win32_channel = g_new (GIOWin32Channel, 1);
Packit ae235b
  channel = (GIOChannel *)win32_channel;
Packit ae235b
Packit ae235b
  g_io_channel_init (channel);
Packit ae235b
  g_io_channel_win32_init (win32_channel);
Packit ae235b
Packit ae235b
  win32_channel->fd = fd;
Packit ae235b
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    g_print ("g_io_channel_win32_new_fd: channel=%p fd=%u\n",
Packit ae235b
	     channel, fd);
Packit ae235b
Packit ae235b
  if (st->st_mode & _S_IFCHR) /* console */
Packit ae235b
    {
Packit ae235b
      channel->funcs = &win32_channel_console_funcs;
Packit ae235b
      win32_channel->type = G_IO_WIN32_CONSOLE;
Packit ae235b
      g_io_win32_console_get_flags_internal (channel);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      channel->funcs = &win32_channel_fd_funcs;
Packit ae235b
      win32_channel->type = G_IO_WIN32_FILE_DESC;
Packit ae235b
      g_io_win32_fd_get_flags_internal (channel, st);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return channel;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_win32_new_fd:
Packit ae235b
 * @fd: a C library file descriptor.
Packit ae235b
 *
Packit ae235b
 * Creates a new #GIOChannel given a file descriptor on Windows. This
Packit ae235b
 * works for file descriptors from the C runtime.
Packit ae235b
 *
Packit ae235b
 * This function works for file descriptors as returned by the open(),
Packit ae235b
 * creat(), pipe() and fileno() calls in the Microsoft C runtime. In
Packit ae235b
 * order to meaningfully use this function your code should use the
Packit ae235b
 * same C runtime as GLib uses, which is msvcrt.dll. Note that in
Packit ae235b
 * current Microsoft compilers it is near impossible to convince it to
Packit ae235b
 * build code that would use msvcrt.dll. The last Microsoft compiler
Packit ae235b
 * version that supported using msvcrt.dll as the C runtime was version 6.
Packit ae235b
 * The GNU compiler and toolchain for Windows, also known as Mingw,
Packit ae235b
 * fully supports msvcrt.dll.
Packit ae235b
 *
Packit ae235b
 * If you have created a #GIOChannel for a file descriptor and started
Packit ae235b
 * watching (polling) it, you shouldn't call read() on the file
Packit ae235b
 * descriptor. This is because adding polling for a file descriptor is
Packit ae235b
 * implemented in GLib on Windows by starting a thread that sits
Packit ae235b
 * blocked in a read() from the file descriptor most of the time. All
Packit ae235b
 * reads from the file descriptor should be done by this internal GLib
Packit ae235b
 * thread. Your code should call only g_io_channel_read().
Packit ae235b
 *
Packit ae235b
 * This function is available only in GLib on Windows.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GIOChannel.
Packit ae235b
 **/
Packit ae235b
GIOChannel *
Packit ae235b
g_io_channel_win32_new_fd (gint fd)
Packit ae235b
{
Packit ae235b
  struct _stati64 st;
Packit ae235b
Packit ae235b
  if (_fstati64 (fd, &st) == -1)
Packit ae235b
    {
Packit ae235b
      g_warning ("g_io_channel_win32_new_fd: %d isn't an open file descriptor in the C library GLib uses.", fd);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_io_channel_win32_new_fd_internal (fd, &st);
Packit ae235b
}
Packit ae235b
Packit ae235b
gint
Packit ae235b
g_io_channel_win32_get_fd (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
Packit ae235b
  return win32_channel->fd;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_win32_new_socket:
Packit ae235b
 * @socket: a Winsock socket
Packit ae235b
 *
Packit ae235b
 * Creates a new #GIOChannel given a socket on Windows.
Packit ae235b
 *
Packit ae235b
 * This function works for sockets created by Winsock. It's available
Packit ae235b
 * only in GLib on Windows.
Packit ae235b
 *
Packit ae235b
 * Polling a #GSource created to watch a channel for a socket puts the
Packit ae235b
 * socket in non-blocking mode. This is a side-effect of the
Packit ae235b
 * implementation and unavoidable.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GIOChannel
Packit ae235b
 **/
Packit ae235b
GIOChannel *
Packit ae235b
g_io_channel_win32_new_socket (int socket)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
Packit ae235b
  GIOChannel *channel = (GIOChannel *)win32_channel;
Packit ae235b
Packit ae235b
  g_io_channel_init (channel);
Packit ae235b
  g_io_channel_win32_init (win32_channel);
Packit ae235b
  if (win32_channel->debug)
Packit ae235b
    g_print ("g_io_channel_win32_new_socket: channel=%p sock=%d\n",
Packit ae235b
	     channel, socket);
Packit ae235b
  channel->funcs = &win32_channel_sock_funcs;
Packit ae235b
  win32_channel->type = G_IO_WIN32_SOCKET;
Packit ae235b
  win32_channel->fd = socket;
Packit ae235b
Packit ae235b
  channel->is_readable = TRUE;
Packit ae235b
  channel->is_writeable = TRUE;
Packit ae235b
  channel->is_seekable = FALSE;
Packit ae235b
Packit ae235b
  return channel;
Packit ae235b
}
Packit ae235b
Packit ae235b
GIOChannel *
Packit ae235b
g_io_channel_unix_new (gint fd)
Packit ae235b
{
Packit ae235b
  gboolean is_fd, is_socket;
Packit ae235b
  struct _stati64 st;
Packit ae235b
  int optval, optlen;
Packit ae235b
Packit ae235b
  is_fd = (_fstati64 (fd, &st) == 0);
Packit ae235b
Packit ae235b
  optlen = sizeof (optval);
Packit ae235b
  is_socket = (getsockopt (fd, SOL_SOCKET, SO_TYPE, (char *) &optval, &optlen) != SOCKET_ERROR);
Packit ae235b
Packit ae235b
  if (is_fd && is_socket)
Packit ae235b
    g_warning ("g_io_channel_unix_new: %d is both a file descriptor and a socket. File descriptor interpretation assumed. To avoid ambiguity, call either g_io_channel_win32_new_fd() or g_io_channel_win32_new_socket() instead.", fd);
Packit ae235b
Packit ae235b
  if (is_fd)
Packit ae235b
    return g_io_channel_win32_new_fd_internal (fd, &st);
Packit ae235b
Packit ae235b
  if (is_socket)
Packit ae235b
    return g_io_channel_win32_new_socket(fd);
Packit ae235b
Packit ae235b
  g_warning ("g_io_channel_unix_new: %d is neither a file descriptor or a socket.", fd);
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
gint
Packit ae235b
g_io_channel_unix_get_fd (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  return g_io_channel_win32_get_fd (channel);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_io_channel_win32_set_debug (GIOChannel *channel,
Packit ae235b
			      gboolean    flag)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
Packit ae235b
  win32_channel->debug = flag;
Packit ae235b
}
Packit ae235b
Packit ae235b
gint
Packit ae235b
g_io_channel_win32_poll (GPollFD *fds,
Packit ae235b
			 gint     n_fds,
Packit ae235b
			 gint     timeout)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (n_fds >= 0, 0);
Packit ae235b
Packit ae235b
  return g_poll (fds, n_fds, timeout);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_io_channel_win32_make_pollfd (GIOChannel   *channel,
Packit ae235b
				GIOCondition  condition,
Packit ae235b
				GPollFD      *fd)
Packit ae235b
{
Packit ae235b
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
Packit ae235b
Packit ae235b
  switch (win32_channel->type)
Packit ae235b
    {
Packit ae235b
    case G_IO_WIN32_FILE_DESC:
Packit ae235b
      if (win32_channel->data_avail_event == NULL)
Packit ae235b
	create_events (win32_channel);
Packit ae235b
Packit ae235b
      fd->fd = (gintptr) win32_channel->data_avail_event;
Packit ae235b
Packit ae235b
      if (win32_channel->thread_id == 0)
Packit ae235b
	{
Packit ae235b
	  /* Is it meaningful for a file descriptor to be polled for
Packit ae235b
	   * both IN and OUT? For what kind of file descriptor would
Packit ae235b
	   * that be? Doesn't seem to make sense, in practise the file
Packit ae235b
	   * descriptors handled here are always read or write ends of
Packit ae235b
	   * pipes surely, and thus unidirectional.
Packit ae235b
	   */
Packit ae235b
	  if (condition & G_IO_IN)
Packit ae235b
	    create_thread (win32_channel, condition, read_thread);
Packit ae235b
	  else if (condition & G_IO_OUT)
Packit ae235b
	    create_thread (win32_channel, condition, write_thread);
Packit ae235b
	}
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case G_IO_WIN32_CONSOLE:
Packit ae235b
      fd->fd = _get_osfhandle (win32_channel->fd);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case G_IO_WIN32_SOCKET:
Packit ae235b
      fd->fd = (gintptr) WSACreateEvent ();
Packit ae235b
      break;
Packit ae235b
      
Packit ae235b
    case G_IO_WIN32_WINDOWS_MESSAGES:
Packit ae235b
      fd->fd = G_WIN32_MSG_HANDLE;
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
      g_abort ();
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  fd->events = condition;
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifndef _WIN64
Packit ae235b
Packit ae235b
/* Binary compatibility */
Packit ae235b
GIOChannel *
Packit ae235b
g_io_channel_win32_new_stream_socket (int socket)
Packit ae235b
{
Packit ae235b
  return g_io_channel_win32_new_socket (socket);
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
Packit ae235b
/* Binary compatibility versions. Not for newly compiled code. */
Packit ae235b
Packit ae235b
_GLIB_EXTERN GIOChannel *g_io_channel_new_file_utf8 (const gchar  *filename,
Packit ae235b
                                                     const gchar  *mode,
Packit ae235b
                                                     GError      **error);
Packit ae235b
Packit ae235b
GIOChannel *
Packit ae235b
g_io_channel_new_file_utf8 (const gchar  *filename,
Packit ae235b
                            const gchar  *mode,
Packit ae235b
                            GError      **error)
Packit ae235b
{
Packit ae235b
  return g_io_channel_new_file (filename, mode, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif