home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / device-tty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-04  |  8.7 KB  |  345 lines

  1. /* TTY device functions.
  2.    Copyright (C) 1994, 1995 Board of Trustees, University of Illinois
  3.    Copyright (C) 1994, 1995 Amdahl Corporation
  4.  
  5. This file is part of XEmacs.
  6.  
  7. XEmacs is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with XEmacs; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Synched up with: Not in FSF. */
  22.  
  23. /* Authors: Chuck Thompson and Ben Wing. */
  24.  
  25. #include <config.h>
  26. #include "lisp.h"
  27.  
  28. #include "device-tty.h"
  29. #include "device-stream.h"
  30. #include "events.h"
  31. #include "faces.h"
  32. #include "frame.h"
  33. #include "redisplay.h"
  34. #include "sysdep.h"
  35.  
  36. #include "syssignal.h" /* for SIGWINCH */
  37.  
  38. #include <errno.h>
  39.  
  40. DEFINE_DEVICE_TYPE (tty);
  41.  
  42. Lisp_Object Qterminal_type;
  43.  
  44.  
  45. static void
  46. allocate_tty_device_struct (struct device *d)
  47. {
  48.   d->device_data =
  49.     (struct tty_device *) xmalloc (sizeof (struct tty_device));
  50.  
  51.   /* zero out all slots. */
  52.   memset (d->device_data, 0, sizeof (struct tty_device));
  53.   /* except the lisp ones ... */
  54.   DEVICE_TTY_DATA (d)->tty = Qnil;
  55.   DEVICE_TTY_DATA (d)->terminal_type = Qnil;
  56. }
  57.  
  58. static void
  59. tty_init_device (struct device *d, Lisp_Object params)
  60. {
  61.   Lisp_Object tty = Qnil, terminal_type = Qnil;
  62.   FILE *infd, *outfd;
  63.  
  64.   tty = Fcdr_safe (Fassq (Qtty, params));
  65.   terminal_type = Fcdr_safe (Fassq (Qterminal_type, params));
  66.  
  67.   /* Open the specified device */
  68.  
  69.   if (NILP (tty))
  70.     {
  71.       infd = stdin;
  72.       outfd = stdout;
  73.     }
  74.   else
  75.     {
  76.       CHECK_STRING (tty, 0);
  77.       infd = outfd = fopen ((char *) string_data (XSTRING (tty)), "r+");
  78.       if (!infd)
  79.     error ("Unable to open tty %s", string_data (XSTRING (tty)));
  80.     }
  81.   
  82.   /* Determine the terminal type */
  83.  
  84.   if (!NILP (terminal_type))
  85.     CHECK_STRING (terminal_type, 0);
  86.   else
  87.     {
  88.       char *temp_type = (char *) getenv ("TERM");
  89.  
  90.       if (!temp_type)
  91.     {
  92.       if (infd != stdin)
  93.         fclose (infd);
  94.       error ("Cannot determine terminal type");
  95.     }
  96.       else
  97.     terminal_type = build_string (temp_type);
  98.     }
  99.  
  100.   allocate_tty_device_struct (d);
  101.   DEVICE_TTY_DATA (d)->infd = infd;
  102.   DEVICE_TTY_DATA (d)->outfd = outfd;
  103.   DEVICE_TTY_DATA (d)->terminal_type = terminal_type;
  104.   if (NILP (tty))
  105.     tty = build_string ("stdio");
  106.   DEVICE_TTY_DATA (d)->tty = tty;
  107.   if (NILP (DEVICE_NAME (d)))
  108.     DEVICE_NAME (d) = Ffile_name_nondirectory (tty);
  109.   DEVICE_INFD (d) = fileno (infd);
  110.   DEVICE_OUTFD (d) = fileno (outfd);
  111.   {
  112.     int tty_pg;
  113.     int controlling_tty_pg;
  114.     int cfd;
  115.  
  116.     /* OK, the only sure-fire way I can think of to determine
  117.        whether a particular TTY is our controlling TTY is to check
  118.        if it has the same foreground process group as our controlling
  119.        TTY.  This is OK because a process group can never simultaneously
  120.        be the foreground process group of two TTY's (in that case it
  121.        would have two controlling TTY's, which is not allowed). */
  122.  
  123.     EMACS_GET_TTY_PROCESS_GROUP (fileno (infd), &tty_pg);
  124.     cfd = open ("/dev/tty", O_RDWR, 0);
  125.     EMACS_GET_TTY_PROCESS_GROUP (cfd, &controlling_tty_pg);
  126.     close (cfd);
  127.     if (tty_pg == controlling_tty_pg)
  128.       {
  129.     DEVICE_TTY_DATA (d)->controlling_terminal = 1;
  130.     XSETDEVICE (Vcontrolling_terminal, d);
  131.     munge_tty_process_group ();
  132.       }
  133.     else
  134.       DEVICE_TTY_DATA (d)->controlling_terminal = 0;
  135.   }
  136.  
  137.   init_baud_rate (d);
  138.  
  139.   switch (init_tty_for_redisplay
  140.       (d, (char *) string_data (XSTRING (terminal_type))))
  141.     {
  142.     case TTY_UNABLE_OPEN_DATABASE:
  143.       error ("Can't access terminal information database");
  144.       break;
  145.     case TTY_TYPE_UNDEFINED:
  146.       error ("Terminal type `%s' undefined",
  147.          string_data (XSTRING (terminal_type)));
  148.       break;
  149.     case TTY_TYPE_INSUFFICIENT:
  150.       error ("Terminal type `%s' not powerful enough to run Emacs",
  151.          string_data (XSTRING (terminal_type)));
  152.       break;
  153.     case TTY_SIZE_UNSPECIFIED:
  154.       error ("Can't determine window size of terminal");
  155.       break;
  156.     case TTY_INIT_SUCCESS:
  157.       break;
  158.     default:
  159.       abort ();
  160.     }
  161.  
  162.   init_one_device (d);
  163.  
  164.   /* This will cause the elisp side of the TTY device intialization to
  165.      be loaded.  At this point we consider the two parts to be one.
  166.      They are split up because of requirements in lisp/prim/startup.el */
  167.   Frequire (intern ("pre-tty-win"), Qnil);
  168.   Frequire (intern ("post-tty-win"), Qnil);
  169. }
  170.  
  171. static void
  172. tty_mark_device (struct device *d, void (*markobj) (Lisp_Object))
  173. {
  174.   ((markobj) (DEVICE_TTY_DATA (d)->tty));
  175.   ((markobj) (DEVICE_TTY_DATA (d)->terminal_type));
  176. }
  177.  
  178.  
  179. static int
  180. tty_initially_selected_for_input (struct device *d)
  181. {
  182.   return 1;
  183. }
  184.  
  185. static void
  186. free_tty_device_struct (struct device *d)
  187. {
  188.   struct tty_device *td = (struct tty_device *) d->device_data;
  189.   if (td && td->term_entry_buffer) /* allocated in term_init () */
  190.     xfree (td->term_entry_buffer);
  191.   if (td)
  192.     xfree (td);
  193. }
  194.  
  195. static void
  196. tty_delete_device (struct device *d)
  197. {
  198.   if (DEVICE_TTY_DATA (d)->infd != stdin)
  199.     fclose (DEVICE_TTY_DATA (d)->infd);
  200.   if (DEVICE_TTY_DATA (d)->controlling_terminal)
  201.     {
  202.       Vcontrolling_terminal = Qnil;
  203.       unmunge_tty_process_group ();
  204.     }
  205.   free_tty_device_struct (d);
  206. }
  207.  
  208. #ifdef SIGWINCH
  209.  
  210. static SIGTYPE
  211. tty_device_size_change_signal (int signo)
  212. {
  213.   int old_errno = errno;
  214.   asynch_device_change_pending++;
  215.   signal_fake_event ();
  216.   EMACS_REESTABLISH_SIGNAL (SIGWINCH, tty_device_size_change_signal);
  217.   errno = old_errno;
  218.   SIGRETURN;
  219. }
  220.  
  221. /* frame_change_signal does nothing but set a flag that it was called.
  222.    When redisplay is called, it will notice that the flag is set and
  223.    call handle_pending_device_size_change to do the actual work. */
  224. static void
  225. tty_asynch_device_change (void)
  226. {
  227.   Lisp_Object device;
  228.  
  229.   for (device = Vdevice_list ; !NILP (device) ; device = XCDR (device))
  230.     {
  231.       int width, height;
  232.       Lisp_Object tail;
  233.       struct device *d = XDEVICE (XCAR (device));
  234.  
  235.       if (!DEVICE_IS_TTY (d))
  236.     continue;
  237.  
  238.       get_tty_device_size (d, &width, &height);
  239.       if (width > 0 && height > 0)
  240.     {
  241.       DEVICE_TTY_DATA (d)->width = width;
  242.       DEVICE_TTY_DATA (d)->height = height;
  243.  
  244.       for (tail = DEVICE_FRAME_LIST (d);
  245.            !NILP (tail);
  246.            tail = XCDR (tail))
  247.         {
  248.           struct frame *f = XFRAME (XCAR (tail));
  249.           
  250.           /* We know the frame is tty because we made sure that the
  251.          device is tty. */
  252.           change_frame_size (f, height, width, 0, 1);
  253.         }
  254.     }
  255.     }
  256. }
  257.  
  258. #endif /* SIGWINCH */
  259.  
  260. static int
  261. tty_device_pixel_width (struct device *d)
  262. {
  263.   return DEVICE_TTY_DATA (d)->width;
  264. }
  265.  
  266. static int
  267. tty_device_pixel_height (struct device *d)
  268. {
  269.   return DEVICE_TTY_DATA (d)->height;
  270. }
  271.  
  272.  
  273. static struct device *
  274. get_tty_device (Lisp_Object device)
  275. {
  276.   if (NILP (device))
  277.     device = Fselected_device ();
  278.   /* quietly accept frames for the device arg */
  279.   if (FRAMEP (device))
  280.     device = XFRAME (device)->device;
  281.   CHECK_TTY_DEVICE (device, 0);
  282.   return XDEVICE (device);
  283. }
  284.  
  285. DEFUN ("device-tty-tty", Fdevice_tty_tty, Sdevice_tty_tty, 0, 1, 0,
  286.        "Return the tty filename which DEVICE is connected to, as a string.")
  287.      (device)
  288.      Lisp_Object device;
  289. {
  290.   return DEVICE_TTY_DATA (get_tty_device (device))->tty;
  291. }
  292.  
  293. DEFUN ("device-tty-terminal-type", Fdevice_tty_terminal_type,
  294.        Sdevice_tty_terminal_type, 0, 1, 0,
  295.   "Return the terminal type of TTY device DEVICE.")
  296.      (device)
  297.      Lisp_Object device;
  298. {
  299.   return DEVICE_TTY_DATA (get_tty_device (device))->terminal_type;
  300. }
  301.  
  302.  
  303. /************************************************************************/
  304. /*                            initialization                            */
  305. /************************************************************************/
  306.  
  307. void
  308. syms_of_device_tty (void)
  309. {
  310.   defsubr (&Sdevice_tty_tty);
  311.   defsubr (&Sdevice_tty_terminal_type);
  312.   defsymbol (&Qterminal_type, "terminal-type");
  313. }
  314.  
  315. void
  316. device_type_create_tty (void)
  317. {
  318.   INITIALIZE_DEVICE_TYPE (tty, "tty", "device-tty-p");
  319.  
  320.   DEVICE_HAS_METHOD (tty, init_device);
  321.   DEVICE_HAS_METHOD (tty, mark_device);
  322.   DEVICE_HAS_METHOD (tty, initially_selected_for_input);
  323.   DEVICE_HAS_METHOD (tty, delete_device);
  324. #ifdef SIGWINCH
  325.   DEVICE_HAS_METHOD (tty, asynch_device_change);
  326. #endif /* SIGWINCH */
  327.   DEVICE_HAS_METHOD (tty, device_pixel_width);
  328.   DEVICE_HAS_METHOD (tty, device_pixel_height);
  329. }
  330.  
  331. void
  332. vars_of_device_tty (void)
  333. {
  334.   Fprovide (Qtty);
  335. }
  336.  
  337. void
  338. init_device_tty (void)
  339. {
  340. #ifdef SIGWINCH
  341.   if (initialized && !noninteractive)
  342.     signal (SIGWINCH, tty_device_size_change_signal);
  343. #endif /* SIGWINCH */
  344. }
  345.