Blame view

xcb-test/src/main.rs 7.39 KB
Georg Hopp authored
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
extern crate xcb;

use std::iter::{Iterator};
use std::{thread, time};
use std::sync::Arc;
use std::ptr::{null, null_mut};
use std::slice::from_raw_parts_mut;

pub fn getshm<'a>(size :usize) -> (i32, &'a mut [u32]) {
    unsafe {
        let id = libc::shmget( libc::IPC_PRIVATE
                             , size * 4
                             , libc::IPC_CREAT | 0o744 );
        let ptr = libc::shmat(id, null(), 0);
        (id as i32, from_raw_parts_mut(ptr as *mut u32, size))
    }
}

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 )
                                   // rest of points are relative
                                   , xcb::Point::new( 5, 20 )
                                   , 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) = {
        let (conn, screen_num) = xcb::Connection::connect(None).unwrap();
        (Arc::new(conn), screen_num)
    };
    let setup = conn.get_setup();
    let screen = setup.roots().nth(screen_num as usize).unwrap();

    let (shmid, shm) = getshm(150 * 150);
    let shmseg       = conn.generate_id();
    xcb::shm::attach(&conn, shmseg, shmid as u32, false);
    unsafe { libc::shmctl(shmid, libc::IPC_RMID, null_mut()); }

    let foreground = conn.generate_id();
    let pix        = conn.generate_id();

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

    let window = conn.generate_id();
    let values = [ ( xcb::CW_BACK_PIXEL, screen.white_pixel() )
                 , ( xcb::CW_EVENT_MASK, xcb::EVENT_MASK_EXPOSURE
                                       | xcb::EVENT_MASK_KEY_PRESS
                                       | xcb::EVENT_MASK_STRUCTURE_NOTIFY
                                       | xcb::EVENT_MASK_PROPERTY_CHANGE ) ];
    xcb::create_window( &conn
                      , xcb::COPY_FROM_PARENT as u8
                      , window
                      , screen.root()
                      , 0, 0, 150, 150, 0
                      , xcb::WINDOW_CLASS_INPUT_OUTPUT as u16
                      , screen.root_visual()
                      , &values);

    xcb::shm::create_pixmap( &conn
                           , pix
                           , window
                           , 150, 150
                           , screen.root_depth()
                           , shmseg
                           , 0 );

    xcb::map_window(&conn, window);

    {
        let conn = conn.clone();

        thread::spawn(move || {
            let mut blink = false;

            let mut i = 0;
            loop {
                let title = if blink {
                    "Basic Threaded Window ;-)"
                } else {
                    "Basic Threaded Window :-)"
                };

                shm[i] = 0xFFFFFF;
                i = (i + 1) % (150 * 150);

                let c = xcb::change_property_checked(
                      &conn
                    , xcb::PROP_MODE_REPLACE as u8
                    , window
                    , xcb::ATOM_WM_NAME
                    , xcb::ATOM_STRING
                    , 8
                    , title.as_bytes() );

                xcb::copy_area( &conn
                              , pix
                              , window
                              , foreground
                              , 0, 0, 0, 0
                              , 150, 150);

                if conn.has_error().is_err() || c.request_check().is_err() {
                    break;
                }

                blink = !blink;
                thread::sleep(time::Duration::from_millis(500));
            }
        });
    }

    conn.flush();

    loop {
        let event = conn.wait_for_event();

        match event {
            None        => break,
            Some(event) => {
                match event.response_type() & !0x80 {
                    xcb::PROPERTY_NOTIFY => {
                        let prop_notify: &xcb::PropertyNotifyEvent = unsafe {
                            xcb::cast_event(&event)
                        };

                        if prop_notify.atom() == xcb::ATOM_WM_NAME {
                            // retrieving title
                            let cookie = xcb::get_property( &conn
                                                          , false
                                                          , window
                                                          , xcb::ATOM_WM_NAME
                                                          , xcb::ATOM_STRING
                                                          , 0, 1024);

                            if let Ok(reply) = cookie.get_reply() {
                                let r = reply.value();
                                let r = std::str::from_utf8(r).unwrap();

                                println!( "title changed to \"{}\"", r);
                            }
                        }
                    },

                    xcb::EXPOSE => {
                        xcb::poly_point( &conn
                                       , xcb::COORD_MODE_ORIGIN as u8
                                       , window
                                       , foreground
                                       , &points );
                        xcb::poly_line( &conn
                                      , xcb::COORD_MODE_PREVIOUS as u8
                                      , window
                                      , foreground
                                      , &polyline );
                        xcb::poly_segment( &conn
                                         , window
                                         , foreground
                                         , &segments );
                        xcb::poly_rectangle( &conn
                                           , window
                                           , foreground
                                           , &rectangles );
                        xcb::poly_arc( &conn
                                     , window
                                     , foreground
                                     , &arcs );

                        conn.flush();
                    },

                    xcb::KEY_PRESS => {
                        let key_press: &xcb::KeyPressEvent = unsafe {
                            xcb::cast_event(&event)
                        };

                        println!("Key '{}' pressed", key_press.detail());

                        if key_press.detail() == 0x18 { // Q (on qwerty)
                            break;
                        }
                    },

                    _ => {},
                }
            }
        }
    }
}