tty-internal.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Internal stuff of the terminal controlling library.
  3. Copyright (C) 2019-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2019.
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /** \file
  20. * \brief Source: internal stuff of the terminal controlling library.
  21. */
  22. #include <config.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include "lib/global.h"
  28. #include <glib-unix.h>
  29. #include "tty-internal.h"
  30. /*** global variables ****************************************************************************/
  31. /* pipe to handle SIGWINCH */
  32. int sigwinch_pipe[2];
  33. /*** file scope macro definitions ****************************************************************/
  34. /*** global variables ****************************************************************************/
  35. /*** file scope type declarations ****************************************************************/
  36. /*** file scope variables ************************************************************************/
  37. /* --------------------------------------------------------------------------------------------- */
  38. /*** file scope functions ************************************************************************/
  39. /* --------------------------------------------------------------------------------------------- */
  40. /* --------------------------------------------------------------------------------------------- */
  41. /*** public functions ****************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. void
  44. tty_create_winch_pipe (void)
  45. {
  46. GError *mcerror = NULL;
  47. if (!g_unix_open_pipe (sigwinch_pipe, FD_CLOEXEC, &mcerror))
  48. {
  49. fprintf (stderr, _("\nCannot create pipe for SIGWINCH: %s (%d)\n"),
  50. mcerror->message, mcerror->code);
  51. g_error_free (mcerror);
  52. exit (EXIT_FAILURE);
  53. }
  54. /* If we read from an empty pipe, then read(2) will block until data is available.
  55. * If we write to a full pipe, then write(2) blocks until sufficient data has been read
  56. * from the pipe to allow the write to complete..
  57. * Therefore, use nonblocking I/O.
  58. */
  59. if (!g_unix_set_fd_nonblocking (sigwinch_pipe[0], TRUE, &mcerror))
  60. {
  61. fprintf (stderr, _("\nCannot configure write end of SIGWINCH pipe: %s (%d)\n"),
  62. mcerror->message, mcerror->code);
  63. g_error_free (mcerror);
  64. tty_destroy_winch_pipe ();
  65. exit (EXIT_FAILURE);
  66. }
  67. if (!g_unix_set_fd_nonblocking (sigwinch_pipe[1], TRUE, &mcerror))
  68. {
  69. fprintf (stderr, _("\nCannot configure read end of SIGWINCH pipe: %s (%d)\n"),
  70. mcerror->message, mcerror->code);
  71. g_error_free (mcerror);
  72. tty_destroy_winch_pipe ();
  73. exit (EXIT_FAILURE);
  74. }
  75. }
  76. /* --------------------------------------------------------------------------------------------- */
  77. void
  78. tty_destroy_winch_pipe (void)
  79. {
  80. (void) close (sigwinch_pipe[0]);
  81. (void) close (sigwinch_pipe[1]);
  82. }
  83. /* --------------------------------------------------------------------------------------------- */