git.lucas.co / cce-browser
web browser (Servo)
git clone https://git.lucas.co/cce-browser.git

examples/wpe_loop.rs (2.2K)

 1 //! Demonstrates the calloop integration pattern: **block on GLib's fds**
 2 //! rather than pumping on a timer.
 3 //!
 4 //! This is what `Application::register_sources` will do — register
 5 //! `host.poll_fd()` as a calloop `Generic` and a timer for
 6 //! `host.poll_timeout()`, both firing a `Message::Spin` that calls `pump`.
 7 //! Here the same thing is done with a bare `poll(2)` so the behaviour can be
 8 //! measured without a compositor.
 9 //!
10 //! Run with `--features wpe`; pass `poll` as argv[1] to compare against the
11 //! old fixed-interval polling.
12 
13 #[cfg(not(feature = "wpe"))]
14 fn main() {
15     eprintln!("build with --features wpe");
16 }
17 
18 #[cfg(feature = "wpe")]
19 #[derive(Debug, Clone, Copy)]
20 pub enum EditingCommand { Copy, Cut, Paste }
21 
22 #[cfg(feature = "wpe")]
23 #[path = "../src/pages.rs"]
24 mod pages;
25 #[cfg(feature = "wpe")]
26 #[path = "../src/downloads.rs"]
27 mod downloads;
28 #[cfg(feature = "wpe")]
29 #[path = "../src/wpe/mod.rs"]
30 mod wpe;
31 
32 #[cfg(feature = "wpe")]
33 fn main() {
34     use rustix::event::{poll, PollFd, PollFlags};
35     use std::time::{Duration, Instant};
36 
37     let blocking = std::env::args().nth(1).as_deref() != Some("poll");
38     let url = std::env::args()
39         .nth(2)
40         .unwrap_or_else(|| "https://example.com".into());
41     let mut host = wpe::WebKitHost::new(url::Url::parse(&url).unwrap(), (1200, 800));
42 
43     let (mut wakeups, mut frames) = (0u32, 0u32);
44     let start = Instant::now();
45     while start.elapsed() < Duration::from_secs(8) {
46         if blocking {
47             // Sleep until GLib has work, or until it asked to be woken.
48             let ms = host
49                 .poll_timeout()
50                 .map(|d| d.as_millis() as i32)
51                 .unwrap_or(1000)
52                 .clamp(0, 1000);
53             if let Some(fd) = host.poll_fd() {
54                 let mut fds = [PollFd::from_borrowed_fd(fd, PollFlags::IN)];
55                 let _ = poll(&mut fds, ms);
56             }
57         } else {
58             std::thread::sleep(Duration::from_millis(16)); // the old way
59         }
60         wakeups += 1;
61         if host.pump().0 {
62             frames += 1;
63         }
64     }
65 
66     println!(
67         "{:<9} {wakeups:>5} wakeups  {frames:>3} frames  over 8s   title={:?}",
68         if blocking { "blocking" } else { "polling" },
69         host.title()
70     );
71 }