kbd_tty.cc
来自「initial working phase of the design of s」· CC 代码 · 共 124 行
CC
124 行
#include "kbd_tty.h"#include <unistd.h> /* STDIN_FILENO */#include <termios.h> /* tcget/setattr, cfmakeraw */#include <stdio.h> // printf#include <stdlib.h>#include <string.h>#include <fcntl.h>#include <linux/kd.h>#include <linux/keyboard.h>#include <sys/ioctl.h>/*cfmakeraw() term_ios.c_lflag &= ~(ICANON|ECHO); // clear ICANON and ECHO term_ios.c_cc[ VMIN ] = 1; term_ios.c_cc[ VTIME ] = 0;*/void kbd_tty::kbd_reset(){ ioctl( handle, KDSETLED, 0 ); tcsetattr( STDIN_FILENO, TCSANOW, /* restore defaults */ ( struct termios* )defaults ); if (ioctl(handle, KDSKBMODE, oldkbmode));}kbd_tty::~kbd_tty(){ kbd_reset(); delete ( struct termios* )settings; delete ( struct termios* )defaults;}#include <signal.h>kbd_tty::kbd_tty(){ signal( SIGIO, SIG_IGN );// signal( SIGPIPE, SIG_IGN );// signal( SIGCHLD, SIG_IGN );// signal( SIGTERM, SIG_IGN );// signal( SIGHUP, SIG_IGN ); handle = STDIN_FILENO; if (ioctl(handle, KDGKBMODE, &oldkbmode));/* { perror("KDGKBMODE"); exit(1); }*/ if( !isatty( handle ) ) /* stdin needs to be a terminal */ { printf( "I need a terminal!\n" ); settings = defaults = NULL; } else { defaults = ( void* )new struct termios; settings = ( void* )new struct termios; tcgetattr( handle, ( struct termios* )defaults ); /* save the usual terminal mode */ memcpy( settings, defaults, sizeof( struct termios ) ); cfmakeraw( ( struct termios* )settings ); /* we want a non-canon terminal */ tcsetattr( handle, TCSANOW, ( struct termios* )settings ); if( ( -1 == fcntl( handle, F_SETFL, fcntl( handle, F_GETFL, 0 ) | O_ASYNC | O_NONBLOCK ) ) || ( -1 == fcntl( handle, F_SETOWN, getpid() ) ) ) { tcsetattr( handle, TCSANOW, ( struct termios* )defaults ); exit(1); } } ioctl( handle, KDSKBMODE, K_MEDIUMRAW );}kbd_tty::kbd_tty( int h ){ handle = h; if( !isatty( handle ) ) /* stdin needs to be a terminal */ { printf( "I need a terminal!\n" ); settings = defaults = NULL; } else { defaults = ( void* )new struct termios; settings = ( void* )new struct termios; tcgetattr( handle, ( struct termios* )defaults ); /* save the usual terminal mode */ memcpy( settings, defaults, sizeof( struct termios ) ); cfmakeraw( ( struct termios* )settings ); /* we want a non-canon terminal */ tcsetattr( handle, TCSANOW, ( struct termios* )settings ); }}void kbd_tty::leds( int snc ){ ioctl( handle, KDSETLED, ( ( snc & 4 ) ? LED_SCR : 0 ) | ( ( snc & 2 ) ? LED_NUM : 0 ) | ( ( snc & 1 ) ? LED_CAP : 0 ) );}#include <linux/vt.h>void kbd_tty::chvt( int vt ){ ioctl( handle, VT_ACTIVATE, vt );}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?