Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / server / X11 / xf_input.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * X11 Server Input
4  *
5  * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <X11/Xlib.h>
21 #include <freerdp/kbd/kbd.h>
22
23 #include "xf_input.h"
24
25 void xf_input_synchronize_event(rdpInput* input, uint32 flags)
26 {
27         printf("Client sent a synchronize event (flags:0x%X)\n", flags);
28 }
29
30 void xf_input_keyboard_event(rdpInput* input, uint16 flags, uint16 code)
31 {
32         unsigned int keycode;
33         boolean extended = false;
34         xfPeerContext* xfp = (xfPeerContext*) input->context;
35         xfInfo* xfi = xfp->info;
36
37         if (flags & KBD_FLAGS_EXTENDED)
38                 extended = true;
39
40         keycode = freerdp_kbd_get_keycode_by_scancode(code, extended);
41
42         if (keycode != 0)
43         {
44 #ifdef WITH_XTEST
45                 pthread_mutex_lock(&(xfp->mutex));
46
47                 if (flags & KBD_FLAGS_DOWN)
48                         XTestFakeKeyEvent(xfi->display, keycode, True, 0);
49                 else if (flags & KBD_FLAGS_RELEASE)
50                         XTestFakeKeyEvent(xfi->display, keycode, False, 0);
51
52                 pthread_mutex_unlock(&(xfp->mutex));
53 #endif
54         }
55 }
56
57 void xf_input_unicode_keyboard_event(rdpInput* input, uint16 flags, uint16 code)
58 {
59         printf("Client sent a unicode keyboard event (flags:0x%X code:0x%X)\n", flags, code);
60 }
61
62 void xf_input_mouse_event(rdpInput* input, uint16 flags, uint16 x, uint16 y)
63 {
64         int button = 0;
65         boolean down = false;
66         xfPeerContext* xfp = (xfPeerContext*) input->context;
67         xfInfo* xfi = xfp->info;
68
69         pthread_mutex_lock(&(xfp->mutex));
70 #ifdef WITH_XTEST
71
72         if (flags & PTR_FLAGS_WHEEL)
73         {
74                 boolean negative = false;
75
76                 if (flags & PTR_FLAGS_WHEEL_NEGATIVE)
77                         negative = true;
78
79                 button = (negative) ? 5 : 4;
80
81                 XTestFakeButtonEvent(xfi->display, button, True, 0);
82                 XTestFakeButtonEvent(xfi->display, button, False, 0);
83         }
84         else
85         {
86                 if (flags & PTR_FLAGS_MOVE)
87                         XTestFakeMotionEvent(xfi->display, 0, x, y, 0);
88
89                 if (flags & PTR_FLAGS_BUTTON1)
90                         button = 1;
91                 else if (flags & PTR_FLAGS_BUTTON2)
92                         button = 3;
93                 else if (flags & PTR_FLAGS_BUTTON3)
94                         button = 2;
95
96                 if (flags & PTR_FLAGS_DOWN)
97                         down = true;
98
99                 if (button != 0)
100                         XTestFakeButtonEvent(xfi->display, button, down, 0);
101         }
102 #endif
103         pthread_mutex_unlock(&(xfp->mutex));
104 }
105
106 void xf_input_extended_mouse_event(rdpInput* input, uint16 flags, uint16 x, uint16 y)
107 {
108         xfPeerContext* xfp = (xfPeerContext*) input->context;
109         xfInfo* xfi = xfp->info;
110
111         pthread_mutex_lock(&(xfp->mutex));
112 #ifdef WITH_XTEST
113         XTestFakeMotionEvent(xfi->display, 0, x, y, CurrentTime);
114 #endif
115         pthread_mutex_unlock(&(xfp->mutex));
116 }
117
118 void xf_input_register_callbacks(rdpInput* input)
119 {
120         input->SynchronizeEvent = xf_input_synchronize_event;
121         input->KeyboardEvent = xf_input_keyboard_event;
122         input->UnicodeKeyboardEvent = xf_input_unicode_keyboard_event;
123         input->MouseEvent = xf_input_mouse_event;
124         input->ExtendedMouseEvent = xf_input_extended_mouse_event;
125 }