From 7971feda4f03c3ae52fa430e7e1752bb3a760134 Mon Sep 17 00:00:00 2001 From: "Brian J. Burg" Date: Fri, 19 Oct 2012 12:45:59 -0700 Subject: [PATCH 001/166] Initial commit --- .gitignore | 11 +++++++++++ Makefile.in | 23 +++++++++++++++++++++++ configure | 5 +++++ core-graphics.rc | 8 ++++++++ data_provider.rs | 16 ++++++++++++++++ font.rs | 11 +++++++++++ 6 files changed, 74 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile.in create mode 100755 configure create mode 100644 core-graphics.rc create mode 100644 data_provider.rs create mode 100644 font.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5f68b7f --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +*~ +*# +*.o +*.a +*.so +*.dylib +*.dSYM +*.dll +*.dummy +cocoa-test +build diff --git a/Makefile.in b/Makefile.in new file mode 100644 index 0000000..cfc1469 --- /dev/null +++ b/Makefile.in @@ -0,0 +1,23 @@ +VPATH=%VPATH% + +RUSTC ?= rustc +RUSTFLAGS ?= + +RUST_SRC=$(shell find $(VPATH)/. -type f -name '*.rs') + +.PHONY: all clean +all: librustcoregraphics.dummy + +librustcoregraphics.dummy: core_graphics.rc $(RUST_SRC) + $(RUSTC) $(RUSTFLAGS) $< -o $@ + touch $@ + +rustcoregraphics-test: core_graphics.rc $(RUST_SRC) + $(RUSTC) $(RUSTFLAGS) $< -o $@ --test + +check: rustcoregraphics-test + ./rustcoregraphics-test + +clean: + rm -f *.o *.a *.so *.dylib *.dll *.dummy + diff --git a/configure b/configure new file mode 100755 index 0000000..ed6c06e --- /dev/null +++ b/configure @@ -0,0 +1,5 @@ +#!/bin/bash + +SRCDIR="$(cd $(dirname $0) && pwd)" +sed "s#%VPATH%#${SRCDIR}#" ${SRCDIR}/Makefile.in > Makefile + diff --git a/core-graphics.rc b/core-graphics.rc new file mode 100644 index 0000000..1c73cc3 --- /dev/null +++ b/core-graphics.rc @@ -0,0 +1,8 @@ +#[link(name = "core_graphics", + vers = "0.1")]; +#[crate_type = "lib"]; + +extern mod std; + +pub mod data_provider; +pub mod font; \ No newline at end of file diff --git a/data_provider.rs b/data_provider.rs new file mode 100644 index 0000000..8890c1b --- /dev/null +++ b/data_provider.rs @@ -0,0 +1,16 @@ +use libc::{c_void, c_char, size_t}; + +pub type CGDataProviderRef = *c_void; +pub type CGDataProviderReleaseDataCallback = *c_void; + +#[link_args="-framework CoreGraphics"] +#[nolink] +extern { + pub fn CGDataProviderCreateWithFilename(filename: *c_char) -> CGDataProviderRef; + pub fn CGDataProviderCreateWithData(info: *c_void, + data: *c_void, + size: size_t, + releaseData: CGDataProviderReleaseDataCallback + ) -> CGDataProviderRef; + pub fn CGDataProviderRelease(provider: CGDataProviderRef); +} diff --git a/font.rs b/font.rs new file mode 100644 index 0000000..02d6a7a --- /dev/null +++ b/font.rs @@ -0,0 +1,11 @@ +use libc::{c_void, c_char, size_t}; +use data_provider::CGDataProviderRef; + +pub type CGFontRef = *c_void; + +#[link_args="-framework CoreGraphics"] +#[nolink] +extern { + pub fn CGFontCreateWithDataProvider(provider: CGDataProviderRef) -> CGFontRef; + pub fn CGFontRelease(font: CGFontRef); +} From 8e575a3cfdcbf81fd2838b3a8e149ff09328fd3d Mon Sep 17 00:00:00 2001 From: "Brian J. Burg" Date: Fri, 19 Oct 2012 12:59:37 -0700 Subject: [PATCH 002/166] Rename the crate file --- core-graphics.rc => core_graphics.rc | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename core-graphics.rc => core_graphics.rc (100%) diff --git a/core-graphics.rc b/core_graphics.rc similarity index 100% rename from core-graphics.rc rename to core_graphics.rc From 6291af5e127bcaf527d689e8a93183c3288e1efe Mon Sep 17 00:00:00 2001 From: "Brian J. Burg" Date: Fri, 19 Oct 2012 13:12:20 -0700 Subject: [PATCH 003/166] Fix linked framework name to ApplicationServices. --- data_provider.rs | 2 +- font.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/data_provider.rs b/data_provider.rs index 8890c1b..3e5389a 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -3,8 +3,8 @@ use libc::{c_void, c_char, size_t}; pub type CGDataProviderRef = *c_void; pub type CGDataProviderReleaseDataCallback = *c_void; -#[link_args="-framework CoreGraphics"] #[nolink] +#[link_args="-framework ApplicationServices"] extern { pub fn CGDataProviderCreateWithFilename(filename: *c_char) -> CGDataProviderRef; pub fn CGDataProviderCreateWithData(info: *c_void, diff --git a/font.rs b/font.rs index 02d6a7a..260d436 100644 --- a/font.rs +++ b/font.rs @@ -3,8 +3,8 @@ use data_provider::CGDataProviderRef; pub type CGFontRef = *c_void; -#[link_args="-framework CoreGraphics"] #[nolink] +#[link_args="-framework ApplicationServices"] extern { pub fn CGFontCreateWithDataProvider(provider: CGDataProviderRef) -> CGFontRef; pub fn CGFontRelease(font: CGFontRef); From b0389b5fbff058daac267caf067f2c6fae9b9a98 Mon Sep 17 00:00:00 2001 From: "Brian J. Burg" Date: Fri, 19 Oct 2012 14:22:11 -0700 Subject: [PATCH 004/166] Straighten out bindings with CF and CT. --- base.rs | 9 +++++++++ core_graphics.rc | 4 +++- font.rs | 1 + geometry.rs | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 base.rs create mode 100644 geometry.rs diff --git a/base.rs b/base.rs new file mode 100644 index 0000000..450ffdb --- /dev/null +++ b/base.rs @@ -0,0 +1,9 @@ +use libc::c_double; + +// this file defines CGFloat, as well as stubbed data types. + +// TODO: this is actually a libc::c_float on 32bit +pub type CGFloat = libc::c_double; + +pub type CGAffineTransform = (); + diff --git a/core_graphics.rc b/core_graphics.rc index 1c73cc3..8be3cd9 100644 --- a/core_graphics.rc +++ b/core_graphics.rc @@ -4,5 +4,7 @@ extern mod std; +pub mod base; pub mod data_provider; -pub mod font; \ No newline at end of file +pub mod font; +pub mod geometry; \ No newline at end of file diff --git a/font.rs b/font.rs index 260d436..6923f1b 100644 --- a/font.rs +++ b/font.rs @@ -2,6 +2,7 @@ use libc::{c_void, c_char, size_t}; use data_provider::CGDataProviderRef; pub type CGFontRef = *c_void; +pub type CGGlyph = libc::c_ushort; #[nolink] #[link_args="-framework ApplicationServices"] diff --git a/geometry.rs b/geometry.rs new file mode 100644 index 0000000..3745553 --- /dev/null +++ b/geometry.rs @@ -0,0 +1,16 @@ +use base::CGFloat; + +pub struct CGSize { + width: CGFloat, + height: CGFloat, +} + +pub struct CGPoint { + x: CGFloat, + y: CGFloat, +} + +pub struct CGRect { + origin: CGPoint, + size: CGSize +} From f971c917baeb3bc5e6dfb1442bfa35a10261b650 Mon Sep 17 00:00:00 2001 From: "Brian J. Burg" Date: Thu, 8 Nov 2012 10:53:10 -0800 Subject: [PATCH 005/166] 'make check' should remove test products. --- Makefile.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile.in b/Makefile.in index cfc1469..7f9a494 100644 --- a/Makefile.in +++ b/Makefile.in @@ -19,5 +19,5 @@ check: rustcoregraphics-test ./rustcoregraphics-test clean: - rm -f *.o *.a *.so *.dylib *.dll *.dummy + rm -f *.o *.a *.so *.dylib *.dll *.dummy rustcoregraphics-test From 9cb470e80897a854096bc07b88857e379945f421 Mon Sep 17 00:00:00 2001 From: "Brian J. Burg" Date: Thu, 8 Nov 2012 17:56:50 -0800 Subject: [PATCH 006/166] Fill out CGDataProvider bindings. --- core_graphics.rc | 1 + data_provider.rs | 88 ++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 79 insertions(+), 10 deletions(-) diff --git a/core_graphics.rc b/core_graphics.rc index 8be3cd9..3dd5410 100644 --- a/core_graphics.rc +++ b/core_graphics.rc @@ -3,6 +3,7 @@ #[crate_type = "lib"]; extern mod std; +extern mod core_foundation; pub mod base; pub mod data_provider; diff --git a/data_provider.rs b/data_provider.rs index 3e5389a..a38fd45 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -1,16 +1,84 @@ +use cf = core_foundation; +use cf::base::{ + AbstractCFType, + AbstractCFTypeRef, + CFIndex, + CFRelease, + CFTypeID, + CFTypeRef, +}; + use libc::{c_void, c_char, size_t}; -pub type CGDataProviderRef = *c_void; -pub type CGDataProviderReleaseDataCallback = *c_void; +pub type CGDataProviderGetBytesCallback = *u8; +pub type CGDataProviderReleaseInfoCallback = *u8; +pub type CGDataProviderRewindCallback = *u8; +pub type CGDataProviderSkipBytesCallback = *u8; +pub type CGDataProviderSkipForwardCallback = *u8; + +pub type CGDataProviderGetBytePointerCallback = *u8; +pub type CGDataProviderGetBytesAtOffsetCallback = *u8; +pub type CGDataProviderReleaseBytePointerCallback = *u8; +pub type CGDataProviderReleaseDataCallback = *u8; +pub type CGDataProviderGetBytesAtPositionCallback = *u8; + +struct __CGDataProvider { private: () } +pub type CGDataProviderRef = *__CGDataProvider; + +pub impl CGDataProviderRef : AbstractCFTypeRef { + pure fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } +} + +pub struct CGDataProvider { + obj: CGDataProviderRef, + + drop { + unsafe { + CFRelease(cast::transmute(self.obj)) + } + } +} + +pub impl CGDataProvider : AbstractCFType { + pure fn get_ref() -> CGDataProviderRef { self.obj } + + static fn wrap(obj: CGDataProviderRef) -> CGDataProvider { + CGDataProvider { obj: obj } + } + + static fn unwrap(wrapper: CGDataProvider) -> CGDataProviderRef { + wrapper.obj + } +} + +pub impl CGDataProvider { + static fn new_from_buffer(buf: *u8, len: uint) -> CGDataProvider unsafe { + let obj = CGDataProviderCreateWithData( + ptr::null(), + cast::transmute(buf), + len as size_t, + ptr::null()); + + return cf::base::wrap(obj); + } +} + #[nolink] #[link_args="-framework ApplicationServices"] -extern { - pub fn CGDataProviderCreateWithFilename(filename: *c_char) -> CGDataProviderRef; - pub fn CGDataProviderCreateWithData(info: *c_void, - data: *c_void, - size: size_t, - releaseData: CGDataProviderReleaseDataCallback - ) -> CGDataProviderRef; - pub fn CGDataProviderRelease(provider: CGDataProviderRef); +pub extern { + //fn CGDataProviderCopyData + //fn CGDataProviderCreateDirect + //fn CGDataProviderCreateSequential + //fn CGDataProviderCreateWithCFData + fn CGDataProviderCreateWithData(info: *c_void, + data: *c_void, + size: size_t, + releaseData: CGDataProviderReleaseDataCallback + ) -> CGDataProviderRef; + fn CGDataProviderCreateWithFilename(filename: *c_char) -> CGDataProviderRef; + //fn CGDataProviderCreateWithURL + fn CGDataProviderGetTypeID() -> CFTypeID; + fn CGDataProviderRelease(provider: CGDataProviderRef); + fn CGDataProviderRetain(provider: CGDataProviderRef) -> CGDataProviderRef; } From afaadb32a7fa732e8f0d86357d424b98d1e48883 Mon Sep 17 00:00:00 2001 From: "Brian J. Burg" Date: Thu, 15 Nov 2012 18:27:38 -0800 Subject: [PATCH 007/166] Rewrite the Core Foundation wrapper system. --- data_provider.rs | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/data_provider.rs b/data_provider.rs index a38fd45..9013020 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -1,11 +1,10 @@ use cf = core_foundation; use cf::base::{ - AbstractCFType, AbstractCFTypeRef, CFIndex, - CFRelease, CFTypeID, CFTypeRef, + CFWrapper, }; use libc::{c_void, c_char, size_t}; @@ -29,37 +28,21 @@ pub impl CGDataProviderRef : AbstractCFTypeRef { pure fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } } -pub struct CGDataProvider { - obj: CGDataProviderRef, +pub type CGDataProvider = CFWrapper; - drop { - unsafe { - CFRelease(cast::transmute(self.obj)) - } - } +pub trait CGDataProviderMethods { + static fn new_from_buffer(buf: *u8, len: uint) -> CGDataProvider; } -pub impl CGDataProvider : AbstractCFType { - pure fn get_ref() -> CGDataProviderRef { self.obj } - - static fn wrap(obj: CGDataProviderRef) -> CGDataProvider { - CGDataProvider { obj: obj } - } - - static fn unwrap(wrapper: CGDataProvider) -> CGDataProviderRef { - wrapper.obj - } -} - -pub impl CGDataProvider { +pub impl CGDataProvider : CGDataProviderMethods { static fn new_from_buffer(buf: *u8, len: uint) -> CGDataProvider unsafe { - let obj = CGDataProviderCreateWithData( + let result = CGDataProviderCreateWithData( ptr::null(), cast::transmute(buf), len as size_t, ptr::null()); - return cf::base::wrap(obj); + CFWrapper::wrap_owned(result) } } From 255ddca46619a6f2bc7c50e1da37096752a07545 Mon Sep 17 00:00:00 2001 From: "Brian J. Burg" Date: Fri, 16 Nov 2012 11:20:13 -0800 Subject: [PATCH 008/166] Move CGDataProvider static methods outside of trait/impl. --- data_provider.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/data_provider.rs b/data_provider.rs index 9013020..529c67f 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -30,12 +30,7 @@ pub impl CGDataProviderRef : AbstractCFTypeRef { pub type CGDataProvider = CFWrapper; -pub trait CGDataProviderMethods { - static fn new_from_buffer(buf: *u8, len: uint) -> CGDataProvider; -} - -pub impl CGDataProvider : CGDataProviderMethods { - static fn new_from_buffer(buf: *u8, len: uint) -> CGDataProvider unsafe { +pub fn new_from_buffer(buf: *u8, len: uint) -> CGDataProvider unsafe { let result = CGDataProviderCreateWithData( ptr::null(), cast::transmute(buf), @@ -43,10 +38,8 @@ pub impl CGDataProvider : CGDataProviderMethods { ptr::null()); CFWrapper::wrap_owned(result) - } } - #[nolink] #[link_args="-framework ApplicationServices"] pub extern { From 55de520630efbaef7de03832b6d3621d2b4b4d29 Mon Sep 17 00:00:00 2001 From: "Brian J. Burg" Date: Fri, 16 Nov 2012 14:30:43 -0800 Subject: [PATCH 009/166] Implement AbstractCFType::type_id(). Switch to use CFTypeRef::downcast(). --- data_provider.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/data_provider.rs b/data_provider.rs index 529c67f..5f16078 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -26,6 +26,7 @@ pub type CGDataProviderRef = *__CGDataProvider; pub impl CGDataProviderRef : AbstractCFTypeRef { pure fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } + static pure fn type_id() -> CFTypeID unsafe { CGDataProviderGetTypeID() } } pub type CGDataProvider = CFWrapper; From c413a96a8c6b70815b4d5ad7469c7983af417728 Mon Sep 17 00:00:00 2001 From: "Brian J. Burg" Date: Fri, 16 Nov 2012 15:18:10 -0800 Subject: [PATCH 010/166] Add binding for CGFont. --- font.rs | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/font.rs b/font.rs index 6923f1b..ea1522c 100644 --- a/font.rs +++ b/font.rs @@ -1,12 +1,48 @@ -use libc::{c_void, c_char, size_t}; -use data_provider::CGDataProviderRef; +use cf = core_foundation; + +use cf::base::{ + AbstractCFTypeRef, + CFIndex, + CFTypeID, + CFTypeRef, + CFWrapper, +}; + +use data_provider::{ + CGDataProvider, + CGDataProviderRef, +}; + +use libc::{c_void, c_char, size_t}; -pub type CGFontRef = *c_void; pub type CGGlyph = libc::c_ushort; +struct __CGFont { private: () } +pub type CGFontRef = *__CGFont; + +pub impl CGFontRef : AbstractCFTypeRef { + pure fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } + static pure fn type_id() -> CFTypeID unsafe { CGFontGetTypeID() } +} + +pub type CGFont = CFWrapper; + +pub fn create_with_data_provider(provider: &CGDataProvider) -> CGFont { + // TODO: error handling + let value = CGFontCreateWithDataProvider(*provider.borrow_ref()); + CFWrapper::wrap_owned(value) +} + #[nolink] #[link_args="-framework ApplicationServices"] extern { - pub fn CGFontCreateWithDataProvider(provider: CGDataProviderRef) -> CGFontRef; - pub fn CGFontRelease(font: CGFontRef); + + // TODO: basically nothing has bindings (even commented-out) besides what we use. + fn CGFontCreateWithDataProvider(provider: CGDataProviderRef) -> CGFontRef; + fn CGFontGetTypeID() -> CFTypeID; + + // These do the same thing as CFRetain/CFRelease, except + // gracefully handle a NULL argument. We don't use them. + fn CGFontRetain(font: CGFontRef); + fn CGFontRelease(font: CGFontRef); } From fb1ea4368fd766239bd3578d66c4218fa46f1e5e Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 13 Dec 2012 18:38:55 -0800 Subject: [PATCH 011/166] Update for language changes --- data_provider.rs | 9 +-------- font.rs | 15 ++------------- 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/data_provider.rs b/data_provider.rs index 5f16078..c7054db 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -1,11 +1,4 @@ -use cf = core_foundation; -use cf::base::{ - AbstractCFTypeRef, - CFIndex, - CFTypeID, - CFTypeRef, - CFWrapper, -}; +use core_foundation::base::{AbstractCFTypeRef, CFIndex, CFTypeID, CFTypeRef, CFWrapper}; use libc::{c_void, c_char, size_t}; diff --git a/font.rs b/font.rs index ea1522c..9fdd7a7 100644 --- a/font.rs +++ b/font.rs @@ -1,17 +1,6 @@ -use cf = core_foundation; +use core_foundation::base::{AbstractCFTypeRef, CFIndex, CFTypeID, CFTypeRef, CFWrapper}; -use cf::base::{ - AbstractCFTypeRef, - CFIndex, - CFTypeID, - CFTypeRef, - CFWrapper, -}; - -use data_provider::{ - CGDataProvider, - CGDataProviderRef, -}; +use data_provider::{CGDataProvider, CGDataProviderRef}; use libc::{c_void, c_char, size_t}; From 4ef941f09e58c122c96f15cecc7bd97e145bed01 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 30 Jan 2013 14:33:39 -0800 Subject: [PATCH 012/166] Rust upgrade --- data_provider.rs | 6 ++++-- font.rs | 10 ++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/data_provider.rs b/data_provider.rs index c7054db..d4268e9 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -19,12 +19,13 @@ pub type CGDataProviderRef = *__CGDataProvider; pub impl CGDataProviderRef : AbstractCFTypeRef { pure fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } - static pure fn type_id() -> CFTypeID unsafe { CGDataProviderGetTypeID() } + static pure fn type_id() -> CFTypeID { unsafe { CGDataProviderGetTypeID() } } } pub type CGDataProvider = CFWrapper; -pub fn new_from_buffer(buf: *u8, len: uint) -> CGDataProvider unsafe { +pub fn new_from_buffer(buf: *u8, len: uint) -> CGDataProvider { + unsafe { let result = CGDataProviderCreateWithData( ptr::null(), cast::transmute(buf), @@ -32,6 +33,7 @@ pub fn new_from_buffer(buf: *u8, len: uint) -> CGDataProvider unsafe { ptr::null()); CFWrapper::wrap_owned(result) + } } #[nolink] diff --git a/font.rs b/font.rs index 9fdd7a7..5f504d5 100644 --- a/font.rs +++ b/font.rs @@ -11,15 +11,17 @@ pub type CGFontRef = *__CGFont; pub impl CGFontRef : AbstractCFTypeRef { pure fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } - static pure fn type_id() -> CFTypeID unsafe { CGFontGetTypeID() } + static pure fn type_id() -> CFTypeID { unsafe { CGFontGetTypeID() } } } pub type CGFont = CFWrapper; pub fn create_with_data_provider(provider: &CGDataProvider) -> CGFont { - // TODO: error handling - let value = CGFontCreateWithDataProvider(*provider.borrow_ref()); - CFWrapper::wrap_owned(value) + unsafe { + // TODO: error handling + let value = CGFontCreateWithDataProvider(*provider.borrow_ref()); + CFWrapper::wrap_owned(value) + } } #[nolink] From f53aaa1cdc5b210d5390be71b92e7c8cc51ac8d2 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 7 Feb 2013 12:20:20 -0800 Subject: [PATCH 013/166] Update for language changes --- data_provider.rs | 7 ++++++- font.rs | 9 +++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/data_provider.rs b/data_provider.rs index d4268e9..394ab9e 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -19,7 +19,12 @@ pub type CGDataProviderRef = *__CGDataProvider; pub impl CGDataProviderRef : AbstractCFTypeRef { pure fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } - static pure fn type_id() -> CFTypeID { unsafe { CGDataProviderGetTypeID() } } + + static pure fn type_id() -> CFTypeID { + unsafe { + CGDataProviderGetTypeID() + } + } } pub type CGDataProvider = CFWrapper; diff --git a/font.rs b/font.rs index 5f504d5..bc8b948 100644 --- a/font.rs +++ b/font.rs @@ -11,14 +11,19 @@ pub type CGFontRef = *__CGFont; pub impl CGFontRef : AbstractCFTypeRef { pure fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } - static pure fn type_id() -> CFTypeID { unsafe { CGFontGetTypeID() } } + + static pure fn type_id() -> CFTypeID { + unsafe { + CGFontGetTypeID() + } + } } pub type CGFont = CFWrapper; pub fn create_with_data_provider(provider: &CGDataProvider) -> CGFont { + // TODO: error handling unsafe { - // TODO: error handling let value = CGFontCreateWithDataProvider(*provider.borrow_ref()); CFWrapper::wrap_owned(value) } From bc86ccb773758b3e2ad7f689b67f77165048b9e6 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Tue, 19 Feb 2013 17:51:10 -0800 Subject: [PATCH 014/166] Update for language changes --- data_provider.rs | 2 +- font.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/data_provider.rs b/data_provider.rs index 394ab9e..1847830 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -17,7 +17,7 @@ pub type CGDataProviderGetBytesAtPositionCallback = *u8; struct __CGDataProvider { private: () } pub type CGDataProviderRef = *__CGDataProvider; -pub impl CGDataProviderRef : AbstractCFTypeRef { +pub impl AbstractCFTypeRef for CGDataProviderRef { pure fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } static pure fn type_id() -> CFTypeID { diff --git a/font.rs b/font.rs index bc8b948..5cd9c37 100644 --- a/font.rs +++ b/font.rs @@ -9,7 +9,7 @@ pub type CGGlyph = libc::c_ushort; struct __CGFont { private: () } pub type CGFontRef = *__CGFont; -pub impl CGFontRef : AbstractCFTypeRef { +pub impl AbstractCFTypeRef for CGFontRef { pure fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } static pure fn type_id() -> CFTypeID { From 93171d52db4ca911525e659334f2d188c599d804 Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Wed, 27 Mar 2013 10:55:52 -0600 Subject: [PATCH 015/166] Update to new syntax and removed unused imports. --- base.rs | 2 -- data_provider.rs | 8 ++++---- font.rs | 8 +++----- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/base.rs b/base.rs index 450ffdb..5e2797d 100644 --- a/base.rs +++ b/base.rs @@ -1,5 +1,3 @@ -use libc::c_double; - // this file defines CGFloat, as well as stubbed data types. // TODO: this is actually a libc::c_float on 32bit diff --git a/data_provider.rs b/data_provider.rs index 1847830..758d090 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -1,6 +1,6 @@ use core_foundation::base::{AbstractCFTypeRef, CFIndex, CFTypeID, CFTypeRef, CFWrapper}; -use libc::{c_void, c_char, size_t}; +use core::libc::{c_void, c_char, size_t}; pub type CGDataProviderGetBytesCallback = *u8; pub type CGDataProviderReleaseInfoCallback = *u8; @@ -17,10 +17,10 @@ pub type CGDataProviderGetBytesAtPositionCallback = *u8; struct __CGDataProvider { private: () } pub type CGDataProviderRef = *__CGDataProvider; -pub impl AbstractCFTypeRef for CGDataProviderRef { - pure fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } +impl AbstractCFTypeRef for CGDataProviderRef { + fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } - static pure fn type_id() -> CFTypeID { + fn type_id() -> CFTypeID { unsafe { CGDataProviderGetTypeID() } diff --git a/font.rs b/font.rs index 5cd9c37..dde0402 100644 --- a/font.rs +++ b/font.rs @@ -2,17 +2,15 @@ use core_foundation::base::{AbstractCFTypeRef, CFIndex, CFTypeID, CFTypeRef, CFW use data_provider::{CGDataProvider, CGDataProviderRef}; -use libc::{c_void, c_char, size_t}; - pub type CGGlyph = libc::c_ushort; struct __CGFont { private: () } pub type CGFontRef = *__CGFont; -pub impl AbstractCFTypeRef for CGFontRef { - pure fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } +impl AbstractCFTypeRef for CGFontRef { + fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } - static pure fn type_id() -> CFTypeID { + fn type_id() -> CFTypeID { unsafe { CGFontGetTypeID() } From 44a3c6c430cb6d5eb54e133f9b88bf1e8bd5a982 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 3 Apr 2013 18:19:09 -0700 Subject: [PATCH 016/166] Add license --- COPYING | 5 ++ LICENSE-APACHE | 201 +++++++++++++++++++++++++++++++++++++++++++++++++ LICENSE-MIT | 25 ++++++ 3 files changed, 231 insertions(+) create mode 100644 COPYING create mode 100644 LICENSE-APACHE create mode 100644 LICENSE-MIT diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..8b7291a --- /dev/null +++ b/COPYING @@ -0,0 +1,5 @@ +Licensed under the Apache License, Version 2.0 or the MIT license +, at your +option. All files in the project carrying such notice may not be +copied, modified, or distributed except according to those terms. diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 0000000..16fe87b --- /dev/null +++ b/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..807526f --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2012-2013 Mozilla Foundation + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. From 6126aa8f1d1c733b2ea1847e030dd00c0b6a518b Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Wed, 3 Apr 2013 22:07:19 -0600 Subject: [PATCH 017/166] Remove unused imports. --- data_provider.rs | 2 +- font.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/data_provider.rs b/data_provider.rs index 758d090..75e98ce 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -1,4 +1,4 @@ -use core_foundation::base::{AbstractCFTypeRef, CFIndex, CFTypeID, CFTypeRef, CFWrapper}; +use core_foundation::base::{AbstractCFTypeRef, CFTypeID, CFTypeRef, CFWrapper}; use core::libc::{c_void, c_char, size_t}; diff --git a/font.rs b/font.rs index dde0402..4e1efac 100644 --- a/font.rs +++ b/font.rs @@ -1,4 +1,4 @@ -use core_foundation::base::{AbstractCFTypeRef, CFIndex, CFTypeID, CFTypeRef, CFWrapper}; +use core_foundation::base::{AbstractCFTypeRef, CFTypeID, CFTypeRef, CFWrapper}; use data_provider::{CGDataProvider, CGDataProviderRef}; From 8082be782bb64468136a3bc024dc8c813246784f Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 4 Apr 2013 19:04:31 -0700 Subject: [PATCH 018/166] Add license headers --- COPYING => COPYRIGHT | 0 base.rs | 9 +++++++++ core_graphics.rc | 9 +++++++++ data_provider.rs | 9 +++++++++ font.rs | 9 +++++++++ geometry.rs | 9 +++++++++ 6 files changed, 45 insertions(+) rename COPYING => COPYRIGHT (100%) diff --git a/COPYING b/COPYRIGHT similarity index 100% rename from COPYING rename to COPYRIGHT diff --git a/base.rs b/base.rs index 5e2797d..90a495e 100644 --- a/base.rs +++ b/base.rs @@ -1,3 +1,12 @@ +// Copyright 2013 The Servo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + // this file defines CGFloat, as well as stubbed data types. // TODO: this is actually a libc::c_float on 32bit diff --git a/core_graphics.rc b/core_graphics.rc index 3dd5410..03e52d8 100644 --- a/core_graphics.rc +++ b/core_graphics.rc @@ -1,3 +1,12 @@ +// Copyright 2013 The Servo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + #[link(name = "core_graphics", vers = "0.1")]; #[crate_type = "lib"]; diff --git a/data_provider.rs b/data_provider.rs index 75e98ce..80cb092 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -1,3 +1,12 @@ +// Copyright 2013 The Servo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + use core_foundation::base::{AbstractCFTypeRef, CFTypeID, CFTypeRef, CFWrapper}; use core::libc::{c_void, c_char, size_t}; diff --git a/font.rs b/font.rs index 4e1efac..5a3a9a9 100644 --- a/font.rs +++ b/font.rs @@ -1,3 +1,12 @@ +// Copyright 2013 The Servo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + use core_foundation::base::{AbstractCFTypeRef, CFTypeID, CFTypeRef, CFWrapper}; use data_provider::{CGDataProvider, CGDataProviderRef}; diff --git a/geometry.rs b/geometry.rs index 3745553..5e9ba8b 100644 --- a/geometry.rs +++ b/geometry.rs @@ -1,3 +1,12 @@ +// Copyright 2013 The Servo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + use base::CGFloat; pub struct CGSize { From 557db505525d759e05d342f87195cccfc9aa4d15 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 2 May 2013 16:37:17 -0700 Subject: [PATCH 019/166] Update for language changes --- font.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/font.rs b/font.rs index 5a3a9a9..dc86609 100644 --- a/font.rs +++ b/font.rs @@ -26,13 +26,32 @@ impl AbstractCFTypeRef for CGFontRef { } } -pub type CGFont = CFWrapper; +pub struct CGFont { + contents: CFWrapper +} + +impl CGFont { + /// Convenience method to make it easier to wrap external `CGFont` instances. + pub fn wrap_shared(font: CGFontRef) -> CGFont { + CGFont { + contents: CFWrapper::wrap_shared(font) + } + } +} + +impl Clone for CGFont { + fn clone(&self) -> CGFont { + CGFont::wrap_shared(*self.contents.borrow_ref()) + } +} pub fn create_with_data_provider(provider: &CGDataProvider) -> CGFont { // TODO: error handling unsafe { let value = CGFontCreateWithDataProvider(*provider.borrow_ref()); - CFWrapper::wrap_owned(value) + CGFont { + contents: CFWrapper::wrap_owned(value) + } } } From 9c02dc748f9f8d3065d309febba296369aed10bf Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 2 May 2013 19:21:22 -0700 Subject: [PATCH 020/166] Add CGFont::wrap_owned --- font.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/font.rs b/font.rs index dc86609..60e2deb 100644 --- a/font.rs +++ b/font.rs @@ -31,6 +31,13 @@ pub struct CGFont { } impl CGFont { + /// Convenience method to make it easier to wrap external `CGFont` instances. + pub fn wrap_owned(font: CGFontRef) -> CGFont { + CGFont { + contents: CFWrapper::wrap_owned(font) + } + } + /// Convenience method to make it easier to wrap external `CGFont` instances. pub fn wrap_shared(font: CGFontRef) -> CGFont { CGFont { From bf9d0ebf3e0283f8bdb505d29d93de9631f7877c Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 25 Jun 2013 19:47:08 -0700 Subject: [PATCH 021/166] Update for language changes --- base.rs | 2 ++ data_provider.rs | 4 +++- font.rs | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/base.rs b/base.rs index 90a495e..5e1a6c7 100644 --- a/base.rs +++ b/base.rs @@ -9,6 +9,8 @@ // this file defines CGFloat, as well as stubbed data types. +use std::libc; + // TODO: this is actually a libc::c_float on 32bit pub type CGFloat = libc::c_double; diff --git a/data_provider.rs b/data_provider.rs index 80cb092..c96dd0d 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -9,7 +9,9 @@ use core_foundation::base::{AbstractCFTypeRef, CFTypeID, CFTypeRef, CFWrapper}; -use core::libc::{c_void, c_char, size_t}; +use std::cast; +use std::libc::{c_void, c_char, size_t}; +use std::ptr; pub type CGDataProviderGetBytesCallback = *u8; pub type CGDataProviderReleaseInfoCallback = *u8; diff --git a/font.rs b/font.rs index 60e2deb..994ceee 100644 --- a/font.rs +++ b/font.rs @@ -10,6 +10,7 @@ use core_foundation::base::{AbstractCFTypeRef, CFTypeID, CFTypeRef, CFWrapper}; use data_provider::{CGDataProvider, CGDataProviderRef}; +use std::libc; pub type CGGlyph = libc::c_ushort; From 9b0026bf6d7c78fa6a4572a58ba9b9ae43aeaf3e Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Thu, 15 Aug 2013 18:43:50 -0600 Subject: [PATCH 022/166] Fix visibility for mac. --- data_provider.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_provider.rs b/data_provider.rs index c96dd0d..91bedc2 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -54,7 +54,7 @@ pub fn new_from_buffer(buf: *u8, len: uint) -> CGDataProvider { #[nolink] #[link_args="-framework ApplicationServices"] -pub extern { +extern { //fn CGDataProviderCopyData //fn CGDataProviderCreateDirect //fn CGDataProviderCreateSequential From 2893fd675c56f28cc0e653c7a7bb35a25ba0a032 Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Fri, 16 Aug 2013 14:25:40 -0600 Subject: [PATCH 023/166] Switch from -o to --out-dir. --- Makefile.in | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile.in b/Makefile.in index 7f9a494..5d20573 100644 --- a/Makefile.in +++ b/Makefile.in @@ -9,11 +9,11 @@ RUST_SRC=$(shell find $(VPATH)/. -type f -name '*.rs') all: librustcoregraphics.dummy librustcoregraphics.dummy: core_graphics.rc $(RUST_SRC) - $(RUSTC) $(RUSTFLAGS) $< -o $@ + $(RUSTC) $(RUSTFLAGS) $< --out-dir . touch $@ rustcoregraphics-test: core_graphics.rc $(RUST_SRC) - $(RUSTC) $(RUSTFLAGS) $< -o $@ --test + $(RUSTC) $(RUSTFLAGS) $< --out-dir . --test check: rustcoregraphics-test ./rustcoregraphics-test From 55169ff9faa6375e4939e614a4e35faef0f5d227 Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Fri, 16 Aug 2013 14:46:53 -0700 Subject: [PATCH 024/166] Revert to using -o for tests --- Makefile.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile.in b/Makefile.in index 5d20573..508c733 100644 --- a/Makefile.in +++ b/Makefile.in @@ -13,7 +13,7 @@ librustcoregraphics.dummy: core_graphics.rc $(RUST_SRC) touch $@ rustcoregraphics-test: core_graphics.rc $(RUST_SRC) - $(RUSTC) $(RUSTFLAGS) $< --out-dir . --test + $(RUSTC) $(RUSTFLAGS) $< -o $@ --test check: rustcoregraphics-test ./rustcoregraphics-test From 4c4662a5d6077b547466d33c9a00cb33df9f6f26 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 29 Aug 2013 15:33:42 -0700 Subject: [PATCH 025/166] Update for language changes --- data_provider.rs | 2 ++ font.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/data_provider.rs b/data_provider.rs index 91bedc2..ee94628 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -31,6 +31,7 @@ pub type CGDataProviderRef = *__CGDataProvider; impl AbstractCFTypeRef for CGDataProviderRef { fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } + #[fixed_stack_segment] fn type_id() -> CFTypeID { unsafe { CGDataProviderGetTypeID() @@ -40,6 +41,7 @@ impl AbstractCFTypeRef for CGDataProviderRef { pub type CGDataProvider = CFWrapper; +#[fixed_stack_segment] pub fn new_from_buffer(buf: *u8, len: uint) -> CGDataProvider { unsafe { let result = CGDataProviderCreateWithData( diff --git a/font.rs b/font.rs index 994ceee..2bc7bf5 100644 --- a/font.rs +++ b/font.rs @@ -20,6 +20,7 @@ pub type CGFontRef = *__CGFont; impl AbstractCFTypeRef for CGFontRef { fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } + #[fixed_stack_segment] fn type_id() -> CFTypeID { unsafe { CGFontGetTypeID() @@ -53,6 +54,7 @@ impl Clone for CGFont { } } +#[fixed_stack_segment] pub fn create_with_data_provider(provider: &CGDataProvider) -> CGFont { // TODO: error handling unsafe { From adb526a6ca8deee37152ef3ba1197894286e2204 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Mon, 2 Sep 2013 17:08:31 +0100 Subject: [PATCH 026/166] Upgrade to rustc 0.8-pre (0ac3e02 2013-08-30 05:45:45 -0700) --- data_provider.rs | 2 +- font.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/data_provider.rs b/data_provider.rs index ee94628..01e3044 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -32,7 +32,7 @@ impl AbstractCFTypeRef for CGDataProviderRef { fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } #[fixed_stack_segment] - fn type_id() -> CFTypeID { + fn type_id(_dummy: Option) -> CFTypeID { unsafe { CGDataProviderGetTypeID() } diff --git a/font.rs b/font.rs index 2bc7bf5..d826d0c 100644 --- a/font.rs +++ b/font.rs @@ -21,7 +21,7 @@ impl AbstractCFTypeRef for CGFontRef { fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } #[fixed_stack_segment] - fn type_id() -> CFTypeID { + fn type_id(_dummy: Option) -> CFTypeID { unsafe { CGFontGetTypeID() } From f6dc4f735397843a4ab920929b00cb334724fd19 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 1 Nov 2013 23:17:20 -0700 Subject: [PATCH 027/166] Update for the Core Foundation bindings revamp --- data_provider.rs | 56 +++++++++++++++++++++++---------- font.rs | 80 ++++++++++++++++++++++++++---------------------- 2 files changed, 82 insertions(+), 54 deletions(-) diff --git a/data_provider.rs b/data_provider.rs index 01e3044..9f155aa 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -7,11 +7,12 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use core_foundation::base::{AbstractCFTypeRef, CFTypeID, CFTypeRef, CFWrapper}; +use core_foundation::base::{CFRelease, CFTypeID, TCFType}; use std::cast; use std::libc::{c_void, c_char, size_t}; use std::ptr; +use std::vec; pub type CGDataProviderGetBytesCallback = *u8; pub type CGDataProviderReleaseInfoCallback = *u8; @@ -25,32 +26,53 @@ pub type CGDataProviderReleaseBytePointerCallback = *u8; pub type CGDataProviderReleaseDataCallback = *u8; pub type CGDataProviderGetBytesAtPositionCallback = *u8; -struct __CGDataProvider { private: () } +struct __CGDataProvider; + pub type CGDataProviderRef = *__CGDataProvider; -impl AbstractCFTypeRef for CGDataProviderRef { - fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } +pub struct CGDataProvider { + obj: CGDataProviderRef, +} + +impl Drop for CGDataProvider { + #[fixed_stack_segment] + fn drop(&mut self) { + unsafe { + CFRelease(self.as_CFTypeRef()) + } + } +} + +impl TCFType for CGDataProvider { + fn as_concrete_TypeRef(&self) -> CGDataProviderRef { + self.obj + } + + unsafe fn wrap_under_create_rule(obj: CGDataProviderRef) -> CGDataProvider { + CGDataProvider { + obj: obj, + } + } #[fixed_stack_segment] - fn type_id(_dummy: Option) -> CFTypeID { + #[inline] + fn type_id(_: Option) -> CFTypeID { unsafe { CGDataProviderGetTypeID() } } } -pub type CGDataProvider = CFWrapper; - -#[fixed_stack_segment] -pub fn new_from_buffer(buf: *u8, len: uint) -> CGDataProvider { - unsafe { - let result = CGDataProviderCreateWithData( - ptr::null(), - cast::transmute(buf), - len as size_t, - ptr::null()); - - CFWrapper::wrap_owned(result) +impl CGDataProvider { + #[fixed_stack_segment] + pub fn from_buffer(buffer: &[u8]) -> CGDataProvider { + unsafe { + let result = CGDataProviderCreateWithData(ptr::null(), + cast::transmute(vec::raw::to_ptr(buffer)), + buffer.len() as u64, + ptr::null()); + TCFType::wrap_under_create_rule(result) + } } } diff --git a/font.rs b/font.rs index d826d0c..55b682a 100644 --- a/font.rs +++ b/font.rs @@ -7,60 +7,66 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use core_foundation::base::{AbstractCFTypeRef, CFTypeID, CFTypeRef, CFWrapper}; +use core_foundation::base::{CFRelease, CFTypeID, TCFType}; use data_provider::{CGDataProvider, CGDataProviderRef}; use std::libc; pub type CGGlyph = libc::c_ushort; -struct __CGFont { private: () } +struct __CGFont; + pub type CGFontRef = *__CGFont; -impl AbstractCFTypeRef for CGFontRef { - fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef } +pub struct CGFont { + obj: CGFontRef, +} + +impl Clone for CGFont { + #[fixed_stack_segment] + #[inline] + fn clone(&self) -> CGFont { + unsafe { + TCFType::wrap_under_get_rule(self.obj) + } + } +} + +impl Drop for CGFont { + #[fixed_stack_segment] + fn drop(&mut self) { + unsafe { + CFRelease(self.as_CFTypeRef()) + } + } +} + +impl TCFType for CGFont { + fn as_concrete_TypeRef(&self) -> CGFontRef { + self.obj + } + + unsafe fn wrap_under_create_rule(obj: CGFontRef) -> CGFont { + CGFont { + obj: obj, + } + } #[fixed_stack_segment] - fn type_id(_dummy: Option) -> CFTypeID { + #[inline] + fn type_id(_: Option) -> CFTypeID { unsafe { CGFontGetTypeID() } } } -pub struct CGFont { - contents: CFWrapper -} - impl CGFont { - /// Convenience method to make it easier to wrap external `CGFont` instances. - pub fn wrap_owned(font: CGFontRef) -> CGFont { - CGFont { - contents: CFWrapper::wrap_owned(font) - } - } - - /// Convenience method to make it easier to wrap external `CGFont` instances. - pub fn wrap_shared(font: CGFontRef) -> CGFont { - CGFont { - contents: CFWrapper::wrap_shared(font) - } - } -} - -impl Clone for CGFont { - fn clone(&self) -> CGFont { - CGFont::wrap_shared(*self.contents.borrow_ref()) - } -} - -#[fixed_stack_segment] -pub fn create_with_data_provider(provider: &CGDataProvider) -> CGFont { - // TODO: error handling - unsafe { - let value = CGFontCreateWithDataProvider(*provider.borrow_ref()); - CGFont { - contents: CFWrapper::wrap_owned(value) + #[fixed_stack_segment] + pub fn from_data_provider(provider: CGDataProvider) -> CGFont { + unsafe { + let font_ref = CGFontCreateWithDataProvider(provider.as_concrete_TypeRef()); + TCFType::wrap_under_create_rule(font_ref) } } } From a4b64b245650d36171f1564119c92b5036aac8fc Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Mon, 16 Sep 2013 12:45:26 -0600 Subject: [PATCH 028/166] Convert to rustpkg. --- Makefile.in | 6 +++--- core_graphics.rc => lib.rs | 6 +----- 2 files changed, 4 insertions(+), 8 deletions(-) rename core_graphics.rc => lib.rs (82%) diff --git a/Makefile.in b/Makefile.in index 508c733..bc5f0ea 100644 --- a/Makefile.in +++ b/Makefile.in @@ -8,11 +8,11 @@ RUST_SRC=$(shell find $(VPATH)/. -type f -name '*.rs') .PHONY: all clean all: librustcoregraphics.dummy -librustcoregraphics.dummy: core_graphics.rc $(RUST_SRC) - $(RUSTC) $(RUSTFLAGS) $< --out-dir . +librustcoregraphics.dummy: lib.rs $(RUST_SRC) + $(RUSTC) $(RUSTFLAGS) $< --lib --out-dir . touch $@ -rustcoregraphics-test: core_graphics.rc $(RUST_SRC) +rustcoregraphics-test: lib.rs $(RUST_SRC) $(RUSTC) $(RUSTFLAGS) $< -o $@ --test check: rustcoregraphics-test diff --git a/core_graphics.rc b/lib.rs similarity index 82% rename from core_graphics.rc rename to lib.rs index 03e52d8..5da1f3e 100644 --- a/core_graphics.rc +++ b/lib.rs @@ -7,12 +7,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[link(name = "core_graphics", - vers = "0.1")]; -#[crate_type = "lib"]; - extern mod std; -extern mod core_foundation; +extern mod core_foundation = "rust-core-foundation"; pub mod base; pub mod data_provider; From 9bd0139ea4ff42b013bd67862b8c5c13692e1b00 Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Wed, 6 Nov 2013 17:49:38 -0800 Subject: [PATCH 029/166] Remove old build system --- Makefile.in | 23 ----------------------- configure | 5 ----- 2 files changed, 28 deletions(-) delete mode 100644 Makefile.in delete mode 100755 configure diff --git a/Makefile.in b/Makefile.in deleted file mode 100644 index bc5f0ea..0000000 --- a/Makefile.in +++ /dev/null @@ -1,23 +0,0 @@ -VPATH=%VPATH% - -RUSTC ?= rustc -RUSTFLAGS ?= - -RUST_SRC=$(shell find $(VPATH)/. -type f -name '*.rs') - -.PHONY: all clean -all: librustcoregraphics.dummy - -librustcoregraphics.dummy: lib.rs $(RUST_SRC) - $(RUSTC) $(RUSTFLAGS) $< --lib --out-dir . - touch $@ - -rustcoregraphics-test: lib.rs $(RUST_SRC) - $(RUSTC) $(RUSTFLAGS) $< -o $@ --test - -check: rustcoregraphics-test - ./rustcoregraphics-test - -clean: - rm -f *.o *.a *.so *.dylib *.dll *.dummy rustcoregraphics-test - diff --git a/configure b/configure deleted file mode 100755 index ed6c06e..0000000 --- a/configure +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -SRCDIR="$(cd $(dirname $0) && pwd)" -sed "s#%VPATH%#${SRCDIR}#" ${SRCDIR}/Makefile.in > Makefile - From 1df1403432c6d6ac89213cf50e28cc7668f8811b Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Thu, 26 Dec 2013 19:46:51 -0700 Subject: [PATCH 030/166] Build with make instead of rustpkg. --- Makefile.in | 24 ++++++++++++++++++++++++ configure | 4 ++++ lib.rs | 6 ++++-- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 Makefile.in create mode 100755 configure diff --git a/Makefile.in b/Makefile.in new file mode 100644 index 0000000..fd9af7c --- /dev/null +++ b/Makefile.in @@ -0,0 +1,24 @@ +VPATH=%VPATH% + +RUSTC ?= rustc +RUSTFLAGS ?= + +RUST_SRC=$(shell find $(VPATH)/. -type f -name '*.rs') + +.PHONY: all +all: libcore_graphics.dummy + +libcore_graphics.dummy: lib.rs $(RUST_SRC) + $(RUSTC) $(RUSTFLAGS) $< --lib --out-dir . + touch $@ + +core_graphics-test: lib.rs $(RUST_SRC) + $(RUSTC) $(RUSTFLAGS) $< -o $@ --test + +.PHONY: check +check: core_graphics-test + ./core_graphics-test $(TEST) + +.PHONY: clean +clean: + rm -f *.o *.a *.so *.dylib *.dll *.dummy *-test diff --git a/configure b/configure new file mode 100755 index 0000000..62a0f4c --- /dev/null +++ b/configure @@ -0,0 +1,4 @@ +#!/bin/bash + +SRCDIR="$(cd $(dirname $0) && pwd)" +sed "s#%VPATH%#${SRCDIR}#" ${SRCDIR}/Makefile.in > Makefile diff --git a/lib.rs b/lib.rs index 5da1f3e..97da337 100644 --- a/lib.rs +++ b/lib.rs @@ -7,10 +7,12 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#[link(name = "core_graphics", vers = "0.1")]; + extern mod std; -extern mod core_foundation = "rust-core-foundation"; +extern mod core_foundation; pub mod base; pub mod data_provider; pub mod font; -pub mod geometry; \ No newline at end of file +pub mod geometry; From afb3bde31826cf7ac42f36decee8903defae9192 Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Fri, 20 Dec 2013 21:35:42 -0700 Subject: [PATCH 031/166] Upgrade to latest Rust. --- data_provider.rs | 17 ++++++----------- font.rs | 11 +++-------- lib.rs | 2 +- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/data_provider.rs b/data_provider.rs index 9f155aa..f238024 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -10,9 +10,8 @@ use core_foundation::base::{CFRelease, CFTypeID, TCFType}; use std::cast; -use std::libc::{c_void, c_char, size_t}; +use std::libc::{c_void, size_t}; use std::ptr; -use std::vec; pub type CGDataProviderGetBytesCallback = *u8; pub type CGDataProviderReleaseInfoCallback = *u8; @@ -35,7 +34,6 @@ pub struct CGDataProvider { } impl Drop for CGDataProvider { - #[fixed_stack_segment] fn drop(&mut self) { unsafe { CFRelease(self.as_CFTypeRef()) @@ -54,7 +52,6 @@ impl TCFType for CGDataProvider { } } - #[fixed_stack_segment] #[inline] fn type_id(_: Option) -> CFTypeID { unsafe { @@ -64,11 +61,10 @@ impl TCFType for CGDataProvider { } impl CGDataProvider { - #[fixed_stack_segment] pub fn from_buffer(buffer: &[u8]) -> CGDataProvider { unsafe { let result = CGDataProviderCreateWithData(ptr::null(), - cast::transmute(vec::raw::to_ptr(buffer)), + cast::transmute(buffer.as_ptr()), buffer.len() as u64, ptr::null()); TCFType::wrap_under_create_rule(result) @@ -76,8 +72,7 @@ impl CGDataProvider { } } -#[nolink] -#[link_args="-framework ApplicationServices"] +#[link(name = "ApplicationServices", kind = "framework")] extern { //fn CGDataProviderCopyData //fn CGDataProviderCreateDirect @@ -88,9 +83,9 @@ extern { size: size_t, releaseData: CGDataProviderReleaseDataCallback ) -> CGDataProviderRef; - fn CGDataProviderCreateWithFilename(filename: *c_char) -> CGDataProviderRef; + //fn CGDataProviderCreateWithFilename(filename: *c_char) -> CGDataProviderRef; //fn CGDataProviderCreateWithURL fn CGDataProviderGetTypeID() -> CFTypeID; - fn CGDataProviderRelease(provider: CGDataProviderRef); - fn CGDataProviderRetain(provider: CGDataProviderRef) -> CGDataProviderRef; + //fn CGDataProviderRelease(provider: CGDataProviderRef); + //fn CGDataProviderRetain(provider: CGDataProviderRef) -> CGDataProviderRef; } diff --git a/font.rs b/font.rs index 55b682a..ab4b2e7 100644 --- a/font.rs +++ b/font.rs @@ -23,7 +23,6 @@ pub struct CGFont { } impl Clone for CGFont { - #[fixed_stack_segment] #[inline] fn clone(&self) -> CGFont { unsafe { @@ -33,7 +32,6 @@ impl Clone for CGFont { } impl Drop for CGFont { - #[fixed_stack_segment] fn drop(&mut self) { unsafe { CFRelease(self.as_CFTypeRef()) @@ -52,7 +50,6 @@ impl TCFType for CGFont { } } - #[fixed_stack_segment] #[inline] fn type_id(_: Option) -> CFTypeID { unsafe { @@ -62,7 +59,6 @@ impl TCFType for CGFont { } impl CGFont { - #[fixed_stack_segment] pub fn from_data_provider(provider: CGDataProvider) -> CGFont { unsafe { let font_ref = CGFontCreateWithDataProvider(provider.as_concrete_TypeRef()); @@ -71,8 +67,7 @@ impl CGFont { } } -#[nolink] -#[link_args="-framework ApplicationServices"] +#[link(name = "ApplicationServices", kind = "framework")] extern { // TODO: basically nothing has bindings (even commented-out) besides what we use. @@ -81,6 +76,6 @@ extern { // These do the same thing as CFRetain/CFRelease, except // gracefully handle a NULL argument. We don't use them. - fn CGFontRetain(font: CGFontRef); - fn CGFontRelease(font: CGFontRef); + //fn CGFontRetain(font: CGFontRef); + //fn CGFontRelease(font: CGFontRef); } diff --git a/lib.rs b/lib.rs index 97da337..eb7a079 100644 --- a/lib.rs +++ b/lib.rs @@ -7,7 +7,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[link(name = "core_graphics", vers = "0.1")]; +#[crate_id = "github.com/mozilla-servo/rust-core-graphics#core_graphics:0.1"]; extern mod std; extern mod core_foundation; From 64667651ef9f37f5ec8cda48992fca9720510dae Mon Sep 17 00:00:00 2001 From: Lars Bergstrom Date: Tue, 4 Mar 2014 08:40:40 -0800 Subject: [PATCH 032/166] Rust upgrade --- Makefile.in | 2 +- lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile.in b/Makefile.in index fd9af7c..88948ce 100644 --- a/Makefile.in +++ b/Makefile.in @@ -9,7 +9,7 @@ RUST_SRC=$(shell find $(VPATH)/. -type f -name '*.rs') all: libcore_graphics.dummy libcore_graphics.dummy: lib.rs $(RUST_SRC) - $(RUSTC) $(RUSTFLAGS) $< --lib --out-dir . + $(RUSTC) $(RUSTFLAGS) $< --crate-type=lib --out-dir . touch $@ core_graphics-test: lib.rs $(RUST_SRC) diff --git a/lib.rs b/lib.rs index eb7a079..8db9e27 100644 --- a/lib.rs +++ b/lib.rs @@ -9,8 +9,8 @@ #[crate_id = "github.com/mozilla-servo/rust-core-graphics#core_graphics:0.1"]; -extern mod std; -extern mod core_foundation; +extern crate std; +extern crate core_foundation; pub mod base; pub mod data_provider; From 5940560e5b62fe63e1e8b1007bb4012d89586635 Mon Sep 17 00:00:00 2001 From: Lars Bergstrom Date: Wed, 19 Mar 2014 13:50:57 -0500 Subject: [PATCH 033/166] Clean rlib files --- Makefile.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile.in b/Makefile.in index 88948ce..1b47858 100644 --- a/Makefile.in +++ b/Makefile.in @@ -21,4 +21,4 @@ check: core_graphics-test .PHONY: clean clean: - rm -f *.o *.a *.so *.dylib *.dll *.dummy *-test + rm -f *.o *.a *.so *.dylib *.rlib *.dll *.dummy *-test From d1c53f142c7885c1cf1a26d4de94fd823d69203c Mon Sep 17 00:00:00 2001 From: Lars Bergstrom Date: Fri, 4 Apr 2014 09:53:12 -0500 Subject: [PATCH 034/166] Generate both dylib and rlib for rustup --- lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib.rs b/lib.rs index 8db9e27..44d702c 100644 --- a/lib.rs +++ b/lib.rs @@ -8,6 +8,9 @@ // except according to those terms. #[crate_id = "github.com/mozilla-servo/rust-core-graphics#core_graphics:0.1"]; +#[crate_type = "lib"]; +#[crate_type = "dylib"]; +#[crate_type = "rlib"]; extern crate std; extern crate core_foundation; From 7de2c6cdd2f63437e784424d07fa625ec9d78568 Mon Sep 17 00:00:00 2001 From: Lars Bergstrom Date: Fri, 11 Apr 2014 17:35:25 -0500 Subject: [PATCH 035/166] Upgrade to rust --- base.rs | 2 +- data_provider.rs | 2 +- font.rs | 2 +- lib.rs | 9 +++++---- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/base.rs b/base.rs index 5e1a6c7..b9d5061 100644 --- a/base.rs +++ b/base.rs @@ -9,7 +9,7 @@ // this file defines CGFloat, as well as stubbed data types. -use std::libc; +use libc; // TODO: this is actually a libc::c_float on 32bit pub type CGFloat = libc::c_double; diff --git a/data_provider.rs b/data_provider.rs index f238024..ef932cd 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -9,8 +9,8 @@ use core_foundation::base::{CFRelease, CFTypeID, TCFType}; +use libc::{c_void, size_t}; use std::cast; -use std::libc::{c_void, size_t}; use std::ptr; pub type CGDataProviderGetBytesCallback = *u8; diff --git a/font.rs b/font.rs index ab4b2e7..467cd82 100644 --- a/font.rs +++ b/font.rs @@ -10,7 +10,7 @@ use core_foundation::base::{CFRelease, CFTypeID, TCFType}; use data_provider::{CGDataProvider, CGDataProviderRef}; -use std::libc; +use libc; pub type CGGlyph = libc::c_ushort; diff --git a/lib.rs b/lib.rs index 44d702c..ab3e512 100644 --- a/lib.rs +++ b/lib.rs @@ -7,11 +7,12 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[crate_id = "github.com/mozilla-servo/rust-core-graphics#core_graphics:0.1"]; -#[crate_type = "lib"]; -#[crate_type = "dylib"]; -#[crate_type = "rlib"]; +#![crate_id = "github.com/mozilla-servo/rust-core-graphics#core_graphics:0.1"] +#![crate_type = "lib"] +#![crate_type = "dylib"] +#![crate_type = "rlib"] +extern crate libc; extern crate std; extern crate core_foundation; From 32c347b35fe8cfd3a98aba9ab1e9681bc58fe8bb Mon Sep 17 00:00:00 2001 From: Lars Bergstrom Date: Fri, 25 Apr 2014 16:30:56 -0500 Subject: [PATCH 036/166] Rust upgrade - fix privacy --- geometry.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/geometry.rs b/geometry.rs index 5e9ba8b..40f50a9 100644 --- a/geometry.rs +++ b/geometry.rs @@ -10,16 +10,16 @@ use base::CGFloat; pub struct CGSize { - width: CGFloat, - height: CGFloat, + pub width: CGFloat, + pub height: CGFloat, } pub struct CGPoint { - x: CGFloat, - y: CGFloat, + pub x: CGFloat, + pub y: CGFloat, } pub struct CGRect { - origin: CGPoint, - size: CGSize + pub origin: CGPoint, + pub size: CGSize } From 2d9116a992f62d072c2831dcf7c9bae207861060 Mon Sep 17 00:00:00 2001 From: Lars Bergstrom Date: Tue, 13 May 2014 19:49:36 -0500 Subject: [PATCH 037/166] Rust upgrade --- Makefile.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile.in b/Makefile.in index 1b47858..a9a17b3 100644 --- a/Makefile.in +++ b/Makefile.in @@ -9,7 +9,7 @@ RUST_SRC=$(shell find $(VPATH)/. -type f -name '*.rs') all: libcore_graphics.dummy libcore_graphics.dummy: lib.rs $(RUST_SRC) - $(RUSTC) $(RUSTFLAGS) $< --crate-type=lib --out-dir . + $(RUSTC) $(RUSTFLAGS) $< --out-dir . touch $@ core_graphics-test: lib.rs $(RUST_SRC) From 48e3223bb0c0ea4020d498f9c39d47ea0701abd6 Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Wed, 28 May 2014 17:32:29 -0600 Subject: [PATCH 038/166] Pass in EXT_DEPS for external dependency tracking. --- Makefile.in | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile.in b/Makefile.in index a9a17b3..d4d1dc1 100644 --- a/Makefile.in +++ b/Makefile.in @@ -2,17 +2,18 @@ VPATH=%VPATH% RUSTC ?= rustc RUSTFLAGS ?= +EXT_DEPS ?= RUST_SRC=$(shell find $(VPATH)/. -type f -name '*.rs') .PHONY: all all: libcore_graphics.dummy -libcore_graphics.dummy: lib.rs $(RUST_SRC) +libcore_graphics.dummy: lib.rs $(RUST_SRC) $(EXT_DEPS) $(RUSTC) $(RUSTFLAGS) $< --out-dir . touch $@ -core_graphics-test: lib.rs $(RUST_SRC) +core_graphics-test: lib.rs $(RUST_SRC) $(EXT_DEPS) $(RUSTC) $(RUSTFLAGS) $< -o $@ --test .PHONY: check From 8bb25c2a193f3b47bb5406c78c2d04396c345f22 Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Sat, 31 May 2014 23:33:25 -0600 Subject: [PATCH 039/166] Upgrade Rust. --- data_provider.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_provider.rs b/data_provider.rs index ef932cd..1651100 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -10,7 +10,7 @@ use core_foundation::base::{CFRelease, CFTypeID, TCFType}; use libc::{c_void, size_t}; -use std::cast; +use std::mem; use std::ptr; pub type CGDataProviderGetBytesCallback = *u8; @@ -64,7 +64,7 @@ impl CGDataProvider { pub fn from_buffer(buffer: &[u8]) -> CGDataProvider { unsafe { let result = CGDataProviderCreateWithData(ptr::null(), - cast::transmute(buffer.as_ptr()), + mem::transmute(buffer.as_ptr()), buffer.len() as u64, ptr::null()); TCFType::wrap_under_create_rule(result) From 889d86eaac1aea69329dfc1f1c07770de612e67e Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Fri, 27 Jun 2014 12:29:56 -0600 Subject: [PATCH 040/166] Upgrade Rust. --- data_provider.rs | 17 ++++++++++++++++- font.rs | 20 ++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/data_provider.rs b/data_provider.rs index 1651100..b250430 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -7,7 +7,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use core_foundation::base::{CFRelease, CFTypeID, TCFType}; +use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; use libc::{c_void, size_t}; use std::mem; @@ -42,10 +42,25 @@ impl Drop for CGDataProvider { } impl TCFType for CGDataProvider { + #[inline] fn as_concrete_TypeRef(&self) -> CGDataProviderRef { self.obj } + #[inline] + unsafe fn wrap_under_get_rule(reference: CGDataProviderRef) -> CGDataProvider { + let reference: CGDataProviderRef = mem::transmute(CFRetain(mem::transmute(reference))); + TCFType::wrap_under_create_rule(reference) + } + + #[inline] + fn as_CFTypeRef(&self) -> CFTypeRef { + unsafe { + mem::transmute(self.as_concrete_TypeRef()) + } + } + + #[inline] unsafe fn wrap_under_create_rule(obj: CGDataProviderRef) -> CGDataProvider { CGDataProvider { obj: obj, diff --git a/font.rs b/font.rs index 467cd82..f4cab76 100644 --- a/font.rs +++ b/font.rs @@ -7,10 +7,11 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use core_foundation::base::{CFRelease, CFTypeID, TCFType}; - +use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; use data_provider::{CGDataProvider, CGDataProviderRef}; + use libc; +use std::mem; pub type CGGlyph = libc::c_ushort; @@ -40,10 +41,25 @@ impl Drop for CGFont { } impl TCFType for CGFont { + #[inline] fn as_concrete_TypeRef(&self) -> CGFontRef { self.obj } + #[inline] + unsafe fn wrap_under_get_rule(reference: CGFontRef) -> CGFont { + let reference: CGFontRef = mem::transmute(CFRetain(mem::transmute(reference))); + TCFType::wrap_under_create_rule(reference) + } + + #[inline] + fn as_CFTypeRef(&self) -> CFTypeRef { + unsafe { + mem::transmute(self.as_concrete_TypeRef()) + } + } + + #[inline] unsafe fn wrap_under_create_rule(obj: CGFontRef) -> CGFont { CGFont { obj: obj, From 9a71cf14031889e33fff8d116e94b1309320d4e9 Mon Sep 17 00:00:00 2001 From: Glenn Watson Date: Mon, 14 Jul 2014 16:01:41 +1000 Subject: [PATCH 041/166] from_data_provider now returns result indicating success. --- font.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/font.rs b/font.rs index f4cab76..03a20d8 100644 --- a/font.rs +++ b/font.rs @@ -12,6 +12,7 @@ use data_provider::{CGDataProvider, CGDataProviderRef}; use libc; use std::mem; +use std::ptr; pub type CGGlyph = libc::c_ushort; @@ -35,7 +36,9 @@ impl Clone for CGFont { impl Drop for CGFont { fn drop(&mut self) { unsafe { - CFRelease(self.as_CFTypeRef()) + let ptr = self.as_CFTypeRef(); + assert!(ptr != ptr::null()); + CFRelease(ptr); } } } @@ -75,10 +78,14 @@ impl TCFType for CGFont { } impl CGFont { - pub fn from_data_provider(provider: CGDataProvider) -> CGFont { + pub fn from_data_provider(provider: CGDataProvider) -> Result { unsafe { let font_ref = CGFontCreateWithDataProvider(provider.as_concrete_TypeRef()); - TCFType::wrap_under_create_rule(font_ref) + if font_ref != ptr::null() { + Ok(TCFType::wrap_under_create_rule(font_ref)) + } else { + Err(()) + } } } } From 7f7b1de962672c7a7c3a15fb0099104811d220ed Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Thu, 24 Jul 2014 21:11:45 -0600 Subject: [PATCH 042/166] Upgrade Rust. --- data_provider.rs | 30 +++++++++++++++--------------- font.rs | 2 +- lib.rs | 4 +--- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/data_provider.rs b/data_provider.rs index b250430..629c2f6 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -13,21 +13,21 @@ use libc::{c_void, size_t}; use std::mem; use std::ptr; -pub type CGDataProviderGetBytesCallback = *u8; -pub type CGDataProviderReleaseInfoCallback = *u8; -pub type CGDataProviderRewindCallback = *u8; -pub type CGDataProviderSkipBytesCallback = *u8; -pub type CGDataProviderSkipForwardCallback = *u8; +pub type CGDataProviderGetBytesCallback = *const u8; +pub type CGDataProviderReleaseInfoCallback = *const u8; +pub type CGDataProviderRewindCallback = *const u8; +pub type CGDataProviderSkipBytesCallback = *const u8; +pub type CGDataProviderSkipForwardCallback = *const u8; -pub type CGDataProviderGetBytePointerCallback = *u8; -pub type CGDataProviderGetBytesAtOffsetCallback = *u8; -pub type CGDataProviderReleaseBytePointerCallback = *u8; -pub type CGDataProviderReleaseDataCallback = *u8; -pub type CGDataProviderGetBytesAtPositionCallback = *u8; +pub type CGDataProviderGetBytePointerCallback = *const u8; +pub type CGDataProviderGetBytesAtOffsetCallback = *const u8; +pub type CGDataProviderReleaseBytePointerCallback = *const u8; +pub type CGDataProviderReleaseDataCallback = *const u8; +pub type CGDataProviderGetBytesAtPositionCallback = *const u8; struct __CGDataProvider; -pub type CGDataProviderRef = *__CGDataProvider; +pub type CGDataProviderRef = *const __CGDataProvider; pub struct CGDataProvider { obj: CGDataProviderRef, @@ -78,8 +78,8 @@ impl TCFType for CGDataProvider { impl CGDataProvider { pub fn from_buffer(buffer: &[u8]) -> CGDataProvider { unsafe { - let result = CGDataProviderCreateWithData(ptr::null(), - mem::transmute(buffer.as_ptr()), + let result = CGDataProviderCreateWithData(ptr::mut_null(), + buffer.as_ptr() as *const c_void, buffer.len() as u64, ptr::null()); TCFType::wrap_under_create_rule(result) @@ -93,8 +93,8 @@ extern { //fn CGDataProviderCreateDirect //fn CGDataProviderCreateSequential //fn CGDataProviderCreateWithCFData - fn CGDataProviderCreateWithData(info: *c_void, - data: *c_void, + fn CGDataProviderCreateWithData(info: *mut c_void, + data: *const c_void, size: size_t, releaseData: CGDataProviderReleaseDataCallback ) -> CGDataProviderRef; diff --git a/font.rs b/font.rs index 03a20d8..2523963 100644 --- a/font.rs +++ b/font.rs @@ -18,7 +18,7 @@ pub type CGGlyph = libc::c_ushort; struct __CGFont; -pub type CGFontRef = *__CGFont; +pub type CGFontRef = *const __CGFont; pub struct CGFont { obj: CGFontRef, diff --git a/lib.rs b/lib.rs index ab3e512..2f23ba5 100644 --- a/lib.rs +++ b/lib.rs @@ -7,9 +7,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![crate_id = "github.com/mozilla-servo/rust-core-graphics#core_graphics:0.1"] -#![crate_type = "lib"] -#![crate_type = "dylib"] +#![crate_name = "core_graphics"] #![crate_type = "rlib"] extern crate libc; From 36817ab7a6457702ffbe0443e75820def88264be Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Mon, 11 Aug 2014 18:37:54 -0600 Subject: [PATCH 043/166] Fixes for building 32bit. --- data_provider.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_provider.rs b/data_provider.rs index 629c2f6..902d13e 100644 --- a/data_provider.rs +++ b/data_provider.rs @@ -80,7 +80,7 @@ impl CGDataProvider { unsafe { let result = CGDataProviderCreateWithData(ptr::mut_null(), buffer.as_ptr() as *const c_void, - buffer.len() as u64, + buffer.len() as size_t, ptr::null()); TCFType::wrap_under_create_rule(result) } From b02256d3a663d193198dee381b948195872ccaed Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Mon, 25 Aug 2014 22:02:58 -0600 Subject: [PATCH 044/166] Cargoify rust-core-graphics --- .gitignore | 3 +++ Cargo.toml | 10 ++++++++++ Makefile.in | 6 +++--- base.rs => src/base.rs | 0 data_provider.rs => src/data_provider.rs | 0 font.rs => src/font.rs | 0 geometry.rs => src/geometry.rs | 0 lib.rs => src/lib.rs | 4 ++++ 8 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 Cargo.toml rename base.rs => src/base.rs (100%) rename data_provider.rs => src/data_provider.rs (100%) rename font.rs => src/font.rs (100%) rename geometry.rs => src/geometry.rs (100%) rename lib.rs => src/lib.rs (85%) diff --git a/.gitignore b/.gitignore index 5f68b7f..c34fbd0 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ *.dummy cocoa-test build +/target +/doc +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..217bb81 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] + +name = "core_graphics" +version = "0.1.0" +authors = ["The Servo Project Developers"] + +[dependencies.core_foundation] + +git = "https://github.com/servo/rust-core-foundation" + diff --git a/Makefile.in b/Makefile.in index d4d1dc1..5ef24cd 100644 --- a/Makefile.in +++ b/Makefile.in @@ -4,16 +4,16 @@ RUSTC ?= rustc RUSTFLAGS ?= EXT_DEPS ?= -RUST_SRC=$(shell find $(VPATH)/. -type f -name '*.rs') +RUST_SRC=$(shell find $(VPATH)/src -type f -name '*.rs') .PHONY: all all: libcore_graphics.dummy -libcore_graphics.dummy: lib.rs $(RUST_SRC) $(EXT_DEPS) +libcore_graphics.dummy: src/lib.rs $(RUST_SRC) $(EXT_DEPS) $(RUSTC) $(RUSTFLAGS) $< --out-dir . touch $@ -core_graphics-test: lib.rs $(RUST_SRC) $(EXT_DEPS) +core_graphics-test: src/lib.rs $(RUST_SRC) $(EXT_DEPS) $(RUSTC) $(RUSTFLAGS) $< -o $@ --test .PHONY: check diff --git a/base.rs b/src/base.rs similarity index 100% rename from base.rs rename to src/base.rs diff --git a/data_provider.rs b/src/data_provider.rs similarity index 100% rename from data_provider.rs rename to src/data_provider.rs diff --git a/font.rs b/src/font.rs similarity index 100% rename from font.rs rename to src/font.rs diff --git a/geometry.rs b/src/geometry.rs similarity index 100% rename from geometry.rs rename to src/geometry.rs diff --git a/lib.rs b/src/lib.rs similarity index 85% rename from lib.rs rename to src/lib.rs index 2f23ba5..a3311c8 100644 --- a/lib.rs +++ b/src/lib.rs @@ -14,7 +14,11 @@ extern crate libc; extern crate std; extern crate core_foundation; +#[cfg(target_os="macos")] pub mod base; +#[cfg(target_os="macos")] pub mod data_provider; +#[cfg(target_os="macos")] pub mod font; +#[cfg(target_os="macos")] pub mod geometry; From 9faeb8fe6dfb82f844eb355b67790f233a9db144 Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Tue, 16 Sep 2014 11:38:44 -0700 Subject: [PATCH 045/166] Rust upgrade --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index a3311c8..30383a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,6 @@ #![crate_type = "rlib"] extern crate libc; -extern crate std; extern crate core_foundation; #[cfg(target_os="macos")] From 066201f910da5811739d821ef9fe75c4e085a6ae Mon Sep 17 00:00:00 2001 From: David Partouche Date: Tue, 4 Nov 2014 17:44:07 +0100 Subject: [PATCH 046/166] Added some Quartz Display Services functions (used in glutin) --- src/base.rs | 1 + src/display.rs | 26 ++++++++++++++++++++++++++ src/lib.rs | 2 ++ 3 files changed, 29 insertions(+) create mode 100644 src/display.rs diff --git a/src/base.rs b/src/base.rs index b9d5061..d100603 100644 --- a/src/base.rs +++ b/src/base.rs @@ -13,6 +13,7 @@ use libc; // TODO: this is actually a libc::c_float on 32bit pub type CGFloat = libc::c_double; +pub type CGError = libc::int32_t; pub type CGAffineTransform = (); diff --git a/src/display.rs b/src/display.rs new file mode 100644 index 0000000..a985bdb --- /dev/null +++ b/src/display.rs @@ -0,0 +1,26 @@ +// Copyright 2013 The Servo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// this file defines CGFloat, as well as stubbed data types. + +use libc; +use base::CGError; + +pub type CGDirectDisplayID = libc::uint32_t; + +#[link(name = "ApplicationServices", kind = "framework")] +extern { + pub fn CGMainDisplayID() -> CGDirectDisplayID; + pub fn CGGetActiveDisplayList(max_displays: libc::uint32_t, + active_displays: *mut CGDirectDisplayID, + display_count: *mut libc::uint32_t) -> CGError; + pub fn CGDisplayModelNumber(display: CGDirectDisplayID) -> libc::uint32_t; + pub fn CGDisplayPixelsHigh(display: CGDirectDisplayID) -> libc::size_t; + pub fn CGDisplayPixelsWide(display: CGDirectDisplayID) -> libc::size_t; +} diff --git a/src/lib.rs b/src/lib.rs index 30383a1..b13b204 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,3 +21,5 @@ pub mod data_provider; pub mod font; #[cfg(target_os="macos")] pub mod geometry; +#[cfg(target_os="macos")] +pub mod quartz_display_services; From 20de0e87e4145b2560650d35206d4a86a5aa07bf Mon Sep 17 00:00:00 2001 From: David Partouche Date: Tue, 4 Nov 2014 17:45:33 +0100 Subject: [PATCH 047/166] Forgot to rename display mod --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index b13b204..e632ccc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,4 +22,4 @@ pub mod font; #[cfg(target_os="macos")] pub mod geometry; #[cfg(target_os="macos")] -pub mod quartz_display_services; +pub mod display; From f5e9e0bcb0fc26a74ee6fb9c36cc02c78ae9a37a Mon Sep 17 00:00:00 2001 From: David Partouche Date: Tue, 4 Nov 2014 17:49:17 +0100 Subject: [PATCH 048/166] Changed mut_null to null_mut --- src/data_provider.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/data_provider.rs b/src/data_provider.rs index 902d13e..8109df4 100644 --- a/src/data_provider.rs +++ b/src/data_provider.rs @@ -78,7 +78,7 @@ impl TCFType for CGDataProvider { impl CGDataProvider { pub fn from_buffer(buffer: &[u8]) -> CGDataProvider { unsafe { - let result = CGDataProviderCreateWithData(ptr::mut_null(), + let result = CGDataProviderCreateWithData(ptr::null_mut(), buffer.as_ptr() as *const c_void, buffer.len() as size_t, ptr::null()); From 909b508fc3fb8688f1a6fa2d299063d69faa28a1 Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Wed, 5 Nov 2014 13:44:48 -0700 Subject: [PATCH 049/166] Upgrade Rust. --- src/data_provider.rs | 1 + src/font.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/data_provider.rs b/src/data_provider.rs index 8109df4..31d15d4 100644 --- a/src/data_provider.rs +++ b/src/data_provider.rs @@ -25,6 +25,7 @@ pub type CGDataProviderReleaseBytePointerCallback = *const u8; pub type CGDataProviderReleaseDataCallback = *const u8; pub type CGDataProviderGetBytesAtPositionCallback = *const u8; +#[repr(C)] struct __CGDataProvider; pub type CGDataProviderRef = *const __CGDataProvider; diff --git a/src/font.rs b/src/font.rs index 2523963..262f3ae 100644 --- a/src/font.rs +++ b/src/font.rs @@ -16,6 +16,7 @@ use std::ptr; pub type CGGlyph = libc::c_ushort; +#[repr(C)] struct __CGFont; pub type CGFontRef = *const __CGFont; From 5dec07e798868370e484f410242d044444956843 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Gawdzik=20=E2=98=A2?= Date: Sat, 21 Feb 2015 03:35:15 -0800 Subject: [PATCH 050/166] Update method signatures from an upstream core_foundation change --- src/data_provider.rs | 2 +- src/font.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/data_provider.rs b/src/data_provider.rs index 31d15d4..bb2fdba 100644 --- a/src/data_provider.rs +++ b/src/data_provider.rs @@ -69,7 +69,7 @@ impl TCFType for CGDataProvider { } #[inline] - fn type_id(_: Option) -> CFTypeID { + fn type_id() -> CFTypeID { unsafe { CGDataProviderGetTypeID() } diff --git a/src/font.rs b/src/font.rs index 262f3ae..ca2449e 100644 --- a/src/font.rs +++ b/src/font.rs @@ -71,7 +71,7 @@ impl TCFType for CGFont { } #[inline] - fn type_id(_: Option) -> CFTypeID { + fn type_id() -> CFTypeID { unsafe { CGFontGetTypeID() } From 4a06037233b1c4100a7bc579b56e29a6ef7fd26e Mon Sep 17 00:00:00 2001 From: Steven Sheldon Date: Thu, 26 Mar 2015 21:20:45 -0700 Subject: [PATCH 051/166] Use libc from crates.io. --- Cargo.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 217bb81..ae2aa5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,9 @@ name = "core_graphics" version = "0.1.0" authors = ["The Servo Project Developers"] +[dependencies] +libc = "*" + [dependencies.core_foundation] git = "https://github.com/servo/rust-core-foundation" From ef6fc9865b64d712dddad3fa94eff0200722742a Mon Sep 17 00:00:00 2001 From: Felix Kaaman Date: Mon, 4 May 2015 09:24:34 +0200 Subject: [PATCH 052/166] Add some more functions and make geometry structs #[repr(C)] --- src/base.rs | 5 +++++ src/display.rs | 29 ++++++++++++++++++++++++++++- src/geometry.rs | 3 +++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/base.rs b/src/base.rs index d100603..94d8249 100644 --- a/src/base.rs +++ b/src/base.rs @@ -11,6 +11,11 @@ use libc; +#[cfg(target_pointer_width = "32")] +pub type boolean_t = i32; +#[cfg(target_pointer_width = "64")] +pub type boolean_t = u32; + // TODO: this is actually a libc::c_float on 32bit pub type CGFloat = libc::c_double; pub type CGError = libc::int32_t; diff --git a/src/display.rs b/src/display.rs index a985bdb..02fa138 100644 --- a/src/display.rs +++ b/src/display.rs @@ -10,17 +10,44 @@ // this file defines CGFloat, as well as stubbed data types. use libc; -use base::CGError; +use base::{CGError, boolean_t}; +use geometry::{CGRect, CGPoint, CGSize}; pub type CGDirectDisplayID = libc::uint32_t; #[link(name = "ApplicationServices", kind = "framework")] extern { pub fn CGMainDisplayID() -> CGDirectDisplayID; + pub fn CGDisplayIsActive(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayIsAlwaysInMirrorSet(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayIsAsleep(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayIsBuiltin(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayIsInHWMirrorSet(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayIsInMirrorSet(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayIsMain(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayIsOnline(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayIsStereo(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayMirrorsDisplay(display: CGDirectDisplayID) -> CGDirectDisplayID; + pub fn CGDisplayPrimaryDisplay(display: CGDirectDisplayID) -> CGDirectDisplayID; + pub fn CGDisplayRotation(display: CGDirectDisplayID) -> libc::c_double; + pub fn CGDisplayScreenSize(display: CGDirectDisplayID) -> CGSize; + pub fn CGDisplaySerialNumber(display: CGDirectDisplayID) -> libc::uint32_t; + pub fn CGDisplayUnitNumber(display: CGDirectDisplayID) -> libc::uint32_t; + pub fn CGDisplayUsesOpenGLAcceleration(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayVendorNumber(display: CGDirectDisplayID) -> libc::uint32_t; pub fn CGGetActiveDisplayList(max_displays: libc::uint32_t, active_displays: *mut CGDirectDisplayID, display_count: *mut libc::uint32_t) -> CGError; pub fn CGDisplayModelNumber(display: CGDirectDisplayID) -> libc::uint32_t; pub fn CGDisplayPixelsHigh(display: CGDirectDisplayID) -> libc::size_t; pub fn CGDisplayPixelsWide(display: CGDirectDisplayID) -> libc::size_t; + pub fn CGDisplayBounds(display: CGDirectDisplayID) -> CGRect; + + // mouse stuff + pub fn CGDisplayHideCursor(display: CGDirectDisplayID) -> CGError; + pub fn CGDisplayShowCursor(display: CGDirectDisplayID) -> CGError; + pub fn CGDisplayMoveCursorToPoint(display: CGDirectDisplayID, point: CGPoint) -> CGError; + pub fn CGWarpMouseCursorPosition(point: CGPoint) -> CGError; + pub fn CGAssociateMouseAndMouseCursorPosition(connected: bool) -> CGError; + } diff --git a/src/geometry.rs b/src/geometry.rs index 40f50a9..fbe1280 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -9,16 +9,19 @@ use base::CGFloat; +#[repr(C)] pub struct CGSize { pub width: CGFloat, pub height: CGFloat, } +#[repr(C)] pub struct CGPoint { pub x: CGFloat, pub y: CGFloat, } +#[repr(C)] pub struct CGRect { pub origin: CGPoint, pub size: CGSize From 2cd1531e2bf4f0d7f11546bfdbc6c0933dc67571 Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Tue, 2 Jun 2015 23:30:59 -0600 Subject: [PATCH 053/166] Use platform dependencies and clean up old cruft. --- Cargo.toml | 3 +-- Makefile.in | 25 ------------------------- configure | 4 ---- 3 files changed, 1 insertion(+), 31 deletions(-) delete mode 100644 Makefile.in delete mode 100755 configure diff --git a/Cargo.toml b/Cargo.toml index ae2aa5e..e90e25b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,6 @@ authors = ["The Servo Project Developers"] [dependencies] libc = "*" -[dependencies.core_foundation] - +[target.x86_64-apple-darwin.dependencies.core_foundation] git = "https://github.com/servo/rust-core-foundation" diff --git a/Makefile.in b/Makefile.in deleted file mode 100644 index 5ef24cd..0000000 --- a/Makefile.in +++ /dev/null @@ -1,25 +0,0 @@ -VPATH=%VPATH% - -RUSTC ?= rustc -RUSTFLAGS ?= -EXT_DEPS ?= - -RUST_SRC=$(shell find $(VPATH)/src -type f -name '*.rs') - -.PHONY: all -all: libcore_graphics.dummy - -libcore_graphics.dummy: src/lib.rs $(RUST_SRC) $(EXT_DEPS) - $(RUSTC) $(RUSTFLAGS) $< --out-dir . - touch $@ - -core_graphics-test: src/lib.rs $(RUST_SRC) $(EXT_DEPS) - $(RUSTC) $(RUSTFLAGS) $< -o $@ --test - -.PHONY: check -check: core_graphics-test - ./core_graphics-test $(TEST) - -.PHONY: clean -clean: - rm -f *.o *.a *.so *.dylib *.rlib *.dll *.dummy *-test diff --git a/configure b/configure deleted file mode 100755 index 62a0f4c..0000000 --- a/configure +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -SRCDIR="$(cd $(dirname $0) && pwd)" -sed "s#%VPATH%#${SRCDIR}#" ${SRCDIR}/Makefile.in > Makefile From 7c41f6db7f60b5138b3f9e4b775bb064f01edd3f Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Tue, 2 Jun 2015 23:42:59 -0600 Subject: [PATCH 054/166] Remove cfgs. --- src/lib.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e632ccc..96e6ffb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,13 +13,8 @@ extern crate libc; extern crate core_foundation; -#[cfg(target_os="macos")] pub mod base; -#[cfg(target_os="macos")] pub mod data_provider; -#[cfg(target_os="macos")] pub mod font; -#[cfg(target_os="macos")] pub mod geometry; -#[cfg(target_os="macos")] pub mod display; From 6937fdfe5012adf60d01400d90c33b26bb43ff1c Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Wed, 3 Jun 2015 16:10:02 -0600 Subject: [PATCH 055/166] Add metadata for crates.io. --- Cargo.toml | 12 +++++++----- src/base.rs | 2 ++ src/lib.rs | 3 --- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e90e25b..0a68ea9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,14 @@ [package] - -name = "core_graphics" +name = "core-graphics" +description = "Bindings to Core Graphics for OS X" +homepage = "https://github.com/servo/core-graphics-rs" +repository = "https://github.com/servo/core-graphics-rs" version = "0.1.0" authors = ["The Servo Project Developers"] +license = "MIT / Apache-2.0" [dependencies] libc = "*" -[target.x86_64-apple-darwin.dependencies.core_foundation] -git = "https://github.com/servo/rust-core-foundation" - +[target.x86_64-apple-darwin.dependencies] +core-foundation = "*" diff --git a/src/base.rs b/src/base.rs index 94d8249..65d3999 100644 --- a/src/base.rs +++ b/src/base.rs @@ -9,6 +9,8 @@ // this file defines CGFloat, as well as stubbed data types. +#![allow(non_camel_case_types)] + use libc; #[cfg(target_pointer_width = "32")] diff --git a/src/lib.rs b/src/lib.rs index 96e6ffb..0ef6aaf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,9 +7,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![crate_name = "core_graphics"] -#![crate_type = "rlib"] - extern crate libc; extern crate core_foundation; From 79c34f66764912a710c93e04570b1b075d16fc8e Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 9 Jun 2015 06:44:57 -0700 Subject: [PATCH 056/166] Build and test on Travis CI (OSX) --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..9aabcaf --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: rust +os: osx +rust: nightly From e7b7fdb8d57e06811467f5bfc42bafa1eed0e074 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 9 Jun 2015 06:48:03 -0700 Subject: [PATCH 057/166] Init README --- README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..a23522d --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# core-graphics-rs + +[![Build Status](https://travis-ci.org/servo/core-graphics-rs.svg?branch=master)](https://travis-ci.org/servo/core-graphics-rs) From 8a662b25de7ae10bb8adfc3099663616fd950d5e Mon Sep 17 00:00:00 2001 From: Steven Sheldon Date: Sun, 13 Sep 2015 11:25:09 -0500 Subject: [PATCH 058/166] core-foundation is always a dependency, not just on OSX. --- Cargo.toml | 2 -- 1 file changed, 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0a68ea9..24f680f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,4 @@ license = "MIT / Apache-2.0" [dependencies] libc = "*" - -[target.x86_64-apple-darwin.dependencies] core-foundation = "*" From ac0c7df3e2cf1cdd4e45c4715f37986b0c5c975c Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Thu, 17 Sep 2015 19:32:43 +0200 Subject: [PATCH 059/166] Enable homu. --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 9aabcaf..f8b2734 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,6 @@ language: rust os: osx rust: nightly + +notifications: + webhooks: http://build.servo.org:54856/travis From 2f8a8e254afb17058aaae3e8eb3220d29572d2a7 Mon Sep 17 00:00:00 2001 From: Steven Sheldon Date: Tue, 22 Sep 2015 23:10:13 -0700 Subject: [PATCH 060/166] Bump version to 0.1.1. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 24f680f..1f4f40e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.1.0" +version = "0.1.1" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" From a534d23e81ce593cd567cb1cb0edbe08d6168440 Mon Sep 17 00:00:00 2001 From: Steven Sheldon Date: Sat, 10 Oct 2015 12:04:25 -0700 Subject: [PATCH 061/166] Fix CGFloat type definition for 32-bit. --- src/base.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/base.rs b/src/base.rs index 65d3999..9e66e4f 100644 --- a/src/base.rs +++ b/src/base.rs @@ -13,13 +13,18 @@ use libc; -#[cfg(target_pointer_width = "32")] -pub type boolean_t = i32; -#[cfg(target_pointer_width = "64")] -pub type boolean_t = u32; +#[cfg(any(target_arch = "x86", + target_arch = "arm", + target_arch = "aarch64"))] +pub type boolean_t = libc::c_int; +#[cfg(target_arch = "x86_64")] +pub type boolean_t = libc::c_uint; -// TODO: this is actually a libc::c_float on 32bit +#[cfg(target_pointer_width = "64")] pub type CGFloat = libc::c_double; +#[cfg(not(target_pointer_width = "64"))] +pub type CGFloat = libc::c_float; + pub type CGError = libc::int32_t; pub type CGAffineTransform = (); From 87cc3ba5512690b5896884722ea45fc83f13fb81 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Tue, 13 Oct 2015 14:29:39 -0700 Subject: [PATCH 062/166] Add some basic support for bitmap graphics contexts and color spaces --- Cargo.toml | 1 + src/base.rs | 8 +++ src/color_space.rs | 88 ++++++++++++++++++++++++++++ src/context.rs | 139 +++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 5 +- 5 files changed, 240 insertions(+), 1 deletion(-) create mode 100644 src/color_space.rs create mode 100644 src/context.rs diff --git a/Cargo.toml b/Cargo.toml index 1f4f40e..575ab70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ license = "MIT / Apache-2.0" [dependencies] libc = "*" core-foundation = "*" + diff --git a/src/base.rs b/src/base.rs index 65d3999..ff20379 100644 --- a/src/base.rs +++ b/src/base.rs @@ -24,3 +24,11 @@ pub type CGError = libc::int32_t; pub type CGAffineTransform = (); +pub const kCGImageAlphaNone: u32 = 0; +pub const kCGImageAlphaPremultipliedLast: u32 = 1; +pub const kCGImageAlphaPremultipliedFirst: u32 = 2; +pub const kCGImageAlphaLast: u32 = 3; +pub const kCGImageAlphaFirst: u32 = 4; +pub const kCGImageAlphaNoneSkipLast: u32 = 5; +pub const kCGImageAlphaNoneSkipFirst: u32 = 6; + diff --git a/src/color_space.rs b/src/color_space.rs new file mode 100644 index 0000000..1947707 --- /dev/null +++ b/src/color_space.rs @@ -0,0 +1,88 @@ +// Copyright 2015 The Servo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; +use libc::{c_void, size_t}; +use std::mem; +use std::ptr; + +#[repr(C)] +struct __CGColorSpace; + +pub type CGColorSpaceRef = *const __CGColorSpace; + +pub struct CGColorSpace { + obj: CGColorSpaceRef, +} + +impl Drop for CGColorSpace { + fn drop(&mut self) { + unsafe { + CFRelease(self.as_CFTypeRef()) + } + } +} + +impl Clone for CGColorSpace { + fn clone(&self) -> CGColorSpace { + unsafe { + TCFType::wrap_under_get_rule(self.as_concrete_TypeRef()) + } + } +} + +impl TCFType for CGColorSpace { + #[inline] + fn as_concrete_TypeRef(&self) -> CGColorSpaceRef { + self.obj + } + + #[inline] + unsafe fn wrap_under_get_rule(reference: CGColorSpaceRef) -> CGColorSpace { + let reference: CGColorSpaceRef = mem::transmute(CFRetain(mem::transmute(reference))); + TCFType::wrap_under_create_rule(reference) + } + + #[inline] + fn as_CFTypeRef(&self) -> CFTypeRef { + unsafe { + mem::transmute(self.as_concrete_TypeRef()) + } + } + + #[inline] + unsafe fn wrap_under_create_rule(obj: CGColorSpaceRef) -> CGColorSpace { + CGColorSpace { + obj: obj, + } + } + + #[inline] + fn type_id() -> CFTypeID { + unsafe { + CGColorSpaceGetTypeID() + } + } +} + +impl CGColorSpace { + pub fn create_device_rgb() -> CGColorSpace { + unsafe { + let result = CGColorSpaceCreateDeviceRGB(); + TCFType::wrap_under_create_rule(result) + } + } +} + +#[link(name = "ApplicationServices", kind = "framework")] +extern { + fn CGColorSpaceCreateDeviceRGB() -> CGColorSpaceRef; + fn CGColorSpaceGetTypeID() -> CFTypeID; +} + diff --git a/src/context.rs b/src/context.rs new file mode 100644 index 0000000..38653c7 --- /dev/null +++ b/src/context.rs @@ -0,0 +1,139 @@ +// Copyright 2015 The Servo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use color_space::{CGColorSpace, CGColorSpaceRef}; +use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; +use libc::{c_void, size_t}; +use std::mem; +use std::ptr; +use std::slice; + +#[repr(C)] +struct __CGContext; + +pub type CGContextRef = *const __CGContext; + +pub struct CGContext { + obj: CGContextRef, +} + +impl Drop for CGContext { + fn drop(&mut self) { + unsafe { + CFRelease(self.as_CFTypeRef()) + } + } +} + +impl Clone for CGContext { + fn clone(&self) -> CGContext { + unsafe { + TCFType::wrap_under_get_rule(self.as_concrete_TypeRef()) + } + } +} + +impl TCFType for CGContext { + #[inline] + fn as_concrete_TypeRef(&self) -> CGContextRef { + self.obj + } + + #[inline] + unsafe fn wrap_under_get_rule(reference: CGContextRef) -> CGContext { + let reference: CGContextRef = mem::transmute(CFRetain(mem::transmute(reference))); + TCFType::wrap_under_create_rule(reference) + } + + #[inline] + fn as_CFTypeRef(&self) -> CFTypeRef { + unsafe { + mem::transmute(self.as_concrete_TypeRef()) + } + } + + #[inline] + unsafe fn wrap_under_create_rule(obj: CGContextRef) -> CGContext { + CGContext { + obj: obj, + } + } + + #[inline] + fn type_id() -> CFTypeID { + unsafe { + CGContextGetTypeID() + } + } +} + +impl CGContext { + pub fn create_bitmap_context(width: size_t, + height: size_t, + bits_per_component: size_t, + bytes_per_row: size_t, + space: &CGColorSpace, + bitmap_info: u32) + -> CGContext { + unsafe { + let result = CGBitmapContextCreate(ptr::null_mut(), + width, + height, + bits_per_component, + bytes_per_row, + space.as_concrete_TypeRef(), + bitmap_info); + TCFType::wrap_under_create_rule(result) + } + } + + pub fn data(&mut self) -> &mut [u8] { + unsafe { + slice::from_raw_parts_mut( + CGBitmapContextGetData(self.as_concrete_TypeRef()) as *mut u8, + (self.height() * self.bytes_per_row()) as usize) + } + } + + pub fn width(&self) -> size_t { + unsafe { + CGBitmapContextGetWidth(self.as_concrete_TypeRef()) + } + } + + pub fn height(&self) -> size_t { + unsafe { + CGBitmapContextGetHeight(self.as_concrete_TypeRef()) + } + } + + pub fn bytes_per_row(&self) -> size_t { + unsafe { + CGBitmapContextGetBytesPerRow(self.as_concrete_TypeRef()) + } + } +} + +#[link(name = "ApplicationServices", kind = "framework")] +extern { + fn CGBitmapContextCreate(data: *mut c_void, + width: size_t, + height: size_t, + bitsPerComponent: size_t, + bytesPerRow: size_t, + space: CGColorSpaceRef, + bitmapInfo: u32) + -> CGContextRef; + fn CGBitmapContextGetData(context: CGContextRef) -> *mut c_void; + fn CGBitmapContextGetWidth(context: CGContextRef) -> size_t; + fn CGBitmapContextGetHeight(context: CGContextRef) -> size_t; + fn CGBitmapContextGetBytesPerRow(context: CGContextRef) -> size_t; + fn CGContextGetTypeID() -> CFTypeID; +} + diff --git a/src/lib.rs b/src/lib.rs index 0ef6aaf..03d2c90 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,10 @@ extern crate libc; extern crate core_foundation; pub mod base; +pub mod color_space; +pub mod context; pub mod data_provider; +pub mod display; pub mod font; pub mod geometry; -pub mod display; + From d072ab30afe60ebb27b93453eccff7fed198873a Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 16 Oct 2015 17:29:05 -0700 Subject: [PATCH 063/166] Add support for fill colors and font AA. Also, mark Core Graphics contexts as `Send` and `Sync` since (I believe) they are thread-safe. --- src/context.rs | 26 ++++++++++++++++++++++++++ src/font.rs | 3 +++ 2 files changed, 29 insertions(+) diff --git a/src/context.rs b/src/context.rs index 38653c7..c37fe2a 100644 --- a/src/context.rs +++ b/src/context.rs @@ -7,6 +7,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use base::CGFloat; use color_space::{CGColorSpace, CGColorSpaceRef}; use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; use libc::{c_void, size_t}; @@ -118,6 +119,24 @@ impl CGContext { CGBitmapContextGetBytesPerRow(self.as_concrete_TypeRef()) } } + + pub fn set_rgb_fill_color(&self, red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) { + unsafe { + CGContextSetRGBFillColor(self.as_concrete_TypeRef(), red, green, blue, alpha) + } + } + + pub fn set_allows_font_smoothing(&self, allows_font_smoothing: bool) { + unsafe { + CGContextSetAllowsFontSmoothing(self.as_concrete_TypeRef(), allows_font_smoothing) + } + } + + pub fn set_should_smooth_fonts(&self, should_smooth_fonts: bool) { + unsafe { + CGContextSetShouldSmoothFonts(self.as_concrete_TypeRef(), should_smooth_fonts) + } + } } #[link(name = "ApplicationServices", kind = "framework")] @@ -135,5 +154,12 @@ extern { fn CGBitmapContextGetHeight(context: CGContextRef) -> size_t; fn CGBitmapContextGetBytesPerRow(context: CGContextRef) -> size_t; fn CGContextGetTypeID() -> CFTypeID; + fn CGContextSetAllowsFontSmoothing(c: CGContextRef, allowsFontSmoothing: bool); + fn CGContextSetShouldSmoothFonts(c: CGContextRef, shouldSmoothFonts: bool); + fn CGContextSetRGBFillColor(context: CGContextRef, + red: CGFloat, + green: CGFloat, + blue: CGFloat, + alpha: CGFloat); } diff --git a/src/font.rs b/src/font.rs index ca2449e..8fcf21f 100644 --- a/src/font.rs +++ b/src/font.rs @@ -25,6 +25,9 @@ pub struct CGFont { obj: CGFontRef, } +unsafe impl Send for CGFont {} +unsafe impl Sync for CGFont {} + impl Clone for CGFont { #[inline] fn clone(&self) -> CGFont { From da0c05993dab4f042fe22c9cc204f07b4ad4238d Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Wed, 28 Oct 2015 17:25:03 -0700 Subject: [PATCH 064/166] Update version number --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 575ab70..b128c34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.1.1" +version = "0.1.2" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" From 76ece0ace5b8ad0639b4815a0f939c1178d220e5 Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Tue, 3 Nov 2015 15:13:33 -0800 Subject: [PATCH 065/166] Upgrade to libc 0.2 --- Cargo.toml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b128c34..6e0bf06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,11 +3,10 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.1.2" +version = "0.2.0" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" [dependencies] -libc = "*" -core-foundation = "*" - +libc = "0.2" +core-foundation = "0.2" From 7f00bbf441e344e1215198bb4c12f57926a0a2a0 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Mon, 30 Nov 2015 16:39:49 -0800 Subject: [PATCH 066/166] Support serialization of `CGFont` objects. Due to API limitations, this will only serialize native fonts, not Web fonts. Specifically, there is no way I know of to fetch the `CGDataProvider` associated with a `CGFont`. This isn't a problem for Servo, however, because Servo knows which fonts are Web fonts and stores their byte data separately. --- Cargo.toml | 2 ++ src/font.rs | 43 +++++++++++++++++++++++++++++++++++++++++-- src/lib.rs | 1 + 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6e0bf06..42299d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,5 @@ license = "MIT / Apache-2.0" [dependencies] libc = "0.2" core-foundation = "0.2" +serde = "0.6" + diff --git a/src/font.rs b/src/font.rs index 8fcf21f..0cb89fc 100644 --- a/src/font.rs +++ b/src/font.rs @@ -8,9 +8,12 @@ // except according to those terms. use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; +use core_foundation::string::{CFString, CFStringRef}; use data_provider::{CGDataProvider, CGDataProviderRef}; use libc; +use serde::de::{self, Deserialize, Deserializer}; +use serde::ser::{Serialize, Serializer}; use std::mem; use std::ptr; @@ -28,6 +31,22 @@ pub struct CGFont { unsafe impl Send for CGFont {} unsafe impl Sync for CGFont {} +impl Serialize for CGFont { + fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer { + let postscript_name = self.postscript_name().to_string(); + postscript_name.serialize(serializer) + } +} + +impl Deserialize for CGFont { + fn deserialize(deserializer: &mut D) -> Result where D: Deserializer { + let postscript_name: String = try!(Deserialize::deserialize(deserializer)); + CGFont::from_name(&CFString::new(&*postscript_name)).map_err(|err| { + de::Error::syntax("Couldn't find a font with that PostScript name!") + }) + } +} + impl Clone for CGFont { #[inline] fn clone(&self) -> CGFont { @@ -92,15 +111,35 @@ impl CGFont { } } } + + pub fn from_name(name: &CFString) -> Result { + unsafe { + let font_ref = CGFontCreateWithFontName(name.as_concrete_TypeRef()); + if font_ref != ptr::null() { + Ok(TCFType::wrap_under_create_rule(font_ref)) + } else { + Err(()) + } + } + } + + pub fn postscript_name(&self) -> CFString { + unsafe { + let string_ref = CGFontCopyPostScriptName(self.obj); + TCFType::wrap_under_create_rule(string_ref) + } + } } #[link(name = "ApplicationServices", kind = "framework")] extern { - - // TODO: basically nothing has bindings (even commented-out) besides what we use. + // TODO: basically nothing has bindings (even commented-out) besides what we use. fn CGFontCreateWithDataProvider(provider: CGDataProviderRef) -> CGFontRef; + fn CGFontCreateWithFontName(name: CFStringRef) -> CGFontRef; fn CGFontGetTypeID() -> CFTypeID; + fn CGFontCopyPostScriptName(font: CGFontRef) -> CFStringRef; + // These do the same thing as CFRetain/CFRelease, except // gracefully handle a NULL argument. We don't use them. //fn CGFontRetain(font: CGFontRef); diff --git a/src/lib.rs b/src/lib.rs index 03d2c90..a7d45ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ extern crate libc; extern crate core_foundation; +extern crate serde; pub mod base; pub mod color_space; From f5e5e9c331e21780b99d08194a9c7826678b0b39 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Mon, 14 Dec 2015 21:10:40 -0500 Subject: [PATCH 067/166] Bump version to 0.2.1 In order to pick up this change: https://github.com/servo/core-graphics-rs/pull/46 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 42299d7..19ee79d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.2.0" +version = "0.2.1" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" From b7c2f5c29416d8478a464367546d8353904cb57b Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Tue, 1 Mar 2016 01:54:41 +0100 Subject: [PATCH 068/166] Bump serde to 0.7 --- Cargo.toml | 5 ++--- src/font.rs | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 19ee79d..302dfcf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,12 +3,11 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.2.1" +version = "0.3.0" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" [dependencies] libc = "0.2" core-foundation = "0.2" -serde = "0.6" - +serde = "0.7" diff --git a/src/font.rs b/src/font.rs index 0cb89fc..0ed20c5 100644 --- a/src/font.rs +++ b/src/font.rs @@ -42,7 +42,7 @@ impl Deserialize for CGFont { fn deserialize(deserializer: &mut D) -> Result where D: Deserializer { let postscript_name: String = try!(Deserialize::deserialize(deserializer)); CGFont::from_name(&CFString::new(&*postscript_name)).map_err(|err| { - de::Error::syntax("Couldn't find a font with that PostScript name!") + de::Error::invalid_value("Couldn't find a font with that PostScript name!") }) } } From b952ddd051ea89e061d956ce0386cd89945348d6 Mon Sep 17 00:00:00 2001 From: Luo Date: Tue, 10 May 2016 13:54:11 +0800 Subject: [PATCH 069/166] update src/display.rs --- src/display.rs | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/display.rs b/src/display.rs index 02fa138..3d86f2b 100644 --- a/src/display.rs +++ b/src/display.rs @@ -8,12 +8,36 @@ // except according to those terms. // this file defines CGFloat, as well as stubbed data types. +extern crate core_foundation; -use libc; -use base::{CGError, boolean_t}; -use geometry::{CGRect, CGPoint, CGSize}; +pub use libc; +pub use base::{CGError, boolean_t}; +pub use geometry::{CGRect, CGPoint, CGSize}; pub type CGDirectDisplayID = libc::uint32_t; +pub type CGWindowID = libc::uint32_t; + +pub const kCGNullWindowID: CGWindowID = 0 as CGWindowID; + + +pub const kCGWindowListOptionAll: libc::uint32_t = 0; +pub const kCGWindowListOptionOnScreenOnly: libc::uint32_t = 1 << 0; +pub const kCGWindowListOptionOnScreenAboveWindow: libc::uint32_t = 1 << 1; +pub const kCGWindowListOptionOnScreenBelowWindow: libc::uint32_t = 1 << 2; +pub const kCGWindowListOptionIncludingWindow: libc::uint32_t = 1 << 3; +pub const kCGWindowListExcludeDesktopElements: libc::uint32_t = 1 << 4; + +pub enum CGWindowListOption { + kCGWindowListOptionAll, + kCGWindowListOptionOnScreenOnly, + kCGWindowListOptionOnScreenAboveWindow, + kCGWindowListOptionOnScreenBelowWindow, + kCGWindowListOptionIncludingWindow, + kCGWindowListExcludeDesktopElements, +} + +pub use core_foundation::dictionary::{ CFDictionary, CFDictionaryRef }; +pub use core_foundation::array::{ CFArray, CFArrayRef }; #[link(name = "ApplicationServices", kind = "framework")] extern { @@ -50,4 +74,7 @@ extern { pub fn CGWarpMouseCursorPosition(point: CGPoint) -> CGError; pub fn CGAssociateMouseAndMouseCursorPosition(connected: bool) -> CGError; + // Window Services Reference + pub fn CGWindowListCopyWindowInfo(option: CGWindowListOption, relativeToWindow: CGWindowID ) -> CFArrayRef; + } From d1cb1a776411fce62fb7055f75512197f6b7296e Mon Sep 17 00:00:00 2001 From: Luo Date: Thu, 12 May 2016 06:34:32 +0800 Subject: [PATCH 070/166] add some display api. --- src/display.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/display.rs b/src/display.rs index 3d86f2b..69d13cb 100644 --- a/src/display.rs +++ b/src/display.rs @@ -9,6 +9,7 @@ // this file defines CGFloat, as well as stubbed data types. extern crate core_foundation; +// extern crate core_foundation_sys; pub use libc; pub use base::{CGError, boolean_t}; @@ -20,24 +21,20 @@ pub type CGWindowID = libc::uint32_t; pub const kCGNullWindowID: CGWindowID = 0 as CGWindowID; -pub const kCGWindowListOptionAll: libc::uint32_t = 0; -pub const kCGWindowListOptionOnScreenOnly: libc::uint32_t = 1 << 0; -pub const kCGWindowListOptionOnScreenAboveWindow: libc::uint32_t = 1 << 1; -pub const kCGWindowListOptionOnScreenBelowWindow: libc::uint32_t = 1 << 2; -pub const kCGWindowListOptionIncludingWindow: libc::uint32_t = 1 << 3; -pub const kCGWindowListExcludeDesktopElements: libc::uint32_t = 1 << 4; +pub type CGWindowListOption = libc::uint32_t; -pub enum CGWindowListOption { - kCGWindowListOptionAll, - kCGWindowListOptionOnScreenOnly, - kCGWindowListOptionOnScreenAboveWindow, - kCGWindowListOptionOnScreenBelowWindow, - kCGWindowListOptionIncludingWindow, - kCGWindowListExcludeDesktopElements, -} +pub const kCGWindowListOptionAll: CGWindowListOption = 0; +pub const kCGWindowListOptionOnScreenOnly: CGWindowListOption = 1 << 0; +pub const kCGWindowListOptionOnScreenAboveWindow: CGWindowListOption = 1 << 1; +pub const kCGWindowListOptionOnScreenBelowWindow: CGWindowListOption = 1 << 2; +pub const kCGWindowListOptionIncludingWindow: CGWindowListOption = 1 << 3; +pub const kCGWindowListExcludeDesktopElements: CGWindowListOption = 1 << 4; -pub use core_foundation::dictionary::{ CFDictionary, CFDictionaryRef }; + +pub use core_foundation::dictionary::{ CFDictionary, CFDictionaryRef, CFDictionaryGetValueIfPresent }; pub use core_foundation::array::{ CFArray, CFArrayRef }; +pub use core_foundation::array::{ CFArrayGetCount, CFArrayGetValueAtIndex }; +pub use core_foundation::base::{ CFIndex, CFRelease, CFTypeRef }; #[link(name = "ApplicationServices", kind = "framework")] extern { From 3c3727442e41c6a8966cf63e7b9829962a15ab4d Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 27 May 2016 13:21:04 -0700 Subject: [PATCH 071/166] Add some bindings to the private `CGSRegion` APIs. These enable us to tell the window server about the opaque regions of the window so that it can perform occlusion culling. See https://github.com/NUIKit/CGSInternal/blob/master/CGSRegion.h --- src/lib.rs | 1 + src/private.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/private.rs diff --git a/src/lib.rs b/src/lib.rs index a7d45ea..a162a1c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,4 +18,5 @@ pub mod data_provider; pub mod display; pub mod font; pub mod geometry; +pub mod private; diff --git a/src/private.rs b/src/private.rs new file mode 100644 index 0000000..ff9a57d --- /dev/null +++ b/src/private.rs @@ -0,0 +1,57 @@ +// Copyright 2016 The Servo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Evil private APIs. +//! +//! These are liable to change at any time. Use with caution! + +use geometry::CGRect; +use std::ptr; + +pub struct CGSRegion { + region: ffi::CGSRegion, +} + +impl Drop for CGSRegion { + fn drop(&mut self) { + unsafe { + ffi::CGSRegionRelease(self.region) + } + } +} + +impl CGSRegion { + #[inline] + pub fn from_rect(rect: &CGRect) -> CGSRegion { + unsafe { + let mut region = ptr::null_mut(); + assert!(ffi::CGSNewRegionWithRect(rect, &mut region) == 0); + CGSRegion { + region: region, + } + } + } +} + +mod ffi { + use geometry::CGRect; + + // This is an enum so that we can't easily make instances of this opaque type. + enum CGSRegionObject {} + + pub type CGSRegion = *mut CGSRegionObject; + pub type OSStatus = i32; + + #[link(name = "ApplicationServices", kind = "framework")] + extern { + pub fn CGSRegionRelease(region: CGSRegion); + pub fn CGSNewRegionWithRect(rect: *const CGRect, region: *mut CGSRegion) -> OSStatus; + } +} + From 1229483c10a82ec1bf37a5c2b029d8fc2b3cb193 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Wed, 1 Jun 2016 20:12:42 -0700 Subject: [PATCH 072/166] 0.3.1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 302dfcf..c3e2f2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.3.0" +version = "0.3.1" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" From b31cffb64868c0cf8e2b217f62989677e88029cc Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 3 Jun 2016 16:03:35 -0700 Subject: [PATCH 073/166] geometry: Make `CGPoint`, `CGRect`, and `CGSize` copyable and clonable. --- src/geometry.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/geometry.rs b/src/geometry.rs index fbe1280..67f4c2d 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -10,18 +10,21 @@ use base::CGFloat; #[repr(C)] +#[derive(Clone, Copy, Debug)] pub struct CGSize { pub width: CGFloat, pub height: CGFloat, } #[repr(C)] +#[derive(Clone, Copy, Debug)] pub struct CGPoint { pub x: CGFloat, pub y: CGFloat, } #[repr(C)] +#[derive(Clone, Copy, Debug)] pub struct CGRect { pub origin: CGPoint, pub size: CGSize From 9b16b0e21429db7132edc024388635107985a1b9 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 3 Jun 2016 16:04:03 -0700 Subject: [PATCH 074/166] geometry: Add `CG_ZERO_POINT` to match `CGZeroPoint`. --- src/geometry.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/geometry.rs b/src/geometry.rs index 67f4c2d..06741f0 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -9,6 +9,11 @@ use base::CGFloat; +pub const CG_ZERO_POINT: CGPoint = CGPoint { + x: 0.0, + y: 0.0, +}; + #[repr(C)] #[derive(Clone, Copy, Debug)] pub struct CGSize { From 508a240632324d4380f1111778ecbdf5fd9a5ebb Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 3 Jun 2016 16:04:40 -0700 Subject: [PATCH 075/166] geometry: Add some convenience constructors for geometry types. --- src/geometry.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/geometry.rs b/src/geometry.rs index 06741f0..b7f73b0 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -21,6 +21,16 @@ pub struct CGSize { pub height: CGFloat, } +impl CGSize { + #[inline] + pub fn new(width: CGFloat, height: CGFloat) -> CGSize { + CGSize { + width: width, + height: height, + } + } +} + #[repr(C)] #[derive(Clone, Copy, Debug)] pub struct CGPoint { @@ -28,9 +38,30 @@ pub struct CGPoint { pub y: CGFloat, } +impl CGPoint { + #[inline] + pub fn new(x: CGFloat, y: CGFloat) -> CGPoint { + CGPoint { + x: x, + y: y, + } + } +} + #[repr(C)] #[derive(Clone, Copy, Debug)] pub struct CGRect { pub origin: CGPoint, pub size: CGSize } + +impl CGRect { + #[inline] + pub fn new(origin: &CGPoint, size: &CGSize) -> CGRect { + CGRect { + origin: *origin, + size: *size, + } + } +} + From 35d51561822465041de29409450a29c36c5aadc6 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 3 Jun 2016 16:04:59 -0700 Subject: [PATCH 076/166] geometry: Add a binding to `CGRectInset`. --- src/geometry.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/geometry.rs b/src/geometry.rs index b7f73b0..e4145da 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -63,5 +63,22 @@ impl CGRect { size: *size, } } + + #[inline] + pub fn inset(&self, size: &CGSize) -> CGRect { + unsafe { + ffi::CGRectInset(*self, size.width, size.height) + } + } +} + +mod ffi { + use base::CGFloat; + use geometry::CGRect; + + #[link(name = "ApplicationServices", kind = "framework")] + extern { + pub fn CGRectInset(rect: CGRect, dx: CGFloat, dy: CGFloat) -> CGRect; + } } From 828a7fde4b529ed4c6016a739fd3630c63eea484 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 3 Jun 2016 16:05:44 -0700 Subject: [PATCH 077/166] private: Rename `CGSRegion` types to match reverse engineered headers available elsewhere. See: https://github.com/NUIKit/CGSInternal/blob/master/CGSRegion.h --- src/private.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/private.rs b/src/private.rs index ff9a57d..4758519 100644 --- a/src/private.rs +++ b/src/private.rs @@ -15,7 +15,7 @@ use geometry::CGRect; use std::ptr; pub struct CGSRegion { - region: ffi::CGSRegion, + region: ffi::CGSRegionRef, } impl Drop for CGSRegion { @@ -45,13 +45,14 @@ mod ffi { // This is an enum so that we can't easily make instances of this opaque type. enum CGSRegionObject {} - pub type CGSRegion = *mut CGSRegionObject; + pub type CGError = OSStatus; + pub type CGSRegionRef = *mut CGSRegionObject; pub type OSStatus = i32; #[link(name = "ApplicationServices", kind = "framework")] extern { - pub fn CGSRegionRelease(region: CGSRegion); - pub fn CGSNewRegionWithRect(rect: *const CGRect, region: *mut CGSRegion) -> OSStatus; + pub fn CGSRegionRelease(region: CGSRegionRef); + pub fn CGSNewRegionWithRect(rect: *const CGRect, outRegion: *mut CGSRegionRef) -> CGError; } } From 621a033730853a8660f6bc83f2e84f1794293470 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 3 Jun 2016 16:07:24 -0700 Subject: [PATCH 078/166] private: Add a binding to `CGSNewRegionWithRectList`. --- src/private.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/private.rs b/src/private.rs index 4758519..3be0f10 100644 --- a/src/private.rs +++ b/src/private.rs @@ -37,10 +37,24 @@ impl CGSRegion { } } } + + #[inline] + pub fn from_rects(rects: &[CGRect]) -> CGSRegion { + unsafe { + let mut region = ptr::null_mut(); + assert!(ffi::CGSNewRegionWithRectList(rects.as_ptr(), + rects.len() as c_uint, + &mut region) == 0); + CGSRegion { + region: region, + } + } + } } mod ffi { use geometry::CGRect; + use libc::c_uint; // This is an enum so that we can't easily make instances of this opaque type. enum CGSRegionObject {} @@ -53,6 +67,10 @@ mod ffi { extern { pub fn CGSRegionRelease(region: CGSRegionRef); pub fn CGSNewRegionWithRect(rect: *const CGRect, outRegion: *mut CGSRegionRef) -> CGError; + pub fn CGSNewRegionWithRectList(rects: *const CGRect, + rectCount: c_uint, + outRegion: *mut CGSRegionRef) + -> CGError; } } From 443e95792aa1f8ec5facf26899e35409484b34ee Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 3 Jun 2016 16:07:58 -0700 Subject: [PATCH 079/166] private: Add a binding to `CGSSetSurfaceShape`. This is useful to make OpenGL areas have rounded corners. It matches what the Cocoa framework does internally for this. --- src/private.rs | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/private.rs b/src/private.rs index 3be0f10..518c55c 100644 --- a/src/private.rs +++ b/src/private.rs @@ -12,6 +12,7 @@ //! These are liable to change at any time. Use with caution! use geometry::CGRect; +use libc::{c_int, c_uint}; use std::ptr; pub struct CGSRegion { @@ -52,9 +53,42 @@ impl CGSRegion { } } +/// This should always be memory-safe; the window server rejects any invalid surface IDs. +pub struct CGSSurface { + context_id: c_uint, + window_number: c_int, + surface_id: c_uint, +} + +impl CGSSurface { + #[inline] + pub fn from_ids(context_id: c_uint, window_number: c_int, surface_id: c_uint) -> CGSSurface { + CGSSurface { + context_id: context_id, + window_number: window_number, + surface_id: surface_id, + } + } + + #[inline] + pub fn id(&self) -> c_uint { + self.surface_id + } + + #[inline] + pub fn set_shape(&self, region: &CGSRegion) { + unsafe { + assert!(ffi::CGSSetSurfaceShape(self.context_id, + self.window_number, + self.surface_id, + region.region) == 0) + } + } +} + mod ffi { use geometry::CGRect; - use libc::c_uint; + use libc::{c_int, c_uint}; // This is an enum so that we can't easily make instances of this opaque type. enum CGSRegionObject {} @@ -71,6 +105,12 @@ mod ffi { rectCount: c_uint, outRegion: *mut CGSRegionRef) -> CGError; + + pub fn CGSSetSurfaceShape(contextID: c_uint, + windowNumber: c_int, + surfaceID: c_uint, + region: CGSRegionRef) + -> CGError; } } From eaea55d746c7bfc2af5d22c7d20f23dcfa1930e0 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 3 Jun 2016 17:21:46 -0700 Subject: [PATCH 080/166] 0.3.2 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c3e2f2f..d474096 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.3.1" +version = "0.3.2" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" From 8675f41d7e075d2eb9edfe851fa98fcac5a0fa02 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Sat, 30 Jul 2016 03:13:49 +0200 Subject: [PATCH 081/166] Update serde to 0.8 --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d474096..22580e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,11 +3,11 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.3.2" +version = "0.4.0" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" [dependencies] libc = "0.2" core-foundation = "0.2" -serde = "0.7" +serde = "0.8" From 51841f4beee82d7936c063308d11e48fe95e8c48 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Wed, 3 Aug 2016 11:09:44 +0200 Subject: [PATCH 082/166] Test on stable. --- .travis.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f8b2734..19ef02b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,9 @@ language: rust os: osx -rust: nightly +rust: + - nightly + - beta + - stable notifications: webhooks: http://build.servo.org:54856/travis From 39173f66d7aaf891cc2983a87e90bb8586c48426 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Thu, 1 Sep 2016 11:49:25 +0200 Subject: [PATCH 083/166] Silence all non_upper_case_globals warnings --- src/base.rs | 1 + src/display.rs | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/base.rs b/src/base.rs index 0ea9795..6c7c33d 100644 --- a/src/base.rs +++ b/src/base.rs @@ -10,6 +10,7 @@ // this file defines CGFloat, as well as stubbed data types. #![allow(non_camel_case_types)] +#![allow(non_upper_case_globals)] use libc; diff --git a/src/display.rs b/src/display.rs index 69d13cb..a7f0847 100644 --- a/src/display.rs +++ b/src/display.rs @@ -7,6 +7,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#![allow(non_upper_case_globals)] + // this file defines CGFloat, as well as stubbed data types. extern crate core_foundation; // extern crate core_foundation_sys; From 17c0b1ed9ef7d2e7fa016672eefb4676a45e4d5a Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Thu, 1 Sep 2016 11:49:45 +0200 Subject: [PATCH 084/166] Remove some useless comment and commented-out code --- src/display.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/display.rs b/src/display.rs index a7f0847..52eec0c 100644 --- a/src/display.rs +++ b/src/display.rs @@ -9,9 +9,7 @@ #![allow(non_upper_case_globals)] -// this file defines CGFloat, as well as stubbed data types. extern crate core_foundation; -// extern crate core_foundation_sys; pub use libc; pub use base::{CGError, boolean_t}; From e2e56e909bdfbcdccb9ee84c042fbacdd8b0e012 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Thu, 1 Sep 2016 11:50:19 +0200 Subject: [PATCH 085/166] Remove some unused imports --- src/color_space.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/color_space.rs b/src/color_space.rs index 1947707..4a2ddda 100644 --- a/src/color_space.rs +++ b/src/color_space.rs @@ -8,9 +8,7 @@ // except according to those terms. use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; -use libc::{c_void, size_t}; use std::mem; -use std::ptr; #[repr(C)] struct __CGColorSpace; From 66df225fd8a5130ef2f6b5cb06039e7c9599fc3b Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Thu, 1 Sep 2016 11:51:28 +0200 Subject: [PATCH 086/166] Silence an unused_variables warning --- src/font.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/font.rs b/src/font.rs index 0ed20c5..fe051be 100644 --- a/src/font.rs +++ b/src/font.rs @@ -41,7 +41,7 @@ impl Serialize for CGFont { impl Deserialize for CGFont { fn deserialize(deserializer: &mut D) -> Result where D: Deserializer { let postscript_name: String = try!(Deserialize::deserialize(deserializer)); - CGFont::from_name(&CFString::new(&*postscript_name)).map_err(|err| { + CGFont::from_name(&CFString::new(&*postscript_name)).map_err(|_| { de::Error::invalid_value("Couldn't find a font with that PostScript name!") }) } From e47505dfa829742a2c1921b363c702a7f49a5e84 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Thu, 1 Sep 2016 11:57:12 +0200 Subject: [PATCH 087/166] Don't reexport libc --- src/display.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/display.rs b/src/display.rs index 52eec0c..491e277 100644 --- a/src/display.rs +++ b/src/display.rs @@ -11,7 +11,7 @@ extern crate core_foundation; -pub use libc; +use libc; pub use base::{CGError, boolean_t}; pub use geometry::{CGRect, CGPoint, CGSize}; From e805130b6bec009bbacedd0646bcb88e717f0e51 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Thu, 1 Sep 2016 11:58:05 +0200 Subject: [PATCH 088/166] Fix all private_in_public errors --- src/color_space.rs | 2 +- src/context.rs | 2 +- src/data_provider.rs | 2 +- src/font.rs | 2 +- src/private.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/color_space.rs b/src/color_space.rs index 4a2ddda..10a728a 100644 --- a/src/color_space.rs +++ b/src/color_space.rs @@ -11,7 +11,7 @@ use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; use std::mem; #[repr(C)] -struct __CGColorSpace; +pub struct __CGColorSpace; pub type CGColorSpaceRef = *const __CGColorSpace; diff --git a/src/context.rs b/src/context.rs index c37fe2a..52987c0 100644 --- a/src/context.rs +++ b/src/context.rs @@ -16,7 +16,7 @@ use std::ptr; use std::slice; #[repr(C)] -struct __CGContext; +pub struct __CGContext; pub type CGContextRef = *const __CGContext; diff --git a/src/data_provider.rs b/src/data_provider.rs index bb2fdba..fe449a2 100644 --- a/src/data_provider.rs +++ b/src/data_provider.rs @@ -26,7 +26,7 @@ pub type CGDataProviderReleaseDataCallback = *const u8; pub type CGDataProviderGetBytesAtPositionCallback = *const u8; #[repr(C)] -struct __CGDataProvider; +pub struct __CGDataProvider; pub type CGDataProviderRef = *const __CGDataProvider; diff --git a/src/font.rs b/src/font.rs index fe051be..e3e2700 100644 --- a/src/font.rs +++ b/src/font.rs @@ -20,7 +20,7 @@ use std::ptr; pub type CGGlyph = libc::c_ushort; #[repr(C)] -struct __CGFont; +pub struct __CGFont; pub type CGFontRef = *const __CGFont; diff --git a/src/private.rs b/src/private.rs index 518c55c..11231a9 100644 --- a/src/private.rs +++ b/src/private.rs @@ -91,7 +91,7 @@ mod ffi { use libc::{c_int, c_uint}; // This is an enum so that we can't easily make instances of this opaque type. - enum CGSRegionObject {} + pub enum CGSRegionObject {} pub type CGError = OSStatus; pub type CGSRegionRef = *mut CGSRegionObject; From 6c4a34acd9186f23cebf74d5b4ef22391c285ed1 Mon Sep 17 00:00:00 2001 From: Glenn Watson Date: Thu, 1 Sep 2016 13:19:09 +1000 Subject: [PATCH 089/166] Support changing AA settings on a context. --- Cargo.toml | 2 +- src/context.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 22580e2..3381499 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.4.0" +version = "0.4.1" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" diff --git a/src/context.rs b/src/context.rs index 52987c0..9cdd522 100644 --- a/src/context.rs +++ b/src/context.rs @@ -137,6 +137,18 @@ impl CGContext { CGContextSetShouldSmoothFonts(self.as_concrete_TypeRef(), should_smooth_fonts) } } + + pub fn set_allows_antialiasing(&self, allows_antialiasing: bool) { + unsafe { + CGContextSetAllowsAntialiasing(self.as_concrete_TypeRef(), allows_antialiasing) + } + } + + pub fn set_should_antialias(&self, should_antialias: bool) { + unsafe { + CGContextSetShouldAntialias(self.as_concrete_TypeRef(), should_antialias) + } + } } #[link(name = "ApplicationServices", kind = "framework")] @@ -156,6 +168,8 @@ extern { fn CGContextGetTypeID() -> CFTypeID; fn CGContextSetAllowsFontSmoothing(c: CGContextRef, allowsFontSmoothing: bool); fn CGContextSetShouldSmoothFonts(c: CGContextRef, shouldSmoothFonts: bool); + fn CGContextSetAllowsAntialiasing(c: CGContextRef, allowsAntialiasing: bool); + fn CGContextSetShouldAntialias(c: CGContextRef, shouldAntialias: bool); fn CGContextSetRGBFillColor(context: CGContextRef, red: CGFloat, green: CGFloat, From 1d652dd5db9124aba79e4f6292c3b292ee5403d7 Mon Sep 17 00:00:00 2001 From: Wilson Giese Date: Tue, 18 Oct 2016 23:03:53 -0700 Subject: [PATCH 090/166] Add CGEvent and CGEventSource Signed-off-by: Wilson Giese --- src/event.rs | 173 ++++++++++++++++++++++++++++++++++++++++++++ src/event_source.rs | 97 +++++++++++++++++++++++++ src/lib.rs | 3 +- 3 files changed, 272 insertions(+), 1 deletion(-) create mode 100644 src/event.rs create mode 100644 src/event_source.rs diff --git a/src/event.rs b/src/event.rs new file mode 100644 index 0000000..ff710b4 --- /dev/null +++ b/src/event.rs @@ -0,0 +1,173 @@ +use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; +use event_source::{CGEventSource,CGEventSourceRef}; + +use libc; +use std::mem; +use std::ptr; + +pub type CGKeyCode = libc::uint16_t; + +/// Flags for events +/// +/// [Ref] (http://opensource.apple.com//source/IOHIDFamily/IOHIDFamily-308/IOHIDSystem/IOKit/hidsystem/IOLLEvent.h) +#[repr(C)] +pub enum CGEventFlags { + // Device-independent modifier key bits. + AlphaShift = 0x00010000, + Shift = 0x00020000, + Control = 0x00040000, + Alternate = 0x00080000, + Command = 0x00100000, + + // Special key identifiers. + Help = 0x00400000, + SecondaryFn = 0x00800000, + + // Identifies key events from numeric keypad area on extended keyboards. + NumericPad = 0x00200000, + + // Indicates if mouse/pen movement events are not being coalesced + NonCoalesced = 0x00000100, +} + +/// Possible tapping points for events. +#[repr(C)] +pub enum CGEventTapLocation { + HIDEventTap, + SessionEventTap, + AnnotatedSessionEventTap, +} + +#[repr(C)] +pub struct __CGEvent; + +pub type CGEventRef = *const __CGEvent; + +pub struct CGEvent { + obj: CGEventRef, +} + +impl Clone for CGEvent { + #[inline] + fn clone(&self) -> CGEvent { + unsafe { + TCFType::wrap_under_get_rule(self.obj) + } + } +} + +impl Drop for CGEvent { + fn drop(&mut self) { + unsafe { + let ptr = self.as_CFTypeRef(); + assert!(ptr != ptr::null()); + CFRelease(ptr); + } + } +} + +impl TCFType for CGEvent { + #[inline] + fn as_concrete_TypeRef(&self) -> CGEventRef { + self.obj + } + + #[inline] + unsafe fn wrap_under_get_rule(reference: CGEventRef) -> CGEvent { + let reference: CGEventRef = mem::transmute(CFRetain(mem::transmute(reference))); + TCFType::wrap_under_create_rule(reference) + } + + #[inline] + fn as_CFTypeRef(&self) -> CFTypeRef { + unsafe { + mem::transmute(self.as_concrete_TypeRef()) + } + } + + #[inline] + unsafe fn wrap_under_create_rule(obj: CGEventRef) -> CGEvent { + CGEvent { + obj: obj, + } + } + + #[inline] + fn type_id() -> CFTypeID { + unsafe { + CGEventGetTypeID() + } + } +} + +impl CGEvent { + pub fn new(source: CGEventSource, keycode: CGKeyCode, keydown: bool) -> Result { + unsafe { + let event_ref = CGEventCreateKeyboardEvent(source.as_concrete_TypeRef(), keycode, keydown); + if event_ref != ptr::null() { + Ok(TCFType::wrap_under_create_rule(event_ref)) + } else { + Err(()) + } + } + } + + pub fn post(&self, tapLocation: CGEventTapLocation) { + unsafe { + CGEventPost(tapLocation, self.as_concrete_TypeRef()); + } + } + + pub fn post_to_pid(&self, pid: libc::pid_t) { + unsafe { + CGEventPostToPid(pid, self.as_concrete_TypeRef()); + } + } + + pub fn set_flags(&self, flags: CGEventFlags) { + unsafe { + CGEventSetFlags(self.as_concrete_TypeRef(), flags); + } + } + + pub fn get_flags(&self) -> CGEventFlags { + unsafe { + CGEventGetFlags(self.as_concrete_TypeRef()) + } + } +} + +#[link(name = "ApplicationServices", kind = "framework")] +extern { + /// Return the type identifier for the opaque type `CGEventRef'. + fn CGEventGetTypeID() -> CFTypeID; + + /// Return a new keyboard event. + /// + /// The event source may be taken from another event, or may be NULL. Based + /// on the virtual key code values entered, the appropriate key down, key up, + /// or flags changed events are generated. + /// + /// All keystrokes needed to generate a character must be entered, including + /// SHIFT, CONTROL, OPTION, and COMMAND keys. For example, to produce a 'Z', + /// the SHIFT key must be down, the 'z' key must go down, and then the SHIFT + /// and 'z' key must be released: + fn CGEventCreateKeyboardEvent(source: CGEventSourceRef, keycode: CGKeyCode, + keydown: bool) -> CGEventRef; + + /// Post an event into the event stream at a specified location. + /// + /// This function posts the specified event immediately before any event taps + /// instantiated for that location, and the event passes through any such + /// taps. + fn CGEventPost(tapLocation: CGEventTapLocation, event: CGEventRef); + + /// Post an event to a specified process ID + fn CGEventPostToPid(pid: libc::pid_t, event: CGEventRef); + + /// Set the event flags of an event. + fn CGEventSetFlags(event: CGEventRef, flags: CGEventFlags); + + /// Return the event flags of an event. + fn CGEventGetFlags(event: CGEventRef) -> CGEventFlags; +} diff --git a/src/event_source.rs b/src/event_source.rs new file mode 100644 index 0000000..5a1ad34 --- /dev/null +++ b/src/event_source.rs @@ -0,0 +1,97 @@ +use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; + +use libc; +use std::mem; +use std::ptr; + +/// Possible source states of an event source. +#[repr(C)] +pub enum CGEventSourceStateID { + Private = -1, + CombinedSessionState = 0, + HIDSystemState = 1, +} + +#[repr(C)] +pub struct __CGEventSource; + +pub type CGEventSourceRef = *const __CGEventSource; + +pub struct CGEventSource { + obj: CGEventSourceRef, +} + +impl Clone for CGEventSource { + #[inline] + fn clone(&self) -> CGEventSource { + unsafe { + TCFType::wrap_under_get_rule(self.obj) + } + } +} + +impl Drop for CGEventSource { + fn drop(&mut self) { + unsafe { + let ptr = self.as_CFTypeRef(); + assert!(ptr != ptr::null()); + CFRelease(ptr); + } + } +} + +impl TCFType for CGEventSource { + #[inline] + fn as_concrete_TypeRef(&self) -> CGEventSourceRef { + self.obj + } + + #[inline] + unsafe fn wrap_under_get_rule(reference: CGEventSourceRef) -> CGEventSource { + let reference: CGEventSourceRef = mem::transmute(CFRetain(mem::transmute(reference))); + TCFType::wrap_under_create_rule(reference) + } + + #[inline] + fn as_CFTypeRef(&self) -> CFTypeRef { + unsafe { + mem::transmute(self.as_concrete_TypeRef()) + } + } + + #[inline] + unsafe fn wrap_under_create_rule(obj: CGEventSourceRef) -> CGEventSource { + CGEventSource { + obj: obj, + } + } + + #[inline] + fn type_id() -> CFTypeID { + unsafe { + CGEventSourceGetTypeID() + } + } +} + +impl CGEventSource { + pub fn new(stateID: CGEventSourceStateID) -> Result { + unsafe { + let event_source_ref = CGEventSourceCreate(stateID); + if event_source_ref != ptr::null() { + Ok(TCFType::wrap_under_create_rule(event_source_ref)) + } else { + Err(()) + } + } + } +} + +#[link(name = "ApplicationServices", kind = "framework")] +extern { + /// Return the type identifier for the opaque type `CGEventSourceRef'. + fn CGEventSourceGetTypeID() -> CFTypeID; + + /// Return a Quartz event source created with a specified source state. + fn CGEventSourceCreate(stateID: CGEventSourceStateID) -> CGEventSourceRef; +} diff --git a/src/lib.rs b/src/lib.rs index a162a1c..fed16b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,8 @@ pub mod color_space; pub mod context; pub mod data_provider; pub mod display; +pub mod event; +pub mod event_source; pub mod font; pub mod geometry; pub mod private; - From 438e083fc31ce4b6b1ecf9cd4db2da694fbddd89 Mon Sep 17 00:00:00 2001 From: Wilson Giese Date: Sun, 23 Oct 2016 23:36:13 -0700 Subject: [PATCH 091/166] Derive clone, copy, debug for enums --- src/event.rs | 2 ++ src/event_source.rs | 1 + 2 files changed, 3 insertions(+) diff --git a/src/event.rs b/src/event.rs index ff710b4..4254055 100644 --- a/src/event.rs +++ b/src/event.rs @@ -11,6 +11,7 @@ pub type CGKeyCode = libc::uint16_t; /// /// [Ref] (http://opensource.apple.com//source/IOHIDFamily/IOHIDFamily-308/IOHIDSystem/IOKit/hidsystem/IOLLEvent.h) #[repr(C)] +#[derive(Clone, Copy, Debug)] pub enum CGEventFlags { // Device-independent modifier key bits. AlphaShift = 0x00010000, @@ -32,6 +33,7 @@ pub enum CGEventFlags { /// Possible tapping points for events. #[repr(C)] +#[derive(Clone, Copy, Debug)] pub enum CGEventTapLocation { HIDEventTap, SessionEventTap, diff --git a/src/event_source.rs b/src/event_source.rs index 5a1ad34..16fa86b 100644 --- a/src/event_source.rs +++ b/src/event_source.rs @@ -6,6 +6,7 @@ use std::ptr; /// Possible source states of an event source. #[repr(C)] +#[derive(Clone, Copy, Debug)] pub enum CGEventSourceStateID { Private = -1, CombinedSessionState = 0, From 179f4747a9c2ca24219684276e0fdb220997dbaf Mon Sep 17 00:00:00 2001 From: Wilson Giese Date: Thu, 27 Oct 2016 17:38:48 -0700 Subject: [PATCH 092/166] Add MouseEvent --- src/event.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/src/event.rs b/src/event.rs index 4254055..7e016ba 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,4 +1,5 @@ use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; +use geometry::CGPoint; use event_source::{CGEventSource,CGEventSourceRef}; use libc; @@ -9,7 +10,7 @@ pub type CGKeyCode = libc::uint16_t; /// Flags for events /// -/// [Ref] (http://opensource.apple.com//source/IOHIDFamily/IOHIDFamily-308/IOHIDSystem/IOKit/hidsystem/IOLLEvent.h) +/// [Ref](http://opensource.apple.com/source/IOHIDFamily/IOHIDFamily-700/IOHIDSystem/IOKit/hidsystem/IOLLEvent.h) #[repr(C)] #[derive(Clone, Copy, Debug)] pub enum CGEventFlags { @@ -31,6 +32,51 @@ pub enum CGEventFlags { NonCoalesced = 0x00000100, } +/// Constants that specify the different types of input events. +/// +/// [Ref](http://opensource.apple.com/source/IOHIDFamily/IOHIDFamily-700/IOHIDSystem/IOKit/hidsystem/IOLLEvent.h) +#[repr(C)] +#[derive(Clone, Copy, Debug)] +pub enum CGEventType { + Null = 1 << 0, + + // Mouse events. + LeftMouseDown = 1 << 1, + LeftMouseUp = 1 << 2, + RightMouseDown = 1 << 3, + RightMouseUp = 1 << 4, + MouseMoved = 1 << 5, + LeftMouseDragged = 1 << 6, + RightMouseDragged = 1 << 7, + + // Keyboard events. + KeyDown = 1 << 10, + KeyUp = 1 << 11, + FlagsChanged = 1 << 12, + + // Specialized control devices. + ScrollWheel = 1 << 22, + TabletPointer = 1 << 23, + TabletProximity = 1 << 24, + OtherMouseDown = 1 << 25, + OtherMouseUp = 1 << 26, + OtherMouseDragged = 1 << 27, + + // Out of band event types. These are delivered to the event tap callback + // to notify it of unusual conditions that disable the event tap. + TapDisabledByTimeout = 0xFFFFFFFE, + TapDisabledByUserInput = 0xFFFFFFFF, +} + +// Constants that specify buttons on a one, two, or three-button mouse. +#[repr(C)] +#[derive(Clone, Copy, Debug)] +pub enum CGMouseButton { + Left, + Right, + Center, +} + /// Possible tapping points for events. #[repr(C)] #[derive(Clone, Copy, Debug)] @@ -157,6 +203,22 @@ extern { fn CGEventCreateKeyboardEvent(source: CGEventSourceRef, keycode: CGKeyCode, keydown: bool) -> CGEventRef; + /// Return a new mouse event. + /// + /// The event source may be taken from another event, or may be NULL. + /// `mouseType' should be one of the mouse event types. `mouseCursorPosition' + /// should be the position of the mouse cursor in global coordinates. + /// `mouseButton' should be the button that's changing state; `mouseButton' + /// is ignored unless `mouseType' is one of `kCGEventOtherMouseDown', + /// `kCGEventOtherMouseDragged', or `kCGEventOtherMouseUp'. + /// + /// The current implemementation of the event system supports a maximum of + /// thirty-two buttons. Mouse button 0 is the primary button on the mouse. + /// Mouse button 1 is the secondary mouse button (right). Mouse button 2 is + /// the center button, and the remaining buttons are in USB device order. + fn CGEventCreateMouseEvent(source: CGEventSourceRef, mouseType: CGEventType, + mouseCursorPosition: CGPoint, mouseButton: CGMouseButton) -> CGEventRef; + /// Post an event into the event stream at a specified location. /// /// This function posts the specified event immediately before any event taps From a70192dae8e8ec21bc00ecd6f5c497baa7e8ee9a Mon Sep 17 00:00:00 2001 From: Wilson Giese Date: Thu, 27 Oct 2016 17:54:35 -0700 Subject: [PATCH 093/166] New for keyboard & mouse events --- src/event.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/event.rs b/src/event.rs index 7e016ba..3ddac10 100644 --- a/src/event.rs +++ b/src/event.rs @@ -149,7 +149,11 @@ impl TCFType for CGEvent { } impl CGEvent { - pub fn new(source: CGEventSource, keycode: CGKeyCode, keydown: bool) -> Result { + pub fn new_keyboard_event( + source: CGEventSource, + keycode: CGKeyCode, + keydown: bool + ) -> Result { unsafe { let event_ref = CGEventCreateKeyboardEvent(source.as_concrete_TypeRef(), keycode, keydown); if event_ref != ptr::null() { @@ -160,6 +164,23 @@ impl CGEvent { } } + pub fn new_mouse_event( + source: CGEventSource, + mouseType: CGEventType, + mouseCursorPosition: CGPoint, + mouseButton: CGMouseButton + ) -> Result { + unsafe { + let event_ref = CGEventCreateMouseEvent(source.as_concrete_TypeRef(), mouseType, + mouseCursorPosition, mouseButton); + if event_ref != ptr::null() { + Ok(TCFType::wrap_under_create_rule(event_ref)) + } else { + Err(()) + } + } + } + pub fn post(&self, tapLocation: CGEventTapLocation) { unsafe { CGEventPost(tapLocation, self.as_concrete_TypeRef()); From b88ff77b0dfa07617110b45d692225c8fdcae844 Mon Sep 17 00:00:00 2001 From: Wilson Giese Date: Fri, 28 Oct 2016 13:11:11 -0700 Subject: [PATCH 094/166] Style --- src/event.rs | 20 ++++++++++---------- src/event_source.rs | 5 ++--- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/event.rs b/src/event.rs index 3ddac10..6f4ed71 100644 --- a/src/event.rs +++ b/src/event.rs @@ -81,9 +81,9 @@ pub enum CGMouseButton { #[repr(C)] #[derive(Clone, Copy, Debug)] pub enum CGEventTapLocation { - HIDEventTap, - SessionEventTap, - AnnotatedSessionEventTap, + HID, + Session, + AnnotatedSession, } #[repr(C)] @@ -166,13 +166,13 @@ impl CGEvent { pub fn new_mouse_event( source: CGEventSource, - mouseType: CGEventType, - mouseCursorPosition: CGPoint, - mouseButton: CGMouseButton + mouse_type: CGEventType, + mouse_cursor_position: CGPoint, + mouse_button: CGMouseButton ) -> Result { unsafe { - let event_ref = CGEventCreateMouseEvent(source.as_concrete_TypeRef(), mouseType, - mouseCursorPosition, mouseButton); + let event_ref = CGEventCreateMouseEvent(source.as_concrete_TypeRef(), mouse_type, + mouse_cursor_position, mouse_button); if event_ref != ptr::null() { Ok(TCFType::wrap_under_create_rule(event_ref)) } else { @@ -181,9 +181,9 @@ impl CGEvent { } } - pub fn post(&self, tapLocation: CGEventTapLocation) { + pub fn post(&self, tap_location: CGEventTapLocation) { unsafe { - CGEventPost(tapLocation, self.as_concrete_TypeRef()); + CGEventPost(tap_location, self.as_concrete_TypeRef()); } } diff --git a/src/event_source.rs b/src/event_source.rs index 16fa86b..32bbaef 100644 --- a/src/event_source.rs +++ b/src/event_source.rs @@ -1,6 +1,5 @@ use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; -use libc; use std::mem; use std::ptr; @@ -76,9 +75,9 @@ impl TCFType for CGEventSource { } impl CGEventSource { - pub fn new(stateID: CGEventSourceStateID) -> Result { + pub fn new(state_id: CGEventSourceStateID) -> Result { unsafe { - let event_source_ref = CGEventSourceCreate(stateID); + let event_source_ref = CGEventSourceCreate(state_id); if event_source_ref != ptr::null() { Ok(TCFType::wrap_under_create_rule(event_source_ref)) } else { From f9a86ff46cc0ceac95421a6a0dece71f9d5db08d Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Sat, 29 Oct 2016 13:44:48 -0400 Subject: [PATCH 095/166] Add support for CGContextSetTextDrawingMode --- src/context.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/context.rs b/src/context.rs index 9cdd522..0fcb5e9 100644 --- a/src/context.rs +++ b/src/context.rs @@ -15,6 +15,17 @@ use std::mem; use std::ptr; use std::slice; +#[repr(C)] +pub enum CGTextDrawingMode { + CGTextFill, + CGTextStroke, + CGTextFillStroke, + CGTextInvisible, + CGTextFillClip, + CGTextStrokeClip, + CGTextClip +} + #[repr(C)] pub struct __CGContext; @@ -149,6 +160,12 @@ impl CGContext { CGContextSetShouldAntialias(self.as_concrete_TypeRef(), should_antialias) } } + + pub fn set_text_drawing_mode(&self, mode: CGTextDrawingMode) { + unsafe { + CGContextSetTextDrawingMode(self.as_concrete_TypeRef(), mode) + } + } } #[link(name = "ApplicationServices", kind = "framework")] @@ -170,6 +187,7 @@ extern { fn CGContextSetShouldSmoothFonts(c: CGContextRef, shouldSmoothFonts: bool); fn CGContextSetAllowsAntialiasing(c: CGContextRef, allowsAntialiasing: bool); fn CGContextSetShouldAntialias(c: CGContextRef, shouldAntialias: bool); + fn CGContextSetTextDrawingMode(c: CGContextRef, mode: CGTextDrawingMode); fn CGContextSetRGBFillColor(context: CGContextRef, red: CGFloat, green: CGFloat, From 005b64013ee2907b8a62bc77111c5229cb53fbea Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Sat, 29 Oct 2016 14:33:18 -0400 Subject: [PATCH 096/166] Add kCGBitmapByteOrder constants --- src/base.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/base.rs b/src/base.rs index 6c7c33d..9cfb0a9 100644 --- a/src/base.rs +++ b/src/base.rs @@ -38,3 +38,18 @@ pub const kCGImageAlphaFirst: u32 = 4; pub const kCGImageAlphaNoneSkipLast: u32 = 5; pub const kCGImageAlphaNoneSkipFirst: u32 = 6; +pub const kCGBitmapByteOrderDefault: u32 = (0 << 12); +pub const kCGBitmapByteOrder16Little: u32 = (1 << 12); +pub const kCGBitmapByteOrder32Little: u32 = (2 << 12); +pub const kCGBitmapByteOrder16Big: u32 = (3 << 12); +pub const kCGBitmapByteOrder32Big: u32 = (4 << 12); + +#[cfg(target_endian = "big")] +pub const kCGBitmapByteOrder16Host: u32 = kCGBitmapByteOrder16Big; +#[cfg(target_endian = "big")] +pub const kCGBitmapByteOrder32Host: u32 = kCGBitmapByteOrder32Big; + +#[cfg(target_endian = "little")] +pub const kCGBitmapByteOrder16Host: u32 = kCGBitmapByteOrder16Little; +#[cfg(target_endian = "little")] +pub const kCGBitmapByteOrder32Host: u32 = kCGBitmapByteOrder32Little; From a7056be382b0545e6a7f47490512c196e8684069 Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Mon, 31 Oct 2016 13:30:32 -0400 Subject: [PATCH 097/166] Add functions for controlling subpixel positioning --- src/context.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/context.rs b/src/context.rs index 0fcb5e9..0854e3d 100644 --- a/src/context.rs +++ b/src/context.rs @@ -161,6 +161,30 @@ impl CGContext { } } + pub fn set_allows_font_subpixel_quantization(&self, allows_font_subpixel_quantization: bool) { + unsafe { + CGContextSetAllowsFontSubpixelQuantization(self.as_concrete_TypeRef(), allows_font_subpixel_quantization) + } + } + + pub fn set_should_subpixel_quantize_fonts(&self, should_subpixel_quantize_fonts: bool) { + unsafe { + CGContextSetShouldSubpixelQuantizeFonts(self.as_concrete_TypeRef(), should_subpixel_quantize_fonts) + } + } + + pub fn set_allows_font_subpixel_positioning(&self, allows_font_subpixel_positioning: bool) { + unsafe { + CGContextSetAllowsFontSubpixelPositioning(self.as_concrete_TypeRef(), allows_font_subpixel_positioning) + } + } + + pub fn set_should_subpixel_position_fonts(&self, should_subpixel_position_fonts: bool) { + unsafe { + CGContextSetShouldSubpixelPositionFonts(self.as_concrete_TypeRef(), should_subpixel_position_fonts) + } + } + pub fn set_text_drawing_mode(&self, mode: CGTextDrawingMode) { unsafe { CGContextSetTextDrawingMode(self.as_concrete_TypeRef(), mode) @@ -187,6 +211,14 @@ extern { fn CGContextSetShouldSmoothFonts(c: CGContextRef, shouldSmoothFonts: bool); fn CGContextSetAllowsAntialiasing(c: CGContextRef, allowsAntialiasing: bool); fn CGContextSetShouldAntialias(c: CGContextRef, shouldAntialias: bool); + fn CGContextSetAllowsFontSubpixelQuantization(c: CGContextRef, + allowsFontSubpixelQuantization: bool); + fn CGContextSetShouldSubpixelQuantizeFonts(c: CGContextRef, + shouldSubpixelQuantizeFonts: bool); + fn CGContextSetAllowsFontSubpixelPositioning(c: CGContextRef, + allowsFontSubpixelPositioning: bool); + fn CGContextSetShouldSubpixelPositionFonts(c: CGContextRef, + shouldSubpixelPositionFonts: bool); fn CGContextSetTextDrawingMode(c: CGContextRef, mode: CGTextDrawingMode); fn CGContextSetRGBFillColor(context: CGContextRef, red: CGFloat, From e22e6f61fcaca5598de795adecafbdc8993c0874 Mon Sep 17 00:00:00 2001 From: Glenn Watson Date: Mon, 7 Nov 2016 10:33:55 +1000 Subject: [PATCH 098/166] Bump version. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3381499..fe3186f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.4.1" +version = "0.4.2" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" From 8079a2f9058d55c85484f04c7dc1de3d73aa2e93 Mon Sep 17 00:00:00 2001 From: Michael Sanders Date: Fri, 30 Dec 2016 22:21:03 -0800 Subject: [PATCH 099/166] Add support for CGEvent::new & CGEventGetLocation --- src/event.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/event.rs b/src/event.rs index 6f4ed71..b193426 100644 --- a/src/event.rs +++ b/src/event.rs @@ -149,6 +149,17 @@ impl TCFType for CGEvent { } impl CGEvent { + pub fn new(source: CGEventSource) -> Result { + unsafe { + let event_ref = CGEventCreate(source.as_concrete_TypeRef()); + if event_ref != ptr::null() { + Ok(TCFType::wrap_under_create_rule(event_ref)) + } else { + Err(()) + } + } + } + pub fn new_keyboard_event( source: CGEventSource, keycode: CGKeyCode, @@ -187,6 +198,12 @@ impl CGEvent { } } + pub fn location(&self) -> CGPoint { + unsafe { + CGEventGetLocation(self.as_concrete_TypeRef()) + } + } + pub fn post_to_pid(&self, pid: libc::pid_t) { unsafe { CGEventPostToPid(pid, self.as_concrete_TypeRef()); @@ -211,6 +228,10 @@ extern { /// Return the type identifier for the opaque type `CGEventRef'. fn CGEventGetTypeID() -> CFTypeID; + /// Return a new event using the event source `source'. If `source' is NULL, + /// the default source is used. + fn CGEventCreate(source: CGEventSourceRef) -> CGEventRef; + /// Return a new keyboard event. /// /// The event source may be taken from another event, or may be NULL. Based @@ -255,4 +276,8 @@ extern { /// Return the event flags of an event. fn CGEventGetFlags(event: CGEventRef) -> CGEventFlags; + + /// Return the location of an event in global display coordinates. + /// CGPointZero is returned if event is not a valid CGEventRef. + fn CGEventGetLocation(event: CGEventRef) -> CGPoint; } From b459976fc82cafbfded2a17a6162ad7a3815db4d Mon Sep 17 00:00:00 2001 From: Mason Chang Date: Mon, 9 Jan 2017 17:18:21 -0800 Subject: [PATCH 100/166] Add CGContextFillRect to CGContext --- Cargo.toml | 2 +- src/context.rs | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index fe3186f..fef99bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.4.2" +version = "0.4.3" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" diff --git a/src/context.rs b/src/context.rs index 0854e3d..ffb8a16 100644 --- a/src/context.rs +++ b/src/context.rs @@ -14,6 +14,7 @@ use libc::{c_void, size_t}; use std::mem; use std::ptr; use std::slice; +use geometry::CGRect; #[repr(C)] pub enum CGTextDrawingMode { @@ -190,6 +191,12 @@ impl CGContext { CGContextSetTextDrawingMode(self.as_concrete_TypeRef(), mode) } } + + pub fn fill_rect(&self, rect: CGRect) { + unsafe { + CGContextFillRect(self.as_concrete_TypeRef(), rect) + } + } } #[link(name = "ApplicationServices", kind = "framework")] @@ -225,5 +232,7 @@ extern { green: CGFloat, blue: CGFloat, alpha: CGFloat); + fn CGContextFillRect(context: CGContextRef, + rect: CGRect); } From 0b08242836fc15531d9b0aeb952640caf21ea967 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Fri, 13 Jan 2017 11:01:45 -0500 Subject: [PATCH 101/166] Hide features from macOS 10.11 behind a feature. --- Cargo.toml | 4 ++++ src/event.rs | 2 ++ 2 files changed, 6 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index fef99bb..1a9f7c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,10 @@ version = "0.4.3" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" +[features] +default = [] +elcapitan = [] + [dependencies] libc = "0.2" core-foundation = "0.2" diff --git a/src/event.rs b/src/event.rs index b193426..f68b8a7 100644 --- a/src/event.rs +++ b/src/event.rs @@ -204,6 +204,7 @@ impl CGEvent { } } + #[cfg(feature = "elcapitan")] pub fn post_to_pid(&self, pid: libc::pid_t) { unsafe { CGEventPostToPid(pid, self.as_concrete_TypeRef()); @@ -268,6 +269,7 @@ extern { /// taps. fn CGEventPost(tapLocation: CGEventTapLocation, event: CGEventRef); + #[cfg(feature = "elcapitan")] /// Post an event to a specified process ID fn CGEventPostToPid(pid: libc::pid_t, event: CGEventRef); From 495c81d35fb4a4f15133c347a30831438d21dd18 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Fri, 13 Jan 2017 11:02:24 -0500 Subject: [PATCH 102/166] Breaking change version bump. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 1a9f7c9..18d4f61 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.4.3" +version = "0.5.0" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" From 5afc7df3af1bed1c08eee6507799d65ecfdf5ac7 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Fri, 13 Jan 2017 11:03:48 -0500 Subject: [PATCH 103/166] Test with features and without. --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index 19ef02b..f741543 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,5 +5,9 @@ rust: - beta - stable +script: + - cargo test + - cargo test --features="elcapitan" + notifications: webhooks: http://build.servo.org:54856/travis From d796d9fae23578be3dc7e196a5113a0ec82cebaa Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Thu, 26 Jan 2017 11:23:07 -0500 Subject: [PATCH 104/166] Update to core-foundation 0.3 --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 18d4f61..4a8508c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.5.0" +version = "0.6.0" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" @@ -13,5 +13,5 @@ elcapitan = [] [dependencies] libc = "0.2" -core-foundation = "0.2" +core-foundation = "0.3" serde = "0.8" From 5df4a8f58e1fad1463859ca9752d8deee202b5b9 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Wed, 1 Feb 2017 13:37:43 +0100 Subject: [PATCH 105/166] Update serde to 0.9 --- Cargo.toml | 4 ++-- src/font.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4a8508c..2604b3f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.6.0" +version = "0.7.0" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" @@ -14,4 +14,4 @@ elcapitan = [] [dependencies] libc = "0.2" core-foundation = "0.3" -serde = "0.8" +serde = "0.9" diff --git a/src/font.rs b/src/font.rs index e3e2700..58b3630 100644 --- a/src/font.rs +++ b/src/font.rs @@ -32,17 +32,17 @@ unsafe impl Send for CGFont {} unsafe impl Sync for CGFont {} impl Serialize for CGFont { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer { + fn serialize(&self, serializer: S) -> Result where S: Serializer { let postscript_name = self.postscript_name().to_string(); postscript_name.serialize(serializer) } } impl Deserialize for CGFont { - fn deserialize(deserializer: &mut D) -> Result where D: Deserializer { + fn deserialize(deserializer: D) -> Result where D: Deserializer { let postscript_name: String = try!(Deserialize::deserialize(deserializer)); CGFont::from_name(&CFString::new(&*postscript_name)).map_err(|_| { - de::Error::invalid_value("Couldn't find a font with that PostScript name!") + de::Error::custom("Couldn't find a font with that PostScript name!") }) } } From 7687a5ed9c27411ed17eea84115e2a6500f669bb Mon Sep 17 00:00:00 2001 From: pythoneer Date: Thu, 2 Feb 2017 21:38:07 +0100 Subject: [PATCH 106/166] Fixed CGEventType in event.rs changed the enum declaration from bit shifts to one without, like in the original CGEventTypes.h --- src/event.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/event.rs b/src/event.rs index f68b8a7..6dc6b73 100644 --- a/src/event.rs +++ b/src/event.rs @@ -38,29 +38,29 @@ pub enum CGEventFlags { #[repr(C)] #[derive(Clone, Copy, Debug)] pub enum CGEventType { - Null = 1 << 0, + Null = 0, // Mouse events. - LeftMouseDown = 1 << 1, - LeftMouseUp = 1 << 2, - RightMouseDown = 1 << 3, - RightMouseUp = 1 << 4, - MouseMoved = 1 << 5, - LeftMouseDragged = 1 << 6, - RightMouseDragged = 1 << 7, + LeftMouseDown = 1, + LeftMouseUp = 2, + RightMouseDown = 3, + RightMouseUp = 4, + MouseMoved = 5, + LeftMouseDragged = 6, + RightMouseDragged = 7, // Keyboard events. - KeyDown = 1 << 10, - KeyUp = 1 << 11, - FlagsChanged = 1 << 12, + KeyDown = 10, + KeyUp = 11, + FlagsChanged = 12, // Specialized control devices. - ScrollWheel = 1 << 22, - TabletPointer = 1 << 23, - TabletProximity = 1 << 24, - OtherMouseDown = 1 << 25, - OtherMouseUp = 1 << 26, - OtherMouseDragged = 1 << 27, + ScrollWheel = 22, + TabletPointer = 23, + TabletProximity = 24, + OtherMouseDown = 25, + OtherMouseUp = 26, + OtherMouseDragged = 27, // Out of band event types. These are delivered to the event tap callback // to notify it of unusual conditions that disable the event tap. From b35e81854441636ebaf7c61869863888bd1ce3ba Mon Sep 17 00:00:00 2001 From: Michael Sanders Date: Sun, 12 Feb 2017 09:55:17 -0800 Subject: [PATCH 107/166] Allow applying multiple bitmask flags to events --- src/event.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/event.rs b/src/event.rs index 6dc6b73..8886441 100644 --- a/src/event.rs +++ b/src/event.rs @@ -7,6 +7,7 @@ use std::mem; use std::ptr; pub type CGKeyCode = libc::uint16_t; +pub type CGEventFlagMask = libc::uint64_t; /// Flags for events /// @@ -211,13 +212,13 @@ impl CGEvent { } } - pub fn set_flags(&self, flags: CGEventFlags) { + pub fn set_flags(&self, flags: CGEventFlagMask) { unsafe { CGEventSetFlags(self.as_concrete_TypeRef(), flags); } } - pub fn get_flags(&self) -> CGEventFlags { + pub fn get_flags(&self) -> CGEventFlagMask { unsafe { CGEventGetFlags(self.as_concrete_TypeRef()) } @@ -274,10 +275,10 @@ extern { fn CGEventPostToPid(pid: libc::pid_t, event: CGEventRef); /// Set the event flags of an event. - fn CGEventSetFlags(event: CGEventRef, flags: CGEventFlags); + fn CGEventSetFlags(event: CGEventRef, flags: CGEventFlagMask); /// Return the event flags of an event. - fn CGEventGetFlags(event: CGEventRef) -> CGEventFlags; + fn CGEventGetFlags(event: CGEventRef) -> CGEventFlagMask; /// Return the location of an event in global display coordinates. /// CGPointZero is returned if event is not a valid CGEventRef. From 34d678356bc7ddc031809cc9d8db115753850042 Mon Sep 17 00:00:00 2001 From: Michael Sanders Date: Mon, 20 Feb 2017 16:48:49 -0800 Subject: [PATCH 108/166] Use bitflags crate for CGEventFlags --- Cargo.toml | 1 + src/event.rs | 43 ++++++++++++++++++++++--------------------- src/lib.rs | 3 +++ 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2604b3f..596cbe9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,4 @@ elcapitan = [] libc = "0.2" core-foundation = "0.3" serde = "0.9" +bitflags = "0.8" diff --git a/src/event.rs b/src/event.rs index 8886441..f13e331 100644 --- a/src/event.rs +++ b/src/event.rs @@ -7,30 +7,31 @@ use std::mem; use std::ptr; pub type CGKeyCode = libc::uint16_t; -pub type CGEventFlagMask = libc::uint64_t; /// Flags for events /// /// [Ref](http://opensource.apple.com/source/IOHIDFamily/IOHIDFamily-700/IOHIDSystem/IOKit/hidsystem/IOLLEvent.h) -#[repr(C)] -#[derive(Clone, Copy, Debug)] -pub enum CGEventFlags { - // Device-independent modifier key bits. - AlphaShift = 0x00010000, - Shift = 0x00020000, - Control = 0x00040000, - Alternate = 0x00080000, - Command = 0x00100000, +bitflags! { + pub flags CGEventFlags: u64 { + const Null = 0, - // Special key identifiers. - Help = 0x00400000, - SecondaryFn = 0x00800000, + // Device-independent modifier key bits. + const AlphaShift = 0x00010000, + const Shift = 0x00020000, + const Control = 0x00040000, + const Alternate = 0x00080000, + const Command = 0x00100000, - // Identifies key events from numeric keypad area on extended keyboards. - NumericPad = 0x00200000, + // Special key identifiers. + const Help = 0x00400000, + const SecondaryFn = 0x00800000, - // Indicates if mouse/pen movement events are not being coalesced - NonCoalesced = 0x00000100, + // Identifies key events from numeric keypad area on extended keyboards. + const NumericPad = 0x00200000, + + // Indicates if mouse/pen movement events are not being coalesced + const NonCoalesced = 0x00000100, + } } /// Constants that specify the different types of input events. @@ -212,13 +213,13 @@ impl CGEvent { } } - pub fn set_flags(&self, flags: CGEventFlagMask) { + pub fn set_flags(&self, flags: CGEventFlags) { unsafe { CGEventSetFlags(self.as_concrete_TypeRef(), flags); } } - pub fn get_flags(&self) -> CGEventFlagMask { + pub fn get_flags(&self) -> CGEventFlags { unsafe { CGEventGetFlags(self.as_concrete_TypeRef()) } @@ -275,10 +276,10 @@ extern { fn CGEventPostToPid(pid: libc::pid_t, event: CGEventRef); /// Set the event flags of an event. - fn CGEventSetFlags(event: CGEventRef, flags: CGEventFlagMask); + fn CGEventSetFlags(event: CGEventRef, flags: CGEventFlags); /// Return the event flags of an event. - fn CGEventGetFlags(event: CGEventRef) -> CGEventFlagMask; + fn CGEventGetFlags(event: CGEventRef) -> CGEventFlags; /// Return the location of an event in global display coordinates. /// CGPointZero is returned if event is not a valid CGEventRef. diff --git a/src/lib.rs b/src/lib.rs index fed16b2..9b3f78e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,9 @@ extern crate libc; extern crate core_foundation; extern crate serde; +#[macro_use] +extern crate bitflags; + pub mod base; pub mod color_space; pub mod context; From a7c53a2bf981c78fa1b6abb947b3feef99ff4e3b Mon Sep 17 00:00:00 2001 From: Michael Sanders Date: Wed, 22 Feb 2017 20:26:47 -0800 Subject: [PATCH 109/166] Add prefix to bitflags Necessary until rust-lang-nursery/bitflags/issues/21 is implemented. --- src/event.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/event.rs b/src/event.rs index f13e331..72b511d 100644 --- a/src/event.rs +++ b/src/event.rs @@ -13,24 +13,24 @@ pub type CGKeyCode = libc::uint16_t; /// [Ref](http://opensource.apple.com/source/IOHIDFamily/IOHIDFamily-700/IOHIDSystem/IOKit/hidsystem/IOLLEvent.h) bitflags! { pub flags CGEventFlags: u64 { - const Null = 0, + const CGEventFlagNull = 0, // Device-independent modifier key bits. - const AlphaShift = 0x00010000, - const Shift = 0x00020000, - const Control = 0x00040000, - const Alternate = 0x00080000, - const Command = 0x00100000, + const CGEventFlagAlphaShift = 0x00010000, + const CGEventFlagShift = 0x00020000, + const CGEventFlagControl = 0x00040000, + const CGEventFlagAlternate = 0x00080000, + const CGEventFlagCommand = 0x00100000, // Special key identifiers. - const Help = 0x00400000, - const SecondaryFn = 0x00800000, + const CGEventFlagHelp = 0x00400000, + const CGEventFlagSecondaryFn = 0x00800000, // Identifies key events from numeric keypad area on extended keyboards. - const NumericPad = 0x00200000, + const CGEventFlagNumericPad = 0x00200000, // Indicates if mouse/pen movement events are not being coalesced - const NonCoalesced = 0x00000100, + const CGEventFlagNonCoalesced = 0x00000100, } } From d15fa934d33c1c5ffc12ea83f27d439cbbaa306a Mon Sep 17 00:00:00 2001 From: Michael Sanders Date: Sun, 12 Feb 2017 00:10:31 -0800 Subject: [PATCH 110/166] Add support for CGEventSetType --- src/event.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/event.rs b/src/event.rs index 6dc6b73..09a3ed2 100644 --- a/src/event.rs +++ b/src/event.rs @@ -222,6 +222,18 @@ impl CGEvent { CGEventGetFlags(self.as_concrete_TypeRef()) } } + + pub fn set_type(&self, event_type: CGEventType) { + unsafe { + CGEventSetType(self.as_concrete_TypeRef(), event_type); + } + } + + pub fn get_type(&self) -> CGEventType { + unsafe { + CGEventGetType(self.as_concrete_TypeRef()) + } + } } #[link(name = "ApplicationServices", kind = "framework")] @@ -282,4 +294,10 @@ extern { /// Return the location of an event in global display coordinates. /// CGPointZero is returned if event is not a valid CGEventRef. fn CGEventGetLocation(event: CGEventRef) -> CGPoint; + + /// Set the event type of an event. + fn CGEventSetType(event: CGEventRef, eventType: CGEventType); + + /// Return the event type of an event (left mouse down, for example). + fn CGEventGetType(event: CGEventRef) -> CGEventType; } From 34659f9ae20c7cce8a029dd94bc761d91981f554 Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Wed, 15 Mar 2017 11:50:43 -0400 Subject: [PATCH 111/166] Fix callback types --- src/data_provider.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/data_provider.rs b/src/data_provider.rs index fe449a2..10d9cb1 100644 --- a/src/data_provider.rs +++ b/src/data_provider.rs @@ -9,21 +9,21 @@ use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; -use libc::{c_void, size_t}; +use libc::{c_void, size_t, off_t}; use std::mem; use std::ptr; -pub type CGDataProviderGetBytesCallback = *const u8; -pub type CGDataProviderReleaseInfoCallback = *const u8; -pub type CGDataProviderRewindCallback = *const u8; -pub type CGDataProviderSkipBytesCallback = *const u8; -pub type CGDataProviderSkipForwardCallback = *const u8; +pub type CGDataProviderGetBytesCallback = Option size_t>; +pub type CGDataProviderReleaseInfoCallback = Option; +pub type CGDataProviderRewindCallback = Option; +pub type CGDataProviderSkipBytesCallback = Option; +pub type CGDataProviderSkipForwardCallback = Option off_t>; -pub type CGDataProviderGetBytePointerCallback = *const u8; -pub type CGDataProviderGetBytesAtOffsetCallback = *const u8; -pub type CGDataProviderReleaseBytePointerCallback = *const u8; -pub type CGDataProviderReleaseDataCallback = *const u8; -pub type CGDataProviderGetBytesAtPositionCallback = *const u8; +pub type CGDataProviderGetBytePointerCallback = Option *mut c_void>; +pub type CGDataProviderGetBytesAtOffsetCallback = Option; +pub type CGDataProviderReleaseBytePointerCallback = Option; +pub type CGDataProviderReleaseDataCallback = Option; +pub type CGDataProviderGetBytesAtPositionCallback = Option; #[repr(C)] pub struct __CGDataProvider; @@ -82,7 +82,7 @@ impl CGDataProvider { let result = CGDataProviderCreateWithData(ptr::null_mut(), buffer.as_ptr() as *const c_void, buffer.len() as size_t, - ptr::null()); + None); TCFType::wrap_under_create_rule(result) } } From 52952115c1ddb103ebe5986de7ba5f39709fbfe0 Mon Sep 17 00:00:00 2001 From: Michael Sanders Date: Wed, 4 Jan 2017 08:38:34 -0800 Subject: [PATCH 112/166] Add support for CGEventKeyboardSetUnicodeString --- src/event.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/event.rs b/src/event.rs index 3ddf9c7..f3361e7 100644 --- a/src/event.rs +++ b/src/event.rs @@ -236,6 +236,18 @@ impl CGEvent { CGEventGetType(self.as_concrete_TypeRef()) } } + + pub fn set_string_from_utf16(&self, buf: &[u16]) { + let buflen = buf.len() as libc::c_ulong; + unsafe { + CGEventKeyboardSetUnicodeString(self.as_concrete_TypeRef(), buflen, buf.as_ptr()); + } + } + + pub fn set_string(&self, string: &str) { + let buf: Vec = string.encode_utf16().collect(); + self.set_string_from_utf16(&buf); + } } #[link(name = "ApplicationServices", kind = "framework")] @@ -302,4 +314,16 @@ extern { /// Return the event type of an event (left mouse down, for example). fn CGEventGetType(event: CGEventRef) -> CGEventType; + + /// Set the Unicode string associated with a keyboard event. + /// + /// By default, the system translates the virtual key code in a keyboard + /// event into a Unicode string based on the keyboard ID in the event + /// source. This function allows you to manually override this string. + /// Note that application frameworks may ignore the Unicode string in a + /// keyboard event and do their own translation based on the virtual + /// keycode and perceived event state. + fn CGEventKeyboardSetUnicodeString(event: CGEventRef, + length: libc::c_ulong, + string: *const u16); } From 65dd626c1c1a61367ed2ca5bdd3f55554d73368e Mon Sep 17 00:00:00 2001 From: Michael Sanders Date: Mon, 20 Feb 2017 08:28:58 -0800 Subject: [PATCH 113/166] Rename to set_string_from_utf16_unchecked --- src/event.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/event.rs b/src/event.rs index f3361e7..c6ea59d 100644 --- a/src/event.rs +++ b/src/event.rs @@ -237,7 +237,7 @@ impl CGEvent { } } - pub fn set_string_from_utf16(&self, buf: &[u16]) { + pub fn set_string_from_utf16_unchecked(&self, buf: &[u16]) { let buflen = buf.len() as libc::c_ulong; unsafe { CGEventKeyboardSetUnicodeString(self.as_concrete_TypeRef(), buflen, buf.as_ptr()); @@ -246,7 +246,7 @@ impl CGEvent { pub fn set_string(&self, string: &str) { let buf: Vec = string.encode_utf16().collect(); - self.set_string_from_utf16(&buf); + self.set_string_from_utf16_unchecked(&buf); } } From 8daf52206a911d3a845820768cb010d79ecb5258 Mon Sep 17 00:00:00 2001 From: Michael Sanders Date: Mon, 30 Jan 2017 10:42:11 +0100 Subject: [PATCH 114/166] Add CGImage support --- src/context.rs | 8 +++ src/image.rs | 133 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 3 files changed, 142 insertions(+) create mode 100644 src/image.rs diff --git a/src/context.rs b/src/context.rs index ffb8a16..43ee619 100644 --- a/src/context.rs +++ b/src/context.rs @@ -10,6 +10,7 @@ use base::CGFloat; use color_space::{CGColorSpace, CGColorSpaceRef}; use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; +use image::{CGImage,CGImageRef}; use libc::{c_void, size_t}; use std::mem; use std::ptr; @@ -197,6 +198,12 @@ impl CGContext { CGContextFillRect(self.as_concrete_TypeRef(), rect) } } + + pub fn draw_image(&self, rect: CGRect, image: CGImage) { + unsafe { + CGContextDrawImage(self.as_concrete_TypeRef(), rect, image.as_concrete_TypeRef()); + } + } } #[link(name = "ApplicationServices", kind = "framework")] @@ -234,5 +241,6 @@ extern { alpha: CGFloat); fn CGContextFillRect(context: CGContextRef, rect: CGRect); + fn CGContextDrawImage(context: CGContextRef, rect: CGRect, image: CGImageRef); } diff --git a/src/image.rs b/src/image.rs new file mode 100644 index 0000000..0e1e988 --- /dev/null +++ b/src/image.rs @@ -0,0 +1,133 @@ +use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; +use color_space::{CGColorSpace, CGColorSpaceRef}; +use libc::{size_t}; +use std::mem; + +#[repr(C)] +pub enum CGImageAlphaInfo { + CGImageAlphaNone, /* For example, RGB. */ + CGImageAlphaPremultipliedLast, /* For example, premultiplied RGBA */ + CGImageAlphaPremultipliedFirst, /* For example, premultiplied ARGB */ + CGImageAlphaLast, /* For example, non-premultiplied RGBA */ + CGImageAlphaFirst, /* For example, non-premultiplied ARGB */ + CGImageAlphaNoneSkipLast, /* For example, RBGX. */ + CGImageAlphaNoneSkipFirst, /* For example, XRBG. */ + CGImageAlphaOnly /* No color data, alpha data only */ +} + +#[repr(C)] +pub enum CGImageByteOrderInfo { + CGImageByteOrderMask = 0x7000, + CGImageByteOrder16Little = (1 << 12), + CGImageByteOrder32Little = (2 << 12), + CGImageByteOrder16Big = (3 << 12), + CGImageByteOrder32Big = (4 << 12) +} + +#[repr(C)] +pub struct __CGImage; + +pub type CGImageRef = *const __CGImage; + +pub struct CGImage { + obj: CGImageRef, +} + +impl Drop for CGImage { + fn drop(&mut self) { + unsafe { + CFRelease(self.as_CFTypeRef()) + } + } +} + +impl Clone for CGImage { + fn clone(&self) -> CGImage { + unsafe { + TCFType::wrap_under_get_rule(self.as_concrete_TypeRef()) + } + } +} + +impl TCFType for CGImage { + #[inline] + fn as_concrete_TypeRef(&self) -> CGImageRef { + self.obj + } + + #[inline] + unsafe fn wrap_under_get_rule(reference: CGImageRef) -> CGImage { + let reference: CGImageRef = mem::transmute(CFRetain(mem::transmute(reference))); + TCFType::wrap_under_create_rule(reference) + } + + #[inline] + fn as_CFTypeRef(&self) -> CFTypeRef { + unsafe { + mem::transmute(self.as_concrete_TypeRef()) + } + } + + #[inline] + unsafe fn wrap_under_create_rule(obj: CGImageRef) -> CGImage { + CGImage { + obj: obj, + } + } + + #[inline] + fn type_id() -> CFTypeID { + unsafe { + CGImageGetTypeID() + } + } +} + +impl CGImage { + pub fn width(&self) -> size_t { + unsafe { + CGImageGetWidth(self.as_concrete_TypeRef()) + } + } + + pub fn height(&self) -> size_t { + unsafe { + CGImageGetHeight(self.as_concrete_TypeRef()) + } + } + + pub fn bits_per_component(&self) -> size_t { + unsafe { + CGImageGetBitsPerComponent(self.as_concrete_TypeRef()) + } + } + + pub fn bits_per_pixel(&self) -> size_t { + unsafe { + CGImageGetBitsPerPixel(self.as_concrete_TypeRef()) + } + } + + pub fn bytes_per_row(&self) -> size_t { + unsafe { + CGImageGetBytesPerRow(self.as_concrete_TypeRef()) + } + } + + pub fn color_space(&self) -> CGColorSpace { + unsafe { + TCFType::wrap_under_get_rule(CGImageGetColorSpace(self.as_concrete_TypeRef())) + } + } +} + +#[link(name = "ApplicationServices", kind = "framework")] +extern { + fn CGImageGetTypeID() -> CFTypeID; + fn CGImageGetWidth(image: CGImageRef) -> size_t; + fn CGImageGetHeight(image: CGImageRef) -> size_t; + fn CGImageGetBitsPerComponent(image: CGImageRef) -> size_t; + fn CGImageGetBitsPerPixel(image: CGImageRef) -> size_t; + fn CGImageGetBytesPerRow(image: CGImageRef) -> size_t; + fn CGImageGetColorSpace(image: CGImageRef) -> CGColorSpaceRef; +} diff --git a/src/lib.rs b/src/lib.rs index 9b3f78e..5c0a59a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,4 +23,5 @@ pub mod event; pub mod event_source; pub mod font; pub mod geometry; +pub mod image; pub mod private; From d503560bb1cea03753146f1ce14dfa41be2ce27d Mon Sep 17 00:00:00 2001 From: alfredoyang Date: Tue, 11 Apr 2017 15:46:58 +0800 Subject: [PATCH 115/166] Remove serde crate --- Cargo.toml | 1 - src/font.rs | 18 ------------------ src/lib.rs | 1 - 3 files changed, 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 596cbe9..c4ef0bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,5 +14,4 @@ elcapitan = [] [dependencies] libc = "0.2" core-foundation = "0.3" -serde = "0.9" bitflags = "0.8" diff --git a/src/font.rs b/src/font.rs index 58b3630..3c1c390 100644 --- a/src/font.rs +++ b/src/font.rs @@ -12,8 +12,6 @@ use core_foundation::string::{CFString, CFStringRef}; use data_provider::{CGDataProvider, CGDataProviderRef}; use libc; -use serde::de::{self, Deserialize, Deserializer}; -use serde::ser::{Serialize, Serializer}; use std::mem; use std::ptr; @@ -31,22 +29,6 @@ pub struct CGFont { unsafe impl Send for CGFont {} unsafe impl Sync for CGFont {} -impl Serialize for CGFont { - fn serialize(&self, serializer: S) -> Result where S: Serializer { - let postscript_name = self.postscript_name().to_string(); - postscript_name.serialize(serializer) - } -} - -impl Deserialize for CGFont { - fn deserialize(deserializer: D) -> Result where D: Deserializer { - let postscript_name: String = try!(Deserialize::deserialize(deserializer)); - CGFont::from_name(&CFString::new(&*postscript_name)).map_err(|_| { - de::Error::custom("Couldn't find a font with that PostScript name!") - }) - } -} - impl Clone for CGFont { #[inline] fn clone(&self) -> CGFont { diff --git a/src/lib.rs b/src/lib.rs index 5c0a59a..c0078a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,6 @@ extern crate libc; extern crate core_foundation; -extern crate serde; #[macro_use] extern crate bitflags; From 02cfd064ada605ac86e4ca2ce571a22d6eb927bb Mon Sep 17 00:00:00 2001 From: Daniel Abramov Date: Mon, 3 Apr 2017 10:49:18 +0200 Subject: [PATCH 116/166] Add major `CGImageRef` related bindings --- src/context.rs | 16 +++++++++++++--- src/data_provider.rs | 8 +++++++- src/image.rs | 34 +++++++++++++++++++++++++++++++--- src/lib.rs | 5 +++-- 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/src/context.rs b/src/context.rs index 43ee619..1788c6b 100644 --- a/src/context.rs +++ b/src/context.rs @@ -10,12 +10,12 @@ use base::CGFloat; use color_space::{CGColorSpace, CGColorSpaceRef}; use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; -use image::{CGImage,CGImageRef}; use libc::{c_void, size_t}; use std::mem; use std::ptr; use std::slice; use geometry::CGRect; +use image::{CGImage, CGImageRef}; #[repr(C)] pub enum CGTextDrawingMode { @@ -199,11 +199,20 @@ impl CGContext { } } - pub fn draw_image(&self, rect: CGRect, image: CGImage) { + pub fn draw_image(&self, rect: CGRect, image: &CGImage) { unsafe { CGContextDrawImage(self.as_concrete_TypeRef(), rect, image.as_concrete_TypeRef()); } } + + pub fn create_image(&self) -> Option { + let image = unsafe { CGBitmapContextCreateImage(self.as_concrete_TypeRef()) }; + if image != ptr::null() { + Some(unsafe { CGImage::wrap_under_create_rule(image) }) + } else { + None + } + } } #[link(name = "ApplicationServices", kind = "framework")] @@ -220,6 +229,7 @@ extern { fn CGBitmapContextGetWidth(context: CGContextRef) -> size_t; fn CGBitmapContextGetHeight(context: CGContextRef) -> size_t; fn CGBitmapContextGetBytesPerRow(context: CGContextRef) -> size_t; + fn CGBitmapContextCreateImage(context: CGContextRef) -> CGImageRef; fn CGContextGetTypeID() -> CFTypeID; fn CGContextSetAllowsFontSmoothing(c: CGContextRef, allowsFontSmoothing: bool); fn CGContextSetShouldSmoothFonts(c: CGContextRef, shouldSmoothFonts: bool); @@ -241,6 +251,6 @@ extern { alpha: CGFloat); fn CGContextFillRect(context: CGContextRef, rect: CGRect); - fn CGContextDrawImage(context: CGContextRef, rect: CGRect, image: CGImageRef); + fn CGContextDrawImage(c: CGContextRef, rect: CGRect, image: CGImageRef); } diff --git a/src/data_provider.rs b/src/data_provider.rs index 10d9cb1..aaa5f75 100644 --- a/src/data_provider.rs +++ b/src/data_provider.rs @@ -8,6 +8,7 @@ // except according to those terms. use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; +use core_foundation::data::{CFData, CFDataRef}; use libc::{c_void, size_t, off_t}; use std::mem; @@ -86,11 +87,16 @@ impl CGDataProvider { TCFType::wrap_under_create_rule(result) } } + + /// Creates a copy of the data from the underlying `CFDataProviderRef`. + pub fn copy_data(&self) -> CFData { + unsafe { CFData::wrap_under_create_rule(CGDataProviderCopyData(self.obj)) } + } } #[link(name = "ApplicationServices", kind = "framework")] extern { - //fn CGDataProviderCopyData + fn CGDataProviderCopyData(provider: CGDataProviderRef) -> CFDataRef; //fn CGDataProviderCreateDirect //fn CGDataProviderCreateSequential //fn CGDataProviderCreateWithCFData diff --git a/src/image.rs b/src/image.rs index 0e1e988..aea7742 100644 --- a/src/image.rs +++ b/src/image.rs @@ -1,6 +1,9 @@ -use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; +use core_foundation::base::{CFRetain, CFTypeID, CFTypeRef, TCFType}; +use core_foundation::data::CFData; use color_space::{CGColorSpace, CGColorSpaceRef}; -use libc::{size_t}; +use data_provider::{CGDataProvider, CGDataProviderRef}; +use libc::size_t; +use std::ops::Deref; use std::mem; #[repr(C)] @@ -36,7 +39,7 @@ pub struct CGImage { impl Drop for CGImage { fn drop(&mut self) { unsafe { - CFRelease(self.as_CFTypeRef()) + CGImageRelease(self.as_concrete_TypeRef()) } } } @@ -49,6 +52,8 @@ impl Clone for CGImage { } } +// TODO: Replace all this stuff by simply using: +// impl_TCFType!(CGImage, CGImageRef, CGImageGetTypeID); impl TCFType for CGImage { #[inline] fn as_concrete_TypeRef(&self) -> CGImageRef { @@ -119,6 +124,24 @@ impl CGImage { TCFType::wrap_under_get_rule(CGImageGetColorSpace(self.as_concrete_TypeRef())) } } + + /// Returns the raw image bytes wrapped in `CFData`. Note, the returned `CFData` owns the + /// underlying buffer. + pub fn data(&self) -> CFData { + let data_provider = unsafe { + CGDataProvider::wrap_under_get_rule(CGImageGetDataProvider(self.as_concrete_TypeRef())) + }; + data_provider.copy_data() + } +} + +impl Deref for CGImage { + type Target = CGImageRef; + + #[inline] + fn deref(&self) -> &CGImageRef { + &self.obj + } } #[link(name = "ApplicationServices", kind = "framework")] @@ -130,4 +153,9 @@ extern { fn CGImageGetBitsPerPixel(image: CGImageRef) -> size_t; fn CGImageGetBytesPerRow(image: CGImageRef) -> size_t; fn CGImageGetColorSpace(image: CGImageRef) -> CGColorSpaceRef; + fn CGImageGetDataProvider(image: CGImageRef) -> CGDataProviderRef; + fn CGImageRelease(image: CGImageRef); + + //fn CGImageGetAlphaInfo(image: CGImageRef) -> CGImageAlphaInfo; + //fn CGImageCreateCopyWithColorSpace(image: CGImageRef, space: CGColorSpaceRef) -> CGImageRef } diff --git a/src/lib.rs b/src/lib.rs index 5c0a59a..42c12e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ // except according to those terms. extern crate libc; +#[macro_use] extern crate core_foundation; extern crate serde; @@ -20,8 +21,8 @@ pub mod context; pub mod data_provider; pub mod display; pub mod event; -pub mod event_source; +pub mod event_source; pub mod font; pub mod geometry; -pub mod image; pub mod private; +pub mod image; From c57d98ab7182a532afd7c12d1d0a1369919f53aa Mon Sep 17 00:00:00 2001 From: Daniel Abramov Date: Mon, 3 Apr 2017 12:12:56 +0200 Subject: [PATCH 117/166] Add CGWindowList API --- src/display.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/display.rs b/src/display.rs index 491e277..5328737 100644 --- a/src/display.rs +++ b/src/display.rs @@ -15,6 +15,8 @@ use libc; pub use base::{CGError, boolean_t}; pub use geometry::{CGRect, CGPoint, CGSize}; +use image::CGImageRef; + pub type CGDirectDisplayID = libc::uint32_t; pub type CGWindowID = libc::uint32_t; @@ -30,6 +32,14 @@ pub const kCGWindowListOptionOnScreenBelowWindow: CGWindowListOption = 1 << 2; pub const kCGWindowListOptionIncludingWindow: CGWindowListOption = 1 << 3; pub const kCGWindowListExcludeDesktopElements: CGWindowListOption = 1 << 4; +pub type CGWindowImageOption = libc::uint32_t; + +pub const kCGWindowImageDefault: CGWindowImageOption = 0; +pub const kCGWindowImageBoundsIgnoreFraming: CGWindowImageOption = 1 << 0; +pub const kCGWindowImageShouldBeOpaque: CGWindowImageOption = 1 << 1; +pub const kCGWindowImageOnlyShadows: CGWindowImageOption = 1 << 2; +pub const kCGWindowImageBestResolution: CGWindowImageOption = 1 << 3; +pub const kCGWindowImageNominalResolution: CGWindowImageOption = 1 << 4; pub use core_foundation::dictionary::{ CFDictionary, CFDictionaryRef, CFDictionaryGetValueIfPresent }; pub use core_foundation::array::{ CFArray, CFArrayRef }; @@ -38,6 +48,9 @@ pub use core_foundation::base::{ CFIndex, CFRelease, CFTypeRef }; #[link(name = "ApplicationServices", kind = "framework")] extern { + pub static CGRectNull: CGRect; + pub static CGRectInfinite: CGRect; + pub fn CGMainDisplayID() -> CGDirectDisplayID; pub fn CGDisplayIsActive(display: CGDirectDisplayID) -> boolean_t; pub fn CGDisplayIsAlwaysInMirrorSet(display: CGDirectDisplayID) -> boolean_t; @@ -73,5 +86,9 @@ extern { // Window Services Reference pub fn CGWindowListCopyWindowInfo(option: CGWindowListOption, relativeToWindow: CGWindowID ) -> CFArrayRef; + pub fn CGWindowListCreateImage(screenBounds: CGRect, + listOptions: CGWindowListOption, + windowId: CGWindowID, + imageOptions: CGWindowImageOption) -> CGImageRef; } From a4d3c12cb55413aba5df097f360fa182c1c1203d Mon Sep 17 00:00:00 2001 From: Daniel Abramov Date: Mon, 3 Apr 2017 16:05:43 +0200 Subject: [PATCH 118/166] Add `CFRectMakeWithDictionaryRepresentation` --- src/geometry.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/geometry.rs b/src/geometry.rs index e4145da..9497fbb 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -8,6 +8,8 @@ // except according to those terms. use base::CGFloat; +use core_foundation::base::TCFType; +use core_foundation::dictionary::CFDictionary; pub const CG_ZERO_POINT: CGPoint = CGPoint { x: 0.0, @@ -70,15 +72,31 @@ impl CGRect { ffi::CGRectInset(*self, size.width, size.height) } } + + #[inline] + pub fn from_dict_representation(dict: &CFDictionary) -> Option { + let mut rect = CGRect::new(&CGPoint::new(0., 0.), &CGSize::new(0., 0.)); + let result = unsafe { + ffi::CGRectMakeWithDictionaryRepresentation(dict.as_concrete_TypeRef(), &mut rect) + }; + if result == 0 { + None + } else { + Some(rect) + } + } } mod ffi { - use base::CGFloat; + use base::{CGFloat, boolean_t}; use geometry::CGRect; + use core_foundation::dictionary::CFDictionaryRef; #[link(name = "ApplicationServices", kind = "framework")] extern { pub fn CGRectInset(rect: CGRect, dx: CGFloat, dy: CGFloat) -> CGRect; + pub fn CGRectMakeWithDictionaryRepresentation(dict: CFDictionaryRef, + rect: *mut CGRect) -> boolean_t; } } From 81ded0d8abc5a1ca95cbfa7a7bf2c475aa1af506 Mon Sep 17 00:00:00 2001 From: Daniel Abramov Date: Tue, 4 Apr 2017 15:40:32 +0200 Subject: [PATCH 119/166] Add the posibility to specify buffer for CGContext --- src/context.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/context.rs b/src/context.rs index 1788c6b..3f05001 100644 --- a/src/context.rs +++ b/src/context.rs @@ -88,7 +88,8 @@ impl TCFType for CGContext { } impl CGContext { - pub fn create_bitmap_context(width: size_t, + pub fn create_bitmap_context(data: Option<*mut c_void>, + width: size_t, height: size_t, bits_per_component: size_t, bytes_per_row: size_t, @@ -96,7 +97,7 @@ impl CGContext { bitmap_info: u32) -> CGContext { unsafe { - let result = CGBitmapContextCreate(ptr::null_mut(), + let result = CGBitmapContextCreate(data.unwrap_or(ptr::null_mut()), width, height, bits_per_component, From f82e6e50159d007fa34e4a54e655720bdcda14df Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Fri, 19 May 2017 10:45:17 +0200 Subject: [PATCH 120/166] CGContextSetFontSmoothingStyle --- src/context.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/context.rs b/src/context.rs index 3f05001..42a6492 100644 --- a/src/context.rs +++ b/src/context.rs @@ -10,7 +10,7 @@ use base::CGFloat; use color_space::{CGColorSpace, CGColorSpaceRef}; use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; -use libc::{c_void, size_t}; +use libc::{c_void, c_int, size_t}; use std::mem; use std::ptr; use std::slice; @@ -146,6 +146,12 @@ impl CGContext { } } + fn set_font_smoothing_style(&self, style: i32) { + unsafe { + CGContextSetFontSmoothingStyle(self.as_concrete_TypeRef(), style as _); + } + } + pub fn set_should_smooth_fonts(&self, should_smooth_fonts: bool) { unsafe { CGContextSetShouldSmoothFonts(self.as_concrete_TypeRef(), should_smooth_fonts) @@ -234,6 +240,7 @@ extern { fn CGContextGetTypeID() -> CFTypeID; fn CGContextSetAllowsFontSmoothing(c: CGContextRef, allowsFontSmoothing: bool); fn CGContextSetShouldSmoothFonts(c: CGContextRef, shouldSmoothFonts: bool); + fn CGContextSetFontSmoothingStyle(c: CGContextRef, style: c_int); fn CGContextSetAllowsAntialiasing(c: CGContextRef, allowsAntialiasing: bool); fn CGContextSetShouldAntialias(c: CGContextRef, shouldAntialias: bool); fn CGContextSetAllowsFontSubpixelQuantization(c: CGContextRef, From 62d4cc7c8d581e66bf664860f01ae8b5d77e2277 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Tue, 23 May 2017 10:28:48 -0400 Subject: [PATCH 121/166] Update minor version. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c4ef0bb..fc5249d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.7.0" +version = "0.8.0" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" From da76a424069e2172488846eabcf83baf4d7a10ec Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Wed, 31 May 2017 16:13:47 +0200 Subject: [PATCH 122/166] pub, obviously --- src/context.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/context.rs b/src/context.rs index 42a6492..3347bf5 100644 --- a/src/context.rs +++ b/src/context.rs @@ -146,7 +146,7 @@ impl CGContext { } } - fn set_font_smoothing_style(&self, style: i32) { + pub fn set_font_smoothing_style(&self, style: i32) { unsafe { CGContextSetFontSmoothingStyle(self.as_concrete_TypeRef(), style as _); } From 2aaf2d2d842bc3f2ac997a75783e54d077734d1b Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Wed, 31 May 2017 16:14:30 +0200 Subject: [PATCH 123/166] patch version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index fc5249d..1c8ea68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.8.0" +version = "0.8.1" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" From 77d66fcc429c6c4566e97e38a2860b75ba2cf426 Mon Sep 17 00:00:00 2001 From: Bastien Orivel Date: Mon, 19 Jun 2017 20:29:12 +0200 Subject: [PATCH 124/166] Bump bitflags to 0.9 and bump version --- Cargo.toml | 4 ++-- src/event.rs | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1c8ea68..dae4dc4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.8.1" +version = "0.8.2" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" @@ -14,4 +14,4 @@ elcapitan = [] [dependencies] libc = "0.2" core-foundation = "0.3" -bitflags = "0.8" +bitflags = "0.9" diff --git a/src/event.rs b/src/event.rs index c6ea59d..d43905c 100644 --- a/src/event.rs +++ b/src/event.rs @@ -12,25 +12,25 @@ pub type CGKeyCode = libc::uint16_t; /// /// [Ref](http://opensource.apple.com/source/IOHIDFamily/IOHIDFamily-700/IOHIDSystem/IOKit/hidsystem/IOLLEvent.h) bitflags! { - pub flags CGEventFlags: u64 { - const CGEventFlagNull = 0, + pub struct CGEventFlags: u64 { + const CGEventFlagNull = 0; // Device-independent modifier key bits. - const CGEventFlagAlphaShift = 0x00010000, - const CGEventFlagShift = 0x00020000, - const CGEventFlagControl = 0x00040000, - const CGEventFlagAlternate = 0x00080000, - const CGEventFlagCommand = 0x00100000, + const CGEventFlagAlphaShift = 0x00010000; + const CGEventFlagShift = 0x00020000; + const CGEventFlagControl = 0x00040000; + const CGEventFlagAlternate = 0x00080000; + const CGEventFlagCommand = 0x00100000; // Special key identifiers. - const CGEventFlagHelp = 0x00400000, - const CGEventFlagSecondaryFn = 0x00800000, + const CGEventFlagHelp = 0x00400000; + const CGEventFlagSecondaryFn = 0x00800000; // Identifies key events from numeric keypad area on extended keyboards. - const CGEventFlagNumericPad = 0x00200000, + const CGEventFlagNumericPad = 0x00200000; // Indicates if mouse/pen movement events are not being coalesced - const CGEventFlagNonCoalesced = 0x00000100, + const CGEventFlagNonCoalesced = 0x00000100; } } From f9d4aa4deaefcab197d764c8306367603e357f13 Mon Sep 17 00:00:00 2001 From: Daniel Abramov Date: Thu, 27 Apr 2017 16:43:41 +0200 Subject: [PATCH 125/166] Add missing `CGDisplayCreateImage()` function. --- src/display.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/display.rs b/src/display.rs index 5328737..e398730 100644 --- a/src/display.rs +++ b/src/display.rs @@ -76,6 +76,7 @@ extern { pub fn CGDisplayPixelsHigh(display: CGDirectDisplayID) -> libc::size_t; pub fn CGDisplayPixelsWide(display: CGDirectDisplayID) -> libc::size_t; pub fn CGDisplayBounds(display: CGDirectDisplayID) -> CGRect; + pub fn CGDisplayCreateImage(display: CGDirectDisplayID) -> CGImageRef; // mouse stuff pub fn CGDisplayHideCursor(display: CGDirectDisplayID) -> CGError; From c60afb8261b02bc3e0fbd44f44e1a2de37299b5e Mon Sep 17 00:00:00 2001 From: Daniel Abramov Date: Thu, 8 Jun 2017 10:41:30 +0200 Subject: [PATCH 126/166] Update core-foundation version, bump crate version --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dae4dc4..88c95e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.8.2" +version = "0.9.0" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" @@ -12,6 +12,6 @@ default = [] elcapitan = [] [dependencies] -libc = "0.2" -core-foundation = "0.3" bitflags = "0.9" +core-foundation = "0.4" +libc = "0.2" From f9f22263d81504368304acab8882eaf197d68033 Mon Sep 17 00:00:00 2001 From: Daniel Abramov Date: Tue, 20 Jun 2017 12:08:09 +0200 Subject: [PATCH 127/166] Add a couple additional CGRect functions --- src/geometry.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/geometry.rs b/src/geometry.rs index 9497fbb..9f165b2 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -85,6 +85,22 @@ impl CGRect { Some(rect) } } + + #[inline] + pub fn is_empty(&self) -> bool { + unsafe { + // I use one, as it seems that `YES` is not available from this crate. + ffi::CGRectIsEmpty(*self) == 1 + } + } + + #[inline] + pub fn is_intersects(&self, other: &CGRect) -> bool { + unsafe { + // I use one, as it seems that `YES` is not available from this crate. + ffi::CGRectIntersectsRect(*self, *other) == 1 + } + } } mod ffi { @@ -97,6 +113,8 @@ mod ffi { pub fn CGRectInset(rect: CGRect, dx: CGFloat, dy: CGFloat) -> CGRect; pub fn CGRectMakeWithDictionaryRepresentation(dict: CFDictionaryRef, rect: *mut CGRect) -> boolean_t; + pub fn CGRectIsEmpty(rect: CGRect) -> boolean_t; + pub fn CGRectIntersectsRect(rect1: CGRect, rect2: CGRect) -> boolean_t; } } From 7066159155a05a5147b7cb9178d79d91d65bb243 Mon Sep 17 00:00:00 2001 From: Daniel Abramov Date: Mon, 24 Jul 2017 15:25:33 +0200 Subject: [PATCH 128/166] Add CGWindowListCreateImageFromArray() --- src/display.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/display.rs b/src/display.rs index e398730..62a2dd9 100644 --- a/src/display.rs +++ b/src/display.rs @@ -91,5 +91,7 @@ extern { listOptions: CGWindowListOption, windowId: CGWindowID, imageOptions: CGWindowImageOption) -> CGImageRef; - + pub fn CGWindowListCreateImageFromArray(screenBounds: CGRect, + windowArray: CFArrayRef, + imageOptions: CGWindowImageOption) -> CGImageRef; } From b0b6dcdbaa66ad96d8451b0dd495f438199b017f Mon Sep 17 00:00:00 2001 From: Ryan Thomas Date: Thu, 27 Jul 2017 19:26:54 +0100 Subject: [PATCH 129/166] Remove warnings for zero-sized structs #89. This follows the servo pattern mentioned in https://github.com/rust-lang/rust/issues/27303 to replace the zero-sized structs with an `enum`. --- src/base.rs | 4 +++- src/context.rs | 5 +++-- src/font.rs | 5 +++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/base.rs b/src/base.rs index 9cfb0a9..5b8e71c 100644 --- a/src/base.rs +++ b/src/base.rs @@ -28,7 +28,9 @@ pub type CGFloat = libc::c_float; pub type CGError = libc::int32_t; -pub type CGAffineTransform = (); +// This is an enum due to zero-sized types warnings. +// For more details see https://github.com/rust-lang/rust/issues/27303 +pub enum CGAffineTransform {} pub const kCGImageAlphaNone: u32 = 0; pub const kCGImageAlphaPremultipliedLast: u32 = 1; diff --git a/src/context.rs b/src/context.rs index 3347bf5..d6c44d3 100644 --- a/src/context.rs +++ b/src/context.rs @@ -28,8 +28,9 @@ pub enum CGTextDrawingMode { CGTextClip } -#[repr(C)] -pub struct __CGContext; +// This is an enum due to zero-sized types warnings. +// For more details see https://github.com/rust-lang/rust/issues/27303 +pub enum __CGContext {} pub type CGContextRef = *const __CGContext; diff --git a/src/font.rs b/src/font.rs index 3c1c390..00d3b89 100644 --- a/src/font.rs +++ b/src/font.rs @@ -17,8 +17,9 @@ use std::ptr; pub type CGGlyph = libc::c_ushort; -#[repr(C)] -pub struct __CGFont; +// This is an enum due to zero-sized types warnings. +// For more details see https://github.com/rust-lang/rust/issues/27303 +pub enum __CGFont {} pub type CGFontRef = *const __CGFont; From 08f5ffd4bfb92d1ab189abeee7206f7597145c15 Mon Sep 17 00:00:00 2001 From: Ryan Thomas Date: Thu, 27 Jul 2017 19:40:39 +0100 Subject: [PATCH 130/166] Squash warnings on zero-sized structs #89. Changes zero-sized structs to an `enum` and removed unused macro use statement. --- src/color_space.rs | 5 +++-- src/data_provider.rs | 5 +++-- src/event.rs | 6 ++++-- src/event_source.rs | 5 +++-- src/image.rs | 5 +++-- src/lib.rs | 1 - 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/color_space.rs b/src/color_space.rs index 10a728a..20764c3 100644 --- a/src/color_space.rs +++ b/src/color_space.rs @@ -10,8 +10,9 @@ use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; use std::mem; -#[repr(C)] -pub struct __CGColorSpace; +// This is an enum due to zero-sized types warnings. +// For more details see https://github.com/rust-lang/rust/issues/27303 +pub enum __CGColorSpace {} pub type CGColorSpaceRef = *const __CGColorSpace; diff --git a/src/data_provider.rs b/src/data_provider.rs index aaa5f75..9cead2c 100644 --- a/src/data_provider.rs +++ b/src/data_provider.rs @@ -26,8 +26,9 @@ pub type CGDataProviderReleaseBytePointerCallback = Option; pub type CGDataProviderGetBytesAtPositionCallback = Option; -#[repr(C)] -pub struct __CGDataProvider; +// This is an enum due to zero-sized types warnings. +// For more details see https://github.com/rust-lang/rust/issues/27303 +pub enum __CGDataProvider {} pub type CGDataProviderRef = *const __CGDataProvider; diff --git a/src/event.rs b/src/event.rs index d43905c..d94b270 100644 --- a/src/event.rs +++ b/src/event.rs @@ -12,6 +12,7 @@ pub type CGKeyCode = libc::uint16_t; /// /// [Ref](http://opensource.apple.com/source/IOHIDFamily/IOHIDFamily-700/IOHIDSystem/IOKit/hidsystem/IOLLEvent.h) bitflags! { + #[repr(C)] pub struct CGEventFlags: u64 { const CGEventFlagNull = 0; @@ -88,8 +89,9 @@ pub enum CGEventTapLocation { AnnotatedSession, } -#[repr(C)] -pub struct __CGEvent; +// This is an enum due to zero-sized types warnings. +// For more details see https://github.com/rust-lang/rust/issues/27303 +pub enum __CGEvent {} pub type CGEventRef = *const __CGEvent; diff --git a/src/event_source.rs b/src/event_source.rs index 32bbaef..c38aca9 100644 --- a/src/event_source.rs +++ b/src/event_source.rs @@ -12,8 +12,9 @@ pub enum CGEventSourceStateID { HIDSystemState = 1, } -#[repr(C)] -pub struct __CGEventSource; +// This is an enum due to zero-sized types warnings. +// For more details see https://github.com/rust-lang/rust/issues/27303 +pub enum __CGEventSource {} pub type CGEventSourceRef = *const __CGEventSource; diff --git a/src/image.rs b/src/image.rs index aea7742..b355870 100644 --- a/src/image.rs +++ b/src/image.rs @@ -27,8 +27,9 @@ pub enum CGImageByteOrderInfo { CGImageByteOrder32Big = (4 << 12) } -#[repr(C)] -pub struct __CGImage; +// This is an enum due to zero-sized types warnings. +// For more details see https://github.com/rust-lang/rust/issues/27303 +pub enum __CGImage {} pub type CGImageRef = *const __CGImage; diff --git a/src/lib.rs b/src/lib.rs index daed628..bfba026 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,6 @@ // except according to those terms. extern crate libc; -#[macro_use] extern crate core_foundation; #[macro_use] From 7fbe5d50abb623941a62d2fe167867a99d4fd10e Mon Sep 17 00:00:00 2001 From: Ryan Thomas Date: Thu, 27 Jul 2017 20:24:47 +0100 Subject: [PATCH 131/166] Bump version number to 0.9.1. --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 88c95e4..d82bdc7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.9.0" +version = "0.9.1" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" @@ -13,5 +13,5 @@ elcapitan = [] [dependencies] bitflags = "0.9" -core-foundation = "0.4" +core-foundation = { path = "../core-foundation-rs/core-foundation" } #"0.4" libc = "0.2" From 4e2947444967b21146bdfa277c5e259a8e76864c Mon Sep 17 00:00:00 2001 From: Ryan Thomas Date: Thu, 27 Jul 2017 20:38:59 +0100 Subject: [PATCH 132/166] Remove testing path of core-foundation. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index d82bdc7..5bb72dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,5 +13,5 @@ elcapitan = [] [dependencies] bitflags = "0.9" -core-foundation = { path = "../core-foundation-rs/core-foundation" } #"0.4" +core-foundation = "0.4" libc = "0.2" From e0b23498ffe10546ba83a1e4ad69ada69ebdebfc Mon Sep 17 00:00:00 2001 From: Kornel Date: Thu, 14 Sep 2017 22:50:07 +0100 Subject: [PATCH 133/166] Silence warning --- src/display.rs | 2 -- src/event.rs | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/display.rs b/src/display.rs index 62a2dd9..8363abc 100644 --- a/src/display.rs +++ b/src/display.rs @@ -9,8 +9,6 @@ #![allow(non_upper_case_globals)] -extern crate core_foundation; - use libc; pub use base::{CGError, boolean_t}; pub use geometry::{CGRect, CGPoint, CGSize}; diff --git a/src/event.rs b/src/event.rs index d94b270..e734664 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,3 +1,5 @@ +#![allow(non_upper_case_globals)] + use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; use geometry::CGPoint; use event_source::{CGEventSource,CGEventSourceRef}; From 987ec455b4e7fc03fdc1cfd248169cac4fdf7597 Mon Sep 17 00:00:00 2001 From: Kornel Date: Thu, 14 Sep 2017 23:54:48 +0100 Subject: [PATCH 134/166] Test --- src/context.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/context.rs b/src/context.rs index d6c44d3..20d84bf 100644 --- a/src/context.rs +++ b/src/context.rs @@ -105,6 +105,7 @@ impl CGContext { bytes_per_row, space.as_concrete_TypeRef(), bitmap_info); + assert!(!result.is_null()); TCFType::wrap_under_create_rule(result) } } @@ -223,6 +224,30 @@ impl CGContext { } } +#[test] +fn create_bitmap_context_test() { + use geometry::*; + + let cs = CGColorSpace::create_device_rgb(); + let ctx = CGContext::create_bitmap_context(None, + 16, 8, + 8, 0, + &cs, + ::base::kCGImageAlphaPremultipliedLast); + ctx.set_rgb_fill_color(1.,0.,1.,1.); + ctx.fill_rect(CGRect::new(&CGPoint::new(0.,0.), &CGSize::new(8.,8.))); + let img = ctx.create_image().unwrap(); + assert_eq!(16, img.width()); + assert_eq!(8, img.height()); + assert_eq!(8, img.bits_per_component()); + assert_eq!(32, img.bits_per_pixel()); + let data = img.data(); + assert_eq!(255, data.bytes()[0]); + assert_eq!(0, data.bytes()[1]); + assert_eq!(255, data.bytes()[2]); + assert_eq!(255, data.bytes()[3]); +} + #[link(name = "ApplicationServices", kind = "framework")] extern { fn CGBitmapContextCreate(data: *mut c_void, From 14f286f8e983e3f14c75875deac78c0d59bf9718 Mon Sep 17 00:00:00 2001 From: Kornel Date: Thu, 14 Sep 2017 22:53:28 +0100 Subject: [PATCH 135/166] Use foreign-types to manage CFImage --- Cargo.toml | 1 + src/context.rs | 13 +++--- src/display.rs | 8 ++-- src/image.rs | 114 +++++++++++++------------------------------------ src/lib.rs | 4 ++ src/sys.rs | 3 ++ 6 files changed, 47 insertions(+), 96 deletions(-) create mode 100644 src/sys.rs diff --git a/Cargo.toml b/Cargo.toml index 5bb72dc..08bc30d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,5 @@ elcapitan = [] [dependencies] bitflags = "0.9" core-foundation = "0.4" +foreign-types = "0.3.0" libc = "0.2" diff --git a/src/context.rs b/src/context.rs index 20d84bf..a0141d0 100644 --- a/src/context.rs +++ b/src/context.rs @@ -15,7 +15,8 @@ use std::mem; use std::ptr; use std::slice; use geometry::CGRect; -use image::{CGImage, CGImageRef}; +use image::CGImage; +use foreign_types::ForeignType; #[repr(C)] pub enum CGTextDrawingMode { @@ -210,14 +211,14 @@ impl CGContext { pub fn draw_image(&self, rect: CGRect, image: &CGImage) { unsafe { - CGContextDrawImage(self.as_concrete_TypeRef(), rect, image.as_concrete_TypeRef()); + CGContextDrawImage(self.as_concrete_TypeRef(), rect, image.as_ptr()); } } pub fn create_image(&self) -> Option { let image = unsafe { CGBitmapContextCreateImage(self.as_concrete_TypeRef()) }; - if image != ptr::null() { - Some(unsafe { CGImage::wrap_under_create_rule(image) }) + if !image.is_null() { + Some(unsafe { CGImage::from_ptr(image) }) } else { None } @@ -262,7 +263,7 @@ extern { fn CGBitmapContextGetWidth(context: CGContextRef) -> size_t; fn CGBitmapContextGetHeight(context: CGContextRef) -> size_t; fn CGBitmapContextGetBytesPerRow(context: CGContextRef) -> size_t; - fn CGBitmapContextCreateImage(context: CGContextRef) -> CGImageRef; + fn CGBitmapContextCreateImage(context: CGContextRef) -> ::sys::CGImageRef; fn CGContextGetTypeID() -> CFTypeID; fn CGContextSetAllowsFontSmoothing(c: CGContextRef, allowsFontSmoothing: bool); fn CGContextSetShouldSmoothFonts(c: CGContextRef, shouldSmoothFonts: bool); @@ -285,6 +286,6 @@ extern { alpha: CGFloat); fn CGContextFillRect(context: CGContextRef, rect: CGRect); - fn CGContextDrawImage(c: CGContextRef, rect: CGRect, image: CGImageRef); + fn CGContextDrawImage(c: CGContextRef, rect: CGRect, image: ::sys::CGImageRef); } diff --git a/src/display.rs b/src/display.rs index 8363abc..345c040 100644 --- a/src/display.rs +++ b/src/display.rs @@ -13,8 +13,6 @@ use libc; pub use base::{CGError, boolean_t}; pub use geometry::{CGRect, CGPoint, CGSize}; -use image::CGImageRef; - pub type CGDirectDisplayID = libc::uint32_t; pub type CGWindowID = libc::uint32_t; @@ -74,7 +72,7 @@ extern { pub fn CGDisplayPixelsHigh(display: CGDirectDisplayID) -> libc::size_t; pub fn CGDisplayPixelsWide(display: CGDirectDisplayID) -> libc::size_t; pub fn CGDisplayBounds(display: CGDirectDisplayID) -> CGRect; - pub fn CGDisplayCreateImage(display: CGDirectDisplayID) -> CGImageRef; + pub fn CGDisplayCreateImage(display: CGDirectDisplayID) -> ::sys::CGImageRef; // mouse stuff pub fn CGDisplayHideCursor(display: CGDirectDisplayID) -> CGError; @@ -88,8 +86,8 @@ extern { pub fn CGWindowListCreateImage(screenBounds: CGRect, listOptions: CGWindowListOption, windowId: CGWindowID, - imageOptions: CGWindowImageOption) -> CGImageRef; + imageOptions: CGWindowImageOption) -> ::sys::CGImageRef; pub fn CGWindowListCreateImageFromArray(screenBounds: CGRect, windowArray: CFArrayRef, - imageOptions: CGWindowImageOption) -> CGImageRef; + imageOptions: CGWindowImageOption) -> ::sys::CGImageRef; } diff --git a/src/image.rs b/src/image.rs index b355870..38c8ca1 100644 --- a/src/image.rs +++ b/src/image.rs @@ -1,15 +1,14 @@ -use core_foundation::base::{CFRetain, CFTypeID, CFTypeRef, TCFType}; +use core_foundation::base::{CFRetain, CFTypeID, TCFType}; use core_foundation::data::CFData; use color_space::{CGColorSpace, CGColorSpaceRef}; use data_provider::{CGDataProvider, CGDataProviderRef}; use libc::size_t; -use std::ops::Deref; -use std::mem; +use foreign_types::ForeignType; #[repr(C)] pub enum CGImageAlphaInfo { CGImageAlphaNone, /* For example, RGB. */ - CGImageAlphaPremultipliedLast, /* For example, premultiplied RGBA */ + CGImageAlphaPremultipliedLast, /* For example, premultiplied RGBA */ CGImageAlphaPremultipliedFirst, /* For example, premultiplied ARGB */ CGImageAlphaLast, /* For example, non-premultiplied RGBA */ CGImageAlphaFirst, /* For example, non-premultiplied ARGB */ @@ -27,102 +26,56 @@ pub enum CGImageByteOrderInfo { CGImageByteOrder32Big = (4 << 12) } -// This is an enum due to zero-sized types warnings. -// For more details see https://github.com/rust-lang/rust/issues/27303 -pub enum __CGImage {} - -pub type CGImageRef = *const __CGImage; - -pub struct CGImage { - obj: CGImageRef, +foreign_type! { + type CType = ::sys::CGImage; + fn drop = CGImageRelease; + fn clone = |p| CFRetain(p as *const _) as *mut _; + pub struct CGImage; + pub struct CGImageRef; } -impl Drop for CGImage { - fn drop(&mut self) { - unsafe { - CGImageRelease(self.as_concrete_TypeRef()) - } - } -} - -impl Clone for CGImage { - fn clone(&self) -> CGImage { - unsafe { - TCFType::wrap_under_get_rule(self.as_concrete_TypeRef()) - } - } -} - -// TODO: Replace all this stuff by simply using: -// impl_TCFType!(CGImage, CGImageRef, CGImageGetTypeID); -impl TCFType for CGImage { - #[inline] - fn as_concrete_TypeRef(&self) -> CGImageRef { - self.obj - } - - #[inline] - unsafe fn wrap_under_get_rule(reference: CGImageRef) -> CGImage { - let reference: CGImageRef = mem::transmute(CFRetain(mem::transmute(reference))); - TCFType::wrap_under_create_rule(reference) - } - - #[inline] - fn as_CFTypeRef(&self) -> CFTypeRef { - unsafe { - mem::transmute(self.as_concrete_TypeRef()) - } - } - - #[inline] - unsafe fn wrap_under_create_rule(obj: CGImageRef) -> CGImage { - CGImage { - obj: obj, - } - } - - #[inline] - fn type_id() -> CFTypeID { +impl CGImage { + pub fn type_id() -> CFTypeID { unsafe { CGImageGetTypeID() } } } -impl CGImage { +impl CGImageRef { pub fn width(&self) -> size_t { unsafe { - CGImageGetWidth(self.as_concrete_TypeRef()) + CGImageGetWidth(self.as_ptr()) } } pub fn height(&self) -> size_t { unsafe { - CGImageGetHeight(self.as_concrete_TypeRef()) + CGImageGetHeight(self.as_ptr()) } } pub fn bits_per_component(&self) -> size_t { unsafe { - CGImageGetBitsPerComponent(self.as_concrete_TypeRef()) + CGImageGetBitsPerComponent(self.as_ptr()) } } pub fn bits_per_pixel(&self) -> size_t { unsafe { - CGImageGetBitsPerPixel(self.as_concrete_TypeRef()) + CGImageGetBitsPerPixel(self.as_ptr()) } } pub fn bytes_per_row(&self) -> size_t { unsafe { - CGImageGetBytesPerRow(self.as_concrete_TypeRef()) + CGImageGetBytesPerRow(self.as_ptr()) } } pub fn color_space(&self) -> CGColorSpace { unsafe { - TCFType::wrap_under_get_rule(CGImageGetColorSpace(self.as_concrete_TypeRef())) + TCFType::wrap_under_get_rule(CGImageGetColorSpace(self.as_ptr())) } } @@ -130,33 +83,24 @@ impl CGImage { /// underlying buffer. pub fn data(&self) -> CFData { let data_provider = unsafe { - CGDataProvider::wrap_under_get_rule(CGImageGetDataProvider(self.as_concrete_TypeRef())) + CGDataProvider::wrap_under_get_rule(CGImageGetDataProvider(self.as_ptr())) }; data_provider.copy_data() } } -impl Deref for CGImage { - type Target = CGImageRef; - - #[inline] - fn deref(&self) -> &CGImageRef { - &self.obj - } -} - #[link(name = "ApplicationServices", kind = "framework")] extern { fn CGImageGetTypeID() -> CFTypeID; - fn CGImageGetWidth(image: CGImageRef) -> size_t; - fn CGImageGetHeight(image: CGImageRef) -> size_t; - fn CGImageGetBitsPerComponent(image: CGImageRef) -> size_t; - fn CGImageGetBitsPerPixel(image: CGImageRef) -> size_t; - fn CGImageGetBytesPerRow(image: CGImageRef) -> size_t; - fn CGImageGetColorSpace(image: CGImageRef) -> CGColorSpaceRef; - fn CGImageGetDataProvider(image: CGImageRef) -> CGDataProviderRef; - fn CGImageRelease(image: CGImageRef); + fn CGImageGetWidth(image: ::sys::CGImageRef) -> size_t; + fn CGImageGetHeight(image: ::sys::CGImageRef) -> size_t; + fn CGImageGetBitsPerComponent(image: ::sys::CGImageRef) -> size_t; + fn CGImageGetBitsPerPixel(image: ::sys::CGImageRef) -> size_t; + fn CGImageGetBytesPerRow(image: ::sys::CGImageRef) -> size_t; + fn CGImageGetColorSpace(image: ::sys::CGImageRef) -> CGColorSpaceRef; + fn CGImageGetDataProvider(image: ::sys::CGImageRef) -> CGDataProviderRef; + fn CGImageRelease(image: ::sys::CGImageRef); - //fn CGImageGetAlphaInfo(image: CGImageRef) -> CGImageAlphaInfo; - //fn CGImageCreateCopyWithColorSpace(image: CGImageRef, space: CGColorSpaceRef) -> CGImageRef + //fn CGImageGetAlphaInfo(image: ::sys::CGImageRef) -> CGImageAlphaInfo; + //fn CGImageCreateCopyWithColorSpace(image: ::sys::CGImageRef, space: CGColorSpaceRef) -> ::sys::CGImageRef } diff --git a/src/lib.rs b/src/lib.rs index bfba026..61caacb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,9 @@ extern crate core_foundation; #[macro_use] extern crate bitflags; +#[macro_use] +extern crate foreign_types; + pub mod base; pub mod color_space; pub mod context; @@ -24,3 +27,4 @@ pub mod font; pub mod geometry; pub mod private; pub mod image; +mod sys; diff --git a/src/sys.rs b/src/sys.rs new file mode 100644 index 0000000..897c3cc --- /dev/null +++ b/src/sys.rs @@ -0,0 +1,3 @@ + +pub enum CGImage {} +pub type CGImageRef = *mut CGImage; From 045fa8ca48cedf375dba36d1332dbf9936f5613c Mon Sep 17 00:00:00 2001 From: Kornel Date: Thu, 14 Sep 2017 23:01:39 +0100 Subject: [PATCH 136/166] Use foreign-types to manage CGColorSpace --- src/color_space.rs | 70 ++++++++-------------------------------------- src/context.rs | 6 ++-- src/image.rs | 10 ++++--- src/sys.rs | 3 ++ 4 files changed, 24 insertions(+), 65 deletions(-) diff --git a/src/color_space.rs b/src/color_space.rs index 20764c3..4117084 100644 --- a/src/color_space.rs +++ b/src/color_space.rs @@ -7,81 +7,35 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; -use std::mem; +use core_foundation::base::{CFRelease, CFRetain, CFTypeID}; +use foreign_types::ForeignType; -// This is an enum due to zero-sized types warnings. -// For more details see https://github.com/rust-lang/rust/issues/27303 -pub enum __CGColorSpace {} - -pub type CGColorSpaceRef = *const __CGColorSpace; - -pub struct CGColorSpace { - obj: CGColorSpaceRef, +foreign_type! { + type CType = ::sys::CGColorSpace; + fn drop = |p| CFRelease(p as *mut _); + fn clone = |p| CFRetain(p as *const _) as *mut _; + pub struct CGColorSpace; + pub struct CGColorSpaceRef; } -impl Drop for CGColorSpace { - fn drop(&mut self) { - unsafe { - CFRelease(self.as_CFTypeRef()) - } - } -} - -impl Clone for CGColorSpace { - fn clone(&self) -> CGColorSpace { - unsafe { - TCFType::wrap_under_get_rule(self.as_concrete_TypeRef()) - } - } -} - -impl TCFType for CGColorSpace { - #[inline] - fn as_concrete_TypeRef(&self) -> CGColorSpaceRef { - self.obj - } - - #[inline] - unsafe fn wrap_under_get_rule(reference: CGColorSpaceRef) -> CGColorSpace { - let reference: CGColorSpaceRef = mem::transmute(CFRetain(mem::transmute(reference))); - TCFType::wrap_under_create_rule(reference) - } - - #[inline] - fn as_CFTypeRef(&self) -> CFTypeRef { - unsafe { - mem::transmute(self.as_concrete_TypeRef()) - } - } - - #[inline] - unsafe fn wrap_under_create_rule(obj: CGColorSpaceRef) -> CGColorSpace { - CGColorSpace { - obj: obj, - } - } - - #[inline] - fn type_id() -> CFTypeID { +impl CGColorSpace { + pub fn type_id() -> CFTypeID { unsafe { CGColorSpaceGetTypeID() } } -} -impl CGColorSpace { pub fn create_device_rgb() -> CGColorSpace { unsafe { let result = CGColorSpaceCreateDeviceRGB(); - TCFType::wrap_under_create_rule(result) + CGColorSpace::from_ptr(result) } } } #[link(name = "ApplicationServices", kind = "framework")] extern { - fn CGColorSpaceCreateDeviceRGB() -> CGColorSpaceRef; + fn CGColorSpaceCreateDeviceRGB() -> ::sys::CGColorSpaceRef; fn CGColorSpaceGetTypeID() -> CFTypeID; } diff --git a/src/context.rs b/src/context.rs index a0141d0..8a9ac3a 100644 --- a/src/context.rs +++ b/src/context.rs @@ -8,7 +8,7 @@ // except according to those terms. use base::CGFloat; -use color_space::{CGColorSpace, CGColorSpaceRef}; +use color_space::CGColorSpace; use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; use libc::{c_void, c_int, size_t}; use std::mem; @@ -104,7 +104,7 @@ impl CGContext { height, bits_per_component, bytes_per_row, - space.as_concrete_TypeRef(), + space.as_ptr(), bitmap_info); assert!(!result.is_null()); TCFType::wrap_under_create_rule(result) @@ -256,7 +256,7 @@ extern { height: size_t, bitsPerComponent: size_t, bytesPerRow: size_t, - space: CGColorSpaceRef, + space: ::sys::CGColorSpaceRef, bitmapInfo: u32) -> CGContextRef; fn CGBitmapContextGetData(context: CGContextRef) -> *mut c_void; diff --git a/src/image.rs b/src/image.rs index 38c8ca1..3cb9c7b 100644 --- a/src/image.rs +++ b/src/image.rs @@ -1,6 +1,6 @@ use core_foundation::base::{CFRetain, CFTypeID, TCFType}; use core_foundation::data::CFData; -use color_space::{CGColorSpace, CGColorSpaceRef}; +use color_space::CGColorSpace; use data_provider::{CGDataProvider, CGDataProviderRef}; use libc::size_t; use foreign_types::ForeignType; @@ -75,7 +75,9 @@ impl CGImageRef { pub fn color_space(&self) -> CGColorSpace { unsafe { - TCFType::wrap_under_get_rule(CGImageGetColorSpace(self.as_ptr())) + let cs = CGImageGetColorSpace(self.as_ptr()); + CFRetain(cs as *mut _); + CGColorSpace::from_ptr(cs) } } @@ -97,10 +99,10 @@ extern { fn CGImageGetBitsPerComponent(image: ::sys::CGImageRef) -> size_t; fn CGImageGetBitsPerPixel(image: ::sys::CGImageRef) -> size_t; fn CGImageGetBytesPerRow(image: ::sys::CGImageRef) -> size_t; - fn CGImageGetColorSpace(image: ::sys::CGImageRef) -> CGColorSpaceRef; + fn CGImageGetColorSpace(image: ::sys::CGImageRef) -> ::sys::CGColorSpaceRef; fn CGImageGetDataProvider(image: ::sys::CGImageRef) -> CGDataProviderRef; fn CGImageRelease(image: ::sys::CGImageRef); //fn CGImageGetAlphaInfo(image: ::sys::CGImageRef) -> CGImageAlphaInfo; - //fn CGImageCreateCopyWithColorSpace(image: ::sys::CGImageRef, space: CGColorSpaceRef) -> ::sys::CGImageRef + //fn CGImageCreateCopyWithColorSpace(image: ::sys::CGImageRef, space: ::sys::CGColorSpaceRef) -> ::sys::CGImageRef } diff --git a/src/sys.rs b/src/sys.rs index 897c3cc..86bf701 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -1,3 +1,6 @@ pub enum CGImage {} pub type CGImageRef = *mut CGImage; + +pub enum CGColorSpace {} +pub type CGColorSpaceRef = *mut CGColorSpace; From ae15062bf18d7ed6d1205aafcc86bd0d143b765f Mon Sep 17 00:00:00 2001 From: Kornel Date: Thu, 14 Sep 2017 23:16:19 +0100 Subject: [PATCH 137/166] Use foreign-types for CGDataProvider --- src/data_provider.rs | 72 ++++++++++++-------------------------------- src/font.rs | 7 +++-- src/image.rs | 10 +++--- src/sys.rs | 5 ++- 4 files changed, 32 insertions(+), 62 deletions(-) diff --git a/src/data_provider.rs b/src/data_provider.rs index 9cead2c..ac2cb5a 100644 --- a/src/data_provider.rs +++ b/src/data_provider.rs @@ -7,13 +7,14 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; +use core_foundation::base::{CFRelease, CFRetain, CFTypeID, TCFType}; use core_foundation::data::{CFData, CFDataRef}; use libc::{c_void, size_t, off_t}; -use std::mem; use std::ptr; +use foreign_types::{ForeignType, ForeignTypeRef}; + pub type CGDataProviderGetBytesCallback = Option size_t>; pub type CGDataProviderReleaseInfoCallback = Option; pub type CGDataProviderRewindCallback = Option; @@ -26,78 +27,43 @@ pub type CGDataProviderReleaseBytePointerCallback = Option; pub type CGDataProviderGetBytesAtPositionCallback = Option; -// This is an enum due to zero-sized types warnings. -// For more details see https://github.com/rust-lang/rust/issues/27303 -pub enum __CGDataProvider {} - -pub type CGDataProviderRef = *const __CGDataProvider; - -pub struct CGDataProvider { - obj: CGDataProviderRef, +foreign_type! { + #[doc(hidden)] + type CType = ::sys::CGDataProvider; + fn drop = |cs| CFRelease(cs as *mut _); + fn clone = |p| CFRetain(p as *const _) as *mut _; + pub struct CGDataProvider; + pub struct CGDataProviderRef; } -impl Drop for CGDataProvider { - fn drop(&mut self) { - unsafe { - CFRelease(self.as_CFTypeRef()) - } - } -} - -impl TCFType for CGDataProvider { - #[inline] - fn as_concrete_TypeRef(&self) -> CGDataProviderRef { - self.obj - } - - #[inline] - unsafe fn wrap_under_get_rule(reference: CGDataProviderRef) -> CGDataProvider { - let reference: CGDataProviderRef = mem::transmute(CFRetain(mem::transmute(reference))); - TCFType::wrap_under_create_rule(reference) - } - - #[inline] - fn as_CFTypeRef(&self) -> CFTypeRef { - unsafe { - mem::transmute(self.as_concrete_TypeRef()) - } - } - - #[inline] - unsafe fn wrap_under_create_rule(obj: CGDataProviderRef) -> CGDataProvider { - CGDataProvider { - obj: obj, - } - } - - #[inline] - fn type_id() -> CFTypeID { +impl CGDataProvider { + pub fn type_id() -> CFTypeID { unsafe { CGDataProviderGetTypeID() } } -} -impl CGDataProvider { - pub fn from_buffer(buffer: &[u8]) -> CGDataProvider { + pub fn from_buffer(buffer: &[u8]) -> Self { unsafe { let result = CGDataProviderCreateWithData(ptr::null_mut(), buffer.as_ptr() as *const c_void, buffer.len() as size_t, None); - TCFType::wrap_under_create_rule(result) + CGDataProvider::from_ptr(result) } } +} +impl CGDataProviderRef { /// Creates a copy of the data from the underlying `CFDataProviderRef`. pub fn copy_data(&self) -> CFData { - unsafe { CFData::wrap_under_create_rule(CGDataProviderCopyData(self.obj)) } + unsafe { CFData::wrap_under_create_rule(CGDataProviderCopyData(self.as_ptr())) } } } #[link(name = "ApplicationServices", kind = "framework")] extern { - fn CGDataProviderCopyData(provider: CGDataProviderRef) -> CFDataRef; + fn CGDataProviderCopyData(provider: ::sys::CGDataProviderRef) -> CFDataRef; //fn CGDataProviderCreateDirect //fn CGDataProviderCreateSequential //fn CGDataProviderCreateWithCFData @@ -105,7 +71,7 @@ extern { data: *const c_void, size: size_t, releaseData: CGDataProviderReleaseDataCallback - ) -> CGDataProviderRef; + ) -> ::sys::CGDataProviderRef; //fn CGDataProviderCreateWithFilename(filename: *c_char) -> CGDataProviderRef; //fn CGDataProviderCreateWithURL fn CGDataProviderGetTypeID() -> CFTypeID; diff --git a/src/font.rs b/src/font.rs index 00d3b89..42a8329 100644 --- a/src/font.rs +++ b/src/font.rs @@ -9,7 +9,8 @@ use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; use core_foundation::string::{CFString, CFStringRef}; -use data_provider::{CGDataProvider, CGDataProviderRef}; +use data_provider::CGDataProvider; +use foreign_types::ForeignType; use libc; use std::mem; @@ -86,7 +87,7 @@ impl TCFType for CGFont { impl CGFont { pub fn from_data_provider(provider: CGDataProvider) -> Result { unsafe { - let font_ref = CGFontCreateWithDataProvider(provider.as_concrete_TypeRef()); + let font_ref = CGFontCreateWithDataProvider(provider.as_ptr()); if font_ref != ptr::null() { Ok(TCFType::wrap_under_create_rule(font_ref)) } else { @@ -117,7 +118,7 @@ impl CGFont { #[link(name = "ApplicationServices", kind = "framework")] extern { // TODO: basically nothing has bindings (even commented-out) besides what we use. - fn CGFontCreateWithDataProvider(provider: CGDataProviderRef) -> CGFontRef; + fn CGFontCreateWithDataProvider(provider: ::sys::CGDataProviderRef) -> CGFontRef; fn CGFontCreateWithFontName(name: CFStringRef) -> CGFontRef; fn CGFontGetTypeID() -> CFTypeID; diff --git a/src/image.rs b/src/image.rs index 3cb9c7b..c6b57a8 100644 --- a/src/image.rs +++ b/src/image.rs @@ -1,9 +1,9 @@ -use core_foundation::base::{CFRetain, CFTypeID, TCFType}; +use core_foundation::base::{CFRetain, CFTypeID}; use core_foundation::data::CFData; use color_space::CGColorSpace; -use data_provider::{CGDataProvider, CGDataProviderRef}; +use data_provider::CGDataProviderRef; use libc::size_t; -use foreign_types::ForeignType; +use foreign_types::{ForeignType, ForeignTypeRef}; #[repr(C)] pub enum CGImageAlphaInfo { @@ -85,7 +85,7 @@ impl CGImageRef { /// underlying buffer. pub fn data(&self) -> CFData { let data_provider = unsafe { - CGDataProvider::wrap_under_get_rule(CGImageGetDataProvider(self.as_ptr())) + CGDataProviderRef::from_ptr(CGImageGetDataProvider(self.as_ptr())) }; data_provider.copy_data() } @@ -100,7 +100,7 @@ extern { fn CGImageGetBitsPerPixel(image: ::sys::CGImageRef) -> size_t; fn CGImageGetBytesPerRow(image: ::sys::CGImageRef) -> size_t; fn CGImageGetColorSpace(image: ::sys::CGImageRef) -> ::sys::CGColorSpaceRef; - fn CGImageGetDataProvider(image: ::sys::CGImageRef) -> CGDataProviderRef; + fn CGImageGetDataProvider(image: ::sys::CGImageRef) -> ::sys::CGDataProviderRef; fn CGImageRelease(image: ::sys::CGImageRef); //fn CGImageGetAlphaInfo(image: ::sys::CGImageRef) -> CGImageAlphaInfo; diff --git a/src/sys.rs b/src/sys.rs index 86bf701..7d3c619 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -1,6 +1,9 @@ - pub enum CGImage {} pub type CGImageRef = *mut CGImage; pub enum CGColorSpace {} pub type CGColorSpaceRef = *mut CGColorSpace; + +pub enum CGDataProvider {} +pub type CGDataProviderRef = *mut CGDataProvider; + From 1993281ca6bc3df73dacb369e45fe66cd859bc17 Mon Sep 17 00:00:00 2001 From: Kornel Date: Thu, 14 Sep 2017 23:27:22 +0100 Subject: [PATCH 138/166] Use foreign-types for CGFont --- src/font.rs | 90 ++++++++++++----------------------------------------- src/sys.rs | 4 +++ 2 files changed, 24 insertions(+), 70 deletions(-) diff --git a/src/font.rs b/src/font.rs index 42a8329..798dd46 100644 --- a/src/font.rs +++ b/src/font.rs @@ -7,89 +7,39 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; +use core_foundation::base::{CFRelease, CFRetain, CFTypeID, TCFType}; use core_foundation::string::{CFString, CFStringRef}; use data_provider::CGDataProvider; + use foreign_types::ForeignType; use libc; -use std::mem; -use std::ptr; pub type CGGlyph = libc::c_ushort; -// This is an enum due to zero-sized types warnings. -// For more details see https://github.com/rust-lang/rust/issues/27303 -pub enum __CGFont {} - -pub type CGFontRef = *const __CGFont; - -pub struct CGFont { - obj: CGFontRef, +foreign_type! { + type CType = ::sys::CGFont; + fn drop = |p| CFRelease(p as *mut _); + fn clone = |p| CFRetain(p as *const _) as *mut _; + pub struct CGFont; + pub struct CGFontRef; } unsafe impl Send for CGFont {} unsafe impl Sync for CGFont {} -impl Clone for CGFont { - #[inline] - fn clone(&self) -> CGFont { - unsafe { - TCFType::wrap_under_get_rule(self.obj) - } - } -} - -impl Drop for CGFont { - fn drop(&mut self) { - unsafe { - let ptr = self.as_CFTypeRef(); - assert!(ptr != ptr::null()); - CFRelease(ptr); - } - } -} - -impl TCFType for CGFont { - #[inline] - fn as_concrete_TypeRef(&self) -> CGFontRef { - self.obj - } - - #[inline] - unsafe fn wrap_under_get_rule(reference: CGFontRef) -> CGFont { - let reference: CGFontRef = mem::transmute(CFRetain(mem::transmute(reference))); - TCFType::wrap_under_create_rule(reference) - } - - #[inline] - fn as_CFTypeRef(&self) -> CFTypeRef { - unsafe { - mem::transmute(self.as_concrete_TypeRef()) - } - } - - #[inline] - unsafe fn wrap_under_create_rule(obj: CGFontRef) -> CGFont { - CGFont { - obj: obj, - } - } - - #[inline] - fn type_id() -> CFTypeID { +impl CGFont { + pub fn type_id() -> CFTypeID { unsafe { CGFontGetTypeID() } } -} -impl CGFont { pub fn from_data_provider(provider: CGDataProvider) -> Result { unsafe { let font_ref = CGFontCreateWithDataProvider(provider.as_ptr()); - if font_ref != ptr::null() { - Ok(TCFType::wrap_under_create_rule(font_ref)) + if !font_ref.is_null() { + Ok(CGFont::from_ptr(font_ref)) } else { Err(()) } @@ -99,8 +49,8 @@ impl CGFont { pub fn from_name(name: &CFString) -> Result { unsafe { let font_ref = CGFontCreateWithFontName(name.as_concrete_TypeRef()); - if font_ref != ptr::null() { - Ok(TCFType::wrap_under_create_rule(font_ref)) + if !font_ref.is_null() { + Ok(CGFont::from_ptr(font_ref)) } else { Err(()) } @@ -109,7 +59,7 @@ impl CGFont { pub fn postscript_name(&self) -> CFString { unsafe { - let string_ref = CGFontCopyPostScriptName(self.obj); + let string_ref = CGFontCopyPostScriptName(self.as_ptr()); TCFType::wrap_under_create_rule(string_ref) } } @@ -118,14 +68,14 @@ impl CGFont { #[link(name = "ApplicationServices", kind = "framework")] extern { // TODO: basically nothing has bindings (even commented-out) besides what we use. - fn CGFontCreateWithDataProvider(provider: ::sys::CGDataProviderRef) -> CGFontRef; - fn CGFontCreateWithFontName(name: CFStringRef) -> CGFontRef; + fn CGFontCreateWithDataProvider(provider: ::sys::CGDataProviderRef) -> ::sys::CGFontRef; + fn CGFontCreateWithFontName(name: CFStringRef) -> ::sys::CGFontRef; fn CGFontGetTypeID() -> CFTypeID; - fn CGFontCopyPostScriptName(font: CGFontRef) -> CFStringRef; + fn CGFontCopyPostScriptName(font: ::sys::CGFontRef) -> CFStringRef; // These do the same thing as CFRetain/CFRelease, except // gracefully handle a NULL argument. We don't use them. - //fn CGFontRetain(font: CGFontRef); - //fn CGFontRelease(font: CGFontRef); + //fn CGFontRetain(font: ::sys::CGFontRef); + //fn CGFontRelease(font: ::sys::CGFontRef); } diff --git a/src/sys.rs b/src/sys.rs index 7d3c619..f709bb6 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -7,3 +7,7 @@ pub type CGColorSpaceRef = *mut CGColorSpace; pub enum CGDataProvider {} pub type CGDataProviderRef = *mut CGDataProvider; +pub enum CGFont {} +pub type CGFontRef = *mut CGFont; + + From 81a2c1dc9d31b68e5c5aa1370c11d26fe0a73880 Mon Sep 17 00:00:00 2001 From: Kornel Date: Thu, 14 Sep 2017 23:36:31 +0100 Subject: [PATCH 139/166] Use foreign-types for CGEvent --- src/event.rs | 121 +++++++++++++++------------------------------------ src/sys.rs | 3 ++ 2 files changed, 39 insertions(+), 85 deletions(-) diff --git a/src/event.rs b/src/event.rs index e734664..ce404c5 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,12 +1,12 @@ #![allow(non_upper_case_globals)] -use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; +use core_foundation::base::{CFRelease, CFRetain, CFTypeID, TCFType}; use geometry::CGPoint; use event_source::{CGEventSource,CGEventSourceRef}; use libc; -use std::mem; -use std::ptr; + +use foreign_types::ForeignType; pub type CGKeyCode = libc::uint16_t; @@ -91,75 +91,26 @@ pub enum CGEventTapLocation { AnnotatedSession, } -// This is an enum due to zero-sized types warnings. -// For more details see https://github.com/rust-lang/rust/issues/27303 -pub enum __CGEvent {} - -pub type CGEventRef = *const __CGEvent; - -pub struct CGEvent { - obj: CGEventRef, +foreign_type! { + type CType = ::sys::CGEvent; + fn drop = |p| CFRelease(p as *mut _); + fn clone = |p| CFRetain(p as *const _) as *mut _; + pub struct CGEvent; + pub struct CGEventRef; } -impl Clone for CGEvent { - #[inline] - fn clone(&self) -> CGEvent { - unsafe { - TCFType::wrap_under_get_rule(self.obj) - } - } -} - -impl Drop for CGEvent { - fn drop(&mut self) { - unsafe { - let ptr = self.as_CFTypeRef(); - assert!(ptr != ptr::null()); - CFRelease(ptr); - } - } -} - -impl TCFType for CGEvent { - #[inline] - fn as_concrete_TypeRef(&self) -> CGEventRef { - self.obj - } - - #[inline] - unsafe fn wrap_under_get_rule(reference: CGEventRef) -> CGEvent { - let reference: CGEventRef = mem::transmute(CFRetain(mem::transmute(reference))); - TCFType::wrap_under_create_rule(reference) - } - - #[inline] - fn as_CFTypeRef(&self) -> CFTypeRef { - unsafe { - mem::transmute(self.as_concrete_TypeRef()) - } - } - - #[inline] - unsafe fn wrap_under_create_rule(obj: CGEventRef) -> CGEvent { - CGEvent { - obj: obj, - } - } - - #[inline] - fn type_id() -> CFTypeID { +impl CGEvent { + pub fn type_id() -> CFTypeID { unsafe { CGEventGetTypeID() } } -} -impl CGEvent { pub fn new(source: CGEventSource) -> Result { unsafe { let event_ref = CGEventCreate(source.as_concrete_TypeRef()); - if event_ref != ptr::null() { - Ok(TCFType::wrap_under_create_rule(event_ref)) + if !event_ref.is_null() { + Ok(Self::from_ptr(event_ref)) } else { Err(()) } @@ -173,8 +124,8 @@ impl CGEvent { ) -> Result { unsafe { let event_ref = CGEventCreateKeyboardEvent(source.as_concrete_TypeRef(), keycode, keydown); - if event_ref != ptr::null() { - Ok(TCFType::wrap_under_create_rule(event_ref)) + if !event_ref.is_null() { + Ok(Self::from_ptr(event_ref)) } else { Err(()) } @@ -190,8 +141,8 @@ impl CGEvent { unsafe { let event_ref = CGEventCreateMouseEvent(source.as_concrete_TypeRef(), mouse_type, mouse_cursor_position, mouse_button); - if event_ref != ptr::null() { - Ok(TCFType::wrap_under_create_rule(event_ref)) + if !event_ref.is_null() { + Ok(Self::from_ptr(event_ref)) } else { Err(()) } @@ -200,13 +151,13 @@ impl CGEvent { pub fn post(&self, tap_location: CGEventTapLocation) { unsafe { - CGEventPost(tap_location, self.as_concrete_TypeRef()); + CGEventPost(tap_location, self.as_ptr()); } } pub fn location(&self) -> CGPoint { unsafe { - CGEventGetLocation(self.as_concrete_TypeRef()) + CGEventGetLocation(self.as_ptr()) } } @@ -219,32 +170,32 @@ impl CGEvent { pub fn set_flags(&self, flags: CGEventFlags) { unsafe { - CGEventSetFlags(self.as_concrete_TypeRef(), flags); + CGEventSetFlags(self.as_ptr(), flags); } } pub fn get_flags(&self) -> CGEventFlags { unsafe { - CGEventGetFlags(self.as_concrete_TypeRef()) + CGEventGetFlags(self.as_ptr()) } } pub fn set_type(&self, event_type: CGEventType) { unsafe { - CGEventSetType(self.as_concrete_TypeRef(), event_type); + CGEventSetType(self.as_ptr(), event_type); } } pub fn get_type(&self) -> CGEventType { unsafe { - CGEventGetType(self.as_concrete_TypeRef()) + CGEventGetType(self.as_ptr()) } } pub fn set_string_from_utf16_unchecked(&self, buf: &[u16]) { let buflen = buf.len() as libc::c_ulong; unsafe { - CGEventKeyboardSetUnicodeString(self.as_concrete_TypeRef(), buflen, buf.as_ptr()); + CGEventKeyboardSetUnicodeString(self.as_ptr(), buflen, buf.as_ptr()); } } @@ -261,7 +212,7 @@ extern { /// Return a new event using the event source `source'. If `source' is NULL, /// the default source is used. - fn CGEventCreate(source: CGEventSourceRef) -> CGEventRef; + fn CGEventCreate(source: CGEventSourceRef) -> ::sys::CGEventRef; /// Return a new keyboard event. /// @@ -274,7 +225,7 @@ extern { /// the SHIFT key must be down, the 'z' key must go down, and then the SHIFT /// and 'z' key must be released: fn CGEventCreateKeyboardEvent(source: CGEventSourceRef, keycode: CGKeyCode, - keydown: bool) -> CGEventRef; + keydown: bool) -> ::sys::CGEventRef; /// Return a new mouse event. /// @@ -290,34 +241,34 @@ extern { /// Mouse button 1 is the secondary mouse button (right). Mouse button 2 is /// the center button, and the remaining buttons are in USB device order. fn CGEventCreateMouseEvent(source: CGEventSourceRef, mouseType: CGEventType, - mouseCursorPosition: CGPoint, mouseButton: CGMouseButton) -> CGEventRef; + mouseCursorPosition: CGPoint, mouseButton: CGMouseButton) -> ::sys::CGEventRef; /// Post an event into the event stream at a specified location. /// /// This function posts the specified event immediately before any event taps /// instantiated for that location, and the event passes through any such /// taps. - fn CGEventPost(tapLocation: CGEventTapLocation, event: CGEventRef); + fn CGEventPost(tapLocation: CGEventTapLocation, event: ::sys::CGEventRef); #[cfg(feature = "elcapitan")] /// Post an event to a specified process ID - fn CGEventPostToPid(pid: libc::pid_t, event: CGEventRef); + fn CGEventPostToPid(pid: libc::pid_t, event: ::sys::CGEventRef); /// Set the event flags of an event. - fn CGEventSetFlags(event: CGEventRef, flags: CGEventFlags); + fn CGEventSetFlags(event: ::sys::CGEventRef, flags: CGEventFlags); /// Return the event flags of an event. - fn CGEventGetFlags(event: CGEventRef) -> CGEventFlags; + fn CGEventGetFlags(event: ::sys::CGEventRef) -> CGEventFlags; /// Return the location of an event in global display coordinates. - /// CGPointZero is returned if event is not a valid CGEventRef. - fn CGEventGetLocation(event: CGEventRef) -> CGPoint; + /// CGPointZero is returned if event is not a valid ::sys::CGEventRef. + fn CGEventGetLocation(event: ::sys::CGEventRef) -> CGPoint; /// Set the event type of an event. - fn CGEventSetType(event: CGEventRef, eventType: CGEventType); + fn CGEventSetType(event: ::sys::CGEventRef, eventType: CGEventType); /// Return the event type of an event (left mouse down, for example). - fn CGEventGetType(event: CGEventRef) -> CGEventType; + fn CGEventGetType(event: ::sys::CGEventRef) -> CGEventType; /// Set the Unicode string associated with a keyboard event. /// @@ -327,7 +278,7 @@ extern { /// Note that application frameworks may ignore the Unicode string in a /// keyboard event and do their own translation based on the virtual /// keycode and perceived event state. - fn CGEventKeyboardSetUnicodeString(event: CGEventRef, + fn CGEventKeyboardSetUnicodeString(event: ::sys::CGEventRef, length: libc::c_ulong, string: *const u16); } diff --git a/src/sys.rs b/src/sys.rs index f709bb6..e586073 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -10,4 +10,7 @@ pub type CGDataProviderRef = *mut CGDataProvider; pub enum CGFont {} pub type CGFontRef = *mut CGFont; +pub enum CGEvent {} +pub type CGEventRef = *mut CGEvent; + From 8c0664a542e06f98342cb983d684f4cdb52dfc9f Mon Sep 17 00:00:00 2001 From: Kornel Date: Fri, 15 Sep 2017 00:08:19 +0100 Subject: [PATCH 140/166] Hide foreign-types internals from docs --- src/color_space.rs | 1 + src/event.rs | 1 + src/font.rs | 1 + src/image.rs | 1 + 4 files changed, 4 insertions(+) diff --git a/src/color_space.rs b/src/color_space.rs index 4117084..9ff558c 100644 --- a/src/color_space.rs +++ b/src/color_space.rs @@ -11,6 +11,7 @@ use core_foundation::base::{CFRelease, CFRetain, CFTypeID}; use foreign_types::ForeignType; foreign_type! { + #[doc(hidden)] type CType = ::sys::CGColorSpace; fn drop = |p| CFRelease(p as *mut _); fn clone = |p| CFRetain(p as *const _) as *mut _; diff --git a/src/event.rs b/src/event.rs index ce404c5..e064258 100644 --- a/src/event.rs +++ b/src/event.rs @@ -92,6 +92,7 @@ pub enum CGEventTapLocation { } foreign_type! { + #[doc(hidden)] type CType = ::sys::CGEvent; fn drop = |p| CFRelease(p as *mut _); fn clone = |p| CFRetain(p as *const _) as *mut _; diff --git a/src/font.rs b/src/font.rs index 798dd46..9615d51 100644 --- a/src/font.rs +++ b/src/font.rs @@ -18,6 +18,7 @@ use libc; pub type CGGlyph = libc::c_ushort; foreign_type! { + #[doc(hidden)] type CType = ::sys::CGFont; fn drop = |p| CFRelease(p as *mut _); fn clone = |p| CFRetain(p as *const _) as *mut _; diff --git a/src/image.rs b/src/image.rs index c6b57a8..e5f238b 100644 --- a/src/image.rs +++ b/src/image.rs @@ -27,6 +27,7 @@ pub enum CGImageByteOrderInfo { } foreign_type! { + #[doc(hidden)] type CType = ::sys::CGImage; fn drop = CGImageRelease; fn clone = |p| CFRetain(p as *const _) as *mut _; From c86445c76ff2bc3c5a67f444e17acc089ea692c3 Mon Sep 17 00:00:00 2001 From: Kornel Date: Fri, 15 Sep 2017 00:38:06 +0100 Subject: [PATCH 141/166] Use foreign-types for CGContext --- src/context.rs | 143 +++++++++++++++++-------------------------------- src/sys.rs | 3 ++ 2 files changed, 52 insertions(+), 94 deletions(-) diff --git a/src/context.rs b/src/context.rs index 8a9ac3a..7009e6b 100644 --- a/src/context.rs +++ b/src/context.rs @@ -9,9 +9,9 @@ use base::CGFloat; use color_space::CGColorSpace; -use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; +use core_foundation::base::{CFRelease, CFRetain, CFTypeID}; use libc::{c_void, c_int, size_t}; -use std::mem; + use std::ptr; use std::slice; use geometry::CGRect; @@ -29,67 +29,22 @@ pub enum CGTextDrawingMode { CGTextClip } -// This is an enum due to zero-sized types warnings. -// For more details see https://github.com/rust-lang/rust/issues/27303 -pub enum __CGContext {} - -pub type CGContextRef = *const __CGContext; - -pub struct CGContext { - obj: CGContextRef, +foreign_type! { + #[doc(hidden)] + type CType = ::sys::CGContext; + fn drop = |cs| CFRelease(cs as *mut _); + fn clone = |p| CFRetain(p as *const _) as *mut _; + pub struct CGContext; + pub struct CGContextRef; } -impl Drop for CGContext { - fn drop(&mut self) { - unsafe { - CFRelease(self.as_CFTypeRef()) - } - } -} - -impl Clone for CGContext { - fn clone(&self) -> CGContext { - unsafe { - TCFType::wrap_under_get_rule(self.as_concrete_TypeRef()) - } - } -} - -impl TCFType for CGContext { - #[inline] - fn as_concrete_TypeRef(&self) -> CGContextRef { - self.obj - } - - #[inline] - unsafe fn wrap_under_get_rule(reference: CGContextRef) -> CGContext { - let reference: CGContextRef = mem::transmute(CFRetain(mem::transmute(reference))); - TCFType::wrap_under_create_rule(reference) - } - - #[inline] - fn as_CFTypeRef(&self) -> CFTypeRef { - unsafe { - mem::transmute(self.as_concrete_TypeRef()) - } - } - - #[inline] - unsafe fn wrap_under_create_rule(obj: CGContextRef) -> CGContext { - CGContext { - obj: obj, - } - } - - #[inline] - fn type_id() -> CFTypeID { +impl CGContext { + pub fn type_id() -> CFTypeID { unsafe { CGContextGetTypeID() } } -} -impl CGContext { pub fn create_bitmap_context(data: Option<*mut c_void>, width: size_t, height: size_t, @@ -107,116 +62,116 @@ impl CGContext { space.as_ptr(), bitmap_info); assert!(!result.is_null()); - TCFType::wrap_under_create_rule(result) + Self::from_ptr(result) } } pub fn data(&mut self) -> &mut [u8] { unsafe { slice::from_raw_parts_mut( - CGBitmapContextGetData(self.as_concrete_TypeRef()) as *mut u8, + CGBitmapContextGetData(self.as_ptr()) as *mut u8, (self.height() * self.bytes_per_row()) as usize) } } pub fn width(&self) -> size_t { unsafe { - CGBitmapContextGetWidth(self.as_concrete_TypeRef()) + CGBitmapContextGetWidth(self.as_ptr()) } } pub fn height(&self) -> size_t { unsafe { - CGBitmapContextGetHeight(self.as_concrete_TypeRef()) + CGBitmapContextGetHeight(self.as_ptr()) } } pub fn bytes_per_row(&self) -> size_t { unsafe { - CGBitmapContextGetBytesPerRow(self.as_concrete_TypeRef()) + CGBitmapContextGetBytesPerRow(self.as_ptr()) } } pub fn set_rgb_fill_color(&self, red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) { unsafe { - CGContextSetRGBFillColor(self.as_concrete_TypeRef(), red, green, blue, alpha) + CGContextSetRGBFillColor(self.as_ptr(), red, green, blue, alpha) } } pub fn set_allows_font_smoothing(&self, allows_font_smoothing: bool) { unsafe { - CGContextSetAllowsFontSmoothing(self.as_concrete_TypeRef(), allows_font_smoothing) + CGContextSetAllowsFontSmoothing(self.as_ptr(), allows_font_smoothing) } } pub fn set_font_smoothing_style(&self, style: i32) { unsafe { - CGContextSetFontSmoothingStyle(self.as_concrete_TypeRef(), style as _); + CGContextSetFontSmoothingStyle(self.as_ptr(), style as _); } } pub fn set_should_smooth_fonts(&self, should_smooth_fonts: bool) { unsafe { - CGContextSetShouldSmoothFonts(self.as_concrete_TypeRef(), should_smooth_fonts) + CGContextSetShouldSmoothFonts(self.as_ptr(), should_smooth_fonts) } } pub fn set_allows_antialiasing(&self, allows_antialiasing: bool) { unsafe { - CGContextSetAllowsAntialiasing(self.as_concrete_TypeRef(), allows_antialiasing) + CGContextSetAllowsAntialiasing(self.as_ptr(), allows_antialiasing) } } pub fn set_should_antialias(&self, should_antialias: bool) { unsafe { - CGContextSetShouldAntialias(self.as_concrete_TypeRef(), should_antialias) + CGContextSetShouldAntialias(self.as_ptr(), should_antialias) } } pub fn set_allows_font_subpixel_quantization(&self, allows_font_subpixel_quantization: bool) { unsafe { - CGContextSetAllowsFontSubpixelQuantization(self.as_concrete_TypeRef(), allows_font_subpixel_quantization) + CGContextSetAllowsFontSubpixelQuantization(self.as_ptr(), allows_font_subpixel_quantization) } } pub fn set_should_subpixel_quantize_fonts(&self, should_subpixel_quantize_fonts: bool) { unsafe { - CGContextSetShouldSubpixelQuantizeFonts(self.as_concrete_TypeRef(), should_subpixel_quantize_fonts) + CGContextSetShouldSubpixelQuantizeFonts(self.as_ptr(), should_subpixel_quantize_fonts) } } pub fn set_allows_font_subpixel_positioning(&self, allows_font_subpixel_positioning: bool) { unsafe { - CGContextSetAllowsFontSubpixelPositioning(self.as_concrete_TypeRef(), allows_font_subpixel_positioning) + CGContextSetAllowsFontSubpixelPositioning(self.as_ptr(), allows_font_subpixel_positioning) } } pub fn set_should_subpixel_position_fonts(&self, should_subpixel_position_fonts: bool) { unsafe { - CGContextSetShouldSubpixelPositionFonts(self.as_concrete_TypeRef(), should_subpixel_position_fonts) + CGContextSetShouldSubpixelPositionFonts(self.as_ptr(), should_subpixel_position_fonts) } } pub fn set_text_drawing_mode(&self, mode: CGTextDrawingMode) { unsafe { - CGContextSetTextDrawingMode(self.as_concrete_TypeRef(), mode) + CGContextSetTextDrawingMode(self.as_ptr(), mode) } } pub fn fill_rect(&self, rect: CGRect) { unsafe { - CGContextFillRect(self.as_concrete_TypeRef(), rect) + CGContextFillRect(self.as_ptr(), rect) } } pub fn draw_image(&self, rect: CGRect, image: &CGImage) { unsafe { - CGContextDrawImage(self.as_concrete_TypeRef(), rect, image.as_ptr()); + CGContextDrawImage(self.as_ptr(), rect, image.as_ptr()); } } pub fn create_image(&self) -> Option { - let image = unsafe { CGBitmapContextCreateImage(self.as_concrete_TypeRef()) }; + let image = unsafe { CGBitmapContextCreateImage(self.as_ptr()) }; if !image.is_null() { Some(unsafe { CGImage::from_ptr(image) }) } else { @@ -258,34 +213,34 @@ extern { bytesPerRow: size_t, space: ::sys::CGColorSpaceRef, bitmapInfo: u32) - -> CGContextRef; - fn CGBitmapContextGetData(context: CGContextRef) -> *mut c_void; - fn CGBitmapContextGetWidth(context: CGContextRef) -> size_t; - fn CGBitmapContextGetHeight(context: CGContextRef) -> size_t; - fn CGBitmapContextGetBytesPerRow(context: CGContextRef) -> size_t; - fn CGBitmapContextCreateImage(context: CGContextRef) -> ::sys::CGImageRef; + -> ::sys::CGContextRef; + fn CGBitmapContextGetData(context: ::sys::CGContextRef) -> *mut c_void; + fn CGBitmapContextGetWidth(context: ::sys::CGContextRef) -> size_t; + fn CGBitmapContextGetHeight(context: ::sys::CGContextRef) -> size_t; + fn CGBitmapContextGetBytesPerRow(context: ::sys::CGContextRef) -> size_t; + fn CGBitmapContextCreateImage(context: ::sys::CGContextRef) -> ::sys::CGImageRef; fn CGContextGetTypeID() -> CFTypeID; - fn CGContextSetAllowsFontSmoothing(c: CGContextRef, allowsFontSmoothing: bool); - fn CGContextSetShouldSmoothFonts(c: CGContextRef, shouldSmoothFonts: bool); - fn CGContextSetFontSmoothingStyle(c: CGContextRef, style: c_int); - fn CGContextSetAllowsAntialiasing(c: CGContextRef, allowsAntialiasing: bool); - fn CGContextSetShouldAntialias(c: CGContextRef, shouldAntialias: bool); - fn CGContextSetAllowsFontSubpixelQuantization(c: CGContextRef, + fn CGContextSetAllowsFontSmoothing(c: ::sys::CGContextRef, allowsFontSmoothing: bool); + fn CGContextSetShouldSmoothFonts(c: ::sys::CGContextRef, shouldSmoothFonts: bool); + fn CGContextSetFontSmoothingStyle(c: ::sys::CGContextRef, style: c_int); + fn CGContextSetAllowsAntialiasing(c: ::sys::CGContextRef, allowsAntialiasing: bool); + fn CGContextSetShouldAntialias(c: ::sys::CGContextRef, shouldAntialias: bool); + fn CGContextSetAllowsFontSubpixelQuantization(c: ::sys::CGContextRef, allowsFontSubpixelQuantization: bool); - fn CGContextSetShouldSubpixelQuantizeFonts(c: CGContextRef, + fn CGContextSetShouldSubpixelQuantizeFonts(c: ::sys::CGContextRef, shouldSubpixelQuantizeFonts: bool); - fn CGContextSetAllowsFontSubpixelPositioning(c: CGContextRef, + fn CGContextSetAllowsFontSubpixelPositioning(c: ::sys::CGContextRef, allowsFontSubpixelPositioning: bool); - fn CGContextSetShouldSubpixelPositionFonts(c: CGContextRef, + fn CGContextSetShouldSubpixelPositionFonts(c: ::sys::CGContextRef, shouldSubpixelPositionFonts: bool); - fn CGContextSetTextDrawingMode(c: CGContextRef, mode: CGTextDrawingMode); - fn CGContextSetRGBFillColor(context: CGContextRef, + fn CGContextSetTextDrawingMode(c: ::sys::CGContextRef, mode: CGTextDrawingMode); + fn CGContextSetRGBFillColor(context: ::sys::CGContextRef, red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat); - fn CGContextFillRect(context: CGContextRef, + fn CGContextFillRect(context: ::sys::CGContextRef, rect: CGRect); - fn CGContextDrawImage(c: CGContextRef, rect: CGRect, image: ::sys::CGImageRef); + fn CGContextDrawImage(c: ::sys::CGContextRef, rect: CGRect, image: ::sys::CGImageRef); } diff --git a/src/sys.rs b/src/sys.rs index e586073..e7dd44f 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -13,4 +13,7 @@ pub type CGFontRef = *mut CGFont; pub enum CGEvent {} pub type CGEventRef = *mut CGEvent; +pub enum CGContext {} +pub type CGContextRef = *mut CGContext; + From 40ee246becf4098f6eec13cecc2f2f974093056e Mon Sep 17 00:00:00 2001 From: Kornel Date: Fri, 15 Sep 2017 00:41:43 +0100 Subject: [PATCH 142/166] Use foreign-types for CGEventSource --- src/event.rs | 16 ++++----- src/event_source.rs | 80 +++++++++------------------------------------ src/sys.rs | 3 ++ 3 files changed, 26 insertions(+), 73 deletions(-) diff --git a/src/event.rs b/src/event.rs index e064258..c3ef9bc 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,8 +1,8 @@ #![allow(non_upper_case_globals)] -use core_foundation::base::{CFRelease, CFRetain, CFTypeID, TCFType}; +use core_foundation::base::{CFRelease, CFRetain, CFTypeID}; use geometry::CGPoint; -use event_source::{CGEventSource,CGEventSourceRef}; +use event_source::CGEventSource; use libc; @@ -109,7 +109,7 @@ impl CGEvent { pub fn new(source: CGEventSource) -> Result { unsafe { - let event_ref = CGEventCreate(source.as_concrete_TypeRef()); + let event_ref = CGEventCreate(source.as_ptr()); if !event_ref.is_null() { Ok(Self::from_ptr(event_ref)) } else { @@ -124,7 +124,7 @@ impl CGEvent { keydown: bool ) -> Result { unsafe { - let event_ref = CGEventCreateKeyboardEvent(source.as_concrete_TypeRef(), keycode, keydown); + let event_ref = CGEventCreateKeyboardEvent(source.as_ptr(), keycode, keydown); if !event_ref.is_null() { Ok(Self::from_ptr(event_ref)) } else { @@ -140,7 +140,7 @@ impl CGEvent { mouse_button: CGMouseButton ) -> Result { unsafe { - let event_ref = CGEventCreateMouseEvent(source.as_concrete_TypeRef(), mouse_type, + let event_ref = CGEventCreateMouseEvent(source.as_ptr(), mouse_type, mouse_cursor_position, mouse_button); if !event_ref.is_null() { Ok(Self::from_ptr(event_ref)) @@ -213,7 +213,7 @@ extern { /// Return a new event using the event source `source'. If `source' is NULL, /// the default source is used. - fn CGEventCreate(source: CGEventSourceRef) -> ::sys::CGEventRef; + fn CGEventCreate(source: ::sys::CGEventSourceRef) -> ::sys::CGEventRef; /// Return a new keyboard event. /// @@ -225,7 +225,7 @@ extern { /// SHIFT, CONTROL, OPTION, and COMMAND keys. For example, to produce a 'Z', /// the SHIFT key must be down, the 'z' key must go down, and then the SHIFT /// and 'z' key must be released: - fn CGEventCreateKeyboardEvent(source: CGEventSourceRef, keycode: CGKeyCode, + fn CGEventCreateKeyboardEvent(source: ::sys::CGEventSourceRef, keycode: CGKeyCode, keydown: bool) -> ::sys::CGEventRef; /// Return a new mouse event. @@ -241,7 +241,7 @@ extern { /// thirty-two buttons. Mouse button 0 is the primary button on the mouse. /// Mouse button 1 is the secondary mouse button (right). Mouse button 2 is /// the center button, and the remaining buttons are in USB device order. - fn CGEventCreateMouseEvent(source: CGEventSourceRef, mouseType: CGEventType, + fn CGEventCreateMouseEvent(source: ::sys::CGEventSourceRef, mouseType: CGEventType, mouseCursorPosition: CGPoint, mouseButton: CGMouseButton) -> ::sys::CGEventRef; /// Post an event into the event stream at a specified location. diff --git a/src/event_source.rs b/src/event_source.rs index c38aca9..b4e96c3 100644 --- a/src/event_source.rs +++ b/src/event_source.rs @@ -1,7 +1,5 @@ -use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; - -use std::mem; -use std::ptr; +use core_foundation::base::{CFRelease, CFRetain, CFTypeID}; +use foreign_types::ForeignType; /// Possible source states of an event source. #[repr(C)] @@ -12,75 +10,27 @@ pub enum CGEventSourceStateID { HIDSystemState = 1, } -// This is an enum due to zero-sized types warnings. -// For more details see https://github.com/rust-lang/rust/issues/27303 -pub enum __CGEventSource {} - -pub type CGEventSourceRef = *const __CGEventSource; - -pub struct CGEventSource { - obj: CGEventSourceRef, +foreign_type! { + #[doc(hidden)] + type CType = ::sys::CGEventSource; + fn drop = |p| CFRelease(p as *mut _); + fn clone = |p| CFRetain(p as *const _) as *mut _; + pub struct CGEventSource; + pub struct CGEventSourceRef; } -impl Clone for CGEventSource { - #[inline] - fn clone(&self) -> CGEventSource { - unsafe { - TCFType::wrap_under_get_rule(self.obj) - } - } -} - -impl Drop for CGEventSource { - fn drop(&mut self) { - unsafe { - let ptr = self.as_CFTypeRef(); - assert!(ptr != ptr::null()); - CFRelease(ptr); - } - } -} - -impl TCFType for CGEventSource { - #[inline] - fn as_concrete_TypeRef(&self) -> CGEventSourceRef { - self.obj - } - - #[inline] - unsafe fn wrap_under_get_rule(reference: CGEventSourceRef) -> CGEventSource { - let reference: CGEventSourceRef = mem::transmute(CFRetain(mem::transmute(reference))); - TCFType::wrap_under_create_rule(reference) - } - - #[inline] - fn as_CFTypeRef(&self) -> CFTypeRef { - unsafe { - mem::transmute(self.as_concrete_TypeRef()) - } - } - - #[inline] - unsafe fn wrap_under_create_rule(obj: CGEventSourceRef) -> CGEventSource { - CGEventSource { - obj: obj, - } - } - - #[inline] - fn type_id() -> CFTypeID { +impl CGEventSource { + pub fn type_id() -> CFTypeID { unsafe { CGEventSourceGetTypeID() } } -} -impl CGEventSource { - pub fn new(state_id: CGEventSourceStateID) -> Result { + pub fn new(state_id: CGEventSourceStateID) -> Result { unsafe { let event_source_ref = CGEventSourceCreate(state_id); - if event_source_ref != ptr::null() { - Ok(TCFType::wrap_under_create_rule(event_source_ref)) + if !event_source_ref.is_null() { + Ok(Self::from_ptr(event_source_ref)) } else { Err(()) } @@ -94,5 +44,5 @@ extern { fn CGEventSourceGetTypeID() -> CFTypeID; /// Return a Quartz event source created with a specified source state. - fn CGEventSourceCreate(stateID: CGEventSourceStateID) -> CGEventSourceRef; + fn CGEventSourceCreate(stateID: CGEventSourceStateID) -> ::sys::CGEventSourceRef; } diff --git a/src/sys.rs b/src/sys.rs index e7dd44f..513b6e9 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -13,6 +13,9 @@ pub type CGFontRef = *mut CGFont; pub enum CGEvent {} pub type CGEventRef = *mut CGEvent; +pub enum CGEventSource {} +pub type CGEventSourceRef = *mut CGEventSource; + pub enum CGContext {} pub type CGContextRef = *mut CGContext; From a7e05d2e3804cdca9fde5305e8e9e77c6d3aab07 Mon Sep 17 00:00:00 2001 From: Kornel Date: Sat, 16 Sep 2017 02:32:47 +0100 Subject: [PATCH 143/166] Missed change --- src/event.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/event.rs b/src/event.rs index c3ef9bc..5682dae 100644 --- a/src/event.rs +++ b/src/event.rs @@ -165,7 +165,7 @@ impl CGEvent { #[cfg(feature = "elcapitan")] pub fn post_to_pid(&self, pid: libc::pid_t) { unsafe { - CGEventPostToPid(pid, self.as_concrete_TypeRef()); + CGEventPostToPid(pid, self.as_ptr()); } } From df2bd7ea4faf8fcf0617ad5d0621609c61f4abf8 Mon Sep 17 00:00:00 2001 From: Andrew McCreight Date: Tue, 19 Sep 2017 14:18:00 -0700 Subject: [PATCH 144/166] Fix typo in event.rs --- src/event.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/event.rs b/src/event.rs index d94b270..2d4f435 100644 --- a/src/event.rs +++ b/src/event.rs @@ -283,7 +283,7 @@ extern { /// is ignored unless `mouseType' is one of `kCGEventOtherMouseDown', /// `kCGEventOtherMouseDragged', or `kCGEventOtherMouseUp'. /// - /// The current implemementation of the event system supports a maximum of + /// The current implementation of the event system supports a maximum of /// thirty-two buttons. Mouse button 0 is the primary button on the mouse. /// Mouse button 1 is the secondary mouse button (right). Mouse button 2 is /// the center button, and the remaining buttons are in USB device order. From 3091be3a44b6cecf492b08c5430f282adc94d977 Mon Sep 17 00:00:00 2001 From: Kornel Date: Fri, 15 Sep 2017 01:02:16 +0100 Subject: [PATCH 145/166] Named color spaces --- src/color_space.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/color_space.rs b/src/color_space.rs index 9ff558c..dec48ed 100644 --- a/src/color_space.rs +++ b/src/color_space.rs @@ -8,6 +8,7 @@ // except according to those terms. use core_foundation::base::{CFRelease, CFRetain, CFTypeID}; +use core_foundation::string::CFStringRef; use foreign_types::ForeignType; foreign_type! { @@ -26,6 +27,13 @@ impl CGColorSpace { } } + pub fn create_with_name(name: CFStringRef) -> Option { + unsafe { + let p = CGColorSpaceCreateWithName(name); + if !p.is_null() {Some(CGColorSpace::from_ptr(p))} else {None} + } + } + pub fn create_device_rgb() -> CGColorSpace { unsafe { let result = CGColorSpaceCreateDeviceRGB(); @@ -36,7 +44,16 @@ impl CGColorSpace { #[link(name = "ApplicationServices", kind = "framework")] extern { + pub static kCGColorSpaceSRGB: CFStringRef; + pub static kCGColorSpaceAdobeRGB1998: CFStringRef; + pub static kCGColorSpaceGenericGray: CFStringRef; + pub static kCGColorSpaceGenericRGB: CFStringRef; + pub static kCGColorSpaceGenericCMYK: CFStringRef; + pub static kCGColorSpaceGenericRGBLinear: CFStringRef; + pub static kCGColorSpaceGenericGrayGamma2_2: CFStringRef; + fn CGColorSpaceCreateDeviceRGB() -> ::sys::CGColorSpaceRef; + fn CGColorSpaceCreateWithName(name: CFStringRef) -> ::sys::CGColorSpaceRef; fn CGColorSpaceGetTypeID() -> CFTypeID; } From 35312c56db192557711e60d92bdc3ac7170b2780 Mon Sep 17 00:00:00 2001 From: Michael Sanders Date: Thu, 28 Sep 2017 22:21:14 +0530 Subject: [PATCH 146/166] Add universal key code constants --- src/event.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/event.rs b/src/event.rs index d94b270..d2251b3 100644 --- a/src/event.rs +++ b/src/event.rs @@ -35,6 +35,62 @@ bitflags! { } } +/// Key codes for keys that are independent of keyboard layout. +/// +/// [Ref](https://github.com/phracker/MacOSX-SDKs/blob/master/MacOSX10.13.sdk/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h) +#[repr(C)] +pub struct KeyCode; +impl KeyCode { + pub const RETURN: CGKeyCode = 0x24; + pub const TAB: CGKeyCode = 0x30; + pub const SPACE: CGKeyCode = 0x31; + pub const DELETE: CGKeyCode = 0x33; + pub const ESCAPE: CGKeyCode = 0x35; + pub const COMMAND: CGKeyCode = 0x37; + pub const SHIFT: CGKeyCode = 0x38; + pub const CAPS_LOCK: CGKeyCode = 0x39; + pub const OPTION: CGKeyCode = 0x3A; + pub const CONTROL: CGKeyCode = 0x3B; + pub const RIGHT_COMMAND: CGKeyCode = 0x36; + pub const RIGHT_SHIFT: CGKeyCode = 0x3C; + pub const RIGHT_OPTION: CGKeyCode = 0x3D; + pub const RIGHT_CONTROL: CGKeyCode = 0x3E; + pub const FUNCTION: CGKeyCode = 0x3F; + pub const VOLUME_UP: CGKeyCode = 0x48; + pub const VOLUME_DOWN: CGKeyCode = 0x49; + pub const MUTE: CGKeyCode = 0x4A; + pub const F1: CGKeyCode = 0x7A; + pub const F2: CGKeyCode = 0x78; + pub const F3: CGKeyCode = 0x63; + pub const F4: CGKeyCode = 0x76; + pub const F5: CGKeyCode = 0x60; + pub const F6: CGKeyCode = 0x61; + pub const F7: CGKeyCode = 0x62; + pub const F8: CGKeyCode = 0x64; + pub const F9: CGKeyCode = 0x65; + pub const F10: CGKeyCode = 0x6D; + pub const F11: CGKeyCode = 0x67; + pub const F12: CGKeyCode = 0x6F; + pub const F13: CGKeyCode = 0x69; + pub const F14: CGKeyCode = 0x6B; + pub const F15: CGKeyCode = 0x71; + pub const F16: CGKeyCode = 0x6A; + pub const F17: CGKeyCode = 0x40; + pub const F18: CGKeyCode = 0x4F; + pub const F19: CGKeyCode = 0x50; + pub const F20: CGKeyCode = 0x5A; + pub const HELP: CGKeyCode = 0x72; + pub const HOME: CGKeyCode = 0x73; + pub const PAGE_UP: CGKeyCode = 0x74; + pub const FORWARD_DELETE: CGKeyCode = 0x75; + pub const END: CGKeyCode = 0x77; + pub const PAGE_DOWN: CGKeyCode = 0x79; + pub const LEFT_ARROW: CGKeyCode = 0x7B; + pub const RIGHT_ARROW: CGKeyCode = 0x7C; + pub const DOWN_ARROW: CGKeyCode = 0x7D; + pub const UP_ARROW: CGKeyCode = 0x7E; +} + /// Constants that specify the different types of input events. /// /// [Ref](http://opensource.apple.com/source/IOHIDFamily/IOHIDFamily-700/IOHIDSystem/IOKit/hidsystem/IOLLEvent.h) From 2eaf8b5b038365a54ced16c0b3946cbda151f168 Mon Sep 17 00:00:00 2001 From: Michael Sanders Date: Sun, 8 Oct 2017 04:03:39 +0530 Subject: [PATCH 147/166] Add support for CGEvent value fields --- src/event.rs | 278 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 278 insertions(+) diff --git a/src/event.rs b/src/event.rs index 84136fd..45a9cca 100644 --- a/src/event.rs +++ b/src/event.rs @@ -8,6 +8,7 @@ use libc; use foreign_types::ForeignType; +pub type CGEventField = libc::uint32_t; pub type CGKeyCode = libc::uint16_t; /// Flags for events @@ -73,6 +74,234 @@ pub enum CGEventType { TapDisabledByUserInput = 0xFFFFFFFF, } +/// Constants used as keys to access specialized fields in low-level events. +/// +/// [Ref](https://developer.apple.com/documentation/coregraphics/cgeventfield) +pub struct EventField; +impl EventField { + /// Key to access an integer field that contains the mouse button event + /// number. Matching mouse-down and mouse-up events will have the same + /// event number. + pub const MOUSE_EVENT_NUMBER: CGEventField = 0; + + /// Key to access an integer field that contains the mouse button click + /// state. A click state of 1 represents a single click. A click state of + /// 2 represents a double-click. A click state of 3 represents a + /// triple-click. + pub const MOUSE_EVENT_CLICK_STATE: CGEventField = 1; + + /// Key to access a double field that contains the mouse button pressure. + /// The pressure value may range from 0 to 1, with 0 representing the + /// mouse being up. This value is commonly set by tablet pens mimicking a + /// mouse. + pub const MOUSE_EVENT_PRESSURE: CGEventField = 2; + + /// Key to access an integer field that contains the mouse button number. + pub const MOUSE_EVENT_BUTTON_NUMBER: CGEventField = 3; + + /// Key to access an integer field that contains the horizontal mouse + /// delta since the last mouse movement event. + pub const MOUSE_EVENT_DELTA_X: CGEventField = 4; + + /// Key to access an integer field that contains the vertical mouse delta + /// since the last mouse movement event. + pub const MOUSE_EVENT_DELTA_Y: CGEventField = 5; + + /// Key to access an integer field. The value is non-zero if the event + /// should be ignored by the Inkwell subsystem. + pub const MOUSE_EVENT_INSTANT_MOUSER: CGEventField = 6; + + /// Key to access an integer field that encodes the mouse event subtype as + /// a `kCFNumberIntType'. + pub const MOUSE_EVENT_SUB_TYPE: CGEventField = 7; + + /// Key to access an integer field, non-zero when this is an autorepeat of + /// a key-down, and zero otherwise. + pub const KEYBOARD_EVENT_AUTOREPEAT: CGEventField = 8; + + /// Key to access an integer field that contains the virtual keycode of the + /// key-down or key-up event. + pub const KEYBOARD_EVENT_KEYCODE: CGEventField = 9; + + /// Key to access an integer field that contains the keyboard type + /// identifier. + pub const KEYBOARD_EVENT_KEYBOARD_TYPE: CGEventField = 10; + + /// Key to access an integer field that contains scrolling data. This field + /// typically contains the change in vertical position since the last + /// scrolling event from a Mighty Mouse scroller or a single-wheel mouse + /// scroller. + pub const SCROLL_WHEEL_EVENT_DELTA_AXIS_1: CGEventField = 11; + + /// Key to access an integer field that contains scrolling data. This field + /// typically contains the change in horizontal position since the last + /// scrolling event from a Mighty Mouse scroller. + pub const SCROLL_WHEEL_EVENT_DELTA_AXIS_2: CGEventField = 12; + + /// Key to access a field that contains scrolling data. The scrolling data + /// represents a line-based or pixel-based change in vertical position + /// since the last scrolling event from a Mighty Mouse scroller or a + /// single-wheel mouse scroller. The scrolling data uses a fixed-point + /// 16.16 signed integer format. If this key is passed to + /// `CGEventGetDoubleValueField', the fixed-point value is converted to a + /// double value. + pub const SCROLL_WHEEL_EVENT_FIXED_POINT_DELTA_AXIS_1: CGEventField = 93; + + /// Key to access a field that contains scrolling data. The scrolling data + /// represents a line-based or pixel-based change in horizontal position + /// since the last scrolling event from a Mighty Mouse scroller. The + /// scrolling data uses a fixed-point 16.16 signed integer format. If this + /// key is passed to `CGEventGetDoubleValueField', the fixed-point value is + /// converted to a double value. + pub const SCROLL_WHEEL_EVENT_FIXED_POINT_DELTA_AXIS_2: CGEventField = 94; + + /// Key to access an integer field that contains pixel-based scrolling + /// data. The scrolling data represents the change in vertical position + /// since the last scrolling event from a Mighty Mouse scroller or a + /// single-wheel mouse scroller. + pub const SCROLL_WHEEL_EVENT_POINT_DELTA_AXIS_1: CGEventField = 96; + + /// Key to access an integer field that contains pixel-based scrolling + /// data. The scrolling data represents the change in horizontal position + /// since the last scrolling event from a Mighty Mouse scroller. + pub const SCROLL_WHEEL_EVENT_POINT_DELTA_AXIS_2: CGEventField = 97; + + /// Key to access an integer field that indicates whether the event should + /// be ignored by the Inkwell subsystem. If the value is non-zero, the + /// event should be ignored. + pub const SCROLL_WHEEL_EVENT_INSTANT_MOUSER: CGEventField = 14; + + /// Key to access an integer field that contains the absolute X coordinate + /// in tablet space at full tablet resolution. + pub const TABLET_EVENT_POINT_X: CGEventField = 15; + + /// Key to access an integer field that contains the absolute Y coordinate + /// in tablet space at full tablet resolution. + pub const TABLET_EVENT_POINT_Y: CGEventField = 16; + + /// Key to access an integer field that contains the absolute Z coordinate + /// in tablet space at full tablet resolution. + pub const TABLET_EVENT_POINT_Z: CGEventField = 17; + + /// Key to access an integer field that contains the tablet button state. + /// Bit 0 is the first button, and a set bit represents a closed or pressed + /// button. Up to 16 buttons are supported. + pub const TABLET_EVENT_POINT_BUTTONS: CGEventField = 18; + + /// Key to access a double field that contains the tablet pen pressure. A + /// value of 0.0 represents no pressure, and 1.0 represents maximum + /// pressure. + pub const TABLET_EVENT_POINT_PRESSURE: CGEventField = 19; + + /// Key to access a double field that contains the horizontal tablet pen + /// tilt. A value of 0 represents no tilt, and 1 represents maximum tilt. + pub const TABLET_EVENT_TILT_X: CGEventField = 20; + + /// Key to access a double field that contains the vertical tablet pen + /// tilt. A value of 0 represents no tilt, and 1 represents maximum tilt. + pub const TABLET_EVENT_TILT_Y: CGEventField = 21; + + /// Key to access a double field that contains the tablet pen rotation. + pub const TABLET_EVENT_ROTATION: CGEventField = 22; + + /// Key to access a double field that contains the tangential pressure on + /// the device. A value of 0.0 represents no pressure, and 1.0 represents + /// maximum pressure. + pub const TABLET_EVENT_TANGENTIAL_PRESSURE: CGEventField = 23; + + /// Key to access an integer field that contains the system-assigned unique + /// device ID. + pub const TABLET_EVENT_DEVICE_ID: CGEventField = 24; + + /// Key to access an integer field that contains a vendor-specified value. + pub const TABLET_EVENT_VENDOR_1: CGEventField = 25; + + /// Key to access an integer field that contains a vendor-specified value. + pub const TABLET_EVENT_VENDOR_2: CGEventField = 26; + + /// Key to access an integer field that contains a vendor-specified value. + pub const TABLET_EVENT_VENDOR_3: CGEventField = 27; + + /// Key to access an integer field that contains the vendor-defined ID, + /// typically the USB vendor ID. + pub const TABLET_PROXIMITY_EVENT_VENDOR_ID: CGEventField = 28; + + /// Key to access an integer field that contains the vendor-defined tablet + /// ID, typically the USB product ID. + pub const TABLET_PROXIMITY_EVENT_TABLET_ID: CGEventField = 29; + + /// Key to access an integer field that contains the vendor-defined ID of + /// the pointing device. + pub const TABLET_PROXIMITY_EVENT_POINTER_ID: CGEventField = 30; + + /// Key to access an integer field that contains the system-assigned + /// device ID. + pub const TABLET_PROXIMITY_EVENT_DEVICE_ID: CGEventField = 31; + + /// Key to access an integer field that contains the system-assigned + /// unique tablet ID. + pub const TABLET_PROXIMITY_EVENT_SYSTEM_TABLET_ID: CGEventField = 32; + + /// Key to access an integer field that contains the vendor-assigned + /// pointer type. + pub const TABLET_PROXIMITY_EVENT_VENDOR_POINTER_TYPE: CGEventField = 33; + + /// Key to access an integer field that contains the vendor-defined + /// pointer serial number. + pub const TABLET_PROXIMITY_EVENT_VENDOR_POINTER_SERIAL_NUMBER: CGEventField = 34; + + /// Key to access an integer field that contains the vendor-defined unique + /// ID. + pub const TABLET_PROXIMITY_EVENT_VENDOR_UNIQUE_ID: CGEventField = 35; + + /// Key to access an integer field that contains the device capabilities + /// mask. + pub const TABLET_PROXIMITY_EVENT_CAPABILITY_MASK: CGEventField = 36; + + /// Key to access an integer field that contains the pointer type. + pub const TABLET_PROXIMITY_EVENT_POINTER_TYPE: CGEventField = 37; + + /// Key to access an integer field that indicates whether the pen is in + /// proximity to the tablet. The value is non-zero if the pen is in + /// proximity to the tablet and zero when leaving the tablet. + pub const TABLET_PROXIMITY_EVENT_ENTER_PROXIMITY: CGEventField = 38; + + /// Key to access a field that contains the event target process serial + /// number. The value is a 64-bit value. + pub const EVENT_TARGET_PROCESS_SERIAL_NUMBER: CGEventField = 39; + + /// Key to access a field that contains the event target Unix process ID. + pub const EVENT_TARGET_UNIX_PROCESS_ID: CGEventField = 40; + + /// Key to access a field that contains the event source Unix process ID. + pub const EVENT_SOURCE_UNIX_PROCESS_ID: CGEventField = 41; + + /// Key to access a field that contains the event source user-supplied + /// data, up to 64 bits. + pub const EVENT_SOURCE_USER_DATA: CGEventField = 42; + + /// Key to access a field that contains the event source Unix effective UID. + pub const EVENT_SOURCE_USER_ID: CGEventField = 43; + + /// Key to access a field that contains the event source Unix effective + /// GID. + pub const EVENT_SOURCE_GROUP_ID: CGEventField = 44; + + /// Key to access a field that contains the event source state ID used to + /// create this event. + pub const EVENT_SOURCE_STATE_ID: CGEventField = 45; + + /// Key to access an integer field that indicates whether a scrolling event + /// contains continuous, pixel-based scrolling data. The value is non-zero + /// when the scrolling data is pixel-based and zero when the scrolling data + /// is line-based. + pub const SCROLL_WHEEL_EVENT_IS_CONTINUOUS: CGEventField = 88; + + /// Added in 10.5; made public in 10.7. + pub const MOUSE_EVENT_WINDOW_UNDER_MOUSE_POINTER: CGEventField = 91; + pub const MOUSE_EVENT_WINDOW_UNDER_MOUSE_POINTER_THAT_CAN_HANDLE_THIS_EVENT: CGEventField = 92; +} + // Constants that specify buttons on a one, two, or three-button mouse. #[repr(C)] #[derive(Clone, Copy, Debug)] @@ -204,6 +433,22 @@ impl CGEvent { let buf: Vec = string.encode_utf16().collect(); self.set_string_from_utf16_unchecked(&buf); } + + pub fn get_integer_value_field(&self, field: CGEventField) -> i64 { + unsafe { CGEventGetIntegerValueField(self.as_ptr(), field) } + } + + pub fn set_integer_value_field(&self, field: CGEventField, value: i64) { + unsafe { CGEventSetIntegerValueField(self.as_ptr(), field, value) } + } + + pub fn get_double_value_field(&self, field: CGEventField) -> f64 { + unsafe { CGEventGetDoubleValueField(self.as_ptr(), field) } + } + + pub fn set_double_value_field(&self, field: CGEventField, value: f64) { + unsafe { CGEventSetDoubleValueField(self.as_ptr(), field, value) } + } } #[link(name = "ApplicationServices", kind = "framework")] @@ -282,4 +527,37 @@ extern { fn CGEventKeyboardSetUnicodeString(event: ::sys::CGEventRef, length: libc::c_ulong, string: *const u16); + + /// Return the integer value of a field in an event. + fn CGEventGetIntegerValueField(event: ::sys::CGEventRef, field: CGEventField) -> i64; + + /// Set the integer value of a field in an event. + /// + /// Before calling this function, the event type must be set using a typed + /// event creation function such as `CGEventCreateMouseEvent', or by + /// calling `CGEventSetType'. + /// + /// If you are creating a mouse event generated by a tablet, call this + /// function and specify the field `kCGMouseEventSubtype' with a value of + /// `kCGEventMouseSubtypeTabletPoint' or + /// `kCGEventMouseSubtypeTabletProximity' before setting other parameters. + fn CGEventSetIntegerValueField(event: ::sys::CGEventRef, field: CGEventField, value: i64); + + /// Return the floating-point value of a field in an event. + /// + /// In cases where the field value is represented within the event by a fixed + /// point number or an integer, the result is scaled to the appropriate range + /// as part of creating the floating-point representation. + fn CGEventGetDoubleValueField(event: ::sys::CGEventRef, field: CGEventField) -> f64; + + /// Set the floating-point value of a field in an event. + /// + /// Before calling this function, the event type must be set using a typed + /// event creation function such as `CGEventCreateMouseEvent', or by calling + /// `CGEventSetType'. + /// + /// In cases where the field’s value is represented within the event by a + /// fixed point number or integer, the value parameter is scaled as needed + /// and converted to the appropriate type. + fn CGEventSetDoubleValueField(event: ::sys::CGEventRef, field: CGEventField, value: f64); } From 12a8f0c0a6b6fdc7a6ccffd18d360eb8ce139c43 Mon Sep 17 00:00:00 2001 From: Michael Sanders Date: Sun, 1 Oct 2017 21:35:41 +0530 Subject: [PATCH 148/166] Add support for CGDisplayMode & safe CGDisplay wrapper --- src/display.rs | 455 ++++++++++++++++++++++++++++++++++++++++++++----- src/sys.rs | 3 + 2 files changed, 418 insertions(+), 40 deletions(-) diff --git a/src/display.rs b/src/display.rs index 345c040..8b105b3 100644 --- a/src/display.rs +++ b/src/display.rs @@ -10,9 +10,14 @@ #![allow(non_upper_case_globals)] use libc; +use std::ptr; pub use base::{CGError, boolean_t}; pub use geometry::{CGRect, CGPoint, CGSize}; +use core_foundation::base::{CFRetain, TCFType}; +use image::CGImage; +use foreign_types::ForeignType; + pub type CGDirectDisplayID = libc::uint32_t; pub type CGWindowID = libc::uint32_t; @@ -42,52 +47,422 @@ pub use core_foundation::array::{ CFArray, CFArrayRef }; pub use core_foundation::array::{ CFArrayGetCount, CFArrayGetValueAtIndex }; pub use core_foundation::base::{ CFIndex, CFRelease, CFTypeRef }; +#[derive(Copy, Clone, Debug)] +pub struct CGDisplay { + pub id: CGDirectDisplayID, +} + +foreign_type! { + #[doc(hidden)] + type CType = ::sys::CGDisplayMode; + fn drop = CGDisplayModeRelease; + fn clone = |p| CFRetain(p as *const _) as *mut _; + pub struct CGDisplayMode; + pub struct CGDisplayModeRef; +} + +impl CGDisplay { + #[inline] + pub fn new(id: CGDirectDisplayID) -> CGDisplay { + CGDisplay { id: id } + } + + /// Returns the the main display. + #[inline] + pub fn main() -> CGDisplay { + CGDisplay::new(unsafe { CGMainDisplayID() }) + } + + /// Returns the bounds of a display in the global display coordinate space. + #[inline] + pub fn bounds(&self) -> CGRect { + unsafe { CGDisplayBounds(self.id) } + } + + /// Returns information about a display's current configuration. + #[inline] + pub fn display_mode(&self) -> Option { + unsafe { + let mode_ref = CGDisplayCopyDisplayMode(self.id); + if !mode_ref.is_null() { + Some(CGDisplayMode::from_ptr(mode_ref)) + } else { + None + } + } + } + + /// Returns an image containing the contents of the specified display. + #[inline] + pub fn image(&self) -> Option { + unsafe { + let image_ref = CGDisplayCreateImage(self.id); + if !image_ref.is_null() { + Some(CGImage::from_ptr(image_ref)) + } else { + None + } + } + } + + /// Returns a composite image based on a dynamically generated list of + /// windows. + #[inline] + pub fn screenshot( + bounds: CGRect, + list_option: CGWindowListOption, + window_id: CGWindowID, + image_option: CGWindowImageOption, + ) -> Option { + unsafe { + let image_ref = CGWindowListCreateImage(bounds, list_option, window_id, image_option); + if !image_ref.is_null() { + Some(CGImage::from_ptr(image_ref)) + } else { + None + } + } + } + + /// Returns a composite image of the specified windows. + #[inline] + pub fn screenshot_from_windows( + bounds: CGRect, + windows: CFArray, + image_option: CGWindowImageOption, + ) -> Option { + unsafe { + let image_ref = CGWindowListCreateImageFromArray( + bounds, + windows.as_concrete_TypeRef(), + image_option, + ); + if !image_ref.is_null() { + Some(CGImage::from_ptr(image_ref)) + } else { + None + } + } + } + + /// Generates and returns information about the selected windows in the + /// current user session. + pub fn window_list_info( + option: CGWindowListOption, + relative_to_window: Option, + ) -> Option { + let relative_to_window = relative_to_window.unwrap_or(kCGNullWindowID); + let array_ref = unsafe { CGWindowListCopyWindowInfo(option, relative_to_window) }; + if array_ref != ptr::null() { + Some(unsafe { TCFType::wrap_under_create_rule(array_ref) }) + } else { + None + } + } + + /// Returns a Boolean value indicating whether a display is active. + #[inline] + pub fn is_active(&self) -> bool { + unsafe { CGDisplayIsActive(self.id) != 0 } + } + + /// Returns a boolean indicating whether a display is always in a + /// mirroring set. + #[inline] + pub fn is_always_in_mirror_set(&self) -> bool { + unsafe { CGDisplayIsAlwaysInMirrorSet(self.id) != 0 } + } + + /// Returns a boolean indicating whether a display is sleeping (and is + /// therefore not drawable.) + #[inline] + pub fn is_asleep(&self) -> bool { + unsafe { CGDisplayIsAsleep(self.id) != 0 } + } + + /// Returns a boolean indicating whether a display is built-in, such as + /// the internal display in portable systems. + #[inline] + pub fn is_builtin(&self) -> bool { + unsafe { CGDisplayIsBuiltin(self.id) != 0 } + } + + /// Returns a boolean indicating whether a display is in a hardware + /// mirroring set. + #[inline] + pub fn is_in_hw_mirror_set(&self) -> bool { + unsafe { CGDisplayIsInHWMirrorSet(self.id) != 0 } + } + + /// Returns a boolean indicating whether a display is in a mirroring set. + #[inline] + pub fn is_in_mirror_set(&self) -> bool { + unsafe { CGDisplayIsInMirrorSet(self.id) != 0 } + } + + /// Returns a boolean indicating whether a display is the main display. + #[inline] + pub fn is_main(&self) -> bool { + unsafe { CGDisplayIsMain(self.id) != 0 } + } + + /// Returns a boolean indicating whether a display is connected or online. + #[inline] + pub fn is_online(&self) -> bool { + unsafe { CGDisplayIsOnline(self.id) != 0 } + } + + /// Returns a boolean indicating whether Quartz is using OpenGL-based + /// window acceleration (Quartz Extreme) to render in a display. + #[inline] + pub fn uses_open_gl_acceleration(&self) -> bool { + unsafe { CGDisplayUsesOpenGLAcceleration(self.id) != 0 } + } + + /// Returns a boolean indicating whether a display is running in a stereo + /// graphics mode. + #[inline] + pub fn is_stereo(&self) -> bool { + unsafe { CGDisplayIsStereo(self.id) != 0 } + } + + /// For a secondary display in a mirroring set, returns the primary + /// display. + #[inline] + pub fn mirrors_display(&self) -> CGDirectDisplayID { + unsafe { CGDisplayMirrorsDisplay(self.id) } + } + + /// Returns the primary display in a hardware mirroring set. + #[inline] + pub fn primary_display(&self) -> CGDirectDisplayID { + unsafe { CGDisplayPrimaryDisplay(self.id) } + } + + /// Returns the rotation angle of a display in degrees. + #[inline] + pub fn rotation(&self) -> f64 { + unsafe { CGDisplayRotation(self.id) } + } + + /// Returns the width and height of a display in millimeters. + #[inline] + pub fn screen_size(&self) -> CGSize { + unsafe { CGDisplayScreenSize(self.id) } + } + + /// Returns the serial number of a display monitor. + #[inline] + pub fn serial_number(&self) -> u32 { + unsafe { CGDisplaySerialNumber(self.id) } + } + + /// Returns the logical unit number of a display. + #[inline] + pub fn unit_number(&self) -> u32 { + unsafe { CGDisplayUnitNumber(self.id) } + } + + /// Returns the vendor number of the specified display's monitor. + #[inline] + pub fn vendor_number(&self) -> u32 { + unsafe { CGDisplayVendorNumber(self.id) } + } + + /// Returns the model number of a display monitor. + #[inline] + pub fn model_number(&self) -> u32 { + unsafe { CGDisplayModelNumber(self.id) } + } + + /// Returns the display height in pixel units. + #[inline] + pub fn pixels_high(&self) -> u64 { + unsafe { CGDisplayPixelsHigh(self.id) as u64 } + } + + /// Returns the display width in pixel units. + #[inline] + pub fn pixels_wide(&self) -> u64 { + unsafe { CGDisplayPixelsWide(self.id) as u64 } + } + + /// Provides a list of displays that are active (or drawable). + #[inline] + pub fn active_displays() -> Result, CGError> { + let count = try!(CGDisplay::active_display_count()); + let mut buf: Vec = vec![0; count as usize]; + let result = + unsafe { CGGetActiveDisplayList(count as u32, buf.as_mut_ptr(), ptr::null_mut()) }; + if result == 0 { + Ok(buf) + } else { + Err(result) + } + } + + /// Provides count of displays that are active (or drawable). + #[inline] + pub fn active_display_count() -> Result { + let mut count: libc::uint32_t = 0; + let result = unsafe { CGGetActiveDisplayList(0, ptr::null_mut(), &mut count) }; + if result == 0 { + Ok(count as u32) + } else { + Err(result) + } + } + + /// Hides the mouse cursor, and increments the hide cursor count. + #[inline] + pub fn hide_cursor(&self) -> Result<(), CGError> { + let result = unsafe { CGDisplayHideCursor(self.id) }; + if result == 0 { + Ok(()) + } else { + Err(result) + } + } + + /// Decrements the hide cursor count, and shows the mouse cursor if the + /// count is 0. + #[inline] + pub fn show_cursor(&self) -> Result<(), CGError> { + let result = unsafe { CGDisplayShowCursor(self.id) }; + if result == 0 { + Ok(()) + } else { + Err(result) + } + } + + /// Moves the mouse cursor to a specified point relative to the display + /// origin (the upper-left corner of the display). + #[inline] + pub fn move_cursor_to_point(&self, point: CGPoint) -> Result<(), CGError> { + let result = unsafe { CGDisplayMoveCursorToPoint(self.id, point) }; + if result == 0 { + Ok(()) + } else { + Err(result) + } + } + + /// Moves the mouse cursor without generating events. + #[inline] + pub fn warp_mouse_cursor_position(point: CGPoint) -> Result<(), CGError> { + let result = unsafe { CGWarpMouseCursorPosition(point) }; + if result == 0 { + Ok(()) + } else { + Err(result) + } + } + + /// Connects or disconnects the mouse and cursor while an application is + /// in the foreground. + #[inline] + pub fn associate_mouse_and_mouse_cursor_position(connected: bool) -> Result<(), CGError> { + let result = unsafe { CGAssociateMouseAndMouseCursorPosition(connected as boolean_t) }; + if result == 0 { + Ok(()) + } else { + Err(result) + } + } +} + +impl CGDisplayMode { + #[inline] + pub fn height(&self) -> u64 { + unsafe { CGDisplayModeGetHeight(self.as_ptr()) as u64 } + } + + #[inline] + pub fn width(&self) -> u64 { + unsafe { CGDisplayModeGetWidth(self.as_ptr()) as u64 } + } + + #[inline] + pub fn pixel_height(&self) -> u64 { + unsafe { CGDisplayModeGetPixelHeight(self.as_ptr()) as u64 } + } + + #[inline] + pub fn pixel_width(&self) -> u64 { + unsafe { CGDisplayModeGetPixelWidth(self.as_ptr()) as u64 } + } + + #[inline] + pub fn refresh_rate(&self) -> f64 { + unsafe { CGDisplayModeGetRefreshRate(self.as_ptr()) } + } +} + #[link(name = "ApplicationServices", kind = "framework")] -extern { +extern "C" { pub static CGRectNull: CGRect; pub static CGRectInfinite: CGRect; - pub fn CGMainDisplayID() -> CGDirectDisplayID; - pub fn CGDisplayIsActive(display: CGDirectDisplayID) -> boolean_t; - pub fn CGDisplayIsAlwaysInMirrorSet(display: CGDirectDisplayID) -> boolean_t; - pub fn CGDisplayIsAsleep(display: CGDirectDisplayID) -> boolean_t; - pub fn CGDisplayIsBuiltin(display: CGDirectDisplayID) -> boolean_t; - pub fn CGDisplayIsInHWMirrorSet(display: CGDirectDisplayID) -> boolean_t; - pub fn CGDisplayIsInMirrorSet(display: CGDirectDisplayID) -> boolean_t; - pub fn CGDisplayIsMain(display: CGDirectDisplayID) -> boolean_t; - pub fn CGDisplayIsOnline(display: CGDirectDisplayID) -> boolean_t; - pub fn CGDisplayIsStereo(display: CGDirectDisplayID) -> boolean_t; - pub fn CGDisplayMirrorsDisplay(display: CGDirectDisplayID) -> CGDirectDisplayID; - pub fn CGDisplayPrimaryDisplay(display: CGDirectDisplayID) -> CGDirectDisplayID; - pub fn CGDisplayRotation(display: CGDirectDisplayID) -> libc::c_double; - pub fn CGDisplayScreenSize(display: CGDirectDisplayID) -> CGSize; - pub fn CGDisplaySerialNumber(display: CGDirectDisplayID) -> libc::uint32_t; - pub fn CGDisplayUnitNumber(display: CGDirectDisplayID) -> libc::uint32_t; - pub fn CGDisplayUsesOpenGLAcceleration(display: CGDirectDisplayID) -> boolean_t; - pub fn CGDisplayVendorNumber(display: CGDirectDisplayID) -> libc::uint32_t; - pub fn CGGetActiveDisplayList(max_displays: libc::uint32_t, - active_displays: *mut CGDirectDisplayID, - display_count: *mut libc::uint32_t) -> CGError; - pub fn CGDisplayModelNumber(display: CGDirectDisplayID) -> libc::uint32_t; - pub fn CGDisplayPixelsHigh(display: CGDirectDisplayID) -> libc::size_t; - pub fn CGDisplayPixelsWide(display: CGDirectDisplayID) -> libc::size_t; - pub fn CGDisplayBounds(display: CGDirectDisplayID) -> CGRect; - pub fn CGDisplayCreateImage(display: CGDirectDisplayID) -> ::sys::CGImageRef; + fn CGDisplayModeRelease(mode: ::sys::CGDisplayModeRef); + + fn CGMainDisplayID() -> CGDirectDisplayID; + fn CGDisplayIsActive(display: CGDirectDisplayID) -> boolean_t; + fn CGDisplayIsAlwaysInMirrorSet(display: CGDirectDisplayID) -> boolean_t; + fn CGDisplayIsAsleep(display: CGDirectDisplayID) -> boolean_t; + fn CGDisplayIsBuiltin(display: CGDirectDisplayID) -> boolean_t; + fn CGDisplayIsInHWMirrorSet(display: CGDirectDisplayID) -> boolean_t; + fn CGDisplayIsInMirrorSet(display: CGDirectDisplayID) -> boolean_t; + fn CGDisplayIsMain(display: CGDirectDisplayID) -> boolean_t; + fn CGDisplayIsOnline(display: CGDirectDisplayID) -> boolean_t; + fn CGDisplayIsStereo(display: CGDirectDisplayID) -> boolean_t; + fn CGDisplayMirrorsDisplay(display: CGDirectDisplayID) -> CGDirectDisplayID; + fn CGDisplayPrimaryDisplay(display: CGDirectDisplayID) -> CGDirectDisplayID; + fn CGDisplayRotation(display: CGDirectDisplayID) -> libc::c_double; + fn CGDisplayScreenSize(display: CGDirectDisplayID) -> CGSize; + fn CGDisplaySerialNumber(display: CGDirectDisplayID) -> libc::uint32_t; + fn CGDisplayUnitNumber(display: CGDirectDisplayID) -> libc::uint32_t; + fn CGDisplayUsesOpenGLAcceleration(display: CGDirectDisplayID) -> boolean_t; + fn CGDisplayVendorNumber(display: CGDirectDisplayID) -> libc::uint32_t; + fn CGGetActiveDisplayList( + max_displays: libc::uint32_t, + active_displays: *mut CGDirectDisplayID, + display_count: *mut libc::uint32_t, + ) -> CGError; + fn CGDisplayModelNumber(display: CGDirectDisplayID) -> libc::uint32_t; + fn CGDisplayPixelsHigh(display: CGDirectDisplayID) -> libc::size_t; + fn CGDisplayPixelsWide(display: CGDirectDisplayID) -> libc::size_t; + fn CGDisplayBounds(display: CGDirectDisplayID) -> CGRect; + fn CGDisplayCreateImage(display: CGDirectDisplayID) -> ::sys::CGImageRef; + + fn CGDisplayCopyDisplayMode(display: CGDirectDisplayID) -> ::sys::CGDisplayModeRef; + fn CGDisplayModeGetHeight(mode: ::sys::CGDisplayModeRef) -> libc::size_t; + fn CGDisplayModeGetWidth(mode: ::sys::CGDisplayModeRef) -> libc::size_t; + fn CGDisplayModeGetPixelHeight(mode: ::sys::CGDisplayModeRef) -> libc::size_t; + fn CGDisplayModeGetPixelWidth(mode: ::sys::CGDisplayModeRef) -> libc::size_t; + fn CGDisplayModeGetRefreshRate(mode: ::sys::CGDisplayModeRef) -> libc::c_double; // mouse stuff - pub fn CGDisplayHideCursor(display: CGDirectDisplayID) -> CGError; - pub fn CGDisplayShowCursor(display: CGDirectDisplayID) -> CGError; - pub fn CGDisplayMoveCursorToPoint(display: CGDirectDisplayID, point: CGPoint) -> CGError; - pub fn CGWarpMouseCursorPosition(point: CGPoint) -> CGError; - pub fn CGAssociateMouseAndMouseCursorPosition(connected: bool) -> CGError; + fn CGDisplayHideCursor(display: CGDirectDisplayID) -> CGError; + fn CGDisplayShowCursor(display: CGDirectDisplayID) -> CGError; + fn CGDisplayMoveCursorToPoint(display: CGDirectDisplayID, point: CGPoint) -> CGError; + fn CGWarpMouseCursorPosition(point: CGPoint) -> CGError; + fn CGAssociateMouseAndMouseCursorPosition(connected: boolean_t) -> CGError; // Window Services Reference - pub fn CGWindowListCopyWindowInfo(option: CGWindowListOption, relativeToWindow: CGWindowID ) -> CFArrayRef; - pub fn CGWindowListCreateImage(screenBounds: CGRect, - listOptions: CGWindowListOption, - windowId: CGWindowID, - imageOptions: CGWindowImageOption) -> ::sys::CGImageRef; - pub fn CGWindowListCreateImageFromArray(screenBounds: CGRect, - windowArray: CFArrayRef, - imageOptions: CGWindowImageOption) -> ::sys::CGImageRef; + fn CGWindowListCopyWindowInfo( + option: CGWindowListOption, + relativeToWindow: CGWindowID, + ) -> CFArrayRef; + fn CGWindowListCreateImage( + screenBounds: CGRect, + listOptions: CGWindowListOption, + windowId: CGWindowID, + imageOptions: CGWindowImageOption, + ) -> ::sys::CGImageRef; + fn CGWindowListCreateImageFromArray( + screenBounds: CGRect, + windowArray: CFArrayRef, + imageOptions: CGWindowImageOption, + ) -> ::sys::CGImageRef; } diff --git a/src/sys.rs b/src/sys.rs index 513b6e9..3542af5 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -19,4 +19,7 @@ pub type CGEventSourceRef = *mut CGEventSource; pub enum CGContext {} pub type CGContextRef = *mut CGContext; +pub enum CGDisplayMode {} +pub type CGDisplayModeRef = *mut CGDisplayMode; + From 8f3f6b34aab04c5943ae1311721950d8e36045af Mon Sep 17 00:00:00 2001 From: Bastien Orivel Date: Thu, 12 Oct 2017 11:12:59 +0200 Subject: [PATCH 149/166] Update bitflags to 1.0 and bump version --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 08bc30d..222f77e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.9.1" +version = "0.10.0" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" @@ -12,7 +12,7 @@ default = [] elcapitan = [] [dependencies] -bitflags = "0.9" +bitflags = "1.0" core-foundation = "0.4" foreign-types = "0.3.0" libc = "0.2" From 82331e7736c9b51a67b6a8b1df2ba29749869da9 Mon Sep 17 00:00:00 2001 From: ratake <32758555+ratake@users.noreply.github.com> Date: Mon, 16 Oct 2017 22:50:52 -0700 Subject: [PATCH 150/166] Add support for iOS targets Only include CGEvent and CGDisplay on macOS --- src/color_space.rs | 2 +- src/context.rs | 2 +- src/data_provider.rs | 2 +- src/display.rs | 2 +- src/event.rs | 2 +- src/event_source.rs | 2 +- src/font.rs | 2 +- src/geometry.rs | 2 +- src/image.rs | 2 +- src/lib.rs | 5 +++++ src/private.rs | 2 +- src/sys.rs | 20 ++++++++++++-------- 12 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/color_space.rs b/src/color_space.rs index dec48ed..67f2504 100644 --- a/src/color_space.rs +++ b/src/color_space.rs @@ -42,7 +42,7 @@ impl CGColorSpace { } } -#[link(name = "ApplicationServices", kind = "framework")] +#[link(name = "CoreGraphics", kind = "framework")] extern { pub static kCGColorSpaceSRGB: CFStringRef; pub static kCGColorSpaceAdobeRGB1998: CFStringRef; diff --git a/src/context.rs b/src/context.rs index 7009e6b..d2d626b 100644 --- a/src/context.rs +++ b/src/context.rs @@ -204,7 +204,7 @@ fn create_bitmap_context_test() { assert_eq!(255, data.bytes()[3]); } -#[link(name = "ApplicationServices", kind = "framework")] +#[link(name = "CoreGraphics", kind = "framework")] extern { fn CGBitmapContextCreate(data: *mut c_void, width: size_t, diff --git a/src/data_provider.rs b/src/data_provider.rs index ac2cb5a..7262934 100644 --- a/src/data_provider.rs +++ b/src/data_provider.rs @@ -61,7 +61,7 @@ impl CGDataProviderRef { } } -#[link(name = "ApplicationServices", kind = "framework")] +#[link(name = "CoreGraphics", kind = "framework")] extern { fn CGDataProviderCopyData(provider: ::sys::CGDataProviderRef) -> CFDataRef; //fn CGDataProviderCreateDirect diff --git a/src/display.rs b/src/display.rs index 8b105b3..f440c1d 100644 --- a/src/display.rs +++ b/src/display.rs @@ -399,7 +399,7 @@ impl CGDisplayMode { } } -#[link(name = "ApplicationServices", kind = "framework")] +#[link(name = "CoreGraphics", kind = "framework")] extern "C" { pub static CGRectNull: CGRect; pub static CGRectInfinite: CGRect; diff --git a/src/event.rs b/src/event.rs index 29f9fed..b49fc12 100644 --- a/src/event.rs +++ b/src/event.rs @@ -507,7 +507,7 @@ impl CGEvent { } } -#[link(name = "ApplicationServices", kind = "framework")] +#[link(name = "CoreGraphics", kind = "framework")] extern { /// Return the type identifier for the opaque type `CGEventRef'. fn CGEventGetTypeID() -> CFTypeID; diff --git a/src/event_source.rs b/src/event_source.rs index b4e96c3..ce13f5a 100644 --- a/src/event_source.rs +++ b/src/event_source.rs @@ -38,7 +38,7 @@ impl CGEventSource { } } -#[link(name = "ApplicationServices", kind = "framework")] +#[link(name = "CoreGraphics", kind = "framework")] extern { /// Return the type identifier for the opaque type `CGEventSourceRef'. fn CGEventSourceGetTypeID() -> CFTypeID; diff --git a/src/font.rs b/src/font.rs index 9615d51..fbdc38a 100644 --- a/src/font.rs +++ b/src/font.rs @@ -66,7 +66,7 @@ impl CGFont { } } -#[link(name = "ApplicationServices", kind = "framework")] +#[link(name = "CoreGraphics", kind = "framework")] extern { // TODO: basically nothing has bindings (even commented-out) besides what we use. fn CGFontCreateWithDataProvider(provider: ::sys::CGDataProviderRef) -> ::sys::CGFontRef; diff --git a/src/geometry.rs b/src/geometry.rs index 9f165b2..9b60b36 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -108,7 +108,7 @@ mod ffi { use geometry::CGRect; use core_foundation::dictionary::CFDictionaryRef; - #[link(name = "ApplicationServices", kind = "framework")] + #[link(name = "CoreGraphics", kind = "framework")] extern { pub fn CGRectInset(rect: CGRect, dx: CGFloat, dy: CGFloat) -> CGRect; pub fn CGRectMakeWithDictionaryRepresentation(dict: CFDictionaryRef, diff --git a/src/image.rs b/src/image.rs index e5f238b..93eb807 100644 --- a/src/image.rs +++ b/src/image.rs @@ -92,7 +92,7 @@ impl CGImageRef { } } -#[link(name = "ApplicationServices", kind = "framework")] +#[link(name = "CoreGraphics", kind = "framework")] extern { fn CGImageGetTypeID() -> CFTypeID; fn CGImageGetWidth(image: ::sys::CGImageRef) -> size_t; diff --git a/src/lib.rs b/src/lib.rs index 61caacb..0c0d9f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ extern crate libc; extern crate core_foundation; #[macro_use] +#[cfg(target_os = "macos")] extern crate bitflags; #[macro_use] @@ -20,11 +21,15 @@ pub mod base; pub mod color_space; pub mod context; pub mod data_provider; +#[cfg(target_os = "macos")] pub mod display; +#[cfg(target_os = "macos")] pub mod event; +#[cfg(target_os = "macos")] pub mod event_source; pub mod font; pub mod geometry; +#[cfg(target_os = "macos")] pub mod private; pub mod image; mod sys; diff --git a/src/private.rs b/src/private.rs index 11231a9..0bc193b 100644 --- a/src/private.rs +++ b/src/private.rs @@ -97,7 +97,7 @@ mod ffi { pub type CGSRegionRef = *mut CGSRegionObject; pub type OSStatus = i32; - #[link(name = "ApplicationServices", kind = "framework")] + #[link(name = "CoreGraphics", kind = "framework")] extern { pub fn CGSRegionRelease(region: CGSRegionRef); pub fn CGSNewRegionWithRect(rect: *const CGRect, outRegion: *mut CGSRegionRef) -> CGError; diff --git a/src/sys.rs b/src/sys.rs index 3542af5..317dd07 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -10,16 +10,20 @@ pub type CGDataProviderRef = *mut CGDataProvider; pub enum CGFont {} pub type CGFontRef = *mut CGFont; -pub enum CGEvent {} -pub type CGEventRef = *mut CGEvent; - -pub enum CGEventSource {} -pub type CGEventSourceRef = *mut CGEventSource; - pub enum CGContext {} pub type CGContextRef = *mut CGContext; -pub enum CGDisplayMode {} -pub type CGDisplayModeRef = *mut CGDisplayMode; +#[cfg(target_os = "macos")] +mod macos { + pub enum CGEvent {} + pub type CGEventRef = *mut CGEvent; + pub enum CGEventSource {} + pub type CGEventSourceRef = *mut CGEventSource; + pub enum CGDisplayMode {} + pub type CGDisplayModeRef = *mut CGDisplayMode; +} + +#[cfg(target_os = "macos")] +pub use self::macos::*; From 5e8f34b93639c87258f96b7d7e8556503baa3faf Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 19 Oct 2017 20:30:06 -0700 Subject: [PATCH 151/166] Flesh out `geometry` a bit more --- src/base.rs | 4 ---- src/geometry.rs | 32 +++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/base.rs b/src/base.rs index 5b8e71c..f5c954d 100644 --- a/src/base.rs +++ b/src/base.rs @@ -28,10 +28,6 @@ pub type CGFloat = libc::c_float; pub type CGError = libc::int32_t; -// This is an enum due to zero-sized types warnings. -// For more details see https://github.com/rust-lang/rust/issues/27303 -pub enum CGAffineTransform {} - pub const kCGImageAlphaNone: u32 = 0; pub const kCGImageAlphaPremultipliedLast: u32 = 1; pub const kCGImageAlphaPremultipliedFirst: u32 = 2; diff --git a/src/geometry.rs b/src/geometry.rs index 9b60b36..2955394 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -16,8 +16,24 @@ pub const CG_ZERO_POINT: CGPoint = CGPoint { y: 0.0, }; +pub const CG_ZERO_SIZE: CGSize = CGSize { + width: 0.0, + height: 0.0, +}; + +pub const CG_ZERO_RECT: CGRect = CGRect { + origin: CG_ZERO_POINT, + size: CG_ZERO_SIZE, +}; + +pub const CG_AFFINE_TRANSFORM_IDENTITY: CGAffineTransform = CGAffineTransform { + a: 1.0, b: 0.0, + c: 0.0, d: 1.0, + tx: 0.0, ty: 0.0, +}; + #[repr(C)] -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, Default)] pub struct CGSize { pub width: CGFloat, pub height: CGFloat, @@ -34,7 +50,7 @@ impl CGSize { } #[repr(C)] -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, Default)] pub struct CGPoint { pub x: CGFloat, pub y: CGFloat, @@ -51,7 +67,7 @@ impl CGPoint { } #[repr(C)] -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, Default)] pub struct CGRect { pub origin: CGPoint, pub size: CGSize @@ -103,6 +119,16 @@ impl CGRect { } } +#[repr(C)] +pub struct CGAffineTransform { + pub a: CGFloat, + pub b: CGFloat, + pub c: CGFloat, + pub d: CGFloat, + pub tx: CGFloat, + pub ty: CGFloat, +} + mod ffi { use base::{CGFloat, boolean_t}; use geometry::CGRect; From 63811809fd280653c1f924825f97e0dc30c36671 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 19 Oct 2017 20:31:01 -0700 Subject: [PATCH 152/166] Add bindings to `CGFontGetGlyphBBoxes()` and `CGFontGetGlyphAdvances()` --- src/font.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/font.rs b/src/font.rs index fbdc38a..d4cf297 100644 --- a/src/font.rs +++ b/src/font.rs @@ -10,10 +10,11 @@ use core_foundation::base::{CFRelease, CFRetain, CFTypeID, TCFType}; use core_foundation::string::{CFString, CFStringRef}; use data_provider::CGDataProvider; +use geometry::CGRect; use foreign_types::ForeignType; -use libc; +use libc::{self, c_int, size_t}; pub type CGGlyph = libc::c_ushort; @@ -64,6 +65,26 @@ impl CGFont { TCFType::wrap_under_create_rule(string_ref) } } + + pub fn get_glyph_b_boxes(&self, glyphs: &[CGGlyph], bboxes: &mut [CGRect]) -> bool { + unsafe { + assert!(bboxes.len() >= glyphs.len()); + CGFontGetGlyphBBoxes(self.as_ptr(), + glyphs.as_ptr(), + glyphs.len(), + bboxes.as_mut_ptr()) + } + } + + pub fn get_glyph_advances(&self, glyphs: &[CGGlyph], advances: &mut [c_int]) -> bool { + unsafe { + assert!(advances.len() >= glyphs.len()); + CGFontGetGlyphAdvances(self.as_ptr(), + glyphs.as_ptr(), + glyphs.len(), + advances.as_mut_ptr()) + } + } } #[link(name = "CoreGraphics", kind = "framework")] @@ -79,4 +100,15 @@ extern { // gracefully handle a NULL argument. We don't use them. //fn CGFontRetain(font: ::sys::CGFontRef); //fn CGFontRelease(font: ::sys::CGFontRef); + + fn CGFontGetGlyphBBoxes(font: ::sys::CGFontRef, + glyphs: *const CGGlyph, + count: size_t, + bboxes: *mut CGRect) + -> bool; + fn CGFontGetGlyphAdvances(font: ::sys::CGFontRef, + glyphs: *const CGGlyph, + count: size_t, + advances: *mut c_int) + -> bool; } From abe37e92e90b456dfd9d4c1204da4810d9215533 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 19 Oct 2017 20:31:10 -0700 Subject: [PATCH 153/166] Add `CGPath` bindings --- src/lib.rs | 1 + src/path.rs | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/sys.rs | 3 ++ 3 files changed, 119 insertions(+) create mode 100644 src/path.rs diff --git a/src/lib.rs b/src/lib.rs index 0c0d9f7..01854e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,4 +32,5 @@ pub mod geometry; #[cfg(target_os = "macos")] pub mod private; pub mod image; +pub mod path; mod sys; diff --git a/src/path.rs b/src/path.rs new file mode 100644 index 0000000..4e60c59 --- /dev/null +++ b/src/path.rs @@ -0,0 +1,115 @@ +// Copyright 2017 The Servo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use core_foundation::base::{CFRelease, CFRetain, CFTypeID}; +use foreign_types::ForeignType; +use geometry::CGPoint; +use libc::c_void; +use std::fmt::{self, Debug, Formatter}; +use std::marker::PhantomData; +use std::ops::Deref; +use std::slice; + +foreign_type! { + #[doc(hidden)] + type CType = ::sys::CGPath; + fn drop = |p| CFRelease(p as *mut _); + fn clone = |p| CFRetain(p as *const _) as *mut _; + pub struct CGPath; + pub struct CGPathRef; +} + +impl CGPath { + pub fn type_id() -> CFTypeID { + unsafe { + CGPathGetTypeID() + } + } + + pub fn apply<'a, F>(&'a self, mut closure: &'a F) where F: FnMut(CGPathElementRef<'a>) { + unsafe { + CGPathApply(self.as_ptr(), &mut closure as *mut _ as *mut c_void, do_apply::); + } + + unsafe extern "C" fn do_apply<'a, F>(info: *mut c_void, element: *const CGPathElement) + where F: FnMut(CGPathElementRef<'a>) { + let closure = info as *mut *mut F; + (**closure)(CGPathElementRef::new(element)) + } + } +} + +#[repr(i32)] +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum CGPathElementType { + MoveToPoint = 0, + AddLineToPoint = 1, + AddQuadCurveToPoint = 2, + AddCurveToPoint = 3, + CloseSubpath = 4, +} + +pub struct CGPathElementRef<'a> { + element: *const CGPathElement, + phantom: PhantomData<&'a CGPathElement>, +} + +impl<'a> CGPathElementRef<'a> { + fn new<'b>(element: *const CGPathElement) -> CGPathElementRef<'b> { + CGPathElementRef { + element: element, + phantom: PhantomData, + } + } +} + +impl<'a> Deref for CGPathElementRef<'a> { + type Target = CGPathElement; + fn deref(&self) -> &CGPathElement { + unsafe { + &*self.element + } + } +} + +#[repr(C)] +pub struct CGPathElement { + pub element_type: CGPathElementType, + points: *mut CGPoint, +} + +impl Debug for CGPathElement { + fn fmt(&self, formatter: &mut Formatter) -> Result<(), fmt::Error> { + write!(formatter, "{:?}: {:?}", self.element_type, self.points()) + } +} + +impl CGPathElement { + pub fn points(&self) -> &[CGPoint] { + unsafe { + match self.element_type { + CGPathElementType::CloseSubpath => &[], + CGPathElementType::MoveToPoint | CGPathElementType::AddLineToPoint => { + slice::from_raw_parts(self.points, 1) + } + CGPathElementType::AddQuadCurveToPoint => slice::from_raw_parts(self.points, 2), + CGPathElementType::AddCurveToPoint => slice::from_raw_parts(self.points, 3), + } + } + } +} + +type CGPathApplierFunction = unsafe extern "C" fn(info: *mut c_void, + element: *const CGPathElement); + +#[link(name = "CoreGraphics", kind = "framework")] +extern { + fn CGPathApply(path: ::sys::CGPathRef, info: *mut c_void, function: CGPathApplierFunction); + fn CGPathGetTypeID() -> CFTypeID; +} diff --git a/src/sys.rs b/src/sys.rs index 317dd07..1048e04 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -4,6 +4,9 @@ pub type CGImageRef = *mut CGImage; pub enum CGColorSpace {} pub type CGColorSpaceRef = *mut CGColorSpace; +pub enum CGPath {} +pub type CGPathRef = *mut CGPath; + pub enum CGDataProvider {} pub type CGDataProviderRef = *mut CGDataProvider; From 0ff1403c56722cedb65f7b46f68827c2e95e0b5c Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 19 Oct 2017 20:31:26 -0700 Subject: [PATCH 154/166] Bump version to 0.11.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 222f77e..ff303ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.10.0" +version = "0.11.0" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" From df8dcca9b511468dbbb655e6ae176cd33cfe4641 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Wed, 25 Oct 2017 15:41:29 -0700 Subject: [PATCH 155/166] Fix memory safety problem in `CGDataProvider::from_buffer()`. The wrapper for that function made no attempt to ensure that the buffer would stay alive, and as a result use-after-free could easily occur. The fix is to require that the buffer be stuffed in an `Arc` box and to teach Core Graphics how to release the `Arc` when it's done with it. --- Cargo.toml | 2 +- src/data_provider.rs | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ff303ed..afe1907 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.11.0" +version = "0.12.0" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" diff --git a/src/data_provider.rs b/src/data_provider.rs index 7262934..9a10496 100644 --- a/src/data_provider.rs +++ b/src/data_provider.rs @@ -11,7 +11,8 @@ use core_foundation::base::{CFRelease, CFRetain, CFTypeID, TCFType}; use core_foundation::data::{CFData, CFDataRef}; use libc::{c_void, size_t, off_t}; -use std::ptr; +use std::mem; +use std::sync::Arc; use foreign_types::{ForeignType, ForeignTypeRef}; @@ -43,13 +44,21 @@ impl CGDataProvider { } } - pub fn from_buffer(buffer: &[u8]) -> Self { + /// Creates a data provider from the given reference-counted buffer. + /// + /// The `CGDataProvider` object takes ownership of the reference. Once the data provider + /// is destroyed, the reference count of the buffer is automatically decremented. + pub fn from_buffer(buffer: Arc>) -> Self { unsafe { - let result = CGDataProviderCreateWithData(ptr::null_mut(), - buffer.as_ptr() as *const c_void, - buffer.len() as size_t, - None); - CGDataProvider::from_ptr(result) + let ptr = (*buffer).as_ptr() as *const c_void; + let len = buffer.len() as size_t; + let info = mem::transmute::>, *mut c_void>(buffer); + let result = CGDataProviderCreateWithData(info, ptr, len, Some(release)); + return CGDataProvider::from_ptr(result); + } + + unsafe extern "C" fn release(info: *mut c_void, _: *const c_void, _: size_t) { + drop(mem::transmute::<*mut c_void, Arc>>(info)) } } } From 8ece71ab12608ef5ef30e98c77ba70cf43fd6536 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 26 Oct 2017 13:47:30 -0700 Subject: [PATCH 156/166] Add bindings to `CGContextSetFont(Size)` and `CGContextShowGlyphsAtPositions`. These functions allow for a minor time savings by avoiding Core Text font creation in the Pathfinder native rasterization functionality. --- src/context.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/context.rs b/src/context.rs index d2d626b..c386627 100644 --- a/src/context.rs +++ b/src/context.rs @@ -10,8 +10,11 @@ use base::CGFloat; use color_space::CGColorSpace; use core_foundation::base::{CFRelease, CFRetain, CFTypeID}; +use font::{CGFont, CGGlyph}; +use geometry::CGPoint; use libc::{c_void, c_int, size_t}; +use std::cmp; use std::ptr; use std::slice; use geometry::CGRect; @@ -178,6 +181,28 @@ impl CGContext { None } } + + pub fn set_font(&self, font: &CGFont) { + unsafe { + CGContextSetFont(self.as_ptr(), font.as_ptr()) + } + } + + pub fn set_font_size(&self, size: CGFloat) { + unsafe { + CGContextSetFontSize(self.as_ptr(), size) + } + } + + pub fn show_glyphs_at_positions(&self, glyphs: &[CGGlyph], positions: &[CGPoint]) { + unsafe { + let count = cmp::min(glyphs.len(), positions.len()); + CGContextShowGlyphsAtPositions(self.as_ptr(), + glyphs.as_ptr(), + positions.as_ptr(), + count) + } + } } #[test] @@ -242,5 +267,11 @@ extern { fn CGContextFillRect(context: ::sys::CGContextRef, rect: CGRect); fn CGContextDrawImage(c: ::sys::CGContextRef, rect: CGRect, image: ::sys::CGImageRef); + fn CGContextSetFont(c: ::sys::CGContextRef, font: ::sys::CGFontRef); + fn CGContextSetFontSize(c: ::sys::CGContextRef, size: CGFloat); + fn CGContextShowGlyphsAtPositions(c: ::sys::CGContextRef, + glyphs: *const CGGlyph, + positions: *const CGPoint, + count: size_t); } From b11b04db2ee12bbdfc89c7a8947098410e804144 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Mon, 13 Nov 2017 20:13:10 -0800 Subject: [PATCH 157/166] Add a binding to `CGFontGetUnitsPerEm()` --- src/font.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/font.rs b/src/font.rs index d4cf297..a456956 100644 --- a/src/font.rs +++ b/src/font.rs @@ -85,6 +85,12 @@ impl CGFont { advances.as_mut_ptr()) } } + + pub fn get_units_per_em(&self) -> c_int { + unsafe { + CGFontGetUnitsPerEm(self.as_ptr()) + } + } } #[link(name = "CoreGraphics", kind = "framework")] @@ -111,4 +117,5 @@ extern { count: size_t, advances: *mut c_int) -> bool; + fn CGFontGetUnitsPerEm(font: ::sys::CGFontRef) -> c_int; } From e7d15ead28fdaabd4b90884071ebb6e98c590a78 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Fri, 17 Nov 2017 11:20:51 +0100 Subject: [PATCH 158/166] Reexport FFI functions that were made private for no reason --- Cargo.toml | 2 +- src/display.rs | 78 +++++++++++++++++++++++++------------------------- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index afe1907..769643d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.12.0" +version = "0.12.1" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" diff --git a/src/display.rs b/src/display.rs index f440c1d..0b1c8b2 100644 --- a/src/display.rs +++ b/src/display.rs @@ -404,63 +404,63 @@ extern "C" { pub static CGRectNull: CGRect; pub static CGRectInfinite: CGRect; - fn CGDisplayModeRelease(mode: ::sys::CGDisplayModeRef); + pub fn CGDisplayModeRelease(mode: ::sys::CGDisplayModeRef); - fn CGMainDisplayID() -> CGDirectDisplayID; - fn CGDisplayIsActive(display: CGDirectDisplayID) -> boolean_t; - fn CGDisplayIsAlwaysInMirrorSet(display: CGDirectDisplayID) -> boolean_t; - fn CGDisplayIsAsleep(display: CGDirectDisplayID) -> boolean_t; - fn CGDisplayIsBuiltin(display: CGDirectDisplayID) -> boolean_t; - fn CGDisplayIsInHWMirrorSet(display: CGDirectDisplayID) -> boolean_t; - fn CGDisplayIsInMirrorSet(display: CGDirectDisplayID) -> boolean_t; - fn CGDisplayIsMain(display: CGDirectDisplayID) -> boolean_t; - fn CGDisplayIsOnline(display: CGDirectDisplayID) -> boolean_t; - fn CGDisplayIsStereo(display: CGDirectDisplayID) -> boolean_t; - fn CGDisplayMirrorsDisplay(display: CGDirectDisplayID) -> CGDirectDisplayID; - fn CGDisplayPrimaryDisplay(display: CGDirectDisplayID) -> CGDirectDisplayID; - fn CGDisplayRotation(display: CGDirectDisplayID) -> libc::c_double; - fn CGDisplayScreenSize(display: CGDirectDisplayID) -> CGSize; - fn CGDisplaySerialNumber(display: CGDirectDisplayID) -> libc::uint32_t; - fn CGDisplayUnitNumber(display: CGDirectDisplayID) -> libc::uint32_t; - fn CGDisplayUsesOpenGLAcceleration(display: CGDirectDisplayID) -> boolean_t; - fn CGDisplayVendorNumber(display: CGDirectDisplayID) -> libc::uint32_t; - fn CGGetActiveDisplayList( + pub fn CGMainDisplayID() -> CGDirectDisplayID; + pub fn CGDisplayIsActive(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayIsAlwaysInMirrorSet(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayIsAsleep(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayIsBuiltin(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayIsInHWMirrorSet(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayIsInMirrorSet(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayIsMain(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayIsOnline(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayIsStereo(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayMirrorsDisplay(display: CGDirectDisplayID) -> CGDirectDisplayID; + pub fn CGDisplayPrimaryDisplay(display: CGDirectDisplayID) -> CGDirectDisplayID; + pub fn CGDisplayRotation(display: CGDirectDisplayID) -> libc::c_double; + pub fn CGDisplayScreenSize(display: CGDirectDisplayID) -> CGSize; + pub fn CGDisplaySerialNumber(display: CGDirectDisplayID) -> libc::uint32_t; + pub fn CGDisplayUnitNumber(display: CGDirectDisplayID) -> libc::uint32_t; + pub fn CGDisplayUsesOpenGLAcceleration(display: CGDirectDisplayID) -> boolean_t; + pub fn CGDisplayVendorNumber(display: CGDirectDisplayID) -> libc::uint32_t; + pub fn CGGetActiveDisplayList( max_displays: libc::uint32_t, active_displays: *mut CGDirectDisplayID, display_count: *mut libc::uint32_t, ) -> CGError; - fn CGDisplayModelNumber(display: CGDirectDisplayID) -> libc::uint32_t; - fn CGDisplayPixelsHigh(display: CGDirectDisplayID) -> libc::size_t; - fn CGDisplayPixelsWide(display: CGDirectDisplayID) -> libc::size_t; - fn CGDisplayBounds(display: CGDirectDisplayID) -> CGRect; - fn CGDisplayCreateImage(display: CGDirectDisplayID) -> ::sys::CGImageRef; + pub fn CGDisplayModelNumber(display: CGDirectDisplayID) -> libc::uint32_t; + pub fn CGDisplayPixelsHigh(display: CGDirectDisplayID) -> libc::size_t; + pub fn CGDisplayPixelsWide(display: CGDirectDisplayID) -> libc::size_t; + pub fn CGDisplayBounds(display: CGDirectDisplayID) -> CGRect; + pub fn CGDisplayCreateImage(display: CGDirectDisplayID) -> ::sys::CGImageRef; - fn CGDisplayCopyDisplayMode(display: CGDirectDisplayID) -> ::sys::CGDisplayModeRef; - fn CGDisplayModeGetHeight(mode: ::sys::CGDisplayModeRef) -> libc::size_t; - fn CGDisplayModeGetWidth(mode: ::sys::CGDisplayModeRef) -> libc::size_t; - fn CGDisplayModeGetPixelHeight(mode: ::sys::CGDisplayModeRef) -> libc::size_t; - fn CGDisplayModeGetPixelWidth(mode: ::sys::CGDisplayModeRef) -> libc::size_t; - fn CGDisplayModeGetRefreshRate(mode: ::sys::CGDisplayModeRef) -> libc::c_double; + pub fn CGDisplayCopyDisplayMode(display: CGDirectDisplayID) -> ::sys::CGDisplayModeRef; + pub fn CGDisplayModeGetHeight(mode: ::sys::CGDisplayModeRef) -> libc::size_t; + pub fn CGDisplayModeGetWidth(mode: ::sys::CGDisplayModeRef) -> libc::size_t; + pub fn CGDisplayModeGetPixelHeight(mode: ::sys::CGDisplayModeRef) -> libc::size_t; + pub fn CGDisplayModeGetPixelWidth(mode: ::sys::CGDisplayModeRef) -> libc::size_t; + pub fn CGDisplayModeGetRefreshRate(mode: ::sys::CGDisplayModeRef) -> libc::c_double; // mouse stuff - fn CGDisplayHideCursor(display: CGDirectDisplayID) -> CGError; - fn CGDisplayShowCursor(display: CGDirectDisplayID) -> CGError; - fn CGDisplayMoveCursorToPoint(display: CGDirectDisplayID, point: CGPoint) -> CGError; - fn CGWarpMouseCursorPosition(point: CGPoint) -> CGError; - fn CGAssociateMouseAndMouseCursorPosition(connected: boolean_t) -> CGError; + pub fn CGDisplayHideCursor(display: CGDirectDisplayID) -> CGError; + pub fn CGDisplayShowCursor(display: CGDirectDisplayID) -> CGError; + pub fn CGDisplayMoveCursorToPoint(display: CGDirectDisplayID, point: CGPoint) -> CGError; + pub fn CGWarpMouseCursorPosition(point: CGPoint) -> CGError; + pub fn CGAssociateMouseAndMouseCursorPosition(connected: boolean_t) -> CGError; // Window Services Reference - fn CGWindowListCopyWindowInfo( + pub fn CGWindowListCopyWindowInfo( option: CGWindowListOption, relativeToWindow: CGWindowID, ) -> CFArrayRef; - fn CGWindowListCreateImage( + pub fn CGWindowListCreateImage( screenBounds: CGRect, listOptions: CGWindowListOption, windowId: CGWindowID, imageOptions: CGWindowImageOption, ) -> ::sys::CGImageRef; - fn CGWindowListCreateImageFromArray( + pub fn CGWindowListCreateImageFromArray( screenBounds: CGRect, windowArray: CFArrayRef, imageOptions: CGWindowImageOption, From 33ba9cb51b417b55ad36bf326cb8c516218fd6d4 Mon Sep 17 00:00:00 2001 From: Natalya Date: Fri, 17 Nov 2017 19:09:27 +0200 Subject: [PATCH 159/166] Added font copy creation with variations Is required for Servo's Webrender to up Core Graphics's version there. --- src/font.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/font.rs b/src/font.rs index a456956..4afc504 100644 --- a/src/font.rs +++ b/src/font.rs @@ -9,6 +9,7 @@ use core_foundation::base::{CFRelease, CFRetain, CFTypeID, TCFType}; use core_foundation::string::{CFString, CFStringRef}; +use core_foundation::dictionary::{CFDictionary, CFDictionaryRef}; use data_provider::CGDataProvider; use geometry::CGRect; @@ -59,6 +60,18 @@ impl CGFont { } } + pub fn create_copy_from_variations(&self, vars: &CFDictionary) -> Result { + unsafe { + let font_ref = CGFontCreateCopyWithVariations(self.as_ptr(), + vars.as_concrete_TypeRef()); + if !font_ref.is_null() { + Ok(CGFont::from_ptr(font_ref)) + } else { + Err(()) + } + } + } + pub fn postscript_name(&self) -> CFString { unsafe { let string_ref = CGFontCopyPostScriptName(self.as_ptr()); @@ -98,6 +111,7 @@ extern { // TODO: basically nothing has bindings (even commented-out) besides what we use. fn CGFontCreateWithDataProvider(provider: ::sys::CGDataProviderRef) -> ::sys::CGFontRef; fn CGFontCreateWithFontName(name: CFStringRef) -> ::sys::CGFontRef; + fn CGFontCreateCopyWithVariations(font: ::sys::CGFontRef, vars: CFDictionaryRef) -> ::sys::CGFontRef; fn CGFontGetTypeID() -> CFTypeID; fn CGFontCopyPostScriptName(font: ::sys::CGFontRef) -> CFStringRef; From 5c4334c120dc69da5f30edfdf225816000172b43 Mon Sep 17 00:00:00 2001 From: Natalya Date: Fri, 17 Nov 2017 21:08:33 +0200 Subject: [PATCH 160/166] spaces alignment --- src/font.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/font.rs b/src/font.rs index 4afc504..73265a7 100644 --- a/src/font.rs +++ b/src/font.rs @@ -63,7 +63,7 @@ impl CGFont { pub fn create_copy_from_variations(&self, vars: &CFDictionary) -> Result { unsafe { let font_ref = CGFontCreateCopyWithVariations(self.as_ptr(), - vars.as_concrete_TypeRef()); + vars.as_concrete_TypeRef()); if !font_ref.is_null() { Ok(CGFont::from_ptr(font_ref)) } else { From 9aa73f00f9a4891132c7d6ebd3879fc27937e736 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Mon, 20 Nov 2017 14:22:25 +0100 Subject: [PATCH 161/166] Bump version to 0.12.2 This includes CGFont::create_copy_from_variations. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 769643d..c3916d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.12.1" +version = "0.12.2" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" From 25827748c0cf6ab0c8cb6177033cd2e2cf9c0ab3 Mon Sep 17 00:00:00 2001 From: Lee Salzman Date: Mon, 20 Nov 2017 14:21:29 -0500 Subject: [PATCH 162/166] extend support for CGAffineTransform in geometry --- Cargo.toml | 2 +- src/context.rs | 9 ++++++++- src/geometry.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c3916d2..91231b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.12.2" +version = "0.12.3" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" diff --git a/src/context.rs b/src/context.rs index c386627..2030b0f 100644 --- a/src/context.rs +++ b/src/context.rs @@ -17,7 +17,7 @@ use libc::{c_void, c_int, size_t}; use std::cmp; use std::ptr; use std::slice; -use geometry::CGRect; +use geometry::{CGAffineTransform, CGRect}; use image::CGImage; use foreign_types::ForeignType; @@ -194,6 +194,12 @@ impl CGContext { } } + pub fn set_text_matrix(&self, t: &CGAffineTransform) { + unsafe { + CGContextSetTextMatrix(self.as_ptr(), *t) + } + } + pub fn show_glyphs_at_positions(&self, glyphs: &[CGGlyph], positions: &[CGPoint]) { unsafe { let count = cmp::min(glyphs.len(), positions.len()); @@ -269,6 +275,7 @@ extern { fn CGContextDrawImage(c: ::sys::CGContextRef, rect: CGRect, image: ::sys::CGImageRef); fn CGContextSetFont(c: ::sys::CGContextRef, font: ::sys::CGFontRef); fn CGContextSetFontSize(c: ::sys::CGContextRef, size: CGFloat); + fn CGContextSetTextMatrix(c: ::sys::CGContextRef, t: CGAffineTransform); fn CGContextShowGlyphsAtPositions(c: ::sys::CGContextRef, glyphs: *const CGGlyph, positions: *const CGPoint, diff --git a/src/geometry.rs b/src/geometry.rs index 2955394..906c605 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -47,6 +47,13 @@ impl CGSize { height: height, } } + + #[inline] + pub fn apply_transform(&self, t: &CGAffineTransform) -> CGSize { + unsafe { + ffi::CGSizeApplyAffineTransform(*self, *t) + } + } } #[repr(C)] @@ -64,6 +71,13 @@ impl CGPoint { y: y, } } + + #[inline] + pub fn apply_transform(&self, t: &CGAffineTransform) -> CGPoint { + unsafe { + ffi::CGPointApplyAffineTransform(*self, *t) + } + } } #[repr(C)] @@ -117,9 +131,17 @@ impl CGRect { ffi::CGRectIntersectsRect(*self, *other) == 1 } } + + #[inline] + pub fn apply_transform(&self, t: &CGAffineTransform) -> CGRect { + unsafe { + ffi::CGRectApplyAffineTransform(*self, *t) + } + } } #[repr(C)] +#[derive(Clone, Copy, Debug, Default)] pub struct CGAffineTransform { pub a: CGFloat, pub b: CGFloat, @@ -129,9 +151,30 @@ pub struct CGAffineTransform { pub ty: CGFloat, } +impl CGAffineTransform { + #[inline] + pub fn new( + a: CGFloat, + b: CGFloat, + c: CGFloat, + d: CGFloat, + tx: CGFloat, + ty: CGFloat, + ) -> CGAffineTransform { + CGAffineTransform { a, b, c, d, tx, ty } + } + + #[inline] + pub fn invert(&self) -> CGAffineTransform { + unsafe { + ffi::CGAffineTransformInvert(*self) + } + } +} + mod ffi { use base::{CGFloat, boolean_t}; - use geometry::CGRect; + use geometry::{CGAffineTransform, CGPoint, CGRect, CGSize}; use core_foundation::dictionary::CFDictionaryRef; #[link(name = "CoreGraphics", kind = "framework")] @@ -141,6 +184,12 @@ mod ffi { rect: *mut CGRect) -> boolean_t; pub fn CGRectIsEmpty(rect: CGRect) -> boolean_t; pub fn CGRectIntersectsRect(rect1: CGRect, rect2: CGRect) -> boolean_t; + + pub fn CGAffineTransformInvert(t: CGAffineTransform) -> CGAffineTransform; + + pub fn CGPointApplyAffineTransform(point: CGPoint, t: CGAffineTransform) -> CGPoint; + pub fn CGRectApplyAffineTransform(rect: CGRect, t: CGAffineTransform) -> CGRect; + pub fn CGSizeApplyAffineTransform(size: CGSize, t: CGAffineTransform) -> CGSize; } } From 66bb295237433eb1e4a6d72eb88e574ba05a913c Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Thu, 16 Nov 2017 22:05:47 -0500 Subject: [PATCH 163/166] Add functions to get font tag information --- Cargo.toml | 2 +- src/font.rs | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 91231b8..c2bdb8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,6 @@ elcapitan = [] [dependencies] bitflags = "1.0" -core-foundation = "0.4" +core-foundation = "0.4.5" foreign-types = "0.3.0" libc = "0.2" diff --git a/src/font.rs b/src/font.rs index 73265a7..96e9f33 100644 --- a/src/font.rs +++ b/src/font.rs @@ -7,7 +7,10 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use std::ptr; use core_foundation::base::{CFRelease, CFRetain, CFTypeID, TCFType}; +use core_foundation::array::{CFArray, CFArrayRef}; +use core_foundation::data::{CFData, CFDataRef}; use core_foundation::string::{CFString, CFStringRef}; use core_foundation::dictionary::{CFDictionary, CFDictionaryRef}; use data_provider::CGDataProvider; @@ -104,6 +107,21 @@ impl CGFont { CGFontGetUnitsPerEm(self.as_ptr()) } } + + pub fn copy_table_tags(&self) -> CFArray { + unsafe { + TCFType::wrap_under_create_rule(CGFontCopyTableTags(self.as_ptr())) + } + } + + pub fn copy_table_for_tag(&self, tag: u32) -> Option { + let data_ref = unsafe { CGFontCopyTableForTag(self.as_ptr(), tag) }; + if data_ref != ptr::null() { + Some(unsafe { TCFType::wrap_under_create_rule(data_ref) }) + } else { + None + } + } } #[link(name = "CoreGraphics", kind = "framework")] @@ -132,4 +150,7 @@ extern { advances: *mut c_int) -> bool; fn CGFontGetUnitsPerEm(font: ::sys::CGFontRef) -> c_int; + + fn CGFontCopyTableTags(font: ::sys::CGFontRef) -> CFArrayRef; + fn CGFontCopyTableForTag(font: ::sys::CGFontRef, tag: u32) -> CFDataRef; } From 68c63984caaf97e3b1b4ea82b02c56dd3c90348c Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Mon, 27 Nov 2017 11:36:44 -0500 Subject: [PATCH 164/166] Bump version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c2bdb8b..fce1375 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.12.3" +version = "0.12.4" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0" From e92e0780de7f3a8feaf3df39b1b3b3c86804396e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20F=C3=A4rnstrand?= Date: Wed, 29 Nov 2017 19:01:44 +0100 Subject: [PATCH 165/166] Update core-foundation to 0.5.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index fce1375..146c109 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,6 @@ elcapitan = [] [dependencies] bitflags = "1.0" -core-foundation = "0.4.5" +core-foundation = "0.5" foreign-types = "0.3.0" libc = "0.2" From a01f58ab3b3d82b23a820824d548b84cc2e4f697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20F=C3=A4rnstrand?= Date: Thu, 11 Jan 2018 23:52:31 +0100 Subject: [PATCH 166/166] Bump version to 0.13.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 146c109..3c18a56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "core-graphics" description = "Bindings to Core Graphics for OS X" homepage = "https://github.com/servo/core-graphics-rs" repository = "https://github.com/servo/core-graphics-rs" -version = "0.12.4" +version = "0.13.0" authors = ["The Servo Project Developers"] license = "MIT / Apache-2.0"