Preform some general cleanup

This commit is contained in:
Isaac Freund
2020-10-06 23:32:00 +02:00
parent 49780b9609
commit 85bef02e7b
4 changed files with 21 additions and 27 deletions
+9 -14
View File
@@ -51,24 +51,19 @@ pub const Proxy = opaque {
error.OutOfMemory;
}
extern fn wl_proxy_add_listener(proxy: *Proxy, implementation: [*]fn () callconv(.C) void, data: ?*c_void) i32;
pub fn addListener(proxy: *Proxy, implementation: [*]fn () callconv(.C) void, data: ?*c_void) error{AlreadyHasListener}!void {
if (wl_proxy_add_listener(proxy, implementation, data) == -1) return error.AlreadyHasListener;
}
const DispatcherFn = fn (
implementation: ?*const c_void,
proxy: *Proxy,
opcode: u32,
message: *const common.Message,
args: [*]common.Argument,
) callconv(.C) i32;
) callconv(.C) c_int;
extern fn wl_proxy_add_dispatcher(
proxy: *Proxy,
dispatcher: DispatcherFn,
implementation: ?*const c_void,
data: ?*c_void,
) i32;
) c_int;
pub fn addDispatcher(
proxy: *Proxy,
dispatcher: DispatcherFn,
@@ -108,7 +103,7 @@ pub const EventQueue = opaque {
}
};
pub fn Dispatcher(comptime ObjectT: type, comptime DataT: type) type {
pub fn Dispatcher(comptime Object: type, comptime Data: type) type {
return struct {
pub fn dispatcher(
implementation: ?*const c_void,
@@ -116,8 +111,8 @@ pub fn Dispatcher(comptime ObjectT: type, comptime DataT: type) type {
opcode: u32,
message: *const common.Message,
args: [*]common.Argument,
) callconv(.C) i32 {
inline for (@typeInfo(ObjectT.Event).Union.fields) |event_field, event_num| {
) callconv(.C) c_int {
inline for (@typeInfo(Object.Event).Union.fields) |event_field, event_num| {
if (event_num == opcode) {
var event_data: event_field.field_type = undefined;
inline for (@typeInfo(event_field.field_type).Struct.fields) |f, i| {
@@ -128,11 +123,11 @@ pub fn Dispatcher(comptime ObjectT: type, comptime DataT: type) type {
};
}
const listener = @ptrCast(fn (object: *ObjectT, event: ObjectT.Event, data: DataT) void, implementation);
const listener = @ptrCast(fn (object: *Object, event: Object.Event, data: Data) void, implementation);
listener(
@ptrCast(*ObjectT, proxy),
@unionInit(ObjectT.Event, event_field.name, event_data),
@intToPtr(DataT, @ptrToInt(proxy.getUserData())),
@ptrCast(*Object, proxy),
@unionInit(Object.Event, event_field.name, event_data),
@intToPtr(Data, @ptrToInt(proxy.getUserData())),
);
return 0;
+1 -1
View File
@@ -97,6 +97,6 @@ pub fn createQueue(display: *Display) error{OutOfMemory}!*client.EventQueue {
// TODO: should we interpret this return value?
extern fn wl_display_get_error(display: *Display) c_int;
pub fn getError(display: *Display) i32 {
pub fn getError(display: *Display) c_int {
return wl_display_get_error(display);
}
+4 -4
View File
@@ -1,6 +1,6 @@
const std = @import("std");
pub const Object = opaque{};
pub const Object = opaque {};
pub const Message = extern struct {
name: [*:0]const u8,
@@ -10,10 +10,10 @@ pub const Message = extern struct {
pub const Interface = extern struct {
name: [*:0]const u8,
version: i32,
method_count: i32,
version: c_int,
method_count: c_int,
methods: ?[*]const Message,
event_count: i32,
event_count: c_int,
events: ?[*]const Message,
};
+7 -8
View File
@@ -178,13 +178,13 @@ pub const Global = opaque {
) ?*Global;
pub fn create(
display: *Display,
comptime ObjectT: type,
comptime Object: type,
version: u32,
comptime T: type,
data: T,
bind: fn (client: *Client, data: T, version: u32, id: u32) callconv(.C) void,
) !*Global {
return wl_global_create(display, ObjectT.interface, version, data, bind) orelse
return wl_global_create(display, Object.interface, version, data, bind) orelse
error.GlobalCreateFailed;
}
@@ -206,10 +206,10 @@ pub const Global = opaque {
pub const Resource = opaque {
extern fn wl_resource_create(client: *Client, interface: *const common.Interface, version: c_int, id: u32) ?*Resource;
pub fn create(client: *Client, comptime ObjectT: type, version: u32, id: u32) ?*Resource {
pub fn create(client: *Client, comptime Object: type, version: u32, id: u32) ?*Resource {
// This is only a c_int because of legacy libwayland reasons. Negative versions are invalid.
// Version is a u32 on the wire and for wl_global, wl_proxy, etc.
return wl_resource_create(client, ObjectT.interface, @intCast(c_int, version), id);
return wl_resource_create(client, Object.interface, @intCast(c_int, version), id);
}
extern fn wl_resource_destroy(resource: *Resource) void;
@@ -248,9 +248,8 @@ pub const Resource = opaque {
implementation: ?*const c_void,
data: ?*c_void,
destroy: DestroyFn,
) !void {
if (wl_resource_set_dispatcher(proxy, dispatcher, implementation, data) == -1)
return error.AlreadyHasListener;
) void {
wl_resource_set_dispatcher(resource, dispatcher, implementation, data, destroy);
}
extern fn wl_resource_get_user_data(resource: *Resource) ?*c_void;
@@ -282,7 +281,7 @@ pub const Resource = opaque {
return @intCast(u32, wl_resource_get_version(resource));
}
extern fn wl_resource_set_destructor(resource: *Resource, destroy: fn (*Resource) void) void;
extern fn wl_resource_set_destructor(resource: *Resource, destroy: DestroyFn) void;
pub const setDestructor = wl_resource_set_destructor;
extern fn wl_resource_get_class(resource: *Resource) [*:0]const u8;