glfw: make monitor/joystick events a proper enum
Helps hexops/mach#37 Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
bec6f7a85b
commit
f1644d2e59
3 changed files with 29 additions and 16 deletions
|
|
@ -288,10 +288,19 @@ pub inline fn getUserPointer(self: Joystick, Type: anytype) ?Type {
|
|||
return null;
|
||||
}
|
||||
|
||||
var _callback: ?fn (joystick: Joystick, event: isize) void = null;
|
||||
/// Describes an event relating to a joystick.
|
||||
pub const Event = enum(c_int) {
|
||||
/// The device was connected.
|
||||
connected = c.GLFW_CONNECTED,
|
||||
|
||||
/// The device was disconnected.
|
||||
disconnected = c.GLFW_DISCONNECTED,
|
||||
};
|
||||
|
||||
var _callback: ?fn (joystick: Joystick, event: Event) void = null;
|
||||
|
||||
fn callbackWrapper(jid: c_int, event: c_int) callconv(.C) void {
|
||||
_callback.?(Joystick{ .jid = jid }, @intCast(isize, event));
|
||||
_callback.?(Joystick{ .jid = jid }, @intToEnum(Event, event));
|
||||
}
|
||||
|
||||
/// Sets the joystick configuration callback.
|
||||
|
|
@ -307,7 +316,7 @@ fn callbackWrapper(jid: c_int, event: c_int) callconv(.C) void {
|
|||
/// @param[in] callback The new callback, or null to remove the currently set callback.
|
||||
///
|
||||
/// @callback_param `jid` The joystick that was connected or disconnected.
|
||||
/// @callback_param `event` One of `glfw.connected` or `glfw.disconnected`. Future releases may add
|
||||
/// @callback_param `event` One of `.connected` or `.disconnected`. Future releases may add
|
||||
/// more events.
|
||||
///
|
||||
/// Possible errors include glfw.Error.NotInitialized.
|
||||
|
|
@ -315,7 +324,7 @@ fn callbackWrapper(jid: c_int, event: c_int) callconv(.C) void {
|
|||
/// @thread_safety This function must only be called from the main thread.
|
||||
///
|
||||
/// see also: joystick_event
|
||||
pub inline fn setCallback(callback: ?fn (joystick: Joystick, event: isize) void) void {
|
||||
pub inline fn setCallback(callback: ?fn (joystick: Joystick, event: Event) void) void {
|
||||
_callback = callback;
|
||||
_ = if (_callback != null) c.glfwSetJoystickCallback(callbackWrapper) else c.glfwSetJoystickCallback(null);
|
||||
|
||||
|
|
@ -527,7 +536,7 @@ test "setCallback" {
|
|||
defer glfw.terminate();
|
||||
|
||||
glfw.Joystick.setCallback((struct {
|
||||
pub fn callback(joystick: Joystick, event: isize) void {
|
||||
pub fn callback(joystick: Joystick, event: Event) void {
|
||||
_ = joystick;
|
||||
_ = event;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -348,29 +348,37 @@ pub inline fn getPrimary() Error!?Monitor {
|
|||
var callback_fn_ptr: ?usize = null;
|
||||
var callback_data_ptr: ?usize = undefined;
|
||||
|
||||
/// Describes an event relating to a monitor.
|
||||
pub const Event = enum(c_int) {
|
||||
/// The device was connected.
|
||||
connected = c.GLFW_CONNECTED,
|
||||
|
||||
/// The device was disconnected.
|
||||
disconnected = c.GLFW_DISCONNECTED,
|
||||
};
|
||||
|
||||
/// Sets the monitor configuration callback.
|
||||
///
|
||||
/// This function sets the monitor configuration callback, or removes the currently set callback.
|
||||
/// This is called when a monitor is connected to or disconnected from the system. Example:
|
||||
///
|
||||
/// ```
|
||||
/// fn monitorCallback(monitor: glfw.Monitor, event: usize, data: *MyData) void {
|
||||
/// fn monitorCallback(monitor: glfw.Monitor, event: glfw.Monitor.Event, data: *MyData) void {
|
||||
/// // data is the pointer you passed into setCallback.
|
||||
/// // event is one of glfw.connected or glfw.disconnected
|
||||
/// // event is one of .connected or .disconnected
|
||||
/// }
|
||||
/// ...
|
||||
/// glfw.Monitor.setCallback(MyData, &myData, monitorCallback)
|
||||
/// ```
|
||||
///
|
||||
/// `event` may be one of glfw.connected or glfw.disconnected. More events may be added in the
|
||||
/// future.
|
||||
/// `event` may be one of .connected or .disconnected. More events may be added in the future.
|
||||
///
|
||||
/// Possible errors include glfw.Error.NotInitialized.
|
||||
///
|
||||
/// @thread_safety This function must only be called from the main thread.
|
||||
///
|
||||
/// see also: monitor_event
|
||||
pub inline fn setCallback(comptime Data: type, data: *Data, f: ?*const fn (monitor: Monitor, event: usize, data: *Data) void) Error!void {
|
||||
pub inline fn setCallback(comptime Data: type, data: *Data, f: ?*const fn (monitor: Monitor, event: Event, data: *Data) void) Error!void {
|
||||
if (f) |new_callback| {
|
||||
callback_fn_ptr = @ptrToInt(new_callback);
|
||||
callback_data_ptr = @ptrToInt(data);
|
||||
|
|
@ -380,7 +388,7 @@ pub inline fn setCallback(comptime Data: type, data: *Data, f: ?*const fn (monit
|
|||
const callback = @intToPtr(NewCallback, callback_fn_ptr.?);
|
||||
callback.*(
|
||||
Monitor{ .handle = monitor.? },
|
||||
@intCast(usize, event),
|
||||
@intToEnum(Event, event),
|
||||
@intToPtr(*Data, callback_data_ptr.?),
|
||||
);
|
||||
}
|
||||
|
|
@ -489,7 +497,7 @@ test "setCallback" {
|
|||
|
||||
var custom_data: u32 = 5;
|
||||
try setCallback(u32, &custom_data, &(struct {
|
||||
fn callback(monitor: Monitor, event: usize, data: *u32) void {
|
||||
fn callback(monitor: Monitor, event: Event, data: *u32) void {
|
||||
_ = monitor;
|
||||
_ = event;
|
||||
_ = data;
|
||||
|
|
|
|||
|
|
@ -30,10 +30,6 @@ pub const osmesa_context_api = c.GLFW_OSMESA_CONTEXT_API;
|
|||
/// Possible value for various window hints, etc.
|
||||
pub const dont_care = c.GLFW_DONT_CARE;
|
||||
|
||||
// TODO(enumify)
|
||||
pub const connected = c.GLFW_CONNECTED;
|
||||
pub const disconnected = c.GLFW_DISCONNECTED;
|
||||
|
||||
// TODO(enumify)
|
||||
/// Joystick hat buttons init hint.
|
||||
pub const joystick_hat_buttons = c.GLFW_JOYSTICK_HAT_BUTTONS;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue