home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / text / emacsdif.lha / emacs-18.58 / src / amiga_tty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-16  |  5.5 KB  |  265 lines

  1. #include "config.h"
  2. #undef NULL
  3. #include "lisp.h"
  4. #include "termchar.h"
  5.  
  6. #include <stdio.h>
  7.  
  8. #undef LONGBITS
  9.  
  10. #include <exec/types.h>
  11. #include <devices/timer.h>
  12.  
  13. struct Library *TimerBase;
  14.  
  15. #include <proto/exec.h>
  16. #include "amiga.h"
  17. #include "termhooks.h"
  18.  
  19. static struct timerequest *far TimeRequest;
  20. static int term_initialised;
  21.  
  22. int sleeping;
  23.  
  24. ULONG inputsig;
  25. static ULONG timesig;
  26. static int timer_sent;
  27.  
  28. /* Get terminal size from system.
  29.    Store number of lines into *heightp and width into *widthp.
  30.    If zero or a negative number is stored, the value is not valid.  */
  31.  
  32. get_screen_size (widthp, heightp)
  33.      int *widthp, *heightp;
  34. {
  35.     if (term_initialised && !inhibit_window_system)
  36.     get_window_size(widthp, heightp);
  37.     else /* We don't known what size the terminal is */
  38.     {
  39.     *widthp = 0;
  40.     *heightp = 0;
  41.     }
  42. }
  43.  
  44. init_baud_rate ()
  45. {
  46.   if (noninteractive || !term_initialised) baud_rate = 1200;
  47.   else if (!inhibit_window_system) baud_rate = 38400;
  48.   else baud_rate = serial_baud_rate();
  49. }
  50.  
  51. void check_intuition ()
  52. {
  53.     if (noninteractive || inhibit_window_system)
  54.         error ("You aren't using a window.");
  55. }
  56.  
  57. void enque(unsigned char toque, int meta)
  58. /* place input keys in keyboard buffer
  59.    If high bit is set, precede character with ^Q (hack).
  60.    If meta is true, set high bit.
  61.    If both the high bit & meta are true, we have a problem. Ignore it.
  62. */
  63. {
  64.     /* Hack CSI to be AMIGASEQ (to allow defining function keys, etc) */
  65.     if (toque == 0233) toque = AMIGASEQ;
  66.  
  67.     if (toque >= 0200) /* Special character, precede with ^Q */
  68.     {
  69.     kbd_buffer_store_char('q' & 037);
  70.     kbd_buffer_store_char(toque);
  71.     }
  72.     else
  73.     {
  74.     if (meta) toque |= 0200;
  75.  
  76.     kbd_buffer_store_char(toque);
  77.     }
  78. }
  79.  
  80. void amiga_consume_input(int force)
  81. /* If force is TRUE & some non-keyboard (eg mouse events) input is pending,
  82.    insert the appropriate magic sequence in the input stream */
  83. {
  84.     if (term_initialised)
  85.     {
  86.     if (!inhibit_window_system) check_window(force);
  87.     else check_serial(force);
  88.     check_arexx(force, TRUE);
  89.     }
  90. }
  91.  
  92. void amiga_wait_for_input(void)
  93. {
  94.     if (term_initialised)
  95.     {
  96.     ULONG sigs = Wait(inputsig | alarmsig);
  97.     
  98.     if (sigs & alarmsig) check_alarm();
  99.     }
  100. }
  101.  
  102. discard_tty_input ()
  103. {
  104.   if (noninteractive)
  105.     return;
  106.  
  107.   /* Not strictly correct, but only use is followed by kbd_count = 0 */
  108.   /* (see discard-input) */
  109.   amiga_consume_input(FALSE);
  110. }
  111.  
  112. #define CBUFSIZE 1024
  113. #undef fwrite
  114. #undef fflush
  115.  
  116. char cbuffer[CBUFSIZE + 16], *cbuffer_pos;
  117.  
  118. void emacs_fflush(FILE *f)
  119. {
  120.     if (noninteractive || f != stdout) _flsbf(-1, f);
  121.     else
  122.     {
  123.     int len;
  124.  
  125.     len = cbuffer_pos - cbuffer;
  126.     if (term_initialised)
  127.         if (!inhibit_window_system) screen_puts(cbuffer, len);
  128.         else serial_puts(cbuffer, len);
  129.     if (termscript) fwrite (cbuffer, 1, len, termscript);
  130.     cbuffer_pos = cbuffer;
  131.     }
  132. }
  133.  
  134. void emacs_putchar(int c)
  135. {
  136.     if (cbuffer_pos >= cbuffer + CBUFSIZE) emacs_fflush(stdout);
  137.     *cbuffer_pos++ = c;
  138. }
  139.  
  140. void emacs_output(char *str, int size)
  141. {
  142.     if (cbuffer_pos + size > cbuffer + CBUFSIZE) emacs_fflush(stdout);
  143.     if (size > CBUFSIZE)
  144.     {
  145.     if (term_initialised)
  146.         if (!inhibit_window_system) screen_puts(str, size);
  147.         else serial_puts(str, size);
  148.     }
  149.     else
  150.     {
  151.     memcpy(cbuffer_pos, str, size);
  152.     cbuffer_pos += size;
  153.     }
  154. }
  155.  
  156. void emacs_fwrite(char *str, unsigned int nblocks, unsigned int len, FILE *f)
  157. {
  158.     if (noninteractive || f != stdout) fwrite (str, nblocks, len, f);
  159.     else
  160.     {
  161.     unsigned int size;
  162.  
  163.     if (nblocks == 1) size = len; /* Emacs always uses 1 "block" */
  164.     else size = nblocks * len;
  165.  
  166.     emacs_output(str, size);
  167.     }
  168. }
  169.  
  170. /* Should sleep & sleep_or_input be broken by an alarm ? */
  171. void sleep(int secs)
  172. {
  173.     extern int kbd_count;
  174.  
  175.     if (timer_sent)
  176.     {
  177.     AbortIO(TimeRequest);
  178.     WaitIO(TimeRequest);
  179.     }
  180.     TimeRequest->tr_time.tv_secs = (ULONG) secs;
  181.     TimeRequest->tr_time.tv_micro = 0L;
  182.     TimeRequest->tr_node.io_Command = TR_ADDREQUEST;
  183.     SendIO (TimeRequest);
  184.     timer_sent = TRUE;
  185.  
  186.     sleeping = TRUE;
  187.     amiga_consume_input(FALSE);
  188.     while (!CheckIO(TimeRequest))
  189.     {
  190.     Wait(inputsig | timesig);
  191.     amiga_consume_input(FALSE);
  192.     }
  193.     sleeping = FALSE;
  194. }
  195.  
  196. void sleep_or_input(int secs)
  197. {
  198.     extern int kbd_count;
  199.  
  200.     if (timer_sent)
  201.     {
  202.     AbortIO(TimeRequest);
  203.     WaitIO(TimeRequest);
  204.     }
  205.     TimeRequest->tr_time.tv_secs = (ULONG) secs;
  206.     TimeRequest->tr_time.tv_micro = 0L;
  207.     TimeRequest->tr_node.io_Command = TR_ADDREQUEST;
  208.     SendIO (TimeRequest);
  209.     timer_sent = TRUE;
  210.  
  211.     sleeping = TRUE;
  212.     amiga_consume_input(FALSE);
  213.     while (kbd_count == 0)
  214.     {
  215.     Wait(inputsig | timesig);
  216.     if (CheckIO(TimeRequest)) break;
  217.     amiga_consume_input(TRUE);
  218.     }
  219.     sleeping = FALSE;
  220.     AbortIO(TimeRequest);
  221.     WaitIO(TimeRequest);
  222. }
  223.  
  224. void syms_of_amiga_tty(void)
  225. {
  226.     syms_of_amiga_screen();
  227.     syms_of_amiga_rexx();
  228. }
  229.  
  230. int init_amiga_tty()
  231. {
  232.     inputsig = 0;
  233.     if (!(TimeRequest = (struct timerequest *)
  234.       device_open("timer.device", 0L, 0L, 0L, 0, sizeof(struct timerequest))))
  235.     {
  236.     fprintf(stderr, "Timer will not open\n");
  237.     return FALSE;
  238.     }
  239.     TimerBase = (struct Library *)TimeRequest->tr_node.io_Device;
  240.     timesig = 1L << TimeRequest->tr_node.io_Message.mn_ReplyPort->mp_SigBit;
  241.     timer_sent = FALSE;
  242.     term_initialised = FALSE;
  243.     return init_amiga_rexx();
  244. }
  245.  
  246. void early_amiga_tty()
  247. {
  248.     cbuffer_pos = cbuffer;
  249. }
  250.  
  251. void amiga_term_open(void)
  252. {
  253.     if (!(inhibit_window_system ? init_amiga_serial() : init_amiga_screen()))
  254.     exit(20);
  255.     term_initialised = TRUE;
  256. }
  257.  
  258. void cleanup_amiga_tty()
  259. {
  260.     cleanup_amiga_rexx();
  261.     cleanup_amiga_serial();
  262.     cleanup_amiga_screen();
  263.     device_close(TimeRequest);
  264. }
  265.