| 52 | } |
| 53 | |
| 54 | int open_jsdev(const char* source, int flags) |
| 55 | { |
| 56 | /* Extract the js number from the dev filename */ |
| 57 | int jsnum; |
| 58 | int ret = sscanf(source, "/dev/input/js%d", &jsnum); |
| 59 | if (ret != 1) { |
| 60 | errno = ENOENT; |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | /* Return -1 and set errno if we don't support this js number */ |
| 65 | if (jsnum < 0 || jsnum >= Global::shared_config.nb_controllers || jsnum >= AllInputsFlat::MAXJOYS) { |
| 66 | errno = ENOENT; |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | LOG(LL_DEBUG, LCF_JOYSTICK, " jsdev device %d detected", jsnum); |
| 71 | |
| 72 | if (jsdevfds[jsnum].second++ == 0) { |
| 73 | /* Register that we use JSDEV for joystick inputs */ |
| 74 | Global::game_info.joystick |= GameInfo::JSDEV; |
| 75 | Global::game_info.tosend = true; |
| 76 | |
| 77 | /* Create an unnamed pipe */ |
| 78 | jsdevfds[jsnum].first = FileHandleList::createPipe(flags); |
| 79 | |
| 80 | /* If pipe creation failed (e.g. when opening the dev file in write mode), |
| 81 | * invalidate the pipe and return -1. */ |
| 82 | if (jsdevfds[jsnum].first.first == -1) { |
| 83 | LOG(LL_DEBUG, LCF_JOYSTICK, " could not create jsdev pipe with flags %d", flags); |
| 84 | jsdevfds[jsnum].second = 0; |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | /* Write the synthetic events corresponding to the initial state of the |
| 89 | * joystick. */ |
| 90 | struct js_event ev; |
| 91 | |
| 92 | struct timespec ts = DeterministicTimer::get().getTicks(); |
| 93 | ev.time = ts.tv_sec*1000 + ts.tv_nsec/1000000; |
| 94 | ev.value = 0; |
| 95 | for (int button = 0; button < SingleInput::BUTTON_LAST; button++) { |
| 96 | ev.type = JS_EVENT_BUTTON | JS_EVENT_INIT; |
| 97 | ev.number = button; |
| 98 | write_jsdev(ev, jsnum); |
| 99 | } |
| 100 | for (int axis = 0; axis < 8; axis++) { |
| 101 | ev.type = JS_EVENT_AXIS | JS_EVENT_INIT; |
| 102 | ev.number = axis; |
| 103 | write_jsdev(ev, jsnum); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return jsdevfds[jsnum].first.first; |
| 108 | } |
| 109 | |
| 110 | void write_jsdev(struct js_event ev, int jsnum) |
| 111 | { |
no test coverage detected