1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
const std = @import("std");
const key = @import("key.zig");
const Config = @import("Config.zig");
const Color = Config.Color;
const Key = key.Key;
const Value = key.Value;
/// Get a value from the config by key into the given pointer. This is
/// specifically for C-compatible APIs. If you're using Zig, just access
/// the configuration directly.
///
/// The return value is false if the given key is not supported by the
/// C API yet. This is a fixable problem so if it is important to support
/// some key, please open an issue.
pub fn get(config: *const Config, k: Key, ptr_raw: *anyopaque) bool {
@setEvalBranchQuota(10_000);
switch (k) {
inline else => |tag| {
const value = fieldByKey(config, tag);
return getValue(ptr_raw, value);
},
}
}
/// Get the value anytype and put it into the pointer. Returns false if
/// the type is not supported by the C API yet or the value is null.
fn getValue(ptr_raw: *anyopaque, value: anytype) bool {
switch (@TypeOf(value)) {
?[:0]const u8 => {
const ptr: *?[*:0]const u8 = @ptrCast(@alignCast(ptr_raw));
ptr.* = if (value) |slice| @ptrCast(slice.ptr) else null;
},
bool => {
const ptr: *bool = @ptrCast(@alignCast(ptr_raw));
ptr.* = value;
},
u8, u32 => {
const ptr: *c_uint = @ptrCast(@alignCast(ptr_raw));
ptr.* = @intCast(value);
},
i16 => {
const ptr: *c_short = @ptrCast(@alignCast(ptr_raw));
ptr.* = @intCast(value);
},
f32, f64 => |Float| {
const ptr: *Float = @ptrCast(@alignCast(ptr_raw));
ptr.* = @floatCast(value);
},
else => |T| switch (@typeInfo(T)) {
.optional => {
// If an optional has no value we return false.
const unwrapped = value orelse return false;
return getValue(ptr_raw, unwrapped);
},
.@"enum" => {
const ptr: *[*:0]const u8 = @ptrCast(@alignCast(ptr_raw));
ptr.* = @tagName(value);
},
.@"struct" => |info| {
// If the struct implements cval then we call then.
if (@hasDecl(T, "cval")) {
const PtrT = @typeInfo(@TypeOf(T.cval)).@"fn".return_type.?;
const ptr: *PtrT = @ptrCast(@alignCast(ptr_raw));
ptr.* = value.cval();
return true;
}
// Packed structs that are less than or equal to the
// size of a C int can be passed directly as their
// bit representation.
if (info.layout != .@"packed") return false;
const Backing = info.backing_integer orelse return false;
if (@bitSizeOf(Backing) > @bitSizeOf(c_uint)) return false;
const ptr: *c_uint = @ptrCast(@alignCast(ptr_raw));
ptr.* = @intCast(@as(Backing, @bitCast(value)));
},
.@"union" => |_| {
if (@hasDecl(T, "cval")) {
const PtrT = @typeInfo(@TypeOf(T.cval)).@"fn".return_type.?;
const ptr: *PtrT = @ptrCast(@alignCast(ptr_raw));
ptr.* = value.cval();
return true;
}
return false;
},
else => return false,
},
}
return true;
}
/// Get a value from the config by key.
fn fieldByKey(self: *const Config, comptime k: Key) Value(k) {
const field = comptime field: {
const fields = std.meta.fields(Config);
for (fields) |field| {
if (@field(Key, field.name) == k) {
break :field field;
}
}
unreachable;
};
return @field(self, field.name);
}
test "c_get: u8" {
const testing = std.testing;
const alloc = testing.allocator;
var c = try Config.default(alloc);
defer c.deinit();
c.@"font-size" = 24;
var cval: f32 = undefined;
try testing.expect(get(&c, .@"font-size", &cval));
try testing.expectEqual(@as(f32, 24), cval);
}
test "c_get: enum" {
const testing = std.testing;
const alloc = testing.allocator;
var c = try Config.default(alloc);
defer c.deinit();
c.@"window-theme" = .dark;
var cval: [*:0]u8 = undefined;
try testing.expect(get(&c, .@"window-theme", @ptrCast(&cval)));
const str = std.mem.sliceTo(cval, 0);
try testing.expectEqualStrings("dark", str);
}
test "c_get: color" {
const testing = std.testing;
const alloc = testing.allocator;
var c = try Config.default(alloc);
defer c.deinit();
c.background = .{ .r = 255, .g = 0, .b = 0 };
var cval: Color.C = undefined;
try testing.expect(get(&c, .background, @ptrCast(&cval)));
try testing.expectEqual(255, cval.r);
try testing.expectEqual(0, cval.g);
try testing.expectEqual(0, cval.b);
}
test "c_get: optional" {
const testing = std.testing;
const alloc = testing.allocator;
var c = try Config.default(alloc);
defer c.deinit();
{
c.@"unfocused-split-fill" = null;
var cval: Color.C = undefined;
try testing.expect(!get(&c, .@"unfocused-split-fill", @ptrCast(&cval)));
}
{
c.@"unfocused-split-fill" = .{ .r = 255, .g = 0, .b = 0 };
var cval: Color.C = undefined;
try testing.expect(get(&c, .@"unfocused-split-fill", @ptrCast(&cval)));
try testing.expectEqual(255, cval.r);
try testing.expectEqual(0, cval.g);
try testing.expectEqual(0, cval.b);
}
}
test "c_get: background-blur" {
const testing = std.testing;
const alloc = testing.allocator;
var c = try Config.default(alloc);
defer c.deinit();
{
c.@"background-blur" = .false;
var cval: u8 = undefined;
try testing.expect(get(&c, .@"background-blur", @ptrCast(&cval)));
try testing.expectEqual(0, cval);
}
{
c.@"background-blur" = .true;
var cval: u8 = undefined;
try testing.expect(get(&c, .@"background-blur", @ptrCast(&cval)));
try testing.expectEqual(20, cval);
}
{
c.@"background-blur" = .{ .radius = 42 };
var cval: u8 = undefined;
try testing.expect(get(&c, .@"background-blur", @ptrCast(&cval)));
try testing.expectEqual(42, cval);
}
}
|