jay-compositor 1.15.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
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
mod removed_output;

use crate::backend;
use crate::backend::BackendColorSpace;
use crate::backend::BackendEotfs;
use crate::backend::BackendLuminance;
use crate::client::Client;
use crate::client::ClientId;
use crate::cmm::cmm_primaries::Primaries;
use crate::format::Format;
use crate::format::XRGB8888;
use crate::gfx_api::ScalingFilter;
use crate::globals::Global;
use crate::globals::GlobalName;
use crate::ifs::wl_surface::WlSurface;
use crate::ifs::zxdg_output_v1::ZxdgOutputV1;
use crate::leaks::Tracker;
use crate::object::BreakLoops;
use crate::object::Version;
use crate::state::ConnectorData;
use crate::state::State;
use crate::tree::NodeBase;
use crate::tree::OutputNode;
use crate::tree::TearingMode;
use crate::tree::Transform;
use crate::tree::TreeTimeline::LiveTL;
use crate::tree::VrrMode;
use crate::utils::bhash::BHashMap;
use crate::utils::cell_ext::CellExt;
use crate::utils::clonecell::CloneCell;
use crate::utils::copyhashmap::CopyHashMap;
use crate::utils::markers::JayHash;
use crate::utils::rc_eq::rc_eq;
use crate::wire::WlOutputId;
use crate::wire::ZxdgOutputV1Id;
use crate::wire::wl_output::*;
use derivative::Derivative;
use hashbrown::hash_map::Entry;
use jay_proc::Global;
use jay_proc::Object;
use linearize::Linearize;
use std::cell::Cell;
use std::cell::RefCell;
use std::convert::Infallible;
use std::hash::Hash;
use std::hash::Hasher;
use std::rc::Rc;

const SP_UNKNOWN: i32 = 0;
#[expect(unused)]
const SP_NONE: i32 = 1;
#[expect(unused)]
const SP_HORIZONTAL_RGB: i32 = 2;
#[expect(unused)]
const SP_HORIZONTAL_BGR: i32 = 3;
#[expect(unused)]
const SP_VERTICAL_RGB: i32 = 4;
#[expect(unused)]
const SP_VERTICAL_BGR: i32 = 5;

pub const TF_NORMAL: i32 = 0;
pub const TF_90: i32 = 1;
pub const TF_180: i32 = 2;
pub const TF_270: i32 = 3;
pub const TF_FLIPPED: i32 = 4;
pub const TF_FLIPPED_90: i32 = 5;
pub const TF_FLIPPED_180: i32 = 6;
pub const TF_FLIPPED_270: i32 = 7;

const MODE_CURRENT: u32 = 1;
#[expect(unused)]
const MODE_PREFERRED: u32 = 2;

#[derive(Global)]
#[dedicated(outputs)]
pub struct WlOutputGlobal {
    pub name: GlobalName,
    pub state: Rc<State>,
    pub connector: Rc<ConnectorData>,
    pub output_id: Rc<OutputId>,
    pub mode: Cell<backend::Mode>,
    pub refresh_nsec: Cell<u64>,
    pub modes: Option<Vec<backend::Mode>>,
    pub formats: CloneCell<Rc<Vec<&'static Format>>>,
    pub format: Cell<&'static Format>,
    pub width_mm: i32,
    pub height_mm: i32,
    pub eotfs: Vec<BackendEotfs>,
    pub color_spaces: Vec<BackendColorSpace>,
    pub primaries: Primaries,
    pub luminance: Option<BackendLuminance>,
    pub bindings: RefCell<BHashMap<ClientId, BHashMap<WlOutputId, Rc<WlOutput>>>>,
    pub destroyed: Cell<bool>,
    pub persistent: Rc<PersistentOutputState>,
    pub opt: Rc<OutputGlobalOpt>,
}

impl PartialEq for WlOutputGlobal {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
    }
}

#[derive(Default)]
pub struct OutputGlobalOpt {
    pub global: CloneCell<Option<Rc<WlOutputGlobal>>>,
    pub node: CloneCell<Option<Rc<OutputNode>>>,
}

impl OutputGlobalOpt {
    pub fn get(&self) -> Option<Rc<WlOutputGlobal>> {
        self.global.get()
    }

    pub fn node(&self) -> Option<Rc<OutputNode>> {
        self.node.get()
    }

    fn clear(&self) {
        self.node.take();
        self.global.take();
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Linearize)]
pub enum BlendSpace {
    Linear,
    Srgb,
}

impl BlendSpace {
    pub fn name(self) -> &'static str {
        match self {
            BlendSpace::Linear => "linear",
            BlendSpace::Srgb => "srgb",
        }
    }
}

#[derive(Derivative)]
#[derivative(Default)]
pub struct PersistentOutputState {
    pub transform: Cell<Transform>,
    pub scale: Cell<crate::scale::Scale>,
    pub scaling_filter: Cell<ScalingFilter>,
    pub pos: Cell<(i32, i32)>,
    #[derivative(Default(value = "Cell::new(VrrMode::Never)"))]
    pub vrr_mode: Cell<VrrMode>,
    pub vrr_cursor_hz: Cell<Option<f64>>,
    #[derivative(Default(value = "Cell::new(TearingMode::Never)"))]
    pub tearing_mode: Cell<TearingMode>,
    pub brightness: Cell<Option<f64>>,
    #[derivative(Default(value = "Cell::new(BlendSpace::Srgb)"))]
    pub blend_space: Cell<BlendSpace>,
    pub use_native_gamut: Cell<bool>,
}

#[derive(Eq, Derivative)]
#[derivative(Debug)]
pub struct OutputId {
    _connector: Option<String>,
    pub manufacturer: String,
    pub model: String,
    pub serial_number: String,
    #[derivative(Debug = "ignore")]
    pub hash: OutputIdHash,
}

hash_type!(OutputIdHash);

impl PartialEq for OutputId {
    fn eq(&self, other: &Self) -> bool {
        self.hash == other.hash
    }
}

impl Hash for OutputId {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.hash.hash(state);
    }
}

unsafe impl JayHash for OutputId {}

impl OutputId {
    pub fn new(
        connector: impl Into<String>,
        manufacturer: impl Into<String>,
        model: impl Into<String>,
        serial_number: impl Into<String>,
    ) -> Rc<Self> {
        let connector = connector.into();
        let manufacturer = manufacturer.into();
        let model = model.into();
        let serial_number = serial_number.into();
        Self::new_(connector, manufacturer, model, serial_number)
    }

    fn new_(
        connector: String,
        manufacturer: String,
        model: String,
        serial_number: String,
    ) -> Rc<Self> {
        let connector = serial_number.is_empty().then_some(connector);
        let mut hasher = blake3::Hasher::new();
        hasher.update(&[connector.is_some() as u8]);
        let mut hash = |s: &str| {
            hasher.update(&(s.len() as u64).to_le_bytes());
            hasher.update(s.as_bytes());
        };
        connector.as_deref().map(&mut hash);
        hash(&manufacturer);
        hash(&model);
        hash(&serial_number);
        Rc::new(Self {
            _connector: connector,
            manufacturer,
            model,
            serial_number,
            hash: OutputIdHash(*hasher.finalize().as_bytes()),
        })
    }
}

impl WlOutputGlobal {
    pub fn clear(&self) {
        self.opt.clear();
        self.bindings.borrow_mut().clear();
    }

    pub fn new(
        name: GlobalName,
        state: &Rc<State>,
        connector: &Rc<ConnectorData>,
        modes: Option<Vec<backend::Mode>>,
        width_mm: i32,
        height_mm: i32,
        output_id: &Rc<OutputId>,
        persistent_state: &Rc<PersistentOutputState>,
        eotfs: Vec<BackendEotfs>,
        color_spaces: Vec<BackendColorSpace>,
        primaries: Primaries,
        luminance: Option<BackendLuminance>,
    ) -> Self {
        let connector_state = connector.state.borrow();
        Self {
            name,
            state: state.clone(),
            connector: connector.clone(),
            output_id: output_id.clone(),
            mode: Cell::new(connector_state.mode),
            refresh_nsec: Cell::new(connector_state.mode.refresh_nsec()),
            modes,
            formats: CloneCell::new(Rc::new(vec![])),
            format: Cell::new(XRGB8888),
            width_mm,
            height_mm,
            eotfs,
            color_spaces,
            primaries,
            luminance,
            bindings: Default::default(),
            destroyed: Cell::new(false),
            persistent: persistent_state.clone(),
            opt: Default::default(),
        }
    }

    fn for_each_binding<F: FnMut(&Rc<WlOutput>)>(&self, client: ClientId, mut f: F) {
        let bindings = self.bindings.borrow_mut();
        if let Some(bindings) = bindings.get(&client) {
            for binding in bindings.values() {
                f(binding);
            }
        }
    }

    pub fn send_enter(&self, surface: &WlSurface) {
        self.for_each_binding(surface.client.id, |b| {
            surface.send_enter(b.id);
        });
    }

    pub fn send_leave(&self, surface: &WlSurface) {
        self.for_each_binding(surface.client.id, |b| {
            surface.send_leave(b.id);
        });
    }

    pub fn send_mode(&self) {
        let bindings = self.bindings.borrow_mut();
        for binding in bindings.values() {
            for binding in binding.values() {
                binding.send_updates();
            }
        }
    }

    fn bind_(
        self: Rc<Self>,
        id: WlOutputId,
        client: &Rc<Client>,
        version: Version,
    ) -> Result<(), Infallible> {
        let obj = Rc::new(WlOutput {
            global: self.opt.clone(),
            id,
            xdg_outputs: Default::default(),
            client: client.clone(),
            version,
            tracker: Default::default(),
        });
        track!(client, obj);
        client.add_client_obj(&obj);
        self.bindings
            .borrow_mut()
            .entry(client.id)
            .or_default()
            .insert(id, obj.clone());
        obj.send_geometry();
        obj.send_mode();
        if obj.version >= SEND_SCALE_SINCE {
            obj.send_scale();
        }
        if obj.version >= SEND_NAME_SINCE {
            obj.send_name();
        }
        if obj.version >= SEND_DONE_SINCE {
            obj.send_done();
        }
        for group in client
            .objects
            .dedicated
            .ext_workspace_group_handle_v1
            .lock()
            .values()
        {
            if rc_eq(&group.output, &self.opt) {
                group.handle_new_output(&obj);
            }
        }
        if let Some(node) = self.opt.node() {
            for surface in client.objects.dedicated.wl_surface.lock().values() {
                if surface.node_output_id() == Some(node.id) {
                    surface.send_enter(obj.id);
                }
            }
            for handle in client
                .objects
                .dedicated
                .zwlr_foreign_toplevel_handle_v1
                .lock()
                .values()
            {
                if handle.output.get() == node.id {
                    handle.send_output_enter(&obj);
                    handle.send_done();
                }
            }
        }
        Ok(())
    }
}

const OUTPUT_VERSION: u32 = 4;

impl Global for WlOutputGlobal {
    fn version(&self) -> u32 {
        OUTPUT_VERSION
    }
}

#[derive(Object)]
#[break_loops]
pub struct WlOutput {
    pub global: Rc<OutputGlobalOpt>,
    pub id: WlOutputId,
    pub xdg_outputs: CopyHashMap<ZxdgOutputV1Id, Rc<ZxdgOutputV1>>,
    client: Rc<Client>,
    pub version: Version,
    tracker: Tracker<Self>,
}

pub const SEND_DONE_SINCE: Version = Version(2);
pub const SEND_SCALE_SINCE: Version = Version(2);
pub const SEND_NAME_SINCE: Version = Version(4);

impl WlOutput {
    pub fn send_updates(&self) {
        self.send_geometry();
        self.send_mode();
        if self.version >= SEND_SCALE_SINCE {
            self.send_scale();
        }
        if self.version >= SEND_DONE_SINCE {
            self.send_done();
        }
        let xdg = self.xdg_outputs.lock();
        for xdg in xdg.values() {
            xdg.send_updates(false);
        }
    }

    fn send_geometry(&self) {
        let Some(node) = self.global.node() else {
            return;
        };
        let global = &node.global;
        let pos = node.node_state[LiveTL].pos.get();
        let mut x = pos.x1();
        let mut y = pos.y1();
        logical_to_client_wire_scale!(self.client, x, y);
        let event = Geometry {
            self_id: self.id,
            x,
            y,
            physical_width: global.width_mm,
            physical_height: global.height_mm,
            subpixel: SP_UNKNOWN,
            make: &global.output_id.manufacturer,
            model: &global.output_id.model,
            transform: node.node_state[LiveTL].transform.get().to_wl(),
        };
        self.client.event(event);
    }

    fn send_mode(&self) {
        let Some(global) = self.global.get() else {
            return;
        };
        let mut mode = global.mode.get();
        logical_to_client_wire_scale!(self.client, mode.width, mode.height);
        let event = Mode {
            self_id: self.id,
            flags: MODE_CURRENT,
            width: mode.width,
            height: mode.height,
            refresh: mode.refresh_rate_millihz as _,
        };
        self.client.event(event);
    }

    fn send_scale(&self) {
        let Some(node) = self.global.node() else {
            return;
        };
        let factor = match self.client.wire_scale.is_some() {
            true => 1,
            false => node.node_state[LiveTL].legacy_scale.get() as _,
        };
        let event = Scale {
            self_id: self.id,
            factor,
        };
        self.client.event(event);
    }

    fn send_name(&self) {
        let Some(global) = self.global.get() else {
            return;
        };
        self.client.event(Name {
            self_id: self.id,
            name: &global.connector.name,
        });
    }

    pub fn send_done(&self) {
        let event = Done { self_id: self.id };
        self.client.event(event);
    }

    fn remove_binding(&self) {
        let Some(global) = self.global.get() else {
            return;
        };
        if let Entry::Occupied(mut e) = global.bindings.borrow_mut().entry(self.client.id) {
            e.get_mut().remove(&self.id);
            if e.get().is_empty() {
                e.remove();
            }
        }
    }
}

impl WlOutputRequestHandler for WlOutput {
    type Error = Infallible;

    fn release(&self, _req: Release, _slf: &Rc<Self>) -> Result<(), Self::Error> {
        self.xdg_outputs.clear();
        self.remove_binding();
        self.client.remove_obj(self);
        Ok(())
    }
}

impl BreakLoops for WlOutput {
    fn break_loops(self: Rc<Self>) {
        self.xdg_outputs.clear();
        self.remove_binding();
    }
}