summaryrefslogtreecommitdiff
path: root/hw/xfree86/os-support/usl/usl_kbd.c
blob: 5f96fa676a87a0d77389ba01f9edf1943927564e (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/* $XdotOrg: xserver/xorg/hw/xfree86/os-support/usl/usl_kbd.c,v 1.2 2005/11/08 06:33:30 jkj Exp $ */
/*
 * Copyright 2005 by Kean Johnston <jkj@sco.com>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name Kean Johnston not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Kean Johnston makes no
 * representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 *
 * KEAN JOHNSTON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEAN JOHNSTON BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */
/* $XConsortium$ */

/*
 * Based on sco_io.c which is
 * (C) Copyright 2003 Kean Johnston <jkj@sco.com>
 *
 * Based on lnx_kbd.c which is 
 * Copyright (c) 2002 by The XFree86 Project, Inc.
 *
 * Based on the code from lnx_io.c which is
 * Copyright 1992 by Orest Zborowski <obz@Kodak.com>
 * Copyright 1993 by David Dawes <dawes@xfree86.org>
 */

#define NEED_EVENTS
#include "X.h"

#include "compiler.h"

#define _NEED_SYSI86
#include "xf86.h"
#include "xf86Priv.h"
#include "xf86OSpriv.h"
#include "xf86_OSlib.h"

#include "xf86Xinput.h"
#include "xf86OSKbd.h"
#include "atKeynames.h"
#include "usl_kbd.h"
#include "usl_xqueue.h"

#include <sys/param.h>

static KbdProtocolRec protocols[] = {
  { "standard", PROT_STD },
  { "Xqueue", PROT_XQUEUE },
  { NULL, PROT_UNKNOWN_KBD }
};

extern Bool VTSwitchEnabled;
#ifdef USE_VT_SYSREQ
extern Bool VTSysreqToggle;
#endif

static void
SoundBell(InputInfoPtr pInfo, int loudness, int pitch, int duration)
{
  if (loudness && pitch) {
    ioctl(pInfo->fd, KIOCSOUND, 1193180 / pitch);
    usleep(duration * loudness * 20);
    ioctl(pInfo->fd, KIOCSOUND, 0);
  }
}

static void
SetKbdLeds(InputInfoPtr pInfo, int leds)
{
  int real_leds = 0;

  if (leds & XLED1)
    real_leds |= LED_CAP;
  if (leds & XLED2)
    real_leds |= LED_NUM;
  if (leds & XLED3)
    real_leds |= LED_SCR;
  ioctl(pInfo->fd, KDSETLED, real_leds);
}

static int
GetKbdLeds(InputInfoPtr pInfo)
{
    int real_leds, leds = 0;

    ioctl(pInfo->fd, KDGETLED, &real_leds);

    if (real_leds & LED_CAP) leds |= XLED1;
    if (real_leds & LED_NUM) leds |= XLED2;
    if (real_leds & LED_SCR) leds |= XLED3;

    return(leds);
}

static void
SetKbdRepeat(InputInfoPtr pInfo, char rad)
{
  KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
  int i;
  int value = 0x7f;     /* Maximum delay with slowest rate */
  int delay = 250;      /* Default delay */
  int rate = 300;       /* Default repeat rate */

  static int valid_rates[] = { 300, 267, 240, 218, 200, 185, 171, 160, 150,
                               133, 120, 109, 100, 92, 86, 80, 75, 67,
                               60, 55, 50, 46, 43, 40, 37, 33, 30, 27,
                               25, 23, 21, 20 };
#define RATE_COUNT (sizeof( valid_rates ) / sizeof( int ))

  static int valid_delays[] = { 250, 500, 750, 1000 };
#define DELAY_COUNT (sizeof( valid_delays ) / sizeof( int ))

  if (pKbd->rate >= 0) 
    rate = pKbd->rate * 10;
  if (pKbd->delay >= 0)
    delay = pKbd->delay;

  for (i = 0; i < RATE_COUNT; i++)
    if (rate >= valid_rates[i]) {
      value &= 0x60;
      value |= i;
      break;
    }

  for (i = 0; i < DELAY_COUNT; i++)
    if (delay <= valid_delays[i]) {
      value &= 0x1f;
      value |= i << 5;
      break;
    }

  ioctl (pInfo->fd, KDSETTYPEMATICS, value);
}

static int
KbdInit(InputInfoPtr pInfo, int what)
{
  KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
  USLKbdPrivPtr priv = (USLKbdPrivPtr) pKbd->private;

  if (pKbd->isConsole) {
    if (ioctl (pInfo->fd, KDGKBMODE, &priv->orig_kbm) < 0) {
      xf86Msg (X_ERROR, "KbdInit: Could not determine keyboard mode\n");
      return !Success;
    }

    /*
     * We need to get the original keyboard map and NUL out the lock
     * modifiers. This prevents the kernel from messing with
     * the keyboard LED's. We restore the original map when we exit.
     * Note that we also have to eliminate screen switch sequences
     * else the VT manager will switch for us, which we don't want.
     * For example, lets say you had changed the VT manager to switch
     * on Alt-Fx instead of Ctrl-Alt-Fx. This means that while inside
     * X, you cant use, for example, Alt-F4, which is a pain in the
     * fundamental when you're using CDE-like thingies.
     */
    if (ioctl (pInfo->fd, GIO_KEYMAP, &priv->keymap) < 0) {
      xf86Msg (X_ERROR, "KbdInit: Failed to get keyboard map (%s)\n",
        strerror(errno));
      return !Success;
    }
    if (ioctl (pInfo->fd, GIO_KEYMAP, &priv->noledmap) < 0) {
      xf86Msg (X_ERROR, "KbdInit: Failed to get keyboard map (%s)\n",
        strerror(errno));
      return !Success;
    } else {
      int i, j;

      for (i = 0; i < priv->noledmap.n_keys; i++) {
        for (j = 0; j < NUM_STATES; j++) {
          if (IS_SPECKEY(&priv->noledmap, i, j) &&
            ((priv->noledmap.key[i].map[j] == K_CLK) ||
             (priv->noledmap.key[i].map[j] == K_NLK) ||
             (priv->noledmap.key[i].map[j] == K_SLK) ||
             (priv->noledmap.key[i].map[j] == K_FRCNEXT) ||
             (priv->noledmap.key[i].map[j] == K_FRCPREV) ||
             ((priv->noledmap.key[i].map[j] >=  K_VTF) &&
	      (priv->noledmap.key[i].map[j] <= K_VTL)) )) {
            priv->noledmap.key[i].map[j] = K_NOP;
          }
        }
      }
    }

    if (ioctl (pInfo->fd, TCGETA, &priv->kbdtty) < 0) {
      xf86Msg (X_ERROR, "KbdInit: Failed to get terminal modes (%s)\n",
        strerror(errno));
      return !Success;
    }
  } /* End of if we are on a console */

  return Success;
}

static int
KbdOn(InputInfoPtr pInfo, int what)
{
  KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
  USLKbdPrivPtr priv = (USLKbdPrivPtr) pKbd->private;
  struct termio newtio;

  if (pKbd->isConsole) {
    /*
     * Use the calculated keyboard map that does not have active
     * LED lock handling (we track LEDs ourselves).
     */
    ioctl (pInfo->fd, PIO_KEYMAP, &priv->noledmap);

#ifdef NOTYET
    newtio = priv->kbdtty;     /* structure copy */
    newtio.c_iflag = (IGNPAR | IGNBRK) & (~PARMRK) & (~ISTRIP);
    newtio.c_oflag = 0;
    newtio.c_cflag = CREAD | CS8 | B9600;
    newtio.c_lflag = 0;
    newtio.c_cc[VTIME]=0;
    newtio.c_cc[VMIN]=1;
    ioctl(pInfo->fd, TCSETA, &newtio);

    if (priv->xq == 0)
      ioctl (pInfo->fd, KDSKBMODE, K_RAW);
    else
#endif
      XqKbdOnOff (pInfo, 1);
  }

  return Success;
}

static int
KbdOff(InputInfoPtr pInfo, int what)
{
  KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
  USLKbdPrivPtr priv = (USLKbdPrivPtr) pKbd->private;

  if (pKbd->isConsole) {
    /* Revert back to original translate scancode mode */
#ifdef NOTYET
    if (priv->xq == 0)
      ioctl (pInfo->fd, KDSKBMODE, priv->orig_kbm);
    else
#endif
      XqKbdOnOff (pInfo, 0);

    ioctl (pInfo->fd, PIO_KEYMAP, &priv->keymap);
    ioctl(pInfo->fd, TCSETA, &priv->kbdtty);
  }

  return Success;
}

#define ModifierSet(k) ((modifiers & (k)) == (k))

static Bool
SpecialKey(InputInfoPtr pInfo, int key, Bool down, int modifiers)
{
  KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;

  if(!pKbd->vtSwitchSupported)
    return FALSE;

  if ((!ModifierSet(ShiftMask)) && ((ModifierSet(ControlMask | AltMask)) ||
      (ModifierSet(ControlMask | AltLangMask)))) {
    if (VTSwitchEnabled && !xf86Info.vtSysreq) {
      switch (key) {
        case KEY_F1:
        case KEY_F2:
        case KEY_F3:
        case KEY_F4:
        case KEY_F5:
        case KEY_F6:
        case KEY_F7:
        case KEY_F8:
        case KEY_F9:
        case KEY_F10:
          if (down) {
	    int sts = key - KEY_F1;
	    if (sts != xf86Info.vtno) {
	      ioctl(pInfo->fd, VT_SWITCH, sts);
	    }
            return TRUE;
          }
        case KEY_F11:
        case KEY_F12:
          if (down) {
	    int sts = key - KEY_F11 + 10;
	    if (sts != xf86Info.vtno) {
	      ioctl(pInfo->fd, VT_SWITCH, sts);
	    }
            return TRUE;
          }
      }
    }
  }
#ifdef USE_VT_SYSREQ
  if (VTSwitchEnabled && xf86Info.vtSysreq) {
    switch (key) {
      case KEY_F1:
      case KEY_F2:
      case KEY_F3:
      case KEY_F4:
      case KEY_F5:
      case KEY_F6:
      case KEY_F7:
      case KEY_F8:
      case KEY_F9:
      case KEY_F10:
        if (VTSysreqToggle && down) {
          ioctl(pInfo->fd, VT_ACTIVATE, key - KEY_F1);
          VTSysreqToggle = FALSE;
          return TRUE;
        }
        break;
      case KEY_F11:
      case KEY_F12:
        if (VTSysreqToggle && down) {
          ioctl(pInfo->fd, VT_ACTIVATE, key - KEY_F11 + 10);
          VTSysreqToggle = FALSE;
          return TRUE;
        }
        break;
        /* Ignore these keys -- ie don't let them cancel an alt-sysreq */
      case KEY_Alt:
      case KEY_AltLang:
        break;
      case KEY_SysReqest:
        if (!(ModifierSet(ShiftMask) || ModifierSet(ControlMask))) {
          if ((ModifierSet(AltMask) || ModifierSet(AltLangMask)) && down)
            VTSysreqToggle = TRUE;
        }
        break;
      default:
        /*
         * We only land here when Alt-SysReq is followed by a
         * non-switching key.
         */
        if (VTSysreqToggle)
          VTSysreqToggle = FALSE;
    }
  }
#endif /* USE_VT_SYSREQ */
  return FALSE;
} 

#ifdef NOTYET
static void
stdReadInput(InputInfoPtr pInfo)
{
  KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
  unsigned char rBuf[64];
  int nBytes, i;

  if ((nBytes = read( pInfo->fd, (char *)rBuf, sizeof(rBuf))) > 0) {
    for (i = 0; i < nBytes; i++) {
      pKbd->PostEvent(pInfo, rBuf[i] & 0x7f, rBuf[i] & 0x80 ? FALSE : TRUE);
    }
  }
}
#endif

static Bool
OpenKeyboard(InputInfoPtr pInfo)
{
  KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
  USLKbdPrivPtr priv = (USLKbdPrivPtr) pKbd->private;
  int i;
  KbdProtocolId prot = PROT_UNKNOWN_KBD;
  char *s;

  s = xf86SetStrOption(pInfo->options, "Protocol", NULL);
  for (i = 0; protocols[i].name; i++) {
    if (xf86NameCmp(s, protocols[i].name) == 0) {
      prot = protocols[i].id;
      break;
    }
  }

  switch (prot) {
    case PROT_STD:
#ifdef NOTYET
      pInfo->read_input = stdReadInput;
      priv->xq = 0;
      break;
#endif
    case PROT_XQUEUE:
      pInfo->read_input = NULL;	/* Handled by the XQUEUE signal handler */
      priv->xq = 1;
      break;
    default:
      xf86Msg(X_ERROR,"\"%s\" is not a valid keyboard protocol name\n", s);
      xfree(s);
      return FALSE;
  }

  xf86Msg(X_CONFIG, "%s: Protocol: %s\n", pInfo->name, s);
  xfree(s);

  s = xf86SetStrOption(pInfo->options, "Device", NULL);
  if (s == NULL) {
    pInfo->fd = xf86Info.consoleFd;
    pKbd->isConsole = TRUE;
  } else {
    pInfo->fd = open(s, O_RDONLY | O_NONBLOCK | O_EXCL);
    if (pInfo->fd == -1) {
      xf86Msg(X_ERROR, "%s: cannot open \"%s\"\n", pInfo->name, s);
      xfree(s);
      return FALSE;
    }
    pKbd->isConsole = FALSE;
    xfree(s);
  }

  if (pKbd->isConsole)
    pKbd->vtSwitchSupported = TRUE;

  return TRUE;
}

_X_EXPORT Bool
xf86OSKbdPreInit(InputInfoPtr pInfo)
{
  KbdDevPtr pKbd = pInfo->private;

  pKbd->KbdInit       = KbdInit;
  pKbd->KbdOn         = KbdOn;
  pKbd->KbdOff        = KbdOff;
  pKbd->Bell          = SoundBell;
  pKbd->SetLeds       = SetKbdLeds;
  pKbd->GetLeds       = GetKbdLeds;
  pKbd->SetKbdRepeat  = SetKbdRepeat;
  pKbd->KbdGetMapping = KbdGetMapping;
  pKbd->SpecialKey    = SpecialKey;
  pKbd->OpenKeyboard  = OpenKeyboard;

  pKbd->GetSpecialKey = NULL;
  pKbd->RemapScanCode = ATScancode;
  pKbd->vtSwitchSupported = FALSE;

  pKbd->private = xcalloc(sizeof(USLKbdPrivRec), 1);
  if (pKbd->private == NULL) {
    xf86Msg(X_ERROR,"can't allocate keyboard OS private data\n");
    return FALSE;
  }

  return TRUE;
}