2011-08-07 21:41:54 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-08-07 21:41:54 +04:00
|
|
|
* X11 Keyboard Handling
|
|
|
|
*
|
|
|
|
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:20:53 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-08-07 21:41:54 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-11-22 06:22:06 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2011-08-07 21:41:54 +04:00
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/Xutil.h>
|
|
|
|
#include <X11/keysym.h>
|
|
|
|
|
2012-11-29 10:33:19 +04:00
|
|
|
#include <freerdp/locale/keyboard.h>
|
|
|
|
|
2011-08-07 21:41:54 +04:00
|
|
|
#include "xf_keyboard.h"
|
|
|
|
|
|
|
|
void xf_kbd_init(xfInfo* xfi)
|
|
|
|
{
|
2012-08-03 00:45:03 +04:00
|
|
|
xf_kbd_clear(xfi);
|
2012-11-07 19:33:06 +04:00
|
|
|
xfi->keyboard_layout_id = xfi->instance->settings->KeyboardLayout;
|
2012-02-19 07:04:28 +04:00
|
|
|
xfi->keyboard_layout_id = freerdp_keyboard_init(xfi->keyboard_layout_id);
|
2012-11-07 19:33:06 +04:00
|
|
|
xfi->instance->settings->KeyboardLayout = xfi->keyboard_layout_id;
|
2013-02-12 05:38:19 +04:00
|
|
|
xfi->modifier_map = XGetModifierMapping(xfi->display);
|
2011-08-07 21:41:54 +04:00
|
|
|
}
|
|
|
|
|
2012-08-03 00:45:03 +04:00
|
|
|
void xf_kbd_clear(xfInfo* xfi)
|
|
|
|
{
|
2013-02-10 22:17:08 +04:00
|
|
|
ZeroMemory(xfi->pressed_keys, 256 * sizeof(BOOL));
|
2012-08-03 00:45:03 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
void xf_kbd_set_keypress(xfInfo* xfi, BYTE keycode, KeySym keysym)
|
2011-08-07 21:41:54 +04:00
|
|
|
{
|
|
|
|
if (keycode >= 8)
|
|
|
|
xfi->pressed_keys[keycode] = keysym;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
void xf_kbd_unset_keypress(xfInfo* xfi, BYTE keycode)
|
2011-08-07 21:41:54 +04:00
|
|
|
{
|
|
|
|
if (keycode >= 8)
|
|
|
|
xfi->pressed_keys[keycode] = NoSymbol;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-11 08:37:47 +04:00
|
|
|
void xf_kbd_release_all_keypress(xfInfo* xfi)
|
|
|
|
{
|
|
|
|
int keycode;
|
2013-03-06 21:50:25 +04:00
|
|
|
DWORD rdp_scancode;
|
2012-04-11 08:37:47 +04:00
|
|
|
|
2012-11-22 05:50:28 +04:00
|
|
|
for (keycode = 0; keycode < ARRAYSIZE(xfi->pressed_keys); keycode++)
|
2012-04-11 08:37:47 +04:00
|
|
|
{
|
|
|
|
if (xfi->pressed_keys[keycode] != NoSymbol)
|
|
|
|
{
|
|
|
|
rdp_scancode = freerdp_keyboard_get_rdp_scancode_from_x11_keycode(keycode);
|
2012-10-09 10:31:28 +04:00
|
|
|
freerdp_input_send_keyboard_event_ex(xfi->instance->input, FALSE, rdp_scancode);
|
2012-04-11 08:37:47 +04:00
|
|
|
xfi->pressed_keys[keycode] = NoSymbol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL xf_kbd_key_pressed(xfInfo* xfi, KeySym keysym)
|
2011-08-07 21:41:54 +04:00
|
|
|
{
|
|
|
|
KeyCode keycode = XKeysymToKeycode(xfi->display, keysym);
|
|
|
|
return (xfi->pressed_keys[keycode] == keysym);
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
void xf_kbd_send_key(xfInfo* xfi, BOOL down, BYTE keycode)
|
2011-08-07 21:41:54 +04:00
|
|
|
{
|
2013-03-06 21:50:25 +04:00
|
|
|
DWORD rdp_scancode;
|
2011-08-07 21:41:54 +04:00
|
|
|
rdpInput* input;
|
|
|
|
|
|
|
|
input = xfi->instance->input;
|
2012-03-29 03:12:35 +04:00
|
|
|
rdp_scancode = freerdp_keyboard_get_rdp_scancode_from_x11_keycode(keycode);
|
2011-08-07 21:41:54 +04:00
|
|
|
|
2012-03-29 03:12:48 +04:00
|
|
|
if (rdp_scancode == RDP_SCANCODE_UNKNOWN)
|
2011-08-07 21:41:54 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Unknown key with X keycode 0x%02x\n", keycode);
|
2011-08-07 21:41:54 +04:00
|
|
|
}
|
2012-03-29 03:12:48 +04:00
|
|
|
else if (rdp_scancode == RDP_SCANCODE_PAUSE &&
|
2011-08-07 21:41:54 +04:00
|
|
|
!xf_kbd_key_pressed(xfi, XK_Control_L) && !xf_kbd_key_pressed(xfi, XK_Control_R))
|
|
|
|
{
|
|
|
|
/* Pause without Ctrl has to be sent as Ctrl + NumLock. */
|
|
|
|
if (down)
|
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
freerdp_input_send_keyboard_event_ex(input, TRUE, RDP_SCANCODE_LCONTROL);
|
|
|
|
freerdp_input_send_keyboard_event_ex(input, TRUE, RDP_SCANCODE_NUMLOCK);
|
|
|
|
freerdp_input_send_keyboard_event_ex(input, FALSE, RDP_SCANCODE_LCONTROL);
|
|
|
|
freerdp_input_send_keyboard_event_ex(input, FALSE, RDP_SCANCODE_NUMLOCK);
|
2011-08-07 21:41:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-22 22:27:30 +04:00
|
|
|
freerdp_input_send_keyboard_event_ex(input, down, rdp_scancode);
|
2011-08-07 21:41:54 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
if ((rdp_scancode == RDP_SCANCODE_CAPSLOCK) && (down == FALSE))
|
2011-08-07 21:41:54 +04:00
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 syncFlags;
|
2011-08-07 21:41:54 +04:00
|
|
|
syncFlags = xf_kbd_get_toggle_keys_state(xfi);
|
|
|
|
input->SynchronizeEvent(input, syncFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int xf_kbd_read_keyboard_state(xfInfo* xfi)
|
|
|
|
{
|
|
|
|
int dummy;
|
|
|
|
Window wdummy;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 state = 0;
|
2011-08-07 21:41:54 +04:00
|
|
|
|
2013-02-10 22:17:08 +04:00
|
|
|
if (!xfi->remote_app)
|
2011-08-18 09:16:49 +04:00
|
|
|
{
|
|
|
|
XQueryPointer(xfi->display, xfi->window->handle,
|
2011-08-07 21:41:54 +04:00
|
|
|
&wdummy, &wdummy, &dummy, &dummy, &dummy, &dummy, &state);
|
2011-08-18 09:16:49 +04:00
|
|
|
}
|
2012-02-28 03:04:11 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
XQueryPointer(xfi->display, DefaultRootWindow(xfi->display),
|
|
|
|
&wdummy, &wdummy, &dummy, &dummy, &dummy, &dummy, &state);
|
|
|
|
}
|
2011-08-07 21:41:54 +04:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL xf_kbd_get_key_state(xfInfo* xfi, int state, int keysym)
|
2011-08-07 21:41:54 +04:00
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
int modifierpos, key, keysymMask = 0;
|
|
|
|
KeyCode keycode = XKeysymToKeycode(xfi->display, keysym);
|
|
|
|
|
|
|
|
if (keycode == NoSymbol)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-07 21:41:54 +04:00
|
|
|
|
|
|
|
for (modifierpos = 0; modifierpos < 8; modifierpos++)
|
|
|
|
{
|
|
|
|
offset = xfi->modifier_map->max_keypermod * modifierpos;
|
2013-02-10 22:17:08 +04:00
|
|
|
|
2011-08-07 21:41:54 +04:00
|
|
|
for (key = 0; key < xfi->modifier_map->max_keypermod; key++)
|
|
|
|
{
|
|
|
|
if (xfi->modifier_map->modifiermap[offset + key] == keycode)
|
|
|
|
{
|
|
|
|
keysymMask |= 1 << modifierpos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return (state & keysymMask) ? TRUE : FALSE;
|
2011-08-07 21:41:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int xf_kbd_get_toggle_keys_state(xfInfo* xfi)
|
|
|
|
{
|
|
|
|
int state;
|
|
|
|
int toggle_keys_state = 0;
|
|
|
|
|
|
|
|
state = xf_kbd_read_keyboard_state(xfi);
|
2013-02-12 05:38:19 +04:00
|
|
|
|
2011-08-07 21:41:54 +04:00
|
|
|
if (xf_kbd_get_key_state(xfi, state, XK_Scroll_Lock))
|
|
|
|
toggle_keys_state |= KBD_SYNC_SCROLL_LOCK;
|
|
|
|
if (xf_kbd_get_key_state(xfi, state, XK_Num_Lock))
|
|
|
|
toggle_keys_state |= KBD_SYNC_NUM_LOCK;
|
|
|
|
if (xf_kbd_get_key_state(xfi, state, XK_Caps_Lock))
|
|
|
|
toggle_keys_state |= KBD_SYNC_CAPS_LOCK;
|
|
|
|
if (xf_kbd_get_key_state(xfi, state, XK_Kana_Lock))
|
|
|
|
toggle_keys_state |= KBD_SYNC_KANA_LOCK;
|
|
|
|
|
|
|
|
return toggle_keys_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void xf_kbd_focus_in(xfInfo* xfi)
|
|
|
|
{
|
|
|
|
rdpInput* input;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 syncFlags;
|
2013-04-15 14:14:09 +04:00
|
|
|
int dummy, mouseX, mouseY;
|
|
|
|
Window wdummy;
|
|
|
|
UINT32 state = 0;
|
2011-08-07 21:41:54 +04:00
|
|
|
|
|
|
|
input = xfi->instance->input;
|
|
|
|
|
|
|
|
syncFlags = xf_kbd_get_toggle_keys_state(xfi);
|
2013-04-15 14:14:09 +04:00
|
|
|
XQueryPointer(xfi->display, xfi->window->handle, &wdummy, &wdummy, &mouseX, &mouseY, &dummy, &dummy, &state);
|
|
|
|
|
|
|
|
input->FocusInEvent(input, syncFlags, mouseX, mouseY);
|
2011-08-07 21:41:54 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL xf_kbd_handle_special_keys(xfInfo* xfi, KeySym keysym)
|
2011-08-07 21:41:54 +04:00
|
|
|
{
|
|
|
|
if (keysym == XK_Return)
|
|
|
|
{
|
|
|
|
if ((xf_kbd_key_pressed(xfi, XK_Alt_L) || xf_kbd_key_pressed(xfi, XK_Alt_R))
|
|
|
|
&& (xf_kbd_key_pressed(xfi, XK_Control_L) || xf_kbd_key_pressed(xfi, XK_Control_R)))
|
|
|
|
{
|
|
|
|
/* Ctrl-Alt-Enter: toggle full screen */
|
|
|
|
xf_toggle_fullscreen(xfi);
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-07 21:41:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-06 23:43:34 +04:00
|
|
|
|
2013-05-05 05:23:24 +04:00
|
|
|
if (keysym == XK_period)
|
|
|
|
{
|
|
|
|
if ((xf_kbd_key_pressed(xfi, XK_Alt_L) || xf_kbd_key_pressed(xfi, XK_Alt_R))
|
|
|
|
&& (xf_kbd_key_pressed(xfi, XK_Control_L) || xf_kbd_key_pressed(xfi, XK_Control_R)))
|
|
|
|
{
|
|
|
|
//Zoom in (scale larger)
|
|
|
|
double s = xfi->scale;
|
|
|
|
s += 0.1;
|
|
|
|
if(s > 1.5)
|
|
|
|
s = 1.5;
|
|
|
|
|
|
|
|
xfi->scale = s;
|
|
|
|
|
2013-05-13 23:23:50 +04:00
|
|
|
XResizeWindow(xfi->display, xfi->window->handle, xfi->originalWidth * s, xfi->originalHeight * s);
|
|
|
|
IFCALL(xfi->client->OnResizeWindow, xfi->instance, xfi->originalWidth * s, xfi->originalHeight * s);
|
2013-05-05 05:38:34 +04:00
|
|
|
//xf_draw_screen_scaled(xfi);
|
2013-05-05 05:23:24 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keysym == XK_comma)
|
|
|
|
{
|
|
|
|
if ((xf_kbd_key_pressed(xfi, XK_Alt_L) || xf_kbd_key_pressed(xfi, XK_Alt_R))
|
|
|
|
&& (xf_kbd_key_pressed(xfi, XK_Control_L) || xf_kbd_key_pressed(xfi, XK_Control_R)))
|
|
|
|
{
|
|
|
|
//Zoom out (scale smaller)
|
|
|
|
double s = xfi->scale;
|
|
|
|
s -= 0.1;
|
|
|
|
if(s < 0.5)
|
|
|
|
s = 0.5;
|
|
|
|
|
|
|
|
xfi->scale = s;
|
|
|
|
|
2013-05-13 23:23:50 +04:00
|
|
|
XResizeWindow(xfi->display, xfi->window->handle, xfi->originalWidth * s, xfi->originalHeight * s);
|
|
|
|
IFCALL(xfi->client->OnResizeWindow, xfi->instance, xfi->originalWidth * s, xfi->originalHeight * s);
|
2013-05-05 05:38:34 +04:00
|
|
|
//xf_draw_screen_scaled(xfi);
|
2013-05-05 05:23:24 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-06 23:43:34 +04:00
|
|
|
|
2013-05-06 01:57:31 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-07 21:41:54 +04:00
|
|
|
}
|
|
|
|
|