Skip to content

Commit 233be3a

Browse files
committed
big ass refactor
1 parent a7f9e38 commit 233be3a

10 files changed

Lines changed: 132 additions & 130 deletions

File tree

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ Smol low-level windowing abstraction with a focus on audio plugin development
66
77
## Goals
88

9-
- Small API surface
10-
- `picoview` should be easy to use and provide only the essentials for window creation, event handling and OS abstraction.
119
- Low compile times
1210
- `picoview` should compile fast and not bloat your compile times. Uses minimum amount of dependencies.
1311
- Complete OS abstraction

ROADMAP.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
| - `WindowClose` | :ok: | :ok: | :ok: | Low |
2020
| - `WindowFocus` | :ok: | :ok: | :ok: | Low |
2121
| - `WindowScale` | :ok: | :ok: | :ok:[^1] | Medium |
22-
| - `WindowTheme` | :x: | :x: | :ok: | Low |
2322
| - `WindowMove` | :ok: | :x: | :ok: | Low |
2423
| - `WindowResize` | :ok: | :ok: | :ok: | High |
2524
| - `WindowFrame` | :ok: | :ok: | :ok: | High |
@@ -30,7 +29,7 @@
3029
| Event loop wakeup | :ok: | :ok: | :ok: | High |
3130
| Vertical blank synchronization | :ok:[^2] | :ok: | :o:[^3] | High |
3231
| OpenGL context creation | :ok: | :ok: | :ok: | High |
33-
| Clipboard text get/set | :ok: | :ok: | :x: | Medium |
32+
| Clipboard text get/set | :ok: | :ok: | :ok: | Medium |
3433
| Pixel scaling abstraction | :ok: | :x: | :ok: | High |
3534
| Set position | :ok: | :x: | :ok: | Medium |
3635
| Set size | :ok: | :ok: | :ok: | High |
@@ -56,8 +55,4 @@
5655
- Crashes when closing any window (even transient).
5756
- Windows:
5857
- Some cursor icons are not supported, fallbacks used.
59-
- `Event::WindowMove` reports wrong absolute position - better WindowMove api needed.
60-
- X11:
61-
- No clipboard support yet (really hard to implement)
62-
- `WindowWaker::wakeup` does not report an error if the window is closed.
63-
58+
- `Event::WindowMove` reports wrong absolute position - better WindowMove api needed.

src/data.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,6 @@ pub enum Key {
264264

265265
/// An event generated by the windowing system and delivered to the event
266266
/// handler.
267-
///
268-
/// The core type of `picoview`.
269267
#[derive(Debug)]
270268
#[non_exhaustive]
271269
pub enum Event<'a> {
@@ -311,10 +309,7 @@ pub enum Event<'a> {
311309
///
312310
/// See [`Window::set_position`](`crate::Window::set_position`) for
313311
/// details on coordinate system.
314-
relative: Point,
315-
316-
/// The new position of the window relative to the entire screen.
317-
absolute: Point,
312+
point: Point,
318313
},
319314

320315
/// Frame event. You should redraw the window in response to this event.

src/opengl.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use std::ffi::{CStr, c_void};
2-
31
use crate::{MakeCurrentError, SwapBuffersError};
2+
use std::{
3+
ffi::{CStr, c_void},
4+
fmt,
5+
};
46

57
/// A requested OpenGL version
68
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -96,17 +98,27 @@ impl Default for GlConfig {
9698
}
9799

98100
/// OpenGL context belonging to a window
99-
pub trait GlContext {
100-
/// Get the address of an OpenGL function
101-
fn get_proc_address(&self, name: &CStr) -> *const c_void;
102-
103-
/// Make the OpenGL context active or inactive on the current thread
104-
/// Returns true on success
105-
///
106-
/// All OpenGL calls must be made only when the context is active for the
107-
/// current thread
108-
fn make_current(&self, current: bool) -> Result<(), MakeCurrentError>;
109-
110-
/// Swap the front and back buffers
111-
fn swap_buffers(&self) -> Result<(), SwapBuffersError>;
101+
pub struct GlContext<'a>(pub(crate) crate::Window<'a>);
102+
103+
impl<'a> GlContext<'a> {
104+
/// Make this OpenGL context current or not current
105+
pub fn make_current(&self, current: bool) -> Result<(), MakeCurrentError> {
106+
self.0.0.opengl_make_current(current)
107+
}
108+
109+
/// Swap the front and back buffers if double buffering is enabled
110+
pub fn swap_buffers(&self) -> Result<(), SwapBuffersError> {
111+
self.0.0.opengl_swap_buffers()
112+
}
113+
114+
/// Get the address of an OpenGL function by name
115+
pub fn get_proc_address(&self, name: &CStr) -> *const c_void {
116+
self.0.0.opengl_get_proc_address(name)
117+
}
118+
}
119+
120+
impl<'a> fmt::Debug for GlContext<'a> {
121+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122+
f.debug_tuple("GlContext").finish_non_exhaustive()
123+
}
112124
}

src/platform/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ pub mod mac;
1414
#[cfg(target_os = "macos")]
1515
pub use mac::*;
1616

17-
use crate::{GlContext, MouseCursor, Point, Size, WakeupError, WindowWaker, rwh_06};
17+
use crate::{
18+
MakeCurrentError, MouseCursor, Point, Size, SwapBuffersError, WakeupError, WindowWaker, rwh_06,
19+
};
20+
use std::ffi::{CStr, c_void};
1821

1922
#[derive(Clone, Copy)]
2023
pub enum OpenMode {
@@ -40,7 +43,6 @@ pub trait PlatformWindow /* : !Send + !Sync */ {
4043

4144
fn close(&self);
4245
fn waker(&self) -> WindowWaker;
43-
fn opengl(&self) -> Option<&dyn GlContext>;
4446

4547
fn set_title(&self, title: &str);
4648
fn set_cursor_icon(&self, icon: MouseCursor);
@@ -53,6 +55,11 @@ pub trait PlatformWindow /* : !Send + !Sync */ {
5355

5456
fn get_clipboard_text(&self) -> Option<String>;
5557
fn set_clipboard_text(&self, text: &str) -> bool;
58+
59+
fn is_opengl_supported(&self) -> bool;
60+
fn opengl_swap_buffers(&self) -> Result<(), SwapBuffersError>;
61+
fn opengl_make_current(&self, current: bool) -> Result<(), MakeCurrentError>;
62+
fn opengl_get_proc_address(&self, name: &CStr) -> *const c_void;
5663
}
5764

5865
pub trait PlatformWaker: Send + Sync + 'static {

src/platform/x11/connection.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use crate::WindowError;
2-
use libc::c_ulong;
32
use raw_window_handle::XlibDisplayHandle;
43
use std::{
54
cell::RefCell,
65
collections::HashMap,
76
ffi::CStr,
87
marker::PhantomData,
98
mem::zeroed,
10-
os::raw::c_int,
9+
os::raw::{c_char, c_int, c_ulong},
1110
ptr::{NonNull, null, null_mut},
1211
str::FromStr,
1312
sync::{LazyLock, Mutex},
@@ -28,8 +27,6 @@ use x11::{
2827
},
2928
};
3029

31-
pub const ATOM_PICOVIEW_WAKEUP: &CStr = c"PICOVIEW_WAKEUP";
32-
3330
unsafe impl Send for Connection {}
3431
pub struct Connection {
3532
display: *mut Display,
@@ -39,7 +36,7 @@ pub struct Connection {
3936
cursor_cache: RefCell<HashMap<usize, c_ulong>>,
4037
atom_cache: RefCell<HashMap<usize, c_ulong>>,
4138

42-
unsync: PhantomData<*mut ()>,
39+
not_sync: PhantomData<*mut ()>,
4340
}
4441

4542
impl Connection {
@@ -51,7 +48,6 @@ impl Connection {
5148
}
5249

5350
XSetErrorHandler(Some(error_handler));
54-
XInternAtom(display, ATOM_PICOVIEW_WAKEUP.as_ptr() as _, 1);
5551

5652
let screen = XDefaultScreen(display);
5753
let connection = Self {
@@ -62,7 +58,7 @@ impl Connection {
6258
cursor_cache: RefCell::new(HashMap::new()),
6359
atom_cache: RefCell::new(HashMap::new()),
6460

65-
unsync: PhantomData,
61+
not_sync: PhantomData,
6662
};
6763

6864
ERRORS_FOR_EACH_DISPLAY
@@ -74,7 +70,7 @@ impl Connection {
7470
}
7571
}
7672

77-
pub fn check_error(&self) -> Result<(), String> {
73+
pub fn last_error(&self) -> Result<(), String> {
7874
let err = ERRORS_FOR_EACH_DISPLAY
7975
.lock()
8076
.expect("poisoned")
@@ -240,7 +236,7 @@ impl Connection {
240236
let mut event = XEvent { type_: 0 };
241237
if XNextEvent(self.display, &mut event) != 0 {
242238
return Err(WindowError::Platform(
243-
self.check_error()
239+
self.last_error()
244240
.err()
245241
.unwrap_or_else(|| "unknown error".to_owned()),
246242
));
@@ -309,19 +305,15 @@ unsafe extern "C" fn error_handler(dpy: *mut Display, err: *mut XErrorEvent) ->
309305
}
310306

311307
unsafe {
312-
let mut buf = [0; 255];
308+
let mut buf = [0 as c_char; 255];
313309
XGetErrorText(
314310
(*err).display,
315311
(*err).error_code.into(),
316-
buf.as_mut_ptr().cast(),
312+
buf.as_mut_ptr(),
317313
(buf.len() - 1) as i32,
318314
);
319315
buf[254] = 0;
320-
conn.replace(
321-
CStr::from_ptr(buf.as_mut_ptr().cast())
322-
.to_string_lossy()
323-
.into(),
324-
);
316+
conn.replace(CStr::from_ptr(buf.as_mut_ptr()).to_string_lossy().into());
325317
}
326318

327319
0

src/platform/x11/gl.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl GlContext {
5353
HashSet::new()
5454
};
5555

56-
connection.check_error().map_err(WindowError::OpenGl)?;
56+
connection.last_error().map_err(WindowError::OpenGl)?;
5757
Ok((major as u8, minor as u8, extensions))
5858
}
5959
}
@@ -254,7 +254,7 @@ impl GlContext {
254254
}
255255

256256
XSync(connection.display(), 0);
257-
connection.check_error().map_err(WindowError::OpenGl)?;
257+
connection.last_error().map_err(WindowError::OpenGl)?;
258258

259259
Ok(GlContext { window, context })
260260
}
@@ -275,23 +275,23 @@ impl GlContext {
275275
}
276276
}
277277

278-
impl<'a> crate::GlContext for GlContextScope<'a> {
279-
fn get_proc_address(&self, symbol: &CStr) -> *const c_void {
278+
impl GlContextScope<'_> {
279+
pub fn get_proc_address(&self, symbol: &CStr) -> *const c_void {
280280
unsafe {
281281
glXGetProcAddress(symbol.as_ptr() as *const u8)
282282
.map(|x| x as *const c_void)
283283
.unwrap_or(null())
284284
}
285285
}
286286

287-
fn swap_buffers(&self) -> Result<(), SwapBuffersError> {
287+
pub fn swap_buffers(&self) -> Result<(), SwapBuffersError> {
288288
unsafe {
289289
glXSwapBuffers(self.connection.display(), self.context.window);
290290
Ok(())
291291
}
292292
}
293293

294-
fn make_current(&self, current: bool) -> Result<(), MakeCurrentError> {
294+
pub fn make_current(&self, current: bool) -> Result<(), MakeCurrentError> {
295295
unsafe {
296296
let result = {
297297
if current {

src/platform/x11/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ pub fn query_cursor(conn: &Connection, cursor: MouseCursor) -> c_ulong {
242242
}
243243
}
244244

245-
pub fn get_position_relative(
245+
pub fn relative_position(
246246
conn: &Connection,
247247
window_parent: c_ulong,
248248
window_id: c_ulong,

0 commit comments

Comments
 (0)