130044Ssos/*-
2229784Suqs * Copyright (c) 1997 S��ren Schmidt
330044Ssos * All rights reserved.
430044Ssos *
530044Ssos * Redistribution and use in source and binary forms, with or without
630044Ssos * modification, are permitted provided that the following conditions
730044Ssos * are met:
830044Ssos * 1. Redistributions of source code must retain the above copyright
930044Ssos *    notice, this list of conditions and the following disclaimer
1030044Ssos *    in this position and unchanged.
1130044Ssos * 2. Redistributions in binary form must reproduce the above copyright
1230044Ssos *    notice, this list of conditions and the following disclaimer in the
1330044Ssos *    documentation and/or other materials provided with the distribution.
1430044Ssos * 3. The name of the author may not be used to endorse or promote products
1597748Sschweikh *    derived from this software without specific prior written permission
1630044Ssos *
1730044Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1830044Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1930044Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2030044Ssos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2130044Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2230044Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2330044Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2430044Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2530044Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2630044Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2730044Ssos */
2830044Ssos
2983551Sdillon#include <sys/cdefs.h>
3083551Sdillon__FBSDID("$FreeBSD$");
3183551Sdillon
3230044Ssos#include <stdio.h>
3330044Ssos#include <sys/types.h>
3430044Ssos#include <sys/ioctl.h>
3530044Ssos#include <termios.h>
3630044Ssos#include <sys/time.h>
3766834Sphk#include <sys/fbio.h>
3866834Sphk#include <sys/kbio.h>
3930044Ssos#include "vgl.h"
4030044Ssos
4130044Ssosstatic struct termios VGLKeyboardTty;
4230044Ssosstatic int VGLKeyboardMode = -1;
4330044Ssos
4430044Ssosint
4530044SsosVGLKeyboardInit(int mode)
4630044Ssos{
4730044Ssos  static struct termios term;
4830044Ssos
4930044Ssos  ioctl(0, KDGKBMODE, &VGLKeyboardMode);
5030044Ssos  tcgetattr(0, &VGLKeyboardTty);
5130044Ssos
5230044Ssos  term = VGLKeyboardTty;
5350141Syokota  cfmakeraw(&term);
5430044Ssos  term.c_iflag = IGNPAR | IGNBRK;
5571518Ssobomax  term.c_oflag = OPOST | ONLCR;
5630044Ssos  term.c_cflag = CREAD | CS8;
5730044Ssos  term.c_lflag &= ~(ICANON | ECHO | ISIG);
5830044Ssos  term.c_cc[VTIME] = 0;
5930044Ssos  term.c_cc[VMIN] = 0;
6030044Ssos  cfsetispeed(&term, 9600);
6130044Ssos  cfsetospeed(&term, 9600);
6271518Ssobomax  tcsetattr(0, TCSANOW | TCSAFLUSH, &term);
6330044Ssos
6430044Ssos  switch (mode) {
6530044Ssos  case VGL_RAWKEYS:
6630044Ssos    ioctl(0, KDSKBMODE, K_RAW);
6730044Ssos    break;
6830044Ssos  case VGL_CODEKEYS:
6930044Ssos    ioctl(0, KDSKBMODE, K_CODE);
7030044Ssos    break;
7130044Ssos  case VGL_XLATEKEYS:
7230044Ssos    ioctl(0, KDSKBMODE, K_XLATE);
7330044Ssos    break;
7430044Ssos  }
7530044Ssos  return 0;
7630044Ssos}
7730044Ssos
7830044Ssosvoid
7930044SsosVGLKeyboardEnd()
8030044Ssos{
8150141Syokota  if (VGLKeyboardMode != -1) {
8230044Ssos    ioctl(0, KDSKBMODE, VGLKeyboardMode);
8371518Ssobomax    tcsetattr(0, TCSANOW | TCSAFLUSH, &VGLKeyboardTty);
8450141Syokota  }
8530044Ssos}
8630044Ssos
8730044Ssosint
8830044SsosVGLKeyboardGetCh()
8930044Ssos{
9030044Ssos  unsigned char ch = 0;
9130044Ssos
9230044Ssos  read (0, &ch, 1);
9330044Ssos  return (int)ch;
9430044Ssos}
95