Commit b4683140 authored by jD91mZM2's avatar jD91mZM2

Make it compile (but not work D:)

parent 3d521ecd
[dependencies.std]
features = ["panic_unwind", "backtrace"]
#[dependencies.test]
#stage = 1
#![feature(asm)]
#[macro_use] extern crate failure;
extern crate event;
extern crate orbclient;
......@@ -5,16 +7,27 @@ extern crate orbfont;
extern crate orbimage;
extern crate syscall;
pub mod rect;
pub mod image;
use event::EventQueue;
use image::{Image, ImageRef};
use orbclient::{Color, Event, EventOption, Renderer};
use rect::Rect;
use std::{
cell::RefCell,
env,
fs::File,
io,
io::{self, Read, Write},
iter,
mem,
os::unix::io::{AsRawFd, FromRawFd},
path::PathBuf,
process::Command,
rc::Rc,
slice,
};
use syscall::data::Packet;
use syscall::flag::{O_CLOEXEC, O_CREAT, O_NONBLOCK, O_RDWR};
#[derive(Debug, Fail)]
......@@ -43,9 +56,26 @@ pub fn fix_env(display_path: &str) {
env::set_var("PATH", new_path);
}
unsafe fn read_to_slice<R: Read, T: Copy>(mut r: R, buf: &mut [T]) -> io::Result<usize> {
r.read(slice::from_raw_parts_mut(
buf.as_mut_ptr() as *mut u8,
buf.len() * mem::size_of::<T>())
).map(|count| count/mem::size_of::<T>())
}
unsafe fn display_fd_map(width: i32, height: i32, display_fd: usize) -> ImageRef<'static> {
let display_ptr = syscall::fmap(display_fd, 0, (width * height * 4) as usize).unwrap();
let display_slice = slice::from_raw_parts_mut(display_ptr as *mut Color, (width * height) as usize);
ImageRef::from_data(width, height, display_slice)
}
unsafe fn display_fd_unmap(image: &mut ImageRef) {
let _ = syscall::funmap(image.data().as_ptr() as usize);
}
pub struct Orbital {
pub socket: File,
pub display: File,
pub image: ImageRef<'static>,
pub width: i32,
pub height: i32
......@@ -87,16 +117,34 @@ impl Orbital {
let width = res.split("/").nth(1).unwrap_or("").parse::<i32>().unwrap_or(0);
let height = res.split("/").nth(2).unwrap_or("").parse::<i32>().unwrap_or(0);
let image = unsafe { display_fd_map(width, height, display_fd) };
Ok(Orbital {
socket: socket,
display: display,
image: image,
width: width,
height: height
})
}
pub fn run<I>(&mut self, login_cmd: &str, args: I) -> Result<(), Error>
where I: IntoIterator<Item = String>
pub fn display_send(&mut self, event: &Event) -> io::Result<()> {
self.display.write_all(event)
}
pub fn display_sync(&mut self) -> io::Result<()> {
self.display.sync_all()
}
pub fn socket_send(&mut self, packet: &Packet) -> io::Result<()> {
self.socket.write_all(packet)
}
pub fn screen_rect(&self) -> Rect {
Rect::new(0, 0, self.image.width(), self.image.height())
}
/// Start the main loop
pub fn run<I, HD, HS>(self, login_cmd: &str, args: I, mut handle_display: HD, mut handle_socket: HS) -> Result<(), Error>
where I: IntoIterator<Item = String>,
HD: FnMut(&mut Orbital, &mut [Event]) -> io::Result<()> + 'static,
HS: FnMut(&mut Orbital, &mut [Packet]) -> io::Result<()> + 'static
{
Command::new(&login_cmd)
.args(args)
......@@ -109,13 +157,42 @@ impl Orbital {
let socket_fd = self.socket.as_raw_fd();
let display_fd = self.display.as_raw_fd();
event_queue.add(display_fd, move |_| -> io::Result<Option<()>> {
// TODO: handle display events
let me = Rc::new(RefCell::new(self));
let me2 = Rc::clone(&me);
event_queue.add(socket_fd, move |_| -> io::Result<Option<()>> {
let mut me = me2.borrow_mut();
let mut packets = [Packet::default(); 16];
let mut all: Vec<Packet> = Vec::new();
loop {
match unsafe { read_to_slice(&mut me.socket, &mut packets) }? {
0 => break,
count => all.extend(&packets[..count])
}
}
handle_socket(&mut me, &mut packets)?;
Ok(None)
})?;
event_queue.add(socket_fd, move |_| -> io::Result<Option<()>> {
// TODO: handle socket events
event_queue.add(display_fd, move |_| -> io::Result<Option<()>> {
let mut me = me.borrow_mut();
let mut events = [Event::new(); 16];
let mut all: Vec<Event> = Vec::new();
loop {
match unsafe { read_to_slice(&mut me.display, &mut events) }? {
0 => break,
count => all.extend(&events[..count])
}
}
for event in &all {
if let EventOption::Resize(event) = event.to_option() {
unsafe {
display_fd_unmap(&mut me.image);
me.image = display_fd_map(event.width as i32, event.height as i32, display_fd as usize);
}
}
}
handle_display(&mut me, &mut events)?;
Ok(None)
})?;
......@@ -127,3 +204,10 @@ impl Orbital {
Ok(())
}
}
impl Drop for Orbital {
fn drop(&mut self) {
unsafe {
display_fd_unmap(&mut self.image);
}
}
}
#![deny(warnings)]
#![feature(asm)]
//#![deny(warnings)]
#![feature(const_fn)]
extern crate orbital;
......@@ -14,22 +13,22 @@ extern crate syscall;
extern crate toml;
use event::EventQueue;
use orbclient::Event;
use orbital::Orbital;
use std::cell::RefCell;
use std::env;
use std::fs::File;
use std::io::{Error, Result};
use std::os::unix::io::{asrawfd, fromrawfd};
use std::os::unix::io::{AsRawFd, FromRawFd};
use std::path::PathBuf;
use std::rc::Rc;
use syscall::flag::{O_CLOEXEC, O_CREAT, O_NONBLOCK, O_RDWR};
use syscall::data::Packet;
use config::Config;
use scheme::OrbitalScheme;
mod config;
mod image;
mod rect;
mod scheme;
mod theme;
mod window;
......@@ -55,12 +54,19 @@ fn main() {
let scheme = Rc::new(RefCell::new(OrbitalScheme::new(
display.width,
display.height,
display.socket,
display.display,
&config
)));
let scheme2 = Rc::clone(&scheme);
display.run(&login_cmd, args).expect("orbital: failed to launch");
let handle_display = move |orb: &mut Orbital, events: &mut [Event]| {
scheme.borrow_mut().with_orbital(orb).display_event(events)
};
let handle_socket = move |orb: &mut Orbital, packets: &mut [Packet]| {
scheme2.borrow_mut().with_orbital(orb).scheme_event(packets)
};
display.run(&login_cmd, args, handle_display, handle_socket)
.expect("orbital: failed to launch");
},
Err(err) => println!("orbital: could not register orbital: {}", err)
}
......
This diff is collapsed.
use orbclient::{Color, Event, Renderer};
use orbfont::Font;
use orbital::image::{Image, ImageRef};
use orbital::rect::Rect;
use std::cmp::{min, max};
use std::collections::VecDeque;
use std::mem::size_of;
use std::ptr;
use image::{Image, ImageRef};
use rect::Rect;
use theme::{BAR_COLOR, BAR_HIGHLIGHT_COLOR, TEXT_COLOR, TEXT_HIGHLIGHT_COLOR};
use syscall::error::{Error, Result, EINVAL};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment