1/*
2 * Copyright 1997,1998 by UCHIYAMA Yasushi
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of UCHIYAMA Yasushi not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission.  UCHIYAMA Yasushi makes no representations
11 * about the suitability of this software for any purpose.  It is provided
12 * "as is" without express or implied warranty.
13 *
14 * UCHIYAMA YASUSHI DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL UCHIYAMA YASUSHI BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
21 *
22 */
23
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
28#include <xorg-server.h>
29#include <X11/X.h>
30
31#include "compiler.h"
32
33#include "xf86.h"
34#include "xf86Priv.h"
35#include "xf86_OSlib.h"
36
37#include "xf86Xinput.h"
38#include "xf86OSKbd.h"
39#include "atKeynames.h"
40#include "xf86Keymap.h"
41
42#include <stdio.h>
43#include <errno.h>
44#include <sys/time.h>
45#include <sys/file.h>
46#include <assert.h>
47#include <mach.h>
48#include <sys/ioctl.h>
49
50typedef unsigned short kev_type;		/* kd event type */
51typedef unsigned char Scancode;
52
53struct mouse_motion {
54    short mm_deltaX;		/* units? */
55    short mm_deltaY;
56};
57
58typedef struct {
59    kev_type type;			/* see below */
60    struct timeval time;		/* timestamp */
61    union {				/* value associated with event */
62	boolean_t up;		/* MOUSE_LEFT .. MOUSE_RIGHT */
63	Scancode sc;		/* KEYBD_EVENT */
64	struct mouse_motion mmotion;	/* MOUSE_MOTION */
65    } value;
66} kd_event;
67
68/*
69 * kd_event ID's.
70 */
71#define MOUSE_LEFT	1		/* mouse left button up/down */
72#define MOUSE_MIDDLE	2
73#define MOUSE_RIGHT	3
74#define MOUSE_MOTION	4		/* mouse motion */
75#define KEYBD_EVENT	5		/* key up/down */
76
77/***********************************************************************
78 * Keyboard
79 **********************************************************************/
80static void
81SoundKbdBell(InputInfoPtr pInfo, int loudness,int pitch,int duration)
82{
83    return;
84}
85
86static void
87SetKbdLeds(InputInfoPtr pInfo, int leds)
88{
89    return;
90}
91
92static int
93GetKbdLeds(InputInfoPtr pInfo)
94{
95    return 0;
96}
97
98static void
99KbdGetMapping(InputInfoPtr pInfo, KeySymsPtr pKeySyms, CARD8 *pModMap)
100{
101    pKeySyms->map        = map;
102    pKeySyms->mapWidth   = GLYPHS_PER_KEY;
103    pKeySyms->minKeyCode = MIN_KEYCODE;
104    pKeySyms->maxKeyCode = MAX_KEYCODE;
105    return;
106}
107
108static int
109KbdOn(InputInfoPtr pInfo, int what)
110{
111    int data = 1;
112    if( ioctl( pInfo->fd, _IOW('k', 1, int),&data) < 0)
113	FatalError("Cannot set event mode on keyboard (%s)\n",strerror(errno));
114    return Success;
115}
116static int
117KbdOff(InputInfoPtr pInfo, int what)
118{
119    int data = 2;
120    if( ioctl( pInfo->fd, _IOW('k', 1, int),&data) < 0)
121	FatalError("can't reset keyboard mode (%s)\n",strerror(errno));
122    return Success;
123}
124
125static int
126KbdInit(InputInfoPtr pInfo, int what)
127{
128    return Success;
129}
130
131static void
132ReadInput(InputInfoPtr pInfo)
133{
134    KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
135    kd_event ke;
136    while( read(pInfo->fd, &ke, sizeof(ke)) == sizeof(ke) )
137	pKbd->PostEvent(pInfo, ke.value.sc & 0x7f, ke.value.sc & 0x80 ? FALSE : TRUE);
138}
139
140static Bool
141OpenKeyboard(InputInfoPtr pInfo)
142{
143    pInfo->fd = xf86Info.consoleFd;
144    return TRUE;
145}
146
147Bool
148xf86OSKbdPreInit(InputInfoPtr pInfo)
149{
150    KbdDevPtr pKbd = pInfo->private;
151
152    pKbd->KbdInit       = KbdInit;
153    pKbd->KbdOn         = KbdOn;
154    pKbd->KbdOff        = KbdOff;
155    pKbd->Bell          = SoundKbdBell;
156    pKbd->SetLeds       = SetKbdLeds;
157    pKbd->GetLeds       = GetKbdLeds;
158    pKbd->KbdGetMapping = KbdGetMapping;
159    pKbd->RemapScanCode = ATScancode;
160    pKbd->OpenKeyboard  = OpenKeyboard;
161    pKbd->private       = NULL;
162    pInfo->read_input   = ReadInput;
163    return TRUE;
164}
165