-
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
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
// Copyright 2025 Shota FUJI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
const std = @import("std");
const TransportService = @import("./services/transport.zig").TransportService;
pub const PlaybackState = enum(c_int) {
loading = 0,
stopped = 1,
paused = 2,
playing = 3,
};
pub const Zone = extern struct {
const cname = "plac_transport_zone";
const allocator = std.heap.c_allocator;
internal: *Internal,
id: [*:0]const u8,
name: [*:0]const u8,
playback: PlaybackState,
pub const Internal = struct {
id: [:0]const u8,
name: [:0]const u8,
ref_count: i64 = 1,
};
pub fn make(src: *const TransportService.Zone) std.mem.Allocator.Error!*Zone {
const id = try allocator.dupeZ(u8, src.zone_id);
errdefer allocator.free(id);
const name = try allocator.dupeZ(u8, src.display_name);
errdefer allocator.free(name);
const internal = try allocator.create(Internal);
errdefer allocator.destroy(internal);
internal.* = .{
.id = id,
.name = name,
};
const self = try allocator.create(Zone);
errdefer allocator.destroy(self);
self.* = .{
.internal = internal,
.id = id.ptr,
.name = name.ptr,
.playback = switch (src.state) {
.loading => .loading,
.stopped => .stopped,
.paused => .paused,
.playing => .playing,
},
};
return self;
}
pub fn retain(ptr: ?*Zone) callconv(.C) *Zone {
var self = ptr orelse @panic(
std.fmt.comptimePrint("Received null pointer on {s}_{s}", .{ cname, @src().fn_name }),
);
self.internal.ref_count += 1;
return self;
}
pub fn release(ptr: ?*Zone) callconv(.C) void {
var self = ptr orelse @panic(
std.fmt.comptimePrint("Received null pointer on {s}_{s}", .{ cname, @src().fn_name }),
);
self.internal.ref_count -= 1;
if (self.internal.ref_count == 0) {
std.log.debug("Releasing {*}...", .{self});
allocator.free(self.internal.id);
allocator.free(self.internal.name);
allocator.destroy(self.internal);
allocator.destroy(self);
} else if (self.internal.ref_count < 0) {
std.log.warn("Over deref detected {*}, count={d}", .{ self, self.internal.ref_count });
}
}
pub fn export_capi() void {
@export(&retain, .{ .name = std.fmt.comptimePrint("{s}_retain", .{cname}) });
@export(&release, .{ .name = std.fmt.comptimePrint("{s}_release", .{cname}) });
}
};
pub const ZoneListEvent = extern struct {
const cname = "plac_transport_zone_list_event";
const allocator = std.heap.c_allocator;
internal: *Internal,
added_zones_ptr: [*]const *Zone,
added_zones_len: usize,
changed_zones_ptr: [*]const *Zone,
changed_zones_len: usize,
removed_zone_ids_ptr: [*]const [*:0]const u8,
removed_zone_ids_len: usize,
pub const Internal = struct {
added_zones: []const *Zone,
changed_zones: []const *Zone,
removed_zone_ids: []const [*:0]const u8,
ref_count: i64 = 1,
};
pub fn makeFromChanges(event: *const TransportService.SubscribeZoneChanges.Response) std.mem.Allocator.Error!*ZoneListEvent {
const added_zones = try allocator.alloc(*Zone, event.zones_added.len);
errdefer allocator.free(added_zones);
var added_i: usize = 0;
errdefer {
for (0..added_i) |i| {
added_zones[i].release();
}
}
for (event.zones_added) |src| {
added_zones[added_i] = try Zone.make(&src);
added_i += 1;
}
const changed_zones = try allocator.alloc(*Zone, event.zones_changed.len);
errdefer allocator.free(changed_zones);
var changed_i: usize = 0;
errdefer {
for (0..changed_i) |i| {
changed_zones[i].release();
}
}
for (event.zones_changed) |src| {
changed_zones[changed_i] = try Zone.make(&src);
changed_i += 1;
}
const removed_zone_ids = try allocator.alloc([*:0]const u8, event.zones_removed.len);
errdefer allocator.free(removed_zone_ids);
var removed_i: usize = 0;
errdefer {
for (0..removed_i) |i| {
allocator.free(std.mem.span(removed_zone_ids[i]));
}
}
for (event.zones_removed) |id| {
removed_zone_ids[removed_i] = (try allocator.dupeZ(u8, id)).ptr;
removed_i += 1;
}
const internal = try allocator.create(Internal);
errdefer allocator.destroy(internal);
internal.* = .{
.added_zones = added_zones,
.changed_zones = changed_zones,
.removed_zone_ids = removed_zone_ids,
};
const self = try allocator.create(ZoneListEvent);
errdefer allocator.destroy(self);
self.* = .{
.internal = internal,
.added_zones_ptr = added_zones.ptr,
.added_zones_len = added_zones.len,
.changed_zones_ptr = changed_zones.ptr,
.changed_zones_len = changed_zones.len,
.removed_zone_ids_ptr = removed_zone_ids.ptr,
.removed_zone_ids_len = removed_zone_ids.len,
};
return self;
}
pub fn makeFromInitial(
event: *const TransportService.SubscribeZoneChanges.InitialResponse,
) std.mem.Allocator.Error!*ZoneListEvent {
const added_zones = try allocator.alloc(*Zone, event.zones.len);
errdefer allocator.free(added_zones);
var added_i: usize = 0;
errdefer {
for (0..added_i) |i| {
added_zones[i].release();
}
}
for (event.zones) |src| {
added_zones[added_i] = try Zone.make(&src);
added_i += 1;
}
const internal = try allocator.create(Internal);
errdefer allocator.destroy(internal);
internal.* = .{
.added_zones = added_zones,
.changed_zones = &.{},
.removed_zone_ids = &.{},
};
const self = try allocator.create(ZoneListEvent);
errdefer allocator.destroy(self);
self.* = .{
.internal = internal,
.added_zones_ptr = added_zones.ptr,
.added_zones_len = added_zones.len,
.changed_zones_ptr = undefined,
.changed_zones_len = 0,
.removed_zone_ids_ptr = undefined,
.removed_zone_ids_len = 0,
};
return self;
}
pub fn retain(ptr: ?*ZoneListEvent) callconv(.C) *ZoneListEvent {
var self = ptr orelse @panic(
std.fmt.comptimePrint("Received null pointer on {s}_{s}", .{ cname, @src().fn_name }),
);
self.internal.ref_count += 1;
return self;
}
pub fn release(ptr: ?*ZoneListEvent) callconv(.C) void {
var self = ptr orelse @panic(
std.fmt.comptimePrint("Received null pointer on {s}_{s}", .{ cname, @src().fn_name }),
);
self.internal.ref_count -= 1;
if (self.internal.ref_count == 0) {
std.log.debug("Releasing {*}...", .{self});
for (self.internal.added_zones) |zone| {
zone.release();
}
allocator.free(self.internal.added_zones);
for (self.internal.changed_zones) |zone| {
zone.release();
}
allocator.free(self.internal.changed_zones);
for (self.internal.removed_zone_ids) |id| {
allocator.free(std.mem.span(id));
}
allocator.free(self.internal.removed_zone_ids);
allocator.destroy(self.internal);
allocator.destroy(self);
} else if (self.internal.ref_count < 0) {
std.log.warn("Over deref detected {*}, count={d}", .{ self, self.internal.ref_count });
}
}
pub fn export_capi() void {
@export(&retain, .{ .name = std.fmt.comptimePrint("{s}_retain", .{cname}) });
@export(&release, .{ .name = std.fmt.comptimePrint("{s}_release", .{cname}) });
}
};
pub fn export_capi() void {
Zone.export_capi();
ZoneListEvent.export_capi();
}