summaryrefslogtreecommitdiffstats
path: root/src/core.c
blob: 0bc13a5b9a8a77671fa6ce7f0e335b0517cca3ba (plain)
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
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

#include "core.h"
#include "module.h"
#include "sink.h"
#include "source.h"

struct core* core_new(struct mainloop *m) {
    struct core* c;
    c = malloc(sizeof(struct core));
    assert(c);

    c->mainloop = m;
    c->clients = idxset_new(NULL, NULL);
    c->sinks = idxset_new(NULL, NULL);
    c->sources = idxset_new(NULL, NULL);
    c->source_outputs = idxset_new(NULL, NULL);
    c->sink_inputs = idxset_new(NULL, NULL);

    c->default_source_index = c->default_sink_index = IDXSET_INVALID;

    c->modules = NULL;
    
    return c;
};

void core_free(struct core *c) {
    assert(c);

    module_unload_all(c);
    assert(!c->modules);
    
    assert(idxset_isempty(c->clients));
    idxset_free(c->clients, NULL, NULL);
    
    assert(idxset_isempty(c->sinks));
    idxset_free(c->sinks, NULL, NULL);

    assert(idxset_isempty(c->sources));
    idxset_free(c->sources, NULL, NULL);
    
    assert(idxset_isempty(c->source_outputs));
    idxset_free(c->source_outputs, NULL, NULL);
    
    assert(idxset_isempty(c->sink_inputs));
    idxset_free(c->sink_inputs, NULL, NULL);

    free(c);    
};

struct sink* core_get_default_sink(struct core *c) {
    struct sink *sink;
    assert(c);

    if ((sink = idxset_get_by_index(c->sinks, c->default_sink_index)))
        return sink;

    if (!(sink = idxset_first(c->sinks, &c->default_sink_index)))
        return NULL;

    fprintf(stderr, "core: default sink vanished, setting to %u.\n", sink->index);
    return sink;
}

struct source* core_get_default_source(struct core *c) {
    struct source *source;
    assert(c);

    if ((source = idxset_get_by_index(c->sources, c->default_source_index)))
        return source;

    if (!(source = idxset_first(c->sources, &c->default_source_index)))
        return NULL;

    fprintf(stderr, "core: default source vanished, setting to %u.\n", source->index);
    return source;
}