2 * Copyright (c) 1997 Metro Link Incorporated
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
22 * 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.
29 X Input Serial Buffer routines for use in any XInput driver that accesses
33 /*****************************************************************************
35 ****************************************************************************/
37 #ifdef HAVE_XORG_CONFIG_H
38 #include <xorg-config.h>
43 #include <xf86_OSproc.h>
44 #include <xf86_OSlib.h>
45 #include <xf86Xinput.h>
48 /*****************************************************************************
50 ****************************************************************************/
52 /*****************************************************************************
53 * Variables without includable headers
54 ****************************************************************************/
56 /*****************************************************************************
58 ****************************************************************************/
60 /*****************************************************************************
61 * Function Definitions
62 ****************************************************************************/
65 XisbNew(int fd
, ssize_t size
)
69 b
= malloc(sizeof(XISBuffer
));
72 b
->buf
= malloc((sizeof(unsigned char) * size
));
80 b
->block_duration
= 0;
81 b
->current
= 1; /* force it to be past the end to trigger initial read */
83 b
->buffer_size
= size
;
88 XisbFree(XISBuffer
* b
)
95 XisbRead(XISBuffer
* b
)
99 if (b
->current
>= b
->end
) {
100 if (b
->block_duration
>= 0) {
101 if (xf86WaitForInput(b
->fd
, b
->block_duration
) < 1)
106 * automatically clear it so if XisbRead is called in a loop
107 * the next call will make sure there is data with select and
108 * thus prevent a blocking read
110 b
->block_duration
= 0;
113 ret
= xf86ReadSerial(b
->fd
, b
->buf
, b
->buffer_size
);
116 return -1; /* timeout */
118 return -2; /* error */
126 ErrorF("read 0x%02x (%c)\n", b
->buf
[b
->current
],
127 isprint(b
->buf
[b
->current
]) ? b
->buf
[b
->current
] : '.');
129 return b
->buf
[b
->current
++];
132 /* the only purpose of this function is to provide output tracing */
134 XisbWrite(XISBuffer
* b
, unsigned char *msg
, ssize_t len
)
139 for (i
= 0; i
< len
; i
++)
140 ErrorF("\t\twrote 0x%02x (%c)\n", msg
[i
], msg
[i
]);
142 return (xf86WriteSerial(b
->fd
, msg
, len
));
145 /* turn tracing of this buffer on (1) or off (0) */
147 XisbTrace(XISBuffer
* b
, int trace
)
153 * specify a block_duration of -1 when you know the buffer's fd is ready to
154 * read. After a read, it is automatically set to 0 so that the next read
155 * will use check to select for data and prevent a block.
156 * It is the caller's responsibility to set the block_duration to -1 if it
157 * knows that there is data to read (because the main select loop triggered
158 * the read) and want's to avoid the unnecessary overhead of the select call
160 * a zero or positive block duration will cause the select to block for the
161 * give duration in usecs.
165 XisbBlockDuration(XISBuffer
* b
, int block_duration
)
167 b
->block_duration
= block_duration
;