pipewire/spa/lib/mapper.c

96 lines
2.1 KiB
C
Raw Normal View History

/* Simple Plugin API
* Copyright (C) 2016 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 <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
2017-03-24 11:40:58 +01:00
#include <spa/type-map.h>
#include <lib/debug.h>
2017-03-24 13:11:57 +01:00
#define MAX_TYPES 4096
typedef struct {
2017-03-24 11:40:58 +01:00
SpaTypeMap map;
2017-03-24 13:11:57 +01:00
char *types[MAX_TYPES];
2017-03-24 11:40:58 +01:00
unsigned int n_types;
} TypeMap;
static uint32_t
2017-03-24 11:40:58 +01:00
type_map_get_id (SpaTypeMap *map, const char *type)
{
2017-03-24 11:40:58 +01:00
TypeMap *this = SPA_CONTAINER_OF (map, TypeMap, map);
unsigned int i = 0;
2017-03-24 11:40:58 +01:00
if (type != NULL) {
for (i = 1; i <= this->n_types; i++) {
if (strcmp (this->types[i], type) == 0)
return i;
}
2017-03-24 11:40:58 +01:00
this->types[i] = (char *)type;
this->n_types++;
}
return i;
}
static const char *
2017-03-24 11:40:58 +01:00
type_map_get_type (SpaTypeMap *map, uint32_t id)
{
2017-03-24 11:40:58 +01:00
TypeMap *this = SPA_CONTAINER_OF (map, TypeMap, map);
2017-03-24 11:40:58 +01:00
if (id <= this->n_types)
return this->types[id];
2017-03-21 13:36:22 +01:00
return NULL;
}
2017-03-24 11:40:58 +01:00
static size_t
type_map_get_size (SpaTypeMap *map)
{
TypeMap *this = SPA_CONTAINER_OF (map, TypeMap, map);
return this->n_types;
}
static TypeMap default_type_map = {
{ sizeof (SpaTypeMap),
NULL,
2017-03-24 11:40:58 +01:00
type_map_get_id,
type_map_get_type,
type_map_get_size,
},
{ NULL, },
0
};
2017-03-24 11:40:58 +01:00
static SpaTypeMap *default_map = &default_type_map.map;
2017-03-24 11:40:58 +01:00
SpaTypeMap *
spa_type_map_get_default (void)
{
return default_map;
}
void
2017-03-24 11:40:58 +01:00
spa_type_map_set_default (SpaTypeMap *map)
{
default_map = map;
}