~nloomans/ft_select

ref: 2e75cb9f27ad9eb723b601f132a4e1449d02a31f ft_select/src/event.c -rw-r--r-- 2.7 KiB
2e75cb9fNoah Loomans fix event norm 1 year, 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
/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   ft_select                                          :+:    :+:            */
/*                                                     +:+                    */
/*   Author: Noah Loomans                             +#+                     */
/*           <nloomans@student.codam.nl>             +#+                      */
/*                                                  #+#    #+#                */
/*   License: GPLv3                                ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <ft_printf.h>
#include "event.h"
#include "terminal.h"
#include "action.h"
#include "read.h"
#include "handle.h"
#include "error.h"

static void						handle_generic(int signum)
{
	*g_handle_signal_map[signum].global = true;
}

t_error							event_init(void)
{
	size_t	signum;

	signum = 0;
	while (signum < sizeof(g_handle_signal_map) / sizeof(*g_handle_signal_map))
	{
		if (g_handle_signal_map[signum].handler)
		{
			if (signal(signum, g_handle_signal_map[signum].handler) == SIG_ERR)
			{
				return (errorf("unable to bind signal %d: %s",
					signum, strerror(errno)));
			}
		}
		else if (g_handle_signal_map[signum].action)
		{
			if (signal(signum, handle_generic) == SIG_ERR)
			{
				return (errorf("unable to bind signal %d: %s",
					signum, strerror(errno)));
			}
		}
		signum++;
	}
	return (ERROR_NULL);
}

static t_action					next_signal(void)
{
	size_t	signum;

	signum = 0;
	while (signum < sizeof(g_handle_signal_map) / sizeof(*g_handle_signal_map))
	{
		if (g_handle_signal_map[signum].action &&
			*g_handle_signal_map[signum].global)
		{
			*g_handle_signal_map[signum].global = false;
			return (g_handle_signal_map[signum].action);
		}
		signum++;
	}
	return (NULL);
}

static t_error					next_key(t_action *dest)
{
	struct s_read_seq	seq;

	seq = read_seq();
	if (is_error(seq.error))
		return (errorf("failed to read input sequence: %s", seq.error.msg));
	*dest = g_handle_key_map[seq.type][(unsigned char)seq.c];
	return (ERROR_NULL);
}

t_error							event_next(t_action *dest)
{
	t_error			error;

	*dest = next_signal();
	if (*dest != NULL)
		return (ERROR_NULL);
	error = next_key(dest);
	if (is_error(error) || *dest != NULL)
		return (error);
	return (ERROR_NULL);
}