forked from obsproject/obs-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobs-data.c
2323 lines (1868 loc) · 55.8 KB
/
obs-data.c
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
Copyright (C) 2014 by Hugh Bailey <obs.jim@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "util/bmem.h"
#include "util/threading.h"
#include "util/dstr.h"
#include "util/darray.h"
#include "util/platform.h"
#include "graphics/vec2.h"
#include "graphics/vec3.h"
#include "graphics/vec4.h"
#include "graphics/quat.h"
#include "obs-data.h"
#include <jansson.h>
struct obs_data_item {
volatile long ref;
struct obs_data *parent;
struct obs_data_item *next;
enum obs_data_type type;
size_t name_len;
size_t data_len;
size_t data_size;
size_t default_len;
size_t default_size;
size_t autoselect_size;
size_t capacity;
};
struct obs_data {
volatile long ref;
char *json;
struct obs_data_item *first_item;
};
struct obs_data_array {
volatile long ref;
DARRAY(obs_data_t *) objects;
};
struct obs_data_number {
enum obs_data_number_type type;
union {
long long int_val;
double double_val;
};
};
/* ------------------------------------------------------------------------- */
/* Item structure, designed to be one allocation only */
static inline size_t get_align_size(size_t size)
{
const size_t alignment = base_get_alignment();
return (size + alignment - 1) & ~(alignment - 1);
}
/* ensures data after the name has alignment (in case of SSE) */
static inline size_t get_name_align_size(const char *name)
{
size_t name_size = strlen(name) + 1;
size_t alignment = base_get_alignment();
size_t total_size;
total_size = sizeof(struct obs_data_item) + (name_size + alignment - 1);
total_size &= ~(alignment - 1);
return total_size - sizeof(struct obs_data_item);
}
static inline char *get_item_name(struct obs_data_item *item)
{
return (char *)item + sizeof(struct obs_data_item);
}
static inline void *get_data_ptr(obs_data_item_t *item)
{
return (uint8_t *)get_item_name(item) + item->name_len;
}
static inline void *get_item_data(struct obs_data_item *item)
{
if (!item->data_size && !item->default_size && !item->autoselect_size)
return NULL;
return get_data_ptr(item);
}
static inline void *get_default_data_ptr(obs_data_item_t *item)
{
return (uint8_t *)get_data_ptr(item) + item->data_len;
}
static inline void *get_item_default_data(struct obs_data_item *item)
{
return item->default_size ? get_default_data_ptr(item) : NULL;
}
static inline void *get_autoselect_data_ptr(obs_data_item_t *item)
{
return (uint8_t *)get_default_data_ptr(item) + item->default_len;
}
static inline void *get_item_autoselect_data(struct obs_data_item *item)
{
return item->autoselect_size ? get_autoselect_data_ptr(item) : NULL;
}
static inline size_t obs_data_item_total_size(struct obs_data_item *item)
{
return sizeof(struct obs_data_item) + item->name_len + item->data_len +
item->default_len + item->autoselect_size;
}
static inline obs_data_t *get_item_obj(struct obs_data_item *item)
{
if (!item)
return NULL;
obs_data_t **data = get_item_data(item);
if (!data)
return NULL;
return *data;
}
static inline obs_data_t *get_item_default_obj(struct obs_data_item *item)
{
if (!item || !item->default_size)
return NULL;
return *(obs_data_t **)get_default_data_ptr(item);
}
static inline obs_data_t *get_item_autoselect_obj(struct obs_data_item *item)
{
if (!item || !item->autoselect_size)
return NULL;
return *(obs_data_t **)get_autoselect_data_ptr(item);
}
static inline obs_data_array_t *get_item_array(struct obs_data_item *item)
{
obs_data_array_t **array;
if (!item)
return NULL;
array = (obs_data_array_t **)get_item_data(item);
return array ? *array : NULL;
}
static inline obs_data_array_t *
get_item_default_array(struct obs_data_item *item)
{
if (!item || !item->default_size)
return NULL;
return *(obs_data_array_t **)get_default_data_ptr(item);
}
static inline obs_data_array_t *
get_item_autoselect_array(struct obs_data_item *item)
{
if (!item || !item->autoselect_size)
return NULL;
return *(obs_data_array_t **)get_autoselect_data_ptr(item);
}
static inline void item_data_release(struct obs_data_item *item)
{
if (!obs_data_item_has_user_value(item))
return;
if (item->type == OBS_DATA_OBJECT) {
obs_data_t *obj = get_item_obj(item);
obs_data_release(obj);
} else if (item->type == OBS_DATA_ARRAY) {
obs_data_array_t *array = get_item_array(item);
obs_data_array_release(array);
}
}
static inline void item_default_data_release(struct obs_data_item *item)
{
if (item->type == OBS_DATA_OBJECT) {
obs_data_t *obj = get_item_default_obj(item);
obs_data_release(obj);
} else if (item->type == OBS_DATA_ARRAY) {
obs_data_array_t *array = get_item_default_array(item);
obs_data_array_release(array);
}
}
static inline void item_autoselect_data_release(struct obs_data_item *item)
{
if (item->type == OBS_DATA_OBJECT) {
obs_data_t *obj = get_item_autoselect_obj(item);
obs_data_release(obj);
} else if (item->type == OBS_DATA_ARRAY) {
obs_data_array_t *array = get_item_autoselect_array(item);
obs_data_array_release(array);
}
}
static inline void item_data_addref(struct obs_data_item *item)
{
if (item->type == OBS_DATA_OBJECT) {
obs_data_t *obj = get_item_obj(item);
obs_data_addref(obj);
} else if (item->type == OBS_DATA_ARRAY) {
obs_data_array_t *array = get_item_array(item);
obs_data_array_addref(array);
}
}
static inline void item_default_data_addref(struct obs_data_item *item)
{
if (!item->data_size)
return;
if (item->type == OBS_DATA_OBJECT) {
obs_data_t *obj = get_item_default_obj(item);
obs_data_addref(obj);
} else if (item->type == OBS_DATA_ARRAY) {
obs_data_array_t *array = get_item_default_array(item);
obs_data_array_addref(array);
}
}
static inline void item_autoselect_data_addref(struct obs_data_item *item)
{
if (item->type == OBS_DATA_OBJECT) {
obs_data_t *obj = get_item_autoselect_obj(item);
obs_data_addref(obj);
} else if (item->type == OBS_DATA_ARRAY) {
obs_data_array_t *array = get_item_autoselect_array(item);
obs_data_array_addref(array);
}
}
static struct obs_data_item *obs_data_item_create(const char *name,
const void *data, size_t size,
enum obs_data_type type,
bool default_data,
bool autoselect_data)
{
struct obs_data_item *item;
size_t name_size, total_size;
if (!name || !data)
return NULL;
name_size = get_name_align_size(name);
total_size = name_size + sizeof(struct obs_data_item) + size;
item = bzalloc(total_size);
item->capacity = total_size;
item->type = type;
item->name_len = name_size;
item->ref = 1;
if (default_data) {
item->default_len = size;
item->default_size = size;
} else if (autoselect_data) {
item->autoselect_size = size;
} else {
item->data_len = size;
item->data_size = size;
}
strcpy(get_item_name(item), name);
memcpy(get_item_data(item), data, size);
item_data_addref(item);
return item;
}
static struct obs_data_item **get_item_prev_next(struct obs_data *data,
struct obs_data_item *current)
{
if (!current || !data)
return NULL;
struct obs_data_item **prev_next = &data->first_item;
struct obs_data_item *item = data->first_item;
while (item) {
if (item == current)
return prev_next;
prev_next = &item->next;
item = item->next;
}
return NULL;
}
static inline void obs_data_item_detach(struct obs_data_item *item)
{
struct obs_data_item **prev_next =
get_item_prev_next(item->parent, item);
if (prev_next) {
*prev_next = item->next;
item->next = NULL;
}
}
static inline void obs_data_item_reattach(struct obs_data_item *old_ptr,
struct obs_data_item *new_ptr)
{
struct obs_data_item **prev_next =
get_item_prev_next(new_ptr->parent, old_ptr);
if (prev_next)
*prev_next = new_ptr;
}
static struct obs_data_item *
obs_data_item_ensure_capacity(struct obs_data_item *item)
{
size_t new_size = obs_data_item_total_size(item);
struct obs_data_item *new_item;
if (item->capacity >= new_size)
return item;
new_item = brealloc(item, new_size);
new_item->capacity = new_size;
obs_data_item_reattach(item, new_item);
return new_item;
}
static inline void obs_data_item_destroy(struct obs_data_item *item)
{
item_data_release(item);
item_default_data_release(item);
item_autoselect_data_release(item);
obs_data_item_detach(item);
bfree(item);
}
static inline void move_data(obs_data_item_t *old_item, void *old_data,
obs_data_item_t *item, void *data, size_t len)
{
ptrdiff_t old_offset = (uint8_t *)old_data - (uint8_t *)old_item;
ptrdiff_t new_offset = (uint8_t *)data - (uint8_t *)item;
if (!old_data)
return;
memmove((uint8_t *)item + new_offset, (uint8_t *)item + old_offset,
len);
}
static inline void obs_data_item_setdata(struct obs_data_item **p_item,
const void *data, size_t size,
enum obs_data_type type)
{
if (!p_item || !*p_item)
return;
struct obs_data_item *item = *p_item;
ptrdiff_t old_default_data_pos =
(uint8_t *)get_default_data_ptr(item) - (uint8_t *)item;
item_data_release(item);
item->data_size = size;
item->type = type;
item->data_len = (item->default_size || item->autoselect_size)
? get_align_size(size)
: size;
item = obs_data_item_ensure_capacity(item);
if (item->default_size || item->autoselect_size)
memmove(get_default_data_ptr(item),
(uint8_t *)item + old_default_data_pos,
item->default_len + item->autoselect_size);
if (size) {
memcpy(get_item_data(item), data, size);
item_data_addref(item);
}
*p_item = item;
}
static inline void obs_data_item_set_default_data(struct obs_data_item **p_item,
const void *data, size_t size,
enum obs_data_type type)
{
if (!p_item || !*p_item)
return;
struct obs_data_item *item = *p_item;
void *old_autoselect_data = get_autoselect_data_ptr(item);
item_default_data_release(item);
item->type = type;
item->default_size = size;
item->default_len = item->autoselect_size ? get_align_size(size) : size;
item->data_len = item->data_size ? get_align_size(item->data_size) : 0;
item = obs_data_item_ensure_capacity(item);
if (item->autoselect_size)
move_data(*p_item, old_autoselect_data, item,
get_autoselect_data_ptr(item), item->autoselect_size);
if (size) {
memcpy(get_item_default_data(item), data, size);
item_default_data_addref(item);
}
*p_item = item;
}
static inline void
obs_data_item_set_autoselect_data(struct obs_data_item **p_item,
const void *data, size_t size,
enum obs_data_type type)
{
if (!p_item || !*p_item)
return;
struct obs_data_item *item = *p_item;
item_autoselect_data_release(item);
item->autoselect_size = size;
item->type = type;
item->data_len = item->data_size ? get_align_size(item->data_size) : 0;
item->default_len =
item->default_size ? get_align_size(item->default_size) : 0;
item = obs_data_item_ensure_capacity(item);
if (size) {
memcpy(get_item_autoselect_data(item), data, size);
item_autoselect_data_addref(item);
}
*p_item = item;
}
/* ------------------------------------------------------------------------- */
static void obs_data_add_json_item(obs_data_t *data, const char *key,
json_t *json);
static inline void obs_data_add_json_object_data(obs_data_t *data, json_t *jobj)
{
const char *item_key;
json_t *jitem;
json_object_foreach (jobj, item_key, jitem) {
obs_data_add_json_item(data, item_key, jitem);
}
}
static inline void obs_data_add_json_object(obs_data_t *data, const char *key,
json_t *jobj)
{
obs_data_t *sub_obj = obs_data_create();
obs_data_add_json_object_data(sub_obj, jobj);
obs_data_set_obj(data, key, sub_obj);
obs_data_release(sub_obj);
}
static void obs_data_add_json_array(obs_data_t *data, const char *key,
json_t *jarray)
{
obs_data_array_t *array = obs_data_array_create();
size_t idx;
json_t *jitem;
json_array_foreach (jarray, idx, jitem) {
obs_data_t *item;
if (!json_is_object(jitem))
continue;
item = obs_data_create();
obs_data_add_json_object_data(item, jitem);
obs_data_array_push_back(array, item);
obs_data_release(item);
}
obs_data_set_array(data, key, array);
obs_data_array_release(array);
}
static void obs_data_add_json_item(obs_data_t *data, const char *key,
json_t *json)
{
if (json_is_object(json))
obs_data_add_json_object(data, key, json);
else if (json_is_array(json))
obs_data_add_json_array(data, key, json);
else if (json_is_string(json))
obs_data_set_string(data, key, json_string_value(json));
else if (json_is_integer(json))
obs_data_set_int(data, key, json_integer_value(json));
else if (json_is_real(json))
obs_data_set_double(data, key, json_real_value(json));
else if (json_is_true(json))
obs_data_set_bool(data, key, true);
else if (json_is_false(json))
obs_data_set_bool(data, key, false);
}
/* ------------------------------------------------------------------------- */
static inline void set_json_string(json_t *json, const char *name,
obs_data_item_t *item)
{
const char *val = obs_data_item_get_string(item);
json_object_set_new(json, name, json_string(val));
}
static inline void set_json_number(json_t *json, const char *name,
obs_data_item_t *item)
{
enum obs_data_number_type type = obs_data_item_numtype(item);
if (type == OBS_DATA_NUM_INT) {
long long val = obs_data_item_get_int(item);
json_object_set_new(json, name, json_integer(val));
} else {
double val = obs_data_item_get_double(item);
json_object_set_new(json, name, json_real(val));
}
}
static inline void set_json_bool(json_t *json, const char *name,
obs_data_item_t *item)
{
bool val = obs_data_item_get_bool(item);
json_object_set_new(json, name, val ? json_true() : json_false());
}
static json_t *obs_data_to_json(obs_data_t *data);
static inline void set_json_obj(json_t *json, const char *name,
obs_data_item_t *item)
{
obs_data_t *obj = obs_data_item_get_obj(item);
json_object_set_new(json, name, obs_data_to_json(obj));
obs_data_release(obj);
}
static inline void set_json_array(json_t *json, const char *name,
obs_data_item_t *item)
{
json_t *jarray = json_array();
obs_data_array_t *array = obs_data_item_get_array(item);
size_t count = obs_data_array_count(array);
for (size_t idx = 0; idx < count; idx++) {
obs_data_t *sub_item = obs_data_array_item(array, idx);
json_t *jitem = obs_data_to_json(sub_item);
json_array_append_new(jarray, jitem);
obs_data_release(sub_item);
}
json_object_set_new(json, name, jarray);
obs_data_array_release(array);
}
static json_t *obs_data_to_json(obs_data_t *data)
{
json_t *json = json_object();
obs_data_item_t *item = NULL;
for (item = obs_data_first(data); item; obs_data_item_next(&item)) {
enum obs_data_type type = obs_data_item_gettype(item);
const char *name = get_item_name(item);
if (!obs_data_item_has_user_value(item))
continue;
if (type == OBS_DATA_STRING)
set_json_string(json, name, item);
else if (type == OBS_DATA_NUMBER)
set_json_number(json, name, item);
else if (type == OBS_DATA_BOOLEAN)
set_json_bool(json, name, item);
else if (type == OBS_DATA_OBJECT)
set_json_obj(json, name, item);
else if (type == OBS_DATA_ARRAY)
set_json_array(json, name, item);
}
return json;
}
/* ------------------------------------------------------------------------- */
obs_data_t *obs_data_create()
{
struct obs_data *data = bzalloc(sizeof(struct obs_data));
data->ref = 1;
return data;
}
obs_data_t *obs_data_create_from_json(const char *json_string)
{
obs_data_t *data = obs_data_create();
json_error_t error;
json_t *root = json_loads(json_string, JSON_REJECT_DUPLICATES, &error);
if (root) {
obs_data_add_json_object_data(data, root);
json_decref(root);
} else {
blog(LOG_ERROR,
"obs-data.c: [obs_data_create_from_json] "
"Failed reading json string (%d): %s",
error.line, error.text);
obs_data_release(data);
data = NULL;
}
return data;
}
obs_data_t *obs_data_create_from_json_file(const char *json_file)
{
char *file_data = os_quick_read_utf8_file(json_file);
obs_data_t *data = NULL;
if (file_data) {
data = obs_data_create_from_json(file_data);
bfree(file_data);
}
return data;
}
obs_data_t *obs_data_create_from_json_file_safe(const char *json_file,
const char *backup_ext)
{
obs_data_t *file_data = obs_data_create_from_json_file(json_file);
if (!file_data && backup_ext && *backup_ext) {
struct dstr backup_file = {0};
dstr_copy(&backup_file, json_file);
if (*backup_ext != '.')
dstr_cat(&backup_file, ".");
dstr_cat(&backup_file, backup_ext);
if (os_file_exists(backup_file.array)) {
blog(LOG_WARNING,
"obs-data.c: "
"[obs_data_create_from_json_file_safe] "
"attempting backup file");
/* delete current file if corrupt to prevent it from
* being backed up again */
os_rename(backup_file.array, json_file);
file_data = obs_data_create_from_json_file(json_file);
}
dstr_free(&backup_file);
}
return file_data;
}
void obs_data_addref(obs_data_t *data)
{
if (data)
os_atomic_inc_long(&data->ref);
}
static inline void obs_data_destroy(struct obs_data *data)
{
struct obs_data_item *item = data->first_item;
while (item) {
struct obs_data_item *next = item->next;
obs_data_item_release(&item);
item = next;
}
/* NOTE: don't use bfree for json text, allocated by json */
free(data->json);
bfree(data);
}
void obs_data_release(obs_data_t *data)
{
if (!data)
return;
if (os_atomic_dec_long(&data->ref) == 0)
obs_data_destroy(data);
}
const char *obs_data_get_json(obs_data_t *data)
{
if (!data)
return NULL;
/* NOTE: don't use libobs bfree for json text */
free(data->json);
data->json = NULL;
json_t *root = obs_data_to_json(data);
data->json = json_dumps(root, JSON_PRESERVE_ORDER | JSON_COMPACT);
json_decref(root);
return data->json;
}
const char *obs_data_get_last_json(obs_data_t *data)
{
return data ? data->json : NULL;
}
bool obs_data_save_json(obs_data_t *data, const char *file)
{
const char *json = obs_data_get_json(data);
if (json && *json) {
return os_quick_write_utf8_file(file, json, strlen(json),
false);
}
return false;
}
bool obs_data_save_json_safe(obs_data_t *data, const char *file,
const char *temp_ext, const char *backup_ext)
{
const char *json = obs_data_get_json(data);
if (json && *json) {
return os_quick_write_utf8_file_safe(
file, json, strlen(json), false, temp_ext, backup_ext);
}
return false;
}
static void get_defaults_array_cb(obs_data_t *data, void *vp)
{
obs_data_array_t *defs = (obs_data_array_t *)vp;
obs_data_t *obs_defaults = obs_data_get_defaults(data);
obs_data_array_push_back(defs, obs_defaults);
obs_data_release(obs_defaults);
}
obs_data_t *obs_data_get_defaults(obs_data_t *data)
{
obs_data_t *defaults = obs_data_create();
if (!data)
return defaults;
struct obs_data_item *item = data->first_item;
while (item) {
const char *name = get_item_name(item);
switch (item->type) {
case OBS_DATA_NULL:
break;
case OBS_DATA_STRING: {
const char *str =
obs_data_get_default_string(data, name);
obs_data_set_string(defaults, name, str);
break;
}
case OBS_DATA_NUMBER: {
switch (obs_data_item_numtype(item)) {
case OBS_DATA_NUM_DOUBLE: {
double val =
obs_data_get_default_double(data, name);
obs_data_set_double(defaults, name, val);
break;
}
case OBS_DATA_NUM_INT: {
long long val =
obs_data_get_default_int(data, name);
obs_data_set_int(defaults, name, val);
break;
}
case OBS_DATA_NUM_INVALID:
break;
}
break;
}
case OBS_DATA_BOOLEAN: {
bool val = obs_data_get_default_bool(data, name);
obs_data_set_bool(defaults, name, val);
break;
}
case OBS_DATA_OBJECT: {
obs_data_t *val = obs_data_get_default_obj(data, name);
obs_data_t *defs = obs_data_get_defaults(val);
obs_data_set_obj(defaults, name, defs);
obs_data_release(defs);
obs_data_release(val);
break;
}
case OBS_DATA_ARRAY: {
obs_data_array_t *arr =
obs_data_get_default_array(data, name);
obs_data_array_t *defs = obs_data_array_create();
obs_data_array_enum(arr, get_defaults_array_cb,
(void *)defs);
obs_data_set_array(defaults, name, defs);
obs_data_array_release(defs);
obs_data_array_release(arr);
break;
}
}
item = item->next;
}
return defaults;
}
static struct obs_data_item *get_item(struct obs_data *data, const char *name)
{
if (!data)
return NULL;
struct obs_data_item *item = data->first_item;
while (item) {
if (strcmp(get_item_name(item), name) == 0)
return item;
item = item->next;
}
return NULL;
}
static void set_item_data(struct obs_data *data, struct obs_data_item **item,
const char *name, const void *ptr, size_t size,
enum obs_data_type type, bool default_data,
bool autoselect_data)
{
obs_data_item_t *new_item = NULL;
if ((!item || !*item) && data) {
new_item = obs_data_item_create(name, ptr, size, type,
default_data, autoselect_data);
obs_data_item_t *prev = obs_data_first(data);
obs_data_item_t *next = obs_data_first(data);
obs_data_item_next(&next);
for (; prev && next;
obs_data_item_next(&prev), obs_data_item_next(&next)) {
if (strcmp(get_item_name(next), name) > 0)
break;
}
new_item->parent = data;
if (prev && strcmp(get_item_name(prev), name) < 0) {
prev->next = new_item;
new_item->next = next;
} else {
data->first_item = new_item;
new_item->next = prev;
}
if (!prev)
data->first_item = new_item;
obs_data_item_release(&prev);
obs_data_item_release(&next);
} else if (default_data) {
obs_data_item_set_default_data(item, ptr, size, type);
} else if (autoselect_data) {
obs_data_item_set_autoselect_data(item, ptr, size, type);
} else {
obs_data_item_setdata(item, ptr, size, type);
}
}
static inline void set_item(struct obs_data *data, obs_data_item_t **item,
const char *name, const void *ptr, size_t size,
enum obs_data_type type)
{
obs_data_item_t *actual_item = NULL;
if (!data && !item)
return;
if (!item) {
actual_item = get_item(data, name);
item = &actual_item;
}
set_item_data(data, item, name, ptr, size, type, false, false);
}
static inline void set_item_def(struct obs_data *data, obs_data_item_t **item,
const char *name, const void *ptr, size_t size,
enum obs_data_type type)
{
obs_data_item_t *actual_item = NULL;
if (!data && !item)
return;
if (!item) {
actual_item = get_item(data, name);
item = &actual_item;
}
if (*item && (*item)->type != type)
return;
set_item_data(data, item, name, ptr, size, type, true, false);
}
static inline void set_item_auto(struct obs_data *data, obs_data_item_t **item,
const char *name, const void *ptr, size_t size,
enum obs_data_type type)
{
obs_data_item_t *actual_item = NULL;
if (!data && !item)
return;
if (!item) {
actual_item = get_item(data, name);
item = &actual_item;
}
set_item_data(data, item, name, ptr, size, type, false, true);
}
static void copy_obj(struct obs_data *data, const char *name,
struct obs_data *obj,
void (*callback)(obs_data_t *, const char *, obs_data_t *))
{
if (obj) {
obs_data_t *new_obj = obs_data_create();
obs_data_apply(new_obj, obj);
callback(data, name, new_obj);
obs_data_release(new_obj);
}
}
static void copy_array(struct obs_data *data, const char *name,
struct obs_data_array *array,
void (*callback)(obs_data_t *, const char *,
obs_data_array_t *))
{
if (array) {