Differences From Artifact [beaad493bb]:
- File src/hid/OFEvdevGameController.m — part of check-in [cb4c3d09f4] at 2024-05-20 15:53:41 on branch gamecontroller — OFGameController: No symbols for custom buttons (user: js, size: 16734) [annotate] [blame] [check-ins using]
To Artifact [5b6c618a19]:
- File
src/hid/OFEvdevGameController.m
— part of check-in
[8e95537be5]
at
2024-05-20 17:19:05
on branch gamecontroller
— OFEvdevGameController: Add support for polling
This is required to get the initial state and when events get dropped. (user: js, size: 19393) [annotate] [blame] [check-ins using] [more...]
︙ | ︙ | |||
237 238 239 240 241 242 243 | { self = [super init]; @try { OFStringEncoding encoding = [OFLocale encoding]; unsigned long evBits[OFRoundUpToPowerOf2(OF_ULONG_BIT, EV_MAX) / OF_ULONG_BIT] = { 0 }; | < < > > > | > | | | | | 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | { self = [super init]; @try { OFStringEncoding encoding = [OFLocale encoding]; unsigned long evBits[OFRoundUpToPowerOf2(OF_ULONG_BIT, EV_MAX) / OF_ULONG_BIT] = { 0 }; unsigned long absBits[OFRoundUpToPowerOf2(OF_ULONG_BIT, ABS_MAX) / OF_ULONG_BIT] = { 0 }; struct input_id inputID; char name[128]; _path = [path copy]; if ((_fd = open([_path cStringWithEncoding: encoding], O_RDONLY | O_NONBLOCK)) == -1) @throw [OFOpenItemFailedException exceptionWithPath: _path mode: @"r" errNo: errno]; if (ioctl(_fd, EVIOCGBIT(0, sizeof(evBits)), evBits) == -1) @throw [OFInitializationFailedException exception]; if (!OFBitSetIsSet(evBits, EV_KEY)) @throw [OFInvalidArgumentException exception]; _keyBits = OFAllocZeroedMemory(OFRoundUpToPowerOf2(OF_ULONG_BIT, KEY_MAX) / OF_ULONG_BIT, sizeof(unsigned long)); if (ioctl(_fd, EVIOCGBIT(EV_KEY, OFRoundUpToPowerOf2( OF_ULONG_BIT, KEY_MAX) / OF_ULONG_BIT * sizeof(unsigned long)), _keyBits) == -1) @throw [OFInitializationFailedException exception]; if (!OFBitSetIsSet(_keyBits, BTN_GAMEPAD) && !OFBitSetIsSet(_keyBits, BTN_DPAD_UP)) @throw [OFInvalidArgumentException exception]; if (ioctl(_fd, EVIOCGID, &inputID) == -1) @throw [OFInvalidArgumentException exception]; _vendorID = inputID.vendor; _productID = inputID.product; if (ioctl(_fd, EVIOCGNAME(sizeof(name)), name) == -1) @throw [OFInitializationFailedException exception]; _name = [[OFString alloc] initWithCString: name encoding: encoding]; _buttons = [[OFMutableSet alloc] init]; for (size_t i = 0; i < sizeof(buttons) / sizeof(*buttons); i++) { if (OFBitSetIsSet(_keyBits, buttons[i])) { OFGameControllerButton button = buttonToName( buttons[i], _vendorID, _productID); if (button != nil) [_buttons addObject: button]; } } |
︙ | ︙ | |||
317 318 319 320 321 322 323 | _rightAnalogStickXBit = ABS_RX; _rightAnalogStickYBit = ABS_RY; _leftTriggerPressureBit = ABS_Z; _rightTriggerPressureBit = ABS_RZ; } if (OFBitSetIsSet(absBits, ABS_X) && | | < < < < < < < < < < < < < < < < | < < < < < < < < < < < < < < < < < < | | | | < < < < < < < < < < < < < < < < < < < < < < > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | _rightAnalogStickXBit = ABS_RX; _rightAnalogStickYBit = ABS_RY; _leftTriggerPressureBit = ABS_Z; _rightTriggerPressureBit = ABS_RZ; } if (OFBitSetIsSet(absBits, ABS_X) && OFBitSetIsSet(absBits, ABS_Y)) _hasLeftAnalogStick = true; if (OFBitSetIsSet(absBits, _rightAnalogStickXBit) && OFBitSetIsSet(absBits, _rightAnalogStickYBit)) _hasRightAnalogStick = true; if (_vendorID == vendorIDNintendo && _productID == productIDN64Controller && OFBitSetIsSet(_keyBits, BTN_Y) && OFBitSetIsSet(_keyBits, BTN_C) && OFBitSetIsSet(_keyBits, BTN_SELECT) && OFBitSetIsSet(_keyBits, BTN_X)) _hasRightAnalogStick = true; if (OFBitSetIsSet(absBits, ABS_HAT0X) && OFBitSetIsSet(absBits, ABS_HAT0Y)) { [_buttons addObject: OFGameControllerDPadLeftButton]; [_buttons addObject: OFGameControllerDPadRightButton]; [_buttons addObject: OFGameControllerDPadUpButton]; [_buttons addObject: OFGameControllerDPadDownButton]; } if (OFBitSetIsSet(absBits, _leftTriggerPressureBit)) { _hasLeftTriggerPressure = true; [_buttons addObject: OFGameControllerLeftTriggerButton]; } if (OFBitSetIsSet(absBits, _rightTriggerPressureBit)) { _hasRightTriggerPressure = true; [_buttons addObject: OFGameControllerRightTriggerButton]; } } [_buttons makeImmutable]; @try { [self of_pollState]; } @catch (OFReadFailedException *e) { @throw [OFInitializationFailedException exception]; } } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { [_path release]; if (_fd != -1) close(_fd); OFFreeMemory(_keyBits); [_name release]; [_buttons release]; [_pressedButtons release]; [super dealloc]; } - (OFNumber *)vendorID { return [OFNumber numberWithUnsignedShort: _vendorID]; } - (OFNumber *)productID { return [OFNumber numberWithUnsignedShort: _productID]; } - (void)of_pollState { unsigned long keyState[OFRoundUpToPowerOf2(OF_ULONG_BIT, KEY_MAX) / OF_ULONG_BIT] = { 0 }; if (ioctl(_fd, EVIOCGKEY(sizeof(keyState)), &keyState) == -1) @throw [OFReadFailedException exceptionWithObject: self requestedLength: sizeof(keyState) errNo: errno]; [_pressedButtons removeAllObjects]; for (size_t i = 0; i < sizeof(buttons) / sizeof(*buttons); i++) { if (OFBitSetIsSet(_keyBits, buttons[i]) && OFBitSetIsSet(keyState, buttons[i])) { OFGameControllerButton button = buttonToName( buttons[i], _vendorID, _productID); if (button != nil) [_pressedButtons addObject: button]; } } if (_hasLeftAnalogStick) { struct input_absinfo infoX, infoY; if (ioctl(_fd, EVIOCGABS(ABS_X), &infoX) == -1) @throw [OFReadFailedException exceptionWithObject: self requestedLength: sizeof(infoX) errNo: errno]; if (ioctl(_fd, EVIOCGABS(ABS_Y), &infoY) == -1) @throw [OFReadFailedException exceptionWithObject: self requestedLength: sizeof(infoY) errNo: errno]; _leftAnalogStickMinX = infoX.minimum; _leftAnalogStickMaxX = infoX.maximum; _leftAnalogStickMinY = infoY.minimum; _leftAnalogStickMaxY = infoY.maximum; _leftAnalogStickPosition.x = scale(infoX.value, _leftAnalogStickMinX, _leftAnalogStickMaxX); _leftAnalogStickPosition.y = scale(infoY.value, _leftAnalogStickMinY, _leftAnalogStickMaxY); } if (_vendorID == vendorIDNintendo && _productID == productIDN64Controller && OFBitSetIsSet(_keyBits, BTN_Y) && OFBitSetIsSet(_keyBits, BTN_C) && OFBitSetIsSet(_keyBits, BTN_SELECT) && OFBitSetIsSet(_keyBits, BTN_X)) _rightAnalogStickPosition = OFMakePoint( -OFBitSetIsSet(keyState, BTN_Y) + OFBitSetIsSet(keyState, BTN_C), -OFBitSetIsSet(keyState, BTN_SELECT) + OFBitSetIsSet(keyState, BTN_X)); else if (_hasRightAnalogStick) { struct input_absinfo infoX, infoY; if (ioctl(_fd, EVIOCGABS(_rightAnalogStickXBit), &infoX) == -1) @throw [OFReadFailedException exceptionWithObject: self requestedLength: sizeof(infoX) errNo: errno]; if (ioctl(_fd, EVIOCGABS(_rightAnalogStickYBit), &infoY) == -1) @throw [OFReadFailedException exceptionWithObject: self requestedLength: sizeof(infoY) errNo: errno]; _rightAnalogStickMinX = infoX.minimum; _rightAnalogStickMaxX = infoX.maximum; _rightAnalogStickMinY = infoY.minimum; _rightAnalogStickMaxY = infoY.maximum; _rightAnalogStickPosition.x = scale(infoX.value, _rightAnalogStickMinX, _rightAnalogStickMaxX); _rightAnalogStickPosition.y = scale(infoY.value, _rightAnalogStickMinY, _rightAnalogStickMaxY); } if (_hasLeftTriggerPressure) { struct input_absinfo info; if (ioctl(_fd, EVIOCGABS( _leftTriggerPressureBit), &info) == -1) @throw [OFReadFailedException exceptionWithObject: self requestedLength: sizeof(info) errNo: errno]; _leftTriggerMinPressure = info.minimum; _leftTriggerMaxPressure = info.maximum; _leftTriggerPressure = scale(info.value, _leftTriggerMinPressure, _leftTriggerMaxPressure); } if (_hasRightTriggerPressure) { struct input_absinfo info; if (ioctl(_fd, EVIOCGABS(_rightTriggerPressureBit), &info) == -1) @throw [OFReadFailedException exceptionWithObject: self requestedLength: sizeof(info) errNo: errno]; _rightTriggerMinPressure = info.minimum; _rightTriggerMaxPressure = info.maximum; _leftTriggerPressure = scale(info.value, _leftTriggerMinPressure, _leftTriggerMaxPressure); } } - (void)retrieveState { struct input_event event; for (;;) { OFGameControllerButton button; errno = 0; if (read(_fd, &event, sizeof(event)) < (int)sizeof(event)) { if (errno == EWOULDBLOCK) return; @throw [OFReadFailedException exceptionWithObject: self requestedLength: sizeof(event) errNo: errno]; } if (_discardUntilReport) { if (event.type == EV_SYN && event.value == SYN_REPORT) { _discardUntilReport = false; [self of_pollState]; } continue; } switch (event.type) { case EV_SYN: if (event.value == SYN_DROPPED) { _discardUntilReport = true; continue; } break; case EV_KEY: if ((button = buttonToName(event.code, _vendorID, _productID)) != nil) { if (event.value) [_pressedButtons addObject: button]; else [_pressedButtons removeObject: button]; |
︙ | ︙ |