~schnouki/pustule

ref: 2e90522e70214bb6d1bb88aef6f7d8c06152a99f pustule/events.c -rw-r--r-- 4.6 KiB
2e90522e — Thomas Jost Expose log functions in Lua 10 months ago
                                                                                
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
/**
 * pustule
 * Copyright 2014-2020 Thomas Jost <schnouki@schnouki.net>
 *
 * This file is part of pustule.
 *
 * pustule 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 3 of the License, or (at your option) any later
 * version.
 *
 * pustule 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
 * pustule. If not, see <http://www.gnu.org/licenses/>.
 */

#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#include "log.h"
#include "pustule.h"


/* Call Lua callbacks */
static void handle_input_added(pustule_input_event_t* iev) {
    log_debug("Handling input_added event with %d listeners", input_added_listeners.size);
    if (input_added_listeners.size == 0)
        return;

    for (size_t cb_idx = 0; cb_idx < input_added_listeners.size; cb_idx++) {
        lua_Integer cb_ref = input_added_listeners.array[cb_idx];

        lua_rawgeti(g_L, LUA_REGISTRYINDEX, cb_ref);  // Push callback to the stack
        lua_pushinteger(g_L, iev->index);  // Push 1st argument (index) to the stack
        pustule_push_input_event(g_L, iev);  // Push 2nd argument (event table) to the stack
        int res = lua_pcall(g_L, 2, 0, 0);  // Call the function with 2 arguments and no result
        check_pcall_error(g_L, res);
    }
}
static void handle_input_removed(pustule_input_event_t* iev) {
    log_debug("Handling input_removed event with %d listeners", input_removed_listeners.size);
    if (input_removed_listeners.size == 0)
        return;

    for (size_t cb_idx = 0; cb_idx < input_removed_listeners.size; cb_idx++) {
        lua_Integer cb_ref = input_removed_listeners.array[cb_idx];

        lua_rawgeti(g_L, LUA_REGISTRYINDEX, cb_ref);  // Push callback to the stack
        lua_pushinteger(g_L, iev->index);  // Push 1st argument (index) to the stack
        int res = lua_pcall(g_L, 1, 0, 0);  // Call the function with 1 argument and no result
        check_pcall_error(g_L, res);
    }
}

static void handle_device_added(pustule_device_event_t* dev) {
    log_debug("Handling device_added event with %d listeners", device_added_listeners.size);
    if (device_added_listeners.size == 0)
        return;

    for (size_t cb_idx = 0; cb_idx < device_added_listeners.size; cb_idx++) {
        lua_Integer cb_ref = device_added_listeners.array[cb_idx];

        lua_rawgeti(g_L, LUA_REGISTRYINDEX, cb_ref);  // Push callback to the stack
        lua_pushinteger(g_L, dev->index);  // Push 1st argument (index) to the stack
        pustule_push_device_event(g_L, dev);  // Push 2nd argument (event table) to the stack
        int res = lua_pcall(g_L, 2, 0, 0);  // Call the function with 2 arguments and no result
        check_pcall_error(g_L, res);
    }
}
static void handle_device_removed(pustule_device_event_t* dev) {
    log_debug("Handling device_removed event with %d listeners", device_removed_listeners.size);
    if (device_removed_listeners.size == 0)
        return;

    for (size_t cb_idx = 0; cb_idx < device_removed_listeners.size; cb_idx++) {
        lua_Integer cb_ref = device_removed_listeners.array[cb_idx];

        lua_rawgeti(g_L, LUA_REGISTRYINDEX, cb_ref);  // Push callback to the stack
        lua_pushinteger(g_L, dev->index);  // Push 1st argument (index) to the stack
        int res = lua_pcall(g_L, 1, 0, 0);  // Call the function with 1 argument and no result
        check_pcall_error(g_L, res);
    }
}


/* Handle events from PulseAudio */
void pustule_handle_event(pustule_event_t* event) {
    pustule_input_event_t* iev = &event->input_event;
    pustule_device_event_t* dev = &event->device_event;

    // Input events
    if (event->type == INPUT_ADDED) {
        handle_input_added(iev);
        if (iev->proplist)
            pa_proplist_free(iev->proplist);
        free(iev->name);
        free(iev->driver);
    }
    else if (event->type == INPUT_REMOVED) {
        handle_input_removed(iev);
    }

    // Device events
    else if (event->type == DEVICE_ADDED) {
        handle_device_added(dev);
        if (dev->proplist)
            pa_proplist_free(dev->proplist);
        free(dev->name);
        free(dev->description);
    }
    else if (event->type == DEVICE_REMOVED) {
        handle_device_removed(dev);
    }

    free(event);
}