⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kb_input.c

📁 功能强大的文本编辑器
💻 C
📖 第 1 页 / 共 5 页
字号:
/*      kb_input.c                                       17.02.04 */
/*!
/  --------------------------------------------------------------
/  Copyright (C) 2004: Michael Braun
/                      Kaetinger Muehlenweg 103 A
/                      D-28816 Stuhr
/  --------------------------------------------------------------
/
/  keyboard input subroutines, operating system dependant
/
*/

/****************************************************************
*                                                               *
*  BENUTZTE UNTERPROGRAMME (C LIBRARY)                          *
*                                                               *
****************************************************************/

#include "config.h"
#if (ACT_OP_SYSTEM == WIN_32)
#include <windows.h>
#endif
#include "standard.h"
#include "global.h"
#include "kb_input.h"
#include "mon_outp.h"
#include "history.h"
#include "disp_hnd.h"
#include "err_mess.h"
#include "macro.h"
#include "perform.h"
#if (ACT_OP_SYSTEM == MS_DOS)
#include "mousec.h"
#endif
#include "mbedit.h"
#include "ansi_out.h"


/****************************************************************
*                                                               *
*  BENUTZTE UNTERPROGRAMME / GLOBALE VARIABLEN                  *
*                                                               *
****************************************************************/


#if (ACT_OP_SYSTEM == OS_9)

   #ifndef fileno
      #define fileno(p)       ((p)->_fd)
   #endif

#endif


#define PATH_0   fileno(stdin)   /* standard input */

#define TEST_PRINT  0


/****************************************************************
*                                                               *
*  ENDE DER DEKLARATIONEN                                       *
*                                                               *
****************************************************************/

/* -FF-  */

#if 0

arrangement of functions (overview 09.05.94)

(COMMON)
int check_input_redirection (void)
int ctrlc_active (void)
void set_key_fifo (int key_back)
void set_string_buff (char *string, int str_len)
int key_pressed (void)
int loc_get_1_key (void)
int get_1_key (int check_macro)
void ctrlchandler (int sig)
int check_end_of_file (int key)

(MS_DOS)
void sleep_msec (int time_delay)
int loc_key_pressed (void)
static int loc_get_1_int (void)
void kb_echo_off (void)
void kb_echo_on (void)

(OS_9)
void sleep_msec (int msec)
int loc_key_pressed (void)
static int get_1_byte (void)
static int get_1_byte_with_timeout (int wait_time)
void kb_echo_off (void)
void kb_echo_on (void)

(UNIX)
void syserr (char *text)
void sleep_msec (int msec)
static void setblock (int fd, int on)
int loc_key_pressed (void)
static int get_1_byte (void)
static int get_1_byte_with_timeout (int wait_time)
void kb_echo_off (void)
static void setraw (uchar c_num, uchar t_msec10)
void kb_echo_on (void)

#endif

/* -FF-  */

/*****************************************/
/*                                       */
/*  operating system independant region  */
/*                                       */
/*****************************************/

/* flag, that shows, if input is redirected */
static int input_redirected = 0;

int check_input_redirection (void)
{
/* call once at program start */

   if (isatty(PATH_0))
      input_redirected = 0;     /* normal tty */
   else
      input_redirected = 1;     /* redirected to file */

#if (TEST_PRINT)
   printf ("\n>>> check_input_redirection: input_redirected = %d \n",
                                        input_redirected);
#endif

   return input_redirected;

}  /* check_input_redirection */

/* -FF-  */

#if 0

We have 2 buffers for keyboard-data:

  - int  key_fifo    [] : is used as int buffer for keyboard keys,
                          (special keys too). new entries are always
                          stored at the end of the queue.

  - char string_buff [] : buffer for complete strings (special functions
                          like ^S, ^N, ^R), only one string at the time.

#endif

static int  key_fifo    [100];      /* type ahead buffer for keyboard keys */
static char string_buff [BUF_256];  /* string buffer for normal chars */


int ctrlc_active (void)
{
   return (*key_fifo == 0x03);    /* fuer check auf ^C */
}


void set_key_fifo (int key_back)
{
int ii, buf_len, index, overflow;
static int old_overflow;

/* put back 1 int key to fifo (^C: begin, any other char: end) */

   index = 0;    /* set to begin of queue */

   if (key_back != 0x03)     /* not ^C ? */
   {                         /* search end of queue */
      buf_len = lengthof(key_fifo) - 1;   /* last entry must always be 0 ! */
      
      for (ii = 0 ; ii < buf_len ; ii++)
      {
         if ((key_fifo [ii] == 0) ||   /* end of queue */
             (ii == (buf_len - 1)))    /* end of buffer */
         {
            index = ii;

            overflow = (ii == (buf_len - 1));
            if (overflow && (!old_overflow))    /* pos. slope ? */
               err_message (KEYBOARD_FIFO_OVERFLOW);
            old_overflow = overflow;

            break;
         }
      }
   }
   else
   {
      reset_macro_stack ();
   }

   key_fifo [index  ] = key_back;  /* store it */
   key_fifo [index+1] = 0;         /* new end of queue */

   return;
}  /* set_key_fifo */

/* -FF-  */

void set_string_buff (char *string, int str_len)
{

/* set 1 char string */
   str_len = min (str_len, (sizeof(string_buff) - 1));
   strncpy (string_buff, string, str_len);      /* copy string */
   string_buff [str_len] = '\0';       /* forced end of string */

   return;
}  /* set_string_buff */

/* -FF-  */

int key_pressed (void)
{

   if (*string_buff)       return 1;
   if (*key_fifo)          return 1;
   if (loc_key_pressed ()) return 1;

   return 0;   /* nothing found */

}   /* key_pressed */

/* -FF-  */

int loc_get_1_key (void)
{
int key;

#if (WITH_ACTION_FLAG)
int do_it;

   do_it = !(loc_key_pressed ());
   if (do_it)
      show_action_flag ('?');
#endif

   key = loc_get_1_int ();

#if (WITH_ACTION_FLAG)
   if (do_it)
      show_action_flag ('!');
#endif


   if (key == 0x0d) key = EOLN;
   return key;

}  /* loc_get_1_key */

/* -FF-  */

int get_1_key (int check_macro)
{
int key;

/*
  Diese Routine liefert einen Tastendruck zurueck, der von verschiedenen
  Quellen herruehren kann :
    - key_fifo (^C wurde gedrueckt ?)
    - string_buff (special keys, von bediener oder macro ausgeloest)
    - macro execution (ablauf eines macros)
    - key_fifo (1 wert, der getestet und zurueckgeschrieben wurde)
    - normal keyboard (bediener-eingabe)

  Die Quellen werden in dieser Reihenfolge abgecheckt, und bei Vorliegen
  eines "Tastendrucks" wird dieser ans rufende Programm zurueckgeliefert
  und ggf. im Buffer geloescht.
*/


/* 1.) ^C gedrueckt ? */
   if (*key_fifo == 0x03)
   {
      *key_fifo = 0;     /* clear input queue */
      reset_macro_stack ();
      return 0x03;
   }


/* 2.) test string buffer */
   if (*string_buff)
   {
      key = (int) (*string_buff & 0xff);      /* take LSB */
      strcpy (string_buff, &string_buff[1]);  /* shift buffer left */
      if (key == 0x03) reset_macro_stack ();
      return key;                             /* ==> */
   }


/* 3.) macro execute ? */
   if (check_macro)
   {
      if (get_macro_status () == MACRO_EXECUTE)
      {
         return get_macro_key ();   /* ==> */
      }
   }


/* 4.) test int buffer */
   if (*key_fifo)
   {
      key = *key_fifo;                        /* take it */
                                              /* shift buffer 1 entry left */
      memcpy (key_fifo, &key_fifo[1],
             (sizeof(key_fifo) - sizeof(key_fifo[0])));
      if (key == 0x03) reset_macro_stack ();
      return key;                             /* ==> */
   }


/* 5.) read 1 key by user input */
   key = loc_get_1_key ();
   if (key == 0x03) reset_macro_stack ();

/* macro create ? */
   if (check_macro)
   {
      if (get_macro_status () == MACRO_CREATE)
      {
         put_macro_key (key);
      }
   }

   return key;    /* ==> */

}  /* get_1_key */


/* Handles SIGINT (CTRL+C) interrupt. */
void ctrlchandler (int sig)
{

/* wg. compiler warning */
   sig = sig;

/* Disallow CTRL+C during handler. */
   signal (SIGINT, SIG_DFL);

/* handling of ^C */
   key_fifo [0] = 0x03;    /* store ^C in keyboard input queue */
   key_fifo [1] = '\0';    /* new end of queue */

   reset_macro_stack ();


/* The CTRL+C interrupt must be reset to our handler since by
 * default it is reset to the system handler.
 */
   signal (SIGINT, ctrlchandler);

}  /* ctrlchandler */

/* -FF-  */

int check_end_of_file (int key)
{
#if (TEST_PRINT)
char help;

   if (mb_isprint (key))
      help = (char) key;
   else
      help = ' ';
      
   printf ("\n>>> check_end_of_file: input_redirected = %d, key =%4d = 0x%02x = \'%c\' \n",
                                  input_redirected, key, key, help);
#endif

   if ((input_redirected) && (key == EOF))
   {
   /* input redirection to console */

      fclose (stdin);
      if ((fopen (CONSOLE_NAME, "r")) != NULL)
         input_redirected = 0;   /* abschalten */

      kb_echo_off ();          /* keyboard input klarmachen */

      set_batch_mode (0);      /* batch mode abschalten */

      err_message (EOF_IN_REDIRECTED_INPUT);

      return KEY_DO_NOTHING;   /* do nothing */
   }
   else
   {
      return key;
   }

}   /* check_end_of_file */

/* -FF-  */

/* fallunterscheidung */

#if (ACT_OP_SYSTEM == MS_DOS)

/*****************************************/
/*                                       */
/*           PC - MF2-Keyboard           */
/*                                       */
/*****************************************/

/*****************************************/
/*                                       */
/*           MS_DOS region               */
/*                                       */
/*****************************************/

/************************/
/*  n msec's delay      */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -