summaryrefslogtreecommitdiffstats
path: root/lib/tty/tty-internal.c
blob: 1e42e0f9f22d7e70966faebb38731bf1ea6a97a6 (plain)
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
/*
   Internal stuff of the terminal controlling library.

   Copyright (C) 2019-2022
   Free Software Foundation, Inc.

   Written by:
   Andrew Borodin <aborodin@vmail.ru>, 2019.

   This file is part of the Midnight Commander.

   The Midnight Commander is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   The Midnight Commander is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file
 *  \brief Source: internal stuff of the terminal controlling library.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#include "lib/global.h"

#include <glib-unix.h>

#include "tty-internal.h"

/*** global variables ****************************************************************************/

/* pipe to handle SIGWINCH */
int sigwinch_pipe[2];

/*** file scope macro definitions ****************************************************************/

/*** global variables ****************************************************************************/

/*** file scope type declarations ****************************************************************/

/*** file scope variables ************************************************************************/

/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */

/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */

void
tty_create_winch_pipe (void)
{
    GError *mcerror = NULL;

    if (!g_unix_open_pipe (sigwinch_pipe, FD_CLOEXEC, &mcerror))
    {
        fprintf (stderr, _("\nCannot create pipe for SIGWINCH: %s (%d)\n"),
                 mcerror->message, mcerror->code);
        g_error_free (mcerror);
        exit (EXIT_FAILURE);
    }

    /* If we read from an empty pipe, then read(2) will block until data is available.
     * If we write to a full pipe, then write(2) blocks until sufficient data has been read
     * from the pipe to allow the write to complete..
     * Therefore, use nonblocking I/O.
     */
    if (!g_unix_set_fd_nonblocking (sigwinch_pipe[0], TRUE, &mcerror))
    {
        fprintf (stderr, _("\nCannot configure write end of SIGWINCH pipe: %s (%d)\n"),
                 mcerror->message, mcerror->code);
        g_error_free (mcerror);
        tty_destroy_winch_pipe ();
        exit (EXIT_FAILURE);
    }

    if (!g_unix_set_fd_nonblocking (sigwinch_pipe[1], TRUE, &mcerror))
    {
        fprintf (stderr, _("\nCannot configure read end of SIGWINCH pipe: %s (%d)\n"),
                 mcerror->message, mcerror->code);
        g_error_free (mcerror);
        tty_destroy_winch_pipe ();
        exit (EXIT_FAILURE);
    }
}

/* --------------------------------------------------------------------------------------------- */

void
tty_destroy_winch_pipe (void)
{
    (void) close (sigwinch_pipe[0]);
    (void) close (sigwinch_pipe[1]);
}

/* --------------------------------------------------------------------------------------------- */