/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   sl_signal.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: bchanot <bchanot@students.42.fr>           +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2016/11/02 21:53:38 by bchanot           #+#    #+#             */
/*   Updated: 2017/01/11 00:00:47 by bchanot          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "ft_select.h"
#include <signal.h>
#include <term.h>

static void	sl_init_screen(int sig)
{
	t_inf *inf;

	(void)sig;
	inf = sl_set_inf(NULL);
	sl_display_screen(inf);
}

static void	sl_abort(int sig)
{
	t_inf *inf;

	(void)sig;
	inf = sl_set_inf(NULL);
	ft_putstr_fd(tgetstr("cl", NULL), 0);
	ft_putstr_fd(tgetstr("te", NULL), 0);
	ft_putstr_fd(tgetstr("ve", NULL), 0);
	sl_exit(NULL, inf, 1);
}

static void	sl_refresh(int sig)
{
	t_inf	*inf;

	(void)sig;
	inf = sl_set_inf(NULL);
	sl_init_term(inf);
	sl_set_signal();
	sl_display_screen(inf);
}

void		sl_set_signal(void)
{
	signal(SIGWINCH, sl_init_screen);
	signal(SIGCONT, sl_refresh);
	signal(SIGINT, sl_abort);
	signal(SIGQUIT, sl_abort);
	signal(SIGABRT, sl_abort);
	signal(SIGKILL, sl_abort);
	signal(SIGTERM, sl_abort);
	signal(SIGSTOP, sl_abort);
}
