2015-02-21 00:04:17 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Christian Meffert <christian.meffert@googlemail.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "listener.h"
|
|
|
|
|
|
|
|
struct listener
|
|
|
|
{
|
|
|
|
notify notify_cb;
|
2015-05-18 14:12:18 -04:00
|
|
|
short events;
|
2015-02-21 00:04:17 -05:00
|
|
|
struct listener *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct listener *listener_list = NULL;
|
|
|
|
|
|
|
|
int
|
2015-05-18 14:12:18 -04:00
|
|
|
listener_add(notify notify_cb, short events)
|
2015-02-21 00:04:17 -05:00
|
|
|
{
|
|
|
|
struct listener *listener;
|
|
|
|
|
|
|
|
listener = (struct listener*)malloc(sizeof(struct listener));
|
2015-05-21 01:57:18 -04:00
|
|
|
if (!listener)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2015-02-21 00:04:17 -05:00
|
|
|
listener->notify_cb = notify_cb;
|
2015-05-18 14:12:18 -04:00
|
|
|
listener->events = events;
|
2015-02-21 00:04:17 -05:00
|
|
|
listener->next = listener_list;
|
|
|
|
listener_list = listener;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-03 02:18:26 -04:00
|
|
|
int
|
|
|
|
listener_remove(notify notify_cb)
|
|
|
|
{
|
|
|
|
struct listener *listener;
|
|
|
|
struct listener *prev;
|
|
|
|
|
|
|
|
prev = NULL;
|
2015-05-13 05:37:09 -04:00
|
|
|
for (listener = listener_list; listener; listener = listener->next)
|
2015-05-03 02:18:26 -04:00
|
|
|
{
|
|
|
|
if (listener->notify_cb == notify_cb)
|
2015-05-13 05:37:09 -04:00
|
|
|
break;
|
2015-05-03 02:18:26 -04:00
|
|
|
|
|
|
|
prev = listener;
|
|
|
|
}
|
|
|
|
|
2015-05-13 05:37:09 -04:00
|
|
|
if (!listener)
|
2015-05-21 01:57:18 -04:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2015-05-13 05:37:09 -04:00
|
|
|
|
|
|
|
if (prev)
|
|
|
|
prev->next = listener->next;
|
|
|
|
else
|
2015-12-16 17:16:58 -05:00
|
|
|
listener_list = listener->next;
|
2015-05-13 05:37:09 -04:00
|
|
|
|
|
|
|
free(listener);
|
|
|
|
return 0;
|
2015-05-03 02:18:26 -04:00
|
|
|
}
|
|
|
|
|
2015-05-21 01:57:18 -04:00
|
|
|
void
|
2015-02-21 00:04:17 -05:00
|
|
|
listener_notify(enum listener_event_type type)
|
|
|
|
{
|
|
|
|
struct listener *listener;
|
|
|
|
|
|
|
|
listener = listener_list;
|
|
|
|
while (listener)
|
|
|
|
{
|
2015-05-18 14:12:18 -04:00
|
|
|
if (type & listener->events)
|
|
|
|
listener->notify_cb(type);
|
2015-02-21 00:04:17 -05:00
|
|
|
listener = listener->next;
|
|
|
|
}
|
|
|
|
}
|