-
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
-
518
-
519
-
520
-
521
-
522
-
523
-
524
-
525
-
526
-
527
-
528
-
529
-
530
-
531
-
532
-
533
-
534
-
535
-
536
-
537
-
538
-
539
-
540
-
541
-
542
-
543
-
544
-
545
-
546
-
547
-
548
-
549
-
550
-
551
-
552
-
553
-
554
-
555
-
556
-
557
-
558
-
559
-
560
-
561
-
562
-
563
-
564
-
565
-
566
-
567
-
568
-
569
-
570
-
571
-
572
-
573
-
574
-
575
-
576
-
577
-
578
-
579
-
580
-
581
-
582
-
583
-
584
-
585
-
586
-
587
-
588
-
589
-
590
-
591
-
592
-
593
-
594
-
595
-
596
-
597
-
598
-
599
-
600
-
601
-
602
-
603
-
604
-
605
-
606
-
607
-
608
-
609
-
610
-
611
-
612
-
613
-
614
-
615
-
616
-
617
-
618
-
619
-
620
-
621
-
622
-
623
-
624
-
625
-
626
-
627
-
628
-
629
-
630
-
631
-
632
-
633
-
634
-
635
-
636
-
637
-
638
-
639
-
640
-
641
-
642
-
643
-
644
-
645
-
646
-
647
-
648
-
649
-
650
-
651
-
652
-
653
-
654
-
655
-
656
-
657
-
658
-
659
-
660
-
661
-
662
-
663
// 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 freelog = @import("./log.zig").freelog;
const BrowseService = @import("./services/BrowseService.zig");
pub const Hierarchy = enum(c_int) {
browse = 0,
playlists = 1,
settings = 2,
internet_radio = 3,
albums = 4,
artists = 5,
genres = 6,
composers = 7,
search = 8,
};
pub const ItemHint = enum(c_int) {
unknown = 0,
action = 1,
action_list = 2,
list = 3,
header = 4,
};
pub const InputPrompt = extern struct {
const cname = "plac_browse_input_prompt";
const allocator = std.heap.c_allocator;
internal: *Internal,
prompt: [*:0]const u8,
action: [*:0]const u8,
default_value: ?[*:0]const u8,
is_password: bool,
pub const Internal = struct {
arc: Arc = .{},
};
pub fn make(src: *const BrowseService.Item.InputPrompt) std.mem.Allocator.Error!*InputPrompt {
const internal = try allocator.create(Internal);
errdefer allocator.destroy(internal);
internal.* = .{};
const prompt = try allocator.dupeZ(u8, src.prompt);
errdefer allocator.free(prompt);
const action = try allocator.dupeZ(u8, src.action);
errdefer allocator.free(action);
const default_value = if (src.value) |value| try allocator.dupeZ(u8, value) else null;
errdefer if (default_value) |slice| allocator.free(slice);
const self = try allocator.create(InputPrompt);
errdefer allocator.destroy(self);
self.* = .{
.internal = internal,
.prompt = prompt.ptr,
.action = action.ptr,
.default_value = if (default_value) |slice| slice.ptr else null,
.is_password = src.is_password,
};
return self;
}
pub fn retain(ptr: ?*InputPrompt) callconv(.C) *InputPrompt {
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: ?*InputPrompt) 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.prompt));
allocator.free(std.mem.span(self.action));
if (self.default_value) |default_value| {
allocator.free(std.mem.span(default_value));
}
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 Item = extern struct {
const cname = "plac_browse_item";
const allocator = std.heap.c_allocator;
internal: *Internal,
title: [*:0]const u8,
subtitle: ?[*:0]const u8,
image_key: ?[*:0]const u8,
item_key: ?[*:0]const u8,
hint: ItemHint,
prompt: ?*InputPrompt,
pub const Internal = struct {
arc: Arc = .{},
};
pub fn make(src: *const BrowseService.Item) std.mem.Allocator.Error!*Item {
const internal = try allocator.create(Internal);
errdefer allocator.destroy(internal);
internal.* = .{};
const title = try allocator.dupeZ(u8, src.title);
errdefer allocator.free(title);
const subtitle = if (src.subtitle) |subtitle| try allocator.dupeZ(u8, subtitle) else null;
errdefer if (subtitle) |slice| allocator.free(slice);
const image_key = if (src.image_key) |key| try allocator.dupeZ(u8, key) else null;
errdefer if (image_key) |slice| allocator.free(slice);
const item_key = if (src.item_key) |key| try allocator.dupeZ(u8, key) else null;
errdefer if (item_key) |slice| allocator.free(slice);
const prompt = if (src.input_prompt) |*input_prompt| prompt: {
const p = try InputPrompt.make(input_prompt);
break :prompt p.retain();
} else null;
errdefer if (prompt) |p| p.release();
const self = try allocator.create(Item);
errdefer allocator.destroy(self);
self.* = .{
.internal = internal,
.title = title,
.subtitle = if (subtitle) |slice| slice.ptr else null,
.image_key = if (image_key) |slice| slice.ptr else null,
.item_key = if (item_key) |slice| slice.ptr else null,
.hint = switch (src.hint) {
.unknown => .unknown,
.action => .action,
.action_list => .action_list,
.header => .header,
.list => .list,
},
.prompt = prompt,
};
return self;
}
pub fn retain(ptr: ?*Item) callconv(.C) *Item {
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: ?*Item) 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.prompt) |prompt| prompt.release();
if (self.item_key) |item_key| allocator.free(std.mem.span(item_key));
if (self.image_key) |image_key| allocator.free(std.mem.span(image_key));
if (self.subtitle) |subtitle| allocator.free(std.mem.span(subtitle));
allocator.free(std.mem.span(self.title));
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 ReplaceItemAction = extern struct {
const cname = "plac_browse_replace_item_action";
const allocator = std.heap.c_allocator;
internal: *Internal,
item: *Item,
pub const Internal = struct {
arc: Arc = .{},
};
pub fn make(item_src: *const BrowseService.Item) std.mem.Allocator.Error!*@This() {
const internal = try allocator.create(Internal);
errdefer allocator.destroy(internal);
internal.* = .{};
const item = try Item.make(item_src);
_ = item.retain();
errdefer item.release();
const self = try allocator.create(@This());
errdefer allocator.destroy(self);
self.* = .{
.internal = internal,
.item = item,
};
return self;
}
pub fn retain(ptr: ?*@This()) callconv(.C) *@This() {
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: ?*@This()) 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);
self.item.release();
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 ListAction = extern struct {
const cname = "plac_browse_list_action";
const allocator = std.heap.c_allocator;
internal: *Internal,
title: [*:0]const u8,
subtitle: ?[*:0]const u8,
image_key: ?[*:0]const u8,
level: u64,
items_ptr: [*]*Item,
items_len: usize,
pub const Internal = struct {
arc: Arc = .{},
};
pub fn make(src: *const BrowseService.Load.Response) std.mem.Allocator.Error!*@This() {
const internal = try allocator.create(Internal);
errdefer allocator.destroy(internal);
internal.* = .{};
const title = try allocator.dupeZ(u8, src.list.title);
errdefer allocator.free(title);
const subtitle = if (src.list.subtitle) |st| try allocator.dupeZ(u8, st) else null;
errdefer if (subtitle) |st| allocator.free(st);
const image_key = if (src.list.image_key) |ik| try allocator.dupeZ(u8, ik) else null;
errdefer if (image_key) |ik| allocator.free(ik);
const items = try allocator.alloc(*Item, src.items.len);
errdefer allocator.free(items);
var items_i: usize = 0;
errdefer for (0..items_i) |i| {
items[i].release();
};
for (src.items) |*item| {
items[items_i] = try Item.make(item);
_ = items[items_i].retain();
items_i += 1;
}
const self = try allocator.create(@This());
errdefer allocator.destroy(self);
self.* = .{
.internal = internal,
.title = title.ptr,
.subtitle = if (subtitle) |st| st.ptr else null,
.image_key = if (image_key) |ik| ik.ptr else null,
.level = src.list.level,
.items_ptr = items.ptr,
.items_len = items.len,
};
return self;
}
pub fn retain(ptr: ?*@This()) callconv(.C) *@This() {
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: ?*@This()) 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);
const items = self.items_ptr[0..self.items_len];
for (items) |item| {
item.release();
}
allocator.free(items);
if (self.image_key) |image_key| allocator.free(std.mem.span(image_key));
if (self.subtitle) |subtitle| allocator.free(std.mem.span(subtitle));
allocator.free(std.mem.span(self.title));
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 ErrorMessageAction = extern struct {
const cname = "plac_browse_error_message_action";
const allocator = std.heap.c_allocator;
internal: *Internal,
message: [*:0]const u8,
pub const Internal = struct {
arc: Arc = .{},
};
pub fn make(message_src: []const u8) std.mem.Allocator.Error!*@This() {
const internal = try allocator.create(Internal);
errdefer allocator.destroy(internal);
internal.* = .{};
const message = try allocator.dupeZ(u8, message_src);
errdefer allocator.free(message);
const self = try allocator.create(@This());
errdefer allocator.destroy(self);
self.* = .{
.internal = internal,
.message = message.ptr,
};
return self;
}
pub fn retain(ptr: ?*@This()) callconv(.C) *@This() {
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: ?*@This()) 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.message));
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 MessageAction = extern struct {
const cname = "plac_browse_message_action";
const allocator = std.heap.c_allocator;
internal: *Internal,
message: [*:0]const u8,
pub const Internal = struct {
arc: Arc = .{},
};
pub fn make(message_src: []const u8) std.mem.Allocator.Error!*@This() {
const internal = try allocator.create(Internal);
errdefer allocator.destroy(internal);
internal.* = .{};
const message = try allocator.dupeZ(u8, message_src);
errdefer allocator.free(message);
const self = try allocator.create(@This());
errdefer allocator.destroy(self);
self.* = .{
.internal = internal,
.message = message.ptr,
};
return self;
}
pub fn retain(ptr: ?*@This()) callconv(.C) *@This() {
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: ?*@This()) 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.message));
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 ResultCode = enum(c_int) {
ok = 0,
unknown_error = 1,
missing_property = 2,
out_of_memory = 3,
failed_to_send = 4,
closed = 5,
timeout = 6,
};
pub const ResultAction = enum(c_int) {
none = 0,
replace_item = 1,
remove_item = 2,
list = 3,
error_message = 4,
message = 5,
};
pub const Result = extern struct {
const cname = "plac_browse_result";
const allocator = std.heap.c_allocator;
internal: *Internal,
code: ResultCode,
action: ResultAction = .none,
pub const Payload = union(ResultAction) {
none: void,
replace_item: *ReplaceItemAction,
remove_item: void,
list: *ListAction,
error_message: *ErrorMessageAction,
message: *MessageAction,
};
pub const Internal = struct {
arc: Arc = .{},
payload: Payload = .none,
};
fn makeError(code: ResultCode) std.mem.Allocator.Error!*@This() {
const internal = try allocator.create(Internal);
errdefer allocator.destroy(internal);
internal.* = .{};
const self = try allocator.create(@This());
errdefer allocator.destroy(self);
self.* = .{
.internal = internal,
.code = code,
};
return self;
}
pub fn makeRetainedError(code: ResultCode) std.mem.Allocator.Error!*@This() {
const result = try makeError(code);
return result.retain();
}
fn make(payload: Payload) std.mem.Allocator.Error!*@This() {
const internal = try allocator.create(Internal);
errdefer allocator.destroy(internal);
switch (payload) {
.replace_item => |action| _ = action.retain(),
.list => |action| _ = action.retain(),
.error_message => |action| _ = action.retain(),
.message => |action| _ = action.retain(),
else => {},
}
errdefer switch (payload) {
.replace_item => |action| action.release(),
.list => |action| action.release(),
.error_message => |action| action.release(),
.message => |action| action.release(),
else => {},
};
internal.* = .{
.payload = payload,
};
const self = try allocator.create(@This());
errdefer allocator.destroy(self);
self.* = .{
.internal = internal,
.code = .ok,
.action = switch (payload) {
.none => .none,
.replace_item => .replace_item,
.remove_item => .remove_item,
.list => .list,
.error_message => .error_message,
.message => .message,
},
};
return self;
}
pub fn makeRetained(payload: Payload) std.mem.Allocator.Error!*@This() {
const result = try make(payload);
return result.retain();
}
fn exportActionGetter(comptime Action: type, tag: ResultAction, name: []const u8) void {
const Getter = struct {
fn get(ptr: ?*Result) callconv(.C) *Action {
const self = ptr orelse @panic(
std.fmt.comptimePrint(
"Received null pointer on {s}_{s}",
.{ cname, name },
),
);
if (@intFromEnum(self.internal.payload) == @intFromEnum(tag)) {
return @field(self.internal.payload, @tagName(tag)).retain();
} else {
std.log.err("{s}_{s} called on {s}", .{
cname,
name,
@tagName(self.internal.payload),
});
@panic(std.fmt.comptimePrint("Union tag mismatch on {s}_{s}", .{ cname, name }));
}
}
};
@export(&Getter.get, .{ .name = std.fmt.comptimePrint("{s}_{s}", .{ cname, name }) });
}
pub fn retain(ptr: ?*@This()) callconv(.C) *@This() {
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: ?*@This()) 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);
switch (self.internal.payload) {
.replace_item => |action| action.release(),
.list => |action| action.release(),
.error_message => |action| action.release(),
.message => |action| action.release(),
else => {},
}
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}) });
exportActionGetter(ReplaceItemAction, .replace_item, "get_replace_item_action");
exportActionGetter(ListAction, .list, "get_list_action");
exportActionGetter(ErrorMessageAction, .error_message, "get_error_message_action");
exportActionGetter(MessageAction, .message, "get_message_action");
}
};
pub fn export_capi() void {
InputPrompt.export_capi();
Item.export_capi();
ReplaceItemAction.export_capi();
ListAction.export_capi();
ErrorMessageAction.export_capi();
MessageAction.export_capi();
Result.export_capi();
}