home *** CD-ROM | disk | FTP | other *** search
- #include "config.h"
- #undef NULL
- #include "lisp.h"
- #include "termchar.h"
-
- #include <stdio.h>
-
- #undef LONGBITS
-
- #include <exec/types.h>
- #include <devices/timer.h>
-
- struct Library *TimerBase;
-
- #include <proto/exec.h>
- #include "amiga.h"
- #include "termhooks.h"
-
- static struct timerequest *far TimeRequest;
- static int term_initialised;
-
- int sleeping;
-
- ULONG inputsig;
- static ULONG timesig;
- static int timer_sent;
-
- /* Get terminal size from system.
- Store number of lines into *heightp and width into *widthp.
- If zero or a negative number is stored, the value is not valid. */
-
- get_screen_size (widthp, heightp)
- int *widthp, *heightp;
- {
- if (term_initialised && !inhibit_window_system)
- get_window_size(widthp, heightp);
- else /* We don't known what size the terminal is */
- {
- *widthp = 0;
- *heightp = 0;
- }
- }
-
- init_baud_rate ()
- {
- if (noninteractive || !term_initialised) baud_rate = 1200;
- else if (!inhibit_window_system) baud_rate = 38400;
- else baud_rate = serial_baud_rate();
- }
-
- void check_intuition ()
- {
- if (noninteractive || inhibit_window_system)
- error ("You aren't using a window.");
- }
-
- void enque(unsigned char toque, int meta)
- /* place input keys in keyboard buffer
- If high bit is set, precede character with ^Q (hack).
- If meta is true, set high bit.
- If both the high bit & meta are true, we have a problem. Ignore it.
- */
- {
- /* Hack CSI to be AMIGASEQ (to allow defining function keys, etc) */
- if (toque == 0233) toque = AMIGASEQ;
-
- if (toque >= 0200) /* Special character, precede with ^Q */
- {
- kbd_buffer_store_char('q' & 037);
- kbd_buffer_store_char(toque);
- }
- else
- {
- if (meta) toque |= 0200;
-
- kbd_buffer_store_char(toque);
- }
- }
-
- void amiga_consume_input(int force)
- /* If force is TRUE & some non-keyboard (eg mouse events) input is pending,
- insert the appropriate magic sequence in the input stream */
- {
- if (term_initialised)
- {
- if (!inhibit_window_system) check_window(force);
- else check_serial(force);
- check_arexx(force, TRUE);
- }
- }
-
- void amiga_wait_for_input(void)
- {
- if (term_initialised)
- {
- ULONG sigs = Wait(inputsig | alarmsig);
-
- if (sigs & alarmsig) check_alarm();
- }
- }
-
- discard_tty_input ()
- {
- if (noninteractive)
- return;
-
- /* Not strictly correct, but only use is followed by kbd_count = 0 */
- /* (see discard-input) */
- amiga_consume_input(FALSE);
- }
-
- #define CBUFSIZE 1024
- #undef fwrite
- #undef fflush
-
- char cbuffer[CBUFSIZE + 16], *cbuffer_pos;
-
- void emacs_fflush(FILE *f)
- {
- if (noninteractive || f != stdout) _flsbf(-1, f);
- else
- {
- int len;
-
- len = cbuffer_pos - cbuffer;
- if (term_initialised)
- if (!inhibit_window_system) screen_puts(cbuffer, len);
- else serial_puts(cbuffer, len);
- if (termscript) fwrite (cbuffer, 1, len, termscript);
- cbuffer_pos = cbuffer;
- }
- }
-
- void emacs_putchar(int c)
- {
- if (cbuffer_pos >= cbuffer + CBUFSIZE) emacs_fflush(stdout);
- *cbuffer_pos++ = c;
- }
-
- void emacs_output(char *str, int size)
- {
- if (cbuffer_pos + size > cbuffer + CBUFSIZE) emacs_fflush(stdout);
- if (size > CBUFSIZE)
- {
- if (term_initialised)
- if (!inhibit_window_system) screen_puts(str, size);
- else serial_puts(str, size);
- }
- else
- {
- memcpy(cbuffer_pos, str, size);
- cbuffer_pos += size;
- }
- }
-
- void emacs_fwrite(char *str, unsigned int nblocks, unsigned int len, FILE *f)
- {
- if (noninteractive || f != stdout) fwrite (str, nblocks, len, f);
- else
- {
- unsigned int size;
-
- if (nblocks == 1) size = len; /* Emacs always uses 1 "block" */
- else size = nblocks * len;
-
- emacs_output(str, size);
- }
- }
-
- /* Should sleep & sleep_or_input be broken by an alarm ? */
- void sleep(int secs)
- {
- extern int kbd_count;
-
- if (timer_sent)
- {
- AbortIO(TimeRequest);
- WaitIO(TimeRequest);
- }
- TimeRequest->tr_time.tv_secs = (ULONG) secs;
- TimeRequest->tr_time.tv_micro = 0L;
- TimeRequest->tr_node.io_Command = TR_ADDREQUEST;
- SendIO (TimeRequest);
- timer_sent = TRUE;
-
- sleeping = TRUE;
- amiga_consume_input(FALSE);
- while (!CheckIO(TimeRequest))
- {
- Wait(inputsig | timesig);
- amiga_consume_input(FALSE);
- }
- sleeping = FALSE;
- }
-
- void sleep_or_input(int secs)
- {
- extern int kbd_count;
-
- if (timer_sent)
- {
- AbortIO(TimeRequest);
- WaitIO(TimeRequest);
- }
- TimeRequest->tr_time.tv_secs = (ULONG) secs;
- TimeRequest->tr_time.tv_micro = 0L;
- TimeRequest->tr_node.io_Command = TR_ADDREQUEST;
- SendIO (TimeRequest);
- timer_sent = TRUE;
-
- sleeping = TRUE;
- amiga_consume_input(FALSE);
- while (kbd_count == 0)
- {
- Wait(inputsig | timesig);
- if (CheckIO(TimeRequest)) break;
- amiga_consume_input(TRUE);
- }
- sleeping = FALSE;
- AbortIO(TimeRequest);
- WaitIO(TimeRequest);
- }
-
- void syms_of_amiga_tty(void)
- {
- syms_of_amiga_screen();
- syms_of_amiga_rexx();
- }
-
- int init_amiga_tty()
- {
- inputsig = 0;
- if (!(TimeRequest = (struct timerequest *)
- device_open("timer.device", 0L, 0L, 0L, 0, sizeof(struct timerequest))))
- {
- fprintf(stderr, "Timer will not open\n");
- return FALSE;
- }
- TimerBase = (struct Library *)TimeRequest->tr_node.io_Device;
- timesig = 1L << TimeRequest->tr_node.io_Message.mn_ReplyPort->mp_SigBit;
- timer_sent = FALSE;
- term_initialised = FALSE;
- return init_amiga_rexx();
- }
-
- void early_amiga_tty()
- {
- cbuffer_pos = cbuffer;
- }
-
- void amiga_term_open(void)
- {
- if (!(inhibit_window_system ? init_amiga_serial() : init_amiga_screen()))
- exit(20);
- term_initialised = TRUE;
- }
-
- void cleanup_amiga_tty()
- {
- cleanup_amiga_rexx();
- cleanup_amiga_serial();
- cleanup_amiga_screen();
- device_close(TimeRequest);
- }
-