summaryrefslogtreecommitdiff
path: root/hw/xscreen/xs-input.c
blob: ad633157f6d72a8051cc3a7c6ce499edea14abcd (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
/**
 * Copyright 2006 Ori Bernstein
 *
 *    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.  Ori Bernstein makes no representations about
 *    the suitability of this software for any purpose.  It is provided "as
 *    is" without express or implied warranty.
 **/

#ifdef HAVE_XSCREEN_CONFIG_H
#include <xs-config.h>
#endif

#include <stdlib.h>

/* need to include Xmd before XCB stuff, or
 * things get redeclared.*/
#include <X11/Xmd.h>
#include <X11/XCB/xcb.h>
#include <X11/XCB/xcb_aux.h>
#include <X11/XCB/xproto.h>
#include <X11/XCB/shape.h>

#include "gcstruct.h"
#include "window.h"
#include "inputstr.h"
#include "scrnintstr.h"
#include "region.h"
#include "misc.h"

#include "mi.h"
#include "mipointer.h"

#include "xs-globals.h"

#define UNUSED __attribute__((__unused__))

/**
 * FIXME: these numbers stolen from Xnest. Are they correct?
 **/
#define MAX_BUTTONS 256

static DevicePtr xsPtr;
static DevicePtr xsKbd;

/**
 * DIX hook for processing input events.
 * Just hooks into the mi stuff.
 **/
void ProcessInputEvents()
{
    mieqProcessInputEvents();
    miPointerUpdate();
}

/*The backing server should have already filtered invalid modifiers*/
Bool LegalModifier(unsigned int key UNUSED, DevicePtr pDev UNUSED)
{
    return TRUE;
}

void OsVendorInit()
{
}

void xsChangePointerControl(DeviceIntPtr pDev UNUSED, PtrCtrl *ctl)
{
    XCBChangePointerControl(xsConnection, 
                            ctl->num, ctl->den,
                            ctl->threshold,
                            TRUE, TRUE);
}
/**
 * Manages initializing and setting up the pointer.
 **/
int xsPtrProc(DeviceIntPtr pDev, int state)
{
    CARD8 map[MAX_BUTTONS];
    XCBGetPointerMappingCookie c;
    XCBGetPointerMappingRep *r;
    int nmap;
    int i;

    switch (state)
    {
        case DEVICE_INIT: 
            c = XCBGetPointerMapping(xsConnection);
            r = XCBGetPointerMappingReply(xsConnection, c, NULL);
            nmap = r->map_len;
            for (i = 0; i <= nmap; i++)
                map[i] = i; /* buttons are already mapped */
            InitPointerDeviceStruct(&pDev->public, map, nmap,
                                    miPointerGetMotionEvents,
                                    xsChangePointerControl,
                                    miPointerGetMotionBufferSize());
            break;

        /* device is always on, so ignore DEVICE_ON, DEVICE_OFF*/
        default:
            break;
    }
    return Success;
}

/**
 * Keyboard callback functions
 **/

/* no-op function */
void xsBell(int vol UNUSED, DeviceIntPtr pDev UNUSED, pointer ctl UNUSED, int wtf_is_this UNUSED)
{
    return;
}

/*no-op function*/
void xsKbdCtl(DeviceIntPtr pDev UNUSED, KeybdCtrl *ctl UNUSED)
{
}


/**
 * Manages initializing and setting up the keyboard.
 **/
int xsKbdProc(DeviceIntPtr pDev, int state)
{   
    const XCBSetup              *setup;
    XCBGetKeyboardMappingCookie  mapcook;
    XCBGetKeyboardMappingRep    *maprep;
    XCBGetModifierMappingCookie  modcook;
    XCBGetModifierMappingRep    *modrep;
    XCBGetKeyboardControlCookie  ctlcook;
    XCBGetKeyboardControlRep    *ctlrep;


    XCBKEYCODE  min;
    XCBKEYCODE  max;
    XCBKEYSYM  *keysyms;
    XCBKEYCODE *modcodes;

    KeySymsRec  keys;
    CARD8       modmap[MAP_LENGTH] = {0};
    CARD8       keycode;
    int         i;
    int         j;

    setup = XCBGetSetup(xsConnection);
    switch (state) 
    {
        case DEVICE_INIT:
            min = setup->min_keycode;
            max = setup->max_keycode;

            /*do all the requests*/
            mapcook = XCBGetKeyboardMapping(xsConnection, min, max.id - min.id);
            modcook = XCBGetModifierMapping(xsConnection);
            ctlcook = XCBGetKeyboardControl(xsConnection);

            /*wait for the keyboard mapping*/
            maprep = XCBGetKeyboardMappingReply(xsConnection, mapcook, NULL);
            keysyms = XCBGetKeyboardMappingKeysyms(maprep);

            /* initialize the keycode list*/
            keys.minKeyCode = min.id;
            keys.maxKeyCode = max.id;
            keys.mapWidth = maprep->keysyms_per_keycode;
            keys.map = (KeySym *)keysyms;
            
            /*wait for the modifier mapping*/
            modrep = XCBGetModifierMappingReply(xsConnection, modcook, NULL);
            modcodes = XCBGetModifierMappingKeycodes(modrep);


            /*initialize the modifiers*/
            for (j = 0; j < 8; j++) {
                for (i = 0; i < modrep->keycodes_per_modifier; i++) {
                    keycode = modcodes[j * modrep->keycodes_per_modifier + i].id;
                    if (keycode != 0)
                        modmap[keycode] |= 1<<j;
                }
            }

            /*wait for the ctl values*/
            ctlrep = XCBGetKeyboardControlReply(xsConnection, ctlcook, NULL);
            /*initialize the auto repeats*/
            memmove(defaultKeyboardControl.autoRepeats,
                    ctlrep->auto_repeats,
                    sizeof(ctlrep->auto_repeats));

            InitKeyboardDeviceStruct(&pDev->public,
                                     &keys,
                                     modmap, 
                                     xsBell, 
                                     xsKbdCtl);

            break;
    }
    return Success;
}

/**
 * DIX hooks for initializing input and output.
 * XKB stuff is not supported yet, since it's currently missing in
 * XCB.
 **/

void xsBlockHandler(pointer blockData, OSTimePtr pTimeout, pointer pReadMask)
{
    /*handle events here*/
    XCBFlush(xsConnection);
}

void xsWakeupHandler(pointer blockData, int result, pointer pReadMask)
{
    /*handle events here*/
}

void InitInput(int argc, char *argv[])
{
    xsPtr = (DevicePtr) AddInputDevice(xsPtrProc, TRUE);
    xsKbd = (DevicePtr) AddInputDevice(xsKbdProc, TRUE);

    RegisterPointerDevice((DeviceIntPtr)xsPtr);
    RegisterKeyboardDevice((DeviceIntPtr)xsKbd);

    mieqInit(xsKbd, xsPtr);

    AddEnabledDevice(XCBGetFileDescriptor(xsConnection));
    RegisterBlockAndWakeupHandlers(xsBlockHandler, xsWakeupHandler, NULL);
}