/* vi: set sw=4 ts=4: */
/*
 * CCE - Console Chinese Environment -
 * Copyright (C) 1998-2003 Rui He (rhe@3eti.com)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

/* Code modified from gpm's src/lib/liblow.c 
   libgpm.so depends on libncurse.so, we just need a few very simple routines,
   so I put it here. We'll connect to gpm server via socket directly.
   In such case, CCE will only depends on C lib.

   You can also -lgpm to link libgpm.so without compiling this file
*/
/* 
 * liblow.c - client library - low level (gpm)
 *
 * Copyright 1994,1995   rubini@linux.it (Alessandro Rubini)
 * Copyright (C) 1998    Ian Zimmerman <itz@rahul.net>
 * Copyright 2001        Nico Schottelius (nico@schottelius.org)
 * 
 * xterm management is mostly by jtklehto@stekt.oulu.fi (Janne Kukonlehto)
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 ********/
#include "config.h"        /* SUPPORT_GPMMOUSE */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>        /* select(); */
#include <errno.h>

#if defined(SUPPORT_MOUSE) && defined(SUPPORT_GPMMOUSE) && defined(linux)

#include <sys/types.h>     /* socket() */
#include <sys/socket.h>    /* socket() */
#include <sys/un.h>        /* struct sockaddr_un */
#include <sys/fcntl.h>     /* O_RDONLY */

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>      /* stat() */
#endif

#include "gpm.h"

int gpm_fd=-1;
int gpm_zerobased=0;  /* necessary ? */

int Gpm_Open(Gpm_Connect *conn, int flag)
{
	int len;
	struct sockaddr_un addr;

	conn->pid=getpid(); /* fill obvious values */

    conn->vc = flag;    /* need to get the correct one!! */

	if ( (gpm_fd = socket(AF_UNIX,SOCK_STREAM,0)) < 0 ) 
		goto err;

	bzero((char *)&addr,sizeof(addr));
    addr.sun_family = AF_UNIX;
    strcpy(addr.sun_path, GPM_NODE_CTL);  /* /dev/gpmctl */
    len = sizeof(addr.sun_family)+strlen(GPM_NODE_CTL);

    if(connect(gpm_fd,(struct sockaddr *)(&addr),len) < 0 ) 
	{
    	struct stat stbuf;

        close(gpm_fd); /* the socket */
		if ((gpm_fd = open(GPM_NODE_DEV,O_RDWR))==-1) goto err;
        if (fstat(gpm_fd,&stbuf) == -1 || (stbuf.st_mode&S_IFMT) != S_IFCHR) 
            goto err;
	}
   
	if (write(gpm_fd, conn, sizeof(Gpm_Connect)) != sizeof(Gpm_Connect))
		goto err;

   	return gpm_fd;

err:
   if (gpm_fd>=0) close(gpm_fd);
   return -1;
}

int Gpm_Close(void)
{
	if (gpm_fd >= 0) close(gpm_fd);

  	gpm_fd=-1;
  	return 0;
}

int Gpm_GetEvent(Gpm_Event *event)
{
	int count;

	if ((count=read(gpm_fd,event,sizeof(Gpm_Event))) < 16) /* != sizeof(Gpm_Event)) */
 	{

    	if (count != -1 || errno != EAGAIN)
      		return -1;
    }

  	event->x -= gpm_zerobased;
  	event->y -= gpm_zerobased;

  	return 1;
}

#endif  /* SUPPORT_GPMMOUSE */
