jay-compositor 1.13.0

The Jay compositor
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
use {
    crate::{
        backend::{InputDeviceGroupId, InputDeviceId},
        cursor_user::CursorUser,
        ifs::{
            wl_seat::{
                WlSeatGlobal,
                tablet::{
                    pad_owner::PadOwnerHolder, tablet_bindings::TabletBindings,
                    tool_owner::ToolOwnerHolder, zwp_tablet_pad_dial_v2::ZwpTabletPadDialV2,
                    zwp_tablet_pad_group_v2::ZwpTabletPadGroupV2,
                    zwp_tablet_pad_ring_v2::ZwpTabletPadRingV2,
                    zwp_tablet_pad_strip_v2::ZwpTabletPadStripV2,
                    zwp_tablet_pad_v2::ZwpTabletPadV2, zwp_tablet_seat_v2::ZwpTabletSeatV2,
                    zwp_tablet_tool_v2::ZwpTabletToolV2, zwp_tablet_v2::ZwpTabletV2,
                },
            },
            wl_surface::WlSurface,
        },
        object::Version,
        tree::{FoundNode, Node},
        utils::{
            bindings::PerClientBindings, clonecell::CloneCell, copyhashmap::CopyHashMap,
            hash_map_ext::HashMapExt,
        },
    },
    std::{
        cell::{Cell, RefCell},
        rc::Rc,
    },
};

mod pad;
mod pad_owner;
mod tablet_bindings;
mod tool;
pub mod tool_owner;
pub mod zwp_tablet_manager_v2;
pub mod zwp_tablet_pad_dial_v2;
pub mod zwp_tablet_pad_group_v2;
pub mod zwp_tablet_pad_ring_v2;
pub mod zwp_tablet_pad_strip_v2;
pub mod zwp_tablet_pad_v2;
pub mod zwp_tablet_seat_v2;
pub mod zwp_tablet_tool_v2;
pub mod zwp_tablet_v2;

#[derive(Default)]
pub struct TabletSeatData {
    seats: PerClientBindings<ZwpTabletSeatV2>,
    tablets: CopyHashMap<TabletId, Rc<Tablet>>,
    tools: CopyHashMap<TabletToolId, Rc<TabletTool>>,
    pads: CopyHashMap<TabletPadId, Rc<TabletPad>>,
}

#[derive(Debug, Clone)]
pub struct TabletInit {
    pub id: TabletId,
    pub group: InputDeviceGroupId,
    pub name: String,
    pub pid: u32,
    pub vid: u32,
    pub bustype: Option<u32>,
    pub path: String,
}

#[derive(Debug, Clone)]
pub struct TabletToolInit {
    pub tablet_id: TabletId,
    pub id: TabletToolId,
    pub type_: TabletToolType,
    pub hardware_serial: u64,
    pub hardware_id_wacom: u64,
    pub capabilities: Vec<TabletToolCapability>,
}

#[derive(Debug, Clone)]
pub struct TabletPadInit {
    pub id: TabletPadId,
    pub group: InputDeviceGroupId,
    pub path: String,
    pub buttons: u32,
    pub strips: u32,
    pub rings: u32,
    pub dials: u32,
    pub groups: Vec<TabletPadGroupInit>,
}

#[derive(Debug, Clone)]
pub struct TabletPadGroupInit {
    pub buttons: Vec<u32>,
    pub rings: Vec<u32>,
    pub strips: Vec<u32>,
    pub dials: Vec<u32>,
    pub modes: u32,
    pub mode: u32,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum PadButtonState {
    Released,
    Pressed,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ToolButtonState {
    Released,
    Pressed,
}

linear_ids!(TabletIds, TabletId);

pub struct Tablet {
    _id: TabletId,
    dev: InputDeviceId,
    group: InputDeviceGroupId,
    name: String,
    pid: u32,
    vid: u32,
    bustype: Option<u32>,
    path: String,
    bindings: TabletBindings<ZwpTabletV2>,
    tools: CopyHashMap<TabletToolId, Rc<TabletTool>>,
    pads: CopyHashMap<TabletPadId, Rc<TabletPad>>,
    tree: RefCell<Vec<FoundNode>>,
    seat: Rc<WlSeatGlobal>,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum TabletToolType {
    Pen,
    Eraser,
    Brush,
    Pencil,
    Airbrush,
    #[expect(dead_code)]
    Finger,
    Mouse,
    Lens,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum TabletToolCapability {
    Tilt,
    Pressure,
    Distance,
    Rotation,
    Slider,
    Wheel,
}

linear_ids!(TabletToolIds, TabletToolId, usize);

#[derive(Default)]
pub struct TabletToolOpt {
    tool: CloneCell<Option<Rc<TabletTool>>>,
}

pub struct TabletTool {
    pub id: TabletToolId,
    opt: Rc<TabletToolOpt>,
    tablet: Rc<Tablet>,
    type_: TabletToolType,
    hardware_serial: u64,
    hardware_id_wacom: u64,
    capabilities: Vec<TabletToolCapability>,
    bindings: TabletBindings<ZwpTabletToolV2>,
    node: CloneCell<Rc<dyn Node>>,
    pub(super) tool_owner: ToolOwnerHolder,
    cursor: Rc<CursorUser>,

    down: Cell<bool>,
    pressure: Cell<f64>,
    distance: Cell<f64>,
    tilt_x: Cell<f64>,
    tilt_y: Cell<f64>,
    rotation: Cell<f64>,
    slider: Cell<f64>,
}

linear_ids!(TabletPadIds, TabletPadId);

pub struct TabletPad {
    pub id: TabletPadId,
    dev: InputDeviceId,
    seat: Rc<WlSeatGlobal>,
    group: InputDeviceGroupId,
    tablet: CloneCell<Option<Rc<Tablet>>>,
    path: String,
    buttons: u32,
    bindings: TabletBindings<ZwpTabletPadV2>,
    groups: Vec<Rc<TabletPadGroup>>,
    strips: Vec<Rc<TabletPadStrip>>,
    rings: Vec<Rc<TabletPadRing>>,
    dials: Vec<Rc<TabletPadDial>>,
    node: CloneCell<Rc<dyn Node>>,
    pub(super) pad_owner: PadOwnerHolder,
}

pub struct TabletPadGroup {
    buttons: Vec<u32>,
    mode: Cell<u32>,
    modes: u32,
    rings: Vec<u32>,
    strips: Vec<u32>,
    dials: Vec<u32>,
    bindings: TabletBindings<ZwpTabletPadGroupV2>,
}

pub struct TabletPadStrip {
    bindings: TabletBindings<ZwpTabletPadStripV2>,
}

pub struct TabletPadRing {
    bindings: TabletBindings<ZwpTabletPadRingV2>,
}

pub struct TabletPadDial {
    bindings: TabletBindings<ZwpTabletPadDialV2>,
}

#[derive(Copy, Clone, Debug)]
pub enum TabletRingEventSource {
    Finger,
}

#[derive(Copy, Clone, Debug)]
pub enum TabletStripEventSource {
    Finger,
}

#[derive(Debug, Default)]
pub struct TabletToolChanges {
    pub down: Option<bool>,
    pub pos: Option<TabletTool2dChange<TabletToolPositionChange>>,
    pub pressure: Option<f64>,
    pub distance: Option<f64>,
    pub tilt: Option<TabletTool2dChange<f64>>,
    pub rotation: Option<f64>,
    pub slider: Option<f64>,
    pub wheel: Option<TabletToolWheelChange>,
}

#[derive(Copy, Clone, Debug)]
pub struct TabletTool2dChange<T> {
    pub x: T,
    pub y: T,
}

#[derive(Copy, Clone, Debug)]
pub struct TabletToolPositionChange {
    pub x: f64,
    pub dx: f64,
}

#[derive(Copy, Clone, Debug)]
pub struct TabletToolWheelChange {
    pub degrees: f64,
    pub clicks: i32,
}

impl WlSeatGlobal {
    fn tablet_add_seat(&self, seat: &Rc<ZwpTabletSeatV2>) {
        self.tablet.seats.add(&seat.client, seat);
        for tablet in self.tablet.tablets.lock().values() {
            seat.announce_tablet(tablet);
        }
        for tool in self.tablet.tools.lock().values() {
            seat.announce_tool(tool);
        }
        for pad in self.tablet.pads.lock().values() {
            seat.announce_pad(pad);
        }
    }

    pub fn tablet_add_tablet(self: &Rc<Self>, dev: InputDeviceId, init: &TabletInit) {
        let tablet = Rc::new(Tablet {
            _id: init.id,
            dev,
            group: init.group,
            name: init.name.clone(),
            pid: init.pid,
            vid: init.vid,
            bustype: init.bustype,
            path: init.path.clone(),
            bindings: Default::default(),
            tools: Default::default(),
            pads: Default::default(),
            tree: Default::default(),
            seat: self.clone(),
        });
        self.tablet.tablets.set(init.id, tablet.clone());
        self.tablet_for_each_seat_obj(|s| s.announce_tablet(&tablet));
        for pad in self.tablet.pads.lock().values() {
            if pad.tablet.is_none() && pad.group == init.group {
                self.connect_tablet_and_pad(&tablet, pad);
            }
        }
    }

    fn tablet_for_each_seat_obj(&self, mut f: impl FnMut(&Rc<ZwpTabletSeatV2>)) {
        for seats in self.tablet.seats.borrow().values() {
            for seat in seats.values() {
                f(seat);
            }
        }
    }

    pub fn tablet_clear(&self) {
        self.tablet.seats.clear();
        for tablet in self.tablet.tablets.lock().drain_values() {
            tablet.pads.clear();
            tablet.bindings.clear();
            tablet.tools.clear();
        }
        for tool in self.tablet.tools.lock().drain_values() {
            tool.cursor.detach();
            tool.opt.tool.take();
            tool.tool_owner.destroy(&tool);
            tool.bindings.clear();
        }
        for pad in self.tablet.pads.lock().drain_values() {
            pad.pad_owner.destroy(&pad);
            pad.tablet.take();
            pad.bindings.clear();
            for group in &pad.groups {
                group.bindings.clear();
            }
            for rings in &pad.rings {
                rings.bindings.clear();
            }
            for strips in &pad.strips {
                strips.bindings.clear();
            }
            for dials in &pad.dials {
                dials.bindings.clear();
            }
        }
    }

    pub fn tablet_remove_tablet(self: &Rc<Self>, id: TabletId) {
        let Some(tablet) = self.tablet.tablets.remove(&id) else {
            return;
        };
        for tool in tablet.tools.lock().drain_values() {
            self.tablet_handle_remove_tool(tablet.seat.state.now_usec(), tool.id);
        }
        for pad in tablet.pads.lock().drain_values() {
            pad.pad_owner.destroy(&pad);
            pad.tablet.take();
        }
        for binding in tablet.bindings.lock().drain_values() {
            binding.send_removed();
        }
    }

    fn connect_tablet_and_pad(self: &Rc<Self>, tablet: &Rc<Tablet>, pad: &Rc<TabletPad>) {
        pad.tablet.set(Some(tablet.clone()));
        tablet.pads.set(pad.id, pad.clone());
        pad.pad_owner.update_node(pad);
    }

    pub fn tablet_on_keyboard_node_change(self: &Rc<Self>) {
        if self.tablet.pads.is_empty() {
            return;
        }
        for pad in self.tablet.pads.lock().values() {
            if pad.tablet.is_some() {
                pad.pad_owner.update_node(pad);
            }
        }
    }

    fn tablet_for_each_seat(&self, surface: &WlSurface, f: impl FnMut(&ZwpTabletSeatV2)) {
        self.tablet
            .seats
            .for_each(surface.client.id, Version::ALL, f)
    }

    pub(super) fn tablet_apply_changes(self: &Rc<Self>) {
        if self.tablet.tools.is_empty() {
            return;
        }
        let now = self.state.now_usec();
        for tool in self.tablet.tools.lock().values() {
            tool.tool_owner.apply_changes(tool, now, None);
        }
    }
}

fn normalizei(n: f64) -> i32 {
    (65535.0 * n) as i32
}

fn normalizeu(n: f64) -> u32 {
    normalizei(n) as u32
}

impl TabletTool {
    pub fn cursor(&self) -> &Rc<CursorUser> {
        &self.cursor
    }

    pub fn node(&self) -> Rc<dyn Node> {
        self.node.get()
    }

    pub fn seat(&self) -> &Rc<WlSeatGlobal> {
        &self.tablet.seat
    }
}