From 8fb886691a650c9a6ece281231b2df773e362a58 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Tue, 15 Sep 2020 00:54:24 +0200 Subject: [PATCH] Start wl_proxy bindings --- client_core.zig | 93 +++++++++++++++++++++++++++++++++++++++++++++++-- util.zig | 53 ++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 util.zig diff --git a/client_core.zig b/client_core.zig index c90c6b0..1524d28 100644 --- a/client_core.zig +++ b/client_core.zig @@ -1,6 +1,93 @@ const std = @import("std"); const os = std.os; +pub usingnamespace @import("util.zig"); + +const wl_proxy = @OpaqueType(); +pub const Proxy = struct { + const Self = @This(); + + impl: *wl_proxy, + + extern fn wl_proxy_create(factory: *wl_proxy, interface: *const Interface) *wl_proxy; + pub fn create(factory: Proxy, interface: *const Interface) error{OutOfMemory}!Self { + return .{ .impl = wl_proxy_create(factory.impl, interface) orelse return error.OutOfMemory }; + } + + extern fn wl_proxy_destroy(proxy: *wl_proxy) void; + pub fn destroy(self: Self) void { + wl_proxy_destroy(self.impl); + } + + extern fn wl_proxy_marshal_array(proxy: *wl_proxy, opcode: u32, args: [*]Argument) void; + pub fn marshal(self: Self, opcode: u32, args: [*]Argument) void { + wl_proxy_marshal_array(self.impl, opcode, args); + } + + extern fn wl_proxy_marshal_array_constructor( + proxy: *wl_proxy, + opcode: u32, + args: [*]Argument, + interface: *Interface, + ) void; + pub fn marshalConstructor( + self: Self, + opcode: u32, + args: [*]Argument, + interface: *const Interface, + ) error{OutOfMemory}!Proxy { + return .{ + .impl = wl_proxy_marshal_array_constructor(self.impl, opcode, args, interface) orelse + return error.OutOfMemory, + }; + } + + extern fn wl_proxy_marshal_array_constructor_versioned( + proxy: *wl_proxy, + opcode: u32, + args: [*]Argument, + interface: *Interface, + version: u32, + ) void; + pub fn marshalConstructorVersioned( + self: Self, + opcode: u32, + args: [*]Argument, + interface: *const Interface, + version: u32, + ) error{OutOfMemory}!Proxy { + return .{ + .impl = wl_proxy_marshal_array_constructor(self.impl, opcode, args, interface, version) orelse + return error.OutOfMemory, + }; + } + + extern fn wl_proxy_add_listener(proxy: *wl_proxy, implementation: [*]fn () void, data: ?*c_void) i32; + pub fn addListener(self: Self, implementation: [*]fn () void, data: *c_void) error{AlreadyHasListener}!void { + if (wl_proxy_add_listener(self.impl, implementation, data) == -1) return error.AlreadyHasListener; + } + + extern fn wl_proxy_set_user_data(proxy: *wl_proxy, user_data: ?*c_void) void; + pub fn setUserData(self: Self, user_data: ?*c_void) void { + wl_proxy_set_user_data(self.impl, user_data); + } + + extern fn wl_proxy_get_user_data(proxy: *wl_proxy) ?*c_void; + pub fn getUserData(self: Self) ?*c_void { + return wl_proxy_get_user_data(self.impl); + } + + extern fn wl_proxy_get_version(proxy: *wl_proxy) u32; + pub fn getVersion(self: Self) u32 { + return wl_proxy_get_version(self.impl); + } + + extern fn wl_proxy_get_id(proxy: *wl_proxy) u32; + pub fn getId(self: Self) u32 { + return wl_proxy_get_id(self.impl); + } +}; + const wl_display = @OpaqueType(); pub const Display = struct { const Self = @This(); @@ -8,12 +95,12 @@ pub const Display = struct { impl: *wl_display, extern fn wl_display_connect(name: ?[*:0]const u8) *wl_display; - pub fn connect(name: [*:0]const u8) error{ConnectFailed}!Display { + pub fn connect(name: [*:0]const u8) error{ConnectFailed}!Self { return .{ .impl = wl_display_connect(name) orelse return error.ConnectFailed }; } extern fn wl_display_connect_to_fd(fd: c_int) *wl_display; - pub fn connectToFd(fd: os.fd_t) error{ConnectFailed}!Display { + pub fn connectToFd(fd: os.fd_t) error{ConnectFailed}!Self { return .{ .impl = wl_display_connect_to_fd(fd) orelse return error.ConnectFailed }; } @@ -98,7 +185,7 @@ pub const Display = struct { } extern fn wl_display_create_queue(display: *wl_display) *wl_event_queue; - pub fn createQueue(self: Self) !EventQueue { + pub fn createQueue(self: Self) error{OutOfMemory}!EventQueue { return .{ .impl = wl_display_create_queue(self.impl) orelse return error.OutOfMemory }; } diff --git a/util.zig b/util.zig new file mode 100644 index 0000000..aef9b1f --- /dev/null +++ b/util.zig @@ -0,0 +1,53 @@ +const std = @import("std"); + +pub const Object = @OpaqueType(); + +pub const Message = extern struct { + name: [*:0]const u8, + signature: [*:0]const u8, + types: [*]const *const Interface, +}; + +pub const Interface = extern struct { + name: [*:0]const u8, + version: i32, + method_count: i32, + methods: [*]const Message, + event_count: i32, + events: [*]const Message, +}; + +pub const Array = extern struct { + size: usize, + alloc: usize, + data: *c_void, +}; + +pub const Fixed = extern enum(i32) { + _, + + pub fn toInt(f: Fixed) i24 { + return @enumToInt(f) >> 8; + } + + pub fn fromInt(i: i24) Fixed { + return @intToEnum(Fixed, @as(i32, i) << 8); + } + + pub fn toDouble() i24 { + // TODO + } + pub fn fromDouble(d: f64) Fixed { + // TODO + } +}; + +pub const Argument = extern union { + i: i32, + u: u32, + f: Fixed, + s: [*:0]const u8, + o: *Object, + a: *Array, + h: std.os.fd_t, +};