(file) Return to xisb.c CVS log (file) (dir) Up to [XFree86 CVS] / xc / programs / Xserver / hw / xfree86 / common

  1 dawes 1.1 /*
  2            * Copyright (c) 1997  Metro Link Incorporated
  3            *
  4            * Permission is hereby granted, free of charge, to any person obtaining a
  5            * copy of this software and associated documentation files (the "Software"),
  6            * to deal in the Software without restriction, including without limitation
  7            * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8            * and/or sell copies of the Software, and to permit persons to whom the
  9            * Software is furnished to do so, subject to the following conditions:
 10            *
 11            * The above copyright notice and this permission notice shall be included in
 12            * all copies or substantial portions of the Software.
 13            *
 14            * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 15            * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 16            * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 17            * THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 18            * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 19            * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 20            * SOFTWARE.
 21            *
 22 dawes 1.1  * Except as contained in this notice, the name of the Metro Link shall not be
 23            * used in advertising or otherwise to promote the sale, use or other dealings
 24            * in this Software without prior written authorization from Metro Link.
 25            *
 26            */
 27 dawes 1.5 /* $XFree86: xc/programs/Xserver/hw/xfree86/common/xisb.c,v 1.4 1999/03/06 13:12:32 dawes Exp $ */
 28 dawes 1.1 
 29           /*
 30           	X Input Serial Buffer routines for use in any XInput driver that accesses
 31           	a serial device.
 32           */
 33           
 34           
 35           /*****************************************************************************
 36            *	Standard Headers
 37            ****************************************************************************/
 38           
 39           #include <misc.h>
 40           #include <xf86.h>
 41           #include <xf86Version.h>
 42           #include <xf86_OSproc.h>
 43 dawes 1.4 #include <xf86_OSlib.h>
 44 dawes 1.1 #include <xf86Xinput.h>
 45           #include "xisb.h"
 46           
 47           /*****************************************************************************
 48            *	Local Headers
 49            ****************************************************************************/
 50           
 51           /*****************************************************************************
 52            *	Variables without includable headers
 53            ****************************************************************************/
 54           
 55           /*****************************************************************************
 56            *	Local Variables
 57            ****************************************************************************/
 58           
 59           /*****************************************************************************
 60            *	Function Definitions
 61            ****************************************************************************/
 62           
 63           XISBuffer *
 64           XisbNew (int fd, xf86ssize_t size)
 65 dawes 1.1 {
 66           	XISBuffer *b;
 67           
 68 dawes 1.3 	b = xalloc (sizeof (XISBuffer));
 69 dawes 1.1 	if (!b)
 70           		return (NULL);
 71 dawes 1.3 	b->buf = xalloc ((sizeof (unsigned char) * size));
 72 dawes 1.1 	if (!b->buf)
 73           	{
 74           		xfree (b);
 75           		return (NULL);
 76           	}
 77           
 78           	b->fd = fd;
 79           	b->trace = 0;
 80           	b->block_duration = 0;
 81           	b->current = 1;	/* force it to be past the end to trigger initial read */
 82           	b->end = 0;
 83           	b->buffer_size = size;
 84           	return (b);
 85           }
 86           
 87           void
 88           XisbFree (XISBuffer *b)
 89           {
 90           	xfree (b->buf);
 91           	xfree (b);
 92           }
 93 dawes 1.1 
 94           int
 95           XisbRead (XISBuffer *b)
 96           {
 97           	int ret;
 98           
 99           	if (b->current >= b->end)
100           	{
101           		if (b->block_duration >= 0)
102           		{
103           			if (xf86WaitForInput (b->fd, b->block_duration) < 1)
104           				return (-1);
105           		}
106           		else
107           		{
108           			/*
109           			 * automatically clear it so if XisbRead is called in a loop
110           			 * the next call will make sure there is data with select and
111           			 * thus prevent a blocking read
112           			 */
113           			b->block_duration = 0;
114 dawes 1.1 		}
115           		
116           		ret = xf86ReadSerial (b->fd, b->buf, b->buffer_size);
117           		switch (ret)
118           		{
119           			case 0:
120           				return (-1); /* timeout */
121           			case -1:
122           				return (-2); /* error */
123           			default:
124           				b->end = ret;
125           				b->current = 0;
126           				break;
127           		}
128           	}
129           	if (b->trace)
130 dawes 1.5 		ErrorF ("read 0x%02x (%c)\n", b->buf[b->current], 
131           			isprint(b->buf[b->current])?b->buf[b->current]:'.');
132 dawes 1.1 
133           	return (b->buf[b->current++]);
134           }
135           
136           /* the only purpose of this function is to provide output tracing */
137           xf86ssize_t
138           XisbWrite (XISBuffer *b, unsigned char *msg, xf86ssize_t len)
139           {
140               if (b->trace)
141               {
142                   int i = 0;
143                   for (i = 0; i < len; i++)
144                       ErrorF ("\t\twrote 0x%02x (%c)\n", msg[i], msg[i]);
145               }
146               return (xf86WriteSerial (b->fd, msg, len));
147           }
148           
149           /* turn tracing of this buffer on (1) or off (0) */
150           void
151           XisbTrace (XISBuffer *b, int trace)
152           {
153 dawes 1.1 	b->trace = trace;
154           }
155           
156           /*
157            * specify a block_duration of -1 when you know the buffer's fd is ready to
158            * read. After a read, it is automatically set to 0 so that the next read
159            * will use check to select for data and prevent a block.
160            * It is the caller's responsibility to set the block_duration to -1 if it
161            * knows that there is data to read (because the main select loop triggered
162            * the read) and want's to avoid the unnecessary overhead of the select call
163            *
164            * a zero or positive block duration will cause the select to block for the
165            * give duration in usecs.
166            */
167           
168           void
169           XisbBlockDuration (XISBuffer *b, int block_duration)
170           {
171           	b->block_duration = block_duration;
172           }

Powered by
ViewCVS 0.9.2