summaryrefslogtreecommitdiff
path: root/gtk/vncdisplaykeymap.c
blob: 1fde6ca86d03e4b96b8c3570398895b9719e9a9c (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * Copyright (C) 2008  Anthony Liguori <anthony@codemonkey.ws>
 * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
#include "vncdisplaykeymap.h"

#include "spice-util.h"

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "vnc-keymap"
#define VNC_DEBUG(message) SPICE_DEBUG(message);

/*
 * This table is taken from QEMU x_keymap.c, under the terms:
 *
 * Copyright (c) 2003 Fabrice Bellard
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/* keycode translation for sending ISO_Left_Send
 * to vncserver
 */
static struct {
	GdkKeymapKey *keys;
	gint n_keys;
	guint keyval;
} untranslated_keys[] = {{NULL, 0, GDK_Tab}};

static unsigned int ref_count_for_untranslated_keys = 0;

#if defined(GDK_WINDOWING_X11)
#include <gdk/gdkx.h>
#include <X11/XKBlib.h>
#include <stdbool.h>
#include <string.h>

/* Xorg Linux + kbd (offset + mangled XT keycodes) */
#include "vncdisplaykeymap_xorgkbd2xtkbd.c"
/* Xorg Linux + evdev (offset evdev keycodes) */
#include "vncdisplaykeymap_xorgevdev2xtkbd.c"
/* Xorg OS-X aka XQuartz (offset OS-X keycodes) */
#include "vncdisplaykeymap_xorgxquartz2xtkbd.c"
/* Xorg Cygwin aka XWin (offset + mangled XT keycodes) */
#include "vncdisplaykeymap_xorgxwin2xtkbd.c"

#define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)

static gboolean check_for_xwin(void)
{
	char *vendor = ServerVendor(GDK_DISPLAY());

	if (strstr(vendor, "Cygwin/X"))
		return TRUE;

	return FALSE;
}

static gboolean check_for_xquartz(void)
{
	int nextensions;
	int i;
	gboolean match = FALSE;
	char **extensions = XListExtensions(GDK_DISPLAY(), &nextensions);
	for (i = 0 ; extensions != NULL && i < nextensions ; i++) {
		if (strcmp(extensions[i], "Apple-WM") == 0 ||
		    strcmp(extensions[i], "Apple-DRI") == 0)
			match = TRUE;
	}
	if (extensions)
		XFreeExtensionList(extensions);

	return match;
}

const guint16 const *vnc_display_keymap_gdk2xtkbd_table(size_t *maplen)
{
	XkbDescPtr desc;
	const gchar *keycodes = NULL;

	/* There is no easy way to determine what X11 server
	 * and platform & keyboard driver is in use. Thus we
	 * do best guess heuristics.
	 *
	 * This will need more work for people with other
	 * X servers..... patches welcomed.
	 */

	desc = XkbGetKeyboard(GDK_DISPLAY(), XkbGBN_AllComponentsMask,
			      XkbUseCoreKbd);
	if (desc) {
		if (desc->names) {
			keycodes = gdk_x11_get_xatom_name(desc->names->keycodes);
			if (!keycodes)
				g_warning("could not lookup keycode name");
		}
		XkbFreeClientMap(desc, XkbGBN_AllComponentsMask, True);
	}

	if (check_for_xwin()) {
		VNC_DEBUG("Using xwin keycode mapping");
		*maplen = G_N_ELEMENTS(keymap_xorgxwin2xtkbd);
		return keymap_xorgxwin2xtkbd;
	} else if (check_for_xquartz()) {
		VNC_DEBUG("Using xquartz keycode mapping");
		*maplen = G_N_ELEMENTS(keymap_xorgxquartz2xtkbd);
		return keymap_xorgxquartz2xtkbd;
	} else if (keycodes && STRPREFIX(keycodes, "evdev_")) {
		VNC_DEBUG("Using evdev keycode mapping");
		*maplen = G_N_ELEMENTS(keymap_xorgevdev2xtkbd);
		return keymap_xorgevdev2xtkbd;
	} else if (keycodes && STRPREFIX(keycodes, "xfree86_")) {
		VNC_DEBUG("Using xfree86 keycode mapping");
		*maplen = G_N_ELEMENTS(keymap_xorgkbd2xtkbd);
		return keymap_xorgkbd2xtkbd;
	} else {
               g_warning("Unknown keycode mapping '%s'.\n"
			 "Please report to gtk-vnc-list@gnome.org\n"
                         "including the following information:\n"
                         "\n"
                         "  - Operating system\n"
                         "  - GTK build\n"
                         "  - X11 Server\n"
                         "  - xprop -root\n"
                         "  - xdpyinfo\n",
			 keycodes);
               return NULL;
	}
}

#elif defined(GDK_WINDOWING_WIN32)
/* Win32 native virtual keycodes */
#include "vncdisplaykeymap_win322xtkbd.c"

const guint16 const *vnc_display_keymap_gdk2xtkbd_table(size_t *maplen)
{
	VNC_DEBUG("Using Win32 virtual keycode mapping");
	*maplen = sizeof(keymap_win322xtkbd);
	return keymap_win322xtkbd;
}

#elif defined(GDK_WINDOWING_QUARTZ)
/* OS-X native keycodes */
#include "vncdisplaykeymap_osx2xtkbd.c"

const guint16 const *vnc_display_keymap_gdk2xtkbd_table(size_t *maplen)
{
	VNC_DEBUG("Using OS-X virtual keycode mapping");
	*maplen = sizeof(keymap_osx2xtkbd);
	return keymap_osx2xtkbd;
}


#else

const guint16 const *vnc_display_keymap_gdk2xtkbd_table(size_t *maplen)
{
	g_warning("Unsupported GDK Windowing platform.\n"
		  "Please report to gtk-vnc-list@gnome.org\n"
		  "including the following information:\n"
		  "\n"
		  "  - Operating system\n"
		  "  - GTK Windowing system build\n");
	return NULL;
}
#endif

guint16 vnc_display_keymap_gdk2xtkbd(const guint16 const *keycode_map,
                                     size_t keycode_maplen,
                                     guint16 keycode)
{
	if (!keycode_map)
		return 0;
	if (keycode >= keycode_maplen)
		return 0;
	return keycode_map[keycode];
}

/* Set the keymap entries */
void vnc_display_keyval_set_entries(void)
{
	size_t i;
	if (ref_count_for_untranslated_keys == 0)
		for (i = 0; i < sizeof(untranslated_keys) / sizeof(untranslated_keys[0]); i++)
			gdk_keymap_get_entries_for_keyval(gdk_keymap_get_default(),
							  untranslated_keys[i].keyval,
							  &untranslated_keys[i].keys,
							  &untranslated_keys[i].n_keys);
	ref_count_for_untranslated_keys++;
}

/* Free the keymap entries */
void vnc_display_keyval_free_entries(void)
{
	size_t i;

	if (ref_count_for_untranslated_keys == 0)
		return;

	ref_count_for_untranslated_keys--;
	if (ref_count_for_untranslated_keys == 0)
		for (i = 0; i < sizeof(untranslated_keys) / sizeof(untranslated_keys[0]); i++)
			g_free(untranslated_keys[i].keys);

}

/* Get the keyval from the keycode without the level. */
guint vnc_display_keyval_from_keycode(guint keycode, guint keyval)
{
	size_t i;
	for (i = 0; i < sizeof(untranslated_keys) / sizeof(untranslated_keys[0]); i++) {
		if (keycode == untranslated_keys[i].keys[0].keycode) {
			return untranslated_keys[i].keyval;
		}
	}

	return keyval;
}
/*
 * Local variables:
 *  c-indent-level: 8
 *  c-basic-offset: 8
 *  tab-width: 8
 * End:
 */