#wayland #wlroots #event-loop #winit #window #events #shell

sessionlockev

extra shell binding for ext-session-shell with winit like eventloop

48 releases (12 breaking)

new 0.13.0 Dec 9, 2024
0.12.0 Dec 6, 2024
0.11.0-rc3 Nov 29, 2024
0.3.0-beta2 Jul 31, 2024
0.1.0 Dec 18, 2023

#786 in GUI

Download history 7/week @ 2024-08-23 61/week @ 2024-08-30 209/week @ 2024-09-06 65/week @ 2024-09-13 355/week @ 2024-09-20 489/week @ 2024-09-27 528/week @ 2024-10-04 351/week @ 2024-10-11 768/week @ 2024-10-18 92/week @ 2024-10-25 212/week @ 2024-11-01 35/week @ 2024-11-08 111/week @ 2024-11-15 135/week @ 2024-11-22 810/week @ 2024-11-29 454/week @ 2024-12-06

1,511 downloads per month
Used in iced_sessionlock

MIT license

220KB
3.5K SLoC

sessionlockev

SessionLockEventloop, take lot reference from winit, amin to make a easy way to create layershell window.

you can take ./examples/simplelock.rs for example

use std::fs::File;
use std::os::fd::AsFd;

use sessionlockev::reexport::*;
use sessionlockev::*;

const ESC_KEY: u32 = 1;

fn main() {
    let mut ev: WindowState<()> = WindowState::new().build().unwrap();

    let mut virtual_keyboard_manager = None;
    ev.running(|event, _ev, _index| {
        println!("{:?}", event);
        match event {
            // NOTE: this will send when init, you can request bind extra object from here
            SessionLockEvent::InitRequest => ReturnData::RequestBind,
            SessionLockEvent::BindProvide(globals, qh) => {
                // NOTE: you can get implied wayland object from here
                virtual_keyboard_manager = Some(
                    globals
                        .bind::<zwp_virtual_keyboard_v1::ZwpVirtualKeyboardManagerV1, _, _>(
                            qh,
                            1..=1,
                            (),
                        )
                        .unwrap(),
                );
                println!("{:?}", virtual_keyboard_manager);
                ReturnData::None
            }
            SessionLockEvent::RequestBuffer(file, shm, qh, init_w, init_h) => {
                draw(file, (init_w, init_h));
                let pool = shm.create_pool(file.as_fd(), (init_w * init_h * 4) as i32, qh, ());
                ReturnData::WlBuffer(pool.create_buffer(
                    0,
                    init_w as i32,
                    init_h as i32,
                    (init_w * 4) as i32,
                    wl_shm::Format::Argb8888,
                    qh,
                    (),
                ))
            }
            SessionLockEvent::RequestMessages(DispatchMessage::RequestRefresh {
                width,
                height,
            }) => {
                println!("{width}, {height}");
                ReturnData::None
            }
            SessionLockEvent::RequestMessages(DispatchMessage::MouseButton { .. }) => {
                ReturnData::None
            }
            SessionLockEvent::RequestMessages(DispatchMessage::MouseEnter {
                serial,
                pointer,
                ..
            }) => ReturnData::RequestSetCursorShape((
                "crosshair".to_owned(),
                pointer.clone(),
                *serial,
            )),
            SessionLockEvent::RequestMessages(DispatchMessage::KeyBoard { key, .. }) => {
                if *key == ESC_KEY {
                    return ReturnData::RequestUnlockAndExist;
                }
                ReturnData::None
            }
            SessionLockEvent::RequestMessages(DispatchMessage::MouseMotion {
                time,
                surface_x,
                surface_y,
            }) => {
                println!("{time}, {surface_x}, {surface_y}");
                ReturnData::None
            }
            _ => ReturnData::None,
        }
    })
    .unwrap();
}

fn draw(tmp: &mut File, (buf_x, buf_y): (u32, u32)) {
    use std::{cmp::min, io::Write};
    let mut buf = std::io::BufWriter::new(tmp);
    for y in 0..buf_y {
        for x in 0..buf_x {
            let a = 0xFF;
            let r = min(((buf_x - x) * 0xFF) / buf_x, ((buf_y - y) * 0xFF) / buf_y);
            let g = min((x * 0xFF) / buf_x, ((buf_y - y) * 0xFF) / buf_y);
            let b = min(((buf_x - x) * 0xFF) / buf_x, (y * 0xFF) / buf_y);

            let color = (a << 24) + (r << 16) + (g << 8) + b;
            buf.write_all(&color.to_ne_bytes()).unwrap();
        }
    }
    buf.flush().unwrap();
}

For more example, please take a look at exwlshelleventloop

Dependencies

~6–15MB
~206K SLoC