2018-08-13 17:17:23 +02:00
|
|
|
/* Simple Plugin API
|
|
|
|
|
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library 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
|
|
|
|
|
* Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <spa/param/props.h>
|
|
|
|
|
#include <spa/pod/iter.h>
|
|
|
|
|
#include <spa/pod/builder.h>
|
|
|
|
|
|
2018-08-24 10:53:09 +02:00
|
|
|
static inline int spa_pod_compare_value(uint32_t type, const void *r1, const void *r2)
|
2018-08-13 17:17:23 +02:00
|
|
|
{
|
|
|
|
|
switch (type) {
|
2018-08-27 15:03:11 +02:00
|
|
|
case SPA_TYPE_None:
|
2018-08-13 17:17:23 +02:00
|
|
|
return 0;
|
2018-08-27 15:03:11 +02:00
|
|
|
case SPA_TYPE_Bool:
|
2018-09-05 16:41:07 +02:00
|
|
|
case SPA_TYPE_Id:
|
2018-08-13 17:17:23 +02:00
|
|
|
return *(int32_t *) r1 == *(uint32_t *) r2 ? 0 : 1;
|
2018-08-27 15:03:11 +02:00
|
|
|
case SPA_TYPE_Int:
|
2018-08-13 17:17:23 +02:00
|
|
|
return *(int32_t *) r1 - *(int32_t *) r2;
|
2018-08-27 15:03:11 +02:00
|
|
|
case SPA_TYPE_Long:
|
2018-08-13 17:17:23 +02:00
|
|
|
return *(int64_t *) r1 - *(int64_t *) r2;
|
2018-08-27 15:03:11 +02:00
|
|
|
case SPA_TYPE_Float:
|
2018-08-13 17:17:23 +02:00
|
|
|
return *(float *) r1 - *(float *) r2;
|
2018-08-27 15:03:11 +02:00
|
|
|
case SPA_TYPE_Double:
|
2018-08-13 17:17:23 +02:00
|
|
|
return *(double *) r1 - *(double *) r2;
|
2018-08-27 15:03:11 +02:00
|
|
|
case SPA_TYPE_String:
|
2018-08-13 17:17:23 +02:00
|
|
|
return strcmp(r1, r2);
|
2018-08-27 15:03:11 +02:00
|
|
|
case SPA_TYPE_Rectangle:
|
2018-08-13 17:17:23 +02:00
|
|
|
{
|
|
|
|
|
const struct spa_rectangle *rec1 = (struct spa_rectangle *) r1,
|
|
|
|
|
*rec2 = (struct spa_rectangle *) r2;
|
|
|
|
|
if (rec1->width == rec2->width && rec1->height == rec2->height)
|
|
|
|
|
return 0;
|
|
|
|
|
else if (rec1->width < rec2->width || rec1->height < rec2->height)
|
|
|
|
|
return -1;
|
|
|
|
|
else
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2018-08-27 15:03:11 +02:00
|
|
|
case SPA_TYPE_Fraction:
|
2018-08-13 17:17:23 +02:00
|
|
|
{
|
|
|
|
|
const struct spa_fraction *f1 = (struct spa_fraction *) r1,
|
|
|
|
|
*f2 = (struct spa_fraction *) r2;
|
|
|
|
|
int64_t n1, n2;
|
|
|
|
|
n1 = ((int64_t) f1->num) * f2->denom;
|
|
|
|
|
n2 = ((int64_t) f2->num) * f1->denom;
|
|
|
|
|
if (n1 < n2)
|
|
|
|
|
return -1;
|
|
|
|
|
else if (n1 > n2)
|
|
|
|
|
return 1;
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int spa_pod_compare_part(const struct spa_pod *pod1, uint32_t pod1_size,
|
|
|
|
|
const struct spa_pod *pod2, uint32_t pod2_size)
|
|
|
|
|
{
|
|
|
|
|
const struct spa_pod *p1, *p2;
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
p2 = pod2;
|
|
|
|
|
|
|
|
|
|
SPA_POD_FOREACH(pod1, pod1_size, p1) {
|
|
|
|
|
bool do_advance = true;
|
|
|
|
|
uint32_t recurse_offset = 0;
|
|
|
|
|
|
|
|
|
|
if (p2 == NULL)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
switch (SPA_POD_TYPE(p1)) {
|
2018-08-27 15:03:11 +02:00
|
|
|
case SPA_TYPE_Struct:
|
|
|
|
|
case SPA_TYPE_Object:
|
2018-08-13 17:17:23 +02:00
|
|
|
if (SPA_POD_TYPE(p2) != SPA_POD_TYPE(p1))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
2018-08-27 15:03:11 +02:00
|
|
|
if (SPA_POD_TYPE(p1) == SPA_TYPE_Struct)
|
2018-08-13 17:17:23 +02:00
|
|
|
recurse_offset = sizeof(struct spa_pod_struct);
|
|
|
|
|
else
|
|
|
|
|
recurse_offset = sizeof(struct spa_pod_object);
|
|
|
|
|
|
|
|
|
|
do_advance = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
if (SPA_POD_TYPE(p1) != SPA_POD_TYPE(p2))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
res = spa_pod_compare_value(SPA_POD_TYPE(p1), SPA_POD_BODY(p1), SPA_POD_BODY(p2));
|
|
|
|
|
do_advance = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (recurse_offset) {
|
|
|
|
|
res = spa_pod_compare_part(SPA_MEMBER(p1,recurse_offset,void),
|
|
|
|
|
SPA_POD_SIZE(p1) - recurse_offset,
|
|
|
|
|
SPA_MEMBER(p2,recurse_offset,void),
|
|
|
|
|
SPA_POD_SIZE(p2) - recurse_offset);
|
|
|
|
|
}
|
|
|
|
|
if (do_advance) {
|
|
|
|
|
p2 = spa_pod_next(p2);
|
|
|
|
|
if (!spa_pod_is_inside(pod2, pod2_size, p2))
|
|
|
|
|
p2 = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (res != 0)
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
if (p2 != NULL)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int spa_pod_compare(const struct spa_pod *pod1,
|
|
|
|
|
const struct spa_pod *pod2)
|
|
|
|
|
{
|
|
|
|
|
spa_return_val_if_fail(pod1 != NULL, -EINVAL);
|
|
|
|
|
spa_return_val_if_fail(pod2 != NULL, -EINVAL);
|
|
|
|
|
|
|
|
|
|
return spa_pod_compare_part(pod1, SPA_POD_SIZE(pod1), pod2, SPA_POD_SIZE(pod2));
|
|
|
|
|
}
|