int XisbRead (XISBuffer *b) { int ret; if (b->current >= b->end) { if (b->block_duration >= 0) { if (xf86WaitForInput (b->fd, b->block_duration) < 1) return (-1); } else { /* * automatically clear it so if XisbRead is called in a loop * the next call will make sure there is data with select and * thus prevent a blocking read */ b->block_duration = 0; } ret = xf86ReadSerial (b->fd, b->buf, b->buffer_size); switch (ret) { case 0: return (-1); /* timeout */ case -1: return (-2); /* error */ default: b->end = ret; b->current = 0; break; } } if (b->trace) ErrorF ("read 0x%02x (%c)\n", b->buf[b->current], isprint(b->buf[b->current])?b->buf[b->current]:'.'); return (b->buf[b->current++]); }
static int jstkReadData_bsd(JoystickDevPtr joystick, JOYSTICKEVENT *event, int *number) { int j,d; struct jstk_bsd_hid_data *bsddata = (struct jstk_bsd_hid_data*)(joystick->devicedata); if (event != NULL) *event = EVENT_NONE; if (bsddata->hotdata == 0) { j= xf86ReadSerial(joystick->fd, bsddata->data_buf, bsddata->dlen); if (j != bsddata->dlen) { ErrorF("Read: %d byte! Should be %d\n",j,bsddata->dlen); return 0; } bsddata->hotdata = 1; } for (j=0; j<joystick->num_axes - (bsddata->hats * 2); j++) { d = hid_get_data(bsddata->data_buf, &bsddata->axis_item[j]); /* Scale the range to our expected range of -32768 to 32767 */ d = d - (bsddata->axis_item[j].logical_maximum - bsddata->axis_item[j].logical_minimum) / 2; d = d * 65536 / (bsddata->axis_item[j].logical_maximum - bsddata->axis_item[j].logical_minimum); if (abs(d) < joystick->axis[j].deadzone) d = 0; if (d != joystick->axis[j].value) { joystick->axis[j].value = d; if (event != NULL) *event = EVENT_AXIS; if (number != NULL) *number = j; return 2; } } for (j=0; j<bsddata->hats; j++) { int a; int v1_data[9] = { 0, 32767, 32767, 32767, 0, -32768, -32768, -32768, 0 }; int v2_data[9] = { -32768, -32768, 0, 32767, 32767, 32767, 0, -32767, 0 }; a = j*2 + joystick->num_axes - bsddata->hats *2; d = hid_get_data(bsddata->data_buf, &bsddata->hat_item[j]) - bsddata->hat_item[j].logical_minimum; if (joystick->axis[a].value != v1_data[d]) { joystick->axis[a].value = v1_data[d]; if (event != NULL) *event = EVENT_AXIS; if (number != NULL) *number = a; return 2; } if (joystick->axis[a+1].value != v2_data[d]) { joystick->axis[a+1].value = v2_data[d]; if (event != NULL) *event = EVENT_AXIS; if (number != NULL) *number = a+1; return 2; } } for (j=0; j<joystick->num_buttons; j++) { int pressed; d = hid_get_data(bsddata->data_buf, &bsddata->button_item[j]); pressed = (d == bsddata->button_item[j].logical_minimum) ? 0 : 1; if (pressed != joystick->button[j].pressed) { joystick->button[j].pressed = pressed; if (event != NULL) *event = EVENT_BUTTON; if (number != NULL) *number = j; return 2; } } bsddata->hotdata = 0; return 1; }