summaryrefslogtreecommitdiff
path: root/src/input.c
blob: ec8a106bd2ba8d33884d7538e03be11edc237cc2 (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
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "Xlib-wayland.h"

#include "private.h"

struct csx_seat {
	struct wl_seat *seat;
	struct wl_pointer *pointer;
	struct csx_display *display;
	struct csx_window *pointer_focus;
	uint32_t button_mask;
	wl_fixed_t pointer_x, pointer_y;
};

void
csx_seat_send_cronssing_event(struct csx_seat *seat, int type)
{
	struct csx_display *display = seat->display;
	struct csx_event *event;

	event = malloc(sizeof *event);

	event->xevent.xmap.type = type;
	event->xevent.xmap.serial = display->serial;

	wl_list_insert(display->event_list.prev, &event->link);
}

void
csx_seat_send_button_event(struct csx_seat *seat, int button, int type)
{
	struct csx_display *display = seat->display;
	struct csx_event *event;

	event = malloc(sizeof *event);

	event->xevent.xmap.type = type;
	event->xevent.xmap.serial = display->serial;

	wl_list_insert(display->event_list.prev, &event->link);
}

void
csx_seat_send_motion(struct csx_seat *seat,
		     uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
{
	struct csx_display *display = seat->display;
	struct csx_event *event;

	event = malloc(sizeof *event);

	event->xevent.xmap.type = MotionNotify;
	event->xevent.xmap.serial = display->serial;

	wl_list_insert(display->event_list.prev, &event->link);
}

static void
pointer_handle_enter(void *data, struct wl_pointer *pointer,
		     uint32_t serial, struct wl_surface *surface,
		     wl_fixed_t sx, wl_fixed_t sy)
{
	struct csx_seat *seat = data;
	struct csx_window *window;

	seat->pointer_focus = wl_surface_get_user_data(surface);
	window = seat->pointer_focus;
	if (window->event_mask & EnterWindowMask)
		csx_seat_send_cronssing_event(seat, EnterNotify);
}

static void
pointer_handle_leave(void *data, struct wl_pointer *pointer,
		     uint32_t serial, struct wl_surface *surface)
{
	struct csx_seat *seat = data;
	struct csx_window *window;

	seat->pointer_focus = wl_surface_get_user_data(surface);
	window = seat->pointer_focus;
	if (window->event_mask & EnterWindowMask)
		csx_seat_send_cronssing_event(seat, EnterNotify);
}

static void
pointer_handle_motion(void *data, struct wl_pointer *pointer,
		      uint32_t time, wl_fixed_t x, wl_fixed_t y)
{
	struct csx_seat *seat = data;
	struct csx_window *window;

	window = seat->pointer_focus;
	seat->pointer_x = x;
	seat->pointer_y = y;
	if ((window->event_mask & PointerMotionMask) &&
	    seat->button_mask == 0)
		csx_seat_send_motion(seat, time, x, y);
	else if ((window->event_mask & Button1MotionMask) &&
		 seat->button_mask == 1)
		csx_seat_send_motion(seat, time, x, y);
}

static void
pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
		      uint32_t serial, uint32_t time, uint32_t button,
		      uint32_t state)
{
	struct csx_seat *seat = data;
	struct csx_window *window;
	uint32_t mask;

	mask = 1 << (button - 272);
	if (state)
		seat->button_mask |= mask;
	else
		seat->button_mask &= ~mask;

	window = seat->pointer_focus;
	if (state && (window->event_mask & ButtonPressMask))
		csx_seat_send_button_event(seat, button, ButtonPress);
	else if (!state && (window->event_mask & ButtonPressMask))
		csx_seat_send_button_event(seat, button, ButtonRelease);
}

static void
pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
		    uint32_t time, uint32_t axis, wl_fixed_t value)
{
}

static const struct wl_pointer_listener pointer_listener = {
	pointer_handle_enter,
	pointer_handle_leave,
	pointer_handle_motion,
	pointer_handle_button,
	pointer_handle_axis,
};

static void
seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
			 enum wl_seat_capability caps)
{
	struct csx_seat *seat = data;

	if ((caps & WL_SEAT_CAPABILITY_POINTER) && !seat->pointer) {
		seat->pointer = wl_seat_get_pointer(seat->seat);
		wl_pointer_add_listener(seat->pointer, &pointer_listener, seat);
	}
}

static const struct wl_seat_listener seat_listener = {
	seat_handle_capabilities,
};

void
csx_display_add_seat(struct csx_display *display,
		     uint32_t name, uint32_t version)
{
	struct csx_seat *seat;

	seat = malloc(sizeof *seat);

	memset(seat, 0, sizeof *seat);
	seat->display = display;
	seat->seat = wl_registry_bind(display->registry, name,
				      &wl_seat_interface, 1);
	wl_seat_add_listener(seat->seat, &seat_listener, seat);
}