From 456f56f5b5f77c7f47114acd1a6cb0f63b3b4009 Mon Sep 17 00:00:00 2001 From: Zynh0722 Date: Wed, 31 Jan 2024 22:34:50 -0800 Subject: [PATCH] some state and FORMATTING BB --- src/main.rs | 5 ++--- src/setup.rs | 10 ++++++++-- src/state.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 src/state.rs diff --git a/src/main.rs b/src/main.rs index e0cecfb..8af4ad1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,9 @@ mod setup; +mod state; use setup::{become_wm, handle_args}; -use x11rb::{ - connect, connection::Connection, -}; +use x11rb::{connect, connection::Connection}; fn main() -> anyhow::Result<()> { handle_args(); diff --git a/src/setup.rs b/src/setup.rs index 5ee7cbe..79e0c46 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -1,9 +1,15 @@ use anyhow::anyhow; -use x11rb::{connection::Connection, protocol::xproto::{ConnectionExt, Screen}}; +use x11rb::{ + connection::Connection, + protocol::xproto::{ConnectionExt, Screen}, +}; pub fn become_wm(conn: &C, screen: &Screen) -> anyhow::Result<()> { - use x11rb::{protocol::xproto::{ChangeWindowAttributesAux, EventMask}, xcb_ffi::ReplyError}; + use x11rb::{ + protocol::xproto::{ChangeWindowAttributesAux, EventMask}, + xcb_ffi::ReplyError, + }; let change = ChangeWindowAttributesAux::default() .event_mask(EventMask::SUBSTRUCTURE_REDIRECT | EventMask::SUBSTRUCTURE_NOTIFY); diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 0000000..e70cde0 --- /dev/null +++ b/src/state.rs @@ -0,0 +1,51 @@ +use x11rb::connection::Connection; +use x11rb::protocol::xproto::{Atom, ConnectionExt, CreateGCAux, Gcontext, Window}; +use x11rb::xcb_ffi::ReplyOrIdError; + +#[derive(Debug)] +pub struct WmState<'a, C: Connection> { + pub conn: &'a C, + pub screen_num: usize, + pub black_gc: Gcontext, + pub windows: Vec, + pub wm_protocols: Atom, + pub wm_delete_window: Atom, +} + +impl<'a, C: Connection> WmState<'a, C> { + pub fn new(conn: &'a C, screen_num: usize) -> Result, ReplyOrIdError> { + let screen = &conn.setup().roots[screen_num]; + let black_gc = conn.generate_id()?; + let font = conn.generate_id()?; + conn.open_font(font, b"9x15")?; + + let gc_aux = CreateGCAux::new() + .graphics_exposures(0) + .background(screen.white_pixel) + .foreground(screen.black_pixel) + .font(font); + conn.create_gc(black_gc, screen.root, &gc_aux)?; + conn.close_font(font)?; + + let wm_protocols = conn.intern_atom(false, b"WM_PROTOCOLS")?; + let wm_delete_window = conn.intern_atom(false, b"WM_DELETE_WINDOW")?; + + Ok(WmState { + conn: conn, + screen_num: screen_num, + black_gc: black_gc, + windows: Vec::default(), + wm_protocols: wm_protocols.reply()?.atom, + wm_delete_window: wm_delete_window.reply()?.atom, + }) + } +} + +#[derive(Debug)] +pub struct WinState { + pub window: Window, + pub frame_window: Window, + pub x: i16, + pub y: i16, + pub width: u16, +}