home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / lib / gcc-lib / djgpp / 2.952 / units / crtlinux386.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-08  |  5.4 KB  |  193 lines

  1. /*
  2. This file implements some system-specific functions, as described in
  3. crtc.c, for a Linux/x86 system. Actually, they're probably not
  4. x86-specific, but I've had no chance to test them on another
  5. processor yet.
  6.  
  7. Copyright (C) 1998-2001 Free Software Foundation, Inc.
  8.  
  9. Author: Frank Heckenbach <frank@pascal.gnu.de>
  10.  
  11. This file is part of GNU Pascal.
  12.  
  13. GNU Pascal is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2, or (at your option)
  16. any later version.
  17.  
  18. GNU Pascal is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with GNU Pascal; see the file COPYING. If not, write to the
  25. Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  26. 02111-1307, USA.
  27.  
  28. As a special exception, if you link this file with files compiled
  29. with a GNU compiler to produce an executable, this does not cause
  30. the resulting executable to be covered by the GNU General Public
  31. License. This exception does not however invalidate any other
  32. reasons why the executable file might be covered by the GNU General
  33. Public License.
  34. */
  35.  
  36. #include <unistd.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <signal.h>
  41. #include <errno.h>
  42. #include <fcntl.h>
  43. #include <sys/wait.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include <sys/ioctl.h>
  47. #include <asm/ioctls.h>
  48. #include <linux/kd.h>
  49.  
  50. #define HAVE_CRT_GETSHIFTSTATE
  51. int crt_getshiftstate ()
  52. {
  53.   int state = crt_VirtualShiftState;
  54.   char arg = 6;
  55.   if (ioctl (crt_get_input_fd (), TIOCLINUX, &arg) == 0)
  56.     {
  57.       if (arg & (1 | 16)) state |= shLeftShift;
  58.       if (arg & (1 | 32)) state |= shRightShift;
  59.       if (arg & 4)        state |= shCtrl;
  60.       if (arg & 8)        state |= shLeftAlt;
  61.       if (arg & 2)        state |= shRightAlt;
  62.       if (arg & 64)       state |= shExtra;
  63.     }
  64.   return state;
  65. }
  66.  
  67. #define HAVE_CRT_SOUND
  68. void crt_sound (unsigned Hz)
  69. {
  70.   ioctl (crt_get_output_fd (), KIOCSOUND, (Hz <= 18) ? 0 : 1193181 / Hz);
  71. }
  72.  
  73. void crt_nosound ()
  74. {
  75.   ioctl (crt_get_output_fd (), KIOCSOUND, 0);
  76. }
  77.  
  78. #define DEV_TTY "/dev/tty"
  79. #define DEV_VCSA "/dev/vcsa"
  80.  
  81. #define HAVE_CRT_SAVE_RESTORE_SCREEN
  82. /* Return value:
  83.    -1 : not a local console
  84.     0 : no access to vcsa device
  85.     1 : ok */
  86. static int crt_save_restore_screen_internal (Boolean Restore)
  87. {
  88.   static int NoConsole = 0, VcsaFile = 0, BufSize = 0x1000, Count = 0;
  89.   static char *Buffer = 0;
  90.   ssize_t r, c = 0;
  91.   if (NoConsole) return - 1;
  92.   if (!VcsaFile)
  93.     {
  94.       char *tty, Vcsa [100];
  95.       tty = ttyname (crt_get_output_fd ());
  96.       if (tty && !strcmp (tty, DEV_TTY)) tty = ttyname (2);
  97.       if (!tty
  98.           || strlen (tty) > sizeof (Vcsa) - strlen (DEV_VCSA) - 1
  99.           || strncmp (tty, DEV_TTY, strlen (DEV_TTY))
  100.           || !*(tty += strlen (DEV_TTY)))
  101.         {
  102.           NoConsole = 1;
  103.           return - 1;
  104.         }
  105.       sprintf (Vcsa, DEV_VCSA "%s", tty);
  106.       while (*tty >= '0' && *tty <= '9') tty++;
  107.       if (*tty)
  108.         {
  109.           NoConsole = 1;
  110.           return - 1;
  111.         }
  112.       VcsaFile = open (Vcsa, O_RDWR);
  113.     }
  114.   if (VcsaFile < 0) return 0;
  115.   lseek (VcsaFile, 0, SEEK_SET);
  116.   if (!Restore)
  117.     do
  118.       if ((!Buffer || c >= BufSize) && (!(Buffer = realloc (Buffer, BufSize *= 2))))
  119.         return 0;
  120.       else if ((r = read (VcsaFile, Buffer + c, BufSize - c)) >= 0)
  121.         Count = c += r;
  122.       else if (errno != EINTR)
  123.         return 0;
  124.     while (r);
  125.   else if (!Count)
  126.     return 0;
  127.   else
  128.     do
  129.       if ((r = write (VcsaFile, Buffer + c, Count - c)) >= 0)
  130.         c += r;
  131.       else if (errno != EINTR)
  132.         return 0;
  133.     while (r && c < Count);
  134.   return 1;
  135. }
  136.  
  137. static int crt_save_restore_screen (Boolean Restore)
  138. {
  139.   static int po [2], pi [2];
  140.   static pid_t pid = 0;
  141.   struct sigaction action, old_action;
  142.   char Res = ' ';
  143.   int internal = crt_save_restore_screen_internal (Restore), a;
  144.   if (internal > 0)
  145.     return 1;
  146.   if (internal < 0)
  147.     /* Call crt_default_save_restore_screen, e.g. for xterms under Linux */
  148.     return crt_default_save_restore_screen (Restore);
  149.   if (!pid)
  150.     {
  151.       if (pipe (po) < 0 || pipe (pi) < 0 || (pid = fork ()) < 0)
  152.         pid = - 1;
  153.       else if (!pid)
  154.         {
  155.           signal (SIGINT, SIG_IGN);
  156.           signal (SIGQUIT, SIG_IGN);
  157.           signal (SIGTSTP, SIG_IGN);
  158.           dup2  (po [1], 1);
  159.           close (po [0]);
  160.           close (po [1]);
  161.           dup2  (pi [0], 0);
  162.           close (pi [0]);
  163.           close (pi [1]);
  164.           execlp ("crtscreen", "crtscreen", NULL);
  165.           _exit (127);
  166.         }
  167.       else
  168.         {
  169.           close (po [1]);
  170.           close (pi [0]);
  171.         }
  172.     }
  173.   if (pid < 0)
  174.     return 0;
  175.   /* Ignore SIGPIPE which might occur when the child has terminated
  176.      for lack of permissions before we can do the write(). Blocking
  177.      is not enough, as the signal would then be delivered after
  178.      unblocking. */
  179.   action.sa_handler = SIG_IGN;
  180.   sigemptyset (&action.sa_mask);
  181.   action.sa_flags = 0;
  182.   a = sigaction (SIGPIPE, &action, &old_action);
  183.   if (write (pi [1], Restore ? "R" : "S", 1) < 0 || read (po [0], &Res, 1) < 1 || Res == 'F')
  184.     {
  185.       close (pi [1]);
  186.       close (po [0]);
  187.       waitpid (pid, NULL, WNOHANG);
  188.       pid = - 1;
  189.     }
  190.   if (!a) sigaction (SIGPIPE, &old_action, NULL);
  191.   return Res == 'O';
  192. }
  193.