aboutsummaryrefslogtreecommitdiffstats
path: root/src/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/api')
-rw-r--r--src/api/cocoa/headless.rs52
-rw-r--r--src/api/cocoa/helpers.rs88
-rw-r--r--src/api/cocoa/mod.rs102
-rw-r--r--src/api/glx/mod.rs7
-rw-r--r--src/api/x11/window.rs57
5 files changed, 141 insertions, 165 deletions
diff --git a/src/api/cocoa/headless.rs b/src/api/cocoa/headless.rs
index d0d59f3..7fe2046 100644
--- a/src/api/cocoa/headless.rs
+++ b/src/api/cocoa/headless.rs
@@ -4,8 +4,6 @@ use CreationError::OsError;
use GlAttributes;
use GlContext;
use PixelFormatRequirements;
-use std::os::raw::c_void;
-use std::ptr;
use core_foundation::base::TCFType;
use core_foundation::string::CFString;
@@ -13,13 +11,7 @@ use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFuncti
use cocoa::base::{id, nil};
use cocoa::appkit::*;
use PixelFormat;
-
-mod gl {
- include!(concat!(env!("OUT_DIR"), "/gl_bindings.rs"));
-}
-
-static mut framebuffer: u32 = 0;
-static mut texture: u32 = 0;
+use api::cocoa::helpers;
pub struct HeadlessContext {
width: u32,
@@ -28,16 +20,12 @@ pub struct HeadlessContext {
}
impl HeadlessContext {
- pub fn new((width, height): (u32, u32), _pf_reqs: &PixelFormatRequirements,
- _opengl: &GlAttributes<&HeadlessContext>) -> Result<HeadlessContext, CreationError>
+ pub fn new((width, height): (u32, u32), pf_reqs: &PixelFormatRequirements,
+ opengl: &GlAttributes<&HeadlessContext>) -> Result<HeadlessContext, CreationError>
{
let context = unsafe {
- let attributes = [
- NSOpenGLPFAAccelerated as u32,
- NSOpenGLPFAAllowOfflineRenderers as u32,
- NSOpenGLPFADoubleBuffer as u32,
- 0
- ];
+
+ let attributes = try!(helpers::build_nsattributes(pf_reqs, opengl));
let pixelformat = NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&attributes);
if pixelformat == nil {
@@ -56,9 +44,6 @@ impl HeadlessContext {
context: context,
};
- // Load the function pointers as we need them to create the FBO
- gl::load_with(|s| headless.get_proc_address(s) as *const c_void);
-
Ok(headless)
}
}
@@ -66,22 +51,6 @@ impl HeadlessContext {
impl GlContext for HeadlessContext {
unsafe fn make_current(&self) -> Result<(), ContextError> {
self.context.makeCurrentContext();
-
- gl::GenFramebuffersEXT(1, &mut framebuffer);
- gl::BindFramebufferEXT(gl::FRAMEBUFFER_EXT, framebuffer);
- gl::GenTextures(1, &mut texture);
- gl::BindTexture(gl::TEXTURE_2D, texture);
- gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::LINEAR as i32);
- gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR as i32);
- gl::TexImage2D(gl::TEXTURE_2D, 0, gl::RGBA8 as i32, self.width as i32, self.height as i32,
- 0, gl::RGBA, gl::UNSIGNED_BYTE, ptr::null());
- gl::FramebufferTexture2DEXT(gl::FRAMEBUFFER_EXT, gl::COLOR_ATTACHMENT0_EXT,
- gl::TEXTURE_2D, texture, 0);
- let status = gl::CheckFramebufferStatusEXT(gl::FRAMEBUFFER_EXT);
- if status != gl::FRAMEBUFFER_COMPLETE_EXT {
- panic!("Error while creating the framebuffer");
- }
-
Ok(())
}
@@ -105,6 +74,7 @@ impl GlContext for HeadlessContext {
#[inline]
fn swap_buffers(&self) -> Result<(), ContextError> {
+ unsafe { self.context.flushBuffer(); }
Ok(())
}
@@ -121,13 +91,3 @@ impl GlContext for HeadlessContext {
unsafe impl Send for HeadlessContext {}
unsafe impl Sync for HeadlessContext {}
-
-impl Drop for HeadlessContext {
- #[inline]
- fn drop(&mut self) {
- unsafe {
- gl::DeleteTextures(1, &texture);
- gl::DeleteFramebuffersEXT(1, &framebuffer);
- }
- }
-}
diff --git a/src/api/cocoa/helpers.rs b/src/api/cocoa/helpers.rs
new file mode 100644
index 0000000..7fd7571
--- /dev/null
+++ b/src/api/cocoa/helpers.rs
@@ -0,0 +1,88 @@
+
+use CreationError;
+use GlAttributes;
+use GlProfile;
+use GlRequest;
+use PixelFormatRequirements;
+use ReleaseBehavior;
+use cocoa::foundation::NSAutoreleasePool;
+use cocoa::appkit::*;
+
+pub fn build_nsattributes<T>(pf_reqs: &PixelFormatRequirements, opengl: &GlAttributes<&T>)
+ -> Result<Vec<u32>, CreationError> {
+
+ let profile = match (opengl.version, opengl.version.to_gl_version(), opengl.profile) {
+
+ // Note: we are not using ranges because of a rust bug that should be fixed here:
+ // https://github.com/rust-lang/rust/pull/27050
+
+ (GlRequest::Latest, _, Some(GlProfile::Compatibility)) => NSOpenGLProfileVersionLegacy as u32,
+ (GlRequest::Latest, _, _) => {
+ if NSAppKitVersionNumber.floor() >= NSAppKitVersionNumber10_9 {
+ NSOpenGLProfileVersion4_1Core as u32
+ } else if NSAppKitVersionNumber.floor() >= NSAppKitVersionNumber10_7 {
+ NSOpenGLProfileVersion3_2Core as u32
+ } else {
+ NSOpenGLProfileVersionLegacy as u32
+ }
+ },
+
+ (_, Some((1, _)), _) => NSOpenGLProfileVersionLegacy as u32,
+ (_, Some((2, _)), _) => NSOpenGLProfileVersionLegacy as u32,
+ (_, Some((3, 0)), _) => NSOpenGLProfileVersionLegacy as u32,
+ (_, Some((3, 1)), _) => NSOpenGLProfileVersionLegacy as u32,
+ (_, Some((3, 2)), _) => NSOpenGLProfileVersion3_2Core as u32,
+ (_, Some((3, _)), Some(GlProfile::Compatibility)) => return Err(CreationError::OpenGlVersionNotSupported),
+ (_, Some((3, _)), _) => NSOpenGLProfileVersion4_1Core as u32,
+ (_, Some((4, _)), Some(GlProfile::Compatibility)) => return Err(CreationError::OpenGlVersionNotSupported),
+ (_, Some((4, _)), _) => NSOpenGLProfileVersion4_1Core as u32,
+ _ => return Err(CreationError::OpenGlVersionNotSupported),
+ };
+
+ // NOTE: OS X no longer has the concept of setting individual
+ // color component's bit size. Instead we can only specify the
+ // full color size and hope for the best. Another hiccup is that
+ // `NSOpenGLPFAColorSize` also includes `NSOpenGLPFAAlphaSize`,
+ // so we have to account for that as well.
+ let alpha_depth = pf_reqs.alpha_bits.unwrap_or(8);
+ let color_depth = pf_reqs.color_bits.unwrap_or(24) + alpha_depth;
+
+ // TODO: handle hardware_accelerated parameter of pf_reqs
+
+ let mut attributes = vec![
+ NSOpenGLPFADoubleBuffer as u32,
+ NSOpenGLPFAClosestPolicy as u32,
+ NSOpenGLPFAColorSize as u32, color_depth as u32,
+ NSOpenGLPFAAlphaSize as u32, alpha_depth as u32,
+ NSOpenGLPFADepthSize as u32, pf_reqs.depth_bits.unwrap_or(24) as u32,
+ NSOpenGLPFAStencilSize as u32, pf_reqs.stencil_bits.unwrap_or(8) as u32,
+ NSOpenGLPFAOpenGLProfile as u32, profile,
+ ];
+
+ if pf_reqs.release_behavior != ReleaseBehavior::Flush {
+ return Err(CreationError::NoAvailablePixelFormat);
+ }
+
+ if pf_reqs.stereoscopy {
+ unimplemented!(); // TODO:
+ }
+
+ if pf_reqs.double_buffer == Some(false) {
+ unimplemented!(); // TODO:
+ }
+
+ if pf_reqs.float_color_buffer {
+ attributes.push(NSOpenGLPFAColorFloat as u32);
+ }
+
+ pf_reqs.multisampling.map(|samples| {
+ attributes.push(NSOpenGLPFAMultisample as u32);
+ attributes.push(NSOpenGLPFASampleBuffers as u32); attributes.push(1);
+ attributes.push(NSOpenGLPFASamples as u32); attributes.push(samples as u32);
+ });
+
+ // attribute list must be null terminated.
+ attributes.push(0);
+
+ Ok(attributes)
+}
diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs
index 2fc1baf..66244d8 100644
--- a/src/api/cocoa/mod.rs
+++ b/src/api/cocoa/mod.rs
@@ -24,8 +24,8 @@ use objc::declare::ClassDecl;
use cgl::{CGLEnable, kCGLCECrashOnRemovedFunctions, CGLSetParameter, kCGLCPSurfaceOpacity};
use cocoa::base::{id, nil};
-use cocoa::foundation::{NSAutoreleasePool, NSDate, NSDefaultRunLoopMode, NSPoint, NSRect, NSSize,
- NSString, NSUInteger};
+use cocoa::foundation::{NSAutoreleasePool, NSDate, NSDefaultRunLoopMode, NSPoint, NSRect, NSSize,
+ NSString, NSUInteger};
use cocoa::appkit;
use cocoa::appkit::*;
use cocoa::appkit::NSEventSubtype::*;
@@ -54,6 +54,7 @@ pub use self::monitor::{MonitorId, get_available_monitors, get_primary_monitor};
mod monitor;
mod event;
mod headless;
+mod helpers;
static mut shift_pressed: bool = false;
static mut ctrl_pressed: bool = false;
@@ -137,7 +138,7 @@ impl WindowDelegate {
window_should_close as extern fn(&Object, Sel, id) -> BOOL);
decl.add_method(sel!(windowDidResize:),
window_did_resize as extern fn(&Object, Sel, id));
-
+
decl.add_method(sel!(windowDidBecomeKey:),
window_did_become_key as extern fn(&Object, Sel, id));
decl.add_method(sel!(windowDidResignKey:),
@@ -317,7 +318,7 @@ impl Window {
let mut opacity = 0;
CGLSetParameter(obj as *mut _, kCGLCPSurfaceOpacity, &mut opacity);
}
-
+
app.activateIgnoringOtherApps_(YES);
if win_attribs.visible {
window.makeKeyAndOrderFront_(nil);
@@ -359,7 +360,7 @@ impl Window {
}
fn create_window(attrs: &WindowAttributes) -> Option<IdRef> {
- unsafe {
+ unsafe {
let screen = match attrs.monitor {
Some(ref monitor_id) => {
let native_id = match monitor_id.get_native_identifier() {
@@ -414,7 +415,7 @@ impl Window {
NSTitledWindowMask as NSUInteger |
NSFullSizeContentViewWindowMask as NSUInteger
};
-
+
let window = IdRef::new(NSWindow::alloc(nil).initWithContentRect_styleMask_backing_defer_(
frame,
masks,
@@ -456,79 +457,7 @@ impl Window {
fn create_context(view: id, pf_reqs: &PixelFormatRequirements, opengl: &GlAttributes<&Window>)
-> Result<(IdRef, PixelFormat), CreationError>
{
- let profile = match (opengl.version, opengl.version.to_gl_version(), opengl.profile) {
-
- // Note: we are not using ranges because of a rust bug that should be fixed here:
- // https://github.com/rust-lang/rust/pull/27050
-
- (GlRequest::Latest, _, Some(GlProfile::Compatibility)) => NSOpenGLProfileVersionLegacy as u32,
- (GlRequest::Latest, _, _) => {
- if NSAppKitVersionNumber.floor() >= NSAppKitVersionNumber10_9 {
- NSOpenGLProfileVersion4_1Core as u32
- } else if NSAppKitVersionNumber.floor() >= NSAppKitVersionNumber10_7 {
- NSOpenGLProfileVersion3_2Core as u32
- } else {
- NSOpenGLProfileVersionLegacy as u32
- }
- },
-
- (_, Some((1, _)), _) => NSOpenGLProfileVersionLegacy as u32,
- (_, Some((2, _)), _) => NSOpenGLProfileVersionLegacy as u32,
- (_, Some((3, 0)), _) => NSOpenGLProfileVersionLegacy as u32,
- (_, Some((3, 1)), _) => NSOpenGLProfileVersionLegacy as u32,
- (_, Some((3, 2)), _) => NSOpenGLProfileVersion3_2Core as u32,
- (_, Some((3, _)), Some(GlProfile::Compatibility)) => return Err(CreationError::OpenGlVersionNotSupported),
- (_, Some((3, _)), _) => NSOpenGLProfileVersion4_1Core as u32,
- (_, Some((4, _)), Some(GlProfile::Compatibility)) => return Err(CreationError::OpenGlVersionNotSupported),
- (_, Some((4, _)), _) => NSOpenGLProfileVersion4_1Core as u32,
- _ => return Err(CreationError::OpenGlVersionNotSupported),
- };
-
- // NOTE: OS X no longer has the concept of setting individual
- // color component's bit size. Instead we can only specify the
- // full color size and hope for the best. Another hiccup is that
- // `NSOpenGLPFAColorSize` also includes `NSOpenGLPFAAlphaSize`,
- // so we have to account for that as well.
- let alpha_depth = pf_reqs.alpha_bits.unwrap_or(8);
- let color_depth = pf_reqs.color_bits.unwrap_or(24) + alpha_depth;
-
- // TODO: handle hardware_accelerated parameter of pf_reqs
-
- let mut attributes = vec![
- NSOpenGLPFADoubleBuffer as u32,
- NSOpenGLPFAClosestPolicy as u32,
- NSOpenGLPFAColorSize as u32, color_depth as u32,
- NSOpenGLPFAAlphaSize as u32, alpha_depth as u32,
- NSOpenGLPFADepthSize as u32, pf_reqs.depth_bits.unwrap_or(24) as u32,
- NSOpenGLPFAStencilSize as u32, pf_reqs.stencil_bits.unwrap_or(8) as u32,
- NSOpenGLPFAOpenGLProfile as u32, profile,
- ];
-
- if pf_reqs.release_behavior != ReleaseBehavior::Flush {
- return Err(CreationError::NoAvailablePixelFormat);
- }
-
- if pf_reqs.stereoscopy {
- unimplemented!(); // TODO:
- }
-
- if pf_reqs.double_buffer == Some(false) {
- unimplemented!(); // TODO:
- }
-
- if pf_reqs.float_color_buffer {
- attributes.push(NSOpenGLPFAColorFloat as u32);
- }
-
- pf_reqs.multisampling.map(|samples| {
- attributes.push(NSOpenGLPFAMultisample as u32);
- attributes.push(NSOpenGLPFASampleBuffers as u32); attributes.push(1);
- attributes.push(NSOpenGLPFASamples as u32); attributes.push(samples as u32);
- });
-
- // attribute list must be null terminated.
- attributes.push(0);
-
+ let attributes = try!(helpers::build_nsattributes(pf_reqs, opengl));
unsafe {
let pixelformat = IdRef::new(NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&attributes));
@@ -604,7 +533,7 @@ impl Window {
pub fn get_position(&self) -> Option<(i32, i32)> {
unsafe {
let content_rect = NSWindow::contentRectForFrameRect_(*self.window, NSWindow::frame(*self.window));
-
+
// TODO: consider extrapolating the calculations for the y axis to
// a private method
Some((content_rect.origin.x as i32, (CGDisplayPixelsHigh(CGMainDisplayID()) as f64 - (content_rect.origin.y + content_rect.size.height)) as i32))
@@ -616,7 +545,7 @@ impl Window {
let frame = NSWindow::frame(*self.view);
// NOTE: `setFrameOrigin` might not give desirable results when
- // setting window, as it treats bottom left as origin.
+ // setting window, as it treats bottom left as origin.
// `setFrameTopLeftPoint` treats top left as origin (duh), but
// does not equal the value returned by `get_window_position`
// (there is a difference by 22 for me on yosemite)
@@ -699,7 +628,7 @@ impl Window {
}
pub fn set_cursor(&self, cursor: MouseCursor) {
- let cursor_name = match cursor {
+ let cursor_name = match cursor {
MouseCursor::Arrow | MouseCursor::Default => "arrowCursor",
MouseCursor::Hand => "pointingHandCursor",
MouseCursor::Grabbing | MouseCursor::Grab => "closedHandCursor",
@@ -768,7 +697,7 @@ impl Window {
pub fn set_cursor_position(&self, x: i32, y: i32) -> Result<(), ()> {
let (window_x, window_y) = self.get_position().unwrap_or((0, 0));
let (cursor_x, cursor_y) = (window_x + x, window_y + y);
-
+
unsafe {
// TODO: Check for errors.
let _ = CGWarpMouseCursorPosition(CGPoint { x: cursor_x as CGFloat, y: cursor_y as CGFloat });
@@ -952,10 +881,13 @@ unsafe fn NSEventToEvent(window: &Window, nsevent: id) -> Option<Event> {
},
NSScrollWheel => {
use events::MouseScrollDelta::{LineDelta, PixelDelta};
+ let scale_factor = window.hidpi_factor();
let delta = if nsevent.hasPreciseScrollingDeltas() == YES {
- PixelDelta(nsevent.scrollingDeltaX() as f32, nsevent.scrollingDeltaY() as f32)
+ PixelDelta(scale_factor * nsevent.scrollingDeltaX() as f32,
+ scale_factor * nsevent.scrollingDeltaY() as f32)
} else {
- LineDelta(nsevent.scrollingDeltaX() as f32, nsevent.scrollingDeltaY() as f32)
+ LineDelta(scale_factor * nsevent.scrollingDeltaX() as f32,
+ scale_factor * nsevent.scrollingDeltaY() as f32)
};
Some(MouseWheel(delta))
},
diff --git a/src/api/glx/mod.rs b/src/api/glx/mod.rs
index d3f28ed..95b6f99 100644
--- a/src/api/glx/mod.rs
+++ b/src/api/glx/mod.rs
@@ -415,10 +415,9 @@ unsafe fn choose_fbconfig(glx: &ffi::glx::Glx, extensions: &str, xlib: &ffi::Xli
out.push(stencil as c_int);
}
- if let Some(double_buffer) = reqs.double_buffer {
- out.push(ffi::glx::DOUBLEBUFFER as c_int);
- out.push(if double_buffer { 1 } else { 0 });
- }
+ let double_buffer = reqs.double_buffer.unwrap_or(true);
+ out.push(ffi::glx::DOUBLEBUFFER as c_int);
+ out.push(if double_buffer { 1 } else { 0 });
if let Some(multisampling) = reqs.multisampling {
if extensions.split(' ').find(|&i| i == "GLX_ARB_multisample").is_some() {
diff --git a/src/api/x11/window.rs b/src/api/x11/window.rs
index 252af75..8cae2be 100644
--- a/src/api/x11/window.rs
+++ b/src/api/x11/window.rs
@@ -57,7 +57,7 @@ pub struct XWindow {
pub context: Context,
is_fullscreen: bool,
screen_id: libc::c_int,
- xf86_desk_mode: ffi::XF86VidModeModeInfo,
+ xf86_desk_mode: Option<ffi::XF86VidModeModeInfo>,
ic: ffi::XIC,
im: ffi::XIM,
colormap: ffi::Colormap,
@@ -90,7 +90,9 @@ impl Drop for XWindow {
let _lock = GLOBAL_XOPENIM_LOCK.lock().unwrap();
if self.is_fullscreen {
- (self.display.xf86vmode.XF86VidModeSwitchToMode)(self.display.display, self.screen_id, &mut self.xf86_desk_mode);
+ if let Some(mut xf86_desk_mode) = self.xf86_desk_mode {
+ (self.display.xf86vmode.XF86VidModeSwitchToMode)(self.display.display, self.screen_id, &mut xf86_desk_mode);
+ }
(self.display.xf86vmode.XF86VidModeSetViewPort)(self.display.display, self.screen_id, 0, 0);
}
@@ -324,36 +326,31 @@ impl Window {
let mut mode_num: libc::c_int = mem::uninitialized();
let mut modes: *mut *mut ffi::XF86VidModeModeInfo = mem::uninitialized();
if (display.xf86vmode.XF86VidModeGetAllModeLines)(display.display, screen_id, &mut mode_num, &mut modes) == 0 {
- return Err(OsError(format!("Could not query the video modes")));
- }
-
- let xf86_desk_mode: ffi::XF86VidModeModeInfo = ptr::read(*modes.offset(0));
-
- let mode_to_switch_to = if window_attrs.monitor.is_some() {
- let matching_mode = (0 .. mode_num).map(|i| {
- let m: ffi::XF86VidModeModeInfo = ptr::read(*modes.offset(i as isize) as *const _); m
- }).find(|m| m.hdisplay == dimensions.0 as u16 && m.vdisplay == dimensions.1 as u16);
-
- if let Some(matching_mode) = matching_mode {
- Some(matching_mode)
-
- } else {
- let m = (0 .. mode_num).map(|i| {
+ (None, None)
+ } else {
+ let xf86_desk_mode: ffi::XF86VidModeModeInfo = ptr::read(*modes.offset(0));
+ let mode_to_switch_to = if window_attrs.monitor.is_some() {
+ let matching_mode = (0 .. mode_num).map(|i| {
let m: ffi::XF86VidModeModeInfo = ptr::read(*modes.offset(i as isize) as *const _); m
- }).find(|m| m.hdisplay >= dimensions.0 as u16 && m.vdisplay >= dimensions.1 as u16);
-
- match m {
- Some(m) => Some(m),
- None => return Err(OsError(format!("Could not find a suitable graphics mode")))
+ }).find(|m| m.hdisplay == dimensions.0 as u16 && m.vdisplay == dimensions.1 as u16);
+ if let Some(matching_mode) = matching_mode {
+ Some(matching_mode)
+ } else {
+ let m = (0 .. mode_num).map(|i| {
+ let m: ffi::XF86VidModeModeInfo = ptr::read(*modes.offset(i as isize) as *const _); m
+ }).find(|m| m.hdisplay >= dimensions.0 as u16 && m.vdisplay >= dimensions.1 as u16);
+
+ match m {
+ Some(m) => Some(m),
+ None => return Err(OsError(format!("Could not find a suitable graphics mode")))
+ }
}
- }
- } else {
- None
- };
-
- (display.xlib.XFree)(modes as *mut _);
-
- (mode_to_switch_to, xf86_desk_mode)
+ } else {
+ None
+ };
+ (display.xlib.XFree)(modes as *mut _);
+ (mode_to_switch_to, Some(xf86_desk_mode))
+ }
};
// start the context building process