2
Copyright (c) 2009-2010 Jay Sorg
4
Permission is hereby granted, free of charge, to any person obtaining a
5
copy of this software and associated documentation files (the "Software"),
6
to deal in the Software without restriction, including without limitation
7
the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
and/or sell copies of the Software, and to permit persons to whom the
9
Software is furnished to do so, subject to the following conditions:
11
The above copyright notice and this permission notice shall be included
12
in all copies or substantial portions of the Software.
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20
DEALINGS IN THE SOFTWARE.
27
#include <X11/Xutil.h>
28
#include <X11/keysym.h>
29
#include <freerdp/freerdp.h>
30
#include <freerdp/kbd.h>
33
#include "xf_keyboard.h"
35
static int xf_kb_keyboard_layout = 0;
36
static RD_BOOL pressed_keys[256] = { False };
39
xf_kb_init(unsigned int keyboard_layout_id)
41
xf_kb_keyboard_layout = freerdp_kbd_init(keyboard_layout_id);
42
printf("freerdp_kbd_init: %X\n", xf_kb_keyboard_layout);
46
xf_kb_inst_init(xfInfo * xfi)
48
xfi->inst->settings->keyboard_layout = xf_kb_keyboard_layout;
52
xf_kb_send_key(xfInfo * xfi, int flags, uint8 keycode)
56
if (keycode == xfi->pause_key)
58
/* This is a special key the actually sends two scancodes to the
59
server. It looks like Control - NumLock but with special flags. */
60
//printf("special VK_PAUSE\n");
61
if (flags & KBD_FLAG_UP)
63
xfi->inst->rdp_send_input(xfi->inst, RDP_INPUT_SCANCODE, 0x8200, 0x1d, 0);
64
xfi->inst->rdp_send_input(xfi->inst, RDP_INPUT_SCANCODE, 0x8000, 0x45, 0);
68
xfi->inst->rdp_send_input(xfi->inst, RDP_INPUT_SCANCODE, 0x0200, 0x1d, 0);
69
xfi->inst->rdp_send_input(xfi->inst, RDP_INPUT_SCANCODE, 0x0000, 0x45, 0);
74
scancode = freerdp_kbd_get_scancode_by_keycode(keycode, &flags);
77
printf("xf_kb_send_key: unable to get scancode (keycode=%d)\n", keycode);
81
xfi->inst->rdp_send_input(xfi->inst, RDP_INPUT_SCANCODE, flags, scancode, 0);
87
xf_kb_read_keyboard_state(xfInfo * xfi)
93
XQueryPointer(xfi->display, xfi->wnd, &wdummy, &wdummy, &dummy, &dummy,
94
&dummy, &dummy, &state);
99
xf_kb_get_key_state(xfInfo * xfi, int state, int keysym)
101
int modifierpos, key, keysymMask = 0;
103
KeyCode keycode = XKeysymToKeycode(xfi->display, keysym);
105
if (keycode == NoSymbol)
109
for (modifierpos = 0; modifierpos < 8; modifierpos++)
111
offset = xfi->mod_map->max_keypermod * modifierpos;
112
for (key = 0; key < xfi->mod_map->max_keypermod; key++)
114
if (xfi->mod_map->modifiermap[offset + key] == keycode)
116
keysymMask |= 1 << modifierpos;
120
return (state & keysymMask) ? True : False;
124
xf_kb_get_toggle_keys_state(xfInfo * xfi)
126
int toggle_keys_state = 0;
129
state = xf_kb_read_keyboard_state(xfi);
130
if (xf_kb_get_key_state(xfi, state, XK_Scroll_Lock))
132
toggle_keys_state |= KBD_SYNC_SCROLL_LOCK;
134
if (xf_kb_get_key_state(xfi, state, XK_Num_Lock))
136
toggle_keys_state |= KBD_SYNC_NUM_LOCK;
138
if (xf_kb_get_key_state(xfi, state, XK_Caps_Lock))
140
toggle_keys_state |= KBD_SYNC_CAPS_LOCK;
142
if (xf_kb_get_key_state(xfi, state, XK_Kana_Lock))
144
toggle_keys_state |= KBD_SYNC_KANA_LOCK;
146
return toggle_keys_state;
150
xf_kb_focus_in(xfInfo * xfi)
155
/* on focus in send a tab up like mstsc.exe */
156
scancode = freerdp_kbd_get_scancode_by_virtualkey(xfi->tab_key);
157
xfi->inst->rdp_send_input(xfi->inst, RDP_INPUT_SCANCODE, KBD_FLAG_UP, scancode, 0);
158
/* sync num, caps, scroll, kana lock */
159
flags = xf_kb_get_toggle_keys_state(xfi);
160
xfi->inst->rdp_sync_input(xfi->inst, flags);
164
xf_kb_set_keypress(uint8 keycode, KeySym keysym)
166
if (keycode < 8 || keycode > 255)
168
pressed_keys[keycode] = keysym;
172
xf_kb_unset_keypress(uint8 keycode)
174
if (keycode < 8 || keycode > 255)
176
pressed_keys[keycode] = NoSymbol;
180
xf_kb_key_pressed(xfInfo * xfi, KeySym keysym)
182
KeyCode keycode = XKeysymToKeycode(xfi->display, keysym);
183
return pressed_keys[keycode] == keysym;
187
xf_kb_handle_special_keys(xfInfo * xfi, KeySym keysym)
189
if (keysym == XK_Return)
191
if ((xf_kb_key_pressed(xfi, XK_Alt_L) || xf_kb_key_pressed(xfi, XK_Alt_R))
192
&& (xf_kb_key_pressed(xfi, XK_Control_L) || xf_kb_key_pressed(xfi, XK_Control_R)))
194
/* Ctrl-Alt-Enter: toggle full screen */
195
xf_toggle_fullscreen(xfi);