keyboard.c revision 50141
1261287Sdes/*-
2261287Sdes * Copyright (c) 1997 S�ren Schmidt
3261287Sdes * All rights reserved.
4261287Sdes *
5261287Sdes * Redistribution and use in source and binary forms, with or without
6261287Sdes * modification, are permitted provided that the following conditions
7261287Sdes * are met:
8261287Sdes * 1. Redistributions of source code must retain the above copyright
9261287Sdes *    notice, this list of conditions and the following disclaimer
10261287Sdes *    in this position and unchanged.
11261287Sdes * 2. Redistributions in binary form must reproduce the above copyright
12261287Sdes *    notice, this list of conditions and the following disclaimer in the
13261287Sdes *    documentation and/or other materials provided with the distribution.
14261287Sdes * 3. The name of the author may not be used to endorse or promote products
15261287Sdes *    derived from this software withough specific prior written permission
16261287Sdes *
17261287Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18261287Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19261287Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20261287Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21261287Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22261287Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23261287Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24261287Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25261287Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26261287Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27261287Sdes *
28261287Sdes *  $Id: keyboard.c,v 1.1 1997/10/01 20:53:38 sos Exp $
29261287Sdes */
30261287Sdes
31261287Sdes#include <stdio.h>
32261287Sdes#include <sys/types.h>
33261287Sdes#include <sys/ioctl.h>
34261287Sdes#include <termios.h>
35261287Sdes#include <sys/time.h>
36261287Sdes#include <machine/console.h>
37261287Sdes#include "vgl.h"
38261287Sdes
39261287Sdesstatic struct termios VGLKeyboardTty;
40261287Sdesstatic int VGLKeyboardMode = -1;
41261287Sdes
42261287Sdesint
43261287SdesVGLKeyboardInit(int mode)
44261287Sdes{
45261287Sdes  static struct termios term;
46261287Sdes
47261287Sdes  ioctl(0, KDGKBMODE, &VGLKeyboardMode);
48261287Sdes  tcgetattr(0, &VGLKeyboardTty);
49261287Sdes
50261287Sdes  term = VGLKeyboardTty;
51261287Sdes  cfmakeraw(&term);
52261287Sdes  term.c_iflag = IGNPAR | IGNBRK;
53261287Sdes  term.c_oflag = 0;
54261287Sdes  term.c_cflag = CREAD | CS8;
55261287Sdes  term.c_lflag &= ~(ICANON | ECHO | ISIG);
56261287Sdes  term.c_cc[VTIME] = 0;
57261287Sdes  term.c_cc[VMIN] = 0;
58261287Sdes  cfsetispeed(&term, 9600);
59261287Sdes  cfsetospeed(&term, 9600);
60261287Sdes  tcsetattr(0, TCSANOW, &term);
61261287Sdes
62261287Sdes  switch (mode) {
63261287Sdes  case VGL_RAWKEYS:
64261287Sdes    ioctl(0, KDSKBMODE, K_RAW);
65261287Sdes    break;
66261287Sdes  case VGL_CODEKEYS:
67261287Sdes    ioctl(0, KDSKBMODE, K_CODE);
68261287Sdes    break;
69261287Sdes  case VGL_XLATEKEYS:
70261287Sdes    ioctl(0, KDSKBMODE, K_XLATE);
71    break;
72  }
73  return 0;
74}
75
76void
77VGLKeyboardEnd()
78{
79  if (VGLKeyboardMode != -1) {
80    ioctl(0, KDSKBMODE, VGLKeyboardMode);
81    tcsetattr(0, TCSANOW, &VGLKeyboardTty);
82  }
83}
84
85int
86VGLKeyboardGetCh()
87{
88  unsigned char ch = 0;
89
90  read (0, &ch, 1);
91  return (int)ch;
92}
93