summaryrefslogtreecommitdiff
path: root/events.c
blob: 4fb395cb01e47c6ec75b2bca87022da0cecd5a6a (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
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
/*
 * OMAP3 generic events handling
 *
 * Copyright (C) 2010-2012 Ideas on board SPRL
 *
 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * 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 for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#define _BSD_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/select.h>

#include "isp/list.h"
#include "isp/omap3isp.h"
#include "isp/tools.h"

#include "events.h"

#define SELECT_TIMEOUT		2000		/* in milliseconds */

struct event_fd {
	struct list_entry list;

	int fd;
	enum omap3_isp_event_type type;
	void (*callback)(void *priv);
	void *priv;
};

void events_watch_fd(struct events *events, int fd,
		     enum omap3_isp_event_type type,
		     void(*callback)(void *), void *priv)
{
	struct event_fd *event;

	event = malloc(sizeof *event);
	if (event == NULL)
		return;

	event->fd = fd;
	event->type = type;
	event->callback = callback;
	event->priv = priv;

	switch (event->type) {
	case OMAP3_ISP_EVENT_READ:
		FD_SET(fd, &events->rfds);
		break;
	case OMAP3_ISP_EVENT_WRITE:
		FD_SET(fd, &events->wfds);
		break;
	case OMAP3_ISP_EVENT_EXCEPTION:
		FD_SET(fd, &events->efds);
		break;
	}

	events->maxfd = max(events->maxfd, fd);

	list_append(&event->list, &events->events);
}

void events_unwatch_fd(struct events *events, int fd)
{
	struct event_fd *event = NULL;
	struct event_fd *entry;
	int maxfd = 0;

	list_for_each_entry(entry, &events->events, list) {
		if (entry->fd == fd)
			event = entry;
		else
			maxfd = max(maxfd, entry->fd);
	}

	if (event == NULL)
		return;

	switch (event->type) {
	case OMAP3_ISP_EVENT_READ:
		FD_CLR(fd, &events->rfds);
		break;
	case OMAP3_ISP_EVENT_WRITE:
		FD_CLR(fd, &events->wfds);
		break;
	case OMAP3_ISP_EVENT_EXCEPTION:
		FD_CLR(fd, &events->efds);
		break;
	}

	events->maxfd = maxfd;

	list_remove(&event->list);
	free(event);
}

static void events_dispatch(struct events *events, const fd_set *rfds,
			    const fd_set *wfds, const fd_set *efds)
{
	struct event_fd *event;
	struct event_fd *next;

	list_for_each_entry_safe(event, next, &events->events, list) {
		if (event->type == OMAP3_ISP_EVENT_READ &&
		    FD_ISSET(event->fd, rfds))
			event->callback(event->priv);
		else if (event->type == OMAP3_ISP_EVENT_WRITE &&
			 FD_ISSET(event->fd, wfds))
			event->callback(event->priv);
		else if (event->type == OMAP3_ISP_EVENT_EXCEPTION &&
			 FD_ISSET(event->fd, efds))
			event->callback(event->priv);

		/* If the callback stopped events processing, we're done. */
		if (events->done)
			break;
	}
}

bool events_loop(struct events *events)
{
	events->done = false;

	while (!events->done) {
		struct timeval timeout;
		fd_set rfds;
		fd_set wfds;
		fd_set efds;
		int ret;

		timeout.tv_sec = SELECT_TIMEOUT / 1000;
		timeout.tv_usec = (SELECT_TIMEOUT % 1000) * 1000;
		rfds = events->rfds;
		wfds = events->wfds;
		efds = events->efds;

		ret = select(events->maxfd + 1, &rfds, &wfds, &efds, &timeout);
		if (ret < 0) {
			/* EINTR means that a signal has been received, continue
			 * to the next iteration in that case.
			 */
			if (errno == EINTR)
				continue;

			printf("error: select failed with %d\n", errno);
			break;
		}
		if (ret == 0) {
			/* select() should never time out as the ISP is supposed
			 * to capture images continuously. A timeout is thus
			 * considered as a fatal error.
			 */
			printf("error: select timeout\n");
			break;
		}

		events_dispatch(events, &rfds, &wfds, &efds);
	}

	return !events->done;
}

void events_stop(struct events *events)
{
	events->done = true;
}

void events_init(struct events *events)
{
	memset(events, 0, sizeof *events);

	FD_ZERO(&events->rfds);
	FD_ZERO(&events->wfds);
	FD_ZERO(&events->efds);
	events->maxfd = 0;
	list_init(&events->events);
}