File: manette-test.c

package info (click to toggle)
libmanette 0.2.12-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 744 kB
  • sloc: ansic: 4,854; sh: 63; makefile: 7; xml: 6; javascript: 5
file content (303 lines) | stat: -rw-r--r-- 8,675 bytes parent folder | download | duplicates (2)
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
/* manette-test.c
 *
 * Copyright (C) 2017 Adrien Plazas <kekun.plazas@laposte.net>
 *
 * This file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib/gprintf.h>
#include <libmanette.h>
#include <linux/input-event-codes.h>

#define CASE_THEN_STRING(x) case x: return #x;

const char *
get_absolute_name (guint16 axis)
{
  switch (axis) {
  CASE_THEN_STRING (ABS_X)
  CASE_THEN_STRING (ABS_Y)
  CASE_THEN_STRING (ABS_RX)
  CASE_THEN_STRING (ABS_RY)
  default:
    return NULL;
  }
}

const char *
get_button_name (guint16 button)
{
  switch (button) {
  CASE_THEN_STRING (BTN_A)
  CASE_THEN_STRING (BTN_B)
  CASE_THEN_STRING (BTN_C)
  CASE_THEN_STRING (BTN_X)
  CASE_THEN_STRING (BTN_Y)
  CASE_THEN_STRING (BTN_Z)
  CASE_THEN_STRING (BTN_TL)
  CASE_THEN_STRING (BTN_TR)
  CASE_THEN_STRING (BTN_TL2)
  CASE_THEN_STRING (BTN_TR2)
  CASE_THEN_STRING (BTN_SELECT)
  CASE_THEN_STRING (BTN_START)
  CASE_THEN_STRING (BTN_MODE)
  CASE_THEN_STRING (BTN_THUMBL)
  CASE_THEN_STRING (BTN_THUMBR)
  CASE_THEN_STRING (BTN_DPAD_UP)
  CASE_THEN_STRING (BTN_DPAD_DOWN)
  CASE_THEN_STRING (BTN_DPAD_LEFT)
  CASE_THEN_STRING (BTN_DPAD_RIGHT)
  CASE_THEN_STRING (BTN_TRIGGER_HAPPY1)
  CASE_THEN_STRING (BTN_TRIGGER_HAPPY2)
  CASE_THEN_STRING (BTN_TRIGGER_HAPPY3)
  CASE_THEN_STRING (BTN_TRIGGER_HAPPY4)
  CASE_THEN_STRING (BTN_TRIGGER_HAPPY5)
  CASE_THEN_STRING (BTN_TRIGGER_HAPPY6)
  CASE_THEN_STRING (BTN_TRIGGER_HAPPY7)
  CASE_THEN_STRING (BTN_TRIGGER_HAPPY8)
  CASE_THEN_STRING (BTN_TRIGGER_HAPPY9)
  CASE_THEN_STRING (BTN_TRIGGER_HAPPY10)
  CASE_THEN_STRING (BTN_TRIGGER_HAPPY11)
  default:
    return NULL;
  }
}

const char *
get_hat_name (guint16 axis)
{
  switch (axis) {
  CASE_THEN_STRING (ABS_HAT0X)
  CASE_THEN_STRING (ABS_HAT0Y)
  CASE_THEN_STRING (ABS_HAT1X)
  CASE_THEN_STRING (ABS_HAT1Y)
  CASE_THEN_STRING (ABS_HAT2X)
  CASE_THEN_STRING (ABS_HAT2Y)
  CASE_THEN_STRING (ABS_HAT3X)
  CASE_THEN_STRING (ABS_HAT3Y)
  default:
    return NULL;
  }
}

static void
device_disconnected_cb (ManetteDevice *emitter,
                        gpointer       user_data)
{
  g_printf ("%s: disconnected\n", manette_device_get_name (emitter));
  g_object_unref (emitter);
}

static void
absolute_axis_event_cb (ManetteDevice *emitter,
                        ManetteEvent  *event,
                        gpointer       user_data)
{
  ManetteDevice *device;
  const char *device_name;
  const char *absolute_axis_name;
  guint16 absolute_axis;
  double value;

  if (!manette_event_get_absolute (event, &absolute_axis, &value))
    return;

  device = manette_event_get_device (event);
  device_name = manette_device_get_name (device);
  absolute_axis_name = get_absolute_name (absolute_axis);

  if (absolute_axis_name != NULL)
    g_printf ("%s: Absolute axis %s moved to %lf\n", device_name, absolute_axis_name, value);
  else
    g_printf ("%s: Unknown absolute axis %u moved to %lf\n", device_name, absolute_axis, value);
}

static void
button_press_event_cb (ManetteDevice *emitter,
                       ManetteEvent  *event,
                       gpointer       user_data)
{
  ManetteDevice *device;
  const char *device_name;
  const char *button_name;
  guint16 button;

  if (!manette_event_get_button (event, &button))
    return;

  device = manette_event_get_device (event);
  device_name = manette_device_get_name (device);
  button_name = get_button_name (button);

  if (button_name != NULL)
    g_printf ("%s: Button %s pressed\n", device_name, button_name);
  else
    g_printf ("%s: Unknown button %u pressed\n", device_name, button);
}

static void
button_release_event_cb (ManetteDevice *emitter,
                         ManetteEvent  *event,
                         gpointer       user_data)
{
  ManetteDevice *device;
  const char *device_name;
  const char *button_name;
  guint16 button;

  if (!manette_event_get_button (event, &button))
    return;

  device = manette_event_get_device (event);
  device_name = manette_device_get_name (device);
  button_name = get_button_name (button);

  if (button_name != NULL)
    g_printf ("%s: Button %s released\n", device_name, button_name);
  else
    g_printf ("%s: Unknown button %u released\n", device_name, button);
}

static void
hat_axis_event_cb (ManetteDevice *emitter,
                   ManetteEvent  *event,
                   gpointer       user_data)
{
  ManetteDevice *device;
  const char *device_name;
  const char *hat_axis_name;
  guint16 hat_axis;
  gint8 value;

  if (!manette_event_get_hat (event, &hat_axis, &value))
    return;

  device = manette_event_get_device (event);
  device_name = manette_device_get_name (device);
  hat_axis_name = get_hat_name (hat_axis);

  if (hat_axis_name != NULL)
    g_printf ("%s: Hat axis %s moved to %d\n", device_name, hat_axis_name, value);
  else
    g_printf ("%s: Unknown hat axis %u moved to %d\n", device_name, hat_axis, value);
}

#define PHASES 4

typedef struct {
  ManetteDevice *device;
  guint8 phase;
} RumbleData;

typedef struct {
  guint16 strong_magnitude;
  guint16 weak_magnitude;
  guint16 duration_ms;
  guint16 wait_ms;
} RumblePhase;

gboolean
rumble (RumbleData *data)
{
  static const RumblePhase phases[] = {
    { G_MAXUINT16/16, G_MAXUINT16/2, 200, 300 },
    { G_MAXUINT16/16, G_MAXUINT16/2, 200, 1300 },
    { G_MAXUINT16/8, G_MAXUINT16/16, 200, 300 },
    { G_MAXUINT16/8, G_MAXUINT16/16, 200, 1800 },
  };

  if (!MANETTE_IS_DEVICE (data->device))
    return FALSE;

  manette_device_rumble (data->device,
                         phases[data->phase].strong_magnitude,
                         phases[data->phase].weak_magnitude,
                         phases[data->phase].duration_ms);
  g_timeout_add (phases[data->phase].wait_ms, (GSourceFunc) rumble, data);
  data->phase = (data->phase + 1) % PHASES;

  return FALSE;
}

static void
listen_to_device (ManetteDevice *device)
{
  RumbleData *rumble_data;

  g_printf ("%s: connected\n", manette_device_get_name (device));
  g_signal_connect_object (G_OBJECT (device),
                           "disconnected",
                           (GCallback) device_disconnected_cb,
                           NULL,
                           0);
  g_signal_connect_object (G_OBJECT (device),
                           "absolute-axis-event",
                           (GCallback) absolute_axis_event_cb,
                           NULL,
                           0);
  g_signal_connect_object (G_OBJECT (device),
                           "button-press-event",
                           (GCallback) button_press_event_cb,
                           NULL,
                           0);
  g_signal_connect_object (G_OBJECT (device),
                           "button-release-event",
                           (GCallback) button_release_event_cb,
                           NULL,
                           0);
  g_signal_connect_object (G_OBJECT (device),
                           "hat-axis-event",
                           (GCallback) hat_axis_event_cb,
                           NULL,
                           0);
  if (manette_device_has_rumble (device)) {
    rumble_data = g_new0 (RumbleData, 1);
    rumble_data->device = g_object_ref (device);
    rumble (rumble_data);
  }
}

static void
device_connected_cb (ManetteMonitor *emitter,
                     ManetteDevice  *device,
                     gpointer        user_data)
{
  listen_to_device (device);
}

int
main (int    argc,
      char **argv)
{
  g_autoptr (GMainLoop) main_loop = NULL;
  g_autoptr (ManetteMonitor) monitor = NULL;
  g_autoptr (ManetteMonitorIter) iter = NULL;
  ManetteDevice *device;

  monitor = manette_monitor_new ();
  g_signal_connect_object (G_OBJECT (monitor),
                           "device-connected",
                           (GCallback) device_connected_cb,
                           NULL,
                           0);

  iter = manette_monitor_iterate (monitor);
  while (manette_monitor_iter_next (iter, &device))
    listen_to_device (device);

  main_loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (main_loop);

  return 0;
}