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