aboutsummaryrefslogtreecommitdiff
path: root/src/cursor.zig
blob: 7582547864e6a0e90b36fe3472242b8dc9594ccb (plain) (blame)
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
const std = @import("std");
const c = @import("c.zig");

const Seat = @import("seat.zig").Seat;
const View = @import("view.zig").View;

const CursorMode = enum {
    Passthrough,
    Move,
    Resize,
};

pub const Cursor = struct {
    const Self = @This();

    seat: *Seat,
    wlr_cursor: *c.wlr_cursor,
    wlr_xcursor_manager: *c.wlr_xcursor_manager,

    mode: CursorMode,
    grabbed_view: ?*View,
    grab_x: f64,
    grab_y: f64,
    grab_width: c_int,
    grab_height: c_int,
    resize_edges: u32,

    listen_axis: c.wl_listener,
    listen_button: c.wl_listener,
    listen_frame: c.wl_listener,
    listen_motion_absolute: c.wl_listener,
    listen_motion: c.wl_listener,
    listen_request_set_cursor: c.wl_listener,

    pub fn init(self: *Self, seat: *Seat) !void {
        self.seat = seat;

        // Creates a wlroots utility for tracking the cursor image shown on screen.
        //
        // TODO: free this, it allocates!
        self.wlr_cursor = c.wlr_cursor_create() orelse
            return error.CantCreateWlrCursor;

        // Creates an xcursor manager, another wlroots utility which loads up
        // Xcursor themes to source cursor images from and makes sure that cursor
        // images are available at all scale factors on the screen (necessary for
        // HiDPI support). We add a cursor theme at scale factor 1 to begin with.
        //
        // TODO: free this, it allocates!
        self.wlr_xcursor_manager = c.wlr_xcursor_manager_create(null, 24) orelse
            return error.CantCreateWlrXCursorManager;

        c.wlr_cursor_attach_output_layout(self.wlr_cursor, seat.input_manager.server.root.wlr_output_layout);
        _ = c.wlr_xcursor_manager_load(self.wlr_xcursor_manager, 1);

        self.mode = CursorMode.Passthrough;
        self.grabbed_view = null;
        self.grab_x = 0.0;
        self.grab_y = 0.0;
        self.grab_width = 0;
        self.grab_height = 0;
        self.resize_edges = 0;

        // wlr_cursor *only* displays an image on screen. It does not move around
        // when the pointer moves. However, we can attach input devices to it, and
        // it will generate aggregate events for all of them. In these events, we
        // can choose how we want to process them, forwarding them to clients and
        // moving the cursor around. See following post for more detail:
        // https://drewdevault.com/2018/07/17/Input-handling-in-wlroots.html
        self.listen_axis.notify = handleAxis;
        c.wl_signal_add(&self.wlr_cursor.events.axis, &self.listen_axis);

        self.listen_button.notify = handleButton;
        c.wl_signal_add(&self.wlr_cursor.events.button, &self.listen_button);

        self.listen_frame.notify = handleFrame;
        c.wl_signal_add(&self.wlr_cursor.events.frame, &self.listen_frame);

        self.listen_motion_absolute.notify = handleMotionAbsolute;
        c.wl_signal_add(&self.wlr_cursor.events.motion_absolute, &self.listen_motion_absolute);

        self.listen_motion.notify = handleMotion;
        c.wl_signal_add(&self.wlr_cursor.events.motion, &self.listen_motion);

        self.listen_request_set_cursor.notify = handleRequestSetCursor;
        c.wl_signal_add(&self.seat.wlr_seat.events.request_set_cursor, &self.listen_request_set_cursor);
    }

    pub fn deinit(self: *Self) void {
        c.wlr_xcursor_manager_destroy(self.wlr_xcursor_manager);
        c.wlr_cursor_destroy(self.wlr_cursor);
    }

    fn handleAxis(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
        // This event is forwarded by the cursor when a pointer emits an axis event,
        // for example when you move the scroll wheel.
        const cursor = @fieldParentPtr(Self, "listen_axis", listener.?);
        const event = @ptrCast(
            *c.wlr_event_pointer_axis,
            @alignCast(@alignOf(*c.wlr_event_pointer_axis), data),
        );

        // Notify the client with pointer focus of the axis event.
        c.wlr_seat_pointer_notify_axis(
            cursor.seat.wlr_seat,
            event.time_msec,
            event.orientation,
            event.delta,
            event.delta_discrete,
            event.source,
        );
    }

    fn handleButton(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
        // This event is forwarded by the cursor when a pointer emits a button
        // event.
        const self = @fieldParentPtr(Self, "listen_button", listener.?);
        const event = @ptrCast(
            *c.wlr_event_pointer_button,
            @alignCast(@alignOf(*c.wlr_event_pointer_button), data),
        );
        // Notify the client with pointer focus that a button press has occurred
        _ = c.wlr_seat_pointer_notify_button(
            self.seat.wlr_seat,
            event.time_msec,
            event.button,
            event.state,
        );

        var sx: f64 = undefined;
        var sy: f64 = undefined;

        var surface: ?*c.wlr_surface = null;
        const view = self.seat.input_manager.server.root.viewAt(
            self.wlr_cursor.x,
            self.wlr_cursor.y,
            &surface,
            &sx,
            &sy,
        );

        if (event.state == c.enum_wlr_button_state.WLR_BUTTON_RELEASED) {
            // If you released any buttons, we exit interactive move/resize mode.
            self.mode = CursorMode.Passthrough;
        } else {
            // Focus that client if the button was _pressed_
            if (view) |v| {
                self.seat.focus(v);
            }
        }
    }

    fn handleFrame(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
        // This event is forwarded by the cursor when a pointer emits an frame
        // event. Frame events are sent after regular pointer events to group
        // multiple events together. For instance, two axis events may happen at the
        // same time, in which case a frame event won't be sent in between.
        const self = @fieldParentPtr(Self, "listen_frame", listener.?);
        // Notify the client with pointer focus of the frame event.
        c.wlr_seat_pointer_notify_frame(self.seat.wlr_seat);
    }

    fn processMove(self: Self, time: u32) void {
        // Move the grabbed view to the new position.
        // TODO: log on null
        if (self.grabbed_view) |view| {
            view.current_box.x = @floatToInt(c_int, self.wlr_cursor.x - self.grab_x);
            view.current_box.y = @floatToInt(c_int, self.wlr_cursor.y - self.grab_y);
        }
    }

    fn handleMotionAbsolute(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
        // This event is forwarded by the cursor when a pointer emits an _absolute_
        // motion event, from 0..1 on each axis. This happens, for example, when
        // wlroots is running under a Wayland window rather than KMS+DRM, and you
        // move the mouse over the window. You could enter the window from any edge,
        // so we have to warp the mouse there. There is also some hardware which
        // emits these events.
        const self = @fieldParentPtr(Self, "listen_motion_absolute", listener.?);
        const event = @ptrCast(
            *c.wlr_event_pointer_motion_absolute,
            @alignCast(@alignOf(*c.wlr_event_pointer_motion_absolute), data),
        );
        c.wlr_cursor_warp_absolute(self.wlr_cursor, event.device, event.x, event.y);
        self.processMotion(event.time_msec);
    }

    fn handleMotion(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
        // This event is forwarded by the cursor when a pointer emits a _relative_
        // pointer motion event (i.e. a delta)
        const self = @fieldParentPtr(Self, "listen_motion", listener.?);
        const event = @ptrCast(
            *c.wlr_event_pointer_motion,
            @alignCast(@alignOf(*c.wlr_event_pointer_motion), data),
        );
        // The cursor doesn't move unless we tell it to. The cursor automatically
        // handles constraining the motion to the output layout, as well as any
        // special configuration applied for the specific input device which
        // generated the event. You can pass NULL for the device if you want to move
        // the cursor around without any input.
        c.wlr_cursor_move(self.wlr_cursor, event.device, event.delta_x, event.delta_y);
        self.processMotion(event.time_msec);
    }

    fn handleRequestSetCursor(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
        // This event is rasied by the seat when a client provides a cursor image
        const self = @fieldParentPtr(Self, "listen_request_set_cursor", listener.?);
        const event = @ptrCast(
            *c.wlr_seat_pointer_request_set_cursor_event,
            @alignCast(@alignOf(*c.wlr_seat_pointer_request_set_cursor_event), data),
        );
        const focused_client = self.seat.wlr_seat.pointer_state.focused_client;

        // This can be sent by any client, so we check to make sure this one is
        // actually has pointer focus first.
        if (focused_client == event.seat_client) {
            // Once we've vetted the client, we can tell the cursor to use the
            // provided surface as the cursor image. It will set the hardware cursor
            // on the output that it's currently on and continue to do so as the
            // cursor moves between outputs.
            c.wlr_cursor_set_surface(
                self.wlr_cursor,
                event.surface,
                event.hotspot_x,
                event.hotspot_y,
            );
        }
    }

    fn processsResize(self: Self, time: u32) void {
        // Resizing the grabbed view can be a little bit complicated, because we
        // could be resizing from any corner or edge. This not only resizes the view
        // on one or two axes, but can also move the view if you resize from the top
        // or left edges (or top-left corner).
        //
        // Note that I took some shortcuts here. In a more fleshed-out compositor,
        // you'd wait for the client to prepare a buffer at the new size, then
        // commit any movement that was prepared.

        // TODO: Handle null view
        const view = self.grabbed_view.?;

        const dx: f64 = self.wlr_cursor.x - self.grab_x;
        const dy: f64 = self.wlr_cursor.y - self.grab_y;

        var x: f64 = @intToFloat(f64, view.current_box.x);
        var y: f64 = @intToFloat(f64, view.current_box.y);

        var width = @intToFloat(f64, self.grab_width);
        var height = @intToFloat(f64, self.grab_height);

        if (self.resize_edges & @intCast(u32, c.WLR_EDGE_TOP) != 0) {
            y = self.grab_y + dy;
            height -= dy;
            if (height < 1) {
                y += height;
            }
        } else if (self.resize_edges & @intCast(u32, c.WLR_EDGE_BOTTOM) != 0) {
            height += dy;
        }
        if (self.resize_edges & @intCast(u32, c.WLR_EDGE_LEFT) != 0) {
            x = self.grab_x + dx;
            width -= dx;
            if (width < 1) {
                x += width;
            }
        } else if (self.resize_edges & @intCast(u32, c.WLR_EDGE_RIGHT) != 0) {
            width += dx;
        }
        view.current_box.x = @floatToInt(c_int, x);
        view.current_box.y = @floatToInt(c_int, y);
        _ = c.wlr_xdg_toplevel_set_size(
            view.wlr_xdg_surface,
            @floatToInt(u32, width),
            @floatToInt(u32, height),
        );
    }

    fn processMotion(self: Self, time: u32) void {
        // If the mode is non-passthrough, delegate to those functions.
        if (self.mode == CursorMode.Move) {
            self.processMove(time);
            return;
        } else if (self.mode == CursorMode.Resize) {
            self.processsResize(time);
            return;
        }

        // Otherwise, find the view under the pointer and send the event along.
        var sx: f64 = undefined;
        var sy: f64 = undefined;
        var opt_surface: ?*c.wlr_surface = null;
        const view = self.seat.input_manager.server.root.viewAt(
            self.wlr_cursor.x,
            self.wlr_cursor.y,
            &opt_surface,
            &sx,
            &sy,
        );

        if (view == null) {
            // If there's no view under the cursor, set the cursor image to a
            // default. This is what makes the cursor image appear when you move it
            // around the screen, not over any views.
            c.wlr_xcursor_manager_set_cursor_image(
                self.wlr_xcursor_manager,
                "left_ptr",
                self.wlr_cursor,
            );
        }

        const wlr_seat = self.seat.wlr_seat;
        if (opt_surface) |surface| {
            const focus_changed = wlr_seat.pointer_state.focused_surface != surface;
            // "Enter" the surface if necessary. This lets the client know that the
            // cursor has entered one of its surfaces.
            //
            // Note that this gives the surface "pointer focus", which is distinct
            // from keyboard focus. You get pointer focus by moving the pointer over
            // a window.
            c.wlr_seat_pointer_notify_enter(wlr_seat, surface, sx, sy);
            if (!focus_changed) {
                // The enter event contains coordinates, so we only need to notify
                //on motion if the focus did not change.
                c.wlr_seat_pointer_notify_motion(wlr_seat, time, sx, sy);
            }
        } else {
            // Clear pointer focus so future button events and such are not sent to
            // the last client to have the cursor over it.
            c.wlr_seat_pointer_clear_focus(wlr_seat);
        }
    }
};