19 releases

0.9.0 Nov 12, 2019
0.8.2 Feb 25, 2018
0.8.1 Aug 15, 2017
0.8.0 Jul 11, 2017
0.7.1 Mar 29, 2016

#6 in Graphics APIs

Download history 2032/week @ 2019-10-07 2241/week @ 2019-10-14 2772/week @ 2019-10-21 2747/week @ 2019-10-28 3050/week @ 2019-11-04 3116/week @ 2019-11-11 3977/week @ 2019-11-18 4090/week @ 2019-11-25 3590/week @ 2019-12-02 3845/week @ 2019-12-09 3356/week @ 2019-12-16 2769/week @ 2019-12-23 3704/week @ 2019-12-30 4961/week @ 2020-01-06 3704/week @ 2020-01-13

13,062 downloads per month
Used in 120 crates (23 directly)

MIT license

195KB
3.5K SLoC

Python 2.5K SLoC // 0.2% comments Rust 1K SLoC // 0.4% comments

Rust XCB

Build Status

Rust-XCB is a set of bindings and wrappers for XCB. It uses the XML protocol descriptions from XCB to generate the bindings and the wrappers.

Rust-XCB is only intended as an interface to XCB, so provides nothing above and beyond that.

[dependencies]
xcb = "0.8"

Documentation: http://rtbo.github.io/rust-xcb/xcb/index.html

The bindings

The bindings are generated from the rs_client.py script with help from the xcbgen library (also from XCB). The bindings are inside the ffi module, which also contains the hand-written bindings to the core library.

Bindings reflect the C API almost one for one.

The wrapper

The wrappers are generated from the same files, and provide a safe and more convenient wrapper over the low-level bindings by having automatic destructors for returned data, trait implementations for object "types" and other safe helpers.

Example

Drawing example (checkout for more here and also here)

extern crate xcb;

fn main() {
    let points: &[xcb::Point] = &[
        xcb::Point::new(10, 10),
        xcb::Point::new(10, 20),
        xcb::Point::new(20, 10),
        xcb::Point::new(20, 20),
    ];
    let polyline: &[xcb::Point] = &[
        xcb::Point::new(50, 10 ),
        xcb::Point::new( 5, 20 ),     /* rest of points are relative */
        xcb::Point::new(25, -20),
        xcb::Point::new(10, 10 )
    ];
    let segments: &[xcb::Segment] = &[
        xcb::Segment::new(100, 10, 140, 30),
        xcb::Segment::new(110, 25, 130, 60)
    ];
    let rectangles: &[xcb::Rectangle] = &[
        xcb::Rectangle::new(10, 50, 40, 20),
        xcb::Rectangle::new(80, 50, 10, 40)
    ];
    let arcs: &[xcb::Arc] = &[
        xcb::Arc::new(10, 100, 60, 40, 0, 90 << 6),
        xcb::Arc::new(90, 100, 55, 40, 0, 270 << 6)
    ];


    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 foreground = conn.generate_id();

    xcb::create_gc(&conn, foreground, screen.root(), &[
            (xcb::GC_FOREGROUND, screen.black_pixel()),
            (xcb::GC_GRAPHICS_EXPOSURES, 0),
    ]);

    let win = conn.generate_id();
    xcb::create_window(&conn,
        xcb::COPY_FROM_PARENT as u8,
        win,
        screen.root(),
        0, 0,
        150, 150,
        10,
        xcb::WINDOW_CLASS_INPUT_OUTPUT as u16,
        screen.root_visual(), &[
            (xcb::CW_BACK_PIXEL, screen.white_pixel()),
            (xcb::CW_EVENT_MASK,
             xcb::EVENT_MASK_EXPOSURE | xcb::EVENT_MASK_KEY_PRESS),
        ]
    );
    xcb::map_window(&conn, 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 => {
                        /* We draw the points */
                        xcb::poly_point(&conn, xcb::COORD_MODE_ORIGIN as u8, win,
                            foreground, &points);

                        /* We draw the polygonal line */
                        xcb::poly_line(&conn, xcb::COORD_MODE_PREVIOUS as u8, win,
                            foreground, &polyline);

                        /* We draw the segements */
                        xcb::poly_segment(&conn, win, foreground, &segments);

                        /* We draw the rectangles */
                        xcb::poly_rectangle(&conn, win, foreground, &rectangles);

                        /* We draw the arcs */
                        xcb::poly_arc(&conn, win, foreground, &arcs);

                        /* We flush the request */
                        conn.flush();

                    },
                    xcb::KEY_PRESS => {
                        let key_press : &xcb::KeyPressEvent = unsafe {
                            xcb::cast_event(&event)
                        };
                        println!("Key '{}' pressed", key_press.detail());
                        break;
                    },
                    _ => {}
                }
            }
        }
    }
}

Dependencies