aboutsummaryrefslogtreecommitdiff
path: root/kernel/features/event.c
blob: d79d5313682a8787e6fef11f41bf33c14e274fa6 (plain) (blame)
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
// MIT License, Copyright (c) 2020 Marvin Borner

#include <assert.h>
#include <def.h>
#include <event.h>
#include <list.h>
#include <mem.h>
#include <proc.h>
#include <sys.h>

struct list *event_table[] = { [EVENT_KEYBOARD] = NULL, [EVENT_MOUSE] = NULL };

u32 event_register(u32 id, struct proc *proc)
{
	assert(id < sizeof(event_table) / sizeof(*event_table));

	if (event_table[id] == NULL)
		event_table[id] = (struct list *)list_new();

	struct event_descriptor *desc = malloc(sizeof(*desc));
	desc->id = id;
	desc->proc = proc;

	list_add(event_table[id], (void *)desc);
	return 0;
}

void event_unregister(u32 id, struct proc *proc)
{
	assert(id < sizeof(event_table) / sizeof(*event_table));

	struct event_descriptor desc;
	desc.id = id;
	desc.proc = proc;

	struct node *iterator = event_table[id]->head;
	while (iterator != NULL) {
		struct event_descriptor *desc_comp = iterator->data;
		if (desc_comp->id == desc.id && desc_comp->proc == desc.proc)
			list_remove(event_table[id], iterator);
		iterator = iterator->next;
	}
}

u32 event_trigger(u32 id, void *data)
{
	assert(id < sizeof(event_table) / sizeof(*event_table));

	if (!event_table[id] || !event_table[id]->head) {
		printf("Event %d not mapped!\n", id);
		return 1;
	}

	struct node *iterator = event_table[id]->head;
	while (iterator != NULL) {
		proc_send(kernel_proc, ((struct event_descriptor *)iterator->data)->proc, id, data);
		iterator = iterator->next;
	}

	return 0;
}