aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs59
1 files changed, 49 insertions, 10 deletions
diff --git a/src/lib.rs b/src/lib.rs
index c0f537d..c73fc0c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,21 +6,45 @@ extern crate image;
extern crate getopts;
use std::env;
-use std::u64;
use std::process::exit;
use getopts::{Options, Matches};
use glium::glutin::os::unix::WindowBuilderExt;
+use glium::{DisplayBuild, Display};
pub mod util;
-fn print_usage(program: &str, opts: Options) {
+pub trait ExuberantHack {
+ fn draw_frame(&mut self) -> Result<(), String>;
+ fn get_display(&self) -> &Display;
+}
+
+/// Prints usage/help info for an individual hack
+pub fn print_usage(opts: &Options) {
+ let args: Vec<String> = env::args().collect();
+ let program = args[0].clone();
let brief = format!("Usage: {} [options]", program);
print!("{}", opts.usage(&brief));
}
-pub fn generic_main(mut opts: Options) -> (Matches, Option<u64>) {
+/// Executes a hack
+pub fn run(hack: &mut ExuberantHack, conf: &Matches) {
+
+ loop {
+ hack.draw_frame().ok();
+ for ev in hack.get_display().poll_events() {
+ match ev {
+ glium::glutin::Event::Closed => return,
+ _ => ()
+ }
+ }
+ // XXX: sleep here for 10ms
+ }
+}
+
+/// Adds and parses most command-line options.
+/// This function handles all the generic stuff, like --window-id and --help.
+pub fn main_helper(mut opts: Options) -> Matches {
let args: Vec<String> = env::args().collect();
- let program = args[0].clone();
// Turn, eg, "-root" into "--root"
let args = util::convert_xscreensaver_args(args);
@@ -29,13 +53,14 @@ pub fn generic_main(mut opts: Options) -> (Matches, Option<u64>) {
opts.optflag("h", "help", "print this help menu");
opts.optflag("", "window", "run in a window (IGNORED)");
opts.optflag("", "root", "run in root window (IGNORED)");
- opts.optflag("", "fps", "show frames per second (IGNORED)");
opts.optopt("", "window-id", "X window id number", "NUM");
+ opts.optflag("", "fps", "show frames per second (IGNORED)");
+ opts.optopt("", "delay", "inter-frame delay (0 to 100000) (IGNORED)", "NUM");
let matches = match opts.parse(&args[1..]) {
Ok(m) => { m }
Err(f) => {
- print_usage(&program, opts);
+ print_usage(&opts);
println!("");
println!("{}", f.to_string());
exit(-1);
@@ -43,14 +68,19 @@ pub fn generic_main(mut opts: Options) -> (Matches, Option<u64>) {
};
if matches.opt_present("help") {
- print_usage(&program, opts);
+ print_usage(&opts);
exit(0);
}
+ matches
+}
+
+pub fn make_display(conf: &Matches) -> glium::Display {
+
// if no "--window-id", try environment variable (arg has priority though)
let window_id_string: Option<String> =
- matches.opt_str("window-id")
- .or(env::var("XSCREENSAVER_WINDOW").ok());
+ conf.opt_str("window-id")
+ .or(env::var("XSCREENSAVER_WINDOW").ok());
let window_id = window_id_string.map(|id| match util::dechex2u64(&id) {
Ok(y) => y,
@@ -63,5 +93,14 @@ pub fn generic_main(mut opts: Options) -> (Matches, Option<u64>) {
println!("Drawing on existing X window: 0x{:07X}", window_id.unwrap());
}
- (matches, window_id)
+ let win_builder: glium::glutin::WindowBuilder = match window_id {
+ Some(id) =>
+ glium::glutin::WindowBuilder::new()
+ .from_existing_window(id),
+ None => glium::glutin::WindowBuilder::new()
+ .with_title(format!("Exuberant Cow!"))
+ .with_depth_buffer(24),
+ };
+ let display = win_builder.build_glium().unwrap();
+ display
}