diff options
-rw-r--r-- | man/.gitignore | 2 | ||||
-rw-r--r-- | src/bsd_jstk.c | 374 | ||||
-rw-r--r-- | src/jstk.c | 585 | ||||
-rw-r--r-- | src/jstk.h | 35 | ||||
-rw-r--r-- | src/jstk_axis.c | 461 | ||||
-rw-r--r-- | src/jstk_options.c | 311 | ||||
-rw-r--r-- | src/jstk_options.h | 6 | ||||
-rw-r--r-- | src/linux_jstk.c | 182 |
8 files changed, 982 insertions, 974 deletions
diff --git a/man/.gitignore b/man/.gitignore index c858bd6..62796a4 100644 --- a/man/.gitignore +++ b/man/.gitignore @@ -1,3 +1,3 @@ Makefile Makefile.in -joystick.4 +joystick.4* diff --git a/src/bsd_jstk.c b/src/bsd_jstk.c index bc0d208..64af0cb 100644 --- a/src/bsd_jstk.c +++ b/src/bsd_jstk.c @@ -78,121 +78,116 @@ struct jstk_bsd_hid_data { int jstkOpenDevice(JoystickDevPtr joystick) { - int cur_axis; - int is_joystick, report_id = 0; - int got_something; - struct hid_data *d; - struct hid_item h; - report_desc_t rd; - struct jstk_bsd_hid_data *bsddata; - - if ((joystick->fd = open(joystick->device, O_RDWR | O_NDELAY, 0)) < 0) { - xf86Msg(X_ERROR, "Cannot open joystick '%s' (%s)\n", joystick->device, - strerror(errno)); - return -1; - } - - if ((rd = hid_get_report_desc(joystick->fd)) == 0) { - xf86Msg(X_ERROR, "Joystick: hid_get_report_desc failed: %s\n", - strerror(errno)); - close(joystick->fd); - joystick->fd = -1; - return -1; - } - - if (ioctl(joystick->fd, USB_GET_REPORT_ID, &report_id) < 0) { - xf86Msg(X_ERROR, "Joystick: ioctl USB_GET_REPORT_ID failed: %s\n", - strerror(errno)); - close(joystick->fd); - joystick->fd = -1; - return -1; - } - - bsddata = (struct jstk_bsd_hid_data*) - malloc(sizeof(struct jstk_bsd_hid_data)); - bsddata->dlen = hid_report_size(rd, hid_input, report_id); - - if ((bsddata->data_buf = malloc(bsddata->dlen)) == NULL) { - fprintf(stderr, "error: couldn't malloc %d bytes\n", bsddata->dlen); - hid_dispose_report_desc(rd); - free(bsddata); - close(joystick->fd); - joystick->fd = -1; - return -1; - } - - is_joystick = 0; - got_something = 0; - cur_axis = 0; - bsddata->hats = 0; - bsddata->axes = 0; - bsddata->buttons = 0; - - for (d = hid_start_parse(rd, 1 << hid_input, report_id); - hid_get_item(d, &h); ) { - int usage, page; - - page = HID_PAGE(h.usage); - usage = HID_USAGE(h.usage); - - is_joystick = is_joystick || - (h.kind == hid_collection && - page == HUP_GENERIC_DESKTOP && - (usage == HUG_JOYSTICK || usage == HUG_GAME_PAD)); - - if (h.kind != hid_input) - continue; - - if (!is_joystick) - continue; - - if (page == HUP_GENERIC_DESKTOP) - { - if (usage == HUG_HAT_SWITCH) - { - if ((bsddata->hats < MAXAXES) && (bsddata->axes <= MAXAXES-2)) { - got_something = 1; - memcpy(&bsddata->hat_item[bsddata->hats], &h, sizeof(h)); - bsddata->hats++; - bsddata->axes += 2; - } - } - else - { - if (bsddata->axes < MAXAXES) { - got_something = 1; - memcpy(&bsddata->axis_item[cur_axis], &h, sizeof(h)); - cur_axis++; - bsddata->axes++; - } - } - } - else if (page == HUP_BUTTON) - { - if (bsddata->buttons < MAXBUTTONS) { - got_something = 1; - memcpy(&bsddata->button_item[bsddata->buttons], &h, sizeof(h)); - bsddata->buttons++; - } + int cur_axis; + int is_joystick, report_id = 0; + int got_something; + struct hid_data *d; + struct hid_item h; + report_desc_t rd; + struct jstk_bsd_hid_data *bsddata; + + if ((joystick->fd = open(joystick->device, O_RDWR | O_NDELAY, 0)) < 0) { + xf86Msg(X_ERROR, "Cannot open joystick '%s' (%s)\n", joystick->device, + strerror(errno)); + return -1; + } + + if ((rd = hid_get_report_desc(joystick->fd)) == 0) { + xf86Msg(X_ERROR, "Joystick: hid_get_report_desc failed: %s\n", + strerror(errno)); + close(joystick->fd); + joystick->fd = -1; + return -1; + } + + if (ioctl(joystick->fd, USB_GET_REPORT_ID, &report_id) < 0) { + xf86Msg(X_ERROR, "Joystick: ioctl USB_GET_REPORT_ID failed: %s\n", + strerror(errno)); + close(joystick->fd); + joystick->fd = -1; + return -1; + } + + bsddata = (struct jstk_bsd_hid_data*) + malloc(sizeof(struct jstk_bsd_hid_data)); + bsddata->dlen = hid_report_size(rd, hid_input, report_id); + + if ((bsddata->data_buf = malloc(bsddata->dlen)) == NULL) { + fprintf(stderr, "error: couldn't malloc %d bytes\n", bsddata->dlen); + hid_dispose_report_desc(rd); + free(bsddata); + close(joystick->fd); + joystick->fd = -1; + return -1; + } + + is_joystick = 0; + got_something = 0; + cur_axis = 0; + bsddata->hats = 0; + bsddata->axes = 0; + bsddata->buttons = 0; + + for (d = hid_start_parse(rd, 1 << hid_input, report_id); + hid_get_item(d, &h); ) + { + int usage, page; + + page = HID_PAGE(h.usage); + usage = HID_USAGE(h.usage); + + is_joystick = is_joystick || + (h.kind == hid_collection && + page == HUP_GENERIC_DESKTOP && + (usage == HUG_JOYSTICK || usage == HUG_GAME_PAD)); + + if (h.kind != hid_input) + continue; + + if (!is_joystick) + continue; + + if (page == HUP_GENERIC_DESKTOP) { + if (usage == HUG_HAT_SWITCH) { + if ((bsddata->hats < MAXAXES) && (bsddata->axes <= MAXAXES-2)) { + got_something = 1; + memcpy(&bsddata->hat_item[bsddata->hats], &h, sizeof(h)); + bsddata->hats++; + bsddata->axes += 2; + } + } else { + if (bsddata->axes < MAXAXES) { + got_something = 1; + memcpy(&bsddata->axis_item[cur_axis], &h, sizeof(h)); + cur_axis++; + bsddata->axes++; + } + } + } else if (page == HUP_BUTTON) { + if (bsddata->buttons < MAXBUTTONS) { + got_something = 1; + memcpy(&bsddata->button_item[bsddata->buttons], &h, sizeof(h)); + bsddata->buttons++; + } } } - hid_end_parse(d); - - if (!got_something) { - free(bsddata->data_buf); - xf86Msg(X_ERROR, "Joystick: Didn't find any usable axes.\n"); - free(bsddata); - close(joystick->fd); - joystick->fd = -1; - return -1; - } - - bsddata->hotdata = 0; - joystick->devicedata = (void*) bsddata; - xf86Msg(X_INFO, "Joystick: %d buttons, %d axes\n", - bsddata->buttons, bsddata->axes); - - return joystick->fd; + hid_end_parse(d); + + if (!got_something) { + free(bsddata->data_buf); + xf86Msg(X_ERROR, "Joystick: Didn't find any usable axes.\n"); + free(bsddata); + close(joystick->fd); + joystick->fd = -1; + return -1; + } + + bsddata->hotdata = 0; + joystick->devicedata = (void*) bsddata; + xf86Msg(X_INFO, "Joystick: %d buttons, %d axes\n", + bsddata->buttons, bsddata->axes); + + return joystick->fd; } @@ -208,15 +203,15 @@ jstkOpenDevice(JoystickDevPtr joystick) void jstkCloseDevice(JoystickDevPtr joystick) { - if ((joystick->fd >= 0)) { - xf86CloseSerial(joystick->fd); - joystick->fd = -1; - } - if (joystick->devicedata != NULL) { - if ((((struct jstk_bsd_hid_data*)joystick->devicedata)->data_buf) != NULL) - free(((struct jstk_bsd_hid_data*)joystick->devicedata)->data_buf); - free(joystick->devicedata); - } + if ((joystick->fd >= 0)) { + xf86CloseSerial(joystick->fd); + joystick->fd = -1; + } + if (joystick->devicedata != NULL) { + if (((struct jstk_bsd_hid_data*)joystick->devicedata)->data_buf) + free(((struct jstk_bsd_hid_data*)joystick->devicedata)->data_buf); + free(joystick->devicedata); + } } @@ -237,82 +232,77 @@ jstkReadData(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; + 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; } - bsddata->hotdata = 1; - } - for (j=0; j<bsddata->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].oldvalue = 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->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].oldvalue = 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 + bsddata->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].oldvalue = joystick->axis[a].value; - 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].oldvalue = joystick->axis[a+1].value; - 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<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 + bsddata->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].oldvalue = joystick->axis[a].value; + 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].oldvalue = joystick->axis[a+1].value; + 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<bsddata->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; - } + for (j=0; j<bsddata->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; + bsddata->hotdata = 0; + return 1; } @@ -44,8 +44,9 @@ #include "jstk_options.h" - -int debug_level = 0; +#if DEBUG + int debug_level = 0; +#endif /* @@ -60,19 +61,19 @@ int debug_level = 0; static Bool jstkConvertProc(LocalDevicePtr local, - int first, - int num, - int v0, - int v1, - int v2, - int v3, - int v4, - int v5, - int* x, - int* y) + int first, + int num, + int v0, + int v1, + int v2, + int v3, + int v4, + int v5, + int* x, + int* y) { if (first != 0 || num != 2) - return FALSE; + return FALSE; *x = v0; *y = v1; @@ -128,149 +129,159 @@ jstkGenerateKeys(LocalDevicePtr local, KEYSCANCODES keys, char pressed) static void jstkReadProc(LocalDevicePtr local) { - JOYSTICKEVENT event; - int number; - int i; - int r; + JOYSTICKEVENT event; + int number; + int i, r; - JoystickDevPtr priv = local->private; + JoystickDevPtr priv = local->private; - do { - if ((r=jstkReadData(priv, &event, &number))==0) { - xf86Msg(X_WARNING, "JOYSTICK: Read failed. Deactivating device.\n"); + do { + if ((r=jstkReadData(priv, &event, &number))==0) { + xf86Msg(X_WARNING, "JOYSTICK: Read failed. Deactivating device.\n"); - if (local->fd >= 0) - RemoveEnabledDevice(local->fd); - return; - } + if (local->fd >= 0) + RemoveEnabledDevice(local->fd); + return; + } + + /* A button's status changed */ + if (event == EVENT_BUTTON) { + DBG(4, ErrorF("Button %d %s. Mapping: %d\n", number, + (priv->button[number].pressed == 0) ? "released" : "pressed", + priv->button[number].mapping)); + + switch (priv->button[number].mapping) { + case MAPPING_BUTTON: + if (priv->mouse_enabled == TRUE) { + xf86PostButtonEvent(local->dev, 0, + priv->button[number].buttonnumber, + priv->button[number].pressed, 0, 0); + } + break; + + case MAPPING_X: + case MAPPING_Y: + case MAPPING_ZX: + case MAPPING_ZY: + if (priv->button[number].pressed == 0) + priv->button[number].currentspeed = 1.0; + else if (priv->mouse_enabled == TRUE) + jstkStartButtonAxisTimer(local, number); + break; + + case MAPPING_KEY: + jstkGenerateKeys(local, + priv->button[number].keys, + priv->button[number].pressed); + break; + + case MAPPING_SPEED_MULTIPLY: + priv->amplify = 1.0; + /* Calculate new amplify value by multiplying them all */ + for (i=0; i<MAXAXES; i++) { + if ((priv->button[i].pressed) && + (priv->button[i].mapping == MAPPING_SPEED_MULTIPLY)) + priv->amplify *= priv->button[i].amplify; + } + DBG(2, ErrorF("Amplify is now %.3f\n", priv->amplify)); + break; + + case MAPPING_DISABLE: + if (priv->button[number].pressed == 1) { + if ((priv->mouse_enabled == TRUE) || + (priv->keys_enabled == TRUE)) + { + priv->mouse_enabled = FALSE; + priv->keys_enabled = FALSE; + DBG(2, ErrorF("All events disabled\n")); + } else { + priv->mouse_enabled = TRUE; + priv->keys_enabled = TRUE; + DBG(2, ErrorF("All events enabled\n")); + } + } + break; + case MAPPING_DISABLE_MOUSE: + if (priv->button[number].pressed == 1) { + if (priv->mouse_enabled == TRUE) + priv->mouse_enabled = FALSE; + else priv->mouse_enabled = TRUE; + DBG(2, ErrorF("Mouse events %s\n", + priv->mouse_enabled ? "enabled" : "disabled")); + } + break; + case MAPPING_DISABLE_KEYS: + if (priv->button[number].pressed == 1) { + if (priv->keys_enabled == TRUE) + priv->keys_enabled = FALSE; + else priv->keys_enabled = TRUE; + DBG(2, ErrorF("Keyboard events %s\n", + priv->mouse_enabled ? "enabled" : "disabled")); + } + break; - /* A button's status changed */ - if (event == EVENT_BUTTON) { - DBG(4, ErrorF("Button %d %s. Mapping: %d\n", number, - (priv->button[number].pressed == 0) ? "released" : "pressed", - priv->button[number].mapping)); - switch (priv->button[number].mapping) { - case MAPPING_BUTTON: - if (priv->mouse_enabled == TRUE) { - xf86PostButtonEvent(local->dev, 0, priv->button[number].buttonnumber, - priv->button[number].pressed, 0, 0); - } - break; - - case MAPPING_X: - case MAPPING_Y: - case MAPPING_ZX: - case MAPPING_ZY: - if (priv->button[number].pressed == 0) /* If button was released */ - priv->button[number].currentspeed = 1.0; /* Reset speed counter */ - else if (priv->mouse_enabled == TRUE) - jstkStartButtonAxisTimer(local, number); - break; - - case MAPPING_KEY: - jstkGenerateKeys(local, priv->button[number].keys, priv->button[number].pressed); - break; - - case MAPPING_SPEED_MULTIPLY: - priv->amplify = 1.0; - /* Calculate new global amplify value by multiplying them all */ - for (i=0; i<MAXAXES; i++) { - if ((priv->button[i].pressed) && - (priv->button[i].mapping == MAPPING_SPEED_MULTIPLY)) - priv->amplify *= priv->button[i].amplify; - } - DBG(2, ErrorF("Global amplify is now %.3f\n", priv->amplify)); - - break; - - case MAPPING_DISABLE: - if (priv->button[number].pressed == 1) { - if ((priv->mouse_enabled == TRUE) || (priv->keys_enabled == TRUE)) { - priv->mouse_enabled = FALSE; - priv->keys_enabled = FALSE; - DBG(2, ErrorF("All events disabled\n")); - } else { - priv->mouse_enabled = TRUE; - priv->keys_enabled = TRUE; - DBG(2, ErrorF("All events enabled\n")); + default: + break; } - } - break; - case MAPPING_DISABLE_MOUSE: - if (priv->button[number].pressed == 1) { - if (priv->mouse_enabled == TRUE) priv->mouse_enabled = FALSE; - else priv->mouse_enabled = TRUE; - DBG(2, ErrorF("Mouse events %s\n", - priv->mouse_enabled ? "enabled" : "disabled")); - } - break; - case MAPPING_DISABLE_KEYS: - if (priv->button[number].pressed == 1) { - if (priv->keys_enabled == TRUE) priv->keys_enabled = FALSE; - else priv->keys_enabled = TRUE; - DBG(2, ErrorF("Keyboard events %s\n", - priv->mouse_enabled ? "enabled" : "disabled")); - } - break; - - default: - break; - } - } + } - /* An axis was moved */ - if ((event == EVENT_AXIS) && - (priv->axis[number].type != TYPE_NONE)) { - DBG(5, ErrorF("Axis %d moved to %d. Type: %d, Mapping: %d\n", number, - priv->axis[number].value, - priv->axis[number].type, - priv->axis[number].mapping)); - - if (priv->axis[number].valuator != -1) - xf86PostMotionEvent(local->dev, 1, priv->axis[number].valuator, - 1, priv->axis[number].value); - - switch (priv->axis[number].mapping) { - case MAPPING_X: - case MAPPING_Y: - case MAPPING_ZX: - case MAPPING_ZY: { - switch (priv->axis[number].type) { - case TYPE_BYVALUE: - case TYPE_ACCELERATED: - if (priv->axis[number].value == 0) /* When axis was released */ - priv->axis[number].currentspeed = 1.0; /* Release speed counter */ - if (priv->mouse_enabled == TRUE) - jstkStartAxisTimer(local, number); - break; - - case TYPE_ABSOLUTE: - if (priv->mouse_enabled == TRUE) - jstkHandleAbsoluteAxis(local, number); - break; + /* An axis was moved */ + if ((event == EVENT_AXIS) && + (priv->axis[number].type != TYPE_NONE)) + { + DBG(5, ErrorF("Axis %d moved to %d. Type: %d, Mapping: %d\n", + number, + priv->axis[number].value, + priv->axis[number].type, + priv->axis[number].mapping)); + + if (priv->axis[number].valuator != -1) + xf86PostMotionEvent(local->dev, 1, priv->axis[number].valuator, + 1, priv->axis[number].value); + + switch (priv->axis[number].mapping) { + case MAPPING_X: + case MAPPING_Y: + case MAPPING_ZX: + case MAPPING_ZY: + switch (priv->axis[number].type) { + case TYPE_BYVALUE: + case TYPE_ACCELERATED: + if (priv->axis[number].value == 0) + priv->axis[number].currentspeed = 1.0; + if (priv->mouse_enabled == TRUE) + jstkStartAxisTimer(local, number); + break; + + case TYPE_ABSOLUTE: + if (priv->mouse_enabled == TRUE) + jstkHandleAbsoluteAxis(local, number); + break; + default: + break; + } /* switch (priv->axis[number].type) */ + break; /* case MAPPING_ZY */ + + case MAPPING_KEY: + if ((priv->axis[number].value > 0) != + (priv->axis[number].oldvalue > 0)) + jstkGenerateKeys(local, + priv->axis[number].keys_high, + (priv->axis[number].value > 0) ? 1 : 0); + + if ((priv->axis[number].value < 0) != + (priv->axis[number].oldvalue < 0)) + jstkGenerateKeys(local, + priv->axis[number].keys_low, + (priv->axis[number].value < 0) ? 1:0); + break; + + case MAPPING_NONE: default: - break; - } /* switch (priv->axis[number].type) */ - break; - } /* case MAPPING_ZY */ - - case MAPPING_KEY: - if ((priv->axis[number].value > 0) != (priv->axis[number].oldvalue > 0)) - jstkGenerateKeys(local, - priv->axis[number].keys_high, - (priv->axis[number].value > 0) ? 1:0); - - if ((priv->axis[number].value < 0) != (priv->axis[number].oldvalue < 0)) - jstkGenerateKeys(local, - priv->axis[number].keys_low, - (priv->axis[number].value < 0) ? 1:0); - break; - - case MAPPING_NONE: - default: break; - } /* switch (priv->axis{number].mapping) */ - } /* if (event == EVENT_AXIS) */ - } while (r == 2); + break; + } /* switch (priv->axis{number].mapping) */ + } /* if (event == EVENT_AXIS) */ + } while (r == 2); } @@ -289,107 +300,106 @@ static Bool jstkDeviceControlProc(DeviceIntPtr pJstk, int what) { - int i; - LocalDevicePtr local = (LocalDevicePtr)pJstk->public.devicePrivate; - JoystickDevPtr priv = (JoystickDevPtr)XI_PRIVATE(pJstk); + int i; + LocalDevicePtr local = (LocalDevicePtr)pJstk->public.devicePrivate; + JoystickDevPtr priv = (JoystickDevPtr)XI_PRIVATE(pJstk); - switch (what) - { + switch (what) { case DEVICE_INIT: { - int m; - DBG(1, ErrorF("jstkDeviceControlProc what=INIT\n")); - /* We want the first 7 button numbers fixed */ - if (priv->buttonmap.size != 0) { - if (InitButtonClassDeviceStruct(pJstk, priv->buttonmap.size, - priv->buttonmap.map) == FALSE) { - ErrorF("unable to allocate Button class device\n"); - return !Success; - } - if (InitFocusClassDeviceStruct(pJstk) == FALSE) { - ErrorF("unable to init Focus class device\n"); - return !Success; - } - } - - m = 2; - for (i=0; i<MAXAXES; i++) - if (priv->axis[i].type != TYPE_NONE) - priv->axis[i].valuator = m++; - - if (InitValuatorClassDeviceStruct(pJstk, - m, - xf86GetMotionEvents, - local->history_size, - Relative) == FALSE) { - ErrorF("unable to allocate Valuator class device\n"); - return !Success; - } else { - InitValuatorAxisStruct(pJstk, - 0, /* valuator num */ - 0, /* min val */ - screenInfo.screens[0]->width, /* max val */ - 1, /* resolution */ - 0, /* min_res */ - 1); /* max_res */ - InitValuatorAxisStruct(pJstk, - 1, /* valuator num */ - 0, /* min val */ - screenInfo.screens[0]->height, /* max val */ - 1, /* resolution */ - 0, /* min_res */ - 1); /* max_res */ + int m; + DBG(1, ErrorF("jstkDeviceControlProc what=INIT\n")); + /* We want the first 7 button numbers fixed */ + if (priv->buttonmap.size != 0) { + if (InitButtonClassDeviceStruct(pJstk, priv->buttonmap.size, + priv->buttonmap.map) == FALSE) { + ErrorF("unable to allocate Button class device\n"); + return !Success; + } + if (InitFocusClassDeviceStruct(pJstk) == FALSE) { + ErrorF("unable to init Focus class device\n"); + return !Success; + } + } + m = 2; for (i=0; i<MAXAXES; i++) if (priv->axis[i].type != TYPE_NONE) - { + priv->axis[i].valuator = m++; + + if (InitValuatorClassDeviceStruct(pJstk, + m, + xf86GetMotionEvents, + local->history_size, + Relative) == FALSE) { + ErrorF("unable to allocate Valuator class device\n"); + return !Success; + } else { InitValuatorAxisStruct(pJstk, - priv->axis[i].valuator, /* valuator num */ - -32768, /* min val */ - 32767, /* max val */ + 0, /* valuator num */ + 0, /* min val */ + screenInfo.screens[0]->width, /* max val */ 1, /* resolution */ 0, /* min_res */ 1); /* max_res */ + InitValuatorAxisStruct(pJstk, + 1, /* valuator num */ + 0, /* min val */ + screenInfo.screens[0]->height, /* max val */ + 1, /* resolution */ + 0, /* min_res */ + 1); /* max_res */ + + for (i=0; i<MAXAXES; i++) + if (priv->axis[i].type != TYPE_NONE) + { + InitValuatorAxisStruct(pJstk, + priv->axis[i].valuator, + -32768, /* min val */ + 32767, /* max val */ + 1, /* resolution */ + 0, /* min_res */ + 1); /* max_res */ + } + /* allocate the motion history buffer if needed */ + xf86MotionHistoryAllocate(local); } - /* allocate the motion history buffer if needed */ - xf86MotionHistoryAllocate(local); - } - break; + break; } case DEVICE_ON: - DBG(1, ErrorF("jstkDeviceControlProc what=ON name=%s\n", priv->device)); + DBG(1, ErrorF("jstkDeviceControlProc what=ON name=%s\n", + priv->device)); - if (jstkOpenDevice(priv) != -1) - { - pJstk->public.on = TRUE; - local->fd = priv->fd; - AddEnabledDevice(local->fd); - } else return !Success; - break; + if (jstkOpenDevice(priv) != -1) { + pJstk->public.on = TRUE; + local->fd = priv->fd; + AddEnabledDevice(local->fd); + } else return !Success; + break; case DEVICE_OFF: case DEVICE_CLOSE: - DBG(1, ErrorF("jstkDeviceControlProc what=%s\n", - (what == DEVICE_CLOSE) ? "CLOSE" : "OFF")); + DBG(1, ErrorF("jstkDeviceControlProc what=%s\n", + (what == DEVICE_CLOSE) ? "CLOSE" : "OFF")); - if (priv->timerrunning == TRUE) { - priv->timerrunning = FALSE; - TimerCancel(priv->timer); - } + if (priv->timerrunning == TRUE) { + priv->timerrunning = FALSE; + TimerCancel(priv->timer); + } - if (local->fd >= 0) - RemoveEnabledDevice(local->fd); - local->fd = -1; - jstkCloseDevice(priv); - pJstk->public.on = FALSE; - break; + if (local->fd >= 0) + RemoveEnabledDevice(local->fd); + local->fd = -1; + jstkCloseDevice(priv); + pJstk->public.on = FALSE; + break; default: - ErrorF("unsupported mode=%d\n", what); - return !Success; - break; - } - return Success; + ErrorF("unsupported mode=%d\n", what); + return !Success; + break; + } /* switch (what) */ + return Success; } @@ -414,7 +424,7 @@ jstkCorePreInit(InputDriverPtr drv, IDevPtr dev, int flags) local = xf86AllocateInput(drv, 0); if (!local) { - goto SetupProc_fail; + goto SetupProc_fail; } local->private = (JoystickDevPtr)xalloc(sizeof(JoystickDevRec)); @@ -455,24 +465,24 @@ jstkCorePreInit(InputDriverPtr drv, IDevPtr dev, int flags) /* Initialize default mappings */ for (i=0; i<MAXAXES; i++) { - priv->axis[i].value = 0; - priv->axis[i].oldvalue = 0; - priv->axis[i].deadzone = 1000; - priv->axis[i].type = TYPE_NONE; - priv->axis[i].mapping = MAPPING_NONE; - priv->axis[i].currentspeed = 0.0f; - priv->axis[i].amplify = 1.0f; - priv->axis[i].valuator = -1; - for (j=0; j<MAXKEYSPERBUTTON; j++) - priv->axis[i].keys_low[j] = priv->axis[i].keys_high[j] = 0; + priv->axis[i].value = 0; + priv->axis[i].oldvalue = 0; + priv->axis[i].deadzone = 1000; + priv->axis[i].type = TYPE_NONE; + priv->axis[i].mapping = MAPPING_NONE; + priv->axis[i].currentspeed = 0.0f; + priv->axis[i].amplify = 1.0f; + priv->axis[i].valuator = -1; + for (j=0; j<MAXKEYSPERBUTTON; j++) + priv->axis[i].keys_low[j] = priv->axis[i].keys_high[j] = 0; } for (i=0; i<MAXBUTTONS; i++) { - priv->button[i].pressed = 0; - priv->button[i].buttonnumber = 0; - priv->button[i].mapping = MAPPING_NONE; - priv->button[i].currentspeed = 1.0f; - for (j=0; j<MAXKEYSPERBUTTON; j++) - priv->button[i].keys[j] = 0; + priv->button[i].pressed = 0; + priv->button[i].buttonnumber = 0; + priv->button[i].mapping = MAPPING_NONE; + priv->button[i].currentspeed = 1.0f; + for (j=0; j<MAXKEYSPERBUTTON; j++) + priv->button[i].keys[j] = 0; } /* First three joystick buttons generate mouse clicks */ @@ -500,11 +510,11 @@ jstkCorePreInit(InputDriverPtr drv, IDevPtr dev, int flags) xf86OptionListReport(local->options); /* Joystick device is mandatory */ - priv->device = xf86SetStrOption(dev->commonOptions, "Device", NULL); + priv->device = xf86SetStrOption(dev->commonOptions, "Device", NULL); if (!priv->device) { - xf86Msg (X_ERROR, "%s: No Device specified.\n", local->name); - goto SetupProc_fail; + xf86Msg (X_ERROR, "%s: No Device specified.\n", local->name); + goto SetupProc_fail; } xf86ProcessCommonOptions(local, local->options); @@ -512,54 +522,55 @@ jstkCorePreInit(InputDriverPtr drv, IDevPtr dev, int flags) #if DEBUG debug_level = xf86SetIntOption(dev->commonOptions, "DebugLevel", 0); if (debug_level > 0) { - xf86Msg(X_CONFIG, "%s: debug level set to %d\n", - local->name, debug_level); + xf86Msg(X_CONFIG, "%s: debug level set to %d\n", + local->name, debug_level); } #else if (xf86SetIntOption(dev->commonOptions, "DebugLevel", 0) != 0) { - xf86Msg(X_WARNING, "%s: DebugLevel: Compiled without Debug support!\n", - local->name); + xf86Msg(X_WARNING, "%s: DebugLevel: Compiled without Debug support!\n", + local->name); } #endif /* Process button mapping options */ for (i=0; i<MAXBUTTONS; i++) { - char p[64]; - sprintf(p,"MapButton%d",i+1); - s = xf86SetStrOption(dev->commonOptions, p, NULL); - if (s != NULL) { - jstkParseButtonOption(s, priv, i, local->name); - } - DBG(1, xf86Msg(X_CONFIG, "Button %d mapped to %d\n", i+1, - priv->button[i].mapping)); + char p[64]; + sprintf(p,"MapButton%d",i+1); + s = xf86SetStrOption(dev->commonOptions, p, NULL); + if (s != NULL) { + jstkParseButtonOption(s, priv, i, local->name); + } + DBG(1, xf86Msg(X_CONFIG, "Button %d mapped to %d\n", i+1, + priv->button[i].mapping)); } /* Process button mapping options */ for (i=0; i<MAXAXES; i++) { - char p[64]; - sprintf(p,"MapAxis%d",i+1); - s = xf86SetStrOption(dev->commonOptions, p, NULL); - if (s != NULL) { - jstkParseAxisOption(s, &priv->axis[i], local->name); - } - DBG(1, xf86Msg(X_CONFIG, - "Axis %d type is %d, mapped to %d, amplify=%.3f\n", i+1, - priv->axis[i].type, - priv->axis[i].mapping, - priv->axis[i].amplify)); + char p[64]; + sprintf(p,"MapAxis%d",i+1); + s = xf86SetStrOption(dev->commonOptions, p, NULL); + if (s != NULL) { + jstkParseAxisOption(s, &priv->axis[i], local->name); + } + DBG(1, xf86Msg(X_CONFIG, + "Axis %d type is %d, mapped to %d, amplify=%.3f\n", i+1, + priv->axis[i].type, + priv->axis[i].mapping, + priv->axis[i].amplify)); } /* return the LocalDevice */ local->flags |= XI86_CONFIGURED ; return (local); - SetupProc_fail: + +SetupProc_fail: if (priv) xfree(priv); if (local) local->private = NULL; - return local; + return (local); } @@ -650,19 +661,19 @@ jstkDriverUnplug(pointer p) */ static XF86ModuleVersionInfo jstkVersionRec = { - "joystick", - MODULEVENDORSTRING, - MODINFOSTRING1, - MODINFOSTRING2, - XORG_VERSION_CURRENT, - PACKAGE_VERSION_MAJOR, - PACKAGE_VERSION_MINOR, - PACKAGE_VERSION_PATCHLEVEL, - ABI_CLASS_XINPUT, - ABI_XINPUT_VERSION, - MOD_CLASS_XINPUT, - {0, 0, 0, 0} /* signature, to be patched into the file by */ - /* a tool */ + "joystick", + MODULEVENDORSTRING, + MODINFOSTRING1, + MODINFOSTRING2, + XORG_VERSION_CURRENT, + PACKAGE_VERSION_MAJOR, + PACKAGE_VERSION_MINOR, + PACKAGE_VERSION_PATCHLEVEL, + ABI_CLASS_XINPUT, + ABI_XINPUT_VERSION, + MOD_CLASS_XINPUT, + {0, 0, 0, 0} /* signature, to be patched into the file by */ + /* a tool */ }; @@ -25,18 +25,21 @@ #define __JSTK_H_INCLUDED__ +#define MAXBUTTONS 32 +#define MAXAXES 32 +#define MAXKEYSPERBUTTON 4 /****************************************************************************** * debugging macro *****************************************************************************/ #ifdef DBG -#undef DBG + #undef DBG #endif + #ifdef DEBUG -#undef DEBUG + #undef DEBUG #endif - #define DEBUG 1 /** @@ -72,8 +75,6 @@ typedef enum _JOYSTICKMAPPING{ MAPPING_DISABLE_KEYS /* Disable only key events */ } JOYSTICKMAPPING; -#define MAXKEYSPERBUTTON 4 - typedef unsigned int KEYSCANCODES [MAXKEYSPERBUTTON]; typedef struct _AXIS { @@ -82,26 +83,22 @@ typedef struct _AXIS { int value, oldvalue; int valuator; int deadzone; - float currentspeed; - float previousposition; + float currentspeed; /* TYPE_ACCELERATED */ + float previousposition; /* TYPE_ABSOLUTE */ float amplify; - KEYSCANCODES keys_low; /* MAPPING_KEY */ - KEYSCANCODES keys_high; /* MAPPING_KEY */ + KEYSCANCODES keys_low, keys_high; /* MAPPING_KEY */ } AXIS; typedef struct _BUTTON { - JOYSTICKMAPPING mapping; - char pressed; - int buttonnumber; /* MAPPING_BUTTON */ - float amplify; /* MAPPING_X/Y/ZX/ZY, - MAPPING_SPEED_MULTIPLY */ - float currentspeed; /* MAPPING_X/Y/ZX/ZY */ - KEYSCANCODES keys; /* MAPPING_KEY */ + JOYSTICKMAPPING mapping; + char pressed; + int buttonnumber; /* MAPPING_BUTTON */ + float amplify; /* MAPPING_X/Y/ZX/ZY, + MAPPING_SPEED_MULTIPLY */ + float currentspeed; /* MAPPING_X/Y/ZX/ZY */ + KEYSCANCODES keys; /* MAPPING_KEY */ } BUTTON; -#define MAXBUTTONS 32 -#define MAXAXES 32 - typedef struct _JoystickDevRec { int fd; /* Actual file descriptor */ void *devicedata; /* Extra platform device dependend data */ diff --git a/src/jstk_axis.c b/src/jstk_axis.c index 82fdcdb..b5ddf9e 100644 --- a/src/jstk_axis.c +++ b/src/jstk_axis.c @@ -52,148 +52,155 @@ jstkAxisTimer(OsTimerPtr timer, CARD32 atime, pointer arg) { -#define NEXTTIMER 15 - DeviceIntPtr device = (DeviceIntPtr)arg; - JoystickDevPtr priv = (JoystickDevPtr)XI_PRIVATE(device); - - int sigstate, i; - int nexttimer; - nexttimer = 0; - - sigstate = xf86BlockSIGIO(); - - for (i=0; i<MAXAXES; i++) if ((priv->axis[i].value != 0) && - (priv->axis[i].type != TYPE_NONE)) { - float p1 = 0.0f; /* Pixels to move cursor */ - float p2 = 0.0f; /* Pixels to scroll */ - float scale; - AXIS *axis; - axis = &priv->axis[i]; - - nexttimer = NEXTTIMER; - - if (priv->axis[i].type == TYPE_BYVALUE) { - /* Calculate scale value, so we still get a range from 0 to 32768 */ - scale = (32768.0f / (float)(32768 - axis->deadzone)); - - /* How many pixels should this axis move the cursor */ - p1 = ((pow((abs((float)axis->value) - (float)axis->deadzone) * - scale / 1700.0f, 3.4f)) + 100.0f) * - ((float)NEXTTIMER / 40000.0f); - /* How many "pixels" should this axis scroll */ - p2 = ((pow((abs((float)axis->value) - (float)axis->deadzone) * - scale / 1000.0f, 2.5f)) + 200.0f) * - ((float)NEXTTIMER / 200000.0f); - - - } else if (axis->type == TYPE_ACCELERATED) { - /* Stop to accelerate at a certain speed */ - if (axis->currentspeed < 100.0f) axis->currentspeed = - (axis->currentspeed + 3.0f) * 1.07f - 3.0f; - p1 = axis->currentspeed * (float)NEXTTIMER / 180.0f; - p2 = p1 / 8.0f; + #define NEXTTIMER 15 + DeviceIntPtr device = (DeviceIntPtr)arg; + JoystickDevPtr priv = (JoystickDevPtr)XI_PRIVATE(device); + + int sigstate, i; + int nexttimer; + nexttimer = 0; + + sigstate = xf86BlockSIGIO(); + + for (i=0; i<MAXAXES; i++) if ((priv->axis[i].value != 0) && + (priv->axis[i].type != TYPE_NONE)) { + float p1 = 0.0f; /* Pixels to move cursor */ + float p2 = 0.0f; /* Pixels to scroll */ + float scale; + AXIS *axis; + axis = &priv->axis[i]; + + nexttimer = NEXTTIMER; + + if (priv->axis[i].type == TYPE_BYVALUE) { + /* Calculate scale value, so we get a range from 0 to 32768 */ + scale = (32768.0f / (float)(32768 - axis->deadzone)); + + /* How many pixels should this axis move the cursor */ + p1 = ((pow((abs((float)axis->value) - (float)axis->deadzone) * + scale / 1700.0f, 3.4f)) + 100.0f) * + ((float)NEXTTIMER / 40000.0f); + /* How many "pixels" should this axis scroll */ + p2 = ((pow((abs((float)axis->value) - (float)axis->deadzone) * + scale / 1000.0f, 2.5f)) + 200.0f) * + ((float)NEXTTIMER / 200000.0f); + } else if (axis->type == TYPE_ACCELERATED) { + /* Stop to accelerate at a certain speed */ + if (axis->currentspeed < 100.0f) + axis->currentspeed = (axis->currentspeed + 3.0f) * 1.07f - 3.0f; + p1 = axis->currentspeed * (float)NEXTTIMER / 180.0f; + p2 = p1 / 8.0f; + } + if (axis->value < 0) { + p1 = -p1; + p2 = -p2; + } + p1 *= axis->amplify * priv->amplify; + p2 *= axis->amplify * priv->amplify; + + /* Apply movement to global amount of pixels to move */ + switch (axis->mapping) { + case MAPPING_X: + priv->x += p1; + break; + case MAPPING_Y: + priv->y += p1; + break; + case MAPPING_ZX: + priv->zx += p2; + break; + case MAPPING_ZY: + priv->zy += p2; + break; + default: + break; + } } - if (axis->value < 0) { - p1 = -p1; - p2 = -p2; + + for (i=0; i<MAXBUTTONS; i++) if (priv->button[i].pressed == 1) { + float p1; + float p2; + + if (priv->button[i].currentspeed < 100.0f) + priv->button[i].currentspeed = + (priv->button[i].currentspeed + 3.0f) * 1.07f - 3.0f; + p1 = priv->button[i].currentspeed * (float)NEXTTIMER / 180.0f * + priv->button[i].amplify; + p1 *= priv->amplify; + p2 = p1 / 8.0f; + + /* Apply movement to global amount of pixels to move */ + switch (priv->button[i].mapping) { + case MAPPING_X: + priv->x += p1; + nexttimer = NEXTTIMER; + break; + case MAPPING_Y: + priv->y += p1; + nexttimer = NEXTTIMER; + break; + case MAPPING_ZX: + priv->zx += p2; + nexttimer = NEXTTIMER; + break; + case MAPPING_ZY: + priv->zy += p2; + nexttimer = NEXTTIMER; + break; + default: + break; + } } - p1 *= axis->amplify * priv->amplify; - p2 *= axis->amplify * priv->amplify; - /* Apply movement to global amount of pixels to move */ - switch (axis->mapping) { - case MAPPING_X: - priv->x += p1; - break; - case MAPPING_Y: - priv->y += p1; - break; - case MAPPING_ZX: - priv->zx += p2; - break; - case MAPPING_ZY: - priv->zy += p2; - break; - default: - break; + /* Actually move the cursor, if there is enough movement in the buffer */ + if (((int)priv->x != 0)||((int)priv->y != 0)) { + xf86PostMotionEvent(device, 0, 0, 2, (int)priv->x, (int)priv->y); + priv->x = priv->x - (int)priv->x; + priv->y = priv->y - (int)priv->y; } - } - - for (i=0; i<MAXBUTTONS; i++) if (priv->button[i].pressed == 1) { - float p1; - float p2; - - if (priv->button[i].currentspeed < 100.0f) priv->button[i].currentspeed = - (priv->button[i].currentspeed + 3.0f) * 1.07f - 3.0f; - p1 = priv->button[i].currentspeed * (float)NEXTTIMER / 180.0f * - priv->button[i].amplify; - p1 *= priv->amplify; - p2 = p1 / 8.0f; - - /* Apply movement to global amount of pixels to move */ - switch (priv->button[i].mapping) { - case MAPPING_X: - priv->x += p1; - nexttimer = NEXTTIMER; - break; - case MAPPING_Y: - priv->y += p1; - nexttimer = NEXTTIMER; - break; - case MAPPING_ZX: - priv->zx += p2; - nexttimer = NEXTTIMER; - break; - case MAPPING_ZY: - priv->zy += p2; - nexttimer = NEXTTIMER; - break; - default: - break; + + /* Generate scrolling events */ + while (priv->zy >= 1.0f) { /* down */ + xf86PostButtonEvent(device, 0, priv->buttonmap.scrollbutton[1], + 1, 0, 0); + xf86PostButtonEvent(device, 0, priv->buttonmap.scrollbutton[1], + 0, 0, 0); + priv->zy-=1.0f; } - } - - /* Actually move the cursor, if there is enough movement in the buffer */ - if (((int)priv->x != 0)||((int)priv->y != 0)) { - xf86PostMotionEvent(device, 0, 0, 2, (int)priv->x, (int)priv->y); - priv->x = priv->x - (int)priv->x; - priv->y = priv->y - (int)priv->y; - } - - /* Generate scrolling events */ - while (priv->zy >= 1.0f) { /* down */ - xf86PostButtonEvent(device, 0, priv->buttonmap.scrollbutton[1], 1, 0, 0); - xf86PostButtonEvent(device, 0, priv->buttonmap.scrollbutton[1], 0, 0, 0); - priv->zy-=1.0f; - } - while (priv->zy <= -1.0f) { /* up */ - xf86PostButtonEvent(device, 0, priv->buttonmap.scrollbutton[0], 1, 0, 0); - xf86PostButtonEvent(device, 0, priv->buttonmap.scrollbutton[0], 0, 0, 0); - priv->zy+=1.0f; - } - - while (priv->zx >= 1.0f) { /* right */ - xf86PostButtonEvent(device, 0, priv->buttonmap.scrollbutton[3], 1, 0, 0); - xf86PostButtonEvent(device, 0, priv->buttonmap.scrollbutton[3], 0, 0, 0); - priv->zx-=1.0f; - } - while (priv->zx <= -1.0f) { /* left */ - xf86PostButtonEvent(device, 0, priv->buttonmap.scrollbutton[2], 1, 0, 0); - xf86PostButtonEvent(device, 0, priv->buttonmap.scrollbutton[2], 0, 0, 0); - priv->zx+=1.0f; - } - - if (priv->mouse_enabled == FALSE) nexttimer = 0; - if (nexttimer == 0) { - priv->timerrunning = FALSE; - priv->x = 0.0f; - priv->y = 0.0f; - priv->zx = 0.0f; - priv->zy = 0.0f; - DBG(2, ErrorF("Stopping Axis Timer\n")); - } - xf86UnblockSIGIO (sigstate); - return nexttimer; + while (priv->zy <= -1.0f) { /* up */ + xf86PostButtonEvent(device, 0, priv->buttonmap.scrollbutton[0], + 1, 0, 0); + xf86PostButtonEvent(device, 0, priv->buttonmap.scrollbutton[0], + 0, 0, 0); + priv->zy+=1.0f; + } + + while (priv->zx >= 1.0f) { /* right */ + xf86PostButtonEvent(device, 0, priv->buttonmap.scrollbutton[3], + 1, 0, 0); + xf86PostButtonEvent(device, 0, priv->buttonmap.scrollbutton[3], + 0, 0, 0); + priv->zx-=1.0f; + } + while (priv->zx <= -1.0f) { /* left */ + xf86PostButtonEvent(device, 0, priv->buttonmap.scrollbutton[2], + 1, 0, 0); + xf86PostButtonEvent(device, 0, priv->buttonmap.scrollbutton[2], + 0, 0, 0); + priv->zx+=1.0f; + } + + if (priv->mouse_enabled == FALSE) nexttimer = 0; + if (nexttimer == 0) { /* No next timer (no subpixel added), so stop */ + priv->timerrunning = FALSE; + priv->x = 0.0f; + priv->y = 0.0f; + priv->zx = 0.0f; + priv->zy = 0.0f; + DBG(2, ErrorF("Stopping Axis Timer\n")); + } + xf86UnblockSIGIO (sigstate); + return nexttimer; } @@ -209,38 +216,38 @@ jstkAxisTimer(OsTimerPtr timer, void jstkStartAxisTimer(LocalDevicePtr device, int number) { - int pixel; - JoystickDevPtr priv = device->private; + int pixel; + JoystickDevPtr priv = device->private; - if (priv->timerrunning) return; - priv->timerrunning = TRUE; + if (priv->timerrunning) return; + priv->timerrunning = TRUE; - pixel = 1; - if (priv->axis[number].value < 0) pixel = -1; - switch (priv->axis[number].mapping) { + pixel = 1; + if (priv->axis[number].value < 0) pixel = -1; + switch (priv->axis[number].mapping) { case MAPPING_X: - priv->x += pixel; - break; + priv->x += pixel; + break; case MAPPING_Y: - priv->y += pixel; - break; + priv->y += pixel; + break; case MAPPING_ZX: - priv->zx += pixel; - break; + priv->zx += pixel; + break; case MAPPING_ZY: - priv->zy += pixel; - break; + priv->zy += pixel; + break; default: - break; - } - - DBG(2, ErrorF("Starting Axis Timer (triggered by axis %d)\n", number)); - priv->timer = TimerSet( - priv->timer, - 0, /* Relative */ - 1, /* What about NOW? */ - jstkAxisTimer, - device->dev); + break; + } + + DBG(2, ErrorF("Starting Axis Timer (triggered by axis %d)\n", number)); + priv->timer = TimerSet( + priv->timer, + 0, /* Relative */ + 1, /* What about NOW? */ + jstkAxisTimer, + device->dev); } /*********************************************************************** @@ -256,38 +263,38 @@ jstkStartAxisTimer(LocalDevicePtr device, int number) void jstkStartButtonAxisTimer(LocalDevicePtr device, int number) { - int pixel; - JoystickDevPtr priv = device->private; + int pixel; + JoystickDevPtr priv = device->private; - if (priv->timerrunning) return; - priv->timerrunning = TRUE; + if (priv->timerrunning) return; + priv->timerrunning = TRUE; - pixel = 1; - if (priv->button[number].amplify < 0) pixel = -1; - switch (priv->button[number].mapping) { + pixel = 1; + if (priv->button[number].amplify < 0) pixel = -1; + switch (priv->button[number].mapping) { case MAPPING_X: - priv->x += pixel; - break; + priv->x += pixel; + break; case MAPPING_Y: - priv->y += pixel; - break; + priv->y += pixel; + break; case MAPPING_ZX: - priv->zx += pixel; - break; + priv->zx += pixel; + break; case MAPPING_ZY: - priv->zy += pixel; - break; + priv->zy += pixel; + break; default: - break; - } - - DBG(2, ErrorF("Starting Axis Timer (triggered by button %d)\n", number)); - priv->timer = TimerSet( - priv->timer, - 0, /* Relative */ - 1, /* What about NOW? */ - jstkAxisTimer, - device->dev); + break; + } + + DBG(2, ErrorF("Starting Axis Timer (triggered by button %d)\n", number)); + priv->timer = TimerSet( + priv->timer, + 0, /* Relative */ + 1, /* What about NOW? */ + jstkAxisTimer, + device->dev); } /*********************************************************************** @@ -302,46 +309,46 @@ jstkStartButtonAxisTimer(LocalDevicePtr device, int number) void jstkHandleAbsoluteAxis(LocalDevicePtr device, int number) { - JoystickDevPtr priv = device->private; - int i,x,y; - - x=0; - y=0; - - for (i=0; i<MAXAXES; i++) - if (priv->axis[i].type == TYPE_ABSOLUTE) - { - float rel; - int dif; - if (priv->axis[i].value >= priv->axis[i].deadzone) - rel = (priv->axis[i].value - priv->axis[i].deadzone); - if (priv->axis[i].value <= -priv->axis[i].deadzone) - rel = (priv->axis[i].value + priv->axis[i].deadzone); - - rel = (rel) / (2.0f * (float)(32768 - priv->axis[i].deadzone)); - /* rel contains numbers between -0.5 and +0.5 now */ - - rel *= priv->axis[i].amplify; - - DBG(5, ErrorF("Relative Position of axis %d: %.2f\n", i, rel)); - - /* Calculate difference to previous position on screen in pixels */ - dif = (int)(rel - priv->axis[i].previousposition + 0.5f); - if ((dif >= 1)||(dif <= -1)) { - if (priv->axis[i].mapping == MAPPING_X) { - x += (dif); - priv->axis[i].previousposition += (float)dif; - } - if (priv->axis[i].mapping == MAPPING_Y) { - y += (int)(dif); - priv->axis[i].previousposition += (float)dif; - } + JoystickDevPtr priv = device->private; + int i,x,y; + + x=0; + y=0; + + for (i=0; i<MAXAXES; i++) + if (priv->axis[i].type == TYPE_ABSOLUTE) + { + float rel; + int dif; + if (priv->axis[i].value >= priv->axis[i].deadzone) + rel = (priv->axis[i].value - priv->axis[i].deadzone); + if (priv->axis[i].value <= -priv->axis[i].deadzone) + rel = (priv->axis[i].value + priv->axis[i].deadzone); + + rel = (rel) / (2.0f * (float)(32768 - priv->axis[i].deadzone)); + /* rel contains numbers between -0.5 and +0.5 now */ + + rel *= priv->axis[i].amplify; + + DBG(5, ErrorF("Relative Position of axis %d: %.2f\n", i, rel)); + + /* Calculate difference to previous position on screen in pixels */ + dif = (int)(rel - priv->axis[i].previousposition + 0.5f); + if ((dif >= 1)||(dif <= -1)) { + if (priv->axis[i].mapping == MAPPING_X) { + x += (dif); + priv->axis[i].previousposition += (float)dif; + } + if (priv->axis[i].mapping == MAPPING_Y) { + y += (int)(dif); + priv->axis[i].previousposition += (float)dif; + } + } + } + /* Still move relative, but relative to previous position of the axis */ + if ((x != 0) || (y != 0)) { + DBG(4, ErrorF("Moving mouse by %dx%d pixels\n", x, y)); + xf86PostMotionEvent(device->dev, 0, 0, 2, x, y); } - } - /* Still move relative, but relative to previous position of the axis */ - if ((x != 0) || (y != 0)) { - DBG(4, ErrorF("Moving mouse by %dx%d pixels\n", x, y)); - xf86PostMotionEvent(device->dev, 0, 0, 2, x, y); - } } diff --git a/src/jstk_options.c b/src/jstk_options.c index 64d5ab7..3e8cb00 100644 --- a/src/jstk_options.c +++ b/src/jstk_options.c @@ -37,7 +37,6 @@ - /*********************************************************************** * * jstkGetAxisMapping -- @@ -76,20 +75,20 @@ jstkGetButtonNumberInMap(JoystickDevPtr priv, static JOYSTICKMAPPING jstkGetAxisMapping(float *value, const char* param, const char* name) { - if (sscanf(param, "%f", value)==0) { - if (param[0] == '-') - *value *= -1.0; - } - if (strstr(param, "zx") != NULL) - return MAPPING_ZX; - else if (strstr(param, "zy") != NULL) - return MAPPING_ZY; - else if (strstr(param, "x") != NULL) - return MAPPING_X; - else if (strstr(param, "y") != NULL) - return MAPPING_Y; + if (sscanf(param, "%f", value)==0) { + if (param[0] == '-') + *value *= -1.0; + } + if (strstr(param, "zx") != NULL) + return MAPPING_ZX; + else if (strstr(param, "zy") != NULL) + return MAPPING_ZY; + else if (strstr(param, "x") != NULL) + return MAPPING_X; + else if (strstr(param, "y") != NULL) + return MAPPING_Y; - return MAPPING_NONE; + return MAPPING_NONE; } @@ -110,59 +109,59 @@ jstkParseButtonOption(const char* org, int number, const char* name) { - char *param; - char *tmp; - int value; - float fvalue; - char p[64]; - BUTTON* button; + char *param; + char *tmp; + int value; + float fvalue; + char p[64]; + BUTTON* button; - button = &priv->button[number]; + button = &priv->button[number]; - param = xstrdup(org); - for (tmp = param; *tmp; tmp++) *tmp = tolower(*tmp); + param = xstrdup(org); + for (tmp = param; *tmp; tmp++) *tmp = tolower(*tmp); - if (strcmp(param, "none") == 0) { - button->mapping = MAPPING_NONE; - } else if (sscanf(param, "button=%d", &value) == 1) { - button->mapping = MAPPING_BUTTON; - button->buttonnumber = jstkGetButtonNumberInMap(priv, value); - } else if (sscanf(param, "axis=%15s", p) == 1) { - p[15]='\0'; - button->mapping = jstkGetAxisMapping(&fvalue, p, name); - button->amplify = fvalue; - button->currentspeed = 1.0f; - if (button->mapping == MAPPING_NONE) - xf86Msg(X_WARNING, "%s: error parsing axis: %s.\n", - name, p); - } else if (sscanf(param, "amplify=%f", &fvalue) == 1) { - button->mapping = MAPPING_SPEED_MULTIPLY; - button->amplify = fvalue; - } else if (sscanf(param, "key=%30s", p) == 1) { - char *current, *next; - p[30]='\0'; - current = p; - button->mapping = MAPPING_KEY; - for (value = 0; value < MAXKEYSPERBUTTON; value++) if (current != NULL) { - next = strchr(current, ','); - if (next) *(next++) = '\0'; - button->keys[value] = atoi(current); - if (button->keys[value] == 0) - xf86Msg(X_WARNING, "%s: error parsing key value: %s.\n", - name, current); - current = next; - } else button->keys[value] = 0; - } else if (strcmp(param, "disable-all") == 0) { - button->mapping = MAPPING_DISABLE; - } else if (strcmp(param, "disable-mouse") == 0) { - button->mapping = MAPPING_DISABLE_MOUSE; - } else if (strcmp(param, "disable-keys") == 0) { - button->mapping = MAPPING_DISABLE_KEYS; - } else { - xf86Msg(X_WARNING, "%s: error parsing button parameter.\n", - name); - } - xfree(param); + if (strcmp(param, "none") == 0) { + button->mapping = MAPPING_NONE; + } else if (sscanf(param, "button=%d", &value) == 1) { + button->mapping = MAPPING_BUTTON; + button->buttonnumber = jstkGetButtonNumberInMap(priv, value); + } else if (sscanf(param, "axis=%15s", p) == 1) { + p[15]='\0'; + button->mapping = jstkGetAxisMapping(&fvalue, p, name); + button->amplify = fvalue; + button->currentspeed = 1.0f; + if (button->mapping == MAPPING_NONE) + xf86Msg(X_WARNING, "%s: error parsing axis: %s.\n", + name, p); + } else if (sscanf(param, "amplify=%f", &fvalue) == 1) { + button->mapping = MAPPING_SPEED_MULTIPLY; + button->amplify = fvalue; + } else if (sscanf(param, "key=%30s", p) == 1) { + char *current, *next; + p[30]='\0'; + current = p; + button->mapping = MAPPING_KEY; + for (value = 0; value < MAXKEYSPERBUTTON; value++) if (current != NULL) { + next = strchr(current, ','); + if (next) *(next++) = '\0'; + button->keys[value] = atoi(current); + if (button->keys[value] == 0) + xf86Msg(X_WARNING, "%s: error parsing key value: %s.\n", + name, current); + current = next; + } else button->keys[value] = 0; + } else if (strcmp(param, "disable-all") == 0) { + button->mapping = MAPPING_DISABLE; + } else if (strcmp(param, "disable-mouse") == 0) { + button->mapping = MAPPING_DISABLE_MOUSE; + } else if (strcmp(param, "disable-keys") == 0) { + button->mapping = MAPPING_DISABLE_KEYS; + } else { + xf86Msg(X_WARNING, "%s: error parsing button parameter.\n", + name); + } + xfree(param); } @@ -180,101 +179,105 @@ jstkParseButtonOption(const char* org, void jstkParseAxisOption(const char* org, AXIS *axis, const char *name) { - char *param; - char *tmp; - int value; - float fvalue; - char p[64]; - param = xstrdup(org); - for (tmp = param; *tmp; tmp++) *tmp = tolower(*tmp); + char *param; + char *tmp; + int value; + float fvalue; + char p[64]; + param = xstrdup(org); + for (tmp = param; *tmp; tmp++) *tmp = tolower(*tmp); - if ((tmp=strstr(param, "mode=")) != NULL) { - if (sscanf(tmp, "mode=%15s", p) == 1) { - p[15] = '\0'; - if (strcmp(p, "relative") == 0) - axis->type = TYPE_BYVALUE; - else if (strcmp(p, "accelerated") == 0) { - axis->type = TYPE_ACCELERATED; - axis->currentspeed = 1.0f; - } else if (strcmp(p, "absolute") == 0) - axis->type = TYPE_ABSOLUTE; - else if (strcmp(p, "none") == 0) - axis->type = TYPE_NONE; - else { - axis->type = TYPE_NONE; - xf86Msg(X_WARNING, "%s: \"%s\": error parsing mode.\n", - name, param); - } - }else xf86Msg(X_WARNING, "%s: \"%s\": error parsing mode.\n", - name, param); - } + if ((tmp=strstr(param, "mode=")) != NULL) { + if (sscanf(tmp, "mode=%15s", p) == 1) { + p[15] = '\0'; + if (strcmp(p, "relative") == 0) { + axis->type = TYPE_BYVALUE; + } else if (strcmp(p, "accelerated") == 0) { + axis->type = TYPE_ACCELERATED; + axis->currentspeed = 1.0f; + } else if (strcmp(p, "absolute") == 0) { + axis->type = TYPE_ABSOLUTE; + } else if (strcmp(p, "none") == 0) { + axis->type = TYPE_NONE; + } else { + axis->type = TYPE_NONE; + xf86Msg(X_WARNING, "%s: \"%s\": error parsing mode.\n", + name, param); + } + } else xf86Msg(X_WARNING, "%s: \"%s\": error parsing mode.\n", + name, param); + } - if ((tmp = strstr(param, "axis=")) != NULL) { - if (sscanf(tmp, "axis=%15s", p) == 1) { - p[15] = '\0'; - fvalue = 1.0f; - axis->mapping = jstkGetAxisMapping(&fvalue, p, name); - if ((axis->type == TYPE_ABSOLUTE) && - ((fvalue <= 1.1)&&(fvalue >= -1.1))) { - if (axis->mapping == MAPPING_X) - fvalue *= (int)screenInfo.screens[0]->width; - if (axis->mapping == MAPPING_Y) - fvalue *= (int)screenInfo.screens[0]->height; - } - axis->amplify = fvalue; - if (axis->mapping == MAPPING_NONE) - xf86Msg(X_WARNING, "%s: error parsing axis: %s.\n", - name, p); - }else xf86Msg(X_WARNING, "%s: error parsing axis.\n", - name); - } + if ((tmp = strstr(param, "axis=")) != NULL) { + if (sscanf(tmp, "axis=%15s", p) == 1) { + p[15] = '\0'; + fvalue = 1.0f; + axis->mapping = jstkGetAxisMapping(&fvalue, p, name); + if ((axis->type == TYPE_ABSOLUTE) && + ((fvalue <= 1.1)&&(fvalue >= -1.1))) { + if (axis->mapping == MAPPING_X) + fvalue *= (int)screenInfo.screens[0]->width; + if (axis->mapping == MAPPING_Y) + fvalue *= (int)screenInfo.screens[0]->height; + } + axis->amplify = fvalue; + if (axis->mapping == MAPPING_NONE) + xf86Msg(X_WARNING, "%s: error parsing axis: %s.\n", + name, p); + }else xf86Msg(X_WARNING, "%s: error parsing axis.\n", + name); + } - if ((tmp = strstr(param, "keylow=")) != NULL) { - if (sscanf(tmp, "keylow=%30s", p) == 1) { - char *current, *next; - p[30]='\0'; - current = p; - axis->mapping = MAPPING_KEY; - for (value = 0; value < MAXKEYSPERBUTTON; value++) if (current != NULL) { - next = strchr(current, ','); - if (next) *(next++) = '\0'; - axis->keys_low[value] = atoi(current); - if (axis->keys_low[value] == 0) - xf86Msg(X_WARNING, "%s: error parsing keylow value: %s.\n", - name, current); - current = next; - } else axis->keys_low[value] = 0; + if ((tmp = strstr(param, "keylow=")) != NULL) { + if (sscanf(tmp, "keylow=%30s", p) == 1) { + char *current, *next; + p[30]='\0'; + current = p; + axis->mapping = MAPPING_KEY; + for (value = 0; value < MAXKEYSPERBUTTON; value++) + if (current != NULL) { + next = strchr(current, ','); + if (next) *(next++) = '\0'; + axis->keys_low[value] = atoi(current); + if (axis->keys_low[value] == 0) + xf86Msg(X_WARNING, + "%s: error parsing keylow value: %s.\n", + name, current); + current = next; + } else axis->keys_low[value] = 0; + } } - } - if ((tmp = strstr(param, "keyhigh=")) != NULL) { - if (sscanf(tmp, "keyhigh=%30s", p) == 1) { - char *current, *next; - p[30]='\0'; - current = p; - axis->mapping = MAPPING_KEY; - for (value = 0; value < MAXKEYSPERBUTTON; value++) if (current != NULL) { - next = strchr(current, ','); - if (next) *(next++) = '\0'; - axis->keys_high[value] = atoi(current); - if (axis->keys_high[value] == 0) - xf86Msg(X_WARNING, "%s: error parsing keyhigh value: %s.\n", - name, current); - current = next; - } else axis->keys_high[value] = 0; + if ((tmp = strstr(param, "keyhigh=")) != NULL) { + if (sscanf(tmp, "keyhigh=%30s", p) == 1) { + char *current, *next; + p[30]='\0'; + current = p; + axis->mapping = MAPPING_KEY; + for (value = 0; value < MAXKEYSPERBUTTON; value++) + if (current != NULL) { + next = strchr(current, ','); + if (next) *(next++) = '\0'; + axis->keys_high[value] = atoi(current); + if (axis->keys_high[value] == 0) + xf86Msg(X_WARNING, + "%s: error parsing keyhigh value: %s.\n", + name, current); + current = next; + } else axis->keys_high[value] = 0; + } } - } - if ((tmp = strstr(param, "deadzone=")) != NULL ) { - if (sscanf(tmp, "deadzone=%d", &value) == 1) { - value = (value < 0) ? (-value) : (value); - if (value > 30000) - xf86Msg(X_WARNING, - "%s: deadzone of %d seems unreasonable. Ignored.\n", - name, value); - else axis->deadzone = value; - }else xf86Msg(X_WARNING, "%s: error parsing deadzone.\n", - name); - } - xfree(param); + if ((tmp = strstr(param, "deadzone=")) != NULL ) { + if (sscanf(tmp, "deadzone=%d", &value) == 1) { + value = (value < 0) ? (-value) : (value); + if (value > 30000) + xf86Msg(X_WARNING, + "%s: deadzone of %d seems unreasonable. Ignored.\n", + name, value); + else axis->deadzone = value; + }else xf86Msg(X_WARNING, "%s: error parsing deadzone.\n", + name); + } + xfree(param); } diff --git a/src/jstk_options.h b/src/jstk_options.h index 05b7d67..237ac12 100644 --- a/src/jstk_options.h +++ b/src/jstk_options.h @@ -28,9 +28,9 @@ int jstkGetButtonNumberInMap(JoystickDevPtr priv, int buttonnumber); void jstkParseButtonOption(const char* org, - JoystickDevPtr priv, - int number, - const char* name); + JoystickDevPtr priv, + int number, + const char* name); void jstkParseAxisOption(const char* org, AXIS *axis, const char* name); diff --git a/src/linux_jstk.c b/src/linux_jstk.c index 2a35942..176dd13 100644 --- a/src/linux_jstk.c +++ b/src/linux_jstk.c @@ -62,58 +62,58 @@ int jstkOpenDevice(JoystickDevPtr joystick) { - char joy_name[128]; - unsigned char axes, buttons; - int driver_version; - - if ((joystick->fd = open(joystick->device, O_RDONLY | O_NDELAY, 0)) < 0) { - xf86Msg(X_ERROR, "Cannot open joystick '%s' (%s)\n", joystick->device, - strerror(errno)); - return -1; - } - - if (ioctl(joystick->fd, JSIOCGVERSION, &driver_version) == -1) { - xf86Msg(X_ERROR, "Joystick: ioctl on '%s' failed: %s\n", joystick->device, - strerror(errno)); - close(joystick->fd); - joystick->fd = -1; - return -1; - } - if ((driver_version >> 16) < 1) { - xf86Msg(X_WARNING, "Joystick: Driver version is only %d.%d.%d\n", - driver_version >> 16, - (driver_version >> 8) & 0xff, - driver_version & 0xff); - } - - if (ioctl(joystick->fd, JSIOCGAXES, &axes) == -1) { - xf86Msg(X_ERROR, "Joystick: ioctl on '%s' failed: %s\n", joystick->device, - strerror(errno)); - close(joystick->fd); - joystick->fd = -1; - return -1; - } - - if (ioctl(joystick->fd, JSIOCGBUTTONS, &buttons) == -1) { - xf86Msg(X_ERROR, "Joystick: ioctl on '%s' failed: %s\n", joystick->device, - strerror(errno)); - close(joystick->fd); - joystick->fd = -1; - return -1; - } - - if (ioctl(joystick->fd, JSIOCGNAME(128), joy_name) == -1) { - xf86Msg(X_ERROR, "Joystick: ioctl on '%s' failed: %s\n", - joystick->device, strerror(errno)); - close(joystick->fd); - joystick->fd = -1; - return -1; - } - - xf86Msg(X_INFO, "Joystick: %s. %d axes, %d buttons\n", - joy_name, axes, buttons); - - return joystick->fd; + char joy_name[128]; + unsigned char axes, buttons; + int driver_version; + + if ((joystick->fd = open(joystick->device, O_RDONLY | O_NDELAY, 0)) < 0) { + xf86Msg(X_ERROR, "Cannot open joystick '%s' (%s)\n", + joystick->device, strerror(errno)); + return -1; + } + + if (ioctl(joystick->fd, JSIOCGVERSION, &driver_version) == -1) { + xf86Msg(X_ERROR, "Joystick: ioctl on '%s' failed: %s\n", + joystick->device, strerror(errno)); + close(joystick->fd); + joystick->fd = -1; + return -1; + } + if ((driver_version >> 16) < 1) { + xf86Msg(X_WARNING, "Joystick: Driver version is only %d.%d.%d\n", + driver_version >> 16, + (driver_version >> 8) & 0xff, + driver_version & 0xff); + } + + if (ioctl(joystick->fd, JSIOCGAXES, &axes) == -1) { + xf86Msg(X_ERROR, "Joystick: ioctl on '%s' failed: %s\n", + joystick->device, strerror(errno)); + close(joystick->fd); + joystick->fd = -1; + return -1; + } + + if (ioctl(joystick->fd, JSIOCGBUTTONS, &buttons) == -1) { + xf86Msg(X_ERROR, "Joystick: ioctl on '%s' failed: %s\n", + joystick->device, strerror(errno)); + close(joystick->fd); + joystick->fd = -1; + return -1; + } + + if (ioctl(joystick->fd, JSIOCGNAME(128), joy_name) == -1) { + xf86Msg(X_ERROR, "Joystick: ioctl on '%s' failed: %s\n", + joystick->device, strerror(errno)); + close(joystick->fd); + joystick->fd = -1; + return -1; + } + + xf86Msg(X_INFO, "Joystick: %s. %d axes, %d buttons\n", + joy_name, axes, buttons); + + return joystick->fd; } @@ -129,10 +129,10 @@ jstkOpenDevice(JoystickDevPtr joystick) void jstkCloseDevice(JoystickDevPtr joystick) { - if ((joystick->fd >= 0)) { - xf86CloseSerial(joystick->fd); - joystick->fd = -1; - } + if ((joystick->fd >= 0)) { + xf86CloseSerial(joystick->fd); + joystick->fd = -1; + } } @@ -153,43 +153,43 @@ jstkReadData(JoystickDevPtr joystick, JOYSTICKEVENT *event, int *number) { - struct js_event js; - if (event != NULL) *event = EVENT_NONE; - if (xf86ReadSerial(joystick->fd, - &js, - sizeof(struct js_event) - ) != sizeof(struct js_event)) - return 0; - - switch(js.type & ~JS_EVENT_INIT) { + struct js_event js; + if (event != NULL) *event = EVENT_NONE; + if (xf86ReadSerial(joystick->fd, &js, sizeof(struct js_event)) != + sizeof(struct js_event)) + return 0; + + switch(js.type & ~JS_EVENT_INIT) { case JS_EVENT_BUTTON: - if (js.number < MAXBUTTONS) - { - if (joystick->button[js.number].pressed != js.value) { - joystick->button[js.number].pressed = js.value; - if (event != NULL) *event = EVENT_BUTTON; - if (number != NULL) *number = js.number; + if (js.number < MAXBUTTONS) + { + if (joystick->button[js.number].pressed != js.value) { + joystick->button[js.number].pressed = js.value; + if (event != NULL) *event = EVENT_BUTTON; + if (number != NULL) *number = js.number; + } } - } - break; + break; case JS_EVENT_AXIS: - if (js.number < MAXAXES) { - if (abs(js.value) < joystick->axis[js.number].deadzone) { - /* We only want one event when in deadzone */ - if (joystick->axis[js.number].value != 0) { - joystick->axis[js.number].oldvalue = joystick->axis[js.number].value; - joystick->axis[js.number].value = 0; - if (event != NULL) *event = EVENT_AXIS; - if (number != NULL) *number = js.number; - } - }else{ - joystick->axis[js.number].oldvalue = joystick->axis[js.number].value; - joystick->axis[js.number].value = js.value; - if (event != NULL) *event = EVENT_AXIS; - if (number != NULL) *number = js.number; + if (js.number < MAXAXES) { + if (abs(js.value) < joystick->axis[js.number].deadzone) { + /* We only want one event when in deadzone */ + if (joystick->axis[js.number].value != 0) { + joystick->axis[js.number].oldvalue = + joystick->axis[js.number].value; + joystick->axis[js.number].value = 0; + if (event != NULL) *event = EVENT_AXIS; + if (number != NULL) *number = js.number; + } + }else{ + joystick->axis[js.number].oldvalue = + joystick->axis[js.number].value; + joystick->axis[js.number].value = js.value; + if (event != NULL) *event = EVENT_AXIS; + if (number != NULL) *number = js.number; + } } - } - break; - } - return 1; + break; + } + return 1; } |