You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.0 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /***************************************************************************
  2. * Copyright (C) 2014 M Wellings *
  3. * info@openforeveryone.co.uk *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License version 2 as *
  7. * published by the Free Software Foundation *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include "qgamecontroller.h"
  20. #include "qgamecontroller_linux_p.h"
  21. #include <QDebug>
  22. QGameControllerPrivate::QGameControllerPrivate(uint id, QGameController *q) :
  23. q_ptr(q)
  24. {
  25. ID=id;
  26. Valid=false;
  27. Axis=0;
  28. Buttons=0;
  29. // qDebug("QGameController::QGameController(%i)", ID);
  30. char number_of_axes=0;
  31. char number_of_buttons=0;
  32. QString filename = QString("/dev/input/js%1").arg(QString::number(id));
  33. qDebug() << "Opening" << filename.toUtf8().data();
  34. if( ( fd = open( filename.toUtf8().data() , O_NONBLOCK)) == -1 )
  35. {
  36. qDebug( "Couldn't open joystick\n" );
  37. return;
  38. }
  39. Valid=true;
  40. ioctl (fd, JSIOCGAXES, &number_of_axes);
  41. ioctl (fd, JSIOCGBUTTONS, &number_of_buttons);
  42. Axis=number_of_axes;
  43. Buttons=number_of_buttons;
  44. char name_of_stick[80];
  45. ioctl (fd, JSIOCGNAME(80), &name_of_stick);
  46. Description=name_of_stick;
  47. qDebug("Joystick: \"%s\" has %i axis and %i buttons", name_of_stick, number_of_axes, number_of_buttons);
  48. readGameController();
  49. }
  50. void QGameControllerPrivate::readGameController()
  51. {
  52. if (!Valid)
  53. return;
  54. // qDebug() << "readJoystick";
  55. struct js_event e;
  56. while (read (fd, &e, sizeof(e)) > 0) {
  57. process_event (e);
  58. }
  59. /* EAGAIN is returned when the queue is empty */
  60. if (errno != EAGAIN) {
  61. qDebug() << "Error";
  62. Valid=false;
  63. }
  64. // else
  65. // qDebug() << "No event";
  66. }
  67. void QGameControllerPrivate::process_event(js_event e)
  68. {
  69. Q_Q(QGameController);
  70. QGameControllerEvent *event = NULL;
  71. if (e.type & JS_EVENT_INIT)
  72. {
  73. // qDebug() << "process_event" << "event was a JS_EVENT_INIT" << e.number << e.value << e.type;
  74. }
  75. qint16 value = e.value;
  76. // qDebug() << "process_event" << e.number << value << e.type;
  77. if (e.type & JS_EVENT_BUTTON)
  78. {
  79. if (e.value==1)
  80. {
  81. // qDebug("Button %i pressed.", e.number);
  82. }
  83. else
  84. {
  85. // qDebug("Button %i released.", e.number);
  86. }
  87. event=new QGameControllerButtonEvent(ID, e.number, value);
  88. ButtonValues.insert(e.number, value);
  89. emit(q->gameControllerButtonEvent((QGameControllerButtonEvent*)event));
  90. } else if (e.type & JS_EVENT_AXIS) {
  91. float Value;
  92. if (value<0)
  93. Value = (float)value/32768.0;
  94. else
  95. Value = (float)value/32767.0;
  96. AxisValues.insert(e.number, Value);
  97. event=new QGameControllerAxisEvent(ID, e.number, Value);
  98. // qDebug("Axis %i moved to %f.", e.number , Value);
  99. emit(q->gameControllerAxisEvent((QGameControllerAxisEvent*)event));
  100. }
  101. emit(q->gameControllerEvent(event));
  102. return;
  103. }