From d9b4ae8b2c6b37311f0bdff12bd438bbad456346 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sun, 12 Jun 2016 12:14:27 -0400 Subject: examples: existing_window --- examples/existing_window.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 examples/existing_window.rs diff --git a/examples/existing_window.rs b/examples/existing_window.rs new file mode 100644 index 0000000..6fa6a21 --- /dev/null +++ b/examples/existing_window.rs @@ -0,0 +1,63 @@ +#![cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))] + +extern crate glutin; + +use std::env; +use std::u64; +use std::process::exit; +mod support; + +use glutin::os::unix::WindowBuilderExt; + +fn resize_callback(width: u32, height: u32) { + println!("Window resized to {}x{}", width, height); +} + +fn usage() { + println!("This example requires a single argument (a hex/decimal X Window ID"); + println!("You can find this with, eg, 'xwininfo -tree'"); +} + +fn main() { + + let args: Vec = env::args().collect(); + + if args.len() != 2 { + usage(); + exit(-1); + } + + let window_id_parse = { + if (&args[1]).starts_with("0x") { + u64::from_str_radix(args[1].trim_left_matches('0').trim_left_matches('x'), 16) + } else { + args[1].parse::() + } + }; + let window_id = window_id_parse.expect("Failed to parse numerical arg"); + + let mut window = glutin::WindowBuilder::new() + .from_existing_window(window_id) + .build() + .unwrap(); + window.set_window_resize_callback(Some(resize_callback as fn(u32, u32))); + let _ = unsafe { window.make_current() }; + + println!("Pixel format of the window: {:?}", window.get_pixel_format()); + + let context = support::load(&window); + + println!("Mouse over target window to draw; events will be printed here"); + + for event in window.wait_events() { + context.draw_frame((0.0, 1.0, 0.0, 1.0)); + let _ = window.swap_buffers(); + + println!("{:?}", event); + + match event { + glutin::Event::Closed => break, + _ => () + } + } +} -- cgit v1.2.3