aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/linux/src/native/JoystickDevice.cpp
blob: 20be09870bbbd0a67b6d9b19d9353e718a19f31a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
 * Copyright (C) 2003 Jeremy Booth (jeremy@newdawnsoftware.com)
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this 
 * list of conditions and the following disclaimer. Redistributions in binary 
 * form must reproduce the above copyright notice, this list of conditions and 
 * the following disclaimer in the documentation and/or other materials provided
 * with the distribution. 
 * The name of the author may not be used to endorse or promote products derived
 * from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
 */

#include "eventInterfaceTypes.h"
#include "JoystickDevice.h"
#include <stdio.h>
#include <linux/input.h>
#include <linux/joystick.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <malloc.h>
#include <errno.h>

#include "logger.h"

JoystickDevice::JoystickDevice(char *deviceFileName) {
  char tempName[Device::MAX_NAME_LENGTH-1] = "Unknown";
  int i;

  LOG_TRACE("Trying to open %s\n", deviceFileName);
  fd = open(deviceFileName, O_RDONLY | O_NONBLOCK);
  /*if(fd<0) {
    char errorMessage[512];
    sprintf(errorMessage, "Error opening device %s\n", deviceFileName);
    perror(errorMessage);
  }*/

  if(fd>0){
    LOG_TRACE("Opened %s, trying to get device name\n", deviceFileName);
    if(ioctl(fd, JSIOCGNAME(sizeof(tempName)), tempName) < 0) {
      LOG_TRACE("Failed to get device name for %s\n", deviceFileName);
      char errorMessage[512];
      sprintf(errorMessage, "Error reading device %s\n", deviceFileName);
      perror(errorMessage);
    }

    int namelength=strlen(tempName);
    name = (char *)malloc(namelength+1);
    strncpy(name,tempName, namelength+1);

    char tempNumButtons;
    char tempNumAxes;
    LOG_TRACE("Getting button and axes information for %s\n", deviceFileName);
    ioctl (fd, JSIOCGBUTTONS, &tempNumButtons);
    ioctl (fd, JSIOCGAXES, &tempNumAxes);

    numButtons = tempNumButtons;
    numAbsAxes = tempNumAxes;

    //fprintf(stderr, "Got joystick %s with %d buttons and %d axes\n", tempName, numButtons, numAbsAxes);

    //buttonData = (uint8_t *)malloc(numButtons * sizeof(uint8_t));
    buttonData = new uint8_t[numButtons];
    //absAxesData = (int *)malloc(numAbsAxes * sizeof(int));
    absAxesData = new int[numAbsAxes];

    LOG_TRACE("Initialisation of %s completed\n", deviceFileName);
    inited = 1;
  } else {
    LOG_TRACE("Failed to open device %s\n", deviceFileName);
    inited = 0;
  }
}

int JoystickDevice::isValidDevice() {
  return inited; 
}

int JoystickDevice::getNumberRelAxes(){
  if(inited!=1) return -1;
  return 0;
}

int JoystickDevice::getNumberAbsAxes(){
  if(inited!=1) return -1;
  return numAbsAxes;
}

int JoystickDevice::getNumberButtons(){
  if(inited!=1) return -1;
  return numButtons;
}

const char *JoystickDevice::getName(){ 
  return name;
}

int JoystickDevice::getBusType(){
  if(inited!=1) return -1;
  return 0;
}

int JoystickDevice::getVendorID(){
  if(inited!=1) return -1;
  return 0;
}

int JoystickDevice::getProductID(){
  if(inited!=1) return -1;
  return 0;
}

int JoystickDevice::getVersion(){
  if(inited!=1) return -1;
  return 0;
}

void JoystickDevice::getSupportedRelAxes(int supportedAxis[]){
}

void JoystickDevice::getSupportedAbsAxes(int supportedAxis[]){
}

void JoystickDevice::getSupportedButtons(int supportedButtons[]){
}

/**
 * A return value of -1 means error, 0 means ok, but no change
 * a return of >0 means the data for this device has changed
 */
int JoystickDevice::poll(){
  struct js_event event;
  int numEvents = 0;
  while(read(fd, &event, sizeof event) > 0) {
    numEvents++;
    event.type &= ~JS_EVENT_INIT;
    if(event.type == JS_EVENT_BUTTON) {
      buttonData[event.number] = event.value;
    } else if(event.type == JS_EVENT_AXIS) {
      absAxesData[event.number] = event.value;
    }

  }
  // EAGAIN is returned when the queue is empty
  if(errno != EAGAIN) {
    printf("Something went wrong getting an event\n");
  }

  return numEvents;
}

void JoystickDevice::getPolledData(int relAxesData[], int absAxesData[], int buttonData[]){
  int i;

  if(inited!=1) return;
  for(i=0;i<numAbsAxes;i++) {
    (absAxesData)[i] = this->absAxesData[i];
  }
  for(i=0;i<numButtons;i++) {
    (buttonData)[i] = this->buttonData[i];
  }
}

int JoystickDevice::getAbsAxisMinimum(int axisNumber) {
  return -32767;
}

int JoystickDevice::getAbsAxisMaximum(int axisNumber) {
  return 32767;
}

int JoystickDevice::getAbsAxisFuzz(int axisNumber) {
  return 0;
}

bool JoystickDevice::getFFEnabled() {
	return false;
}

void JoystickDevice::rumble(float force) {
	return;
}

void JoystickDevice::cleanup() {
	close(fd);
}