aboutsummaryrefslogtreecommitdiffhomepage
path: root/backends/evdev.c
blob: 8f7c4f98ff1aefbc836cf735d04382c96eb40a44 (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
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
#define BACKEND_NAME "evdev"

#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <libevdev/libevdev.h>
#include <limits.h>
#include <sys/types.h>
#include <dirent.h>
#include <linux/input.h>
#ifndef EVDEV_NO_UINPUT
#include <libevdev/libevdev-uinput.h>
#endif

#include "midimonster.h"
#include "evdev.h"

static struct {
	uint8_t detect;
} evdev_config = {
	.detect = 0
};

MM_PLUGIN_API int init(){
	backend evdev = {
		.name = BACKEND_NAME,
		.conf = evdev_configure,
		.create = evdev_instance,
		.conf_instance = evdev_configure_instance,
		.channel = evdev_channel,
		.handle = evdev_set,
		.process = evdev_handle,
		.start = evdev_start,
		.shutdown = evdev_shutdown
	};

	if(sizeof(evdev_channel_ident) != sizeof(uint64_t)){
		LOG("Channel identification union out of bounds");
		return 1;
	}

	if(mm_backend_register(evdev)){
		LOG("Failed to register backend");
		return 1;
	}

	return 0;
}

static int evdev_configure(char* option, char* value) {
	if(!strcmp(option, "detect")){
		evdev_config.detect = 1;
		if(!strcmp(value, "off")){
			evdev_config.detect = 0;
		}
		return 0;
	}

	LOGPF("Unknown backend configuration option %s", option);
	return 1;
}

static int evdev_instance(instance* inst){
	evdev_instance_data* data = calloc(1, sizeof(evdev_instance_data));
	if(!data){
		LOG("Failed to allocate memory");
		return 1;
	}

	data->input_fd = -1;
#ifndef EVDEV_NO_UINPUT
	data->output_proto = libevdev_new();
	if(!data->output_proto){
		LOG("Failed to initialize libevdev output prototype device");
		free(data);
		return 1;
	}
#endif

	inst->impl = data;
	return 0;
}

static int evdev_attach(instance* inst, evdev_instance_data* data, char* node){
	if(data->input_fd >= 0){
		LOGPF("Instance %s already assigned an input device", inst->name);
		return 1;
	}

	data->input_fd = open(node, O_RDONLY | O_NONBLOCK);
	if(data->input_fd < 0){
		LOGPF("Failed to open input device node %s: %s", node, strerror(errno));
		return 1;
	}

	if(libevdev_new_from_fd(data->input_fd, &data->input_ev)){
		LOGPF("Failed to initialize libevdev for %s", node);
		close(data->input_fd);
		data->input_fd = -1;
		return 1;
	}

	if(data->exclusive && libevdev_grab(data->input_ev, LIBEVDEV_GRAB)){
		LOGPF("Failed to obtain exclusive device access on %s", node);
	}

	return 0;
}

static char* evdev_find(char* name){
	int fd = -1;
	struct dirent* file = NULL;
	char file_path[PATH_MAX * 2], *result = calloc(PATH_MAX * 2, sizeof(char));
	DIR* nodes = opendir(INPUT_NODES);
	char device_name[UINPUT_MAX_NAME_SIZE];
	size_t min_distance = -1, distance = 0;

	if(!nodes){
		LOGPF("Failed to query input device nodes in %s: %s", INPUT_NODES, strerror(errno));
		free(result);
		return NULL;
	}

	for(file = readdir(nodes); file; file = readdir(nodes)){
		if(!strncmp(file->d_name, INPUT_PREFIX, strlen(INPUT_PREFIX)) && file->d_type == DT_CHR){
			snprintf(file_path, sizeof(file_path), "%s/%s", INPUT_NODES, file->d_name);

			fd = open(file_path, O_RDONLY);
			if(fd < 0){
				LOGPF("Failed to access %s: %s", file_path, strerror(errno));
				continue;
			}

			if(ioctl(fd, EVIOCGNAME(sizeof(device_name)), device_name) < 0){
				LOGPF("Failed to read name for %s: %s", file_path, strerror(errno));
				close(fd);
				continue;
			}

			close(fd);

			if(!strncmp(device_name, name, strlen(name))){
				distance = strlen(device_name) - strlen(name);
				LOGPF("Matched name %s as candidate (distance %" PRIsize_t ") for %s: %s", device_name, distance, name, file_path);
				if(distance < min_distance){
					strncpy(result, file_path, (PATH_MAX * 2) - 1);
					min_distance = distance;
				}
			}
		}
	}

	closedir(nodes);

	if(!result[0]){
		free(result);
		return NULL;
	}
	LOGPF("Using %s for input name %s", result, name);
	return result;
}

static int evdev_configure_instance(instance* inst, char* option, char* value) {
	evdev_instance_data* data = (evdev_instance_data*) inst->impl;
	char* next_token = NULL;
#ifndef EVDEV_NO_UINPUT
	struct input_absinfo abs_info = {
		0
	};
#endif

	if(!strcmp(option, "device")){
		return evdev_attach(inst, data, value);
	}
	else if(!strcmp(option, "input")){
		next_token = evdev_find(value);
		if(!next_token){
			LOGPF("Failed to match input device with name %s for instance %s", value, inst->name);
			return 1;
		}
		if(evdev_attach(inst, data, next_token)){
			free(next_token);
			return 1;
		}
		free(next_token);
		return 0;
	}
	else if(!strcmp(option, "exclusive")){
		if(data->input_fd >= 0 && libevdev_grab(data->input_ev, LIBEVDEV_GRAB)){
			LOGPF("Failed to obtain exclusive device access on %s", inst->name);
		}
		data->exclusive = 1;
		return 0;
	}
	else if(!strncmp(option, "relaxis.", 8)){
		data->relative_axis = realloc(data->relative_axis, (data->relative_axes + 1) * sizeof(evdev_relaxis_config));
		if(!data->relative_axis){
			LOG("Failed to allocate memory");
			return 1;
		}
		data->relative_axis[data->relative_axes].inverted = 0;
		data->relative_axis[data->relative_axes].code = libevdev_event_code_from_name(EV_REL, option + 8);
		data->relative_axis[data->relative_axes].max = strtoll(value, &next_token, 0);
		if(data->relative_axis[data->relative_axes].max < 0){
			data->relative_axis[data->relative_axes].max *= -1;
			data->relative_axis[data->relative_axes].inverted = 1;
		}
		else if(data->relative_axis[data->relative_axes].max == 0){
			LOGPF("Relative axis configuration for %s.%s has invalid range", inst->name, option + 8);
		}
		//this does not crash on single-integer `value`s because strtoll sets `next_token` to the terminator
		data->relative_axis[data->relative_axes].current = strtoul(next_token, NULL, 0);
		if(data->relative_axis[data->relative_axes].code < 0){
			LOGPF("Failed to configure relative axis extents for %s.%s", inst->name, option + 8);
			return 1;
		}
		data->relative_axes++;
		return 0;
	}
#ifndef EVDEV_NO_UINPUT
	else if(!strcmp(option, "output")){
		data->output_enabled = 1;
		libevdev_set_name(data->output_proto, value);
		return 0;
	}
	else if(!strcmp(option, "id")){
		next_token = value;
		libevdev_set_id_vendor(data->output_proto, strtol(next_token, &next_token, 0));
		libevdev_set_id_product(data->output_proto, strtol(next_token, &next_token, 0));
		libevdev_set_id_version(data->output_proto, strtol(next_token, &next_token, 0));
		return 0;
	}
	else if(!strncmp(option, "axis.", 5)){
		//value minimum maximum fuzz flat resolution
		next_token = value;
		abs_info.value = strtol(next_token, &next_token, 0);
		abs_info.minimum = strtol(next_token, &next_token, 0);
		abs_info.maximum = strtol(next_token, &next_token, 0);
		abs_info.fuzz = strtol(next_token, &next_token, 0);
		abs_info.flat = strtol(next_token, &next_token, 0);
		abs_info.resolution = strtol(next_token, &next_token, 0);
		if(libevdev_enable_event_code(data->output_proto, EV_ABS, libevdev_event_code_from_name(EV_ABS, option + 5), &abs_info)){
			LOGPF("Failed to enable absolute axis %s.%s for output", inst->name, option + 5);
			return 1;
		}
		return 0;
	}
#endif
	LOGPF("Unknown instance configuration parameter %s for instance %s", option, inst->name);
	return 1;
}

static channel* evdev_channel(instance* inst, char* spec, uint8_t flags){
#ifndef EVDEV_NO_UINPUT
	evdev_instance_data* data = (evdev_instance_data*) inst->impl;
#endif
	char* separator = strchr(spec, '.');
	evdev_channel_ident ident = {
		.label = 0
	};

	if(!separator){
		LOGPF("Invalid channel specification %s", spec);
		return NULL;
	}

	*(separator++) = 0;

	if(libevdev_event_type_from_name(spec) < 0){
		LOGPF("Invalid type specification: %s", spec);
		return NULL;
	}
	ident.fields.type = libevdev_event_type_from_name(spec);

	if(libevdev_event_code_from_name(ident.fields.type, separator) >= 0){
		ident.fields.code = libevdev_event_code_from_name(ident.fields.type, separator);
	}
	else{
		LOGPF("Code name not recognized, using as number: %s.%s", inst->name, separator);
		ident.fields.code = strtoul(separator, NULL, 10);
	}

#ifndef EVDEV_NO_UINPUT
	if(data->output_enabled){
		if(!libevdev_has_event_code(data->output_proto, ident.fields.type, ident.fields.code)){
			//enable the event on the device
			//the previous check is necessary to not fail while enabling axes, which require additional information
			if(libevdev_enable_event_code(data->output_proto, ident.fields.type, ident.fields.code, NULL)){
				LOGPF("Failed to enable output event %s.%s%s",
						libevdev_event_type_get_name(ident.fields.type),
						libevdev_event_code_get_name(ident.fields.type, ident.fields.code),
						(ident.fields.type == EV_ABS) ? ": To output absolute axes, specify their details in the configuration":"");
				return NULL;
			}
		}
	}
#endif

	return mm_channel(inst, ident.label, 1);
}

static int evdev_push_event(instance* inst, evdev_instance_data* data, struct input_event event){
	uint64_t range = 0;
	channel_value val;
	evdev_channel_ident ident = {
		.fields.type = event.type,
		.fields.code = event.code
	};
	channel* chan = mm_channel(inst, ident.label, 0);
	size_t axis;

	if(chan){
		val.raw.u64 = event.value;
		switch(event.type){
			case EV_REL:
				for(axis = 0; axis < data->relative_axes; axis++){
					if(data->relative_axis[axis].code == event.code){
						if(data->relative_axis[axis].inverted){
							event.value *= -1;
						}
						data->relative_axis[axis].current = clamp(data->relative_axis[axis].current + event.value, data->relative_axis[axis].max, 0);
						val.normalised = (double) data->relative_axis[axis].current / (double) data->relative_axis[axis].max;
						break;
					}
				}
				if(axis == data->relative_axes){
					val.normalised = 0.5 + ((event.value < 0) ? 0.5 : -0.5);
					break;
				}
				break;
			case EV_ABS:
				range = libevdev_get_abs_maximum(data->input_ev, event.code) - libevdev_get_abs_minimum(data->input_ev, event.code);
				val.normalised = clamp((event.value - libevdev_get_abs_minimum(data->input_ev, event.code)) / (double) range, 1.0, 0.0);
				break;
			case EV_KEY:
			case EV_SW:
			default:
				val.normalised = clamp(1.0 * event.value, 1.0, 0.0);
				break;
		}

		if(mm_channel_event(chan, val)){
			LOG("Failed to push channel event to core");
			return 1;
		}
	}

	if(evdev_config.detect){
		LOGPF("Incoming data for channel %s.%s.%s", inst->name, libevdev_event_type_get_name(event.type), libevdev_event_code_get_name(event.type, event.code));
	}

	return 0;
}

static int evdev_handle(size_t num, managed_fd* fds){
	instance* inst = NULL;
	evdev_instance_data* data = NULL;
	size_t fd;
	unsigned int read_flags = LIBEVDEV_READ_FLAG_NORMAL;
	int read_status;
	struct input_event ev;

	for(fd = 0; fd < num; fd++){
		inst = (instance*) fds[fd].impl;
		if(!inst){
			LOG("Signaled for unknown FD");
			continue;
		}

		data = (evdev_instance_data*) inst->impl;

		for(read_status = libevdev_next_event(data->input_ev, read_flags, &ev);
				read_status == LIBEVDEV_READ_STATUS_SUCCESS || read_status == LIBEVDEV_READ_STATUS_SYNC;
				read_status = libevdev_next_event(data->input_ev, read_flags, &ev)){
			read_flags = LIBEVDEV_READ_FLAG_NORMAL;
			if(read_status == LIBEVDEV_READ_STATUS_SYNC){
				read_flags = LIBEVDEV_READ_FLAG_SYNC;
			}

			//exclude synchronization events
			if(ev.type == EV_SYN){
				continue;
			}

			//handle event
			if(evdev_push_event(inst, data, ev)){
				return 1;
			}
		}

		if(read_status != -EAGAIN){
			LOGPF("Failed to handle events: %s\n", strerror(-read_status));
			return 1;
		}
	}

	return 0;
}

static int evdev_start(size_t n, instance** inst){
	size_t u, fds = 0;
	evdev_instance_data* data = NULL;

	for(u = 0; u < n; u++){
		data = (evdev_instance_data*) inst[u]->impl;

#ifndef EVDEV_NO_UINPUT
		if(data->output_enabled){
			if(libevdev_uinput_create_from_device(data->output_proto, LIBEVDEV_UINPUT_OPEN_MANAGED, &data->output_ev)){
				LOGPF("Failed to create output device: %s", strerror(errno));
				return 1;
			}
			LOGPF("Created device node %s for instance %s", libevdev_uinput_get_devnode(data->output_ev), inst[u]->name);
		}
#endif

		inst[u]->ident = data->input_fd;
		if(data->input_fd >= 0){
			if(mm_manage_fd(data->input_fd, BACKEND_NAME, 1, inst[u])){
				LOGPF("Failed to register input descriptor for instance %s", inst[u]->name);
				return 1;
			}
			fds++;
		}

		if(data->input_fd <= 0 && !data->output_ev){
			LOGPF("Instance %s has neither input nor output device set up", inst[u]->name);
		}

	}

	LOGPF("Registered %zu descriptors to core", fds);
	return 0;
}

static int evdev_set(instance* inst, size_t num, channel** c, channel_value* v) {
#ifndef EVDEV_NO_UINPUT
	size_t evt = 0, axis = 0;
	evdev_instance_data* data = (evdev_instance_data*) inst->impl;
	evdev_channel_ident ident = {
		.label = 0
	};
	int32_t value = 0;
	uint64_t range = 0;

	if(!data->output_enabled){
		LOGPF("Instance %s not enabled for output (%" PRIsize_t " channel events)", inst->name, num);
		return 0;
	}

	for(evt = 0; evt < num; evt++){
		ident.label = c[evt]->ident;

		switch(ident.fields.type){
			case EV_REL:
				for(axis = 0; axis < data->relative_axes; axis++){
					if(data->relative_axis[axis].code == ident.fields.code){
						value = (v[evt].normalised * data->relative_axis[axis].max) - data->relative_axis[axis].current;
						data->relative_axis[axis].current = v[evt].normalised * data->relative_axis[axis].max;

						if(data->relative_axis[axis].inverted){
							value *= -1;
						}
						break;
					}
				}
				if(axis == data->relative_axes){
					value = (v[evt].normalised < 0.5) ? -1 : ((v[evt].normalised > 0.5) ? 1 : 0);
				}
				break;
			case EV_ABS:
				range = libevdev_get_abs_maximum(data->output_proto, ident.fields.code) - libevdev_get_abs_minimum(data->output_proto, ident.fields.code);
				value = (range * v[evt].normalised) + libevdev_get_abs_minimum(data->output_proto, ident.fields.code);
				break;
			case EV_KEY:
			case EV_SW:
			default:
				value = (v[evt].normalised > 0.9) ? 1 : 0;
				break;
		}

		if(libevdev_uinput_write_event(data->output_ev, ident.fields.type, ident.fields.code, value)){
			LOGPF("Failed to output event on instance %s", inst->name);
			return 1;
		}
	}

	//send syn event to publish all events
	if(libevdev_uinput_write_event(data->output_ev, EV_SYN, SYN_REPORT, 0)){
		LOGPF("Failed to output sync event on instance %s", inst->name);
		return 1;
	}

	return 0;
#else
	LOG("No output support on this platform");
	return 1;
#endif
}

static int evdev_shutdown(size_t n, instance** inst){
	evdev_instance_data* data = NULL;
	size_t u;

	for(u = 0; u < n; u++){
		data = (evdev_instance_data*) inst[u]->impl;

		if(data->input_fd >= 0){
			libevdev_free(data->input_ev);
			close(data->input_fd);
		}

#ifndef EVDEV_NO_UINPUT
		if(data->output_enabled){
			libevdev_uinput_destroy(data->output_ev);
		}

		libevdev_free(data->output_proto);
#endif
		data->relative_axes = 0;
		free(data->relative_axis);
		free(inst[u]->impl);
	}

	LOG("Backend shut down");
	return 0;
}