augenblick 0.5.0

Fullscreen eye-blink overlay for X11 and Wayland
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
use super::{FRAME_MS, blink_duration, fade_alpha};
use crate::config::{Config, Movement};
use smithay_client_toolkit::{
  compositor::{CompositorHandler, CompositorState},
  delegate_compositor, delegate_layer, delegate_output, delegate_registry,
  delegate_shm,
  output::{OutputHandler, OutputState},
  registry::{ProvidesRegistryState, RegistryState},
  registry_handlers,
  shell::{
    WaylandSurface,
    wlr_layer::{
      Anchor, KeyboardInteractivity, Layer, LayerShell, LayerShellHandler,
      LayerSurface, LayerSurfaceConfigure,
    },
  },
  shm::{Shm, ShmHandler, slot::SlotPool},
};
use std::thread;
use std::time::{Duration, Instant};
use wayland_client::{
  Connection, EventQueue, QueueHandle,
  globals::registry_queue_init,
  protocol::{wl_output, wl_shm, wl_surface},
};

pub struct WaylandBackend;

impl WaylandBackend {
  pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
    // Just verify we can connect — actual work happens per blink
    Connection::connect_to_env()?;
    Ok(Self)
  }
}

impl super::Backend for WaylandBackend {
  fn blink(&self, cfg: &Config) -> Result<(), Box<dyn std::error::Error>> {
    match cfg.movement() {
      Movement::Blink => blink_wayland(cfg),
      Movement::Fill => fill_wayland(cfg),
    }
  }
}

#[allow(dead_code)]
struct AppState {
  registry_state: RegistryState,
  output_state: OutputState,
  compositor_state: CompositorState,
  shm: Shm,
  layer_shell: LayerShell,

  // surfaces: both lids in blink movement, only `top` in fill movement
  top: Option<LayerSurface>,
  bot: Option<LayerSurface>,
  top_configured: bool,
  bot_configured: bool,

  // screen dimensions filled on configure
  screen_w: u32,
  screen_h: u32,

  // drawing params
  color: u32,
  frames: u32,
}

impl AppState {}

impl CompositorHandler for AppState {
  fn scale_factor_changed(
    &mut self,
    _conn: &Connection,
    _qh: &QueueHandle<Self>,
    _surface: &wl_surface::WlSurface,
    _new_factor: i32,
  ) {
  }
  fn transform_changed(
    &mut self,
    _conn: &Connection,
    _qh: &QueueHandle<Self>,
    _surface: &wl_surface::WlSurface,
    _new_transform: wl_output::Transform,
  ) {
  }
  fn frame(
    &mut self,
    _conn: &Connection,
    _qh: &QueueHandle<Self>,
    _surface: &wl_surface::WlSurface,
    _time: u32,
  ) {
  }
  fn surface_enter(
    &mut self,
    _conn: &Connection,
    _qh: &QueueHandle<Self>,
    _surface: &wl_surface::WlSurface,
    _output: &wl_output::WlOutput,
  ) {
  }
  fn surface_leave(
    &mut self,
    _conn: &Connection,
    _qh: &QueueHandle<Self>,
    _surface: &wl_surface::WlSurface,
    _output: &wl_output::WlOutput,
  ) {
  }
}

impl OutputHandler for AppState {
  fn output_state(&mut self) -> &mut OutputState {
    &mut self.output_state
  }
  fn new_output(
    &mut self,
    _conn: &Connection,
    _qh: &QueueHandle<Self>,
    _output: wl_output::WlOutput,
  ) {
  }
  fn update_output(
    &mut self,
    _conn: &Connection,
    _qh: &QueueHandle<Self>,
    _output: wl_output::WlOutput,
  ) {
  }
  fn output_destroyed(
    &mut self,
    _conn: &Connection,
    _qh: &QueueHandle<Self>,
    _output: wl_output::WlOutput,
  ) {
  }
}

impl LayerShellHandler for AppState {
  fn closed(
    &mut self,
    _conn: &Connection,
    _qh: &QueueHandle<Self>,
    _layer: &LayerSurface,
  ) {
  }
  fn configure(
    &mut self,
    _conn: &Connection,
    _qh: &QueueHandle<Self>,
    layer: &LayerSurface,
    configure: LayerSurfaceConfigure,
    _serial: u32,
  ) {
    if let Some(top) = &self.top
      && top.wl_surface() == layer.wl_surface()
    {
      self.screen_w = configure.new_size.0;
      self.screen_h = configure.new_size.1;
      self.top_configured = true;
    }
    if let Some(bot) = &self.bot
      && bot.wl_surface() == layer.wl_surface()
    {
      self.bot_configured = true;
    }
  }
}

impl ShmHandler for AppState {
  fn shm_state(&mut self) -> &mut Shm {
    &mut self.shm
  }
}

impl ProvidesRegistryState for AppState {
  fn registry(&mut self) -> &mut RegistryState {
    &mut self.registry_state
  }
  registry_handlers![OutputState];
}

delegate_compositor!(AppState);
delegate_output!(AppState);
delegate_shm!(AppState);
delegate_layer!(AppState);
delegate_registry!(AppState);

// a fresh connection with its globals bound, before any surface exists
struct Globals {
  event_queue: EventQueue<AppState>,
  qh: QueueHandle<AppState>,
  registry_state: RegistryState,
  output_state: OutputState,
  compositor_state: CompositorState,
  shm: Shm,
  layer_shell: LayerShell,
}

fn connect() -> Result<Globals, Box<dyn std::error::Error>> {
  let conn = Connection::connect_to_env()?;
  let (globals, event_queue) = registry_queue_init(&conn)?;
  let qh = event_queue.handle();

  let compositor_state = CompositorState::bind(&globals, &qh)?;
  let layer_shell = LayerShell::bind(&globals, &qh).map_err(|_| {
        "wlr-layer-shell not supported by this compositor (requires wlroots-based compositor like Sway or Hyprland)"
    })?;
  let shm = Shm::bind(&globals, &qh)?;
  let output_state = OutputState::new(&globals, &qh);
  let registry_state = RegistryState::new(&globals);

  Ok(Globals {
    event_queue,
    qh,
    registry_state,
    output_state,
    compositor_state,
    shm,
    layer_shell,
  })
}

// a layer surface on the overlay layer that never takes keyboard focus
fn make_surface(g: &Globals, namespace: &str, anchor: Anchor) -> LayerSurface {
  let surface = g.compositor_state.create_surface(&g.qh);
  let layer = g.layer_shell.create_layer_surface(
    &g.qh,
    surface,
    Layer::Overlay,
    Some(namespace),
    None,
  );
  layer.set_anchor(anchor);
  layer.set_exclusive_zone(-1);
  layer.set_keyboard_interactivity(KeyboardInteractivity::None);
  layer
}

// covers the whole screen at once, holds, then uncovers. With fade, the
// cover ramps in before the hold and out after it.
fn fill_wayland(cfg: &Config) -> Result<(), Box<dyn std::error::Error>> {
  let g = connect()?;

  // one surface anchored to all four edges: size 0x0 means "fill the output"
  let full = make_surface(
    &g,
    "augenblick-fill",
    Anchor::TOP | Anchor::BOTTOM | Anchor::LEFT | Anchor::RIGHT,
  );
  full.set_size(0, 0);
  full.commit();

  let Globals {
    mut event_queue,
    registry_state,
    output_state,
    compositor_state,
    shm,
    layer_shell,
    ..
  } = g;
  let mut state = AppState {
    registry_state,
    output_state,
    compositor_state,
    shm,
    layer_shell,
    top: Some(full),
    bot: None,
    top_configured: false,
    bot_configured: true,
    screen_w: 0,
    screen_h: 0,
    color: cfg.color(),
    frames: cfg.animation_frames(),
  };

  while !state.top_configured {
    event_queue.blocking_dispatch(&mut state)?;
  }

  let w = state.screen_w;
  let h = state.screen_h;
  let fade = cfg.fade();
  let hold = super::hold(cfg);
  let mut pool = SlotPool::new((w * h * 4) as usize, &state.shm)?;
  let full = state.top.take().expect("fill surface");

  if fade.is_zero() {
    draw(&full, &mut pool, w, h, state.color, 1.0)?;
    event_queue.flush()?;
    thread::sleep(hold);
  } else {
    let total = fade + hold + fade;
    let start = Instant::now();
    loop {
      let elapsed = start.elapsed();
      if elapsed >= total {
        break;
      }
      let alpha = fade_alpha(elapsed, total, fade);
      draw(&full, &mut pool, w, h, state.color, alpha)?;
      event_queue.flush()?;
      thread::sleep(Duration::from_millis(FRAME_MS));
    }
  }

  full.wl_surface().destroy();
  event_queue.flush()?;

  Ok(())
}

fn blink_wayland(cfg: &Config) -> Result<(), Box<dyn std::error::Error>> {
  let g = connect()?;

  // top lid: anchored to the top edge, full width, starts at 1px
  let top = make_surface(
    &g,
    "augenblick-top",
    Anchor::TOP | Anchor::LEFT | Anchor::RIGHT,
  );
  top.set_size(0, 1);
  top.commit();

  // bottom lid: anchored to the bottom edge
  let bot = make_surface(
    &g,
    "augenblick-bot",
    Anchor::BOTTOM | Anchor::LEFT | Anchor::RIGHT,
  );
  bot.set_size(0, 1);
  bot.commit();

  let Globals {
    mut event_queue,
    registry_state,
    output_state,
    compositor_state,
    shm,
    layer_shell,
    ..
  } = g;
  let mut state = AppState {
    registry_state,
    output_state,
    compositor_state,
    shm,
    layer_shell,
    top: Some(top),
    bot: Some(bot),
    top_configured: false,
    bot_configured: false,
    screen_w: 0,
    screen_h: 0,
    color: cfg.color(),
    frames: cfg.animation_frames(),
  };

  // wait for both surfaces to be configured
  while !state.top_configured || !state.bot_configured {
    event_queue.blocking_dispatch(&mut state)?;
  }

  let w = state.screen_w;
  let h = state.screen_h;
  let half_h = h / 2;
  let frames = state.frames;
  let color = state.color;
  let fade = cfg.fade();
  let hold = super::hold(cfg);
  let total = blink_duration(frames, hold);
  let start = Instant::now();

  let mut pool = SlotPool::new((w * h * 4) as usize, &state.shm)?;
  let top = state.top.take().expect("top lid");
  let bot = state.bot.take().expect("bottom lid");

  // both lids at `lid` px, at the opacity the fade envelope gives right now
  let draw_lids =
    |lid: u32, pool: &mut SlotPool| -> Result<(), Box<dyn std::error::Error>> {
      let alpha = fade_alpha(start.elapsed(), total, fade);
      top.set_size(w, lid);
      top.commit();
      draw(&top, pool, w, lid, color, alpha)?;
      bot.set_size(w, lid);
      bot.commit();
      draw(&bot, pool, w, lid, color, alpha)?;
      Ok(())
    };

  // close: lids sweep inward
  for frame in 0..=frames {
    let progress = frame as f32 / frames as f32;
    let eased = progress * progress * (3.0 - 2.0 * progress);
    let lid = ((half_h as f32) * eased) as u32;
    draw_lids(lid.max(1), &mut pool)?;
    event_queue.flush()?;
    thread::sleep(Duration::from_millis(FRAME_MS));
  }

  if fade.is_zero() {
    thread::sleep(hold);
  } else {
    // keep the opacity envelope moving while the eye stays closed
    let hold_end = Instant::now() + hold;
    while Instant::now() < hold_end {
      draw_lids(half_h.max(1), &mut pool)?;
      event_queue.flush()?;
      thread::sleep(Duration::from_millis(FRAME_MS));
    }
  }

  // open: lids sweep back out
  for frame in 0..=frames {
    let progress = frame as f32 / frames as f32;
    let eased = progress * progress * (3.0 - 2.0 * progress);
    let lid = ((half_h as f32) * (1.0 - eased)) as u32;
    draw_lids(lid.max(1), &mut pool)?;
    event_queue.flush()?;
    thread::sleep(Duration::from_millis(FRAME_MS));
  }

  top.wl_surface().destroy();
  bot.wl_surface().destroy();
  event_queue.flush()?;

  Ok(())
}

// fills the surface with `color` at opacity `alpha` (premultiplied ARGB)
fn draw(
  surface: &LayerSurface,
  pool: &mut SlotPool,
  width: u32,
  height: u32,
  color: u32,
  alpha: f32,
) -> Result<(), Box<dyn std::error::Error>> {
  if width == 0 || height == 0 {
    return Ok(());
  }
  let a = (alpha.clamp(0.0, 1.0) * 255.0).round() as u32;
  let premul = |shift: u32| (((color >> shift) & 0xFF) * a / 255) as u8;
  let (r, g, b, a) = (premul(16), premul(8), premul(0), a as u8);
  let stride = width as i32 * 4;
  let (buffer, canvas) = pool.create_buffer(
    width as i32,
    height as i32,
    stride,
    wl_shm::Format::Argb8888,
  )?;
  for pixel in canvas.as_chunks_mut::<4>().0 {
    *pixel = [b, g, r, a];
  }
  surface.wl_surface().attach(Some(buffer.wl_buffer()), 0, 0);
  surface
    .wl_surface()
    .damage_buffer(0, 0, width as i32, height as i32);
  surface.wl_surface().commit();
  Ok(())
}