aboutsummaryrefslogtreecommitdiff
path: root/kernel/features/event.c
blob: e0a7d9385ba06aceab351bb6f3aec90f878abe8d (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// 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_map(enum event id, struct proc *proc, u32 *func)
{
	assert(id < sizeof(event_table) / sizeof(*event_table));
	assert(func);

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

	struct node *iterator = event_table[id]->head;
	do {
		assert(((struct event_descriptor *)iterator->data)->func != func);
	} while ((iterator = iterator->next) != NULL);

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

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

void event_unmap(enum event id, struct proc *proc, u32 *func)
{
	assert(id < sizeof(event_table) / sizeof(*event_table));
	assert(func);

	struct list *list = event_table[id];

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

	struct node *iterator = list->head;
	do {
		if (memcmp(iterator->data, desc, sizeof(*desc)) == 0)
			list_remove(list, iterator);
	} while ((iterator = iterator->next) != NULL);
}

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

	struct node *iterator = event_table[id]->head;

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

	while (1) {
		struct proc_event *proc_event = malloc(sizeof(*proc_event));
		struct event_descriptor *desc = iterator->data;
		proc_event->desc = desc;
		proc_event->data = data;
		list_add(desc->proc->events, proc_event);
		iterator = iterator->next;
		if (iterator == NULL)
			break;
	}

	return 0;
}