File: rig.c

package info (click to toggle)
xwax 0.9-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 420 kB
  • sloc: ansic: 4,215; makefile: 263; sh: 124
file content (201 lines) | stat: -rw-r--r-- 4,799 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
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
/*
 * Copyright (C) 2011 Mark Hills <mark@pogo.org.uk>
 *
 * 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 <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/poll.h>

#include "rig.h"
#include "track.h"

#define MAX_POLLFDS 3

#define EVENT_WAKE 0
#define EVENT_QUIT 1

int rig_init(struct rig_t *rig)
{
    /* Create a pipe which will be used to wake us from other threads */

    if (pipe(rig->event) == -1) {
        perror("pipe");
        return -1;
    }

    if (fcntl(rig->event[0], F_SETFL, O_NONBLOCK) == -1) {
        perror("fcntl");
        if (close(rig->event[1]) == -1)
            abort();
        if (close(rig->event[0]) == -1)
            abort();
        return -1;
    }

    rig->ntrack = 0;

    return 0;
}

void rig_clear(struct rig_t *rig)
{
    if (close(rig->event[0]) == -1)
        abort();
    if (close(rig->event[1]) == -1)
        abort();
}

/*
 * Add a track to be monitored
 *
 * This function is not thread or signal safe to rig_main() as
 * is only used as part of initialisation.
 *
 * Pre: less than MAX_TRACKS tracks already added to this rig
 */

void rig_add_track(struct rig_t *rig, struct track_t *track)
{
    assert(rig->ntrack < MAX_TRACKS);

    rig->track[rig->ntrack] = track;
    rig->ntrack++;
}

/*
 * Main thread which handles input and output
 *
 * The rig is the main thread of execution. It is responsible for all
 * non-priority event driven operations (eg. everything but audio).
 *
 * The SDL interface requires its own event loop, and so whilst the
 * rig is technically responsible for managing it, it does very little
 * on its behalf. In future if there are other interfaces or
 * controllers (which expected to use more traditional file-descriptor
 * I/O), the rig will also be responsible for them.
 */

int rig_main(struct rig_t *rig)
{
    int r;
    size_t n;
    struct pollfd pt[MAX_POLLFDS], *pe;

    assert(rig->ntrack <= MAX_POLLFDS);

    for (;;) { /* exit via EVENT_QUIT */
        pe = pt;

        /* ppoll() is not widely available, so use a pipe between this
         * thread and the outside. A single byte wakes up poll() to
         * inform us that new file descriptors need to be polled */

        pe->fd = rig->event[0];
        pe->revents = 0;
        pe->events = POLLIN;
        pe++;

        /* Fetch file descriptors to monitor from each track */

        for (n = 0; n < rig->ntrack; n++)
            pe += track_pollfd(rig->track[n], pe);

        r = poll(pt, pe - pt, -1);
        if (r == -1) {
            if (errno == EINTR) {
                continue;
            } else {
                perror("poll");
                return -1;
            }
        }

        /* Process all events on the event pipe */

        if (pt[0].revents != 0) {
            for (;;) {
                char event;
                size_t z;

                z = read(rig->event[0], &event, 1);
                if (z == -1) {
                    if (errno == EAGAIN) {
                        break;
                    } else {
                        perror("read");
                        return -1;
                    }
                }

                switch (event) {
                case EVENT_WAKE:
                    break;

                case EVENT_QUIT:
                    goto finish;

                default:
                    abort();
                }
            }
        }

        /* Do any reading and writing on all tracks */

        for (n = 0; n < rig->ntrack; n++)
            track_handle(rig->track[n]);
    }
 finish:

    return 0;
}

/*
 * Post a simple event into the rig event loop
 */

static int post_event(struct rig_t *rig, char event)
{
    if (write(rig->event[1], &event, 1) == -1) {
        perror("write");
        return -1;
    }
    return 0;
}

/*
 * Wake up the rig to inform it that the poll table has changed
 */

int rig_awaken(struct rig_t *rig)
{
    return post_event(rig, EVENT_WAKE);
}

/*
 * Ask the rig to exit from another thread or signal handler
 */

int rig_quit(struct rig_t *rig)
{
    return post_event(rig, EVENT_QUIT);
}