web browser (Servo)
git clone https://git.lucas.co/cce-browser.git
examples/wpe_loop.rs (2.4K)
1 //! Demonstrates the calloop integration pattern: **block on GLib's fds**
2 //! rather than pumping on a timer.
3 //!
4 //! This is what the app does — `Application::register_sources` registers
5 //! `host.poll_fd()` as a calloop `Generic` firing a `Message::Spin` that
6 //! calls `pump`, and the runner's idle sleep ends at `host.glib_deadline()`.
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 // The host's vi channel and scripts.
30 #[path = "../src/vi.rs"]
31 #[allow(dead_code)]
32 mod vi;
33
34 #[path = "../src/wpe/mod.rs"]
35 mod wpe;
36
37 #[cfg(feature = "wpe")]
38 fn main() {
39 use rustix::event::{poll, PollFd, PollFlags};
40 use std::time::{Duration, Instant};
41
42 let blocking = std::env::args().nth(1).as_deref() != Some("poll");
43 let url = std::env::args()
44 .nth(2)
45 .unwrap_or_else(|| "https://example.com".into());
46 let mut host = wpe::WebKitHost::new(url::Url::parse(&url).unwrap(), (1200, 800));
47
48 let (mut wakeups, mut frames) = (0u32, 0u32);
49 let start = Instant::now();
50 while start.elapsed() < Duration::from_secs(8) {
51 if blocking {
52 // Sleep until GLib has work, or until it asked to be woken.
53 let ms = host
54 .glib_deadline()
55 .map(|t| t.saturating_duration_since(Instant::now()).as_millis() as i32)
56 .unwrap_or(1000)
57 .clamp(0, 1000);
58 if let Some(fd) = host.poll_fd() {
59 let mut fds = [PollFd::from_borrowed_fd(fd, PollFlags::IN)];
60 let _ = poll(&mut fds, ms);
61 }
62 } else {
63 std::thread::sleep(Duration::from_millis(16)); // the old way
64 }
65 wakeups += 1;
66 if host.pump().0 {
67 frames += 1;
68 }
69 host.frame_drawn();
70 }
71
72 println!(
73 "{:<9} {wakeups:>5} wakeups {frames:>3} frames over 8s title={:?}",
74 if blocking { "blocking" } else { "polling" },
75 host.title()
76 );
77 }