-
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
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
-
374
-
375
-
376
-
377
-
378
-
379
-
380
-
381
-
382
-
383
-
384
-
385
-
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
-
394
-
395
-
396
-
397
-
398
-
399
-
400
-
401
-
402
-
403
-
404
-
405
-
406
-
407
-
408
-
409
-
410
-
411
-
412
-
413
-
414
-
415
-
416
-
417
-
418
-
419
-
420
-
421
-
422
-
423
-
424
-
425
-
426
-
427
-
428
-
429
-
430
-
431
-
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
-
440
-
441
-
442
-
443
-
444
-
445
-
446
-
447
-
448
-
449
-
450
-
451
-
452
-
453
-
454
-
455
-
456
-
457
-
458
-
459
-
460
-
461
-
462
-
463
-
464
-
465
-
466
-
467
-
468
-
469
-
470
-
471
-
472
-
473
-
474
-
475
-
476
-
477
-
478
-
479
-
480
-
481
-
482
-
483
-
484
-
485
-
486
-
487
-
488
-
489
-
490
-
491
-
492
-
493
-
494
-
495
-
496
-
497
-
498
-
499
-
500
-
501
-
502
-
503
-
504
-
505
-
506
-
507
-
508
-
509
-
510
-
511
-
512
-
513
-
514
-
515
-
516
-
517
// 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 Arc = @import("./Arc.zig");
const TransportService = @import("./services/TransportService.zig");
const freelog = @import("./log.zig").freelog;
pub const NowPlaying = extern struct {
const cname = "plac_transport_now_playing";
const allocator = std.heap.c_allocator;
internal: *Internal,
one_line_line1: [*:0]const u8,
two_line_line1: [*:0]const u8,
two_line_line2: ?[*:0]const u8,
three_line_line1: [*:0]const u8,
three_line_line2: ?[*:0]const u8,
three_line_line3: ?[*:0]const u8,
seek_position: u64,
length: u64,
has_seek_position: bool,
has_length: bool,
image_key: ?[*:0]const u8,
pub const Internal = struct {
arc: Arc = .{},
};
pub fn make(src: *const TransportService.NowPlaying) std.mem.Allocator.Error!*NowPlaying {
const one_line_line1 = try allocator.dupeZ(u8, src.one_line.line1);
errdefer allocator.free(one_line_line1);
const two_line_line1 = try allocator.dupeZ(u8, src.two_line.line1);
errdefer allocator.free(two_line_line1);
const two_line_line2 = if (src.two_line.line2) |input|
try allocator.dupeZ(u8, input)
else
null;
errdefer if (two_line_line2) |buf| allocator.free(buf);
const three_line_line1 = try allocator.dupeZ(u8, src.three_line.line1);
errdefer allocator.free(three_line_line1);
const three_line_line2 = if (src.three_line.line2) |input|
try allocator.dupeZ(u8, input)
else
null;
errdefer if (three_line_line2) |buf| allocator.free(buf);
const three_line_line3 = if (src.three_line.line3) |input|
try allocator.dupeZ(u8, input)
else
null;
errdefer if (three_line_line3) |buf| allocator.free(buf);
const image_key = if (src.image_key) |input|
try allocator.dupeZ(u8, input)
else
null;
errdefer if (image_key) |buf| allocator.free(buf);
const internal = try allocator.create(Internal);
errdefer allocator.destroy(internal);
internal.* = .{};
const self = try allocator.create(NowPlaying);
errdefer allocator.destroy(self);
self.* = .{
.internal = internal,
.one_line_line1 = one_line_line1.ptr,
.two_line_line1 = two_line_line1.ptr,
.two_line_line2 = if (two_line_line2) |slice| slice.ptr else null,
.three_line_line1 = three_line_line1,
.three_line_line2 = if (three_line_line2) |slice| slice.ptr else null,
.three_line_line3 = if (three_line_line3) |slice| slice.ptr else null,
.seek_position = src.seek_position orelse 0,
.length = src.length orelse 0,
.has_seek_position = src.seek_position != null,
.has_length = src.length != null,
.image_key = if (image_key) |slice| slice.ptr else null,
};
return self;
}
pub fn retain(ptr: ?*NowPlaying) callconv(.C) *NowPlaying {
var self = ptr orelse @panic(
std.fmt.comptimePrint("Received null pointer on {s}_{s}", .{ cname, @src().fn_name }),
);
self.internal.arc.ref();
return self;
}
pub fn release(ptr: ?*NowPlaying) callconv(.C) void {
var self = ptr orelse @panic(
std.fmt.comptimePrint("Received null pointer on {s}_{s}", .{ cname, @src().fn_name }),
);
if (self.internal.arc.unref()) {
freelog(self);
allocator.free(std.mem.span(self.one_line_line1));
allocator.free(std.mem.span(self.two_line_line1));
if (self.two_line_line2) |line| {
allocator.free(std.mem.span(line));
}
allocator.free(std.mem.span(self.three_line_line1));
if (self.three_line_line2) |line| {
allocator.free(std.mem.span(line));
}
if (self.three_line_line3) |line| {
allocator.free(std.mem.span(line));
}
if (self.image_key) |image_key| {
allocator.free(std.mem.span(image_key));
}
allocator.destroy(self.internal);
allocator.destroy(self);
}
}
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 SeekChange = extern struct {
const cname = "plac_transport_seek_change";
const allocator = std.heap.c_allocator;
internal: *Internal,
zone_id: [*:0]const u8,
seek_position: u64,
has_seek_position: bool,
pub const Internal = struct {
arc: Arc = .{},
};
pub fn make(src: *const TransportService.SeekChange) std.mem.Allocator.Error!*SeekChange {
const zone_id = try allocator.dupeZ(u8, src.zone_id);
errdefer allocator.free(zone_id);
const internal = try allocator.create(Internal);
errdefer allocator.destroy(internal);
internal.* = .{};
const self = try allocator.create(SeekChange);
errdefer allocator.destroy(self);
self.* = .{
.internal = internal,
.zone_id = zone_id.ptr,
.seek_position = src.seek_position orelse 0,
.has_seek_position = src.seek_position != null,
};
return self;
}
pub fn retain(ptr: ?*SeekChange) callconv(.C) *SeekChange {
var self = ptr orelse @panic(
std.fmt.comptimePrint("Received null pointer on {s}_{s}", .{ cname, @src().fn_name }),
);
self.internal.arc.ref();
return self;
}
pub fn release(ptr: ?*SeekChange) callconv(.C) void {
var self = ptr orelse @panic(
std.fmt.comptimePrint("Received null pointer on {s}_{s}", .{ cname, @src().fn_name }),
);
if (self.internal.arc.unref()) {
freelog(self);
allocator.free(std.mem.span(self.zone_id));
allocator.destroy(self.internal);
allocator.destroy(self);
}
}
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 PlaybackState = enum(c_int) {
loading = 0,
stopped = 1,
paused = 2,
playing = 3,
};
pub const Action = packed struct(u16) {
next: bool,
prev: bool,
pause: bool,
play: bool,
seek: bool,
_padding: u11 = 0,
};
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,
now_playing: ?*NowPlaying,
playback: PlaybackState,
allowed_action: Action,
pub const Internal = struct {
arc: Arc = .{},
};
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 now_playing = if (src.now_playing) |*input| now_playing: {
const n = try NowPlaying.make(input);
break :now_playing n.retain();
} else null;
errdefer if (now_playing) |n| n.release();
const internal = try allocator.create(Internal);
errdefer allocator.destroy(internal);
internal.* = .{};
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,
},
.allowed_action = .{
.next = src.is_next_allowed,
.prev = src.is_previous_allowed,
.pause = src.is_pause_allowed,
.play = src.is_play_allowed,
.seek = src.is_seek_allowed,
},
.now_playing = now_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.arc.ref();
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 }),
);
if (self.internal.arc.unref()) {
freelog(self);
if (self.now_playing) |now_playing| {
now_playing.release();
}
allocator.free(std.mem.span(self.id));
allocator.free(std.mem.span(self.name));
allocator.destroy(self.internal);
allocator.destroy(self);
}
}
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,
seek_changes_ptr: [*]const *SeekChange,
seek_changes_len: usize,
pub const Internal = struct {
arc: Arc = .{},
};
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| {
const zone = try Zone.make(&src);
added_zones[added_i] = zone.retain();
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| {
const zone = try Zone.make(&src);
changed_zones[changed_i] = zone.retain();
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 seek_changes = try allocator.alloc(*SeekChange, event.zones_seek_changed.len);
errdefer allocator.free(seek_changes);
var seek_i: usize = 0;
errdefer {
for (0..seek_i) |i| {
seek_changes[i].release();
}
}
for (event.zones_seek_changed) |src| {
const change = try SeekChange.make(&src);
seek_changes[seek_i] = change.retain();
seek_i += 1;
}
const internal = try allocator.create(Internal);
errdefer allocator.destroy(internal);
internal.* = .{};
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,
.seek_changes_ptr = seek_changes.ptr,
.seek_changes_len = seek_changes.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| {
const zone = try Zone.make(&src);
added_zones[added_i] = zone.retain();
added_i += 1;
}
const internal = try allocator.create(Internal);
errdefer allocator.destroy(internal);
internal.* = .{};
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,
.seek_changes_ptr = undefined,
.seek_changes_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.arc.ref();
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 }),
);
if (self.internal.arc.unref()) {
freelog(self);
for (self.added_zones_ptr[0..self.added_zones_len]) |zone| {
zone.release();
}
allocator.free(self.added_zones_ptr[0..self.added_zones_len]);
for (self.changed_zones_ptr[0..self.changed_zones_len]) |zone| {
zone.release();
}
allocator.free(self.changed_zones_ptr[0..self.changed_zones_len]);
for (self.removed_zone_ids_ptr[0..self.removed_zone_ids_len]) |id| {
allocator.free(std.mem.span(id));
}
allocator.free(self.removed_zone_ids_ptr[0..self.removed_zone_ids_len]);
for (self.seek_changes_ptr[0..self.seek_changes_len]) |change| {
change.release();
}
allocator.free(self.seek_changes_ptr[0..self.seek_changes_len]);
allocator.destroy(self.internal);
allocator.destroy(self);
}
}
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 ControlResultCode = enum(c_int) {
ok = 0,
unknown_error = 1,
out_of_memory = 2,
failed_to_send = 3,
closed = 4,
no_action_bit_set = 5,
server_error = 6,
};
pub fn export_capi() void {
SeekChange.export_capi();
NowPlaying.export_capi();
Zone.export_capi();
ZoneListEvent.export_capi();
}