summaryrefslogtreecommitdiff
path: root/console-client/kbd-repeat.c
blob: e45902d73a8307ca301ea775734f827478d46fbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* 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 <pthread.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;
  pthread_cond_t readcond;
  pthread_cond_t writecond;
} kbdbuf;

/* Wakeup for select */
static pthread_cond_t select_alert;

/* The global lock */
static pthread_mutex_t 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;

  pthread_mutex_lock (&global_lock);
  while (kbdbuf.size + sizeof (kd_event) > KBDBUFSZ)
    {
      /* The input buffer is full, wait until there is some space. If this call
       * is interrupted, silently continue. */
      (void) pthread_hurd_cond_wait_np (&kbdbuf.writecond, &global_lock);
    }
  ev = (kd_event *) &kbdbuf.keybuffer[KBDBUF_POS (kbdbuf.pos 
						  + kbdbuf.size)];
  kbdbuf.size += sizeof (kd_event);
  memcpy (ev, key, sizeof (kd_event));
  
  pthread_cond_broadcast (&kbdbuf.readcond);
  pthread_cond_broadcast (&select_alert);
  pthread_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;

  pthread_mutex_lock (&global_lock);
  while (1)
    {
      if (kbdbuf.size > 0)
	{
	  *type = SELECT_READ;
	  pthread_mutex_unlock (&global_lock);

	  return 0;
	}
      
      ports_interrupt_self_on_port_death (cred, reply);
      if (pthread_hurd_cond_wait_np (&select_alert, &global_lock))
	{
	  *type = 0;
	  pthread_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;
  
  pthread_mutex_lock (&global_lock);
  while (amount > kbdbuf.size)
    {
      if (cred->po->openstat & O_NONBLOCK && amount > kbdbuf.size)
	{
	  pthread_mutex_unlock (&global_lock);
	  return EWOULDBLOCK;
	}
      
      if (pthread_hurd_cond_wait_np (&kbdbuf.readcond, &global_lock))
	{
	  pthread_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)
	    {
	      pthread_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;
      pthread_cond_broadcast (&kbdbuf.writecond);
    }
  
  *datalen = amount;
  pthread_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;
  
  pthread_mutex_init (&global_lock, NULL);

  pthread_cond_init (&kbdbuf.readcond, NULL);
  pthread_cond_init (&kbdbuf.writecond, NULL);
  pthread_cond_init (&select_alert, NULL);
  
  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;
}