xref: /freebsd-src/lib/libvgl/keyboard.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1b478da36SSøren Schmidt /*-
2*5e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*5e53a4f9SPedro F. Giffuni  *
4bf3f9db6SUlrich Spörlein  * Copyright (c) 1997 Søren Schmidt
5b478da36SSøren Schmidt  * All rights reserved.
6b478da36SSøren Schmidt  *
7b478da36SSøren Schmidt  * Redistribution and use in source and binary forms, with or without
8b478da36SSøren Schmidt  * modification, are permitted provided that the following conditions
9b478da36SSøren Schmidt  * are met:
10b478da36SSøren Schmidt  * 1. Redistributions of source code must retain the above copyright
11b478da36SSøren Schmidt  *    notice, this list of conditions and the following disclaimer
12b478da36SSøren Schmidt  *    in this position and unchanged.
13b478da36SSøren Schmidt  * 2. Redistributions in binary form must reproduce the above copyright
14b478da36SSøren Schmidt  *    notice, this list of conditions and the following disclaimer in the
15b478da36SSøren Schmidt  *    documentation and/or other materials provided with the distribution.
16b478da36SSøren Schmidt  * 3. The name of the author may not be used to endorse or promote products
1721dc7d4fSJens Schweikhardt  *    derived from this software without specific prior written permission
18b478da36SSøren Schmidt  *
19b478da36SSøren Schmidt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20b478da36SSøren Schmidt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21b478da36SSøren Schmidt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22b478da36SSøren Schmidt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23b478da36SSøren Schmidt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24b478da36SSøren Schmidt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b478da36SSøren Schmidt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b478da36SSøren Schmidt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b478da36SSøren Schmidt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28b478da36SSøren Schmidt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b478da36SSøren Schmidt  */
30b478da36SSøren Schmidt 
31e67f5b9fSMatthew Dillon #include <sys/cdefs.h>
32b478da36SSøren Schmidt #include <stdio.h>
33b478da36SSøren Schmidt #include <sys/types.h>
34b478da36SSøren Schmidt #include <sys/ioctl.h>
35b478da36SSøren Schmidt #include <termios.h>
36b478da36SSøren Schmidt #include <sys/time.h>
3700d25f51SPoul-Henning Kamp #include <sys/fbio.h>
3800d25f51SPoul-Henning Kamp #include <sys/kbio.h>
39b478da36SSøren Schmidt #include "vgl.h"
40b478da36SSøren Schmidt 
41b478da36SSøren Schmidt static struct termios VGLKeyboardTty;
42b478da36SSøren Schmidt static int VGLKeyboardMode = -1;
43b478da36SSøren Schmidt 
44b478da36SSøren Schmidt int
VGLKeyboardInit(int mode)45b478da36SSøren Schmidt VGLKeyboardInit(int mode)
46b478da36SSøren Schmidt {
47b478da36SSøren Schmidt   static struct termios term;
48b478da36SSøren Schmidt 
49b478da36SSøren Schmidt   ioctl(0, KDGKBMODE, &VGLKeyboardMode);
50b478da36SSøren Schmidt   tcgetattr(0, &VGLKeyboardTty);
51b478da36SSøren Schmidt 
52b478da36SSøren Schmidt   term = VGLKeyboardTty;
535e7a62b2SKazutaka YOKOTA   cfmakeraw(&term);
54b478da36SSøren Schmidt   term.c_iflag = IGNPAR | IGNBRK;
555c9a541bSMaxim Sobolev   term.c_oflag = OPOST | ONLCR;
56b478da36SSøren Schmidt   term.c_cflag = CREAD | CS8;
57b478da36SSøren Schmidt   term.c_lflag &= ~(ICANON | ECHO | ISIG);
58b478da36SSøren Schmidt   term.c_cc[VTIME] = 0;
59b478da36SSøren Schmidt   term.c_cc[VMIN] = 0;
60b478da36SSøren Schmidt   cfsetispeed(&term, 9600);
61b478da36SSøren Schmidt   cfsetospeed(&term, 9600);
625c9a541bSMaxim Sobolev   tcsetattr(0, TCSANOW | TCSAFLUSH, &term);
63b478da36SSøren Schmidt 
64b478da36SSøren Schmidt   switch (mode) {
65b478da36SSøren Schmidt   case VGL_RAWKEYS:
66b478da36SSøren Schmidt     ioctl(0, KDSKBMODE, K_RAW);
67b478da36SSøren Schmidt     break;
68b478da36SSøren Schmidt   case VGL_CODEKEYS:
69b478da36SSøren Schmidt     ioctl(0, KDSKBMODE, K_CODE);
70b478da36SSøren Schmidt     break;
71b478da36SSøren Schmidt   case VGL_XLATEKEYS:
72b478da36SSøren Schmidt     ioctl(0, KDSKBMODE, K_XLATE);
73b478da36SSøren Schmidt     break;
74b478da36SSøren Schmidt   }
75b478da36SSøren Schmidt   return 0;
76b478da36SSøren Schmidt }
77b478da36SSøren Schmidt 
78b478da36SSøren Schmidt void
VGLKeyboardEnd()79b478da36SSøren Schmidt VGLKeyboardEnd()
80b478da36SSøren Schmidt {
815e7a62b2SKazutaka YOKOTA   if (VGLKeyboardMode != -1) {
82b478da36SSøren Schmidt     ioctl(0, KDSKBMODE, VGLKeyboardMode);
835c9a541bSMaxim Sobolev     tcsetattr(0, TCSANOW | TCSAFLUSH, &VGLKeyboardTty);
84b478da36SSøren Schmidt   }
855e7a62b2SKazutaka YOKOTA }
86b478da36SSøren Schmidt 
87b478da36SSøren Schmidt int
VGLKeyboardGetCh()88b478da36SSøren Schmidt VGLKeyboardGetCh()
89b478da36SSøren Schmidt {
90b478da36SSøren Schmidt   unsigned char ch = 0;
91b478da36SSøren Schmidt 
92b478da36SSøren Schmidt   read (0, &ch, 1);
93b478da36SSøren Schmidt   return (int)ch;
94b478da36SSøren Schmidt }
95