summaryrefslogtreecommitdiff
path: root/console-client/xkb/xkbtimer.c
blob: 7621af7299f983ea9e85d436ce63cc1a25444917 (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
/*  xkbtimer.c -- Manage XKB timers.

    Copyright (C) 2003, 2004  Marco Gerards
   
    Written by Marco Gerards <marco@student.han.nl>
    
    This program 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 of the License, or
    (at your option) any later version.
  
    This program 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.  */

/* XKB requires a timer for key repeat and accessibility. Carefully
   stack all compatibility features.  */

#include <mach.h>
#include <errno.h>
#include <assert.h>

#include "xkb.h"
#include <timer.h>

/* For key repeat.  */
static int key_delay = 0;
static int key_repeat = 0;

/* SlowKeys.  */
static int slowkeys_delay = 50;
static int slowkeys_active = 0;

/* BounceKeys.  */
static int bouncekeys_delay = 100;
static int bouncekeys_active = 0;

/* The current status of the timer.  */
enum timer_status
  {
    timer_stopped,
    timer_slowkeys,
    timer_repeat_delay,
    timer_repeating
  };

/* Timer used to time key controls.  */
static struct per_key_timer
{
  /* Used for slowkeys and repeat.  */
  struct timer_list enable_timer;
  
  /* Status of the enable timer.  */
  enum timer_status enable_status;
  
  /* Used for bouncekeys.  */
  struct timer_list disable_timer;
} per_key_timers[255];

/* The last pressed key. Only this key may generate keyrepeat events.  */
static int lastkey = 0;

error_t
xkb_handle_key (keycode_t kc)
{
  static keycode_t prevkc = 0;
  keypress_t key;
  
  key.keycode = kc & 127;
  key.prevkc = prevkc;
  key.repeat = (prevkc == kc);
  key.redir = 0;
  key.rel = kc & 128;
  keystate[key.keycode & 127].keypressed = key.rel ? 0 : 1;
  debug_printf ("PRESSED: %d\n", !(key.rel));
  xkb_input (key);
  prevkc = key.keycode;
  return 0;
}

error_t
xkb_init_repeat (int delay, int repeat)
{
  error_t err;
  err = timer_init ();
  if (err)
    return err;

  key_delay = delay;
  key_repeat = repeat;
  
  return 0;
}

static int
key_enable (void *handle)
{
  int key = (int) handle;
  
  /* Enable the key.  */
  keystate[key].disabled = 0;
  
  return 0;
}

/* Called by key timer. The global variable timer_status determines
   the current control.  */
static int
key_timing (void *handle)
{
  int current_key = (int) handle;

  xkb_handle_key (current_key);

  /* Another key was pressed after this key, stop repeating.  */
  if (lastkey != current_key)
    {
      per_key_timers[current_key].enable_status = timer_stopped;
      return 0;
    }

  switch (per_key_timers[current_key].enable_status)
    {
    case timer_stopped:
      assert ("Stopped timer triggered timer event\n");
      break;
    case timer_slowkeys:
      per_key_timers[current_key].enable_timer.expires
	= fetch_jiffies () + key_delay;
      lastkey = current_key;
      
      if (keys[current_key].flags & KEYNOREPEAT)
	{
	  per_key_timers[current_key].enable_status = timer_stopped;
	  /* Stop the timer.  */
	  return 0;
	}
      else
	{
	  per_key_timers[current_key].enable_status = timer_repeat_delay;
	}
      break;
    case timer_repeat_delay:
      per_key_timers[current_key].enable_status = timer_repeating;
      /* Fall through.  */
    case timer_repeating:
      per_key_timers[current_key].enable_timer.expires 
	= fetch_jiffies () + key_repeat;
      break;
    }
  return 1;
}

error_t
xkb_input_key (int key)
{
  int pressed = !(key & 128);
  int keyc = key & 127;

  debug_printf ("KEYIN: %d\n", key);

  /* Filter out any double or disabled keys.  */
  if (key == lastkey || keystate[keyc].disabled)
    return 0;

  /* Always handle keyrelease events.  */
  if (!pressed)
    {
      /* Stop the timer for this released key.  */
      if (per_key_timers[keyc].enable_status != timer_stopped)
	{
	  timer_remove (&per_key_timers[keyc].enable_timer);
	  per_key_timers[keyc].enable_status = timer_stopped;
	}

      /* No more last key; it was released.  */
      if (keyc == lastkey)
	lastkey = 0;

      /* Make sure the key was pressed before releasing it, it might
	 not have been accepted.  */
      if (keystate[key & 127].keypressed)
	xkb_handle_key (key);

      /* If bouncekeys is active, disable the key.  */
      if (bouncekeys_active)
	{
	  keystate[keyc].disabled = 1;
	    
	  /* Setup a timer to enable the key.  */
	  timer_clear (&per_key_timers[keyc].disable_timer);
	  per_key_timers[keyc].disable_timer.fnc = key_enable;
	  per_key_timers[keyc].disable_timer.fnc_data = (void *) keyc;
	  per_key_timers[keyc].disable_timer.expires
	    = fetch_jiffies () + bouncekeys_delay;
	  timer_add (&per_key_timers[keyc].disable_timer);
	}

      return 0;
    }

  /* Setup the timer for slowkeys.  */
  timer_clear (&per_key_timers[keyc].enable_timer);
  lastkey = keyc;
  per_key_timers[keyc].enable_timer.fnc = key_timing;
  per_key_timers[keyc].enable_timer.fnc_data = (void *) keyc;

  if (slowkeys_active)
    {
      per_key_timers[keyc].enable_status = timer_slowkeys;
      per_key_timers[keyc].enable_timer.expires
	= fetch_jiffies () + slowkeys_delay;
    }
  else
    {
      /* Immediatly report the keypress.  */
      xkb_handle_key (keyc);

      /* Check if this repeat is allowed for this keycode.  */
      if (keys[keyc].flags & KEYNOREPEAT)
	return 0; /* Nope.  */

      per_key_timers[keyc].enable_status = timer_repeat_delay;
      per_key_timers[keyc].enable_timer.expires
	= fetch_jiffies () + key_delay;
    }
  timer_add (&per_key_timers[keyc].enable_timer);

  return 0;
}