owntone-server/src/listener.c

94 lines
2.0 KiB
C
Raw Normal View History

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 "logger.h"
#include "listener.h"
struct listener
{
notify notify_cb;
short events;
2015-02-21 00:04:17 -05:00
struct listener *next;
};
struct listener *listener_list = NULL;
int
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));
listener->notify_cb = notify_cb;
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;
for (listener = listener_list; listener; listener = listener->next)
2015-05-03 02:18:26 -04:00
{
if (listener->notify_cb == notify_cb)
break;
2015-05-03 02:18:26 -04:00
prev = listener;
}
if (!listener)
return 0;
if (prev)
prev->next = listener->next;
else
listener_list = NULL;
free(listener);
return 0;
2015-05-03 02:18:26 -04:00
}
2015-02-21 00:04:17 -05:00
int
listener_notify(enum listener_event_type type)
{
struct listener *listener;
DPRINTF(E_DBG, L_MPD, "Notify event type %d\n", type);
listener = listener_list;
while (listener)
{
if (type & listener->events)
listener->notify_cb(type);
2015-02-21 00:04:17 -05:00
listener = listener->next;
}
return 0;
}