Last active
September 15, 2020 18:38
-
-
Save charlesvdv/de36473893913f7ebbf342047ea8c4ae to your computer and use it in GitHub Desktop.
xcb + cairo test (translated in Rust from https://www.cairographics.org/cookbook/xcbsurface.c/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "xcb_cairo" | |
version = "0.1.0" | |
authors = ["Charles Vandevoorde <charles.vandevoorde@hotmail.be>"] | |
[dependencies] | |
xcb = "0.7.6" | |
[dependencies.cairo-rs] | |
git = "https://github.com/charlesvdv/cairo" | |
branch = "master" | |
default-features = false | |
features = ["xcb"] | |
[dependencies.cairo-sys-rs] | |
git = "https://github.com/charlesvdv/cairo" | |
branch = "master" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate xcb; | |
extern crate cairo_sys; | |
extern crate cairo; | |
use cairo::XCBSurface; | |
fn get_root_visual_type(screen: &xcb::Screen) -> xcb::Visualtype { | |
for depth in screen.allowed_depths() { | |
for visual in depth.visuals() { | |
if screen.root_visual() == visual.visual_id() { | |
return visual; | |
} | |
} | |
} | |
panic!("No visual type found"); | |
} | |
fn create_cairo_context(conn: &xcb::Connection, | |
screen: &xcb::Screen, | |
window: &xcb::Window) | |
-> cairo::Context { | |
let surface; | |
unsafe { | |
let cairo_conn = cairo::XCBConnection::from_raw_none(conn.get_raw_conn() as | |
*mut cairo_sys::xcb_connection_t); | |
let visual_ptr: *mut cairo_sys::xcb_visualtype_t = | |
&mut get_root_visual_type(&screen).base as *mut _ as *mut cairo_sys::xcb_visualtype_t; | |
let visual = cairo::XCBVisualType::from_raw_none(visual_ptr); | |
let cairo_screen = cairo::XCBDrawable(window.to_owned()); | |
surface = cairo::Surface::create(&cairo_conn, &cairo_screen, &visual, 150, 150); | |
} | |
cairo::Context::new(&surface) | |
} | |
fn main() { | |
let (conn, screen_num) = xcb::Connection::connect(None).unwrap(); | |
let setup = conn.get_setup(); | |
let screen = setup.roots().nth(screen_num as usize).unwrap(); | |
let win = conn.generate_id(); | |
xcb::create_window(&conn, | |
xcb::COPY_FROM_PARENT as u8, | |
win, | |
screen.root(), | |
20, | |
20, | |
150, | |
150, | |
0, | |
xcb::WINDOW_CLASS_INPUT_OUTPUT as u16, | |
screen.root_visual(), | |
&[(xcb::CW_EVENT_MASK, | |
xcb::EVENT_MASK_EXPOSURE | xcb::EVENT_MASK_KEY_PRESS), | |
(xcb::CW_OVERRIDE_REDIRECT, 1)]); | |
xcb::map_window(&conn, win); | |
let cr = create_cairo_context(&conn, &screen, &win); | |
conn.flush(); | |
loop { | |
let event = conn.wait_for_event(); | |
match event { | |
None => { | |
break; | |
} | |
Some(event) => { | |
let r = event.response_type() & !0x80; | |
match r { | |
xcb::EXPOSE => { | |
println!("EXPOSED"); | |
cr.set_source_rgb(0.5, 0.5, 0.5); | |
cr.paint(); | |
cr.set_source_rgb(1.0, 0.0, 0.0); | |
cr.move_to(0.0, 0.0); | |
cr.line_to(150.0, 0.0); | |
cr.line_to(150.0, 150.0); | |
cr.close_path(); | |
cr.fill(); | |
cr.set_source_rgb(0.0, 0.0, 1.0); | |
cr.set_line_width(20.0); | |
cr.move_to(0.0, 150.0); | |
cr.move_to(150.0, 0.0); | |
cr.stroke(); | |
} | |
xcb::KEY_PRESS => { | |
let key_press: &xcb::KeyPressEvent = xcb::cast_event(&event); | |
println!("Key '{}' pressed", key_press.detail()); | |
break; | |
} | |
_ => {} | |
} | |
conn.flush(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment