bsd_kbd.c revision 632d17a8
1
2/*
3 * Copyright (c) 2002 by The XFree86 Project, Inc.
4 * Author: Ivan Pascal.
5 *
6 * Based on the code from bsd_io.c which is
7 * Copyright 1992 by Rich Murphey <Rich@Rice.edu>
8 * Copyright 1993 by David Dawes <dawes@xfree86.org>
9 */
10
11#ifdef HAVE_CONFIG_H
12#include "config.h"
13#endif
14
15#include <xorg-server.h>
16#include <X11/X.h>
17#include <termios.h>
18
19#include "compiler.h"
20
21#include "xf86.h"
22#include "xf86Priv.h"
23#include "xf86_OSlib.h"
24
25#include "xf86Xinput.h"
26#include "xf86OSKbd.h"
27#include "atKeynames.h"
28
29extern void KbdGetMapping(InputInfoPtr pInfo, KeySymsPtr pKeySyms,
30                          CARD8 *pModMap);
31
32extern Bool VTSwitchEnabled;
33
34static KbdProtocolRec protocols[] = {
35   {"standard", PROT_STD },
36#ifdef WSCONS_SUPPORT
37   {"wskbd", PROT_WSCONS },
38#endif
39   { NULL, PROT_UNKNOWN_KBD }
40};
41
42typedef struct {
43   struct termios kbdtty;
44} BsdKbdPrivRec, *BsdKbdPrivPtr;
45
46static
47int KbdInit(InputInfoPtr pInfo, int what)
48{
49    KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
50    BsdKbdPrivPtr priv = (BsdKbdPrivPtr) pKbd->private;
51
52    if (pKbd->isConsole) {
53        switch (pKbd->consType) {
54#if defined(PCCONS_SUPPORT) || defined(SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)  || defined (WSCONS_SUPPORT)
55	    case PCCONS:
56	    case SYSCONS:
57	    case PCVT:
58#if defined WSCONS_SUPPORT
59            case WSCONS:
60#endif
61 	         tcgetattr(pInfo->fd, &(priv->kbdtty));
62#endif
63	         break;
64        }
65    }
66
67    return Success;
68}
69
70static void
71SetKbdLeds(InputInfoPtr pInfo, int leds)
72{
73    KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
74    int real_leds = 0;
75
76#ifdef LED_CAP
77    if (leds & XLED1)  real_leds |= LED_CAP;
78#endif
79#ifdef LED_NUM
80    if (leds & XLED2)  real_leds |= LED_NUM;
81#endif
82#ifdef LED_SCR
83    if (leds & XLED3)  real_leds |= LED_SCR;
84    if (leds & XLED4)  real_leds |= LED_SCR;
85#endif
86
87    switch (pKbd->consType) {
88
89#ifdef PCCONS_SUPPORT
90	case PCCONS:
91		break;
92#endif
93#if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
94	case SYSCONS:
95	case PCVT:
96	     ioctl(pInfo->fd, KDSETLED, real_leds);
97	     break;
98#endif
99#if defined(WSCONS_SUPPORT)
100        case WSCONS:
101             ioctl(pInfo->fd, WSKBDIO_SETLEDS, &real_leds);
102             break;
103#endif
104    }
105}
106
107static int
108GetKbdLeds(InputInfoPtr pInfo)
109{
110    KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
111    int leds = 0, real_leds = 0;
112
113    switch (pKbd->consType) {
114
115#ifdef PCCONS_SUPPORT
116	case PCCONS:
117	     break;
118#endif
119#if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
120	case SYSCONS:
121	case PCVT:
122	     ioctl(pInfo->fd, KDGETLED, &real_leds);
123	     break;
124#endif
125#if defined(WSCONS_SUPPORT)
126        case WSCONS:
127             ioctl(pInfo->fd, WSKBDIO_GETLEDS, &real_leds);
128             break;
129#endif
130    }
131
132#ifdef LED_CAP
133    if (real_leds & LED_CAP) leds |= XLED1;
134#endif
135#ifdef LED_NUM
136    if (real_leds & LED_NUM) leds |= XLED2;
137#endif
138#ifdef LED_SCR
139    if (real_leds & LED_SCR) leds |= XLED3;
140#endif
141
142    return(leds);
143}
144
145static void
146SetKbdRepeat(InputInfoPtr pInfo, char rad)
147{
148    KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
149    switch (pKbd->consType) {
150
151#ifdef PCCONS_SUPPORT
152	case PCCONS:
153		break;
154#endif
155#if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
156	case SYSCONS:
157	case PCVT:
158		ioctl(pInfo->fd, KDSETRAD, rad);
159		break;
160#endif
161    }
162}
163
164static int
165KbdOn(InputInfoPtr pInfo, int what)
166{
167    KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
168#if defined(SYSCONS_SUPPORT) || defined(PCCONS_SUPPORT) || defined(PCVT_SUPPORT) || defined(WSCONS_SUPPORT)
169    BsdKbdPrivPtr priv = (BsdKbdPrivPtr) pKbd->private;
170    struct termios nTty;
171#endif
172#ifdef WSCONS_SUPPORT
173    int option;
174#endif
175
176    if (pKbd->isConsole) {
177        switch (pKbd->consType) {
178
179#if defined(SYSCONS_SUPPORT) || defined(PCCONS_SUPPORT) || defined(PCVT_SUPPORT) || defined(WSCONS_SUPPORT)
180	    case SYSCONS:
181	    case PCCONS:
182	    case PCVT:
183#ifdef WSCONS_SUPPORT
184            case WSCONS:
185#endif
186		 nTty = priv->kbdtty;
187		 nTty.c_iflag = IGNPAR | IGNBRK;
188		 nTty.c_oflag = 0;
189		 nTty.c_cflag = CREAD | CS8;
190		 nTty.c_lflag = 0;
191		 nTty.c_cc[VTIME] = 0;
192		 nTty.c_cc[VMIN] = 1;
193		 cfsetispeed(&nTty, 9600);
194		 cfsetospeed(&nTty, 9600);
195		 if (tcsetattr(pInfo->fd, TCSANOW, &nTty) < 0) {
196			 xf86Msg(X_ERROR, "KbdOn: tcsetattr: %s\n",
197			     strerror(errno));
198		 }
199                 break;
200#endif
201        }
202#if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT) || defined (WSCONS_SUPPORT)
203        switch (pKbd->consType) {
204	    case SYSCONS:
205	    case PCVT:
206#ifdef K_CODE
207                 if (pKbd->CustomKeycodes)
208		     ioctl(pInfo->fd, KDSKBMODE, K_CODE);
209	         else
210	             ioctl(pInfo->fd, KDSKBMODE, K_RAW);
211#else
212		 ioctl(pInfo->fd, KDSKBMODE, K_RAW);
213#endif
214	         break;
215#ifdef WSCONS_SUPPORT
216            case WSCONS:
217                 option = WSKBD_RAW;
218                 if (ioctl(pInfo->fd, WSKBDIO_SETMODE, &option) == -1) {
219			 FatalError("can't switch keyboard to raw mode. "
220				    "Enable support for it in the kernel\n"
221				    "or use for example:\n\n"
222				    "Option \"Protocol\" \"wskbd\"\n"
223				    "Option \"Device\" \"/dev/wskbd0\"\n"
224				    "\nin your xorg.conf(5) file\n");
225		 }
226		 break;
227#endif
228        }
229#endif
230    }
231    return Success;
232}
233
234static int
235KbdOff(InputInfoPtr pInfo, int what)
236{
237    KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
238    BsdKbdPrivPtr priv = (BsdKbdPrivPtr) pKbd->private;
239#ifdef WSCONS_SUPPORT
240    int option;
241#endif
242
243    if (pKbd->isConsole) {
244        switch (pKbd->consType) {
245#if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
246	    case SYSCONS:
247	    case PCVT:
248	         ioctl(pInfo->fd, KDSKBMODE, K_XLATE);
249	         /* FALL THROUGH */
250#endif
251#if defined(SYSCONS_SUPPORT) || defined(PCCONS_SUPPORT) || defined(PCVT_SUPPORT)
252	    case PCCONS:
253	         tcsetattr(pInfo->fd, TCSANOW, &(priv->kbdtty));
254	         break;
255#endif
256#ifdef WSCONS_SUPPORT
257            case WSCONS:
258                 option = WSKBD_TRANSLATED;
259                 ioctl(xf86Info.consoleFd, WSKBDIO_SETMODE, &option);
260                 tcsetattr(pInfo->fd, TCSANOW, &(priv->kbdtty));
261	         break;
262#endif
263        }
264    }
265    return Success;
266}
267
268static void
269SoundBell(InputInfoPtr pInfo, int loudness, int pitch, int duration)
270{
271    KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
272#ifdef WSCONS_SUPPORT
273    struct wskbd_bell_data wsb;
274#endif
275
276    if (loudness && pitch) {
277    	switch (pKbd->consType) {
278#ifdef PCCONS_SUPPORT
279	    case PCCONS:
280	         { int data[2];
281		   data[0] = pitch;
282		   data[1] = (duration * loudness) / 50;
283		   ioctl(pInfo->fd, CONSOLE_X_BELL, data);
284		   break;
285		 }
286#endif
287#if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
288	    case SYSCONS:
289	    case PCVT:
290		 ioctl(pInfo->fd, KDMKTONE,
291		 ((1193190 / pitch) & 0xffff) |
292		 (((unsigned long)duration*loudness/50)<<16));
293		 break;
294#endif
295#if defined (WSCONS_SUPPORT)
296            case WSCONS:
297                 wsb.which = WSKBD_BELL_DOALL;
298                 wsb.pitch = pitch;
299                 wsb.period = duration;
300                 wsb.volume = loudness;
301                 ioctl(pInfo->fd, WSKBDIO_COMPLEXBELL, &wsb);
302                 break;
303#endif
304	}
305    }
306}
307
308static void
309stdReadInput(InputInfoPtr pInfo)
310{
311    KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
312    unsigned char rBuf[64];
313    int nBytes, i;
314    if ((nBytes = read( pInfo->fd, (char *)rBuf, sizeof(rBuf))) > 0) {
315       for (i = 0; i < nBytes; i++)
316	   pKbd->PostEvent(pInfo, rBuf[i] & 0x7f,
317                           rBuf[i] & 0x80 ? FALSE : TRUE);
318       }
319}
320
321#ifdef WSCONS_SUPPORT
322
323static void
324WSReadInput(InputInfoPtr pInfo)
325{
326    KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
327    struct wscons_event events[64];
328    int type;
329    int blocked, n, i;
330
331    if ((n = read( pInfo->fd, events, sizeof(events))) > 0) {
332        n /=  sizeof(struct wscons_event);
333        for (i = 0; i < n; i++) {
334	    type = events[i].type;
335	    if (type == WSCONS_EVENT_KEY_UP || type == WSCONS_EVENT_KEY_DOWN) {
336		/* It seems better to block SIGIO there */
337		blocked = xf86BlockSIGIO();
338		pKbd->PostEvent(pInfo, (unsigned int)(events[i].value),
339				type == WSCONS_EVENT_KEY_DOWN ? TRUE : FALSE);
340		xf86UnblockSIGIO(blocked);
341	    }
342	} /* for */
343    }
344}
345
346static void
347printWsType(const char *type, char *name)
348{
349    xf86Msg(X_PROBED, "%s: Keyboard type: %s\n", name, type);
350}
351#endif
352
353static Bool
354OpenKeyboard(InputInfoPtr pInfo)
355{
356    KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
357    int i;
358    KbdProtocolId prot = PROT_UNKNOWN_KBD;
359    char *s;
360
361    s = xf86SetStrOption(pInfo->options, "Protocol", NULL);
362    for (i = 0; protocols[i].name; i++) {
363        if (xf86NameCmp(s, protocols[i].name) == 0) {
364           prot = protocols[i].id;
365           break;
366        }
367    }
368
369    switch (prot) {
370    	case PROT_STD:
371           pInfo->read_input = stdReadInput;
372           break;
373#ifdef WSCONS_SUPPORT
374        case PROT_WSCONS:
375           pInfo->read_input = WSReadInput;
376           break;
377#endif
378        default:
379           xf86Msg(X_ERROR,"\"%s\" is not a valid keyboard protocol name\n", s);
380           xfree(s);
381           return FALSE;
382    }
383    xf86Msg(X_CONFIG, "%s: Protocol: %s\n", pInfo->name, s);
384    xfree(s);
385
386    s = xf86SetStrOption(pInfo->options, "Device", NULL);
387    if (s == NULL) {
388       if (prot == PROT_WSCONS) {
389           xf86Msg(X_ERROR,"A \"device\" option is required with"
390                                  " the \"wskbd\" keyboard protocol\n");
391           return FALSE;
392       } else {
393           pInfo->fd = xf86Info.consoleFd;
394           pKbd->isConsole = TRUE;
395           pKbd->consType = xf86Info.consType;
396       }
397    } else {
398	pInfo->fd = open(s, O_RDONLY | O_NONBLOCK | O_EXCL);
399       if (pInfo->fd == -1) {
400           xf86Msg(X_ERROR, "%s: cannot open \"%s\"\n", pInfo->name, s);
401           xfree(s);
402           return FALSE;
403       }
404       pKbd->isConsole = FALSE;
405       pKbd->consType = xf86Info.consType;
406       xfree(s);
407    }
408
409#ifdef WSCONS_SUPPORT
410    if( prot == PROT_WSCONS) {
411       pKbd->consType = WSCONS;
412#ifdef WSKBDIO_SETVERSION
413       int version = WSKBDIO_EVENT_VERSION;
414       if (ioctl(pInfo->fd, WSKBDIO_SETVERSION, &version) == -1) {
415           xf86Msg(X_WARNING, "%s: cannot set version\n", pInfo->name);
416           return FALSE;
417       }
418#endif
419       /* Find out keyboard type */
420       if (ioctl(pInfo->fd, WSKBDIO_GTYPE, &(pKbd->wsKbdType)) == -1) {
421           xf86Msg(X_ERROR, "%s: cannot get keyboard type", pInfo->name);
422           close(pInfo->fd);
423           return FALSE;
424       }
425       switch (pKbd->wsKbdType) {
426           case WSKBD_TYPE_PC_XT:
427               printWsType("XT", pInfo->name);
428               break;
429           case WSKBD_TYPE_PC_AT:
430               printWsType("AT", pInfo->name);
431               break;
432           case WSKBD_TYPE_USB:
433               printWsType("USB", pInfo->name);
434               break;
435#ifdef WSKBD_TYPE_ADB
436           case WSKBD_TYPE_ADB:
437               printWsType("ADB", pInfo->name);
438               break;
439#endif
440#ifdef WSKBD_TYPE_MAPLE
441           case WSKBD_TYPE_MAPLE:
442               printWsType("Maple", pInfo->name);
443               break;
444#endif
445#ifdef WSKBD_TYPE_SUN
446           case WSKBD_TYPE_SUN:
447               printWsType("Sun", pInfo->name);
448               break;
449#endif
450#ifdef WSKBD_TYPE_SUN5
451     case WSKBD_TYPE_SUN5:
452               printWsType("Sun5", pInfo->name);
453               break;
454#endif
455           default:
456               xf86Msg(X_ERROR, "%s: Unsupported wskbd type \"%d\"",
457                                pInfo->name, pKbd->wsKbdType);
458               close(pInfo->fd);
459               return FALSE;
460       }
461    }
462#endif
463
464#if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT) || defined (WSCONS_SUPPORT)
465    if ((pKbd->isConsole &&
466         ((pKbd->consType == SYSCONS) || (pKbd->consType == PCVT))) ||
467	(pKbd->consType == WSCONS))
468        pKbd->vtSwitchSupported = TRUE;
469#endif
470
471    return TRUE;
472}
473
474_X_EXPORT Bool
475xf86OSKbdPreInit(InputInfoPtr pInfo)
476{
477    KbdDevPtr pKbd = pInfo->private;
478
479    pKbd->KbdInit	= KbdInit;
480    pKbd->KbdOn		= KbdOn;
481    pKbd->KbdOff	= KbdOff;
482    pKbd->Bell		= SoundBell;
483    pKbd->SetLeds	= SetKbdLeds;
484    pKbd->GetLeds	= GetKbdLeds;
485    pKbd->SetKbdRepeat	= SetKbdRepeat;
486    pKbd->KbdGetMapping	= KbdGetMapping;
487
488    pKbd->RemapScanCode = NULL;
489
490    pKbd->OpenKeyboard = OpenKeyboard;
491    pKbd->vtSwitchSupported = FALSE;
492    pKbd->CustomKeycodes = FALSE;
493
494    pKbd->private = xcalloc(sizeof(BsdKbdPrivRec), 1);
495    if (pKbd->private == NULL) {
496       xf86Msg(X_ERROR,"can't allocate keyboard OS private data\n");
497       return FALSE;
498    }
499    return TRUE;
500}
501