aboutsummaryrefslogtreecommitdiffstats
path: root/event.c
blob: bff2a968d51119af8e58958a41bef33bd0378605 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
# Sweden - Karlstad Sörmon Valid from 2007 09 26. Ver. 2 Correct FEC
# T freq bw fec_hi fec_lo mod transmission-mode guard-interval hierarchy
# Channels
# Mux3=30
T 546000000 8MHz 2/3 NONE QAM64 8k 1/8 NONE
# Mux4=42
T 642000000 8MHz 2/3 NONE QAM64 8k 1/8 NONE
# Mux1=43
T 650000000 8MHz 2/3 NONE QAM64 8k 1/8 NONE
href='#n202'>202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
/* $Id: event.c 1040 2009-09-23 04:14:17Z michael $
 * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/event.c $
 *
 * generic timer handling
 *
 * Copyright (C) 2009 Ed Martin <edman007@edman007.com>
 * Copyright (C) 2004, 2009 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/* 
 * exported functions:
 *
 * int event_add(void (*callback) (void *data), void *data, const int fd, const int read, const int write);
 *   Adds a file description to watch
 *
 * int event_del(const int fd);
 *   Remove an event
 *
 * int event_modify(const int fd, const int read, const int write, const int active);
 *   Modify an event
 *
 * int named_event_add(char *event, void (*callback) (void *data), void *data);
 *   Add an event identified by a string
 *
 * int named_event_del(char *event, void (*callback) (void *data), void *data);
 *   delete the event identified by this string/callback/data
 *
 * int named_event_trigger(char *event);	//call all calbacks for this event
 *   call the callbacks of all events that have identified as this string
 *
 * int event_process(const struct timespec *delay);
 *   process the event list
 *
 * void event_exit();
 *   releases all events
 *
 */


#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>



#include <poll.h>

#include "debug.h"
#include "cfg.h"
#include "event.h"

#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif

typedef struct {
    void (*callback) (event_flags_t flags, void *data);
    void *data;
    int fd;
    int read;
    int write;
    int active;
    int fds_id;
} event_t;


//our set of FDs
static event_t *events = NULL;
static int event_count = 0;
static void free_events(void);

int event_add(void (*callback) (event_flags_t flags, void *data), void *data, const int fd, const int read,
	      const int write, const int active)
{
    event_count++;
    events = realloc(events, sizeof(event_t) * event_count);

    int i = event_count - 1;
    events[i].callback = callback;
    events[i].data = data;
    events[i].fd = fd;
    events[i].read = read;
    events[i].write = write;
    events[i].active = active;
    events[i].fds_id = -1;
    return 0;
}


int event_process(const struct timespec *timeout)
{
    int i, j;
    struct pollfd *fds = malloc(sizeof(struct pollfd) * event_count);
    for (i = 0, j = 0; i < event_count; i++) {
	events[i].fds_id = -1;
	if (events[i].active) {
	    events[i].fds_id = j;
	    fds[j].events = 0;
	    fds[j].fd = events[i].fd;
	    if (events[i].read) {
		fds[j].events |= POLLIN;
	    }
	    if (events[i].write) {
		fds[j].events |= POLLOUT;
	    }
	    j++;
	}
    }
#if (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 4)
    int ready = ppoll(fds, j, timeout, NULL);
#else
    int ready = poll(fds, j, timeout->tv_sec * 1000 + timeout->tv_nsec / 1000000);
#endif

    if (ready > 0) {
	//search the file descriptors, call all relavant callbacks
	for (i = 0, j = 0; i < event_count; i++) {
	    if (events[i].fds_id != j) {
		continue;
	    }
	    if (fds[j].revents) {
		int flags = 0;
		if (fds[j].revents & POLLIN) {
		    flags |= EVENT_READ;
		}
		if (fds[j].revents & POLLOUT) {
		    flags |= EVENT_WRITE;
		}
		if (fds[j].revents & POLLHUP) {
		    flags |= EVENT_HUP;
		}
		if (fds[j].revents & POLLERR) {
		    flags |= EVENT_ERR;
		}
		events[i].callback(flags, events[i].data);

	    }
	    j++;
	}
    }
    free(fds);
    return 0;

}

int event_del(const int fd)
{
    int i;
    for (i = 0; i < event_count; i++) {
	if (events[i].fd == fd) {
	    events[i] = events[event_count - 1];
	    break;
	}
    }
    event_count--;
    events = realloc(events, sizeof(event_t) * event_count);
    return 0;
}

int event_modify(const int fd, const int read, const int write, const int active)
{
    int i;
    for (i = 0; i < event_count; i++) {
	if (events[i].fd == fd) {
	    events[i].read = read;
	    events[i].write = write;
	    events[i].active = active;
	    break;
	}
    }
    event_count--;
    events = realloc(events, sizeof(event_t) * event_count);
    return 0;
}

static void free_events(void)
{
    if (events != NULL) {
	free(events);
    }
    event_count = 0;
    events = NULL;

}

void event_exit(void)
{
    free_events();
}

/*
 * Named events are the user facing side of the event subsystem
 *
 */


//we rely on "=" working for copying these structs (no pointers except the callers)
typedef struct {
    void (*callback) (void *data);
    void *data;
} event_callback_t;

typedef struct {
    char *name;
    event_callback_t *c;
    int callback_count;
} named_event_list_t;


static named_event_list_t *ev_names = NULL;
static int ev_count = 0;

int named_event_add(char *event, void (*callback) (void *data), void *data)
{
    if (event == NULL || strlen(event) == 0) {
	return 1;
    }
    if (callback == NULL) {
	return 2;
    }
    int i;
    for (i = 0; i < ev_count; i++) {
	if (0 == strcmp(event, ev_names[i].name)) {
	    break;
	}
    }
    if (i >= ev_count) {
	//create the entry
	ev_count++;
	ev_names = realloc(ev_names, sizeof(named_event_list_t) * ev_count);
	ev_names[i].name = strdup(event);
	ev_names[i].callback_count = 0;
	ev_names[i].c = NULL;
    }
    int j = ev_names[i].callback_count;
    ev_names[i].callback_count++;
    ev_names[i].c = realloc(ev_names[i].c, sizeof(event_callback_t) * ev_names[i].callback_count);

    ev_names[i].c[j].callback = callback;
    ev_names[i].c[j].data = data;
    return 0;
}

int named_event_del(char *event, void (*callback) (void *data), void *data)
{
    int i, j;
    for (i = 0; i < ev_count; i++) {
	if (0 == strcmp(event, ev_names[i].name)) {
	    break;
	}
    }
    if (i >= ev_count) {
	return 1;		//nothing removed
    }
    for (j = 0; j < ev_names[i].callback_count; j++) {
	if (ev_names[i].c[j].callback == callback && ev_names[i].c[j].data == data) {
	    ev_names[i].callback_count--;
	    ev_names[i].c[j] = ev_names[i].c[ev_names[i].callback_count];
	    ev_names[i].c = realloc(ev_names[i].c, sizeof(event_callback_t) * ev_names[i].callback_count);
	    if (ev_names[i].callback_count == 0) {
		//drop this event
		free(ev_names[i].name);
		ev_count--;
		ev_names[i] = ev_names[ev_count];
		ev_names = realloc(ev_names, sizeof(named_event_list_t) * ev_count);
	    }
	    return 0;
	}
    }
    return 2;
}

int named_event_trigger(char *event)
{
    int i, j;
    for (i = 0; i < ev_count; i++) {
	if (0 == strcmp(event, ev_names[i].name)) {
	    for (j = 0; j < ev_names[i].callback_count; j++) {
		ev_names[i].c[j].callback(ev_names[i].c[j].data);
	    }
	    return 0;
	}
    }
    return 1;
}