aboutsummaryrefslogtreecommitdiff
path: root/rootston/input.c
blob: b7a5f1ba136be81d57114f23dcd45c9d44d76c83 (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
#define _POSIX_C_SOURCE 200112L
#include <assert.h>
#include <stdlib.h>
#include <time.h>
#include <wayland-server.h>
#include <wlr/backend/libinput.h>
#include <wlr/config.h>
#include <wlr/types/wlr_cursor.h>
#include <wlr/util/log.h>
#include <wlr/xcursor.h>
#ifdef WLR_HAS_XWAYLAND
#include <wlr/xwayland.h>
#endif
#include "rootston/config.h"
#include "rootston/input.h"
#include "rootston/keyboard.h"
#include "rootston/seat.h"
#include "rootston/server.h"

static const char *device_type(enum wlr_input_device_type type) {
	switch (type) {
	case WLR_INPUT_DEVICE_KEYBOARD:
		return "keyboard";
	case WLR_INPUT_DEVICE_POINTER:
		return "pointer";
	case WLR_INPUT_DEVICE_TOUCH:
		return "touch";
	case WLR_INPUT_DEVICE_TABLET_TOOL:
		return "tablet tool";
	case WLR_INPUT_DEVICE_TABLET_PAD:
		return "tablet pad";
	}
	return NULL;
}

struct roots_seat *input_get_seat(struct roots_input *input, char *name) {
	struct roots_seat *seat = NULL;
	wl_list_for_each(seat, &input->seats, link) {
		if (strcmp(seat->seat->name, name) == 0) {
			return seat;
		}
	}

	seat = roots_seat_create(input, name);
	return seat;
}

static void handle_new_input(struct wl_listener *listener, void *data) {
	struct wlr_input_device *device = data;
	struct roots_input *input = wl_container_of(listener, input, new_input);

	char *seat_name = ROOTS_CONFIG_DEFAULT_SEAT_NAME;
	struct roots_device_config *dc =
		roots_config_get_device(input->config, device);
	if (dc) {
		seat_name = dc->seat;
	}

	struct roots_seat *seat = input_get_seat(input, seat_name);
	if (!seat) {
		wlr_log(WLR_ERROR, "could not create roots seat");
		return;
	}

	wlr_log(WLR_DEBUG, "New input device: %s (%d:%d) %s seat:%s", device->name,
			device->vendor, device->product, device_type(device->type), seat_name);

	roots_seat_add_device(seat, device);

	if (dc && wlr_input_device_is_libinput(device)) {
		struct libinput_device *libinput_dev =
			wlr_libinput_get_device_handle(device);

		wlr_log(WLR_DEBUG, "input has config, tap_enabled: %d\n", dc->tap_enabled);
		if (dc->tap_enabled) {
			libinput_device_config_tap_set_enabled(libinput_dev,
					LIBINPUT_CONFIG_TAP_ENABLED);
		}
	}
}

struct roots_input *input_create(struct roots_server *server,
		struct roots_config *config) {
	wlr_log(WLR_DEBUG, "Initializing roots input");
	assert(server->desktop);

	struct roots_input *input = calloc(1, sizeof(struct roots_input));
	if (input == NULL) {
		return NULL;
	}

	input->config = config;
	input->server = server;

	wl_list_init(&input->seats);

	input->new_input.notify = handle_new_input;
	wl_signal_add(&server->backend->events.new_input, &input->new_input);

	return input;
}

void input_destroy(struct roots_input *input) {
	// TODO
}

struct roots_seat *input_seat_from_wlr_seat(struct roots_input *input,
		struct wlr_seat *wlr_seat) {
	struct roots_seat *seat = NULL;
	wl_list_for_each(seat, &input->seats, link) {
		if (seat->seat == wlr_seat) {
			return seat;
		}
	}
	return seat;
}

bool input_view_has_focus(struct roots_input *input, struct roots_view *view) {
	if (!view) {
		return false;
	}
	struct roots_seat *seat;
	wl_list_for_each(seat, &input->seats, link) {
		if (view == roots_seat_get_focus(seat)) {
			return true;
		}
	}

	return false;
}

static inline int64_t timespec_to_msec(const struct timespec *a) {
	return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000;
}

void input_update_cursor_focus(struct roots_input *input) {
	struct roots_seat *seat;
	struct timespec now;
	wl_list_for_each(seat, &input->seats, link) {
		clock_gettime(CLOCK_MONOTONIC, &now);
		roots_cursor_update_position(seat->cursor, timespec_to_msec(&now));
	}
}