~ubuntu-branches/ubuntu/dapper/vino/dapper

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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Copyright (C) 2003 Sun Microsystems, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * Authors:
 *      Mark McLoughlin <mark@skynet.ie>
 *
 *
 *   The keyboard and pointer handling code is borrowed from
 *   x11vnc.c in libvncserver/contrib which is:
 *
 *     Copyright (c) 2002-2003 Karl J. Runge <runge@karlrunge.com>
 *
 *   x11vnc.c itself is based heavily on:
 *       the originial x11vnc.c in libvncserver (Johannes E. Schindelin)
 *       krfb, the KDE desktopsharing project (Tim Jansen)
 *       x0rfbserver, the original native X vnc server (Jens Wagner)
 */

#include <config.h>

#include "vino-input.h"

#include <string.h>
#include <gdk/gdkx.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#ifdef HAVE_XTEST
#include <X11/extensions/XTest.h>
#endif

#include "vino-util.h"

/* See <X11/keysymdef.h> - "Latin 1: Byte 3 = 0"
 */
#define VINO_IS_LATIN1_KEYSYM(k) ((k) != NoSymbol && ((k) & 0x0f00) == 0)

typedef enum
{
  VINO_LEFT_SHIFT  = 1 << 0,
  VINO_RIGHT_SHIFT = 1 << 1,
  VINO_ALT_GR      = 1 << 2
} VinoModifierState;

#define VINO_LEFT_OR_RIGHT_SHIFT (VINO_LEFT_SHIFT | VINO_RIGHT_SHIFT)

typedef struct
{
  guint8            button_mask;

  VinoModifierState modifier_state;
  guint8            modifiers [0x100];
  KeyCode           keycodes [0x100];
  KeyCode           left_shift_keycode;
  KeyCode           right_shift_keycode;
  KeyCode           alt_gr_keycode;

  guint             initialized : 1;
  guint             xtest_supported : 1;
} VinoInputData;

/* Data is per-display, but we only handle a single display.
 */
static VinoInputData global_input_data = { 0, };

/* Set up a keysym -> keycode + modifier mapping.
 *
 * RFB transmits the KeySym for a keypress, but we may only inject
 * keycodes using XTest. Thus, we must ensure that the modifier
 * state is such that the keycode we inject maps to the KeySym
 * we received from the client.
 */
#ifdef HAVE_XTEST
static void
vino_input_initialize_keycodes (GdkDisplay *display)
{
  Display *xdisplay;
  int      min_keycodes, max_keycodes;
  int      keysyms_per_keycode;
  KeySym  *keymap;
  int      keycode;

  xdisplay = GDK_DISPLAY_XDISPLAY (display);

  memset (global_input_data.keycodes,   0, sizeof (global_input_data.keycodes));
  memset (global_input_data.modifiers, -1, sizeof (global_input_data.modifiers));

  XDisplayKeycodes (xdisplay, &min_keycodes, &max_keycodes);

  g_assert (min_keycodes >= 8);
  g_assert (max_keycodes <= 255);

  keymap = XGetKeyboardMapping (xdisplay,
				min_keycodes,
				max_keycodes - min_keycodes + 1,
				&keysyms_per_keycode);

  g_assert (keymap != NULL);

  dprintf (INPUT, "Initializing keysym to keycode/modifier mapping\n");

  for (keycode = min_keycodes; keycode < max_keycodes; keycode++)
    {
      int    keycode_index = (keycode - min_keycodes) * keysyms_per_keycode;
      guint8 modifier;

      for (modifier = 0; modifier < keysyms_per_keycode; modifier++)
	{
	  guint32 keysym = keymap [keycode_index + modifier];

	  if (VINO_IS_LATIN1_KEYSYM (keysym) &&
	      XKeysymToKeycode (xdisplay, keysym) == keycode)
	    {
	      global_input_data.keycodes  [keysym] = keycode;
	      global_input_data.modifiers [keysym] = modifier;

	      dprintf (INPUT, "\t0x%.2x -> %d %d\n", keysym, keycode, modifier);
	    }
	}
    }

  XFree (keymap);

  global_input_data.left_shift_keycode  = XKeysymToKeycode (xdisplay, XK_Shift_L);
  global_input_data.right_shift_keycode = XKeysymToKeycode (xdisplay, XK_Shift_R);
  global_input_data.alt_gr_keycode      = XKeysymToKeycode (xdisplay, XK_Mode_switch);
}
#endif /* HAVE_XTEST */

gboolean
vino_input_init (GdkDisplay *display)
{
#ifdef HAVE_XTEST
  Display *xdisplay;
  int      ignore, *i = &ignore;

  g_assert (global_input_data.initialized != TRUE);

  xdisplay = GDK_DISPLAY_XDISPLAY (display);

  if (XTestQueryExtension (xdisplay, i, i, i, i))
    {
      XTestGrabControl (xdisplay, True);

      global_input_data.xtest_supported = TRUE;
    }

  vino_input_initialize_keycodes (display);

  global_input_data.initialized = TRUE;

  return global_input_data.xtest_supported;
#else
  return global_input_data.xtest_supported = FALSE;
#endif /* HAVE_XSHM */
}

void
vino_input_handle_pointer_event (GdkScreen *screen,
				 guint8     button_mask,
				 guint16    x,
				 guint16    y)
{
#ifdef HAVE_XTEST
  Display *xdisplay;
  guint8   prev_mask = global_input_data.button_mask;
  int      i;

  xdisplay = GDK_DISPLAY_XDISPLAY (gdk_screen_get_display (screen));

  XTestFakeMotionEvent (xdisplay,
			gdk_screen_get_number (screen),
			x, y,
			CurrentTime);
  
  dprintf (INPUT, "Injected motion event: %d, %d\n", x, y);

  for (i = 0; i < 5; i++)
    {
      gboolean button_down      = (button_mask & (1 << i)) != FALSE;
      gboolean prev_button_down = (prev_mask   & (1 << i)) != FALSE;

      if (button_down != prev_button_down)
	{
	  XTestFakeButtonEvent (xdisplay, i + 1, button_down, CurrentTime);
	  
	  dprintf (INPUT, "Injected button %d %s\n",
		   i + 1, button_down ? "press" : "release");
	}
    }

  global_input_data.button_mask = button_mask;
#endif /* HAVE_XTEST */
}

#ifdef HAVE_XTEST
static inline void
vino_input_update_modifier_state (VinoInputData    *input_data,
				  VinoModifierState state,
				  guint32           check_keysym,
				  guint32           keysym,
				  gboolean          key_press)
{
  if (keysym == check_keysym)
    {
      if (key_press)
	input_data->modifier_state |= state;
      else
	input_data->modifier_state &= ~state;
    }
}

static void
vino_input_fake_modifier (GdkScreen         *screen,
			  VinoInputData     *input_data,
			  guint8             modifier,
			  gboolean           key_press)
{
  Display           *xdisplay;
  VinoModifierState  modifier_state = input_data->modifier_state;

  xdisplay = GDK_DISPLAY_XDISPLAY (gdk_screen_get_display (screen));

  if ((modifier_state & VINO_LEFT_OR_RIGHT_SHIFT) && modifier != 1)
    {
      dprintf (INPUT, "Shift is down, but we don't want it to be\n");

      if (modifier_state & VINO_LEFT_SHIFT)
	XTestFakeKeyEvent (xdisplay,
			   input_data->left_shift_keycode,
			   !key_press,
			   CurrentTime);
      
      if (modifier_state & VINO_RIGHT_SHIFT)
	XTestFakeKeyEvent (xdisplay,
			   input_data->right_shift_keycode,
			   !key_press,
			   CurrentTime);
    }

  if (!(modifier_state & VINO_LEFT_OR_RIGHT_SHIFT) && modifier == 1)
    {
      dprintf (INPUT, "Shift isn't down, but we want it to be\n");

      XTestFakeKeyEvent (xdisplay,
			 input_data->left_shift_keycode,
			 key_press,
			 CurrentTime);
    }
      
  if ((modifier_state & VINO_ALT_GR) && modifier != 2)
    {
      dprintf (INPUT, "Alt is down, but we don't want it to be\n");

      XTestFakeKeyEvent (xdisplay,
			 input_data->alt_gr_keycode,
			 !key_press,
			 CurrentTime);
    }
      
  if (!(modifier_state & VINO_ALT_GR) && modifier == 2)
    {
      dprintf (INPUT, "Alt isn't down, but we want it to be\n");

      XTestFakeKeyEvent (xdisplay,
			 input_data->alt_gr_keycode,
			 key_press,
			 CurrentTime);
    }
}
#endif /* HAVE_XTEST */

void
vino_input_handle_key_event (GdkScreen *screen,
			     guint32    keysym,
			     gboolean   key_press)
{
#ifdef HAVE_XTEST
  Display *xdisplay;
  
  xdisplay = GDK_DISPLAY_XDISPLAY (gdk_screen_get_display (screen));
  
  vino_input_update_modifier_state (&global_input_data,
				    VINO_LEFT_SHIFT, XK_Shift_L,
				    keysym, key_press);

  vino_input_update_modifier_state (&global_input_data,
				    VINO_RIGHT_SHIFT, XK_Shift_R,
				    keysym, key_press);

  vino_input_update_modifier_state (&global_input_data,
				    VINO_ALT_GR, XK_Mode_switch,
				    keysym, key_press);

  if (VINO_IS_LATIN1_KEYSYM (keysym))
    {
      KeyCode keycode  = global_input_data.keycodes [keysym];
      guint8  modifier = global_input_data.modifiers [keysym];

      if (keycode != NoSymbol)
	{
	  if (key_press)
	    vino_input_fake_modifier (screen, &global_input_data, modifier, TRUE);

	  dprintf (INPUT, "Injecting keysym 0x%.2x %s (keycode %d, modifier %d)\n",
		   keysym, key_press ? "press" : "release", keycode, modifier);

	  XTestFakeKeyEvent (xdisplay, keycode, key_press, CurrentTime);

	  if (key_press)
	    vino_input_fake_modifier (screen, &global_input_data, modifier, FALSE);
	}
    }
  else
    {
      KeyCode keycode;

      if ((keycode = XKeysymToKeycode (xdisplay, keysym)) != NoSymbol)
	{
	  dprintf (INPUT, "Injecting keysym 0x%.2x %s (keycode %d)\n",
		   keysym, key_press ? "press" : "release", keycode);

	  XTestFakeKeyEvent (xdisplay, keycode, key_press, CurrentTime);
	}
    }
#endif /* HAVE_XTEST */
}

void
vino_input_handle_clipboard_event (GdkScreen *screen,
				   char      *text,
				   int        len)
{
}