Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ documentation = "https://docs.rs/scrap"
keywords = ["screen", "capture", "record"]
license = "MIT"
authors = ["Ram <quadrupleslap@gmail.com>"]
edition = "2018"

[dependencies]
block = "0.1"
Expand Down
25 changes: 13 additions & 12 deletions src/common/dxgi.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
use dxgi;
use crate::dxgi;

use std::io::ErrorKind::{NotFound, TimedOut, WouldBlock};
use std::{io, ops};
use std::io::ErrorKind::{WouldBlock, TimedOut, NotFound};

pub struct Capturer {
inner: dxgi::Capturer,
width: usize,
height: usize
height: usize,
}

impl Capturer {
pub fn new(display: Display) -> io::Result<Capturer> {
let width = display.width();
let height = display.height();
let inner = dxgi::Capturer::new(&display.0)?;
Ok(Capturer { inner, width, height })
Ok(Capturer {
inner,
width,
height,
})
}

pub fn width(&self) -> usize {
Expand All @@ -28,10 +33,8 @@ impl Capturer {
const MILLISECONDS_PER_FRAME: u32 = 0;
match self.inner.frame(MILLISECONDS_PER_FRAME) {
Ok(frame) => Ok(Frame(frame)),
Err(ref error) if error.kind() == TimedOut => {
Err(WouldBlock.into())
},
Err(error) => Err(error)
Err(ref error) if error.kind() == TimedOut => Err(WouldBlock.into()),
Err(error) => Err(error),
}
}
}
Expand All @@ -51,14 +54,12 @@ impl Display {
pub fn primary() -> io::Result<Display> {
match dxgi::Displays::new()?.next() {
Some(inner) => Ok(Display(inner)),
None => Err(NotFound.into())
None => Err(NotFound.into()),
}
}

pub fn all() -> io::Result<Vec<Display>> {
Ok(dxgi::Displays::new()?
.map(Display)
.collect::<Vec<_>>())
Ok(dxgi::Displays::new()?.map(Display).collect::<Vec<_>>())
}

pub fn width(&self) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cfg_if! {
cfg_if::cfg_if! {
if #[cfg(quartz)] {
mod quartz;
pub use self::quartz::*;
Expand Down
41 changes: 17 additions & 24 deletions src/common/quartz.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use quartz;
use std::{io, ops, mem};
use crate::quartz;

use std::marker::PhantomData;
use std::sync::{Arc, Mutex, TryLockError};
use std::{io, mem, ops};

pub struct Capturer {
inner: quartz::Capturer,
frame: Arc<Mutex<Option<quartz::Frame>>>
frame: Arc<Mutex<Option<quartz::Frame>>>,
}

impl Capturer {
Expand All @@ -23,8 +24,9 @@ impl Capturer {
if let Ok(mut f) = f.lock() {
*f = Some(inner);
}
}
).map_err(|_| io::Error::from(io::ErrorKind::Other))?;
},
)
.map_err(|_| io::Error::from(io::ErrorKind::Other))?;

Ok(Capturer { inner, frame })
}
Expand All @@ -44,27 +46,20 @@ impl Capturer {
mem::swap(&mut frame, &mut handle);

match frame {
Some(frame) =>
Ok(Frame(frame, PhantomData)),
Some(frame) => Ok(Frame(frame, PhantomData)),

None =>
Err(io::ErrorKind::WouldBlock.into())
None => Err(io::ErrorKind::WouldBlock.into()),
}
}

Err(TryLockError::WouldBlock) =>
Err(io::ErrorKind::WouldBlock.into()),
Err(TryLockError::WouldBlock) => Err(io::ErrorKind::WouldBlock.into()),

Err(TryLockError::Poisoned(..)) =>
Err(io::ErrorKind::Other.into())
Err(TryLockError::Poisoned(..)) => Err(io::ErrorKind::Other.into()),
}
}
}

pub struct Frame<'a>(
quartz::Frame,
PhantomData<&'a [u8]>
);
pub struct Frame<'a>(quartz::Frame, PhantomData<&'a [u8]>);

impl<'a> ops::Deref for Frame<'a> {
type Target = [u8];
Expand All @@ -81,13 +76,11 @@ impl Display {
}

pub fn all() -> io::Result<Vec<Display>> {
Ok(
quartz::Display::online()
.map_err(|_| io::Error::from(io::ErrorKind::Other))?
.into_iter()
.map(Display)
.collect()
)
Ok(quartz::Display::online()
.map_err(|_| io::Error::from(io::ErrorKind::Other))?
.into_iter()
.map(Display)
.collect())
}

pub fn width(&self) -> usize {
Expand Down
11 changes: 6 additions & 5 deletions src/common/x11.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use x11;
use std::{io, ops};
use crate::x11;

use std::rc::Rc;
use std::{io, ops};

pub struct Capturer(x11::Capturer);

Expand Down Expand Up @@ -37,7 +38,7 @@ impl Display {
pub fn primary() -> io::Result<Display> {
let server = Rc::new(match x11::Server::default() {
Ok(server) => server,
Err(_) => return Err(io::ErrorKind::ConnectionRefused.into())
Err(_) => return Err(io::ErrorKind::ConnectionRefused.into()),
});

let mut displays = x11::Server::displays(server);
Expand All @@ -48,14 +49,14 @@ impl Display {

match best {
Some(best) => Ok(Display(best)),
None => Err(io::ErrorKind::NotFound.into())
None => Err(io::ErrorKind::NotFound.into()),
}
}

pub fn all() -> io::Result<Vec<Display>> {
let server = Rc::new(match x11::Server::default() {
Ok(server) => server,
Err(_) => return Err(io::ErrorKind::ConnectionRefused.into())
Err(_) => return Err(io::ErrorKind::ConnectionRefused.into()),
});

Ok(x11::Server::displays(server).map(Display).collect())
Expand Down
32 changes: 10 additions & 22 deletions src/dxgi/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
use winapi::{
GUID,
HRESULT,
REFIID,
IDXGIFactory1,
IDXGIAdapter,
D3D_DRIVER_TYPE,
HMODULE,
UINT,
ID3D11Device,
D3D_FEATURE_LEVEL,
ID3D11DeviceContext
ID3D11Device, ID3D11DeviceContext, IDXGIAdapter, IDXGIFactory1, D3D_DRIVER_TYPE,
D3D_FEATURE_LEVEL, GUID, HMODULE, HRESULT, REFIID, UINT,
};

pub const DXGI_MAP_READ: UINT = 1;
Expand All @@ -18,37 +9,34 @@ pub const IID_IDXGIFACTORY1: GUID = GUID {
Data1: 0x770aae78,
Data2: 0xf26f,
Data3: 0x4dba,
Data4: [0xa8, 0x29, 0x25, 0x3c, 0x83, 0xd1, 0xb3, 0x87]
Data4: [0xa8, 0x29, 0x25, 0x3c, 0x83, 0xd1, 0xb3, 0x87],
};

pub const IID_IDXGIOUTPUT1: GUID = GUID {
Data1: 0x00cddea8,
Data2: 0x939b,
Data3: 0x4b83,
Data4: [0xa3, 0x40, 0xa6, 0x85, 0x22, 0x66, 0x66, 0xcc]
Data4: [0xa3, 0x40, 0xa6, 0x85, 0x22, 0x66, 0x66, 0xcc],
};

pub const IID_IDXGISURFACE: GUID = GUID {
Data1: 3405559148,
Data2: 27331,
Data3: 18569,
Data4: [191, 71, 158, 35, 187, 210, 96, 236]
Data4: [191, 71, 158, 35, 187, 210, 96, 236],
};

pub const IID_ID3D11TEXTURE2D: GUID = GUID {
Data1: 1863690994,
Data2: 53768,
Data3: 20105,
Data4: [154, 180, 72, 149, 53, 211, 79, 156]
Data4: [154, 180, 72, 149, 53, 211, 79, 156],
};

#[link(name="dxgi")]
#[link(name="d3d11")]
#[link(name = "dxgi")]
#[link(name = "d3d11")]
extern "system" {
pub fn CreateDXGIFactory1(
id: REFIID,
pp_factory: *mut *mut IDXGIFactory1
) -> HRESULT;
pub fn CreateDXGIFactory1(id: REFIID, pp_factory: *mut *mut IDXGIFactory1) -> HRESULT;

pub fn D3D11CreateDevice(
pAdapter: *mut IDXGIAdapter,
Expand All @@ -60,6 +48,6 @@ extern "system" {
SDKVersion: UINT,
ppDevice: *mut *mut ID3D11Device,
pFeatureLevel: *mut D3D_FEATURE_LEVEL,
ppImmediateContext: *mut *mut ID3D11DeviceContext
ppImmediateContext: *mut *mut ID3D11DeviceContext,
) -> HRESULT;
}
Loading