File: controller.c

package info (click to toggle)
xwax 1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 604 kB
  • sloc: ansic: 6,907; sh: 170; makefile: 136
file content (89 lines) | stat: -rw-r--r-- 2,293 bytes parent folder | download
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
/*
 * Copyright (C) 2018 Mark Hills <mark@xwax.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * 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 version 2 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this program; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 */

#include <assert.h>

#include "controller.h"
#include "deck.h"
#include "debug.h"

#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))

int controller_init(struct controller *c, struct controller_ops *ops,
                    void *local, struct rt *rt)
{
    debug("%p", c);

    c->fault = false;
    c->ops = ops;
    c->local = local;

    return rt_add_controller(rt, c);
}

void controller_clear(struct controller *c)
{
    debug("%p", c);
    c->ops->clear(c);
}

/*
 * Add a deck to this controller, if possible
 */

void controller_add_deck(struct controller *c, struct deck *d)
{
    debug("%p adding deck %p", c, d);

    if (c->ops->add_deck(c, d) == 0) {
        debug("deck was added");

        assert(d->ncontrol < ARRAY_SIZE(d->control)); /* FIXME: report error */
        d->control[d->ncontrol++] = c; /* for callbacks */
    }
}

/*
 * Get file descriptors which should be polled for this controller
 *
 * Important on systems where only callback-based audio devices
 * (eg. JACK) are used. We need to return some descriptors so
 * that the realtime thread runs.
 *
 * Return: the number of pollfd filled, or -1 on error
 */

ssize_t controller_pollfds(struct controller *c, struct pollfd *pe, size_t z)
{
    if (c->ops->pollfds != NULL)
        return c->ops->pollfds(c, pe, z);
    else
        return 0;
}

void controller_handle(struct controller *c)
{
    if (c->fault)
        return;

    if (c->ops->realtime(c) != 0) {
        c->fault = true;
        fputs("Error handling hardware controller; disabling it\n", stderr);
    }
}