summaryrefslogtreecommitdiff
path: root/src/libaudcore/eventqueue.cc
blob: 64732d23df19acd4a70f4f2338a08e9b7cdfc5dc (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
/*
 * eventqueue.cc
 * Copyright 2011-2014 John Lindgren, Michał Lipski
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include "hook.h"

#include <string.h>

#include "internal.h"
#include "list.h"
#include "mainloop.h"
#include "objects.h"
#include "threads.h"

struct Event : public ListNode
{
    String name;
    void * data;
    void (*destroy)(void *);

    Event(const char * name, void * data, EventDestroyFunc destroy)
        : name(name), data(data), destroy(destroy)
    {
    }

    ~Event()
    {
        if (destroy)
            destroy(data);
    }
};

static aud::mutex mutex;
static bool paused;
static List<Event> events;
static QueuedFunc queued_events;

static void events_execute()
{
    auto mh = mutex.take();

    Event * event;
    while (!paused && (event = events.head()))
    {
        events.remove(event);

        mh.unlock();

        hook_call(event->name, event->data);
        delete event;

        mh.lock();
    }
}

EXPORT void event_queue(const char * name, void * data,
                        EventDestroyFunc destroy)
{
    auto mh = mutex.take();

    if (!paused && !events.head())
        queued_events.queue(events_execute);

    events.append(new Event(name, data, destroy));
}

EXPORT void event_queue_cancel(const char * name, void * data)
{
    auto mh = mutex.take();

    Event * event = events.head();
    while (event)
    {
        Event * next = events.next(event);

        if (!strcmp(event->name, name) && (!data || event->data == data))
        {
            events.remove(event);
            delete event;
        }

        event = next;
    }
}

// this is only for use by the playlist, to ensure that queued playlist
// updates are processed before generic events
void event_queue_pause()
{
    auto mh = mutex.take();
    if (!paused)
        queued_events.stop();

    paused = true;
}

void event_queue_unpause()
{
    auto mh = mutex.take();
    if (paused && events.head())
        queued_events.queue(events_execute);

    paused = false;
}

void event_queue_cancel_all()
{
    auto mh = mutex.take();
    events.clear();
}