summaryrefslogtreecommitdiff
path: root/console-client/kbd-repeat.c
diff options
context:
space:
mode:
authorMarco Gerards <marco@gnu.org>2005-01-06 21:43:53 +0000
committerMarco Gerards <marco@gnu.org>2005-01-06 21:43:53 +0000
commit38fc4e4d1cef98a8b3d60b717647a9c65d91307f (patch)
tree805792e935ebbf353301070209d0af1aea1d908f /console-client/kbd-repeat.c
parent4456094d61a6f5baabadc18096fc1f516921b736 (diff)
2005-01-06 Marco Gerards <metgerards@student.han.nl>
* Makefile (SRCS): Add `trans.c'. (LCLHDRS): Add `mach-inputdev.h'. (HURDLIBS): Add `netfs', `fshelp' and `iohelp'. (modules): Add `pc_mouse'. (pc_kbd.so.$(hurd-version)): Add `kdioctlServer.o' and `kbd-repeat.c'. (pc_mouse.so.$(hurd-version)): New variable. * console.c: Include <trans.h>. (DEFAULT_CONSOLE_NODE): New macro. (saved_id, saved_cons, consnode_path): New variables. (console_move_mouse): New function. (console_switch_away): New function. (console_switch_back): Likewise. (cons_vcons_set_mousecursor_pos): Likewise. (cons_vcons_set_mousecursor_status): Likewise. (options): Add the option `--console-node'. (parse_opt): Parse the options that were added to `options'. (main): Setup the console client translator node. * display.h (display_ops): New members `set_mousecursor_pos' and `set_mousecursor_status'. * driver.c (driver_start): Change the type of `i' to `unsigned int'. * driver.h (driver_ops): New members `save_status' and `restore_status'. * input.h (console_switch_away): New prototype. (console_switch_back): Likewise. (console_move_mouse): Likewise. * kbd-repeat.c: New file. * mach-inputdev.h: Likewise. * pc-mouse.c: Likewise. * trans.c: Likewise. * trans.h: Likewise. * pc-kbd.c: Include <argp.h> and "mach-inputdev.h". (DEFAULT_REPEATER_NODE): New macro. (repeater_node, cnode): New variables. (kev_type, mouse_motion, Scancode, m_deltaX, m_deltaY, MOUSE_LEFT) (MOUSE_MIDDLE, MOUSE_RIGHT, MOUSE_MOTION, KEYBD_EVENT) (IOCPARM_MASK, IOC_OUT, IOC_IN, _IOC, _IOR, _IOW, KDSKBDMODE, (KB_EVENT, KB_ASCII, KDGKBDTYPE, KB_VANILLAKB, KDSETLEDS): Removed. (gnumach_v1_input_next): Call the repeater when repeating is active. (doc, options, argp): New variables. (parse_opt): New function. (pc_kbd_init): Function rewritten. (pc_kbd_start): Initialize the repeater, when it is active. (pc_kbd_fini): Destroy the console node. * vga.c (vga_mousecursor): New struct. (vga_mousecursor_t): New type. (mousecursor): New variable. (hide_mousecursor): New function. (draw_mousecursor): Likewise. (vga_display_restore_status): Likewise. (vga_display_update): Likewise. (vga_set_mousecursor_pos): Likewise. (vga_set_mousecursor_status): Likewise. (vga_display_scroll): Update the mousecursor state. (driver_vga_ops): Add `vga_display_restore_status'. (vga_display_op): Add `vga_display_update', `vga_set_mousecursor_pos' and `vga_set_mousecursor_status'.
Diffstat (limited to 'console-client/kbd-repeat.c')
-rw-r--r--console-client/kbd-repeat.c258
1 files changed, 258 insertions, 0 deletions
diff --git a/console-client/kbd-repeat.c b/console-client/kbd-repeat.c
new file mode 100644
index 00000000..4bf9268e
--- /dev/null
+++ b/console-client/kbd-repeat.c
@@ -0,0 +1,258 @@
+/* kbd-repeat.c - Keyboard repeater.
+ Copyright (C) 2004, 2005 Free Software Foundation, Inc.
+ Written by Marco Gerards.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include <hurd/netfs.h>
+#include <stdlib.h>
+#include <error.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+
+#include "kdioctl_S.h"
+#include "mach-inputdev.h"
+#include "input.h"
+
+/* The amount of keyboard events that can be stored in the keyboard buffer. */
+#define KBDEVTBUFSZ 20
+
+/* The size of the keyboard buffer in bytes. */
+#define KBDBUFSZ (KBDEVTBUFSZ * sizeof (kd_event))
+
+/* Return the position of X in the buffer. */
+#define KBDBUF_POS(x) ((x) % KBDBUFSZ)
+
+/* The keyboard buffer. */
+static struct kbdbuf
+{
+ char keybuffer[KBDBUFSZ];
+ int pos;
+ size_t size;
+ struct condition readcond;
+ struct condition writecond;
+} kbdbuf;
+
+/* Wakeup for select */
+static struct condition select_alert;
+
+/* The global lock */
+static struct mutex global_lock;
+
+/* Amount of times the device was opened. Normally this translator
+ should be only opened once. */
+int kbd_repeater_opened;
+
+
+/* Place the keyboard event KEY in the keyboard buffer. */
+void
+kbd_repeat_key (kd_event *key)
+{
+ kd_event *ev;
+
+ mutex_lock (&global_lock);
+ while (kbdbuf.size + sizeof (kd_event) > KBDBUFSZ)
+ {
+ /* The input buffer is full, wait until there is some space. */
+ if (hurd_condition_wait (&kbdbuf.writecond, &global_lock))
+ {
+ mutex_unlock (&global_lock);
+ /* Interrupt, silently continue. */
+ }
+ }
+ ev = (kd_event *) &kbdbuf.keybuffer[KBDBUF_POS (kbdbuf.pos
+ + kbdbuf.size)];
+ kbdbuf.size += sizeof (kd_event);
+ memcpy (ev, key, sizeof (kd_event));
+
+ condition_broadcast (&kbdbuf.readcond);
+ mutex_unlock (&global_lock);
+}
+
+
+static error_t
+repeater_select (struct protid *cred, mach_port_t reply,
+ mach_msg_type_name_t replytype, int *type)
+{
+ if (!cred)
+ return EOPNOTSUPP;
+
+ if (*type & ~SELECT_READ)
+ return EINVAL;
+
+ if (*type == 0)
+ return 0;
+
+ mutex_lock (&global_lock);
+ while (1)
+ {
+ if (kbdbuf.size > 0)
+ {
+ *type = SELECT_READ;
+ mutex_unlock (&global_lock);
+
+ return 0;
+ }
+
+ ports_interrupt_self_on_port_death (cred, reply);
+ if (hurd_condition_wait (&select_alert, &global_lock))
+ {
+ *type = 0;
+ mutex_unlock (&global_lock);
+
+ return EINTR;
+ }
+ }
+}
+
+
+static error_t
+repeater_read (struct protid *cred, char **data,
+ mach_msg_type_number_t *datalen, off_t offset,
+ mach_msg_type_number_t amount)
+{
+ /* Deny access if they have bad credentials. */
+ if (! cred)
+ return EOPNOTSUPP;
+ else if (! (cred->po->openstat & O_READ))
+ return EBADF;
+
+ mutex_lock (&global_lock);
+ while (amount > kbdbuf.size)
+ {
+ if (cred->po->openstat & O_NONBLOCK && amount > kbdbuf.size)
+ {
+ mutex_unlock (&global_lock);
+ return EWOULDBLOCK;
+ }
+
+ if (hurd_condition_wait (&kbdbuf.readcond, &global_lock))
+ {
+ mutex_unlock (&global_lock);
+ return EINTR;
+ }
+ }
+
+ if (amount > 0)
+ {
+ char *keydata;
+ unsigned int i = 0;
+
+ /* Allocate a buffer when this is required. */
+ if (*datalen < amount)
+ {
+ *data = mmap (0, amount, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
+ if (*data == MAP_FAILED)
+ {
+ mutex_unlock (&global_lock);
+ return ENOMEM;
+ }
+ }
+
+ /* Copy the bytes to the user's buffer and remove them from the
+ keyboard buffer. */
+ keydata = *data;
+ while (i != amount)
+ {
+ keydata[i++] = kbdbuf.keybuffer[kbdbuf.pos++];
+ kbdbuf.pos = KBDBUF_POS (kbdbuf.pos);
+ }
+ kbdbuf.size -= amount;
+ condition_broadcast (&kbdbuf.writecond);
+ }
+
+ *datalen = amount;
+ mutex_unlock (&global_lock);
+
+ return 0;
+}
+
+
+static void
+repeater_open (void)
+{
+ /* Make sure the console does not access the hardware anymore. */
+ if (! kbd_repeater_opened)
+ console_switch_away ();
+ kbd_repeater_opened++;
+}
+
+
+static void
+repeater_close (void)
+{
+ kbd_repeater_opened--;
+
+ /* Allow the console to access the hardware again. */
+ if (! kbd_repeater_opened)
+ {
+ console_switch_back ();
+ kbdbuf.pos = 0;
+ kbdbuf.size = 0;
+ }
+}
+
+
+/* Set the repeater translator. The node will be named NODENAME and
+ NODE will be filled with information about this node. */
+error_t
+kbd_setrepeater (const char *nodename, consnode_t *cn)
+{
+ extern int kdioctl_server (mach_msg_header_t *inp, mach_msg_header_t *outp);
+ error_t err;
+
+ err = console_create_consnode (nodename, cn);
+ if (err)
+ return err;
+
+ (*cn)->read = repeater_read;
+ (*cn)->write = 0;
+ (*cn)->select = repeater_select;
+ (*cn)->open = repeater_open;
+ (*cn)->close = repeater_close;
+ (*cn)->demuxer = kdioctl_server;
+
+ mutex_init (&global_lock);
+
+ condition_init (&kbdbuf.readcond);
+ condition_init (&kbdbuf.writecond);
+
+ condition_init (&select_alert);
+ condition_implies (&kbdbuf.readcond, &select_alert);
+
+ console_register_consnode (*cn);
+
+ return 0;
+}
+
+
+/* Some RPC calls for controlling the keyboard. These calls are just
+ ignored and just exist to make XFree happy. */
+
+kern_return_t
+S_kdioctl_kdskbdmode (io_t port, int mode)
+{
+ return 0;
+}
+
+
+kern_return_t
+S_kdioctl_kdgkbdmode (io_t port, int *mode)
+{
+ return 0;
+}