xref: /OK3568_Linux_fs/external/xserver/hw/dmx/input/dmxmap.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2003 Red Hat Inc., Durham, North Carolina.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * All Rights Reserved.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining
7*4882a593Smuzhiyun  * a copy of this software and associated documentation files (the
8*4882a593Smuzhiyun  * "Software"), to deal in the Software without restriction, including
9*4882a593Smuzhiyun  * without limitation on the rights to use, copy, modify, merge,
10*4882a593Smuzhiyun  * publish, distribute, sublicense, and/or sell copies of the Software,
11*4882a593Smuzhiyun  * and to permit persons to whom the Software is furnished to do so,
12*4882a593Smuzhiyun  * subject to the following conditions:
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the
15*4882a593Smuzhiyun  * next paragraph) shall be included in all copies or substantial
16*4882a593Smuzhiyun  * portions of the Software.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21*4882a593Smuzhiyun  * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
22*4882a593Smuzhiyun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23*4882a593Smuzhiyun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24*4882a593Smuzhiyun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25*4882a593Smuzhiyun  * SOFTWARE.
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun  * Authors:
30*4882a593Smuzhiyun  *   Rickard E. (Rik) Faith <faith@redhat.com>
31*4882a593Smuzhiyun  */
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /** \file
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * This file implements a mapping from remote XInput event types to Xdmx
36*4882a593Smuzhiyun  * XInput event types.
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * The exglobals.h file defines global server-side variables with names
39*4882a593Smuzhiyun  * Device* to be integers that hold the value of the type of the
40*4882a593Smuzhiyun  * server-side XInput extension event.
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  * The client-side X11/extensions/XInput.h file defines macros with THE
43*4882a593Smuzhiyun  * EXACT SAME Device* names!
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  * Using those macros to extract remote server event type values from
46*4882a593Smuzhiyun  * the (opaque) XDevice structure is appropriate, but makes a direct
47*4882a593Smuzhiyun  * mapping to the Device* integers impossible.  So we use the normalized
48*4882a593Smuzhiyun  * XI_Device* names for these routines.
49*4882a593Smuzhiyun  */
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #ifdef HAVE_DMX_CONFIG_H
52*4882a593Smuzhiyun #include <dmx-config.h>
53*4882a593Smuzhiyun #endif
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun #include "dmxinputinit.h"
56*4882a593Smuzhiyun #include "dmxmap.h"
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /** Create a mapping from \a remoteEvent to \a serverEvent. The \a
59*4882a593Smuzhiyun  * remoteEvent is the type returned from the remote server.  The \a
60*4882a593Smuzhiyun  * serverEvent is from the XI_* list of events in
61*4882a593Smuzhiyun  * include/extensions/XIproto.h. */
62*4882a593Smuzhiyun void
dmxMapInsert(DMXLocalInputInfoPtr dmxLocal,int remoteEvent,int serverEvent)63*4882a593Smuzhiyun dmxMapInsert(DMXLocalInputInfoPtr dmxLocal, int remoteEvent, int serverEvent)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun     int hash = remoteEvent & DMX_MAP_MASK;
66*4882a593Smuzhiyun     int i;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun     /* Return if this has already been mapped */
69*4882a593Smuzhiyun     if (dmxLocal->map[hash].remote == remoteEvent
70*4882a593Smuzhiyun         && dmxLocal->map[hash].server == serverEvent)
71*4882a593Smuzhiyun         return;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun     if (dmxLocal->map[hash].remote) {
74*4882a593Smuzhiyun         dmxLocal->mapOptimize = 0;
75*4882a593Smuzhiyun         for (i = 0; i < DMX_MAP_ENTRIES; i++) {
76*4882a593Smuzhiyun             if (!dmxLocal->map[i].remote) {
77*4882a593Smuzhiyun                 dmxLocal->map[i].remote = remoteEvent;
78*4882a593Smuzhiyun                 dmxLocal->map[i].server = serverEvent;
79*4882a593Smuzhiyun                 return;
80*4882a593Smuzhiyun             }
81*4882a593Smuzhiyun         }
82*4882a593Smuzhiyun         dmxLog(dmxWarning,
83*4882a593Smuzhiyun                "Out of map entries, cannot map remove event type %d\n",
84*4882a593Smuzhiyun                remoteEvent);
85*4882a593Smuzhiyun     }
86*4882a593Smuzhiyun     else {
87*4882a593Smuzhiyun         dmxLocal->map[hash].remote = remoteEvent;
88*4882a593Smuzhiyun         dmxLocal->map[hash].server = serverEvent;
89*4882a593Smuzhiyun     }
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /** Remove all mappings there were inserted with #dmxMapInsert. */
93*4882a593Smuzhiyun void
dmxMapClear(DMXLocalInputInfoPtr dmxLocal)94*4882a593Smuzhiyun dmxMapClear(DMXLocalInputInfoPtr dmxLocal)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun     int i;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun     for (i = 0; i < DMX_MAP_ENTRIES; i++)
99*4882a593Smuzhiyun         dmxLocal->map[i].remote = 0;
100*4882a593Smuzhiyun     dmxLocal->mapOptimize = 1;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /** Lookup a mapping for \a remoteEvent.  The \a remoteEvent is the type
104*4882a593Smuzhiyun  * returned from the remote server.  The return value is that which was
105*4882a593Smuzhiyun  * passed into #dmxMapInsert (i.e., a value from the XI_* list in
106*4882a593Smuzhiyun  * include/extensions/XIproto.h).  If a mapping is not available, -1 is
107*4882a593Smuzhiyun  * returned. */
108*4882a593Smuzhiyun int
dmxMapLookup(DMXLocalInputInfoPtr dmxLocal,int remoteEvent)109*4882a593Smuzhiyun dmxMapLookup(DMXLocalInputInfoPtr dmxLocal, int remoteEvent)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun     int hash = remoteEvent & DMX_MAP_MASK;
112*4882a593Smuzhiyun     int serverEvent = -1;
113*4882a593Smuzhiyun     int i;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun     if (dmxLocal->mapOptimize && dmxLocal->map[hash].remote == remoteEvent) {
116*4882a593Smuzhiyun         serverEvent = dmxLocal->map[hash].server;
117*4882a593Smuzhiyun     }
118*4882a593Smuzhiyun     else {
119*4882a593Smuzhiyun         for (i = 0; i < DMX_MAP_ENTRIES; i++)
120*4882a593Smuzhiyun             if (dmxLocal->map[i].remote == remoteEvent) {
121*4882a593Smuzhiyun                 serverEvent = dmxLocal->map[hash].server;
122*4882a593Smuzhiyun                 break;
123*4882a593Smuzhiyun             }
124*4882a593Smuzhiyun     }
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun     return serverEvent;
127*4882a593Smuzhiyun }
128