1 mod utils;
2 use crate::utils::*;
3 
4 use drm::control::Device as ControlDevice;
5 
6 use drm::buffer::DrmFourcc;
7 
8 use drm::control::{connector, crtc};
9 
main()10 pub fn main() {
11     let card = Card::open_global();
12 
13     // Load the information.
14     let res = card
15         .resource_handles()
16         .expect("Could not load normal resource ids.");
17     let coninfo: Vec<connector::Info> = res
18         .connectors()
19         .iter()
20         .flat_map(|con| card.get_connector(*con, true))
21         .collect();
22     let crtcinfo: Vec<crtc::Info> = res
23         .crtcs()
24         .iter()
25         .flat_map(|crtc| card.get_crtc(*crtc))
26         .collect();
27 
28     // Filter each connector until we find one that's connected.
29     let con = coninfo
30         .iter()
31         .find(|&i| i.state() == connector::State::Connected)
32         .expect("No connected connectors");
33 
34     // Get the first (usually best) mode
35     let &mode = con.modes().first().expect("No modes found on connector");
36 
37     let (disp_width, disp_height) = mode.size();
38 
39     // Find a crtc and FB
40     let crtc = crtcinfo.first().expect("No crtcs found");
41 
42     // Select the pixel format
43     let fmt = DrmFourcc::Xrgb8888;
44 
45     // Create a DB
46     // If buffer resolution is larger than display resolution, an ENOSPC (not enough video memory)
47     // error may occur
48     let mut db = card
49         .create_dumb_buffer((disp_width.into(), disp_height.into()), fmt, 32)
50         .expect("Could not create dumb buffer");
51 
52     // Map it and grey it out.
53     {
54         let mut map = card
55             .map_dumb_buffer(&mut db)
56             .expect("Could not map dumbbuffer");
57         for b in map.as_mut() {
58             *b = 128;
59         }
60     }
61 
62     // Create an FB:
63     let fb = card
64         .add_framebuffer(&db, 24, 32)
65         .expect("Could not create FB");
66 
67     println!("{:#?}", mode);
68     println!("{:#?}", fb);
69     println!("{:#?}", db);
70 
71     // Set the crtc
72     // On many setups, this requires root access.
73     card.set_crtc(crtc.handle(), Some(fb), (0, 0), &[con.handle()], Some(mode))
74         .expect("Could not set CRTC");
75 
76     let five_seconds = ::std::time::Duration::from_millis(5000);
77     ::std::thread::sleep(five_seconds);
78 
79     card.destroy_framebuffer(fb).unwrap();
80     card.destroy_dumb_buffer(db).unwrap();
81 }
82