~nloomans/ft_select

ref: 2e75cb9f27ad9eb723b601f132a4e1449d02a31f ft_select/src/handle.c -rw-r--r-- 3.5 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
103
104
105
106
107
108
109
110
111
112
113
114
/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   ft_select                                          :+:    :+:            */
/*                                                     +:+                    */
/*   Author: Noah Loomans                             +#+                     */
/*           <nloomans@student.codam.nl>             +#+                      */
/*                                                  #+#    #+#                */
/*   License: GPLv3                                ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <stdbool.h>
#include <ft_printf.h>
#include "terminal.h"
#include "handle.h"
#include "read.h"

bool								g_handle_sigwinch = false;
bool								g_handle_sigcont = false;

static void							handle_suspend(int signum)
{
	struct termios attr;

	(void)signum;
	terminal_configure(TERMINAL_CONFIGURE_RESTORE);
	signal(SIGTSTP, SIG_DFL);
	tcgetattr(STDIN_FILENO, &attr);
	ioctl(STDIN_FILENO, TIOCSTI, (char[]){attr.c_cc[VSUSP], '\0'});
}

static void							handle_continue(int signum)
{
	terminal_configure(TERMINAL_CONFIGURE_SETUP);
	signal(SIGTSTP, handle_suspend);
	*g_handle_signal_map[signum].global = true;
}

static void							handle_exit_signal(int signum)
{
	terminal_configure(TERMINAL_CONFIGURE_RESTORE);
	ft_dprintf(STDERR_FILENO, "ft_select: received '%s' signal, exiting...\n",
		sys_siglist[signum]);
	exit(1);
}

const struct s_handle_signal_map	g_handle_signal_map[29] = {
	[SIGHUP]	= { .handler = handle_exit_signal },
	[SIGINT]	= { .handler = handle_exit_signal },
	[SIGQUIT]	= { .handler = handle_exit_signal },
	[SIGILL]	= { .handler = handle_exit_signal },
	[SIGTRAP]	= { .handler = handle_exit_signal },
	[SIGABRT]	= { .handler = handle_exit_signal },
	[SIGFPE]	= { .handler = handle_exit_signal },
	[SIGBUS]	= { .handler = handle_exit_signal },
	[SIGSEGV]	= { .handler = handle_exit_signal },
	[SIGSYS]	= { .handler = handle_exit_signal },
	[SIGPIPE]	= { .handler = handle_exit_signal },
	[SIGALRM]	= { .handler = handle_exit_signal },
	[SIGTERM]	= { .handler = handle_exit_signal },
	[SIGTSTP]	= { .handler = handle_suspend },
	[SIGCONT]	= {
		.handler = handle_continue,
		.global = &g_handle_sigcont,
		.action = action_update_size,
	},
	[SIGTTIN]	= { .handler = handle_exit_signal },
	[SIGTTOU]	= { .handler = handle_exit_signal },
	[SIGXCPU]	= { .handler = handle_exit_signal },
	[SIGXFSZ]	= { .handler = handle_exit_signal },
	[SIGVTALRM]	= { .handler = handle_exit_signal },
	[SIGPROF]	= { .handler = handle_exit_signal },
	[SIGWINCH]	= {
		.global = &g_handle_sigwinch,
		.action = action_update_size
	},

/*
** SIGEMT is not POSIX and doesn't exist on Linux
*/
#ifdef SIGEMT

	[SIGEMT]	= { .handler = handle_exit_signal },

#endif

};

const t_action						g_handle_key_map[READ_TYPE_AMOUNT][256] = {
	[READ_TYPE_REG] = {
		['q'] = action_quit,
		['j'] = action_down,
		['k'] = action_up,
		['h'] = action_left,
		['l'] = action_right,
		[' '] = action_select,
		['\x7f'] = action_delete,
		['\n'] = action_confirm,
	},
	[READ_TYPE_ESC] = {
		['A'] = action_up,
		['B'] = action_down,
		['D'] = action_left,
		['C'] = action_right,
	},
	[READ_TYPE_ESC_SQL] = {
		['3'] = action_delete,
	},
};