summaryrefslogtreecommitdiffstats
path: root/dom/gamepad/linux/LinuxGamepad.cpp
blob: c45f4174a2f55be8a0fd3e5ab0785112c48092ab (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * LinuxGamepadService: A Linux backend for the GamepadService.
 * Derived from the kernel documentation at
 * http://www.kernel.org/doc/Documentation/input/joystick-api.txt
 */
#include <algorithm>
#include <cstddef>

#include <glib.h>
#include <linux/joystick.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "nscore.h"
#include "mozilla/dom/GamepadPlatformService.h"
#include "udev.h"

namespace {

using namespace mozilla::dom;
using mozilla::udev_lib;
using mozilla::udev_device;
using mozilla::udev_list_entry;
using mozilla::udev_enumerate;
using mozilla::udev_monitor;

static const float kMaxAxisValue = 32767.0;
static const char kJoystickPath[] = "/dev/input/js";

//TODO: should find a USB identifier for each device so we can
// provide something that persists across connect/disconnect cycles.
typedef struct {
  int index;
  guint source_id;
  int numAxes;
  int numButtons;
  char idstring[128];
  char devpath[PATH_MAX];
} Gamepad;

class LinuxGamepadService {
public:
  LinuxGamepadService() : mMonitor(nullptr),
                          mMonitorSourceID(0) {
  }

  void Startup();
  void Shutdown();

private:
  void AddDevice(struct udev_device* dev);
  void RemoveDevice(struct udev_device* dev);
  void ScanForDevices();
  void AddMonitor();
  void RemoveMonitor();
  bool is_gamepad(struct udev_device* dev);
  void ReadUdevChange();

  // handler for data from /dev/input/jsN
  static gboolean OnGamepadData(GIOChannel *source,
                                GIOCondition condition,
                                gpointer data);

  // handler for data from udev monitor
  static gboolean OnUdevMonitor(GIOChannel *source,
                                GIOCondition condition,
                                gpointer data);

  udev_lib mUdev;
  struct udev_monitor* mMonitor;
  guint mMonitorSourceID;
  // Information about currently connected gamepads.
  AutoTArray<Gamepad,4> mGamepads;
};

// singleton instance
LinuxGamepadService* gService = nullptr;

void
LinuxGamepadService::AddDevice(struct udev_device* dev)
{
  RefPtr<GamepadPlatformService> service =
    GamepadPlatformService::GetParentService();
  if (!service) {
    return;
  }

  const char* devpath = mUdev.udev_device_get_devnode(dev);
  if (!devpath) {
    return;
  }

  // Ensure that this device hasn't already been added.
  for (unsigned int i = 0; i < mGamepads.Length(); i++) {
    if (strcmp(mGamepads[i].devpath, devpath) == 0) {
      return;
    }
  }

  Gamepad gamepad;
  snprintf(gamepad.devpath, sizeof(gamepad.devpath), "%s", devpath);
  GIOChannel* channel = g_io_channel_new_file(devpath, "r", nullptr);
  if (!channel) {
    return;
  }

  g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, nullptr);
  g_io_channel_set_encoding(channel, nullptr, nullptr);
  g_io_channel_set_buffered(channel, FALSE);
  int fd = g_io_channel_unix_get_fd(channel);
  char name[128];
  if (ioctl(fd, JSIOCGNAME(sizeof(name)), &name) == -1) {
    strcpy(name, "unknown");
  }
  const char* vendor_id =
    mUdev.udev_device_get_property_value(dev, "ID_VENDOR_ID");
  const char* model_id =
    mUdev.udev_device_get_property_value(dev, "ID_MODEL_ID");
  if (!vendor_id || !model_id) {
    struct udev_device* parent =
      mUdev.udev_device_get_parent_with_subsystem_devtype(dev,
                                                          "input",
                                                          nullptr);
    if (parent) {
      vendor_id = mUdev.udev_device_get_sysattr_value(parent, "id/vendor");
      model_id = mUdev.udev_device_get_sysattr_value(parent, "id/product");
    }
  }
  snprintf(gamepad.idstring, sizeof(gamepad.idstring),
           "%s-%s-%s",
           vendor_id ? vendor_id : "unknown",
           model_id ? model_id : "unknown",
           name);

  char numAxes = 0, numButtons = 0;
  ioctl(fd, JSIOCGAXES, &numAxes);
  gamepad.numAxes = numAxes;
  ioctl(fd, JSIOCGBUTTONS, &numButtons);
  gamepad.numButtons = numButtons;

  gamepad.index = service->AddGamepad(gamepad.idstring,
                                      mozilla::dom::GamepadMappingType::_empty,
                                      gamepad.numButtons,
                                      gamepad.numAxes);

  gamepad.source_id =
    g_io_add_watch(channel,
                   GIOCondition(G_IO_IN | G_IO_ERR | G_IO_HUP),
                   OnGamepadData,
                   GINT_TO_POINTER(gamepad.index));
  g_io_channel_unref(channel);

  mGamepads.AppendElement(gamepad);
}

void
LinuxGamepadService::RemoveDevice(struct udev_device* dev)
{
  RefPtr<GamepadPlatformService> service =
    GamepadPlatformService::GetParentService();
  if (!service) {
    return;
  }

  const char* devpath = mUdev.udev_device_get_devnode(dev);
  if (!devpath) {
    return;
  }

  for (unsigned int i = 0; i < mGamepads.Length(); i++) {
    if (strcmp(mGamepads[i].devpath, devpath) == 0) {
      g_source_remove(mGamepads[i].source_id);
      service->RemoveGamepad(mGamepads[i].index);
      mGamepads.RemoveElementAt(i);
      break;
    }
  }
}

void
LinuxGamepadService::ScanForDevices()
{
  struct udev_enumerate* en = mUdev.udev_enumerate_new(mUdev.udev);
  mUdev.udev_enumerate_add_match_subsystem(en, "input");
  mUdev.udev_enumerate_scan_devices(en);

  struct udev_list_entry* dev_list_entry;
  for (dev_list_entry = mUdev.udev_enumerate_get_list_entry(en);
       dev_list_entry != nullptr;
       dev_list_entry = mUdev.udev_list_entry_get_next(dev_list_entry)) {
    const char* path = mUdev.udev_list_entry_get_name(dev_list_entry);
    struct udev_device* dev = mUdev.udev_device_new_from_syspath(mUdev.udev,
                                                                 path);
    if (is_gamepad(dev)) {
      AddDevice(dev);
    }

    mUdev.udev_device_unref(dev);
  }

  mUdev.udev_enumerate_unref(en);
}

void
LinuxGamepadService::AddMonitor()
{
  // Add a monitor to watch for device changes
  mMonitor =
    mUdev.udev_monitor_new_from_netlink(mUdev.udev, "udev");
  if (!mMonitor) {
    // Not much we can do here.
    return;
  }
  mUdev.udev_monitor_filter_add_match_subsystem_devtype(mMonitor,
                                                        "input",
							nullptr);

  int monitor_fd = mUdev.udev_monitor_get_fd(mMonitor);
  GIOChannel* monitor_channel = g_io_channel_unix_new(monitor_fd);
  mMonitorSourceID =
    g_io_add_watch(monitor_channel,
                   GIOCondition(G_IO_IN | G_IO_ERR | G_IO_HUP),
                   OnUdevMonitor,
                   nullptr);
  g_io_channel_unref(monitor_channel);

  mUdev.udev_monitor_enable_receiving(mMonitor);
}

void
LinuxGamepadService::RemoveMonitor()
{
  if (mMonitorSourceID) {
    g_source_remove(mMonitorSourceID);
    mMonitorSourceID = 0;
  }
  if (mMonitor) {
    mUdev.udev_monitor_unref(mMonitor);
    mMonitor = nullptr;
  }
}

void
LinuxGamepadService::Startup()
{
  // Don't bother starting up if libudev couldn't be loaded or initialized.
  if (!mUdev)
    return;

  AddMonitor();
  ScanForDevices();
}

void
LinuxGamepadService::Shutdown()
{
  for (unsigned int i = 0; i < mGamepads.Length(); i++) {
    g_source_remove(mGamepads[i].source_id);
  }
  mGamepads.Clear();
  RemoveMonitor();
}

bool
LinuxGamepadService::is_gamepad(struct udev_device* dev)
{
  if (!mUdev.udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK"))
    return false;

  const char* devpath = mUdev.udev_device_get_devnode(dev);
  if (!devpath) {
    return false;
  }
  if (strncmp(kJoystickPath, devpath, sizeof(kJoystickPath) - 1) != 0) {
    return false;
  }

  return true;
}

void
LinuxGamepadService::ReadUdevChange()
{
  struct udev_device* dev =
    mUdev.udev_monitor_receive_device(mMonitor);
  const char* action = mUdev.udev_device_get_action(dev);
  if (is_gamepad(dev)) {
    if (strcmp(action, "add") == 0) {
      AddDevice(dev);
    } else if (strcmp(action, "remove") == 0) {
      RemoveDevice(dev);
    }
  }
  mUdev.udev_device_unref(dev);
}

// static
gboolean
LinuxGamepadService::OnGamepadData(GIOChannel* source,
                                   GIOCondition condition,
                                   gpointer data)
{
  RefPtr<GamepadPlatformService> service =
    GamepadPlatformService::GetParentService();
  if (!service) {
    return TRUE;
  }
  int index = GPOINTER_TO_INT(data);
  //TODO: remove gamepad?
  if (condition & G_IO_ERR || condition & G_IO_HUP)
    return FALSE;

  while (true) {
    struct js_event event;
    gsize count;
    GError* err = nullptr;
    if (g_io_channel_read_chars(source,
				(gchar*)&event,
				sizeof(event),
				&count,
				&err) != G_IO_STATUS_NORMAL ||
	count == 0) {
      break;
    }

    //TODO: store device state?
    if (event.type & JS_EVENT_INIT) {
      continue;
    }

    switch (event.type) {
    case JS_EVENT_BUTTON:
      service->NewButtonEvent(index, event.number, !!event.value);
      break;
    case JS_EVENT_AXIS:
      service->NewAxisMoveEvent(index, event.number,
                                ((float)event.value) / kMaxAxisValue);
      break;
    }
  }

  return TRUE;
}

// static
gboolean
LinuxGamepadService::OnUdevMonitor(GIOChannel* source,
                                   GIOCondition condition,
                                   gpointer data)
{
  if (condition & G_IO_ERR || condition & G_IO_HUP)
    return FALSE;

  gService->ReadUdevChange();
  return TRUE;
}

} // namespace

namespace mozilla {
namespace dom {

void StartGamepadMonitoring()
{
  if (gService) {
    return;
  }
  gService = new LinuxGamepadService();
  gService->Startup();
}

void StopGamepadMonitoring()
{
  if (!gService) {
    return;
  }
  gService->Shutdown();
  delete gService;
  gService = nullptr;
}

} // namespace dom
} // namespace mozilla