• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

krfb

  • sources
  • kde-4.14
  • kdenetwork
  • krfb
  • krfb
events.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the KDE project
3 
4  Copyright (C) 2010 Collabora Ltd.
5  @author George Kiagiadakis <george.kiagiadakis@collabora.co.uk>
6  Copyright (C) 2007 Alessandro Praduroux <pradu@pradu.it>
7  Copyright (C) 2001-2003 by Tim Jansen <tim@tjansen.de>
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public
11  License as published by the Free Software Foundation; either
12  version 2 of the License, or (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; see the file COPYING. If not, write to
21  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  Boston, MA 02110-1301, USA.
23 */
24 
25 #include "events.h"
26 
27 #include <QtGui/QApplication>
28 #include <QtGui/QX11Info>
29 #include <QtGui/QDesktopWidget>
30 #include <KGlobal>
31 
32 #include <X11/Xutil.h>
33 #include <X11/keysym.h>
34 #include <X11/extensions/XTest.h>
35 
36 enum {
37  LEFTSHIFT = 1,
38  RIGHTSHIFT = 2,
39  ALTGR = 4
40 };
41 
42 class EventData
43 {
44 public:
45  EventData();
46 
47  //keyboard
48  Display *dpy;
49  signed char modifiers[0x100];
50  KeyCode keycodes[0x100];
51  KeyCode leftShiftCode;
52  KeyCode rightShiftCode;
53  KeyCode altGrCode;
54  char modifierState;
55 
56  //mouse
57  int buttonMask;
58 
59 private:
60  void init();
61 };
62 
63 K_GLOBAL_STATIC(EventData, data)
64 
65 EventData::EventData()
66 {
67  init();
68 }
69 
70 void EventData::init()
71 {
72  dpy = QX11Info::display();
73  buttonMask = 0;
74 
75  //initialize keycodes
76  KeySym key, *keymap;
77  int i, j, minkey, maxkey, syms_per_keycode;
78 
79  memset(modifiers, -1, sizeof(modifiers));
80 
81  XDisplayKeycodes(dpy, &minkey, &maxkey);
82  Q_ASSERT(minkey >= 8);
83  Q_ASSERT(maxkey < 256);
84  keymap = (KeySym *) XGetKeyboardMapping(dpy, minkey,
85  (maxkey - minkey + 1),
86  &syms_per_keycode);
87  Q_ASSERT(keymap);
88 
89  for (i = minkey; i <= maxkey; i++) {
90  for (j = 0; j < syms_per_keycode; j++) {
91  key = keymap[(i-minkey)*syms_per_keycode+j];
92 
93  if (key >= ' ' && key < 0x100 && i == XKeysymToKeycode(dpy, key)) {
94  keycodes[key] = i;
95  modifiers[key] = j;
96  }
97  }
98  }
99 
100  leftShiftCode = XKeysymToKeycode(dpy, XK_Shift_L);
101  rightShiftCode = XKeysymToKeycode(dpy, XK_Shift_R);
102  altGrCode = XKeysymToKeycode(dpy, XK_Mode_switch);
103 
104  XFree((char *)keymap);
105 }
106 
107 /* this function adjusts the modifiers according to mod (as from modifiers) and data->modifierState */
108 static void tweakModifiers(signed char mod, bool down)
109 {
110  bool isShift = data->modifierState & (LEFTSHIFT | RIGHTSHIFT);
111 
112  if (mod < 0) {
113  return;
114  }
115 
116  if (isShift && mod != 1) {
117  if (data->modifierState & LEFTSHIFT) {
118  XTestFakeKeyEvent(data->dpy, data->leftShiftCode,
119  down, CurrentTime);
120  }
121 
122  if (data->modifierState & RIGHTSHIFT) {
123  XTestFakeKeyEvent(data->dpy, data->rightShiftCode,
124  down, CurrentTime);
125  }
126  }
127 
128  if (!isShift && mod == 1) {
129  XTestFakeKeyEvent(data->dpy, data->leftShiftCode,
130  down, CurrentTime);
131  }
132 
133  if ((data->modifierState & ALTGR) && mod != 2) {
134  XTestFakeKeyEvent(data->dpy, data->altGrCode,
135  !down, CurrentTime);
136  }
137 
138  if (!(data->modifierState & ALTGR) && mod == 2) {
139  XTestFakeKeyEvent(data->dpy, data->altGrCode,
140  down, CurrentTime);
141  }
142 }
143 
144 void EventHandler::handleKeyboard(bool down, rfbKeySym keySym)
145 {
146 #define ADJUSTMOD(sym,state) \
147  if(keySym==sym) { if(down) data->modifierState|=state; else data->modifierState&=~state; }
148 
149  ADJUSTMOD(XK_Shift_L, LEFTSHIFT);
150  ADJUSTMOD(XK_Shift_R, RIGHTSHIFT);
151  ADJUSTMOD(XK_Mode_switch, ALTGR);
152 
153  if (keySym >= ' ' && keySym < 0x100) {
154  KeyCode k;
155 
156  if (down) {
157  tweakModifiers(data->modifiers[keySym], True);
158  }
159 
160  k = data->keycodes[keySym];
161 
162  if (k != NoSymbol) {
163  XTestFakeKeyEvent(data->dpy, k, down, CurrentTime);
164  }
165 
166  if (down) {
167  tweakModifiers(data->modifiers[keySym], False);
168  }
169  } else {
170  KeyCode k = XKeysymToKeycode(data->dpy, keySym);
171 
172  if (k != NoSymbol) {
173  XTestFakeKeyEvent(data->dpy, k, down, CurrentTime);
174  }
175  }
176 }
177 
178 void EventHandler::handlePointer(int buttonMask, int x, int y)
179 {
180  QDesktopWidget *desktopWidget = QApplication::desktop();
181 
182  int screen = desktopWidget->screenNumber();
183 
184  if (screen < 0) {
185  screen = 0;
186  }
187 
188  XTestFakeMotionEvent(data->dpy, screen, x, y, CurrentTime);
189 
190  for (int i = 0; i < 5; i++) {
191  if ((data->buttonMask&(1 << i)) != (buttonMask&(1 << i))) {
192  XTestFakeButtonEvent(data->dpy,
193  i + 1,
194  (buttonMask&(1 << i)) ? True : False,
195  CurrentTime);
196  }
197  }
198 
199  data->buttonMask = buttonMask;
200 }
tweakModifiers
static void tweakModifiers(signed char mod, bool down)
Definition: events.cpp:108
RIGHTSHIFT
Definition: events.cpp:38
QX11Info::display
Display * display()
XK_Shift_R
#define XK_Shift_R
Definition: keysym.h:313
QDesktopWidget::screenNumber
int screenNumber(const QWidget *widget) const
rfbKeySym
uint32_t rfbKeySym
Definition: rfbproto.h:109
EventHandler::handleKeyboard
static void handleKeyboard(bool down, rfbKeySym key)
Definition: events.cpp:144
QDesktopWidget
events.h
XK_Shift_L
#define XK_Shift_L
Definition: keysym.h:312
LEFTSHIFT
Definition: events.cpp:37
ALTGR
Definition: events.cpp:39
QApplication::desktop
QDesktopWidget * desktop()
XK_Mode_switch
#define XK_Mode_switch
Definition: keysym.h:195
ADJUSTMOD
#define ADJUSTMOD(sym, state)
EventHandler::handlePointer
static void handlePointer(int buttonMask, int x, int y)
Definition: events.cpp:178
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:29:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

krfb

Skip menu "krfb"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal